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