Coverage Report

Created: 2026-12-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/M68K/M68KDisassembler.h
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* M68K Backend by Daniel Collin <daniel@collin.com> 2015-2016 */
3
4
#ifndef CS_M68KDISASSEMBLER_H
5
#define CS_M68KDISASSEMBLER_H
6
7
#include "../../MCInst.h"
8
9
/* ======================================================================== */
10
/* ============================ GENERAL DEFINES =========================== */
11
/* ======================================================================== */
12
13
/* Bit Isolation Functions */
14
2.43k
#define BIT_0(A) ((A) & 0x00000001)
15
#define BIT_1(A) ((A) & 0x00000002)
16
#define BIT_2(A) ((A) & 0x00000004)
17
0
#define BIT_3(A) ((A) & 0x00000008)
18
#define BIT_4(A) ((A) & 0x00000010)
19
1.97k
#define BIT_5(A) ((A) & 0x00000020)
20
6.55k
#define BIT_6(A) ((A) & 0x00000040)
21
6.55k
#define BIT_7(A) ((A) & 0x00000080)
22
17.3k
#define BIT_8(A) ((A) & 0x00000100)
23
#define BIT_9(A) ((A) & 0x00000200)
24
2.27k
#define BIT_A(A) ((A) & 0x00000400)
25
19.8k
#define BIT_B(A) ((A) & 0x00000800)
26
#define BIT_C(A) ((A) & 0x00001000)
27
#define BIT_D(A) ((A) & 0x00002000)
28
#define BIT_E(A) ((A) & 0x00004000)
29
20.3k
#define BIT_F(A) ((A) & 0x00008000)
30
#define BIT_10(A) ((A) & 0x00010000)
31
#define BIT_11(A) ((A) & 0x00020000)
32
#define BIT_12(A) ((A) & 0x00040000)
33
#define BIT_13(A) ((A) & 0x00080000)
34
#define BIT_14(A) ((A) & 0x00100000)
35
#define BIT_15(A) ((A) & 0x00200000)
36
#define BIT_16(A) ((A) & 0x00400000)
37
#define BIT_17(A) ((A) & 0x00800000)
38
#define BIT_18(A) ((A) & 0x01000000)
39
#define BIT_19(A) ((A) & 0x02000000)
40
#define BIT_1A(A) ((A) & 0x04000000)
41
#define BIT_1B(A) ((A) & 0x08000000)
42
#define BIT_1C(A) ((A) & 0x10000000)
43
#define BIT_1D(A) ((A) & 0x20000000)
44
#define BIT_1E(A) ((A) & 0x40000000)
45
1.34k
#define BIT_1F(A) ((A) & 0x80000000)
46
47
/* These are the M68K feature masks understood by this disassembler. */
48
#define M68000_ONLY CS_MODE_M68K_000
49
50
#define M68010_ONLY CS_MODE_M68K_010
51
261k
#define M68010_LESS (CS_MODE_M68K_000 | CS_MODE_M68K_010)
52
#define M68010_PLUS \
53
  (CS_MODE_M68K_010 | CS_MODE_M68K_020 | CS_MODE_M68K_030 | \
54
   CS_MODE_M68K_040 | CS_MODE_M68K_060)
55
56
#define M68020_ONLY CS_MODE_M68K_020
57
#define M68020_LESS (CS_MODE_M68K_010 | CS_MODE_M68K_020)
58
#define M68020_PLUS \
59
  (CS_MODE_M68K_020 | CS_MODE_M68K_030 | CS_MODE_M68K_040 | \
60
   CS_MODE_M68K_060)
