1d4c55
Partial backport of:
1d4c55
1d4c55
commit cb7be1590e9b18e272e72eb4e910a7ad06a53bd0
1d4c55
Author: Joseph Myers <joseph@codesourcery.com>
1d4c55
Date:   Mon Dec 10 22:56:59 2018 +0000
1d4c55
1d4c55
    Use gen-as-const.py to process .pysym files.
1d4c55
    
1d4c55
    This patch eliminates the gen-py-const.awk variant of gen-as-const,
1d4c55
    switching to use of gnu-as-const.py (with a new --python option) to
1d4c55
    process .pysym files (i.e., to generate nptl_lock_constants.py), as
1d4c55
    the syntax of those files is identical to that of .sym files.
1d4c55
    
1d4c55
    Note that the generated nptl_lock_constants.py is *not* identical to
1d4c55
    the version generated by the awk script.  Apart from the trivial
1d4c55
    changes (comment referencing the new script, and output being sorted),
1d4c55
    the constant FUTEX_WAITERS, PTHREAD_MUTEXATTR_FLAG_BITS,
1d4c55
    PTHREAD_MUTEXATTR_FLAG_PSHARED and PTHREAD_MUTEX_PRIO_CEILING_MASK are
1d4c55
    now output as positive rather than negative constants (on x86_64
1d4c55
    anyway; maybe not necessarily on 32-bit systems):
1d4c55
    
1d4c55
    < FUTEX_WAITERS = -2147483648
1d4c55
    ---
1d4c55
    > FUTEX_WAITERS = 2147483648
1d4c55
    
1d4c55
    < PTHREAD_MUTEXATTR_FLAG_BITS = -251662336
1d4c55
    < PTHREAD_MUTEXATTR_FLAG_PSHARED = -2147483648
1d4c55
    ---
1d4c55
    > PTHREAD_MUTEXATTR_FLAG_BITS = 4043304960
1d4c55
    > PTHREAD_MUTEXATTR_FLAG_PSHARED = 2147483648
1d4c55
    
1d4c55
    < PTHREAD_MUTEX_PRIO_CEILING_MASK = -524288
1d4c55
    ---
1d4c55
    > PTHREAD_MUTEX_PRIO_CEILING_MASK = 4294443008
1d4c55
    
1d4c55
    This is because gen-as-const has a cast of the constant value to long
1d4c55
    int, which gen-py-const lacks.
1d4c55
    
1d4c55
    I think the positive values are more logically correct, since the
1d4c55
    constants in question are in fact unsigned in C.  But to reliably
1d4c55
    produce gen-as-const.py output for constants that always (in C and
1d4c55
    Python) reflects the signedness of values with the high bit of "long
1d4c55
    int" set would mean more complicated logic needs to be used in
1d4c55
    computing values.
1d4c55
    
1d4c55
    The more correct positive values by themselves produce a failure of
1d4c55
    nptl/test-mutexattr-printers, because masking with
1d4c55
    ~PTHREAD_MUTEXATTR_FLAG_BITS & ~PTHREAD_MUTEX_NO_ELISION_NP now leaves
1d4c55
    a bit -1 << 32 in the Python value, resulting in a KeyError exception.
1d4c55
    To avoid that, places masking with ~ of one of the constants in
1d4c55
    question are changed to mask with 0xffffffff as well (this reflects
1d4c55
    how ~ in Python applies to an infinite-precision integer whereas ~ in
1d4c55
    C does not do any promotions beyond the width of int).
1d4c55
    
1d4c55
    Tested for x86_64.
1d4c55
    
1d4c55
            * scripts/gen-as-const.py (main): Handle --python option.
1d4c55
            * scripts/gen-py-const.awk: Remove.
1d4c55
            * Makerules (py-const-script): Use gen-as-const.py.
1d4c55
            ($(py-const)): Likewise.
1d4c55
            * nptl/nptl-printers.py (MutexPrinter.read_status_no_robust): Mask
1d4c55
            with 0xffffffff together with ~(PTHREAD_MUTEX_PRIO_CEILING_MASK).
1d4c55
            (MutexAttributesPrinter.read_values): Mask with 0xffffffff
1d4c55
            together with ~PTHREAD_MUTEXATTR_FLAG_BITS and
1d4c55
            ~PTHREAD_MUTEX_NO_ELISION_NP.
1d4c55
            * manual/README.pretty-printers: Update reference to
1d4c55
            gen-py-const.awk.
1d4c55
1d4c55
Only the gen-as-const.py changes are included downstream.  We keep using
1d4c55
gen-py-const.awk for the build.
1d4c55
1d4c55
diff --git a/scripts/gen-as-const.py b/scripts/gen-as-const.py
1d4c55
index f85e359394acb1a4..2f1dff092b98e044 100644
1d4c55
--- a/scripts/gen-as-const.py
1d4c55
+++ b/scripts/gen-as-const.py
1d4c55
@@ -75,6 +75,8 @@ def main():
1d4c55
                         help='C compiler (including options) to use')
1d4c55
     parser.add_argument('--test', action='store_true',
1d4c55
                         help='Generate test case instead of header')
1d4c55
+    parser.add_argument('--python', action='store_true',
1d4c55
+                        help='Generate Python file instead of header')
1d4c55
     parser.add_argument('sym_file',
1d4c55
                         help='.sym file to process')
1d4c55
     args = parser.parse_args()
1d4c55
@@ -103,6 +105,13 @@ def main():
1d4c55
             sym_data.append('START')
1d4c55
     if args.test:
1d4c55
         print(gen_test(sym_data))
1d4c55
+    elif args.python:
1d4c55
+        consts = glibcextract.compute_c_consts(sym_data, args.cc)
1d4c55
+        print('# GENERATED FILE\n'
1d4c55
+              '\n'
1d4c55
+              '# Constant definitions.\n'
1d4c55
+              '# See gen-as-const.py for details.\n')
1d4c55
+        print(''.join('%s = %s\n' % c for c in sorted(consts.items())), end='')
1d4c55
     else:
1d4c55
         consts = glibcextract.compute_c_consts(sym_data, args.cc)
1d4c55
         print(''.join('#define %s %s\n' % c for c in sorted(consts.items())), end='')