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 (((a) >> (offs)) & ((2ull << (n_bits - 1)) - 1))
36 /* Set Bit Mask - a mask to set all bits starting from offset 'offs'. */
37 #define SBM(offs) ((-1u << (offs)) & 0xffffffff)
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 static 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 static int NUMCINVS
= ((sizeof crx_cinvs
)/(sizeof crx_cinvs
[0]));
84 /* Current opcode table entry we're disassembling. */
85 static const inst
*instruction
;
86 /* Current instruction we're disassembling. */
88 /* The current instruction is read into 3 consecutive words. */
89 static wordU words
[3];
90 /* Contains all words in appropriate order. */
91 static ULONGLONG allWords
;
92 /* Holds the current processed argument number. */
93 static 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). */
98 static int size_changed
;
101 /* Retrieve the number of operands for the current assembled instruction. */
104 get_number_of_operands (void)
108 for (i
= 0; instruction
->operands
[i
].op_type
&& i
< MAX_OPERANDS
; i
++)
114 /* Return the bit size for a given operand. */
117 getbits (operand_type op
)
120 return crx_optab
[op
].bit_size
;
125 /* Return the argument type of a given operand. */
128 getargtype (operand_type op
)
131 return crx_optab
[op
].arg_type
;
136 /* Given the trap index in dispatch table, return its name.
137 This routine is used when disassembling the 'excp' instruction. */
140 gettrapstring (unsigned int trap_index
)
142 const trap_entry
*trap
;
144 for (trap
= crx_traps
; trap
< crx_traps
+ NUMTRAPS
; trap
++)
145 if (trap
->entry
== trap_index
)
151 /* Given a 'cinv' instruction constant operand, return its corresponding string.
152 This routine is used when disassembling the 'cinv' instruction. */
155 getcinvstring (unsigned int num
)
157 const cinv_entry
*cinv
;
159 for (cinv
= crx_cinvs
; cinv
< (crx_cinvs
+ NUMCINVS
); cinv
++)
160 if (cinv
->value
== num
)
166 /* Given a register enum value, retrieve its name. */
171 const reg_entry
* regentry
= &crx_regtab
[r
];
173 if (regentry
->type
!= CRX_R_REGTYPE
)
176 return regentry
->name
;
179 /* Given a coprocessor register enum value, retrieve its name. */
182 getcopregname (copreg r
, reg_type type
)
184 const reg_entry
* regentry
;
186 if (type
== CRX_C_REGTYPE
)
187 regentry
= &crx_copregtab
[r
];
188 else if (type
== CRX_CS_REGTYPE
)
189 regentry
= &crx_copregtab
[r
+(cs0
-c0
)];
193 return regentry
->name
;
197 /* Getting a processor register name. */
200 getprocregname (int reg_index
)
204 for (r
= crx_regtab
; r
< crx_regtab
+ NUMREGS
; r
++)
205 if (r
->image
== reg_index
)
208 return "ILLEGAL REGISTER";
211 /* Get the power of two for a given integer. */
218 for (i
= 0, product
= 1; i
< x
; i
++)
224 /* Transform a register bit mask to a register list. */
227 getregliststring (int mask
, char *string
, enum REG_ARG_TYPE core_cop
)
229 char temp_string
[16];
236 /* A zero mask means HI/LO registers. */
239 if (core_cop
== USER_REG_ARG
)
240 strcat (string
, "ulo,uhi");
242 strcat (string
, "lo,hi");
246 for (i
= 0; i
< 16; i
++)
253 sprintf (temp_string
, "r%d", i
);
256 sprintf (temp_string
, "u%d", i
);
259 sprintf (temp_string
, "c%d", i
);
262 sprintf (temp_string
, "cs%d", i
);
267 strcat (string
, temp_string
);
269 strcat (string
, ",");
275 strcat (string
, "}");
278 /* START and END are relating 'allWords' struct, which is 48 bits size.
281 +---------+---------+---------+---------+
283 +---------+---------+---------+---------+
288 makelongparameter (ULONGLONG val
, int start
, int end
)
292 p
.val
= (dwordU
) EXTRACT(val
, 48 - end
, end
- start
);
293 p
.nbits
= end
- start
;
297 /* Build a mask of the instruction's 'constant' opcode,
298 based on the instruction's printing flags. */
303 unsigned int print_flags
;
306 print_flags
= instruction
->flags
& FMT_CRX
;
325 mask
= SBM(instruction
->match_bits
);
332 /* Search for a matching opcode. Return 1 for success, 0 for failure. */
339 /* The instruction 'constant' opcode doewsn't exceed 32 bits. */
340 unsigned int doubleWord
= (words
[1] + (words
[0] << 16)) & 0xffffffff;
342 /* Start searching from end of instruction table. */
343 instruction
= &crx_instruction
[NUMOPCODES
- 2];
345 /* Loop over instruction table until a full match is found. */
346 while (instruction
>= crx_instruction
)
348 mask
= build_mask ();
349 if ((doubleWord
& mask
) == BIN(instruction
->match
, instruction
->match_bits
))
357 /* Set the proper parameter value for different type of arguments. */
360 make_argument (argument
* a
, int start_bits
)
362 int inst_bit_size
, total_size
;
365 if ((instruction
->size
== 3) && a
->size
>= 16)
374 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ a
->size
),
375 inst_bit_size
- start_bits
);
380 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ a
->size
),
381 inst_bit_size
- start_bits
);
386 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ a
->size
),
387 inst_bit_size
- start_bits
);
389 if ((p
.nbits
== 4) && cst4flag
)
391 if (IS_INSN_TYPE (CMPBR_INS
) && (p
.val
== ESCAPE_16_BIT
))
393 /* A special case, where the value is actually stored
394 in the last 4 bits. */
395 p
= makelongparameter (allWords
, 44, 48);
396 /* The size of the instruction should be incremented. */
402 else if (p
.val
== 13)
406 else if (p
.val
== 10)
408 else if (p
.val
== 11)
419 total_size
= a
->size
+ 10; /* sizeof(rbase + ridx + scl2) = 10. */
420 p
= makelongparameter (allWords
, inst_bit_size
- total_size
,
421 inst_bit_size
- (total_size
- 4));
423 p
= makelongparameter (allWords
, inst_bit_size
- (total_size
- 4),
424 inst_bit_size
- (total_size
- 8));
426 p
= makelongparameter (allWords
, inst_bit_size
- (total_size
- 8),
427 inst_bit_size
- (total_size
- 10));
429 p
= makelongparameter (allWords
, inst_bit_size
- (total_size
- 10),
435 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ 4),
436 inst_bit_size
- start_bits
);
443 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ 4),
444 inst_bit_size
- start_bits
);
446 /* Case for opc4 r dispu rbase. */
447 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ 8),
448 inst_bit_size
- (start_bits
+ 4));
452 /* The 'rbase' start_bits is always relative to a 32-bit data type. */
453 p
= makelongparameter (allWords
, 32 - (start_bits
+ 4),
456 p
= makelongparameter (allWords
, 32 - start_bits
,
459 if ((p
.nbits
== 4) && cst4flag
)
461 if (instruction
->flags
& DISPUW4
)
463 else if (instruction
->flags
& DISPUD4
)
470 p
= makelongparameter (allWords
, inst_bit_size
- (start_bits
+ a
->size
),
471 inst_bit_size
- start_bits
);
479 /* Print a single argument. */
482 print_arg (argument
*a
, bfd_vma memaddr
, struct disassemble_info
*info
)
484 LONGLONG longdisp
, mask
;
490 PTR stream
= info
->stream
;
491 fprintf_ftype func
= info
->fprintf_func
;
496 func (stream
, "%s", getcopregname (a
->cr
, CRX_C_REGTYPE
));
500 func (stream
, "%s", getcopregname (a
->cr
, CRX_CS_REGTYPE
));
504 if (IS_INSN_MNEMONIC ("mtpr") || IS_INSN_MNEMONIC ("mfpr"))
505 func (stream
, "%s", getprocregname (a
->r
));
507 func (stream
, "%s", getregname (a
->r
));
511 if (IS_INSN_MNEMONIC ("excp"))
512 func (stream
, "%s", gettrapstring (a
->constant
));
514 else if (IS_INSN_MNEMONIC ("cinv"))
515 func (stream
, "%s", getcinvstring (a
->constant
));
517 else if (INST_HAS_REG_LIST
)
519 REG_ARG_TYPE reg_arg_type
= IS_INSN_TYPE (COP_REG_INS
) ?
520 COP_ARG
: IS_INSN_TYPE (COPS_REG_INS
) ?
521 COPS_ARG
: (instruction
->flags
& USER_REG
) ?
522 USER_REG_ARG
: REG_ARG
;
524 if ((reg_arg_type
== COP_ARG
) || (reg_arg_type
== COPS_ARG
))
526 /* Check for proper argument number. */
527 if (processing_argument_number
== 2)
529 getregliststring (a
->constant
, string
, reg_arg_type
);
530 func (stream
, "%s", string
);
533 func (stream
, "$0x%lx", a
->constant
& 0xffffffff);
537 getregliststring (a
->constant
, string
, reg_arg_type
);
538 func (stream
, "%s", string
);
542 func (stream
, "$0x%lx", a
->constant
& 0xffffffff);
546 func (stream
, "0x%lx(%s,%s,%d)", a
->constant
& 0xffffffff,
547 getregname (a
->r
), getregname (a
->i_r
), powerof2 (a
->scale
));
551 func (stream
, "(%s)", getregname (a
->r
));
555 func (stream
, "0x%lx(%s)", a
->constant
& 0xffffffff, getregname (a
->r
));
557 if (IS_INSN_TYPE (LD_STOR_INS_INC
))
562 /* Removed the *2 part as because implicit zeros are no more required.
563 Have to fix this as this needs a bit of extension in terms of branchins.
564 Have to add support for cmp and branch instructions. */
565 if (IS_INSN_TYPE (BRANCH_INS
) || IS_INSN_MNEMONIC ("bal")
566 || IS_INSN_TYPE (CMPBR_INS
) || IS_INSN_TYPE (DCR_BRANCH_INS
)
567 || IS_INSN_TYPE (COP_BRANCH_INS
))
570 longdisp
= a
->constant
;
579 mask
= ((LONGLONG
)1 << a
->size
) - 1;
580 if (longdisp
& ((LONGLONG
)1 << a
->size
))
583 longdisp
= ~(longdisp
) + 1;
585 a
->constant
= (unsigned long int) (longdisp
& mask
);
589 "Wrong offset used in branch/bal instruction");
594 /* For branch Neq instruction it is 2*offset + 2. */
595 else if (IS_INSN_TYPE (BRANCH_NEQ_INS
))
596 a
->constant
= 2 * a
->constant
+ 2;
597 else if (IS_INSN_TYPE (LD_STOR_INS_INC
)
598 || IS_INSN_TYPE (LD_STOR_INS
)
599 || IS_INSN_TYPE (STOR_IMM_INS
)
600 || IS_INSN_TYPE (CSTBIT_INS
))
602 op_index
= instruction
->flags
& REVERSE_MATCH
? 0 : 1;
603 if (instruction
->operands
[op_index
].op_type
== abs16
)
604 a
->constant
|= 0xFFFF0000;
606 func (stream
, "%s", "0x");
607 number
= (relative
? memaddr
: 0)
608 + (sign_flag
? -a
->constant
: a
->constant
);
609 (*info
->print_address_func
) (number
, info
);
616 /* Print all the arguments of CURRINSN instruction. */
619 print_arguments (ins
*currentInsn
, bfd_vma memaddr
, struct disassemble_info
*info
)
623 for (i
= 0; i
< currentInsn
->nargs
; i
++)
625 processing_argument_number
= i
;
627 print_arg (¤tInsn
->arg
[i
], memaddr
, info
);
629 if (i
!= currentInsn
->nargs
- 1)
630 info
->fprintf_func (info
->stream
, ", ");
634 /* Build the instruction's arguments. */
637 make_instruction (void)
642 for (i
= 0; i
< currInsn
.nargs
; i
++)
646 memset (&a
, 0, sizeof (a
));
647 a
.type
= getargtype (instruction
->operands
[i
].op_type
);
648 if (instruction
->operands
[i
].op_type
== cst4
649 || instruction
->operands
[i
].op_type
== rbase_dispu4
)
651 a
.size
= getbits (instruction
->operands
[i
].op_type
);
652 shift
= instruction
->operands
[i
].shift
;
654 make_argument (&a
, shift
);
658 /* Calculate instruction size (in bytes). */
659 currInsn
.size
= instruction
->size
+ (size_changed
? 1 : 0);
664 /* Retrieve a single word from a given memory address. */
667 get_word_at_PC (bfd_vma memaddr
, struct disassemble_info
*info
)
673 status
= info
->read_memory_func (memaddr
, buffer
, 2, info
);
676 insn
= (wordU
) bfd_getl16 (buffer
);
681 /* Retrieve multiple words (3) from a given memory address. */
684 get_words_at_PC (bfd_vma memaddr
, struct disassemble_info
*info
)
689 for (i
= 0, mem
= memaddr
; i
< 3; i
++, mem
+= 2)
690 words
[i
] = get_word_at_PC (mem
, info
);
693 ((ULONGLONG
) words
[0] << 32) + ((unsigned long) words
[1] << 16) + words
[2];
696 /* Prints the instruction by calling print_arguments after proper matching. */
699 print_insn_crx (bfd_vma memaddr
, struct disassemble_info
*info
)
701 int is_decoded
; /* Nonzero means instruction has a match. */
703 /* Initialize global variables. */
707 /* Retrieve the encoding from current memory location. */
708 get_words_at_PC (memaddr
, info
);
709 /* Find a matching opcode in table. */
710 is_decoded
= match_opcode ();
711 /* If found, print the instruction's mnemonic and arguments. */
712 if (is_decoded
> 0 && (words
[0] != 0 || words
[1] != 0))
714 info
->fprintf_func (info
->stream
, "%s", instruction
->mnemonic
);
715 if ((currInsn
.nargs
= get_number_of_operands ()) != 0)
716 info
->fprintf_func (info
->stream
, "\t");
718 print_arguments (&currInsn
, memaddr
, info
);
719 return currInsn
.size
;
722 /* No match found. */
723 info
->fprintf_func (info
->stream
,"%s ",ILLEGAL
);