Coverage Report

Created: 2026-03-13 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/SystemZ/SystemZMapping.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Rot127 <unisono@quyllur.org> 2022-2023 */
3
4
#ifdef CAPSTONE_HAS_SYSTEMZ
5
6
#include <stdio.h> // debug
7
#include <string.h>
8
9
#include "../../Mapping.h"
10
#include "../../utils.h"
11
#include "../../cs_simple_types.h"
12
#include <capstone/cs_operand.h>
13
14
#include "SystemZMCTargetDesc.h"
15
#include "SystemZMapping.h"
16
#include "SystemZLinkage.h"
17
18
#ifndef CAPSTONE_DIET
19
20
static const char *const insn_name_maps[] = {
21
#include "SystemZGenCSMappingInsnName.inc"
22
};
23
24
static const name_map insn_alias_mnem_map[] = {
25
#include "SystemZGenCSAliasMnemMap.inc"
26
  { SYSTEMZ_INS_ALIAS_END, NULL },
27
};
28
29
static const map_insn_ops insn_operands[] = {
30
#include "SystemZGenCSMappingInsnOp.inc"
31
};
32
33
#endif
34
35
#define GET_REGINFO_MC_DESC
36
#include "SystemZGenRegisterInfo.inc"
37
38
const insn_map systemz_insns[] = {
39
#include "SystemZGenCSMappingInsn.inc"
40
};
41
42
void SystemZ_set_instr_map_data(MCInst *MI, const uint8_t *Bytes,
43
        size_t BytesLen)
44
91.1k
{
45
91.1k
  map_cs_id(MI, systemz_insns, ARR_SIZE(systemz_insns));
46
91.1k
  map_implicit_reads(MI, systemz_insns);
47
91.1k
  map_implicit_writes(MI, systemz_insns);
48
91.1k
  map_groups(MI, systemz_insns);
49
91.1k
  const systemz_suppl_info *suppl_info =
50
91.1k
    map_get_suppl_info(MI, systemz_insns);
51
91.1k
  if (suppl_info) {
52
91.1k
    SystemZ_get_detail(MI)->format = suppl_info->form;
53
91.1k
  }
54
91.1k
}
55
56
void SystemZ_init_mri(MCRegisterInfo *MRI)
57
2.52k
{
58
2.52k
  MCRegisterInfo_InitMCRegisterInfo(
59
2.52k
    MRI, SystemZRegDesc, AARCH64_REG_ENDING, 0, 0,
60
2.52k
    SystemZMCRegisterClasses, ARR_SIZE(SystemZMCRegisterClasses), 0,
61
2.52k
    0, SystemZRegDiffLists, 0, SystemZSubRegIdxLists,
62
2.52k
    ARR_SIZE(SystemZSubRegIdxLists), 0);
63
2.52k
}
64
65
const char *SystemZ_reg_name(csh handle, unsigned int reg)
66
54.4k
{
67
54.4k
  return SystemZ_LLVM_getRegisterName(reg);
68
54.4k
}
69
70
void SystemZ_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
71
89.2k
{
72
89.2k
  MI->MRI = (MCRegisterInfo *)info;
73
89.2k
  MI->fillDetailOps = detail_is_set(MI);
74
89.2k
  SystemZ_LLVM_printInstruction(MI, "", O);
75
89.2k
#ifndef CAPSTONE_DIET
76
89.2k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
77
89.2k
       ARR_SIZE(insn_alias_mnem_map));
78
89.2k
#endif
79
89.2k
}
80
81
void SystemZ_init_cs_detail(MCInst *MI)
82
91.1k
{
83
91.1k
  if (!detail_is_set(MI)) {
84
0
    return;
85
0
  }
86
91.1k
  memset(get_detail(MI), 0, sizeof(cs_detail));
87
91.1k
  if (detail_is_set(MI)) {
88
91.1k
    SystemZ_get_detail(MI)->cc = SYSTEMZ_CC_INVALID;
89
91.1k
  }
90
91.1k
}
91
92
bool SystemZ_getInstruction(csh handle, const uint8_t *bytes, size_t bytes_len,
93
          MCInst *MI, uint16_t *size, uint64_t address,
94
          void *info)
