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