1 /* Copyright (C) 2022-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 "loongarch.h"
20 #include <unordered_map>
22 /* Target description features. */
24 #include "../features/loongarch/base32.c"
25 #include "../features/loongarch/base64.c"
26 #include "../features/loongarch/fpu.c"
27 #include "../features/loongarch/lsx.c"
28 #include "../features/loongarch/lasx.c"
29 #include "../features/loongarch/lbt.c"
32 #define STATIC_IN_GDB static
37 STATIC_IN_GDB target_desc_up
38 loongarch_create_target_description (const struct loongarch_gdbarch_features features
)
40 /* Now we should create a new target description. */
41 target_desc_up tdesc
= allocate_target_description ();
43 std::string arch_name
= "loongarch";
45 if (features
.xlen
== 4)
46 arch_name
.append ("32");
47 else if (features
.xlen
== 8)
48 arch_name
.append ("64");
50 if (features
.fputype
== SINGLE_FLOAT
)
51 arch_name
.append ("f");
52 else if (features
.fputype
== DOUBLE_FLOAT
)
53 arch_name
.append ("d");
55 set_tdesc_architecture (tdesc
.get (), arch_name
.c_str ());
59 /* For now we only support creating 32-bit or 64-bit x-registers. */
60 if (features
.xlen
== 4)
61 regnum
= create_feature_loongarch_base32 (tdesc
.get (), regnum
);
62 else if (features
.xlen
== 8)
63 regnum
= create_feature_loongarch_base64 (tdesc
.get (), regnum
);
65 /* For now we only support creating single float and double float. */
66 regnum
= create_feature_loongarch_fpu (tdesc
.get (), regnum
);
68 /* For now we only support creating lsx and lasx. */
69 regnum
= create_feature_loongarch_lsx (tdesc
.get (), regnum
);
70 regnum
= create_feature_loongarch_lasx (tdesc
.get (), regnum
);
72 /* For now we only support creating scr registers, eflags and ftop. */
73 regnum
= create_feature_loongarch_lbt (tdesc
.get (), regnum
);
80 /* Wrapper used by std::unordered_map to generate hash for feature set. */
81 struct loongarch_gdbarch_features_hasher
84 operator() (const loongarch_gdbarch_features
&features
) const noexcept
86 return features
.hash ();
90 /* Cache of previously seen target descriptions, indexed by the feature set
92 static std::unordered_map
<loongarch_gdbarch_features
,
94 loongarch_gdbarch_features_hasher
> loongarch_tdesc_cache
;
97 loongarch_lookup_target_description (const struct loongarch_gdbarch_features features
)
99 /* Lookup in the cache. If we find it then return the pointer out of
100 the target_desc_up (which is a unique_ptr). This is safe as the
101 loongarch_tdesc_cache will exist until GDB exits. */
102 const auto it
= loongarch_tdesc_cache
.find (features
);
103 if (it
!= loongarch_tdesc_cache
.end ())
104 return it
->second
.get ();
106 target_desc_up
tdesc (loongarch_create_target_description (features
));
108 /* Add to the cache, and return a pointer borrowed from the
109 target_desc_up. This is safe as the cache (and the pointers
110 contained within it) are not deleted until GDB exits. */
111 target_desc
*ptr
= tdesc
.get ();
112 loongarch_tdesc_cache
.emplace (features
, std::move (tdesc
));
116 #endif /* !GDBSERVER */