drd/tests/tsan_thread_wrappers_pthread.h: Fix MyThread::ThreadBody()
[valgrind.git] / VEX / priv / s390_disasm.c
blob58189f1233577b27b2abac7dbf21de3d73354c4c
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*---------------------------------------------------------------*/
4 /*--- begin s390_disasm.c ---*/
5 /*---------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
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
26 02110-1301, USA.
28 The GNU General Public License is contained in the file COPYING.
31 /* Contributed by Florian Krohm */
33 #include <stdarg.h>
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 */
42 static const HChar *
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);
51 return buf;
55 /* Return the name of a general purpose register for dis-assembly purposes. */
56 static const HChar *
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. */
73 static const HChar *
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. */
90 static const HChar *
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. */
109 static const HChar *
110 cab_operand(const HChar *base, UInt mask)
112 HChar *to;
113 const HChar *from;
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) {
126 *to = *from;
128 /* strcat(buf, suffix); */
129 for (from = suffix[mask >> 1]; *from; ++from, ++to) {
130 *to = *from;
132 *to = '\0';
134 return buf;
138 /* Return the name of a vector register for dis-assembly purposes. */
139 static const HChar *
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
159 mask. */
160 static const HChar *
161 construct_mnemonic(const HChar *prefix, const HChar *suffix, UInt mask)
163 HChar *to;
164 const HChar *from;
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) {
181 *to = *from;
183 /* strcat(buf, mask_id); */
184 for (from = mask_id[mask]; *from; ++from, ++to) {
185 *to = *from;
187 /* strcat(buf, suffix); */
188 for (from = suffix; *from; ++from, ++to) {
189 *to = *from;
191 *to = '\0';
193 return buf;
197 /* Return the special mnemonic for the BCR opcode */
198 static const HChar *
199 bcr_operand(UInt m1)
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 */
209 static const HChar *
210 bc_operand(UInt m1)
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 */
220 static const HChar *
221 brc_operand(UInt m1)
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 */
231 static const HChar *
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 */
242 static const HChar *
243 cls_operand(Int kind, UInt mask)
245 const HChar *prefix;
247 switch (kind) {
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;
260 default:
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
270 sign extended */
271 static HChar *
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);
278 } else {
279 p += vex_sprintf(p, "%u", d);
281 if (x != 0) {
282 p += vex_sprintf(p, "(%s", gpr_operand(x));
283 if (b != 0) {
284 p += vex_sprintf(p, ",%s", gpr_operand(b));
286 p += vex_sprintf(p, ")");
287 } else {
288 if (b != 0) {
289 p += vex_sprintf(p, "(%s)", gpr_operand(b));
293 return p;
297 /* An operand with base register, unsigned length, and a 12-bit
298 unsigned displacement */
299 static HChar *
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
304 if (b != 0) {
305 p += vex_sprintf(p, ",%s", gpr_operand(b));
307 p += vex_sprintf(p, ")");
309 return 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
315 sign extended */
316 static HChar *
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);
323 } else {
324 p += vex_sprintf(p, "%u", d);
326 if (v != 0) {
327 p += vex_sprintf(p, "(%s", vr_operand(v));
328 if (b != 0) {
329 p += vex_sprintf(p, ",%s", gpr_operand(b));
331 p += vex_sprintf(p, ")");
332 } else {
333 if (b != 0) {
334 p += vex_sprintf(p, "(%s)", gpr_operand(b));
338 return p;
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
349 command. */
350 void
351 s390_disasm(UInt command, ...)
353 va_list args;
354 UInt argkind;
355 HChar buf[128]; /* holds the disassembled insn */
356 HChar *p;
357 HChar separator;
358 Int mask_suffix = -1;
360 va_start(args, command);
362 p = buf;
363 separator = 0;
365 while (42) {
366 argkind = command & 0xF;
367 command >>= 4;
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;
376 /* argument */
377 switch (argkind) {
378 case S390_ARG_MNM:
379 p += vex_sprintf(p, "%s", mnemonic(va_arg(args, HChar *)));
380 separator = ' ';
381 continue;
383 case S390_ARG_XMNM: {
384 UInt mask, kind;
385 const HChar *mnm;
387 kind = va_arg(args, UInt);
389 separator = ' ';
390 switch (kind) {
391 case S390_XMNM_BC:
392 case S390_XMNM_BCR:
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;
398 break;
400 case S390_XMNM_BRC:
401 case S390_XMNM_BRCL:
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 */
407 if (mask == 0) {
408 p += vex_sprintf(p, " 0");
409 separator = ',';
411 break;
413 case S390_XMNM_CAB:
414 mnm = va_arg(args, HChar *);
415 mask = va_arg(args, UInt);
416 p += vex_sprintf(p, "%s", mnemonic(cab_operand(mnm, mask)));
417 break;
419 case S390_XMNM_LOCR:
420 case S390_XMNM_LOCGR:
421 case S390_XMNM_LOC:
422 case S390_XMNM_LOCG:
423 case S390_XMNM_STOC:
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;
437 break;
440 continue;
442 case S390_ARG_GPR:
443 p += vex_sprintf(p, "%s", gpr_operand(va_arg(args, UInt)));
444 break;
446 case S390_ARG_FPR:
447 p += vex_sprintf(p, "%s", fpr_operand(va_arg(args, UInt)));
448 break;
450 case S390_ARG_AR:
451 p += vex_sprintf(p, "%s", ar_operand(va_arg(args, UInt)));
452 break;
454 case S390_ARG_UINT:
455 p += vex_sprintf(p, "%u", va_arg(args, UInt));
456 break;
458 case S390_ARG_INT:
459 p += vex_sprintf(p, "%d", (Int)(va_arg(args, UInt)));
460 break;
462 case S390_ARG_PCREL: {
463 Long offset = va_arg(args, Int);
465 /* Convert # halfwords to # bytes */
466 offset <<= 1;
468 if (offset < 0) {
469 p += vex_sprintf(p, ".%lld", offset);
470 } else {
471 p += vex_sprintf(p, ".+%lld", offset);
473 break;
476 case S390_ARG_SDXB: {
477 UInt dh, dl, x, b;
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 */);
485 break;
488 case S390_ARG_UDXB: {
489 UInt d, x, b;
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 */);
496 break;
499 case S390_ARG_UDLB: {
500 UInt d, l, b;
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);
507 break;
510 case S390_ARG_CABM: {
511 UInt mask;
513 mask = va_arg(args, UInt) & 0xE;
514 if (mask == 0 || mask == 14) {
515 p += vex_sprintf(p, ",%u", mask);
517 break;
520 case S390_ARG_VR:
521 p += vex_sprintf(p, "%s", vr_operand(va_arg(args, UInt)));
522 break;
524 case S390_ARG_UDVB: {
525 UInt d, v, b;
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 */);
532 break;
536 separator = ',';
539 done:
540 va_end(args);
542 if (mask_suffix != -1)
543 p += vex_sprintf(p, ",%d", mask_suffix);
544 *p = '\0';
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 /*---------------------------------------------------------------*/