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