mmix-dis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* mmix-dis.c -- Disassemble MMIX instructions.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Written by Hans-Peter Nilsson (hp@bitrange.com)
  4. This file is part of the GNU opcodes library.
  5. This library 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 3, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this file; see the file COPYING. If not, write to the Free
  15. Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stdio.h>
  19. #include "opcode/mmix.h"
  20. #include "dis-asm.h"
  21. #include "libiberty.h"
  22. #include "bfd.h"
  23. #include "opintl.h"
  24. #define BAD_CASE(x) \
  25. do \
  26. { \
  27. fprintf (stderr, \
  28. _("Bad case %d (%s) in %s:%d\n"), \
  29. x, #x, __FILE__, __LINE__); \
  30. abort (); \
  31. } \
  32. while (0)
  33. #define FATAL_DEBUG \
  34. do \
  35. { \
  36. fprintf (stderr, \
  37. _("Internal: Non-debugged code (test-case missing): %s:%d"),\
  38. __FILE__, __LINE__); \
  39. abort (); \
  40. } \
  41. while (0)
  42. #define ROUND_MODE(n) \
  43. ((n) == 1 ? "ROUND_OFF" : (n) == 2 ? "ROUND_UP" : \
  44. (n) == 3 ? "ROUND_DOWN" : (n) == 4 ? "ROUND_NEAR" : \
  45. _("(unknown)"))
  46. #define INSN_IMMEDIATE_BIT (IMM_OFFSET_BIT << 24)
  47. #define INSN_BACKWARD_OFFSET_BIT (1 << 24)
  48. struct mmix_dis_info
  49. {
  50. const char *reg_name[256];
  51. const char *spec_reg_name[32];
  52. /* Waste a little memory so we don't have to allocate each separately.
  53. We could have an array with static contents for these, but on the
  54. other hand, we don't have to. */
  55. char basic_reg_name[256][sizeof ("$255")];
  56. };
  57. /* Initialize a target-specific array in INFO. */
  58. static bfd_boolean
  59. initialize_mmix_dis_info (struct disassemble_info *info)
  60. {
  61. struct mmix_dis_info *minfop = malloc (sizeof (struct mmix_dis_info));
  62. long i;
  63. if (minfop == NULL)
  64. return FALSE;
  65. memset (minfop, 0, sizeof (*minfop));
  66. /* Initialize register names from register symbols. If there's no
  67. register section, then there are no register symbols. */
  68. if ((info->section != NULL && info->section->owner != NULL)
  69. || (info->symbols != NULL
  70. && info->symbols[0] != NULL
  71. && bfd_asymbol_bfd (info->symbols[0]) != NULL))
  72. {
  73. bfd *abfd = info->section && info->section->owner != NULL
  74. ? info->section->owner
  75. : bfd_asymbol_bfd (info->symbols[0]);
  76. asection *reg_section = bfd_get_section_by_name (abfd, "*REG*");
  77. if (reg_section != NULL)
  78. {
  79. /* The returned symcount *does* include the ending NULL. */
  80. long symsize = bfd_get_symtab_upper_bound (abfd);
  81. asymbol **syms = malloc (symsize);
  82. long nsyms;
  83. if (syms == NULL)
  84. {
  85. FATAL_DEBUG;
  86. free (minfop);
  87. return FALSE;
  88. }
  89. nsyms = bfd_canonicalize_symtab (abfd, syms);
  90. /* We use the first name for a register. If this is MMO, then
  91. it's the name with the first sequence number, presumably the
  92. first in the source. */
  93. for (i = 0; i < nsyms && syms[i] != NULL; i++)
  94. {
  95. if (syms[i]->section == reg_section
  96. && syms[i]->value < 256
  97. && minfop->reg_name[syms[i]->value] == NULL)
  98. minfop->reg_name[syms[i]->value] = syms[i]->name;
  99. }
  100. }
  101. }
  102. /* Fill in the rest with the canonical names. */
  103. for (i = 0; i < 256; i++)
  104. if (minfop->reg_name[i] == NULL)
  105. {
  106. sprintf (minfop->basic_reg_name[i], "$%ld", i);
  107. minfop->reg_name[i] = minfop->basic_reg_name[i];
  108. }
  109. /* We assume it's actually a one-to-one mapping of number-to-name. */
  110. for (i = 0; mmix_spec_regs[i].name != NULL; i++)
  111. minfop->spec_reg_name[mmix_spec_regs[i].number] = mmix_spec_regs[i].name;
  112. info->private_data = (void *) minfop;
  113. return TRUE;
  114. }
  115. /* A table indexed by the first byte is constructed as we disassemble each
  116. tetrabyte. The contents is a pointer into mmix_insns reflecting the
  117. first found entry with matching match-bits and lose-bits. Further
  118. entries are considered one after one until the operand constraints
  119. match or the match-bits and lose-bits do not match. Normally a
  120. "further entry" will just show that there was no other match. */
  121. static const struct mmix_opcode *
  122. get_opcode (unsigned long insn)
  123. {
  124. static const struct mmix_opcode **opcodes = NULL;
  125. const struct mmix_opcode *opcodep = mmix_opcodes;
  126. unsigned int opcode_part = (insn >> 24) & 255;
  127. if (opcodes == NULL)
  128. opcodes = xcalloc (256, sizeof (struct mmix_opcode *));
  129. opcodep = opcodes[opcode_part];
  130. if (opcodep == NULL
  131. || (opcodep->match & insn) != opcodep->match
  132. || (opcodep->lose & insn) != 0)
  133. {
  134. /* Search through the table. */
  135. for (opcodep = mmix_opcodes; opcodep->name != NULL; opcodep++)
  136. {
  137. /* FIXME: Break out this into an initialization function. */
  138. if ((opcodep->match & (opcode_part << 24)) == opcode_part
  139. && (opcodep->lose & (opcode_part << 24)) == 0)
  140. opcodes[opcode_part] = opcodep;
  141. if ((opcodep->match & insn) == opcodep->match
  142. && (opcodep->lose & insn) == 0)
  143. break;
  144. }
  145. }
  146. if (opcodep->name == NULL)
  147. return NULL;
  148. /* Check constraints. If they don't match, loop through the next opcode
  149. entries. */
  150. do
  151. {
  152. switch (opcodep->operands)
  153. {
  154. /* These have no restraint on what can be in the lower three
  155. bytes. */
  156. case mmix_operands_regs:
  157. case mmix_operands_reg_yz:
  158. case mmix_operands_regs_z_opt:
  159. case mmix_operands_regs_z:
  160. case mmix_operands_jmp:
  161. case mmix_operands_pushgo:
  162. case mmix_operands_pop:
  163. case mmix_operands_sync:
  164. case mmix_operands_x_regs_z:
  165. case mmix_operands_neg:
  166. case mmix_operands_pushj:
  167. case mmix_operands_regaddr:
  168. case mmix_operands_get:
  169. case mmix_operands_set:
  170. case mmix_operands_save:
  171. case mmix_operands_unsave:
  172. case mmix_operands_xyz_opt:
  173. return opcodep;
  174. /* For a ROUND_MODE, the middle byte must be 0..4. */
  175. case mmix_operands_roundregs_z:
  176. case mmix_operands_roundregs:
  177. {
  178. int midbyte = (insn >> 8) & 255;
  179. if (midbyte <= 4)
  180. return opcodep;
  181. }
  182. break;
  183. case mmix_operands_put:
  184. /* A "PUT". If it is "immediate", then no restrictions,
  185. otherwise we have to make sure the register number is < 32. */
  186. if ((insn & INSN_IMMEDIATE_BIT)
  187. || ((insn >> 16) & 255) < 32)
  188. return opcodep;
  189. break;
  190. case mmix_operands_resume:
  191. /* Middle bytes must be zero. */
  192. if ((insn & 0x00ffff00) == 0)
  193. return opcodep;
  194. break;
  195. default:
  196. BAD_CASE (opcodep->operands);
  197. }
  198. opcodep++;
  199. }
  200. while ((opcodep->match & insn) == opcodep->match
  201. && (opcodep->lose & insn) == 0);
  202. /* If we got here, we had no match. */
  203. return NULL;
  204. }
  205. /* The main disassembly function. */
  206. int
  207. print_insn_mmix (bfd_vma memaddr, struct disassemble_info *info)
  208. {
  209. unsigned char buffer[4];
  210. unsigned long insn;
  211. unsigned int x, y, z;
  212. const struct mmix_opcode *opcodep;
  213. int status = (*info->read_memory_func) (memaddr, buffer, 4, info);
  214. struct mmix_dis_info *minfop;
  215. if (status != 0)
  216. {
  217. (*info->memory_error_func) (status, memaddr, info);
  218. return -1;
  219. }
  220. /* FIXME: Is -1 suitable? */
  221. if (info->private_data == NULL
  222. && ! initialize_mmix_dis_info (info))
  223. return -1;
  224. minfop = (struct mmix_dis_info *) info->private_data;
  225. x = buffer[1];
  226. y = buffer[2];
  227. z = buffer[3];
  228. insn = bfd_getb32 (buffer);
  229. opcodep = get_opcode (insn);
  230. if (opcodep == NULL)
  231. {
  232. (*info->fprintf_func) (info->stream, _("*unknown*"));
  233. return 4;
  234. }
  235. (*info->fprintf_func) (info->stream, "%s ", opcodep->name);
  236. /* Present bytes in the order they are laid out in memory. */
  237. info->display_endian = BFD_ENDIAN_BIG;
  238. info->insn_info_valid = 1;
  239. info->bytes_per_chunk = 4;
  240. info->branch_delay_insns = 0;
  241. info->target = 0;
  242. switch (opcodep->type)
  243. {
  244. case mmix_type_normal:
  245. case mmix_type_memaccess_block:
  246. info->insn_type = dis_nonbranch;
  247. break;
  248. case mmix_type_branch:
  249. info->insn_type = dis_branch;
  250. break;
  251. case mmix_type_condbranch:
  252. info->insn_type = dis_condbranch;
  253. break;
  254. case mmix_type_memaccess_octa:
  255. info->insn_type = dis_dref;
  256. info->data_size = 8;
  257. break;
  258. case mmix_type_memaccess_tetra:
  259. info->insn_type = dis_dref;
  260. info->data_size = 4;
  261. break;
  262. case mmix_type_memaccess_wyde:
  263. info->insn_type = dis_dref;
  264. info->data_size = 2;
  265. break;
  266. case mmix_type_memaccess_byte:
  267. info->insn_type = dis_dref;
  268. info->data_size = 1;
  269. break;
  270. case mmix_type_jsr:
  271. info->insn_type = dis_jsr;
  272. break;
  273. default:
  274. BAD_CASE(opcodep->type);
  275. }
  276. switch (opcodep->operands)
  277. {
  278. case mmix_operands_regs:
  279. /* All registers: "$X,$Y,$Z". */
  280. (*info->fprintf_func) (info->stream, "%s,%s,%s",
  281. minfop->reg_name[x],
  282. minfop->reg_name[y],
  283. minfop->reg_name[z]);
  284. break;
  285. case mmix_operands_reg_yz:
  286. /* Like SETH - "$X,YZ". */
  287. (*info->fprintf_func) (info->stream, "%s,0x%x",
  288. minfop->reg_name[x], y * 256 + z);
  289. break;
  290. case mmix_operands_regs_z_opt:
  291. case mmix_operands_regs_z:
  292. case mmix_operands_pushgo:
  293. /* The regular "$X,$Y,$Z|Z". */
  294. if (insn & INSN_IMMEDIATE_BIT)
  295. (*info->fprintf_func) (info->stream, "%s,%s,%d",
  296. minfop->reg_name[x], minfop->reg_name[y], z);
  297. else
  298. (*info->fprintf_func) (info->stream, "%s,%s,%s",
  299. minfop->reg_name[x],
  300. minfop->reg_name[y],
  301. minfop->reg_name[z]);
  302. break;
  303. case mmix_operands_jmp:
  304. /* Address; only JMP. */
  305. {
  306. bfd_signed_vma offset = (x * 65536 + y * 256 + z) * 4;
  307. if (insn & INSN_BACKWARD_OFFSET_BIT)
  308. offset -= (256 * 65536) * 4;
  309. info->target = memaddr + offset;
  310. (*info->print_address_func) (memaddr + offset, info);
  311. }
  312. break;
  313. case mmix_operands_roundregs_z:
  314. /* Two registers, like FLOT, possibly with rounding: "$X,$Z|Z"
  315. "$X,ROUND_MODE,$Z|Z". */
  316. if (y != 0)
  317. {
  318. if (insn & INSN_IMMEDIATE_BIT)
  319. (*info->fprintf_func) (info->stream, "%s,%s,%d",
  320. minfop->reg_name[x],
  321. ROUND_MODE (y), z);
  322. else
  323. (*info->fprintf_func) (info->stream, "%s,%s,%s",
  324. minfop->reg_name[x],
  325. ROUND_MODE (y),
  326. minfop->reg_name[z]);
  327. }
  328. else
  329. {
  330. if (insn & INSN_IMMEDIATE_BIT)
  331. (*info->fprintf_func) (info->stream, "%s,%d",
  332. minfop->reg_name[x], z);
  333. else
  334. (*info->fprintf_func) (info->stream, "%s,%s",
  335. minfop->reg_name[x],
  336. minfop->reg_name[z]);
  337. }
  338. break;
  339. case mmix_operands_pop:
  340. /* Like POP - "X,YZ". */
  341. (*info->fprintf_func) (info->stream, "%d,%d", x, y*256 + z);
  342. break;
  343. case mmix_operands_roundregs:
  344. /* Two registers, possibly with rounding: "$X,$Z" or
  345. "$X,ROUND_MODE,$Z". */
  346. if (y != 0)
  347. (*info->fprintf_func) (info->stream, "%s,%s,%s",
  348. minfop->reg_name[x],
  349. ROUND_MODE (y),
  350. minfop->reg_name[z]);
  351. else
  352. (*info->fprintf_func) (info->stream, "%s,%s",
  353. minfop->reg_name[x],
  354. minfop->reg_name[z]);
  355. break;
  356. case mmix_operands_sync:
  357. /* Like SYNC - "XYZ". */
  358. (*info->fprintf_func) (info->stream, "%u",
  359. x * 65536 + y * 256 + z);
  360. break;
  361. case mmix_operands_x_regs_z:
  362. /* Like SYNCD - "X,$Y,$Z|Z". */
  363. if (insn & INSN_IMMEDIATE_BIT)
  364. (*info->fprintf_func) (info->stream, "%d,%s,%d",
  365. x, minfop->reg_name[y], z);
  366. else
  367. (*info->fprintf_func) (info->stream, "%d,%s,%s",
  368. x, minfop->reg_name[y],
  369. minfop->reg_name[z]);
  370. break;
  371. case mmix_operands_neg:
  372. /* Like NEG and NEGU - "$X,Y,$Z|Z". */
  373. if (insn & INSN_IMMEDIATE_BIT)
  374. (*info->fprintf_func) (info->stream, "%s,%d,%d",
  375. minfop->reg_name[x], y, z);
  376. else
  377. (*info->fprintf_func) (info->stream, "%s,%d,%s",
  378. minfop->reg_name[x], y,
  379. minfop->reg_name[z]);
  380. break;
  381. case mmix_operands_pushj:
  382. case mmix_operands_regaddr:
  383. /* Like GETA or branches - "$X,Address". */
  384. {
  385. bfd_signed_vma offset = (y * 256 + z) * 4;
  386. if (insn & INSN_BACKWARD_OFFSET_BIT)
  387. offset -= 65536 * 4;
  388. info->target = memaddr + offset;
  389. (*info->fprintf_func) (info->stream, "%s,", minfop->reg_name[x]);
  390. (*info->print_address_func) (memaddr + offset, info);
  391. }
  392. break;
  393. case mmix_operands_get:
  394. /* GET - "X,spec_reg". */
  395. (*info->fprintf_func) (info->stream, "%s,%s",
  396. minfop->reg_name[x],
  397. minfop->spec_reg_name[z]);
  398. break;
  399. case mmix_operands_put:
  400. /* PUT - "spec_reg,$Z|Z". */
  401. if (insn & INSN_IMMEDIATE_BIT)
  402. (*info->fprintf_func) (info->stream, "%s,%d",
  403. minfop->spec_reg_name[x], z);
  404. else
  405. (*info->fprintf_func) (info->stream, "%s,%s",
  406. minfop->spec_reg_name[x],
  407. minfop->reg_name[z]);
  408. break;
  409. case mmix_operands_set:
  410. /* Two registers, "$X,$Y". */
  411. (*info->fprintf_func) (info->stream, "%s,%s",
  412. minfop->reg_name[x],
  413. minfop->reg_name[y]);
  414. break;
  415. case mmix_operands_save:
  416. /* SAVE - "$X,0". */
  417. (*info->fprintf_func) (info->stream, "%s,0", minfop->reg_name[x]);
  418. break;
  419. case mmix_operands_unsave:
  420. /* UNSAVE - "0,$Z". */
  421. (*info->fprintf_func) (info->stream, "0,%s", minfop->reg_name[z]);
  422. break;
  423. case mmix_operands_xyz_opt:
  424. /* Like SWYM or TRAP - "X,Y,Z". */
  425. (*info->fprintf_func) (info->stream, "%d,%d,%d", x, y, z);
  426. break;
  427. case mmix_operands_resume:
  428. /* Just "Z", like RESUME. */
  429. (*info->fprintf_func) (info->stream, "%d", z);
  430. break;
  431. default:
  432. (*info->fprintf_func) (info->stream, _("*unknown operands type: %d*"),
  433. opcodep->operands);
  434. break;
  435. }
  436. return 4;
  437. }