llvm.org GIT mirror llvm / 2ea9387
Enable streaming of bitcode This CL delays reading of function bodies from initial parse until materialization, allowing overlap of compilation with bitcode download. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149918 91177308-0d34-0410-b5e6-96231b3b80d8 Derek Schuff 8 years ago
26 changed file(s) with 753 addition(s) and 168 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef BITSTREAM_READER_H
1515 #define BITSTREAM_READER_H
1616
17 #include "llvm/ADT/OwningPtr.h"
1718 #include "llvm/Bitcode/BitCodes.h"
1819 #include
1920 #include
2021 #include
22 #include "llvm/Support/StreamableMemoryObject.h"
2123
2224 namespace llvm {
2325
3537 std::vector > RecordNames;
3638 };
3739 private:
38 /// FirstChar/LastChar - This remembers the first and last bytes of the
39 /// stream.
40 const unsigned char *FirstChar, *LastChar;
40 OwningPtr BitcodeBytes;
4141
4242 std::vector BlockInfoRecords;
4343
4646 /// uses this.
4747 bool IgnoreBlockInfoNames;
4848
49 BitstreamReader(const BitstreamReader&); // NOT IMPLEMENTED
50 void operator=(const BitstreamReader&); // NOT IMPLEMENTED
49 BitstreamReader(const BitstreamReader&); // DO NOT IMPLEMENT
50 void operator=(const BitstreamReader&); // DO NOT IMPLEMENT
5151 public:
52 BitstreamReader() : FirstChar(0), LastChar(0), IgnoreBlockInfoNames(true) {
52 BitstreamReader() : IgnoreBlockInfoNames(true) {
5353 }
5454
5555 BitstreamReader(const unsigned char *Start, const unsigned char *End) {
5757 init(Start, End);
5858 }
5959
60 BitstreamReader(StreamableMemoryObject *bytes) {
61 BitcodeBytes.reset(bytes);
62 }
63
6064 void init(const unsigned char *Start, const unsigned char *End) {
61 FirstChar = Start;
62 LastChar = End;
6365 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
64 }
66 BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End));
67 }
68
69 StreamableMemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
6570
6671 ~BitstreamReader() {
6772 // Free the BlockInfoRecords.
7479 BlockInfoRecords.pop_back();
7580 }
7681 }
77
78 const unsigned char *getFirstChar() const { return FirstChar; }
79 const unsigned char *getLastChar() const { return LastChar; }
8082
8183 /// CollectBlockInfoNames - This is called by clients that want block/record
8284 /// name information.
121123 class BitstreamCursor {
122124 friend class Deserializer;
123125 BitstreamReader *BitStream;
124 const unsigned char *NextChar;
126 size_t NextChar;
125127
126128 /// CurWord - This is the current data we have pulled from the stream but have
127129 /// not returned to the client.
155157 }
156158
157159 explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
158 NextChar = R.getFirstChar();
159 assert(NextChar && "Bitstream not initialized yet");
160 NextChar = 0;
160161 CurWord = 0;
161162 BitsInCurWord = 0;
162163 CurCodeSize = 2;
166167 freeState();
167168
168169 BitStream = &R;
169 NextChar = R.getFirstChar();
170 assert(NextChar && "Bitstream not initialized yet");
170 NextChar = 0;
171171 CurWord = 0;
172172 BitsInCurWord = 0;
173173 CurCodeSize = 2;
224224 /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
225225 unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
226226
227 bool AtEndOfStream() const {
228 return NextChar == BitStream->getLastChar() && BitsInCurWord == 0;
227 bool isEndPos(size_t pos) {
228 return BitStream->getBitcodeBytes().isObjectEnd(static_cast(pos));
229 }
230
231 bool canSkipToPos(size_t pos) const {
232 // pos can be skipped to if it is a valid address or one byte past the end.
233 return pos == 0 || BitStream->getBitcodeBytes().isValidAddress(
234 static_cast(pos - 1));
235 }
236
237 unsigned char getByte(size_t pos) {
238 uint8_t byte = -1;
239 BitStream->getBitcodeBytes().readByte(pos, &byte);
240 return byte;
241 }
242
243 uint32_t getWord(size_t pos) {
244 uint32_t word = -1;
245 BitStream->getBitcodeBytes().readBytes(pos,
246 sizeof(word),
247 reinterpret_cast(&word),
248 NULL);
249 return word;
250 }
251
252 bool AtEndOfStream() {
253 return isEndPos(NextChar) && BitsInCurWord == 0;
229254 }
230255
231256 /// GetCurrentBitNo - Return the bit # of the bit we are reading.
232257 uint64_t GetCurrentBitNo() const {
233 return (NextChar-BitStream->getFirstChar())*CHAR_BIT - BitsInCurWord;
258 return NextChar*CHAR_BIT - BitsInCurWord;
234259 }
235260
236261 BitstreamReader *getBitStreamReader() {
245270 void JumpToBit(uint64_t BitNo) {
246271 uintptr_t ByteNo = uintptr_t(BitNo/8) & ~3;
247272 uintptr_t WordBitNo = uintptr_t(BitNo) & 31;
248 assert(ByteNo <= (uintptr_t)(BitStream->getLastChar()-
249 BitStream->getFirstChar()) &&
250 "Invalid location");
273 assert(canSkipToPos(ByteNo) && "Invalid location");
251274
252275 // Move the cursor to the right word.
253 NextChar = BitStream->getFirstChar()+ByteNo;
276 NextChar = ByteNo;
254277 BitsInCurWord = 0;
255278 CurWord = 0;
256279
271294 }
272295
273296 // If we run out of data, stop at the end of the stream.
274 if (NextChar == BitStream->getLastChar()) {
297 if (isEndPos(NextChar)) {
275298 CurWord = 0;
276299 BitsInCurWord = 0;
277300 return 0;
280303 unsigned R = CurWord;
281304
282305 // Read the next word from the stream.
283 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
284 (NextChar[2] << 16) | (NextChar[3] << 24);
306 CurWord = getWord(NextChar);
285307 NextChar += 4;
286308
287309 // Extract NumBits-BitsInCurWord from what we just read.
375397
376398 // Check that the block wasn't partially defined, and that the offset isn't
377399 // bogus.
378 const unsigned char *const SkipTo = NextChar + NumWords*4;
379 if (AtEndOfStream() || SkipTo > BitStream->getLastChar() ||
380 SkipTo < BitStream->getFirstChar())
400 size_t SkipTo = NextChar + NumWords*4;
401 if (AtEndOfStream() || !canSkipToPos(SkipTo))
381402 return true;
382403
383404 NextChar = SkipTo;
408429 if (NumWordsP) *NumWordsP = NumWords;
409430
410431 // Validate that this block is sane.
411 if (CurCodeSize == 0 || AtEndOfStream() ||
412 NextChar+NumWords*4 > BitStream->getLastChar())
432 if (CurCodeSize == 0 || AtEndOfStream())
413433 return true;
414434
415435 return false;
511531 SkipToWord(); // 32-bit alignment
512532
513533 // Figure out where the end of this blob will be including tail padding.
514 const unsigned char *NewEnd = NextChar+((NumElts+3)&~3);
534 size_t NewEnd = NextChar+((NumElts+3)&~3);
515535
516536 // If this would read off the end of the bitcode file, just set the
517537 // record to empty and return.
518 if (NewEnd > BitStream->getLastChar()) {
538 if (!canSkipToPos(NewEnd)) {
519539 Vals.append(NumElts, 0);
520 NextChar = BitStream->getLastChar();
540 NextChar = BitStream->getBitcodeBytes().getExtent();
521541 break;
522542 }
523543
524544 // Otherwise, read the number of bytes. If we can return a reference to
525545 // the data, do so to avoid copying it.
526546 if (BlobStart) {
527 *BlobStart = (const char*)NextChar;
547 *BlobStart = (const char*)BitStream->getBitcodeBytes().getPointer(
548 NextChar, NumElts);
528549 *BlobLen = NumElts;
529550 } else {
530551 for (; NumElts; ++NextChar, --NumElts)
531 Vals.push_back(*NextChar);
552 Vals.push_back(getByte(NextChar));
532553 }
533554 // Skip over tail padding.
534555 NextChar = NewEnd;
1616 #include
1717
1818 namespace llvm {
19 class BitstreamWriter;
20 class MemoryBuffer;
21 class DataStreamer;
22 class LLVMContext;
1923 class Module;
20 class MemoryBuffer;
2124 class ModulePass;
22 class BitstreamWriter;
23 class LLVMContext;
2425 class raw_ostream;
25
26
2627 /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
2728 /// and prepare for lazy deserialization of function bodies. If successful,
2829 /// this takes ownership of 'buffer' and returns a non-null pointer. On
2930 /// error, this returns null, *does not* take ownership of Buffer, and fills
3031 /// in *ErrMsg with an error description if ErrMsg is non-null.
3132 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
32 LLVMContext& Context,
33 LLVMContext &Context,
3334 std::string *ErrMsg = 0);
35
36 /// getStreamedBitcodeModule - Read the header of the specified stream
37 /// and prepare for lazy deserialization and streaming of function bodies.
38 /// On error, this returns null, and fills in *ErrMsg with an error
39 /// description if ErrMsg is non-null.
40 Module *getStreamedBitcodeModule(const std::string &name,
41 DataStreamer *streamer,
42 LLVMContext &Context,
43 std::string *ErrMsg = 0);
3444
3545 /// getBitcodeTargetTriple - Read the header of the specified bitcode
3646 /// buffer and extract just the triple information. If successful,
3848 /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
3949 /// if ErrMsg is non-null.
4050 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
41 LLVMContext& Context,
51 LLVMContext &Context,
4252 std::string *ErrMsg = 0);
4353
4454 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
4555 /// If an error occurs, this returns null and fills in *ErrMsg if it is
4656 /// non-null. This method *never* takes ownership of Buffer.
47 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
57 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
4858 std::string *ErrMsg = 0);
4959
5060 /// WriteBitcodeToFile - Write the specified module to the specified
5969 /// createBitcodeWriterPass - Create and return a pass that writes the module
6070 /// to the specified ostream.
6171 ModulePass *createBitcodeWriterPass(raw_ostream &Str);
62
63
72
73
6474 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
6575 /// for an LLVM IR bitcode wrapper.
6676 ///
108118 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
109119 /// ... potentially other gunk ...
110120 /// };
111 ///
121 ///
112122 /// This function is called when we find a file with a matching magic number.
113123 /// In this case, skip down to the subsection of the file that is actually a
114124 /// BC file.
115 static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
116 unsigned char *&BufEnd) {
125 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
126 /// contain the whole bitcode file.
127 static inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
128 const unsigned char *&BufEnd,
129 bool VerifyBufferSize) {
117130 enum {
118131 KnownHeaderSize = 4*4, // Size of header we read.
119132 OffsetField = 2*4, // Offset in bytes to Offset field.
120133 SizeField = 3*4 // Offset in bytes to Size field.
121134 };
122
135
123136 // Must contain the header!
124137 if (BufEnd-BufPtr < KnownHeaderSize) return true;
125
138
126139 unsigned Offset = ( BufPtr[OffsetField ] |
127140 (BufPtr[OffsetField+1] << 8) |
128141 (BufPtr[OffsetField+2] << 16) |
131144 (BufPtr[SizeField +1] << 8) |
132145 (BufPtr[SizeField +2] << 16) |
133146 (BufPtr[SizeField +3] << 24));
134
147
135148 // Verify that Offset+Size fits in the file.
136 if (Offset+Size > unsigned(BufEnd-BufPtr))
149 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
137150 return true;
138151 BufPtr += Offset;
139152 BufEnd = BufPtr+Size;
7878 /// MCDisassembler::Fail if the instruction was invalid.
7979 virtual DecodeStatus getInstruction(MCInst& instr,
8080 uint64_t& size,
81 const MemoryObject ®ion,
81 MemoryObject ®ion,
8282 uint64_t address,
8383 raw_ostream &vStream,
8484 raw_ostream &cStream) const = 0;
0 //===---- llvm/Support/DataStream.h - Lazy bitcode streaming -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines DataStreamer, which fetches bytes of data from
10 // a stream source. It provides support for streaming (lazy reading) of
11 // data, e.g. bitcode
12 //
13 //===----------------------------------------------------------------------===//
14
15
16 #ifndef LLVM_SUPPORT_DATASTREAM_H_
17 #define LLVM_SUPPORT_DATASTREAM_H_
18
19 #include
20
21 namespace llvm {
22
23 class DataStreamer {
24 public:
25 /// Fetch bytes [start-end) from the stream, and write them to the
26 /// buffer pointed to by buf. Returns the number of bytes actually written.
27 virtual size_t GetBytes(unsigned char *buf, size_t len) = 0;
28
29 virtual ~DataStreamer();
30 };
31
32 DataStreamer *getDataFileStreamer(const std::string &Filename,
33 std::string *Err);
34
35 }
36
37 #endif // LLVM_SUPPORT_DATASTREAM_H_
3333 /// is getBase() + getExtent() - 1).
3434 ///
3535 /// @result - The size of the region.
36 virtual uint64_t getExtent() const = 0;
36 virtual uint64_t getExtent() = 0;
3737
3838 /// readByte - Tries to read a single byte from the region.
3939 ///
4141 /// @param ptr - A pointer to a byte to be filled in. Must be non-NULL.
4242 /// @result - 0 if successful; -1 if not. Failure may be due to a
4343 /// bounds violation or an implementation-specific error.
44 virtual int readByte(uint64_t address, uint8_t* ptr) const = 0;
44 virtual int readByte(uint64_t address, uint8_t* ptr) = 0;
4545
4646 /// readBytes - Tries to read a contiguous range of bytes from the
4747 /// region, up to the end of the region.
6060 virtual int readBytes(uint64_t address,
6161 uint64_t size,
6262 uint8_t* buf,
63 uint64_t* copied) const;
63 uint64_t* copied);
6464 };
6565
6666 }
0 //===- StreamableMemoryObject.h - Streamable data interface - -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9
10 #ifndef STREAMABLEMEMORYOBJECT_H_
11 #define STREAMABLEMEMORYOBJECT_H_
12
13 #include "llvm/ADT/OwningPtr.h"
14 #include "llvm/Support/MemoryObject.h"
15 #include "llvm/Support/DataStream.h"
16 #include
17
18 namespace llvm {
19
20 /// StreamableMemoryObject - Interface to data which might be streamed.
21 /// Streamability has 2 important implications/restrictions. First, the data
22 /// might not yet exist in memory when the request is made. This just means
23 /// that readByte/readBytes might have to block or do some work to get it.
24 /// More significantly, the exact size of the object might not be known until
25 /// it has all been fetched. This means that to return the right result,
26 /// getExtent must also wait for all the data to arrive; therefore it should
27 /// not be called on objects which are actually streamed (this would defeat
28 /// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
29 /// used to test addresses without knowing the exact size of the stream.
30 /// Finally, getPointer can be used instead of readBytes to avoid extra copying.
31 class StreamableMemoryObject : public MemoryObject {
32 public:
33 /// Destructor - Override as necessary.
34 virtual ~StreamableMemoryObject();
35
36 /// getBase - Returns the lowest valid address in the region.
37 ///
38 /// @result - The lowest valid address.
39 virtual uint64_t getBase() const = 0;
40
41 /// getExtent - Returns the size of the region in bytes. (The region is
42 /// contiguous, so the highest valid address of the region
43 /// is getBase() + getExtent() - 1).
44 /// May block until all bytes in the stream have been read
45 ///
46 /// @result - The size of the region.
47 virtual uint64_t getExtent() = 0;
48
49 /// readByte - Tries to read a single byte from the region.
50 /// May block until (address - base) bytes have been read
51 /// @param address - The address of the byte, in the same space as getBase().
52 /// @param ptr - A pointer to a byte to be filled in. Must be non-NULL.
53 /// @result - 0 if successful; -1 if not. Failure may be due to a
54 /// bounds violation or an implementation-specific error.
55 virtual int readByte(uint64_t address, uint8_t* ptr) = 0;
56
57 /// readBytes - Tries to read a contiguous range of bytes from the
58 /// region, up to the end of the region.
59 /// May block until (address - base + size) bytes have
60 /// been read. Additionally, StreamableMemoryObjects will
61 /// not do partial reads - if size bytes cannot be read,
62 /// readBytes will fail.
63 ///
64 /// @param address - The address of the first byte, in the same space as
65 /// getBase().
66 /// @param size - The maximum number of bytes to copy.
67 /// @param buf - A pointer to a buffer to be filled in. Must be non-NULL
68 /// and large enough to hold size bytes.
69 /// @param copied - A pointer to a nunber that is filled in with the number
70 /// of bytes actually read. May be NULL.
71 /// @result - 0 if successful; -1 if not. Failure may be due to a
72 /// bounds violation or an implementation-specific error.
73 virtual int readBytes(uint64_t address,
74 uint64_t size,
75 uint8_t* buf,
76 uint64_t* copied) = 0;
77
78 /// getPointer - Ensures that the requested data is in memory, and returns
79 /// A pointer to it. More efficient than using readBytes if the
80 /// data is already in memory.
81 /// May block until (address - base + size) bytes have been read
82 /// @param address - address of the byte, in the same space as getBase()
83 /// @param size - amount of data that must be available on return
84 /// @result - valid pointer to the requested data
85 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) = 0;
86
87 /// isValidAddress - Returns true if the address is within the object
88 /// (i.e. between base and base + extent - 1 inclusive)
89 /// May block until (address - base) bytes have been read
90 /// @param address - address of the byte, in the same space as getBase()
91 /// @result - true if the address may be read with readByte()
92 virtual bool isValidAddress(uint64_t address) = 0;
93
94 /// isObjectEnd - Returns true if the address is one past the end of the
95 /// object (i.e. if it is equal to base + extent)
96 /// May block until (address - base) bytes have been read
97 /// @param address - address of the byte, in the same space as getBase()
98 /// @result - true if the address is equal to base + extent
99 virtual bool isObjectEnd(uint64_t address) = 0;
100 };
101
102 /// StreamingMemoryObject - interface to data which is actually streamed from
103 /// a DataStreamer. In addition to inherited members, it has the
104 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
105 /// to non-streamed objects.
106 class StreamingMemoryObject : public StreamableMemoryObject {
107 public:
108 StreamingMemoryObject(DataStreamer *streamer);
109 virtual uint64_t getBase() const { return 0; }
110 virtual uint64_t getExtent();
111 virtual int readByte(uint64_t address, uint8_t* ptr);
112 virtual int readBytes(uint64_t address,
113 uint64_t size,
114 uint8_t* buf,
115 uint64_t* copied);
116 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) {
117 // This could be fixed by ensuring the bytes are fetched and making a copy,
118 // requiring that the bitcode size be known, or otherwise ensuring that
119 // the memory doesn't go away/get reallocated, but it's
120 // not currently necessary. Users that need the pointer don't stream.
121 assert(0 && "getPointer in streaming memory objects not allowed");
122 return NULL;
123 }
124 virtual bool isValidAddress(uint64_t address);
125 virtual bool isObjectEnd(uint64_t address);
126
127 /// Drop s bytes from the front of the stream, pushing the positions of the
128 /// remaining bytes down by s. This is used to skip past the bitcode header,
129 /// since we don't know a priori if it's present, and we can't put bytes
130 /// back into the stream once we've read them.
131 bool dropLeadingBytes(size_t s);
132
133 /// If the data object size is known in advance, many of the operations can
134 /// be made more efficient, so this method should be called before reading
135 /// starts (although it can be called anytime).
136 void setKnownObjectSize(size_t size);
137
138 private:
139 const static uint32_t kChunkSize = 4096 * 4;
140 std::vector Bytes;
141 OwningPtr Streamer;
142 size_t BytesRead; // Bytes read from stream
143 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
144 size_t ObjectSize; // 0 if unknown, set if wrapper was seen or EOF reached
145 bool EOFReached;
146
147 // Fetch enough bytes such that Pos can be read or EOF is reached
148 // (i.e. BytesRead > Pos). Return true if Pos can be read.
149 // Unlike most of the functions in BitcodeReader, returns true on success.
150 // Most of the requests will be small, but we fetch at kChunkSize bytes
151 // at a time to avoid making too many potentially expensive GetBytes calls
152 bool fetchToPos(size_t Pos) {
153 if (EOFReached) return Pos < ObjectSize;
154 while (Pos >= BytesRead) {
155 Bytes.resize(BytesRead + kChunkSize);
156 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
157 kChunkSize);
158 BytesRead += bytes;
159 if (bytes < kChunkSize) {
160 if (ObjectSize && BytesRead < Pos)
161 assert(0 && "Unexpected short read fetching bitcode");
162 if (BytesRead <= Pos) { // reached EOF/ran out of bytes
163 ObjectSize = BytesRead;
164 EOFReached = true;
165 return false;
166 }
167 }
168 }
169 return true;
170 }
171
172 StreamingMemoryObject(const StreamingMemoryObject&); // DO NOT IMPLEMENT
173 void operator=(const StreamingMemoryObject&); // DO NOT IMPLEMENT
174 };
175
176 StreamableMemoryObject *getNonStreamedMemoryObject(
177 const unsigned char *Start, const unsigned char *End);
178
179 }
180 #endif // STREAMABLEMEMORYOBJECT_H_
2121 #include "llvm/AutoUpgrade.h"
2222 #include "llvm/ADT/SmallString.h"
2323 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/DataStream.h"
2425 #include "llvm/Support/MathExtras.h"
2526 #include "llvm/Support/MemoryBuffer.h"
2627 #include "llvm/OperandTraits.h"
14081409 return false;
14091410 }
14101411
1411 bool BitcodeReader::ParseModule() {
1412 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1412 bool BitcodeReader::GlobalCleanup() {
1413 // Patch the initializers for globals and aliases up.
1414 ResolveGlobalAndAliasInits();
1415 if (!GlobalInits.empty() || !AliasInits.empty())
1416 return Error("Malformed global initializer set");
1417
1418 // Look for intrinsic functions which need to be upgraded at some point
1419 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1420 FI != FE; ++FI) {
1421 Function *NewFn;
1422 if (UpgradeIntrinsicFunction(FI, NewFn))
1423 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1424 }
1425
1426 // Look for global variables which need to be renamed.
1427 for (Module::global_iterator
1428 GI = TheModule->global_begin(), GE = TheModule->global_end();
1429 GI != GE; ++GI)
1430 UpgradeGlobalVariable(GI);
1431 // Force deallocation of memory for these vectors to favor the client that
1432 // want lazy deserialization.
1433 std::vector >().swap(GlobalInits);
1434 std::vector >().swap(AliasInits);
1435 return false;
1436 }
1437
1438 bool BitcodeReader::ParseModule(bool Resume) {
1439 if (Resume)
1440 Stream.JumpToBit(NextUnreadBit);
1441 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
14131442 return Error("Malformed block record");
14141443
14151444 SmallVector Record;
14231452 if (Stream.ReadBlockEnd())
14241453 return Error("Error at end of module block");
14251454
1426 // Patch the initializers for globals and aliases up.
1427 ResolveGlobalAndAliasInits();
1428 if (!GlobalInits.empty() || !AliasInits.empty())
1429 return Error("Malformed global initializer set");
1430 if (!FunctionsWithBodies.empty())
1431 return Error("Too few function bodies found");
1432
1433 // Look for intrinsic functions which need to be upgraded at some point
1434 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1435 FI != FE; ++FI) {
1436 Function* NewFn;
1437 if (UpgradeIntrinsicFunction(FI, NewFn))
1438 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1439 }
1440
1441 // Look for global variables which need to be renamed.
1442 for (Module::global_iterator
1443 GI = TheModule->global_begin(), GE = TheModule->global_end();
1444 GI != GE; ++GI)
1445 UpgradeGlobalVariable(GI);
1446
1447 // Force deallocation of memory for these vectors to favor the client that
1448 // want lazy deserialization.
1449 std::vector >().swap(GlobalInits);
1450 std::vector >().swap(AliasInits);
1451 std::vector().swap(FunctionsWithBodies);
1452 return false;
1455 return GlobalCleanup();
14531456 }
14541457
14551458 if (Code == bitc::ENTER_SUBBLOCK) {
14731476 case bitc::VALUE_SYMTAB_BLOCK_ID:
14741477 if (ParseValueSymbolTable())
14751478 return true;
1479 SeenValueSymbolTable = true;
14761480 break;
14771481 case bitc::CONSTANTS_BLOCK_ID:
14781482 if (ParseConstants() || ResolveGlobalAndAliasInits())
14851489 case bitc::FUNCTION_BLOCK_ID:
14861490 // If this is the first function body we've seen, reverse the
14871491 // FunctionsWithBodies list.
1488 if (!HasReversedFunctionsWithBodies) {
1492 if (!SeenFirstFunctionBody) {
14891493 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1490 HasReversedFunctionsWithBodies = true;
1494 if (GlobalCleanup())
1495 return true;
1496 SeenFirstFunctionBody = true;
14911497 }
14921498
14931499 if (RememberAndSkipFunctionBody())
14941500 return true;
1501 // For streaming bitcode, suspend parsing when we reach the function
1502 // bodies. Subsequent materialization calls will resume it when
1503 // necessary. For streaming, the function bodies must be at the end of
1504 // the bitcode. If the bitcode file is old, the symbol table will be
1505 // at the end instead and will not have been seen yet. In this case,
1506 // just finish the parse now.
1507 if (LazyStreamer && SeenValueSymbolTable) {
1508 NextUnreadBit = Stream.GetCurrentBitNo();
1509 return false;
1510 }
14951511 break;
14961512 case bitc::USELIST_BLOCK_ID:
14971513 if (ParseUseLists())
16501666
16511667 // If this is a function with a body, remember the prototype we are
16521668 // creating now, so that we can match up the body with them later.
1653 if (!isProto)
1669 if (!isProto) {
16541670 FunctionsWithBodies.push_back(Func);
1671 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1672 }
16551673 break;
16561674 }
16571675 // ALIAS: [alias type, aliasee val#, linkage]
16901708 bool BitcodeReader::ParseBitcodeInto(Module *M) {
16911709 TheModule = 0;
16921710
1693 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
1694 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1695
1696 if (Buffer->getBufferSize() & 3) {
1697 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
1698 return Error("Invalid bitcode signature");
1699 else
1700 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1701 }
1702
1703 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1704 // The magic number is 0x0B17C0DE stored in little endian.
1705 if (isBitcodeWrapper(BufPtr, BufEnd))
1706 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1707 return Error("Invalid bitcode wrapper header");
1708
1709 StreamFile.init(BufPtr, BufEnd);
1710 Stream.init(StreamFile);
1711 if (InitStream()) return true;
17111712
17121713 // Sniff for the signature.
17131714 if (Stream.Read(8) != 'B' ||
17491750 if (TheModule)
17501751 return Error("Multiple MODULE_BLOCKs in same stream");
17511752 TheModule = M;
1752 if (ParseModule())
1753 if (ParseModule(false))
17531754 return true;
1755 if (LazyStreamer) return false;
17541756 break;
17551757 default:
17561758 if (Stream.SkipBlock())
18181820 }
18191821
18201822 bool BitcodeReader::ParseTriple(std::string &Triple) {
1821 if (Buffer->getBufferSize() & 3)
1822 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1823
1824 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
1825 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1826
1827 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1828 // The magic number is 0x0B17C0DE stored in little endian.
1829 if (isBitcodeWrapper(BufPtr, BufEnd))
1830 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1831 return Error("Invalid bitcode wrapper header");
1832
1833 StreamFile.init(BufPtr, BufEnd);
1834 Stream.init(StreamFile);
1823 if (InitStream()) return true;
18351824
18361825 // Sniff for the signature.
18371826 if (Stream.Read(8) != 'B' ||
27072696 return false;
27082697 }
27092698
2699 /// FindFunctionInStream - Find the function body in the bitcode stream
2700 bool BitcodeReader::FindFunctionInStream(Function *F,
2701 DenseMap::iterator DeferredFunctionInfoIterator) {
2702 while (DeferredFunctionInfoIterator->second == 0) {
2703 if (Stream.AtEndOfStream())
2704 return Error("Could not find Function in stream");
2705 // ParseModule will parse the next body in the stream and set its
2706 // position in the DeferredFunctionInfo map.
2707 if (ParseModule(true)) return true;
2708 }
2709 return false;
2710 }
2711
27102712 //===----------------------------------------------------------------------===//
27112713 // GVMaterializer implementation
27122714 //===----------------------------------------------------------------------===//
27272729
27282730 DenseMap::iterator DFII = DeferredFunctionInfo.find(F);
27292731 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
2732 // If its position is recorded as 0, its body is somewhere in the stream
2733 // but we haven't seen it yet.
2734 if (DFII->second == 0)
2735 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
27302736
27312737 // Move the bit stream to the saved position of the deferred function body.
27322738 Stream.JumpToBit(DFII->second);
28042810 return false;
28052811 }
28062812
2813 bool BitcodeReader::InitStream() {
2814 if (LazyStreamer) return InitLazyStream();
2815 return InitStreamFromBuffer();
2816 }
2817
2818 bool BitcodeReader::InitStreamFromBuffer() {
2819 const unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
2820 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
2821
2822 if (Buffer->getBufferSize() & 3) {
2823 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
2824 return Error("Invalid bitcode signature");
2825 else
2826 return Error("Bitcode stream should be a multiple of 4 bytes in length");
2827 }
2828
2829 // If we have a wrapper header, parse it and ignore the non-bc file contents.
2830 // The magic number is 0x0B17C0DE stored in little endian.
2831 if (isBitcodeWrapper(BufPtr, BufEnd))
2832 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
2833 return Error("Invalid bitcode wrapper header");
2834
2835 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
2836 Stream.init(*StreamFile);
2837
2838 return false;
2839 }
2840
2841 bool BitcodeReader::InitLazyStream() {
2842 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
2843 // see it.
2844 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
2845 StreamFile.reset(new BitstreamReader(Bytes));
2846 Stream.init(*StreamFile);
2847
2848 unsigned char buf[16];
2849 if (Bytes->readBytes(0, 16, buf, NULL) == -1)
2850 return Error("Bitcode stream must be at least 16 bytes in length");
2851
2852 if (!isBitcode(buf, buf + 16))
2853 return Error("Invalid bitcode signature");
2854
2855 if (isBitcodeWrapper(buf, buf + 4)) {
2856 const unsigned char *bitcodeStart = buf;
2857 const unsigned char *bitcodeEnd = buf + 16;
2858 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
2859 Bytes->dropLeadingBytes(bitcodeStart - buf);
2860 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
2861 }
2862 return false;
2863 }
28072864
28082865 //===----------------------------------------------------------------------===//
28092866 // External interface
28322889 return M;
28332890 }
28342891
2892
2893 Module *llvm::getStreamedBitcodeModule(const std::string &name,
2894 DataStreamer *streamer,
2895 LLVMContext &Context,
2896 std::string *ErrMsg) {
2897 Module *M = new Module(name, Context);
2898 BitcodeReader *R = new BitcodeReader(streamer, Context);
2899 M->setMaterializer(R);
2900 if (R->ParseBitcodeInto(M)) {
2901 if (ErrMsg)
2902 *ErrMsg = R->getErrorString();
2903 delete M; // Also deletes R.
2904 return 0;
2905 }
2906 R->setBufferOwned(false); // no buffer to delete
2907 return M;
2908 }
2909
28352910 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
28362911 /// If an error occurs, return null and fill in *ErrMsg if non-null.
28372912 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
125125 Module *TheModule;
126126 MemoryBuffer *Buffer;
127127 bool BufferOwned;
128 BitstreamReader StreamFile;
128 OwningPtr StreamFile;
129129 BitstreamCursor Stream;
130 DataStreamer *LazyStreamer;
131 uint64_t NextUnreadBit;
132 bool SeenValueSymbolTable;
130133
131134 const char *ErrorString;
132135
160163 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
161164 DenseMap MDKindMap;
162165
163 // After the module header has been read, the FunctionsWithBodies list is
164 // reversed. This keeps track of whether we've done this yet.
165 bool HasReversedFunctionsWithBodies;
166 // Several operations happen after the module header has been read, but
167 // before function bodies are processed. This keeps track of whether
168 // we've done this yet.
169 bool SeenFirstFunctionBody;
166170
167171 /// DeferredFunctionInfo - When function bodies are initially scanned, this
168172 /// map contains info about where to find deferred function body in the
177181 public:
178182 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
179183 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
180 ErrorString(0), ValueList(C), MDValueList(C) {
181 HasReversedFunctionsWithBodies = false;
184 LazyStreamer(0), SeenValueSymbolTable(false), ErrorString(0),
185 ValueList(C), MDValueList(C), SeenFirstFunctionBody(false) {
186 }
187 explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
188 : Context(C), TheModule(0), Buffer(0), BufferOwned(false),
189 LazyStreamer(streamer), SeenValueSymbolTable(false), ErrorString(0),
190 ValueList(C), MDValueList(C), SeenFirstFunctionBody(false) {
182191 }
183192 ~BitcodeReader() {
184193 FreeState();
257266 }
258267
259268
260 bool ParseModule();
269 bool ParseModule(bool Resume);
261270 bool ParseAttributeBlock();
262271 bool ParseTypeTable();
263272 bool ParseTypeTableBody();
266275 bool ParseConstants();
267276 bool RememberAndSkipFunctionBody();
268277 bool ParseFunctionBody(Function *F);
278 bool GlobalCleanup();
269279 bool ResolveGlobalAndAliasInits();
270280 bool ParseMetadata();
271281 bool ParseMetadataAttachment();
272282 bool ParseModuleTriple(std::string &Triple);
273283 bool ParseUseLists();
284 bool InitStream();
285 bool InitStreamFromBuffer();
286 bool InitLazyStream();
287 bool FindFunctionInStream(Function *F,
288 DenseMap::iterator DeferredFunctionInfoIterator);
274289 };
275290
276291 } // End llvm namespace
17371737 // Emit metadata.
17381738 WriteModuleMetadata(M, VE, Stream);
17391739
1740 // Emit metadata.
1741 WriteModuleMetadataStore(M, Stream);
1742
1743 // Emit names for globals/functions etc.
1744 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1745
1746 // Emit use-lists.
1747 if (EnablePreserveUseListOrdering)
1748 WriteModuleUseLists(M, VE, Stream);
1749
17401750 // Emit function bodies.
17411751 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
17421752 if (!F->isDeclaration())
17431753 WriteFunction(*F, VE, Stream);
1744
1745 // Emit metadata.
1746 WriteModuleMetadataStore(M, Stream);
1747
1748 // Emit names for globals/functions etc.
1749 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1750
1751 // Emit use-lists.
1752 if (EnablePreserveUseListOrdering)
1753 WriteModuleUseLists(M, VE, Stream);
17541754
17551755 Stream.ExitBlock();
17561756 }
9999 Bytes(bytes), Size(size), BasePC(basePC) {}
100100
101101 uint64_t getBase() const { return BasePC; }
102 uint64_t getExtent() const { return Size; }
102 uint64_t getExtent() { return Size; }
103103
104 int readByte(uint64_t Addr, uint8_t *Byte) const {
104 int readByte(uint64_t Addr, uint8_t *Byte) {
105105 if (Addr - BasePC >= Size)
106106 return -1;
107107 *Byte = Bytes[Addr - BasePC];
206206 void *arg) : Callback(callback), Arg(arg) { }
207207 ~EDMemoryObject() { }
208208 uint64_t getBase() const { return 0x0; }
209 uint64_t getExtent() const { return (uint64_t)-1; }
210 int readByte(uint64_t address, uint8_t *ptr) const {
209 uint64_t getExtent() { return (uint64_t)-1; }
210 int readByte(uint64_t address, uint8_t *ptr) {
211211 if (!Callback)
212212 return -1;
213213
1515 ConstantRange.cpp
1616 CrashRecoveryContext.cpp
1717 DataExtractor.cpp
18 DataStream.cpp
1819 Debug.cpp
1920 DeltaAlgorithm.cpp
2021 DAGDeltaAlgorithm.cpp
4142 SmallVector.cpp
4243 SourceMgr.cpp
4344 Statistic.cpp
45 StreamableMemoryObject.cpp
4446 StringExtras.cpp
4547 StringMap.cpp
4648 StringPool.cpp
0 //===--- llvm/Support/DataStream.cpp - Lazy streamed Data ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements DataStreamer, which fetches bytes of Data from
10 // a stream source. It provides support for streaming (lazy reading) of
11 // bitcode. An example implementation of streaming from a file or stdin
12 // is included.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "Data-stream"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Support/DataStream.h"
19 #include "llvm/Support/system_error.h"
20 #include
21 #include
22 #include
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 #include
25 #else
26 #include
27 #endif
28 #include
29 using namespace llvm;
30
31 // Interface goals:
32 // * StreamableMemoryObject doesn't care about complexities like using
33 // threads/async callbacks to actually overlap download+compile
34 // * Don't want to duplicate Data in memory
35 // * Don't need to know total Data len in advance
36 // Non-goals:
37 // StreamableMemoryObject already has random access so this interface only does
38 // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
39 // Data here in addition to MemoryObject). This also means that if we want
40 // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
41
42 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
43
44 namespace llvm {
45 DataStreamer::~DataStreamer() {}
46 }
47
48 namespace {
49
50 const static error_code success;
51
52 // Very simple stream backed by a file. Mostly useful for stdin and debugging;
53 // actual file access is probably still best done with mmap.
54 class DataFileStreamer : public DataStreamer {
55 int Fd;
56 public:
57 DataFileStreamer() : Fd(0) {}
58 virtual ~DataFileStreamer() {
59 close(Fd);
60 }
61 virtual size_t GetBytes(unsigned char *buf, size_t len) {
62 NumStreamFetches++;
63 return read(Fd, buf, len);
64 }
65
66 error_code OpenFile(const std::string &Filename) {
67 int OpenFlags = O_RDONLY;
68 #ifdef O_BINARY
69 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
70 #endif
71 if (Filename == "-")
72 Fd = 0;
73 else
74 Fd = ::open(Filename.c_str(), OpenFlags);
75 if (Fd == -1) return error_code(errno, posix_category());
76 return success;
77 }
78 };
79
80 }
81
82 namespace llvm {
83 DataStreamer *getDataFileStreamer(const std::string &Filename,
84 std::string *StrError) {
85 DataFileStreamer *s = new DataFileStreamer();
86 error_code e = s->OpenFile(Filename);
87 if (e != success) {
88 *StrError = std::string("Could not open ") + Filename + ": " +
89 e.message() + "\n";
90 return NULL;
91 }
92 return s;
93 }
94
95 }
1515 int MemoryObject::readBytes(uint64_t address,
1616 uint64_t size,
1717 uint8_t* buf,
18 uint64_t* copied) const {
18 uint64_t* copied) {
1919 uint64_t current = address;
2020 uint64_t limit = getBase() + getExtent();
2121
0 //===- StreamableMemoryObject.cpp - Streamable data interface - -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/StreamableMemoryObject.h"
10 #include
11 #include
12
13
14 using namespace llvm;
15
16 namespace {
17
18 class RawMemoryObject : public StreamableMemoryObject {
19 public:
20 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
21 FirstChar(Start), LastChar(End) {
22 assert(LastChar > FirstChar && "Invalid start/end range");
23 }
24
25 virtual uint64_t getBase() const { return 0; }
26 virtual uint64_t getExtent() { return LastChar - FirstChar; }
27 virtual int readByte(uint64_t address, uint8_t* ptr);
28 virtual int readBytes(uint64_t address,
29 uint64_t size,
30 uint8_t* buf,
31 uint64_t* copied);
32 virtual const uint8_t *getPointer(uint64_t address, uint64_t size);
33 virtual bool isValidAddress(uint64_t address) {return validAddress(address);}
34 virtual bool isObjectEnd(uint64_t address) {return objectEnd(address);}
35
36 private:
37 const uint8_t* const FirstChar;
38 const uint8_t* const LastChar;
39
40 // These are implemented as inline functions here to avoid multiple virtual
41 // calls per public function
42 bool validAddress(uint64_t address) {
43 return static_cast(address) < LastChar - FirstChar;
44 }
45 bool objectEnd(uint64_t address) {
46 return static_cast(address) == LastChar - FirstChar;
47 }
48
49 RawMemoryObject(const RawMemoryObject&); // DO NOT IMPLEMENT
50 void operator=(const RawMemoryObject&); // DO NOT IMPLEMENT
51 };
52
53 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
54 if (!validAddress(address)) return -1;
55 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
56 return 0;
57 }
58
59 int RawMemoryObject::readBytes(uint64_t address,
60 uint64_t size,
61 uint8_t* buf,
62 uint64_t* copied) {
63 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
64 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
65 if (copied) *copied = size;
66 return size;
67 }
68
69 const uint8_t *RawMemoryObject::getPointer(uint64_t address, uint64_t size) {
70 return FirstChar + address;
71 }
72 } // anonymous namespace
73
74 namespace llvm {
75 // If the bitcode has a header, then its size is known, and we don't have to
76 // block until we actually want to read it.
77 bool StreamingMemoryObject::isValidAddress(uint64_t address) {
78 if (ObjectSize && address < ObjectSize) return true;
79 return fetchToPos(address);
80 }
81
82 bool StreamingMemoryObject::isObjectEnd(uint64_t address) {
83 if (ObjectSize) return address == ObjectSize;
84 fetchToPos(address);
85 return address == ObjectSize && address != 0;
86 }
87
88 uint64_t StreamingMemoryObject::getExtent() {
89 if (ObjectSize) return ObjectSize;
90 size_t pos = BytesRead + kChunkSize;
91 // keep fetching until we run out of bytes
92 while (fetchToPos(pos)) pos += kChunkSize;
93 return ObjectSize;
94 }
95
96 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
97 if (!fetchToPos(address)) return -1;
98 *ptr = Bytes[address + BytesSkipped];
99 return 0;
100 }
101
102 int StreamingMemoryObject::readBytes(uint64_t address,
103 uint64_t size,
104 uint8_t* buf,
105 uint64_t* copied) {
106 if (!fetchToPos(address + size - 1)) return -1;
107 memcpy(buf, &Bytes[address + BytesSkipped], size);
108 if (copied) *copied = size;
109 return 0;
110 }
111
112 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
113 if (BytesRead < s) return true;
114 BytesSkipped = s;
115 BytesRead -= s;
116 return false;
117 }
118
119 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
120 ObjectSize = size;
121 Bytes.reserve(size);
122 }
123
124 StreamableMemoryObject *getNonStreamedMemoryObject(
125 const unsigned char *Start, const unsigned char *End) {
126 return new RawMemoryObject(Start, End);
127 }
128
129 StreamableMemoryObject::~StreamableMemoryObject() { }
130
131 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
132 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
133 ObjectSize(0), EOFReached(false) {
134 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
135 }
136 }
4545 /// getInstruction - See MCDisassembler.
4646 DecodeStatus getInstruction(MCInst &instr,
4747 uint64_t &size,
48 const MemoryObject ®ion,
48 MemoryObject ®ion,
4949 uint64_t address,
5050 raw_ostream &vStream,
5151 raw_ostream &cStream) const;
7070 /// getInstruction - See MCDisassembler.
7171 DecodeStatus getInstruction(MCInst &instr,
7272 uint64_t &size,
73 const MemoryObject ®ion,
73 MemoryObject ®ion,
7474 uint64_t address,
7575 raw_ostream &vStream,
7676 raw_ostream &cStream) const;
340340 }
341341
342342 DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
343 const MemoryObject &Region,
343 MemoryObject &Region,
344344 uint64_t Address,
345345 raw_ostream &os,
346346 raw_ostream &cs) const {
690690 }
691691
692692 DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
693 const MemoryObject &Region,
693 MemoryObject &Region,
694694 uint64_t Address,
695695 raw_ostream &os,
696696 raw_ostream &cs) const {
501501
502502 MCDisassembler::DecodeStatus MBlazeDisassembler::getInstruction(MCInst &instr,
503503 uint64_t &size,
504 const MemoryObject ®ion,
504 MemoryObject ®ion,
505505 uint64_t address,
506506 raw_ostream &vStream,
507507 raw_ostream &cStream) const {
3939 /// getInstruction - See MCDisassembler.
4040 MCDisassembler::DecodeStatus getInstruction(MCInst &instr,
4141 uint64_t &size,
42 const MemoryObject ®ion,
42 MemoryObject ®ion,
4343 uint64_t address,
4444 raw_ostream &vStream,
4545 raw_ostream &cStream) const;
111111 MCDisassembler::DecodeStatus
112112 X86GenericDisassembler::getInstruction(MCInst &instr,
113113 uint64_t &size,
114 const MemoryObject ®ion,
114 MemoryObject ®ion,
115115 uint64_t address,
116116 raw_ostream &vStream,
117117 raw_ostream &cStream) const {
113113 /// getInstruction - See MCDisassembler.
114114 DecodeStatus getInstruction(MCInst &instr,
115115 uint64_t &size,
116 const MemoryObject ®ion,
116 MemoryObject ®ion,
117117 uint64_t address,
118118 raw_ostream &vStream,
119119 raw_ostream &cStream) const;
482482 if (MemBuf->getBufferSize() & 3)
483483 return Error("Bitcode stream should be a multiple of 4 bytes in length");
484484
485 unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart();
486 unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
485 const unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart();
486 const unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
487487
488488 // If we have a wrapper header, parse it and ignore the non-bc file contents.
489489 // The magic number is 0x0B17C0DE stored in little endian.
490490 if (isBitcodeWrapper(BufPtr, EndBufPtr))
491 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr))
491 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
492492 return Error("Invalid bitcode wrapper header");
493493
494494 BitstreamReader StreamFile(BufPtr, EndBufPtr);
2323 #include "llvm/Analysis/DebugInfo.h"
2424 #include "llvm/Assembly/AssemblyAnnotationWriter.h"
2525 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/DataStream.h"
2627 #include "llvm/Support/FormattedStream.h"
2728 #include "llvm/Support/ManagedStatic.h"
2829 #include "llvm/Support/MemoryBuffer.h"
125126 std::string ErrorMessage;
126127 std::auto_ptr M;
127128
128 {
129 OwningPtr BufferPtr;
130 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
131 ErrorMessage = ec.message();
129 // Use the bitcode streaming interface
130 DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
131 if (streamer) {
132 std::string DisplayFilename;
133 if (InputFilename == "-")
134 DisplayFilename = "";
132135 else
133 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
136 DisplayFilename = InputFilename;
137 M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context,
138 &ErrorMessage));
139 if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) {
140 M.reset();
141 }
134142 }
135143
136144 if (M.get() == 0) {
182190
183191 return 0;
184192 }
185
4141 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
4242
4343 uint64_t getBase() const { return 0; }
44 uint64_t getExtent() const { return Bytes.size(); }
45
46 int readByte(uint64_t Addr, uint8_t *Byte) const {
44 uint64_t getExtent() { return Bytes.size(); }
45
46 int readByte(uint64_t Addr, uint8_t *Byte) {
4747 if (Addr >= getExtent())
4848 return -1;
4949 *Byte = Bytes[Addr].first;
2727
2828 MCFunction
2929 MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
30 const MemoryObject &Region, uint64_t Start,
30 MemoryObject &Region, uint64_t Start,
3131 uint64_t End, const MCInstrAnalysis *Ana,
3232 raw_ostream &DebugOut,
3333 SmallVectorImpl &Calls) {
7878 // Create an MCFunction from a region of binary machine code.
7979 static MCFunction
8080 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
81 const MemoryObject &Region, uint64_t Start, uint64_t End,
81 MemoryObject &Region, uint64_t Start, uint64_t End,
8282 const MCInstrAnalysis *Ana, raw_ostream &DebugOut,
8383 SmallVectorImpl &Calls);
8484
3030 StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {}
3131
3232 uint64_t getBase() const { return 0; }
33 uint64_t getExtent() const { return Bytes.size(); }
33 uint64_t getExtent() { return Bytes.size(); }
3434
35 int readByte(uint64_t Addr, uint8_t *Byte) const {
35 int readByte(uint64_t Addr, uint8_t *Byte) {
3636 if (Addr >= getExtent())
3737 return -1;
3838 *Byte = Bytes[Addr];