Skip to content

Commit 9a603cc

Browse files
committed
Optimize tests and fix imm in map
1 parent 6b7b141 commit 9a603cc

File tree

113 files changed

+115635
-509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+115635
-509
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ cstool/cstool
143143
android-ndk-*
144144

145145
# python virtual env
146-
.venv/
146+
.ven*/
147147

148148
# Auto-sync files
149149
suite/auto-sync/src/autosync.egg-info

arch/Mips/MipsGenCSMappingInsnOp.inc

Lines changed: 123 additions & 123 deletions
Large diffs are not rendered by default.

arch/Mips/MipsInstPrinter.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,24 @@ static void printRegName(MCInst *MI, SStream *OS, MCRegister Reg)
121121
}
122122

123123
void Mips_LLVM_printInst(MCInst *MI, uint64_t Address, SStream *O) {
124-
if (!printAliasInstr(MI, Address, O) && !printAlias4(MI, Address, O))
124+
bool useAliasDetails = map_use_alias_details(MI);
125+
if (!useAliasDetails) {
126+
SStream_Close(O);
125127
printInstruction(MI, Address, O);
128+
SStream_Open(O);
129+
map_set_fill_detail_ops(MI, false);
130+
}
131+
132+
if (printAliasInstr(MI, Address, O) ||
133+
printAlias4(MI, Address, O)) {
134+
MCInst_setIsAlias(MI, true);
135+
} else {
136+
printInstruction(MI, Address, O);
137+
}
138+
139+
if (!useAliasDetails) {
140+
map_set_fill_detail_ops(MI, true);
141+
}
126142
}
127143

128144
void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
@@ -520,7 +536,7 @@ static void printPCRel(MCInst *MI, uint64_t Address, int OpNum, SStream *O)
520536

