Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 1 | //===- Reader.cpp - Code to read bytecode files ---------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // This library implements the functionality defined in llvm/Bytecode/Reader.h |
| 4 | // |
| 5 | // Note that this library should be as fast as possible, reentrant, and |
| 6 | // threadsafe!! |
| 7 | // |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 8 | // TODO: Return error messages to caller instead of printing them out directly. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // TODO: Allow passing in an option to ignore the symbol table |
| 10 | // |
Chris Lattner | d6b6525 | 2001-10-24 01:15:12 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 13 | #include "ReaderInternals.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/Bytecode/Reader.h" |
| 15 | #include "llvm/Bytecode/Format.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 17 | #include "llvm/iPHINode.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include "llvm/iOther.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "Support/StringExtras.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 21 | #include "Config/unistd.h" |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 22 | #include "Config/sys/mman.h" |
| 23 | #include "Config/sys/stat.h" |
| 24 | #include "Config/sys/types.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 26 | #include <memory> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 28 | static inline void ALIGN32(const unsigned char *&begin, |
| 29 | const unsigned char *end) { |
| 30 | if (align32(begin, end)) |
| 31 | throw std::string("Alignment error in buffer: read past end of block."); |
| 32 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 33 | |
| 34 | void |
| 35 | BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | if (Ty->isPrimitiveType()) { |
| 37 | Slot = Ty->getPrimitiveID(); |
| 38 | } else { |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 39 | // Check the function level types first... |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 40 | TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(), |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 41 | FunctionTypeValues.end(), Ty); |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 42 | if (I != FunctionTypeValues.end()) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 43 | Slot = FirstDerivedTyID + ModuleTypeValues.size() + |
| 44 | (&*I - &FunctionTypeValues[0]); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 45 | } else { |
| 46 | I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 47 | if (I == ModuleTypeValues.end()) |
| 48 | throw std::string("Didn't find type in ModuleTypeValues."); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 49 | Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]); |
| 50 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 52 | //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | const Type *BytecodeParser::getType(unsigned ID) { |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 56 | if (ID < Type::NumPrimitiveIDs) { |
| 57 | const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID); |
| 58 | if (T) return T; |
| 59 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 61 | //cerr << "Looking up Type ID: " << ID << "\n"; |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 62 | const Value *V = getValue(Type::TypeTy, ID, false); |
| 63 | return cast_or_null<Type>(V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 66 | int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) { |
| 67 | assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) || |
| 68 | Val->getType()->isPrimitiveType() || |
| 69 | !cast<Constant>(Val)->isNullValue()) && |
| 70 | "Cannot read null values from bytecode!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | unsigned type; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 72 | getTypeSlot(Val->getType(), type); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 73 | assert(type != Type::TypeTyID && "Types should never be insertValue'd!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 75 | if (ValueTab.size() <= type) { |
| 76 | unsigned OldSize = ValueTab.size(); |
| 77 | ValueTab.resize(type+1); |
| 78 | while (OldSize != type+1) |
| 79 | ValueTab[OldSize++] = new ValueList(); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 81 | |
| 82 | //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 83 | // << "] = " << Val << "\n"; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 84 | ValueTab[type]->push_back(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 86 | bool HasOffset = HasImplicitZeroInitializer && |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 87 | !Val->getType()->isPrimitiveType(); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 88 | |
| 89 | return ValueTab[type]->size()-1 + HasOffset; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot, |
| 94 | Value *Val) { |
| 95 | assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!"); |
| 96 | unsigned type; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 97 | getTypeSlot(Val->getType(), type); |
| 98 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 99 | assert((!HasImplicitZeroInitializer || Slot != 0) && |
| 100 | "Cannot change zero init"); |
| 101 | assert(type < ValueTab.size() && Slot <= ValueTab[type]->size()); |
| 102 | ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { |
| 106 | unsigned Num = oNum; |
| 107 | unsigned type; // The type plane it lives in... |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 108 | getTypeSlot(Ty, type); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 109 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 110 | if (type == Type::TypeTyID) { // The 'type' plane has implicit values |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 111 | assert(Create == false); |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 112 | if (Num < Type::NumPrimitiveIDs) { |
| 113 | const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num); |
| 114 | if (T) return (Value*)T; // Asked for a primitive type... |
| 115 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | |
| 117 | // Otherwise, derived types need offset... |
| 118 | Num -= FirstDerivedTyID; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 119 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 120 | // Is it a module-level type? |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 121 | if (Num < ModuleTypeValues.size()) |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 122 | return (Value*)ModuleTypeValues[Num].get(); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 123 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 124 | // Nope, is it a function-level type? |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 125 | Num -= ModuleTypeValues.size(); |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 126 | if (Num < FunctionTypeValues.size()) |
| 127 | return (Value*)FunctionTypeValues[Num].get(); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 128 | |
| 129 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 132 | if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) { |
| 133 | if (Num == 0) |
| 134 | return Constant::getNullValue(Ty); |
| 135 | --Num; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 138 | if (type < ModuleValues.size()) { |
| 139 | if (Num < ModuleValues[type]->size()) |
| 140 | return ModuleValues[type]->getOperand(Num); |
| 141 | Num -= ModuleValues[type]->size(); |
| 142 | } |
| 143 | |
| 144 | if (Values.size() > type && Values[type]->size() > Num) |
| 145 | return Values[type]->getOperand(Num); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 147 | if (!Create) return 0; // Do not create a placeholder? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | |
| 149 | Value *d = 0; |
| 150 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 151 | case Type::LabelTyID: |
| 152 | d = new BBPHolder(Ty, oNum); |
| 153 | break; |
| 154 | default: |
| 155 | d = new ValPHolder(Ty, oNum); |
| 156 | break; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | assert(d != 0 && "How did we not make something?"); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 160 | if (insertValue(d, LateResolveValues) == -1) return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 161 | return d; |
| 162 | } |
| 163 | |
Chris Lattner | bbd4b30 | 2002-10-14 03:33:02 +0000 | [diff] [blame] | 164 | /// getConstantValue - Just like getValue, except that it returns a null pointer |
| 165 | /// only on error. It always returns a constant (meaning that if the value is |
| 166 | /// defined, but is not a constant, that is an error). If the specified |
| 167 | /// constant hasn't been parsed yet, a placeholder is defined and used. Later, |
| 168 | /// after the real value is parsed, the placeholder is eliminated. |
| 169 | /// |
| 170 | Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) { |
| 171 | if (Value *V = getValue(Ty, Slot, false)) |
| 172 | return dyn_cast<Constant>(V); // If we already have the value parsed... |
| 173 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 174 | std::pair<const Type*, unsigned> Key(Ty, Slot); |
| 175 | GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key); |
| 176 | |
| 177 | if (I != GlobalRefs.end() && I->first == Key) { |
Chris Lattner | bbd4b30 | 2002-10-14 03:33:02 +0000 | [diff] [blame] | 178 | BCR_TRACE(5, "Previous forward ref found!\n"); |
| 179 | return cast<Constant>(I->second); |
| 180 | } else { |
| 181 | // Create a placeholder for the constant reference and |
| 182 | // keep track of the fact that we have a forward ref to recycle it |
| 183 | BCR_TRACE(5, "Creating new forward ref to a constant!\n"); |
| 184 | Constant *C = new ConstPHolder(Ty, Slot); |
| 185 | |
| 186 | // Keep track of the fact that we have a forward ref to recycle it |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 187 | GlobalRefs.insert(I, std::make_pair(Key, C)); |
Chris Lattner | bbd4b30 | 2002-10-14 03:33:02 +0000 | [diff] [blame] | 188 | return C; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 193 | void BytecodeParser::postResolveValues(ValueTable &ValTab) { |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 194 | while (!ValTab.empty()) { |
| 195 | ValueList &DL = *ValTab.back(); |
| 196 | ValTab.pop_back(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 198 | while (!DL.empty()) { |
| 199 | Value *D = DL.back(); |
| 200 | unsigned IDNumber = getValueIDNumberFromPlaceHolder(D); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 201 | DL.pop_back(); |
| 202 | |
| 203 | Value *NewDef = getValue(D->getType(), IDNumber, false); |
| 204 | if (NewDef == 0) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 205 | throw std::string("Unresolvable reference found: <" + |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 206 | D->getType()->getDescription() + ">:" + |
| 207 | utostr(IDNumber) + "."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | } else { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 209 | // Fixup all of the uses of this placeholder def... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | D->replaceAllUsesWith(NewDef); |
| 211 | |
| 212 | // Now that all the uses are gone, delete the placeholder... |
| 213 | // If we couldn't find a def (error case), then leak a little |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 214 | delete D; // memory, 'cause otherwise we can't remove all uses! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 217 | delete &DL; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 221 | std::auto_ptr<BasicBlock> |
| 222 | BytecodeParser::ParseBasicBlock(const unsigned char *&Buf, |
| 223 | const unsigned char *EndBuf) { |
| 224 | std::auto_ptr<BasicBlock> BB(new BasicBlock()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | |
| 226 | while (Buf < EndBuf) { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 227 | Instruction *Inst; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 228 | ParseInstruction(Buf, EndBuf, Inst); |
| 229 | |
| 230 | if (Inst == 0) { throw std::string("Could not parse Instruction."); } |
| 231 | if (insertValue(Inst, Values) == -1) { |
| 232 | throw std::string("Could not insert value."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 235 | BB->getInstList().push_back(Inst); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 236 | BCR_TRACE(4, Inst); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 239 | return BB; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 242 | void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 243 | const unsigned char *EndBuf, |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 244 | SymbolTable *ST) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 245 | while (Buf < EndBuf) { |
| 246 | // Symtab block header: [num entries][type id number] |
| 247 | unsigned NumEntries, Typ; |
| 248 | if (read_vbr(Buf, EndBuf, NumEntries) || |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 249 | read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 250 | const Type *Ty = getType(Typ); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 251 | if (Ty == 0) throw std::string("Invalid type read in symbol table."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 253 | BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries << |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 254 | " entries\n"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 256 | for (unsigned i = 0; i < NumEntries; ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | // Symtab entry: [def slot #][name] |
| 258 | unsigned slot; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 259 | if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 260 | std::string Name; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 261 | if (read(Buf, EndBuf, Name, false)) // Not aligned... |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 262 | throw std::string("Buffer not aligned."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 264 | Value *V = getValue(Ty, slot, false); // Find mapping... |
| 265 | if (V == 0) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 266 | BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n"); |
| 267 | throw std::string("Failed value look-up."); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 269 | BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 270 | if (!isa<Instruction>(V)) std::cerr << "\n"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 272 | V->setName(Name, ST); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 276 | if (Buf > EndBuf) throw std::string("Tried to read past end of buffer."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 279 | void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) { |
Chris Lattner | 0d75d8d7 | 2003-03-06 16:32:25 +0000 | [diff] [blame] | 280 | GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(), |
| 281 | Slot)); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 282 | if (I == GlobalRefs.end()) return; // Never forward referenced? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 284 | BCR_TRACE(3, "Mutating forward refs!\n"); |
| 285 | Value *VPH = I->second; // Get the placeholder... |
Vikram S. Adve | c1e4a81 | 2002-07-14 23:04:18 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 287 | VPH->replaceAllUsesWith(NewV); |
| 288 | |
| 289 | // If this is a global variable being resolved, remove the placeholder from |
| 290 | // the module... |
| 291 | if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV)) |
| 292 | GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH)); |
Vikram S. Adve | c1e4a81 | 2002-07-14 23:04:18 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 294 | delete VPH; // Delete the old placeholder |
| 295 | GlobalRefs.erase(I); // Remove the map entry for it |
Vikram S. Adve | c1e4a81 | 2002-07-14 23:04:18 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 298 | void |
| 299 | BytecodeParser::ParseFunction(const unsigned char *&Buf, |
| 300 | const unsigned char *EndBuf) { |
| 301 | if (FunctionSignatureList.empty()) |
| 302 | throw std::string("FunctionSignatureList empty!"); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 303 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 304 | Function *F = FunctionSignatureList.back().first; |
| 305 | unsigned FunctionSlot = FunctionSignatureList.back().second; |
| 306 | FunctionSignatureList.pop_back(); |
| 307 | |
| 308 | // Save the information for future reading of the function |
| 309 | LazyFunctionInfo *LFI = new LazyFunctionInfo(); |
| 310 | LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot; |
| 311 | LazyFunctionLoadMap[F] = LFI; |
| 312 | // Pretend we've `parsed' this function |
| 313 | Buf = EndBuf; |
| 314 | } |
| 315 | |
| 316 | void BytecodeParser::materializeFunction(Function* F) { |
| 317 | // Find {start, end} pointers and slot in the map. If not there, we're done. |
| 318 | std::map<Function*, LazyFunctionInfo*>::iterator Fi = |
| 319 | LazyFunctionLoadMap.find(F); |
| 320 | if (Fi == LazyFunctionLoadMap.end()) return; |
| 321 | |
| 322 | LazyFunctionInfo *LFI = Fi->second; |
| 323 | const unsigned char *Buf = LFI->Buf; |
| 324 | const unsigned char *EndBuf = LFI->EndBuf; |
| 325 | unsigned FunctionSlot = LFI->FunctionSlot; |
| 326 | LazyFunctionLoadMap.erase(Fi); |
| 327 | delete LFI; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 328 | |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 329 | GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage; |
| 330 | |
| 331 | if (!hasInternalMarkerOnly) { |
| 332 | unsigned LinkageType; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 333 | if (read_vbr(Buf, EndBuf, LinkageType)) |
| 334 | throw std::string("ParseFunction: Error reading from buffer."); |
| 335 | if (LinkageType & ~0x3) |
| 336 | throw std::string("Invalid linkage type for Function."); |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 337 | Linkage = (GlobalValue::LinkageTypes)LinkageType; |
| 338 | } else { |
| 339 | // We used to only support two linkage models: internal and external |
| 340 | unsigned isInternal; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 341 | if (read_vbr(Buf, EndBuf, isInternal)) |
| 342 | throw std::string("ParseFunction: Error reading from buffer."); |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 343 | if (isInternal) Linkage = GlobalValue::InternalLinkage; |
| 344 | } |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 345 | |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 346 | F->setLinkage(Linkage); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 348 | const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes(); |
| 349 | Function::aiterator AI = F->abegin(); |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 350 | for (FunctionType::ParamTypes::const_iterator It = Params.begin(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 351 | It != Params.end(); ++It, ++AI) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 352 | if (insertValue(AI, Values) == -1) |
| 353 | throw std::string("Error reading function arguments!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | while (Buf < EndBuf) { |
| 357 | unsigned Type, Size; |
Chris Lattner | b6c4695 | 2003-03-06 17:03:28 +0000 | [diff] [blame] | 358 | const unsigned char *OldBuf = Buf; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 359 | readBlock(Buf, EndBuf, Type, Size); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | |
| 361 | switch (Type) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 362 | case BytecodeFormat::ConstantPool: { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 363 | BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 364 | ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 365 | break; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 367 | |
| 368 | case BytecodeFormat::BasicBlock: { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 369 | BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 370 | std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size); |
| 371 | if (!BB.get() || insertValue(BB.get(), Values) == -1) |
| 372 | throw std::string("Parse error: BasicBlock"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 373 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 374 | F->getBasicBlockList().push_back(BB.release()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 378 | case BytecodeFormat::SymbolTable: { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 379 | BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 380 | ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 381 | break; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 382 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 383 | |
| 384 | default: |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 385 | BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 386 | Buf += Size; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 387 | if (OldBuf > Buf) |
| 388 | throw std::string("Wrapped around reading bytecode."); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 389 | break; |
| 390 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 391 | BCR_TRACE(2, "} end block\n"); |
| 392 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 393 | // Malformed bc file if read past end of block. |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 394 | ALIGN32(Buf, EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 397 | // Check for unresolvable references |
| 398 | postResolveValues(LateResolveValues); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 399 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 400 | //ResolveReferencesToValue(F, FunctionSlot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 401 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 402 | // Clear out function-level types... |
Chris Lattner | 6e5a0e4 | 2003-03-06 17:18:14 +0000 | [diff] [blame] | 403 | FunctionTypeValues.clear(); |
Chris Lattner | e4d71a1 | 2001-09-14 22:03:42 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 405 | freeTable(Values); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 408 | void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf, |
| 409 | const unsigned char *End) { |
| 410 | if (!FunctionSignatureList.empty()) |
| 411 | throw std::string("Two ModuleGlobalInfo packets found!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 413 | // Read global variables... |
| 414 | unsigned VarType; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 415 | if (read_vbr(Buf, End, VarType)) throw Error_readvbr; |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 416 | while (VarType != Type::VoidTyID) { // List is terminated by Void |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 417 | unsigned SlotNo; |
| 418 | GlobalValue::LinkageTypes Linkage; |
| 419 | |
| 420 | if (!hasInternalMarkerOnly) { |
| 421 | // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, |
| 422 | // bit2,3 = Linkage, bit4+ = slot# |
| 423 | SlotNo = VarType >> 4; |
| 424 | Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3); |
| 425 | } else { |
| 426 | // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, |
| 427 | // bit2 = isInternal, bit3+ = slot# |
| 428 | SlotNo = VarType >> 3; |
| 429 | Linkage = (VarType & 4) ? GlobalValue::InternalLinkage : |
| 430 | GlobalValue::ExternalLinkage; |
| 431 | } |
| 432 | |
| 433 | const Type *Ty = getType(SlotNo); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 434 | if (!Ty || !isa<PointerType>(Ty)) |
| 435 | throw std::string("Global not pointer type! Ty = " + |
| 436 | Ty->getDescription()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 438 | const Type *ElTy = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 440 | // Create the global variable... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 441 | GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage, |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 442 | 0, "", TheModule); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 443 | int DestSlot = insertValue(GV, ModuleValues); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 444 | if (DestSlot == -1) throw Error_DestSlot; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 445 | BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n"); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 446 | ResolveReferencesToValue(GV, (unsigned)DestSlot); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 447 | |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 448 | if (VarType & 2) { // Does it have an initializer? |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 449 | unsigned InitSlot; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 450 | if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 451 | GlobalInits.push_back(std::make_pair(GV, InitSlot)); |
| 452 | } |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 453 | if (read_vbr(Buf, End, VarType)) throw Error_readvbr; |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 456 | // Read the function objects for all of the functions that are coming |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 457 | unsigned FnSignature; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 458 | if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr; |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 459 | while (FnSignature != Type::VoidTyID) { // List is terminated by Void |
| 460 | const Type *Ty = getType(FnSignature); |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 461 | if (!Ty || !isa<PointerType>(Ty) || |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 462 | !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 463 | throw std::string("Function not ptr to func type! Ty = " + |
| 464 | Ty->getDescription()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 465 | } |
Chris Lattner | 8cdc6b7 | 2002-10-23 00:51:54 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 467 | // We create functions by passing the underlying FunctionType to create... |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 468 | Ty = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 470 | // When the ModuleGlobalInfo section is read, we load the type of each |
| 471 | // function and the 'ModuleValues' slot that it lands in. We then load a |
| 472 | // placeholder into its slot to reserve it. When the function is loaded, |
| 473 | // this placeholder is replaced. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 474 | |
| 475 | // Insert the placeholder... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 476 | Function *Func = new Function(cast<FunctionType>(Ty), |
| 477 | GlobalValue::InternalLinkage, "", TheModule); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 478 | int DestSlot = insertValue(Func, ModuleValues); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 479 | if (DestSlot == -1) throw Error_DestSlot; |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 480 | ResolveReferencesToValue(Func, (unsigned)DestSlot); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 482 | // Keep track of this information in a list that is emptied as functions are |
| 483 | // loaded... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 484 | // |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 485 | FunctionSignatureList.push_back(std::make_pair(Func, DestSlot)); |
| 486 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 487 | if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr; |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 488 | BCR_TRACE(2, "Function of type: " << Ty << "\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 491 | ALIGN32(Buf, End); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 492 | |
| 493 | // Now that the function signature list is set up, reverse it so that we can |
| 494 | // remove elements efficiently from the back of the vector. |
| 495 | std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 496 | |
| 497 | // This is for future proofing... in the future extra fields may be added that |
| 498 | // we don't understand, so we transparently ignore them. |
| 499 | // |
| 500 | Buf = End; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 503 | void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 504 | const unsigned char *EndBuf) { |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 505 | unsigned Version; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 506 | if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr; |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 507 | |
| 508 | // Unpack version number: low four bits are for flags, top bits = version |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 509 | Module::Endianness Endianness; |
| 510 | Module::PointerSize PointerSize; |
| 511 | Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian; |
| 512 | PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32; |
| 513 | |
| 514 | bool hasNoEndianness = Version & 4; |
| 515 | bool hasNoPointerSize = Version & 8; |
| 516 | |
| 517 | RevisionNum = Version >> 4; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 518 | |
| 519 | // Default values for the current bytecode version |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 520 | HasImplicitZeroInitializer = true; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 521 | hasInternalMarkerOnly = false; |
| 522 | FirstDerivedTyID = 14; |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 523 | |
| 524 | switch (RevisionNum) { |
| 525 | case 0: // Initial revision |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 526 | // Version #0 didn't have any of the flags stored correctly, and in fact as |
| 527 | // only valid with a 14 in the flags values. Also, it does not support |
| 528 | // encoding zero initializers for arrays compactly. |
| 529 | // |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 530 | if (Version != 14) throw std::string("Unknown revision 0 flags?"); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 531 | HasImplicitZeroInitializer = false; |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 532 | Endianness = Module::BigEndian; |
| 533 | PointerSize = Module::Pointer64; |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 534 | hasInternalMarkerOnly = true; |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 535 | hasNoEndianness = hasNoPointerSize = false; |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 536 | break; |
| 537 | case 1: |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 538 | // Version #1 has four bit fields: isBigEndian, hasLongPointers, |
| 539 | // hasNoEndianness, and hasNoPointerSize. |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 540 | hasInternalMarkerOnly = true; |
| 541 | break; |
| 542 | case 2: |
| 543 | // Version #2 added information about all 4 linkage types instead of just |
| 544 | // having internal and external. |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 545 | break; |
| 546 | default: |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 547 | throw std::string("Unknown bytecode version number!"); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 550 | if (hasNoEndianness) Endianness = Module::AnyEndianness; |
| 551 | if (hasNoPointerSize) PointerSize = Module::AnyPointerSize; |
Chris Lattner | 76e3896 | 2003-04-22 18:15:10 +0000 | [diff] [blame] | 552 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 553 | TheModule->setEndianness(Endianness); |
| 554 | TheModule->setPointerSize(PointerSize); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 555 | BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n"); |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 556 | BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << "," |
| 557 | << PointerSize << "\n"); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 558 | BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n"); |
Chris Lattner | 036b8aa | 2003-03-06 17:55:45 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 561 | void BytecodeParser::ParseModule(const unsigned char *Buf, |
Chris Lattner | 12e6465 | 2003-05-22 18:08:30 +0000 | [diff] [blame] | 562 | const unsigned char *EndBuf) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 563 | unsigned Type, Size; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 564 | readBlock(Buf, EndBuf, Type, Size); |
| 565 | if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) |
| 566 | throw std::string("Expected Module packet! B: "+ |
| 567 | utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+ |
| 568 | " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 570 | BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n"); |
Chris Lattner | 7473413 | 2002-08-17 22:01:27 +0000 | [diff] [blame] | 571 | FunctionSignatureList.clear(); // Just in case... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 572 | |
| 573 | // Read into instance variables... |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 574 | ParseVersionInfo(Buf, EndBuf); |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 575 | ALIGN32(Buf, EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 577 | while (Buf < EndBuf) { |
Chris Lattner | b6c4695 | 2003-03-06 17:03:28 +0000 | [diff] [blame] | 578 | const unsigned char *OldBuf = Buf; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 579 | readBlock(Buf, EndBuf, Type, Size); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 580 | switch (Type) { |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 581 | case BytecodeFormat::GlobalTypePlane: |
| 582 | BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 583 | ParseGlobalTypes(Buf, Buf+Size); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 584 | break; |
| 585 | |
| 586 | case BytecodeFormat::ModuleGlobalInfo: |
| 587 | BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 588 | ParseModuleGlobalInfo(Buf, Buf+Size); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 589 | break; |
| 590 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 591 | case BytecodeFormat::ConstantPool: |
| 592 | BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 593 | ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 594 | break; |
| 595 | |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 596 | case BytecodeFormat::Function: { |
| 597 | BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 598 | ParseFunction(Buf, Buf+Size); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | |
| 602 | case BytecodeFormat::SymbolTable: |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 603 | BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n"); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 604 | ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 605 | break; |
| 606 | |
| 607 | default: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 608 | Buf += Size; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 609 | if (OldBuf > Buf) throw std::string("Expected Module Block!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 610 | break; |
| 611 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 612 | BCR_TRACE(1, "} end block\n"); |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 613 | ALIGN32(Buf, EndBuf); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 616 | // After the module constant pool has been read, we can safely initialize |
| 617 | // global variables... |
| 618 | while (!GlobalInits.empty()) { |
| 619 | GlobalVariable *GV = GlobalInits.back().first; |
| 620 | unsigned Slot = GlobalInits.back().second; |
| 621 | GlobalInits.pop_back(); |
| 622 | |
| 623 | // Look up the initializer value... |
| 624 | if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) { |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 625 | if (GV->hasInitializer()) |
| 626 | throw std::string("Global *already* has an initializer?!"); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 627 | GV->setInitializer(cast<Constant>(V)); |
| 628 | } else |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 629 | throw std::string("Cannot find initializer value."); |
Chris Lattner | 52e20b0 | 2003-03-19 20:54:26 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 632 | if (!FunctionSignatureList.empty()) |
| 633 | throw std::string("Function expected, but bytecode stream ended!"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 634 | |
| 635 | BCR_TRACE(0, "} end block\n\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 638 | void |
| 639 | BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length, |
| 640 | const std::string &ModuleID) { |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 641 | |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 642 | unsigned char *EndBuf = (unsigned char*)(Buf + Length); |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 644 | // Read and check signature... |
Misha Brukman | e0dd0d4 | 2003-09-23 16:15:29 +0000 | [diff] [blame] | 645 | unsigned Sig; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 646 | if (read(Buf, EndBuf, Sig) || |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 647 | Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) |
| 648 | throw std::string("Invalid bytecode signature!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 75f2053 | 2003-04-22 18:02:52 +0000 | [diff] [blame] | 650 | TheModule = new Module(ModuleID); |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 651 | try { |
| 652 | ParseModule(Buf, EndBuf); |
| 653 | } catch (std::string &Error) { |
Chris Lattner | a2602f3 | 2003-05-22 18:26:48 +0000 | [diff] [blame] | 654 | freeState(); // Must destroy handles before deleting module! |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 655 | delete TheModule; |
| 656 | TheModule = 0; |
Misha Brukman | 12c29d1 | 2003-09-22 23:38:23 +0000 | [diff] [blame] | 657 | throw Error; |
Chris Lattner | 2a7b6ba | 2003-03-06 17:15:19 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 659 | } |