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