llvm.org GIT mirror llvm / adef06a
Make MemoryObject accessor members const again git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151687 91177308-0d34-0410-b5e6-96231b3b80d8 Derek Schuff 8 years ago
16 changed file(s) with 65 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
7878 /// MCDisassembler::Fail if the instruction was invalid.
7979 virtual DecodeStatus getInstruction(MCInst& instr,
8080 uint64_t& size,
81 MemoryObject ®ion,
81 const MemoryObject ®ion,
8282 uint64_t address,
8383 raw_ostream &vStream,
8484 raw_ostream &cStream) const = 0;
2222 public:
2323 /// Destructor - Override as necessary.
2424 virtual ~MemoryObject();
25
25
2626 /// getBase - Returns the lowest valid address in the region.
2727 ///
2828 /// @result - The lowest valid address.
2929 virtual uint64_t getBase() const = 0;
30
30
3131 /// getExtent - Returns the size of the region in bytes. (The region is
32 /// contiguous, so the highest valid address of the region
32 /// contiguous, so the highest valid address of the region
3333 /// is getBase() + getExtent() - 1).
3434 ///
3535 /// @result - The size of the region.
36 virtual uint64_t getExtent() = 0;
37
36 virtual uint64_t getExtent() const = 0;
37
3838 /// readByte - Tries to read a single byte from the region.
3939 ///
4040 /// @param address - The address of the byte, in the same space as getBase().
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) = 0;
45
44 virtual int readByte(uint64_t address, uint8_t* ptr) const = 0;
45
4646 /// readBytes - Tries to read a contiguous range of bytes from the
4747 /// region, up to the end of the region.
4848 /// You should override this function if there is a quicker
6060 virtual int readBytes(uint64_t address,
6161 uint64_t size,
6262 uint8_t* buf,
63 uint64_t* copied);
63 uint64_t* copied) const;
6464 };
6565
6666 }
6767
6868 #endif
69
4444 /// May block until all bytes in the stream have been read
4545 ///
4646 /// @result - The size of the region.
47 virtual uint64_t getExtent() = 0;
47 virtual uint64_t getExtent() const = 0;
4848
4949 /// readByte - Tries to read a single byte from the region.
5050 /// May block until (address - base) bytes have been read
5252 /// @param ptr - A pointer to a byte to be filled in. Must be non-NULL.
5353 /// @result - 0 if successful; -1 if not. Failure may be due to a
5454 /// bounds violation or an implementation-specific error.
55 virtual int readByte(uint64_t address, uint8_t* ptr) = 0;
55 virtual int readByte(uint64_t address, uint8_t* ptr) const = 0;
5656
5757 /// readBytes - Tries to read a contiguous range of bytes from the
5858 /// region, up to the end of the region.
7373 virtual int readBytes(uint64_t address,
7474 uint64_t size,
7575 uint8_t* buf,
76 uint64_t* copied) = 0;
76 uint64_t* copied) const = 0;
7777
7878 /// getPointer - Ensures that the requested data is in memory, and returns
7979 /// A pointer to it. More efficient than using readBytes if the
8282 /// @param address - address of the byte, in the same space as getBase()
8383 /// @param size - amount of data that must be available on return
8484 /// @result - valid pointer to the requested data
85 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) = 0;
85 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
8686
8787 /// isValidAddress - Returns true if the address is within the object
8888 /// (i.e. between base and base + extent - 1 inclusive)
8989 /// May block until (address - base) bytes have been read
9090 /// @param address - address of the byte, in the same space as getBase()
9191 /// @result - true if the address may be read with readByte()
92 virtual bool isValidAddress(uint64_t address) = 0;
92 virtual bool isValidAddress(uint64_t address) const = 0;
9393
9494 /// isObjectEnd - Returns true if the address is one past the end of the
9595 /// object (i.e. if it is equal to base + extent)
9696 /// May block until (address - base) bytes have been read
9797 /// @param address - address of the byte, in the same space as getBase()
9898 /// @result - true if the address is equal to base + extent
99 virtual bool isObjectEnd(uint64_t address) = 0;
99 virtual bool isObjectEnd(uint64_t address) const = 0;
100100 };
101101
102102 /// StreamingMemoryObject - interface to data which is actually streamed from
107107 public:
108108 StreamingMemoryObject(DataStreamer *streamer);
109109 virtual uint64_t getBase() const { return 0; }
110 virtual uint64_t getExtent();
111 virtual int readByte(uint64_t address, uint8_t* ptr);
110 virtual uint64_t getExtent() const;
111 virtual int readByte(uint64_t address, uint8_t* ptr) const;
112112 virtual int readBytes(uint64_t address,
113113 uint64_t size,
114114 uint8_t* buf,
115 uint64_t* copied);
116 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) {
115 uint64_t* copied) const ;
116 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const {
117117 // This could be fixed by ensuring the bytes are fetched and making a copy,
118118 // requiring that the bitcode size be known, or otherwise ensuring that
119119 // the memory doesn't go away/get reallocated, but it's
121121 assert(0 && "getPointer in streaming memory objects not allowed");
122122 return NULL;
123123 }
124 virtual bool isValidAddress(uint64_t address);
125 virtual bool isObjectEnd(uint64_t address);
124 virtual bool isValidAddress(uint64_t address) const;
125 virtual bool isObjectEnd(uint64_t address) const;
126126
127127 /// Drop s bytes from the front of the stream, pushing the positions of the
128128 /// remaining bytes down by s. This is used to skip past the bitcode header,
137137
138138 private:
139139 const static uint32_t kChunkSize = 4096 * 4;
140 std::vector Bytes;
140 mutable std::vector Bytes;
141141 OwningPtr Streamer;
142 size_t BytesRead; // Bytes read from stream
142 mutable size_t BytesRead; // Bytes read from stream
143143 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;
144 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
145 mutable bool EOFReached;
146146
147147 // Fetch enough bytes such that Pos can be read or EOF is reached
148148 // (i.e. BytesRead > Pos). Return true if Pos can be read.
149149 // Unlike most of the functions in BitcodeReader, returns true on success.
150150 // Most of the requests will be small, but we fetch at kChunkSize bytes
151151 // at a time to avoid making too many potentially expensive GetBytes calls
152 bool fetchToPos(size_t Pos) {
152 bool fetchToPos(size_t Pos) const {
153153 if (EOFReached) return Pos < ObjectSize;
154154 while (Pos >= BytesRead) {
155155 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
112112 Bytes(bytes), Size(size), BasePC(basePC) {}
113113
114114 uint64_t getBase() const { return BasePC; }
115 uint64_t getExtent() { return Size; }
115 uint64_t getExtent() const { return Size; }
116116
117 int readByte(uint64_t Addr, uint8_t *Byte) {
117 int readByte(uint64_t Addr, uint8_t *Byte) const {
118118 if (Addr - BasePC >= Size)
119119 return -1;
120120 *Byte = Bytes[Addr - BasePC];
198198 void *arg) : Callback(callback), Arg(arg) { }
199199 ~EDMemoryObject() { }
200200 uint64_t getBase() const { return 0x0; }
201 uint64_t getExtent() { return (uint64_t)-1; }
202 int readByte(uint64_t address, uint8_t *ptr) {
201 uint64_t getExtent() const { return (uint64_t)-1; }
202 int readByte(uint64_t address, uint8_t *ptr) const {
203203 if (!Callback)
204204 return -1;
205205
1515 int MemoryObject::readBytes(uint64_t address,
1616 uint64_t size,
1717 uint8_t* buf,
18 uint64_t* copied) {
18 uint64_t* copied) const {
1919 uint64_t current = address;
2020 uint64_t limit = getBase() + getExtent();
2121
2323 }
2424
2525 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);
26 virtual uint64_t getExtent() const { return LastChar - FirstChar; }
27 virtual int readByte(uint64_t address, uint8_t* ptr) const;
2828 virtual int readBytes(uint64_t address,
2929 uint64_t size,
3030 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);}
31 uint64_t* copied) const;
32 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const;
33 virtual bool isValidAddress(uint64_t address) const {
34 return validAddress(address);
35 }
36 virtual bool isObjectEnd(uint64_t address) const {return objectEnd(address);}
3537
3638 private:
3739 const uint8_t* const FirstChar;
3941
4042 // These are implemented as inline functions here to avoid multiple virtual
4143 // calls per public function
42 bool validAddress(uint64_t address) {
44 bool validAddress(uint64_t address) const {
4345 return static_cast(address) < LastChar - FirstChar;
4446 }
45 bool objectEnd(uint64_t address) {
47 bool objectEnd(uint64_t address) const {
4648 return static_cast(address) == LastChar - FirstChar;
4749 }
4850
5052 void operator=(const RawMemoryObject&); // DO NOT IMPLEMENT
5153 };
5254
53 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
55 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
5456 if (!validAddress(address)) return -1;
5557 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
5658 return 0;
5961 int RawMemoryObject::readBytes(uint64_t address,
6062 uint64_t size,
6163 uint8_t* buf,
62 uint64_t* copied) {
64 uint64_t* copied) const {
6365 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
6466 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
6567 if (copied) *copied = size;
6668 return size;
6769 }
6870
69 const uint8_t *RawMemoryObject::getPointer(uint64_t address, uint64_t size) {
71 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
72 uint64_t size) const {
7073 return FirstChar + address;
7174 }
7275 } // anonymous namespace
7477 namespace llvm {
7578 // If the bitcode has a header, then its size is known, and we don't have to
7679 // block until we actually want to read it.
77 bool StreamingMemoryObject::isValidAddress(uint64_t address) {
80 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
7881 if (ObjectSize && address < ObjectSize) return true;
7982 return fetchToPos(address);
8083 }
8184
82 bool StreamingMemoryObject::isObjectEnd(uint64_t address) {
85 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
8386 if (ObjectSize) return address == ObjectSize;
8487 fetchToPos(address);
8588 return address == ObjectSize && address != 0;
8689 }
8790
88 uint64_t StreamingMemoryObject::getExtent() {
91 uint64_t StreamingMemoryObject::getExtent() const {
8992 if (ObjectSize) return ObjectSize;
9093 size_t pos = BytesRead + kChunkSize;
9194 // keep fetching until we run out of bytes
9396 return ObjectSize;
9497 }
9598
96 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
99 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
97100 if (!fetchToPos(address)) return -1;
98101 *ptr = Bytes[address + BytesSkipped];
99102 return 0;
102105 int StreamingMemoryObject::readBytes(uint64_t address,
103106 uint64_t size,
104107 uint8_t* buf,
105 uint64_t* copied) {
108 uint64_t* copied) const {
106109 if (!fetchToPos(address + size - 1)) return -1;
107110 memcpy(buf, &Bytes[address + BytesSkipped], size);
108111 if (copied) *copied = size;
4545 /// getInstruction - See MCDisassembler.
4646 DecodeStatus getInstruction(MCInst &instr,
4747 uint64_t &size,
48 MemoryObject ®ion,
48 const 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 MemoryObject ®ion,
73 const 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 MemoryObject &Region,
343 const MemoryObject &Region,
344344 uint64_t Address,
345345 raw_ostream &os,
346346 raw_ostream &cs) const {
688688 }
689689
690690 DecodeStatus ThumbDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
691 MemoryObject &Region,
691 const MemoryObject &Region,
692692 uint64_t Address,
693693 raw_ostream &os,
694694 raw_ostream &cs) const {
501501
502502 MCDisassembler::DecodeStatus MBlazeDisassembler::getInstruction(MCInst &instr,
503503 uint64_t &size,
504 MemoryObject ®ion,
504 const 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 MemoryObject ®ion,
42 const MemoryObject ®ion,
4343 uint64_t address,
4444 raw_ostream &vStream,
4545 raw_ostream &cStream) const;
120120 MCDisassembler::DecodeStatus
121121 X86GenericDisassembler::getInstruction(MCInst &instr,
122122 uint64_t &size,
123 MemoryObject ®ion,
123 const MemoryObject ®ion,
124124 uint64_t address,
125125 raw_ostream &vStream,
126126 raw_ostream &cStream) const {
116116 /// getInstruction - See MCDisassembler.
117117 DecodeStatus getInstruction(MCInst &instr,
118118 uint64_t &size,
119 MemoryObject ®ion,
119 const MemoryObject ®ion,
120120 uint64_t address,
121121 raw_ostream &vStream,
122122 raw_ostream &cStream) const;
4141 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
4242
4343 uint64_t getBase() const { return 0; }
44 uint64_t getExtent() { return Bytes.size(); }
45
46 int readByte(uint64_t Addr, uint8_t *Byte) {
44 uint64_t getExtent() const { return Bytes.size(); }
45
46 int readByte(uint64_t Addr, uint8_t *Byte) const {
4747 if (Addr >= getExtent())
4848 return -1;
4949 *Byte = Bytes[Addr].first;
364364
365365 return 0;
366366 }
367
2727
2828 MCFunction
2929 MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
30 MemoryObject &Region, uint64_t Start,
30 const 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 MemoryObject &Region, uint64_t Start, uint64_t End,
81 const 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() { return Bytes.size(); }
33 uint64_t getExtent() const { return Bytes.size(); }
3434
35 int readByte(uint64_t Addr, uint8_t *Byte) {
35 int readByte(uint64_t Addr, uint8_t *Byte) const {
3636 if (Addr >= getExtent())
3737 return -1;
3838 *Byte = Bytes[Addr];