1 /* Disassembler code for Renesas RL78.
2 Copyright (C) 2011-2016 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. */
29 #include "opcode/rl78.h"
32 #define DEBUG_SEMANTICS 0
37 disassemble_info * dis;
41 rl78_get_byte (void * vdata)
44 RL78_Data *rl78_data = (RL78_Data *) vdata;
46 rl78_data->dis->read_memory_func (rl78_data->pc,
59 "x", "a", "c", "b", "e", "d", "l", "h",
60 "ax", "bc", "de", "hl",
61 "sp", "psw", "cs", "es", "pmc", "mem"
67 "t", "f", "c", "nc", "h", "nh", "z", "nz"
75 case RL78_Operand_Indirect:
76 case RL78_Operand_BitIndirect:
77 case RL78_Operand_PostInc:
78 case RL78_Operand_PreDec:
86 print_insn_rl78_common (bfd_vma addr, disassemble_info * dis, RL78_Dis_Isa isa)
90 RL78_Opcode_Decoded opcode;
99 rv = rl78_decode_opcode (addr, &opcode, rl78_get_byte, &rl78_data, isa);
101 dis->bytes_per_line = 10;
103 #define PR (dis->fprintf_func)
104 #define PS (dis->stream)
105 #define PC(c) PR (PS, "%c", c)
113 case RLO_unknown: s = "uknown"; break;
114 case RLO_add: s = "add: %e0%0 += %e1%1"; break;
115 case RLO_addc: s = "addc: %e0%0 += %e1%1 + CY"; break;
116 case RLO_and: s = "and: %e0%0 &= %e1%1"; break;
117 case RLO_branch: s = "branch: pc = %e0%0"; break;
118 case RLO_branch_cond: s = "branch_cond: pc = %e0%0 if %c1 / %e1%1"; break;
119 case RLO_branch_cond_clear: s = "branch_cond_clear: pc = %e0%0 if %c1 / %e1%1, %e1%1 = 0"; break;
120 case RLO_call: s = "call: pc = %e1%0"; break;
121 case RLO_cmp: s = "cmp: %e0%0 - %e1%1"; break;
122 case RLO_mov: s = "mov: %e0%0 = %e1%1"; break;
123 case RLO_or: s = "or: %e0%0 |= %e1%1"; break;
124 case RLO_rol: s = "rol: %e0%0 <<= %e1%1"; break;
125 case RLO_rolc: s = "rol: %e0%0 <<= %e1%1,CY"; break;
126 case RLO_ror: s = "ror: %e0%0 >>= %e1%1"; break;
127 case RLO_rorc: s = "ror: %e0%0 >>= %e1%1,CY"; break;
128 case RLO_sar: s = "sar: %e0%0 >>= %e1%1 signed"; break;
129 case RLO_sel: s = "sel: rb = %1"; break;
130 case RLO_shr: s = "shr: %e0%0 >>= %e1%1 unsigned"; break;
131 case RLO_shl: s = "shl: %e0%0 <<= %e1%1"; break;
132 case RLO_skip: s = "skip: if %c1"; break;
133 case RLO_sub: s = "sub: %e0%0 -= %e1%1"; break;
134 case RLO_subc: s = "subc: %e0%0 -= %e1%1 - CY"; break;
135 case RLO_xch: s = "xch: %e0%0 <-> %e1%1"; break;
136 case RLO_xor: s = "xor: %e0%0 ^= %e1%1"; break;
139 sprintf(buf, "%s%%W%%f\t\033[32m%s\033[0m", s, opcode.syntax);
152 RL78_Opcode_Operand * oper;
184 goto no_more_modifiers;
198 if (opcode.size == RL78_Word)
199 PR (PS, " \033[33mW\033[0m");
206 PR (PS, " \033[35m");
208 if (opcode.flags & RL78_PSW_Z)
209 { PR (PS, "Z"); comma = ","; }
210 if (opcode.flags & RL78_PSW_AC)
211 { PR (PS, "%sAC", comma); comma = ","; }
212 if (opcode.flags & RL78_PSW_CY)
213 { PR (PS, "%sCY", comma); comma = ","; }
222 oper = *s == '0' ? &opcode.op[0] : &opcode.op[1];
225 if (oper->use_es && indirect_type (oper->type))
231 /* If we are going to display SP by name, we must omit the bang. */
232 if ((oper->type == RL78_Operand_Indirect
233 || oper->type == RL78_Operand_BitIndirect)
234 && oper->reg == RL78_Reg_None
236 && ((oper->addend == 0xffff8 && opcode.size == RL78_Word)
237 || (oper->addend == 0x0fff8 && do_es && opcode.size == RL78_Word)))
245 PR (PS, "%s", condition_names[oper->condition]);
251 case RL78_Operand_Immediate:
253 dis->print_address_func (oper->addend, dis);
255 || oper->addend > 999
256 || oper->addend < -999)
257 PR (PS, "%#x", oper->addend);
259 PR (PS, "%d", oper->addend);
262 case RL78_Operand_Register:
263 PR (PS, "%s", register_names[oper->reg]);
266 case RL78_Operand_Bit:
267 PR (PS, "%s.%d", register_names[oper->reg], oper->bit_number);
270 case RL78_Operand_Indirect:
271 case RL78_Operand_BitIndirect:
275 if (oper->addend == 0xffffa && do_sfr && opcode.size == RL78_Byte)
277 else if (oper->addend == 0xffff8 && do_sfr && opcode.size == RL78_Word)
279 else if (oper->addend == 0x0fff8 && do_sfr && do_es && opcode.size == RL78_Word)
281 else if (oper->addend == 0xffff8 && do_sfr && opcode.size == RL78_Byte)
283 else if (oper->addend == 0xffff9 && do_sfr && opcode.size == RL78_Byte)
285 else if (oper->addend == 0xffffc && do_sfr && opcode.size == RL78_Byte)
287 else if (oper->addend == 0xffffd && do_sfr && opcode.size == RL78_Byte)
289 else if (oper->addend == 0xffffe && do_sfr && opcode.size == RL78_Byte)
291 else if (oper->addend == 0xfffff && do_sfr && opcode.size == RL78_Byte)
293 else if (oper->addend >= 0xffe20)
294 PR (PS, "%#x", oper->addend);
297 int faddr = oper->addend;
298 if (do_es && ! oper->use_es)
300 dis->print_address_func (faddr, dis);
307 PR (PS, "%d[%s]", oper->addend, register_names[oper->reg]);
311 PR (PS, "[%s", register_names[oper->reg]);
312 if (oper->reg2 != RL78_Reg_None)
313 PR (PS, "+%s", register_names[oper->reg2]);
314 if (oper->addend || do_addr)
315 PR (PS, "+%d", oper->addend);
320 if (oper->type == RL78_Operand_BitIndirect)
321 PR (PS, ".%d", oper->bit_number);
325 /* Shouldn't happen - push and pop don't print
326 [SP] directly. But we *do* use them for
327 semantic debugging. */
328 case RL78_Operand_PostInc:
329 PR (PS, "[%s++]", register_names[oper->reg]);
331 case RL78_Operand_PreDec:
332 PR (PS, "[--%s]", register_names[oper->reg]);
337 /* If we ever print this, that means the
338 programmer tried to print an operand with a
339 type we don't expect. Print the line and
340 operand number from rl78-decode.opc for
342 PR (PS, "???%d.%d", opcode.lineno, *s - '0');
351 PR (PS, "\t\033[34m(line %d)\033[0m", opcode.lineno);
359 print_insn_rl78 (bfd_vma addr, disassemble_info * dis)
361 return print_insn_rl78_common (addr, dis, RL78_ISA_DEFAULT);
365 print_insn_rl78_g10 (bfd_vma addr, disassemble_info * dis)
367 return print_insn_rl78_common (addr, dis, RL78_ISA_G10);
371 print_insn_rl78_g13 (bfd_vma addr, disassemble_info * dis)
373 return print_insn_rl78_common (addr, dis, RL78_ISA_G13);
377 print_insn_rl78_g14 (bfd_vma addr, disassemble_info * dis)
379 return print_insn_rl78_common (addr, dis, RL78_ISA_G14);
383 rl78_get_disassembler (bfd *abfd)
385 int cpu = abfd->tdata.elf_obj_data->elf_header->e_flags & E_FLAG_RL78_CPU_MASK;
388 case E_FLAG_RL78_G10:
389 return print_insn_rl78_g10;
390 case E_FLAG_RL78_G13:
391 return print_insn_rl78_g13;
392 case E_FLAG_RL78_G14:
393 return print_insn_rl78_g14;
395 return print_insn_rl78;