2001-02-17 Philip Blundell <philb@gnu.org>
[binutils.git] / opcodes / arc-dis.c
blob3920006ee54ad33667f4475a3d2a2f6ee94deb66
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. */
19 #include <ansidecl.h>
20 #include <libiberty.h>
21 #include "dis-asm.h"
22 #include "opcode/arc.h"
23 #include "elf-bfd.h"
24 #include "elf/arc.h"
25 #include <string.h>
26 #include "opintl.h"
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include "arc-dis.h"
31 #include "arc-ext.h"
33 #ifndef dbg
34 #define dbg (0)
35 #endif
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) \
49 do \
50 { \
51 if (is_limm == 1 && !NEXT_WORD (1)) \
52 mwerror (state, _("Illegal limm reference in last instruction!\n")); \
53 a = state->words[1]; \
54 } \
55 while (0)
57 #define CHECK_FLAG_COND_NULLIFY() \
58 do \
59 { \
60 if (is_shimm == 0) \
61 { \
62 flag = BIT (state->words[0], 8); \
63 state->nullifyMode = BITS (state->words[0], 5, 6); \
64 cond = BITS (state->words[0], 0, 4); \
65 } \
66 } \
67 while (0)
69 #define CHECK_COND() \
70 do \
71 { \
72 if (is_shimm == 0) \
73 cond = BITS (state->words[0], 0, 4); \
74 } \
75 while (0)
77 #define CHECK_FIELD(field) \
78 do \
79 { \
80 if (field == 62) \
81 { \
82 is_limm++; \
83 field##isReg = 0; \
84 PUT_NEXT_WORD_IN (field); \
85 limm_value = field; \
86 } \
87 else if (field > 60) \
88 { \
89 field##isReg = 0; \
90 is_shimm++; \
91 flag = (field == 61); \
92 field = FIELDD (state->words[0]); \
93 } \
94 } \
95 while (0)
97 #define CHECK_FIELD_A() \
98 do \
99 { \
100 fieldA = FIELDA(state->words[0]); \
101 if (fieldA > 60) \
103 fieldAisReg = 0; \
104 fieldA = 0; \
107 while (0)
109 #define CHECK_FIELD_B() \
110 do \
112 fieldB = FIELDB (state->words[0]); \
113 CHECK_FIELD (fieldB); \
115 while (0)
117 #define CHECK_FIELD_C() \
118 do \
120 fieldC = FIELDC (state->words[0]); \
121 CHECK_FIELD (fieldC); \
123 while (0)
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; ";
148 static const char *
149 core_reg_name (state, val)
150 struct arcDisState * state;
151 int val;
153 if (state->coreRegName)
154 return (*state->coreRegName)(state->_this, val);
155 return 0;
158 static const char *
159 aux_reg_name (state, val)
160 struct arcDisState * state;
161 int val;
163 if (state->auxRegName)
164 return (*state->auxRegName)(state->_this, val);
165 return 0;
168 static const char *
169 cond_code_name (state, val)
170 struct arcDisState * state;
171 int val;
173 if (state->condCodeName)
174 return (*state->condCodeName)(state->_this, val);
175 return 0;
178 static const char *
179 instruction_name (state, op1, op2, flags)
180 struct arcDisState * state;
181 int op1;
182 int op2;
183 int * flags;
185 if (state->instName)
186 return (*state->instName)(state->_this, op1, op2, flags);
187 return 0;
190 static void
191 mwerror (state, msg)
192 struct arcDisState * state;
193 const char * msg;
195 if (state->err != 0)
196 (*state->err)(state->_this, (msg));
199 static const char *
200 post_address (state, addr)
201 struct arcDisState * state;
202 int addr;
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;
210 ++state->acnt;
211 j = i*3;
212 id[j+0] = '@';
213 id[j+1] = '0'+i;
214 id[j+2] = 0;
216 return id + j;
218 return "";
221 static void
222 my_sprintf (
223 struct arcDisState * state,
224 char * buf,
225 const char * format,
226 ...)
228 char *bp;
229 const char *p;
230 int size, leading_zero, regMap[2];
231 long auxNum;
232 va_list ap;
234 va_start (ap, format);
236 bp = buf;
237 *bp = 0;
238 p = format;
239 auxNum = -1;
240 regMap[0] = 0;
241 regMap[1] = 0;
243 while (1)
244 switch (*p++)
246 case 0:
247 goto DOCOMM; /* (return) */
248 default:
249 *bp++ = p[-1];
250 break;
251 case '%':
252 size = 0;
253 leading_zero = 0;
254 RETRY: ;
255 switch (*p++)
257 case '0':
258 case '1':
259 case '2':
260 case '3':
261 case '4':
262 case '5':
263 case '6':
264 case '7':
265 case '8':
266 case '9':
268 /* size. */
269 size = p[-1] - '0';
270 if (size == 0)
271 leading_zero = 1; /* e.g. %08x */
272 while (*p >= '0' && *p <= '9')
274 size = size * 10 + *p - '0';
275 p++;
277 goto RETRY;
279 #define inc_bp() bp = bp + strlen (bp)
281 case 'h':
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. */
288 if (u > 65536)
289 sprintf (bp, "0x%x_%04x", u >> 16, u & 0xffff);
290 else
291 sprintf (bp, "0x%x", u);
292 inc_bp ();
294 break;
295 case 'X': case 'x':
297 int val = va_arg (ap, int);
299 if (size != 0)
300 if (leading_zero)
301 sprintf (bp, "%0*x", size, val);
302 else
303 sprintf (bp, "%*x", size, val);
304 else
305 sprintf (bp, "%x", val);
306 inc_bp ();
308 break;
309 case 'd':
311 int val = va_arg (ap, int);
313 if (size != 0)
314 sprintf (bp, "%*d", size, val);
315 else
316 sprintf (bp, "%d", val);
317 inc_bp ();
319 break;
320 case 'r':
322 /* Register. */
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;
328 switch (val)
330 REG2NAME (26, "gp");
331 REG2NAME (27, "fp");
332 REG2NAME (28, "sp");
333 REG2NAME (29, "ilink1");
334 REG2NAME (30, "ilink2");
335 REG2NAME (31, "blink");
336 REG2NAME (60, "lp_count");
337 default:
339 const char * ext;
341 ext = core_reg_name (state, val);
342 if (ext)
343 sprintf (bp, "%s", ext);
344 else
345 sprintf (bp,"r%d",val);
347 break;
349 inc_bp ();
350 } break;
352 case 'a':
354 /* Aux Register. */
355 int val = va_arg (ap, int);
357 #define AUXREG2NAME(num, name) case num: sprintf (bp,name); break;
359 switch (val)
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");
367 default:
369 const char *ext;
371 ext = aux_reg_name (state, val);
372 if (ext)
373 sprintf (bp, "%s", ext);
374 else
375 my_sprintf (state, bp, "%h", val);
377 break;
379 inc_bp ();
381 break;
383 case 's':
385 sprintf (bp, "%s", va_arg (ap, char *));
386 inc_bp ();
388 break;
390 default:
391 fprintf (stderr, "?? format %c\n", p[-1]);
392 break;
396 DOCOMM: *bp = 0;
399 static void
400 write_comments_(state, shimm, is_limm, limm_value)
401 struct arcDisState * state;
402 int shimm;
403 int is_limm;
404 long limm_value;
406 if (state->commentBuffer != 0)
408 int i;
410 if (is_limm)
412 const char *name = post_address (state, limm_value + shimm);
414 if (*name != 0)
415 WRITE_COMMENT (name);
417 for (i = 0; i < state->commNum; i++)
419 if (i == 0)
420 strcpy (state->commentBuffer, comment_prefix);
421 else
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[] = {
432 /* 0..15. */
433 "" , "z" , "nz" , "p" , "n" , "c" , "nc" , "v" ,
434 "nv" , "gt" , "ge" , "lt" , "le" , "hi" , "ls" , "pnz"
437 static void
438 write_instr_name_(state, instrName, cond, condCodeIsPartOfName, flag, signExtend, addrWriteBack, directMem)
439 struct arcDisState * state;
440 const char * instrName;
441 int cond;
442 int condCodeIsPartOfName;
443 int flag;
444 int signExtend;
445 int addrWriteBack;
446 int directMem;
448 strcpy (state->instrBuffer, instrName);
450 if (cond > 0)
452 const char *cc = 0;
454 if (!condCodeIsPartOfName)
455 strcat (state->instrBuffer, ".");
457 if (cond < 16)
458 cc = condName[cond];
459 else
460 cc = cond_code_name (state, cond);
462 if (!cc)
463 cc = "???";
465 strcat (state->instrBuffer, cc);
468 if (flag)
469 strcat (state->instrBuffer, ".f");
471 switch (state->nullifyMode)
473 case BR_exec_always:
474 strcat (state->instrBuffer, ".d");
475 break;
476 case BR_exec_when_jump:
477 strcat (state->instrBuffer, ".jd");
478 break;
481 if (signExtend)
482 strcat (state->instrBuffer, ".x");
484 if (addrWriteBack)
485 strcat (state->instrBuffer, ".a");
487 if (directMem)
488 strcat (state->instrBuffer, ".di");
491 #define write_instr_name() \
492 do \
494 write_instr_name_(state, instrName,cond, condCodeIsPartOfName, \
495 flag, signExtend, addrWriteBack, directMem); \
496 formatString[0] = '\0'; \
498 while (0)
500 enum {
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;
509 static int
510 dsmOneArcInst (addr, state)
511 bfd_vma addr;
512 struct arcDisState * state;
514 int condCodeIsPartOfName = 0;
515 int decodingClass;
516 const char * instrName;
517 int repeatsOp = 0;
518 int fieldAisReg = 1;
519 int fieldBisReg = 1;
520 int fieldCisReg = 1;
521 int fieldA;
522 int fieldB;
523 int fieldC = 0;
524 int flag = 0;
525 int cond = 0;
526 int is_shimm = 0;
527 int is_limm = 0;
528 long limm_value = 0;
529 int signExtend = 0;
530 int addrWriteBack = 0;
531 int directMem = 0;
532 int is_linked = 0;
533 int offset = 0;
534 int usesAuxReg = 0;
535 int flags;
536 int ignoreFirstOpd;
537 char formatString[60];
539 state->instructionLen = 4;
540 state->nullifyMode = BR_exec_when_no_jump;
541 state->opWidth = 12;
542 state->isBranch = 0;
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;
549 state->_offset = 0;
551 if (! NEXT_WORD (0))
552 return 0;
554 state->_opcode = OPCODE (state->words[0]);
555 instrName = 0;
556 decodingClass = 0; /* default! */
557 repeatsOp = 0;
558 condCodeIsPartOfName=0;
559 state->commNum = 0;
560 state->tcnt = 0;
561 state->acnt = 0;
562 state->flow = noflow;
563 ignoreFirstOpd = 0;
565 if (state->commentBuffer)
566 state->commentBuffer[0] = '\0';
568 switch (state->_opcode)
570 case op_LD0:
571 switch (BITS (state->words[0],1,2))
573 case 0:
574 instrName = "ld";
575 state->_load_len = 4;
576 break;
577 case 1:
578 instrName = "ldb";
579 state->_load_len = 1;
580 break;
581 case 2:
582 instrName = "ldw";
583 state->_load_len = 2;
584 break;
585 default:
586 instrName = "??? (0[3])";
587 state->flow = invalid_instr;
588 break;
590 decodingClass = 5;
591 break;
593 case op_LD1:
594 if (BIT (state->words[0],13))
596 instrName = "lr";
597 decodingClass = 10;
599 else
601 switch (BITS (state->words[0],10,11))
603 case 0:
604 instrName = "ld";
605 state->_load_len = 4;
606 break;
607 case 1:
608 instrName = "ldb";
609 state->_load_len = 1;
610 break;
611 case 2:
612 instrName = "ldw";
613 state->_load_len = 2;
614 break;
615 default:
616 instrName = "??? (1[3])";
617 state->flow = invalid_instr;
618 break;
620 decodingClass = 6;
622 break;
624 case op_ST:
625 if (BIT (state->words[0],25))
627 instrName = "sr";
628 decodingClass = 8;
630 else
632 switch (BITS (state->words[0],22,23))
634 case 0:
635 instrName = "st";
636 break;
637 case 1:
638 instrName = "stb";
639 break;
640 case 2:
641 instrName = "stw";
642 break;
643 default:
644 instrName = "??? (2[3])";
645 state->flow = invalid_instr;
646 break;
648 decodingClass = 7;
650 break;
652 case op_3:
653 decodingClass = 1; /* default for opcode 3... */
654 switch (FIELDC (state->words[0]))
656 case 0:
657 instrName = "flag";
658 decodingClass = 2;
659 break;
660 case 1:
661 instrName = "asr";
662 break;
663 case 2:
664 instrName = "lsr";
665 break;
666 case 3:
667 instrName = "ror";
668 break;
669 case 4:
670 instrName = "rrc";
671 break;
672 case 5:
673 instrName = "sexb";
674 break;
675 case 6:
676 instrName = "sexw";
677 break;
678 case 7:
679 instrName = "extb";
680 break;
681 case 8:
682 instrName = "extw";
683 break;
684 case 0x3f:
686 decodingClass = 9;
687 switch( FIELDD (state->words[0]) )
689 case 0:
690 instrName = "brk";
691 break;
692 case 1:
693 instrName = "sleep";
694 break;
695 case 2:
696 instrName = "swi";
697 break;
698 default:
699 instrName = "???";
700 state->flow=invalid_instr;
701 break;
704 break;
706 /* ARC Extension Library Instructions
707 NOTE: We assume that extension codes are these instrs. */
708 default:
709 instrName = instruction_name (state,
710 state->_opcode,
711 FIELDC (state->words[0]),
712 & flags);
713 if (!instrName)
715 instrName = "???";
716 state->flow = invalid_instr;
718 if (flags & IGNORE_FIRST_OPD)
719 ignoreFirstOpd = 1;
720 break;
722 break;
724 case op_BC:
725 instrName = "b";
726 case op_BLC:
727 if (!instrName)
728 instrName = "bl";
729 case op_LPC:
730 if (!instrName)
731 instrName = "lp";
732 case op_JC:
733 if (!instrName)
735 if (BITS (state->words[0],9,9))
737 instrName = "jl";
738 is_linked = 1;
740 else
742 instrName = "j";
743 is_linked = 0;
746 condCodeIsPartOfName = 1;
747 decodingClass = ((state->_opcode == op_JC) ? 4 : 3);
748 state->isBranch = 1;
749 break;
751 case op_ADD:
752 case op_ADC:
753 case op_AND:
754 repeatsOp = (FIELDC (state->words[0]) == FIELDB (state->words[0]));
755 decodingClass = 0;
757 switch (state->_opcode)
759 case op_ADD:
760 instrName = (repeatsOp ? "asl" : "add");
761 break;
762 case op_ADC:
763 instrName = (repeatsOp ? "rlc" : "adc");
764 break;
765 case op_AND:
766 instrName = (repeatsOp ? "mov" : "and");
767 break;
769 break;
771 case op_SUB: instrName = "sub";
772 break;
773 case op_SBC: instrName = "sbc";
774 break;
775 case op_OR: instrName = "or";
776 break;
777 case op_BIC: instrName = "bic";
778 break;
780 case op_XOR:
781 if (state->words[0] == 0x7fffffff)
783 /* nop encoded as xor -1, -1, -1 */
784 instrName = "nop";
785 decodingClass = 9;
787 else
788 instrName = "xor";
789 break;
791 default:
792 instrName = instruction_name (state,state->_opcode,0,&flags);
793 /* if (instrName) printf("FLAGS=0x%x\n", flags); */
794 if (!instrName)
796 instrName = "???";
797 state->flow=invalid_instr;
799 if (flags & IGNORE_FIRST_OPD)
800 ignoreFirstOpd = 1;
801 break;
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;
808 usesAuxReg = 0;
810 switch (decodingClass)
812 case 0:
813 CHECK_FIELD_A ();
814 CHECK_FIELD_B ();
815 if (!repeatsOp)
816 CHECK_FIELD_C ();
817 CHECK_FLAG_COND_NULLIFY ();
819 write_instr_name ();
820 if (!ignoreFirstOpd)
822 WRITE_FORMAT_x (A);
823 WRITE_FORMAT_COMMA_x (B);
824 if (!repeatsOp)
825 WRITE_FORMAT_COMMA_x (C);
826 WRITE_NOP_COMMENT ();
827 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB, fieldC);
829 else
831 WRITE_FORMAT_x (B);
832 if (!repeatsOp)
833 WRITE_FORMAT_COMMA_x (C);
834 my_sprintf (state, state->operandBuffer, formatString, fieldB, fieldC);
836 write_comments ();
837 break;
839 case 1:
840 CHECK_FIELD_A ();
841 CHECK_FIELD_B ();
842 CHECK_FLAG_COND_NULLIFY ();
844 write_instr_name ();
845 if (!ignoreFirstOpd)
847 WRITE_FORMAT_x (A);
848 WRITE_FORMAT_COMMA_x (B);
849 WRITE_NOP_COMMENT ();
850 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB);
852 else
854 WRITE_FORMAT_x (B);
855 my_sprintf (state, state->operandBuffer, formatString, fieldB);
857 write_comments ();
858 break;
860 case 2:
861 CHECK_FIELD_B ();
862 CHECK_FLAG_COND_NULLIFY ();
863 flag = 0; /* this is the FLAG instruction -- it's redundant */
865 write_instr_name ();
866 WRITE_FORMAT_x (B);
867 my_sprintf (state, state->operandBuffer, formatString, fieldB);
868 write_comments ();
869 break;
871 case 3:
872 fieldA = BITS (state->words[0],7,26) << 2;
873 fieldA = (fieldA << 10) >> 10; /* make it signed */
874 fieldA += addr + 4;
875 CHECK_FLAG_COND_NULLIFY ();
876 flag = 0;
878 write_instr_name ();
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 */
884 ? direct_call
885 : direct_jump;
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));
892 write_comments ();
893 break;
895 case 4:
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. */
899 fieldA = 0;
900 CHECK_FIELD_B ();
901 CHECK_FLAG_COND_NULLIFY ();
903 if (!fieldBisReg)
905 fieldAisReg = 0;
906 fieldA = (fieldB >> 25) & 0x7F; /* flags */
907 fieldB = (fieldB & 0xFFFFFF) << 2;
908 state->flow = is_linked ? direct_call : direct_jump;
909 add_target (fieldB);
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;
915 else
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;
925 write_instr_name ();
926 strcat (formatString,
927 IS_REG (B) ? "[%r]" : "%s"); /* address/label name */
928 if (fieldA != 0)
930 fieldAisReg = 0;
931 WRITE_FORMAT_COMMA_x (A);
933 if (IS_REG (B))
934 my_sprintf (state, state->operandBuffer, formatString, fieldB, fieldA);
935 else
936 my_sprintf (state, state->operandBuffer, formatString,
937 post_address (state, fieldB), fieldA);
938 write_comments ();
939 break;
941 case 5:
942 /* LD instruction.
943 B and C can be regs, or one (both?) can be limm. */
944 CHECK_FIELD_A ();
945 CHECK_FIELD_B ();
946 CHECK_FIELD_C ();
947 if (dbg)
948 printf ("5:b reg %d %d c reg %d %d \n",
949 fieldBisReg,fieldB,fieldCisReg,fieldC);
950 state->_offset = 0;
951 state->_ea_present = 1;
952 if (fieldBisReg)
953 state->ea_reg1 = fieldB;
954 else
955 state->_offset += fieldB;
956 if (fieldCisReg)
957 state->ea_reg2 = fieldC;
958 else
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);
966 write_instr_name ();
967 WRITE_FORMAT_x_COMMA_LB(A);
968 if (fieldBisReg || fieldB != 0)
969 WRITE_FORMAT_x_COMMA (B);
970 else
971 fieldB = fieldC;
973 WRITE_FORMAT_x_RB (C);
974 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB, fieldC);
975 write_comments ();
976 break;
978 case 6:
979 /* LD instruction. */
980 CHECK_FIELD_B ();
981 CHECK_FIELD_A ();
982 fieldC = FIELDD (state->words[0]);
984 if (dbg)
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;
990 if (fieldBisReg)
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. */
994 else
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);
1003 if (!fieldBisReg)
1005 fieldB = state->_offset;
1006 WRITE_FORMAT_x_RB (B);
1008 else
1010 WRITE_FORMAT_x (B);
1011 if (fieldC != 0 && !BIT (state->words[0],13))
1013 fieldCisReg = 0;
1014 WRITE_FORMAT_COMMA_x_RB (C);
1016 else
1017 WRITE_FORMAT_RB ();
1019 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB, fieldC);
1020 write_comments ();
1021 break;
1023 case 7:
1024 /* ST instruction. */
1025 CHECK_FIELD_B();
1026 CHECK_FIELD_C();
1027 fieldA = FIELDD(state->words[0]); /* shimm */
1029 /* [B,A offset] */
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;
1034 if (fieldBisReg)
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). */
1039 else
1040 state->_offset += fieldB, state->_ea_present = 0;
1042 directMem = BIT(state->words[0],26);
1043 addrWriteBack = BIT(state->words[0],24);
1045 write_instr_name();
1046 WRITE_FORMAT_x_COMMA_LB(C);
1048 if (!fieldBisReg)
1050 fieldB = state->_offset;
1051 WRITE_FORMAT_x_RB(B);
1053 else
1055 WRITE_FORMAT_x(B);
1056 if (fieldBisReg && fieldA != 0)
1058 fieldAisReg = 0;
1059 WRITE_FORMAT_COMMA_x_RB(A);
1061 else
1062 WRITE_FORMAT_RB();
1064 my_sprintf (state, state->operandBuffer, formatString, fieldC, fieldB, fieldA);
1065 write_comments2(fieldA);
1066 break;
1067 case 8:
1068 /* SR instruction */
1069 CHECK_FIELD_B();
1070 CHECK_FIELD_C();
1072 write_instr_name();
1073 WRITE_FORMAT_x_COMMA_LB(C);
1074 /* Try to print B as an aux reg if it is not a core reg. */
1075 usesAuxReg = 1;
1076 WRITE_FORMAT_x(B);
1077 WRITE_FORMAT_RB();
1078 my_sprintf (state, state->operandBuffer, formatString, fieldC, fieldB);
1079 write_comments();
1080 break;
1082 case 9:
1083 write_instr_name();
1084 state->operandBuffer[0] = '\0';
1085 break;
1087 case 10:
1088 /* LR instruction */
1089 CHECK_FIELD_A();
1090 CHECK_FIELD_B();
1092 write_instr_name();
1093 WRITE_FORMAT_x_COMMA_LB(A);
1094 /* Try to print B as an aux reg if it is not a core reg. */
1095 usesAuxReg = 1;
1096 WRITE_FORMAT_x(B);
1097 WRITE_FORMAT_RB();
1098 my_sprintf (state, state->operandBuffer, formatString, fieldA, fieldB);
1099 write_comments();
1100 break;
1102 case 11:
1103 CHECK_COND();
1104 write_instr_name();
1105 state->operandBuffer[0] = '\0';
1106 break;
1108 default:
1109 mwerror (state, "Bad decoding class in ARC disassembler");
1110 break;
1113 state->_cond = cond;
1114 return state->instructionLen = offset;
1118 /* Returns the name the user specified core extension register. */
1119 static const char *
1120 _coreRegName(arg, regval)
1121 void * arg ATTRIBUTE_UNUSED;
1122 int regval;
1124 return arcExtMap_coreRegName (regval);
1127 /* Returns the name the user specified AUX extension register. */
1128 static const char *
1129 _auxRegName(void *_this ATTRIBUTE_UNUSED, int regval)
1131 return arcExtMap_auxRegName(regval);
1135 /* Returns the name the user specified condition code name. */
1136 static const char *
1137 _condCodeName(void *_this ATTRIBUTE_UNUSED, int regval)
1139 return arcExtMap_condCodeName(regval);
1142 /* Returns the name the user specified extension instruction. */
1143 static const char *
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. */
1151 static int
1152 decodeInstr (address, info)
1153 bfd_vma address; /* Address of this instruction. */
1154 disassemble_info * info;
1156 int status;
1157 bfd_byte buffer[4];
1158 struct arcDisState s; /* ARC Disassembler state */
1159 void *stream = info->stream; /* output stream */
1160 fprintf_ftype func = info->fprintf_func;
1161 int bytes;
1163 memset (&s, 0, sizeof(struct arcDisState));
1165 /* read first instruction */
1166 status = (*info->read_memory_func) (address, buffer, 4, info);
1167 if (status != 0)
1169 (*info->memory_error_func) (status, address, info);
1170 return 0;
1172 if (info->endian == BFD_ENDIAN_LITTLE)
1173 s.words[0] = bfd_getl32(buffer);
1174 else
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);
1182 else
1183 s.words[1] = bfd_getb32(buffer);
1185 s._this = &s;
1186 s.coreRegName = _coreRegName;
1187 s.auxRegName = _auxRegName;
1188 s.condCodeName = _condCodeName;
1189 s.instName = _instName;
1191 /* disassemble */
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");
1206 else
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 */
1214 disassembler_ftype
1215 arc_get_disassembler (void *ptr)
1217 if (ptr)
1218 build_ARC_extmap (ptr);
1219 return decodeInstr;