Coverage Report

Created: 2025-07-11 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
96.7k
{
36
  /*
37
     if (((cs_struct *)ud)->detail != CS_OPT_ON)
38
     return;
39
   */
40
96.7k
}
41
42
static void printAddress(MCInst *MI, unsigned Base, int64_t Disp, unsigned Index, SStream *O)
43
39.4k
{
44
39.4k
  printInt64(O, Disp);
45
46
39.4k
  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.90k
    if (MI->csh->detail) {
61
4.90k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
62
4.90k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Disp;
63
4.90k
      MI->flat_insn->detail->sysz.op_count++;
64
4.90k
    }
65
4.90k
  } else {
66
2.50k
    SStream_concat(O, "(%%%s)", getRegisterName(Index));
67
2.50k
    if (MI->csh->detail) {
68
2.50k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
69
2.50k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
70
2.50k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.index = (uint8_t)SystemZ_map_register(Index);
71
2.50k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = Disp;
72
2.50k
      MI->flat_insn->detail->sysz.op_count++;
73
2.50k
    }
74
2.50k
  }
75
39.4k
}
76
77
static void _printOperand(MCInst *MI, MCOperand *MO, SStream *O)
78
142k
{
79
142k
  if (MCOperand_isReg(MO)) {
80
142k
    unsigned reg;
81
82
142k
    reg = MCOperand_getReg(MO);
83
142k
    SStream_concat(O, "%%%s", getRegisterName(reg));
84
142k
    reg = SystemZ_map_register(reg);
85
86
142k
    if (MI->csh->detail) {
87
142k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_REG;
88
142k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].reg = reg;
89
142k
      MI->flat_insn->detail->sysz.op_count++;
90
142k
    }
91
142k
  } 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
142k
}
103
104
static void printU1ImmOperand(MCInst *MI, int OpNum, SStream *O)
105
455
{
106
455
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
107
  // assert(isUInt<1>(Value) && "Invalid u1imm argument");
108
455
  printInt64(O, Value);
109
110
455
  if (MI->csh->detail) {
111
455
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
112
455
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
113
455
    MI->flat_insn->detail->sysz.op_count++;
114
455
  }
115
455
}
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
97
{
132
97
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
133
  // assert(isUInt<3>(Value) && "Invalid u4imm argument");
134
97
  printInt64(O, Value);
135
136
97
  if (MI->csh->detail) {
137
97
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
138
97
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
139
97
    MI->flat_insn->detail->sysz.op_count++;
140
97
  }
141
97
}
142
143
static void printU4ImmOperand(MCInst *MI, int OpNum, SStream *O)
144
22.9k
{
145
22.9k
  int64_t Value = MCOperand_getImm(MCInst_getOperand(MI, OpNum));
146
  // assert(isUInt<4>(Value) && "Invalid u4imm argument");
147
22.9k
  printInt64(O, Value);
148
149
22.9k
  if (MI->csh->detail) {
150
22.9k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
151
22.9k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = Value;
152
22.9k
    MI->flat_insn->detail->sysz.op_count++;
153
22.9k
  }
154
22.9k
}
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
778
    if (Value > HEX_THRESHOLD)
177
671
      SStream_concat(O, "0x%x", Value);
178
107
    else
179
107
      SStream_concat(O, "%u", Value);
180
979
  } else {
181
979
    if (Value < -HEX_THRESHOLD)
182
557
      SStream_concat(O, "-0x%x", -Value);
183
422
    else
184
422
      SStream_concat(O, "-%u", -Value);
185
979
  }
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.05k
{
196
6.05k
  uint8_t Value = (uint8_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
197
  // assert(isUInt<8>(Value) && "Invalid u8imm argument");
198
199
6.05k
  if (Value > HEX_THRESHOLD)
200
5.21k
    SStream_concat(O, "0x%x", Value);
201
834
  else
202
834
    SStream_concat(O, "%u", Value);
203
204
6.05k
  if (MI->csh->detail) {
205
6.05k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
206
6.05k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
207
6.05k
    MI->flat_insn->detail->sysz.op_count++;
208
6.05k
  }
209
6.05k
}
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.32k
{
226
3.32k
  int16_t Value = (int16_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
227
  // assert(isInt<16>(Value) && "Invalid s16imm argument");
228
229
3.32k
  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
506
    if (Value < -HEX_THRESHOLD)
236
344
      SStream_concat(O, "-0x%x", -Value);
237
162
    else
238
162
      SStream_concat(O, "-%u", -Value);
239
506
  }
240
241
3.32k
  if (MI->csh->detail) {
242
3.32k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
243
3.32k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
244
3.32k
    MI->flat_insn->detail->sysz.op_count++;
245
3.32k
  }
246
3.32k
}
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
276
  else
256
276
    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
919
{
281
919
  uint32_t Value = (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum));
282
  // assert(isUInt<32>(Value) && "Invalid u32imm argument");
283
284
919
  printUInt32(O, Value);
285
286
919
  if (MI->csh->detail) {
287
919
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
288
919
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = (int64_t)Value;
289
919
    MI->flat_insn->detail->sysz.op_count++;
290
919
  }
291
919
}
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.03k
{
308
5.03k
  MCOperand *MO = MCInst_getOperand(MI, OpNum);
309
310
5.03k
  if (MCOperand_isImm(MO)) {
311
5.03k
    int64_t imm = (int64_t)MCOperand_getImm(MO);
312
313
5.03k
    printInt64(O, imm);
314
315
5.03k
    if (MI->csh->detail) {
316
5.03k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_IMM;
317
5.03k
      MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].imm = imm;
318
5.03k
      MI->flat_insn->detail->sysz.op_count++;
319
5.03k
    }
320
5.03k
  }
