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