Coverage Report

Created: 2025-07-11 06:32

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