1 /* Instruction printing code for the ARC.
2 Copyright (C) 1994-2016 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_nps400:
136 isa_mask = ARC_OPCODE_ARC700 | ARC_OPCODE_NPS400;
139 case bfd_mach_arc_arc700:
140 isa_mask = ARC_OPCODE_ARC700;
143 case bfd_mach_arc_arc600:
144 isa_mask = ARC_OPCODE_ARC600;
147 case bfd_mach_arc_arcv2:
149 isa_mask = ARC_OPCODE_ARCv2HS | ARC_OPCODE_ARCv2EM;
153 /* This variable may be set by the instruction decoder. It suggests
154 the number of bytes objdump should display on a single line. If
155 the instruction decoder sets this, it should always set it to
156 the same value in order to get reasonable looking output. */
158 info->bytes_per_line = 8;
160 /* In the next lines, we set two info variables control the way
161 objdump displays the raw data. For example, if bytes_per_line is
162 8 and bytes_per_chunk is 4, the output will look like this:
163 00: 00000000 00000000
164 with the chunks displayed according to "display_endian". */
167 && !(info->section->flags & SEC_CODE))
169 /* This is not a CODE section. */
170 switch (info->section->size)
175 size = info->section->size;
178 size = (info->section->size & 0x01) ? 1 : 4;
181 info->bytes_per_chunk = 1;
182 info->display_endian = info->endian;
187 info->bytes_per_chunk = 2;
188 info->display_endian = info->endian;
191 /* Read the insn into a host word. */
192 status = (*info->read_memory_func) (memaddr, buffer, size, info);
195 (*info->memory_error_func) (status, memaddr, info);
200 && !(info->section->flags & SEC_CODE))
205 data = bfd_get_bits (buffer, size * 8,
206 info->display_endian == BFD_ENDIAN_BIG);
210 (*info->fprintf_func) (info->stream, ".byte\t0x%02lx", data);
213 (*info->fprintf_func) (info->stream, ".short\t0x%04lx", data);
216 (*info->fprintf_func) (info->stream, ".word\t0x%08lx", data);
224 if ( (((buffer[lowbyte] & 0xf8) > 0x38)
225 && ((buffer[lowbyte] & 0xf8) != 0x48))
226 || ((info->mach == bfd_mach_arc_arcv2)
227 && ((buffer[lowbyte] & 0xF8) == 0x48)) /* FIXME! ugly. */
230 /* This is a short instruction. */
232 insn[0] = (buffer[lowbyte] << 8) | buffer[highbyte];
238 /* This is a long instruction: Read the remaning 2 bytes. */
239 status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 2, info);
242 (*info->memory_error_func) (status, memaddr + 2, info);
245 insn[0] = ARRANGE_ENDIAN (info, buffer);
248 /* Set some defaults for the insn info. */
249 info->insn_info_valid = 1;
250 info->branch_delay_insns = 0;
252 info->insn_type = dis_nonbranch;
256 /* FIXME to be moved in dissasemble_init_for_target. */
257 info->disassembler_needs_relocs = TRUE;
259 /* Find the first match in the opcode table. */
260 for (i = 0; i < arc_num_opcodes; i++)
262 bfd_boolean invalid = FALSE;
264 opcode = &arc_opcodes[i];
266 if (ARC_SHORT (opcode->mask) && (insnLen == 2))
268 if (OPCODE_AC (opcode->opcode) != OPCODE_AC (insn[0]))
271 else if (!ARC_SHORT (opcode->mask) && (insnLen == 4))
273 if (OPCODE (opcode->opcode) != OPCODE (insn[0]))
279 if ((insn[0] ^ opcode->opcode) & opcode->mask)
282 if (!(opcode->cpu & isa_mask))
285 /* Possible candidate, check the operands. */
286 for (opidx = opcode->operands; *opidx; opidx++)
289 const struct arc_operand *operand = &arc_operands[*opidx];
291 if (operand->flags & ARC_OPERAND_FAKE)
294 if (operand->extract)
295 value = (*operand->extract) (insn[0], &invalid);
297 value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
299 /* Check for LIMM indicator. If it is there, then make sure
300 we pick the right format. */
301 if (operand->flags & ARC_OPERAND_IR
302 && !(operand->flags & ARC_OPERAND_LIMM))
304 if ((value == 0x3E && insnLen == 4)
305 || (value == 0x1E && insnLen == 2))
313 /* Check the flags. */
314 for (flgidx = opcode->flags; *flgidx; flgidx++)
316 /* Get a valid flag class. */
317 const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
318 const unsigned *flgopridx;
319 int foundA = 0, foundB = 0;
321 for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
323 const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
326 value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
327 if (value == flg_operand->code)
332 if (!foundA && foundB)
342 /* The instruction is valid. */
346 /* No instruction found. Try the extenssions. */
347 instrName = arcExtMap_instName (OPCODE (insn[0]), insn[0], &flags);
350 opcode = &arc_opcodes[0];
351 (*info->fprintf_func) (info->stream, "%s", instrName);
356 (*info->fprintf_func) (info->stream, ".long %#04x", insn[0]);
358 (*info->fprintf_func) (info->stream, ".long %#08x", insn[0]);
360 info->insn_type = dis_noninsn;
364 /* Print the mnemonic. */
365 (*info->fprintf_func) (info->stream, "%s", opcode->name);
367 /* Preselect the insn class. */
368 switch (opcode->class)
372 if (!strncmp (opcode->name, "bl", 2)
373 || !strncmp (opcode->name, "jl", 2))
374 info->insn_type = dis_jsr;
376 info->insn_type = dis_branch;
379 info->insn_type = dis_dref; /* FIXME! DB indicates mov as memory! */
382 info->insn_type = dis_nonbranch;
386 pr_debug ("%s: 0x%08x\n", opcode->name, opcode->opcode);
389 /* Now extract and print the flags. */
390 for (flgidx = opcode->flags; *flgidx; flgidx++)
392 /* Get a valid flag class. */
393 const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
394 const unsigned *flgopridx;
396 for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
398 const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
401 if (!flg_operand->favail)
404 value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
405 if (value == flg_operand->code)
407 /* FIXME!: print correctly nt/t flag. */
408 if (!special_flag_p (opcode->name, flg_operand->name))
409 (*info->fprintf_func) (info->stream, ".");
410 else if (info->insn_type == dis_dref)
412 switch (flg_operand->name[0])
426 (*info->fprintf_func) (info->stream, "%s", flg_operand->name);
429 if (flg_operand->name[0] == 'd'
430 && flg_operand->name[1] == 0)
431 info->branch_delay_insns = 1;
435 if (opcode->operands[0] != 0)
436 (*info->fprintf_func) (info->stream, "\t");
441 /* Now extract and print the operands. */
442 for (opidx = opcode->operands; *opidx; opidx++)
444 const struct arc_operand *operand = &arc_operands[*opidx];
447 if (open_braket && (operand->flags & ARC_OPERAND_BRAKET))
449 (*info->fprintf_func) (info->stream, "]");
454 /* Only take input from real operands. */
455 if ((operand->flags & ARC_OPERAND_FAKE)
456 && !(operand->flags & ARC_OPERAND_BRAKET))
459 if (operand->extract)
460 value = (*operand->extract) (insn[0], (int *) NULL);
463 if (operand->flags & ARC_OPERAND_ALIGNED32)
465 value = (insn[0] >> operand->shift)
466 & ((1 << (operand->bits - 2)) - 1);
471 value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
473 if (operand->flags & ARC_OPERAND_SIGNED)
475 int signbit = 1 << (operand->bits - 1);
476 value = (value ^ signbit) - signbit;
480 if (operand->flags & ARC_OPERAND_IGNORE
481 && (operand->flags & ARC_OPERAND_IR
486 (*info->fprintf_func) (info->stream, ",");
488 if (!open_braket && (operand->flags & ARC_OPERAND_BRAKET))
490 (*info->fprintf_func) (info->stream, "[");
496 /* Read the limm operand, if required. */
497 if (operand->flags & ARC_OPERAND_LIMM
498 && !(operand->flags & ARC_OPERAND_DUPLICATE))
500 status = (*info->read_memory_func) (memaddr + insnLen, buffer,
504 (*info->memory_error_func) (status, memaddr + insnLen, info);
507 insn[1] = ARRANGE_ENDIAN (info, buffer);
510 /* Print the operand as directed by the flags. */
511 if (operand->flags & ARC_OPERAND_IR)
513 assert (value >=0 && value < 64);
514 (*info->fprintf_func) (info->stream, "%s", regnames[value]);
515 if (operand->flags & ARC_OPERAND_TRUNCATE)
516 (*info->fprintf_func) (info->stream, "%s", regnames[value+1]);
518 else if (operand->flags & ARC_OPERAND_LIMM)
520 (*info->fprintf_func) (info->stream, "%#x", insn[1]);
521 if (info->insn_type == dis_branch
522 || info->insn_type == dis_jsr)
523 info->target = (bfd_vma) insn[1];
525 else if (operand->flags & ARC_OPERAND_PCREL)
528 if (info->flags & INSN_HAS_RELOC)
530 (*info->print_address_func) ((memaddr & ~3) + value, info);
532 info->target = (bfd_vma) (memaddr & ~3) + value;
534 else if (operand->flags & ARC_OPERAND_SIGNED)
535 (*info->fprintf_func) (info->stream, "%d", value);
537 if (operand->flags & ARC_OPERAND_TRUNCATE
538 && !(operand->flags & ARC_OPERAND_ALIGNED32)
539 && !(operand->flags & ARC_OPERAND_ALIGNED16)
540 && value > 0 && value <= 14)
541 (*info->fprintf_func) (info->stream, "r13-%s",
542 regnames[13 + value - 1]);
544 (*info->fprintf_func) (info->stream, "%#x", value);
548 /* Adjust insn len. */
549 if (operand->flags & ARC_OPERAND_LIMM
550 && !(operand->flags & ARC_OPERAND_DUPLICATE))
559 arc_get_disassembler (bfd *abfd)
561 /* Read the extenssion insns and registers, if any. */
562 build_ARC_extmap (abfd);
565 return print_insn_arc;
568 /* Disassemble ARC instructions. Used by debugger. */
571 arcAnalyzeInstr (bfd_vma memaddr,
572 struct disassemble_info *info)
574 struct arcDisState ret;
575 memset (&ret, 0, sizeof (struct arcDisState));
577 ret.instructionLen = print_insn_arc (memaddr, info);
580 ret.words[0] = insn[0];
581 ret.words[1] = insn[1];
583 ret.coreRegName = _coreRegName;
584 ret.auxRegName = _auxRegName;
585 ret.condCodeName = _condCodeName;
586 ret.instName = _instName;
593 eval: (c-set-style "gnu")