OLD | NEW |
1 //===- NaClBitcodeReader.cpp ----------------------------------------------===// | 1 //===- NaClBitcodeReader.cpp ----------------------------------------------===// |
2 // Internal NaClBitcodeReader implementation | 2 // Internal NaClBitcodeReader implementation |
3 // | 3 // |
4 // The LLVM Compiler Infrastructure | 4 // The LLVM Compiler Infrastructure |
5 // | 5 // |
6 // This file is distributed under the University of Illinois Open Source | 6 // This file is distributed under the University of Illinois Open Source |
7 // License. See LICENSE.TXT for details. | 7 // License. See LICENSE.TXT for details. |
8 // | 8 // |
9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
10 | 10 |
11 #define DEBUG_TYPE "NaClBitcodeReader" | 11 #define DEBUG_TYPE "NaClBitcodeReader" |
12 | 12 |
13 #include "llvm/ADT/SmallString.h" | 13 #include "llvm/ADT/SmallString.h" |
14 #include "llvm/ADT/SmallVector.h" | 14 #include "llvm/ADT/SmallVector.h" |
15 #include "llvm/Analysis/NaCl/PNaClABITypeChecker.h" | 15 #include "llvm/Analysis/NaCl/PNaClABITypeChecker.h" |
16 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" | 16 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
17 #include "llvm/Bitcode/NaCl/NaClBitcodeDefs.h" | 17 #include "llvm/Bitcode/NaCl/NaClBitcodeDefs.h" |
18 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" | 18 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" |
19 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" | 19 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" |
20 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 20 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 21 #include "llvm/Bitcode/ReaderWriter.h" |
21 #include "llvm/IR/AutoUpgrade.h" | 22 #include "llvm/IR/AutoUpgrade.h" |
22 #include "llvm/IR/Constants.h" | 23 #include "llvm/IR/Constants.h" |
23 #include "llvm/IR/DerivedTypes.h" | 24 #include "llvm/IR/DerivedTypes.h" |
| 25 #include "llvm/IR/DiagnosticPrinter.h" |
24 #include "llvm/IR/GVMaterializer.h" | 26 #include "llvm/IR/GVMaterializer.h" |
25 #include "llvm/IR/InlineAsm.h" | 27 #include "llvm/IR/InlineAsm.h" |
26 #include "llvm/IR/IntrinsicInst.h" | 28 #include "llvm/IR/IntrinsicInst.h" |
| 29 #include "llvm/IR/LLVMContext.h" |
27 #include "llvm/IR/Module.h" | 30 #include "llvm/IR/Module.h" |
28 #include "llvm/IR/OperandTraits.h" | 31 #include "llvm/IR/OperandTraits.h" |
29 #include "llvm/IR/Operator.h" | 32 #include "llvm/IR/Operator.h" |
30 #include "llvm/Analysis/NaCl/PNaClAllowedIntrinsics.h" | 33 #include "llvm/Analysis/NaCl/PNaClAllowedIntrinsics.h" |
31 #include "llvm/IR/ValueHandle.h" | 34 #include "llvm/IR/ValueHandle.h" |
32 #include "llvm/Support/DataStream.h" | 35 #include "llvm/Support/DataStream.h" |
33 #include "llvm/Support/Debug.h" | 36 #include "llvm/Support/Debug.h" |
34 #include "llvm/Support/MathExtras.h" | 37 #include "llvm/Support/MathExtras.h" |
35 #include "llvm/Support/MemoryBuffer.h" | 38 #include "llvm/Support/MemoryBuffer.h" |
36 #include "llvm/Support/raw_ostream.h" | 39 #include "llvm/Support/raw_ostream.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 Value *getValueFwdRef(NaClBcIndexSize_t Idx); | 126 Value *getValueFwdRef(NaClBcIndexSize_t Idx); |
124 | 127 |
125 // Assigns V to value index Idx. | 128 // Assigns V to value index Idx. |
126 void AssignValue(Value *V, NaClBcIndexSize_t Idx); | 129 void AssignValue(Value *V, NaClBcIndexSize_t Idx); |
127 | 130 |
128 // Assigns Idx to the given value, overwriting the existing entry | 131 // Assigns Idx to the given value, overwriting the existing entry |
129 // and possibly modifying the type of the entry. | 132 // and possibly modifying the type of the entry. |
130 void OverwriteValue(Value *V, NaClBcIndexSize_t Idx); | 133 void OverwriteValue(Value *V, NaClBcIndexSize_t Idx); |
131 }; | 134 }; |
132 | 135 |
| 136 static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F, |
| 137 LLVMContext &C) { |
| 138 if (F) |
| 139 return F; |
| 140 return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); }; |
| 141 } |
| 142 |
133 class NaClBitcodeReader : public GVMaterializer { | 143 class NaClBitcodeReader : public GVMaterializer { |
134 NaClBitcodeHeader Header; // Header fields of the PNaCl bitcode file. | 144 NaClBitcodeHeader Header; // Header fields of the PNaCl bitcode file. |
135 LLVMContext &Context; | 145 LLVMContext &Context; |
| 146 DiagnosticHandlerFunction DiagnosticHandler; |
136 Module *TheModule; | 147 Module *TheModule; |
137 // If non-null, stream to write verbose errors to. | |
138 raw_ostream *Verbose; | |
139 PNaClAllowedIntrinsics AllowedIntrinsics; | 148 PNaClAllowedIntrinsics AllowedIntrinsics; |
140 std::unique_ptr<MemoryBuffer> Buffer; | 149 std::unique_ptr<MemoryBuffer> Buffer; |
141 std::unique_ptr<NaClBitstreamReader> StreamFile; | 150 std::unique_ptr<NaClBitstreamReader> StreamFile; |
142 NaClBitstreamCursor Stream; | 151 NaClBitstreamCursor Stream; |
143 StreamingMemoryObject *LazyStreamer; | 152 StreamingMemoryObject *LazyStreamer; |
144 uint64_t NextUnreadBit; | 153 uint64_t NextUnreadBit; |
145 bool SeenValueSymbolTable; | 154 bool SeenValueSymbolTable; |
146 std::vector<Type*> TypeList; | 155 std::vector<Type*> TypeList; |
147 NaClBitcodeReaderValueList ValueList; | 156 NaClBitcodeReaderValueList ValueList; |
148 | 157 |
(...skipping 29 matching lines...) Expand all Loading... |
178 /// When function bodies are initially scanned, this map contains info about | 187 /// When function bodies are initially scanned, this map contains info about |
179 /// where to find deferred function body in the stream. | 188 /// where to find deferred function body in the stream. |
180 DenseMap<Function*, uint64_t> DeferredFunctionInfo; | 189 DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
181 | 190 |
182 /// True if we should only accept supported bitcode format. | 191 /// True if we should only accept supported bitcode format. |
183 bool AcceptSupportedBitcodeOnly; | 192 bool AcceptSupportedBitcodeOnly; |
184 | 193 |
185 /// Integer type use for PNaCl conversion of pointers. | 194 /// Integer type use for PNaCl conversion of pointers. |
186 Type *IntPtrType; | 195 Type *IntPtrType; |
187 | 196 |
188 static const std::error_category &BitcodeErrorCategory(); | |
189 | |
190 public: | 197 public: |
191 | |
192 /// Types of errors reported. | |
193 enum ErrorType { | |
194 CouldNotFindFunctionInStream // Unable to find function in bitcode stream. | |
195 = 1, // Note: Error types must not be zero! | |
196 InsufficientFunctionProtos, | |
197 InvalidBitstream, // Error in bitstream format. | |
198 InvalidBlock, // Invalid block found in bitcode. | |
199 InvalidConstantReference, // Bad constant reference. | |
200 InvalidDataAfterModule, // Invalid data after module. | |
201 InvalidInstructionWithNoBB, // No basic block for instruction. | |
202 InvalidMultipleBlocks, // Multiple blocks for a kind of block that should | |
203 // have only one. | |
204 InvalidRecord, // Record doesn't have expected size or structure. | |
205 InvalidSkippedBlock, // Unable to skip unknown block in bitcode file. | |
206 InvalidType, // Invalid type in record. | |
207 InvalidTypeForValue, // Type of value incorrect. | |
208 InvalidValue, // Invalid value in record. | |
209 MalformedBlock // Unable to advance over block. | |
210 }; | |
211 | |
212 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, | 198 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
213 raw_ostream *Verbose, | 199 DiagnosticHandlerFunction DiagnosticHandler, |
214 bool AcceptSupportedOnly) | 200 bool AcceptSupportedOnly) |
215 : Context(C), TheModule(nullptr), Verbose(Verbose), AllowedIntrinsics(&C), | 201 : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), |
216 Buffer(buffer), | 202 TheModule(nullptr), AllowedIntrinsics(&C), Buffer(buffer), |
217 LazyStreamer(nullptr), NextUnreadBit(0), SeenValueSymbolTable(false), | 203 LazyStreamer(nullptr), NextUnreadBit(0), SeenValueSymbolTable(false), |
218 ValueList(), | 204 ValueList(), SeenFirstFunctionBody(false), |
219 SeenFirstFunctionBody(false), | |
220 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 205 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
221 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 206 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) {} |
222 } | 207 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, LLVMContext &C, |
223 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, | 208 DiagnosticHandlerFunction DiagnosticHandler, |
224 LLVMContext &C, | |
225 raw_ostream *Verbose, | |
226 bool AcceptSupportedOnly) | 209 bool AcceptSupportedOnly) |
227 : Context(C), TheModule(nullptr), Verbose(Verbose), AllowedIntrinsics(&C), | 210 : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), |
228 Buffer(nullptr), | 211 TheModule(nullptr), AllowedIntrinsics(&C), Buffer(nullptr), |
229 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), | 212 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), |
230 ValueList(), | 213 ValueList(), SeenFirstFunctionBody(false), |
231 SeenFirstFunctionBody(false), | |
232 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 214 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
233 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 215 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) {} |
234 } | |
235 ~NaClBitcodeReader() override { | 216 ~NaClBitcodeReader() override { |
236 FreeState(); | 217 FreeState(); |
237 } | 218 } |
238 | 219 |
239 void FreeState(); | 220 void FreeState(); |
240 | 221 |
241 bool isDematerializable(const GlobalValue *GV) const override; | 222 bool isDematerializable(const GlobalValue *GV) const override; |
242 std::error_code materialize(GlobalValue *GV) override; | 223 std::error_code materialize(GlobalValue *GV) override; |
243 std::error_code MaterializeModule(Module *M) override; | 224 std::error_code MaterializeModule(Module *M) override; |
244 std::vector<StructType *> getIdentifiedStructTypes() const override; | 225 std::vector<StructType *> getIdentifiedStructTypes() const override; |
245 void Dematerialize(GlobalValue *GV) override; | 226 void Dematerialize(GlobalValue *GV) override; |
246 void releaseBuffer(); | 227 void releaseBuffer(); |
247 | 228 |
248 std::error_code Error(ErrorType E) const { | 229 std::error_code Error(std::error_code EC, const Twine &Message) const; |
249 return std::error_code(E, BitcodeErrorCategory()); | 230 |
| 231 std::error_code Error(const Twine &Message) const { |
| 232 return Error(make_error_code(BitcodeError::CorruptedBitcode), Message); |
250 } | 233 } |
251 | 234 |
252 /// Generates the corresponding verbose Message, then generates error. | |
253 std::error_code Error(ErrorType E, const std::string &Message) const; | |
254 | |
255 /// Main interface to parsing a bitcode buffer. returns true if an error | 235 /// Main interface to parsing a bitcode buffer. returns true if an error |
256 /// occurred. | 236 /// occurred. |
257 std::error_code ParseBitcodeInto(Module *M); | 237 std::error_code ParseBitcodeInto(Module *M); |
258 | 238 |
259 /// Convert alignment exponent (i.e. power of two (or zero)) to the | 239 /// Convert alignment exponent (i.e. power of two (or zero)) to the |
260 /// corresponding alignment to use. If alignment is too large, it generates | 240 /// corresponding alignment to use. If alignment is too large, it generates |
261 /// an error message and returns corresponding error code. | 241 /// an error message and returns corresponding error code. |
262 std::error_code getAlignmentValue(uint64_t Exponent, unsigned &Alignment); | 242 std::error_code getAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
263 | 243 |
264 // GVMaterializer interface. It's a no-op for PNaCl bitcode, which has no | 244 // GVMaterializer interface. It's a no-op for PNaCl bitcode, which has no |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 425 |
446 // No type specified, must be invalid reference. | 426 // No type specified, must be invalid reference. |
447 if (Ty == 0) | 427 if (Ty == 0) |
448 return true; | 428 return true; |
449 | 429 |
450 // Create a placeholder, which will later be RAUW'd. | 430 // Create a placeholder, which will later be RAUW'd. |
451 ValuePtrs[Idx] = new Argument(Ty); | 431 ValuePtrs[Idx] = new Argument(Ty); |
452 return false; | 432 return false; |
453 } | 433 } |
454 | 434 |
455 class NaClBitcodeErrorCategoryType : public std::error_category { | |
456 const char *name() const LLVM_NOEXCEPT override { | |
457 return "pnacl.bitcode"; | |
458 } | |
459 std::string message(int IndexError) const override { | |
460 switch(static_cast<NaClBitcodeReader::ErrorType>(IndexError)) { | |
461 case NaClBitcodeReader::CouldNotFindFunctionInStream: | |
462 return "Unable to find function in bitcode stream."; | |
463 case NaClBitcodeReader::InsufficientFunctionProtos: | |
464 return "Insufficient function protos"; | |
465 case NaClBitcodeReader::InvalidBitstream: | |
466 return "Error in bitstream format"; | |
467 case NaClBitcodeReader::InvalidBlock: | |
468 return "Invalid block found in bitcode file"; | |
469 case NaClBitcodeReader::InvalidConstantReference: | |
470 return "Bad constant reference"; | |
471 case NaClBitcodeReader::InvalidDataAfterModule: | |
472 return "Invalid data after module"; | |
473 case NaClBitcodeReader::InvalidInstructionWithNoBB: | |
474 return "No basic block for instruction"; | |
475 case NaClBitcodeReader::InvalidMultipleBlocks: | |
476 return "Multiple blocks for a kind of block that should have only one"; | |
477 case NaClBitcodeReader::InvalidRecord: | |
478 return "Record doesn't have expected size or structure"; | |
479 case NaClBitcodeReader::InvalidSkippedBlock: | |
480 return "Unable to skip unknown block in bitcode file"; | |
481 case NaClBitcodeReader::InvalidType: | |
482 return "Invalid type in record"; | |
483 case NaClBitcodeReader::InvalidTypeForValue: | |
484 return "Type of value in record incorrect"; | |
485 case NaClBitcodeReader::InvalidValue: | |
486 return "Invalid value in record"; | |
487 case NaClBitcodeReader::MalformedBlock: | |
488 return "Malformed block. Unable to advance over block"; | |
489 } | |
490 llvm_unreachable("Unknown error type!"); | |
491 } | |
492 }; | |
493 | |
494 const std::error_category &NaClBitcodeReader::BitcodeErrorCategory() { | |
495 static NaClBitcodeErrorCategoryType ErrCat; | |
496 return ErrCat; | |
497 } | |
498 | |
499 Type *NaClBitcodeReader::getTypeByID(NaClBcIndexSize_t ID) { | 435 Type *NaClBitcodeReader::getTypeByID(NaClBcIndexSize_t ID) { |
500 // The type table size is always specified correctly. | 436 // The type table size is always specified correctly. |
501 if (ID >= TypeList.size()) | 437 if (ID >= TypeList.size()) |
502 return 0; | 438 return 0; |
503 | 439 |
504 if (Type *Ty = TypeList[ID]) | 440 if (Type *Ty = TypeList[ID]) |
505 return Ty; | 441 return Ty; |
506 | 442 |
507 // If we have a forward reference, the only possible case is when it is to a | 443 // If we have a forward reference, the only possible case is when it is to a |
508 // named struct. Just create a placeholder for now. | 444 // named struct. Just create a placeholder for now. |
509 return TypeList[ID] = StructType::create(Context); | 445 return TypeList[ID] = StructType::create(Context); |
510 } | 446 } |
511 | 447 |
512 | 448 |
513 //===----------------------------------------------------------------------===// | 449 //===----------------------------------------------------------------------===// |
514 // Functions for parsing blocks from the bitcode file | 450 // Functions for parsing blocks from the bitcode file |
515 //===----------------------------------------------------------------------===// | 451 //===----------------------------------------------------------------------===// |
516 | 452 |
517 static const unsigned MaxAlignmentExponent = 29; | 453 static const unsigned MaxAlignmentExponent = 29; |
518 static_assert( | 454 static_assert( |
519 (1u << MaxAlignmentExponent) == Value::MaximumAlignment, | 455 (1u << MaxAlignmentExponent) == Value::MaximumAlignment, |
520 "Inconsistency between Value.MaxAlignment and PNaCl alignment limit"); | 456 "Inconsistency between Value.MaxAlignment and PNaCl alignment limit"); |
521 | 457 |
522 std::error_code NaClBitcodeReader::Error(ErrorType E, | 458 std::error_code NaClBitcodeReader::Error(std::error_code EC, |
523 const std::string &Message) const { | 459 const Twine &Message) const { |
524 if (Verbose) { | 460 std::string Buffer; |
525 naclbitc::ErrorAt(*Verbose, naclbitc::Error, Stream.GetCurrentBitNo()) | 461 raw_string_ostream StrBuf(Buffer); |
526 << Message << "\n"; | 462 StrBuf << "(" << naclbitc::getBitAddress(Stream.GetCurrentBitNo()) << ") " |
527 } | 463 << Message; |
528 return Error(E); | 464 BitcodeDiagnosticInfo DI(EC, DS_Error, StrBuf.str()); |
| 465 DiagnosticHandler(DI); |
| 466 return EC; |
529 } | 467 } |
530 | 468 |
531 std::error_code NaClBitcodeReader::getAlignmentValue( | 469 std::error_code NaClBitcodeReader::getAlignmentValue( |
532 uint64_t Exponent, unsigned &Alignment) { | 470 uint64_t Exponent, unsigned &Alignment) { |
533 if (Exponent > MaxAlignmentExponent + 1) { | 471 if (Exponent > MaxAlignmentExponent + 1) { |
534 std::string Buffer; | 472 std::string Buffer; |
535 raw_string_ostream StrBuf(Buffer); | 473 raw_string_ostream StrBuf(Buffer); |
536 StrBuf << "Alignment can't be greater than 2**" << MaxAlignmentExponent | 474 StrBuf << "Alignment can't be greater than 2**" << MaxAlignmentExponent |
537 << ". Found: 2**" << (Exponent - 1); | 475 << ". Found: 2**" << (Exponent - 1); |
538 return Error(InvalidValue, StrBuf.str()); | 476 return Error(StrBuf.str()); |
539 } | 477 } |
540 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; | 478 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; |
541 return std::error_code(); | 479 return std::error_code(); |
542 } | 480 } |
543 | 481 |
544 std::error_code NaClBitcodeReader::ParseTypeTable() { | 482 std::error_code NaClBitcodeReader::ParseTypeTable() { |
545 DEBUG(dbgs() << "-> ParseTypeTable\n"); | 483 DEBUG(dbgs() << "-> ParseTypeTable\n"); |
546 if (Stream.EnterSubBlock(naclbitc::TYPE_BLOCK_ID_NEW)) | 484 if (Stream.EnterSubBlock(naclbitc::TYPE_BLOCK_ID_NEW)) |
547 return Error(InvalidRecord, "Malformed block record"); | 485 return Error("Malformed block record"); |
548 | 486 |
549 std::error_code result = ParseTypeTableBody(); | 487 std::error_code result = ParseTypeTableBody(); |
550 if (!result) | 488 if (!result) |
551 DEBUG(dbgs() << "<- ParseTypeTable\n"); | 489 DEBUG(dbgs() << "<- ParseTypeTable\n"); |
552 return result; | 490 return result; |
553 } | 491 } |
554 | 492 |
555 std::error_code NaClBitcodeReader::ParseTypeTableBody() { | 493 std::error_code NaClBitcodeReader::ParseTypeTableBody() { |
556 if (!TypeList.empty()) | 494 if (!TypeList.empty()) |
557 return Error(InvalidMultipleBlocks, "Multiple TYPE_BLOCKs found!"); | 495 return Error("Multiple TYPE_BLOCKs found!"); |
558 | 496 |
559 SmallVector<uint64_t, 64> Record; | 497 SmallVector<uint64_t, 64> Record; |
560 NaClBcIndexSize_t NumRecords = 0; | 498 NaClBcIndexSize_t NumRecords = 0; |
561 NaClBcIndexSize_t ExpectedNumRecords = 0; | 499 NaClBcIndexSize_t ExpectedNumRecords = 0; |
562 | 500 |
563 // Read all the records for this type table. | 501 // Read all the records for this type table. |
564 while (1) { | 502 while (1) { |
565 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); | 503 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); |
566 | 504 |
567 switch (Entry.Kind) { | 505 switch (Entry.Kind) { |
568 case NaClBitstreamEntry::SubBlock: | 506 case NaClBitstreamEntry::SubBlock: |
569 return Error(InvalidBlock, "Invalid block found in the types block"); | 507 return Error("Invalid block found in the types block"); |
570 case NaClBitstreamEntry::Error: | 508 case NaClBitstreamEntry::Error: |
571 return Error(MalformedBlock, "Malformed types block"); | 509 return Error("Malformed types block"); |
572 case NaClBitstreamEntry::EndBlock: | 510 case NaClBitstreamEntry::EndBlock: |
573 if (NumRecords != ExpectedNumRecords) | 511 if (NumRecords != ExpectedNumRecords) |
574 return Error(MalformedBlock, | 512 return Error("Invalid forward reference in the types block"); |
575 "Invalid forward reference in the types block"); | |
576 return std::error_code(); | 513 return std::error_code(); |
577 case NaClBitstreamEntry::Record: | 514 case NaClBitstreamEntry::Record: |
578 // The interesting case. | 515 // The interesting case. |
579 break; | 516 break; |
580 } | 517 } |
581 | 518 |
582 // Read a record. | 519 // Read a record. |
583 Record.clear(); | 520 Record.clear(); |
584 Type *ResultTy = 0; | 521 Type *ResultTy = 0; |
585 unsigned TypeCode = Stream.readRecord(Entry.ID, Record); | 522 unsigned TypeCode = Stream.readRecord(Entry.ID, Record); |
586 switch (TypeCode) { | 523 switch (TypeCode) { |
587 default: { | 524 default: { |
588 std::string Message; | 525 std::string Message; |
589 raw_string_ostream StrM(Message); | 526 raw_string_ostream StrM(Message); |
590 StrM << "Unknown type code in type table: " << TypeCode; | 527 StrM << "Unknown type code in type table: " << TypeCode; |
591 StrM.flush(); | 528 StrM.flush(); |
592 return Error(InvalidValue, Message); | 529 return Error(Message); |
593 } | 530 } |
594 | 531 |
595 case naclbitc::TYPE_CODE_NUMENTRY: { // TYPE_CODE_NUMENTRY: [numentries] | 532 case naclbitc::TYPE_CODE_NUMENTRY: { // TYPE_CODE_NUMENTRY: [numentries] |
596 // TYPE_CODE_NUMENTRY contains a count of the number of types in the | 533 // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
597 // type list. This allows us to reserve space. | 534 // type list. This allows us to reserve space. |
598 if (Record.size() != 1) | 535 if (Record.size() != 1) |
599 return Error(InvalidRecord, "Invalid TYPE_CODE_NUMENTRY record"); | 536 return Error("Invalid TYPE_CODE_NUMENTRY record"); |
600 uint64_t Size = Record[0]; | 537 uint64_t Size = Record[0]; |
601 if (Size > NaClBcIndexSize_t_Max) | 538 if (Size > NaClBcIndexSize_t_Max) |
602 return Error(InvalidValue, "Size too big in TYPE_CODE_NUMENTRY record"); | 539 return Error("Size too big in TYPE_CODE_NUMENTRY record"); |
603 // The code double checks that Expected size and the actual size | 540 // The code double checks that Expected size and the actual size |
604 // at the end of the block. To reduce allocations we preallocate | 541 // at the end of the block. To reduce allocations we preallocate |
605 // the space. | 542 // the space. |
606 // | 543 // |
607 // However, if the number is large, we suspect that the number | 544 // However, if the number is large, we suspect that the number |
608 // is (possibly) incorrect. In that case, we preallocate a | 545 // is (possibly) incorrect. In that case, we preallocate a |
609 // smaller space. | 546 // smaller space. |
610 TypeList.resize(std::min(Size, (uint64_t) 1000000)); | 547 TypeList.resize(std::min(Size, (uint64_t) 1000000)); |
611 ExpectedNumRecords = Size; | 548 ExpectedNumRecords = Size; |
612 // No type was defined, skip the checks that follow the switch. | 549 // No type was defined, skip the checks that follow the switch. |
613 continue; | 550 continue; |
614 } | 551 } |
615 case naclbitc::TYPE_CODE_VOID: // VOID | 552 case naclbitc::TYPE_CODE_VOID: // VOID |
616 if (Record.size() != 0) | 553 if (Record.size() != 0) |
617 return Error(InvalidRecord, "Invalid TYPE_CODE_VOID record"); | 554 return Error("Invalid TYPE_CODE_VOID record"); |
618 ResultTy = Type::getVoidTy(Context); | 555 ResultTy = Type::getVoidTy(Context); |
619 break; | 556 break; |
620 | 557 |
621 case naclbitc::TYPE_CODE_FLOAT: // FLOAT | 558 case naclbitc::TYPE_CODE_FLOAT: // FLOAT |
622 if (Record.size() != 0) | 559 if (Record.size() != 0) |
623 return Error(InvalidRecord, "Invalid TYPE_CODE_FLOAT record"); | 560 return Error("Invalid TYPE_CODE_FLOAT record"); |
624 ResultTy = Type::getFloatTy(Context); | 561 ResultTy = Type::getFloatTy(Context); |
625 break; | 562 break; |
626 | 563 |
627 case naclbitc::TYPE_CODE_DOUBLE: // DOUBLE | 564 case naclbitc::TYPE_CODE_DOUBLE: // DOUBLE |
628 if (Record.size() != 0) | 565 if (Record.size() != 0) |
629 return Error(InvalidRecord, "Invalid TYPE_CODE_DOUBLE record"); | 566 return Error("Invalid TYPE_CODE_DOUBLE record"); |
630 ResultTy = Type::getDoubleTy(Context); | 567 ResultTy = Type::getDoubleTy(Context); |
631 break; | 568 break; |
632 | 569 |
633 case naclbitc::TYPE_CODE_INTEGER: // INTEGER: [width] | 570 case naclbitc::TYPE_CODE_INTEGER: // INTEGER: [width] |
634 if (Record.size() != 1) | 571 if (Record.size() != 1) |
635 return Error(InvalidRecord, "Invalid TYPE_CODE_INTEGER record"); | 572 return Error("Invalid TYPE_CODE_INTEGER record"); |
636 // TODO(kschimpf): Should we check if Record[0] is in range? | 573 // TODO(kschimpf): Should we check if Record[0] is in range? |
637 ResultTy = IntegerType::get(Context, Record[0]); | 574 ResultTy = IntegerType::get(Context, Record[0]); |
638 break; | 575 break; |
639 | 576 |
640 case naclbitc::TYPE_CODE_FUNCTION: { | 577 case naclbitc::TYPE_CODE_FUNCTION: { |
641 // FUNCTION: [vararg, retty, paramty x N] | 578 // FUNCTION: [vararg, retty, paramty x N] |
642 if (Record.size() < 2) | 579 if (Record.size() < 2) |
643 return Error(InvalidRecord, "Invalid TYPE_CODE_FUNCTION record"); | 580 return Error("Invalid TYPE_CODE_FUNCTION record"); |
644 SmallVector<Type *, 8> ArgTys; | 581 SmallVector<Type *, 8> ArgTys; |
645 for (size_t i = 2, e = Record.size(); i != e; ++i) { | 582 for (size_t i = 2, e = Record.size(); i != e; ++i) { |
646 if (Type *T = getTypeByID(Record[i])) | 583 if (Type *T = getTypeByID(Record[i])) |
647 ArgTys.push_back(T); | 584 ArgTys.push_back(T); |
648 else | 585 else |
649 break; | 586 break; |
650 } | 587 } |
651 | 588 |
652 ResultTy = getTypeByID(Record[1]); | 589 ResultTy = getTypeByID(Record[1]); |
653 if (ResultTy == 0 || ArgTys.size() < Record.size() - 2) | 590 if (ResultTy == 0 || ArgTys.size() < Record.size() - 2) |
654 return Error(InvalidType, "invalid type in function type"); | 591 return Error("invalid type in function type"); |
655 | 592 |
656 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); | 593 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
657 break; | 594 break; |
658 } | 595 } |
659 case naclbitc::TYPE_CODE_VECTOR: { // VECTOR: [numelts, eltty] | 596 case naclbitc::TYPE_CODE_VECTOR: { // VECTOR: [numelts, eltty] |
660 if (Record.size() != 2) | 597 if (Record.size() != 2) |
661 return Error(InvalidRecord, "Invalid VECTOR type record"); | 598 return Error("Invalid VECTOR type record"); |
662 if ((ResultTy = getTypeByID(Record[1]))) | 599 if ((ResultTy = getTypeByID(Record[1]))) |
663 ResultTy = VectorType::get(ResultTy, Record[0]); | 600 ResultTy = VectorType::get(ResultTy, Record[0]); |
664 else | 601 else |
665 return Error(InvalidType, "invalid type in vector type"); | 602 return Error("invalid type in vector type"); |
666 break; | 603 break; |
667 } | 604 } |
668 } | 605 } |
669 | 606 |
670 if (NumRecords >= ExpectedNumRecords) | 607 if (NumRecords >= ExpectedNumRecords) |
671 return Error(MalformedBlock, "invalid TYPE table"); | 608 return Error("invalid TYPE table"); |
672 assert(ResultTy && "Didn't read a type?"); | 609 assert(ResultTy && "Didn't read a type?"); |
673 assert(TypeList[NumRecords] == 0 && "Already read type?"); | 610 assert(TypeList[NumRecords] == 0 && "Already read type?"); |
674 if (NumRecords == NaClBcIndexSize_t_Max) | 611 if (NumRecords == NaClBcIndexSize_t_Max) |
675 return Error(InvalidRecord, "Exceeded type index limit"); | 612 return Error("Exceeded type index limit"); |
676 TypeList[NumRecords++] = ResultTy; | 613 TypeList[NumRecords++] = ResultTy; |
677 } | 614 } |
678 return std::error_code(); | 615 return std::error_code(); |
679 } | 616 } |
680 | 617 |
681 // Class to process globals in two passes. In the first pass, build | 618 // Class to process globals in two passes. In the first pass, build |
682 // the corresponding global variables with no initializers. In the | 619 // the corresponding global variables with no initializers. In the |
683 // second pass, add initializers. The purpose of putting off | 620 // second pass, add initializers. The purpose of putting off |
684 // initializers is to make sure that we don't need to generate | 621 // initializers is to make sure that we don't need to generate |
685 // placeholders for relocation records, and the corresponding cost | 622 // placeholders for relocation records, and the corresponding cost |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 unsigned VarAlignment = 0; | 677 unsigned VarAlignment = 0; |
741 // True if the variable is read-only. | 678 // True if the variable is read-only. |
742 bool VarIsConstant = false; | 679 bool VarIsConstant = false; |
743 | 680 |
744 // Read all records to build global variables without initializers. | 681 // Read all records to build global variables without initializers. |
745 while (1) { | 682 while (1) { |
746 NaClBitstreamEntry Entry = | 683 NaClBitstreamEntry Entry = |
747 Stream.advance(NaClBitstreamCursor::AF_DontPopBlockAtEnd, nullptr); | 684 Stream.advance(NaClBitstreamCursor::AF_DontPopBlockAtEnd, nullptr); |
748 switch (Entry.Kind) { | 685 switch (Entry.Kind) { |
749 case NaClBitstreamEntry::SubBlock: | 686 case NaClBitstreamEntry::SubBlock: |
750 » return Reader.Error(NaClBitcodeReader::InvalidBlock, | 687 return Reader.Error("Invalid block in the global vars block"); |
751 » » » "Invalid block in the global vars block"); | |
752 case NaClBitstreamEntry::Error: | 688 case NaClBitstreamEntry::Error: |
753 return Reader.Error(NaClBitcodeReader::MalformedBlock, | 689 return Reader.Error("Error in the global vars block"); |
754 "Error in the global vars block"); | |
755 case NaClBitstreamEntry::EndBlock: | 690 case NaClBitstreamEntry::EndBlock: |
756 if (ProcessingGlobal || NumGlobals != (NextValueNo - FirstValueNo)) | 691 if (ProcessingGlobal || NumGlobals != (NextValueNo - FirstValueNo)) |
757 return Reader.Error(NaClBitcodeReader::MalformedBlock, | 692 return Reader.Error("Error in the global vars block"); |
758 "Error in the global vars block"); | |
759 return std::error_code(); | 693 return std::error_code(); |
760 case NaClBitstreamEntry::Record: | 694 case NaClBitstreamEntry::Record: |
761 // The interesting case. | 695 // The interesting case. |
762 break; | 696 break; |
763 } | 697 } |
764 | 698 |
765 // Read a record. | 699 // Read a record. |
766 Record.clear(); | 700 Record.clear(); |
767 unsigned Bitcode = Stream.readRecord(Entry.ID, Record); | 701 unsigned Bitcode = Stream.readRecord(Entry.ID, Record); |
768 switch (Bitcode) { | 702 switch (Bitcode) { |
769 default: | 703 default: |
770 return Reader.Error(NaClBitcodeReader::InvalidValue, | 704 return Reader.Error("Unknown global variable entry"); |
771 "Unknown global variable entry"); | |
772 case naclbitc::GLOBALVAR_VAR: | 705 case naclbitc::GLOBALVAR_VAR: |
773 // Start the definition of a global variable. | 706 // Start the definition of a global variable. |
774 if (ProcessingGlobal || Record.size() != 2) | 707 if (ProcessingGlobal || Record.size() != 2) |
775 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 708 return Reader.Error("Bad GLOBALVAR_VAR record"); |
776 "Bad GLOBALVAR_VAR record"); | |
777 ProcessingGlobal = true; | 709 ProcessingGlobal = true; |
778 if (std::error_code EC = | 710 if (std::error_code EC = |
779 Reader.getAlignmentValue(Record[0], VarAlignment)) | 711 Reader.getAlignmentValue(Record[0], VarAlignment)) |
780 return EC; | 712 return EC; |
781 VarIsConstant = Record[1] != 0; | 713 VarIsConstant = Record[1] != 0; |
782 // Assume (by default) there is a single initializer. | 714 // Assume (by default) there is a single initializer. |
783 VarInitializersNeeded = 1; | 715 VarInitializersNeeded = 1; |
784 break; | 716 break; |
785 case naclbitc::GLOBALVAR_COMPOUND: { | 717 case naclbitc::GLOBALVAR_COMPOUND: { |
786 // Global variable has multiple initializers. Changes the | 718 // Global variable has multiple initializers. Changes the |
787 // default number of initializers to the given value in | 719 // default number of initializers to the given value in |
788 // Record[0]. | 720 // Record[0]. |
789 if (!ProcessingGlobal || !VarType.empty() || | 721 if (!ProcessingGlobal || !VarType.empty() || |
790 VarInitializersNeeded != 1 || Record.size() != 1) | 722 VarInitializersNeeded != 1 || Record.size() != 1) |
791 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 723 return Reader.Error("Bad GLOBALVAR_COMPOUND record"); |
792 "Bad GLOBALVAR_COMPOUND record"); | |
793 uint64_t Size = Record[0]; | 724 uint64_t Size = Record[0]; |
794 // Check that ILP32 assumption met. | 725 // Check that ILP32 assumption met. |
795 if (Size > MaxNaClGlobalVarInits) | 726 if (Size > MaxNaClGlobalVarInits) |
796 return Reader.Error(NaClBitcodeReader::InvalidValue, | 727 return Reader.Error("Size too big in GLOBALVAR_COMPOUND record"); |
797 "Size too big in GLOBALVAR_COMPOUND record"); | |
798 VarInitializersNeeded = static_cast<size_t>(Size); | 728 VarInitializersNeeded = static_cast<size_t>(Size); |
799 break; | 729 break; |
800 } | 730 } |
801 case naclbitc::GLOBALVAR_ZEROFILL: { | 731 case naclbitc::GLOBALVAR_ZEROFILL: { |
802 // Define a type that defines a sequence of zero-filled bytes. | 732 // Define a type that defines a sequence of zero-filled bytes. |
803 if (!ProcessingGlobal || Record.size() != 1) | 733 if (!ProcessingGlobal || Record.size() != 1) |
804 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 734 return Reader.Error("Bad GLOBALVAR_ZEROFILL record"); |
805 "Bad GLOBALVAR_ZEROFILL record"); | |
806 VarType.push_back(ArrayType::get( | 735 VarType.push_back(ArrayType::get( |
807 Type::getInt8Ty(Context), Record[0])); | 736 Type::getInt8Ty(Context), Record[0])); |
808 break; | 737 break; |
809 } | 738 } |
810 case naclbitc::GLOBALVAR_DATA: { | 739 case naclbitc::GLOBALVAR_DATA: { |
811 // Defines a type defined by a sequence of byte values. | 740 // Defines a type defined by a sequence of byte values. |
812 if (!ProcessingGlobal || Record.size() < 1) | 741 if (!ProcessingGlobal || Record.size() < 1) |
813 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 742 return Reader.Error("Bad GLOBALVAR_DATA record"); |
814 "Bad GLOBALVAR_DATA record"); | |
815 VarType.push_back(ArrayType::get( | 743 VarType.push_back(ArrayType::get( |
816 Type::getInt8Ty(Context), Record.size())); | 744 Type::getInt8Ty(Context), Record.size())); |
817 break; | 745 break; |
818 } | 746 } |
819 case naclbitc::GLOBALVAR_RELOC: { | 747 case naclbitc::GLOBALVAR_RELOC: { |
820 // Define a relocation initializer type. | 748 // Define a relocation initializer type. |
821 if (!ProcessingGlobal || Record.size() < 1 || Record.size() > 2) | 749 if (!ProcessingGlobal || Record.size() < 1 || Record.size() > 2) |
822 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 750 return Reader.Error("Bad GLOBALVAR_RELOC record"); |
823 "Bad GLOBALVAR_RELOC record"); | |
824 VarType.push_back(IntegerType::get(Context, 32)); | 751 VarType.push_back(IntegerType::get(Context, 32)); |
825 break; | 752 break; |
826 } | 753 } |
827 case naclbitc::GLOBALVAR_COUNT: | 754 case naclbitc::GLOBALVAR_COUNT: |
828 if (Record.size() != 1 || NumGlobals != 0) | 755 if (Record.size() != 1 || NumGlobals != 0) |
829 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 756 return Reader.Error("Invalid global count record"); |
830 "Invalid global count record"); | |
831 if (Record[0] > NaClBcIndexSize_t_Max) | 757 if (Record[0] > NaClBcIndexSize_t_Max) |
832 return Reader.Error(NaClBitcodeReader::InvalidValue, | 758 return Reader.Error("Size too big in global count record"); |
833 "Size too big in global count record"); | |
834 NumGlobals = static_cast<NaClBcIndexSize_t>(Record[0]); | 759 NumGlobals = static_cast<NaClBcIndexSize_t>(Record[0]); |
835 break; | 760 break; |
836 } | 761 } |
837 | 762 |
838 // If more initializers needed for global variable, continue processing. | 763 // If more initializers needed for global variable, continue processing. |
839 if (!ProcessingGlobal || VarType.size() < VarInitializersNeeded) | 764 if (!ProcessingGlobal || VarType.size() < VarInitializersNeeded) |
840 continue; | 765 continue; |
841 | 766 |
842 Type *Ty = 0; | 767 Type *Ty = 0; |
843 switch (VarType.size()) { | 768 switch (VarType.size()) { |
844 case 0: | 769 case 0: |
845 return Reader.Error( | 770 return Reader.Error( |
846 NaClBitcodeReader::InvalidRecord, | |
847 "No initializer for global variable in global vars block"); | 771 "No initializer for global variable in global vars block"); |
848 case 1: | 772 case 1: |
849 Ty = VarType[0]; | 773 Ty = VarType[0]; |
850 break; | 774 break; |
851 default: | 775 default: |
852 Ty = StructType::get(Context, VarType, true); | 776 Ty = StructType::get(Context, VarType, true); |
853 break; | 777 break; |
854 } | 778 } |
855 GlobalVariable *GV = new GlobalVariable( | 779 GlobalVariable *GV = new GlobalVariable( |
856 *TheModule, Ty, VarIsConstant, | 780 *TheModule, Ty, VarIsConstant, |
(...skipping 13 matching lines...) Expand all Loading... |
870 std::error_code GenerateGlobalVarInitsPass() { | 794 std::error_code GenerateGlobalVarInitsPass() { |
871 InitPass(); | 795 InitPass(); |
872 // The initializer for the global variable. | 796 // The initializer for the global variable. |
873 SmallVector<Constant *, 10> VarInit; | 797 SmallVector<Constant *, 10> VarInit; |
874 | 798 |
875 while (1) { | 799 while (1) { |
876 NaClBitstreamEntry Entry = | 800 NaClBitstreamEntry Entry = |
877 Stream.advance(NaClBitstreamCursor::AF_DontAutoprocessAbbrevs, nullptr); | 801 Stream.advance(NaClBitstreamCursor::AF_DontAutoprocessAbbrevs, nullptr); |
878 switch (Entry.Kind) { | 802 switch (Entry.Kind) { |
879 case NaClBitstreamEntry::SubBlock: | 803 case NaClBitstreamEntry::SubBlock: |
880 » return Reader.Error(NaClBitcodeReader::InvalidBlock, | 804 return Reader.Error("Invalid block in the global vars block"); |
881 » » » "Invalid block in the global vars block"); | |
882 case NaClBitstreamEntry::Error: | 805 case NaClBitstreamEntry::Error: |
883 return Reader.Error(NaClBitcodeReader::MalformedBlock, | 806 return Reader.Error("Error in the global vars block"); |
884 "Error in the global vars block"); | |
885 case NaClBitstreamEntry::EndBlock: | 807 case NaClBitstreamEntry::EndBlock: |
886 if (ProcessingGlobal || NumGlobals != (NextValueNo - FirstValueNo)) | 808 if (ProcessingGlobal || NumGlobals != (NextValueNo - FirstValueNo)) |
887 return Reader.Error(NaClBitcodeReader::MalformedBlock, | 809 return Reader.Error("Error in the global vars block"); |
888 "Error in the global vars block"); | |
889 return std::error_code(); | 810 return std::error_code(); |
890 case NaClBitstreamEntry::Record: | 811 case NaClBitstreamEntry::Record: |
891 if (Entry.ID == naclbitc::DEFINE_ABBREV) { | 812 if (Entry.ID == naclbitc::DEFINE_ABBREV) { |
892 Stream.SkipAbbrevRecord(); | 813 Stream.SkipAbbrevRecord(); |
893 continue; | 814 continue; |
894 } | 815 } |
895 // The interesting case. | 816 // The interesting case. |
896 break; | 817 break; |
897 } | 818 } |
898 | 819 |
899 // Read a record. | 820 // Read a record. |
900 Record.clear(); | 821 Record.clear(); |
901 unsigned Bitcode = Stream.readRecord(Entry.ID, Record); | 822 unsigned Bitcode = Stream.readRecord(Entry.ID, Record); |
902 switch (Bitcode) { | 823 switch (Bitcode) { |
903 default: | 824 default: |
904 return Reader.Error(NaClBitcodeReader::InvalidValue, | 825 return Reader.Error("Unknown global variable entry (pass 2)"); |
905 "Unknown global variable entry 2"); | |
906 case naclbitc::GLOBALVAR_VAR: | 826 case naclbitc::GLOBALVAR_VAR: |
907 // Start the definition of a global variable. | 827 // Start the definition of a global variable. |
908 ProcessingGlobal = true; | 828 ProcessingGlobal = true; |
909 // Assume (by default) there is a single initializer. | 829 // Assume (by default) there is a single initializer. |
910 VarInitializersNeeded = 1; | 830 VarInitializersNeeded = 1; |
911 break; | 831 break; |
912 case naclbitc::GLOBALVAR_COMPOUND: | 832 case naclbitc::GLOBALVAR_COMPOUND: |
913 // Global variable has multiple initializers. Changes the | 833 // Global variable has multiple initializers. Changes the |
914 // default number of initializers to the given value in | 834 // default number of initializers to the given value in |
915 // Record[0]. | 835 // Record[0]. |
916 if (!ProcessingGlobal || !VarInit.empty() || | 836 if (!ProcessingGlobal || !VarInit.empty() || |
917 VarInitializersNeeded != 1 || Record.size() != 1) | 837 VarInitializersNeeded != 1 || Record.size() != 1) |
918 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 838 return Reader.Error("Bad GLOBALVAR_COMPOUND record"); |
919 "Bad GLOBALVAR_COMPOUND record"); | |
920 // Note: We assume VarInitializersNeeded size was checked | 839 // Note: We assume VarInitializersNeeded size was checked |
921 // in GenerateGlobalVarsPass. | 840 // in GenerateGlobalVarsPass. |
922 VarInitializersNeeded = static_cast<size_t>(Record[0]); | 841 VarInitializersNeeded = static_cast<size_t>(Record[0]); |
923 break; | 842 break; |
924 case naclbitc::GLOBALVAR_ZEROFILL: { | 843 case naclbitc::GLOBALVAR_ZEROFILL: { |
925 // Define an initializer that defines a sequence of zero-filled bytes. | 844 // Define an initializer that defines a sequence of zero-filled bytes. |
926 if (!ProcessingGlobal || Record.size() != 1) | 845 if (!ProcessingGlobal || Record.size() != 1) |
927 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 846 return Reader.Error("Bad GLOBALVAR_ZEROFILL record"); |
928 "Bad GLOBALVAR_ZEROFILL record"); | |
929 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), | 847 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), |
930 Record[0]); | 848 Record[0]); |
931 Constant *Zero = ConstantAggregateZero::get(Ty); | 849 Constant *Zero = ConstantAggregateZero::get(Ty); |
932 VarInit.push_back(Zero); | 850 VarInit.push_back(Zero); |
933 break; | 851 break; |
934 } | 852 } |
935 case naclbitc::GLOBALVAR_DATA: { | 853 case naclbitc::GLOBALVAR_DATA: { |
936 // Defines an initializer defined by a sequence of byte values. | 854 // Defines an initializer defined by a sequence of byte values. |
937 if (!ProcessingGlobal || Record.size() < 1) | 855 if (!ProcessingGlobal || Record.size() < 1) |
938 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 856 return Reader.Error("Bad GLOBALVAR_DATA record"); |
939 "Bad GLOBALVAR_DATA record"); | |
940 size_t Size = Record.size(); | 857 size_t Size = Record.size(); |
941 uint8_t *Buf = new uint8_t[Size]; | 858 uint8_t *Buf = new uint8_t[Size]; |
942 assert(Buf); | 859 assert(Buf); |
943 for (size_t i = 0; i < Size; ++i) | 860 for (size_t i = 0; i < Size; ++i) |
944 Buf[i] = Record[i]; | 861 Buf[i] = Record[i]; |
945 Constant *Init = ConstantDataArray::get( | 862 Constant *Init = ConstantDataArray::get( |
946 Context, ArrayRef<uint8_t>(Buf, Buf + Size)); | 863 Context, ArrayRef<uint8_t>(Buf, Buf + Size)); |
947 VarInit.push_back(Init); | 864 VarInit.push_back(Init); |
948 delete[] Buf; | 865 delete[] Buf; |
949 break; | 866 break; |
950 } | 867 } |
951 case naclbitc::GLOBALVAR_RELOC: { | 868 case naclbitc::GLOBALVAR_RELOC: { |
952 // Define a relocation initializer. | 869 // Define a relocation initializer. |
953 if (!ProcessingGlobal || Record.size() < 1 || Record.size() > 2) | 870 if (!ProcessingGlobal || Record.size() < 1 || Record.size() > 2) |
954 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 871 return Reader.Error("Bad GLOBALVAR_RELOC record"); |
955 "Bad GLOBALVAR_RELOC record"); | |
956 Constant *BaseVal = cast<Constant>(Reader.getFnValueByID(Record[0])); | 872 Constant *BaseVal = cast<Constant>(Reader.getFnValueByID(Record[0])); |
957 Type *IntPtrType = IntegerType::get(Context, 32); | 873 Type *IntPtrType = IntegerType::get(Context, 32); |
958 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); | 874 Constant *Val = ConstantExpr::getPtrToInt(BaseVal, IntPtrType); |
959 if (Record.size() == 2) { | 875 if (Record.size() == 2) { |
960 uint64_t Addend = Record[1]; | 876 uint64_t Addend = Record[1]; |
961 // Note: PNaCl is ILP32, so Addend must be uint32_t. | 877 // Note: PNaCl is ILP32, so Addend must be uint32_t. |
962 if (Addend > std::numeric_limits<uint32_t>::max()) | 878 if (Addend > std::numeric_limits<uint32_t>::max()) |
963 return Reader.Error(NaClBitcodeReader::InvalidValue, | 879 return Reader.Error("Addend of GLOBALVAR_RELOC record too big"); |
964 "Addend of GLOBALVAR_RELOC record too big"); | |
965 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, | 880 Val = ConstantExpr::getAdd(Val, ConstantInt::get(IntPtrType, |
966 Addend)); | 881 Addend)); |
967 } | 882 } |
968 VarInit.push_back(Val); | 883 VarInit.push_back(Val); |
969 break; | 884 break; |
970 } | 885 } |
971 case naclbitc::GLOBALVAR_COUNT: | 886 case naclbitc::GLOBALVAR_COUNT: |
972 if (Record.size() != 1) | 887 if (Record.size() != 1) |
973 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 888 return Reader.Error("Invalid global count record"); |
974 "Invalid global count record"); | |
975 // Note: NumGlobals should have been set in GenerateGlobalVarsPass. | 889 // Note: NumGlobals should have been set in GenerateGlobalVarsPass. |
976 // Fail if methods are called in wrong order. | 890 // Fail if methods are called in wrong order. |
977 assert(NumGlobals == Record[0]); | 891 assert(NumGlobals == Record[0]); |
978 break; | 892 break; |
979 } | 893 } |
980 | 894 |
981 // If more initializers needed for global variable, continue processing. | 895 // If more initializers needed for global variable, continue processing. |
982 if (!ProcessingGlobal || VarInit.size() < VarInitializersNeeded) | 896 if (!ProcessingGlobal || VarInit.size() < VarInitializersNeeded) |
983 continue; | 897 continue; |
984 | 898 |
985 Constant *Init = 0; | 899 Constant *Init = 0; |
986 switch (VarInit.size()) { | 900 switch (VarInit.size()) { |
987 case 0: | 901 case 0: |
988 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 902 return Reader.Error( |
989 "No initializer for global variable in global vars block"); | 903 "No initializer for global variable in global vars block"); |
990 case 1: | 904 case 1: |
991 Init = VarInit[0]; | 905 Init = VarInit[0]; |
992 break; | 906 break; |
993 default: | 907 default: |
994 Init = ConstantStruct::getAnon(Context, VarInit, true); | 908 Init = ConstantStruct::getAnon(Context, VarInit, true); |
995 break; | 909 break; |
996 } | 910 } |
997 cast<GlobalVariable>(ValueList[NextValueNo])->setInitializer(Init); | 911 cast<GlobalVariable>(ValueList[NextValueNo])->setInitializer(Init); |
998 if (NextValueNo == NaClBcIndexSize_t_Max) | 912 if (NextValueNo == NaClBcIndexSize_t_Max) |
999 return Reader.Error(NaClBitcodeReader::InvalidRecord, | 913 return Reader.Error("Exceeded value index limit"); |
1000 "Exceeded value index limit"); | |
1001 ++NextValueNo; | 914 ++NextValueNo; |
1002 ProcessingGlobal = false; | 915 ProcessingGlobal = false; |
1003 VarInitializersNeeded = 0; | 916 VarInitializersNeeded = 0; |
1004 VarInit.clear(); | 917 VarInit.clear(); |
1005 } | 918 } |
1006 return std::error_code(); | 919 return std::error_code(); |
1007 } | 920 } |
1008 }; | 921 }; |
1009 | 922 |
1010 std::error_code NaClBitcodeReader::ParseGlobalVars() { | 923 std::error_code NaClBitcodeReader::ParseGlobalVars() { |
1011 if (Stream.EnterSubBlock(naclbitc::GLOBALVAR_BLOCK_ID)) | 924 if (Stream.EnterSubBlock(naclbitc::GLOBALVAR_BLOCK_ID)) |
1012 return Error(InvalidRecord, "Malformed block record"); | 925 return Error("Malformed block record"); |
1013 | 926 |
1014 ParseGlobalsHandler PassHandler(*this, ValueList, Stream, Context, TheModule); | 927 ParseGlobalsHandler PassHandler(*this, ValueList, Stream, Context, TheModule); |
1015 if (std::error_code EC = PassHandler.GenerateGlobalVarsPass()) | 928 if (std::error_code EC = PassHandler.GenerateGlobalVarsPass()) |
1016 return EC; | 929 return EC; |
1017 return PassHandler.GenerateGlobalVarInitsPass(); | 930 return PassHandler.GenerateGlobalVarInitsPass(); |
1018 } | 931 } |
1019 | 932 |
1020 std::error_code NaClBitcodeReader::ParseValueSymbolTable() { | 933 std::error_code NaClBitcodeReader::ParseValueSymbolTable() { |
1021 DEBUG(dbgs() << "-> ParseValueSymbolTable\n"); | 934 DEBUG(dbgs() << "-> ParseValueSymbolTable\n"); |
1022 if (Stream.EnterSubBlock(naclbitc::VALUE_SYMTAB_BLOCK_ID)) | 935 if (Stream.EnterSubBlock(naclbitc::VALUE_SYMTAB_BLOCK_ID)) |
1023 return Error(InvalidRecord, "Malformed block record"); | 936 return Error("Malformed block record"); |
1024 | 937 |
1025 SmallVector<uint64_t, 64> Record; | 938 SmallVector<uint64_t, 64> Record; |
1026 | 939 |
1027 // Read all the records for this value table. | 940 // Read all the records for this value table. |
1028 SmallString<128> ValueName; | 941 SmallString<128> ValueName; |
1029 while (1) { | 942 while (1) { |
1030 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); | 943 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); |
1031 | 944 |
1032 switch (Entry.Kind) { | 945 switch (Entry.Kind) { |
1033 case NaClBitstreamEntry::SubBlock: | 946 case NaClBitstreamEntry::SubBlock: |
1034 return Error(InvalidBlock, | 947 return Error("Invalid block in the value symbol table block"); |
1035 "Invalid block in the value symbol table block"); | |
1036 case NaClBitstreamEntry::Error: | 948 case NaClBitstreamEntry::Error: |
1037 return Error(MalformedBlock, "malformed value symbol table block"); | 949 return Error("malformed value symbol table block"); |
1038 case NaClBitstreamEntry::EndBlock: | 950 case NaClBitstreamEntry::EndBlock: |
1039 DEBUG(dbgs() << "<- ParseValueSymbolTable\n"); | 951 DEBUG(dbgs() << "<- ParseValueSymbolTable\n"); |
1040 return std::error_code(); | 952 return std::error_code(); |
1041 case NaClBitstreamEntry::Record: | 953 case NaClBitstreamEntry::Record: |
1042 // The interesting case. | 954 // The interesting case. |
1043 break; | 955 break; |
1044 } | 956 } |
1045 | 957 |
1046 // Read a record. | 958 // Read a record. |
1047 Record.clear(); | 959 Record.clear(); |
1048 switch (Stream.readRecord(Entry.ID, Record)) { | 960 switch (Stream.readRecord(Entry.ID, Record)) { |
1049 default: // Default behavior: unknown type. | 961 default: // Default behavior: unknown type. |
1050 break; | 962 break; |
1051 case naclbitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] | 963 case naclbitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] |
1052 if (ConvertToString(Record, 1, ValueName)) | 964 if (ConvertToString(Record, 1, ValueName)) |
1053 return Error(InvalidRecord, "Invalid VST_ENTRY record"); | 965 return Error("Invalid VST_ENTRY record"); |
1054 Value *V = getFnValueByID(Record[0]); | 966 Value *V = getFnValueByID(Record[0]); |
1055 if (V == nullptr) | 967 if (V == nullptr) |
1056 return Error(InvalidValue, "Invalid Value ID in VST_ENTRY record"); | 968 return Error("Invalid Value ID in VST_ENTRY record"); |
1057 | 969 |
1058 V->setName(StringRef(ValueName.data(), ValueName.size())); | 970 V->setName(StringRef(ValueName.data(), ValueName.size())); |
1059 ValueName.clear(); | 971 ValueName.clear(); |
1060 break; | 972 break; |
1061 } | 973 } |
1062 case naclbitc::VST_CODE_BBENTRY: { | 974 case naclbitc::VST_CODE_BBENTRY: { |
1063 if (ConvertToString(Record, 1, ValueName)) | 975 if (ConvertToString(Record, 1, ValueName)) |
1064 return Error(InvalidRecord, "Invalid VST_BBENTRY record"); | 976 return Error("Invalid VST_BBENTRY record"); |
1065 BasicBlock *BB = getBasicBlock(Record[0]); | 977 BasicBlock *BB = getBasicBlock(Record[0]); |
1066 if (BB == 0) | 978 if (BB == 0) |
1067 return Error(InvalidValue, "Invalid BB ID in VST_BBENTRY record"); | 979 return Error("Invalid BB ID in VST_BBENTRY record"); |
1068 | 980 |
1069 BB->setName(StringRef(ValueName.data(), ValueName.size())); | 981 BB->setName(StringRef(ValueName.data(), ValueName.size())); |
1070 ValueName.clear(); | 982 ValueName.clear(); |
1071 break; | 983 break; |
1072 } | 984 } |
1073 } | 985 } |
1074 } | 986 } |
1075 } | 987 } |
1076 | 988 |
1077 std::error_code NaClBitcodeReader::ParseConstants() { | 989 std::error_code NaClBitcodeReader::ParseConstants() { |
1078 DEBUG(dbgs() << "-> ParseConstants\n"); | 990 DEBUG(dbgs() << "-> ParseConstants\n"); |
1079 if (Stream.EnterSubBlock(naclbitc::CONSTANTS_BLOCK_ID)) | 991 if (Stream.EnterSubBlock(naclbitc::CONSTANTS_BLOCK_ID)) |
1080 return Error(InvalidRecord, "Malformed block record"); | 992 return Error("Malformed block record"); |
1081 | 993 |
1082 SmallVector<uint64_t, 64> Record; | 994 SmallVector<uint64_t, 64> Record; |
1083 | 995 |
1084 // Read all the records for this value table. | 996 // Read all the records for this value table. |
1085 Type *CurTy = Type::getInt32Ty(Context); | 997 Type *CurTy = Type::getInt32Ty(Context); |
1086 NaClBcIndexSize_t NextCstNo = ValueList.size(); | 998 NaClBcIndexSize_t NextCstNo = ValueList.size(); |
1087 while (1) { | 999 while (1) { |
1088 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); | 1000 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); |
1089 | 1001 |
1090 switch (Entry.Kind) { | 1002 switch (Entry.Kind) { |
1091 case NaClBitstreamEntry::SubBlock: | 1003 case NaClBitstreamEntry::SubBlock: |
1092 return Error(InvalidBlock, "Invalid block in function constants block"); | 1004 return Error("Invalid block in function constants block"); |
1093 case NaClBitstreamEntry::Error: | 1005 case NaClBitstreamEntry::Error: |
1094 return Error(MalformedBlock, "malformed function constants block"); | 1006 return Error("malformed function constants block"); |
1095 case NaClBitstreamEntry::EndBlock: | 1007 case NaClBitstreamEntry::EndBlock: |
1096 if (NextCstNo != ValueList.size()) | 1008 if (NextCstNo != ValueList.size()) |
1097 return Error(InvalidConstantReference, | 1009 return Error("Invalid constant reference!"); |
1098 "Invalid constant reference!"); | |
1099 DEBUG(dbgs() << "<- ParseConstants\n"); | 1010 DEBUG(dbgs() << "<- ParseConstants\n"); |
1100 return std::error_code(); | 1011 return std::error_code(); |
1101 case NaClBitstreamEntry::Record: | 1012 case NaClBitstreamEntry::Record: |
1102 // The interesting case. | 1013 // The interesting case. |
1103 break; | 1014 break; |
1104 } | 1015 } |
1105 | 1016 |
1106 // Read a record. | 1017 // Read a record. |
1107 Record.clear(); | 1018 Record.clear(); |
1108 Value *V = 0; | 1019 Value *V = 0; |
1109 unsigned BitCode = Stream.readRecord(Entry.ID, Record); | 1020 unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
1110 switch (BitCode) { | 1021 switch (BitCode) { |
1111 default: { | 1022 default: { |
1112 std::string Message; | 1023 std::string Message; |
1113 raw_string_ostream StrM(Message); | 1024 raw_string_ostream StrM(Message); |
1114 StrM << "Invalid Constant code: " << BitCode; | 1025 StrM << "Invalid Constant code: " << BitCode; |
1115 StrM.flush(); | 1026 return Error(StrM.str()); |
1116 return Error(InvalidValue, Message); | |
1117 } | 1027 } |
1118 case naclbitc::CST_CODE_UNDEF: // UNDEF | 1028 case naclbitc::CST_CODE_UNDEF: // UNDEF |
1119 V = UndefValue::get(CurTy); | 1029 V = UndefValue::get(CurTy); |
1120 break; | 1030 break; |
1121 case naclbitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] | 1031 case naclbitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
1122 if (Record.empty()) | 1032 if (Record.empty()) |
1123 return Error(NaClBitcodeReader::InvalidRecord, | 1033 return Error("Malformed CST_SETTYPE record"); |
1124 "Malformed CST_SETTYPE record"); | |
1125 if (Record[0] >= TypeList.size()) | 1034 if (Record[0] >= TypeList.size()) |
1126 return Error(NaClBitcodeReader::InvalidType, | 1035 return Error("Invalid Type ID in CST_SETTYPE record"); |
1127 "Invalid Type ID in CST_SETTYPE record"); | |
1128 CurTy = TypeList[Record[0]]; | 1036 CurTy = TypeList[Record[0]]; |
1129 continue; // Skip the ValueList manipulation. | 1037 continue; // Skip the ValueList manipulation. |
1130 case naclbitc::CST_CODE_INTEGER: // INTEGER: [intval] | 1038 case naclbitc::CST_CODE_INTEGER: // INTEGER: [intval] |
1131 if (!CurTy->isIntegerTy() || Record.empty()) | 1039 if (!CurTy->isIntegerTy() || Record.empty()) |
1132 return Error(InvalidRecord, "Invalid CST_INTEGER record"); | 1040 return Error("Invalid CST_INTEGER record"); |
1133 V = ConstantInt::get(CurTy, NaClDecodeSignRotatedValue(Record[0])); | 1041 V = ConstantInt::get(CurTy, NaClDecodeSignRotatedValue(Record[0])); |
1134 break; | 1042 break; |
1135 case naclbitc::CST_CODE_FLOAT: { // FLOAT: [fpval] | 1043 case naclbitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
1136 if (Record.empty()) | 1044 if (Record.empty()) |
1137 return Error(NaClBitcodeReader::InvalidRecord, "Invalid FLOAT record"); | 1045 return Error("Invalid FLOAT record"); |
1138 if (CurTy->isFloatTy()) | 1046 if (CurTy->isFloatTy()) |
1139 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle, | 1047 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle, |
1140 APInt(32, (uint32_t)Record[0]))); | 1048 APInt(32, (uint32_t)Record[0]))); |
1141 else if (CurTy->isDoubleTy()) | 1049 else if (CurTy->isDoubleTy()) |
1142 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble, | 1050 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble, |
1143 APInt(64, Record[0]))); | 1051 APInt(64, Record[0]))); |
1144 else | 1052 else |
1145 return Error(NaClBitcodeReader::InvalidRecord, | 1053 return Error("Unknown type for FLOAT record"); |
1146 "Unknown type for FLOAT record"); | |
1147 break; | 1054 break; |
1148 } | 1055 } |
1149 } | 1056 } |
1150 | 1057 |
1151 ValueList.AssignValue(V, NextCstNo); | 1058 ValueList.AssignValue(V, NextCstNo); |
1152 ++NextCstNo; | 1059 ++NextCstNo; |
1153 } | 1060 } |
1154 return std::error_code(); | 1061 return std::error_code(); |
1155 } | 1062 } |
1156 | 1063 |
1157 // When we see the block for a function body, remember where it is and then skip | 1064 // When we see the block for a function body, remember where it is and then skip |
1158 // it. This lets us lazily deserialize the functions. | 1065 // it. This lets us lazily deserialize the functions. |
1159 std::error_code NaClBitcodeReader::RememberAndSkipFunctionBody() { | 1066 std::error_code NaClBitcodeReader::RememberAndSkipFunctionBody() { |
1160 DEBUG(dbgs() << "-> RememberAndSkipFunctionBody\n"); | 1067 DEBUG(dbgs() << "-> RememberAndSkipFunctionBody\n"); |
1161 // Get the function we are talking about. | 1068 // Get the function we are talking about. |
1162 if (FunctionsWithBodies.empty()) | 1069 if (FunctionsWithBodies.empty()) |
1163 return Error(InsufficientFunctionProtos, | 1070 return Error("Insufficient function protos"); |
1164 "Insufficient function protos"); | |
1165 | 1071 |
1166 Function *Fn = FunctionsWithBodies.back(); | 1072 Function *Fn = FunctionsWithBodies.back(); |
1167 FunctionsWithBodies.pop_back(); | 1073 FunctionsWithBodies.pop_back(); |
1168 | 1074 |
1169 // Save the current stream state. | 1075 // Save the current stream state. |
1170 uint64_t CurBit = Stream.GetCurrentBitNo(); | 1076 uint64_t CurBit = Stream.GetCurrentBitNo(); |
1171 DeferredFunctionInfo[Fn] = CurBit; | 1077 DeferredFunctionInfo[Fn] = CurBit; |
1172 | 1078 |
1173 // Skip over the function block for now. | 1079 // Skip over the function block for now. |
1174 if (Stream.SkipBlock()) | 1080 if (Stream.SkipBlock()) |
1175 return Error(InvalidSkippedBlock, "Unable to skip function block."); | 1081 return Error("Unable to skip function block."); |
1176 DEBUG(dbgs() << "<- RememberAndSkipFunctionBody\n"); | 1082 DEBUG(dbgs() << "<- RememberAndSkipFunctionBody\n"); |
1177 return std::error_code(); | 1083 return std::error_code(); |
1178 } | 1084 } |
1179 | 1085 |
1180 std::error_code NaClBitcodeReader::GlobalCleanup() { | 1086 std::error_code NaClBitcodeReader::GlobalCleanup() { |
1181 // Look for intrinsic functions which need to be upgraded at some point | 1087 // Look for intrinsic functions which need to be upgraded at some point |
1182 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); | 1088 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
1183 FI != FE; ++FI) { | 1089 FI != FE; ++FI) { |
1184 Function *NewFn; | 1090 Function *NewFn; |
1185 if (UpgradeIntrinsicFunction(FI, NewFn)) | 1091 if (UpgradeIntrinsicFunction(FI, NewFn)) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1246 } | 1152 } |
1247 } | 1153 } |
1248 } | 1154 } |
1249 } | 1155 } |
1250 | 1156 |
1251 std::error_code NaClBitcodeReader::ParseModule(bool Resume) { | 1157 std::error_code NaClBitcodeReader::ParseModule(bool Resume) { |
1252 DEBUG(dbgs() << "-> ParseModule\n"); | 1158 DEBUG(dbgs() << "-> ParseModule\n"); |
1253 if (Resume) | 1159 if (Resume) |
1254 Stream.JumpToBit(NextUnreadBit); | 1160 Stream.JumpToBit(NextUnreadBit); |
1255 else if (Stream.EnterSubBlock(naclbitc::MODULE_BLOCK_ID)) | 1161 else if (Stream.EnterSubBlock(naclbitc::MODULE_BLOCK_ID)) |
1256 return Error(InvalidRecord, "Malformed block record"); | 1162 return Error("Malformed block record"); |
1257 | 1163 |
1258 SmallVector<uint64_t, 64> Record; | 1164 SmallVector<uint64_t, 64> Record; |
1259 | 1165 |
1260 // Read all the records for this module. | 1166 // Read all the records for this module. |
1261 while (1) { | 1167 while (1) { |
1262 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); | 1168 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); |
1263 | 1169 |
1264 switch (Entry.Kind) { | 1170 switch (Entry.Kind) { |
1265 case NaClBitstreamEntry::Error: | 1171 case NaClBitstreamEntry::Error: |
1266 return Error(MalformedBlock, "malformed module block"); | 1172 return Error("malformed module block"); |
1267 case NaClBitstreamEntry::EndBlock: | 1173 case NaClBitstreamEntry::EndBlock: |
1268 DEBUG(dbgs() << "<- ParseModule\n"); | 1174 DEBUG(dbgs() << "<- ParseModule\n"); |
1269 if (std::error_code EC = GlobalCleanup()) | 1175 if (std::error_code EC = GlobalCleanup()) |
1270 return EC; | 1176 return EC; |
1271 if (!Stream.AtEndOfStream()) | 1177 if (!Stream.AtEndOfStream()) |
1272 return Error(InvalidDataAfterModule, "Invalid data after module"); | 1178 return Error("Invalid data after module"); |
1273 return std::error_code(); | 1179 return std::error_code(); |
1274 case NaClBitstreamEntry::SubBlock: | 1180 case NaClBitstreamEntry::SubBlock: |
1275 switch (Entry.ID) { | 1181 switch (Entry.ID) { |
1276 default: { | 1182 default: { |
1277 std::string Message; | 1183 std::string Message; |
1278 raw_string_ostream StrM(Message); | 1184 raw_string_ostream StrM(Message); |
1279 StrM << "Unknown block ID: " << Entry.ID; | 1185 StrM << "Unknown block ID: " << Entry.ID; |
1280 return Error(InvalidRecord, StrM.str()); | 1186 return Error(StrM.str()); |
1281 } | 1187 } |
1282 case naclbitc::BLOCKINFO_BLOCK_ID: | 1188 case naclbitc::BLOCKINFO_BLOCK_ID: |
1283 if (Stream.ReadBlockInfoBlock(0)) | 1189 if (Stream.ReadBlockInfoBlock(0)) |
1284 return Error(MalformedBlock, "Malformed BlockInfoBlock"); | 1190 return Error("Malformed BlockInfoBlock"); |
1285 break; | 1191 break; |
1286 case naclbitc::TYPE_BLOCK_ID_NEW: | 1192 case naclbitc::TYPE_BLOCK_ID_NEW: |
1287 if (std::error_code EC = ParseTypeTable()) | 1193 if (std::error_code EC = ParseTypeTable()) |
1288 return EC; | 1194 return EC; |
1289 break; | 1195 break; |
1290 case naclbitc::GLOBALVAR_BLOCK_ID: | 1196 case naclbitc::GLOBALVAR_BLOCK_ID: |
1291 if (std::error_code EC = ParseGlobalVars()) | 1197 if (std::error_code EC = ParseGlobalVars()) |
1292 return EC; | 1198 return EC; |
1293 break; | 1199 break; |
1294 case naclbitc::VALUE_SYMTAB_BLOCK_ID: | 1200 case naclbitc::VALUE_SYMTAB_BLOCK_ID: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1333 } | 1239 } |
1334 | 1240 |
1335 // Read a record. | 1241 // Read a record. |
1336 unsigned Selector = Stream.readRecord(Entry.ID, Record); | 1242 unsigned Selector = Stream.readRecord(Entry.ID, Record); |
1337 switch (Selector) { | 1243 switch (Selector) { |
1338 default: { | 1244 default: { |
1339 std::string Message; | 1245 std::string Message; |
1340 raw_string_ostream StrM(Message); | 1246 raw_string_ostream StrM(Message); |
1341 StrM << "Invalid MODULE_CODE: " << Selector; | 1247 StrM << "Invalid MODULE_CODE: " << Selector; |
1342 StrM.flush(); | 1248 StrM.flush(); |
1343 return Error(InvalidValue, Message); | 1249 return Error(Message); |
1344 } | 1250 } |
1345 case naclbitc::MODULE_CODE_VERSION: { // VERSION: [version#] | 1251 case naclbitc::MODULE_CODE_VERSION: { // VERSION: [version#] |
1346 if (Record.size() < 1) | 1252 if (Record.size() < 1) |
1347 return Error(InvalidRecord, "Malformed MODULE_CODE_VERSION"); | 1253 return Error("Malformed MODULE_CODE_VERSION"); |
1348 // Only version #1 is supported for PNaCl. Version #0 is not supported. | 1254 // Only version #1 is supported for PNaCl. Version #0 is not supported. |
1349 uint64_t module_version = Record[0]; | 1255 uint64_t module_version = Record[0]; |
1350 if (module_version != 1) | 1256 if (module_version != 1) |
1351 return Error(InvalidValue, "Unknown bitstream version!"); | 1257 return Error("Unknown bitstream version!"); |
1352 break; | 1258 break; |
1353 } | 1259 } |
1354 // FUNCTION: [type, callingconv, isproto, linkage] | 1260 // FUNCTION: [type, callingconv, isproto, linkage] |
1355 case naclbitc::MODULE_CODE_FUNCTION: { | 1261 case naclbitc::MODULE_CODE_FUNCTION: { |
1356 if (Record.size() < 4) | 1262 if (Record.size() < 4) |
1357 return Error(InvalidRecord, "Invalid MODULE_CODE_FUNCTION record"); | 1263 return Error("Invalid MODULE_CODE_FUNCTION record"); |
1358 Type *Ty = getTypeByID(Record[0]); | 1264 Type *Ty = getTypeByID(Record[0]); |
1359 if (!Ty) | 1265 if (!Ty) |
1360 return Error(InvalidType, "Invalid MODULE_CODE_FUNCTION record"); | 1266 return Error("Invalid MODULE_CODE_FUNCTION record"); |
1361 FunctionType *FTy = dyn_cast<FunctionType>(Ty); | 1267 FunctionType *FTy = dyn_cast<FunctionType>(Ty); |
1362 if (!FTy) | 1268 if (!FTy) |
1363 return Error(InvalidType, | 1269 return Error("Function not declared with a function type!"); |
1364 "Function not declared with a function type!"); | |
1365 | 1270 |
1366 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, | 1271 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, |
1367 "", TheModule); | 1272 "", TheModule); |
1368 | 1273 |
1369 CallingConv::ID CallingConv; | 1274 CallingConv::ID CallingConv; |
1370 if (!naclbitc::DecodeCallingConv(Record[1], CallingConv)) | 1275 if (!naclbitc::DecodeCallingConv(Record[1], CallingConv)) |
1371 return Error(InvalidValue, | 1276 return Error("PNaCl bitcode contains invalid calling conventions."); |
1372 "PNaCl bitcode contains invalid calling conventions."); | |
1373 Func->setCallingConv(CallingConv); | 1277 Func->setCallingConv(CallingConv); |
1374 bool isProto = Record[2]; | 1278 bool isProto = Record[2]; |
1375 GlobalValue::LinkageTypes Linkage; | 1279 GlobalValue::LinkageTypes Linkage; |
1376 if (!naclbitc::DecodeLinkage(Record[3], Linkage)) | 1280 if (!naclbitc::DecodeLinkage(Record[3], Linkage)) |
1377 return Error(InvalidValue, "Unknown linkage type"); | 1281 return Error("Unknown linkage type"); |
1378 Func->setLinkage(Linkage); | 1282 Func->setLinkage(Linkage); |
1379 ValueList.push_back(Func); | 1283 ValueList.push_back(Func); |
1380 | 1284 |
1381 // If this is a function with a body, remember the prototype we are | 1285 // If this is a function with a body, remember the prototype we are |
1382 // creating now, so that we can match up the body with them later. | 1286 // creating now, so that we can match up the body with them later. |
1383 if (!isProto) { | 1287 if (!isProto) { |
1384 Func->setIsMaterializable(true); | 1288 Func->setIsMaterializable(true); |
1385 FunctionsWithBodies.push_back(Func); | 1289 FunctionsWithBodies.push_back(Func); |
1386 if (LazyStreamer) DeferredFunctionInfo[Func] = 0; | 1290 if (LazyStreamer) DeferredFunctionInfo[Func] = 0; |
1387 } | 1291 } |
(...skipping 24 matching lines...) Expand all Loading... |
1412 // need to understand them all. | 1316 // need to understand them all. |
1413 while (1) { | 1317 while (1) { |
1414 if (Stream.AtEndOfStream()) | 1318 if (Stream.AtEndOfStream()) |
1415 return std::error_code(); | 1319 return std::error_code(); |
1416 | 1320 |
1417 NaClBitstreamEntry Entry = | 1321 NaClBitstreamEntry Entry = |
1418 Stream.advance(NaClBitstreamCursor::AF_DontAutoprocessAbbrevs, nullptr); | 1322 Stream.advance(NaClBitstreamCursor::AF_DontAutoprocessAbbrevs, nullptr); |
1419 | 1323 |
1420 switch (Entry.Kind) { | 1324 switch (Entry.Kind) { |
1421 case NaClBitstreamEntry::Error: | 1325 case NaClBitstreamEntry::Error: |
1422 return Error(MalformedBlock, "malformed module file"); | 1326 return Error("malformed module file"); |
1423 case NaClBitstreamEntry::EndBlock: | 1327 case NaClBitstreamEntry::EndBlock: |
1424 return std::error_code(); | 1328 return std::error_code(); |
1425 | 1329 |
1426 case NaClBitstreamEntry::SubBlock: | 1330 case NaClBitstreamEntry::SubBlock: |
1427 switch (Entry.ID) { | 1331 switch (Entry.ID) { |
1428 case naclbitc::MODULE_BLOCK_ID: | 1332 case naclbitc::MODULE_BLOCK_ID: |
1429 // Reject multiple MODULE_BLOCK's in a single bitstream. | 1333 // Reject multiple MODULE_BLOCK's in a single bitstream. |
1430 if (TheModule) | 1334 if (TheModule) |
1431 return Error(InvalidMultipleBlocks, | 1335 return Error("Multiple MODULE_BLOCKs in same stream"); |
1432 "Multiple MODULE_BLOCKs in same stream"); | |
1433 TheModule = M; | 1336 TheModule = M; |
1434 if (std::error_code EC = ParseModule(false)) | 1337 if (std::error_code EC = ParseModule(false)) |
1435 return EC; | 1338 return EC; |
1436 if (LazyStreamer) | 1339 if (LazyStreamer) |
1437 return std::error_code(); | 1340 return std::error_code(); |
1438 break; | 1341 break; |
1439 default: | 1342 default: |
1440 return Error(InvalidBlock, "Invalid top-level block found."); | 1343 return Error("Invalid top-level block found."); |
1441 break; | 1344 break; |
1442 } | 1345 } |
1443 continue; | 1346 continue; |
1444 case NaClBitstreamEntry::Record: | 1347 case NaClBitstreamEntry::Record: |
1445 // There should be no records in the top-level of blocks. | 1348 // There should be no records in the top-level of blocks. |
1446 return Error(InvalidRecord, "Invalid record at top-level"); | 1349 return Error("Invalid record at top-level"); |
1447 } | 1350 } |
1448 } | 1351 } |
1449 } | 1352 } |
1450 | 1353 |
1451 // Returns true if error occured installing I into BB. | 1354 // Returns true if error occured installing I into BB. |
1452 std::error_code NaClBitcodeReader::InstallInstruction( | 1355 std::error_code NaClBitcodeReader::InstallInstruction( |
1453 BasicBlock *BB, Instruction *I) { | 1356 BasicBlock *BB, Instruction *I) { |
1454 // Add instruction to end of current BB. If there is no current BB, reject | 1357 // Add instruction to end of current BB. If there is no current BB, reject |
1455 // this file. | 1358 // this file. |
1456 if (BB == 0) { | 1359 if (BB == 0) { |
1457 delete I; | 1360 delete I; |
1458 return Error(InvalidInstructionWithNoBB, | 1361 return Error("Instruction with no BB, can't install"); |
1459 "Instruction with no BB, can't install"); | |
1460 } | 1362 } |
1461 BB->getInstList().push_back(I); | 1363 BB->getInstList().push_back(I); |
1462 return std::error_code(); | 1364 return std::error_code(); |
1463 } | 1365 } |
1464 | 1366 |
1465 CastInst * | 1367 CastInst * |
1466 NaClBitcodeReader::CreateCast(NaClBcIndexSize_t BBIndex, | 1368 NaClBitcodeReader::CreateCast(NaClBcIndexSize_t BBIndex, |
1467 Instruction::CastOps Op, | 1369 Instruction::CastOps Op, |
1468 Type *CT, Value *V, bool DeferInsertion) { | 1370 Type *CT, Value *V, bool DeferInsertion) { |
1469 if (BBIndex >= FunctionBBs.size()) | 1371 if (BBIndex >= FunctionBBs.size()) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1512 std::string Message; | 1414 std::string Message; |
1513 raw_string_ostream StrM(Message); | 1415 raw_string_ostream StrM(Message); |
1514 StrM << "Can't convert " << *Op << " to type " << *T << "\n"; | 1416 StrM << "Can't convert " << *Op << " to type " << *T << "\n"; |
1515 report_fatal_error(StrM.str()); | 1417 report_fatal_error(StrM.str()); |
1516 } | 1418 } |
1517 | 1419 |
1518 // Lazily parse the specified function body block. | 1420 // Lazily parse the specified function body block. |
1519 std::error_code NaClBitcodeReader::ParseFunctionBody(Function *F) { | 1421 std::error_code NaClBitcodeReader::ParseFunctionBody(Function *F) { |
1520 DEBUG(dbgs() << "-> ParseFunctionBody\n"); | 1422 DEBUG(dbgs() << "-> ParseFunctionBody\n"); |
1521 if (Stream.EnterSubBlock(naclbitc::FUNCTION_BLOCK_ID)) | 1423 if (Stream.EnterSubBlock(naclbitc::FUNCTION_BLOCK_ID)) |
1522 return Error(InvalidRecord, "Malformed block record"); | 1424 return Error("Malformed block record"); |
1523 | 1425 |
1524 NaClBcIndexSize_t ModuleValueListSize = ValueList.size(); | 1426 NaClBcIndexSize_t ModuleValueListSize = ValueList.size(); |
1525 | 1427 |
1526 // Add all the function arguments to the value table. | 1428 // Add all the function arguments to the value table. |
1527 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) | 1429 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
1528 ValueList.push_back(I); | 1430 ValueList.push_back(I); |
1529 | 1431 |
1530 NaClBcIndexSize_t NextValueNo = ValueList.size(); | 1432 NaClBcIndexSize_t NextValueNo = ValueList.size(); |
1531 BasicBlock *CurBB = 0; | 1433 BasicBlock *CurBB = 0; |
1532 NaClBcIndexSize_t CurBBNo = 0; | 1434 NaClBcIndexSize_t CurBBNo = 0; |
1533 | 1435 |
1534 // Read all the records. | 1436 // Read all the records. |
1535 SmallVector<uint64_t, 64> Record; | 1437 SmallVector<uint64_t, 64> Record; |
1536 while (1) { | 1438 while (1) { |
1537 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); | 1439 NaClBitstreamEntry Entry = Stream.advance(0, nullptr); |
1538 | 1440 |
1539 switch (Entry.Kind) { | 1441 switch (Entry.Kind) { |
1540 case NaClBitstreamEntry::Error: | 1442 case NaClBitstreamEntry::Error: |
1541 return Error(MalformedBlock, "Bitcode error in function block"); | 1443 return Error("Bitcode error in function block"); |
1542 case NaClBitstreamEntry::EndBlock: | 1444 case NaClBitstreamEntry::EndBlock: |
1543 goto OutOfRecordLoop; | 1445 goto OutOfRecordLoop; |
1544 | 1446 |
1545 case NaClBitstreamEntry::SubBlock: | 1447 case NaClBitstreamEntry::SubBlock: |
1546 switch (Entry.ID) { | 1448 switch (Entry.ID) { |
1547 default: | 1449 default: |
1548 return Error(InvalidBlock, "Invalid block in function block"); | 1450 return Error("Invalid block in function block"); |
1549 break; | 1451 break; |
1550 case naclbitc::CONSTANTS_BLOCK_ID: | 1452 case naclbitc::CONSTANTS_BLOCK_ID: |
1551 if (std::error_code EC = ParseConstants()) | 1453 if (std::error_code EC = ParseConstants()) |
1552 return EC; | 1454 return EC; |
1553 NextValueNo = ValueList.size(); | 1455 NextValueNo = ValueList.size(); |
1554 break; | 1456 break; |
1555 case naclbitc::VALUE_SYMTAB_BLOCK_ID: | 1457 case naclbitc::VALUE_SYMTAB_BLOCK_ID: |
1556 if (PNaClAllowLocalSymbolTables) { | 1458 if (PNaClAllowLocalSymbolTables) { |
1557 if (std::error_code EC = ParseValueSymbolTable()) | 1459 if (std::error_code EC = ParseValueSymbolTable()) |
1558 return EC; | 1460 return EC; |
1559 } else { | 1461 } else { |
1560 return Error(InvalidRecord, "Local value symbol tables not allowed"); | 1462 return Error("Local value symbol tables not allowed"); |
1561 } | 1463 } |
1562 break; | 1464 break; |
1563 } | 1465 } |
1564 continue; | 1466 continue; |
1565 | 1467 |
1566 case NaClBitstreamEntry::Record: | 1468 case NaClBitstreamEntry::Record: |
1567 // The interesting case. | 1469 // The interesting case. |
1568 break; | 1470 break; |
1569 } | 1471 } |
1570 | 1472 |
1571 // Read a record. | 1473 // Read a record. |
1572 Record.clear(); | 1474 Record.clear(); |
1573 Instruction *I = 0; | 1475 Instruction *I = 0; |
1574 unsigned BitCode = Stream.readRecord(Entry.ID, Record); | 1476 unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
1575 switch (BitCode) { | 1477 switch (BitCode) { |
1576 default: {// Default behavior: reject | 1478 default: {// Default behavior: reject |
1577 std::string Message; | 1479 std::string Message; |
1578 raw_string_ostream StrM(Message); | 1480 raw_string_ostream StrM(Message); |
1579 StrM << "Unknown instruction record: <" << BitCode; | 1481 StrM << "Unknown instruction record: <" << BitCode; |
1580 for (size_t I = 0, E = Record.size(); I != E; ++I) { | 1482 for (size_t I = 0, E = Record.size(); I != E; ++I) { |
1581 StrM << " " << Record[I]; | 1483 StrM << " " << Record[I]; |
1582 } | 1484 } |
1583 StrM << ">"; | 1485 StrM << ">"; |
1584 return Error(InvalidRecord, StrM.str()); | 1486 return Error(StrM.str()); |
1585 } | 1487 } |
1586 | 1488 |
1587 case naclbitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] | 1489 case naclbitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] |
1588 if (Record.size() != 1 || Record[0] == 0) | 1490 if (Record.size() != 1 || Record[0] == 0) |
1589 return Error(InvalidRecord, "Invalid DECLAREBLOCKS record"); | 1491 return Error("Invalid DECLAREBLOCKS record"); |
1590 // Create all the basic blocks for the function. | 1492 // Create all the basic blocks for the function. |
1591 // TODO(kschimpf): Figure out how to handle size values that | 1493 // TODO(kschimpf): Figure out how to handle size values that |
1592 // are too large. | 1494 // are too large. |
1593 FunctionBBs.resize(Record[0]); | 1495 FunctionBBs.resize(Record[0]); |
1594 for (size_t i = 0, e = FunctionBBs.size(); i != e; ++i) { | 1496 for (size_t i = 0, e = FunctionBBs.size(); i != e; ++i) { |
1595 BasicBlockInfo &BBInfo = FunctionBBs[i]; | 1497 BasicBlockInfo &BBInfo = FunctionBBs[i]; |
1596 BBInfo.BB = BasicBlock::Create(Context, "", F); | 1498 BBInfo.BB = BasicBlock::Create(Context, "", F); |
1597 } | 1499 } |
1598 CurBB = FunctionBBs.at(0).BB; | 1500 CurBB = FunctionBBs.at(0).BB; |
1599 continue; | 1501 continue; |
1600 | 1502 |
1601 case naclbitc::FUNC_CODE_INST_BINOP: { | 1503 case naclbitc::FUNC_CODE_INST_BINOP: { |
1602 // BINOP: [opval, opval, opcode[, flags]] | 1504 // BINOP: [opval, opval, opcode[, flags]] |
1603 // Note: Only old PNaCl bitcode files may contain flags. If | 1505 // Note: Only old PNaCl bitcode files may contain flags. If |
1604 // they are found, we ignore them. | 1506 // they are found, we ignore them. |
1605 size_t OpNum = 0; | 1507 size_t OpNum = 0; |
1606 Value *LHS, *RHS; | 1508 Value *LHS, *RHS; |
1607 if (popValue(Record, &OpNum, NextValueNo, &LHS) || | 1509 if (popValue(Record, &OpNum, NextValueNo, &LHS) || |
1608 popValue(Record, &OpNum, NextValueNo, &RHS) || | 1510 popValue(Record, &OpNum, NextValueNo, &RHS) || |
1609 OpNum+1 > Record.size()) | 1511 OpNum+1 > Record.size()) |
1610 return Error(InvalidRecord, "Invalid BINOP record"); | 1512 return Error("Invalid BINOP record"); |
1611 | 1513 |
1612 LHS = ConvertOpToScalar(LHS, CurBBNo); | 1514 LHS = ConvertOpToScalar(LHS, CurBBNo); |
1613 RHS = ConvertOpToScalar(RHS, CurBBNo); | 1515 RHS = ConvertOpToScalar(RHS, CurBBNo); |
1614 | 1516 |
1615 Instruction::BinaryOps Opc; | 1517 Instruction::BinaryOps Opc; |
1616 if (!naclbitc::DecodeBinaryOpcode(Record[OpNum++], LHS->getType(), Opc)) | 1518 if (!naclbitc::DecodeBinaryOpcode(Record[OpNum++], LHS->getType(), Opc)) |
1617 return Error(InvalidValue, "Invalid binary opcode in BINOP record"); | 1519 return Error("Invalid binary opcode in BINOP record"); |
1618 I = BinaryOperator::Create(Opc, LHS, RHS); | 1520 I = BinaryOperator::Create(Opc, LHS, RHS); |
1619 break; | 1521 break; |
1620 } | 1522 } |
1621 case naclbitc::FUNC_CODE_INST_CAST: { // CAST: [opval, destty, castopc] | 1523 case naclbitc::FUNC_CODE_INST_CAST: { // CAST: [opval, destty, castopc] |
1622 size_t OpNum = 0; | 1524 size_t OpNum = 0; |
1623 Value *Op; | 1525 Value *Op; |
1624 if (popValue(Record, &OpNum, NextValueNo, &Op) || | 1526 if (popValue(Record, &OpNum, NextValueNo, &Op) || |
1625 OpNum+2 != Record.size()) | 1527 OpNum+2 != Record.size()) |
1626 return Error(InvalidRecord, "Invalid CAST record: bad record size"); | 1528 return Error("Invalid CAST record: bad record size"); |
1627 | 1529 |
1628 Type *ResTy = getTypeByID(Record[OpNum]); | 1530 Type *ResTy = getTypeByID(Record[OpNum]); |
1629 if (ResTy == 0) | 1531 if (ResTy == 0) |
1630 return Error(InvalidType, "Invalid CAST record: bad type ID"); | 1532 return Error("Invalid CAST record: bad type ID"); |
1631 Instruction::CastOps Opc; | 1533 Instruction::CastOps Opc; |
1632 if (!naclbitc::DecodeCastOpcode(Record[OpNum+1], Opc)) { | 1534 if (!naclbitc::DecodeCastOpcode(Record[OpNum+1], Opc)) { |
1633 return Error(InvalidValue, "Invalid CAST record: bad opcode"); | 1535 return Error("Invalid CAST record: bad opcode"); |
1634 } | 1536 } |
1635 | 1537 |
1636 // If a ptrtoint cast was elided on the argument of the cast, | 1538 // If a ptrtoint cast was elided on the argument of the cast, |
1637 // add it back. Note: The casts allowed here should match the | 1539 // add it back. Note: The casts allowed here should match the |
1638 // casts in NaClValueEnumerator::ExpectsScalarValue. | 1540 // casts in NaClValueEnumerator::ExpectsScalarValue. |
1639 switch (Opc) { | 1541 switch (Opc) { |
1640 case Instruction::Trunc: | 1542 case Instruction::Trunc: |
1641 case Instruction::ZExt: | 1543 case Instruction::ZExt: |
1642 case Instruction::SExt: | 1544 case Instruction::SExt: |
1643 case Instruction::UIToFP: | 1545 case Instruction::UIToFP: |
(...skipping 10 matching lines...) Expand all Loading... |
1654 | 1556 |
1655 case naclbitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [opval, opval, pred] | 1557 case naclbitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [opval, opval, pred] |
1656 // new form of select | 1558 // new form of select |
1657 // handles select i1 or select [N x i1] | 1559 // handles select i1 or select [N x i1] |
1658 size_t OpNum = 0; | 1560 size_t OpNum = 0; |
1659 Value *TrueVal, *FalseVal, *Cond; | 1561 Value *TrueVal, *FalseVal, *Cond; |
1660 if (popValue(Record, &OpNum, NextValueNo, &TrueVal) || | 1562 if (popValue(Record, &OpNum, NextValueNo, &TrueVal) || |
1661 popValue(Record, &OpNum, NextValueNo, &FalseVal) || | 1563 popValue(Record, &OpNum, NextValueNo, &FalseVal) || |
1662 popValue(Record, &OpNum, NextValueNo, &Cond) || | 1564 popValue(Record, &OpNum, NextValueNo, &Cond) || |
1663 OpNum != Record.size()) | 1565 OpNum != Record.size()) |
1664 return Error(InvalidRecord, "Invalid SELECT record"); | 1566 return Error("Invalid SELECT record"); |
1665 | 1567 |
1666 TrueVal = ConvertOpToScalar(TrueVal, CurBBNo); | 1568 TrueVal = ConvertOpToScalar(TrueVal, CurBBNo); |
1667 FalseVal = ConvertOpToScalar(FalseVal, CurBBNo); | 1569 FalseVal = ConvertOpToScalar(FalseVal, CurBBNo); |
1668 | 1570 |
1669 // select condition can be either i1 or [N x i1] | 1571 // select condition can be either i1 or [N x i1] |
1670 if (VectorType* vector_type = | 1572 if (VectorType* vector_type = |
1671 dyn_cast<VectorType>(Cond->getType())) { | 1573 dyn_cast<VectorType>(Cond->getType())) { |
1672 // expect <n x i1> | 1574 // expect <n x i1> |
1673 if (vector_type->getElementType() != Type::getInt1Ty(Context)) | 1575 if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
1674 return Error(InvalidTypeForValue, | 1576 return Error("Invalid SELECT vector condition type"); |
1675 "Invalid SELECT vector condition type"); | |
1676 } else { | 1577 } else { |
1677 // expect i1 | 1578 // expect i1 |
1678 if (Cond->getType() != Type::getInt1Ty(Context)) | 1579 if (Cond->getType() != Type::getInt1Ty(Context)) |
1679 return Error(InvalidTypeForValue, "Invalid SELECT condition type"); | 1580 return Error("Invalid SELECT condition type"); |
1680 } | 1581 } |
1681 | 1582 |
1682 I = SelectInst::Create(Cond, TrueVal, FalseVal); | 1583 I = SelectInst::Create(Cond, TrueVal, FalseVal); |
1683 break; | 1584 break; |
1684 } | 1585 } |
1685 | 1586 |
1686 case naclbitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opval, opval] | 1587 case naclbitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opval, opval] |
1687 size_t OpNum = 0; | 1588 size_t OpNum = 0; |
1688 Value *Vec, *Idx; | 1589 Value *Vec, *Idx; |
1689 if (popValue(Record, &OpNum, NextValueNo, &Vec) || | 1590 if (popValue(Record, &OpNum, NextValueNo, &Vec) || |
1690 popValue(Record, &OpNum, NextValueNo, &Idx) || OpNum != Record.size()) | 1591 popValue(Record, &OpNum, NextValueNo, &Idx) || OpNum != Record.size()) |
1691 return Error(InvalidRecord, "Invalid EXTRACTELEMENT record"); | 1592 return Error("Invalid EXTRACTELEMENT record"); |
1692 | 1593 |
1693 // expect i32 | 1594 // expect i32 |
1694 if (Idx->getType() != Type::getInt32Ty(Context)) | 1595 if (Idx->getType() != Type::getInt32Ty(Context)) |
1695 return Error(InvalidTypeForValue, "Invalid EXTRACTELEMENT index type"); | 1596 return Error("Invalid EXTRACTELEMENT index type"); |
1696 | 1597 |
1697 I = ExtractElementInst::Create(Vec, Idx); | 1598 I = ExtractElementInst::Create(Vec, Idx); |
1698 break; | 1599 break; |
1699 } | 1600 } |
1700 | 1601 |
1701 case naclbitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [opval,opval,opval] | 1602 case naclbitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [opval,opval,opval] |
1702 size_t OpNum = 0; | 1603 size_t OpNum = 0; |
1703 Value *Vec, *Elt, *Idx; | 1604 Value *Vec, *Elt, *Idx; |
1704 if (popValue(Record, &OpNum, NextValueNo, &Vec) || | 1605 if (popValue(Record, &OpNum, NextValueNo, &Vec) || |
1705 popValue(Record, &OpNum, NextValueNo, &Elt) || | 1606 popValue(Record, &OpNum, NextValueNo, &Elt) || |
1706 popValue(Record, &OpNum, NextValueNo, &Idx) || OpNum != Record.size()) | 1607 popValue(Record, &OpNum, NextValueNo, &Idx) || OpNum != Record.size()) |
1707 return Error(InvalidRecord, "Invalid INSERTELEMENT record"); | 1608 return Error("Invalid INSERTELEMENT record"); |
1708 | 1609 |
1709 // expect vector type | 1610 // expect vector type |
1710 if (!isa<VectorType>(Vec->getType())) | 1611 if (!isa<VectorType>(Vec->getType())) |
1711 return Error(InvalidTypeForValue, "Invalid INSERTELEMENT vector type"); | 1612 return Error("Invalid INSERTELEMENT vector type"); |
1712 // match vector and element types | 1613 // match vector and element types |
1713 if (cast<VectorType>(Vec->getType())->getElementType() != Elt->getType()) | 1614 if (cast<VectorType>(Vec->getType())->getElementType() != Elt->getType()) |
1714 return Error(InvalidTypeForValue, | 1615 return Error("Mismatched INSERTELEMENT vector and element type"); |
1715 "Mismatched INSERTELEMENT vector and element type"); | |
1716 // expect i32 | 1616 // expect i32 |
1717 if (Idx->getType() != Type::getInt32Ty(Context)) | 1617 if (Idx->getType() != Type::getInt32Ty(Context)) |
1718 return Error(InvalidTypeForValue, "Invalid INSERTELEMENT index type"); | 1618 return Error("Invalid INSERTELEMENT index type"); |
1719 | 1619 |
1720 I = InsertElementInst::Create(Vec, Elt, Idx); | 1620 I = InsertElementInst::Create(Vec, Elt, Idx); |
1721 break; | 1621 break; |
1722 } | 1622 } |
1723 | 1623 |
1724 case naclbitc::FUNC_CODE_INST_CMP2: { // CMP2: [opval, opval, pred] | 1624 case naclbitc::FUNC_CODE_INST_CMP2: { // CMP2: [opval, opval, pred] |
1725 // FCmp/ICmp returning bool or vector of bool | 1625 // FCmp/ICmp returning bool or vector of bool |
1726 | 1626 |
1727 size_t OpNum = 0; | 1627 size_t OpNum = 0; |
1728 Value *LHS, *RHS; | 1628 Value *LHS, *RHS; |
1729 if (popValue(Record, &OpNum, NextValueNo, &LHS) || | 1629 if (popValue(Record, &OpNum, NextValueNo, &LHS) || |
1730 popValue(Record, &OpNum, NextValueNo, &RHS) || | 1630 popValue(Record, &OpNum, NextValueNo, &RHS) || |
1731 OpNum+1 != Record.size()) | 1631 OpNum+1 != Record.size()) |
1732 return Error(InvalidRecord, "Invalid CMP record"); | 1632 return Error("Invalid CMP record"); |
1733 | 1633 |
1734 LHS = ConvertOpToScalar(LHS, CurBBNo); | 1634 LHS = ConvertOpToScalar(LHS, CurBBNo); |
1735 RHS = ConvertOpToScalar(RHS, CurBBNo); | 1635 RHS = ConvertOpToScalar(RHS, CurBBNo); |
1736 | 1636 |
1737 CmpInst::Predicate Predicate; | 1637 CmpInst::Predicate Predicate; |
1738 if (LHS->getType()->isFPOrFPVectorTy()) { | 1638 if (LHS->getType()->isFPOrFPVectorTy()) { |
1739 if (!naclbitc::DecodeFcmpPredicate(Record[OpNum], Predicate)) | 1639 if (!naclbitc::DecodeFcmpPredicate(Record[OpNum], Predicate)) |
1740 return Error( | 1640 return Error( |
1741 InvalidValue, | |
1742 "PNaCl bitcode contains invalid floating comparison predicate"); | 1641 "PNaCl bitcode contains invalid floating comparison predicate"); |
1743 I = new FCmpInst(Predicate, LHS, RHS); | 1642 I = new FCmpInst(Predicate, LHS, RHS); |
1744 } else { | 1643 } else { |
1745 if (!naclbitc::DecodeIcmpPredicate(Record[OpNum], Predicate)) | 1644 if (!naclbitc::DecodeIcmpPredicate(Record[OpNum], Predicate)) |
1746 return Error( | 1645 return Error( |
1747 InvalidValue, | |
1748 "PNaCl bitcode contains invalid integer comparison predicate"); | 1646 "PNaCl bitcode contains invalid integer comparison predicate"); |
1749 I = new ICmpInst(Predicate, LHS, RHS); | 1647 I = new ICmpInst(Predicate, LHS, RHS); |
1750 } | 1648 } |
1751 break; | 1649 break; |
1752 } | 1650 } |
1753 | 1651 |
1754 case naclbitc::FUNC_CODE_INST_RET: // RET: [opval<optional>] | 1652 case naclbitc::FUNC_CODE_INST_RET: // RET: [opval<optional>] |
1755 { | 1653 { |
1756 size_t Size = Record.size(); | 1654 size_t Size = Record.size(); |
1757 if (Size == 0) { | 1655 if (Size == 0) { |
1758 I = ReturnInst::Create(Context); | 1656 I = ReturnInst::Create(Context); |
1759 break; | 1657 break; |
1760 } | 1658 } |
1761 | 1659 |
1762 size_t OpNum = 0; | 1660 size_t OpNum = 0; |
1763 Value *Op = NULL; | 1661 Value *Op = NULL; |
1764 if (popValue(Record, &OpNum, NextValueNo, &Op)) | 1662 if (popValue(Record, &OpNum, NextValueNo, &Op)) |
1765 return Error(InvalidRecord, "Invalid RET record"); | 1663 return Error("Invalid RET record"); |
1766 if (OpNum != Record.size()) | 1664 if (OpNum != Record.size()) |
1767 return Error(InvalidRecord, "Invalid RET record"); | 1665 return Error("Invalid RET record"); |
1768 | 1666 |
1769 I = ReturnInst::Create(Context, ConvertOpToScalar(Op, CurBBNo)); | 1667 I = ReturnInst::Create(Context, ConvertOpToScalar(Op, CurBBNo)); |
1770 break; | 1668 break; |
1771 } | 1669 } |
1772 case naclbitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] | 1670 case naclbitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
1773 if (Record.size() != 1 && Record.size() != 3) | 1671 if (Record.size() != 1 && Record.size() != 3) |
1774 return Error(InvalidRecord, "Invalid BR record"); | 1672 return Error("Invalid BR record"); |
1775 BasicBlock *TrueDest = getBasicBlock(Record[0]); | 1673 BasicBlock *TrueDest = getBasicBlock(Record[0]); |
1776 if (TrueDest == 0) | 1674 if (TrueDest == 0) |
1777 return Error(InvalidRecord, "Invalid BR record"); | 1675 return Error("Invalid BR record"); |
1778 | 1676 |
1779 if (Record.size() == 1) { | 1677 if (Record.size() == 1) { |
1780 I = BranchInst::Create(TrueDest); | 1678 I = BranchInst::Create(TrueDest); |
1781 } | 1679 } |
1782 else { | 1680 else { |
1783 BasicBlock *FalseDest = getBasicBlock(Record[1]); | 1681 BasicBlock *FalseDest = getBasicBlock(Record[1]); |
1784 Value *Cond = getValue(Record, 2, NextValueNo); | 1682 Value *Cond = getValue(Record, 2, NextValueNo); |
1785 if (FalseDest == 0 || Cond == 0) | 1683 if (FalseDest == 0 || Cond == 0) |
1786 return Error(InvalidValue, "Invalid BR record"); | 1684 return Error("Invalid BR record"); |
1787 if (!Cond->getType()->isIntegerTy(1)) { | 1685 if (!Cond->getType()->isIntegerTy(1)) { |
1788 std::string Buffer; | 1686 std::string Buffer; |
1789 raw_string_ostream StrBuf(Buffer); | 1687 raw_string_ostream StrBuf(Buffer); |
1790 StrBuf << "Type of branch condition not i1. Found: " | 1688 StrBuf << "Type of branch condition not i1. Found: " |
1791 << *Cond->getType() << "\n"; | 1689 << *Cond->getType() << "\n"; |
1792 return Error(InvalidValue, StrBuf.str()); | 1690 return Error(StrBuf.str()); |
1793 } | 1691 } |
1794 I = BranchInst::Create(TrueDest, FalseDest, Cond); | 1692 I = BranchInst::Create(TrueDest, FalseDest, Cond); |
1795 } | 1693 } |
1796 break; | 1694 break; |
1797 } | 1695 } |
1798 case naclbitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] | 1696 case naclbitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
1799 if (Record.size() < 4) | 1697 if (Record.size() < 4) |
1800 return Error(InvalidRecord, "Invalid SWITCH record"); | 1698 return Error("Invalid SWITCH record"); |
1801 Type *OpTy = getTypeByID(Record[0]); | 1699 Type *OpTy = getTypeByID(Record[0]); |
1802 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); | 1700 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
1803 if (ValueBitWidth > 64) | 1701 if (ValueBitWidth > 64) |
1804 return Error(InvalidValue, | 1702 return Error("Wide integers are not supported in PNaCl bitcode"); |
1805 "Wide integers are not supported in PNaCl bitcode"); | |
1806 | 1703 |
1807 Value *Cond = getValue(Record, 1, NextValueNo); | 1704 Value *Cond = getValue(Record, 1, NextValueNo); |
1808 BasicBlock *Default = getBasicBlock(Record[2]); | 1705 BasicBlock *Default = getBasicBlock(Record[2]); |
1809 if (OpTy == 0 || Cond == 0 || Default == 0) | 1706 if (OpTy == 0 || Cond == 0 || Default == 0) |
1810 return Error(InvalidRecord, "Invalid SWITCH record"); | 1707 return Error("Invalid SWITCH record"); |
1811 | 1708 |
1812 Cond = ConvertOpToScalar(Cond, CurBBNo); | 1709 Cond = ConvertOpToScalar(Cond, CurBBNo); |
1813 // TODO(kschimpf): Deal with values that are too large for NumCases. | 1710 // TODO(kschimpf): Deal with values that are too large for NumCases. |
1814 size_t NumCases = Record[3]; | 1711 size_t NumCases = Record[3]; |
1815 | 1712 |
1816 std::unique_ptr<SwitchInst> SI( | 1713 std::unique_ptr<SwitchInst> SI( |
1817 SwitchInst::Create(Cond, Default, NumCases)); | 1714 SwitchInst::Create(Cond, Default, NumCases)); |
1818 | 1715 |
1819 size_t CurIdx = 4; | 1716 size_t CurIdx = 4; |
1820 for (size_t i = 0; i != NumCases; ++i) { | 1717 for (size_t i = 0; i != NumCases; ++i) { |
1821 // The PNaCl bitcode format has vestigial support for case | 1718 // The PNaCl bitcode format has vestigial support for case |
1822 // ranges, but we no longer support reading them because | 1719 // ranges, but we no longer support reading them because |
1823 // no-one produced them. | 1720 // no-one produced them. |
1824 // See https://code.google.com/p/nativeclient/issues/detail?id=3758 | 1721 // See https://code.google.com/p/nativeclient/issues/detail?id=3758 |
1825 if (CurIdx + 3 >= Record.size()) | 1722 if (CurIdx + 3 >= Record.size()) |
1826 return Error(InvalidRecord, | 1723 return Error("Incomplete case entry in SWITCH record"); |
1827 "Incomplete case entry in SWITCH record"); | |
1828 uint64_t NumItems = Record[CurIdx++]; | 1724 uint64_t NumItems = Record[CurIdx++]; |
1829 bool isSingleNumber = Record[CurIdx++]; | 1725 bool isSingleNumber = Record[CurIdx++]; |
1830 if (NumItems != 1 || !isSingleNumber) | 1726 if (NumItems != 1 || !isSingleNumber) |
1831 return Error(InvalidRecord, | 1727 return Error("Case ranges are not supported in PNaCl bitcode"); |
1832 "Case ranges are not supported in PNaCl bitcode"); | |
1833 | 1728 |
1834 APInt CaseValue(ValueBitWidth, | 1729 APInt CaseValue(ValueBitWidth, |
1835 NaClDecodeSignRotatedValue(Record[CurIdx++])); | 1730 NaClDecodeSignRotatedValue(Record[CurIdx++])); |
1836 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); | 1731 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
1837 if (DestBB == nullptr) | 1732 if (DestBB == nullptr) |
1838 return Error(InvalidValue, "Invalid branch in SWITCH case"); | 1733 return Error("Invalid branch in SWITCH case"); |
1839 SI->addCase(ConstantInt::get(Context, CaseValue), DestBB); | 1734 SI->addCase(ConstantInt::get(Context, CaseValue), DestBB); |
1840 } | 1735 } |
1841 I = SI.release(); | 1736 I = SI.release(); |
1842 break; | 1737 break; |
1843 } | 1738 } |
1844 case naclbitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE | 1739 case naclbitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
1845 I = new UnreachableInst(Context); | 1740 I = new UnreachableInst(Context); |
1846 break; | 1741 break; |
1847 case naclbitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] | 1742 case naclbitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
1848 if (Record.size() < 1 || ((Record.size()-1)&1)) | 1743 if (Record.size() < 1 || ((Record.size()-1)&1)) |
1849 return Error(InvalidRecord, "Invalid PHI record"); | 1744 return Error("Invalid PHI record"); |
1850 Type *Ty = getTypeByID(Record[0]); | 1745 Type *Ty = getTypeByID(Record[0]); |
1851 if (!Ty) return Error(InvalidType, "Invalid PHI record"); | 1746 if (!Ty) |
| 1747 return Error("Invalid PHI record"); |
1852 | 1748 |
1853 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); | 1749 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); |
1854 | 1750 |
1855 for (size_t i = 0, e = Record.size()-1; i != e; i += 2) { | 1751 for (size_t i = 0, e = Record.size()-1; i != e; i += 2) { |
1856 Value *V; | 1752 Value *V; |
1857 // With relative value IDs, it is possible that operands have | 1753 // With relative value IDs, it is possible that operands have |
1858 // negative IDs (for forward references). Use a signed VBR | 1754 // negative IDs (for forward references). Use a signed VBR |
1859 // representation to keep the encoding small. | 1755 // representation to keep the encoding small. |
1860 V = getValueSigned(Record, 1+i, NextValueNo); | 1756 V = getValueSigned(Record, 1+i, NextValueNo); |
1861 NaClBcIndexSize_t BBIndex = Record[2+i]; | 1757 NaClBcIndexSize_t BBIndex = Record[2+i]; |
1862 BasicBlock *BB = getBasicBlock(BBIndex); | 1758 BasicBlock *BB = getBasicBlock(BBIndex); |
1863 if (!V || !BB) | 1759 if (!V || !BB) |
1864 return Error(InvalidValue, "Invalid PHI record"); | 1760 return Error("Invalid PHI record"); |
1865 if (Ty == IntPtrType) { | 1761 if (Ty == IntPtrType) { |
1866 // Delay installing scalar casts until all instructions of | 1762 // Delay installing scalar casts until all instructions of |
1867 // the function are rendered. This guarantees that we insert | 1763 // the function are rendered. This guarantees that we insert |
1868 // the conversion just before the incoming edge (or use an | 1764 // the conversion just before the incoming edge (or use an |
1869 // existing conversion if already installed). | 1765 // existing conversion if already installed). |
1870 V = ConvertOpToScalar(V, BBIndex, /* DeferInsertion = */ true); | 1766 V = ConvertOpToScalar(V, BBIndex, /* DeferInsertion = */ true); |
1871 } | 1767 } |
1872 PN->addIncoming(V, BB); | 1768 PN->addIncoming(V, BB); |
1873 } | 1769 } |
1874 I = PN; | 1770 I = PN; |
1875 break; | 1771 break; |
1876 } | 1772 } |
1877 | 1773 |
1878 case naclbitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [op, align] | 1774 case naclbitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [op, align] |
1879 if (Record.size() != 2) | 1775 if (Record.size() != 2) |
1880 return Error(InvalidRecord, "Invalid ALLOCA record"); | 1776 return Error("Invalid ALLOCA record"); |
1881 Value *Size; | 1777 Value *Size; |
1882 size_t OpNum = 0; | 1778 size_t OpNum = 0; |
1883 if (popValue(Record, &OpNum, NextValueNo, &Size)) | 1779 if (popValue(Record, &OpNum, NextValueNo, &Size)) |
1884 return Error(InvalidRecord, "Invalid ALLOCA record"); | 1780 return Error("Invalid ALLOCA record"); |
1885 unsigned Alignment; | 1781 unsigned Alignment; |
1886 if (std::error_code EC = getAlignmentValue(Record[1], Alignment)) | 1782 if (std::error_code EC = getAlignmentValue(Record[1], Alignment)) |
1887 return EC; | 1783 return EC; |
1888 I = new AllocaInst(Type::getInt8Ty(Context), Size, Alignment); | 1784 I = new AllocaInst(Type::getInt8Ty(Context), Size, Alignment); |
1889 break; | 1785 break; |
1890 } | 1786 } |
1891 case naclbitc::FUNC_CODE_INST_LOAD: { | 1787 case naclbitc::FUNC_CODE_INST_LOAD: { |
1892 // LOAD: [op, align, ty] | 1788 // LOAD: [op, align, ty] |
1893 size_t OpNum = 0; | 1789 size_t OpNum = 0; |
1894 Value *Op; | 1790 Value *Op; |
1895 if (popValue(Record, &OpNum, NextValueNo, &Op) || | 1791 if (popValue(Record, &OpNum, NextValueNo, &Op) || |
1896 Record.size() != 3) | 1792 Record.size() != 3) |
1897 return Error(InvalidRecord, "Invalid LOAD record"); | 1793 return Error("Invalid LOAD record"); |
1898 | 1794 |
1899 // Add pointer cast to op. | 1795 // Add pointer cast to op. |
1900 Type *T = getTypeByID(Record[2]); | 1796 Type *T = getTypeByID(Record[2]); |
1901 if (T == nullptr) | 1797 if (T == nullptr) |
1902 return Error(InvalidType, "Invalid type for load instruction"); | 1798 return Error("Invalid type for load instruction"); |
1903 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo); | 1799 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo); |
1904 if (Op == nullptr) | 1800 if (Op == nullptr) |
1905 return Error(InvalidTypeForValue, "Can't convert cast to type"); | 1801 return Error("Can't convert cast to type"); |
1906 unsigned Alignment; | 1802 unsigned Alignment; |
1907 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment)) | 1803 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment)) |
1908 return EC; | 1804 return EC; |
1909 I = new LoadInst(Op, "", false, Alignment); | 1805 I = new LoadInst(Op, "", false, Alignment); |
1910 break; | 1806 break; |
1911 } | 1807 } |
1912 case naclbitc::FUNC_CODE_INST_STORE: { | 1808 case naclbitc::FUNC_CODE_INST_STORE: { |
1913 // STORE: [ptr, val, align] | 1809 // STORE: [ptr, val, align] |
1914 size_t OpNum = 0; | 1810 size_t OpNum = 0; |
1915 Value *Val, *Ptr; | 1811 Value *Val, *Ptr; |
1916 if (popValue(Record, &OpNum, NextValueNo, &Ptr) || | 1812 if (popValue(Record, &OpNum, NextValueNo, &Ptr) || |
1917 popValue(Record, &OpNum, NextValueNo, &Val) || | 1813 popValue(Record, &OpNum, NextValueNo, &Val) || |
1918 OpNum+1 != Record.size()) | 1814 OpNum+1 != Record.size()) |
1919 return Error(InvalidRecord, "Invalid STORE record"); | 1815 return Error("Invalid STORE record"); |
1920 Val = ConvertOpToScalar(Val, CurBBNo); | 1816 Val = ConvertOpToScalar(Val, CurBBNo); |
1921 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo); | 1817 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo); |
1922 if (Ptr == nullptr) | 1818 if (Ptr == nullptr) |
1923 return Error(InvalidTypeForValue, "Can't convert cast to type"); | 1819 return Error("Can't convert cast to type"); |
1924 unsigned Alignment; | 1820 unsigned Alignment; |
1925 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment)) | 1821 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment)) |
1926 return EC; | 1822 return EC; |
1927 I = new StoreInst(Val, Ptr, false, Alignment); | 1823 I = new StoreInst(Val, Ptr, false, Alignment); |
1928 break; | 1824 break; |
1929 } | 1825 } |
1930 case naclbitc::FUNC_CODE_INST_CALL: | 1826 case naclbitc::FUNC_CODE_INST_CALL: |
1931 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: { | 1827 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: { |
1932 // CALL: [cc, fnid, arg0, arg1...] | 1828 // CALL: [cc, fnid, arg0, arg1...] |
1933 // CALL_INDIRECT: [cc, fnid, returnty, args...] | 1829 // CALL_INDIRECT: [cc, fnid, returnty, args...] |
1934 if ((Record.size() < 2) || | 1830 if ((Record.size() < 2) || |
1935 (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT && | 1831 (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT && |
1936 Record.size() < 3)) | 1832 Record.size() < 3)) |
1937 return Error(InvalidRecord, "Invalid CALL record"); | 1833 return Error("Invalid CALL record"); |
1938 | 1834 |
1939 unsigned CCInfo = Record[0]; | 1835 unsigned CCInfo = Record[0]; |
1940 | 1836 |
1941 size_t OpNum = 1; | 1837 size_t OpNum = 1; |
1942 Value *Callee; | 1838 Value *Callee; |
1943 if (popValue(Record, &OpNum, NextValueNo, &Callee)) | 1839 if (popValue(Record, &OpNum, NextValueNo, &Callee)) |
1944 return Error(InvalidRecord, "Invalid CALL record"); | 1840 return Error("Invalid CALL record"); |
1945 | 1841 |
1946 // Build function type for call. | 1842 // Build function type for call. |
1947 FunctionType *FTy = 0; | 1843 FunctionType *FTy = 0; |
1948 Type *ReturnType = 0; | 1844 Type *ReturnType = 0; |
1949 if (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT) { | 1845 if (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT) { |
1950 // Callee type has been elided, add back in. | 1846 // Callee type has been elided, add back in. |
1951 ReturnType = getTypeByID(Record[2]); | 1847 ReturnType = getTypeByID(Record[2]); |
1952 ++OpNum; | 1848 ++OpNum; |
1953 } else { | 1849 } else { |
1954 // Get type signature from callee. | 1850 // Get type signature from callee. |
1955 if (PointerType *OpTy = dyn_cast<PointerType>(Callee->getType())) { | 1851 if (PointerType *OpTy = dyn_cast<PointerType>(Callee->getType())) { |
1956 FTy = dyn_cast<FunctionType>(OpTy->getElementType()); | 1852 FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
1957 } | 1853 } |
1958 if (FTy == 0) | 1854 if (FTy == 0) |
1959 return Error(InvalidType, "Invalid type for CALL record"); | 1855 return Error("Invalid type for CALL record"); |
1960 } | 1856 } |
1961 | 1857 |
1962 size_t NumParams = Record.size() - OpNum; | 1858 size_t NumParams = Record.size() - OpNum; |
1963 if (FTy && NumParams != FTy->getNumParams()) | 1859 if (FTy && NumParams != FTy->getNumParams()) |
1964 return Error(InvalidRecord, "Invalid CALL record"); | 1860 return Error("Invalid CALL record"); |
1965 | 1861 |
1966 // Process call arguments. | 1862 // Process call arguments. |
1967 SmallVector<Value*, 6> Args; | 1863 SmallVector<Value*, 6> Args; |
1968 for (size_t Index = 0; Index < NumParams; ++Index) { | 1864 for (size_t Index = 0; Index < NumParams; ++Index) { |
1969 Value *Arg; | 1865 Value *Arg; |
1970 if (popValue(Record, &OpNum, NextValueNo, &Arg)) { | 1866 if (popValue(Record, &OpNum, NextValueNo, &Arg)) { |
1971 std::string Buffer; | 1867 std::string Buffer; |
1972 raw_string_ostream StrBuf(Buffer); | 1868 raw_string_ostream StrBuf(Buffer); |
1973 StrBuf << "Invalid call argument: Index " << Index; | 1869 StrBuf << "Invalid call argument: Index " << Index; |
1974 return Error(InvalidValue, StrBuf.str()); | 1870 return Error(StrBuf.str()); |
1975 } | 1871 } |
1976 if (FTy) { | 1872 if (FTy) { |
1977 // Add a cast, to a pointer type if necessary, in case this | 1873 // Add a cast, to a pointer type if necessary, in case this |
1978 // is an intrinsic call that takes a pointer argument. | 1874 // is an intrinsic call that takes a pointer argument. |
1979 Arg = ConvertOpToType(Arg, FTy->getParamType(Index), CurBBNo); | 1875 Arg = ConvertOpToType(Arg, FTy->getParamType(Index), CurBBNo); |
1980 } else { | 1876 } else { |
1981 Arg = ConvertOpToScalar(Arg, CurBBNo); | 1877 Arg = ConvertOpToScalar(Arg, CurBBNo); |
1982 } | 1878 } |
1983 if (Arg == nullptr) { | 1879 if (Arg == nullptr) { |
1984 std::string Buffer; | 1880 std::string Buffer; |
1985 raw_string_ostream StrBuf(Buffer); | 1881 raw_string_ostream StrBuf(Buffer); |
1986 StrBuf << "Unable to cast call argument to parameter type: " << Index; | 1882 StrBuf << "Unable to cast call argument to parameter type: " << Index; |
1987 return Error(InvalidValue, StrBuf.str()); | 1883 return Error(StrBuf.str()); |
1988 } | 1884 } |
1989 Args.push_back(Arg); | 1885 Args.push_back(Arg); |
1990 } | 1886 } |
1991 if (FTy == nullptr) { | 1887 if (FTy == nullptr) { |
1992 // Reconstruct the function type and cast the function pointer | 1888 // Reconstruct the function type and cast the function pointer |
1993 // to it. | 1889 // to it. |
1994 SmallVector<Type*, 6> ArgTypes; | 1890 SmallVector<Type*, 6> ArgTypes; |
1995 for (const auto Arg : Args) { | 1891 for (const auto Arg : Args) { |
1996 ArgTypes.push_back(Arg->getType()); | 1892 ArgTypes.push_back(Arg->getType()); |
1997 } | 1893 } |
1998 FTy = FunctionType::get(ReturnType, ArgTypes, false); | 1894 FTy = FunctionType::get(ReturnType, ArgTypes, false); |
1999 Callee = ConvertOpToType(Callee, FTy->getPointerTo(), CurBBNo); | 1895 Callee = ConvertOpToType(Callee, FTy->getPointerTo(), CurBBNo); |
2000 } | 1896 } |
2001 | 1897 |
2002 // Construct call. | 1898 // Construct call. |
2003 I = CallInst::Create(Callee, Args); | 1899 I = CallInst::Create(Callee, Args); |
2004 CallingConv::ID CallingConv; | 1900 CallingConv::ID CallingConv; |
2005 if (!naclbitc::DecodeCallingConv(CCInfo>>1, CallingConv)) | 1901 if (!naclbitc::DecodeCallingConv(CCInfo>>1, CallingConv)) |
2006 return Error(InvalidValue, | 1902 return Error("PNaCl bitcode contains invalid calling conventions."); |
2007 "PNaCl bitcode contains invalid calling conventions."); | |
2008 cast<CallInst>(I)->setCallingConv(CallingConv); | 1903 cast<CallInst>(I)->setCallingConv(CallingConv); |
2009 cast<CallInst>(I)->setTailCall(CCInfo & 1); | 1904 cast<CallInst>(I)->setTailCall(CCInfo & 1); |
2010 break; | 1905 break; |
2011 } | 1906 } |
2012 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: | 1907 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: |
2013 // Build corresponding forward reference. | 1908 // Build corresponding forward reference. |
2014 if (Record.size() != 2 || | 1909 if (Record.size() != 2 || |
2015 ValueList.createValueFwdRef(Record[0], getTypeByID(Record[1]))) | 1910 ValueList.createValueFwdRef(Record[0], getTypeByID(Record[1]))) |
2016 return Error(InvalidRecord, "Invalid FORWARDTYPEREF record"); | 1911 return Error("Invalid FORWARDTYPEREF record"); |
2017 continue; | 1912 continue; |
2018 } | 1913 } |
2019 | 1914 |
2020 if (std::error_code EC = InstallInstruction(CurBB, I)) | 1915 if (std::error_code EC = InstallInstruction(CurBB, I)) |
2021 return EC; | 1916 return EC; |
2022 | 1917 |
2023 // If this was a terminator instruction, move to the next block. | 1918 // If this was a terminator instruction, move to the next block. |
2024 if (isa<TerminatorInst>(I)) { | 1919 if (isa<TerminatorInst>(I)) { |
2025 ++CurBBNo; | 1920 ++CurBBNo; |
2026 CurBB = getBasicBlock(CurBBNo); | 1921 CurBB = getBasicBlock(CurBBNo); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2061 // Check the function list for unresolved values. | 1956 // Check the function list for unresolved values. |
2062 if (Argument *A = dyn_cast<Argument>(ValueList.back())) { | 1957 if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
2063 if (A->getParent() == 0) { | 1958 if (A->getParent() == 0) { |
2064 // We found at least one unresolved value. Nuke them all to avoid leaks. | 1959 // We found at least one unresolved value. Nuke them all to avoid leaks. |
2065 for (size_t i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ | 1960 for (size_t i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
2066 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) { | 1961 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) { |
2067 A->replaceAllUsesWith(UndefValue::get(A->getType())); | 1962 A->replaceAllUsesWith(UndefValue::get(A->getType())); |
2068 delete A; | 1963 delete A; |
2069 } | 1964 } |
2070 } | 1965 } |
2071 return Error(InvalidValue, "Never resolved value found in function!"); | 1966 return Error("Never resolved value found in function!"); |
2072 } | 1967 } |
2073 } | 1968 } |
2074 | 1969 |
2075 if (CurBBNo != FunctionBBs.size()) { | 1970 if (CurBBNo != FunctionBBs.size()) { |
2076 std::string Buffer; | 1971 std::string Buffer; |
2077 raw_string_ostream StrBuf(Buffer); | 1972 raw_string_ostream StrBuf(Buffer); |
2078 StrBuf << "Declared " << FunctionBBs.size() << " basic blocks. Found: " | 1973 StrBuf << "Declared " << FunctionBBs.size() << " basic blocks. Found: " |
2079 << CurBBNo; | 1974 << CurBBNo; |
2080 return Error(MalformedBlock, StrBuf.str()); | 1975 return Error(StrBuf.str()); |
2081 } | 1976 } |
2082 | 1977 |
2083 // Trim the value list down to the size it was before we parsed this function. | 1978 // Trim the value list down to the size it was before we parsed this function. |
2084 ValueList.shrinkTo(ModuleValueListSize); | 1979 ValueList.shrinkTo(ModuleValueListSize); |
2085 FunctionBBs.clear(); | 1980 FunctionBBs.clear(); |
2086 DEBUG(dbgs() << "-> ParseFunctionBody\n"); | 1981 DEBUG(dbgs() << "-> ParseFunctionBody\n"); |
2087 return std::error_code(); | 1982 return std::error_code(); |
2088 } | 1983 } |
2089 | 1984 |
2090 // Find the function body in the bitcode stream | 1985 // Find the function body in the bitcode stream |
2091 std::error_code NaClBitcodeReader::FindFunctionInStream( | 1986 std::error_code NaClBitcodeReader::FindFunctionInStream( |
2092 Function *F, | 1987 Function *F, |
2093 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) { | 1988 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) { |
2094 while (DeferredFunctionInfoIterator->second == 0) { | 1989 while (DeferredFunctionInfoIterator->second == 0) { |
2095 if (Stream.AtEndOfStream()) | 1990 if (Stream.AtEndOfStream()) |
2096 return Error(CouldNotFindFunctionInStream, | 1991 return Error("Could not find Function in stream"); |
2097 "Could not find Function in stream"); | |
2098 // ParseModule will parse the next body in the stream and set its | 1992 // ParseModule will parse the next body in the stream and set its |
2099 // position in the DeferredFunctionInfo map. | 1993 // position in the DeferredFunctionInfo map. |
2100 if (std::error_code EC = ParseModule(true)) | 1994 if (std::error_code EC = ParseModule(true)) |
2101 return EC; | 1995 return EC; |
2102 } | 1996 } |
2103 return std::error_code(); | 1997 return std::error_code(); |
2104 } | 1998 } |
2105 | 1999 |
2106 //===----------------------------------------------------------------------===// | 2000 //===----------------------------------------------------------------------===// |
2107 // GVMaterializer implementation | 2001 // GVMaterializer implementation |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2220 if (LazyStreamer) | 2114 if (LazyStreamer) |
2221 return InitLazyStream(); | 2115 return InitLazyStream(); |
2222 return InitStreamFromBuffer(); | 2116 return InitStreamFromBuffer(); |
2223 } | 2117 } |
2224 | 2118 |
2225 std::error_code NaClBitcodeReader::InitStreamFromBuffer() { | 2119 std::error_code NaClBitcodeReader::InitStreamFromBuffer() { |
2226 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); | 2120 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); |
2227 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | 2121 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
2228 | 2122 |
2229 if (Buffer->getBufferSize() & 3) | 2123 if (Buffer->getBufferSize() & 3) |
2230 return Error(InvalidBitstream, | 2124 return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
2231 "Bitcode stream should be a multiple of 4 bytes in length"); | |
2232 | 2125 |
2233 if (Header.Read(BufPtr, BufEnd)) | 2126 if (Header.Read(BufPtr, BufEnd)) |
2234 return Error(InvalidBitstream, Header.Unsupported()); | 2127 return Error(Header.Unsupported()); |
2235 | 2128 |
2236 if (AcceptHeader()) | 2129 if (AcceptHeader()) |
2237 return Error(InvalidBitstream, Header.Unsupported()); | 2130 return Error(Header.Unsupported()); |
2238 | 2131 |
2239 StreamFile.reset(new NaClBitstreamReader(BufPtr, BufEnd, Header)); | 2132 StreamFile.reset(new NaClBitstreamReader(BufPtr, BufEnd, Header)); |
2240 Stream.init(StreamFile.get()); | 2133 Stream.init(StreamFile.get()); |
2241 | 2134 |
2242 return std::error_code(); | 2135 return std::error_code(); |
2243 } | 2136 } |
2244 | 2137 |
2245 std::error_code NaClBitcodeReader::InitLazyStream() { | 2138 std::error_code NaClBitcodeReader::InitLazyStream() { |
2246 if (Header.Read(LazyStreamer)) | 2139 if (Header.Read(LazyStreamer)) |
2247 return Error(InvalidBitstream, Header.Unsupported()); | 2140 return Error(Header.Unsupported()); |
2248 | 2141 |
2249 if (AcceptHeader()) | 2142 if (AcceptHeader()) |
2250 return Error(InvalidBitstream, Header.Unsupported()); | 2143 return Error(Header.Unsupported()); |
2251 | 2144 |
2252 StreamFile.reset(new NaClBitstreamReader(LazyStreamer, Header)); | 2145 StreamFile.reset(new NaClBitstreamReader(LazyStreamer, Header)); |
2253 Stream.init(StreamFile.get()); | 2146 Stream.init(StreamFile.get()); |
2254 return std::error_code(); | 2147 return std::error_code(); |
2255 } | 2148 } |
2256 | 2149 |
2257 } // end of anonymous namespace | 2150 } // end of anonymous namespace |
2258 | 2151 |
2259 //===----------------------------------------------------------------------===// | 2152 //===----------------------------------------------------------------------===// |
2260 // External interface | 2153 // External interface |
2261 //===----------------------------------------------------------------------===// | 2154 //===----------------------------------------------------------------------===// |
2262 | 2155 |
2263 cl::opt<bool> llvm::PNaClAllowLocalSymbolTables( | 2156 cl::opt<bool> llvm::PNaClAllowLocalSymbolTables( |
2264 "allow-local-symbol-tables", | 2157 "allow-local-symbol-tables", |
2265 cl::desc("Allow (function) local symbol tables in PNaCl bitcode files"), | 2158 cl::desc("Allow (function) local symbol tables in PNaCl bitcode files"), |
2266 cl::init(false)); | 2159 cl::init(false)); |
2267 | 2160 |
2268 const char *llvm::PNaClDataLayout = | 2161 const char *llvm::PNaClDataLayout = |
2269 "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-" | 2162 "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-" |
2270 "f32:32:32-f64:64:64-p:32:32:32-v128:32:32"; | 2163 "f32:32:32-f64:64:64-p:32:32:32-v128:32:32"; |
2271 | 2164 |
| 2165 DiagnosticHandlerFunction |
| 2166 llvm::redirectNaClDiagnosticToStream(raw_ostream &Out) { |
| 2167 return [&](const DiagnosticInfo &DI) { |
| 2168 switch (DI.getSeverity()) { |
| 2169 case DS_Error: |
| 2170 Out << "Error: "; |
| 2171 break; |
| 2172 case DS_Warning: |
| 2173 Out << "Warning: "; |
| 2174 case DS_Remark: |
| 2175 case DS_Note: |
| 2176 break; |
| 2177 } |
| 2178 // Now print diagnostic error message. |
| 2179 DiagnosticPrinterRawOStream Printer(Out); |
| 2180 DI.print(Printer); |
| 2181 Out << "\n"; |
| 2182 }; |
| 2183 } |
| 2184 |
2272 /// \brief Get a lazy one-at-time loading module from bitcode. | 2185 /// \brief Get a lazy one-at-time loading module from bitcode. |
2273 /// | 2186 /// |
2274 /// This isn't always used in a lazy context. In particular, it's also used by | 2187 /// This isn't always used in a lazy context. In particular, it's also used by |
2275 /// \a NaClParseBitcodeFile(). Compared to the upstream LLVM bitcode reader, | 2188 /// \a NaClParseBitcodeFile(). Compared to the upstream LLVM bitcode reader, |
2276 /// NaCl does not support BlockAddresses, so it does not need to materialize | 2189 /// NaCl does not support BlockAddresses, so it does not need to materialize |
2277 /// forward-referenced functions from block address references. | 2190 /// forward-referenced functions from block address references. |
2278 ErrorOr<Module *> llvm::getNaClLazyBitcodeModule( | 2191 ErrorOr<Module *> llvm::getNaClLazyBitcodeModule( |
2279 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext& Context, | 2192 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, |
2280 raw_ostream *Verbose, bool AcceptSupportedOnly) { | 2193 DiagnosticHandlerFunction DiagnosticHandler, bool AcceptSupportedOnly) { |
2281 Module *M = new Module(Buffer->getBufferIdentifier(), Context); | 2194 Module *M = new Module(Buffer->getBufferIdentifier(), Context); |
2282 NaClBitcodeReader *R = | 2195 NaClBitcodeReader *R = new NaClBitcodeReader( |
2283 new NaClBitcodeReader(Buffer.get(), Context, Verbose, AcceptSupportedOnly)
; | 2196 Buffer.get(), Context, DiagnosticHandler, AcceptSupportedOnly); |
2284 M->setMaterializer(R); | 2197 M->setMaterializer(R); |
2285 | 2198 |
2286 auto cleanupOnError = [&](std::error_code EC) { | 2199 auto cleanupOnError = [&](std::error_code EC) { |
2287 R->releaseBuffer(); // Never take ownership on error. | 2200 R->releaseBuffer(); // Never take ownership on error. |
2288 delete M; // Also deletes R. | 2201 delete M; // Also deletes R. |
2289 return EC; | 2202 return EC; |
2290 }; | 2203 }; |
2291 | 2204 |
2292 if (std::error_code EC = R->ParseBitcodeInto(M)) | 2205 if (std::error_code EC = R->ParseBitcodeInto(M)) |
2293 return cleanupOnError(EC); | 2206 return cleanupOnError(EC); |
2294 | 2207 |
2295 Buffer.release(); // The BitcodeReader owns it now. | 2208 Buffer.release(); // The BitcodeReader owns it now. |
2296 return M; | 2209 return M; |
2297 } | 2210 } |
2298 | 2211 |
2299 | 2212 Module *llvm::getNaClStreamedBitcodeModule( |
2300 Module *llvm::getNaClStreamedBitcodeModule(const std::string &name, | 2213 const std::string &name, StreamingMemoryObject *Streamer, |
2301 StreamingMemoryObject *Streamer, | 2214 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler, |
2302 LLVMContext &Context, | 2215 std::string *ErrMsg, bool AcceptSupportedOnly) { |
2303 raw_ostream *Verbose, | |
2304 std::string *ErrMsg, | |
2305 bool AcceptSupportedOnly) { | |
2306 Module *M = new Module(name, Context); | 2216 Module *M = new Module(name, Context); |
2307 NaClBitcodeReader *R = | 2217 NaClBitcodeReader *R = new NaClBitcodeReader( |
2308 new NaClBitcodeReader(Streamer, Context, Verbose, | 2218 Streamer, Context, DiagnosticHandler, AcceptSupportedOnly); |
2309 AcceptSupportedOnly); | |
2310 M->setMaterializer(R); | 2219 M->setMaterializer(R); |
2311 if (std::error_code EC = R->ParseBitcodeInto(M)) { | 2220 if (std::error_code EC = R->ParseBitcodeInto(M)) { |
2312 if (ErrMsg) | 2221 if (ErrMsg) |
2313 *ErrMsg = EC.message(); | 2222 *ErrMsg = EC.message(); |
2314 delete M; // Also deletes R. | 2223 delete M; // Also deletes R. |
2315 return nullptr; | 2224 return nullptr; |
2316 } | 2225 } |
2317 | 2226 |
2318 return M; | 2227 return M; |
2319 } | 2228 } |
2320 | 2229 |
2321 ErrorOr<Module *> llvm::NaClParseBitcodeFile( | 2230 ErrorOr<Module *> |
2322 MemoryBufferRef Buffer, LLVMContext& Context, raw_ostream *Verbose, | 2231 llvm::NaClParseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, |
2323 bool AcceptSupportedOnly){ | 2232 DiagnosticHandlerFunction DiagnosticHandler, |
| 2233 bool AcceptSupportedOnly) { |
2324 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); | 2234 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); |
2325 ErrorOr<Module *> ModuleOrErr = | 2235 ErrorOr<Module *> ModuleOrErr = getNaClLazyBitcodeModule( |
2326 getNaClLazyBitcodeModule(std::move(Buf), Context, Verbose, | 2236 std::move(Buf), Context, DiagnosticHandler, AcceptSupportedOnly); |
2327 AcceptSupportedOnly); | |
2328 if (!ModuleOrErr) | 2237 if (!ModuleOrErr) |
2329 return ModuleOrErr; | 2238 return ModuleOrErr; |
2330 Module *M = ModuleOrErr.get(); | 2239 Module *M = ModuleOrErr.get(); |
2331 // Read in the entire module, and destroy the NaClBitcodeReader. | 2240 // Read in the entire module, and destroy the NaClBitcodeReader. |
2332 if (std::error_code EC = M->materializeAllPermanently()) { | 2241 if (std::error_code EC = M->materializeAllPermanently()) { |
2333 delete M; | 2242 delete M; |
2334 return EC; | 2243 return EC; |
2335 } | 2244 } |
2336 | 2245 |
2337 // TODO: Restore the use-lists to the in-memory state when the bitcode was | 2246 // TODO: Restore the use-lists to the in-memory state when the bitcode was |
2338 // written. We must defer until the Module has been fully materialized. | 2247 // written. We must defer until the Module has been fully materialized. |
2339 | 2248 |
2340 return M; | 2249 return M; |
2341 } | 2250 } |
OLD | NEW |