1 /* Instruction printing code for the ARC.
2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "libiberty.h"
23 #include "opcode/arc.h"
38 /* Classification of the opcodes for the decoder to print
45 /* All branches other than JC. */
48 /* All loads other than immediate
54 /* All single operand instructions. */
55 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))
68 /* FIELD D is signed in all of its uses, so we make sure argument is
69 treated as signed for bit shifting purposes: */
70 #define FIELDD(word) (BITS (((signed int)word), 0, 8))
72 #define PUT_NEXT_WORD_IN(a) \
75 if (is_limm == 1 && !NEXT_WORD (1)) \
76 mwerror (state, _("Illegal limm reference in last instruction!\n")); \
77 a = state->words[1]; \
81 #define CHECK_FLAG_COND_NULLIFY() \
86 flag = BIT (state->words[0], 8); \
87 state->nullifyMode = BITS (state->words[0], 5, 6); \
88 cond = BITS (state->words[0], 0, 4); \
93 #define CHECK_COND() \
97 cond = BITS (state->words[0], 0, 4); \
101 #define CHECK_FIELD(field) \
108 PUT_NEXT_WORD_IN (field); \
109 limm_value = field; \
111 else if (field > 60) \
115 flag = (field == 61); \
116 field = FIELDD (state->words[0]); \
121 #define CHECK_FIELD_A() \
124 fieldA = FIELDA (state->words[0]); \
133 #define CHECK_FIELD_B() \
136 fieldB = FIELDB (state->words[0]); \
137 CHECK_FIELD (fieldB); \
141 #define CHECK_FIELD_C() \
144 fieldC = FIELDC (state->words[0]); \
145 CHECK_FIELD (fieldC); \
149 #define IS_SMALL(x) (((field##x) < 256) && ((field##x) > -257))
150 #define IS_REG(x) (field##x##isReg)
151 #define WRITE_FORMAT_LB_Rx_RB(x) WRITE_FORMAT(x,"[","]","","")
152 #define WRITE_FORMAT_x_COMMA_LB(x) WRITE_FORMAT(x,"",",[","",",[")
153 #define WRITE_FORMAT_COMMA_x_RB(x) WRITE_FORMAT(x,",","]",",","]")
154 #define WRITE_FORMAT_x_RB(x) WRITE_FORMAT(x,"","]","","]")
155 #define WRITE_FORMAT_COMMA_x(x) WRITE_FORMAT(x,",","",",","")
156 #define WRITE_FORMAT_x_COMMA(x) WRITE_FORMAT(x,"",",","",",")
157 #define WRITE_FORMAT_x(x) WRITE_FORMAT(x,"","","","")
158 #define WRITE_FORMAT(x,cb1,ca1,cb,ca) strcat (formatString, \
159 (IS_REG (x) ? cb1"%r"ca1 : \
160 usesAuxReg ? cb"%a"ca : \
161 IS_SMALL (x) ? cb"%d"ca : cb"%h"ca))
162 #define WRITE_FORMAT_RB() strcat (formatString, "]")
163 #define WRITE_COMMENT(str) (state->comm[state->commNum++] = (str))
164 #define WRITE_NOP_COMMENT() if (!fieldAisReg && !flag) WRITE_COMMENT ("nop");
166 #define NEXT_WORD(x) (offset += 4, state->words[x])
168 #define add_target(x) (state->targets[state->tcnt++] = (x))
170 static char comment_prefix[] = "\t; ";
172 static const char *core_reg_name PARAMS ((struct arcDisState *, int));
173 static const char *aux_reg_name PARAMS ((struct arcDisState *, int));
174 static const char *cond_code_name PARAMS ((struct arcDisState *, int));
175 static const char *instruction_name
176 PARAMS ((struct arcDisState *, int, int, int *));
177 static void mwerror PARAMS ((struct arcDisState *, const char *));
178 static const char *post_address PARAMS ((struct arcDisState *, int));
179 static void write_comments_
180 PARAMS ((struct arcDisState *, int, int, long int));
181 static void write_instr_name_
182 PARAMS ((struct arcDisState *, const char *, int, int, int, int, int, int));
183 static int dsmOneArcInst PARAMS ((bfd_vma, struct arcDisState *));
184 static const char *_coreRegName PARAMS ((void *, int));
185 static int decodeInstr PARAMS ((bfd_vma, disassemble_info *));
188 core_reg_name (state, val)
189 struct arcDisState * state;
192 if (state->coreRegName)
193 return (*state->coreRegName)(state->_this, val);
198 aux_reg_name (state, val)
199 struct arcDisState * state;
202 if (state->auxRegName)
203 return (*state->auxRegName)(state->_this, val);
208 cond_code_name (state, val)
209 struct arcDisState * state;
212 if (state->condCodeName)
213 return (*state->condCodeName)(state->_this, val);
218 instruction_name (state, op1, op2, flags)
219 struct arcDisState * state;
225 return (*state->instName)(state->_this, op1, op2, flags);
231 struct arcDisState * state;
235 (*state->err)(state->_this, (msg));
239 post_address (state, addr)
240 struct arcDisState * state;
243 static char id[3 * ARRAY_SIZE (state->addresses)];
244 int j, i = state->acnt;
246 if (i < ((int) ARRAY_SIZE (state->addresses)))
248 state->addresses[i] = addr;
260 static void my_sprintf PARAMS ((struct arcDisState *, char *, const char *,
264 my_sprintf VPARAMS ((struct arcDisState *state, char *buf, const char *format,
269 int size, leading_zero, regMap[2];
272 VA_OPEN (ap, format);
273 VA_FIXEDARG (ap, struct arcDisState *, state);
274 VA_FIXEDARG (ap, char *, buf);
275 VA_FIXEDARG (ap, const char *, format);
288 goto DOCOMM; /* (return) */
312 leading_zero = 1; /* e.g. %08x */
313 while (*p >= '0' && *p <= '9')
315 size = size * 10 + *p - '0';
320 #define inc_bp() bp = bp + strlen (bp)
324 unsigned u = va_arg (ap, int);
326 /* Hex. We can change the format to 0x%08x in
327 one place, here, if we wish.
328 We add underscores for easy reading. */
330 sprintf (bp, "0x%x_%04x", u >> 16, u & 0xffff);
332 sprintf (bp, "0x%x", u);
338 int val = va_arg (ap, int);
342 sprintf (bp, "%0*x", size, val);
344 sprintf (bp, "%*x", size, val);
346 sprintf (bp, "%x", val);
352 int val = va_arg (ap, int);
355 sprintf (bp, "%*d", size, val);
357 sprintf (bp, "%d", val);
364 int val = va_arg (ap, int);
366 #define REG2NAME(num, name) case num: sprintf (bp, ""name); \
367 regMap[(num < 32) ? 0 : 1] |= 1 << (num - ((num < 32) ? 0 : 32)); break;
374 REG2NAME (29, "ilink1");
375 REG2NAME (30, "ilink2");
376 REG2NAME (31, "blink");
377 REG2NAME (60, "lp_count");
382 ext = core_reg_name (state, val);
384 sprintf (bp, "%s", ext);
386 sprintf (bp,"r%d",val);
396 int val = va_arg (ap, int);
398 #define AUXREG2NAME(num, name) case num: sprintf (bp,name); break;
402 AUXREG2NAME (0x0, "status");
403 AUXREG2NAME (0x1, "semaphore");
404 AUXREG2NAME (0x2, "lp_start");
405 AUXREG2NAME (0x3, "lp_end");
406 AUXREG2NAME (0x4, "identity");
407 AUXREG2NAME (0x5, "debug");
412 ext = aux_reg_name (state, val);
414 sprintf (bp, "%s", ext);
416 my_sprintf (state, bp, "%h", val);
426 sprintf (bp, "%s", va_arg (ap, char *));
432 fprintf (stderr, "?? format %c\n", p[-1]);
442 write_comments_(state, shimm, is_limm, limm_value)
443 struct arcDisState * state;
448 if (state->commentBuffer != 0)
454 const char *name = post_address (state, limm_value + shimm);
457 WRITE_COMMENT (name);
459 for (i = 0; i < state->commNum; i++)
462 strcpy (state->commentBuffer, comment_prefix);
464 strcat (state->commentBuffer, ", ");
465 strncat (state->commentBuffer, state->comm[i],
466 sizeof (state->commentBuffer));
471 #define write_comments2(x) write_comments_(state, x, is_limm, limm_value)
472 #define write_comments() write_comments2(0)
474 static const char *condName[] = {
476 "" , "z" , "nz" , "p" , "n" , "c" , "nc" , "v" ,
477 "nv" , "gt" , "ge" , "lt" , "le" , "hi" , "ls" , "pnz"
481 write_instr_name_(state, instrName, cond, condCodeIsPartOfName, flag, signExtend, addrWriteBack, directMem)
482 struct arcDisState * state;
483 const char * instrName;
485 int condCodeIsPartOfName;
491 strcpy (state->instrBuffer, instrName);
497 if (!condCodeIsPartOfName)
498 strcat (state->instrBuffer, ".");
503 cc = cond_code_name (state, cond);
508 strcat (state->instrBuffer, cc);
512 strcat (state->instrBuffer, ".f");
514 switch (state->nullifyMode)
517 strcat (state->instrBuffer, ".d");
519 case BR_exec_when_jump:
520 strcat (state->instrBuffer, ".jd");
525 strcat (state->instrBuffer, ".x");
528 strcat (state->instrBuffer, ".a");
531 strcat (state->instrBuffer, ".di");
534 #define write_instr_name() \
537 write_instr_name_(state, instrName,cond, condCodeIsPartOfName, \
538 flag, signExtend, addrWriteBack, directMem); \
539 formatString[0] = '\0'; \
544 op_LD0 = 0, op_LD1 = 1, op_ST = 2, op_3 = 3,
545 op_BC = 4, op_BLC = 5, op_LPC = 6, op_JC = 7,
546 op_ADD = 8, op_ADC = 9, op_SUB = 10, op_SBC = 11,
547 op_AND = 12, op_OR = 13, op_BIC = 14, op_XOR = 15
550 extern disassemble_info tm_print_insn_info;
553 dsmOneArcInst (addr, state)
555 struct arcDisState * state;
557 int condCodeIsPartOfName = 0;
558 a4_decoding_class decodingClass;
559 const char * instrName;
573 int addrWriteBack = 0;
580 char formatString[60];
582 state->instructionLen = 4;
583 state->nullifyMode = BR_exec_when_no_jump;
587 state->_mem_load = 0;
588 state->_ea_present = 0;
589 state->_load_len = 0;
590 state->ea_reg1 = no_reg;
591 state->ea_reg2 = no_reg;
597 state->_opcode = OPCODE (state->words[0]);
599 decodingClass = CLASS_A4_ARITH; /* default! */
601 condCodeIsPartOfName=0;
605 state->flow = noflow;
608 if (state->commentBuffer)
609 state->commentBuffer[0] = '\0';
611 switch (state->_opcode)
614 switch (BITS (state->words[0],1,2))
618 state->_load_len = 4;
622 state->_load_len = 1;
626 state->_load_len = 2;
629 instrName = "??? (0[3])";
630 state->flow = invalid_instr;
633 decodingClass = CLASS_A4_LD0;
637 if (BIT (state->words[0],13))
640 decodingClass = CLASS_A4_LR;
644 switch (BITS (state->words[0],10,11))
648 state->_load_len = 4;
652 state->_load_len = 1;
656 state->_load_len = 2;
659 instrName = "??? (1[3])";
660 state->flow = invalid_instr;
663 decodingClass = CLASS_A4_LD1;
668 if (BIT (state->words[0],25))
671 decodingClass = CLASS_A4_SR;
675 switch (BITS (state->words[0],22,23))
687 instrName = "??? (2[3])";
688 state->flow = invalid_instr;
691 decodingClass = CLASS_A4_ST;
696 decodingClass = CLASS_A4_OP3_GENERAL; /* default for opcode 3... */
697 switch (FIELDC (state->words[0]))
701 decodingClass = CLASS_A4_FLAG;
729 decodingClass = CLASS_A4_OP3_SUBOPC3F;
730 switch( FIELDD (state->words[0]) )
743 state->flow=invalid_instr;
749 /* ARC Extension Library Instructions
750 NOTE: We assume that extension codes are these instrs. */
752 instrName = instruction_name (state,
754 FIELDC (state->words[0]),
759 state->flow = invalid_instr;
761 if (flags & IGNORE_FIRST_OPD)
778 if (BITS (state->words[0],9,9))
789 condCodeIsPartOfName = 1;
790 decodingClass = ((state->_opcode == op_JC) ? CLASS_A4_JC : CLASS_A4_BRANCH );
797 repeatsOp = (FIELDC (state->words[0]) == FIELDB (state->words[0]));
799 switch (state->_opcode)
802 instrName = (repeatsOp ? "asl" : "add");
805 instrName = (repeatsOp ? "rlc" : "adc");
808 instrName = (repeatsOp ? "mov" : "and");
813 case op_SUB: instrName = "sub";
815 case op_SBC: instrName = "sbc";
817 case op_OR: instrName = "or";
819 case op_BIC: instrName = "bic";
823 if (state->words[0] == 0x7fffffff)
825 /* nop encoded as xor -1, -1, -1 */
827 decodingClass = CLASS_A4_OP3_SUBOPC3F;
834 instrName = instruction_name (state,state->_opcode,0,&flags);
835 /* if (instrName) printf("FLAGS=0x%x\n", flags); */
839 state->flow=invalid_instr;
841 if (flags & IGNORE_FIRST_OPD)
846 fieldAisReg = fieldBisReg = fieldCisReg = 1; /* Assume regs for now. */
847 flag = cond = is_shimm = is_limm = 0;
848 state->nullifyMode = BR_exec_when_no_jump; /* 0 */
849 signExtend = addrWriteBack = directMem = 0;
852 switch (decodingClass)
859 CHECK_FLAG_COND_NULLIFY ();
865 WRITE_FORMAT_COMMA_x (B);
867 WRITE_FORMAT_COMMA_x (C);
868 WRITE_NOP_COMMENT ();
869 my_sprintf (state, state->operandBuffer, formatString,
870 fieldA, fieldB, fieldC);
876 WRITE_FORMAT_COMMA_x (C);
877 my_sprintf (state, state->operandBuffer, formatString,
883 case CLASS_A4_OP3_GENERAL:
886 CHECK_FLAG_COND_NULLIFY ();
892 WRITE_FORMAT_COMMA_x (B);
893 WRITE_NOP_COMMENT ();
894 my_sprintf (state, state->operandBuffer, formatString,
900 my_sprintf (state, state->operandBuffer, formatString, fieldB);
907 CHECK_FLAG_COND_NULLIFY ();
908 flag = 0; /* this is the FLAG instruction -- it's redundant */
912 my_sprintf (state, state->operandBuffer, formatString, fieldB);
916 case CLASS_A4_BRANCH:
917 fieldA = BITS (state->words[0],7,26) << 2;
918 fieldA = (fieldA << 10) >> 10; /* make it signed */
920 CHECK_FLAG_COND_NULLIFY ();
924 /* This address could be a label we know. Convert it. */
925 if (state->_opcode != op_LPC /* LP */)
927 add_target (fieldA); /* For debugger. */
928 state->flow = state->_opcode == op_BLC /* BL */
931 /* indirect calls are achieved by "lr blink,[status];
932 lr dest<- func addr; j [dest]" */
935 strcat (formatString, "%s"); /* address/label name */
936 my_sprintf (state, state->operandBuffer, formatString,
937 post_address (state, fieldA));
942 /* For op_JC -- jump to address specified.
943 Also covers jump and link--bit 9 of the instr. word
944 selects whether linked, thus "is_linked" is set above. */
947 CHECK_FLAG_COND_NULLIFY ();
952 fieldA = (fieldB >> 25) & 0x7F; /* flags */
953 fieldB = (fieldB & 0xFFFFFF) << 2;
954 state->flow = is_linked ? direct_call : direct_jump;
956 /* screwy JLcc requires .jd mode to execute correctly
957 * but we pretend it is .nd (no delay slot). */
958 if (is_linked && state->nullifyMode == BR_exec_when_jump)
959 state->nullifyMode = BR_exec_when_no_jump;
963 state->flow = is_linked ? indirect_call : indirect_jump;
964 /* We should also treat this as indirect call if NOT linked
965 * but the preceding instruction was a "lr blink,[status]"
966 * and we have a delay slot with "add blink,blink,2".
967 * For now we can't detect such. */
968 state->register_for_indirect_jump = fieldB;
972 strcat (formatString,
973 IS_REG (B) ? "[%r]" : "%s"); /* address/label name */
977 WRITE_FORMAT_COMMA_x (A);
980 my_sprintf (state, state->operandBuffer, formatString, fieldB, fieldA);
982 my_sprintf (state, state->operandBuffer, formatString,
983 post_address (state, fieldB), fieldA);
989 B and C can be regs, or one (both?) can be limm. */
994 printf ("5:b reg %d %d c reg %d %d \n",
995 fieldBisReg,fieldB,fieldCisReg,fieldC);
997 state->_ea_present = 1;
999 state->ea_reg1 = fieldB;
1001 state->_offset += fieldB;
1003 state->ea_reg2 = fieldC;
1005 state->_offset += fieldC;
1006 state->_mem_load = 1;
1008 directMem = BIT (state->words[0],5);
1009 addrWriteBack = BIT (state->words[0],3);
1010 signExtend = BIT (state->words[0],0);
1012 write_instr_name ();
1013 WRITE_FORMAT_x_COMMA_LB(A);
1014 if (fieldBisReg || fieldB != 0)
1015 WRITE_FORMAT_x_COMMA (B);
1019 WRITE_FORMAT_x_RB (C);
1020 my_sprintf (state, state->operandBuffer, formatString,
1021 fieldA, fieldB, fieldC);
1026 /* LD instruction. */
1029 fieldC = FIELDD (state->words[0]);
1032 printf ("6:b reg %d %d c 0x%x \n",
1033 fieldBisReg, fieldB, fieldC);
1034 state->_ea_present = 1;
1035 state->_offset = fieldC;
1036 state->_mem_load = 1;
1038 state->ea_reg1 = fieldB;
1039 /* field B is either a shimm (same as fieldC) or limm (different!)
1040 Say ea is not present, so only one of us will do the name lookup. */
1042 state->_offset += fieldB, state->_ea_present = 0;
1044 directMem = BIT (state->words[0],14);
1045 addrWriteBack = BIT (state->words[0],12);
1046 signExtend = BIT (state->words[0],9);
1048 write_instr_name ();
1049 WRITE_FORMAT_x_COMMA_LB (A);
1052 fieldB = state->_offset;
1053 WRITE_FORMAT_x_RB (B);
1058 if (fieldC != 0 && !BIT (state->words[0],13))
1061 WRITE_FORMAT_COMMA_x_RB (C);
1066 my_sprintf (state, state->operandBuffer, formatString,
1067 fieldA, fieldB, fieldC);
1072 /* ST instruction. */
1075 fieldA = FIELDD(state->words[0]); /* shimm */
1078 if (dbg) printf("7:b reg %d %x off %x\n",
1079 fieldBisReg,fieldB,fieldA);
1080 state->_ea_present = 1;
1081 state->_offset = fieldA;
1083 state->ea_reg1 = fieldB;
1084 /* field B is either a shimm (same as fieldA) or limm (different!)
1085 Say ea is not present, so only one of us will do the name lookup.
1086 (for is_limm we do the name translation here). */
1088 state->_offset += fieldB, state->_ea_present = 0;
1090 directMem = BIT(state->words[0],26);
1091 addrWriteBack = BIT(state->words[0],24);
1094 WRITE_FORMAT_x_COMMA_LB(C);
1098 fieldB = state->_offset;
1099 WRITE_FORMAT_x_RB(B);
1104 if (fieldBisReg && fieldA != 0)
1107 WRITE_FORMAT_COMMA_x_RB(A);
1112 my_sprintf (state, state->operandBuffer, formatString,
1113 fieldC, fieldB, fieldA);
1114 write_comments2(fieldA);
1118 /* SR instruction */
1123 WRITE_FORMAT_x_COMMA_LB(C);
1124 /* Try to print B as an aux reg if it is not a core reg. */
1128 my_sprintf (state, state->operandBuffer, formatString, fieldC, fieldB);
1132 case CLASS_A4_OP3_SUBOPC3F:
1134 state->operandBuffer[0] = '\0';
1138 /* LR instruction */
1143 WRITE_FORMAT_x_COMMA_LB(A);
1144 /* Try to print B as an aux reg if it is not a core reg. */
1148 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB);
1154 mwerror (state, "Bad decoding class in ARC disassembler");
1158 state->_cond = cond;
1159 return state->instructionLen = offset;
1163 /* Returns the name the user specified core extension register. */
1165 _coreRegName(arg, regval)
1166 void * arg ATTRIBUTE_UNUSED;
1169 return arcExtMap_coreRegName (regval);
1172 /* Returns the name the user specified AUX extension register. */
1174 _auxRegName(void *_this ATTRIBUTE_UNUSED, int regval)
1176 return arcExtMap_auxRegName(regval);
1180 /* Returns the name the user specified condition code name. */
1182 _condCodeName(void *_this ATTRIBUTE_UNUSED, int regval)
1184 return arcExtMap_condCodeName(regval);
1187 /* Returns the name the user specified extension instruction. */
1189 _instName (void *_this ATTRIBUTE_UNUSED, int majop, int minop, int *flags)
1191 return arcExtMap_instName(majop, minop, flags);
1194 /* Decode an instruction returning the size of the instruction
1195 in bytes or zero if unrecognized. */
1197 decodeInstr (address, info)
1198 bfd_vma address; /* Address of this instruction. */
1199 disassemble_info * info;
1203 struct arcDisState s; /* ARC Disassembler state */
1204 void *stream = info->stream; /* output stream */
1205 fprintf_ftype func = info->fprintf_func;
1208 memset (&s, 0, sizeof(struct arcDisState));
1210 /* read first instruction */
1211 status = (*info->read_memory_func) (address, buffer, 4, info);
1214 (*info->memory_error_func) (status, address, info);
1217 if (info->endian == BFD_ENDIAN_LITTLE)
1218 s.words[0] = bfd_getl32(buffer);
1220 s.words[0] = bfd_getb32(buffer);
1221 /* always read second word in case of limm */
1223 /* we ignore the result since last insn may not have a limm */
1224 status = (*info->read_memory_func) (address + 4, buffer, 4, info);
1225 if (info->endian == BFD_ENDIAN_LITTLE)
1226 s.words[1] = bfd_getl32(buffer);
1228 s.words[1] = bfd_getb32(buffer);
1231 s.coreRegName = _coreRegName;
1232 s.auxRegName = _auxRegName;
1233 s.condCodeName = _condCodeName;
1234 s.instName = _instName;
1237 bytes = dsmOneArcInst(address, (void *)&s);
1239 /* display the disassembly instruction */
1240 (*func) (stream, "%08x ", s.words[0]);
1241 (*func) (stream, " ");
1243 (*func) (stream, "%-10s ", s.instrBuffer);
1245 if (__TRANSLATION_REQUIRED(s))
1247 bfd_vma addr = s.addresses[s.operandBuffer[1] - '0'];
1248 (*info->print_address_func) ((bfd_vma) addr, info);
1249 (*func) (stream, "\n");
1252 (*func) (stream, "%s",s.operandBuffer);
1253 return s.instructionLen;
1256 /* Return the print_insn function to use.
1257 Side effect: load (possibly empty) extension section */
1260 arc_get_disassembler (void *ptr)
1263 build_ARC_extmap (ptr);