merge from gcc
[binutils.git] / gas / config / tc-m32r.c
blobe990d9d941c691bb8a52af5a715b0b71c9eac8c0
1 /* tc-m32r.c -- Assembler for the Renesas M32R.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
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 "safe-ctype.h"
25 #include "subsegs.h"
26 #include "symcat.h"
27 #include "opcodes/m32r-desc.h"
28 #include "opcodes/m32r-opc.h"
29 #include "cgen.h"
30 #include "elf/m32r.h"
32 /* Linked list of symbols that are debugging symbols to be defined as the
33 beginning of the current instruction. */
34 typedef struct sym_link
36 struct sym_link *next;
37 symbolS *symbol;
38 } sym_linkS;
40 static sym_linkS *debug_sym_link = (sym_linkS *) 0;
42 /* Structure to hold all of the different components describing
43 an individual instruction. */
44 typedef struct
46 const CGEN_INSN *insn;
47 const CGEN_INSN *orig_insn;
48 CGEN_FIELDS fields;
49 #if CGEN_INT_INSN_P
50 CGEN_INSN_INT buffer[1];
51 #define INSN_VALUE(buf) (*(buf))
52 #else
53 unsigned char buffer[CGEN_MAX_INSN_SIZE];
54 #define INSN_VALUE(buf) (buf)
55 #endif
56 char *addr;
57 fragS *frag;
58 int num_fixups;
59 fixS *fixups[GAS_CGEN_MAX_FIXUPS];
60 int indices[MAX_OPERAND_INSTANCES];
61 sym_linkS *debug_sym_link;
63 m32r_insn;
65 /* prev_insn.insn is non-null if last insn was a 16 bit insn on a 32 bit
66 boundary (i.e. was the first of two 16 bit insns). */
67 static m32r_insn prev_insn;
69 /* Non-zero if we've seen a relaxable insn since the last 32 bit
70 alignment request. */
71 static int seen_relaxable_p = 0;
73 /* Non-zero if we are generating PIC code. */
74 int pic_code;
76 /* Non-zero if -relax specified, in which case sufficient relocs are output
77 for the linker to do relaxing.
78 We do simple forms of relaxing internally, but they are always done.
79 This flag does not apply to them. */
80 static int m32r_relax;
82 #if 0
83 /* Not supported yet. */
84 /* If non-NULL, pointer to cpu description file to read.
85 This allows runtime additions to the assembler. */
86 static const char *m32r_cpu_desc;
87 #endif
89 /* Non-zero if warn when a high/shigh reloc has no matching low reloc.
90 Each high/shigh reloc must be paired with it's low cousin in order to
91 properly calculate the addend in a relocatable link (since there is a
92 potential carry from the low to the high/shigh).
93 This option is off by default though for user-written assembler code it
94 might make sense to make the default be on (i.e. have gcc pass a flag
95 to turn it off). This warning must not be on for GCC created code as
96 optimization may delete the low but not the high/shigh (at least we
97 shouldn't assume or require it to). */
98 static int warn_unmatched_high = 0;
100 /* 1 if -m32rx has been specified, in which case support for
101 the extended M32RX instruction set should be enabled.
102 2 if -m32r2 has been specified, in which case support for
103 the extended M32R2 instruction set should be enabled. */
104 static int enable_m32rx = 0; /* Default to M32R. */
106 /* Non-zero if -m32rx -hidden has been specified, in which case support for
107 the special M32RX instruction set should be enabled. */
108 static int enable_special = 0;
110 /* Non-zero if -bitinst has been specified, in which case support
111 for extended M32R bit-field instruction set should be enabled. */
112 static int enable_special_m32r = 1;
114 /* Non-zero if -float has been specified, in which case support for
115 extended M32R floating point instruction set should be enabled. */
116 static int enable_special_float = 0;
118 /* Non-zero if the programmer should be warned when an explicit parallel
119 instruction might have constraint violations. */
120 static int warn_explicit_parallel_conflicts = 1;
122 /* Non-zero if the programmer should not receive any messages about
123 parallel instruction with potential or real constraint violations.
124 The ability to suppress these messages is intended only for hardware
125 vendors testing the chip. It superceedes
126 warn_explicit_parallel_conflicts. */
127 static int ignore_parallel_conflicts = 0;
129 /* Non-zero if insns can be made parallel. */
130 static int use_parallel = 1;
132 /* Non-zero if optimizations should be performed. */
133 static int optimize;
135 /* m32r er_flags. */
136 static int m32r_flags = 0;
138 /* Stuff for .scomm symbols. */
139 static segT sbss_section;
140 static asection scom_section;
141 static asymbol scom_symbol;
143 const char comment_chars[] = ";";
144 const char line_comment_chars[] = "#";
145 const char line_separator_chars[] = "!";
146 const char EXP_CHARS[] = "eE";
147 const char FLT_CHARS[] = "dD";
149 /* Relocations against symbols are done in two
150 parts, with a HI relocation and a LO relocation. Each relocation
151 has only 16 bits of space to store an addend. This means that in
152 order for the linker to handle carries correctly, it must be able
153 to locate both the HI and the LO relocation. This means that the
154 relocations must appear in order in the relocation table.
156 In order to implement this, we keep track of each unmatched HI
157 relocation. We then sort them so that they immediately precede the
158 corresponding LO relocation. */
160 struct m32r_hi_fixup
162 /* Next HI fixup. */
163 struct m32r_hi_fixup *next;
165 /* This fixup. */
166 fixS *fixp;
168 /* The section this fixup is in. */
169 segT seg;
172 /* The list of unmatched HI relocs. */
174 static struct m32r_hi_fixup *m32r_hi_fixup_list;
176 struct {
177 enum bfd_architecture bfd_mach;
178 int mach_flags;
179 } mach_table[] =
181 { bfd_mach_m32r, (1<<MACH_M32R) },
182 { bfd_mach_m32rx, (1<<MACH_M32RX) },
183 { bfd_mach_m32r2, (1<<MACH_M32R2) }
186 static void allow_m32rx (int);
188 static void
189 allow_m32rx (int on)
191 enable_m32rx = on;
193 if (stdoutput != NULL)
194 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach_table[on].bfd_mach);
196 if (gas_cgen_cpu_desc != NULL)
197 gas_cgen_cpu_desc->machs = mach_table[on].mach_flags;
200 #define M32R_SHORTOPTS "O::K:"
202 const char *md_shortopts = M32R_SHORTOPTS;
204 struct option md_longopts[] =
206 #define OPTION_M32R (OPTION_MD_BASE)
207 #define OPTION_M32RX (OPTION_M32R + 1)
208 #define OPTION_M32R2 (OPTION_M32RX + 1)
209 #define OPTION_BIG (OPTION_M32R2 + 1)
210 #define OPTION_LITTLE (OPTION_BIG + 1)
211 #define OPTION_PARALLEL (OPTION_LITTLE + 1)
212 #define OPTION_NO_PARALLEL (OPTION_PARALLEL + 1)
213 #define OPTION_WARN_PARALLEL (OPTION_NO_PARALLEL + 1)
214 #define OPTION_NO_WARN_PARALLEL (OPTION_WARN_PARALLEL + 1)
215 #define OPTION_IGNORE_PARALLEL (OPTION_NO_WARN_PARALLEL + 1)
216 #define OPTION_NO_IGNORE_PARALLEL (OPTION_IGNORE_PARALLEL + 1)
217 #define OPTION_SPECIAL (OPTION_NO_IGNORE_PARALLEL + 1)
218 #define OPTION_SPECIAL_M32R (OPTION_SPECIAL + 1)
219 #define OPTION_NO_SPECIAL_M32R (OPTION_SPECIAL_M32R + 1)
220 #define OPTION_SPECIAL_FLOAT (OPTION_NO_SPECIAL_M32R + 1)
221 #define OPTION_WARN_UNMATCHED (OPTION_SPECIAL_FLOAT + 1)
222 #define OPTION_NO_WARN_UNMATCHED (OPTION_WARN_UNMATCHED + 1)
223 {"m32r", no_argument, NULL, OPTION_M32R},
224 {"m32rx", no_argument, NULL, OPTION_M32RX},
225 {"m32r2", no_argument, NULL, OPTION_M32R2},
226 {"big", no_argument, NULL, OPTION_BIG},
227 {"little", no_argument, NULL, OPTION_LITTLE},
228 {"EB", no_argument, NULL, OPTION_BIG},
229 {"EL", no_argument, NULL, OPTION_LITTLE},
230 {"parallel", no_argument, NULL, OPTION_PARALLEL},
231 {"no-parallel", no_argument, NULL, OPTION_NO_PARALLEL},
232 {"warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_WARN_PARALLEL},
233 {"Wp", no_argument, NULL, OPTION_WARN_PARALLEL},
234 {"no-warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_NO_WARN_PARALLEL},
235 {"Wnp", no_argument, NULL, OPTION_NO_WARN_PARALLEL},
236 {"ignore-parallel-conflicts", no_argument, NULL, OPTION_IGNORE_PARALLEL},
237 {"Ip", no_argument, NULL, OPTION_IGNORE_PARALLEL},
238 {"no-ignore-parallel-conflicts", no_argument, NULL, OPTION_NO_IGNORE_PARALLEL},
239 {"nIp", no_argument, NULL, OPTION_NO_IGNORE_PARALLEL},
240 {"hidden", no_argument, NULL, OPTION_SPECIAL},
241 {"bitinst", no_argument, NULL, OPTION_SPECIAL_M32R},
242 {"no-bitinst", no_argument, NULL, OPTION_NO_SPECIAL_M32R},
243 {"float", no_argument, NULL, OPTION_SPECIAL_FLOAT},
244 /* Sigh. I guess all warnings must now have both variants. */
245 {"warn-unmatched-high", no_argument, NULL, OPTION_WARN_UNMATCHED},
246 {"Wuh", no_argument, NULL, OPTION_WARN_UNMATCHED},
247 {"no-warn-unmatched-high", no_argument, NULL, OPTION_NO_WARN_UNMATCHED},
248 {"Wnuh", no_argument, NULL, OPTION_NO_WARN_UNMATCHED},
250 #if 0
251 /* Not supported yet. */
252 #define OPTION_RELAX (OPTION_NO_WARN_UNMATCHED + 1)
253 #define OPTION_CPU_DESC (OPTION_RELAX + 1)
254 {"relax", no_argument, NULL, OPTION_RELAX},
255 {"cpu-desc", required_argument, NULL, OPTION_CPU_DESC},
256 #endif
257 {NULL, no_argument, NULL, 0}
260 size_t md_longopts_size = sizeof (md_longopts);
262 static void little (int);
263 static int parallel (void);
265 static void
266 little (int on)
268 target_big_endian = ! on;
271 /* Use parallel execution. */
273 static int
274 parallel (void)
276 if (! enable_m32rx)
277 return 0;
279 if (use_parallel == 1)
280 return 1;
282 return 0;
286 md_parse_option (c, arg)
287 int c;
288 char *arg ATTRIBUTE_UNUSED;
290 switch (c)
292 case 'O':
293 optimize = 1;
294 use_parallel = 1;
295 break;
297 case OPTION_M32R:
298 allow_m32rx (0);
299 break;
301 case OPTION_M32RX:
302 allow_m32rx (1);
303 break;
305 case OPTION_M32R2:
306 allow_m32rx (2);
307 enable_special = 1;
308 enable_special_m32r = 1;
309 break;
311 case OPTION_BIG:
312 target_big_endian = 1;
313 break;
315 case OPTION_LITTLE:
316 target_big_endian = 0;
317 break;
319 case OPTION_PARALLEL:
320 use_parallel = 1;
321 break;
323 case OPTION_NO_PARALLEL:
324 use_parallel = 0;
325 break;
327 case OPTION_WARN_PARALLEL:
328 warn_explicit_parallel_conflicts = 1;
329 break;
331 case OPTION_NO_WARN_PARALLEL:
332 warn_explicit_parallel_conflicts = 0;
333 break;
335 case OPTION_IGNORE_PARALLEL:
336 ignore_parallel_conflicts = 1;
337 break;
339 case OPTION_NO_IGNORE_PARALLEL:
340 ignore_parallel_conflicts = 0;
341 break;
343 case OPTION_SPECIAL:
344 if (enable_m32rx)
345 enable_special = 1;
346 else
348 /* Pretend that we do not recognise this option. */
349 as_bad (_("Unrecognised option: -hidden"));
350 return 0;
352 break;
354 case OPTION_SPECIAL_M32R:
355 enable_special_m32r = 1;
356 break;
358 case OPTION_NO_SPECIAL_M32R:
359 enable_special_m32r = 0;
360 break;
362 case OPTION_SPECIAL_FLOAT:
363 enable_special_float = 1;
364 break;
366 case OPTION_WARN_UNMATCHED:
367 warn_unmatched_high = 1;
368 break;
370 case OPTION_NO_WARN_UNMATCHED:
371 warn_unmatched_high = 0;
372 break;
374 case 'K':
375 if (strcmp (arg, "PIC") != 0)
376 as_warn (_("Unrecognized option following -K"));
377 else
378 pic_code = 1;
379 break;
381 #if 0
382 /* Not supported yet. */
383 case OPTION_RELAX:
384 m32r_relax = 1;
385 break;
386 case OPTION_CPU_DESC:
387 m32r_cpu_desc = arg;
388 break;
389 #endif
391 default:
392 return 0;
395 return 1;
398 void
399 md_show_usage (stream)
400 FILE *stream;
402 fprintf (stream, _(" M32R specific command line options:\n"));
404 fprintf (stream, _("\
405 -m32r disable support for the m32rx instruction set\n"));
406 fprintf (stream, _("\
407 -m32rx support the extended m32rx instruction set\n"));
408 fprintf (stream, _("\
409 -m32r2 support the extended m32r2 instruction set\n"));
410 fprintf (stream, _("\
411 -EL,-little produce little endian code and data\n"));
412 fprintf (stream, _("\
413 -EB,-big produce big endian code and data\n"));
414 fprintf (stream, _("\
415 -parallel try to combine instructions in parallel\n"));
416 fprintf (stream, _("\
417 -no-parallel disable -parallel\n"));
418 fprintf (stream, _("\
419 -no-bitinst disallow the M32R2's extended bit-field instructions\n"));
420 fprintf (stream, _("\
421 -O try to optimize code. Implies -parallel\n"));
423 fprintf (stream, _("\
424 -warn-explicit-parallel-conflicts warn when parallel instructions\n"));
425 fprintf (stream, _("\
426 might violate contraints\n"));
427 fprintf (stream, _("\
428 -no-warn-explicit-parallel-conflicts do not warn when parallel\n"));
429 fprintf (stream, _("\
430 instructions might violate contraints\n"));
431 fprintf (stream, _("\
432 -Wp synonym for -warn-explicit-parallel-conflicts\n"));
433 fprintf (stream, _("\
434 -Wnp synonym for -no-warn-explicit-parallel-conflicts\n"));
435 fprintf (stream, _("\
436 -ignore-parallel-conflicts do not check parallel instructions\n"));
437 fprintf (stream, _("\
438 fo contraint violations\n"));
439 fprintf (stream, _("\
440 -no-ignore-parallel-conflicts check parallel instructions for\n"));
441 fprintf (stream, _("\
442 contraint violations\n"));
443 fprintf (stream, _("\
444 -Ip synonym for -ignore-parallel-conflicts\n"));
445 fprintf (stream, _("\
446 -nIp synonym for -no-ignore-parallel-conflicts\n"));
448 fprintf (stream, _("\
449 -warn-unmatched-high warn when an (s)high reloc has no matching low reloc\n"));
450 fprintf (stream, _("\
451 -no-warn-unmatched-high do not warn about missing low relocs\n"));
452 fprintf (stream, _("\
453 -Wuh synonym for -warn-unmatched-high\n"));
454 fprintf (stream, _("\
455 -Wnuh synonym for -no-warn-unmatched-high\n"));
457 fprintf (stream, _("\
458 -KPIC generate PIC\n"));
460 #if 0
461 fprintf (stream, _("\
462 -relax create linker relaxable code\n"));
463 fprintf (stream, _("\
464 -cpu-desc provide runtime cpu description file\n"));
465 #endif
468 static void fill_insn PARAMS ((int));
469 static void m32r_scomm PARAMS ((int));
470 static void debug_sym PARAMS ((int));
471 static void expand_debug_syms PARAMS ((sym_linkS *, int));
473 /* Set by md_assemble for use by m32r_fill_insn. */
474 static subsegT prev_subseg;
475 static segT prev_seg;
477 /* The target specific pseudo-ops which we support. */
478 const pseudo_typeS md_pseudo_table[] =
480 { "word", cons, 4 },
481 { "fillinsn", fill_insn, 0 },
482 { "scomm", m32r_scomm, 0 },
483 { "debugsym", debug_sym, 0 },
484 { "m32r", allow_m32rx, 0 },
485 { "m32rx", allow_m32rx, 1 },
486 { "m32r2", allow_m32rx, 2 },
487 { "little", little, 1 },
488 { "big", little, 0 },
489 { NULL, NULL, 0 }
492 /* FIXME: Should be machine generated. */
493 #define NOP_INSN 0x7000
494 #define PAR_NOP_INSN 0xf000 /* Can only be used in 2nd slot. */
496 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
497 of an rs_align_code fragment. */
499 void
500 m32r_handle_align (fragp)
501 fragS *fragp;
503 static const unsigned char nop_pattern[] = { 0xf0, 0x00 };
504 static const unsigned char multi_nop_pattern[] = { 0x70, 0x00, 0xf0, 0x00 };
506 int bytes, fix;
507 char *p;
509 if (fragp->fr_type != rs_align_code)
510 return;
512 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
513 p = fragp->fr_literal + fragp->fr_fix;
514 fix = 0;
516 if (bytes & 1)
518 fix = 1;
519 *p++ = 0;
520 bytes--;
523 if (bytes & 2)
525 memcpy (p, nop_pattern, 2);
526 p += 2;
527 bytes -= 2;
528 fix += 2;
531 memcpy (p, multi_nop_pattern, 4);
533 fragp->fr_fix += fix;
534 fragp->fr_var = 4;
537 /* If the last instruction was the first of 2 16 bit insns,
538 output a nop to move the PC to a 32 bit boundary.
540 This is done via an alignment specification since branch relaxing
541 may make it unnecessary.
543 Internally, we need to output one of these each time a 32 bit insn is
544 seen after an insn that is relaxable. */
546 static void
547 fill_insn (ignore)
548 int ignore ATTRIBUTE_UNUSED;
550 frag_align_code (2, 0);
551 prev_insn.insn = NULL;
552 seen_relaxable_p = 0;
555 /* Record the symbol so that when we output the insn, we can create
556 a symbol that is at the start of the instruction. This is used
557 to emit the label for the start of a breakpoint without causing
558 the assembler to emit a NOP if the previous instruction was a
559 16 bit instruction. */
561 static void
562 debug_sym (ignore)
563 int ignore ATTRIBUTE_UNUSED;
565 register char *name;
566 register char delim;
567 register char *end_name;
568 register symbolS *symbolP;
569 register sym_linkS *link;
571 name = input_line_pointer;
572 delim = get_symbol_end ();
573 end_name = input_line_pointer;
575 if ((symbolP = symbol_find (name)) == NULL
576 && (symbolP = md_undefined_symbol (name)) == NULL)
578 symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
581 symbol_table_insert (symbolP);
582 if (S_IS_DEFINED (symbolP) && (S_GET_SEGMENT (symbolP) != reg_section
583 || S_IS_EXTERNAL (symbolP)
584 || S_IS_WEAK (symbolP)))
585 /* xgettext:c-format */
586 as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
588 else
590 link = (sym_linkS *) xmalloc (sizeof (sym_linkS));
591 link->symbol = symbolP;
592 link->next = debug_sym_link;
593 debug_sym_link = link;
594 symbol_get_obj (symbolP)->local = 1;
597 *end_name = delim;
598 demand_empty_rest_of_line ();
601 /* Second pass to expanding the debug symbols, go through linked
602 list of symbols and reassign the address. */
604 static void
605 expand_debug_syms (syms, align)
606 sym_linkS *syms;
607 int align;
609 char *save_input_line = input_line_pointer;
610 sym_linkS *next_syms;
612 if (!syms)
613 return;
615 (void) frag_align_code (align, 0);
616 for (; syms != (sym_linkS *) 0; syms = next_syms)
618 symbolS *symbolP = syms->symbol;
619 next_syms = syms->next;
620 input_line_pointer = ".\n";
621 pseudo_set (symbolP);
622 free ((char *) syms);
625 input_line_pointer = save_input_line;
628 void
629 m32r_flush_pending_output()
631 if (debug_sym_link)
633 expand_debug_syms (debug_sym_link, 1);
634 debug_sym_link = (sym_linkS *) 0;
638 /* Cover function to fill_insn called after a label and at end of assembly.
639 The result is always 1: we're called in a conditional to see if the
640 current line is a label. */
643 m32r_fill_insn (done)
644 int done;
646 if (prev_seg != NULL)
648 segT seg = now_seg;
649 subsegT subseg = now_subseg;
651 subseg_set (prev_seg, prev_subseg);
653 fill_insn (0);
655 subseg_set (seg, subseg);
658 if (done && debug_sym_link)
660 expand_debug_syms (debug_sym_link, 1);
661 debug_sym_link = (sym_linkS *) 0;
664 return 1;
667 /* The default target format to use. */
669 const char *
670 m32r_target_format ()
672 #ifdef TE_LINUX
673 if (target_big_endian)
674 return "elf32-m32r-linux";
675 else
676 return "elf32-m32rle-linux";
677 #else
678 if (target_big_endian)
679 return "elf32-m32r";
680 else
681 return "elf32-m32rle";
682 #endif
685 void
686 md_begin ()
688 flagword applicable;
689 segT seg;
690 subsegT subseg;
692 /* Initialize the `cgen' interface. */
694 /* Set the machine number and endian. */
695 gas_cgen_cpu_desc = m32r_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
696 CGEN_CPU_OPEN_ENDIAN,
697 (target_big_endian ?
698 CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE),
699 CGEN_CPU_OPEN_END);
700 m32r_cgen_init_asm (gas_cgen_cpu_desc);
702 /* The operand instance table is used during optimization to determine
703 which insns can be executed in parallel. It is also used to give
704 warnings regarding operand interference in parallel insns. */
705 m32r_cgen_init_opinst_table (gas_cgen_cpu_desc);
707 /* This is a callback from cgen to gas to parse operands. */
708 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
710 #if 0
711 /* Not supported yet. */
712 /* If a runtime cpu description file was provided, parse it. */
713 if (m32r_cpu_desc != NULL)
715 const char *errmsg;
717 errmsg = cgen_read_cpu_file (gas_cgen_cpu_desc, m32r_cpu_desc);
718 if (errmsg != NULL)
719 as_bad ("%s: %s", m32r_cpu_desc, errmsg);
721 #endif
723 /* Save the current subseg so we can restore it [it's the default one and
724 we don't want the initial section to be .sbss]. */
725 seg = now_seg;
726 subseg = now_subseg;
728 /* The sbss section is for local .scomm symbols. */
729 sbss_section = subseg_new (".sbss", 0);
731 /* This is copied from perform_an_assembly_pass. */
732 applicable = bfd_applicable_section_flags (stdoutput);
733 bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
735 #if 0
736 /* What does this do? [see perform_an_assembly_pass] */
737 seg_info (bss_section)->bss = 1;
738 #endif
740 subseg_set (seg, subseg);
742 /* We must construct a fake section similar to bfd_com_section
743 but with the name .scommon. */
744 scom_section = bfd_com_section;
745 scom_section.name = ".scommon";
746 scom_section.output_section = &scom_section;
747 scom_section.symbol = &scom_symbol;
748 scom_section.symbol_ptr_ptr = &scom_section.symbol;
749 scom_symbol = *bfd_com_section.symbol;
750 scom_symbol.name = ".scommon";
751 scom_symbol.section = &scom_section;
753 allow_m32rx (enable_m32rx);
755 gas_cgen_initialize_saved_fixups_array ();
758 #define OPERAND_IS_COND_BIT(operand, indices, index) \
759 ((operand)->hw_type == HW_H_COND \
760 || ((operand)->hw_type == HW_H_PSW) \
761 || ((operand)->hw_type == HW_H_CR \
762 && (indices [index] == 0 || indices [index] == 1)))
764 /* Returns true if an output of instruction 'a' is referenced by an operand
765 of instruction 'b'. If 'check_outputs' is true then b's outputs are
766 checked, otherwise its inputs are examined. */
768 static int first_writes_to_seconds_operands
769 PARAMS ((m32r_insn *, m32r_insn *, const int));
771 static int
772 first_writes_to_seconds_operands (a, b, check_outputs)
773 m32r_insn *a;
774 m32r_insn *b;
775 const int check_outputs;
777 const CGEN_OPINST *a_operands = CGEN_INSN_OPERANDS (a->insn);
778 const CGEN_OPINST *b_ops = CGEN_INSN_OPERANDS (b->insn);
779 int a_index;
781 if (ignore_parallel_conflicts)
782 return 0;
784 /* If at least one of the instructions takes no operands, then there is
785 nothing to check. There really are instructions without operands,
786 eg 'nop'. */
787 if (a_operands == NULL || b_ops == NULL)
788 return 0;
790 /* Scan the operand list of 'a' looking for an output operand. */
791 for (a_index = 0;
792 a_operands->type != CGEN_OPINST_END;
793 a_index ++, a_operands ++)
795 if (a_operands->type == CGEN_OPINST_OUTPUT)
797 int b_index;
798 const CGEN_OPINST *b_operands = b_ops;
800 /* Special Case:
801 The Condition bit 'C' is a shadow of the CBR register (control
802 register 1) and also a shadow of bit 31 of the program status
803 word (control register 0). For now this is handled here, rather
804 than by cgen.... */
806 if (OPERAND_IS_COND_BIT (a_operands, a->indices, a_index))
808 /* Scan operand list of 'b' looking for another reference to the
809 condition bit, which goes in the right direction. */
810 for (b_index = 0;
811 b_operands->type != CGEN_OPINST_END;
812 b_index++, b_operands++)
814 if ((b_operands->type
815 == (check_outputs
816 ? CGEN_OPINST_OUTPUT
817 : CGEN_OPINST_INPUT))
818 && OPERAND_IS_COND_BIT (b_operands, b->indices, b_index))
819 return 1;
822 else
824 /* Scan operand list of 'b' looking for an operand that
825 references the same hardware element, and which goes in the
826 right direction. */
827 for (b_index = 0;
828 b_operands->type != CGEN_OPINST_END;
829 b_index++, b_operands++)
831 if ((b_operands->type
832 == (check_outputs
833 ? CGEN_OPINST_OUTPUT
834 : CGEN_OPINST_INPUT))
835 && (b_operands->hw_type == a_operands->hw_type)
836 && (a->indices[a_index] == b->indices[b_index]))
837 return 1;
843 return 0;
846 /* Returns true if the insn can (potentially) alter the program counter. */
848 static int writes_to_pc PARAMS ((m32r_insn *));
850 static int
851 writes_to_pc (a)
852 m32r_insn *a;
854 #if 0
855 /* Once PC operands are working.... */
856 const CGEN_OPINST *a_operands == CGEN_INSN_OPERANDS (gas_cgen_cpu_desc,
857 a->insn);
859 if (a_operands == NULL)
860 return 0;
862 while (a_operands->type != CGEN_OPINST_END)
864 if (a_operands->operand != NULL
865 && CGEN_OPERAND_INDEX (gas_cgen_cpu_desc,
866 a_operands->operand) == M32R_OPERAND_PC)
867 return 1;
869 a_operands++;
871 #else
872 if (CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_UNCOND_CTI)
873 || CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_COND_CTI))
874 return 1;
875 #endif
876 return 0;
879 /* Return NULL if the two 16 bit insns can be executed in parallel.
880 Otherwise return a pointer to an error message explaining why not. */
882 static const char *can_make_parallel PARAMS ((m32r_insn *, m32r_insn *));
884 static const char *
885 can_make_parallel (a, b)
886 m32r_insn *a;
887 m32r_insn *b;
889 PIPE_ATTR a_pipe;
890 PIPE_ATTR b_pipe;
892 /* Make sure the instructions are the right length. */
893 if (CGEN_FIELDS_BITSIZE (&a->fields) != 16
894 || CGEN_FIELDS_BITSIZE (&b->fields) != 16)
895 abort ();
897 if (first_writes_to_seconds_operands (a, b, TRUE))
898 return _("instructions write to the same destination register.");
900 a_pipe = CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_PIPE);
901 b_pipe = CGEN_INSN_ATTR_VALUE (b->insn, CGEN_INSN_PIPE);
903 /* Make sure that the instructions use the correct execution pipelines. */
904 if (a_pipe == PIPE_NONE
905 || b_pipe == PIPE_NONE)
906 return _("Instructions do not use parallel execution pipelines.");
908 /* Leave this test for last, since it is the only test that can
909 go away if the instructions are swapped, and we want to make
910 sure that any other errors are detected before this happens. */
911 if (a_pipe == PIPE_S
912 || b_pipe == PIPE_O
913 || (b_pipe == PIPE_O_OS && (enable_m32rx != 2)))
914 return _("Instructions share the same execution pipeline");
916 return NULL;
919 /* Force the top bit of the second 16-bit insn to be set. */
921 static void make_parallel PARAMS ((CGEN_INSN_BYTES_PTR));
923 static void
924 make_parallel (buffer)
925 CGEN_INSN_BYTES_PTR buffer;
927 #if CGEN_INT_INSN_P
928 *buffer |= 0x8000;
929 #else
930 buffer[CGEN_CPU_ENDIAN (gas_cgen_cpu_desc) == CGEN_ENDIAN_BIG ? 0 : 1]
931 |= 0x80;
932 #endif
935 /* Same as make_parallel except buffer contains the bytes in target order. */
937 static void target_make_parallel PARAMS ((char *));
939 static void
940 target_make_parallel (buffer)
941 char *buffer;
943 buffer[CGEN_CPU_ENDIAN (gas_cgen_cpu_desc) == CGEN_ENDIAN_BIG ? 0 : 1]
944 |= 0x80;
947 /* Assemble two instructions with an explicit parallel operation (||) or
948 sequential operation (->). */
950 static void assemble_two_insns PARAMS ((char *, char *, int));
952 static void
953 assemble_two_insns (str, str2, parallel_p)
954 char *str;
955 char *str2;
956 int parallel_p;
958 char *str3;
959 m32r_insn first;
960 m32r_insn second;
961 char *errmsg;
962 char save_str2 = *str2;
964 /* Separate the two instructions. */
965 *str2 = 0;
967 /* Make sure the two insns begin on a 32 bit boundary.
968 This is also done for the serial case (foo -> bar), relaxing doesn't
969 affect insns written like this.
970 Note that we must always do this as we can't assume anything about
971 whether we're currently on a 32 bit boundary or not. Relaxing may
972 change this. */
973 fill_insn (0);
975 first.debug_sym_link = debug_sym_link;
976 debug_sym_link = (sym_linkS *) 0;
978 /* Parse the first instruction. */
979 if (! (first.insn = m32r_cgen_assemble_insn
980 (gas_cgen_cpu_desc, str, & first.fields, first.buffer, & errmsg)))
982 as_bad (errmsg);
983 return;
986 /* Check it. */
987 if (CGEN_FIELDS_BITSIZE (&first.fields) != 16)
989 /* xgettext:c-format */
990 as_bad (_("not a 16 bit instruction '%s'"), str);
991 return;
993 #ifdef E_M32R2_ARCH
994 else if ((enable_m32rx == 1)
995 /* FIXME: Need standard macro to perform this test. */
996 && ((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
997 & (1 << MACH_M32R2))
998 && !((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
999 & (1 << MACH_M32RX)))))
1001 /* xgettext:c-format */
1002 as_bad (_("instruction '%s' is for the M32R2 only"), str);
1003 return;
1005 else if ((! enable_special
1006 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL))
1007 || (! enable_special_m32r
1008 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_M32R)))
1009 #else
1010 else if (! enable_special
1011 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL))
1012 #endif
1014 /* xgettext:c-format */
1015 as_bad (_("unknown instruction '%s'"), str);
1016 return;
1018 else if (! enable_m32rx
1019 /* FIXME: Need standard macro to perform this test. */
1020 && (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
1021 == (1 << MACH_M32RX)))
1023 /* xgettext:c-format */
1024 as_bad (_("instruction '%s' is for the M32RX only"), str);
1025 return;
1028 /* Check to see if this is an allowable parallel insn. */
1029 if (parallel_p
1030 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_PIPE) == PIPE_NONE)
1032 /* xgettext:c-format */
1033 as_bad (_("instruction '%s' cannot be executed in parallel."), str);
1034 return;
1037 /* Restore the original assembly text, just in case it is needed. */
1038 *str2 = save_str2;
1040 /* Save the original string pointer. */
1041 str3 = str;
1043 /* Advanced past the parsed string. */
1044 str = str2 + 2;
1046 /* Remember the entire string in case it is needed for error
1047 messages. */
1048 str2 = str3;
1050 /* Convert the opcode to lower case. */
1052 char *s2 = str;
1054 while (ISSPACE (*s2++))
1055 continue;
1057 --s2;
1059 while (ISALNUM (*s2))
1061 *s2 = TOLOWER (*s2);
1062 s2++;
1066 /* Preserve any fixups that have been generated and reset the list
1067 to empty. */
1068 gas_cgen_save_fixups (0);
1070 /* Get the indices of the operands of the instruction. */
1071 /* FIXME: CGEN_FIELDS is already recorded, but relying on that fact
1072 doesn't seem right. Perhaps allow passing fields like we do insn. */
1073 /* FIXME: ALIAS insns do not have operands, so we use this function
1074 to find the equivalent insn and overwrite the value stored in our
1075 structure. We still need the original insn, however, since this
1076 may have certain attributes that are not present in the unaliased
1077 version (eg relaxability). When aliases behave differently this
1078 may have to change. */
1079 first.orig_insn = first.insn;
1081 CGEN_FIELDS tmp_fields;
1082 first.insn = cgen_lookup_get_insn_operands
1083 (gas_cgen_cpu_desc, NULL, INSN_VALUE (first.buffer), NULL, 16,
1084 first.indices, &tmp_fields);
1087 if (first.insn == NULL)
1088 as_fatal (_("internal error: lookup/get operands failed"));
1090 second.debug_sym_link = NULL;
1092 /* Parse the second instruction. */
1093 if (! (second.insn = m32r_cgen_assemble_insn
1094 (gas_cgen_cpu_desc, str, & second.fields, second.buffer, & errmsg)))
1096 as_bad (errmsg);
1097 return;
1100 /* Check it. */
1101 if (CGEN_FIELDS_BITSIZE (&second.fields) != 16)
1103 /* xgettext:c-format */
1104 as_bad (_("not a 16 bit instruction '%s'"), str);
1105 return;
1107 #ifdef E_M32R2_ARCH
1108 else if ((enable_m32rx == 1)
1109 /* FIXME: Need standard macro to perform this test. */
1110 && ((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
1111 & (1 << MACH_M32R2))
1112 && !((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
1113 & (1 << MACH_M32RX)))))
1115 /* xgettext:c-format */
1116 as_bad (_("instruction '%s' is for the M32R2 only"), str);
1117 return;
1119 else if ((! enable_special
1120 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1121 || (! enable_special_m32r
1122 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_M32R)))
1123 #else
1124 else if (! enable_special
1125 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1126 #endif
1128 /* xgettext:c-format */
1129 as_bad (_("unknown instruction '%s'"), str);
1130 return;
1132 else if (! enable_m32rx
1133 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
1135 /* xgettext:c-format */
1136 as_bad (_("instruction '%s' is for the M32RX only"), str);
1137 return;
1140 /* Check to see if this is an allowable parallel insn. */
1141 if (parallel_p
1142 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_PIPE) == PIPE_NONE)
1144 /* xgettext:c-format */
1145 as_bad (_("instruction '%s' cannot be executed in parallel."), str);
1146 return;
1149 if (parallel_p && ! enable_m32rx)
1151 if (CGEN_INSN_NUM (first.insn) != M32R_INSN_NOP
1152 && CGEN_INSN_NUM (second.insn) != M32R_INSN_NOP)
1154 /* xgettext:c-format */
1155 as_bad (_("'%s': only the NOP instruction can be issued in parallel on the m32r"), str2);
1156 return;
1160 /* Get the indices of the operands of the instruction. */
1161 second.orig_insn = second.insn;
1163 CGEN_FIELDS tmp_fields;
1164 second.insn = cgen_lookup_get_insn_operands
1165 (gas_cgen_cpu_desc, NULL, INSN_VALUE (second.buffer), NULL, 16,
1166 second.indices, &tmp_fields);
1169 if (second.insn == NULL)
1170 as_fatal (_("internal error: lookup/get operands failed"));
1172 /* We assume that if the first instruction writes to a register that is
1173 read by the second instruction it is because the programmer intended
1174 this to happen, (after all they have explicitly requested that these
1175 two instructions be executed in parallel). Although if the global
1176 variable warn_explicit_parallel_conflicts is true then we do generate
1177 a warning message. Similarly we assume that parallel branch and jump
1178 instructions are deliberate and should not produce errors. */
1180 if (parallel_p && warn_explicit_parallel_conflicts)
1182 if (first_writes_to_seconds_operands (&first, &second, FALSE))
1183 /* xgettext:c-format */
1184 as_warn (_("%s: output of 1st instruction is the same as an input to 2nd instruction - is this intentional ?"), str2);
1186 if (first_writes_to_seconds_operands (&second, &first, FALSE))
1187 /* xgettext:c-format */
1188 as_warn (_("%s: output of 2nd instruction is the same as an input to 1st instruction - is this intentional ?"), str2);
1191 if (!parallel_p
1192 || (errmsg = (char *) can_make_parallel (&first, &second)) == NULL)
1194 /* Get the fixups for the first instruction. */
1195 gas_cgen_swap_fixups (0);
1197 /* Write it out. */
1198 expand_debug_syms (first.debug_sym_link, 1);
1199 gas_cgen_finish_insn (first.orig_insn, first.buffer,
1200 CGEN_FIELDS_BITSIZE (&first.fields), 0, NULL);
1202 /* Force the top bit of the second insn to be set. */
1203 if (parallel_p)
1204 make_parallel (second.buffer);
1206 /* Get its fixups. */
1207 gas_cgen_restore_fixups (0);
1209 /* Write it out. */
1210 expand_debug_syms (second.debug_sym_link, 1);
1211 gas_cgen_finish_insn (second.orig_insn, second.buffer,
1212 CGEN_FIELDS_BITSIZE (&second.fields), 0, NULL);
1214 /* Try swapping the instructions to see if they work that way. */
1215 else if (can_make_parallel (&second, &first) == NULL)
1217 /* Write out the second instruction first. */
1218 expand_debug_syms (second.debug_sym_link, 1);
1219 gas_cgen_finish_insn (second.orig_insn, second.buffer,
1220 CGEN_FIELDS_BITSIZE (&second.fields), 0, NULL);
1222 /* Force the top bit of the first instruction to be set. */
1223 make_parallel (first.buffer);
1225 /* Get the fixups for the first instruction. */
1226 gas_cgen_restore_fixups (0);
1228 /* Write out the first instruction. */
1229 expand_debug_syms (first.debug_sym_link, 1);
1230 gas_cgen_finish_insn (first.orig_insn, first.buffer,
1231 CGEN_FIELDS_BITSIZE (&first.fields), 0, NULL);
1233 else
1235 as_bad ("'%s': %s", str2, errmsg);
1236 return;
1239 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL)
1240 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1241 m32r_flags |= E_M32R_HAS_HIDDEN_INST;
1242 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_M32R)
1243 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_M32R))
1244 m32r_flags |= E_M32R_HAS_BIT_INST;
1245 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_FLOAT)
1246 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_FLOAT))
1247 m32r_flags |= E_M32R_HAS_FLOAT_INST;
1249 /* Set these so m32r_fill_insn can use them. */
1250 prev_seg = now_seg;
1251 prev_subseg = now_subseg;
1254 void
1255 md_assemble (str)
1256 char *str;
1258 m32r_insn insn;
1259 char *errmsg;
1260 char *str2 = NULL;
1262 /* Initialize GAS's cgen interface for a new instruction. */
1263 gas_cgen_init_parse ();
1265 /* Look for a parallel instruction separator. */
1266 if ((str2 = strstr (str, "||")) != NULL)
1268 assemble_two_insns (str, str2, 1);
1269 m32r_flags |= E_M32R_HAS_PARALLEL;
1270 return;
1273 /* Also look for a sequential instruction separator. */
1274 if ((str2 = strstr (str, "->")) != NULL)
1276 assemble_two_insns (str, str2, 0);
1277 return;
1280 insn.debug_sym_link = debug_sym_link;
1281 debug_sym_link = (sym_linkS *) 0;
1283 insn.insn = m32r_cgen_assemble_insn
1284 (gas_cgen_cpu_desc, str, &insn.fields, insn.buffer, & errmsg);
1286 if (!insn.insn)
1288 as_bad (errmsg);
1289 return;
1292 #ifdef E_M32R2_ARCH
1293 if ((enable_m32rx == 1)
1294 /* FIXME: Need standard macro to perform this test. */
1295 && ((CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH)
1296 & (1 << MACH_M32R2))
1297 && !((CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH)
1298 & (1 << MACH_M32RX)))))
1300 /* xgettext:c-format */
1301 as_bad (_("instruction '%s' is for the M32R2 only"), str);
1302 return;
1304 else if ((! enable_special
1305 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1306 || (! enable_special_m32r
1307 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_M32R)))
1308 #else
1309 if (! enable_special
1310 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1311 #endif
1313 /* xgettext:c-format */
1314 as_bad (_("unknown instruction '%s'"), str);
1315 return;
1317 else if (! enable_m32rx
1318 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
1320 /* xgettext:c-format */
1321 as_bad (_("instruction '%s' is for the M32RX only"), str);
1322 return;
1325 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1326 m32r_flags |= E_M32R_HAS_HIDDEN_INST;
1327 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_M32R))
1328 m32r_flags |= E_M32R_HAS_BIT_INST;
1329 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_FLOAT))
1330 m32r_flags |= E_M32R_HAS_FLOAT_INST;
1332 if (CGEN_INSN_BITSIZE (insn.insn) == 32)
1334 /* 32 bit insns must live on 32 bit boundaries. */
1335 if (prev_insn.insn || seen_relaxable_p)
1337 /* ??? If calling fill_insn too many times turns us into a memory
1338 pig, can we call a fn to assemble a nop instead of
1339 !seen_relaxable_p? */
1340 fill_insn (0);
1343 expand_debug_syms (insn.debug_sym_link, 2);
1345 /* Doesn't really matter what we pass for RELAX_P here. */
1346 gas_cgen_finish_insn (insn.insn, insn.buffer,
1347 CGEN_FIELDS_BITSIZE (&insn.fields), 1, NULL);
1349 else
1351 int on_32bit_boundary_p;
1352 int swap = FALSE;
1354 if (CGEN_INSN_BITSIZE (insn.insn) != 16)
1355 abort ();
1357 insn.orig_insn = insn.insn;
1359 /* If the previous insn was relaxable, then it may be expanded
1360 to fill the current 16 bit slot. Emit a NOP here to occupy
1361 this slot, so that we can start at optimizing at a 32 bit
1362 boundary. */
1363 if (prev_insn.insn && seen_relaxable_p && optimize)
1364 fill_insn (0);
1366 if (enable_m32rx)
1368 /* Get the indices of the operands of the instruction.
1369 FIXME: See assemble_parallel for notes on orig_insn. */
1371 CGEN_FIELDS tmp_fields;
1372 insn.insn = cgen_lookup_get_insn_operands
1373 (gas_cgen_cpu_desc, NULL, INSN_VALUE (insn.buffer), NULL,
1374 16, insn.indices, &tmp_fields);
1377 if (insn.insn == NULL)
1378 as_fatal (_("internal error: lookup/get operands failed"));
1381 /* Compute whether we're on a 32 bit boundary or not.
1382 prev_insn.insn is NULL when we're on a 32 bit boundary. */
1383 on_32bit_boundary_p = prev_insn.insn == NULL;
1385 /* Change a frag to, if each insn to swap is in a different frag.
1386 It must keep only one instruction in a frag. */
1387 if (parallel() && on_32bit_boundary_p)
1389 frag_wane (frag_now);
1390 frag_new (0);
1393 /* Look to see if this instruction can be combined with the
1394 previous instruction to make one, parallel, 32 bit instruction.
1395 If the previous instruction (potentially) changed the flow of
1396 program control, then it cannot be combined with the current
1397 instruction. If the current instruction is relaxable, then it
1398 might be replaced with a longer version, so we cannot combine it.
1399 Also if the output of the previous instruction is used as an
1400 input to the current instruction then it cannot be combined.
1401 Otherwise call can_make_parallel() with both orderings of the
1402 instructions to see if they can be combined. */
1403 if (! on_32bit_boundary_p
1404 && parallel ()
1405 && CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_RELAXABLE) == 0
1406 && ! writes_to_pc (&prev_insn)
1407 && ! first_writes_to_seconds_operands (&prev_insn, &insn, FALSE))
1409 if (can_make_parallel (&prev_insn, &insn) == NULL)
1410 make_parallel (insn.buffer);
1411 else if (can_make_parallel (&insn, &prev_insn) == NULL)
1412 swap = TRUE;
1415 expand_debug_syms (insn.debug_sym_link, 1);
1418 int i;
1419 finished_insnS fi;
1421 /* Ensure each pair of 16 bit insns is in the same frag. */
1422 frag_grow (4);
1424 gas_cgen_finish_insn (insn.orig_insn, insn.buffer,
1425 CGEN_FIELDS_BITSIZE (&insn.fields),
1426 1 /* relax_p */, &fi);
1427 insn.addr = fi.addr;
1428 insn.frag = fi.frag;
1429 insn.num_fixups = fi.num_fixups;
1430 for (i = 0; i < fi.num_fixups; ++i)
1431 insn.fixups[i] = fi.fixups[i];
1434 if (swap)
1436 int i, tmp;
1438 #define SWAP_BYTES(a,b) tmp = a; a = b; b = tmp
1440 /* Swap the two insns */
1441 SWAP_BYTES (prev_insn.addr[0], insn.addr[0]);
1442 SWAP_BYTES (prev_insn.addr[1], insn.addr[1]);
1444 target_make_parallel (insn.addr);
1446 /* Swap any relaxable frags recorded for the two insns. */
1447 /* FIXME: Clarify. relaxation precludes parallel insns */
1448 if (prev_insn.frag->fr_opcode == prev_insn.addr)
1449 prev_insn.frag->fr_opcode = insn.addr;
1450 else if (insn.frag->fr_opcode == insn.addr)
1451 insn.frag->fr_opcode = prev_insn.addr;
1453 /* Change a frag to, if each insn is in a different frag.
1454 It must keep only one instruction in a frag. */
1455 if (prev_insn.frag != insn.frag)
1457 for (i = 0; i < prev_insn.num_fixups; ++i)
1458 prev_insn.fixups[i]->fx_frag = insn.frag;
1459 for (i = 0; i < insn.num_fixups; ++i)
1460 insn.fixups[i]->fx_frag = prev_insn.frag;
1462 else
1464 /* Update the addresses in any fixups.
1465 Note that we don't have to handle the case where each insn is in
1466 a different frag as we ensure they're in the same frag above. */
1467 for (i = 0; i < prev_insn.num_fixups; ++i)
1468 prev_insn.fixups[i]->fx_where += 2;
1469 for (i = 0; i < insn.num_fixups; ++i)
1470 insn.fixups[i]->fx_where -= 2;
1474 /* Keep track of whether we've seen a pair of 16 bit insns.
1475 prev_insn.insn is NULL when we're on a 32 bit boundary. */
1476 if (on_32bit_boundary_p)
1477 prev_insn = insn;
1478 else
1479 prev_insn.insn = NULL;
1481 /* If the insn needs the following one to be on a 32 bit boundary
1482 (e.g. subroutine calls), fill this insn's slot. */
1483 if (on_32bit_boundary_p
1484 && CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_FILL_SLOT) != 0)
1485 fill_insn (0);
1487 /* If this is a relaxable insn (can be replaced with a larger version)
1488 mark the fact so that we can emit an alignment directive for a
1489 following 32 bit insn if we see one. */
1490 if (CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_RELAXABLE) != 0)
1491 seen_relaxable_p = 1;
1494 /* Set these so m32r_fill_insn can use them. */
1495 prev_seg = now_seg;
1496 prev_subseg = now_subseg;
1499 /* The syntax in the manual says constants begin with '#'.
1500 We just ignore it. */
1502 void
1503 md_operand (expressionP)
1504 expressionS *expressionP;
1506 if (*input_line_pointer == '#')
1508 input_line_pointer++;
1509 expression (expressionP);
1513 valueT
1514 md_section_align (segment, size)
1515 segT segment;
1516 valueT size;
1518 int align = bfd_get_section_alignment (stdoutput, segment);
1519 return ((size + (1 << align) - 1) & (-1 << align));
1522 symbolS *
1523 md_undefined_symbol (name)
1524 char *name ATTRIBUTE_UNUSED;
1526 return 0;
1529 /* .scomm pseudo-op handler.
1531 This is a new pseudo-op to handle putting objects in .scommon.
1532 By doing this the linker won't need to do any work,
1533 and more importantly it removes the implicit -G arg necessary to
1534 correctly link the object file. */
1536 static void
1537 m32r_scomm (ignore)
1538 int ignore ATTRIBUTE_UNUSED;
1540 register char *name;
1541 register char c;
1542 register char *p;
1543 offsetT size;
1544 register symbolS *symbolP;
1545 offsetT align;
1546 int align2;
1548 name = input_line_pointer;
1549 c = get_symbol_end ();
1551 /* Just after name is now '\0'. */
1552 p = input_line_pointer;
1553 *p = c;
1554 SKIP_WHITESPACE ();
1555 if (*input_line_pointer != ',')
1557 as_bad (_("Expected comma after symbol-name: rest of line ignored."));
1558 ignore_rest_of_line ();
1559 return;
1562 /* Skip ','. */
1563 input_line_pointer++;
1564 if ((size = get_absolute_expression ()) < 0)
1566 /* xgettext:c-format */
1567 as_warn (_(".SCOMMon length (%ld.) <0! Ignored."), (long) size);
1568 ignore_rest_of_line ();
1569 return;
1572 /* The third argument to .scomm is the alignment. */
1573 if (*input_line_pointer != ',')
1574 align = 8;
1575 else
1577 ++input_line_pointer;
1578 align = get_absolute_expression ();
1579 if (align <= 0)
1581 as_warn (_("ignoring bad alignment"));
1582 align = 8;
1586 /* Convert to a power of 2 alignment. */
1587 if (align)
1589 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
1590 continue;
1591 if (align != 1)
1593 as_bad (_("Common alignment not a power of 2"));
1594 ignore_rest_of_line ();
1595 return;
1598 else
1599 align2 = 0;
1601 *p = 0;
1602 symbolP = symbol_find_or_make (name);
1603 *p = c;
1605 if (S_IS_DEFINED (symbolP))
1607 /* xgettext:c-format */
1608 as_bad (_("Ignoring attempt to re-define symbol `%s'."),
1609 S_GET_NAME (symbolP));
1610 ignore_rest_of_line ();
1611 return;
1614 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1616 /* xgettext:c-format */
1617 as_bad (_("Length of .scomm \"%s\" is already %ld. Not changed to %ld."),
1618 S_GET_NAME (symbolP),
1619 (long) S_GET_VALUE (symbolP),
1620 (long) size);
1622 ignore_rest_of_line ();
1623 return;
1626 if (symbol_get_obj (symbolP)->local)
1628 segT old_sec = now_seg;
1629 int old_subsec = now_subseg;
1630 char *pfrag;
1632 record_alignment (sbss_section, align2);
1633 subseg_set (sbss_section, 0);
1635 if (align2)
1636 frag_align (align2, 0, 0);
1638 if (S_GET_SEGMENT (symbolP) == sbss_section)
1639 symbol_get_frag (symbolP)->fr_symbol = 0;
1641 symbol_set_frag (symbolP, frag_now);
1643 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1644 (char *) 0);
1645 *pfrag = 0;
1646 S_SET_SIZE (symbolP, size);
1647 S_SET_SEGMENT (symbolP, sbss_section);
1648 S_CLEAR_EXTERNAL (symbolP);
1649 subseg_set (old_sec, old_subsec);
1651 else
1653 S_SET_VALUE (symbolP, (valueT) size);
1654 S_SET_ALIGN (symbolP, align2);
1655 S_SET_EXTERNAL (symbolP);
1656 S_SET_SEGMENT (symbolP, &scom_section);
1659 demand_empty_rest_of_line ();
1662 /* Interface to relax_segment. */
1664 /* FIXME: Build table by hand, get it working, then machine generate. */
1666 const relax_typeS md_relax_table[] =
1668 /* The fields are:
1669 1) most positive reach of this state,
1670 2) most negative reach of this state,
1671 3) how many bytes this mode will add to the size of the current frag
1672 4) which index into the table to try if we can't fit into this one. */
1674 /* The first entry must be unused because an `rlx_more' value of zero ends
1675 each list. */
1676 {1, 1, 0, 0},
1678 /* The displacement used by GAS is from the end of the 2 byte insn,
1679 so we subtract 2 from the following. */
1680 /* 16 bit insn, 8 bit disp -> 10 bit range.
1681 This doesn't handle a branch in the right slot at the border:
1682 the "& -4" isn't taken into account. It's not important enough to
1683 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
1684 case). */
1685 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
1686 /* 32 bit insn, 24 bit disp -> 26 bit range. */
1687 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
1688 /* Same thing, but with leading nop for alignment. */
1689 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
1692 long
1693 m32r_relax_frag (segment, fragP, stretch)
1694 segT segment;
1695 fragS *fragP;
1696 long stretch;
1698 /* Address of branch insn. */
1699 long address = fragP->fr_address + fragP->fr_fix - 2;
1700 long growth = 0;
1702 /* Keep 32 bit insns aligned on 32 bit boundaries. */
1703 if (fragP->fr_subtype == 2)
1705 if ((address & 3) != 0)
1707 fragP->fr_subtype = 3;
1708 growth = 2;
1711 else if (fragP->fr_subtype == 3)
1713 if ((address & 3) == 0)
1715 fragP->fr_subtype = 2;
1716 growth = -2;
1719 else
1721 growth = relax_frag (segment, fragP, stretch);
1723 /* Long jump on odd halfword boundary? */
1724 if (fragP->fr_subtype == 2 && (address & 3) != 0)
1726 fragP->fr_subtype = 3;
1727 growth += 2;
1731 return growth;
1734 /* Return an initial guess of the length by which a fragment must grow to
1735 hold a branch to reach its destination.
1736 Also updates fr_type/fr_subtype as necessary.
1738 Called just before doing relaxation.
1739 Any symbol that is now undefined will not become defined.
1740 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
1741 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
1742 Although it may not be explicit in the frag, pretend fr_var starts
1743 with a 0 value. */
1746 md_estimate_size_before_relax (fragP, segment)
1747 fragS *fragP;
1748 segT segment;
1750 /* The only thing we have to handle here are symbols outside of the
1751 current segment. They may be undefined or in a different segment in
1752 which case linker scripts may place them anywhere.
1753 However, we can't finish the fragment here and emit the reloc as insn
1754 alignment requirements may move the insn about. */
1756 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
1757 || S_IS_EXTERNAL (fragP->fr_symbol)
1758 || S_IS_WEAK (fragP->fr_symbol))
1760 #if 0
1761 int old_fr_fix = fragP->fr_fix;
1762 #endif
1764 /* The symbol is undefined in this segment.
1765 Change the relaxation subtype to the max allowable and leave
1766 all further handling to md_convert_frag. */
1767 fragP->fr_subtype = 2;
1769 #if 0
1770 /* Can't use this, but leave in for illustration. */
1771 /* Change 16 bit insn to 32 bit insn. */
1772 fragP->fr_opcode[0] |= 0x80;
1774 /* Increase known (fixed) size of fragment. */
1775 fragP->fr_fix += 2;
1777 /* Create a relocation for it. */
1778 fix_new (fragP, old_fr_fix, 4,
1779 fragP->fr_symbol,
1780 fragP->fr_offset, 1 /* pcrel */,
1781 /* FIXME: Can't use a real BFD reloc here.
1782 gas_cgen_md_apply_fix3 can't handle it. */
1783 BFD_RELOC_M32R_26_PCREL);
1785 /* Mark this fragment as finished. */
1786 frag_wane (fragP);
1787 return fragP->fr_fix - old_fr_fix;
1788 #else
1790 const CGEN_INSN *insn;
1791 int i;
1793 /* Update the recorded insn.
1794 Fortunately we don't have to look very far.
1795 FIXME: Change this to record in the instruction the next higher
1796 relaxable insn to use. */
1797 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
1799 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
1800 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
1801 == 0)
1802 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
1803 break;
1805 if (i == 4)
1806 abort ();
1808 fragP->fr_cgen.insn = insn;
1809 return 2;
1811 #endif
1814 return md_relax_table[fragP->fr_subtype].rlx_length;
1817 /* *FRAGP has been relaxed to its final size, and now needs to have
1818 the bytes inside it modified to conform to the new size.
1820 Called after relaxation is finished.
1821 fragP->fr_type == rs_machine_dependent.
1822 fragP->fr_subtype is the subtype of what the address relaxed to. */
1824 void
1825 md_convert_frag (abfd, sec, fragP)
1826 bfd *abfd ATTRIBUTE_UNUSED;
1827 segT sec;
1828 fragS *fragP;
1830 char *opcode;
1831 char *displacement;
1832 int target_address;
1833 int opcode_address;
1834 int extension;
1835 int addend;
1837 opcode = fragP->fr_opcode;
1839 /* Address opcode resides at in file space. */
1840 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
1842 switch (fragP->fr_subtype)
1844 case 1:
1845 extension = 0;
1846 displacement = &opcode[1];
1847 break;
1848 case 2:
1849 opcode[0] |= 0x80;
1850 extension = 2;
1851 displacement = &opcode[1];
1852 break;
1853 case 3:
1854 opcode[2] = opcode[0] | 0x80;
1855 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
1856 opcode_address += 2;
1857 extension = 4;
1858 displacement = &opcode[3];
1859 break;
1860 default:
1861 abort ();
1864 if (S_GET_SEGMENT (fragP->fr_symbol) != sec
1865 || S_IS_EXTERNAL (fragP->fr_symbol)
1866 || S_IS_WEAK (fragP->fr_symbol))
1868 /* Symbol must be resolved by linker. */
1869 if (fragP->fr_offset & 3)
1870 as_warn (_("Addend to unresolved symbol not on word boundary."));
1871 #ifdef USE_M32R_OLD_RELOC
1872 addend = fragP->fr_offset >> 2; /* Old M32R used USE_REL. */
1873 #else
1874 addend = 0;
1875 #endif
1877 else
1879 /* Address we want to reach in file space. */
1880 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1881 addend = (target_address - (opcode_address & -4)) >> 2;
1884 /* Create a relocation for symbols that must be resolved by the linker.
1885 Otherwise output the completed insn. */
1887 if (S_GET_SEGMENT (fragP->fr_symbol) != sec
1888 || S_IS_EXTERNAL (fragP->fr_symbol)
1889 || S_IS_WEAK (fragP->fr_symbol))
1891 assert (fragP->fr_subtype != 1);
1892 assert (fragP->fr_cgen.insn != 0);
1893 gas_cgen_record_fixup (fragP,
1894 /* Offset of branch insn in frag. */
1895 fragP->fr_fix + extension - 4,
1896 fragP->fr_cgen.insn,
1897 4 /* Length. */,
1898 /* FIXME: quick hack. */
1899 #if 0
1900 cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
1901 fragP->fr_cgen.opindex),
1902 #else
1903 cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
1904 M32R_OPERAND_DISP24),
1905 #endif
1906 fragP->fr_cgen.opinfo,
1907 fragP->fr_symbol, fragP->fr_offset);
1910 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
1912 md_number_to_chars (displacement, (valueT) addend,
1913 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
1915 fragP->fr_fix += extension;
1918 /* Functions concerning relocs. */
1920 /* The location from which a PC relative jump should be calculated,
1921 given a PC relative reloc. */
1923 long
1924 md_pcrel_from_section (fixP, sec)
1925 fixS *fixP;
1926 segT sec;
1928 if (fixP->fx_addsy != (symbolS *) NULL
1929 && (! S_IS_DEFINED (fixP->fx_addsy)
1930 || S_GET_SEGMENT (fixP->fx_addsy) != sec
1931 || S_IS_EXTERNAL (fixP->fx_addsy)
1932 || S_IS_WEAK (fixP->fx_addsy)))
1934 /* The symbol is undefined (or is defined but not in this section).
1935 Let the linker figure it out. */
1936 return 0;
1939 return (fixP->fx_frag->fr_address + fixP->fx_where) & -4L;
1942 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
1943 Returns BFD_RELOC_NONE if no reloc type can be found.
1944 *FIXP may be modified if desired. */
1946 bfd_reloc_code_real_type
1947 md_cgen_lookup_reloc (insn, operand, fixP)
1948 const CGEN_INSN *insn ATTRIBUTE_UNUSED;
1949 const CGEN_OPERAND *operand;
1950 fixS *fixP;
1952 switch (operand->type)
1954 case M32R_OPERAND_DISP8: return BFD_RELOC_M32R_10_PCREL;
1955 case M32R_OPERAND_DISP16: return BFD_RELOC_M32R_18_PCREL;
1956 case M32R_OPERAND_DISP24: return BFD_RELOC_M32R_26_PCREL;
1957 case M32R_OPERAND_UIMM24: return BFD_RELOC_M32R_24;
1958 case M32R_OPERAND_HI16:
1959 case M32R_OPERAND_SLO16:
1960 case M32R_OPERAND_ULO16:
1961 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
1962 if (fixP->fx_cgen.opinfo != 0)
1963 return fixP->fx_cgen.opinfo;
1964 break;
1965 default:
1966 /* Avoid -Wall warning. */
1967 break;
1969 return BFD_RELOC_NONE;
1972 /* Record a HI16 reloc for later matching with its LO16 cousin. */
1974 static void m32r_record_hi16 PARAMS ((int, fixS *, segT));
1976 static void
1977 m32r_record_hi16 (reloc_type, fixP, seg)
1978 int reloc_type;
1979 fixS *fixP;
1980 segT seg ATTRIBUTE_UNUSED;
1982 struct m32r_hi_fixup *hi_fixup;
1984 assert (reloc_type == BFD_RELOC_M32R_HI16_SLO
1985 || reloc_type == BFD_RELOC_M32R_HI16_ULO);
1987 hi_fixup = ((struct m32r_hi_fixup *)
1988 xmalloc (sizeof (struct m32r_hi_fixup)));
1989 hi_fixup->fixp = fixP;
1990 hi_fixup->seg = now_seg;
1991 hi_fixup->next = m32r_hi_fixup_list;
1993 m32r_hi_fixup_list = hi_fixup;
1996 /* Called while parsing an instruction to create a fixup.
1997 We need to check for HI16 relocs and queue them up for later sorting. */
1999 fixS *
2000 m32r_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
2001 fragS *frag;
2002 int where;
2003 const CGEN_INSN *insn;
2004 int length;
2005 const CGEN_OPERAND *operand;
2006 int opinfo;
2007 expressionS *exp;
2009 fixS *fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
2010 operand, opinfo, exp);
2012 switch (operand->type)
2014 case M32R_OPERAND_HI16:
2015 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
2016 if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_SLO
2017 || fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_ULO)
2018 m32r_record_hi16 (fixP->fx_cgen.opinfo, fixP, now_seg);
2019 break;
2020 default:
2021 /* Avoid -Wall warning */
2022 break;
2025 return fixP;
2028 /* Return BFD reloc type from opinfo field in a fixS.
2029 It's tricky using fx_r_type in m32r_frob_file because the values
2030 are BFD_RELOC_UNUSED + operand number. */
2031 #define FX_OPINFO_R_TYPE(f) ((f)->fx_cgen.opinfo)
2033 /* Sort any unmatched HI16 relocs so that they immediately precede
2034 the corresponding LO16 reloc. This is called before md_apply_fix3 and
2035 tc_gen_reloc. */
2037 void
2038 m32r_frob_file ()
2040 struct m32r_hi_fixup *l;
2042 for (l = m32r_hi_fixup_list; l != NULL; l = l->next)
2044 segment_info_type *seginfo;
2045 int pass;
2047 assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_SLO
2048 || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_ULO);
2050 /* Check quickly whether the next fixup happens to be a matching low. */
2051 if (l->fixp->fx_next != NULL
2052 && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_M32R_LO16
2053 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
2054 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
2055 continue;
2057 /* Look through the fixups for this segment for a matching `low'.
2058 When we find one, move the high/shigh just in front of it. We do
2059 this in two passes. In the first pass, we try to find a
2060 unique `low'. In the second pass, we permit multiple high's
2061 relocs for a single `low'. */
2062 seginfo = seg_info (l->seg);
2063 for (pass = 0; pass < 2; pass++)
2065 fixS *f;
2066 fixS *prev;
2068 prev = NULL;
2069 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
2071 /* Check whether this is a `low' fixup which matches l->fixp. */
2072 if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_M32R_LO16
2073 && f->fx_addsy == l->fixp->fx_addsy
2074 && f->fx_offset == l->fixp->fx_offset
2075 && (pass == 1
2076 || prev == NULL
2077 || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_SLO
2078 && FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_ULO)
2079 || prev->fx_addsy != f->fx_addsy
2080 || prev->fx_offset != f->fx_offset))
2082 fixS **pf;
2084 /* Move l->fixp before f. */
2085 for (pf = &seginfo->fix_root;
2086 *pf != l->fixp;
2087 pf = & (*pf)->fx_next)
2088 assert (*pf != NULL);
2090 *pf = l->fixp->fx_next;
2092 l->fixp->fx_next = f;
2093 if (prev == NULL)
2094 seginfo->fix_root = l->fixp;
2095 else
2096 prev->fx_next = l->fixp;
2098 break;
2101 prev = f;
2104 if (f != NULL)
2105 break;
2107 if (pass == 1
2108 && warn_unmatched_high)
2109 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
2110 _("Unmatched high/shigh reloc"));
2115 /* See whether we need to force a relocation into the output file.
2116 This is used to force out switch and PC relative relocations when
2117 relaxing. */
2120 m32r_force_relocation (fix)
2121 fixS *fix;
2123 if (generic_force_reloc (fix))
2124 return 1;
2126 if (! m32r_relax)
2127 return 0;
2129 return fix->fx_pcrel;
2132 /* Write a value out to the object file, using the appropriate endianness. */
2134 void
2135 md_number_to_chars (buf, val, n)
2136 char *buf;
2137 valueT val;
2138 int n;
2140 if (target_big_endian)
2141 number_to_chars_bigendian (buf, val, n);
2142 else
2143 number_to_chars_littleendian (buf, val, n);
2146 /* Turn a string in input_line_pointer into a floating point constant
2147 of type TYPE, and store the appropriate bytes in *LITP. The number
2148 of LITTLENUMS emitted is stored in *SIZEP. An error message is
2149 returned, or NULL on OK. */
2151 /* Equal to MAX_PRECISION in atof-ieee.c. */
2152 #define MAX_LITTLENUMS 6
2154 char *
2155 md_atof (type, litP, sizeP)
2156 char type;
2157 char *litP;
2158 int *sizeP;
2160 int i;
2161 int prec;
2162 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2163 char *t;
2165 switch (type)
2167 case 'f':
2168 case 'F':
2169 case 's':
2170 case 'S':
2171 prec = 2;
2172 break;
2174 case 'd':
2175 case 'D':
2176 case 'r':
2177 case 'R':
2178 prec = 4;
2179 break;
2181 /* FIXME: Some targets allow other format chars for bigger sizes
2182 here. */
2184 default:
2185 *sizeP = 0;
2186 return _("Bad call to md_atof()");
2189 t = atof_ieee (input_line_pointer, type, words);
2190 if (t)
2191 input_line_pointer = t;
2192 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2194 if (target_big_endian)
2196 for (i = 0; i < prec; i++)
2198 md_number_to_chars (litP, (valueT) words[i],
2199 sizeof (LITTLENUM_TYPE));
2200 litP += sizeof (LITTLENUM_TYPE);
2203 else
2205 for (i = prec - 1; i >= 0; i--)
2207 md_number_to_chars (litP, (valueT) words[i],
2208 sizeof (LITTLENUM_TYPE));
2209 litP += sizeof (LITTLENUM_TYPE);
2213 return 0;
2216 void
2217 m32r_elf_section_change_hook ()
2219 /* If we have reached the end of a section and we have just emitted a
2220 16 bit insn, then emit a nop to make sure that the section ends on
2221 a 32 bit boundary. */
2223 if (prev_insn.insn || seen_relaxable_p)
2224 (void) m32r_fill_insn (0);
2227 /* Return true if can adjust the reloc to be relative to its section
2228 (such as .data) instead of relative to some symbol. */
2230 bfd_boolean
2231 m32r_fix_adjustable (fixP)
2232 fixS *fixP;
2234 bfd_reloc_code_real_type reloc_type;
2236 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2238 const CGEN_INSN *insn = NULL;
2239 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2240 const CGEN_OPERAND *operand =
2241 cgen_operand_lookup_by_num(gas_cgen_cpu_desc, opindex);
2242 reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
2244 else
2245 reloc_type = fixP->fx_r_type;
2247 if (fixP->fx_addsy == NULL)
2248 return 1;
2250 /* Prevent all adjustments to global symbols. */
2251 if (S_IS_EXTERN (fixP->fx_addsy))
2252 return 0;
2253 if (S_IS_WEAK (fixP->fx_addsy))
2254 return 0;
2256 if (pic_code
2257 && (reloc_type == BFD_RELOC_M32R_24
2258 || reloc_type == BFD_RELOC_M32R_26_PCREL
2259 || reloc_type == BFD_RELOC_M32R_HI16_SLO
2260 || reloc_type == BFD_RELOC_M32R_HI16_ULO
2261 || reloc_type == BFD_RELOC_M32R_LO16))
2262 return 0;
2264 /* We need the symbol name for the VTABLE entries. */
2265 if (reloc_type == BFD_RELOC_VTABLE_INHERIT
2266 || reloc_type == BFD_RELOC_VTABLE_ENTRY)
2267 return 0;
2269 return 1;
2272 void
2273 m32r_elf_final_processing ()
2275 if (use_parallel)
2276 m32r_flags |= E_M32R_HAS_PARALLEL;
2277 elf_elfheader (stdoutput)->e_flags |= m32r_flags;
2280 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
2282 /* Translate internal representation of relocation info to BFD target
2283 format. */
2284 arelent *
2285 tc_gen_reloc (section, fixP)
2286 asection * section;
2287 fixS * fixP;
2289 arelent * reloc;
2290 bfd_reloc_code_real_type code;
2292 reloc = (arelent *) xmalloc (sizeof (arelent));
2294 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2295 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
2296 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
2298 code = fixP->fx_r_type;
2299 if (pic_code)
2301 #ifdef DEBUG_PIC
2302 printf("%s",bfd_get_reloc_code_name(code));
2303 #endif
2304 switch (code)
2306 case BFD_RELOC_M32R_26_PCREL:
2307 code = BFD_RELOC_M32R_26_PLTREL;
2308 break;
2309 case BFD_RELOC_M32R_24:
2310 if (fixP->fx_addsy != NULL
2311 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2312 code = BFD_RELOC_M32R_GOTPC24;
2313 else
2314 code = BFD_RELOC_M32R_GOT24;
2315 break;
2316 case BFD_RELOC_M32R_HI16_ULO:
2317 if (fixP->fx_addsy != NULL
2318 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2319 code = BFD_RELOC_M32R_GOTPC_HI_ULO;
2320 else
2321 code = BFD_RELOC_M32R_GOT16_HI_ULO;
2322 break;
2323 case BFD_RELOC_M32R_HI16_SLO:
2324 if (fixP->fx_addsy != NULL
2325 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2326 code = BFD_RELOC_M32R_GOTPC_HI_SLO;
2327 else
2328 code = BFD_RELOC_M32R_GOT16_HI_SLO;
2329 break;
2330 case BFD_RELOC_M32R_LO16:
2331 if (fixP->fx_addsy != NULL
2332 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2333 code = BFD_RELOC_M32R_GOTPC_LO;
2334 else
2335 code = BFD_RELOC_M32R_GOT16_LO;
2336 break;
2337 default:
2338 break;
2340 #ifdef DEBUG_PIC
2341 printf(" => %s",bfd_get_reloc_code_name(code));
2342 #endif
2345 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2346 #ifdef DEBUG_PIC
2347 printf(" => %s\n",reloc->howto->name);
2348 #endif
2349 if (reloc->howto == (reloc_howto_type *) NULL)
2351 as_bad_where (fixP->fx_file, fixP->fx_line,
2352 _("internal error: can't export reloc type %d (`%s')"),
2353 fixP->fx_r_type, bfd_get_reloc_code_name (code));
2354 return NULL;
2357 /* Use fx_offset for these cases */
2358 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2359 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
2360 reloc->addend = fixP->fx_offset;
2361 else if (!pic_code
2362 && fixP->fx_pcrel
2363 && fixP->fx_addsy != NULL
2364 && (S_GET_SEGMENT(fixP->fx_addsy) != section)
2365 && S_IS_DEFINED (fixP->fx_addsy)
2366 && ! S_IS_EXTERNAL(fixP->fx_addsy)
2367 && ! S_IS_WEAK(fixP->fx_addsy))
2368 /* already used fx_offset in the opcode field itseld. */
2369 reloc->addend = 0;
2370 else
2371 reloc->addend = fixP->fx_addnumber;
2373 return reloc;