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