rl78-dis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Disassembler code for Renesas RL78.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. Written by DJ Delorie.
  5. This file is part of the GNU opcodes library.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. 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. */
  18. #include "sysdep.h"
  19. #include <stdio.h>
  20. #include "bfd.h"
  21. #include "elf-bfd.h"
  22. #include "disassemble.h"
  23. #include "opcode/rl78.h"
  24. #include "elf/rl78.h"
  25. #include <setjmp.h>
  26. #define DEBUG_SEMANTICS 0
  27. typedef struct
  28. {
  29. bfd_vma pc;
  30. disassemble_info * dis;
  31. } RL78_Data;
  32. struct private
  33. {
  34. OPCODES_SIGJMP_BUF bailout;
  35. };
  36. static int
  37. rl78_get_byte (void * vdata)
  38. {
  39. bfd_byte buf[1];
  40. RL78_Data *rl78_data = (RL78_Data *) vdata;
  41. int status;
  42. status = rl78_data->dis->read_memory_func (rl78_data->pc,
  43. buf,
  44. 1,
  45. rl78_data->dis);
  46. if (status != 0)
  47. {
  48. struct private *priv = (struct private *) rl78_data->dis->private_data;
  49. rl78_data->dis->memory_error_func (status, rl78_data->pc,
  50. rl78_data->dis);
  51. OPCODES_SIGLONGJMP (priv->bailout, 1);
  52. }
  53. rl78_data->pc ++;
  54. return buf[0];
  55. }
  56. static char const *
  57. register_names[] =
  58. {
  59. "",
  60. "x", "a", "c", "b", "e", "d", "l", "h",
  61. "ax", "bc", "de", "hl",
  62. "sp", "psw", "cs", "es", "pmc", "mem"
  63. };
  64. static char const *
  65. condition_names[] =
  66. {
  67. "t", "f", "c", "nc", "h", "nh", "z", "nz"
  68. };
  69. static int
  70. indirect_type (int t)
  71. {
  72. switch (t)
  73. {
  74. case RL78_Operand_Indirect:
  75. case RL78_Operand_BitIndirect:
  76. case RL78_Operand_PostInc:
  77. case RL78_Operand_PreDec:
  78. return 1;
  79. default:
  80. return 0;
  81. }
  82. }
  83. static int
  84. print_insn_rl78_common (bfd_vma addr, disassemble_info * dis, RL78_Dis_Isa isa)
  85. {
  86. int rv;
  87. RL78_Data rl78_data;
  88. RL78_Opcode_Decoded opcode;
  89. const char * s;
  90. #if DEBUG_SEMANTICS
  91. static char buf[200];
  92. #endif
  93. struct private priv;
  94. dis->private_data = (PTR) &priv;
  95. rl78_data.pc = addr;
  96. rl78_data.dis = dis;
  97. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  98. {
  99. /* Error return. */
  100. return -1;
  101. }
  102. rv = rl78_decode_opcode (addr, &opcode, rl78_get_byte, &rl78_data, isa);
  103. dis->bytes_per_line = 10;
  104. #define PR (dis->fprintf_func)
  105. #define PS (dis->stream)
  106. #define PC(c) PR (PS, "%c", c)
  107. s = opcode.syntax;
  108. #if DEBUG_SEMANTICS
  109. switch (opcode.id)
  110. {
  111. case RLO_unknown: s = "uknown"; break;
  112. case RLO_add: s = "add: %e0%0 += %e1%1"; break;
  113. case RLO_addc: s = "addc: %e0%0 += %e1%1 + CY"; break;
  114. case RLO_and: s = "and: %e0%0 &= %e1%1"; break;
  115. case RLO_branch: s = "branch: pc = %e0%0"; break;
  116. case RLO_branch_cond: s = "branch_cond: pc = %e0%0 if %c1 / %e1%1"; break;
  117. case RLO_branch_cond_clear: s = "branch_cond_clear: pc = %e0%0 if %c1 / %e1%1, %e1%1 = 0"; break;
  118. case RLO_call: s = "call: pc = %e1%0"; break;
  119. case RLO_cmp: s = "cmp: %e0%0 - %e1%1"; break;
  120. case RLO_mov: s = "mov: %e0%0 = %e1%1"; break;
  121. case RLO_or: s = "or: %e0%0 |= %e1%1"; break;
  122. case RLO_rol: s = "rol: %e0%0 <<= %e1%1"; break;
  123. case RLO_rolc: s = "rol: %e0%0 <<= %e1%1,CY"; break;
  124. case RLO_ror: s = "ror: %e0%0 >>= %e1%1"; break;
  125. case RLO_rorc: s = "ror: %e0%0 >>= %e1%1,CY"; break;
  126. case RLO_sar: s = "sar: %e0%0 >>= %e1%1 signed"; break;
  127. case RLO_sel: s = "sel: rb = %1"; break;
  128. case RLO_shr: s = "shr: %e0%0 >>= %e1%1 unsigned"; break;
  129. case RLO_shl: s = "shl: %e0%0 <<= %e1%1"; break;
  130. case RLO_skip: s = "skip: if %c1"; break;
  131. case RLO_sub: s = "sub: %e0%0 -= %e1%1"; break;
  132. case RLO_subc: s = "subc: %e0%0 -= %e1%1 - CY"; break;
  133. case RLO_xch: s = "xch: %e0%0 <-> %e1%1"; break;
  134. case RLO_xor: s = "xor: %e0%0 ^= %e1%1"; break;
  135. }
  136. sprintf(buf, "%s%%W%%f\t\033[32m%s\033[0m", s, opcode.syntax);
  137. s = buf;
  138. #endif
  139. for (; *s; s++)
  140. {
  141. if (*s != '%')
  142. {
  143. PC (*s);
  144. }
  145. else
  146. {
  147. RL78_Opcode_Operand * oper;
  148. int do_hex = 0;
  149. int do_addr = 0;
  150. int do_es = 0;
  151. int do_sfr = 0;
  152. int do_cond = 0;
  153. int do_bang = 0;
  154. while (1)
  155. {
  156. s ++;
  157. switch (*s)
  158. {
  159. case 'x':
  160. do_hex = 1;
  161. break;
  162. case '!':
  163. do_bang = 1;
  164. break;
  165. case 'e':
  166. do_es = 1;
  167. break;
  168. case 'a':
  169. do_addr = 1;
  170. break;
  171. case 's':
  172. do_sfr = 1;
  173. break;
  174. case 'c':
  175. do_cond = 1;
  176. break;
  177. default:
  178. goto no_more_modifiers;
  179. }
  180. }
  181. no_more_modifiers:;
  182. switch (*s)
  183. {
  184. case '%':
  185. PC ('%');
  186. break;
  187. #if DEBUG_SEMANTICS
  188. case 'W':
  189. if (opcode.size == RL78_Word)
  190. PR (PS, " \033[33mW\033[0m");
  191. break;
  192. case 'f':
  193. if (opcode.flags)
  194. {
  195. char *comma = "";
  196. PR (PS, " \033[35m");
  197. if (opcode.flags & RL78_PSW_Z)
  198. { PR (PS, "Z"); comma = ","; }
  199. if (opcode.flags & RL78_PSW_AC)
  200. { PR (PS, "%sAC", comma); comma = ","; }
  201. if (opcode.flags & RL78_PSW_CY)
  202. { PR (PS, "%sCY", comma); comma = ","; }
  203. PR (PS, "\033[0m");
  204. }
  205. break;
  206. #endif
  207. case '0':
  208. case '1':
  209. oper = *s == '0' ? &opcode.op[0] : &opcode.op[1];
  210. if (do_es)
  211. {
  212. if (oper->use_es && indirect_type (oper->type))
  213. PR (PS, "es:");
  214. }
  215. if (do_bang)
  216. {
  217. /* If we are going to display SP by name, we must omit the bang. */
  218. if ((oper->type == RL78_Operand_Indirect
  219. || oper->type == RL78_Operand_BitIndirect)
  220. && oper->reg == RL78_Reg_None
  221. && do_sfr
  222. && ((oper->addend == 0xffff8 && opcode.size == RL78_Word)
  223. || (oper->addend == 0x0fff8 && do_es && opcode.size == RL78_Word)))
  224. ;
  225. else
  226. PC ('!');
  227. }
  228. if (do_cond)
  229. {
  230. PR (PS, "%s", condition_names[oper->condition]);
  231. break;
  232. }
  233. switch (oper->type)
  234. {
  235. case RL78_Operand_Immediate:
  236. if (do_addr)
  237. dis->print_address_func (oper->addend, dis);
  238. else if (do_hex
  239. || oper->addend > 999
  240. || oper->addend < -999)
  241. PR (PS, "%#x", oper->addend);
  242. else
  243. PR (PS, "%d", oper->addend);
  244. break;
  245. case RL78_Operand_Register:
  246. PR (PS, "%s", register_names[oper->reg]);
  247. break;
  248. case RL78_Operand_Bit:
  249. PR (PS, "%s.%d", register_names[oper->reg], oper->bit_number);
  250. break;
  251. case RL78_Operand_Indirect:
  252. case RL78_Operand_BitIndirect:
  253. switch (oper->reg)
  254. {
  255. case RL78_Reg_None:
  256. if (oper->addend == 0xffffa && do_sfr && opcode.size == RL78_Byte)
  257. PR (PS, "psw");
  258. else if (oper->addend == 0xffff8 && do_sfr && opcode.size == RL78_Word)
  259. PR (PS, "sp");
  260. else if (oper->addend == 0x0fff8 && do_sfr && do_es && opcode.size == RL78_Word)
  261. PR (PS, "sp");
  262. else if (oper->addend == 0xffff8 && do_sfr && opcode.size == RL78_Byte)
  263. PR (PS, "spl");
  264. else if (oper->addend == 0xffff9 && do_sfr && opcode.size == RL78_Byte)
  265. PR (PS, "sph");
  266. else if (oper->addend == 0xffffc && do_sfr && opcode.size == RL78_Byte)
  267. PR (PS, "cs");
  268. else if (oper->addend == 0xffffd && do_sfr && opcode.size == RL78_Byte)
  269. PR (PS, "es");
  270. else if (oper->addend == 0xffffe && do_sfr && opcode.size == RL78_Byte)
  271. PR (PS, "pmc");
  272. else if (oper->addend == 0xfffff && do_sfr && opcode.size == RL78_Byte)
  273. PR (PS, "mem");
  274. else if (oper->addend >= 0xffe20)
  275. PR (PS, "%#x", oper->addend);
  276. else
  277. {
  278. int faddr = oper->addend;
  279. if (do_es && ! oper->use_es)
  280. faddr += 0xf0000;
  281. dis->print_address_func (faddr, dis);
  282. }
  283. break;
  284. case RL78_Reg_B:
  285. case RL78_Reg_C:
  286. case RL78_Reg_BC:
  287. PR (PS, "%d[%s]", oper->addend, register_names[oper->reg]);
  288. break;
  289. default:
  290. PR (PS, "[%s", register_names[oper->reg]);
  291. if (oper->reg2 != RL78_Reg_None)
  292. PR (PS, "+%s", register_names[oper->reg2]);
  293. if (oper->addend || do_addr)
  294. PR (PS, "+%d", oper->addend);
  295. PC (']');
  296. break;
  297. }
  298. if (oper->type == RL78_Operand_BitIndirect)
  299. PR (PS, ".%d", oper->bit_number);
  300. break;
  301. #if DEBUG_SEMANTICS
  302. /* Shouldn't happen - push and pop don't print
  303. [SP] directly. But we *do* use them for
  304. semantic debugging. */
  305. case RL78_Operand_PostInc:
  306. PR (PS, "[%s++]", register_names[oper->reg]);
  307. break;
  308. case RL78_Operand_PreDec:
  309. PR (PS, "[--%s]", register_names[oper->reg]);
  310. break;
  311. #endif
  312. default:
  313. /* If we ever print this, that means the
  314. programmer tried to print an operand with a
  315. type we don't expect. Print the line and
  316. operand number from rl78-decode.opc for
  317. them. */
  318. PR (PS, "???%d.%d", opcode.lineno, *s - '0');
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. #if DEBUG_SEMANTICS
  325. PR (PS, "\t\033[34m(line %d)\033[0m", opcode.lineno);
  326. #endif
  327. return rv;
  328. }
  329. int
  330. print_insn_rl78 (bfd_vma addr, disassemble_info * dis)
  331. {
  332. return print_insn_rl78_common (addr, dis, RL78_ISA_DEFAULT);
  333. }
  334. int
  335. print_insn_rl78_g10 (bfd_vma addr, disassemble_info * dis)
  336. {
  337. return print_insn_rl78_common (addr, dis, RL78_ISA_G10);
  338. }
  339. int
  340. print_insn_rl78_g13 (bfd_vma addr, disassemble_info * dis)
  341. {
  342. return print_insn_rl78_common (addr, dis, RL78_ISA_G13);
  343. }
  344. int
  345. print_insn_rl78_g14 (bfd_vma addr, disassemble_info * dis)
  346. {
  347. return print_insn_rl78_common (addr, dis, RL78_ISA_G14);
  348. }
  349. disassembler_ftype
  350. rl78_get_disassembler (bfd *abfd)
  351. {
  352. int cpu = E_FLAG_RL78_ANY_CPU;
  353. if (abfd != NULL)
  354. cpu = abfd->tdata.elf_obj_data->elf_header->e_flags & E_FLAG_RL78_CPU_MASK;
  355. switch (cpu)
  356. {
  357. case E_FLAG_RL78_G10:
  358. return print_insn_rl78_g10;
  359. case E_FLAG_RL78_G13:
  360. return print_insn_rl78_g13;
  361. case E_FLAG_RL78_G14:
  362. return print_insn_rl78_g14;
  363. default:
  364. return print_insn_rl78;
  365. }
  366. }