Coverage Report

Created: 2025-07-04 06:11

/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
110k
{
45
110k
  map_cs_id(MI, systemz_insns, ARR_SIZE(systemz_insns));
46
110k
  map_implicit_reads(MI, systemz_insns);
47
110k
  map_implicit_writes(MI, systemz_insns);
48
110k
  map_groups(MI, systemz_insns);
49
110k
  const systemz_suppl_info *suppl_info =
50
110k
    map_get_suppl_info(MI, systemz_insns);
51
110k
  if (suppl_info) {
52
110k
    SystemZ_get_detail(MI)->format = suppl_info->form;
53
110k
  }
54
110k
}
55
56
void SystemZ_init_mri(MCRegisterInfo *MRI)
57
3.55k
{
58
3.55k
  MCRegisterInfo_InitMCRegisterInfo(
59
3.55k
    MRI, SystemZRegDesc, AARCH64_REG_ENDING, 0, 0,
60
3.55k
    SystemZMCRegisterClasses, ARR_SIZE(SystemZMCRegisterClasses), 0,
61
3.55k
    0, SystemZRegDiffLists, 0, SystemZSubRegIdxLists,
62
3.55k
    ARR_SIZE(SystemZSubRegIdxLists), 0);
63
3.55k
}
64
65
const char *SystemZ_reg_name(csh handle, unsigned int reg)
66
70.2k
{
67
70.2k
  return SystemZ_LLVM_getRegisterName(reg);
68
70.2k
}
69
70
void SystemZ_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
71
108k
{
72
108k
  MI->MRI = (MCRegisterInfo *)info;
73
108k
  MI->fillDetailOps = detail_is_set(MI);
74
108k
  SystemZ_LLVM_printInstruction(MI, "", O);
75
108k
#ifndef CAPSTONE_DIET
76
108k
  map_set_alias_id(MI, O, insn_alias_mnem_map,
77
108k
       ARR_SIZE(insn_alias_mnem_map));
78
108k
#endif
79
108k
}
80
81
110k
void SystemZ_init_cs_detail(MCInst *MI) {
82
110k
  if (!detail_is_set(MI)) {
83
0
    return;
84
0
  }
85
110k
  memset(get_detail(MI), 0, sizeof(cs_detail));
86
110k
  if (detail_is_set(MI)) {
87
110k
    SystemZ_get_detail(MI)->cc = SYSTEMZ_CC_INVALID;
88
110k
  }
89
110k
}
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
110k
{
95
110k
  SystemZ_init_cs_detail(MI);
96
110k
  MI->MRI = (MCRegisterInfo *)info;
97
110k
  DecodeStatus Result = SystemZ_LLVM_getInstruction(
98
110k
    handle, bytes, bytes_len, MI, size, address, info);
99
110k
  SystemZ_set_instr_map_data(MI, bytes, bytes_len);
100
110k
  if (Result == MCDisassembler_SoftFail) {
101
0
    MCInst_setSoftFail(MI);
102
0
  }
103
110k
  return Result != MCDisassembler_Fail;
104
110k
}
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
108k
{
109
  // We do this after Instruction disassembly.
110
108k
}
111
112
const char *SystemZ_insn_name(csh handle, unsigned int id)
113
108k
{
114
108k
#ifndef CAPSTONE_DIET
115
108k
  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
108k
  if (id >= SYSTEMZ_INS_ENDING)
124
0
    return NULL;
125
126
108k
  if (id < ARR_SIZE(insn_name_maps))
127
108k
    return insn_name_maps[id];
128
129
  // not found
130
0
  return NULL;
131
#else
132
  return NULL;
133
#endif
134
108k
}
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
69.9k
{
154
69.9k
#ifndef CAPSTONE_DIET
155
69.9k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
156
#else
157
  return NULL;
158
#endif
159
69.9k
}
160
161
void SystemZ_add_cs_detail(MCInst *MI, int /* aarch64_op_group */ op_group,
162
         va_list args)
