1 /* Disassembler code for CR16.
2 Copyright 2007, 2008 Free Software Foundation, Inc.
3 Contributed by M R Swami Reddy (MR.Swami.Reddy@nsc.com).
5 This file is part of GAS, GDB and the GNU binutils.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "opcode/cr16.h"
24 #include "libiberty.h"
26 /* String to print when opcode was not matched. */
27 #define ILLEGAL "illegal"
28 /* Escape to 16-bit immediate. */
29 #define ESCAPE_16_BIT 0xB
31 /* Extract 'n_bits' from 'a' starting from offset 'offs'. */
32 #define EXTRACT(a, offs, n_bits) \
33 (n_bits == 32 ? (((a) >> (offs)) & 0xffffffffL) \
34 : (((a) >> (offs)) & ((1 << (n_bits)) -1)))
36 /* Set Bit Mask - a mask to set all bits starting from offset 'offs'. */
37 #define SBM(offs) ((((1 << (32 - offs)) -1) << (offs)))
39 typedef unsigned long dwordU;
40 typedef unsigned short wordU;
48 /* Structure to map valid 'cinv' instruction options. */
52 /* Cinv printed string. */
54 /* Value corresponding to the string. */
59 /* CR16 'cinv' options mapping. */
60 const cinv_entry cr16_cinvs[] =
62 {"cinv[i]", "cinv [i]"},
63 {"cinv[i,u]", "cinv [i,u]"},
64 {"cinv[d]", "cinv [d]"},
65 {"cinv[d,u]", "cinv [d,u]"},
66 {"cinv[d,i]", "cinv [d,i]"},
67 {"cinv[d,i,u]", "cinv [d,i,u]"}
70 /* Number of valid 'cinv' instruction options. */
71 static int NUMCINVS = ARRAY_SIZE (cr16_cinvs);
73 /* Enum to distinguish different registers argument types. */
74 typedef enum REG_ARG_TYPE
76 /* General purpose register (r<N>). */
78 /*Processor register */
83 /* Current opcode table entry we're disassembling. */
84 const inst *instruction;
85 /* Current instruction we're disassembling. */
87 /* The current instruction is read into 3 consecutive words. */
89 /* Contains all words in appropriate order. */
91 /* Holds the current processed argument number. */
92 int processing_argument_number;
93 /* Nonzero means a IMM4 instruction. */
95 /* Nonzero means the instruction's original size is
96 incremented (escape sequence is used). */
100 /* Print the constant expression length. */
103 print_exp_len (int size)
126 /* Retrieve the number of operands for the current assembled instruction. */
129 get_number_of_operands (void)
133 for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
139 /* Return the bit size for a given operand. */
142 getbits (operand_type op)
145 return cr16_optab[op].bit_size;
150 /* Return the argument type of a given operand. */
153 getargtype (operand_type op)
156 return cr16_optab[op].arg_type;
161 /* Given a 'CC' instruction constant operand, return its corresponding
162 string. This routine is used when disassembling the 'CC' instruction. */
165 getccstring (unsigned cc)
167 return (char *) cr16_b_cond_tab[cc];
171 /* Given a 'cinv' instruction constant operand, return its corresponding
172 string. This routine is used when disassembling the 'cinv' instruction. */
175 getcinvstring (const char *str)
177 const cinv_entry *cinv;
179 for (cinv = cr16_cinvs; cinv < (cr16_cinvs + NUMCINVS); cinv++)
180 if (strcmp (cinv->istr, str) == 0)
186 /* Given the trap index in dispatch table, return its name.
187 This routine is used when disassembling the 'excp' instruction. */
190 gettrapstring (unsigned int index)
192 const trap_entry *trap;
194 for (trap = cr16_traps; trap < cr16_traps + NUMTRAPS; trap++)
195 if (trap->entry == index)
201 /* Given a register enum value, retrieve its name. */
206 const reg_entry *reg = cr16_regtab + r;
208 if (reg->type != CR16_R_REGTYPE)
214 /* Given a register pair enum value, retrieve its name. */
219 const reg_entry *reg = cr16_regptab + r;
221 if (reg->type != CR16_RP_REGTYPE)
227 /* Given a index register pair enum value, retrieve its name. */
230 getidxregpname (reg r)
232 const reg_entry *reg;
236 case 0: r = 0; break;
237 case 1: r = 2; break;
238 case 2: r = 4; break;
239 case 3: r = 6; break;
240 case 4: r = 8; break;
241 case 5: r = 10; break;
242 case 6: r = 3; break;
243 case 7: r = 5; break;
248 reg = cr16_regptab + r;
250 if (reg->type != CR16_RP_REGTYPE)
256 /* Getting a processor register name. */
259 getprocregname (int index)
263 for (r = cr16_pregtab; r < cr16_pregtab + NUMPREGS; r++)
264 if (r->image == index)
267 return "ILLEGAL REGISTER";
270 /* Getting a processor register name - 32 bit size. */
273 getprocpregname (int index)
277 for (r = cr16_pregptab; r < cr16_pregptab + NUMPREGPS; r++)
278 if (r->image == index)
281 return "ILLEGAL REGISTER";
284 /* START and END are relating 'allWords' struct, which is 48 bits size.
287 +---------+---------+---------+---------+
289 +---------+---------+---------+---------+
294 makelongparameter (ULONGLONG val, int start, int end)
298 p.val = (dwordU) EXTRACT (val, 48 - end, end - start);
299 p.nbits = end - start;
303 /* Build a mask of the instruction's 'constant' opcode,
304 based on the instruction's printing flags. */
309 unsigned long mask = SBM (instruction->match_bits);
311 /* Adjust mask for bcond with 32-bit size instruction. */
312 if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
318 /* Search for a matching opcode. Return 1 for success, 0 for failure. */
324 /* The instruction 'constant' opcode doewsn't exceed 32 bits. */
325 unsigned long doubleWord = (words[1] + (words[0] << 16)) & 0xffffffff;
327 /* Start searching from end of instruction table. */
328 instruction = &cr16_instruction[NUMOPCODES - 2];
330 /* Loop over instruction table until a full match is found. */
331 while (instruction >= cr16_instruction)
333 mask = build_mask ();
334 /* Adjust mask for bcond with 32-bit size instruction */
335 if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
338 if ((doubleWord & mask) == BIN (instruction->match,
339 instruction->match_bits))
347 /* Set the proper parameter value for different type of arguments. */
350 make_argument (argument * a, int start_bits)
355 if ((instruction->size == 3) && a->size >= 16)
363 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
364 inst_bit_size - start_bits);
369 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
370 inst_bit_size - start_bits);
375 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
376 inst_bit_size - start_bits);
381 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
382 inst_bit_size - start_bits);
387 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
388 inst_bit_size - start_bits);
393 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
394 inst_bit_size - start_bits);
400 if ((IS_INSN_MNEMONIC ("cbitb"))
401 || (IS_INSN_MNEMONIC ("sbitb"))
402 || (IS_INSN_MNEMONIC ("tbitb")))
403 p = makelongparameter (allWords, 8, 9);
405 p = makelongparameter (allWords, 9, 10);
407 p = makelongparameter (allWords, inst_bit_size - a->size, inst_bit_size);
412 p = makelongparameter (allWords, start_bits + 12, start_bits + 13);
414 p = makelongparameter (allWords, start_bits + 13, start_bits + 16);
416 if (inst_bit_size > 32)
418 p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
420 a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
422 else if (instruction->size == 2)
424 p = makelongparameter (allWords, inst_bit_size - 22, inst_bit_size);
425 a->constant = (p.val & 0xf) | (((p.val >>20) & 0x3) << 4)
426 | ((p.val >>14 & 0x3) << 6) | (((p.val >>7) & 0x1f) <<7);
428 else if (instruction->size == 1 && a->size == 0)
434 p = makelongparameter (allWords, inst_bit_size, inst_bit_size);
436 p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
437 inst_bit_size - start_bits);
442 p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
444 p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
449 if (instruction->size == 1)
450 p = makelongparameter (allWords, 12, 16);
452 p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
455 if (inst_bit_size > 32)
457 p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
459 a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
461 else if (instruction->size == 2)
463 p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
466 else if (instruction->size == 1 && a->size != 0)
468 p = makelongparameter (allWords, 4, 8);
469 if (IS_INSN_MNEMONIC ("loadw")
470 || IS_INSN_MNEMONIC ("loadd")
471 || IS_INSN_MNEMONIC ("storw")
472 || IS_INSN_MNEMONIC ("stord"))
473 a->constant = (p.val * 2);
477 else /* below case for 0x0(reg pair) */
484 if ((IS_INSN_TYPE (BRANCH_INS))
485 || (IS_INSN_MNEMONIC ("bal"))
486 || (IS_INSN_TYPE (CSTBIT_INS))
487 || (IS_INSN_TYPE (LD_STOR_INS)))
492 p = makelongparameter (allWords, 0, start_bits);
493 a->constant = ((((p.val&0xf00)>>4)) | (p.val&0xf));
497 if (instruction->size == 3)
499 p = makelongparameter (allWords, 16, inst_bit_size);
500 a->constant = ((((p.val>>16)&0xf) << 20)
501 | (((p.val>>24)&0xf) << 16)
504 else if (instruction->size == 2)
506 p = makelongparameter (allWords, 8, inst_bit_size);
512 p = makelongparameter (allWords, inst_bit_size - (start_bits +
513 a->size), inst_bit_size - start_bits);
520 p = makelongparameter (allWords, inst_bit_size -
521 (start_bits + a->size),
522 inst_bit_size - start_bits);
532 /* Print a single argument. */
535 print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
537 LONGLONG longdisp, mask;
541 PTR stream = info->stream;
542 fprintf_ftype func = info->fprintf_func;
547 func (stream, "%s", getregname (a->r));
551 func (stream, "%s", getregpname (a->rp));
555 func (stream, "%s", getprocregname (a->pr));
559 func (stream, "%s", getprocpregname (a->prp));
563 func (stream, "%s", getccstring (a->cc));
564 func (stream, "%s", "\t");
568 if (IS_INSN_MNEMONIC ("excp"))
570 func (stream, "%s", gettrapstring (a->constant));
573 else if ((IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS))
574 && ((instruction->size == 1) && (a->constant == 9)))
575 func (stream, "$%d", -1);
576 else if (INST_HAS_REG_LIST)
577 func (stream, "$0x%lx", a->constant +1);
578 else if (IS_INSN_TYPE (SHIFT_INS))
580 longdisp = a->constant;
581 mask = ((LONGLONG)1 << a->size) - 1;
582 if (longdisp & ((LONGLONG)1 << (a->size -1)))
585 longdisp = ~(longdisp) + 1;
587 a->constant = (unsigned long int) (longdisp & mask);
588 func (stream, "$%d", ((int)(sign_flag ? -a->constant :
592 func (stream, "$0x%lx", a->constant);
595 case 4 : case 5 : case 6 : case 8 :
596 func (stream, "%s", ":s"); break;
597 case 16 : case 20 : func (stream, "%s", ":m"); break;
598 case 24 : case 32 : func (stream, "%s", ":l"); break;
604 if (a->i_r == 0) func (stream, "[r12]");
605 if (a->i_r == 1) func (stream, "[r13]");
606 func (stream, "0x%lx", a->constant);
607 func (stream, "%s", print_exp_len (instruction->size * 16));
611 if (a->i_r == 0) func (stream, "[r12]");
612 if (a->i_r == 1) func (stream, "[r13]");
613 func (stream, "0x%lx", a->constant);
614 func (stream, "%s", print_exp_len (instruction->size * 16));
615 func (stream, "%s", getidxregpname (a->rp));
619 func (stream, "(%s)", getregname (a->r));
623 func (stream, "0x%lx", a->constant);
624 func (stream, "%s", print_exp_len (instruction->size * 16));
625 func (stream, "(%s)", getregname (a->r));
629 func (stream, "0x%lx", a->constant);
630 func (stream, "%s", print_exp_len (instruction->size * 16));
631 func (stream, "%s", getregpname (a->rp));
635 /*Removed the *2 part as because implicit zeros are no more required.
636 Have to fix this as this needs a bit of extension in terms of branch
638 if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
641 longdisp = a->constant;
642 /* REVISIT: To sync with WinIDEA and CR16 4.1tools, the below
644 /* longdisp <<= 1; */
645 mask = ((LONGLONG)1 << a->size) - 1;
651 if (longdisp & ((LONGLONG)1 << a->size))
654 longdisp = ~(longdisp) + 1;
664 longdisp = ~(longdisp) + 1;
669 func (stream, "Wrong offset used in branch/bal instruction");
672 a->constant = (unsigned long int) (longdisp & mask);
674 /* For branch Neq instruction it is 2*offset + 2. */
675 else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
676 a->constant = 2 * a->constant + 2;
678 if ((!IS_INSN_TYPE (CSTBIT_INS)) && (!IS_INSN_TYPE (LD_STOR_INS)))
679 (sign_flag) ? func (stream, "%s", "*-"): func (stream, "%s","*+");
681 func (stream, "%s", "0x");
682 number = ((relative ? memaddr : 0) +
683 (sign_flag ? ((- a->constant) & 0xffffffe) : a->constant));
685 (*info->print_address_func) ((number & ((1 << 24) - 1)), info);
687 func (stream, "%s", print_exp_len (instruction->size * 16));
695 /* Print all the arguments of CURRINSN instruction. */
698 print_arguments (ins *currInsn, bfd_vma memaddr, struct disassemble_info *info)
702 /* For "pop/push/popret RA instruction only. */
703 if ((IS_INSN_MNEMONIC ("pop")
704 || (IS_INSN_MNEMONIC ("popret")
705 || (IS_INSN_MNEMONIC ("push"))))
706 && currInsn->nargs == 1)
708 info->fprintf_func (info->stream, "RA");
712 for (i = 0; i < currInsn->nargs; i++)
714 processing_argument_number = i;
716 /* For "bal (ra), disp17" instruction only. */
717 if ((IS_INSN_MNEMONIC ("bal")) && (i == 0) && instruction->size == 2)
719 info->fprintf_func (info->stream, "(ra),");
723 if ((INST_HAS_REG_LIST) && (i == 2))
724 info->fprintf_func (info->stream, "RA");
726 print_arg (&currInsn->arg[i], memaddr, info);
728 if ((i != currInsn->nargs - 1) && (!IS_INSN_MNEMONIC ("b")))
729 info->fprintf_func (info->stream, ",");
733 /* Build the instruction's arguments. */
736 make_instruction (void)
741 for (i = 0; i < currInsn.nargs; i++)
745 memset (&a, 0, sizeof (a));
746 a.type = getargtype (instruction->operands[i].op_type);
747 a.size = getbits (instruction->operands[i].op_type);
748 shift = instruction->operands[i].shift;
750 make_argument (&a, shift);
754 /* Calculate instruction size (in bytes). */
755 currInsn.size = instruction->size + (size_changed ? 1 : 0);
760 /* Retrieve a single word from a given memory address. */
763 get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
769 status = info->read_memory_func (memaddr, buffer, 2, info);
772 insn = (wordU) bfd_getl16 (buffer);
777 /* Retrieve multiple words (3) from a given memory address. */
780 get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
785 for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
786 words[i] = get_word_at_PC (mem, info);
789 ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
792 /* Prints the instruction by calling print_arguments after proper matching. */
795 print_insn_cr16 (bfd_vma memaddr, struct disassemble_info *info)
797 int is_decoded; /* Nonzero means instruction has a match. */
799 /* Initialize global variables. */
803 /* Retrieve the encoding from current memory location. */
804 get_words_at_PC (memaddr, info);
805 /* Find a matching opcode in table. */
806 is_decoded = match_opcode ();
807 /* If found, print the instruction's mnemonic and arguments. */
808 if (is_decoded > 0 && (words[0] << 16 || words[1]) != 0)
810 if (strneq (instruction->mnemonic, "cinv", 4))
811 info->fprintf_func (info->stream,"%s", getcinvstring (instruction->mnemonic));
813 info->fprintf_func (info->stream, "%s", instruction->mnemonic);
815 if (((currInsn.nargs = get_number_of_operands ()) != 0)
816 && ! (IS_INSN_MNEMONIC ("b")))
817 info->fprintf_func (info->stream, "\t");
819 /* For push/pop/pushrtn with RA instructions. */
820 if ((INST_HAS_REG_LIST) && ((words[0] >> 7) & 0x1))
822 print_arguments (&currInsn, memaddr, info);
823 return currInsn.size;
826 /* No match found. */
827 info->fprintf_func (info->stream,"%s ",ILLEGAL);