Coverage Report

Created: 2025-10-10 06:20

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