aboutsummaryrefslogtreecommitdiff
path: root/lib/Bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Bytecode')
-rw-r--r--lib/Bytecode/Reader/Reader.cpp19
-rw-r--r--lib/Bytecode/Writer/SlotCalculator.cpp8
-rw-r--r--lib/Bytecode/Writer/Writer.cpp16
3 files changed, 36 insertions, 7 deletions
diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp
index 71fec255cc..a2038edf34 100644
--- a/lib/Bytecode/Reader/Reader.cpp
+++ b/lib/Bytecode/Reader/Reader.cpp
@@ -323,14 +323,29 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
unsigned VarType;
if (read_vbr(Buf, End, VarType)) return failure(true);
while (VarType != Type::VoidTyID) { // List is terminated by Void
- const Type *Ty = getType(VarType);
+ // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
+ const Type *Ty = getType(VarType >> 2);
if (!Ty || !Ty->isPointerType()) {
cerr << "Global not pointer type! Ty = " << Ty << endl;
return failure(true);
}
+ ConstPoolVal *Initializer = 0;
+ if (VarType & 2) { // Does it have an initalizer?
+ // Do not improvise... values must have been stored in the constant pool,
+ // which should have been read before now.
+ //
+ unsigned InitSlot;
+ if (read_vbr(Buf, End, InitSlot)) return failure(true);
+
+ Value *V = getValue(Ty->castPointerType()->getValueType(),
+ InitSlot, false);
+ if (V == 0) return failure(true);
+ Initializer = V->castConstantAsserting();
+ }
+
// Create the global variable...
- GlobalVariable *GV = new GlobalVariable(Ty);
+ GlobalVariable *GV = new GlobalVariable(Ty, VarType & 1, Initializer);
insertValue(GV, ModuleValues);
C->getGlobalList().push_back(GV);
diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp
index cc7d4e56df..0ac371cbce 100644
--- a/lib/Bytecode/Writer/SlotCalculator.cpp
+++ b/lib/Bytecode/Writer/SlotCalculator.cpp
@@ -223,11 +223,13 @@ int SlotCalculator::getValSlot(const Value *D) const {
int SlotCalculator::insertValue(const Value *D) {
- if (const ConstPoolVal *CPV = D->castConstant()) {
+ if (D->isConstant() || D->isGlobal()) {
+ const User *U = (const User *)D;
// This makes sure that if a constant has uses (for example an array
- // of const ints), that they are inserted also.
+ // of const ints), that they are inserted also. Same for global variable
+ // initializers.
//
- for_each(CPV->op_begin(), CPV->op_end(),
+ for_each(U->op_begin(), U->op_end(),
bind_obj(this, &SlotCalculator::insertValue));
}
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index e6562f5fb9..da480180a8 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -120,9 +120,21 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
// Output the types for the global variables in the module...
for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
- int Slot = Table.getValSlot((*I)->getType());
+ const GlobalVariable *GV = *I;
+ int Slot = Table.getValSlot(GV->getType());
assert(Slot != -1 && "Module global vars is broken!");
- output_vbr((unsigned)Slot, Out);
+
+ // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
+ unsigned oSlot = ((unsigned)Slot << 2) | (GV->hasInitializer() << 1) |
+ GV->isConstant();
+ output_vbr(oSlot, Out);
+
+ // If we have an initialized, output it now.
+ if (GV->hasInitializer()) {
+ Slot = Table.getValSlot(GV->getInitializer());
+ assert(Slot != -1 && "No slot for global var initializer!");
+ output_vbr((unsigned)Slot, Out);
+ }
}
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);