* config/tc-mn10300.c (md_assemble): Don't use any MN10300 specific
[external/binutils.git] / gas / config / tc-mn10300.c
1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
2
3    Copyright (C) 1996 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 /* local functions */
57 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
58                                             const struct mn10300_operand *,
59                                             offsetT, char *, unsigned,
60                                             unsigned));
61 static unsigned long check_operand PARAMS ((unsigned long,
62                                             const struct mn10300_operand *,
63                                             offsetT));
64 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
65 static boolean data_register_name PARAMS ((expressionS *expressionP));
66 static boolean address_register_name PARAMS ((expressionS *expressionP));
67 static boolean other_register_name PARAMS ((expressionS *expressionP));
68
69
70 /* fixups */
71 #define MAX_INSN_FIXUPS (5)
72 struct mn10300_fixup
73 {
74   expressionS exp;
75   int opindex;
76   bfd_reloc_code_real_type reloc;
77 };
78 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
79 static int fc;
80 \f
81 const char *md_shortopts = "";
82 struct option md_longopts[] = {
83   {NULL, no_argument, NULL, 0}
84 };
85 size_t md_longopts_size = sizeof(md_longopts); 
86
87 /* The target specific pseudo-ops which we support.  */
88 const pseudo_typeS md_pseudo_table[] =
89 {
90   { NULL,       NULL,           0 }
91 };
92
93 /* Opcode hash table.  */
94 static struct hash_control *mn10300_hash;
95
96 /* This table is sorted. Suitable for searching by a binary search. */
97 static const struct reg_name data_registers[] =
98 {
99   { "d0", 0 },
100   { "d1", 1 },
101   { "d2", 2 },
102   { "d3", 3 },
103 };
104 #define DATA_REG_NAME_CNT       (sizeof(data_registers) / sizeof(struct reg_name))
105
106 static const struct reg_name address_registers[] =
107 {
108   { "a0", 0 },
109   { "a1", 1 },
110   { "a2", 2 },
111   { "a3", 3 },
112 };
113 #define ADDRESS_REG_NAME_CNT    (sizeof(address_registers) / sizeof(struct reg_name))
114
115 static const struct reg_name other_registers[] =
116 {
117   { "mdr", 0 },
118   { "psw", 0 },
119   { "sp", 0 },
120 };
121 #define OTHER_REG_NAME_CNT      (sizeof(other_registers) / sizeof(struct reg_name))
122
123 /* reg_name_search does a binary search of the given register table
124    to see if "name" is a valid regiter name.  Returns the register
125    number from the array on success, or -1 on failure. */
126
127 static int
128 reg_name_search (regs, regcount, name)
129      const struct reg_name *regs;
130      int regcount;
131      const char *name;
132 {
133   int middle, low, high;
134   int cmp;
135
136   low = 0;
137   high = regcount - 1;
138
139   do
140     {
141       middle = (low + high) / 2;
142       cmp = strcasecmp (name, regs[middle].name);
143       if (cmp < 0)
144         high = middle - 1;
145       else if (cmp > 0)
146         low = middle + 1;
147       else 
148           return regs[middle].value;
149     }
150   while (low <= high);
151   return -1;
152 }
153
154
155 /* Summary of register_name().
156  *
157  * in: Input_line_pointer points to 1st char of operand.
158  *
159  * out: A expressionS.
160  *      The operand may have been a register: in this case, X_op == O_register,
161  *      X_add_number is set to the register number, and truth is returned.
162  *      Input_line_pointer->(next non-blank) char after operand, or is in
163  *      its original state.
164  */
165 static boolean
166 data_register_name (expressionP)
167      expressionS *expressionP;
168 {
169   int reg_number;
170   char *name;
171   char *start;
172   char c;
173
174   /* Find the spelling of the operand */
175   start = name = input_line_pointer;
176
177   c = get_symbol_end ();
178   reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
179
180   /* look to see if it's in the register table */
181   if (reg_number >= 0) 
182     {
183       expressionP->X_op = O_register;
184       expressionP->X_add_number = reg_number;
185
186       /* make the rest nice */
187       expressionP->X_add_symbol = NULL;
188       expressionP->X_op_symbol = NULL;
189       *input_line_pointer = c;  /* put back the delimiting char */
190       return true;
191     }
192   else
193     {
194       /* reset the line as if we had not done anything */
195       *input_line_pointer = c;   /* put back the delimiting char */
196       input_line_pointer = start; /* reset input_line pointer */
197       return false;
198     }
199 }
200
201 /* Summary of register_name().
202  *
203  * in: Input_line_pointer points to 1st char of operand.
204  *
205  * out: A expressionS.
206  *      The operand may have been a register: in this case, X_op == O_register,
207  *      X_add_number is set to the register number, and truth is returned.
208  *      Input_line_pointer->(next non-blank) char after operand, or is in
209  *      its original state.
210  */
211 static boolean
212 address_register_name (expressionP)
213      expressionS *expressionP;
214 {
215   int reg_number;
216   char *name;
217   char *start;
218   char c;
219
220   /* Find the spelling of the operand */
221   start = name = input_line_pointer;
222
223   c = get_symbol_end ();
224   reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
225
226   /* look to see if it's in the register table */
227   if (reg_number >= 0) 
228     {
229       expressionP->X_op = O_register;
230       expressionP->X_add_number = reg_number;
231
232       /* make the rest nice */
233       expressionP->X_add_symbol = NULL;
234       expressionP->X_op_symbol = NULL;
235       *input_line_pointer = c;  /* put back the delimiting char */
236       return true;
237     }
238   else
239     {
240       /* reset the line as if we had not done anything */
241       *input_line_pointer = c;   /* put back the delimiting char */
242       input_line_pointer = start; /* reset input_line pointer */
243       return false;
244     }
245 }
246
247 /* Summary of register_name().
248  *
249  * in: Input_line_pointer points to 1st char of operand.
250  *
251  * out: A expressionS.
252  *      The operand may have been a register: in this case, X_op == O_register,
253  *      X_add_number is set to the register number, and truth is returned.
254  *      Input_line_pointer->(next non-blank) char after operand, or is in
255  *      its original state.
256  */
257 static boolean
258 other_register_name (expressionP)
259      expressionS *expressionP;
260 {
261   int reg_number;
262   char *name;
263   char *start;
264   char c;
265
266   /* Find the spelling of the operand */
267   start = name = input_line_pointer;
268
269   c = get_symbol_end ();
270   reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
271
272   /* look to see if it's in the register table */
273   if (reg_number >= 0) 
274     {
275       expressionP->X_op = O_register;
276       expressionP->X_add_number = reg_number;
277
278       /* make the rest nice */
279       expressionP->X_add_symbol = NULL;
280       expressionP->X_op_symbol = NULL;
281       *input_line_pointer = c;  /* put back the delimiting char */
282       return true;
283     }
284   else
285     {
286       /* reset the line as if we had not done anything */
287       *input_line_pointer = c;   /* put back the delimiting char */
288       input_line_pointer = start; /* reset input_line pointer */
289       return false;
290     }
291 }
292
293 void
294 md_show_usage (stream)
295   FILE *stream;
296 {
297   fprintf(stream, "MN10300 options:\n\
298 none yet\n");
299
300
301 int
302 md_parse_option (c, arg)
303      int c;
304      char *arg;
305 {
306   return 0;
307 }
308
309 symbolS *
310 md_undefined_symbol (name)
311   char *name;
312 {
313   return 0;
314 }
315
316 char *
317 md_atof (type, litp, sizep)
318   int type;
319   char *litp;
320   int *sizep;
321 {
322   int prec;
323   LITTLENUM_TYPE words[4];
324   char *t;
325   int i;
326
327   switch (type)
328     {
329     case 'f':
330       prec = 2;
331       break;
332
333     case 'd':
334       prec = 4;
335       break;
336
337     default:
338       *sizep = 0;
339       return "bad call to md_atof";
340     }
341   
342   t = atof_ieee (input_line_pointer, type, words);
343   if (t)
344     input_line_pointer = t;
345
346   *sizep = prec * 2;
347
348   for (i = prec - 1; i >= 0; i--)
349     {
350       md_number_to_chars (litp, (valueT) words[i], 2);
351       litp += 2;
352     }
353
354   return NULL;
355 }
356
357
358 void
359 md_convert_frag (abfd, sec, fragP)
360   bfd *abfd;
361   asection *sec;
362   fragS *fragP;
363 {
364   /* printf ("call to md_convert_frag \n"); */
365   abort ();
366 }
367
368 valueT
369 md_section_align (seg, addr)
370      asection *seg;
371      valueT addr;
372 {
373   int align = bfd_get_section_alignment (stdoutput, seg);
374   return ((addr + (1 << align) - 1) & (-1 << align));
375 }
376
377 void
378 md_begin ()
379 {
380   char *prev_name = "";
381   register const struct mn10300_opcode *op;
382
383   mn10300_hash = hash_new();
384
385   /* Insert unique names into hash table.  The MN10300 instruction set
386      has many identical opcode names that have different opcodes based
387      on the operands.  This hash table then provides a quick index to
388      the first opcode with a particular name in the opcode table.  */
389
390   op = mn10300_opcodes;
391   while (op->name)
392     {
393       if (strcmp (prev_name, op->name)) 
394         {
395           prev_name = (char *) op->name;
396           hash_insert (mn10300_hash, op->name, (char *) op);
397         }
398       op++;
399     }
400
401   /* This is both a simplification (we don't have to write md_apply_fix)
402      and support for future optimizations (branch shortening and similar
403      stuff in the linker.  */
404   linkrelax = 1;
405 }
406
407 void
408 md_assemble (str) 
409      char *str;
410 {
411   char *s;
412   struct mn10300_opcode *opcode;
413   struct mn10300_opcode *next_opcode;
414   const unsigned char *opindex_ptr;
415   int next_opindex;
416   unsigned long insn, extension, size = 0;
417   char *f;
418   int i;
419   int match;
420
421   /* Get the opcode.  */
422   for (s = str; *s != '\0' && ! isspace (*s); s++)
423     ;
424   if (*s != '\0')
425     *s++ = '\0';
426
427   /* find the first opcode with the proper name */
428   opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
429   if (opcode == NULL)
430     {
431       as_bad ("Unrecognized opcode: `%s'", str);
432       return;
433     }
434
435   str = s;
436   while (isspace (*str))
437     ++str;
438
439   input_line_pointer = str;
440
441   for(;;)
442     {
443       const char *errmsg = NULL;
444       int op_idx;
445       char *hold;
446       int extra_shift = 0;
447
448       fc = 0;
449       match = 0;
450       next_opindex = 0;
451       insn = opcode->opcode;
452       extension = 0;
453       for (op_idx = 1, opindex_ptr = opcode->operands;
454            *opindex_ptr != 0;
455            opindex_ptr++, op_idx++)
456         {
457           const struct mn10300_operand *operand;
458           expressionS ex;
459
460           if (next_opindex == 0)
461             {
462               operand = &mn10300_operands[*opindex_ptr];
463             }
464           else
465             {
466               operand = &mn10300_operands[next_opindex];
467               next_opindex = 0;
468             }
469
470           errmsg = NULL;
471
472           while (*str == ' ' || *str == ',')
473             ++str;
474
475           /* Gather the operand. */
476           hold = input_line_pointer;
477           input_line_pointer = str;
478
479           if (operand->flags & MN10300_OPERAND_PAREN)
480             {
481               if (*input_line_pointer != ')' && *input_line_pointer != '(')
482                 {
483                   input_line_pointer = hold;
484                   str = hold;
485                   goto error;
486                 }
487               input_line_pointer++;
488               goto keep_going;
489             }
490           /* See if we can match the operands.  */
491           else if (operand->flags & MN10300_OPERAND_DREG)
492             {
493               if (!data_register_name (&ex))
494                 {
495                   input_line_pointer = hold;
496                   str = hold;
497                   goto error;
498                 }
499             }
500           else if (operand->flags & MN10300_OPERAND_AREG)
501             {
502               if (!address_register_name (&ex))
503                 {
504                   input_line_pointer = hold;
505                   str = hold;
506                   goto error;
507                 }
508             }
509           else if (operand->flags & MN10300_OPERAND_SP)
510             {
511               char *start = input_line_pointer;
512               char c = get_symbol_end ();
513
514               if (strcmp (start, "sp") != 0)
515                 {
516                   *input_line_pointer = c;
517                   input_line_pointer = hold;
518                   str = hold;
519                   goto error;
520                 }
521               *input_line_pointer = c;
522               goto keep_going;
523             }
524           else if (operand->flags & MN10300_OPERAND_PSW)
525             {
526               char *start = input_line_pointer;
527               char c = get_symbol_end ();
528
529               if (strcmp (start, "psw") != 0)
530                 {
531                   *input_line_pointer = c;
532                   input_line_pointer = hold;
533                   str = hold;
534                   goto error;
535                 }
536               *input_line_pointer = c;
537               goto keep_going;
538             }
539           else if (operand->flags & MN10300_OPERAND_MDR)
540             {
541               char *start = input_line_pointer;
542               char c = get_symbol_end ();
543
544               if (strcmp (start, "mdr") != 0)
545                 {
546                   *input_line_pointer = c;
547                   input_line_pointer = hold;
548                   str = hold;
549                   goto error;
550                 }
551               *input_line_pointer = c;
552               goto keep_going;
553             }
554           else if (operand->flags & MN10300_OPERAND_REG_LIST)
555             {
556               unsigned int value = 0;
557               if (*input_line_pointer != '[')
558                 {
559                   input_line_pointer = hold;
560                   str = hold;
561                   goto error;
562                 }
563
564               /* Eat the '['.  */
565               input_line_pointer++;
566              
567               /* A null register list can not be specified.  */
568               if (*input_line_pointer == ']')
569                 {
570                   input_line_pointer = hold;
571                   str = hold;
572                   goto error;
573                 }
574
575               while (*input_line_pointer != ']')
576                 {
577                   char *start;
578                   char c;
579
580                   if (*input_line_pointer == ',')
581                     input_line_pointer++;
582
583                   start = input_line_pointer;
584                   c = get_symbol_end ();
585
586                   if (strcmp (start, "d2") == 0)
587                     {
588                       value |= 0x80;
589                       *input_line_pointer = c;
590                     }
591                   else if (strcmp (start, "d3") == 0)
592                     {
593                       value |= 0x40;
594                       *input_line_pointer = c;
595                     }
596                   else if (strcmp (start, "a2") == 0)
597                     {
598                       value |= 0x20;
599                       *input_line_pointer = c;
600                     }
601                   else if (strcmp (start, "a3") == 0)
602                     {
603                       value |= 0x10;
604                       *input_line_pointer = c;
605                     }
606                   else if (strcmp (start, "other") == 0)
607                     {
608                       value |= 0x08;
609                       *input_line_pointer = c;
610                     }
611                   else
612                     {
613                       input_line_pointer = hold;
614                       str = hold;
615                       goto error;
616                     }
617                 }
618               input_line_pointer++;
619               mn10300_insert_operand (&insn, &extension, operand,
620                                       value, (char *) NULL, 0, 0);
621               goto keep_going;
622
623             }
624           else if (data_register_name (&ex))
625             {
626               input_line_pointer = hold;
627               str = hold;
628               goto error;
629             }
630           else if (address_register_name (&ex))
631             {
632               input_line_pointer = hold;
633               str = hold;
634               goto error;
635             }
636           else if (other_register_name (&ex))
637             {
638               input_line_pointer = hold;
639               str = hold;
640               goto error;
641             }
642           else if (*str == ')' || *str == '(')
643             {
644               input_line_pointer = hold;
645               str = hold;
646               goto error;
647             }
648           else
649             {
650               expression (&ex);
651             }
652
653           switch (ex.X_op) 
654             {
655             case O_illegal:
656               errmsg = "illegal operand";
657               goto error;
658             case O_absent:
659               errmsg = "missing operand";
660               goto error;
661             case O_register:
662               if ((operand->flags
663                    & (MN10300_OPERAND_DREG | MN10300_OPERAND_AREG)) == 0)
664                 {
665                   input_line_pointer = hold;
666                   str = hold;
667                   goto error;
668                 }
669                 
670               if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
671                 extra_shift = 8;
672               else if (opcode->format == FMT_D2 || opcode->format == FMT_D4
673                        || opcode->format == FMT_S2 || opcode->format == FMT_S4
674                        || opcode->format == FMT_S6 || opcode->format == FMT_D5)
675                 extra_shift = 16;
676               else
677                 extra_shift = 0;
678               
679               mn10300_insert_operand (&insn, &extension, operand,
680                                       ex.X_add_number, (char *) NULL,
681                                       0, extra_shift);
682
683               break;
684
685             case O_constant:
686               /* If this operand can be promoted, and it doesn't
687                  fit into the allocated bitfield for this insn,
688                  then promote it (ie this opcode does not match).  */
689               if (operand->flags & MN10300_OPERAND_PROMOTE
690                   && ! check_operand (insn, operand, ex.X_add_number))
691                 {
692                   input_line_pointer = hold;
693                   str = hold;
694                   goto error;
695                 }
696
697               mn10300_insert_operand (&insn, &extension, operand,
698                                       ex.X_add_number, (char *) NULL,
699                                       0, 0);
700               break;
701
702             default:
703               /* If this operand can be promoted, then this opcode didn't
704                  match since we can't know if it needed promotion!  */
705               if (operand->flags & MN10300_OPERAND_PROMOTE)
706                 {
707                   input_line_pointer = hold;
708                   str = hold;
709                   goto error;
710                 }
711
712               /* We need to generate a fixup for this expression.  */
713               if (fc >= MAX_INSN_FIXUPS)
714                 as_fatal ("too many fixups");
715               fixups[fc].exp = ex;
716               fixups[fc].opindex = *opindex_ptr;
717               fixups[fc].reloc = BFD_RELOC_UNUSED;
718               ++fc;
719               break;
720             }
721
722 keep_going:
723           str = input_line_pointer;
724           input_line_pointer = hold;
725
726           while (*str == ' ' || *str == ',')
727             ++str;
728
729         }
730
731       /* Make sure we used all the operands!  */
732       if (*str != ',')
733         match = 1;
734
735     error:
736       if (match == 0)
737         {
738           next_opcode = opcode + 1;
739           if (!strcmp(next_opcode->name, opcode->name))
740             {
741               opcode = next_opcode;
742               continue;
743             }
744           
745           as_bad ("%s", errmsg);
746           return;
747         }
748       break;
749     }
750       
751   while (isspace (*str))
752     ++str;
753
754   if (*str != '\0')
755     as_bad ("junk at end of line: `%s'", str);
756
757   input_line_pointer = str;
758
759   /* Determine the size of the instruction.  */
760   if (opcode->format == FMT_S0)
761     size = 1;
762
763   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
764     size = 2;
765
766   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
767     size = 3;
768
769   if (opcode->format == FMT_S4)
770     size = 5;
771
772   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
773     size = 7;
774
775   if (opcode->format == FMT_D2)
776     size = 4;
777
778   if (opcode->format == FMT_D4)
779     size = 6;
780
781   /* Allocate space for the instruction.  */
782   f = frag_more (size);
783
784   /* Fill in bytes for the instruction.  Note that opcode fields
785      are written big-endian, 16 & 32bit immediates are written
786      little endian.  Egad.  */
787   if (opcode->format == FMT_S0
788       || opcode->format == FMT_S1
789       || opcode->format == FMT_D0
790       || opcode->format == FMT_D1)
791     {
792       number_to_chars_bigendian (f, insn, size);
793     }
794   else if (opcode->format == FMT_S2
795            && opcode->opcode != 0xdf0000
796            && opcode->opcode != 0xde0000)
797     {
798       /* A format S2 instruction that is _not_ "ret" and "retf".  */
799       number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
800       number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
801     }
802   else if (opcode->format == FMT_S2)
803     {
804       /* This must be a ret or retf, which is written entirely in big-endian
805          format.  */
806       number_to_chars_bigendian (f, insn, 3);
807     }
808   else if (opcode->format == FMT_S4
809            && opcode->opcode != 0xdc000000)
810     {
811       /* This must be a format S4 "call" instruction.  What a pain.  */
812       unsigned long temp = (insn >> 8) & 0xffff;
813       number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
814       number_to_chars_littleendian (f + 1, temp, 2);
815       number_to_chars_bigendian (f + 3, insn & 0xff, 1);
816       number_to_chars_bigendian (f + 4, extension & 0xff, 1);
817     }
818   else if (opcode->format == FMT_S4)
819     {
820       /* This must be a format S4 "jmp" instruction.  */
821       unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
822       number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
823       number_to_chars_littleendian (f + 1, temp, 4);
824     }
825   else if (opcode->format == FMT_S6)
826     {
827       unsigned long temp = ((insn & 0xffffff) << 8)
828                             | ((extension >> 16) & 0xff);
829       number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
830       number_to_chars_littleendian (f + 1, temp, 4);
831       number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
832       number_to_chars_bigendian (f + 6, extension & 0xff, 1);
833     }
834   else if (opcode->format == FMT_D2
835            && opcode->opcode != 0xfaf80000
836            && opcode->opcode != 0xfaf00000
837            && opcode->opcode != 0xfaf40000)
838     {
839       /* A format D2 instruction where the 16bit immediate is
840          really a single 16bit value, not two 8bit values.  */
841       number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
842       number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
843     }
844   else if (opcode->format == FMT_D2)
845     {
846       /* A format D2 instruction where the 16bit immediate
847          is really two 8bit immediates.  */
848       number_to_chars_bigendian (f, insn, 4);
849     }
850   else if (opcode->format == FMT_D4)
851     {
852       unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
853       number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
854       number_to_chars_littleendian (f + 2, temp, 4);
855     }
856   else if (opcode->format == FMT_D5)
857     {
858       unsigned long temp = ((insn & 0xffff) << 16) | ((extension >> 8) & 0xffff);
859       number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
860       number_to_chars_littleendian (f + 2, temp, 4);
861       number_to_chars_bigendian (f + 6, extension & 0xff, 1);
862     }
863
864   /* Create any fixups.  */
865   for (i = 0; i < fc; i++)
866     {
867       const struct mn10300_operand *operand;
868
869       operand = &mn10300_operands[fixups[i].opindex];
870       if (fixups[i].reloc != BFD_RELOC_UNUSED)
871         {
872           reloc_howto_type *reloc_howto;
873           int size;
874           int offset;
875           fixS *fixP;
876
877           reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
878
879           if (!reloc_howto)
880             abort();
881           
882           size = bfd_get_reloc_size (reloc_howto);
883
884           if (size < 1 || size > 4)
885             abort();
886
887           offset = 4 - size;
888           fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
889                               &fixups[i].exp, 
890                               reloc_howto->pc_relative,
891                               fixups[i].reloc);
892         }
893       else
894         {
895           int reloc, pcrel, reloc_size, offset;
896           fixS *fixP;
897
898           reloc = BFD_RELOC_NONE;
899           /* How big is the reloc?  Remember SPLIT relocs are
900              implicitly 32bits.  */
901           if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
902             reloc_size = 32;
903           else
904             reloc_size = operand->bits;
905
906           /* Is the reloc pc-relative?  */
907           pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
908
909           /* Gross.  This disgusting hack is to make sure we
910              get the right offset for the 16/32 bit reloc in 
911              "call" instructions.  Basically they're a pain
912              because the reloc isn't at the end of the instruction.  */
913           if ((size == 5 || size == 7)
914               && (((insn >> 24) & 0xff) == 0xcd
915                   || ((insn >> 24) & 0xff) == 0xdd))
916             size -= 2;
917
918           /* Similarly for certain bit instructions which don't
919              hav their 32bit reloc at the tail of the instruction.  */
920           if (size == 7
921               && (((insn >> 16) & 0xffff) == 0xfe00
922                   || ((insn >> 16) & 0xffff) == 0xfe01
923                   || ((insn >> 16) & 0xffff) == 0xfe02))
924             size -= 1;
925         
926           offset = size - reloc_size / 8;
927
928           /* Choose a proper BFD relocation type.  */
929           if (pcrel)
930             {
931               if (reloc_size == 32)
932                 reloc = BFD_RELOC_32_PCREL;
933               else if (reloc_size == 16)
934                 reloc = BFD_RELOC_16_PCREL;
935               else if (reloc_size == 8)
936                 reloc = BFD_RELOC_8_PCREL;
937               else
938                 abort ();
939             }
940           else
941             {
942               if (reloc_size == 32)
943                 reloc = BFD_RELOC_32;
944               else if (reloc_size == 16)
945                 reloc = BFD_RELOC_16;
946               else if (reloc_size == 8)
947                 reloc = BFD_RELOC_8;
948               else
949                 abort ();
950             }
951
952           /* Convert the size of the reloc into what fix_new_exp wants.  */
953           reloc_size = reloc_size / 8;
954           if (reloc_size == 8)
955             reloc_size = 0;
956           else if (reloc_size == 16)
957             reloc_size = 1;
958           else if (reloc_size == 32)
959             reloc_size = 2;
960
961           fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
962                               reloc_size, &fixups[i].exp, pcrel,
963                               ((bfd_reloc_code_real_type) reloc));
964
965           if (pcrel)
966             fixP->fx_offset += offset;
967         }
968     }
969 }
970
971
972 /* if while processing a fixup, a reloc really needs to be created */
973 /* then it is done here */
974                  
975 arelent *
976 tc_gen_reloc (seg, fixp)
977      asection *seg;
978      fixS *fixp;
979 {
980   arelent *reloc;
981   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
982
983   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
984   if (reloc->howto == (reloc_howto_type *) NULL)
985     {
986       as_bad_where (fixp->fx_file, fixp->fx_line,
987                     "reloc %d not supported by object file format",
988                     (int)fixp->fx_r_type);
989       return NULL;
990     }
991   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
992
993   if (fixp->fx_addsy && fixp->fx_subsy)
994     {
995       reloc->sym_ptr_ptr = &bfd_abs_symbol;
996       reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
997                        - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
998     }
999   else 
1000     {
1001       reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1002       reloc->addend = fixp->fx_offset;
1003     }
1004   return reloc;
1005 }
1006
1007 int
1008 md_estimate_size_before_relax (fragp, seg)
1009      fragS *fragp;
1010      asection *seg;
1011 {
1012   return 0;
1013
1014
1015 long
1016 md_pcrel_from (fixp)
1017      fixS *fixp;
1018 {
1019   return fixp->fx_frag->fr_address;
1020 #if 0
1021   if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1022     {
1023       /* The symbol is undefined.  Let the linker figure it out.  */
1024       return 0;
1025     }
1026   return fixp->fx_frag->fr_address + fixp->fx_where;
1027 #endif
1028 }
1029
1030 int
1031 md_apply_fix3 (fixp, valuep, seg)
1032      fixS *fixp;
1033      valueT *valuep;
1034      segT seg;
1035 {
1036   /* We shouldn't ever get here because linkrelax is nonzero.  */
1037   abort ();
1038   fixp->fx_done = 1;
1039   return 0;
1040 }
1041
1042 /* Insert an operand value into an instruction.  */
1043
1044 static void
1045 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
1046      unsigned long *insnp;
1047      unsigned long *extensionp;
1048      const struct mn10300_operand *operand;
1049      offsetT val;
1050      char *file;
1051      unsigned int line;
1052      unsigned int shift;
1053 {
1054   /* No need to check 32bit operands for a bit.  Note that
1055      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
1056   if (operand->bits != 32
1057       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1058     {
1059       long min, max;
1060       offsetT test;
1061
1062       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1063         {
1064           max = (1 << (operand->bits - 1)) - 1;
1065           min = - (1 << (operand->bits - 1));
1066         }
1067       else
1068         {
1069           max = (1 << operand->bits) - 1;
1070           min = 0;
1071         }
1072
1073       test = val;
1074
1075
1076       if (test < (offsetT) min || test > (offsetT) max)
1077         {
1078           const char *err =
1079             "operand out of range (%s not between %ld and %ld)";
1080           char buf[100];
1081
1082           sprint_value (buf, test);
1083           if (file == (char *) NULL)
1084             as_warn (err, buf, min, max);
1085           else
1086             as_warn_where (file, line, err, buf, min, max);
1087         }
1088     }
1089
1090   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1091     {
1092       *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
1093       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
1094                       << operand->shift);
1095     }
1096   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
1097     {
1098       *insnp |= (((long) val & ((1 << operand->bits) - 1))
1099                  << (operand->shift + shift));
1100
1101       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1102         *insnp |= (((long) val & ((1 << operand->bits) - 1))
1103                    << (operand->shift + shift + 2));
1104     }
1105   else
1106     {
1107       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1108                       << (operand->shift + shift));
1109
1110       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1111         *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1112                         << (operand->shift + shift + 2));
1113     }
1114 }
1115
1116 static unsigned long
1117 check_operand (insn, operand, val)
1118      unsigned long insn;
1119      const struct mn10300_operand *operand;
1120      offsetT val;
1121 {
1122   /* No need to check 32bit operands for a bit.  Note that
1123      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
1124   if (operand->bits != 32
1125       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1126     {
1127       long min, max;
1128       offsetT test;
1129
1130       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1131         {
1132           max = (1 << (operand->bits - 1)) - 1;
1133           min = - (1 << (operand->bits - 1));
1134         }
1135       else
1136         {
1137           max = (1 << operand->bits) - 1;
1138           min = 0;
1139         }
1140
1141       test = val;
1142
1143
1144       if (test < (offsetT) min || test > (offsetT) max)
1145         return 0;
1146       else
1147         return 1;
1148     }
1149   return 1;
1150 }