Coverage Report

Created: 2026-04-29 06:06

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
65.8k
{
36
  /*
37
     if (((cs_struct *)ud)->detail != CS_OPT_ON)
38
     return;
39
   */
40
65.8k
}
41
42
static void printAddress(MCInst *MI, unsigned Base, int64_t Disp, unsigned Index, SStream *O)
43
25.4k
{
44
25.4k
  printInt64(O, Disp);
45
46
25.4k
  if (Base) {
47
20.4k
    SStream_concat0(O, "(");
48
20.4k
    if (Index)
49
6.66k
      SStream_concat(O, "%%%s, ", getRegisterName(Index));
50
20.4k
    SStream_concat(O, "%%%s)", getRegisterName(Base));
51
52
20.4k
    if (MI->csh->detail) {
53
20.4k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
54
20.4k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
55
20.4k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.index = (uint8_t)SystemZ_map_register(Index);
56
20.4k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = Disp;
57
20.4k
      MI->flat_insn->detail->sysz.op_count++;
58
20.4k
    }
59
20.4k
  } else if (!Index) {
60
3.25k
    if (MI->csh->detail) {
61
3.25k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
62
3.25k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Disp;
63
3.25k
      MI->flat_insn->detail->sysz.op_count++;
64
3.25k
    }
65
3.25k
  } else {
66
1.77k
    SStream_concat(O, "(%%%s)", getRegisterName(Index));
67
1.77k
    if (MI->csh->detail) {
68
1.77k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
69
1.77k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
70
1.77k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.index = (uint8_t)SystemZ_map_register(Index);
71
1.77k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = Disp;
72
1.77k
      MI->flat_insn->detail->sysz.op_count++;
73
1.77k
    }
74
1.77k
  }
75
25.4k
}
76
77
static void _printOperand(MCInst *MI, MCOperand *MO, SStream *O)
78
97.6k
{
79
97.6k
  if (MCOperand_isReg(MO)) {
80
97.6k
    unsigned reg;
81
82
97.6k
    reg = MCOperand_getReg(MO);
83
97.6k
    SStream_concat(O, "%%%s", getRegisterName(reg));
84
97.6k
    reg = SystemZ_map_register(reg);
85
86
97.6k
    if (MI->csh->detail) {
87
97.6k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_REG;
88
97.6k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].reg = reg;
89
97.6k
      MI->flat_insn->detail->sysz.op_count++;
90
97.6k
    }
91
97.6k
  } 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
