2 # Produce headers of assembly constants from C expressions.
3 # Copyright (C) 2018-2022 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 # <https://www.gnu.org/licenses/>.
20 # The input to this script looks like:
23 # NAME2 expression ...
24 # A line giving just a name implies an expression consisting of just that name.
31 def gen_test(sym_data
):
32 """Generate a test for the values of some C constants.
34 The first argument is as for glibcextract.compute_c_consts.
39 if isinstance(arg
, str):
41 out_lines
.append('#include <stdint.h>\n'
42 '#include <stdio.h>\n'
43 '#include <bits/wordsize.h>\n'
44 '#if __WORDSIZE == 64\n'
45 'typedef uint64_t c_t;\n'
46 '# define U(n) UINT64_C (n)\n'
48 'typedef uint32_t c_t;\n'
49 '# define U(n) UINT32_C (n)\n'
54 # Compilation test only, using static
58 '#include <support/test-driver.c>')
64 out_lines
.append('_Static_assert (U (asconst_%s) == (c_t) (%s), '
66 % (name
, value
, name
))
67 return '\n'.join(out_lines
)
71 """The main entry point."""
72 parser
= argparse
.ArgumentParser(
73 description
='Produce headers of assembly constants.')
74 parser
.add_argument('--cc', metavar
='CC',
75 help='C compiler (including options) to use')
76 parser
.add_argument('--test', action
='store_true',
77 help='Generate test case instead of header')
78 parser
.add_argument('--python', action
='store_true',
79 help='Generate Python file instead of header')
80 parser
.add_argument('sym_file',
81 help='.sym file to process')
82 args
= parser
.parse_args()
84 with
open(args
.sym_file
, 'r') as sym_file
:
90 # Pass preprocessor directives through.
91 if line
.startswith('#'):
94 words
= line
.split(maxsplit
=1)
96 sym_data
.append('START')
102 value
= words
[1] if len(words
) > 1 else words
[0]
103 sym_data
.append((name
, value
))
105 sym_data
.append('START')
107 print(gen_test(sym_data
))
109 consts
= glibcextract
.compute_c_consts(sym_data
, args
.cc
)
110 print('# GENERATED FILE\n'
112 '# Constant definitions.\n'
113 '# See gen-as-const.py for details.\n')
114 print(''.join('%s = %s\n' % c
for c
in sorted(consts
.items())), end
='')
116 consts
= glibcextract
.compute_c_consts(sym_data
, args
.cc
)
117 print(''.join('#define %s %s\n' % c
for c
in sorted(consts
.items())), end
='')
119 if __name__
== '__main__':