Blame scripts/gen-as-const.awk

Packit Service 82fcde
# Script used in producing headers of assembly constants from C expressions.
Packit Service 82fcde
# The input to this script looks like:
Packit Service 82fcde
#	#cpp-directive ...
Packit Service 82fcde
#	NAME1
Packit Service 82fcde
#	NAME2 expression ...
Packit Service 82fcde
# The output of this script is C code to be run through gcc -S and then
Packit Service 82fcde
# massaged to extract the integer constant values of the given C expressions.
Packit Service 82fcde
# A line giving just a name implies an expression consisting of just that name.
Packit Service 82fcde
Packit Service 82fcde
BEGIN { started = 0 }
Packit Service 82fcde
Packit Service 82fcde
# cpp directives go straight through.
Packit Service 82fcde
/^#/ { print; next }
Packit Service 82fcde
Packit Service 82fcde
NF >= 1 && !started {
Packit Service 82fcde
  if (test) {
Packit Service 82fcde
    print "\n#include <inttypes.h>";
Packit Service 82fcde
    print "\n#include <stdio.h>";
Packit Service 82fcde
    print "\n#include <bits/wordsize.h>";
Packit Service 82fcde
    print "\n#if __WORDSIZE == 64";
Packit Service 82fcde
    print "\ntypedef uint64_t c_t;";
Packit Service 82fcde
    print "\n#define U(n) UINT64_C (n)";
Packit Service 82fcde
    print "\n#define PRI PRId64";
Packit Service 82fcde
    print "\n#else";
Packit Service 82fcde
    print "\ntypedef uint32_t c_t;";
Packit Service 82fcde
    print "\n#define U(n) UINT32_C (n)";
Packit Service 82fcde
    print "\n#define PRI PRId32";
Packit Service 82fcde
    print "\n#endif";
Packit Service 82fcde
    print "\nstatic int do_test (void)\n{\n  int bad = 0, good = 0;\n";
Packit Service 82fcde
    print "#define TEST(name, source, expr) \\\n" \
Packit Service 82fcde
      "  if (U (asconst_##name) != (c_t) (expr)) { ++bad;" \
Packit Service 82fcde
      " fprintf (stderr, \"%s: %s is %\" PRI \" but %s is %\"PRI \"\\n\"," \
Packit Service 82fcde
      " source, #name, U (asconst_##name), #expr, (c_t) (expr));" \
Packit Service 82fcde
      " } else ++good;\n";
Packit Service 82fcde
  }
Packit Service 82fcde
  else
Packit Service 82fcde
    print "void dummy(void) {";
Packit Service 82fcde
  started = 1;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
# Separator.
Packit Service 82fcde
$1 == "--" { next }
Packit Service 82fcde
Packit Service 82fcde
NF == 1 { sub(/^.*$/, "& &";; }
Packit Service 82fcde
Packit Service 82fcde
NF > 1 {
Packit Service 82fcde
  name = $1;
Packit Service 82fcde
  sub(/^[^ 	]+[ 	]+/, "");
Packit Service 82fcde
  if (test)
Packit Service 82fcde
    print "  TEST (" name ", \"" FILENAME ":" FNR "\", " $0 ")";
Packit Service 82fcde
  else
Packit Service 82fcde
    printf "asm (\"@@@name@@@%s@@@value@@@%%0@@@end@@@\" : : \"i\" ((long) %s));\n",
Packit Service 82fcde
      name, $0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
END {
Packit Service 82fcde
  if (test) {
Packit Service 82fcde
    print "  printf (\"%d errors in %d tests\\n\", bad, good + bad);"
Packit Service 82fcde
    print "  return bad != 0 || good == 0;\n}\n";
Packit Service 82fcde
    print "#define TEST_FUNCTION do_test ()";
Packit Service 82fcde
  }
Packit Service 82fcde
  else if (started) print "}";
Packit Service 82fcde
}