* scripts/gen-as-const.awk: Generate more widely usable code by
[glibc.git] / scripts / gen-as-const.awk
blob23f2f2bc9bd23dbbbf44243607f32de45b19fbcc
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#define U(n) UINT64_C (n)";
20 print "\nstatic int do_test (void)\n{\n int bad = 0, good = 0;\n";
21 print "#define TEST(name, source, expr) \\\n" \
22 " if (U (asconst_##name) != (uint64_t) (expr)) { ++bad;" \
23 " fprintf (stderr, \"%s: %s is %\" PRId64 \" but %s is %\"PRId64 \"\\n\"," \
24 " source, #name, U (asconst_##name), #expr, (uint64_t) (expr));" \
25 " } else ++good;\n";
27 else
28 print "void dummy(void) {";
29 started = 1;
32 # Separator.
33 $1 == "--" { next }
35 NF == 1 { sub(/^.*$/, "& &"); }
37 NF > 1 {
38 name = $1;
39 sub(/^[^ ]+[ ]+/, "");
40 if (test)
41 print " TEST (" name ", \"" FILENAME ":" FNR "\", " $0 ")";
42 else
43 printf "asm (\"@@@name@@@%s@@@value@@@%%0@@@end@@@\" : : \"i\" ((long) %s));\n",
44 name, $0;
47 END {
48 if (test) {
49 print " printf (\"%d errors in %d tests\\n\", bad, good + bad);"
50 print " return bad != 0 || good == 0;\n}\n";
51 print "#define TEST_FUNCTION do_test ()";
53 else if (started) print "}";