* tc-h8300.c: If OBJ_ELF, include elf/h8.h, and define
[deliverable/binutils-gdb.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 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 "subsegs.h"
27 #include "bfd.h"
28 #define DEFINE_TABLE
29 #define h8_opcodes ops
30 #include "opcode/h8300.h"
31 #include <ctype.h>
32
33 #ifdef OBJ_ELF
34 #include "elf/h8.h"
35
36 #define R_MOV24B1 BFD_RELOC_H8_DIR24A8
37 #define R_MOVL1 BFD_RELOC_H8_DIR32A16
38 #define R_MOV24B1 BFD_RELOC_H8_DIR24A8
39 #define R_MOVL1 BFD_RELOC_H8_DIR32A16
40 #define R_RELLONG BFD_RELOC_32
41 #define R_MOV16B1 BFD_RELOC_H8_DIR16A8
42 #define R_RELWORD BFD_RELOC_16
43 #define R_RELBYTE BFD_RELOC_8
44 #define R_PCRWORD BFD_RELOC_16_PCREL
45 #define R_PCRBYTE BFD_RELOC_8_PCREL
46 #define R_JMPL1 BFD_RELOC_H8_DIR24R8
47 #endif
48
49 const char comment_chars[] = ";";
50 const char line_comment_chars[] = "#";
51 const char line_separator_chars[] = "";
52
53 /* This table describes all the machine specific pseudo-ops the assembler
54 has to support. The fields are:
55 pseudo-op name without dot
56 function to call to execute this pseudo-op
57 Integer arg to pass to the function
58 */
59
60 void cons ();
61
62 int Hmode;
63 int Smode;
64 #define PSIZE (Hmode ? L_32 : L_16)
65 #define DMODE (L_16)
66 #define DSYMMODE (Hmode ? L_24 : L_16)
67 int bsize = L_8; /* default branch displacement */
68
69 void
70 h8300hmode ()
71 {
72 Hmode = 1;
73 Smode = 0;
74 #ifdef BFD_ASSEMBLER
75 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
76 as_warn (_("could not set architecture and machine"));
77 #endif
78 }
79
80 void
81 h8300smode ()
82 {
83 Smode = 1;
84 Hmode = 1;
85 #ifdef BFD_ASSEMBLER
86 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
87 as_warn (_("could not set architecture and machine"));
88 #endif
89 }
90
91 void
92 sbranch (size)
93 int size;
94 {
95 bsize = size;
96 }
97
98 static void
99 pint ()
100 {
101 cons (Hmode ? 4 : 2);
102 }
103
104 const pseudo_typeS md_pseudo_table[] =
105 {
106 {"h8300h", h8300hmode, 0},
107 {"h8300s", h8300smode, 0},
108 {"sbranch", sbranch, L_8},
109 {"lbranch", sbranch, L_16},
110
111 {"int", pint, 0},
112 {"data.b", cons, 1},
113 {"data.w", cons, 2},
114 {"data.l", cons, 4},
115 {"form", listing_psize, 0},
116 {"heading", listing_title, 0},
117 {"import", s_ignore, 0},
118 {"page", listing_eject, 0},
119 {"program", s_ignore, 0},
120 {0, 0, 0}
121 };
122
123 const int md_reloc_size;
124
125 const char EXP_CHARS[] = "eE";
126
127 /* Chars that mean this number is a floating point constant */
128 /* As in 0f12.456 */
129 /* or 0d1.2345e12 */
130 const char FLT_CHARS[] = "rRsSfFdDxXpP";
131
132 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
133
134 /* This function is called once, at assembler startup time. This
135 should set up all the tables, etc. that the MD part of the assembler
136 needs. */
137 void
138 md_begin ()
139 {
140 struct h8_opcode *opcode;
141 char prev_buffer[100];
142 int idx = 0;
143
144 #ifdef BFD_ASSEMBLER
145 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
146 as_warn (_("could not set architecture and machine"));
147 #endif
148
149 opcode_hash_control = hash_new ();
150 prev_buffer[0] = 0;
151
152 for (opcode = h8_opcodes; opcode->name; opcode++)
153 {
154 /* Strip off any . part when inserting the opcode and only enter
155 unique codes into the hash table. */
156 char *src = opcode->name;
157 unsigned int len = strlen (src);
158 char *dst = malloc (len + 1);
159 char *buffer = dst;
160
161 opcode->size = 0;
162 while (*src)
163 {
164 if (*src == '.')
165 {
166 src++;
167 opcode->size = *src;
168 break;
169 }
170 *dst++ = *src++;
171 }
172 *dst++ = 0;
173 if (strcmp (buffer, prev_buffer))
174 {
175 hash_insert (opcode_hash_control, buffer, (char *) opcode);
176 strcpy (prev_buffer, buffer);
177 idx++;
178 }
179 opcode->idx = idx;
180
181 /* Find the number of operands. */
182 opcode->noperands = 0;
183 while (opcode->args.nib[opcode->noperands] != E)
184 opcode->noperands++;
185
186 /* Find the length of the opcode in bytes. */
187 opcode->length = 0;
188 while (opcode->data.nib[opcode->length * 2] != E)
189 opcode->length++;
190 }
191
192 linkrelax = 1;
193 }
194
195 struct h8_exp
196 {
197 char *e_beg;
198 char *e_end;
199 expressionS e_exp;
200 };
201
202 int dispreg;
203 int opsize; /* Set when a register size is seen */
204
205 struct h8_op
206 {
207 op_type mode;
208 unsigned reg;
209 expressionS exp;
210 };
211
212 /*
213 parse operands
214 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
215 r0l,r0h,..r7l,r7h
216 @WREG
217 @WREG+
218 @-WREG
219 #const
220 ccr
221 */
222
223 /* Try to parse a reg name. Return the number of chars consumed. */
224
225 static int
226 parse_reg (src, mode, reg, direction)
227 char *src;
228 op_type *mode;
229 unsigned int *reg;
230 int direction;
231
232 {
233 char *end;
234 int len;
235
236 /* Cribbed from get_symbol_end. */
237 if (!is_name_beginner (*src) || *src == '\001')
238 return 0;
239 end = src + 1;
240 while (is_part_of_name (*end) || *end == '\001')
241 end++;
242 len = end - src;
243
244 if (len == 2 && src[0] == 's' && src[1] == 'p')
245 {
246 *mode = PSIZE | REG | direction;
247 *reg = 7;
248 return len;
249 }
250 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
251 {
252 *mode = CCR;
253 *reg = 0;
254 return len;
255 }
256 if (len == 3 && src[0] == 'e' && src[1] == 'x' && src[2] == 'r')
257 {
258 *mode = EXR;
259 *reg = 0;
260 return len;
261 }
262 if (len == 2 && src[0] == 'f' && src[1] == 'p')
263 {
264 *mode = PSIZE | REG | direction;
265 *reg = 6;
266 return len;
267 }
268 if (len == 3 && src[0] == 'e' && src[1] == 'r'
269 && src[2] >= '0' && src[2] <= '7')
270 {
271 *mode = L_32 | REG | direction;
272 *reg = src[2] - '0';
273 if (!Hmode)
274 as_warn (_("Reg not valid for H8/300"));
275 return len;
276 }
277 if (len == 2 && src[0] == 'e' && src[1] >= '0' && src[1] <= '7')
278 {
279 *mode = L_16 | REG | direction;
280 *reg = src[1] - '0' + 8;
281 if (!Hmode)
282 as_warn (_("Reg not valid for H8/300"));
283 return len;
284 }
285
286 if (src[0] == 'r')
287 {
288 if (src[1] >= '0' && src[1] <= '7')
289 {
290 if (len == 3 && src[2] == 'l')
291 {
292 *mode = L_8 | REG | direction;
293 *reg = (src[1] - '0') + 8;
294 return len;
295 }
296 if (len == 3 && src[2] == 'h')
297 {
298 *mode = L_8 | REG | direction;
299 *reg = (src[1] - '0');
300 return len;
301 }
302 if (len == 2)
303 {
304 *mode = L_16 | REG | direction;
305 *reg = (src[1] - '0');
306 return len;
307 }
308 }
309 }
310
311 return 0;
312 }
313
314 static char *
315 parse_exp (s, op)
316 char *s;
317 expressionS *op;
318 {
319 char *save = input_line_pointer;
320 char *new;
321
322 input_line_pointer = s;
323 expression (op);
324 if (op->X_op == O_absent)
325 as_bad (_("missing operand"));
326 new = input_line_pointer;
327 input_line_pointer = save;
328 return new;
329 }
330
331 static char *
332 skip_colonthing (ptr, exp, mode)
333 char *ptr;
334 expressionS *exp ATTRIBUTE_UNUSED;
335 int *mode;
336 {
337 if (*ptr == ':')
338 {
339 ptr++;
340 *mode &= ~SIZE;
341 if (*ptr == '8')
342 {
343 ptr++;
344 /* ff fill any 8 bit quantity */
345 /* exp->X_add_number -= 0x100; */
346 *mode |= L_8;
347 }
348 else
349 {
350 if (*ptr == '2')
351 {
352 *mode |= L_24;
353 }
354 else if (*ptr == '3')
355 {
356 *mode |= L_32;
357 }
358 else if (*ptr == '1')
359 {
360 *mode |= L_16;
361 }
362 while (isdigit (*ptr))
363 ptr++;
364 }
365 }
366 return ptr;
367 }
368
369 /* The many forms of operand:
370
371 Rn Register direct
372 @Rn Register indirect
373 @(exp[:16], Rn) Register indirect with displacement
374 @Rn+
375 @-Rn
376 @aa:8 absolute 8 bit
377 @aa:16 absolute 16 bit
378 @aa absolute 16 bit
379
380 #xx[:size] immediate data
381 @(exp:[8], pc) pc rel
382 @@aa[:8] memory indirect
383
384 */
385
386 char *
387 colonmod24 (op, src)
388 struct h8_op *op;
389 char *src;
390
391 {
392 int mode = 0;
393 src = skip_colonthing (src, &op->exp, &mode);
394
395 if (!mode)
396 {
397 /* Choose a default mode. */
398 if (op->exp.X_add_number < -32768
399 || op->exp.X_add_number > 32767)
400 {
401 if (Hmode)
402 mode = L_24;
403 else
404 mode = L_16;
405 }
406 else if (op->exp.X_add_symbol
407 || op->exp.X_op_symbol)
408 mode = DSYMMODE;
409 else
410 mode = DMODE;
411 }
412 op->mode |= mode;
413 return src;
414
415 }
416
417 static void
418 get_operand (ptr, op, dst, direction)
419 char **ptr;
420 struct h8_op *op;
421 unsigned int dst ATTRIBUTE_UNUSED;
422 int direction;
423 {
424 char *src = *ptr;
425 op_type mode;
426 unsigned int num;
427 unsigned int len;
428
429 op->mode = E;
430
431 /* Gross. Gross. ldm and stm have a format not easily handled
432 by get_operand. We deal with it explicitly here. */
433 if (src[0] == 'e' && src[1] == 'r' && isdigit (src[2])
434 && src[3] == '-' && src[4] == 'e' && src[5] == 'r' && isdigit (src[6]))
435 {
436 int low, high;
437
438 low = src[2] - '0';
439 high = src[6] - '0';
440
441 if (high < low)
442 as_bad (_("Invalid register list for ldm/stm\n"));
443
444 if (low % 2)
445 as_bad (_("Invalid register list for ldm/stm\n"));
446
447 if (high - low > 3)
448 as_bad (_("Invalid register list for ldm/stm\n"));
449
450 if (high - low != 1
451 && low % 4)
452 as_bad (_("Invalid register list for ldm/stm\n"));
453
454 /* Even sicker. We encode two registers into op->reg. One
455 for the low register to save, the other for the high
456 register to save; we also set the high bit in op->reg
457 so we know this is "very special". */
458 op->reg = 0x80000000 | (high << 8) | low;
459 op->mode = REG;
460 *ptr = src + 7;
461 return;
462 }
463
464 len = parse_reg (src, &op->mode, &op->reg, direction);
465 if (len)
466 {
467 *ptr = src + len;
468 return;
469 }
470
471 if (*src == '@')
472 {
473 src++;
474 if (*src == '@')
475 {
476 src++;
477 src = parse_exp (src, &op->exp);
478
479 src = skip_colonthing (src, &op->exp, &op->mode);
480
481 *ptr = src;
482
483 op->mode = MEMIND;
484 return;
485 }
486
487 if (*src == '-')
488 {
489 src++;
490 len = parse_reg (src, &mode, &num, direction);
491 if (len == 0)
492 {
493 /* Oops, not a reg after all, must be ordinary exp. */
494 src--;
495 /* Must be a symbol. */
496 op->mode = ABS | PSIZE | direction;
497 *ptr = skip_colonthing (parse_exp (src, &op->exp),
498 &op->exp, &op->mode);
499
500 return;
501 }
502
503 if ((mode & SIZE) != PSIZE)
504 as_bad (_("Wrong size pointer register for architecture."));
505 op->mode = RDDEC;
506 op->reg = num;
507 *ptr = src + len;
508 return;
509 }
510 if (*src == '(')
511 {
512 /* Disp. */
513 src++;
514
515 /* Start off assuming a 16 bit offset. */
516
517 src = parse_exp (src, &op->exp);
518
519 src = colonmod24 (op, src);
520
521 if (*src == ')')
522 {
523 src++;
524 op->mode |= ABS | direction;
525 *ptr = src;
526 return;
527 }
528
529 if (*src != ',')
530 {
531 as_bad (_("expected @(exp, reg16)"));
532 return;
533
534 }
535 src++;
536
537 len = parse_reg (src, &mode, &op->reg, direction);
538 if (len == 0 || !(mode & REG))
539 {
540 as_bad (_("expected @(exp, reg16)"));
541 return;
542 }
543 op->mode |= DISP | direction;
544 dispreg = op->reg;
545 src += len;
546 src = skip_colonthing (src, &op->exp, &op->mode);
547
548 if (*src != ')' && '(')
549 {
550 as_bad (_("expected @(exp, reg16)"));
551 return;
552 }
553 *ptr = src + 1;
554
555 return;
556 }
557 len = parse_reg (src, &mode, &num, direction);
558
559 if (len)
560 {
561 src += len;
562 if (*src == '+')
563 {
564 src++;
565 if ((mode & SIZE) != PSIZE)
566 as_bad (_("Wrong size pointer register for architecture."));
567 op->mode = RSINC;
568 op->reg = num;
569 *ptr = src;
570 return;
571 }
572 if ((mode & SIZE) != PSIZE)
573 as_bad (_("Wrong size pointer register for architecture."));
574
575 op->mode = direction | IND | PSIZE;
576 op->reg = num;
577 *ptr = src;
578
579 return;
580 }
581 else
582 {
583 /* must be a symbol */
584
585 op->mode = ABS | direction;
586 src = parse_exp (src, &op->exp);
587
588 *ptr = colonmod24 (op, src);
589
590 return;
591 }
592 }
593
594 if (*src == '#')
595 {
596 src++;
597 op->mode = IMM;
598 src = parse_exp (src, &op->exp);
599 *ptr = skip_colonthing (src, &op->exp, &op->mode);
600
601 return;
602 }
603 else if (strncmp (src, "mach", 4) == 0
604 || strncmp (src, "macl", 4) == 0)
605 {
606 op->reg = src[3] == 'l';
607 op->mode = MACREG;
608 *ptr = src + 4;
609 return;
610 }
611 else
612 {
613 src = parse_exp (src, &op->exp);
614 /* Trailing ':' size ? */
615 if (*src == ':')
616 {
617 if (src[1] == '1' && src[2] == '6')
618 {
619 op->mode = PCREL | L_16;
620 src += 3;
621 }
622 else if (src[1] == '8')
623 {
624 op->mode = PCREL | L_8;
625 src += 2;
626 }
627 else
628 {
629 as_bad (_("expect :8 or :16 here"));
630 }
631 }
632 else
633 {
634 op->mode = PCREL | bsize;
635 }
636 *ptr = src;
637 }
638 }
639
640 static char *
641 get_operands (noperands, op_end, operand)
642 unsigned int noperands;
643 char *op_end;
644 struct h8_op *operand;
645 {
646 char *ptr = op_end;
647
648 switch (noperands)
649 {
650 case 0:
651 operand[0].mode = 0;
652 operand[1].mode = 0;
653 break;
654
655 case 1:
656 ptr++;
657 get_operand (&ptr, operand + 0, 0, SRC);
658 if (*ptr == ',')
659 {
660 ptr++;
661 get_operand (&ptr, operand + 1, 1, DST);
662 }
663 else
664 {
665 operand[1].mode = 0;
666 }
667 break;
668
669 case 2:
670 ptr++;
671 get_operand (&ptr, operand + 0, 0, SRC);
672 if (*ptr == ',')
673 ptr++;
674 get_operand (&ptr, operand + 1, 1, DST);
675 break;
676
677 default:
678 abort ();
679 }
680
681 return ptr;
682 }
683
684 /* Passed a pointer to a list of opcodes which use different
685 addressing modes, return the opcode which matches the opcodes
686 provided. */
687 static struct h8_opcode *
688 get_specific (opcode, operands, size)
689 struct h8_opcode *opcode;
690 struct h8_op *operands;
691 int size;
692 {
693 struct h8_opcode *this_try = opcode;
694 int found = 0;
695
696 unsigned int this_index = opcode->idx;
697
698 /* There's only one ldm/stm and it's easier to just
699 get out quick for them. */
700 if (strcmp (opcode->name, "stm.l") == 0
701 || strcmp (opcode->name, "ldm.l") == 0)
702 return this_try;
703
704 while (this_index == opcode->idx && !found)
705 {
706 found = 1;
707
708 this_try = opcode++;
709 if (this_try->noperands == 0)
710 {
711 int this_size;
712
713 this_size = this_try->how & SN;
714 if (this_size != size && (this_size != SB || size != SN))
715 found = 0;
716 }
717 else
718 {
719 unsigned int i;
720
721 for (i = 0; i < this_try->noperands && found; i++)
722 {
723 op_type op = this_try->args.nib[i];
724 int x = operands[i].mode;
725
726 if ((op & (DISP | REG)) == (DISP | REG)
727 && ((x & (DISP | REG)) == (DISP | REG)))
728 {
729 dispreg = operands[i].reg;
730 }
731 else if (op & REG)
732 {
733 if (!(x & REG))
734 found = 0;
735
736 if (x & L_P)
737 x = (x & ~L_P) | (Hmode ? L_32 : L_16);
738 if (op & L_P)
739 op = (op & ~L_P) | (Hmode ? L_32 : L_16);
740
741 opsize = op & SIZE;
742
743 /* The size of the reg is v important. */
744 if ((op & SIZE) != (x & SIZE))
745 found = 0;
746 }
747 else if ((op & ABSJMP) && (x & ABS))
748 {
749 operands[i].mode &= ~ABS;
750 operands[i].mode |= ABSJMP;
751 /* But it may not be 24 bits long. */
752 if (!Hmode)
753 {
754 operands[i].mode &= ~SIZE;
755 operands[i].mode |= L_16;
756 }
757 }
758 else if ((op & (KBIT | DBIT)) && (x & IMM))
759 {
760 /* This is ok if the immediate value is sensible. */
761 }
762 else if (op & PCREL)
763 {
764 /* The size of the displacement is important. */
765 if ((op & SIZE) != (x & SIZE))
766 found = 0;
767 }
768 else if ((op & (DISP | IMM | ABS))
769 && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
770 {
771 /* Promote a L_24 to L_32 if it makes us match. */
772 if ((x & L_24) && (op & L_32))
773 {
774 x &= ~L_24;
775 x |= L_32;
776 }
777 /* Promote an L8 to L_16 if it makes us match. */
778 if (op & ABS && op & L_8 && op & DISP)
779 {
780 if (x & L_16)
781 found = 1;
782 }
783 else if ((x & SIZE) != 0
784 && ((op & SIZE) != (x & SIZE)))
785 found = 0;
786 }
787 else if ((op & MACREG) != (x & MACREG))
788 {
789 found = 0;
790 }
791 else if ((op & MODE) != (x & MODE))
792 {
793 found = 0;
794 }
795 }
796 }
797 }
798 if (found)
799 return this_try;
800 else
801 return 0;
802 }
803
804 static void
805 check_operand (operand, width, string)
806 struct h8_op *operand;
807 unsigned int width;
808 char *string;
809 {
810 if (operand->exp.X_add_symbol == 0
811 && operand->exp.X_op_symbol == 0)
812 {
813 /* No symbol involved, let's look at offset, it's dangerous if
814 any of the high bits are not 0 or ff's, find out by oring or
815 anding with the width and seeing if the answer is 0 or all
816 fs. */
817
818 if ((operand->exp.X_add_number & ~width) != 0 &&
819 (operand->exp.X_add_number | width) != (~0))
820 {
821 if (width == 255
822 && (operand->exp.X_add_number & 0xff00) == 0xff00)
823 {
824 /* Just ignore this one - which happens when trying to
825 fit a 16 bit address truncated into an 8 bit address
826 of something like bset. */
827 }
828 else
829 {
830 as_warn (_("operand %s0x%lx out of range."), string,
831 (unsigned long) operand->exp.X_add_number);
832 }
833 }
834 }
835 }
836
837 /* RELAXMODE has one of 3 values:
838
839 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
840
841 1 Output a relaxable 24bit absolute mov.w address relocation
842 (may relax into a 16bit absolute address).
843
844 2 Output a relaxable 16/24 absolute mov.b address relocation
845 (may relax into an 8bit absolute address). */
846
847 static void
848 do_a_fix_imm (offset, operand, relaxmode)
849 int offset;
850 struct h8_op *operand;
851 int relaxmode;
852 {
853 int idx;
854 int size;
855 int where;
856
857 char *t = operand->mode & IMM ? "#" : "@";
858
859 if (operand->exp.X_add_symbol == 0)
860 {
861 char *bytes = frag_now->fr_literal + offset;
862 switch (operand->mode & SIZE)
863 {
864 case L_2:
865 check_operand (operand, 0x3, t);
866 bytes[0] |= (operand->exp.X_add_number) << 4;
867 break;
868 case L_3:
869 check_operand (operand, 0x7, t);
870 bytes[0] |= (operand->exp.X_add_number) << 4;
871 break;
872 case L_8:
873 check_operand (operand, 0xff, t);
874 bytes[0] = operand->exp.X_add_number;
875 break;
876 case L_16:
877 check_operand (operand, 0xffff, t);
878 bytes[0] = operand->exp.X_add_number >> 8;
879 bytes[1] = operand->exp.X_add_number >> 0;
880 break;
881 case L_24:
882 check_operand (operand, 0xffffff, t);
883 bytes[0] = operand->exp.X_add_number >> 16;
884 bytes[1] = operand->exp.X_add_number >> 8;
885 bytes[2] = operand->exp.X_add_number >> 0;
886 break;
887
888 case L_32:
889 /* This should be done with bfd. */
890 bytes[0] = operand->exp.X_add_number >> 24;
891 bytes[1] = operand->exp.X_add_number >> 16;
892 bytes[2] = operand->exp.X_add_number >> 8;
893 bytes[3] = operand->exp.X_add_number >> 0;
894 if (relaxmode != 0)
895 {
896 idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
897 fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
898 }
899 break;
900 }
901 }
902 else
903 {
904 switch (operand->mode & SIZE)
905 {
906 case L_24:
907 case L_32:
908 size = 4;
909 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
910 if (relaxmode == 2)
911 idx = R_MOV24B1;
912 else if (relaxmode == 1)
913 idx = R_MOVL1;
914 else
915 idx = R_RELLONG;
916 break;
917 default:
918 as_bad (_("Can't work out size of operand.\n"));
919 case L_16:
920 size = 2;
921 where = 0;
922 if (relaxmode == 2)
923 idx = R_MOV16B1;
924 else
925 idx = R_RELWORD;
926 operand->exp.X_add_number =
927 ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
928 break;
929 case L_8:
930 size = 1;
931 where = 0;
932 idx = R_RELBYTE;
933 operand->exp.X_add_number =
934 ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
935 }
936
937 fix_new_exp (frag_now,
938 offset + where,
939 size,
940 &operand->exp,
941 0,
942 idx);
943 }
944 }
945
946 /* Now we know what sort of opcodes it is, let's build the bytes. */
947 static void
948 build_bytes (this_try, operand)
949 struct h8_opcode *this_try;
950 struct h8_op *operand;
951 {
952 unsigned int i;
953
954 char *output = frag_more (this_try->length);
955 op_type *nibble_ptr = this_try->data.nib;
956 op_type c;
957 unsigned int nibble_count = 0;
958 int absat;
959 int immat;
960 int nib;
961 int movb = 0;
962 char asnibbles[30];
963 char *p = asnibbles;
964
965 if (!(this_try->inbase || Hmode))
966 as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
967 this_try->name);
968
969 while (*nibble_ptr != E)
970 {
971 int d;
972 c = *nibble_ptr++;
973
974 d = (c & (DST | SRC_IN_DST)) != 0;
975
976 if (c < 16)
977 {
978 nib = c;
979 }
980 else
981 {
982 if (c & (REG | IND | INC | DEC))
983 {
984 nib = operand[d].reg;
985 }
986 else if ((c & DISPREG) == (DISPREG))
987 {
988 nib = dispreg;
989 }
990 else if (c & ABS)
991 {
992 operand[d].mode = c;
993 absat = nibble_count / 2;
994 nib = 0;
995 }
996 else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
997 {
998 operand[d].mode = c;
999 immat = nibble_count / 2;
1000 nib = 0;
1001 }
1002 else if (c & IGNORE)
1003 {
1004 nib = 0;
1005 }
1006 else if (c & DBIT)
1007 {
1008 switch (operand[0].exp.X_add_number)
1009 {
1010 case 1:
1011 nib = c;
1012 break;
1013 case 2:
1014 nib = 0x8 | c;
1015 break;
1016 default:
1017 as_bad (_("Need #1 or #2 here"));
1018 }
1019 }
1020 else if (c & KBIT)
1021 {
1022 switch (operand[0].exp.X_add_number)
1023 {
1024 case 1:
1025 nib = 0;
1026 break;
1027 case 2:
1028 nib = 8;
1029 break;
1030 case 4:
1031 if (!Hmode)
1032 as_warn (_("#4 not valid on H8/300."));
1033 nib = 9;
1034 break;
1035
1036 default:
1037 as_bad (_("Need #1 or #2 here"));
1038 break;
1039 }
1040 /* Stop it making a fix. */
1041 operand[0].mode = 0;
1042 }
1043
1044 if (c & MEMRELAX)
1045 {
1046 operand[d].mode |= MEMRELAX;
1047 }
1048
1049 if (c & B31)
1050 {
1051 nib |= 0x8;
1052 }
1053
1054 if (c & MACREG)
1055 {
1056 if (operand[0].mode == MACREG)
1057 /* stmac has mac[hl] as the first operand. */
1058 nib = 2 + operand[0].reg;
1059 else
1060 /* ldmac has mac[hl] as the second operand. */
1061 nib = 2 + operand[1].reg;
1062 }
1063 }
1064 nibble_count++;
1065
1066 *p++ = nib;
1067 }
1068
1069 /* Disgusting. Why, oh why didn't someone ask us for advice
1070 on the assembler format. */
1071 if (strcmp (this_try->name, "stm.l") == 0
1072 || strcmp (this_try->name, "ldm.l") == 0)
1073 {
1074 int high, low;
1075 high = (operand[this_try->name[0] == 'l' ? 1 : 0].reg >> 8) & 0xf;
1076 low = operand[this_try->name[0] == 'l' ? 1 : 0].reg & 0xf;
1077
1078 asnibbles[2] = high - low;
1079 asnibbles[7] = (this_try->name[0] == 'l') ? high : low;
1080 }
1081
1082 for (i = 0; i < this_try->length; i++)
1083 {
1084 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1085 }
1086
1087 /* Note if this is a movb instruction -- there's a special relaxation
1088 which only applies to them. */
1089 if (strcmp (this_try->name, "mov.b") == 0)
1090 movb = 1;
1091
1092 /* Output any fixes. */
1093 for (i = 0; i < 2; i++)
1094 {
1095 int x = operand[i].mode;
1096
1097 if (x & (IMM | DISP))
1098 {
1099 do_a_fix_imm (output - frag_now->fr_literal + immat,
1100 operand + i, x & MEMRELAX != 0);
1101 }
1102 else if (x & ABS)
1103 {
1104 do_a_fix_imm (output - frag_now->fr_literal + absat,
1105 operand + i, x & MEMRELAX ? movb + 1 : 0);
1106 }
1107 else if (x & PCREL)
1108 {
1109 int size16 = x & L_16;
1110 int where = size16 ? 2 : 1;
1111 int size = size16 ? 2 : 1;
1112 int type = size16 ? R_PCRWORD : R_PCRBYTE;
1113
1114 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1115
1116 if (operand[i].exp.X_add_number & 1)
1117 {
1118 as_warn (_("branch operand has odd offset (%lx)\n"),
1119 (unsigned long) operand->exp.X_add_number);
1120 }
1121
1122 operand[i].exp.X_add_number -= 1;
1123 operand[i].exp.X_add_number =
1124 ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1125
1126 fix_new_exp (frag_now,
1127 output - frag_now->fr_literal + where,
1128 size,
1129 &operand[i].exp,
1130 1,
1131 type);
1132 }
1133 else if (x & MEMIND)
1134 {
1135 check_operand (operand + i, 0xff, "@@");
1136 fix_new_exp (frag_now,
1137 output - frag_now->fr_literal + 1,
1138 1,
1139 &operand[i].exp,
1140 0,
1141 R_MEM_INDIRECT);
1142 }
1143 else if (x & ABSJMP)
1144 {
1145 /* This jmp may be a jump or a branch. */
1146
1147 check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1148 if (operand[i].exp.X_add_number & 1)
1149 {
1150 as_warn (_("branch operand has odd offset (%lx)\n"),
1151 (unsigned long) operand->exp.X_add_number);
1152 }
1153 if (!Hmode)
1154 operand[i].exp.X_add_number =
1155 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1156 fix_new_exp (frag_now,
1157 output - frag_now->fr_literal,
1158 4,
1159 &operand[i].exp,
1160 0,
1161 R_JMPL1);
1162 }
1163 }
1164 }
1165
1166 /* Try to give an intelligent error message for common and simple to
1167 detect errors. */
1168 static void
1169 clever_message (opcode, operand)
1170 struct h8_opcode *opcode;
1171 struct h8_op *operand;
1172 {
1173 /* Find out if there was more than one possible opcode. */
1174
1175 if ((opcode + 1)->idx != opcode->idx)
1176 {
1177 unsigned int argn;
1178
1179 /* Only one opcode of this flavour, try to guess which operand
1180 didn't match. */
1181 for (argn = 0; argn < opcode->noperands; argn++)
1182 {
1183 switch (opcode->args.nib[argn])
1184 {
1185 case RD16:
1186 if (operand[argn].mode != RD16)
1187 {
1188 as_bad (_("destination operand must be 16 bit register"));
1189 return;
1190
1191 }
1192 break;
1193
1194 case RS8:
1195 if (operand[argn].mode != RS8)
1196 {
1197 as_bad (_("source operand must be 8 bit register"));
1198 return;
1199 }
1200 break;
1201
1202 case ABS16DST:
1203 if (operand[argn].mode != ABS16DST)
1204 {
1205 as_bad (_("destination operand must be 16bit absolute address"));
1206 return;
1207 }
1208 break;
1209 case RD8:
1210 if (operand[argn].mode != RD8)
1211 {
1212 as_bad (_("destination operand must be 8 bit register"));
1213 return;
1214 }
1215 break;
1216
1217 case ABS16SRC:
1218 if (operand[argn].mode != ABS16SRC)
1219 {
1220 as_bad (_("source operand must be 16bit absolute address"));
1221 return;
1222 }
1223 break;
1224
1225 }
1226 }
1227 }
1228 as_bad (_("invalid operands"));
1229 }
1230
1231 /* This is the guts of the machine-dependent assembler. STR points to
1232 a machine dependent instruction. This function is supposed to emit
1233 the frags/bytes it assembles. */
1234 void
1235 md_assemble (str)
1236 char *str;
1237 {
1238 char *op_start;
1239 char *op_end;
1240 struct h8_op operand[2];
1241 struct h8_opcode *opcode;
1242 struct h8_opcode *prev_opcode;
1243
1244 char *dot = 0;
1245 char c;
1246 int size;
1247
1248 /* Drop leading whitespace. */
1249 while (*str == ' ')
1250 str++;
1251
1252 /* Find the op code end. */
1253 for (op_start = op_end = str;
1254 *op_end != 0 && *op_end != ' ';
1255 op_end++)
1256 {
1257 if (*op_end == '.')
1258 {
1259 dot = op_end + 1;
1260 *op_end = 0;
1261 op_end += 2;
1262 break;
1263 }
1264 }
1265
1266 if (op_end == op_start)
1267 {
1268 as_bad (_("can't find opcode "));
1269 }
1270 c = *op_end;
1271
1272 *op_end = 0;
1273
1274 opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1275 op_start);
1276
1277 if (opcode == NULL)
1278 {
1279 as_bad (_("unknown opcode"));
1280 return;
1281 }
1282
1283 /* We used to set input_line_pointer to the result of get_operands,
1284 but that is wrong. Our caller assumes we don't change it. */
1285
1286 (void) get_operands (opcode->noperands, op_end, operand);
1287 *op_end = c;
1288 prev_opcode = opcode;
1289
1290 size = SN;
1291 if (dot)
1292 {
1293 switch (*dot)
1294 {
1295 case 'b':
1296 size = SB;
1297 break;
1298
1299 case 'w':
1300 size = SW;
1301 break;
1302
1303 case 'l':
1304 size = SL;
1305 break;
1306 }
1307 }
1308 opcode = get_specific (opcode, operand, size);
1309
1310 if (opcode == 0)
1311 {
1312 /* Couldn't find an opcode which matched the operands. */
1313 char *where = frag_more (2);
1314
1315 where[0] = 0x0;
1316 where[1] = 0x0;
1317 clever_message (prev_opcode, operand);
1318
1319 return;
1320 }
1321 if (opcode->size && dot)
1322 {
1323 if (opcode->size != *dot)
1324 {
1325 as_warn (_("mismatch between opcode size and operand size"));
1326 }
1327 }
1328
1329 build_bytes (opcode, operand);
1330 }
1331
1332 void
1333 tc_crawl_symbol_chain (headers)
1334 object_headers *headers ATTRIBUTE_UNUSED;
1335 {
1336 printf (_("call to tc_crawl_symbol_chain \n"));
1337 }
1338
1339 symbolS *
1340 md_undefined_symbol (name)
1341 char *name ATTRIBUTE_UNUSED;
1342 {
1343 return 0;
1344 }
1345
1346 void
1347 tc_headers_hook (headers)
1348 object_headers *headers ATTRIBUTE_UNUSED;
1349 {
1350 printf (_("call to tc_headers_hook \n"));
1351 }
1352
1353 /* Various routines to kill one day */
1354 /* Equal to MAX_PRECISION in atof-ieee.c */
1355 #define MAX_LITTLENUMS 6
1356
1357 /* Turn a string in input_line_pointer into a floating point constant
1358 of type TYPE, and store the appropriate bytes in *LITP. The number
1359 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1360 returned, or NULL on OK. */
1361
1362 char *
1363 md_atof (type, litP, sizeP)
1364 char type;
1365 char *litP;
1366 int *sizeP;
1367 {
1368 int prec;
1369 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1370 LITTLENUM_TYPE *wordP;
1371 char *t;
1372 char *atof_ieee ();
1373
1374 switch (type)
1375 {
1376 case 'f':
1377 case 'F':
1378 case 's':
1379 case 'S':
1380 prec = 2;
1381 break;
1382
1383 case 'd':
1384 case 'D':
1385 case 'r':
1386 case 'R':
1387 prec = 4;
1388 break;
1389
1390 case 'x':
1391 case 'X':
1392 prec = 6;
1393 break;
1394
1395 case 'p':
1396 case 'P':
1397 prec = 6;
1398 break;
1399
1400 default:
1401 *sizeP = 0;
1402 return _("Bad call to MD_ATOF()");
1403 }
1404 t = atof_ieee (input_line_pointer, type, words);
1405 if (t)
1406 input_line_pointer = t;
1407
1408 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1409 for (wordP = words; prec--;)
1410 {
1411 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1412 litP += sizeof (LITTLENUM_TYPE);
1413 }
1414 return 0;
1415 }
1416 \f
1417 CONST char *md_shortopts = "";
1418 struct option md_longopts[] = {
1419 {NULL, no_argument, NULL, 0}
1420 };
1421
1422 size_t md_longopts_size = sizeof (md_longopts);
1423
1424 int
1425 md_parse_option (c, arg)
1426 int c ATTRIBUTE_UNUSED;
1427 char *arg ATTRIBUTE_UNUSED;
1428 {
1429 return 0;
1430 }
1431
1432 void
1433 md_show_usage (stream)
1434 FILE *stream ATTRIBUTE_UNUSED;
1435 {
1436 }
1437 \f
1438 void
1439 tc_aout_fix_to_chars ()
1440 {
1441 printf (_("call to tc_aout_fix_to_chars \n"));
1442 abort ();
1443 }
1444
1445 void
1446 md_convert_frag (headers, seg, fragP)
1447 object_headers *headers ATTRIBUTE_UNUSED;
1448 segT seg ATTRIBUTE_UNUSED;
1449 fragS *fragP ATTRIBUTE_UNUSED;
1450 {
1451 printf (_("call to md_convert_frag \n"));
1452 abort ();
1453 }
1454
1455 valueT
1456 md_section_align (seg, size)
1457 segT seg;
1458 valueT size;
1459 {
1460 return ((size + (1 << section_alignment[(int) seg]) - 1)
1461 & (-1 << section_alignment[(int) seg]));
1462 }
1463
1464 void
1465 md_apply_fix (fixP, val)
1466 fixS *fixP;
1467 long val;
1468 {
1469 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1470
1471 switch (fixP->fx_size)
1472 {
1473 case 1:
1474 *buf++ = val;
1475 break;
1476 case 2:
1477 *buf++ = (val >> 8);
1478 *buf++ = val;
1479 break;
1480 case 4:
1481 *buf++ = (val >> 24);
1482 *buf++ = (val >> 16);
1483 *buf++ = (val >> 8);
1484 *buf++ = val;
1485 break;
1486 default:
1487 abort ();
1488 }
1489 }
1490
1491 int
1492 md_estimate_size_before_relax (fragP, segment_type)
1493 register fragS *fragP ATTRIBUTE_UNUSED;
1494 register segT segment_type ATTRIBUTE_UNUSED;
1495 {
1496 printf (_("call tomd_estimate_size_before_relax \n"));
1497 abort ();
1498 }
1499
1500 /* Put number into target byte order. */
1501 void
1502 md_number_to_chars (ptr, use, nbytes)
1503 char *ptr;
1504 valueT use;
1505 int nbytes;
1506 {
1507 number_to_chars_bigendian (ptr, use, nbytes);
1508 }
1509
1510 long
1511 md_pcrel_from (fixP)
1512 fixS *fixP ATTRIBUTE_UNUSED;
1513 {
1514 abort ();
1515 }
1516
1517 void
1518 tc_reloc_mangle (fix_ptr, intr, base)
1519 fixS *fix_ptr;
1520 struct internal_reloc *intr;
1521 bfd_vma base;
1522
1523 {
1524 symbolS *symbol_ptr;
1525
1526 symbol_ptr = fix_ptr->fx_addsy;
1527
1528 /* If this relocation is attached to a symbol then it's ok
1529 to output it. */
1530 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1531 {
1532 /* cons likes to create reloc32's whatever the size of the reloc..
1533 */
1534 switch (fix_ptr->fx_size)
1535 {
1536 case 4:
1537 intr->r_type = R_RELLONG;
1538 break;
1539 case 2:
1540 intr->r_type = R_RELWORD;
1541 break;
1542 case 1:
1543 intr->r_type = R_RELBYTE;
1544 break;
1545 default:
1546 abort ();
1547 }
1548 }
1549 else
1550 {
1551 intr->r_type = fix_ptr->fx_r_type;
1552 }
1553
1554 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1555 intr->r_offset = fix_ptr->fx_offset;
1556
1557 if (symbol_ptr)
1558 {
1559 if (symbol_ptr->sy_number != -1)
1560 intr->r_symndx = symbol_ptr->sy_number;
1561 else
1562 {
1563 symbolS *segsym;
1564
1565 /* This case arises when a reference is made to `.'. */
1566 segsym = seg_info (S_GET_SEGMENT (symbol_ptr))->dot;
1567 if (segsym == NULL)
1568 intr->r_symndx = -1;
1569 else
1570 {
1571 intr->r_symndx = segsym->sy_number;
1572 intr->r_offset += S_GET_VALUE (symbol_ptr);
1573 }
1574 }
1575 }
1576 else
1577 intr->r_symndx = -1;
1578 }
This page took 0.07818 seconds and 5 git commands to generate.