David Srbecky | 0adf4d8 | 2018-10-01 18:17:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # This script looks through compiled object file (stored human readable text), |
| 18 | # and looks for the compile-time constants (added through custom "asm" block). |
| 19 | # For example: .ascii ">>OBJECT_ALIGNMENT_MASK $7 $0<<" |
| 20 | # |
| 21 | # It will transform each such line to #define which is usabe in assembly code. |
| 22 | # For example: #define OBJECT_ALIGNMENT_MASK 0x7 |
| 23 | # |
| 24 | # Usage: make_header.py out/soong/.intermediates/.../asm_defines.o |
| 25 | # |
| 26 | |
| 27 | import argparse |
| 28 | import re |
| 29 | import sys |
| 30 | |
| 31 | def convert(input): |
| 32 | """Find all defines in the compiler generated assembly and convert them to #define pragmas""" |
| 33 | |
| 34 | asm_define_re = re.compile(r'">>(\w+) (?:\$|#)([-0-9]+) (?:\$|#)(0|1)<<"') |
| 35 | asm_defines = asm_define_re.findall(input) |
| 36 | if not asm_defines: |
| 37 | raise RuntimeError("Failed to find any asm defines in the input") |
| 38 | |
| 39 | # Convert the found constants to #define pragmas. |
| 40 | # In case the C++ compiler decides to reorder the AsmDefinesFor_${name} functions, |
| 41 | # we don't want the order of the .h file to change from one compilation to another. |
| 42 | # Sorting ensures deterministic order of the #defines. |
| 43 | output = [] |
| 44 | for name, value, negative_value in sorted(asm_defines): |
| 45 | value = int(value) |
| 46 | if value < 0 and negative_value == "0": |
| 47 | # Overflow - uint64_t constant was pretty printed as negative value. |
| 48 | value += 2 ** 64 # Python will use arbitrary precision arithmetic. |
| 49 | output.append("#define {0} {1:#x}".format(name, value)) |
David Srbecky | 0adf4d8 | 2018-10-01 18:17:45 +0100 | [diff] [blame] | 50 | return "\n".join(output) |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | parser = argparse.ArgumentParser() |
| 54 | parser.add_argument('input', help="Object file as text") |
| 55 | args = parser.parse_args() |
| 56 | print(convert(open(args.input, "r").read())) |