61
62
#define M68030_ONLY CS_MODE_M68K_030
63
#define M68030_LESS (CS_MODE_M68K_010 | CS_MODE_M68K_020 | CS_MODE_M68K_030)
64
#define M68030_PLUS (CS_MODE_M68K_030 | CS_MODE_M68K_040 | CS_MODE_M68K_060)
65
66
602
#define M68040_PLUS (CS_MODE_M68K_040 | CS_MODE_M68K_060)
67
68
typedef uint32_t m68k_feature_mask;
69
70
/* Extension word formats */
71
#define EXT_8BIT_DISPLACEMENT(A) ((A) & 0xff)
72
17.3k
#define EXT_FULL(A) BIT_8(A)
73
#define EXT_EFFECTIVE_ZERO(A) (((A) & 0xe4) == 0xc4 || ((A) & 0xe2) == 0xc0)
74
6.55k
#define EXT_BASE_REGISTER_PRESENT(A) (!BIT_7(A))
75
6.55k
#define EXT_INDEX_REGISTER_PRESENT(A) (!BIT_6(A))
76
14.7k
#define EXT_INDEX_REGISTER(A) (((A) >> 12) & 7)
77
#define EXT_INDEX_PRE_POST(A) (EXT_INDEX_REGISTER_PRESENT(A) && (A) & 3)
78
#define EXT_INDEX_PRE(A) \
79
  (EXT_INDEX_REGISTER_PRESENT(A) && ((A) & 7) < 4 && ((A) & 7) != 0)
80
#define EXT_INDEX_POST(A) (EXT_INDEX_REGISTER_PRESENT(A) && ((A) & 7) > 4)
81
23.5k
#define EXT_INDEX_SCALE(A) (((A) >> 9) & 3)
82
14.7k
#define EXT_INDEX_LONG(A) BIT_B(A)
83
14.7k
#define EXT_INDEX_AR(A) BIT_F(A)
84
19.6k
#define EXT_BASE_DISPLACEMENT_PRESENT(A) (((A) & 0x30) > 0x10)
85
#define EXT_BASE_DISPLACEMENT_WORD(A) (((A) & 0x30) == 0x20)
86
5.14k
#define EXT_BASE_DISPLACEMENT_LONG(A) (((A) & 0x30) == 0x30)
87
/* Outer displacement is present when I/IS[1:0] (bits 1-0) is 2 (word) or 3 (long).
88
 * This applies regardless of the IS bit (bit 6): when index is suppressed,
89
 * I/IS values 5-7 mirror 1-3 (just indirect instead of postindexed).
90
 * The old check ((A) & 0x47) < 0x44 incorrectly excluded IS=1 cases
91
 * (I/IS=6,7) which DO have outer displacements per the M68K spec.
92
 */
93
19.6k
#define EXT_OUTER_DISPLACEMENT_PRESENT(A) (((A) & 3) > 1)
94
#define EXT_OUTER_DISPLACEMENT_WORD(A) (((A) & 3) == 2)
95
3.96k
#define EXT_OUTER_DISPLACEMENT_LONG(A) (((A) & 3) == 3)
96
97
#define IS_BITSET(val, b) ((val) & (1 << (b)))
98
63.4k
#define BITFIELD_MASK(sb, eb) (((1 << ((sb) + 1)) - 1) & (~((1 << (eb)) - 1)))
99
63.4k
#define BITFIELD(val, sb, eb) ((BITFIELD_MASK(sb, eb) & (val)) >> (eb))
100
101
/* Bitfield offset/width encoding.
102
 * Public decode macros (M68K_BF_*) live in <capstone/m68k.h>.
103
 * Internal aliases kept for brevity within arch code. */
104
#define M68K_BITFIELD_REG_FLAG M68K_BF_REG_FLAG
105
#define M68K_BITFIELD_IS_REG(v) M68K_BF_IS_REG(v)
106
#define M68K_BITFIELD_REG_NUM(v) M68K_BF_REG_NUM(v)
107
2.22k
#define M68K_BITFIELD_ENCODE_REG(regnum) (((regnum) & 7) | M68K_BF_REG_FLAG)
108
109
/* ── Coprocessor ID (CpID) ───────────────────────────────────────────
110
 * Bits 11:9 of the F-line instruction word select the coprocessor.   */
111
59.8k
#define M68K_CPID(info) (((info)->ir >> 9) & 7)
112
113
51.2k
#define M68K_CPID_MMU 0 /* PMMU (68030/68851)                     */
114
62.4k
#define M68K_CPID_FPU 1 /* FPU  (68881/68882/internal)            */
115
1.85k
#define M68K_CPID_CACHE 2 /* Cache ops -- cinvl/cpushl on 68040+    */
116
117
/* ── IR bit-field helpers ────────────────────────────────────────────
118
 * Extract commonly-used fields from the first instruction word.      */