95
91.1k
{
96
91.1k
  SystemZ_init_cs_detail(MI);
97
91.1k
  MI->MRI = (MCRegisterInfo *)info;
98
91.1k
  DecodeStatus Result = SystemZ_LLVM_getInstruction(
99
91.1k
    handle, bytes, bytes_len, MI, size, address, info);
100
91.1k
  SystemZ_set_instr_map_data(MI, bytes, bytes_len);
101
91.1k
  if (Result == MCDisassembler_SoftFail) {
102
0
    MCInst_setSoftFail(MI);
103
0
  }
104
91.1k
  return Result != MCDisassembler_Fail;
105
91.1k
}
106
107
// given internal insn id, return public instruction info
108
void SystemZ_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
109
89.2k
{
110
  // We do this after Instruction disassembly.
111
89.2k
}
112
113
const char *SystemZ_insn_name(csh handle, unsigned int id)
114
89.2k
{
115
89.2k
#ifndef CAPSTONE_DIET
116
89.2k
  if (id < SYSTEMZ_INS_ALIAS_END && id > SYSTEMZ_INS_ALIAS_BEGIN) {
117
0
    if (id - SYSTEMZ_INS_ALIAS_BEGIN >=
118
0
        ARR_SIZE(insn_alias_mnem_map))
119
0
      return NULL;
120
121
0
    return insn_alias_mnem_map[id - SYSTEMZ_INS_ALIAS_BEGIN - 1]
122
0
      .name;
123
0
  }
124
89.2k
  if (id >= SYSTEMZ_INS_ENDING)
125
0
    return NULL;
126
127
89.2k
  if (id < ARR_SIZE(insn_name_maps))
128
89.2k
    return insn_name_maps[id];
129
130
  // not found
131
0
  return NULL;
132
#else
133
  return NULL;
134
#endif
135
89.2k
}
136
137
#ifndef CAPSTONE_DIET
138
static const name_map group_name_maps[] = {
139
  // generic groups
140
  { SYSTEMZ_GRP_INVALID, NULL },
141
  { SYSTEMZ_GRP_JUMP, "jump" },
142
  { SYSTEMZ_GRP_CALL, "call" },
143
  { SYSTEMZ_GRP_RET, "return" },
144
  { SYSTEMZ_GRP_INT, "int" },
145
  { SYSTEMZ_GRP_IRET, "iret" },
146
  { SYSTEMZ_GRP_PRIVILEGE, "privilege" },
147
  { SYSTEMZ_GRP_BRANCH_RELATIVE, "branch_relative" },
148
149
#include "SystemZGenCSFeatureName.inc"
150
};
151
#endif
152
153
const char *SystemZ_group_name(csh handle, unsigned int id)
154
57.3k
{
155
57.3k
#ifndef CAPSTONE_DIET
156
57.3k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
157
#else
158
  return NULL;
159
#endif
160
57.3k
}
161
162
void SystemZ_add_cs_detail_0(MCInst *MI, int /* systemz_op_group */ op_group,
163
           size_t op_num)
