Coverage Report

Created: 2026-01-09 06:55

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