119
120
/* 4-bit condition selector used by Bcc/DBcc/Scc/TRAPcc. */
121
11.8k
#define M68K_IR_CONDITION_NIBBLE(info) (((info)->ir >> 8) & 0xf)
122
0
#define M68K_CONDITION_FALSE 1
123
124
/* cinv/cpush: select cpush(1) vs cinv(0) -- bit 5 of IR. */
125
200
#define M68K_IR_IS_CPUSH(info) (((info)->ir >> 5) & 1)
126
127
/* cinv/cpush: cache scope -- bits 4:3 of IR (0=invalid,1=line,2=page,3=all). */
128
1.19k
#define M68K_IR_CACHE_SCOPE(info) (((info)->ir >> 3) & 3)
129
130
/* cinv/cpush: cache selector -- bits 7:6 of IR (DC/IC/BC). */
131
890
#define M68K_IR_CACHE_SEL(info) (((info)->ir >> 6) & 3)
132
133
/* ── FPU extension-word bit-field helpers ────────────────────────────
134
 * The FPU command word is the 16-bit extension following the F-line. */
135
136
/* R/M bit (bit 14): 1 = effective-address operand, 0 = FP-register operand. */
137
21.8k
#define M68K_FEXT_RM(ext) (((ext) >> 14) & 1)
138
139
/* Type / command class (bits 15:13). */
140
22.3k
#define M68K_FEXT_TYPE(ext) (((ext) >> 13) & 7)
141
43.7k
#define M68K_FEXT_TYPE_FP_REG 0x0
142
22.3k
#define M68K_FEXT_TYPE_RESERVED 0x1
143
15.9k
#define M68K_FEXT_TYPE_FP_EA 0x2
144
21.8k
#define M68K_FEXT_TYPE_FMOVE_TO_EA 0x3
145
499
#define M68K_FEXT_TYPE_FPCR_FROM_EA 0x4
146
1.05k
#define M68K_FEXT_TYPE_FPCR_TO_EA 0x5
147
2.01k
#define M68K_FEXT_TYPE_FMOVEM_FROM_EA 0x6
148
4.56k
#define M68K_FEXT_TYPE_FMOVEM_TO_EA 0x7
149
150
/* Source/format specifier (bits 12:10). */
151
21.8k
#define M68K_FEXT_SRC(ext) (((ext) >> 10) & 7)
152
153
/* Destination FP register (bits 9:7). */
154
22.0k
#define M68K_FEXT_DST(ext) (((ext) >> 7) & 7)
155
156
/* Opmode (bits 6:0) -- FPU operation selector. */
157
22.0k
#define M68K_FEXT_OPMODE(ext) ((ext) & 0x7f)
158
159
/* Single/double precision flag (bit 6) -- 68040+ only. */
160
14.1k
#define M68K_FEXT_SD_FLAG(ext) (((ext) >> 6) & 1)
161
162
/* FMOVECR signature: bits 15:10 == 0x17 (010111b). */
163
40.3k
#define M68K_FEXT_IS_FMOVECR(ext) (BITFIELD((ext), 15, 10) == 0x17)
164
165
/* Register-select field for FMOVE to/from FPCR/FPSR/FPIAR (bits 12:10). */
166
1.05k
#define M68K_FEXT_REGSEL(ext) (((ext) >> 10) & 7)
167
168
/* Direction bit for FMOVE FPCR (bit 13): 0 = ea->fpcr, 1 = fpcr->ea. */
169
10.1k
#define M68K_FEXT_DIR(ext) (((ext) >> 13) & 1)
170
171
/* FMOVEM register-list encoding.
172
 *
173
 * Bits 12:11 select static/dynamic and predecrement versus
174
 * postincrement-or-control forms. A static list occupies bits 7:0; a dynamic
175
 * list has the reserved-bit format 0rrr0000, with Dn in bits 6:4.
176
 *
177
 * Reference: Motorola MC68881/MC68882 Floating-Point Coprocessor User's
178
 * Manual, first edition (1987), section 4.7.1.6, Table 4-18, pages 4-126
179
 * through 4-128:
180
 * https://www.bitsavers.org/components/motorola/68000/68020/MC68881_MC68882_Floating-Point_Coprocessor_Users_Manual_1ed_1987.pdf
181
 */
