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