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