* config/tc-mips.c (append_insn): Correctly handle mips16 case
[binutils.git] / gas / config / tc-m32c.c
blob9ccc9a7f8c8bb7875c0b4fa81a2ee7a57255b704
1 /* tc-m32c.c -- Assembler for the Renesas M32C.
2 Copyright (C) 2005 Free Software Foundation.
3 Contributed by RedHat.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <stdio.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "symcat.h"
26 #include "opcodes/m32c-desc.h"
27 #include "opcodes/m32c-opc.h"
28 #include "cgen.h"
29 #include "elf/common.h"
30 #include "elf/m32c.h"
31 #include "libbfd.h"
32 #include "libiberty.h"
33 #include "safe-ctype.h"
35 /* Structure to hold all of the different components
36 describing an individual instruction. */
37 typedef struct
39 const CGEN_INSN * insn;
40 const CGEN_INSN * orig_insn;
41 CGEN_FIELDS fields;
42 #if CGEN_INT_INSN_P
43 CGEN_INSN_INT buffer [1];
44 #define INSN_VALUE(buf) (*(buf))
45 #else
46 unsigned char buffer [CGEN_MAX_INSN_SIZE];
47 #define INSN_VALUE(buf) (buf)
48 #endif
49 char * addr;
50 fragS * frag;
51 int num_fixups;
52 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
53 int indices [MAX_OPERAND_INSTANCES];
55 m32c_insn;
57 const char comment_chars[] = ";";
58 const char line_comment_chars[] = "#";
59 const char line_separator_chars[] = "|";
60 const char EXP_CHARS[] = "eE";
61 const char FLT_CHARS[] = "dD";
63 #define M32C_SHORTOPTS ""
64 const char * md_shortopts = M32C_SHORTOPTS;
66 /* assembler options */
67 #define OPTION_CPU_M16C (OPTION_MD_BASE)
68 #define OPTION_CPU_M32C (OPTION_MD_BASE + 1)
70 struct option md_longopts[] =
72 { "m16c", no_argument, NULL, OPTION_CPU_M16C },
73 { "m32c", no_argument, NULL, OPTION_CPU_M32C },
74 {NULL, no_argument, NULL, 0}
76 size_t md_longopts_size = sizeof (md_longopts);
78 /* Default machine */
80 #define DEFAULT_MACHINE bfd_mach_m16c
81 #define DEFAULT_FLAGS EF_M32C_CPU_M16C
83 static unsigned long m32c_mach = bfd_mach_m16c;
84 static int cpu_mach = (1 << MACH_M16C);
85 static int insn_size;
87 /* Flags to set in the elf header */
88 static flagword m32c_flags = DEFAULT_FLAGS;
90 static unsigned int m32c_isa = (1 << ISA_M16C);
92 static void
93 set_isa (enum isa_attr isa_num)
95 m32c_isa = (1 << isa_num);
98 static void s_bss (int);
101 md_parse_option (int c, char * arg ATTRIBUTE_UNUSED)
103 switch (c)
105 case OPTION_CPU_M16C:
106 m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M16C;
107 m32c_mach = bfd_mach_m16c;
108 cpu_mach = (1 << MACH_M16C);
109 set_isa (ISA_M16C);
110 break;
112 case OPTION_CPU_M32C:
113 m32c_flags = (m32c_flags & ~EF_M32C_CPU_MASK) | EF_M32C_CPU_M32C;
114 m32c_mach = bfd_mach_m32c;
115 cpu_mach = (1 << MACH_M32C);
116 set_isa (ISA_M32C);
117 break;
119 default:
120 return 0;
122 return 1;
125 void
126 md_show_usage (FILE * stream)
128 fprintf (stream, _(" M32C specific command line options:\n"));
131 static void
132 s_bss (int ignore ATTRIBUTE_UNUSED)
134 int temp;
136 temp = get_absolute_expression ();
137 subseg_set (bss_section, (subsegT) temp);
138 demand_empty_rest_of_line ();
141 /* The target specific pseudo-ops which we support. */
142 const pseudo_typeS md_pseudo_table[] =
144 { "bss", s_bss, 0},
145 { "word", cons, 4 },
146 { NULL, NULL, 0 }
150 void
151 md_begin (void)
153 /* Initialize the `cgen' interface. */
155 /* Set the machine number and endian. */
156 gas_cgen_cpu_desc = m32c_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, cpu_mach,
157 CGEN_CPU_OPEN_ENDIAN,
158 CGEN_ENDIAN_BIG,
159 CGEN_CPU_OPEN_ISAS, m32c_isa,
160 CGEN_CPU_OPEN_END);
162 m32c_cgen_init_asm (gas_cgen_cpu_desc);
164 /* This is a callback from cgen to gas to parse operands. */
165 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
167 /* Set the ELF flags if desired. */
168 if (m32c_flags)
169 bfd_set_private_flags (stdoutput, m32c_flags);
171 /* Set the machine type */
172 bfd_default_set_arch_mach (stdoutput, bfd_arch_m32c, m32c_mach);
174 insn_size = 0;
177 void
178 m32c_md_end (void)
180 int i, n_nops;
182 /* Pad with nops for objdump. */
183 n_nops = (32 - ((insn_size) % 32)) / 8;
184 for (i = 1; i <= n_nops; i++)
185 md_assemble ("nop");
188 void
189 m32c_start_line_hook (void)
191 #if 0 /* not necessary....handled in the .cpu file */
192 char *s = input_line_pointer;
193 char *sg;
195 for (s = input_line_pointer ; s && s[0] != '\n'; s++)
197 if (s[0] == ':')
199 /* Remove :g suffix. Squeeze out blanks. */
200 if (s[1] == 'g')
202 for (sg = s - 1; sg && sg >= input_line_pointer; sg--)
204 sg[2] = sg[0];
206 sg[1] = ' ';
207 sg[2] = ' ';
208 input_line_pointer += 2;
212 #endif
215 /* Process [[indirect-operands]] in instruction str. */
217 static bfd_boolean
218 m32c_indirect_operand (char *str)
220 char *new_str;
221 char *s;
222 char *ns;
223 int ns_len;
224 char *ns_end;
225 enum indirect_type {none, relative, absolute} ;
226 enum indirect_type indirection [3] = { none, none, none };
227 int brace_n [3] = { 0, 0, 0 };
228 int operand;
230 s = str;
231 operand = 1;
232 for (s = str; *s; s++)
234 if (s[0] == ',')
235 operand = 2;
236 /* [abs] where abs is not a0 or a1 */
237 if (s[1] == '[' && ! (s[2] == 'a' && (s[3] == '0' || s[3] == '1'))
238 && (ISBLANK (s[0]) || s[0] == ','))
239 indirection[operand] = absolute;
240 if (s[0] == ']' && s[1] == ']')
241 indirection[operand] = relative;
242 if (s[0] == '[' && s[1] == '[')
243 indirection[operand] = relative;
246 if (indirection[1] == none && indirection[2] == none)
247 return FALSE;
249 operand = 1;
250 ns_len = strlen (str);
251 new_str = (char*) xmalloc (ns_len);
252 ns = new_str;
253 ns_end = ns + ns_len;
255 for (s = str; *s; s++)
257 if (s[0] == ',')
258 operand = 2;
260 if (s[0] == '[' && ! brace_n[operand])
262 brace_n[operand] += 1;
263 /* Squeeze [[ to [ if this is an indirect operand. */
264 if (indirection[operand] != none)
265 continue;
268 else if (s[0] == '[' && brace_n[operand])
270 brace_n[operand] += 1;
272 else if (s[0] == ']' && s[1] == ']' && indirection[operand] == relative)
274 s += 1; /* skip one ]. */
275 brace_n[operand] -= 2; /* allow for 2 [. */
277 else if (s[0] == ']' && indirection[operand] == absolute)
279 brace_n[operand] -= 1;
280 continue; /* skip closing ]. */
282 else if (s[0] == ']')
284 brace_n[operand] -= 1;
286 *ns = s[0];
287 ns += 1;
288 if (ns >= ns_end)
289 return FALSE;
290 if (s[0] == 0)
291 break;
293 *ns = '\0';
294 for (operand = 1; operand <= 2; operand++)
295 if (brace_n[operand])
297 fprintf (stderr, "Unmatched [[operand-%d]] %d\n", operand, brace_n[operand]);
300 if (indirection[1] != none && indirection[2] != none)
301 md_assemble ("src-dest-indirect");
302 else if (indirection[1] != none)
303 md_assemble ("src-indirect");
304 else if (indirection[2] != none)
305 md_assemble ("dest-indirect");
307 md_assemble (new_str);
308 free (new_str);
309 return TRUE;
312 void
313 md_assemble (char * str)
315 static int last_insn_had_delay_slot = 0;
316 m32c_insn insn;
317 char * errmsg;
319 if (m32c_mach == bfd_mach_m32c && m32c_indirect_operand (str))
320 return;
322 /* Initialize GAS's cgen interface for a new instruction. */
323 gas_cgen_init_parse ();
325 insn.insn = m32c_cgen_assemble_insn
326 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
328 if (!insn.insn)
330 as_bad (errmsg);
331 return;
334 /* Doesn't really matter what we pass for RELAX_P here. */
335 gas_cgen_finish_insn (insn.insn, insn.buffer,
336 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
338 last_insn_had_delay_slot
339 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
340 insn_size = CGEN_INSN_BITSIZE(insn.insn);
343 /* The syntax in the manual says constants begin with '#'.
344 We just ignore it. */
346 void
347 md_operand (expressionS * exp)
349 /* In case of a syntax error, escape back to try next syntax combo. */
350 if (exp->X_op == O_absent)
351 gas_cgen_md_operand (exp);
354 valueT
355 md_section_align (segT segment, valueT size)
357 int align = bfd_get_section_alignment (stdoutput, segment);
358 return ((size + (1 << align) - 1) & (-1 << align));
361 symbolS *
362 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
364 return 0;
367 const relax_typeS md_relax_table[] =
369 /* The fields are:
370 1) most positive reach of this state,
371 2) most negative reach of this state,
372 3) how many bytes this mode will have in the variable part of the frag
373 4) which index into the table to try if we can't fit into this one. */
375 /* 0 */ { 0, 0, 0, 0 }, /* unused */
376 /* 1 */ { 0, 0, 0, 0 }, /* marker for "don't know yet" */
378 /* 2 */ { 127, -128, 2, 3 }, /* jcnd16_5.b */
379 /* 3 */ { 32767, -32768, 5, 4 }, /* jcnd16_5.w */
380 /* 4 */ { 0, 0, 6, 0 }, /* jcnd16_5.a */
382 /* 5 */ { 127, -128, 2, 6 }, /* jcnd16.b */
383 /* 6 */ { 32767, -32768, 5, 7 }, /* jcnd16.w */
384 /* 7 */ { 0, 0, 6, 0 }, /* jcnd16.a */
386 /* 8 */ { 8, 1, 1, 9 }, /* jmp16.s */
387 /* 9 */ { 127, -128, 2, 10 }, /* jmp16.b */
388 /* 10 */ { 32767, -32768, 3, 11 }, /* jmp16.w */
389 /* 11 */ { 0, 0, 4, 0 }, /* jmp16.a */
391 /* 12 */ { 127, -128, 2, 13 }, /* jcnd32.b */
392 /* 13 */ { 32767, -32768, 5, 14 }, /* jcnd32.w */
393 /* 14 */ { 0, 0, 6, 0 }, /* jcnd32.a */
395 /* 15 */ { 8, 1, 1, 16 }, /* jmp32.s */
396 /* 16 */ { 127, -128, 2, 17 }, /* jmp32.b */
397 /* 17 */ { 32767, -32768, 3, 18 }, /* jmp32.w */
398 /* 18 */ { 0, 0, 4, 0 } /* jmp32.a */
401 enum {
402 M32C_MACRO_JCND16_5_W,
403 M32C_MACRO_JCND16_5_A,
404 M32C_MACRO_JCND16_W,
405 M32C_MACRO_JCND16_A,
406 M32C_MACRO_JCND32_W,
407 M32C_MACRO_JCND32_A,
408 } M32C_Macros;
410 static struct {
411 int insn;
412 int bytes;
413 int insn_for_extern;
414 int pcrel_aim_offset;
415 } subtype_mappings[] = {
416 /* 0 */ { 0, 0, 0, 0 },
417 /* 1 */ { 0, 0, 0, 0 },
419 /* 2 */ { M32C_INSN_JCND16_5, 2, -M32C_MACRO_JCND16_5_A, 1 },
420 /* 3 */ { -M32C_MACRO_JCND16_5_W, 5, -M32C_MACRO_JCND16_5_A, 4 },
421 /* 4 */ { -M32C_MACRO_JCND16_5_A, 6, -M32C_MACRO_JCND16_5_A, 0 },
423 /* 5 */ { M32C_INSN_JCND16, 3, -M32C_MACRO_JCND16_A, 1 },
424 /* 6 */ { -M32C_MACRO_JCND16_W, 6, -M32C_MACRO_JCND16_A, 4 },
425 /* 7 */ { -M32C_MACRO_JCND16_A, 7, -M32C_MACRO_JCND16_A, 0 },
427 /* 8 */ { M32C_INSN_JMP16_S, 1, M32C_INSN_JMP16_A, 0 },
428 /* 9 */ { M32C_INSN_JMP16_B, 2, M32C_INSN_JMP16_A, 1 },
429 /* 10 */ { M32C_INSN_JMP16_W, 3, M32C_INSN_JMP16_A, 2 },
430 /* 11 */ { M32C_INSN_JMP16_A, 4, M32C_INSN_JMP16_A, 0 },
432 /* 12 */ { M32C_INSN_JCND32, 2, -M32C_MACRO_JCND32_A, 1 },
433 /* 13 */ { -M32C_MACRO_JCND32_W, 5, -M32C_MACRO_JCND32_A, 4 },
434 /* 14 */ { -M32C_MACRO_JCND32_A, 6, -M32C_MACRO_JCND32_A, 0 },
436 /* 15 */ { M32C_INSN_JMP32_S, 1, M32C_INSN_JMP32_A, 0 },
437 /* 16 */ { M32C_INSN_JMP32_B, 2, M32C_INSN_JMP32_A, 1 },
438 /* 17 */ { M32C_INSN_JMP32_W, 3, M32C_INSN_JMP32_A, 2 },
439 /* 18 */ { M32C_INSN_JMP32_A, 4, M32C_INSN_JMP32_A, 0 }
441 #define NUM_MAPPINGS (sizeof (subtype_mappings) / sizeof (subtype_mappings[0]))
443 void
444 m32c_prepare_relax_scan (fragS *fragP, offsetT *aim, relax_substateT this_state)
446 symbolS *symbolP = fragP->fr_symbol;
447 if (symbolP && !S_IS_DEFINED (symbolP))
448 *aim = 0;
449 /* Adjust for m32c pcrel not being relative to the next opcode. */
450 *aim += subtype_mappings[this_state].pcrel_aim_offset;
453 static int
454 insn_to_subtype (int insn)
456 unsigned int i;
457 for (i=0; i<NUM_MAPPINGS; i++)
458 if (insn == subtype_mappings[i].insn)
460 /*printf("mapping %d used\n", i);*/
461 return i;
463 abort ();
466 /* Return an initial guess of the length by which a fragment must grow to
467 hold a branch to reach its destination.
468 Also updates fr_type/fr_subtype as necessary.
470 Called just before doing relaxation.
471 Any symbol that is now undefined will not become defined.
472 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
473 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
474 Although it may not be explicit in the frag, pretend fr_var starts with a
475 0 value. */
478 md_estimate_size_before_relax (fragS * fragP, segT segment ATTRIBUTE_UNUSED)
480 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);
485 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
487 int new_insn;
489 new_insn = subtype_mappings[fragP->fr_subtype].insn_for_extern;
490 fragP->fr_subtype = insn_to_subtype (new_insn);
493 if (fragP->fr_cgen.insn->base
494 && fragP->fr_cgen.insn->base->num
495 != subtype_mappings[fragP->fr_subtype].insn
496 && subtype_mappings[fragP->fr_subtype].insn > 0)
498 int new_insn= subtype_mappings[fragP->fr_subtype].insn;
499 if (new_insn >= 0)
501 fragP->fr_cgen.insn = (fragP->fr_cgen.insn
502 - fragP->fr_cgen.insn->base->num
503 + new_insn);
507 return subtype_mappings[fragP->fr_subtype].bytes - (fragP->fr_fix - where);
510 /* *fragP has been relaxed to its final size, and now needs to have
511 the bytes inside it modified to conform to the new size.
513 Called after relaxation is finished.
514 fragP->fr_type == rs_machine_dependent.
515 fragP->fr_subtype is the subtype of what the address relaxed to. */
517 static int
518 target_address_for (fragS *frag)
520 int rv = frag->fr_offset;
521 symbolS *sym = frag->fr_symbol;
523 if (sym)
524 rv += S_GET_VALUE (sym);
526 /*printf("target_address_for returns %d\n", rv);*/
527 return rv;
530 void
531 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
532 segT sec ATTRIBUTE_UNUSED,
533 fragS * fragP ATTRIBUTE_UNUSED)
535 int addend;
536 int operand;
537 int new_insn;
538 int where = fragP->fr_opcode - fragP->fr_literal;
539 unsigned char *op = (unsigned char *)fragP->fr_opcode;
541 addend = target_address_for (fragP) - (fragP->fr_address + where);
542 new_insn = subtype_mappings[fragP->fr_subtype].insn;
544 fragP->fr_fix = where + subtype_mappings[fragP->fr_subtype].bytes;
546 switch (subtype_mappings[fragP->fr_subtype].insn)
548 case M32C_INSN_JCND16_5:
549 op[1] = addend - 1;
550 operand = M32C_OPERAND_LAB_8_8;
551 break;
553 case -M32C_MACRO_JCND16_5_W:
554 op[0] ^= 0x04;
555 op[1] = 4;
556 op[2] = 0xf4;
557 op[3] = addend - 3;
558 op[4] = (addend - 3) >> 8;
559 operand = M32C_OPERAND_LAB_8_16;
560 where += 2;
561 new_insn = M32C_INSN_JMP16_W;
562 break;
564 case -M32C_MACRO_JCND16_5_A:
565 op[0] ^= 0x04;
566 op[1] = 5;
567 op[2] = 0xfc;
568 operand = M32C_OPERAND_LAB_8_24;
569 where += 2;
570 new_insn = M32C_INSN_JMP16_A;
571 break;
574 case M32C_INSN_JCND16:
575 op[2] = addend - 2;
576 operand = M32C_OPERAND_LAB_16_8;
577 break;
579 case -M32C_MACRO_JCND16_W:
580 op[1] ^= 0x04;
581 op[2] = 4;
582 op[3] = 0xf4;
583 op[4] = addend - 4;
584 op[5] = (addend - 4) >> 8;
585 operand = M32C_OPERAND_LAB_8_16;
586 where += 3;
587 new_insn = M32C_INSN_JMP16_W;
588 break;
590 case -M32C_MACRO_JCND16_A:
591 op[1] ^= 0x04;
592 op[2] = 5;
593 op[3] = 0xfc;
594 operand = M32C_OPERAND_LAB_8_24;
595 where += 3;
596 new_insn = M32C_INSN_JMP16_A;
597 break;
599 case M32C_INSN_JMP16_S:
600 op[0] = 0x60 | ((addend-2) & 0x07);
601 operand = M32C_OPERAND_LAB_5_3;
602 break;
604 case M32C_INSN_JMP16_B:
605 op[0] = 0xfe;
606 op[1] = addend - 1;
607 operand = M32C_OPERAND_LAB_8_8;
608 break;
610 case M32C_INSN_JMP16_W:
611 op[0] = 0xf4;
612 op[1] = addend - 1;
613 op[2] = (addend - 1) >> 8;
614 operand = M32C_OPERAND_LAB_8_16;
615 break;
617 case M32C_INSN_JMP16_A:
618 op[0] = 0xfc;
619 op[1] = 0;
620 op[2] = 0;
621 op[3] = 0;
622 operand = M32C_OPERAND_LAB_8_24;
623 break;
625 case M32C_INSN_JCND32:
626 op[1] = addend - 1;
627 operand = M32C_OPERAND_LAB_8_8;
628 break;
630 case -M32C_MACRO_JCND32_W:
631 op[0] ^= 0x40;
632 op[1] = 4;
633 op[2] = 0xce;
634 op[3] = addend - 3;
635 op[4] = (addend - 3) >> 8;
636 operand = M32C_OPERAND_LAB_8_16;
637 where += 2;
638 new_insn = M32C_INSN_JMP32_W;
639 break;
641 case -M32C_MACRO_JCND32_A:
642 op[0] ^= 0x40;
643 op[1] = 5;
644 op[2] = 0xcc;
645 operand = M32C_OPERAND_LAB_8_24;
646 where += 2;
647 new_insn = M32C_INSN_JMP32_A;
648 break;
652 case M32C_INSN_JMP32_S:
653 addend = ((addend-2) & 0x07);
654 op[0] = 0x4a | (addend & 0x01) | ((addend << 3) & 0x30);
655 operand = M32C_OPERAND_LAB32_JMP_S;
656 break;
658 case M32C_INSN_JMP32_B:
659 op[0] = 0xbb;
660 op[1] = addend - 1;
661 operand = M32C_OPERAND_LAB_8_8;
662 break;
664 case M32C_INSN_JMP32_W:
665 op[0] = 0xce;
666 op[1] = addend - 1;
667 op[2] = (addend - 1) >> 8;
668 operand = M32C_OPERAND_LAB_8_16;
669 break;
671 case M32C_INSN_JMP32_A:
672 op[0] = 0xcc;
673 op[1] = 0;
674 op[2] = 0;
675 op[3] = 0;
676 operand = M32C_OPERAND_LAB_8_24;
677 break;
680 default:
681 printf("\nHey! Need more opcode converters! missing: %d %s\n\n",
682 fragP->fr_subtype,
683 fragP->fr_cgen.insn->base->name);
684 abort();
687 if (S_GET_SEGMENT (fragP->fr_symbol) != sec
688 || operand == M32C_OPERAND_LAB_8_24)
690 assert (fragP->fr_cgen.insn != 0);
691 gas_cgen_record_fixup (fragP,
692 where,
693 fragP->fr_cgen.insn,
694 (fragP->fr_fix - where) * 8,
695 cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
696 operand),
697 fragP->fr_cgen.opinfo,
698 fragP->fr_symbol, fragP->fr_offset);
702 /* Functions concerning relocs. */
704 /* The location from which a PC relative jump should be calculated,
705 given a PC relative reloc. */
707 long
708 md_pcrel_from_section (fixS * fixP, segT sec)
710 if (fixP->fx_addsy != (symbolS *) NULL
711 && (! S_IS_DEFINED (fixP->fx_addsy)
712 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
713 /* The symbol is undefined (or is defined but not in this section).
714 Let the linker figure it out. */
715 return 0;
717 return (fixP->fx_frag->fr_address + fixP->fx_where);
720 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
721 Returns BFD_RELOC_NONE if no reloc type can be found.
722 *FIXP may be modified if desired. */
724 bfd_reloc_code_real_type
725 md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
726 const CGEN_OPERAND * operand,
727 fixS * fixP ATTRIBUTE_UNUSED)
729 static const struct op_reloc {
730 /* A CGEN operand type that can be a relocatable expression. */
731 CGEN_OPERAND_TYPE operand;
733 /* The appropriate BFD reloc type to use for that. */
734 bfd_reloc_code_real_type reloc;
736 /* The offset from the start of the instruction to the field to be
737 relocated, in bytes. */
738 int offset;
739 } op_reloc_table[] = {
741 /* Absolute relocs for 8-bit fields. */
742 { M32C_OPERAND_IMM_8_QI, BFD_RELOC_8, 1 },
743 { M32C_OPERAND_IMM_16_QI, BFD_RELOC_8, 2 },
744 { M32C_OPERAND_IMM_24_QI, BFD_RELOC_8, 3 },
745 { M32C_OPERAND_IMM_32_QI, BFD_RELOC_8, 4 },
746 { M32C_OPERAND_IMM_40_QI, BFD_RELOC_8, 5 },
747 { M32C_OPERAND_IMM_48_QI, BFD_RELOC_8, 6 },
748 { M32C_OPERAND_IMM_56_QI, BFD_RELOC_8, 7 },
749 { M32C_OPERAND_DSP_8_S8, BFD_RELOC_8, 1 },
750 { M32C_OPERAND_DSP_16_S8, BFD_RELOC_8, 2 },
751 { M32C_OPERAND_DSP_24_S8, BFD_RELOC_8, 3 },
752 { M32C_OPERAND_DSP_32_S8, BFD_RELOC_8, 4 },
753 { M32C_OPERAND_DSP_40_S8, BFD_RELOC_8, 5 },
754 { M32C_OPERAND_DSP_48_S8, BFD_RELOC_8, 6 },
755 { M32C_OPERAND_DSP_8_U8, BFD_RELOC_8, 1 },
756 { M32C_OPERAND_DSP_16_U8, BFD_RELOC_8, 2 },
757 { M32C_OPERAND_DSP_24_U8, BFD_RELOC_8, 3 },
758 { M32C_OPERAND_DSP_32_U8, BFD_RELOC_8, 4 },
759 { M32C_OPERAND_DSP_40_U8, BFD_RELOC_8, 5 },
760 { M32C_OPERAND_DSP_48_U8, BFD_RELOC_8, 6 },
762 /* Absolute relocs for 16-bit fields. */
763 { M32C_OPERAND_IMM_16_HI, BFD_RELOC_16, 2 },
764 { M32C_OPERAND_IMM_24_HI, BFD_RELOC_16, 3 },
765 { M32C_OPERAND_IMM_32_HI, BFD_RELOC_16, 4 },
766 { M32C_OPERAND_DSP_16_S16, BFD_RELOC_16, 2 },
767 { M32C_OPERAND_DSP_24_S16, BFD_RELOC_16, 3 },
768 { M32C_OPERAND_DSP_32_S16, BFD_RELOC_16, 4 },
769 { M32C_OPERAND_DSP_40_S16, BFD_RELOC_16, 5 },
770 { M32C_OPERAND_DSP_8_U16, BFD_RELOC_16, 1 },
771 { M32C_OPERAND_DSP_16_U16, BFD_RELOC_16, 2 },
772 { M32C_OPERAND_DSP_24_U16, BFD_RELOC_16, 3 },
773 { M32C_OPERAND_DSP_32_U16, BFD_RELOC_16, 4 },
775 /* Absolute relocs for 24-bit fields. */
776 { M32C_OPERAND_LAB_8_24, BFD_RELOC_24, 1 },
777 { M32C_OPERAND_DSP_16_U24, BFD_RELOC_24, 2 },
778 { M32C_OPERAND_DSP_24_U24, BFD_RELOC_24, 3 },
779 { M32C_OPERAND_DSP_32_U24, BFD_RELOC_24, 4 },
780 { M32C_OPERAND_DSP_40_U24, BFD_RELOC_24, 5 },
781 { M32C_OPERAND_DSP_8_U24, BFD_RELOC_24, 1 },
783 /* Absolute relocs for 32-bit fields. */
784 { M32C_OPERAND_IMM_16_SI, BFD_RELOC_32, 2 },
785 { M32C_OPERAND_IMM_24_SI, BFD_RELOC_32, 3 },
786 { M32C_OPERAND_IMM_32_SI, BFD_RELOC_32, 4 },
787 { M32C_OPERAND_IMM_40_SI, BFD_RELOC_32, 5 },
791 int i;
793 for (i = ARRAY_SIZE (op_reloc_table); --i >= 0; )
795 const struct op_reloc *or = &op_reloc_table[i];
797 if (or->operand == operand->type)
799 fixP->fx_where += or->offset;
800 fixP->fx_size -= or->offset;
802 if (fixP->fx_cgen.opinfo
803 && fixP->fx_cgen.opinfo != BFD_RELOC_NONE)
804 return fixP->fx_cgen.opinfo;
806 return or->reloc;
810 fprintf
811 (stderr,
812 "Error: tc-m32c.c:md_cgen_lookup_reloc Unimplemented relocation for operand %d\n",
813 operand->type);
815 return BFD_RELOC_NONE;
818 /* See whether we need to force a relocation into the output file.
819 This is used to force out switch and PC relative relocations when
820 relaxing. */
823 m32c_force_relocation (fixS * fixp)
825 int reloc = fixp->fx_r_type;
827 if (reloc > (int)BFD_RELOC_UNUSED)
829 reloc -= (int)BFD_RELOC_UNUSED;
830 switch (reloc)
832 case M32C_OPERAND_DSP_32_S16:
833 case M32C_OPERAND_DSP_32_U16:
834 case M32C_OPERAND_IMM_32_HI:
835 case M32C_OPERAND_DSP_16_S16:
836 case M32C_OPERAND_DSP_16_U16:
837 case M32C_OPERAND_IMM_16_HI:
838 case M32C_OPERAND_DSP_24_S16:
839 case M32C_OPERAND_DSP_24_U16:
840 case M32C_OPERAND_IMM_24_HI:
841 return 1;
844 else
846 if (fixp->fx_r_type == BFD_RELOC_16)
847 return 1;
850 return generic_force_reloc (fixp);
853 /* Write a value out to the object file, using the appropriate endianness. */
855 void
856 md_number_to_chars (char * buf, valueT val, int n)
858 number_to_chars_littleendian (buf, val, n);
861 /* Turn a string in input_line_pointer into a floating point constant of type
862 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
863 emitted is stored in *sizeP . An error message is returned, or NULL on OK. */
865 /* Equal to MAX_PRECISION in atof-ieee.c. */
866 #define MAX_LITTLENUMS 6
868 char *
869 md_atof (int type, char * litP, int * sizeP)
871 int i;
872 int prec;
873 LITTLENUM_TYPE words [MAX_LITTLENUMS];
874 char * t;
876 switch (type)
878 case 'f':
879 case 'F':
880 case 's':
881 case 'S':
882 prec = 2;
883 break;
885 case 'd':
886 case 'D':
887 case 'r':
888 case 'R':
889 prec = 4;
890 break;
892 /* FIXME: Some targets allow other format chars for bigger sizes here. */
894 default:
895 * sizeP = 0;
896 return _("Bad call to md_atof()");
899 t = atof_ieee (input_line_pointer, type, words);
900 if (t)
901 input_line_pointer = t;
902 * sizeP = prec * sizeof (LITTLENUM_TYPE);
904 for (i = 0; i < prec; i++)
906 md_number_to_chars (litP, (valueT) words[i],
907 sizeof (LITTLENUM_TYPE));
908 litP += sizeof (LITTLENUM_TYPE);
911 return 0;
914 bfd_boolean
915 m32c_fix_adjustable (fixS * fixP)
917 int reloc;
918 if (fixP->fx_addsy == NULL)
919 return 1;
921 /* We need the symbol name for the VTABLE entries. */
922 reloc = fixP->fx_r_type;
923 if (reloc > (int)BFD_RELOC_UNUSED)
925 reloc -= (int)BFD_RELOC_UNUSED;
926 switch (reloc)
928 case M32C_OPERAND_DSP_32_S16:
929 case M32C_OPERAND_DSP_32_U16:
930 case M32C_OPERAND_IMM_32_HI:
931 case M32C_OPERAND_DSP_16_S16:
932 case M32C_OPERAND_DSP_16_U16:
933 case M32C_OPERAND_IMM_16_HI:
934 case M32C_OPERAND_DSP_24_S16:
935 case M32C_OPERAND_DSP_24_U16:
936 case M32C_OPERAND_IMM_24_HI:
937 return 0;
940 else
942 if (fixP->fx_r_type == BFD_RELOC_16)
943 return 0;
946 /* Do not adjust relocations involving symbols in merged sections.
948 A reloc patching in the value of some symbol S plus some addend A
949 can be produced in different ways:
951 1) It might simply be a reference to the data at S + A. Clearly,
952 if linker merging shift that data around, the value patched in
953 by the reloc needs to be adjusted accordingly.
955 2) Or, it might be a reference to S, with A added in as a constant
956 bias. For example, given code like this:
958 static int S[100];
960 ... S[i - 8] ...
962 it would be reasonable for the compiler to rearrange the array
963 reference to something like:
965 ... (S-8)[i] ...
967 and emit assembly code that refers to S - (8 * sizeof (int)),
968 so the subtraction is done entirely at compile-time. In this
969 case, the reloc's addend A would be -(8 * sizeof (int)), and
970 shifting around code or data at S + A should not affect the
971 reloc: the reloc isn't referring to that code or data at all.
973 The linker has no way of knowing which case it has in hand. So,
974 to disambiguate, we have the linker always treat reloc addends as
975 in case 2): they're constants that should be simply added to the
976 symbol value, just like the reloc says. And we express case 1)
977 in different way: we have the compiler place a label at the real
978 target, and reference that label with an addend of zero. (The
979 compiler is unlikely to reference code using a label plus an
980 offset anyway, since it doesn't know the sizes of the
981 instructions.)
983 The simplification being done by gas/write.c:adjust_reloc_syms,
984 however, turns the explicit-label usage into the label-plus-
985 offset usage, re-introducing the ambiguity the compiler avoided.
986 So we need to disable that simplification for symbols referring
987 to merged data.
989 This only affects object size a little bit. */
990 if (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE)
991 return 0;
993 return 1;
996 /* Worker function for m32c_is_colon_insn(). */
997 static char restore_colon PARAMS ((int));
999 static char
1000 restore_colon (int advance_i_l_p_by)
1002 char c;
1004 /* Restore the colon, and advance input_line_pointer to
1005 the end of the new symbol. */
1006 * input_line_pointer = ':';
1007 input_line_pointer += advance_i_l_p_by;
1008 c = * input_line_pointer;
1009 * input_line_pointer = 0;
1011 return c;
1014 /* Determines if the symbol starting at START and ending in
1015 a colon that was at the location pointed to by INPUT_LINE_POINTER
1016 (but which has now been replaced bu a NUL) is in fact an
1017 :Z, :S, :Q, or :G suffix.
1018 If it is, then it restores the colon, advances INPUT_LINE_POINTER
1019 to the real end of the instruction/symbol, and returns the character
1020 that really terminated the symbol. Otherwise it returns 0. */
1021 char
1022 m32c_is_colon_insn (char *start ATTRIBUTE_UNUSED)
1024 char * i_l_p = input_line_pointer;
1026 /* Check to see if the text following the colon is 'G' */
1027 if (TOLOWER (i_l_p[1]) == 'g' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1028 return restore_colon (2);
1030 /* Check to see if the text following the colon is 'Q' */
1031 if (TOLOWER (i_l_p[1]) == 'q' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1032 return restore_colon (2);
1034 /* Check to see if the text following the colon is 'S' */
1035 if (TOLOWER (i_l_p[1]) == 's' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1036 return restore_colon (2);
1038 /* Check to see if the text following the colon is 'Z' */
1039 if (TOLOWER (i_l_p[1]) == 'z' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
1040 return restore_colon (2);
1042 return 0;