BZ #15711: Avoid circular dependency for syscall.h
[glibc.git] / scripts / gen-as-const.awk
blob1ffd5f2c1cbd8c7f59fe8173f542d668f8aff852
1 # Script used in producing headers of assembly constants from C expressions.
2 # The input to this script looks like:
3 # #cpp-directive ...
4 # NAME1
5 # NAME2 expression ...
6 # The output of this script is C code to be run through gcc -S and then
7 # massaged to extract the integer constant values of the given C expressions.
8 # A line giving just a name implies an expression consisting of just that name.
10 BEGIN { started = 0 }
12 # cpp directives go straight through.
13 /^#/ { print; next }
15 NF >= 1 && !started {
16 if (test) {
17 print "\n#include <inttypes.h>";
18 print "\n#include <stdio.h>";
19 print "\n#include <bits/wordsize.h>";
20 print "\n#if __WORDSIZE == 64";
21 print "\ntypedef uint64_t c_t;";
22 print "\n#define U(n) UINT64_C (n)";
23 print "\n#define PRI PRId64";
24 print "\n#else";
25 print "\ntypedef uint32_t c_t;";
26 print "\n#define U(n) UINT32_C (n)";
27 print "\n#define PRI PRId32";
28 print "\n#endif";
29 print "\nstatic int do_test (void)\n{\n int bad = 0, good = 0;\n";
30 print "#define TEST(name, source, expr) \\\n" \
31 " if (U (asconst_##name) != (c_t) (expr)) { ++bad;" \
32 " fprintf (stderr, \"%s: %s is %\" PRI \" but %s is %\"PRI \"\\n\"," \
33 " source, #name, U (asconst_##name), #expr, (c_t) (expr));" \
34 " } else ++good;\n";
36 else
37 print "void dummy(void) {";
38 started = 1;
41 # Separator.
42 $1 == "--" { next }
44 NF == 1 { sub(/^.*$/, "& &"); }
46 NF > 1 {
47 name = $1;
48 sub(/^[^ ]+[ ]+/, "");
49 if (test)
50 print " TEST (" name ", \"" FILENAME ":" FNR "\", " $0 ")";
51 else
52 printf "asm (\"@@@name@@@%s@@@value@@@%%0@@@end@@@\" : : \"i\" ((long) %s));\n",
53 name, $0;
56 END {
57 if (test) {
58 print " printf (\"%d errors in %d tests\\n\", bad, good + bad);"
59 print " return bad != 0 || good == 0;\n}\n";
60 print "#define TEST_FUNCTION do_test ()";
62 else if (started) print "}";