[gdb/tdep] Fix reverse execution of LDR(immediate) T4
[binutils-gdb.git] / gdb / gdbarch.py
blob4b4db667ca40b91531086d7c856a8ab60c6703fd
1 #!/usr/bin/env python3
3 # Architecture commands for GDB, the GNU debugger.
5 # Copyright (C) 1998-2024 Free Software Foundation, Inc.
7 # This file is part of GDB.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import textwrap
24 # gdbarch_components is imported only for its side-effect of filling
25 # `gdbarch_types.components`.
26 import gdbarch_components # noqa: F401 # type: ignore
27 import gdbcopyright
28 from gdbarch_types import Component, Function, Info, Value, components
31 def indentation(n_columns: int):
32 """Return string with tabs and spaces to indent line to N_COLUMNS."""
33 return "\t" * (n_columns // 8) + " " * (n_columns % 8)
36 copyright = gdbcopyright.copyright(
37 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
41 def info(c: Component):
42 "Filter function to only allow Info components."
43 return type(c) is Info
46 def not_info(c: Component):
47 "Filter function to omit Info components."
48 return type(c) is not Info
51 with open("gdbarch-gen.h", "w") as f:
52 print(copyright, file=f)
53 print(file=f)
54 print(file=f)
55 print(file=f)
56 print("/* The following are pre-initialized by GDBARCH. */", file=f)
58 # Do Info components first.
59 for c in filter(info, components):
60 print(file=f)
61 print(
62 f"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
63 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
64 file=f,
67 print(file=f)
68 print(file=f)
69 print("/* The following are initialized by the target dependent code. */", file=f)
71 # Generate decls for accessors, setters, and predicates for all
72 # non-Info components.
73 for c in filter(not_info, components):
74 if c.comment:
75 print(file=f)
76 comment = c.comment.split("\n")
77 if comment[0] == "":
78 comment = comment[1:]
79 if comment[-1] == "":
80 comment = comment[:-1]
81 print("/* ", file=f, end="")
82 print(comment[0], file=f, end="")
83 if len(comment) > 1:
84 print(file=f)
85 print(
86 textwrap.indent("\n".join(comment[1:]), prefix=" "),
87 end="",
88 file=f,
90 print(" */", file=f)
92 if c.predicate:
93 print(file=f)
94 print(f"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f)
96 print(file=f)
97 if isinstance(c, Value):
98 print(
99 f"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
100 file=f,
102 print(
103 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
104 file=f,
106 else:
107 assert isinstance(c, Function)
108 print(
109 f"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
110 file=f,
112 if c.implement:
113 print(
114 f"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
115 file=f,
117 print(
118 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
119 file=f,
122 with open("gdbarch.c", "w") as f:
123 print(copyright, file=f)
124 print(file=f)
125 print(file=f)
126 print("/* Maintain the struct gdbarch object. */", file=f)
127 print(file=f)
129 # The struct definition body.
131 print("struct gdbarch", file=f)
132 print("{", file=f)
133 print(" /* Has this architecture been fully initialized? */", file=f)
134 print(" bool initialized_p = false;", file=f)
135 print(file=f)
136 print(" /* An obstack bound to the lifetime of the architecture. */", file=f)
137 print(" auto_obstack obstack;", file=f)
138 print(" /* Registry. */", file=f)
139 print(" registry<gdbarch> registry_fields;", file=f)
140 print(file=f)
141 print(" /* basic architectural information. */", file=f)
142 for c in filter(info, components):
143 print(f" {c.type} {c.name};", file=f)
144 print(file=f)
145 print(" /* target specific vector. */", file=f)
146 print(" gdbarch_tdep_up tdep;", file=f)
147 print(" gdbarch_dump_tdep_ftype *dump_tdep = nullptr;", file=f)
148 print(file=f)
149 for c in filter(not_info, components):
150 if isinstance(c, Function):
151 print(f" gdbarch_{c.name}_ftype *", file=f, end="")
152 else:
153 print(f" {c.type} ", file=f, end="")
154 print(f"{c.name} = ", file=f, end="")
155 if c.predefault is not None:
156 print(f"{c.predefault};", file=f)
157 elif isinstance(c, Value):
158 print("0;", file=f)
159 else:
160 assert isinstance(c, Function)
161 print("nullptr;", file=f)
162 print("};", file=f)
163 print(file=f)
165 # Initialization.
167 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f)
168 print(" ``struct gdbarch_info''. */", file=f)
169 print(file=f)
170 print("struct gdbarch *", file=f)
171 print("gdbarch_alloc (const struct gdbarch_info *info,", file=f)
172 print(" gdbarch_tdep_up tdep)", file=f)
173 print("{", file=f)
174 print(" struct gdbarch *gdbarch;", file=f)
175 print("", file=f)
176 print(" gdbarch = new struct gdbarch;", file=f)
177 print(file=f)
178 print(" gdbarch->tdep = std::move (tdep);", file=f)
179 print(file=f)
180 for c in filter(info, components):
181 print(f" gdbarch->{c.name} = info->{c.name};", file=f)
182 print(file=f)
183 print(" return gdbarch;", file=f)
184 print("}", file=f)
185 print(file=f)
186 print(file=f)
187 print(file=f)
189 # Post-initialization validation and updating
191 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f)
192 print(file=f)
193 print("static void", file=f)
194 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f)
195 print("{", file=f)
196 print(" string_file log;", file=f)
197 print(file=f)
198 print(" /* fundamental */", file=f)
199 print(" if (gdbarch->byte_order == BFD_ENDIAN_UNKNOWN)", file=f)
200 print(""" log.puts ("\\n\\tbyte-order");""", file=f)
201 print(" if (gdbarch->bfd_arch_info == NULL)", file=f)
202 print(""" log.puts ("\\n\\tbfd_arch_info");""", file=f)
203 print(
204 " /* Check those that need to be defined for the given multi-arch level. */",
205 file=f,
207 for c in filter(not_info, components):
208 # An opportunity to write in the 'postdefault' value. We
209 # change field's value to the postdefault if its current value
210 # is not different to the initial value of the field.
211 if c.postdefault is not None:
212 init_value = c.predefault or "0"
213 print(f" if (gdbarch->{c.name} == {init_value})", file=f)
214 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
216 # Now validate the value.
217 if isinstance(c.invalid, str):
218 print(f" if ({c.invalid})", file=f)
219 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
220 elif c.predicate:
221 print(f" /* Skip verify of {c.name}, has predicate. */", file=f)
222 elif c.invalid:
223 if c.postdefault is not None:
224 # This component has its 'invalid' field set to True, but
225 # also has a postdefault. This makes no sense, the
226 # postdefault will have been applied above, so this field
227 # will not have a zero value.
228 raise Exception(
229 f"component {c.name} has postdefault and invalid set to True"
231 else:
232 init_value = c.predefault or "0"
233 print(f" if (gdbarch->{c.name} == {init_value})", file=f)
234 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
235 else:
236 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
237 print(" if (!log.empty ())", file=f)
238 print(
239 """ internal_error (_("verify_gdbarch: the following are invalid ...%s"),""",
240 file=f,
242 print(" log.c_str ());", file=f)
243 print("}", file=f)
244 print(file=f)
245 print(file=f)
247 # Dumping.
249 print("/* Print out the details of the current architecture. */", file=f)
250 print(file=f)
251 print("void", file=f)
252 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f)
253 print("{", file=f)
254 print(""" const char *gdb_nm_file = "<not-defined>";""", file=f)
255 print(file=f)
256 print("#if defined (GDB_NM_FILE)", file=f)
257 print(" gdb_nm_file = GDB_NM_FILE;", file=f)
258 print("#endif", file=f)
259 print(" gdb_printf (file,", file=f)
260 print(""" "gdbarch_dump: GDB_NM_FILE = %s\\n",""", file=f)
261 print(" gdb_nm_file);", file=f)
262 for c in components:
263 if c.predicate:
264 print(" gdb_printf (file,", file=f)
265 print(
266 f""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
267 file=f,
269 print(f" gdbarch_{c.name}_p (gdbarch));", file=f)
270 if isinstance(c, Function):
271 print(" gdb_printf (file,", file=f)
272 print(f""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f)
273 print(
274 f" host_address_to_string (gdbarch->{c.name}));",
275 file=f,
277 else:
278 if c.printer:
279 printer = c.printer
280 elif c.type == "CORE_ADDR":
281 printer = f"core_addr_to_string_nz (gdbarch->{c.name})"
282 else:
283 printer = f"plongest (gdbarch->{c.name})"
284 print(" gdb_printf (file,", file=f)
285 print(f""" "gdbarch_dump: {c.name} = %s\\n",""", file=f)
286 print(f" {printer});", file=f)
287 print(" if (gdbarch->dump_tdep != NULL)", file=f)
288 print(" gdbarch->dump_tdep (gdbarch, file);", file=f)
289 print("}", file=f)
290 print(file=f)
292 # Bodies of setter, accessor, and predicate functions.
294 for c in components:
295 if c.predicate:
296 print(file=f)
297 print("bool", file=f)
298 print(f"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f)
299 print("{", file=f)
300 print(" gdb_assert (gdbarch != NULL);", file=f)
301 print(f" return {c.get_predicate()};", file=f)
302 print("}", file=f)
303 if isinstance(c, Function):
304 if c.implement:
305 print(file=f)
306 print(f"{c.type}", file=f)
307 print(f"gdbarch_{c.name} ({c.set_list()})", file=f)
308 print("{", file=f)
309 print(" gdb_assert (gdbarch != NULL);", file=f)
310 print(f" gdb_assert (gdbarch->{c.name} != NULL);", file=f)
311 if c.predicate and c.predefault:
312 # Allow a call to a function with a predicate.
313 print(
314 f" /* Do not check predicate: {c.get_predicate()}, allow call. */",
315 file=f,
317 if c.param_checks:
318 for rule in c.param_checks:
319 print(f" gdb_assert ({rule});", file=f)
320 print(" if (gdbarch_debug >= 2)", file=f)
321 print(
322 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
323 file=f,
325 print(" ", file=f, end="")
326 if c.type != "void":
327 if c.result_checks:
328 print("auto result = ", file=f, end="")
329 else:
330 print("return ", file=f, end="")
331 print(f"gdbarch->{c.name} ({c.actuals()});", file=f)
332 if c.type != "void" and c.result_checks:
333 for rule in c.result_checks:
334 print(f" gdb_assert ({rule});", file=f)
335 print(" return result;", file=f)
336 print("}", file=f)
337 print(file=f)
338 print("void", file=f)
339 setter_name = f"set_gdbarch_{c.name}"
340 ftype_name = f"gdbarch_{c.name}_ftype"
341 print(f"{setter_name} (struct gdbarch *gdbarch,", file=f)
342 indent_columns = len(f"{setter_name} (")
343 print(f"{indentation(indent_columns)}{ftype_name} {c.name})", file=f)
344 print("{", file=f)
345 print(f" gdbarch->{c.name} = {c.name};", file=f)
346 print("}", file=f)
347 elif isinstance(c, Value):
348 print(file=f)
349 print(f"{c.type}", file=f)
350 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
351 print("{", file=f)
352 print(" gdb_assert (gdbarch != NULL);", file=f)
353 if isinstance(c.invalid, str):
354 print(" /* Check variable is valid. */", file=f)
355 print(f" gdb_assert (!({c.invalid}));", file=f)
356 elif c.predicate:
357 print(" /* Check predicate was used. */", file=f)
358 print(f" gdb_assert (gdbarch_{c.name}_p (gdbarch));", file=f)
359 elif c.invalid or c.postdefault is not None:
360 init_value = c.predefault or "0"
361 print(" /* Check variable changed from its initial value. */", file=f)
362 print(f" gdb_assert (gdbarch->{c.name} != {init_value});", file=f)
363 else:
364 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
365 print(" if (gdbarch_debug >= 2)", file=f)
366 print(
367 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
368 file=f,
370 print(f" return gdbarch->{c.name};", file=f)
371 print("}", file=f)
372 print(file=f)
373 print("void", file=f)
374 setter_name = f"set_gdbarch_{c.name}"
375 print(f"{setter_name} (struct gdbarch *gdbarch,", file=f)
376 indent_columns = len(f"{setter_name} (")
377 print(f"{indentation(indent_columns)}{c.type} {c.name})", file=f)
378 print("{", file=f)
379 print(f" gdbarch->{c.name} = {c.name};", file=f)
380 print("}", file=f)
381 else:
382 assert isinstance(c, Info)
383 print(file=f)
384 print(f"{c.type}", file=f)
385 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
386 print("{", file=f)
387 print(" gdb_assert (gdbarch != NULL);", file=f)
388 print(" if (gdbarch_debug >= 2)", file=f)
389 print(
390 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
391 file=f,
393 print(f" return gdbarch->{c.name};", file=f)
394 print("}", file=f)