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