ns32k-dis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /* Print National Semiconductor 32000 instructions.
  2. Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "dis-asm.h"
  19. #if !defined(const) && !defined(__STDC__)
  20. #define const
  21. #endif
  22. #include "opcode/ns32k.h"
  23. #include "opintl.h"
  24. static disassemble_info *dis_info;
  25. /* Hacks to get it to compile <= READ THESE AS FIXES NEEDED. */
  26. #define INVALID_FLOAT(val, size) invalid_float ((bfd_byte *) val, size)
  27. static long
  28. read_memory_integer (unsigned char * addr, int nr)
  29. {
  30. long val;
  31. int i;
  32. for (val = 0, i = nr - 1; i >= 0; i--)
  33. {
  34. val = (val << 8);
  35. val |= (0xff & *(addr + i));
  36. }
  37. return val;
  38. }
  39. /* 32000 instructions are never longer than this. */
  40. #define MAXLEN 62
  41. #include <setjmp.h>
  42. struct private
  43. {
  44. /* Points to first byte not fetched. */
  45. bfd_byte *max_fetched;
  46. bfd_byte the_buffer[MAXLEN];
  47. bfd_vma insn_start;
  48. OPCODES_SIGJMP_BUF bailout;
  49. };
  50. /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
  51. to ADDR (exclusive) are valid. Returns 1 for success, longjmps
  52. on error. */
  53. #define FETCH_DATA(info, addr) \
  54. ((addr) <= ((struct private *)(info->private_data))->max_fetched \
  55. ? 1 : fetch_data ((info), (addr)))
  56. static int
  57. fetch_data (struct disassemble_info *info, bfd_byte *addr)
  58. {
  59. int status;
  60. struct private *priv = (struct private *) info->private_data;
  61. bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
  62. status = (*info->read_memory_func) (start,
  63. priv->max_fetched,
  64. addr - priv->max_fetched,
  65. info);
  66. if (status != 0)
  67. {
  68. (*info->memory_error_func) (status, start, info);
  69. OPCODES_SIGLONGJMP (priv->bailout, 1);
  70. }
  71. else
  72. priv->max_fetched = addr;
  73. return 1;
  74. }
  75. /* Number of elements in the opcode table. */
  76. #define NOPCODES (sizeof ns32k_opcodes / sizeof ns32k_opcodes[0])
  77. #define NEXT_IS_ADDR '|'
  78. struct ns32k_option
  79. {
  80. char *pattern; /* The option itself. */
  81. unsigned long value; /* Binary value of the option. */
  82. unsigned long match; /* These bits must match. */
  83. };
  84. static const struct ns32k_option opt_u[]= /* Restore, exit. */
  85. {
  86. { "r0", 0x80, 0x80 },
  87. { "r1", 0x40, 0x40 },
  88. { "r2", 0x20, 0x20 },
  89. { "r3", 0x10, 0x10 },
  90. { "r4", 0x08, 0x08 },
  91. { "r5", 0x04, 0x04 },
  92. { "r6", 0x02, 0x02 },
  93. { "r7", 0x01, 0x01 },
  94. { 0 , 0x00, 0x00 }
  95. };
  96. static const struct ns32k_option opt_U[]= /* Save, enter. */
  97. {
  98. { "r0", 0x01, 0x01 },
  99. { "r1", 0x02, 0x02 },
  100. { "r2", 0x04, 0x04 },
  101. { "r3", 0x08, 0x08 },
  102. { "r4", 0x10, 0x10 },
  103. { "r5", 0x20, 0x20 },
  104. { "r6", 0x40, 0x40 },
  105. { "r7", 0x80, 0x80 },
  106. { 0 , 0x00, 0x00 }
  107. };
  108. static const struct ns32k_option opt_O[]= /* Setcfg. */
  109. {
  110. { "c", 0x8, 0x8 },
  111. { "m", 0x4, 0x4 },
  112. { "f", 0x2, 0x2 },
  113. { "i", 0x1, 0x1 },
  114. { 0 , 0x0, 0x0 }
  115. };
  116. static const struct ns32k_option opt_C[]= /* Cinv. */
  117. {
  118. { "a", 0x4, 0x4 },
  119. { "i", 0x2, 0x2 },
  120. { "d", 0x1, 0x1 },
  121. { 0 , 0x0, 0x0 }
  122. };
  123. static const struct ns32k_option opt_S[]= /* String inst. */
  124. {
  125. { "b", 0x1, 0x1 },
  126. { "u", 0x6, 0x6 },
  127. { "w", 0x2, 0x2 },
  128. { 0 , 0x0, 0x0 }
  129. };
  130. static const struct ns32k_option list_P532[]= /* Lpr spr. */
  131. {
  132. { "us", 0x0, 0xf },
  133. { "dcr", 0x1, 0xf },
  134. { "bpc", 0x2, 0xf },
  135. { "dsr", 0x3, 0xf },
  136. { "car", 0x4, 0xf },
  137. { "fp", 0x8, 0xf },
  138. { "sp", 0x9, 0xf },
  139. { "sb", 0xa, 0xf },
  140. { "usp", 0xb, 0xf },
  141. { "cfg", 0xc, 0xf },
  142. { "psr", 0xd, 0xf },
  143. { "intbase", 0xe, 0xf },
  144. { "mod", 0xf, 0xf },
  145. { 0 , 0x00, 0xf }
  146. };
  147. static const struct ns32k_option list_M532[]= /* Lmr smr. */
  148. {
  149. { "mcr", 0x9, 0xf },
  150. { "msr", 0xa, 0xf },
  151. { "tear", 0xb, 0xf },
  152. { "ptb0", 0xc, 0xf },
  153. { "ptb1", 0xd, 0xf },
  154. { "ivar0", 0xe, 0xf },
  155. { "ivar1", 0xf, 0xf },
  156. { 0 , 0x0, 0xf }
  157. };
  158. static const struct ns32k_option list_P032[]= /* Lpr spr. */
  159. {
  160. { "upsr", 0x0, 0xf },
  161. { "fp", 0x8, 0xf },
  162. { "sp", 0x9, 0xf },
  163. { "sb", 0xa, 0xf },
  164. { "psr", 0xb, 0xf },
  165. { "intbase", 0xe, 0xf },
  166. { "mod", 0xf, 0xf },
  167. { 0 , 0x0, 0xf }
  168. };
  169. static const struct ns32k_option list_M032[]= /* Lmr smr. */
  170. {
  171. { "bpr0", 0x0, 0xf },
  172. { "bpr1", 0x1, 0xf },
  173. { "pf0", 0x4, 0xf },
  174. { "pf1", 0x5, 0xf },
  175. { "sc", 0x8, 0xf },
  176. { "msr", 0xa, 0xf },
  177. { "bcnt", 0xb, 0xf },
  178. { "ptb0", 0xc, 0xf },
  179. { "ptb1", 0xd, 0xf },
  180. { "eia", 0xf, 0xf },
  181. { 0 , 0x0, 0xf }
  182. };
  183. /* Figure out which options are present. */
  184. static void
  185. optlist (int options, const struct ns32k_option * optionP, char * result)
  186. {
  187. if (options == 0)
  188. {
  189. sprintf (result, "[]");
  190. return;
  191. }
  192. sprintf (result, "[");
  193. for (; (options != 0) && optionP->pattern; optionP++)
  194. {
  195. if ((options & optionP->match) == optionP->value)
  196. {
  197. /* We found a match, update result and options. */
  198. strcat (result, optionP->pattern);
  199. options &= ~optionP->value;
  200. if (options != 0) /* More options to come. */
  201. strcat (result, ",");
  202. }
  203. }
  204. if (options != 0)
  205. strcat (result, "undefined");
  206. strcat (result, "]");
  207. }
  208. static void
  209. list_search (int reg_value, const struct ns32k_option *optionP, char *result)
  210. {
  211. for (; optionP->pattern; optionP++)
  212. {
  213. if ((reg_value & optionP->match) == optionP->value)
  214. {
  215. sprintf (result, "%s", optionP->pattern);
  216. return;
  217. }
  218. }
  219. sprintf (result, "undefined");
  220. }
  221. /* Extract "count" bits starting "offset" bits into buffer. */
  222. static int
  223. bit_extract (bfd_byte *buffer, int offset, int count)
  224. {
  225. int result;
  226. int bit;
  227. buffer += offset >> 3;
  228. offset &= 7;
  229. bit = 1;
  230. result = 0;
  231. while (count--)
  232. {
  233. FETCH_DATA (dis_info, buffer + 1);
  234. if ((*buffer & (1 << offset)))
  235. result |= bit;
  236. if (++offset == 8)
  237. {
  238. offset = 0;
  239. buffer++;
  240. }
  241. bit <<= 1;
  242. }
  243. return result;
  244. }
  245. /* Like bit extract but the buffer is valid and doen't need to be fetched. */
  246. static int
  247. bit_extract_simple (bfd_byte *buffer, int offset, int count)
  248. {
  249. int result;
  250. int bit;
  251. buffer += offset >> 3;
  252. offset &= 7;
  253. bit = 1;
  254. result = 0;
  255. while (count--)
  256. {
  257. if ((*buffer & (1 << offset)))
  258. result |= bit;
  259. if (++offset == 8)
  260. {
  261. offset = 0;
  262. buffer++;
  263. }
  264. bit <<= 1;
  265. }
  266. return result;
  267. }
  268. static void
  269. bit_copy (bfd_byte *buffer, int offset, int count, char *to)
  270. {
  271. for (; count > 8; count -= 8, to++, offset += 8)
  272. *to = bit_extract (buffer, offset, 8);
  273. *to = bit_extract (buffer, offset, count);
  274. }
  275. static int
  276. sign_extend (int value, int bits)
  277. {
  278. value = value & ((1 << bits) - 1);
  279. return (value & (1 << (bits - 1))
  280. ? value | (~((1 << bits) - 1))
  281. : value);
  282. }
  283. static void
  284. flip_bytes (char *ptr, int count)
  285. {
  286. char tmp;
  287. while (count > 0)
  288. {
  289. tmp = ptr[0];
  290. ptr[0] = ptr[count - 1];
  291. ptr[count - 1] = tmp;
  292. ptr++;
  293. count -= 2;
  294. }
  295. }
  296. /* Given a character C, does it represent a general addressing mode? */
  297. #define Is_gen(c) \
  298. ((c) == 'F' || (c) == 'L' || (c) == 'B' \
  299. || (c) == 'W' || (c) == 'D' || (c) == 'A' || (c) == 'I' || (c) == 'Z')
  300. /* Adressing modes. */
  301. #define Adrmod_index_byte 0x1c
  302. #define Adrmod_index_word 0x1d
  303. #define Adrmod_index_doubleword 0x1e
  304. #define Adrmod_index_quadword 0x1f
  305. /* Is MODE an indexed addressing mode? */
  306. #define Adrmod_is_index(mode) \
  307. ( mode == Adrmod_index_byte \
  308. || mode == Adrmod_index_word \
  309. || mode == Adrmod_index_doubleword \
  310. || mode == Adrmod_index_quadword)
  311. static int
  312. get_displacement (bfd_byte *buffer, int *aoffsetp)
  313. {
  314. int Ivalue;
  315. short Ivalue2;
  316. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  317. switch (Ivalue & 0xc0)
  318. {
  319. case 0x00:
  320. case 0x40:
  321. Ivalue = sign_extend (Ivalue, 7);
  322. *aoffsetp += 8;
  323. break;
  324. case 0x80:
  325. Ivalue2 = bit_extract (buffer, *aoffsetp, 16);
  326. flip_bytes ((char *) & Ivalue2, 2);
  327. Ivalue = sign_extend (Ivalue2, 14);
  328. *aoffsetp += 16;
  329. break;
  330. case 0xc0:
  331. Ivalue = bit_extract (buffer, *aoffsetp, 32);
  332. flip_bytes ((char *) & Ivalue, 4);
  333. Ivalue = sign_extend (Ivalue, 30);
  334. *aoffsetp += 32;
  335. break;
  336. }
  337. return Ivalue;
  338. }
  339. #if 1 /* A version that should work on ns32k f's&d's on any machine. */
  340. static int
  341. invalid_float (bfd_byte *p, int len)
  342. {
  343. int val;
  344. if (len == 4)
  345. val = (bit_extract_simple (p, 23, 8)/*exponent*/ == 0xff
  346. || (bit_extract_simple (p, 23, 8)/*exponent*/ == 0
  347. && bit_extract_simple (p, 0, 23)/*mantisa*/ != 0));
  348. else if (len == 8)
  349. val = (bit_extract_simple (p, 52, 11)/*exponent*/ == 0x7ff
  350. || (bit_extract_simple (p, 52, 11)/*exponent*/ == 0
  351. && (bit_extract_simple (p, 0, 32)/*low mantisa*/ != 0
  352. || bit_extract_simple (p, 32, 20)/*high mantisa*/ != 0)));
  353. else
  354. val = 1;
  355. return (val);
  356. }
  357. #else
  358. /* Assumes the bytes have been swapped to local order. */
  359. typedef union
  360. {
  361. double d;
  362. float f;
  363. struct { unsigned m:23, e:8, :1;} sf;
  364. struct { unsigned lm; unsigned m:20, e:11, :1;} sd;
  365. } float_type_u;
  366. static int
  367. invalid_float (float_type_u *p, int len)
  368. {
  369. int val;
  370. if (len == sizeof (float))
  371. val = (p->sf.e == 0xff
  372. || (p->sf.e == 0 && p->sf.m != 0));
  373. else if (len == sizeof (double))
  374. val = (p->sd.e == 0x7ff
  375. || (p->sd.e == 0 && (p->sd.m != 0 || p->sd.lm != 0)));
  376. else
  377. val = 1;
  378. return val;
  379. }
  380. #endif
  381. /* Print an instruction operand of category given by d. IOFFSET is
  382. the bit position below which small (<1 byte) parts of the operand can
  383. be found (usually in the basic instruction, but for indexed
  384. addressing it can be in the index byte). AOFFSETP is a pointer to the
  385. bit position of the addressing extension. BUFFER contains the
  386. instruction. ADDR is where BUFFER was read from. Put the disassembled
  387. version of the operand in RESULT. INDEX_OFFSET is the bit position
  388. of the index byte (it contains garbage if this operand is not a
  389. general operand using scaled indexed addressing mode). */
  390. static int
  391. print_insn_arg (int d,
  392. int ioffset,
  393. int *aoffsetp,
  394. bfd_byte *buffer,
  395. bfd_vma addr,
  396. char *result,
  397. int index_offset)
  398. {
  399. union
  400. {
  401. float f;
  402. double d;
  403. int i[2];
  404. } value;
  405. int Ivalue;
  406. int addr_mode;
  407. int disp1, disp2;
  408. int size;
  409. switch (d)
  410. {
  411. case 'f':
  412. /* A "gen" operand but 5 bits from the end of instruction. */
  413. ioffset -= 5;
  414. case 'Z':
  415. case 'F':
  416. case 'L':
  417. case 'I':
  418. case 'B':
  419. case 'W':
  420. case 'D':
  421. case 'A':
  422. addr_mode = bit_extract (buffer, ioffset - 5, 5);
  423. ioffset -= 5;
  424. switch (addr_mode)
  425. {
  426. case 0x0: case 0x1: case 0x2: case 0x3:
  427. case 0x4: case 0x5: case 0x6: case 0x7:
  428. /* Register mode R0 -- R7. */
  429. switch (d)
  430. {
  431. case 'F':
  432. case 'L':
  433. case 'Z':
  434. sprintf (result, "f%d", addr_mode);
  435. break;
  436. default:
  437. sprintf (result, "r%d", addr_mode);
  438. }
  439. break;
  440. case 0x8: case 0x9: case 0xa: case 0xb:
  441. case 0xc: case 0xd: case 0xe: case 0xf:
  442. /* Register relative disp(R0 -- R7). */
  443. disp1 = get_displacement (buffer, aoffsetp);
  444. sprintf (result, "%d(r%d)", disp1, addr_mode & 7);
  445. break;
  446. case 0x10:
  447. case 0x11:
  448. case 0x12:
  449. /* Memory relative disp2(disp1(FP, SP, SB)). */
  450. disp1 = get_displacement (buffer, aoffsetp);
  451. disp2 = get_displacement (buffer, aoffsetp);
  452. sprintf (result, "%d(%d(%s))", disp2, disp1,
  453. addr_mode == 0x10 ? "fp" : addr_mode == 0x11 ? "sp" : "sb");
  454. break;
  455. case 0x13:
  456. /* Reserved. */
  457. sprintf (result, "reserved");
  458. break;
  459. case 0x14:
  460. /* Immediate. */
  461. switch (d)
  462. {
  463. case 'I':
  464. case 'Z':
  465. case 'A':
  466. /* I and Z are output operands and can`t be immediate
  467. A is an address and we can`t have the address of
  468. an immediate either. We don't know how much to increase
  469. aoffsetp by since whatever generated this is broken
  470. anyway! */
  471. sprintf (result, _("$<undefined>"));
  472. break;
  473. case 'B':
  474. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  475. Ivalue = sign_extend (Ivalue, 8);
  476. *aoffsetp += 8;
  477. sprintf (result, "$%d", Ivalue);
  478. break;
  479. case 'W':
  480. Ivalue = bit_extract (buffer, *aoffsetp, 16);
  481. flip_bytes ((char *) & Ivalue, 2);
  482. *aoffsetp += 16;
  483. Ivalue = sign_extend (Ivalue, 16);
  484. sprintf (result, "$%d", Ivalue);
  485. break;
  486. case 'D':
  487. Ivalue = bit_extract (buffer, *aoffsetp, 32);
  488. flip_bytes ((char *) & Ivalue, 4);
  489. *aoffsetp += 32;
  490. sprintf (result, "$%d", Ivalue);
  491. break;
  492. case 'F':
  493. bit_copy (buffer, *aoffsetp, 32, (char *) &value.f);
  494. flip_bytes ((char *) &value.f, 4);
  495. *aoffsetp += 32;
  496. if (INVALID_FLOAT (&value.f, 4))
  497. sprintf (result, "<<invalid float 0x%.8x>>", value.i[0]);
  498. else /* Assume host has ieee float. */
  499. sprintf (result, "$%g", value.f);
  500. break;
  501. case 'L':
  502. bit_copy (buffer, *aoffsetp, 64, (char *) &value.d);
  503. flip_bytes ((char *) &value.d, 8);
  504. *aoffsetp += 64;
  505. if (INVALID_FLOAT (&value.d, 8))
  506. sprintf (result, "<<invalid double 0x%.8x%.8x>>",
  507. value.i[1], value.i[0]);
  508. else /* Assume host has ieee float. */
  509. sprintf (result, "$%g", value.d);
  510. break;
  511. }
  512. break;
  513. case 0x15:
  514. /* Absolute @disp. */
  515. disp1 = get_displacement (buffer, aoffsetp);
  516. sprintf (result, "@|%d|", disp1);
  517. break;
  518. case 0x16:
  519. /* External EXT(disp1) + disp2 (Mod table stuff). */
  520. disp1 = get_displacement (buffer, aoffsetp);
  521. disp2 = get_displacement (buffer, aoffsetp);
  522. sprintf (result, "EXT(%d) + %d", disp1, disp2);
  523. break;
  524. case 0x17:
  525. /* Top of stack tos. */
  526. sprintf (result, "tos");
  527. break;
  528. case 0x18:
  529. /* Memory space disp(FP). */
  530. disp1 = get_displacement (buffer, aoffsetp);
  531. sprintf (result, "%d(fp)", disp1);
  532. break;
  533. case 0x19:
  534. /* Memory space disp(SP). */
  535. disp1 = get_displacement (buffer, aoffsetp);
  536. sprintf (result, "%d(sp)", disp1);
  537. break;
  538. case 0x1a:
  539. /* Memory space disp(SB). */
  540. disp1 = get_displacement (buffer, aoffsetp);
  541. sprintf (result, "%d(sb)", disp1);
  542. break;
  543. case 0x1b:
  544. /* Memory space disp(PC). */
  545. disp1 = get_displacement (buffer, aoffsetp);
  546. *result++ = NEXT_IS_ADDR;
  547. sprintf_vma (result, addr + disp1);
  548. result += strlen (result);
  549. *result++ = NEXT_IS_ADDR;
  550. *result = '\0';
  551. break;
  552. case 0x1c:
  553. case 0x1d:
  554. case 0x1e:
  555. case 0x1f:
  556. {
  557. int bit_index;
  558. static const char *ind = "bwdq";
  559. char *off;
  560. /* Scaled index basemode[R0 -- R7:B,W,D,Q]. */
  561. bit_index = bit_extract (buffer, index_offset - 8, 3);
  562. print_insn_arg (d, index_offset, aoffsetp, buffer, addr,
  563. result, 0);
  564. off = result + strlen (result);
  565. sprintf (off, "[r%d:%c]", bit_index, ind[addr_mode & 3]);
  566. }
  567. break;
  568. }
  569. break;
  570. case 'H':
  571. case 'q':
  572. Ivalue = bit_extract (buffer, ioffset-4, 4);
  573. Ivalue = sign_extend (Ivalue, 4);
  574. sprintf (result, "%d", Ivalue);
  575. ioffset -= 4;
  576. break;
  577. case 'r':
  578. Ivalue = bit_extract (buffer, ioffset-3, 3);
  579. sprintf (result, "r%d", Ivalue&7);
  580. ioffset -= 3;
  581. break;
  582. case 'd':
  583. sprintf (result, "%d", get_displacement (buffer, aoffsetp));
  584. break;
  585. case 'b':
  586. Ivalue = get_displacement (buffer, aoffsetp);
  587. /* Warning!! HACK ALERT!
  588. Operand type 'b' is only used by the cmp{b,w,d} and
  589. movm{b,w,d} instructions; we need to know whether
  590. it's a `b' or `w' or `d' instruction; and for both
  591. cmpm and movm it's stored at the same place so we
  592. just grab two bits of the opcode and look at it... */
  593. size = bit_extract(buffer, ioffset-6, 2);
  594. if (size == 0) /* 00 => b. */
  595. size = 1;
  596. else if (size == 1) /* 01 => w. */
  597. size = 2;
  598. else
  599. size = 4; /* 11 => d. */
  600. sprintf (result, "%d", (Ivalue / size) + 1);
  601. break;
  602. case 'p':
  603. *result++ = NEXT_IS_ADDR;
  604. sprintf_vma (result, addr + get_displacement (buffer, aoffsetp));
  605. result += strlen (result);
  606. *result++ = NEXT_IS_ADDR;
  607. *result = '\0';
  608. break;
  609. case 'i':
  610. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  611. *aoffsetp += 8;
  612. sprintf (result, "0x%x", Ivalue);
  613. break;
  614. case 'u':
  615. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  616. optlist (Ivalue, opt_u, result);
  617. *aoffsetp += 8;
  618. break;
  619. case 'U':
  620. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  621. optlist (Ivalue, opt_U, result);
  622. *aoffsetp += 8;
  623. break;
  624. case 'O':
  625. Ivalue = bit_extract (buffer, ioffset - 9, 9);
  626. optlist (Ivalue, opt_O, result);
  627. ioffset -= 9;
  628. break;
  629. case 'C':
  630. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  631. optlist (Ivalue, opt_C, result);
  632. ioffset -= 4;
  633. break;
  634. case 'S':
  635. Ivalue = bit_extract (buffer, ioffset - 8, 8);
  636. optlist (Ivalue, opt_S, result);
  637. ioffset -= 8;
  638. break;
  639. case 'M':
  640. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  641. list_search (Ivalue, 0 ? list_M032 : list_M532, result);
  642. ioffset -= 4;
  643. break;
  644. case 'P':
  645. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  646. list_search (Ivalue, 0 ? list_P032 : list_P532, result);
  647. ioffset -= 4;
  648. break;
  649. case 'g':
  650. Ivalue = bit_extract (buffer, *aoffsetp, 3);
  651. sprintf (result, "%d", Ivalue);
  652. *aoffsetp += 3;
  653. break;
  654. case 'G':
  655. Ivalue = bit_extract(buffer, *aoffsetp, 5);
  656. sprintf (result, "%d", Ivalue + 1);
  657. *aoffsetp += 5;
  658. break;
  659. }
  660. return ioffset;
  661. }
  662. /* Print the 32000 instruction at address MEMADDR in debugged memory,
  663. on STREAM. Returns length of the instruction, in bytes. */
  664. int
  665. print_insn_ns32k (bfd_vma memaddr, disassemble_info *info)
  666. {
  667. unsigned int i;
  668. const char *d;
  669. unsigned short first_word;
  670. int ioffset; /* Bits into instruction. */
  671. int aoffset; /* Bits into arguments. */
  672. char arg_bufs[MAX_ARGS+1][ARG_LEN];
  673. int argnum;
  674. int maxarg;
  675. struct private priv;
  676. bfd_byte *buffer = priv.the_buffer;
  677. dis_info = info;
  678. info->private_data = & priv;
  679. priv.max_fetched = priv.the_buffer;
  680. priv.insn_start = memaddr;
  681. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  682. /* Error return. */
  683. return -1;
  684. /* Look for 8bit opcodes first. Other wise, fetching two bytes could take
  685. us over the end of accessible data unnecessarilly. */
  686. FETCH_DATA (info, buffer + 1);
  687. for (i = 0; i < NOPCODES; i++)
  688. if (ns32k_opcodes[i].opcode_id_size <= 8
  689. && ((buffer[0]
  690. & (((unsigned long) 1 << ns32k_opcodes[i].opcode_id_size) - 1))
  691. == ns32k_opcodes[i].opcode_seed))
  692. break;
  693. if (i == NOPCODES)
  694. {
  695. /* Maybe it is 9 to 16 bits big. */
  696. FETCH_DATA (info, buffer + 2);
  697. first_word = read_memory_integer(buffer, 2);
  698. for (i = 0; i < NOPCODES; i++)
  699. if ((first_word
  700. & (((unsigned long) 1 << ns32k_opcodes[i].opcode_id_size) - 1))
  701. == ns32k_opcodes[i].opcode_seed)
  702. break;
  703. /* Handle undefined instructions. */
  704. if (i == NOPCODES)
  705. {
  706. (*dis_info->fprintf_func)(dis_info->stream, "0%o", buffer[0]);
  707. return 1;
  708. }
  709. }
  710. (*dis_info->fprintf_func)(dis_info->stream, "%s", ns32k_opcodes[i].name);
  711. ioffset = ns32k_opcodes[i].opcode_size;
  712. aoffset = ns32k_opcodes[i].opcode_size;
  713. d = ns32k_opcodes[i].operands;
  714. if (*d)
  715. {
  716. /* Offset in bits of the first thing beyond each index byte.
  717. Element 0 is for operand A and element 1 is for operand B.
  718. The rest are irrelevant, but we put them here so we don't
  719. index outside the array. */
  720. int index_offset[MAX_ARGS];
  721. /* 0 for operand A, 1 for operand B, greater for other args. */
  722. int whicharg = 0;
  723. (*dis_info->fprintf_func)(dis_info->stream, "\t");
  724. maxarg = 0;
  725. /* First we have to find and keep track of the index bytes,
  726. if we are using scaled indexed addressing mode, since the index
  727. bytes occur right after the basic instruction, not as part
  728. of the addressing extension. */
  729. if (Is_gen(d[1]))
  730. {
  731. int addr_mode = bit_extract (buffer, ioffset - 5, 5);
  732. if (Adrmod_is_index (addr_mode))
  733. {
  734. aoffset += 8;
  735. index_offset[0] = aoffset;
  736. }
  737. }
  738. if (d[2] && Is_gen(d[3]))
  739. {
  740. int addr_mode = bit_extract (buffer, ioffset - 10, 5);
  741. if (Adrmod_is_index (addr_mode))
  742. {
  743. aoffset += 8;
  744. index_offset[1] = aoffset;
  745. }
  746. }
  747. while (*d)
  748. {
  749. argnum = *d - '1';
  750. d++;
  751. if (argnum > maxarg && argnum < MAX_ARGS)
  752. maxarg = argnum;
  753. ioffset = print_insn_arg (*d, ioffset, &aoffset, buffer,
  754. memaddr, arg_bufs[argnum],
  755. index_offset[whicharg]);
  756. d++;
  757. whicharg++;
  758. }
  759. for (argnum = 0; argnum <= maxarg; argnum++)
  760. {
  761. bfd_vma addr;
  762. char *ch;
  763. for (ch = arg_bufs[argnum]; *ch;)
  764. {
  765. if (*ch == NEXT_IS_ADDR)
  766. {
  767. ++ch;
  768. addr = bfd_scan_vma (ch, NULL, 16);
  769. (*dis_info->print_address_func) (addr, dis_info);
  770. while (*ch && *ch != NEXT_IS_ADDR)
  771. ++ch;
  772. if (*ch)
  773. ++ch;
  774. }
  775. else
  776. (*dis_info->fprintf_func)(dis_info->stream, "%c", *ch++);
  777. }
  778. if (argnum < maxarg)
  779. (*dis_info->fprintf_func)(dis_info->stream, ", ");
  780. }
  781. }
  782. return aoffset / 8;
  783. }