summaryrefslogtreecommitdiffstats
path: root/tools/cpp-define-generator/main.cc
blob: 7c515be12f5570a4bec7ee9626cbc227a9c2537f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * 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;
}