[gdb/testsuite] Fix gdb.python/py-inferior.exp with -fsanitize=thread
[binutils-gdb.git] / gdb / arch / riscv.c
blob9a84844a64569a06e27a27506a16cf0016562c3b
1 /* Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
18 #include "riscv.h"
19 #include <stdlib.h>
20 #include <unordered_map>
22 #include "../features/riscv/32bit-cpu.c"
23 #include "../features/riscv/64bit-cpu.c"
24 #include "../features/riscv/32bit-fpu.c"
25 #include "../features/riscv/64bit-fpu.c"
26 #include "../features/riscv/rv32e-xregs.c"
28 #ifndef GDBSERVER
29 #define STATIC_IN_GDB static
30 #else
31 #define STATIC_IN_GDB
32 #endif
34 /* See arch/riscv.h. */
36 STATIC_IN_GDB target_desc_up
37 riscv_create_target_description (const struct riscv_gdbarch_features features)
39 /* Now we should create a new target description. */
40 target_desc_up tdesc = allocate_target_description ();
42 #ifndef IN_PROCESS_AGENT
43 std::string arch_name = "riscv";
45 if (features.xlen == 4)
47 if (features.embedded)
48 arch_name.append (":rv32e");
49 else
50 arch_name.append (":rv32i");
52 else if (features.xlen == 8)
53 arch_name.append (":rv64i");
54 else if (features.xlen == 16)
55 arch_name.append (":rv128i");
57 if (features.flen == 4)
58 arch_name.append ("f");
59 else if (features.flen == 8)
60 arch_name.append ("d");
61 else if (features.flen == 16)
62 arch_name.append ("q");
64 set_tdesc_architecture (tdesc.get (), arch_name.c_str ());
65 #endif
67 long regnum = 0;
69 /* For now we only support creating 32-bit or 64-bit x-registers. */
70 if (features.xlen == 4)
72 if (features.embedded)
73 regnum = create_feature_riscv_rv32e_xregs (tdesc.get (), regnum);
74 else
75 regnum = create_feature_riscv_32bit_cpu (tdesc.get (), regnum);
77 else if (features.xlen == 8)
78 regnum = create_feature_riscv_64bit_cpu (tdesc.get (), regnum);
80 /* For now we only support creating 32-bit or 64-bit f-registers. */
81 if (features.flen == 4)
82 regnum = create_feature_riscv_32bit_fpu (tdesc.get (), regnum);
83 else if (features.flen == 8)
84 regnum = create_feature_riscv_64bit_fpu (tdesc.get (), regnum);
86 /* Currently GDB only supports vector features coming from remote
87 targets. We don't support creating vector features on native targets
88 (yet). */
89 if (features.vlen != 0)
90 error (_("unable to create vector feature"));
92 return tdesc;
95 #ifndef GDBSERVER
97 /* Wrapper used by std::unordered_map to generate hash for feature set. */
98 struct riscv_gdbarch_features_hasher
100 std::size_t
101 operator() (const riscv_gdbarch_features &features) const noexcept
103 return features.hash ();
107 /* Cache of previously seen target descriptions, indexed by the feature set
108 that created them. */
109 static std::unordered_map<riscv_gdbarch_features,
110 const target_desc_up,
111 riscv_gdbarch_features_hasher> riscv_tdesc_cache;
113 /* See arch/riscv.h. */
115 const target_desc *
116 riscv_lookup_target_description (const struct riscv_gdbarch_features features)
118 /* Lookup in the cache. If we find it then return the pointer out of
119 the target_desc_up (which is a unique_ptr). This is safe as the
120 riscv_tdesc_cache will exist until GDB exits. */
121 const auto it = riscv_tdesc_cache.find (features);
122 if (it != riscv_tdesc_cache.end ())
123 return it->second.get ();
125 target_desc_up tdesc (riscv_create_target_description (features));
127 /* Add to the cache, and return a pointer borrowed from the
128 target_desc_up. This is safe as the cache (and the pointers
129 contained within it) are not deleted until GDB exits. */
130 target_desc *ptr = tdesc.get ();
131 riscv_tdesc_cache.emplace (features, std::move (tdesc));
132 return ptr;
135 #endif /* !GDBSERVER */