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