* config/tc-mn10300.c (md_assemble): Make sure "errmsg" has a non-NULL
[external/binutils.git] / gas / config / tc-mn10300.c
1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
2
3    Copyright (C) 1996, 1997, 1998 Free Software Foundation.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdio.h>
23 #include <ctype.h>
24 #include "as.h"
25 #include "subsegs.h"     
26 #include "opcode/mn10300.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 targets. */
36
37 /* Characters which always start a comment. */
38 const char comment_chars[] = "#";
39
40 /* Characters which start a comment at the beginning of a line.  */
41 const char line_comment_chars[] = ";#";
42
43 /* Characters which may be used to separate multiple commands on a 
44    single line.  */
45 const char line_separator_chars[] = ";";
46
47 /* Characters which are used to indicate an exponent in a floating 
48    point number.  */
49 const char EXP_CHARS[] = "eE";
50
51 /* Characters which mean that a number is a floating point constant, 
52    as in 0d1.0.  */
53 const char FLT_CHARS[] = "dD";
54 \f
55
56 const relax_typeS md_relax_table[] = {
57   /* bCC relaxing */
58   {0x7f, -0x80, 2, 1},
59   {0x7fff, -0x8000, 5, 2},
60   {0x7fffffff, -0x80000000, 7, 0},
61
62   /* bCC relaxing (uncommon cases) */
63   {0x7f, -0x80, 3, 4},
64   {0x7fff, -0x8000, 6, 5},
65   {0x7fffffff, -0x80000000, 8, 0},
66
67   /* call relaxing */
68   {0x7fff, -0x8000, 5, 7},
69   {0x7fffffff, -0x80000000, 7, 0},
70
71   /* calls relaxing */
72   {0x7fff, -0x8000, 4, 9},
73   {0x7fffffff, -0x80000000, 6, 0},
74
75   /* jmp relaxing */
76   {0x7f, -0x80, 2, 11},
77   {0x7fff, -0x8000, 3, 12},
78   {0x7fffffff, -0x80000000, 5, 0},
79
80 };
81
82 /* local functions */
83 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
84                                             const struct mn10300_operand *,
85                                             offsetT, char *, unsigned,
86                                             unsigned));
87 static unsigned long check_operand PARAMS ((unsigned long,
88                                             const struct mn10300_operand *,
89                                             offsetT));
90 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
91 static boolean data_register_name PARAMS ((expressionS *expressionP));
92 static boolean address_register_name PARAMS ((expressionS *expressionP));
93 static boolean other_register_name PARAMS ((expressionS *expressionP));
94 static void set_arch_mach PARAMS ((int));
95
96 static int current_machine;
97
98 /* fixups */
99 #define MAX_INSN_FIXUPS (5)
100 struct mn10300_fixup
101 {
102   expressionS exp;
103   int opindex;
104   bfd_reloc_code_real_type reloc;
105 };
106 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
107 static int fc;
108 \f
109 const char *md_shortopts = "";
110 struct option md_longopts[] = {
111   {NULL, no_argument, NULL, 0}
112 };
113 size_t md_longopts_size = sizeof(md_longopts); 
114
115 /* The target specific pseudo-ops which we support.  */
116 const pseudo_typeS md_pseudo_table[] =
117 {
118   { "am30",     set_arch_mach,          300 },
119   /* start-sanitize-am33 */
120   { "am33",     set_arch_mach,          330 },
121   /* end-sanitize-am33 */
122   { "mn10300",  set_arch_mach,          300 },
123   {NULL, 0, 0}
124 };
125
126 /* Opcode hash table.  */
127 static struct hash_control *mn10300_hash;
128
129 /* This table is sorted. Suitable for searching by a binary search. */
130 static const struct reg_name data_registers[] =
131 {
132   { "d0", 0 },
133   { "d1", 1 },
134   { "d2", 2 },
135   { "d3", 3 },
136 };
137 #define DATA_REG_NAME_CNT       (sizeof(data_registers) / sizeof(struct reg_name))
138
139 static const struct reg_name address_registers[] =
140 {
141   { "a0", 0 },
142   { "a1", 1 },
143   { "a2", 2 },
144   { "a3", 3 },
145 };
146 #define ADDRESS_REG_NAME_CNT    (sizeof(address_registers) / sizeof(struct reg_name))
147
148 /* start-sanitize-am33 */
149 static const struct reg_name r_registers[] =
150 {
151   { "a0", 8 },
152   { "a1", 9 },
153   { "a2", 10 },
154   { "a3", 11 },
155   { "d0", 12 },
156   { "d1", 13 },
157   { "d2", 14 },
158   { "d3", 15 },
159   { "e0", 0 },
160   { "e1", 1 },
161   { "e10", 10 },
162   { "e11", 11 },
163   { "e12", 12 },
164   { "e13", 13 },
165   { "e14", 14 },
166   { "e15", 15 },
167   { "e2", 2 },
168   { "e3", 3 },
169   { "e4", 4 },
170   { "e5", 5 },
171   { "e6", 6 },
172   { "e7", 7 },
173   { "e8", 8 },
174   { "e9", 9 },
175   { "r0", 0 },
176   { "r1", 1 },
177   { "r10", 10 },
178   { "r11", 11 },
179   { "r12", 12 },
180   { "r13", 13 },
181   { "r14", 14 },
182   { "r15", 15 },
183   { "r2", 2 },
184   { "r3", 3 },
185   { "r4", 4 },
186   { "r5", 5 },
187   { "r6", 6 },
188   { "r7", 7 },
189   { "r8", 8 },
190   { "r9", 9 },
191 };
192 #define R_REG_NAME_CNT  (sizeof(r_registers) / sizeof(struct reg_name))
193
194 static const struct reg_name xr_registers[] =
195 {
196   { "mcrh", 2 },
197   { "mcrl", 3 },
198   { "mcvf", 4 },
199   { "mdrq", 1 },
200   { "sp", 0 },
201   { "xr0", 0 },
202   { "xr1", 1 },
203   { "xr10", 10 },
204   { "xr11", 11 },
205   { "xr12", 12 },
206   { "xr13", 13 },
207   { "xr14", 14 },
208   { "xr15", 15 },
209   { "xr2", 2 },
210   { "xr3", 3 },
211   { "xr4", 4 },
212   { "xr5", 5 },
213   { "xr6", 6 },
214   { "xr7", 7 },
215   { "xr8", 8 },
216   { "xr9", 9 },
217 };
218 #define XR_REG_NAME_CNT (sizeof(xr_registers) / sizeof(struct reg_name))
219
220 /* end-sanitize-am33 */
221
222 static const struct reg_name other_registers[] =
223 {
224   { "mdr", 0 },
225   { "psw", 0 },
226   { "sp", 0 },
227 };
228 #define OTHER_REG_NAME_CNT      (sizeof(other_registers) / sizeof(struct reg_name))
229
230 /* reg_name_search does a binary search of the given register table
231    to see if "name" is a valid regiter name.  Returns the register
232    number from the array on success, or -1 on failure. */
233
234 static int
235 reg_name_search (regs, regcount, name)
236      const struct reg_name *regs;
237      int regcount;
238      const char *name;
239 {
240   int middle, low, high;
241   int cmp;
242
243   low = 0;
244   high = regcount - 1;
245
246   do
247     {
248       middle = (low + high) / 2;
249       cmp = strcasecmp (name, regs[middle].name);
250       if (cmp < 0)
251         high = middle - 1;
252       else if (cmp > 0)
253         low = middle + 1;
254       else 
255           return regs[middle].value;
256     }
257   while (low <= high);
258   return -1;
259 }
260
261
262 /* start-sanitize-am33 */
263 /* Summary of register_name().
264  *
265  * in: Input_line_pointer points to 1st char of operand.
266  *
267  * out: A expressionS.
268  *      The operand may have been a register: in this case, X_op == O_register,
269  *      X_add_number is set to the register number, and truth is returned.
270  *      Input_line_pointer->(next non-blank) char after operand, or is in
271  *      its original state.
272  */
273 static boolean
274 r_register_name (expressionP)
275      expressionS *expressionP;
276 {
277   int reg_number;
278   char *name;
279   char *start;
280   char c;
281
282   /* Find the spelling of the operand */
283   start = name = input_line_pointer;
284
285   c = get_symbol_end ();
286   reg_number = reg_name_search (r_registers, R_REG_NAME_CNT, name);
287
288   /* look to see if it's in the register table */
289   if (reg_number >= 0) 
290     {
291       expressionP->X_op = O_register;
292       expressionP->X_add_number = reg_number;
293
294       /* make the rest nice */
295       expressionP->X_add_symbol = NULL;
296       expressionP->X_op_symbol = NULL;
297       *input_line_pointer = c;  /* put back the delimiting char */
298       return true;
299     }
300   else
301     {
302       /* reset the line as if we had not done anything */
303       *input_line_pointer = c;   /* put back the delimiting char */
304       input_line_pointer = start; /* reset input_line pointer */
305       return false;
306     }
307 }
308
309 /* Summary of register_name().
310  *
311  * in: Input_line_pointer points to 1st char of operand.
312  *
313  * out: A expressionS.
314  *      The operand may have been a register: in this case, X_op == O_register,
315  *      X_add_number is set to the register number, and truth is returned.
316  *      Input_line_pointer->(next non-blank) char after operand, or is in
317  *      its original state.
318  */
319 static boolean
320 xr_register_name (expressionP)
321      expressionS *expressionP;
322 {
323   int reg_number;
324   char *name;
325   char *start;
326   char c;
327
328   /* Find the spelling of the operand */
329   start = name = input_line_pointer;
330
331   c = get_symbol_end ();
332   reg_number = reg_name_search (xr_registers, XR_REG_NAME_CNT, name);
333
334   /* look to see if it's in the register table */
335   if (reg_number >= 0) 
336     {
337       expressionP->X_op = O_register;
338       expressionP->X_add_number = reg_number;
339
340       /* make the rest nice */
341       expressionP->X_add_symbol = NULL;
342       expressionP->X_op_symbol = NULL;
343       *input_line_pointer = c;  /* put back the delimiting char */
344       return true;
345     }
346   else
347     {
348       /* reset the line as if we had not done anything */
349       *input_line_pointer = c;   /* put back the delimiting char */
350       input_line_pointer = start; /* reset input_line pointer */
351       return false;
352     }
353 }
354 /* end-sanitize-am33 */
355
356 /* Summary of register_name().
357  *
358  * in: Input_line_pointer points to 1st char of operand.
359  *
360  * out: A expressionS.
361  *      The operand may have been a register: in this case, X_op == O_register,
362  *      X_add_number is set to the register number, and truth is returned.
363  *      Input_line_pointer->(next non-blank) char after operand, or is in
364  *      its original state.
365  */
366 static boolean
367 data_register_name (expressionP)
368      expressionS *expressionP;
369 {
370   int reg_number;
371   char *name;
372   char *start;
373   char c;
374
375   /* Find the spelling of the operand */
376   start = name = input_line_pointer;
377
378   c = get_symbol_end ();
379   reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
380
381   /* look to see if it's in the register table */
382   if (reg_number >= 0) 
383     {
384       expressionP->X_op = O_register;
385       expressionP->X_add_number = reg_number;
386
387       /* make the rest nice */
388       expressionP->X_add_symbol = NULL;
389       expressionP->X_op_symbol = NULL;
390       *input_line_pointer = c;  /* put back the delimiting char */
391       return true;
392     }
393   else
394     {
395       /* reset the line as if we had not done anything */
396       *input_line_pointer = c;   /* put back the delimiting char */
397       input_line_pointer = start; /* reset input_line pointer */
398       return false;
399     }
400 }
401
402 /* Summary of register_name().
403  *
404  * in: Input_line_pointer points to 1st char of operand.
405  *
406  * out: A expressionS.
407  *      The operand may have been a register: in this case, X_op == O_register,
408  *      X_add_number is set to the register number, and truth is returned.
409  *      Input_line_pointer->(next non-blank) char after operand, or is in
410  *      its original state.
411  */
412 static boolean
413 address_register_name (expressionP)
414      expressionS *expressionP;
415 {
416   int reg_number;
417   char *name;
418   char *start;
419   char c;
420
421   /* Find the spelling of the operand */
422   start = name = input_line_pointer;
423
424   c = get_symbol_end ();
425   reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
426
427   /* look to see if it's in the register table */
428   if (reg_number >= 0) 
429     {
430       expressionP->X_op = O_register;
431       expressionP->X_add_number = reg_number;
432
433       /* make the rest nice */
434       expressionP->X_add_symbol = NULL;
435       expressionP->X_op_symbol = NULL;
436       *input_line_pointer = c;  /* put back the delimiting char */
437       return true;
438     }
439   else
440     {
441       /* reset the line as if we had not done anything */
442       *input_line_pointer = c;   /* put back the delimiting char */
443       input_line_pointer = start; /* reset input_line pointer */
444       return false;
445     }
446 }
447
448 /* Summary of register_name().
449  *
450  * in: Input_line_pointer points to 1st char of operand.
451  *
452  * out: A expressionS.
453  *      The operand may have been a register: in this case, X_op == O_register,
454  *      X_add_number is set to the register number, and truth is returned.
455  *      Input_line_pointer->(next non-blank) char after operand, or is in
456  *      its original state.
457  */
458 static boolean
459 other_register_name (expressionP)
460      expressionS *expressionP;
461 {
462   int reg_number;
463   char *name;
464   char *start;
465   char c;
466
467   /* Find the spelling of the operand */
468   start = name = input_line_pointer;
469
470   c = get_symbol_end ();
471   reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
472
473   /* look to see if it's in the register table */
474   if (reg_number >= 0) 
475     {
476       expressionP->X_op = O_register;
477       expressionP->X_add_number = reg_number;
478
479       /* make the rest nice */
480       expressionP->X_add_symbol = NULL;
481       expressionP->X_op_symbol = NULL;
482       *input_line_pointer = c;  /* put back the delimiting char */
483       return true;
484     }
485   else
486     {
487       /* reset the line as if we had not done anything */
488       *input_line_pointer = c;   /* put back the delimiting char */
489       input_line_pointer = start; /* reset input_line pointer */
490       return false;
491     }
492 }
493
494 void
495 md_show_usage (stream)
496   FILE *stream;
497 {
498   fprintf(stream, _("MN10300 options:\n\
499 none yet\n"));
500
501
502 int
503 md_parse_option (c, arg)
504      int c;
505      char *arg;
506 {
507   return 0;
508 }
509
510 symbolS *
511 md_undefined_symbol (name)
512   char *name;
513 {
514   return 0;
515 }
516
517 char *
518 md_atof (type, litp, sizep)
519   int type;
520   char *litp;
521   int *sizep;
522 {
523   int prec;
524   LITTLENUM_TYPE words[4];
525   char *t;
526   int i;
527
528   switch (type)
529     {
530     case 'f':
531       prec = 2;
532       break;
533
534     case 'd':
535       prec = 4;
536       break;
537
538     default:
539       *sizep = 0;
540       return "bad call to md_atof";
541     }
542   
543   t = atof_ieee (input_line_pointer, type, words);
544   if (t)
545     input_line_pointer = t;
546
547   *sizep = prec * 2;
548
549   for (i = prec - 1; i >= 0; i--)
550     {
551       md_number_to_chars (litp, (valueT) words[i], 2);
552       litp += 2;
553     }
554
555   return NULL;
556 }
557
558
559 void
560 md_convert_frag (abfd, sec, fragP)
561   bfd *abfd;
562   asection *sec;
563   fragS *fragP;
564 {
565   static unsigned long label_count = 0;
566   char buf[40];
567
568   subseg_change (sec, 0);
569   if (fragP->fr_subtype == 0)
570     {
571       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
572                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
573       fragP->fr_var = 0;
574       fragP->fr_fix += 2;
575     }
576   else if (fragP->fr_subtype == 1)
577     {
578       /* Reverse the condition of the first branch.  */
579       int offset = fragP->fr_fix;
580       int opcode = fragP->fr_literal[offset] & 0xff;
581
582       switch (opcode)
583         {
584         case 0xc8:
585           opcode = 0xc9;
586           break;
587         case 0xc9:
588           opcode = 0xc8;
589           break;
590         case 0xc0:
591           opcode = 0xc2;
592           break;
593         case 0xc2:
594           opcode = 0xc0;
595           break;
596         case 0xc3:
597           opcode = 0xc1;
598           break;
599         case 0xc1:
600           opcode = 0xc3;
601           break;
602         case 0xc4:
603           opcode = 0xc6;
604           break;
605         case 0xc6:
606           opcode = 0xc4;
607           break;
608         case 0xc7:
609           opcode = 0xc5;
610           break;
611         case 0xc5:
612           opcode = 0xc7;
613           break;
614         default:
615           abort ();
616         }
617       fragP->fr_literal[offset] = opcode;
618
619       /* Create a fixup for the reversed conditional branch.  */
620       sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
621       fix_new (fragP, fragP->fr_fix + 1, 1,
622                symbol_new (buf, sec, 0, fragP->fr_next),
623                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
624
625       /* Now create the unconditional branch + fixup to the
626          final target.  */
627       fragP->fr_literal[offset + 2] = 0xcc;
628       fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
629                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
630       fragP->fr_var = 0;
631       fragP->fr_fix += 5;
632     }
633   else if (fragP->fr_subtype == 2)
634     {
635       /* Reverse the condition of the first branch.  */
636       int offset = fragP->fr_fix;
637       int opcode = fragP->fr_literal[offset] & 0xff;
638
639       switch (opcode)
640         {
641         case 0xc8:
642           opcode = 0xc9;
643           break;
644         case 0xc9:
645           opcode = 0xc8;
646           break;
647         case 0xc0:
648           opcode = 0xc2;
649           break;
650         case 0xc2:
651           opcode = 0xc0;
652           break;
653         case 0xc3:
654           opcode = 0xc1;
655           break;
656         case 0xc1:
657           opcode = 0xc3;
658           break;
659         case 0xc4:
660           opcode = 0xc6;
661           break;
662         case 0xc6:
663           opcode = 0xc4;
664           break;
665         case 0xc7:
666           opcode = 0xc5;
667           break;
668         case 0xc5:
669           opcode = 0xc7;
670           break;
671         default:
672           abort ();
673         }
674       fragP->fr_literal[offset] = opcode;
675
676       /* Create a fixup for the reversed conditional branch.  */
677       sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
678       fix_new (fragP, fragP->fr_fix + 1, 1,
679                symbol_new (buf, sec, 0, fragP->fr_next),
680                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
681
682       /* Now create the unconditional branch + fixup to the
683          final target.  */
684       fragP->fr_literal[offset + 2] = 0xdc;
685       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
686                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
687       fragP->fr_var = 0;
688       fragP->fr_fix += 7;
689     }
690   else if (fragP->fr_subtype == 3)
691     {
692       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
693                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
694       fragP->fr_var = 0;
695       fragP->fr_fix += 3;
696     }
697   else if (fragP->fr_subtype == 4)
698     {
699       /* Reverse the condition of the first branch.  */
700       int offset = fragP->fr_fix;
701       int opcode = fragP->fr_literal[offset + 1] & 0xff;
702
703       switch (opcode)
704         {
705         case 0xe8:
706           opcode = 0xe9;
707           break;
708         case 0xe9:
709           opcode = 0xe8;
710           break;
711         case 0xea:
712           opcode = 0xeb;
713           break;
714         case 0xeb:
715           opcode = 0xea;
716           break;
717         default:
718           abort ();
719         }
720       fragP->fr_literal[offset + 1] = opcode;
721
722       /* Create a fixup for the reversed conditional branch.  */
723       sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
724       fix_new (fragP, fragP->fr_fix + 2, 1,
725                symbol_new (buf, sec, 0, fragP->fr_next),
726                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
727
728       /* Now create the unconditional branch + fixup to the
729          final target.  */
730       fragP->fr_literal[offset + 3] = 0xcc;
731       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
732                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
733       fragP->fr_var = 0;
734       fragP->fr_fix += 6;
735     }
736   else if (fragP->fr_subtype == 5)
737     {
738       /* Reverse the condition of the first branch.  */
739       int offset = fragP->fr_fix;
740       int opcode = fragP->fr_literal[offset + 1] & 0xff;
741
742       switch (opcode)
743         {
744         case 0xe8:
745           opcode = 0xe9;
746           break;
747         case 0xea:
748           opcode = 0xeb;
749           break;
750         case 0xeb:
751           opcode = 0xea;
752           break;
753         default:
754           abort ();
755         }
756       fragP->fr_literal[offset + 1] = opcode;
757
758       /* Create a fixup for the reversed conditional branch.  */
759       sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
760       fix_new (fragP, fragP->fr_fix + 2, 1,
761                symbol_new (buf, sec, 0, fragP->fr_next),
762                fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
763
764       /* Now create the unconditional branch + fixup to the
765          final target.  */
766       fragP->fr_literal[offset + 3] = 0xdc;
767       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
768                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
769       fragP->fr_var = 0;
770       fragP->fr_fix += 8;
771     }
772   else if (fragP->fr_subtype == 6)
773     {
774       int offset = fragP->fr_fix;
775       fragP->fr_literal[offset] = 0xcd;
776       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
777                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
778       fragP->fr_var = 0;
779       fragP->fr_fix += 5;
780     }
781   else if (fragP->fr_subtype == 7)
782     {
783       int offset = fragP->fr_fix;
784       fragP->fr_literal[offset] = 0xdd;
785       fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
786       fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
787
788       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
789                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
790       fragP->fr_var = 0;
791       fragP->fr_fix += 7;
792     }
793   else if (fragP->fr_subtype == 8)
794     {
795       int offset = fragP->fr_fix;
796       fragP->fr_literal[offset] = 0xfa;
797       fragP->fr_literal[offset + 1] = 0xff;
798       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
799                fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
800       fragP->fr_var = 0;
801       fragP->fr_fix += 4;
802     }
803   else if (fragP->fr_subtype == 9)
804     {
805       int offset = fragP->fr_fix;
806       fragP->fr_literal[offset] = 0xfc;
807       fragP->fr_literal[offset + 1] = 0xff;
808
809       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
810                fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
811       fragP->fr_var = 0;
812       fragP->fr_fix += 6;
813     }
814   else if (fragP->fr_subtype == 10)
815     {
816       fragP->fr_literal[fragP->fr_fix] = 0xca;
817       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
818                fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
819       fragP->fr_var = 0;
820       fragP->fr_fix += 2;
821     }
822   else if (fragP->fr_subtype == 11)
823     {
824       int offset = fragP->fr_fix;
825       fragP->fr_literal[offset] = 0xcc;
826
827       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
828                fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
829       fragP->fr_var = 0;
830       fragP->fr_fix += 3;
831     }
832   else if (fragP->fr_subtype == 12)
833     {
834       int offset = fragP->fr_fix;
835       fragP->fr_literal[offset] = 0xdc;
836
837       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
838                fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
839       fragP->fr_var = 0;
840       fragP->fr_fix += 5;
841     }
842   else
843     abort ();
844 }
845
846 valueT
847 md_section_align (seg, addr)
848      asection *seg;
849      valueT addr;
850 {
851   int align = bfd_get_section_alignment (stdoutput, seg);
852   return ((addr + (1 << align) - 1) & (-1 << align));
853 }
854
855 void
856 md_begin ()
857 {
858   char *prev_name = "";
859   register const struct mn10300_opcode *op;
860
861   mn10300_hash = hash_new();
862
863   /* Insert unique names into hash table.  The MN10300 instruction set
864      has many identical opcode names that have different opcodes based
865      on the operands.  This hash table then provides a quick index to
866      the first opcode with a particular name in the opcode table.  */
867
868   op = mn10300_opcodes;
869   while (op->name)
870     {
871       if (strcmp (prev_name, op->name)) 
872         {
873           prev_name = (char *) op->name;
874           hash_insert (mn10300_hash, op->name, (char *) op);
875         }
876       op++;
877     }
878
879   /* This is both a simplification (we don't have to write md_apply_fix)
880      and support for future optimizations (branch shortening and similar
881      stuff in the linker.  */
882   linkrelax = 1;
883
884   /* Set the default machine type.  */
885   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, 300))
886     as_warn (_("could not set architecture and machine"));
887
888   current_machine = 300;
889 }
890
891 void
892 md_assemble (str) 
893      char *str;
894 {
895   char *s;
896   struct mn10300_opcode *opcode;
897   struct mn10300_opcode *next_opcode;
898   const unsigned char *opindex_ptr;
899   int next_opindex, relaxable;
900   unsigned long insn, extension, size = 0;
901   char *f;
902   int i;
903   int match;
904
905   /* Get the opcode.  */
906   for (s = str; *s != '\0' && ! isspace (*s); s++)
907     ;
908   if (*s != '\0')
909     *s++ = '\0';
910
911   /* find the first opcode with the proper name */
912   opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
913   if (opcode == NULL)
914     {
915       as_bad (_("Unrecognized opcode: `%s'"), str);
916       return;
917     }
918
919   str = s;
920   while (isspace (*str))
921     ++str;
922
923   input_line_pointer = str;
924
925   for(;;)
926     {
927       const char *errmsg = "Invalid opcode/operands";
928       int op_idx;
929       char *hold;
930       int extra_shift = 0;
931
932
933       relaxable = 0;
934       fc = 0;
935       match = 0;
936       next_opindex = 0;
937       insn = opcode->opcode;
938       extension = 0;
939
940       /* If the instruction is not available on the current machine
941          then it can not possibly match.  */
942       if (opcode->machine
943           && (opcode->machine != current_machine))
944         goto error;
945
946       for (op_idx = 1, opindex_ptr = opcode->operands;
947            *opindex_ptr != 0;
948            opindex_ptr++, op_idx++)
949         {
950           const struct mn10300_operand *operand;
951           expressionS ex;
952
953           if (next_opindex == 0)
954             {
955               operand = &mn10300_operands[*opindex_ptr];
956             }
957           else
958             {
959               operand = &mn10300_operands[next_opindex];
960               next_opindex = 0;
961             }
962
963           while (*str == ' ' || *str == ',')
964             ++str;
965
966           if (operand->flags & MN10300_OPERAND_RELAX)
967             relaxable = 1;
968
969           /* Gather the operand. */
970           hold = input_line_pointer;
971           input_line_pointer = str;
972
973           if (operand->flags & MN10300_OPERAND_PAREN)
974             {
975               if (*input_line_pointer != ')' && *input_line_pointer != '(')
976                 {
977                   input_line_pointer = hold;
978                   str = hold;
979                   goto error;
980                 }
981               input_line_pointer++;
982               goto keep_going;
983             }
984           /* See if we can match the operands.  */
985           else if (operand->flags & MN10300_OPERAND_DREG)
986             {
987               if (!data_register_name (&ex))
988                 {
989                   input_line_pointer = hold;
990                   str = hold;
991                   goto error;
992                 }
993             }
994           else if (operand->flags & MN10300_OPERAND_AREG)
995             {
996               if (!address_register_name (&ex))
997                 {
998                   input_line_pointer = hold;
999                   str = hold;
1000                   goto error;
1001                 }
1002             }
1003           else if (operand->flags & MN10300_OPERAND_SP)
1004             {
1005               char *start = input_line_pointer;
1006               char c = get_symbol_end ();
1007
1008               if (strcasecmp (start, "sp") != 0)
1009                 {
1010                   *input_line_pointer = c;
1011                   input_line_pointer = hold;
1012                   str = hold;
1013                   goto error;
1014                 }
1015               *input_line_pointer = c;
1016               goto keep_going;
1017             }
1018           /* start-sanitize-am33 */
1019           else if (operand->flags & MN10300_OPERAND_RREG)
1020             {
1021               if (!r_register_name (&ex))
1022                 {
1023                   input_line_pointer = hold;
1024                   str = hold;
1025                   goto error;
1026                 }
1027             }
1028           else if (operand->flags & MN10300_OPERAND_XRREG)
1029             {
1030               if (!xr_register_name (&ex))
1031                 {
1032                   input_line_pointer = hold;
1033                   str = hold;
1034                   goto error;
1035                 }
1036             }
1037           else if (operand->flags & MN10300_OPERAND_USP)
1038             {
1039               char *start = input_line_pointer;
1040               char c = get_symbol_end ();
1041
1042               if (strcasecmp (start, "usp") != 0)
1043                 {
1044                   *input_line_pointer = c;
1045                   input_line_pointer = hold;
1046                   str = hold;
1047                   goto error;
1048                 }
1049               *input_line_pointer = c;
1050               goto keep_going;
1051             }
1052           else if (operand->flags & MN10300_OPERAND_SSP)
1053             {
1054               char *start = input_line_pointer;
1055               char c = get_symbol_end ();
1056
1057               if (strcasecmp (start, "ssp") != 0)
1058                 {
1059                   *input_line_pointer = c;
1060                   input_line_pointer = hold;
1061                   str = hold;
1062                   goto error;
1063                 }
1064               *input_line_pointer = c;
1065               goto keep_going;
1066             }
1067           else if (operand->flags & MN10300_OPERAND_MSP)
1068             {
1069               char *start = input_line_pointer;
1070               char c = get_symbol_end ();
1071
1072               if (strcasecmp (start, "msp") != 0)
1073                 {
1074                   *input_line_pointer = c;
1075                   input_line_pointer = hold;
1076                   str = hold;
1077                   goto error;
1078                 }
1079               *input_line_pointer = c;
1080               goto keep_going;
1081             }
1082           else if (operand->flags & MN10300_OPERAND_PC)
1083             {
1084               char *start = input_line_pointer;
1085               char c = get_symbol_end ();
1086
1087               if (strcasecmp (start, "pc") != 0)
1088                 {
1089                   *input_line_pointer = c;
1090                   input_line_pointer = hold;
1091                   str = hold;
1092                   goto error;
1093                 }
1094               *input_line_pointer = c;
1095               goto keep_going;
1096             }
1097           else if (operand->flags & MN10300_OPERAND_EPSW)
1098             {
1099               char *start = input_line_pointer;
1100               char c = get_symbol_end ();
1101
1102               if (strcasecmp (start, "epsw") != 0)
1103                 {
1104                   *input_line_pointer = c;
1105                   input_line_pointer = hold;
1106                   str = hold;
1107                   goto error;
1108                 }
1109               *input_line_pointer = c;
1110               goto keep_going;
1111             }
1112           else if (operand->flags & MN10300_OPERAND_PLUS)
1113             {
1114               if (*input_line_pointer != '+')
1115                 {
1116                   input_line_pointer = hold;
1117                   str = hold;
1118                   goto error;
1119                 }
1120               input_line_pointer++;
1121               goto keep_going;
1122             }
1123           /* end-sanitize-am33 */
1124           else if (operand->flags & MN10300_OPERAND_PSW)
1125             {
1126               char *start = input_line_pointer;
1127               char c = get_symbol_end ();
1128
1129               if (strcasecmp (start, "psw") != 0)
1130                 {
1131                   *input_line_pointer = c;
1132                   input_line_pointer = hold;
1133                   str = hold;
1134                   goto error;
1135                 }
1136               *input_line_pointer = c;
1137               goto keep_going;
1138             }
1139           else if (operand->flags & MN10300_OPERAND_MDR)
1140             {
1141               char *start = input_line_pointer;
1142               char c = get_symbol_end ();
1143
1144               if (strcasecmp (start, "mdr") != 0)
1145                 {
1146                   *input_line_pointer = c;
1147                   input_line_pointer = hold;
1148                   str = hold;
1149                   goto error;
1150                 }
1151               *input_line_pointer = c;
1152               goto keep_going;
1153             }
1154           else if (operand->flags & MN10300_OPERAND_REG_LIST)
1155             {
1156               unsigned int value = 0;
1157               if (*input_line_pointer != '[')
1158                 {
1159                   input_line_pointer = hold;
1160                   str = hold;
1161                   goto error;
1162                 }
1163
1164               /* Eat the '['.  */
1165               input_line_pointer++;
1166              
1167               /* We used to reject a null register list here; however,
1168                  we accept it now so the compiler can emit "call" instructions
1169                  for all calls to named functions.
1170
1171                  The linker can then fill in the appropriate bits for the
1172                  register list and stack size or change the instruction
1173                  into a "calls" if using "call" is not profitable.  */
1174               while (*input_line_pointer != ']')
1175                 {
1176                   char *start;
1177                   char c;
1178
1179                   if (*input_line_pointer == ',')
1180                     input_line_pointer++;
1181
1182                   start = input_line_pointer;
1183                   c = get_symbol_end ();
1184
1185                   if (strcasecmp (start, "d2") == 0)
1186                     {
1187                       value |= 0x80;
1188                       *input_line_pointer = c;
1189                     }
1190                   else if (strcasecmp (start, "d3") == 0)
1191                     {
1192                       value |= 0x40;
1193                       *input_line_pointer = c;
1194                     }
1195                   else if (strcasecmp (start, "a2") == 0)
1196                     {
1197                       value |= 0x20;
1198                       *input_line_pointer = c;
1199                     }
1200                   else if (strcasecmp (start, "a3") == 0)
1201                     {
1202                       value |= 0x10;
1203                       *input_line_pointer = c;
1204                     }
1205                   else if (strcasecmp (start, "other") == 0)
1206                     {
1207                       value |= 0x08;
1208                       *input_line_pointer = c;
1209                     }
1210                   /* start-sanitize-am33 */
1211                   else if (strcasecmp (start, "exreg0") == 0)
1212                     {
1213                       value |= 0x04;
1214                       *input_line_pointer = c;
1215                     }
1216                   else if (strcasecmp (start, "exreg1") == 0)
1217                     {
1218                       value |= 0x02;
1219                       *input_line_pointer = c;
1220                     }
1221                   else if (strcasecmp (start, "exother") == 0)
1222                     {
1223                       value |= 0x01;
1224                       *input_line_pointer = c;
1225                     }
1226                   else if (strcasecmp (start, "all") == 0)
1227                     {
1228                       value |= 0xff;
1229                       *input_line_pointer = c;
1230                     }
1231                   /* end-sanitize-am33 */
1232                   else
1233                     {
1234                       input_line_pointer = hold;
1235                       str = hold;
1236                       goto error;
1237                     }
1238                 }
1239               input_line_pointer++;
1240               mn10300_insert_operand (&insn, &extension, operand,
1241                                       value, (char *) NULL, 0, 0);
1242               goto keep_going;
1243
1244             }
1245           else if (data_register_name (&ex))
1246             {
1247               input_line_pointer = hold;
1248               str = hold;
1249               goto error;
1250             }
1251           else if (address_register_name (&ex))
1252             {
1253               input_line_pointer = hold;
1254               str = hold;
1255               goto error;
1256             }
1257           else if (other_register_name (&ex))
1258             {
1259               input_line_pointer = hold;
1260               str = hold;
1261               goto error;
1262             }
1263           /* start-sanitize-am33 */
1264           else if (r_register_name (&ex))
1265             {
1266               input_line_pointer = hold;
1267               str = hold;
1268               goto error;
1269             }
1270           else if (xr_register_name (&ex))
1271             {
1272               input_line_pointer = hold;
1273               str = hold;
1274               goto error;
1275             }
1276           /* end-sanitize-am33 */
1277           else if (*str == ')' || *str == '(')
1278             {
1279               input_line_pointer = hold;
1280               str = hold;
1281               goto error;
1282             }
1283           else
1284             {
1285               expression (&ex);
1286             }
1287
1288           switch (ex.X_op) 
1289             {
1290             case O_illegal:
1291               errmsg = _("illegal operand");
1292               goto error;
1293             case O_absent:
1294               errmsg = _("missing operand");
1295               goto error;
1296             case O_register:
1297               {
1298                 int mask;
1299
1300                 mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
1301                 /* start-sanitize-am33 */
1302                 mask |= MN10300_OPERAND_RREG | MN10300_OPERAND_XRREG;
1303                 /* end-sanitize-am33 */
1304                 if ((operand->flags & mask) == 0)
1305                   {
1306                     input_line_pointer = hold;
1307                     str = hold;
1308                     goto error;
1309                   }
1310                 
1311                 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
1312                   extra_shift = 8;
1313                 else if (opcode->format == FMT_D2
1314                          || opcode->format == FMT_D4
1315                          || opcode->format == FMT_S2
1316                          || opcode->format == FMT_S4
1317                          || opcode->format == FMT_S6
1318                          || opcode->format == FMT_D5)
1319                   extra_shift = 16;
1320                 /* start-sanitize-am33 */
1321                 else if (opcode->format == FMT_D7)
1322                   extra_shift = 8;
1323                 else if (opcode->format == FMT_D8 || opcode->format == FMT_D9)
1324                   extra_shift = 8;
1325                 /* end-sanitize-am33 */
1326                 else
1327                   extra_shift = 0;
1328               
1329                 mn10300_insert_operand (&insn, &extension, operand,
1330                                         ex.X_add_number, (char *) NULL,
1331                                         0, extra_shift);
1332
1333                 break;
1334               }
1335
1336             case O_constant:
1337               /* If this operand can be promoted, and it doesn't
1338                  fit into the allocated bitfield for this insn,
1339                  then promote it (ie this opcode does not match).  */
1340               if (operand->flags
1341                   & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
1342                   && ! check_operand (insn, operand, ex.X_add_number))
1343                 {
1344                   input_line_pointer = hold;
1345                   str = hold;
1346                   goto error;
1347                 }
1348
1349               mn10300_insert_operand (&insn, &extension, operand,
1350                                       ex.X_add_number, (char *) NULL,
1351                                       0, 0);
1352               break;
1353
1354             default:
1355               /* If this operand can be promoted, then this opcode didn't
1356                  match since we can't know if it needed promotion!  */
1357               if (operand->flags & MN10300_OPERAND_PROMOTE)
1358                 {
1359                   input_line_pointer = hold;
1360                   str = hold;
1361                   goto error;
1362                 }
1363
1364               /* We need to generate a fixup for this expression.  */
1365               if (fc >= MAX_INSN_FIXUPS)
1366                 as_fatal (_("too many fixups"));
1367               fixups[fc].exp = ex;
1368               fixups[fc].opindex = *opindex_ptr;
1369               fixups[fc].reloc = BFD_RELOC_UNUSED;
1370               ++fc;
1371               break;
1372             }
1373
1374 keep_going:
1375           str = input_line_pointer;
1376           input_line_pointer = hold;
1377
1378           while (*str == ' ' || *str == ',')
1379             ++str;
1380
1381         }
1382
1383       /* Make sure we used all the operands!  */
1384       if (*str != ',')
1385         match = 1;
1386
1387     error:
1388       if (match == 0)
1389         {
1390           next_opcode = opcode + 1;
1391           if (!strcmp(next_opcode->name, opcode->name))
1392             {
1393               opcode = next_opcode;
1394               continue;
1395             }
1396           
1397           as_bad ("%s", errmsg);
1398           return;
1399         }
1400       break;
1401     }
1402       
1403   while (isspace (*str))
1404     ++str;
1405
1406   if (*str != '\0')
1407     as_bad (_("junk at end of line: `%s'"), str);
1408
1409   input_line_pointer = str;
1410
1411   /* Determine the size of the instruction.  */
1412   if (opcode->format == FMT_S0)
1413     size = 1;
1414
1415   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
1416     size = 2;
1417
1418   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
1419     size = 3;
1420
1421   /* start-sanitize-am33 */
1422   if (opcode->format == FMT_D6)
1423     size = 3;
1424
1425   if (opcode->format == FMT_D7 || opcode->format == FMT_D10)
1426     size = 4;
1427
1428   if (opcode->format == FMT_D8)
1429     size = 6;
1430
1431   if (opcode->format == FMT_D9)
1432     size = 7;
1433   /* end-sanitize-am33 */
1434
1435   if (opcode->format == FMT_S4)
1436     size = 5;
1437
1438   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
1439     size = 7;
1440
1441   if (opcode->format == FMT_D2)
1442     size = 4;
1443
1444   if (opcode->format == FMT_D4)
1445     size = 6;
1446
1447   if (relaxable && fc > 0)
1448     {
1449       int type;
1450
1451       /* bCC */
1452       if (size == 2)
1453         {
1454           /* Handle bra specially.  Basically treat it like jmp so
1455              that we automatically handle 8, 16 and 32 bit offsets
1456              correctly as well as jumps to an undefined address.
1457
1458              It is also important to not treat it like other bCC
1459              instructions since the long forms of bra is different
1460              from other bCC instructions.  */
1461           if (opcode->opcode == 0xca00)
1462             type = 10;
1463           else
1464             type = 0;
1465         }
1466       /* call */
1467       else if (size == 5)
1468         type = 6;
1469       /* calls */
1470       else if (size == 4)
1471         type = 8;
1472       /* jmp */
1473       else if (size == 3 && opcode->opcode == 0xcc0000)
1474         type = 10;
1475       /* bCC (uncommon cases) */
1476       else
1477         type = 3;
1478
1479       f = frag_var (rs_machine_dependent, 8, 8 - size, type,
1480                     fixups[0].exp.X_add_symbol,
1481                     fixups[0].exp.X_add_number,
1482                     (char *)fixups[0].opindex);
1483       
1484       /* This is pretty hokey.  We basically just care about the
1485          opcode, so we have to write out the first word big endian.
1486
1487          The exception is "call", which has two operands that we
1488          care about.
1489
1490          The first operand (the register list) happens to be in the
1491          first instruction word, and will be in the right place if
1492          we output the first word in big endian mode.
1493
1494          The second operand (stack size) is in the extension word,
1495          and we want it to appear as the first character in the extension
1496          word (as it appears in memory).  Luckily, writing the extension
1497          word in big endian format will do what we want.  */
1498       number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
1499       if (size > 8)
1500         {
1501           number_to_chars_bigendian (f + 4, extension, 4);
1502           number_to_chars_bigendian (f + 8, 0, size - 8);
1503         }
1504       else if (size > 4)
1505         number_to_chars_bigendian (f + 4, extension, size - 4);
1506     }
1507   else
1508     {
1509       /* Allocate space for the instruction.  */
1510       f = frag_more (size);
1511
1512       /* Fill in bytes for the instruction.  Note that opcode fields
1513          are written big-endian, 16 & 32bit immediates are written
1514          little endian.  Egad.  */
1515       if (opcode->format == FMT_S0
1516           || opcode->format == FMT_S1
1517           || opcode->format == FMT_D0
1518           /* start-sanitize-am33 */
1519           || opcode->format == FMT_D6
1520           || opcode->format == FMT_D7
1521           || opcode->format == FMT_D10
1522           /* end-sanitize-am33 */
1523           || opcode->format == FMT_D1)
1524         {
1525           number_to_chars_bigendian (f, insn, size);
1526         }
1527       else if (opcode->format == FMT_S2
1528                && opcode->opcode != 0xdf0000
1529                && opcode->opcode != 0xde0000)
1530         {
1531           /* A format S2 instruction that is _not_ "ret" and "retf".  */
1532           number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
1533           number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
1534         }
1535       else if (opcode->format == FMT_S2)
1536         {
1537           /* This must be a ret or retf, which is written entirely in
1538              big-endian format.  */
1539           number_to_chars_bigendian (f, insn, 3);
1540         }
1541       else if (opcode->format == FMT_S4
1542                && opcode->opcode != 0xdc000000)
1543         {
1544           /* This must be a format S4 "call" instruction.  What a pain.  */
1545           unsigned long temp = (insn >> 8) & 0xffff;
1546           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1547           number_to_chars_littleendian (f + 1, temp, 2);
1548           number_to_chars_bigendian (f + 3, insn & 0xff, 1);
1549           number_to_chars_bigendian (f + 4, extension & 0xff, 1);
1550         }
1551       else if (opcode->format == FMT_S4)
1552         {
1553           /* This must be a format S4 "jmp" instruction.  */
1554           unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
1555           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1556           number_to_chars_littleendian (f + 1, temp, 4);
1557         }
1558       else if (opcode->format == FMT_S6)
1559         {
1560           unsigned long temp = ((insn & 0xffffff) << 8)
1561             | ((extension >> 16) & 0xff);
1562           number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1563           number_to_chars_littleendian (f + 1, temp, 4);
1564           number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
1565           number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1566         }
1567       else if (opcode->format == FMT_D2
1568                && opcode->opcode != 0xfaf80000
1569                && opcode->opcode != 0xfaf00000
1570                && opcode->opcode != 0xfaf40000)
1571         {
1572           /* A format D2 instruction where the 16bit immediate is
1573              really a single 16bit value, not two 8bit values.  */
1574           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1575           number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1576         }
1577       else if (opcode->format == FMT_D2)
1578         {
1579           /* A format D2 instruction where the 16bit immediate
1580              is really two 8bit immediates.  */
1581           number_to_chars_bigendian (f, insn, 4);
1582         }
1583       else if (opcode->format == FMT_D4)
1584         {
1585           unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
1586           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1587           number_to_chars_littleendian (f + 2, temp, 4);
1588         }
1589       else if (opcode->format == FMT_D5)
1590         {
1591           unsigned long temp = ((insn & 0xffff) << 16)
1592                                 | ((extension >> 8) & 0xffff);
1593           number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1594           number_to_chars_littleendian (f + 2, temp, 4);
1595           number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1596         }
1597       /* start-sanitize-am33 */
1598       else if (opcode->format == FMT_D8)
1599         {
1600           unsigned long temp = ((insn & 0xff) << 16) | (extension & 0xffff);
1601           number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
1602           number_to_chars_bigendian (f + 3, (temp & 0xff), 1);
1603           number_to_chars_littleendian (f + 4, temp >> 8, 2);
1604         }
1605       else if (opcode->format == FMT_D9)
1606         {
1607           unsigned long temp = ((insn & 0xff) << 24) | (extension & 0xffffff);
1608           number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
1609           number_to_chars_littleendian (f + 3, temp, 4);
1610         }
1611       /* end-sanitize-am33 */
1612
1613       /* Create any fixups.  */
1614       for (i = 0; i < fc; i++)
1615         {
1616           const struct mn10300_operand *operand;
1617
1618           operand = &mn10300_operands[fixups[i].opindex];
1619           if (fixups[i].reloc != BFD_RELOC_UNUSED)
1620             {
1621               reloc_howto_type *reloc_howto;
1622               int size;
1623               int offset;
1624               fixS *fixP;
1625
1626               reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1627
1628               if (!reloc_howto)
1629                 abort();
1630           
1631               size = bfd_get_reloc_size (reloc_howto);
1632
1633               if (size < 1 || size > 4)
1634                 abort();
1635
1636               offset = 4 - size;
1637               fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1638                                   size, &fixups[i].exp,
1639                                   reloc_howto->pc_relative,
1640                                   fixups[i].reloc);
1641             }
1642           else
1643             {
1644               int reloc, pcrel, reloc_size, offset;
1645               fixS *fixP;
1646
1647               reloc = BFD_RELOC_NONE;
1648               /* How big is the reloc?  Remember SPLIT relocs are
1649                  implicitly 32bits.  */
1650               if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1651                 reloc_size = 32;
1652               /* start-sanitize-am33 */
1653               else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
1654                 reloc_size = 24;
1655               /* end-sanitize-am33 */
1656               else
1657                 reloc_size = operand->bits;
1658
1659               /* Is the reloc pc-relative?  */
1660               pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
1661
1662               /* Gross.  This disgusting hack is to make sure we
1663                  get the right offset for the 16/32 bit reloc in 
1664                  "call" instructions.  Basically they're a pain
1665                  because the reloc isn't at the end of the instruction.  */
1666               if ((size == 5 || size == 7)
1667                   && (((insn >> 24) & 0xff) == 0xcd
1668                       || ((insn >> 24) & 0xff) == 0xdd))
1669                 size -= 2;
1670
1671               /* Similarly for certain bit instructions which don't
1672                  hav their 32bit reloc at the tail of the instruction.  */
1673               if (size == 7
1674                   && (((insn >> 16) & 0xffff) == 0xfe00
1675                       || ((insn >> 16) & 0xffff) == 0xfe01
1676                       || ((insn >> 16) & 0xffff) == 0xfe02))
1677                 size -= 1;
1678         
1679               offset = size - reloc_size / 8;
1680
1681               /* Choose a proper BFD relocation type.  */
1682               if (pcrel)
1683                 {
1684                   if (reloc_size == 32)
1685                     reloc = BFD_RELOC_32_PCREL;
1686                   else if (reloc_size == 16)
1687                     reloc = BFD_RELOC_16_PCREL;
1688                   else if (reloc_size == 8)
1689                     reloc = BFD_RELOC_8_PCREL;
1690                   else
1691                     abort ();
1692                 }
1693               else
1694                 {
1695                   if (reloc_size == 32)
1696                     reloc = BFD_RELOC_32;
1697                   else if (reloc_size == 16)
1698                     reloc = BFD_RELOC_16;
1699                   else if (reloc_size == 8)
1700                     reloc = BFD_RELOC_8;
1701                   else
1702                     abort ();
1703                 }
1704
1705               /* Convert the size of the reloc into what fix_new_exp wants.  */
1706               reloc_size = reloc_size / 8;
1707               if (reloc_size == 8)
1708                 reloc_size = 0;
1709               else if (reloc_size == 16)
1710                 reloc_size = 1;
1711               else if (reloc_size == 32)
1712                 reloc_size = 2;
1713
1714               fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1715                                   reloc_size, &fixups[i].exp, pcrel,
1716                                   ((bfd_reloc_code_real_type) reloc));
1717
1718               if (pcrel)
1719                 fixP->fx_offset += offset;
1720             }
1721         }
1722     }
1723 }
1724
1725
1726 /* if while processing a fixup, a reloc really needs to be created */
1727 /* then it is done here */
1728                  
1729 arelent *
1730 tc_gen_reloc (seg, fixp)
1731      asection *seg;
1732      fixS *fixp;
1733 {
1734   arelent *reloc;
1735   reloc = (arelent *) xmalloc (sizeof (arelent));
1736
1737   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1738   if (reloc->howto == (reloc_howto_type *) NULL)
1739     {
1740       as_bad_where (fixp->fx_file, fixp->fx_line,
1741                     _("reloc %d not supported by object file format"),
1742                     (int)fixp->fx_r_type);
1743       return NULL;
1744     }
1745   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1746
1747   if (fixp->fx_addsy && fixp->fx_subsy)
1748     {
1749     
1750       if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
1751           || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
1752         {
1753           as_bad_where (fixp->fx_file, fixp->fx_line,
1754                         "Difference of symbols in different sections is not supported");
1755           return NULL;
1756         }
1757
1758       reloc->sym_ptr_ptr = &bfd_abs_symbol;
1759       reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
1760                        - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
1761     }
1762   else 
1763     {
1764       reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1765       reloc->addend = fixp->fx_offset;
1766     }
1767   return reloc;
1768 }
1769
1770 int
1771 md_estimate_size_before_relax (fragp, seg)
1772      fragS *fragp;
1773      asection *seg;
1774 {
1775   if (fragp->fr_subtype == 0)
1776     return 2;
1777   if (fragp->fr_subtype == 3)
1778     return 3;
1779   if (fragp->fr_subtype == 6)
1780     {
1781       if (!S_IS_DEFINED (fragp->fr_symbol)
1782           || seg != S_GET_SEGMENT (fragp->fr_symbol))
1783         {
1784           fragp->fr_subtype = 7;
1785           return 7;
1786         }
1787       else
1788         return 5;
1789     }
1790   if (fragp->fr_subtype == 8)
1791     {
1792       if (!S_IS_DEFINED (fragp->fr_symbol)
1793           || seg != S_GET_SEGMENT (fragp->fr_symbol))
1794         {
1795           fragp->fr_subtype = 9;
1796           return 6;
1797         }
1798       else
1799         return 4;
1800     }
1801   if (fragp->fr_subtype == 10)
1802     {
1803       if (!S_IS_DEFINED (fragp->fr_symbol)
1804           || seg != S_GET_SEGMENT (fragp->fr_symbol))
1805         {
1806           fragp->fr_subtype = 12;
1807           return 5;
1808         }
1809       else
1810         return 2;
1811     }
1812
1813
1814 long
1815 md_pcrel_from (fixp)
1816      fixS *fixp;
1817 {
1818   return fixp->fx_frag->fr_address;
1819 #if 0
1820   if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1821     {
1822       /* The symbol is undefined.  Let the linker figure it out.  */
1823       return 0;
1824     }
1825   return fixp->fx_frag->fr_address + fixp->fx_where;
1826 #endif
1827 }
1828
1829 int
1830 md_apply_fix3 (fixp, valuep, seg)
1831      fixS *fixp;
1832      valueT *valuep;
1833      segT seg;
1834 {
1835   /* We shouldn't ever get here because linkrelax is nonzero.  */
1836   abort ();
1837   fixp->fx_done = 1;
1838   return 0;
1839 }
1840
1841 /* Insert an operand value into an instruction.  */
1842
1843 static void
1844 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
1845      unsigned long *insnp;
1846      unsigned long *extensionp;
1847      const struct mn10300_operand *operand;
1848      offsetT val;
1849      char *file;
1850      unsigned int line;
1851      unsigned int shift;
1852 {
1853   /* No need to check 32bit operands for a bit.  Note that
1854      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
1855   if (operand->bits != 32
1856       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1857     {
1858       long min, max;
1859       offsetT test;
1860       int bits;
1861
1862       bits = operand->bits;
1863       /* start-sanitize-am33 */
1864       if (operand->flags & MN10300_OPERAND_24BIT)
1865         bits = 24;
1866       /* end-sanitize-am33 */
1867
1868       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1869         {
1870           max = (1 << (bits - 1)) - 1;
1871           min = - (1 << (bits - 1));
1872         }
1873       else
1874         {
1875           max = (1 << bits) - 1;
1876           min = 0;
1877         }
1878
1879       test = val;
1880
1881
1882       if (test < (offsetT) min || test > (offsetT) max)
1883         {
1884           const char *err =
1885             _("operand out of range (%s not between %ld and %ld)");
1886           char buf[100];
1887
1888           sprint_value (buf, test);
1889           if (file == (char *) NULL)
1890             as_warn (err, buf, min, max);
1891           else
1892             as_warn_where (file, line, err, buf, min, max);
1893         }
1894     }
1895
1896   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1897     {
1898       *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
1899       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
1900                       << operand->shift);
1901     }
1902   /* start-sanitize-am33 */
1903   else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
1904     {
1905       *insnp |= (val >> (24 - operand->bits)) & ((1 << operand->bits) - 1);
1906       *extensionp |= ((val & ((1 << (24 - operand->bits)) - 1))
1907                       << operand->shift);
1908     }
1909   /* end-sanitize-am33 */
1910   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
1911     {
1912       *insnp |= (((long) val & ((1 << operand->bits) - 1))
1913                  << (operand->shift + shift));
1914
1915       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1916         *insnp |= (((long) val & ((1 << operand->bits) - 1))
1917                    << (operand->shift + shift + operand->bits));
1918     }
1919   else
1920     {
1921       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1922                       << (operand->shift + shift));
1923
1924       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1925         *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1926                         << (operand->shift + shift + operand->bits));
1927     }
1928 }
1929
1930 static unsigned long
1931 check_operand (insn, operand, val)
1932      unsigned long insn;
1933      const struct mn10300_operand *operand;
1934      offsetT val;
1935 {
1936   /* No need to check 32bit operands for a bit.  Note that
1937      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
1938   if (operand->bits != 32
1939       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1940     {
1941       long min, max;
1942       offsetT test;
1943       int bits;
1944
1945       bits = operand->bits;
1946       /* start-sanitize-am33 */
1947       if (operand->flags & MN10300_OPERAND_24BIT)
1948         bits = 24;
1949       /* end-sanitize-am33 */
1950
1951       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1952         {
1953           max = (1 << (bits - 1)) - 1;
1954           min = - (1 << (bits - 1));
1955         }
1956       else
1957         {
1958           max = (1 << bits) - 1;
1959           min = 0;
1960         }
1961
1962       test = val;
1963
1964
1965       if (test < (offsetT) min || test > (offsetT) max)
1966         return 0;
1967       else
1968         return 1;
1969     }
1970   return 1;
1971 }
1972
1973 static void
1974 set_arch_mach (mach)
1975      int mach;
1976 {
1977   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
1978     as_warn (_("could not set architecture and machine"));
1979
1980   current_machine = mach;
1981 }