Blame scripts/gen-py-const.awk

Packit 6c4009
# Script to generate constants for Python pretty printers.
Packit 6c4009
#
Packit 6c4009
# Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
# This file is part of the GNU C Library.
Packit 6c4009
#
Packit 6c4009
# The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
# modify it under the terms of the GNU Lesser General Public
Packit 6c4009
# License as published by the Free Software Foundation; either
Packit 6c4009
# version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
#
Packit 6c4009
# The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
# Lesser General Public License for more details.
Packit 6c4009
#
Packit 6c4009
# You should have received a copy of the GNU Lesser General Public
Packit 6c4009
# License along with the GNU C Library; if not, see
Packit 6c4009
# <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
# This script is a smaller version of the clever gen-asm-const.awk hack used to
Packit 6c4009
# generate ASM constants from .sym files.  We'll use this to generate constants
Packit 6c4009
# for Python pretty printers.
Packit 6c4009
#
Packit 6c4009
# The input to this script are .pysym files that look like:
Packit 6c4009
# #C_Preprocessor_Directive...
Packit 6c4009
# NAME1
Packit 6c4009
# NAME2 expression...
Packit 6c4009
#
Packit 6c4009
# A line giving just a name implies an expression consisting of just that name.
Packit 6c4009
# Comments start with '--'.
Packit 6c4009
#
Packit 6c4009
# The output of this script is a 'dummy' function containing 'asm' declarations
Packit 6c4009
# for each non-preprocessor line in the .pysym file.  The expression values
Packit 6c4009
# will appear as input operands to the 'asm' declaration.  For example, if we
Packit 6c4009
# have:
Packit 6c4009
#
Packit 6c4009
# /* header.h */
Packit 6c4009
# #define MACRO 42
Packit 6c4009
#
Packit 6c4009
# struct S {
Packit 6c4009
#     char c1;
Packit 6c4009
#     char c2;
Packit 6c4009
#     char c3;
Packit 6c4009
# };
Packit 6c4009
#
Packit 6c4009
# enum E {
Packit 6c4009
#     ZERO,
Packit 6c4009
#     ONE
Packit 6c4009
# };
Packit 6c4009
#
Packit 6c4009
# /* symbols.pysym */
Packit 6c4009
# #include <stddef.h>
Packit 6c4009
# #include "header.h"
Packit 6c4009
# -- This is a comment
Packit 6c4009
# MACRO
Packit 6c4009
# C3_OFFSET offsetof(struct S, c3)
Packit 6c4009
# E_ONE ONE
Packit 6c4009
#
Packit 6c4009
# the output will be:
Packit 6c4009
#
Packit 6c4009
# #include <stddef.h>
Packit 6c4009
# #include "header.h"
Packit 6c4009
# void dummy(void)
Packit 6c4009
# {
Packit 6c4009
#   asm ("@name@MACRO@value@%0@" : : "i" (MACRO));
Packit 6c4009
#   asm ("@name@C3_OFFSET@value@%0@" : : "i" (offsetof(struct S, c3)));
Packit 6c4009
#   asm ("@name@E_ONE@value@%0@" : : "i" (ONE));
Packit 6c4009
# }
Packit 6c4009
#
Packit 6c4009
# We'll later feed this output to gcc -S.  Since '-S' tells gcc to compile but
Packit 6c4009
# not assemble, gcc will output something like:
Packit 6c4009
#
Packit 6c4009
# dummy:
Packit 6c4009
# 	...
Packit 6c4009
# 	@name@MACRO@value@$42@
Packit 6c4009
# 	@name@C3_OFFSET@value@$2@
Packit 6c4009
# 	@name@E_ONE@value@$1@
Packit 6c4009
#
Packit 6c4009
# Finally, we can process that output to extract the constant values.
Packit 6c4009
# Notice gcc may prepend a special character such as '$' to each value.
Packit 6c4009
Packit 6c4009
# found_symbol indicates whether we found a non-comment, non-preprocessor line.
Packit 6c4009
BEGIN { found_symbol = 0 }
Packit 6c4009
Packit 6c4009
# C preprocessor directives go straight through.
Packit 6c4009
/^#/ { print; next; }
Packit 6c4009
Packit 6c4009
# Skip comments.
Packit 6c4009
/--/ { next; }
Packit 6c4009
Packit 6c4009
# Trim leading whitespace.
Packit 6c4009
{ sub(/^[[:blank:]]*/, ""); }
Packit 6c4009
Packit 6c4009
# If we found a non-comment, non-preprocessor line, print the 'dummy' function
Packit 6c4009
# header.
Packit 6c4009
NF > 0 && !found_symbol {
Packit 6c4009
    print "void dummy(void)\n{";
Packit 6c4009
    found_symbol = 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# If the line contains just a name, duplicate it so we can use that name
Packit 6c4009
# as the value of the expression.
Packit 6c4009
NF == 1 { sub(/^.*$/, "& &";; }
Packit 6c4009
Packit 6c4009
# If a line contains a name and an expression...
Packit 6c4009
NF > 1 {
Packit 6c4009
    name = $1;
Packit 6c4009
Packit 6c4009
    # Remove any characters before the second field.
Packit 6c4009
    sub(/^[^[:blank:]]+[[:blank:]]+/, "");
Packit 6c4009
Packit 6c4009
    # '$0' ends up being everything that appeared after the first field
Packit 6c4009
    # separator.
Packit 6c4009
    printf "  asm (\"@name@%s@value@%0@\" : : \"i\" (%s));\n", name, $0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# Close the 'dummy' function.
Packit 6c4009
END { if (found_symbol) print "}"; }