Don't emit gdbarch_return_value
[binutils-gdb.git] / gdb / gdbarch.py
1 #!/usr/bin/env python3
2
3 # Architecture commands for GDB, the GNU debugger.
4 #
5 # Copyright (C) 1998-2023 Free Software Foundation, Inc.
6 #
7 # This file is part of GDB.
8 #
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.
13 #
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.
18 #
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/>.
21
22 import textwrap
23 import gdbcopyright
24
25 # All the components created in gdbarch-components.py.
26 components = []
27
28
29 def indentation(n_columns):
30 """Return string with tabs and spaces to indent line to N_COLUMNS."""
31 return "\t" * (n_columns // 8) + " " * (n_columns % 8)
32
33
34 def join_type_and_name(t, n):
35 "Combine the type T and the name N into a C declaration."
36 if t.endswith("*") or t.endswith("&"):
37 return t + n
38 else:
39 return t + " " + n
40
41
42 def join_params(params):
43 """Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
44 list of declarations."""
45 params = [join_type_and_name(p[0], p[1]) for p in params]
46 return ", ".join(params)
47
48
49 class _Component:
50 "Base class for all components."
51
52 def __init__(self, **kwargs):
53 for key in kwargs:
54 setattr(self, key, kwargs[key])
55 components.append(self)
56
57 # It doesn't make sense to have a check of the result value
58 # for a function or method with void return type.
59 if self.type == "void" and self.result_checks:
60 raise Exception("can't have result checks with a void return type")
61
62 def get_predicate(self):
63 "Return the expression used for validity checking."
64 assert self.predicate and not isinstance(self.invalid, str)
65 if self.predefault:
66 predicate = f"gdbarch->{self.name} != {self.predefault}"
67 elif isinstance(c, Value):
68 predicate = f"gdbarch->{self.name} != 0"
69 else:
70 predicate = f"gdbarch->{self.name} != NULL"
71 return predicate
72
73
74 class Info(_Component):
75 "An Info component is copied from the gdbarch_info."
76
77 def __init__(self, *, name, type, printer=None):
78 super().__init__(name=name, type=type, printer=printer)
79 # This little hack makes the generator a bit simpler.
80 self.predicate = None
81
82
83 class Value(_Component):
84 "A Value component is just a data member."
85
86 def __init__(
87 self,
88 *,
89 name,
90 type,
91 comment=None,
92 predicate=None,
93 predefault=None,
94 postdefault=None,
95 invalid=None,
96 printer=None,
97 ):
98 super().__init__(
99 comment=comment,
100 name=name,
101 type=type,
102 predicate=predicate,
103 predefault=predefault,
104 postdefault=postdefault,
105 invalid=invalid,
106 printer=printer,
107 )
108
109
110 class Function(_Component):
111 "A Function component is a function pointer member."
112
113 def __init__(
114 self,
115 *,
116 name,
117 type,
118 params,
119 comment=None,
120 predicate=None,
121 predefault=None,
122 postdefault=None,
123 invalid=None,
124 printer=None,
125 param_checks=None,
126 result_checks=None,
127 implement=True,
128 ):
129 super().__init__(
130 comment=comment,
131 name=name,
132 type=type,
133 predicate=predicate,
134 predefault=predefault,
135 postdefault=postdefault,
136 invalid=invalid,
137 printer=printer,
138 params=params,
139 param_checks=param_checks,
140 result_checks=result_checks,
141 implement=implement,
142 )
143
144 def ftype(self):
145 "Return the name of the function typedef to use."
146 return f"gdbarch_{self.name}_ftype"
147
148 def param_list(self):
149 "Return the formal parameter list as a string."
150 return join_params(self.params)
151
152 def set_list(self):
153 """Return the formal parameter list of the caller function,
154 as a string. This list includes the gdbarch."""
155 arch_arg = ("struct gdbarch *", "gdbarch")
156 arch_tuple = [arch_arg]
157 return join_params(arch_tuple + list(self.params))
158
159 def actuals(self):
160 "Return the actual parameters to forward, as a string."
161 return ", ".join([p[1] for p in self.params])
162
163
164 class Method(Function):
165 "A Method is like a Function but passes the gdbarch through."
166
167 def param_list(self):
168 "See superclass."
169 return self.set_list()
170
171 def actuals(self):
172 "See superclass."
173 result = ["gdbarch"] + [p[1] for p in self.params]
174 return ", ".join(result)
175
176
177 # Read the components.
178 with open("gdbarch-components.py") as fd:
179 exec(fd.read())
180
181 copyright = gdbcopyright.copyright(
182 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
183 )
184
185
186 def info(c):
187 "Filter function to only allow Info components."
188 return type(c) is Info
189
190
191 def not_info(c):
192 "Filter function to omit Info components."
193 return type(c) is not Info
194
195
196 with open("gdbarch-gen.h", "w") as f:
197 print(copyright, file=f)
198 print(file=f)
199 print(file=f)
200 print("/* The following are pre-initialized by GDBARCH. */", file=f)
201
202 # Do Info components first.
203 for c in filter(info, components):
204 print(file=f)
205 print(
206 f"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
207 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
208 file=f,
209 )
210
211 print(file=f)
212 print(file=f)
213 print("/* The following are initialized by the target dependent code. */", file=f)
214
215 # Generate decls for accessors, setters, and predicates for all
216 # non-Info components.
217 for c in filter(not_info, components):
218 if c.comment:
219 print(file=f)
220 comment = c.comment.split("\n")
221 if comment[0] == "":
222 comment = comment[1:]
223 if comment[-1] == "":
224 comment = comment[:-1]
225 print("/* ", file=f, end="")
226 print(comment[0], file=f, end="")
227 if len(comment) > 1:
228 print(file=f)
229 print(
230 textwrap.indent("\n".join(comment[1:]), prefix=" "),
231 end="",
232 file=f,
233 )
234 print(" */", file=f)
235
236 if c.predicate:
237 print(file=f)
238 print(f"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f)
239
240 print(file=f)
241 if isinstance(c, Value):
242 print(
243 f"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
244 file=f,
245 )
246 print(
247 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
248 file=f,
249 )
250 else:
251 assert isinstance(c, Function)
252 print(
253 f"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
254 file=f,
255 )
256 if c.implement:
257 print(
258 f"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
259 file=f,
260 )
261 print(
262 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
263 file=f,
264 )
265
266 with open("gdbarch.c", "w") as f:
267 print(copyright, file=f)
268 print(file=f)
269 print("/* Maintain the struct gdbarch object. */", file=f)
270 print(file=f)
271 #
272 # The struct definition body.
273 #
274 print("struct gdbarch", file=f)
275 print("{", file=f)
276 print(" /* Has this architecture been fully initialized? */", file=f)
277 print(" bool initialized_p = false;", file=f)
278 print(file=f)
279 print(" /* An obstack bound to the lifetime of the architecture. */", file=f)
280 print(" auto_obstack obstack;", file=f)
281 print(" /* Registry. */", file=f)
282 print(" registry<gdbarch> registry_fields;", file=f)
283 print(file=f)
284 print(" /* basic architectural information. */", file=f)
285 for c in filter(info, components):
286 print(f" {c.type} {c.name};", file=f)
287 print(file=f)
288 print(" /* target specific vector. */", file=f)
289 print(" struct gdbarch_tdep_base *tdep = nullptr;", file=f)
290 print(" gdbarch_dump_tdep_ftype *dump_tdep = nullptr;", file=f)
291 print(file=f)
292 print(" /* per-architecture data-pointers. */", file=f)
293 print(" unsigned nr_data = 0;", file=f)
294 print(" void **data = nullptr;", file=f)
295 print(file=f)
296 for c in filter(not_info, components):
297 if isinstance(c, Function):
298 print(f" gdbarch_{c.name}_ftype *", file=f, end="")
299 else:
300 print(f" {c.type} ", file=f, end="")
301 print(f"{c.name} = ", file=f, end="")
302 if c.predefault is not None:
303 print(f"{c.predefault};", file=f)
304 elif isinstance(c, Value):
305 print("0;", file=f)
306 else:
307 assert isinstance(c, Function)
308 print("nullptr;", file=f)
309 print("};", file=f)
310 print(file=f)
311 #
312 # Initialization.
313 #
314 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f)
315 print(" ``struct gdbarch_info''. */", file=f)
316 print(file=f)
317 print("struct gdbarch *", file=f)
318 print("gdbarch_alloc (const struct gdbarch_info *info,", file=f)
319 print(" struct gdbarch_tdep_base *tdep)", file=f)
320 print("{", file=f)
321 print(" struct gdbarch *gdbarch;", file=f)
322 print("", file=f)
323 print(" gdbarch = new struct gdbarch;", file=f)
324 print(file=f)
325 print(" gdbarch->tdep = tdep;", file=f)
326 print(file=f)
327 for c in filter(info, components):
328 print(f" gdbarch->{c.name} = info->{c.name};", file=f)
329 print(file=f)
330 print(" return gdbarch;", file=f)
331 print("}", file=f)
332 print(file=f)
333 print(file=f)
334 print(file=f)
335 #
336 # Post-initialization validation and updating
337 #
338 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f)
339 print(file=f)
340 print("static void", file=f)
341 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f)
342 print("{", file=f)
343 print(" string_file log;", file=f)
344 print(file=f)
345 print(" /* fundamental */", file=f)
346 print(" if (gdbarch->byte_order == BFD_ENDIAN_UNKNOWN)", file=f)
347 print(""" log.puts ("\\n\\tbyte-order");""", file=f)
348 print(" if (gdbarch->bfd_arch_info == NULL)", file=f)
349 print(""" log.puts ("\\n\\tbfd_arch_info");""", file=f)
350 print(
351 " /* Check those that need to be defined for the given multi-arch level. */",
352 file=f,
353 )
354 for c in filter(not_info, components):
355 if c.invalid is False:
356 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
357 elif c.predicate:
358 print(f" /* Skip verify of {c.name}, has predicate. */", file=f)
359 elif isinstance(c.invalid, str) and c.postdefault is not None:
360 print(f" if ({c.invalid})", file=f)
361 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
362 elif c.predefault is not None and c.postdefault is not None:
363 print(f" if (gdbarch->{c.name} == {c.predefault})", file=f)
364 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
365 elif c.postdefault is not None:
366 print(f" if (gdbarch->{c.name} == 0)", file=f)
367 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
368 elif isinstance(c.invalid, str):
369 print(f" if ({c.invalid})", file=f)
370 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
371 elif c.predefault is not None:
372 print(f" if (gdbarch->{c.name} == {c.predefault})", file=f)
373 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
374 elif c.invalid is True:
375 print(f" if (gdbarch->{c.name} == 0)", file=f)
376 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
377 else:
378 # We should not allow ourselves to simply do nothing here
379 # because no other case applies. If we end up here then
380 # either the input data needs adjusting so one of the
381 # above cases matches, or we need additional cases adding
382 # here.
383 raise Exception("unhandled case when generating gdbarch validation")
384 print(" if (!log.empty ())", file=f)
385 print(
386 """ internal_error (_("verify_gdbarch: the following are invalid ...%s"),""",
387 file=f,
388 )
389 print(" log.c_str ());", file=f)
390 print("}", file=f)
391 print(file=f)
392 print(file=f)
393 #
394 # Dumping.
395 #
396 print("/* Print out the details of the current architecture. */", file=f)
397 print(file=f)
398 print("void", file=f)
399 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f)
400 print("{", file=f)
401 print(""" const char *gdb_nm_file = "<not-defined>";""", file=f)
402 print(file=f)
403 print("#if defined (GDB_NM_FILE)", file=f)
404 print(" gdb_nm_file = GDB_NM_FILE;", file=f)
405 print("#endif", file=f)
406 print(" gdb_printf (file,", file=f)
407 print(""" "gdbarch_dump: GDB_NM_FILE = %s\\n",""", file=f)
408 print(" gdb_nm_file);", file=f)
409 for c in components:
410 if c.predicate:
411 print(" gdb_printf (file,", file=f)
412 print(
413 f""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
414 file=f,
415 )
416 print(f" gdbarch_{c.name}_p (gdbarch));", file=f)
417 if isinstance(c, Function):
418 print(" gdb_printf (file,", file=f)
419 print(f""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f)
420 print(
421 f" host_address_to_string (gdbarch->{c.name}));",
422 file=f,
423 )
424 else:
425 if c.printer:
426 printer = c.printer
427 elif c.type == "CORE_ADDR":
428 printer = f"core_addr_to_string_nz (gdbarch->{c.name})"
429 else:
430 printer = f"plongest (gdbarch->{c.name})"
431 print(" gdb_printf (file,", file=f)
432 print(f""" "gdbarch_dump: {c.name} = %s\\n",""", file=f)
433 print(f" {printer});", file=f)
434 print(" if (gdbarch->dump_tdep != NULL)", file=f)
435 print(" gdbarch->dump_tdep (gdbarch, file);", file=f)
436 print("}", file=f)
437 print(file=f)
438 #
439 # Bodies of setter, accessor, and predicate functions.
440 #
441 for c in components:
442 if c.predicate:
443 print(file=f)
444 print("bool", file=f)
445 print(f"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f)
446 print("{", file=f)
447 print(" gdb_assert (gdbarch != NULL);", file=f)
448 print(f" return {c.get_predicate()};", file=f)
449 print("}", file=f)
450 if isinstance(c, Function):
451 if c.implement:
452 print(file=f)
453 print(f"{c.type}", file=f)
454 print(f"gdbarch_{c.name} ({c.set_list()})", file=f)
455 print("{", file=f)
456 print(" gdb_assert (gdbarch != NULL);", file=f)
457 print(f" gdb_assert (gdbarch->{c.name} != NULL);", file=f)
458 if c.predicate and c.predefault:
459 # Allow a call to a function with a predicate.
460 print(
461 f" /* Do not check predicate: {c.get_predicate()}, allow call. */",
462 file=f,
463 )
464 if c.param_checks:
465 for rule in c.param_checks:
466 print(f" gdb_assert ({rule});", file=f)
467 print(" if (gdbarch_debug >= 2)", file=f)
468 print(
469 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
470 file=f,
471 )
472 print(" ", file=f, end="")
473 if c.type != "void":
474 if c.result_checks:
475 print("auto result = ", file=f, end="")
476 else:
477 print("return ", file=f, end="")
478 print(f"gdbarch->{c.name} ({c.actuals()});", file=f)
479 if c.type != "void" and c.result_checks:
480 for rule in c.result_checks:
481 print(f" gdb_assert ({rule});", file=f)
482 print(" return result;", file=f)
483 print("}", file=f)
484 print(file=f)
485 print("void", file=f)
486 setter_name = f"set_gdbarch_{c.name}"
487 ftype_name = f"gdbarch_{c.name}_ftype"
488 print(f"{setter_name} (struct gdbarch *gdbarch,", file=f)
489 indent_columns = len(f"{setter_name} (")
490 print(f"{indentation(indent_columns)}{ftype_name} {c.name})", file=f)
491 print("{", file=f)
492 print(f" gdbarch->{c.name} = {c.name};", file=f)
493 print("}", file=f)
494 elif isinstance(c, Value):
495 print(file=f)
496 print(f"{c.type}", file=f)
497 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
498 print("{", file=f)
499 print(" gdb_assert (gdbarch != NULL);", file=f)
500 if c.invalid is False:
501 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
502 elif isinstance(c.invalid, str):
503 print(" /* Check variable is valid. */", file=f)
504 print(f" gdb_assert (!({c.invalid}));", file=f)
505 elif c.predefault:
506 print(" /* Check variable changed from pre-default. */", file=f)
507 print(f" gdb_assert (gdbarch->{c.name} != {c.predefault});", file=f)
508 print(" if (gdbarch_debug >= 2)", file=f)
509 print(
510 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
511 file=f,
512 )
513 print(f" return gdbarch->{c.name};", file=f)
514 print("}", file=f)
515 print(file=f)
516 print("void", file=f)
517 setter_name = f"set_gdbarch_{c.name}"
518 print(f"{setter_name} (struct gdbarch *gdbarch,", file=f)
519 indent_columns = len(f"{setter_name} (")
520 print(f"{indentation(indent_columns)}{c.type} {c.name})", file=f)
521 print("{", file=f)
522 print(f" gdbarch->{c.name} = {c.name};", file=f)
523 print("}", file=f)
524 else:
525 assert isinstance(c, Info)
526 print(file=f)
527 print(f"{c.type}", file=f)
528 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
529 print("{", file=f)
530 print(" gdb_assert (gdbarch != NULL);", file=f)
531 print(" if (gdbarch_debug >= 2)", file=f)
532 print(
533 f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
534 file=f,
535 )
536 print(f" return gdbarch->{c.name};", file=f)
537 print("}", file=f)