obj-coff-bfd:
[platform/upstream/binutils.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Hitachi h8/300
2    Copyright (C) 1991 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
24   steve@cygnus.com
25  */
26
27 #include <stdio.h>
28 #include "as.h"
29 #include "bfd.h"
30 #include "h8300-opcode.h"
31 #include <ctype.h>
32
33 char  comment_chars[]  = { ';',0 };
34
35 /* This table describes all the machine specific pseudo-ops the assembler
36    has to support.  The fields are:
37           pseudo-op name without dot
38           function to call to execute this pseudo-op
39           Integer arg to pass to the function
40  */
41 const pseudo_typeS md_pseudo_table[] = {
42         { 0,            0,              0       }
43 };
44
45 int  md_reloc_size ;
46  
47 const char EXP_CHARS[] = "eE";
48
49 /* Chars that mean this number is a floating point constant */
50 /* As in 0f12.456 */
51 /* or    0d1.2345e12 */
52  char FLT_CHARS[] = "rRsSfFdDxXpP";
53
54
55 const relax_typeS md_relax_table[1];
56
57
58 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
59 static struct hash_control *register_hash_control;      /* Register name hash table */
60
61
62 /*
63  This function is called once, at assembler startup time.  This should
64  set up all the tables, etc that the MD part of the assembler needs
65 */
66
67 reloc_howto_type *r16;
68 reloc_howto_type *r8;
69 reloc_howto_type *r8ff;
70 reloc_howto_type *r8pcrel;
71
72 void md_begin () 
73 {
74   bfd_arch_info_type *ai;
75   const struct h8_opcode *opcode;
76
77   opcode_hash_control = hash_new();
78   for (opcode = h8_opcodes; opcode->name; opcode++) {
79     hash_insert(opcode_hash_control, opcode->name, (char *)opcode);
80   }
81
82   ai = bfd_lookup_arch(bfd_arch_h8300,0);
83   
84   r16 = ai->reloc_type_lookup(ai, BFD_RELOC_16);
85   r8 = ai->reloc_type_lookup(ai, BFD_RELOC_8);
86   r8ff = ai->reloc_type_lookup(ai, BFD_RELOC_8_FFnn);
87   r8pcrel = ai->reloc_type_lookup(ai, BFD_RELOC_8_PCREL);
88
89   
90 }
91
92
93 struct h8_exp {
94   char *e_beg;
95   char *e_end;
96   expressionS e_exp;
97 };
98 struct h8_op 
99 {
100 op_type mode;
101   unsigned reg;
102   expressionS exp;
103 };
104
105
106
107 /*
108  parse operands 
109  WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
110  r0l,r0h,..r7l,r7h
111  @WREG
112  @WREG+
113  @-WREG
114  #const
115
116 */
117
118 op_type r8_sord[] = {RS8, RD8};
119 op_type r16_sord[] = {RS16, RD16};
120 op_type rind_sord[] = {RSIND, RDIND};
121 op_type abs_sord[2] = {ABS16SRC, ABS16DST};
122 op_type disp_sord[] = {DISPSRC, DISPDST};
123 /* try and parse a reg name, returns number of chars consumed */
124 int DEFUN(parse_reg,(src, mode, reg, dst),
125           char *src AND
126           op_type *mode AND
127           unsigned int *reg AND
128           int dst)
129 {
130   if (src[0]  == 's' && src[1] == 'p') {
131     *mode = r16_sord[dst];
132     *reg = 7;
133     return 2;
134   }
135   if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r') {
136     *mode = CCR;
137     *reg = 0;
138     return 3;
139   }
140   if (src[0]  == 'f' && src[1] == 'p') {
141     *mode = r16_sord[dst];
142     *reg = 6;
143     return 2;
144   }
145   if (src[0] == 'r') {
146     if (src[1] >= '0' && src[1] <= '7') {
147       if(src[2] == 'l') {
148         *mode = r8_sord[dst];
149         *reg = (src[1] - '0') + 8;
150         return 3;
151       }
152       if(src[2] == 'h') {
153         *mode = r8_sord[dst];
154         *reg = (src[1] - '0')  ;
155         return 3;
156       }
157       *mode = r16_sord[dst];
158       *reg = (src[1] - '0');
159       return 2;
160     }
161   }
162   return 0;
163 }
164
165 char *
166 DEFUN(parse_exp,(s, op),
167       char *s AND
168       expressionS *op)
169 {
170   char *save = input_line_pointer;
171   char *new;
172   segT seg;
173   input_line_pointer = s;
174   seg = expr(0,op);
175   new = input_line_pointer;
176   input_line_pointer = save;
177   if (SEG_NORMAL(seg)) 
178     return new;
179   switch (seg) {
180   case SEG_ABSOLUTE:
181   case SEG_UNKNOWN:
182   case SEG_DIFFERENCE:
183   case SEG_BIG:
184   case SEG_REGISTER:
185     return new;
186   case SEG_ABSENT:
187     as_bad("Missing operand");
188     return new;
189   default:
190     as_bad("Don't understand operand of type %s", segment_name (seg));
191     return new;
192   }
193 }
194
195
196 static void 
197 DEFUN(get_operand,(ptr, op, dst),
198       char **ptr AND 
199       struct h8_op *op AND 
200       unsigned int dst)
201 {
202   char *src = *ptr;
203   op_type mode;
204   unsigned   int num;
205   unsigned  int len;
206   op->mode = E;
207
208   while (*src == ' ') src++;
209   len = parse_reg(src, &op->mode, &op->reg, dst);
210   if (len) {
211     *ptr = src + len;
212     return ;
213   }
214       
215   if (*src == '@') {
216     src++;
217     if (*src == '-') {  
218       src++;
219       len = parse_reg(src, &mode, &num, dst);
220       if (len == 0 || mode != r16_sord[dst]) {
221         as_bad("@- needs word register");
222       }
223       op->mode = RDDEC;
224       op->reg = num;
225       *ptr = src + len;
226       return;
227     }
228     if (*src == '(' && ')') {
229       /* Disp */
230       src++;
231       src =      parse_exp(src, &op->exp);
232
233       if (*src == ')') {
234         src++;
235         op->mode = abs_sord[dst];
236         *ptr = src;
237         return;
238       }
239       if (*src  != ',') {
240         as_bad("expected @(exp, reg16)");
241       }
242       src++;
243       len = parse_reg(src, &mode, &op->reg, dst);
244       if (len == 0 || mode != r16_sord[dst])
245           {
246             as_bad("expected @(exp, reg16)");
247           }
248       op->mode = disp_sord[dst];
249       src += len;
250       if (*src != ')' && '(') {
251         as_bad("expected @(exp, reg16)");
252
253       }
254       *ptr = src +1;
255
256       return;
257     }
258     len = parse_reg(src, &mode, &num, dst);
259
260     if(len) {
261       src += len;
262       if (*src == '+') {
263         src++;
264         if (mode != RS16) {
265           as_bad("@Rn+ needs word register");
266         }
267         op->mode = RSINC;
268         op->reg = num;
269         *ptr = src;
270         return;
271       }
272       if (mode != r16_sord[dst]) {
273         as_bad("@Rn needs word register");
274       }
275       op->mode =rind_sord[dst];
276       op->reg = num;
277       *ptr = src;
278       return;
279     }
280     else {
281       /* must be a symbol */
282       op->mode = abs_sord[dst];
283       *ptr = parse_exp(src, &op->exp);
284       return;
285     }
286   }
287
288   
289   if (*src == '#') {
290     src++;
291     op->mode = IMM16;
292     *ptr = parse_exp(src, &op->exp);
293     return;
294   }
295   else {
296     *ptr = parse_exp(src, &op->exp);
297     op->mode = DISP8;
298   }
299 }
300
301 /* This is the guts of the machine-dependent assembler.  STR points to a
302    machine dependent instruction.  This funciton is supposed to emit
303    the frags/bytes it assembles to.
304  */
305  
306
307 void 
308 DEFUN(md_assemble,(str),
309       char *str)
310 {
311   char *op_start;
312   char *op_end;
313   unsigned int i;
314   
315   struct h8_opcode * opcode;
316   /* Drop leading whitespace */
317   while (*str == ' ')
318    str++;
319
320
321   /* find the op code end */
322   for (op_start = op_end = str;
323        *op_end != 0 && *op_end != ' ';
324        op_end ++)
325    ;
326
327   if (op_end == op_start) {
328       as_bad("can't find opcode ");
329     }
330   *op_end = 0;
331   opcode = (struct h8_opcode *) hash_find(opcode_hash_control,
332                                           op_start);
333
334   if (opcode == NULL) {
335       as_bad("unknown opcode");
336       return;
337     }
338
339
340 {
341   int ok = 1;
342   int j,i;
343   int dispreg = 0;
344   struct       h8_op operand[2];
345   char *ptr = op_end+1;
346   if (opcode->noperands)
347    get_operand(& ptr, &operand[0],0);
348   else operand[0].mode = 0;
349   if (opcode->noperands==2) {
350       if (*ptr == ',') ptr++;
351       get_operand(& ptr, &operand[1], 1);
352     }
353   else operand[1].mode = 0;
354
355
356
357 {
358   struct h8_opcode *this_try ;
359   int found = 0;
360   for (j = 0; j < opcode->nopcodes && !found; j++) {
361       this_try  = opcode + j;
362       for (i = 0; i < opcode->noperands; i++) {
363           op_type op = (this_try->args.nib[i]) & ~(B30|B31);
364           switch (op) {
365             case Hex0:
366             case Hex1:
367             case Hex2:
368             case Hex3:
369             case Hex4:
370             case Hex5:
371             case Hex6:
372             case Hex7:
373             case Hex8:
374             case Hex9:
375             case HexA:
376             case HexB:
377             case HexC:
378             case HexD:
379             case HexE:
380             case HexF:
381               break;
382             case DISPSRC:
383             case DISPDST:
384               dispreg = operand[i].reg; 
385             case RD8:
386             case RS8:
387             case RDIND:
388             case RSIND:
389             case RD16:
390             case RS16:
391             case CCR:
392             case RSINC:
393             case RDDEC:
394               if (operand[i].mode != op) goto fail;
395               break;
396             case IMM8:
397               /* We have an expression, called IMM16, but we know we
398                  want an 8 bit value here */
399               if (operand[i].mode != IMM16) goto fail;
400               operand[i].mode = IMM8;
401               break;
402             case KBIT:
403             case IMM16:
404             case IMM3:
405               if (operand[i].mode != IMM16) goto fail;
406               break;
407             case ABS16SRC:
408             case ABS8SRC:
409               if (operand[i].mode != ABS16SRC) goto fail;
410               break;
411             case ABS16DST:
412             case ABS8DST:
413               if (operand[i].mode != ABS16DST) goto fail;
414                 
415               break;
416             }
417         }
418       found =1;
419     fail: ;
420     }
421   if (found == 0) 
422    as_bad("illegal operands for opcode");
423
424
425   /* Now we know what sort of opcodes etc, lets build the bytes -
426      actually we know how big the instruction will be too. So we
427      can get
428      */
429 {
430         char *output = frag_more(this_try->length);
431         char *output_ptr = output;
432         op_type *nibble_ptr = this_try->data.nib;
433         char part;
434         op_type c;
435         char high;
436         int nib;
437  top: ;
438         while (*nibble_ptr != E) {
439                 int nibble;
440                 for (nibble = 0; nibble <2; nibble++) {
441                         c = *nibble_ptr & ~(B30|B31);
442                         switch (c) {
443                         default:
444                                 abort();
445                         case KBIT:
446                                 switch  (operand[0].exp.X_add_number) {
447                                 case 1:
448                                         nib = 0;
449                                         break;
450                                 case 2:
451                                         nib = 8;
452                                         break;
453                                 default:
454                                         as_bad("Need #1 or #2 here");
455                                         break;
456                                 }
457                                 /* stop it making a fix */
458                                 operand[0].mode = 0;
459                                 break;
460                         case 0:
461                         case 1:
462                         case 2: case 3: case 4: case 5: case  6:
463                         case 7: case 8: case 9: case 10: case 11: 
464                         case  12: case 13: case 14: case 15:
465                                 nib = c;
466                                 break;
467                         case DISPREG:
468                                 nib = dispreg;
469                                 break;
470                         case IMM8:
471                                 nib = 0;
472                                 break;
473
474                         case DISPDST:
475                                 nib = 0;
476                                 break;
477                         case IMM3: 
478                                 if (operand[0].exp.X_add_symbol == 0) {
479                                         operand[0].mode = 0; /* stop it making a fix */
480                                         nib =  (operand[0].exp.X_add_number);
481                                 }
482                                 else as_bad("can't have symbol for bit number");
483                                 break;
484
485                         case ABS16DST:
486                                 nib = 0;
487                                 break;
488                         case DISPSRC:               
489                         case ABS16SRC:
490                         case IMM16:
491                                 nib=0;
492                                 break;
493
494
495                         case ABS8DST:
496                         case ABS8SRC:
497                         case IGNORE:
498
499
500                                 nib = 0;
501                                 break;
502                         case DISP8:
503                                 nib = 0;
504                                 break;
505                       
506                     
507                         case RS8:
508                         case RS16:
509                         case RSIND:
510                         case RSINC:
511
512                                 nib=  operand[0].reg;
513                                 break;
514                         case RD8:
515                         case RD16:
516                         case RDDEC:
517                         case RDIND:
518                                 nib  = operand[1].reg;
519
520                                 break;
521                         case E: 
522                                 abort();
523                                 break;
524                         }
525                         if (*nibble_ptr & B31) nib|=0x8;
526                         if (nibble == 0) {
527                                 *output_ptr = nib << 4;
528                         }
529                         else {
530                                 *output_ptr |= nib;
531                                 output_ptr++;
532                         }
533                         nibble_ptr++;
534                 }
535
536         }
537
538         /* output any fixes */
539         for (i = 0; i < 2; i++) 
540             {
541                     switch (operand[i].mode) {
542                     case 0:
543                             break;
544                     case DISP8:
545                             fix_new(frag_now,
546                                     output - frag_now->fr_literal + 1, 
547                                     1,
548                                     operand[i].exp.X_add_symbol,
549                                     operand[i].exp.X_subtract_symbol,
550                                     operand[i].exp.X_add_number -1,
551                                     1,
552                                     (int)r8pcrel);
553                             break;
554                     case IMM8:
555                             fix_new(frag_now,
556                                     output - frag_now->fr_literal + 1, 
557                                     1,
558                                     operand[i].exp.X_add_symbol,
559                                     operand[i].exp.X_subtract_symbol,
560                                     operand[i].exp.X_add_number,
561                                     0,
562                                     0);
563                             break;
564           
565                     case ABS16SRC:
566                     case ABS16DST:
567                     case IMM16:
568                     case DISPSRC:
569                     case DISPDST:
570                             fix_new(frag_now,
571                                     output - frag_now->fr_literal + 2, 
572                                     2,
573                                     operand[i].exp.X_add_symbol,
574                                     operand[i].exp.X_subtract_symbol,
575                                     operand[i].exp.X_add_number,
576                                     0,
577                                     (int)r16);
578                             break;
579                     case RS8:
580                     case RD8:
581                     case RS16:
582                     case RD16:
583                     case RDDEC: 
584                     case KBIT:
585                     case RSINC:
586                     case RDIND:
587                     case RSIND:
588                             break;
589                     default:
590                             abort();
591                     }
592             }
593
594
595 }
596 }
597 }
598 }
599
600 void 
601 DEFUN(tc_crawl_symbol_chain, (headers),
602 object_headers *headers)
603 {
604    printf("call to tc_crawl_symbol_chain \n");
605 }
606
607 symbolS *DEFUN(md_undefined_symbol,(name),
608                char *name)
609 {
610 return 0;
611 }
612
613 void 
614 DEFUN(tc_headers_hook,(headers),
615       object_headers *headers)
616 {
617   printf("call to tc_headers_hook \n"); 
618 }
619 void
620 DEFUN_VOID(md_end) 
621 {
622 }
623
624 /* Various routines to kill one day */
625 /* Equal to MAX_PRECISION in atof-ieee.c */
626 #define MAX_LITTLENUMS 6
627
628 /* Turn a string in input_line_pointer into a floating point constant of type
629    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
630    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
631  */
632 char *
633 md_atof(type,litP,sizeP)
634 char type;
635 char *litP;
636 int *sizeP;
637 {
638         int     prec;
639         LITTLENUM_TYPE words[MAX_LITTLENUMS];
640         LITTLENUM_TYPE *wordP;
641         char    *t;
642         char    *atof_ieee();
643
644         switch(type) {
645         case 'f':
646         case 'F':
647         case 's':
648         case 'S':
649                 prec = 2;
650                 break;
651
652         case 'd':
653         case 'D':
654         case 'r':
655         case 'R':
656                 prec = 4;
657                 break;
658
659         case 'x':
660         case 'X':
661                 prec = 6;
662                 break;
663
664         case 'p':
665         case 'P':
666                 prec = 6;
667                 break;
668
669         default:
670                 *sizeP=0;
671                 return "Bad call to MD_ATOF()";
672         }
673         t=atof_ieee(input_line_pointer,type,words);
674         if(t)
675                 input_line_pointer=t;
676
677         *sizeP=prec * sizeof(LITTLENUM_TYPE);
678         for(wordP=words;prec--;) {
679                 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
680                 litP+=sizeof(LITTLENUM_TYPE);
681         }
682         return "";      /* Someone should teach Dean about null pointers */
683 }
684
685 int
686 md_parse_option(argP, cntP, vecP)
687     char **argP;
688     int *cntP;
689     char ***vecP;
690
691   {abort();
692   }
693
694 int md_short_jump_size;
695
696 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
697                           abort(); }
698 void md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
699 char *ptr;
700 long from_addr;
701 long to_addr;
702 fragS *frag;
703 symbolS *to_symbol;
704 {
705         as_fatal("failed sanity check.");
706       }
707
708 void
709 md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
710     char *ptr;
711     long from_addr, to_addr;
712     fragS *frag;
713     symbolS *to_symbol;
714 {
715         as_fatal("failed sanity check.");
716 }
717
718 void
719 md_convert_frag(headers, fragP)
720 object_headers *headers;
721     fragS * fragP;
722
723  { printf("call to md_convert_frag \n"); abort(); }
724
725 long
726 DEFUN(md_section_align,(seg, size),
727         segT seg AND
728         long size)
729 {
730         return((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
731
732 }
733
734 void
735 md_apply_fix(fixP, val)
736         fixS *fixP;
737         long val;
738 {
739         char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
740
741         switch(fixP->fx_size) {
742         case 1:
743                 *buf++=val;
744                 break;
745         case 2:
746                 *buf++=(val>>8);
747                 *buf++=val;
748                 break;
749         case 4:
750                 *buf++=(val>>24);
751                 *buf++=(val>>16);
752                 *buf++=(val>>8);
753                 *buf++=val;
754                 break;
755         default:
756                 abort();
757                 
758         }
759 }
760
761 void DEFUN(md_operand, (expressionP),expressionS *expressionP) 
762 { }
763
764 int  md_long_jump_size;
765 int
766 md_estimate_size_before_relax(fragP, segment_type)
767      register fragS *fragP;
768      register segT segment_type;
769 { printf("call tomd_estimate_size_before_relax \n"); abort(); }
770 /* Put number into target byte order */
771
772 void DEFUN(md_number_to_chars,(ptr, use, nbytes),
773            char *ptr AND
774            long use AND
775            int nbytes)
776 {
777   switch (nbytes) {
778   case 4: *ptr++ = (use >> 24) & 0xff;
779   case 3: *ptr++ = (use >> 16) & 0xff;
780   case 2: *ptr++ = (use >> 8) & 0xff;
781   case 1: *ptr++ = (use >> 0) & 0xff;
782     break;
783   default:
784     abort();
785   }
786 }
787 long md_pcrel_from(fixP) 
788 fixS *fixP; { abort(); }
789
790 void tc_coff_symbol_emit_hook() { }