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
diff --git a/runtime/Android.bp b/runtime/Android.bp
index f4b8697..9c52b73 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -642,6 +642,7 @@
     ],
     header_libs: [
         "art_cmdlineparser_headers", // For parsed_options_test.
+        "cpp-define-generator-definitions",
     ],
     include_dirs: [
         "external/zlib",
diff --git a/runtime/arch/arch_test.cc b/runtime/arch/arch_test.cc
index d4dbbf9..01b65bc 100644
--- a/runtime/arch/arch_test.cc
+++ b/runtime/arch/arch_test.cc
@@ -42,6 +42,12 @@
 // Generate CheckAsmSupportOffsetsAndSizes().
 #include "asm_support_check.h"
 
+// Static asserts to check the values of generated #defines for assembly.
+#define DEFINE_INCLUDE_DEPENDENCIES
+#include "offsets_all.def"
+#define DEFINE_EXPR(NAME, TYPE, EXPR) static_assert(NAME == EXPR, "Unexpected value of " #NAME);
+#include "offsets_all.def"
+
 namespace art {
 
 class ArchTest : public CommonRuntimeTest {
diff --git a/tools/cpp-define-generator/Android.bp b/tools/cpp-define-generator/Android.bp
index a94f404..0d3943e 100644
--- a/tools/cpp-define-generator/Android.bp
+++ b/tools/cpp-define-generator/Android.bp
@@ -14,16 +14,11 @@
 // limitations under the License.
 //
 
-// Build a "data" binary which will hold all the symbol values that will be parsed by the other scripts.
-//
-// Builds are for host only, target-specific define generation is possibly but is trickier and would need extra tooling.
-//
-// In the future we may wish to parameterize this on (32,64)x(read_barrier,no_read_barrier).
-
-cc_binary { // Do not use art_cc_binary because HOST_PREFER_32_BIT is incompatible with genrule.
-    name: "cpp-define-generator-data",
+// This produces human-readable asm_defines.s with the embedded compile-time constants.
+cc_object {
+    name: "asm_defines.s",
     host_supported: true,
-    device_supported: false,
+    device_supported: true,
     defaults: [
         "art_debug_defaults",
         "art_defaults",
@@ -33,20 +28,36 @@
         "art/libdexfile",
         "art/libartbase",
         "art/runtime",
+        "system/core/base/include",
     ],
-    srcs: ["main.cc"],
-    shared_libs: [
-        "libbase",
-    ],
+    // Produce text file rather than binary.
+    cflags: ["-S"],
+    srcs: ["asm_defines.cc"],
 }
 
-// Note: See $OUT_DIR/soong/build.ninja
-// For the exact filename that this generates to run make command on just
-// this rule later.
-genrule {
+// This extracts the compile-time constants from asm_defines.s and creates the header.
+cc_genrule {
     name: "cpp-define-generator-asm-support",
+    host_supported: true,
+    device_supported: true,
+    srcs: [":asm_defines.s"],
     out: ["asm_support_gen.h"],
-    tools: ["cpp-define-generator-data"],
-    tool_files: ["*.def"],
-    cmd: "$(location cpp-define-generator-data) > \"$(out)\"",
+    tool_files: ["make_header.py"],
+    cmd: "$(location make_header.py) \"$(in)\" > \"$(out)\"",
+}
+
+cc_library_headers {
+    name: "cpp-define-generator-definitions",
+    host_supported: true,
+    export_include_dirs: ["."],
+}
+
+python_binary_host {
+    name: "cpp-define-generator-test",
+    main: "make_header_test.py",
+    srcs: [
+        "make_header.py",
+        "make_header_test.py",
+    ],
+    test_suites: ["general-tests"],
 }
diff --git a/tools/cpp-define-generator/asm_defines.cc b/tools/cpp-define-generator/asm_defines.cc
new file mode 100644
index 0000000..de67f1b
--- /dev/null
+++ b/tools/cpp-define-generator/asm_defines.cc
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+// Art Offset file dependencies
+#define DEFINE_INCLUDE_DEPENDENCIES
+#include "offsets_all.def"
+
+// We use "asm volatile" to generate text that will stand out in the
+// compiler generated intermediate assembly file (eg. ">>FOO 42 0<<").
+// We emit all values as 64-bit integers (which we will printed as text).
+// We also store a flag which specifies whether the constant is negative.
+// Note that "asm volatile" must be inside a method to please the compiler.
+#define DEFINE_EXPR(NAME, TYPE, EXPR) \
+void AsmDefineHelperFor_##NAME() { \
+  asm volatile("\n.ascii \">>" #NAME " %0 %1<<\"" \
+  :: "i" (static_cast<int64_t>(EXPR)), "i" (EXPR < 0 ? 1 : 0)); \
+}
+#include "offsets_all.def"
diff --git a/tools/cpp-define-generator/main.cc b/tools/cpp-define-generator/main.cc
deleted file mode 100644
index 7c515be..0000000
--- a/tools/cpp-define-generator/main.cc
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#include <algorithm>
-#include <ios>
-#include <iostream>
-#include <sstream>
-#include <string>
-#include <type_traits>
-
-// Art Offset file dependencies
-#define DEFINE_INCLUDE_DEPENDENCIES
-#include "offsets_all.def"
-
-std::string to_upper(std::string input) {
-  std::transform(input.begin(), input.end(), input.begin(), ::toupper);
-  return input;
-}
-
-template <typename T, typename = void>
-typename std::enable_if<!std::is_signed<T>::value, std::string>::type
-pretty_format(T value) {
-  // Print most values as hex.
-  std::stringstream ss;
-  ss << std::showbase << std::hex << value;
-  return ss.str();
-}
-
-template <typename T, typename = void>
-typename std::enable_if<std::is_signed<T>::value, std::string>::type
-pretty_format(T value) {
-  // Print "signed" values as decimal so that the negativity doesn't get lost.
-  std::stringstream ss;
-
-  // For negative values add a (). Omit it from positive values for conciseness.
-  if (value < 0) {
-    ss << "(";
-  }
-
-  ss << value;
-
-  if (value < 0) {
-    ss << ")";
-  }
-  return ss.str();
-}
-
-template <typename T>
-void cpp_define(const std::string& name, T value) {
-  std::cout << "#define " << name << " " << pretty_format(value) << std::endl;
-}
-
-template <typename T>
-void emit_check_eq(T value, const std::string& expr) {
-  std::cout << "DEFINE_CHECK_EQ(" << value << ", (" << expr << "))" << std::endl;
-}
-
-const char *kFileHeader = /* // NOLINT [readability/multiline_string] [5] */ R"L1C3NS3(
-/*
- * Copyright (C) 2016 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.
- */
-
-#ifndef ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
-#define ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
-
-// This file has been auto-generated by cpp-define-generator; do not edit directly.
-)L1C3NS3";  // NOLINT [readability/multiline_string] [5]
-
-const char *kFileFooter = /* // NOLINT [readability/multiline_string] [5] */ R"F00T3R(
-#endif  // ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
-)F00T3R";  // NOLINT [readability/multiline_string] [5]
-
-#define MACROIZE(holder_type, field_name) to_upper(#holder_type "_" #field_name "_OFFSET")
-
-int main() {
-  std::cout << kFileHeader << std::endl;
-
-  std::string z = "";
-
-  // Print every constant expression to stdout as a #define or a CHECK_EQ
-#define DEFINE_EXPR(macro_name, field_type, expr) \
-  cpp_define(to_upper(#macro_name), static_cast<field_type>(expr)); \
-  emit_check_eq(z + "static_cast<" #field_type ">(" + to_upper(#macro_name) + ")", \
-                "static_cast<" #field_type ">(" #expr ")");
-#include "offsets_all.def"
-
-  std::cout << kFileFooter << std::endl;
-  return 0;
-}
diff --git a/tools/cpp-define-generator/make_header.py b/tools/cpp-define-generator/make_header.py
new file mode 100755
index 0000000..95ef7bc
--- /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()))
diff --git a/tools/cpp-define-generator/make_header_test.py b/tools/cpp-define-generator/make_header_test.py
new file mode 100755
index 0000000..eec1c68
--- /dev/null
+++ b/tools/cpp-define-generator/make_header_test.py
@@ -0,0 +1,53 @@
+#!/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.
+
+import unittest
+import make_header
+
+test_input = r'''
+// Check that the various other assembly lines are ignored.
+.globl  _Z49AsmDefineHelperFor_MIRROR_OBJECT_LOCK_WORD_OFFSETv
+.type   _Z49AsmDefineHelperFor_MIRROR_OBJECT_LOCK_WORD_OFFSETv,%function
+.ascii  ">>MIRROR_OBJECT_LOCK_WORD_OFFSET #4 #0<<"
+bx      lr
+
+// Check large positive 32-bit constant.
+.ascii  ">>OBJECT_ALIGNMENT_MASK_TOGGLED #4294967288 #0<<"
+
+// Check large positive 64-bit constant (it overflows into negative value).
+.ascii  ">>OBJECT_ALIGNMENT_MASK_TOGGLED64 #-8 #0<<"
+
+// Check negative constant.
+.ascii  ">>JIT_CHECK_OSR #-1 #1<<"
+'''
+
+test_output = r'''
+#define JIT_CHECK_OSR -0x1
+#define JIT_CHECK_OSR -0x1
+#define MIRROR_OBJECT_LOCK_WORD_OFFSET 0x4
+#define MIRROR_OBJECT_LOCK_WORD_OFFSET 0x4
+#define OBJECT_ALIGNMENT_MASK_TOGGLED 0xfffffff8
+#define OBJECT_ALIGNMENT_MASK_TOGGLED 0xfffffff8
+#define OBJECT_ALIGNMENT_MASK_TOGGLED64 0xfffffffffffffff8
+#define OBJECT_ALIGNMENT_MASK_TOGGLED64 0xfffffffffffffff8
+'''
+
+class CppDefineGeneratorTest(unittest.TestCase):
+  def test_convert(self):
+    self.assertEqual(test_output.strip(), make_header.convert(test_input))
+
+if __name__ == '__main__':
+  unittest.main()