Loading [MathJax]/extensions/tex2jax.js
libTriton version 1.0 build 1599
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
immediate.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
9#include <triton/cpuSize.hpp>
10#include <triton/exceptions.hpp>
11#include <triton/softfloat.hpp>
12#include <triton/coreUtils.hpp>
13
14#ifdef LITTLE_ENDIAN // provided by CMake
15constexpr auto sys_endianness = triton::arch::LE_ENDIANNESS;
16#else
17constexpr auto sys_endianness = triton::arch::BE_ENDIANNESS;
18#endif
19
20
21namespace triton {
22 namespace arch {
23
25 this->value = 0;
26 }
27
28
30 this->setValue(value, size);
31 }
32
33
34 Immediate::Immediate(double value, triton::uint32 size /* bytes */, triton::arch::endianness_e platform_endianness) {
35 triton::uint64 imm_value;
36 auto need_swap = sys_endianness != platform_endianness;
37
38 if (size == sizeof(double)) {
39 static_assert(sizeof(double) == sizeof(triton::uint64), "Unexpected double type size");
40 std::memcpy(&imm_value, &value, sizeof(double));
41 if (need_swap) {
42 imm_value = utils::byteswap(imm_value);
43 }
44 }
45
46 else if (size == sizeof(float)) { // single-precision
47 float fvalue = static_cast<float>(value);
48 triton::uint32 repr;
49 static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size");
50 std::memcpy(&repr, &fvalue, sizeof(float));
51 imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr)) : static_cast<triton::uint64>(repr);
52 }
53
54 else if (size == 2) { // half-precision
55 float fvalue = static_cast<float>(value);
57 imm_value = need_swap ? static_cast<triton::uint64>(utils::byteswap(repr)) : static_cast<triton::uint64>(repr);
58 }
59
60 else {
61 throw triton::exceptions::Immediate("Immediate::Immediate(double): Invalid encoding size.");
62 }
63
64 this->setValue(imm_value, size);
65 }
66
67
69 : BitsVector(other),
70 ArmOperandProperties(other) {
71 this->copy(other);
72 }
73
74
76 return this->value;
77 }
78
79
80 void Immediate::setValue(triton::uint64 value, triton::uint32 size /* bytes */) {
81 /* If the size is zero, try to define the size according to the value. */
82 if (size == 0) {
83 if (/* ..... 0x0000000000000000 */ value <= 0x00000000000000ff) size = triton::size::byte;
84 else if (value >= 0x0000000000000100 && value <= 0x000000000000ffff) size = triton::size::word;
85 else if (value >= 0x0000000000010000 && value <= 0x00000000ffffffff) size = triton::size::dword;
86 else if (value >= 0x0000000100000000 && value <= 0xffffffffffffffff) size = triton::size::qword;
87 }
88
89 if (size != triton::size::byte &&
90 size != triton::size::word &&
91 size != triton::size::dword &&
92 size != triton::size::qword &&
93 size != triton::size::fword &&
94 size != triton::size::dqword &&
95 size != triton::size::qqword &&
97 throw triton::exceptions::Immediate("Immediate::setValue(): size must be aligned.");
98
99 switch (size) {
101 this->value = static_cast<triton::uint8>(value);
102 break;
103
105 this->value = static_cast<triton::uint16>(value);
106 break;
107
109 this->value = static_cast<triton::uint32>(value);
110 break;
111
112 /* In most CPU cases, integers more than 64 are loaded from memory */
113 default:
114 this->value = value;
115 }
116
117 this->setBits(((size * triton::bitsize::byte) - 1), 0);
118 }
119
120
122 return this->getVectorSize() / triton::bitsize::byte;
123 }
124
125
127 return this->getVectorSize();
128 }
129
130
134
135
137 ArmOperandProperties::operator=(other);
139 this->copy(other);
140 return *this;
141 }
142
143
144 void Immediate::copy(const Immediate& other) {
145 this->value = other.value;
146 }
147
148
149 std::ostream& operator<<(std::ostream& stream, const Immediate& imm) {
150 stream << "0x"
151 << std::hex << imm.getValue()
152 << ":"
153 << std::dec << imm.getBitSize()
154 << " bv["
155 << imm.getHigh()
156 << ".."
157 << imm.getLow()
158 << "]";
159 return stream;
160 }
161
162
163 std::ostream& operator<<(std::ostream& stream, const Immediate* imm) {
164 stream << *imm;
165 return stream;
166 }
167
168
169 bool operator==(const Immediate& imm1, const Immediate& imm2) {
170 if (imm1.getValue() != imm2.getValue())
171 return false;
172 if (imm1.getSize() != imm2.getSize())
173 return false;
174 return true;
175 }
176
177
178 bool operator!=(const Immediate& imm1, const Immediate& imm2) {
179 return !(imm1 == imm2);
180 }
181
182
183 bool operator<(const Immediate& imm1, const Immediate& imm2) {
184 triton::uint64 seed1 = 0;
185 triton::uint64 seed2 = 0;
186
187 /*
188 * Golden ratio 32-bits -> 0x9e3779b9
189 * Golden ratio 64-bits -> 0x9e3779b97f4a7c13
190 */
191 seed1 ^= imm1.getValue() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
192 seed1 ^= imm1.getSize() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
193
194 seed2 ^= imm2.getValue() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
195 seed2 ^= imm2.getSize() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
196
197 return (seed1 < seed2);
198 }
199
200 }; /* arch namespace */
201}; /* 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
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.
TRITON_EXPORT Immediate & operator=(const Immediate &other)
Copy an Immediate.
TRITON_EXPORT triton::uint32 getBitSize(void) const
Returns the size (in bits) of the immediate vector.
TRITON_EXPORT triton::uint64 getValue(void) const
Returns the value of the operand.
Definition immediate.cpp:75
TRITON_EXPORT triton::arch::operand_e getType(void) const
Returns the type of the operand (triton::arch::OPERAND_IMMEDIATE).
TRITON_EXPORT void setValue(triton::uint64 v, triton::uint32 size)
Sets the value of the operand.
Definition immediate.cpp:80
TRITON_EXPORT Immediate()
Constructor.
Definition immediate.cpp:24
The exception class used by immediates.
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)
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
auto f32_to_f16(float value) -> uint16_t
Cast 32-bit floating point value to 16-bit according to IEEE-754.
Definition softfloat.cpp:15
std::uint16_t uint16
unisgned 16-bits
std::uint64_t uint64
unisgned 64-bits
std::uint32_t uint32
unisgned 32-bits
std::uint8_t uint8
unisgned 8-bits
The Triton namespace.