cris-dis.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. /* Disassembler code for CRIS.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Axis Communications AB, Lund, Sweden.
  4. Written by Hans-Peter Nilsson.
  5. This file is part of the GNU opcodes library.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "dis-asm.h"
  20. #include "opcode/cris.h"
  21. #include "libiberty.h"
  22. /* No instruction will be disassembled longer than this. In theory, and
  23. in silicon, address prefixes can be cascaded. In practice, cascading
  24. is not used by GCC, and not supported by the assembler. */
  25. #ifndef MAX_BYTES_PER_CRIS_INSN
  26. #define MAX_BYTES_PER_CRIS_INSN 8
  27. #endif
  28. /* Whether or not to decode prefixes, folding it into the following
  29. instruction. FIXME: Make this optional later. */
  30. #ifndef PARSE_PREFIX
  31. #define PARSE_PREFIX 1
  32. #endif
  33. /* Sometimes we prefix all registers with this character. */
  34. #define REGISTER_PREFIX_CHAR '$'
  35. /* Whether or not to trace the following sequence:
  36. sub* X,r%d
  37. bound* Y,r%d
  38. adds.w [pc+r%d.w],pc
  39. This is the assembly form of a switch-statement in C.
  40. The "sub is optional. If there is none, then X will be zero.
  41. X is the value of the first case,
  42. Y is the number of cases (including default).
  43. This results in case offsets printed on the form:
  44. case N: -> case_address
  45. where N is an estimation on the corresponding 'case' operand in C,
  46. and case_address is where execution of that case continues after the
  47. sequence presented above.
  48. The old style of output was to print the offsets as instructions,
  49. which made it hard to follow "case"-constructs in the disassembly,
  50. and caused a lot of annoying warnings about undefined instructions.
  51. FIXME: Make this optional later. */
  52. #ifndef TRACE_CASE
  53. #define TRACE_CASE (disdata->trace_case)
  54. #endif
  55. enum cris_disass_family
  56. { cris_dis_v0_v10, cris_dis_common_v10_v32, cris_dis_v32 };
  57. /* Stored in the disasm_info->private_data member. */
  58. struct cris_disasm_data
  59. {
  60. /* Whether to print something less confusing if we find something
  61. matching a switch-construct. */
  62. bool trace_case;
  63. /* Whether this code is flagged as crisv32. FIXME: Should be an enum
  64. that includes "compatible". */
  65. enum cris_disass_family distype;
  66. };
  67. /* Value of first element in switch. */
  68. static long case_offset = 0;
  69. /* How many more case-offsets to print. */
  70. static long case_offset_counter = 0;
  71. /* Number of case offsets. */
  72. static long no_of_case_offsets = 0;
  73. /* Candidate for next case_offset. */
  74. static long last_immediate = 0;
  75. static int cris_constraint
  76. (const char *, unsigned, unsigned, struct cris_disasm_data *);
  77. /* Parse disassembler options and store state in info. FIXME: For the
  78. time being, we abuse static variables. */
  79. static bool
  80. cris_parse_disassembler_options (disassemble_info *info,
  81. enum cris_disass_family distype)
  82. {
  83. struct cris_disasm_data *disdata;
  84. info->private_data = calloc (1, sizeof (struct cris_disasm_data));
  85. disdata = (struct cris_disasm_data *) info->private_data;
  86. if (disdata == NULL)
  87. return false;
  88. /* Default true. */
  89. disdata->trace_case
  90. = (info->disassembler_options == NULL
  91. || (strcmp (info->disassembler_options, "nocase") != 0));
  92. disdata->distype = distype;
  93. return true;
  94. }
  95. static const struct cris_spec_reg *
  96. spec_reg_info (unsigned int sreg, enum cris_disass_family distype)
  97. {
  98. int i;
  99. for (i = 0; cris_spec_regs[i].name != NULL; i++)
  100. {
  101. if (cris_spec_regs[i].number == sreg)
  102. {
  103. if (distype == cris_dis_v32)
  104. switch (cris_spec_regs[i].applicable_version)
  105. {
  106. case cris_ver_warning:
  107. case cris_ver_version_all:
  108. case cris_ver_v3p:
  109. case cris_ver_v8p:
  110. case cris_ver_v10p:
  111. case cris_ver_v32p:
  112. /* No ambiguous sizes or register names with CRISv32. */
  113. if (cris_spec_regs[i].warning == NULL)
  114. return &cris_spec_regs[i];
  115. default:
  116. ;
  117. }
  118. else if (cris_spec_regs[i].applicable_version != cris_ver_v32p)
  119. return &cris_spec_regs[i];
  120. }
  121. }
  122. return NULL;
  123. }
  124. /* Return the number of bits in the argument. */
  125. static int
  126. number_of_bits (unsigned int val)
  127. {
  128. int bits;
  129. for (bits = 0; val != 0; val &= val - 1)
  130. bits++;
  131. return bits;
  132. }
  133. /* Get an entry in the opcode-table. */
  134. static const struct cris_opcode *
  135. get_opcode_entry (unsigned int insn,
  136. unsigned int prefix_insn,
  137. struct cris_disasm_data *disdata)
  138. {
  139. /* For non-prefixed insns, we keep a table of pointers, indexed by the
  140. insn code. Each entry is initialized when found to be NULL. */
  141. static const struct cris_opcode **opc_table = NULL;
  142. const struct cris_opcode *max_matchedp = NULL;
  143. const struct cris_opcode **prefix_opc_table = NULL;
  144. /* We hold a table for each prefix that need to be handled differently. */
  145. static const struct cris_opcode **dip_prefixes = NULL;
  146. static const struct cris_opcode **bdapq_m1_prefixes = NULL;
  147. static const struct cris_opcode **bdapq_m2_prefixes = NULL;
  148. static const struct cris_opcode **bdapq_m4_prefixes = NULL;
  149. static const struct cris_opcode **rest_prefixes = NULL;
  150. /* Allocate and clear the opcode-table. */
  151. if (opc_table == NULL)
  152. {
  153. opc_table = malloc (65536 * sizeof (opc_table[0]));
  154. if (opc_table == NULL)
  155. return NULL;
  156. memset (opc_table, 0, 65536 * sizeof (const struct cris_opcode *));
  157. dip_prefixes
  158. = malloc (65536 * sizeof (const struct cris_opcode **));
  159. if (dip_prefixes == NULL)
  160. return NULL;
  161. memset (dip_prefixes, 0, 65536 * sizeof (dip_prefixes[0]));
  162. bdapq_m1_prefixes
  163. = malloc (65536 * sizeof (const struct cris_opcode **));
  164. if (bdapq_m1_prefixes == NULL)
  165. return NULL;
  166. memset (bdapq_m1_prefixes, 0, 65536 * sizeof (bdapq_m1_prefixes[0]));
  167. bdapq_m2_prefixes
  168. = malloc (65536 * sizeof (const struct cris_opcode **));
  169. if (bdapq_m2_prefixes == NULL)
  170. return NULL;
  171. memset (bdapq_m2_prefixes, 0, 65536 * sizeof (bdapq_m2_prefixes[0]));
  172. bdapq_m4_prefixes
  173. = malloc (65536 * sizeof (const struct cris_opcode **));
  174. if (bdapq_m4_prefixes == NULL)
  175. return NULL;
  176. memset (bdapq_m4_prefixes, 0, 65536 * sizeof (bdapq_m4_prefixes[0]));
  177. rest_prefixes
  178. = malloc (65536 * sizeof (const struct cris_opcode **));
  179. if (rest_prefixes == NULL)
  180. return NULL;
  181. memset (rest_prefixes, 0, 65536 * sizeof (rest_prefixes[0]));
  182. }
  183. /* Get the right table if this is a prefix.
  184. This code is connected to cris_constraints in that it knows what
  185. prefixes play a role in recognition of patterns; the necessary
  186. state is reflected by which table is used. If constraints
  187. involving match or non-match of prefix insns are changed, then this
  188. probably needs changing too. */
  189. if (prefix_insn != NO_CRIS_PREFIX)
  190. {
  191. const struct cris_opcode *popcodep
  192. = (opc_table[prefix_insn] != NULL
  193. ? opc_table[prefix_insn]
  194. : get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata));
  195. if (popcodep == NULL)
  196. return NULL;
  197. if (popcodep->match == BDAP_QUICK_OPCODE)
  198. {
  199. /* Since some offsets are recognized with "push" macros, we
  200. have to have different tables for them. */
  201. int offset = (prefix_insn & 255);
  202. if (offset > 127)
  203. offset -= 256;
  204. switch (offset)
  205. {
  206. case -4:
  207. prefix_opc_table = bdapq_m4_prefixes;
  208. break;
  209. case -2:
  210. prefix_opc_table = bdapq_m2_prefixes;
  211. break;
  212. case -1:
  213. prefix_opc_table = bdapq_m1_prefixes;
  214. break;
  215. default:
  216. prefix_opc_table = rest_prefixes;
  217. break;
  218. }
  219. }
  220. else if (popcodep->match == DIP_OPCODE)
  221. /* We don't allow postincrement when the prefix is DIP, so use a
  222. different table for DIP. */
  223. prefix_opc_table = dip_prefixes;
  224. else
  225. prefix_opc_table = rest_prefixes;
  226. }
  227. if (prefix_insn != NO_CRIS_PREFIX
  228. && prefix_opc_table[insn] != NULL)
  229. max_matchedp = prefix_opc_table[insn];
  230. else if (prefix_insn == NO_CRIS_PREFIX && opc_table[insn] != NULL)
  231. max_matchedp = opc_table[insn];
  232. else
  233. {
  234. const struct cris_opcode *opcodep;
  235. int max_level_of_match = -1;
  236. for (opcodep = cris_opcodes;
  237. opcodep->name != NULL;
  238. opcodep++)
  239. {
  240. int level_of_match;
  241. if (disdata->distype == cris_dis_v32)
  242. {
  243. switch (opcodep->applicable_version)
  244. {
  245. case cris_ver_version_all:
  246. break;
  247. case cris_ver_v0_3:
  248. case cris_ver_v0_10:
  249. case cris_ver_v3_10:
  250. case cris_ver_sim_v0_10:
  251. case cris_ver_v8_10:
  252. case cris_ver_v10:
  253. case cris_ver_warning:
  254. continue;
  255. case cris_ver_v3p:
  256. case cris_ver_v8p:
  257. case cris_ver_v10p:
  258. case cris_ver_v32p:
  259. break;
  260. case cris_ver_v8:
  261. abort ();
  262. default:
  263. abort ();
  264. }
  265. }
  266. else
  267. {
  268. switch (opcodep->applicable_version)
  269. {
  270. case cris_ver_version_all:
  271. case cris_ver_v0_3:
  272. case cris_ver_v3p:
  273. case cris_ver_v0_10:
  274. case cris_ver_v8p:
  275. case cris_ver_v8_10:
  276. case cris_ver_v10:
  277. case cris_ver_sim_v0_10:
  278. case cris_ver_v10p:
  279. case cris_ver_warning:
  280. break;
  281. case cris_ver_v32p:
  282. continue;
  283. case cris_ver_v8:
  284. abort ();
  285. default:
  286. abort ();
  287. }
  288. }
  289. /* We give a double lead for bits matching the template in
  290. cris_opcodes. Not even, because then "move p8,r10" would
  291. be given 2 bits lead over "clear.d r10". When there's a
  292. tie, the first entry in the table wins. This is
  293. deliberate, to avoid a more complicated recognition
  294. formula. */
  295. if ((opcodep->match & insn) == opcodep->match
  296. && (opcodep->lose & insn) == 0
  297. && ((level_of_match
  298. = cris_constraint (opcodep->args,
  299. insn,
  300. prefix_insn,
  301. disdata))
  302. >= 0)
  303. && ((level_of_match
  304. += 2 * number_of_bits (opcodep->match
  305. | opcodep->lose))
  306. > max_level_of_match))
  307. {
  308. max_matchedp = opcodep;
  309. max_level_of_match = level_of_match;
  310. /* If there was a full match, never mind looking
  311. further. */
  312. if (level_of_match >= 2 * 16)
  313. break;
  314. }
  315. }
  316. /* Fill in the new entry.
  317. If there are changes to the opcode-table involving prefixes, and
  318. disassembly then does not work correctly, try removing the
  319. else-clause below that fills in the prefix-table. If that
  320. helps, you need to change the prefix_opc_table setting above, or
  321. something related. */
  322. if (prefix_insn == NO_CRIS_PREFIX)
  323. opc_table[insn] = max_matchedp;
  324. else
  325. prefix_opc_table[insn] = max_matchedp;
  326. }
  327. return max_matchedp;
  328. }
  329. /* Return -1 if the constraints of a bitwise-matched instruction say
  330. that there is no match. Otherwise return a nonnegative number
  331. indicating the confidence in the match (higher is better). */
  332. static int
  333. cris_constraint (const char *cs,
  334. unsigned int insn,
  335. unsigned int prefix_insn,
  336. struct cris_disasm_data *disdata)
  337. {
  338. int retval = 0;
  339. int tmp;
  340. int prefix_ok = 0;
  341. const char *s;
  342. for (s = cs; *s; s++)
  343. switch (*s)
  344. {
  345. case '!':
  346. /* Do not recognize "pop" if there's a prefix and then only for
  347. v0..v10. */
  348. if (prefix_insn != NO_CRIS_PREFIX
  349. || disdata->distype != cris_dis_v0_v10)
  350. return -1;
  351. break;
  352. case 'U':
  353. /* Not recognized at disassembly. */
  354. return -1;
  355. case 'M':
  356. /* Size modifier for "clear", i.e. special register 0, 4 or 8.
  357. Check that it is one of them. Only special register 12 could
  358. be mismatched, but checking for matches is more logical than
  359. checking for mismatches when there are only a few cases. */
  360. tmp = ((insn >> 12) & 0xf);
  361. if (tmp != 0 && tmp != 4 && tmp != 8)
  362. return -1;
  363. break;
  364. case 'm':
  365. if ((insn & 0x30) == 0x30)
  366. return -1;
  367. break;
  368. case 'S':
  369. /* A prefix operand without side-effect. */
  370. if (prefix_insn != NO_CRIS_PREFIX && (insn & 0x400) == 0)
  371. {
  372. prefix_ok = 1;
  373. break;
  374. }
  375. else
  376. return -1;
  377. case 's':
  378. case 'y':
  379. case 'Y':
  380. /* If this is a prefixed insn with postincrement (side-effect),
  381. the prefix must not be DIP. */
  382. if (prefix_insn != NO_CRIS_PREFIX)
  383. {
  384. if (insn & 0x400)
  385. {
  386. const struct cris_opcode *prefix_opcodep
  387. = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
  388. if (prefix_opcodep->match == DIP_OPCODE)
  389. return -1;
  390. }
  391. prefix_ok = 1;
  392. }
  393. break;
  394. case 'B':
  395. /* If we don't fall through, then the prefix is ok. */
  396. prefix_ok = 1;
  397. /* A "push" prefix. Check for valid "push" size.
  398. In case of special register, it may be != 4. */
  399. if (prefix_insn != NO_CRIS_PREFIX)
  400. {
  401. /* Match the prefix insn to BDAPQ. */
  402. const struct cris_opcode *prefix_opcodep
  403. = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
  404. if (prefix_opcodep->match == BDAP_QUICK_OPCODE)
  405. {
  406. int pushsize = (prefix_insn & 255);
  407. if (pushsize > 127)
  408. pushsize -= 256;
  409. if (s[1] == 'P')
  410. {
  411. unsigned int spec_reg = (insn >> 12) & 15;
  412. const struct cris_spec_reg *sregp
  413. = spec_reg_info (spec_reg, disdata->distype);
  414. /* For a special-register, the "prefix size" must
  415. match the size of the register. */
  416. if (sregp && sregp->reg_size == (unsigned int) -pushsize)
  417. break;
  418. }
  419. else if (s[1] == 'R')
  420. {
  421. if ((insn & 0x30) == 0x20 && pushsize == -4)
  422. break;
  423. }
  424. /* FIXME: Should abort here; next constraint letter
  425. *must* be 'P' or 'R'. */
  426. }
  427. }
  428. return -1;
  429. case 'D':
  430. retval = (((insn >> 12) & 15) == (insn & 15));
  431. if (!retval)
  432. return -1;
  433. else
  434. retval += 4;
  435. break;
  436. case 'P':
  437. {
  438. const struct cris_spec_reg *sregp
  439. = spec_reg_info ((insn >> 12) & 15, disdata->distype);
  440. /* Since we match four bits, we will give a value of 4-1 = 3
  441. in a match. If there is a corresponding exact match of a
  442. special register in another pattern, it will get a value of
  443. 4, which will be higher. This should be correct in that an
  444. exact pattern would match better than a general pattern.
  445. Note that there is a reason for not returning zero; the
  446. pattern for "clear" is partly matched in the bit-pattern
  447. (the two lower bits must be zero), while the bit-pattern
  448. for a move from a special register is matched in the
  449. register constraint. */
  450. if (sregp != NULL)
  451. {
  452. retval += 3;
  453. break;
  454. }
  455. else
  456. return -1;
  457. }
  458. }
  459. if (prefix_insn != NO_CRIS_PREFIX && ! prefix_ok)
  460. return -1;
  461. return retval;
  462. }
  463. /* Format number as hex with a leading "0x" into outbuffer. */
  464. static char *
  465. format_hex (unsigned long number,
  466. char *outbuffer,
  467. struct cris_disasm_data *disdata)
  468. {
  469. /* Truncate negative numbers on >32-bit hosts. */
  470. number &= 0xffffffff;
  471. sprintf (outbuffer, "0x%lx", number);
  472. /* Save this value for the "case" support. */
  473. if (TRACE_CASE)
  474. last_immediate = number;
  475. return outbuffer + strlen (outbuffer);
  476. }
  477. /* Format number as decimal into outbuffer. Parameter signedp says
  478. whether the number should be formatted as signed (!= 0) or
  479. unsigned (== 0). */
  480. static char *
  481. format_dec (long number, char *outbuffer, int signedp)
  482. {
  483. last_immediate = number;
  484. if (signedp)
  485. sprintf (outbuffer, "%ld", number);
  486. else
  487. sprintf (outbuffer, "%lu", (unsigned long) number);
  488. return outbuffer + strlen (outbuffer);
  489. }
  490. /* Format the name of the general register regno into outbuffer. */
  491. static char *
  492. format_reg (struct cris_disasm_data *disdata,
  493. int regno,
  494. char *outbuffer_start,
  495. bool with_reg_prefix)
  496. {
  497. char *outbuffer = outbuffer_start;
  498. if (with_reg_prefix)
  499. *outbuffer++ = REGISTER_PREFIX_CHAR;
  500. switch (regno)
  501. {
  502. case 15:
  503. /* For v32, there is no context in which we output PC. */
  504. if (disdata->distype == cris_dis_v32)
  505. strcpy (outbuffer, "acr");
  506. else
  507. strcpy (outbuffer, "pc");
  508. break;
  509. case 14:
  510. strcpy (outbuffer, "sp");
  511. break;
  512. default:
  513. sprintf (outbuffer, "r%d", regno);
  514. break;
  515. }
  516. return outbuffer_start + strlen (outbuffer_start);
  517. }
  518. /* Format the name of a support register into outbuffer. */
  519. static char *
  520. format_sup_reg (unsigned int regno,
  521. char *outbuffer_start,
  522. bool with_reg_prefix)
  523. {
  524. char *outbuffer = outbuffer_start;
  525. int i;
  526. if (with_reg_prefix)
  527. *outbuffer++ = REGISTER_PREFIX_CHAR;
  528. for (i = 0; cris_support_regs[i].name != NULL; i++)
  529. if (cris_support_regs[i].number == regno)
  530. {
  531. sprintf (outbuffer, "%s", cris_support_regs[i].name);
  532. return outbuffer_start + strlen (outbuffer_start);
  533. }
  534. /* There's supposed to be register names covering all numbers, though
  535. some may be generic names. */
  536. sprintf (outbuffer, "format_sup_reg-BUG");
  537. return outbuffer_start + strlen (outbuffer_start);
  538. }
  539. /* Return the length of an instruction. */
  540. static unsigned
  541. bytes_to_skip (unsigned int insn,
  542. const struct cris_opcode *matchedp,
  543. enum cris_disass_family distype,
  544. const struct cris_opcode *prefix_matchedp)
  545. {
  546. /* Each insn is a word plus "immediate" operands. */
  547. unsigned to_skip = 2;
  548. const char *template_name = (const char *) matchedp->args;
  549. const char *s;
  550. for (s = template_name; *s; s++)
  551. if ((*s == 's' || *s == 'N' || *s == 'Y')
  552. && (insn & 0x400) && (insn & 15) == 15
  553. && prefix_matchedp == NULL)
  554. {
  555. /* Immediate via [pc+], so we have to check the size of the
  556. operand. */
  557. int mode_size = 1 << ((insn >> 4) & (*template_name == 'z' ? 1 : 3));
  558. if (matchedp->imm_oprnd_size == SIZE_FIX_32)
  559. to_skip += 4;
  560. else if (matchedp->imm_oprnd_size == SIZE_SPEC_REG)
  561. {
  562. const struct cris_spec_reg *sregp
  563. = spec_reg_info ((insn >> 12) & 15, distype);
  564. /* FIXME: Improve error handling; should have been caught
  565. earlier. */
  566. if (sregp == NULL)
  567. return 2;
  568. /* PC is incremented by two, not one, for a byte. Except on
  569. CRISv32, where constants are always DWORD-size for
  570. special registers. */
  571. to_skip +=
  572. distype == cris_dis_v32 ? 4 : (sregp->reg_size + 1) & ~1;
  573. }
  574. else
  575. to_skip += (mode_size + 1) & ~1;
  576. }
  577. else if (*s == 'n')
  578. to_skip += 4;
  579. else if (*s == 'b')
  580. to_skip += 2;
  581. return to_skip;
  582. }
  583. /* Print condition code flags. */
  584. static char *
  585. print_flags (struct cris_disasm_data *disdata, unsigned int insn, char *cp)
  586. {
  587. /* Use the v8 (Etrax 100) flag definitions for disassembly.
  588. The differences with v0 (Etrax 1..4) vs. Svinto are:
  589. v0 'd' <=> v8 'm'
  590. v0 'e' <=> v8 'b'.
  591. FIXME: Emit v0..v3 flag names somehow. */
  592. static const char v8_fnames[] = "cvznxibm";
  593. static const char v32_fnames[] = "cvznxiup";
  594. const char *fnames
  595. = disdata->distype == cris_dis_v32 ? v32_fnames : v8_fnames;
  596. unsigned char flagbits = (((insn >> 8) & 0xf0) | (insn & 15));
  597. int i;
  598. for (i = 0; i < 8; i++)
  599. if (flagbits & (1 << i))
  600. *cp++ = fnames[i];
  601. return cp;
  602. }
  603. /* Print out an insn with its operands, and update the info->insn_type
  604. fields. The prefix_opcodep and the rest hold a prefix insn that is
  605. supposed to be output as an address mode. */
  606. static void
  607. print_with_operands (const struct cris_opcode *opcodep,
  608. unsigned int insn,
  609. unsigned char *buffer,
  610. bfd_vma addr,
  611. disassemble_info *info,
  612. /* If a prefix insn was before this insn (and is supposed
  613. to be output as an address), here is a description of
  614. it. */
  615. const struct cris_opcode *prefix_opcodep,
  616. unsigned int prefix_insn,
  617. unsigned char *prefix_buffer,
  618. bool with_reg_prefix)
  619. {
  620. /* Get a buffer of somewhat reasonable size where we store
  621. intermediate parts of the insn. */
  622. char temp[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
  623. char *tp = temp;
  624. static const char mode_char[] = "bwd?";
  625. const char *s;
  626. const char *cs;
  627. struct cris_disasm_data *disdata
  628. = (struct cris_disasm_data *) info->private_data;
  629. /* Print out the name first thing we do. */
  630. (*info->fprintf_func) (info->stream, "%s", opcodep->name);
  631. cs = opcodep->args;
  632. s = cs;
  633. /* Ignore any prefix indicator. */
  634. if (*s == 'p')
  635. s++;
  636. if (*s == 'm' || *s == 'M' || *s == 'z')
  637. {
  638. *tp++ = '.';
  639. /* Get the size-letter. */
  640. *tp++ = *s == 'M'
  641. ? (insn & 0x8000 ? 'd'
  642. : insn & 0x4000 ? 'w' : 'b')
  643. : mode_char[(insn >> 4) & (*s == 'z' ? 1 : 3)];
  644. /* Ignore the size and the space character that follows. */
  645. s += 2;
  646. }
  647. /* Add a space if this isn't a long-branch, because for those will add
  648. the condition part of the name later. */
  649. if (opcodep->match != (BRANCH_PC_LOW + BRANCH_INCR_HIGH * 256))
  650. *tp++ = ' ';
  651. /* Fill in the insn-type if deducible from the name (and there's no
  652. better way). */
  653. if (opcodep->name[0] == 'j')
  654. {
  655. if (startswith (opcodep->name, "jsr"))
  656. /* It's "jsr" or "jsrc". */
  657. info->insn_type = dis_jsr;
  658. else
  659. /* Any other jump-type insn is considered a branch. */
  660. info->insn_type = dis_branch;
  661. }
  662. /* We might know some more fields right now. */
  663. info->branch_delay_insns = opcodep->delayed;
  664. /* Handle operands. */
  665. for (; *s; s++)
  666. {
  667. switch (*s)
  668. {
  669. case 'T':
  670. tp = format_sup_reg ((insn >> 12) & 15, tp, with_reg_prefix);
  671. break;
  672. case 'A':
  673. if (with_reg_prefix)
  674. *tp++ = REGISTER_PREFIX_CHAR;
  675. *tp++ = 'a';
  676. *tp++ = 'c';
  677. *tp++ = 'r';
  678. break;
  679. case '[':
  680. case ']':
  681. case ',':
  682. *tp++ = *s;
  683. break;
  684. case '!':
  685. /* Ignore at this point; used at earlier stages to avoid
  686. recognition if there's a prefix at something that in other
  687. ways looks like a "pop". */
  688. break;
  689. case 'd':
  690. /* Ignore. This is an optional ".d " on the large one of
  691. relaxable insns. */
  692. break;
  693. case 'B':
  694. /* This was the prefix that made this a "push". We've already
  695. handled it by recognizing it, so signal that the prefix is
  696. handled by setting it to NULL. */
  697. prefix_opcodep = NULL;
  698. break;
  699. case 'D':
  700. case 'r':
  701. tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
  702. break;
  703. case 'R':
  704. tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
  705. break;
  706. case 'n':
  707. {
  708. /* Like N but pc-relative to the start of the insn. */
  709. int32_t number = (buffer[2] + buffer[3] * 256 + buffer[4] * 65536
  710. + buffer[5] * 0x1000000u);
  711. /* Finish off and output previous formatted bytes. */
  712. *tp = 0;
  713. if (temp[0])
  714. (*info->fprintf_func) (info->stream, "%s", temp);
  715. tp = temp;
  716. (*info->print_address_func) (addr + number, info);
  717. }
  718. break;
  719. case 'u':
  720. {
  721. /* Like n but the offset is bits <3:0> in the instruction. */
  722. unsigned int number = (buffer[0] & 0xf) * 2;
  723. /* Finish off and output previous formatted bytes. */
  724. *tp = 0;
  725. if (temp[0])
  726. (*info->fprintf_func) (info->stream, "%s", temp);
  727. tp = temp;
  728. (*info->print_address_func) (addr + number, info);
  729. }
  730. break;
  731. case 'N':
  732. case 'y':
  733. case 'Y':
  734. case 'S':
  735. case 's':
  736. /* Any "normal" memory operand. */
  737. if ((insn & 0x400) && (insn & 15) == 15 && prefix_opcodep == NULL)
  738. {
  739. /* We're looking at [pc+], i.e. we need to output an immediate
  740. number, where the size can depend on different things. */
  741. int32_t number;
  742. int signedp
  743. = ((*cs == 'z' && (insn & 0x20))
  744. || opcodep->match == BDAP_QUICK_OPCODE);
  745. int nbytes;
  746. if (opcodep->imm_oprnd_size == SIZE_FIX_32)
  747. nbytes = 4;
  748. else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
  749. {
  750. const struct cris_spec_reg *sregp
  751. = spec_reg_info ((insn >> 12) & 15, disdata->distype);
  752. /* A NULL return should have been as a non-match earlier,
  753. so catch it as an internal error in the error-case
  754. below. */
  755. if (sregp == NULL)
  756. /* Whatever non-valid size. */
  757. nbytes = 42;
  758. else
  759. /* PC is always incremented by a multiple of two.
  760. For CRISv32, immediates are always 4 bytes for
  761. special registers. */
  762. nbytes = disdata->distype == cris_dis_v32
  763. ? 4 : (sregp->reg_size + 1) & ~1;
  764. }
  765. else
  766. {
  767. int mode_size = 1 << ((insn >> 4) & (*cs == 'z' ? 1 : 3));
  768. if (mode_size == 1)
  769. nbytes = 2;
  770. else
  771. nbytes = mode_size;
  772. }
  773. switch (nbytes)
  774. {
  775. case 1:
  776. number = buffer[2];
  777. if (signedp && number > 127)
  778. number -= 256;
  779. break;
  780. case 2:
  781. number = buffer[2] + buffer[3] * 256;
  782. if (signedp && number > 32767)
  783. number -= 65536;
  784. break;
  785. case 4:
  786. number = (buffer[2] + buffer[3] * 256 + buffer[4] * 65536
  787. + buffer[5] * 0x1000000u);
  788. break;
  789. default:
  790. strcpy (tp, "bug");
  791. tp += 3;
  792. number = 42;
  793. }
  794. if ((*cs == 'z' && (insn & 0x20))
  795. || (opcodep->match == BDAP_QUICK_OPCODE
  796. && (nbytes <= 2 || buffer[1 + nbytes] == 0)))
  797. tp = format_dec (number, tp, signedp);
  798. else
  799. {
  800. unsigned int highbyte = (number >> 24) & 0xff;
  801. /* Either output this as an address or as a number. If it's
  802. a dword with the same high-byte as the address of the
  803. insn, assume it's an address, and also if it's a non-zero
  804. non-0xff high-byte. If this is a jsr or a jump, then
  805. it's definitely an address. */
  806. if (nbytes == 4
  807. && (highbyte == ((addr >> 24) & 0xff)
  808. || (highbyte != 0 && highbyte != 0xff)
  809. || info->insn_type == dis_branch
  810. || info->insn_type == dis_jsr))
  811. {
  812. /* Finish off and output previous formatted bytes. */
  813. *tp = 0;
  814. tp = temp;
  815. if (temp[0])
  816. (*info->fprintf_func) (info->stream, "%s", temp);
  817. (*info->print_address_func) ((bfd_vma) number, info);
  818. info->target = number;
  819. }
  820. else
  821. tp = format_hex (number, tp, disdata);
  822. }
  823. }
  824. else
  825. {
  826. /* Not an immediate number. Then this is a (possibly
  827. prefixed) memory operand. */
  828. if (info->insn_type != dis_nonbranch)
  829. {
  830. int mode_size
  831. = 1 << ((insn >> 4)
  832. & (opcodep->args[0] == 'z' ? 1 : 3));
  833. int size;
  834. info->insn_type = dis_dref;
  835. info->flags |= CRIS_DIS_FLAG_MEMREF;
  836. if (opcodep->imm_oprnd_size == SIZE_FIX_32)
  837. size = 4;
  838. else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
  839. {
  840. const struct cris_spec_reg *sregp
  841. = spec_reg_info ((insn >> 12) & 15, disdata->distype);
  842. /* FIXME: Improve error handling; should have been caught
  843. earlier. */
  844. if (sregp == NULL)
  845. size = 4;
  846. else
  847. size = sregp->reg_size;
  848. }
  849. else
  850. size = mode_size;
  851. info->data_size = size;
  852. }
  853. *tp++ = '[';
  854. if (prefix_opcodep
  855. /* We don't match dip with a postincremented field
  856. as a side-effect address mode. */
  857. && ((insn & 0x400) == 0
  858. || prefix_opcodep->match != DIP_OPCODE))
  859. {
  860. if (insn & 0x400)
  861. {
  862. tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
  863. *tp++ = '=';
  864. }
  865. /* We mainly ignore the prefix format string when the
  866. address-mode syntax is output. */
  867. switch (prefix_opcodep->match)
  868. {
  869. case DIP_OPCODE:
  870. /* It's [r], [r+] or [pc+]. */
  871. if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
  872. {
  873. /* It's [pc+]. This cannot possibly be anything
  874. but an address. */
  875. int32_t number = (prefix_buffer[2]
  876. + prefix_buffer[3] * 256
  877. + prefix_buffer[4] * 65536
  878. + prefix_buffer[5] * 0x1000000u);
  879. info->target = (bfd_vma) number;
  880. /* Finish off and output previous formatted
  881. data. */
  882. *tp = 0;
  883. tp = temp;
  884. if (temp[0])
  885. (*info->fprintf_func) (info->stream, "%s", temp);
  886. (*info->print_address_func) ((bfd_vma) number, info);
  887. }
  888. else
  889. {
  890. /* For a memref in an address, we use target2.
  891. In this case, target is zero. */
  892. info->flags
  893. |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
  894. | CRIS_DIS_FLAG_MEM_TARGET2_MEM);
  895. info->target2 = prefix_insn & 15;
  896. *tp++ = '[';
  897. tp = format_reg (disdata, prefix_insn & 15, tp,
  898. with_reg_prefix);
  899. if (prefix_insn & 0x400)
  900. *tp++ = '+';
  901. *tp++ = ']';
  902. }
  903. break;
  904. case BDAP_QUICK_OPCODE:
  905. {
  906. int number;
  907. number = prefix_buffer[0];
  908. if (number > 127)
  909. number -= 256;
  910. /* Output "reg+num" or, if num < 0, "reg-num". */
  911. tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
  912. with_reg_prefix);
  913. if (number >= 0)
  914. *tp++ = '+';
  915. tp = format_dec (number, tp, 1);
  916. info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
  917. info->target = (prefix_insn >> 12) & 15;
  918. info->target2 = (bfd_vma) number;
  919. break;
  920. }
  921. case BIAP_OPCODE:
  922. /* Output "r+R.m". */
  923. tp = format_reg (disdata, prefix_insn & 15, tp,
  924. with_reg_prefix);
  925. *tp++ = '+';
  926. tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
  927. with_reg_prefix);
  928. *tp++ = '.';
  929. *tp++ = mode_char[(prefix_insn >> 4) & 3];
  930. info->flags
  931. |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
  932. | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
  933. | ((prefix_insn & 0x8000)
  934. ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
  935. : ((prefix_insn & 0x8000)
  936. ? CRIS_DIS_FLAG_MEM_TARGET2_MULT2 : 0)));
  937. /* Is it the casejump? It's a "adds.w [pc+r%d.w],pc". */
  938. if (insn == 0xf83f && (prefix_insn & ~0xf000) == 0x55f)
  939. /* Then start interpreting data as offsets. */
  940. case_offset_counter = no_of_case_offsets;
  941. break;
  942. case BDAP_INDIR_OPCODE:
  943. /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
  944. "r-s". */
  945. tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
  946. with_reg_prefix);
  947. if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
  948. {
  949. int32_t number;
  950. unsigned int nbytes;
  951. /* It's a value. Get its size. */
  952. int mode_size = 1 << ((prefix_insn >> 4) & 3);
  953. if (mode_size == 1)
  954. nbytes = 2;
  955. else
  956. nbytes = mode_size;
  957. switch (nbytes)
  958. {
  959. case 1:
  960. number = prefix_buffer[2];
  961. if (number > 127)
  962. number -= 256;
  963. break;
  964. case 2:
  965. number = prefix_buffer[2] + prefix_buffer[3] * 256;
  966. if (number > 32767)
  967. number -= 65536;
  968. break;
  969. case 4:
  970. number = (prefix_buffer[2] + prefix_buffer[3] * 256
  971. + prefix_buffer[4] * 65536
  972. + prefix_buffer[5] * 0x1000000u);
  973. break;
  974. default:
  975. strcpy (tp, "bug");
  976. tp += 3;
  977. number = 42;
  978. }
  979. info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
  980. info->target2 = (bfd_vma) number;
  981. /* If the size is dword, then assume it's an
  982. address. */
  983. if (nbytes == 4)
  984. {
  985. /* Finish off and output previous formatted
  986. bytes. */
  987. *tp++ = '+';
  988. *tp = 0;
  989. tp = temp;
  990. (*info->fprintf_func) (info->stream, "%s", temp);
  991. (*info->print_address_func) ((bfd_vma) number, info);
  992. }
  993. else
  994. {
  995. if (number >= 0)
  996. *tp++ = '+';
  997. tp = format_dec (number, tp, 1);
  998. }
  999. }
  1000. else
  1001. {
  1002. /* Output "r+[R].m" or "r+[R+].m". */
  1003. *tp++ = '+';
  1004. *tp++ = '[';
  1005. tp = format_reg (disdata, prefix_insn & 15, tp,
  1006. with_reg_prefix);
  1007. if (prefix_insn & 0x400)
  1008. *tp++ = '+';
  1009. *tp++ = ']';
  1010. *tp++ = '.';
  1011. *tp++ = mode_char[(prefix_insn >> 4) & 3];
  1012. info->flags
  1013. |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
  1014. | CRIS_DIS_FLAG_MEM_TARGET2_MEM
  1015. | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
  1016. | (((prefix_insn >> 4) == 2)
  1017. ? 0
  1018. : (((prefix_insn >> 4) & 3) == 1
  1019. ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
  1020. : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE)));
  1021. }
  1022. break;
  1023. default:
  1024. (*info->fprintf_func) (info->stream, "?prefix-bug");
  1025. }
  1026. /* To mark that the prefix is used, reset it. */
  1027. prefix_opcodep = NULL;
  1028. }
  1029. else
  1030. {
  1031. tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
  1032. info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
  1033. info->target = insn & 15;
  1034. if (insn & 0x400)
  1035. *tp++ = '+';
  1036. }
  1037. *tp++ = ']';
  1038. }
  1039. break;
  1040. case 'x':
  1041. tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
  1042. *tp++ = '.';
  1043. *tp++ = mode_char[(insn >> 4) & 3];
  1044. break;
  1045. case 'I':
  1046. tp = format_dec (insn & 63, tp, 0);
  1047. break;
  1048. case 'b':
  1049. {
  1050. int where = buffer[2] + buffer[3] * 256;
  1051. if (where > 32767)
  1052. where -= 65536;
  1053. where += addr + ((disdata->distype == cris_dis_v32) ? 0 : 4);
  1054. if (insn == BA_PC_INCR_OPCODE)
  1055. info->insn_type = dis_branch;
  1056. else
  1057. info->insn_type = dis_condbranch;
  1058. info->target = (bfd_vma) where;
  1059. *tp = 0;
  1060. tp = temp;
  1061. (*info->fprintf_func) (info->stream, "%s%s ",
  1062. temp, cris_cc_strings[insn >> 12]);
  1063. (*info->print_address_func) ((bfd_vma) where, info);
  1064. }
  1065. break;
  1066. case 'c':
  1067. tp = format_dec (insn & 31, tp, 0);
  1068. break;
  1069. case 'C':
  1070. tp = format_dec (insn & 15, tp, 0);
  1071. break;
  1072. case 'o':
  1073. {
  1074. long offset = insn & 0xfe;
  1075. bfd_vma target;
  1076. if (insn & 1)
  1077. offset |= ~0xff;
  1078. if (opcodep->match == BA_QUICK_OPCODE)
  1079. info->insn_type = dis_branch;
  1080. else
  1081. info->insn_type = dis_condbranch;
  1082. target = addr + ((disdata->distype == cris_dis_v32) ? 0 : 2) + offset;
  1083. info->target = target;
  1084. *tp = 0;
  1085. tp = temp;
  1086. (*info->fprintf_func) (info->stream, "%s", temp);
  1087. (*info->print_address_func) (target, info);
  1088. }
  1089. break;
  1090. case 'Q':
  1091. case 'O':
  1092. {
  1093. long number = buffer[0];
  1094. if (number > 127)
  1095. number = number - 256;
  1096. tp = format_dec (number, tp, 1);
  1097. *tp++ = ',';
  1098. tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
  1099. }
  1100. break;
  1101. case 'f':
  1102. tp = print_flags (disdata, insn, tp);
  1103. break;
  1104. case 'i':
  1105. tp = format_dec ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp, 1);
  1106. break;
  1107. case 'P':
  1108. {
  1109. const struct cris_spec_reg *sregp
  1110. = spec_reg_info ((insn >> 12) & 15, disdata->distype);
  1111. if (sregp->name == NULL)
  1112. /* Should have been caught as a non-match eariler. */
  1113. *tp++ = '?';
  1114. else
  1115. {
  1116. if (with_reg_prefix)
  1117. *tp++ = REGISTER_PREFIX_CHAR;
  1118. strcpy (tp, sregp->name);
  1119. tp += strlen (tp);
  1120. }
  1121. }
  1122. break;
  1123. default:
  1124. strcpy (tp, "???");
  1125. tp += 3;
  1126. }
  1127. }
  1128. *tp = 0;
  1129. if (prefix_opcodep)
  1130. (*info->fprintf_func) (info->stream, " (OOPS unused prefix \"%s: %s\")",
  1131. prefix_opcodep->name, prefix_opcodep->args);
  1132. (*info->fprintf_func) (info->stream, "%s", temp);
  1133. /* Get info for matching case-tables, if we don't have any active.
  1134. We assume that the last constant seen is used; either in the insn
  1135. itself or in a "move.d const,rN, sub.d rN,rM"-like sequence. */
  1136. if (TRACE_CASE && case_offset_counter == 0)
  1137. {
  1138. if (startswith (opcodep->name, "sub"))
  1139. case_offset = last_immediate;
  1140. /* It could also be an "add", if there are negative case-values. */
  1141. else if (startswith (opcodep->name, "add"))
  1142. /* The first case is the negated operand to the add. */
  1143. case_offset = -last_immediate;
  1144. /* A bound insn will tell us the number of cases. */
  1145. else if (startswith (opcodep->name, "bound"))
  1146. no_of_case_offsets = last_immediate + 1;
  1147. /* A jump or jsr or branch breaks the chain of insns for a
  1148. case-table, so assume default first-case again. */
  1149. else if (info->insn_type == dis_jsr
  1150. || info->insn_type == dis_branch
  1151. || info->insn_type == dis_condbranch)
  1152. case_offset = 0;
  1153. }
  1154. }
  1155. /* Print the CRIS instruction at address memaddr on stream. Returns
  1156. length of the instruction, in bytes. Prefix register names with `$' if
  1157. WITH_REG_PREFIX. */
  1158. static int
  1159. print_insn_cris_generic (bfd_vma memaddr,
  1160. disassemble_info *info,
  1161. bool with_reg_prefix)
  1162. {
  1163. int nbytes;
  1164. unsigned int insn;
  1165. const struct cris_opcode *matchedp;
  1166. int advance = 0;
  1167. struct cris_disasm_data *disdata
  1168. = (struct cris_disasm_data *) info->private_data;
  1169. /* No instruction will be disassembled as longer than this number of
  1170. bytes; stacked prefixes will not be expanded. */
  1171. unsigned char buffer[MAX_BYTES_PER_CRIS_INSN];
  1172. unsigned char *bufp;
  1173. int status = 0;
  1174. bfd_vma addr;
  1175. /* There will be an "out of range" error after the last instruction.
  1176. Reading pairs of bytes in decreasing number, we hope that we will get
  1177. at least the amount that we will consume.
  1178. If we can't get any data, or we do not get enough data, we print
  1179. the error message. */
  1180. for (nbytes = MAX_BYTES_PER_CRIS_INSN; nbytes > 0; nbytes -= 2)
  1181. {
  1182. status = (*info->read_memory_func) (memaddr, buffer, nbytes, info);
  1183. if (status == 0)
  1184. break;
  1185. }
  1186. /* If we did not get all we asked for, then clear the rest.
  1187. Hopefully this makes a reproducible result in case of errors. */
  1188. if (nbytes != MAX_BYTES_PER_CRIS_INSN)
  1189. memset (buffer + nbytes, 0, MAX_BYTES_PER_CRIS_INSN - nbytes);
  1190. addr = memaddr;
  1191. bufp = buffer;
  1192. /* Set some defaults for the insn info. */
  1193. info->insn_info_valid = 1;
  1194. info->branch_delay_insns = 0;
  1195. info->data_size = 0;
  1196. info->insn_type = dis_nonbranch;
  1197. info->flags = 0;
  1198. info->target = 0;
  1199. info->target2 = 0;
  1200. /* If we got any data, disassemble it. */
  1201. if (nbytes != 0)
  1202. {
  1203. matchedp = NULL;
  1204. insn = bufp[0] + bufp[1] * 256;
  1205. /* If we're in a case-table, don't disassemble the offsets. */
  1206. if (TRACE_CASE && case_offset_counter != 0)
  1207. {
  1208. info->insn_type = dis_noninsn;
  1209. advance += 2;
  1210. /* If to print data as offsets, then shortcut here. */
  1211. (*info->fprintf_func) (info->stream, "case %ld%s: -> ",
  1212. case_offset + no_of_case_offsets
  1213. - case_offset_counter,
  1214. case_offset_counter == 1 ? "/default" :
  1215. "");
  1216. (*info->print_address_func) ((bfd_vma)
  1217. ((short) (insn)
  1218. + (long) (addr
  1219. - (no_of_case_offsets
  1220. - case_offset_counter)
  1221. * 2)), info);
  1222. case_offset_counter--;
  1223. /* The default case start (without a "sub" or "add") must be
  1224. zero. */
  1225. if (case_offset_counter == 0)
  1226. case_offset = 0;
  1227. }
  1228. else if (insn == 0)
  1229. {
  1230. /* We're often called to disassemble zeroes. While this is a
  1231. valid "bcc .+2" insn, it is also useless enough and enough
  1232. of a nuiscance that we will just output "bcc .+2" for it
  1233. and signal it as a noninsn. */
  1234. (*info->fprintf_func) (info->stream,
  1235. disdata->distype == cris_dis_v32
  1236. ? "bcc ." : "bcc .+2");
  1237. info->insn_type = dis_noninsn;
  1238. advance += 2;
  1239. }
  1240. else
  1241. {
  1242. const struct cris_opcode *prefix_opcodep = NULL;
  1243. unsigned char *prefix_buffer = bufp;
  1244. unsigned int prefix_insn = insn;
  1245. int prefix_size = 0;
  1246. matchedp = get_opcode_entry (insn, NO_CRIS_PREFIX, disdata);
  1247. /* Check if we're supposed to write out prefixes as address
  1248. modes and if this was a prefix. */
  1249. if (matchedp != NULL && PARSE_PREFIX && matchedp->args[0] == 'p')
  1250. {
  1251. /* If it's a prefix, put it into the prefix vars and get the
  1252. main insn. */
  1253. prefix_size = bytes_to_skip (prefix_insn, matchedp,
  1254. disdata->distype, NULL);
  1255. prefix_opcodep = matchedp;
  1256. insn = bufp[prefix_size] + bufp[prefix_size + 1] * 256;
  1257. matchedp = get_opcode_entry (insn, prefix_insn, disdata);
  1258. if (matchedp != NULL)
  1259. {
  1260. addr += prefix_size;
  1261. bufp += prefix_size;
  1262. advance += prefix_size;
  1263. }
  1264. else
  1265. {
  1266. /* The "main" insn wasn't valid, at least not when
  1267. prefixed. Put back things enough to output the
  1268. prefix insn only, as a normal insn. */
  1269. matchedp = prefix_opcodep;
  1270. insn = prefix_insn;
  1271. prefix_opcodep = NULL;
  1272. }
  1273. }
  1274. if (matchedp == NULL)
  1275. {
  1276. (*info->fprintf_func) (info->stream, "??0x%x", insn);
  1277. advance += 2;
  1278. info->insn_type = dis_noninsn;
  1279. }
  1280. else
  1281. {
  1282. advance
  1283. += bytes_to_skip (insn, matchedp, disdata->distype,
  1284. prefix_opcodep);
  1285. /* The info_type and assorted fields will be set according
  1286. to the operands. */
  1287. print_with_operands (matchedp, insn, bufp, addr, info,
  1288. prefix_opcodep, prefix_insn,
  1289. prefix_buffer, with_reg_prefix);
  1290. }
  1291. }
  1292. }
  1293. else
  1294. info->insn_type = dis_noninsn;
  1295. /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
  1296. status when reading that much, and the insn decoding indicated a
  1297. length exceeding what we read, there is an error. */
  1298. if (status != 0 && (nbytes == 0 || advance > nbytes))
  1299. {
  1300. (*info->memory_error_func) (status, memaddr, info);
  1301. return -1;
  1302. }
  1303. /* Max supported insn size with one folded prefix insn. */
  1304. info->bytes_per_line = MAX_BYTES_PER_CRIS_INSN;
  1305. /* I would like to set this to a fixed value larger than the actual
  1306. number of bytes to print in order to avoid spaces between bytes,
  1307. but objdump.c (2.9.1) does not like that, so we print 16-bit
  1308. chunks, which is the next choice. */
  1309. info->bytes_per_chunk = 2;
  1310. /* Printing bytes in order of increasing addresses makes sense,
  1311. especially on a little-endian target.
  1312. This is completely the opposite of what you think; setting this to
  1313. BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
  1314. we want. */
  1315. info->display_endian = BFD_ENDIAN_BIG;
  1316. return advance;
  1317. }
  1318. /* Disassemble, prefixing register names with `$'. CRIS v0..v10. */
  1319. static int
  1320. print_insn_cris_with_register_prefix (bfd_vma vma,
  1321. disassemble_info *info)
  1322. {
  1323. if (info->private_data == NULL
  1324. && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
  1325. return -1;
  1326. return print_insn_cris_generic (vma, info, true);
  1327. }
  1328. /* Disassemble, prefixing register names with `$'. CRIS v32. */
  1329. static int
  1330. print_insn_crisv32_with_register_prefix (bfd_vma vma,
  1331. disassemble_info *info)
  1332. {
  1333. if (info->private_data == NULL
  1334. && !cris_parse_disassembler_options (info, cris_dis_v32))
  1335. return -1;
  1336. return print_insn_cris_generic (vma, info, true);
  1337. }
  1338. /* Disassemble, prefixing register names with `$'.
  1339. Common v10 and v32 subset. */
  1340. static int
  1341. print_insn_crisv10_v32_with_register_prefix (bfd_vma vma,
  1342. disassemble_info *info)
  1343. {
  1344. if (info->private_data == NULL
  1345. && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
  1346. return -1;
  1347. return print_insn_cris_generic (vma, info, true);
  1348. }
  1349. /* Disassemble, no prefixes on register names. CRIS v0..v10. */
  1350. static int
  1351. print_insn_cris_without_register_prefix (bfd_vma vma,
  1352. disassemble_info *info)
  1353. {
  1354. if (info->private_data == NULL
  1355. && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
  1356. return -1;
  1357. return print_insn_cris_generic (vma, info, false);
  1358. }
  1359. /* Disassemble, no prefixes on register names. CRIS v32. */
  1360. static int
  1361. print_insn_crisv32_without_register_prefix (bfd_vma vma,
  1362. disassemble_info *info)
  1363. {
  1364. if (info->private_data == NULL
  1365. && !cris_parse_disassembler_options (info, cris_dis_v32))
  1366. return -1;
  1367. return print_insn_cris_generic (vma, info, false);
  1368. }
  1369. /* Disassemble, no prefixes on register names.
  1370. Common v10 and v32 subset. */
  1371. static int
  1372. print_insn_crisv10_v32_without_register_prefix (bfd_vma vma,
  1373. disassemble_info *info)
  1374. {
  1375. if (info->private_data == NULL
  1376. && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
  1377. return -1;
  1378. return print_insn_cris_generic (vma, info, false);
  1379. }
  1380. /* Return a disassembler-function that prints registers with a `$' prefix,
  1381. or one that prints registers without a prefix.
  1382. FIXME: We should improve the solution to avoid the multitude of
  1383. functions seen above. */
  1384. disassembler_ftype
  1385. cris_get_disassembler (bfd *abfd)
  1386. {
  1387. /* If there's no bfd in sight, we return what is valid as input in all
  1388. contexts if fed back to the assembler: disassembly *with* register
  1389. prefix. Unfortunately this will be totally wrong for v32. */
  1390. if (abfd == NULL)
  1391. return print_insn_cris_with_register_prefix;
  1392. if (bfd_get_symbol_leading_char (abfd) == 0)
  1393. {
  1394. if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
  1395. return print_insn_crisv32_with_register_prefix;
  1396. if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
  1397. return print_insn_crisv10_v32_with_register_prefix;
  1398. /* We default to v10. This may be specifically specified in the
  1399. bfd mach, but is also the default setting. */
  1400. return print_insn_cris_with_register_prefix;
  1401. }
  1402. if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
  1403. return print_insn_crisv32_without_register_prefix;
  1404. if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
  1405. return print_insn_crisv10_v32_without_register_prefix;
  1406. return print_insn_cris_without_register_prefix;
  1407. }
  1408. /* Local variables:
  1409. eval: (c-set-style "gnu")
  1410. indent-tabs-mode: t
  1411. End: */