26import gdbarch_components
28from gdbarch_types
import Component, Function, Info, Value, components
32 """Return string with tabs and spaces to indent line to N_COLUMNS."""
33 return "\t" * (n_columns // 8) +
" " * (n_columns % 8)
37 "gdbarch.py",
"Dynamic architecture support for GDB, the GNU debugger."
42 "Filter function to only allow Info components."
43 return type(c)
is Info
47 "Filter function to omit Info components."
48 return type(c)
is not Info
51with open(
"gdbarch-gen.h",
"w")
as f:
52 print(copyright, file=f)
56 print(
"/* The following are pre-initialized by GDBARCH. */", file=f)
59 for c
in filter(info, components):
62 f
"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
63/* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
69 print(
"/* The following are initialized by the target dependent code. */", file=f)
73 for c
in filter(not_info, components):
76 comment = c.comment.split(
"\n")
80 comment = comment[:-1]
81 print(
"/* ", file=f, end=
"")
82 print(comment[0], file=f, end=
"")
86 textwrap.indent(
"\n".join(comment[1:]), prefix=
" "),
94 print(f
"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f)
97 if isinstance(c, Value):
99 f
"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
103 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
107 assert isinstance(c, Function)
109 f
"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
114 f
"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
118 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
122with open(
"gdbarch.c",
"w")
as f:
123 print(copyright, file=f)
126 print(
"/* Maintain the struct gdbarch object. */", file=f)
131 print(
"struct gdbarch", file=f)
133 print(
" /* Has this architecture been fully initialized? */", file=f)
134 print(
" bool initialized_p = false;", 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)
141 print(
" /* basic architectural information. */", file=f)
142 for c
in filter(info, components):
143 print(f
" {c.type} {c.name};", 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)
149 for c
in filter(not_info, components):
150 if isinstance(c, Function):
151 print(f
" gdbarch_{c.name}_ftype *", file=f, end=
"")
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):
160 assert isinstance(c, Function)
161 print(
"nullptr;", file=f)
167 print(
"/* Create a new ``struct gdbarch'' based on information provided by", file=f)
168 print(
" ``struct gdbarch_info''. */", 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)
174 print(
" struct gdbarch *gdbarch;", file=f)
176 print(
" gdbarch = new struct gdbarch;", file=f)
178 print(
" gdbarch->tdep = std::move (tdep);", file=f)
180 for c
in filter(info, components):
181 print(f
" gdbarch->{c.name} = info->{c.name};", file=f)
183 print(
" return gdbarch;", file=f)
191 print(
"/* Ensure that all values in a GDBARCH are reasonable. */", file=f)
193 print(
"static void", file=f)
194 print(
"verify_gdbarch (struct gdbarch *gdbarch)", file=f)
196 print(
" string_file log;", 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)
204 " /* Check those that need to be defined for the given multi-arch level. */",
207 for c
in filter(not_info, components):
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)
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)
221 print(f
" /* Skip verify of {c.name}, has predicate. */", file=f)
223 if c.postdefault
is not None:
229 f
"component {c.name} has postdefault and invalid set to True"
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)
236 print(f
" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
237 print(
" if (!log.empty ())", file=f)
239 """ internal_error (_("verify_gdbarch: the following are invalid ...%s"),""",
242 print(
" log.c_str ());", file=f)
249 print(
"/* Print out the details of the current architecture. */", file=f)
251 print(
"void", file=f)
252 print(
"gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f)
254 print(
""" const char *gdb_nm_file = "<not-defined>";""", 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)
264 print(
" gdb_printf (file,", file=f)
266 f
""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
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)
274 f
" host_address_to_string (gdbarch->{c.name}));",
280 elif c.type ==
"CORE_ADDR":
281 printer = f
"core_addr_to_string_nz (gdbarch->{c.name})"
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)
297 print(
"bool", file=f)
298 print(f
"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f)
300 print(
" gdb_assert (gdbarch != NULL);", file=f)
301 print(f
" return {c.get_predicate()};", file=f)
303 if isinstance(c, Function):
306 print(f
"{c.type}", file=f)
307 print(f
"gdbarch_{c.name} ({c.set_list()})", 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:
314 f
" /* Do not check predicate: {c.get_predicate()}, allow call. */",
318 for rule
in c.param_checks:
319 print(f
" gdb_assert ({rule});", file=f)
320 print(
" if (gdbarch_debug >= 2)", file=f)
322 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
325 print(
" ", file=f, end=
"")
328 print(
"auto result = ", file=f, end=
"")
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)
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)
345 print(f
" gdbarch->{c.name} = {c.name};", file=f)
347 elif isinstance(c, Value):
349 print(f
"{c.type}", file=f)
350 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", 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)
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)
364 print(f
" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
365 print(
" if (gdbarch_debug >= 2)", file=f)
367 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
370 print(f
" return gdbarch->{c.name};", 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)
379 print(f
" gdbarch->{c.name} = {c.name};", file=f)
382 assert isinstance(c, Info)
384 print(f
"{c.type}", file=f)
385 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
387 print(
" gdb_assert (gdbarch != NULL);", file=f)
388 print(
" if (gdbarch_debug >= 2)", file=f)
390 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
393 print(f
" return gdbarch->{c.name};", file=f)
indentation(int n_columns)
copyright(str tool, str description)