tc-iq2000.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /* tc-iq2000.c -- Assembler for the Sitera IQ2000.
  2. Copyright (C) 2003-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,
  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/iq2000-desc.h"
  21. #include "opcodes/iq2000-opc.h"
  22. #include "cgen.h"
  23. #include "elf/common.h"
  24. #include "elf/iq2000.h"
  25. #include "libbfd.h"
  26. #include "sb.h"
  27. #include "macro.h"
  28. /* Structure to hold all of the different components describing
  29. an individual instruction. */
  30. typedef struct
  31. {
  32. const CGEN_INSN * insn;
  33. const CGEN_INSN * orig_insn;
  34. CGEN_FIELDS fields;
  35. #if CGEN_INT_INSN_P
  36. CGEN_INSN_INT buffer [1];
  37. #define INSN_VALUE(buf) (*(buf))
  38. #else
  39. unsigned char buffer [CGEN_MAX_INSN_SIZE];
  40. #define INSN_VALUE(buf) (buf)
  41. #endif
  42. char * addr;
  43. fragS * frag;
  44. int num_fixups;
  45. fixS * fixups [GAS_CGEN_MAX_FIXUPS];
  46. int indices [MAX_OPERAND_INSTANCES];
  47. }
  48. iq2000_insn;
  49. const char comment_chars[] = "#";
  50. const char line_comment_chars[] = "#";
  51. const char line_separator_chars[] = ";";
  52. const char EXP_CHARS[] = "eE";
  53. const char FLT_CHARS[] = "dD";
  54. /* Default machine. */
  55. #define DEFAULT_MACHINE bfd_mach_iq2000
  56. #define DEFAULT_FLAGS EF_IQ2000_CPU_IQ2000
  57. static unsigned long iq2000_mach = bfd_mach_iq2000;
  58. static int cpu_mach = (1 << MACH_IQ2000);
  59. /* Flags to set in the elf header. */
  60. static flagword iq2000_flags = DEFAULT_FLAGS;
  61. typedef struct proc
  62. {
  63. symbolS *isym;
  64. unsigned long reg_mask;
  65. unsigned long reg_offset;
  66. unsigned long fpreg_mask;
  67. unsigned long fpreg_offset;
  68. unsigned long frame_offset;
  69. unsigned long frame_reg;
  70. unsigned long pc_reg;
  71. } procS;
  72. static procS cur_proc;
  73. static procS *cur_proc_ptr;
  74. static int numprocs;
  75. /* Relocations against symbols are done in two
  76. parts, with a HI relocation and a LO relocation. Each relocation
  77. has only 16 bits of space to store an addend. This means that in
  78. order for the linker to handle carries correctly, it must be able
  79. to locate both the HI and the LO relocation. This means that the
  80. relocations must appear in order in the relocation table.
  81. In order to implement this, we keep track of each unmatched HI
  82. relocation. We then sort them so that they immediately precede the
  83. corresponding LO relocation. */
  84. struct iq2000_hi_fixup
  85. {
  86. struct iq2000_hi_fixup * next; /* Next HI fixup. */
  87. fixS * fixp; /* This fixup. */
  88. segT seg; /* The section this fixup is in. */
  89. };
  90. /* The list of unmatched HI relocs. */
  91. static struct iq2000_hi_fixup * iq2000_hi_fixup_list;
  92. /* Macro hash table, which we will add to. */
  93. extern struct hash_control *macro_hash;
  94. const char *md_shortopts = "";
  95. struct option md_longopts[] =
  96. {
  97. {NULL, no_argument, NULL, 0}
  98. };
  99. size_t md_longopts_size = sizeof (md_longopts);
  100. int
  101. md_parse_option (int c ATTRIBUTE_UNUSED,
  102. char * arg ATTRIBUTE_UNUSED)
  103. {
  104. return 0;
  105. }
  106. void
  107. md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
  108. {
  109. }
  110. /* Automatically enter conditional branch macros. */
  111. typedef struct
  112. {
  113. const char * mnemonic;
  114. const char ** expansion;
  115. const char ** args;
  116. } iq2000_macro_defs_s;
  117. static const char * abs_args[] = { "rd", "rs", "scratch=%1", NULL };
  118. static const char * abs_expn = "\n sra \\rd,\\rs,31\n xor \\scratch,\\rd,\\rs\n sub \\rd,\\scratch,\\rd\n";
  119. static const char * la_expn = "\n lui \\reg,%hi(\\label)\n ori \\reg,\\reg,%lo(\\label)\n";
  120. static const char * la_args[] = { "reg", "label", NULL };
  121. static const char * bxx_args[] = { "rs", "rt", "label", "scratch=%1", NULL };
  122. static const char * bge_expn = "\n slt \\scratch,\\rs,\\rt\n beq %0,\\scratch,\\label\n";
  123. static const char * bgeu_expn = "\n sltu \\scratch,\\rs,\\rt\n beq %0,\\scratch,\\label\n";
  124. static const char * bgt_expn = "\n slt \\scratch,\\rt,\\rs\n bne %0,\\scratch,\\label\n";
  125. static const char * bgtu_expn = "\n sltu \\scratch,\\rt,\\rs\n bne %0,\\scratch,\\label\n";
  126. static const char * ble_expn = "\n slt \\scratch,\\rt,\\rs\n beq %0,\\scratch,\\label\n";
  127. static const char * bleu_expn = "\n sltu \\scratch,\\rt,\\rs\n beq %0,\\scratch,\\label\n";
  128. static const char * blt_expn = "\n slt \\scratch,\\rs,\\rt\n bne %0,\\scratch,\\label\n";
  129. static const char * bltu_expn = "\n sltu \\scratch,\\rs,\\rt\n bne %0,\\scratch,\\label\n";
  130. static const char * sxx_args[] = { "rd", "rs", "rt", NULL };
  131. static const char * sge_expn = "\n slt \\rd,\\rs,\\rt\n xori \\rd,\\rd,1\n";
  132. static const char * sgeu_expn = "\n sltu \\rd,\\rs,\\rt\n xori \\rd,\\rd,1\n";
  133. static const char * sle_expn = "\n slt \\rd,\\rt,\\rs\n xori \\rd,\\rd,1\n";
  134. static const char * sleu_expn = "\n sltu \\rd,\\rt,\\rs\n xori \\rd,\\rd,1\n";
  135. static const char * sgt_expn = "\n slt \\rd,\\rt,\\rs\n";
  136. static const char * sgtu_expn = "\n sltu \\rd,\\rt,\\rs\n";
  137. static const char * sne_expn = "\n xor \\rd,\\rt,\\rs\n sltu \\rd,%0,\\rd\n";
  138. static const char * seq_expn = "\n xor \\rd,\\rt,\\rs\n sltu \\rd,%0,\\rd\n xori \\rd,\\rd,1\n";
  139. static const char * ai32_args[] = { "rt", "rs", "imm", NULL };
  140. static const char * andi32_expn = "\n\
  141. .if (\\imm & 0xffff0000 == 0xffff0000)\n\
  142. andoi \\rt,\\rs,%lo(\\imm)\n\
  143. .elseif (\\imm & 0x0000ffff == 0x0000ffff)\n\
  144. andoui \\rt,\\rs,%uhi(\\imm)\n\
  145. .elseif (\\imm & 0xffff0000 == 0x00000000)\n\
  146. andi \\rt,\\rs,%lo(\\imm)\n\
  147. .else\n\
  148. andoui \\rt,\\rs,%uhi(\\imm)\n\
  149. andoi \\rt,\\rt,%lo(\\imm)\n\
  150. .endif\n";
  151. static const char * ori32_expn = "\n\
  152. .if (\\imm & 0xffff == 0)\n\
  153. orui \\rt,\\rs,%uhi(\\imm)\n\
  154. .elseif (\\imm & 0xffff0000 == 0)\n\
  155. ori \\rt,\\rs,%lo(\\imm)\n\
  156. .else\n\
  157. orui \\rt,\\rs,%uhi(\\imm)\n\
  158. ori \\rt,\\rt,%lo(\\imm)\n\
  159. .endif\n";
  160. static const char * neg_args[] = { "rd", "rs", NULL };
  161. static const char * neg_expn = "\n sub \\rd,%0,\\rs\n";
  162. static const char * negu_expn = "\n subu \\rd,%0,\\rs\n";
  163. static const char * li_args[] = { "rt", "imm", NULL };
  164. static const char * li_expn = "\n\
  165. .if (\\imm & 0xffff0000 == 0x0)\n\
  166. ori \\rt,%0,\\imm\n\
  167. .elseif (\\imm & 0xffff0000 == 0xffff0000)\n\
  168. addi \\rt,%0,\\imm\n\
  169. .elseif (\\imm & 0x0000ffff == 0)\n\
  170. lui \\rt,%uhi(\\imm)\n\
  171. .else\n\
  172. lui \\rt,%uhi(\\imm)\n\
  173. ori \\rt,\\rt,%lo(\\imm)\n\
  174. .endif\n";
  175. static iq2000_macro_defs_s iq2000_macro_defs[] =
  176. {
  177. {"abs", (const char **) & abs_expn, (const char **) & abs_args},
  178. {"la", (const char **) & la_expn, (const char **) & la_args},
  179. {"bge", (const char **) & bge_expn, (const char **) & bxx_args},
  180. {"bgeu", (const char **) & bgeu_expn, (const char **) & bxx_args},
  181. {"bgt", (const char **) & bgt_expn, (const char **) & bxx_args},
  182. {"bgtu", (const char **) & bgtu_expn, (const char **) & bxx_args},
  183. {"ble", (const char **) & ble_expn, (const char **) & bxx_args},
  184. {"bleu", (const char **) & bleu_expn, (const char **) & bxx_args},
  185. {"blt", (const char **) & blt_expn, (const char **) & bxx_args},
  186. {"bltu", (const char **) & bltu_expn, (const char **) & bxx_args},
  187. {"sge", (const char **) & sge_expn, (const char **) & sxx_args},
  188. {"sgeu", (const char **) & sgeu_expn, (const char **) & sxx_args},
  189. {"sle", (const char **) & sle_expn, (const char **) & sxx_args},
  190. {"sleu", (const char **) & sleu_expn, (const char **) & sxx_args},
  191. {"sgt", (const char **) & sgt_expn, (const char **) & sxx_args},
  192. {"sgtu", (const char **) & sgtu_expn, (const char **) & sxx_args},
  193. {"seq", (const char **) & seq_expn, (const char **) & sxx_args},
  194. {"sne", (const char **) & sne_expn, (const char **) & sxx_args},
  195. {"neg", (const char **) & neg_expn, (const char **) & neg_args},
  196. {"negu", (const char **) & negu_expn, (const char **) & neg_args},
  197. {"li", (const char **) & li_expn, (const char **) & li_args},
  198. {"ori32", (const char **) & ori32_expn, (const char **) & ai32_args},
  199. {"andi32",(const char **) & andi32_expn,(const char **) & ai32_args},
  200. };
  201. static void
  202. iq2000_add_macro (const char * name,
  203. const char * semantics,
  204. const char ** arguments)
  205. {
  206. macro_entry *macro;
  207. sb macro_name;
  208. const char *namestr;
  209. macro = xmalloc (sizeof (macro_entry));
  210. sb_new (& macro->sub);
  211. sb_new (& macro_name);
  212. macro->formal_count = 0;
  213. macro->formals = 0;
  214. sb_add_string (& macro->sub, semantics);
  215. if (arguments != NULL)
  216. {
  217. formal_entry ** p = &macro->formals;
  218. macro->formal_count = 0;
  219. macro->formal_hash = hash_new ();
  220. while (*arguments != NULL)
  221. {
  222. formal_entry *formal;
  223. formal = xmalloc (sizeof (formal_entry));
  224. sb_new (& formal->name);
  225. sb_new (& formal->def);
  226. sb_new (& formal->actual);
  227. /* chlm: Added the following to allow defaulted args. */
  228. if (strchr (*arguments,'='))
  229. {
  230. char * tt_args = strdup (*arguments);
  231. char * tt_dflt = strchr (tt_args,'=');
  232. *tt_dflt = 0;
  233. sb_add_string (& formal->name, tt_args);
  234. sb_add_string (& formal->def, tt_dflt + 1);
  235. }
  236. else
  237. sb_add_string (& formal->name, *arguments);
  238. /* Add to macro's hash table. */
  239. hash_jam (macro->formal_hash, sb_terminate (& formal->name), formal);
  240. formal->index = macro->formal_count;
  241. macro->formal_count++;
  242. *p = formal;
  243. p = & formal->next;
  244. *p = NULL;
  245. ++arguments;
  246. }
  247. }
  248. sb_add_string (&macro_name, name);
  249. namestr = sb_terminate (&macro_name);
  250. hash_jam (macro_hash, namestr, macro);
  251. macro_defined = 1;
  252. }
  253. static void
  254. iq2000_load_macros (void)
  255. {
  256. int i;
  257. int mcnt = ARRAY_SIZE (iq2000_macro_defs);
  258. for (i = 0; i < mcnt; i++)
  259. iq2000_add_macro (iq2000_macro_defs[i].mnemonic,
  260. *iq2000_macro_defs[i].expansion,
  261. iq2000_macro_defs[i].args);
  262. }
  263. void
  264. md_begin (void)
  265. {
  266. /* Initialize the `cgen' interface. */
  267. /* Set the machine number and endian. */
  268. gas_cgen_cpu_desc = iq2000_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, cpu_mach,
  269. CGEN_CPU_OPEN_ENDIAN,
  270. CGEN_ENDIAN_BIG,
  271. CGEN_CPU_OPEN_END);
  272. iq2000_cgen_init_asm (gas_cgen_cpu_desc);
  273. /* This is a callback from cgen to gas to parse operands. */
  274. cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
  275. /* Set the ELF flags if desired. */
  276. if (iq2000_flags)
  277. bfd_set_private_flags (stdoutput, iq2000_flags);
  278. /* Set the machine type */
  279. bfd_default_set_arch_mach (stdoutput, bfd_arch_iq2000, iq2000_mach);
  280. iq2000_load_macros ();
  281. }
  282. void
  283. md_assemble (char * str)
  284. {
  285. static long delayed_load_register = 0;
  286. static int last_insn_had_delay_slot = 0;
  287. static int last_insn_has_load_delay = 0;
  288. static int last_insn_unconditional_jump = 0;
  289. static int last_insn_was_ldw = 0;
  290. iq2000_insn insn;
  291. char * errmsg;
  292. /* Initialize GAS's cgen interface for a new instruction. */
  293. gas_cgen_init_parse ();
  294. insn.insn = iq2000_cgen_assemble_insn
  295. (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
  296. if (!insn.insn)
  297. {
  298. as_bad ("%s", errmsg);
  299. return;
  300. }
  301. /* Doesn't really matter what we pass for RELAX_P here. */
  302. gas_cgen_finish_insn (insn.insn, insn.buffer,
  303. CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
  304. /* We need to generate an error if there's a yielding instruction in the delay
  305. slot of a control flow modifying instruction (jump (yes), load (no)) */
  306. if ((last_insn_had_delay_slot && !last_insn_has_load_delay) &&
  307. CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_YIELD_INSN))
  308. as_bad (_("the yielding instruction %s may not be in a delay slot."),
  309. CGEN_INSN_NAME (insn.insn));
  310. /* Warn about odd numbered base registers for paired-register
  311. instructions like LDW. On iq2000, result is always rt. */
  312. if (iq2000_mach == bfd_mach_iq2000
  313. && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_EVEN_REG_NUM)
  314. && (insn.fields.f_rt % 2))
  315. as_bad (_("Register number (R%ld) for double word access must be even."),
  316. insn.fields.f_rt);
  317. /* Warn about insns that reference the target of a previous load. */
  318. /* NOTE: R0 is a special case and is not subject to load delays (except for ldw). */
  319. if (delayed_load_register && (last_insn_has_load_delay || last_insn_was_ldw))
  320. {
  321. if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RD) &&
  322. insn.fields.f_rd == delayed_load_register)
  323. as_warn (_("operand references R%ld of previous load."),
  324. insn.fields.f_rd);
  325. if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RS) &&
  326. insn.fields.f_rs == delayed_load_register)
  327. as_warn (_("operand references R%ld of previous load."),
  328. insn.fields.f_rs);
  329. if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RT) &&
  330. insn.fields.f_rt == delayed_load_register)
  331. as_warn (_("operand references R%ld of previous load."),
  332. insn.fields.f_rt);
  333. if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_R31) &&
  334. delayed_load_register == 31)
  335. as_warn (_("instruction implicitly accesses R31 of previous load."));
  336. }
  337. /* Warn about insns that reference the (target + 1) of a previous ldw. */
  338. if (last_insn_was_ldw)
  339. {
  340. if ((CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RD)
  341. && insn.fields.f_rd == delayed_load_register + 1)
  342. || (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RS)
  343. && insn.fields.f_rs == delayed_load_register + 1)
  344. || (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_USES_RT)
  345. && insn.fields.f_rt == delayed_load_register + 1))
  346. as_warn (_("operand references R%ld of previous load."),
  347. delayed_load_register + 1);
  348. }
  349. last_insn_had_delay_slot =
  350. CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
  351. last_insn_has_load_delay =
  352. CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_LOAD_DELAY);
  353. if (last_insn_unconditional_jump)
  354. last_insn_has_load_delay = last_insn_unconditional_jump = 0;
  355. else if (! strcmp (CGEN_INSN_MNEMONIC (insn.insn), "j")
  356. || ! strcmp (CGEN_INSN_MNEMONIC (insn.insn), "jal"))
  357. last_insn_unconditional_jump = 1;
  358. /* The meaning of EVEN_REG_NUM was overloaded to also imply LDW. Since
  359. that's not true for IQ10, let's make the above logic specific to LDW. */
  360. last_insn_was_ldw = ! strcmp ("ldw", CGEN_INSN_NAME (insn.insn));
  361. /* The assumption here is that the target of a load is always rt. */
  362. delayed_load_register = insn.fields.f_rt;
  363. }
  364. valueT
  365. md_section_align (segT segment, valueT size)
  366. {
  367. int align = bfd_get_section_alignment (stdoutput, segment);
  368. return ((size + (1 << align) - 1) & (-1 << align));
  369. }
  370. symbolS *
  371. md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
  372. {
  373. return 0;
  374. }
  375. /* Interface to relax_segment. */
  376. /* Return an initial guess of the length by which a fragment must grow to
  377. hold a branch to reach its destination.
  378. Also updates fr_type/fr_subtype as necessary.
  379. Called just before doing relaxation.
  380. Any symbol that is now undefined will not become defined.
  381. The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  382. Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  383. Although it may not be explicit in the frag, pretend fr_var starts with a
  384. 0 value. */
  385. int
  386. md_estimate_size_before_relax (fragS * fragP,
  387. segT segment ATTRIBUTE_UNUSED)
  388. {
  389. int old_fr_fix = fragP->fr_fix;
  390. /* The only thing we have to handle here are symbols outside of the
  391. current segment. They may be undefined or in a different segment in
  392. which case linker scripts may place them anywhere.
  393. However, we can't finish the fragment here and emit the reloc as insn
  394. alignment requirements may move the insn about. */
  395. return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
  396. }
  397. /* *fragP has been relaxed to its final size, and now needs to have
  398. the bytes inside it modified to conform to the new size.
  399. Called after relaxation is finished.
  400. fragP->fr_type == rs_machine_dependent.
  401. fragP->fr_subtype is the subtype of what the address relaxed to. */
  402. void
  403. md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
  404. segT sec ATTRIBUTE_UNUSED,
  405. fragS * fragP ATTRIBUTE_UNUSED)
  406. {
  407. }
  408. /* Functions concerning relocs. */
  409. long
  410. md_pcrel_from_section (fixS * fixP, segT sec)
  411. {
  412. if (fixP->fx_addsy != (symbolS *) NULL
  413. && (! S_IS_DEFINED (fixP->fx_addsy)
  414. || S_GET_SEGMENT (fixP->fx_addsy) != sec))
  415. {
  416. /* The symbol is undefined (or is defined but not in this section).
  417. Let the linker figure it out. */
  418. return 0;
  419. }
  420. /* Return the address of the delay slot. */
  421. return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  422. }
  423. /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
  424. Returns BFD_RELOC_NONE if no reloc type can be found.
  425. *FIXP may be modified if desired. */
  426. bfd_reloc_code_real_type
  427. md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
  428. const CGEN_OPERAND * operand,
  429. fixS * fixP ATTRIBUTE_UNUSED)
  430. {
  431. switch (operand->type)
  432. {
  433. case IQ2000_OPERAND_OFFSET: return BFD_RELOC_16_PCREL_S2;
  434. case IQ2000_OPERAND_JMPTARG: return BFD_RELOC_IQ2000_OFFSET_16;
  435. case IQ2000_OPERAND_JMPTARGQ10: return BFD_RELOC_NONE;
  436. case IQ2000_OPERAND_HI16: return BFD_RELOC_HI16;
  437. case IQ2000_OPERAND_LO16: return BFD_RELOC_LO16;
  438. default: break;
  439. }
  440. return BFD_RELOC_NONE;
  441. }
  442. /* Record a HI16 reloc for later matching with its LO16 cousin. */
  443. static void
  444. iq2000_record_hi16 (int reloc_type,
  445. fixS * fixP,
  446. segT seg ATTRIBUTE_UNUSED)
  447. {
  448. struct iq2000_hi_fixup * hi_fixup;
  449. gas_assert (reloc_type == BFD_RELOC_HI16);
  450. hi_fixup = xmalloc (sizeof * hi_fixup);
  451. hi_fixup->fixp = fixP;
  452. hi_fixup->seg = now_seg;
  453. hi_fixup->next = iq2000_hi_fixup_list;
  454. iq2000_hi_fixup_list = hi_fixup;
  455. }
  456. /* Called while parsing an instruction to create a fixup.
  457. We need to check for HI16 relocs and queue them up for later sorting. */
  458. fixS *
  459. iq2000_cgen_record_fixup_exp (fragS * frag,
  460. int where,
  461. const CGEN_INSN * insn,
  462. int length,
  463. const CGEN_OPERAND * operand,
  464. int opinfo,
  465. expressionS * exp)
  466. {
  467. fixS * fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
  468. operand, opinfo, exp);
  469. if (operand->type == IQ2000_OPERAND_HI16
  470. /* If low/high was used, it is recorded in `opinfo'. */
  471. && (fixP->fx_cgen.opinfo == BFD_RELOC_HI16
  472. || fixP->fx_cgen.opinfo == BFD_RELOC_LO16))
  473. iq2000_record_hi16 (fixP->fx_cgen.opinfo, fixP, now_seg);
  474. return fixP;
  475. }
  476. /* Return BFD reloc type from opinfo field in a fixS.
  477. It's tricky using fx_r_type in iq2000_frob_file because the values
  478. are BFD_RELOC_UNUSED + operand number. */
  479. #define FX_OPINFO_R_TYPE(f) ((f)->fx_cgen.opinfo)
  480. /* Sort any unmatched HI16 relocs so that they immediately precede
  481. the corresponding LO16 reloc. This is called before md_apply_fix and
  482. tc_gen_reloc. */
  483. void
  484. iq2000_frob_file (void)
  485. {
  486. struct iq2000_hi_fixup * l;
  487. for (l = iq2000_hi_fixup_list; l != NULL; l = l->next)
  488. {
  489. segment_info_type * seginfo;
  490. int pass;
  491. gas_assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_HI16
  492. || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_LO16);
  493. /* Check quickly whether the next fixup happens to be a matching low. */
  494. if (l->fixp->fx_next != NULL
  495. && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_LO16
  496. && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
  497. && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
  498. continue;
  499. /* Look through the fixups for this segment for a matching
  500. `low'. When we find one, move the high just in front of it.
  501. We do this in two passes. In the first pass, we try to find
  502. a unique `low'. In the second pass, we permit multiple
  503. high's relocs for a single `low'. */
  504. seginfo = seg_info (l->seg);
  505. for (pass = 0; pass < 2; pass++)
  506. {
  507. fixS * f;
  508. fixS * prev;
  509. prev = NULL;
  510. for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
  511. {
  512. /* Check whether this is a `low' fixup which matches l->fixp. */
  513. if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_LO16
  514. && f->fx_addsy == l->fixp->fx_addsy
  515. && f->fx_offset == l->fixp->fx_offset
  516. && (pass == 1
  517. || prev == NULL
  518. || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_HI16)
  519. || prev->fx_addsy != f->fx_addsy
  520. || prev->fx_offset != f->fx_offset))
  521. {
  522. fixS ** pf;
  523. /* Move l->fixp before f. */
  524. for (pf = &seginfo->fix_root;
  525. * pf != l->fixp;
  526. pf = & (* pf)->fx_next)
  527. gas_assert (* pf != NULL);
  528. * pf = l->fixp->fx_next;
  529. l->fixp->fx_next = f;
  530. if (prev == NULL)
  531. seginfo->fix_root = l->fixp;
  532. else
  533. prev->fx_next = l->fixp;
  534. break;
  535. }
  536. prev = f;
  537. }
  538. if (f != NULL)
  539. break;
  540. if (pass == 1)
  541. as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
  542. _("Unmatched high relocation"));
  543. }
  544. }
  545. }
  546. /* See whether we need to force a relocation into the output file. */
  547. int
  548. iq2000_force_relocation (fixS * fix)
  549. {
  550. if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  551. || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  552. return 1;
  553. return 0;
  554. }
  555. /* Handle the .set pseudo-op. */
  556. static void
  557. s_iq2000_set (int x ATTRIBUTE_UNUSED)
  558. {
  559. static const char * ignored_arguments [] =
  560. {
  561. "reorder",
  562. "noreorder",
  563. "at",
  564. "noat",
  565. "macro",
  566. "nomacro",
  567. "move",
  568. "novolatile",
  569. "nomove",
  570. "volatile",
  571. "bopt",
  572. "nobopt",
  573. NULL
  574. };
  575. const char ** ignored;
  576. char *name = input_line_pointer, ch;
  577. char *save_ILP = input_line_pointer;
  578. while (!is_end_of_line[(unsigned char) *input_line_pointer])
  579. input_line_pointer++;
  580. ch = *input_line_pointer;
  581. *input_line_pointer = '\0';
  582. for (ignored = ignored_arguments; * ignored; ignored ++)
  583. if (strcmp (* ignored, name) == 0)
  584. break;
  585. if (* ignored == NULL)
  586. {
  587. /* We'd like to be able to use .set symbol, expn */
  588. input_line_pointer = save_ILP;
  589. s_set (0);
  590. return;
  591. }
  592. *input_line_pointer = ch;
  593. demand_empty_rest_of_line ();
  594. }
  595. /* Write a value out to the object file, using the appropriate endianness. */
  596. void
  597. md_number_to_chars (char * buf, valueT val, int n)
  598. {
  599. number_to_chars_bigendian (buf, val, n);
  600. }
  601. void
  602. md_operand (expressionS * exp)
  603. {
  604. /* In case of a syntax error, escape back to try next syntax combo. */
  605. if (exp->X_op == O_absent)
  606. gas_cgen_md_operand (exp);
  607. }
  608. char *
  609. md_atof (int type, char * litP, int * sizeP)
  610. {
  611. return ieee_md_atof (type, litP, sizeP, TRUE);
  612. }
  613. bfd_boolean
  614. iq2000_fix_adjustable (fixS * fixP)
  615. {
  616. bfd_reloc_code_real_type reloc_type;
  617. if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
  618. {
  619. const CGEN_INSN *insn = NULL;
  620. int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
  621. const CGEN_OPERAND *operand = cgen_operand_lookup_by_num(gas_cgen_cpu_desc, opindex);
  622. reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
  623. }
  624. else
  625. reloc_type = fixP->fx_r_type;
  626. if (fixP->fx_addsy == NULL)
  627. return TRUE;
  628. /* Prevent all adjustments to global symbols. */
  629. if (S_IS_EXTERNAL (fixP->fx_addsy))
  630. return FALSE;
  631. if (S_IS_WEAK (fixP->fx_addsy))
  632. return FALSE;
  633. /* We need the symbol name for the VTABLE entries. */
  634. if ( reloc_type == BFD_RELOC_VTABLE_INHERIT
  635. || reloc_type == BFD_RELOC_VTABLE_ENTRY)
  636. return FALSE;
  637. return TRUE;
  638. }
  639. static void
  640. s_change_sec (int sec)
  641. {
  642. #ifdef OBJ_ELF
  643. /* The ELF backend needs to know that we are changing sections, so
  644. that .previous works correctly. We could do something like check
  645. for a obj_section_change_hook macro, but that might be confusing
  646. as it would not be appropriate to use it in the section changing
  647. functions in read.c, since obj-elf.c intercepts those. FIXME:
  648. This should be cleaner, somehow. */
  649. obj_elf_section_change_hook ();
  650. #endif
  651. switch (sec)
  652. {
  653. case 't':
  654. s_text (0);
  655. break;
  656. case 'd':
  657. case 'r':
  658. s_data (0);
  659. break;
  660. }
  661. }
  662. static symbolS *
  663. get_symbol (void)
  664. {
  665. int c;
  666. char *name;
  667. symbolS *p;
  668. c = get_symbol_name (&name);
  669. p = (symbolS *) symbol_find_or_make (name);
  670. (void) restore_line_pointer (c);
  671. return p;
  672. }
  673. /* The .end directive. */
  674. static void
  675. s_iq2000_end (int x ATTRIBUTE_UNUSED)
  676. {
  677. symbolS *p;
  678. int maybe_text;
  679. if (!is_end_of_line[(unsigned char) *input_line_pointer])
  680. {
  681. p = get_symbol ();
  682. demand_empty_rest_of_line ();
  683. }
  684. else
  685. p = NULL;
  686. if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
  687. maybe_text = 1;
  688. else
  689. maybe_text = 0;
  690. if (!maybe_text)
  691. as_warn (_(".end not in text section"));
  692. if (!cur_proc_ptr)
  693. {
  694. as_warn (_(".end directive without a preceding .ent directive."));
  695. demand_empty_rest_of_line ();
  696. return;
  697. }
  698. if (p != NULL)
  699. {
  700. gas_assert (S_GET_NAME (p));
  701. if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
  702. as_warn (_(".end symbol does not match .ent symbol."));
  703. }
  704. else
  705. as_warn (_(".end directive missing or unknown symbol"));
  706. cur_proc_ptr = NULL;
  707. }
  708. static int
  709. get_number (void)
  710. {
  711. int negative = 0;
  712. long val = 0;
  713. if (*input_line_pointer == '-')
  714. {
  715. ++input_line_pointer;
  716. negative = 1;
  717. }
  718. if (! ISDIGIT (*input_line_pointer))
  719. as_bad (_("Expected simple number."));
  720. if (input_line_pointer[0] == '0')
  721. {
  722. if (input_line_pointer[1] == 'x')
  723. {
  724. input_line_pointer += 2;
  725. while (ISXDIGIT (*input_line_pointer))
  726. {
  727. val <<= 4;
  728. val |= hex_value (*input_line_pointer++);
  729. }
  730. return negative ? -val : val;
  731. }
  732. else
  733. {
  734. ++input_line_pointer;
  735. while (ISDIGIT (*input_line_pointer))
  736. {
  737. val <<= 3;
  738. val |= *input_line_pointer++ - '0';
  739. }
  740. return negative ? -val : val;
  741. }
  742. }
  743. if (! ISDIGIT (*input_line_pointer))
  744. {
  745. printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
  746. *input_line_pointer, *input_line_pointer);
  747. as_warn (_("Invalid number"));
  748. return -1;
  749. }
  750. while (ISDIGIT (*input_line_pointer))
  751. {
  752. val *= 10;
  753. val += *input_line_pointer++ - '0';
  754. }
  755. return negative ? -val : val;
  756. }
  757. /* The .aent and .ent directives. */
  758. static void
  759. s_iq2000_ent (int aent)
  760. {
  761. symbolS *symbolP;
  762. int maybe_text;
  763. symbolP = get_symbol ();
  764. if (*input_line_pointer == ',')
  765. input_line_pointer++;
  766. SKIP_WHITESPACE ();
  767. if (ISDIGIT (*input_line_pointer) || *input_line_pointer == '-')
  768. get_number ();
  769. if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
  770. maybe_text = 1;
  771. else
  772. maybe_text = 0;
  773. if (!maybe_text)
  774. as_warn (_(".ent or .aent not in text section."));
  775. if (!aent && cur_proc_ptr)
  776. as_warn (_("missing `.end'"));
  777. if (!aent)
  778. {
  779. cur_proc_ptr = &cur_proc;
  780. memset (cur_proc_ptr, '\0', sizeof (procS));
  781. cur_proc_ptr->isym = symbolP;
  782. symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
  783. numprocs++;
  784. }
  785. demand_empty_rest_of_line ();
  786. }
  787. /* The .frame directive. If the mdebug section is present (IRIX 5 native)
  788. then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
  789. s_iq2000_frame is used so that we can set the PDR information correctly.
  790. We can't use the ecoff routines because they make reference to the ecoff
  791. symbol table (in the mdebug section). */
  792. static void
  793. s_iq2000_frame (int ignore)
  794. {
  795. s_ignore (ignore);
  796. }
  797. /* The .fmask and .mask directives. If the mdebug section is present
  798. (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
  799. embedded targets, s_iq2000_mask is used so that we can set the PDR
  800. information correctly. We can't use the ecoff routines because they
  801. make reference to the ecoff symbol table (in the mdebug section). */
  802. static void
  803. s_iq2000_mask (int reg_type)
  804. {
  805. s_ignore (reg_type);
  806. }
  807. /* The target specific pseudo-ops which we support. */
  808. const pseudo_typeS md_pseudo_table[] =
  809. {
  810. { "align", s_align_bytes, 0 },
  811. { "word", cons, 4 },
  812. { "rdata", s_change_sec, 'r'},
  813. { "sdata", s_change_sec, 's'},
  814. { "set", s_iq2000_set, 0 },
  815. { "ent", s_iq2000_ent, 0 },
  816. { "end", s_iq2000_end, 0 },
  817. { "frame", s_iq2000_frame, 0 },
  818. { "fmask", s_iq2000_mask, 'F'},
  819. { "mask", s_iq2000_mask, 'R'},
  820. { "dword", cons, 8 },
  821. { "half", cons, 2 },
  822. { NULL, NULL, 0 }
  823. };