OLD | NEW |
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the PNaCl bitcode file to Ice, to machine code | 10 // This file implements the PNaCl bitcode file to Ice, to machine code |
11 // translator. | 11 // translator. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #include "PNaClTranslator.h" | 15 #include "PNaClTranslator.h" |
16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
| 17 #include "IceCfgNode.h" |
| 18 #include "IceClFlags.h" |
| 19 #include "IceDefs.h" |
| 20 #include "IceInst.h" |
| 21 #include "IceOperand.h" |
| 22 #include "IceTypeConverter.h" |
17 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" | 23 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
18 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 24 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
19 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 25 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
20 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 26 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
21 #include "llvm/IR/Constants.h" | 27 #include "llvm/IR/Constants.h" |
22 #include "llvm/IR/LLVMContext.h" | 28 #include "llvm/IR/LLVMContext.h" |
23 #include "llvm/IR/Module.h" | 29 #include "llvm/IR/Module.h" |
24 #include "llvm/Support/Format.h" | 30 #include "llvm/Support/Format.h" |
25 #include "llvm/Support/MemoryBuffer.h" | 31 #include "llvm/Support/MemoryBuffer.h" |
26 #include "llvm/Support/raw_ostream.h" | 32 #include "llvm/Support/raw_ostream.h" |
27 #include "llvm/Support/ValueHandle.h" | 33 #include "llvm/Support/ValueHandle.h" |
28 | 34 |
29 #include <vector> | 35 #include <vector> |
30 #include <cassert> | 36 #include <cassert> |
31 | 37 |
32 using namespace llvm; | 38 using namespace llvm; |
33 | 39 |
34 namespace { | 40 namespace { |
35 | 41 |
| 42 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 43 static cl::opt<bool> |
| 44 AllowErrorRecovery("allow-pnacl-reader-error-recovery", |
| 45 cl::desc("Allow error recovery when reading PNaCl bitcode."), |
| 46 cl::init(false)); |
| 47 |
36 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 48 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
37 class TopLevelParser : public NaClBitcodeParser { | 49 class TopLevelParser : public NaClBitcodeParser { |
38 TopLevelParser(const TopLevelParser &) LLVM_DELETED_FUNCTION; | 50 TopLevelParser(const TopLevelParser &) LLVM_DELETED_FUNCTION; |
39 TopLevelParser &operator=(const TopLevelParser &) LLVM_DELETED_FUNCTION; | 51 TopLevelParser &operator=(const TopLevelParser &) LLVM_DELETED_FUNCTION; |
40 | 52 |
41 public: | 53 public: |
42 TopLevelParser(const std::string &InputName, NaClBitcodeHeader &Header, | 54 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, |
43 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 55 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, |
44 : NaClBitcodeParser(Cursor), | 56 bool &ErrorStatus) |
| 57 : NaClBitcodeParser(Cursor), Translator(Translator), |
45 Mod(new Module(InputName, getGlobalContext())), Header(Header), | 58 Mod(new Module(InputName, getGlobalContext())), Header(Header), |
46 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 59 TypeConverter(getLLVMContext()), ErrorStatus(ErrorStatus), NumErrors(0), |
47 GlobalVarPlaceHolderType(Type::getInt8Ty(getLLVMContext())) { | 60 NumFunctionIds(0), NumFunctionBlocks(0), |
| 61 GlobalVarPlaceHolderType(convertToLLVMType(Ice::IceType_i8)) { |
48 Mod->setDataLayout(PNaClDataLayout); | 62 Mod->setDataLayout(PNaClDataLayout); |
49 } | 63 } |
50 | 64 |
51 virtual ~TopLevelParser() {} | 65 virtual ~TopLevelParser() {} |
52 LLVM_OVERRIDE; | 66 LLVM_OVERRIDE; |
53 | 67 |
| 68 Ice::Translator &getTranslator() { return Translator; } |
| 69 |
| 70 // Generates error with given Message. Always returns true. |
54 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 71 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
55 ErrorStatus = true; | 72 ErrorStatus = true; |
56 ++NumErrors; | 73 ++NumErrors; |
57 return NaClBitcodeParser::Error(Message); | 74 NaClBitcodeParser::Error(Message); |
| 75 if (!AllowErrorRecovery) |
| 76 report_fatal_error("Unable to continue"); |
| 77 return true; |
58 } | 78 } |
59 | 79 |
60 /// Returns the number of errors found while parsing the bitcode | 80 /// Returns the number of errors found while parsing the bitcode |
61 /// file. | 81 /// file. |
62 unsigned getNumErrors() const { return NumErrors; } | 82 unsigned getNumErrors() const { return NumErrors; } |
63 | 83 |
64 /// Returns the LLVM module associated with the translation. | 84 /// Returns the LLVM module associated with the translation. |
65 Module *getModule() const { return Mod.get(); } | 85 Module *getModule() const { return Mod.get(); } |
66 | 86 |
67 /// Returns the number of bytes in the bitcode header. | 87 /// Returns the number of bytes in the bitcode header. |
(...skipping 29 matching lines...) Expand all Loading... |
97 ++NumFunctionIds; | 117 ++NumFunctionIds; |
98 ValueIDValues.push_back(Fcn); | 118 ValueIDValues.push_back(Fcn); |
99 } | 119 } |
100 | 120 |
101 /// Defines the next function ID as one that has an implementation | 121 /// Defines the next function ID as one that has an implementation |
102 /// (i.e a corresponding function block in the bitcode). | 122 /// (i.e a corresponding function block in the bitcode). |
103 void setNextValueIDAsImplementedFunction() { | 123 void setNextValueIDAsImplementedFunction() { |
104 DefiningFunctionsList.push_back(ValueIDValues.size()); | 124 DefiningFunctionsList.push_back(ValueIDValues.size()); |
105 } | 125 } |
106 | 126 |
| 127 /// Returns the value id that should be associated with the the |
| 128 /// current function block. Increments internal counters during call |
| 129 /// so that it will be in correct position for next function block. |
| 130 unsigned getNextFunctionBlockValueID() { |
| 131 if (NumFunctionBlocks >= DefiningFunctionsList.size()) |
| 132 report_fatal_error( |
| 133 "More function blocks than defined function addresses"); |
| 134 return DefiningFunctionsList[NumFunctionBlocks++]; |
| 135 } |
| 136 |
107 /// Returns the LLVM IR value associatd with the global value ID. | 137 /// Returns the LLVM IR value associatd with the global value ID. |
108 Value *getGlobalValueByID(unsigned ID) const { | 138 Value *getGlobalValueByID(unsigned ID) const { |
109 if (ID >= ValueIDValues.size()) | 139 if (ID >= ValueIDValues.size()) |
110 return 0; | 140 return NULL; |
111 return ValueIDValues[ID]; | 141 return ValueIDValues[ID]; |
112 } | 142 } |
113 | 143 |
114 /// Returns the number of function addresses (i.e. ID's) defined in | 144 /// Returns the number of function addresses (i.e. ID's) defined in |
115 /// the bitcode file. | 145 /// the bitcode file. |
116 unsigned getNumFunctionIDs() const { return NumFunctionIds; } | 146 unsigned getNumFunctionIDs() const { return NumFunctionIds; } |
117 | 147 |
118 /// Returns the number of global values defined in the bitcode | 148 /// Returns the number of global values defined in the bitcode |
119 /// file. | 149 /// file. |
120 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); } | 150 unsigned getNumGlobalValueIDs() const { return ValueIDValues.size(); } |
121 | 151 |
122 /// Resizes the list of value IDs to include Count global variable | 152 /// Resizes the list of value IDs to include Count global variable |
123 /// IDs. | 153 /// IDs. |
124 void resizeValueIDsForGlobalVarCount(unsigned Count) { | 154 void resizeValueIDsForGlobalVarCount(unsigned Count) { |
125 ValueIDValues.resize(ValueIDValues.size() + Count); | 155 ValueIDValues.resize(ValueIDValues.size() + Count); |
126 } | 156 } |
127 | 157 |
128 /// Returns the global variable address associated with the given | 158 /// Returns the global variable address associated with the given |
129 /// value ID. If the ID refers to a global variable address not yet | 159 /// value ID. If the ID refers to a global variable address not yet |
130 /// defined, a placeholder is created so that we can fix it up | 160 /// defined, a placeholder is created so that we can fix it up |
131 /// later. | 161 /// later. |
132 Constant *getOrCreateGlobalVarRef(unsigned ID) { | 162 Constant *getOrCreateGlobalVarRef(unsigned ID) { |
133 if (ID >= ValueIDValues.size()) | 163 if (ID >= ValueIDValues.size()) |
134 return 0; | 164 return NULL; |
135 if (Value *C = ValueIDValues[ID]) | 165 if (Value *C = ValueIDValues[ID]) |
136 return dyn_cast<Constant>(C); | 166 return dyn_cast<Constant>(C); |
137 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false, | 167 Constant *C = new GlobalVariable(*Mod, GlobalVarPlaceHolderType, false, |
138 GlobalValue::ExternalLinkage, 0); | 168 GlobalValue::ExternalLinkage, 0); |
139 ValueIDValues[ID] = C; | 169 ValueIDValues[ID] = C; |
140 return C; | 170 return C; |
141 } | 171 } |
142 | 172 |
143 /// Assigns the given global variable (address) to the given value | 173 /// Assigns the given global variable (address) to the given value |
144 /// ID. Returns true if ID is a valid global variable ID. Otherwise | 174 /// ID. Returns true if ID is a valid global variable ID. Otherwise |
145 /// returns false. | 175 /// returns false. |
146 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) { | 176 bool assignGlobalVariable(GlobalVariable *GV, unsigned ID) { |
147 if (ID < NumFunctionIds || ID >= ValueIDValues.size()) | 177 if (ID < NumFunctionIds || ID >= ValueIDValues.size()) |
148 return false; | 178 return false; |
149 WeakVH &OldV = ValueIDValues[ID]; | 179 WeakVH &OldV = ValueIDValues[ID]; |
150 if (OldV == 0) { | 180 if (OldV == NULL) { |
151 ValueIDValues[ID] = GV; | 181 ValueIDValues[ID] = GV; |
152 return true; | 182 return true; |
153 } | 183 } |
154 | 184 |
155 // If reached, there was a forward reference to this value. Replace it. | 185 // If reached, there was a forward reference to this value. Replace it. |
156 Value *PrevVal = OldV; | 186 Value *PrevVal = OldV; |
157 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); | 187 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); |
158 Placeholder->replaceAllUsesWith( | 188 Placeholder->replaceAllUsesWith( |
159 ConstantExpr::getBitCast(GV, Placeholder->getType())); | 189 ConstantExpr::getBitCast(GV, Placeholder->getType())); |
160 Placeholder->eraseFromParent(); | 190 Placeholder->eraseFromParent(); |
161 ValueIDValues[ID] = GV; | 191 ValueIDValues[ID] = GV; |
162 return true; | 192 return true; |
163 } | 193 } |
164 | 194 |
| 195 /// Returns the corresponding ICE type for LLVMTy. |
| 196 Ice::Type convertToIceType(Type *LLVMTy) { |
| 197 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); |
| 198 if (IceTy >= Ice::IceType_NUM) { |
| 199 return convertToIceTypeError(LLVMTy); |
| 200 } |
| 201 return IceTy; |
| 202 } |
| 203 |
| 204 /// Returns the corresponding LLVM type for IceTy. |
| 205 Type *convertToLLVMType(Ice::Type IceTy) const { |
| 206 return TypeConverter.convertToLLVMType(IceTy); |
| 207 } |
| 208 |
| 209 /// Returns the LLVM integer type with the given number of Bits. If |
| 210 /// Bits is not a valid PNaCl type, returns NULL. |
| 211 Type *getLLVMIntegerType(unsigned Bits) const { |
| 212 return TypeConverter.getLLVMIntegerType(Bits); |
| 213 } |
| 214 |
| 215 /// Returns the LLVM vector with the given Size and Ty. If not a |
| 216 /// valid PNaCl vector type, returns NULL. |
| 217 Type *getLLVMVectorType(unsigned Size, Ice::Type Ty) const { |
| 218 return TypeConverter.getLLVMVectorType(Size, Ty); |
| 219 } |
| 220 |
| 221 /// Returns the model for pointer types in ICE. |
| 222 Ice::Type getIcePointerType() const { |
| 223 return TypeConverter.getIcePointerType(); |
| 224 } |
| 225 |
165 private: | 226 private: |
| 227 // The translator associated with the parser. |
| 228 Ice::Translator &Translator; |
166 // The parsed module. | 229 // The parsed module. |
167 OwningPtr<Module> Mod; | 230 OwningPtr<Module> Mod; |
168 // The bitcode header. | 231 // The bitcode header. |
169 NaClBitcodeHeader &Header; | 232 NaClBitcodeHeader &Header; |
| 233 // Converter between LLVM and ICE types. |
| 234 Ice::TypeConverter TypeConverter; |
170 // The exit status that should be set to true if an error occurs. | 235 // The exit status that should be set to true if an error occurs. |
171 bool &ErrorStatus; | 236 bool &ErrorStatus; |
172 // The number of errors reported. | 237 // The number of errors reported. |
173 unsigned NumErrors; | 238 unsigned NumErrors; |
174 // The types associated with each type ID. | 239 // The types associated with each type ID. |
175 std::vector<Type *> TypeIDValues; | 240 std::vector<Type *> TypeIDValues; |
176 // The (global) value IDs. | 241 // The (global) value IDs. |
177 std::vector<WeakVH> ValueIDValues; | 242 std::vector<WeakVH> ValueIDValues; |
178 // The number of function IDs. | 243 // The number of function IDs. |
179 unsigned NumFunctionIds; | 244 unsigned NumFunctionIds; |
| 245 // The number of function blocks (processed so far). |
| 246 unsigned NumFunctionBlocks; |
180 // The list of value IDs (in the order found) of defining function | 247 // The list of value IDs (in the order found) of defining function |
181 // addresses. | 248 // addresses. |
182 std::vector<unsigned> DefiningFunctionsList; | 249 std::vector<unsigned> DefiningFunctionsList; |
183 // Cached global variable placeholder type. Used for all forward | 250 // Cached global variable placeholder type. Used for all forward |
184 // references to global variable addresses. | 251 // references to global variable addresses. |
185 Type *GlobalVarPlaceHolderType; | 252 Type *GlobalVarPlaceHolderType; |
186 | 253 |
187 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 254 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
188 | 255 |
189 /// Reports that type ID is undefined, and then returns | 256 /// Reports that type ID is undefined, and then returns |
190 /// the void type. | 257 /// the void type. |
191 Type *reportTypeIDAsUndefined(unsigned ID); | 258 Type *reportTypeIDAsUndefined(unsigned ID); |
192 | 259 |
193 /// Reports error about bad call to setTypeID. | 260 /// Reports error about bad call to setTypeID. |
194 void reportBadSetTypeID(unsigned ID, Type *Ty); | 261 void reportBadSetTypeID(unsigned ID, Type *Ty); |
| 262 |
| 263 // Reports that there is no corresponding ICE type for LLVMTy, and |
| 264 // returns ICE::IceType_void. |
| 265 Ice::Type convertToIceTypeError(Type *LLVMTy); |
195 }; | 266 }; |
196 | 267 |
197 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { | 268 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { |
198 std::string Buffer; | 269 std::string Buffer; |
199 raw_string_ostream StrBuf(Buffer); | 270 raw_string_ostream StrBuf(Buffer); |
200 StrBuf << "Can't find type for type id: " << ID; | 271 StrBuf << "Can't find type for type id: " << ID; |
201 Error(StrBuf.str()); | 272 Error(StrBuf.str()); |
202 Type *Ty = Type::getVoidTy(getLLVMContext()); | 273 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 274 Type *Ty = TypeConverter.convertToLLVMType(Ice::IceType_void); |
203 // To reduce error messages, update type list if possible. | 275 // To reduce error messages, update type list if possible. |
204 if (ID < TypeIDValues.size()) | 276 if (ID < TypeIDValues.size()) |
205 TypeIDValues[ID] = Ty; | 277 TypeIDValues[ID] = Ty; |
206 return Ty; | 278 return Ty; |
207 } | 279 } |
208 | 280 |
209 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { | 281 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { |
210 std::string Buffer; | 282 std::string Buffer; |
211 raw_string_ostream StrBuf(Buffer); | 283 raw_string_ostream StrBuf(Buffer); |
212 if (ID >= TypeIDValues.size()) { | 284 if (ID >= TypeIDValues.size()) { |
213 StrBuf << "Type index " << ID << " out of range: can't install."; | 285 StrBuf << "Type index " << ID << " out of range: can't install."; |
214 } else { | 286 } else { |
215 // Must be case that index already defined. | 287 // Must be case that index already defined. |
216 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] | 288 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] |
217 << " and " << *Ty << "."; | 289 << " and " << *Ty << "."; |
218 } | 290 } |
219 Error(StrBuf.str()); | 291 Error(StrBuf.str()); |
220 } | 292 } |
221 | 293 |
| 294 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { |
| 295 std::string Buffer; |
| 296 raw_string_ostream StrBuf(Buffer); |
| 297 StrBuf << "Invalid LLVM type: " << *LLVMTy; |
| 298 Error(StrBuf.str()); |
| 299 return Ice::IceType_void; |
| 300 } |
| 301 |
222 // Base class for parsing blocks within the bitcode file. Note: | 302 // Base class for parsing blocks within the bitcode file. Note: |
223 // Because this is the base class of block parsers, we generate error | 303 // Because this is the base class of block parsers, we generate error |
224 // messages if ParseBlock or ParseRecord is not overridden in derived | 304 // messages if ParseBlock or ParseRecord is not overridden in derived |
225 // classes. | 305 // classes. |
226 class BlockParserBaseClass : public NaClBitcodeParser { | 306 class BlockParserBaseClass : public NaClBitcodeParser { |
227 public: | 307 public: |
228 // Constructor for the top-level module block parser. | 308 // Constructor for the top-level module block parser. |
229 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) | 309 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) |
230 : NaClBitcodeParser(BlockID, Context), Context(Context) {} | 310 : NaClBitcodeParser(BlockID, Context), Context(Context) {} |
231 | 311 |
232 virtual ~BlockParserBaseClass() LLVM_OVERRIDE {} | 312 virtual ~BlockParserBaseClass() LLVM_OVERRIDE {} |
233 | 313 |
234 protected: | 314 protected: |
235 // The context parser that contains the decoded state. | 315 // The context parser that contains the decoded state. |
236 TopLevelParser *Context; | 316 TopLevelParser *Context; |
237 | 317 |
238 // Constructor for nested block parsers. | 318 // Constructor for nested block parsers. |
239 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 319 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
240 : NaClBitcodeParser(BlockID, EnclosingParser), | 320 : NaClBitcodeParser(BlockID, EnclosingParser), |
241 Context(EnclosingParser->Context) {} | 321 Context(EnclosingParser->Context) {} |
242 | 322 |
| 323 // Gets the translator associated with the bitcode parser. |
| 324 Ice::Translator &getTranslator() { return Context->getTranslator(); } |
| 325 |
243 // Generates an error Message with the bit address prefixed to it. | 326 // Generates an error Message with the bit address prefixed to it. |
244 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 327 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
245 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; | 328 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; |
246 std::string Buffer; | 329 std::string Buffer; |
247 raw_string_ostream StrBuf(Buffer); | 330 raw_string_ostream StrBuf(Buffer); |
248 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), | 331 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), |
249 static_cast<unsigned>(Bit % 8)) << ") " << Message; | 332 static_cast<unsigned>(Bit % 8)) << ") " << Message; |
250 return Context->Error(StrBuf.str()); | 333 return Context->Error(StrBuf.str()); |
251 } | 334 } |
252 | 335 |
253 // Default implementation. Reports that block is unknown and skips | 336 // Default implementation. Reports that block is unknown and skips |
254 // its contents. | 337 // its contents. |
255 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 338 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
256 | 339 |
257 // Default implementation. Reports that the record is not | 340 // Default implementation. Reports that the record is not |
258 // understood. | 341 // understood. |
259 virtual void ProcessRecord() LLVM_OVERRIDE; | 342 virtual void ProcessRecord() LLVM_OVERRIDE; |
260 | 343 |
261 /// Checks if the size of the record is Size. If not, an error is | 344 // Checks if the size of the record is Size. Return true if valid. |
262 /// produced using the given RecordName. Return true if error was | 345 // Otherwise generates an error and returns false. |
263 /// reported. Otherwise false. | 346 bool isValidRecordSize(unsigned Size, const char *RecordName) { |
264 bool checkRecordSize(unsigned Size, const char *RecordName) { | |
265 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 347 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
266 if (Values.size() != Size) { | 348 if (Values.size() == Size) |
267 return RecordSizeError(Size, RecordName, 0); | 349 return true; |
268 } | 350 ReportRecordSizeError(Size, RecordName, NULL); |
269 return false; | 351 return false; |
270 } | 352 } |
271 | 353 |
272 /// Checks if the size of the record is at least as large as the | 354 // Checks if the size of the record is at least as large as the |
273 /// LowerLimit. | 355 // LowerLimit. Returns true if valid. Otherwise generates an error |
274 bool checkRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) { | 356 // and returns false. |
| 357 bool isValidRecordSizeAtLeast(unsigned LowerLimit, const char *RecordName) { |
275 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 358 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
276 if (Values.size() < LowerLimit) { | 359 if (Values.size() >= LowerLimit) |
277 return RecordSizeError(LowerLimit, RecordName, "at least"); | 360 return true; |
278 } | 361 ReportRecordSizeError(LowerLimit, RecordName, "at least"); |
279 return false; | 362 return false; |
280 } | 363 } |
281 | 364 |
282 /// Checks if the size of the record is no larger than the | 365 // Checks if the size of the record is no larger than the |
283 /// UpperLimit. | 366 // UpperLimit. Returns true if valid. Otherwise generates an error |
284 bool checkRecordSizeNoMoreThan(unsigned UpperLimit, const char *RecordName) { | 367 // and returns false. |
| 368 bool isValidRecordSizeAtMost(unsigned UpperLimit, const char *RecordName) { |
285 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 369 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
286 if (Values.size() > UpperLimit) { | 370 if (Values.size() <= UpperLimit) |
287 return RecordSizeError(UpperLimit, RecordName, "no more than"); | 371 return true; |
288 } | 372 ReportRecordSizeError(UpperLimit, RecordName, "no more than"); |
289 return false; | 373 return false; |
290 } | 374 } |
291 | 375 |
292 /// Checks if the size of the record is at least as large as the | 376 // Checks if the size of the record is at least as large as the |
293 /// LowerLimit, and no larger than the UpperLimit. | 377 // LowerLimit, and no larger than the UpperLimit. Returns true if |
294 bool checkRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit, | 378 // valid. Otherwise generates an error and returns false. |
| 379 bool isValidRecordSizeInRange(unsigned LowerLimit, unsigned UpperLimit, |
295 const char *RecordName) { | 380 const char *RecordName) { |
296 return checkRecordSizeAtLeast(LowerLimit, RecordName) || | 381 return isValidRecordSizeAtLeast(LowerLimit, RecordName) || |
297 checkRecordSizeNoMoreThan(UpperLimit, RecordName); | 382 isValidRecordSizeAtMost(UpperLimit, RecordName); |
298 } | 383 } |
299 | 384 |
300 private: | 385 private: |
301 /// Generates a record size error. ExpectedSize is the number | 386 /// Generates a record size error. ExpectedSize is the number |
302 /// of elements expected. RecordName is the name of the kind of | 387 /// of elements expected. RecordName is the name of the kind of |
303 /// record that has incorrect size. ContextMessage (if not 0) | 388 /// record that has incorrect size. ContextMessage (if not NULL) |
304 /// is appended to "record expects" to describe how ExpectedSize | 389 /// is appended to "record expects" to describe how ExpectedSize |
305 /// should be interpreted. | 390 /// should be interpreted. |
306 bool RecordSizeError(unsigned ExpectedSize, const char *RecordName, | 391 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName, |
307 const char *ContextMessage) { | 392 const char *ContextMessage); |
308 std::string Buffer; | |
309 raw_string_ostream StrBuf(Buffer); | |
310 StrBuf << RecordName << " record expects"; | |
311 if (ContextMessage) | |
312 StrBuf << " " << ContextMessage; | |
313 StrBuf << " " << ExpectedSize << " argument"; | |
314 if (ExpectedSize > 1) | |
315 StrBuf << "s"; | |
316 StrBuf << ". Found: " << Record.GetValues().size(); | |
317 return Error(StrBuf.str()); | |
318 } | |
319 }; | 393 }; |
320 | 394 |
| 395 void BlockParserBaseClass::ReportRecordSizeError( |
| 396 unsigned ExpectedSize, const char *RecordName, const char *ContextMessage) { |
| 397 std::string Buffer; |
| 398 raw_string_ostream StrBuf(Buffer); |
| 399 StrBuf << RecordName << " record expects"; |
| 400 if (ContextMessage) |
| 401 StrBuf << " " << ContextMessage; |
| 402 StrBuf << " " << ExpectedSize << " argument"; |
| 403 if (ExpectedSize > 1) |
| 404 StrBuf << "s"; |
| 405 StrBuf << ". Found: " << Record.GetValues().size(); |
| 406 Error(StrBuf.str()); |
| 407 } |
| 408 |
321 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { | 409 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { |
322 // If called, derived class doesn't know how to handle block. | 410 // If called, derived class doesn't know how to handle block. |
323 // Report error and skip. | 411 // Report error and skip. |
324 std::string Buffer; | 412 std::string Buffer; |
325 raw_string_ostream StrBuf(Buffer); | 413 raw_string_ostream StrBuf(Buffer); |
326 StrBuf << "Don't know how to parse block id: " << BlockID; | 414 StrBuf << "Don't know how to parse block id: " << BlockID; |
327 Error(StrBuf.str()); | 415 Error(StrBuf.str()); |
| 416 // TODO(kschimpf) Remove error recovery once implementation complete. |
328 SkipBlock(); | 417 SkipBlock(); |
329 return false; | 418 return false; |
330 } | 419 } |
331 | 420 |
332 void BlockParserBaseClass::ProcessRecord() { | 421 void BlockParserBaseClass::ProcessRecord() { |
333 // If called, derived class doesn't know how to handle. | 422 // If called, derived class doesn't know how to handle. |
334 std::string Buffer; | 423 std::string Buffer; |
335 raw_string_ostream StrBuf(Buffer); | 424 raw_string_ostream StrBuf(Buffer); |
336 StrBuf << "Don't know how to process record: " << Record; | 425 StrBuf << "Don't know how to process record: " << Record; |
337 Error(StrBuf.str()); | 426 Error(StrBuf.str()); |
(...skipping 14 matching lines...) Expand all Loading... |
352 | 441 |
353 virtual void ProcessRecord() LLVM_OVERRIDE; | 442 virtual void ProcessRecord() LLVM_OVERRIDE; |
354 }; | 443 }; |
355 | 444 |
356 void TypesParser::ProcessRecord() { | 445 void TypesParser::ProcessRecord() { |
357 Type *Ty = NULL; | 446 Type *Ty = NULL; |
358 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 447 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
359 switch (Record.GetCode()) { | 448 switch (Record.GetCode()) { |
360 case naclbitc::TYPE_CODE_NUMENTRY: | 449 case naclbitc::TYPE_CODE_NUMENTRY: |
361 // NUMENTRY: [numentries] | 450 // NUMENTRY: [numentries] |
362 if (checkRecordSize(1, "Type count")) | 451 if (!isValidRecordSize(1, "Type count")) |
363 return; | 452 return; |
364 Context->resizeTypeIDValues(Values[0]); | 453 Context->resizeTypeIDValues(Values[0]); |
365 return; | 454 return; |
366 case naclbitc::TYPE_CODE_VOID: | 455 case naclbitc::TYPE_CODE_VOID: |
367 // VOID | 456 // VOID |
368 if (checkRecordSize(0, "Type void")) | 457 if (!isValidRecordSize(0, "Type void")) |
369 break; | 458 return; |
370 Ty = Type::getVoidTy(Context->getLLVMContext()); | 459 Ty = Context->convertToLLVMType(Ice::IceType_void); |
371 break; | 460 break; |
372 case naclbitc::TYPE_CODE_FLOAT: | 461 case naclbitc::TYPE_CODE_FLOAT: |
373 // FLOAT | 462 // FLOAT |
374 if (checkRecordSize(0, "Type float")) | 463 if (!isValidRecordSize(0, "Type float")) |
375 break; | 464 return; |
376 Ty = Type::getFloatTy(Context->getLLVMContext()); | 465 Ty = Context->convertToLLVMType(Ice::IceType_f32); |
377 break; | 466 break; |
378 case naclbitc::TYPE_CODE_DOUBLE: | 467 case naclbitc::TYPE_CODE_DOUBLE: |
379 // DOUBLE | 468 // DOUBLE |
380 if (checkRecordSize(0, "Type double")) | 469 if (!isValidRecordSize(0, "Type double")) |
381 break; | 470 return; |
382 Ty = Type::getDoubleTy(Context->getLLVMContext()); | 471 Ty = Context->convertToLLVMType(Ice::IceType_f64); |
383 break; | 472 break; |
384 case naclbitc::TYPE_CODE_INTEGER: | 473 case naclbitc::TYPE_CODE_INTEGER: |
385 // INTEGER: [width] | 474 // INTEGER: [width] |
386 if (checkRecordSize(1, "Type integer")) | 475 if (!isValidRecordSize(1, "Type integer")) |
387 break; | 476 return; |
388 Ty = IntegerType::get(Context->getLLVMContext(), Values[0]); | 477 Ty = Context->getLLVMIntegerType(Values[0]); |
389 // TODO(kschimpf) Check if size is legal. | 478 if (Ty == NULL) { |
| 479 std::string Buffer; |
| 480 raw_string_ostream StrBuf(Buffer); |
| 481 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; |
| 482 Error(StrBuf.str()); |
| 483 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 484 // Fix type so that we can continue. |
| 485 Ty = Context->convertToLLVMType(Ice::IceType_i32); |
| 486 } |
390 break; | 487 break; |
391 case naclbitc::TYPE_CODE_VECTOR: | 488 case naclbitc::TYPE_CODE_VECTOR: { |
392 // VECTOR: [numelts, eltty] | 489 // VECTOR: [numelts, eltty] |
393 if (checkRecordSize(2, "Type vector")) | 490 if (!isValidRecordSize(2, "Type vector")) |
394 break; | 491 return; |
395 Ty = VectorType::get(Context->getTypeByID(Values[1]), Values[0]); | 492 Type *BaseTy = Context->getTypeByID(Values[1]); |
| 493 Ty = Context->getLLVMVectorType(Values[0], |
| 494 Context->convertToIceType(BaseTy)); |
| 495 if (Ty == NULL) { |
| 496 std::string Buffer; |
| 497 raw_string_ostream StrBuf(Buffer); |
| 498 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << *BaseTy |
| 499 << ">"; |
| 500 Error(StrBuf.str()); |
| 501 Ty = Context->convertToLLVMType(Ice::IceType_void); |
| 502 } |
396 break; | 503 break; |
| 504 } |
397 case naclbitc::TYPE_CODE_FUNCTION: { | 505 case naclbitc::TYPE_CODE_FUNCTION: { |
398 // FUNCTION: [vararg, retty, paramty x N] | 506 // FUNCTION: [vararg, retty, paramty x N] |
399 if (checkRecordSizeAtLeast(2, "Type signature")) | 507 if (!isValidRecordSizeAtLeast(2, "Type signature")) |
400 break; | 508 return; |
401 SmallVector<Type *, 8> ArgTys; | 509 SmallVector<Type *, 8> ArgTys; |
402 for (unsigned i = 2, e = Values.size(); i != e; ++i) { | 510 for (unsigned i = 2, e = Values.size(); i != e; ++i) { |
403 ArgTys.push_back(Context->getTypeByID(Values[i])); | 511 ArgTys.push_back(Context->getTypeByID(Values[i])); |
404 } | 512 } |
405 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); | 513 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); |
406 break; | 514 break; |
407 } | 515 } |
408 default: | 516 default: |
409 BlockParserBaseClass::ProcessRecord(); | 517 BlockParserBaseClass::ProcessRecord(); |
410 break; | 518 return; |
411 } | 519 } |
412 // If Ty not defined, assume error. Use void as filler. | 520 // If Ty not defined, assume error. Use void as filler. |
413 if (Ty == NULL) | 521 if (Ty == NULL) |
414 Ty = Type::getVoidTy(Context->getLLVMContext()); | 522 Ty = Context->convertToLLVMType(Ice::IceType_void); |
415 Context->setTypeID(NextTypeId++, Ty); | 523 Context->setTypeID(NextTypeId++, Ty); |
416 } | 524 } |
417 | 525 |
418 /// Parses the globals block (i.e. global variables). | 526 /// Parses the globals block (i.e. global variables). |
419 class GlobalsParser : public BlockParserBaseClass { | 527 class GlobalsParser : public BlockParserBaseClass { |
420 public: | 528 public: |
421 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 529 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
422 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), | 530 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), |
423 Alignment(1), IsConstant(false) { | 531 Alignment(1), IsConstant(false) { |
424 NextGlobalID = Context->getNumFunctionIDs(); | 532 NextGlobalID = Context->getNumFunctionIDs(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 if (InitializersNeeded != Initializers.size()) { | 575 if (InitializersNeeded != Initializers.size()) { |
468 std::string Buffer; | 576 std::string Buffer; |
469 raw_string_ostream StrBuf(Buffer); | 577 raw_string_ostream StrBuf(Buffer); |
470 StrBuf << "Global variable @g" | 578 StrBuf << "Global variable @g" |
471 << (NextGlobalID - Context->getNumFunctionIDs()) << " expected " | 579 << (NextGlobalID - Context->getNumFunctionIDs()) << " expected " |
472 << InitializersNeeded << " initializer"; | 580 << InitializersNeeded << " initializer"; |
473 if (InitializersNeeded > 1) | 581 if (InitializersNeeded > 1) |
474 StrBuf << "s"; | 582 StrBuf << "s"; |
475 StrBuf << ". Found: " << Initializers.size(); | 583 StrBuf << ". Found: " << Initializers.size(); |
476 Error(StrBuf.str()); | 584 Error(StrBuf.str()); |
| 585 // TODO(kschimpf) Remove error recovery once implementation complete. |
477 // Fix up state so that we can continue. | 586 // Fix up state so that we can continue. |
478 InitializersNeeded = Initializers.size(); | 587 InitializersNeeded = Initializers.size(); |
479 installGlobalVar(); | 588 installGlobalVar(); |
480 } | 589 } |
481 } | 590 } |
482 | 591 |
483 // Reserves a slot in the list of initializers being built. If there | 592 // Reserves a slot in the list of initializers being built. If there |
484 // isn't room for the slot, an error message is generated. | 593 // isn't room for the slot, an error message is generated. |
485 void reserveInitializer(const char *RecordName) { | 594 void reserveInitializer(const char *RecordName) { |
486 if (InitializersNeeded <= Initializers.size()) { | 595 if (InitializersNeeded <= Initializers.size()) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 Alignment = 1; | 632 Alignment = 1; |
524 IsConstant = false; | 633 IsConstant = false; |
525 } | 634 } |
526 }; | 635 }; |
527 | 636 |
528 void GlobalsParser::ProcessRecord() { | 637 void GlobalsParser::ProcessRecord() { |
529 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 638 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
530 switch (Record.GetCode()) { | 639 switch (Record.GetCode()) { |
531 case naclbitc::GLOBALVAR_COUNT: | 640 case naclbitc::GLOBALVAR_COUNT: |
532 // COUNT: [n] | 641 // COUNT: [n] |
533 if (checkRecordSize(1, "Globals count")) | 642 if (!isValidRecordSize(1, "Globals count")) |
534 return; | 643 return; |
535 if (NextGlobalID != Context->getNumFunctionIDs()) { | 644 if (NextGlobalID != Context->getNumFunctionIDs()) { |
536 Error("Globals count record not first in block."); | 645 Error("Globals count record not first in block."); |
537 return; | 646 return; |
538 } | 647 } |
539 verifyNoMissingInitializers(); | 648 verifyNoMissingInitializers(); |
540 Context->resizeValueIDsForGlobalVarCount(Values[0]); | 649 Context->resizeValueIDsForGlobalVarCount(Values[0]); |
541 return; | 650 return; |
542 case naclbitc::GLOBALVAR_VAR: { | 651 case naclbitc::GLOBALVAR_VAR: { |
543 // VAR: [align, isconst] | 652 // VAR: [align, isconst] |
544 if (checkRecordSize(2, "Globals variable")) | 653 if (!isValidRecordSize(2, "Globals variable")) |
545 return; | 654 return; |
546 verifyNoMissingInitializers(); | 655 verifyNoMissingInitializers(); |
547 InitializersNeeded = 1; | 656 InitializersNeeded = 1; |
548 Initializers.clear(); | 657 Initializers.clear(); |
549 Alignment = (1 << Values[0]) >> 1; | 658 Alignment = (1 << Values[0]) >> 1; |
550 IsConstant = Values[1] != 0; | 659 IsConstant = Values[1] != 0; |
551 return; | 660 return; |
552 } | 661 } |
553 case naclbitc::GLOBALVAR_COMPOUND: | 662 case naclbitc::GLOBALVAR_COMPOUND: |
554 // COMPOUND: [size] | 663 // COMPOUND: [size] |
555 if (checkRecordSize(1, "globals compound")) | 664 if (!isValidRecordSize(1, "globals compound")) |
556 return; | 665 return; |
557 if (Initializers.size() > 0 || InitializersNeeded != 1) { | 666 if (Initializers.size() > 0 || InitializersNeeded != 1) { |
558 Error("Globals compound record not first initializer"); | 667 Error("Globals compound record not first initializer"); |
559 return; | 668 return; |
560 } | 669 } |
561 if (Values[0] < 2) { | 670 if (Values[0] < 2) { |
562 std::string Buffer; | 671 std::string Buffer; |
563 raw_string_ostream StrBuf(Buffer); | 672 raw_string_ostream StrBuf(Buffer); |
564 StrBuf << "Globals compound record size invalid. Found: " << Values[0]; | 673 StrBuf << "Globals compound record size invalid. Found: " << Values[0]; |
565 Error(StrBuf.str()); | 674 Error(StrBuf.str()); |
566 return; | 675 return; |
567 } | 676 } |
568 InitializersNeeded = Values[0]; | 677 InitializersNeeded = Values[0]; |
569 return; | 678 return; |
570 case naclbitc::GLOBALVAR_ZEROFILL: { | 679 case naclbitc::GLOBALVAR_ZEROFILL: { |
571 // ZEROFILL: [size] | 680 // ZEROFILL: [size] |
572 if (checkRecordSize(1, "Globals zerofill")) | 681 if (!isValidRecordSize(1, "Globals zerofill")) |
573 return; | 682 return; |
574 reserveInitializer("Globals zerofill"); | 683 reserveInitializer("Globals zerofill"); |
575 Type *Ty = | 684 Type *Ty = |
576 ArrayType::get(Type::getInt8Ty(Context->getLLVMContext()), Values[0]); | 685 ArrayType::get(Context->convertToLLVMType(Ice::IceType_i8), Values[0]); |
577 Constant *Zero = ConstantAggregateZero::get(Ty); | 686 Constant *Zero = ConstantAggregateZero::get(Ty); |
578 Initializers.push_back(Zero); | 687 Initializers.push_back(Zero); |
579 break; | 688 break; |
580 } | 689 } |
581 case naclbitc::GLOBALVAR_DATA: { | 690 case naclbitc::GLOBALVAR_DATA: { |
582 // DATA: [b0, b1, ...] | 691 // DATA: [b0, b1, ...] |
583 if (checkRecordSizeAtLeast(1, "Globals data")) | 692 if (!isValidRecordSizeAtLeast(1, "Globals data")) |
584 return; | 693 return; |
585 reserveInitializer("Globals data"); | 694 reserveInitializer("Globals data"); |
586 unsigned Size = Values.size(); | 695 unsigned Size = Values.size(); |
587 SmallVector<uint8_t, 32> Buf; | 696 SmallVector<uint8_t, 32> Buf; |
588 for (unsigned i = 0; i < Size; ++i) | 697 for (unsigned i = 0; i < Size; ++i) |
589 Buf.push_back(static_cast<uint8_t>(Values[i])); | 698 Buf.push_back(static_cast<uint8_t>(Values[i])); |
590 Constant *Init = ConstantDataArray::get( | 699 Constant *Init = ConstantDataArray::get( |
591 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size())); | 700 Context->getLLVMContext(), ArrayRef<uint8_t>(Buf.data(), Buf.size())); |
592 Initializers.push_back(Init); | 701 Initializers.push_back(Init); |
593 break; | 702 break; |
594 } | 703 } |
595 case naclbitc::GLOBALVAR_RELOC: { | 704 case naclbitc::GLOBALVAR_RELOC: { |
596 // RELOC: [val, [addend]] | 705 // RELOC: [val, [addend]] |
597 if (checkRecordSizeInRange(1, 2, "Globals reloc")) | 706 if (!isValidRecordSizeInRange(1, 2, "Globals reloc")) |
598 return; | 707 return; |
599 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); | 708 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); |
600 if (BaseVal == 0) { | 709 if (BaseVal == NULL) { |
601 std::string Buffer; | 710 std::string Buffer; |
602 raw_string_ostream StrBuf(Buffer); | 711 raw_string_ostream StrBuf(Buffer); |
603 StrBuf << "Can't find global relocation value: " << Values[0]; | 712 StrBuf << "Can't find global relocation value: " << Values[0]; |
604 Error(StrBuf.str()); | 713 Error(StrBuf.str()); |
605 return; | 714 return; |
606 } | 715 } |
607 Type *IntPtrType = IntegerType::get(Context->getLLVMContext(), 32); | 716 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType()); |
608 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); | 717 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); |
609 if (Values.size() == 2) { | 718 if (Values.size() == 2) { |
610 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); | 719 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); |
611 } | 720 } |
612 Initializers.push_back(Val); | 721 Initializers.push_back(Val); |
613 break; | 722 break; |
614 } | 723 } |
615 default: | 724 default: |
616 BlockParserBaseClass::ProcessRecord(); | 725 BlockParserBaseClass::ProcessRecord(); |
617 return; | 726 return; |
(...skipping 29 matching lines...) Expand all Loading... |
647 } | 756 } |
648 } | 757 } |
649 }; | 758 }; |
650 | 759 |
651 void ValuesymtabParser::ProcessRecord() { | 760 void ValuesymtabParser::ProcessRecord() { |
652 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 761 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
653 StringType ConvertedName; | 762 StringType ConvertedName; |
654 switch (Record.GetCode()) { | 763 switch (Record.GetCode()) { |
655 case naclbitc::VST_CODE_ENTRY: { | 764 case naclbitc::VST_CODE_ENTRY: { |
656 // VST_ENTRY: [ValueId, namechar x N] | 765 // VST_ENTRY: [ValueId, namechar x N] |
657 if (checkRecordSizeAtLeast(2, "Valuesymtab value entry")) | 766 if (!isValidRecordSizeAtLeast(2, "Valuesymtab value entry")) |
658 return; | 767 return; |
659 ConvertToString(ConvertedName); | 768 ConvertToString(ConvertedName); |
660 Value *V = Context->getGlobalValueByID(Values[0]); | 769 Value *V = Context->getGlobalValueByID(Values[0]); |
661 if (V == 0) { | 770 if (V == NULL) { |
662 std::string Buffer; | 771 std::string Buffer; |
663 raw_string_ostream StrBuf(Buffer); | 772 raw_string_ostream StrBuf(Buffer); |
664 StrBuf << "Invalid global address ID in valuesymtab: " << Values[0]; | 773 StrBuf << "Invalid global address ID in valuesymtab: " << Values[0]; |
665 Error(StrBuf.str()); | 774 Error(StrBuf.str()); |
666 return; | 775 return; |
667 } | 776 } |
668 V->setName(StringRef(ConvertedName.data(), ConvertedName.size())); | 777 V->setName(StringRef(ConvertedName.data(), ConvertedName.size())); |
669 return; | 778 return; |
670 } | 779 } |
671 case naclbitc::VST_CODE_BBENTRY: { | 780 case naclbitc::VST_CODE_BBENTRY: { |
672 // VST_BBENTRY: [BbId, namechar x N] | 781 // VST_BBENTRY: [BbId, namechar x N] |
673 // For now, since we aren't processing function blocks, don't handle. | 782 // For now, since we aren't processing function blocks, don't handle. |
674 if (AllowBbEntries) { | 783 if (AllowBbEntries) { |
675 Error("Valuesymtab bb entry not implemented"); | 784 Error("Valuesymtab bb entry not implemented"); |
676 return; | 785 return; |
677 } | 786 } |
678 break; | 787 break; |
679 } | 788 } |
680 default: | 789 default: |
681 break; | 790 break; |
682 } | 791 } |
683 // If reached, don't know how to handle record. | 792 // If reached, don't know how to handle record. |
684 BlockParserBaseClass::ProcessRecord(); | 793 BlockParserBaseClass::ProcessRecord(); |
685 return; | 794 return; |
686 } | 795 } |
687 | 796 |
| 797 /// Parses function blocks in the bitcode file. |
| 798 class FunctionParser : public BlockParserBaseClass { |
| 799 FunctionParser(const FunctionParser&) LLVM_DELETED_FUNCTION; |
| 800 FunctionParser &operator=(const FunctionParser&) LLVM_DELETED_FUNCTION; |
| 801 public: |
| 802 FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 803 : BlockParserBaseClass(BlockID, EnclosingParser), |
| 804 Func(new Ice::Cfg(getTranslator().getContext())), CurrentBbIndex(0), |
| 805 FcnId(Context->getNextFunctionBlockValueID()), |
| 806 LLVMFunc(cast<Function>(Context->getGlobalValueByID(FcnId))), |
| 807 CachedNumGlobalValueIDs(Context->getNumGlobalValueIDs()), |
| 808 InstIsTerminating(false) { |
| 809 Func->setFunctionName(LLVMFunc->getName()); |
| 810 Func->setReturnType(Context->convertToIceType(LLVMFunc->getReturnType())); |
| 811 Func->setInternal(LLVMFunc->hasInternalLinkage()); |
| 812 CurrentNode = InstallNextBasicBlock(); |
| 813 for (Function::const_arg_iterator ArgI = LLVMFunc->arg_begin(), |
| 814 ArgE = LLVMFunc->arg_end(); |
| 815 ArgI != ArgE; ++ArgI) { |
| 816 Func->addArg(NextInstVar(Context->convertToIceType(ArgI->getType()))); |
| 817 } |
| 818 } |
| 819 |
| 820 ~FunctionParser() LLVM_OVERRIDE; |
| 821 |
| 822 private: |
| 823 // Timer for reading function bitcode and converting to ICE. |
| 824 Ice::Timer TConvert; |
| 825 // The corresponding ICE function defined by the function block. |
| 826 Ice::Cfg *Func; |
| 827 // The index to the current basic block being built. |
| 828 uint32_t CurrentBbIndex; |
| 829 // The basic block being built. |
| 830 Ice::CfgNode *CurrentNode; |
| 831 // The ID for the function. |
| 832 unsigned FcnId; |
| 833 // The corresponding LLVM function. |
| 834 Function *LLVMFunc; |
| 835 // Holds operands local to the function block, based on indices |
| 836 // defined in the bitcode file. |
| 837 std::vector<Ice::Operand *> LocalOperands; |
| 838 // Holds the dividing point between local and global absolute value indices. |
| 839 uint32_t CachedNumGlobalValueIDs; |
| 840 // True if the last processed instruction was a terminating |
| 841 // instruction. |
| 842 bool InstIsTerminating; |
| 843 |
| 844 virtual void ProcessRecord() LLVM_OVERRIDE; |
| 845 |
| 846 virtual void ExitBlock() LLVM_OVERRIDE; |
| 847 |
| 848 // Creates and appends a new basic block to the list of basic blocks. |
| 849 Ice::CfgNode *InstallNextBasicBlock() { return Func->makeNode(); } |
| 850 |
| 851 // Returns the Index-th basic block in the list of basic blocks. |
| 852 Ice::CfgNode *GetBasicBlock(uint32_t Index) { |
| 853 const Ice::NodeList &Nodes = Func->getNodes(); |
| 854 if (Index >= Nodes.size()) { |
| 855 std::string Buffer; |
| 856 raw_string_ostream StrBuf(Buffer); |
| 857 StrBuf << "Reference to basic block " << Index |
| 858 << " not found. Must be less than " << Nodes.size(); |
| 859 Error(StrBuf.str()); |
| 860 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 861 Index = 0; |
| 862 } |
| 863 return Nodes[Index]; |
| 864 } |
| 865 |
| 866 // Generates the next available local variable using the given |
| 867 // type. Note: if Ty is void, this function returns NULL. |
| 868 Ice::Variable *NextInstVar(Ice::Type Ty) { |
| 869 if (Ty == Ice::IceType_void) |
| 870 return NULL; |
| 871 Ice::Variable *Var = Func->makeVariable(Ty, CurrentNode); |
| 872 LocalOperands.push_back(Var); |
| 873 return Var; |
| 874 } |
| 875 |
| 876 // Converts a relative index (to the next instruction to be read) to |
| 877 // an absolute value index. |
| 878 uint32_t convertRelativeToAbsIndex(int32_t Id) { |
| 879 int32_t AbsNextId = CachedNumGlobalValueIDs + LocalOperands.size(); |
| 880 if (Id > 0 && AbsNextId < static_cast<uint32_t>(Id)) { |
| 881 std::string Buffer; |
| 882 raw_string_ostream StrBuf(Buffer); |
| 883 StrBuf << "Invalid relative value id: " << Id |
| 884 << " (must be <= " << AbsNextId << ")"; |
| 885 Error(StrBuf.str()); |
| 886 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 887 return 0; |
| 888 } |
| 889 return AbsNextId - Id; |
| 890 } |
| 891 |
| 892 // Returns the value referenced by the given value Index. |
| 893 Ice::Operand *getOperand(uint32_t Index) { |
| 894 if (Index < CachedNumGlobalValueIDs) { |
| 895 // TODO(kschimpf): Define implementation. |
| 896 report_fatal_error("getOperand of global addresses not implemented"); |
| 897 } |
| 898 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; |
| 899 if (LocalIndex >= LocalOperands.size()) { |
| 900 std::string Buffer; |
| 901 raw_string_ostream StrBuf(Buffer); |
| 902 StrBuf << "Value index " << Index << " out of range. Must be less than " |
| 903 << (LocalOperands.size() + CachedNumGlobalValueIDs); |
| 904 Error(StrBuf.str()); |
| 905 report_fatal_error("Unable to continue"); |
| 906 } |
| 907 return LocalOperands[LocalIndex]; |
| 908 } |
| 909 |
| 910 // Generates type error message for binary operator Op |
| 911 // operating on Type OpTy. |
| 912 void ReportInvalidBinaryOp(Ice::InstArithmetic::OpKind Op, |
| 913 Ice::Type OpTy); |
| 914 |
| 915 // Validates if integer logical Op, for type OpTy, is valid. |
| 916 // Returns true if valid. Otherwise generates error message and |
| 917 // returns false. |
| 918 bool isValidIntegerLogicalOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 919 if (Ice::isIntegerType(OpTy)) |
| 920 return true; |
| 921 ReportInvalidBinaryOp(Op, OpTy); |
| 922 return false; |
| 923 } |
| 924 |
| 925 // Validates if integer (or vector of integers) arithmetic Op, for type |
| 926 // OpTy, is valid. Returns true if valid. Otherwise generates |
| 927 // error message and returns false. |
| 928 bool isValidIntegerArithOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 929 if (Ice::isIntegerArithmeticType(OpTy)) |
| 930 return true; |
| 931 ReportInvalidBinaryOp(Op, OpTy); |
| 932 return false; |
| 933 } |
| 934 |
| 935 // Checks if floating arithmetic Op, for type OpTy, is valid. |
| 936 // Returns false if valid. Otherwise generates an error message and |
| 937 // returns true. |
| 938 bool isValidFloatingArithOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 939 if (Ice::isFloatingType(OpTy)) |
| 940 return true; |
| 941 ReportInvalidBinaryOp(Op, OpTy); |
| 942 return false; |
| 943 } |
| 944 |
| 945 // Reports that the given binary Opcode, for the given type Ty, |
| 946 // is not understood. |
| 947 void ReportInvalidBinopOpcode(unsigned Opcode, Ice::Type Ty); |
| 948 |
| 949 // Takes the PNaCl bitcode binary operator Opcode, and the opcode |
| 950 // type Ty, and sets Op to the corresponding ICE binary |
| 951 // opcode. Returns true if able to convert, false otherwise. |
| 952 bool convertBinopOpcode(unsigned Opcode, Ice::Type Ty, |
| 953 Ice::InstArithmetic::OpKind &Op) { |
| 954 Instruction::BinaryOps LLVMOpcode; |
| 955 if (!naclbitc::DecodeBinaryOpcode(Opcode, Context->convertToLLVMType(Ty), |
| 956 LLVMOpcode)) { |
| 957 ReportInvalidBinopOpcode(Opcode, Ty); |
| 958 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 959 Op = Ice::InstArithmetic::Add; |
| 960 return false; |
| 961 } |
| 962 switch (LLVMOpcode) { |
| 963 default: { |
| 964 ReportInvalidBinopOpcode(Opcode, Ty); |
| 965 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 966 Op = Ice::InstArithmetic::Add; |
| 967 return false; |
| 968 } |
| 969 case Instruction::Add: |
| 970 Op = Ice::InstArithmetic::Add; |
| 971 return isValidIntegerArithOp(Op, Ty); |
| 972 case Instruction::FAdd: |
| 973 Op = Ice::InstArithmetic::Fadd; |
| 974 return isValidFloatingArithOp(Op, Ty); |
| 975 case Instruction::Sub: |
| 976 Op = Ice::InstArithmetic::Sub; |
| 977 return isValidIntegerArithOp(Op, Ty); |
| 978 case Instruction::FSub: |
| 979 Op = Ice::InstArithmetic::Fsub; |
| 980 return isValidFloatingArithOp(Op, Ty); |
| 981 case Instruction::Mul: |
| 982 Op = Ice::InstArithmetic::Mul; |
| 983 return isValidIntegerArithOp(Op, Ty); |
| 984 case Instruction::FMul: |
| 985 Op = Ice::InstArithmetic::Fmul; |
| 986 return isValidFloatingArithOp(Op, Ty); |
| 987 case Instruction::UDiv: |
| 988 Op = Ice::InstArithmetic::Udiv; |
| 989 return isValidIntegerArithOp(Op, Ty); |
| 990 case Instruction::SDiv: |
| 991 Op = Ice::InstArithmetic::Sdiv; |
| 992 return isValidIntegerArithOp(Op, Ty); |
| 993 case Instruction::FDiv: |
| 994 Op = Ice::InstArithmetic::Fdiv; |
| 995 return isValidFloatingArithOp(Op, Ty); |
| 996 case Instruction::URem: |
| 997 Op = Ice::InstArithmetic::Urem; |
| 998 return isValidIntegerArithOp(Op, Ty); |
| 999 case Instruction::SRem: |
| 1000 Op = Ice::InstArithmetic::Srem; |
| 1001 return isValidIntegerArithOp(Op, Ty); |
| 1002 case Instruction::FRem: |
| 1003 Op = Ice::InstArithmetic::Frem; |
| 1004 return isValidFloatingArithOp(Op, Ty); |
| 1005 case Instruction::Shl: |
| 1006 Op = Ice::InstArithmetic::Shl; |
| 1007 return isValidIntegerArithOp(Op, Ty); |
| 1008 case Instruction::LShr: |
| 1009 Op = Ice::InstArithmetic::Lshr; |
| 1010 return isValidIntegerArithOp(Op, Ty); |
| 1011 case Instruction::AShr: |
| 1012 Op = Ice::InstArithmetic::Ashr; |
| 1013 return isValidIntegerArithOp(Op, Ty); |
| 1014 case Instruction::And: |
| 1015 Op = Ice::InstArithmetic::And; |
| 1016 return isValidIntegerLogicalOp(Op, Ty); |
| 1017 case Instruction::Or: |
| 1018 Op = Ice::InstArithmetic::Or; |
| 1019 return isValidIntegerLogicalOp(Op, Ty); |
| 1020 case Instruction::Xor: |
| 1021 Op = Ice::InstArithmetic::Xor; |
| 1022 return isValidIntegerLogicalOp(Op, Ty); |
| 1023 } |
| 1024 } |
| 1025 }; |
| 1026 |
| 1027 FunctionParser::~FunctionParser() { |
| 1028 if (getTranslator().getFlags().SubzeroTimingEnabled) { |
| 1029 errs() << "[Subzero timing] Convert function " << Func->getFunctionName() |
| 1030 << ": " << TConvert.getElapsedSec() << " sec\n"; |
| 1031 } |
| 1032 } |
| 1033 |
| 1034 void FunctionParser::ReportInvalidBinopOpcode(unsigned Opcode, Ice::Type Ty) { |
| 1035 std::string Buffer; |
| 1036 raw_string_ostream StrBuf(Buffer); |
| 1037 StrBuf << "Binary opcode " << Opcode << "not understood for type " << Ty; |
| 1038 Error(StrBuf.str()); |
| 1039 } |
| 1040 |
| 1041 void FunctionParser::ExitBlock() { |
| 1042 // Before translating, check for blocks without instructions, and |
| 1043 // insert unreachable. This shouldn't happen, but be safe. |
| 1044 unsigned Index = 0; |
| 1045 const Ice::NodeList &Nodes = Func->getNodes(); |
| 1046 for (std::vector<Ice::CfgNode *>::const_iterator Iter = Nodes.begin(), |
| 1047 IterEnd = Nodes.end(); |
| 1048 Iter != IterEnd; ++Iter, ++Index) { |
| 1049 Ice::CfgNode *Node = *Iter; |
| 1050 if (Node->getInsts().size() == 0) { |
| 1051 std::string Buffer; |
| 1052 raw_string_ostream StrBuf(Buffer); |
| 1053 StrBuf << "Basic block " << Index << " contains no instructions"; |
| 1054 Error(StrBuf.str()); |
| 1055 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 1056 Node->appendInst(Ice::InstUnreachable::create(Func)); |
| 1057 } |
| 1058 } |
| 1059 getTranslator().translateFcn(Func); |
| 1060 } |
| 1061 |
| 1062 void FunctionParser::ReportInvalidBinaryOp( |
| 1063 Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 1064 std::string Buffer; |
| 1065 raw_string_ostream StrBuf(Buffer); |
| 1066 StrBuf << "Invalid operator type for " |
| 1067 << Ice::InstArithmetic::getOpName(Op) |
| 1068 << ". Found " << OpTy; |
| 1069 Error(StrBuf.str()); |
| 1070 } |
| 1071 |
| 1072 void FunctionParser::ProcessRecord() { |
| 1073 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 1074 if (InstIsTerminating) { |
| 1075 InstIsTerminating = false; |
| 1076 CurrentNode = GetBasicBlock(++CurrentBbIndex); |
| 1077 } |
| 1078 Ice::Inst *Inst = NULL; |
| 1079 switch (Record.GetCode()) { |
| 1080 case naclbitc::FUNC_CODE_DECLAREBLOCKS: { |
| 1081 // DECLAREBLOCKS: [n] |
| 1082 if (!isValidRecordSize(1, "function block count")) |
| 1083 break; |
| 1084 if (Func->getNodes().size() != 1) { |
| 1085 Error("Duplicate function block count record"); |
| 1086 return; |
| 1087 } |
| 1088 uint32_t NumBbs = Values[0]; |
| 1089 if (NumBbs == 0) { |
| 1090 Error("Functions must contain at least one basic block."); |
| 1091 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 1092 NumBbs = 1; |
| 1093 } |
| 1094 // Install the basic blocks, skipping bb0 which was created in the |
| 1095 // constructor. |
| 1096 for (size_t i = 1; i < NumBbs; ++i) |
| 1097 InstallNextBasicBlock(); |
| 1098 break; |
| 1099 } |
| 1100 case naclbitc::FUNC_CODE_INST_BINOP: { |
| 1101 // BINOP: [opval, opval, opcode] |
| 1102 if (!isValidRecordSize(3, "function block binop")) |
| 1103 break; |
| 1104 Ice::Operand *Op1 = getOperand(convertRelativeToAbsIndex(Values[0])); |
| 1105 Ice::Operand *Op2 = getOperand(convertRelativeToAbsIndex(Values[1])); |
| 1106 Ice::Type Type1 = Op1->getType(); |
| 1107 Ice::Type Type2 = Op2->getType(); |
| 1108 if (Type1 != Type2) { |
| 1109 std::string Buffer; |
| 1110 raw_string_ostream StrBuf(Buffer); |
| 1111 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; |
| 1112 Error(StrBuf.str()); |
| 1113 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 1114 Op2 = Op1; |
| 1115 } |
| 1116 |
| 1117 Ice::InstArithmetic::OpKind Opcode; |
| 1118 if (!convertBinopOpcode(Values[2], Type1, Opcode)) |
| 1119 break; |
| 1120 Ice::Variable *Dest = NextInstVar(Type1); |
| 1121 Inst = Ice::InstArithmetic::create(Func, Opcode, Dest, Op1, Op2); |
| 1122 break; |
| 1123 } |
| 1124 case naclbitc::FUNC_CODE_INST_RET: { |
| 1125 // RET: [opval?] |
| 1126 InstIsTerminating = true; |
| 1127 if (!isValidRecordSizeInRange(0, 1, "function block ret")) |
| 1128 break; |
| 1129 if (Values.size() == 0) { |
| 1130 Inst = Ice::InstRet::create(Func); |
| 1131 } else { |
| 1132 Inst = Ice::InstRet::create( |
| 1133 Func, getOperand(convertRelativeToAbsIndex(Values[0]))); |
| 1134 } |
| 1135 break; |
| 1136 } |
| 1137 default: |
| 1138 // Generate error message! |
| 1139 BlockParserBaseClass::ProcessRecord(); |
| 1140 break; |
| 1141 } |
| 1142 if (Inst) |
| 1143 CurrentNode->appendInst(Inst); |
| 1144 } |
| 1145 |
688 /// Parses the module block in the bitcode file. | 1146 /// Parses the module block in the bitcode file. |
689 class ModuleParser : public BlockParserBaseClass { | 1147 class ModuleParser : public BlockParserBaseClass { |
690 public: | 1148 public: |
691 ModuleParser(unsigned BlockID, TopLevelParser *Context) | 1149 ModuleParser(unsigned BlockID, TopLevelParser *Context) |
692 : BlockParserBaseClass(BlockID, Context) {} | 1150 : BlockParserBaseClass(BlockID, Context) {} |
693 | 1151 |
694 virtual ~ModuleParser() LLVM_OVERRIDE {} | 1152 virtual ~ModuleParser() LLVM_OVERRIDE {} |
695 | 1153 |
696 protected: | 1154 protected: |
697 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 1155 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
(...skipping 11 matching lines...) Expand all Loading... |
709 } | 1167 } |
710 case naclbitc::GLOBALVAR_BLOCK_ID: { | 1168 case naclbitc::GLOBALVAR_BLOCK_ID: { |
711 GlobalsParser Parser(BlockID, this); | 1169 GlobalsParser Parser(BlockID, this); |
712 return Parser.ParseThisBlock(); | 1170 return Parser.ParseThisBlock(); |
713 } | 1171 } |
714 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { | 1172 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
715 ValuesymtabParser Parser(BlockID, this, false); | 1173 ValuesymtabParser Parser(BlockID, this, false); |
716 return Parser.ParseThisBlock(); | 1174 return Parser.ParseThisBlock(); |
717 } | 1175 } |
718 case naclbitc::FUNCTION_BLOCK_ID: { | 1176 case naclbitc::FUNCTION_BLOCK_ID: { |
719 Error("Function block parser not yet implemented, skipping"); | 1177 FunctionParser Parser(BlockID, this); |
720 SkipBlock(); | 1178 return Parser.ParseThisBlock(); |
721 return false; | |
722 } | 1179 } |
723 default: | 1180 default: |
724 return BlockParserBaseClass::ParseBlock(BlockID); | 1181 return BlockParserBaseClass::ParseBlock(BlockID); |
725 } | 1182 } |
726 } | 1183 } |
727 | 1184 |
728 void ModuleParser::ProcessRecord() { | 1185 void ModuleParser::ProcessRecord() { |
729 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 1186 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
730 switch (Record.GetCode()) { | 1187 switch (Record.GetCode()) { |
731 case naclbitc::MODULE_CODE_VERSION: { | 1188 case naclbitc::MODULE_CODE_VERSION: { |
732 // VERSION: [version#] | 1189 // VERSION: [version#] |
733 if (checkRecordSize(1, "Module version")) | 1190 if (!isValidRecordSize(1, "Module version")) |
734 return; | 1191 return; |
735 unsigned Version = Values[0]; | 1192 unsigned Version = Values[0]; |
736 if (Version != 1) { | 1193 if (Version != 1) { |
737 std::string Buffer; | 1194 std::string Buffer; |
738 raw_string_ostream StrBuf(Buffer); | 1195 raw_string_ostream StrBuf(Buffer); |
739 StrBuf << "Unknown bitstream version: " << Version; | 1196 StrBuf << "Unknown bitstream version: " << Version; |
740 Error(StrBuf.str()); | 1197 Error(StrBuf.str()); |
741 } | 1198 } |
742 return; | 1199 return; |
743 } | 1200 } |
744 case naclbitc::MODULE_CODE_FUNCTION: { | 1201 case naclbitc::MODULE_CODE_FUNCTION: { |
745 // FUNCTION: [type, callingconv, isproto, linkage] | 1202 // FUNCTION: [type, callingconv, isproto, linkage] |
746 if (checkRecordSize(4, "Function heading")) | 1203 if (!isValidRecordSize(4, "Function heading")) |
747 return; | 1204 return; |
748 Type *Ty = Context->getTypeByID(Values[0]); | 1205 Type *Ty = Context->getTypeByID(Values[0]); |
749 FunctionType *FTy = dyn_cast<FunctionType>(Ty); | 1206 FunctionType *FTy = dyn_cast<FunctionType>(Ty); |
750 if (FTy == 0) { | 1207 if (FTy == NULL) { |
751 std::string Buffer; | 1208 std::string Buffer; |
752 raw_string_ostream StrBuf(Buffer); | 1209 raw_string_ostream StrBuf(Buffer); |
753 StrBuf << "Function heading expects function type. Found: " << Ty; | 1210 StrBuf << "Function heading expects function type. Found: " << Ty; |
754 Error(StrBuf.str()); | 1211 Error(StrBuf.str()); |
755 return; | 1212 return; |
756 } | 1213 } |
757 CallingConv::ID CallingConv; | 1214 CallingConv::ID CallingConv; |
758 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { | 1215 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { |
759 std::string Buffer; | 1216 std::string Buffer; |
760 raw_string_ostream StrBuf(Buffer); | 1217 raw_string_ostream StrBuf(Buffer); |
(...skipping 20 matching lines...) Expand all Loading... |
781 } | 1238 } |
782 default: | 1239 default: |
783 BlockParserBaseClass::ProcessRecord(); | 1240 BlockParserBaseClass::ProcessRecord(); |
784 return; | 1241 return; |
785 } | 1242 } |
786 } | 1243 } |
787 | 1244 |
788 bool TopLevelParser::ParseBlock(unsigned BlockID) { | 1245 bool TopLevelParser::ParseBlock(unsigned BlockID) { |
789 if (BlockID == naclbitc::MODULE_BLOCK_ID) { | 1246 if (BlockID == naclbitc::MODULE_BLOCK_ID) { |
790 ModuleParser Parser(BlockID, this); | 1247 ModuleParser Parser(BlockID, this); |
791 bool ReturnValue = Parser.ParseThisBlock(); | 1248 return Parser.ParseThisBlock(); |
792 // TODO(kschimpf): Remove once translating function blocks. | |
793 errs() << "Global addresses:\n"; | |
794 for (size_t i = 0; i < ValueIDValues.size(); ++i) { | |
795 errs() << "[" << i << "]: " << *ValueIDValues[i] << "\n"; | |
796 } | |
797 return ReturnValue; | |
798 } | 1249 } |
799 // Generate error message by using default block implementation. | 1250 // Generate error message by using default block implementation. |
800 BlockParserBaseClass Parser(BlockID, this); | 1251 BlockParserBaseClass Parser(BlockID, this); |
801 return Parser.ParseThisBlock(); | 1252 return Parser.ParseThisBlock(); |
802 } | 1253 } |
803 | 1254 |
804 } // end of anonymous namespace. | 1255 } // end of anonymous namespace. |
805 | 1256 |
806 namespace Ice { | 1257 namespace Ice { |
807 | 1258 |
(...skipping 21 matching lines...) Expand all Loading... |
829 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 1280 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
830 errs() << "Invalid PNaCl bitcode header.\n"; | 1281 errs() << "Invalid PNaCl bitcode header.\n"; |
831 ErrorStatus = true; | 1282 ErrorStatus = true; |
832 return; | 1283 return; |
833 } | 1284 } |
834 | 1285 |
835 // Create a bitstream reader to read the bitcode file. | 1286 // Create a bitstream reader to read the bitcode file. |
836 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 1287 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
837 NaClBitstreamCursor InputStream(InputStreamFile); | 1288 NaClBitstreamCursor InputStream(InputStreamFile); |
838 | 1289 |
839 TopLevelParser Parser(MemBuf->getBufferIdentifier(), Header, InputStream, | 1290 TopLevelParser Parser(*this, MemBuf->getBufferIdentifier(), Header, |
840 ErrorStatus); | 1291 InputStream, ErrorStatus); |
841 int TopLevelBlocks = 0; | 1292 int TopLevelBlocks = 0; |
842 while (!InputStream.AtEndOfStream()) { | 1293 while (!InputStream.AtEndOfStream()) { |
843 if (Parser.Parse()) { | 1294 if (Parser.Parse()) { |
844 ErrorStatus = true; | 1295 ErrorStatus = true; |
845 return; | 1296 return; |
846 } | 1297 } |
847 ++TopLevelBlocks; | 1298 ++TopLevelBlocks; |
848 } | 1299 } |
849 | 1300 |
850 if (TopLevelBlocks != 1) { | 1301 if (TopLevelBlocks != 1) { |
851 errs() << IRFilename | 1302 errs() << IRFilename |
852 << ": Contains more than one module. Found: " << TopLevelBlocks | 1303 << ": Contains more than one module. Found: " << TopLevelBlocks |
853 << "\n"; | 1304 << "\n"; |
854 ErrorStatus = true; | 1305 ErrorStatus = true; |
855 } | 1306 } |
856 return; | 1307 return; |
857 } | 1308 } |
858 | 1309 |
859 } // end of anonymous namespace. | 1310 } // end of anonymous namespace. |
OLD | NEW |