* config/obj-ieee.c: Remove duplicate prototypes.
[external/binutils.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Renesas H8/300
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3    2001, 2002, 2003 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 "subsegs.h"
27 #include "bfd.h"
28
29 #ifdef BFD_ASSEMBLER
30 #include "dwarf2dbg.h"
31 #endif
32
33 #define DEFINE_TABLE
34 #define h8_opcodes ops
35 #include "opcode/h8300.h"
36 #include "safe-ctype.h"
37
38 #ifdef OBJ_ELF
39 #include "elf/h8.h"
40 #endif
41
42 const char comment_chars[] = ";";
43 const char line_comment_chars[] = "#";
44 const char line_separator_chars[] = "";
45
46 void sbranch (int);
47 void h8300hmode (int);
48 void h8300smode (int);
49 void h8300hnmode (int);
50 void h8300snmode (int);
51 void h8300sxmode (int);
52 void h8300sxnmode (int);
53 static void pint (int);
54
55 int Hmode;
56 int Smode;
57 int Nmode;
58 int SXmode;
59
60 #define PSIZE (Hmode && !Nmode ? L_32 : L_16)
61
62 int bsize = L_8;                /* Default branch displacement.  */
63
64 struct h8_instruction
65 {
66   int length;
67   int noperands;
68   int idx;
69   int size;
70   const struct h8_opcode *opcode;
71 };
72
73 struct h8_instruction *h8_instructions;
74
75 void
76 h8300hmode (int arg ATTRIBUTE_UNUSED)
77 {
78   Hmode = 1;
79   Smode = 0;
80 #ifdef BFD_ASSEMBLER
81   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
82     as_warn (_("could not set architecture and machine"));
83 #endif
84 }
85
86 void
87 h8300smode (int arg ATTRIBUTE_UNUSED)
88 {
89   Smode = 1;
90   Hmode = 1;
91 #ifdef BFD_ASSEMBLER
92   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
93     as_warn (_("could not set architecture and machine"));
94 #endif
95 }
96
97 void
98 h8300hnmode (int arg ATTRIBUTE_UNUSED)
99 {
100   Hmode = 1;
101   Smode = 0;
102   Nmode = 1;
103 #ifdef BFD_ASSEMBLER
104   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300hn))
105     as_warn (_("could not set architecture and machine"));
106 #endif
107 }
108
109 void
110 h8300snmode (int arg ATTRIBUTE_UNUSED)
111 {
112   Smode = 1;
113   Hmode = 1;
114   Nmode = 1;
115 #ifdef BFD_ASSEMBLER
116   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sn))
117     as_warn (_("could not set architecture and machine"));
118 #endif
119 }
120
121 void
122 h8300sxmode (int arg ATTRIBUTE_UNUSED)
123 {
124   Smode = 1;
125   Hmode = 1;
126   SXmode = 1;
127 #ifdef BFD_ASSEMBLER
128   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sx))
129     as_warn (_("could not set architecture and machine"));
130 #endif
131 }
132
133 void
134 h8300sxnmode (int arg ATTRIBUTE_UNUSED)
135 {
136   Smode = 1;
137   Hmode = 1;
138   SXmode = 1;
139   Nmode = 1;
140 #ifdef BFD_ASSEMBLER
141   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sxn))
142     as_warn (_("could not set architecture and machine"));
143 #endif
144 }
145
146 void
147 sbranch (int size)
148 {
149   bsize = size;
150 }
151
152 static void
153 pint (int arg ATTRIBUTE_UNUSED)
154 {
155   cons (Hmode ? 4 : 2);
156 }
157
158 /* This table describes all the machine specific pseudo-ops the assembler
159    has to support.  The fields are:
160    pseudo-op name without dot
161    function to call to execute this pseudo-op
162    Integer arg to pass to the function.  */
163
164 const pseudo_typeS md_pseudo_table[] =
165 {
166   {"h8300h",  h8300hmode,  0},
167   {"h8300hn", h8300hnmode, 0},
168   {"h8300s",  h8300smode,  0},
169   {"h8300sn", h8300snmode, 0},
170   {"h8300sx", h8300sxmode, 0},
171   {"h8300sxn", h8300sxnmode, 0},
172   {"sbranch", sbranch, L_8},
173   {"lbranch", sbranch, L_16},
174
175   {"int", pint, 0},
176   {"data.b", cons, 1},
177   {"data.w", cons, 2},
178   {"data.l", cons, 4},
179   {"form", listing_psize, 0},
180   {"heading", listing_title, 0},
181   {"import",  s_ignore, 0},
182   {"page",    listing_eject, 0},
183   {"program", s_ignore, 0},
184   {0, 0, 0}
185 };
186
187 const int md_reloc_size;
188
189 const char EXP_CHARS[] = "eE";
190
191 /* Chars that mean this number is a floating point constant
192    As in 0f12.456
193    or    0d1.2345e12.  */
194 const char FLT_CHARS[] = "rRsSfFdDxXpP";
195
196 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics.  */
197
198 /* This function is called once, at assembler startup time.  This
199    should set up all the tables, etc. that the MD part of the assembler
200    needs.  */
201
202 void
203 md_begin (void)
204 {
205   unsigned int nopcodes;
206   struct h8_opcode *p, *p1;
207   struct h8_instruction *pi;
208   char prev_buffer[100];
209   int idx = 0;
210
211 #ifdef BFD_ASSEMBLER
212   if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
213     as_warn (_("could not set architecture and machine"));
214 #endif
215
216   opcode_hash_control = hash_new ();
217   prev_buffer[0] = 0;
218
219   nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode);
220   
221   h8_instructions = (struct h8_instruction *)
222     xmalloc (nopcodes * sizeof (struct h8_instruction));
223
224   pi = h8_instructions;
225   p1 = h8_opcodes;
226   /* We do a minimum amount of sorting on the opcode table; this is to
227      make it easy to describe the mova instructions without unnecessary
228      code duplication.
229      Sorting only takes place inside blocks of instructions of the form
230      X/Y, so for example mova/b, mova/w and mova/l can be intermixed.  */
231   while (p1)
232     {
233       struct h8_opcode *first_skipped = 0;
234       int len, cmplen = 0;
235       char *src = p1->name;
236       char *dst, *buffer;
237
238       if (p1->name == 0)
239         break;
240       /* Strip off any . part when inserting the opcode and only enter
241          unique codes into the hash table.  */
242       dst = buffer = malloc (strlen (src) + 1);
243       while (*src)
244         {
245           if (*src == '.')
246             {
247               src++;
248               break;
249             }
250           if (*src == '/')
251             cmplen = src - p1->name + 1;
252           *dst++ = *src++;
253         }
254       *dst = 0;
255       len = dst - buffer;
256       if (cmplen == 0)
257         cmplen = len;
258       hash_insert (opcode_hash_control, buffer, (char *) pi);
259       strcpy (prev_buffer, buffer);
260       idx++;
261
262       for (p = p1; p->name; p++)
263         {
264           /* A negative TIME is used to indicate that we've added this opcode
265              already.  */
266           if (p->time == -1)
267             continue;
268           if (strncmp (p->name, buffer, cmplen) != 0
269               || (p->name[cmplen] != '\0' && p->name[cmplen] != '.'
270                   && p->name[cmplen - 1] != '/'))
271             {
272               if (first_skipped == 0)
273                 first_skipped = p;
274               break;
275             }
276           if (strncmp (p->name, buffer, len) != 0)
277             {
278               if (first_skipped == 0)
279                 first_skipped = p;
280               continue;
281             }
282
283           p->time = -1;
284           pi->size = p->name[len] == '.' ? p->name[len + 1] : 0;
285           pi->idx = idx;
286
287           /* Find the number of operands.  */
288           pi->noperands = 0;
289           while (pi->noperands < 3 && p->args.nib[pi->noperands] != (op_type) E)
290             pi->noperands++;
291
292           /* Find the length of the opcode in bytes.  */
293           pi->length = 0;
294           while (p->data.nib[pi->length * 2] != (op_type) E)
295             pi->length++;
296
297           pi->opcode = p;
298           pi++;
299         }
300       p1 = first_skipped;
301     }
302
303   /* Add entry for the NULL vector terminator.  */
304   pi->length = 0;
305   pi->noperands = 0;
306   pi->idx = 0;
307   pi->size = 0;
308   pi->opcode = 0;
309
310   linkrelax = 1;
311 }
312
313 struct h8_exp
314 {
315   char *e_beg;
316   char *e_end;
317   expressionS e_exp;
318 };
319
320 struct h8_op
321 {
322   op_type mode;
323   unsigned reg;
324   expressionS exp;
325 };
326
327 static void clever_message (const struct h8_instruction *, struct h8_op *);
328 static void fix_operand_size (struct h8_op *, int);
329 static void build_bytes (const struct h8_instruction *, struct h8_op *);
330 static void do_a_fix_imm (int, int, struct h8_op *, int);
331 static void check_operand (struct h8_op *, unsigned int, char *);
332 static const struct h8_instruction * get_specific (const struct h8_instruction *, struct h8_op *, int) ;
333 static char *get_operands (unsigned, char *, struct h8_op *);
334 static void get_operand (char **, struct h8_op *, int);
335 static int parse_reg (char *, op_type *, unsigned *, int);
336 static char *skip_colonthing (char *, int *);
337 static char *parse_exp (char *, struct h8_op *);
338
339 static int constant_fits_width_p (struct h8_op *, unsigned int);
340 static int constant_fits_size_p (struct h8_op *, int, int);
341
342 /*
343   parse operands
344   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
345   r0l,r0h,..r7l,r7h
346   @WREG
347   @WREG+
348   @-WREG
349   #const
350   ccr
351 */
352
353 /* Try to parse a reg name.  Return the number of chars consumed.  */
354
355 static int
356 parse_reg (char *src, op_type *mode, unsigned int *reg, int direction)
357 {
358   char *end;
359   int len;
360
361   /* Cribbed from get_symbol_end.  */
362   if (!is_name_beginner (*src) || *src == '\001')
363     return 0;
364   end = src + 1;
365   while ((is_part_of_name (*end) && *end != '.') || *end == '\001')
366     end++;
367   len = end - src;
368
369   if (len == 2 && TOLOWER (src[0]) == 's' && TOLOWER (src[1]) == 'p')
370     {
371       *mode = PSIZE | REG | direction;
372       *reg = 7;
373       return len;
374     }
375   if (len == 3 && 
376       TOLOWER (src[0]) == 'c' && 
377       TOLOWER (src[1]) == 'c' && 
378       TOLOWER (src[2]) == 'r')
379     {
380       *mode = CCR;
381       *reg = 0;
382       return len;
383     }
384   if (len == 3 && 
385       TOLOWER (src[0]) == 'e' && 
386       TOLOWER (src[1]) == 'x' && 
387       TOLOWER (src[2]) == 'r')
388     {
389       *mode = EXR;
390       *reg = 1;
391       return len;
392     }
393   if (len == 3 && 
394       TOLOWER (src[0]) == 'v' && 
395       TOLOWER (src[1]) == 'b' && 
396       TOLOWER (src[2]) == 'r')
397     {
398       *mode = VBR;
399       *reg = 6;
400       return len;
401     }
402   if (len == 3 && 
403       TOLOWER (src[0]) == 's' && 
404       TOLOWER (src[1]) == 'b' && 
405       TOLOWER (src[2]) == 'r')
406     {
407       *mode = SBR;
408       *reg = 7;
409       return len;
410     }
411   if (len == 2 && TOLOWER (src[0]) == 'f' && TOLOWER (src[1]) == 'p')
412     {
413       *mode = PSIZE | REG | direction;
414       *reg = 6;
415       return len;
416     }
417   if (len == 3 && TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' &&
418       src[2] >= '0' && src[2] <= '7')
419     {
420       *mode = L_32 | REG | direction;
421       *reg = src[2] - '0';
422       if (!Hmode)
423         as_warn (_("Reg not valid for H8/300"));
424       return len;
425     }
426   if (len == 2 && TOLOWER (src[0]) == 'e' && src[1] >= '0' && src[1] <= '7')
427     {
428       *mode = L_16 | REG | direction;
429       *reg = src[1] - '0' + 8;
430       if (!Hmode)
431         as_warn (_("Reg not valid for H8/300"));
432       return len;
433     }
434
435   if (TOLOWER (src[0]) == 'r')
436     {
437       if (src[1] >= '0' && src[1] <= '7')
438         {
439           if (len == 3 && TOLOWER (src[2]) == 'l')
440             {
441               *mode = L_8 | REG | direction;
442               *reg = (src[1] - '0') + 8;
443               return len;
444             }
445           if (len == 3 && TOLOWER (src[2]) == 'h')
446             {
447               *mode = L_8 | REG | direction;
448               *reg = (src[1] - '0');
449               return len;
450             }
451           if (len == 2)
452             {
453               *mode = L_16 | REG | direction;
454               *reg = (src[1] - '0');
455               return len;
456             }
457         }
458     }
459
460   return 0;
461 }
462
463
464 /* Parse an immediate or address-related constant and store it in OP.
465    If the user also specifies the operand's size, store that size
466    in OP->MODE, otherwise leave it for later code to decide.  */
467
468 static char *
469 parse_exp (char *src, struct h8_op *op)
470 {
471   char *save;
472
473   save = input_line_pointer;
474   input_line_pointer = src;
475   expression (&op->exp);
476   if (op->exp.X_op == O_absent)
477     as_bad (_("missing operand"));
478   src = input_line_pointer;
479   input_line_pointer = save;
480
481   return skip_colonthing (src, &op->mode);
482 }
483
484
485 /* If SRC starts with an explicit operand size, skip it and store the size
486    in *MODE.  Leave *MODE unchanged otherwise.  */
487
488 static char *
489 skip_colonthing (char *src, int *mode)
490 {
491   if (*src == ':')
492     {
493       src++;
494       *mode &= ~SIZE;
495       if (src[0] == '8' && !ISDIGIT (src[1]))
496         *mode |= L_8;
497       else if (src[0] == '2' && !ISDIGIT (src[1]))
498         *mode |= L_2;
499       else if (src[0] == '3' && !ISDIGIT (src[1]))
500         *mode |= L_3;
501       else if (src[0] == '4' && !ISDIGIT (src[1]))
502         *mode |= L_4;
503       else if (src[0] == '5' && !ISDIGIT (src[1]))
504         *mode |= L_5;
505       else if (src[0] == '2' && src[1] == '4' && !ISDIGIT (src[2]))
506         *mode |= L_24;
507       else if (src[0] == '3' && src[1] == '2' && !ISDIGIT (src[2]))
508         *mode |= L_32;
509       else if (src[0] == '1' && src[1] == '6' && !ISDIGIT (src[2]))
510         *mode |= L_16;
511       else
512         as_bad (_("invalid operand size requested"));
513
514       while (ISDIGIT (*src))
515         src++;
516     }
517   return src;
518 }
519
520 /* The many forms of operand:
521
522    Rn                   Register direct
523    @Rn                  Register indirect
524    @(exp[:16], Rn)      Register indirect with displacement
525    @Rn+
526    @-Rn
527    @aa:8                absolute 8 bit
528    @aa:16               absolute 16 bit
529    @aa                  absolute 16 bit
530
531    #xx[:size]           immediate data
532    @(exp:[8], pc)       pc rel
533    @@aa[:8]             memory indirect.  */
534
535 static int
536 constant_fits_width_p (struct h8_op *operand, unsigned int width)
537 {
538   return ((operand->exp.X_add_number & ~width) == 0
539           || (operand->exp.X_add_number | width) == (unsigned)(~0));
540 }
541
542 static int
543 constant_fits_size_p (struct h8_op *operand, int size, int no_symbols)
544 {
545   offsetT num = operand->exp.X_add_number;
546   if (no_symbols
547       && (operand->exp.X_add_symbol != 0 || operand->exp.X_op_symbol != 0))
548     return 0;
549   switch (size)
550     {
551     case L_2:
552       return (num & ~3) == 0;
553     case L_3:
554       return (num & ~7) == 0;
555     case L_3NZ:
556       return num >= 1 && num < 8;
557     case L_4:
558       return (num & ~15) == 0;
559     case L_5:
560       return num >= 1 && num < 32;
561     case L_8:
562       return (num & ~0xFF) == 0 || ((unsigned)num | 0x7F) == ~0u;
563     case L_8U:
564       return (num & ~0xFF) == 0;
565     case L_16:
566       return (num & ~0xFFFF) == 0 || ((unsigned)num | 0x7FFF) == ~0u;
567     case L_16U:
568       return (num & ~0xFFFF) == 0;
569     case L_32:
570       return 1;
571     default:
572       abort ();
573     }
574 }
575
576 static void
577 get_operand (char **ptr, struct h8_op *op, int direction)
578 {
579   char *src = *ptr;
580   op_type mode;
581   unsigned int num;
582   unsigned int len;
583
584   op->mode = 0;
585
586   /* Check for '(' and ')' for instructions ldm and stm.  */
587   if (src[0] == '(' && src[8] == ')')
588     ++ src;
589
590   /* Gross.  Gross.  ldm and stm have a format not easily handled
591      by get_operand.  We deal with it explicitly here.  */
592   if (TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' && 
593       ISDIGIT (src[2]) && src[3] == '-' &&
594       TOLOWER (src[4]) == 'e' && TOLOWER (src[5]) == 'r' && ISDIGIT (src[6]))
595     {
596       int low, high;
597
598       low = src[2] - '0';
599       high = src[6] - '0';
600
601       if (high == low)
602         as_bad (_("Invalid register list for ldm/stm\n"));
603
604       if (high < low)
605         as_bad (_("Invalid register list for ldm/stm\n"));
606
607       if (high - low > 3)
608         as_bad (_("Invalid register list for ldm/stm)\n"));
609
610       /* Even sicker.  We encode two registers into op->reg.  One
611          for the low register to save, the other for the high
612          register to save;  we also set the high bit in op->reg
613          so we know this is "very special".  */
614       op->reg = 0x80000000 | (high << 8) | low;
615       op->mode = REG;
616       if (src[7] == ')')
617         *ptr = src + 8;
618       else
619         *ptr = src + 7;
620       return;
621     }
622
623   len = parse_reg (src, &op->mode, &op->reg, direction);
624   if (len)
625     {
626       src += len;
627       if (*src == '.')
628         {
629           int size = op->mode & SIZE;
630           switch (src[1])
631             {
632             case 'l': case 'L':
633               if (size != L_32)
634                 as_warn (_("mismatch between register and suffix"));
635               op->mode = (op->mode & ~MODE) | LOWREG;
636               break;
637             case 'w': case 'W':
638               if (size != L_32 && size != L_16)
639                 as_warn (_("mismatch between register and suffix"));
640               op->mode = (op->mode & ~MODE) | LOWREG;
641               op->mode = (op->mode & ~SIZE) | L_16;
642               break;
643             case 'b': case 'B':
644               op->mode = (op->mode & ~MODE) | LOWREG;
645               if (size != L_32 && size != L_8)
646                 as_warn (_("mismatch between register and suffix"));
647               op->mode = (op->mode & ~MODE) | LOWREG;
648               op->mode = (op->mode & ~SIZE) | L_8;
649               break;
650             default:
651               as_warn ("invalid suffix after register.");
652               break;
653             }
654           src += 2;
655         }
656       *ptr = src;
657       return;
658     }
659
660   if (*src == '@')
661     {
662       src++;
663       if (*src == '@')
664         {
665           *ptr = parse_exp (src + 1, op);
666           if (op->exp.X_add_number >= 0x100)
667             {
668               int divisor = 1;
669
670               op->mode = VECIND;
671               /* FIXME : 2?  or 4?  */
672               if (op->exp.X_add_number >= 0x400)
673                 as_bad (_("address too high for vector table jmp/jsr"));
674               else if (op->exp.X_add_number >= 0x200)
675                 divisor = 4;
676               else
677                 divisor = 2;
678
679               op->exp.X_add_number = op->exp.X_add_number / divisor - 0x80;
680             }
681           else
682             op->mode = MEMIND;
683           return;
684         }
685
686       if (*src == '-' || *src == '+')
687         {
688           len = parse_reg (src + 1, &mode, &num, direction);
689           if (len == 0)
690             {
691               /* Oops, not a reg after all, must be ordinary exp.  */
692               op->mode = ABS | direction;
693               *ptr = parse_exp (src, op);
694               return;
695             }
696
697           if (((mode & SIZE) != PSIZE)
698               /* For Normal mode accept 16 bit and 32 bit pointer registers.  */
699               && (!Nmode || ((mode & SIZE) != L_32)))
700             as_bad (_("Wrong size pointer register for architecture."));
701
702           op->mode = src[0] == '-' ? RDPREDEC : RDPREINC;
703           op->reg = num;
704           *ptr = src + 1 + len;
705           return;
706         }
707       if (*src == '(')
708         {
709           src++;
710
711           /* See if this is @(ERn.x, PC).  */
712           len = parse_reg (src, &mode, &op->reg, direction);
713           if (len != 0 && (mode & MODE) == REG && src[len] == '.')
714             {
715               switch (TOLOWER (src[len + 1]))
716                 {
717                 case 'b':
718                   mode = PCIDXB | direction;
719                   break;
720                 case 'w':
721                   mode = PCIDXW | direction;
722                   break;
723                 case 'l':
724                   mode = PCIDXL | direction;
725                   break;
726                 default:
727                   mode = 0;
728                   break;
729                 }
730               if (mode
731                   && src[len + 2] == ','
732                   && TOLOWER (src[len + 3]) != 'p' 
733                   && TOLOWER (src[len + 4]) != 'c'
734                   && src[len + 5] != ')')
735                 {
736                   *ptr = src + len + 6;
737                   op->mode |= mode;
738                   return;
739                 }
740               /* Fall through into disp case - the grammar is somewhat
741                  ambiguous, so we should try whether it's a DISP operand
742                  after all ("ER3.L" might be a poorly named label...).  */
743             }
744
745           /* Disp.  */
746
747           /* Start off assuming a 16 bit offset.  */
748
749           src = parse_exp (src, op);
750           if (*src == ')')
751             {
752               op->mode |= ABS | direction;
753               *ptr = src + 1;
754               return;
755             }
756
757           if (*src != ',')
758             {
759               as_bad (_("expected @(exp, reg16)"));
760               return;
761             }
762           src++;
763
764           len = parse_reg (src, &mode, &op->reg, direction);
765           if (len == 0 || (mode & MODE) != REG)
766             {
767               as_bad (_("expected @(exp, reg16)"));
768               return;
769             }
770           src += len;
771           if (src[0] == '.')
772             {
773               switch (TOLOWER (src[1]))
774                 {
775                 case 'b':
776                   op->mode |= INDEXB | direction;
777                   break;
778                 case 'w':
779                   op->mode |= INDEXW | direction;
780                   break;
781                 case 'l':
782                   op->mode |= INDEXL | direction;
783                   break;
784                 default:
785                   as_bad (_("expected .L, .W or .B for register in indexed addressing mode"));
786                 }
787               src += 2;
788               op->reg &= 7;
789             }
790           else
791             op->mode |= DISP | direction;
792           src = skip_colonthing (src, &op->mode);
793
794           if (*src != ')' && '(')
795             {
796               as_bad (_("expected @(exp, reg16)"));
797               return;
798             }
799           *ptr = src + 1;
800           return;
801         }
802       len = parse_reg (src, &mode, &num, direction);
803
804       if (len)
805         {
806           src += len;
807           if (*src == '+' || *src == '-')
808             {
809               if (((mode & SIZE) != PSIZE)
810                   /* For Normal mode accept 16 bit and 32 bit pointer registers.  */
811                   && (!Nmode || ((mode & SIZE) != L_32)))
812                 as_bad (_("Wrong size pointer register for architecture."));
813               op->mode = *src == '+' ? RSPOSTINC : RSPOSTDEC;
814               op->reg = num;
815               src++;
816               *ptr = src;
817               return;
818             }
819           if (((mode & SIZE) != PSIZE)
820               /* For Normal mode accept 16 bit and 32 bit pointer registers.  */
821               && (!Nmode || ((mode & SIZE) != L_32)))
822             as_bad (_("Wrong size pointer register for architecture."));
823
824           op->mode = direction | IND | PSIZE;
825           op->reg = num;
826           *ptr = src;
827
828           return;
829         }
830       else
831         {
832           /* must be a symbol */
833
834           op->mode = ABS | direction;
835           *ptr = parse_exp (src, op);
836           return;
837         }
838     }
839
840   if (*src == '#')
841     {
842       op->mode = IMM;
843       *ptr = parse_exp (src + 1, op);
844       return;
845     }
846   else if (strncmp (src, "mach", 4) == 0 || 
847            strncmp (src, "macl", 4) == 0 ||
848            strncmp (src, "MACH", 4) == 0 || 
849            strncmp (src, "MACL", 4) == 0)
850     {
851       op->reg = TOLOWER (src[3]) == 'l';
852       op->mode = MACREG;
853       *ptr = src + 4;
854       return;
855     }
856   else
857     {
858       op->mode = PCREL;
859       *ptr = parse_exp (src, op);
860     }
861 }
862
863 static char *
864 get_operands (unsigned int noperands, char *op_end, struct h8_op *operand)
865 {
866   char *ptr = op_end;
867
868   switch (noperands)
869     {
870     case 0:
871       break;
872
873     case 1:
874       ptr++;
875       get_operand (&ptr, operand + 0, SRC);
876       if (*ptr == ',')
877         {
878           ptr++;
879           get_operand (&ptr, operand + 1, DST);
880         }
881       break;
882
883     case 2:
884       ptr++;
885       get_operand (&ptr, operand + 0, SRC);
886       if (*ptr == ',')
887         ptr++;
888       get_operand (&ptr, operand + 1, DST);
889       break;
890
891     case 3:
892       ptr++;
893       get_operand (&ptr, operand + 0, SRC);
894       if (*ptr == ',')
895         ptr++;
896       get_operand (&ptr, operand + 1, DST);
897       if (*ptr == ',')
898         ptr++;
899       get_operand (&ptr, operand + 2, OP3);
900       break;
901
902     default:
903       abort ();
904     }
905
906   return ptr;
907 }
908
909 /* MOVA has special requirements.  Rather than adding twice the amount of
910    addressing modes, we simply special case it a bit.  */
911 static void
912 get_mova_operands (char *op_end, struct h8_op *operand)
913 {
914   char *ptr = op_end;
915
916   if (ptr[1] != '@' || ptr[2] != '(')
917     goto error;
918   ptr += 3;
919   operand[0].mode = 0;
920   ptr = parse_exp (ptr, &operand[0]);
921
922   if (*ptr !=',')
923     goto error;
924   ptr++;
925   get_operand (&ptr, operand + 1, DST);
926
927   if (*ptr =='.')
928     {
929       ptr++;
930       switch (*ptr++)
931         {
932         case 'b': case 'B':
933           operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
934           break;
935         case 'w': case 'W':
936           operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
937           break;
938         case 'l': case 'L':
939           operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
940           break;
941         default:
942           goto error;
943         }
944     }
945   else if ((operand[1].mode & MODE) == LOWREG)
946     {
947       switch (operand[1].mode & SIZE) 
948         {
949         case L_8:
950           operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
951           break;
952         case L_16:
953           operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
954           break;
955         case L_32:
956           operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
957           break;
958         default:
959           goto error;
960         }
961     }
962   else
963     goto error;
964
965   if (*ptr++ != ')' || *ptr++ != ',')
966     goto error;
967   get_operand (&ptr, operand + 2, OP3);
968   /* See if we can use the short form of MOVA.  */
969   if (((operand[1].mode & MODE) == REG || (operand[1].mode & MODE) == LOWREG)
970       && (operand[2].mode & MODE) == REG
971       && (operand[1].reg & 7) == (operand[2].reg & 7))
972     {
973       operand[1].mode = operand[2].mode = 0;
974       operand[0].reg = operand[2].reg & 7;
975     }
976   return;
977
978  error:
979   as_bad (_("expected valid addressing mode for mova: \"@(disp, ea.sz),ERn\""));
980   return;
981 }
982
983 static void
984 get_rtsl_operands (char *ptr, struct h8_op *operand)
985 {
986   int mode, num, num2, len, type = 0;
987
988   ptr++;
989   if (*ptr == '(')
990     {
991       ptr++;
992       type = 1;
993     }
994   len = parse_reg (ptr, &mode, &num, SRC);
995   if (len == 0 || (mode & MODE) != REG)
996     {
997       as_bad (_("expected register"));
998       return;
999     }
1000   ptr += len;
1001   if (*ptr == '-')
1002     {
1003       len = parse_reg (++ptr, &mode, &num2, SRC);
1004       if (len == 0 || (mode & MODE) != REG)
1005         {
1006           as_bad (_("expected register"));
1007           return;
1008         }
1009       ptr += len;
1010       /* CONST_xxx are used as placeholders in the opcode table.  */
1011       num = num2 - num;
1012       if (num < 0 || num > 3)
1013         {
1014           as_bad (_("invalid register list"));
1015           return;
1016         }
1017     }
1018   else
1019     num2 = num, num = 0;
1020   if (type == 1 && *ptr++ != ')')
1021     {
1022       as_bad (_("expected closing paren"));
1023       return;
1024     }
1025   operand[0].mode = RS32;
1026   operand[1].mode = RD32;
1027   operand[0].reg = num;
1028   operand[1].reg = num2;
1029 }
1030
1031 /* Passed a pointer to a list of opcodes which use different
1032    addressing modes, return the opcode which matches the opcodes
1033    provided.  */
1034
1035 static const struct h8_instruction *
1036 get_specific (const struct h8_instruction *instruction,
1037               struct h8_op *operands, int size)
1038 {
1039   const struct h8_instruction *this_try = instruction;
1040   const struct h8_instruction *found_other = 0, *found_mismatched = 0;
1041   int found = 0;
1042   int this_index = instruction->idx;
1043   int noperands = 0;
1044
1045   /* There's only one ldm/stm and it's easier to just
1046      get out quick for them.  */
1047   if (OP_KIND (instruction->opcode->how) == O_LDM
1048       || OP_KIND (instruction->opcode->how) == O_STM)
1049     return this_try;
1050
1051   while (noperands < 3 && operands[noperands].mode != 0)
1052     noperands++;
1053
1054   while (this_index == instruction->idx && !found)
1055     {
1056       int this_size;
1057
1058       found = 1;
1059       this_try = instruction++;
1060       this_size = this_try->opcode->how & SN;
1061
1062       if (this_try->noperands != noperands)
1063         found = 0;
1064       else if (this_try->noperands > 0)
1065         {
1066           int i;
1067
1068           for (i = 0; i < this_try->noperands && found; i++)
1069             {
1070               op_type op = this_try->opcode->args.nib[i];
1071               int op_mode = op & MODE;
1072               int op_size = op & SIZE;
1073               int x = operands[i].mode;
1074               int x_mode = x & MODE;
1075               int x_size = x & SIZE;
1076
1077               if (op_mode == LOWREG && (x_mode == REG || x_mode == LOWREG))
1078                 {
1079                   if ((x_size == L_8 && (operands[i].reg & 8) == 0)
1080                       || (x_size == L_16 && (operands[i].reg & 8) == 8))
1081                     as_warn (_("can't use high part of register in operand %d"), i);
1082
1083                   if (x_size != op_size)
1084                     found = 0;
1085                 }
1086               else if (op_mode == REG)
1087                 {
1088                   if (x_mode == LOWREG)
1089                     x_mode = REG;
1090                   if (x_mode != REG)
1091                     found = 0;
1092
1093                   if (x_size == L_P)
1094                     x_size = (Hmode ? L_32 : L_16);
1095                   if (op_size == L_P)
1096                     op_size = (Hmode ? L_32 : L_16);
1097
1098                   /* The size of the reg is v important.  */
1099                   if (op_size != x_size)
1100                     found = 0;
1101                 }
1102               else if (op_mode & CTRL)  /* control register */
1103                 {
1104                   if (!(x_mode & CTRL))
1105                     found = 0;
1106
1107                   switch (x_mode)
1108                     {
1109                     case CCR:
1110                       if (op_mode != CCR &&
1111                           op_mode != CCR_EXR &&
1112                           op_mode != CC_EX_VB_SB)
1113                         found = 0;
1114                       break;
1115                     case EXR:
1116                       if (op_mode != EXR &&
1117                           op_mode != CCR_EXR &&
1118                           op_mode != CC_EX_VB_SB)
1119                         found = 0;
1120                       break;
1121                     case MACH:
1122                       if (op_mode != MACH &&
1123                           op_mode != MACREG)
1124                         found = 0;
1125                       break;
1126                     case MACL:
1127                       if (op_mode != MACL &&
1128                           op_mode != MACREG)
1129                         found = 0;
1130                       break;
1131                     case VBR:
1132                       if (op_mode != VBR &&
1133                           op_mode != VBR_SBR &&
1134                           op_mode != CC_EX_VB_SB)
1135                         found = 0;
1136                       break;
1137                     case SBR:
1138                       if (op_mode != SBR &&
1139                           op_mode != VBR_SBR &&
1140                           op_mode != CC_EX_VB_SB)
1141                         found = 0;
1142                       break;
1143                     }
1144                 }
1145               else if ((op & ABSJMP) && (x_mode == ABS || x_mode == PCREL))
1146                 {
1147                   operands[i].mode &= ~MODE;
1148                   operands[i].mode |= ABSJMP;
1149                   /* But it may not be 24 bits long.  */
1150                   if (x_mode == ABS && !Hmode)
1151                     {
1152                       operands[i].mode &= ~SIZE;
1153                       operands[i].mode |= L_16;
1154                     }
1155                   if ((operands[i].mode & SIZE) == L_32
1156                       && (op_mode & SIZE) != L_32)
1157                    found = 0;
1158                 }
1159               else if (x_mode == IMM && op_mode != IMM)
1160                 {
1161                   offsetT num = operands[i].exp.X_add_number;
1162                   if (op_mode == KBIT || op_mode == DBIT)
1163                     /* This is ok if the immediate value is sensible.  */;
1164                   else if (op_mode == CONST_2)
1165                     found = num == 2;
1166                   else if (op_mode == CONST_4)
1167                     found = num == 4;
1168                   else if (op_mode == CONST_8)
1169                     found = num == 8;
1170                   else if (op_mode == CONST_16)
1171                     found = num == 16;
1172                   else
1173                     found = 0;
1174                 }
1175               else if (op_mode == PCREL && op_mode == x_mode)
1176                 {
1177                   /* movsd, bsr/bc and bsr/bs only come in PCREL16 flavour:
1178                      If x_size is L_8, promote it.  */
1179                   if (OP_KIND (this_try->opcode->how) == O_MOVSD
1180                       || OP_KIND (this_try->opcode->how) == O_BSRBC
1181                       || OP_KIND (this_try->opcode->how) == O_BSRBS)
1182                     if (x_size == L_8)
1183                       x_size = L_16;
1184
1185                   /* The size of the displacement is important.  */
1186                   if (op_size != x_size)
1187                     found = 0;
1188                 }
1189               else if ((op_mode == DISP || op_mode == IMM || op_mode == ABS
1190                         || op_mode == INDEXB || op_mode == INDEXW
1191                         || op_mode == INDEXL)
1192                        && op_mode == x_mode)
1193                 {
1194                   /* Promote a L_24 to L_32 if it makes us match.  */
1195                   if (x_size == L_24 && op_size == L_32)
1196                     {
1197                       x &= ~SIZE;
1198                       x |= x_size = L_32;
1199                     }
1200
1201 #if 0 /* ??? */
1202                   /* Promote an L8 to L_16 if it makes us match.  */
1203                   if ((op_mode == ABS || op_mode == DISP) && x_size == L_8)
1204                     {
1205                       if (op_size == L_16)
1206                         x_size = L_16;
1207                     }
1208 #endif
1209
1210                   if (((x_size == L_16 && op_size == L_16U)
1211                        || (x_size == L_8 && op_size == L_8U)
1212                        || (x_size == L_3 && op_size == L_3NZ))
1213                       /* We're deliberately more permissive for ABS modes.  */
1214                       && (op_mode == ABS
1215                           || constant_fits_size_p (operands + i, op_size,
1216                                                    op & NO_SYMBOLS)))
1217                     x_size = op_size;
1218
1219                   if (x_size != 0 && op_size != x_size)
1220                     found = 0;
1221                   else if (x_size == 0
1222                            && ! constant_fits_size_p (operands + i, op_size,
1223                                                       op & NO_SYMBOLS))
1224                     found = 0;
1225                 }
1226               else if (op_mode != x_mode)
1227                 {
1228                   found = 0;
1229                 }
1230             }
1231         }
1232       if (found)
1233         {
1234           if ((this_try->opcode->available == AV_H8SX && ! SXmode)
1235               || (this_try->opcode->available == AV_H8S && ! Smode)
1236               || (this_try->opcode->available == AV_H8H && ! Hmode))
1237             found = 0, found_other = this_try;
1238           else if (this_size != size && (this_size != SN && size != SN))
1239             found_mismatched = this_try, found = 0;
1240
1241         }
1242     }
1243   if (found)
1244     return this_try;
1245   if (found_other)
1246     {
1247       as_warn (_("Opcode `%s' with these operand types not available in %s mode"),
1248                found_other->opcode->name,
1249                (! Hmode && ! Smode ? "H8/300"
1250                 : SXmode ? "H8sx"
1251                 : Smode ? "H8/300S"
1252                 : "H8/300H"));
1253     }
1254   else if (found_mismatched)
1255     {
1256       as_warn (_("mismatch between opcode size and operand size"));
1257       return found_mismatched;
1258     }
1259   return 0;
1260 }
1261
1262 static void
1263 check_operand (struct h8_op *operand, unsigned int width, char *string)
1264 {
1265   if (operand->exp.X_add_symbol == 0
1266       && operand->exp.X_op_symbol == 0)
1267     {
1268       /* No symbol involved, let's look at offset, it's dangerous if
1269          any of the high bits are not 0 or ff's, find out by oring or
1270          anding with the width and seeing if the answer is 0 or all
1271          fs.  */
1272
1273       if (! constant_fits_width_p (operand, width))
1274         {
1275           if (width == 255
1276               && (operand->exp.X_add_number & 0xff00) == 0xff00)
1277             {
1278               /* Just ignore this one - which happens when trying to
1279                  fit a 16 bit address truncated into an 8 bit address
1280                  of something like bset.  */
1281             }
1282           else if (strcmp (string, "@") == 0
1283                    && width == 0xffff
1284                    && (operand->exp.X_add_number & 0xff8000) == 0xff8000)
1285             {
1286               /* Just ignore this one - which happens when trying to
1287                  fit a 24 bit address truncated into a 16 bit address
1288                  of something like mov.w.  */
1289             }
1290           else
1291             {
1292               as_warn (_("operand %s0x%lx out of range."), string,
1293                        (unsigned long) operand->exp.X_add_number);
1294             }
1295         }
1296     }
1297 }
1298
1299 /* RELAXMODE has one of 3 values:
1300
1301    0 Output a "normal" reloc, no relaxing possible for this insn/reloc
1302
1303    1 Output a relaxable 24bit absolute mov.w address relocation
1304      (may relax into a 16bit absolute address).
1305
1306    2 Output a relaxable 16/24 absolute mov.b address relocation
1307      (may relax into an 8bit absolute address).  */
1308
1309 static void
1310 do_a_fix_imm (int offset, int nibble, struct h8_op *operand, int relaxmode)
1311 {
1312   int idx;
1313   int size;
1314   int where;
1315   char *bytes = frag_now->fr_literal + offset;
1316
1317   char *t = ((operand->mode & MODE) == IMM) ? "#" : "@";
1318
1319   if (operand->exp.X_add_symbol == 0)
1320     {
1321       switch (operand->mode & SIZE)
1322         {
1323         case L_2:
1324           check_operand (operand, 0x3, t);
1325           bytes[0] |= (operand->exp.X_add_number & 3) << (nibble ? 0 : 4);
1326           break;
1327         case L_3:
1328         case L_3NZ:
1329           check_operand (operand, 0x7, t);
1330           bytes[0] |= (operand->exp.X_add_number & 7) << (nibble ? 0 : 4);
1331           break;
1332         case L_4:
1333           check_operand (operand, 0xF, t);
1334           bytes[0] |= (operand->exp.X_add_number & 15) << (nibble ? 0 : 4);
1335           break;
1336         case L_5:
1337           check_operand (operand, 0x1F, t);
1338           bytes[0] |= operand->exp.X_add_number & 31;
1339           break;
1340         case L_8:
1341         case L_8U:
1342           check_operand (operand, 0xff, t);
1343           bytes[0] |= operand->exp.X_add_number;
1344           break;
1345         case L_16:
1346         case L_16U:
1347           check_operand (operand, 0xffff, t);
1348           bytes[0] |= operand->exp.X_add_number >> 8;
1349           bytes[1] |= operand->exp.X_add_number >> 0;
1350           break;
1351         case L_24:
1352           check_operand (operand, 0xffffff, t);
1353           bytes[0] |= operand->exp.X_add_number >> 16;
1354           bytes[1] |= operand->exp.X_add_number >> 8;
1355           bytes[2] |= operand->exp.X_add_number >> 0;
1356           break;
1357
1358         case L_32:
1359           /* This should be done with bfd.  */
1360           bytes[0] |= operand->exp.X_add_number >> 24;
1361           bytes[1] |= operand->exp.X_add_number >> 16;
1362           bytes[2] |= operand->exp.X_add_number >> 8;
1363           bytes[3] |= operand->exp.X_add_number >> 0;
1364           if (relaxmode != 0)
1365             {
1366               idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
1367               fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
1368             }
1369           break;
1370         }
1371     }
1372   else
1373     {
1374       switch (operand->mode & SIZE)
1375         {
1376         case L_24:
1377         case L_32:
1378           size = 4;
1379           where = (operand->mode & SIZE) == L_24 ? -1 : 0;
1380           if (relaxmode == 2)
1381             idx = R_MOV24B1;
1382           else if (relaxmode == 1)
1383             idx = R_MOVL1;
1384           else
1385             idx = R_RELLONG;
1386           break;
1387         default:
1388           as_bad (_("Can't work out size of operand.\n"));
1389         case L_16:
1390         case L_16U:
1391           size = 2;
1392           where = 0;
1393           if (relaxmode == 2)
1394             idx = R_MOV16B1;
1395           else
1396             idx = R_RELWORD;
1397           operand->exp.X_add_number =
1398             ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1399           operand->exp.X_add_number |= (bytes[0] << 8) | bytes[1];
1400           break;
1401         case L_8:
1402           size = 1;
1403           where = 0;
1404           idx = R_RELBYTE;
1405           operand->exp.X_add_number =
1406             ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1407           operand->exp.X_add_number |= bytes[0];
1408         }
1409
1410       fix_new_exp (frag_now,
1411                    offset + where,
1412                    size,
1413                    &operand->exp,
1414                    0,
1415                    idx);
1416     }
1417 }
1418
1419 /* Now we know what sort of opcodes it is, let's build the bytes.  */
1420
1421 static void
1422 build_bytes (const struct h8_instruction *this_try, struct h8_op *operand)
1423 {
1424   int i;
1425   char *output = frag_more (this_try->length);
1426   op_type *nibble_ptr = this_try->opcode->data.nib;
1427   op_type c;
1428   unsigned int nibble_count = 0;
1429   int op_at[3];
1430   int nib = 0;
1431   int movb = 0;
1432   char asnibbles[100];
1433   char *p = asnibbles;
1434   int high, low;
1435
1436   if (!Hmode && this_try->opcode->available != AV_H8)
1437     as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
1438              this_try->opcode->name);
1439   else if (!Smode 
1440            && this_try->opcode->available != AV_H8 
1441            && this_try->opcode->available != AV_H8H)
1442     as_warn (_("Opcode `%s' with these operand types not available in H8/300H mode"),
1443              this_try->opcode->name);
1444   else if (!SXmode 
1445            && this_try->opcode->available != AV_H8
1446            && this_try->opcode->available != AV_H8H
1447            && this_try->opcode->available != AV_H8S)
1448     as_warn (_("Opcode `%s' with these operand types not available in H8/300S mode"),
1449              this_try->opcode->name);
1450
1451   while (*nibble_ptr != (op_type) E)
1452     {
1453       int d;
1454
1455       nib = 0;
1456       c = *nibble_ptr++;
1457
1458       d = (c & OP3) == OP3 ? 2 : (c & DST) == DST ? 1 : 0;
1459
1460       if (c < 16)
1461         nib = c;
1462       else
1463         {
1464           int c2 = c & MODE;
1465
1466           if (c2 == REG || c2 == LOWREG
1467               || c2 == IND || c2 == PREINC || c2 == PREDEC
1468               || c2 == POSTINC || c2 == POSTDEC)
1469             {
1470               nib = operand[d].reg;
1471               if (c2 == LOWREG)
1472                 nib &= 7;
1473             }
1474
1475           else if (c & CTRL)    /* Control reg operand.  */
1476             nib = operand[d].reg;
1477
1478           else if ((c & DISPREG) == (DISPREG))
1479             {
1480               nib = operand[d].reg;
1481             }
1482           else if (c2 == ABS)
1483             {
1484               operand[d].mode = c;
1485               op_at[d] = nibble_count;
1486               nib = 0;
1487             }
1488           else if (c2 == IMM || c2 == PCREL || c2 == ABS
1489                    || (c & ABSJMP) || c2 == DISP)
1490             {
1491               operand[d].mode = c;
1492               op_at[d] = nibble_count;
1493               nib = 0;
1494             }
1495           else if ((c & IGNORE) || (c & DATA))
1496             nib = 0;
1497
1498           else if (c2 == DBIT)
1499             {
1500               switch (operand[0].exp.X_add_number)
1501                 {
1502                 case 1:
1503                   nib = c;
1504                   break;
1505                 case 2:
1506                   nib = 0x8 | c;
1507                   break;
1508                 default:
1509                   as_bad (_("Need #1 or #2 here"));
1510                 }
1511             }
1512           else if (c2 == KBIT)
1513             {
1514               switch (operand[0].exp.X_add_number)
1515                 {
1516                 case 1:
1517                   nib = 0;
1518                   break;
1519                 case 2:
1520                   nib = 8;
1521                   break;
1522                 case 4:
1523                   if (!Hmode)
1524                     as_warn (_("#4 not valid on H8/300."));
1525                   nib = 9;
1526                   break;
1527
1528                 default:
1529                   as_bad (_("Need #1 or #2 here"));
1530                   break;
1531                 }
1532               /* Stop it making a fix.  */
1533               operand[0].mode = 0;
1534             }
1535
1536           if (c & MEMRELAX)
1537             operand[d].mode |= MEMRELAX;
1538
1539           if (c & B31)
1540             nib |= 0x8;
1541
1542           if (c & B21)
1543             nib |= 0x4;
1544
1545           if (c & B11)
1546             nib |= 0x2;
1547
1548           if (c & B01)
1549             nib |= 0x1;
1550
1551           if (c2 == MACREG)
1552             {
1553               if (operand[0].mode == MACREG)
1554                 /* stmac has mac[hl] as the first operand.  */
1555                 nib = 2 + operand[0].reg;
1556               else
1557                 /* ldmac has mac[hl] as the second operand.  */
1558                 nib = 2 + operand[1].reg;
1559             }
1560         }
1561       nibble_count++;
1562
1563       *p++ = nib;
1564     }
1565
1566   /* Disgusting.  Why, oh why didn't someone ask us for advice
1567      on the assembler format.  */
1568   if (OP_KIND (this_try->opcode->how) == O_LDM)
1569     {
1570       high = (operand[1].reg >> 8) & 0xf;
1571       low  = (operand[1].reg) & 0xf;
1572       asnibbles[2] = high - low;
1573       asnibbles[7] = high;
1574     }
1575   else if (OP_KIND (this_try->opcode->how) == O_STM)
1576     {
1577       high = (operand[0].reg >> 8) & 0xf;
1578       low  = (operand[0].reg) & 0xf;
1579       asnibbles[2] = high - low;
1580       asnibbles[7] = low;
1581     }
1582
1583   for (i = 0; i < this_try->length; i++)
1584     output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1585
1586   /* Note if this is a movb instruction -- there's a special relaxation
1587      which only applies to them.  */
1588   if (this_try->opcode->how == O (O_MOV, SB))
1589     movb = 1;
1590
1591   /* Output any fixes.  */
1592   for (i = 0; i < this_try->noperands; i++)
1593     {
1594       int x = operand[i].mode;
1595       int x_mode = x & MODE;
1596
1597       if (x_mode == IMM || x_mode == DISP)
1598         do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
1599                       op_at[i] & 1, operand + i, (x & MEMRELAX) != 0);
1600
1601       else if (x_mode == ABS)
1602         do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
1603                       op_at[i] & 1, operand + i,
1604                       (x & MEMRELAX) ? movb + 1 : 0);
1605
1606       else if (x_mode == PCREL)
1607         {
1608           int size16 = (x & SIZE) == L_16;
1609           int size = size16 ? 2 : 1;
1610           int type = size16 ? R_PCRWORD : R_PCRBYTE;
1611           fixS *fixP;
1612
1613           check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1614
1615           if (operand[i].exp.X_add_number & 1)
1616             as_warn (_("branch operand has odd offset (%lx)\n"),
1617                      (unsigned long) operand->exp.X_add_number);
1618 #ifndef OBJ_ELF
1619           /* The COFF port has always been off by one, changing it
1620              now would be an incompatible change, so we leave it as-is.
1621
1622              We don't want to do this for ELF as we want to be
1623              compatible with the proposed ELF format from Hitachi.  */
1624           operand[i].exp.X_add_number -= 1;
1625 #endif
1626           if (size16)
1627             {
1628               operand[i].exp.X_add_number =
1629                 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1630             }
1631           else
1632             {
1633               operand[i].exp.X_add_number =
1634                 ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1635             }
1636
1637           /* For BRA/S.  */
1638           if (! size16)
1639             operand[i].exp.X_add_number |= output[op_at[i] / 2];
1640
1641           fixP = fix_new_exp (frag_now,
1642                               output - frag_now->fr_literal + op_at[i] / 2,
1643                               size,
1644                               &operand[i].exp,
1645                               1,
1646                               type);
1647           fixP->fx_signed = 1;
1648         }
1649       else if (x_mode == MEMIND)
1650         {
1651           check_operand (operand + i, 0xff, "@@");
1652           fix_new_exp (frag_now,
1653                        output - frag_now->fr_literal + 1,
1654                        1,
1655                        &operand[i].exp,
1656                        0,
1657                        R_MEM_INDIRECT);
1658         }
1659       else if (x_mode == VECIND)
1660         {
1661           check_operand (operand + i, 0x7f, "@@");
1662           /* FIXME: approximating the effect of "B31" here...
1663              This is very hackish, and ought to be done a better way.  */
1664           operand[i].exp.X_add_number |= 0x80;
1665           fix_new_exp (frag_now,
1666                        output - frag_now->fr_literal + 1,
1667                        1,
1668                        &operand[i].exp,
1669                        0,
1670                        R_MEM_INDIRECT);
1671         }
1672       else if (x & ABSJMP)
1673         {
1674           int where = 0;
1675           bfd_reloc_code_real_type reloc_type = R_JMPL1;
1676
1677 #ifdef OBJ_ELF
1678           /* To be compatible with the proposed H8 ELF format, we
1679              want the relocation's offset to point to the first byte
1680              that will be modified, not to the start of the instruction.  */
1681           
1682           if ((operand->mode & SIZE) == L_32)
1683             {
1684               where = 2;
1685               reloc_type = R_RELLONG;
1686             }
1687           else
1688             where = 1;
1689 #endif
1690
1691           /* This jmp may be a jump or a branch.  */
1692
1693           check_operand (operand + i, 
1694                          SXmode ? 0xffffffff : Hmode ? 0xffffff : 0xffff, 
1695                          "@");
1696
1697           if (operand[i].exp.X_add_number & 1)
1698             as_warn (_("branch operand has odd offset (%lx)\n"),
1699                      (unsigned long) operand->exp.X_add_number);
1700
1701           if (!Hmode)
1702             operand[i].exp.X_add_number =
1703               ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1704           fix_new_exp (frag_now,
1705                        output - frag_now->fr_literal + where,
1706                        4,
1707                        &operand[i].exp,
1708                        0,
1709                        reloc_type);
1710         }
1711     }
1712 }
1713
1714 /* Try to give an intelligent error message for common and simple to
1715    detect errors.  */
1716
1717 static void
1718 clever_message (const struct h8_instruction *instruction,
1719                 struct h8_op *operand)
1720 {
1721   /* Find out if there was more than one possible opcode.  */
1722
1723   if ((instruction + 1)->idx != instruction->idx)
1724     {
1725       int argn;
1726
1727       /* Only one opcode of this flavour, try to guess which operand
1728          didn't match.  */
1729       for (argn = 0; argn < instruction->noperands; argn++)
1730         {
1731           switch (instruction->opcode->args.nib[argn])
1732             {
1733             case RD16:
1734               if (operand[argn].mode != RD16)
1735                 {
1736                   as_bad (_("destination operand must be 16 bit register"));
1737                   return;
1738
1739                 }
1740               break;
1741
1742             case RS8:
1743               if (operand[argn].mode != RS8)
1744                 {
1745                   as_bad (_("source operand must be 8 bit register"));
1746                   return;
1747                 }
1748               break;
1749
1750             case ABS16DST:
1751               if (operand[argn].mode != ABS16DST)
1752                 {
1753                   as_bad (_("destination operand must be 16bit absolute address"));
1754                   return;
1755                 }
1756               break;
1757             case RD8:
1758               if (operand[argn].mode != RD8)
1759                 {
1760                   as_bad (_("destination operand must be 8 bit register"));
1761                   return;
1762                 }
1763               break;
1764
1765             case ABS16SRC:
1766               if (operand[argn].mode != ABS16SRC)
1767                 {
1768                   as_bad (_("source operand must be 16bit absolute address"));
1769                   return;
1770                 }
1771               break;
1772
1773             }
1774         }
1775     }
1776   as_bad (_("invalid operands"));
1777 }
1778
1779
1780 /* If OPERAND is part of an address, adjust its size and value given
1781    that it addresses SIZE bytes.
1782
1783    This function decides how big non-immediate constants are when no
1784    size was explicitly given.  It also scales down the assembly-level
1785    displacement in an @(d:2,ERn) operand.  */
1786
1787 static void
1788 fix_operand_size (struct h8_op *operand, int size)
1789 {
1790   if (SXmode && (operand->mode & MODE) == DISP)
1791     {
1792       /* If the user didn't specify an operand width, see if we
1793          can use @(d:2,ERn).  */
1794       if ((operand->mode & SIZE) == 0
1795           && operand->exp.X_add_symbol == 0
1796           && operand->exp.X_op_symbol == 0
1797           && (operand->exp.X_add_number == size
1798               || operand->exp.X_add_number == size * 2
1799               || operand->exp.X_add_number == size * 3))
1800         operand->mode |= L_2;
1801
1802       /* Scale down the displacement in an @(d:2,ERn) operand.
1803          X_add_number then contains the desired field value.  */
1804       if ((operand->mode & SIZE) == L_2)
1805         {
1806           if (operand->exp.X_add_number % size != 0)
1807             as_warn (_("operand/size mis-match"));
1808           operand->exp.X_add_number /= size;
1809         }
1810     }
1811
1812   if ((operand->mode & SIZE) == 0)
1813     switch (operand->mode & MODE)
1814       {
1815       case DISP:
1816       case INDEXB:
1817       case INDEXW:
1818       case INDEXL:
1819       case ABS:
1820         /* Pick a 24-bit address unless we know that a 16-bit address
1821            is safe.  get_specific() will relax L_24 into L_32 where
1822            necessary.  */
1823         if (Hmode
1824             && !Nmode 
1825             && (operand->exp.X_add_number < -32768
1826                 || operand->exp.X_add_number > 32767
1827                 || operand->exp.X_add_symbol != 0
1828                 || operand->exp.X_op_symbol != 0))
1829           operand->mode |= L_24;
1830         else
1831           operand->mode |= L_16;
1832         break;
1833
1834       case PCREL:
1835         /* This condition is long standing, though somewhat suspect.  */
1836         if (operand->exp.X_add_number > -128
1837             && operand->exp.X_add_number < 127)
1838           operand->mode |= L_8;
1839         else
1840           operand->mode |= L_16;
1841         break;
1842       }
1843 }
1844
1845
1846 /* This is the guts of the machine-dependent assembler.  STR points to
1847    a machine dependent instruction.  This function is supposed to emit
1848    the frags/bytes it assembles.  */
1849
1850 void
1851 md_assemble (char *str)
1852 {
1853   char *op_start;
1854   char *op_end;
1855   struct h8_op operand[3];
1856   const struct h8_instruction *instruction;
1857   const struct h8_instruction *prev_instruction;
1858
1859   char *dot = 0;
1860   char *slash = 0;
1861   char c;
1862   int size, i;
1863
1864   /* Drop leading whitespace.  */
1865   while (*str == ' ')
1866     str++;
1867
1868   /* Find the op code end.  */
1869   for (op_start = op_end = str;
1870        *op_end != 0 && *op_end != ' ';
1871        op_end++)
1872     {
1873       if (*op_end == '.')
1874         {
1875           dot = op_end + 1;
1876           *op_end = 0;
1877           op_end += 2;
1878           break;
1879         }
1880       else if (*op_end == '/' && ! slash)
1881         slash = op_end;
1882     }
1883
1884   if (op_end == op_start)
1885     {
1886       as_bad (_("can't find opcode "));
1887     }
1888   c = *op_end;
1889
1890   *op_end = 0;
1891
1892   /* The assembler stops scanning the opcode at slashes, so it fails
1893      to make characters following them lower case.  Fix them.  */
1894   if (slash)
1895     while (*++slash)
1896       *slash = TOLOWER (*slash);
1897
1898   instruction = (const struct h8_instruction *)
1899     hash_find (opcode_hash_control, op_start);
1900
1901   if (instruction == NULL)
1902     {
1903       as_bad (_("unknown opcode"));
1904       return;
1905     }
1906
1907   /* We used to set input_line_pointer to the result of get_operands,
1908      but that is wrong.  Our caller assumes we don't change it.  */
1909
1910   operand[0].mode = 0;
1911   operand[1].mode = 0;
1912   operand[2].mode = 0;
1913
1914   if (OP_KIND (instruction->opcode->how) == O_MOVAB
1915       || OP_KIND (instruction->opcode->how) == O_MOVAW
1916       || OP_KIND (instruction->opcode->how) == O_MOVAL)
1917     get_mova_operands (op_end, operand);
1918   else if (OP_KIND (instruction->opcode->how) == O_RTEL
1919            || OP_KIND (instruction->opcode->how) == O_RTSL)
1920     get_rtsl_operands (op_end, operand);
1921   else
1922     get_operands (instruction->noperands, op_end, operand);
1923
1924   *op_end = c;
1925   prev_instruction = instruction;
1926
1927   size = SN;
1928   if (dot)
1929     {
1930       switch (TOLOWER (*dot))
1931         {
1932         case 'b':
1933           size = SB;
1934           break;
1935
1936         case 'w':
1937           size = SW;
1938           break;
1939
1940         case 'l':
1941           size = SL;
1942           break;
1943         }
1944     }
1945   if (OP_KIND (instruction->opcode->how) == O_MOVAB ||
1946       OP_KIND (instruction->opcode->how) == O_MOVAW ||
1947       OP_KIND (instruction->opcode->how) == O_MOVAL)
1948     {
1949       switch (operand[0].mode & MODE)
1950         {
1951         case INDEXB:
1952         default:
1953           fix_operand_size (&operand[1], 1);
1954           break;
1955         case INDEXW:
1956           fix_operand_size (&operand[1], 2);
1957           break;
1958         case INDEXL:
1959           fix_operand_size (&operand[1], 4);
1960           break;
1961         }
1962     }
1963   else
1964     {
1965       for (i = 0; i < 3 && operand[i].mode != 0; i++)
1966         switch (size)
1967           {
1968           case SN:
1969           case SB:
1970           default:
1971             fix_operand_size (&operand[i], 1);
1972             break;
1973           case SW:
1974             fix_operand_size (&operand[i], 2);
1975             break;
1976           case SL:
1977             fix_operand_size (&operand[i], 4);
1978             break;
1979           }
1980     }
1981
1982   instruction = get_specific (instruction, operand, size);
1983
1984   if (instruction == 0)
1985     {
1986       /* Couldn't find an opcode which matched the operands.  */
1987       char *where = frag_more (2);
1988
1989       where[0] = 0x0;
1990       where[1] = 0x0;
1991       clever_message (prev_instruction, operand);
1992
1993       return;
1994     }
1995
1996   build_bytes (instruction, operand);
1997
1998 #ifdef BFD_ASSEMBLER
1999   dwarf2_emit_insn (instruction->length);
2000 #endif
2001 }
2002
2003 #ifndef BFD_ASSEMBLER
2004 void
2005 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
2006 {
2007   printf (_("call to tc_crawl_symbol_chain \n"));
2008 }
2009 #endif
2010
2011 symbolS *
2012 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2013 {
2014   return 0;
2015 }
2016
2017 #ifndef BFD_ASSEMBLER
2018 void
2019 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
2020 {
2021   printf (_("call to tc_headers_hook \n"));
2022 }
2023 #endif
2024
2025 /* Various routines to kill one day */
2026 /* Equal to MAX_PRECISION in atof-ieee.c */
2027 #define MAX_LITTLENUMS 6
2028
2029 /* Turn a string in input_line_pointer into a floating point constant
2030    of type TYPE, and store the appropriate bytes in *LITP.  The number
2031    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
2032    returned, or NULL on OK.  */
2033
2034 char *
2035 md_atof (int type, char *litP, int *sizeP)
2036 {
2037   int prec;
2038   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2039   LITTLENUM_TYPE *wordP;
2040   char *t;
2041
2042   switch (type)
2043     {
2044     case 'f':
2045     case 'F':
2046     case 's':
2047     case 'S':
2048       prec = 2;
2049       break;
2050
2051     case 'd':
2052     case 'D':
2053     case 'r':
2054     case 'R':
2055       prec = 4;
2056       break;
2057
2058     case 'x':
2059     case 'X':
2060       prec = 6;
2061       break;
2062
2063     case 'p':
2064     case 'P':
2065       prec = 6;
2066       break;
2067
2068     default:
2069       *sizeP = 0;
2070       return _("Bad call to MD_ATOF()");
2071     }
2072   t = atof_ieee (input_line_pointer, type, words);
2073   if (t)
2074     input_line_pointer = t;
2075
2076   *sizeP = prec * sizeof (LITTLENUM_TYPE);
2077   for (wordP = words; prec--;)
2078     {
2079       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
2080       litP += sizeof (LITTLENUM_TYPE);
2081     }
2082   return 0;
2083 }
2084 \f
2085 const char *md_shortopts = "";
2086 struct option md_longopts[] = {
2087   {NULL, no_argument, NULL, 0}
2088 };
2089
2090 size_t md_longopts_size = sizeof (md_longopts);
2091
2092 int
2093 md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
2094 {
2095   return 0;
2096 }
2097
2098 void
2099 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
2100 {
2101 }
2102 \f
2103 void tc_aout_fix_to_chars (void);
2104
2105 void
2106 tc_aout_fix_to_chars (void)
2107 {
2108   printf (_("call to tc_aout_fix_to_chars \n"));
2109   abort ();
2110 }
2111
2112 void
2113 md_convert_frag (
2114 #ifdef BFD_ASSEMBLER
2115                  bfd *headers ATTRIBUTE_UNUSED,
2116 #else
2117                  object_headers *headers ATTRIBUTE_UNUSED,
2118 #endif
2119                  segT seg ATTRIBUTE_UNUSED,
2120                  fragS *fragP ATTRIBUTE_UNUSED)
2121 {
2122   printf (_("call to md_convert_frag \n"));
2123   abort ();
2124 }
2125
2126 #ifdef BFD_ASSEMBLER
2127 valueT
2128 md_section_align (segT segment, valueT size)
2129 {
2130   int align = bfd_get_section_alignment (stdoutput, segment);
2131   return ((size + (1 << align) - 1) & (-1 << align));
2132 }
2133 #else
2134 valueT
2135 md_section_align (segT seg, valueT size)
2136 {
2137   return ((size + (1 << section_alignment[(int) seg]) - 1)
2138           & (-1 << section_alignment[(int) seg]));
2139 }
2140 #endif
2141
2142
2143 void
2144 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2145 {
2146   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2147   long val = *valP;
2148
2149   switch (fixP->fx_size)
2150     {
2151     case 1:
2152       *buf++ = val;
2153       break;
2154     case 2:
2155       *buf++ = (val >> 8);
2156       *buf++ = val;
2157       break;
2158     case 4:
2159       *buf++ = (val >> 24);
2160       *buf++ = (val >> 16);
2161       *buf++ = (val >> 8);
2162       *buf++ = val;
2163       break;
2164     default:
2165       abort ();
2166     }
2167
2168   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
2169     fixP->fx_done = 1;
2170 }
2171
2172 int
2173 md_estimate_size_before_relax (register fragS *fragP ATTRIBUTE_UNUSED,
2174                                register segT segment_type ATTRIBUTE_UNUSED)
2175 {
2176   printf (_("call tomd_estimate_size_before_relax \n"));
2177   abort ();
2178 }
2179
2180 /* Put number into target byte order.  */
2181 void
2182 md_number_to_chars (char *ptr, valueT use, int nbytes)
2183 {
2184   number_to_chars_bigendian (ptr, use, nbytes);
2185 }
2186
2187 long
2188 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
2189 {
2190   abort ();
2191 }
2192
2193 #ifndef BFD_ASSEMBLER
2194 void
2195 tc_reloc_mangle (fixS *fix_ptr, struct internal_reloc *intr, bfd_vma base)
2196 {
2197   symbolS *symbol_ptr;
2198
2199   symbol_ptr = fix_ptr->fx_addsy;
2200
2201   /* If this relocation is attached to a symbol then it's ok
2202      to output it.  */
2203   if (fix_ptr->fx_r_type == TC_CONS_RELOC)
2204     {
2205       /* cons likes to create reloc32's whatever the size of the reloc..
2206        */
2207       switch (fix_ptr->fx_size)
2208         {
2209         case 4:
2210           intr->r_type = R_RELLONG;
2211           break;
2212         case 2:
2213           intr->r_type = R_RELWORD;
2214           break;
2215         case 1:
2216           intr->r_type = R_RELBYTE;
2217           break;
2218         default:
2219           abort ();
2220         }
2221     }
2222   else
2223     {
2224       intr->r_type = fix_ptr->fx_r_type;
2225     }
2226
2227   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
2228   intr->r_offset = fix_ptr->fx_offset;
2229
2230   if (symbol_ptr)
2231     {
2232       if (symbol_ptr->sy_number != -1)
2233         intr->r_symndx = symbol_ptr->sy_number;
2234       else
2235         {
2236           symbolS *segsym;
2237
2238           /* This case arises when a reference is made to `.'.  */
2239           segsym = seg_info (S_GET_SEGMENT (symbol_ptr))->dot;
2240           if (segsym == NULL)
2241             intr->r_symndx = -1;
2242           else
2243             {
2244               intr->r_symndx = segsym->sy_number;
2245               intr->r_offset += S_GET_VALUE (symbol_ptr);
2246             }
2247         }
2248     }
2249   else
2250     intr->r_symndx = -1;
2251 }
2252 #else /* BFD_ASSEMBLER */
2253 arelent *
2254 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2255 {
2256   arelent *rel;
2257   bfd_reloc_code_real_type r_type;
2258
2259   if (fixp->fx_addsy && fixp->fx_subsy)
2260     {
2261       if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
2262           || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
2263         {
2264           as_bad_where (fixp->fx_file, fixp->fx_line,
2265                         "Difference of symbols in different sections is not supported");
2266           return NULL;
2267         }
2268     }
2269
2270   rel = (arelent *) xmalloc (sizeof (arelent));
2271   rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2272   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2273   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2274   rel->addend = fixp->fx_offset;
2275
2276   r_type = fixp->fx_r_type;
2277
2278 #define DEBUG 0
2279 #if DEBUG
2280   fprintf (stderr, "%s\n", bfd_get_reloc_code_name (r_type));
2281   fflush(stderr);
2282 #endif
2283   rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
2284   if (rel->howto == NULL)
2285     {
2286       as_bad_where (fixp->fx_file, fixp->fx_line,
2287                     _("Cannot represent relocation type %s"),
2288                     bfd_get_reloc_code_name (r_type));
2289       return NULL;
2290     }
2291
2292   return rel;
2293 }
2294 #endif