1 # Script to generate constants for Python pretty printers.
3 # Copyright (C) 2016-2017 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <http://www.gnu.org/licenses/>.
20 # This script is a smaller version of the clever gen-asm-const.awk hack used to
21 # generate ASM constants from .sym files. We'll use this to generate constants
22 # for Python pretty printers.
24 # The input to this script are .pysym files that look like:
25 # #C_Preprocessor_Directive...
29 # A line giving just a name implies an expression consisting of just that name.
30 # Comments start with '--'.
32 # The output of this script is a 'dummy' function containing 'asm' declarations
33 # for each non-preprocessor line in the .pysym file. The expression values
34 # will appear as input operands to the 'asm' declaration. For example, if we
54 # -- This is a comment
56 # C3_OFFSET offsetof(struct S, c3)
65 # asm ("@name@MACRO@value@%0@" : : "i" (MACRO));
66 # asm ("@name@C3_OFFSET@value@%0@" : : "i" (offsetof(struct S, c3)));
67 # asm ("@name@E_ONE@value@%0@" : : "i" (ONE));
70 # We'll later feed this output to gcc -S. Since '-S' tells gcc to compile but
71 # not assemble, gcc will output something like:
75 # @name@MACRO@value@$42@
76 # @name@C3_OFFSET@value@$2@
77 # @name@E_ONE@value@$1@
79 # Finally, we can process that output to extract the constant values.
80 # Notice gcc may prepend a special character such as '$' to each value.
82 # found_symbol indicates whether we found a non-comment, non-preprocessor line.
83 BEGIN { found_symbol =
0 }
85 # C preprocessor directives go straight through.
91 # Trim leading whitespace.
92 { sub(/^
[[:blank
:]]*/, ""); }
94 # If we found a non-comment, non-preprocessor line, print the 'dummy' function
96 NF > 0 && !found_symbol
{
97 print "void dummy(void)\n{";
101 # If the line contains just a name, duplicate it so we can use that name
102 # as the value of the expression.
103 NF ==
1 { sub(/^.
*$
/, "& &"); }
105 # If a line contains a name and an expression...
109 # Remove any characters before the second field.
110 sub(/^
[^
[:blank
:]]+[[:blank
:]]+/, "");
112 # '$0' ends up being everything that appeared after the first field
114 printf " asm (\"@name@%s@value@%0@\" : : \"i\" (%s));\n", name
, $
0;
117 # Close the 'dummy' function.
118 END { if (found_symbol
) print "}"; }