mmix-dis.c 15 KB

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