Coverage Report

Created: 2025-07-09 06:32

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