1 /* Disassembler code for CRX.
2 Copyright (C) 2004-2019 Free Software Foundation, Inc.
3 Contributed by Tomer Levi, NSC, Israel.
6 This file is part of the GNU opcodes library.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 #include "disassemble.h"
25 #include "opcode/crx.h"
27 /* String to print when opcode was not matched. */
28 #define ILLEGAL "illegal"
29 /* Escape to 16-bit immediate. */
30 #define ESCAPE_16_BIT 0xE
32 /* Extract 'n_bits' from 'a' starting from offset 'offs'. */
33 #define EXTRACT(a, offs, n_bits) \
34 (n_bits == 32 ? (((a) >> (offs)) & 0xffffffffL) \
35 : (((a) >> (offs)) & ((1 << (n_bits)) -1)))
37 /* Set Bit Mask - a mask to set all bits starting from offset 'offs'. */
38 #define SBM(offs) ((((1 << (32 - offs)) -1) << (offs)))
40 typedef unsigned long dwordU;
41 typedef unsigned short wordU;
49 /* Structure to hold valid 'cinv' instruction options. */
53 /* Cinv printed string. */
55 /* Value corresponding to the string. */
60 /* CRX 'cinv' options. */
61 static const cinv_entry crx_cinvs[] =
63 {"[i]", 2}, {"[i,u]", 3}, {"[d]", 4}, {"[d,u]", 5},
64 {"[d,i]", 6}, {"[d,i,u]", 7}, {"[b]", 8},
65 {"[b,i]", 10}, {"[b,i,u]", 11}, {"[b,d]", 12},
66 {"[b,d,u]", 13}, {"[b,d,i]", 14}, {"[b,d,i,u]", 15}
69 /* Enum to distinguish different registers argument types. */
70 typedef enum REG_ARG_TYPE
72 /* General purpose register (r<N>). */
74 /* User register (u<N>). */
76 /* CO-Processor register (c<N>). */
78 /* CO-Processor special register (cs<N>). */
83 /* Number of valid 'cinv' instruction options. */
84 static int NUMCINVS = ((sizeof crx_cinvs)/(sizeof crx_cinvs[0]));
85 /* Current opcode table entry we're disassembling. */
86 static const inst *instruction;
87 /* Current instruction we're disassembling. */
89 /* The current instruction is read into 3 consecutive words. */
90 static wordU words[3];
91 /* Contains all words in appropriate order. */
92 static ULONGLONG allWords;
93 /* Holds the current processed argument number. */
94 static int processing_argument_number;
95 /* Nonzero means a CST4 instruction. */
97 /* Nonzero means the instruction's original size is
98 incremented (escape sequence is used). */
99 static int size_changed;
101 static int get_number_of_operands (void);
102 static argtype getargtype (operand_type);
103 static int getbits (operand_type);
104 static char *getregname (reg);
105 static char *getcopregname (copreg, reg_type);
106 static char * getprocregname (int);
107 static char *gettrapstring (unsigned);
108 static char *getcinvstring (unsigned);
109 static void getregliststring (int, char *, enum REG_ARG_TYPE);
110 static wordU get_word_at_PC (bfd_vma, struct disassemble_info *);
111 static void get_words_at_PC (bfd_vma, struct disassemble_info *);
112 static unsigned long build_mask (void);
113 static int powerof2 (int);
114 static int match_opcode (void);
115 static void make_instruction (void);
116 static void print_arguments (ins *, bfd_vma, struct disassemble_info *);
117 static void print_arg (argument *, bfd_vma, struct disassemble_info *);
119 /* Retrieve the number of operands for the current assembled instruction. */
122 get_number_of_operands (void)
126 for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
132 /* Return the bit size for a given operand. */
135 getbits (operand_type op)
138 return crx_optab[op].bit_size;
143 /* Return the argument type of a given operand. */
146 getargtype (operand_type op)
149 return crx_optab[op].arg_type;
154 /* Given the trap index in dispatch table, return its name.
155 This routine is used when disassembling the 'excp' instruction. */
158 gettrapstring (unsigned int trap_index)
160 const trap_entry *trap;
162 for (trap = crx_traps; trap < crx_traps + NUMTRAPS; trap++)
163 if (trap->entry == trap_index)
169 /* Given a 'cinv' instruction constant operand, return its corresponding string.
170 This routine is used when disassembling the 'cinv' instruction. */
173 getcinvstring (unsigned int num)
175 const cinv_entry *cinv;
177 for (cinv = crx_cinvs; cinv < (crx_cinvs + NUMCINVS); cinv++)
178 if (cinv->value == num)
184 /* Given a register enum value, retrieve its name. */
189 const reg_entry * regentry = &crx_regtab[r];
191 if (regentry->type != CRX_R_REGTYPE)
194 return regentry->name;
197 /* Given a coprocessor register enum value, retrieve its name. */
200 getcopregname (copreg r, reg_type type)
202 const reg_entry * regentry;
204 if (type == CRX_C_REGTYPE)
205 regentry = &crx_copregtab[r];
206 else if (type == CRX_CS_REGTYPE)
207 regentry = &crx_copregtab[r+(cs0-c0)];
211 return regentry->name;
215 /* Getting a processor register name. */
218 getprocregname (int reg_index)
222 for (r = crx_regtab; r < crx_regtab + NUMREGS; r++)
223 if (r->image == reg_index)
226 return "ILLEGAL REGISTER";
229 /* Get the power of two for a given integer. */
236 for (i = 0, product = 1; i < x; i++)
242 /* Transform a register bit mask to a register list. */
245 getregliststring (int mask, char *string, enum REG_ARG_TYPE core_cop)
247 char temp_string[16];
254 /* A zero mask means HI/LO registers. */
257 if (core_cop == USER_REG_ARG)
258 strcat (string, "ulo,uhi");
260 strcat (string, "lo,hi");
264 for (i = 0; i < 16; i++)
271 sprintf (temp_string, "r%d", i);
274 sprintf (temp_string, "u%d", i);
277 sprintf (temp_string, "c%d", i);
280 sprintf (temp_string, "cs%d", i);
285 strcat (string, temp_string);
287 strcat (string, ",");
293 strcat (string, "}");
296 /* START and END are relating 'allWords' struct, which is 48 bits size.
299 +---------+---------+---------+---------+
301 +---------+---------+---------+---------+
306 makelongparameter (ULONGLONG val, int start, int end)
310 p.val = (dwordU) EXTRACT(val, 48 - end, end - start);
311 p.nbits = end - start;
315 /* Build a mask of the instruction's 'constant' opcode,
316 based on the instruction's printing flags. */
321 unsigned int print_flags;
324 print_flags = instruction->flags & FMT_CRX;
343 mask = SBM(instruction->match_bits);
350 /* Search for a matching opcode. Return 1 for success, 0 for failure. */
357 /* The instruction 'constant' opcode doewsn't exceed 32 bits. */
358 unsigned long doubleWord = (words[1] + (words[0] << 16)) & 0xffffffff;
360 /* Start searching from end of instruction table. */
361 instruction = &crx_instruction[NUMOPCODES - 2];
363 /* Loop over instruction table until a full match is found. */
364 while (instruction >= crx_instruction)
366 mask = build_mask ();
367 if ((doubleWord & mask) == BIN(instruction->match, instruction->match_bits))
375 /* Set the proper parameter value for different type of arguments. */
378 make_argument (argument * a, int start_bits)
380 int inst_bit_size, total_size;
383 if ((instruction->size == 3) && a->size >= 16)
392 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
393 inst_bit_size - start_bits);
398 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
399 inst_bit_size - start_bits);
404 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
405 inst_bit_size - start_bits);
407 if ((p.nbits == 4) && cst4flag)
409 if (IS_INSN_TYPE (CMPBR_INS) && (p.val == ESCAPE_16_BIT))
411 /* A special case, where the value is actually stored
412 in the last 4 bits. */
413 p = makelongparameter (allWords, 44, 48);
414 /* The size of the instruction should be incremented. */
420 else if (p.val == 13)
424 else if (p.val == 10)
426 else if (p.val == 11)
437 total_size = a->size + 10; /* sizeof(rbase + ridx + scl2) = 10. */
438 p = makelongparameter (allWords, inst_bit_size - total_size,
439 inst_bit_size - (total_size - 4));
441 p = makelongparameter (allWords, inst_bit_size - (total_size - 4),
442 inst_bit_size - (total_size - 8));
444 p = makelongparameter (allWords, inst_bit_size - (total_size - 8),
445 inst_bit_size - (total_size - 10));
447 p = makelongparameter (allWords, inst_bit_size - (total_size - 10),
453 p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
454 inst_bit_size - start_bits);
461 p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
462 inst_bit_size - start_bits);
464 /* Case for opc4 r dispu rbase. */
465 p = makelongparameter (allWords, inst_bit_size - (start_bits + 8),
466 inst_bit_size - (start_bits + 4));
470 /* The 'rbase' start_bits is always relative to a 32-bit data type. */
471 p = makelongparameter (allWords, 32 - (start_bits + 4),
474 p = makelongparameter (allWords, 32 - start_bits,
477 if ((p.nbits == 4) && cst4flag)
479 if (instruction->flags & DISPUW4)
481 else if (instruction->flags & DISPUD4)
488 p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
489 inst_bit_size - start_bits);
497 /* Print a single argument. */
500 print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
502 LONGLONG longdisp, mask;
508 PTR stream = info->stream;
509 fprintf_ftype func = info->fprintf_func;
514 func (stream, "%s", getcopregname (a->cr, CRX_C_REGTYPE));
518 func (stream, "%s", getcopregname (a->cr, CRX_CS_REGTYPE));
522 if (IS_INSN_MNEMONIC ("mtpr") || IS_INSN_MNEMONIC ("mfpr"))
523 func (stream, "%s", getprocregname (a->r));
525 func (stream, "%s", getregname (a->r));
529 if (IS_INSN_MNEMONIC ("excp"))
530 func (stream, "%s", gettrapstring (a->constant));
532 else if (IS_INSN_MNEMONIC ("cinv"))
533 func (stream, "%s", getcinvstring (a->constant));
535 else if (INST_HAS_REG_LIST)
537 REG_ARG_TYPE reg_arg_type = IS_INSN_TYPE (COP_REG_INS) ?
538 COP_ARG : IS_INSN_TYPE (COPS_REG_INS) ?
539 COPS_ARG : (instruction->flags & USER_REG) ?
540 USER_REG_ARG : REG_ARG;
542 if ((reg_arg_type == COP_ARG) || (reg_arg_type == COPS_ARG))
544 /* Check for proper argument number. */
545 if (processing_argument_number == 2)
547 getregliststring (a->constant, string, reg_arg_type);
548 func (stream, "%s", string);
551 func (stream, "$0x%lx", a->constant & 0xffffffff);
555 getregliststring (a->constant, string, reg_arg_type);
556 func (stream, "%s", string);
560 func (stream, "$0x%lx", a->constant & 0xffffffff);
564 func (stream, "0x%lx(%s,%s,%d)", a->constant & 0xffffffff,
565 getregname (a->r), getregname (a->i_r), powerof2 (a->scale));
569 func (stream, "(%s)", getregname (a->r));
573 func (stream, "0x%lx(%s)", a->constant & 0xffffffff, getregname (a->r));
575 if (IS_INSN_TYPE (LD_STOR_INS_INC))
580 /* Removed the *2 part as because implicit zeros are no more required.
581 Have to fix this as this needs a bit of extension in terms of branchins.
582 Have to add support for cmp and branch instructions. */
583 if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal")
584 || IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (DCR_BRANCH_INS)
585 || IS_INSN_TYPE (COP_BRANCH_INS))
588 longdisp = a->constant;
597 mask = ((LONGLONG)1 << a->size) - 1;
598 if (longdisp & ((LONGLONG)1 << a->size))
601 longdisp = ~(longdisp) + 1;
603 a->constant = (unsigned long int) (longdisp & mask);
607 "Wrong offset used in branch/bal instruction");
612 /* For branch Neq instruction it is 2*offset + 2. */
613 else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
614 a->constant = 2 * a->constant + 2;
615 else if (IS_INSN_TYPE (LD_STOR_INS_INC)
616 || IS_INSN_TYPE (LD_STOR_INS)
617 || IS_INSN_TYPE (STOR_IMM_INS)
618 || IS_INSN_TYPE (CSTBIT_INS))
620 op_index = instruction->flags & REVERSE_MATCH ? 0 : 1;
621 if (instruction->operands[op_index].op_type == abs16)
622 a->constant |= 0xFFFF0000;
624 func (stream, "%s", "0x");
625 number = (relative ? memaddr : 0)
626 + (sign_flag ? -a->constant : a->constant);
627 (*info->print_address_func) (number, info);
634 /* Print all the arguments of CURRINSN instruction. */
637 print_arguments (ins *currentInsn, bfd_vma memaddr, struct disassemble_info *info)
641 for (i = 0; i < currentInsn->nargs; i++)
643 processing_argument_number = i;
645 print_arg (¤tInsn->arg[i], memaddr, info);
647 if (i != currentInsn->nargs - 1)
648 info->fprintf_func (info->stream, ", ");
652 /* Build the instruction's arguments. */
655 make_instruction (void)
660 for (i = 0; i < currInsn.nargs; i++)
664 memset (&a, 0, sizeof (a));
665 a.type = getargtype (instruction->operands[i].op_type);
666 if (instruction->operands[i].op_type == cst4
667 || instruction->operands[i].op_type == rbase_dispu4)
669 a.size = getbits (instruction->operands[i].op_type);
670 shift = instruction->operands[i].shift;
672 make_argument (&a, shift);
676 /* Calculate instruction size (in bytes). */
677 currInsn.size = instruction->size + (size_changed ? 1 : 0);
682 /* Retrieve a single word from a given memory address. */
685 get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
691 status = info->read_memory_func (memaddr, buffer, 2, info);
694 insn = (wordU) bfd_getl16 (buffer);
699 /* Retrieve multiple words (3) from a given memory address. */
702 get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
707 for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
708 words[i] = get_word_at_PC (mem, info);
711 ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
714 /* Prints the instruction by calling print_arguments after proper matching. */
717 print_insn_crx (bfd_vma memaddr, 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] != 0 || 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);