1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*---------------------------------------------------------------*/
4 /*--- begin s390_disasm.c ---*/
5 /*---------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright IBM Corp. 2010-2017
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 /* Contributed by Florian Krohm */
32 #include "libvex_basictypes.h"
33 #include "main_util.h" // vassert
34 #include "main_globals.h" // vex_traceflags
35 #include "s390_defs.h" // S390_MAX_MNEMONIC_LEN
36 #include "s390_disasm.h"
39 /* Return the mnemonic padded with blanks to its right */
41 mnemonic(const HChar
*mnm
)
43 vassert(vex_strlen(mnm
) <= S390_MAX_MNEMONIC_LEN
);
45 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
47 vex_sprintf(buf
, "%-*s", S390_MAX_MNEMONIC_LEN
, mnm
);
53 /* Return the name of a general purpose register for dis-assembly purposes. */
55 gpr_operand(UInt archreg
)
57 static const HChar names
[16][5] = {
58 "%r0", "%r1", "%r2", "%r3",
59 "%r4", "%r5", "%r6", "%r7",
60 "%r8", "%r9", "%r10", "%r11",
61 "%r12", "%r13", "%r14", "%r15",
64 vassert(archreg
< 16);
66 return names
[archreg
];
70 /* Return the name of a floating point register for dis-assembly purposes. */
72 fpr_operand(UInt archreg
)
74 static const HChar names
[16][5] = {
75 "%f0", "%f1", "%f2", "%f3",
76 "%f4", "%f5", "%f6", "%f7",
77 "%f8", "%f9", "%f10", "%f11",
78 "%f12", "%f13", "%f14", "%f15",
81 vassert(archreg
< 16);
83 return names
[archreg
];
87 /* Return the name of an access register for dis-assembly purposes. */
89 ar_operand(UInt archreg
)
91 static const HChar names
[16][5] = {
92 "%a0", "%a1", "%a2", "%a3",
93 "%a4", "%a5", "%a6", "%a7",
94 "%a8", "%a9", "%a10", "%a11",
95 "%a12", "%a13", "%a14", "%a15",
98 vassert(archreg
< 16);
100 return names
[archreg
];
104 /* Build and return the extended mnemonic for the compare and branch
105 opcodes as introduced by z10. See also the opcodes in file
106 opcodes/s390-opc.txt (from binutils) that have a '$' in their name. */
108 cab_operand(const HChar
*base
, UInt mask
)
113 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
115 static const HChar suffix
[8][3] = {
116 "", "h", "l", "ne", "e", "nl", "nh", ""
119 /* Guard against buffer overflow */
120 vassert(vex_strlen(base
) + sizeof suffix
[0] <= sizeof buf
);
122 /* strcpy(buf, from); */
123 for (from
= base
, to
= buf
; *from
; ++from
, ++to
) {
126 /* strcat(buf, suffix); */
127 if (! (mask
& 0x1)) {
128 for (from
= suffix
[mask
>> 1]; *from
; ++from
, ++to
) {
138 /* Return the name of a vector register for dis-assembly purposes. */
140 vr_operand(UInt archreg
)
142 static const HChar names
[32][5] = {
143 "%v0", "%v1", "%v2", "%v3",
144 "%v4", "%v5", "%v6", "%v7",
145 "%v8", "%v9", "%v10", "%v11",
146 "%v12", "%v13", "%v14", "%v15",
147 "%v16", "%v17", "%v18", "%v19",
148 "%v20", "%v21", "%v22", "%v23",
149 "%v24", "%v25", "%v26", "%v27",
150 "%v28", "%v29", "%v30", "%v31",
153 vassert(archreg
< 32);
155 return names
[archreg
];
159 /* Common function used to construct a mnemonic based on a condition code
162 construct_mnemonic(const HChar
*prefix
, const HChar
*suffix
, UInt mask
)
167 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
169 static HChar mask_id
[16][4] = {
170 "", /* 0 -> unused */
171 "o", "h", "nle", "l", "nhe", "lh", "ne",
172 "e", "nlh", "he", "nl", "le", "nh", "no",
173 "" /* 15 -> unused */
176 /* Guard against buffer overflow */
177 vassert(vex_strlen(prefix
) + vex_strlen(suffix
) +
178 sizeof mask_id
[0] <= sizeof buf
);
180 /* strcpy(buf, prefix); */
181 for (from
= prefix
, to
= buf
; *from
; ++from
, ++to
) {
184 /* strcat(buf, mask_id); */
185 for (from
= mask_id
[mask
]; *from
; ++from
, ++to
) {
188 /* strcat(buf, suffix); */
189 for (from
= suffix
; *from
; ++from
, ++to
) {
198 /* Return the special mnemonic for the BCR opcode */
202 if (m1
== 0) return "nopr";
203 if (m1
== 15) return "br";
205 return construct_mnemonic("b", "r", m1
);
209 /* Return the special mnemonic for the BC opcode */
213 if (m1
== 0) return "nop";
214 if (m1
== 15) return "b";
216 return construct_mnemonic("b", "", m1
);
220 /* Return the special mnemonic for the BRC opcode */
224 if (m1
== 0) return "jnop";
225 if (m1
== 15) return "j";
227 return construct_mnemonic("j", "", m1
);
231 /* Return the special mnemonic for the BRCL opcode */
233 brcl_operand(UInt m1
)
235 if (m1
== 0) return "jgnop";
236 if (m1
== 15) return "jg";
238 return construct_mnemonic("jg", "", m1
);
242 /* Return the special mnemonic for a conditional load/store opcode */
244 cls_operand(const HChar
*prefix
, UInt mask
)
246 return construct_mnemonic(prefix
, "", mask
);
250 /* An operand with a base register, an index register, and a displacement.
251 If the displacement is signed, the rightmost 20 bit of D need to be
254 dxb_operand(HChar
*p
, UInt d
, UInt x
, UInt b
, Bool displacement_is_signed
)
256 if (displacement_is_signed
) {
257 Int displ
= (Int
)(d
<< 12) >> 12; /* sign extend */
259 p
+= vex_sprintf(p
, "%d", displ
);
261 p
+= vex_sprintf(p
, "%u", d
);
264 p
+= vex_sprintf(p
, "(%s,%s)", gpr_operand(x
), gpr_operand(b
));
267 p
+= vex_sprintf(p
, "(%s)", gpr_operand(b
));
275 /* An operand with base register, unsigned length, and a 12-bit
276 unsigned displacement */
278 udlb_operand(HChar
*p
, UInt d
, UInt length
, UInt b
)
280 p
+= vex_sprintf(p
, "%u", d
);
281 p
+= vex_sprintf(p
, "(%u", length
+ 1); // actual length is +1
283 p
+= vex_sprintf(p
, ",%s", gpr_operand(b
));
285 p
+= vex_sprintf(p
, ")");
291 /* An operand with a base register, an vector register, and a displacement.
292 If the displacement is signed, the rightmost 20 bit of D need to be
295 dvb_operand(HChar
*p
, UInt d
, UInt v
, UInt b
, Bool displacement_is_signed
)
297 if (displacement_is_signed
) {
298 Int displ
= (Int
)(d
<< 12) >> 12; /* sign extend */
300 p
+= vex_sprintf(p
, "%d", displ
);
302 p
+= vex_sprintf(p
, "%u", d
);
305 p
+= vex_sprintf(p
, "(%s", vr_operand(v
));
307 p
+= vex_sprintf(p
, ",%s", gpr_operand(b
));
309 p
+= vex_sprintf(p
, ")");
312 p
+= vex_sprintf(p
, "(%s)", gpr_operand(b
));
320 /* The first argument is the command that says how to write the disassembled
321 insn. It is understood that the mnemonic comes first and that arguments
322 are separated by a ','. The command holds the arguments. Each argument is
323 encoded using a 4-bit S390_ARG_xyz value. The first argument is placed
324 in the least significant bits of the command and so on. There are at most
325 7 arguments in an insn and a sentinel (S390_ARG_DONE) is needed to identify
326 the end of the argument list. 8 * 4 = 32 bits are required for the
329 s390_disasm(UInt command
, ...)
333 HChar buf
[128]; /* holds the disassembled insn */
336 Int mask_suffix
= -1;
338 va_start(args
, command
);
344 argkind
= command
& 0xF;
347 if (argkind
== S390_ARG_DONE
) goto done
;
349 if (argkind
== S390_ARG_CABM
) separator
= 0; /* optional */
351 /* Write out the separator */
352 if (separator
) *p
++ = separator
;
357 p
+= vex_sprintf(p
, "%s", mnemonic(va_arg(args
, HChar
*)));
361 case S390_ARG_XMNM
: {
365 kind
= va_arg(args
, UInt
);
371 mask
= va_arg(args
, UInt
);
372 mnm
= kind
== S390_XMNM_BCR
? bcr_operand(mask
) : bc_operand(mask
);
373 p
+= vex_sprintf(p
, "%s", mnemonic(mnm
));
378 mask
= va_arg(args
, UInt
);
379 mnm
= kind
== S390_XMNM_BRC
? brc_operand(mask
) : brcl_operand(mask
);
380 p
+= vex_sprintf(p
, "%s", mnemonic(mnm
));
384 mnm
= va_arg(args
, HChar
*);
385 mask
= va_arg(args
, UInt
);
386 p
+= vex_sprintf(p
, "%s", mnemonic(cab_operand(mnm
, mask
)));
390 mnm
= va_arg(args
, HChar
*);
391 mask
= va_arg(args
, UInt
);
392 p
+= vex_sprintf(p
, "%s", mnemonic(cls_operand(mnm
, mask
)));
393 /* There are no special opcodes when mask == 0 or 15. In that case
394 the integer mask is appended as the final operand */
395 if (mask
== 0 || mask
== 15) mask_suffix
= mask
;
399 mask
= va_arg(args
, UInt
);
401 /* There is no special opcode when mask == 0. */
402 p
+= vex_sprintf(p
, "%s", mnemonic("bic"));
403 p
+= vex_sprintf(p
, "%u,", mask
);
405 p
+= vex_sprintf(p
, "%s", construct_mnemonic("bi", "", mask
));
413 p
+= vex_sprintf(p
, "%s", gpr_operand(va_arg(args
, UInt
)));
417 p
+= vex_sprintf(p
, "%s", fpr_operand(va_arg(args
, UInt
)));
421 p
+= vex_sprintf(p
, "%s", ar_operand(va_arg(args
, UInt
)));
425 p
+= vex_sprintf(p
, "%u", va_arg(args
, UInt
));
429 p
+= vex_sprintf(p
, "%d", va_arg(args
, Int
));
432 case S390_ARG_PCREL
: {
433 Long offset
= va_arg(args
, Int
);
435 /* Convert # halfwords to # bytes */
439 p
+= vex_sprintf(p
, ".%lld", offset
);
441 p
+= vex_sprintf(p
, ".+%lld", offset
);
446 case S390_ARG_SDXB
: {
449 dh
= va_arg(args
, UInt
);
450 dl
= va_arg(args
, UInt
);
451 x
= va_arg(args
, UInt
);
452 b
= va_arg(args
, UInt
);
454 p
= dxb_operand(p
, (dh
<< 12) | dl
, x
, b
, 1 /* signed_displacement */);
458 case S390_ARG_UDXB
: {
461 d
= va_arg(args
, UInt
);
462 x
= va_arg(args
, UInt
);
463 b
= va_arg(args
, UInt
);
465 p
= dxb_operand(p
, d
, x
, b
, 0 /* signed_displacement */);
469 case S390_ARG_UDLB
: {
472 d
= va_arg(args
, UInt
);
473 l
= va_arg(args
, UInt
);
474 b
= va_arg(args
, UInt
);
476 p
= udlb_operand(p
, d
, l
, b
);
480 case S390_ARG_CABM
: {
483 mask
= va_arg(args
, UInt
);
484 if (mask
== 0 || mask
== 14 || (mask
& 0x1)) {
485 p
+= vex_sprintf(p
, ",%u", mask
);
491 p
+= vex_sprintf(p
, "%s", vr_operand(va_arg(args
, UInt
)));
494 case S390_ARG_UDVB
: {
497 d
= va_arg(args
, UInt
);
498 v
= va_arg(args
, UInt
);
499 b
= va_arg(args
, UInt
);
501 p
= dvb_operand(p
, d
, v
, b
, 0 /* signed_displacement */);
512 if (mask_suffix
!= -1)
513 p
+= vex_sprintf(p
, ",%d", mask_suffix
);
516 vassert(p
< buf
+ sizeof buf
); /* detect buffer overwrite */
518 /* Finally, write out the disassembled insn */
519 vex_printf("%s\n", buf
);
522 /*---------------------------------------------------------------*/
523 /*--- end s390_disasm.c ---*/
524 /*---------------------------------------------------------------*/