tc-m32r.c revision 1.1 1 /* tc-m32r.c -- Assembler for the Renesas M32R.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009, 2011, 2012 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
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 3, or (at your option)
10 any later version.
11
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.
16
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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "symcat.h"
26 #include "opcodes/m32r-desc.h"
27 #include "opcodes/m32r-opc.h"
28 #include "cgen.h"
29 #include "elf/m32r.h"
30
31 /* Linked list of symbols that are debugging symbols to be defined as the
32 beginning of the current instruction. */
33 typedef struct sym_link
34 {
35 struct sym_link *next;
36 symbolS *symbol;
37 } sym_linkS;
38
39 static sym_linkS *debug_sym_link = (sym_linkS *) 0;
40
41 /* Structure to hold all of the different components describing
42 an individual instruction. */
43 typedef struct
44 {
45 const CGEN_INSN *insn;
46 const CGEN_INSN *orig_insn;
47 CGEN_FIELDS fields;
48 #if CGEN_INT_INSN_P
49 CGEN_INSN_INT buffer[1];
50 #define INSN_VALUE(buf) (*(buf))
51 #else
52 unsigned char buffer[CGEN_MAX_INSN_SIZE];
53 #define INSN_VALUE(buf) (buf)
54 #endif
55 char *addr;
56 fragS *frag;
57 int num_fixups;
58 fixS *fixups[GAS_CGEN_MAX_FIXUPS];
59 int indices[MAX_OPERAND_INSTANCES];
60 sym_linkS *debug_sym_link;
61 }
62 m32r_insn;
63
64 /* prev_insn.insn is non-null if last insn was a 16 bit insn on a 32 bit
65 boundary (i.e. was the first of two 16 bit insns). */
66 static m32r_insn prev_insn;
67
68 /* Non-zero if we've seen a relaxable insn since the last 32 bit
69 alignment request. */
70 static int seen_relaxable_p = 0;
71
72 /* Non-zero if we are generating PIC code. */
73 int pic_code;
74
75 /* Non-zero if -relax specified, in which case sufficient relocs are output
76 for the linker to do relaxing.
77 We do simple forms of relaxing internally, but they are always done.
78 This flag does not apply to them. */
79 static int m32r_relax;
80
81 /* Non-zero if warn when a high/shigh reloc has no matching low reloc.
82 Each high/shigh reloc must be paired with it's low cousin in order to
83 properly calculate the addend in a relocatable link (since there is a
84 potential carry from the low to the high/shigh).
85 This option is off by default though for user-written assembler code it
86 might make sense to make the default be on (i.e. have gcc pass a flag
87 to turn it off). This warning must not be on for GCC created code as
88 optimization may delete the low but not the high/shigh (at least we
89 shouldn't assume or require it to). */
90 static int warn_unmatched_high = 0;
91
92 /* 1 if -m32rx has been specified, in which case support for
93 the extended M32RX instruction set should be enabled.
94 2 if -m32r2 has been specified, in which case support for
95 the extended M32R2 instruction set should be enabled. */
96 static int enable_m32rx = 0; /* Default to M32R. */
97
98 /* Non-zero if -m32rx -hidden has been specified, in which case support for
99 the special M32RX instruction set should be enabled. */
100 static int enable_special = 0;
101
102 /* Non-zero if -bitinst has been specified, in which case support
103 for extended M32R bit-field instruction set should be enabled. */
104 static int enable_special_m32r = 1;
105
106 /* Non-zero if -float has been specified, in which case support for
107 extended M32R floating point instruction set should be enabled. */
108 static int enable_special_float = 0;
109
110 /* Non-zero if the programmer should be warned when an explicit parallel
111 instruction might have constraint violations. */
112 static int warn_explicit_parallel_conflicts = 1;
113
114 /* Non-zero if the programmer should not receive any messages about
115 parallel instruction with potential or real constraint violations.
116 The ability to suppress these messages is intended only for hardware
117 vendors testing the chip. It superceedes
118 warn_explicit_parallel_conflicts. */
119 static int ignore_parallel_conflicts = 0;
120
121 /* Non-zero if insns can be made parallel. */
122 static int use_parallel = 0;
123
124 /* Non-zero if optimizations should be performed. */
125 static int optimize;
126
127 /* m32r er_flags. */
128 static int m32r_flags = 0;
129
130 /* Stuff for .scomm symbols. */
131 static segT sbss_section;
132 static asection scom_section;
133 static asymbol scom_symbol;
134
135 const char comment_chars[] = ";";
136 const char line_comment_chars[] = "#";
137 const char line_separator_chars[] = "!";
138 const char EXP_CHARS[] = "eE";
139 const char FLT_CHARS[] = "dD";
140
141 /* Relocations against symbols are done in two
142 parts, with a HI relocation and a LO relocation. Each relocation
143 has only 16 bits of space to store an addend. This means that in
144 order for the linker to handle carries correctly, it must be able
145 to locate both the HI and the LO relocation. This means that the
146 relocations must appear in order in the relocation table.
147
148 In order to implement this, we keep track of each unmatched HI
149 relocation. We then sort them so that they immediately precede the
150 corresponding LO relocation. */
151
152 struct m32r_hi_fixup
153 {
154 /* Next HI fixup. */
155 struct m32r_hi_fixup *next;
156
157 /* This fixup. */
158 fixS *fixp;
159
160 /* The section this fixup is in. */
161 segT seg;
162 };
163
164 /* The list of unmatched HI relocs. */
165
166 static struct m32r_hi_fixup *m32r_hi_fixup_list;
167
168 struct
170 {
171 enum bfd_architecture bfd_mach;
172 int mach_flags;
173 } mach_table[] =
174 {
175 { bfd_mach_m32r, (1<<MACH_M32R) },
176 { bfd_mach_m32rx, (1<<MACH_M32RX) },
177 { bfd_mach_m32r2, (1<<MACH_M32R2) }
178 };
179
180 static void
181 allow_m32rx (int on)
182 {
183 enable_m32rx = on;
184
185 if (stdoutput != NULL)
186 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach_table[on].bfd_mach);
187
188 if (gas_cgen_cpu_desc != NULL)
189 gas_cgen_cpu_desc->machs = mach_table[on].mach_flags;
190 }
191
192 #define M32R_SHORTOPTS "O::K:"
194
195 const char *md_shortopts = M32R_SHORTOPTS;
196
197 enum md_option_enums
198 {
199 OPTION_M32R = OPTION_MD_BASE,
200 OPTION_M32RX,
201 OPTION_M32R2,
202 OPTION_BIG,
203 OPTION_LITTLE,
204 OPTION_PARALLEL,
205 OPTION_NO_PARALLEL,
206 OPTION_WARN_PARALLEL,
207 OPTION_NO_WARN_PARALLEL,
208 OPTION_IGNORE_PARALLEL,
209 OPTION_NO_IGNORE_PARALLEL,
210 OPTION_SPECIAL,
211 OPTION_SPECIAL_M32R,
212 OPTION_NO_SPECIAL_M32R,
213 OPTION_SPECIAL_FLOAT,
214 OPTION_WARN_UNMATCHED,
215 OPTION_NO_WARN_UNMATCHED
216 };
217
218 struct option md_longopts[] =
219 {
220 {"m32r", no_argument, NULL, OPTION_M32R},
221 {"m32rx", no_argument, NULL, OPTION_M32RX},
222 {"m32r2", no_argument, NULL, OPTION_M32R2},
223 {"big", no_argument, NULL, OPTION_BIG},
224 {"little", no_argument, NULL, OPTION_LITTLE},
225 {"EB", no_argument, NULL, OPTION_BIG},
226 {"EL", no_argument, NULL, OPTION_LITTLE},
227 {"parallel", no_argument, NULL, OPTION_PARALLEL},
228 {"no-parallel", no_argument, NULL, OPTION_NO_PARALLEL},
229 {"warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_WARN_PARALLEL},
230 {"Wp", no_argument, NULL, OPTION_WARN_PARALLEL},
231 {"no-warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_NO_WARN_PARALLEL},
232 {"Wnp", no_argument, NULL, OPTION_NO_WARN_PARALLEL},
233 {"ignore-parallel-conflicts", no_argument, NULL, OPTION_IGNORE_PARALLEL},
234 {"Ip", no_argument, NULL, OPTION_IGNORE_PARALLEL},
235 {"no-ignore-parallel-conflicts", no_argument, NULL, OPTION_NO_IGNORE_PARALLEL},
236 {"nIp", no_argument, NULL, OPTION_NO_IGNORE_PARALLEL},
237 {"hidden", no_argument, NULL, OPTION_SPECIAL},
238 {"bitinst", no_argument, NULL, OPTION_SPECIAL_M32R},
239 {"no-bitinst", no_argument, NULL, OPTION_NO_SPECIAL_M32R},
240 {"float", no_argument, NULL, OPTION_SPECIAL_FLOAT},
241 /* Sigh. I guess all warnings must now have both variants. */
242 {"warn-unmatched-high", no_argument, NULL, OPTION_WARN_UNMATCHED},
243 {"Wuh", no_argument, NULL, OPTION_WARN_UNMATCHED},
244 {"no-warn-unmatched-high", no_argument, NULL, OPTION_NO_WARN_UNMATCHED},
245 {"Wnuh", no_argument, NULL, OPTION_NO_WARN_UNMATCHED},
246 {NULL, no_argument, NULL, 0}
247 };
248
249 size_t md_longopts_size = sizeof (md_longopts);
250
251 static void
252 little (int on)
253 {
254 target_big_endian = ! on;
255 }
256
257 /* Use parallel execution. */
258
259 static int
260 parallel (void)
261 {
262 if (! enable_m32rx)
263 return 0;
264
265 if (use_parallel == 1)
266 return 1;
267
268 return 0;
269 }
270
271 int
272 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
273 {
274 switch (c)
275 {
276 case 'O':
277 optimize = 1;
278 use_parallel = 1;
279 break;
280
281 case OPTION_M32R:
282 allow_m32rx (0);
283 break;
284
285 case OPTION_M32RX:
286 allow_m32rx (1);
287 break;
288
289 case OPTION_M32R2:
290 allow_m32rx (2);
291 enable_special = 1;
292 enable_special_m32r = 1;
293 break;
294
295 case OPTION_BIG:
296 target_big_endian = 1;
297 break;
298
299 case OPTION_LITTLE:
300 target_big_endian = 0;
301 break;
302
303 case OPTION_PARALLEL:
304 use_parallel = 1;
305 break;
306
307 case OPTION_NO_PARALLEL:
308 use_parallel = 0;
309 break;
310
311 case OPTION_WARN_PARALLEL:
312 warn_explicit_parallel_conflicts = 1;
313 break;
314
315 case OPTION_NO_WARN_PARALLEL:
316 warn_explicit_parallel_conflicts = 0;
317 break;
318
319 case OPTION_IGNORE_PARALLEL:
320 ignore_parallel_conflicts = 1;
321 break;
322
323 case OPTION_NO_IGNORE_PARALLEL:
324 ignore_parallel_conflicts = 0;
325 break;
326
327 case OPTION_SPECIAL:
328 if (enable_m32rx)
329 enable_special = 1;
330 else
331 {
332 /* Pretend that we do not recognise this option. */
333 as_bad (_("Unrecognised option: -hidden"));
334 return 0;
335 }
336 break;
337
338 case OPTION_SPECIAL_M32R:
339 enable_special_m32r = 1;
340 break;
341
342 case OPTION_NO_SPECIAL_M32R:
343 enable_special_m32r = 0;
344 break;
345
346 case OPTION_SPECIAL_FLOAT:
347 enable_special_float = 1;
348 break;
349
350 case OPTION_WARN_UNMATCHED:
351 warn_unmatched_high = 1;
352 break;
353
354 case OPTION_NO_WARN_UNMATCHED:
355 warn_unmatched_high = 0;
356 break;
357
358 case 'K':
359 if (strcmp (arg, "PIC") != 0)
360 as_warn (_("Unrecognized option following -K"));
361 else
362 pic_code = 1;
363 break;
364
365 default:
366 return 0;
367 }
368
369 return 1;
370 }
371
372 void
373 md_show_usage (FILE *stream)
374 {
375 fprintf (stream, _(" M32R specific command line options:\n"));
376
377 fprintf (stream, _("\
378 -m32r disable support for the m32rx instruction set\n"));
379 fprintf (stream, _("\
380 -m32rx support the extended m32rx instruction set\n"));
381 fprintf (stream, _("\
382 -m32r2 support the extended m32r2 instruction set\n"));
383 fprintf (stream, _("\
384 -EL,-little produce little endian code and data\n"));
385 fprintf (stream, _("\
386 -EB,-big produce big endian code and data\n"));
387 fprintf (stream, _("\
388 -parallel try to combine instructions in parallel\n"));
389 fprintf (stream, _("\
390 -no-parallel disable -parallel\n"));
391 fprintf (stream, _("\
392 -no-bitinst disallow the M32R2's extended bit-field instructions\n"));
393 fprintf (stream, _("\
394 -O try to optimize code. Implies -parallel\n"));
395
396 fprintf (stream, _("\
397 -warn-explicit-parallel-conflicts warn when parallel instructions\n"));
398 fprintf (stream, _("\
399 might violate contraints\n"));
400 fprintf (stream, _("\
401 -no-warn-explicit-parallel-conflicts do not warn when parallel\n"));
402 fprintf (stream, _("\
403 instructions might violate contraints\n"));
404 fprintf (stream, _("\
405 -Wp synonym for -warn-explicit-parallel-conflicts\n"));
406 fprintf (stream, _("\
407 -Wnp synonym for -no-warn-explicit-parallel-conflicts\n"));
408 fprintf (stream, _("\
409 -ignore-parallel-conflicts do not check parallel instructions\n"));
410 fprintf (stream, _("\
411 for constraint violations\n"));
412 fprintf (stream, _("\
413 -no-ignore-parallel-conflicts check parallel instructions for\n"));
414 fprintf (stream, _("\
415 constraint violations\n"));
416 fprintf (stream, _("\
417 -Ip synonym for -ignore-parallel-conflicts\n"));
418 fprintf (stream, _("\
419 -nIp synonym for -no-ignore-parallel-conflicts\n"));
420
421 fprintf (stream, _("\
422 -warn-unmatched-high warn when an (s)high reloc has no matching low reloc\n"));
423 fprintf (stream, _("\
424 -no-warn-unmatched-high do not warn about missing low relocs\n"));
425 fprintf (stream, _("\
426 -Wuh synonym for -warn-unmatched-high\n"));
427 fprintf (stream, _("\
428 -Wnuh synonym for -no-warn-unmatched-high\n"));
429
430 fprintf (stream, _("\
431 -KPIC generate PIC\n"));
432 }
433
434 /* Set by md_assemble for use by m32r_fill_insn. */
435 static subsegT prev_subseg;
436 static segT prev_seg;
437
438 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
439 symbolS * GOT_symbol;
440
441 static inline int
442 m32r_PIC_related_p (symbolS *sym)
443 {
444 expressionS *exp;
445
446 if (! sym)
447 return 0;
448
449 if (sym == GOT_symbol)
450 return 1;
451
452 exp = symbol_get_value_expression (sym);
453
454 return (exp->X_op == O_PIC_reloc
455 || exp->X_md == BFD_RELOC_M32R_26_PLTREL
456 || m32r_PIC_related_p (exp->X_add_symbol)
457 || m32r_PIC_related_p (exp->X_op_symbol));
458 }
459
460 static inline int
461 m32r_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
462 {
463 expressionS *exp = main_exp;
464
465 if (exp->X_op == O_add && m32r_PIC_related_p (exp->X_op_symbol))
466 return 1;
467
468 if (exp->X_op == O_symbol && exp->X_add_symbol)
469 {
470 if (exp->X_add_symbol == GOT_symbol)
471 {
472 *r_type_p = BFD_RELOC_M32R_GOTPC24;
473 return 0;
474 }
475 }
476 else if (exp->X_op == O_add)
477 {
478 exp = symbol_get_value_expression (exp->X_add_symbol);
479 if (! exp)
480 return 0;
481 }
482
483 if (exp->X_op == O_PIC_reloc)
484 {
485 *r_type_p = exp->X_md;
486 if (exp == main_exp)
487 exp->X_op = O_symbol;
488 else
489 {
490 main_exp->X_add_symbol = exp->X_add_symbol;
491 main_exp->X_add_number += exp->X_add_number;
492 }
493 }
494 else
495 return (m32r_PIC_related_p (exp->X_add_symbol)
496 || m32r_PIC_related_p (exp->X_op_symbol));
497
498 return 0;
499 }
500
501 /* FIXME: Should be machine generated. */
502 #define NOP_INSN 0x7000
503 #define PAR_NOP_INSN 0xf000 /* Can only be used in 2nd slot. */
504
505 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
506 of an rs_align_code fragment. */
507
508 void
509 m32r_handle_align (fragS *fragp)
510 {
511 static const unsigned char nop_pattern[] = { 0xf0, 0x00 };
512 static const unsigned char multi_nop_pattern[] = { 0x70, 0x00, 0xf0, 0x00 };
513
514 int bytes, fix;
515 char *p;
516
517 if (fragp->fr_type != rs_align_code)
518 return;
519
520 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
521 p = fragp->fr_literal + fragp->fr_fix;
522 fix = 0;
523
524 if (bytes & 1)
525 {
526 fix = 1;
527 *p++ = 0;
528 bytes--;
529 }
530
531 if (bytes & 2)
532 {
533 memcpy (p, nop_pattern, 2);
534 p += 2;
535 bytes -= 2;
536 fix += 2;
537 }
538
539 memcpy (p, multi_nop_pattern, 4);
540
541 fragp->fr_fix += fix;
542 fragp->fr_var = 4;
543 }
544
545 /* If the last instruction was the first of 2 16 bit insns,
546 output a nop to move the PC to a 32 bit boundary.
547
548 This is done via an alignment specification since branch relaxing
549 may make it unnecessary.
550
551 Internally, we need to output one of these each time a 32 bit insn is
552 seen after an insn that is relaxable. */
553
554 static void
555 fill_insn (int ignore ATTRIBUTE_UNUSED)
556 {
557 frag_align_code (2, 0);
558 prev_insn.insn = NULL;
559 seen_relaxable_p = 0;
560 }
561
562 /* Record the symbol so that when we output the insn, we can create
563 a symbol that is at the start of the instruction. This is used
564 to emit the label for the start of a breakpoint without causing
565 the assembler to emit a NOP if the previous instruction was a
566 16 bit instruction. */
567
568 static void
569 debug_sym (int ignore ATTRIBUTE_UNUSED)
570 {
571 char *name;
572 char delim;
573 char *end_name;
574 symbolS *symbolP;
575 sym_linkS *lnk;
576
577 name = input_line_pointer;
578 delim = get_symbol_end ();
579 end_name = input_line_pointer;
580
581 if ((symbolP = symbol_find (name)) == NULL
582 && (symbolP = md_undefined_symbol (name)) == NULL)
583 symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
584
585 symbol_table_insert (symbolP);
586 if (S_IS_DEFINED (symbolP) && (S_GET_SEGMENT (symbolP) != reg_section
587 || S_IS_EXTERNAL (symbolP)
588 || S_IS_WEAK (symbolP)))
589 /* xgettext:c-format */
590 as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
591
592 else
593 {
594 lnk = (sym_linkS *) xmalloc (sizeof (sym_linkS));
595 lnk->symbol = symbolP;
596 lnk->next = debug_sym_link;
597 debug_sym_link = lnk;
598 symbol_get_obj (symbolP)->local = 1;
599 }
600
601 *end_name = delim;
602 demand_empty_rest_of_line ();
603 }
604
605 /* Second pass to expanding the debug symbols, go through linked
606 list of symbols and reassign the address. */
607
608 static void
609 expand_debug_syms (sym_linkS *syms, int align)
610 {
611 char *save_input_line = input_line_pointer;
612 sym_linkS *next_syms;
613
614 if (!syms)
615 return;
616
617 (void) frag_align_code (align, 0);
618 for (; syms != (sym_linkS *) 0; syms = next_syms)
619 {
620 symbolS *symbolP = syms->symbol;
621 next_syms = syms->next;
622 input_line_pointer = ".\n";
623 pseudo_set (symbolP);
624 free ((char *) syms);
625 }
626
627 input_line_pointer = save_input_line;
628 }
629
630 void
631 m32r_flush_pending_output (void)
632 {
633 if (debug_sym_link)
634 {
635 expand_debug_syms (debug_sym_link, 1);
636 debug_sym_link = (sym_linkS *) 0;
637 }
638 }
639
640 /* Cover function to fill_insn called after a label and at end of assembly.
641 The result is always 1: we're called in a conditional to see if the
642 current line is a label. */
643
644 int
645 m32r_fill_insn (int done)
646 {
647 if (prev_seg != NULL)
648 {
649 segT seg = now_seg;
650 subsegT subseg = now_subseg;
651
652 subseg_set (prev_seg, prev_subseg);
653
654 fill_insn (0);
655
656 subseg_set (seg, subseg);
657 }
658
659 if (done && debug_sym_link)
660 {
661 expand_debug_syms (debug_sym_link, 1);
662 debug_sym_link = (sym_linkS *) 0;
663 }
664
665 return 1;
666 }
667
668 /* The default target format to use. */
670
671 const char *
672 m32r_target_format (void)
673 {
674 #ifdef TE_LINUX
675 if (target_big_endian)
676 return "elf32-m32r-linux";
677 else
678 return "elf32-m32rle-linux";
679 #else
680 if (target_big_endian)
681 return "elf32-m32r";
682 else
683 return "elf32-m32rle";
684 #endif
685 }
686
687 void
688 md_begin (void)
689 {
690 flagword applicable;
691 segT seg;
692 subsegT subseg;
693
694 /* Initialize the `cgen' interface. */
695
696 /* Set the machine number and endian. */
697 gas_cgen_cpu_desc = m32r_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
698 CGEN_CPU_OPEN_ENDIAN,
699 (target_big_endian ?
700 CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE),
701 CGEN_CPU_OPEN_END);
702 m32r_cgen_init_asm (gas_cgen_cpu_desc);
703
704 /* The operand instance table is used during optimization to determine
705 which insns can be executed in parallel. It is also used to give
706 warnings regarding operand interference in parallel insns. */
707 m32r_cgen_init_opinst_table (gas_cgen_cpu_desc);
708
709 /* This is a callback from cgen to gas to parse operands. */
710 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
711
712 /* Save the current subseg so we can restore it [it's the default one and
713 we don't want the initial section to be .sbss]. */
714 seg = now_seg;
715 subseg = now_subseg;
716
717 /* The sbss section is for local .scomm symbols. */
718 sbss_section = subseg_new (".sbss", 0);
719 seg_info (sbss_section)->bss = 1;
720
721 /* This is copied from perform_an_assembly_pass. */
722 applicable = bfd_applicable_section_flags (stdoutput);
723 bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
724
725 subseg_set (seg, subseg);
726
727 /* We must construct a fake section similar to bfd_com_section
728 but with the name .scommon. */
729 scom_section = *bfd_com_section_ptr;
730 scom_section.name = ".scommon";
731 scom_section.output_section = & scom_section;
732 scom_section.symbol = & scom_symbol;
733 scom_section.symbol_ptr_ptr = & scom_section.symbol;
734 scom_symbol = * bfd_com_section_ptr->symbol;
735 scom_symbol.name = ".scommon";
736 scom_symbol.section = & scom_section;
737
738 allow_m32rx (enable_m32rx);
739
740 gas_cgen_initialize_saved_fixups_array ();
741 }
742
743 #define OPERAND_IS_COND_BIT(operand, indices, index) \
744 ((operand)->hw_type == HW_H_COND \
745 || ((operand)->hw_type == HW_H_PSW) \
746 || ((operand)->hw_type == HW_H_CR \
747 && (indices [index] == 0 || indices [index] == 1)))
748
749 /* Returns true if an output of instruction 'a' is referenced by an operand
750 of instruction 'b'. If 'check_outputs' is true then b's outputs are
751 checked, otherwise its inputs are examined. */
752
753 static int
754 first_writes_to_seconds_operands (m32r_insn *a,
755 m32r_insn *b,
756 const int check_outputs)
757 {
758 const CGEN_OPINST *a_operands = CGEN_INSN_OPERANDS (a->insn);
759 const CGEN_OPINST *b_ops = CGEN_INSN_OPERANDS (b->insn);
760 int a_index;
761
762 if (ignore_parallel_conflicts)
763 return 0;
764
765 /* If at least one of the instructions takes no operands, then there is
766 nothing to check. There really are instructions without operands,
767 eg 'nop'. */
768 if (a_operands == NULL || b_ops == NULL)
769 return 0;
770
771 /* Scan the operand list of 'a' looking for an output operand. */
772 for (a_index = 0;
773 a_operands->type != CGEN_OPINST_END;
774 a_index ++, a_operands ++)
775 {
776 if (a_operands->type == CGEN_OPINST_OUTPUT)
777 {
778 int b_index;
779 const CGEN_OPINST *b_operands = b_ops;
780
781 /* Special Case:
782 The Condition bit 'C' is a shadow of the CBR register (control
783 register 1) and also a shadow of bit 31 of the program status
784 word (control register 0). For now this is handled here, rather
785 than by cgen.... */
786
787 if (OPERAND_IS_COND_BIT (a_operands, a->indices, a_index))
788 {
789 /* Scan operand list of 'b' looking for another reference to the
790 condition bit, which goes in the right direction. */
791 for (b_index = 0;
792 b_operands->type != CGEN_OPINST_END;
793 b_index++, b_operands++)
794 {
795 if ((b_operands->type
796 == (check_outputs
797 ? CGEN_OPINST_OUTPUT
798 : CGEN_OPINST_INPUT))
799 && OPERAND_IS_COND_BIT (b_operands, b->indices, b_index))
800 return 1;
801 }
802 }
803 else
804 {
805 /* Scan operand list of 'b' looking for an operand that
806 references the same hardware element, and which goes in the
807 right direction. */
808 for (b_index = 0;
809 b_operands->type != CGEN_OPINST_END;
810 b_index++, b_operands++)
811 {
812 if ((b_operands->type
813 == (check_outputs
814 ? CGEN_OPINST_OUTPUT
815 : CGEN_OPINST_INPUT))
816 && (b_operands->hw_type == a_operands->hw_type)
817 && (a->indices[a_index] == b->indices[b_index]))
818 return 1;
819 }
820 }
821 }
822 }
823
824 return 0;
825 }
826
827 /* Returns true if the insn can (potentially) alter the program counter. */
828
829 static int
830 writes_to_pc (m32r_insn *a)
831 {
832 if (CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_UNCOND_CTI)
833 || CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_COND_CTI))
834 return 1;
835 return 0;
836 }
837
838 /* Return NULL if the two 16 bit insns can be executed in parallel.
839 Otherwise return a pointer to an error message explaining why not. */
840
841 static const char *
842 can_make_parallel (m32r_insn *a, m32r_insn *b)
843 {
844 PIPE_ATTR a_pipe;
845 PIPE_ATTR b_pipe;
846
847 /* Make sure the instructions are the right length. */
848 if (CGEN_FIELDS_BITSIZE (&a->fields) != 16
849 || CGEN_FIELDS_BITSIZE (&b->fields) != 16)
850 abort ();
851
852 if (first_writes_to_seconds_operands (a, b, TRUE))
853 return _("instructions write to the same destination register.");
854
855 a_pipe = CGEN_INSN_ATTR_VALUE (a->insn, CGEN_INSN_PIPE);
856 b_pipe = CGEN_INSN_ATTR_VALUE (b->insn, CGEN_INSN_PIPE);
857
858 /* Make sure that the instructions use the correct execution pipelines. */
859 if (a_pipe == PIPE_NONE
860 || b_pipe == PIPE_NONE)
861 return _("Instructions do not use parallel execution pipelines.");
862
863 /* Leave this test for last, since it is the only test that can
864 go away if the instructions are swapped, and we want to make
865 sure that any other errors are detected before this happens. */
866 if (a_pipe == PIPE_S
867 || b_pipe == PIPE_O
868 || (b_pipe == PIPE_O_OS && (enable_m32rx != 2)))
869 return _("Instructions share the same execution pipeline");
870
871 return NULL;
872 }
873
874 /* Force the top bit of the second 16-bit insn to be set. */
875
876 static void
877 make_parallel (CGEN_INSN_BYTES_PTR buffer)
878 {
879 #if CGEN_INT_INSN_P
880 *buffer |= 0x8000;
881 #else
882 buffer[CGEN_CPU_ENDIAN (gas_cgen_cpu_desc) == CGEN_ENDIAN_BIG ? 0 : 1]
883 |= 0x80;
884 #endif
885 }
886
887 /* Same as make_parallel except buffer contains the bytes in target order. */
888
889 static void
890 target_make_parallel (char *buffer)
891 {
892 buffer[CGEN_CPU_ENDIAN (gas_cgen_cpu_desc) == CGEN_ENDIAN_BIG ? 0 : 1]
893 |= 0x80;
894 }
895
896 /* Assemble two instructions with an explicit parallel operation (||) or
897 sequential operation (->). */
898
899 static void
900 assemble_two_insns (char *str1, char *str2, int parallel_p)
901 {
902 char *str3;
903 m32r_insn first;
904 m32r_insn second;
905 char *errmsg;
906 char save_str2 = *str2;
907
908 /* Separate the two instructions. */
909 *str2 = 0;
910
911 /* Make sure the two insns begin on a 32 bit boundary.
912 This is also done for the serial case (foo -> bar), relaxing doesn't
913 affect insns written like this.
914 Note that we must always do this as we can't assume anything about
915 whether we're currently on a 32 bit boundary or not. Relaxing may
916 change this. */
917 fill_insn (0);
918
919 first.debug_sym_link = debug_sym_link;
920 debug_sym_link = (sym_linkS *) 0;
921
922 /* Parse the first instruction. */
923 if (! (first.insn = m32r_cgen_assemble_insn
924 (gas_cgen_cpu_desc, str1, & first.fields, first.buffer, & errmsg)))
925 {
926 as_bad ("%s", errmsg);
927 return;
928 }
929
930 /* Check it. */
931 if (CGEN_FIELDS_BITSIZE (&first.fields) != 16)
932 {
933 /* xgettext:c-format */
934 as_bad (_("not a 16 bit instruction '%s'"), str1);
935 return;
936 }
937 #ifdef E_M32R2_ARCH
938 else if ((enable_m32rx == 1)
939 /* FIXME: Need standard macro to perform this test. */
940 && ((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
941 & (1 << MACH_M32R2))
942 && !((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
943 & (1 << MACH_M32RX)))))
944 {
945 /* xgettext:c-format */
946 as_bad (_("instruction '%s' is for the M32R2 only"), str1);
947 return;
948 }
949 else if ((! enable_special
950 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL))
951 || (! enable_special_m32r
952 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_M32R)))
953 #else
954 else if (! enable_special
955 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL))
956 #endif
957 {
958 /* xgettext:c-format */
959 as_bad (_("unknown instruction '%s'"), str1);
960 return;
961 }
962 else if (! enable_m32rx
963 /* FIXME: Need standard macro to perform this test. */
964 && (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
965 == (1 << MACH_M32RX)))
966 {
967 /* xgettext:c-format */
968 as_bad (_("instruction '%s' is for the M32RX only"), str1);
969 return;
970 }
971
972 /* Check to see if this is an allowable parallel insn. */
973 if (parallel_p
974 && CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_PIPE) == PIPE_NONE)
975 {
976 /* xgettext:c-format */
977 as_bad (_("instruction '%s' cannot be executed in parallel."), str1);
978 return;
979 }
980
981 /* Restore the original assembly text, just in case it is needed. */
982 *str2 = save_str2;
983
984 /* Save the original string pointer. */
985 str3 = str1;
986
987 /* Advanced past the parsed string. */
988 str1 = str2 + 2;
989
990 /* Remember the entire string in case it is needed for error
991 messages. */
992 str2 = str3;
993
994 /* Convert the opcode to lower case. */
995 {
996 char *s2 = str1;
997
998 while (ISSPACE (*s2++))
999 continue;
1000
1001 --s2;
1002
1003 while (ISALNUM (*s2))
1004 {
1005 *s2 = TOLOWER (*s2);
1006 s2++;
1007 }
1008 }
1009
1010 /* Preserve any fixups that have been generated and reset the list
1011 to empty. */
1012 gas_cgen_save_fixups (0);
1013
1014 /* Get the indices of the operands of the instruction. */
1015 /* FIXME: CGEN_FIELDS is already recorded, but relying on that fact
1016 doesn't seem right. Perhaps allow passing fields like we do insn. */
1017 /* FIXME: ALIAS insns do not have operands, so we use this function
1018 to find the equivalent insn and overwrite the value stored in our
1019 structure. We still need the original insn, however, since this
1020 may have certain attributes that are not present in the unaliased
1021 version (eg relaxability). When aliases behave differently this
1022 may have to change. */
1023 first.orig_insn = first.insn;
1024 {
1025 CGEN_FIELDS tmp_fields;
1026 first.insn = cgen_lookup_get_insn_operands
1027 (gas_cgen_cpu_desc, NULL, INSN_VALUE (first.buffer), NULL, 16,
1028 first.indices, &tmp_fields);
1029 }
1030
1031 if (first.insn == NULL)
1032 as_fatal (_("internal error: lookup/get operands failed"));
1033
1034 second.debug_sym_link = NULL;
1035
1036 /* Parse the second instruction. */
1037 if (! (second.insn = m32r_cgen_assemble_insn
1038 (gas_cgen_cpu_desc, str1, & second.fields, second.buffer, & errmsg)))
1039 {
1040 as_bad ("%s", errmsg);
1041 return;
1042 }
1043
1044 /* Check it. */
1045 if (CGEN_FIELDS_BITSIZE (&second.fields) != 16)
1046 {
1047 /* xgettext:c-format */
1048 as_bad (_("not a 16 bit instruction '%s'"), str1);
1049 return;
1050 }
1051 #ifdef E_M32R2_ARCH
1052 else if ((enable_m32rx == 1)
1053 /* FIXME: Need standard macro to perform this test. */
1054 && ((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
1055 & (1 << MACH_M32R2))
1056 && !((CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_MACH)
1057 & (1 << MACH_M32RX)))))
1058 {
1059 /* xgettext:c-format */
1060 as_bad (_("instruction '%s' is for the M32R2 only"), str1);
1061 return;
1062 }
1063 else if ((! enable_special
1064 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1065 || (! enable_special_m32r
1066 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_M32R)))
1067 #else
1068 else if (! enable_special
1069 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1070 #endif
1071 {
1072 /* xgettext:c-format */
1073 as_bad (_("unknown instruction '%s'"), str1);
1074 return;
1075 }
1076 else if (! enable_m32rx
1077 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
1078 {
1079 /* xgettext:c-format */
1080 as_bad (_("instruction '%s' is for the M32RX only"), str1);
1081 return;
1082 }
1083
1084 /* Check to see if this is an allowable parallel insn. */
1085 if (parallel_p
1086 && CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_PIPE) == PIPE_NONE)
1087 {
1088 /* xgettext:c-format */
1089 as_bad (_("instruction '%s' cannot be executed in parallel."), str1);
1090 return;
1091 }
1092
1093 if (parallel_p && ! enable_m32rx)
1094 {
1095 if (CGEN_INSN_NUM (first.insn) != M32R_INSN_NOP
1096 && CGEN_INSN_NUM (second.insn) != M32R_INSN_NOP)
1097 {
1098 /* xgettext:c-format */
1099 as_bad (_("'%s': only the NOP instruction can be issued in parallel on the m32r"), str2);
1100 return;
1101 }
1102 }
1103
1104 /* Get the indices of the operands of the instruction. */
1105 second.orig_insn = second.insn;
1106 {
1107 CGEN_FIELDS tmp_fields;
1108 second.insn = cgen_lookup_get_insn_operands
1109 (gas_cgen_cpu_desc, NULL, INSN_VALUE (second.buffer), NULL, 16,
1110 second.indices, &tmp_fields);
1111 }
1112
1113 if (second.insn == NULL)
1114 as_fatal (_("internal error: lookup/get operands failed"));
1115
1116 /* We assume that if the first instruction writes to a register that is
1117 read by the second instruction it is because the programmer intended
1118 this to happen, (after all they have explicitly requested that these
1119 two instructions be executed in parallel). Although if the global
1120 variable warn_explicit_parallel_conflicts is true then we do generate
1121 a warning message. Similarly we assume that parallel branch and jump
1122 instructions are deliberate and should not produce errors. */
1123
1124 if (parallel_p && warn_explicit_parallel_conflicts)
1125 {
1126 if (first_writes_to_seconds_operands (&first, &second, FALSE))
1127 /* xgettext:c-format */
1128 as_warn (_("%s: output of 1st instruction is the same as an input to 2nd instruction - is this intentional ?"), str2);
1129
1130 if (first_writes_to_seconds_operands (&second, &first, FALSE))
1131 /* xgettext:c-format */
1132 as_warn (_("%s: output of 2nd instruction is the same as an input to 1st instruction - is this intentional ?"), str2);
1133 }
1134
1135 if (!parallel_p
1136 || (errmsg = (char *) can_make_parallel (&first, &second)) == NULL)
1137 {
1138 /* Get the fixups for the first instruction. */
1139 gas_cgen_swap_fixups (0);
1140
1141 /* Write it out. */
1142 expand_debug_syms (first.debug_sym_link, 1);
1143 gas_cgen_finish_insn (first.orig_insn, first.buffer,
1144 CGEN_FIELDS_BITSIZE (&first.fields), 0, NULL);
1145
1146 /* Force the top bit of the second insn to be set. */
1147 if (parallel_p)
1148 make_parallel (second.buffer);
1149
1150 /* Get its fixups. */
1151 gas_cgen_restore_fixups (0);
1152
1153 /* Write it out. */
1154 expand_debug_syms (second.debug_sym_link, 1);
1155 gas_cgen_finish_insn (second.orig_insn, second.buffer,
1156 CGEN_FIELDS_BITSIZE (&second.fields), 0, NULL);
1157 }
1158 /* Try swapping the instructions to see if they work that way. */
1159 else if (can_make_parallel (&second, &first) == NULL)
1160 {
1161 /* Write out the second instruction first. */
1162 expand_debug_syms (second.debug_sym_link, 1);
1163 gas_cgen_finish_insn (second.orig_insn, second.buffer,
1164 CGEN_FIELDS_BITSIZE (&second.fields), 0, NULL);
1165
1166 /* Force the top bit of the first instruction to be set. */
1167 make_parallel (first.buffer);
1168
1169 /* Get the fixups for the first instruction. */
1170 gas_cgen_restore_fixups (0);
1171
1172 /* Write out the first instruction. */
1173 expand_debug_syms (first.debug_sym_link, 1);
1174 gas_cgen_finish_insn (first.orig_insn, first.buffer,
1175 CGEN_FIELDS_BITSIZE (&first.fields), 0, NULL);
1176 }
1177 else
1178 {
1179 as_bad ("'%s': %s", str2, errmsg);
1180 return;
1181 }
1182
1183 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL)
1184 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL))
1185 m32r_flags |= E_M32R_HAS_HIDDEN_INST;
1186 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_M32R)
1187 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_M32R))
1188 m32r_flags |= E_M32R_HAS_BIT_INST;
1189 if (CGEN_INSN_ATTR_VALUE (first.insn, CGEN_INSN_SPECIAL_FLOAT)
1190 || CGEN_INSN_ATTR_VALUE (second.insn, CGEN_INSN_SPECIAL_FLOAT))
1191 m32r_flags |= E_M32R_HAS_FLOAT_INST;
1192
1193 /* Set these so m32r_fill_insn can use them. */
1194 prev_seg = now_seg;
1195 prev_subseg = now_subseg;
1196 }
1197
1198 void
1199 md_assemble (char *str)
1200 {
1201 m32r_insn insn;
1202 char *errmsg;
1203 char *str2 = NULL;
1204
1205 /* Initialize GAS's cgen interface for a new instruction. */
1206 gas_cgen_init_parse ();
1207
1208 /* Look for a parallel instruction separator. */
1209 if ((str2 = strstr (str, "||")) != NULL)
1210 {
1211 assemble_two_insns (str, str2, 1);
1212 m32r_flags |= E_M32R_HAS_PARALLEL;
1213 return;
1214 }
1215
1216 /* Also look for a sequential instruction separator. */
1217 if ((str2 = strstr (str, "->")) != NULL)
1218 {
1219 assemble_two_insns (str, str2, 0);
1220 return;
1221 }
1222
1223 insn.debug_sym_link = debug_sym_link;
1224 debug_sym_link = (sym_linkS *) 0;
1225
1226 insn.insn = m32r_cgen_assemble_insn
1227 (gas_cgen_cpu_desc, str, &insn.fields, insn.buffer, & errmsg);
1228
1229 if (!insn.insn)
1230 {
1231 as_bad ("%s", errmsg);
1232 return;
1233 }
1234
1235 #ifdef E_M32R2_ARCH
1236 if ((enable_m32rx == 1)
1237 /* FIXME: Need standard macro to perform this test. */
1238 && ((CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH)
1239 & (1 << MACH_M32R2))
1240 && !((CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH)
1241 & (1 << MACH_M32RX)))))
1242 {
1243 /* xgettext:c-format */
1244 as_bad (_("instruction '%s' is for the M32R2 only"), str);
1245 return;
1246 }
1247 else if ((! enable_special
1248 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1249 || (! enable_special_m32r
1250 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_M32R)))
1251 #else
1252 if (! enable_special
1253 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1254 #endif
1255 {
1256 /* xgettext:c-format */
1257 as_bad (_("unknown instruction '%s'"), str);
1258 return;
1259 }
1260 else if (! enable_m32rx
1261 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
1262 {
1263 /* xgettext:c-format */
1264 as_bad (_("instruction '%s' is for the M32RX only"), str);
1265 return;
1266 }
1267
1268 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL))
1269 m32r_flags |= E_M32R_HAS_HIDDEN_INST;
1270 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_M32R))
1271 m32r_flags |= E_M32R_HAS_BIT_INST;
1272 if (CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_SPECIAL_FLOAT))
1273 m32r_flags |= E_M32R_HAS_FLOAT_INST;
1274
1275 if (CGEN_INSN_BITSIZE (insn.insn) == 32)
1276 {
1277 /* 32 bit insns must live on 32 bit boundaries. */
1278 if (prev_insn.insn || seen_relaxable_p)
1279 {
1280 /* ??? If calling fill_insn too many times turns us into a memory
1281 pig, can we call a fn to assemble a nop instead of
1282 !seen_relaxable_p? */
1283 fill_insn (0);
1284 }
1285
1286 expand_debug_syms (insn.debug_sym_link, 2);
1287
1288 /* Doesn't really matter what we pass for RELAX_P here. */
1289 gas_cgen_finish_insn (insn.insn, insn.buffer,
1290 CGEN_FIELDS_BITSIZE (&insn.fields), 1, NULL);
1291 }
1292 else
1293 {
1294 int on_32bit_boundary_p;
1295 int swap = FALSE;
1296
1297 if (CGEN_INSN_BITSIZE (insn.insn) != 16)
1298 abort ();
1299
1300 insn.orig_insn = insn.insn;
1301
1302 /* If the previous insn was relaxable, then it may be expanded
1303 to fill the current 16 bit slot. Emit a NOP here to occupy
1304 this slot, so that we can start at optimizing at a 32 bit
1305 boundary. */
1306 if (prev_insn.insn && seen_relaxable_p && optimize)
1307 fill_insn (0);
1308
1309 if (enable_m32rx)
1310 {
1311 /* Get the indices of the operands of the instruction.
1312 FIXME: See assemble_parallel for notes on orig_insn. */
1313 {
1314 CGEN_FIELDS tmp_fields;
1315 insn.insn = cgen_lookup_get_insn_operands
1316 (gas_cgen_cpu_desc, NULL, INSN_VALUE (insn.buffer), NULL,
1317 16, insn.indices, &tmp_fields);
1318 }
1319
1320 if (insn.insn == NULL)
1321 as_fatal (_("internal error: lookup/get operands failed"));
1322 }
1323
1324 /* Compute whether we're on a 32 bit boundary or not.
1325 prev_insn.insn is NULL when we're on a 32 bit boundary. */
1326 on_32bit_boundary_p = prev_insn.insn == NULL;
1327
1328 /* Change a frag to, if each insn to swap is in a different frag.
1329 It must keep only one instruction in a frag. */
1330 if (parallel() && on_32bit_boundary_p)
1331 {
1332 frag_wane (frag_now);
1333 frag_new (0);
1334 }
1335
1336 /* Look to see if this instruction can be combined with the
1337 previous instruction to make one, parallel, 32 bit instruction.
1338 If the previous instruction (potentially) changed the flow of
1339 program control, then it cannot be combined with the current
1340 instruction. If the current instruction is relaxable, then it
1341 might be replaced with a longer version, so we cannot combine it.
1342 Also if the output of the previous instruction is used as an
1343 input to the current instruction then it cannot be combined.
1344 Otherwise call can_make_parallel() with both orderings of the
1345 instructions to see if they can be combined. */
1346 if (! on_32bit_boundary_p
1347 && parallel ()
1348 && CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_RELAXABLE) == 0
1349 && ! writes_to_pc (&prev_insn)
1350 && ! first_writes_to_seconds_operands (&prev_insn, &insn, FALSE))
1351 {
1352 if (can_make_parallel (&prev_insn, &insn) == NULL)
1353 make_parallel (insn.buffer);
1354 else if (can_make_parallel (&insn, &prev_insn) == NULL)
1355 swap = TRUE;
1356 }
1357
1358 expand_debug_syms (insn.debug_sym_link, 1);
1359
1360 {
1361 int i;
1362 finished_insnS fi;
1363
1364 /* Ensure each pair of 16 bit insns is in the same frag. */
1365 frag_grow (4);
1366
1367 gas_cgen_finish_insn (insn.orig_insn, insn.buffer,
1368 CGEN_FIELDS_BITSIZE (&insn.fields),
1369 1 /* relax_p */, &fi);
1370 insn.addr = fi.addr;
1371 insn.frag = fi.frag;
1372 insn.num_fixups = fi.num_fixups;
1373 for (i = 0; i < fi.num_fixups; ++i)
1374 insn.fixups[i] = fi.fixups[i];
1375 }
1376
1377 if (swap)
1378 {
1379 int i, tmp;
1380
1381 #define SWAP_BYTES(a,b) tmp = a; a = b; b = tmp
1382
1383 /* Swap the two insns */
1384 SWAP_BYTES (prev_insn.addr[0], insn.addr[0]);
1385 SWAP_BYTES (prev_insn.addr[1], insn.addr[1]);
1386
1387 target_make_parallel (insn.addr);
1388
1389 /* Swap any relaxable frags recorded for the two insns. */
1390 /* FIXME: Clarify. relaxation precludes parallel insns */
1391 if (prev_insn.frag->fr_opcode == prev_insn.addr)
1392 prev_insn.frag->fr_opcode = insn.addr;
1393 else if (insn.frag->fr_opcode == insn.addr)
1394 insn.frag->fr_opcode = prev_insn.addr;
1395
1396 /* Change a frag to, if each insn is in a different frag.
1397 It must keep only one instruction in a frag. */
1398 if (prev_insn.frag != insn.frag)
1399 {
1400 for (i = 0; i < prev_insn.num_fixups; ++i)
1401 prev_insn.fixups[i]->fx_frag = insn.frag;
1402 for (i = 0; i < insn.num_fixups; ++i)
1403 insn.fixups[i]->fx_frag = prev_insn.frag;
1404 }
1405 else
1406 {
1407 /* Update the addresses in any fixups.
1408 Note that we don't have to handle the case where each insn is in
1409 a different frag as we ensure they're in the same frag above. */
1410 for (i = 0; i < prev_insn.num_fixups; ++i)
1411 prev_insn.fixups[i]->fx_where += 2;
1412 for (i = 0; i < insn.num_fixups; ++i)
1413 insn.fixups[i]->fx_where -= 2;
1414 }
1415 }
1416
1417 /* Keep track of whether we've seen a pair of 16 bit insns.
1418 prev_insn.insn is NULL when we're on a 32 bit boundary. */
1419 if (on_32bit_boundary_p)
1420 prev_insn = insn;
1421 else
1422 prev_insn.insn = NULL;
1423
1424 /* If the insn needs the following one to be on a 32 bit boundary
1425 (e.g. subroutine calls), fill this insn's slot. */
1426 if (on_32bit_boundary_p
1427 && CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_FILL_SLOT) != 0)
1428 fill_insn (0);
1429
1430 /* If this is a relaxable insn (can be replaced with a larger version)
1431 mark the fact so that we can emit an alignment directive for a
1432 following 32 bit insn if we see one. */
1433 if (CGEN_INSN_ATTR_VALUE (insn.orig_insn, CGEN_INSN_RELAXABLE) != 0)
1434 seen_relaxable_p = 1;
1435 }
1436
1437 /* Set these so m32r_fill_insn can use them. */
1438 prev_seg = now_seg;
1439 prev_subseg = now_subseg;
1440 }
1441
1442 /* The syntax in the manual says constants begin with '#'.
1443 We just ignore it. */
1444
1445 void
1446 md_operand (expressionS *expressionP)
1447 {
1448 if (*input_line_pointer == '#')
1449 {
1450 input_line_pointer++;
1451 expression (expressionP);
1452 }
1453 }
1454
1455 valueT
1456 md_section_align (segT segment, valueT size)
1457 {
1458 int align = bfd_get_section_alignment (stdoutput, segment);
1459
1460 return ((size + (1 << align) - 1) & (-1 << align));
1461 }
1462
1463 symbolS *
1464 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1465 {
1466 return 0;
1467 }
1468
1469 /* .scomm pseudo-op handler.
1471
1472 This is a new pseudo-op to handle putting objects in .scommon.
1473 By doing this the linker won't need to do any work,
1474 and more importantly it removes the implicit -G arg necessary to
1475 correctly link the object file. */
1476
1477 static void
1478 m32r_scomm (int ignore ATTRIBUTE_UNUSED)
1479 {
1480 char *name;
1481 char c;
1482 char *p;
1483 offsetT size;
1484 symbolS *symbolP;
1485 offsetT align;
1486 int align2;
1487
1488 name = input_line_pointer;
1489 c = get_symbol_end ();
1490
1491 /* Just after name is now '\0'. */
1492 p = input_line_pointer;
1493 *p = c;
1494 SKIP_WHITESPACE ();
1495 if (*input_line_pointer != ',')
1496 {
1497 as_bad (_("Expected comma after symbol-name: rest of line ignored."));
1498 ignore_rest_of_line ();
1499 return;
1500 }
1501
1502 /* Skip ','. */
1503 input_line_pointer++;
1504 if ((size = get_absolute_expression ()) < 0)
1505 {
1506 /* xgettext:c-format */
1507 as_warn (_(".SCOMMon length (%ld.) <0! Ignored."), (long) size);
1508 ignore_rest_of_line ();
1509 return;
1510 }
1511
1512 /* The third argument to .scomm is the alignment. */
1513 if (*input_line_pointer != ',')
1514 align = 8;
1515 else
1516 {
1517 ++input_line_pointer;
1518 align = get_absolute_expression ();
1519 if (align <= 0)
1520 {
1521 as_warn (_("ignoring bad alignment"));
1522 align = 8;
1523 }
1524 }
1525
1526 /* Convert to a power of 2 alignment. */
1527 if (align)
1528 {
1529 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
1530 continue;
1531 if (align != 1)
1532 {
1533 as_bad (_("Common alignment not a power of 2"));
1534 ignore_rest_of_line ();
1535 return;
1536 }
1537 }
1538 else
1539 align2 = 0;
1540
1541 *p = 0;
1542 symbolP = symbol_find_or_make (name);
1543 *p = c;
1544
1545 if (S_IS_DEFINED (symbolP))
1546 {
1547 /* xgettext:c-format */
1548 as_bad (_("Ignoring attempt to re-define symbol `%s'."),
1549 S_GET_NAME (symbolP));
1550 ignore_rest_of_line ();
1551 return;
1552 }
1553
1554 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1555 {
1556 /* xgettext:c-format */
1557 as_bad (_("Length of .scomm \"%s\" is already %ld. Not changed to %ld."),
1558 S_GET_NAME (symbolP),
1559 (long) S_GET_VALUE (symbolP),
1560 (long) size);
1561
1562 ignore_rest_of_line ();
1563 return;
1564 }
1565
1566 if (symbol_get_obj (symbolP)->local)
1567 {
1568 segT old_sec = now_seg;
1569 int old_subsec = now_subseg;
1570 char *pfrag;
1571
1572 record_alignment (sbss_section, align2);
1573 subseg_set (sbss_section, 0);
1574
1575 if (align2)
1576 frag_align (align2, 0, 0);
1577
1578 if (S_GET_SEGMENT (symbolP) == sbss_section)
1579 symbol_get_frag (symbolP)->fr_symbol = 0;
1580
1581 symbol_set_frag (symbolP, frag_now);
1582
1583 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1584 (char *) 0);
1585 *pfrag = 0;
1586 S_SET_SIZE (symbolP, size);
1587 S_SET_SEGMENT (symbolP, sbss_section);
1588 S_CLEAR_EXTERNAL (symbolP);
1589 subseg_set (old_sec, old_subsec);
1590 }
1591 else
1592 {
1593 S_SET_VALUE (symbolP, (valueT) size);
1594 S_SET_ALIGN (symbolP, align2);
1595 S_SET_EXTERNAL (symbolP);
1596 S_SET_SEGMENT (symbolP, &scom_section);
1597 }
1598
1599 demand_empty_rest_of_line ();
1600 }
1601
1602 /* The target specific pseudo-ops which we support. */
1603 const pseudo_typeS md_pseudo_table[] =
1604 {
1605 { "word", cons, 4 },
1606 { "fillinsn", fill_insn, 0 },
1607 { "scomm", m32r_scomm, 0 },
1608 { "debugsym", debug_sym, 0 },
1609 { "m32r", allow_m32rx, 0 },
1610 { "m32rx", allow_m32rx, 1 },
1611 { "m32r2", allow_m32rx, 2 },
1612 { "little", little, 1 },
1613 { "big", little, 0 },
1614 { NULL, NULL, 0 }
1615 };
1616
1617 /* Interface to relax_segment. */
1619
1620 /* FIXME: Build table by hand, get it working, then machine generate. */
1621
1622 const relax_typeS md_relax_table[] =
1623 {
1624 /* The fields are:
1625 1) most positive reach of this state,
1626 2) most negative reach of this state,
1627 3) how many bytes this mode will add to the size of the current frag
1628 4) which index into the table to try if we can't fit into this one. */
1629
1630 /* The first entry must be unused because an `rlx_more' value of zero ends
1631 each list. */
1632 {1, 1, 0, 0},
1633
1634 /* The displacement used by GAS is from the end of the 2 byte insn,
1635 so we subtract 2 from the following. */
1636 /* 16 bit insn, 8 bit disp -> 10 bit range.
1637 This doesn't handle a branch in the right slot at the border:
1638 the "& -4" isn't taken into account. It's not important enough to
1639 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
1640 case). */
1641 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
1642 /* 32 bit insn, 24 bit disp -> 26 bit range. */
1643 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
1644 /* Same thing, but with leading nop for alignment. */
1645 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
1646 };
1647
1648 long
1649 m32r_relax_frag (segT segment, fragS *fragP, long stretch)
1650 {
1651 /* Address of branch insn. */
1652 long address = fragP->fr_address + fragP->fr_fix - 2;
1653 long growth = 0;
1654
1655 /* Keep 32 bit insns aligned on 32 bit boundaries. */
1656 if (fragP->fr_subtype == 2)
1657 {
1658 if ((address & 3) != 0)
1659 {
1660 fragP->fr_subtype = 3;
1661 growth = 2;
1662 }
1663 }
1664 else if (fragP->fr_subtype == 3)
1665 {
1666 if ((address & 3) == 0)
1667 {
1668 fragP->fr_subtype = 2;
1669 growth = -2;
1670 }
1671 }
1672 else
1673 {
1674 growth = relax_frag (segment, fragP, stretch);
1675
1676 /* Long jump on odd halfword boundary? */
1677 if (fragP->fr_subtype == 2 && (address & 3) != 0)
1678 {
1679 fragP->fr_subtype = 3;
1680 growth += 2;
1681 }
1682 }
1683
1684 return growth;
1685 }
1686
1687 /* Return an initial guess of the length by which a fragment must grow to
1688 hold a branch to reach its destination.
1689 Also updates fr_type/fr_subtype as necessary.
1690
1691 Called just before doing relaxation.
1692 Any symbol that is now undefined will not become defined.
1693 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
1694 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
1695 Although it may not be explicit in the frag, pretend fr_var starts
1696 with a 0 value. */
1697
1698 int
1699 md_estimate_size_before_relax (fragS *fragP, segT segment)
1700 {
1701 /* The only thing we have to handle here are symbols outside of the
1702 current segment. They may be undefined or in a different segment in
1703 which case linker scripts may place them anywhere.
1704 However, we can't finish the fragment here and emit the reloc as insn
1705 alignment requirements may move the insn about. */
1706 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
1707 || S_IS_EXTERNAL (fragP->fr_symbol)
1708 || S_IS_WEAK (fragP->fr_symbol))
1709 {
1710 /* The symbol is undefined in this segment.
1711 Change the relaxation subtype to the max allowable and leave
1712 all further handling to md_convert_frag. */
1713 fragP->fr_subtype = 2;
1714
1715 {
1716 const CGEN_INSN *insn;
1717 int i;
1718
1719 /* Update the recorded insn.
1720 Fortunately we don't have to look very far.
1721 FIXME: Change this to record in the instruction the next higher
1722 relaxable insn to use. */
1723 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
1724 {
1725 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
1726 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
1727 == 0)
1728 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
1729 break;
1730 }
1731 if (i == 4)
1732 abort ();
1733
1734 fragP->fr_cgen.insn = insn;
1735 return 2;
1736 }
1737 }
1738
1739 return md_relax_table[fragP->fr_subtype].rlx_length;
1740 }
1741
1742 /* *FRAGP has been relaxed to its final size, and now needs to have
1743 the bytes inside it modified to conform to the new size.
1744
1745 Called after relaxation is finished.
1746 fragP->fr_type == rs_machine_dependent.
1747 fragP->fr_subtype is the subtype of what the address relaxed to. */
1748
1749 void
1750 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
1751 segT sec,
1752 fragS *fragP)
1753 {
1754 char *opcode;
1755 char *displacement;
1756 int target_address;
1757 int opcode_address;
1758 int extension;
1759 int addend;
1760
1761 opcode = fragP->fr_opcode;
1762
1763 /* Address opcode resides at in file space. */
1764 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
1765
1766 switch (fragP->fr_subtype)
1767 {
1768 case 1:
1769 extension = 0;
1770 displacement = &opcode[1];
1771 break;
1772 case 2:
1773 opcode[0] |= 0x80;
1774 extension = 2;
1775 displacement = &opcode[1];
1776 break;
1777 case 3:
1778 opcode[2] = opcode[0] | 0x80;
1779 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
1780 opcode_address += 2;
1781 extension = 4;
1782 displacement = &opcode[3];
1783 break;
1784 default:
1785 abort ();
1786 }
1787
1788 if (S_GET_SEGMENT (fragP->fr_symbol) != sec
1789 || S_IS_EXTERNAL (fragP->fr_symbol)
1790 || S_IS_WEAK (fragP->fr_symbol))
1791 {
1792 /* Symbol must be resolved by linker. */
1793 if (fragP->fr_offset & 3)
1794 as_warn (_("Addend to unresolved symbol not on word boundary."));
1795 #ifdef USE_M32R_OLD_RELOC
1796 addend = fragP->fr_offset >> 2; /* Old M32R used USE_REL. */
1797 #else
1798 addend = 0;
1799 #endif
1800 }
1801 else
1802 {
1803 /* Address we want to reach in file space. */
1804 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1805 addend = (target_address - (opcode_address & -4)) >> 2;
1806 }
1807
1808 /* Create a relocation for symbols that must be resolved by the linker.
1809 Otherwise output the completed insn. */
1810
1811 if (S_GET_SEGMENT (fragP->fr_symbol) != sec
1812 || S_IS_EXTERNAL (fragP->fr_symbol)
1813 || S_IS_WEAK (fragP->fr_symbol))
1814 {
1815 fixS *fixP;
1816
1817 gas_assert (fragP->fr_subtype != 1);
1818 gas_assert (fragP->fr_cgen.insn != 0);
1819
1820 fixP = gas_cgen_record_fixup (fragP,
1821 /* Offset of branch insn in frag. */
1822 fragP->fr_fix + extension - 4,
1823 fragP->fr_cgen.insn,
1824 4 /* Length. */,
1825 /* FIXME: quick hack. */
1826 cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
1827 M32R_OPERAND_DISP24),
1828 fragP->fr_cgen.opinfo,
1829 fragP->fr_symbol, fragP->fr_offset);
1830 if (fragP->fr_cgen.opinfo)
1831 fixP->fx_r_type = fragP->fr_cgen.opinfo;
1832 }
1833
1834 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
1835
1836 md_number_to_chars (displacement, (valueT) addend,
1837 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
1838
1839 fragP->fr_fix += extension;
1840 }
1841
1842 /* Functions concerning relocs. */
1844
1845 /* The location from which a PC relative jump should be calculated,
1846 given a PC relative reloc. */
1847
1848 long
1849 md_pcrel_from_section (fixS *fixP, segT sec)
1850 {
1851 if (fixP->fx_addsy != (symbolS *) NULL
1852 && (! S_IS_DEFINED (fixP->fx_addsy)
1853 || S_GET_SEGMENT (fixP->fx_addsy) != sec
1854 || S_IS_EXTERNAL (fixP->fx_addsy)
1855 || S_IS_WEAK (fixP->fx_addsy)))
1856 {
1857 if (S_GET_SEGMENT (fixP->fx_addsy) != sec
1858 && S_IS_DEFINED (fixP->fx_addsy)
1859 && ! S_IS_EXTERNAL (fixP->fx_addsy)
1860 && ! S_IS_WEAK (fixP->fx_addsy))
1861 return fixP->fx_offset;
1862
1863 /* The symbol is undefined (or is defined but not in this section).
1864 Let the linker figure it out. */
1865 return 0;
1866 }
1867
1868 return (fixP->fx_frag->fr_address + fixP->fx_where) & -4L;
1869 }
1870
1871 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
1872 Returns BFD_RELOC_NONE if no reloc type can be found.
1873 *FIXP may be modified if desired. */
1874
1875 bfd_reloc_code_real_type
1876 md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
1877 const CGEN_OPERAND *operand,
1878 fixS *fixP)
1879 {
1880 switch (operand->type)
1881 {
1882 case M32R_OPERAND_DISP8: return BFD_RELOC_M32R_10_PCREL;
1883 case M32R_OPERAND_DISP16: return BFD_RELOC_M32R_18_PCREL;
1884 case M32R_OPERAND_DISP24: return BFD_RELOC_M32R_26_PCREL;
1885 case M32R_OPERAND_UIMM24: return BFD_RELOC_M32R_24;
1886 case M32R_OPERAND_HI16:
1887 case M32R_OPERAND_SLO16:
1888 case M32R_OPERAND_ULO16:
1889 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
1890 if (fixP->fx_cgen.opinfo != 0)
1891 return fixP->fx_cgen.opinfo;
1892 break;
1893 default:
1894 /* Avoid -Wall warning. */
1895 break;
1896 }
1897 return BFD_RELOC_NONE;
1898 }
1899
1900 /* Record a HI16 reloc for later matching with its LO16 cousin. */
1901
1902 static void
1903 m32r_record_hi16 (int reloc_type,
1904 fixS *fixP,
1905 segT seg ATTRIBUTE_UNUSED)
1906 {
1907 struct m32r_hi_fixup *hi_fixup;
1908
1909 gas_assert (reloc_type == BFD_RELOC_M32R_HI16_SLO
1910 || reloc_type == BFD_RELOC_M32R_HI16_ULO);
1911
1912 hi_fixup = xmalloc (sizeof (* hi_fixup));
1913 hi_fixup->fixp = fixP;
1914 hi_fixup->seg = now_seg;
1915 hi_fixup->next = m32r_hi_fixup_list;
1916
1917 m32r_hi_fixup_list = hi_fixup;
1918 }
1919
1920 /* Called while parsing an instruction to create a fixup.
1921 We need to check for HI16 relocs and queue them up for later sorting. */
1922
1923 fixS *
1924 m32r_cgen_record_fixup_exp (fragS *frag,
1925 int where,
1926 const CGEN_INSN *insn,
1927 int length,
1928 const CGEN_OPERAND *operand,
1929 int opinfo,
1930 expressionS *exp)
1931 {
1932 fixS *fixP;
1933 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
1934
1935 if (m32r_check_fixup (exp, &r_type))
1936 as_bad (_("Invalid PIC expression."));
1937
1938 fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
1939 operand, opinfo, exp);
1940
1941 switch (operand->type)
1942 {
1943 case M32R_OPERAND_HI16:
1944 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
1945 if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_SLO
1946 || fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_ULO)
1947 m32r_record_hi16 (fixP->fx_cgen.opinfo, fixP, now_seg);
1948 break;
1949
1950 default:
1951 /* Avoid -Wall warning. */
1952 break;
1953 }
1954
1955 switch (r_type)
1956 {
1957 case BFD_RELOC_UNUSED:
1958 default:
1959 return fixP;
1960
1961 case BFD_RELOC_M32R_GOTPC24:
1962 if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_SLO)
1963 r_type = BFD_RELOC_M32R_GOTPC_HI_SLO;
1964 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_ULO)
1965 r_type = BFD_RELOC_M32R_GOTPC_HI_ULO;
1966 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_LO16)
1967 r_type = BFD_RELOC_M32R_GOTPC_LO;
1968 break;
1969
1970 case BFD_RELOC_M32R_GOT24:
1971 if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_SLO)
1972 r_type = BFD_RELOC_M32R_GOT16_HI_SLO;
1973 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_ULO)
1974 r_type = BFD_RELOC_M32R_GOT16_HI_ULO;
1975 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_LO16)
1976 r_type = BFD_RELOC_M32R_GOT16_LO;
1977 break;
1978
1979 case BFD_RELOC_M32R_GOTOFF:
1980 if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_SLO)
1981 r_type = BFD_RELOC_M32R_GOTOFF_HI_SLO;
1982 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_HI16_ULO)
1983 r_type = BFD_RELOC_M32R_GOTOFF_HI_ULO;
1984 else if (fixP->fx_cgen.opinfo == BFD_RELOC_M32R_LO16)
1985 r_type = BFD_RELOC_M32R_GOTOFF_LO;
1986 break;
1987
1988 case BFD_RELOC_M32R_26_PLTREL:
1989 as_bad (_("Invalid PIC expression."));
1990 break;
1991 }
1992
1993 fixP->fx_r_type = r_type;
1994
1995 return fixP;
1996 }
1997
1998 /* Return BFD reloc type from opinfo field in a fixS.
1999 It's tricky using fx_r_type in m32r_frob_file because the values
2000 are BFD_RELOC_UNUSED + operand number. */
2001 #define FX_OPINFO_R_TYPE(f) ((f)->fx_cgen.opinfo)
2002
2003 /* Sort any unmatched HI16 relocs so that they immediately precede
2004 the corresponding LO16 reloc. This is called before md_apply_fix and
2005 tc_gen_reloc. */
2006
2007 void
2008 m32r_frob_file (void)
2009 {
2010 struct m32r_hi_fixup *l;
2011
2012 for (l = m32r_hi_fixup_list; l != NULL; l = l->next)
2013 {
2014 segment_info_type *seginfo;
2015 int pass;
2016
2017 gas_assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_SLO
2018 || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_ULO);
2019
2020 /* Check quickly whether the next fixup happens to be a matching low. */
2021 if (l->fixp->fx_next != NULL
2022 && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_M32R_LO16
2023 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
2024 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
2025 continue;
2026
2027 /* Look through the fixups for this segment for a matching `low'.
2028 When we find one, move the high/shigh just in front of it. We do
2029 this in two passes. In the first pass, we try to find a
2030 unique `low'. In the second pass, we permit multiple high's
2031 relocs for a single `low'. */
2032 seginfo = seg_info (l->seg);
2033 for (pass = 0; pass < 2; pass++)
2034 {
2035 fixS *f;
2036 fixS *prev;
2037
2038 prev = NULL;
2039 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
2040 {
2041 /* Check whether this is a `low' fixup which matches l->fixp. */
2042 if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_M32R_LO16
2043 && f->fx_addsy == l->fixp->fx_addsy
2044 && f->fx_offset == l->fixp->fx_offset
2045 && (pass == 1
2046 || prev == NULL
2047 || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_SLO
2048 && FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_ULO)
2049 || prev->fx_addsy != f->fx_addsy
2050 || prev->fx_offset != f->fx_offset))
2051 {
2052 fixS **pf;
2053
2054 /* Move l->fixp before f. */
2055 for (pf = &seginfo->fix_root;
2056 *pf != l->fixp;
2057 pf = & (*pf)->fx_next)
2058 gas_assert (*pf != NULL);
2059
2060 *pf = l->fixp->fx_next;
2061
2062 l->fixp->fx_next = f;
2063 if (prev == NULL)
2064 seginfo->fix_root = l->fixp;
2065 else
2066 prev->fx_next = l->fixp;
2067
2068 break;
2069 }
2070
2071 prev = f;
2072 }
2073
2074 if (f != NULL)
2075 break;
2076
2077 if (pass == 1
2078 && warn_unmatched_high)
2079 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
2080 _("Unmatched high/shigh reloc"));
2081 }
2082 }
2083 }
2084
2085 /* See whether we need to force a relocation into the output file.
2086 This is used to force out switch and PC relative relocations when
2087 relaxing. */
2088
2089 int
2090 m32r_force_relocation (fixS *fix)
2091 {
2092 if (generic_force_reloc (fix))
2093 return 1;
2094
2095 if (! m32r_relax)
2096 return 0;
2097
2098 return fix->fx_pcrel;
2099 }
2100
2101 /* Write a value out to the object file, using the appropriate endianness. */
2103
2104 void
2105 md_number_to_chars (char *buf, valueT val, int n)
2106 {
2107 if (target_big_endian)
2108 number_to_chars_bigendian (buf, val, n);
2109 else
2110 number_to_chars_littleendian (buf, val, n);
2111 }
2112
2113 /* Turn a string in input_line_pointer into a floating point constant
2114 of type TYPE, and store the appropriate bytes in *LITP. The number
2115 of LITTLENUMS emitted is stored in *SIZEP. An error message is
2116 returned, or NULL on OK. */
2117
2118 /* Equal to MAX_PRECISION in atof-ieee.c. */
2119 #define MAX_LITTLENUMS 6
2120
2121 char *
2122 md_atof (int type, char *litP, int *sizeP)
2123 {
2124 return ieee_md_atof (type, litP, sizeP, target_big_endian);
2125 }
2126
2127 void
2128 m32r_elf_section_change_hook (void)
2129 {
2130 /* If we have reached the end of a section and we have just emitted a
2131 16 bit insn, then emit a nop to make sure that the section ends on
2132 a 32 bit boundary. */
2133
2134 if (prev_insn.insn || seen_relaxable_p)
2135 (void) m32r_fill_insn (0);
2136 }
2137
2138 /* Return true if can adjust the reloc to be relative to its section
2139 (such as .data) instead of relative to some symbol. */
2140
2141 bfd_boolean
2142 m32r_fix_adjustable (fixS *fixP)
2143 {
2144 bfd_reloc_code_real_type reloc_type;
2145
2146 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2147 {
2148 const CGEN_INSN *insn = NULL;
2149 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2150 const CGEN_OPERAND *operand =
2151 cgen_operand_lookup_by_num(gas_cgen_cpu_desc, opindex);
2152
2153 reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
2154 }
2155 else
2156 reloc_type = fixP->fx_r_type;
2157
2158 if (fixP->fx_addsy == NULL)
2159 return 1;
2160
2161 /* Prevent all adjustments to global symbols. */
2162 if (S_IS_EXTERNAL (fixP->fx_addsy))
2163 return 0;
2164 if (S_IS_WEAK (fixP->fx_addsy))
2165 return 0;
2166
2167 if (pic_code
2168 && (reloc_type == BFD_RELOC_M32R_24
2169 || reloc_type == BFD_RELOC_M32R_26_PCREL
2170 || reloc_type == BFD_RELOC_M32R_HI16_SLO
2171 || reloc_type == BFD_RELOC_M32R_HI16_ULO
2172 || reloc_type == BFD_RELOC_M32R_LO16))
2173 return 0;
2174
2175 if (reloc_type == BFD_RELOC_M32R_GOT24
2176 || reloc_type == BFD_RELOC_M32R_26_PLTREL
2177 || reloc_type == BFD_RELOC_M32R_GOTPC_HI_SLO
2178 || reloc_type == BFD_RELOC_M32R_GOTPC_HI_ULO
2179 || reloc_type == BFD_RELOC_M32R_GOTPC_LO
2180 || reloc_type == BFD_RELOC_M32R_GOT16_HI_SLO
2181 || reloc_type == BFD_RELOC_M32R_GOT16_HI_ULO
2182 || reloc_type == BFD_RELOC_M32R_GOT16_LO)
2183 return 0;
2184
2185 /* We need the symbol name for the VTABLE entries. */
2186 if (reloc_type == BFD_RELOC_VTABLE_INHERIT
2187 || reloc_type == BFD_RELOC_VTABLE_ENTRY)
2188 return 0;
2189
2190 return 1;
2191 }
2192
2193 void
2194 m32r_elf_final_processing (void)
2195 {
2196 if (use_parallel)
2197 m32r_flags |= E_M32R_HAS_PARALLEL;
2198 elf_elfheader (stdoutput)->e_flags |= m32r_flags;
2199 }
2200
2201 /* Translate internal representation of relocation info to BFD target
2202 format. */
2203
2204 arelent *
2205 tc_gen_reloc (asection * section, fixS * fixP)
2206 {
2207 arelent * reloc;
2208 bfd_reloc_code_real_type code;
2209
2210 reloc = xmalloc (sizeof (* reloc));
2211
2212 reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
2213 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
2214 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
2215
2216 if (fixP->fx_pcrel)
2217 {
2218 if (fixP->fx_r_type == BFD_RELOC_32)
2219 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2220 else if (fixP->fx_r_type == BFD_RELOC_16)
2221 {
2222 fixP->fx_r_type = BFD_RELOC_16_PCREL;
2223 bfd_set_error (bfd_error_bad_value);
2224 }
2225 }
2226
2227 code = fixP->fx_r_type;
2228 if (pic_code)
2229 {
2230 #ifdef DEBUG_PIC
2231 printf("%s",bfd_get_reloc_code_name(code));
2232 #endif
2233 switch (code)
2234 {
2235 case BFD_RELOC_M32R_26_PCREL:
2236 code = BFD_RELOC_M32R_26_PLTREL;
2237 break;
2238
2239 case BFD_RELOC_M32R_24:
2240 if (fixP->fx_addsy != NULL
2241 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2242 code = BFD_RELOC_M32R_GOTPC24;
2243 else
2244 code = BFD_RELOC_M32R_GOT24;
2245 break;
2246
2247 case BFD_RELOC_M32R_HI16_ULO:
2248 if (fixP->fx_addsy != NULL
2249 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2250 code = BFD_RELOC_M32R_GOTPC_HI_ULO;
2251 else
2252 code = BFD_RELOC_M32R_GOT16_HI_ULO;
2253 break;
2254
2255 case BFD_RELOC_M32R_HI16_SLO:
2256 if (fixP->fx_addsy != NULL
2257 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2258 code = BFD_RELOC_M32R_GOTPC_HI_SLO;
2259 else
2260 code = BFD_RELOC_M32R_GOT16_HI_SLO;
2261 break;
2262
2263 case BFD_RELOC_M32R_LO16:
2264 if (fixP->fx_addsy != NULL
2265 && strcmp (S_GET_NAME (fixP->fx_addsy), GOT_NAME) == 0)
2266 code = BFD_RELOC_M32R_GOTPC_LO;
2267 else
2268 code = BFD_RELOC_M32R_GOT16_LO;
2269 break;
2270
2271 default:
2272 break;
2273 }
2274 #ifdef DEBUG_PIC
2275 printf(" => %s",bfd_get_reloc_code_name(code));
2276 #endif
2277 }
2278
2279 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2280
2281 #ifdef DEBUG_PIC
2282 printf(" => %s\n",reloc->howto->name);
2283 #endif
2284
2285 if (reloc->howto == (reloc_howto_type *) NULL)
2286 {
2287 as_bad_where (fixP->fx_file, fixP->fx_line,
2288 _("internal error: can't export reloc type %d (`%s')"),
2289 fixP->fx_r_type, bfd_get_reloc_code_name (code));
2290 return NULL;
2291 }
2292
2293 /* Use fx_offset for these cases. */
2294 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2295 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2296 || fixP->fx_r_type == BFD_RELOC_32_PCREL)
2297 reloc->addend = fixP->fx_offset;
2298 else if ((!pic_code
2299 && code != BFD_RELOC_M32R_26_PLTREL)
2300 && fixP->fx_pcrel
2301 && fixP->fx_addsy != NULL
2302 && (S_GET_SEGMENT(fixP->fx_addsy) != section)
2303 && S_IS_DEFINED (fixP->fx_addsy)
2304 && ! S_IS_EXTERNAL(fixP->fx_addsy)
2305 && ! S_IS_WEAK(fixP->fx_addsy))
2306 /* Already used fx_offset in the opcode field itseld. */
2307 reloc->addend = fixP->fx_offset;
2308 else
2309 reloc->addend = fixP->fx_addnumber;
2310
2311 return reloc;
2312 }
2313
2314 inline static char *
2315 m32r_end_of_match (char *cont, char *what)
2316 {
2317 int len = strlen (what);
2318
2319 if (strncasecmp (cont, what, strlen (what)) == 0
2320 && ! is_part_of_name (cont[len]))
2321 return cont + len;
2322
2323 return NULL;
2324 }
2325
2326 int
2327 m32r_parse_name (char const *name,
2328 expressionS *exprP,
2329 enum expr_mode mode,
2330 char *nextcharP)
2331 {
2332 char *next = input_line_pointer;
2333 char *next_end;
2334 int reloc_type;
2335 operatorT op_type;
2336 segT segment;
2337
2338 exprP->X_op_symbol = NULL;
2339 exprP->X_md = BFD_RELOC_UNUSED;
2340
2341 if (strcmp (name, GOT_NAME) == 0)
2342 {
2343 if (! GOT_symbol)
2344 GOT_symbol = symbol_find_or_make (name);
2345
2346 exprP->X_add_symbol = GOT_symbol;
2347 no_suffix:
2348 /* If we have an absolute symbol or a
2349 reg, then we know its value now. */
2350 segment = S_GET_SEGMENT (exprP->X_add_symbol);
2351 if (mode != expr_defer && segment == absolute_section)
2352 {
2353 exprP->X_op = O_constant;
2354 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
2355 exprP->X_add_symbol = NULL;
2356 }
2357 else if (mode != expr_defer && segment == reg_section)
2358 {
2359 exprP->X_op = O_register;
2360 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
2361 exprP->X_add_symbol = NULL;
2362 }
2363 else
2364 {
2365 exprP->X_op = O_symbol;
2366 exprP->X_add_number = 0;
2367 }
2368
2369 return 1;
2370 }
2371
2372 exprP->X_add_symbol = symbol_find_or_make (name);
2373
2374 if (*nextcharP != '@')
2375 goto no_suffix;
2376 else if ((next_end = m32r_end_of_match (next + 1, "GOTOFF")))
2377 {
2378 reloc_type = BFD_RELOC_M32R_GOTOFF;
2379 op_type = O_PIC_reloc;
2380 }
2381 else if ((next_end = m32r_end_of_match (next + 1, "GOT")))
2382 {
2383 reloc_type = BFD_RELOC_M32R_GOT24;
2384 op_type = O_PIC_reloc;
2385 }
2386 else if ((next_end = m32r_end_of_match (next + 1, "PLT")))
2387 {
2388 reloc_type = BFD_RELOC_M32R_26_PLTREL;
2389 op_type = O_PIC_reloc;
2390 }
2391 else
2392 goto no_suffix;
2393
2394 *input_line_pointer = *nextcharP;
2395 input_line_pointer = next_end;
2396 *nextcharP = *input_line_pointer;
2397 *input_line_pointer = '\0';
2398
2399 exprP->X_op = op_type;
2400 exprP->X_add_number = 0;
2401 exprP->X_md = reloc_type;
2402
2403 return 1;
2404 }
2405
2406 int
2407 m32r_cgen_parse_fix_exp(int opinfo, expressionS *exp)
2408 {
2409 if (exp->X_op == O_PIC_reloc
2410 && exp->X_md == BFD_RELOC_M32R_26_PLTREL)
2411 {
2412 exp->X_op = O_symbol;
2413 opinfo = exp->X_md;
2414 }
2415
2416 return opinfo;
2417 }
2418