Add support for --m32rx command line option.
[binutils-gdb.git] / gas / config / tc-m32r.c
1 /* tc-m32r.c -- Assembler for the Mitsubishi M32R/X.
2 Copyright (C) 1996, 1997 Free Software Foundation.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "cgen-opc.h"
26
27 /* Non-null if last insn was a 16 bit insn on a 32 bit boundary
28 (i.e. was the first of two 16 bit insns). */
29 static const struct cgen_insn *prev_insn = NULL;
30
31 /* Non-zero if we've seen a relaxable insn since the last 32 bit
32 alignment request. */
33 static int seen_relaxable_p = 0;
34
35 /* Non-zero if -relax specified, in which case sufficient relocs are output
36 for the linker to do relaxing.
37 We do simple forms of relaxing internally, but they are always done.
38 This flag does not apply to them. */
39 static int m32r_relax;
40
41 /* If non-NULL, pointer to cpu description file to read.
42 This allows runtime additions to the assembler. */
43 static char *m32r_cpu_desc;
44
45 /* Non-zero if -m32rx has been specified, in which case support for the
46 extended M32RX instruction set should be enabled. */
47 /* Indicates the target BFD machine number. */
48 static int enable_m32rx = 0;
49
50 /* stuff for .scomm symbols. */
51 static segT sbss_section;
52 static asection scom_section;
53 static asymbol scom_symbol;
54
55 const char comment_chars[] = ";";
56 const char line_comment_chars[] = "#";
57 const char line_separator_chars[] = "";
58 const char EXP_CHARS[] = "eE";
59 const char FLT_CHARS[] = "dD";
60
61 /* Relocations against symbols are done in two
62 parts, with a HI relocation and a LO relocation. Each relocation
63 has only 16 bits of space to store an addend. This means that in
64 order for the linker to handle carries correctly, it must be able
65 to locate both the HI and the LO relocation. This means that the
66 relocations must appear in order in the relocation table.
67
68 In order to implement this, we keep track of each unmatched HI
69 relocation. We then sort them so that they immediately precede the
70 corresponding LO relocation. */
71
72 struct m32r_hi_fixup
73 {
74 /* Next HI fixup. */
75 struct m32r_hi_fixup *next;
76 /* This fixup. */
77 fixS *fixp;
78 /* The section this fixup is in. */
79 segT seg;
80 };
81
82 /* The list of unmatched HI relocs. */
83
84 static struct m32r_hi_fixup *m32r_hi_fixup_list;
85
86 static void m32r_record_hi16 PARAMS ((int, fixS *, segT seg));
87
88 \f
89 static void
90 allow_m32rx (int on)
91 {
92 enable_m32rx = on;
93
94 if (stdoutput != NULL)
95 bfd_set_arch_mach (stdoutput, TARGET_ARCH, enable_m32rx ? bfd_mach_m32rx : bfd_mach_m32r);
96 }
97 \f
98 const char *md_shortopts = "";
99
100 struct option md_longopts[] =
101 {
102 #define OPTION_M32RX (OPTION_MD_BASE)
103 {"m32rx", no_argument, NULL, OPTION_M32RX},
104
105 #if 0 /* not supported yet */
106 #define OPTION_RELAX (OPTION_MD_BASE + 1)
107 {"relax", no_argument, NULL, OPTION_RELAX},
108 #define OPTION_CPU_DESC (OPTION_MD_BASE + 2)
109 {"cpu-desc", required_argument, NULL, OPTION_CPU_DESC},
110 #endif
111
112 {NULL, no_argument, NULL, 0}
113 };
114 size_t md_longopts_size = sizeof(md_longopts);
115
116 int
117 md_parse_option (c, arg)
118 int c;
119 char *arg;
120 {
121 switch (c)
122 {
123 case OPTION_M32RX:
124 allow_m32rx (1);
125 break;
126
127 #if 0 /* not supported yet */
128 case OPTION_RELAX:
129 m32r_relax = 1;
130 break;
131 case OPTION_CPU_DESC:
132 m32r_cpu_desc = arg;
133 break;
134 #endif
135 default:
136 return 0;
137 }
138 return 1;
139 }
140
141 void
142 md_show_usage (stream)
143 FILE *stream;
144 {
145 fprintf (stream, "M32R/X options:\n");
146 fprintf (stream, "\
147 --m32rx support the extended m32rx instruction set\n");
148
149 #if 0
150 fprintf (stream, "\
151 --relax create linker relaxable code\n");
152 fprintf (stream, "\
153 --cpu-desc provide runtime cpu description file\n");
154 #endif
155 }
156
157 static void fill_insn PARAMS ((int));
158 static void m32r_scomm PARAMS ((int));
159
160 /* Set by md_assemble for use by m32r_fill_insn. */
161 static subsegT prev_subseg;
162 static segT prev_seg;
163
164 /* The target specific pseudo-ops which we support. */
165 const pseudo_typeS md_pseudo_table[] =
166 {
167 { "word", cons, 4 },
168 { "fillinsn", fill_insn, 0 },
169 { "scomm", m32r_scomm, 0 },
170 { "m32r", allow_m32rx, 0},
171 { "m32rx", allow_m32rx, 1},
172 { NULL, NULL, 0 }
173 };
174
175 /* FIXME: Should be machine generated. */
176 #define NOP_INSN 0x7000
177 #define PAR_NOP_INSN 0xf000 /* can only be used in 2nd slot */
178
179 /* When we align the .text section, insert the correct NOP pattern.
180 N is the power of 2 alignment. LEN is the length of pattern FILL.
181 MAX is the maximum number of characters to skip when doing the alignment,
182 or 0 if there is no maximum. */
183
184 int
185 m32r_do_align (n, fill, len, max)
186 int n;
187 const char *fill;
188 int len;
189 int max;
190 {
191 if ((fill == NULL || (*fill == 0 && len == 1))
192 && (now_seg->flags & SEC_CODE) != 0
193 /* Only do this special handling if aligning to at least a
194 4 byte boundary. */
195 && n > 1
196 /* Only do this special handling if we're allowed to emit at
197 least two bytes. */
198 && (max == 0 || max > 1))
199 {
200 static const unsigned char nop_pattern[] = { 0xf0, 0x00 };
201
202 #if 0
203 /* First align to a 2 byte boundary, in case there is an odd .byte. */
204 /* FIXME: How much memory will cause gas to use when assembling a big
205 program? Perhaps we can avoid the frag_align call? */
206 frag_align (1, 0, 0);
207 #endif
208 /* Next align to a 4 byte boundary (we know n >= 2) using a parallel
209 nop. */
210 frag_align_pattern (2, nop_pattern, sizeof nop_pattern, 0);
211 /* If doing larger alignments use a repeating sequence of appropriate
212 nops. */
213 if (n > 2)
214 {
215 static const unsigned char multi_nop_pattern[] = { 0x70, 0x00, 0xf0, 0x00 };
216 frag_align_pattern (n, multi_nop_pattern, sizeof multi_nop_pattern,
217 max ? max - 2 : 0);
218 }
219 return 1;
220 }
221
222 return 0;
223 }
224
225 static void
226 assemble_nop (opcode)
227 int opcode;
228 {
229 char *f = frag_more (2);
230 md_number_to_chars (f, opcode, 2);
231 }
232
233 /* If the last instruction was the first of 2 16 bit insns,
234 output a nop to move the PC to a 32 bit boundary.
235
236 This is done via an alignment specification since branch relaxing
237 may make it unnecessary.
238
239 Internally, we need to output one of these each time a 32 bit insn is
240 seen after an insn that is relaxable. */
241
242 static void
243 fill_insn (ignore)
244 int ignore;
245 {
246 (void) m32r_do_align (2, NULL, 0, 0);
247 prev_insn = NULL;
248 seen_relaxable_p = 0;
249 }
250
251 /* Cover function to fill_insn called after a label and at end of assembly.
252
253 The result is always 1: we're called in a conditional to see if the
254 current line is a label. */
255
256 int
257 m32r_fill_insn (done)
258 int done;
259 {
260 segT seg;
261 subsegT subseg;
262
263 if (prev_seg != NULL)
264 {
265 seg = now_seg;
266 subseg = now_subseg;
267 subseg_set (prev_seg, prev_subseg);
268 fill_insn (0);
269 subseg_set (seg, subseg);
270 }
271 return 1;
272 }
273 \f
274 void
275 md_begin ()
276 {
277 flagword applicable;
278 segT seg;
279 subsegT subseg;
280
281 /* Initialize the `cgen' interface. */
282
283 /* This is a callback from cgen to gas to parse operands. */
284 cgen_parse_operand_fn = cgen_parse_operand;
285 /* Set the machine number and endian. */
286 CGEN_SYM (init_asm) (0 /* mach number */,
287 target_big_endian ? CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE);
288
289 #if 0 /* not supported yet */
290 /* If a runtime cpu description file was provided, parse it. */
291 if (m32r_cpu_desc != NULL)
292 {
293 const char *errmsg;
294
295 errmsg = cgen_read_cpu_file (m32r_cpu_desc);
296 if (errmsg != NULL)
297 as_bad ("%s: %s", m32r_cpu_desc, errmsg);
298 }
299 #endif
300
301 /* Save the current subseg so we can restore it [it's the default one and
302 we don't want the initial section to be .sbss. */
303 seg = now_seg;
304 subseg = now_subseg;
305
306 /* The sbss section is for local .scomm symbols. */
307 sbss_section = subseg_new (".sbss", 0);
308 /* This is copied from perform_an_assembly_pass. */
309 applicable = bfd_applicable_section_flags (stdoutput);
310 bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
311 #if 0 /* What does this do? [see perform_an_assembly_pass] */
312 seg_info (bss_section)->bss = 1;
313 #endif
314
315 subseg_set (seg, subseg);
316
317 /* We must construct a fake section similar to bfd_com_section
318 but with the name .scommon. */
319 scom_section = bfd_com_section;
320 scom_section.name = ".scommon";
321 scom_section.output_section = &scom_section;
322 scom_section.symbol = &scom_symbol;
323 scom_section.symbol_ptr_ptr = &scom_section.symbol;
324 scom_symbol = *bfd_com_section.symbol;
325 scom_symbol.name = ".scommon";
326 scom_symbol.section = &scom_section;
327
328 allow_m32rx (enable_m32rx);
329 }
330
331 void
332 md_assemble (str)
333 char *str;
334 {
335 #ifdef CGEN_INT_INSN
336 cgen_insn_t buffer[CGEN_MAX_INSN_SIZE / sizeof (cgen_insn_t)];
337 #else
338 char buffer[CGEN_MAX_INSN_SIZE];
339 #endif
340 struct cgen_fields fields;
341 const struct cgen_insn *insn;
342 char *errmsg;
343
344 /* Initialize GAS's cgen interface for a new instruction. */
345 cgen_asm_init_parse ();
346
347 insn = CGEN_SYM (assemble_insn) (str, &fields, buffer, &errmsg);
348 if (!insn)
349 {
350 as_bad (errmsg);
351 return;
352 }
353
354 if (CGEN_INSN_BITSIZE (insn) == 32)
355 {
356 /* 32 bit insns must live on 32 bit boundaries. */
357 /* FIXME: If calling fill_insn too many times turns us into a memory
358 pig, can we call assemble_nop instead of !seen_relaxable_p? */
359 if (prev_insn || seen_relaxable_p)
360 fill_insn (0);
361 cgen_asm_finish_insn (insn, buffer, CGEN_FIELDS_BITSIZE (&fields));
362 }
363 else
364 {
365 /* Keep track of whether we've seen a pair of 16 bit insns.
366 PREV_INSN is NULL when we're on a 32 bit boundary. */
367 if (prev_insn)
368 prev_insn = NULL;
369 else
370 prev_insn = insn;
371 cgen_asm_finish_insn (insn, buffer, CGEN_FIELDS_BITSIZE (&fields));
372
373 /* If the insn needs the following one to be on a 32 bit boundary
374 (e.g. subroutine calls), fill this insn's slot. */
375 if (prev_insn
376 && CGEN_INSN_ATTR (insn, CGEN_INSN_FILL_SLOT) != 0)
377 fill_insn (0);
378 }
379
380 /* If this is a relaxable insn (can be replaced with a larger version)
381 mark the fact so that we can emit an alignment directive for a following
382 32 bit insn if we see one. */
383 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0)
384 seen_relaxable_p = 1;
385
386 /* Set these so m32r_fill_insn can use them. */
387 prev_seg = now_seg;
388 prev_subseg = now_subseg;
389 }
390
391 /* The syntax in the manual says constants begin with '#'.
392 We just ignore it. */
393
394 void
395 md_operand (expressionP)
396 expressionS *expressionP;
397 {
398 if (*input_line_pointer == '#')
399 {
400 input_line_pointer++;
401 expression (expressionP);
402 }
403 }
404
405 valueT
406 md_section_align (segment, size)
407 segT segment;
408 valueT size;
409 {
410 int align = bfd_get_section_alignment (stdoutput, segment);
411 return ((size + (1 << align) - 1) & (-1 << align));
412 }
413
414 symbolS *
415 md_undefined_symbol (name)
416 char *name;
417 {
418 return 0;
419 }
420 \f
421 /* .scomm pseudo-op handler.
422
423 This is a new pseudo-op to handle putting objects in .scommon.
424 By doing this the linker won't need to do any work and more importantly
425 it removes the implicit -G arg necessary to correctly link the object file.
426 */
427
428 static void
429 m32r_scomm (ignore)
430 int ignore;
431 {
432 register char *name;
433 register char c;
434 register char *p;
435 offsetT size;
436 register symbolS *symbolP;
437 offsetT align;
438 int align2;
439
440 name = input_line_pointer;
441 c = get_symbol_end ();
442
443 /* just after name is now '\0' */
444 p = input_line_pointer;
445 *p = c;
446 SKIP_WHITESPACE ();
447 if (*input_line_pointer != ',')
448 {
449 as_bad ("Expected comma after symbol-name: rest of line ignored.");
450 ignore_rest_of_line ();
451 return;
452 }
453
454 input_line_pointer++; /* skip ',' */
455 if ((size = get_absolute_expression ()) < 0)
456 {
457 as_warn (".SCOMMon length (%ld.) <0! Ignored.", (long) size);
458 ignore_rest_of_line ();
459 return;
460 }
461
462 /* The third argument to .scomm is the alignment. */
463 if (*input_line_pointer != ',')
464 align = 8;
465 else
466 {
467 ++input_line_pointer;
468 align = get_absolute_expression ();
469 if (align <= 0)
470 {
471 as_warn ("ignoring bad alignment");
472 align = 8;
473 }
474 }
475 /* Convert to a power of 2 alignment. */
476 if (align)
477 {
478 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
479 continue;
480 if (align != 1)
481 {
482 as_bad ("Common alignment not a power of 2");
483 ignore_rest_of_line ();
484 return;
485 }
486 }
487 else
488 align2 = 0;
489
490 *p = 0;
491 symbolP = symbol_find_or_make (name);
492 *p = c;
493
494 if (S_IS_DEFINED (symbolP))
495 {
496 as_bad ("Ignoring attempt to re-define symbol `%s'.",
497 S_GET_NAME (symbolP));
498 ignore_rest_of_line ();
499 return;
500 }
501
502 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
503 {
504 as_bad ("Length of .scomm \"%s\" is already %ld. Not changed to %ld.",
505 S_GET_NAME (symbolP),
506 (long) S_GET_VALUE (symbolP),
507 (long) size);
508
509 ignore_rest_of_line ();
510 return;
511 }
512
513 if (symbolP->local)
514 {
515 segT old_sec = now_seg;
516 int old_subsec = now_subseg;
517 char *pfrag;
518
519 record_alignment (sbss_section, align2);
520 subseg_set (sbss_section, 0);
521 if (align2)
522 frag_align (align2, 0, 0);
523 if (S_GET_SEGMENT (symbolP) == sbss_section)
524 symbolP->sy_frag->fr_symbol = 0;
525 symbolP->sy_frag = frag_now;
526 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
527 (char *) 0);
528 *pfrag = 0;
529 S_SET_SIZE (symbolP, size);
530 S_SET_SEGMENT (symbolP, sbss_section);
531 S_CLEAR_EXTERNAL (symbolP);
532 subseg_set (old_sec, old_subsec);
533 }
534 else
535 {
536 S_SET_VALUE (symbolP, (valueT) size);
537 S_SET_ALIGN (symbolP, align2);
538 S_SET_EXTERNAL (symbolP);
539 S_SET_SEGMENT (symbolP, &scom_section);
540 }
541
542 demand_empty_rest_of_line ();
543 }
544 \f
545 /* Interface to relax_segment. */
546
547 /* FIXME: Build table by hand, get it working, then machine generate. */
548
549 const relax_typeS md_relax_table[] =
550 {
551 /* The fields are:
552 1) most positive reach of this state,
553 2) most negative reach of this state,
554 3) how many bytes this mode will add to the size of the current frag
555 4) which index into the table to try if we can't fit into this one. */
556
557 /* The first entry must be unused because an `rlx_more' value of zero ends
558 each list. */
559 {1, 1, 0, 0},
560
561 /* The displacement used by GAS is from the end of the 2 byte insn,
562 so we subtract 2 from the following. */
563 /* 16 bit insn, 8 bit disp -> 10 bit range.
564 This doesn't handle a branch in the right slot at the border:
565 the "& -4" isn't taken into account. It's not important enough to
566 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
567 case). */
568 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
569 /* 32 bit insn, 24 bit disp -> 26 bit range. */
570 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
571 /* Same thing, but with leading nop for alignment. */
572 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
573 };
574
575 long
576 m32r_relax_frag (fragP, stretch)
577 fragS *fragP;
578 long stretch;
579 {
580 /* Address of branch insn. */
581 long address = fragP->fr_address + fragP->fr_fix - 2;
582 long growth = 0;
583
584 /* Keep 32 bit insns aligned on 32 bit boundaries. */
585 if (fragP->fr_subtype == 2)
586 {
587 if ((address & 3) != 0)
588 {
589 fragP->fr_subtype = 3;
590 growth = 2;
591 }
592 }
593 else if (fragP->fr_subtype == 3)
594 {
595 if ((address & 3) == 0)
596 {
597 fragP->fr_subtype = 2;
598 growth = -2;
599 }
600 }
601 else
602 {
603 growth = relax_frag (fragP, stretch);
604
605 /* Long jump on odd halfword boundary? */
606 if (fragP->fr_subtype == 2 && (address & 3) != 0)
607 {
608 fragP->fr_subtype = 3;
609 growth += 2;
610 }
611 }
612
613 return growth;
614 }
615
616 /* Return an initial guess of the length by which a fragment must grow to
617 hold a branch to reach its destination.
618 Also updates fr_type/fr_subtype as necessary.
619
620 Called just before doing relaxation.
621 Any symbol that is now undefined will not become defined.
622 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
623 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
624 Although it may not be explicit in the frag, pretend fr_var starts with a
625 0 value. */
626
627 int
628 md_estimate_size_before_relax (fragP, segment)
629 fragS *fragP;
630 segT segment;
631 {
632 int old_fr_fix = fragP->fr_fix;
633 char *opcode = fragP->fr_opcode;
634
635 /* The only thing we have to handle here are symbols outside of the
636 current segment. They may be undefined or in a different segment in
637 which case linker scripts may place them anywhere.
638 However, we can't finish the fragment here and emit the reloc as insn
639 alignment requirements may move the insn about. */
640
641 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
642 {
643 /* The symbol is undefined in this segment.
644 Change the relaxation subtype to the max allowable and leave
645 all further handling to md_convert_frag. */
646 fragP->fr_subtype = 2;
647
648 #if 0 /* Can't use this, but leave in for illustration. */
649 /* Change 16 bit insn to 32 bit insn. */
650 opcode[0] |= 0x80;
651
652 /* Increase known (fixed) size of fragment. */
653 fragP->fr_fix += 2;
654
655 /* Create a relocation for it. */
656 fix_new (fragP, old_fr_fix, 4,
657 fragP->fr_symbol,
658 fragP->fr_offset, 1 /* pcrel */,
659 /* FIXME: Can't use a real BFD reloc here.
660 cgen_md_apply_fix3 can't handle it. */
661 BFD_RELOC_M32R_26_PCREL);
662
663 /* Mark this fragment as finished. */
664 frag_wane (fragP);
665 #else
666 {
667 const struct cgen_insn *insn;
668 int i;
669
670 /* Update the recorded insn.
671 Fortunately we don't have to look very far.
672 FIXME: Change this to record in the instruction the next higher
673 relaxable insn to use. */
674 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
675 {
676 if ((strcmp (CGEN_INSN_SYNTAX (insn)->mnemonic,
677 CGEN_INSN_SYNTAX (fragP->fr_cgen.insn)->mnemonic)
678 == 0)
679 && CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX))
680 break;
681 }
682 if (i == 4)
683 abort ();
684 fragP->fr_cgen.insn = insn;
685 return 2;
686 }
687 #endif
688 }
689
690 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
691 }
692
693 /* *fragP has been relaxed to its final size, and now needs to have
694 the bytes inside it modified to conform to the new size.
695
696 Called after relaxation is finished.
697 fragP->fr_type == rs_machine_dependent.
698 fragP->fr_subtype is the subtype of what the address relaxed to. */
699
700 void
701 md_convert_frag (abfd, sec, fragP)
702 bfd *abfd;
703 segT sec;
704 fragS *fragP;
705 {
706 char *opcode, *displacement;
707 int target_address, opcode_address, extension, addend;
708
709 opcode = fragP->fr_opcode;
710
711 /* Address opcode resides at in file space. */
712 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
713
714 switch (fragP->fr_subtype)
715 {
716 case 1 :
717 extension = 0;
718 displacement = &opcode[1];
719 break;
720 case 2 :
721 opcode[0] |= 0x80;
722 extension = 2;
723 displacement = &opcode[1];
724 break;
725 case 3 :
726 opcode[2] = opcode[0] | 0x80;
727 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
728 opcode_address += 2;
729 extension = 4;
730 displacement = &opcode[3];
731 break;
732 default :
733 abort ();
734 }
735
736 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
737 {
738 /* symbol must be resolved by linker */
739 if (fragP->fr_offset & 3)
740 as_warn ("Addend to unresolved symbol not on word boundary.");
741 addend = fragP->fr_offset >> 2;
742 }
743 else
744 {
745 /* Address we want to reach in file space. */
746 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
747 target_address += fragP->fr_symbol->sy_frag->fr_address;
748 addend = (target_address - (opcode_address & -4)) >> 2;
749 }
750
751 /* Create a relocation for symbols that must be resolved by the linker.
752 Otherwise output the completed insn. */
753
754 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
755 {
756 assert (fragP->fr_subtype != 1);
757 assert (fragP->fr_cgen.insn != 0);
758 cgen_record_fixup (fragP,
759 /* Offset of branch insn in frag. */
760 fragP->fr_fix + extension - 4,
761 fragP->fr_cgen.insn,
762 4 /*length*/,
763 /* FIXME: quick hack */
764 #if 0
765 CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
766 #else
767 CGEN_OPERAND_ENTRY (M32R_OPERAND_DISP24),
768 #endif
769 fragP->fr_cgen.opinfo,
770 fragP->fr_symbol, fragP->fr_offset);
771 }
772
773 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
774
775 md_number_to_chars (displacement, (valueT) addend,
776 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
777
778 fragP->fr_fix += extension;
779 }
780 \f
781 /* Functions concerning relocs. */
782
783 /* The location from which a PC relative jump should be calculated,
784 given a PC relative reloc. */
785
786 long
787 md_pcrel_from_section (fixP, sec)
788 fixS *fixP;
789 segT sec;
790 {
791 if (fixP->fx_addsy != (symbolS *) NULL
792 && (! S_IS_DEFINED (fixP->fx_addsy)
793 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
794 {
795 /* The symbol is undefined (or is defined but not in this section).
796 Let the linker figure it out. */
797 return 0;
798 }
799
800 return (fixP->fx_frag->fr_address + fixP->fx_where) & -4L;
801 }
802
803 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
804 Returns BFD_RELOC_NONE if no reloc type can be found.
805 *FIXP may be modified if desired. */
806
807 bfd_reloc_code_real_type
808 CGEN_SYM (lookup_reloc) (insn, operand, fixP)
809 const struct cgen_insn *insn;
810 const struct cgen_operand *operand;
811 fixS *fixP;
812 {
813 switch (CGEN_OPERAND_TYPE (operand))
814 {
815 case M32R_OPERAND_DISP8 : return BFD_RELOC_M32R_10_PCREL;
816 case M32R_OPERAND_DISP16 : return BFD_RELOC_M32R_18_PCREL;
817 case M32R_OPERAND_DISP24 : return BFD_RELOC_M32R_26_PCREL;
818 case M32R_OPERAND_UIMM24 : return BFD_RELOC_M32R_24;
819 case M32R_OPERAND_HI16 :
820 case M32R_OPERAND_SLO16 :
821 case M32R_OPERAND_ULO16 :
822 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
823 if (fixP->tc_fix_data.opinfo != 0)
824 return fixP->tc_fix_data.opinfo;
825 break;
826 }
827 return BFD_RELOC_NONE;
828 }
829
830 /* Called while parsing an instruction to create a fixup.
831 We need to check for HI16 relocs and queue them up for later sorting. */
832
833 fixS *
834 m32r_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
835 fragS *frag;
836 int where;
837 const struct cgen_insn *insn;
838 int length;
839 const struct cgen_operand *operand;
840 int opinfo;
841 expressionS *exp;
842 {
843 fixS *fixP = cgen_record_fixup_exp (frag, where, insn, length,
844 operand, opinfo, exp);
845
846 switch (CGEN_OPERAND_TYPE (operand))
847 {
848 case M32R_OPERAND_HI16 :
849 /* If low/high/shigh/sda was used, it is recorded in `opinfo'. */
850 if (fixP->tc_fix_data.opinfo == BFD_RELOC_M32R_HI16_SLO
851 || fixP->tc_fix_data.opinfo == BFD_RELOC_M32R_HI16_ULO)
852 m32r_record_hi16 (fixP->tc_fix_data.opinfo, fixP, now_seg);
853 break;
854 }
855
856 return fixP;
857 }
858
859 /* Record a HI16 reloc for later matching with its LO16 cousin. */
860
861 static void
862 m32r_record_hi16 (reloc_type, fixP, seg)
863 int reloc_type;
864 fixS *fixP;
865 segT seg;
866 {
867 struct m32r_hi_fixup *hi_fixup;
868
869 assert (reloc_type == BFD_RELOC_M32R_HI16_SLO
870 || reloc_type == BFD_RELOC_M32R_HI16_ULO);
871
872 hi_fixup = ((struct m32r_hi_fixup *)
873 xmalloc (sizeof (struct m32r_hi_fixup)));
874 hi_fixup->fixp = fixP;
875 hi_fixup->seg = now_seg;
876 hi_fixup->next = m32r_hi_fixup_list;
877 m32r_hi_fixup_list = hi_fixup;
878 }
879
880 /* Return BFD reloc type from opinfo field in a fixS.
881 It's tricky using fx_r_type in m32r_frob_file because the values
882 are BFD_RELOC_UNUSED + operand number. */
883 #define FX_OPINFO_R_TYPE(f) ((f)->tc_fix_data.opinfo)
884
885 /* Sort any unmatched HI16 relocs so that they immediately precede
886 the corresponding LO16 reloc. This is called before md_apply_fix and
887 tc_gen_reloc. */
888
889 void
890 m32r_frob_file ()
891 {
892 struct m32r_hi_fixup *l;
893
894 for (l = m32r_hi_fixup_list; l != NULL; l = l->next)
895 {
896 segment_info_type *seginfo;
897 int pass;
898
899 assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_SLO
900 || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_ULO);
901
902 /* Check quickly whether the next fixup happens to be a matching low. */
903 if (l->fixp->fx_next != NULL
904 && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_M32R_LO16
905 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
906 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
907 continue;
908
909 /* Look through the fixups for this segment for a matching `low'.
910 When we find one, move the high/shigh just in front of it. We do
911 this in two passes. In the first pass, we try to find a
912 unique `low'. In the second pass, we permit multiple high's
913 relocs for a single `low'. */
914 seginfo = seg_info (l->seg);
915 for (pass = 0; pass < 2; pass++)
916 {
917 fixS *f, *prev;
918
919 prev = NULL;
920 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
921 {
922 /* Check whether this is a `low' fixup which matches l->fixp. */
923 if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_M32R_LO16
924 && f->fx_addsy == l->fixp->fx_addsy
925 && f->fx_offset == l->fixp->fx_offset
926 && (pass == 1
927 || prev == NULL
928 || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_SLO
929 && FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_ULO)
930 || prev->fx_addsy != f->fx_addsy
931 || prev->fx_offset != f->fx_offset))
932 {
933 fixS **pf;
934
935 /* Move l->fixp before f. */
936 for (pf = &seginfo->fix_root;
937 *pf != l->fixp;
938 pf = &(*pf)->fx_next)
939 assert (*pf != NULL);
940
941 *pf = l->fixp->fx_next;
942
943 l->fixp->fx_next = f;
944 if (prev == NULL)
945 seginfo->fix_root = l->fixp;
946 else
947 prev->fx_next = l->fixp;
948
949 break;
950 }
951
952 prev = f;
953 }
954
955 if (f != NULL)
956 break;
957
958 if (pass == 1)
959 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
960 "Unmatched high/shigh reloc");
961 }
962 }
963 }
964
965 /* See whether we need to force a relocation into the output file.
966 This is used to force out switch and PC relative relocations when
967 relaxing. */
968
969 int
970 m32r_force_relocation (fix)
971 fixS *fix;
972 {
973 if (! m32r_relax)
974 return 0;
975
976 return (fix->fx_pcrel
977 || 0 /* ??? */);
978 }
979 \f
980 /* Write a value out to the object file, using the appropriate endianness. */
981
982 void
983 md_number_to_chars (buf, val, n)
984 char *buf;
985 valueT val;
986 int n;
987 {
988 if (target_big_endian)
989 number_to_chars_bigendian (buf, val, n);
990 else
991 number_to_chars_littleendian (buf, val, n);
992 }
993
994 /* Turn a string in input_line_pointer into a floating point constant of type
995 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
996 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
997 */
998
999 /* Equal to MAX_PRECISION in atof-ieee.c */
1000 #define MAX_LITTLENUMS 6
1001
1002 char *
1003 md_atof (type, litP, sizeP)
1004 char type;
1005 char *litP;
1006 int *sizeP;
1007 {
1008 int i,prec;
1009 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1010 LITTLENUM_TYPE *wordP;
1011 char *t;
1012 char *atof_ieee ();
1013
1014 switch (type)
1015 {
1016 case 'f':
1017 case 'F':
1018 case 's':
1019 case 'S':
1020 prec = 2;
1021 break;
1022
1023 case 'd':
1024 case 'D':
1025 case 'r':
1026 case 'R':
1027 prec = 4;
1028 break;
1029
1030 /* FIXME: Some targets allow other format chars for bigger sizes here. */
1031
1032 default:
1033 *sizeP = 0;
1034 return "Bad call to md_atof()";
1035 }
1036
1037 t = atof_ieee (input_line_pointer, type, words);
1038 if (t)
1039 input_line_pointer = t;
1040 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1041
1042 if (target_big_endian)
1043 {
1044 for (i = 0; i < prec; i++)
1045 {
1046 md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1047 litP += sizeof (LITTLENUM_TYPE);
1048 }
1049 }
1050 else
1051 {
1052 for (i = prec - 1; i >= 0; i--)
1053 {
1054 md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1055 litP += sizeof (LITTLENUM_TYPE);
1056 }
1057 }
1058
1059 return 0;
1060 }