1 /* Instruction printing code for the ARC.
2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
4 Contributed by Claudiu Zissulescu (claziss@synopsys.com)
6 This file is part of libopcodes.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "opcode/arc.h"
32 /* Globals variables. */
34 static const char * const regnames[64] =
36 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
37 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
38 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
39 "r24", "r25", "gp", "fp", "sp", "ilink", "r30", "blink",
41 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
42 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
43 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
44 "r56", "r57", "ACCL", "ACCH", "lp_count", "rezerved", "LIMM", "pcl"
50 # define pr_debug(fmt, args...) fprintf (stderr, fmt, ##args)
52 # define pr_debug(fmt, args...)
55 #define ARRANGE_ENDIAN(info, buf) \
56 (info->endian == BFD_ENDIAN_LITTLE ? bfd_getm32 (bfd_getl32 (buf)) \
59 #define BITS(word,s,e) (((word) << (sizeof (word) * 8 - 1 - e)) >> \
60 (s + (sizeof (word) * 8 - 1 - e)))
61 #define OPCODE(word) (BITS ((word), 27, 31))
62 #define FIELDA(word) (BITS ((word), 21, 26))
63 #define FIELDB(word) (BITS ((word), 15, 20))
64 #define FIELDC(word) (BITS ((word), 9, 14))
66 #define OPCODE_AC(word) (BITS ((word), 11, 15))
68 /* Functions implementation. */
71 bfd_getm32 (unsigned int data)
75 value = ((data & 0xff00) | (data & 0xff)) << 16;
76 value |= ((data & 0xff0000) | (data & 0xff000000)) >> 16;
81 special_flag_p (const char *opname,
84 const struct arc_flag_special *flg_spec;
85 unsigned i, j, flgidx;
87 for (i = 0; i < arc_num_flag_special; i++)
89 flg_spec = &arc_flag_special_cases[i];
91 if (strcmp (opname, flg_spec->name))
94 /* Found potential special case instruction. */
97 flgidx = flg_spec->flags[j];
99 break; /* End of the array. */
101 if (strcmp (flgname, arc_flag_operands[flgidx].name) == 0)
108 /* Disassemble ARC instructions. */
111 print_insn_arc (bfd_vma memaddr,
112 struct disassemble_info *info)
115 unsigned int lowbyte, highbyte;
119 unsigned insn[2] = { 0, 0 };
121 const unsigned char *opidx;
122 const unsigned char *flgidx;
123 const struct arc_opcode *opcode;
124 const char *instrName;
126 bfd_boolean need_comma;
127 bfd_boolean open_braket;
130 lowbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 1 : 0);
131 highbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 0 : 1);
135 case bfd_mach_arc_arc700:
136 isa_mask = ARC_OPCODE_ARC700;
139 case bfd_mach_arc_arc600:
140 isa_mask = ARC_OPCODE_ARC600;
143 case bfd_mach_arc_arcv2:
145 isa_mask = ARC_OPCODE_ARCv2HS | ARC_OPCODE_ARCv2EM;
149 /* This variable may be set by the instruction decoder. It suggests
150 the number of bytes objdump should display on a single line. If
151 the instruction decoder sets this, it should always set it to
152 the same value in order to get reasonable looking output. */
154 info->bytes_per_line = 8;
156 /* In the next lines, we set two info variables control the way
157 objdump displays the raw data. For example, if bytes_per_line is
158 8 and bytes_per_chunk is 4, the output will look like this:
159 00: 00000000 00000000
160 with the chunks displayed according to "display_endian". */
163 && !(info->section->flags & SEC_CODE))
165 /* This is not a CODE section. */
166 switch (info->section->size)
171 size = info->section->size;
174 size = (info->section->size & 0x01) ? 1 : 4;
177 info->bytes_per_chunk = 1;
178 info->display_endian = info->endian;
183 info->bytes_per_chunk = 2;
184 info->display_endian = info->endian;
187 /* Read the insn into a host word. */
188 status = (*info->read_memory_func) (memaddr, buffer, size, info);
191 (*info->memory_error_func) (status, memaddr, info);
196 && !(info->section->flags & SEC_CODE))
201 data = bfd_get_bits (buffer, size * 8,
202 info->display_endian == BFD_ENDIAN_BIG);
206 (*info->fprintf_func) (info->stream, ".byte\t0x%02lx", data);
209 (*info->fprintf_func) (info->stream, ".short\t0x%04lx", data);
212 (*info->fprintf_func) (info->stream, ".word\t0x%08lx", data);
220 if ( (((buffer[lowbyte] & 0xf8) > 0x38)
221 && ((buffer[lowbyte] & 0xf8) != 0x48))
222 || ((info->mach == bfd_mach_arc_arcv2)
223 && ((buffer[lowbyte] & 0xF8) == 0x48)) /* FIXME! ugly. */
226 /* This is a short instruction. */
228 insn[0] = (buffer[lowbyte] << 8) | buffer[highbyte];
234 /* This is a long instruction: Read the remaning 2 bytes. */
235 status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 2, info);
238 (*info->memory_error_func) (status, memaddr + 2, info);
241 insn[0] = ARRANGE_ENDIAN (info, buffer);
244 /* Set some defaults for the insn info. */
245 info->insn_info_valid = 1;
246 info->branch_delay_insns = 0;
248 info->insn_type = dis_nonbranch;
252 /* FIXME to be moved in dissasemble_init_for_target. */
253 info->disassembler_needs_relocs = TRUE;
255 /* Find the first match in the opcode table. */
256 for (i = 0; i < arc_num_opcodes; i++)
258 bfd_boolean invalid = FALSE;
260 opcode = &arc_opcodes[i];
262 if (ARC_SHORT (opcode->mask) && (insnLen == 2))
264 if (OPCODE_AC (opcode->opcode) != OPCODE_AC (insn[0]))
267 else if (!ARC_SHORT (opcode->mask) && (insnLen == 4))
269 if (OPCODE (opcode->opcode) != OPCODE (insn[0]))
275 if ((insn[0] ^ opcode->opcode) & opcode->mask)
278 if (!(opcode->cpu & isa_mask))
281 /* Possible candidate, check the operands. */
282 for (opidx = opcode->operands; *opidx; opidx++)
285 const struct arc_operand *operand = &arc_operands[*opidx];
287 if (operand->flags & ARC_OPERAND_FAKE)
290 if (operand->extract)
291 value = (*operand->extract) (insn[0], &invalid);
293 value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
295 /* Check for LIMM indicator. If it is there, then make sure
296 we pick the right format. */
297 if (operand->flags & ARC_OPERAND_IR
298 && !(operand->flags & ARC_OPERAND_LIMM))
300 if ((value == 0x3E && insnLen == 4)
301 || (value == 0x1E && insnLen == 2))
309 /* Check the flags. */
310 for (flgidx = opcode->flags; *flgidx; flgidx++)
312 /* Get a valid flag class. */
313 const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
314 const unsigned *flgopridx;
315 int foundA = 0, foundB = 0;
317 for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
319 const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
322 value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
323 if (value == flg_operand->code)
328 if (!foundA && foundB)
338 /* The instruction is valid. */
342 /* No instruction found. Try the extenssions. */
343 instrName = arcExtMap_instName (OPCODE (insn[0]), insn[0], &flags);
346 opcode = &arc_opcodes[0];
347 (*info->fprintf_func) (info->stream, "%s", instrName);
352 (*info->fprintf_func) (info->stream, ".long %#04x", insn[0]);
354 (*info->fprintf_func) (info->stream, ".long %#08x", insn[0]);
356 info->insn_type = dis_noninsn;
360 /* Print the mnemonic. */
361 (*info->fprintf_func) (info->stream, "%s", opcode->name);
363 /* Preselect the insn class. */
364 switch (opcode->class)
368 if (!strncmp (opcode->name, "bl", 2)
369 || !strncmp (opcode->name, "jl", 2))
370 info->insn_type = dis_jsr;
372 info->insn_type = dis_branch;
375 info->insn_type = dis_dref; /* FIXME! DB indicates mov as memory! */
378 info->insn_type = dis_nonbranch;
382 pr_debug ("%s: 0x%08x\n", opcode->name, opcode->opcode);
385 /* Now extract and print the flags. */
386 for (flgidx = opcode->flags; *flgidx; flgidx++)
388 /* Get a valid flag class. */
389 const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
390 const unsigned *flgopridx;
392 for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
394 const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
397 if (!flg_operand->favail)
400 value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
401 if (value == flg_operand->code)
403 /* FIXME!: print correctly nt/t flag. */
404 if (!special_flag_p (opcode->name, flg_operand->name))
405 (*info->fprintf_func) (info->stream, ".");
406 else if (info->insn_type == dis_dref)
408 switch (flg_operand->name[0])
422 (*info->fprintf_func) (info->stream, "%s", flg_operand->name);
425 if (flg_operand->name[0] == 'd'
426 && flg_operand->name[1] == 0)
427 info->branch_delay_insns = 1;
431 if (opcode->operands[0] != 0)
432 (*info->fprintf_func) (info->stream, "\t");
437 /* Now extract and print the operands. */
438 for (opidx = opcode->operands; *opidx; opidx++)
440 const struct arc_operand *operand = &arc_operands[*opidx];
443 if (open_braket && (operand->flags & ARC_OPERAND_BRAKET))
445 (*info->fprintf_func) (info->stream, "]");
450 /* Only take input from real operands. */
451 if ((operand->flags & ARC_OPERAND_FAKE)
452 && !(operand->flags & ARC_OPERAND_BRAKET))
455 if (operand->extract)
456 value = (*operand->extract) (insn[0], (int *) NULL);
459 if (operand->flags & ARC_OPERAND_ALIGNED32)
461 value = (insn[0] >> operand->shift)
462 & ((1 << (operand->bits - 2)) - 1);
467 value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
469 if (operand->flags & ARC_OPERAND_SIGNED)
471 int signbit = 1 << (operand->bits - 1);
472 value = (value ^ signbit) - signbit;
476 if (operand->flags & ARC_OPERAND_IGNORE
477 && (operand->flags & ARC_OPERAND_IR
482 (*info->fprintf_func) (info->stream, ",");
484 if (!open_braket && (operand->flags & ARC_OPERAND_BRAKET))
486 (*info->fprintf_func) (info->stream, "[");
492 /* Read the limm operand, if required. */
493 if (operand->flags & ARC_OPERAND_LIMM
494 && !(operand->flags & ARC_OPERAND_DUPLICATE))
496 status = (*info->read_memory_func) (memaddr + insnLen, buffer,
500 (*info->memory_error_func) (status, memaddr + insnLen, info);
503 insn[1] = ARRANGE_ENDIAN (info, buffer);
506 /* Print the operand as directed by the flags. */
507 if (operand->flags & ARC_OPERAND_IR)
509 assert (value >=0 && value < 64);
510 (*info->fprintf_func) (info->stream, "%s", regnames[value]);
511 if (operand->flags & ARC_OPERAND_TRUNCATE)
512 (*info->fprintf_func) (info->stream, "%s", regnames[value+1]);
514 else if (operand->flags & ARC_OPERAND_LIMM)
516 (*info->fprintf_func) (info->stream, "%#x", insn[1]);
517 if (info->insn_type == dis_branch
518 || info->insn_type == dis_jsr)
519 info->target = (bfd_vma) insn[1];
521 else if (operand->flags & ARC_OPERAND_PCREL)
524 if (info->flags & INSN_HAS_RELOC)
526 (*info->print_address_func) ((memaddr & ~3) + value, info);
528 info->target = (bfd_vma) (memaddr & ~3) + value;
530 else if (operand->flags & ARC_OPERAND_SIGNED)
531 (*info->fprintf_func) (info->stream, "%d", value);
533 if (operand->flags & ARC_OPERAND_TRUNCATE
534 && !(operand->flags & ARC_OPERAND_ALIGNED32)
535 && !(operand->flags & ARC_OPERAND_ALIGNED16)
536 && value > 0 && value <= 14)
537 (*info->fprintf_func) (info->stream, "r13-%s",
538 regnames[13 + value - 1]);
540 (*info->fprintf_func) (info->stream, "%#x", value);
544 /* Adjust insn len. */
545 if (operand->flags & ARC_OPERAND_LIMM
546 && !(operand->flags & ARC_OPERAND_DUPLICATE))
555 arc_get_disassembler (bfd *abfd)
557 /* Read the extenssion insns and registers, if any. */
558 build_ARC_extmap (abfd);
561 return print_insn_arc;
564 /* Disassemble ARC instructions. Used by debugger. */
567 arcAnalyzeInstr (bfd_vma memaddr,
568 struct disassemble_info *info)
570 struct arcDisState ret;
571 memset (&ret, 0, sizeof (struct arcDisState));
573 ret.instructionLen = print_insn_arc (memaddr, info);
576 ret.words[0] = insn[0];
577 ret.words[1] = insn[1];
579 ret.coreRegName = _coreRegName;
580 ret.auxRegName = _auxRegName;
581 ret.condCodeName = _condCodeName;
582 ret.instName = _instName;
589 eval: (c-set-style "gnu")