about summary refs log tree commit diff
path: root/scripts/gen-as-const.awk
blob: 23f2f2bc9bd23dbbbf44243607f32de45b19fbcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Script used in producing headers of assembly constants from C expressions.
# The input to this script looks like:
#	#cpp-directive ...
#	NAME1
#	NAME2 expression ...
# The output of this script is C code to be run through gcc -S and then
# massaged to extract the integer constant values of the given C expressions.
# A line giving just a name implies an expression consisting of just that name.

BEGIN { started = 0 }

# cpp directives go straight through.
/^#/ { print; next }

NF >= 1 && !started {
  if (test) {
    print "\n#include <inttypes.h>";
    print "\n#include <stdio.h>";
    print "\n#define U(n) UINT64_C (n)";
    print "\nstatic int do_test (void)\n{\n  int bad = 0, good = 0;\n";
    print "#define TEST(name, source, expr) \\\n" \
      "  if (U (asconst_##name) != (uint64_t) (expr)) { ++bad;" \
      " fprintf (stderr, \"%s: %s is %\" PRId64 \" but %s is %\"PRId64 \"\\n\"," \
      " source, #name, U (asconst_##name), #expr, (uint64_t) (expr));" \
      " } else ++good;\n";
  }
  else
    print "void dummy(void) {";
  started = 1;
}

# Separator.
$1 == "--" { next }

NF == 1 { sub(/^.*$/, "& &"); }

NF > 1 {
  name = $1;
  sub(/^[^ 	]+[ 	]+/, "");
  if (test)
    print "  TEST (" name ", \"" FILENAME ":" FNR "\", " $0 ")";
  else
    printf "asm (\"@@@name@@@%s@@@value@@@%%0@@@end@@@\" : : \"i\" ((long) %s));\n",
      name, $0;
}

END {
  if (test) {
    print "  printf (\"%d errors in %d tests\\n\", bad, good + bad);"
    print "  return bad != 0 || good == 0;\n}\n";
    print "#define TEST_FUNCTION do_test ()";
  }
  else if (started) print "}";
}