521537
const char *Mips_LLVM_getRegisterName(unsigned RegNo, bool noRegName)
522538
{
523-
if (RegNo >= MIPS_REG_ENDING) {
539+
if (!RegNo || RegNo >= MIPS_REG_ENDING) {
524540
return NULL;
525541
}
526542
if (noRegName) {

arch/Mips/MipsMapping.c

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ bool Mips_getInstruction(csh handle, const uint8_t *code, size_t code_len,
175175
uint64_t size64;
176176
Mips_init_cs_detail(instr);
177177
instr->MRI = (MCRegisterInfo *)info;
178+
map_set_fill_detail_ops(instr, true);
178179

179180
bool result = Mips_LLVM_getInstruction(instr, &size64, code,
180181
code_len, address, info)
@@ -250,33 +251,49 @@ static void Mips_set_detail_op_imm(MCInst *MI, unsigned OpNum, int64_t Imm)
250251
Mips_inc_op_count(MI);
251252
}
252253

253-
static void Mips_set_detail_op_reg(MCInst *MI, unsigned OpNum, mips_reg Reg)
254+
static void Mips_set_detail_op_uimm(MCInst *MI, unsigned OpNum, uint64_t Imm)
254255
{
255256
if (!detail_is_set(MI))
256257
return;
257258

258259
if (doing_mem(MI)) {
259-
Mips_set_detail_op_mem_reg(MI, OpNum, Reg);
260+
Mips_set_detail_op_mem_disp(MI, OpNum, Imm);
260261
return;
261262
}
262263

263-
assert((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_REG);
264+
Mips_get_detail_op(MI, 0)->type = MIPS_OP_IMM;
265+
Mips_get_detail_op(MI, 0)->imm = (int64_t)Imm;
266+
Mips_get_detail_op(MI, 0)->is_unsigned = true;
267+
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
268+
Mips_inc_op_count(MI);
269+
}
264270

271+
static void Mips_set_detail_op_reg(MCInst *MI, unsigned OpNum, mips_reg Reg, bool is_reglist)
272+
{
273+
if (!detail_is_set(MI))
274+
return;
275+
276+
if (doing_mem(MI)) {
277+
Mips_set_detail_op_mem_reg(MI, OpNum, Reg);
278+
return;
279+
}
280+
281+
CS_ASSERT((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_REG);
265282
Mips_get_detail_op(MI, 0)->type = MIPS_OP_REG;
266283
Mips_get_detail_op(MI, 0)->reg = Reg;
284+
Mips_get_detail_op(MI, 0)->is_reglist = is_reglist;
267285
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
268286
Mips_inc_op_count(MI);
269287
}
270288

271289
static void Mips_set_detail_op_operand(MCInst *MI, unsigned OpNum)
272290
{
273291
cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
292+
int64_t value = MCInst_getOpVal(MI, OpNum);
274293
if (op_type == CS_OP_IMM) {
275-
Mips_set_detail_op_imm(MI, OpNum,
276-
MCInst_getOpVal(MI, OpNum));
294+
Mips_set_detail_op_imm(MI, OpNum, value);
277295
} else if (op_type == CS_OP_REG) {
278-
Mips_set_detail_op_reg(MI, OpNum,
279-
MCInst_getOpVal(MI, OpNum));
296+
Mips_set_detail_op_reg(MI, OpNum, value, false);
280297
} else
281298
printf("Operand type %d not handled!\n", op_type);
282299
}
@@ -286,34 +303,33 @@ static void Mips_set_detail_op_branch(MCInst *MI, unsigned OpNum)
286303
cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
287304
if (op_type == CS_OP_IMM) {
288305
uint64_t Target = (uint64_t)MCInst_getOpVal(MI, OpNum);
289-
Mips_set_detail_op_imm(MI, OpNum, Target + MI->address);
306+
Mips_set_detail_op_uimm(MI, OpNum, Target + MI->address);
290307
} else if (op_type == CS_OP_REG) {
291308
Mips_set_detail_op_reg(MI, OpNum,
292-
MCInst_getOpVal(MI, OpNum));
309+
MCInst_getOpVal(MI, OpNum), false);
293310
} else
294311
printf("Operand type %d not handled!\n", op_type);
295312
}
296313

297-
static void Mips_set_detail_op_uimm(MCInst *MI, unsigned OpNum)
314+
static void Mips_set_detail_op_unsigned(MCInst *MI, unsigned OpNum)
298315
{
299-
Mips_set_detail_op_imm(MI, OpNum,
316+
Mips_set_detail_op_uimm(MI, OpNum,
300317
MCInst_getOpVal(MI, OpNum));
301318
}
302319

303-
static void Mips_set_detail_op_uimm_offset(MCInst *MI, unsigned OpNum,
320+
static void Mips_set_detail_op_unsigned_offset(MCInst *MI, unsigned OpNum,
304321
unsigned Bits, uint64_t Offset)
305322
{
306323
uint64_t Imm = MCInst_getOpVal(MI, OpNum);
307324
Imm -= Offset;
308325
Imm &= (1 << Bits) - 1;
309326
Imm += Offset;
310-
Mips_set_detail_op_imm(MI, OpNum, Imm);
327+
Mips_set_detail_op_uimm(MI, OpNum, Imm);
311328
}
312329

313330
static void Mips_set_detail_op_mem_nanomips(MCInst *MI, unsigned OpNum)
314331
{
315-
if (!detail_is_set(MI) || !doing_mem(MI))
316-
return;
332+
CS_ASSERT(doing_mem(MI))
317333

318334
MCOperand *Op = MCInst_getOperand(MI, OpNum);
319335
Mips_get_detail_op(MI, 0)->type = MIPS_OP_MEM;
@@ -324,31 +340,28 @@ static void Mips_set_detail_op_mem_nanomips(MCInst *MI, unsigned OpNum)
324340

325341
static void Mips_set_detail_op_reglist(MCInst *MI, unsigned OpNum, bool isNanoMips)
326342
{
327-
if (!detail_is_set(MI))
328-
return;
329-
330343
if (isNanoMips) {
331344
for (unsigned i = OpNum; i < MCInst_getNumOperands(MI); i++) {
332-
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i));
345+
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i), true);
333346
}
334347
return;
335348
}
336349
// -2 because register List is always first operand of instruction
337350
// and it is always followed by memory operand (base + offset).
338351
for (unsigned i = OpNum, e = MCInst_getNumOperands(MI) - 2; i != e; ++i) {
339-
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i));
352+
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i), true);
340353
}
341354
}
342355

343-
static void Mips_set_detail_op_uimm_address(MCInst *MI, unsigned OpNum)
356+
static void Mips_set_detail_op_unsigned_address(MCInst *MI, unsigned OpNum)
344357
{
345358
uint64_t Target = MI->address + (uint64_t)MCInst_getOpVal(MI, OpNum);
346359
Mips_set_detail_op_imm(MI, OpNum, Target);
347360
}
348361