163
253k
{
164
253k
#ifndef CAPSTONE_DIET
165
253k
  if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
166
0
    return;
167
168
253k
  unsigned op_num = va_arg(args, unsigned);
169
170
253k
  switch (op_group) {
171
0
  default:
172
0
    printf("Operand group %d not handled\n", op_group);
173
0
    break;
174
150k
  case SystemZ_OP_GROUP_Operand: {
175
150k
    cs_op_type secondary_op_type = map_get_op_type(MI, op_num) &
176
150k
               ~(CS_OP_MEM | CS_OP_BOUND);
177
150k
    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
150k
    } else if (secondary_op_type == CS_OP_REG) {
181
150k
      SystemZ_set_detail_op_reg(MI, op_num,
182
150k
                MCInst_getOpVal(MI, op_num));
183
150k
    } else {
184
0
      CS_ASSERT_RET(0 && "Op type not handled.");
185
0
    }
186
150k
    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
27.8k
  case SystemZ_OP_GROUP_BDAddrOperand:
194
27.8k
    CS_ASSERT_RET(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
195
27.8k
    CS_ASSERT_RET(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
196
27.8k
    CS_ASSERT_RET(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
197
27.8k
    CS_ASSERT_RET(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
198
27.8k
    SystemZ_set_detail_op_mem(MI,
199
27.8k
                              op_num,
200
27.8k
                              MCInst_getOpVal(MI, (op_num)),
201
27.8k
                              MCInst_getOpVal(MI, (op_num + 1)),
202
27.8k
                              0,
203
27.8k
                              0,
204
27.8k
                              SYSTEMZ_AM_BD
205
27.8k
                            );
206
27.8k
    break;
207
608
  case SystemZ_OP_GROUP_BDVAddrOperand:
208
27.7k
  case SystemZ_OP_GROUP_BDXAddrOperand: {
209
27.7k
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
210
27.7k
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
211
27.7k
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
212
27.7k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
213
27.7k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
214
27.7k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num + 2))));
215
27.7k
    SystemZ_set_detail_op_mem(MI,
216
27.7k
                              op_num,
217
27.7k
                              MCInst_getOpVal(MI, (op_num)),
218
27.7k
                              MCInst_getOpVal(MI, (op_num + 1)),
219
27.7k
                              0,
220
27.7k
                              MCInst_getOpVal(MI, (op_num + 2)),
221
27.7k
                              (op_group == SystemZ_OP_GROUP_BDXAddrOperand ? SYSTEMZ_AM_BDX : SYSTEMZ_AM_BDV)
222
27.7k
                            );
223
27.7k
    break;
224
608
  }
225
8.19k
  case SystemZ_OP_GROUP_BDLAddrOperand:
226
8.19k
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
227
8.19k
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
228
8.19k
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
229
8.19k
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
230
8.19k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
231
8.19k
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 2))));
232
8.19k
    SystemZ_set_detail_op_mem(MI,
233
8.19k
                              op_num,
234
8.19k
                              MCInst_getOpVal(MI, (op_num)),
235
8.19k
                              MCInst_getOpVal(MI, (op_num + 1)),
236
8.19k
                              MCInst_getOpVal(MI, (op_num + 2)),
237
8.19k
                              0,
238
8.19k
                              SYSTEMZ_AM_BDL
239
8.19k
                            );
240
8.19k
    break;
241
677
  case SystemZ_OP_GROUP_BDRAddrOperand:
242
677
    CS_ASSERT(map_get_op_type(MI, (op_num)) & CS_OP_MEM);
243
677
    CS_ASSERT(map_get_op_type(MI, (op_num + 1)) & CS_OP_MEM);
244
677
    CS_ASSERT(map_get_op_type(MI, (op_num + 2)) & CS_OP_MEM);
245
677
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num))));
246
677
    CS_ASSERT(MCOperand_isImm(MCInst_getOperand(MI, (op_num + 1))));
247
677
    CS_ASSERT(MCOperand_isReg(MCInst_getOperand(MI, (op_num + 2))));
248
677
    SystemZ_set_detail_op_mem(MI,
249
677
                              op_num,
250
677
                              MCInst_getOpVal(MI, (op_num)),
251
677
                              MCInst_getOpVal(MI, (op_num + 1)),
252
677
                              MCInst_getOpVal(MI, (op_num + 2)),
253
677
                              0,
254
677
                              SYSTEMZ_AM_BDL
255
677
                            );
256
677
    break;
257
4.15k
  case SystemZ_OP_GROUP_PCRelOperand:
258
4.15k
      SystemZ_set_detail_op_imm(MI, op_num,
259
4.15k
                MCInst_getOpVal(MI, op_num), 0);
260
4.15k
    break;
261
932
  case SystemZ_OP_GROUP_U1ImmOperand:
262
932
      SystemZ_set_detail_op_imm(MI, op_num,
263
932
                MCInst_getOpVal(MI, op_num), 1);
264
932
    break;
265
1.40k
  case SystemZ_OP_GROUP_U2ImmOperand:
266
1.40k
      SystemZ_set_detail_op_imm(MI, op_num,
267
1.40k
                MCInst_getOpVal(MI, op_num), 2);
268
1.40k
    break;
269
755
  case SystemZ_OP_GROUP_U3ImmOperand:
270
755
      SystemZ_set_detail_op_imm(MI, op_num,
271
755
                MCInst_getOpVal(MI, op_num), 3);
272
755
    break;
273
19.2k
  case SystemZ_OP_GROUP_U4ImmOperand:
274
19.2k
      SystemZ_set_detail_op_imm(MI, op_num,
275
19.2k
                MCInst_getOpVal(MI, op_num), 4);
276
19.2k
    break;
277
5.19k
  case SystemZ_OP_GROUP_U8ImmOperand:
278
6.17k
  case SystemZ_OP_GROUP_S8ImmOperand:
279
6.17k
      SystemZ_set_detail_op_imm(MI, op_num,
280
6.17k
                MCInst_getOpVal(MI, op_num), 8);
281
6.17k
    break;
282
70
  case SystemZ_OP_GROUP_U12ImmOperand:
283
70
      SystemZ_set_detail_op_imm(MI, op_num,
284
70
                MCInst_getOpVal(MI, op_num), 12);
285
70
    break;
286
1.53k
  case SystemZ_OP_GROUP_U16ImmOperand:
287
3.78k
  case SystemZ_OP_GROUP_S16ImmOperand:
288
3.78k
      SystemZ_set_detail_op_imm(MI, op_num,
289
3.78k
                MCInst_getOpVal(MI, op_num), 16);
290
3.78k
    break;
291
883
  case SystemZ_OP_GROUP_U32ImmOperand:
292
1.50k
  case SystemZ_OP_GROUP_S32ImmOperand:
293
1.50k
      SystemZ_set_detail_op_imm(MI, op_num,
294
1.50k
                MCInst_getOpVal(MI, op_num), 32);
295
1.50k
    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
253k
  }
301
253k
#endif
302
253k
}
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
38.0k
{
308
38.0k
  if (!detail_is_set(MI))
309
0
    return;
310
38.0k
  CS_ASSERT((map_get_op_type(MI, op_num) & ~CS_OP_MEM) == CS_OP_IMM);
311
312
38.0k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_IMM;
313
38.0k
  SystemZ_get_detail_op(MI, 0)->imm = Imm;
314
38.0k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
315
38.0k
  SystemZ_get_detail_op(MI, 0)->imm_width = width;
316
38.0k
  SystemZ_inc_op_count(MI);
317
38.0k
}
318
319
void SystemZ_set_detail_op_reg(MCInst *MI, unsigned op_num, systemz_reg Reg)
320
150k
{
321
150k
  if (!detail_is_set(MI))
322
0
    return;
323
150k
  CS_ASSERT((map_get_op_type(MI, op_num) & ~CS_OP_MEM) == CS_OP_REG);
324
325
150k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_REG;
326
150k
  SystemZ_get_detail_op(MI, 0)->reg = Reg;
327
150k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
328
150k
  SystemZ_inc_op_count(MI);
329
150k
}
330
331
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)
332
64.5k
{
333
64.5k
  if (!detail_is_set(MI))
334
0
    return;
335
64.5k
  SystemZ_get_detail_op(MI, 0)->type = SYSTEMZ_OP_MEM;
336
64.5k
  SystemZ_get_detail_op(MI, 0)->access = map_get_op_access(MI, op_num);
337
64.5k
  SystemZ_get_detail_op(MI, 0)->mem.am = am;
338
64.5k
  switch(am) {
339
0
  default:
340
0
    CS_ASSERT(0 && "Address mode not handled\n");
341
0
    break;
342
27.8k
  case SYSTEMZ_AM_BD:
343
27.8k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
344
27.8k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
345
27.8k
    break;
346
27.1k
  case SYSTEMZ_AM_BDX:
347
27.7k
  case SYSTEMZ_AM_BDV:
348
27.7k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
349
27.7k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
350
27.7k
    SystemZ_get_detail_op(MI, 0)->mem.index = index;
351
27.7k
    break;
352
8.87k
  case SYSTEMZ_AM_BDL:
353
8.87k
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
354
8.87k
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
355
8.87k
    SystemZ_get_detail_op(MI, 0)->mem.length = length;
356
8.87k
    break;
357
0
  case SYSTEMZ_AM_BDR:
358
0
    SystemZ_get_detail_op(MI, 0)->mem.base = base;
359
0
    SystemZ_get_detail_op(MI, 0)->mem.disp = disp;
360
0
    SystemZ_get_detail_op(MI, 0)->mem.length = length;
361
0
    break;
362
64.5k
  }
363
64.5k
  SystemZ_inc_op_count(MI);
364
64.5k
}
365
366
#endif
367
368
#endif