321
5.03k
}
322
323
static void printPCRelTLSOperand(MCInst *MI, int OpNum, SStream *O)
324
243
{
325
  // Output the PC-relative operand.
326
243
  printPCRelOperand(MI, OpNum, O);
327
243
}
328
329
static void printOperand(MCInst *MI, int OpNum, SStream *O)
330
324k
{
331
324k
  _printOperand(MI, MCInst_getOperand(MI, OpNum), O);
332
324k
}
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
47.9k
{
342
47.9k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
343
47.9k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
344
47.9k
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
345
47.9k
}
346
347
static void printBDLAddrOperand(MCInst *MI, int OpNum, SStream *O)
348
8.48k
{
349
8.48k
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
350
8.48k
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
351
8.48k
  uint64_t Length = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 2));
352
353
8.48k
  if (Disp > HEX_THRESHOLD)
354
8.15k
    SStream_concat(O, "0x%"PRIx64, Disp);
355
332
  else
356
332
    SStream_concat(O, "%"PRIu64, Disp);
357
358
8.48k
  if (Length > HEX_THRESHOLD)
359
5.97k
    SStream_concat(O, "(0x%"PRIx64, Length);
360
2.50k
  else
361
2.50k
    SStream_concat(O, "(%"PRIu64, Length);
362
363
8.48k
  if (Base)
364
6.61k
    SStream_concat(O, ", %%%s", getRegisterName(Base));
365
8.48k
  SStream_concat0(O, ")");
366
367
8.48k
  if (MI->csh->detail) {
368
8.48k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
369
8.48k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
370
8.48k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = Length;
371
8.48k
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
372
8.48k
    MI->flat_insn->detail->sysz.op_count++;
373
8.48k
  }
374
8.48k
}
375
376
static void printBDRAddrOperand(MCInst *MI, int OpNum, SStream *O)
377
706
{
378
706
  unsigned Base = MCOperand_getReg(MCInst_getOperand(MI, OpNum));
379
706
  uint64_t Disp = (uint64_t)MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1));
380
706
  uint64_t Length = MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2));
381
382
706
  if (Disp > HEX_THRESHOLD)
383
603
    SStream_concat(O, "0x%"PRIx64, Disp);
384
103
  else
385
103
    SStream_concat(O, "%"PRIu64, Disp);
386
387
706
  SStream_concat0(O, "(");
388
706
  SStream_concat(O, "%%%s", getRegisterName(Length));
389
390
706
  if (Base)
391
557
    SStream_concat(O, ", %%%s", getRegisterName(Base));
392
706
  SStream_concat0(O, ")");
393
394
706
  if (MI->csh->detail) {
395
706
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].type = SYSZ_OP_MEM;
396
706
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.base = (uint8_t)SystemZ_map_register(Base);
397
706
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.length = (uint8_t)SystemZ_map_register(Length);
398
706
    MI->flat_insn->detail->sysz.operands[MI->flat_insn->detail->sysz.op_count].mem.disp = (int64_t)Disp;
399
706
    MI->flat_insn->detail->sysz.op_count++;
400
706
  }
401
706
}
402
403
static void printBDVAddrOperand(MCInst *MI, int OpNum, SStream *O)
404
1.73k
{
405
1.73k
  printAddress(MI, MCOperand_getReg(MCInst_getOperand(MI, OpNum)),
406
1.73k
      MCOperand_getImm(MCInst_getOperand(MI, OpNum + 1)),
407
1.73k
      MCOperand_getReg(MCInst_getOperand(MI, OpNum + 2)), O);
408
1.73k
}
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
96.7k
{
430
96.7k
  printInstruction(MI, O, Info);
431
96.7k
}
432
433
#endif