Chromium Code Reviews| 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" |
|
Jim Stichnoth
2014/07/17 23:00:33
Alphabetize this after Ice*.h if possible.
Karl
2014/07/18 20:27:43
I intentionally placed this header file first, fol
| |
| 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" | |
| 23 #include "llvm/Analysis/NaCl/PNaClABITypeChecker.h" | |
| 17 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" | 24 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
| 18 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 25 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
| 19 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 26 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 20 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 27 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 21 #include "llvm/IR/Constants.h" | 28 #include "llvm/IR/Constants.h" |
| 22 #include "llvm/IR/LLVMContext.h" | 29 #include "llvm/IR/LLVMContext.h" |
| 23 #include "llvm/IR/Module.h" | 30 #include "llvm/IR/Module.h" |
| 24 #include "llvm/Support/Format.h" | 31 #include "llvm/Support/Format.h" |
| 25 #include "llvm/Support/MemoryBuffer.h" | 32 #include "llvm/Support/MemoryBuffer.h" |
| 26 #include "llvm/Support/raw_ostream.h" | 33 #include "llvm/Support/raw_ostream.h" |
| 27 #include "llvm/Support/ValueHandle.h" | 34 #include "llvm/Support/ValueHandle.h" |
| 28 | 35 |
| 29 #include <vector> | 36 #include <vector> |
| 30 #include <cassert> | 37 #include <cassert> |
| 31 | 38 |
| 32 using namespace llvm; | 39 using namespace llvm; |
| 33 | 40 |
| 34 namespace { | 41 namespace { |
| 35 | 42 |
| 36 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 43 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
| 37 class TopLevelParser : public NaClBitcodeParser { | 44 class TopLevelParser : public NaClBitcodeParser { |
| 38 TopLevelParser(const TopLevelParser &) LLVM_DELETED_FUNCTION; | 45 TopLevelParser(const TopLevelParser &) LLVM_DELETED_FUNCTION; |
| 39 TopLevelParser &operator=(const TopLevelParser &) LLVM_DELETED_FUNCTION; | 46 TopLevelParser &operator=(const TopLevelParser &) LLVM_DELETED_FUNCTION; |
| 40 | 47 |
| 41 public: | 48 public: |
| 42 TopLevelParser(const std::string &InputName, NaClBitcodeHeader &Header, | 49 TopLevelParser(Ice::Translator &Translator, |
| 50 const std::string &InputName, NaClBitcodeHeader &Header, | |
| 43 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 51 NaClBitstreamCursor &Cursor, bool &ErrorStatus) |
| 44 : NaClBitcodeParser(Cursor), | 52 : NaClBitcodeParser(Cursor), |
| 53 Translator(Translator), | |
| 45 Mod(new Module(InputName, getGlobalContext())), Header(Header), | 54 Mod(new Module(InputName, getGlobalContext())), Header(Header), |
| 46 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 55 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
| 47 GlobalVarPlaceHolderType(Type::getInt8Ty(getLLVMContext())) { | 56 NumFunctionBlocks(0), |
| 57 GlobalVarPlaceHolderType(Type::getInt8Ty(getLLVMContext())), | |
| 58 TypeConverter(getLLVMContext()) { | |
| 48 Mod->setDataLayout(PNaClDataLayout); | 59 Mod->setDataLayout(PNaClDataLayout); |
| 49 } | 60 } |
| 50 | 61 |
| 51 virtual ~TopLevelParser() {} | 62 virtual ~TopLevelParser() {} |
| 52 LLVM_OVERRIDE; | 63 LLVM_OVERRIDE; |
| 53 | 64 |
| 65 Ice::Translator &getTranslator() { | |
| 66 return Translator; | |
| 67 } | |
| 68 | |
| 54 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 69 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
| 55 ErrorStatus = true; | 70 ErrorStatus = true; |
| 56 ++NumErrors; | 71 ++NumErrors; |
| 57 return NaClBitcodeParser::Error(Message); | 72 return NaClBitcodeParser::Error(Message); |
| 58 } | 73 } |
| 59 | 74 |
| 60 /// Returns the number of errors found while parsing the bitcode | 75 /// Returns the number of errors found while parsing the bitcode |
| 61 /// file. | 76 /// file. |
| 62 unsigned getNumErrors() const { return NumErrors; } | 77 unsigned getNumErrors() const { return NumErrors; } |
| 63 | 78 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 ++NumFunctionIds; | 112 ++NumFunctionIds; |
| 98 ValueIDValues.push_back(Fcn); | 113 ValueIDValues.push_back(Fcn); |
| 99 } | 114 } |
| 100 | 115 |
| 101 /// Defines the next function ID as one that has an implementation | 116 /// Defines the next function ID as one that has an implementation |
| 102 /// (i.e a corresponding function block in the bitcode). | 117 /// (i.e a corresponding function block in the bitcode). |
| 103 void setNextValueIDAsImplementedFunction() { | 118 void setNextValueIDAsImplementedFunction() { |
| 104 DefiningFunctionsList.push_back(ValueIDValues.size()); | 119 DefiningFunctionsList.push_back(ValueIDValues.size()); |
| 105 } | 120 } |
| 106 | 121 |
| 122 /// Returns the value id that should be associated with the the | |
|
jvoung (off chromium)
2014/07/17 23:57:56
the the -> the
Karl
2014/07/18 20:27:43
Done.
| |
| 123 /// current function block. Increments internal counters during call | |
| 124 /// so that it will be in correct position for next function block. | |
| 125 unsigned getNextFunctionBlockValueID() { | |
| 126 if (DefiningFunctionsList.size() <= NumFunctionBlocks) | |
| 127 report_fatal_error("More function blocks than defined function addresses") ; | |
|
Jim Stichnoth
2014/07/17 23:00:33
80-char
Karl
2014/07/18 20:27:43
Done.
| |
| 128 return DefiningFunctionsList[NumFunctionBlocks++]; | |
| 129 } | |
| 130 | |
| 107 /// Returns the LLVM IR value associatd with the global value ID. | 131 /// Returns the LLVM IR value associatd with the global value ID. |
| 108 Value *getGlobalValueByID(unsigned ID) const { | 132 Value *getGlobalValueByID(unsigned ID) const { |
| 109 if (ID >= ValueIDValues.size()) | 133 if (ID >= ValueIDValues.size()) |
| 110 return 0; | 134 return 0; |
| 111 return ValueIDValues[ID]; | 135 return ValueIDValues[ID]; |
| 112 } | 136 } |
| 113 | 137 |
| 114 /// Returns the number of function addresses (i.e. ID's) defined in | 138 /// Returns the number of function addresses (i.e. ID's) defined in |
| 115 /// the bitcode file. | 139 /// the bitcode file. |
| 116 unsigned getNumFunctionIDs() const { return NumFunctionIds; } | 140 unsigned getNumFunctionIDs() const { return NumFunctionIds; } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 // If reached, there was a forward reference to this value. Replace it. | 179 // If reached, there was a forward reference to this value. Replace it. |
| 156 Value *PrevVal = OldV; | 180 Value *PrevVal = OldV; |
| 157 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); | 181 GlobalVariable *Placeholder = cast<GlobalVariable>(PrevVal); |
| 158 Placeholder->replaceAllUsesWith( | 182 Placeholder->replaceAllUsesWith( |
| 159 ConstantExpr::getBitCast(GV, Placeholder->getType())); | 183 ConstantExpr::getBitCast(GV, Placeholder->getType())); |
| 160 Placeholder->eraseFromParent(); | 184 Placeholder->eraseFromParent(); |
| 161 ValueIDValues[ID] = GV; | 185 ValueIDValues[ID] = GV; |
| 162 return true; | 186 return true; |
| 163 } | 187 } |
| 164 | 188 |
| 189 /// Returns the corresponding ICE type for LlvmTy. | |
| 190 Ice::Type convertToIceType(Type *LlvmTy) { | |
| 191 Ice::Type IceTy = TypeConverter.convertToIceType(LlvmTy); | |
| 192 if (IceTy == Ice::IceType_NUM) { | |
| 193 return convertToIceTypeError(LlvmTy); | |
| 194 } | |
| 195 return IceTy; | |
| 196 } | |
| 197 | |
| 198 /// Returns the corresponding LLVM type for IceTy. | |
| 199 Type *convertToLlvmType(Ice::Type IceTy) const { | |
| 200 return TypeConverter.convertToLlvmType(IceTy); | |
| 201 } | |
| 202 | |
| 203 /// Returns the LLVM integer type with the given number of Bits. If | |
| 204 /// Bits is not a valid PNaCl type, returns NULL. | |
| 205 Type *getLlvmIntegerType(unsigned Bits) const { | |
| 206 return TypeConverter.getLlvmIntegerType(Bits); | |
| 207 } | |
| 208 | |
| 209 /// Returns the LLVM vector with the given Size and Ty. If not a | |
| 210 /// valid PNaCl vector type, returns NULL. | |
| 211 Type *getLlvmVectorType(unsigned Size, Ice::Type Ty) const { | |
| 212 return TypeConverter.getLlvmVectorType(Size, Ty); | |
| 213 } | |
| 214 | |
| 165 private: | 215 private: |
| 216 // The translator associated with the parser. | |
| 217 Ice::Translator &Translator; | |
| 166 // The parsed module. | 218 // The parsed module. |
| 167 OwningPtr<Module> Mod; | 219 OwningPtr<Module> Mod; |
| 168 // The bitcode header. | 220 // The bitcode header. |
| 169 NaClBitcodeHeader &Header; | 221 NaClBitcodeHeader &Header; |
| 170 // The exit status that should be set to true if an error occurs. | 222 // The exit status that should be set to true if an error occurs. |
| 171 bool &ErrorStatus; | 223 bool &ErrorStatus; |
| 172 // The number of errors reported. | 224 // The number of errors reported. |
| 173 unsigned NumErrors; | 225 unsigned NumErrors; |
| 174 // The types associated with each type ID. | 226 // The types associated with each type ID. |
| 175 std::vector<Type *> TypeIDValues; | 227 std::vector<Type *> TypeIDValues; |
| 176 // The (global) value IDs. | 228 // The (global) value IDs. |
| 177 std::vector<WeakVH> ValueIDValues; | 229 std::vector<WeakVH> ValueIDValues; |
| 178 // The number of function IDs. | 230 // The number of function IDs. |
| 179 unsigned NumFunctionIds; | 231 unsigned NumFunctionIds; |
| 232 // The number of function blocks (processed so far). | |
| 233 unsigned NumFunctionBlocks; | |
| 180 // The list of value IDs (in the order found) of defining function | 234 // The list of value IDs (in the order found) of defining function |
| 181 // addresses. | 235 // addresses. |
| 182 std::vector<unsigned> DefiningFunctionsList; | 236 std::vector<unsigned> DefiningFunctionsList; |
| 183 // Cached global variable placeholder type. Used for all forward | 237 // Cached global variable placeholder type. Used for all forward |
| 184 // references to global variable addresses. | 238 // references to global variable addresses. |
| 185 Type *GlobalVarPlaceHolderType; | 239 Type *GlobalVarPlaceHolderType; |
| 240 // Converter between LLVM and ICE types. | |
| 241 Ice::TypeConverter TypeConverter; | |
| 186 | 242 |
| 187 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 243 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
| 188 | 244 |
| 189 /// Reports that type ID is undefined, and then returns | 245 /// Reports that type ID is undefined, and then returns |
| 190 /// the void type. | 246 /// the void type. |
| 191 Type *reportTypeIDAsUndefined(unsigned ID); | 247 Type *reportTypeIDAsUndefined(unsigned ID); |
| 192 | 248 |
| 193 /// Reports error about bad call to setTypeID. | 249 /// Reports error about bad call to setTypeID. |
| 194 void reportBadSetTypeID(unsigned ID, Type *Ty); | 250 void reportBadSetTypeID(unsigned ID, Type *Ty); |
| 251 | |
| 252 // Reports that there is no corresponding ICE type for LlvmTy. | |
| 253 // returns ICE::IceType_void. | |
| 254 Ice::Type convertToIceTypeError(Type *LlvmTy); | |
| 195 }; | 255 }; |
| 196 | 256 |
| 197 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { | 257 Type *TopLevelParser::reportTypeIDAsUndefined(unsigned ID) { |
| 198 std::string Buffer; | 258 std::string Buffer; |
| 199 raw_string_ostream StrBuf(Buffer); | 259 raw_string_ostream StrBuf(Buffer); |
| 200 StrBuf << "Can't find type for type id: " << ID; | 260 StrBuf << "Can't find type for type id: " << ID; |
| 201 Error(StrBuf.str()); | 261 Error(StrBuf.str()); |
| 202 Type *Ty = Type::getVoidTy(getLLVMContext()); | 262 Type *Ty = Type::getVoidTy(getLLVMContext()); |
| 203 // To reduce error messages, update type list if possible. | 263 // To reduce error messages, update type list if possible. |
| 204 if (ID < TypeIDValues.size()) | 264 if (ID < TypeIDValues.size()) |
| 205 TypeIDValues[ID] = Ty; | 265 TypeIDValues[ID] = Ty; |
| 206 return Ty; | 266 return Ty; |
| 207 } | 267 } |
| 208 | 268 |
| 209 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { | 269 void TopLevelParser::reportBadSetTypeID(unsigned ID, Type *Ty) { |
| 210 std::string Buffer; | 270 std::string Buffer; |
| 211 raw_string_ostream StrBuf(Buffer); | 271 raw_string_ostream StrBuf(Buffer); |
| 212 if (ID >= TypeIDValues.size()) { | 272 if (ID >= TypeIDValues.size()) { |
| 213 StrBuf << "Type index " << ID << " out of range: can't install."; | 273 StrBuf << "Type index " << ID << " out of range: can't install."; |
| 214 } else { | 274 } else { |
| 215 // Must be case that index already defined. | 275 // Must be case that index already defined. |
| 216 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] | 276 StrBuf << "Type index " << ID << " defined as " << *TypeIDValues[ID] |
| 217 << " and " << *Ty << "."; | 277 << " and " << *Ty << "."; |
| 218 } | 278 } |
| 219 Error(StrBuf.str()); | 279 Error(StrBuf.str()); |
| 220 } | 280 } |
| 221 | 281 |
| 282 Ice::Type TopLevelParser::convertToIceTypeError(Type *LlvmTy) { | |
| 283 std::string Buffer; | |
| 284 raw_string_ostream StrBuf(Buffer); | |
| 285 StrBuf << "Invalid LLVM type: " << *LlvmTy; | |
| 286 Error(StrBuf.str()); | |
| 287 return Ice::IceType_void; | |
| 288 } | |
| 289 | |
| 222 // Base class for parsing blocks within the bitcode file. Note: | 290 // Base class for parsing blocks within the bitcode file. Note: |
| 223 // Because this is the base class of block parsers, we generate error | 291 // Because this is the base class of block parsers, we generate error |
| 224 // messages if ParseBlock or ParseRecord is not overridden in derived | 292 // messages if ParseBlock or ParseRecord is not overridden in derived |
| 225 // classes. | 293 // classes. |
| 226 class BlockParserBaseClass : public NaClBitcodeParser { | 294 class BlockParserBaseClass : public NaClBitcodeParser { |
| 227 public: | 295 public: |
| 228 // Constructor for the top-level module block parser. | 296 // Constructor for the top-level module block parser. |
| 229 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) | 297 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) |
| 230 : NaClBitcodeParser(BlockID, Context), Context(Context) {} | 298 : NaClBitcodeParser(BlockID, Context), Context(Context) {} |
| 231 | 299 |
| 232 virtual ~BlockParserBaseClass() LLVM_OVERRIDE {} | 300 virtual ~BlockParserBaseClass() LLVM_OVERRIDE {} |
| 233 | 301 |
| 234 protected: | 302 protected: |
| 235 // The context parser that contains the decoded state. | 303 // The context parser that contains the decoded state. |
| 236 TopLevelParser *Context; | 304 TopLevelParser *Context; |
| 237 | 305 |
| 238 // Constructor for nested block parsers. | 306 // Constructor for nested block parsers. |
| 239 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 307 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 240 : NaClBitcodeParser(BlockID, EnclosingParser), | 308 : NaClBitcodeParser(BlockID, EnclosingParser), |
| 241 Context(EnclosingParser->Context) {} | 309 Context(EnclosingParser->Context) {} |
| 242 | 310 |
| 311 // Gets the translator associated with the bitcode parser. | |
| 312 Ice::Translator &getTranslator() { | |
| 313 return Context->getTranslator(); | |
| 314 } | |
| 315 | |
| 243 // Generates an error Message with the bit address prefixed to it. | 316 // Generates an error Message with the bit address prefixed to it. |
| 244 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 317 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
| 245 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; | 318 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; |
| 246 std::string Buffer; | 319 std::string Buffer; |
| 247 raw_string_ostream StrBuf(Buffer); | 320 raw_string_ostream StrBuf(Buffer); |
| 248 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), | 321 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), |
| 249 static_cast<unsigned>(Bit % 8)) << ") " << Message; | 322 static_cast<unsigned>(Bit % 8)) << ") " << Message; |
| 250 return Context->Error(StrBuf.str()); | 323 return Context->Error(StrBuf.str()); |
| 251 } | 324 } |
| 252 | 325 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 case naclbitc::TYPE_CODE_NUMENTRY: | 433 case naclbitc::TYPE_CODE_NUMENTRY: |
| 361 // NUMENTRY: [numentries] | 434 // NUMENTRY: [numentries] |
| 362 if (checkRecordSize(1, "Type count")) | 435 if (checkRecordSize(1, "Type count")) |
| 363 return; | 436 return; |
| 364 Context->resizeTypeIDValues(Values[0]); | 437 Context->resizeTypeIDValues(Values[0]); |
| 365 return; | 438 return; |
| 366 case naclbitc::TYPE_CODE_VOID: | 439 case naclbitc::TYPE_CODE_VOID: |
| 367 // VOID | 440 // VOID |
| 368 if (checkRecordSize(0, "Type void")) | 441 if (checkRecordSize(0, "Type void")) |
| 369 break; | 442 break; |
| 370 Ty = Type::getVoidTy(Context->getLLVMContext()); | 443 Ty = Context->convertToLlvmType(Ice::IceType_void); |
|
jvoung (off chromium)
2014/07/17 23:57:56
Is this mostly because it's cheaper to check the c
Karl
2014/07/18 20:27:43
What I was trying to do is, whenever possible, alw
| |
| 371 break; | 444 break; |
| 372 case naclbitc::TYPE_CODE_FLOAT: | 445 case naclbitc::TYPE_CODE_FLOAT: |
| 373 // FLOAT | 446 // FLOAT |
| 374 if (checkRecordSize(0, "Type float")) | 447 if (checkRecordSize(0, "Type float")) |
| 375 break; | 448 break; |
| 376 Ty = Type::getFloatTy(Context->getLLVMContext()); | 449 Ty = Context->convertToLlvmType(Ice::IceType_f32); |
| 377 break; | 450 break; |
| 378 case naclbitc::TYPE_CODE_DOUBLE: | 451 case naclbitc::TYPE_CODE_DOUBLE: |
| 379 // DOUBLE | 452 // DOUBLE |
| 380 if (checkRecordSize(0, "Type double")) | 453 if (checkRecordSize(0, "Type double")) |
| 381 break; | 454 break; |
| 382 Ty = Type::getDoubleTy(Context->getLLVMContext()); | 455 Ty = Context->convertToLlvmType(Ice::IceType_f64); |
| 383 break; | 456 break; |
| 384 case naclbitc::TYPE_CODE_INTEGER: | 457 case naclbitc::TYPE_CODE_INTEGER: |
| 385 // INTEGER: [width] | 458 // INTEGER: [width] |
| 386 if (checkRecordSize(1, "Type integer")) | 459 if (checkRecordSize(1, "Type integer")) |
| 387 break; | 460 break; |
| 388 Ty = IntegerType::get(Context->getLLVMContext(), Values[0]); | 461 Ty = Context->getLlvmIntegerType(Values[0]); |
| 389 // TODO(kschimpf) Check if size is legal. | 462 if (Ty == NULL) { |
| 463 std::string Buffer; | |
| 464 raw_string_ostream StrBuf(Buffer); | |
| 465 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; | |
| 466 Error(StrBuf.str()); | |
| 467 // Fix type so that we can continue. | |
| 468 Ty = Context->convertToLlvmType(Ice::IceType_i32); | |
| 469 } | |
| 390 break; | 470 break; |
| 391 case naclbitc::TYPE_CODE_VECTOR: | 471 case naclbitc::TYPE_CODE_VECTOR: { |
| 392 // VECTOR: [numelts, eltty] | 472 // VECTOR: [numelts, eltty] |
| 393 if (checkRecordSize(2, "Type vector")) | 473 if (checkRecordSize(2, "Type vector")) |
| 394 break; | 474 break; |
| 395 Ty = VectorType::get(Context->getTypeByID(Values[1]), Values[0]); | 475 Type *BaseTy = Context->getTypeByID(Values[1]); |
| 476 Ty = Context->getLlvmVectorType(Values[0], | |
| 477 Context->convertToIceType(BaseTy)); | |
| 478 if (Ty == NULL) { | |
| 479 std::string Buffer; | |
| 480 raw_string_ostream StrBuf(Buffer); | |
| 481 StrBuf << "Invalid type vector record: <" << Values[0] | |
| 482 << " x " << *BaseTy << ">"; | |
| 483 Error(StrBuf.str()); | |
| 484 Ty = Context->convertToLlvmType(Ice::IceType_void); | |
| 485 } | |
| 396 break; | 486 break; |
| 487 } | |
| 397 case naclbitc::TYPE_CODE_FUNCTION: { | 488 case naclbitc::TYPE_CODE_FUNCTION: { |
| 398 // FUNCTION: [vararg, retty, paramty x N] | 489 // FUNCTION: [vararg, retty, paramty x N] |
| 399 if (checkRecordSizeAtLeast(2, "Type signature")) | 490 if (checkRecordSizeAtLeast(2, "Type signature")) |
| 400 break; | 491 break; |
| 401 SmallVector<Type *, 8> ArgTys; | 492 SmallVector<Type *, 8> ArgTys; |
| 402 for (unsigned i = 2, e = Values.size(); i != e; ++i) { | 493 for (unsigned i = 2, e = Values.size(); i != e; ++i) { |
| 403 ArgTys.push_back(Context->getTypeByID(Values[i])); | 494 ArgTys.push_back(Context->getTypeByID(Values[i])); |
| 404 } | 495 } |
| 405 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); | 496 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); |
| 406 break; | 497 break; |
| 407 } | 498 } |
| 408 default: | 499 default: |
| 409 BlockParserBaseClass::ProcessRecord(); | 500 BlockParserBaseClass::ProcessRecord(); |
| 410 break; | 501 break; |
| 411 } | 502 } |
| 412 // If Ty not defined, assume error. Use void as filler. | 503 // If Ty not defined, assume error. Use void as filler. |
| 413 if (Ty == NULL) | 504 if (Ty == NULL) |
| 414 Ty = Type::getVoidTy(Context->getLLVMContext()); | 505 Ty = Type::getVoidTy(Context->getLLVMContext()); |
|
jvoung (off chromium)
2014/07/17 23:57:56
E.g., how come this isn't using the converter?
Karl
2014/07/18 20:27:43
Fixed.
| |
| 415 Context->setTypeID(NextTypeId++, Ty); | 506 Context->setTypeID(NextTypeId++, Ty); |
| 416 } | 507 } |
| 417 | 508 |
| 418 /// Parses the globals block (i.e. global variables). | 509 /// Parses the globals block (i.e. global variables). |
| 419 class GlobalsParser : public BlockParserBaseClass { | 510 class GlobalsParser : public BlockParserBaseClass { |
| 420 public: | 511 public: |
| 421 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 512 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 422 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), | 513 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), |
| 423 Alignment(1), IsConstant(false) { | 514 Alignment(1), IsConstant(false) { |
| 424 NextGlobalID = Context->getNumFunctionIDs(); | 515 NextGlobalID = Context->getNumFunctionIDs(); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 break; | 769 break; |
| 679 } | 770 } |
| 680 default: | 771 default: |
| 681 break; | 772 break; |
| 682 } | 773 } |
| 683 // If reached, don't know how to handle record. | 774 // If reached, don't know how to handle record. |
| 684 BlockParserBaseClass::ProcessRecord(); | 775 BlockParserBaseClass::ProcessRecord(); |
| 685 return; | 776 return; |
| 686 } | 777 } |
| 687 | 778 |
| 779 /// Parses function blocks in the bitcode file. | |
| 780 class FunctionParser : public BlockParserBaseClass { | |
| 781 public: | |
| 782 FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | |
| 783 : BlockParserBaseClass(BlockID, EnclosingParser), | |
| 784 Func(new Ice::Cfg(getTranslator().getContext())), | |
| 785 CurrentBbIndex(0), | |
| 786 FcnId(Context->getNextFunctionBlockValueID()), | |
| 787 LlvmFunc(cast<Function>(Context->getGlobalValueByID(FcnId))), | |
| 788 CachedNumGlobalValueIDs(Context->getNumGlobalValueIDs()), | |
| 789 InstIsTerminating(false) | |
| 790 { | |
| 791 Func->setFunctionName(LlvmFunc->getName()); | |
| 792 Func->setReturnType(Context->convertToIceType(LlvmFunc->getReturnType())); | |
| 793 Func->setInternal(LlvmFunc->hasInternalLinkage()); | |
| 794 CurrentNode = InstallNextBasicBlock(); | |
| 795 for (Function::const_arg_iterator ArgI = LlvmFunc->arg_begin(), | |
| 796 ArgE = LlvmFunc->arg_end(); | |
| 797 ArgI != ArgE; ++ArgI) { | |
| 798 Func->addArg(NextInstVar(Context->convertToIceType(ArgI->getType()))); | |
| 799 } | |
| 800 } | |
| 801 | |
| 802 ~FunctionParser() LLVM_OVERRIDE; | |
| 803 | |
| 804 private: | |
| 805 // Timer for reading function bitcode and converting to ICE. | |
| 806 Ice::Timer TConvert; | |
| 807 // The corresponding ICE function defined by the function block. | |
| 808 Ice::Cfg *Func; | |
| 809 // The index to the current basic block being built. | |
| 810 uint32_t CurrentBbIndex; | |
| 811 // The basic block being built. | |
| 812 Ice::CfgNode *CurrentNode; | |
| 813 // The ID for the function. | |
| 814 unsigned FcnId; | |
| 815 // The corresponding llvm function. | |
| 816 Function *LlvmFunc; | |
| 817 // Holds variables local to the function block. | |
| 818 std::vector<Ice::Operand *> LocalVars; | |
| 819 // Holds the list of basic blocks defined for the function. | |
| 820 std::vector<Ice::CfgNode*> Nodes; | |
| 821 // Holds the dividing point between local and global absolute value indices. | |
| 822 uint32_t CachedNumGlobalValueIDs; | |
| 823 // True if the last processed instruction was a terminating | |
| 824 // instruction. | |
| 825 bool InstIsTerminating; | |
| 826 | |
| 827 virtual void ProcessRecord() LLVM_OVERRIDE; | |
| 828 | |
| 829 // Creates and appends a new basic block to the list of basic blocks. | |
| 830 Ice::CfgNode *InstallNextBasicBlock() { | |
| 831 std::string Buffer; | |
| 832 raw_string_ostream StrBuf(Buffer); | |
| 833 // TODO(kschimpf): Make (basic block) nodes make up the name if needed. | |
| 834 StrBuf << "bb" << Nodes.size(); | |
| 835 Ice::CfgNode *Node = Func->makeNode(StrBuf.str()); | |
| 836 Nodes.push_back(Node); | |
| 837 return Node; | |
| 838 } | |
| 839 | |
| 840 // Returns the Index-th basic block in the list of basic blocks. | |
| 841 Ice::CfgNode *GetBasicBlock(uint32_t Index) { | |
| 842 if (Index >= Nodes.size()) { | |
| 843 std::string Buffer; | |
| 844 raw_string_ostream StrBuf(Buffer); | |
| 845 StrBuf << "Reference to basic block " << Index | |
| 846 << " not found. Must be less than " << Nodes.size(); | |
| 847 Error(StrBuf.str()); | |
| 848 Index = 0; | |
| 849 } | |
| 850 return Nodes[Index]; | |
| 851 } | |
| 852 | |
| 853 // Generates the next available local variable using the given | |
| 854 // type. Note: if Ty is void, this function returns NULL. | |
| 855 Ice::Variable *NextInstVar(Ice::Type Ty) { | |
| 856 if (Ty == Ice::IceType_void) | |
| 857 return NULL; | |
| 858 Ice::Variable *Var = Func->makeVariable(Ty, CurrentNode); | |
| 859 LocalVars.push_back(Var); | |
| 860 return Var; | |
| 861 } | |
| 862 | |
| 863 // Converts a relative index (to the next instruction to be read) to | |
| 864 // an absolute value index. | |
| 865 uint32_t convertRelativeToAbsIndex(int32_t Id) { | |
| 866 int32_t AbsNextId = CachedNumGlobalValueIDs + LocalVars.size(); | |
| 867 if (Id > 0 && AbsNextId < static_cast<uint32_t>(Id)) { | |
| 868 std::string Buffer; | |
| 869 raw_string_ostream StrBuf(Buffer); | |
| 870 StrBuf << "Invalid relative value id: " << Id | |
| 871 << " (must be <= " << AbsNextId << ")"; | |
| 872 Error(StrBuf.str()); | |
| 873 // Fix so that we can go on. | |
|
jvoung (off chromium)
2014/07/17 23:57:56
Hmm, while this is still a bit early, what should
Karl
2014/07/18 20:27:43
The goal of error recovery is to make it easier to
| |
| 874 return 0; | |
| 875 } | |
| 876 return AbsNextId - Id; | |
| 877 } | |
| 878 | |
| 879 // Returns the value referenced by the given value Index. | |
| 880 Ice::Operand *getOperand(uint32_t Index) { | |
| 881 if (Index < CachedNumGlobalValueIDs) { | |
| 882 // TODO(kschimpf): Define implementation. | |
| 883 report_fatal_error("getOperand of global addresses not implemented"); | |
| 884 } | |
| 885 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; | |
| 886 if (LocalIndex >= LocalVars.size()) { | |
| 887 std::string Buffer; | |
| 888 raw_string_ostream StrBuf(Buffer); | |
| 889 StrBuf << "Value index " << Index << " out of range. Must be less than " | |
| 890 << (LocalVars.size() + CachedNumGlobalValueIDs); | |
| 891 // TODO(kschimpf): Fix to recover. | |
| 892 report_fatal_error(StrBuf.str()); | |
| 893 } | |
| 894 return LocalVars[LocalIndex]; | |
| 895 } | |
| 896 | |
| 897 // Returns true if LLVM float/double type. | |
| 898 bool isFloatingType(Type *Ty) { | |
| 899 return Ty->isFloatTy() || Ty->isDoubleTy(); | |
| 900 } | |
| 901 | |
| 902 // Checks if integer arithmetic Op, for type OpTy, is valid. | |
| 903 // Returns false if valid. Otherwise generates error message and | |
| 904 // returns true. | |
| 905 bool checkIfIntArithmeticOp(Ice::InstArithmetic::OpKind Op, Type *OpTy) { | |
|
jvoung (off chromium)
2014/07/17 23:57:55
Some of these functions take LLVM types. At some p
Karl
2014/07/18 20:27:43
That would be ideal. However, I'm not sure how we
| |
| 906 if (!PNaClABITypeChecker::isValidIntArithmeticType(OpTy)) { | |
| 907 std::string Buffer; | |
| 908 raw_string_ostream StrBuf(Buffer); | |
| 909 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 910 << ": Invalid integer arithmetic type: " << *OpTy; | |
| 911 return Error(StrBuf.str()); | |
| 912 } | |
| 913 return false; | |
| 914 } | |
| 915 | |
| 916 // Checks if integer (or vector of integers) arithmetic Op, for type | |
| 917 // OpTy, is valid. Returns false if valid. Otherwise generates | |
| 918 // error message and returns true. | |
| 919 bool checkIfIntegerOrVectorOp(Ice::InstArithmetic::OpKind Op, Type *OpTy) { | |
| 920 Type *BaseTy = OpTy; | |
| 921 if (OpTy->isVectorTy()) { | |
| 922 if (!PNaClABITypeChecker::isValidVectorType(OpTy)) { | |
| 923 std::string Buffer; | |
| 924 raw_string_ostream StrBuf(Buffer); | |
| 925 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 926 << ": invalid vector type: " << *OpTy; | |
| 927 return Error(StrBuf.str()); | |
| 928 } | |
| 929 BaseTy = OpTy->getVectorElementType(); | |
| 930 } | |
| 931 if (BaseTy->isIntegerTy()) { | |
| 932 if (PNaClABITypeChecker::isValidScalarType(BaseTy)) return false; | |
| 933 std::string Buffer; | |
| 934 raw_string_ostream StrBuf(Buffer); | |
| 935 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 936 << ": Invalid integer type: " << *OpTy; | |
| 937 return Error(StrBuf.str()); | |
| 938 } else { | |
| 939 std::string Buffer; | |
| 940 raw_string_ostream StrBuf(Buffer); | |
| 941 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 942 << ": Expects integer type. Found: " << *OpTy; | |
| 943 return Error(StrBuf.str()); | |
| 944 } | |
| 945 return false; | |
| 946 } | |
| 947 | |
| 948 // Checks if floating arithmetic Op, for type OpTy, is valid. | |
| 949 // Returns false if valid. Otherwise generates an error message and | |
| 950 // returns true. | |
| 951 bool checkIfFloatingOrVectorOp(Ice::InstArithmetic::OpKind Op, Type *OpTy) { | |
| 952 Type *BaseTy = OpTy; | |
| 953 if (OpTy->isVectorTy()) { | |
| 954 if (!PNaClABITypeChecker::isValidVectorType(OpTy)) { | |
| 955 std::string Buffer; | |
| 956 raw_string_ostream StrBuf(Buffer); | |
| 957 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 958 << ": invalid vector type: " << *OpTy; | |
| 959 return Error(StrBuf.str()); | |
| 960 } | |
| 961 BaseTy = OpTy->getVectorElementType(); | |
| 962 } | |
| 963 if (!isFloatingType(BaseTy)) { | |
| 964 std::string Buffer; | |
| 965 raw_string_ostream StrBuf(Buffer); | |
| 966 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 967 << ": Expects floating point. Found " << *OpTy; | |
| 968 return Error(StrBuf.str()); | |
| 969 } | |
| 970 if (!PNaClABITypeChecker::isValidScalarType(BaseTy)) { | |
| 971 std::string Buffer; | |
| 972 raw_string_ostream StrBuf(Buffer); | |
| 973 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 974 << ": type not allowed: " << *OpTy; | |
| 975 return Error(StrBuf.str()); | |
| 976 } | |
| 977 return false; | |
| 978 } | |
| 979 | |
| 980 /// Takes the PNaCl bitcode binary operator Opcode, and the opcode | |
| 981 /// type Ty, and sets Op to the corresponding ICE binary | |
| 982 /// opcode. Returns false if able to convert, true otherwise. | |
| 983 bool convertBinopOpcode(unsigned Opcode, | |
| 984 Ice::Type Ty, | |
| 985 Ice::InstArithmetic::OpKind &Op) { | |
| 986 Instruction::BinaryOps LLVMOpcode; | |
| 987 Type *LlvmTy = Context->convertToLlvmType(Ty); | |
| 988 if (!naclbitc::DecodeBinaryOpcode(Opcode, LlvmTy, LLVMOpcode)) { | |
| 989 std::string Buffer; | |
| 990 raw_string_ostream StrBuf(Buffer); | |
| 991 StrBuf << "Binary opcode not understood: " << Opcode; | |
| 992 Error(StrBuf.str()); | |
| 993 Op = Ice::InstArithmetic::Add; | |
| 994 return true; | |
| 995 } | |
| 996 Op = Ice::InstArithmetic::Add; | |
| 997 switch (LLVMOpcode) { | |
| 998 default: { | |
| 999 std::string Buffer; | |
| 1000 raw_string_ostream StrBuf(Buffer); | |
| 1001 StrBuf << "Binary opcode not understood: " << Opcode; | |
| 1002 Error(StrBuf.str()); | |
| 1003 return true; | |
| 1004 } | |
| 1005 case Instruction::Add: | |
| 1006 Op = Ice::InstArithmetic::Add; | |
| 1007 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1008 case Instruction::FAdd: | |
| 1009 Op = Ice::InstArithmetic::Fadd; | |
| 1010 return checkIfFloatingOrVectorOp(Op, LlvmTy); | |
| 1011 case Instruction::Sub: | |
| 1012 Op = Ice::InstArithmetic::Sub; | |
| 1013 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1014 case Instruction::FSub: | |
| 1015 Op = Ice::InstArithmetic::Fsub; | |
| 1016 return checkIfFloatingOrVectorOp(Op, LlvmTy); | |
| 1017 case Instruction::Mul: | |
| 1018 Op = Ice::InstArithmetic::Mul; | |
| 1019 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1020 case Instruction::FMul: | |
| 1021 Op = Ice::InstArithmetic::Fmul; | |
| 1022 return checkIfFloatingOrVectorOp(Op, LlvmTy); | |
| 1023 case Instruction::UDiv: | |
| 1024 Op = Ice::InstArithmetic::Udiv; | |
| 1025 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1026 case Instruction::SDiv: | |
| 1027 Op = Ice::InstArithmetic::Sdiv; | |
| 1028 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1029 case Instruction::FDiv: | |
| 1030 Op = Ice::InstArithmetic::Fdiv; | |
| 1031 return checkIfFloatingOrVectorOp(Op, LlvmTy); | |
| 1032 case Instruction::URem: | |
| 1033 Op = Ice::InstArithmetic::Urem; | |
| 1034 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1035 case Instruction::SRem: | |
| 1036 Op = Ice::InstArithmetic::Srem; | |
| 1037 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1038 case Instruction::FRem: | |
| 1039 Op = Ice::InstArithmetic::Frem; | |
| 1040 return checkIfFloatingOrVectorOp(Op, LlvmTy); | |
| 1041 case Instruction::Shl: | |
| 1042 Op = Ice::InstArithmetic::Shl; | |
| 1043 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1044 case Instruction::LShr: | |
| 1045 Op = Ice::InstArithmetic::Lshr; | |
| 1046 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1047 case Instruction::AShr: | |
| 1048 Op = Ice::InstArithmetic::Ashr; | |
| 1049 return checkIfIntArithmeticOp(Op, LlvmTy); | |
| 1050 case Instruction::And: | |
| 1051 Op = Ice::InstArithmetic::And; | |
| 1052 return checkIfIntegerOrVectorOp(Op, LlvmTy); | |
| 1053 case Instruction::Or: | |
| 1054 Op = Ice::InstArithmetic::Or; | |
| 1055 return checkIfIntegerOrVectorOp(Op, LlvmTy); | |
| 1056 case Instruction::Xor: | |
| 1057 Op = Ice::InstArithmetic::Or; | |
|
jvoung (off chromium)
2014/07/17 23:57:56
Xor
Karl
2014/07/18 20:27:43
Done.
| |
| 1058 return checkIfIntegerOrVectorOp(Op, LlvmTy); | |
| 1059 } | |
| 1060 } | |
| 1061 }; | |
| 1062 | |
| 1063 FunctionParser::~FunctionParser() { | |
| 1064 if (getTranslator().getFlags().SubzeroTimingEnabled) { | |
| 1065 errs() << "[Subzero timing] Convert function " | |
| 1066 << Func->getFunctionName() << ": " << TConvert.getElapsedSec() | |
| 1067 << " sec\n"; | |
| 1068 } | |
| 1069 // Before translating, check for blocks without instructions, and | |
| 1070 // insert unreachable. This shouldn't happen, but be safe. | |
| 1071 unsigned Index = 0; | |
| 1072 for (std::vector<Ice::CfgNode*>::iterator | |
| 1073 Iter = Nodes.begin(), IterEnd = Nodes.end(); | |
| 1074 Iter != IterEnd; ++Iter, ++Index) { | |
| 1075 Ice::CfgNode *Node = *Iter; | |
| 1076 if (Node->getInsts().size() == 0) { | |
| 1077 std::string Buffer; | |
| 1078 raw_string_ostream StrBuf(Buffer); | |
| 1079 StrBuf << "Basic block " << Index << " contains no instructions"; | |
| 1080 Error(StrBuf.str()); | |
| 1081 Node->appendInst(Ice::InstUnreachable::create(Func)); | |
| 1082 } | |
| 1083 } | |
| 1084 getTranslator().translateFcn(Func); | |
| 1085 } | |
| 1086 | |
| 1087 void FunctionParser::ProcessRecord() { | |
| 1088 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | |
| 1089 if (InstIsTerminating) { | |
| 1090 InstIsTerminating = false; | |
| 1091 CurrentNode = GetBasicBlock(++CurrentBbIndex); | |
| 1092 } | |
| 1093 Ice::Inst *Inst = NULL; | |
| 1094 switch (Record.GetCode()) { | |
| 1095 case naclbitc::FUNC_CODE_DECLAREBLOCKS: { | |
| 1096 // DECLAREBLOCKS: [n] | |
| 1097 if (checkRecordSize(1, "function block count")) | |
| 1098 break; | |
| 1099 if (Nodes.size() != 1) { | |
| 1100 Error("Duplicate function block count record"); | |
| 1101 return; | |
| 1102 } | |
| 1103 uint32_t NumBbs = Values[0]; | |
| 1104 if (NumBbs == 0) { | |
| 1105 Error("Functions must contain at least one basic block."); | |
| 1106 NumBbs = 1; | |
| 1107 } | |
| 1108 // Install the basic blocks, skipping bb0 which was created in the | |
| 1109 // constructor. | |
| 1110 for (size_t i = 1; i < NumBbs; ++i) | |
| 1111 InstallNextBasicBlock(); | |
| 1112 break; | |
| 1113 } | |
| 1114 case naclbitc::FUNC_CODE_INST_BINOP: { | |
| 1115 // BINOP: [opval, opval, opcode] | |
| 1116 if (checkRecordSize(3, "function block binop")) | |
| 1117 break; | |
| 1118 Ice::Operand *Op1 = getOperand(convertRelativeToAbsIndex(Values[0])); | |
| 1119 Ice::Operand *Op2 = getOperand(convertRelativeToAbsIndex(Values[1])); | |
| 1120 Ice::Type Type1 = Op1->getType(); | |
| 1121 Ice::Type Type2 = Op2->getType(); | |
| 1122 if (Type1 != Type2) { | |
| 1123 std::string Buffer; | |
| 1124 raw_string_ostream StrBuf(Buffer); | |
| 1125 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; | |
| 1126 Error(StrBuf.str()); | |
| 1127 // Fix so that we can continue. | |
| 1128 Op2 = Op1; | |
| 1129 } | |
| 1130 | |
| 1131 Ice::InstArithmetic::OpKind Opcode; | |
| 1132 if (convertBinopOpcode(Values[2], Type1, Opcode)) | |
| 1133 break; | |
| 1134 Ice::Variable *Dest = NextInstVar(Type1); | |
| 1135 Inst = Ice::InstArithmetic::create(Func, Opcode, Dest, Op1, Op2); | |
| 1136 break; | |
| 1137 } | |
| 1138 case naclbitc::FUNC_CODE_INST_RET: { | |
| 1139 // RET: [opval?] | |
| 1140 InstIsTerminating = true; | |
| 1141 if (checkRecordSizeInRange(0, 1, "function block ret")) | |
| 1142 break; | |
| 1143 if (Values.size() == 0) { | |
| 1144 Inst = Ice::InstRet::create(Func); | |
| 1145 } else { | |
| 1146 Inst = Ice::InstRet::create( | |
| 1147 Func, getOperand(convertRelativeToAbsIndex(Values[0]))); | |
| 1148 } | |
| 1149 break; | |
| 1150 } | |
| 1151 default: | |
| 1152 // Generate error message! | |
| 1153 BlockParserBaseClass::ProcessRecord(); | |
| 1154 break; | |
| 1155 } | |
| 1156 if (Inst) | |
| 1157 CurrentNode->appendInst(Inst); | |
| 1158 } | |
| 1159 | |
| 688 /// Parses the module block in the bitcode file. | 1160 /// Parses the module block in the bitcode file. |
| 689 class ModuleParser : public BlockParserBaseClass { | 1161 class ModuleParser : public BlockParserBaseClass { |
| 690 public: | 1162 public: |
| 691 ModuleParser(unsigned BlockID, TopLevelParser *Context) | 1163 ModuleParser(unsigned BlockID, TopLevelParser *Context) |
| 692 : BlockParserBaseClass(BlockID, Context) {} | 1164 : BlockParserBaseClass(BlockID, Context) {} |
| 693 | 1165 |
| 694 virtual ~ModuleParser() LLVM_OVERRIDE {} | 1166 virtual ~ModuleParser() LLVM_OVERRIDE {} |
| 695 | 1167 |
| 696 protected: | 1168 protected: |
| 697 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 1169 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 709 } | 1181 } |
| 710 case naclbitc::GLOBALVAR_BLOCK_ID: { | 1182 case naclbitc::GLOBALVAR_BLOCK_ID: { |
| 711 GlobalsParser Parser(BlockID, this); | 1183 GlobalsParser Parser(BlockID, this); |
| 712 return Parser.ParseThisBlock(); | 1184 return Parser.ParseThisBlock(); |
| 713 } | 1185 } |
| 714 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { | 1186 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
| 715 ValuesymtabParser Parser(BlockID, this, false); | 1187 ValuesymtabParser Parser(BlockID, this, false); |
| 716 return Parser.ParseThisBlock(); | 1188 return Parser.ParseThisBlock(); |
| 717 } | 1189 } |
| 718 case naclbitc::FUNCTION_BLOCK_ID: { | 1190 case naclbitc::FUNCTION_BLOCK_ID: { |
| 719 Error("Function block parser not yet implemented, skipping"); | 1191 FunctionParser Parser(BlockID, this); |
| 720 SkipBlock(); | 1192 return Parser.ParseThisBlock(); |
| 721 return false; | |
| 722 } | 1193 } |
| 723 default: | 1194 default: |
| 724 return BlockParserBaseClass::ParseBlock(BlockID); | 1195 return BlockParserBaseClass::ParseBlock(BlockID); |
| 725 } | 1196 } |
| 726 } | 1197 } |
| 727 | 1198 |
| 728 void ModuleParser::ProcessRecord() { | 1199 void ModuleParser::ProcessRecord() { |
| 729 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 1200 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 730 switch (Record.GetCode()) { | 1201 switch (Record.GetCode()) { |
| 731 case naclbitc::MODULE_CODE_VERSION: { | 1202 case naclbitc::MODULE_CODE_VERSION: { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 } | 1252 } |
| 782 default: | 1253 default: |
| 783 BlockParserBaseClass::ProcessRecord(); | 1254 BlockParserBaseClass::ProcessRecord(); |
| 784 return; | 1255 return; |
| 785 } | 1256 } |
| 786 } | 1257 } |
| 787 | 1258 |
| 788 bool TopLevelParser::ParseBlock(unsigned BlockID) { | 1259 bool TopLevelParser::ParseBlock(unsigned BlockID) { |
| 789 if (BlockID == naclbitc::MODULE_BLOCK_ID) { | 1260 if (BlockID == naclbitc::MODULE_BLOCK_ID) { |
| 790 ModuleParser Parser(BlockID, this); | 1261 ModuleParser Parser(BlockID, this); |
| 791 bool ReturnValue = Parser.ParseThisBlock(); | 1262 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 } | 1263 } |
| 799 // Generate error message by using default block implementation. | 1264 // Generate error message by using default block implementation. |
| 800 BlockParserBaseClass Parser(BlockID, this); | 1265 BlockParserBaseClass Parser(BlockID, this); |
| 801 return Parser.ParseThisBlock(); | 1266 return Parser.ParseThisBlock(); |
| 802 } | 1267 } |
| 803 | 1268 |
| 804 } // end of anonymous namespace. | 1269 } // end of anonymous namespace. |
| 805 | 1270 |
| 806 namespace Ice { | 1271 namespace Ice { |
| 807 | 1272 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 829 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 1294 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
| 830 errs() << "Invalid PNaCl bitcode header.\n"; | 1295 errs() << "Invalid PNaCl bitcode header.\n"; |
| 831 ErrorStatus = true; | 1296 ErrorStatus = true; |
| 832 return; | 1297 return; |
| 833 } | 1298 } |
| 834 | 1299 |
| 835 // Create a bitstream reader to read the bitcode file. | 1300 // Create a bitstream reader to read the bitcode file. |
| 836 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 1301 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
| 837 NaClBitstreamCursor InputStream(InputStreamFile); | 1302 NaClBitstreamCursor InputStream(InputStreamFile); |
| 838 | 1303 |
| 839 TopLevelParser Parser(MemBuf->getBufferIdentifier(), Header, InputStream, | 1304 TopLevelParser Parser(*this, MemBuf->getBufferIdentifier(), Header, |
| 840 ErrorStatus); | 1305 InputStream, ErrorStatus); |
| 841 int TopLevelBlocks = 0; | 1306 int TopLevelBlocks = 0; |
| 842 while (!InputStream.AtEndOfStream()) { | 1307 while (!InputStream.AtEndOfStream()) { |
| 843 if (Parser.Parse()) { | 1308 if (Parser.Parse()) { |
| 844 ErrorStatus = true; | 1309 ErrorStatus = true; |
| 845 return; | 1310 return; |
| 846 } | 1311 } |
| 847 ++TopLevelBlocks; | 1312 ++TopLevelBlocks; |
| 848 } | 1313 } |
| 849 | 1314 |
| 850 if (TopLevelBlocks != 1) { | 1315 if (TopLevelBlocks != 1) { |
| 851 errs() << IRFilename | 1316 errs() << IRFilename |
| 852 << ": Contains more than one module. Found: " << TopLevelBlocks | 1317 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 853 << "\n"; | 1318 << "\n"; |
| 854 ErrorStatus = true; | 1319 ErrorStatus = true; |
| 855 } | 1320 } |
| 856 return; | 1321 return; |
| 857 } | 1322 } |
| 858 | 1323 |
| 859 } // end of anonymous namespace. | 1324 } // end of anonymous namespace. |
| OLD | NEW |