tc-fr30.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* tc-fr30.c -- Assembler for the Fujitsu FR30.
  2. Copyright (C) 1998-2022 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,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public 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
  14. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "as.h"
  17. #include "safe-ctype.h"
  18. #include "subsegs.h"
  19. #include "symcat.h"
  20. #include "opcodes/fr30-desc.h"
  21. #include "opcodes/fr30-opc.h"
  22. #include "cgen.h"
  23. /* Structure to hold all of the different components describing
  24. an individual instruction. */
  25. typedef struct
  26. {
  27. const CGEN_INSN * insn;
  28. const CGEN_INSN * orig_insn;
  29. CGEN_FIELDS fields;
  30. #if CGEN_INT_INSN_P
  31. CGEN_INSN_INT buffer [1];
  32. #define INSN_VALUE(buf) (*(buf))
  33. #else
  34. unsigned char buffer [CGEN_MAX_INSN_SIZE];
  35. #define INSN_VALUE(buf) (buf)
  36. #endif
  37. char * addr;
  38. fragS * frag;
  39. int num_fixups;
  40. fixS * fixups [GAS_CGEN_MAX_FIXUPS];
  41. int indices [MAX_OPERAND_INSTANCES];
  42. }
  43. fr30_insn;
  44. const char comment_chars[] = ";";
  45. const char line_comment_chars[] = "#";
  46. const char line_separator_chars[] = "|";
  47. const char EXP_CHARS[] = "eE";
  48. const char FLT_CHARS[] = "dD";
  49. #define FR30_SHORTOPTS ""
  50. const char * md_shortopts = FR30_SHORTOPTS;
  51. struct option md_longopts[] =
  52. {
  53. {NULL, no_argument, NULL, 0}
  54. };
  55. size_t md_longopts_size = sizeof (md_longopts);
  56. int
  57. md_parse_option (int c ATTRIBUTE_UNUSED,
  58. const char *arg ATTRIBUTE_UNUSED)
  59. {
  60. switch (c)
  61. {
  62. default:
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. void
  68. md_show_usage (FILE * stream)
  69. {
  70. fprintf (stream, _(" FR30 specific command line options:\n"));
  71. }
  72. /* The target specific pseudo-ops which we support. */
  73. const pseudo_typeS md_pseudo_table[] =
  74. {
  75. { "word", cons, 4 },
  76. { NULL, NULL, 0 }
  77. };
  78. void
  79. md_begin (void)
  80. {
  81. /* Initialize the `cgen' interface. */
  82. /* Set the machine number and endian. */
  83. gas_cgen_cpu_desc = fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
  84. CGEN_CPU_OPEN_ENDIAN,
  85. CGEN_ENDIAN_BIG,
  86. CGEN_CPU_OPEN_END);
  87. fr30_cgen_init_asm (gas_cgen_cpu_desc);
  88. /* This is a callback from cgen to gas to parse operands. */
  89. cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
  90. }
  91. void
  92. md_assemble (char *str)
  93. {
  94. static int last_insn_had_delay_slot = 0;
  95. fr30_insn insn;
  96. char *errmsg;
  97. /* Initialize GAS's cgen interface for a new instruction. */
  98. gas_cgen_init_parse ();
  99. insn.insn = fr30_cgen_assemble_insn
  100. (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
  101. if (!insn.insn)
  102. {
  103. as_bad ("%s", errmsg);
  104. return;
  105. }
  106. /* Doesn't really matter what we pass for RELAX_P here. */
  107. gas_cgen_finish_insn (insn.insn, insn.buffer,
  108. CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
  109. /* Warn about invalid insns in delay slots. */
  110. if (last_insn_had_delay_slot
  111. && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT))
  112. as_warn (_("Instruction %s not allowed in a delay slot."),
  113. CGEN_INSN_NAME (insn.insn));
  114. last_insn_had_delay_slot
  115. = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
  116. }
  117. /* The syntax in the manual says constants begin with '#'.
  118. We just ignore it. */
  119. void
  120. md_operand (expressionS * expressionP)
  121. {
  122. if (* input_line_pointer == '#')
  123. {
  124. input_line_pointer ++;
  125. expression (expressionP);
  126. }
  127. }
  128. valueT
  129. md_section_align (segT segment, valueT size)
  130. {
  131. int align = bfd_section_alignment (segment);
  132. return ((size + (1 << align) - 1) & -(1 << align));
  133. }
  134. symbolS *
  135. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  136. {
  137. return NULL;
  138. }
  139. /* Interface to relax_segment. */
  140. /* FIXME: Build table by hand, get it working, then machine generate. */
  141. const relax_typeS md_relax_table[] =
  142. {
  143. /* The fields are:
  144. 1) most positive reach of this state,
  145. 2) most negative reach of this state,
  146. 3) how many bytes this mode will add to the size of the current frag
  147. 4) which index into the table to try if we can't fit into this one. */
  148. /* The first entry must be unused because an `rlx_more' value of zero ends
  149. each list. */
  150. {1, 1, 0, 0},
  151. /* The displacement used by GAS is from the end of the 2 byte insn,
  152. so we subtract 2 from the following. */
  153. /* 16 bit insn, 8 bit disp -> 10 bit range.
  154. This doesn't handle a branch in the right slot at the border:
  155. the "& -4" isn't taken into account. It's not important enough to
  156. complicate things over it, so we subtract an extra 2 (or + 2 in -ve
  157. case). */
  158. {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
  159. /* 32 bit insn, 24 bit disp -> 26 bit range. */
  160. {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
  161. /* Same thing, but with leading nop for alignment. */
  162. {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
  163. };
  164. /* Return an initial guess of the length by which a fragment must grow to
  165. hold a branch to reach its destination.
  166. Also updates fr_type/fr_subtype as necessary.
  167. Called just before doing relaxation.
  168. Any symbol that is now undefined will not become defined.
  169. The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  170. Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  171. Although it may not be explicit in the frag, pretend fr_var starts with a
  172. 0 value. */
  173. int
  174. md_estimate_size_before_relax (fragS * fragP, segT segment)
  175. {
  176. /* The only thing we have to handle here are symbols outside of the
  177. current segment. They may be undefined or in a different segment in
  178. which case linker scripts may place them anywhere.
  179. However, we can't finish the fragment here and emit the reloc as insn
  180. alignment requirements may move the insn about. */
  181. if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
  182. {
  183. /* The symbol is undefined in this segment.
  184. Change the relaxation subtype to the max allowable and leave
  185. all further handling to md_convert_frag. */
  186. fragP->fr_subtype = 2;
  187. {
  188. const CGEN_INSN * insn;
  189. int i;
  190. /* Update the recorded insn.
  191. Fortunately we don't have to look very far.
  192. FIXME: Change this to record in the instruction the next higher
  193. relaxable insn to use. */
  194. for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
  195. {
  196. if ((strcmp (CGEN_INSN_MNEMONIC (insn),
  197. CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
  198. == 0)
  199. && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
  200. break;
  201. }
  202. if (i == 4)
  203. abort ();
  204. fragP->fr_cgen.insn = insn;
  205. return 2;
  206. }
  207. }
  208. /* Return the size of the variable part of the frag. */
  209. return md_relax_table[fragP->fr_subtype].rlx_length;
  210. }
  211. /* *fragP has been relaxed to its final size, and now needs to have
  212. the bytes inside it modified to conform to the new size.
  213. Called after relaxation is finished.
  214. fragP->fr_type == rs_machine_dependent.
  215. fragP->fr_subtype is the subtype of what the address relaxed to. */
  216. void
  217. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
  218. segT sec ATTRIBUTE_UNUSED,
  219. fragS *fragP ATTRIBUTE_UNUSED)
  220. {
  221. }
  222. /* Functions concerning relocs. */
  223. /* The location from which a PC relative jump should be calculated,
  224. given a PC relative reloc. */
  225. long
  226. md_pcrel_from_section (fixS * fixP, segT sec)
  227. {
  228. if (fixP->fx_addsy != (symbolS *) NULL
  229. && (! S_IS_DEFINED (fixP->fx_addsy)
  230. || S_GET_SEGMENT (fixP->fx_addsy) != sec))
  231. /* The symbol is undefined (or is defined but not in this section).
  232. Let the linker figure it out. */
  233. return 0;
  234. return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
  235. }
  236. /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
  237. Returns BFD_RELOC_NONE if no reloc type can be found.
  238. *FIXP may be modified if desired. */
  239. bfd_reloc_code_real_type
  240. md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
  241. const CGEN_OPERAND *operand,
  242. fixS *fixP)
  243. {
  244. switch (operand->type)
  245. {
  246. case FR30_OPERAND_LABEL9: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
  247. case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
  248. case FR30_OPERAND_DISP10: return BFD_RELOC_FR30_10_IN_8;
  249. case FR30_OPERAND_DISP9: return BFD_RELOC_FR30_9_IN_8;
  250. case FR30_OPERAND_DISP8: return BFD_RELOC_FR30_8_IN_8;
  251. case FR30_OPERAND_UDISP6: return BFD_RELOC_FR30_6_IN_4;
  252. case FR30_OPERAND_I8: return BFD_RELOC_8;
  253. case FR30_OPERAND_I32: return BFD_RELOC_FR30_48;
  254. case FR30_OPERAND_I20: return BFD_RELOC_FR30_20;
  255. default : /* Avoid -Wall warning. */
  256. break;
  257. }
  258. return BFD_RELOC_NONE;
  259. }
  260. /* Write a value out to the object file, using the appropriate endianness. */
  261. void
  262. md_number_to_chars (char * buf, valueT val, int n)
  263. {
  264. number_to_chars_bigendian (buf, val, n);
  265. }
  266. const char *
  267. md_atof (int type, char * litP, int * sizeP)
  268. {
  269. return ieee_md_atof (type, litP, sizeP, true);
  270. }
  271. /* Worker function for fr30_is_colon_insn(). */
  272. static int
  273. restore_colon (char *next_i_l_p, char *nul_char)
  274. {
  275. /* Restore the colon, and advance input_line_pointer to
  276. the end of the new symbol. */
  277. *input_line_pointer = *nul_char;
  278. input_line_pointer = next_i_l_p;
  279. *nul_char = *next_i_l_p;
  280. *next_i_l_p = 0;
  281. return 1;
  282. }
  283. /* Determines if the symbol starting at START and ending in
  284. a colon that was at the location pointed to by INPUT_LINE_POINTER
  285. (but which has now been replaced bu a NUL) is in fact an
  286. LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
  287. If it is, then it restores the colon, advances INPUT_LINE_POINTER
  288. to the real end of the instruction/symbol, saves the char there to
  289. NUL_CHAR and pokes a NUL, and returns 1. Otherwise it returns 0. */
  290. int
  291. fr30_is_colon_insn (char *start, char *nul_char)
  292. {
  293. char * i_l_p = input_line_pointer;
  294. if (*nul_char == '"')
  295. ++i_l_p;
  296. /* Check to see if the symbol parsed so far is 'ldi'. */
  297. if ( (start[0] != 'l' && start[0] != 'L')
  298. || (start[1] != 'd' && start[1] != 'D')
  299. || (start[2] != 'i' && start[2] != 'I')
  300. || start[3] != 0)
  301. {
  302. /* Nope - check to see a 'd' follows the colon. */
  303. if ( (i_l_p[1] == 'd' || i_l_p[1] == 'D')
  304. && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
  305. {
  306. /* Yup - it might be delay slot instruction. */
  307. int i;
  308. static const char * delay_insns [] =
  309. {
  310. "call", "jmp", "ret", "bra", "bno",
  311. "beq", "bne", "bc", "bnc", "bn",
  312. "bp", "bv", "bnv", "blt", "bge",
  313. "ble", "bgt", "bls", "bhi"
  314. };
  315. for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
  316. {
  317. const char * insn = delay_insns[i];
  318. int len = strlen (insn);
  319. if (start [len] != 0)
  320. continue;
  321. while (len --)
  322. if (TOLOWER (start [len]) != insn [len])
  323. break;
  324. if (len == -1)
  325. return restore_colon (i_l_p + 1, nul_char);
  326. }
  327. }
  328. /* Nope - it is a normal label. */
  329. return 0;
  330. }
  331. /* Check to see if the text following the colon is '8'. */
  332. if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
  333. return restore_colon (i_l_p + 2, nul_char);
  334. /* Check to see if the text following the colon is '20'. */
  335. else if (i_l_p[1] == '2' && i_l_p[2] =='0'
  336. && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
  337. return restore_colon (i_l_p + 3, nul_char);
  338. /* Check to see if the text following the colon is '32'. */
  339. else if (i_l_p[1] == '3' && i_l_p[2] =='2'
  340. && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
  341. return restore_colon (i_l_p + 3, nul_char);
  342. return 0;
  343. }
  344. bool
  345. fr30_fix_adjustable (fixS * fixP)
  346. {
  347. /* We need the symbol name for the VTABLE entries. */
  348. if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  349. || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  350. return 0;
  351. return 1;
  352. }