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