1
//! \file
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>
9
#include <triton/exceptions.hpp>
10
#include <triton/memoryAccess.hpp>
11

12

13

14
namespace triton {
15
  namespace arch {
16

17 1
    MemoryAccess::MemoryAccess() {
18 1
      this->address    = 0;
19 1
      this->leaAst     = nullptr;
20 1
      this->pcRelative = 0;
21
    }
22

23

24 1
    MemoryAccess::MemoryAccess(triton::uint64 address, triton::uint32 size /* bytes */)
25 1
      : MemoryAccess() {
26 1
      this->address = address;
27

28 1
      if (size == 0)
29 0
        throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size cannot be zero.");
30

31 1
      if (size != triton::size::byte     &&
32 1
          size != triton::size::word     &&
33 1
          size != triton::size::dword    &&
34 1
          size != triton::size::qword    &&
35 1
          size != triton::size::dqword   &&
36 0
          size != triton::size::qqword   &&
37 0
          size != triton::size::dqqword)
38 0
        throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size must be aligned.");
39

40 1
      this->setPair(std::make_pair(((size * triton::bitsize::byte) - 1), 0));
41
    }
42

43

44 1
    MemoryAccess::MemoryAccess(const MemoryAccess& other)
45 1
      : BitsVector(other) {
46 1
      this->copy(other);
47
    }
48

49

50 1
    triton::uint64 MemoryAccess::getAddress(void) const {
51 1
      return this->address;
52
    }
53

54

55 1
    triton::ast::SharedAbstractNode MemoryAccess::getLeaAst(void) const {
56 1
      return this->leaAst;
57
    }
58

59

60 1
    triton::uint64 MemoryAccess::getPcRelative(void) const {
61 1
      return this->pcRelative;
62
    }
63

64

65 1
    triton::uint32 MemoryAccess::getBitSize(void) const {
66 1
      return this->getVectorSize();
67
    }
68

69

70 1
    triton::uint32 MemoryAccess::getSize(void) const {
71 1
      return this->getVectorSize() / triton::bitsize::byte;
72
    }
73

74

75 1
    triton::arch::operand_e MemoryAccess::getType(void) const {
76 1
      return triton::arch::OP_MEM;
77
    }
78

79

80 1
    triton::arch::Register& MemoryAccess::getSegmentRegister(void) {
81 1
      return this->segmentReg;
82
    }
83

84

85 1
    triton::arch::Register& MemoryAccess::getBaseRegister(void) {
86 1
      return this->baseReg;
87
    }
88

89

90 1
    triton::arch::Register& MemoryAccess::getIndexRegister(void) {
91 1
      return this->indexReg;
92
    }
93

94

95 1
    triton::arch::Immediate& MemoryAccess::getDisplacement(void) {
96 1
      return this->displacement;
97
    }
98

99

100 1
    triton::arch::Immediate& MemoryAccess::getScale(void) {
101 1
      return this->scale;
102
    }
103

104

105 1
    const triton::arch::Register& MemoryAccess::getConstSegmentRegister(void) const {
106 1
      return this->segmentReg;
107
    }
108

109

110 1
    const triton::arch::Register& MemoryAccess::getConstBaseRegister(void) const {
111 1
      return this->baseReg;
112
    }
113

114

115 1
    const triton::arch::Register& MemoryAccess::getConstIndexRegister(void) const {
116 1
      return this->indexReg;
117
    }
118

119

120 1
    const triton::arch::Immediate& MemoryAccess::getConstDisplacement(void) const {
121 1
      return this->displacement;
122
    }
123

124

125 1
    const triton::arch::Immediate& MemoryAccess::getConstScale(void) const {
126 1
      return this->scale;
127
    }
128

129

130 1
    bool MemoryAccess::isOverlapWith(const MemoryAccess& other) const {
131 1
      if (this->getAddress() <= other.getAddress() && other.getAddress() < (this->getAddress() + this->getSize())) return true;
132 1
      if (other.getAddress() <= this->getAddress() && this->getAddress() < (other.getAddress() + other.getSize())) return true;
133 1
      return false;
134
    }
135

136

137 1
    void MemoryAccess::setAddress(triton::uint64 addr) {
138 1
      this->address = addr;
139
    }
140

141

142 1
    void MemoryAccess::setPcRelative(triton::uint64 addr) {
143 1
      this->pcRelative = addr;
144
    }
145

146

147 1
    void MemoryAccess::setSegmentRegister(const triton::arch::Register& segment) {
148 1
      this->segmentReg = segment;
149
    }
150

151

152 1
    void MemoryAccess::setBaseRegister(const triton::arch::Register& base) {
153 1
      this->baseReg = base;
154
    }
155

156

157 1
    void MemoryAccess::setIndexRegister(const triton::arch::Register& index) {
158 1
      this->indexReg = index;
159
    }
160

161

162 1
    void MemoryAccess::setDisplacement(const triton::arch::Immediate& displacement) {
163 1
      this->displacement = displacement;
164
    }
165

166

167 1
    void MemoryAccess::setScale(const triton::arch::Immediate& scale) {
168 1
      this->scale = scale;
169
    }
170

171

172 1
    void MemoryAccess::setLeaAst(const triton::ast::SharedAbstractNode& ast) {
173 1
      this->leaAst = ast;
174
    }
175

176

177 1
    MemoryAccess& MemoryAccess::operator=(const MemoryAccess& other) {
178 1
      BitsVector::operator=(other);
179 1
      this->copy(other);
180 1
      return *this;
181
    }
182

183

184 1
    void MemoryAccess::copy(const MemoryAccess& other) {
185 1
      this->address      = other.address;
186 1
      this->baseReg      = other.baseReg;
187 1
      this->displacement = other.displacement;
188 1
      this->indexReg     = other.indexReg;
189 1
      this->leaAst       = other.leaAst;
190 1
      this->pcRelative   = other.pcRelative;
191 1
      this->scale        = other.scale;
192 1
      this->segmentReg   = other.segmentReg;
193
    }
194

195

196 1
    std::ostream& operator<<(std::ostream& stream, const MemoryAccess& mem) {
197 1
      stream << "[@0x"
198 1
             << std::hex << mem.getAddress()
199 1
             << "]:"
200 1
             << std::dec << mem.getBitSize()
201 1
             << " bv["
202 1
             << mem.getHigh()
203 1
             << ".."
204 1
             << mem.getLow()
205 1
             << "]";
206 1
      return stream;
207
    }
208

209

210 1
    std::ostream& operator<<(std::ostream& stream, const MemoryAccess* mem) {
211 1
      stream << *mem;
212 1
      return stream;
213
    }
214

215

216 1
    bool operator==(const MemoryAccess& mem1, const MemoryAccess& mem2) {
217 1
      if (mem1.getAddress() != mem2.getAddress())
218
        return false;
219 1
      if (mem1.getSize() != mem2.getSize())
220
        return false;
221 1
      if (mem1.getConstBaseRegister() != mem2.getConstBaseRegister())
222
        return false;
223 1
      if (mem1.getConstIndexRegister() != mem2.getConstIndexRegister())
224
        return false;
225 1
      if (mem1.getConstScale() != mem2.getConstScale())
226
        return false;
227 1
      if (mem1.getConstDisplacement() != mem2.getConstDisplacement())
228
        return false;
229 1
      if (mem1.getConstSegmentRegister() != mem2.getConstSegmentRegister())
230
        return false;
231 1
      if (mem1.getPcRelative() != mem2.getPcRelative())
232
        return false;
233 1
      return true;
234
    }
235

236

237 1
    bool operator!=(const MemoryAccess& mem1, const MemoryAccess& mem2) {
238 1
      return !(mem1 == mem2);
239
    }
240

241

242 1
    bool operator<(const MemoryAccess& mem1, const MemoryAccess& mem2) {
243 1
      triton::uint64 seed1 = 0;
244 1
      triton::uint64 seed2 = 0;
245

246
      /*
247
       * Golden ratio 32-bits -> 0x9e3779b9
248
       * Golden ratio 64-bits -> 0x9e3779b97f4a7c13
249
       */
250 1
      seed1 ^= mem1.getAddress() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
251 1
      seed1 ^= mem1.getSize() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
252

253 1
      seed2 ^= mem2.getAddress() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
254 1
      seed2 ^= mem2.getSize() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
255

256 1
      return (seed1 < seed2);
257
    }
258

259
  }; /* arch namespace */
260 1
}; /* triton namespace */

Read our documentation on viewing source code .

Loading