Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/arch/SystemZ/SystemZInstPrinter.c
Line
Count
Source
1
//===-- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly syntax --------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This class prints an SystemZ MCInst to a .s file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
/* Capstone Disassembly Engine */
15
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
16
17
#ifdef CAPSTONE_HAS_SYSZ
18
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <capstone/platform.h>
23
24
#include "SystemZInstPrinter.h"
25
#include "../../MCInst.h"
26
#include "../../utils.h"
27
#include "../../SStream.h"
28
#include "../../MCRegisterInfo.h"
29
#include "../../MathExtras.h"
30
#include "SystemZMapping.h"
31
32
static const char *getRegisterName(unsigned RegNo);
33
34
void SystemZ_post_printer(csh ud, cs_insn *insn, char *insn_asm, MCInst *mci)
35
73.9k
{
36
  /*
37
     if (((cs_struct *)ud)->detail != CS_OPT_ON)
38
     return;
39
   */
40
73.9k
}
41
42
static void printAddress(MCInst *MI, unsigned Base, int64_t Disp, unsigned Index, SStream *O)
43
27.3k
{
44
27.3k
  printInt64(O, Disp);
45
46
27.3k
  if (Base) {
47
21.1k
    SStream_concat0(O, "(");
48
21.1k
    if (Index)
49
7.53k
      SStream_concat(O, "%%%s, ", getRegisterName(Index));
50
21.1k
    SStream_concat(O, "%%%s)", getRegisterName(Base));
51
52
21.1k
    if (MI->csh->detail) {
53
21.1k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
54
21.1k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
55
21.1k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.index = (uint8_t)SystemZ_map_register(Index);
56
21.1k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = Disp;
57
21.1k
      MI->flat_insn->detail->sysz.op_count++;
58
21.1k
    }
59
21.1k
  } else if (!Index) {
60
4.30k
    if (MI->csh->detail) {
61
4.30k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
62
4.30k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Disp;
63
4.30k
      MI->flat_insn->detail->sysz.op_count++;
64
4.30k
    }
65
4.30k
  } else {
66
1.94k
    SStream_concat(O, "(%%%s)", getRegisterName(Index));
67
1.94k
    if (MI->csh->detail) {
68
1.94k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
69
1.94k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
70
1.94k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.index = (uint8_t)SystemZ_map_register(Index);
71
1.94k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = Disp;
72
1.94k
      MI->flat_insn->detail->sysz.op_count++;
73
1.94k
    }
74
1.94k
  }
75
27.3k
}
76
77
static void _printOperand(MCInst *MI, MCOperand *MO, SStream *O)
78
111k
{
79
111k
  if (MCOperand_isReg(MO)) {
80
111k
    unsigned reg;
81
82
111k
    reg = MCOperand_getReg(MO);
83
111k
    SStream_concat(O, "%%%s", getRegisterName(reg));
84
111k
    reg = SystemZ_map_register(reg);
85
86
111k
    if (MI->csh->detail) {
87
111k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_REG;
88
111k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].reg = reg;
89
111k
      MI->flat_insn->detail->sysz.op_count++;
90
111k
    }
91
111k
  } else if (MCOperand_isImm(MO)) {
92
0
    int64_t Imm = MCOperand_getImm(MO);
93
94
0
    printInt64(O, Imm);
95
96
0
    if (MI->csh->detail) {
97
0
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
98
0
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Imm;
99
0
      MI->flat_insn->detail->sysz.op_count++;
100
0
    }
101
0
  }
