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