remove #ifdef HAVE_CPU_M32RX uses. tweak some comments
[external/binutils.git] / gas / config / tc-m32r.c
1 /* tc-m32r.c -- Assembler for the Mitsubishi M32R.
2    Copyright (C) 1996, 1997, 1998 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 /* Structure to hold all of the different components describing an individual instruction.  */
28 typedef struct
29 {
30   const CGEN_INSN *     insn;
31   CGEN_FIELDS           fields;
32 #ifdef CGEN_INT_INSN
33   cgen_insn_t           buffer [CGEN_MAX_INSN_SIZE / sizeof (cgen_insn_t)];
34 #else
35   char                  buffer [CGEN_MAX_INSN_SIZE];
36 #endif
37   char *                addr;
38   fragS *               frag;
39   int                   indices [MAX_OPERAND_INSTANCES];
40 }
41 m32r_insn;
42
43 /* prev_insn.insn is non-null if last insn was a 16 bit insn on a 32 bit
44    boundary (i.e. was the first of two 16 bit insns).  */
45 static m32r_insn        prev_insn;
46
47 /* Non-zero if we've seen a relaxable insn since the last 32 bit
48    alignment request.  */
49 static int seen_relaxable_p = 0;
50
51 /* Non-zero if -relax specified, in which case sufficient relocs are output
52    for the linker to do relaxing.
53    We do simple forms of relaxing internally, but they are always done.
54    This flag does not apply to them.  */
55 static int m32r_relax;
56
57 /* If non-NULL, pointer to cpu description file to read.
58    This allows runtime additions to the assembler.  */
59 static char * m32r_cpu_desc;
60
61 /* start-sanitize-m32rx */
62 /* Non-zero if -m32rx has been specified, in which case support for the
63    extended M32RX instruction set should be enabled.  */
64 static int enable_m32rx = 0;
65
66 /* Non-zero if the programmer should be warned when an explicit parallel
67    instruction might have constraint violations.  */
68 static int warn_explicit_parallel_conflicts = 1;
69 /* end-sanitize-m32rx */
70
71 /* stuff for .scomm symbols.  */
72 static segT     sbss_section;
73 static asection scom_section;
74 static asymbol  scom_symbol;
75
76 const char comment_chars[]        = ";";
77 const char line_comment_chars[]   = "#";
78 const char line_separator_chars[] = "";
79 const char EXP_CHARS[]            = "eE";
80 const char FLT_CHARS[]            = "dD";
81
82 /* Relocations against symbols are done in two
83    parts, with a HI relocation and a LO relocation.  Each relocation
84    has only 16 bits of space to store an addend.  This means that in
85    order for the linker to handle carries correctly, it must be able
86    to locate both the HI and the LO relocation.  This means that the
87    relocations must appear in order in the relocation table.
88
89    In order to implement this, we keep track of each unmatched HI
90    relocation.  We then sort them so that they immediately precede the
91    corresponding LO relocation. */
92
93 struct m32r_hi_fixup
94 {
95   struct m32r_hi_fixup * next;  /* Next HI fixup.  */
96   fixS *                 fixp;  /* This fixup.  */
97   segT                   seg;   /* The section this fixup is in.  */
98
99 };
100
101 /* The list of unmatched HI relocs.  */
102
103 static struct m32r_hi_fixup * m32r_hi_fixup_list;
104
105 \f
106 /* start-sanitize-m32rx */
107 static void
108 allow_m32rx (on)
109      int on;
110 {
111   enable_m32rx = on;
112
113   if (stdoutput != NULL)
114     bfd_set_arch_mach (stdoutput, TARGET_ARCH,
115                        enable_m32rx ? bfd_mach_m32rx : bfd_mach_m32r);
116 }
117 /* end-sanitize-m32rx */
118 \f
119 const char * md_shortopts = "";
120
121 struct option md_longopts[] =
122 {
123 /* start-sanitize-m32rx */
124 #define OPTION_M32RX    (OPTION_MD_BASE)
125   {"m32rx", no_argument, NULL, OPTION_M32RX},
126 #define OPTION_WARN     (OPTION_MD_BASE + 1)
127   {"warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_WARN},
128 #define OPTION_NO_WARN  (OPTION_MD_BASE + 2)
129   {"no-warn-explicit-parallel-conflicts", no_argument, NULL, OPTION_NO_WARN},
130 /* end-sanitize-m32rx */
131
132 #if 0 /* not supported yet */
133 #define OPTION_RELAX  (OPTION_MD_BASE + 3)
134   {"relax", no_argument, NULL, OPTION_RELAX},
135 #define OPTION_CPU_DESC (OPTION_MD_BASE + 4)
136   {"cpu-desc", required_argument, NULL, OPTION_CPU_DESC},
137 #endif
138
139   {NULL, no_argument, NULL, 0}
140 };
141 size_t md_longopts_size = sizeof (md_longopts);
142
143 int
144 md_parse_option (c, arg)
145      int    c;
146      char * arg;
147 {
148   switch (c)
149     {
150 /* start-sanitize-m32rx */
151     case OPTION_M32RX:
152       allow_m32rx (1);
153       break;
154       
155     case OPTION_WARN:
156       warn_explicit_parallel_conflicts = 1;
157       break;
158       
159     case OPTION_NO_WARN:
160       warn_explicit_parallel_conflicts = 0;
161       break;
162 /* end-sanitize-m32rx */
163       
164 #if 0 /* not supported yet */
165     case OPTION_RELAX:
166       m32r_relax = 1;
167       break;
168     case OPTION_CPU_DESC:
169       m32r_cpu_desc = arg;
170       break;
171 #endif
172     default:
173       return 0;
174     }
175   return 1;
176 }
177
178 void
179 md_show_usage (stream)
180   FILE * stream;
181 {
182   fprintf (stream, "M32R/X options:\n");
183 /* start-sanitize-m32rx */
184   fprintf (stream, "\
185 --m32rx                 support the extended m32rx instruction set\n");
186   
187   fprintf (stream, "\
188 --warn-explicit-parallel-conflicts      Warn when parallel instrucitons violate contraints\n");
189   fprintf (stream, "\
190 --no-warn-explicit-parallel-conflicts   Do not warn when parallel instrucitons violate contraints\n");
191 /* end-sanitize-m32rx */
192
193 #if 0
194   fprintf (stream, "\
195 --relax                 create linker relaxable code\n");
196   fprintf (stream, "\
197 --cpu-desc              provide runtime cpu description file\n");
198 #endif
199
200
201 static void fill_insn PARAMS ((int));
202 static void m32r_scomm PARAMS ((int));
203
204 /* Set by md_assemble for use by m32r_fill_insn.  */
205 static subsegT prev_subseg;
206 static segT prev_seg;
207
208 /* The target specific pseudo-ops which we support.  */
209 const pseudo_typeS md_pseudo_table[] =
210 {
211   { "word", cons, 4 },
212   { "fillinsn", fill_insn, 0 },
213   { "scomm", m32r_scomm, 0 },
214 /* start-sanitize-m32rx */
215   { "m32r",  allow_m32rx, 0},
216   { "m32rx", allow_m32rx, 1},
217 /* end-sanitize-m32rx */
218   { NULL, NULL, 0 }
219 };
220
221 /* FIXME: Should be machine generated.  */
222 #define NOP_INSN 0x7000
223 #define PAR_NOP_INSN 0xf000 /* can only be used in 2nd slot */
224
225 /* When we align the .text section, insert the correct NOP pattern.
226    N is the power of 2 alignment.  LEN is the length of pattern FILL.
227    MAX is the maximum number of characters to skip when doing the alignment,
228    or 0 if there is no maximum.  */
229
230 int
231 m32r_do_align (n, fill, len, max)
232      int          n;
233      const char * fill;
234      int          len;
235      int          max;
236 {
237   if ((fill == NULL || (* fill == 0 && len == 1))
238       && (now_seg->flags & SEC_CODE) != 0
239       /* Only do this special handling if aligning to at least a
240          4 byte boundary.  */
241       && n > 1
242      /* Only do this special handling if we're allowed to emit at
243          least two bytes.  */
244       && (max == 0 || max > 1))
245     {
246       static const unsigned char nop_pattern[] = { 0xf0, 0x00 };
247
248 #if 0
249       /* First align to a 2 byte boundary, in case there is an odd .byte.  */
250       /* FIXME: How much memory will cause gas to use when assembling a big
251          program?  Perhaps we can avoid the frag_align call?  */
252       frag_align (1, 0, 0);
253 #endif
254       /* Next align to a 4 byte boundary (we know n >= 2) using a parallel
255          nop.  */
256       frag_align_pattern (2, nop_pattern, sizeof nop_pattern, 0);
257       /* If doing larger alignments use a repeating sequence of appropriate
258          nops.  */
259       if (n > 2)
260         {
261           static const unsigned char multi_nop_pattern[] =
262           { 0x70, 0x00, 0xf0, 0x00 };
263           frag_align_pattern (n, multi_nop_pattern, sizeof multi_nop_pattern,
264                               max ? max - 2 : 0);
265         }
266       return 1;
267     }
268
269   return 0;
270 }
271
272 static void
273 assemble_nop (opcode)
274      int opcode;
275 {
276   char * f = frag_more (2);
277   md_number_to_chars (f, opcode, 2);
278 }
279
280 /* If the last instruction was the first of 2 16 bit insns,
281    output a nop to move the PC to a 32 bit boundary.
282
283    This is done via an alignment specification since branch relaxing
284    may make it unnecessary.
285
286    Internally, we need to output one of these each time a 32 bit insn is
287    seen after an insn that is relaxable.  */
288
289 static void
290 fill_insn (ignore)
291      int ignore;
292 {
293   (void) m32r_do_align (2, NULL, 0, 0);
294   prev_insn.insn = NULL;
295   seen_relaxable_p = 0;
296 }
297
298 /* Cover function to fill_insn called after a label and at end of assembly.
299
300    The result is always 1: we're called in a conditional to see if the
301    current line is a label.  */
302
303 int
304 m32r_fill_insn (done)
305      int done;
306 {
307   segT    seg;
308   subsegT subseg;
309
310   if (prev_seg != NULL)
311     {
312       seg    = now_seg;
313       subseg = now_subseg;
314       
315       subseg_set (prev_seg, prev_subseg);
316       
317       fill_insn (0);
318       
319       subseg_set (seg, subseg);
320     }
321   
322   return 1;
323 }
324 \f
325 void
326 md_begin ()
327 {
328   flagword applicable;
329   segT     seg;
330   subsegT  subseg;
331
332   /* Initialize the `cgen' interface.  */
333
334   /* This is a callback from cgen to gas to parse operands.  */
335   cgen_parse_operand_fn = cgen_parse_operand;
336   
337   /* Set the machine number and endian.  */
338   CGEN_SYM (init_asm) (0 /* mach number */,
339                        target_big_endian ?
340                        CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE);
341
342 #if 0 /* not supported yet */
343   /* If a runtime cpu description file was provided, parse it.  */
344   if (m32r_cpu_desc != NULL)
345     {
346       const char * errmsg;
347
348       errmsg = cgen_read_cpu_file (m32r_cpu_desc);
349       if (errmsg != NULL)
350         as_bad ("%s: %s", m32r_cpu_desc, errmsg);
351     }
352 #endif
353
354   /* Save the current subseg so we can restore it [it's the default one and
355      we don't want the initial section to be .sbss].  */
356   seg    = now_seg;
357   subseg = now_subseg;
358
359   /* The sbss section is for local .scomm symbols.  */
360   sbss_section = subseg_new (".sbss", 0);
361   
362   /* This is copied from perform_an_assembly_pass.  */
363   applicable = bfd_applicable_section_flags (stdoutput);
364   bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
365   
366 #if 0 /* What does this do? [see perform_an_assembly_pass]  */
367   seg_info (bss_section)->bss = 1;
368 #endif
369
370   subseg_set (seg, subseg);
371
372   /* We must construct a fake section similar to bfd_com_section
373      but with the name .scommon.  */
374   scom_section                = bfd_com_section;
375   scom_section.name           = ".scommon";
376   scom_section.output_section = & scom_section;
377   scom_section.symbol         = & scom_symbol;
378   scom_section.symbol_ptr_ptr = & scom_section.symbol;
379   scom_symbol                 = * bfd_com_section.symbol;
380   scom_symbol.name            = ".scommon";
381   scom_symbol.section         = & scom_section;
382
383 /* start-sanitize-m32rx */
384   allow_m32rx (enable_m32rx);
385 /* end-sanitize-m32rx */
386 }
387
388 /* start-sanitize-m32rx */
389
390 /* Returns true if an output of instruction 'a' is referenced by an operand
391    of instruction 'b'.  If 'check_outputs' is true then b's outputs are
392    checked, otherwise its inputs are examined.  */
393 static int
394 first_writes_to_seconds_operands (a, b, check_outputs)
395      m32r_insn * a;
396      m32r_insn * b;
397      const int   check_outputs;
398 {
399   const CGEN_OPERAND_INSTANCE * a_operands = CGEN_INSN_OPERANDS (a->insn);
400   const CGEN_OPERAND_INSTANCE * b_operands = CGEN_INSN_OPERANDS (b->insn);
401   int                           a_index;
402
403   /* If at least one of the instructions takes no operands, then there is
404      nothing to check.  There really are instructions without operands,
405      eg 'nop'.  */
406   if (a_operands == NULL || b_operands == NULL)
407     return 0;
408       
409   /* Scan the operand list of 'a' looking for an output operand.  */
410   for (a_index = 0;
411        CGEN_OPERAND_INSTANCE_TYPE (a_operands) != CGEN_OPERAND_INSTANCE_END;
412        a_index ++, a_operands ++)
413     {
414       if (CGEN_OPERAND_INSTANCE_TYPE (a_operands) == CGEN_OPERAND_INSTANCE_OUTPUT)
415         {
416           int b_index;
417             
418           /* Scan operand list of 'b' looking for an operand that references
419              the same hardware element, and which goes in the right direction.  */
420           for (b_index = 0;
421                CGEN_OPERAND_INSTANCE_TYPE (b_operands) != CGEN_OPERAND_INSTANCE_END;
422                b_index ++, b_operands ++)
423             {
424               if ((CGEN_OPERAND_INSTANCE_TYPE (b_operands) ==
425                   (check_outputs ? CGEN_OPERAND_INSTANCE_OUTPUT : CGEN_OPERAND_INSTANCE_INPUT))
426                   && (CGEN_OPERAND_INSTANCE_HW (b_operands) == CGEN_OPERAND_INSTANCE_HW (a_operands))
427                   && (a->indices [a_index] == b->indices [b_index]))
428                 return 1;
429             }
430         }
431     }
432
433     return 0;
434 }
435
436 /* Returns true if the insn can (potentially) alter the program counter.  */
437 static int
438 writes_to_pc (a)
439      m32r_insn * a;
440 {
441 #if 0
442   const CGEN_OPERAND_INSTANCE * a_operands == CGEN_INSN_OPERANDS (a->insn);
443
444   if (a_operands == NULL)
445     return 0;
446
447   while (CGEN_OPERAND_INSTANCE_TYPE (a_operands) != CGEN_OPERAND_INSTANCE_END)
448     {
449       if (CGEN_OPERAND_INSTANCE_OPERAND (a_operands) != NULL
450           && CGEN_OPERAND_INDEX (CGEN_OPERAND_INSTANCE_OPERAND (a_operands)) == M32R_OPERAND_PC)
451         return 1;
452       
453       a_operands ++;
454     }
455 #else
456   if (CGEN_INSN_ATTR (a->insn, CGEN_INSN_UNCOND_CTI)
457       || CGEN_INSN_ATTR (a->insn, CGEN_INSN_COND_CTI))
458     return 1;
459 #endif
460   return 0;
461 }
462
463 /* Returns NULL if the two 16 bit insns can be executed in parallel,
464    otherwise it returns a pointer to an error message explaining why not.  */
465 static const char *
466 can_make_parallel (a, b)
467      m32r_insn * a;
468      m32r_insn * b;
469 {
470   PIPE_ATTR a_pipe;
471   PIPE_ATTR b_pipe;
472   
473   /* Make sure the instructions are the right length.  */
474   if (   CGEN_FIELDS_BITSIZE (& a->fields) != 16
475       || CGEN_FIELDS_BITSIZE (& b->fields) != 16)
476     abort();
477
478   if (first_writes_to_seconds_operands (a, b, true))
479     return "Instructions write to the same destination register.";
480   
481   a_pipe = CGEN_INSN_ATTR (a->insn, CGEN_INSN_PIPE);
482   b_pipe = CGEN_INSN_ATTR (b->insn, CGEN_INSN_PIPE);
483
484   /* Make sure that the instructions use the correct execution pipelines.  */
485   if (   a_pipe == PIPE_NONE
486       || b_pipe == PIPE_NONE)
487     return "Instructions do not use parallel execution pipelines.";
488
489   /* Leave this test for last, since it is the only test that can
490      go away if the instructions are swapped, and we want to make
491      sure that any other errors are detected before this happens.  */
492   if (   a_pipe == PIPE_S
493       || b_pipe == PIPE_O)
494     return "Instructions share the same execution pipeline";
495   
496   return NULL;
497 }
498
499 #ifdef CGEN_INT_INSN
500 static void
501 make_parallel (buffer)
502      cgen_insn_t * buffer;
503 {
504   /* Force the top bit of the second insn to be set.  */
505
506   bfd_vma value;
507       
508   if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
509     {
510       value = bfd_getb16 ((bfd_byte *) buffer);
511       value |= 0x8000;
512       bfd_putb16 (value, (char *) buffer);
513     }
514   else
515     {
516       value = bfd_getl16 ((bfd_byte *) buffer);
517       value |= 0x8000;
518       bfd_putl16 (value, (char *) buffer);
519     }
520 }
521 #else
522 static void
523 make_parallel (buffer)
524      char * buffer;
525 {
526   /* Force the top bit of the second insn to be set.  */
527
528   buffer [CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG ? 0 : 1] |= 0x80;
529 }
530 #endif
531
532
533 static void
534 assemble_parallel_insn (str, str2)
535      char * str;
536      char * str2;
537 {
538   char *    str3;
539   m32r_insn first;
540   m32r_insn second;
541   char *    errmsg;
542   
543   * str2 = 0; /* Seperate the two instructions.  */
544
545   /* If there was a previous 16 bit insn, then fill the following 16 bit slot,
546      so that the parallel instruction will start on a 32 bit boundary.  */
547   if (prev_insn.insn)
548     fill_insn (0);
549
550   /* Parse the first instruction.  */
551   if (! (first.insn = CGEN_SYM (assemble_insn)
552          (str, & first.fields, first.buffer, & errmsg)))
553     {
554       as_bad (errmsg);
555       return;
556     }
557   
558   /* Check to see if this is an allowable parallel insn.  */
559   if (CGEN_INSN_ATTR (first.insn, CGEN_INSN_PIPE) == PIPE_NONE)
560     {
561       as_bad ("instruction '%s' cannot be executed in parallel.", str);
562       return;
563     }
564   
565   if (! enable_m32rx
566       && CGEN_INSN_ATTR (first.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
567     {
568       as_bad ("instruction '%s' is for the M32RX only", str);
569       return;
570     }
571   
572   *str2 = '|';       /* Restore the original assembly text, just in case it is needed.  */
573   str3  = str;       /* Save the original string pointer.  */
574   str   = str2 + 2;  /* Advanced past the parsed string.  */
575   str2  = str3;      /* Remember the entire string in case it is needed for error messages.  */
576   
577   /* Preserve any fixups that have been generated and reset the list to empty.  */
578   cgen_save_fixups();
579
580   /* Get the indicies of the operands of the instruction.  */
581   /* FIXME: CGEN_FIELDS is already recorded, but relying on that fact
582      doesn't seem right.  Perhaps allow passing fields like we do insn.  */
583   /* FIXME: ALIAS insns do not have operands, so we use this function
584      to find the equivalent insn and overwrite the value stored in our
585      structure.  When aliases behave differently this may have to change.  */
586   first.insn = m32r_cgen_get_insn_operands (first.insn, bfd_getb16 ((char *) first.buffer), 16,
587                                             first.indices);
588   if (first.insn == NULL)
589     as_fatal ("internal error: m32r_cgen_get_insn_operands failed for first insn");
590
591   /* Parse the second instruction.  */
592   if (! (second.insn = CGEN_SYM (assemble_insn)
593          (str, & second.fields, second.buffer, & errmsg)))
594     {
595       as_bad (errmsg);
596       return;
597     }
598
599   /* Check it.  */
600   if (! enable_m32rx
601       && CGEN_INSN_ATTR (second.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
602     {
603       as_bad ("instruction '%s' is for the M32RX only", str);
604       return;
605     }
606   
607   if (! enable_m32rx)
608     {
609       if (   strcmp (first.insn->name, "nop") != 0
610           && strcmp (second.insn->name, "nop") != 0)
611         {
612           as_bad ("'%s': only the NOP instruction can be issued in parallel on the m32r", str2);
613           return;
614         }
615     }
616
617   /* Get the indicies of the operands of the instruction.  */
618   second.insn = m32r_cgen_get_insn_operands (second.insn, bfd_getb16 ((char *) second.buffer), 16,
619                                              second.indices);
620   if (second.insn == NULL)
621     as_fatal ("internal error: m32r_cgen_get_insn_operands failed for second insn");
622
623   /* We assume that if the first instruction writes to a register that is
624      read by the second instruction it is because the programmer intended
625      this to happen, (after all they have explicitly requested that these
626      two instructions be executed in parallel).  Although if the global
627      variable warn_explicit_parallel_conflicts is true then we do generate
628      a warning message.  Similarly we assume that parallel branch and jump
629      instructions are deliberate and should not produce errors.  */
630   
631   if (warn_explicit_parallel_conflicts)
632     {
633       if (first_writes_to_seconds_operands (& first, & second, false))
634         as_warn ("%s: output of 1st instruction is the same as an input to 2nd instruction - is this intentional ?", str2);
635       
636       if (first_writes_to_seconds_operands (& second, & first, false))
637         as_warn ("%s: output of 2nd instruction is the same as an input to 1st instruction - is this intentional ?", str2);
638     }
639       
640   if ((errmsg = (char *) can_make_parallel (& first, & second)) == NULL)
641     {
642       /* Get the fixups for the first instruction.  */
643       cgen_swap_fixups ();
644
645       /* Write it out.  */
646       (void) cgen_asm_finish_insn (first.insn, first.buffer,
647                                    CGEN_FIELDS_BITSIZE (& first.fields));
648       
649       /* Force the top bit of the second insn to be set.  */
650       make_parallel (second.buffer);
651
652       /* Get its fixups.  */
653       cgen_restore_fixups ();
654
655       /* Write it out.  */
656       (void) cgen_asm_finish_insn (second.insn, second.buffer,
657                                    CGEN_FIELDS_BITSIZE (& second.fields));
658     }
659   /* Try swapping the instructions to see if they work that way.  */
660   else if (can_make_parallel (& second, & first) == NULL)
661     {
662       /* Write out the second instruction first.  */
663       (void) cgen_asm_finish_insn (second.insn, second.buffer,
664                                    CGEN_FIELDS_BITSIZE (& second.fields));
665       
666       /* Force the top bit of the first instruction to be set.  */
667       make_parallel (first.buffer);
668
669       /* Get the fixups for the first instruction.  */
670       cgen_restore_fixups ();
671
672       /* Write out the first instruction.  */
673       (void) cgen_asm_finish_insn (first.insn, first.buffer,
674                                    CGEN_FIELDS_BITSIZE (& first.fields));
675     }
676   else
677     {
678       as_bad ("'%s': %s", str2, errmsg);
679       return;
680     }
681       
682   /* Set these so m32r_fill_insn can use them.  */
683   prev_seg    = now_seg;
684   prev_subseg = now_subseg;
685
686   return;
687 }
688
689 /* end-sanitize-m32rx */
690
691
692 void
693 md_assemble (str)
694      char * str;
695 {
696   m32r_insn insn;
697   char *    errmsg;
698   char *    str2 = NULL;
699
700   /* Initialize GAS's cgen interface for a new instruction.  */
701   cgen_asm_init_parse ();
702
703 /* start-sanitize-m32rx */
704   /* Look for a parallel instruction seperator.  */
705   if ((str2 = strstr (str, "||")) != NULL)
706     {
707       assemble_parallel_insn (str, str2);
708       return;
709     }
710 /* end-sanitize-m32rx */
711   
712   insn.insn = CGEN_SYM (assemble_insn) (str, & insn.fields, insn.buffer, & errmsg);
713   if (!insn.insn)
714     {
715       as_bad (errmsg);
716       return;
717     }
718
719 /* start-sanitize-m32rx */
720   if (! enable_m32rx && CGEN_INSN_ATTR (insn.insn, CGEN_INSN_MACH) == (1 << MACH_M32RX))
721     {
722       as_bad ("instruction '%s' is for the M32RX only", str);
723       return;
724     }
725 /* end-sanitize-m32rx */
726   
727   if (CGEN_INSN_BITSIZE (insn.insn) == 32)
728     {
729       /* 32 bit insns must live on 32 bit boundaries.  */
730       if (prev_insn.insn || seen_relaxable_p)
731         {
732           /* ??? If calling fill_insn too many times turns us into a memory
733              pig, can we call assemble_nop instead of !seen_relaxable_p?  */
734           fill_insn (0);
735         }
736       
737       (void) cgen_asm_finish_insn (insn.insn, insn.buffer,
738                                    CGEN_FIELDS_BITSIZE (& insn.fields));
739     }
740   else
741     {
742 /* start-sanitize-m32rx */
743 /* start-sanitize-phase2-m32rx */
744       int swap = false;
745 /* end-sanitize-phase2-m32rx */
746 /* end-sanitize-m32rx */
747       
748       if (CGEN_INSN_BITSIZE (insn.insn) != 16)
749         abort();
750       
751       /* Get the indices of the operands of the instruction.  */
752       insn.insn = m32r_cgen_get_insn_operands (insn.insn,
753                                                bfd_getb16 ((char *) insn.buffer),
754                                                16,
755                                                insn.indices);
756       if (insn.insn == NULL)
757         as_fatal ("internal error: m32r_cgen_get_insn_operands failed");
758
759       /* Keep track of whether we've seen a pair of 16 bit insns.
760          prev_insn.insn is NULL when we're on a 32 bit boundary.  */
761       if (prev_insn.insn)
762         {
763 /* start-sanitize-m32rx */
764 /* start-sanitize-phase2-m32rx */
765           /* Look to see if this instruction can be combined with the
766              previous instruction to make one, parallel, 32 bit instruction.
767              If the previous instruction (potentially) changed the flow of
768              program control, then it cannot be combined with the current
769              instruction.  If the current instruction is relaxable, then it
770              might be replaced with a longer version, so we cannot combine it.
771              Also if the output of the previous instruction is used as an
772              input to the current instruction then it cannot be combined.
773              Otherwise call can_make_parallel() with both orderings of the
774              instructions to see if they can be combined.  */
775           if (     enable_m32rx
776               &&   CGEN_INSN_ATTR (insn.insn, CGEN_INSN_RELAXABLE) == 0
777               && ! writes_to_pc (& prev_insn)
778               && ! first_writes_to_seconds_operands (& prev_insn, &insn, false)
779                  )
780             {
781               if (can_make_parallel (& prev_insn, & insn) == NULL)
782                 make_parallel (insn.buffer);
783               else if (can_make_parallel (& insn, & prev_insn.insn) == NULL)
784                 swap = true;
785             }
786 /* end-sanitize-phase2-m32rx */
787 /* end-sanitize-m32rx */
788           
789           prev_insn.insn = NULL;
790         }
791       else
792         {
793           prev_insn = insn;
794         }
795
796       /* Record the frag that might be used by this insn.  */
797       insn.frag = frag_now;
798       insn.addr = cgen_asm_finish_insn (insn.insn, insn.buffer,
799                                    CGEN_FIELDS_BITSIZE (& insn.fields));
800
801 /* start-sanitize-m32rx */
802 /* start-sanitize-phase2-m32rx */
803       if (swap)
804         {
805           int tmp;
806           
807 #define SWAP_BYTES(a,b) tmp = a; a = b; b = tmp
808
809           /* Swap the two insns */
810           SWAP_BYTES (prev_insn.addr [0], insn.addr [0]);
811           SWAP_BYTES (prev_insn.addr [1], insn.addr [1]);
812
813           make_parallel (insn.addr);
814
815           /* Swap any relaxable frags recorded for the two insns.  */
816           if (prev_insn.frag->fr_opcode == prev_insn.addr)
817             prev_insn.frag->fr_opcode = insn.addr;
818           else if (insn.frag->fr_opcode == insn.addr)
819             insn.frag->fr_opcode = prev_insn.addr;
820         }
821 /* end-sanitize-phase2-m32rx */
822
823       /* Record where this instruction was assembled.  */
824       prev_insn.addr = insn.addr;
825       prev_insn.frag = insn.frag;
826 /* end-sanitize-m32rx */
827       
828       /* If the insn needs the following one to be on a 32 bit boundary
829          (e.g. subroutine calls), fill this insn's slot.  */
830       if (prev_insn.insn != NULL
831           && CGEN_INSN_ATTR (insn.insn, CGEN_INSN_FILL_SLOT) != 0)
832         fill_insn (0);
833
834       /* If this is a relaxable insn (can be replaced with a larger version)
835          mark the fact so that we can emit an alignment directive for a
836          following 32 bit insn if we see one.   */
837       if (CGEN_INSN_ATTR (insn.insn, CGEN_INSN_RELAXABLE) != 0)
838         seen_relaxable_p = 1;
839     }
840
841   /* Set these so m32r_fill_insn can use them.  */
842   prev_seg    = now_seg;
843   prev_subseg = now_subseg;
844 }
845
846 /* The syntax in the manual says constants begin with '#'.
847    We just ignore it.  */
848
849 void 
850 md_operand (expressionP)
851      expressionS * expressionP;
852 {
853   if (* input_line_pointer == '#')
854     {
855       input_line_pointer ++;
856       expression (expressionP);
857     }
858 }
859
860 valueT
861 md_section_align (segment, size)
862      segT   segment;
863      valueT size;
864 {
865   int align = bfd_get_section_alignment (stdoutput, segment);
866   return ((size + (1 << align) - 1) & (-1 << align));
867 }
868
869 symbolS *
870 md_undefined_symbol (name)
871   char * name;
872 {
873   return 0;
874 }
875 \f
876 /* .scomm pseudo-op handler.
877
878    This is a new pseudo-op to handle putting objects in .scommon.
879    By doing this the linker won't need to do any work and more importantly
880    it removes the implicit -G arg necessary to correctly link the object file.
881 */
882
883 static void
884 m32r_scomm (ignore)
885      int ignore;
886 {
887   register char *    name;
888   register char      c;
889   register char *    p;
890   offsetT            size;
891   register symbolS * symbolP;
892   offsetT            align;
893   int                align2;
894
895   name = input_line_pointer;
896   c = get_symbol_end ();
897
898   /* just after name is now '\0' */
899   p = input_line_pointer;
900   * p = c;
901   SKIP_WHITESPACE ();
902   if (* input_line_pointer != ',')
903     {
904       as_bad ("Expected comma after symbol-name: rest of line ignored.");
905       ignore_rest_of_line ();
906       return;
907     }
908
909   input_line_pointer ++;                /* skip ',' */
910   if ((size = get_absolute_expression ()) < 0)
911     {
912       as_warn (".SCOMMon length (%ld.) <0! Ignored.", (long) size);
913       ignore_rest_of_line ();
914       return;
915     }
916
917   /* The third argument to .scomm is the alignment.  */
918   if (* input_line_pointer != ',')
919     align = 8;
920   else
921     {
922       ++ input_line_pointer;
923       align = get_absolute_expression ();
924       if (align <= 0)
925         {
926           as_warn ("ignoring bad alignment");
927           align = 8;
928         }
929     }
930   /* Convert to a power of 2 alignment.  */
931   if (align)
932     {
933       for (align2 = 0; (align & 1) == 0; align >>= 1, ++ align2)
934         continue;
935       if (align != 1)
936         {
937           as_bad ("Common alignment not a power of 2");
938           ignore_rest_of_line ();
939           return;
940         }
941     }
942   else
943     align2 = 0;
944
945   * p = 0;
946   symbolP = symbol_find_or_make (name);
947   * p = c;
948
949   if (S_IS_DEFINED (symbolP))
950     {
951       as_bad ("Ignoring attempt to re-define symbol `%s'.",
952               S_GET_NAME (symbolP));
953       ignore_rest_of_line ();
954       return;
955     }
956
957   if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
958     {
959       as_bad ("Length of .scomm \"%s\" is already %ld. Not changed to %ld.",
960               S_GET_NAME (symbolP),
961               (long) S_GET_VALUE (symbolP),
962               (long) size);
963
964       ignore_rest_of_line ();
965       return;
966     }
967
968   if (symbolP->local)
969     {
970       segT   old_sec    = now_seg;
971       int    old_subsec = now_subseg;
972       char * pfrag;
973
974       record_alignment (sbss_section, align2);
975       subseg_set (sbss_section, 0);
976       
977       if (align2)
978         frag_align (align2, 0, 0);
979       
980       if (S_GET_SEGMENT (symbolP) == sbss_section)
981         symbolP->sy_frag->fr_symbol = 0;
982       
983       symbolP->sy_frag = frag_now;
984       
985       pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
986                         (char *) 0);
987       * pfrag = 0;
988       S_SET_SIZE (symbolP, size);
989       S_SET_SEGMENT (symbolP, sbss_section);
990       S_CLEAR_EXTERNAL (symbolP);
991       subseg_set (old_sec, old_subsec);
992     }
993   else
994     {
995       S_SET_VALUE (symbolP, (valueT) size);
996       S_SET_ALIGN (symbolP, align2);
997       S_SET_EXTERNAL (symbolP);
998       S_SET_SEGMENT (symbolP, & scom_section);
999     }
1000
1001   demand_empty_rest_of_line ();
1002 }
1003 \f
1004 /* Interface to relax_segment.  */
1005
1006 /* FIXME: Build table by hand, get it working, then machine generate.  */
1007
1008 const relax_typeS md_relax_table[] =
1009 {
1010 /* The fields are:
1011    1) most positive reach of this state,
1012    2) most negative reach of this state,
1013    3) how many bytes this mode will add to the size of the current frag
1014    4) which index into the table to try if we can't fit into this one.  */
1015
1016   /* The first entry must be unused because an `rlx_more' value of zero ends
1017      each list.  */
1018   {1, 1, 0, 0},
1019
1020   /* The displacement used by GAS is from the end of the 2 byte insn,
1021      so we subtract 2 from the following.  */
1022   /* 16 bit insn, 8 bit disp -> 10 bit range.
1023      This doesn't handle a branch in the right slot at the border:
1024      the "& -4" isn't taken into account.  It's not important enough to
1025      complicate things over it, so we subtract an extra 2 (or + 2 in -ve
1026      case).  */
1027   {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
1028   /* 32 bit insn, 24 bit disp -> 26 bit range.  */
1029   {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
1030   /* Same thing, but with leading nop for alignment.  */
1031   {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
1032 };
1033
1034 long
1035 m32r_relax_frag (fragP, stretch)
1036      fragS * fragP;
1037      long    stretch;
1038 {
1039   /* Address of branch insn.  */
1040   long address = fragP->fr_address + fragP->fr_fix - 2;
1041   long growth = 0;
1042
1043   /* Keep 32 bit insns aligned on 32 bit boundaries.  */
1044   if (fragP->fr_subtype == 2)
1045     {
1046       if ((address & 3) != 0)
1047         {
1048           fragP->fr_subtype = 3;
1049           growth = 2;
1050         }
1051     }
1052   else if (fragP->fr_subtype == 3)
1053     {
1054       if ((address & 3) == 0)
1055         {
1056           fragP->fr_subtype = 2;
1057           growth = -2;
1058         }
1059     }
1060   else
1061     {
1062       growth = relax_frag (fragP, stretch);
1063
1064       /* Long jump on odd halfword boundary?  */
1065       if (fragP->fr_subtype == 2 && (address & 3) != 0)
1066         {
1067           fragP->fr_subtype = 3;
1068           growth += 2;
1069         }
1070     }
1071
1072   return growth;
1073 }
1074
1075 /* Return an initial guess of the length by which a fragment must grow to
1076    hold a branch to reach its destination.
1077    Also updates fr_type/fr_subtype as necessary.
1078
1079    Called just before doing relaxation.
1080    Any symbol that is now undefined will not become defined.
1081    The guess for fr_var is ACTUALLY the growth beyond fr_fix.
1082    Whatever we do to grow fr_fix or fr_var contributes to our returned value.
1083    Although it may not be explicit in the frag, pretend fr_var starts with a
1084    0 value.  */
1085
1086 int
1087 md_estimate_size_before_relax (fragP, segment)
1088      fragS * fragP;
1089      segT    segment;
1090 {
1091   int    old_fr_fix = fragP->fr_fix;
1092   char * opcode = fragP->fr_opcode;
1093
1094   /* The only thing we have to handle here are symbols outside of the
1095      current segment.  They may be undefined or in a different segment in
1096      which case linker scripts may place them anywhere.
1097      However, we can't finish the fragment here and emit the reloc as insn
1098      alignment requirements may move the insn about.  */
1099
1100   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
1101     {
1102       /* The symbol is undefined in this segment.
1103          Change the relaxation subtype to the max allowable and leave
1104          all further handling to md_convert_frag.  */
1105       fragP->fr_subtype = 2;
1106
1107 #if 0 /* Can't use this, but leave in for illustration.  */     
1108       /* Change 16 bit insn to 32 bit insn.  */
1109       opcode[0] |= 0x80;
1110
1111       /* Increase known (fixed) size of fragment.  */
1112       fragP->fr_fix += 2;
1113
1114       /* Create a relocation for it.  */
1115       fix_new (fragP, old_fr_fix, 4,
1116                fragP->fr_symbol,
1117                fragP->fr_offset, 1 /* pcrel */,
1118                /* FIXME: Can't use a real BFD reloc here.
1119                   cgen_md_apply_fix3 can't handle it.  */
1120                BFD_RELOC_M32R_26_PCREL);
1121
1122       /* Mark this fragment as finished.  */
1123       frag_wane (fragP);
1124 #else
1125       {
1126         const CGEN_INSN * insn;
1127         int               i;
1128
1129         /* Update the recorded insn.
1130            Fortunately we don't have to look very far.
1131            FIXME: Change this to record in the instruction the next higher
1132            relaxable insn to use.  */
1133         for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
1134           {
1135             if ((strcmp (CGEN_INSN_MNEMONIC (insn),
1136                          CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
1137                  == 0)
1138                 && CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX))
1139               break;
1140           }
1141         if (i == 4)
1142           abort ();
1143
1144         fragP->fr_cgen.insn = insn;
1145         return 2;
1146       }
1147 #endif
1148     }
1149
1150   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
1151
1152
1153 /* *fragP has been relaxed to its final size, and now needs to have
1154    the bytes inside it modified to conform to the new size.
1155
1156    Called after relaxation is finished.
1157    fragP->fr_type == rs_machine_dependent.
1158    fragP->fr_subtype is the subtype of what the address relaxed to.  */
1159
1160 void
1161 md_convert_frag (abfd, sec, fragP)
1162   bfd *   abfd;
1163   segT    sec;
1164   fragS * fragP;
1165 {
1166   char * opcode;
1167   char * displacement;
1168   int    target_address;
1169   int    opcode_address;
1170   int    extension;
1171   int    addend;
1172
1173   opcode = fragP->fr_opcode;
1174
1175   /* Address opcode resides at in file space.  */
1176   opcode_address = fragP->fr_address + fragP->fr_fix - 2;
1177
1178   switch (fragP->fr_subtype)
1179     {
1180     case 1 :
1181       extension = 0;
1182       displacement = & opcode[1];
1183       break;
1184     case 2 :
1185       opcode[0] |= 0x80;
1186       extension = 2;
1187       displacement = & opcode[1];
1188       break;
1189     case 3 :
1190       opcode[2] = opcode[0] | 0x80;
1191       md_number_to_chars (opcode, PAR_NOP_INSN, 2);
1192       opcode_address += 2;
1193       extension = 4;
1194       displacement = & opcode[3];
1195       break;
1196     default :
1197       abort ();
1198     }
1199
1200   if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
1201     {
1202       /* symbol must be resolved by linker */
1203       if (fragP->fr_offset & 3)
1204         as_warn ("Addend to unresolved symbol not on word boundary.");
1205       addend = fragP->fr_offset >> 2;
1206     }
1207   else
1208     {
1209       /* Address we want to reach in file space.  */
1210       target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1211       target_address += fragP->fr_symbol->sy_frag->fr_address;
1212       addend = (target_address - (opcode_address & -4)) >> 2;
1213     }
1214
1215   /* Create a relocation for symbols that must be resolved by the linker.
1216      Otherwise output the completed insn.  */
1217
1218   if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
1219     {
1220       assert (fragP->fr_subtype != 1);
1221       assert (fragP->fr_cgen.insn != 0);
1222       cgen_record_fixup (fragP,
1223                          /* Offset of branch insn in frag.  */
1224                          fragP->fr_fix + extension - 4,
1225                          fragP->fr_cgen.insn,
1226                          4 /*length*/,
1227                          /* FIXME: quick hack */
1228 #if 0
1229                          CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
1230 #else
1231                          CGEN_OPERAND_ENTRY (M32R_OPERAND_DISP24),
1232 #endif
1233                          fragP->fr_cgen.opinfo,
1234                          fragP->fr_symbol, fragP->fr_offset);
1235     }
1236
1237 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
1238
1239   md_number_to_chars (displacement, (valueT) addend,
1240                       SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
1241
1242   fragP->fr_fix += extension;
1243 }
1244 \f
1245 /* Functions concerning relocs.  */
1246
1247 /* The location from which a PC relative jump should be calculated,
1248    given a PC relative reloc.  */
1249
1250 long
1251 md_pcrel_from_section (fixP, sec)
1252      fixS * fixP;
1253      segT   sec;
1254 {
1255   if (fixP->fx_addsy != (symbolS *) NULL
1256       && (! S_IS_DEFINED (fixP->fx_addsy)
1257           || S_GET_SEGMENT (fixP->fx_addsy) != sec))
1258     {
1259       /* The symbol is undefined (or is defined but not in this section).
1260          Let the linker figure it out.  */
1261       return 0;
1262     }
1263
1264   return (fixP->fx_frag->fr_address + fixP->fx_where) & -4L;
1265 }
1266
1267 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
1268    Returns BFD_RELOC_NONE if no reloc type can be found.
1269    *FIXP may be modified if desired.  */
1270
1271 bfd_reloc_code_real_type
1272 CGEN_SYM (lookup_reloc) (insn, operand, fixP)
1273      const CGEN_INSN *    insn;
1274      const CGEN_OPERAND * operand;
1275      fixS *               fixP;
1276 {
1277   switch (CGEN_OPERAND_TYPE (operand))
1278     {
1279     case M32R_OPERAND_DISP8 : return  BFD_RELOC_M32R_10_PCREL;
1280     case M32R_OPERAND_DISP16 : return BFD_RELOC_M32R_18_PCREL;
1281     case M32R_OPERAND_DISP24 : return BFD_RELOC_M32R_26_PCREL;
1282     case M32R_OPERAND_UIMM24 : return BFD_RELOC_M32R_24;
1283     case M32R_OPERAND_HI16 :
1284     case M32R_OPERAND_SLO16 :
1285     case M32R_OPERAND_ULO16 :
1286       /* If low/high/shigh/sda was used, it is recorded in `opinfo'.  */
1287       if (fixP->tc_fix_data.opinfo != 0)
1288         return fixP->tc_fix_data.opinfo;
1289       break;
1290     }
1291   return BFD_RELOC_NONE;
1292 }
1293
1294 /* Record a HI16 reloc for later matching with its LO16 cousin.  */
1295
1296 static void
1297 m32r_record_hi16 (reloc_type, fixP, seg)
1298      int    reloc_type;
1299      fixS * fixP;
1300      segT   seg;
1301 {
1302   struct m32r_hi_fixup * hi_fixup;
1303
1304   assert (reloc_type == BFD_RELOC_M32R_HI16_SLO
1305           || reloc_type == BFD_RELOC_M32R_HI16_ULO);
1306
1307   hi_fixup = ((struct m32r_hi_fixup *)
1308               xmalloc (sizeof (struct m32r_hi_fixup)));
1309   hi_fixup->fixp = fixP;
1310   hi_fixup->seg  = now_seg;
1311   hi_fixup->next = m32r_hi_fixup_list;
1312   
1313   m32r_hi_fixup_list = hi_fixup;
1314 }
1315
1316 /* Called while parsing an instruction to create a fixup.
1317    We need to check for HI16 relocs and queue them up for later sorting.  */
1318
1319 fixS *
1320 m32r_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
1321      fragS *              frag;
1322      int                  where;
1323      const CGEN_INSN *    insn;
1324      int                  length;
1325      const CGEN_OPERAND * operand;
1326      int                  opinfo;
1327      expressionS *        exp;
1328 {
1329   fixS * fixP = cgen_record_fixup_exp (frag, where, insn, length,
1330                                       operand, opinfo, exp);
1331
1332   switch (CGEN_OPERAND_TYPE (operand))
1333     {
1334     case M32R_OPERAND_HI16 :
1335       /* If low/high/shigh/sda was used, it is recorded in `opinfo'.  */
1336       if (fixP->tc_fix_data.opinfo == BFD_RELOC_M32R_HI16_SLO
1337           || fixP->tc_fix_data.opinfo == BFD_RELOC_M32R_HI16_ULO)
1338         m32r_record_hi16 (fixP->tc_fix_data.opinfo, fixP, now_seg);
1339       break;
1340     }
1341
1342   return fixP;
1343 }
1344
1345 /* Return BFD reloc type from opinfo field in a fixS.
1346    It's tricky using fx_r_type in m32r_frob_file because the values
1347    are BFD_RELOC_UNUSED + operand number.  */
1348 #define FX_OPINFO_R_TYPE(f) ((f)->tc_fix_data.opinfo)
1349
1350 /* Sort any unmatched HI16 relocs so that they immediately precede
1351    the corresponding LO16 reloc.  This is called before md_apply_fix and
1352    tc_gen_reloc.  */
1353
1354 void
1355 m32r_frob_file ()
1356 {
1357   struct m32r_hi_fixup * l;
1358
1359   for (l = m32r_hi_fixup_list; l != NULL; l = l->next)
1360     {
1361       segment_info_type * seginfo;
1362       int                 pass;
1363
1364       assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_SLO
1365               || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_M32R_HI16_ULO);
1366
1367       /* Check quickly whether the next fixup happens to be a matching low.  */
1368       if (l->fixp->fx_next != NULL
1369           && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_M32R_LO16
1370           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
1371           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
1372         continue;
1373
1374       /* Look through the fixups for this segment for a matching `low'.
1375          When we find one, move the high/shigh just in front of it.  We do
1376          this in two passes.  In the first pass, we try to find a
1377          unique `low'.  In the second pass, we permit multiple high's
1378          relocs for a single `low'.  */
1379       seginfo = seg_info (l->seg);
1380       for (pass = 0; pass < 2; pass++)
1381         {
1382           fixS * f;
1383           fixS * prev;
1384
1385           prev = NULL;
1386           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
1387             {
1388               /* Check whether this is a `low' fixup which matches l->fixp.  */
1389               if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_M32R_LO16
1390                   && f->fx_addsy == l->fixp->fx_addsy
1391                   && f->fx_offset == l->fixp->fx_offset
1392                   && (pass == 1
1393                       || prev == NULL
1394                       || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_SLO
1395                           && FX_OPINFO_R_TYPE (prev) != BFD_RELOC_M32R_HI16_ULO)
1396                       || prev->fx_addsy != f->fx_addsy
1397                       || prev->fx_offset !=  f->fx_offset))
1398                 {
1399                   fixS ** pf;
1400
1401                   /* Move l->fixp before f.  */
1402                   for (pf = &seginfo->fix_root;
1403                        * pf != l->fixp;
1404                        pf = & (* pf)->fx_next)
1405                     assert (* pf != NULL);
1406
1407                   * pf = l->fixp->fx_next;
1408
1409                   l->fixp->fx_next = f;
1410                   if (prev == NULL)
1411                     seginfo->fix_root = l->fixp;
1412                   else
1413                     prev->fx_next = l->fixp;
1414
1415                   break;
1416                 }
1417
1418               prev = f;
1419             }
1420
1421           if (f != NULL)
1422             break;
1423
1424           if (pass == 1)
1425             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
1426                            "Unmatched high/shigh reloc");
1427         }
1428     }
1429 }
1430
1431 /* See whether we need to force a relocation into the output file.
1432    This is used to force out switch and PC relative relocations when
1433    relaxing.  */
1434
1435 int
1436 m32r_force_relocation (fix)
1437      fixS * fix;
1438 {
1439   if (! m32r_relax)
1440     return 0;
1441
1442   return (fix->fx_pcrel
1443           || 0 /* ??? */);
1444 }
1445 \f
1446 /* Write a value out to the object file, using the appropriate endianness.  */
1447
1448 void
1449 md_number_to_chars (buf, val, n)
1450      char * buf;
1451      valueT val;
1452      int    n;
1453 {
1454   if (target_big_endian)
1455     number_to_chars_bigendian (buf, val, n);
1456   else
1457     number_to_chars_littleendian (buf, val, n);
1458 }
1459
1460 /* Turn a string in input_line_pointer into a floating point constant of type
1461    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1462    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1463 */
1464
1465 /* Equal to MAX_PRECISION in atof-ieee.c */
1466 #define MAX_LITTLENUMS 6
1467
1468 char *
1469 md_atof (type, litP, sizeP)
1470      char type;
1471      char *litP;
1472      int *sizeP;
1473 {
1474   int              i;
1475   int              prec;
1476   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
1477   LITTLENUM_TYPE * wordP;
1478   char *           t;
1479   char *           atof_ieee ();
1480
1481   switch (type)
1482     {
1483     case 'f':
1484     case 'F':
1485     case 's':
1486     case 'S':
1487       prec = 2;
1488       break;
1489
1490     case 'd':
1491     case 'D':
1492     case 'r':
1493     case 'R':
1494       prec = 4;
1495       break;
1496
1497    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
1498
1499     default:
1500       * sizeP = 0;
1501       return "Bad call to md_atof()";
1502     }
1503
1504   t = atof_ieee (input_line_pointer, type, words);
1505   if (t)
1506     input_line_pointer = t;
1507   * sizeP = prec * sizeof (LITTLENUM_TYPE);
1508
1509   if (target_big_endian)
1510     {
1511       for (i = 0; i < prec; i++)
1512         {
1513           md_number_to_chars (litP, (valueT) words[i],
1514                               sizeof (LITTLENUM_TYPE));
1515           litP += sizeof (LITTLENUM_TYPE);
1516         }
1517     }
1518   else
1519     {
1520       for (i = prec - 1; i >= 0; i--)
1521         {
1522           md_number_to_chars (litP, (valueT) words[i],
1523                               sizeof (LITTLENUM_TYPE));
1524           litP += sizeof (LITTLENUM_TYPE);
1525         }
1526     }
1527      
1528   return 0;
1529 }