Add 469782 to NEWS
[valgrind.git] / VEX / priv / s390_disasm.c
blobb4ff13c23d2ef5e00dff55f0d3202657d0303c82
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, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 /* Contributed by Florian Krohm */
31 #include <stdarg.h>
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 */
40 static const HChar *
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);
49 return buf;
53 /* Return the name of a general purpose register for dis-assembly purposes. */
54 static const HChar *
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. */
71 static const HChar *
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. */
88 static const HChar *
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. */
107 static const HChar *
108 cab_operand(const HChar *base, UInt mask)
110 HChar *to;
111 const HChar *from;
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) {
124 *to = *from;
126 /* strcat(buf, suffix); */
127 if (! (mask & 0x1)) {
128 for (from = suffix[mask >> 1]; *from; ++from, ++to) {
129 *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 "%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
160 mask. */
161 static const HChar *
162 construct_mnemonic(const HChar *prefix, const HChar *suffix, UInt mask)
164 HChar *to;
165 const HChar *from;
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) {
182 *to = *from;
184 /* strcat(buf, mask_id); */
185 for (from = mask_id[mask]; *from; ++from, ++to) {
186 *to = *from;
188 /* strcat(buf, suffix); */
189 for (from = suffix; *from; ++from, ++to) {
190 *to = *from;
192 *to = '\0';
194 return buf;
198 /* Return the special mnemonic for the BCR opcode */
199 static const HChar *
200 bcr_operand(UInt m1)
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 */
210 static const HChar *
211 bc_operand(UInt m1)
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 */
221 static const HChar *
222 brc_operand(UInt m1)
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 */
232 static const HChar *
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 */
243 static const HChar *
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
252 sign extended */
253 static HChar *
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);
260 } else {
261 p += vex_sprintf(p, "%u", d);
263 if (x != 0) {
264 p += vex_sprintf(p, "(%s,%s)", gpr_operand(x), gpr_operand(b));
265 } else {
266 if (b != 0) {
267 p += vex_sprintf(p, "(%s)", gpr_operand(b));
271 return p;
275 /* An operand with base register, unsigned length, and a 12-bit
276 unsigned displacement */
277 static HChar *
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
282 if (b != 0) {
283 p += vex_sprintf(p, ",%s", gpr_operand(b));
285 p += vex_sprintf(p, ")");
287 return 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
293 sign extended */
294 static HChar *
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);
301 } else {
302 p += vex_sprintf(p, "%u", d);
304 if (v != 0) {
305 p += vex_sprintf(p, "(%s", vr_operand(v));
306 if (b != 0) {
307 p += vex_sprintf(p, ",%s", gpr_operand(b));
309 p += vex_sprintf(p, ")");
310 } else {
311 if (b != 0) {
312 p += vex_sprintf(p, "(%s)", gpr_operand(b));
316 return p;
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
327 command. */
328 void
329 s390_disasm(UInt command, ...)
331 va_list args;
332 UInt argkind;
333 HChar buf[128]; /* holds the disassembled insn */
334 HChar *p;
335 HChar separator;
336 Int mask_suffix = -1;
338 va_start(args, command);
340 p = buf;
341 separator = 0;
343 while (42) {
344 argkind = command & 0xF;
345 command >>= 4;
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;
354 /* argument */
355 switch (argkind) {
356 case S390_ARG_MNM:
357 p += vex_sprintf(p, "%s", mnemonic(va_arg(args, HChar *)));
358 separator = ' ';
359 continue;
361 case S390_ARG_XMNM: {
362 UInt mask, kind;
363 const HChar *mnm;
365 kind = va_arg(args, UInt);
367 separator = ' ';
368 switch (kind) {
369 case S390_XMNM_BC:
370 case S390_XMNM_BCR:
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));
374 break;
376 case S390_XMNM_BRC:
377 case S390_XMNM_BRCL:
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));
381 break;
383 case S390_XMNM_CAB:
384 mnm = va_arg(args, HChar *);
385 mask = va_arg(args, UInt);
386 p += vex_sprintf(p, "%s", mnemonic(cab_operand(mnm, mask)));
387 break;
389 case S390_XMNM_CLS:
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;
396 break;
398 case S390_XMNM_BIC:
399 mask = va_arg(args, UInt);
400 if (mask == 0) {
401 /* There is no special opcode when mask == 0. */
402 p += vex_sprintf(p, "%s", mnemonic("bic"));
403 p += vex_sprintf(p, "%u,", mask);
404 } else {
405 p += vex_sprintf(p, "%s", construct_mnemonic("bi", "", mask));
407 break;
410 continue;
412 case S390_ARG_GPR:
413 p += vex_sprintf(p, "%s", gpr_operand(va_arg(args, UInt)));
414 break;
416 case S390_ARG_FPR:
417 p += vex_sprintf(p, "%s", fpr_operand(va_arg(args, UInt)));
418 break;
420 case S390_ARG_AR:
421 p += vex_sprintf(p, "%s", ar_operand(va_arg(args, UInt)));
422 break;
424 case S390_ARG_UINT:
425 p += vex_sprintf(p, "%u", va_arg(args, UInt));
426 break;
428 case S390_ARG_INT:
429 p += vex_sprintf(p, "%d", va_arg(args, Int));
430 break;
432 case S390_ARG_PCREL: {
433 Long offset = va_arg(args, Int);
435 /* Convert # halfwords to # bytes */
436 offset <<= 1;
438 if (offset < 0) {
439 p += vex_sprintf(p, ".%lld", offset);
440 } else {
441 p += vex_sprintf(p, ".+%lld", offset);
443 break;
446 case S390_ARG_SDXB: {
447 UInt dh, dl, x, b;
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 */);
455 break;
458 case S390_ARG_UDXB: {
459 UInt d, x, b;
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 */);
466 break;
469 case S390_ARG_UDLB: {
470 UInt d, l, b;
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);
477 break;
480 case S390_ARG_CABM: {
481 UInt mask;
483 mask = va_arg(args, UInt);
484 if (mask == 0 || mask == 14 || (mask & 0x1)) {
485 p += vex_sprintf(p, ",%u", mask);
487 break;
490 case S390_ARG_VR:
491 p += vex_sprintf(p, "%s", vr_operand(va_arg(args, UInt)));
492 break;
494 case S390_ARG_UDVB: {
495 UInt d, v, b;
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 */);
502 break;
506 separator = ',';
509 done:
510 va_end(args);
512 if (mask_suffix != -1)
513 p += vex_sprintf(p, ",%d", mask_suffix);
514 *p = '\0';
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 /*---------------------------------------------------------------*/