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