* config/tc-h8300.c (h8300hmode): Record the machine type
[external/binutils.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000
3    Free Software Foundation, Inc.
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 the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>.  */
23
24 #include <stdio.h>
25 #include "as.h"
26 #include "subsegs.h"
27 #include "bfd.h"
28 #define DEFINE_TABLE
29 #define h8_opcodes ops
30 #include "opcode/h8300.h"
31 #include <ctype.h>
32
33 const char comment_chars[] = ";";
34 const char line_comment_chars[] = "#";
35 const char line_separator_chars[] = "";
36
37 /* This table describes all the machine specific pseudo-ops the assembler
38    has to support.  The fields are:
39    pseudo-op name without dot
40    function to call to execute this pseudo-op
41    Integer arg to pass to the function
42    */
43
44 void cons ();
45
46 int Hmode;
47 int Smode;
48 #define PSIZE (Hmode ? L_32 : L_16)
49 #define DMODE (L_16)
50 #define DSYMMODE (Hmode ? L_24 : L_16)
51 int bsize = L_8;                /* default branch displacement */
52
53 void
54 h8300hmode ()
55 {
56   Hmode = 1;
57   Smode = 0;
58 #ifdef BFD_ASSEMBLER
59   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
60     as_warn (_("could not set architecture and machine"));
61 #endif
62 }
63
64 void
65 h8300smode ()
66 {
67   Smode = 1;
68   Hmode = 1;
69 #ifdef BFD_ASSEMBLER
70   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
71     as_warn (_("could not set architecture and machine"));
72 #endif
73 }
74
75 void
76 sbranch (size)
77      int size;
78 {
79   bsize = size;
80 }
81
82 static void
83 pint ()
84 {
85   cons (Hmode ? 4 : 2);
86 }
87
88 const pseudo_typeS md_pseudo_table[] =
89 {
90   {"h8300h", h8300hmode, 0},
91   {"h8300s", h8300smode, 0},
92   {"sbranch", sbranch, L_8},
93   {"lbranch", sbranch, L_16},
94
95   {"int", pint, 0},
96   {"data.b", cons, 1},
97   {"data.w", cons, 2},
98   {"data.l", cons, 4},
99   {"form", listing_psize, 0},
100   {"heading", listing_title, 0},
101   {"import", s_ignore, 0},
102   {"page", listing_eject, 0},
103   {"program", s_ignore, 0},
104   {0, 0, 0}
105 };
106
107 const int md_reloc_size;
108
109 const char EXP_CHARS[] = "eE";
110
111 /* Chars that mean this number is a floating point constant */
112 /* As in 0f12.456 */
113 /* or    0d1.2345e12 */
114 const char FLT_CHARS[] = "rRsSfFdDxXpP";
115
116 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
117
118 /* This function is called once, at assembler startup time.  This
119    should set up all the tables, etc. that the MD part of the assembler
120    needs.  */
121 void
122 md_begin ()
123 {
124   struct h8_opcode *opcode;
125   char prev_buffer[100];
126   int idx = 0;
127
128 #ifdef BFD_ASSEMBLER
129   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
130     as_warn (_("could not set architecture and machine"));
131 #endif
132
133   opcode_hash_control = hash_new ();
134   prev_buffer[0] = 0;
135
136   for (opcode = h8_opcodes; opcode->name; opcode++)
137     {
138       /* Strip off any . part when inserting the opcode and only enter
139          unique codes into the hash table.  */
140       char *src = opcode->name;
141       unsigned int len = strlen (src);
142       char *dst = malloc (len + 1);
143       char *buffer = dst;
144
145       opcode->size = 0;
146       while (*src)
147         {
148           if (*src == '.')
149             {
150               src++;
151               opcode->size = *src;
152               break;
153             }
154           *dst++ = *src++;
155         }
156       *dst++ = 0;
157       if (strcmp (buffer, prev_buffer))
158         {
159           hash_insert (opcode_hash_control, buffer, (char *) opcode);
160           strcpy (prev_buffer, buffer);
161           idx++;
162         }
163       opcode->idx = idx;
164
165       /* Find the number of operands.  */
166       opcode->noperands = 0;
167       while (opcode->args.nib[opcode->noperands] != E)
168         opcode->noperands++;
169
170       /* Find the length of the opcode in bytes.  */
171       opcode->length = 0;
172       while (opcode->data.nib[opcode->length * 2] != E)
173         opcode->length++;
174     }
175
176   linkrelax = 1;
177 }
178
179 struct h8_exp
180 {
181   char *e_beg;
182   char *e_end;
183   expressionS e_exp;
184 };
185
186 int dispreg;
187 int opsize;                     /* Set when a register size is seen */
188
189 struct h8_op
190 {
191   op_type mode;
192   unsigned reg;
193   expressionS exp;
194 };
195
196 /*
197   parse operands
198   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
199   r0l,r0h,..r7l,r7h
200   @WREG
201   @WREG+
202   @-WREG
203   #const
204   ccr
205 */
206
207 /* Try to parse a reg name.  Return the number of chars consumed.  */
208
209 static int
210 parse_reg (src, mode, reg, direction)
211      char *src;
212      op_type *mode;
213      unsigned int *reg;
214      int direction;
215
216 {
217   char *end;
218   int len;
219
220   /* Cribbed from get_symbol_end.  */
221   if (!is_name_beginner (*src) || *src == '\001')
222     return 0;
223   end = src + 1;
224   while (is_part_of_name (*end) || *end == '\001')
225     end++;
226   len = end - src;
227
228   if (len == 2 && src[0] == 's' && src[1] == 'p')
229     {
230       *mode = PSIZE | REG | direction;
231       *reg = 7;
232       return len;
233     }
234   if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
235     {
236       *mode = CCR;
237       *reg = 0;
238       return len;
239     }
240   if (len == 3 && src[0] == 'e' && src[1] == 'x' && src[2] == 'r')
241     {
242       *mode = EXR;
243       *reg = 0;
244       return len;
245     }
246   if (len == 2 && src[0] == 'f' && src[1] == 'p')
247     {
248       *mode = PSIZE | REG | direction;
249       *reg = 6;
250       return len;
251     }
252   if (len == 3 && src[0] == 'e' && src[1] == 'r'
253       && src[2] >= '0' && src[2] <= '7')
254     {
255       *mode = L_32 | REG | direction;
256       *reg = src[2] - '0';
257       if (!Hmode)
258         as_warn (_("Reg not valid for H8/300"));
259       return len;
260     }
261   if (len == 2 && src[0] == 'e' && src[1] >= '0' && src[1] <= '7')
262     {
263       *mode = L_16 | REG | direction;
264       *reg = src[1] - '0' + 8;
265       if (!Hmode)
266         as_warn (_("Reg not valid for H8/300"));
267       return len;
268     }
269
270   if (src[0] == 'r')
271     {
272       if (src[1] >= '0' && src[1] <= '7')
273         {
274           if (len == 3 && src[2] == 'l')
275             {
276               *mode = L_8 | REG | direction;
277               *reg = (src[1] - '0') + 8;
278               return len;
279             }
280           if (len == 3 && src[2] == 'h')
281             {
282               *mode = L_8 | REG | direction;
283               *reg = (src[1] - '0');
284               return len;
285             }
286           if (len == 2)
287             {
288               *mode = L_16 | REG | direction;
289               *reg = (src[1] - '0');
290               return len;
291             }
292         }
293     }
294
295   return 0;
296 }
297
298 static char *
299 parse_exp (s, op)
300      char *s;
301      expressionS *op;
302 {
303   char *save = input_line_pointer;
304   char *new;
305
306   input_line_pointer = s;
307   expression (op);
308   if (op->X_op == O_absent)
309     as_bad (_("missing operand"));
310   new = input_line_pointer;
311   input_line_pointer = save;
312   return new;
313 }
314
315 static char *
316 skip_colonthing (ptr, exp, mode)
317      char *ptr;
318      expressionS *exp ATTRIBUTE_UNUSED;
319      int *mode;
320 {
321   if (*ptr == ':')
322     {
323       ptr++;
324       *mode &= ~SIZE;
325       if (*ptr == '8')
326         {
327           ptr++;
328           /* ff fill any 8 bit quantity */
329           /* exp->X_add_number -= 0x100; */
330           *mode |= L_8;
331         }
332       else
333         {
334           if (*ptr == '2')
335             {
336               *mode |= L_24;
337             }
338           else if (*ptr == '3')
339             {
340               *mode |= L_32;
341             }
342           else if (*ptr == '1')
343             {
344               *mode |= L_16;
345             }
346           while (isdigit (*ptr))
347             ptr++;
348         }
349     }
350   return ptr;
351 }
352
353 /* The many forms of operand:
354
355    Rn                   Register direct
356    @Rn                  Register indirect
357    @(exp[:16], Rn)      Register indirect with displacement
358    @Rn+
359    @-Rn
360    @aa:8                absolute 8 bit
361    @aa:16               absolute 16 bit
362    @aa                  absolute 16 bit
363
364    #xx[:size]           immediate data
365    @(exp:[8], pc)       pc rel
366    @@aa[:8]             memory indirect
367
368    */
369
370 char *
371 colonmod24 (op, src)
372      struct h8_op *op;
373      char *src;
374
375 {
376   int mode = 0;
377   src = skip_colonthing (src, &op->exp, &mode);
378
379   if (!mode)
380     {
381       /* Choose a default mode.  */
382       if (op->exp.X_add_number < -32768
383           || op->exp.X_add_number > 32767)
384         {
385           if (Hmode)
386             mode = L_24;
387           else
388             mode = L_16;
389         }
390       else if (op->exp.X_add_symbol
391                || op->exp.X_op_symbol)
392         mode = DSYMMODE;
393       else
394         mode = DMODE;
395     }
396   op->mode |= mode;
397   return src;
398
399 }
400
401 static void
402 get_operand (ptr, op, dst, direction)
403      char **ptr;
404      struct h8_op *op;
405      unsigned int dst ATTRIBUTE_UNUSED;
406      int direction;
407 {
408   char *src = *ptr;
409   op_type mode;
410   unsigned int num;
411   unsigned int len;
412
413   op->mode = E;
414
415   /* Gross.  Gross.  ldm and stm have a format not easily handled
416      by get_operand.  We deal with it explicitly here.  */
417   if (src[0] == 'e' && src[1] == 'r' && isdigit (src[2])
418       && src[3] == '-' && src[4] == 'e' && src[5] == 'r' && isdigit (src[6]))
419     {
420       int low, high;
421
422       low = src[2] - '0';
423       high = src[6] - '0';
424
425       if (high < low)
426         as_bad (_("Invalid register list for ldm/stm\n"));
427
428       if (low % 2)
429         as_bad (_("Invalid register list for ldm/stm\n"));
430
431       if (high - low > 3)
432         as_bad (_("Invalid register list for ldm/stm\n"));
433
434       if (high - low != 1
435           && low % 4)
436         as_bad (_("Invalid register list for ldm/stm\n"));
437
438       /* Even sicker.  We encode two registers into op->reg.  One
439          for the low register to save, the other for the high
440          register to save;  we also set the high bit in op->reg
441          so we know this is "very special".  */
442       op->reg = 0x80000000 | (high << 8) | low;
443       op->mode = REG;
444       *ptr = src + 7;
445       return;
446     }
447
448   len = parse_reg (src, &op->mode, &op->reg, direction);
449   if (len)
450     {
451       *ptr = src + len;
452       return;
453     }
454
455   if (*src == '@')
456     {
457       src++;
458       if (*src == '@')
459         {
460           src++;
461           src = parse_exp (src, &op->exp);
462
463           src = skip_colonthing (src, &op->exp, &op->mode);
464
465           *ptr = src;
466
467           op->mode = MEMIND;
468           return;
469         }
470
471       if (*src == '-')
472         {
473           src++;
474           len = parse_reg (src, &mode, &num, direction);
475           if (len == 0)
476             {
477               /* Oops, not a reg after all, must be ordinary exp.  */
478               src--;
479               /* Must be a symbol.  */
480               op->mode = ABS | PSIZE | direction;
481               *ptr = skip_colonthing (parse_exp (src, &op->exp),
482                                       &op->exp, &op->mode);
483
484               return;
485             }
486
487           if ((mode & SIZE) != PSIZE)
488             as_bad (_("Wrong size pointer register for architecture."));
489           op->mode = RDDEC;
490           op->reg = num;
491           *ptr = src + len;
492           return;
493         }
494       if (*src == '(')
495         {
496           /* Disp.  */
497           src++;
498
499           /* Start off assuming a 16 bit offset.  */
500
501           src = parse_exp (src, &op->exp);
502
503           src = colonmod24 (op, src);
504
505           if (*src == ')')
506             {
507               src++;
508               op->mode |= ABS | direction;
509               *ptr = src;
510               return;
511             }
512
513           if (*src != ',')
514             {
515               as_bad (_("expected @(exp, reg16)"));
516               return;
517
518             }
519           src++;
520
521           len = parse_reg (src, &mode, &op->reg, direction);
522           if (len == 0 || !(mode & REG))
523             {
524               as_bad (_("expected @(exp, reg16)"));
525               return;
526             }
527           op->mode |= DISP | direction;
528           dispreg = op->reg;
529           src += len;
530           src = skip_colonthing (src, &op->exp, &op->mode);
531
532           if (*src != ')' && '(')
533             {
534               as_bad (_("expected @(exp, reg16)"));
535               return;
536             }
537           *ptr = src + 1;
538
539           return;
540         }
541       len = parse_reg (src, &mode, &num, direction);
542
543       if (len)
544         {
545           src += len;
546           if (*src == '+')
547             {
548               src++;
549               if ((mode & SIZE) != PSIZE)
550                 as_bad (_("Wrong size pointer register for architecture."));
551               op->mode = RSINC;
552               op->reg = num;
553               *ptr = src;
554               return;
555             }
556           if ((mode & SIZE) != PSIZE)
557             as_bad (_("Wrong size pointer register for architecture."));
558
559           op->mode = direction | IND | PSIZE;
560           op->reg = num;
561           *ptr = src;
562
563           return;
564         }
565       else
566         {
567           /* must be a symbol */
568
569           op->mode = ABS | direction;
570           src = parse_exp (src, &op->exp);
571
572           *ptr = colonmod24 (op, src);
573
574           return;
575         }
576     }
577
578   if (*src == '#')
579     {
580       src++;
581       op->mode = IMM;
582       src = parse_exp (src, &op->exp);
583       *ptr = skip_colonthing (src, &op->exp, &op->mode);
584
585       return;
586     }
587   else if (strncmp (src, "mach", 4) == 0
588            || strncmp (src, "macl", 4) == 0)
589     {
590       op->reg = src[3] == 'l';
591       op->mode = MACREG;
592       *ptr = src + 4;
593       return;
594     }
595   else
596     {
597       src = parse_exp (src, &op->exp);
598       /* Trailing ':' size ? */
599       if (*src == ':')
600         {
601           if (src[1] == '1' && src[2] == '6')
602             {
603               op->mode = PCREL | L_16;
604               src += 3;
605             }
606           else if (src[1] == '8')
607             {
608               op->mode = PCREL | L_8;
609               src += 2;
610             }
611           else
612             {
613               as_bad (_("expect :8 or :16 here"));
614             }
615         }
616       else
617         {
618           op->mode = PCREL | bsize;
619         }
620       *ptr = src;
621     }
622 }
623
624 static char *
625 get_operands (noperands, op_end, operand)
626      unsigned int noperands;
627      char *op_end;
628      struct h8_op *operand;
629 {
630   char *ptr = op_end;
631
632   switch (noperands)
633     {
634     case 0:
635       operand[0].mode = 0;
636       operand[1].mode = 0;
637       break;
638
639     case 1:
640       ptr++;
641       get_operand (&ptr, operand + 0, 0, SRC);
642       if (*ptr == ',')
643         {
644           ptr++;
645           get_operand (&ptr, operand + 1, 1, DST);
646         }
647       else
648         {
649           operand[1].mode = 0;
650         }
651       break;
652
653     case 2:
654       ptr++;
655       get_operand (&ptr, operand + 0, 0, SRC);
656       if (*ptr == ',')
657         ptr++;
658       get_operand (&ptr, operand + 1, 1, DST);
659       break;
660
661     default:
662       abort ();
663     }
664
665   return ptr;
666 }
667
668 /* Passed a pointer to a list of opcodes which use different
669    addressing modes, return the opcode which matches the opcodes
670    provided.  */
671 static struct h8_opcode *
672 get_specific (opcode, operands, size)
673      struct h8_opcode *opcode;
674      struct h8_op *operands;
675      int size;
676 {
677   struct h8_opcode *this_try = opcode;
678   int found = 0;
679
680   unsigned int this_index = opcode->idx;
681
682   /* There's only one ldm/stm and it's easier to just
683      get out quick for them.  */
684   if (strcmp (opcode->name, "stm.l") == 0
685       || strcmp (opcode->name, "ldm.l") == 0)
686     return this_try;
687
688   while (this_index == opcode->idx && !found)
689     {
690       found = 1;
691
692       this_try = opcode++;
693       if (this_try->noperands == 0)
694         {
695           int this_size;
696
697           this_size = this_try->how & SN;
698           if (this_size != size && (this_size != SB || size != SN))
699             found = 0;
700         }
701       else
702         {
703           unsigned int i;
704
705           for (i = 0; i < this_try->noperands && found; i++)
706             {
707               op_type op = this_try->args.nib[i];
708               int x = operands[i].mode;
709
710               if ((op & (DISP | REG)) == (DISP | REG)
711                   && ((x & (DISP | REG)) == (DISP | REG)))
712                 {
713                   dispreg = operands[i].reg;
714                 }
715               else if (op & REG)
716                 {
717                   if (!(x & REG))
718                     found = 0;
719
720                   if (x & L_P)
721                     x = (x & ~L_P) | (Hmode ? L_32 : L_16);
722                   if (op & L_P)
723                     op = (op & ~L_P) | (Hmode ? L_32 : L_16);
724
725                   opsize = op & SIZE;
726
727                   /* The size of the reg is v important.  */
728                   if ((op & SIZE) != (x & SIZE))
729                     found = 0;
730                 }
731               else if ((op & ABSJMP) && (x & ABS))
732                 {
733                   operands[i].mode &= ~ABS;
734                   operands[i].mode |= ABSJMP;
735                   /* But it may not be 24 bits long.  */
736                   if (!Hmode)
737                     {
738                       operands[i].mode &= ~SIZE;
739                       operands[i].mode |= L_16;
740                     }
741                 }
742               else if ((op & (KBIT | DBIT)) && (x & IMM))
743                 {
744                   /* This is ok if the immediate value is sensible.  */
745                 }
746               else if (op & PCREL)
747                 {
748                   /* The size of the displacement is important.  */
749                   if ((op & SIZE) != (x & SIZE))
750                     found = 0;
751                 }
752               else if ((op & (DISP | IMM | ABS))
753                        && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
754                 {
755                   /* Promote a L_24 to L_32 if it makes us match.  */
756                   if ((x & L_24) && (op & L_32))
757                     {
758                       x &= ~L_24;
759                       x |= L_32;
760                     }
761                   /* Promote an L8 to L_16 if it makes us match.  */
762                   if (op & ABS && op & L_8 && op & DISP)
763                     {
764                       if (x & L_16)
765                         found = 1;
766                     }
767                   else if ((x & SIZE) != 0
768                            && ((op & SIZE) != (x & SIZE)))
769                     found = 0;
770                 }
771               else if ((op & MACREG) != (x & MACREG))
772                 {
773                   found = 0;
774                 }
775               else if ((op & MODE) != (x & MODE))
776                 {
777                   found = 0;
778                 }
779             }
780         }
781     }
782   if (found)
783     return this_try;
784   else
785     return 0;
786 }
787
788 static void
789 check_operand (operand, width, string)
790      struct h8_op *operand;
791      unsigned int width;
792      char *string;
793 {
794   if (operand->exp.X_add_symbol == 0
795       && operand->exp.X_op_symbol == 0)
796     {
797       /* No symbol involved, let's look at offset, it's dangerous if
798          any of the high bits are not 0 or ff's, find out by oring or
799          anding with the width and seeing if the answer is 0 or all
800          fs.  */
801
802       if ((operand->exp.X_add_number & ~width) != 0 &&
803           (operand->exp.X_add_number | width) != (~0))
804         {
805           if (width == 255
806               && (operand->exp.X_add_number & 0xff00) == 0xff00)
807             {
808               /* Just ignore this one - which happens when trying to
809                  fit a 16 bit address truncated into an 8 bit address
810                  of something like bset.  */
811             }
812           else
813             {
814               as_warn (_("operand %s0x%lx out of range."), string,
815                        (unsigned long) operand->exp.X_add_number);
816             }
817         }
818     }
819 }
820
821 /* RELAXMODE has one of 3 values:
822
823    0 Output a "normal" reloc, no relaxing possible for this insn/reloc
824
825    1 Output a relaxable 24bit absolute mov.w address relocation
826      (may relax into a 16bit absolute address).
827
828    2 Output a relaxable 16/24 absolute mov.b address relocation
829      (may relax into an 8bit absolute address).  */
830
831 static void
832 do_a_fix_imm (offset, operand, relaxmode)
833      int offset;
834      struct h8_op *operand;
835      int relaxmode;
836 {
837   int idx;
838   int size;
839   int where;
840
841   char *t = operand->mode & IMM ? "#" : "@";
842
843   if (operand->exp.X_add_symbol == 0)
844     {
845       char *bytes = frag_now->fr_literal + offset;
846       switch (operand->mode & SIZE)
847         {
848         case L_2:
849           check_operand (operand, 0x3, t);
850           bytes[0] |= (operand->exp.X_add_number) << 4;
851           break;
852         case L_3:
853           check_operand (operand, 0x7, t);
854           bytes[0] |= (operand->exp.X_add_number) << 4;
855           break;
856         case L_8:
857           check_operand (operand, 0xff, t);
858           bytes[0] = operand->exp.X_add_number;
859           break;
860         case L_16:
861           check_operand (operand, 0xffff, t);
862           bytes[0] = operand->exp.X_add_number >> 8;
863           bytes[1] = operand->exp.X_add_number >> 0;
864           break;
865         case L_24:
866           check_operand (operand, 0xffffff, t);
867           bytes[0] = operand->exp.X_add_number >> 16;
868           bytes[1] = operand->exp.X_add_number >> 8;
869           bytes[2] = operand->exp.X_add_number >> 0;
870           break;
871
872         case L_32:
873           /* This should be done with bfd.  */
874           bytes[0] = operand->exp.X_add_number >> 24;
875           bytes[1] = operand->exp.X_add_number >> 16;
876           bytes[2] = operand->exp.X_add_number >> 8;
877           bytes[3] = operand->exp.X_add_number >> 0;
878           if (relaxmode != 0)
879             {
880               idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
881               fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
882             }
883           break;
884         }
885     }
886   else
887     {
888       switch (operand->mode & SIZE)
889         {
890         case L_24:
891         case L_32:
892           size = 4;
893           where = (operand->mode & SIZE) == L_24 ? -1 : 0;
894           if (relaxmode == 2)
895             idx = R_MOV24B1;
896           else if (relaxmode == 1)
897             idx = R_MOVL1;
898           else
899             idx = R_RELLONG;
900           break;
901         default:
902           as_bad (_("Can't work out size of operand.\n"));
903         case L_16:
904           size = 2;
905           where = 0;
906           if (relaxmode == 2)
907             idx = R_MOV16B1;
908           else
909             idx = R_RELWORD;
910           operand->exp.X_add_number =
911             ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
912           break;
913         case L_8:
914           size = 1;
915           where = 0;
916           idx = R_RELBYTE;
917           operand->exp.X_add_number =
918             ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
919         }
920
921       fix_new_exp (frag_now,
922                    offset + where,
923                    size,
924                    &operand->exp,
925                    0,
926                    idx);
927     }
928 }
929
930 /* Now we know what sort of opcodes it is, let's build the bytes.  */
931 static void
932 build_bytes (this_try, operand)
933      struct h8_opcode *this_try;
934      struct h8_op *operand;
935 {
936   unsigned int i;
937
938   char *output = frag_more (this_try->length);
939   op_type *nibble_ptr = this_try->data.nib;
940   op_type c;
941   unsigned int nibble_count = 0;
942   int absat;
943   int immat;
944   int nib;
945   int movb = 0;
946   char asnibbles[30];
947   char *p = asnibbles;
948
949   if (!(this_try->inbase || Hmode))
950     as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
951              this_try->name);
952
953   while (*nibble_ptr != E)
954     {
955       int d;
956       c = *nibble_ptr++;
957
958       d = (c & (DST | SRC_IN_DST)) != 0;
959
960       if (c < 16)
961         {
962           nib = c;
963         }
964       else
965         {
966           if (c & (REG | IND | INC | DEC))
967             {
968               nib = operand[d].reg;
969             }
970           else if ((c & DISPREG) == (DISPREG))
971             {
972               nib = dispreg;
973             }
974           else if (c & ABS)
975             {
976               operand[d].mode = c;
977               absat = nibble_count / 2;
978               nib = 0;
979             }
980           else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
981             {
982               operand[d].mode = c;
983               immat = nibble_count / 2;
984               nib = 0;
985             }
986           else if (c & IGNORE)
987             {
988               nib = 0;
989             }
990           else if (c & DBIT)
991             {
992               switch (operand[0].exp.X_add_number)
993                 {
994                 case 1:
995                   nib = c;
996                   break;
997                 case 2:
998                   nib = 0x8 | c;
999                   break;
1000                 default:
1001                   as_bad (_("Need #1 or #2 here"));
1002                 }
1003             }
1004           else if (c & KBIT)
1005             {
1006               switch (operand[0].exp.X_add_number)
1007                 {
1008                 case 1:
1009                   nib = 0;
1010                   break;
1011                 case 2:
1012                   nib = 8;
1013                   break;
1014                 case 4:
1015                   if (!Hmode)
1016                     as_warn (_("#4 not valid on H8/300."));
1017                   nib = 9;
1018                   break;
1019
1020                 default:
1021                   as_bad (_("Need #1 or #2 here"));
1022                   break;
1023                 }
1024               /* Stop it making a fix.  */
1025               operand[0].mode = 0;
1026             }
1027
1028           if (c & MEMRELAX)
1029             {
1030               operand[d].mode |= MEMRELAX;
1031             }
1032
1033           if (c & B31)
1034             {
1035               nib |= 0x8;
1036             }
1037
1038           if (c & MACREG)
1039             {
1040               if (operand[0].mode == MACREG)
1041                 /* stmac has mac[hl] as the first operand.  */
1042                 nib = 2 + operand[0].reg;
1043               else
1044                 /* ldmac has mac[hl] as the second operand.  */
1045                 nib = 2 + operand[1].reg;
1046             }
1047         }
1048       nibble_count++;
1049
1050       *p++ = nib;
1051     }
1052
1053   /* Disgusting.  Why, oh why didn't someone ask us for advice
1054      on the assembler format.  */
1055   if (strcmp (this_try->name, "stm.l") == 0
1056       || strcmp (this_try->name, "ldm.l") == 0)
1057     {
1058       int high, low;
1059       high = (operand[this_try->name[0] == 'l' ? 1 : 0].reg >> 8) & 0xf;
1060       low = operand[this_try->name[0] == 'l' ? 1 : 0].reg & 0xf;
1061
1062       asnibbles[2] = high - low;
1063       asnibbles[7] = (this_try->name[0] == 'l') ? high : low;
1064     }
1065
1066   for (i = 0; i < this_try->length; i++)
1067     {
1068       output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1069     }
1070
1071   /* Note if this is a movb instruction -- there's a special relaxation
1072      which only applies to them.  */
1073   if (strcmp (this_try->name, "mov.b") == 0)
1074     movb = 1;
1075
1076   /* Output any fixes.  */
1077   for (i = 0; i < 2; i++)
1078     {
1079       int x = operand[i].mode;
1080
1081       if (x & (IMM | DISP))
1082         {
1083           do_a_fix_imm (output - frag_now->fr_literal + immat,
1084                         operand + i, x & MEMRELAX != 0);
1085         }
1086       else if (x & ABS)
1087         {
1088           do_a_fix_imm (output - frag_now->fr_literal + absat,
1089                         operand + i, x & MEMRELAX ? movb + 1 : 0);
1090         }
1091       else if (x & PCREL)
1092         {
1093           int size16 = x & L_16;
1094           int where = size16 ? 2 : 1;
1095           int size = size16 ? 2 : 1;
1096           int type = size16 ? R_PCRWORD : R_PCRBYTE;
1097
1098           check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1099
1100           if (operand[i].exp.X_add_number & 1)
1101             {
1102               as_warn (_("branch operand has odd offset (%lx)\n"),
1103                        (unsigned long) operand->exp.X_add_number);
1104             }
1105
1106           operand[i].exp.X_add_number -= 1;
1107           operand[i].exp.X_add_number =
1108             ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1109
1110           fix_new_exp (frag_now,
1111                        output - frag_now->fr_literal + where,
1112                        size,
1113                        &operand[i].exp,
1114                        1,
1115                        type);
1116         }
1117       else if (x & MEMIND)
1118         {
1119           check_operand (operand + i, 0xff, "@@");
1120           fix_new_exp (frag_now,
1121                        output - frag_now->fr_literal + 1,
1122                        1,
1123                        &operand[i].exp,
1124                        0,
1125                        R_MEM_INDIRECT);
1126         }
1127       else if (x & ABSJMP)
1128         {
1129           /* This jmp may be a jump or a branch.  */
1130
1131           check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1132           if (operand[i].exp.X_add_number & 1)
1133             {
1134               as_warn (_("branch operand has odd offset (%lx)\n"),
1135                        (unsigned long) operand->exp.X_add_number);
1136             }
1137           if (!Hmode)
1138             operand[i].exp.X_add_number =
1139               ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1140           fix_new_exp (frag_now,
1141                        output - frag_now->fr_literal,
1142                        4,
1143                        &operand[i].exp,
1144                        0,
1145                        R_JMPL1);
1146         }
1147     }
1148 }
1149
1150 /* Try to give an intelligent error message for common and simple to
1151    detect errors.  */
1152 static void
1153 clever_message (opcode, operand)
1154      struct h8_opcode *opcode;
1155      struct h8_op *operand;
1156 {
1157   /* Find out if there was more than one possible opcode.  */
1158
1159   if ((opcode + 1)->idx != opcode->idx)
1160     {
1161       unsigned int argn;
1162
1163       /* Only one opcode of this flavour, try to guess which operand
1164          didn't match.  */
1165       for (argn = 0; argn < opcode->noperands; argn++)
1166         {
1167           switch (opcode->args.nib[argn])
1168             {
1169             case RD16:
1170               if (operand[argn].mode != RD16)
1171                 {
1172                   as_bad (_("destination operand must be 16 bit register"));
1173                   return;
1174
1175                 }
1176               break;
1177
1178             case RS8:
1179               if (operand[argn].mode != RS8)
1180                 {
1181                   as_bad (_("source operand must be 8 bit register"));
1182                   return;
1183                 }
1184               break;
1185
1186             case ABS16DST:
1187               if (operand[argn].mode != ABS16DST)
1188                 {
1189                   as_bad (_("destination operand must be 16bit absolute address"));
1190                   return;
1191                 }
1192               break;
1193             case RD8:
1194               if (operand[argn].mode != RD8)
1195                 {
1196                   as_bad (_("destination operand must be 8 bit register"));
1197                   return;
1198                 }
1199               break;
1200
1201             case ABS16SRC:
1202               if (operand[argn].mode != ABS16SRC)
1203                 {
1204                   as_bad (_("source operand must be 16bit absolute address"));
1205                   return;
1206                 }
1207               break;
1208
1209             }
1210         }
1211     }
1212   as_bad (_("invalid operands"));
1213 }
1214
1215 /* This is the guts of the machine-dependent assembler.  STR points to
1216    a machine dependent instruction.  This function is supposed to emit
1217    the frags/bytes it assembles.  */
1218 void
1219 md_assemble (str)
1220      char *str;
1221 {
1222   char *op_start;
1223   char *op_end;
1224   struct h8_op operand[2];
1225   struct h8_opcode *opcode;
1226   struct h8_opcode *prev_opcode;
1227
1228   char *dot = 0;
1229   char c;
1230   int size;
1231
1232   /* Drop leading whitespace.  */
1233   while (*str == ' ')
1234     str++;
1235
1236   /* Find the op code end.  */
1237   for (op_start = op_end = str;
1238        *op_end != 0 && *op_end != ' ';
1239        op_end++)
1240     {
1241       if (*op_end == '.')
1242         {
1243           dot = op_end + 1;
1244           *op_end = 0;
1245           op_end += 2;
1246           break;
1247         }
1248     }
1249
1250   if (op_end == op_start)
1251     {
1252       as_bad (_("can't find opcode "));
1253     }
1254   c = *op_end;
1255
1256   *op_end = 0;
1257
1258   opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1259                                            op_start);
1260
1261   if (opcode == NULL)
1262     {
1263       as_bad (_("unknown opcode"));
1264       return;
1265     }
1266
1267   /* We used to set input_line_pointer to the result of get_operands,
1268      but that is wrong.  Our caller assumes we don't change it.  */
1269
1270   (void) get_operands (opcode->noperands, op_end, operand);
1271   *op_end = c;
1272   prev_opcode = opcode;
1273
1274   size = SN;
1275   if (dot)
1276     {
1277       switch (*dot)
1278         {
1279         case 'b':
1280           size = SB;
1281           break;
1282
1283         case 'w':
1284           size = SW;
1285           break;
1286
1287         case 'l':
1288           size = SL;
1289           break;
1290         }
1291     }
1292   opcode = get_specific (opcode, operand, size);
1293
1294   if (opcode == 0)
1295     {
1296       /* Couldn't find an opcode which matched the operands.  */
1297       char *where = frag_more (2);
1298
1299       where[0] = 0x0;
1300       where[1] = 0x0;
1301       clever_message (prev_opcode, operand);
1302
1303       return;
1304     }
1305   if (opcode->size && dot)
1306     {
1307       if (opcode->size != *dot)
1308         {
1309           as_warn (_("mismatch between opcode size and operand size"));
1310         }
1311     }
1312
1313   build_bytes (opcode, operand);
1314 }
1315
1316 void
1317 tc_crawl_symbol_chain (headers)
1318      object_headers *headers ATTRIBUTE_UNUSED;
1319 {
1320   printf (_("call to tc_crawl_symbol_chain \n"));
1321 }
1322
1323 symbolS *
1324 md_undefined_symbol (name)
1325      char *name ATTRIBUTE_UNUSED;
1326 {
1327   return 0;
1328 }
1329
1330 void
1331 tc_headers_hook (headers)
1332      object_headers *headers ATTRIBUTE_UNUSED;
1333 {
1334   printf (_("call to tc_headers_hook \n"));
1335 }
1336
1337 /* Various routines to kill one day */
1338 /* Equal to MAX_PRECISION in atof-ieee.c */
1339 #define MAX_LITTLENUMS 6
1340
1341 /* Turn a string in input_line_pointer into a floating point constant
1342    of type TYPE, and store the appropriate bytes in *LITP.  The number
1343    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1344    returned, or NULL on OK.  */
1345
1346 char *
1347 md_atof (type, litP, sizeP)
1348      char type;
1349      char *litP;
1350      int *sizeP;
1351 {
1352   int prec;
1353   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1354   LITTLENUM_TYPE *wordP;
1355   char *t;
1356   char *atof_ieee ();
1357
1358   switch (type)
1359     {
1360     case 'f':
1361     case 'F':
1362     case 's':
1363     case 'S':
1364       prec = 2;
1365       break;
1366
1367     case 'd':
1368     case 'D':
1369     case 'r':
1370     case 'R':
1371       prec = 4;
1372       break;
1373
1374     case 'x':
1375     case 'X':
1376       prec = 6;
1377       break;
1378
1379     case 'p':
1380     case 'P':
1381       prec = 6;
1382       break;
1383
1384     default:
1385       *sizeP = 0;
1386       return _("Bad call to MD_ATOF()");
1387     }
1388   t = atof_ieee (input_line_pointer, type, words);
1389   if (t)
1390     input_line_pointer = t;
1391
1392   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1393   for (wordP = words; prec--;)
1394     {
1395       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1396       litP += sizeof (LITTLENUM_TYPE);
1397     }
1398   return 0;
1399 }
1400 \f
1401 CONST char *md_shortopts = "";
1402 struct option md_longopts[] = {
1403   {NULL, no_argument, NULL, 0}
1404 };
1405
1406 size_t md_longopts_size = sizeof (md_longopts);
1407
1408 int
1409 md_parse_option (c, arg)
1410      int c ATTRIBUTE_UNUSED;
1411      char *arg ATTRIBUTE_UNUSED;
1412 {
1413   return 0;
1414 }
1415
1416 void
1417 md_show_usage (stream)
1418      FILE *stream ATTRIBUTE_UNUSED;
1419 {
1420 }
1421 \f
1422 void
1423 tc_aout_fix_to_chars ()
1424 {
1425   printf (_("call to tc_aout_fix_to_chars \n"));
1426   abort ();
1427 }
1428
1429 void
1430 md_convert_frag (headers, seg, fragP)
1431      object_headers *headers ATTRIBUTE_UNUSED;
1432      segT seg ATTRIBUTE_UNUSED;
1433      fragS *fragP ATTRIBUTE_UNUSED;
1434 {
1435   printf (_("call to md_convert_frag \n"));
1436   abort ();
1437 }
1438
1439 valueT
1440 md_section_align (seg, size)
1441      segT seg;
1442      valueT size;
1443 {
1444   return ((size + (1 << section_alignment[(int) seg]) - 1)
1445           & (-1 << section_alignment[(int) seg]));
1446 }
1447
1448 void
1449 md_apply_fix (fixP, val)
1450      fixS *fixP;
1451      long val;
1452 {
1453   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1454
1455   switch (fixP->fx_size)
1456     {
1457     case 1:
1458       *buf++ = val;
1459       break;
1460     case 2:
1461       *buf++ = (val >> 8);
1462       *buf++ = val;
1463       break;
1464     case 4:
1465       *buf++ = (val >> 24);
1466       *buf++ = (val >> 16);
1467       *buf++ = (val >> 8);
1468       *buf++ = val;
1469       break;
1470     default:
1471       abort ();
1472     }
1473 }
1474
1475 int
1476 md_estimate_size_before_relax (fragP, segment_type)
1477      register fragS *fragP ATTRIBUTE_UNUSED;
1478      register segT segment_type ATTRIBUTE_UNUSED;
1479 {
1480   printf (_("call tomd_estimate_size_before_relax \n"));
1481   abort ();
1482 }
1483
1484 /* Put number into target byte order.  */
1485 void
1486 md_number_to_chars (ptr, use, nbytes)
1487      char *ptr;
1488      valueT use;
1489      int nbytes;
1490 {
1491   number_to_chars_bigendian (ptr, use, nbytes);
1492 }
1493
1494 long
1495 md_pcrel_from (fixP)
1496      fixS *fixP ATTRIBUTE_UNUSED;
1497 {
1498   abort ();
1499 }
1500
1501 void
1502 tc_reloc_mangle (fix_ptr, intr, base)
1503      fixS *fix_ptr;
1504      struct internal_reloc *intr;
1505      bfd_vma base;
1506
1507 {
1508   symbolS *symbol_ptr;
1509
1510   symbol_ptr = fix_ptr->fx_addsy;
1511
1512   /* If this relocation is attached to a symbol then it's ok
1513      to output it.  */
1514   if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1515     {
1516       /* cons likes to create reloc32's whatever the size of the reloc..
1517        */
1518       switch (fix_ptr->fx_size)
1519         {
1520         case 4:
1521           intr->r_type = R_RELLONG;
1522           break;
1523         case 2:
1524           intr->r_type = R_RELWORD;
1525           break;
1526         case 1:
1527           intr->r_type = R_RELBYTE;
1528           break;
1529         default:
1530           abort ();
1531         }
1532     }
1533   else
1534     {
1535       intr->r_type = fix_ptr->fx_r_type;
1536     }
1537
1538   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1539   intr->r_offset = fix_ptr->fx_offset;
1540
1541   if (symbol_ptr)
1542     {
1543       if (symbol_ptr->sy_number != -1)
1544         intr->r_symndx = symbol_ptr->sy_number;
1545       else
1546         {
1547           symbolS *segsym;
1548
1549           /* This case arises when a reference is made to `.'.  */
1550           segsym = seg_info (S_GET_SEGMENT (symbol_ptr))->dot;
1551           if (segsym == NULL)
1552             intr->r_symndx = -1;
1553           else
1554             {
1555               intr->r_symndx = segsym->sy_number;
1556               intr->r_offset += S_GET_VALUE (symbol_ptr);
1557             }
1558         }
1559     }
1560   else
1561     intr->r_symndx = -1;
1562 }