ns32k-dis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* Print National Semiconductor 32000 instructions.
  2. Copyright (C) 1986-2022 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 "disassemble.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. unsigned int result;
  226. unsigned int bit;
  227. if (offset < 0 || count < 0)
  228. return 0;
  229. buffer += offset >> 3;
  230. offset &= 7;
  231. bit = 1;
  232. result = 0;
  233. while (count--)
  234. {
  235. FETCH_DATA (dis_info, buffer + 1);
  236. if ((*buffer & (1 << offset)))
  237. result |= bit;
  238. if (++offset == 8)
  239. {
  240. offset = 0;
  241. buffer++;
  242. }
  243. bit <<= 1;
  244. }
  245. return result;
  246. }
  247. /* Like bit extract but the buffer is valid and doen't need to be fetched. */
  248. static int
  249. bit_extract_simple (bfd_byte *buffer, int offset, int count)
  250. {
  251. unsigned int result;
  252. unsigned int bit;
  253. if (offset < 0 || count < 0)
  254. return 0;
  255. buffer += offset >> 3;
  256. offset &= 7;
  257. bit = 1;
  258. result = 0;
  259. while (count--)
  260. {
  261. if ((*buffer & (1 << offset)))
  262. result |= bit;
  263. if (++offset == 8)
  264. {
  265. offset = 0;
  266. buffer++;
  267. }
  268. bit <<= 1;
  269. }
  270. return result;
  271. }
  272. static void
  273. bit_copy (bfd_byte *buffer, int offset, int count, char *to)
  274. {
  275. if (offset < 0 || count < 0)
  276. return;
  277. for (; count > 8; count -= 8, to++, offset += 8)
  278. *to = bit_extract (buffer, offset, 8);
  279. *to = bit_extract (buffer, offset, count);
  280. }
  281. static int
  282. sign_extend (unsigned int value, unsigned int bits)
  283. {
  284. unsigned int sign = 1u << (bits - 1);
  285. return ((value & (sign + sign - 1)) ^ sign) - sign;
  286. }
  287. static void
  288. flip_bytes (char *ptr, int count)
  289. {
  290. char tmp;
  291. while (count > 0)
  292. {
  293. tmp = ptr[0];
  294. ptr[0] = ptr[count - 1];
  295. ptr[count - 1] = tmp;
  296. ptr++;
  297. count -= 2;
  298. }
  299. }
  300. /* Given a character C, does it represent a general addressing mode? */
  301. #define Is_gen(c) (strchr ("FLBWDAIZf", (c)) != NULL)
  302. /* Adressing modes. */
  303. #define Adrmod_index_byte 0x1c
  304. #define Adrmod_index_word 0x1d
  305. #define Adrmod_index_doubleword 0x1e
  306. #define Adrmod_index_quadword 0x1f
  307. /* Is MODE an indexed addressing mode? */
  308. #define Adrmod_is_index(mode) \
  309. ( mode == Adrmod_index_byte \
  310. || mode == Adrmod_index_word \
  311. || mode == Adrmod_index_doubleword \
  312. || mode == Adrmod_index_quadword)
  313. static int
  314. get_displacement (bfd_byte *buffer, int *aoffsetp)
  315. {
  316. int Ivalue;
  317. short Ivalue2;
  318. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  319. switch (Ivalue & 0xc0)
  320. {
  321. case 0x00:
  322. case 0x40:
  323. Ivalue = sign_extend (Ivalue, 7);
  324. *aoffsetp += 8;
  325. break;
  326. case 0x80:
  327. Ivalue2 = bit_extract (buffer, *aoffsetp, 16);
  328. flip_bytes ((char *) & Ivalue2, 2);
  329. Ivalue = sign_extend (Ivalue2, 14);
  330. *aoffsetp += 16;
  331. break;
  332. case 0xc0:
  333. Ivalue = bit_extract (buffer, *aoffsetp, 32);
  334. flip_bytes ((char *) & Ivalue, 4);
  335. Ivalue = sign_extend (Ivalue, 30);
  336. *aoffsetp += 32;
  337. break;
  338. }
  339. return Ivalue;
  340. }
  341. #if 1 /* A version that should work on ns32k f's&d's on any machine. */
  342. static int
  343. invalid_float (bfd_byte *p, int len)
  344. {
  345. int val;
  346. if (len == 4)
  347. val = (bit_extract_simple (p, 23, 8)/*exponent*/ == 0xff
  348. || (bit_extract_simple (p, 23, 8)/*exponent*/ == 0
  349. && bit_extract_simple (p, 0, 23)/*mantisa*/ != 0));
  350. else if (len == 8)
  351. val = (bit_extract_simple (p, 52, 11)/*exponent*/ == 0x7ff
  352. || (bit_extract_simple (p, 52, 11)/*exponent*/ == 0
  353. && (bit_extract_simple (p, 0, 32)/*low mantisa*/ != 0
  354. || bit_extract_simple (p, 32, 20)/*high mantisa*/ != 0)));
  355. else
  356. val = 1;
  357. return (val);
  358. }
  359. #else
  360. /* Assumes the bytes have been swapped to local order. */
  361. typedef union
  362. {
  363. double d;
  364. float f;
  365. struct { unsigned m:23, e:8, :1;} sf;
  366. struct { unsigned lm; unsigned m:20, e:11, :1;} sd;
  367. } float_type_u;
  368. static int
  369. invalid_float (float_type_u *p, int len)
  370. {
  371. int val;
  372. if (len == sizeof (float))
  373. val = (p->sf.e == 0xff
  374. || (p->sf.e == 0 && p->sf.m != 0));
  375. else if (len == sizeof (double))
  376. val = (p->sd.e == 0x7ff
  377. || (p->sd.e == 0 && (p->sd.m != 0 || p->sd.lm != 0)));
  378. else
  379. val = 1;
  380. return val;
  381. }
  382. #endif
  383. /* Print an instruction operand of category given by d. IOFFSET is
  384. the bit position below which small (<1 byte) parts of the operand can
  385. be found (usually in the basic instruction, but for indexed
  386. addressing it can be in the index byte). AOFFSETP is a pointer to the
  387. bit position of the addressing extension. BUFFER contains the
  388. instruction. ADDR is where BUFFER was read from. Put the disassembled
  389. version of the operand in RESULT. INDEX_OFFSET is the bit position
  390. of the index byte (it contains -1 if this operand is not a
  391. general operand using scaled indexed addressing mode). */
  392. static int
  393. print_insn_arg (int d,
  394. int ioffset,
  395. int *aoffsetp,
  396. bfd_byte *buffer,
  397. bfd_vma addr,
  398. char *result,
  399. int index_offset)
  400. {
  401. union
  402. {
  403. float f;
  404. double d;
  405. int i[2];
  406. } value;
  407. int Ivalue;
  408. int addr_mode;
  409. int disp1, disp2;
  410. int size;
  411. switch (d)
  412. {
  413. case 'f':
  414. /* A "gen" operand but 5 bits from the end of instruction. */
  415. ioffset -= 5;
  416. /* Fall through. */
  417. case 'Z':
  418. case 'F':
  419. case 'L':
  420. case 'I':
  421. case 'B':
  422. case 'W':
  423. case 'D':
  424. case 'A':
  425. addr_mode = bit_extract (buffer, ioffset - 5, 5);
  426. ioffset -= 5;
  427. switch (addr_mode)
  428. {
  429. case 0x0: case 0x1: case 0x2: case 0x3:
  430. case 0x4: case 0x5: case 0x6: case 0x7:
  431. /* Register mode R0 -- R7. */
  432. switch (d)
  433. {
  434. case 'F':
  435. case 'L':
  436. case 'Z':
  437. sprintf (result, "f%d", addr_mode);
  438. break;
  439. default:
  440. sprintf (result, "r%d", addr_mode);
  441. }
  442. break;
  443. case 0x8: case 0x9: case 0xa: case 0xb:
  444. case 0xc: case 0xd: case 0xe: case 0xf:
  445. /* Register relative disp(R0 -- R7). */
  446. disp1 = get_displacement (buffer, aoffsetp);
  447. sprintf (result, "%d(r%d)", disp1, addr_mode & 7);
  448. break;
  449. case 0x10:
  450. case 0x11:
  451. case 0x12:
  452. /* Memory relative disp2(disp1(FP, SP, SB)). */
  453. disp1 = get_displacement (buffer, aoffsetp);
  454. disp2 = get_displacement (buffer, aoffsetp);
  455. sprintf (result, "%d(%d(%s))", disp2, disp1,
  456. addr_mode == 0x10 ? "fp" : addr_mode == 0x11 ? "sp" : "sb");
  457. break;
  458. case 0x13:
  459. /* Reserved. */
  460. sprintf (result, "reserved");
  461. break;
  462. case 0x14:
  463. /* Immediate. */
  464. switch (d)
  465. {
  466. default:
  467. /* I and Z are output operands and can`t be immediate
  468. A is an address and we can`t have the address of
  469. an immediate either. We don't know how much to increase
  470. aoffsetp by since whatever generated this is broken
  471. anyway! */
  472. sprintf (result, _("$<undefined>"));
  473. break;
  474. case 'B':
  475. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  476. Ivalue = sign_extend (Ivalue, 8);
  477. *aoffsetp += 8;
  478. sprintf (result, "$%d", Ivalue);
  479. break;
  480. case 'W':
  481. Ivalue = bit_extract (buffer, *aoffsetp, 16);
  482. flip_bytes ((char *) & Ivalue, 2);
  483. *aoffsetp += 16;
  484. Ivalue = sign_extend (Ivalue, 16);
  485. sprintf (result, "$%d", Ivalue);
  486. break;
  487. case 'D':
  488. Ivalue = bit_extract (buffer, *aoffsetp, 32);
  489. flip_bytes ((char *) & Ivalue, 4);
  490. *aoffsetp += 32;
  491. sprintf (result, "$%d", Ivalue);
  492. break;
  493. case 'F':
  494. bit_copy (buffer, *aoffsetp, 32, (char *) &value.f);
  495. flip_bytes ((char *) &value.f, 4);
  496. *aoffsetp += 32;
  497. if (INVALID_FLOAT (&value.f, 4))
  498. sprintf (result, "<<invalid float 0x%.8x>>", value.i[0]);
  499. else /* Assume host has ieee float. */
  500. sprintf (result, "$%g", value.f);
  501. break;
  502. case 'L':
  503. bit_copy (buffer, *aoffsetp, 64, (char *) &value.d);
  504. flip_bytes ((char *) &value.d, 8);
  505. *aoffsetp += 64;
  506. if (INVALID_FLOAT (&value.d, 8))
  507. sprintf (result, "<<invalid double 0x%.8x%.8x>>",
  508. value.i[1], value.i[0]);
  509. else /* Assume host has ieee float. */
  510. sprintf (result, "$%g", value.d);
  511. break;
  512. }
  513. break;
  514. case 0x15:
  515. /* Absolute @disp. */
  516. disp1 = get_displacement (buffer, aoffsetp);
  517. sprintf (result, "@|%d|", disp1);
  518. break;
  519. case 0x16:
  520. /* External EXT(disp1) + disp2 (Mod table stuff). */
  521. disp1 = get_displacement (buffer, aoffsetp);
  522. disp2 = get_displacement (buffer, aoffsetp);
  523. sprintf (result, "EXT(%d) + %d", disp1, disp2);
  524. break;
  525. case 0x17:
  526. /* Top of stack tos. */
  527. sprintf (result, "tos");
  528. break;
  529. case 0x18:
  530. /* Memory space disp(FP). */
  531. disp1 = get_displacement (buffer, aoffsetp);
  532. sprintf (result, "%d(fp)", disp1);
  533. break;
  534. case 0x19:
  535. /* Memory space disp(SP). */
  536. disp1 = get_displacement (buffer, aoffsetp);
  537. sprintf (result, "%d(sp)", disp1);
  538. break;
  539. case 0x1a:
  540. /* Memory space disp(SB). */
  541. disp1 = get_displacement (buffer, aoffsetp);
  542. sprintf (result, "%d(sb)", disp1);
  543. break;
  544. case 0x1b:
  545. /* Memory space disp(PC). */
  546. disp1 = get_displacement (buffer, aoffsetp);
  547. *result++ = NEXT_IS_ADDR;
  548. sprintf_vma (result, addr + disp1);
  549. result += strlen (result);
  550. *result++ = NEXT_IS_ADDR;
  551. *result = '\0';
  552. break;
  553. case 0x1c:
  554. case 0x1d:
  555. case 0x1e:
  556. case 0x1f:
  557. {
  558. int bit_index;
  559. static const char *ind = "bwdq";
  560. char *off;
  561. /* Scaled index basemode[R0 -- R7:B,W,D,Q]. */
  562. bit_index = bit_extract (buffer, index_offset - 8, 3);
  563. print_insn_arg (d, index_offset, aoffsetp, buffer, addr,
  564. result, 0);
  565. off = result + strlen (result);
  566. sprintf (off, "[r%d:%c]", bit_index, ind[addr_mode & 3]);
  567. }
  568. break;
  569. }
  570. break;
  571. case 'H':
  572. case 'q':
  573. Ivalue = bit_extract (buffer, ioffset-4, 4);
  574. Ivalue = sign_extend (Ivalue, 4);
  575. sprintf (result, "%d", Ivalue);
  576. ioffset -= 4;
  577. break;
  578. case 'r':
  579. Ivalue = bit_extract (buffer, ioffset-3, 3);
  580. sprintf (result, "r%d", Ivalue&7);
  581. ioffset -= 3;
  582. break;
  583. case 'd':
  584. sprintf (result, "%d", get_displacement (buffer, aoffsetp));
  585. break;
  586. case 'b':
  587. Ivalue = get_displacement (buffer, aoffsetp);
  588. /* Warning!! HACK ALERT!
  589. Operand type 'b' is only used by the cmp{b,w,d} and
  590. movm{b,w,d} instructions; we need to know whether
  591. it's a `b' or `w' or `d' instruction; and for both
  592. cmpm and movm it's stored at the same place so we
  593. just grab two bits of the opcode and look at it... */
  594. size = bit_extract(buffer, ioffset-6, 2);
  595. if (size == 0) /* 00 => b. */
  596. size = 1;
  597. else if (size == 1) /* 01 => w. */
  598. size = 2;
  599. else
  600. size = 4; /* 11 => d. */
  601. sprintf (result, "%d", (Ivalue / size) + 1);
  602. break;
  603. case 'p':
  604. *result++ = NEXT_IS_ADDR;
  605. sprintf_vma (result, addr + get_displacement (buffer, aoffsetp));
  606. result += strlen (result);
  607. *result++ = NEXT_IS_ADDR;
  608. *result = '\0';
  609. break;
  610. case 'i':
  611. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  612. *aoffsetp += 8;
  613. sprintf (result, "0x%x", Ivalue);
  614. break;
  615. case 'u':
  616. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  617. optlist (Ivalue, opt_u, result);
  618. *aoffsetp += 8;
  619. break;
  620. case 'U':
  621. Ivalue = bit_extract (buffer, *aoffsetp, 8);
  622. optlist (Ivalue, opt_U, result);
  623. *aoffsetp += 8;
  624. break;
  625. case 'O':
  626. Ivalue = bit_extract (buffer, ioffset - 9, 9);
  627. optlist (Ivalue, opt_O, result);
  628. ioffset -= 9;
  629. break;
  630. case 'C':
  631. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  632. optlist (Ivalue, opt_C, result);
  633. ioffset -= 4;
  634. break;
  635. case 'S':
  636. Ivalue = bit_extract (buffer, ioffset - 8, 8);
  637. optlist (Ivalue, opt_S, result);
  638. ioffset -= 8;
  639. break;
  640. case 'M':
  641. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  642. list_search (Ivalue, 0 ? list_M032 : list_M532, result);
  643. ioffset -= 4;
  644. break;
  645. case 'P':
  646. Ivalue = bit_extract (buffer, ioffset - 4, 4);
  647. list_search (Ivalue, 0 ? list_P032 : list_P532, result);
  648. ioffset -= 4;
  649. break;
  650. case 'g':
  651. Ivalue = bit_extract (buffer, *aoffsetp, 3);
  652. sprintf (result, "%d", Ivalue);
  653. *aoffsetp += 3;
  654. break;
  655. case 'G':
  656. Ivalue = bit_extract(buffer, *aoffsetp, 5);
  657. sprintf (result, "%d", Ivalue + 1);
  658. *aoffsetp += 5;
  659. break;
  660. }
  661. return ioffset;
  662. }
  663. /* Print the 32000 instruction at address MEMADDR in debugged memory,
  664. on STREAM. Returns length of the instruction, in bytes. */
  665. int
  666. print_insn_ns32k (bfd_vma memaddr, disassemble_info *info)
  667. {
  668. unsigned int i;
  669. const char *d;
  670. unsigned short first_word;
  671. int ioffset; /* Bits into instruction. */
  672. int aoffset; /* Bits into arguments. */
  673. char arg_bufs[MAX_ARGS+1][ARG_LEN];
  674. int argnum;
  675. int maxarg;
  676. struct private priv;
  677. bfd_byte *buffer = priv.the_buffer;
  678. dis_info = info;
  679. info->private_data = & priv;
  680. priv.max_fetched = priv.the_buffer;
  681. priv.insn_start = memaddr;
  682. if (OPCODES_SIGSETJMP (priv.bailout) != 0)
  683. /* Error return. */
  684. return -1;
  685. /* Look for 8bit opcodes first. Other wise, fetching two bytes could take
  686. us over the end of accessible data unnecessarilly. */
  687. FETCH_DATA (info, buffer + 1);
  688. for (i = 0; i < NOPCODES; i++)
  689. if (ns32k_opcodes[i].opcode_id_size <= 8
  690. && ((buffer[0]
  691. & (((unsigned long) 1 << ns32k_opcodes[i].opcode_id_size) - 1))
  692. == ns32k_opcodes[i].opcode_seed))
  693. break;
  694. if (i == NOPCODES)
  695. {
  696. /* Maybe it is 9 to 16 bits big. */
  697. FETCH_DATA (info, buffer + 2);
  698. first_word = read_memory_integer(buffer, 2);
  699. for (i = 0; i < NOPCODES; i++)
  700. if ((first_word
  701. & (((unsigned long) 1 << ns32k_opcodes[i].opcode_id_size) - 1))
  702. == ns32k_opcodes[i].opcode_seed)
  703. break;
  704. /* Handle undefined instructions. */
  705. if (i == NOPCODES)
  706. {
  707. (*dis_info->fprintf_func)(dis_info->stream, "0%o", buffer[0]);
  708. return 1;
  709. }
  710. }
  711. (*dis_info->fprintf_func)(dis_info->stream, "%s", ns32k_opcodes[i].name);
  712. ioffset = ns32k_opcodes[i].opcode_size;
  713. aoffset = ns32k_opcodes[i].opcode_size;
  714. d = ns32k_opcodes[i].operands;
  715. if (*d)
  716. {
  717. /* Offset in bits of the first thing beyond each index byte.
  718. Element 0 is for operand A and element 1 is for operand B. */
  719. int index_offset[2];
  720. /* 0 for operand A, 1 for operand B, greater for other args. */
  721. int whicharg = 0;
  722. (*dis_info->fprintf_func)(dis_info->stream, "\t");
  723. maxarg = 0;
  724. /* First we have to find and keep track of the index bytes,
  725. if we are using scaled indexed addressing mode, since the index
  726. bytes occur right after the basic instruction, not as part
  727. of the addressing extension. */
  728. index_offset[0] = -1;
  729. index_offset[1] = -1;
  730. if (Is_gen (d[1]))
  731. {
  732. int bitoff = d[1] == 'f' ? 10 : 5;
  733. int addr_mode = bit_extract (buffer, ioffset - bitoff, 5);
  734. if (Adrmod_is_index (addr_mode))
  735. {
  736. aoffset += 8;
  737. index_offset[0] = aoffset;
  738. }
  739. }
  740. if (d[2] && Is_gen (d[3]))
  741. {
  742. int addr_mode = bit_extract (buffer, ioffset - 10, 5);
  743. if (Adrmod_is_index (addr_mode))
  744. {
  745. aoffset += 8;
  746. index_offset[1] = aoffset;
  747. }
  748. }
  749. while (*d)
  750. {
  751. argnum = *d - '1';
  752. if (argnum >= MAX_ARGS)
  753. abort ();
  754. d++;
  755. if (argnum > maxarg)
  756. maxarg = argnum;
  757. ioffset = print_insn_arg (*d, ioffset, &aoffset, buffer,
  758. memaddr, arg_bufs[argnum],
  759. whicharg > 1 ? -1 : index_offset[whicharg]);
  760. d++;
  761. whicharg++;
  762. }
  763. for (argnum = 0; argnum <= maxarg; argnum++)
  764. {
  765. bfd_vma addr;
  766. char *ch;
  767. for (ch = arg_bufs[argnum]; *ch;)
  768. {
  769. if (*ch == NEXT_IS_ADDR)
  770. {
  771. ++ch;
  772. addr = bfd_scan_vma (ch, NULL, 16);
  773. (*dis_info->print_address_func) (addr, dis_info);
  774. while (*ch && *ch != NEXT_IS_ADDR)
  775. ++ch;
  776. if (*ch)
  777. ++ch;
  778. }
  779. else
  780. (*dis_info->fprintf_func)(dis_info->stream, "%c", *ch++);
  781. }
  782. if (argnum < maxarg)
  783. (*dis_info->fprintf_func)(dis_info->stream, ", ");
  784. }
  785. }
  786. return aoffset / 8;
  787. }