Coverage Report

Created: 2026-12-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/M680X/M680XInstPrinter.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* M680X Backend by Wolfgang Schwotzer <wolfgang.schwotzer@gmx.net> 2017 */
3
4
#ifdef CAPSTONE_HAS_M680X
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <capstone/platform.h>
9
10
#include "../../cs_priv.h"
11
#include "../../MCInst.h"
12
#include "../../SStream.h"
13
#include "../../MCRegisterInfo.h"
14
#include "../../Mapping.h"
15
#include "../../MCInstPrinter.h"
16
#include "../../utils.h"
17
#include "M680XInstPrinter.h"
18
#include "M680XDisassembler.h"
19
#include "M680XDisassemblerInternals.h"
20
21
#ifndef CAPSTONE_DIET
22
static const char s_reg_names[][10] = {
23
  "<invalid>", "a",     "b",     "e", "f",  "0",  "d",  "w",
24
  "cc",      "dp",    "md",    "hx",  "h",  "x",  "y",  "s",
25
  "u",       "v",     "q",     "pc",  "spc",  "tmp2", "tmp3", "tmp1",
26
  "tmp2h",     "tmp2l", "tmp3h", "tmp3l", "ccrh", "ccrw", "xh", "xl",
27
  "yh",      "yl",    "sph",   "spl",
28
};
29
30
static const char s_instruction_names[][6] = {
31
  "invld", "aba",   "abx",   "aby",   "adc",   "adca",  "adcb",  "adcd",
32
  "adcr",  "add",   "adda",  "addb",  "addd",  "adde",  "addf",  "addr",
33
  "addw",  "addx",  "addy",  "aded",  "adex",  "adey",  "aim",   "ais",
34
  "aix",   "and",   "anda",  "andb",  "andx",  "andy",  "andcc", "andd",
35
  "andr",  "asl",   "asla",  "aslb",  "asld",  "aslw",  "aslx",  "asly",
36
  "asr",   "asra",  "asrb",  "asrd",  "asrw",  "asrx",  "asry",  "band",
37
  "bcc",   "bclr",  "bcs",   "beor",  "beq",   "bge",   "bgnd",  "bgt",
38
  "bhcc",  "bhcs",  "bhi",   "biand", "bieor", "bih",   "bil",   "bior",
39
  "bit",   "bita",  "bitb",  "bitd",  "bitmd", "bitx",  "bity",  "ble",
40
  "bls",   "blt",   "bmc",   "bmi",   "bms",   "bne",   "bor",   "bpl",
41
  "brclr", "brset", "bra",   "brn",   "bset",  "bsr",   "btas",  "bvc",
42
  "bvs",   "call",  "cba",   "cbeq",  "cbeqa", "cbeqx", "clc",   "cli",
43
  "clr",   "clra",  "clrb",  "clrd",  "clre",  "clrf",  "clrh",  "clrw",
44
  "clrx",  "clry",  "clv",   "cmp",   "cmpa",  "cmpb",  "cmpd",  "cmpe",
45
  "cmpf",  "cmpr",  "cmps",  "cmpu",  "cmpw",  "cmpx",  "cmpy",  "com",
46
  "coma",  "comb",  "comd",  "come",  "comf",  "comw",  "comx",  "comy",
47
  "cpd",   "cped",  "cpes",  "cpex",  "cpey",  "cphx",  "cps",   "cpx",
48
  "cpy",   "cwai",  "daa",   "dbeq",  "dbne",  "dbnz",  "dbnza", "dbnzx",
49
  "dec",   "deca",  "decb",  "decd",  "dece",  "decf",  "decw",  "decx",
50
  "decy",  "des",   "dex",   "dey",   "div",   "divd",  "divq",  "ediv",
51
  "edivs", "eim",   "emacs", "emaxd", "emaxm", "emind", "eminm", "emul",
52
  "emuls", "eor",   "eora",  "eorb",  "eord",  "eorr",  "eorx",  "eory",
53
  "etbl",  "exg",   "fdiv",  "gldaa", "gldab", "gldd",  "glds",  "gldx",
54
  "gldy",  "gstaa", "gstab", "gstd",  "gsts",  "gstx",  "gsty",  "ibeq",
55
  "ibne",  "idiv",  "idivs", "illgl", "inc",   "inca",  "incb",  "incd",
56
  "ince",  "incf",  "incw",  "incx",  "incy",  "ins",   "inx",   "iny",
57
  "jmp",   "jsr",   "lbcc",  "lbcs",  "lbeq",  "lbge",  "lbgt",  "lbhi",
58
  "lble",  "lbls",  "lblt",  "lbmi",  "lbne",  "lbpl",  "lbra",  "lbrn",
59
  "lbsr",  "lbvc",  "lbvs",  "lda",   "ldaa",  "ldab",  "ldb",   "ldbt",
60
  "ldd",   "lde",   "ldf",   "ldhx",  "ldmd",  "ldq",   "lds",   "ldu",
61
  "ldw",   "ldx",   "ldy",   "leas",  "leau",  "leax",  "leay",  "lsl",
62
  "lsla",  "lslb",  "lsld",  "lslx",  "lsr",   "lsra",  "lsrb",  "lsrd",
63
  "lsrw",  "lsrx",  "lsry",  "maxa",  "maxm",  "mem",   "mina",  "minm",
64
  "mov",   "movb",  "movw",  "mul",   "muld",  "neg",   "nega",  "negb",
65
  "negd",  "negw",  "negx",  "negy",  "nop",   "nsa",   "oim",   "ora",
66
  "oraa",  "orab",  "orb",   "orcc",  "ord",   "orr",   "orx",   "ory",
67
  "psha",  "pshb",  "pshc",  "pshcw", "pshd",  "pshh",  "pshs",  "pshsw",
68
  "pshu",  "pshuw", "pshx",  "pshy",  "pula",  "pulb",  "pulc",  "pulcw",
69
  "puld",  "pulh",  "puls",  "pulsw", "pulu",  "puluw", "pulx",  "puly",
70
  "rev",   "revw",  "rol",   "rola",  "rolb",  "rold",  "rolw",  "rolx",
71
  "roly",  "ror",   "rora",  "rorb",  "rord",  "rorw",  "rorx",  "rory",
72
  "rsp",   "rtc",   "rti",   "rts",   "sba",   "sbc",   "sbca",  "sbcb",
73
  "sbcd",  "sbcr",  "sbed",  "sbex",  "sbey",  "sec",   "sei",   "sev",
74
  "sex",   "sexw",  "sha",   "sla",   "slp",   "sta",   "staa",  "stab",
75
  "stb",   "stbt",  "std",   "ste",   "stf",   "stop",  "sthx",  "stq",
76
  "sts",   "stu",   "stw",   "stx",   "sty",   "sub",   "suba",  "subb",
77
  "subd",  "sube",  "subf",  "subr",  "subw",  "subx",  "suby",  "swi",
78
  "swi2",  "swi3",  "sync",  "sys",   "tab",   "tap",   "tax",   "tba",
79
  "tbeq",  "tbl",   "tbne",  "test",  "tfm",   "tfr",   "tim",   "tpa",
80
  "trap",  "tst",   "tsta",  "tstb",  "tstd",  "tste",  "tstf",  "tstw",
81
  "tstx",  "tsty",  "tsx",   "tsy",   "txa",   "txs",   "tys",   "wai",
82
  "wait",  "wav",   "wavr",  "xgdx",  "xgdy",
83
};
84
85
static const name_map s_group_names[] = {
86
  { M680X_GRP_INVALID, "<invalid>" },
87
  { M680X_GRP_JUMP, "jump" },
88
  { M680X_GRP_CALL, "call" },
89
  { M680X_GRP_RET, "return" },
90
  { M680X_GRP_INT, "interrupt" },
91
  { M680X_GRP_IRET, "interrupt_return" },
92
  { M680X_GRP_PRIV, "privileged" },
93
  { M680X_GRP_BRAREL, "branch_relative" },
94
};
95
#endif
96
97
static void printRegName(cs_struct *handle, SStream *OS, unsigned int reg)
98
161k
{
99
161k
#ifndef CAPSTONE_DIET
100
161k
  SStream_concat0(OS, handle->reg_name((csh)handle, reg));
101
161k
#endif
102
161k
}
103
104
static void printInstructionName(cs_struct *handle, SStream *OS,
105
         unsigned int insn)
