Fixed typos.
[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
273   input_line_pointer = s;
274   if (expression (op) == O_absent)
275     as_bad ("missing operand");
276   new = input_line_pointer;
277   input_line_pointer = save;
278   return new;
279 }
280
281 static char *
282 skip_colonthing (ptr, exp, mode)
283      char *ptr;
284      expressionS *exp;
285      int *mode;
286 {
287   if (*ptr == ':')
288     {
289       ptr++;
290       if (*ptr == '8')
291         {
292           ptr++;
293           /* ff fill any 8 bit quantity */
294           exp->X_add_number |= 0xff00;
295         }
296       else
297         {
298           *mode &= ~SIZE;
299           if (*ptr == '2')
300             {
301               *mode |= L_24;
302             }
303           else if (*ptr == '1')
304             {
305               *mode |= L_16;
306             }
307           while (isdigit (*ptr))
308             ptr++;
309         }
310     }
311   return ptr;
312 }
313
314 /* The many forms of operand:
315
316    Rn                   Register direct
317    @Rn                  Register indirect
318    @(exp[:16], Rn)      Register indirect with displacement
319    @Rn+
320    @-Rn
321    @aa:8                        absolute 8 bit
322    @aa:16                       absolute 16 bit
323    @aa                  absolute 16 bit
324
325    #xx[:size]           immediate data
326    @(exp:[8], pc)               pc rel
327    @@aa[:8]             memory indirect
328
329    */
330
331 char *
332 colonmod24 (op, src)
333      struct h8_op *op;
334      char *src;
335
336 {
337   int mode = 0;
338   src = skip_colonthing (src, &op->exp, &mode);
339
340   if (!mode)
341     {
342       /* Choose a default mode */
343       if (op->exp.X_add_number < -32768
344           || op->exp.X_add_number > 32767)
345         {
346           if (Hmode)
347             mode = L_24;
348           else 
349             mode = L_16;
350       }
351       else if (op->exp.X_add_symbol
352                || op->exp.X_op_symbol)
353         mode = DSYMMODE;
354       else
355         mode = DMODE;
356     }
357   op->mode |= mode;
358   return src;
359   
360 }
361
362
363 static void
364 get_operand (ptr, op, dst, direction)
365      char **ptr;
366      struct h8_op *op;
367      unsigned int dst;
368
369 {
370   char *src = *ptr;
371   op_type mode;
372   unsigned int num;
373   unsigned int len;
374   unsigned int size;
375
376   op->mode = E;
377
378   len = parse_reg (src, &op->mode, &op->reg, direction);
379   if (len)
380     {
381       *ptr = src + len;
382       return;
383     }
384
385   if (*src == '@')
386     {
387       src++;
388       if (*src == '@')
389         {
390           src++;
391           src = parse_exp (src, &op->exp);
392
393           src = skip_colonthing (src, &op->exp, &op->mode);
394
395           *ptr = src;
396
397           op->mode = MEMIND;
398           return;
399
400         }
401
402
403       if (*src == '-')
404         {
405           src++;
406           len = parse_reg (src, &mode, &num, direction);
407           if (len == 0)
408             {
409               /* Oops, not a reg after all, must be ordinary exp */
410               src--;
411               /* must be a symbol */
412               op->mode = ABS | PSIZE | direction;
413               *ptr = skip_colonthing (parse_exp (src, &op->exp),
414                                       &op->exp, &op->mode);
415
416               return;
417
418
419             }
420
421
422           if ((mode & SIZE) != PSIZE)
423             as_bad ("Wrong size pointer register for architecture.");
424           op->mode = RDDEC;
425           op->reg = num;
426           *ptr = src + len;
427           return;
428         }
429       if (*src == '(' )
430         {
431           /* Disp */
432           src++;
433
434           /* Start off assuming a 16 bit offset */
435
436
437           src = parse_exp (src, &op->exp);
438
439           src = colonmod24 (op, src);
440
441           if (*src == ')')
442             {
443               src++;
444               op->mode = DISP | direction;
445               *ptr = src;
446               return;
447             }
448
449           if (*src != ',')
450             {
451               as_bad ("expected @(exp, reg16)");
452               return;
453
454             }
455           src++;
456
457           len = parse_reg (src, &mode, &op->reg, direction);
458           if (len == 0 || !(mode & REG))
459             {
460               as_bad ("expected @(exp, reg16)");
461               return;
462             }
463           op->mode |= DISP | direction;
464           dispreg = op->reg;
465           src += len;
466           src = skip_colonthing (src, &op->exp, &op->mode);
467
468           if (*src != ')' && '(')
469             {
470               as_bad ("expected @(exp, reg16)");
471               return;
472             }
473           *ptr = src + 1;
474
475           return;
476         }
477       len = parse_reg (src, &mode, &num, direction);
478
479       if (len)
480         {
481           src += len;
482           if (*src == '+')
483             {
484               src++;
485               if ((mode & SIZE) != PSIZE)
486                 as_bad ("Wrong size pointer register for architecture.");
487               op->mode = RSINC;
488               op->reg = num;
489               *ptr = src;
490               return;
491             }
492           if ((mode & SIZE) != PSIZE)
493             as_bad ("Wrong size pointer register for architecture.");
494
495           op->mode = direction | IND | PSIZE;
496           op->reg = num;
497           *ptr = src;
498
499           return;
500         }
501       else
502         {
503           /* must be a symbol */
504
505           op->mode = ABS | direction;
506           src = parse_exp (src, &op->exp);
507
508           *ptr = colonmod24 (op, src);
509
510           return;
511         }
512     }
513
514
515   if (*src == '#')
516     {
517       src++;
518       op->mode = IMM;
519       src = parse_exp (src, &op->exp);
520       *ptr = skip_colonthing (src, &op->exp, &op->mode);
521
522       return;
523     }
524   else
525     {
526       src = parse_exp (src, &op->exp);
527       /* Trailing ':' size ? */
528       if (*src == ':')
529         {
530           if (src[1] == '1' && src[2] == '6')
531             {
532               op->mode = PCREL | L_16;
533               src += 3;
534             }
535           else if (src[1] == '8')
536             {
537               op->mode = PCREL | L_8;
538               src += 2;
539             }
540           else
541             {
542               as_bad ("expect :8 or :16 here");
543             }
544         }
545       else
546         {
547           op->mode = PCREL | bsize;
548         }
549       *ptr = src;
550     }
551 }
552
553
554 static
555 char *
556 DEFUN (get_operands, (noperands, op_end, operand),
557        unsigned int noperands AND
558        char *op_end AND
559        struct h8_op *operand)
560 {
561   char *ptr = op_end;
562
563   switch (noperands)
564     {
565     case 0:
566       operand[0].mode = 0;
567       operand[1].mode = 0;
568       break;
569
570     case 1:
571       ptr++;
572       get_operand (&ptr, operand + 0, 0, SRC);
573       if (*ptr == ',')
574         {
575           ptr++;
576           get_operand (&ptr, operand + 1, 1, DST);
577         }
578       else
579         {
580           operand[1].mode = 0;
581         }
582
583       break;
584     case 2:
585       ptr++;
586       get_operand (&ptr, operand + 0, 0, SRC);
587       if (*ptr == ',')
588         ptr++;
589       get_operand (&ptr, operand + 1, 1, DST);
590       break;
591
592     default:
593       abort ();
594     }
595
596
597   return ptr;
598 }
599
600 /* Passed a pointer to a list of opcodes which use different
601    addressing modes, return the opcode which matches the opcodes
602    provided
603    */
604 static
605 struct h8_opcode *
606 get_specific (opcode, operands)
607      struct h8_opcode *opcode;
608      struct h8_op *operands;
609 {
610   struct h8_opcode *this_try = opcode;
611   int found = 0;
612
613   unsigned int this_index = opcode->idx;
614
615   while (this_index == opcode->idx && !found)
616     {
617       unsigned int i;
618       found = 1;
619
620       this_try = opcode++;
621       for (i = 0; i < this_try->noperands && found; i++)
622         {
623           op_type op = this_try->args.nib[i];
624           int x = operands[i].mode;
625
626           if ((op & (DISP | REG)) == (DISP | REG)
627               && ((x & DISP | REG) == (DISP | REG)))
628             {
629               dispreg = operands[i].reg;
630             }
631           else if (op & REG)
632             {
633               if (!(x & REG))
634                 found = 0;
635
636               if (x & L_P)
637                 {
638                   x = (x & ~L_P) | (Hmode ? L_32 : L_16);
639                 }
640               if (op & L_P)
641                 {
642                   op = (op & ~L_P) | (Hmode ? L_32 : L_16);
643                 }
644
645               opsize = op & SIZE;
646
647               /* The size of the reg is v important */
648               if ((op & SIZE) != (x & SIZE))
649                 found = 0;
650             }
651           else if ((op & ABSJMP) && (x & ABS))
652             {
653               operands[i].mode &= ~ABS;
654               operands[i].mode |= ABSJMP;
655               /* But it may not be 24 bits long */
656               if (!Hmode)
657                 {
658                   operands[i].mode &= ~SIZE;
659                   operands[i].mode |= L_16;
660                 }
661
662
663             }
664           else if ((op & (KBIT | DBIT)) && (x & IMM))
665             {
666               /* This is ok if the immediate value is sensible */
667
668             }
669           else if (op & PCREL)
670             {
671
672               /* The size of the displacement is important */
673               if ((op & SIZE) != (x & SIZE))
674                 found = 0;
675
676             }
677           else if ((op & (DISP | IMM | ABS))
678                    && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
679             {
680               /* Got a diplacement,will fit if no size or same size as try */
681               if ((x & SIZE) != 0
682                   && ((op & SIZE) != (x & SIZE)))
683                 found = 0;
684             }
685           else if ((op & ABSMOV) && (x & ABS))
686             {
687               /* Ok */
688             }
689           else if ((op & MODE) != (x & MODE))
690             {
691               found = 0;
692             }
693
694         }
695     }
696   if (found)
697     return this_try;
698   else
699     return 0;
700 }
701
702 static void
703 DEFUN (check_operand, (operand, width, string),
704        struct h8_op *operand AND
705        unsigned int width AND
706        char *string)
707 {
708   if (operand->exp.X_add_symbol == 0
709       && operand->exp.X_op_symbol == 0)
710     {
711
712       /* No symbol involved, let's look at offset, it's dangerous if any of
713          the high bits are not 0 or ff's, find out by oring or anding with
714          the width and seeing if the answer is 0 or all fs*/
715       if ((operand->exp.X_add_number & ~width) != 0 &&
716           (operand->exp.X_add_number | width) != (~0))
717         {
718           as_warn ("operand %s0x%x out of range.", string, operand->exp.X_add_number);
719         }
720     }
721
722 }
723
724 static void
725 do_a_fix_imm (offset, operand, relaxing)
726      int offset;
727      struct h8_op *operand;
728      int relaxing;
729 {
730   int idx;
731   int size;
732   int where;
733
734
735   char *t = operand->mode & IMM ? "#" : "@";
736
737   if (operand->exp.X_add_symbol == 0)
738     {
739       char *bytes = frag_now->fr_literal + offset;
740       switch (operand->mode & SIZE)
741         {
742         case L_3:
743           check_operand (operand, 0x7, t);
744           bytes[0] |= (operand->exp.X_add_number) << 4;
745           break;
746         case L_8:
747           check_operand (operand, 0xff, t);
748           bytes[0] = operand->exp.X_add_number;
749           break;
750         case L_16:
751           check_operand (operand, 0xffff, t);
752           bytes[0] = operand->exp.X_add_number >> 8;
753           bytes[1] = operand->exp.X_add_number >> 0;
754           break;
755         case L_24:
756           check_operand (operand, 0xffffff, t);
757           bytes[0] = operand->exp.X_add_number >> 16;
758           bytes[1] = operand->exp.X_add_number >> 8;
759           bytes[2] = operand->exp.X_add_number >> 0;
760           break;
761
762         case L_32:
763           /* This should be done with bfd */
764           bytes[0] = operand->exp.X_add_number >> 24;
765           bytes[1] = operand->exp.X_add_number >> 16;
766           bytes[2] = operand->exp.X_add_number >> 8;
767           bytes[3] = operand->exp.X_add_number >> 0;
768           break;
769         }
770
771     }
772   else
773     {
774       switch (operand->mode & SIZE)
775         {
776         default:
777           abort ();
778         case L_24:
779           size = 4;
780           where = -1;
781           idx = relaxing ? R_MOVLB1 : R_RELLONG;
782           break;
783
784         case L_32:
785           size = 4;
786           where = 0;
787           idx = R_RELLONG;
788           break;
789         case L_16:
790           size = 2;
791           where = 0;
792           idx = relaxing ? R_MOVB1 : R_RELWORD;
793           break;
794         case L_8:
795           size = 1;
796           where = 0;
797           idx = R_RELBYTE;
798         }
799
800
801       operand->exp.X_add_number = (short) operand->exp.X_add_number;
802       fix_new_exp (frag_now,
803                    offset + where,
804                    size,
805                    &operand->exp,
806                    0,
807                    idx);
808     }
809
810 }
811
812 /* Now we know what sort of opcodes it is, lets build the bytes -
813  */
814 static void
815 build_bytes (this_try, operand)
816      struct h8_opcode *this_try;
817      struct h8_op *operand;
818 {
819   unsigned int i;
820
821   char *output = frag_more (this_try->length);
822   char *output_ptr = output;
823   op_type *nibble_ptr = this_try->data.nib;
824   char part;
825   op_type c;
826   char high;
827   unsigned int nibble_count = 0;
828
829   int immat;
830   int nib;
831   char asnibbles[30];
832   char *p = asnibbles;
833
834   if (!(this_try->inbase || Hmode))
835     {
836       as_warn ("Opcode `%s' only available on H8/300-H", this_try->name);
837     }
838
839   while (*nibble_ptr != E)
840     {
841       int d;
842       c = *nibble_ptr++;
843
844       d = (c & DST) != 0;
845
846       if (c < 16)
847         {
848           nib = c;
849         }
850       else
851         {
852
853           if (c & (REG | IND | INC | DEC))
854             {
855               nib = operand[d].reg;
856             }
857           else if ((c & DISPREG) == (DISPREG))
858             {
859               nib = dispreg;
860             }
861
862           else if (c & ABSMOV) 
863             {
864               operand[d].mode &= ~ABS;
865               operand[d].mode |= ABSMOV;
866               immat = nibble_count / 2;
867               nib = 0;
868             }
869           else if (c & (IMM | PCREL | ABS | ABSJMP | DISP ))
870             {
871               operand[d].mode = c;
872               immat = nibble_count / 2;
873               nib = 0;
874             }
875           else if (c & IGNORE)
876             {
877               nib = 0;
878             }
879           else if (c & DBIT)
880             {
881               switch (operand[0].exp.X_add_number)
882                 {
883                 case 1:
884                   nib = c;
885                   break;
886                 case 2:
887                   nib = 0x8 | c;
888                   break;
889                 default:
890                   as_bad ("Need #1 or #2 here");
891                 }
892             }
893           else if (c & KBIT)
894             {
895               switch (operand[0].exp.X_add_number)
896                 {
897                 case 1:
898                   nib = 0;
899                   break;
900                 case 2:
901                   nib = 8;
902                   break;
903                 case 4:
904                   if (!Hmode)
905                     as_warn ("#4 only valid in h8/300 mode.");
906                   nib = 9;
907                   break;
908
909                 default:
910                   as_bad ("Need #1 or #2 here");
911                   break;
912                 }
913               /* stop it making a fix */
914               operand[0].mode = 0;
915             }
916
917           if (c & B31)
918             {
919               nib |= 0x8;
920             }
921         }
922       nibble_count++;
923
924       *p++ = nib;
925     }
926
927   for (i = 0; i < this_try->length; i++)
928     {
929       output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
930     }
931
932   /* output any fixes */
933   for (i = 0; i < 2; i++)
934     {
935       int x = operand[i].mode;
936
937       if (x & (IMM | ABS | DISP))
938         {
939           do_a_fix_imm (output - frag_now->fr_literal + immat, operand + i,0);
940         }
941       else if (x & PCREL)
942         {
943           int size16 = x & L_16;
944           int where = size16 ? 2 : 1;
945           int size = size16 ? 2 : 1;
946           int type = size16 ? R_PCRWORD : R_PCRBYTE;
947
948           check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
949
950           if (operand[i].exp.X_add_number & 1)
951             {
952               as_warn ("branch operand has odd offset (%x)\n",
953                        operand->exp.X_add_number);
954             }
955
956           operand[i].exp.X_add_number =
957             (char) (operand[i].exp.X_add_number - 1);
958           fix_new_exp (frag_now,
959                        output - frag_now->fr_literal + where,
960                        size,
961                        &operand[i].exp,
962                        1,
963                        type);
964         }
965       else if (x & MEMIND)
966         {
967
968           check_operand (operand + i, 0xff, "@@");
969           fix_new_exp (frag_now,
970                        output - frag_now->fr_literal + 1,
971                        1,
972                        &operand[i].exp,
973                        0,
974                        R_RELBYTE);
975         }
976
977       else if (x & ABSMOV)
978         {
979           /* This mov is either absolute long or thru a memory loc */
980           do_a_fix_imm (output - frag_now->fr_literal + immat, operand + i,1);
981         }
982
983       else if (x & ABSJMP)
984         {
985           /* This jmp may be a jump or a branch */
986
987           check_operand (operand + i, Hmode ? 0xfffff : 0xffff, "@");
988           if (operand[i].exp.X_add_number & 1)
989             {
990               as_warn ("branch operand has odd offset (%x)\n",
991                        operand->exp.X_add_number);
992             }
993           operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
994           fix_new_exp (frag_now,
995                        output - frag_now->fr_literal,
996                        4,
997                        &operand[i].exp,
998                        0,
999                        R_JMPL1);
1000         }
1001     }
1002
1003 }
1004
1005 /*
1006   try and give an intelligent error message for common and simple to
1007   detect errors
1008   */
1009
1010 static void
1011 DEFUN (clever_message, (opcode, operand),
1012        struct h8_opcode *opcode AND
1013        struct h8_op *operand)
1014 {
1015   struct h8_opcode *scan = opcode;
1016
1017   /* Find out if there was more than one possible opccode */
1018
1019   if ((opcode + 1)->idx != opcode->idx)
1020     {
1021       unsigned int argn;
1022
1023       /* Only one opcode of this flavour, try and guess which operand
1024          didn't match */
1025       for (argn = 0; argn < opcode->noperands; argn++)
1026         {
1027           switch (opcode->args.nib[argn])
1028             {
1029             case RD16:
1030               if (operand[argn].mode != RD16)
1031                 {
1032                   as_bad ("destination operand must be 16 bit register");
1033                   return;
1034
1035                 }
1036               break;
1037
1038             case RS8:
1039
1040               if (operand[argn].mode != RS8)
1041                 {
1042                   as_bad ("source operand must be 8 bit register");
1043                   return;
1044                 }
1045               break;
1046
1047             case ABS16DST:
1048               if (operand[argn].mode != ABS16DST)
1049                 {
1050                   as_bad ("destination operand must be 16bit absolute address");
1051                   return;
1052                 }
1053               break;
1054             case RD8:
1055               if (operand[argn].mode != RD8)
1056                 {
1057                   as_bad ("destination operand must be 8 bit register");
1058                   return;
1059                 }
1060               break;
1061
1062
1063             case ABS16SRC:
1064               if (operand[argn].mode != ABS16SRC)
1065                 {
1066                   as_bad ("source operand must be 16bit absolute address");
1067                   return;
1068                 }
1069               break;
1070
1071             }
1072         }
1073     }
1074   as_bad ("invalid operands");
1075 }
1076
1077 /* This is the guts of the machine-dependent assembler.  STR points to a
1078    machine dependent instruction.  This funciton is supposed to emit
1079    the frags/bytes it assembles to.
1080    */
1081
1082
1083
1084 void
1085 DEFUN (md_assemble, (str),
1086        char *str)
1087 {
1088   char *op_start;
1089   char *op_end;
1090   unsigned int i;
1091   struct h8_op operand[2];
1092   struct h8_opcode *opcode;
1093   struct h8_opcode *prev_opcode;
1094
1095   char *dot = 0;
1096   char c;
1097
1098   /* Drop leading whitespace */
1099   while (*str == ' ')
1100     str++;
1101
1102   /* find the op code end */
1103   for (op_start = op_end = str;
1104        *op_end != 0 && *op_end != ' ';
1105        op_end++)
1106     {
1107       if (*op_end == '.')
1108         {
1109           dot = op_end + 1;
1110           *op_end = 0;
1111           op_end += 2;
1112           break;
1113         }
1114     }
1115
1116   ;
1117
1118   if (op_end == op_start)
1119     {
1120       as_bad ("can't find opcode ");
1121     }
1122   c = *op_end;
1123
1124   *op_end = 0;
1125
1126   opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1127                                            op_start);
1128
1129   if (opcode == NULL)
1130     {
1131       as_bad ("unknown opcode");
1132       return;
1133     }
1134
1135
1136   input_line_pointer = get_operands (opcode->noperands, op_end,
1137                                      operand);
1138   *op_end = c;
1139   prev_opcode = opcode;
1140
1141   opcode = get_specific (opcode, operand);
1142
1143   if (opcode == 0)
1144     {
1145       /* Couldn't find an opcode which matched the operands */
1146       char *where = frag_more (2);
1147
1148       where[0] = 0x0;
1149       where[1] = 0x0;
1150       clever_message (prev_opcode, operand);
1151
1152       return;
1153     }
1154   if (opcode->size && dot)
1155     {
1156       if (opcode->size != *dot)
1157         {
1158           as_warn ("mismatch between opcode size and operand size");
1159         }
1160     }
1161
1162   build_bytes (opcode, operand);
1163
1164 }
1165
1166 void
1167 DEFUN (tc_crawl_symbol_chain, (headers),
1168        object_headers * headers)
1169 {
1170   printf ("call to tc_crawl_symbol_chain \n");
1171 }
1172
1173 symbolS *
1174 DEFUN (md_undefined_symbol, (name),
1175        char *name)
1176 {
1177   return 0;
1178 }
1179
1180 void
1181 DEFUN (tc_headers_hook, (headers),
1182        object_headers * headers)
1183 {
1184   printf ("call to tc_headers_hook \n");
1185 }
1186
1187 void
1188 DEFUN_VOID (md_end)
1189 {
1190 }
1191
1192 /* Various routines to kill one day */
1193 /* Equal to MAX_PRECISION in atof-ieee.c */
1194 #define MAX_LITTLENUMS 6
1195
1196 /* Turn a string in input_line_pointer into a floating point constant of type
1197    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1198    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1199    */
1200 char *
1201 md_atof (type, litP, sizeP)
1202      char type;
1203      char *litP;
1204      int *sizeP;
1205 {
1206   int prec;
1207   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1208   LITTLENUM_TYPE *wordP;
1209   char *t;
1210   char *atof_ieee ();
1211
1212   switch (type)
1213     {
1214     case 'f':
1215     case 'F':
1216     case 's':
1217     case 'S':
1218       prec = 2;
1219       break;
1220
1221     case 'd':
1222     case 'D':
1223     case 'r':
1224     case 'R':
1225       prec = 4;
1226       break;
1227
1228     case 'x':
1229     case 'X':
1230       prec = 6;
1231       break;
1232
1233     case 'p':
1234     case 'P':
1235       prec = 6;
1236       break;
1237
1238     default:
1239       *sizeP = 0;
1240       return "Bad call to MD_ATOF()";
1241     }
1242   t = atof_ieee (input_line_pointer, type, words);
1243   if (t)
1244     input_line_pointer = t;
1245
1246   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1247   for (wordP = words; prec--;)
1248     {
1249       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1250       litP += sizeof (LITTLENUM_TYPE);
1251     }
1252   return "";
1253 }
1254
1255 int
1256 md_parse_option (argP, cntP, vecP)
1257      char **argP;
1258      int *cntP;
1259      char ***vecP;
1260
1261 {
1262   return 0;
1263
1264 }
1265
1266 int md_short_jump_size;
1267
1268 void
1269 tc_aout_fix_to_chars ()
1270 {
1271   printf ("call to tc_aout_fix_to_chars \n");
1272   abort ();
1273 }
1274
1275 void
1276 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1277      char *ptr;
1278      addressT from_addr;
1279      addressT to_addr;
1280      fragS *frag;
1281      symbolS *to_symbol;
1282 {
1283   as_fatal ("failed sanity check.");
1284 }
1285
1286 void
1287 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1288      char *ptr;
1289      addressT from_addr, to_addr;
1290      fragS *frag;
1291      symbolS *to_symbol;
1292 {
1293   as_fatal ("failed sanity check.");
1294 }
1295
1296 void
1297 md_convert_frag (headers, fragP)
1298      object_headers *headers;
1299      fragS *fragP;
1300
1301 {
1302   printf ("call to md_convert_frag \n");
1303   abort ();
1304 }
1305
1306 valueT md_section_align (seg, size)
1307      segT seg;
1308      valueT size;
1309 {
1310   return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1311
1312 }
1313
1314 void
1315 md_apply_fix (fixP, val)
1316      fixS *fixP;
1317      long val;
1318 {
1319   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1320
1321   switch (fixP->fx_size)
1322     {
1323     case 1:
1324       *buf++ = val;
1325       break;
1326     case 2:
1327       *buf++ = (val >> 8);
1328       *buf++ = val;
1329       break;
1330     case 4:
1331       *buf++ = (val >> 24);
1332       *buf++ = (val >> 16);
1333       *buf++ = (val >> 8);
1334       *buf++ = val;
1335       break;
1336     default:
1337       abort ();
1338
1339     }
1340 }
1341
1342 void
1343 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1344 {
1345 }
1346
1347 int md_long_jump_size;
1348 int
1349 md_estimate_size_before_relax (fragP, segment_type)
1350      register fragS *fragP;
1351      register segT segment_type;
1352 {
1353   printf ("call tomd_estimate_size_before_relax \n");
1354   abort ();
1355 }
1356
1357 /* Put number into target byte order */
1358
1359 void
1360 DEFUN (md_number_to_chars, (ptr, use, nbytes),
1361        char *ptr AND
1362        valueT use AND
1363        int nbytes)
1364 {
1365   switch (nbytes)
1366     {
1367     case 4:
1368       *ptr++ = (use >> 24) & 0xff;
1369     case 3:
1370       *ptr++ = (use >> 16) & 0xff;
1371     case 2:
1372       *ptr++ = (use >> 8) & 0xff;
1373     case 1:
1374       *ptr++ = (use >> 0) & 0xff;
1375       break;
1376     default:
1377       abort ();
1378     }
1379 }
1380 long
1381 md_pcrel_from (fixP)
1382      fixS *fixP;
1383 {
1384   abort ();
1385 }
1386
1387 void
1388 tc_coff_symbol_emit_hook ()
1389 {
1390 }
1391
1392
1393 void
1394 tc_reloc_mangle (fix_ptr, intr, base)
1395      fixS *fix_ptr;
1396      struct internal_reloc *intr;
1397      bfd_vma base;
1398
1399 {
1400   symbolS *symbol_ptr;
1401
1402   symbol_ptr = fix_ptr->fx_addsy;
1403
1404   /* If this relocation is attached to a symbol then it's ok
1405      to output it */
1406   if (fix_ptr->fx_r_type == RELOC_32)
1407     {
1408       /* cons likes to create reloc32's whatever the size of the reloc..
1409        */
1410       switch (fix_ptr->fx_size)
1411         {
1412
1413         case 2:
1414           intr->r_type = R_RELWORD;
1415           break;
1416         case 1:
1417           intr->r_type = R_RELBYTE;
1418           break;
1419         default:
1420           abort ();
1421
1422         }
1423
1424     }
1425   else
1426     {
1427       intr->r_type = fix_ptr->fx_r_type;
1428     }
1429
1430   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1431   intr->r_offset = fix_ptr->fx_offset;
1432
1433   if (symbol_ptr)
1434     intr->r_symndx = symbol_ptr->sy_number;
1435   else
1436     intr->r_symndx = -1;
1437
1438
1439 }
1440
1441 tc_coff_sizemachdep ()
1442 {
1443   abort ();
1444 }
1445
1446 /* end of tc-h8300.c */