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, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 /* Contributed by Florian Krohm */
34 #include "libvex_basictypes.h"
35 #include "main_util.h" // vassert
36 #include "main_globals.h" // vex_traceflags
37 #include "s390_defs.h" // S390_MAX_MNEMONIC_LEN
38 #include "s390_disasm.h"
41 /* Return the mnemonic padded with blanks to its right */
43 mnemonic(const HChar
*mnm
)
45 vassert(vex_strlen(mnm
) <= S390_MAX_MNEMONIC_LEN
);
47 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
49 vex_sprintf(buf
, "%-*s", S390_MAX_MNEMONIC_LEN
, mnm
);
55 /* Return the name of a general purpose register for dis-assembly purposes. */
57 gpr_operand(UInt archreg
)
59 static const HChar names
[16][5] = {
60 "%r0", "%r1", "%r2", "%r3",
61 "%r4", "%r5", "%r6", "%r7",
62 "%r8", "%r9", "%r10", "%r11",
63 "%r12", "%r13", "%r14", "%r15",
66 vassert(archreg
< 16);
68 return names
[archreg
];
72 /* Return the name of a floating point register for dis-assembly purposes. */
74 fpr_operand(UInt archreg
)
76 static const HChar names
[16][5] = {
77 "%f0", "%f1", "%f2", "%f3",
78 "%f4", "%f5", "%f6", "%f7",
79 "%f8", "%f9", "%f10", "%f11",
80 "%f12", "%f13", "%f14", "%f15",
83 vassert(archreg
< 16);
85 return names
[archreg
];
89 /* Return the name of an access register for dis-assembly purposes. */
91 ar_operand(UInt archreg
)
93 static const HChar names
[16][5] = {
94 "%a0", "%a1", "%a2", "%a3",
95 "%a4", "%a5", "%a6", "%a7",
96 "%a8", "%a9", "%a10", "%a11",
97 "%a12", "%a13", "%a14", "%a15",
100 vassert(archreg
< 16);
102 return names
[archreg
];
106 /* Build and return the extended mnemonic for the compare and branch
107 opcodes as introduced by z10. See also the opcodes in file
108 opcodes/s390-opc.txt (from binutils) that have a '$' in their name. */
110 cab_operand(const HChar
*base
, UInt mask
)
115 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
117 static const HChar suffix
[8][3] = {
118 "", "h", "l", "ne", "e", "nl", "nh", ""
121 /* Guard against buffer overflow */
122 vassert(vex_strlen(base
) + sizeof suffix
[0] <= sizeof buf
);
124 /* strcpy(buf, from); */
125 for (from
= base
, to
= buf
; *from
; ++from
, ++to
) {
128 /* strcat(buf, suffix); */
129 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 "%v16", "%v17", "%v18", "%v19",
147 "%v20", "%v21", "%v22", "%v23",
148 "%v24", "%v25", "%v26", "%v27",
149 "%v28", "%v29", "%v30", "%v31",
152 vassert(archreg
< 32);
154 return names
[archreg
];
158 /* Common function used to construct a mnemonic based on a condition code
161 construct_mnemonic(const HChar
*prefix
, const HChar
*suffix
, UInt mask
)
166 static HChar buf
[S390_MAX_MNEMONIC_LEN
+ 1];
168 static HChar mask_id
[16][4] = {
169 "", /* 0 -> unused */
170 "o", "h", "nle", "l", "nhe", "lh", "ne",
171 "e", "nlh", "he", "nl", "le", "nh", "no",
172 "" /* 15 -> unused */
175 /* Guard against buffer overflow */
176 vassert(vex_strlen(prefix
) + vex_strlen(suffix
) +
177 sizeof mask_id
[0] <= sizeof buf
);
179 /* strcpy(buf, prefix); */
180 for (from
= prefix
, to
= buf
; *from
; ++from
, ++to
) {
183 /* strcat(buf, mask_id); */
184 for (from
= mask_id
[mask
]; *from
; ++from
, ++to
) {
187 /* strcat(buf, suffix); */
188 for (from
= suffix
; *from
; ++from
, ++to
) {
197 /* Return the special mnemonic for the BCR opcode */
201 if (m1
== 0) return "nopr";
202 if (m1
== 15) return "br";
204 return construct_mnemonic("b", "r", m1
);
208 /* Return the special mnemonic for the BC opcode */
212 if (m1
== 0) return "nop";
213 if (m1
== 15) return "b";
215 return construct_mnemonic("b", "", m1
);
219 /* Return the special mnemonic for the BRC opcode */
223 if (m1
== 0) return "brc";
224 if (m1
== 15) return "j";
226 return construct_mnemonic("j", "", m1
);
230 /* Return the special mnemonic for the BRCL opcode */
232 brcl_operand(UInt m1
)
234 if (m1
== 0) return "brcl";
235 if (m1
== 15) return "jg";
237 return construct_mnemonic("jg", "", m1
);
241 /* Return the special mnemonic for a conditional load/store opcode */
243 cls_operand(Int kind
, UInt mask
)
248 case S390_XMNM_LOCR
: prefix
= "locr"; break;
249 case S390_XMNM_LOCGR
: prefix
= "locgr"; break;
250 case S390_XMNM_LOC
: prefix
= "loc"; break;
251 case S390_XMNM_LOCG
: prefix
= "locg"; break;
252 case S390_XMNM_STOC
: prefix
= "stoc"; break;
253 case S390_XMNM_STOCG
: prefix
= "stocg"; break;
254 case S390_XMNM_STOCFH
: prefix
= "stocfh"; break;
255 case S390_XMNM_LOCFH
: prefix
= "locgh"; break;
256 case S390_XMNM_LOCFHR
: prefix
= "locghr"; break;
257 case S390_XMNM_LOCHI
: prefix
= "lochi"; break;
258 case S390_XMNM_LOCGHI
: prefix
= "locghi"; break;
259 case S390_XMNM_LOCHHI
: prefix
= "lochhi"; break;
261 vpanic("cls_operand");
264 return construct_mnemonic(prefix
, "", mask
);
268 /* An operand with a base register, an index register, and a displacement.
269 If the displacement is signed, the rightmost 20 bit of D need to be
272 dxb_operand(HChar
*p
, UInt d
, UInt x
, UInt b
, Bool displacement_is_signed
)
274 if (displacement_is_signed
) {
275 Int displ
= (Int
)(d
<< 12) >> 12; /* sign extend */
277 p
+= vex_sprintf(p
, "%d", displ
);
279 p
+= vex_sprintf(p
, "%u", d
);
282 p
+= vex_sprintf(p
, "(%s", gpr_operand(x
));
284 p
+= vex_sprintf(p
, ",%s", gpr_operand(b
));
286 p
+= vex_sprintf(p
, ")");
289 p
+= vex_sprintf(p
, "(%s)", gpr_operand(b
));
297 /* An operand with base register, unsigned length, and a 12-bit
298 unsigned displacement */
300 udlb_operand(HChar
*p
, UInt d
, UInt length
, UInt b
)
302 p
+= vex_sprintf(p
, "%u", d
);
303 p
+= vex_sprintf(p
, "(%u", length
+ 1); // actual length is +1
305 p
+= vex_sprintf(p
, ",%s", gpr_operand(b
));
307 p
+= vex_sprintf(p
, ")");
313 /* An operand with a base register, an vector register, and a displacement.
314 If the displacement is signed, the rightmost 20 bit of D need to be
317 dvb_operand(HChar
*p
, UInt d
, UInt v
, UInt b
, Bool displacement_is_signed
)
319 if (displacement_is_signed
) {
320 Int displ
= (Int
)(d
<< 12) >> 12; /* sign extend */
322 p
+= vex_sprintf(p
, "%d", displ
);
324 p
+= vex_sprintf(p
, "%u", d
);
327 p
+= vex_sprintf(p
, "(%s", vr_operand(v
));
329 p
+= vex_sprintf(p
, ",%s", gpr_operand(b
));
331 p
+= vex_sprintf(p
, ")");
334 p
+= vex_sprintf(p
, "(%s)", gpr_operand(b
));
342 /* The first argument is the command that says how to write the disassembled
343 insn. It is understood that the mnemonic comes first and that arguments
344 are separated by a ','. The command holds the arguments. Each argument is
345 encoded using a 4-bit S390_ARG_xyz value. The first argument is placed
346 in the least significant bits of the command and so on. There are at most
347 7 arguments in an insn and a sentinel (S390_ARG_DONE) is needed to identify
348 the end of the argument list. 8 * 4 = 32 bits are required for the
351 s390_disasm(UInt command
, ...)
355 HChar buf
[128]; /* holds the disassembled insn */
358 Int mask_suffix
= -1;
360 va_start(args
, command
);
366 argkind
= command
& 0xF;
369 if (argkind
== S390_ARG_DONE
) goto done
;
371 if (argkind
== S390_ARG_CABM
) separator
= 0; /* optional */
373 /* Write out the separator */
374 if (separator
) *p
++ = separator
;
379 p
+= vex_sprintf(p
, "%s", mnemonic(va_arg(args
, HChar
*)));
383 case S390_ARG_XMNM
: {
387 kind
= va_arg(args
, UInt
);
393 mask
= va_arg(args
, UInt
);
394 mnm
= kind
== S390_XMNM_BCR
? bcr_operand(mask
) : bc_operand(mask
);
395 p
+= vex_sprintf(p
, "%s", mnemonic(mnm
));
396 /* mask == 0 is a NOP and has no argument */
397 if (mask
== 0) goto done
;
402 mask
= va_arg(args
, UInt
);
403 mnm
= kind
== S390_XMNM_BRC
? brc_operand(mask
) : brcl_operand(mask
);
404 p
+= vex_sprintf(p
, "%s", mnemonic(mnm
));
406 /* mask == 0 has no special mnemonic */
408 p
+= vex_sprintf(p
, " 0");
414 mnm
= va_arg(args
, HChar
*);
415 mask
= va_arg(args
, UInt
);
416 p
+= vex_sprintf(p
, "%s", mnemonic(cab_operand(mnm
, mask
)));
420 case S390_XMNM_LOCGR
:
424 case S390_XMNM_STOCG
:
425 case S390_XMNM_STOCFH
:
426 case S390_XMNM_LOCFH
:
427 case S390_XMNM_LOCFHR
:
428 case S390_XMNM_LOCHI
:
429 case S390_XMNM_LOCGHI
:
430 case S390_XMNM_LOCHHI
:
431 mask
= va_arg(args
, UInt
);
432 mnm
= cls_operand(kind
, mask
);
433 p
+= vex_sprintf(p
, "%s", mnemonic(mnm
));
434 /* There are no special opcodes when mask == 0 or 15. In that case
435 the integer mask is appended as the final operand */
436 if (mask
== 0 || mask
== 15) mask_suffix
= mask
;
443 p
+= vex_sprintf(p
, "%s", gpr_operand(va_arg(args
, UInt
)));
447 p
+= vex_sprintf(p
, "%s", fpr_operand(va_arg(args
, UInt
)));
451 p
+= vex_sprintf(p
, "%s", ar_operand(va_arg(args
, UInt
)));
455 p
+= vex_sprintf(p
, "%u", va_arg(args
, UInt
));
459 p
+= vex_sprintf(p
, "%d", (Int
)(va_arg(args
, UInt
)));
462 case S390_ARG_PCREL
: {
463 Long offset
= va_arg(args
, Int
);
465 /* Convert # halfwords to # bytes */
469 p
+= vex_sprintf(p
, ".%lld", offset
);
471 p
+= vex_sprintf(p
, ".+%lld", offset
);
476 case S390_ARG_SDXB
: {
479 dh
= va_arg(args
, UInt
);
480 dl
= va_arg(args
, UInt
);
481 x
= va_arg(args
, UInt
);
482 b
= va_arg(args
, UInt
);
484 p
= dxb_operand(p
, (dh
<< 12) | dl
, x
, b
, 1 /* signed_displacement */);
488 case S390_ARG_UDXB
: {
491 d
= va_arg(args
, UInt
);
492 x
= va_arg(args
, UInt
);
493 b
= va_arg(args
, UInt
);
495 p
= dxb_operand(p
, d
, x
, b
, 0 /* signed_displacement */);
499 case S390_ARG_UDLB
: {
502 d
= va_arg(args
, UInt
);
503 l
= va_arg(args
, UInt
);
504 b
= va_arg(args
, UInt
);
506 p
= udlb_operand(p
, d
, l
, b
);
510 case S390_ARG_CABM
: {
513 mask
= va_arg(args
, UInt
) & 0xE;
514 if (mask
== 0 || mask
== 14) {
515 p
+= vex_sprintf(p
, ",%u", mask
);
521 p
+= vex_sprintf(p
, "%s", vr_operand(va_arg(args
, UInt
)));
524 case S390_ARG_UDVB
: {
527 d
= va_arg(args
, UInt
);
528 v
= va_arg(args
, UInt
);
529 b
= va_arg(args
, UInt
);
531 p
= dvb_operand(p
, d
, v
, b
, 0 /* signed_displacement */);
542 if (mask_suffix
!= -1)
543 p
+= vex_sprintf(p
, ",%d", mask_suffix
);
546 vassert(p
< buf
+ sizeof buf
); /* detect buffer overwrite */
548 /* Finally, write out the disassembled insn */
549 vex_printf("%s\n", buf
);
552 /*---------------------------------------------------------------*/
553 /*--- end s390_disasm.c ---*/
554 /*---------------------------------------------------------------*/