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 "llvm/ADT/SmallString.h" | 15 #include "llvm/ADT/SmallString.h" |
16 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" | 16 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
17 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 17 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
18 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 18 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
19 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 19 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
20 #include "llvm/IR/LLVMContext.h" | 20 #include "llvm/IR/LLVMContext.h" |
jvoung (off chromium)
2014/12/15 19:40:41
Can the LLVMContext header be removed too?
Karl
2014/12/15 20:27:54
Removed.
| |
21 #include "llvm/IR/Module.h" | |
22 #include "llvm/Support/Format.h" | 21 #include "llvm/Support/Format.h" |
23 #include "llvm/Support/MemoryBuffer.h" | 22 #include "llvm/Support/MemoryBuffer.h" |
24 #include "llvm/Support/raw_ostream.h" | 23 #include "llvm/Support/raw_ostream.h" |
25 | 24 |
26 #include "IceAPInt.h" | 25 #include "IceAPInt.h" |
27 #include "IceAPFloat.h" | 26 #include "IceAPFloat.h" |
28 #include "IceCfg.h" | 27 #include "IceCfg.h" |
29 #include "IceCfgNode.h" | 28 #include "IceCfgNode.h" |
30 #include "IceClFlags.h" | 29 #include "IceClFlags.h" |
31 #include "IceDefs.h" | 30 #include "IceDefs.h" |
32 #include "IceGlobalInits.h" | 31 #include "IceGlobalInits.h" |
33 #include "IceInst.h" | 32 #include "IceInst.h" |
34 #include "IceOperand.h" | 33 #include "IceOperand.h" |
35 #include "IceTypeConverter.h" | |
36 #include "PNaClTranslator.h" | 34 #include "PNaClTranslator.h" |
37 | 35 |
38 #include <memory> | 36 #include <memory> |
39 | 37 |
40 namespace { | 38 namespace { |
41 using namespace llvm; | 39 using namespace llvm; |
42 | 40 |
43 // TODO(kschimpf) Remove error recovery once implementation complete. | 41 // TODO(kschimpf) Remove error recovery once implementation complete. |
44 static cl::opt<bool> AllowErrorRecovery( | 42 static cl::opt<bool> AllowErrorRecovery( |
45 "allow-pnacl-reader-error-recovery", | 43 "allow-pnacl-reader-error-recovery", |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 class BlockParserBaseClass; | 161 class BlockParserBaseClass; |
164 | 162 |
165 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 163 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
166 class TopLevelParser : public NaClBitcodeParser { | 164 class TopLevelParser : public NaClBitcodeParser { |
167 TopLevelParser(const TopLevelParser &) = delete; | 165 TopLevelParser(const TopLevelParser &) = delete; |
168 TopLevelParser &operator=(const TopLevelParser &) = delete; | 166 TopLevelParser &operator=(const TopLevelParser &) = delete; |
169 | 167 |
170 public: | 168 public: |
171 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 169 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
172 | 170 |
173 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, | 171 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, |
Jim Stichnoth
2014/12/15 20:17:08
Can you remove the InputName arg, or something? I
Karl
2014/12/15 20:27:54
Removed.
| |
174 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, | 172 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, |
175 bool &ErrorStatus) | 173 bool &ErrorStatus) |
176 : NaClBitcodeParser(Cursor), Translator(Translator), | 174 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), |
177 Mod(new Module(InputName, getGlobalContext())), Header(Header), | 175 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
178 TypeConverter(Mod->getContext()), ErrorStatus(ErrorStatus), | 176 NumFunctionBlocks(0), BlockParser(nullptr) { |
179 NumErrors(0), NumFunctionIds(0), NumFunctionBlocks(0), | |
180 BlockParser(nullptr) { | |
181 setErrStream(Translator.getContext()->getStrDump()); | 177 setErrStream(Translator.getContext()->getStrDump()); |
182 } | 178 } |
183 | 179 |
184 ~TopLevelParser() override {} | 180 ~TopLevelParser() override {} |
185 | 181 |
186 Ice::Translator &getTranslator() { return Translator; } | 182 Ice::Translator &getTranslator() { return Translator; } |
187 | 183 |
188 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 184 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
189 BlockParser = NewBlockParser; | 185 BlockParser = NewBlockParser; |
190 } | 186 } |
191 | 187 |
192 // Generates error with given Message. Always returns true. | 188 // Generates error with given Message. Always returns true. |
193 bool Error(const std::string &Message) override; | 189 bool Error(const std::string &Message) override; |
194 | 190 |
195 // Generates error message with respect to the current block parser. | 191 // Generates error message with respect to the current block parser. |
196 bool BlockError(const std::string &Message); | 192 bool BlockError(const std::string &Message); |
197 | 193 |
198 /// Returns the number of errors found while parsing the bitcode | 194 /// Returns the number of errors found while parsing the bitcode |
199 /// file. | 195 /// file. |
200 unsigned getNumErrors() const { return NumErrors; } | 196 unsigned getNumErrors() const { return NumErrors; } |
201 | 197 |
202 /// Returns the LLVM module associated with the translation. | |
203 Module *getModule() const { return Mod.get(); } | |
204 | |
205 /// Returns the number of bytes in the bitcode header. | 198 /// Returns the number of bytes in the bitcode header. |
206 size_t getHeaderSize() const { return Header.getHeaderSize(); } | 199 size_t getHeaderSize() const { return Header.getHeaderSize(); } |
207 | 200 |
208 /// Changes the size of the type list to the given size. | 201 /// Changes the size of the type list to the given size. |
209 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } | 202 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } |
210 | 203 |
211 /// Returns true if generation of Subzero IR is disabled. | 204 /// Returns true if generation of Subzero IR is disabled. |
212 bool isIRGenerationDisabled() const { | 205 bool isIRGenerationDisabled() const { |
213 return ALLOW_DISABLE_IR_GEN ? Translator.getFlags().DisableIRGeneration | 206 return ALLOW_DISABLE_IR_GEN ? Translator.getFlags().DisableIRGeneration |
214 : false; | 207 : false; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 return getFunctionByID(Index); | 363 return getFunctionByID(Index); |
371 else | 364 else |
372 return getGlobalVariableByID(Index - NumFunctionIds); | 365 return getGlobalVariableByID(Index - NumFunctionIds); |
373 } | 366 } |
374 | 367 |
375 /// Returns the list of parsed global variable declarations. | 368 /// Returns the list of parsed global variable declarations. |
376 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { | 369 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { |
377 return VariableDeclarations; | 370 return VariableDeclarations; |
378 } | 371 } |
379 | 372 |
380 /// Returns the corresponding ICE type for LLVMTy. | |
381 Ice::Type convertToIceType(Type *LLVMTy) { | |
382 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); | |
383 if (IceTy >= Ice::IceType_NUM) { | |
384 return convertToIceTypeError(LLVMTy); | |
385 } | |
386 return IceTy; | |
387 } | |
388 | |
389 /// Returns the corresponding LLVM type for IceTy. | |
390 Type *convertToLLVMType(Ice::Type IceTy) const { | |
391 return TypeConverter.convertToLLVMType(IceTy); | |
392 } | |
393 | |
394 /// Returns the model for pointer types in ICE. | |
395 Ice::Type getIcePointerType() const { | |
396 return TypeConverter.getIcePointerType(); | |
397 } | |
398 | |
399 private: | 373 private: |
400 // The translator associated with the parser. | 374 // The translator associated with the parser. |
401 Ice::Translator &Translator; | 375 Ice::Translator &Translator; |
402 // The parsed module. | |
403 std::unique_ptr<Module> Mod; | |
404 // The bitcode header. | 376 // The bitcode header. |
405 NaClBitcodeHeader &Header; | 377 NaClBitcodeHeader &Header; |
406 // Converter between LLVM and ICE types. | |
407 Ice::TypeConverter TypeConverter; | |
408 // The exit status that should be set to true if an error occurs. | 378 // The exit status that should be set to true if an error occurs. |
409 bool &ErrorStatus; | 379 bool &ErrorStatus; |
410 // The number of errors reported. | 380 // The number of errors reported. |
411 unsigned NumErrors; | 381 unsigned NumErrors; |
412 // The types associated with each type ID. | 382 // The types associated with each type ID. |
413 std::vector<ExtendedType> TypeIDValues; | 383 std::vector<ExtendedType> TypeIDValues; |
414 // The set of functions. | 384 // The set of functions. |
415 FunctionDeclarationListType FunctionDeclarationList; | 385 FunctionDeclarationListType FunctionDeclarationList; |
416 // The set of global variables. | 386 // The set of global variables. |
417 Ice::Translator::VariableDeclarationListType VariableDeclarations; | 387 Ice::Translator::VariableDeclarationListType VariableDeclarations; |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1400 if (Ice::isFloatingType(OpTy)) | 1370 if (Ice::isFloatingType(OpTy)) |
1401 return true; | 1371 return true; |
1402 ReportInvalidBinaryOp(Op, OpTy); | 1372 ReportInvalidBinaryOp(Op, OpTy); |
1403 return false; | 1373 return false; |
1404 } | 1374 } |
1405 | 1375 |
1406 // Checks if the type of operand Op is the valid pointer type, for | 1376 // Checks if the type of operand Op is the valid pointer type, for |
1407 // the given InstructionName. Returns true if valid. Otherwise | 1377 // the given InstructionName. Returns true if valid. Otherwise |
1408 // generates an error message and returns false. | 1378 // generates an error message and returns false. |
1409 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { | 1379 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { |
1410 Ice::Type PtrType = Context->getIcePointerType(); | 1380 Ice::Type PtrType = Ice::getPointerType(); |
1411 if (Op->getType() == PtrType) | 1381 if (Op->getType() == PtrType) |
1412 return true; | 1382 return true; |
1413 std::string Buffer; | 1383 std::string Buffer; |
1414 raw_string_ostream StrBuf(Buffer); | 1384 raw_string_ostream StrBuf(Buffer); |
1415 StrBuf << InstructionName << " address not " << PtrType | 1385 StrBuf << InstructionName << " address not " << PtrType |
1416 << ". Found: " << *Op; | 1386 << ". Found: " << *Op; |
1417 Error(StrBuf.str()); | 1387 Error(StrBuf.str()); |
1418 return false; | 1388 return false; |
1419 } | 1389 } |
1420 | 1390 |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2358 if (!isValidRecordSize(2, "alloca")) | 2328 if (!isValidRecordSize(2, "alloca")) |
2359 return; | 2329 return; |
2360 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); | 2330 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); |
2361 uint32_t Alignment; | 2331 uint32_t Alignment; |
2362 extractAlignment("Alloca", Values[1], Alignment); | 2332 extractAlignment("Alloca", Values[1], Alignment); |
2363 if (isIRGenerationDisabled()) { | 2333 if (isIRGenerationDisabled()) { |
2364 assert(ByteCount == nullptr); | 2334 assert(ByteCount == nullptr); |
2365 setNextLocalInstIndex(nullptr); | 2335 setNextLocalInstIndex(nullptr); |
2366 return; | 2336 return; |
2367 } | 2337 } |
2368 Ice::Type PtrTy = Context->getIcePointerType(); | 2338 Ice::Type PtrTy = Ice::getPointerType(); |
2369 if (ByteCount->getType() != Ice::IceType_i32) { | 2339 if (ByteCount->getType() != Ice::IceType_i32) { |
2370 std::string Buffer; | 2340 std::string Buffer; |
2371 raw_string_ostream StrBuf(Buffer); | 2341 raw_string_ostream StrBuf(Buffer); |
2372 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; | 2342 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; |
2373 Error(StrBuf.str()); | 2343 Error(StrBuf.str()); |
2374 appendErrorInstruction(PtrTy); | 2344 appendErrorInstruction(PtrTy); |
2375 return; | 2345 return; |
2376 } | 2346 } |
2377 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment, | 2347 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment, |
2378 getNextInstVar(PtrTy))); | 2348 getNextInstVar(PtrTy))); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2648 case naclbitc::CST_CODE_INTEGER: { | 2618 case naclbitc::CST_CODE_INTEGER: { |
2649 // INTEGER: [intval] | 2619 // INTEGER: [intval] |
2650 if (!isValidRecordSize(1, "integer")) | 2620 if (!isValidRecordSize(1, "integer")) |
2651 return; | 2621 return; |
2652 if (!isValidNextConstantType()) | 2622 if (!isValidNextConstantType()) |
2653 return; | 2623 return; |
2654 if (isIRGenerationDisabled()) { | 2624 if (isIRGenerationDisabled()) { |
2655 FuncParser->setNextConstantID(nullptr); | 2625 FuncParser->setNextConstantID(nullptr); |
2656 return; | 2626 return; |
2657 } | 2627 } |
2658 if (auto IType = dyn_cast<IntegerType>( | 2628 if (Ice::isScalarIntegerType(NextConstantType)) { |
2659 Context->convertToLLVMType(NextConstantType))) { | 2629 Ice::APInt Value(Ice::getScalarIntBitWidth(NextConstantType), |
2660 Ice::APInt Value(IType->getBitWidth(), | |
2661 NaClDecodeSignRotatedValue(Values[0])); | 2630 NaClDecodeSignRotatedValue(Values[0])); |
2662 if (Ice::Constant *C = getContext()->getConstantInt( | 2631 if (Ice::Constant *C = getContext()->getConstantInt( |
2663 NextConstantType, Value.getSExtValue())) { | 2632 NextConstantType, Value.getSExtValue())) { |
2664 FuncParser->setNextConstantID(C); | 2633 FuncParser->setNextConstantID(C); |
2665 return; | 2634 return; |
2666 } | 2635 } |
2667 } | 2636 } |
2668 std::string Buffer; | 2637 std::string Buffer; |
2669 raw_string_ostream StrBuf(Buffer); | 2638 raw_string_ostream StrBuf(Buffer); |
2670 StrBuf << "constant block integer record for non-integer type " | 2639 StrBuf << "constant block integer record for non-integer type " |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3035 | 3004 |
3036 if (TopLevelBlocks != 1) { | 3005 if (TopLevelBlocks != 1) { |
3037 errs() << IRFilename | 3006 errs() << IRFilename |
3038 << ": Contains more than one module. Found: " << TopLevelBlocks | 3007 << ": Contains more than one module. Found: " << TopLevelBlocks |
3039 << "\n"; | 3008 << "\n"; |
3040 ErrorStatus = true; | 3009 ErrorStatus = true; |
3041 } | 3010 } |
3042 } | 3011 } |
3043 | 3012 |
3044 } // end of namespace Ice | 3013 } // end of namespace Ice |
OLD | NEW |