182
typedef enum {
183
  M68K_FMOVEM_MODE_STATIC_PREDECREMENT = 0,
184
  M68K_FMOVEM_MODE_DYNAMIC_PREDECREMENT = 1,
185
  M68K_FMOVEM_MODE_STATIC_POSTINCREMENT_OR_CONTROL = 2,
186
  M68K_FMOVEM_MODE_DYNAMIC_POSTINCREMENT_OR_CONTROL = 3,
187
} m68k_fmovem_mode;
188
189
static inline m68k_fmovem_mode m68k_fmovem_get_mode(uint32_t extension)
190
9.12k
{
191
9.12k
  return (m68k_fmovem_mode)BITFIELD(extension, 12, 11);
192
9.12k
}
Unexecuted instantiation: M68KModule.c:m68k_fmovem_get_mode
M68KDisassembler.c:m68k_fmovem_get_mode
Line
Count
Source
190
9.12k
{
191
9.12k
  return (m68k_fmovem_mode)BITFIELD(extension, 12, 11);
192
9.12k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_fmovem_get_mode
193
194
static inline uint32_t m68k_fmovem_register_list(uint32_t extension)
195
4.56k
{
196
4.56k
  return BITFIELD(extension, 7, 0);
197
4.56k
}
Unexecuted instantiation: M68KModule.c:m68k_fmovem_register_list
M68KDisassembler.c:m68k_fmovem_register_list
Line
Count
Source
195
4.56k
{
196
4.56k
  return BITFIELD(extension, 7, 0);
197
4.56k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_fmovem_register_list
198
199
static inline bool
200
m68k_fmovem_dynamic_reserved_bits_are_zero(uint32_t extension)
201
1.59k
{
202
1.59k
  return BITFIELD(extension, 7, 7) == 0 && BITFIELD(extension, 3, 0) == 0;
203
1.59k
}
Unexecuted instantiation: M68KModule.c:m68k_fmovem_dynamic_reserved_bits_are_zero
M68KDisassembler.c:m68k_fmovem_dynamic_reserved_bits_are_zero
Line
Count
Source
201
1.59k
{
202
1.59k
  return BITFIELD(extension, 7, 7) == 0 && BITFIELD(extension, 3, 0) == 0;
203
1.59k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_fmovem_dynamic_reserved_bits_are_zero
204
205
static inline uint32_t m68k_fmovem_dynamic_register(uint32_t extension)
206
1.58k
{
207
1.58k
  return BITFIELD(extension, 6, 4);
208
1.58k
}
Unexecuted instantiation: M68KModule.c:m68k_fmovem_dynamic_register
M68KDisassembler.c:m68k_fmovem_dynamic_register
Line
Count
Source
206
1.58k
{
207
1.58k
  return BITFIELD(extension, 6, 4);
208
1.58k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_fmovem_dynamic_register
209
210
/* ── FPU condition-code mask ─────────────────────────────────────────
211
 * The FPU defines predicates 0xxxxx; 1xxxxx encodings are reserved
212
 * aliases that evaluate identically. Use the low five bits to index the
213
 * contiguous FBcc/FDBcc/FScc/FTRAPcc opcode ranges. See the manual
214
 * reference above.                                                   */
215
3.34k
#define M68K_FPU_CONDITION_INDEX_MASK 0x1f
216
217
static inline uint32_t m68k_fpu_condition_index(uint32_t word)
218
3.34k
{
219
3.34k
  return word & M68K_FPU_CONDITION_INDEX_MASK;
220
3.34k
}
Unexecuted instantiation: M68KModule.c:m68k_fpu_condition_index
M68KDisassembler.c:m68k_fpu_condition_index
Line
Count
Source
218
3.34k
{
219
3.34k
  return word & M68K_FPU_CONDITION_INDEX_MASK;
220
3.34k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_fpu_condition_index
221
222
/* ── FPU source-format constants (bits 12:10 of ext word) ───────────*/
223
365
#define M68K_FPSRC_LONG 0x00 /* .l  -- 32-bit integer            */
224
710
#define M68K_FPSRC_SINGLE 0x01 /* .s  -- 32-bit IEEE single        */
225
3.23k
#define M68K_FPSRC_EXTENDED 0x02 /* .x  -- 96-bit extended real      */
226
2.27k
#define M68K_FPSRC_PACKED 0x03 /* .p  -- 96-bit packed decimal     */
227
83
#define M68K_FPSRC_WORD 0x04 /* .w  -- 16-bit integer            */
228
1.74k
#define M68K_FPSRC_DOUBLE 0x05 /* .d  -- 64-bit IEEE double        */
229
628
#define M68K_FPSRC_BYTE 0x06 /* .b  -- 8-bit integer             */
230
231
/* FMOVE register-to-memory packed-decimal destination encodings. */
232
4.69k
#define M68K_FPDST_PACKED_STATIC 0x03
233
1.13k
#define M68K_FPDST_PACKED_DYNAMIC 0x07
234
235
/* ── Feature guard macros ────────────────────────────────────────────
236
 * These reference the `info` parameter available at each call site.
237
 * They early-return from the calling function on guard mismatch.     */
238
239
#define LIMIT_FEATURE(info, FEATURES) \
240
82.7k
  do { \
241
82.7k
    if (!m68k_has_feature(info, FEATURES)) { \
242
31.6k
      d68000_invalid(info); \
243
31.6k
      return; \
244
31.6k
    } \
245
82.7k
  } while (0)
246
247
/* Like LIMIT_FEATURE but also reverses the instruction word consumption,
248
 * so the invalid instruction produces size=0 (not decoded) instead of size=2.
249
 * Use for handlers that replace d68000_invalid in the dispatch table. */
250
#define LIMIT_FEATURE_UNDECODED(info, FEATURES) \
251
3
  do { \
252
3
    if (!m68k_has_feature(info, FEATURES)) { \
253
3
      info->pc -= 2; \
254
3
      d68000_invalid(info); \
255
3
      return; \
256
3
    } \
257
3
  } while (0)
258
259
/* Like LIMIT_FEATURE but also rejects a feature subset.  CPU32 implies 68020
260
 * but lacks some 68020 instructions (CAS, CAS2, CHK.L, PACK, UNPK). */
261
#define LIMIT_FEATURE_EXCLUDING(info, FEATURES, EXCLUDED_FEATURES) \
262
12.0k
  do { \
263
12.0k
    if (!m68k_has_feature(info, FEATURES) || \
264
12.0k
        m68k_has_feature(info, EXCLUDED_FEATURES)) { \
265
4.47k
      d68000_invalid(info); \
266
4.47k
      return; \
267
4.47k
    } \
268
12.0k
  } while (0)
269
270
/* CPU32 lacks the 68020 coprocessor interface. */
271
#define REQUIRE_CPID_FPU(info) \
272
31.2k
  do { \
273
31.2k
    if (M68K_CPID(info) != M68K_CPID_FPU || \
274
31.2k
        m68k_has_feature(info, CS_MODE_M68K_CPU32)) { \
275
4.64k
      d68000_invalid(info); \
276
4.64k
      return; \
277
4.64k
    } \
278
31.2k
  } while (0)
279
280
/* ── EA / immediate convenience aliases ─────────────────────────────
281
 * Shorthand wrappers around the sized get_ea_mode_str / get_imm_str
282
 * functions.  These expand at the call site where 'info' is in scope. */
283
284
/* Fake a split interface */
285
#define get_ea_mode_str_8(instruction) get_ea_mode_str(instruction, 0)
286
#define get_ea_mode_str_16(instruction) get_ea_mode_str(instruction, 1)
287
#define get_ea_mode_str_32(instruction) get_ea_mode_str(instruction, 2)
288
289
#define get_imm_str_s8() get_imm_str_s(0)
290
#define get_imm_str_s16() get_imm_str_s(1)
291
#define get_imm_str_s32() get_imm_str_s(2)
292
293
#define get_imm_str_u8() get_imm_str_u(0)
294
#define get_imm_str_u16() get_imm_str_u(1)
295
#define get_imm_str_u32() get_imm_str_u(2)
296
297
/* ── Operand access shorthands ──────────────────────────────────────
298
 * Quick access to the operand array and instruction size via `info`. */
299
#define IOPS(I) (&info->extension.operands[(I)])
300
4.31k
#define ISIZE (info->extension.op_size.cpu_size)
301
302
/* ======================================================================== */
303
/* ============================ INTERNAL TYPES ============================ */
304
/* ======================================================================== */
305
306
/* Private, For internal use only */
307
typedef struct m68k_info {
308
  const uint8_t *code;
309
  size_t code_len;
310
  uint64_t baseAddress;
311
  MCInst *inst;
312
  uint32_t pc; /* program counter */
313
  uint32_t ir; /* instruction register */
314
  m68k_feature_mask features;
315
  uint32_t address_mask; /* Address mask to simulate address lines */
316
  cs_m68k extension;
317
  uint16_t regs_read
318
    [MAX_IMPL_R_REGS]; // list of implicit registers read by this insn
319
  uint8_t regs_read_count; // number of implicit registers read by this insn
320
  uint16_t regs_write
321
    [MAX_IMPL_W_REGS]; // list of implicit registers modified by this insn
322
  uint8_t regs_write_count; // number of implicit registers modified by this insn
323
  uint8_t groups[MAX_NUM_GROUPS];
324
  uint8_t groups_count;
325
} m68k_info;
326
327
static inline bool m68k_has_feature(const m68k_info *info,
328
            m68k_feature_mask features)
329
418k
{
330
418k
  m68k_feature_mask available = info->features;
331
332
418k
  if (available & CS_MODE_M68K_CPU32)
333
0
    available |= CS_MODE_M68K_020;
334
418k
  if (available & CS_MODE_M68K_CF_ISA_A_PLUS)
335
0
    available |= CS_MODE_M68K_CF_ISA_A;
336
418k
  if (available & CS_MODE_M68K_CF_ISA_B)
337
0
    available |= CS_MODE_M68K_CF_ISA_A | CS_MODE_M68K_CF_ISA_A_PLUS;
338
418k
  if (available & CS_MODE_M68K_CF_ISA_C)
339
0
    available |= CS_MODE_M68K_CF_ISA_A;
340
418k
  if (available & CS_MODE_M68K_CF_EMAC)
341
0
    available |= CS_MODE_M68K_CF_MAC;
342
418k
  if (available & CS_MODE_M68K_CF_EMAC_B)
343
0
    available |= CS_MODE_M68K_CF_EMAC | CS_MODE_M68K_CF_MAC;
344
345
418k
  return (available & features) != 0;
346
418k
}
Unexecuted instantiation: M68KModule.c:m68k_has_feature
M68KDisassembler.c:m68k_has_feature
Line
Count
Source
329
418k
{
330
418k
  m68k_feature_mask available = info->features;
331
332
418k
  if (available & CS_MODE_M68K_CPU32)
333
0
    available |= CS_MODE_M68K_020;
334
418k
  if (available & CS_MODE_M68K_CF_ISA_A_PLUS)
335
0
    available |= CS_MODE_M68K_CF_ISA_A;
336
418k
  if (available & CS_MODE_M68K_CF_ISA_B)
337
0
    available |= CS_MODE_M68K_CF_ISA_A | CS_MODE_M68K_CF_ISA_A_PLUS;
338
418k
  if (available & CS_MODE_M68K_CF_ISA_C)
339
0
    available |= CS_MODE_M68K_CF_ISA_A;
340
418k
  if (available & CS_MODE_M68K_CF_EMAC)
341
0
    available |= CS_MODE_M68K_CF_MAC;
342
418k
  if (available & CS_MODE_M68K_CF_EMAC_B)
343
0
    available |= CS_MODE_M68K_CF_EMAC | CS_MODE_M68K_CF_MAC;
344
345
418k
  return (available & features) != 0;
346
418k
}
Unexecuted instantiation: M68KInstPrinter.c:m68k_has_feature
347
348
bool M68K_getInstruction(csh ud, const uint8_t *code, size_t code_len,
349
       MCInst *instr, uint16_t *size, uint64_t address,
350
       void *info);
351
352
#endif