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