164
216k
{
165
216k
#ifndef CAPSTONE_DIET
166
216k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
167
0
    return;
168
169
216k
  switch (op_group) {
170
0
  default:
171
0
    printf("Operand group %d not handled\n", op_group);
172
0
    break;
173
128k
  case SystemZ_OP_GROUP_Operand: {
174
128k
    cs_op_type secondary_op_type = map_get_op_type(MI, op_num) &
175
128k
                 ~(CS_OP_MEM | CS_OP_BOUND);
176
128k
    if (secondary_op_type == CS_OP_IMM) {
177
0
      SystemZ_set_detail_op_imm(
178
0
        MI, op_num, MCInst_getOpVal(MI, op_num), 0);
179
128k
    } else if (secondary_op_type == CS_OP_REG) {
180
128k
      SystemZ_set_detail_op_reg(MI, op_num,
181
128k
              MCInst_getOpVal(MI, op_num));
182
128k
    } else {
183
0
      CS_ASSERT_RET(0 && "Op type not handled.");
184
0
    }
185
128k
    break;
186
128k
  }
187
128k
  case SystemZ_OP_GROUP_Cond4Operand: {
188
0
    systemz_cc cc = MCInst_getOpVal(MI, op_num);
189
0
    SystemZ_get_detail(MI)->cc = cc;
190
0
    break;
191
128k
  }
192
24.8k
  case SystemZ_OP_GROUP_BDAddrOperand:
193
24.8k
    CS_ASSERT_RET(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
194
24.8k
    CS_ASSERT_RET(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
195
24.8k
    CS_ASSERT_RET(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
196
24.8k
    CS_ASSERT_RET(
197
24.8k
      MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
198
24.8k
    SystemZ_set_detail_op_mem(MI, op_num,
199
24.8k
            MCInst_getOpVal(MI, (op_num)),
200
24.8k
            MCInst_getOpVal(MI, (op_num + 1)), 0,
201
24.8k
            0, SYSTEMZ_AM_BD);
202
24.8k
    break;
203
1.12k
  case SystemZ_OP_GROUP_BDVAddrOperand:
204
20.0k
  case SystemZ_OP_GROUP_BDXAddrOperand: {
205
20.0k
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
206
20.0k
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
207
20.0k
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
208
20.0k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
209
20.0k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
210
20.0k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num + 2))));
211
20.0k
    SystemZ_set_detail_op_mem(
212
20.0k
      MI, op_num, MCInst_getOpVal(MI, (op_num)),
213
20.0k
      MCInst_getOpVal(MI, (op_num + 1)), 0,
214
20.0k
      MCInst_getOpVal(MI, (op_num + 2)),
215
20.0k
      (op_group == SystemZ_OP_GROUP_BDXAddrOperand ?
216
18.9k
         SYSTEMZ_AM_BDX :
217
20.0k
         SYSTEMZ_AM_BDV));
218
20.0k
    break;
219
1.12k
  }
220
6.02k
  case SystemZ_OP_GROUP_BDLAddrOperand:
221
6.02k
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
222
6.02k
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
223
6.02k
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
224
6.02k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
225
6.02k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
226
6.02k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 2))));
227
6.02k
    SystemZ_set_detail_op_mem(MI, op_num,
228
6.02k
            MCInst_getOpVal(MI, (op_num)),
229
6.02k
            MCInst_getOpVal(MI, (op_num + 1)),
230
6.02k
            MCInst_getOpVal(MI, (op_num + 2)), 0,
231
6.02k
            SYSTEMZ_AM_BDL);
232
6.02k
    break;
233
245
  case SystemZ_OP_GROUP_BDRAddrOperand:
234
245
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
235
245
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
236
245
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
237
245
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
238
245
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
239
245
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num + 2))));
240
245
    SystemZ_set_detail_op_mem(MI, op_num,
241
245
            MCInst_getOpVal(MI, (op_num)),
242
245
            MCInst_getOpVal(MI, (op_num + 1)),
243
245
            MCInst_getOpVal(MI, (op_num + 2)), 0,
244
245
            SYSTEMZ_AM_BDL);
245
245
    break;
246
3.09k
  case SystemZ_OP_GROUP_PCRelOperand:
247
3.09k
    SystemZ_set_detail_op_imm(MI, op_num,
248
3.09k
            MCInst_getOpVal(MI, op_num), 0);
249
3.09k
    break;
250
822
  case SystemZ_OP_GROUP_U1ImmOperand:
251
822
    SystemZ_set_detail_op_imm(MI, op_num,
252
822
            MCInst_getOpVal(MI, op_num), 1);
253
822
    break;
254
966
  case SystemZ_OP_GROUP_U2ImmOperand:
255
966
    SystemZ_set_detail_op_imm(MI, op_num,
256
966
            MCInst_getOpVal(MI, op_num), 2);
257
966
    break;
258
616
  case SystemZ_OP_GROUP_U3ImmOperand:
259
616
    SystemZ_set_detail_op_imm(MI, op_num,
260
616
            MCInst_getOpVal(MI, op_num), 3);
261
616
    break;
262
19.4k
  case SystemZ_OP_GROUP_U4ImmOperand:
263
19.4k
    SystemZ_set_detail_op_imm(MI, op_num,
264
19.4k
            MCInst_getOpVal(MI, op_num), 4);
265
19.4k
    break;
266
4.50k
  case SystemZ_OP_GROUP_U8ImmOperand:
267
5.80k
  case SystemZ_OP_GROUP_S8ImmOperand:
268
5.80k
    SystemZ_set_detail_op_imm(MI, op_num,
269
5.80k
            MCInst_getOpVal(MI, op_num), 8);
270
5.80k
    break;
271
530
  case SystemZ_OP_GROUP_U12ImmOperand:
272
530
    SystemZ_set_detail_op_imm(MI, op_num,
273
530
            MCInst_getOpVal(MI, op_num), 12);
274
530
    break;
275
1.56k
  case SystemZ_OP_GROUP_U16ImmOperand:
276
3.24k
  case SystemZ_OP_GROUP_S16ImmOperand:
277
3.24k
    SystemZ_set_detail_op_imm(MI, op_num,
278
3.24k
            MCInst_getOpVal(MI, op_num), 16);
