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