Coverage Report

Created: 2025-07-18 06:43

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