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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "cutils/log.h"
#include "enc_base.h"
#include "enc_wrapper.h"
#include "dec_base.h"
//#define PRINT_ENCODER_STREAM
bool dump_x86_inst = false;
//map_reg
const RegName map_of_regno_2_regname[] = {
RegName_EAX, RegName_EBX, RegName_ECX, RegName_EDX,
RegName_EDI, RegName_ESI, RegName_ESP, RegName_EBP,
RegName_XMM0, RegName_XMM1, RegName_XMM2, RegName_XMM3,
RegName_XMM4, RegName_XMM5, RegName_XMM6, RegName_XMM7,
RegName_Null, RegName_Null, RegName_Null, RegName_Null,
RegName_Null, RegName_Null, RegName_Null, RegName_Null,
RegName_Null,
RegName_Null, RegName_Null, RegName_Null, RegName_Null, RegName_Null,
RegName_Null, RegName_Null, RegName_Null, RegName_Null,
RegName_Null, RegName_Null, RegName_Null, RegName_Null,
RegName_Null, RegName_Null, //SCRATCH
RegName_Null, RegName_Null, RegName_Null, RegName_Null
};
//getRegSize, getAliasReg:
//OpndSize, RegName, OpndExt: enum enc_defs.h
inline void add_r(EncoderBase::Operands & args, int physicalReg, OpndSize sz, OpndExt ext = OpndExt_None) {
RegName reg = map_of_regno_2_regname[physicalReg];
if (sz != getRegSize(reg)) {
reg = getAliasReg(reg, sz);
}
args.add(EncoderBase::Operand(reg, ext));
}
inline void add_m(EncoderBase::Operands & args, int baseReg, int disp, OpndSize sz, OpndExt ext = OpndExt_None) {
args.add(EncoderBase::Operand(sz,
map_of_regno_2_regname[baseReg],
RegName_Null, 0,
disp, ext));
}
inline void add_m_scale(EncoderBase::Operands & args, int baseReg, int indexReg, int scale,
OpndSize sz, OpndExt ext = OpndExt_None) {
args.add(EncoderBase::Operand(sz,
map_of_regno_2_regname[baseReg],
map_of_regno_2_regname[indexReg], scale,
0, ext));
}
inline void add_m_disp_scale(EncoderBase::Operands & args, int baseReg, int disp, int indexReg, int scale,
OpndSize sz, OpndExt ext = OpndExt_None) {
args.add(EncoderBase::Operand(sz,
map_of_regno_2_regname[baseReg],
map_of_regno_2_regname[indexReg], scale,
disp, ext));
}
inline void add_fp(EncoderBase::Operands & args, unsigned i, bool dbl) {
return args.add((RegName)( (dbl ? RegName_FP0D : RegName_FP0S) + i));
}
inline void add_imm(EncoderBase::Operands & args, OpndSize sz, int value, bool is_signed) {
//assert(n_size != imm.get_size());
args.add(EncoderBase::Operand(sz, value,
is_signed ? OpndExt_Signed : OpndExt_Zero));
}
#define MAX_DECODED_STRING_LEN 1024
char tmpBuffer[MAX_DECODED_STRING_LEN];
void printOperand(const EncoderBase::Operand & opnd) {
unsigned int sz;
if(!dump_x86_inst) return;
sz = strlen(tmpBuffer);
if(opnd.size() != OpndSize_32) {
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, "%s ",
getOpndSizeString(opnd.size()));
}
if(opnd.is_mem()) {
if(opnd.scale() != 0) {
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz,
"%d(%s,%s,%d)", opnd.disp(),
getRegNameString(opnd.base()),
getRegNameString(opnd.index()), opnd.scale());
} else {
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, "%d(%s)",
opnd.disp(), getRegNameString(opnd.base()));
}
}
if(opnd.is_imm()) {
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, "#%x",
(int)opnd.imm());
}
if(opnd.is_reg()) {
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, "%s",
getRegNameString(opnd.reg()));
}
}
//TODO: the order of operands
//to make the printout have the same order as assembly in .S
//I reverse the order here
void printDecoderInst(Inst & decInst) {
unsigned int sz;
if(!dump_x86_inst) return;
sz = strlen(tmpBuffer);
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, "%s ",
EncoderBase::toStr(decInst.mn));
for(unsigned int k = 0; k < decInst.argc; k++) {
if(k > 0) {
sz = strlen(tmpBuffer);
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, ", ");
}
printOperand(decInst.operands[decInst.argc-1-k]);
}
ALOGE("%s", tmpBuffer);
}
void printOperands(EncoderBase::Operands& opnds) {
unsigned int sz;
if(!dump_x86_inst) return;
for(unsigned int k = 0; k < opnds.count(); k++) {
if(k > 0) {
sz = strlen(tmpBuffer);
sz += snprintf(&tmpBuffer[sz], MAX_DECODED_STRING_LEN-sz, ", ");
}
printOperand(opnds[opnds.count()-1-k]);
}
}
void printEncoderInst(Mnemonic m, EncoderBase::Operands& opnds) {
if(!dump_x86_inst) return;
snprintf(tmpBuffer, MAX_DECODED_STRING_LEN, "--- ENC %s ",
EncoderBase::toStr(m));
printOperands(opnds);
ALOGE("%s", tmpBuffer);
}
int decodeThenPrint(char* stream_start) {
if(!dump_x86_inst) return 0;
snprintf(tmpBuffer, MAX_DECODED_STRING_LEN, "--- INST @ %p: ",
stream_start);
Inst decInst;
unsigned numBytes = DecoderBase::decode(stream_start, &decInst);
printDecoderInst(decInst);
return numBytes;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_imm(Mnemonic m, OpndSize size, int imm, char * stream) {
EncoderBase::Operands args;
//assert(imm.get_size() == size_32);
add_imm(args, size, imm, true/*is_signed*/);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT unsigned encoder_get_inst_size(char * stream) {
Inst decInst;
unsigned numBytes = DecoderBase::decode(stream, &decInst);
return numBytes;
}
extern "C" ENCODER_DECLARE_EXPORT unsigned encoder_get_cur_operand_offset(int opnd_id)
{
return (unsigned)EncoderBase::getOpndLocation(opnd_id);
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_update_imm(int imm, char * stream) {
Inst decInst;
unsigned numBytes = DecoderBase::decode(stream, &decInst);
EncoderBase::Operands args;
//assert(imm.get_size() == size_32);
add_imm(args, decInst.operands[0].size(), imm, true/*is_signed*/);
char* stream_next = (char *)EncoderBase::encode(stream, decInst.mn, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(decInst.mn, args);
decodeThenPrint(stream);
#endif
return stream_next;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_mem(Mnemonic m, OpndSize size,
int disp, int base_reg, bool isBasePhysical, char * stream) {
EncoderBase::Operands args;
add_m(args, base_reg, disp, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg(Mnemonic m, OpndSize size,
int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
if(m == Mnemonic_IDIV || m == Mnemonic_MUL || m == Mnemonic_IMUL) {
add_r(args, 0/*eax*/, size);
add_r(args, 3/*edx*/, size);
}
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
//both operands have same size
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg_reg(Mnemonic m, OpndSize size,
int reg, bool isPhysical,
int reg2, bool isPhysical2, LowOpndRegType type, char * stream) {
if((m == Mnemonic_MOV || m == Mnemonic_MOVQ) && reg == reg2) return stream;
EncoderBase::Operands args;
add_r(args, reg2, size); //destination
if(m == Mnemonic_SAL || m == Mnemonic_SHR || m == Mnemonic_SHL || m == Mnemonic_SAR)
add_r(args, reg, OpndSize_8);
else
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_mem_reg(Mnemonic m, OpndSize size,
int disp, int base_reg, bool isBasePhysical,
int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, size);
add_m(args, base_reg, disp, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_mem_scale_reg(Mnemonic m, OpndSize size,
int base_reg, bool isBasePhysical, int index_reg, bool isIndexPhysical, int scale,
int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, size);
add_m_scale(args, base_reg, index_reg, scale, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg_mem_scale(Mnemonic m, OpndSize size,
int reg, bool isPhysical,
int base_reg, bool isBasePhysical, int index_reg, bool isIndexPhysical, int scale,
LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_m_scale(args, base_reg, index_reg, scale, size);
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_mem_disp_scale_reg(Mnemonic m, OpndSize size,
int base_reg, bool isBasePhysical, int disp, int index_reg, bool isIndexPhysical, int scale,
int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, size);
add_m_disp_scale(args, base_reg, disp, index_reg, scale, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_movzs_mem_disp_scale_reg(Mnemonic m, OpndSize size,
int base_reg, bool isBasePhysical, int disp, int index_reg, bool isIndexPhysical, int scale,
int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, OpndSize_32);
add_m_disp_scale(args, base_reg, disp, index_reg, scale, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char* encoder_reg_mem_disp_scale(Mnemonic m, OpndSize size,
int reg, bool isPhysical,
int base_reg, bool isBasePhysical, int disp, int index_reg, bool isIndexPhysical, int scale,
LowOpndRegType type, char* stream) {
EncoderBase::Operands args;
add_m_disp_scale(args, base_reg, disp, index_reg, scale, size);
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg_mem(Mnemonic m, OpndSize size,
int reg, bool isPhysical,
int disp, int base_reg, bool isBasePhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_m(args, base_reg, disp, size);
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_imm_reg(Mnemonic m, OpndSize size,
int imm, int reg, bool isPhysical, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, size); //dst
if(m == Mnemonic_IMUL) add_r(args, reg, size); //src CHECK
if(m == Mnemonic_SAL || m == Mnemonic_SHR || m == Mnemonic_SHL
|| m == Mnemonic_SAR || m == Mnemonic_ROR) //fix for shift opcodes
add_imm(args, OpndSize_8, imm, true/*is_signed*/);
else
add_imm(args, size, imm, true/*is_signed*/);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_update_imm_rm(int imm, char * stream) {
Inst decInst;
unsigned numBytes = DecoderBase::decode(stream, &decInst);
EncoderBase::Operands args;
args.add(decInst.operands[0]);
add_imm(args, decInst.operands[1].size(), imm, true/*is_signed*/);
char* stream_next = (char *)EncoderBase::encode(stream, decInst.mn, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(decInst.mn, args);
decodeThenPrint(stream);
#endif
return stream_next;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_imm_mem(Mnemonic m, OpndSize size,
int imm,
int disp, int base_reg, bool isBasePhysical, char * stream) {
EncoderBase::Operands args;
add_m(args, base_reg, disp, size);
if (m == Mnemonic_SAL || m == Mnemonic_SHR || m == Mnemonic_SHL
|| m == Mnemonic_SAR || m == Mnemonic_ROR)
size = OpndSize_8;
add_imm(args, size, imm, true);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_fp_mem(Mnemonic m, OpndSize size, int reg,
int disp, int base_reg, bool isBasePhysical, char * stream) {
EncoderBase::Operands args;
add_m(args, base_reg, disp, size);
// a fake FP register as operand
add_fp(args, reg, size == OpndSize_64/*is_double*/);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_mem_fp(Mnemonic m, OpndSize size,
int disp, int base_reg, bool isBasePhysical,
int reg, char * stream) {
EncoderBase::Operands args;
// a fake FP register as operand
add_fp(args, reg, size == OpndSize_64/*is_double*/);
add_m(args, base_reg, disp, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_return(char * stream) {
EncoderBase::Operands args;
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, Mnemonic_RET, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(Mnemonic_RET, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_compare_fp_stack(bool pop, int reg, bool isDouble, char * stream) {
//Mnemonic m = pop ? Mnemonic_FUCOMP : Mnemonic_FUCOM;
Mnemonic m = pop ? Mnemonic_FUCOMIP : Mnemonic_FUCOMI;
//a single operand or 2 operands?
//FST ST(i) has a single operand in encoder.inl?
EncoderBase::Operands args;
add_fp(args, reg, isDouble);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, m, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(m, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_movez_mem_to_reg(OpndSize size,
int disp, int base_reg, bool isBasePhysical,
int reg, bool isPhysical, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, OpndSize_32);
add_m(args, base_reg, disp, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVZX, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(Mnemonic_MOVZX, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_moves_mem_to_reg(OpndSize size,
int disp, int base_reg, bool isBasePhysical,
int reg, bool isPhysical, char * stream) {
EncoderBase::Operands args;
add_r(args, reg, OpndSize_32);
add_m(args, base_reg, disp, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVSX, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(Mnemonic_MOVSX, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_movez_reg_to_reg(OpndSize size,
int reg, bool isPhysical, int reg2,
bool isPhysical2, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg2, OpndSize_32); //destination
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVZX, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(Mnemonic_MOVZX, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
extern "C" ENCODER_DECLARE_EXPORT char * encoder_moves_reg_to_reg(OpndSize size,
int reg, bool isPhysical,int reg2,
bool isPhysical2, LowOpndRegType type, char * stream) {
EncoderBase::Operands args;
add_r(args, reg2, OpndSize_32); //destination
add_r(args, reg, size);
char* stream_start = stream;
stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVSX, args);
#ifdef PRINT_ENCODER_STREAM
printEncoderInst(Mnemonic_MOVSX, args);
decodeThenPrint(stream_start);
#endif
return stream;
}
// Disassemble the operand "opnd" and put the readable format in "strbuf"
// up to a string length of "len".
unsigned int DisassembleOperandToBuf(const EncoderBase::Operand& opnd, char* strbuf, unsigned int len)
{
unsigned int sz = 0;
if(opnd.size() != OpndSize_32) {
sz += snprintf(&strbuf[sz], len-sz, "%s ",
getOpndSizeString(opnd.size()));
}
if(opnd.is_mem()) {
if(opnd.scale() != 0) {
sz += snprintf(&strbuf[sz], len-sz, "%d(%s,%s,%d)", opnd.disp(),
getRegNameString(opnd.base()),
getRegNameString(opnd.index()), opnd.scale());
} else {
sz += snprintf(&strbuf[sz], len-sz, "%d(%s)",
opnd.disp(), getRegNameString(opnd.base()));
}
} else if(opnd.is_imm()) {
sz += snprintf(&strbuf[sz], len-sz, "#%x", (int)opnd.imm());
} else if(opnd.is_reg()) {
sz += snprintf(&strbuf[sz], len-sz, "%s",
getRegNameString(opnd.reg()));
}
return sz;
}
// Disassemble the instruction "decInst" and put the readable format
// in "strbuf" up to a string length of "len".
void DisassembleInstToBuf(Inst& decInst, char* strbuf, unsigned int len)
{
unsigned int sz = 0;
int k;
sz += snprintf(&strbuf[sz], len-sz, "%s ", EncoderBase::toStr(decInst.mn));
if (decInst.argc > 0) {
sz += DisassembleOperandToBuf(decInst.operands[decInst.argc-1],
&strbuf[sz], len-sz);
for(k = decInst.argc-2; k >= 0; k--) {
sz += snprintf(&strbuf[sz], len-sz, ", ");
sz += DisassembleOperandToBuf(decInst.operands[k], &strbuf[sz], len-sz);
}
}
}
// Disassmble the x86 instruction pointed to by code pointer "stream."
// Put the disassemble text in the "strbuf" up to string length "len".
// Return the code pointer after the disassemble x86 instruction.
extern "C" ENCODER_DECLARE_EXPORT
char* decoder_disassemble_instr(char* stream, char* strbuf, unsigned int len)
{
Inst decInst;
unsigned numBytes = DecoderBase::decode(stream, &decInst);
DisassembleInstToBuf(decInst, strbuf, len);
return (stream + numBytes);
}
|