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