1 /* Disassemble D30V instructions.
2 Copyright 1997, 1998, 2000, 2001, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
17 MA 02110-1301, USA. */
21 #include "opcode/d30v.h"
25 #define PC_MASK 0xFFFFFFFF
27 /* Return 0 if lookup fails,
28 1 if found and only one form,
29 2 if found and there are short and long forms. */
32 lookup_opcode (struct d30v_insn
*insn
, long num
, int is_long
)
35 struct d30v_format
*f
;
36 struct d30v_opcode
*op
= (struct d30v_opcode
*) d30v_opcode_table
;
37 int op1
= (num
>> 25) & 0x7;
38 int op2
= (num
>> 20) & 0x1f;
39 int mod
= (num
>> 18) & 0x3;
41 /* Find the opcode. */
44 if ((op
->op1
== op1
) && (op
->op2
== op2
))
53 while (op
->op1
== op1
&& op
->op2
== op2
)
55 /* Scan through all the formats for the opcode. */
56 index
= op
->format
[i
++];
59 f
= (struct d30v_format
*) &d30v_format_table
[index
];
60 while (f
->form
== index
)
62 if ((!is_long
|| f
->form
>= LONG
) && (f
->modifier
== mod
))
72 while ((index
= op
->format
[i
++]) != 0);
78 if (insn
->form
== NULL
)
82 insn
->ecc
= (num
>> 28) & 0x7;
90 extract_value (long long num
, struct d30v_operand
*oper
, int is_long
)
93 int shift
= 12 - oper
->position
;
94 int mask
= (0xFFFFFFFF >> (32 - oper
->bits
));
99 /* Piece together 32-bit constant. */
100 val
= ((num
& 0x3FFFF)
101 | ((num
& 0xFF00000) >> 2)
102 | ((num
& 0x3F00000000LL
) >> 6));
104 val
= (num
>> (32 + shift
)) & mask
;
107 val
= (num
>> shift
) & mask
;
109 if (oper
->flags
& OPERAND_SHIFT
)
116 print_insn (struct disassemble_info
*info
,
119 struct d30v_insn
*insn
,
123 int val
, opnum
, need_comma
= 0;
124 struct d30v_operand
*oper
;
125 int i
, match
, opind
= 0, need_paren
= 0, found_control
= 0;
127 (*info
->fprintf_func
) (info
->stream
, "%s", insn
->op
->name
);
129 /* Check for CMP or CMPU. */
130 if (d30v_operand_table
[insn
->form
->operands
[0]].flags
& OPERAND_NAME
)
135 (struct d30v_operand
*) &d30v_operand_table
[insn
->form
->operands
[0]],
137 (*info
->fprintf_func
) (info
->stream
, "%s", d30v_cc_names
[val
]);
140 /* Add in ".s" or ".l". */
144 (*info
->fprintf_func
) (info
->stream
, ".l");
146 (*info
->fprintf_func
) (info
->stream
, ".s");
150 (*info
->fprintf_func
) (info
->stream
, "/%s", d30v_ecc_names
[insn
->ecc
]);
152 (*info
->fprintf_func
) (info
->stream
, "\t");
154 while ((opnum
= insn
->form
->operands
[opind
++]) != 0)
158 oper
= (struct d30v_operand
*) &d30v_operand_table
[opnum
];
160 if (oper
->flags
& OPERAND_SHIFT
)
164 && oper
->flags
!= OPERAND_PLUS
165 && oper
->flags
!= OPERAND_MINUS
)
168 (*info
->fprintf_func
) (info
->stream
, ", ");
171 if (oper
->flags
== OPERAND_ATMINUS
)
173 (*info
->fprintf_func
) (info
->stream
, "@-");
176 if (oper
->flags
== OPERAND_MINUS
)
178 (*info
->fprintf_func
) (info
->stream
, "-");
181 if (oper
->flags
== OPERAND_PLUS
)
183 (*info
->fprintf_func
) (info
->stream
, "+");
186 if (oper
->flags
== OPERAND_ATSIGN
)
188 (*info
->fprintf_func
) (info
->stream
, "@");
191 if (oper
->flags
== OPERAND_ATPAR
)
193 (*info
->fprintf_func
) (info
->stream
, "@(");
198 if (oper
->flags
== OPERAND_SPECIAL
)
201 val
= extract_value (num
, oper
, is_long
);
203 if (oper
->flags
& OPERAND_REG
)
206 if (oper
->flags
& OPERAND_CONTROL
)
208 struct d30v_operand
*oper3
=
209 (struct d30v_operand
*) &d30v_operand_table
[insn
->form
->operands
[2]];
210 int id
= extract_value (num
, oper3
, is_long
);
216 val
|= OPERAND_CONTROL
;
220 val
= OPERAND_CONTROL
+ MAX_CONTROL_REG
+ id
;
226 fprintf (stderr
, "illegal id (%d)\n", id
);
229 else if (oper
->flags
& OPERAND_ACC
)
231 else if (oper
->flags
& OPERAND_FLAG
)
233 for (i
= 0; i
< reg_name_cnt (); i
++)
235 if (val
== pre_defined_registers
[i
].value
)
237 if (pre_defined_registers
[i
].pname
)
238 (*info
->fprintf_func
)
239 (info
->stream
, "%s", pre_defined_registers
[i
].pname
);
241 (*info
->fprintf_func
)
242 (info
->stream
, "%s", pre_defined_registers
[i
].name
);
249 /* This would only get executed if a register was not in
250 the register table. */
251 (*info
->fprintf_func
)
252 (info
->stream
, _("<unknown register %d>"), val
& 0x3F);
255 /* repeati has a relocation, but its first argument is a plain
256 immediate. OTOH instructions like djsri have a pc-relative
257 delay target, but an absolute jump target. Therefore, a test
258 of insn->op->reloc_flag is not specific enough; we must test
259 if the actual operand we are handling now is pc-relative. */
260 else if (oper
->flags
& OPERAND_PCREL
)
264 /* IMM6S3 is unsigned. */
265 if (oper
->flags
& OPERAND_SIGNED
|| bits
== 32)
268 max
= (1 << (bits
- 1));
274 val
= -val
& ((1 << bits
) - 1);
280 (*info
->fprintf_func
) (info
->stream
, "-%x\t(", val
);
281 (*info
->print_address_func
) ((memaddr
- val
) & PC_MASK
, info
);
282 (*info
->fprintf_func
) (info
->stream
, ")");
286 (*info
->fprintf_func
) (info
->stream
, "%x\t(", val
);
287 (*info
->print_address_func
) ((memaddr
+ val
) & PC_MASK
, info
);
288 (*info
->fprintf_func
) (info
->stream
, ")");
291 else if (insn
->op
->reloc_flag
== RELOC_ABS
)
293 (*info
->print_address_func
) (val
, info
);
297 if (oper
->flags
& OPERAND_SIGNED
)
299 int max
= (1 << (bits
- 1));
305 val
&= ((1 << bits
) - 1);
306 (*info
->fprintf_func
) (info
->stream
, "-");
309 (*info
->fprintf_func
) (info
->stream
, "0x%x", val
);
311 /* If there is another operand, then write a comma and space. */
312 if (insn
->form
->operands
[opind
] && !(found_control
&& opind
== 2))
316 (*info
->fprintf_func
) (info
->stream
, ")");
320 print_insn_d30v (bfd_vma memaddr
, struct disassemble_info
*info
)
324 unsigned long in1
, in2
;
325 struct d30v_insn insn
;
330 info
->bytes_per_line
= 8;
331 info
->bytes_per_chunk
= 4;
332 info
->display_endian
= BFD_ENDIAN_BIG
;
334 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
337 (*info
->memory_error_func
) (status
, memaddr
, info
);
340 in1
= bfd_getb32 (buffer
);
342 status
= (*info
->read_memory_func
) (memaddr
+ 4, buffer
, 4, info
);
345 info
->bytes_per_line
= 8;
346 if (!(result
= lookup_opcode (&insn
, in1
, 0)))
347 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%lx", in1
);
349 print_insn (info
, memaddr
, (long long) in1
, &insn
, 0, result
);
352 in2
= bfd_getb32 (buffer
);
354 if (in1
& in2
& FM01
)
356 /* LONG instruction. */
357 if (!(result
= lookup_opcode (&insn
, in1
, 1)))
359 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%lx,0x%lx", in1
, in2
);
362 num
= (long long) in1
<< 32 | in2
;
363 print_insn (info
, memaddr
, num
, &insn
, 1, result
);
368 if (!(result
= lookup_opcode (&insn
, in1
, 0)))
369 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%lx", in1
);
371 print_insn (info
, memaddr
, num
, &insn
, 0, result
);
373 switch (((in1
>> 31) << 1) | (in2
>> 31))
376 (*info
->fprintf_func
) (info
->stream
, "\t||\t");
379 (*info
->fprintf_func
) (info
->stream
, "\t->\t");
382 (*info
->fprintf_func
) (info
->stream
, "\t<-\t");
389 if (!(result
= lookup_opcode (&insn
, in2
, 0)))
390 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%lx", in2
);
392 print_insn (info
, memaddr
, num
, &insn
, 0, result
);