* config/tc-mn10300.c (md_assemble): Handle MN10300_OPERAND_REG_LIST.
[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 register_name PARAMS ((expressionS *expressionP));
66 static boolean system_register_name PARAMS ((expressionS *expressionP));
67 static boolean cc_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
402 void
403 md_assemble (str) 
404      char *str;
405 {
406   char *s;
407   struct mn10300_opcode *opcode;
408   struct mn10300_opcode *next_opcode;
409   const unsigned char *opindex_ptr;
410   int next_opindex;
411   unsigned long insn, extension, size;
412   char *f;
413   int i;
414   int match;
415   bfd_reloc_code_real_type reloc;
416
417   /* Get the opcode.  */
418   for (s = str; *s != '\0' && ! isspace (*s); s++)
419     ;
420   if (*s != '\0')
421     *s++ = '\0';
422
423   /* find the first opcode with the proper name */
424   opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
425   if (opcode == NULL)
426     {
427       as_bad ("Unrecognized opcode: `%s'", str);
428       return;
429     }
430
431   str = s;
432   while (isspace (*str))
433     ++str;
434
435   input_line_pointer = str;
436
437   for(;;)
438     {
439       const char *errmsg = NULL;
440       int op_idx;
441       char *hold;
442       int extra_shift = 0;
443
444       fc = 0;
445       match = 0;
446       next_opindex = 0;
447       insn = opcode->opcode;
448       extension = 0;
449       for (op_idx = 1, opindex_ptr = opcode->operands;
450            *opindex_ptr != 0;
451            opindex_ptr++, op_idx++)
452         {
453           const struct mn10300_operand *operand;
454           expressionS ex;
455
456           if (next_opindex == 0)
457             {
458               operand = &mn10300_operands[*opindex_ptr];
459             }
460           else
461             {
462               operand = &mn10300_operands[next_opindex];
463               next_opindex = 0;
464             }
465
466           errmsg = NULL;
467
468           while (*str == ' ' || *str == ',')
469             ++str;
470
471           /* Gather the operand. */
472           hold = input_line_pointer;
473           input_line_pointer = str;
474
475           if (operand->flags & MN10300_OPERAND_PAREN)
476             {
477               if (*input_line_pointer != ')' && *input_line_pointer != '(')
478                 {
479                   input_line_pointer = hold;
480                   str = hold;
481                   goto error;
482                 }
483               input_line_pointer++;
484               goto keep_going;
485             }
486           /* See if we can match the operands.  */
487           else if (operand->flags & MN10300_OPERAND_DREG)
488             {
489               if (!data_register_name (&ex))
490                 {
491                   input_line_pointer = hold;
492                   str = hold;
493                   goto error;
494                 }
495             }
496           else if (operand->flags & MN10300_OPERAND_AREG)
497             {
498               if (!address_register_name (&ex))
499                 {
500                   input_line_pointer = hold;
501                   str = hold;
502                   goto error;
503                 }
504             }
505           else if (operand->flags & MN10300_OPERAND_SP)
506             {
507               char *start = input_line_pointer;
508               char c = get_symbol_end ();
509
510               if (strcmp (start, "sp") != 0)
511                 {
512                   *input_line_pointer = c;
513                   input_line_pointer = hold;
514                   str = hold;
515                   goto error;
516                 }
517               *input_line_pointer = c;
518               goto keep_going;
519             }
520           else if (operand->flags & MN10300_OPERAND_PSW)
521             {
522               char *start = input_line_pointer;
523               char c = get_symbol_end ();
524
525               if (strcmp (start, "psw") != 0)
526                 {
527                   *input_line_pointer = c;
528                   input_line_pointer = hold;
529                   str = hold;
530                   goto error;
531                 }
532               *input_line_pointer = c;
533               goto keep_going;
534             }
535           else if (operand->flags & MN10300_OPERAND_MDR)
536             {
537               char *start = input_line_pointer;
538               char c = get_symbol_end ();
539
540               if (strcmp (start, "mdr") != 0)
541                 {
542                   *input_line_pointer = c;
543                   input_line_pointer = hold;
544                   str = hold;
545                   goto error;
546                 }
547               *input_line_pointer = c;
548               goto keep_going;
549             }
550           else if (operand->flags & MN10300_OPERAND_REG_LIST)
551             {
552               unsigned int value = 0;
553               if (*input_line_pointer != '[')
554                 {
555                   input_line_pointer = hold;
556                   str = hold;
557                   goto error;
558                 }
559
560               /* Eat the '['.  */
561               input_line_pointer++;
562              
563               /* A null register list can not be specified.  */
564               if (*input_line_pointer == ']')
565                 {
566                   input_line_pointer = hold;
567                   str = hold;
568                   goto error;
569                 }
570
571               while (*input_line_pointer != ']')
572                 {
573                   char *start;
574                   char c;
575
576                   if (*input_line_pointer == ',')
577                     input_line_pointer++;
578
579                   start = input_line_pointer;
580                   c = get_symbol_end ();
581
582                   if (strcmp (start, "d2") == 0)
583                     {
584                       value |= 0x80;
585                       *input_line_pointer = c;
586                     }
587                   else if (strcmp (start, "d3") == 0)
588                     {
589                       value |= 0x40;
590                       *input_line_pointer = c;
591                     }
592                   else if (strcmp (start, "a2") == 0)
593                     {
594                       value |= 0x20;
595                       *input_line_pointer = c;
596                     }
597                   else if (strcmp (start, "a3") == 0)
598                     {
599                       value |= 0x10;
600                       *input_line_pointer = c;
601                     }
602                   else if (strcmp (start, "other") == 0)
603                     {
604                       value |= 0x08;
605                       *input_line_pointer = c;
606                     }
607                   else
608                     {
609                       input_line_pointer = hold;
610                       str = hold;
611                       goto error;
612                     }
613                 }
614               input_line_pointer++;
615               mn10300_insert_operand (&insn, &extension, operand,
616                                       value, (char *) NULL, 0, 0);
617               goto keep_going;
618
619             }
620           else if (data_register_name (&ex))
621             {
622               input_line_pointer = hold;
623               str = hold;
624               goto error;
625             }
626           else if (address_register_name (&ex))
627             {
628               input_line_pointer = hold;
629               str = hold;
630               goto error;
631             }
632           else if (other_register_name (&ex))
633             {
634               input_line_pointer = hold;
635               str = hold;
636               goto error;
637             }
638           else if (*str == ')' || *str == '(')
639             {
640               input_line_pointer = hold;
641               str = hold;
642               goto error;
643             }
644           else
645             {
646               expression (&ex);
647             }
648
649           switch (ex.X_op) 
650             {
651             case O_illegal:
652               errmsg = "illegal operand";
653               goto error;
654             case O_absent:
655               errmsg = "missing operand";
656               goto error;
657             case O_register:
658               if (operand->flags & (MN10300_OPERAND_DREG 
659                                     | MN10300_OPERAND_AREG) == 0)
660                 {
661                   input_line_pointer = hold;
662                   str = hold;
663                   goto error;
664                 }
665                 
666               if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
667                 extra_shift = 8;
668               else if (opcode->format == FMT_D2 || opcode->format == FMT_D4
669                        || opcode->format == FMT_S2 || opcode->format == FMT_S4
670                        || opcode->format == FMT_S6 || opcode->format == FMT_D5)
671                 extra_shift = 16;
672               else
673                 extra_shift = 0;
674               
675               mn10300_insert_operand (&insn, &extension, operand,
676                                       ex.X_add_number, (char *) NULL,
677                                       0, extra_shift);
678
679               break;
680
681             case O_constant:
682               /* If this operand can be promoted, and it doesn't
683                  fit into the allocated bitfield for this insn,
684                  then promote it (ie this opcode does not match).  */
685               if (operand->flags & MN10300_OPERAND_PROMOTE
686                   && ! check_operand (insn, operand, ex.X_add_number))
687                 {
688                   input_line_pointer = hold;
689                   str = hold;
690                   goto error;
691                 }
692
693               mn10300_insert_operand (&insn, &extension, operand,
694                                       ex.X_add_number, (char *) NULL,
695                                       0, 0);
696               break;
697
698             default:
699               /* If this operand can be promoted, then this opcode didn't
700                  match since we can't know if it needed promotion!  */
701               if (operand->flags & MN10300_OPERAND_PROMOTE)
702                 {
703                   input_line_pointer = hold;
704                   str = hold;
705                   goto error;
706                 }
707
708               /* We need to generate a fixup for this expression.  */
709               if (fc >= MAX_INSN_FIXUPS)
710                 as_fatal ("too many fixups");
711               fixups[fc].exp = ex;
712               fixups[fc].opindex = *opindex_ptr;
713               fixups[fc].reloc = BFD_RELOC_UNUSED;
714               ++fc;
715               break;
716             }
717
718 keep_going:
719           str = input_line_pointer;
720           input_line_pointer = hold;
721
722           while (*str == ' ' || *str == ',')
723             ++str;
724
725         }
726
727       /* Make sure we used all the operands!  */
728       if (*str != ',')
729         match = 1;
730
731     error:
732       if (match == 0)
733         {
734           next_opcode = opcode + 1;
735           if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
736             {
737               opcode = next_opcode;
738               continue;
739             }
740           
741           as_bad ("%s", errmsg);
742           return;
743         }
744       break;
745     }
746       
747   while (isspace (*str))
748     ++str;
749
750   if (*str != '\0')
751     as_bad ("junk at end of line: `%s'", str);
752
753   input_line_pointer = str;
754
755   /* Determine the size of the instruction.  */
756   if (opcode->format == FMT_S0)
757     size = 1;
758
759   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
760     size = 2;
761
762   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
763     size = 3;
764
765   if (opcode->format == FMT_S4)
766     size = 5;
767
768   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
769     size = 7;
770
771   if (opcode->format == FMT_D2)
772     size = 4;
773
774   if (opcode->format == FMT_D4)
775     size = 6;
776
777   /* Write out the instruction.  */
778
779   f = frag_more (size);
780   number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
781   if (size > 4)
782     number_to_chars_bigendian (f + 4, extension, size - 4);
783 }
784
785
786 /* if while processing a fixup, a reloc really needs to be created */
787 /* then it is done here */
788                  
789 arelent *
790 tc_gen_reloc (seg, fixp)
791      asection *seg;
792      fixS *fixp;
793 {
794   arelent *reloc;
795   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
796   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
797   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
798   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
799   if (reloc->howto == (reloc_howto_type *) NULL)
800     {
801       as_bad_where (fixp->fx_file, fixp->fx_line,
802                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
803       return NULL;
804     }
805   reloc->addend = fixp->fx_addnumber;
806   /*  printf("tc_gen_reloc: addr=%x  addend=%x\n", reloc->address, reloc->addend); */
807   return reloc;
808 }
809
810 int
811 md_estimate_size_before_relax (fragp, seg)
812      fragS *fragp;
813      asection *seg;
814 {
815   return 0;
816
817
818 long
819 md_pcrel_from (fixp)
820      fixS *fixp;
821 {
822   if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
823     {
824       /* The symbol is undefined.  Let the linker figure it out.  */
825       return 0;
826     }
827   return fixp->fx_frag->fr_address + fixp->fx_where;
828 }
829
830 int
831 md_apply_fix3 (fixp, valuep, seg)
832      fixS *fixp;
833      valueT *valuep;
834      segT seg;
835 {
836   valueT value;
837   char *where;
838
839   fixp->fx_done = 1;
840   return 0;
841
842   if (fixp->fx_addsy == (symbolS *) NULL)
843     {
844       value = *valuep;
845       fixp->fx_done = 1;
846     }
847   else if (fixp->fx_pcrel)
848     value = *valuep;
849   else
850     {
851       value = fixp->fx_offset;
852       if (fixp->fx_subsy != (symbolS *) NULL)
853         {
854           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
855             value -= S_GET_VALUE (fixp->fx_subsy);
856           else
857             {
858               /* We don't actually support subtracting a symbol.  */
859               as_bad_where (fixp->fx_file, fixp->fx_line,
860                             "expression too complex");
861             }
862         }
863     }
864
865   /* printf("md_apply_fix: value=0x%x  type=%d\n",  value, fixp->fx_r_type); */
866
867   if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
868     {
869       int opindex;
870       const struct mn10300_operand *operand;
871       char *where;
872       unsigned long insn, extension;
873
874       opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
875       operand = &mn10300_operands[opindex];
876
877       /* Fetch the instruction, insert the fully resolved operand
878          value, and stuff the instruction back again.
879
880          Note the instruction has been stored in little endian
881          format!  */
882       where = fixp->fx_frag->fr_literal + fixp->fx_where;
883
884       insn = bfd_getl32((unsigned char *) where);
885       extension = 0;
886       mn10300_insert_operand (&insn, &extension, operand,
887                               (offsetT) value, fixp->fx_file,
888                               fixp->fx_line, 0);
889       bfd_putl32((bfd_vma) insn, (unsigned char *) where);
890
891       if (fixp->fx_done)
892         {
893           /* Nothing else to do here. */
894           return 1;
895         }
896
897       /* Determine a BFD reloc value based on the operand information.  
898          We are only prepared to turn a few of the operands into relocs. */
899
900         {
901           as_bad_where(fixp->fx_file, fixp->fx_line,
902                        "unresolved expression that must be resolved");
903           fixp->fx_done = 1;
904           return 1;
905         }
906     }
907   else if (fixp->fx_done)
908     {
909       /* We still have to insert the value into memory!  */
910       where = fixp->fx_frag->fr_literal + fixp->fx_where;
911       if (fixp->fx_size == 1)
912         *where = value & 0xff;
913       if (fixp->fx_size == 2)
914         bfd_putl16(value & 0xffff, (unsigned char *) where);
915       if (fixp->fx_size == 4)
916         bfd_putl32(value, (unsigned char *) where);
917     }
918
919   fixp->fx_addnumber = value;
920   return 1;
921 }
922
923 /* Insert an operand value into an instruction.  */
924
925 static void
926 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
927      unsigned long *insnp;
928      unsigned long *extensionp;
929      const struct mn10300_operand *operand;
930      offsetT val;
931      char *file;
932      unsigned int line;
933      unsigned int shift;
934 {
935   /* No need to check 32bit operands for a bit.  Note that
936      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
937   if (operand->bits != 32
938       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
939     {
940       long min, max;
941       offsetT test;
942
943       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
944         {
945           max = (1 << (operand->bits - 1)) - 1;
946           min = - (1 << (operand->bits - 1));
947         }
948       else
949         {
950           max = (1 << operand->bits) - 1;
951           min = 0;
952         }
953
954       test = val;
955
956
957       if (test < (offsetT) min || test > (offsetT) max)
958         {
959           const char *err =
960             "operand out of range (%s not between %ld and %ld)";
961           char buf[100];
962
963           sprint_value (buf, test);
964           if (file == (char *) NULL)
965             as_warn (err, buf, min, max);
966           else
967             as_warn_where (file, line, err, buf, min, max);
968         }
969     }
970
971   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
972     {
973       *insnp |= (val >> 32 - operand->bits) & ((1 << operand->bits) - 1);
974       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
975                       << operand->shift);
976     }
977   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
978     {
979       *insnp |= (((long) val & ((1 << operand->bits) - 1))
980                  << (operand->shift + shift));
981
982       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
983         *insnp |= (((long) val & ((1 << operand->bits) - 1))
984                    << (operand->shift + shift + 2));
985     }
986   else
987     {
988       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
989                       << (operand->shift + shift));
990
991       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
992         *extensionp |= (((long) val & ((1 << operand->bits) - 1))
993                         << (operand->shift + shift + 2));
994     }
995 }
996
997 static unsigned long
998 check_operand (insn, operand, val)
999      unsigned long insn;
1000      const struct mn10300_operand *operand;
1001      offsetT val;
1002 {
1003   /* No need to check 32bit operands for a bit.  Note that
1004      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
1005   if (operand->bits != 32
1006       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1007     {
1008       long min, max;
1009       offsetT test;
1010
1011       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1012         {
1013           max = (1 << (operand->bits - 1)) - 1;
1014           min = - (1 << (operand->bits - 1));
1015         }
1016       else
1017         {
1018           max = (1 << operand->bits) - 1;
1019           min = 0;
1020         }
1021
1022       test = val;
1023
1024
1025       if (test < (offsetT) min || test > (offsetT) max)
1026         return 0;
1027       else
1028         return 1;
1029     }
1030   return 1;
1031 }