106
382k
{
107
382k
#ifndef CAPSTONE_DIET
108
382k
  SStream_concat0(OS, handle->insn_name((csh)handle, insn));
109
382k
#endif
110
382k
}
111
112
static uint32_t get_unsigned(int32_t value, int byte_size)
113
0
{
114
0
  switch (byte_size) {
115
0
  case 1:
116
0
    return (uint32_t)(value & 0xff);
117
118
0
  case 2:
119
0
    return (uint32_t)(value & 0xffff);
120
121
0
  default:
122
0
  case 4:
123
0
    return (uint32_t)value;
124
0
  }
125
0
}
126
127
static void printIncDec(bool isPost, SStream *O, m680x_info *info,
128
      cs_m680x_op *op)
129
223k
{
130
223k
  static const char s_inc_dec[][3] = { "--", "-", "", "+", "++" };
131
132
223k
  if (!op->idx.inc_dec)
133
172k
    return;
134
135
50.8k
  if ((!isPost && !(op->idx.flags & M680X_IDX_POST_INC_DEC)) ||
136
37.8k
      (isPost && (op->idx.flags & M680X_IDX_POST_INC_DEC))) {
137
25.4k
    const char *prePostfix = "";
138
139
25.4k
    if (info->cpu_type == M680X_CPU_TYPE_CPU12)
140
9.89k
      prePostfix = (op->idx.inc_dec < 0) ? "-" : "+";
141
15.5k
    else if (op->idx.inc_dec >= -2 && (op->idx.inc_dec <= 2)) {
142
12.1k
      prePostfix = (char *)s_inc_dec[op->idx.inc_dec + 2];
143
12.1k
    }
144
145
25.4k
    SStream_concat0(O, prePostfix);
146
25.4k
  }
147
50.8k
}
148
149
static void printOperand(MCInst *MI, SStream *O, m680x_info *info,
150
       cs_m680x_op *op)
