]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arch/riscv.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / arch / riscv.c
CommitLineData
3666a048 1/* Copyright (C) 2018-2021 Free Software Foundation, Inc.
b5ffee31
AB
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
268a13a5 18#include "gdbsupport/common-defs.h"
b5ffee31
AB
19#include "riscv.h"
20#include <stdlib.h>
63449436 21#include <unordered_map>
b5ffee31
AB
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"
25428040 27#include "../features/riscv/rv32e-xregs.c"
b5ffee31 28
d1c9b20f
AB
29#ifndef GDBSERVER
30#define STATIC_IN_GDB static
31#else
32#define STATIC_IN_GDB
33#endif
63449436 34
b5ffee31
AB
35/* See arch/riscv.h. */
36
51a948fd 37STATIC_IN_GDB target_desc_up
d1c9b20f 38riscv_create_target_description (const struct riscv_gdbarch_features features)
b5ffee31 39{
63449436 40 /* Now we should create a new target description. */
51a948fd 41 target_desc_up tdesc = allocate_target_description ();
b5ffee31
AB
42
43#ifndef IN_PROCESS_AGENT
44 std::string arch_name = "riscv";
45
46 if (features.xlen == 4)
25428040
AB
47 {
48 if (features.embedded)
49 arch_name.append (":rv32e");
50 else
51 arch_name.append (":rv32i");
52 }
b5ffee31
AB
53 else if (features.xlen == 8)
54 arch_name.append (":rv64i");
55 else if (features.xlen == 16)
56 arch_name.append (":rv128i");
57
58 if (features.flen == 4)
59 arch_name.append ("f");
60 else if (features.flen == 8)
61 arch_name.append ("d");
62 else if (features.flen == 16)
63 arch_name.append ("q");
64
51a948fd 65 set_tdesc_architecture (tdesc.get (), arch_name.c_str ());
b5ffee31
AB
66#endif
67
68 long regnum = 0;
69
70 /* For now we only support creating 32-bit or 64-bit x-registers. */
71 if (features.xlen == 4)
25428040
AB
72 {
73 if (features.embedded)
74 regnum = create_feature_riscv_rv32e_xregs (tdesc.get (), regnum);
75 else
76 regnum = create_feature_riscv_32bit_cpu (tdesc.get (), regnum);
77 }
b5ffee31 78 else if (features.xlen == 8)
51a948fd 79 regnum = create_feature_riscv_64bit_cpu (tdesc.get (), regnum);
b5ffee31
AB
80
81 /* For now we only support creating 32-bit or 64-bit f-registers. */
82 if (features.flen == 4)
51a948fd 83 regnum = create_feature_riscv_32bit_fpu (tdesc.get (), regnum);
b5ffee31 84 else if (features.flen == 8)
51a948fd 85 regnum = create_feature_riscv_64bit_fpu (tdesc.get (), regnum);
b5ffee31 86
d1c9b20f
AB
87 return tdesc;
88}
89
90#ifndef GDBSERVER
91
92/* Wrapper used by std::unordered_map to generate hash for feature set. */
93struct riscv_gdbarch_features_hasher
94{
95 std::size_t
96 operator() (const riscv_gdbarch_features &features) const noexcept
97 {
98 return features.hash ();
99 }
100};
101
102/* Cache of previously seen target descriptions, indexed by the feature set
103 that created them. */
104static std::unordered_map<riscv_gdbarch_features,
0e267416 105 const target_desc_up,
d1c9b20f
AB
106 riscv_gdbarch_features_hasher> riscv_tdesc_cache;
107
108/* See arch/riscv.h. */
109
110const target_desc *
111riscv_lookup_target_description (const struct riscv_gdbarch_features features)
112{
0e267416
AB
113 /* Lookup in the cache. If we find it then return the pointer out of
114 the target_desc_up (which is a unique_ptr). This is safe as the
115 riscv_tdesc_cache will exist until GDB exits. */
d1c9b20f
AB
116 const auto it = riscv_tdesc_cache.find (features);
117 if (it != riscv_tdesc_cache.end ())
0e267416 118 return it->second.get ();
d1c9b20f 119
51a948fd 120 target_desc_up tdesc (riscv_create_target_description (features));
d1c9b20f 121
51a948fd
AB
122 /* Add to the cache, and return a pointer borrowed from the
123 target_desc_up. This is safe as the cache (and the pointers
124 contained within it) are not deleted until GDB exits. */
125 target_desc *ptr = tdesc.get ();
126 riscv_tdesc_cache.emplace (features, std::move (tdesc));
127 return ptr;
b5ffee31 128}
d1c9b20f
AB
129
130#endif /* !GDBSERVER */