Remove duplicate definitions of the md_atof() function
[external/binutils.git] / gas / config / tc-mn10300.c
1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3    2006, 2007 Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "opcode/mn10300.h"
26 #include "dwarf2dbg.h"
27 \f
28 /* Structure to hold information about predefined registers.  */
29 struct reg_name
30 {
31   const char *name;
32   int value;
33 };
34
35 /* Generic assembler global variables which must be defined by all
36    targets.  */
37
38 /* Characters which always start a comment.  */
39 const char comment_chars[] = "#";
40
41 /* Characters which start a comment at the beginning of a line.  */
42 const char line_comment_chars[] = ";#";
43
44 /* Characters which may be used to separate multiple commands on a
45    single line.  */
46 const char line_separator_chars[] = ";";
47
48 /* Characters which are used to indicate an exponent in a floating
49    point number.  */
50 const char EXP_CHARS[] = "eE";
51
52 /* Characters which mean that a number is a floating point constant,
53    as in 0d1.0.  */
54 const char FLT_CHARS[] = "dD";
55 \f
56 const relax_typeS md_relax_table[] =
57 {
58   /* The plus values for the bCC and fBCC instructions in the table below
59      are because the branch instruction is translated into a jump
60      instruction that is now +2 or +3 bytes further on in memory, and the
61      correct size of jump instruction must be selected.  */
62   /* bCC relaxing  */
63   {0x7f, -0x80, 2, 1},
64   {0x7fff + 2, -0x8000 + 2, 5, 2},
65   {0x7fffffff, -0x80000000, 7, 0},
66
67   /* bCC relaxing (uncommon cases for 3byte length instructions)  */
68   {0x7f, -0x80, 3, 4},
69   {0x7fff + 3, -0x8000 + 3, 6, 5},
70   {0x7fffffff, -0x80000000, 8, 0},
71
72   /* call relaxing  */
73   {0x7fff, -0x8000, 5, 7},
74   {0x7fffffff, -0x80000000, 7, 0},
75
76   /* calls relaxing  */
77   {0x7fff, -0x8000, 4, 9},
78   {0x7fffffff, -0x80000000, 6, 0},
79
80   /* jmp relaxing  */
81   {0x7f, -0x80, 2, 11},
82   {0x7fff, -0x8000, 3, 12},
83   {0x7fffffff, -0x80000000, 5, 0},
84
85   /* fbCC relaxing  */
86   {0x7f, -0x80, 3, 14},
87   {0x7fff + 3, -0x8000 + 3, 6, 15},
88   {0x7fffffff, -0x80000000, 8, 0},
89
90 };
91
92 /* Local functions.  */
93 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
94                                             const struct mn10300_operand *,
95                                             offsetT, char *, unsigned,
96                                             unsigned));
97 static unsigned long check_operand PARAMS ((unsigned long,
98                                             const struct mn10300_operand *,
99                                             offsetT));
100 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
101 static bfd_boolean data_register_name PARAMS ((expressionS *expressionP));
102 static bfd_boolean address_register_name PARAMS ((expressionS *expressionP));
103 static bfd_boolean other_register_name PARAMS ((expressionS *expressionP));
104 static bfd_boolean r_register_name PARAMS ((expressionS *expressionP));
105 static bfd_boolean xr_register_name PARAMS ((expressionS *expressionP));
106 static void set_arch_mach PARAMS ((int));
107
108 /*  Set linkrelax here to avoid fixups in most sections.  */
109 int linkrelax = 1;
110
111 static int current_machine;
112
113 /* Fixups.  */
114 #define MAX_INSN_FIXUPS (5)
115 struct mn10300_fixup
116 {
117   expressionS exp;
118   int opindex;
119   bfd_reloc_code_real_type reloc;
120 };
121 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
122 static int fc;
123
124 /* We must store the value of each register operand so that we can
125    verify that certain registers do not match.  */
126 int mn10300_reg_operands[MN10300_MAX_OPERANDS];
127 \f
128 const char *md_shortopts = "";
129 struct option md_longopts[] = {
130   {NULL, no_argument, NULL, 0}
131 };
132 size_t md_longopts_size = sizeof (md_longopts);
133
134 /* The target specific pseudo-ops which we support.  */
135 const pseudo_typeS md_pseudo_table[] =
136 {
137   { "am30",     set_arch_mach,          AM30 },
138   { "am33",     set_arch_mach,          AM33 },
139   { "am33_2",   (void (*) PARAMS ((int))) set_arch_mach, AM33_2 },
140   { "mn10300",  set_arch_mach,          MN103 },
141   {NULL, 0, 0}
142 };
143
144 #define HAVE_AM33_2 (current_machine == AM33_2)
145 #define HAVE_AM33 (current_machine == AM33 || HAVE_AM33_2)
146 #define HAVE_AM30 (current_machine == AM30)
147
148 /* Opcode hash table.  */
149 static struct hash_control *mn10300_hash;
150
151 /* This table is sorted. Suitable for searching by a binary search.  */
152 static const struct reg_name data_registers[] =
153 {
154   { "d0", 0 },
155   { "d1", 1 },
156   { "d2", 2 },
157   { "d3", 3 },
158 };
159 #define DATA_REG_NAME_CNT                               \
160   (sizeof (data_registers) / sizeof (struct reg_name))
161
162 static const struct reg_name address_registers[] =
163 {
164   { "a0", 0 },
165   { "a1", 1 },
166   { "a2", 2 },
167   { "a3", 3 },
168 };
169
170 #define ADDRESS_REG_NAME_CNT                                    \
171   (sizeof (address_registers) / sizeof (struct reg_name))
172
173 static const struct reg_name r_registers[] =
174 {
175   { "a0", 8 },
176   { "a1", 9 },
177   { "a2", 10 },
178   { "a3", 11 },
179   { "d0", 12 },
180   { "d1", 13 },
181   { "d2", 14 },
182   { "d3", 15 },
183   { "e0", 0 },
184   { "e1", 1 },
185   { "e10", 10 },
186   { "e11", 11 },
187   { "e12", 12 },
188   { "e13", 13 },
189   { "e14", 14 },
190   { "e15", 15 },
191   { "e2", 2 },
192   { "e3", 3 },
193   { "e4", 4 },
194   { "e5", 5 },
195   { "e6", 6 },
196   { "e7", 7 },
197   { "e8", 8 },
198   { "e9", 9 },
199   { "r0", 0 },
200   { "r1", 1 },
201   { "r10", 10 },
202   { "r11", 11 },
203   { "r12", 12 },
204   { "r13", 13 },
205   { "r14", 14 },
206   { "r15", 15 },
207   { "r2", 2 },
208   { "r3", 3 },
209   { "r4", 4 },
210   { "r5", 5 },
211   { "r6", 6 },
212   { "r7", 7 },
213   { "r8", 8 },
214   { "r9", 9 },
215 };
216
217 #define R_REG_NAME_CNT                                  \
218   (sizeof (r_registers) / sizeof (struct reg_name))
219
220 static const struct reg_name xr_registers[] =
221 {
222   { "mcrh", 2 },
223   { "mcrl", 3 },
224   { "mcvf", 4 },
225   { "mdrq", 1 },
226   { "sp", 0 },
227   { "xr0", 0 },
228   { "xr1", 1 },
229   { "xr10", 10 },
230   { "xr11", 11 },
231   { "xr12", 12 },
232   { "xr13", 13 },
233   { "xr14", 14 },
234   { "xr15", 15 },
235   { "xr2", 2 },
236   { "xr3", 3 },
237   { "xr4", 4 },
238   { "xr5", 5 },
239   { "xr6", 6 },
240   { "xr7", 7 },
241   { "xr8", 8 },
242   { "xr9", 9 },
243 };
244
245 #define XR_REG_NAME_CNT                                 \
246   (sizeof (xr_registers) / sizeof (struct reg_name))
247
248 /* We abuse the `value' field, that would be otherwise unused, to
249    encode the architecture on which (access to) the register was
250    introduced.  FIXME: we should probably warn when we encounter a
251    register name when assembling for an architecture that doesn't
252    support it, before parsing it as a symbol name.  */
253 static const struct reg_name other_registers[] =
254 {
255   { "epsw", AM33 },
256   { "mdr", 0 },
257   { "pc", AM33 },
258   { "psw", 0 },
259   { "sp", 0 },
260 };
261
262 #define OTHER_REG_NAME_CNT                              \
263   (sizeof (other_registers) / sizeof (struct reg_name))
264
265 static const struct reg_name float_registers[] =
266 {
267   { "fs0", 0 },
268   { "fs1", 1 },
269   { "fs10", 10 },
270   { "fs11", 11 },
271   { "fs12", 12 },
272   { "fs13", 13 },
273   { "fs14", 14 },
274   { "fs15", 15 },
275   { "fs16", 16 },
276   { "fs17", 17 },
277   { "fs18", 18 },
278   { "fs19", 19 },
279   { "fs2",   2 },
280   { "fs20", 20 },
281   { "fs21", 21 },
282   { "fs22", 22 },
283   { "fs23", 23 },
284   { "fs24", 24 },
285   { "fs25", 25 },
286   { "fs26", 26 },
287   { "fs27", 27 },
288   { "fs28", 28 },
289   { "fs29", 29 },
290   { "fs3",   3 },
291   { "fs30", 30 },
292   { "fs31", 31 },
293   { "fs4",   4 },
294   { "fs5",   5 },
295   { "fs6",   6 },
296   { "fs7",   7 },
297   { "fs8",   8 },
298   { "fs9",   9 },
299 };
300
301 #define FLOAT_REG_NAME_CNT \
302   (sizeof (float_registers) / sizeof (struct reg_name))
303
304 static const struct reg_name double_registers[] =
305 {
306   { "fd0",   0 },
307   { "fd10", 10 },
308   { "fd12", 12 },
309   { "fd14", 14 },
310   { "fd16", 16 },
311   { "fd18", 18 },
312   { "fd2",   2 },
313   { "fd20", 20 },
314   { "fd22", 22 },
315   { "fd24", 24 },
316   { "fd26", 26 },
317   { "fd28", 28 },
318   { "fd30", 30 },
319   { "fd4",   4 },
320   { "fd6",   6 },
321   { "fd8",   8 },
322 };
323
324 #define DOUBLE_REG_NAME_CNT \
325   (sizeof (double_registers) / sizeof (struct reg_name))
326
327
328 /* reg_name_search does a binary search of the given register table
329    to see if "name" is a valid regiter name.  Returns the register
330    number from the array on success, or -1 on failure.  */
331
332 static int
333 reg_name_search (regs, regcount, name)
334      const struct reg_name *regs;
335      int regcount;
336      const char *name;
337 {
338   int middle, low, high;
339   int cmp;
340
341   low = 0;
342   high = regcount - 1;
343
344   do
345     {
346       middle = (low + high) / 2;
347       cmp = strcasecmp (name, regs[middle].name);
348       if (cmp < 0)
349         high = middle - 1;
350       else if (cmp > 0)
351         low = middle + 1;
352       else
353         return regs[middle].value;
354     }
355   while (low <= high);
356   return -1;
357 }
358
359 /* Summary of register_name().
360  *
361  * in: Input_line_pointer points to 1st char of operand.
362  *
363  * out: An expressionS.
364  *      The operand may have been a register: in this case, X_op == O_register,
365  *      X_add_number is set to the register number, and truth is returned.
366  *      Input_line_pointer->(next non-blank) char after operand, or is in
367  *      its original state.
368  */
369
370 static bfd_boolean
371 r_register_name (expressionP)
372      expressionS *expressionP;
373 {
374   int reg_number;
375   char *name;
376   char *start;
377   char c;
378
379   /* Find the spelling of the operand.  */
380   start = name = input_line_pointer;
381
382   c = get_symbol_end ();
383   reg_number = reg_name_search (r_registers, R_REG_NAME_CNT, name);
384
385   /* Put back the delimiting char.  */
386   *input_line_pointer = c;
387
388   /* Look to see if it's in the register table.  */
389   if (reg_number >= 0)
390     {
391       expressionP->X_op = O_register;
392       expressionP->X_add_number = reg_number;
393
394       /* Make the rest nice.  */
395       expressionP->X_add_symbol = NULL;
396       expressionP->X_op_symbol = NULL;
397
398       return TRUE;
399     }
400
401   /* Reset the line as if we had not done anything.  */
402   input_line_pointer = start;
403   return FALSE;
404 }
405
406 /* Summary of register_name().
407  *
408  * in: Input_line_pointer points to 1st char of operand.
409  *
410  * out: An expressionS.
411  *      The operand may have been a register: in this case, X_op == O_register,
412  *      X_add_number is set to the register number, and truth is returned.
413  *      Input_line_pointer->(next non-blank) char after operand, or is in
414  *      its original state.
415  */
416
417 static bfd_boolean
418 xr_register_name (expressionP)
419      expressionS *expressionP;
420 {
421   int reg_number;
422   char *name;
423   char *start;
424   char c;
425
426   /* Find the spelling of the operand.  */
427   start = name = input_line_pointer;
428
429   c = get_symbol_end ();
430   reg_number = reg_name_search (xr_registers, XR_REG_NAME_CNT, name);
431
432   /* Put back the delimiting char.  */
433   *input_line_pointer = c;
434
435   /* Look to see if it's in the register table.  */
436   if (reg_number >= 0)
437     {
438       expressionP->X_op = O_register;
439       expressionP->X_add_number = reg_number;
440
441       /* Make the rest nice.  */
442       expressionP->X_add_symbol = NULL;
443       expressionP->X_op_symbol = NULL;
444
445       return TRUE;
446     }
447
448   /* Reset the line as if we had not done anything.  */
449   input_line_pointer = start;
450   return FALSE;
451 }
452
453 /* Summary of register_name().
454  *
455  * in: Input_line_pointer points to 1st char of operand.
456  *
457  * out: An expressionS.
458  *      The operand may have been a register: in this case, X_op == O_register,
459  *      X_add_number is set to the register number, and truth is returned.
460  *      Input_line_pointer->(next non-blank) char after operand, or is in
461  *      its original state.
462  */
463
464 static bfd_boolean
465 data_register_name (expressionP)
466      expressionS *expressionP;
467 {
468   int reg_number;
469   char *name;
470   char *start;
471   char c;
472
473   /* Find the spelling of the operand.  */
474   start = name = input_line_pointer;
475
476   c = get_symbol_end ();
477   reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
478
479   /* Put back the delimiting char.  */
480   *input_line_pointer = c;
481
482   /* Look to see if it's in the register table.  */
483   if (reg_number >= 0)
484     {
485       expressionP->X_op = O_register;
486       expressionP->X_add_number = reg_number;
487
488       /* Make the rest nice.  */
489       expressionP->X_add_symbol = NULL;
490       expressionP->X_op_symbol = NULL;
491
492       return TRUE;
493     }
494
495   /* Reset the line as if we had not done anything.  */
496   input_line_pointer = start;
497   return FALSE;
498 }
499
500 /* Summary of register_name().
501  *
502  * in: Input_line_pointer points to 1st char of operand.
503  *
504  * out: An expressionS.
505  *      The operand may have been a register: in this case, X_op == O_register,
506  *      X_add_number is set to the register number, and truth is returned.
507  *      Input_line_pointer->(next non-blank) char after operand, or is in
508  *      its original state.
509  */
510
511 static bfd_boolean
512 address_register_name (expressionP)
513      expressionS *expressionP;
514 {
515   int reg_number;
516   char *name;
517   char *start;
518   char c;
519
520   /* Find the spelling of the operand.  */
521   start = name = input_line_pointer;
522
523   c = get_symbol_end ();
524   reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
525
526   /* Put back the delimiting char.  */
527   *input_line_pointer = c;
528
529   /* Look to see if it's in the register table.  */
530   if (reg_number >= 0)
531     {
532       expressionP->X_op = O_register;
533       expressionP->X_add_number = reg_number;
534
535       /* Make the rest nice.  */
536       expressionP->X_add_symbol = NULL;
537       expressionP->X_op_symbol = NULL;
538
539       return TRUE;
540     }
541
542   /* Reset the line as if we had not done anything.  */
543   input_line_pointer = start;
544   return FALSE;
545 }
546
547 /* Summary of register_name().
548  *
549  * in: Input_line_pointer points to 1st char of operand.
550  *
551  * out: An expressionS.
552  *      The operand may have been a register: in this case, X_op == O_register,
553  *      X_add_number is set to the register number, and truth is returned.
554  *      Input_line_pointer->(next non-blank) char after operand, or is in
555  *      its original state.
556  */
557
558 static bfd_boolean
559 other_register_name (expressionP)
560      expressionS *expressionP;
561 {
562   int reg_number;
563   char *name;
564   char *start;
565   char c;
566
567   /* Find the spelling of the operand.  */
568   start = name = input_line_pointer;
569
570   c = get_symbol_end ();
571   reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
572
573   /* Put back the delimiting char.  */
574   *input_line_pointer = c;
575
576   /* Look to see if it's in the register table.  */
577   if (reg_number == 0
578       || (reg_number == AM33 && HAVE_AM33))
579     {
580       expressionP->X_op = O_register;
581       expressionP->X_add_number = 0;
582
583       /* Make the rest nice.  */
584       expressionP->X_add_symbol = NULL;
585       expressionP->X_op_symbol = NULL;
586
587       return TRUE;
588     }
589
590   /* Reset the line as if we had not done anything.  */
591   input_line_pointer = start;
592   return FALSE;
593 }
594
595 static bfd_boolean double_register_name PARAMS ((expressionS *));
596 static bfd_boolean float_register_name  PARAMS ((expressionS *));
597
598 /* Summary of float_register_name:
599
600    in: Input_line_pointer points to 1st char of operand.
601
602    out: A expressionS.
603         The operand may have been a register: in this case, X_op == O_register,
604         X_add_number is set to the register number, and truth is returned.
605         Input_line_pointer->(next non-blank) char after operand, or is in
606         its original state.  */
607
608 static bfd_boolean
609 float_register_name (expressionP)
610      expressionS *expressionP;
611 {
612   int reg_number;
613   char *name;
614   char *start;
615   char c;
616
617   /* Find the spelling of the operand.  */
618   start = name = input_line_pointer;
619
620   c = get_symbol_end ();
621   reg_number = reg_name_search (float_registers, FLOAT_REG_NAME_CNT, name);
622
623   /* Put back the delimiting char.  */
624   * input_line_pointer = c;
625
626   /* Look to see if it's in the register table.  */
627   if (reg_number >= 0)
628     {
629       expressionP->X_op = O_register;
630       expressionP->X_add_number = reg_number;
631
632       /* Make the rest nice.  */
633       expressionP->X_add_symbol = NULL;
634       expressionP->X_op_symbol = NULL;
635
636       return TRUE;
637     }
638
639   /* Reset the line as if we had not done anything.  */
640   input_line_pointer = start;
641   return FALSE;
642 }
643
644 /* Summary of double_register_name:
645
646    in: Input_line_pointer points to 1st char of operand.
647
648    out: A expressionS.
649         The operand may have been a register: in this case, X_op == O_register,
650         X_add_number is set to the register number, and truth is returned.
651         Input_line_pointer->(next non-blank) char after operand, or is in
652         its original state.  */
653
654 static bfd_boolean
655 double_register_name (expressionP)
656      expressionS *expressionP;
657 {
658   int reg_number;
659   char *name;
660   char *start;
661   char c;
662
663   /* Find the spelling of the operand.  */
664   start = name = input_line_pointer;
665
666   c = get_symbol_end ();
667   reg_number = reg_name_search (double_registers, DOUBLE_REG_NAME_CNT, name);
668
669   /* Put back the delimiting char.  */
670   * input_line_pointer = c;
671
672   /* Look to see if it's in the register table.  */
673   if (reg_number >= 0)
674     {
675       expressionP->X_op = O_register;
676       expressionP->X_add_number = reg_number;
677
678       /* Make the rest nice.  */
679       expressionP->X_add_symbol = NULL;
680       expressionP->X_op_symbol = NULL;
681
682       return TRUE;
683     }
684
685   /* Reset the line as if we had not done anything.  */
686   input_line_pointer = start;
687   return FALSE;
688 }
689
690 void
691 md_show_usage (stream)
692      FILE *stream;
693 {
694   fprintf (stream, _("MN10300 assembler options:\n\
695 none yet\n"));
696 }
697
698 int
699 md_parse_option (c, arg)
700      int c ATTRIBUTE_UNUSED;
701      char *arg ATTRIBUTE_UNUSED;
702 {
703   return 0;
704 }
705
706 symbolS *
707 md_undefined_symbol (name)
708      char *name ATTRIBUTE_UNUSED;
709 {
710   return 0;
711 }
712
713 char *
714 md_atof (int type, char *litp, int *sizep)
715 {
716   return ieee_md_atof (type, litp, sizep, FALSE);
717 }
718
719 void
720 md_convert_frag (abfd, sec, fragP)
721      bfd *abfd ATTRIBUTE_UNUSED;
722      asection *sec;
723      fragS *fragP;
724 {
725   static unsigned long label_count = 0;
726   char buf[40];
727
728   subseg_change (sec, 0);
729   if (fragP->fr_subtype == 0)
730     {
731       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
732                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
733       fragP->fr_var = 0;
734       fragP->fr_fix += 2;
735     }
736   else if (fragP->fr_subtype == 1)
737     {
738       /* Reverse the condition of the first branch.  */
739       int offset = fragP->fr_fix;
740       int opcode = fragP->fr_literal[offset] & 0xff;
741
742       switch (opcode)
743         {
744         case 0xc8:
745           opcode = 0xc9;
746           break;
747         case 0xc9:
748           opcode = 0xc8;
749           break;
750         case 0xc0:
751           opcode = 0xc2;
752           break;
753         case 0xc2:
754           opcode = 0xc0;
755           break;
756         case 0xc3:
757           opcode = 0xc1;
758           break;
759         case 0xc1:
760           opcode = 0xc3;
761           break;
762         case 0xc4:
763           opcode = 0xc6;
764           break;
765         case 0xc6:
766           opcode = 0xc4;
767           break;
768         case 0xc7:
769           opcode = 0xc5;
770           break;
771         case 0xc5:
772           opcode = 0xc7;
773           break;
774         default:
775           abort ();
776         }
777       fragP->fr_literal[offset] = opcode;
778
779       /* Create a fixup for the reversed conditional branch.  */
780       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
781       fix_new (fragP, fragP->fr_fix + 1, 1,
782                symbol_new (buf, sec, 0, fragP->fr_next),
783                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
784
785       /* Now create the unconditional branch + fixup to the
786          final target.  */
787       fragP->fr_literal[offset + 2] = 0xcc;
788       fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
789                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
790       fragP->fr_var = 0;
791       fragP->fr_fix += 5;
792     }
793   else if (fragP->fr_subtype == 2)
794     {
795       /* Reverse the condition of the first branch.  */
796       int offset = fragP->fr_fix;
797       int opcode = fragP->fr_literal[offset] & 0xff;
798
799       switch (opcode)
800         {
801         case 0xc8:
802           opcode = 0xc9;
803           break;
804         case 0xc9:
805           opcode = 0xc8;
806           break;
807         case 0xc0:
808           opcode = 0xc2;
809           break;
810         case 0xc2:
811           opcode = 0xc0;
812           break;
813         case 0xc3:
814           opcode = 0xc1;
815           break;
816         case 0xc1:
817           opcode = 0xc3;
818           break;
819         case 0xc4:
820           opcode = 0xc6;
821           break;
822         case 0xc6:
823           opcode = 0xc4;
824           break;
825         case 0xc7:
826           opcode = 0xc5;
827           break;
828         case 0xc5:
829           opcode = 0xc7;
830           break;
831         default:
832           abort ();
833         }
834       fragP->fr_literal[offset] = opcode;
835
836       /* Create a fixup for the reversed conditional branch.  */
837       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
838       fix_new (fragP, fragP->fr_fix + 1, 1,
839                symbol_new (buf, sec, 0, fragP->fr_next),
840                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
841
842       /* Now create the unconditional branch + fixup to the
843          final target.  */
844       fragP->fr_literal[offset + 2] = 0xdc;
845       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
846                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
847       fragP->fr_var = 0;
848       fragP->fr_fix += 7;
849     }
850   else if (fragP->fr_subtype == 3)
851     {
852       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
853                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
854       fragP->fr_var = 0;
855       fragP->fr_fix += 3;
856     }
857   else if (fragP->fr_subtype == 4)
858     {
859       /* Reverse the condition of the first branch.  */
860       int offset = fragP->fr_fix;
861       int opcode = fragP->fr_literal[offset + 1] & 0xff;
862
863       switch (opcode)
864         {
865         case 0xe8:
866           opcode = 0xe9;
867           break;
868         case 0xe9:
869           opcode = 0xe8;
870           break;
871         case 0xea:
872           opcode = 0xeb;
873           break;
874         case 0xeb:
875           opcode = 0xea;
876           break;
877         default:
878           abort ();
879         }
880       fragP->fr_literal[offset + 1] = opcode;
881
882       /* Create a fixup for the reversed conditional branch.  */
883       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
884       fix_new (fragP, fragP->fr_fix + 2, 1,
885                symbol_new (buf, sec, 0, fragP->fr_next),
886                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
887
888       /* Now create the unconditional branch + fixup to the
889          final target.  */
890       fragP->fr_literal[offset + 3] = 0xcc;
891       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
892                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
893       fragP->fr_var = 0;
894       fragP->fr_fix += 6;
895     }
896   else if (fragP->fr_subtype == 5)
897     {
898       /* Reverse the condition of the first branch.  */
899       int offset = fragP->fr_fix;
900       int opcode = fragP->fr_literal[offset + 1] & 0xff;
901
902       switch (opcode)
903         {
904         case 0xe8:
905           opcode = 0xe9;
906           break;
907         case 0xea:
908           opcode = 0xeb;
909           break;
910         case 0xeb:
911           opcode = 0xea;
912           break;
913         default:
914           abort ();
915         }
916       fragP->fr_literal[offset + 1] = opcode;
917
918       /* Create a fixup for the reversed conditional branch.  */
919       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
920       fix_new (fragP, fragP->fr_fix + 2, 1,
921                symbol_new (buf, sec, 0, fragP->fr_next),
922                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
923
924       /* Now create the unconditional branch + fixup to the
925          final target.  */
926       fragP->fr_literal[offset + 3] = 0xdc;
927       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
928                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
929       fragP->fr_var = 0;
930       fragP->fr_fix += 8;
931     }
932   else if (fragP->fr_subtype == 6)
933     {
934       int offset = fragP->fr_fix;
935       fragP->fr_literal[offset] = 0xcd;
936       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
937                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
938       fragP->fr_var = 0;
939       fragP->fr_fix += 5;
940     }
941   else if (fragP->fr_subtype == 7)
942     {
943       int offset = fragP->fr_fix;
944       fragP->fr_literal[offset] = 0xdd;
945       fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
946       fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
947
948       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
949                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
950       fragP->fr_var = 0;
951       fragP->fr_fix += 7;
952     }
953   else if (fragP->fr_subtype == 8)
954     {
955       int offset = fragP->fr_fix;
956       fragP->fr_literal[offset] = 0xfa;
957       fragP->fr_literal[offset + 1] = 0xff;
958       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
959                fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
960       fragP->fr_var = 0;
961       fragP->fr_fix += 4;
962     }
963   else if (fragP->fr_subtype == 9)
964     {
965       int offset = fragP->fr_fix;
966       fragP->fr_literal[offset] = 0xfc;
967       fragP->fr_literal[offset + 1] = 0xff;
968
969       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
970                fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
971       fragP->fr_var = 0;
972       fragP->fr_fix += 6;
973     }
974   else if (fragP->fr_subtype == 10)
975     {
976       fragP->fr_literal[fragP->fr_fix] = 0xca;
977       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
978                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
979       fragP->fr_var = 0;
980       fragP->fr_fix += 2;
981     }
982   else if (fragP->fr_subtype == 11)
983     {
984       int offset = fragP->fr_fix;
985       fragP->fr_literal[offset] = 0xcc;
986
987       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
988                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
989       fragP->fr_var = 0;
990       fragP->fr_fix += 3;
991     }
992   else if (fragP->fr_subtype == 12)
993     {
994       int offset = fragP->fr_fix;
995       fragP->fr_literal[offset] = 0xdc;
996
997       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
998                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
999       fragP->fr_var = 0;
1000       fragP->fr_fix += 5;
1001     }
1002   else if (fragP->fr_subtype == 13)
1003     {
1004       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
1005                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
1006       fragP->fr_var = 0;
1007       fragP->fr_fix += 3;
1008     }
1009   else if (fragP->fr_subtype == 14)
1010     {
1011       /* Reverse the condition of the first branch.  */
1012       int offset = fragP->fr_fix;
1013       int opcode = fragP->fr_literal[offset + 1] & 0xff;
1014
1015       switch (opcode)
1016         {
1017         case 0xd0:
1018           opcode = 0xd1;
1019           break;
1020         case 0xd1:
1021           opcode = 0xd0;
1022           break;
1023         case 0xd2:
1024           opcode = 0xdc;
1025           break;
1026         case 0xd3:
1027           opcode = 0xdb;
1028           break;
1029         case 0xd4:
1030           opcode = 0xda;
1031           break;
1032         case 0xd5:
1033           opcode = 0xd9;
1034           break;
1035         case 0xd6:
1036           opcode = 0xd8;
1037           break;
1038         case 0xd7:
1039           opcode = 0xdd;
1040           break;
1041         case 0xd8:
1042           opcode = 0xd6;
1043           break;
1044         case 0xd9:
1045           opcode = 0xd5;
1046           break;
1047         case 0xda:
1048           opcode = 0xd4;
1049           break;
1050         case 0xdb:
1051           opcode = 0xd3;
1052           break;
1053         case 0xdc:
1054           opcode = 0xd2;
1055           break;
1056         case 0xdd:
1057           opcode = 0xd7;
1058           break;
1059         default:
1060           abort ();
1061         }
1062       fragP->fr_literal[offset + 1] = opcode;
1063
1064       /* Create a fixup for the reversed conditional branch.  */
1065       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
1066       fix_new (fragP, fragP->fr_fix + 2, 1,
1067                symbol_new (buf, sec, 0, fragP->fr_next),
1068                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
1069
1070       /* Now create the unconditional branch + fixup to the
1071          final target.  */
1072       fragP->fr_literal[offset + 3] = 0xcc;
1073       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
1074                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
1075       fragP->fr_var = 0;
1076       fragP->fr_fix += 6;
1077     }
1078   else if (fragP->fr_subtype == 15)
1079     {
1080       /* Reverse the condition of the first branch.  */
1081       int offset = fragP->fr_fix;
1082       int opcode = fragP->fr_literal[offset + 1] & 0xff;
1083
1084       switch (opcode)
1085         {
1086         case 0xd0:
1087           opcode = 0xd1;
1088           break;
1089         case 0xd1:
1090           opcode = 0xd0;
1091           break;
1092         case 0xd2:
1093           opcode = 0xdc;
1094           break;
1095         case 0xd3:
1096           opcode = 0xdb;
1097           break;
1098         case 0xd4:
1099           opcode = 0xda;
1100           break;
1101         case 0xd5:
1102           opcode = 0xd9;
1103           break;
1104         case 0xd6:
1105           opcode = 0xd8;
1106           break;
1107         case 0xd7:
1108           opcode = 0xdd;
1109           break;
1110         case 0xd8:
1111           opcode = 0xd6;
1112           break;
1113         case 0xd9:
1114           opcode = 0xd5;
1115           break;
1116         case 0xda:
1117           opcode = 0xd4;
1118           break;
1119         case 0xdb:
1120           opcode = 0xd3;
1121           break;
1122         case 0xdc:
1123           opcode = 0xd2;
1124           break;
1125         case 0xdd:
1126           opcode = 0xd7;
1127           break;
1128         default:
1129           abort ();
1130         }
1131       fragP->fr_literal[offset + 1] = opcode;
1132
1133       /* Create a fixup for the reversed conditional branch.  */
1134       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
1135       fix_new (fragP, fragP->fr_fix + 2, 1,
1136                symbol_new (buf, sec, 0, fragP->fr_next),
1137                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
1138
1139       /* Now create the unconditional branch + fixup to the
1140          final target.  */
1141       fragP->fr_literal[offset + 3] = 0xdc;
1142       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
1143                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
1144       fragP->fr_var = 0;
1145       fragP->fr_fix += 8;
1146     }
1147   else
1148     abort ();
1149 }
1150
1151 valueT
1152 md_section_align (seg, addr)
1153      asection *seg;
1154      valueT addr;
1155 {
1156   int align = bfd_get_section_alignment (stdoutput, seg);
1157   return ((addr + (1 << align) - 1) & (-1 << align));
1158 }
1159
1160 void
1161 md_begin ()
1162 {
1163   char *prev_name = "";
1164   register const struct mn10300_opcode *op;
1165
1166   mn10300_hash = hash_new ();
1167
1168   /* Insert unique names into hash table.  The MN10300 instruction set
1169      has many identical opcode names that have different opcodes based
1170      on the operands.  This hash table then provides a quick index to
1171      the first opcode with a particular name in the opcode table.  */
1172
1173   op = mn10300_opcodes;
1174   while (op->name)
1175     {
1176       if (strcmp (prev_name, op->name))
1177         {
1178           prev_name = (char *) op->name;
1179           hash_insert (mn10300_hash, op->name, (char *) op);
1180         }
1181       op++;
1182     }
1183
1184   /* Set the default machine type.  */
1185 #ifdef TE_LINUX
1186   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, AM33_2))
1187     as_warn (_("could not set architecture and machine"));
1188
1189   current_machine = AM33_2;
1190 #else  
1191   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, MN103))
1192     as_warn (_("could not set architecture and machine"));
1193
1194   current_machine = MN103;
1195 #endif
1196 }
1197
1198 static symbolS *GOT_symbol;
1199
1200 static inline int mn10300_check_fixup PARAMS ((struct mn10300_fixup *));
1201 static inline int mn10300_PIC_related_p PARAMS ((symbolS *));
1202
1203 static inline int
1204 mn10300_PIC_related_p (sym)
1205      symbolS *sym;
1206 {
1207   expressionS *exp;
1208
1209   if (! sym)
1210     return 0;
1211
1212   if (sym == GOT_symbol)
1213     return 1;
1214
1215   exp = symbol_get_value_expression (sym);
1216
1217   return (exp->X_op == O_PIC_reloc
1218           || mn10300_PIC_related_p (exp->X_add_symbol)
1219           || mn10300_PIC_related_p (exp->X_op_symbol));
1220 }
1221
1222 static inline int
1223 mn10300_check_fixup (fixup)
1224      struct mn10300_fixup *fixup;
1225 {
1226   expressionS *exp = &fixup->exp;
1227
1228  repeat:
1229   switch (exp->X_op)
1230     {
1231     case O_add:
1232     case O_subtract: /* If we're sufficiently unlucky that the label
1233                         and the expression that references it happen
1234                         to end up in different frags, the subtract
1235                         won't be simplified within expression().  */
1236       /* The PIC-related operand must be the first operand of a sum.  */
1237       if (exp != &fixup->exp || mn10300_PIC_related_p (exp->X_op_symbol))
1238         return 1;
1239
1240       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
1241         fixup->reloc = BFD_RELOC_32_GOT_PCREL;
1242
1243       exp = symbol_get_value_expression (exp->X_add_symbol);
1244       goto repeat;
1245
1246     case O_symbol:
1247       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
1248         fixup->reloc = BFD_RELOC_32_GOT_PCREL;
1249       break;
1250
1251     case O_PIC_reloc:
1252       fixup->reloc = exp->X_md;
1253       exp->X_op = O_symbol;
1254       if (fixup->reloc == BFD_RELOC_32_PLT_PCREL
1255           && fixup->opindex >= 0
1256           && (mn10300_operands[fixup->opindex].flags
1257               & MN10300_OPERAND_RELAX))
1258         return 1;
1259       break;
1260
1261     default:
1262       return (mn10300_PIC_related_p (exp->X_add_symbol)
1263               || mn10300_PIC_related_p (exp->X_op_symbol));
1264     }
1265
1266   return 0;
1267 }
1268
1269 void
1270 mn10300_cons_fix_new (frag, off, size, exp)
1271      fragS *frag;
1272      int off, size;
1273      expressionS *exp;
1274 {
1275   struct mn10300_fixup fixup;
1276
1277   fixup.opindex = -1;
1278   fixup.exp = *exp;
1279   fixup.reloc = BFD_RELOC_UNUSED;
1280
1281   mn10300_check_fixup (&fixup);
1282
1283   if (fixup.reloc == BFD_RELOC_MN10300_GOT32)
1284     switch (size)
1285       {
1286       case 2:
1287         fixup.reloc = BFD_RELOC_MN10300_GOT16;
1288         break;
1289
1290       case 3:
1291         fixup.reloc = BFD_RELOC_MN10300_GOT24;
1292         break;
1293
1294       case 4:
1295         break;
1296
1297       default:
1298         goto error;
1299       }
1300   else if (fixup.reloc == BFD_RELOC_UNUSED)
1301     switch (size)
1302       {
1303       case 1:
1304         fixup.reloc = BFD_RELOC_8;
1305         break;
1306
1307       case 2:
1308         fixup.reloc = BFD_RELOC_16;
1309         break;
1310
1311       case 3:
1312         fixup.reloc = BFD_RELOC_24;
1313         break;
1314
1315       case 4:
1316         fixup.reloc = BFD_RELOC_32;
1317         break;
1318
1319       default:
1320         goto error;
1321       }
1322   else if (size != 4)
1323     {
1324     error:
1325       as_bad (_("unsupported BFD relocation size %u"), size);
1326       fixup.reloc = BFD_RELOC_UNUSED;
1327     }
1328     
1329   fix_new_exp (frag, off, size, &fixup.exp, 0, fixup.reloc);
1330 }
1331
1332 void
1333 md_assemble (str)
1334      char *str;
1335 {
1336   char *s;
1337   struct mn10300_opcode *opcode;
1338   struct mn10300_opcode *next_opcode;
1339   const unsigned char *opindex_ptr;
1340   int next_opindex, relaxable;
1341   unsigned long insn, extension, size = 0;
1342   char *f;
1343   int i;
1344   int match;
1345
1346   /* Get the opcode.  */
1347   for (s = str; *s != '\0' && !ISSPACE (*s); s++)
1348     ;
1349   if (*s != '\0')
1350     *s++ = '\0';
1351
1352   /* Find the first opcode with the proper name.  */
1353   opcode = (struct mn10300_opcode *) hash_find (mn10300_hash, str);
1354   if (opcode == NULL)
1355     {
1356       as_bad (_("Unrecognized opcode: `%s'"), str);
1357       return;
1358     }
1359
1360   str = s;
1361   while (ISSPACE (*str))
1362     ++str;
1363
1364   input_line_pointer = str;
1365
1366   for (;;)
1367     {
1368       const char *errmsg;
1369       int op_idx;
1370       char *hold;
1371       int extra_shift = 0;
1372
1373       errmsg = _("Invalid opcode/operands");
1374
1375       /* Reset the array of register operands.  */
1376       memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
1377
1378       relaxable = 0;
1379       fc = 0;
1380       match = 0;
1381       next_opindex = 0;
1382       insn = opcode->opcode;
1383       extension = 0;
1384
1385       /* If the instruction is not available on the current machine
1386          then it can not possibly match.  */
1387       if (opcode->machine
1388           && !(opcode->machine == AM33_2 && HAVE_AM33_2)
1389           && !(opcode->machine == AM33 && HAVE_AM33)
1390           && !(opcode->machine == AM30 && HAVE_AM30))
1391         goto error;
1392
1393       for (op_idx = 1, opindex_ptr = opcode->operands;
1394            *opindex_ptr != 0;
1395            opindex_ptr++, op_idx++)
1396         {
1397           const struct mn10300_operand *operand;
1398           expressionS ex;
1399
1400           if (next_opindex == 0)
1401             {
1402               operand = &mn10300_operands[*opindex_ptr];
1403             }
1404           else
1405             {
1406               operand = &mn10300_operands[next_opindex];
1407               next_opindex = 0;
1408             }
1409
1410           while (*str == ' ' || *str == ',')
1411             ++str;
1412
1413           if (operand->flags & MN10300_OPERAND_RELAX)
1414             relaxable = 1;
1415
1416           /* Gather the operand.  */
1417           hold = input_line_pointer;
1418           input_line_pointer = str;
1419
1420           if (operand->flags & MN10300_OPERAND_PAREN)
1421             {
1422               if (*input_line_pointer != ')' && *input_line_pointer != '(')
1423                 {
1424                   input_line_pointer = hold;
1425                   str = hold;
1426                   goto error;
1427                 }
1428               input_line_pointer++;
1429               goto keep_going;
1430             }
1431           /* See if we can match the operands.  */
1432           else if (operand->flags & MN10300_OPERAND_DREG)
1433             {
1434               if (!data_register_name (&ex))
1435                 {
1436                   input_line_pointer = hold;
1437                   str = hold;
1438                   goto error;
1439                 }
1440             }
1441           else if (operand->flags & MN10300_OPERAND_AREG)
1442             {
1443               if (!address_register_name (&ex))
1444                 {
1445                   input_line_pointer = hold;
1446                   str = hold;
1447                   goto error;
1448                 }
1449             }
1450           else if (operand->flags & MN10300_OPERAND_SP)
1451             {
1452               char *start = input_line_pointer;
1453               char c = get_symbol_end ();
1454
1455               if (strcasecmp (start, "sp") != 0)
1456                 {
1457                   *input_line_pointer = c;
1458                   input_line_pointer = hold;
1459                   str = hold;
1460                   goto error;
1461                 }
1462               *input_line_pointer = c;
1463               goto keep_going;
1464             }
1465           else if (operand->flags & MN10300_OPERAND_RREG)
1466             {
1467               if (!r_register_name (&ex))
1468                 {
1469                   input_line_pointer = hold;
1470                   str = hold;
1471                   goto error;
1472                 }
1473             }
1474           else if (operand->flags & MN10300_OPERAND_XRREG)
1475             {
1476               if (!xr_register_name (&ex))
1477                 {
1478                   input_line_pointer = hold;
1479                   str = hold;
1480                   goto error;
1481                 }
1482             }
1483           else if (operand->flags & MN10300_OPERAND_FSREG)
1484             {
1485               if (!float_register_name (&ex))
1486                 {
1487                   input_line_pointer = hold;
1488                   str = hold;
1489                   goto error;
1490                 }
1491             }
1492           else if (operand->flags & MN10300_OPERAND_FDREG)
1493             {
1494               if (!double_register_name (&ex))
1495                 {
1496                   input_line_pointer = hold;
1497                   str = hold;
1498                   goto error;
1499                 }
1500             }
1501           else if (operand->flags & MN10300_OPERAND_FPCR)
1502             {
1503               char *start = input_line_pointer;
1504               char c = get_symbol_end ();
1505
1506               if (strcasecmp (start, "fpcr") != 0)
1507                 {
1508                   *input_line_pointer = c;
1509                   input_line_pointer = hold;
1510                   str = hold;
1511                   goto error;
1512                 }
1513               *input_line_pointer = c;
1514               goto keep_going;
1515             }
1516           else if (operand->flags & MN10300_OPERAND_USP)
1517             {
1518               char *start = input_line_pointer;
1519               char c = get_symbol_end ();
1520
1521               if (strcasecmp (start, "usp") != 0)
1522                 {
1523                   *input_line_pointer = c;
1524                   input_line_pointer = hold;
1525                   str = hold;
1526                   goto error;
1527                 }
1528               *input_line_pointer = c;
1529               goto keep_going;
1530             }
1531           else if (operand->flags & MN10300_OPERAND_SSP)
1532             {
1533               char *start = input_line_pointer;
1534               char c = get_symbol_end ();
1535
1536               if (strcasecmp (start, "ssp") != 0)
1537                 {
1538                   *input_line_pointer = c;
1539                   input_line_pointer = hold;
1540                   str = hold;
1541                   goto error;
1542                 }
1543               *input_line_pointer = c;
1544               goto keep_going;
1545             }
1546           else if (operand->flags & MN10300_OPERAND_MSP)
1547             {
1548               char *start = input_line_pointer;
1549               char c = get_symbol_end ();
1550
1551               if (strcasecmp (start, "msp") != 0)
1552                 {
1553                   *input_line_pointer = c;
1554                   input_line_pointer = hold;
1555                   str = hold;
1556                   goto error;
1557                 }
1558               *input_line_pointer = c;
1559               goto keep_going;
1560             }
1561           else if (operand->flags & MN10300_OPERAND_PC)
1562             {
1563               char *start = input_line_pointer;
1564               char c = get_symbol_end ();
1565
1566               if (strcasecmp (start, "pc") != 0)
1567                 {
1568                   *input_line_pointer = c;
1569                   input_line_pointer = hold;
1570                   str = hold;
1571                   goto error;
1572                 }
1573               *input_line_pointer = c;
1574               goto keep_going;
1575             }
1576           else if (operand->flags & MN10300_OPERAND_EPSW)
1577             {
1578               char *start = input_line_pointer;
1579               char c = get_symbol_end ();
1580
1581               if (strcasecmp (start, "epsw") != 0)
1582                 {
1583                   *input_line_pointer = c;
1584                   input_line_pointer = hold;
1585                   str = hold;
1586                   goto error;
1587                 }
1588               *input_line_pointer = c;
1589               goto keep_going;
1590             }
1591           else if (operand->flags & MN10300_OPERAND_PLUS)
1592             {
1593               if (*input_line_pointer != '+')
1594                 {
1595                   input_line_pointer = hold;
1596                   str = hold;
1597                   goto error;
1598                 }
1599               input_line_pointer++;
1600               goto keep_going;
1601             }
1602           else if (operand->flags & MN10300_OPERAND_PSW)
1603             {
1604               char *start = input_line_pointer;
1605               char c = get_symbol_end ();
1606
1607               if (strcasecmp (start, "psw") != 0)
1608                 {
1609                   *input_line_pointer = c;
1610                   input_line_pointer = hold;
1611                   str = hold;
1612                   goto error;
1613                 }
1614               *input_line_pointer = c;
1615               goto keep_going;
1616             }
1617           else if (operand->flags & MN10300_OPERAND_MDR)
1618             {
1619               char *start = input_line_pointer;
1620               char c = get_symbol_end ();
1621
1622               if (strcasecmp (start, "mdr") != 0)
1623                 {
1624                   *input_line_pointer = c;
1625                   input_line_pointer = hold;
1626                   str = hold;
1627                   goto error;
1628                 }
1629               *input_line_pointer = c;
1630               goto keep_going;
1631             }
1632           else if (operand->flags & MN10300_OPERAND_REG_LIST)
1633             {
1634               unsigned int value = 0;
1635               if (*input_line_pointer != '[')
1636                 {
1637                   input_line_pointer = hold;
1638                   str = hold;
1639                   goto error;
1640                 }
1641
1642               /* Eat the '['.  */
1643               input_line_pointer++;
1644
1645               /* We used to reject a null register list here; however,
1646                  we accept it now so the compiler can emit "call"
1647                  instructions for all calls to named functions.
1648
1649                  The linker can then fill in the appropriate bits for the
1650                  register list and stack size or change the instruction
1651                  into a "calls" if using "call" is not profitable.  */
1652               while (*input_line_pointer != ']')
1653                 {
1654                   char *start;
1655                   char c;
1656
1657                   if (*input_line_pointer == ',')
1658                     input_line_pointer++;
1659
1660                   start = input_line_pointer;
1661                   c = get_symbol_end ();
1662
1663                   if (strcasecmp (start, "d2") == 0)
1664                     {
1665                       value |= 0x80;
1666                       *input_line_pointer = c;
1667                     }
1668                   else if (strcasecmp (start, "d3") == 0)
1669                     {
1670                       value |= 0x40;
1671                       *input_line_pointer = c;
1672                     }
1673                   else if (strcasecmp (start, "a2") == 0)
1674                     {
1675                       value |= 0x20;
1676                       *input_line_pointer = c;
1677                     }
1678                   else if (strcasecmp (start, "a3") == 0)
1679                     {
1680                       value |= 0x10;
1681                       *input_line_pointer = c;
1682                     }
1683                   else if (strcasecmp (start, "other") == 0)
1684                     {
1685                       value |= 0x08;
1686                       *input_line_pointer = c;
1687                     }
1688                   else if (HAVE_AM33
1689                            && strcasecmp (start, "exreg0") == 0)
1690                     {
1691                       value |= 0x04;
1692                       *input_line_pointer = c;
1693                     }
1694                   else if (HAVE_AM33
1695                            && strcasecmp (start, "exreg1") == 0)
1696                     {
1697                       value |= 0x02;
1698                       *input_line_pointer = c;
1699                     }
1700                   else if (HAVE_AM33
1701                            && strcasecmp (start, "exother") == 0)
1702                     {
1703                       value |= 0x01;
1704                       *input_line_pointer = c;
1705                     }
1706                   else if (HAVE_AM33
1707                            && strcasecmp (start, "all") == 0)
1708                     {
1709                       value |= 0xff;
1710                       *input_line_pointer = c;
1711                     }
1712                   else
1713                     {
1714                       input_line_pointer = hold;
1715                       str = hold;
1716                       goto error;
1717                     }
1718                 }
1719               input_line_pointer++;
1720               mn10300_insert_operand (&insn, &extension, operand,
1721                                       value, (char *) NULL, 0, 0);
1722               goto keep_going;
1723
1724             }
1725           else if (data_register_name (&ex))
1726             {
1727               input_line_pointer = hold;
1728               str = hold;
1729               goto error;
1730             }
1731           else if (address_register_name (&ex))
1732             {
1733               input_line_pointer = hold;
1734               str = hold;
1735               goto error;
1736             }
1737           else if (other_register_name (&ex))
1738             {
1739               input_line_pointer = hold;
1740               str = hold;
1741               goto error;
1742             }
1743           else if (HAVE_AM33 && r_register_name (&ex))
1744             {
1745               input_line_pointer = hold;
1746               str = hold;
1747               goto error;
1748             }
1749           else if (HAVE_AM33 && xr_register_name (&ex))
1750             {
1751               input_line_pointer = hold;
1752               str = hold;
1753               goto error;
1754             }
1755           else if (HAVE_AM33_2 && float_register_name (&ex))
1756             {
1757               input_line_pointer = hold;
1758               str = hold;
1759               goto error;
1760             }
1761           else if (HAVE_AM33_2 && double_register_name (&ex))
1762             {
1763               input_line_pointer = hold;
1764               str = hold;
1765               goto error;
1766             }
1767           else if (*str == ')' || *str == '(')
1768             {
1769               input_line_pointer = hold;
1770               str = hold;
1771               goto error;
1772             }
1773           else
1774             {
1775               expression (&ex);
1776             }
1777
1778           switch (ex.X_op)
1779             {
1780             case O_illegal:
1781               errmsg = _("illegal operand");
1782               goto error;
1783             case O_absent:
1784               errmsg = _("missing operand");
1785               goto error;
1786             case O_register:
1787               {
1788                 int mask;
1789
1790                 mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
1791                 if (HAVE_AM33)
1792                   mask |= MN10300_OPERAND_RREG | MN10300_OPERAND_XRREG;
1793                 if (HAVE_AM33_2)
1794                   mask |= MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG;
1795                 if ((operand->flags & mask) == 0)
1796                   {
1797                     input_line_pointer = hold;
1798                     str = hold;
1799                     goto error;
1800                   }
1801
1802                 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
1803                   extra_shift = 8;
1804                 else if (opcode->format == FMT_D2
1805                          || opcode->format == FMT_D4
1806                          || opcode->format == FMT_S2
1807                          || opcode->format == FMT_S4
1808                          || opcode->format == FMT_S6
1809                          || opcode->format == FMT_D5)
1810                   extra_shift = 16;
1811                 else if (opcode->format == FMT_D7)
1812                   extra_shift = 8;
1813                 else if (opcode->format == FMT_D8 || opcode->format == FMT_D9)
1814                   extra_shift = 8;
1815                 else
1816                   extra_shift = 0;
1817
1818                 mn10300_insert_operand (&insn, &extension, operand,
1819                                         ex.X_add_number, (char *) NULL,
1820                                         0, extra_shift);
1821
1822                 /* And note the register number in the register array.  */
1823                 mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
1824                 break;
1825               }
1826
1827             case O_constant:
1828               /* If this operand can be promoted, and it doesn't
1829                  fit into the allocated bitfield for this insn,
1830                  then promote it (ie this opcode does not match).  */
1831               if (operand->flags
1832                   & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
1833                   && !check_operand (insn, operand, ex.X_add_number))
1834                 {
1835                   input_line_pointer = hold;
1836                   str = hold;
1837                   goto error;
1838                 }
1839
1840               mn10300_insert_operand (&insn, &extension, operand,
1841                                       ex.X_add_number, (char *) NULL,
1842                                       0, 0);
1843               break;
1844
1845             default:
1846               /* If this operand can be promoted, then this opcode didn't
1847                  match since we can't know if it needed promotion!  */
1848               if (operand->flags & MN10300_OPERAND_PROMOTE)
1849                 {
1850                   input_line_pointer = hold;
1851                   str = hold;
1852                   goto error;
1853                 }
1854
1855               /* We need to generate a fixup for this expression.  */
1856               if (fc >= MAX_INSN_FIXUPS)
1857                 as_fatal (_("too many fixups"));
1858               fixups[fc].exp = ex;
1859               fixups[fc].opindex = *opindex_ptr;
1860               fixups[fc].reloc = BFD_RELOC_UNUSED;
1861               if (mn10300_check_fixup (& fixups[fc]))
1862                 goto error;
1863               ++fc;
1864               break;
1865             }
1866
1867 keep_going:
1868           str = input_line_pointer;
1869           input_line_pointer = hold;
1870
1871           while (*str == ' ' || *str == ',')
1872             ++str;
1873
1874         }
1875
1876       /* Make sure we used all the operands!  */
1877       if (*str != ',')
1878         match = 1;
1879
1880       /* If this instruction has registers that must not match, verify
1881          that they do indeed not match.  */
1882       if (opcode->no_match_operands)
1883         {
1884           int i;
1885
1886           /* Look at each operand to see if it's marked.  */
1887           for (i = 0; i < MN10300_MAX_OPERANDS; i++)
1888             {
1889               if ((1 << i) & opcode->no_match_operands)
1890                 {
1891                   int j;
1892
1893                   /* operand I is marked.  Check that it does not match any
1894                      operands > I which are marked.  */
1895                   for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
1896                     {
1897                       if (((1 << j) & opcode->no_match_operands)
1898                           && mn10300_reg_operands[i] == mn10300_reg_operands[j])
1899                         {
1900                           errmsg = _("Invalid register specification.");
1901                           match = 0;
1902                           goto error;
1903                         }
1904                     }
1905                 }
1906             }
1907         }
1908
1909     error:
1910       if (match == 0)
1911         {
1912           next_opcode = opcode + 1;
1913           if (!strcmp (next_opcode->name, opcode->name))
1914             {
1915               opcode = next_opcode;
1916               continue;
1917             }
1918
1919           as_bad ("%s", errmsg);
1920           return;
1921         }
1922       break;
1923     }
1924
1925   while (ISSPACE (*str))
1926     ++str;
1927
1928   if (*str != '\0')
1929     as_bad (_("junk at end of line: `%s'"), str);
1930
1931   input_line_pointer = str;
1932
1933   /* Determine the size of the instruction.  */
1934   if (opcode->format == FMT_S0)
1935     size = 1;
1936
1937   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
1938     size = 2;
1939
1940   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
1941     size = 3;
1942
1943   if (opcode->format == FMT_D6)
1944     size = 3;
1945
1946   if (opcode->format == FMT_D7 || opcode->format == FMT_D10)
1947     size = 4;
1948
1949   if (opcode->format == FMT_D8)
1950     size = 6;
1951
1952   if (opcode->format == FMT_D9)
1953     size = 7;
1954
1955   if (opcode->format == FMT_S4)
1956     size = 5;
1957
1958   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
1959     size = 7;
1960
1961   if (opcode->format == FMT_D2)
1962     size = 4;
1963
1964   if (opcode->format == FMT_D3)
1965     size = 5;
1966
1967   if (opcode->format == FMT_D4)
1968     size = 6;
1969
1970   if (relaxable && fc > 0)
1971     {
1972       /* On a 64-bit host the size of an 'int' is not the same
1973          as the size of a pointer, so we need a union to convert
1974          the opindex field of the fr_cgen structure into a char *
1975          so that it can be stored in the frag.  We do not have
1976          to worry about loosing accuracy as we are not going to
1977          be even close to the 32bit limit of the int.  */
1978       union
1979       {
1980         int opindex;
1981         char * ptr;
1982       }
1983       opindex_converter;
1984       int type;
1985
1986       /* We want to anchor the line info to the previous frag (if
1987          there isn't one, create it), so that, when the insn is
1988          resized, we still get the right address for the beginning of
1989          the region.  */
1990       f = frag_more (0);
1991       dwarf2_emit_insn (0);
1992
1993       /* bCC  */
1994       if (size == 2)
1995         {
1996           /* Handle bra specially.  Basically treat it like jmp so
1997              that we automatically handle 8, 16 and 32 bit offsets
1998              correctly as well as jumps to an undefined address.
1999
2000              It is also important to not treat it like other bCC
2001              instructions since the long forms of bra is different
2002              from other bCC instructions.  */
2003           if (opcode->opcode == 0xca00)
2004             type = 10;
2005           else
2006             type = 0;
2007         }
2008       /* call  */
2009       else if (size == 5)
2010         type = 6;
2011       /* calls  */
2012       else if (size == 4)
2013         type = 8;
2014       /* jmp  */
2015       else if (size == 3 && opcode->opcode == 0xcc0000)
2016         type = 10;
2017       else if (size == 3 && (opcode->opcode & 0xfff000) == 0xf8d000)
2018         type = 13;
2019       /* bCC (uncommon cases)  */
2020       else
2021         type = 3;
2022
2023       opindex_converter.opindex = fixups[0].opindex;
2024       f = frag_var (rs_machine_dependent, 8, 8 - size, type,
2025                     fixups[0].exp.X_add_symbol,
2026                     fixups[0].exp.X_add_number,
2027                     opindex_converter.ptr);
2028
2029       /* This is pretty hokey.  We basically just care about the
2030          opcode, so we have to write out the first word big endian.
2031
2032          The exception is "call", which has two operands that we
2033          care about.
2034
2035          The first operand (the register list) happens to be in the
2036          first instruction word, and will be in the right place if
2037          we output the first word in big endian mode.
2038
2039          The second operand (stack size) is in the extension word,
2040          and we want it to appear as the first character in the extension
2041          word (as it appears in memory).  Luckily, writing the extension
2042          word in big endian format will do what we want.  */
2043       number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
2044       if (size > 8)
2045         {
2046           number_to_chars_bigendian (f + 4, extension, 4);
2047           number_to_chars_bigendian (f + 8, 0, size - 8);
2048         }
2049       else if (size > 4)
2050         number_to_chars_bigendian (f + 4, extension, size - 4);
2051     }
2052   else
2053     {
2054       /* Allocate space for the instruction.  */
2055       f = frag_more (size);
2056
2057       /* Fill in bytes for the instruction.  Note that opcode fields
2058          are written big-endian, 16 & 32bit immediates are written
2059          little endian.  Egad.  */
2060       if (opcode->format == FMT_S0
2061           || opcode->format == FMT_S1
2062           || opcode->format == FMT_D0
2063           || opcode->format == FMT_D6
2064           || opcode->format == FMT_D7
2065           || opcode->format == FMT_D10
2066           || opcode->format == FMT_D1)
2067         {
2068           number_to_chars_bigendian (f, insn, size);
2069         }
2070       else if (opcode->format == FMT_S2
2071                && opcode->opcode != 0xdf0000
2072                && opcode->opcode != 0xde0000)
2073         {
2074           /* A format S2 instruction that is _not_ "ret" and "retf".  */
2075           number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
2076           number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
2077         }
2078       else if (opcode->format == FMT_S2)
2079         {
2080           /* This must be a ret or retf, which is written entirely in
2081              big-endian format.  */
2082           number_to_chars_bigendian (f, insn, 3);
2083         }
2084       else if (opcode->format == FMT_S4
2085                && opcode->opcode != 0xdc000000)
2086         {
2087           /* This must be a format S4 "call" instruction.  What a pain.  */
2088           unsigned long temp = (insn >> 8) & 0xffff;
2089           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
2090           number_to_chars_littleendian (f + 1, temp, 2);
2091           number_to_chars_bigendian (f + 3, insn & 0xff, 1);
2092           number_to_chars_bigendian (f + 4, extension & 0xff, 1);
2093         }
2094       else if (opcode->format == FMT_S4)
2095         {
2096           /* This must be a format S4 "jmp" instruction.  */
2097           unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
2098           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
2099           number_to_chars_littleendian (f + 1, temp, 4);
2100         }
2101       else if (opcode->format == FMT_S6)
2102         {
2103           unsigned long temp = ((insn & 0xffffff) << 8)
2104             | ((extension >> 16) & 0xff);
2105           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
2106           number_to_chars_littleendian (f + 1, temp, 4);
2107           number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
2108           number_to_chars_bigendian (f + 6, extension & 0xff, 1);
2109         }
2110       else if (opcode->format == FMT_D2
2111                && opcode->opcode != 0xfaf80000
2112                && opcode->opcode != 0xfaf00000
2113                && opcode->opcode != 0xfaf40000)
2114         {
2115           /* A format D2 instruction where the 16bit immediate is
2116              really a single 16bit value, not two 8bit values.  */
2117           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
2118           number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
2119         }
2120       else if (opcode->format == FMT_D2)
2121         {
2122           /* A format D2 instruction where the 16bit immediate
2123              is really two 8bit immediates.  */
2124           number_to_chars_bigendian (f, insn, 4);
2125         }
2126       else if (opcode->format == FMT_D3)
2127         {
2128           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
2129           number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
2130           number_to_chars_bigendian (f + 4, extension & 0xff, 1);
2131         }
2132       else if (opcode->format == FMT_D4)
2133         {
2134           unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
2135
2136           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
2137           number_to_chars_littleendian (f + 2, temp, 4);
2138         }
2139       else if (opcode->format == FMT_D5)
2140         {
2141           unsigned long temp = (((insn & 0xffff) << 16)
2142                                 | ((extension >> 8) & 0xffff));
2143
2144           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
2145           number_to_chars_littleendian (f + 2, temp, 4);
2146           number_to_chars_bigendian (f + 6, extension & 0xff, 1);
2147         }
2148       else if (opcode->format == FMT_D8)
2149         {
2150           unsigned long temp = ((insn & 0xff) << 16) | (extension & 0xffff);
2151
2152           number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
2153           number_to_chars_bigendian (f + 3, (temp & 0xff), 1);
2154           number_to_chars_littleendian (f + 4, temp >> 8, 2);
2155         }
2156       else if (opcode->format == FMT_D9)
2157         {
2158           unsigned long temp = ((insn & 0xff) << 24) | (extension & 0xffffff);
2159
2160           number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
2161           number_to_chars_littleendian (f + 3, temp, 4);
2162         }
2163
2164       /* Create any fixups.  */
2165       for (i = 0; i < fc; i++)
2166         {
2167           const struct mn10300_operand *operand;
2168
2169           operand = &mn10300_operands[fixups[i].opindex];
2170           if (fixups[i].reloc != BFD_RELOC_UNUSED
2171               && fixups[i].reloc != BFD_RELOC_32_GOT_PCREL
2172               && fixups[i].reloc != BFD_RELOC_32_GOTOFF
2173               && fixups[i].reloc != BFD_RELOC_32_PLT_PCREL
2174               && fixups[i].reloc != BFD_RELOC_MN10300_GOT32)
2175             {
2176               reloc_howto_type *reloc_howto;
2177               int size;
2178               int offset;
2179               fixS *fixP;
2180
2181               reloc_howto = bfd_reloc_type_lookup (stdoutput,
2182                                                    fixups[i].reloc);
2183
2184               if (!reloc_howto)
2185                 abort ();
2186
2187               size = bfd_get_reloc_size (reloc_howto);
2188
2189               if (size < 1 || size > 4)
2190                 abort ();
2191
2192               offset = 4 - size;
2193               fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
2194                                   size, &fixups[i].exp,
2195                                   reloc_howto->pc_relative,
2196                                   fixups[i].reloc);
2197             }
2198           else
2199             {
2200               int reloc, pcrel, reloc_size, offset;
2201               fixS *fixP;
2202
2203               reloc = BFD_RELOC_NONE;
2204               if (fixups[i].reloc != BFD_RELOC_UNUSED)
2205                 reloc = fixups[i].reloc;
2206               /* How big is the reloc?  Remember SPLIT relocs are
2207                  implicitly 32bits.  */
2208               if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
2209                 reloc_size = 32;
2210               else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
2211                 reloc_size = 24;
2212               else
2213                 reloc_size = operand->bits;
2214
2215               /* Is the reloc pc-relative?  */
2216               pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
2217               if (reloc != BFD_RELOC_NONE)
2218                 pcrel = bfd_reloc_type_lookup (stdoutput, reloc)->pc_relative;
2219
2220               offset = size - (reloc_size + operand->shift) / 8;
2221
2222               /* Choose a proper BFD relocation type.  */
2223               if (reloc != BFD_RELOC_NONE)
2224                 ;
2225               else if (pcrel)
2226                 {
2227                   if (reloc_size == 32)
2228                     reloc = BFD_RELOC_32_PCREL;
2229                   else if (reloc_size == 16)
2230                     reloc = BFD_RELOC_16_PCREL;
2231                   else if (reloc_size == 8)
2232                     reloc = BFD_RELOC_8_PCREL;
2233                   else
2234                     abort ();
2235                 }
2236               else
2237                 {
2238                   if (reloc_size == 32)
2239                     reloc = BFD_RELOC_32;
2240                   else if (reloc_size == 16)
2241                     reloc = BFD_RELOC_16;
2242                   else if (reloc_size == 8)
2243                     reloc = BFD_RELOC_8;
2244                   else
2245                     abort ();
2246                 }
2247
2248               fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
2249                                   reloc_size / 8, &fixups[i].exp, pcrel,
2250                                   ((bfd_reloc_code_real_type) reloc));
2251
2252               if (pcrel)
2253                 fixP->fx_offset += offset;
2254             }
2255         }
2256
2257       dwarf2_emit_insn (size);
2258     }
2259 }
2260
2261 /* If while processing a fixup, a reloc really needs to be created
2262    then it is done here.  */
2263
2264 arelent *
2265 tc_gen_reloc (seg, fixp)
2266      asection *seg ATTRIBUTE_UNUSED;
2267      fixS *fixp;
2268 {
2269   arelent *reloc;
2270   reloc = (arelent *) xmalloc (sizeof (arelent));
2271
2272   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2273   if (reloc->howto == (reloc_howto_type *) NULL)
2274     {
2275       as_bad_where (fixp->fx_file, fixp->fx_line,
2276                     _("reloc %d not supported by object file format"),
2277                     (int) fixp->fx_r_type);
2278       return NULL;
2279     }
2280   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2281
2282   if (fixp->fx_subsy
2283       && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
2284     {
2285       fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
2286       fixp->fx_subsy = 0;
2287     }
2288
2289   if (fixp->fx_addsy && fixp->fx_subsy)
2290     {
2291       reloc->sym_ptr_ptr = NULL;
2292
2293       /* If we got a difference between two symbols, and the
2294          subtracted symbol is in the current section, use a
2295          PC-relative relocation.  If both symbols are in the same
2296          section, the difference would have already been simplified
2297          to a constant.  */
2298       if (S_GET_SEGMENT (fixp->fx_subsy) == seg)
2299         {
2300           reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2301           *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2302           reloc->addend = (reloc->address - S_GET_VALUE (fixp->fx_subsy)
2303                            + fixp->fx_offset);
2304
2305           switch (fixp->fx_r_type)
2306             {
2307             case BFD_RELOC_8:
2308               reloc->howto = bfd_reloc_type_lookup (stdoutput,
2309                                                     BFD_RELOC_8_PCREL);
2310               return reloc;
2311
2312             case BFD_RELOC_16:
2313               reloc->howto = bfd_reloc_type_lookup (stdoutput,
2314                                                     BFD_RELOC_16_PCREL);
2315               return reloc;
2316
2317             case BFD_RELOC_24:
2318               reloc->howto = bfd_reloc_type_lookup (stdoutput,
2319                                                     BFD_RELOC_24_PCREL);
2320               return reloc;
2321
2322             case BFD_RELOC_32:
2323               reloc->howto = bfd_reloc_type_lookup (stdoutput,
2324                                                     BFD_RELOC_32_PCREL);
2325               return reloc;
2326
2327             default:
2328               /* Try to compute the absolute value below.  */
2329               break;
2330             }
2331         }
2332
2333       if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
2334           || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
2335         {
2336           as_bad_where (fixp->fx_file, fixp->fx_line,
2337                         "Difference of symbols in different sections is not supported");
2338         }
2339       else
2340         {
2341           char *fixpos = fixp->fx_where + fixp->fx_frag->fr_literal;
2342
2343           reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
2344                            - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
2345
2346           switch (fixp->fx_r_type)
2347             {
2348             case BFD_RELOC_8:
2349               md_number_to_chars (fixpos, reloc->addend, 1);
2350               break;
2351
2352             case BFD_RELOC_16:
2353               md_number_to_chars (fixpos, reloc->addend, 2);
2354               break;
2355
2356             case BFD_RELOC_24:
2357               md_number_to_chars (fixpos, reloc->addend, 3);
2358               break;
2359
2360             case BFD_RELOC_32:
2361               md_number_to_chars (fixpos, reloc->addend, 4);
2362               break;
2363
2364             default:
2365               reloc->sym_ptr_ptr
2366                 = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
2367               return reloc;
2368             }
2369         }
2370
2371       if (reloc->sym_ptr_ptr)
2372         free (reloc->sym_ptr_ptr);
2373       free (reloc);
2374       return NULL;
2375     }
2376   else
2377     {
2378       reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2379       *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2380       reloc->addend = fixp->fx_offset;
2381     }
2382   return reloc;
2383 }
2384
2385 int
2386 md_estimate_size_before_relax (fragp, seg)
2387      fragS *fragp;
2388      asection *seg;
2389 {
2390   if (fragp->fr_subtype == 6
2391       && (!S_IS_DEFINED (fragp->fr_symbol)
2392           || seg != S_GET_SEGMENT (fragp->fr_symbol)))
2393     fragp->fr_subtype = 7;
2394   else if (fragp->fr_subtype == 8
2395            && (!S_IS_DEFINED (fragp->fr_symbol)
2396                || seg != S_GET_SEGMENT (fragp->fr_symbol)))
2397     fragp->fr_subtype = 9;
2398   else if (fragp->fr_subtype == 10
2399            &&  (!S_IS_DEFINED (fragp->fr_symbol)
2400                 || seg != S_GET_SEGMENT (fragp->fr_symbol)))
2401     fragp->fr_subtype = 12;
2402
2403   if (fragp->fr_subtype == 13)
2404     return 3;
2405   if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
2406     abort ();
2407
2408   return md_relax_table[fragp->fr_subtype].rlx_length;
2409 }
2410
2411 long
2412 md_pcrel_from (fixp)
2413      fixS *fixp;
2414 {
2415   if (fixp->fx_addsy != (symbolS *) NULL && !S_IS_DEFINED (fixp->fx_addsy))
2416     {
2417       /* The symbol is undefined.  Let the linker figure it out.  */
2418       return 0;
2419     }
2420   return fixp->fx_frag->fr_address + fixp->fx_where;
2421 }
2422
2423 void
2424 md_apply_fix (fixP, valP, seg)
2425      fixS * fixP;
2426      valueT * valP;
2427      segT seg;
2428 {
2429   char * fixpos = fixP->fx_where + fixP->fx_frag->fr_literal;
2430   int size = 0;
2431   int value = (int) * valP;
2432
2433   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
2434
2435   /* This should never happen.  */
2436   if (seg->flags & SEC_ALLOC)
2437     abort ();
2438
2439   /* The value we are passed in *valuep includes the symbol values.
2440      If we are doing this relocation the code in write.c is going to
2441      call bfd_install_relocation, which is also going to use the symbol
2442      value.  That means that if the reloc is fully resolved we want to
2443      use *valuep since bfd_install_relocation is not being used.
2444
2445      However, if the reloc is not fully resolved we do not want to use
2446      *valuep, and must use fx_offset instead.  However, if the reloc
2447      is PC relative, we do want to use *valuep since it includes the
2448      result of md_pcrel_from.  */
2449   if (fixP->fx_addsy != (symbolS *) NULL && ! fixP->fx_pcrel)
2450     value = fixP->fx_offset;
2451
2452   /* If the fix is relative to a symbol which is not defined, or not
2453      in the same segment as the fix, we cannot resolve it here.  */
2454   if (fixP->fx_addsy != NULL
2455       && (! S_IS_DEFINED (fixP->fx_addsy)
2456           || (S_GET_SEGMENT (fixP->fx_addsy) != seg)))
2457     {
2458       fixP->fx_done = 0;
2459       return;
2460     }
2461
2462   switch (fixP->fx_r_type)
2463     {
2464     case BFD_RELOC_8:
2465     case BFD_RELOC_8_PCREL:
2466       size = 1;
2467       break;
2468
2469     case BFD_RELOC_16:
2470     case BFD_RELOC_16_PCREL:
2471       size = 2;
2472       break;
2473
2474     case BFD_RELOC_32:
2475     case BFD_RELOC_32_PCREL:
2476       size = 4;
2477       break;
2478
2479     case BFD_RELOC_VTABLE_INHERIT:
2480     case BFD_RELOC_VTABLE_ENTRY:
2481       fixP->fx_done = 0;
2482       return;
2483
2484     case BFD_RELOC_NONE:
2485     default:
2486       as_bad_where (fixP->fx_file, fixP->fx_line,
2487                    _("Bad relocation fixup type (%d)"), fixP->fx_r_type);
2488     }
2489
2490   md_number_to_chars (fixpos, value, size);
2491
2492   /* If a symbol remains, pass the fixup, as a reloc, onto the linker.  */
2493   if (fixP->fx_addsy == NULL)
2494     fixP->fx_done = 1;
2495 }
2496
2497 /* Return zero if the fixup in fixp should be left alone and not
2498    adjusted.  */
2499
2500 bfd_boolean
2501 mn10300_fix_adjustable (fixp)
2502      struct fix *fixp;
2503 {
2504   if (TC_FORCE_RELOCATION_LOCAL (fixp))
2505     return 0;
2506
2507   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2508       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2509     return 0;
2510
2511   /* Do not adjust relocations involving symbols in code sections,
2512      because it breaks linker relaxations.  This could be fixed in the
2513      linker, but this fix is simpler, and it pretty much only affects
2514      object size a little bit.  */
2515   if (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_CODE)
2516     return 0;
2517
2518   /* Likewise, do not adjust symbols that won't be merged, or debug
2519      symbols, because they too break relaxation.  We do want to adjust
2520      other mergable symbols, like .rodata, because code relaxations
2521      need section-relative symbols to properly relax them.  */
2522   if (! (S_GET_SEGMENT(fixp->fx_addsy)->flags & SEC_MERGE))
2523     return 0;
2524   if (strncmp (S_GET_SEGMENT (fixp->fx_addsy)->name, ".debug", 6) == 0)
2525     return 0;
2526
2527   return 1;
2528 }
2529
2530 /* Insert an operand value into an instruction.  */
2531
2532 static void
2533 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
2534      unsigned long *insnp;
2535      unsigned long *extensionp;
2536      const struct mn10300_operand *operand;
2537      offsetT val;
2538      char *file;
2539      unsigned int line;
2540      unsigned int shift;
2541 {
2542   /* No need to check 32bit operands for a bit.  Note that
2543      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
2544   if (operand->bits != 32
2545       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
2546     {
2547       long min, max;
2548       offsetT test;
2549       int bits;
2550
2551       bits = operand->bits;
2552       if (operand->flags & MN10300_OPERAND_24BIT)
2553         bits = 24;
2554
2555       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
2556         {
2557           max = (1 << (bits - 1)) - 1;
2558           min = - (1 << (bits - 1));
2559         }
2560       else
2561         {
2562           max = (1 << bits) - 1;
2563           min = 0;
2564         }
2565
2566       test = val;
2567
2568       if (test < (offsetT) min || test > (offsetT) max)
2569         as_warn_value_out_of_range (_("operand"), test, (offsetT) min, (offsetT) max, file, line);
2570     }
2571
2572   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
2573     {
2574       *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
2575       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
2576                       << operand->shift);
2577     }
2578   else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
2579     {
2580       *insnp |= (val >> (24 - operand->bits)) & ((1 << operand->bits) - 1);
2581       *extensionp |= ((val & ((1 << (24 - operand->bits)) - 1))
2582                       << operand->shift);
2583     }
2584   else if ((operand->flags & (MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG)))
2585     {
2586       /* See devo/opcodes/m10300-opc.c just before #define FSM0 for an
2587          explanation of these variables.  Note that FMT-implied shifts
2588         are not taken into account for FP registers.  */
2589       unsigned long mask_low, mask_high;
2590       int shl_low, shr_high, shl_high;
2591
2592       switch (operand->bits)
2593         {
2594         case 5:
2595           /* Handle regular FP registers.  */
2596           if (operand->shift >= 0)
2597             {
2598               /* This is an `m' register.  */
2599               shl_low = operand->shift;
2600               shl_high = 8 + (8 & shl_low) + (shl_low & 4) / 4;
2601             }
2602           else
2603             {
2604               /* This is an `n' register.  */
2605               shl_low = -operand->shift;
2606               shl_high = shl_low / 4;
2607             }
2608
2609           mask_low = 0x0f;
2610           mask_high = 0x10;
2611           shr_high = 4;
2612           break;
2613
2614         case 3:
2615           /* Handle accumulators.  */
2616           shl_low = -operand->shift;
2617           shl_high = 0;
2618           mask_low = 0x03;
2619           mask_high = 0x04;
2620           shr_high = 2;
2621           break;
2622
2623         default:
2624           abort ();
2625         }
2626       *insnp |= ((((val & mask_high) >> shr_high) << shl_high)
2627                  | ((val & mask_low) << shl_low));
2628     }
2629   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
2630     {
2631       *insnp |= (((long) val & ((1 << operand->bits) - 1))
2632                  << (operand->shift + shift));
2633
2634       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
2635         *insnp |= (((long) val & ((1 << operand->bits) - 1))
2636                    << (operand->shift + shift + operand->bits));
2637     }
2638   else
2639     {
2640       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
2641                       << (operand->shift + shift));
2642
2643       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
2644         *extensionp |= (((long) val & ((1 << operand->bits) - 1))
2645                         << (operand->shift + shift + operand->bits));
2646     }
2647 }
2648
2649 static unsigned long
2650 check_operand (insn, operand, val)
2651      unsigned long insn ATTRIBUTE_UNUSED;
2652      const struct mn10300_operand *operand;
2653      offsetT val;
2654 {
2655   /* No need to check 32bit operands for a bit.  Note that
2656      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
2657   if (operand->bits != 32
2658       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
2659     {
2660       long min, max;
2661       offsetT test;
2662       int bits;
2663
2664       bits = operand->bits;
2665       if (operand->flags & MN10300_OPERAND_24BIT)
2666         bits = 24;
2667
2668       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
2669         {
2670           max = (1 << (bits - 1)) - 1;
2671           min = - (1 << (bits - 1));
2672         }
2673       else
2674         {
2675           max = (1 << bits) - 1;
2676           min = 0;
2677         }
2678
2679       test = val;
2680
2681       if (test < (offsetT) min || test > (offsetT) max)
2682         return 0;
2683       else
2684         return 1;
2685     }
2686   return 1;
2687 }
2688
2689 static void
2690 set_arch_mach (mach)
2691      int mach;
2692 {
2693   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
2694     as_warn (_("could not set architecture and machine"));
2695
2696   current_machine = mach;
2697 }
2698
2699 static inline char * mn10300_end_of_match PARAMS ((char *, char *));
2700
2701 static inline char *
2702 mn10300_end_of_match (cont, what)
2703      char *cont, *what;
2704 {
2705   int len = strlen (what);
2706
2707   if (strncmp (cont, what, strlen (what)) == 0
2708       && ! is_part_of_name (cont[len]))
2709     return cont + len;
2710
2711   return NULL;
2712 }  
2713
2714 int
2715 mn10300_parse_name (name, exprP, mode, nextcharP)
2716      char const *name;
2717      expressionS *exprP;
2718      enum expr_mode mode;
2719      char *nextcharP;
2720 {
2721   char *next = input_line_pointer;
2722   char *next_end;
2723   int reloc_type;
2724   segT segment;
2725
2726   exprP->X_op_symbol = NULL;
2727
2728   if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
2729     {
2730       if (! GOT_symbol)
2731         GOT_symbol = symbol_find_or_make (name);
2732
2733       exprP->X_add_symbol = GOT_symbol;
2734     no_suffix:
2735       /* If we have an absolute symbol or a reg,
2736          then we know its value now.  */
2737       segment = S_GET_SEGMENT (exprP->X_add_symbol);
2738       if (mode != expr_defer && segment == absolute_section)
2739         {
2740           exprP->X_op = O_constant;
2741           exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
2742           exprP->X_add_symbol = NULL;
2743         }
2744       else if (mode != expr_defer && segment == reg_section)
2745         {
2746           exprP->X_op = O_register;
2747           exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
2748           exprP->X_add_symbol = NULL;
2749         }
2750       else
2751         {
2752           exprP->X_op = O_symbol;
2753           exprP->X_add_number = 0;
2754         }
2755
2756       return 1;
2757     }
2758
2759   exprP->X_add_symbol = symbol_find_or_make (name);
2760   
2761   if (*nextcharP != '@')
2762     goto no_suffix;
2763   else if ((next_end = mn10300_end_of_match (next + 1, "GOTOFF")))
2764     reloc_type = BFD_RELOC_32_GOTOFF;
2765   else if ((next_end = mn10300_end_of_match (next + 1, "GOT")))
2766     reloc_type = BFD_RELOC_MN10300_GOT32;
2767   else if ((next_end = mn10300_end_of_match (next + 1, "PLT")))
2768     reloc_type = BFD_RELOC_32_PLT_PCREL;
2769   else
2770     goto no_suffix;
2771
2772   *input_line_pointer = *nextcharP;
2773   input_line_pointer = next_end;
2774   *nextcharP = *input_line_pointer;
2775   *input_line_pointer = '\0';
2776
2777   exprP->X_op = O_PIC_reloc;
2778   exprP->X_add_number = 0;
2779   exprP->X_md = reloc_type;
2780
2781   return 1;
2782 }