1 /* Disassembler code for Renesas RL78.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
3 Contributed by Red Hat.
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. */
28 #include "disassemble.h"
29 #include "opcode/rl78.h"
34 #define DEBUG_SEMANTICS 0
39 disassemble_info
* dis
;
44 OPCODES_SIGJMP_BUF bailout
;
48 rl78_get_byte (void * vdata
)
51 RL78_Data
*rl78_data
= (RL78_Data
*) vdata
;
54 status
= rl78_data
->dis
->read_memory_func (rl78_data
->pc
,
60 struct private *priv
= (struct private *) rl78_data
->dis
->private_data
;
62 rl78_data
->dis
->memory_error_func (status
, rl78_data
->pc
,
64 OPCODES_SIGLONGJMP (priv
->bailout
, 1);
75 "x", "a", "c", "b", "e", "d", "l", "h",
76 "ax", "bc", "de", "hl",
77 "sp", "psw", "cs", "es", "pmc", "mem"
83 "t", "f", "c", "nc", "h", "nh", "z", "nz"
91 case RL78_Operand_Indirect
:
92 case RL78_Operand_BitIndirect
:
93 case RL78_Operand_PostInc
:
94 case RL78_Operand_PreDec
:
102 print_insn_rl78_common (bfd_vma addr
, disassemble_info
* dis
, RL78_Dis_Isa isa
)
106 RL78_Opcode_Decoded opcode
;
109 static char buf
[200];
113 dis
->private_data
= (PTR
) &priv
;
117 if (OPCODES_SIGSETJMP (priv
.bailout
) != 0)
123 rv
= rl78_decode_opcode (addr
, &opcode
, rl78_get_byte
, &rl78_data
, isa
);
125 dis
->bytes_per_line
= 10;
127 #define PR (dis->fprintf_func)
128 #define PS (dis->stream)
129 #define PC(c) PR (PS, "%c", c)
137 case RLO_unknown
: s
= "uknown"; break;
138 case RLO_add
: s
= "add: %e0%0 += %e1%1"; break;
139 case RLO_addc
: s
= "addc: %e0%0 += %e1%1 + CY"; break;
140 case RLO_and
: s
= "and: %e0%0 &= %e1%1"; break;
141 case RLO_branch
: s
= "branch: pc = %e0%0"; break;
142 case RLO_branch_cond
: s
= "branch_cond: pc = %e0%0 if %c1 / %e1%1"; break;
143 case RLO_branch_cond_clear
: s
= "branch_cond_clear: pc = %e0%0 if %c1 / %e1%1, %e1%1 = 0"; break;
144 case RLO_call
: s
= "call: pc = %e1%0"; break;
145 case RLO_cmp
: s
= "cmp: %e0%0 - %e1%1"; break;
146 case RLO_mov
: s
= "mov: %e0%0 = %e1%1"; break;
147 case RLO_or
: s
= "or: %e0%0 |= %e1%1"; break;
148 case RLO_rol
: s
= "rol: %e0%0 <<= %e1%1"; break;
149 case RLO_rolc
: s
= "rol: %e0%0 <<= %e1%1,CY"; break;
150 case RLO_ror
: s
= "ror: %e0%0 >>= %e1%1"; break;
151 case RLO_rorc
: s
= "ror: %e0%0 >>= %e1%1,CY"; break;
152 case RLO_sar
: s
= "sar: %e0%0 >>= %e1%1 signed"; break;
153 case RLO_sel
: s
= "sel: rb = %1"; break;
154 case RLO_shr
: s
= "shr: %e0%0 >>= %e1%1 unsigned"; break;
155 case RLO_shl
: s
= "shl: %e0%0 <<= %e1%1"; break;
156 case RLO_skip
: s
= "skip: if %c1"; break;
157 case RLO_sub
: s
= "sub: %e0%0 -= %e1%1"; break;
158 case RLO_subc
: s
= "subc: %e0%0 -= %e1%1 - CY"; break;
159 case RLO_xch
: s
= "xch: %e0%0 <-> %e1%1"; break;
160 case RLO_xor
: s
= "xor: %e0%0 ^= %e1%1"; break;
163 sprintf(buf
, "%s%%W%%f\t\033[32m%s\033[0m", s
, opcode
.syntax
);
176 RL78_Opcode_Operand
* oper
;
208 goto no_more_modifiers
;
222 if (opcode
.size
== RL78_Word
)
223 PR (PS
, " \033[33mW\033[0m");
230 PR (PS
, " \033[35m");
232 if (opcode
.flags
& RL78_PSW_Z
)
233 { PR (PS
, "Z"); comma
= ","; }
234 if (opcode
.flags
& RL78_PSW_AC
)
235 { PR (PS
, "%sAC", comma
); comma
= ","; }
236 if (opcode
.flags
& RL78_PSW_CY
)
237 { PR (PS
, "%sCY", comma
); comma
= ","; }
246 oper
= *s
== '0' ? &opcode
.op
[0] : &opcode
.op
[1];
249 if (oper
->use_es
&& indirect_type (oper
->type
))
255 /* If we are going to display SP by name, we must omit the bang. */
256 if ((oper
->type
== RL78_Operand_Indirect
257 || oper
->type
== RL78_Operand_BitIndirect
)
258 && oper
->reg
== RL78_Reg_None
260 && ((oper
->addend
== 0xffff8 && opcode
.size
== RL78_Word
)
261 || (oper
->addend
== 0x0fff8 && do_es
&& opcode
.size
== RL78_Word
)))
269 PR (PS
, "%s", condition_names
[oper
->condition
]);
275 case RL78_Operand_Immediate
:
277 dis
->print_address_func (oper
->addend
, dis
);
279 || oper
->addend
> 999
280 || oper
->addend
< -999)
281 PR (PS
, "%#x", oper
->addend
);
283 PR (PS
, "%d", oper
->addend
);
286 case RL78_Operand_Register
:
287 PR (PS
, "%s", register_names
[oper
->reg
]);
290 case RL78_Operand_Bit
:
291 PR (PS
, "%s.%d", register_names
[oper
->reg
], oper
->bit_number
);
294 case RL78_Operand_Indirect
:
295 case RL78_Operand_BitIndirect
:
299 if (oper
->addend
== 0xffffa && do_sfr
&& opcode
.size
== RL78_Byte
)
301 else if (oper
->addend
== 0xffff8 && do_sfr
&& opcode
.size
== RL78_Word
)
303 else if (oper
->addend
== 0x0fff8 && do_sfr
&& do_es
&& opcode
.size
== RL78_Word
)
305 else if (oper
->addend
== 0xffff8 && do_sfr
&& opcode
.size
== RL78_Byte
)
307 else if (oper
->addend
== 0xffff9 && do_sfr
&& opcode
.size
== RL78_Byte
)
309 else if (oper
->addend
== 0xffffc && do_sfr
&& opcode
.size
== RL78_Byte
)
311 else if (oper
->addend
== 0xffffd && do_sfr
&& opcode
.size
== RL78_Byte
)
313 else if (oper
->addend
== 0xffffe && do_sfr
&& opcode
.size
== RL78_Byte
)
315 else if (oper
->addend
== 0xfffff && do_sfr
&& opcode
.size
== RL78_Byte
)
317 else if (oper
->addend
>= 0xffe20)
318 PR (PS
, "%#x", oper
->addend
);
321 int faddr
= oper
->addend
;
322 if (do_es
&& ! oper
->use_es
)
324 dis
->print_address_func (faddr
, dis
);
331 PR (PS
, "%d[%s]", oper
->addend
, register_names
[oper
->reg
]);
335 PR (PS
, "[%s", register_names
[oper
->reg
]);
336 if (oper
->reg2
!= RL78_Reg_None
)
337 PR (PS
, "+%s", register_names
[oper
->reg2
]);
338 if (oper
->addend
|| do_addr
)
339 PR (PS
, "+%d", oper
->addend
);
344 if (oper
->type
== RL78_Operand_BitIndirect
)
345 PR (PS
, ".%d", oper
->bit_number
);
349 /* Shouldn't happen - push and pop don't print
350 [SP] directly. But we *do* use them for
351 semantic debugging. */
352 case RL78_Operand_PostInc
:
353 PR (PS
, "[%s++]", register_names
[oper
->reg
]);
355 case RL78_Operand_PreDec
:
356 PR (PS
, "[--%s]", register_names
[oper
->reg
]);
361 /* If we ever print this, that means the
362 programmer tried to print an operand with a
363 type we don't expect. Print the line and
364 operand number from rl78-decode.opc for
366 PR (PS
, "???%d.%d", opcode
.lineno
, *s
- '0');
375 PR (PS
, "\t\033[34m(line %d)\033[0m", opcode
.lineno
);
383 print_insn_rl78 (bfd_vma addr
, disassemble_info
* dis
)
385 return print_insn_rl78_common (addr
, dis
, RL78_ISA_DEFAULT
);
389 print_insn_rl78_g10 (bfd_vma addr
, disassemble_info
* dis
)
391 return print_insn_rl78_common (addr
, dis
, RL78_ISA_G10
);
395 print_insn_rl78_g13 (bfd_vma addr
, disassemble_info
* dis
)
397 return print_insn_rl78_common (addr
, dis
, RL78_ISA_G13
);
401 print_insn_rl78_g14 (bfd_vma addr
, disassemble_info
* dis
)
403 return print_insn_rl78_common (addr
, dis
, RL78_ISA_G14
);
407 rl78_get_disassembler (bfd
*abfd
)
409 int cpu
= E_FLAG_RL78_ANY_CPU
;
412 cpu
= abfd
->tdata
.elf_obj_data
->elf_header
->e_flags
& E_FLAG_RL78_CPU_MASK
;
416 case E_FLAG_RL78_G10
:
417 return print_insn_rl78_g10
;
418 case E_FLAG_RL78_G13
:
419 return print_insn_rl78_g13
;
420 case E_FLAG_RL78_G14
:
421 return print_insn_rl78_g14
;
423 return print_insn_rl78
;