m68k-pinsn.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /* Print m68k instructions for GDB, the GNU debugger.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <stdio.h>
  18. #include "defs.h"
  19. #include "param.h"
  20. #include "symtab.h"
  21. #include "m68k-opcode.h"
  22. /* 68k instructions are never longer than this many bytes. */
  23. #define MAXLEN 22
  24. /* Number of elements in the opcode table. */
  25. #define NOPCODES (sizeof m68k_opcodes / sizeof m68k_opcodes[0])
  26. extern char *reg_names[];
  27. char *fpcr_names[] = { "", "fpiar", "fpsr", "fpiar/fpsr", "fpcr",
  28. "fpiar/fpcr", "fpsr/fpcr", "fpiar-fpcr"};
  29. static unsigned char *print_insn_arg ();
  30. static unsigned char *print_indexed ();
  31. static void print_base ();
  32. static int fetch_arg ();
  33. #define NEXTBYTE(p) (p += 2, ((char *)p)[-1])
  34. #define NEXTWORD(p) \
  35. (p += 2, ((((char *)p)[-2]) << 8) + p[-1])
  36. #define NEXTLONG(p) \
  37. (p += 4, (((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])
  38. #define NEXTSINGLE(p) \
  39. (p += 4, *((float *)(p - 4)))
  40. #define NEXTDOUBLE(p) \
  41. (p += 8, *((double *)(p - 8)))
  42. #define NEXTEXTEND(p) \
  43. (p += 12, 0.0) /* Need a function to convert from extended to double
  44. precision... */
  45. #define NEXTPACKED(p) \
  46. (p += 12, 0.0) /* Need a function to convert from packed to double
  47. precision. Actually, it's easier to print a
  48. packed number than a double anyway, so maybe
  49. there should be a special case to handle this... */
  50. /* Print the m68k instruction at address MEMADDR in debugged memory,
  51. on STREAM. Returns length of the instruction, in bytes. */
  52. int
  53. print_insn (memaddr, stream)
  54. CORE_ADDR memaddr;
  55. FILE *stream;
  56. {
  57. unsigned char buffer[MAXLEN];
  58. register int i;
  59. register unsigned char *p;
  60. register char *d;
  61. register int bestmask;
  62. int best;
  63. read_memory (memaddr, buffer, MAXLEN);
  64. bestmask = 0;
  65. best = -1;
  66. for (i = 0; i < NOPCODES; i++)
  67. {
  68. register unsigned int opcode = m68k_opcodes[i].opcode;
  69. register unsigned int match = m68k_opcodes[i].match;
  70. if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
  71. && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
  72. && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
  73. && ((0xff & buffer[3] & match) == (0xff & opcode)))
  74. {
  75. /* Don't use for printout the variants of divul and divsl
  76. that have the same register number in two places.
  77. The more general variants will match instead. */
  78. for (d = m68k_opcodes[i].args; *d; d += 2)
  79. if (d[1] == 'D')
  80. break;
  81. /* Don't use for printout the variants of most floating
  82. point coprocessor instructions which use the same
  83. register number in two places, as above. */
  84. if (*d == 0)
  85. for (d = m68k_opcodes[i].args; *d; d += 2)
  86. if (d[1] == 't')
  87. break;
  88. if (*d == 0 && match > bestmask)
  89. {
  90. best = i;
  91. bestmask = match;
  92. }
  93. }
  94. }
  95. /* Handle undefined instructions. */
  96. if (best < 0)
  97. {
  98. fprintf (stream, "0%o", (buffer[0] << 8) + buffer[1]);
  99. return 2;
  100. }
  101. fprintf (stream, "%s", m68k_opcodes[best].name);
  102. /* Point at first word of argument data,
  103. and at descriptor for first argument. */
  104. p = buffer + 2;
  105. /* Why do this this way? -MelloN */
  106. for (d = m68k_opcodes[best].args; *d; d += 2)
  107. {
  108. if (d[0] == '#')
  109. {
  110. if (d[1] == 'l' && p - buffer < 6)
  111. p = buffer + 6;
  112. else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8' )
  113. p = buffer + 4;
  114. }
  115. if (d[1] >= '1' && d[1] <= '3' && p - buffer < 4)
  116. p = buffer + 4;
  117. if (d[1] >= '4' && d[1] <= '6' && p - buffer < 6)
  118. p = buffer + 6;
  119. }
  120. d = m68k_opcodes[best].args;
  121. if (*d)
  122. fputc (' ', stream);
  123. while (*d)
  124. {
  125. p = print_insn_arg (d, buffer, p, memaddr + p - buffer, stream);
  126. d += 2;
  127. if (*d && *(d - 2) != 'I' && *d != 'k')
  128. fprintf (stream, ",");
  129. }
  130. return p - buffer;
  131. }
  132. static unsigned char *
  133. print_insn_arg (d, buffer, p, addr, stream)
  134. char *d;
  135. unsigned char *buffer;
  136. register unsigned char *p;
  137. CORE_ADDR addr; /* PC for this arg to be relative to */
  138. FILE *stream;
  139. {
  140. register int val;
  141. register int place = d[1];
  142. int regno;
  143. register char *regname;
  144. register unsigned char *p1;
  145. register double flval;
  146. int flt_p;
  147. switch (*d)
  148. {
  149. case 'C':
  150. fprintf (stream, "ccr");
  151. break;
  152. case 'S':
  153. fprintf (stream, "sr");
  154. break;
  155. case 'U':
  156. fprintf (stream, "usp");
  157. break;
  158. case 'Q':
  159. val = fetch_arg (buffer, place, 3);
  160. if (val == 0) val = 8;
  161. fprintf (stream, "#%d", val);
  162. break;
  163. case 'M':
  164. val = fetch_arg (buffer, place, 8);
  165. if (val & 0x80)
  166. val = val - 0x100;
  167. fprintf (stream, "#%d", val);
  168. break;
  169. case 'T':
  170. val = fetch_arg (buffer, place, 4);
  171. fprintf (stream, "#%d", val);
  172. break;
  173. case 'D':
  174. fprintf (stream, "%s", reg_names[fetch_arg (buffer, place, 3)]);
  175. break;
  176. case 'A':
  177. fprintf (stream, "%s", reg_names[fetch_arg (buffer, place, 3) + 010]);
  178. break;
  179. case 'R':
  180. fprintf (stream, "%s", reg_names[fetch_arg (buffer, place, 4)]);
  181. break;
  182. case 'F':
  183. fprintf (stream, "fp%d", fetch_arg (buffer, place, 3));
  184. break;
  185. case 'O':
  186. val = fetch_arg (buffer, place, 6);
  187. if (val & 0x20)
  188. fprintf (stream, "%s", reg_names [val & 7]);
  189. else
  190. fprintf (stream, "%d", val);
  191. break;
  192. case '+':
  193. fprintf (stream, "(%s)+", reg_names[fetch_arg (buffer, place, 3) + 8]);
  194. break;
  195. case '-':
  196. fprintf (stream, "-(%s)", reg_names[fetch_arg (buffer, place, 3) + 8]);
  197. break;
  198. case 'k':
  199. if (place == 'k')
  200. fprintf (stream, "{%s}", reg_names[fetch_arg (buffer, place, 3)]);
  201. else if (place == 'C')
  202. {
  203. val = fetch_arg (buffer, place, 7);
  204. if ( val > 63 ) /* This is a signed constant. */
  205. val -= 128;
  206. fprintf (stream, "{#%d}", val);
  207. }
  208. else
  209. error ("Invalid arg format in opcode table: \"%c%c\".",
  210. *d, place);
  211. break;
  212. case '#':
  213. p1 = buffer + 2;
  214. if (place == 's')
  215. val = fetch_arg (buffer, place, 4);
  216. else if (place == 'C')
  217. val = fetch_arg (buffer, place, 7);
  218. else if (place == '8')
  219. val = fetch_arg (buffer, place, 3);
  220. else if (place == 'b')
  221. val = NEXTBYTE (p1);
  222. else if (place == 'w')
  223. val = NEXTWORD (p1);
  224. else if (place == 'l')
  225. val = NEXTLONG (p1);
  226. else
  227. error ("Invalid arg format in opcode table: \"%c%c\".",
  228. *d, place);
  229. fprintf (stream, "#%d", val);
  230. break;
  231. case '^':
  232. if (place == 's')
  233. val = fetch_arg (buffer, place, 4);
  234. else if (place == 'C')
  235. val = fetch_arg (buffer, place, 7);
  236. else if (place == '8')
  237. val = fetch_arg (buffer, place, 3);
  238. else if (place == 'b')
  239. val = NEXTBYTE (p);
  240. else if (place == 'w')
  241. val = NEXTWORD (p);
  242. else if (place == 'l')
  243. val = NEXTLONG (p);
  244. else
  245. error ("Invalid arg format in opcode table: \"%c%c\".",
  246. *d, place);
  247. fprintf (stream, "#%d", val);
  248. break;
  249. case 'B':
  250. if (place == 'b')
  251. val = NEXTBYTE (p);
  252. else if (place == 'w')
  253. val = NEXTWORD (p);
  254. else if (place == 'l')
  255. val = NEXTLONG (p);
  256. else if (place == 'g')
  257. {
  258. val = ((char *)buffer)[1];
  259. if (val == 0)
  260. val = NEXTWORD (p);
  261. else if (val == -1)
  262. val = NEXTLONG (p);
  263. }
  264. else if (place == 'c')
  265. {
  266. if (buffer[1] & 0x40) /* If bit six is one, long offset */
  267. val = NEXTLONG (p);
  268. else
  269. val = NEXTWORD (p);
  270. }
  271. else
  272. error ("Invalid arg format in opcode table: \"%c%c\".",
  273. *d, place);
  274. print_address (addr + val, stream);
  275. break;
  276. case 'd':
  277. val = NEXTWORD (p);
  278. fprintf (stream, "%d(%s)", val, fetch_arg (buffer, place, 3));
  279. break;
  280. case 's':
  281. fprintf (stream, "%s", fpcr_names[fetch_arg (buffer, place, 3)]);
  282. break;
  283. case 'I':
  284. val = fetch_arg (buffer, 'd', 3); /* Get coprocessor ID... */
  285. if (val != 1) /* Unusual coprocessor ID? */
  286. fprintf (stream, "(cpid=%d) ", val);
  287. if (place == 'i')
  288. p += 2; /* Skip coprocessor extended operands */
  289. break;
  290. case '*':
  291. case '~':
  292. case '%':
  293. case ';':
  294. case '@':
  295. case '!':
  296. case '$':
  297. case '?':
  298. case '/':
  299. case '&':
  300. if (place == 'd')
  301. {
  302. val = fetch_arg (buffer, 'x', 6);
  303. val = ((val & 7) << 3) + ((val >> 3) & 7);
  304. }
  305. else
  306. val = fetch_arg (buffer, 's', 6);
  307. /* Get register number assuming address register. */
  308. regno = (val & 7) + 8;
  309. regname = reg_names[regno];
  310. switch (val >> 3)
  311. {
  312. case 0:
  313. fprintf (stream, "%s", reg_names[val]);
  314. break;
  315. case 1:
  316. fprintf (stream, "%s", regname);
  317. break;
  318. case 2:
  319. fprintf (stream, "(%s)", regname);
  320. break;
  321. case 3:
  322. fprintf (stream, "(%s)+", regname);
  323. break;
  324. case 4:
  325. fprintf (stream, "-(%s)", regname);
  326. break;
  327. case 5:
  328. val = NEXTWORD (p);
  329. fprintf (stream, "%d(%s)", val, regname);
  330. break;
  331. case 6:
  332. p = print_indexed (regno, p, addr, stream);
  333. break;
  334. case 7:
  335. switch (val & 7)
  336. {
  337. case 0:
  338. val = NEXTWORD (p);
  339. fprintf (stream, "@#");
  340. print_address (val, stream);
  341. break;
  342. case 1:
  343. val = NEXTLONG (p);
  344. fprintf (stream, "@#");
  345. print_address (val, stream);
  346. break;
  347. case 2:
  348. val = NEXTWORD (p);
  349. print_address (addr + val, stream);
  350. break;
  351. case 3:
  352. p = print_indexed (-1, p, addr, stream);
  353. break;
  354. case 4:
  355. flt_p = 1; /* Assume it's a float... */
  356. switch( place )
  357. {
  358. case 'b':
  359. val = NEXTBYTE (p);
  360. flt_p = 0;
  361. break;
  362. case 'w':
  363. val = NEXTWORD (p);
  364. flt_p = 0;
  365. break;
  366. case 'l':
  367. val = NEXTLONG (p);
  368. flt_p = 0;
  369. break;
  370. case 'f':
  371. flval = NEXTSINGLE(p);
  372. break;
  373. case 'F':
  374. flval = NEXTDOUBLE(p);
  375. break;
  376. case 'x':
  377. flval = NEXTEXTEND(p);
  378. break;
  379. case 'p':
  380. flval = NEXTPACKED(p);
  381. break;
  382. default:
  383. error ("Invalid arg format in opcode table: \"%c%c\".",
  384. *d, place);
  385. }
  386. if ( flt_p ) /* Print a float? */
  387. fprintf (stream, "#%g", flval);
  388. else
  389. fprintf (stream, "#%d", val);
  390. break;
  391. default:
  392. fprintf (stream, "<invalid address mode 0%o>", val);
  393. }
  394. }
  395. break;
  396. default:
  397. error ("Invalid arg format in opcode table: \"%c\".", *d);
  398. }
  399. return (unsigned char *) p;
  400. }
  401. /* Fetch BITS bits from a position in the instruction specified by CODE.
  402. CODE is a "place to put an argument", or 'x' for a destination
  403. that is a general address (mode and register).
  404. BUFFER contains the instruction. */
  405. static int
  406. fetch_arg (buffer, code, bits)
  407. unsigned char *buffer;
  408. char code;
  409. int bits;
  410. {
  411. register int val;
  412. switch (code)
  413. {
  414. case 's':
  415. val = buffer[1];
  416. break;
  417. case 'd': /* Destination, for register or quick. */
  418. val = (buffer[0] << 8) + buffer[1];
  419. val >>= 9;
  420. break;
  421. case 'x': /* Destination, for general arg */
  422. val = (buffer[0] << 8) + buffer[1];
  423. val >>= 6;
  424. break;
  425. case 'k':
  426. val = (buffer[3] >> 4);
  427. break;
  428. case 'C':
  429. val = buffer[3];
  430. break;
  431. case '1':
  432. val = (buffer[2] << 8) + buffer[3];
  433. val >>= 12;
  434. break;
  435. case '2':
  436. val = (buffer[2] << 8) + buffer[3];
  437. val >>= 6;
  438. break;
  439. case '3':
  440. val = (buffer[2] << 8) + buffer[3];
  441. break;
  442. case '4':
  443. val = (buffer[4] << 8) + buffer[5];
  444. val >>= 12;
  445. break;
  446. case '5':
  447. val = (buffer[4] << 8) + buffer[5];
  448. val >>= 6;
  449. break;
  450. case '6':
  451. val = (buffer[4] << 8) + buffer[5];
  452. break;
  453. case '7':
  454. val = (buffer[2] << 8) + buffer[3];
  455. val >>= 7;
  456. break;
  457. case '8':
  458. val = (buffer[2] << 8) + buffer[3];
  459. val >>= 10;
  460. break;
  461. default:
  462. abort ();
  463. }
  464. switch (bits)
  465. {
  466. case 3:
  467. return val & 7;
  468. case 4:
  469. return val & 017;
  470. case 5:
  471. return val & 037;
  472. case 6:
  473. return val & 077;
  474. case 7:
  475. return val & 0177;
  476. case 8:
  477. return val & 0377;
  478. default:
  479. abort ();
  480. }
  481. }
  482. /* Print an indexed argument. The base register is BASEREG (-1 for pc).
  483. P points to extension word, in buffer.
  484. ADDR is the nominal core address of that extension word. */
  485. static unsigned char *
  486. print_indexed (basereg, p, addr, stream)
  487. int basereg;
  488. unsigned char *p;
  489. FILE *stream;
  490. CORE_ADDR addr;
  491. {
  492. register int word;
  493. static char *scales[] = {"", "*2", "*4", "*8"};
  494. register int base_disp;
  495. register int outer_disp;
  496. char buf[40];
  497. word = NEXTWORD (p);
  498. /* Generate the text for the index register.
  499. Where this will be output is not yet determined. */
  500. sprintf (buf, "[%s.%c%s]",
  501. reg_names[(word >> 12) & 0xf],
  502. (word & 0x800) ? 'l' : 'w',
  503. scales[(word >> 9) & 3]);
  504. /* Handle the 68000 style of indexing. */
  505. if ((word & 0x100) == 0)
  506. {
  507. print_base (basereg,
  508. ((word & 0x80) ? word | 0xff00 : word & 0xff)
  509. + ((basereg == -1) ? addr : 0),
  510. stream);
  511. fprintf (stream, "%s", buf);
  512. return p;
  513. }
  514. /* Handle the generalized kind. */
  515. /* First, compute the displacement to add to the base register. */
  516. if (word & 0200)
  517. basereg = -2;
  518. if (word & 0100)
  519. buf[0] = 0;
  520. base_disp = 0;
  521. switch ((word >> 4) & 3)
  522. {
  523. case 2:
  524. base_disp = NEXTWORD (p);
  525. break;
  526. case 3:
  527. base_disp = NEXTLONG (p);
  528. }
  529. if (basereg == -1)
  530. base_disp += addr;
  531. /* Handle single-level case (not indirect) */
  532. if ((word & 7) == 0)
  533. {
  534. print_base (basereg, base_disp, stream);
  535. fprintf (stream, "%s", buf);
  536. return p;
  537. }
  538. /* Two level. Compute displacement to add after indirection. */
  539. outer_disp = 0;
  540. switch (word & 3)
  541. {
  542. case 2:
  543. outer_disp = NEXTWORD (p);
  544. break;
  545. case 3:
  546. outer_disp = NEXTLONG (p);
  547. }
  548. fprintf (stream, "%d(", outer_disp);
  549. print_base (basereg, base_disp, stream);
  550. /* If postindexed, print the closeparen before the index. */
  551. if (word & 4)
  552. fprintf (stream, ")%s", buf);
  553. /* If preindexed, print the closeparen after the index. */
  554. else
  555. fprintf (stream, "%s)", buf);
  556. return p;
  557. }
  558. /* Print a base register REGNO and displacement DISP, on STREAM.
  559. REGNO = -1 for pc, -2 for none (suppressed). */
  560. static void
  561. print_base (regno, disp, stream)
  562. int regno;
  563. int disp;
  564. FILE *stream;
  565. {
  566. if (regno == -2)
  567. fprintf (stream, "%d", disp);
  568. else if (regno == -1)
  569. fprintf (stream, "0x%x", disp);
  570. else
  571. fprintf (stream, "%d(%s)", disp, reg_names[regno]);
  572. }
  573. /* This is not part of insn printing, but it is machine-specific,
  574. so this is a convenient place to put it.
  575. Convert a 68881 extended float to a double.
  576. FROM is the address of the extended float.
  577. Store the double in *TO. */
  578. convert_from_68881 (from, to)
  579. char *from;
  580. double *to;
  581. {
  582. asm ("movl a6@(8),a0");
  583. asm ("fmovex a0@,fp0");
  584. asm ("movl a6@(12),a1");
  585. asm ("fmoved fp0,a1@");
  586. }
  587. /* The converse: convert the double *FROM to an extended float
  588. and store where TO points. */
  589. convert_to_68881 (from, to)
  590. double *from;
  591. char *to;
  592. {
  593. asm ("movl a6@(8),a0");
  594. asm ("fmoved a0@,fp0");
  595. asm ("movl a6@(12),a1");
  596. asm ("fmovex fp0,a1@");
  597. }