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" | |
21 #include "llvm/IR/Module.h" | |
22 #include "llvm/Support/Format.h" | 20 #include "llvm/Support/Format.h" |
23 #include "llvm/Support/MemoryBuffer.h" | 21 #include "llvm/Support/MemoryBuffer.h" |
24 #include "llvm/Support/raw_ostream.h" | 22 #include "llvm/Support/raw_ostream.h" |
25 | 23 |
26 #include "IceAPInt.h" | 24 #include "IceAPInt.h" |
27 #include "IceAPFloat.h" | 25 #include "IceAPFloat.h" |
28 #include "IceCfg.h" | 26 #include "IceCfg.h" |
29 #include "IceCfgNode.h" | 27 #include "IceCfgNode.h" |
30 #include "IceClFlags.h" | 28 #include "IceClFlags.h" |
31 #include "IceDefs.h" | 29 #include "IceDefs.h" |
32 #include "IceGlobalInits.h" | 30 #include "IceGlobalInits.h" |
33 #include "IceInst.h" | 31 #include "IceInst.h" |
34 #include "IceOperand.h" | 32 #include "IceOperand.h" |
35 #include "IceTypeConverter.h" | |
36 #include "PNaClTranslator.h" | 33 #include "PNaClTranslator.h" |
37 | 34 |
38 #include <memory> | 35 #include <memory> |
39 | 36 |
40 namespace { | 37 namespace { |
41 using namespace llvm; | 38 using namespace llvm; |
42 | 39 |
43 // TODO(kschimpf) Remove error recovery once implementation complete. | 40 // TODO(kschimpf) Remove error recovery once implementation complete. |
44 static cl::opt<bool> AllowErrorRecovery( | 41 static cl::opt<bool> AllowErrorRecovery( |
45 "allow-pnacl-reader-error-recovery", | 42 "allow-pnacl-reader-error-recovery", |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 class BlockParserBaseClass; | 160 class BlockParserBaseClass; |
164 | 161 |
165 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 162 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
166 class TopLevelParser : public NaClBitcodeParser { | 163 class TopLevelParser : public NaClBitcodeParser { |
167 TopLevelParser(const TopLevelParser &) = delete; | 164 TopLevelParser(const TopLevelParser &) = delete; |
168 TopLevelParser &operator=(const TopLevelParser &) = delete; | 165 TopLevelParser &operator=(const TopLevelParser &) = delete; |
169 | 166 |
170 public: | 167 public: |
171 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 168 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
172 | 169 |
173 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, | 170 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, |
174 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, | 171 NaClBitstreamCursor &Cursor, bool &ErrorStatus) |
175 bool &ErrorStatus) | 172 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), |
176 : NaClBitcodeParser(Cursor), Translator(Translator), | 173 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
177 Mod(new Module(InputName, getGlobalContext())), Header(Header), | 174 NumFunctionBlocks(0), BlockParser(nullptr) { |
178 TypeConverter(Mod->getContext()), ErrorStatus(ErrorStatus), | |
179 NumErrors(0), NumFunctionIds(0), NumFunctionBlocks(0), | |
180 BlockParser(nullptr) { | |
181 setErrStream(Translator.getContext()->getStrDump()); | 175 setErrStream(Translator.getContext()->getStrDump()); |
182 } | 176 } |
183 | 177 |
184 ~TopLevelParser() override {} | 178 ~TopLevelParser() override {} |
185 | 179 |
186 Ice::Translator &getTranslator() { return Translator; } | 180 Ice::Translator &getTranslator() { return Translator; } |
187 | 181 |
188 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 182 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
189 BlockParser = NewBlockParser; | 183 BlockParser = NewBlockParser; |
190 } | 184 } |
191 | 185 |
192 // Generates error with given Message. Always returns true. | 186 // Generates error with given Message. Always returns true. |
193 bool Error(const std::string &Message) override; | 187 bool Error(const std::string &Message) override; |
194 | 188 |
195 // Generates error message with respect to the current block parser. | 189 // Generates error message with respect to the current block parser. |
196 bool BlockError(const std::string &Message); | 190 bool BlockError(const std::string &Message); |
197 | 191 |
198 /// Returns the number of errors found while parsing the bitcode | 192 /// Returns the number of errors found while parsing the bitcode |
199 /// file. | 193 /// file. |
200 unsigned getNumErrors() const { return NumErrors; } | 194 unsigned getNumErrors() const { return NumErrors; } |
201 | 195 |
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. | 196 /// Returns the number of bytes in the bitcode header. |
206 size_t getHeaderSize() const { return Header.getHeaderSize(); } | 197 size_t getHeaderSize() const { return Header.getHeaderSize(); } |
207 | 198 |
208 /// Changes the size of the type list to the given size. | 199 /// Changes the size of the type list to the given size. |
209 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } | 200 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } |
210 | 201 |
211 /// Returns true if generation of Subzero IR is disabled. | 202 /// Returns true if generation of Subzero IR is disabled. |
212 bool isIRGenerationDisabled() const { | 203 bool isIRGenerationDisabled() const { |
213 return ALLOW_DISABLE_IR_GEN ? Translator.getFlags().DisableIRGeneration | 204 return ALLOW_DISABLE_IR_GEN ? Translator.getFlags().DisableIRGeneration |
214 : false; | 205 : false; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 return getFunctionByID(Index); | 361 return getFunctionByID(Index); |
371 else | 362 else |
372 return getGlobalVariableByID(Index - NumFunctionIds); | 363 return getGlobalVariableByID(Index - NumFunctionIds); |
373 } | 364 } |
374 | 365 |
375 /// Returns the list of parsed global variable declarations. | 366 /// Returns the list of parsed global variable declarations. |
376 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { | 367 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { |
377 return VariableDeclarations; | 368 return VariableDeclarations; |
378 } | 369 } |
379 | 370 |
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: | 371 private: |
400 // The translator associated with the parser. | 372 // The translator associated with the parser. |
401 Ice::Translator &Translator; | 373 Ice::Translator &Translator; |
402 // The parsed module. | |
403 std::unique_ptr<Module> Mod; | |
404 // The bitcode header. | 374 // The bitcode header. |
405 NaClBitcodeHeader &Header; | 375 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. | 376 // The exit status that should be set to true if an error occurs. |
409 bool &ErrorStatus; | 377 bool &ErrorStatus; |
410 // The number of errors reported. | 378 // The number of errors reported. |
411 unsigned NumErrors; | 379 unsigned NumErrors; |
412 // The types associated with each type ID. | 380 // The types associated with each type ID. |
413 std::vector<ExtendedType> TypeIDValues; | 381 std::vector<ExtendedType> TypeIDValues; |
414 // The set of functions. | 382 // The set of functions. |
415 FunctionDeclarationListType FunctionDeclarationList; | 383 FunctionDeclarationListType FunctionDeclarationList; |
416 // The set of global variables. | 384 // The set of global variables. |
417 Ice::Translator::VariableDeclarationListType VariableDeclarations; | 385 Ice::Translator::VariableDeclarationListType VariableDeclarations; |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1400 if (Ice::isFloatingType(OpTy)) | 1368 if (Ice::isFloatingType(OpTy)) |
1401 return true; | 1369 return true; |
1402 ReportInvalidBinaryOp(Op, OpTy); | 1370 ReportInvalidBinaryOp(Op, OpTy); |
1403 return false; | 1371 return false; |
1404 } | 1372 } |
1405 | 1373 |
1406 // Checks if the type of operand Op is the valid pointer type, for | 1374 // Checks if the type of operand Op is the valid pointer type, for |
1407 // the given InstructionName. Returns true if valid. Otherwise | 1375 // the given InstructionName. Returns true if valid. Otherwise |
1408 // generates an error message and returns false. | 1376 // generates an error message and returns false. |
1409 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { | 1377 bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { |
1410 Ice::Type PtrType = Context->getIcePointerType(); | 1378 Ice::Type PtrType = Ice::getPointerType(); |
1411 if (Op->getType() == PtrType) | 1379 if (Op->getType() == PtrType) |
1412 return true; | 1380 return true; |
1413 std::string Buffer; | 1381 std::string Buffer; |
1414 raw_string_ostream StrBuf(Buffer); | 1382 raw_string_ostream StrBuf(Buffer); |
1415 StrBuf << InstructionName << " address not " << PtrType | 1383 StrBuf << InstructionName << " address not " << PtrType |
1416 << ". Found: " << *Op; | 1384 << ". Found: " << *Op; |
1417 Error(StrBuf.str()); | 1385 Error(StrBuf.str()); |
1418 return false; | 1386 return false; |
1419 } | 1387 } |
1420 | 1388 |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2358 if (!isValidRecordSize(2, "alloca")) | 2326 if (!isValidRecordSize(2, "alloca")) |
2359 return; | 2327 return; |
2360 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); | 2328 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); |
2361 uint32_t Alignment; | 2329 uint32_t Alignment; |
2362 extractAlignment("Alloca", Values[1], Alignment); | 2330 extractAlignment("Alloca", Values[1], Alignment); |
2363 if (isIRGenerationDisabled()) { | 2331 if (isIRGenerationDisabled()) { |
2364 assert(ByteCount == nullptr); | 2332 assert(ByteCount == nullptr); |
2365 setNextLocalInstIndex(nullptr); | 2333 setNextLocalInstIndex(nullptr); |
2366 return; | 2334 return; |
2367 } | 2335 } |
2368 Ice::Type PtrTy = Context->getIcePointerType(); | 2336 Ice::Type PtrTy = Ice::getPointerType(); |
2369 if (ByteCount->getType() != Ice::IceType_i32) { | 2337 if (ByteCount->getType() != Ice::IceType_i32) { |
2370 std::string Buffer; | 2338 std::string Buffer; |
2371 raw_string_ostream StrBuf(Buffer); | 2339 raw_string_ostream StrBuf(Buffer); |
2372 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; | 2340 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; |
2373 Error(StrBuf.str()); | 2341 Error(StrBuf.str()); |
2374 appendErrorInstruction(PtrTy); | 2342 appendErrorInstruction(PtrTy); |
2375 return; | 2343 return; |
2376 } | 2344 } |
2377 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment, | 2345 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment, |
2378 getNextInstVar(PtrTy))); | 2346 getNextInstVar(PtrTy))); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2648 case naclbitc::CST_CODE_INTEGER: { | 2616 case naclbitc::CST_CODE_INTEGER: { |
2649 // INTEGER: [intval] | 2617 // INTEGER: [intval] |
2650 if (!isValidRecordSize(1, "integer")) | 2618 if (!isValidRecordSize(1, "integer")) |
2651 return; | 2619 return; |
2652 if (!isValidNextConstantType()) | 2620 if (!isValidNextConstantType()) |
2653 return; | 2621 return; |
2654 if (isIRGenerationDisabled()) { | 2622 if (isIRGenerationDisabled()) { |
2655 FuncParser->setNextConstantID(nullptr); | 2623 FuncParser->setNextConstantID(nullptr); |
2656 return; | 2624 return; |
2657 } | 2625 } |
2658 if (auto IType = dyn_cast<IntegerType>( | 2626 if (Ice::isScalarIntegerType(NextConstantType)) { |
2659 Context->convertToLLVMType(NextConstantType))) { | 2627 Ice::APInt Value(Ice::getScalarIntBitWidth(NextConstantType), |
2660 Ice::APInt Value(IType->getBitWidth(), | |
2661 NaClDecodeSignRotatedValue(Values[0])); | 2628 NaClDecodeSignRotatedValue(Values[0])); |
2662 if (Ice::Constant *C = getContext()->getConstantInt( | 2629 if (Ice::Constant *C = getContext()->getConstantInt( |
2663 NextConstantType, Value.getSExtValue())) { | 2630 NextConstantType, Value.getSExtValue())) { |
2664 FuncParser->setNextConstantID(C); | 2631 FuncParser->setNextConstantID(C); |
2665 return; | 2632 return; |
2666 } | 2633 } |
2667 } | 2634 } |
2668 std::string Buffer; | 2635 std::string Buffer; |
2669 raw_string_ostream StrBuf(Buffer); | 2636 raw_string_ostream StrBuf(Buffer); |
2670 StrBuf << "constant block integer record for non-integer type " | 2637 StrBuf << "constant block integer record for non-integer type " |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3015 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 2982 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
3016 errs() << "Invalid PNaCl bitcode header.\n"; | 2983 errs() << "Invalid PNaCl bitcode header.\n"; |
3017 ErrorStatus = true; | 2984 ErrorStatus = true; |
3018 return; | 2985 return; |
3019 } | 2986 } |
3020 | 2987 |
3021 // Create a bitstream reader to read the bitcode file. | 2988 // Create a bitstream reader to read the bitcode file. |
3022 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 2989 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
3023 NaClBitstreamCursor InputStream(InputStreamFile); | 2990 NaClBitstreamCursor InputStream(InputStreamFile); |
3024 | 2991 |
3025 TopLevelParser Parser(*this, MemBuf->getBufferIdentifier(), Header, | 2992 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); |
3026 InputStream, ErrorStatus); | |
3027 int TopLevelBlocks = 0; | 2993 int TopLevelBlocks = 0; |
3028 while (!InputStream.AtEndOfStream()) { | 2994 while (!InputStream.AtEndOfStream()) { |
3029 if (Parser.Parse()) { | 2995 if (Parser.Parse()) { |
3030 ErrorStatus = true; | 2996 ErrorStatus = true; |
3031 return; | 2997 return; |
3032 } | 2998 } |
3033 ++TopLevelBlocks; | 2999 ++TopLevelBlocks; |
3034 } | 3000 } |
3035 | 3001 |
3036 if (TopLevelBlocks != 1) { | 3002 if (TopLevelBlocks != 1) { |
3037 errs() << IRFilename | 3003 errs() << IRFilename |
3038 << ": Contains more than one module. Found: " << TopLevelBlocks | 3004 << ": Contains more than one module. Found: " << TopLevelBlocks |
3039 << "\n"; | 3005 << "\n"; |
3040 ErrorStatus = true; | 3006 ErrorStatus = true; |
3041 } | 3007 } |
3042 } | 3008 } |
3043 | 3009 |
3044 } // end of namespace Ice | 3010 } // end of namespace Ice |
OLD | NEW |