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> AllowErrorRecovery( | |
| 45 "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, |
| 56 const std::string &InputName, NaClBitcodeHeader &Header, | |
| 43 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 57 NaClBitstreamCursor &Cursor, bool &ErrorStatus) |
| 44 : NaClBitcodeParser(Cursor), | 58 : NaClBitcodeParser(Cursor), |
| 59 Translator(Translator), | |
| 45 Mod(new Module(InputName, getGlobalContext())), Header(Header), | 60 Mod(new Module(InputName, getGlobalContext())), Header(Header), |
| 61 TypeConverter(getLLVMContext()), | |
| 46 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 62 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
| 47 GlobalVarPlaceHolderType(Type::getInt8Ty(getLLVMContext())) { | 63 NumFunctionBlocks(0), |
| 64 GlobalVarPlaceHolderType(convertToLLVMType(Ice::IceType_i8)) { | |
| 48 Mod->setDataLayout(PNaClDataLayout); | 65 Mod->setDataLayout(PNaClDataLayout); |
| 49 } | 66 } |
| 50 | 67 |
| 51 virtual ~TopLevelParser() {} | 68 virtual ~TopLevelParser() {} |
| 52 LLVM_OVERRIDE; | 69 LLVM_OVERRIDE; |
| 53 | 70 |
| 71 Ice::Translator &getTranslator() { return Translator; } | |
| 72 | |
| 54 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 73 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
|
Jim Stichnoth
2014/07/21 22:25:22
Should be error() not Error() according to coding
Karl
2014/07/23 20:22:20
This is an inherited method, and must follow the c
| |
| 55 ErrorStatus = true; | 74 ErrorStatus = true; |
| 56 ++NumErrors; | 75 ++NumErrors; |
| 57 return NaClBitcodeParser::Error(Message); | 76 bool ReturnValue = NaClBitcodeParser::Error(Message); |
| 77 if (!AllowErrorRecovery) report_fatal_error("Unable to continue"); | |
|
Jim Stichnoth
2014/07/21 22:25:21
I don't like "if (cond) stmt;" on a single line, a
Karl
2014/07/23 20:22:21
Fixed by running through clang-format.
| |
| 78 return ReturnValue; | |
| 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 (DefiningFunctionsList.size() <= NumFunctionBlocks) | |
|
jvoung (off chromium)
2014/07/23 16:10:17
nit: flipping the comparison and using >= seems to
Karl
2014/07/23 20:22:21
Done.
| |
| 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() { | |
| 326 return Context->getTranslator(); | |
| 327 } | |
| 328 | |
| 243 // Generates an error Message with the bit address prefixed to it. | 329 // Generates an error Message with the bit address prefixed to it. |
| 244 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { | 330 virtual bool Error(const std::string &Message) LLVM_OVERRIDE { |
| 245 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; | 331 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; |
| 246 std::string Buffer; | 332 std::string Buffer; |
| 247 raw_string_ostream StrBuf(Buffer); | 333 raw_string_ostream StrBuf(Buffer); |
| 248 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), | 334 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), |
| 249 static_cast<unsigned>(Bit % 8)) << ") " << Message; | 335 static_cast<unsigned>(Bit % 8)) << ") " << Message; |
| 250 return Context->Error(StrBuf.str()); | 336 return Context->Error(StrBuf.str()); |
| 251 } | 337 } |
| 252 | 338 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 404 } |
| 319 }; | 405 }; |
| 320 | 406 |
| 321 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { | 407 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { |
| 322 // If called, derived class doesn't know how to handle block. | 408 // If called, derived class doesn't know how to handle block. |
| 323 // Report error and skip. | 409 // Report error and skip. |
| 324 std::string Buffer; | 410 std::string Buffer; |
| 325 raw_string_ostream StrBuf(Buffer); | 411 raw_string_ostream StrBuf(Buffer); |
| 326 StrBuf << "Don't know how to parse block id: " << BlockID; | 412 StrBuf << "Don't know how to parse block id: " << BlockID; |
| 327 Error(StrBuf.str()); | 413 Error(StrBuf.str()); |
| 414 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 328 SkipBlock(); | 415 SkipBlock(); |
| 329 return false; | 416 return false; |
| 330 } | 417 } |
| 331 | 418 |
| 332 void BlockParserBaseClass::ProcessRecord() { | 419 void BlockParserBaseClass::ProcessRecord() { |
| 333 // If called, derived class doesn't know how to handle. | 420 // If called, derived class doesn't know how to handle. |
| 334 std::string Buffer; | 421 std::string Buffer; |
| 335 raw_string_ostream StrBuf(Buffer); | 422 raw_string_ostream StrBuf(Buffer); |
| 336 StrBuf << "Don't know how to process record: " << Record; | 423 StrBuf << "Don't know how to process record: " << Record; |
| 337 Error(StrBuf.str()); | 424 Error(StrBuf.str()); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 360 case naclbitc::TYPE_CODE_NUMENTRY: | 447 case naclbitc::TYPE_CODE_NUMENTRY: |
| 361 // NUMENTRY: [numentries] | 448 // NUMENTRY: [numentries] |
| 362 if (checkRecordSize(1, "Type count")) | 449 if (checkRecordSize(1, "Type count")) |
| 363 return; | 450 return; |
| 364 Context->resizeTypeIDValues(Values[0]); | 451 Context->resizeTypeIDValues(Values[0]); |
| 365 return; | 452 return; |
| 366 case naclbitc::TYPE_CODE_VOID: | 453 case naclbitc::TYPE_CODE_VOID: |
| 367 // VOID | 454 // VOID |
| 368 if (checkRecordSize(0, "Type void")) | 455 if (checkRecordSize(0, "Type void")) |
| 369 break; | 456 break; |
| 370 Ty = Type::getVoidTy(Context->getLLVMContext()); | 457 Ty = Context->convertToLLVMType(Ice::IceType_void); |
| 371 break; | 458 break; |
| 372 case naclbitc::TYPE_CODE_FLOAT: | 459 case naclbitc::TYPE_CODE_FLOAT: |
| 373 // FLOAT | 460 // FLOAT |
| 374 if (checkRecordSize(0, "Type float")) | 461 if (checkRecordSize(0, "Type float")) |
| 375 break; | 462 break; |
| 376 Ty = Type::getFloatTy(Context->getLLVMContext()); | 463 Ty = Context->convertToLLVMType(Ice::IceType_f32); |
| 377 break; | 464 break; |
| 378 case naclbitc::TYPE_CODE_DOUBLE: | 465 case naclbitc::TYPE_CODE_DOUBLE: |
| 379 // DOUBLE | 466 // DOUBLE |
| 380 if (checkRecordSize(0, "Type double")) | 467 if (checkRecordSize(0, "Type double")) |
| 381 break; | 468 break; |
| 382 Ty = Type::getDoubleTy(Context->getLLVMContext()); | 469 Ty = Context->convertToLLVMType(Ice::IceType_f64); |
| 383 break; | 470 break; |
| 384 case naclbitc::TYPE_CODE_INTEGER: | 471 case naclbitc::TYPE_CODE_INTEGER: |
| 385 // INTEGER: [width] | 472 // INTEGER: [width] |
| 386 if (checkRecordSize(1, "Type integer")) | 473 if (checkRecordSize(1, "Type integer")) |
| 387 break; | 474 break; |
| 388 Ty = IntegerType::get(Context->getLLVMContext(), Values[0]); | 475 Ty = Context->getLLVMIntegerType(Values[0]); |
| 389 // TODO(kschimpf) Check if size is legal. | 476 if (Ty == NULL) { |
| 477 std::string Buffer; | |
| 478 raw_string_ostream StrBuf(Buffer); | |
| 479 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; | |
| 480 Error(StrBuf.str()); | |
| 481 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 482 // Fix type so that we can continue. | |
| 483 Ty = Context->convertToLLVMType(Ice::IceType_i32); | |
| 484 } | |
| 390 break; | 485 break; |
| 391 case naclbitc::TYPE_CODE_VECTOR: | 486 case naclbitc::TYPE_CODE_VECTOR: { |
| 392 // VECTOR: [numelts, eltty] | 487 // VECTOR: [numelts, eltty] |
| 393 if (checkRecordSize(2, "Type vector")) | 488 if (checkRecordSize(2, "Type vector")) |
| 394 break; | 489 break; |
| 395 Ty = VectorType::get(Context->getTypeByID(Values[1]), Values[0]); | 490 Type *BaseTy = Context->getTypeByID(Values[1]); |
| 491 Ty = Context->getLLVMVectorType(Values[0], | |
| 492 Context->convertToIceType(BaseTy)); | |
| 493 if (Ty == NULL) { | |
| 494 std::string Buffer; | |
| 495 raw_string_ostream StrBuf(Buffer); | |
| 496 StrBuf << "Invalid type vector record: <" << Values[0] | |
| 497 << " x " << *BaseTy << ">"; | |
| 498 Error(StrBuf.str()); | |
| 499 Ty = Context->convertToLLVMType(Ice::IceType_void); | |
| 500 } | |
| 396 break; | 501 break; |
| 502 } | |
| 397 case naclbitc::TYPE_CODE_FUNCTION: { | 503 case naclbitc::TYPE_CODE_FUNCTION: { |
| 398 // FUNCTION: [vararg, retty, paramty x N] | 504 // FUNCTION: [vararg, retty, paramty x N] |
| 399 if (checkRecordSizeAtLeast(2, "Type signature")) | 505 if (checkRecordSizeAtLeast(2, "Type signature")) |
| 400 break; | 506 break; |
| 401 SmallVector<Type *, 8> ArgTys; | 507 SmallVector<Type *, 8> ArgTys; |
| 402 for (unsigned i = 2, e = Values.size(); i != e; ++i) { | 508 for (unsigned i = 2, e = Values.size(); i != e; ++i) { |
| 403 ArgTys.push_back(Context->getTypeByID(Values[i])); | 509 ArgTys.push_back(Context->getTypeByID(Values[i])); |
| 404 } | 510 } |
| 405 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); | 511 Ty = FunctionType::get(Context->getTypeByID(Values[1]), ArgTys, Values[0]); |
| 406 break; | 512 break; |
| 407 } | 513 } |
| 408 default: | 514 default: |
| 409 BlockParserBaseClass::ProcessRecord(); | 515 BlockParserBaseClass::ProcessRecord(); |
| 410 break; | 516 break; |
| 411 } | 517 } |
| 412 // If Ty not defined, assume error. Use void as filler. | 518 // If Ty not defined, assume error. Use void as filler. |
| 413 if (Ty == NULL) | 519 if (Ty == NULL) |
| 414 Ty = Type::getVoidTy(Context->getLLVMContext()); | 520 Ty = Context->convertToLLVMType(Ice::IceType_void); |
| 415 Context->setTypeID(NextTypeId++, Ty); | 521 Context->setTypeID(NextTypeId++, Ty); |
| 416 } | 522 } |
| 417 | 523 |
| 418 /// Parses the globals block (i.e. global variables). | 524 /// Parses the globals block (i.e. global variables). |
| 419 class GlobalsParser : public BlockParserBaseClass { | 525 class GlobalsParser : public BlockParserBaseClass { |
| 420 public: | 526 public: |
| 421 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 527 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 422 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), | 528 : BlockParserBaseClass(BlockID, EnclosingParser), InitializersNeeded(0), |
| 423 Alignment(1), IsConstant(false) { | 529 Alignment(1), IsConstant(false) { |
| 424 NextGlobalID = Context->getNumFunctionIDs(); | 530 NextGlobalID = Context->getNumFunctionIDs(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 if (InitializersNeeded != Initializers.size()) { | 573 if (InitializersNeeded != Initializers.size()) { |
| 468 std::string Buffer; | 574 std::string Buffer; |
| 469 raw_string_ostream StrBuf(Buffer); | 575 raw_string_ostream StrBuf(Buffer); |
| 470 StrBuf << "Global variable @g" | 576 StrBuf << "Global variable @g" |
| 471 << (NextGlobalID - Context->getNumFunctionIDs()) << " expected " | 577 << (NextGlobalID - Context->getNumFunctionIDs()) << " expected " |
| 472 << InitializersNeeded << " initializer"; | 578 << InitializersNeeded << " initializer"; |
| 473 if (InitializersNeeded > 1) | 579 if (InitializersNeeded > 1) |
| 474 StrBuf << "s"; | 580 StrBuf << "s"; |
| 475 StrBuf << ". Found: " << Initializers.size(); | 581 StrBuf << ". Found: " << Initializers.size(); |
| 476 Error(StrBuf.str()); | 582 Error(StrBuf.str()); |
| 583 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 477 // Fix up state so that we can continue. | 584 // Fix up state so that we can continue. |
| 478 InitializersNeeded = Initializers.size(); | 585 InitializersNeeded = Initializers.size(); |
| 479 installGlobalVar(); | 586 installGlobalVar(); |
| 480 } | 587 } |
| 481 } | 588 } |
| 482 | 589 |
| 483 // Reserves a slot in the list of initializers being built. If there | 590 // Reserves a slot in the list of initializers being built. If there |
| 484 // isn't room for the slot, an error message is generated. | 591 // isn't room for the slot, an error message is generated. |
| 485 void reserveInitializer(const char *RecordName) { | 592 void reserveInitializer(const char *RecordName) { |
| 486 if (InitializersNeeded <= Initializers.size()) { | 593 if (InitializersNeeded <= Initializers.size()) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 return; | 673 return; |
| 567 } | 674 } |
| 568 InitializersNeeded = Values[0]; | 675 InitializersNeeded = Values[0]; |
| 569 return; | 676 return; |
| 570 case naclbitc::GLOBALVAR_ZEROFILL: { | 677 case naclbitc::GLOBALVAR_ZEROFILL: { |
| 571 // ZEROFILL: [size] | 678 // ZEROFILL: [size] |
| 572 if (checkRecordSize(1, "Globals zerofill")) | 679 if (checkRecordSize(1, "Globals zerofill")) |
| 573 return; | 680 return; |
| 574 reserveInitializer("Globals zerofill"); | 681 reserveInitializer("Globals zerofill"); |
| 575 Type *Ty = | 682 Type *Ty = |
| 576 ArrayType::get(Type::getInt8Ty(Context->getLLVMContext()), Values[0]); | 683 ArrayType::get(Context->convertToLLVMType(Ice::IceType_i8), Values[0]); |
| 577 Constant *Zero = ConstantAggregateZero::get(Ty); | 684 Constant *Zero = ConstantAggregateZero::get(Ty); |
| 578 Initializers.push_back(Zero); | 685 Initializers.push_back(Zero); |
| 579 break; | 686 break; |
| 580 } | 687 } |
| 581 case naclbitc::GLOBALVAR_DATA: { | 688 case naclbitc::GLOBALVAR_DATA: { |
| 582 // DATA: [b0, b1, ...] | 689 // DATA: [b0, b1, ...] |
| 583 if (checkRecordSizeAtLeast(1, "Globals data")) | 690 if (checkRecordSizeAtLeast(1, "Globals data")) |
| 584 return; | 691 return; |
| 585 reserveInitializer("Globals data"); | 692 reserveInitializer("Globals data"); |
| 586 unsigned Size = Values.size(); | 693 unsigned Size = Values.size(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 597 if (checkRecordSizeInRange(1, 2, "Globals reloc")) | 704 if (checkRecordSizeInRange(1, 2, "Globals reloc")) |
| 598 return; | 705 return; |
| 599 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); | 706 Constant *BaseVal = Context->getOrCreateGlobalVarRef(Values[0]); |
| 600 if (BaseVal == 0) { | 707 if (BaseVal == 0) { |
| 601 std::string Buffer; | 708 std::string Buffer; |
| 602 raw_string_ostream StrBuf(Buffer); | 709 raw_string_ostream StrBuf(Buffer); |
| 603 StrBuf << "Can't find global relocation value: " << Values[0]; | 710 StrBuf << "Can't find global relocation value: " << Values[0]; |
| 604 Error(StrBuf.str()); | 711 Error(StrBuf.str()); |
| 605 return; | 712 return; |
| 606 } | 713 } |
| 607 Type *IntPtrType = IntegerType::get(Context->getLLVMContext(), 32); | 714 Type *IntPtrType = Context->convertToLLVMType(Context->getIcePointerType()); |
| 608 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); | 715 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); |
| 609 if (Values.size() == 2) { | 716 if (Values.size() == 2) { |
| 610 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); | 717 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, Values[1])); |
| 611 } | 718 } |
| 612 Initializers.push_back(Val); | 719 Initializers.push_back(Val); |
| 613 break; | 720 break; |
| 614 } | 721 } |
| 615 default: | 722 default: |
| 616 BlockParserBaseClass::ProcessRecord(); | 723 BlockParserBaseClass::ProcessRecord(); |
| 617 return; | 724 return; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 break; | 785 break; |
| 679 } | 786 } |
| 680 default: | 787 default: |
| 681 break; | 788 break; |
| 682 } | 789 } |
| 683 // If reached, don't know how to handle record. | 790 // If reached, don't know how to handle record. |
| 684 BlockParserBaseClass::ProcessRecord(); | 791 BlockParserBaseClass::ProcessRecord(); |
| 685 return; | 792 return; |
| 686 } | 793 } |
| 687 | 794 |
| 795 /// Parses function blocks in the bitcode file. | |
| 796 class FunctionParser : public BlockParserBaseClass { | |
| 797 public: | |
| 798 FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | |
|
Jim Stichnoth
2014/07/21 22:25:22
Disable default copy ctor etc.
Karl
2014/07/23 20:22:21
Done.
| |
| 799 : BlockParserBaseClass(BlockID, EnclosingParser), | |
| 800 Func(new Ice::Cfg(getTranslator().getContext())), | |
| 801 CurrentBbIndex(0), | |
| 802 FcnId(Context->getNextFunctionBlockValueID()), | |
| 803 LLVMFunc(cast<Function>(Context->getGlobalValueByID(FcnId))), | |
| 804 CachedNumGlobalValueIDs(Context->getNumGlobalValueIDs()), | |
| 805 InstIsTerminating(false) | |
| 806 { | |
| 807 Func->setFunctionName(LLVMFunc->getName()); | |
| 808 Func->setReturnType(Context->convertToIceType(LLVMFunc->getReturnType())); | |
| 809 Func->setInternal(LLVMFunc->hasInternalLinkage()); | |
| 810 CurrentNode = InstallNextBasicBlock(); | |
| 811 for (Function::const_arg_iterator ArgI = LLVMFunc->arg_begin(), | |
| 812 ArgE = LLVMFunc->arg_end(); | |
| 813 ArgI != ArgE; ++ArgI) { | |
| 814 Func->addArg(NextInstVar(Context->convertToIceType(ArgI->getType()))); | |
| 815 } | |
| 816 } | |
| 817 | |
| 818 ~FunctionParser() LLVM_OVERRIDE; | |
| 819 | |
| 820 private: | |
| 821 // Timer for reading function bitcode and converting to ICE. | |
| 822 Ice::Timer TConvert; | |
| 823 // The corresponding ICE function defined by the function block. | |
| 824 Ice::Cfg *Func; | |
|
jvoung (off chromium)
2014/07/23 16:10:17
Smart pointer for Func?
What deletes Func?
I gues
Karl
2014/07/23 20:22:21
Yes. We call translateFcn with it in the destructo
jvoung (off chromium)
2014/07/24 19:01:44
Okay.
Something about running translateFcn() in t
Karl
2014/07/25 21:49:03
Decided to move the cleanup/call to the ExitBlock
| |
| 825 // The index to the current basic block being built. | |
| 826 uint32_t CurrentBbIndex; | |
| 827 // The basic block being built. | |
| 828 Ice::CfgNode *CurrentNode; | |
| 829 // The ID for the function. | |
| 830 unsigned FcnId; | |
| 831 // The corresponding llvm function. | |
|
Jim Stichnoth
2014/07/21 22:25:22
LLVM :)
Karl
2014/07/23 20:22:21
Done.
| |
| 832 Function *LLVMFunc; | |
| 833 // Holds variables local to the function block. | |
| 834 std::vector<Ice::Operand *> LocalVars; | |
|
Jim Stichnoth
2014/07/21 22:25:21
This should probably be a vector of Variable*, not
Karl
2014/07/23 20:22:21
This vector is used to convert indices from the bi
jvoung (off chromium)
2014/07/24 19:01:44
It can still be a std::vector<Ice::Variable *> (do
Jim Stichnoth
2014/07/24 19:55:22
What I understood from Karl is that ultimately thi
Karl
2014/07/25 21:49:03
That is correct.
| |
| 835 // Holds the list of basic blocks defined for the function. | |
| 836 std::vector<Ice::CfgNode*> Nodes; | |
|
Jim Stichnoth
2014/07/21 22:25:21
I don't think you need this, since it should be av
Karl
2014/07/23 20:22:21
Done.
| |
| 837 // Holds the dividing point between local and global absolute value indices. | |
| 838 uint32_t CachedNumGlobalValueIDs; | |
| 839 // True if the last processed instruction was a terminating | |
| 840 // instruction. | |
| 841 bool InstIsTerminating; | |
| 842 | |
| 843 virtual void ProcessRecord() LLVM_OVERRIDE; | |
| 844 | |
| 845 // Creates and appends a new basic block to the list of basic blocks. | |
| 846 Ice::CfgNode *InstallNextBasicBlock() { | |
| 847 std::string Buffer; | |
| 848 raw_string_ostream StrBuf(Buffer); | |
| 849 // TODO(kschimpf): Make (basic block) nodes make up the name if needed. | |
| 850 StrBuf << "bb" << Nodes.size(); | |
| 851 Ice::CfgNode *Node = Func->makeNode(StrBuf.str()); | |
| 852 Nodes.push_back(Node); | |
| 853 return Node; | |
| 854 } | |
| 855 | |
| 856 // Returns the Index-th basic block in the list of basic blocks. | |
| 857 Ice::CfgNode *GetBasicBlock(uint32_t Index) { | |
| 858 if (Index >= Nodes.size()) { | |
| 859 std::string Buffer; | |
| 860 raw_string_ostream StrBuf(Buffer); | |
| 861 StrBuf << "Reference to basic block " << Index | |
| 862 << " not found. Must be less than " << Nodes.size(); | |
| 863 Error(StrBuf.str()); | |
| 864 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 865 Index = 0; | |
| 866 } | |
| 867 return Nodes[Index]; | |
| 868 } | |
| 869 | |
| 870 // Generates the next available local variable using the given | |
| 871 // type. Note: if Ty is void, this function returns NULL. | |
| 872 Ice::Variable *NextInstVar(Ice::Type Ty) { | |
| 873 if (Ty == Ice::IceType_void) | |
| 874 return NULL; | |
| 875 Ice::Variable *Var = Func->makeVariable(Ty, CurrentNode); | |
| 876 LocalVars.push_back(Var); | |
| 877 return Var; | |
| 878 } | |
| 879 | |
| 880 // Converts a relative index (to the next instruction to be read) to | |
| 881 // an absolute value index. | |
| 882 uint32_t convertRelativeToAbsIndex(int32_t Id) { | |
| 883 int32_t AbsNextId = CachedNumGlobalValueIDs + LocalVars.size(); | |
| 884 if (Id > 0 && AbsNextId < static_cast<uint32_t>(Id)) { | |
| 885 std::string Buffer; | |
| 886 raw_string_ostream StrBuf(Buffer); | |
| 887 StrBuf << "Invalid relative value id: " << Id | |
| 888 << " (must be <= " << AbsNextId << ")"; | |
| 889 Error(StrBuf.str()); | |
| 890 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 891 return 0; | |
| 892 } | |
| 893 return AbsNextId - Id; | |
| 894 } | |
| 895 | |
| 896 // Returns the value referenced by the given value Index. | |
| 897 Ice::Operand *getOperand(uint32_t Index) { | |
| 898 if (Index < CachedNumGlobalValueIDs) { | |
| 899 // TODO(kschimpf): Define implementation. | |
| 900 report_fatal_error("getOperand of global addresses not implemented"); | |
| 901 } | |
| 902 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; | |
| 903 if (LocalIndex >= LocalVars.size()) { | |
| 904 std::string Buffer; | |
| 905 raw_string_ostream StrBuf(Buffer); | |
| 906 StrBuf << "Value index " << Index << " out of range. Must be less than " | |
| 907 << (LocalVars.size() + CachedNumGlobalValueIDs); | |
| 908 Error(StrBuf.str()); | |
| 909 report_fatal_error("Unable to continue"); | |
| 910 } | |
| 911 return LocalVars[LocalIndex]; | |
| 912 } | |
| 913 | |
| 914 // Checks if integer arithmetic Op, for type OpTy, is valid. | |
| 915 // Returns false if valid. Otherwise generates error message and | |
|
Jim Stichnoth
2014/07/21 22:25:22
I find the "return false if valid, true if invalid
Karl
2014/07/23 20:22:20
Changed the context of boolean functions (when pos
| |
| 916 // returns true. | |
| 917 bool checkIfIntArithmeticOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { | |
| 918 if (!PNaClABITypeChecker::isValidIntArithmeticType( | |
| 919 Context->convertToLLVMType(OpTy))) { | |
| 920 std::string Buffer; | |
| 921 raw_string_ostream StrBuf(Buffer); | |
| 922 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 923 << ": Invalid integer arithmetic type: " << OpTy; | |
| 924 return Error(StrBuf.str()); | |
| 925 } | |
| 926 return false; | |
| 927 } | |
| 928 | |
| 929 // Checks if integer (or vector of integers) arithmetic Op, for type | |
| 930 // OpTy, is valid. Returns false if valid. Otherwise generates | |
| 931 // error message and returns true. | |
| 932 bool checkIfIntOrIntVectorArithOp(Ice::InstArithmetic::OpKind Op, | |
| 933 Ice::Type OpTy) { | |
| 934 Ice::Type BaseTy = OpTy; | |
| 935 if (Ice::isVectorType(OpTy)) { | |
| 936 if (!PNaClABITypeChecker::isValidVectorType( | |
| 937 Context->convertToLLVMType(OpTy))) { | |
| 938 std::string Buffer; | |
| 939 raw_string_ostream StrBuf(Buffer); | |
| 940 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 941 << ": invalid vector type: " << OpTy; | |
| 942 return Error(StrBuf.str()); | |
| 943 } | |
| 944 BaseTy = Ice::typeElementType(OpTy); | |
| 945 } | |
| 946 if (Ice::isIntegerType(BaseTy)) { | |
| 947 if (PNaClABITypeChecker::isValidIntArithmeticType( | |
| 948 Context->convertToLLVMType(BaseTy))) { | |
| 949 return false; | |
| 950 } | |
| 951 std::string Buffer; | |
| 952 raw_string_ostream StrBuf(Buffer); | |
| 953 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 954 << ": Invalid integer type: " << OpTy; | |
| 955 return Error(StrBuf.str()); | |
| 956 } else { | |
|
Jim Stichnoth
2014/07/21 22:25:21
http://llvm.org/docs/CodingStandards.html#don-t-us
Karl
2014/07/23 20:22:20
Done.
| |
| 957 std::string Buffer; | |
| 958 raw_string_ostream StrBuf(Buffer); | |
| 959 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 960 << ": Expects integer type. Found: " << OpTy; | |
| 961 return Error(StrBuf.str()); | |
| 962 } | |
| 963 return false; | |
|
Jim Stichnoth
2014/07/21 22:25:21
This return statement is unreachable, right?
Karl
2014/07/23 20:22:20
Yes. But I've refactored the code to better match
| |
| 964 } | |
| 965 | |
| 966 // Checks if floating arithmetic Op, for type OpTy, is valid. | |
| 967 // Returns false if valid. Otherwise generates an error message and | |
| 968 // returns true. | |
| 969 bool checkIfFloatOrVectorFloatArithOp(Ice::InstArithmetic::OpKind Op, | |
| 970 Ice::Type OpTy) { | |
| 971 Ice::Type BaseTy = OpTy; | |
| 972 if (Ice::isVectorType(OpTy)) { | |
| 973 if (!PNaClABITypeChecker::isValidVectorType( | |
| 974 Context->convertToLLVMType(OpTy))) { | |
| 975 std::string Buffer; | |
| 976 raw_string_ostream StrBuf(Buffer); | |
| 977 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 978 << ": invalid vector type: " << OpTy; | |
| 979 return Error(StrBuf.str()); | |
| 980 } | |
| 981 BaseTy = Ice::typeElementType(OpTy); | |
| 982 } | |
| 983 if (Ice::isFloatingType(BaseTy)) { | |
|
jvoung (off chromium)
2014/07/22 18:40:18
not isFloatingType ?
Karl
2014/07/23 20:22:21
Yes. Fixed.
| |
| 984 std::string Buffer; | |
| 985 raw_string_ostream StrBuf(Buffer); | |
| 986 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 987 << ": Expects floating point. Found " << OpTy; | |
| 988 return Error(StrBuf.str()); | |
| 989 } | |
| 990 if (!PNaClABITypeChecker::isValidScalarType( | |
|
jvoung (off chromium)
2014/07/22 18:40:18
When will this end up begin checked, if isFloating
Karl
2014/07/23 20:22:20
At the moment yes. However, the question is whethe
| |
| 991 Context->convertToLLVMType(BaseTy))) { | |
| 992 std::string Buffer; | |
| 993 raw_string_ostream StrBuf(Buffer); | |
| 994 StrBuf << Ice::InstArithmetic::getOpName(Op) | |
| 995 << ": type not allowed: " << OpTy; | |
| 996 return Error(StrBuf.str()); | |
| 997 } | |
| 998 return false; | |
| 999 } | |
| 1000 | |
| 1001 /// Takes the PNaCl bitcode binary operator Opcode, and the opcode | |
| 1002 /// type Ty, and sets Op to the corresponding ICE binary | |
| 1003 /// opcode. Returns false if able to convert, true otherwise. | |
| 1004 bool convertBinopOpcode(unsigned Opcode, | |
| 1005 Ice::Type Ty, | |
| 1006 Ice::InstArithmetic::OpKind &Op) { | |
| 1007 Instruction::BinaryOps LLVMOpcode; | |
| 1008 if (!naclbitc::DecodeBinaryOpcode( | |
| 1009 Opcode, Context->convertToLLVMType(Ty), LLVMOpcode)) { | |
| 1010 std::string Buffer; | |
| 1011 raw_string_ostream StrBuf(Buffer); | |
| 1012 StrBuf << "Binary opcode not understood: " << Opcode; | |
| 1013 Error(StrBuf.str()); | |
| 1014 Op = Ice::InstArithmetic::Add; | |
| 1015 return true; | |
| 1016 } | |
| 1017 switch (LLVMOpcode) { | |
| 1018 default: { | |
| 1019 std::string Buffer; | |
| 1020 raw_string_ostream StrBuf(Buffer); | |
| 1021 StrBuf << "Binary opcode not understood: " << Opcode; | |
| 1022 Error(StrBuf.str()); | |
| 1023 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 1024 Op = Ice::InstArithmetic::Add; | |
| 1025 return true; | |
| 1026 } | |
| 1027 case Instruction::Add: | |
| 1028 Op = Ice::InstArithmetic::Add; | |
| 1029 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1030 case Instruction::FAdd: | |
| 1031 Op = Ice::InstArithmetic::Fadd; | |
| 1032 return checkIfFloatOrVectorFloatArithOp(Op, Ty); | |
| 1033 case Instruction::Sub: | |
| 1034 Op = Ice::InstArithmetic::Sub; | |
| 1035 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1036 case Instruction::FSub: | |
| 1037 Op = Ice::InstArithmetic::Fsub; | |
| 1038 return checkIfFloatOrVectorFloatArithOp(Op, Ty); | |
| 1039 case Instruction::Mul: | |
| 1040 Op = Ice::InstArithmetic::Mul; | |
| 1041 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1042 case Instruction::FMul: | |
| 1043 Op = Ice::InstArithmetic::Fmul; | |
| 1044 return checkIfFloatOrVectorFloatArithOp(Op, Ty); | |
| 1045 case Instruction::UDiv: | |
| 1046 Op = Ice::InstArithmetic::Udiv; | |
| 1047 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1048 case Instruction::SDiv: | |
| 1049 Op = Ice::InstArithmetic::Sdiv; | |
| 1050 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1051 case Instruction::FDiv: | |
| 1052 Op = Ice::InstArithmetic::Fdiv; | |
| 1053 return checkIfFloatOrVectorFloatArithOp(Op, Ty); | |
| 1054 case Instruction::URem: | |
| 1055 Op = Ice::InstArithmetic::Urem; | |
| 1056 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1057 case Instruction::SRem: | |
| 1058 Op = Ice::InstArithmetic::Srem; | |
| 1059 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1060 case Instruction::FRem: | |
| 1061 Op = Ice::InstArithmetic::Frem; | |
| 1062 return checkIfFloatOrVectorFloatArithOp(Op, Ty); | |
| 1063 case Instruction::Shl: | |
| 1064 Op = Ice::InstArithmetic::Shl; | |
| 1065 return checkIfIntArithmeticOp(Op, Ty); | |
|
jvoung (off chromium)
2014/07/23 16:10:17
It should be possible to Shl on an integer vector
Karl
2014/07/23 20:22:21
I see. I'm slightly confused with the semantics. R
jvoung (off chromium)
2014/07/24 19:01:44
I think you mean changing to isValidIntOrIntVector
Karl
2014/07/25 21:49:03
Yes. My comment was incorrect. However the code lo
| |
| 1066 case Instruction::LShr: | |
| 1067 Op = Ice::InstArithmetic::Lshr; | |
| 1068 return checkIfIntArithmeticOp(Op, Ty); | |
|
jvoung (off chromium)
2014/07/23 16:10:17
similar
Karl
2014/07/23 20:22:21
Done.
| |
| 1069 case Instruction::AShr: | |
| 1070 Op = Ice::InstArithmetic::Ashr; | |
| 1071 return checkIfIntArithmeticOp(Op, Ty); | |
| 1072 case Instruction::And: | |
| 1073 Op = Ice::InstArithmetic::And; | |
| 1074 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1075 case Instruction::Or: | |
| 1076 Op = Ice::InstArithmetic::Or; | |
| 1077 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1078 case Instruction::Xor: | |
| 1079 Op = Ice::InstArithmetic::Xor; | |
| 1080 return checkIfIntOrIntVectorArithOp(Op, Ty); | |
| 1081 } | |
| 1082 } | |
| 1083 }; | |
| 1084 | |
| 1085 FunctionParser::~FunctionParser() { | |
| 1086 if (getTranslator().getFlags().SubzeroTimingEnabled) { | |
| 1087 errs() << "[Subzero timing] Convert function " | |
| 1088 << Func->getFunctionName() << ": " << TConvert.getElapsedSec() | |
| 1089 << " sec\n"; | |
| 1090 } | |
| 1091 // Before translating, check for blocks without instructions, and | |
| 1092 // insert unreachable. This shouldn't happen, but be safe. | |
| 1093 unsigned Index = 0; | |
| 1094 for (std::vector<Ice::CfgNode*>::iterator | |
| 1095 Iter = Nodes.begin(), IterEnd = Nodes.end(); | |
| 1096 Iter != IterEnd; ++Iter, ++Index) { | |
| 1097 Ice::CfgNode *Node = *Iter; | |
| 1098 if (Node->getInsts().size() == 0) { | |
| 1099 std::string Buffer; | |
| 1100 raw_string_ostream StrBuf(Buffer); | |
| 1101 StrBuf << "Basic block " << Index << " contains no instructions"; | |
| 1102 Error(StrBuf.str()); | |
| 1103 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 1104 Node->appendInst(Ice::InstUnreachable::create(Func)); | |
| 1105 } | |
| 1106 } | |
| 1107 getTranslator().translateFcn(Func); | |
| 1108 } | |
| 1109 | |
| 1110 void FunctionParser::ProcessRecord() { | |
| 1111 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | |
| 1112 if (InstIsTerminating) { | |
| 1113 InstIsTerminating = false; | |
| 1114 CurrentNode = GetBasicBlock(++CurrentBbIndex); | |
| 1115 } | |
| 1116 Ice::Inst *Inst = NULL; | |
| 1117 switch (Record.GetCode()) { | |
| 1118 case naclbitc::FUNC_CODE_DECLAREBLOCKS: { | |
| 1119 // DECLAREBLOCKS: [n] | |
| 1120 if (checkRecordSize(1, "function block count")) | |
| 1121 break; | |
| 1122 if (Nodes.size() != 1) { | |
| 1123 Error("Duplicate function block count record"); | |
| 1124 return; | |
| 1125 } | |
| 1126 uint32_t NumBbs = Values[0]; | |
| 1127 if (NumBbs == 0) { | |
| 1128 Error("Functions must contain at least one basic block."); | |
| 1129 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 1130 NumBbs = 1; | |
| 1131 } | |
| 1132 // Install the basic blocks, skipping bb0 which was created in the | |
| 1133 // constructor. | |
| 1134 for (size_t i = 1; i < NumBbs; ++i) | |
| 1135 InstallNextBasicBlock(); | |
| 1136 break; | |
| 1137 } | |
| 1138 case naclbitc::FUNC_CODE_INST_BINOP: { | |
| 1139 // BINOP: [opval, opval, opcode] | |
| 1140 if (checkRecordSize(3, "function block binop")) | |
| 1141 break; | |
| 1142 Ice::Operand *Op1 = getOperand(convertRelativeToAbsIndex(Values[0])); | |
| 1143 Ice::Operand *Op2 = getOperand(convertRelativeToAbsIndex(Values[1])); | |
| 1144 Ice::Type Type1 = Op1->getType(); | |
| 1145 Ice::Type Type2 = Op2->getType(); | |
| 1146 if (Type1 != Type2) { | |
| 1147 std::string Buffer; | |
| 1148 raw_string_ostream StrBuf(Buffer); | |
| 1149 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; | |
| 1150 Error(StrBuf.str()); | |
| 1151 // TODO(kschimpf) Remove error recovery once implementation complete. | |
| 1152 Op2 = Op1; | |
| 1153 } | |
| 1154 | |
| 1155 Ice::InstArithmetic::OpKind Opcode; | |
| 1156 if (convertBinopOpcode(Values[2], Type1, Opcode)) | |
| 1157 break; | |
| 1158 Ice::Variable *Dest = NextInstVar(Type1); | |
| 1159 Inst = Ice::InstArithmetic::create(Func, Opcode, Dest, Op1, Op2); | |
| 1160 break; | |
| 1161 } | |
| 1162 case naclbitc::FUNC_CODE_INST_RET: { | |
| 1163 // RET: [opval?] | |
| 1164 InstIsTerminating = true; | |
| 1165 if (checkRecordSizeInRange(0, 1, "function block ret")) | |
| 1166 break; | |
| 1167 if (Values.size() == 0) { | |
| 1168 Inst = Ice::InstRet::create(Func); | |
| 1169 } else { | |
| 1170 Inst = Ice::InstRet::create( | |
| 1171 Func, getOperand(convertRelativeToAbsIndex(Values[0]))); | |
| 1172 } | |
| 1173 break; | |
| 1174 } | |
| 1175 default: | |
| 1176 // Generate error message! | |
| 1177 BlockParserBaseClass::ProcessRecord(); | |
| 1178 break; | |
| 1179 } | |
| 1180 if (Inst) | |
| 1181 CurrentNode->appendInst(Inst); | |
| 1182 } | |
| 1183 | |
| 688 /// Parses the module block in the bitcode file. | 1184 /// Parses the module block in the bitcode file. |
| 689 class ModuleParser : public BlockParserBaseClass { | 1185 class ModuleParser : public BlockParserBaseClass { |
| 690 public: | 1186 public: |
| 691 ModuleParser(unsigned BlockID, TopLevelParser *Context) | 1187 ModuleParser(unsigned BlockID, TopLevelParser *Context) |
| 692 : BlockParserBaseClass(BlockID, Context) {} | 1188 : BlockParserBaseClass(BlockID, Context) {} |
| 693 | 1189 |
| 694 virtual ~ModuleParser() LLVM_OVERRIDE {} | 1190 virtual ~ModuleParser() LLVM_OVERRIDE {} |
| 695 | 1191 |
| 696 protected: | 1192 protected: |
| 697 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 1193 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 709 } | 1205 } |
| 710 case naclbitc::GLOBALVAR_BLOCK_ID: { | 1206 case naclbitc::GLOBALVAR_BLOCK_ID: { |
| 711 GlobalsParser Parser(BlockID, this); | 1207 GlobalsParser Parser(BlockID, this); |
| 712 return Parser.ParseThisBlock(); | 1208 return Parser.ParseThisBlock(); |
| 713 } | 1209 } |
| 714 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { | 1210 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
| 715 ValuesymtabParser Parser(BlockID, this, false); | 1211 ValuesymtabParser Parser(BlockID, this, false); |
| 716 return Parser.ParseThisBlock(); | 1212 return Parser.ParseThisBlock(); |
| 717 } | 1213 } |
| 718 case naclbitc::FUNCTION_BLOCK_ID: { | 1214 case naclbitc::FUNCTION_BLOCK_ID: { |
| 719 Error("Function block parser not yet implemented, skipping"); | 1215 FunctionParser Parser(BlockID, this); |
| 720 SkipBlock(); | 1216 return Parser.ParseThisBlock(); |
| 721 return false; | |
| 722 } | 1217 } |
| 723 default: | 1218 default: |
| 724 return BlockParserBaseClass::ParseBlock(BlockID); | 1219 return BlockParserBaseClass::ParseBlock(BlockID); |
| 725 } | 1220 } |
| 726 } | 1221 } |
| 727 | 1222 |
| 728 void ModuleParser::ProcessRecord() { | 1223 void ModuleParser::ProcessRecord() { |
| 729 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | 1224 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 730 switch (Record.GetCode()) { | 1225 switch (Record.GetCode()) { |
| 731 case naclbitc::MODULE_CODE_VERSION: { | 1226 case naclbitc::MODULE_CODE_VERSION: { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 } | 1276 } |
| 782 default: | 1277 default: |
| 783 BlockParserBaseClass::ProcessRecord(); | 1278 BlockParserBaseClass::ProcessRecord(); |
| 784 return; | 1279 return; |
| 785 } | 1280 } |
| 786 } | 1281 } |
| 787 | 1282 |
| 788 bool TopLevelParser::ParseBlock(unsigned BlockID) { | 1283 bool TopLevelParser::ParseBlock(unsigned BlockID) { |
| 789 if (BlockID == naclbitc::MODULE_BLOCK_ID) { | 1284 if (BlockID == naclbitc::MODULE_BLOCK_ID) { |
| 790 ModuleParser Parser(BlockID, this); | 1285 ModuleParser Parser(BlockID, this); |
| 791 bool ReturnValue = Parser.ParseThisBlock(); | 1286 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 } | 1287 } |
| 799 // Generate error message by using default block implementation. | 1288 // Generate error message by using default block implementation. |
| 800 BlockParserBaseClass Parser(BlockID, this); | 1289 BlockParserBaseClass Parser(BlockID, this); |
| 801 return Parser.ParseThisBlock(); | 1290 return Parser.ParseThisBlock(); |
| 802 } | 1291 } |
| 803 | 1292 |
| 804 } // end of anonymous namespace. | 1293 } // end of anonymous namespace. |
| 805 | 1294 |
| 806 namespace Ice { | 1295 namespace Ice { |
| 807 | 1296 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 829 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 1318 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
| 830 errs() << "Invalid PNaCl bitcode header.\n"; | 1319 errs() << "Invalid PNaCl bitcode header.\n"; |
| 831 ErrorStatus = true; | 1320 ErrorStatus = true; |
| 832 return; | 1321 return; |
| 833 } | 1322 } |
| 834 | 1323 |
| 835 // Create a bitstream reader to read the bitcode file. | 1324 // Create a bitstream reader to read the bitcode file. |
| 836 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 1325 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
| 837 NaClBitstreamCursor InputStream(InputStreamFile); | 1326 NaClBitstreamCursor InputStream(InputStreamFile); |
| 838 | 1327 |
| 839 TopLevelParser Parser(MemBuf->getBufferIdentifier(), Header, InputStream, | 1328 TopLevelParser Parser(*this, MemBuf->getBufferIdentifier(), Header, |
| 840 ErrorStatus); | 1329 InputStream, ErrorStatus); |
| 841 int TopLevelBlocks = 0; | 1330 int TopLevelBlocks = 0; |
| 842 while (!InputStream.AtEndOfStream()) { | 1331 while (!InputStream.AtEndOfStream()) { |
| 843 if (Parser.Parse()) { | 1332 if (Parser.Parse()) { |
| 844 ErrorStatus = true; | 1333 ErrorStatus = true; |
| 845 return; | 1334 return; |
| 846 } | 1335 } |
| 847 ++TopLevelBlocks; | 1336 ++TopLevelBlocks; |
| 848 } | 1337 } |
| 849 | 1338 |
| 850 if (TopLevelBlocks != 1) { | 1339 if (TopLevelBlocks != 1) { |
| 851 errs() << IRFilename | 1340 errs() << IRFilename |
| 852 << ": Contains more than one module. Found: " << TopLevelBlocks | 1341 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 853 << "\n"; | 1342 << "\n"; |
| 854 ErrorStatus = true; | 1343 ErrorStatus = true; |
| 855 } | 1344 } |
| 856 return; | 1345 return; |
| 857 } | 1346 } |
| 858 | 1347 |
| 859 } // end of anonymous namespace. | 1348 } // end of anonymous namespace. |
| OLD | NEW |