-
Notifications
You must be signed in to change notification settings - Fork 3
/
Instruction.cpp
252 lines (209 loc) · 4.08 KB
/
Instruction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "Instruction.hpp"
#include "Error.hpp"
#include "String.hpp"
namespace SoftWire
{
Instruction::Instruction()
{
}
Instruction::Instruction(const Syntax *syntax)
{
this->syntax = syntax;
extractOperands(syntax->operands);
if(secondOperand == Operand::OPERAND_IMM8)
{
if(Operand::isSubtypeOf(firstOperand, Operand::OPERAND_R_M16) ||
Operand::isSubtypeOf(firstOperand, Operand::OPERAND_R_M32))
{
secondOperand = Operand::OPERAND_EXT8;
}
}
}
Instruction::~Instruction()
{
}
Instruction &Instruction::operator=(const Instruction &instruction)
{
syntax = instruction.syntax;
specifier = instruction.specifier;
firstOperand = instruction.firstOperand;
secondOperand = instruction.secondOperand;
thirdOperand = instruction.thirdOperand;
return *this;
}
void Instruction::extractOperands(const char *syntax)
{
if(!syntax)
{
throw INTERNAL_ERROR;
}
specifier = Specifier::TYPE_UNKNOWN;
firstOperand = Operand::OPERAND_VOID;
secondOperand = Operand::OPERAND_VOID;
thirdOperand = Operand::OPERAND_VOID;
char string[256];
strncpy(string, syntax, 255);
const char *token = strtok(string, " ,");
if(!token)
{
return;
}
specifier = Specifier::scan(token);
if(specifier != Specifier::TYPE_UNKNOWN)
{
token = strtok(0, " ,");
if(!token)
{
return;
}
}
firstOperand = Operand::scanSyntax(token);
if(firstOperand != Operand::OPERAND_UNKNOWN)
{
token = strtok(0, " ,");
if(token == 0)
{
return;
}
}
secondOperand = Operand::scanSyntax(token);
if(secondOperand != Operand::OPERAND_UNKNOWN)
{
token = strtok(0, " ,");
if(token == 0)
{
return;
}
}
thirdOperand = Operand::scanSyntax(token);
if(thirdOperand != Operand::OPERAND_UNKNOWN)
{
token = strtok(0, " ,");
if(token == 0)
{
return;
}
}
if(token == 0)
{
return;
}
else
{
throw Error("Invalid operand encoding '%s'", syntax);
}
}
const char *Instruction::getMnemonic() const
{
return syntax->mnemonic;
}
Operand::Type Instruction::getFirstOperand() const
{
return firstOperand;
}
Operand::Type Instruction::getSecondOperand() const
{
return secondOperand;
}
Operand::Type Instruction::getThirdOperand() const
{
return thirdOperand;
}
const char *Instruction::getOperandSyntax() const
{
return syntax->operands;
}
const char *Instruction::getEncoding() const
{
return syntax->encoding;
}
bool Instruction::is16Bit() const
{
return (syntax->flags & CPU_386) != CPU_386;
}
bool Instruction::is32Bit() const
{
return (syntax->flags & CPU_386) == CPU_386;
}
bool Instruction::is64Bit() const
{
return (syntax->flags & CPU_X64) == CPU_X64;
}
bool Instruction::isInvalid64() const
{
return (syntax->flags & CPU_INVALID64) == CPU_INVALID64;
}
int Instruction::approximateSize() const
{
const char *format = syntax->encoding;
if(!format)
{
throw INTERNAL_ERROR;
}
int size = 0;
while(*format)
{
switch((format[0] << 8) | format[1])
{
case LOCK_PRE:
case CONST_PRE:
case REPNE_PRE:
case REP_PRE:
size += 1;
break;
case OFF_PRE:
if(!is32Bit())
{
size += 1;
}
break;
case ADDR_PRE:
if(!is32Bit())
{
size += 1;
}
break;
case ADD_REG:
break;
case EFF_ADDR:
case MOD_RM_0:
case MOD_RM_1:
case MOD_RM_2:
case MOD_RM_3:
case MOD_RM_4:
case MOD_RM_5:
case MOD_RM_6:
case MOD_RM_7:
size += 1;
break;
case DWORD_IMM:
case DWORD_REL:
size += 4;
break;
case WORD_IMM:
size += 2;
break;
case BYTE_IMM:
case BYTE_REL:
size += 1;
break;
default:
size += 1;
}
format += 2;
if(*format == ' ')
{
format++;
}
else if(*format == '\0')
{
break;
}
else
{
throw INTERNAL_ERROR;
}
}
return size;
}
}