Coverage Report

Created: 2026-01-17 06:58

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