1 /* Instruction printing code for the ARC.
2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2005, 2007
3 Free Software Foundation, Inc.
4 Contributed by Doug Evans (dje@cygnus.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. */
24 #include "libiberty.h"
26 #include "opcode/arc.h"
40 /* Classification of the opcodes for the decoder to print
48 /* All branches other than JC. */
51 /* All loads other than immediate
57 /* All single operand instructions. */
58 CLASS_A4_OP3_SUBOPC3F,
62 #define BIT(word,n) ((word) & (1 << n))
63 #define BITS(word,s,e) (((word) << (31 - e)) >> (s + (31 - e)))
64 #define OPCODE(word) (BITS ((word), 27, 31))
65 #define FIELDA(word) (BITS ((word), 21, 26))
66 #define FIELDB(word) (BITS ((word), 15, 20))
67 #define FIELDC(word) (BITS ((word), 9, 14))
69 /* FIELD D is signed in all of its uses, so we make sure argument is
70 treated as signed for bit shifting purposes: */
71 #define FIELDD(word) (BITS (((signed int)word), 0, 8))
73 #define PUT_NEXT_WORD_IN(a) \
76 if (is_limm == 1 && !NEXT_WORD (1)) \
77 mwerror (state, _("Illegal limm reference in last instruction!\n")); \
78 a = state->words[1]; \
82 #define CHECK_FLAG_COND_NULLIFY() \
87 flag = BIT (state->words[0], 8); \
88 state->nullifyMode = BITS (state->words[0], 5, 6); \
89 cond = BITS (state->words[0], 0, 4); \
94 #define CHECK_COND() \
98 cond = BITS (state->words[0], 0, 4); \
102 #define CHECK_FIELD(field) \
109 PUT_NEXT_WORD_IN (field); \
110 limm_value = field; \
112 else if (field > 60) \
116 flag = (field == 61); \
117 field = FIELDD (state->words[0]); \
122 #define CHECK_FIELD_A() \
125 fieldA = FIELDA (state->words[0]); \
134 #define CHECK_FIELD_B() \
137 fieldB = FIELDB (state->words[0]); \
138 CHECK_FIELD (fieldB); \
142 #define CHECK_FIELD_C() \
145 fieldC = FIELDC (state->words[0]); \
146 CHECK_FIELD (fieldC); \
150 #define IS_SMALL(x) (((field##x) < 256) && ((field##x) > -257))
151 #define IS_REG(x) (field##x##isReg)
152 #define WRITE_FORMAT_LB_Rx_RB(x) WRITE_FORMAT (x, "[","]","","")
153 #define WRITE_FORMAT_x_COMMA_LB(x) WRITE_FORMAT (x, "",",[","",",[")
154 #define WRITE_FORMAT_COMMA_x_RB(x) WRITE_FORMAT (x, ",","]",",","]")
155 #define WRITE_FORMAT_x_RB(x) WRITE_FORMAT (x, "","]","","]")
156 #define WRITE_FORMAT_COMMA_x(x) WRITE_FORMAT (x, ",","",",","")
157 #define WRITE_FORMAT_x_COMMA(x) WRITE_FORMAT (x, "",",","",",")
158 #define WRITE_FORMAT_x(x) WRITE_FORMAT (x, "","","","")
159 #define WRITE_FORMAT(x,cb1,ca1,cb,ca) strcat (formatString, \
160 (IS_REG (x) ? cb1"%r"ca1 : \
161 usesAuxReg ? cb"%a"ca : \
162 IS_SMALL (x) ? cb"%d"ca : cb"%h"ca))
163 #define WRITE_FORMAT_RB() strcat (formatString, "]")
164 #define WRITE_COMMENT(str) (state->comm[state->commNum++] = (str))
165 #define WRITE_NOP_COMMENT() if (!fieldAisReg && !flag) WRITE_COMMENT ("nop");
167 #define NEXT_WORD(x) (offset += 4, state->words[x])
169 #define add_target(x) (state->targets[state->tcnt++] = (x))
171 static char comment_prefix[] = "\t; ";
174 core_reg_name (struct arcDisState * state, int val)
176 if (state->coreRegName)
177 return (*state->coreRegName)(state->_this, val);
182 aux_reg_name (struct arcDisState * state, int val)
184 if (state->auxRegName)
185 return (*state->auxRegName)(state->_this, val);
190 cond_code_name (struct arcDisState * state, int val)
192 if (state->condCodeName)
193 return (*state->condCodeName)(state->_this, val);
198 instruction_name (struct arcDisState * state,
204 return (*state->instName)(state->_this, op1, op2, flags);
209 mwerror (struct arcDisState * state, const char * msg)
212 (*state->err)(state->_this, (msg));
216 post_address (struct arcDisState * state, int addr)
218 static char id[3 * ARRAY_SIZE (state->addresses)];
219 int j, i = state->acnt;
221 if (i < ((int) ARRAY_SIZE (state->addresses)))
223 state->addresses[i] = addr;
236 arc_sprintf (struct arcDisState *state, char *buf, const char *format, ...)
240 int size, leading_zero, regMap[2];
244 va_start (ap, format);
257 goto DOCOMM; /* (return) */
281 leading_zero = 1; /* e.g. %08x */
282 while (*p >= '0' && *p <= '9')
284 size = size * 10 + *p - '0';
289 #define inc_bp() bp = bp + strlen (bp)
293 unsigned u = va_arg (ap, int);
295 /* Hex. We can change the format to 0x%08x in
296 one place, here, if we wish.
297 We add underscores for easy reading. */
299 sprintf (bp, "0x%x_%04x", u >> 16, u & 0xffff);
301 sprintf (bp, "0x%x", u);
307 int val = va_arg (ap, int);
311 sprintf (bp, "%0*x", size, val);
313 sprintf (bp, "%*x", size, val);
315 sprintf (bp, "%x", val);
321 int val = va_arg (ap, int);
324 sprintf (bp, "%*d", size, val);
326 sprintf (bp, "%d", val);
333 int val = va_arg (ap, int);
335 #define REG2NAME(num, name) case num: sprintf (bp, ""name); \
336 regMap[(num < 32) ? 0 : 1] |= 1 << (num - ((num < 32) ? 0 : 32)); break;
343 REG2NAME (29, "ilink1");
344 REG2NAME (30, "ilink2");
345 REG2NAME (31, "blink");
346 REG2NAME (60, "lp_count");
351 ext = core_reg_name (state, val);
353 sprintf (bp, "%s", ext);
355 sprintf (bp,"r%d",val);
365 int val = va_arg (ap, int);
367 #define AUXREG2NAME(num, name) case num: sprintf (bp,name); break;
371 AUXREG2NAME (0x0, "status");
372 AUXREG2NAME (0x1, "semaphore");
373 AUXREG2NAME (0x2, "lp_start");
374 AUXREG2NAME (0x3, "lp_end");
375 AUXREG2NAME (0x4, "identity");
376 AUXREG2NAME (0x5, "debug");
381 ext = aux_reg_name (state, val);
383 sprintf (bp, "%s", ext);
385 arc_sprintf (state, bp, "%h", val);
395 sprintf (bp, "%s", va_arg (ap, char *));
401 fprintf (stderr, "?? format %c\n", p[-1]);
411 write_comments_(struct arcDisState * state,
416 if (state->commentBuffer != 0)
422 const char *name = post_address (state, limm_value + shimm);
425 WRITE_COMMENT (name);
427 for (i = 0; i < state->commNum; i++)
430 strcpy (state->commentBuffer, comment_prefix);
432 strcat (state->commentBuffer, ", ");
433 strncat (state->commentBuffer, state->comm[i],
434 sizeof (state->commentBuffer));
439 #define write_comments2(x) write_comments_ (state, x, is_limm, limm_value)
440 #define write_comments() write_comments2 (0)
442 static const char *condName[] =
445 "" , "z" , "nz" , "p" , "n" , "c" , "nc" , "v" ,
446 "nv" , "gt" , "ge" , "lt" , "le" , "hi" , "ls" , "pnz"
450 write_instr_name_(struct arcDisState * state,
451 const char * instrName,
453 int condCodeIsPartOfName,
459 strcpy (state->instrBuffer, instrName);
465 if (!condCodeIsPartOfName)
466 strcat (state->instrBuffer, ".");
471 cc = cond_code_name (state, cond);
476 strcat (state->instrBuffer, cc);
480 strcat (state->instrBuffer, ".f");
482 switch (state->nullifyMode)
485 strcat (state->instrBuffer, ".d");
487 case BR_exec_when_jump:
488 strcat (state->instrBuffer, ".jd");
493 strcat (state->instrBuffer, ".x");
496 strcat (state->instrBuffer, ".a");
499 strcat (state->instrBuffer, ".di");
502 #define write_instr_name() \
505 write_instr_name_(state, instrName,cond, condCodeIsPartOfName, \
506 flag, signExtend, addrWriteBack, directMem); \
507 formatString[0] = '\0'; \
513 op_LD0 = 0, op_LD1 = 1, op_ST = 2, op_3 = 3,
514 op_BC = 4, op_BLC = 5, op_LPC = 6, op_JC = 7,
515 op_ADD = 8, op_ADC = 9, op_SUB = 10, op_SBC = 11,
516 op_AND = 12, op_OR = 13, op_BIC = 14, op_XOR = 15
519 extern disassemble_info tm_print_insn_info;
522 dsmOneArcInst (bfd_vma addr, struct arcDisState * state)
524 int condCodeIsPartOfName = 0;
525 a4_decoding_class decodingClass;
526 const char * instrName;
540 int addrWriteBack = 0;
547 char formatString[60];
549 state->instructionLen = 4;
550 state->nullifyMode = BR_exec_when_no_jump;
554 state->_mem_load = 0;
555 state->_ea_present = 0;
556 state->_load_len = 0;
557 state->ea_reg1 = no_reg;
558 state->ea_reg2 = no_reg;
564 state->_opcode = OPCODE (state->words[0]);
566 decodingClass = CLASS_A4_ARITH; /* default! */
568 condCodeIsPartOfName=0;
572 state->flow = noflow;
575 if (state->commentBuffer)
576 state->commentBuffer[0] = '\0';
578 switch (state->_opcode)
581 switch (BITS (state->words[0],1,2))
585 state->_load_len = 4;
589 state->_load_len = 1;
593 state->_load_len = 2;
596 instrName = "??? (0[3])";
597 state->flow = invalid_instr;
600 decodingClass = CLASS_A4_LD0;
604 if (BIT (state->words[0],13))
607 decodingClass = CLASS_A4_LR;
611 switch (BITS (state->words[0], 10, 11))
615 state->_load_len = 4;
619 state->_load_len = 1;
623 state->_load_len = 2;
626 instrName = "??? (1[3])";
627 state->flow = invalid_instr;
630 decodingClass = CLASS_A4_LD1;
635 if (BIT (state->words[0], 25))
638 decodingClass = CLASS_A4_SR;
642 switch (BITS (state->words[0], 22, 23))
654 instrName = "??? (2[3])";
655 state->flow = invalid_instr;
658 decodingClass = CLASS_A4_ST;
663 decodingClass = CLASS_A4_OP3_GENERAL; /* default for opcode 3... */
664 switch (FIELDC (state->words[0]))
668 decodingClass = CLASS_A4_FLAG;
696 decodingClass = CLASS_A4_OP3_SUBOPC3F;
697 switch (FIELDD (state->words[0]))
710 state->flow=invalid_instr;
716 /* ARC Extension Library Instructions
717 NOTE: We assume that extension codes are these instrs. */
719 instrName = instruction_name (state,
721 FIELDC (state->words[0]),
726 state->flow = invalid_instr;
728 if (flags & IGNORE_FIRST_OPD)
745 if (BITS (state->words[0],9,9))
756 condCodeIsPartOfName = 1;
757 decodingClass = ((state->_opcode == op_JC) ? CLASS_A4_JC : CLASS_A4_BRANCH );
764 repeatsOp = (FIELDC (state->words[0]) == FIELDB (state->words[0]));
766 switch (state->_opcode)
769 instrName = (repeatsOp ? "asl" : "add");
772 instrName = (repeatsOp ? "rlc" : "adc");
775 instrName = (repeatsOp ? "mov" : "and");
780 case op_SUB: instrName = "sub";
782 case op_SBC: instrName = "sbc";
784 case op_OR: instrName = "or";
786 case op_BIC: instrName = "bic";
790 if (state->words[0] == 0x7fffffff)
792 /* NOP encoded as xor -1, -1, -1. */
794 decodingClass = CLASS_A4_OP3_SUBOPC3F;
801 instrName = instruction_name (state,state->_opcode,0,&flags);
802 /* if (instrName) printf("FLAGS=0x%x\n", flags); */
806 state->flow=invalid_instr;
808 if (flags & IGNORE_FIRST_OPD)
813 fieldAisReg = fieldBisReg = fieldCisReg = 1; /* Assume regs for now. */
814 flag = cond = is_shimm = is_limm = 0;
815 state->nullifyMode = BR_exec_when_no_jump; /* 0 */
816 signExtend = addrWriteBack = directMem = 0;
819 switch (decodingClass)
826 CHECK_FLAG_COND_NULLIFY ();
832 WRITE_FORMAT_COMMA_x (B);
834 WRITE_FORMAT_COMMA_x (C);
835 WRITE_NOP_COMMENT ();
836 arc_sprintf (state, state->operandBuffer, formatString,
837 fieldA, fieldB, fieldC);
843 WRITE_FORMAT_COMMA_x (C);
844 arc_sprintf (state, state->operandBuffer, formatString,
850 case CLASS_A4_OP3_GENERAL:
853 CHECK_FLAG_COND_NULLIFY ();
859 WRITE_FORMAT_COMMA_x (B);
860 WRITE_NOP_COMMENT ();
861 arc_sprintf (state, state->operandBuffer, formatString,
867 arc_sprintf (state, state->operandBuffer, formatString, fieldB);
874 CHECK_FLAG_COND_NULLIFY ();
875 flag = 0; /* This is the FLAG instruction -- it's redundant. */
879 arc_sprintf (state, state->operandBuffer, formatString, fieldB);
883 case CLASS_A4_BRANCH:
884 fieldA = BITS (state->words[0],7,26) << 2;
885 fieldA = (fieldA << 10) >> 10; /* Make it signed. */
887 CHECK_FLAG_COND_NULLIFY ();
891 /* This address could be a label we know. Convert it. */
892 if (state->_opcode != op_LPC /* LP */)
894 add_target (fieldA); /* For debugger. */
895 state->flow = state->_opcode == op_BLC /* BL */
898 /* indirect calls are achieved by "lr blink,[status];
899 lr dest<- func addr; j [dest]" */
902 strcat (formatString, "%s"); /* Address/label name. */
903 arc_sprintf (state, state->operandBuffer, formatString,
904 post_address (state, fieldA));
909 /* For op_JC -- jump to address specified.
910 Also covers jump and link--bit 9 of the instr. word
911 selects whether linked, thus "is_linked" is set above. */
914 CHECK_FLAG_COND_NULLIFY ();
919 fieldA = (fieldB >> 25) & 0x7F; /* Flags. */
920 fieldB = (fieldB & 0xFFFFFF) << 2;
921 state->flow = is_linked ? direct_call : direct_jump;
923 /* Screwy JLcc requires .jd mode to execute correctly
924 but we pretend it is .nd (no delay slot). */
925 if (is_linked && state->nullifyMode == BR_exec_when_jump)
926 state->nullifyMode = BR_exec_when_no_jump;
930 state->flow = is_linked ? indirect_call : indirect_jump;
931 /* We should also treat this as indirect call if NOT linked
932 but the preceding instruction was a "lr blink,[status]"
933 and we have a delay slot with "add blink,blink,2".
934 For now we can't detect such. */
935 state->register_for_indirect_jump = fieldB;
939 strcat (formatString,
940 IS_REG (B) ? "[%r]" : "%s"); /* Address/label name. */
944 WRITE_FORMAT_COMMA_x (A);
947 arc_sprintf (state, state->operandBuffer, formatString, fieldB, fieldA);
949 arc_sprintf (state, state->operandBuffer, formatString,
950 post_address (state, fieldB), fieldA);
956 B and C can be regs, or one (both?) can be limm. */
961 printf ("5:b reg %d %d c reg %d %d \n",
962 fieldBisReg,fieldB,fieldCisReg,fieldC);
964 state->_ea_present = 1;
966 state->ea_reg1 = fieldB;
968 state->_offset += fieldB;
970 state->ea_reg2 = fieldC;
972 state->_offset += fieldC;
973 state->_mem_load = 1;
975 directMem = BIT (state->words[0], 5);
976 addrWriteBack = BIT (state->words[0], 3);
977 signExtend = BIT (state->words[0], 0);
980 WRITE_FORMAT_x_COMMA_LB(A);
981 if (fieldBisReg || fieldB != 0)
982 WRITE_FORMAT_x_COMMA (B);
986 WRITE_FORMAT_x_RB (C);
987 arc_sprintf (state, state->operandBuffer, formatString,
988 fieldA, fieldB, fieldC);
993 /* LD instruction. */
996 fieldC = FIELDD (state->words[0]);
999 printf ("6:b reg %d %d c 0x%x \n",
1000 fieldBisReg, fieldB, fieldC);
1001 state->_ea_present = 1;
1002 state->_offset = fieldC;
1003 state->_mem_load = 1;
1005 state->ea_reg1 = fieldB;
1006 /* Field B is either a shimm (same as fieldC) or limm (different!)
1007 Say ea is not present, so only one of us will do the name lookup. */
1009 state->_offset += fieldB, state->_ea_present = 0;
1011 directMem = BIT (state->words[0],14);
1012 addrWriteBack = BIT (state->words[0],12);
1013 signExtend = BIT (state->words[0],9);
1015 write_instr_name ();
1016 WRITE_FORMAT_x_COMMA_LB (A);
1019 fieldB = state->_offset;
1020 WRITE_FORMAT_x_RB (B);
1025 if (fieldC != 0 && !BIT (state->words[0],13))
1028 WRITE_FORMAT_COMMA_x_RB (C);
1033 arc_sprintf (state, state->operandBuffer, formatString,
1034 fieldA, fieldB, fieldC);
1039 /* ST instruction. */
1042 fieldA = FIELDD(state->words[0]); /* shimm */
1045 if (dbg) printf("7:b reg %d %x off %x\n",
1046 fieldBisReg,fieldB,fieldA);
1047 state->_ea_present = 1;
1048 state->_offset = fieldA;
1050 state->ea_reg1 = fieldB;
1051 /* Field B is either a shimm (same as fieldA) or limm (different!)
1052 Say ea is not present, so only one of us will do the name lookup.
1053 (for is_limm we do the name translation here). */
1055 state->_offset += fieldB, state->_ea_present = 0;
1057 directMem = BIT (state->words[0], 26);
1058 addrWriteBack = BIT (state->words[0], 24);
1060 write_instr_name ();
1061 WRITE_FORMAT_x_COMMA_LB(C);
1065 fieldB = state->_offset;
1066 WRITE_FORMAT_x_RB (B);
1071 if (fieldBisReg && fieldA != 0)
1074 WRITE_FORMAT_COMMA_x_RB(A);
1079 arc_sprintf (state, state->operandBuffer, formatString,
1080 fieldC, fieldB, fieldA);
1081 write_comments2 (fieldA);
1085 /* SR instruction */
1089 write_instr_name ();
1090 WRITE_FORMAT_x_COMMA_LB(C);
1091 /* Try to print B as an aux reg if it is not a core reg. */
1095 arc_sprintf (state, state->operandBuffer, formatString, fieldC, fieldB);
1099 case CLASS_A4_OP3_SUBOPC3F:
1100 write_instr_name ();
1101 state->operandBuffer[0] = '\0';
1105 /* LR instruction */
1109 write_instr_name ();
1110 WRITE_FORMAT_x_COMMA_LB (A);
1111 /* Try to print B as an aux reg if it is not a core reg. */
1115 arc_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB);
1120 mwerror (state, "Bad decoding class in ARC disassembler");
1124 state->_cond = cond;
1125 return state->instructionLen = offset;
1129 /* Returns the name the user specified core extension register. */
1132 _coreRegName(void * arg ATTRIBUTE_UNUSED, int regval)
1134 return arcExtMap_coreRegName (regval);
1137 /* Returns the name the user specified AUX extension register. */
1140 _auxRegName(void *_this ATTRIBUTE_UNUSED, int regval)
1142 return arcExtMap_auxRegName(regval);
1145 /* Returns the name the user specified condition code name. */
1148 _condCodeName(void *_this ATTRIBUTE_UNUSED, int regval)
1150 return arcExtMap_condCodeName(regval);
1153 /* Returns the name the user specified extension instruction. */
1156 _instName (void *_this ATTRIBUTE_UNUSED, int majop, int minop, int *flags)
1158 return arcExtMap_instName(majop, minop, flags);
1161 /* Decode an instruction returning the size of the instruction
1162 in bytes or zero if unrecognized. */
1165 decodeInstr (bfd_vma address, /* Address of this instruction. */
1166 disassemble_info * info)
1170 struct arcDisState s; /* ARC Disassembler state. */
1171 void *stream = info->stream; /* Output stream. */
1172 fprintf_ftype func = info->fprintf_func;
1175 memset (&s, 0, sizeof(struct arcDisState));
1177 /* read first instruction */
1178 status = (*info->read_memory_func) (address, buffer, 4, info);
1181 (*info->memory_error_func) (status, address, info);
1184 if (info->endian == BFD_ENDIAN_LITTLE)
1185 s.words[0] = bfd_getl32(buffer);
1187 s.words[0] = bfd_getb32(buffer);
1188 /* Always read second word in case of limm. */
1190 /* We ignore the result since last insn may not have a limm. */
1191 status = (*info->read_memory_func) (address + 4, buffer, 4, info);
1192 if (info->endian == BFD_ENDIAN_LITTLE)
1193 s.words[1] = bfd_getl32(buffer);
1195 s.words[1] = bfd_getb32(buffer);
1198 s.coreRegName = _coreRegName;
1199 s.auxRegName = _auxRegName;
1200 s.condCodeName = _condCodeName;
1201 s.instName = _instName;
1204 bytes = dsmOneArcInst (address, (void *)& s);
1206 /* Display the disassembly instruction. */
1207 (*func) (stream, "%08lx ", s.words[0]);
1208 (*func) (stream, " ");
1209 (*func) (stream, "%-10s ", s.instrBuffer);
1211 if (__TRANSLATION_REQUIRED (s))
1213 bfd_vma addr = s.addresses[s.operandBuffer[1] - '0'];
1215 (*info->print_address_func) ((bfd_vma) addr, info);
1216 (*func) (stream, "\n");
1219 (*func) (stream, "%s",s.operandBuffer);
1221 return s.instructionLen;
1224 /* Return the print_insn function to use.
1225 Side effect: load (possibly empty) extension section */
1228 arc_get_disassembler (void *ptr)
1231 build_ARC_extmap (ptr);