2005-04-12 Paul Brook <paul@codesourcery.com>
[binutils.git] / gas / config / tc-h8500.c
bloba2e7f73115eb75b5d57bbdaa63612d758d1a341c
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.
5 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
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"
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
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. */
43 const pseudo_typeS md_pseudo_table[] =
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}
57 const int md_reloc_size;
59 const char EXP_CHARS[] = "eE";
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";
66 #define C(a,b) ENCODE_RELAX(a, b)
67 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
69 #define GET_WHAT(x) ((x >> 2))
71 #define BYTE_DISP 1
72 #define WORD_DISP 2
73 #define UNDEF_BYTE_DISP 0
74 #define UNDEF_WORD_DISP 3
76 #define BRANCH 1
77 #define SCB_F 2
78 #define SCB_TST 3
79 #define END 4
81 #define BYTE_F 127
82 #define BYTE_B -126
83 #define WORD_F 32767
84 #define WORD_B 32768
86 relax_typeS md_relax_table[C (END, 0)] =
88 { 0, 0, 0, 0 },
89 { 0, 0, 0, 0 },
90 { 0, 0, 0, 0 },
91 { 0, 0, 0, 0 },
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 },
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 },
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 }
113 static struct hash_control *opcode_hash_control; /* Opcode mnemonics. */
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. */
118 void
119 md_begin (void)
121 const h8500_opcode_info *opcode;
122 char prev_buffer[100];
123 int idx = 0;
125 opcode_hash_control = hash_new ();
126 prev_buffer[0] = 0;
128 /* Insert unique names into hash table. */
129 for (opcode = h8500_table; opcode->name; opcode++)
131 if (idx != opcode->idx)
133 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
134 idx++;
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. */
146 static expressionS displacement;/* Displacement expression. */
147 static int immediate_inpage;
148 static expressionS immediate; /* Immediate expression. */
149 static expressionS absolute; /* Absolute expression. */
151 typedef struct
153 int type;
154 unsigned int reg;
155 expressionS exp;
156 int page;
159 h8500_operand_info;
161 /* Try to parse a reg name. Return the number of chars consumed. */
163 static int parse_reg (char *, int *, unsigned int *);
165 static int
166 parse_reg (char *src, int *mode, unsigned int *reg)
168 char *end;
169 int len;
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;
179 if (len == 2 && src[0] == 'r')
181 if (src[1] >= '0' && src[1] <= '7')
183 *mode = RN;
184 *reg = (src[1] - '0');
185 return len;
188 if (len == 2 && src[0] == 's' && src[1] == 'p')
190 *mode = RN;
191 *reg = 7;
192 return len;
194 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
196 *mode = CRB;
197 *reg = 1;
198 return len;
200 if (len == 2 && src[0] == 's' && src[1] == 'r')
202 *mode = CRW;
203 *reg = 0;
204 return len;
206 if (len == 2 && src[0] == 'b' && src[1] == 'r')
208 *mode = CRB;
209 *reg = 3;
210 return len;
212 if (len == 2 && src[0] == 'e' && src[1] == 'p')
214 *mode = CRB;
215 *reg = 4;
216 return len;
218 if (len == 2 && src[0] == 'd' && src[1] == 'p')
220 *mode = CRB;
221 *reg = 5;
222 return len;
224 if (len == 2 && src[0] == 't' && src[1] == 'p')
226 *mode = CRB;
227 *reg = 7;
228 return len;
230 if (len == 2 && src[0] == 'f' && src[1] == 'p')
232 *mode = RN;
233 *reg = 6;
234 return len;
236 return 0;
239 static char *
240 parse_exp (char *s, expressionS *op, int *page)
242 char *save;
243 char *new;
245 save = input_line_pointer;
247 *page = 0;
248 if (s[0] == '%')
250 if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
252 s += 5;
253 *page = 'p';
255 if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
257 s += 5;
258 *page = 'h';
260 else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
262 s += 4;
263 *page = 'o';
267 input_line_pointer = s;
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;
277 typedef enum
279 exp_signed, exp_unsigned, exp_sandu
280 } sign_type;
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)
291 ptr = parse_exp (ptr, &exp->exp, &exp->page);
292 if (*ptr == ':')
294 ptr++;
295 if (*ptr == '8')
297 ptr++;
298 exp->type = size8;
300 else if (ptr[0] == '1' && ptr[1] == '6')
302 ptr += 2;
303 exp->type = size16;
305 else if (ptr[0] == '2' && ptr[1] == '4')
307 if (!size24)
309 as_bad (_(":24 not valid for this opcode"));
311 ptr += 2;
312 exp->type = size24;
314 else
316 as_bad (_("expect :8,:16 or :24"));
317 exp->type = size16;
320 else
322 if (exp->page == 'p')
323 exp->type = IMM8;
324 else if (exp->page == 'h')
325 exp->type = IMM16;
326 else
328 /* Let's work out the size from the context. */
329 int n = exp->exp.X_add_number;
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;
341 return ptr;
344 static int
345 parse_reglist (char *src, h8500_operand_info *op)
347 int mode;
348 unsigned int rn;
349 int mask = 0;
350 unsigned int rm;
351 int idx = 1;
353 /* Skip (. */
354 while (src[idx] && src[idx] != ')')
356 int done = parse_reg (src + idx, &mode, &rn);
358 if (done)
360 idx += done;
361 mask |= 1 << rn;
363 else
365 as_bad (_("syntax error in reg list"));
366 return 0;
369 if (src[idx] == '-')
371 idx++;
372 done = parse_reg (src + idx, &mode, &rm);
373 if (done)
375 idx += done;
376 while (rn <= rm)
378 mask |= 1 << rn;
379 rn++;
382 else
383 as_bad (_("missing final register in range"));
386 if (src[idx] == ',')
387 idx++;
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;
399 /* The many forms of operand:
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. */
409 static void
410 get_operand (char **ptr,
411 h8500_operand_info *op,
412 char ispage)
414 char *src = *ptr;
415 int mode;
416 unsigned int num;
417 unsigned int len;
419 op->page = 0;
420 if (src[0] == '(' && src[1] == 'r')
422 /* This is a register list */
423 *ptr = src + parse_reglist (src, op);
424 return;
427 len = parse_reg (src, &op->type, &op->reg);
429 if (len)
431 *ptr = src + len;
432 return;
435 if (*src == '@')
437 src++;
438 if (*src == '-')
440 src++;
441 len = parse_reg (src, &mode, &num);
442 if (len == 0)
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;
452 op->type = RNDEC;
453 op->reg = num;
454 *ptr = src + len;
455 return;
457 if (*src == '(')
459 /* Disp */
460 src++;
462 src = skip_colonthing (exp_signed, src,
463 op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
465 if (*src != ',')
467 as_bad (_("expected @(exp, Rn)"));
468 return;
470 src++;
471 len = parse_reg (src, &mode, &op->reg);
472 if (len == 0 || mode != RN)
474 as_bad (_("expected @(exp, Rn)"));
475 return;
477 src += len;
478 if (*src != ')')
480 as_bad (_("expected @(exp, Rn)"));
481 return;
483 *ptr = src + 1;
484 return;
486 len = parse_reg (src, &mode, &num);
488 if (len)
490 src += len;
491 if (*src == '+')
493 src++;
494 if (mode != RN)
496 as_bad (_("@Rn+ needs word register"));
497 return;
499 op->type = RNINC;
500 op->reg = num;
501 *ptr = src;
502 return;
504 if (mode != RN)
506 as_bad (_("@Rn needs word register"));
507 return;
509 op->type = RNIND;
510 op->reg = num;
511 *ptr = src;
512 return;
514 else
516 /* must be a symbol */
517 *ptr =
518 skip_colonthing (exp_unsigned, src, op,
519 ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
520 return;
524 if (*src == '#')
526 src++;
527 *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
528 return;
530 else
531 *ptr = skip_colonthing (exp_signed, src, op,
532 ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
535 static char *
536 get_operands (h8500_opcode_info *info,
537 char *args,
538 h8500_operand_info *operand)
540 char *ptr = args;
542 switch (info->nargs)
544 case 0:
545 operand[0].type = 0;
546 operand[1].type = 0;
547 break;
549 case 1:
550 ptr++;
551 get_operand (&ptr, operand + 0, info->name[0] == 'p');
552 operand[1].type = 0;
553 break;
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;
563 default:
564 abort ();
567 return ptr;
570 /* Passed a pointer to a list of opcodes which use different
571 addressing modes, return the opcode which matches the opcodes
572 provided. */
574 int pcrel8; /* Set when we've seen a pcrel operand. */
576 static h8500_opcode_info *
577 get_specific (h8500_opcode_info *opcode,
578 h8500_operand_info *operands)
580 h8500_opcode_info *this_try = opcode;
581 int found = 0;
582 unsigned int noperands = opcode->nargs;
583 int this_index = opcode->idx;
585 while (this_index == opcode->idx && !found)
587 unsigned int i;
589 this_try = opcode++;
591 /* Look at both operands needed by the opcodes and provided by
592 the user. */
593 for (i = 0; i < noperands; i++)
595 h8500_operand_info *user = operands + i;
597 switch (this_try->arg_type[i])
599 case FPIND_D8:
600 /* Opcode needs (disp:8,fp). */
601 if (user->type == RNIND_D8 && user->reg == 6)
603 displacement = user->exp;
604 continue;
606 break;
607 case RDIND_D16:
608 if (user->type == RNIND_D16)
610 displacement = user->exp;
611 rd = user->reg;
612 continue;
614 break;
615 case RDIND_D8:
616 if (user->type == RNIND_D8)
618 displacement = user->exp;
619 rd = user->reg;
620 continue;
622 break;
623 case RNIND_D16:
624 case RNIND_D8:
625 if (user->type == this_try->arg_type[i])
627 displacement = user->exp;
628 rn = user->reg;
629 continue;
631 break;
633 case SPDEC:
634 if (user->type == RNDEC && user->reg == 7)
635 continue;
636 break;
638 case SPINC:
639 if (user->type == RNINC && user->reg == 7)
640 continue;
641 break;
643 case ABS16:
644 if (user->type == ABS16)
646 absolute = user->exp;
647 continue;
649 break;
650 case ABS8:
651 if (user->type == ABS8)
653 absolute = user->exp;
654 continue;
656 break;
657 case ABS24:
658 if (user->type == ABS24)
660 absolute = user->exp;
661 continue;
663 break;
665 case CRB:
666 if ((user->type == CRB || user->type == CR) && user->reg != 0)
668 crb = user->reg;
669 continue;
671 break;
672 case CRW:
673 if ((user->type == CRW || user->type == CR) && user->reg == 0)
675 crw = user->reg;
676 continue;
678 break;
679 case DISP16:
680 if (user->type == DISP16)
682 displacement = user->exp;
683 continue;
685 break;
686 case DISP8:
687 if (user->type == DISP8)
689 displacement = user->exp;
690 continue;
692 break;
693 case FP:
694 if (user->type == RN && user->reg == 6)
695 continue;
696 break;
698 case PCREL16:
699 if (user->type == PCREL16)
701 displacement = user->exp;
702 continue;
704 break;
705 case PCREL8:
706 if (user->type == PCREL8)
708 displacement = user->exp;
709 pcrel8 = 1;
710 continue;
712 break;
714 case IMM16:
715 if (user->type == IMM16
716 || user->type == IMM8)
718 immediate_inpage = user->page;
719 immediate = user->exp;
720 continue;
722 break;
723 case RLIST:
724 case IMM8:
725 if (user->type == IMM8)
727 immediate_inpage = user->page;
728 immediate = user->exp;
729 continue;
731 break;
732 case IMM4:
733 if (user->type == IMM8)
735 immediate_inpage = user->page;
736 immediate = user->exp;
737 continue;
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))
748 immediate_inpage = user->page;
749 immediate = user->exp;
750 continue;
752 break;
753 case RD:
754 if (user->type == RN)
756 rd = user->reg;
757 continue;
759 break;
760 case RS:
761 if (user->type == RN)
763 rs = user->reg;
764 continue;
766 break;
767 case RDIND:
768 if (user->type == RNIND)
770 rd = user->reg;
771 continue;
774 break;
775 case RNINC:
776 case RNIND:
777 case RNDEC:
778 case RN:
779 if (user->type == this_try->arg_type[i])
781 rn = user->reg;
782 continue;
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;
794 /* If we get here this didn't work out. */
795 goto fail;
797 found = 1;
798 fail:;
802 if (found)
803 return this_try;
804 else
805 return 0;
808 static int
809 check (expressionS *operand,
810 int low,
811 int high)
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);
818 return operand->X_add_number;
821 static void
822 insert (char *output, int index, expressionS *exp, int reloc, int pcrel)
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);
830 static void
831 build_relaxable_instruction (h8500_opcode_info *opcode,
832 h8500_operand_info *operand ATTRIBUTE_UNUSED)
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;
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;
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,
857 p[0] = opcode->bytes[0].contents;
858 if (type != BRANCH)
859 p[1] = opcode->bytes[1].contents | rs;
862 /* Now we know what sort of opcodes it is, let's build the bytes. */
864 static void
865 build_bytes (h8500_opcode_info *opcode, h8500_operand_info *operand)
867 int index;
869 if (pcrel8)
871 pcrel8 = 0;
872 build_relaxable_instruction (opcode, operand);
874 else
876 char *output = frag_more (opcode->length);
878 memset (output, 0, opcode->length);
879 for (index = 0; index < opcode->length; index++)
881 output[index] = opcode->bytes[index].contents;
883 switch (opcode->bytes[index].insert)
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:
910 int p;
912 switch (immediate_inpage)
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;
924 insert (output, index, &immediate, p, 0);
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)
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;
987 break;
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. */
997 void
998 md_assemble (char *str)
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];
1007 int nlen = 0;
1009 /* Drop leading whitespace. */
1010 while (*str == ' ')
1011 str++;
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;
1020 name[nlen] = 0;
1022 if (op_end == op_start)
1023 as_bad (_("can't find opcode "));
1025 opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1027 if (opcode == NULL)
1029 as_bad (_("unknown opcode"));
1030 return;
1033 get_operands (opcode, op_end, operand);
1034 prev_opcode = opcode;
1036 opcode = get_specific (opcode, operand);
1038 if (opcode == 0)
1040 /* Couldn't find an opcode which matched the operands. */
1041 char *where = frag_more (2);
1043 where[0] = 0x0;
1044 where[1] = 0x0;
1045 as_bad (_("invalid operands for opcode"));
1046 return;
1049 build_bytes (opcode, operand);
1052 void
1053 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
1055 printf (_("call to tc_crawl_symbol_chain \n"));
1058 symbolS *
1059 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1061 return NULL;
1064 void
1065 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
1067 printf (_("call to tc_headers_hook \n"));
1070 /* Various routines to kill one day. */
1071 /* Equal to MAX_PRECISION in atof-ieee.c. */
1072 #define MAX_LITTLENUMS 6
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. */
1079 char *
1080 md_atof (int type, char *litP, int *sizeP)
1082 int prec;
1083 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1084 LITTLENUM_TYPE *wordP;
1085 char *t;
1087 switch (type)
1089 case 'f':
1090 case 'F':
1091 case 's':
1092 case 'S':
1093 prec = 2;
1094 break;
1096 case 'd':
1097 case 'D':
1098 case 'r':
1099 case 'R':
1100 prec = 4;
1101 break;
1103 case 'x':
1104 case 'X':
1105 prec = 6;
1106 break;
1108 case 'p':
1109 case 'P':
1110 prec = 6;
1111 break;
1113 default:
1114 *sizeP = 0;
1115 return _("Bad call to MD_ATOF()");
1117 t = atof_ieee (input_line_pointer, type, words);
1118 if (t)
1119 input_line_pointer = t;
1121 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1122 for (wordP = words; prec--;)
1124 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1125 litP += sizeof (LITTLENUM_TYPE);
1127 return 0;
1130 const char *md_shortopts = "";
1131 struct option md_longopts[] =
1133 {NULL, no_argument, NULL, 0}
1135 size_t md_longopts_size = sizeof (md_longopts);
1138 md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
1140 return 0;
1143 void
1144 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1148 static void
1149 wordify_scb (char *buffer, int *disp_size, int *inst_size)
1151 int rn = buffer[1] & 0x7;
1153 switch (buffer[0])
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 ();
1181 case 0x01:
1182 *inst_size = 6;
1183 *disp_size = 2;
1184 break;
1185 case 0x06:
1186 *inst_size = 8;
1187 *disp_size = 2;
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;
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;
1210 /* Called after relaxing, change the frags so they know how big they
1211 are. */
1213 void
1214 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
1215 segT seg ATTRIBUTE_UNUSED,
1216 fragS *fragP)
1218 int disp_size = 0;
1219 int inst_size = 0;
1220 char *buffer = fragP->fr_fix + fragP->fr_literal;
1222 switch (fragP->fr_subtype)
1224 case C (BRANCH, BYTE_DISP):
1225 disp_size = 1;
1226 inst_size = 1;
1227 break;
1229 case C (SCB_F, BYTE_DISP):
1230 case C (SCB_TST, BYTE_DISP):
1231 disp_size = 1;
1232 inst_size = 2;
1233 break;
1235 /* Branches to a known 16 bit displacement. */
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;
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);
1251 /* Make a reloc */
1252 fix_new (fragP,
1253 fragP->fr_fix + inst_size,
1255 fragP->fr_symbol,
1256 fragP->fr_offset,
1258 R_H8500_PCREL16);
1260 fragP->fr_fix += disp_size + inst_size;
1261 return;
1262 break;
1263 default:
1264 abort ();
1266 if (inst_size)
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;
1274 md_number_to_chars (buffer + inst_size, disp, disp_size);
1275 fragP->fr_fix += disp_size + inst_size;
1279 valueT
1280 md_section_align (segT seg, valueT size)
1282 return ((size + (1 << section_alignment[(int) seg]) - 1)
1283 & (-1 << section_alignment[(int) seg]));
1286 void
1287 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1289 long val = * (long *) valP;
1290 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1292 if (fixP->fx_r_type == 0)
1293 fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1295 switch (fixP->fx_r_type)
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 ();
1329 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1330 fixP->fx_done = 1;
1333 /* Called just before address relaxation, return the length
1334 by which a fragment must grow to reach it's destination. */
1337 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
1339 int what;
1341 switch (fragP->fr_subtype)
1343 default:
1344 abort ();
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)
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);
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;
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;
1377 return md_relax_table[fragP->fr_subtype].rlx_length;
1380 /* Put number into target byte order. */
1382 void
1383 md_number_to_chars (char *ptr, valueT use, int nbytes)
1385 number_to_chars_bigendian (ptr, use, nbytes);
1388 long
1389 md_pcrel_from (fixS *fixP)
1391 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1394 void
1395 tc_coff_symbol_emit_hook (symbolS *ignore ATTRIBUTE_UNUSED)
1399 short
1400 tc_coff_fix2rtype (fixS *fix_ptr)
1402 if (fix_ptr->fx_r_type == RELOC_32)
1404 /* Cons likes to create reloc32's whatever the size of the reloc. */
1405 switch (fix_ptr->fx_size)
1407 case 2:
1408 return R_H8500_IMM16;
1409 break;
1410 case 1:
1411 return R_H8500_IMM8;
1412 break;
1413 default:
1414 abort ();
1417 return fix_ptr->fx_r_type;
1420 void
1421 tc_reloc_mangle (fixS *fix_ptr,
1422 struct internal_reloc *intr,
1423 bfd_vma base)
1425 symbolS *symbol_ptr;
1427 symbol_ptr = fix_ptr->fx_addsy;
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)
1433 /* Cons likes to create reloc32's whatever the size of the reloc. */
1434 switch (fix_ptr->fx_size)
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 ();
1446 else
1447 intr->r_type = fix_ptr->fx_r_type;
1449 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1450 intr->r_offset = fix_ptr->fx_offset;
1452 /* Turn the segment of the symbol into an offset. */
1453 if (symbol_ptr)
1455 symbolS *dot;
1457 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1458 if (dot)
1460 intr->r_offset += S_GET_VALUE (symbol_ptr);
1461 intr->r_symndx = dot->sy_number;
1463 else
1464 intr->r_symndx = symbol_ptr->sy_number;
1466 else
1467 intr->r_symndx = -1;
1471 start_label (char *ptr)
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;
1483 tc_coff_sizemachdep (fragS *frag)
1485 return md_relax_table[frag->fr_subtype].rlx_length;