1 /* Instruction printing code for the ARC.
2 Copyright (C) 1994, 1995, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Doug Evans (dje@cygnus.com).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <libiberty.h>
22 #include "opcode/arc.h"
37 #define BIT(word,n) ((word) & (1 << n))
38 #define BITS(word,s,e) (((word) << (31 - e)) >> (s + (31 - e)))
39 #define OPCODE(word) (BITS ((word), 27, 31))
40 #define FIELDA(word) (BITS ((word), 21, 26))
41 #define FIELDB(word) (BITS ((word), 15, 20))
42 #define FIELDC(word) (BITS ((word), 9, 14))
44 /* FIELD D is signed in all of its uses, so we make sure argument is
45 treated as signed for bit shifting purposes: */
46 #define FIELDD(word) (BITS (((signed int)word), 0, 8))
48 #define PUT_NEXT_WORD_IN(a) \
51 if (is_limm == 1 && !NEXT_WORD (1)) \
52 mwerror (state, _("Illegal limm reference in last instruction!\n")); \
53 a = state->words[1]; \
57 #define CHECK_FLAG_COND_NULLIFY() \
62 flag = BIT (state->words[0], 8); \
63 state->nullifyMode = BITS (state->words[0], 5, 6); \
64 cond = BITS (state->words[0], 0, 4); \
69 #define CHECK_COND() \
73 cond = BITS (state->words[0], 0, 4); \
77 #define CHECK_FIELD(field) \
84 PUT_NEXT_WORD_IN (field); \
87 else if (field > 60) \
91 flag = (field == 61); \
92 field = FIELDD (state->words[0]); \
97 #define CHECK_FIELD_A() \
100 fieldA = FIELDA(state->words[0]); \
109 #define CHECK_FIELD_B() \
112 fieldB = FIELDB (state->words[0]); \
113 CHECK_FIELD (fieldB); \
117 #define CHECK_FIELD_C() \
120 fieldC = FIELDC (state->words[0]); \
121 CHECK_FIELD (fieldC); \
125 #define IS_SMALL(x) (((field##x) < 256) && ((field##x) > -257))
126 #define IS_REG(x) (field##x##isReg)
127 #define WRITE_FORMAT_LB_Rx_RB(x) WRITE_FORMAT(x,"[","]","","")
128 #define WRITE_FORMAT_x_COMMA_LB(x) WRITE_FORMAT(x,"",",[","",",[")
129 #define WRITE_FORMAT_COMMA_x_RB(x) WRITE_FORMAT(x,",","]",",","]")
130 #define WRITE_FORMAT_x_RB(x) WRITE_FORMAT(x,"","]","","]")
131 #define WRITE_FORMAT_COMMA_x(x) WRITE_FORMAT(x,",","",",","")
132 #define WRITE_FORMAT_x_COMMA(x) WRITE_FORMAT(x,"",",","",",")
133 #define WRITE_FORMAT_x(x) WRITE_FORMAT(x,"","","","")
134 #define WRITE_FORMAT(x,cb1,ca1,cb,ca) strcat (formatString, \
135 (IS_REG (x) ? cb1"%r"ca1 : \
136 usesAuxReg ? cb"%a"ca : \
137 IS_SMALL (x) ? cb"%d"ca : cb"%h"ca))
138 #define WRITE_FORMAT_RB() strcat (formatString, "]")
139 #define WRITE_COMMENT(str) (state->comm[state->commNum++] = (str))
140 #define WRITE_NOP_COMMENT() if (!fieldAisReg && !flag) WRITE_COMMENT ("nop");
142 #define NEXT_WORD(x) (offset += 4, state->words[x])
144 #define add_target(x) (state->targets[state->tcnt++] = (x))
146 static char comment_prefix
[] = "\t; ";
149 core_reg_name (state
, val
)
150 struct arcDisState
* state
;
153 if (state
->coreRegName
)
154 return (*state
->coreRegName
)(state
->_this
, val
);
159 aux_reg_name (state
, val
)
160 struct arcDisState
* state
;
163 if (state
->auxRegName
)
164 return (*state
->auxRegName
)(state
->_this
, val
);
169 cond_code_name (state
, val
)
170 struct arcDisState
* state
;
173 if (state
->condCodeName
)
174 return (*state
->condCodeName
)(state
->_this
, val
);
179 instruction_name (state
, op1
, op2
, flags
)
180 struct arcDisState
* state
;
186 return (*state
->instName
)(state
->_this
, op1
, op2
, flags
);
192 struct arcDisState
* state
;
196 (*state
->err
)(state
->_this
, (msg
));
200 post_address (state
, addr
)
201 struct arcDisState
* state
;
204 static char id
[3 * ARRAY_SIZE (state
->addresses
)];
205 int j
, i
= state
->acnt
;
207 if (i
< ((int) ARRAY_SIZE (state
->addresses
)))
209 state
->addresses
[i
] = addr
;
223 struct arcDisState
* state
,
230 int size
, leading_zero
, regMap
[2];
234 va_start (ap
, format
);
247 goto DOCOMM
; /* (return) */
271 leading_zero
= 1; /* e.g. %08x */
272 while (*p
>= '0' && *p
<= '9')
274 size
= size
* 10 + *p
- '0';
279 #define inc_bp() bp = bp + strlen (bp)
283 unsigned u
= va_arg (ap
, int);
285 /* Hex. We can change the format to 0x%08x in
286 one place, here, if we wish.
287 We add underscores for easy reading. */
289 sprintf (bp
, "0x%x_%04x", u
>> 16, u
& 0xffff);
291 sprintf (bp
, "0x%x", u
);
297 int val
= va_arg (ap
, int);
301 sprintf (bp
, "%0*x", size
, val
);
303 sprintf (bp
, "%*x", size
, val
);
305 sprintf (bp
, "%x", val
);
311 int val
= va_arg (ap
, int);
314 sprintf (bp
, "%*d", size
, val
);
316 sprintf (bp
, "%d", val
);
323 int val
= va_arg (ap
, int);
325 #define REG2NAME(num, name) case num: sprintf (bp, ""name); \
326 regMap[(num < 32) ? 0 : 1] |= 1 << (num - ((num < 32) ? 0 : 32)); break;
333 REG2NAME (29, "ilink1");
334 REG2NAME (30, "ilink2");
335 REG2NAME (31, "blink");
336 REG2NAME (60, "lp_count");
341 ext
= core_reg_name (state
, val
);
343 sprintf (bp
, "%s", ext
);
345 sprintf (bp
,"r%d",val
);
355 int val
= va_arg (ap
, int);
357 #define AUXREG2NAME(num, name) case num: sprintf (bp,name); break;
361 AUXREG2NAME (0x0, "status");
362 AUXREG2NAME (0x1, "semaphore");
363 AUXREG2NAME (0x2, "lp_start");
364 AUXREG2NAME (0x3, "lp_end");
365 AUXREG2NAME (0x4, "identity");
366 AUXREG2NAME (0x5, "debug");
371 ext
= aux_reg_name (state
, val
);
373 sprintf (bp
, "%s", ext
);
375 my_sprintf (state
, bp
, "%h", val
);
385 sprintf (bp
, "%s", va_arg (ap
, char *));
391 fprintf (stderr
, "?? format %c\n", p
[-1]);
400 write_comments_(state
, shimm
, is_limm
, limm_value
)
401 struct arcDisState
* state
;
406 if (state
->commentBuffer
!= 0)
412 const char *name
= post_address (state
, limm_value
+ shimm
);
415 WRITE_COMMENT (name
);
417 for (i
= 0; i
< state
->commNum
; i
++)
420 strcpy (state
->commentBuffer
, comment_prefix
);
422 strcat (state
->commentBuffer
, ", ");
423 strncat (state
->commentBuffer
, state
->comm
[i
], sizeof (state
->commentBuffer
));
428 #define write_comments2(x) write_comments_(state, x, is_limm, limm_value)
429 #define write_comments() write_comments2(0)
431 static const char *condName
[] = {
433 "" , "z" , "nz" , "p" , "n" , "c" , "nc" , "v" ,
434 "nv" , "gt" , "ge" , "lt" , "le" , "hi" , "ls" , "pnz"
438 write_instr_name_(state
, instrName
, cond
, condCodeIsPartOfName
, flag
, signExtend
, addrWriteBack
, directMem
)
439 struct arcDisState
* state
;
440 const char * instrName
;
442 int condCodeIsPartOfName
;
448 strcpy (state
->instrBuffer
, instrName
);
454 if (!condCodeIsPartOfName
)
455 strcat (state
->instrBuffer
, ".");
460 cc
= cond_code_name (state
, cond
);
465 strcat (state
->instrBuffer
, cc
);
469 strcat (state
->instrBuffer
, ".f");
471 switch (state
->nullifyMode
)
474 strcat (state
->instrBuffer
, ".d");
476 case BR_exec_when_jump
:
477 strcat (state
->instrBuffer
, ".jd");
482 strcat (state
->instrBuffer
, ".x");
485 strcat (state
->instrBuffer
, ".a");
488 strcat (state
->instrBuffer
, ".di");
491 #define write_instr_name() \
494 write_instr_name_(state, instrName,cond, condCodeIsPartOfName, \
495 flag, signExtend, addrWriteBack, directMem); \
496 formatString[0] = '\0'; \
501 op_LD0
= 0, op_LD1
= 1, op_ST
= 2, op_3
= 3,
502 op_BC
= 4, op_BLC
= 5, op_LPC
= 6, op_JC
= 7,
503 op_ADD
= 8, op_ADC
= 9, op_SUB
= 10, op_SBC
= 11,
504 op_AND
= 12, op_OR
= 13, op_BIC
= 14, op_XOR
= 15
507 extern disassemble_info tm_print_insn_info
;
510 dsmOneArcInst (addr
, state
)
512 struct arcDisState
* state
;
514 int condCodeIsPartOfName
= 0;
516 const char * instrName
;
530 int addrWriteBack
= 0;
537 char formatString
[60];
539 state
->instructionLen
= 4;
540 state
->nullifyMode
= BR_exec_when_no_jump
;
544 state
->_mem_load
= 0;
545 state
->_ea_present
= 0;
546 state
->_load_len
= 0;
547 state
->ea_reg1
= no_reg
;
548 state
->ea_reg2
= no_reg
;
554 state
->_opcode
= OPCODE (state
->words
[0]);
556 decodingClass
= 0; /* default! */
558 condCodeIsPartOfName
=0;
562 state
->flow
= noflow
;
565 if (state
->commentBuffer
)
566 state
->commentBuffer
[0] = '\0';
568 switch (state
->_opcode
)
571 switch (BITS (state
->words
[0],1,2))
575 state
->_load_len
= 4;
579 state
->_load_len
= 1;
583 state
->_load_len
= 2;
586 instrName
= "??? (0[3])";
587 state
->flow
= invalid_instr
;
594 if (BIT (state
->words
[0],13))
601 switch (BITS (state
->words
[0],10,11))
605 state
->_load_len
= 4;
609 state
->_load_len
= 1;
613 state
->_load_len
= 2;
616 instrName
= "??? (1[3])";
617 state
->flow
= invalid_instr
;
625 if (BIT (state
->words
[0],25))
632 switch (BITS (state
->words
[0],22,23))
644 instrName
= "??? (2[3])";
645 state
->flow
= invalid_instr
;
653 decodingClass
= 1; /* default for opcode 3... */
654 switch (FIELDC (state
->words
[0]))
687 switch( FIELDD (state
->words
[0]) )
700 state
->flow
=invalid_instr
;
706 /* ARC Extension Library Instructions
707 NOTE: We assume that extension codes are these instrs. */
709 instrName
= instruction_name (state
,
711 FIELDC (state
->words
[0]),
716 state
->flow
= invalid_instr
;
718 if (flags
& IGNORE_FIRST_OPD
)
735 if (BITS (state
->words
[0],9,9))
746 condCodeIsPartOfName
= 1;
747 decodingClass
= ((state
->_opcode
== op_JC
) ? 4 : 3);
754 repeatsOp
= (FIELDC (state
->words
[0]) == FIELDB (state
->words
[0]));
757 switch (state
->_opcode
)
760 instrName
= (repeatsOp
? "asl" : "add");
763 instrName
= (repeatsOp
? "rlc" : "adc");
766 instrName
= (repeatsOp
? "mov" : "and");
771 case op_SUB
: instrName
= "sub";
773 case op_SBC
: instrName
= "sbc";
775 case op_OR
: instrName
= "or";
777 case op_BIC
: instrName
= "bic";
781 if (state
->words
[0] == 0x7fffffff)
783 /* nop encoded as xor -1, -1, -1 */
792 instrName
= instruction_name (state
,state
->_opcode
,0,&flags
);
793 /* if (instrName) printf("FLAGS=0x%x\n", flags); */
797 state
->flow
=invalid_instr
;
799 if (flags
& IGNORE_FIRST_OPD
)
804 fieldAisReg
= fieldBisReg
= fieldCisReg
= 1; /* Assume regs for now. */
805 flag
= cond
= is_shimm
= is_limm
= 0;
806 state
->nullifyMode
= BR_exec_when_no_jump
; /* 0 */
807 signExtend
= addrWriteBack
= directMem
= 0;
810 switch (decodingClass
)
817 CHECK_FLAG_COND_NULLIFY ();
823 WRITE_FORMAT_COMMA_x (B
);
825 WRITE_FORMAT_COMMA_x (C
);
826 WRITE_NOP_COMMENT ();
827 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldA
, fieldB
, fieldC
);
833 WRITE_FORMAT_COMMA_x (C
);
834 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldB
, fieldC
);
842 CHECK_FLAG_COND_NULLIFY ();
848 WRITE_FORMAT_COMMA_x (B
);
849 WRITE_NOP_COMMENT ();
850 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldA
, fieldB
);
855 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldB
);
862 CHECK_FLAG_COND_NULLIFY ();
863 flag
= 0; /* this is the FLAG instruction -- it's redundant */
867 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldB
);
872 fieldA
= BITS (state
->words
[0],7,26) << 2;
873 fieldA
= (fieldA
<< 10) >> 10; /* make it signed */
875 CHECK_FLAG_COND_NULLIFY ();
879 /* This address could be a label we know. Convert it. */
880 if (state
->_opcode
!= op_LPC
/* LP */)
882 add_target (fieldA
); /* For debugger. */
883 state
->flow
= state
->_opcode
== op_BLC
/* BL */
886 /* indirect calls are achieved by "lr blink,[status];
887 lr dest<- func addr; j [dest]" */
890 strcat (formatString
, "%s"); /* address/label name */
891 my_sprintf (state
, state
->operandBuffer
, formatString
, post_address (state
, fieldA
));
896 /* For op_JC -- jump to address specified.
897 Also covers jump and link--bit 9 of the instr. word
898 selects whether linked, thus "is_linked" is set above. */
901 CHECK_FLAG_COND_NULLIFY ();
906 fieldA
= (fieldB
>> 25) & 0x7F; /* flags */
907 fieldB
= (fieldB
& 0xFFFFFF) << 2;
908 state
->flow
= is_linked
? direct_call
: direct_jump
;
910 /* screwy JLcc requires .jd mode to execute correctly
911 * but we pretend it is .nd (no delay slot). */
912 if (is_linked
&& state
->nullifyMode
== BR_exec_when_jump
)
913 state
->nullifyMode
= BR_exec_when_no_jump
;
917 state
->flow
= is_linked
? indirect_call
: indirect_jump
;
918 /* We should also treat this as indirect call if NOT linked
919 * but the preceding instruction was a "lr blink,[status]"
920 * and we have a delay slot with "add blink,blink,2".
921 * For now we can't detect such. */
922 state
->register_for_indirect_jump
= fieldB
;
926 strcat (formatString
,
927 IS_REG (B
) ? "[%r]" : "%s"); /* address/label name */
931 WRITE_FORMAT_COMMA_x (A
);
934 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldB
, fieldA
);
936 my_sprintf (state
, state
->operandBuffer
, formatString
,
937 post_address (state
, fieldB
), fieldA
);
943 B and C can be regs, or one (both?) can be limm. */
948 printf ("5:b reg %d %d c reg %d %d \n",
949 fieldBisReg
,fieldB
,fieldCisReg
,fieldC
);
951 state
->_ea_present
= 1;
953 state
->ea_reg1
= fieldB
;
955 state
->_offset
+= fieldB
;
957 state
->ea_reg2
= fieldC
;
959 state
->_offset
+= fieldC
;
960 state
->_mem_load
= 1;
962 directMem
= BIT (state
->words
[0],5);
963 addrWriteBack
= BIT (state
->words
[0],3);
964 signExtend
= BIT (state
->words
[0],0);
967 WRITE_FORMAT_x_COMMA_LB(A
);
968 if (fieldBisReg
|| fieldB
!= 0)
969 WRITE_FORMAT_x_COMMA (B
);
973 WRITE_FORMAT_x_RB (C
);
974 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldA
, fieldB
, fieldC
);
979 /* LD instruction. */
982 fieldC
= FIELDD (state
->words
[0]);
985 printf ("6:b reg %d %d c 0x%x \n",
986 fieldBisReg
, fieldB
, fieldC
);
987 state
->_ea_present
= 1;
988 state
->_offset
= fieldC
;
989 state
->_mem_load
= 1;
991 state
->ea_reg1
= fieldB
;
992 /* field B is either a shimm (same as fieldC) or limm (different!)
993 Say ea is not present, so only one of us will do the name lookup. */
995 state
->_offset
+= fieldB
, state
->_ea_present
= 0;
997 directMem
= BIT (state
->words
[0],14);
998 addrWriteBack
= BIT (state
->words
[0],12);
999 signExtend
= BIT (state
->words
[0],9);
1001 write_instr_name ();
1002 WRITE_FORMAT_x_COMMA_LB (A
);
1005 fieldB
= state
->_offset
;
1006 WRITE_FORMAT_x_RB (B
);
1011 if (fieldC
!= 0 && !BIT (state
->words
[0],13))
1014 WRITE_FORMAT_COMMA_x_RB (C
);
1019 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldA
, fieldB
, fieldC
);
1024 /* ST instruction. */
1027 fieldA
= FIELDD(state
->words
[0]); /* shimm */
1030 if (dbg
) printf("7:b reg %d %x off %x\n",
1031 fieldBisReg
,fieldB
,fieldA
);
1032 state
->_ea_present
= 1;
1033 state
->_offset
= fieldA
;
1035 state
->ea_reg1
= fieldB
;
1036 /* field B is either a shimm (same as fieldA) or limm (different!)
1037 Say ea is not present, so only one of us will do the name lookup.
1038 (for is_limm we do the name translation here). */
1040 state
->_offset
+= fieldB
, state
->_ea_present
= 0;
1042 directMem
= BIT(state
->words
[0],26);
1043 addrWriteBack
= BIT(state
->words
[0],24);
1046 WRITE_FORMAT_x_COMMA_LB(C
);
1050 fieldB
= state
->_offset
;
1051 WRITE_FORMAT_x_RB(B
);
1056 if (fieldBisReg
&& fieldA
!= 0)
1059 WRITE_FORMAT_COMMA_x_RB(A
);
1064 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldC
, fieldB
, fieldA
);
1065 write_comments2(fieldA
);
1068 /* SR instruction */
1073 WRITE_FORMAT_x_COMMA_LB(C
);
1074 /* Try to print B as an aux reg if it is not a core reg. */
1078 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldC
, fieldB
);
1084 state
->operandBuffer
[0] = '\0';
1088 /* LR instruction */
1093 WRITE_FORMAT_x_COMMA_LB(A
);
1094 /* Try to print B as an aux reg if it is not a core reg. */
1098 my_sprintf (state
, state
->operandBuffer
, formatString
, fieldA
, fieldB
);
1105 state
->operandBuffer
[0] = '\0';
1109 mwerror (state
, "Bad decoding class in ARC disassembler");
1113 state
->_cond
= cond
;
1114 return state
->instructionLen
= offset
;
1118 /* Returns the name the user specified core extension register. */
1120 _coreRegName(arg
, regval
)
1121 void * arg ATTRIBUTE_UNUSED
;
1124 return arcExtMap_coreRegName (regval
);
1127 /* Returns the name the user specified AUX extension register. */
1129 _auxRegName(void *_this ATTRIBUTE_UNUSED
, int regval
)
1131 return arcExtMap_auxRegName(regval
);
1135 /* Returns the name the user specified condition code name. */
1137 _condCodeName(void *_this ATTRIBUTE_UNUSED
, int regval
)
1139 return arcExtMap_condCodeName(regval
);
1142 /* Returns the name the user specified extension instruction. */
1144 _instName (void *_this ATTRIBUTE_UNUSED
, int majop
, int minop
, int *flags
)
1146 return arcExtMap_instName(majop
, minop
, flags
);
1149 /* Decode an instruction returning the size of the instruction
1150 in bytes or zero if unrecognized. */
1152 decodeInstr (address
, info
)
1153 bfd_vma address
; /* Address of this instruction. */
1154 disassemble_info
* info
;
1158 struct arcDisState s
; /* ARC Disassembler state */
1159 void *stream
= info
->stream
; /* output stream */
1160 fprintf_ftype func
= info
->fprintf_func
;
1163 memset (&s
, 0, sizeof(struct arcDisState
));
1165 /* read first instruction */
1166 status
= (*info
->read_memory_func
) (address
, buffer
, 4, info
);
1169 (*info
->memory_error_func
) (status
, address
, info
);
1172 if (info
->endian
== BFD_ENDIAN_LITTLE
)
1173 s
.words
[0] = bfd_getl32(buffer
);
1175 s
.words
[0] = bfd_getb32(buffer
);
1176 /* always read second word in case of limm */
1178 /* we ignore the result since last insn may not have a limm */
1179 status
= (*info
->read_memory_func
) (address
+ 4, buffer
, 4, info
);
1180 if (info
->endian
== BFD_ENDIAN_LITTLE
)
1181 s
.words
[1] = bfd_getl32(buffer
);
1183 s
.words
[1] = bfd_getb32(buffer
);
1186 s
.coreRegName
= _coreRegName
;
1187 s
.auxRegName
= _auxRegName
;
1188 s
.condCodeName
= _condCodeName
;
1189 s
.instName
= _instName
;
1192 bytes
= dsmOneArcInst(address
, (void *)&s
);
1194 /* display the disassembly instruction */
1195 (*func
) (stream
, "%08x ", s
.words
[0]);
1196 (*func
) (stream
, " ");
1198 (*func
) (stream
, "%-10s ", s
.instrBuffer
);
1200 if (__TRANSLATION_REQUIRED(s
))
1202 bfd_vma addr
= s
.addresses
[s
.operandBuffer
[1] - '0'];
1203 (*info
->print_address_func
) ((bfd_vma
) addr
, info
);
1204 (*func
) (stream
, "\n");
1207 (*func
) (stream
, "%s",s
.operandBuffer
);
1208 return s
.instructionLen
;
1211 /* Return the print_insn function to use.
1212 Side effect: load (possibly empty) extension section */
1215 arc_get_disassembler (void *ptr
)
1218 build_ARC_extmap (ptr
);