1 /* Disassembler code for CRX.
2 Copyright 2004 Free Software Foundation, Inc.
3 Contributed by Tomer Levi, NSC, Israel.
6 This file is part of the GNU binutils and GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "opcode/crx.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 0xE
31 /* Extract 'n_bits' from 'a' starting from offset 'offs'. */
32 #define EXTRACT(a, offs, n_bits) \
33 (n_bits == 32 ? (((a) >> (offs)) & ~0L) \
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 hold valid 'cinv' instruction options. */
52 /* Cinv printed string. */
54 /* Value corresponding to the string. */
59 /* CRX 'cinv' options. */
60 const cinv_entry crx_cinvs[] =
62 {"[i]", 2}, {"[i,u]", 3}, {"[d]", 4}, {"[d,u]", 5},
63 {"[d,i]", 6}, {"[d,i,u]", 7}, {"[b]", 8},
64 {"[b,i]", 10}, {"[b,i,u]", 11}, {"[b,d]", 12},
65 {"[b,d,u]", 13}, {"[b,d,i]", 14}, {"[b,d,i,u]", 15}
68 /* Enum to distinguish different registers argument types. */
69 typedef enum REG_ARG_TYPE
71 /* General purpose register (r<N>). */
73 /* User register (u<N>). */
75 /* CO-Processor register (c<N>). */
77 /* CO-Processor special register (cs<N>). */
82 /* Number of valid 'cinv' instruction options. */
83 int NUMCINVS = ((sizeof crx_cinvs)/(sizeof crx_cinvs[0]));
84 /* Current opcode table entry we're disassembling. */
85 const inst *instruction;
86 /* Current instruction we're disassembling. */
88 /* The current instruction is read into 3 consecutive words. */
90 /* Contains all words in appropriate order. */
92 /* Holds the current processed argument number. */
93 int processing_argument_number;
94 /* Nonzero means a CST4 instruction. */
96 /* Nonzero means the instruction's original size is
97 incremented (escape sequence is used). */
100 static int get_number_of_operands (void);
101 static argtype getargtype (operand_type);
102 static int getbits (operand_type);
103 static char *getregname (reg);
104 static char *getcopregname (copreg, reg_type);
105 static char * getprocregname (int);
106 static char *gettrapstring (unsigned);
107 static char *getcinvstring (unsigned);
108 static void getregliststring (int, char *, enum REG_ARG_TYPE);
109 static wordU get_word_at_PC (bfd_vma, struct disassemble_info *);
110 static void get_words_at_PC (bfd_vma, struct disassemble_info *);
111 static unsigned long build_mask (void);
112 static int powerof2 (int);
113 static int match_opcode (void);
114 static void make_instruction (void);
115 static void print_arguments (ins *, bfd_vma, struct disassemble_info *);
116 static void print_arg (argument *, bfd_vma, struct disassemble_info *);
118 /* Retrieve the number of operands for the current assembled instruction. */
121 get_number_of_operands (void)
125 for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
131 /* Return the bit size for a given operand. */
134 getbits (operand_type op)
137 return crx_optab[op].bit_size;
142 /* Return the argument type of a given operand. */
145 getargtype (operand_type op)
148 return crx_optab[op].arg_type;
153 /* Given the trap index in dispatch table, return its name.
154 This routine is used when disassembling the 'excp' instruction. */
157 gettrapstring (unsigned int index)
159 const trap_entry *trap;
161 for (trap = crx_traps; trap < crx_traps + NUMTRAPS; trap++)
162 if (trap->entry == index)
168 /* Given a 'cinv' instruction constant operand, return its corresponding string.
169 This routine is used when disassembling the 'cinv' instruction. */
172 getcinvstring (unsigned int num)
174 const cinv_entry *cinv;
176 for (cinv = crx_cinvs; cinv < (crx_cinvs + NUMCINVS); cinv++)
177 if (cinv->value == num)
183 /* Given a register enum value, retrieve its name. */
188 const reg_entry *reg = &crx_regtab[r];
190 if (reg->type != CRX_R_REGTYPE)
196 /* Given a coprocessor register enum value, retrieve its name. */
199 getcopregname (copreg r, reg_type type)
201 const reg_entry *reg;
203 if (type == CRX_C_REGTYPE)
204 reg = &crx_copregtab[r];
205 else if (type == CRX_CS_REGTYPE)
206 reg = &crx_copregtab[r+(cs0-c0)];
214 /* Getting a processor register name. */
217 getprocregname (int index)
221 for (r = crx_regtab; r < crx_regtab + NUMREGS; r++)
222 if (r->image == index)
225 return "ILLEGAL REGISTER";
228 /* Get the power of two for a given integer. */
235 for (i = 0, product = 1; i < x; i++)
241 /* Transform a register bit mask to a register list. */
244 getregliststring (int mask, char *string, enum REG_ARG_TYPE core_cop)
253 /* A zero mask means HI/LO registers. */
256 if (core_cop == USER_REG_ARG)
257 strcat (string, "ulo,uhi");
259 strcat (string, "lo,hi");
263 for (i = 0; i < 16; i++)
270 sprintf (temp_string, "r%d", i);
273 sprintf (temp_string, "u%d", i);
276 sprintf (temp_string, "c%d", i);
279 sprintf (temp_string, "cs%d", i);
284 strcat (string, temp_string);
286 strcat (string, ",");
292 strcat (string, "}");
295 /* START and END are relating 'allWords' struct, which is 48 bits size.
298 +---------+---------+---------+---------+
300 +---------+---------+---------+---------+
305 makelongparameter (ULONGLONG val, int start, int end)
309 p.val = (dwordU) EXTRACT(val, 48 - end, end - start);
310 p.nbits = end - start;
314 /* Build a mask of the instruction's 'constant' opcode,
315 based on the instruction's printing flags. */
320 unsigned int print_flags;
323 print_flags = instruction->flags & FMT_CRX;
342 mask = SBM(instruction->match_bits);
349 /* Search for a matching opcode. Return 1 for success, 0 for failure. */
356 /* The instruction 'constant' opcode doewsn't exceed 32 bits. */
357 unsigned long doubleWord = words[1] + (words[0] << 16);
359 /* Start searching from end of instruction table. */
360 instruction = &crx_instruction[NUMOPCODES - 2];
362 /* Loop over instruction table until a full match is found. */
363 while (instruction >= crx_instruction)
365 mask = build_mask ();
366 if ((doubleWord & mask) == BIN(instruction->match, instruction->match_bits))
374 /* Set the proper parameter value for different type of arguments. */
377 make_argument (argument * a, int start_bits)
379 int inst_bit_size, total_size;
382 if ((instruction->size == 3) && a->size >= 16)
391 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
392 inst_bit_size - start_bits);
397 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
398 inst_bit_size - start_bits);
403 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
404 inst_bit_size - start_bits);
406 if ((p.nbits == 4) && cst4flag)
408 if (IS_INSN_TYPE (CMPBR_INS) && (p.val == ESCAPE_16_BIT))
410 /* A special case, where the value is actually stored
411 in the last 4 bits. */
412 p = makelongparameter (allWords, 44, 48);
413 /* The size of the instruction should be incremented. */
419 else if (p.val == 13)
423 else if (p.val == 10)
425 else if (p.val == 11)
436 total_size = a->size + 10; /* sizeof(rbase + ridx + scl2) = 10. */
437 p = makelongparameter (allWords, inst_bit_size - total_size,
438 inst_bit_size - (total_size - 4));
440 p = makelongparameter (allWords, inst_bit_size - (total_size - 4),
441 inst_bit_size - (total_size - 8));
443 p = makelongparameter (allWords, inst_bit_size - (total_size - 8),
444 inst_bit_size - (total_size - 10));
446 p = makelongparameter (allWords, inst_bit_size - (total_size - 10),
452 p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
453 inst_bit_size - start_bits);
460 p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
461 inst_bit_size - start_bits);
463 /* Case for opc4 r dispu rbase. */
464 p = makelongparameter (allWords, inst_bit_size - (start_bits + 8),
465 inst_bit_size - (start_bits + 4));
469 /* The 'rbase' start_bits is always relative to a 32-bit data type. */
470 p = makelongparameter (allWords, 32 - (start_bits + 4),
473 p = makelongparameter (allWords, 32 - start_bits,
476 if ((p.nbits == 4) && cst4flag)
478 if (instruction->flags & DISPUW4)
480 else if (instruction->flags & DISPUD4)
487 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
488 inst_bit_size - start_bits);
496 /* Print a single argument. */
499 print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
501 LONGLONG longdisp, mask;
507 PTR stream = info->stream;
508 fprintf_ftype func = info->fprintf_func;
513 func (stream, "%s", getcopregname (a->cr, CRX_C_REGTYPE));
517 func (stream, "%s", getcopregname (a->cr, CRX_CS_REGTYPE));
521 if (IS_INSN_MNEMONIC ("mtpr") || IS_INSN_MNEMONIC ("mfpr"))
522 func (stream, "%s", getprocregname (a->r));
524 func (stream, "%s", getregname (a->r));
528 if (IS_INSN_MNEMONIC ("excp"))
529 func (stream, "%s", gettrapstring (a->constant));
531 else if (IS_INSN_MNEMONIC ("cinv"))
532 func (stream, "%s", getcinvstring (a->constant));
534 else if (INST_HAS_REG_LIST)
536 REG_ARG_TYPE reg_arg_type = IS_INSN_TYPE (COP_REG_INS) ?
537 COP_ARG : IS_INSN_TYPE (COPS_REG_INS) ?
538 COPS_ARG : (instruction->flags & USER_REG) ?
539 USER_REG_ARG : REG_ARG;
541 if ((reg_arg_type == COP_ARG) || (reg_arg_type == COPS_ARG))
543 /* Check for proper argument number. */
544 if (processing_argument_number == 2)
546 getregliststring (a->constant, string, reg_arg_type);
547 func (stream, "%s", string);
550 func (stream, "$0x%x", a->constant);
554 getregliststring (a->constant, string, reg_arg_type);
555 func (stream, "%s", string);
559 func (stream, "$0x%x", a->constant);
563 func (stream, "0x%x(%s,%s,%d)", a->constant, getregname (a->r),
564 getregname (a->i_r), powerof2 (a->scale));
568 func (stream, "(%s)", getregname (a->r));
572 func (stream, "0x%x(%s)", a->constant, getregname (a->r));
574 if (IS_INSN_TYPE (LD_STOR_INS_INC))
579 /* Removed the *2 part as because implicit zeros are no more required.
580 Have to fix this as this needs a bit of extension in terms of branchins.
581 Have to add support for cmp and branch instructions. */
582 if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal")
583 || IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (DCR_BRANCH_INS)
584 || IS_INSN_TYPE (COP_BRANCH_INS))
587 longdisp = a->constant;
596 mask = ((LONGLONG)1 << a->size) - 1;
597 if (longdisp & ((LONGLONG)1 << a->size))
600 longdisp = ~(longdisp) + 1;
602 a->constant = (unsigned long int) (longdisp & mask);
606 "Wrong offset used in branch/bal instruction");
611 /* For branch Neq instruction it is 2*offset + 2. */
612 else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
613 a->constant = 2 * a->constant + 2;
614 else if (IS_INSN_TYPE (LD_STOR_INS_INC)
615 || IS_INSN_TYPE (LD_STOR_INS)
616 || IS_INSN_TYPE (STOR_IMM_INS)
617 || IS_INSN_TYPE (CSTBIT_INS))
619 op_index = instruction->flags & REVERSE_MATCH ? 0 : 1;
620 if (instruction->operands[op_index].op_type == abs16)
621 a->constant |= 0xFFFF0000;
623 func (stream, "%s", "0x");
624 number = (relative ? memaddr : 0)
625 + (sign_flag ? -a->constant : a->constant);
626 (*info->print_address_func) (number, info);
633 /* Print all the arguments of CURRINSN instruction. */
636 print_arguments (ins *currInsn, bfd_vma memaddr, struct disassemble_info *info)
640 for (i = 0; i < currInsn->nargs; i++)
642 processing_argument_number = i;
644 print_arg (&currInsn->arg[i], memaddr, info);
646 if (i != currInsn->nargs - 1)
647 info->fprintf_func (info->stream, ", ");
651 /* Build the instruction's arguments. */
654 make_instruction (void)
659 for (i = 0; i < currInsn.nargs; i++)
663 a.type = getargtype (instruction->operands[i].op_type);
664 if (instruction->operands[i].op_type == cst4
665 || instruction->operands[i].op_type == rbase_dispu4)
667 a.size = getbits (instruction->operands[i].op_type);
668 shift = instruction->operands[i].shift;
670 make_argument (&a, shift);
674 /* Calculate instruction size (in bytes). */
675 currInsn.size = instruction->size + (size_changed ? 1 : 0);
680 /* Retrieve a single word from a given memory address. */
683 get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
689 status = info->read_memory_func (memaddr, buffer, 2, info);
692 insn = (wordU) bfd_getl16 (buffer);
697 /* Retrieve multiple words (3) from a given memory address. */
700 get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
705 for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
706 words[i] = get_word_at_PC (mem, info);
709 ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
712 /* Prints the instruction by calling print_arguments after proper matching. */
715 print_insn_crx (memaddr, info)
717 struct disassemble_info *info;
719 int is_decoded; /* Nonzero means instruction has a match. */
721 /* Initialize global variables. */
725 /* Retrieve the encoding from current memory location. */
726 get_words_at_PC (memaddr, info);
727 /* Find a matching opcode in table. */
728 is_decoded = match_opcode ();
729 /* If found, print the instruction's mnemonic and arguments. */
730 if (is_decoded > 0 && (words[0] << 16 || words[1]) != 0)
732 info->fprintf_func (info->stream, "%s", instruction->mnemonic);
733 if ((currInsn.nargs = get_number_of_operands ()) != 0)
734 info->fprintf_func (info->stream, "\t");
736 print_arguments (&currInsn, memaddr, info);
737 return currInsn.size;
740 /* No match found. */
741 info->fprintf_func (info->stream,"%s ",ILLEGAL);