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