GDB (xrefs)
Loading...
Searching...
No Matches
make-target-delegates.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (C) 2013-2023 Free Software Foundation, Inc.
4#
5# This file is part of GDB.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20# Usage:
21# make-target-delegates.py
22
23import re
24from typing import Dict, List, TextIO
25
26import gdbcopyright
27
28# The line we search for in target.h that marks where we should start
29# looking for methods.
30TRIGGER = re.compile(r"^struct target_ops$")
31# The end of the methods part.
32ENDER = re.compile(r"^\s*};$")
33
34# Match a C symbol.
35SYMBOL = "[a-zA-Z_][a-zA-Z0-9_]*"
36# Match the name part of a method in struct target_ops.
37NAME_PART = r"(?P<name>" + SYMBOL + r")\s"
38# Match the arguments to a method.
39ARGS_PART = r"(?P<args>\‍(.*\‍))"
40# We strip the indentation so here we only need the caret.
41INTRO_PART = r"^"
42
43POINTER_PART = r"\s*(\*|\&)?\s*"
44
45# Match a C++ symbol, including scope operators and template
46# parameters. E.g., 'std::vector<something>'.
47CP_SYMBOL = r"[a-zA-Z_][a-zA-Z0-9_<>:]*"
48# Match the return type when it is "ordinary".
49SIMPLE_RETURN_PART = r"((struct|class|enum|union)\s+)?" + CP_SYMBOL
50
51# Match a return type.
52RETURN_PART = r"((const|volatile)\s+)?(" + SIMPLE_RETURN_PART + ")" + POINTER_PART
53
54# Match "virtual".
55VIRTUAL_PART = r"virtual\s"
56
57# Match the TARGET_DEFAULT_* attribute for a method.
58TARGET_DEFAULT_PART = r"TARGET_DEFAULT_(?P<style>[A-Z_]+)\s*\‍((?P<default_arg>.*)\‍)"
59
60# Match the arguments and trailing attribute of a method definition.
61# Note we don't match the trailing ";".
62METHOD_TRAILER = r"\s*" + TARGET_DEFAULT_PART + "$"
63
64# Match an entire method definition.
65METHOD = re.compile(
66 INTRO_PART
67 + VIRTUAL_PART
68 + "(?P<return_type>"
69 + RETURN_PART
70 + ")"
71 + NAME_PART
72 + ARGS_PART
73 + METHOD_TRAILER
74)
75
76# Regular expression used to dissect argument types.
77ARGTYPES = re.compile(
78 "^("
79 + r"(?P<E>enum\s+"
80 + SYMBOL
81 + r"\s*)("
82 + SYMBOL
83 + ")?"
84 + r"|(?P<T>.*(enum\s+)?"
85 + SYMBOL
86 + r".*(\s|\*|&))"
87 + SYMBOL
88 + ")$"
89)
90
91# Match TARGET_DEBUG_PRINTER in an argument type.
92# This must match the whole "sub-expression" including the parens.
93TARGET_DEBUG_PRINTER = r"\s*TARGET_DEBUG_PRINTER\s*\‍((?P<arg>[^)]*)\‍)\s*"
94
95
96class Entry:
98 self, argtypes: List[str], return_type: str, style: str, default_arg: str
99 ):
100 self.argtypes = argtypes
101 self.return_type = return_type
102 self.style = style
103 self.default_arg = default_arg
104
105
107 found_trigger = False
108 all_the_text = ""
109 with open("target.h", "r") as target_h:
110 for line in target_h:
111 line = line.strip()
112 if not found_trigger:
113 if TRIGGER.match(line):
114 found_trigger = True
115 elif "{" in line:
116 # Skip the open brace.
117 pass
118 elif ENDER.match(line):
119 break
120 else:
121 # Strip // comments.
122 line = re.split("//", line)[0]
123 all_the_text = all_the_text + " " + line
124 if not found_trigger:
125 raise RuntimeError("Could not find trigger line")
126 # Now strip out the C comments.
127 all_the_text = re.sub(r"/\*(.*?)\*/", "", all_the_text)
128 # Replace sequences whitespace with a single space character.
129 # We need the space because the method may have been split
130 # between multiple lines, like e.g.:
131 #
132 # virtual std::vector<long_type_name>
133 # my_long_method_name ()
134 # TARGET_DEFAULT_IGNORE ();
135 #
136 # If we didn't preserve the space, then we'd end up with:
137 #
138 # virtual std::vector<long_type_name>my_long_method_name ()TARGET_DEFAULT_IGNORE ()
139 #
140 # ... which wouldn't later be parsed correctly.
141 all_the_text = re.sub(r"\s+", " ", all_the_text)
142 return all_the_text.split(";")
143
144
145# Parse arguments into a list.
146def parse_argtypes(typestr: str):
147 # Remove the outer parens.
148 typestr = re.sub(r"^\‍((.*)\‍)$", r"\1", typestr)
149 result: list[str] = []
150 for item in re.split(r",\s*", typestr):
151 if item == "void" or item == "":
152 continue
153 m = ARGTYPES.match(item)
154 if m:
155 if m.group("E"):
156 onetype = m.group("E")
157 else:
158 onetype = m.group("T")
159 else:
160 onetype = item
161 result.append(onetype.strip())
162 return result
163
164
165# Write function header given name, return type, and argtypes.
166# Returns a list of actual argument names.
168 f: TextIO, decl: bool, name: str, return_type: str, argtypes: List[str]
169):
170 print(return_type, file=f, end="")
171 if decl:
172 if not return_type.endswith("*"):
173 print(" ", file=f, end="")
174 else:
175 print("", file=f)
176 print(name + " (", file=f, end="")
177 argdecls: list[str] = []
178 actuals: list[str] = []
179 for i in range(len(argtypes)):
180 val = re.sub(TARGET_DEBUG_PRINTER, "", argtypes[i])
181 if not val.endswith("*") and not val.endswith("&"):
182 val = val + " "
183 vname = "arg" + str(i)
184 val = val + vname
185 argdecls.append(val)
186 actuals.append(vname)
187 print(", ".join(argdecls) + ")", file=f, end="")
188 if decl:
189 print(" override;", file=f)
190 else:
191 print("\n{", file=f)
192 return actuals
193
194
195# Write out a declaration.
196def write_declaration(f: TextIO, name: str, return_type: str, argtypes: List[str]):
197 write_function_header(f, True, name, return_type, argtypes)
198
199
200# Write out a delegation function.
201def write_delegator(f: TextIO, name: str, return_type: str, argtypes: List[str]):
202 print("", file=f)
203 names = write_function_header(
204 f, False, "target_ops::" + name, return_type, argtypes
205 )
206 print(" ", file=f, end="")
207 if return_type != "void":
208 print("return ", file=f, end="")
209 print("this->beneath ()->" + name + " (", file=f, end="")
210 print(", ".join(names), file=f, end="")
211 print(");", file=f)
212 print("}", file=f)
213
214
215# Write out a default function.
217 f: TextIO,
218 content: str,
219 style: str,
220 name: str,
221 return_type: str,
222 argtypes: List[str],
223):
224 print("", file=f)
225 name = "dummy_target::" + name
226 names = write_function_header(f, False, name, return_type, argtypes)
227 if style == "FUNC":
228 print(" ", file=f, end="")
229 if return_type != "void":
230 print("return ", file=f, end="")
231 print(content + " (", file=f, end="")
232 names.insert(0, "this")
233 print(", ".join(names) + ");", file=f)
234 elif style == "RETURN":
235 print(" return " + content + ";", file=f)
236 elif style == "NORETURN":
237 print(" " + content + ";", file=f)
238 elif style == "IGNORE":
239 # Nothing.
240 pass
241 else:
242 raise RuntimeError("unrecognized style: " + style)
243 print("}", file=f)
244
245
246def munge_type(typename: str):
247 m = re.search(TARGET_DEBUG_PRINTER, typename)
248 if m:
249 return m.group("arg")
250 typename = typename.rstrip()
251 # There's no reason to have these keywords in the name, and their
252 # presence makes it harder to change styles.
253 typename = re.sub("\\b(struct|enum|class|union) ", "", typename)
254 typename = re.sub("[ ()<>:]", "_", typename)
255 typename = re.sub("[*]", "p", typename)
256 typename = re.sub("&", "r", typename)
257 # Identifiers with double underscores are reserved to the C++
258 # implementation.
259 typename = re.sub("_+", "_", typename)
260 # Avoid ending the function name with underscore, for
261 # cosmetics. Trailing underscores appear after munging types
262 # with template parameters, like e.g. "foo<int>".
263 typename = re.sub("_+$", "", typename)
264 return "target_debug_print_" + typename
265
266
267# Write out a debug method.
269 f: TextIO, content: str, name: str, return_type: str, argtypes: List[str]
270):
271 print("", file=f)
272 debugname = "debug_target::" + name
273 names = write_function_header(f, False, debugname, return_type, argtypes)
274 print(
275 ' gdb_printf (gdb_stdlog, "-> %s->'
276 + name
277 + ' (...)\\n", this->beneath ()->shortname ());',
278 file=f,
279 )
280
281 # Delegate to the beneath target.
282 if return_type != "void":
283 print(" " + return_type + " result", file=f)
284 print(" = ", file=f, end="")
285 else:
286 print(" ", file=f, end="")
287 print("this->beneath ()->" + name + " (", file=f, end="")
288 print(", ".join(names), file=f, end="")
289 print(");", file=f)
290
291 # Now print the arguments.
292 print(
293 ' gdb_printf (gdb_stdlog, "<- %s->'
294 + name
295 + ' (", this->beneath ()->shortname ());',
296 file=f,
297 )
298 for i in range(len(argtypes)):
299 if i > 0:
300 print(' gdb_puts (", ", gdb_stdlog);', file=f)
301 printer = munge_type(argtypes[i])
302 print(" " + printer + " (" + names[i] + ");", file=f)
303 if return_type != "void":
304 print(' gdb_puts (") = ", gdb_stdlog);', file=f)
305 printer = munge_type(return_type)
306 print(" " + printer + " (result);", file=f)
307 print(' gdb_puts ("\\n", gdb_stdlog);', file=f)
308 else:
309 print(' gdb_puts (")\\n", gdb_stdlog);', file=f)
310
311 if return_type != "void":
312 print(" return result;", file=f)
313
314 print("}", file=f)
315
316
318 f: TextIO,
319 class_name: str,
320 delegators: List[str],
321 entries: Dict[str, Entry],
322):
323 print("", file=f)
324 print("struct " + class_name + " : public target_ops", file=f)
325 print("{", file=f)
326 print(" const target_info &info () const override;", file=f)
327 print("", file=f)
328 print(" strata stratum () const override;", file=f)
329 print("", file=f)
330
331 for name in delegators:
332 print(" ", file=f, end="")
333 entry = entries[name]
334 write_declaration(f, name, entry.return_type, entry.argtypes)
335
336 print("};", file=f)
337
338
339delegators: List[str] = []
340entries: Dict[str, Entry] = {}
341
342for current_line in scan_target_h():
343 # See comments in scan_target_h. Here we strip away the leading
344 # and trailing whitespace.
345 current_line = current_line.strip()
346 m = METHOD.match(current_line)
347 if not m:
348 continue
349 data = m.groupdict()
350 name = data["name"]
351 argtypes = parse_argtypes(data["args"])
352 return_type = data["return_type"].strip()
353 style = data["style"]
354 default_arg = data["default_arg"]
355 entries[name] = Entry(argtypes, return_type, style, default_arg)
356
357 delegators.append(name)
358
359with open("target-delegates.c", "w") as f:
360 print(
362 "make-target-delegates.py", "Boilerplate target methods for GDB"
363 ),
364 file=f,
365 )
366 print_class(f, "dummy_target", delegators, entries)
367 print_class(f, "debug_target", delegators, entries)
368
369 for name in delegators:
370 entry = entries[name]
371
372 write_delegator(f, name, entry.return_type, entry.argtypes)
374 f,
375 entry.default_arg,
376 entry.style,
377 name,
378 entry.return_type,
379 entry.argtypes,
380 )
382 f,
383 entry.default_arg,
384 name,
385 entry.return_type,
386 entry.argtypes,
387 )
__init__(self, List[str] argtypes, str return_type, str style, str default_arg)
copyright(str tool, str description)
print_class(TextIO f, str class_name, List[str] delegators, Dict[str, Entry] entries)
write_delegator(TextIO f, str name, str return_type, List[str] argtypes)
write_debugmethod(TextIO f, str content, str name, str return_type, List[str] argtypes)
write_function_header(TextIO f, bool decl, str name, str return_type, List[str] argtypes)
write_tdefault(TextIO f, str content, str style, str name, str return_type, List[str] argtypes)
write_declaration(TextIO f, str name, str return_type, List[str] argtypes)
Definition value.h:90