aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/CppBackend/CPPBackend.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-12-03 22:42:07 -0500
committerAlon Zakai <alonzakai@gmail.com>2013-12-03 22:42:07 -0500
commit8682fe4be604a244616b34afe61d89b35892fc7b (patch)
tree6061626ffa881a9c7c278c9c38671bb7a5979abd /lib/Target/CppBackend/CPPBackend.cpp
parent0b932ccd52067e83aa413c23d1ef4cbc93e07cdb (diff)
emit global initializers
Diffstat (limited to 'lib/Target/CppBackend/CPPBackend.cpp')
-rw-r--r--lib/Target/CppBackend/CPPBackend.cpp56
1 files changed, 39 insertions, 17 deletions
diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp
index ed166a178c..7aba24cb26 100644
--- a/lib/Target/CppBackend/CPPBackend.cpp
+++ b/lib/Target/CppBackend/CPPBackend.cpp
@@ -146,6 +146,7 @@ namespace {
std::string PostSets;
std::map<std::string, unsigned> IndexedFunctions; // name -> index
FunctionTableMap FunctionTables; // sig => list of functions
+ std::vector<std::string> GlobalInitializers;
#include "CallHandlers.h"
@@ -2458,7 +2459,19 @@ void CppWriter::printModuleBody() {
if (--Num > 0) Out << ",";
Out << "\n";
}
- Out << "}";
+ Out << "},";
+
+ Out << "\"initializers\": [";
+ first = true;
+ for (unsigned i = 0; i < GlobalInitializers.size(); i++) {
+ if (first) {
+ first = false;
+ } else {
+ Out << ", ";
+ }
+ Out << "\"" + GlobalInitializers[i] + "\"";
+ }
+ Out << "]";
Out << "\n}\n";
}
@@ -2591,24 +2604,33 @@ void CppWriter::parseConstant(std::string name, const Constant* CV, bool calcula
} else if (isa<BlockAddress>(CV)) {
assert(false);
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
- if (CE->getOpcode() == Instruction::GetElementPtr) {
- assert(false && "Unhandled CE GEP");
- } else if (CE->isCast()) {
- // a global equal to a ptrtoint of some function, so a 32-bit integer for us
- if (calculate) {
- HeapData *GlobalData = allocateAddress(name);
- for (unsigned i = 0; i < 4; ++i) {
- GlobalData->push_back(0);
+ if (CE->isCast()) {
+ if (name == "__init_array_start") {
+ // this is the global static initializer
+ if (calculate) {
+ Value *V = CE->getOperand(0);
+ GlobalInitializers.push_back(getCppName(V));
+ // is the func waka
}
+ } else if (name == "__fini_array_start") {
+ // nothing to do
} else {
- unsigned Offset = getRelativeGlobalAddress(name);
- Value *V = CE->getOperand(0);
- unsigned Data = getConstAsOffset(V, getGlobalAddress(name));
- union { unsigned i; unsigned char b[sizeof(unsigned)]; } integer;
- integer.i = Data;
- assert(Offset+4 <= GlobalData64.size());
- for (unsigned i = 0; i < 4; ++i) {
- GlobalData64[Offset++] = integer.b[i];
+ // a global equal to a ptrtoint of some function, so a 32-bit integer for us
+ if (calculate) {
+ HeapData *GlobalData = allocateAddress(name);
+ for (unsigned i = 0; i < 4; ++i) {
+ GlobalData->push_back(0);
+ }
+ } else {
+ unsigned Offset = getRelativeGlobalAddress(name);
+ Value *V = CE->getOperand(0);
+ unsigned Data = getConstAsOffset(V, getGlobalAddress(name));
+ union { unsigned i; unsigned char b[sizeof(unsigned)]; } integer;
+ integer.i = Data;
+ assert(Offset+4 <= GlobalData64.size());
+ for (unsigned i = 0; i < 4; ++i) {
+ GlobalData64[Offset++] = integer.b[i];
+ }
}
}
} else {