279
3.24k
    break;
280
763
  case SystemZ_OP_GROUP_U32ImmOperand:
281
1.61k
  case SystemZ_OP_GROUP_S32ImmOperand:
282
1.61k
    SystemZ_set_detail_op_imm(MI, op_num,
283
1.61k
            MCInst_getOpVal(MI, op_num), 32);
284
1.61k
    break;
285
0
  case SystemZ_OP_GROUP_U48ImmOperand:
286
0
    SystemZ_set_detail_op_imm(MI, op_num,
287
0
            MCInst_getOpVal(MI, op_num), 48);
288
0
    break;
289
216k
  }
290
216k
#endif
291
216k
}
292
293
#ifndef CAPSTONE_DIET
294
295
void SystemZ_set_detail_op_imm(MCInst *MI, unsigned op_num, int64_t Imm,
296
             size_t width)
297
36.1k
{
298
36.1k
  if (!detail_is_set(MI))
299
0
    return;
300
36.1k
  CS_ASSERT((map_get_op_type(MI, op_num) & ~CS_OP_MEM) == CS_OP_IMM);
301
302
36.1k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_IMM;
303
36.1k
  SystemZ_get_detail_op(MI, 0)->imm = Imm;
304
36.1k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
305
36.1k
  SystemZ_get_detail_op(MI, 0)->imm_width = width;
306
36.1k
  SystemZ_inc_op_count(MI);
307
36.1k
}
308
309
void SystemZ_set_detail_op_reg(MCInst *MI, unsigned op_num, systemz_reg Reg)
310
128k
{
311
128k
  if (!detail_is_set(MI))
312
0
    return;
313
128k
  CS_ASSERT((map_get_op_type(MI, op_num) & ~CS_OP_MEM) == CS_OP_REG);
314
128k
  if (Reg == SYSTEMZ_REG_INVALID) {
315
    // This case is legal. The ISA says:
316
    // "
317
    // When the R1 field is not zero, bits 8-15 of the instruction designated
318
    // by the second-operand address are ORed with bits 56-63 of
319
    // general register R1. [...] When the R1 field is zero, no ORing takes place
320
    // "
321
    // This means we just save the neutral element for ORing, so 0.
322
1.02k
    SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_IMM;
323
1.02k
    SystemZ_get_detail_op(MI, 0)->imm = 0;
324
1.02k
    SystemZ_get_detail_op(MI, 0)->access =
325
1.02k
      map_get_op_access(MI, op_num);
326
1.02k
    SystemZ_get_detail_op(MI, 0)->imm_width = 0;
327
1.02k
    SystemZ_inc_op_count(MI);
328
1.02k
    return;
329
1.02k
  }
330
331
127k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_REG;
332
127k
  SystemZ_get_detail_op(MI, 0)->reg = Reg;
333
127k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
334
127k
  SystemZ_inc_op_count(MI);
335
127k
}
336
337
void SystemZ_set_detail_op_mem(MCInst *MI, unsigned op_num, systemz_reg base,
338
             int64_t disp, uint64_t length, systemz_reg index,
339
             systemz_addr_mode am)
340
51.1k
{
341
51.1k
  if (!detail_is_set(MI))
342
0
    return;
343
51.1k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_MEM;
344
51.1k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
345
51.1k
  SystemZ_get_detail_op(MI, 0)->mem.am = am;
346
51.1k
  switch (am) {
347
0
  default:
348
0
    CS_ASSERT(0 && "Address mode not handled\n");
349
0
    break;
350
24.8k
  case SYSTEMZ_AM_BD:
351
24.8k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
352
24.8k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
353
24.8k
    break;
354
18.9k
  case SYSTEMZ_AM_BDX:
355
20.0k
  case SYSTEMZ_AM_BDV:
356
20.0k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
357
20.0k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
358
20.0k
    SystemZ_get_detail_op(MI, 0)->mem.index = index;
359
20.0k
    break;
360
6.26k
  case SYSTEMZ_AM_BDL:
361
6.26k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
362
6.26k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
363
6.26k
    SystemZ_get_detail_op(MI, 0)->mem.length = length;
364
6.26k
    break;
365
0
  case SYSTEMZ_AM_BDR:
366
0
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
367
0
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
368
0
    SystemZ_get_detail_op(MI, 0)->mem.length = length;
369
0
    break;
370
51.1k
  }
371
51.1k
  SystemZ_inc_op_count(MI);
372
51.1k
}
373
374
#endif
375
376
#endif