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