151
346k
{
152
346k
  switch (op->type) {
153
38.4k
  case M680X_OP_REGISTER:
154
38.4k
    printRegName(MI->csh, O, op->reg);
155
38.4k
    break;
156
157
8.93k
  case M680X_OP_CONSTANT:
158
8.93k
    SStream_concat(O, "%u", op->const_val);
159
8.93k
    break;
160
161
42.7k
  case M680X_OP_IMMEDIATE:
162
42.7k
    if (MI->csh->imm_unsigned)
163
0
      SStream_concat(O, "#%u",
164
0
               get_unsigned(op->imm, op->size));
165
42.7k
    else
166
42.7k
      SStream_concat(O, "#%d", op->imm);
167
168
42.7k
    break;
169
170
111k
  case M680X_OP_INDEXED:
171
111k
    if (op->idx.flags & M680X_IDX_INDIRECT)
172
12.3k
      SStream_concat0(O, "[");
173
174
111k
    if (op->idx.offset_reg != M680X_REG_INVALID)
175
11.2k
      printRegName(MI->csh, O, op->idx.offset_reg);
176
100k
    else if (op->idx.offset_bits > 0) {
177
62.9k
      if (op->idx.base_reg == M680X_REG_PC)
178
8.24k
        SStream_concat(O, "$%04x", op->idx.offset_addr);
179
54.7k
      else
180
54.7k
        SStream_concat(O, "%d", op->idx.offset);
181
62.9k
    } else if (op->idx.inc_dec != 0 &&
182
24.2k
         info->cpu_type == M680X_CPU_TYPE_CPU12)
183
9.89k
      SStream_concat(O, "%d", abs(op->idx.inc_dec));
184
185
111k
    if (!(op->idx.flags & M680X_IDX_NO_COMMA))
186
107k
      SStream_concat(O, ", ");
187
188
111k
    printIncDec(false, O, info, op);
189
190
111k
    printRegName(MI->csh, O, op->idx.base_reg);
191
192
111k
    if (op->idx.base_reg == M680X_REG_PC &&
193
10.2k
        (op->idx.offset_bits > 0))
194
8.24k
      SStream_concat(O, "r");
195
196
111k
    printIncDec(true, O, info, op);
197
198
111k
    if (op->idx.flags & M680X_IDX_INDIRECT)
199
12.3k
      SStream_concat(O, "]");
200
201
111k
    break;
202
203
33.3k
  case M680X_OP_RELATIVE:
204
33.3k
    SStream_concat(O, "$%04x", op->rel.address);
205
33.3k
    break;
206
207
58.6k
  case M680X_OP_DIRECT:
208
58.6k
    SStream_concat(O, "$%02x", op->direct_addr);
209
58.6k
    break;
210
211
52.7k
  case M680X_OP_EXTENDED:
212
52.7k
    if (op->ext.indirect)
213
625
      SStream_concat(O, "[$%04x]", op->ext.address);
214
52.1k
    else {
215
52.1k
      if (op->ext.address < 256) {
216
2.03k
        SStream_concat(O, ">$%04x", op->ext.address);
217
50.1k
      } else {
218
50.1k
        SStream_concat(O, "$%04x", op->ext.address);
219
50.1k
      }
220
52.1k
    }
221
222
52.7k
    break;
223
224
0
  default:
225
0
    SStream_concat0(O, "<invalid_operand>");
226
0
    break;
227
346k
  }
228
346k
}
229
230
static const char *getDelimiter(m680x_info *info, cs_m680x *m680x)
231
424k
{
232
424k
  bool indexed = false;
233
424k
  int count = 0;
234
424k
  int i;
235
236
424k
  if (info->insn == M680X_INS_TFM)
237
1.76k
    return ", ";
238
239
423k
  if (m680x->op_count > 1) {
240
635k
    for (i = 0; i < m680x->op_count; ++i) {
241
433k
      if (m680x->operands[i].type == M680X_OP_INDEXED)
242
81.8k
        indexed = true;
243
244
433k
      if (m680x->operands[i].type != M680X_OP_REGISTER)
245
225k
        count++;
246
433k
    }
247
202k
  }
248
249
423k
  return (indexed && (count >= 1)) ? "; " : ", ";
250
424k
};
251
252
void M680X_printInst(MCInst *MI, SStream *O, void *PrinterInfo)
253
424k
{
254
424k
  m680x_info *info = (m680x_info *)PrinterInfo;
255
424k
  cs_m680x *m680x = &info->m680x;
256
424k
  cs_detail *detail = MI->flat_insn->detail;
257
424k
  int suppress_operands = 0;
258
424k
  const char *delimiter = getDelimiter(info, m680x);
259
424k
  int i;
260
261
424k
  if (detail != NULL)
262
424k
    memcpy(&detail->m680x, m680x, sizeof(cs_m680x));
263
264
424k
  if (info->insn == M680X_INS_INVLD || info->insn == M680X_INS_ILLGL) {
265
41.8k
    if (m680x->op_count)
266
41.8k
      SStream_concat(O, "fcb $%02x", m680x->operands[0].imm);
267
0
    else
268
0
      SStream_concat0(O, "fcb $<unknown>");
269
270
41.8k
    return;
271
41.8k
  }
272
273
382k
  printInstructionName(MI->csh, O, info->insn);
274
382k
  SStream_concat0(O, " ");
275
276
382k
  if ((m680x->flags & M680X_FIRST_OP_IN_MNEM) != 0)
277
206k
    suppress_operands++;
278
279
382k
  if ((m680x->flags & M680X_SECOND_OP_IN_MNEM) != 0)
280
4.82k
    suppress_operands++;
281
282
941k
  for (i = 0; i < m680x->op_count; ++i) {
283
558k
    if (i >= suppress_operands) {
284
346k
      printOperand(MI, O, info, &m680x->operands[i]);
285
286
346k
      if ((i + 1) != m680x->op_count)
287
68.2k
        SStream_concat0(O, delimiter);
288
346k
    }
289
558k
  }
290
382k
}
291
292
const char *M680X_reg_name(csh handle, unsigned int reg)
293
1.32M
{
294
1.32M
#ifndef CAPSTONE_DIET
295
296
1.32M
  if (reg >= ARR_SIZE(s_reg_names))
297
0
    return NULL;
298
299
1.32M
  return s_reg_names[(int)reg];
300
#else
301
  return NULL;
302
#endif
303
1.32M
}
304
305
const char *M680X_insn_name(csh handle, unsigned int id)
306
807k
{
307
807k
#ifndef CAPSTONE_DIET
308
309
807k
  if (id >= ARR_SIZE(s_instruction_names))
310
0
    return NULL;
311
807k
  else
312
807k
    return s_instruction_names[(int)id];
313
314
#else
315
  return NULL;
316
#endif
317
807k
}
318
319
const char *M680X_group_name(csh handle, unsigned int id)
320
88.9k
{
321
88.9k
#ifndef CAPSTONE_DIET
322
88.9k
  return id2name(s_group_names, ARR_SIZE(s_group_names), id);
323
#else
324
  return NULL;
325
#endif
326
88.9k
}
327
328
cs_err M680X_instprinter_init(cs_struct *ud)
329
4.60k
{
330
4.60k
#ifndef CAPSTONE_DIET
331
332
4.60k
  if (M680X_REG_ENDING != ARR_SIZE(s_reg_names)) {
333
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(s_reg_names));
334
0
    return CS_ERR_MODE;
335
0
  }
336
337
4.60k
  if (M680X_INS_ENDING != ARR_SIZE(s_instruction_names)) {
338
0
    CS_ASSERT(M680X_INS_ENDING == ARR_SIZE(s_instruction_names));
339
0
    return CS_ERR_MODE;
340
0
  }
341
342
4.60k
  if (M680X_GRP_ENDING != ARR_SIZE(s_group_names)) {
343
0
    CS_ASSERT(M680X_GRP_ENDING == ARR_SIZE(s_group_names));
344
0
    return CS_ERR_MODE;
345
0
  }
346
347
4.60k
#endif
348
349
4.60k
  return CS_ERR_OK;
350
4.60k
}
351
352
#endif