arc-dis.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. /* Instruction printing code for the ARC.
  2. Copyright (C) 1994-2022 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 "elf/arc.h"
  23. #include "arc-dis.h"
  24. #include "arc-ext.h"
  25. #include "elf-bfd.h"
  26. #include "libiberty.h"
  27. #include "opintl.h"
  28. /* Structure used to iterate over, and extract the values for, operands of
  29. an opcode. */
  30. struct arc_operand_iterator
  31. {
  32. /* The complete instruction value to extract operands from. */
  33. unsigned long long insn;
  34. /* The LIMM if this is being tracked separately. This field is only
  35. valid if we find the LIMM operand in the operand list. */
  36. unsigned limm;
  37. /* The opcode this iterator is operating on. */
  38. const struct arc_opcode *opcode;
  39. /* The index into the opcodes operand index list. */
  40. const unsigned char *opidx;
  41. };
  42. /* A private data used by ARC decoder. */
  43. struct arc_disassemble_info
  44. {
  45. /* The current disassembled arc opcode. */
  46. const struct arc_opcode *opcode;
  47. /* Instruction length w/o limm field. */
  48. unsigned insn_len;
  49. /* TRUE if we have limm. */
  50. bool limm_p;
  51. /* LIMM value, if exists. */
  52. unsigned limm;
  53. /* Condition code, if exists. */
  54. unsigned condition_code;
  55. /* Writeback mode. */
  56. unsigned writeback_mode;
  57. /* Number of operands. */
  58. unsigned operands_count;
  59. struct arc_insn_operand operands[MAX_INSN_ARGS];
  60. };
  61. /* Globals variables. */
  62. static const char * const regnames[64] =
  63. {
  64. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  65. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  66. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  67. "r24", "r25", "gp", "fp", "sp", "ilink", "r30", "blink",
  68. "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
  69. "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
  70. "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
  71. "r56", "r57", "r58", "r59", "lp_count", "reserved", "LIMM", "pcl"
  72. };
  73. static const char * const addrtypenames[ARC_NUM_ADDRTYPES] =
  74. {
  75. "bd", "jid", "lbd", "mbd", "sd", "sm", "xa", "xd",
  76. "cd", "cbd", "cjid", "clbd", "cm", "csd", "cxa", "cxd"
  77. };
  78. static int addrtypenames_max = ARC_NUM_ADDRTYPES - 1;
  79. static const char * const addrtypeunknown = "unknown";
  80. /* This structure keeps track which instruction class(es)
  81. should be ignored durring disassembling. */
  82. typedef struct skipclass
  83. {
  84. insn_class_t insn_class;
  85. insn_subclass_t subclass;
  86. struct skipclass *nxt;
  87. } skipclass_t, *linkclass;
  88. /* Intial classes of instructions to be consider first when
  89. disassembling. */
  90. static linkclass decodelist = NULL;
  91. /* ISA mask value enforced via disassembler info options. ARC_OPCODE_NONE
  92. value means that no CPU is enforced. */
  93. static unsigned enforced_isa_mask = ARC_OPCODE_NONE;
  94. /* True if we want to print using only hex numbers. */
  95. static bool print_hex = false;
  96. /* Macros section. */
  97. #ifdef DEBUG
  98. # define pr_debug(fmt, args...) fprintf (stderr, fmt, ##args)
  99. #else
  100. # define pr_debug(fmt, args...)
  101. #endif
  102. #define ARRANGE_ENDIAN(info, buf) \
  103. (info->endian == BFD_ENDIAN_LITTLE ? bfd_getm32 (bfd_getl32 (buf)) \
  104. : bfd_getb32 (buf))
  105. #define BITS(word,s,e) (((word) >> (s)) & ((1ull << ((e) - (s)) << 1) - 1))
  106. #define OPCODE_32BIT_INSN(word) (BITS ((word), 27, 31))
  107. /* Functions implementation. */
  108. /* Initialize private data. */
  109. static bool
  110. init_arc_disasm_info (struct disassemble_info *info)
  111. {
  112. struct arc_disassemble_info *arc_infop
  113. = calloc (sizeof (*arc_infop), 1);
  114. if (arc_infop == NULL)
  115. return false;
  116. info->private_data = arc_infop;
  117. return true;
  118. }
  119. /* Add a new element to the decode list. */
  120. static void
  121. add_to_decodelist (insn_class_t insn_class,
  122. insn_subclass_t subclass)
  123. {
  124. linkclass t = (linkclass) xmalloc (sizeof (skipclass_t));
  125. t->insn_class = insn_class;
  126. t->subclass = subclass;
  127. t->nxt = decodelist;
  128. decodelist = t;
  129. }
  130. /* Return TRUE if we need to skip the opcode from being
  131. disassembled. */
  132. static bool
  133. skip_this_opcode (const struct arc_opcode *opcode)
  134. {
  135. linkclass t = decodelist;
  136. /* Check opcode for major 0x06, return if it is not in. */
  137. if (arc_opcode_len (opcode) == 4
  138. && (OPCODE_32BIT_INSN (opcode->opcode) != 0x06
  139. /* Can be an APEX extensions. */
  140. && OPCODE_32BIT_INSN (opcode->opcode) != 0x07))
  141. return false;
  142. /* or not a known truble class. */
  143. switch (opcode->insn_class)
  144. {
  145. case FLOAT:
  146. case DSP:
  147. case ARITH:
  148. case MPY:
  149. break;
  150. default:
  151. return false;
  152. }
  153. while (t != NULL)
  154. {
  155. if ((t->insn_class == opcode->insn_class)
  156. && (t->subclass == opcode->subclass))
  157. return false;
  158. t = t->nxt;
  159. }
  160. return true;
  161. }
  162. static bfd_vma
  163. bfd_getm32 (unsigned int data)
  164. {
  165. bfd_vma value = 0;
  166. value = ((data & 0xff00) | (data & 0xff)) << 16;
  167. value |= ((data & 0xff0000) | (data & 0xff000000)) >> 16;
  168. return value;
  169. }
  170. static bool
  171. special_flag_p (const char *opname,
  172. const char *flgname)
  173. {
  174. const struct arc_flag_special *flg_spec;
  175. unsigned i, j, flgidx;
  176. for (i = 0; i < arc_num_flag_special; i++)
  177. {
  178. flg_spec = &arc_flag_special_cases[i];
  179. if (strcmp (opname, flg_spec->name))
  180. continue;
  181. /* Found potential special case instruction. */
  182. for (j=0;; ++j)
  183. {
  184. flgidx = flg_spec->flags[j];
  185. if (flgidx == 0)
  186. break; /* End of the array. */
  187. if (strcmp (flgname, arc_flag_operands[flgidx].name) == 0)
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. /* Find opcode from ARC_TABLE given the instruction described by INSN and
  194. INSNLEN. The ISA_MASK restricts the possible matches in ARC_TABLE. */
  195. static const struct arc_opcode *
  196. find_format_from_table (struct disassemble_info *info,
  197. const struct arc_opcode *arc_table,
  198. unsigned long long insn,
  199. unsigned int insn_len,
  200. unsigned isa_mask,
  201. bool *has_limm,
  202. bool overlaps)
  203. {
  204. unsigned int i = 0;
  205. const struct arc_opcode *opcode = NULL;
  206. const struct arc_opcode *t_op = NULL;
  207. const unsigned char *opidx;
  208. const unsigned char *flgidx;
  209. bool warn_p = false;
  210. do
  211. {
  212. bool invalid = false;
  213. opcode = &arc_table[i++];
  214. if (!(opcode->cpu & isa_mask))
  215. continue;
  216. if (arc_opcode_len (opcode) != (int) insn_len)
  217. continue;
  218. if ((insn & opcode->mask) != opcode->opcode)
  219. continue;
  220. *has_limm = false;
  221. /* Possible candidate, check the operands. */
  222. for (opidx = opcode->operands; *opidx; opidx++)
  223. {
  224. int value, limmind;
  225. const struct arc_operand *operand = &arc_operands[*opidx];
  226. if (operand->flags & ARC_OPERAND_FAKE)
  227. continue;
  228. if (operand->extract)
  229. value = (*operand->extract) (insn, &invalid);
  230. else
  231. value = (insn >> operand->shift) & ((1ull << operand->bits) - 1);
  232. /* Check for LIMM indicator. If it is there, then make sure
  233. we pick the right format. */
  234. limmind = (isa_mask & ARC_OPCODE_ARCV2) ? 0x1E : 0x3E;
  235. if (operand->flags & ARC_OPERAND_IR
  236. && !(operand->flags & ARC_OPERAND_LIMM))
  237. {
  238. if ((value == 0x3E && insn_len == 4)
  239. || (value == limmind && insn_len == 2))
  240. {
  241. invalid = true;
  242. break;
  243. }
  244. }
  245. if (operand->flags & ARC_OPERAND_LIMM
  246. && !(operand->flags & ARC_OPERAND_DUPLICATE))
  247. *has_limm = true;
  248. }
  249. /* Check the flags. */
  250. for (flgidx = opcode->flags; *flgidx; flgidx++)
  251. {
  252. /* Get a valid flag class. */
  253. const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
  254. const unsigned *flgopridx;
  255. int foundA = 0, foundB = 0;
  256. unsigned int value;
  257. /* Check first the extensions. */
  258. if (cl_flags->flag_class & F_CLASS_EXTEND)
  259. {
  260. value = (insn & 0x1F);
  261. if (arcExtMap_condCodeName (value))
  262. continue;
  263. }
  264. /* Check for the implicit flags. */
  265. if (cl_flags->flag_class & F_CLASS_IMPLICIT)
  266. continue;
  267. for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
  268. {
  269. const struct arc_flag_operand *flg_operand =
  270. &arc_flag_operands[*flgopridx];
  271. value = (insn >> flg_operand->shift)
  272. & ((1 << flg_operand->bits) - 1);
  273. if (value == flg_operand->code)
  274. foundA = 1;
  275. if (value)
  276. foundB = 1;
  277. }
  278. if (!foundA && foundB)
  279. {
  280. invalid = true;
  281. break;
  282. }
  283. }
  284. if (invalid)
  285. continue;
  286. if (insn_len == 4
  287. && overlaps)
  288. {
  289. warn_p = true;
  290. t_op = opcode;
  291. if (skip_this_opcode (opcode))
  292. continue;
  293. }
  294. /* The instruction is valid. */
  295. return opcode;
  296. }
  297. while (opcode->mask);
  298. if (warn_p)
  299. {
  300. info->fprintf_func (info->stream,
  301. _("\nWarning: disassembly may be wrong due to "
  302. "guessed opcode class choice.\n"
  303. "Use -M<class[,class]> to select the correct "
  304. "opcode class(es).\n\t\t\t\t"));
  305. return t_op;
  306. }
  307. return NULL;
  308. }
  309. /* Find opcode for INSN, trying various different sources. The instruction
  310. length in INSN_LEN will be updated if the instruction requires a LIMM
  311. extension.
  312. A pointer to the opcode is placed into OPCODE_RESULT, and ITER is
  313. initialised, ready to iterate over the operands of the found opcode. If
  314. the found opcode requires a LIMM then the LIMM value will be loaded into a
  315. field of ITER.
  316. This function returns TRUE in almost all cases, FALSE is reserved to
  317. indicate an error (failing to find an opcode is not an error) a returned
  318. result of FALSE would indicate that the disassembler can't continue.
  319. If no matching opcode is found then the returned result will be TRUE, the
  320. value placed into OPCODE_RESULT will be NULL, ITER will be undefined, and
  321. INSN_LEN will be unchanged.
  322. If a matching opcode is found, then the returned result will be TRUE, the
  323. opcode pointer is placed into OPCODE_RESULT, INSN_LEN will be increased by
  324. 4 if the instruction requires a LIMM, and the LIMM value will have been
  325. loaded into a field of ITER. Finally, ITER will have been initialised so
  326. that calls to OPERAND_ITERATOR_NEXT will iterate over the opcode's
  327. operands. */
  328. static bool
  329. find_format (bfd_vma memaddr,
  330. unsigned long long insn,
  331. unsigned int * insn_len,
  332. unsigned isa_mask,
  333. struct disassemble_info * info,
  334. const struct arc_opcode ** opcode_result,
  335. struct arc_operand_iterator * iter)
  336. {
  337. const struct arc_opcode *opcode = NULL;
  338. bool needs_limm = false;
  339. const extInstruction_t *einsn, *i;
  340. unsigned limm = 0;
  341. struct arc_disassemble_info *arc_infop = info->private_data;
  342. /* First, try the extension instructions. */
  343. if (*insn_len == 4)
  344. {
  345. einsn = arcExtMap_insn (OPCODE_32BIT_INSN (insn), insn);
  346. for (i = einsn; (i != NULL) && (opcode == NULL); i = i->next)
  347. {
  348. const char *errmsg = NULL;
  349. opcode = arcExtMap_genOpcode (i, isa_mask, &errmsg);
  350. if (opcode == NULL)
  351. {
  352. (*info->fprintf_func) (info->stream,
  353. _("An error occurred while generating the "
  354. "extension instruction operations"));
  355. *opcode_result = NULL;
  356. return false;
  357. }
  358. opcode = find_format_from_table (info, opcode, insn, *insn_len,
  359. isa_mask, &needs_limm, false);
  360. }
  361. }
  362. /* Then, try finding the first match in the opcode table. */
  363. if (opcode == NULL)
  364. opcode = find_format_from_table (info, arc_opcodes, insn, *insn_len,
  365. isa_mask, &needs_limm, true);
  366. if (opcode != NULL && needs_limm)
  367. {
  368. bfd_byte buffer[4];
  369. int status;
  370. status = (*info->read_memory_func) (memaddr + *insn_len, buffer,
  371. 4, info);
  372. if (status != 0)
  373. {
  374. opcode = NULL;
  375. }
  376. else
  377. {
  378. limm = ARRANGE_ENDIAN (info, buffer);
  379. *insn_len += 4;
  380. }
  381. }
  382. if (opcode != NULL)
  383. {
  384. iter->insn = insn;
  385. iter->limm = limm;
  386. iter->opcode = opcode;
  387. iter->opidx = opcode->operands;
  388. }
  389. *opcode_result = opcode;
  390. /* Update private data. */
  391. arc_infop->opcode = opcode;
  392. arc_infop->limm = limm;
  393. arc_infop->limm_p = needs_limm;
  394. return true;
  395. }
  396. static void
  397. print_flags (const struct arc_opcode *opcode,
  398. unsigned long long *insn,
  399. struct disassemble_info *info)
  400. {
  401. const unsigned char *flgidx;
  402. unsigned int value;
  403. struct arc_disassemble_info *arc_infop = info->private_data;
  404. /* Now extract and print the flags. */
  405. for (flgidx = opcode->flags; *flgidx; flgidx++)
  406. {
  407. /* Get a valid flag class. */
  408. const struct arc_flag_class *cl_flags = &arc_flag_classes[*flgidx];
  409. const unsigned *flgopridx;
  410. /* Check first the extensions. */
  411. if (cl_flags->flag_class & F_CLASS_EXTEND)
  412. {
  413. const char *name;
  414. value = (insn[0] & 0x1F);
  415. name = arcExtMap_condCodeName (value);
  416. if (name)
  417. {
  418. (*info->fprintf_func) (info->stream, ".%s", name);
  419. continue;
  420. }
  421. }
  422. for (flgopridx = cl_flags->flags; *flgopridx; ++flgopridx)
  423. {
  424. const struct arc_flag_operand *flg_operand =
  425. &arc_flag_operands[*flgopridx];
  426. /* Implicit flags are only used for the insn decoder. */
  427. if (cl_flags->flag_class & F_CLASS_IMPLICIT)
  428. {
  429. if (cl_flags->flag_class & F_CLASS_COND)
  430. arc_infop->condition_code = flg_operand->code;
  431. else if (cl_flags->flag_class & F_CLASS_WB)
  432. arc_infop->writeback_mode = flg_operand->code;
  433. else if (cl_flags->flag_class & F_CLASS_ZZ)
  434. info->data_size = flg_operand->code;
  435. continue;
  436. }
  437. if (!flg_operand->favail)
  438. continue;
  439. value = (insn[0] >> flg_operand->shift)
  440. & ((1 << flg_operand->bits) - 1);
  441. if (value == flg_operand->code)
  442. {
  443. /* FIXME!: print correctly nt/t flag. */
  444. if (!special_flag_p (opcode->name, flg_operand->name))
  445. (*info->fprintf_func) (info->stream, ".");
  446. else if (info->insn_type == dis_dref)
  447. {
  448. switch (flg_operand->name[0])
  449. {
  450. case 'b':
  451. info->data_size = 1;
  452. break;
  453. case 'h':
  454. case 'w':
  455. info->data_size = 2;
  456. break;
  457. default:
  458. info->data_size = 4;
  459. break;
  460. }
  461. }
  462. if (flg_operand->name[0] == 'd'
  463. && flg_operand->name[1] == 0)
  464. info->branch_delay_insns = 1;
  465. /* Check if it is a conditional flag. */
  466. if (cl_flags->flag_class & F_CLASS_COND)
  467. {
  468. if (info->insn_type == dis_jsr)
  469. info->insn_type = dis_condjsr;
  470. else if (info->insn_type == dis_branch)
  471. info->insn_type = dis_condbranch;
  472. arc_infop->condition_code = flg_operand->code;
  473. }
  474. /* Check for the write back modes. */
  475. if (cl_flags->flag_class & F_CLASS_WB)
  476. arc_infop->writeback_mode = flg_operand->code;
  477. (*info->fprintf_func) (info->stream, "%s", flg_operand->name);
  478. }
  479. }
  480. }
  481. }
  482. static const char *
  483. get_auxreg (const struct arc_opcode *opcode,
  484. int value,
  485. unsigned isa_mask)
  486. {
  487. const char *name;
  488. unsigned int i;
  489. const struct arc_aux_reg *auxr = &arc_aux_regs[0];
  490. if (opcode->insn_class != AUXREG)
  491. return NULL;
  492. name = arcExtMap_auxRegName (value);
  493. if (name)
  494. return name;
  495. for (i = 0; i < arc_num_aux_regs; i++, auxr++)
  496. {
  497. if (!(auxr->cpu & isa_mask))
  498. continue;
  499. if (auxr->subclass != NONE)
  500. return NULL;
  501. if (auxr->address == value)
  502. return auxr->name;
  503. }
  504. return NULL;
  505. }
  506. /* Convert a value representing an address type to a string used to refer to
  507. the address type in assembly code. */
  508. static const char *
  509. get_addrtype (int value)
  510. {
  511. if (value < 0 || value > addrtypenames_max)
  512. return addrtypeunknown;
  513. return addrtypenames[value];
  514. }
  515. /* Calculate the instruction length for an instruction starting with MSB
  516. and LSB, the most and least significant byte. The ISA_MASK is used to
  517. filter the instructions considered to only those that are part of the
  518. current architecture.
  519. The instruction lengths are calculated from the ARC_OPCODE table, and
  520. cached for later use. */
  521. static unsigned int
  522. arc_insn_length (bfd_byte msb, bfd_byte lsb, struct disassemble_info *info)
  523. {
  524. bfd_byte major_opcode = msb >> 3;
  525. switch (info->mach)
  526. {
  527. case bfd_mach_arc_arc700:
  528. /* The nps400 extension set requires this special casing of the
  529. instruction length calculation. Right now this is not causing any
  530. problems as none of the known extensions overlap in opcode space,
  531. but, if they ever do then we might need to start carrying
  532. information around in the elf about which extensions are in use. */
  533. if (major_opcode == 0xb)
  534. {
  535. bfd_byte minor_opcode = lsb & 0x1f;
  536. if (minor_opcode < 4)
  537. return 6;
  538. else if (minor_opcode == 0x10 || minor_opcode == 0x11)
  539. return 8;
  540. }
  541. if (major_opcode == 0xa)
  542. {
  543. return 8;
  544. }
  545. /* Fall through. */
  546. case bfd_mach_arc_arc600:
  547. return (major_opcode > 0xb) ? 2 : 4;
  548. break;
  549. case bfd_mach_arc_arcv2:
  550. return (major_opcode > 0x7) ? 2 : 4;
  551. break;
  552. default:
  553. return 0;
  554. }
  555. }
  556. /* Extract and return the value of OPERAND from the instruction whose value
  557. is held in the array INSN. */
  558. static int
  559. extract_operand_value (const struct arc_operand *operand,
  560. unsigned long long insn,
  561. unsigned limm)
  562. {
  563. int value;
  564. /* Read the limm operand, if required. */
  565. if (operand->flags & ARC_OPERAND_LIMM)
  566. /* The second part of the instruction value will have been loaded as
  567. part of the find_format call made earlier. */
  568. value = limm;
  569. else
  570. {
  571. if (operand->extract)
  572. value = (*operand->extract) (insn, (bool *) NULL);
  573. else
  574. {
  575. if (operand->flags & ARC_OPERAND_ALIGNED32)
  576. {
  577. value = (insn >> operand->shift)
  578. & ((1 << (operand->bits - 2)) - 1);
  579. value = value << 2;
  580. }
  581. else
  582. {
  583. value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
  584. }
  585. if (operand->flags & ARC_OPERAND_SIGNED)
  586. {
  587. int signbit = 1 << (operand->bits - 1);
  588. value = (value ^ signbit) - signbit;
  589. }
  590. }
  591. }
  592. return value;
  593. }
  594. /* Find the next operand, and the operands value from ITER. Return TRUE if
  595. there is another operand, otherwise return FALSE. If there is an
  596. operand returned then the operand is placed into OPERAND, and the value
  597. into VALUE. If there is no operand returned then OPERAND and VALUE are
  598. unchanged. */
  599. static bool
  600. operand_iterator_next (struct arc_operand_iterator *iter,
  601. const struct arc_operand **operand,
  602. int *value)
  603. {
  604. if (*iter->opidx == 0)
  605. {
  606. *operand = NULL;
  607. return false;
  608. }
  609. *operand = &arc_operands[*iter->opidx];
  610. *value = extract_operand_value (*operand, iter->insn, iter->limm);
  611. iter->opidx++;
  612. return true;
  613. }
  614. /* Helper for parsing the options. */
  615. static void
  616. parse_option (const char *option)
  617. {
  618. if (disassembler_options_cmp (option, "dsp") == 0)
  619. add_to_decodelist (DSP, NONE);
  620. else if (disassembler_options_cmp (option, "spfp") == 0)
  621. add_to_decodelist (FLOAT, SPX);
  622. else if (disassembler_options_cmp (option, "dpfp") == 0)
  623. add_to_decodelist (FLOAT, DPX);
  624. else if (disassembler_options_cmp (option, "quarkse_em") == 0)
  625. {
  626. add_to_decodelist (FLOAT, DPX);
  627. add_to_decodelist (FLOAT, SPX);
  628. add_to_decodelist (FLOAT, QUARKSE1);
  629. add_to_decodelist (FLOAT, QUARKSE2);
  630. }
  631. else if (disassembler_options_cmp (option, "fpuda") == 0)
  632. add_to_decodelist (FLOAT, DPA);
  633. else if (disassembler_options_cmp (option, "nps400") == 0)
  634. {
  635. add_to_decodelist (ACL, NPS400);
  636. add_to_decodelist (ARITH, NPS400);
  637. add_to_decodelist (BITOP, NPS400);
  638. add_to_decodelist (BMU, NPS400);
  639. add_to_decodelist (CONTROL, NPS400);
  640. add_to_decodelist (DMA, NPS400);
  641. add_to_decodelist (DPI, NPS400);
  642. add_to_decodelist (MEMORY, NPS400);
  643. add_to_decodelist (MISC, NPS400);
  644. add_to_decodelist (NET, NPS400);
  645. add_to_decodelist (PMU, NPS400);
  646. add_to_decodelist (PROTOCOL_DECODE, NPS400);
  647. add_to_decodelist (ULTRAIP, NPS400);
  648. }
  649. else if (disassembler_options_cmp (option, "fpus") == 0)
  650. {
  651. add_to_decodelist (FLOAT, SP);
  652. add_to_decodelist (FLOAT, CVT);
  653. }
  654. else if (disassembler_options_cmp (option, "fpud") == 0)
  655. {
  656. add_to_decodelist (FLOAT, DP);
  657. add_to_decodelist (FLOAT, CVT);
  658. }
  659. else if (startswith (option, "hex"))
  660. print_hex = true;
  661. else
  662. /* xgettext:c-format */
  663. opcodes_error_handler (_("unrecognised disassembler option: %s"), option);
  664. }
  665. #define ARC_CPU_TYPE_A6xx(NAME,EXTRA) \
  666. { #NAME, ARC_OPCODE_ARC600, "ARC600" }
  667. #define ARC_CPU_TYPE_A7xx(NAME,EXTRA) \
  668. { #NAME, ARC_OPCODE_ARC700, "ARC700" }
  669. #define ARC_CPU_TYPE_AV2EM(NAME,EXTRA) \
  670. { #NAME, ARC_OPCODE_ARCv2EM, "ARC EM" }
  671. #define ARC_CPU_TYPE_AV2HS(NAME,EXTRA) \
  672. { #NAME, ARC_OPCODE_ARCv2HS, "ARC HS" }
  673. #define ARC_CPU_TYPE_NONE \
  674. { 0, 0, 0 }
  675. /* A table of CPU names and opcode sets. */
  676. static const struct cpu_type
  677. {
  678. const char *name;
  679. unsigned flags;
  680. const char *isa;
  681. }
  682. cpu_types[] =
  683. {
  684. #include "elf/arc-cpu.def"
  685. };
  686. /* Helper for parsing the CPU options. Accept any of the ARC architectures
  687. values. OPTION should be a value passed to cpu=. */
  688. static unsigned
  689. parse_cpu_option (const char *option)
  690. {
  691. int i;
  692. for (i = 0; cpu_types[i].name; ++i)
  693. {
  694. if (!disassembler_options_cmp (cpu_types[i].name, option))
  695. {
  696. return cpu_types[i].flags;
  697. }
  698. }
  699. /* xgettext:c-format */
  700. opcodes_error_handler (_("unrecognised disassembler CPU option: %s"), option);
  701. return ARC_OPCODE_NONE;
  702. }
  703. /* Go over the options list and parse it. */
  704. static void
  705. parse_disassembler_options (const char *options)
  706. {
  707. const char *option;
  708. if (options == NULL)
  709. return;
  710. /* Disassembler might be reused for difference CPU's, and cpu option set for
  711. the first one shouldn't be applied to second (which might not have
  712. explicit cpu in its options. Therefore it is required to reset enforced
  713. CPU when new options are being parsed. */
  714. enforced_isa_mask = ARC_OPCODE_NONE;
  715. FOR_EACH_DISASSEMBLER_OPTION (option, options)
  716. {
  717. /* A CPU option? Cannot use STRING_COMMA_LEN because strncmp is also a
  718. preprocessor macro. */
  719. if (strncmp (option, "cpu=", 4) == 0)
  720. /* Strip leading `cpu=`. */
  721. enforced_isa_mask = parse_cpu_option (option + 4);
  722. else
  723. parse_option (option);
  724. }
  725. }
  726. /* Return the instruction type for an instruction described by OPCODE. */
  727. static enum dis_insn_type
  728. arc_opcode_to_insn_type (const struct arc_opcode *opcode)
  729. {
  730. enum dis_insn_type insn_type;
  731. switch (opcode->insn_class)
  732. {
  733. case BRANCH:
  734. case BBIT0:
  735. case BBIT1:
  736. case BI:
  737. case BIH:
  738. case BRCC:
  739. case EI:
  740. case JLI:
  741. case JUMP:
  742. case LOOP:
  743. if (!strncmp (opcode->name, "bl", 2)
  744. || !strncmp (opcode->name, "jl", 2))
  745. {
  746. if (opcode->subclass == COND)
  747. insn_type = dis_condjsr;
  748. else
  749. insn_type = dis_jsr;
  750. }
  751. else
  752. {
  753. if (opcode->subclass == COND)
  754. insn_type = dis_condbranch;
  755. else
  756. insn_type = dis_branch;
  757. }
  758. break;
  759. case LOAD:
  760. case STORE:
  761. case MEMORY:
  762. case ENTER:
  763. case PUSH:
  764. case POP:
  765. insn_type = dis_dref;
  766. break;
  767. case LEAVE:
  768. insn_type = dis_branch;
  769. break;
  770. default:
  771. insn_type = dis_nonbranch;
  772. break;
  773. }
  774. return insn_type;
  775. }
  776. /* Disassemble ARC instructions. */
  777. static int
  778. print_insn_arc (bfd_vma memaddr,
  779. struct disassemble_info *info)
  780. {
  781. bfd_byte buffer[8];
  782. unsigned int highbyte, lowbyte;
  783. int status;
  784. unsigned int insn_len;
  785. unsigned long long insn = 0;
  786. unsigned isa_mask = ARC_OPCODE_NONE;
  787. const struct arc_opcode *opcode;
  788. bool need_comma;
  789. bool open_braket;
  790. int size;
  791. const struct arc_operand *operand;
  792. int value, vpcl;
  793. struct arc_operand_iterator iter;
  794. struct arc_disassemble_info *arc_infop;
  795. bool rpcl = false, rset = false;
  796. if (info->disassembler_options)
  797. {
  798. parse_disassembler_options (info->disassembler_options);
  799. /* Avoid repeated parsing of the options. */
  800. info->disassembler_options = NULL;
  801. }
  802. if (info->private_data == NULL && !init_arc_disasm_info (info))
  803. return -1;
  804. memset (&iter, 0, sizeof (iter));
  805. highbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 1 : 0);
  806. lowbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 0 : 1);
  807. /* Figure out CPU type, unless it was enforced via disassembler options. */
  808. if (enforced_isa_mask == ARC_OPCODE_NONE)
  809. {
  810. Elf_Internal_Ehdr *header = NULL;
  811. if (info->section && info->section->owner)
  812. header = elf_elfheader (info->section->owner);
  813. switch (info->mach)
  814. {
  815. case bfd_mach_arc_arc700:
  816. isa_mask = ARC_OPCODE_ARC700;
  817. break;
  818. case bfd_mach_arc_arc600:
  819. isa_mask = ARC_OPCODE_ARC600;
  820. break;
  821. case bfd_mach_arc_arcv2:
  822. default:
  823. isa_mask = ARC_OPCODE_ARCv2EM;
  824. /* TODO: Perhaps remove definition of header since it is only used at
  825. this location. */
  826. if (header != NULL
  827. && (header->e_flags & EF_ARC_MACH_MSK) == EF_ARC_CPU_ARCV2HS)
  828. isa_mask = ARC_OPCODE_ARCv2HS;
  829. break;
  830. }
  831. }
  832. else
  833. isa_mask = enforced_isa_mask;
  834. if (isa_mask == ARC_OPCODE_ARCv2HS)
  835. {
  836. /* FPU instructions are not extensions for HS. */
  837. add_to_decodelist (FLOAT, SP);
  838. add_to_decodelist (FLOAT, DP);
  839. add_to_decodelist (FLOAT, CVT);
  840. }
  841. /* This variable may be set by the instruction decoder. It suggests
  842. the number of bytes objdump should display on a single line. If
  843. the instruction decoder sets this, it should always set it to
  844. the same value in order to get reasonable looking output. */
  845. info->bytes_per_line = 8;
  846. /* In the next lines, we set two info variables control the way
  847. objdump displays the raw data. For example, if bytes_per_line is
  848. 8 and bytes_per_chunk is 4, the output will look like this:
  849. 00: 00000000 00000000
  850. with the chunks displayed according to "display_endian". */
  851. if (info->section
  852. && !(info->section->flags & SEC_CODE))
  853. {
  854. /* This is not a CODE section. */
  855. switch (info->section->size)
  856. {
  857. case 1:
  858. case 2:
  859. case 4:
  860. size = info->section->size;
  861. break;
  862. default:
  863. size = (info->section->size & 0x01) ? 1 : 4;
  864. break;
  865. }
  866. info->bytes_per_chunk = 1;
  867. info->display_endian = info->endian;
  868. }
  869. else
  870. {
  871. size = 2;
  872. info->bytes_per_chunk = 2;
  873. info->display_endian = info->endian;
  874. }
  875. /* Read the insn into a host word. */
  876. status = (*info->read_memory_func) (memaddr, buffer, size, info);
  877. if (status != 0)
  878. {
  879. (*info->memory_error_func) (status, memaddr, info);
  880. return -1;
  881. }
  882. if (info->section
  883. && !(info->section->flags & SEC_CODE))
  884. {
  885. /* Data section. */
  886. unsigned long data;
  887. data = bfd_get_bits (buffer, size * 8,
  888. info->display_endian == BFD_ENDIAN_BIG);
  889. switch (size)
  890. {
  891. case 1:
  892. (*info->fprintf_func) (info->stream, ".byte\t0x%02lx", data);
  893. break;
  894. case 2:
  895. (*info->fprintf_func) (info->stream, ".short\t0x%04lx", data);
  896. break;
  897. case 4:
  898. (*info->fprintf_func) (info->stream, ".word\t0x%08lx", data);
  899. break;
  900. default:
  901. return -1;
  902. }
  903. return size;
  904. }
  905. insn_len = arc_insn_length (buffer[highbyte], buffer[lowbyte], info);
  906. pr_debug ("instruction length = %d bytes\n", insn_len);
  907. if (insn_len == 0)
  908. return -1;
  909. arc_infop = info->private_data;
  910. arc_infop->insn_len = insn_len;
  911. switch (insn_len)
  912. {
  913. case 2:
  914. insn = (buffer[highbyte] << 8) | buffer[lowbyte];
  915. break;
  916. case 4:
  917. {
  918. /* This is a long instruction: Read the remaning 2 bytes. */
  919. status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 2, info);
  920. if (status != 0)
  921. {
  922. (*info->memory_error_func) (status, memaddr + 2, info);
  923. return -1;
  924. }
  925. insn = (unsigned long long) ARRANGE_ENDIAN (info, buffer);
  926. }
  927. break;
  928. case 6:
  929. {
  930. status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 4, info);
  931. if (status != 0)
  932. {
  933. (*info->memory_error_func) (status, memaddr + 2, info);
  934. return -1;
  935. }
  936. insn = (unsigned long long) ARRANGE_ENDIAN (info, &buffer[2]);
  937. insn |= ((unsigned long long) buffer[highbyte] << 40)
  938. | ((unsigned long long) buffer[lowbyte] << 32);
  939. }
  940. break;
  941. case 8:
  942. {
  943. status = (*info->read_memory_func) (memaddr + 2, &buffer[2], 6, info);
  944. if (status != 0)
  945. {
  946. (*info->memory_error_func) (status, memaddr + 2, info);
  947. return -1;
  948. }
  949. insn =
  950. ((((unsigned long long) ARRANGE_ENDIAN (info, buffer)) << 32)
  951. | ((unsigned long long) ARRANGE_ENDIAN (info, &buffer[4])));
  952. }
  953. break;
  954. default:
  955. /* There is no instruction whose length is not 2, 4, 6, or 8. */
  956. return -1;
  957. }
  958. pr_debug ("instruction value = %llx\n", insn);
  959. /* Set some defaults for the insn info. */
  960. info->insn_info_valid = 1;
  961. info->branch_delay_insns = 0;
  962. info->data_size = 4;
  963. info->insn_type = dis_nonbranch;
  964. info->target = 0;
  965. info->target2 = 0;
  966. /* FIXME to be moved in dissasemble_init_for_target. */
  967. info->disassembler_needs_relocs = true;
  968. /* Find the first match in the opcode table. */
  969. if (!find_format (memaddr, insn, &insn_len, isa_mask, info, &opcode, &iter))
  970. return -1;
  971. if (!opcode)
  972. {
  973. switch (insn_len)
  974. {
  975. case 2:
  976. (*info->fprintf_func) (info->stream, ".shor\t%#04llx",
  977. insn & 0xffff);
  978. break;
  979. case 4:
  980. (*info->fprintf_func) (info->stream, ".word\t%#08llx",
  981. insn & 0xffffffff);
  982. break;
  983. case 6:
  984. (*info->fprintf_func) (info->stream, ".long\t%#08llx",
  985. insn & 0xffffffff);
  986. (*info->fprintf_func) (info->stream, ".long\t%#04llx",
  987. (insn >> 32) & 0xffff);
  988. break;
  989. case 8:
  990. (*info->fprintf_func) (info->stream, ".long\t%#08llx",
  991. insn & 0xffffffff);
  992. (*info->fprintf_func) (info->stream, ".long\t%#08llx",
  993. insn >> 32);
  994. break;
  995. default:
  996. return -1;
  997. }
  998. info->insn_type = dis_noninsn;
  999. return insn_len;
  1000. }
  1001. /* Print the mnemonic. */
  1002. (*info->fprintf_func) (info->stream, "%s", opcode->name);
  1003. /* Preselect the insn class. */
  1004. info->insn_type = arc_opcode_to_insn_type (opcode);
  1005. pr_debug ("%s: 0x%08llx\n", opcode->name, opcode->opcode);
  1006. print_flags (opcode, &insn, info);
  1007. if (opcode->operands[0] != 0)
  1008. (*info->fprintf_func) (info->stream, "\t");
  1009. need_comma = false;
  1010. open_braket = false;
  1011. arc_infop->operands_count = 0;
  1012. /* Now extract and print the operands. */
  1013. operand = NULL;
  1014. vpcl = 0;
  1015. while (operand_iterator_next (&iter, &operand, &value))
  1016. {
  1017. if (open_braket && (operand->flags & ARC_OPERAND_BRAKET))
  1018. {
  1019. (*info->fprintf_func) (info->stream, "]");
  1020. open_braket = false;
  1021. continue;
  1022. }
  1023. /* Only take input from real operands. */
  1024. if (ARC_OPERAND_IS_FAKE (operand))
  1025. continue;
  1026. if ((operand->flags & ARC_OPERAND_IGNORE)
  1027. && (operand->flags & ARC_OPERAND_IR)
  1028. && value == -1)
  1029. continue;
  1030. if (operand->flags & ARC_OPERAND_COLON)
  1031. {
  1032. (*info->fprintf_func) (info->stream, ":");
  1033. continue;
  1034. }
  1035. if (need_comma)
  1036. (*info->fprintf_func) (info->stream, ",");
  1037. if (!open_braket && (operand->flags & ARC_OPERAND_BRAKET))
  1038. {
  1039. (*info->fprintf_func) (info->stream, "[");
  1040. open_braket = true;
  1041. need_comma = false;
  1042. continue;
  1043. }
  1044. need_comma = true;
  1045. if (operand->flags & ARC_OPERAND_PCREL)
  1046. {
  1047. rpcl = true;
  1048. vpcl = value;
  1049. rset = true;
  1050. info->target = (bfd_vma) (memaddr & ~3) + value;
  1051. }
  1052. else if (!(operand->flags & ARC_OPERAND_IR))
  1053. {
  1054. vpcl = value;
  1055. rset = true;
  1056. }
  1057. /* Print the operand as directed by the flags. */
  1058. if (operand->flags & ARC_OPERAND_IR)
  1059. {
  1060. const char *rname;
  1061. assert (value >=0 && value < 64);
  1062. rname = arcExtMap_coreRegName (value);
  1063. if (!rname)
  1064. rname = regnames[value];
  1065. (*info->fprintf_func) (info->stream, "%s", rname);
  1066. /* Check if we have a double register to print. */
  1067. if (operand->flags & ARC_OPERAND_TRUNCATE)
  1068. {
  1069. if ((value & 0x01) == 0)
  1070. {
  1071. rname = arcExtMap_coreRegName (value + 1);
  1072. if (!rname)
  1073. rname = regnames[value + 1];
  1074. }
  1075. else
  1076. rname = _("\nWarning: illegal use of double register "
  1077. "pair.\n");
  1078. (*info->fprintf_func) (info->stream, "%s", rname);
  1079. }
  1080. if (value == 63)
  1081. rpcl = true;
  1082. else
  1083. rpcl = false;
  1084. }
  1085. else if (operand->flags & ARC_OPERAND_LIMM)
  1086. {
  1087. const char *rname = get_auxreg (opcode, value, isa_mask);
  1088. if (rname && open_braket)
  1089. (*info->fprintf_func) (info->stream, "%s", rname);
  1090. else
  1091. {
  1092. (*info->fprintf_func) (info->stream, "%#x", value);
  1093. if (info->insn_type == dis_branch
  1094. || info->insn_type == dis_jsr)
  1095. info->target = (bfd_vma) value;
  1096. }
  1097. }
  1098. else if (operand->flags & ARC_OPERAND_SIGNED)
  1099. {
  1100. const char *rname = get_auxreg (opcode, value, isa_mask);
  1101. if (rname && open_braket)
  1102. (*info->fprintf_func) (info->stream, "%s", rname);
  1103. else
  1104. {
  1105. if (print_hex)
  1106. (*info->fprintf_func) (info->stream, "%#x", value);
  1107. else
  1108. (*info->fprintf_func) (info->stream, "%d", value);
  1109. }
  1110. }
  1111. else if (operand->flags & ARC_OPERAND_ADDRTYPE)
  1112. {
  1113. const char *addrtype = get_addrtype (value);
  1114. (*info->fprintf_func) (info->stream, "%s", addrtype);
  1115. /* A colon follow an address type. */
  1116. need_comma = false;
  1117. }
  1118. else
  1119. {
  1120. if (operand->flags & ARC_OPERAND_TRUNCATE
  1121. && !(operand->flags & ARC_OPERAND_ALIGNED32)
  1122. && !(operand->flags & ARC_OPERAND_ALIGNED16)
  1123. && value >= 0 && value <= 14)
  1124. {
  1125. /* Leave/Enter mnemonics. */
  1126. switch (value)
  1127. {
  1128. case 0:
  1129. need_comma = false;
  1130. break;
  1131. case 1:
  1132. (*info->fprintf_func) (info->stream, "r13");
  1133. break;
  1134. default:
  1135. (*info->fprintf_func) (info->stream, "r13-%s",
  1136. regnames[13 + value - 1]);
  1137. break;
  1138. }
  1139. rpcl = false;
  1140. rset = false;
  1141. }
  1142. else
  1143. {
  1144. const char *rname = get_auxreg (opcode, value, isa_mask);
  1145. if (rname && open_braket)
  1146. (*info->fprintf_func) (info->stream, "%s", rname);
  1147. else
  1148. (*info->fprintf_func) (info->stream, "%#x", value);
  1149. }
  1150. }
  1151. if (operand->flags & ARC_OPERAND_LIMM)
  1152. {
  1153. arc_infop->operands[arc_infop->operands_count].kind
  1154. = ARC_OPERAND_KIND_LIMM;
  1155. /* It is not important to have exactly the LIMM indicator
  1156. here. */
  1157. arc_infop->operands[arc_infop->operands_count].value = 63;
  1158. }
  1159. else
  1160. {
  1161. arc_infop->operands[arc_infop->operands_count].value = value;
  1162. arc_infop->operands[arc_infop->operands_count].kind
  1163. = (operand->flags & ARC_OPERAND_IR
  1164. ? ARC_OPERAND_KIND_REG
  1165. : ARC_OPERAND_KIND_SHIMM);
  1166. }
  1167. arc_infop->operands_count ++;
  1168. }
  1169. /* Pretty print extra info for pc-relative operands. */
  1170. if (rpcl && rset)
  1171. {
  1172. if (info->flags & INSN_HAS_RELOC)
  1173. /* If the instruction has a reloc associated with it, then the
  1174. offset field in the instruction will actually be the addend
  1175. for the reloc. (We are using REL type relocs). In such
  1176. cases, we can ignore the pc when computing addresses, since
  1177. the addend is not currently pc-relative. */
  1178. memaddr = 0;
  1179. (*info->fprintf_func) (info->stream, "\t;");
  1180. (*info->print_address_func) ((memaddr & ~3) + vpcl, info);
  1181. }
  1182. return insn_len;
  1183. }
  1184. disassembler_ftype
  1185. arc_get_disassembler (bfd *abfd)
  1186. {
  1187. /* BFD my be absent, if opcodes is invoked from the debugger that
  1188. has connected to remote target and doesn't have an ELF file. */
  1189. if (abfd != NULL)
  1190. {
  1191. /* Read the extension insns and registers, if any. */
  1192. build_ARC_extmap (abfd);
  1193. #ifdef DEBUG
  1194. dump_ARC_extmap ();
  1195. #endif
  1196. }
  1197. return print_insn_arc;
  1198. }
  1199. /* Indices into option argument vector for options that do require
  1200. an argument. Use ARC_OPTION_ARG_NONE for options that don't
  1201. expect an argument. */
  1202. typedef enum
  1203. {
  1204. ARC_OPTION_ARG_NONE = -1,
  1205. ARC_OPTION_ARG_ARCH,
  1206. ARC_OPTION_ARG_SIZE
  1207. } arc_option_arg_t;
  1208. /* Valid ARC disassembler options. */
  1209. static struct
  1210. {
  1211. const char *name;
  1212. const char *description;
  1213. arc_option_arg_t arg;
  1214. } arc_options[] =
  1215. {
  1216. { "cpu=", N_("Enforce the designated architecture while decoding."),
  1217. ARC_OPTION_ARG_ARCH },
  1218. { "dsp", N_("Recognize DSP instructions."),
  1219. ARC_OPTION_ARG_NONE },
  1220. { "spfp", N_("Recognize FPX SP instructions."),
  1221. ARC_OPTION_ARG_NONE },
  1222. { "dpfp", N_("Recognize FPX DP instructions."),
  1223. ARC_OPTION_ARG_NONE },
  1224. { "quarkse_em", N_("Recognize FPU QuarkSE-EM instructions."),
  1225. ARC_OPTION_ARG_NONE },
  1226. { "fpuda", N_("Recognize double assist FPU instructions."),
  1227. ARC_OPTION_ARG_NONE },
  1228. { "fpus", N_("Recognize single precision FPU instructions."),
  1229. ARC_OPTION_ARG_NONE },
  1230. { "fpud", N_("Recognize double precision FPU instructions."),
  1231. ARC_OPTION_ARG_NONE },
  1232. { "nps400", N_("Recognize NPS400 instructions."),
  1233. ARC_OPTION_ARG_NONE },
  1234. { "hex", N_("Use only hexadecimal number to print immediates."),
  1235. ARC_OPTION_ARG_NONE }
  1236. };
  1237. /* Populate the structure for representing ARC's disassembly options.
  1238. Such a dynamic initialization is desired, because it makes the maintenance
  1239. easier and also gdb uses this to enable the "disassembler-option". */
  1240. const disasm_options_and_args_t *
  1241. disassembler_options_arc (void)
  1242. {
  1243. static disasm_options_and_args_t *opts_and_args;
  1244. if (opts_and_args == NULL)
  1245. {
  1246. disasm_option_arg_t *args;
  1247. disasm_options_t *opts;
  1248. size_t i;
  1249. const size_t nr_of_options = ARRAY_SIZE (arc_options);
  1250. /* There is a null element at the end of CPU_TYPES, therefore
  1251. NR_OF_CPUS is actually 1 more and that is desired here too. */
  1252. const size_t nr_of_cpus = ARRAY_SIZE (cpu_types);
  1253. opts_and_args = XNEW (disasm_options_and_args_t);
  1254. opts_and_args->args
  1255. = XNEWVEC (disasm_option_arg_t, ARC_OPTION_ARG_SIZE + 1);
  1256. opts_and_args->options.name
  1257. = XNEWVEC (const char *, nr_of_options + 1);
  1258. opts_and_args->options.description
  1259. = XNEWVEC (const char *, nr_of_options + 1);
  1260. opts_and_args->options.arg
  1261. = XNEWVEC (const disasm_option_arg_t *, nr_of_options + 1);
  1262. /* Populate the arguments for "cpu=" option. */
  1263. args = opts_and_args->args;
  1264. args[ARC_OPTION_ARG_ARCH].name = "ARCH";
  1265. args[ARC_OPTION_ARG_ARCH].values = XNEWVEC (const char *, nr_of_cpus);
  1266. for (i = 0; i < nr_of_cpus; ++i)
  1267. args[ARC_OPTION_ARG_ARCH].values[i] = cpu_types[i].name;
  1268. args[ARC_OPTION_ARG_SIZE].name = NULL;
  1269. args[ARC_OPTION_ARG_SIZE].values = NULL;
  1270. /* Populate the options. */
  1271. opts = &opts_and_args->options;
  1272. for (i = 0; i < nr_of_options; ++i)
  1273. {
  1274. opts->name[i] = arc_options[i].name;
  1275. opts->description[i] = arc_options[i].description;
  1276. if (arc_options[i].arg != ARC_OPTION_ARG_NONE)
  1277. opts->arg[i] = &args[arc_options[i].arg];
  1278. else
  1279. opts->arg[i] = NULL;
  1280. }
  1281. opts->name[nr_of_options] = NULL;
  1282. opts->description[nr_of_options] = NULL;
  1283. opts->arg[nr_of_options] = NULL;
  1284. }
  1285. return opts_and_args;
  1286. }
  1287. void
  1288. print_arc_disassembler_options (FILE *stream)
  1289. {
  1290. const disasm_options_and_args_t *opts_and_args;
  1291. const disasm_option_arg_t *args;
  1292. const disasm_options_t *opts;
  1293. size_t i, j;
  1294. size_t max_len = 0;
  1295. opts_and_args = disassembler_options_arc ();
  1296. opts = &opts_and_args->options;
  1297. args = opts_and_args->args;
  1298. fprintf (stream, _("\nThe following ARC specific disassembler options are"
  1299. " supported for use \nwith the -M switch (multiple"
  1300. " options should be separated by commas):\n"));
  1301. /* Find the maximum length for printing options (and their arg name). */
  1302. for (i = 0; opts->name[i] != NULL; ++i)
  1303. {
  1304. size_t len = strlen (opts->name[i]);
  1305. len += (opts->arg[i]) ? strlen (opts->arg[i]->name) : 0;
  1306. max_len = (len > max_len) ? len : max_len;
  1307. }
  1308. /* Print the options, their arg and description, if any. */
  1309. for (i = 0, ++max_len; opts->name[i] != NULL; ++i)
  1310. {
  1311. fprintf (stream, " %s", opts->name[i]);
  1312. if (opts->arg[i] != NULL)
  1313. fprintf (stream, "%s", opts->arg[i]->name);
  1314. if (opts->description[i] != NULL)
  1315. {
  1316. size_t len = strlen (opts->name[i]);
  1317. len += (opts->arg[i]) ? strlen (opts->arg[i]->name) : 0;
  1318. fprintf (stream,
  1319. "%*c %s", (int) (max_len - len), ' ', opts->description[i]);
  1320. }
  1321. fprintf (stream, _("\n"));
  1322. }
  1323. /* Print the possible values of an argument. */
  1324. for (i = 0; args[i].name != NULL; ++i)
  1325. {
  1326. size_t len = 3;
  1327. fprintf (stream, _("\n\
  1328. For the options above, the following values are supported for \"%s\":\n "),
  1329. args[i].name);
  1330. for (j = 0; args[i].values[j] != NULL; ++j)
  1331. {
  1332. fprintf (stream, " %s", args[i].values[j]);
  1333. len += strlen (args[i].values[j]) + 1;
  1334. /* reset line if printed too long. */
  1335. if (len >= 78)
  1336. {
  1337. fprintf (stream, _("\n "));
  1338. len = 3;
  1339. }
  1340. }
  1341. fprintf (stream, _("\n"));
  1342. }
  1343. fprintf (stream, _("\n"));
  1344. }
  1345. void arc_insn_decode (bfd_vma addr,
  1346. struct disassemble_info *info,
  1347. disassembler_ftype disasm_func,
  1348. struct arc_instruction *insn)
  1349. {
  1350. const struct arc_opcode *opcode;
  1351. struct arc_disassemble_info *arc_infop;
  1352. /* Ensure that insn would be in the reset state. */
  1353. memset (insn, 0, sizeof (struct arc_instruction));
  1354. /* There was an error when disassembling, for example memory read error. */
  1355. if (disasm_func (addr, info) < 0)
  1356. {
  1357. insn->valid = false;
  1358. return;
  1359. }
  1360. assert (info->private_data != NULL);
  1361. arc_infop = info->private_data;
  1362. insn->length = arc_infop->insn_len;;
  1363. insn->address = addr;
  1364. /* Quick exit if memory at this address is not an instruction. */
  1365. if (info->insn_type == dis_noninsn)
  1366. {
  1367. insn->valid = false;
  1368. return;
  1369. }
  1370. insn->valid = true;
  1371. opcode = (const struct arc_opcode *) arc_infop->opcode;
  1372. insn->insn_class = opcode->insn_class;
  1373. insn->limm_value = arc_infop->limm;
  1374. insn->limm_p = arc_infop->limm_p;
  1375. insn->is_control_flow = (info->insn_type == dis_branch
  1376. || info->insn_type == dis_condbranch
  1377. || info->insn_type == dis_jsr
  1378. || info->insn_type == dis_condjsr);
  1379. insn->has_delay_slot = info->branch_delay_insns;
  1380. insn->writeback_mode
  1381. = (enum arc_ldst_writeback_mode) arc_infop->writeback_mode;
  1382. insn->data_size_mode = info->data_size;
  1383. insn->condition_code = arc_infop->condition_code;
  1384. memcpy (insn->operands, arc_infop->operands,
  1385. sizeof (struct arc_insn_operand) * MAX_INSN_ARGS);
  1386. insn->operands_count = arc_infop->operands_count;
  1387. }
  1388. /* Local variables:
  1389. eval: (c-set-style "gnu")
  1390. indent-tabs-mode: t
  1391. End: */