S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / scripts / gen-py-const.awk
blob694b8a5fd0ca20c2bf9134f7d31926a3a0d78e71
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...
26 # NAME1
27 # NAME2 expression...
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
35 # have:
37 # /* header.h */
38 # #define MACRO 42
40 # struct S {
41 # char c1;
42 # char c2;
43 # char c3;
44 # };
46 # enum E {
47 # ZERO,
48 # ONE
49 # };
51 # /* symbols.pysym */
52 # #include <stddef.h>
53 # #include "header.h"
54 # -- This is a comment
55 # MACRO
56 # C3_OFFSET offsetof(struct S, c3)
57 # E_ONE ONE
59 # the output will be:
61 # #include <stddef.h>
62 # #include "header.h"
63 # void dummy(void)
64 # {
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));
68 # }
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:
73 # dummy:
74 # ...
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.
86 /^#/ { print; next; }
88 # Skip comments.
89 /--/ { next; }
91 # Trim leading whitespace.
92 { sub(/^[[:blank:]]*/, ""); }
94 # If we found a non-comment, non-preprocessor line, print the 'dummy' function
95 # header.
96 NF > 0 && !found_symbol {
97 print "void dummy(void)\n{";
98 found_symbol = 1;
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...
106 NF > 1 {
107 name = $1;
109 # Remove any characters before the second field.
110 sub(/^[^[:blank:]]+[[:blank:]]+/, "");
112 # '$0' ends up being everything that appeared after the first field
113 # separator.
114 printf " asm (\"@name@%s@value@%0@\" : : \"i\" (%s));\n", name, $0;
117 # Close the 'dummy' function.
118 END { if (found_symbol) print "}"; }