tc-m32c.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /* tc-m32c.c -- Assembler for the Renesas M32C.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by RedHat.
  4. This file is part of GAS, the GNU Assembler.
  5. GAS is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GAS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GAS; see the file COPYING. If not, write to
  15. the Free Software Foundation, 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include "as.h"
  18. #include "subsegs.h"
  19. #include "symcat.h"
  20. #include "opcodes/m32c-desc.h"
  21. #include "opcodes/m32c-opc.h"
  22. #include "cgen.h"
  23. #include "elf/common.h"
  24. #include "elf/m32c.h"
  25. #include "libbfd.h"
  26. #include "safe-ctype.h"
  27. /* Structure to hold all of the different components
  28. describing an individual instruction. */
  29. typedef struct
  30. {
  31. const CGEN_INSN * insn;
  32. const CGEN_INSN * orig_insn;
  33. CGEN_FIELDS fields;
  34. #if CGEN_INT_INSN_P
  35. CGEN_INSN_INT buffer [1];
  36. #define INSN_VALUE(buf) (*(buf))
  37. #else
  38. unsigned char buffer [CGEN_MAX_INSN_SIZE];
  39. #define INSN_VALUE(buf) (buf)
  40. #endif
  41. char * addr;
  42. fragS * frag;
  43. int num_fixups;
  44. fixS * fixups [GAS_CGEN_MAX_FIXUPS];
  45. int indices [MAX_OPERAND_INSTANCES];
  46. }
  47. m32c_insn;
  48. #define rl_for(_insn) (CGEN_ATTR_CGEN_INSN_RL_TYPE_VALUE (&((_insn).insn->base->attrs)))
  49. #define relaxable(_insn) (CGEN_ATTR_CGEN_INSN_RELAXABLE_VALUE (&((_insn).insn->base->attrs)))
  50. const char comment_chars[] = ";";
  51. const char line_comment_chars[] = "#";
  52. const char line_separator_chars[] = "|";
  53. const char EXP_CHARS[] = "eE";
  54. const char FLT_CHARS[] = "dD";
  55. #define M32C_SHORTOPTS ""
  56. const char * md_shortopts = M32C_SHORTOPTS;
  57. /* assembler options */
  58. #define OPTION_CPU_M16C (OPTION_MD_BASE)
  59. #define OPTION_CPU_M32C (OPTION_MD_BASE + 1)
  60. #define OPTION_LINKRELAX (OPTION_MD_BASE + 2)
  61. #define OPTION_H_TICK_HEX (OPTION_MD_BASE + 3)
  62. struct option md_longopts[] =
  63. {
  64. { "m16c", no_argument, NULL, OPTION_CPU_M16C },
  65. { "m32c", no_argument, NULL, OPTION_CPU_M32C },
  66. { "relax", no_argument, NULL, OPTION_LINKRELAX },
  67. { "h-tick-hex", no_argument, NULL, OPTION_H_TICK_HEX },
  68. {NULL, no_argument, NULL, 0}
  69. };
  70. size_t md_longopts_size = sizeof (md_longopts);
  71. /* Default machine */
  72. #define DEFAULT_MACHINE bfd_mach_m16c
  73. #define DEFAULT_FLAGS EF_M32C_CPU_M16C
  74. static unsigned long m32c_mach = bfd_mach_m16c;
  75. static int cpu_mach = (1 << MACH_M16C);
  76. static int insn_size;
  77. static int m32c_relax = 0;
  78. /* Flags to set in the elf header */
  79. static flagword m32c_flags = DEFAULT_FLAGS;
  80. static char default_isa = 1 << (7 - ISA_M16C);
  81. static CGEN_BITSET m32c_isa = {1, & default_isa};
  82. static void
  83. set_isa (enum isa_attr isa_num)
  84. {
  85. cgen_bitset_set (& m32c_isa, isa_num);
  86. }
  87. static void s_bss (int);
  88. int
  89. md_parse_option (int c, char * arg ATTRIBUTE_UNUSED)
  90. {
  91. switch (c)
  92. {
  93. case OPTION_CPU_M16C:
  94. m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M16C;
  95. m32c_mach = bfd_mach_m16c;
  96. cpu_mach = (1 << MACH_M16C);
  97. set_isa (ISA_M16C);
  98. break;
  99. case OPTION_CPU_M32C:
  100. m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M32C;
  101. m32c_mach = bfd_mach_m32c;
  102. cpu_mach = (1 << MACH_M32C);
  103. set_isa (ISA_M32C);
  104. break;
  105. case OPTION_LINKRELAX:
  106. m32c_relax = 1;
  107. break;
  108. case OPTION_H_TICK_HEX:
  109. enable_h_tick_hex = 1;
  110. break;
  111. default:
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. void
  117. md_show_usage (FILE * stream)
  118. {
  119. fprintf (stream, _(" M32C specific command line options:\n"));
  120. }
  121. static void
  122. s_bss (int ignore ATTRIBUTE_UNUSED)
  123. {
  124. int temp;
  125. temp = get_absolute_expression ();
  126. subseg_set (bss_section, (subsegT) temp);
  127. demand_empty_rest_of_line ();
  128. }
  129. /* The target specific pseudo-ops which we support. */
  130. const pseudo_typeS md_pseudo_table[] =
  131. {
  132. { "bss", s_bss, 0},
  133. { "3byte", cons, 3 },
  134. { "word", cons, 4 },
  135. { NULL, NULL, 0 }
  136. };
  137. void
  138. md_begin (void)
  139. {
  140. /* Initialize the `cgen' interface. */
  141. /* Set the machine number and endian. */
  142. gas_cgen_cpu_desc = m32c_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, cpu_mach,
  143. CGEN_CPU_OPEN_ENDIAN,
  144. CGEN_ENDIAN_BIG,
  145. CGEN_CPU_OPEN_ISAS, & m32c_isa,
  146. CGEN_CPU_OPEN_END);
  147. m32c_cgen_init_asm (gas_cgen_cpu_desc);
  148. /* This is a callback from cgen to gas to parse operands. */
  149. cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
  150. /* Set the ELF flags if desired. */
  151. if (m32c_flags)
  152. bfd_set_private_flags (stdoutput, m32c_flags);
  153. /* Set the machine type */
  154. bfd_default_set_arch_mach (stdoutput, bfd_arch_m32c, m32c_mach);
  155. insn_size = 0;
  156. }
  157. void
  158. m32c_md_end (void)
  159. {
  160. int i, n_nops;
  161. if (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE)
  162. {
  163. /* Pad with nops for objdump. */
  164. n_nops = (32 - ((insn_size) % 32)) / 8;
  165. for (i = 1; i <= n_nops; i++)
  166. md_assemble ("nop");
  167. }
  168. }
  169. void
  170. m32c_start_line_hook (void)
  171. {
  172. #if 0 /* not necessary....handled in the .cpu file */
  173. char *s = input_line_pointer;
  174. char *sg;
  175. for (s = input_line_pointer ; s && s[0] != '\n'; s++)
  176. {
  177. if (s[0] == ':')
  178. {
  179. /* Remove :g suffix. Squeeze out blanks. */
  180. if (s[1] == 'g')
  181. {
  182. for (sg = s - 1; sg && sg >= input_line_pointer; sg--)
  183. {
  184. sg[2] = sg[0];
  185. }
  186. sg[1] = ' ';
  187. sg[2] = ' ';
  188. input_line_pointer += 2;
  189. }
  190. }
  191. }
  192. #endif
  193. }
  194. /* Process [[indirect-operands]] in instruction str. */
  195. static bfd_boolean
  196. m32c_indirect_operand (char *str)
  197. {
  198. char *new_str;
  199. char *s;
  200. char *ns;
  201. int ns_len;
  202. char *ns_end;
  203. enum indirect_type {none, relative, absolute} ;
  204. enum indirect_type indirection [3] = { none, none, none };
  205. int brace_n [3] = { 0, 0, 0 };
  206. int operand;
  207. s = str;
  208. operand = 1;
  209. for (s = str; *s; s++)
  210. {
  211. if (s[0] == ',')
  212. operand = 2;
  213. /* [abs] where abs is not a0 or a1 */
  214. if (s[1] == '[' && ! (s[2] == 'a' && (s[3] == '0' || s[3] == '1'))
  215. && (ISBLANK (s[0]) || s[0] == ','))
  216. indirection[operand] = absolute;
  217. if (s[0] == ']' && s[1] == ']')
  218. indirection[operand] = relative;
  219. if (s[0] == '[' && s[1] == '[')
  220. indirection[operand] = relative;
  221. }
  222. if (indirection[1] == none && indirection[2] == none)
  223. return FALSE;
  224. operand = 1;
  225. ns_len = strlen (str);
  226. new_str = (char*) xmalloc (ns_len);
  227. ns = new_str;
  228. ns_end = ns + ns_len;
  229. for (s = str; *s; s++)
  230. {
  231. if (s[0] == ',')
  232. operand = 2;
  233. if (s[0] == '[' && ! brace_n[operand])
  234. {
  235. brace_n[operand] += 1;
  236. /* Squeeze [[ to [ if this is an indirect operand. */
  237. if (indirection[operand] != none)
  238. continue;
  239. }
  240. else if (s[0] == '[' && brace_n[operand])
  241. {
  242. brace_n[operand] += 1;
  243. }
  244. else if (s[0] == ']' && s[1] == ']' && indirection[operand] == relative)
  245. {
  246. s += 1; /* skip one ]. */
  247. brace_n[operand] -= 2; /* allow for 2 [. */
  248. }
  249. else if (s[0] == ']' && indirection[operand] == absolute)
  250. {
  251. brace_n[operand] -= 1;
  252. continue; /* skip closing ]. */
  253. }
  254. else if (s[0] == ']')
  255. {
  256. brace_n[operand] -= 1;
  257. }
  258. *ns = s[0];
  259. ns += 1;
  260. if (ns >= ns_end)
  261. return FALSE;
  262. if (s[0] == 0)
  263. break;
  264. }
  265. *ns = '\0';
  266. for (operand = 1; operand <= 2; operand++)
  267. if (brace_n[operand])
  268. {
  269. fprintf (stderr, "Unmatched [[operand-%d]] %d\n", operand, brace_n[operand]);
  270. }
  271. if (indirection[1] != none && indirection[2] != none)
  272. md_assemble ("src-dest-indirect");
  273. else if (indirection[1] != none)
  274. md_assemble ("src-indirect");
  275. else if (indirection[2] != none)
  276. md_assemble ("dest-indirect");
  277. md_assemble (new_str);
  278. free (new_str);
  279. return TRUE;
  280. }
  281. void
  282. md_assemble (char * str)
  283. {
  284. static int last_insn_had_delay_slot = 0;
  285. m32c_insn insn;
  286. char * errmsg;
  287. finished_insnS results;
  288. int rl_type;
  289. if (m32c_mach == bfd_mach_m32c && m32c_indirect_operand (str))
  290. return;
  291. /* Initialize GAS's cgen interface for a new instruction. */
  292. gas_cgen_init_parse ();
  293. insn.insn = m32c_cgen_assemble_insn
  294. (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
  295. if (!insn.insn)
  296. {
  297. as_bad ("%s", errmsg);
  298. return;
  299. }
  300. results.num_fixups = 0;
  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, &results);
  304. last_insn_had_delay_slot
  305. = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
  306. (void) last_insn_had_delay_slot;
  307. insn_size = CGEN_INSN_BITSIZE(insn.insn);
  308. rl_type = rl_for (insn);
  309. /* We have to mark all the jumps, because we need to adjust them
  310. when we delete bytes, but we only need to mark the displacements
  311. if they're symbolic - if they're not, we've already picked the
  312. shortest opcode by now. The linker, however, will still have to
  313. check any operands to see if they're the displacement type, since
  314. we don't know (nor record) *which* operands are relaxable. */
  315. if (m32c_relax
  316. && rl_type != RL_TYPE_NONE
  317. && (rl_type == RL_TYPE_JUMP || results.num_fixups)
  318. && !relaxable (insn))
  319. {
  320. int reloc = 0;
  321. int addend = results.num_fixups + 16 * insn_size/8;
  322. switch (rl_for (insn))
  323. {
  324. case RL_TYPE_JUMP: reloc = BFD_RELOC_M32C_RL_JUMP; break;
  325. case RL_TYPE_1ADDR: reloc = BFD_RELOC_M32C_RL_1ADDR; break;
  326. case RL_TYPE_2ADDR: reloc = BFD_RELOC_M32C_RL_2ADDR; break;
  327. }
  328. if (insn.insn->base->num == M32C_INSN_JMP16_S
  329. || insn.insn->base->num == M32C_INSN_JMP32_S)
  330. addend = 0x10;
  331. fix_new (results.frag,
  332. results.addr - results.frag->fr_literal,
  333. 0, abs_section_sym, addend, 0,
  334. reloc);
  335. }
  336. }
  337. /* The syntax in the manual says constants begin with '#'.
  338. We just ignore it. */
  339. void
  340. md_operand (expressionS * exp)
  341. {
  342. /* In case of a syntax error, escape back to try next syntax combo. */
  343. if (exp->X_op == O_absent)
  344. gas_cgen_md_operand (exp);
  345. }
  346. valueT
  347. md_section_align (segT segment, valueT size)
  348. {
  349. int align = bfd_get_section_alignment (stdoutput, segment);
  350. return ((size + (1 << align) - 1) & (-1 << align));
  351. }
  352. symbolS *
  353. md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
  354. {
  355. return 0;
  356. }
  357. const relax_typeS md_relax_table[] =
  358. {
  359. /* The fields are:
  360. 1) most positive reach of this state,
  361. 2) most negative reach of this state,
  362. 3) how many bytes this mode will have in the variable part of the frag
  363. 4) which index into the table to try if we can't fit into this one. */
  364. /* 0 */ { 0, 0, 0, 0 }, /* unused */
  365. /* 1 */ { 0, 0, 0, 0 }, /* marker for "don't know yet" */
  366. /* 2 */ { 127, -128, 2, 3 }, /* jcnd16_5.b */
  367. /* 3 */ { 32767, -32768, 5, 4 }, /* jcnd16_5.w */
  368. /* 4 */ { 0, 0, 6, 0 }, /* jcnd16_5.a */
  369. /* 5 */ { 127, -128, 2, 6 }, /* jcnd16.b */
  370. /* 6 */ { 32767, -32768, 5, 7 }, /* jcnd16.w */
  371. /* 7 */ { 0, 0, 6, 0 }, /* jcnd16.a */
  372. /* 8 */ { 8, 1, 1, 9 }, /* jmp16.s */
  373. /* 9 */ { 127, -128, 2, 10 }, /* jmp16.b */
  374. /* 10 */ { 32767, -32768, 3, 11 }, /* jmp16.w */
  375. /* 11 */ { 0, 0, 4, 0 }, /* jmp16.a */
  376. /* 12 */ { 127, -128, 2, 13 }, /* jcnd32.b */
  377. /* 13 */ { 32767, -32768, 5, 14 }, /* jcnd32.w */
  378. /* 14 */ { 0, 0, 6, 0 }, /* jcnd32.a */
  379. /* 15 */ { 8, 1, 1, 16 }, /* jmp32.s */
  380. /* 16 */ { 127, -128, 2, 17 }, /* jmp32.b */
  381. /* 17 */ { 32767, -32768, 3, 18 }, /* jmp32.w */
  382. /* 18 */ { 0, 0, 4, 0 }, /* jmp32.a */
  383. /* 19 */ { 32767, -32768, 3, 20 }, /* jsr16.w */
  384. /* 20 */ { 0, 0, 4, 0 }, /* jsr16.a */
  385. /* 21 */ { 32767, -32768, 3, 11 }, /* jsr32.w */
  386. /* 22 */ { 0, 0, 4, 0 }, /* jsr32.a */
  387. /* 23 */ { 0, 0, 3, 0 }, /* adjnz pc8 */
  388. /* 24 */ { 0, 0, 4, 0 }, /* adjnz disp8 pc8 */
  389. /* 25 */ { 0, 0, 5, 0 }, /* adjnz disp16 pc8 */
  390. /* 26 */ { 0, 0, 6, 0 } /* adjnz disp24 pc8 */
  391. };
  392. enum {
  393. M32C_MACRO_JCND16_5_W,
  394. M32C_MACRO_JCND16_5_A,
  395. M32C_MACRO_JCND16_W,
  396. M32C_MACRO_JCND16_A,
  397. M32C_MACRO_JCND32_W,
  398. M32C_MACRO_JCND32_A,
  399. /* the digit is the array index of the pcrel byte */
  400. M32C_MACRO_ADJNZ_2,
  401. M32C_MACRO_ADJNZ_3,
  402. M32C_MACRO_ADJNZ_4,
  403. M32C_MACRO_ADJNZ_5,
  404. } M32C_Macros;
  405. static struct {
  406. int insn;
  407. int bytes;
  408. int insn_for_extern;
  409. int pcrel_aim_offset;
  410. } subtype_mappings[] = {
  411. /* 0 */ { 0, 0, 0, 0 },
  412. /* 1 */ { 0, 0, 0, 0 },
  413. /* 2 */ { M32C_INSN_JCND16_5, 2, -M32C_MACRO_JCND16_5_A, 1 },
  414. /* 3 */ { -M32C_MACRO_JCND16_5_W, 5, -M32C_MACRO_JCND16_5_A, 4 },
  415. /* 4 */ { -M32C_MACRO_JCND16_5_A, 6, -M32C_MACRO_JCND16_5_A, 0 },
  416. /* 5 */ { M32C_INSN_JCND16, 3, -M32C_MACRO_JCND16_A, 1 },
  417. /* 6 */ { -M32C_MACRO_JCND16_W, 6, -M32C_MACRO_JCND16_A, 4 },
  418. /* 7 */ { -M32C_MACRO_JCND16_A, 7, -M32C_MACRO_JCND16_A, 0 },
  419. /* 8 */ { M32C_INSN_JMP16_S, 1, M32C_INSN_JMP16_A, 0 },
  420. /* 9 */ { M32C_INSN_JMP16_B, 2, M32C_INSN_JMP16_A, 1 },
  421. /* 10 */ { M32C_INSN_JMP16_W, 3, M32C_INSN_JMP16_A, 2 },
  422. /* 11 */ { M32C_INSN_JMP16_A, 4, M32C_INSN_JMP16_A, 0 },
  423. /* 12 */ { M32C_INSN_JCND32, 2, -M32C_MACRO_JCND32_A, 1 },
  424. /* 13 */ { -M32C_MACRO_JCND32_W, 5, -M32C_MACRO_JCND32_A, 4 },
  425. /* 14 */ { -M32C_MACRO_JCND32_A, 6, -M32C_MACRO_JCND32_A, 0 },
  426. /* 15 */ { M32C_INSN_JMP32_S, 1, M32C_INSN_JMP32_A, 0 },
  427. /* 16 */ { M32C_INSN_JMP32_B, 2, M32C_INSN_JMP32_A, 1 },
  428. /* 17 */ { M32C_INSN_JMP32_W, 3, M32C_INSN_JMP32_A, 2 },
  429. /* 18 */ { M32C_INSN_JMP32_A, 4, M32C_INSN_JMP32_A, 0 },
  430. /* 19 */ { M32C_INSN_JSR16_W, 3, M32C_INSN_JSR16_A, 2 },
  431. /* 20 */ { M32C_INSN_JSR16_A, 4, M32C_INSN_JSR16_A, 0 },
  432. /* 21 */ { M32C_INSN_JSR32_W, 3, M32C_INSN_JSR32_A, 2 },
  433. /* 22 */ { M32C_INSN_JSR32_A, 4, M32C_INSN_JSR32_A, 0 },
  434. /* 23 */ { -M32C_MACRO_ADJNZ_2, 3, -M32C_MACRO_ADJNZ_2, 0 },
  435. /* 24 */ { -M32C_MACRO_ADJNZ_3, 4, -M32C_MACRO_ADJNZ_3, 0 },
  436. /* 25 */ { -M32C_MACRO_ADJNZ_4, 5, -M32C_MACRO_ADJNZ_4, 0 },
  437. /* 26 */ { -M32C_MACRO_ADJNZ_5, 6, -M32C_MACRO_ADJNZ_5, 0 }
  438. };
  439. #define NUM_MAPPINGS (sizeof (subtype_mappings) / sizeof (subtype_mappings[0]))
  440. void
  441. m32c_prepare_relax_scan (fragS *fragP, offsetT *aim, relax_substateT this_state)
  442. {
  443. symbolS *symbolP = fragP->fr_symbol;
  444. if (symbolP && !S_IS_DEFINED (symbolP))
  445. *aim = 0;
  446. /* Adjust for m32c pcrel not being relative to the next opcode. */
  447. *aim += subtype_mappings[this_state].pcrel_aim_offset;
  448. }
  449. static int
  450. insn_to_subtype (int inum, const CGEN_INSN *insn)
  451. {
  452. unsigned int i;
  453. if (insn
  454. && (strncmp (insn->base->mnemonic, "adjnz", 5) == 0
  455. || strncmp (insn->base->mnemonic, "sbjnz", 5) == 0))
  456. {
  457. i = 23 + insn->base->bitsize/8 - 3;
  458. /*printf("mapping %d used for %s\n", i, insn->base->mnemonic);*/
  459. return i;
  460. }
  461. for (i=0; i<NUM_MAPPINGS; i++)
  462. if (inum == subtype_mappings[i].insn)
  463. {
  464. /*printf("mapping %d used\n", i);*/
  465. return i;
  466. }
  467. abort ();
  468. }
  469. /* Return an initial guess of the length by which a fragment must grow to
  470. hold a branch to reach its destination.
  471. Also updates fr_type/fr_subtype as necessary.
  472. Called just before doing relaxation.
  473. Any symbol that is now undefined will not become defined.
  474. The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  475. Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  476. Although it may not be explicit in the frag, pretend fr_var starts with a
  477. 0 value. */
  478. int
  479. md_estimate_size_before_relax (fragS * fragP, segT segment ATTRIBUTE_UNUSED)
  480. {
  481. int where = fragP->fr_opcode - fragP->fr_literal;
  482. if (fragP->fr_subtype == 1)
  483. fragP->fr_subtype = insn_to_subtype (fragP->fr_cgen.insn->base->num, fragP->fr_cgen.insn);
  484. if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
  485. {
  486. int new_insn;
  487. new_insn = subtype_mappings[fragP->fr_subtype].insn_for_extern;
  488. fragP->fr_subtype = insn_to_subtype (new_insn, 0);
  489. }
  490. if (fragP->fr_cgen.insn->base
  491. && fragP->fr_cgen.insn->base->num
  492. != subtype_mappings[fragP->fr_subtype].insn
  493. && subtype_mappings[fragP->fr_subtype].insn > 0)
  494. {
  495. int new_insn= subtype_mappings[fragP->fr_subtype].insn;
  496. if (new_insn >= 0)
  497. {
  498. fragP->fr_cgen.insn = (fragP->fr_cgen.insn
  499. - fragP->fr_cgen.insn->base->num
  500. + new_insn);
  501. }
  502. }
  503. return subtype_mappings[fragP->fr_subtype].bytes - (fragP->fr_fix - where);
  504. }
  505. /* *fragP has been relaxed to its final size, and now needs to have
  506. the bytes inside it modified to conform to the new size.
  507. Called after relaxation is finished.
  508. fragP->fr_type == rs_machine_dependent.
  509. fragP->fr_subtype is the subtype of what the address relaxed to. */
  510. static int
  511. target_address_for (fragS *frag)
  512. {
  513. int rv = frag->fr_offset;
  514. symbolS *sym = frag->fr_symbol;
  515. if (sym)
  516. rv += S_GET_VALUE (sym);
  517. /*printf("target_address_for returns %d\n", rv);*/
  518. return rv;
  519. }
  520. void
  521. md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
  522. segT sec ATTRIBUTE_UNUSED,
  523. fragS * fragP ATTRIBUTE_UNUSED)
  524. {
  525. int addend;
  526. int operand;
  527. int where = fragP->fr_opcode - fragP->fr_literal;
  528. int rl_where = fragP->fr_opcode - fragP->fr_literal;
  529. unsigned char *op = (unsigned char *)fragP->fr_opcode;
  530. int rl_addend = 0;
  531. addend = target_address_for (fragP) - (fragP->fr_address + where);
  532. fragP->fr_fix = where + subtype_mappings[fragP->fr_subtype].bytes;
  533. switch (subtype_mappings[fragP->fr_subtype].insn)
  534. {
  535. case M32C_INSN_JCND16_5:
  536. op[1] = addend - 1;
  537. operand = M32C_OPERAND_LAB_8_8;
  538. rl_addend = 0x21;
  539. break;
  540. case -M32C_MACRO_JCND16_5_W:
  541. op[0] ^= 0x04;
  542. op[1] = 4;
  543. op[2] = 0xf4;
  544. op[3] = addend - 3;
  545. op[4] = (addend - 3) >> 8;
  546. operand = M32C_OPERAND_LAB_8_16;
  547. where += 2;
  548. rl_addend = 0x51;
  549. break;
  550. case -M32C_MACRO_JCND16_5_A:
  551. op[0] ^= 0x04;
  552. op[1] = 5;
  553. op[2] = 0xfc;
  554. operand = M32C_OPERAND_LAB_8_24;
  555. where += 2;
  556. rl_addend = 0x61;
  557. break;
  558. case M32C_INSN_JCND16:
  559. op[2] = addend - 2;
  560. operand = M32C_OPERAND_LAB_16_8;
  561. rl_addend = 0x31;
  562. break;
  563. case -M32C_MACRO_JCND16_W:
  564. op[1] ^= 0x04;
  565. op[2] = 4;
  566. op[3] = 0xf4;
  567. op[4] = addend - 4;
  568. op[5] = (addend - 4) >> 8;
  569. operand = M32C_OPERAND_LAB_8_16;
  570. where += 3;
  571. rl_addend = 0x61;
  572. break;
  573. case -M32C_MACRO_JCND16_A:
  574. op[1] ^= 0x04;
  575. op[2] = 5;
  576. op[3] = 0xfc;
  577. operand = M32C_OPERAND_LAB_8_24;
  578. where += 3;
  579. rl_addend = 0x71;
  580. break;
  581. case M32C_INSN_JMP16_S:
  582. op[0] = 0x60 | ((addend-2) & 0x07);
  583. operand = M32C_OPERAND_LAB_5_3;
  584. rl_addend = 0x10;
  585. break;
  586. case M32C_INSN_JMP16_B:
  587. op[0] = 0xfe;
  588. op[1] = addend - 1;
  589. operand = M32C_OPERAND_LAB_8_8;
  590. rl_addend = 0x21;
  591. break;
  592. case M32C_INSN_JMP16_W:
  593. op[0] = 0xf4;
  594. op[1] = addend - 1;
  595. op[2] = (addend - 1) >> 8;
  596. operand = M32C_OPERAND_LAB_8_16;
  597. rl_addend = 0x31;
  598. break;
  599. case M32C_INSN_JMP16_A:
  600. op[0] = 0xfc;
  601. op[1] = 0;
  602. op[2] = 0;
  603. op[3] = 0;
  604. operand = M32C_OPERAND_LAB_8_24;
  605. rl_addend = 0x41;
  606. break;
  607. case M32C_INSN_JCND32:
  608. op[1] = addend - 1;
  609. operand = M32C_OPERAND_LAB_8_8;
  610. rl_addend = 0x21;
  611. break;
  612. case -M32C_MACRO_JCND32_W:
  613. op[0] ^= 0x40;
  614. op[1] = 4;
  615. op[2] = 0xce;
  616. op[3] = addend - 3;
  617. op[4] = (addend - 3) >> 8;
  618. operand = M32C_OPERAND_LAB_8_16;
  619. where += 2;
  620. rl_addend = 0x51;
  621. break;
  622. case -M32C_MACRO_JCND32_A:
  623. op[0] ^= 0x40;
  624. op[1] = 5;
  625. op[2] = 0xcc;
  626. operand = M32C_OPERAND_LAB_8_24;
  627. where += 2;
  628. rl_addend = 0x61;
  629. break;
  630. case M32C_INSN_JMP32_S:
  631. addend = ((addend-2) & 0x07);
  632. op[0] = 0x4a | (addend & 0x01) | ((addend << 3) & 0x30);
  633. operand = M32C_OPERAND_LAB32_JMP_S;
  634. rl_addend = 0x10;
  635. break;
  636. case M32C_INSN_JMP32_B:
  637. op[0] = 0xbb;
  638. op[1] = addend - 1;
  639. operand = M32C_OPERAND_LAB_8_8;
  640. rl_addend = 0x21;
  641. break;
  642. case M32C_INSN_JMP32_W:
  643. op[0] = 0xce;
  644. op[1] = addend - 1;
  645. op[2] = (addend - 1) >> 8;
  646. operand = M32C_OPERAND_LAB_8_16;
  647. rl_addend = 0x31;
  648. break;
  649. case M32C_INSN_JMP32_A:
  650. op[0] = 0xcc;
  651. op[1] = 0;
  652. op[2] = 0;
  653. op[3] = 0;
  654. operand = M32C_OPERAND_LAB_8_24;
  655. rl_addend = 0x41;
  656. break;
  657. case M32C_INSN_JSR16_W:
  658. op[0] = 0xf5;
  659. op[1] = addend - 1;
  660. op[2] = (addend - 1) >> 8;
  661. operand = M32C_OPERAND_LAB_8_16;
  662. rl_addend = 0x31;
  663. break;
  664. case M32C_INSN_JSR16_A:
  665. op[0] = 0xfd;
  666. op[1] = 0;
  667. op[2] = 0;
  668. op[3] = 0;
  669. operand = M32C_OPERAND_LAB_8_24;
  670. rl_addend = 0x41;
  671. break;
  672. case M32C_INSN_JSR32_W:
  673. op[0] = 0xcf;
  674. op[1] = addend - 1;
  675. op[2] = (addend - 1) >> 8;
  676. operand = M32C_OPERAND_LAB_8_16;
  677. rl_addend = 0x31;
  678. break;
  679. case M32C_INSN_JSR32_A:
  680. op[0] = 0xcd;
  681. op[1] = 0;
  682. op[2] = 0;
  683. op[3] = 0;
  684. operand = M32C_OPERAND_LAB_8_24;
  685. rl_addend = 0x41;
  686. break;
  687. case -M32C_MACRO_ADJNZ_2:
  688. rl_addend = 0x31;
  689. op[2] = addend - 2;
  690. operand = M32C_OPERAND_LAB_16_8;
  691. break;
  692. case -M32C_MACRO_ADJNZ_3:
  693. rl_addend = 0x41;
  694. op[3] = addend - 2;
  695. operand = M32C_OPERAND_LAB_24_8;
  696. break;
  697. case -M32C_MACRO_ADJNZ_4:
  698. rl_addend = 0x51;
  699. op[4] = addend - 2;
  700. operand = M32C_OPERAND_LAB_32_8;
  701. break;
  702. case -M32C_MACRO_ADJNZ_5:
  703. rl_addend = 0x61;
  704. op[5] = addend - 2;
  705. operand = M32C_OPERAND_LAB_40_8;
  706. break;
  707. default:
  708. printf("\nHey! Need more opcode converters! missing: %d %s\n\n",
  709. fragP->fr_subtype,
  710. fragP->fr_cgen.insn->base->name);
  711. abort();
  712. }
  713. if (m32c_relax)
  714. {
  715. if (operand != M32C_OPERAND_LAB_8_24)
  716. fragP->fr_offset = (fragP->fr_address + where);
  717. fix_new (fragP,
  718. rl_where,
  719. 0, abs_section_sym, rl_addend, 0,
  720. BFD_RELOC_M32C_RL_JUMP);
  721. }
  722. if (S_GET_SEGMENT (fragP->fr_symbol) != sec
  723. || operand == M32C_OPERAND_LAB_8_24
  724. || (m32c_relax && (operand != M32C_OPERAND_LAB_5_3
  725. && operand != M32C_OPERAND_LAB32_JMP_S)))
  726. {
  727. gas_assert (fragP->fr_cgen.insn != 0);
  728. gas_cgen_record_fixup (fragP,
  729. where,
  730. fragP->fr_cgen.insn,
  731. (fragP->fr_fix - where) * 8,
  732. cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
  733. operand),
  734. fragP->fr_cgen.opinfo,
  735. fragP->fr_symbol,
  736. fragP->fr_offset);
  737. }
  738. }
  739. /* Functions concerning relocs. */
  740. /* The location from which a PC relative jump should be calculated,
  741. given a PC relative reloc. */
  742. long
  743. md_pcrel_from_section (fixS * fixP, segT sec)
  744. {
  745. if (fixP->fx_addsy != (symbolS *) NULL
  746. && (! S_IS_DEFINED (fixP->fx_addsy)
  747. || S_GET_SEGMENT (fixP->fx_addsy) != sec))
  748. /* The symbol is undefined (or is defined but not in this section).
  749. Let the linker figure it out. */
  750. return 0;
  751. return (fixP->fx_frag->fr_address + fixP->fx_where);
  752. }
  753. /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
  754. Returns BFD_RELOC_NONE if no reloc type can be found.
  755. *FIXP may be modified if desired. */
  756. bfd_reloc_code_real_type
  757. md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
  758. const CGEN_OPERAND * operand,
  759. fixS * fixP ATTRIBUTE_UNUSED)
  760. {
  761. static const struct op_reloc {
  762. /* A CGEN operand type that can be a relocatable expression. */
  763. CGEN_OPERAND_TYPE operand;
  764. /* The appropriate BFD reloc type to use for that. */
  765. bfd_reloc_code_real_type reloc;
  766. /* The offset from the start of the instruction to the field to be
  767. relocated, in bytes. */
  768. int offset;
  769. } op_reloc_table[] = {
  770. /* PC-REL relocs for 8-bit fields. */
  771. { M32C_OPERAND_LAB_8_8, BFD_RELOC_8_PCREL, 1 },
  772. { M32C_OPERAND_LAB_16_8, BFD_RELOC_8_PCREL, 2 },
  773. { M32C_OPERAND_LAB_24_8, BFD_RELOC_8_PCREL, 3 },
  774. { M32C_OPERAND_LAB_32_8, BFD_RELOC_8_PCREL, 4 },
  775. { M32C_OPERAND_LAB_40_8, BFD_RELOC_8_PCREL, 5 },
  776. /* PC-REL relocs for 16-bit fields. */
  777. { M32C_OPERAND_LAB_8_16, BFD_RELOC_16_PCREL, 1 },
  778. /* Absolute relocs for 8-bit fields. */
  779. { M32C_OPERAND_IMM_8_QI, BFD_RELOC_8, 1 },
  780. { M32C_OPERAND_IMM_16_QI, BFD_RELOC_8, 2 },
  781. { M32C_OPERAND_IMM_24_QI, BFD_RELOC_8, 3 },
  782. { M32C_OPERAND_IMM_32_QI, BFD_RELOC_8, 4 },
  783. { M32C_OPERAND_IMM_40_QI, BFD_RELOC_8, 5 },
  784. { M32C_OPERAND_IMM_48_QI, BFD_RELOC_8, 6 },
  785. { M32C_OPERAND_IMM_56_QI, BFD_RELOC_8, 7 },
  786. { M32C_OPERAND_DSP_8_S8, BFD_RELOC_8, 1 },
  787. { M32C_OPERAND_DSP_16_S8, BFD_RELOC_8, 2 },
  788. { M32C_OPERAND_DSP_24_S8, BFD_RELOC_8, 3 },
  789. { M32C_OPERAND_DSP_32_S8, BFD_RELOC_8, 4 },
  790. { M32C_OPERAND_DSP_40_S8, BFD_RELOC_8, 5 },
  791. { M32C_OPERAND_DSP_48_S8, BFD_RELOC_8, 6 },
  792. { M32C_OPERAND_DSP_8_U8, BFD_RELOC_8, 1 },
  793. { M32C_OPERAND_DSP_16_U8, BFD_RELOC_8, 2 },
  794. { M32C_OPERAND_DSP_24_U8, BFD_RELOC_8, 3 },
  795. { M32C_OPERAND_DSP_32_U8, BFD_RELOC_8, 4 },
  796. { M32C_OPERAND_DSP_40_U8, BFD_RELOC_8, 5 },
  797. { M32C_OPERAND_DSP_48_U8, BFD_RELOC_8, 6 },
  798. { M32C_OPERAND_BITBASE32_16_S11_UNPREFIXED, BFD_RELOC_8, 2 },
  799. { M32C_OPERAND_BITBASE32_16_U11_UNPREFIXED, BFD_RELOC_8, 2 },
  800. { M32C_OPERAND_BITBASE32_24_S11_PREFIXED, BFD_RELOC_8, 3 },
  801. { M32C_OPERAND_BITBASE32_24_U11_PREFIXED, BFD_RELOC_8, 3 },
  802. /* Absolute relocs for 16-bit fields. */
  803. { M32C_OPERAND_IMM_8_HI, BFD_RELOC_16, 1 },
  804. { M32C_OPERAND_IMM_16_HI, BFD_RELOC_16, 2 },
  805. { M32C_OPERAND_IMM_24_HI, BFD_RELOC_16, 3 },
  806. { M32C_OPERAND_IMM_32_HI, BFD_RELOC_16, 4 },
  807. { M32C_OPERAND_IMM_40_HI, BFD_RELOC_16, 5 },
  808. { M32C_OPERAND_IMM_48_HI, BFD_RELOC_16, 6 },
  809. { M32C_OPERAND_IMM_56_HI, BFD_RELOC_16, 7 },
  810. { M32C_OPERAND_IMM_64_HI, BFD_RELOC_16, 8 },
  811. { M32C_OPERAND_DSP_16_S16, BFD_RELOC_16, 2 },
  812. { M32C_OPERAND_DSP_24_S16, BFD_RELOC_16, 3 },
  813. { M32C_OPERAND_DSP_32_S16, BFD_RELOC_16, 4 },
  814. { M32C_OPERAND_DSP_40_S16, BFD_RELOC_16, 5 },
  815. { M32C_OPERAND_DSP_8_U16, BFD_RELOC_16, 1 },
  816. { M32C_OPERAND_DSP_16_U16, BFD_RELOC_16, 2 },
  817. { M32C_OPERAND_DSP_24_U16, BFD_RELOC_16, 3 },
  818. { M32C_OPERAND_DSP_32_U16, BFD_RELOC_16, 4 },
  819. { M32C_OPERAND_DSP_40_U16, BFD_RELOC_16, 5 },
  820. { M32C_OPERAND_DSP_48_U16, BFD_RELOC_16, 6 },
  821. { M32C_OPERAND_BITBASE32_16_S19_UNPREFIXED, BFD_RELOC_16, 2 },
  822. { M32C_OPERAND_BITBASE32_16_U19_UNPREFIXED, BFD_RELOC_16, 2 },
  823. { M32C_OPERAND_BITBASE32_24_S19_PREFIXED, BFD_RELOC_16, 3 },
  824. { M32C_OPERAND_BITBASE32_24_U19_PREFIXED, BFD_RELOC_16, 3 },
  825. /* Absolute relocs for 24-bit fields. */
  826. { M32C_OPERAND_LAB_8_24, BFD_RELOC_24, 1 },
  827. { M32C_OPERAND_DSP_8_S24, BFD_RELOC_24, 1 },
  828. { M32C_OPERAND_DSP_8_U24, BFD_RELOC_24, 1 },
  829. { M32C_OPERAND_DSP_16_U24, BFD_RELOC_24, 2 },
  830. { M32C_OPERAND_DSP_24_U24, BFD_RELOC_24, 3 },
  831. { M32C_OPERAND_DSP_32_U24, BFD_RELOC_24, 4 },
  832. { M32C_OPERAND_DSP_40_U24, BFD_RELOC_24, 5 },
  833. { M32C_OPERAND_DSP_48_U24, BFD_RELOC_24, 6 },
  834. { M32C_OPERAND_DSP_16_U20, BFD_RELOC_24, 2 },
  835. { M32C_OPERAND_DSP_24_U20, BFD_RELOC_24, 3 },
  836. { M32C_OPERAND_DSP_32_U20, BFD_RELOC_24, 4 },
  837. { M32C_OPERAND_BITBASE32_16_U27_UNPREFIXED, BFD_RELOC_24, 2 },
  838. { M32C_OPERAND_BITBASE32_24_U27_PREFIXED, BFD_RELOC_24, 3 },
  839. /* Absolute relocs for 32-bit fields. */
  840. { M32C_OPERAND_IMM_16_SI, BFD_RELOC_32, 2 },
  841. { M32C_OPERAND_IMM_24_SI, BFD_RELOC_32, 3 },
  842. { M32C_OPERAND_IMM_32_SI, BFD_RELOC_32, 4 },
  843. { M32C_OPERAND_IMM_40_SI, BFD_RELOC_32, 5 },
  844. };
  845. int i;
  846. for (i = ARRAY_SIZE (op_reloc_table); --i >= 0; )
  847. {
  848. const struct op_reloc *or = &op_reloc_table[i];
  849. if (or->operand == operand->type)
  850. {
  851. fixP->fx_where += or->offset;
  852. fixP->fx_size -= or->offset;
  853. if (fixP->fx_cgen.opinfo
  854. && fixP->fx_cgen.opinfo != BFD_RELOC_NONE)
  855. return fixP->fx_cgen.opinfo;
  856. return or->reloc;
  857. }
  858. }
  859. fprintf
  860. (stderr,
  861. "Error: tc-m32c.c:md_cgen_lookup_reloc Unimplemented relocation for operand %s\n",
  862. operand->name);
  863. return BFD_RELOC_NONE;
  864. }
  865. void
  866. m32c_cons_fix_new (fragS * frag,
  867. int where,
  868. int size,
  869. expressionS *exp,
  870. bfd_reloc_code_real_type type)
  871. {
  872. switch (size)
  873. {
  874. case 1:
  875. type = BFD_RELOC_8;
  876. break;
  877. case 2:
  878. type = BFD_RELOC_16;
  879. break;
  880. case 3:
  881. type = BFD_RELOC_24;
  882. break;
  883. case 4:
  884. default:
  885. type = BFD_RELOC_32;
  886. break;
  887. case 8:
  888. type = BFD_RELOC_64;
  889. break;
  890. }
  891. fix_new_exp (frag, where, (int) size, exp, 0, type);
  892. }
  893. void
  894. m32c_apply_fix (struct fix *f, valueT *t, segT s)
  895. {
  896. if (f->fx_r_type == BFD_RELOC_M32C_RL_JUMP
  897. || f->fx_r_type == BFD_RELOC_M32C_RL_1ADDR
  898. || f->fx_r_type == BFD_RELOC_M32C_RL_2ADDR)
  899. return;
  900. gas_cgen_md_apply_fix (f, t, s);
  901. }
  902. arelent *
  903. tc_gen_reloc (asection *sec, fixS *fx)
  904. {
  905. if (fx->fx_r_type == BFD_RELOC_M32C_RL_JUMP
  906. || fx->fx_r_type == BFD_RELOC_M32C_RL_1ADDR
  907. || fx->fx_r_type == BFD_RELOC_M32C_RL_2ADDR)
  908. {
  909. arelent * reloc;
  910. reloc = xmalloc (sizeof (* reloc));
  911. reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
  912. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fx->fx_addsy);
  913. reloc->address = fx->fx_frag->fr_address + fx->fx_where;
  914. reloc->howto = bfd_reloc_type_lookup (stdoutput, fx->fx_r_type);
  915. reloc->addend = fx->fx_offset;
  916. return reloc;
  917. }
  918. return gas_cgen_tc_gen_reloc (sec, fx);
  919. }
  920. /* See whether we need to force a relocation into the output file.
  921. This is used to force out switch and PC relative relocations when
  922. relaxing. */
  923. int
  924. m32c_force_relocation (fixS * fixp)
  925. {
  926. int reloc = fixp->fx_r_type;
  927. if (reloc > (int)BFD_RELOC_UNUSED)
  928. {
  929. reloc -= (int)BFD_RELOC_UNUSED;
  930. switch (reloc)
  931. {
  932. case M32C_OPERAND_DSP_32_S16:
  933. case M32C_OPERAND_DSP_32_U16:
  934. case M32C_OPERAND_IMM_32_HI:
  935. case M32C_OPERAND_DSP_16_S16:
  936. case M32C_OPERAND_DSP_16_U16:
  937. case M32C_OPERAND_IMM_16_HI:
  938. case M32C_OPERAND_DSP_24_S16:
  939. case M32C_OPERAND_DSP_24_U16:
  940. case M32C_OPERAND_IMM_24_HI:
  941. return 1;
  942. /* If we're doing linker relaxing, we need to keep all the
  943. pc-relative jumps in case we need to fix them due to
  944. deleted bytes between the jump and its destination. */
  945. case M32C_OPERAND_LAB_8_8:
  946. case M32C_OPERAND_LAB_8_16:
  947. case M32C_OPERAND_LAB_8_24:
  948. case M32C_OPERAND_LAB_16_8:
  949. case M32C_OPERAND_LAB_24_8:
  950. case M32C_OPERAND_LAB_32_8:
  951. case M32C_OPERAND_LAB_40_8:
  952. if (m32c_relax)
  953. return 1;
  954. default:
  955. break;
  956. }
  957. }
  958. else
  959. {
  960. switch (fixp->fx_r_type)
  961. {
  962. case BFD_RELOC_16:
  963. return 1;
  964. case BFD_RELOC_M32C_RL_JUMP:
  965. case BFD_RELOC_M32C_RL_1ADDR:
  966. case BFD_RELOC_M32C_RL_2ADDR:
  967. case BFD_RELOC_8_PCREL:
  968. case BFD_RELOC_16_PCREL:
  969. if (m32c_relax)
  970. return 1;
  971. default:
  972. break;
  973. }
  974. }
  975. return generic_force_reloc (fixp);
  976. }
  977. /* Write a value out to the object file, using the appropriate endianness. */
  978. void
  979. md_number_to_chars (char * buf, valueT val, int n)
  980. {
  981. number_to_chars_littleendian (buf, val, n);
  982. }
  983. /* Turn a string in input_line_pointer into a floating point constant of type
  984. type, and store the appropriate bytes in *litP. The number of LITTLENUMS
  985. emitted is stored in *sizeP . An error message is returned, or NULL on OK. */
  986. /* Equal to MAX_PRECISION in atof-ieee.c. */
  987. #define MAX_LITTLENUMS 6
  988. char *
  989. md_atof (int type, char * litP, int * sizeP)
  990. {
  991. return ieee_md_atof (type, litP, sizeP, TRUE);
  992. }
  993. bfd_boolean
  994. m32c_fix_adjustable (fixS * fixP)
  995. {
  996. int reloc;
  997. if (fixP->fx_addsy == NULL)
  998. return 1;
  999. /* We need the symbol name for the VTABLE entries. */
  1000. reloc = fixP->fx_r_type;
  1001. if (reloc > (int)BFD_RELOC_UNUSED)
  1002. {
  1003. reloc -= (int)BFD_RELOC_UNUSED;
  1004. switch (reloc)
  1005. {
  1006. case M32C_OPERAND_DSP_32_S16:
  1007. case M32C_OPERAND_DSP_32_U16:
  1008. case M32C_OPERAND_IMM_32_HI:
  1009. case M32C_OPERAND_DSP_16_S16:
  1010. case M32C_OPERAND_DSP_16_U16:
  1011. case M32C_OPERAND_IMM_16_HI:
  1012. case M32C_OPERAND_DSP_24_S16:
  1013. case M32C_OPERAND_DSP_24_U16:
  1014. case M32C_OPERAND_IMM_24_HI:
  1015. return 0;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. if (fixP->fx_r_type == BFD_RELOC_16)
  1021. return 0;
  1022. }
  1023. /* Do not adjust relocations involving symbols in merged sections.
  1024. A reloc patching in the value of some symbol S plus some addend A
  1025. can be produced in different ways:
  1026. 1) It might simply be a reference to the data at S + A. Clearly,
  1027. if linker merging shift that data around, the value patched in
  1028. by the reloc needs to be adjusted accordingly.
  1029. 2) Or, it might be a reference to S, with A added in as a constant
  1030. bias. For example, given code like this:
  1031. static int S[100];
  1032. ... S[i - 8] ...
  1033. it would be reasonable for the compiler to rearrange the array
  1034. reference to something like:
  1035. ... (S-8)[i] ...
  1036. and emit assembly code that refers to S - (8 * sizeof (int)),
  1037. so the subtraction is done entirely at compile-time. In this
  1038. case, the reloc's addend A would be -(8 * sizeof (int)), and
  1039. shifting around code or data at S + A should not affect the
  1040. reloc: the reloc isn't referring to that code or data at all.
  1041. The linker has no way of knowing which case it has in hand. So,
  1042. to disambiguate, we have the linker always treat reloc addends as
  1043. in case 2): they're constants that should be simply added to the
  1044. symbol value, just like the reloc says. And we express case 1)
  1045. in different way: we have the compiler place a label at the real
  1046. target, and reference that label with an addend of zero. (The
  1047. compiler is unlikely to reference code using a label plus an
  1048. offset anyway, since it doesn't know the sizes of the
  1049. instructions.)
  1050. The simplification being done by gas/write.c:adjust_reloc_syms,
  1051. however, turns the explicit-label usage into the label-plus-
  1052. offset usage, re-introducing the ambiguity the compiler avoided.
  1053. So we need to disable that simplification for symbols referring
  1054. to merged data.
  1055. This only affects object size a little bit. */
  1056. if (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE)
  1057. return 0;
  1058. if (m32c_relax)
  1059. return 0;
  1060. return 1;
  1061. }
  1062. /* Worker function for m32c_is_colon_insn(). */
  1063. static int
  1064. restore_colon (char *next_i_l_p, char *nul_char)
  1065. {
  1066. /* Restore the colon, and advance input_line_pointer to
  1067. the end of the new symbol. */
  1068. *input_line_pointer = *nul_char;
  1069. input_line_pointer = next_i_l_p;
  1070. *nul_char = *next_i_l_p;
  1071. *next_i_l_p = 0;
  1072. return 1;
  1073. }
  1074. /* Determines if the symbol starting at START and ending in
  1075. a colon that was at the location pointed to by INPUT_LINE_POINTER
  1076. (but which has now been replaced bu a NUL) is in fact an
  1077. :Z, :S, :Q, or :G suffix.
  1078. If it is, then it restores the colon, advances INPUT_LINE_POINTER
  1079. to the real end of the instruction/symbol, saves the char there to
  1080. NUL_CHAR and pokes a NUL, and returns 1. Otherwise it returns 0. */
  1081. int
  1082. m32c_is_colon_insn (char *start ATTRIBUTE_UNUSED, char *nul_char)
  1083. {
  1084. char * i_l_p = input_line_pointer;
  1085. if (*nul_char == '"')
  1086. ++i_l_p;
  1087. /* Check to see if the text following the colon is 'G' */
  1088. if (TOLOWER (i_l_p[1]) == 'g' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
  1089. return restore_colon (i_l_p + 2, nul_char);
  1090. /* Check to see if the text following the colon is 'Q' */
  1091. if (TOLOWER (i_l_p[1]) == 'q' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
  1092. return restore_colon (i_l_p + 2, nul_char);
  1093. /* Check to see if the text following the colon is 'S' */
  1094. if (TOLOWER (i_l_p[1]) == 's' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
  1095. return restore_colon (i_l_p + 2, nul_char);
  1096. /* Check to see if the text following the colon is 'Z' */
  1097. if (TOLOWER (i_l_p[1]) == 'z' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
  1098. return restore_colon (i_l_p + 2, nul_char);
  1099. return 0;
  1100. }