libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
memoryAccess.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#include <triton/cpuSize.hpp>
11
12
13
14namespace triton {
15 namespace arch {
16
18 this->address = 0;
19 this->leaAst = nullptr;
20 this->pcRelative = 0;
21 }
22
23
25 : MemoryAccess() {
26 this->address = address;
27
28 if (size == 0)
29 throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size cannot be zero.");
30
31 if (size != triton::size::byte &&
32 size != triton::size::word &&
33 size != triton::size::dword &&
34 size != triton::size::qword &&
35 size != triton::size::fword &&
36 size != triton::size::dqword &&
37 size != triton::size::qqword &&
39 throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size must be aligned.");
40
41 this->setBits(((size * triton::bitsize::byte) - 1), 0);
42 }
43
44
46 : BitsVector(other) {
47 this->copy(other);
48 }
49
50
52 return this->address;
53 }
54
55
59
60
62 return this->pcRelative;
63 }
64
65
67 return this->getVectorSize();
68 }
69
70
74
75
79
80
84
85
89
90
94
95
99
100
102 return this->scale;
103 }
104
105
107 return this->segmentReg;
108 }
109
110
112 return this->baseReg;
113 }
114
115
117 return this->indexReg;
118 }
119
120
122 return this->displacement;
123 }
124
125
127 return this->scale;
128 }
129
130
131 bool MemoryAccess::isOverlapWith(const MemoryAccess& other) const {
132 if (this->getAddress() <= other.getAddress() && other.getAddress() < (this->getAddress() + this->getSize())) return true;
133 if (other.getAddress() <= this->getAddress() && this->getAddress() < (other.getAddress() + other.getSize())) return true;
134 return false;
135 }
136
137
139 this->address = addr;
140 }
141
142
144 this->pcRelative = addr;
145 }
146
147
149 this->segmentReg = segment;
150 }
151
152
154 this->baseReg = base;
155 }
156
157
159 this->indexReg = index;
160 }
161
162
164 this->displacement = displacement;
165 }
166
167
169 this->scale = scale;
170 }
171
172
174 this->leaAst = ast;
175 }
176
177
180 this->copy(other);
181 return *this;
182 }
183
184
185 void MemoryAccess::copy(const MemoryAccess& other) {
186 this->address = other.address;
187 this->baseReg = other.baseReg;
188 this->displacement = other.displacement;
189 this->indexReg = other.indexReg;
190 this->leaAst = other.leaAst;
191 this->pcRelative = other.pcRelative;
192 this->scale = other.scale;
193 this->segmentReg = other.segmentReg;
194 }
195
196
197 std::ostream& operator<<(std::ostream& stream, const MemoryAccess& mem) {
198 stream << "[@0x"
199 << std::hex << mem.getAddress()
200 << "]:"
201 << std::dec << mem.getBitSize()
202 << " bv["
203 << mem.getHigh()
204 << ".."
205 << mem.getLow()
206 << "]";
207 return stream;
208 }
209
210
211 std::ostream& operator<<(std::ostream& stream, const MemoryAccess* mem) {
212 stream << *mem;
213 return stream;
214 }
215
216
217 bool operator==(const MemoryAccess& mem1, const MemoryAccess& mem2) {
218 if (mem1.getAddress() != mem2.getAddress())
219 return false;
220 if (mem1.getSize() != mem2.getSize())
221 return false;
222 if (mem1.getConstBaseRegister() != mem2.getConstBaseRegister())
223 return false;
224 if (mem1.getConstIndexRegister() != mem2.getConstIndexRegister())
225 return false;
226 if (mem1.getConstScale() != mem2.getConstScale())
227 return false;
228 if (mem1.getConstDisplacement() != mem2.getConstDisplacement())
229 return false;
231 return false;
232 if (mem1.getPcRelative() != mem2.getPcRelative())
233 return false;
234 return true;
235 }
236
237
238 bool operator!=(const MemoryAccess& mem1, const MemoryAccess& mem2) {
239 return !(mem1 == mem2);
240 }
241
242
243 bool operator<(const MemoryAccess& mem1, const MemoryAccess& mem2) {
244 triton::uint64 seed1 = 0;
245 triton::uint64 seed2 = 0;
246
247 /*
248 * Golden ratio 32-bits -> 0x9e3779b9
249 * Golden ratio 64-bits -> 0x9e3779b97f4a7c13
250 */
251 seed1 ^= mem1.getAddress() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
252 seed1 ^= mem1.getSize() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
253
254 seed2 ^= mem2.getAddress() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
255 seed2 ^= mem2.getSize() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
256
257 return (seed1 < seed2);
258 }
259
260 }; /* arch namespace */
261}; /* triton namespace */
This class is used to deal with registers and memory as bits vector.
TRITON_EXPORT triton::uint32 getHigh(void) const
Returns the highest bit.
TRITON_EXPORT triton::uint32 getVectorSize(void) const
Returns the size in bits of the vector.
TRITON_EXPORT triton::uint32 getLow(void) const
Returns the lower bit.
TRITON_EXPORT void setBits(triton::uint32 high, triton::uint32 low)
Sets the bits (high, low) position.
TRITON_EXPORT BitsVector & operator=(const BitsVector &other)
Copy a BitsVector.
This class is used to represent an immediate.
Definition immediate.hpp:37
This class is used to represent a memory access.
triton::arch::Register baseReg
LEA - If the operand has a base register, this attribute is filled.
TRITON_EXPORT bool isOverlapWith(const MemoryAccess &other) const
Returns true if other and self overlap.
triton::uint64 address
The memory' address.
TRITON_EXPORT triton::uint64 getPcRelative(void) const
LEA - Gets pc relative.
TRITON_EXPORT triton::ast::SharedAbstractNode getLeaAst(void) const
Returns the AST of the memory access (LEA).
TRITON_EXPORT void setDisplacement(const triton::arch::Immediate &displacement)
LEA - Sets the displacement operand.
triton::arch::Immediate displacement
LEA - If the operand has a displacement, this attribute is filled.
triton::uint64 pcRelative
Contains the pc relative if it exists.
TRITON_EXPORT triton::arch::Register & getSegmentRegister(void)
LEA - Returns the segment register operand.
TRITON_EXPORT const triton::arch::Register & getConstBaseRegister(void) const
LEA - Returns the base register operand.
TRITON_EXPORT void setLeaAst(const triton::ast::SharedAbstractNode &ast)
Sets the AST of the memory access (LEA).
TRITON_EXPORT const triton::arch::Register & getConstSegmentRegister(void) const
LEA - Returns the segment register operand.
TRITON_EXPORT triton::uint32 getBitSize(void) const
Returns the size (in bits) of the memory vector.
TRITON_EXPORT triton::arch::operand_e getType(void) const
Returns the type of the operand (triton::arch::OPERAND_MEMORY).
TRITON_EXPORT triton::arch::Register & getIndexRegister(void)
LEA - Returns the index register operand.
TRITON_EXPORT triton::arch::Immediate & getScale(void)
LEA - Returns the scale operand.
TRITON_EXPORT const triton::arch::Immediate & getConstDisplacement(void) const
LEA - Returns the displacement operand.
TRITON_EXPORT MemoryAccess & operator=(const MemoryAccess &other)
Copies a MemoryAccess.
TRITON_EXPORT void setScale(const triton::arch::Immediate &scale)
LEA - Sets the scale operand.
TRITON_EXPORT triton::uint64 getAddress(void) const
Returns the address of the memory.
TRITON_EXPORT void setPcRelative(triton::uint64 addr)
LEA - Sets pc relative.
TRITON_EXPORT triton::uint32 getSize(void) const
Returns the size (in bytes) of the memory vector.
TRITON_EXPORT triton::arch::Immediate & getDisplacement(void)
LEA - Returns the displacement operand.
triton::arch::Immediate scale
LEA - If the operand has a scale, this attribute is filled.
triton::arch::Register segmentReg
LEA - If the operand has a segment register, this attribute is filled.
triton::arch::Register indexReg
LEA - If the operand has an index register, this attribute is filled.
TRITON_EXPORT void setAddress(triton::uint64 addr)
Sets the address of the memory access.
TRITON_EXPORT void setIndexRegister(const triton::arch::Register &index)
LEA - Sets the index register operand.
TRITON_EXPORT triton::arch::Register & getBaseRegister(void)
LEA - Returns the base register operand.
triton::ast::SharedAbstractNode leaAst
The AST of the memory access (LEA).
TRITON_EXPORT const triton::arch::Register & getConstIndexRegister(void) const
LEA - Returns the index register operand.
TRITON_EXPORT const triton::arch::Immediate & getConstScale(void) const
LEA - Returns the scale operand.
TRITON_EXPORT void setSegmentRegister(const triton::arch::Register &segment)
LEA - Sets the segment register operand.
TRITON_EXPORT void setBaseRegister(const triton::arch::Register &base)
LEA - Sets the base register operand.
TRITON_EXPORT MemoryAccess()
Constructor.
This class is used when an instruction has a register operand.
Definition register.hpp:44
The exception class used by memory access.
bool operator==(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
std::ostream & operator<<(std::ostream &stream, BasicBlock &block)
Displays an BasicBlock.
bool operator!=(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
bool operator<(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate (needed for std::map)
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition ast.hpp:59
constexpr triton::uint32 byte
byte size in bit
Definition cpuSize.hpp:60
constexpr triton::uint32 fword
fword size in byte
Definition cpuSize.hpp:38
constexpr triton::uint32 dword
dword size in byte
Definition cpuSize.hpp:34
constexpr triton::uint32 dqqword
dqqword size in byte
Definition cpuSize.hpp:44
constexpr triton::uint32 word
word size in byte
Definition cpuSize.hpp:32
constexpr triton::uint32 dqword
dqword size in byte
Definition cpuSize.hpp:40
constexpr triton::uint32 byte
byte size in byte
Definition cpuSize.hpp:30
constexpr triton::uint32 qword
qword size in byte
Definition cpuSize.hpp:36
constexpr triton::uint32 qqword
qqword size in byte
Definition cpuSize.hpp:42
std::uint64_t uint64
unisgned 64-bits
std::uint32_t uint32
unisgned 32-bits
The Triton namespace.