From 0adf4d80ca1c673e5f7c5249faabadccdc1ddbbd Mon Sep 17 00:00:00 2001 From: David Srbecky Date: Mon, 1 Oct 2018 18:17:45 +0100 Subject: Rewrite cpp-define-generator The new method works by generating temporary per-architecture human-readable object file with the constants embedded in it. Python script extracts those values and generates the header. This means the values can now implicitly depend on pointer size, compile time flags, or ABI specific object layout with no hacks. Test: test-art-host-gtest-arch_test Change-Id: Id6e8c77c01f9d6c49cd6d40e3487b56fa4777349 --- tools/cpp-define-generator/make_header.py | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 tools/cpp-define-generator/make_header.py (limited to 'tools/cpp-define-generator/make_header.py') diff --git a/tools/cpp-define-generator/make_header.py b/tools/cpp-define-generator/make_header.py new file mode 100755 index 0000000000..95ef7bc56d --- /dev/null +++ b/tools/cpp-define-generator/make_header.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# +# Copyright (C) 2018 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script looks through compiled object file (stored human readable text), +# and looks for the compile-time constants (added through custom "asm" block). +# For example: .ascii ">>OBJECT_ALIGNMENT_MASK $7 $0<<" +# +# It will transform each such line to #define which is usabe in assembly code. +# For example: #define OBJECT_ALIGNMENT_MASK 0x7 +# +# Usage: make_header.py out/soong/.intermediates/.../asm_defines.o +# + +import argparse +import re +import sys + +def convert(input): + """Find all defines in the compiler generated assembly and convert them to #define pragmas""" + + asm_define_re = re.compile(r'">>(\w+) (?:\$|#)([-0-9]+) (?:\$|#)(0|1)<<"') + asm_defines = asm_define_re.findall(input) + if not asm_defines: + raise RuntimeError("Failed to find any asm defines in the input") + + # Convert the found constants to #define pragmas. + # In case the C++ compiler decides to reorder the AsmDefinesFor_${name} functions, + # we don't want the order of the .h file to change from one compilation to another. + # Sorting ensures deterministic order of the #defines. + output = [] + for name, value, negative_value in sorted(asm_defines): + value = int(value) + if value < 0 and negative_value == "0": + # Overflow - uint64_t constant was pretty printed as negative value. + value += 2 ** 64 # Python will use arbitrary precision arithmetic. + output.append("#define {0} {1:#x}".format(name, value)) + output.append("#define {0} {1:#x}".format(name.upper(), value)) + return "\n".join(output) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('input', help="Object file as text") + args = parser.parse_args() + print(convert(open(args.input, "r").read())) -- cgit v1.2.3-59-g8ed1b