1 /* Instruction printing code for the DLX Microprocessor
2 Copyright 2002, 2005 Free Software Foundation, Inc.
3 Contributed by Kuang Hwa Lin. Written by Kuang Hwa Lin, 03/2002.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 MA 02110-1301, USA. */
22 #include "opcode/dlx.h"
34 #define OPC(x) ((x >> 26) & 0x3F)
35 #define FUNC(x) (x & 0x7FF)
37 unsigned char opc
, rs1
, rs2
, rd
;
38 unsigned long imm26
, imm16
, func
, current_insn_addr
;
40 /* Print one instruction from MEMADDR on INFO->STREAM.
41 Return the size of the instruction (always 4 on dlx). */
44 dlx_get_opcode (unsigned long opcode
)
46 return (unsigned char) ((opcode
>> 26) & 0x3F);
50 dlx_get_rs1 (unsigned long opcode
)
52 return (unsigned char) ((opcode
>> 21) & 0x1F);
56 dlx_get_rs2 (unsigned long opcode
)
58 return (unsigned char) ((opcode
>> 16) & 0x1F);
62 dlx_get_rdR (unsigned long opcode
)
64 return (unsigned char) ((opcode
>> 11) & 0x1F);
68 dlx_get_func (unsigned long opcode
)
70 return (unsigned char) (opcode
& 0x7FF);
74 dlx_get_imm16 (unsigned long opcode
)
76 return (unsigned long) (opcode
& 0xFFFF);
80 dlx_get_imm26 (unsigned long opcode
)
82 return (unsigned long) (opcode
& 0x03FFFFFF);
85 /* Fill the opcode to the max length. */
88 operand_deliminator (struct disassemble_info
*info
, char *ptr
)
90 int difft
= 8 - (int) strlen (ptr
);
94 (*info
->fprintf_func
) (info
->stream
, "%c", ' ');
99 /* Process the R-type opcode. */
102 dlx_r_type (struct disassemble_info
*info
)
104 unsigned char r_opc
[] = { OPC(ALUOP
) }; /* Fix ME */
105 int r_opc_num
= (sizeof r_opc
) / (sizeof (char));
113 { NOPF
, "nop" }, /* NOP */
114 { ADDF
, "add" }, /* Add */
115 { ADDUF
, "addu" }, /* Add Unsigned */
116 { SUBF
, "sub" }, /* SUB */
117 { SUBUF
, "subu" }, /* Sub Unsigned */
118 { MULTF
, "mult" }, /* MULTIPLY */
119 { MULTUF
, "multu" }, /* MULTIPLY Unsigned */
120 { DIVF
, "div" }, /* DIVIDE */
121 { DIVUF
, "divu" }, /* DIVIDE Unsigned */
122 { ANDF
, "and" }, /* AND */
123 { ORF
, "or" }, /* OR */
124 { XORF
, "xor" }, /* Exclusive OR */
125 { SLLF
, "sll" }, /* SHIFT LEFT LOGICAL */
126 { SRAF
, "sra" }, /* SHIFT RIGHT ARITHMETIC */
127 { SRLF
, "srl" }, /* SHIFT RIGHT LOGICAL */
128 { SEQF
, "seq" }, /* Set if equal */
129 { SNEF
, "sne" }, /* Set if not equal */
130 { SLTF
, "slt" }, /* Set if less */
131 { SGTF
, "sgt" }, /* Set if greater */
132 { SLEF
, "sle" }, /* Set if less or equal */
133 { SGEF
, "sge" }, /* Set if greater or equal */
134 { SEQUF
, "sequ" }, /* Set if equal */
135 { SNEUF
, "sneu" }, /* Set if not equal */
136 { SLTUF
, "sltu" }, /* Set if less */
137 { SGTUF
, "sgtu" }, /* Set if greater */
138 { SLEUF
, "sleu" }, /* Set if less or equal */
139 { SGEUF
, "sgeu" }, /* Set if greater or equal */
140 { MVTSF
, "mvts" }, /* Move to special register */
141 { MVFSF
, "mvfs" }, /* Move from special register */
142 { BSWAPF
, "bswap" }, /* Byte swap ?? */
143 { LUTF
, "lut" } /* ????????? ?? */
145 int dlx_r_opcode_num
= (sizeof dlx_r_opcode
) / (sizeof dlx_r_opcode
[0]);
148 for (idx
= 0; idx
< r_opc_num
; idx
++)
150 if (r_opc
[idx
] != opc
)
156 if (idx
== r_opc_num
)
159 for (idx
= 0 ; idx
< dlx_r_opcode_num
; idx
++)
160 if (dlx_r_opcode
[idx
].func
== func
)
162 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_r_opcode
[idx
].name
);
166 /* This is not a nop. */
167 operand_deliminator (info
, dlx_r_opcode
[idx
].name
);
168 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int)rd
);
169 (*info
->fprintf_func
) (info
->stream
, "r%d", (int)rs1
);
170 if (func
!= MVTSF
&& func
!= MVFSF
)
171 (*info
->fprintf_func
) (info
->stream
, ",r%d", (int)rs2
);
173 return (unsigned char) R_TYPE
;
176 return (unsigned char) R_ERROR
;
179 /* Process the memory read opcode. */
182 dlx_load_type (struct disassemble_info
* info
)
186 unsigned long opcode
;
191 { OPC(LHIOP
), "lhi" }, /* Load HI to register. */
192 { OPC(LBOP
), "lb" }, /* load byte sign extended. */
193 { OPC(LBUOP
), "lbu" }, /* load byte unsigned. */
194 { OPC(LSBUOP
),"ldstbu"}, /* load store byte unsigned. */
195 { OPC(LHOP
), "lh" }, /* load halfword sign extended. */
196 { OPC(LHUOP
), "lhu" }, /* load halfword unsigned. */
197 { OPC(LSHUOP
),"ldsthu"}, /* load store halfword unsigned. */
198 { OPC(LWOP
), "lw" }, /* load word. */
199 { OPC(LSWOP
), "ldstw" } /* load store word. */
201 int dlx_load_opcode_num
=
202 (sizeof dlx_load_opcode
) / (sizeof dlx_load_opcode
[0]);
205 for (idx
= 0 ; idx
< dlx_load_opcode_num
; idx
++)
206 if (dlx_load_opcode
[idx
].opcode
== opc
)
208 if (opc
== OPC (LHIOP
))
210 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_load_opcode
[idx
].name
);
211 operand_deliminator (info
, dlx_load_opcode
[idx
].name
);
212 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int)rs2
);
213 (*info
->fprintf_func
) (info
->stream
, "0x%04x", (int)imm16
);
217 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_load_opcode
[idx
].name
);
218 operand_deliminator (info
, dlx_load_opcode
[idx
].name
);
219 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int)rs2
);
220 (*info
->fprintf_func
) (info
->stream
, "0x%04x[r%d]", (int)imm16
, (int)rs1
);
223 return (unsigned char) ILD_TYPE
;
226 return (unsigned char) NIL
;
229 /* Process the memory store opcode. */
232 dlx_store_type (struct disassemble_info
* info
)
236 unsigned long opcode
;
241 { OPC(SBOP
), "sb" }, /* Store byte. */
242 { OPC(SHOP
), "sh" }, /* Store halfword. */
243 { OPC(SWOP
), "sw" }, /* Store word. */
245 int dlx_store_opcode_num
=
246 (sizeof dlx_store_opcode
) / (sizeof dlx_store_opcode
[0]);
249 for (idx
= 0 ; idx
< dlx_store_opcode_num
; idx
++)
250 if (dlx_store_opcode
[idx
].opcode
== opc
)
252 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_store_opcode
[idx
].name
);
253 operand_deliminator (info
, dlx_store_opcode
[idx
].name
);
254 (*info
->fprintf_func
) (info
->stream
, "0x%04x[r%d],", (int)imm16
, (int)rs1
);
255 (*info
->fprintf_func
) (info
->stream
, "r%d", (int)rs2
);
256 return (unsigned char) IST_TYPE
;
259 return (unsigned char) NIL
;
262 /* Process the Arithmetic and Logical I-TYPE opcode. */
265 dlx_aluI_type (struct disassemble_info
* info
)
269 unsigned long opcode
;
274 { OPC(ADDIOP
), "addi" }, /* Store byte. */
275 { OPC(ADDUIOP
), "addui" }, /* Store halfword. */
276 { OPC(SUBIOP
), "subi" }, /* Store word. */
277 { OPC(SUBUIOP
), "subui" }, /* Store word. */
278 { OPC(ANDIOP
), "andi" }, /* Store word. */
279 { OPC(ORIOP
), "ori" }, /* Store word. */
280 { OPC(XORIOP
), "xori" }, /* Store word. */
281 { OPC(SLLIOP
), "slli" }, /* Store word. */
282 { OPC(SRAIOP
), "srai" }, /* Store word. */
283 { OPC(SRLIOP
), "srli" }, /* Store word. */
284 { OPC(SEQIOP
), "seqi" }, /* Store word. */
285 { OPC(SNEIOP
), "snei" }, /* Store word. */
286 { OPC(SLTIOP
), "slti" }, /* Store word. */
287 { OPC(SGTIOP
), "sgti" }, /* Store word. */
288 { OPC(SLEIOP
), "slei" }, /* Store word. */
289 { OPC(SGEIOP
), "sgei" }, /* Store word. */
290 { OPC(SEQUIOP
), "sequi" }, /* Store word. */
291 { OPC(SNEUIOP
), "sneui" }, /* Store word. */
292 { OPC(SLTUIOP
), "sltui" }, /* Store word. */
293 { OPC(SGTUIOP
), "sgtui" }, /* Store word. */
294 { OPC(SLEUIOP
), "sleui" }, /* Store word. */
295 { OPC(SGEUIOP
), "sgeui" }, /* Store word. */
297 { OPC(MVTSOP
), "mvts" }, /* Store word. */
298 { OPC(MVFSOP
), "mvfs" }, /* Store word. */
301 int dlx_aluI_opcode_num
=
302 (sizeof dlx_aluI_opcode
) / (sizeof dlx_aluI_opcode
[0]);
305 for (idx
= 0 ; idx
< dlx_aluI_opcode_num
; idx
++)
306 if (dlx_aluI_opcode
[idx
].opcode
== opc
)
308 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_aluI_opcode
[idx
].name
);
309 operand_deliminator (info
, dlx_aluI_opcode
[idx
].name
);
310 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int)rs2
);
311 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int)rs1
);
312 (*info
->fprintf_func
) (info
->stream
, "0x%04x", (int)imm16
);
314 return (unsigned char) IAL_TYPE
;
317 return (unsigned char) NIL
;
320 /* Process the branch instruction. */
323 dlx_br_type (struct disassemble_info
* info
)
327 unsigned long opcode
;
332 { OPC(BEQOP
), "beqz" }, /* Store byte. */
333 { OPC(BNEOP
), "bnez" } /* Store halfword. */
335 int dlx_br_opcode_num
=
336 (sizeof dlx_br_opcode
) / (sizeof dlx_br_opcode
[0]);
339 for (idx
= 0 ; idx
< dlx_br_opcode_num
; idx
++)
340 if (dlx_br_opcode
[idx
].opcode
== opc
)
342 if (imm16
& 0x00008000)
345 imm16
+= (current_insn_addr
+ 4);
346 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_br_opcode
[idx
].name
);
347 operand_deliminator (info
, dlx_br_opcode
[idx
].name
);
348 (*info
->fprintf_func
) (info
->stream
, "r%d,", (int) rs1
);
349 (*info
->fprintf_func
) (info
->stream
, "0x%08x", (int) imm16
);
351 return (unsigned char) IBR_TYPE
;
354 return (unsigned char) NIL
;
357 /* Process the jump instruction. */
360 dlx_jmp_type (struct disassemble_info
* info
)
364 unsigned long opcode
;
369 { OPC(JOP
), "j" }, /* Store byte. */
370 { OPC(JALOP
), "jal" }, /* Store halfword. */
371 { OPC(BREAKOP
), "break" }, /* Store halfword. */
372 { OPC(TRAPOP
), "trap" }, /* Store halfword. */
373 { OPC(RFEOP
), "rfe" } /* Store halfword. */
375 int dlx_jmp_opcode_num
=
376 (sizeof dlx_jmp_opcode
) / (sizeof dlx_jmp_opcode
[0]);
379 for (idx
= 0 ; idx
< dlx_jmp_opcode_num
; idx
++)
380 if (dlx_jmp_opcode
[idx
].opcode
== opc
)
382 if (imm26
& 0x02000000)
385 imm26
+= (current_insn_addr
+ 4);
387 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_jmp_opcode
[idx
].name
);
388 operand_deliminator (info
, dlx_jmp_opcode
[idx
].name
);
389 (*info
->fprintf_func
) (info
->stream
, "0x%08x", (int)imm26
);
391 return (unsigned char) IJ_TYPE
;
394 return (unsigned char) NIL
;
397 /* Process the jump register instruction. */
400 dlx_jr_type (struct disassemble_info
* info
)
404 unsigned long opcode
;
409 { OPC(JROP
), "jr" }, /* Store byte. */
410 { OPC(JALROP
), "jalr" } /* Store halfword. */
412 int dlx_jr_opcode_num
=
413 (sizeof dlx_jr_opcode
) / (sizeof dlx_jr_opcode
[0]);
416 for (idx
= 0 ; idx
< dlx_jr_opcode_num
; idx
++)
417 if (dlx_jr_opcode
[idx
].opcode
== opc
)
419 (*info
->fprintf_func
) (info
->stream
, "%s", dlx_jr_opcode
[idx
].name
);
420 operand_deliminator (info
, dlx_jr_opcode
[idx
].name
);
421 (*info
->fprintf_func
) (info
->stream
, "r%d", (int)rs1
);
422 return (unsigned char) IJR_TYPE
;
425 return (unsigned char) NIL
;
428 typedef unsigned char (* dlx_insn
) (struct disassemble_info
*);
430 /* This is the main DLX insn handling routine. */
433 print_insn_dlx (bfd_vma memaddr
, struct disassemble_info
* info
)
437 unsigned long insn_word
;
438 unsigned char rtn_code
;
439 unsigned long dlx_insn_type
[] =
441 (unsigned long) dlx_r_type
,
442 (unsigned long) dlx_load_type
,
443 (unsigned long) dlx_store_type
,
444 (unsigned long) dlx_aluI_type
,
445 (unsigned long) dlx_br_type
,
446 (unsigned long) dlx_jmp_type
,
447 (unsigned long) dlx_jr_type
,
450 int dlx_insn_type_num
= ((sizeof dlx_insn_type
) / (sizeof (unsigned long))) - 1;
452 (*info
->read_memory_func
) (memaddr
, (bfd_byte
*) &buffer
[0], 4, info
);
456 (*info
->memory_error_func
) (status
, memaddr
, info
);
460 /* Now decode the insn */
461 insn_word
= bfd_getb32 (buffer
);
462 opc
= dlx_get_opcode (insn_word
);
463 rs1
= dlx_get_rs1 (insn_word
);
464 rs2
= dlx_get_rs2 (insn_word
);
465 rd
= dlx_get_rdR (insn_word
);
466 func
= dlx_get_func (insn_word
);
467 imm16
= dlx_get_imm16 (insn_word
);
468 imm26
= dlx_get_imm26 (insn_word
);
471 printf ("print_insn_big_dlx: opc = 0x%02x\n"
478 opc
, rs1
, rs2
, rd
, func
, imm16
, imm26
);
481 /* Scan through all the insn type and print the insn out. */
483 current_insn_addr
= (unsigned long) memaddr
;
485 for (insn_idx
= 0; dlx_insn_type
[insn_idx
] != 0x0; insn_idx
++)
486 switch (((dlx_insn
) (dlx_insn_type
[insn_idx
])) (info
))
488 /* Found the correct opcode */
498 /* Wrong insn type check next one. */
503 /* All rest of the return code are not recongnized, treat it as error */
504 /* we should never get here, I hope! */
509 if (insn_idx
== dlx_insn_type_num
)
510 /* Well, does not recoganize this opcode. */
511 (*info
->fprintf_func
) (info
->stream
, "<%s>", "Unrecognized Opcode");