cgen.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /* GAS interface for targets using CGEN: Cpu tools GENerator.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include "as.h"
  16. #include <setjmp.h>
  17. #include "symcat.h"
  18. #include "cgen-desc.h"
  19. #include "subsegs.h"
  20. #include "cgen.h"
  21. #include "dwarf2dbg.h"
  22. #include "symbols.h"
  23. #include "struc-symbol.h"
  24. #ifdef OBJ_COMPLEX_RELC
  25. static expressionS * make_right_shifted_expr
  26. (expressionS *, const int, const int);
  27. static unsigned long gas_cgen_encode_addend
  28. (const unsigned long, const unsigned long, const unsigned long, \
  29. const unsigned long, const unsigned long, const unsigned long, \
  30. const unsigned long);
  31. static char * weak_operand_overflow_check
  32. (const expressionS *, const CGEN_OPERAND *);
  33. static void queue_fixup_recursively
  34. (const int, const int, expressionS *, \
  35. const CGEN_MAYBE_MULTI_IFLD *, const int, const int);
  36. static int rightshift = 0;
  37. #endif
  38. static void queue_fixup (int, int, expressionS *);
  39. /* Opcode table descriptor, must be set by md_begin. */
  40. CGEN_CPU_DESC gas_cgen_cpu_desc;
  41. /* Callback to insert a register into the symbol table.
  42. A target may choose to let GAS parse the registers.
  43. ??? Not currently used. */
  44. void
  45. cgen_asm_record_register (name, number)
  46. char *name;
  47. int number;
  48. {
  49. /* Use symbol_create here instead of symbol_new so we don't try to
  50. output registers into the object file's symbol table. */
  51. symbol_table_insert (symbol_create (name, reg_section,
  52. number, &zero_address_frag));
  53. }
  54. /* We need to keep a list of fixups. We can't simply generate them as
  55. we go, because that would require us to first create the frag, and
  56. that would screw up references to ``.''.
  57. This is used by cpu's with simple operands. It keeps knowledge of what
  58. an `expressionS' is and what a `fixup' is out of CGEN which for the time
  59. being is preferable.
  60. OPINDEX is the index in the operand table.
  61. OPINFO is something the caller chooses to help in reloc determination. */
  62. struct fixup
  63. {
  64. int opindex;
  65. int opinfo;
  66. expressionS exp;
  67. struct cgen_maybe_multi_ifield * field;
  68. int msb_field_p;
  69. };
  70. static struct fixup fixups[GAS_CGEN_MAX_FIXUPS];
  71. static int num_fixups;
  72. /* Prepare to parse an instruction.
  73. ??? May wish to make this static and delete calls in md_assemble. */
  74. void
  75. gas_cgen_init_parse ()
  76. {
  77. num_fixups = 0;
  78. }
  79. /* Queue a fixup. */
  80. static void
  81. queue_fixup (opindex, opinfo, expP)
  82. int opindex;
  83. int opinfo;
  84. expressionS * expP;
  85. {
  86. /* We need to generate a fixup for this expression. */
  87. if (num_fixups >= GAS_CGEN_MAX_FIXUPS)
  88. as_fatal (_("too many fixups"));
  89. fixups[num_fixups].exp = *expP;
  90. fixups[num_fixups].opindex = opindex;
  91. fixups[num_fixups].opinfo = opinfo;
  92. ++ num_fixups;
  93. }
  94. /* The following functions allow fixup chains to be stored, retrieved,
  95. and swapped. They are a generalization of a pre-existing scheme
  96. for storing, restoring and swapping fixup chains that was used by
  97. the m32r port. The functionality is essentially the same, only
  98. instead of only being able to store a single fixup chain, an entire
  99. array of fixup chains can be stored. It is the user's responsibility
  100. to keep track of how many fixup chains have been stored and which
  101. elements of the array they are in.
  102. The algorithms used are the same as in the old scheme. Other than the
  103. "array-ness" of the whole thing, the functionality is identical to the
  104. old scheme.
  105. gas_cgen_initialize_saved_fixups_array():
  106. Sets num_fixups_in_chain to 0 for each element. Call this from
  107. md_begin() if you plan to use these functions and you want the
  108. fixup count in each element to be set to 0 initially. This is
  109. not necessary, but it's included just in case. It performs
  110. the same function for each element in the array of fixup chains
  111. that gas_init_parse() performs for the current fixups.
  112. gas_cgen_save_fixups (element):
  113. element - element number of the array you wish to store the fixups
  114. to. No mechanism is built in for tracking what element
  115. was last stored to.
  116. gas_cgen_restore_fixups (element):
  117. element - element number of the array you wish to restore the fixups
  118. from.
  119. gas_cgen_swap_fixups(int element):
  120. element - swap the current fixups with those in this element number.
  121. */
  122. struct saved_fixups
  123. {
  124. struct fixup fixup_chain[GAS_CGEN_MAX_FIXUPS];
  125. int num_fixups_in_chain;
  126. };
  127. static struct saved_fixups stored_fixups[MAX_SAVED_FIXUP_CHAINS];
  128. void
  129. gas_cgen_initialize_saved_fixups_array ()
  130. {
  131. int i = 0;
  132. while (i < MAX_SAVED_FIXUP_CHAINS)
  133. stored_fixups[i++].num_fixups_in_chain = 0;
  134. }
  135. void
  136. gas_cgen_save_fixups (i)
  137. int i;
  138. {
  139. if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
  140. {
  141. as_fatal ("index into stored_fixups[] out of bounds");
  142. return;
  143. }
  144. stored_fixups[i].num_fixups_in_chain = num_fixups;
  145. memcpy (stored_fixups[i].fixup_chain, fixups,
  146. sizeof (fixups[0]) * num_fixups);
  147. num_fixups = 0;
  148. }
  149. void
  150. gas_cgen_restore_fixups (i)
  151. int i;
  152. {
  153. if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
  154. {
  155. as_fatal ("index into stored_fixups[] out of bounds");
  156. return;
  157. }
  158. num_fixups = stored_fixups[i].num_fixups_in_chain;
  159. memcpy (fixups, stored_fixups[i].fixup_chain,
  160. (sizeof (stored_fixups[i].fixup_chain[0])) * num_fixups);
  161. stored_fixups[i].num_fixups_in_chain = 0;
  162. }
  163. void
  164. gas_cgen_swap_fixups (i)
  165. int i;
  166. {
  167. if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
  168. {
  169. as_fatal ("index into stored_fixups[] out of bounds");
  170. return;
  171. }
  172. if (num_fixups == 0)
  173. gas_cgen_restore_fixups (i);
  174. else if (stored_fixups[i].num_fixups_in_chain == 0)
  175. gas_cgen_save_fixups (i);
  176. else
  177. {
  178. int tmp;
  179. struct fixup tmp_fixup;
  180. tmp = stored_fixups[i].num_fixups_in_chain;
  181. stored_fixups[i].num_fixups_in_chain = num_fixups;
  182. num_fixups = tmp;
  183. for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;)
  184. {
  185. tmp_fixup = stored_fixups[i].fixup_chain [tmp];
  186. stored_fixups[i].fixup_chain[tmp] = fixups [tmp];
  187. fixups [tmp] = tmp_fixup;
  188. }
  189. }
  190. }
  191. /* Default routine to record a fixup.
  192. This is a cover function to fix_new.
  193. It exists because we record INSN with the fixup.
  194. FRAG and WHERE are their respective arguments to fix_new_exp.
  195. LENGTH is in bits.
  196. OPINFO is something the caller chooses to help in reloc determination.
  197. At this point we do not use a bfd_reloc_code_real_type for
  198. operands residing in the insn, but instead just use the
  199. operand index. This lets us easily handle fixups for any
  200. operand type. We pick a BFD reloc type in md_apply_fix. */
  201. fixS *
  202. gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
  203. fragS * frag;
  204. int where;
  205. const CGEN_INSN * insn;
  206. int length;
  207. const CGEN_OPERAND * operand;
  208. int opinfo;
  209. symbolS * symbol;
  210. offsetT offset;
  211. {
  212. fixS *fixP;
  213. /* It may seem strange to use operand->attrs and not insn->attrs here,
  214. but it is the operand that has a pc relative relocation. */
  215. fixP = fix_new (frag, where, length / 8, symbol, offset,
  216. CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
  217. (bfd_reloc_code_real_type)
  218. ((int) BFD_RELOC_UNUSED
  219. + (int) operand->type));
  220. fixP->fx_cgen.insn = insn;
  221. fixP->fx_cgen.opinfo = opinfo;
  222. fixP->fx_cgen.field = NULL;
  223. fixP->fx_cgen.msb_field_p = 0;
  224. return fixP;
  225. }
  226. /* Default routine to record a fixup given an expression.
  227. This is a cover function to fix_new_exp.
  228. It exists because we record INSN with the fixup.
  229. FRAG and WHERE are their respective arguments to fix_new_exp.
  230. LENGTH is in bits.
  231. OPINFO is something the caller chooses to help in reloc determination.
  232. At this point we do not use a bfd_reloc_code_real_type for
  233. operands residing in the insn, but instead just use the
  234. operand index. This lets us easily handle fixups for any
  235. operand type. We pick a BFD reloc type in md_apply_fix. */
  236. fixS *
  237. gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
  238. fragS * frag;
  239. int where;
  240. const CGEN_INSN * insn;
  241. int length;
  242. const CGEN_OPERAND * operand;
  243. int opinfo;
  244. expressionS * exp;
  245. {
  246. fixS *fixP;
  247. /* It may seem strange to use operand->attrs and not insn->attrs here,
  248. but it is the operand that has a pc relative relocation. */
  249. fixP = fix_new_exp (frag, where, length / 8, exp,
  250. CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
  251. (bfd_reloc_code_real_type)
  252. ((int) BFD_RELOC_UNUSED
  253. + (int) operand->type));
  254. fixP->fx_cgen.insn = insn;
  255. fixP->fx_cgen.opinfo = opinfo;
  256. fixP->fx_cgen.field = NULL;
  257. fixP->fx_cgen.msb_field_p = 0;
  258. return fixP;
  259. }
  260. #ifdef OBJ_COMPLEX_RELC
  261. static symbolS *
  262. expr_build_binary (operatorT op, symbolS * s1, symbolS * s2)
  263. {
  264. expressionS e;
  265. e.X_op = op;
  266. e.X_add_symbol = s1;
  267. e.X_op_symbol = s2;
  268. e.X_add_number = 0;
  269. return make_expr_symbol (& e);
  270. }
  271. #endif
  272. /* Used for communication between the next two procedures. */
  273. static jmp_buf expr_jmp_buf;
  274. static int expr_jmp_buf_p;
  275. /* Callback for cgen interface. Parse the expression at *STRP.
  276. The result is an error message or NULL for success (in which case
  277. *STRP is advanced past the parsed text).
  278. WANT is an indication of what the caller is looking for.
  279. If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
  280. a table entry with the insn, reset the queued fixups counter.
  281. An enum cgen_parse_operand_result is stored in RESULTP.
  282. OPINDEX is the operand's table entry index.
  283. OPINFO is something the caller chooses to help in reloc determination.
  284. The resulting value is stored in VALUEP. */
  285. const char *
  286. gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP)
  287. #ifdef OBJ_COMPLEX_RELC
  288. CGEN_CPU_DESC cd;
  289. #else
  290. CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
  291. #endif
  292. enum cgen_parse_operand_type want;
  293. const char **strP;
  294. int opindex;
  295. int opinfo;
  296. enum cgen_parse_operand_result *resultP;
  297. bfd_vma *valueP;
  298. {
  299. #ifdef __STDC__
  300. /* These are volatile to survive the setjmp. */
  301. char * volatile hold;
  302. enum cgen_parse_operand_result * volatile resultP_1;
  303. volatile int opinfo_1;
  304. #else
  305. static char *hold;
  306. static enum cgen_parse_operand_result *resultP_1;
  307. int opinfo_1;
  308. #endif
  309. const char *errmsg;
  310. expressionS exp;
  311. #ifdef OBJ_COMPLEX_RELC
  312. volatile int signed_p = 0;
  313. symbolS * stmp = NULL;
  314. bfd_reloc_code_real_type reloc_type;
  315. const CGEN_OPERAND * operand;
  316. fixS dummy_fixup;
  317. #endif
  318. if (want == CGEN_PARSE_OPERAND_INIT)
  319. {
  320. gas_cgen_init_parse ();
  321. return NULL;
  322. }
  323. resultP_1 = resultP;
  324. hold = input_line_pointer;
  325. input_line_pointer = (char *) *strP;
  326. opinfo_1 = opinfo;
  327. /* We rely on md_operand to longjmp back to us.
  328. This is done via gas_cgen_md_operand. */
  329. if (setjmp (expr_jmp_buf) != 0)
  330. {
  331. expr_jmp_buf_p = 0;
  332. input_line_pointer = (char *) hold;
  333. *resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
  334. return _("illegal operand");
  335. }
  336. expr_jmp_buf_p = 1;
  337. expression (&exp);
  338. expr_jmp_buf_p = 0;
  339. errmsg = NULL;
  340. *strP = input_line_pointer;
  341. input_line_pointer = hold;
  342. #ifdef TC_CGEN_PARSE_FIX_EXP
  343. opinfo_1 = TC_CGEN_PARSE_FIX_EXP (opinfo_1, & exp);
  344. #endif
  345. /* FIXME: Need to check `want'. */
  346. switch (exp.X_op)
  347. {
  348. case O_illegal:
  349. errmsg = _("illegal operand");
  350. *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
  351. break;
  352. case O_absent:
  353. errmsg = _("missing operand");
  354. *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
  355. break;
  356. case O_constant:
  357. if (want == CGEN_PARSE_OPERAND_SYMBOLIC)
  358. goto de_fault;
  359. *valueP = exp.X_add_number;
  360. *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
  361. break;
  362. case O_register:
  363. *valueP = exp.X_add_number;
  364. *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
  365. break;
  366. de_fault:
  367. default:
  368. #ifdef OBJ_COMPLEX_RELC
  369. /* Look up operand, check to see if there's an obvious
  370. overflow (this helps disambiguate some insn parses). */
  371. operand = cgen_operand_lookup_by_num (cd, opindex);
  372. errmsg = weak_operand_overflow_check (& exp, operand);
  373. if (! errmsg)
  374. {
  375. /* Fragment the expression as necessary, and queue a reloc. */
  376. memset (& dummy_fixup, 0, sizeof (fixS));
  377. reloc_type = md_cgen_lookup_reloc (0, operand, & dummy_fixup);
  378. if (exp.X_op == O_symbol
  379. && reloc_type == BFD_RELOC_RELC
  380. && exp.X_add_symbol->sy_value.X_op == O_constant
  381. && (!exp.X_add_symbol->bsym
  382. || (exp.X_add_symbol->bsym->section != expr_section
  383. && exp.X_add_symbol->bsym->section != absolute_section
  384. && exp.X_add_symbol->bsym->section != undefined_section)))
  385. {
  386. /* Local labels will have been (eagerly) turned into constants
  387. by now, due to the inappropriately deep insight of the
  388. expression parser. Unfortunately make_expr_symbol
  389. prematurely dives into the symbol evaluator, and in this
  390. case it gets a bad answer, so we manually create the
  391. expression symbol we want here. */
  392. stmp = symbol_create (FAKE_LABEL_NAME, expr_section, 0,
  393. & zero_address_frag);
  394. symbol_set_value_expression (stmp, & exp);
  395. }
  396. else
  397. stmp = make_expr_symbol (& exp);
  398. /* If this is a pc-relative RELC operand, we
  399. need to subtract "." from the expression. */
  400. if (reloc_type == BFD_RELOC_RELC
  401. && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR))
  402. stmp = expr_build_binary (O_subtract, stmp, expr_build_dot ());
  403. /* FIXME: this is not a perfect heuristic for figuring out
  404. whether an operand is signed: it only works when the operand
  405. is an immediate. it's not terribly likely that any other
  406. values will be signed relocs, but it's possible. */
  407. if (operand && (operand->hw_type == HW_H_SINT))
  408. signed_p = 1;
  409. if (stmp->bsym && (stmp->bsym->section == expr_section)
  410. && ! S_IS_LOCAL (stmp))
  411. {
  412. if (signed_p)
  413. stmp->bsym->flags |= BSF_SRELC;
  414. else
  415. stmp->bsym->flags |= BSF_RELC;
  416. }
  417. /* Now package it all up for the fixup emitter. */
  418. exp.X_op = O_symbol;
  419. exp.X_op_symbol = 0;
  420. exp.X_add_symbol = stmp;
  421. exp.X_add_number = 0;
  422. /* Re-init rightshift quantity, just in case. */
  423. rightshift = operand->length;
  424. queue_fixup_recursively (opindex, opinfo_1, & exp,
  425. (reloc_type == BFD_RELOC_RELC) ?
  426. & (operand->index_fields) : 0,
  427. signed_p, -1);
  428. }
  429. * resultP = errmsg
  430. ? CGEN_PARSE_OPERAND_RESULT_ERROR
  431. : CGEN_PARSE_OPERAND_RESULT_QUEUED;
  432. *valueP = 0;
  433. #else
  434. queue_fixup (opindex, opinfo_1, &exp);
  435. *valueP = 0;
  436. *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
  437. #endif
  438. break;
  439. }
  440. return errmsg;
  441. }
  442. /* md_operand handler to catch unrecognized expressions and halt the
  443. parsing process so the next entry can be tried.
  444. ??? This could be done differently by adding code to `expression'. */
  445. void
  446. gas_cgen_md_operand (expressionP)
  447. expressionS *expressionP ATTRIBUTE_UNUSED;
  448. {
  449. /* Don't longjmp if we're not called from within cgen_parse_operand(). */
  450. if (expr_jmp_buf_p)
  451. longjmp (expr_jmp_buf, 1);
  452. }
  453. /* Finish assembling instruction INSN.
  454. BUF contains what we've built up so far.
  455. LENGTH is the size of the insn in bits.
  456. RELAX_P is non-zero if relaxable insns should be emitted as such.
  457. Otherwise they're emitted in non-relaxable forms.
  458. The "result" is stored in RESULT if non-NULL. */
  459. void
  460. gas_cgen_finish_insn (insn, buf, length, relax_p, result)
  461. const CGEN_INSN *insn;
  462. CGEN_INSN_BYTES_PTR buf;
  463. unsigned int length;
  464. int relax_p;
  465. finished_insnS *result;
  466. {
  467. int i;
  468. int relax_operand;
  469. char *f;
  470. unsigned int byte_len = length / 8;
  471. /* ??? Target foo issues various warnings here, so one might want to provide
  472. a hook here. However, our caller is defined in tc-foo.c so there
  473. shouldn't be a need for a hook. */
  474. /* Write out the instruction.
  475. It is important to fetch enough space in one call to `frag_more'.
  476. We use (f - frag_now->fr_literal) to compute where we are and we
  477. don't want frag_now to change between calls.
  478. Relaxable instructions: We need to ensure we allocate enough
  479. space for the largest insn. */
  480. if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
  481. /* These currently shouldn't get here. */
  482. abort ();
  483. /* Is there a relaxable insn with the relaxable operand needing a fixup? */
  484. relax_operand = -1;
  485. if (relax_p && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE))
  486. {
  487. /* Scan the fixups for the operand affected by relaxing
  488. (i.e. the branch address). */
  489. for (i = 0; i < num_fixups; ++i)
  490. {
  491. if (CGEN_OPERAND_ATTR_VALUE (cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex),
  492. CGEN_OPERAND_RELAX))
  493. {
  494. relax_operand = i;
  495. break;
  496. }
  497. }
  498. }
  499. if (relax_operand != -1)
  500. {
  501. int max_len;
  502. fragS *old_frag;
  503. expressionS *exp;
  504. symbolS *sym;
  505. offsetT off;
  506. #ifdef TC_CGEN_MAX_RELAX
  507. max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
  508. #else
  509. max_len = CGEN_MAX_INSN_SIZE;
  510. #endif
  511. /* Ensure variable part and fixed part are in same fragment. */
  512. /* FIXME: Having to do this seems like a hack. */
  513. frag_grow (max_len);
  514. /* Allocate space for the fixed part. */
  515. f = frag_more (byte_len);
  516. /* Create a relaxable fragment for this instruction. */
  517. old_frag = frag_now;
  518. exp = &fixups[relax_operand].exp;
  519. sym = exp->X_add_symbol;
  520. off = exp->X_add_number;
  521. if (exp->X_op != O_constant && exp->X_op != O_symbol)
  522. {
  523. /* Handle complex expressions. */
  524. sym = make_expr_symbol (exp);
  525. off = 0;
  526. }
  527. frag_var (rs_machine_dependent,
  528. max_len - byte_len /* max chars */,
  529. 0 /* variable part already allocated */,
  530. /* FIXME: When we machine generate the relax table,
  531. machine generate a macro to compute subtype. */
  532. 1 /* subtype */,
  533. sym,
  534. off,
  535. f);
  536. /* Record the operand number with the fragment so md_convert_frag
  537. can use gas_cgen_md_record_fixup to record the appropriate reloc. */
  538. old_frag->fr_cgen.insn = insn;
  539. old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
  540. old_frag->fr_cgen.opinfo = fixups[relax_operand].opinfo;
  541. if (result)
  542. result->frag = old_frag;
  543. }
  544. else
  545. {
  546. f = frag_more (byte_len);
  547. if (result)
  548. result->frag = frag_now;
  549. }
  550. /* If we're recording insns as numbers (rather than a string of bytes),
  551. target byte order handling is deferred until now. */
  552. #if CGEN_INT_INSN_P
  553. cgen_put_insn_value (gas_cgen_cpu_desc, (unsigned char *) f, length, *buf);
  554. #else
  555. memcpy (f, buf, byte_len);
  556. #endif
  557. /* Emit DWARF2 debugging information. */
  558. dwarf2_emit_insn (byte_len);
  559. /* Create any fixups. */
  560. for (i = 0; i < num_fixups; ++i)
  561. {
  562. fixS *fixP;
  563. const CGEN_OPERAND *operand =
  564. cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex);
  565. /* Don't create fixups for these. That's done during relaxation.
  566. We don't need to test for CGEN_INSN_RELAXED as they can't get here
  567. (see above). */
  568. if (relax_p
  569. && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE)
  570. && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_RELAX))
  571. continue;
  572. #ifndef md_cgen_record_fixup_exp
  573. #define md_cgen_record_fixup_exp gas_cgen_record_fixup_exp
  574. #endif
  575. fixP = md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
  576. insn, length, operand,
  577. fixups[i].opinfo,
  578. &fixups[i].exp);
  579. fixP->fx_cgen.field = fixups[i].field;
  580. fixP->fx_cgen.msb_field_p = fixups[i].msb_field_p;
  581. if (result)
  582. result->fixups[i] = fixP;
  583. }
  584. if (result)
  585. {
  586. result->num_fixups = num_fixups;
  587. result->addr = f;
  588. }
  589. }
  590. #ifdef OBJ_COMPLEX_RELC
  591. /* Queue many fixups, recursively. If the field is a multi-ifield,
  592. repeatedly queue its sub-parts, right shifted to fit into the field (we
  593. assume here multi-fields represent a left-to-right, MSB0-LSB0
  594. reading). */
  595. static void
  596. queue_fixup_recursively (const int opindex,
  597. const int opinfo,
  598. expressionS * expP,
  599. const CGEN_MAYBE_MULTI_IFLD * field,
  600. const int signed_p,
  601. const int part_of_multi)
  602. {
  603. if (field && field->count)
  604. {
  605. int i;
  606. for (i = 0; i < field->count; ++ i)
  607. queue_fixup_recursively (opindex, opinfo, expP,
  608. & (field->val.multi[i]), signed_p, i);
  609. }
  610. else
  611. {
  612. expressionS * new_exp = expP;
  613. #ifdef DEBUG
  614. printf ("queueing fixup for field %s\n",
  615. (field ? field->val.leaf->name : "??"));
  616. print_symbol_value (expP->X_add_symbol);
  617. #endif
  618. if (field && part_of_multi != -1)
  619. {
  620. rightshift -= field->val.leaf->length;
  621. /* Shift reloc value by number of bits remaining after this
  622. field. */
  623. if (rightshift)
  624. new_exp = make_right_shifted_expr (expP, rightshift, signed_p);
  625. }
  626. /* Truncate reloc values to length, *after* leftmost one. */
  627. fixups[num_fixups].msb_field_p = (part_of_multi <= 0);
  628. fixups[num_fixups].field = (CGEN_MAYBE_MULTI_IFLD *) field;
  629. queue_fixup (opindex, opinfo, new_exp);
  630. }
  631. }
  632. /* Encode the self-describing RELC reloc format's addend. */
  633. static unsigned long
  634. gas_cgen_encode_addend (const unsigned long start, /* in bits */
  635. const unsigned long len, /* in bits */
  636. const unsigned long oplen, /* in bits */
  637. const unsigned long wordsz, /* in bytes */
  638. const unsigned long chunksz, /* in bytes */
  639. const unsigned long signed_p,
  640. const unsigned long trunc_p)
  641. {
  642. unsigned long res = 0L;
  643. res |= start & 0x3F;
  644. res |= (oplen & 0x3F) << 6;
  645. res |= (len & 0x3F) << 12;
  646. res |= (wordsz & 0xF) << 18;
  647. res |= (chunksz & 0xF) << 22;
  648. res |= (CGEN_INSN_LSB0_P ? 1 : 0) << 27;
  649. res |= signed_p << 28;
  650. res |= trunc_p << 29;
  651. return res;
  652. }
  653. /* Purpose: make a weak check that the expression doesn't overflow the
  654. operand it's to be inserted into.
  655. Rationale: some insns used to use %operators to disambiguate during a
  656. parse. when these %operators are translated to expressions by the macro
  657. expander, the ambiguity returns. we attempt to disambiguate by field
  658. size.
  659. Method: check to see if the expression's top node is an O_and operator,
  660. and the mask is larger than the operand length. This would be an
  661. overflow, so signal it by returning an error string. Any other case is
  662. ambiguous, so we assume it's OK and return NULL. */
  663. static char *
  664. weak_operand_overflow_check (const expressionS * exp,
  665. const CGEN_OPERAND * operand)
  666. {
  667. const unsigned long len = operand->length;
  668. unsigned long mask;
  669. unsigned long opmask = (((1L << (len - 1)) - 1) << 1) | 1;
  670. if (!exp)
  671. return NULL;
  672. if (exp->X_op != O_bit_and)
  673. {
  674. /* Check for implicit overflow flag. */
  675. if (CGEN_OPERAND_ATTR_VALUE
  676. (operand, CGEN_OPERAND_RELOC_IMPLIES_OVERFLOW))
  677. return _("a reloc on this operand implies an overflow");
  678. return NULL;
  679. }
  680. mask = exp->X_add_number;
  681. if (exp->X_add_symbol
  682. && exp->X_add_symbol->sy_value.X_op == O_constant)
  683. mask |= exp->X_add_symbol->sy_value.X_add_number;
  684. if (exp->X_op_symbol
  685. && exp->X_op_symbol->sy_value.X_op == O_constant)
  686. mask |= exp->X_op_symbol->sy_value.X_add_number;
  687. /* Want to know if mask covers more bits than opmask.
  688. this is the same as asking if mask has any bits not in opmask,
  689. or whether (mask & ~opmask) is nonzero. */
  690. if (mask && (mask & ~opmask))
  691. {
  692. #ifdef DEBUG
  693. printf ("overflow: (mask = %8.8x, ~opmask = %8.8x, AND = %8.8x)\n",
  694. mask, ~opmask, (mask & ~opmask));
  695. #endif
  696. return _("operand mask overflow");
  697. }
  698. return NULL;
  699. }
  700. static expressionS *
  701. make_right_shifted_expr (expressionS * exp,
  702. const int amount,
  703. const int signed_p)
  704. {
  705. symbolS * stmp = 0;
  706. expressionS * new_exp;
  707. stmp = expr_build_binary (O_right_shift,
  708. make_expr_symbol (exp),
  709. expr_build_uconstant (amount));
  710. if (signed_p)
  711. stmp->bsym->flags |= BSF_SRELC;
  712. else
  713. stmp->bsym->flags |= BSF_RELC;
  714. /* Then wrap that in a "symbol expr" for good measure. */
  715. new_exp = xmalloc (sizeof (expressionS));
  716. memset (new_exp, 0, sizeof (expressionS));
  717. new_exp->X_op = O_symbol;
  718. new_exp->X_op_symbol = 0;
  719. new_exp->X_add_symbol = stmp;
  720. new_exp->X_add_number = 0;
  721. return new_exp;
  722. }
  723. #endif
  724. /* Apply a fixup to the object code. This is called for all the
  725. fixups we generated by the call to fix_new_exp, above. In the call
  726. above we used a reloc code which was the largest legal reloc code
  727. plus the operand index. Here we undo that to recover the operand
  728. index. At this point all symbol values should be fully resolved,
  729. and we attempt to completely resolve the reloc. If we can not do
  730. that, we determine the correct reloc code and put it back in the fixup. */
  731. /* FIXME: This function handles some of the fixups and bfd_install_relocation
  732. handles the rest. bfd_install_relocation (or some other bfd function)
  733. should handle them all. */
  734. void
  735. gas_cgen_md_apply_fix (fixP, valP, seg)
  736. fixS * fixP;
  737. valueT * valP;
  738. segT seg ATTRIBUTE_UNUSED;
  739. {
  740. char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
  741. valueT value = * valP;
  742. /* Canonical name, since used a lot. */
  743. CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
  744. if (fixP->fx_addsy == (symbolS *) NULL)
  745. fixP->fx_done = 1;
  746. /* We don't actually support subtracting a symbol. */
  747. if (fixP->fx_subsy != (symbolS *) NULL)
  748. as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
  749. if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
  750. {
  751. int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
  752. const CGEN_OPERAND *operand = cgen_operand_lookup_by_num (cd, opindex);
  753. const char *errmsg;
  754. bfd_reloc_code_real_type reloc_type;
  755. CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
  756. const CGEN_INSN *insn = fixP->fx_cgen.insn;
  757. #ifdef OBJ_COMPLEX_RELC
  758. int start;
  759. int length;
  760. int signed_p = 0;
  761. if (fixP->fx_cgen.field)
  762. {
  763. /* Use the twisty little pointer path
  764. back to the ifield if it exists. */
  765. start = fixP->fx_cgen.field->val.leaf->start;
  766. length = fixP->fx_cgen.field->val.leaf->length;
  767. }
  768. else
  769. {
  770. /* Or the far less useful operand-size guesstimate. */
  771. start = operand->start;
  772. length = operand->length;
  773. }
  774. /* FIXME: this is not a perfect heuristic for figuring out
  775. whether an operand is signed: it only works when the operand
  776. is an immediate. it's not terribly likely that any other
  777. values will be signed relocs, but it's possible. */
  778. if (operand && (operand->hw_type == HW_H_SINT))
  779. signed_p = 1;
  780. #endif
  781. /* If the reloc has been fully resolved finish the operand here. */
  782. /* FIXME: This duplicates the capabilities of code in BFD. */
  783. if (fixP->fx_done
  784. /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
  785. finish the job. Testing for pcrel is a temporary hack. */
  786. || fixP->fx_pcrel)
  787. {
  788. CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
  789. CGEN_CPU_SET_VMA_OPERAND (cd) (cd, opindex, fields, (bfd_vma) value);
  790. #if CGEN_INT_INSN_P
  791. {
  792. CGEN_INSN_INT insn_value =
  793. cgen_get_insn_value (cd, (unsigned char *) where,
  794. CGEN_INSN_BITSIZE (insn));
  795. /* ??? 0 is passed for `pc'. */
  796. errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
  797. &insn_value, (bfd_vma) 0);
  798. cgen_put_insn_value (cd, (unsigned char *) where,
  799. CGEN_INSN_BITSIZE (insn), insn_value);
  800. }
  801. #else
  802. /* ??? 0 is passed for `pc'. */
  803. errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
  804. (unsigned char *) where,
  805. (bfd_vma) 0);
  806. #endif
  807. if (errmsg)
  808. as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
  809. }
  810. if (fixP->fx_done)
  811. return;
  812. /* The operand isn't fully resolved. Determine a BFD reloc value
  813. based on the operand information and leave it to
  814. bfd_install_relocation. Note that this doesn't work when
  815. partial_inplace == false. */
  816. reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
  817. #ifdef OBJ_COMPLEX_RELC
  818. if (reloc_type == BFD_RELOC_RELC)
  819. {
  820. /* Change addend to "self-describing" form,
  821. for BFD to handle in the linker. */
  822. value = gas_cgen_encode_addend (start, operand->length,
  823. length, fixP->fx_size,
  824. cd->insn_chunk_bitsize / 8,
  825. signed_p,
  826. ! (fixP->fx_cgen.msb_field_p));
  827. }
  828. #endif
  829. if (reloc_type != BFD_RELOC_NONE)
  830. fixP->fx_r_type = reloc_type;
  831. else
  832. {
  833. as_bad_where (fixP->fx_file, fixP->fx_line,
  834. _("unresolved expression that must be resolved"));
  835. fixP->fx_done = 1;
  836. return;
  837. }
  838. }
  839. else if (fixP->fx_done)
  840. {
  841. /* We're finished with this fixup. Install it because
  842. bfd_install_relocation won't be called to do it. */
  843. switch (fixP->fx_r_type)
  844. {
  845. case BFD_RELOC_8:
  846. md_number_to_chars (where, value, 1);
  847. break;
  848. case BFD_RELOC_16:
  849. md_number_to_chars (where, value, 2);
  850. break;
  851. case BFD_RELOC_32:
  852. md_number_to_chars (where, value, 4);
  853. break;
  854. case BFD_RELOC_64:
  855. md_number_to_chars (where, value, 8);
  856. break;
  857. default:
  858. as_bad_where (fixP->fx_file, fixP->fx_line,
  859. _("internal error: can't install fix for reloc type %d (`%s')"),
  860. fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
  861. break;
  862. }
  863. }
  864. /* else
  865. bfd_install_relocation will be called to finish things up. */
  866. /* Tuck `value' away for use by tc_gen_reloc.
  867. See the comment describing fx_addnumber in write.h.
  868. This field is misnamed (or misused :-). */
  869. fixP->fx_addnumber = value;
  870. }
  871. bfd_reloc_code_real_type
  872. gas_cgen_pcrel_r_type (bfd_reloc_code_real_type r)
  873. {
  874. switch (r)
  875. {
  876. case BFD_RELOC_8: r = BFD_RELOC_8_PCREL; break;
  877. case BFD_RELOC_16: r = BFD_RELOC_16_PCREL; break;
  878. case BFD_RELOC_24: r = BFD_RELOC_24_PCREL; break;
  879. case BFD_RELOC_32: r = BFD_RELOC_32_PCREL; break;
  880. case BFD_RELOC_64: r = BFD_RELOC_64_PCREL; break;
  881. default:
  882. break;
  883. }
  884. return r;
  885. }
  886. /* Translate internal representation of relocation info to BFD target format.
  887. FIXME: To what extent can we get all relevant targets to use this? */
  888. arelent *
  889. gas_cgen_tc_gen_reloc (section, fixP)
  890. asection * section ATTRIBUTE_UNUSED;
  891. fixS * fixP;
  892. {
  893. bfd_reloc_code_real_type r_type = fixP->fx_r_type;
  894. arelent *reloc;
  895. reloc = (arelent *) xmalloc (sizeof (arelent));
  896. #ifdef GAS_CGEN_PCREL_R_TYPE
  897. if (fixP->fx_pcrel)
  898. r_type = GAS_CGEN_PCREL_R_TYPE (r_type);
  899. #endif
  900. reloc->howto = bfd_reloc_type_lookup (stdoutput, r_type);
  901. if (reloc->howto == (reloc_howto_type *) NULL)
  902. {
  903. as_bad_where (fixP->fx_file, fixP->fx_line,
  904. _("relocation is not supported"));
  905. return NULL;
  906. }
  907. gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
  908. reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
  909. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
  910. /* Use fx_offset for these cases. */
  911. if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
  912. || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
  913. reloc->addend = fixP->fx_offset;
  914. else
  915. reloc->addend = fixP->fx_addnumber;
  916. reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
  917. return reloc;
  918. }
  919. /* Perform any cgen specific initialisation.
  920. Called after gas_cgen_cpu_desc has been created. */
  921. void
  922. gas_cgen_begin ()
  923. {
  924. if (flag_signed_overflow_ok)
  925. cgen_set_signed_overflow_ok (gas_cgen_cpu_desc);
  926. else
  927. cgen_clear_signed_overflow_ok (gas_cgen_cpu_desc);
  928. }