Ensure -mcpu=xscale works for arm targets, after rL252903 and rL252904
[oota-llvm.git] / include / llvm / Support / StreamingMemoryObject.h
1 //===- StreamingMemoryObject.h - Streamable data interface -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
11 #define LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
12
13 #include "llvm/Support/Compiler.h"
14 #include "llvm/Support/DataStream.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/MemoryObject.h"
17 #include <memory>
18 #include <vector>
19
20 namespace llvm {
21
22 /// Interface to data which is actually streamed from a DataStreamer. In
23 /// addition to inherited members, it has the dropLeadingBytes and
24 /// setKnownObjectSize methods which are not applicable to non-streamed objects.
25 class StreamingMemoryObject : public MemoryObject {
26 public:
27   StreamingMemoryObject(std::unique_ptr<DataStreamer> Streamer);
28   uint64_t getExtent() const override;
29   uint64_t readBytes(uint8_t *Buf, uint64_t Size,
30                      uint64_t Address) const override;
31   const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
32     // FIXME: This could be fixed by ensuring the bytes are fetched and
33     // making a copy, requiring that the bitcode size be known, or
34     // otherwise ensuring that the memory doesn't go away/get reallocated,
35     // but it's not currently necessary. Users that need the pointer (any
36     // that need Blobs) don't stream.
37     report_fatal_error("getPointer in streaming memory objects not allowed");
38     return nullptr;
39   }
40   bool isValidAddress(uint64_t address) const override;
41
42   /// Drop s bytes from the front of the stream, pushing the positions of the
43   /// remaining bytes down by s. This is used to skip past the bitcode header,
44   /// since we don't know a priori if it's present, and we can't put bytes
45   /// back into the stream once we've read them.
46   bool dropLeadingBytes(size_t s);
47
48   /// If the data object size is known in advance, many of the operations can
49   /// be made more efficient, so this method should be called before reading
50   /// starts (although it can be called anytime).
51   void setKnownObjectSize(size_t size);
52
53   /// The number of bytes read at a time from the data streamer.
54   static const uint32_t kChunkSize = 4096 * 4;
55
56 private:
57   mutable std::vector<unsigned char> Bytes;
58   std::unique_ptr<DataStreamer> Streamer;
59   mutable size_t BytesRead;   // Bytes read from stream
60   size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
61   mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
62   mutable bool EOFReached;
63
64   // Fetch enough bytes such that Pos can be read (i.e. BytesRead >
65   // Pos). Returns true if Pos can be read.  Unlike most of the
66   // functions in BitcodeReader, returns true on success.  Most of the
67   // requests will be small, but we fetch at kChunkSize bytes at a
68   // time to avoid making too many potentially expensive GetBytes
69   // calls.
70   bool fetchToPos(size_t Pos) const {
71     while (Pos >= BytesRead) {
72       if (EOFReached)
73         return false;
74       Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
75       size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
76                                         kChunkSize);
77       BytesRead += bytes;
78       if (bytes == 0) { // reached EOF/ran out of bytes
79         if (ObjectSize == 0)
80           ObjectSize = BytesRead;
81         EOFReached = true;
82       }
83     }
84     return !ObjectSize || Pos < ObjectSize;
85   }
86
87   StreamingMemoryObject(const StreamingMemoryObject&) = delete;
88   void operator=(const StreamingMemoryObject&) = delete;
89 };
90
91 MemoryObject *getNonStreamedMemoryObject(
92     const unsigned char *Start, const unsigned char *End);
93
94 }
95 #endif  // STREAMINGMEMORYOBJECT_H_