349362
void Mips_add_cs_detail(MCInst *MI, mips_op_group op_group, va_list args)
350363
{
351-
if (!detail_is_set(MI))
364+
if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
352365
return;
353366

354367
unsigned OpNum = va_arg(args, unsigned);
@@ -367,57 +380,57 @@ void Mips_add_cs_detail(MCInst *MI, mips_op_group op_group, va_list args)
367380
case Mips_OP_GROUP_Operand:
368381
return Mips_set_detail_op_operand(MI, OpNum);
369382
case Mips_OP_GROUP_UImm_1_0:
370-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 1, 0);
383+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 1, 0);
371384
case Mips_OP_GROUP_UImm_2_0:
372-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 2, 0);
385+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 2, 0);
373386
case Mips_OP_GROUP_UImm_3_0:
374-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 3, 0);
387+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 3, 0);
375388
case Mips_OP_GROUP_UImm_32_0:
376-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 32, 0);
389+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 32, 0);
377390
case Mips_OP_GROUP_UImm_16_0:
378-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 16, 0);
391+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 16, 0);
379392
case Mips_OP_GROUP_UImm_8_0:
380-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 8, 0);
393+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 8, 0);
381394
case Mips_OP_GROUP_UImm_5_0:
382-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 5, 0);
395+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 0);
383396
case Mips_OP_GROUP_UImm_6_0:
384-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 6, 0);
397+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 0);
385398
case Mips_OP_GROUP_UImm_4_0:
386-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 4, 0);
399+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 4, 0);
387400
case Mips_OP_GROUP_UImm_7_0:
388-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 7, 0);
401+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 7, 0);
389402
case Mips_OP_GROUP_UImm_10_0:
390-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 10, 0);
403+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 10, 0);
391404
case Mips_OP_GROUP_UImm_6_1:
392-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 6, 1);
405+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 1);
393406
case Mips_OP_GROUP_UImm_5_1:
394-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 5, 1);
407+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 1);
395408
case Mips_OP_GROUP_UImm_5_33:
396-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 5, 33);
409+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 33);
397410
case Mips_OP_GROUP_UImm_5_32:
398-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 5, 32);
411+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 32);
399412
case Mips_OP_GROUP_UImm_6_2:
400-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 6, 2);
413+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 2);
401414
case Mips_OP_GROUP_UImm_2_1:
402-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 2, 1);
415+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 2, 1);
403416
case Mips_OP_GROUP_UImm_0_0:
404-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 0, 0);
417+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 0, 0);
405418
case Mips_OP_GROUP_UImm_26_0:
406-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 26, 0);
419+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 26, 0);
407420
case Mips_OP_GROUP_UImm_12_0:
408-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 12, 0);
421+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 12, 0);
409422
case Mips_OP_GROUP_UImm_20_0:
410-
return Mips_set_detail_op_uimm_offset(MI, OpNum, 20, 0);
423+
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 20, 0);
411424
case Mips_OP_GROUP_RegisterList:
412425
return Mips_set_detail_op_reglist(MI, OpNum, false);
413426
case Mips_OP_GROUP_NanoMipsRegisterList:
414427
return Mips_set_detail_op_reglist(MI, OpNum, true);
415428
case Mips_OP_GROUP_PCRel:
416429
/* fall-thru */
417430
case Mips_OP_GROUP_Hi20PCRel:
418-
return Mips_set_detail_op_uimm_address(MI, OpNum);
431+
return Mips_set_detail_op_unsigned_address(MI, OpNum);
419432
case Mips_OP_GROUP_Hi20:
420-
return Mips_set_detail_op_uimm(MI, OpNum);
433+
return Mips_set_detail_op_unsigned(MI, OpNum);
421434
}
422435
}
423436

arch/Mips/MipsMapping.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "../../include/capstone/capstone.h"
88
#include "../../utils.h"
9+
#include "../../Mapping.h"
910

1011
typedef enum {
1112
#include "MipsGenCSOpGroup.inc"
@@ -43,7 +44,7 @@ void Mips_add_cs_detail(MCInst *MI, mips_op_group op_group, va_list args);
4344

4445
static inline void add_cs_detail(MCInst *MI, mips_op_group op_group, ...)
4546
{
46-
if (!MI->flat_insn->detail)
47+
if (!detail_is_set(MI))
4748
return;
4849
va_list args;
4950
va_start(args, op_group);
@@ -53,7 +54,7 @@ static inline void add_cs_detail(MCInst *MI, mips_op_group op_group, ...)
5354

5455
static inline void set_mem_access(MCInst *MI, bool status)
5556
{
56-
if (!MI->flat_insn->detail)
57+
if (!detail_is_set(MI))
5758
return;
5859
Mips_set_mem_access(MI, status);
5960
}

0 commit comments

Comments
 (0)