97.6k
}
103
104
static void printU1ImmOperand(MCInst *MI, int OpNum, SStream *O)
105
784
{
106
784
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
107
  // assert(isUInt<1>(Value) && "Invalid u1imm argument");
108
784
  printInt64(O, Value);
109
110
784
  if (MI->csh->detail) {
111
784
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
112
784
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
113
784
    MI->flat_insn->detail->sysz.op_count++;
114
784
  }
115
784
}
116
117
static void printU2ImmOperand(MCInst *MI, int OpNum, SStream *O)
118
727
{
119
727
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
120
  // assert(isUInt<2>(Value) && "Invalid u2imm argument");
121
727
  printInt64(O, Value);
122
123
727
  if (MI->csh->detail) {
124
727
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
125
727
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
126
727
    MI->flat_insn->detail->sysz.op_count++;
127
727
  }
128
727
}
129
130
static void printU3ImmOperand(MCInst *MI, int OpNum, SStream *O)
131
111
{
132
111
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
133
  // assert(isUInt<3>(Value) && "Invalid u4imm argument");
134
111
  printInt64(O, Value);
135
136
111
  if (MI->csh->detail) {
137
111
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
138
111
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
139
111
    MI->flat_insn->detail->sysz.op_count++;
140
111
  }
141
111
}
142
143
static void printU4ImmOperand(MCInst *MI, int OpNum, SStream *O)
144
17.9k
{
145
17.9k
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
146
  // assert(isUInt<4>(Value) && "Invalid u4imm argument");
147
17.9k
  printInt64(O, Value);
148
149
17.9k
  if (MI->csh->detail) {
150
17.9k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
151
17.9k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
152
17.9k
    MI->flat_insn->detail->sysz.op_count++;
153
17.9k
  }
154
17.9k
}
155
156
static void printU6ImmOperand(MCInst *MI, int OpNum, SStream *O)
157
206
{
158
206
  uint32_t Value = (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
159
  // assert(isUInt<6>(Value) && "Invalid u6imm argument");
160
161
206
  printUInt32(O, Value);
162
163
206
  if (MI->csh->detail) {
164
206
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
165
206
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
166
206
    MI->flat_insn->detail->sysz.op_count++;
167
206
  }
168
206
}
169
170
static void printS8ImmOperand(MCInst *MI, int OpNum, SStream *O)
171
630
{
172
630
  int8_t Value = (int8_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
173
  // assert(isInt<8>(Value) && "Invalid s8imm argument");
174
175
630
  if (Value >= 0) {
176
471
    if (Value > HEX_THRESHOLD)
177
375
      SStream_concat(O, "0x%x", Value);
178
96
    else
179
96
      SStream_concat(O, "%u", Value);
180
471
  } else {
181
159
    if (Value < -HEX_THRESHOLD)
182
99
      SStream_concat(O, "-0x%x", -Value);
183
60
    else
184
60
      SStream_concat(O, "-%u", -Value);
185
159
  }
186
187
630
  if (MI->csh->detail) {
188
630
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
189
630
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
190
630
    MI->flat_insn->detail->sysz.op_count++;
191
630
  }
192
630
}
193
194
static void printU8ImmOperand(MCInst *MI, int OpNum, SStream *O)
195
4.30k
{
196
4.30k
  uint8_t Value = (uint8_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
197
  // assert(isUInt<8>(Value) && "Invalid u8imm argument");
198
199
4.30k
  if (Value > HEX_THRESHOLD)
200
3.88k
    SStream_concat(O, "0x%x", Value);
201
413
  else
202
413
    SStream_concat(O, "%u", Value);
203
204
4.30k
  if (MI->csh->detail) {
205
4.30k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
206
4.30k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
207
4.30k
    MI->flat_insn->detail->sysz.op_count++;
208
4.30k
  }
209
4.30k
}
210
211
static void printU12ImmOperand(MCInst *MI, int OpNum, SStream *O)
212
599
{
213
599
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
214
  // assert(isUInt<12>(Value) && "Invalid u12imm argument");
215
599
  printInt64(O, Value);
216
217
599
  if (MI->csh->detail) {
218
599
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
219
599
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
220
599
    MI->flat_insn->detail->sysz.op_count++;
221
599
  }
222
599
}
223
224
static void printS16ImmOperand(MCInst *MI, int OpNum, SStream *O)
225
3.12k
{
226
3.12k
  int16_t Value = (int16_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
227
  // assert(isInt<16>(Value) && "Invalid s16imm argument");
228
229
3.12k
  if (Value >= 0) {
230
1.61k
    if (Value > HEX_THRESHOLD)
231
1.30k
      SStream_concat(O, "0x%x", Value);
232
305
    else
233
305
      SStream_concat(O, "%u", Value);
234
1.61k
  } else {
235
1.51k
    if (Value < -HEX_THRESHOLD)
236
1.31k
      SStream_concat(O, "-0x%x", -Value);
237
204
    else
238
204
      SStream_concat(O, "-%u", -Value);
239
1.51k
  }
240
241
3.12k
  if (MI->csh->detail) {
242
3.12k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
243
3.12k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
244
3.12k
    MI->flat_insn->detail->sysz.op_count++;
245
3.12k
  }
246
3.12k
}
247
248
static void printU16ImmOperand(MCInst *MI, int OpNum, SStream *O)
249
1.50k
{
250
1.50k
  uint16_t Value = (uint16_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
251
  // assert(isUInt<16>(Value) && "Invalid u16imm argument");
252
253
1.50k
  if (Value > HEX_THRESHOLD)
254
1.36k
    SStream_concat(O, "0x%x", Value);
255
137
  else
256
137
    SStream_concat(O, "%u", Value);
257
258
1.50k
  if (MI->csh->detail) {
259
1.50k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
260
1.50k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
261
1.50k
    MI->flat_insn->detail->sysz.op_count++;
262
1.50k
  }
263
1.50k
}
264
265
static void printS32ImmOperand(MCInst *MI, int OpNum, SStream *O)
266
830
{
267
830
  int32_t Value = (int32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
268
  // assert(isInt<32>(Value) && "Invalid s32imm argument");
269
270
830
  printInt32(O, Value);
271
272
830
  if (MI->csh->detail) {
273
830
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
274
830
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
275
830
    MI->flat_insn->detail->sysz.op_count++;
276
830
  }
277
830
}
278
279
static void printU32ImmOperand(MCInst *MI, int OpNum, SStream *O)
280
345
{
281
345
  uint32_t Value = (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
282
  // assert(isUInt<32>(Value) && "Invalid u32imm argument");
283
284
345
  printUInt32(O, Value);
285
286
345
  if (MI->csh->detail) {
287
345
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
288
345
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
289
345
    MI->flat_insn->detail->sysz.op_count++;
290
345
  }
291
345
}
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.15k
{
308
4.15k
  MCOperand *MO = MCInst_getOperand(MI, OpNum);
309
310
4.15k
  if (MCOperand_isImm(MO)) {
311
4.15k
    int64_t imm = (int64_t)MCOperand_getImm(MO);
312
313
4.15k
    printInt64(O, imm);
314
315
4.15k
    if (MI->csh->detail) {
316
4.15k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
317
4.15k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = imm;
318
4.15k
      MI->flat_insn->detail->sysz.op_count++;
319
4.15k
    }
320
4.15k
  }
321
4.15k
}
322
323
static void printPCRelTLSOperand(MCInst *MI, int OpNum, SStream *O)
324
298
{
325
  // Output the PC-relative operand.
326
298
  printPCRelOperand(MI, OpNum, O);
327
298
}
328
329
static void printOperand(MCInst *MI, int OpNum, SStream *O)
330
280k
{
331
280k
  _printOperand(MI, MCInst_getOperand(MI, OpNum), O);
332
280k
}
333
334
static void printBDAddrOperand(MCInst *MI, int OpNum, SStream *O)
335
49.4k
{
336
49.4k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
337
49.4k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)), 0, O);
338
49.4k
}
339
340
static void printBDXAddrOperand(MCInst *MI, int OpNum, SStream *O)
341
39.6k
{
342
39.6k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
343
39.6k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
344
39.6k
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
345
39.6k
}
346
347
static void printBDLAddrOperand(MCInst *MI, int OpNum, SStream *O)
348
4.51k
{
349
4.51k
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
350
4.51k
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
351
4.51k
  uint64_t Length = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 2));
352
353
4.51k
  if (Disp > HEX_THRESHOLD)
354
4.07k
    SStream_concat(O, "0x%"PRIx64, Disp);
355
434
  else
356
434
    SStream_concat(O, "%"PRIu64, Disp);
357
358
4.51k
  if (Length > HEX_THRESHOLD)
359
3.02k
    SStream_concat(O, "(0x%"PRIx64, Length);
360
1.49k
  else
361
1.49k
    SStream_concat(O, "(%"PRIu64, Length);
362
363
4.51k
  if (Base)
364
3.41k
    SStream_concat(O, ", %%%s", getRegisterName(Base));
365
4.51k
  SStream_concat0(O, ")");
366
367
4.51k
  if (MI->csh->detail) {
368
4.51k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
369
4.51k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
370
4.51k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = Length;
371
4.51k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
372
4.51k
    MI->flat_insn->detail->sysz.op_count++;
373
4.51k
  }
374
4.51k
}
375
376
static void printBDRAddrOperand(MCInst *MI, int OpNum, SStream *O)
377
558
{
378
558
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
379
558
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
380
558
  uint64_t Length = MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2));
381
382
558
  if (Disp > HEX_THRESHOLD)
383
452
    SStream_concat(O, "0x%"PRIx64, Disp);
384
106
  else
385
106
    SStream_concat(O, "%"PRIu64, Disp);
386
387
558
  SStream_concat0(O, "(");
388
558
  SStream_concat(O, "%%%s", getRegisterName(Length));
389
390
558
  if (Base)
391
420
    SStream_concat(O, ", %%%s", getRegisterName(Base));
392
558
  SStream_concat0(O, ")");
393
394
558
  if (MI->csh->detail) {
395
558
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
396
558
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
397
558
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = (uint8_t)SystemZ_map_register(Length);
398
558
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
399
558
    MI->flat_insn->detail->sysz.op_count++;
400
558
  }
401
558
}
402
403
static void printBDVAddrOperand(MCInst *MI, int OpNum, SStream *O)
404
2.23k
{
405
2.23k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
406
2.23k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
407
2.23k
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
408
2.23k
}
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
65.8k
{
430
65.8k
  printInstruction(MI, O, Info);
431
65.8k
}
432
433
#endif