libTriton  version 0.9 build 1502
immediate.cpp
Go to the documentation of this file.
1 /*
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/immediate.hpp>
11 
12 
13 
14 namespace triton {
15  namespace arch {
16 
18  this->value = 0;
19  }
20 
21 
23  this->setValue(value, size);
24  }
25 
26 
28  : BitsVector(other),
29  ArmOperandProperties(other) {
30  this->copy(other);
31  }
32 
33 
35  return this->value;
36  }
37 
38 
39  void Immediate::setValue(triton::uint64 value, triton::uint32 size /* bytes */) {
40  /* If the size is zero, try to define the size according to the value. */
41  if (size == 0) {
42  if (/* ..... 0x0000000000000000 */ value <= 0x00000000000000ff) size = triton::size::byte;
43  else if (value >= 0x0000000000000100 && value <= 0x000000000000ffff) size = triton::size::word;
44  else if (value >= 0x0000000000010000 && value <= 0x00000000ffffffff) size = triton::size::dword;
45  else if (value >= 0x0000000100000000 && value <= 0xffffffffffffffff) size = triton::size::qword;
46  }
47 
48  if (size != triton::size::byte &&
49  size != triton::size::word &&
50  size != triton::size::dword &&
51  size != triton::size::qword &&
52  size != triton::size::dqword &&
53  size != triton::size::qqword &&
54  size != triton::size::dqqword)
55  throw triton::exceptions::Immediate("Immediate::setValue(): size must be aligned.");
56 
57  switch (size) {
58  case triton::size::byte:
59  this->value = static_cast<triton::uint8>(value);
60  break;
61 
62  case triton::size::word:
63  this->value = static_cast<triton::uint16>(value);
64  break;
65 
67  this->value = static_cast<triton::uint32>(value);
68  break;
69 
70  /* In most CPU cases, integers more than 64 are loaded from memory */
71  default:
72  this->value = value;
73  }
74 
75  this->setBits(((size * triton::bitsize::byte) - 1), 0);
76  }
77 
78 
80  return this->getVectorSize() / triton::bitsize::byte;
81  }
82 
83 
85  return this->getVectorSize();
86  }
87 
88 
90  return triton::arch::OP_IMM;
91  }
92 
93 
95  ArmOperandProperties::operator=(other);
96  BitsVector::operator=(other);
97  this->copy(other);
98  return *this;
99  }
100 
101 
102  void Immediate::copy(const Immediate& other) {
103  this->value = other.value;
104  }
105 
106 
107  std::ostream& operator<<(std::ostream& stream, const Immediate& imm) {
108  stream << "0x"
109  << std::hex << imm.getValue()
110  << ":"
111  << std::dec << imm.getBitSize()
112  << " bv["
113  << imm.getHigh()
114  << ".."
115  << imm.getLow()
116  << "]";
117  return stream;
118  }
119 
120 
121  std::ostream& operator<<(std::ostream& stream, const Immediate* imm) {
122  stream << *imm;
123  return stream;
124  }
125 
126 
127  bool operator==(const Immediate& imm1, const Immediate& imm2) {
128  if (imm1.getValue() != imm2.getValue())
129  return false;
130  if (imm1.getSize() != imm2.getSize())
131  return false;
132  return true;
133  }
134 
135 
136  bool operator!=(const Immediate& imm1, const Immediate& imm2) {
137  return !(imm1 == imm2);
138  }
139 
140 
141  bool operator<(const Immediate& imm1, const Immediate& imm2) {
142  triton::uint64 seed1 = 0;
143  triton::uint64 seed2 = 0;
144 
145  /*
146  * Golden ratio 32-bits -> 0x9e3779b9
147  * Golden ratio 64-bits -> 0x9e3779b97f4a7c13
148  */
149  seed1 ^= imm1.getValue() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
150  seed1 ^= imm1.getSize() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
151 
152  seed2 ^= imm2.getValue() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
153  seed2 ^= imm2.getSize() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
154 
155  return (seed1 < seed2);
156  }
157 
158  }; /* arch namespace */
159 }; /* triton namespace */
This class is used to deal with registers and memory as bits vector.
Definition: bitsVector.hpp:36
TRITON_EXPORT triton::uint32 getHigh(void) const
Returns the highest bit.
Definition: bitsVector.cpp:34
TRITON_EXPORT triton::uint32 getVectorSize(void) const
Returns the size in bits of the vector.
Definition: bitsVector.cpp:44
TRITON_EXPORT triton::uint32 getLow(void) const
Returns the lower bit.
Definition: bitsVector.cpp:39
TRITON_EXPORT void setBits(triton::uint32 high, triton::uint32 low)
Sets the bits (high, low) position.
Definition: bitsVector.cpp:78
TRITON_EXPORT BitsVector & operator=(const BitsVector &other)
Copy a BitsVector.
Definition: bitsVector.cpp:96
This class is used to represent an immediate.
Definition: immediate.hpp:37
triton::uint64 value
The value of the operand.
Definition: immediate.hpp:40
TRITON_EXPORT triton::uint32 getSize(void) const
Returns the size (in bytes) of the immediate vector.
Definition: immediate.cpp:79
TRITON_EXPORT Immediate & operator=(const Immediate &other)
Copy an Immediate.
Definition: immediate.cpp:94
TRITON_EXPORT triton::uint32 getBitSize(void) const
Returns the size (in bits) of the immediate vector.
Definition: immediate.cpp:84
TRITON_EXPORT triton::uint64 getValue(void) const
Returns the value of the operand.
Definition: immediate.cpp:34
TRITON_EXPORT triton::arch::operand_e getType(void) const
Returns the type of the operand (triton::arch::OPERAND_IMMEDIATE).
Definition: immediate.cpp:89
TRITON_EXPORT void setValue(triton::uint64 v, triton::uint32 size)
Sets the value of the operand.
Definition: immediate.cpp:39
TRITON_EXPORT Immediate()
Constructor.
Definition: immediate.cpp:17
The exception class used by immediates.
Definition: exceptions.hpp:221
std::ostream & operator<<(std::ostream &stream, const BitsVector &bv)
Displays a BitsVector.
Definition: bitsVector.cpp:103
bool operator==(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
Definition: immediate.cpp:127
bool operator!=(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
Definition: immediate.cpp:136
bool operator<(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate (needed for std::map)
Definition: immediate.cpp:141
@ OP_IMM
immediate operand
Definition: archEnums.hpp:49
constexpr triton::uint32 byte
byte size in bit
Definition: cpuSize.hpp:58
constexpr triton::uint32 dword
dword size in byte
Definition: cpuSize.hpp:34
constexpr triton::uint32 dqqword
dqqword size in byte
Definition: cpuSize.hpp:42
constexpr triton::uint32 word
word size in byte
Definition: cpuSize.hpp:32
constexpr triton::uint32 dqword
dqword size in byte
Definition: cpuSize.hpp:38
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:40
std::uint16_t uint16
unisgned 16-bits
Definition: tritonTypes.hpp:28
std::uint64_t uint64
unisgned 64-bits
Definition: tritonTypes.hpp:34
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
std::uint8_t uint8
unisgned 8-bits
Definition: tritonTypes.hpp:25
The Triton namespace.
Definition: api.cpp:227