Fix more fallout from multi-pass relaxation patch.
[external/binutils.git] / gas / config / tc-h8500.c
1 /* tc-h8500.c -- Assemble code for the Hitachi H8/500
2    Copyright 1993, 1994, 1995, 1998, 2000, 2001
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>.  */
23
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #define ASSEMBLER_TABLE
30 #include "opcodes/h8500-opc.h"
31 #include <ctype.h>
32
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
36
37 /* This table describes all the machine specific pseudo-ops the assembler
38    has to support.  The fields are:
39    pseudo-op name without dot
40    function to call to execute this pseudo-op
41    Integer arg to pass to the function
42    */
43
44 void cons ();
45
46 const pseudo_typeS md_pseudo_table[] =
47 {
48   {"int", cons, 2},
49   {"data.b", cons, 1},
50   {"data.w", cons, 2},
51   {"data.l", cons, 4},
52   {"form", listing_psize, 0},
53   {"heading", listing_title, 0},
54   {"import", s_ignore, 0},
55   {"page", listing_eject, 0},
56   {"program", s_ignore, 0},
57   {0, 0, 0}
58 };
59
60 const int md_reloc_size;
61
62 const char EXP_CHARS[] = "eE";
63
64 /* Chars that mean this number is a floating point constant */
65 /* As in 0f12.456 */
66 /* or    0d1.2345e12 */
67 const char FLT_CHARS[] = "rRsSfFdDxXpP";
68
69 #define C(a,b) ENCODE_RELAX(a,b)
70 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
71
72 #define GET_WHAT(x) ((x>>2))
73
74 #define BYTE_DISP 1
75 #define WORD_DISP 2
76 #define UNDEF_BYTE_DISP 0
77 #define UNDEF_WORD_DISP 3
78
79 #define BRANCH  1
80 #define SCB_F   2
81 #define SCB_TST 3
82 #define END 4
83
84 #define BYTE_F 127
85 #define BYTE_B -126
86 #define WORD_F 32767
87 #define WORD_B 32768
88
89 relax_typeS md_relax_table[C (END, 0)] = {
90   { 0, 0, 0, 0 },
91   { 0, 0, 0, 0 },
92   { 0, 0, 0, 0 },
93   { 0, 0, 0, 0 },
94
95   /* BRANCH */
96   { 0,      0,       0, 0 },
97   { BYTE_F, BYTE_B,  2, C (BRANCH, WORD_DISP) },
98   { WORD_F, WORD_B,  3, 0 },
99   { 0,      0,       3, 0 },
100
101   /* SCB_F */
102   { 0,      0,       0, 0 },
103   { BYTE_F, BYTE_B,  3, C (SCB_F, WORD_DISP) },
104   { WORD_F, WORD_B,  8, 0 },
105   { 0,      0,       8, 0 },
106
107   /* SCB_TST */
108   { 0,      0,       0, 0 },
109   { BYTE_F, BYTE_B,  3, C (SCB_TST, WORD_DISP) },
110   { WORD_F, WORD_B, 10, 0 },
111   { 0,      0,      10, 0 }
112
113 };
114
115 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
116
117 /*
118   This function is called once, at assembler startup time.  This should
119   set up all the tables, etc. that the MD part of the assembler needs
120   */
121
122 void
123 md_begin ()
124 {
125   h8500_opcode_info *opcode;
126   char prev_buffer[100];
127   int idx = 0;
128
129   opcode_hash_control = hash_new ();
130   prev_buffer[0] = 0;
131
132   /* Insert unique names into hash table */
133   for (opcode = h8500_table; opcode->name; opcode++)
134     {
135       if (idx != opcode->idx)
136         {
137           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
138           idx++;
139         }
140     }
141 }
142
143 static int rn;                  /* register number used by RN */
144 static int rs;                  /* register number used by RS */
145 static int rd;                  /* register number used by RD */
146 static int crb;                 /* byte size cr */
147 static int crw;                 /* word sized cr */
148 static int cr;                  /* unknown size cr */
149
150 static expressionS displacement;/* displacement expression */
151 static int displacement_size;   /* and size if given */
152
153 static int immediate_inpage;
154 static expressionS immediate;   /* immediate expression */
155 static int immediate_size;      /* and size if given */
156
157 static expressionS absolute;    /* absolute expression */
158 static int absolute_size;       /* and size if given */
159
160 typedef struct
161 {
162   int type;
163   int reg;
164   expressionS exp;
165   int page;
166 }
167
168 h8500_operand_info;
169
170 /* Try to parse a reg name.  Return the number of chars consumed.  */
171
172 static int
173 parse_reg (src, mode, reg)
174      char *src;
175      int *mode;
176      int *reg;
177 {
178   char *end;
179   int len;
180
181   /* Cribbed from get_symbol_end().  */
182   if (!is_name_beginner (*src) || *src == '\001')
183     return 0;
184   end = src + 1;
185   while (is_part_of_name (*end) || *end == '\001')
186     end++;
187   len = end - src;
188
189   if (len == 2 && src[0] == 'r')
190     {
191       if (src[1] >= '0' && src[1] <= '7')
192         {
193           *mode = RN;
194           *reg = (src[1] - '0');
195           return len;
196         }
197     }
198   if (len == 2 && src[0] == 's' && src[1] == 'p')
199     {
200       *mode = RN;
201       *reg = 7;
202       return len;
203     }
204   if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
205     {
206       *mode = CRB;
207       *reg = 1;
208       return len;
209     }
210   if (len == 2 && src[0] == 's' && src[1] == 'r')
211     {
212       *mode = CRW;
213       *reg = 0;
214       return len;
215     }
216   if (len == 2 && src[0] == 'b' && src[1] == 'r')
217     {
218       *mode = CRB;
219       *reg = 3;
220       return len;
221     }
222   if (len == 2 && src[0] == 'e' && src[1] == 'p')
223     {
224       *mode = CRB;
225       *reg = 4;
226       return len;
227     }
228   if (len == 2 && src[0] == 'd' && src[1] == 'p')
229     {
230       *mode = CRB;
231       *reg = 5;
232       return len;
233     }
234   if (len == 2 && src[0] == 't' && src[1] == 'p')
235     {
236       *mode = CRB;
237       *reg = 7;
238       return len;
239     }
240   if (len == 2 && src[0] == 'f' && src[1] == 'p')
241     {
242       *mode = RN;
243       *reg = 6;
244       return len;
245     }
246   return 0;
247 }
248
249 static char *
250 parse_exp (s, op, page)
251      char *s;
252      expressionS *op;
253      int *page;
254 {
255   char *save;
256   char *new;
257
258   save = input_line_pointer;
259
260   *page = 0;
261   if (s[0] == '%')
262     {
263       if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
264         {
265           s += 5;
266           *page = 'p';
267         }
268       if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
269         {
270           s += 5;
271           *page = 'h';
272         }
273       else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
274         {
275           s += 4;
276           *page = 'o';
277         }
278     }
279
280   input_line_pointer = s;
281
282   expression (op);
283   if (op->X_op == O_absent)
284     as_bad (_("missing operand"));
285   new = input_line_pointer;
286   input_line_pointer = save;
287   return new;
288 }
289
290 typedef enum
291   {
292     exp_signed, exp_unsigned, exp_sandu
293   } sign_type;
294
295 static char *
296 skip_colonthing (sign, ptr, exp, def, size8, size16, size24)
297      sign_type sign;
298      char *ptr;
299      h8500_operand_info *exp;
300      int def;
301      int size8;
302      int size16;
303      int size24;
304 {
305   ptr = parse_exp (ptr, &exp->exp, &exp->page);
306   if (*ptr == ':')
307     {
308       ptr++;
309       if (*ptr == '8')
310         {
311           ptr++;
312           exp->type = size8;
313         }
314       else if (ptr[0] == '1' & ptr[1] == '6')
315         {
316           ptr += 2;
317           exp->type = size16;
318         }
319       else if (ptr[0] == '2' & ptr[1] == '4')
320         {
321           if (!size24)
322             {
323               as_bad (_(":24 not valid for this opcode"));
324             }
325           ptr += 2;
326           exp->type = size24;
327         }
328       else
329         {
330           as_bad (_("expect :8,:16 or :24"));
331           exp->type = size16;
332         }
333     }
334   else
335     {
336       if (exp->page == 'p')
337         {
338           exp->type = IMM8;
339         }
340       else if (exp->page == 'h')
341         {
342           exp->type = IMM16;
343         }
344       else
345         {
346           /* Let's work out the size from the context */
347           int n = exp->exp.X_add_number;
348           if (size8
349               && exp->exp.X_op == O_constant
350               && ((sign == exp_signed && (n >= -128 && n <= 127))
351                   || (sign == exp_unsigned && (n >= 0 && (n <= 255)))
352                   || (sign == exp_sandu && (n >= -128 && (n <= 255)))))
353             {
354               exp->type = size8;
355             }
356           else
357             {
358               exp->type = def;
359             }
360         }
361     }
362   return ptr;
363 }
364
365 static int
366 parse_reglist (src, op)
367      char *src;
368      h8500_operand_info *op;
369 {
370   int mode;
371   int rn;
372   int mask = 0;
373   int rm;
374   int idx = 1;                  /* skip ( */
375
376   while (src[idx] && src[idx] != ')')
377     {
378       int done = parse_reg (src + idx, &mode, &rn);
379
380       if (done)
381         {
382           idx += done;
383           mask |= 1 << rn;
384         }
385       else
386         {
387           as_bad (_("syntax error in reg list"));
388           return 0;
389         }
390       if (src[idx] == '-')
391         {
392           idx++;
393           done = parse_reg (src + idx, &mode, &rm);
394           if (done)
395             {
396               idx += done;
397               while (rn <= rm)
398                 {
399                   mask |= 1 << rn;
400                   rn++;
401                 }
402             }
403           else
404             {
405               as_bad (_("missing final register in range"));
406             }
407         }
408       if (src[idx] == ',')
409         idx++;
410     }
411   idx++;
412   op->exp.X_add_symbol = 0;
413   op->exp.X_op_symbol = 0;
414   op->exp.X_add_number = mask;
415   op->exp.X_op = O_constant;
416   op->exp.X_unsigned = 1;
417   op->type = IMM8;
418   return idx;
419
420 }
421
422 /* The many forms of operand:
423
424    Rn                   Register direct
425    @Rn                  Register indirect
426    @(disp[:size], Rn)   Register indirect with displacement
427    @Rn+
428    @-Rn
429    @aa[:size]           absolute
430    #xx[:size]           immediate data
431
432    */
433
434 static void
435 get_operand (ptr, op, ispage)
436      char **ptr;
437      h8500_operand_info *op;
438      char ispage;
439 {
440   char *src = *ptr;
441   int mode;
442   unsigned int num;
443   unsigned int len;
444   op->page = 0;
445   if (src[0] == '(' && src[1] == 'r')
446     {
447       /* This is a register list */
448       *ptr = src + parse_reglist (src, op);
449       return;
450     }
451
452   len = parse_reg (src, &op->type, &op->reg);
453
454   if (len)
455     {
456       *ptr = src + len;
457       return;
458     }
459
460   if (*src == '@')
461     {
462       src++;
463       if (*src == '-')
464         {
465           src++;
466           len = parse_reg (src, &mode, &num);
467           if (len == 0)
468             {
469               /* Oops, not a reg after all, must be ordinary exp */
470               src--;
471               /* must be a symbol */
472               *ptr = skip_colonthing (exp_unsigned, src,
473                                       op, ABS16, ABS8, ABS16, ABS24);
474               return;
475             }
476
477           op->type = RNDEC;
478           op->reg = num;
479           *ptr = src + len;
480           return;
481         }
482       if (*src == '(')
483         {
484           /* Disp */
485           src++;
486
487           src = skip_colonthing (exp_signed, src,
488                                  op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
489
490           if (*src != ',')
491             {
492               as_bad (_("expected @(exp, Rn)"));
493               return;
494             }
495           src++;
496           len = parse_reg (src, &mode, &op->reg);
497           if (len == 0 || mode != RN)
498             {
499               as_bad (_("expected @(exp, Rn)"));
500               return;
501             }
502           src += len;
503           if (*src != ')')
504             {
505               as_bad (_("expected @(exp, Rn)"));
506               return;
507             }
508           *ptr = src + 1;
509           return;
510         }
511       len = parse_reg (src, &mode, &num);
512
513       if (len)
514         {
515           src += len;
516           if (*src == '+')
517             {
518               src++;
519               if (mode != RN)
520                 {
521                   as_bad (_("@Rn+ needs word register"));
522                   return;
523                 }
524               op->type = RNINC;
525               op->reg = num;
526               *ptr = src;
527               return;
528             }
529           if (mode != RN)
530             {
531               as_bad (_("@Rn needs word register"));
532               return;
533             }
534           op->type = RNIND;
535           op->reg = num;
536           *ptr = src;
537           return;
538         }
539       else
540         {
541           /* must be a symbol */
542           *ptr =
543             skip_colonthing (exp_unsigned, src, op,
544                              ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
545           return;
546         }
547     }
548
549   if (*src == '#')
550     {
551       src++;
552       *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
553       return;
554     }
555   else
556     {
557       *ptr = skip_colonthing (exp_signed, src, op,
558                               ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
559     }
560 }
561
562 static char *
563 get_operands (info, args, operand)
564      h8500_opcode_info *info;
565      char *args;
566      h8500_operand_info *operand;
567
568 {
569   char *ptr = args;
570
571   switch (info->nargs)
572     {
573     case 0:
574       operand[0].type = 0;
575       operand[1].type = 0;
576       break;
577
578     case 1:
579       ptr++;
580       get_operand (&ptr, operand + 0, info->name[0] == 'p');
581       operand[1].type = 0;
582       break;
583
584     case 2:
585       ptr++;
586       get_operand (&ptr, operand + 0, 0);
587       if (*ptr == ',')
588         ptr++;
589       get_operand (&ptr, operand + 1, 0);
590       break;
591
592     default:
593       abort ();
594     }
595
596   return ptr;
597 }
598
599 /* Passed a pointer to a list of opcodes which use different
600    addressing modes, return the opcode which matches the opcodes
601    provided.  */
602
603 int pcrel8;                     /* Set when we've seen a pcrel operand */
604
605 static h8500_opcode_info *
606 get_specific (opcode, operands)
607      h8500_opcode_info *opcode;
608      h8500_operand_info *operands;
609 {
610   h8500_opcode_info *this_try = opcode;
611   int found = 0;
612   unsigned int noperands = opcode->nargs;
613
614   unsigned int this_index = opcode->idx;
615
616   while (this_index == opcode->idx && !found)
617     {
618       unsigned int i;
619
620       this_try = opcode++;
621
622       /* look at both operands needed by the opcodes and provided by
623        the user*/
624       for (i = 0; i < noperands; i++)
625         {
626           h8500_operand_info *user = operands + i;
627
628           switch (this_try->arg_type[i])
629             {
630             case FPIND_D8:
631               /* Opcode needs (disp:8,fp) */
632               if (user->type == RNIND_D8 && user->reg == 6)
633                 {
634                   displacement = user->exp;
635                   continue;
636                 }
637               break;
638             case RDIND_D16:
639               if (user->type == RNIND_D16)
640                 {
641                   displacement = user->exp;
642                   rd = user->reg;
643                   continue;
644                 }
645               break;
646             case RDIND_D8:
647               if (user->type == RNIND_D8)
648                 {
649                   displacement = user->exp;
650                   rd = user->reg;
651                   continue;
652                 }
653               break;
654             case RNIND_D16:
655             case RNIND_D8:
656               if (user->type == this_try->arg_type[i])
657                 {
658                   displacement = user->exp;
659                   rn = user->reg;
660                   continue;
661                 }
662               break;
663
664             case SPDEC:
665               if (user->type == RNDEC && user->reg == 7)
666                 {
667                   continue;
668                 }
669               break;
670             case SPINC:
671               if (user->type == RNINC && user->reg == 7)
672                 {
673                   continue;
674                 }
675               break;
676             case ABS16:
677               if (user->type == ABS16)
678                 {
679                   absolute = user->exp;
680                   continue;
681                 }
682               break;
683             case ABS8:
684               if (user->type == ABS8)
685                 {
686                   absolute = user->exp;
687                   continue;
688                 }
689               break;
690             case ABS24:
691               if (user->type == ABS24)
692                 {
693                   absolute = user->exp;
694                   continue;
695                 }
696               break;
697
698             case CRB:
699               if ((user->type == CRB || user->type == CR) && user->reg != 0)
700                 {
701                   crb = user->reg;
702                   continue;
703                 }
704               break;
705             case CRW:
706               if ((user->type == CRW || user->type == CR) && user->reg == 0)
707                 {
708                   crw = user->reg;
709                   continue;
710                 }
711               break;
712             case DISP16:
713               if (user->type == DISP16)
714                 {
715                   displacement = user->exp;
716                   continue;
717                 }
718               break;
719             case DISP8:
720               if (user->type == DISP8)
721                 {
722                   displacement = user->exp;
723                   continue;
724                 }
725               break;
726             case FP:
727               if (user->type == RN && user->reg == 6)
728                 {
729                   continue;
730                 }
731               break;
732             case PCREL16:
733               if (user->type == PCREL16)
734                 {
735                   displacement = user->exp;
736                   continue;
737                 }
738               break;
739             case PCREL8:
740               if (user->type == PCREL8)
741                 {
742                   displacement = user->exp;
743                   pcrel8 = 1;
744                   continue;
745                 }
746               break;
747
748             case IMM16:
749               if (user->type == IMM16
750                   || user->type == IMM8)
751                 {
752                   immediate_inpage = user->page;
753                   immediate = user->exp;
754                   continue;
755                 }
756               break;
757             case RLIST:
758             case IMM8:
759               if (user->type == IMM8)
760                 {
761                   immediate_inpage = user->page;
762                   immediate = user->exp;
763                   continue;
764                 }
765               break;
766             case IMM4:
767               if (user->type == IMM8)
768                 {
769                   immediate_inpage = user->page;
770                   immediate = user->exp;
771                   continue;
772                 }
773               break;
774             case QIM:
775               if (user->type == IMM8
776                   && user->exp.X_op == O_constant
777                   &&
778                   (user->exp.X_add_number == -2
779                    || user->exp.X_add_number == -1
780                    || user->exp.X_add_number == 1
781                    || user->exp.X_add_number == 2))
782                 {
783                   immediate_inpage = user->page;
784                   immediate = user->exp;
785                   continue;
786                 }
787               break;
788             case RD:
789               if (user->type == RN)
790                 {
791                   rd = user->reg;
792                   continue;
793                 }
794               break;
795             case RS:
796               if (user->type == RN)
797                 {
798                   rs = user->reg;
799                   continue;
800                 }
801               break;
802             case RDIND:
803               if (user->type == RNIND)
804                 {
805                   rd = user->reg;
806                   continue;
807
808                 }
809               break;
810             case RNINC:
811             case RNIND:
812             case RNDEC:
813             case RN:
814
815               if (user->type == this_try->arg_type[i])
816                 {
817                   rn = user->reg;
818                   continue;
819                 }
820               break;
821             case SP:
822               if (user->type == RN && user->reg == 7)
823                 {
824                   continue;
825                 }
826               break;
827             default:
828               printf (_("unhandled %d\n"), this_try->arg_type[i]);
829               break;
830             }
831
832           /* If we get here this didn't work out */
833           goto fail;
834         }
835       found = 1;
836     fail:;
837
838     }
839
840   if (found)
841     return this_try;
842   else
843     return 0;
844 }
845
846 int
847 check (operand, low, high)
848      expressionS *operand;
849      int low;
850      int high;
851 {
852   if (operand->X_op != O_constant
853       || operand->X_add_number < low
854       || operand->X_add_number > high)
855     {
856       as_bad (_("operand must be absolute in range %d..%d"), low, high);
857     }
858   return operand->X_add_number;
859 }
860
861 static void
862 insert (output, index, exp, reloc, pcrel)
863      char *output;
864      int index;
865      expressionS *exp;
866      int reloc;
867      int pcrel;
868 {
869   fix_new_exp (frag_now,
870                output - frag_now->fr_literal + index,
871                4,               /* always say size is 4, but we know better */
872                exp,
873                pcrel,
874                reloc);
875 }
876
877 void
878 build_relaxable_instruction (opcode, operand)
879      h8500_opcode_info *opcode;
880      h8500_operand_info *operand;
881 {
882   /* All relaxable instructions start life as two bytes but can become
883      three bytes long if a lonely branch and up to 9 bytes if long
884      scb.  */
885   char *p;
886   int len;
887   int type;
888
889   if (opcode->bytes[0].contents == 0x01)
890     {
891       type = SCB_F;
892     }
893   else if (opcode->bytes[0].contents == 0x06
894            || opcode->bytes[0].contents == 0x07)
895     {
896       type = SCB_TST;
897     }
898   else
899     {
900       type = BRANCH;
901     }
902
903   p = frag_var (rs_machine_dependent,
904                 md_relax_table[C (type, WORD_DISP)].rlx_length,
905                 len = md_relax_table[C (type, BYTE_DISP)].rlx_length,
906                 C (type, UNDEF_BYTE_DISP),
907                 displacement.X_add_symbol,
908                 displacement.X_add_number,
909                 0);
910
911   p[0] = opcode->bytes[0].contents;
912   if (type != BRANCH)
913     {
914       p[1] = opcode->bytes[1].contents | rs;
915     }
916 }
917
918 /* Now we know what sort of opcodes it is, let's build the bytes.  */
919
920 static void
921 build_bytes (opcode, operand)
922      h8500_opcode_info *opcode;
923      h8500_operand_info *operand;
924
925 {
926   int index;
927
928   if (pcrel8)
929     {
930       pcrel8 = 0;
931       build_relaxable_instruction (opcode, operand);
932     }
933   else
934     {
935       char *output = frag_more (opcode->length);
936
937       memset (output, 0, opcode->length);
938       for (index = 0; index < opcode->length; index++)
939         {
940           output[index] = opcode->bytes[index].contents;
941
942           switch (opcode->bytes[index].insert)
943             {
944             default:
945               printf (_("failed for %d\n"), opcode->bytes[index].insert);
946               break;
947             case 0:
948               break;
949             case RN:
950               output[index] |= rn;
951               break;
952             case RD:
953             case RDIND:
954               output[index] |= rd;
955               break;
956             case RS:
957               output[index] |= rs;
958               break;
959             case DISP16:
960               insert (output, index, &displacement, R_H8500_IMM16, 0);
961               index++;
962               break;
963             case DISP8:
964             case FPIND_D8:
965               insert (output, index, &displacement, R_H8500_IMM8, 0);
966               break;
967             case IMM16:
968               {
969                 int p;
970
971                 switch (immediate_inpage)
972                   {
973                   case 'p':
974                     p = R_H8500_HIGH16;
975                     break;
976                   case 'h':
977                     p = R_H8500_HIGH16;
978                     break;
979                   default:
980                     p = R_H8500_IMM16;
981                     break;
982                   }
983                 insert (output, index, &immediate, p, 0);
984               }
985               index++;
986               break;
987             case RLIST:
988             case IMM8:
989               if (immediate_inpage)
990                 insert (output, index, &immediate, R_H8500_HIGH8, 0);
991               else
992                 insert (output, index, &immediate, R_H8500_IMM8, 0);
993               break;
994             case PCREL16:
995               insert (output, index, &displacement, R_H8500_PCREL16, 1);
996               index++;
997               break;
998             case PCREL8:
999               insert (output, index, &displacement, R_H8500_PCREL8, 1);
1000               break;
1001             case IMM4:
1002               output[index] |= check (&immediate, 0, 15);
1003               break;
1004             case CR:
1005               output[index] |= cr;
1006               if (cr == 0)
1007                 output[0] |= 0x8;
1008               else
1009                 output[0] &= ~0x8;
1010               break;
1011             case CRB:
1012               output[index] |= crb;
1013               output[0] &= ~0x8;
1014               break;
1015             case CRW:
1016               output[index] |= crw;
1017               output[0] |= 0x8;
1018               break;
1019             case ABS24:
1020               insert (output, index, &absolute, R_H8500_IMM24, 0);
1021               index += 2;
1022               break;
1023             case ABS16:
1024               insert (output, index, &absolute, R_H8500_IMM16, 0);
1025               index++;
1026               break;
1027             case ABS8:
1028               insert (output, index, &absolute, R_H8500_IMM8, 0);
1029               break;
1030             case QIM:
1031               switch (immediate.X_add_number)
1032                 {
1033                 case -2:
1034                   output[index] |= 0x5;
1035                   break;
1036                 case -1:
1037                   output[index] |= 0x4;
1038                   break;
1039                 case 1:
1040                   output[index] |= 0;
1041                   break;
1042                 case 2:
1043                   output[index] |= 1;
1044                   break;
1045                 }
1046               break;
1047             }
1048         }
1049     }
1050 }
1051
1052 /* This is the guts of the machine-dependent assembler.  STR points to
1053    a machine dependent instruction.  This function is supposed to emit
1054    the frags/bytes it assembles to.  */
1055
1056 void
1057 md_assemble (str)
1058      char *str;
1059 {
1060   char *op_start;
1061   char *op_end;
1062   h8500_operand_info operand[2];
1063   h8500_opcode_info *opcode;
1064   h8500_opcode_info *prev_opcode;
1065   char name[11];
1066
1067   int nlen = 0;
1068
1069   /* Drop leading whitespace.  */
1070   while (*str == ' ')
1071     str++;
1072
1073   /* Find the op code end.  */
1074   for (op_start = op_end = str;
1075        !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
1076        op_end++)
1077     {
1078       if (                      /**op_end != '.'
1079           && *op_end != ':'
1080                                   && */ nlen < 10)
1081         {
1082           name[nlen++] = *op_end;
1083         }
1084     }
1085   name[nlen] = 0;
1086
1087   if (op_end == op_start)
1088     as_bad (_("can't find opcode "));
1089
1090   opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1091
1092   if (opcode == NULL)
1093     {
1094       as_bad (_("unknown opcode"));
1095       return;
1096     }
1097
1098   get_operands (opcode, op_end, operand);
1099   prev_opcode = opcode;
1100
1101   opcode = get_specific (opcode, operand);
1102
1103   if (opcode == 0)
1104     {
1105       /* Couldn't find an opcode which matched the operands */
1106       char *where = frag_more (2);
1107
1108       where[0] = 0x0;
1109       where[1] = 0x0;
1110       as_bad (_("invalid operands for opcode"));
1111       return;
1112     }
1113
1114   build_bytes (opcode, operand);
1115 }
1116
1117 void
1118 tc_crawl_symbol_chain (headers)
1119      object_headers *headers;
1120 {
1121   printf (_("call to tc_crawl_symbol_chain \n"));
1122 }
1123
1124 symbolS *
1125 md_undefined_symbol (name)
1126      char *name;
1127 {
1128   return 0;
1129 }
1130
1131 void
1132 tc_headers_hook (headers)
1133      object_headers *headers;
1134 {
1135   printf (_("call to tc_headers_hook \n"));
1136 }
1137
1138 /* Various routines to kill one day.  */
1139 /* Equal to MAX_PRECISION in atof-ieee.c.  */
1140 #define MAX_LITTLENUMS 6
1141
1142 /* Turn a string in input_line_pointer into a floating point constant
1143    of type type, and store the appropriate bytes in *LITP.  The number
1144    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1145    returned, or NULL on OK.  */
1146
1147 char *
1148 md_atof (type, litP, sizeP)
1149      char type;
1150      char *litP;
1151      int *sizeP;
1152 {
1153   int prec;
1154   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1155   LITTLENUM_TYPE *wordP;
1156   char *t;
1157   char *atof_ieee ();
1158
1159   switch (type)
1160     {
1161     case 'f':
1162     case 'F':
1163     case 's':
1164     case 'S':
1165       prec = 2;
1166       break;
1167
1168     case 'd':
1169     case 'D':
1170     case 'r':
1171     case 'R':
1172       prec = 4;
1173       break;
1174
1175     case 'x':
1176     case 'X':
1177       prec = 6;
1178       break;
1179
1180     case 'p':
1181     case 'P':
1182       prec = 6;
1183       break;
1184
1185     default:
1186       *sizeP = 0;
1187       return _("Bad call to MD_ATOF()");
1188     }
1189   t = atof_ieee (input_line_pointer, type, words);
1190   if (t)
1191     input_line_pointer = t;
1192
1193   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1194   for (wordP = words; prec--;)
1195     {
1196       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1197       litP += sizeof (LITTLENUM_TYPE);
1198     }
1199   return 0;
1200 }
1201 \f
1202 CONST char *md_shortopts = "";
1203 struct option md_longopts[] = {
1204   {NULL, no_argument, NULL, 0}
1205 };
1206 size_t md_longopts_size = sizeof (md_longopts);
1207
1208 int
1209 md_parse_option (c, arg)
1210      int c;
1211      char *arg;
1212 {
1213   return 0;
1214 }
1215
1216 void
1217 md_show_usage (stream)
1218      FILE *stream;
1219 {
1220 }
1221 \f
1222 void
1223 tc_aout_fix_to_chars ()
1224 {
1225   printf (_("call to tc_aout_fix_to_chars \n"));
1226   abort ();
1227 }
1228
1229 static void
1230 wordify_scb (buffer, disp_size, inst_size)
1231      char *buffer;
1232      int *disp_size;
1233      int *inst_size;
1234 {
1235   int rn = buffer[1] & 0x7;
1236
1237   switch (buffer[0])
1238     {
1239     case 0x0e:                  /* BSR */
1240     case 0x20:
1241     case 0x21:
1242     case 0x22:
1243     case 0x23:
1244     case 0x24:
1245     case 0x25:
1246     case 0x26:
1247     case 0x27:
1248     case 0x28:
1249     case 0x29:
1250     case 0x2a:
1251     case 0x2b:
1252     case 0x2c:
1253     case 0x2d:
1254     case 0x2e:
1255     case 0x2f:
1256       buffer[0] |= 0x10;
1257       buffer[1] = 0;
1258       buffer[2] = 0;
1259       *disp_size = 2;
1260       *inst_size = 1;
1261       return;
1262     default:
1263       abort ();
1264
1265     case 0x01:
1266       *inst_size = 6;
1267       *disp_size = 2;
1268       break;
1269     case 0x06:
1270       *inst_size = 8;
1271       *disp_size = 2;
1272
1273       *buffer++ = 0x26;         /* bne + 8 */
1274       *buffer++ = 0x08;
1275       break;
1276     case 0x07:
1277       *inst_size = 8;
1278       *disp_size = 2;
1279       *buffer++ = 0x27;         /* bne + 8 */
1280       *buffer++ = 0x08;
1281       break;
1282
1283     }
1284   *buffer++ = 0xa8 | rn;        /* addq -1,rn */
1285   *buffer++ = 0x0c;
1286   *buffer++ = 0x04;             /* cmp #0xff:8, rn */
1287   *buffer++ = 0xff;
1288   *buffer++ = 0x70 | rn;
1289   *buffer++ = 0x36;             /* bne ...  */
1290   *buffer++ = 0;
1291   *buffer++ = 0;
1292 }
1293
1294 /* Called after relaxing, change the frags so they know how big they
1295    are.  */
1296
1297 void
1298 md_convert_frag (headers, seg, fragP)
1299      object_headers *headers;
1300      segT seg;
1301      fragS *fragP;
1302 {
1303   int disp_size = 0;
1304   int inst_size = 0;
1305   char *buffer = fragP->fr_fix + fragP->fr_literal;
1306
1307   switch (fragP->fr_subtype)
1308     {
1309     case C (BRANCH, BYTE_DISP):
1310       disp_size = 1;
1311       inst_size = 1;
1312       break;
1313
1314     case C (SCB_F, BYTE_DISP):
1315     case C (SCB_TST, BYTE_DISP):
1316       disp_size = 1;
1317       inst_size = 2;
1318       break;
1319
1320       /* Branches to a known 16 bit displacement.  */
1321
1322       /* Turn on the 16bit bit.  */
1323     case C (BRANCH, WORD_DISP):
1324     case C (SCB_F, WORD_DISP):
1325     case C (SCB_TST, WORD_DISP):
1326       wordify_scb (buffer, &disp_size, &inst_size);
1327       break;
1328
1329     case C (BRANCH, UNDEF_WORD_DISP):
1330     case C (SCB_F, UNDEF_WORD_DISP):
1331     case C (SCB_TST, UNDEF_WORD_DISP):
1332       /* This tried to be relaxed, but didn't manage it, it now needs
1333          a fix.  */
1334       wordify_scb (buffer, &disp_size, &inst_size);
1335
1336       /* Make a reloc */
1337       fix_new (fragP,
1338                fragP->fr_fix + inst_size,
1339                4,
1340                fragP->fr_symbol,
1341                fragP->fr_offset,
1342                0,
1343                R_H8500_PCREL16);
1344
1345       fragP->fr_fix += disp_size + inst_size;
1346       return;
1347       break;
1348     default:
1349       abort ();
1350     }
1351   if (inst_size)
1352     {
1353       /* Get the address of the end of the instruction */
1354       int next_inst = fragP->fr_fix + fragP->fr_address + disp_size + inst_size;
1355       int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
1356                        fragP->fr_offset);
1357       int disp = targ_addr - next_inst;
1358
1359       md_number_to_chars (buffer + inst_size, disp, disp_size);
1360       fragP->fr_fix += disp_size + inst_size;
1361     }
1362 }
1363
1364 valueT
1365 md_section_align (seg, size)
1366      segT seg ;
1367      valueT size;
1368 {
1369   return ((size + (1 << section_alignment[(int) seg]) - 1)
1370           & (-1 << section_alignment[(int) seg]));
1371
1372 }
1373
1374 void
1375 md_apply_fix (fixP, val)
1376      fixS *fixP;
1377      long val;
1378 {
1379   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1380
1381   if (fixP->fx_r_type == 0)
1382     {
1383       fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1384     }
1385
1386   switch (fixP->fx_r_type)
1387     {
1388
1389     case R_H8500_IMM8:
1390     case R_H8500_PCREL8:
1391       *buf++ = val;
1392       break;
1393     case R_H8500_IMM16:
1394     case R_H8500_LOW16:
1395     case R_H8500_PCREL16:
1396       *buf++ = (val >> 8);
1397       *buf++ = val;
1398       break;
1399     case R_H8500_HIGH8:
1400       *buf++ = val >> 16;
1401       break;
1402     case R_H8500_HIGH16:
1403       *buf++ = val >> 24;
1404       *buf++ = val >> 16;
1405       break;
1406     case R_H8500_IMM24:
1407       *buf++ = (val >> 16);
1408       *buf++ = (val >> 8);
1409       *buf++ = val;
1410       break;
1411     case R_H8500_IMM32:
1412       *buf++ = (val >> 24);
1413       *buf++ = (val >> 16);
1414       *buf++ = (val >> 8);
1415       *buf++ = val;
1416       break;
1417     default:
1418       abort ();
1419
1420     }
1421 }
1422
1423 /*
1424 called just before address relaxation, return the length
1425 by which a fragment must grow to reach it's destination
1426 */
1427 int
1428 md_estimate_size_before_relax (fragP, segment_type)
1429      register fragS *fragP;
1430      register segT segment_type;
1431 {
1432   int what;
1433
1434   switch (fragP->fr_subtype)
1435     {
1436     default:
1437       abort ();
1438
1439     case C (BRANCH, UNDEF_BYTE_DISP):
1440     case C (SCB_F, UNDEF_BYTE_DISP):
1441     case C (SCB_TST, UNDEF_BYTE_DISP):
1442       what = GET_WHAT (fragP->fr_subtype);
1443       /* used to be a branch to somewhere which was unknown */
1444       if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1445         {
1446           /* Got a symbol and it's defined in this segment, become byte
1447              sized - maybe it will fix up.  */
1448           fragP->fr_subtype = C (what, BYTE_DISP);
1449         }
1450       else
1451         {
1452           /* Its got a segment, but its not ours, so it will always be
1453              long.  */
1454           fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1455         }
1456       break;
1457
1458     case C (BRANCH, BYTE_DISP):
1459     case C (BRANCH, WORD_DISP):
1460     case C (BRANCH, UNDEF_WORD_DISP):
1461     case C (SCB_F, BYTE_DISP):
1462     case C (SCB_F, WORD_DISP):
1463     case C (SCB_F, UNDEF_WORD_DISP):
1464     case C (SCB_TST, BYTE_DISP):
1465     case C (SCB_TST, WORD_DISP):
1466     case C (SCB_TST, UNDEF_WORD_DISP):
1467       /* When relaxing a section for the second time, we don't need to
1468          do anything besides return the current size.  */
1469       break;
1470     }
1471
1472   return md_relax_table[fragP->fr_subtype].rlx_length;
1473 }
1474
1475 /* Put number into target byte order.  */
1476
1477 void
1478 md_number_to_chars (ptr, use, nbytes)
1479      char *ptr;
1480      valueT use;
1481      int nbytes;
1482 {
1483   number_to_chars_bigendian (ptr, use, nbytes);
1484 }
1485
1486 long
1487 md_pcrel_from (fixP)
1488      fixS *fixP;
1489 {
1490   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1491 }
1492
1493 void
1494 tc_coff_symbol_emit_hook (ignore)
1495      symbolS *ignore;
1496 {
1497 }
1498
1499 short
1500 tc_coff_fix2rtype (fix_ptr)
1501      fixS *fix_ptr;
1502 {
1503   if (fix_ptr->fx_r_type == RELOC_32)
1504     {
1505       /* cons likes to create reloc32's whatever the size of the reloc..
1506      */
1507       switch (fix_ptr->fx_size)
1508         {
1509         case 2:
1510           return R_H8500_IMM16;
1511           break;
1512         case 1:
1513           return R_H8500_IMM8;
1514           break;
1515         default:
1516           abort ();
1517         }
1518     }
1519   return fix_ptr->fx_r_type;
1520 }
1521
1522 void
1523 tc_reloc_mangle (fix_ptr, intr, base)
1524      fixS *fix_ptr;
1525      struct internal_reloc *intr;
1526      bfd_vma base;
1527
1528 {
1529   symbolS *symbol_ptr;
1530
1531   symbol_ptr = fix_ptr->fx_addsy;
1532
1533   /* If this relocation is attached to a symbol then it's ok
1534      to output it */
1535   if (fix_ptr->fx_r_type == RELOC_32)
1536     {
1537       /* cons likes to create reloc32's whatever the size of the reloc..
1538        */
1539       switch (fix_ptr->fx_size)
1540         {
1541         case 2:
1542           intr->r_type = R_IMM16;
1543           break;
1544         case 1:
1545           intr->r_type = R_IMM8;
1546           break;
1547         default:
1548           abort ();
1549         }
1550     }
1551   else
1552     {
1553       intr->r_type = fix_ptr->fx_r_type;
1554     }
1555
1556   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1557   intr->r_offset = fix_ptr->fx_offset;
1558
1559   /* Turn the segment of the symbol into an offset.  */
1560   if (symbol_ptr)
1561     {
1562       symbolS *dot;
1563
1564       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1565       if (dot)
1566         {
1567 #if 0
1568           intr->r_offset -=
1569             segment_info[S_GET_SEGMENT (symbol_ptr)].scnhdr.s_paddr;
1570 #endif
1571           intr->r_offset += S_GET_VALUE (symbol_ptr);
1572           intr->r_symndx = dot->sy_number;
1573         }
1574       else
1575         {
1576           intr->r_symndx = symbol_ptr->sy_number;
1577         }
1578
1579     }
1580   else
1581     {
1582       intr->r_symndx = -1;
1583     }
1584
1585 }
1586
1587 int
1588 start_label (ptr)
1589      char *ptr;
1590 {
1591   /* Check for :s.w */
1592   if (isalpha (ptr[1]) && ptr[2] == '.')
1593     return 0;
1594   /* Check for :s */
1595   if (isalpha (ptr[1]) && !isalpha (ptr[2]))
1596     return 0;
1597   return 1;
1598 }
1599
1600 int
1601 tc_coff_sizemachdep (frag)
1602      fragS *frag;
1603 {
1604   return md_relax_table[frag->fr_subtype].rlx_length;
1605 }