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