1 /* Copyright (C) 2018-2019 Free Software Foundation, Inc. 2 3 This file is part of GDB. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include "common/common-defs.h" 19 #include "riscv.h" 20 #include <stdlib.h> 21 #include <unordered_map> 22 23 #include "../features/riscv/32bit-cpu.c" 24 #include "../features/riscv/64bit-cpu.c" 25 #include "../features/riscv/32bit-fpu.c" 26 #include "../features/riscv/64bit-fpu.c" 27 28 /* Wrapper used by std::unordered_map to generate hash for feature set. */ 29 struct riscv_gdbarch_features_hasher 30 { 31 std::size_t 32 operator() (const riscv_gdbarch_features &features) const noexcept 33 { 34 return features.hash (); 35 } 36 }; 37 38 /* Cache of previously seen target descriptions, indexed by the feature set 39 that created them. */ 40 static std::unordered_map<riscv_gdbarch_features, 41 target_desc *, 42 riscv_gdbarch_features_hasher> riscv_tdesc_cache; 43 44 /* See arch/riscv.h. */ 45 46 const target_desc * 47 riscv_create_target_description (struct riscv_gdbarch_features features) 48 { 49 /* Have we seen this feature set before? If we have return the same 50 target description. GDB expects that if two target descriptions are 51 the same (in content terms) then they will actually be the same 52 instance. This is important when trying to lookup gdbarch objects as 53 GDBARCH_LIST_LOOKUP_BY_INFO performs a pointer comparison on target 54 descriptions to find candidate gdbarch objects. */ 55 const auto it = riscv_tdesc_cache.find (features); 56 if (it != riscv_tdesc_cache.end ()) 57 return it->second; 58 59 /* Now we should create a new target description. */ 60 target_desc *tdesc = allocate_target_description (); 61 62 #ifndef IN_PROCESS_AGENT 63 std::string arch_name = "riscv"; 64 65 if (features.xlen == 4) 66 arch_name.append (":rv32i"); 67 else if (features.xlen == 8) 68 arch_name.append (":rv64i"); 69 else if (features.xlen == 16) 70 arch_name.append (":rv128i"); 71 72 if (features.flen == 4) 73 arch_name.append ("f"); 74 else if (features.flen == 8) 75 arch_name.append ("d"); 76 else if (features.flen == 16) 77 arch_name.append ("q"); 78 79 set_tdesc_architecture (tdesc, arch_name.c_str ()); 80 #endif 81 82 long regnum = 0; 83 84 /* For now we only support creating 32-bit or 64-bit x-registers. */ 85 if (features.xlen == 4) 86 regnum = create_feature_riscv_32bit_cpu (tdesc, regnum); 87 else if (features.xlen == 8) 88 regnum = create_feature_riscv_64bit_cpu (tdesc, regnum); 89 90 /* For now we only support creating 32-bit or 64-bit f-registers. */ 91 if (features.flen == 4) 92 regnum = create_feature_riscv_32bit_fpu (tdesc, regnum); 93 else if (features.flen == 8) 94 regnum = create_feature_riscv_64bit_fpu (tdesc, regnum); 95 96 /* Add to the cache. */ 97 riscv_tdesc_cache.emplace (features, tdesc); 98 99 return tdesc; 100 } 101