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