Wed Jun 24 10:57:54 1992 Steve Chamberlain (sac@thepub.cygnus.com)
[platform/upstream/binutils.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2    Copyright (C) 1991, 1992 Free Software Foundation.
3    
4    This file is part of GAS, the GNU Assembler.
5    
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10    
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* 
22   Written By Steve Chamberlain
23   sac@cygnus.com
24   */
25
26 #include <stdio.h>
27 #include "as.h"
28 #include "bfd.h"
29 #include "opcode/h8300.h"
30 #include <ctype.h>
31 #include "listing.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
45 const pseudo_typeS md_pseudo_table[] = 
46 {
47 { "int",        cons,           2       },
48 { "data.b",     cons,           1      },
49 { "data.w",     cons,            2      },
50 { "data.l",     cons,           4      },
51 { "form",       listing_psize,  0      },
52 { "heading",    listing_title,  0},
53 { "import",     s_ignore,       0},
54 { "page",       listing_eject,  0},
55 { "program",    s_ignore,       0},
56 { 0,0,0 }
57 };
58
59 int  md_reloc_size ;
60
61 const char EXP_CHARS[] = "eE";
62
63 /* Chars that mean this number is a floating point constant */
64 /* As in 0f12.456 */
65 /* or    0d1.2345e12 */
66 char FLT_CHARS[] = "rRsSfFdDxXpP";
67
68
69 const relax_typeS md_relax_table[1];
70
71
72 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
73
74
75 /*
76   This function is called once, at assembler startup time.  This should
77   set up all the tables, etc that the MD part of the assembler needs
78   */
79 #if 0
80 /* encode the size and number into the number field
81    xxnnnn
82    00    8 bit
83    01    16 bit
84    10    ccr
85    nnnnreg number
86    */
87 #define WORD_REG 0x10
88 #define BYTE_REG 0x00
89 #define CCR_REG  0x20
90 struct reg_entry
91 {
92         char *name;
93         char number;
94 };
95
96 struct reg_entry reg_list[] = { 
97         "r0",WORD_REG +0,
98         "r1",WORD_REG +1,
99         "r2",WORD_REG +2,
100         "r3",WORD_REG +3,
101         "r4",WORD_REG +4,
102         "r5",WORD_REG +5,
103         "r6",WORD_REG +6,
104         "r7",WORD_REG +7,
105         "fp",WORD_REG +6,
106         "sp",WORD_REG +7,
107         "r0h",BYTE_REG + 0,
108         "r0l",BYTE_REG + 1,
109         "r1h",BYTE_REG + 2,
110         "r1l",BYTE_REG + 3,
111         "r2h",BYTE_REG + 4,
112         "r2l",BYTE_REG + 5,
113         "r3h",BYTE_REG + 6,
114         "r3l",BYTE_REG + 7,
115         "r4h",BYTE_REG + 8,
116         "r4l",BYTE_REG + 9,
117         "r5h",BYTE_REG + 10,
118         "r5l",BYTE_REG + 11,
119         "r6h",BYTE_REG + 12,
120         "r6l",BYTE_REG + 13,
121         "r7h",BYTE_REG + 14,
122         "r7l",BYTE_REG + 15,
123         "ccr",CCR_REG,
124         0,0
125     }
126 ;
127
128
129 #endif
130
131
132 void md_begin () 
133 {
134         struct h8_opcode *opcode;
135         const  struct reg_entry *reg;
136         char prev_buffer[100];
137         int idx = 0;
138         
139         opcode_hash_control = hash_new();
140         prev_buffer[0] = 0;
141         
142         for (opcode = h8_opcodes; opcode->name; opcode++) 
143             {
144                     /* Strip off any . part when inserting the opcode and only enter
145                        unique codes into the hash table
146                        */
147                     char *src= opcode->name;
148                     unsigned int len = strlen(src);
149                     char *dst = malloc(len+1);
150                     char *buffer = dst;
151                     opcode->size = 0;    
152                     while (*src) {
153                             if (*src == '.') {
154                                     *dst++ = 0;
155                                     src++;
156                                     opcode->size = *src;
157                                     break;
158                             }
159                             *dst++ = *src++;
160                     }
161                     if (strcmp(buffer, prev_buffer)) 
162                         {
163                                 hash_insert(opcode_hash_control, buffer, (char *)opcode);
164                                 strcpy(prev_buffer, buffer);
165                                 idx++;
166                         }
167                     opcode->idx = idx;
168                     
169                     
170                     /* Find the number of operands */
171                     opcode->noperands = 0;
172                     while (opcode->args.nib[opcode->noperands] != E)
173                         opcode->noperands ++;
174                     /* Find the length of the opcode in bytes */
175                     opcode->length =0;
176                     while (opcode->data.nib[opcode->length*2] != E)
177                         opcode->length++;
178             }
179         
180 }
181
182
183 struct h8_exp {
184         char *e_beg;
185         char *e_end;
186         expressionS e_exp;
187 };
188 struct h8_op 
189 {
190         unsigned int dispreg;
191         op_type mode;
192         unsigned reg;
193         expressionS exp;
194 };
195
196
197
198 /*
199   parse operands        
200   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
201   r0l,r0h,..r7l,r7h
202   @WREG
203   @WREG+
204   @-WREG
205   #const
206   
207   */
208
209 op_type r8_sord[] = {RS8, RD8};
210 op_type r16_sord[] = {RS16, RD16};
211 op_type rind_sord[] = {RSIND, RDIND};
212 op_type abs_sord[2] = {ABS16SRC, ABS16DST};
213 op_type disp_sord[] = {DISPSRC, DISPDST};
214
215 /* try and parse a reg name, returns number of chars consumed */
216 int 
217     DEFUN(parse_reg,(src, mode, reg, dst),
218           char *src AND
219           op_type *mode AND
220           unsigned int *reg AND
221           int dst)
222 {
223         if (src[0]  == 's' && src[1] == 'p') 
224             {
225                     *mode = r16_sord[dst];
226                     *reg = 7;
227                     return 2;
228             }
229         if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r') 
230             {
231                     *mode = CCR;
232                     *reg = 0;
233                     return 3;
234             }
235         if (src[0]  == 'f' && src[1] == 'p') 
236             {
237                     *mode = r16_sord[dst];
238                     *reg = 6;
239                     return 2;
240             }
241         if (src[0] == 'r') 
242             {
243                     if (src[1] >= '0' && src[1] <= '7') 
244                         {
245                                 if(src[2] == 'l') 
246                                     {
247                                             *mode = r8_sord[dst];
248                                             *reg = (src[1] - '0') + 8;
249                                             return 3;
250                                     }
251                                 if(src[2] == 'h') 
252                                     {
253                                             *mode = r8_sord[dst];
254                                             *reg = (src[1] - '0')  ;
255                                             return 3;
256                                     }
257                                 *mode = r16_sord[dst];
258                                 *reg = (src[1] - '0');
259                                 return 2;
260                         }
261             }
262         return 0;
263 }
264
265 char *
266     DEFUN(parse_exp,(s, op),
267           char *s AND
268           expressionS *op)
269 {
270         char *save = input_line_pointer;
271         char *new;
272         segT seg;
273         input_line_pointer = s;
274         seg = expr(0,op);
275         new = input_line_pointer;
276         input_line_pointer = save;
277         if (SEG_NORMAL(seg)) 
278             return new;
279         switch (seg) {
280         case SEG_ABSOLUTE:
281         case SEG_UNKNOWN:
282         case SEG_DIFFERENCE:
283         case SEG_BIG:
284         case SEG_REGISTER:
285                 return new;
286         case SEG_ABSENT:
287                 as_bad("Missing operand");
288                 return new;
289         default:
290                 as_bad("Don't understand operand of type %s", segment_name (seg));
291                 return new;
292         }
293 }
294
295 static char *
296     DEFUN(skip_colonthing,(ptr),
297           char *ptr)
298 {
299         if (*ptr == ':') {
300                 ptr++;
301                 while (isdigit(*ptr))
302                     ptr++;
303                 
304         }
305         return ptr;
306 }
307
308 /* The many forms of operand:
309    
310    Rn                   Register direct
311    @Rn                  Register indirect
312    @(exp[:16], Rn)      Register indirect with displacement
313    @Rn+
314    @-Rn
315    @aa:8                        absolute 8 bit
316    @aa:16                       absolute 16 bit
317    @aa                  absolute 16 bit
318    
319    #xx[:size]           immediate data
320    @(exp:[8], pc)               pc rel
321    @@aa[:8]             memory indirect
322    
323    */
324
325 static void 
326     DEFUN(get_operand,(ptr, op, dst),
327           char **ptr AND 
328           struct h8_op *op AND 
329           unsigned int dst)
330 {
331   char *src = *ptr;
332   op_type mode;
333   unsigned   int num;
334   unsigned  int len;
335   unsigned int size;
336   op->mode = E;
337         
338   len = parse_reg(src, &op->mode, &op->reg, dst);
339   if (len) {
340       *ptr = src + len;
341       return ;
342     }
343         
344   if (*src == '@') 
345   {
346     src++;
347     if (*src == '@') 
348     {
349       src++;
350       src = parse_exp(src,&op->exp);
351       src = skip_colonthing(src);
352                                 
353       *ptr = src;
354                                 
355       op->mode = MEMIND;
356       return;
357                                 
358     }
359                     
360                     
361     if (*src == '-') 
362     {   
363       src++;
364       len = parse_reg(src, &mode, &num, dst);
365       if (len == 0) 
366       {
367         /* Oops, not a reg after all, must be ordinary exp */
368         src--;
369         /* must be a symbol */
370         op->mode = abs_sord[dst];
371         *ptr = skip_colonthing(parse_exp(src, &op->exp));
372                                             
373         return;
374                                             
375                                             
376       }
377                                 
378       if (mode != r16_sord[dst]) 
379       {
380         as_bad("@- needs word register");
381       }
382       op->mode = RDDEC;
383       op->reg = num;
384       *ptr = src + len;
385       return;
386     }
387     if (*src == '(' && ')') 
388     {
389       /* Disp */
390       src++;
391       src =      parse_exp(src, &op->exp);
392                                 
393       if (*src == ')') 
394       {
395         src++;
396         op->mode = abs_sord[dst];
397         *ptr = src;
398         return;
399       }
400       src = skip_colonthing(src);
401                                 
402       if (*src  != ',') 
403       {
404         as_bad("expected @(exp, reg16)");
405         return;
406                                             
407       }
408       src++;
409       len = parse_reg(src, &mode, &op->reg, dst);
410       if (len == 0 || mode != r16_sord[dst])
411       {
412         as_bad("expected @(exp, reg16)");
413         return;
414       }
415       op->mode = disp_sord[dst];
416       src += len;
417       src = skip_colonthing(src);
418                                 
419       if (*src != ')' && '(') 
420       {
421         as_bad("expected @(exp, reg16)");
422         return;                                     
423       }
424       *ptr = src +1;
425                                 
426       return;
427     }
428     len = parse_reg(src, &mode, &num, dst);
429                     
430     if(len) {
431         src += len;
432         if (*src == '+') 
433         {
434           src++;
435           if (mode != RS16) 
436           {
437             as_bad("@Rn+ needs src word register");
438             return;
439           }
440           op->mode = RSINC;
441           op->reg = num;
442           *ptr = src;
443           return;
444         }
445         if (mode != r16_sord[dst]) 
446         {
447           as_bad("@Rn needs word register");
448           return;
449                                         
450         }
451         op->mode =rind_sord[dst];
452         op->reg = num;
453         *ptr = src;
454         
455          return;
456       }
457     else 
458     {
459       /* must be a symbol */
460       op->mode = abs_sord[dst];
461       *ptr = skip_colonthing(parse_exp(src, &op->exp));
462                                 
463       return;
464     }
465   }
466         
467         
468   if (*src == '#') {
469       src++;
470       op->mode = IMM16;
471       src = parse_exp(src, &op->exp);
472       *ptr= skip_colonthing(src);
473                 
474       return;
475     }
476   else {
477       *ptr = parse_exp(src, &op->exp);
478       op->mode = DISP8;
479     }
480 }
481
482
483 static
484     char *
485     DEFUN(get_operands,(noperands,op_end, operand),
486           unsigned int noperands AND
487           char *op_end AND
488           struct h8_op *operand) 
489 {
490         char *ptr = op_end;
491         switch (noperands) 
492             {
493             case 0:
494                     operand[0].mode = 0;
495                     operand[1].mode = 0;
496                     break;
497                     
498             case 1:    
499                     ptr++;
500                     get_operand(& ptr, operand +0,0);
501                     operand[1].mode =0;
502                     break;
503                     
504             case 2:
505                     ptr++;
506                     get_operand(& ptr, operand +0,0);
507                     if (*ptr == ',') ptr++;
508                     get_operand(& ptr, operand +1, 1);
509                     break;
510                     
511             default:
512                     abort();    
513             }
514         
515         
516         return ptr;    
517 }
518
519 /* Passed a pointer to a list of opcodes which use different
520    addressing modes, return the opcode which matches the opcodes
521    provided
522    */
523 static
524     struct h8_opcode *
525     DEFUN(get_specific,(opcode,  operands),
526           struct h8_opcode *opcode AND
527           struct     h8_op *operands)
528
529 {
530         struct h8_opcode *this_try = opcode ;
531         int found = 0;
532         unsigned int noperands = opcode->noperands;
533         
534         unsigned int dispreg;
535         unsigned int this_index = opcode->idx;
536         while (this_index == opcode->idx && !found) 
537             {
538                     unsigned int i;
539                     
540                     this_try  = opcode ++;
541                     for (i = 0; i < noperands; i++) 
542                         {
543                                 op_type op = (this_try->args.nib[i]) & ~(B30|B31);
544                                 switch (op) 
545                                     {
546                                     case Hex0:
547                                     case Hex1:
548                                     case Hex2:
549                                     case Hex3:
550                                     case Hex4:
551                                     case Hex5:
552                                     case Hex6:
553                                     case Hex7:
554                                     case Hex8:
555                                     case Hex9:
556                                     case HexA:
557                                     case HexB:
558                                     case HexC:
559                                     case HexD:
560                                     case HexE:
561                                     case HexF:
562                                             break;
563                                     case DISPSRC:
564                                     case DISPDST:
565                                             operands[0].dispreg = operands[i].reg; 
566                                     case RD8:
567                                     case RS8:
568                                     case RDIND:
569                                     case RSIND:
570                                     case RD16:
571                                     case RS16:
572                                     case CCR:
573                                     case RSINC:
574                                     case RDDEC:
575                                             if (operands[i].mode != op) goto fail;
576                                             break;
577                                     case KBIT:
578                                     case IMM16:
579                                     case IMM3:
580                                     case IMM8:
581                                             if (operands[i].mode != IMM16) goto fail;
582                                             break;
583                                     case MEMIND:
584                                             if (operands[i].mode != MEMIND) goto fail;
585                                             break;
586                                     case ABS16SRC:
587                                     case ABS8SRC:
588                                     case ABS16OR8SRC:
589                                     case ABS16ORREL8SRC:
590                                             
591                                             if (operands[i].mode != ABS16SRC) goto fail;
592                                             break;
593                                     case ABS16OR8DST:
594                                     case ABS16DST:
595                                     case ABS8DST:
596                                             if (operands[i].mode != ABS16DST) goto fail;
597                                             break;
598                                     }
599                         }
600                     found =1;
601             fail: ;
602             }
603         if (found) 
604             return this_try;
605         else 
606             return 0;
607 }
608
609 static void
610     DEFUN(check_operand,(operand, width, string),
611           struct h8_op *operand AND
612           unsigned int width AND
613           char *string)
614 {
615   if (operand->exp.X_add_symbol == 0 
616       && operand->exp.X_subtract_symbol == 0)
617   {
618                     
619     /* No symbol involved, let's look at offset, it's dangerous if any of
620        the high bits are not 0 or ff's, find out by oring or anding with
621        the width and seeing if the answer is 0 or all fs*/
622     if ((operand->exp.X_add_number & ~width) != 0 &&
623         (operand->exp.X_add_number | width)!= (~0))
624     {
625       as_warn("operand %s0x%x out of range.", string, operand->exp.X_add_number);
626     }
627   }
628         
629 }
630
631 /* Now we know what sort of opcodes it is, lets build the bytes -
632  */
633 static void 
634     DEFUN (build_bytes,(this_try, operand),
635            struct h8_opcode *this_try AND
636            struct h8_op *operand)
637
638 {
639         unsigned int i;
640         
641         char *output = frag_more(this_try->length);
642         char *output_ptr = output;
643         op_type *nibble_ptr = this_try->data.nib;
644         char part;
645         op_type c;
646         char high;
647         int nib;
648  top: ;
649         while (*nibble_ptr != E) 
650             {
651                     int nibble;
652                     for (nibble = 0; nibble <2; nibble++) 
653                         {
654                                 c = *nibble_ptr & ~(B30|B31);
655                                 switch (c) 
656                                     {
657                                     default:
658                                             abort();
659                                     case KBIT:
660                                             switch  (operand[0].exp.X_add_number) 
661                                                 {
662                                                 case 1:
663                                                         nib = 0;
664                                                         break;
665                                                 case 2:
666                                                         nib = 8;
667                                                         break;
668                                                 default:
669                                                         as_bad("Need #1 or #2 here");
670                                                         break;
671                                                 }
672                                             /* stop it making a fix */
673                                             operand[0].mode = 0;
674                                             break;
675                                     case 0:
676                                     case 1:
677                                     case 2: case 3: case 4: case 5: case  6:
678                                     case 7: case 8: case 9: case 10: case 11: 
679                                     case  12: case 13: case 14: case 15:
680                                             nib = c;
681                                             break;
682                                     case DISPREG:
683                                             nib = operand[0].dispreg;
684                                             break;
685                                     case IMM8:
686                                             operand[0].mode = IMM8;
687                                             nib = 0;
688                                             break;
689                                             
690                                     case DISPDST:
691                                             nib = 0;
692                                             break;
693                                     case IMM3: 
694                                             if (operand[0].exp.X_add_symbol == 0) {
695                                                     operand[0].mode = 0; /* stop it making a fix */
696                                                     nib =  (operand[0].exp.X_add_number);
697                                             }
698                                             else as_bad("can't have symbol for bit number");
699                                             if (nib < 0 || nib > 7) 
700                                                 {
701                                                         as_bad("Bit number out of range %d", nib);
702                                                 }
703                                             
704                                             break;
705                                             
706                                     case ABS16DST:
707                                             nib = 0;
708                                             break;
709                                     case ABS8DST:
710                                             operand[1].mode = ABS8DST;
711                                             nib = 0;
712                                             break;
713                                     case ABS8SRC:
714                                             operand[0].mode = ABS8SRC;
715                                             nib = 0;
716                                             break;
717                                     case ABS16OR8DST:
718                                             operand[1].mode = c;
719                                             
720                                             nib = 0;
721                                             
722                                             break;
723                                             
724                                     case ABS16ORREL8SRC:
725                                             operand[0].mode = c;
726                                             nib=0;
727                                             break;
728                                             
729                                     case ABS16OR8SRC:
730                                             operand[0].mode = ABS16OR8SRC;
731                                             nib = 0;
732                                             break;
733                                     case DISPSRC:                   
734                                             operand[0].mode = ABS16SRC;
735                                             nib = 0;
736                                             break;
737                                             
738                                     case DISP8:
739                                             operand[0].mode = DISP8;
740                                             nib = 0;
741                                             break;
742                                             
743                                     case ABS16SRC:
744                                     case IMM16:
745                                     case IGNORE:
746                                     case MEMIND:
747                                             
748                                             nib=0;
749                                             break;
750                                     case RS8:
751                                     case RS16:
752                                     case RSIND:
753                                     case RSINC:
754                                             nib =  operand[0].reg;
755                                             break;
756                                             
757                                     case RD8:
758                                     case RD16:
759                                     case RDDEC:
760                                     case RDIND:
761                                             nib  = operand[1].reg;
762                                             break;
763                                             
764                                     case E: 
765                                             abort();
766                                             break;
767                                     }
768                                 if (*nibble_ptr & B31) {
769                                         nib |=0x8;
770                                 }
771                                 
772                                 if (nibble == 0) {
773                                         *output_ptr = nib << 4;
774                                 }
775                                 else {
776                                         *output_ptr |= nib;
777                                         output_ptr++;
778                                 }
779                                 nibble_ptr++;
780                         }
781                     
782             }
783         
784         /* output any fixes */
785         for (i = 0; i < 2; i++) 
786             {
787                     switch (operand[i].mode) {
788                     case 0:
789                             break;
790                             
791                     case DISP8:
792                             check_operand(operand+i, 0x7f,"@");
793
794                             if (operand[i].exp.X_add_number & 1) {
795                               as_warn("branch operand has odd offset (%x)\n",
796                                       operand->exp.X_add_number);
797                             }
798                             
799                             fix_new(frag_now,
800                                     output - frag_now->fr_literal + 1, 
801                                     1,
802                                     operand[i].exp.X_add_symbol,
803                                     operand[i].exp.X_subtract_symbol,
804                                     operand[i].exp.X_add_number -1,
805                                     1,
806                                     R_PCRBYTE);
807                             break;
808                     case IMM8:
809                             check_operand(operand+i, 0xff,"#");
810                             /* If there is nothing else going on we can safely
811                                reloc in place */
812                             if (operand[i].exp.X_add_symbol == 0) 
813                                 {
814                                         output[1] = operand[i].exp.X_add_number;
815                                 }
816                             else 
817                                 {
818                                         fix_new(frag_now,
819                                                 output - frag_now->fr_literal + 1, 
820                                                 1,
821                                                 operand[i].exp.X_add_symbol,
822                                                 operand[i].exp.X_subtract_symbol,
823                                                 operand[i].exp.X_add_number,
824                                                 0,
825                                                 R_RELBYTE);
826                                 }
827                             
828                             break;
829                     case MEMIND:
830                             check_operand(operand+i, 0xff,"@@");
831                             fix_new(frag_now,
832                                     output - frag_now->fr_literal + 1, 
833                                     1,
834                                     operand[i].exp.X_add_symbol,
835                                     operand[i].exp.X_subtract_symbol,
836                                     operand[i].exp.X_add_number,
837                                     0,
838                                     R_RELBYTE);
839                             break;
840                     case ABS8DST:
841                     case ABS8SRC:
842                             check_operand(operand+i, 0xff,"@");
843                             fix_new(frag_now,
844                                     output - frag_now->fr_literal + 1, 
845                                     1,
846                                     operand[i].exp.X_add_symbol,
847                                     operand[i].exp.X_subtract_symbol,
848                                     operand[i].exp.X_add_number,
849                                     0,
850                                     R_RELBYTE);
851                             break;
852                             
853                     case ABS16OR8SRC:           
854                     case ABS16OR8DST:
855                             check_operand(operand+i, 0xffff,"@");
856                             
857                             fix_new(frag_now,
858                                     output - frag_now->fr_literal + 2, 
859                                     2,
860                                     operand[i].exp.X_add_symbol,
861                                     operand[i].exp.X_subtract_symbol,
862                                     operand[i].exp.X_add_number,
863                                     0,
864                                     R_MOVB1);
865                             break;
866                             
867                     case ABS16ORREL8SRC:                
868                             check_operand(operand+i, 0xffff,"@");
869                             if (operand[i].exp.X_add_number & 1) {
870                               as_warn("branch operand has odd offset (%x)\n",
871                                       operand->exp.X_add_number);
872                             }
873                             fix_new(frag_now,
874                                     output - frag_now->fr_literal + 2, 
875                                     2,
876                                     operand[i].exp.X_add_symbol,
877                                     operand[i].exp.X_subtract_symbol,
878                                     operand[i].exp.X_add_number,
879                                     0,
880                                     R_JMP1);
881                             break;
882                             
883                             
884                     case ABS16SRC:
885                     case ABS16DST:
886                     case IMM16:
887                     case DISPSRC:
888                     case DISPDST:
889                             check_operand(operand+i, 0xffff,"@");
890                             if (operand[i].exp.X_add_symbol == 0) 
891                                 {
892                                         /* This should be done with bfd */
893                                         output[3] = operand[i].exp.X_add_number & 0xff;   
894                                         output[2] = operand[i].exp.X_add_number >> 8;
895                                         
896                                 }
897                             else 
898                                 {
899                                         
900                                         fix_new(frag_now,
901                                                 output - frag_now->fr_literal + 2, 
902                                                 2,
903                                                 operand[i].exp.X_add_symbol,
904                                                 operand[i].exp.X_subtract_symbol,
905                                                 operand[i].exp.X_add_number,
906                                                 0,
907                                                 R_RELWORD);
908                                 }
909                             
910                             break;
911                     case RS8:
912                     case RD8:
913                     case RS16:
914                     case RD16:
915                     case RDDEC: 
916                     case KBIT:
917                     case RSINC:
918                     case RDIND:
919                     case RSIND:
920                     case CCR:
921                             
922                             break;
923                     default:
924                             abort();
925                     }
926             }
927         
928 }
929 /*
930   try and give an intelligent error message for common and simple to
931   detect errors
932   */
933
934 static void
935     DEFUN(clever_message, (opcode, operand),
936           struct h8_opcode *opcode AND
937           struct h8_op *operand)
938 {
939   struct h8_opcode *scan = opcode;
940         
941   /* Find out if there was more than one possible opccode */  
942         
943   if ((opcode+1)->idx != opcode->idx) 
944   {
945     unsigned int argn;
946                     
947     /* Only one opcode of this flavour, try and guess which operand
948        didn't match */
949     for (argn = 0; argn < opcode->noperands; argn++) 
950     {
951       switch (opcode->args.nib[argn]) 
952       {
953       case RD16:
954         if (operand[argn].mode != RD16)        
955         {
956           as_bad("destination operand must be 16 bit register");          
957           return;
958                                                         
959         }
960         break;
961                                             
962       case RS8:
963                                             
964         if (operand[argn].mode != RS8) 
965         {
966           as_bad("source operand must be 8 bit register");
967           return;
968         }
969         break;
970                                             
971       case ABS16DST:
972         if (operand[argn].mode != ABS16DST) 
973         {
974           as_bad("destination operand must be 16bit absolute address");
975           return;
976         }
977 break;                                      
978       case RD8:
979         if (operand[argn].mode != RD8) 
980         {
981           as_bad("destination operand must be 8 bit register");  
982         return;
983         }
984         break;
985         
986                                             
987       case ABS16SRC:
988         if (operand[argn].mode != ABS16SRC) 
989         {
990           as_bad("source operand must be 16bit absolute address");
991           return;
992         }
993         break;
994         
995       }
996     }
997   }
998   as_bad("invalid operands");
999 }
1000
1001 /* This is the guts of the machine-dependent assembler.  STR points to a
1002    machine dependent instruction.  This funciton is supposed to emit
1003    the frags/bytes it assembles to.
1004    */
1005
1006
1007
1008 void 
1009     DEFUN(md_assemble,(str),
1010           char *str)
1011 {
1012         char *op_start;
1013         char *op_end;
1014         unsigned int i;
1015         struct       h8_op operand[2];  
1016         struct h8_opcode * opcode;
1017         struct h8_opcode * prev_opcode;
1018         
1019         char *dot = 0;
1020         char c;    
1021         /* Drop leading whitespace */
1022         while (*str == ' ')
1023             str++;
1024         
1025         /* find the op code end */
1026         for (op_start = op_end = str;
1027              *op_end != 0 && *op_end != ' ';
1028              op_end ++) 
1029             {
1030                     if (*op_end == '.') {
1031                             dot = op_end+1;
1032                             *op_end = 0;
1033                             op_end+=2;
1034                             break;
1035                     }
1036             }
1037         
1038         ;
1039         
1040         if (op_end == op_start) 
1041             {
1042                     as_bad("can't find opcode ");
1043             }
1044         c = *op_end;
1045         
1046         *op_end = 0;
1047         
1048         opcode = (struct h8_opcode *) hash_find(opcode_hash_control,
1049                                                 op_start);
1050         
1051         if (opcode == NULL) 
1052             {
1053                     as_bad("unknown opcode");
1054                     return;
1055             }
1056         
1057         
1058         input_line_pointer =   get_operands(opcode->noperands, op_end,
1059                                             operand);
1060         *op_end = c;
1061         prev_opcode = opcode;
1062         
1063         opcode = get_specific(opcode,  operand);
1064         
1065         if (opcode == 0)  
1066             {
1067                     /* Couldn't find an opcode which matched the operands */
1068                     char *where =frag_more(2);
1069                     where[0] = 0x0;
1070                     where[1] = 0x0;
1071                     clever_message(prev_opcode, operand);
1072                     
1073                     return;
1074             }
1075         if (opcode->size && dot) 
1076             {
1077                     if (opcode->size != *dot) 
1078                         {
1079                                 as_warn("mismatch between opcode size and operand size");
1080                         }
1081             }
1082         
1083         build_bytes(opcode, operand);
1084         
1085 }
1086
1087 void 
1088     DEFUN(tc_crawl_symbol_chain, (headers),
1089           object_headers *headers)
1090 {
1091         printf("call to tc_crawl_symbol_chain \n");
1092 }
1093
1094 symbolS *DEFUN(md_undefined_symbol,(name),
1095                char *name)
1096 {
1097         return 0;
1098 }
1099
1100 void 
1101     DEFUN(tc_headers_hook,(headers),
1102           object_headers *headers)
1103 {
1104         printf("call to tc_headers_hook \n"); 
1105 }
1106 void
1107     DEFUN_VOID(md_end) 
1108 {
1109 }
1110
1111 /* Various routines to kill one day */
1112 /* Equal to MAX_PRECISION in atof-ieee.c */
1113 #define MAX_LITTLENUMS 6
1114
1115 /* Turn a string in input_line_pointer into a floating point constant of type
1116    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1117    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1118    */
1119 char *
1120     md_atof(type,litP,sizeP)
1121 char type;
1122 char *litP;
1123 int *sizeP;
1124 {
1125         int     prec;
1126         LITTLENUM_TYPE words[MAX_LITTLENUMS];
1127         LITTLENUM_TYPE *wordP;
1128         char    *t;
1129         char    *atof_ieee();
1130         
1131         switch(type) {
1132         case 'f':
1133         case 'F':
1134         case 's':
1135         case 'S':
1136                 prec = 2;
1137                 break;
1138                 
1139         case 'd':
1140         case 'D':
1141         case 'r':
1142         case 'R':
1143                 prec = 4;
1144                 break;
1145                 
1146         case 'x':
1147         case 'X':
1148                 prec = 6;
1149                 break;
1150                 
1151         case 'p':
1152         case 'P':
1153                 prec = 6;
1154                 break;
1155                 
1156         default:
1157                 *sizeP=0;
1158                 return "Bad call to MD_ATOF()";
1159         }
1160         t=atof_ieee(input_line_pointer,type,words);
1161         if(t)
1162             input_line_pointer=t;
1163         
1164         *sizeP=prec * sizeof(LITTLENUM_TYPE);
1165         for(wordP=words;prec--;) {
1166                 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
1167                 litP+=sizeof(LITTLENUM_TYPE);
1168         }
1169         return "";      /* Someone should teach Dean about null pointers */
1170 }
1171
1172 int
1173     md_parse_option(argP, cntP, vecP)
1174 char **argP;
1175 int *cntP;
1176 char ***vecP;
1177
1178 {
1179         return 0;
1180         
1181 }
1182
1183 int md_short_jump_size;
1184
1185 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
1186                                abort(); }
1187 void md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
1188 char *ptr;
1189 long from_addr;
1190 long to_addr;
1191 fragS *frag;
1192 symbolS *to_symbol;
1193 {
1194         as_fatal("failed sanity check.");
1195 }
1196
1197 void
1198     md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
1199 char *ptr;
1200 long from_addr, to_addr;
1201 fragS *frag;
1202 symbolS *to_symbol;
1203 {
1204         as_fatal("failed sanity check.");
1205 }
1206
1207 void
1208     md_convert_frag(headers, fragP)
1209 object_headers *headers;
1210 fragS * fragP;
1211
1212 { printf("call to md_convert_frag \n"); abort(); }
1213
1214 long
1215     DEFUN(md_section_align,(seg, size),
1216           segT seg AND
1217           long size)
1218 {
1219         return((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1220         
1221 }
1222
1223 void
1224     md_apply_fix(fixP, val)
1225 fixS *fixP;
1226 long val;
1227 {
1228         char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1229         
1230         switch(fixP->fx_size) {
1231         case 1:
1232                 *buf++=val;
1233                 break;
1234         case 2:
1235                 *buf++=(val>>8);
1236                 *buf++=val;
1237                 break;
1238         case 4:
1239                 *buf++=(val>>24);
1240                 *buf++=(val>>16);
1241                 *buf++=(val>>8);
1242                 *buf++=val;
1243                 break;
1244         default:
1245                 abort();
1246                 
1247         }
1248 }
1249
1250 void DEFUN(md_operand, (expressionP),expressionS *expressionP) 
1251 { }
1252
1253 int  md_long_jump_size;
1254 int
1255     md_estimate_size_before_relax(fragP, segment_type)
1256 register fragS *fragP;
1257 register segT segment_type;
1258
1259         printf("call tomd_estimate_size_before_relax \n"); abort(); }
1260 /* Put number into target byte order */
1261
1262 void DEFUN(md_number_to_chars,(ptr, use, nbytes),
1263            char *ptr AND
1264            long use AND
1265            int nbytes)
1266 {
1267         switch (nbytes) {
1268         case 4: *ptr++ = (use >> 24) & 0xff;
1269         case 3: *ptr++ = (use >> 16) & 0xff;
1270         case 2: *ptr++ = (use >> 8) & 0xff;
1271         case 1: *ptr++ = (use >> 0) & 0xff;
1272                 break;
1273         default:
1274                 abort();
1275         }
1276 }
1277 long md_pcrel_from(fixP) 
1278 fixS *fixP; { abort(); }
1279
1280 void tc_coff_symbol_emit_hook() { }
1281
1282
1283 void tc_reloc_mangle(fix_ptr, intr, base)
1284 fixS *fix_ptr;
1285 struct internal_reloc *intr;
1286 bfd_vma base;
1287
1288 {
1289         symbolS *symbol_ptr;
1290         
1291         symbol_ptr = fix_ptr->fx_addsy;
1292         
1293         /* If this relocation is attached to a symbol then it's ok
1294            to output it */
1295         if (fix_ptr->fx_r_type == RELOC_32) {
1296                 /* cons likes to create reloc32's whatever the size of the reloc..
1297                  */
1298                 switch (fix_ptr->fx_size) 
1299                     {
1300                             
1301                     case 2:
1302                             intr->r_type = R_RELWORD;
1303                             break;
1304                     case 1:
1305                             intr->r_type = R_RELBYTE;
1306                             break;
1307                     default:
1308                             abort();
1309                             
1310                     }
1311                 
1312         }
1313         else {  
1314                 intr->r_type = fix_ptr->fx_r_type;
1315         }
1316         
1317         intr->r_vaddr = fix_ptr->fx_frag->fr_address +  fix_ptr->fx_where  +base;
1318         intr->r_offset = fix_ptr->fx_offset;
1319         
1320         if (symbol_ptr)
1321             intr->r_symndx = symbol_ptr->sy_number;
1322         else
1323             intr->r_symndx = -1;
1324         
1325         
1326 }
1327
1328 /* end of tc-h8300.c */