1 /* Instruction printing code for the ARC.
2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2005
3 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
22 #include "libiberty.h"
24 #include "opcode/arc.h"
38 /* Classification of the opcodes for the decoder to print
46 /* All branches other than JC. */
49 /* All loads other than immediate
55 /* All single operand instructions. */
56 CLASS_A4_OP3_SUBOPC3F,
60 #define BIT(word,n) ((word) & (1 << n))
61 #define BITS(word,s,e) (((word) << (31 - e)) >> (s + (31 - e)))
62 #define OPCODE(word) (BITS ((word), 27, 31))
63 #define FIELDA(word) (BITS ((word), 21, 26))
64 #define FIELDB(word) (BITS ((word), 15, 20))
65 #define FIELDC(word) (BITS ((word), 9, 14))
67 /* FIELD D is signed in all of its uses, so we make sure argument is
68 treated as signed for bit shifting purposes: */
69 #define FIELDD(word) (BITS (((signed int)word), 0, 8))
71 #define PUT_NEXT_WORD_IN(a) \
74 if (is_limm == 1 && !NEXT_WORD (1)) \
75 mwerror (state, _("Illegal limm reference in last instruction!\n")); \
76 a = state->words[1]; \
80 #define CHECK_FLAG_COND_NULLIFY() \
85 flag = BIT (state->words[0], 8); \
86 state->nullifyMode = BITS (state->words[0], 5, 6); \
87 cond = BITS (state->words[0], 0, 4); \
92 #define CHECK_COND() \
96 cond = BITS (state->words[0], 0, 4); \
100 #define CHECK_FIELD(field) \
107 PUT_NEXT_WORD_IN (field); \
108 limm_value = field; \
110 else if (field > 60) \
114 flag = (field == 61); \
115 field = FIELDD (state->words[0]); \
120 #define CHECK_FIELD_A() \
123 fieldA = FIELDA (state->words[0]); \
132 #define CHECK_FIELD_B() \
135 fieldB = FIELDB (state->words[0]); \
136 CHECK_FIELD (fieldB); \
140 #define CHECK_FIELD_C() \
143 fieldC = FIELDC (state->words[0]); \
144 CHECK_FIELD (fieldC); \
148 #define IS_SMALL(x) (((field##x) < 256) && ((field##x) > -257))
149 #define IS_REG(x) (field##x##isReg)
150 #define WRITE_FORMAT_LB_Rx_RB(x) WRITE_FORMAT (x, "[","]","","")
151 #define WRITE_FORMAT_x_COMMA_LB(x) WRITE_FORMAT (x, "",",[","",",[")
152 #define WRITE_FORMAT_COMMA_x_RB(x) WRITE_FORMAT (x, ",","]",",","]")
153 #define WRITE_FORMAT_x_RB(x) WRITE_FORMAT (x, "","]","","]")
154 #define WRITE_FORMAT_COMMA_x(x) WRITE_FORMAT (x, ",","",",","")
155 #define WRITE_FORMAT_x_COMMA(x) WRITE_FORMAT (x, "",",","",",")
156 #define WRITE_FORMAT_x(x) WRITE_FORMAT (x, "","","","")
157 #define WRITE_FORMAT(x,cb1,ca1,cb,ca) strcat (formatString, \
158 (IS_REG (x) ? cb1"%r"ca1 : \
159 usesAuxReg ? cb"%a"ca : \
160 IS_SMALL (x) ? cb"%d"ca : cb"%h"ca))
161 #define WRITE_FORMAT_RB() strcat (formatString, "]")
162 #define WRITE_COMMENT(str) (state->comm[state->commNum++] = (str))
163 #define WRITE_NOP_COMMENT() if (!fieldAisReg && !flag) WRITE_COMMENT ("nop");
165 #define NEXT_WORD(x) (offset += 4, state->words[x])
167 #define add_target(x) (state->targets[state->tcnt++] = (x))
169 static char comment_prefix[] = "\t; ";
172 core_reg_name (struct arcDisState * state, int val)
174 if (state->coreRegName)
175 return (*state->coreRegName)(state->_this, val);
180 aux_reg_name (struct arcDisState * state, int val)
182 if (state->auxRegName)
183 return (*state->auxRegName)(state->_this, val);
188 cond_code_name (struct arcDisState * state, int val)
190 if (state->condCodeName)
191 return (*state->condCodeName)(state->_this, val);
196 instruction_name (struct arcDisState * state,
202 return (*state->instName)(state->_this, op1, op2, flags);
207 mwerror (struct arcDisState * state, const char * msg)
210 (*state->err)(state->_this, (msg));
214 post_address (struct arcDisState * state, int addr)
216 static char id[3 * ARRAY_SIZE (state->addresses)];
217 int j, i = state->acnt;
219 if (i < ((int) ARRAY_SIZE (state->addresses)))
221 state->addresses[i] = addr;
234 arc_sprintf (struct arcDisState *state, char *buf, const char *format, ...)
238 int size, leading_zero, regMap[2];
242 va_start (ap, format);
255 goto DOCOMM; /* (return) */
279 leading_zero = 1; /* e.g. %08x */
280 while (*p >= '0' && *p <= '9')
282 size = size * 10 + *p - '0';
287 #define inc_bp() bp = bp + strlen (bp)
291 unsigned u = va_arg (ap, int);
293 /* Hex. We can change the format to 0x%08x in
294 one place, here, if we wish.
295 We add underscores for easy reading. */
297 sprintf (bp, "0x%x_%04x", u >> 16, u & 0xffff);
299 sprintf (bp, "0x%x", u);
305 int val = va_arg (ap, int);
309 sprintf (bp, "%0*x", size, val);
311 sprintf (bp, "%*x", size, val);
313 sprintf (bp, "%x", val);
319 int val = va_arg (ap, int);
322 sprintf (bp, "%*d", size, val);
324 sprintf (bp, "%d", val);
331 int val = va_arg (ap, int);
333 #define REG2NAME(num, name) case num: sprintf (bp, ""name); \
334 regMap[(num < 32) ? 0 : 1] |= 1 << (num - ((num < 32) ? 0 : 32)); break;
341 REG2NAME (29, "ilink1");
342 REG2NAME (30, "ilink2");
343 REG2NAME (31, "blink");
344 REG2NAME (60, "lp_count");
349 ext = core_reg_name (state, val);
351 sprintf (bp, "%s", ext);
353 sprintf (bp,"r%d",val);
363 int val = va_arg (ap, int);
365 #define AUXREG2NAME(num, name) case num: sprintf (bp,name); break;
369 AUXREG2NAME (0x0, "status");
370 AUXREG2NAME (0x1, "semaphore");
371 AUXREG2NAME (0x2, "lp_start");
372 AUXREG2NAME (0x3, "lp_end");
373 AUXREG2NAME (0x4, "identity");
374 AUXREG2NAME (0x5, "debug");
379 ext = aux_reg_name (state, val);
381 sprintf (bp, "%s", ext);
383 arc_sprintf (state, bp, "%h", val);
393 sprintf (bp, "%s", va_arg (ap, char *));
399 fprintf (stderr, "?? format %c\n", p[-1]);
409 write_comments_(struct arcDisState * state,
414 if (state->commentBuffer != 0)
420 const char *name = post_address (state, limm_value + shimm);
423 WRITE_COMMENT (name);
425 for (i = 0; i < state->commNum; i++)
428 strcpy (state->commentBuffer, comment_prefix);
430 strcat (state->commentBuffer, ", ");
431 strncat (state->commentBuffer, state->comm[i],
432 sizeof (state->commentBuffer));
437 #define write_comments2(x) write_comments_ (state, x, is_limm, limm_value)
438 #define write_comments() write_comments2 (0)
440 static const char *condName[] =
443 "" , "z" , "nz" , "p" , "n" , "c" , "nc" , "v" ,
444 "nv" , "gt" , "ge" , "lt" , "le" , "hi" , "ls" , "pnz"
448 write_instr_name_(struct arcDisState * state,
449 const char * instrName,
451 int condCodeIsPartOfName,
457 strcpy (state->instrBuffer, instrName);
463 if (!condCodeIsPartOfName)
464 strcat (state->instrBuffer, ".");
469 cc = cond_code_name (state, cond);
474 strcat (state->instrBuffer, cc);
478 strcat (state->instrBuffer, ".f");
480 switch (state->nullifyMode)
483 strcat (state->instrBuffer, ".d");
485 case BR_exec_when_jump:
486 strcat (state->instrBuffer, ".jd");
491 strcat (state->instrBuffer, ".x");
494 strcat (state->instrBuffer, ".a");
497 strcat (state->instrBuffer, ".di");
500 #define write_instr_name() \
503 write_instr_name_(state, instrName,cond, condCodeIsPartOfName, \
504 flag, signExtend, addrWriteBack, directMem); \
505 formatString[0] = '\0'; \
511 op_LD0 = 0, op_LD1 = 1, op_ST = 2, op_3 = 3,
512 op_BC = 4, op_BLC = 5, op_LPC = 6, op_JC = 7,
513 op_ADD = 8, op_ADC = 9, op_SUB = 10, op_SBC = 11,
514 op_AND = 12, op_OR = 13, op_BIC = 14, op_XOR = 15
517 extern disassemble_info tm_print_insn_info;
520 dsmOneArcInst (bfd_vma addr, struct arcDisState * state)
522 int condCodeIsPartOfName = 0;
523 a4_decoding_class decodingClass;
524 const char * instrName;
538 int addrWriteBack = 0;
545 char formatString[60];
547 state->instructionLen = 4;
548 state->nullifyMode = BR_exec_when_no_jump;
552 state->_mem_load = 0;
553 state->_ea_present = 0;
554 state->_load_len = 0;
555 state->ea_reg1 = no_reg;
556 state->ea_reg2 = no_reg;
562 state->_opcode = OPCODE (state->words[0]);
564 decodingClass = CLASS_A4_ARITH; /* default! */
566 condCodeIsPartOfName=0;
570 state->flow = noflow;
573 if (state->commentBuffer)
574 state->commentBuffer[0] = '\0';
576 switch (state->_opcode)
579 switch (BITS (state->words[0],1,2))
583 state->_load_len = 4;
587 state->_load_len = 1;
591 state->_load_len = 2;
594 instrName = "??? (0[3])";
595 state->flow = invalid_instr;
598 decodingClass = CLASS_A4_LD0;
602 if (BIT (state->words[0],13))
605 decodingClass = CLASS_A4_LR;
609 switch (BITS (state->words[0], 10, 11))
613 state->_load_len = 4;
617 state->_load_len = 1;
621 state->_load_len = 2;
624 instrName = "??? (1[3])";
625 state->flow = invalid_instr;
628 decodingClass = CLASS_A4_LD1;
633 if (BIT (state->words[0], 25))
636 decodingClass = CLASS_A4_SR;
640 switch (BITS (state->words[0], 22, 23))
652 instrName = "??? (2[3])";
653 state->flow = invalid_instr;
656 decodingClass = CLASS_A4_ST;
661 decodingClass = CLASS_A4_OP3_GENERAL; /* default for opcode 3... */
662 switch (FIELDC (state->words[0]))
666 decodingClass = CLASS_A4_FLAG;
694 decodingClass = CLASS_A4_OP3_SUBOPC3F;
695 switch (FIELDD (state->words[0]))
708 state->flow=invalid_instr;
714 /* ARC Extension Library Instructions
715 NOTE: We assume that extension codes are these instrs. */
717 instrName = instruction_name (state,
719 FIELDC (state->words[0]),
724 state->flow = invalid_instr;
726 if (flags & IGNORE_FIRST_OPD)
743 if (BITS (state->words[0],9,9))
754 condCodeIsPartOfName = 1;
755 decodingClass = ((state->_opcode == op_JC) ? CLASS_A4_JC : CLASS_A4_BRANCH );
762 repeatsOp = (FIELDC (state->words[0]) == FIELDB (state->words[0]));
764 switch (state->_opcode)
767 instrName = (repeatsOp ? "asl" : "add");
770 instrName = (repeatsOp ? "rlc" : "adc");
773 instrName = (repeatsOp ? "mov" : "and");
778 case op_SUB: instrName = "sub";
780 case op_SBC: instrName = "sbc";
782 case op_OR: instrName = "or";
784 case op_BIC: instrName = "bic";
788 if (state->words[0] == 0x7fffffff)
790 /* NOP encoded as xor -1, -1, -1. */
792 decodingClass = CLASS_A4_OP3_SUBOPC3F;
799 instrName = instruction_name (state,state->_opcode,0,&flags);
800 /* if (instrName) printf("FLAGS=0x%x\n", flags); */
804 state->flow=invalid_instr;
806 if (flags & IGNORE_FIRST_OPD)
811 fieldAisReg = fieldBisReg = fieldCisReg = 1; /* Assume regs for now. */
812 flag = cond = is_shimm = is_limm = 0;
813 state->nullifyMode = BR_exec_when_no_jump; /* 0 */
814 signExtend = addrWriteBack = directMem = 0;
817 switch (decodingClass)
824 CHECK_FLAG_COND_NULLIFY ();
830 WRITE_FORMAT_COMMA_x (B);
832 WRITE_FORMAT_COMMA_x (C);
833 WRITE_NOP_COMMENT ();
834 arc_sprintf (state, state->operandBuffer, formatString,
835 fieldA, fieldB, fieldC);
841 WRITE_FORMAT_COMMA_x (C);
842 arc_sprintf (state, state->operandBuffer, formatString,
848 case CLASS_A4_OP3_GENERAL:
851 CHECK_FLAG_COND_NULLIFY ();
857 WRITE_FORMAT_COMMA_x (B);
858 WRITE_NOP_COMMENT ();
859 arc_sprintf (state, state->operandBuffer, formatString,
865 arc_sprintf (state, state->operandBuffer, formatString, fieldB);
872 CHECK_FLAG_COND_NULLIFY ();
873 flag = 0; /* This is the FLAG instruction -- it's redundant. */
877 arc_sprintf (state, state->operandBuffer, formatString, fieldB);
881 case CLASS_A4_BRANCH:
882 fieldA = BITS (state->words[0],7,26) << 2;
883 fieldA = (fieldA << 10) >> 10; /* Make it signed. */
885 CHECK_FLAG_COND_NULLIFY ();
889 /* This address could be a label we know. Convert it. */
890 if (state->_opcode != op_LPC /* LP */)
892 add_target (fieldA); /* For debugger. */
893 state->flow = state->_opcode == op_BLC /* BL */
896 /* indirect calls are achieved by "lr blink,[status];
897 lr dest<- func addr; j [dest]" */
900 strcat (formatString, "%s"); /* Address/label name. */
901 arc_sprintf (state, state->operandBuffer, formatString,
902 post_address (state, fieldA));
907 /* For op_JC -- jump to address specified.
908 Also covers jump and link--bit 9 of the instr. word
909 selects whether linked, thus "is_linked" is set above. */
912 CHECK_FLAG_COND_NULLIFY ();
917 fieldA = (fieldB >> 25) & 0x7F; /* Flags. */
918 fieldB = (fieldB & 0xFFFFFF) << 2;
919 state->flow = is_linked ? direct_call : direct_jump;
921 /* Screwy JLcc requires .jd mode to execute correctly
922 but we pretend it is .nd (no delay slot). */
923 if (is_linked && state->nullifyMode == BR_exec_when_jump)
924 state->nullifyMode = BR_exec_when_no_jump;
928 state->flow = is_linked ? indirect_call : indirect_jump;
929 /* We should also treat this as indirect call if NOT linked
930 but the preceding instruction was a "lr blink,[status]"
931 and we have a delay slot with "add blink,blink,2".
932 For now we can't detect such. */
933 state->register_for_indirect_jump = fieldB;
937 strcat (formatString,
938 IS_REG (B) ? "[%r]" : "%s"); /* Address/label name. */
942 WRITE_FORMAT_COMMA_x (A);
945 arc_sprintf (state, state->operandBuffer, formatString, fieldB, fieldA);
947 arc_sprintf (state, state->operandBuffer, formatString,
948 post_address (state, fieldB), fieldA);
954 B and C can be regs, or one (both?) can be limm. */
959 printf ("5:b reg %d %d c reg %d %d \n",
960 fieldBisReg,fieldB,fieldCisReg,fieldC);
962 state->_ea_present = 1;
964 state->ea_reg1 = fieldB;
966 state->_offset += fieldB;
968 state->ea_reg2 = fieldC;
970 state->_offset += fieldC;
971 state->_mem_load = 1;
973 directMem = BIT (state->words[0], 5);
974 addrWriteBack = BIT (state->words[0], 3);
975 signExtend = BIT (state->words[0], 0);
978 WRITE_FORMAT_x_COMMA_LB(A);
979 if (fieldBisReg || fieldB != 0)
980 WRITE_FORMAT_x_COMMA (B);
984 WRITE_FORMAT_x_RB (C);
985 arc_sprintf (state, state->operandBuffer, formatString,
986 fieldA, fieldB, fieldC);
991 /* LD instruction. */
994 fieldC = FIELDD (state->words[0]);
997 printf ("6:b reg %d %d c 0x%x \n",
998 fieldBisReg, fieldB, fieldC);
999 state->_ea_present = 1;
1000 state->_offset = fieldC;
1001 state->_mem_load = 1;
1003 state->ea_reg1 = fieldB;
1004 /* Field B is either a shimm (same as fieldC) or limm (different!)
1005 Say ea is not present, so only one of us will do the name lookup. */
1007 state->_offset += fieldB, state->_ea_present = 0;
1009 directMem = BIT (state->words[0],14);
1010 addrWriteBack = BIT (state->words[0],12);
1011 signExtend = BIT (state->words[0],9);
1013 write_instr_name ();
1014 WRITE_FORMAT_x_COMMA_LB (A);
1017 fieldB = state->_offset;
1018 WRITE_FORMAT_x_RB (B);
1023 if (fieldC != 0 && !BIT (state->words[0],13))
1026 WRITE_FORMAT_COMMA_x_RB (C);
1031 arc_sprintf (state, state->operandBuffer, formatString,
1032 fieldA, fieldB, fieldC);
1037 /* ST instruction. */
1040 fieldA = FIELDD(state->words[0]); /* shimm */
1043 if (dbg) printf("7:b reg %d %x off %x\n",
1044 fieldBisReg,fieldB,fieldA);
1045 state->_ea_present = 1;
1046 state->_offset = fieldA;
1048 state->ea_reg1 = fieldB;
1049 /* Field B is either a shimm (same as fieldA) or limm (different!)
1050 Say ea is not present, so only one of us will do the name lookup.
1051 (for is_limm we do the name translation here). */
1053 state->_offset += fieldB, state->_ea_present = 0;
1055 directMem = BIT (state->words[0], 26);
1056 addrWriteBack = BIT (state->words[0], 24);
1058 write_instr_name ();
1059 WRITE_FORMAT_x_COMMA_LB(C);
1063 fieldB = state->_offset;
1064 WRITE_FORMAT_x_RB (B);
1069 if (fieldBisReg && fieldA != 0)
1072 WRITE_FORMAT_COMMA_x_RB(A);
1077 arc_sprintf (state, state->operandBuffer, formatString,
1078 fieldC, fieldB, fieldA);
1079 write_comments2 (fieldA);
1083 /* SR instruction */
1087 write_instr_name ();
1088 WRITE_FORMAT_x_COMMA_LB(C);
1089 /* Try to print B as an aux reg if it is not a core reg. */
1093 arc_sprintf (state, state->operandBuffer, formatString, fieldC, fieldB);
1097 case CLASS_A4_OP3_SUBOPC3F:
1098 write_instr_name ();
1099 state->operandBuffer[0] = '\0';
1103 /* LR instruction */
1107 write_instr_name ();
1108 WRITE_FORMAT_x_COMMA_LB (A);
1109 /* Try to print B as an aux reg if it is not a core reg. */
1113 arc_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB);
1118 mwerror (state, "Bad decoding class in ARC disassembler");
1122 state->_cond = cond;
1123 return state->instructionLen = offset;
1127 /* Returns the name the user specified core extension register. */
1130 _coreRegName(void * arg ATTRIBUTE_UNUSED, int regval)
1132 return arcExtMap_coreRegName (regval);
1135 /* Returns the name the user specified AUX extension register. */
1138 _auxRegName(void *_this ATTRIBUTE_UNUSED, int regval)
1140 return arcExtMap_auxRegName(regval);
1143 /* Returns the name the user specified condition code name. */
1146 _condCodeName(void *_this ATTRIBUTE_UNUSED, int regval)
1148 return arcExtMap_condCodeName(regval);
1151 /* Returns the name the user specified extension instruction. */
1154 _instName (void *_this ATTRIBUTE_UNUSED, int majop, int minop, int *flags)
1156 return arcExtMap_instName(majop, minop, flags);
1159 /* Decode an instruction returning the size of the instruction
1160 in bytes or zero if unrecognized. */
1163 decodeInstr (bfd_vma address, /* Address of this instruction. */
1164 disassemble_info * info)
1168 struct arcDisState s; /* ARC Disassembler state. */
1169 void *stream = info->stream; /* Output stream. */
1170 fprintf_ftype func = info->fprintf_func;
1173 memset (&s, 0, sizeof(struct arcDisState));
1175 /* read first instruction */
1176 status = (*info->read_memory_func) (address, buffer, 4, info);
1179 (*info->memory_error_func) (status, address, info);
1182 if (info->endian == BFD_ENDIAN_LITTLE)
1183 s.words[0] = bfd_getl32(buffer);
1185 s.words[0] = bfd_getb32(buffer);
1186 /* Always read second word in case of limm. */
1188 /* We ignore the result since last insn may not have a limm. */
1189 status = (*info->read_memory_func) (address + 4, buffer, 4, info);
1190 if (info->endian == BFD_ENDIAN_LITTLE)
1191 s.words[1] = bfd_getl32(buffer);
1193 s.words[1] = bfd_getb32(buffer);
1196 s.coreRegName = _coreRegName;
1197 s.auxRegName = _auxRegName;
1198 s.condCodeName = _condCodeName;
1199 s.instName = _instName;
1202 bytes = dsmOneArcInst (address, (void *)& s);
1204 /* Display the disassembly instruction. */
1205 (*func) (stream, "%08lx ", s.words[0]);
1206 (*func) (stream, " ");
1207 (*func) (stream, "%-10s ", s.instrBuffer);
1209 if (__TRANSLATION_REQUIRED (s))
1211 bfd_vma addr = s.addresses[s.operandBuffer[1] - '0'];
1213 (*info->print_address_func) ((bfd_vma) addr, info);
1214 (*func) (stream, "\n");
1217 (*func) (stream, "%s",s.operandBuffer);
1219 return s.instructionLen;
1222 /* Return the print_insn function to use.
1223 Side effect: load (possibly empty) extension section */
1226 arc_get_disassembler (void *ptr)
1229 build_ARC_extmap (ptr);