Coverage Report

Created: 2025-08-26 06:30

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