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