102
111k
}
103
104
static void printU1ImmOperand(MCInst *MI, int OpNum, SStream *O)
105
794
{
106
794
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
107
  // assert(isUInt<1>(Value) && "Invalid u1imm argument");
108
794
  printInt64(O, Value);
109
110
794
  if (MI->csh->detail) {
111
794
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
112
794
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
113
794
    MI->flat_insn->detail->sysz.op_count++;
114
794
  }
115
794
}
116
117
static void printU2ImmOperand(MCInst *MI, int OpNum, SStream *O)
118
648
{
119
648
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
120
  // assert(isUInt<2>(Value) && "Invalid u2imm argument");
121
648
  printInt64(O, Value);
122
123
648
  if (MI->csh->detail) {
124
648
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
125
648
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
126
648
    MI->flat_insn->detail->sysz.op_count++;
127
648
  }
128
648
}
129
130
static void printU3ImmOperand(MCInst *MI, int OpNum, SStream *O)
131
174
{
132
174
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
133
  // assert(isUInt<3>(Value) && "Invalid u4imm argument");
134
174
  printInt64(O, Value);
135
136
174
  if (MI->csh->detail) {
137
174
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
138
174
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
139
174
    MI->flat_insn->detail->sysz.op_count++;
140
174
  }
141
174
}
142
143
static void printU4ImmOperand(MCInst *MI, int OpNum, SStream *O)
144
22.5k
{
145
22.5k
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
146
  // assert(isUInt<4>(Value) && "Invalid u4imm argument");
147
22.5k
  printInt64(O, Value);
148
149
22.5k
  if (MI->csh->detail) {
150
22.5k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
151
22.5k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
152
22.5k
    MI->flat_insn->detail->sysz.op_count++;
153
22.5k
  }
154
22.5k
}
155
156
static void printU6ImmOperand(MCInst *MI, int OpNum, SStream *O)
157
529
{
158
529
  uint32_t Value = (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
159
  // assert(isUInt<6>(Value) && "Invalid u6imm argument");
160
161
529
  printUInt32(O, Value);
162
163
529
  if (MI->csh->detail) {
164
529
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
165
529
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
166
529
    MI->flat_insn->detail->sysz.op_count++;
167
529
  }
168
529
}
169
170
static void printS8ImmOperand(MCInst *MI, int OpNum, SStream *O)
171
930
{
172
930
  int8_t Value = (int8_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
173
  // assert(isInt<8>(Value) && "Invalid s8imm argument");
174
175
930
  if (Value >= 0) {
176
610
    if (Value > HEX_THRESHOLD)
177
320
      SStream_concat(O, "0x%x", Value);
178
290
    else
179
290
      SStream_concat(O, "%u", Value);
180
610
  } else {
181
320
    if (Value < -HEX_THRESHOLD)
182
215
      SStream_concat(O, "-0x%x", -Value);
183
105
    else
184
105
      SStream_concat(O, "-%u", -Value);
185
320
  }
186
187
930
  if (MI->csh->detail) {
188
930
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
189
930
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
190
930
    MI->flat_insn->detail->sysz.op_count++;
191
930
  }
192
930
}
193
194
static void printU8ImmOperand(MCInst *MI, int OpNum, SStream *O)
195
5.11k
{
196
5.11k
  uint8_t Value = (uint8_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
197
  // assert(isUInt<8>(Value) && "Invalid u8imm argument");
198
199
5.11k
  if (Value > HEX_THRESHOLD)
200
4.25k
    SStream_concat(O, "0x%x", Value);
201
852
  else
202
852
    SStream_concat(O, "%u", Value);
203
204
5.11k
  if (MI->csh->detail) {
205
5.11k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
206
5.11k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
207
5.11k
    MI->flat_insn->detail->sysz.op_count++;
208
5.11k
  }
209
5.11k
}
210
211
static void printU12ImmOperand(MCInst *MI, int OpNum, SStream *O)
212
455
{
213
455
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
214
  // assert(isUInt<12>(Value) && "Invalid u12imm argument");
215
455
  printInt64(O, Value);
216
217
455
  if (MI->csh->detail) {
218
455
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
219
455
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
220
455
    MI->flat_insn->detail->sysz.op_count++;
221
455
  }
222
455
}
223
224
static void printS16ImmOperand(MCInst *MI, int OpNum, SStream *O)
225
2.83k
{
226
2.83k
  int16_t Value = (int16_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
227
  // assert(isInt<16>(Value) && "Invalid s16imm argument");
228
229
2.83k
  if (Value >= 0) {
230
1.81k
    if (Value > HEX_THRESHOLD)
231
1.74k
      SStream_concat(O, "0x%x", Value);
232
69
    else
233
69
      SStream_concat(O, "%u", Value);
234
1.81k
  } else {
235
1.02k
    if (Value < -HEX_THRESHOLD)
236
655
      SStream_concat(O, "-0x%x", -Value);
237
366
    else
238
366
      SStream_concat(O, "-%u", -Value);
239
1.02k
  }
240
241
2.83k
  if (MI->csh->detail) {
242
2.83k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
243
2.83k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
244
2.83k
    MI->flat_insn->detail->sysz.op_count++;
245
2.83k
  }
246
2.83k
}
247
248
static void printU16ImmOperand(MCInst *MI, int OpNum, SStream *O)
249
1.59k
{
250
1.59k
  uint16_t Value = (uint16_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
251
  // assert(isUInt<16>(Value) && "Invalid u16imm argument");
252
253
1.59k
  if (Value > HEX_THRESHOLD)
254
1.57k
    SStream_concat(O, "0x%x", Value);
255
17
  else
256
17
    SStream_concat(O, "%u", Value);
257
258
1.59k
  if (MI->csh->detail) {
259
1.59k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
260
1.59k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
261
1.59k
    MI->flat_insn->detail->sysz.op_count++;
262
1.59k
  }
263
1.59k
}
264
265
static void printS32ImmOperand(MCInst *MI, int OpNum, SStream *O)
266
861
{
267
861
  int32_t Value = (int32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
268
  // assert(isInt<32>(Value) && "Invalid s32imm argument");
269
270
861
  printInt32(O, Value);
271
272
861
  if (MI->csh->detail) {
273
861
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
274
861
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
275
861
    MI->flat_insn->detail->sysz.op_count++;
276
861
  }
277
861
}
278
279
static void printU32ImmOperand(MCInst *MI, int OpNum, SStream *O)
280
893
{
281
893
  uint32_t Value = (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
282
  // assert(isUInt<32>(Value) && "Invalid u32imm argument");
283
284
893
  printUInt32(O, Value);
285
286
893
  if (MI->csh->detail) {
287
893
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
288
893
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
289
893
    MI->flat_insn->detail->sysz.op_count++;
290
893
  }
291
893
}
292
293
static void printU48ImmOperand(MCInst *MI, int OpNum, SStream *O)
294
0
{
295
0
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
296
  // assert(isUInt<48>(Value) && "Invalid u48imm argument");
297
0
  printInt64(O, Value);
298
299
0
  if (MI->csh->detail) {
300
0
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
301
0
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
302
0
    MI->flat_insn->detail->sysz.op_count++;
303
0
  }
304
0
}
305
306
static void printPCRelOperand(MCInst *MI, int OpNum, SStream *O)
307
4.59k
{
308
4.59k
  MCOperand *MO = MCInst_getOperand(MI, OpNum);
309
310
4.59k
  if (MCOperand_isImm(MO)) {
311
4.59k
    int64_t imm = (int64_t)MCOperand_getImm(MO);
312
313
4.59k
    printInt64(O, imm);
314
315
4.59k
    if (MI->csh->detail) {
316
4.59k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
317
4.59k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = imm;
318
4.59k
      MI->flat_insn->detail->sysz.op_count++;
319
4.59k
    }
320
4.59k
  }
321
4.59k
}
322
323
static void printPCRelTLSOperand(MCInst *MI, int OpNum, SStream *O)
324
219
{
325
  // Output the PC-relative operand.
326
219
  printPCRelOperand(MI, OpNum, O);
327
219
}
328
329
static void printOperand(MCInst *MI, int OpNum, SStream *O)
330
111k
{
331
111k
  _printOperand(MI, MCInst_getOperand(MI, OpNum), O);
332
111k
}
333
334
static void printBDAddrOperand(MCInst *MI, int OpNum, SStream *O)
335
15.5k
{
336
15.5k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
337
15.5k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)), 0, O);
338
15.5k
}
339
340
static void printBDXAddrOperand(MCInst *MI, int OpNum, SStream *O)
341
11.0k
{
342
11.0k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
343
11.0k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
344
11.0k
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
345
11.0k
}
346
347
static void printBDLAddrOperand(MCInst *MI, int OpNum, SStream *O)
348
5.88k
{
349
5.88k
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
350
5.88k
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
351
5.88k
  uint64_t Length = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 2));
352
353
5.88k
  if (Disp > HEX_THRESHOLD)
354
5.28k
    SStream_concat(O, "0x%"PRIx64, Disp);
355
600
  else
356
600
    SStream_concat(O, "%"PRIu64, Disp);
357
358
5.88k
  if (Length > HEX_THRESHOLD)
359
4.27k
    SStream_concat(O, "(0x%"PRIx64, Length);
360
1.61k
  else
361
1.61k
    SStream_concat(O, "(%"PRIu64, Length);
362
363
5.88k
  if (Base)
364
4.45k
    SStream_concat(O, ", %%%s", getRegisterName(Base));
365
5.88k
  SStream_concat0(O, ")");
366
367
5.88k
  if (MI->csh->detail) {
368
5.88k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
369
5.88k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
370
5.88k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = Length;
371
5.88k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
372
5.88k
    MI->flat_insn->detail->sysz.op_count++;
373
5.88k
  }
374
5.88k
}
375
376
static void printBDRAddrOperand(MCInst *MI, int OpNum, SStream *O)
377
489
{
378
489
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
379
489
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
380
489
  uint64_t Length = MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2));
381
382
489
  if (Disp > HEX_THRESHOLD)
383
290
    SStream_concat(O, "0x%"PRIx64, Disp);
384
199
  else
385
199
    SStream_concat(O, "%"PRIu64, Disp);
386
387
489
  SStream_concat0(O, "(");
388
489
  SStream_concat(O, "%%%s", getRegisterName(Length));
389
390
489
  if (Base)
391
256
    SStream_concat(O, ", %%%s", getRegisterName(Base));
392
489
  SStream_concat0(O, ")");
393
394
489
  if (MI->csh->detail) {
395
489
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
396
489
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
397
489
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = (uint8_t)SystemZ_map_register(Length);
398
489
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
399
489
    MI->flat_insn->detail->sysz.op_count++;
400
489
  }
401
489
}
402
403
static void printBDVAddrOperand(MCInst *MI, int OpNum, SStream *O)
404
768
{
405
768
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
406
768
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
407
768
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
408
768
}
409
410
static void printCond4Operand(MCInst *MI, int OpNum, SStream *O)
411
0
{
412
0
  static const char *const CondNames[] = {
413
0
    "o", "h", "nle", "l", "nhe", "lh", "ne",
414
0
    "e", "nlh", "he", "nl", "le", "nh", "no"
415
0
  };
416
417
0
  uint64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
418
  // assert(Imm > 0 && Imm < 15 && "Invalid condition");
419
0
  SStream_concat0(O, CondNames[Imm - 1]);
420
421
0
  if (MI->csh->detail)
422
0
    MI->flat_insn->detail->sysz.cc = (sysz_cc)Imm;
423
0
}
424
425
#define PRINT_ALIAS_INSTR
426
#include "SystemZGenAsmWriter.inc"
427
428
void SystemZ_printInst(MCInst *MI, SStream *O, void *Info)
429
73.9k
{
430
73.9k
  printInstruction(MI, O, Info);
431
73.9k
}
432
433
#endif