arc-dis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* Instruction printing code for the ARC.
  2. Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. Contributed by Claudiu Zissulescu (claziss@synopsys.com)
  4. This file is part of libopcodes.
  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 program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include "dis-asm.h"
  21. #include "opcode/arc.h"
  22. #include "arc-dis.h"
  23. #include "arc-ext.h"
  24. /* Globals variables. */
  25. static const char * const regnames[64] =
  26. {
  27. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  28. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  29. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  30. "r24", "r25", "gp", "fp", "sp", "ilink", "r30", "blink",
  31. "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
  32. "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
  33. "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
  34. "r56", "r57", "ACCL", "ACCH", "lp_count", "rezerved", "LIMM", "pcl"
  35. };
  36. /* Macros section. */
  37. #ifdef DEBUG
  38. # define pr_debug(fmt, args...) fprintf (stderr, fmt, ##args)
  39. #else
  40. # define pr_debug(fmt, args...)
  41. #endif
  42. #define ARRANGE_ENDIAN(info, buf) \
  43. (info->endian == BFD_ENDIAN_LITTLE ? bfd_getm32 (bfd_getl32 (buf)) \
  44. : bfd_getb32 (buf))
  45. #define BITS(word,s,e) (((word) << (sizeof (word) * 8 - 1 - e)) >> \
  46. (s + (sizeof (word) * 8 - 1 - e)))
  47. #define OPCODE(word) (BITS ((word), 27, 31))
  48. #define FIELDA(word) (BITS ((word), 21, 26))
  49. #define FIELDB(word) (BITS ((word), 15, 20))
  50. #define FIELDC(word) (BITS ((word), 9, 14))
  51. #define OPCODE_AC(word) (BITS ((word), 11, 15))
  52. /* Functions implementation. */
  53. static bfd_vma
  54. bfd_getm32 (unsigned int data)
  55. {
  56. bfd_vma value = 0;
  57. value = ((data & 0xff00) | (data & 0xff)) << 16;
  58. value |= ((data & 0xff0000) | (data & 0xff000000)) >> 16;
  59. return value;
  60. }
  61. static int
  62. special_flag_p (const char *opname,
  63. const char *flgname)
  64. {
  65. const struct arc_flag_special *flg_spec;
  66. size_t len;
  67. unsigned i, j, flgidx;
  68. for (i = 0; i < arc_num_flag_special; i++)
  69. {
  70. flg_spec = &arc_flag_special_cases[i];
  71. len = strlen (flg_spec->name);
  72. if (strncmp (opname, flg_spec->name, len) != 0)
  73. continue;
  74. /* Found potential special case instruction. */
  75. for (j=0;; ++j)
  76. {
  77. flgidx = flg_spec->flags[j];
  78. if (flgidx == 0)
  79. break; /* End of the array. */
  80. if (strcmp (flgname, arc_flag_operands[flgidx].name) == 0)
  81. return 1;
  82. }
  83. }
  84. return 0;
  85. }
  86. /* Disassemble ARC instructions. */
  87. static int
  88. print_insn_arc (bfd_vma memaddr,
  89. struct disassemble_info *info)
  90. {
  91. bfd_byte buffer[4];
  92. unsigned int lowbyte, highbyte;
  93. int status;
  94. unsigned int i;
  95. int insnLen = 0;
  96. unsigned insn[2] = { 0, 0 };
  97. unsigned isa_mask;
  98. const unsigned char *opidx;
  99. const unsigned char *flgidx;
  100. const struct arc_opcode *opcode;
  101. const char *instrName;
  102. int flags;
  103. bfd_boolean need_comma;
  104. bfd_boolean open_braket;
  105. lowbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 1 : 0);
  106. highbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 0 : 1);
  107. switch (info->mach)
  108. {
  109. case bfd_mach_arc_arc700:
  110. isa_mask = ARC_OPCODE_ARC700;
  111. break;
  112. case bfd_mach_arc_arc600:
  113. isa_mask = ARC_OPCODE_ARC600;
  114. break;
  115. case bfd_mach_arc_arcv2:
  116. default:
  117. isa_mask = ARC_OPCODE_ARCv2HS | ARC_OPCODE_ARCv2EM;
  118. break;
  119. }
  120. /* Read the insn into a host word. */
  121. status = (*info->read_memory_func) (memaddr, buffer, 2, info);
  122. if (status != 0)
  123. {
  124. (*info->memory_error_func) (status, memaddr, info);
  125. return -1;
  126. }
  127. if (info->section
  128. && !(info->section->flags & SEC_CODE))
  129. {
  130. /* Sort of data section, just print a 32 bit number. */
  131. insnLen = 4;
  132. status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 2, info);
  133. if (status != 0)
  134. {
  135. (*info->memory_error_func) (status, memaddr + 2, info);
  136. return -1;
  137. }
  138. insn[0] = ARRANGE_ENDIAN (info, buffer);
  139. (*info->fprintf_func) (info->stream, ".long %#08x", insn[0]);
  140. return insnLen;
  141. }
  142. if ((((buffer[lowbyte] & 0xf8) > 0x38)
  143. && ((buffer[lowbyte] & 0xf8) != 0x48))
  144. || ((info->mach == bfd_mach_arc_arcv2)
  145. && ((buffer[lowbyte] & 0xF8) == 0x48)) /* FIXME! ugly. */
  146. )
  147. {
  148. /* This is a short instruction. */
  149. insnLen = 2;
  150. insn[0] = (buffer[lowbyte] << 8) | buffer[highbyte];
  151. }
  152. else
  153. {
  154. insnLen = 4;
  155. /* This is a long instruction: Read the remaning 2 bytes. */
  156. status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 2, info);
  157. if (status != 0)
  158. {
  159. (*info->memory_error_func) (status, memaddr + 2, info);
  160. return -1;
  161. }
  162. insn[0] = ARRANGE_ENDIAN (info, buffer);
  163. }
  164. /* This variable may be set by the instruction decoder. It suggests
  165. the number of bytes objdump should display on a single line. If
  166. the instruction decoder sets this, it should always set it to
  167. the same value in order to get reasonable looking output. */
  168. info->bytes_per_line = 8;
  169. /* The next two variables control the way objdump displays the raw data.
  170. For example, if bytes_per_line is 8 and bytes_per_chunk is 4, the
  171. output will look like this:
  172. 00: 00000000 00000000
  173. with the chunks displayed according to "display_endian". */
  174. info->bytes_per_chunk = 2;
  175. info->display_endian = info->endian;
  176. /* Set some defaults for the insn info. */
  177. info->insn_info_valid = 1;
  178. info->branch_delay_insns = 0;
  179. info->data_size = 0;
  180. info->insn_type = dis_nonbranch;
  181. info->target = 0;
  182. info->target2 = 0;
  183. /* FIXME to be moved in dissasemble_init_for_target. */
  184. info->disassembler_needs_relocs = TRUE;
  185. /* Find the first match in the opcode table. */
  186. for (i = 0; i < arc_num_opcodes; i++)
  187. {
  188. bfd_boolean invalid = FALSE;
  189. opcode = &arc_opcodes[i];
  190. if (ARC_SHORT (opcode->mask) && (insnLen == 2))
  191. {
  192. if (OPCODE_AC (opcode->opcode) != OPCODE_AC (insn[0]))
  193. continue;
  194. }
  195. else if (!ARC_SHORT (opcode->mask) && (insnLen == 4))
  196. {
  197. if (OPCODE (opcode->opcode) != OPCODE (insn[0]))
  198. continue;
  199. }
  200. else
  201. continue;
  202. if ((insn[0] ^ opcode->opcode) & opcode->mask)
  203. continue;
  204. if (!(opcode->cpu & isa_mask))
  205. continue;
  206. /* Possible candidate, check the operands. */
  207. for (opidx = opcode->operands; *opidx; opidx++)
  208. {
  209. int value;
  210. const struct arc_operand *operand = &arc_operands[*opidx];
  211. if (operand->flags & ARC_OPERAND_FAKE)
  212. continue;
  213. if (operand->extract)
  214. value = (*operand->extract) (insn[0], &invalid);
  215. else
  216. value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
  217. /* Check for LIMM indicator. If it is there, then make sure
  218. we pick the right format. */
  219. if (operand->flags & ARC_OPERAND_IR
  220. && !(operand->flags & ARC_OPERAND_LIMM))
  221. {
  222. if ((value == 0x3E && insnLen == 4)
  223. || (value == 0x1E && insnLen == 2))
  224. {
  225. invalid = TRUE;
  226. break;
  227. }
  228. }
  229. }
  230. /* Check the flags. */
  231. for (flgidx = opcode->flags; *flgidx; flgidx++)
  232. {
  233. /* Get a valid flag class. */
  234. const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
  235. const unsigned *flgopridx;
  236. int foundA = 0, foundB = 0;
  237. for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
  238. {
  239. const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
  240. unsigned int value;
  241. value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
  242. if (value == flg_operand->code)
  243. foundA = 1;
  244. if (value)
  245. foundB = 1;
  246. }
  247. if (!foundA && foundB)
  248. {
  249. invalid = TRUE;
  250. break;
  251. }
  252. }
  253. if (invalid)
  254. continue;
  255. /* The instruction is valid. */
  256. goto found;
  257. }
  258. /* No instruction found. Try the extenssions. */
  259. instrName = arcExtMap_instName (OPCODE (insn[0]), insn[0], &flags);
  260. if (instrName)
  261. {
  262. opcode = &arc_opcodes[0];
  263. (*info->fprintf_func) (info->stream, "%s", instrName);
  264. goto print_flags;
  265. }
  266. if (insnLen == 2)
  267. (*info->fprintf_func) (info->stream, ".long %#04x", insn[0]);
  268. else
  269. (*info->fprintf_func) (info->stream, ".long %#08x", insn[0]);
  270. info->insn_type = dis_noninsn;
  271. return insnLen;
  272. found:
  273. /* Print the mnemonic. */
  274. (*info->fprintf_func) (info->stream, "%s", opcode->name);
  275. /* Preselect the insn class. */
  276. switch (opcode->class)
  277. {
  278. case BRANCH:
  279. case JUMP:
  280. if (!strncmp (opcode->name, "bl", 2)
  281. || !strncmp (opcode->name, "jl", 2))
  282. info->insn_type = dis_jsr;
  283. else
  284. info->insn_type = dis_branch;
  285. break;
  286. case MEMORY:
  287. info->insn_type = dis_dref; /* FIXME! DB indicates mov as memory! */
  288. break;
  289. default:
  290. info->insn_type = dis_nonbranch;
  291. break;
  292. }
  293. pr_debug ("%s: 0x%08x\n", opcode->name, opcode->opcode);
  294. print_flags:
  295. /* Now extract and print the flags. */
  296. for (flgidx = opcode->flags; *flgidx; flgidx++)
  297. {
  298. /* Get a valid flag class. */
  299. const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
  300. const unsigned *flgopridx;
  301. for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
  302. {
  303. const struct arc_flag_operand *flg_operand = &arc_flag_operands[*flgopridx];
  304. unsigned int value;
  305. if (!flg_operand->favail)
  306. continue;
  307. value = (insn[0] >> flg_operand->shift) & ((1 << flg_operand->bits) - 1);
  308. if (value == flg_operand->code)
  309. {
  310. /* FIXME!: print correctly nt/t flag. */
  311. if (!special_flag_p (opcode->name, flg_operand->name))
  312. (*info->fprintf_func) (info->stream, ".");
  313. else if (info->insn_type == dis_dref)
  314. {
  315. switch (flg_operand->name[0])
  316. {
  317. case 'b':
  318. info->data_size = 1;
  319. break;
  320. case 'h':
  321. case 'w':
  322. info->data_size = 2;
  323. break;
  324. default:
  325. info->data_size = 4;
  326. break;
  327. }
  328. }
  329. (*info->fprintf_func) (info->stream, "%s", flg_operand->name);
  330. }
  331. if (flg_operand->name[0] == 'd'
  332. && flg_operand->name[1] == 0)
  333. info->branch_delay_insns = 1;
  334. }
  335. }
  336. if (opcode->operands[0] != 0)
  337. (*info->fprintf_func) (info->stream, "\t");
  338. need_comma = FALSE;
  339. open_braket = FALSE;
  340. /* Now extract and print the operands. */
  341. for (opidx = opcode->operands; *opidx; opidx++)
  342. {
  343. const struct arc_operand *operand = &arc_operands[*opidx];
  344. int value;
  345. if (open_braket && (operand->flags & ARC_OPERAND_BRAKET))
  346. {
  347. (*info->fprintf_func) (info->stream, "]");
  348. open_braket = FALSE;
  349. continue;
  350. }
  351. /* Only take input from real operands. */
  352. if ((operand->flags & ARC_OPERAND_FAKE)
  353. && !(operand->flags & ARC_OPERAND_BRAKET))
  354. continue;
  355. if (operand->extract)
  356. value = (*operand->extract) (insn[0], (int *) NULL);
  357. else
  358. {
  359. if (operand->flags & ARC_OPERAND_ALIGNED32)
  360. {
  361. value = (insn[0] >> operand->shift)
  362. & ((1 << (operand->bits - 2)) - 1);
  363. value = value << 2;
  364. }
  365. else
  366. {
  367. value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
  368. }
  369. if (operand->flags & ARC_OPERAND_SIGNED)
  370. {
  371. int signbit = 1 << (operand->bits - 1);
  372. value = (value ^ signbit) - signbit;
  373. }
  374. }
  375. if (operand->flags & ARC_OPERAND_IGNORE
  376. && (operand->flags & ARC_OPERAND_IR
  377. && value == -1))
  378. continue;
  379. if (need_comma)
  380. (*info->fprintf_func) (info->stream, ",");
  381. if (!open_braket && (operand->flags & ARC_OPERAND_BRAKET))
  382. {
  383. (*info->fprintf_func) (info->stream, "[");
  384. open_braket = TRUE;
  385. need_comma = FALSE;
  386. continue;
  387. }
  388. /* Read the limm operand, if required. */
  389. if (operand->flags & ARC_OPERAND_LIMM
  390. && !(operand->flags & ARC_OPERAND_DUPLICATE))
  391. {
  392. status = (*info->read_memory_func) (memaddr + insnLen, buffer,
  393. 4, info);
  394. if (status != 0)
  395. {
  396. (*info->memory_error_func) (status, memaddr + insnLen, info);
  397. return -1;
  398. }
  399. insn[1] = ARRANGE_ENDIAN (info, buffer);
  400. }
  401. /* Print the operand as directed by the flags. */
  402. if (operand->flags & ARC_OPERAND_IR)
  403. {
  404. assert (value >=0 && value < 64);
  405. (*info->fprintf_func) (info->stream, "%s", regnames[value]);
  406. if (operand->flags & ARC_OPERAND_TRUNCATE)
  407. (*info->fprintf_func) (info->stream, "%s", regnames[value+1]);
  408. }
  409. else if (operand->flags & ARC_OPERAND_LIMM)
  410. {
  411. (*info->fprintf_func) (info->stream, "%#x", insn[1]);
  412. if (info->insn_type == dis_branch
  413. || info->insn_type == dis_jsr)
  414. info->target = (bfd_vma) insn[1];
  415. }
  416. else if (operand->flags & ARC_OPERAND_PCREL)
  417. {
  418. /* PCL relative. */
  419. if (info->flags & INSN_HAS_RELOC)
  420. memaddr = 0;
  421. (*info->print_address_func) ((memaddr & ~3) + value, info);
  422. info->target = (bfd_vma) (memaddr & ~3) + value;
  423. }
  424. else if (operand->flags & ARC_OPERAND_SIGNED)
  425. (*info->fprintf_func) (info->stream, "%d", value);
  426. else
  427. if (operand->flags & ARC_OPERAND_TRUNCATE
  428. && !(operand->flags & ARC_OPERAND_ALIGNED32)
  429. && !(operand->flags & ARC_OPERAND_ALIGNED16)
  430. && value > 0 && value <= 14)
  431. (*info->fprintf_func) (info->stream, "r13-%s",
  432. regnames[13 + value - 1]);
  433. else
  434. (*info->fprintf_func) (info->stream, "%#x", value);
  435. need_comma = TRUE;
  436. /* Adjust insn len. */
  437. if (operand->flags & ARC_OPERAND_LIMM
  438. && !(operand->flags & ARC_OPERAND_DUPLICATE))
  439. insnLen += 4;
  440. }
  441. return insnLen;
  442. }
  443. disassembler_ftype
  444. arc_get_disassembler (bfd *abfd)
  445. {
  446. /* Read the extenssion insns and registers, if any. */
  447. build_ARC_extmap (abfd);
  448. dump_ARC_extmap ();
  449. return print_insn_arc;
  450. }
  451. /* Disassemble ARC instructions. Used by debugger. */
  452. struct arcDisState
  453. arcAnalyzeInstr (bfd_vma memaddr,
  454. struct disassemble_info *info)
  455. {
  456. struct arcDisState ret;
  457. memset (&ret, 0, sizeof (struct arcDisState));
  458. ret.instructionLen = print_insn_arc (memaddr, info);
  459. #if 0
  460. ret.words[0] = insn[0];
  461. ret.words[1] = insn[1];
  462. ret._this = &ret;
  463. ret.coreRegName = _coreRegName;
  464. ret.auxRegName = _auxRegName;
  465. ret.condCodeName = _condCodeName;
  466. ret.instName = _instName;
  467. #endif
  468. return ret;
  469. }
  470. /* Local variables:
  471. eval: (c-set-style "gnu")
  472. indent-tabs-mode: t
  473. End: */