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