1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
3 Copyright (C) 1993, 1994, 1995 Free Software Foundation.
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)
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
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 Written By Steve Chamberlain
32 #include "opcodes/sh-opc.h"
34 const char comment_chars
[] = "!";
35 const char line_separator_chars
[] = ";";
36 const char line_comment_chars
[] = "!#";
38 static void s_uses
PARAMS ((int));
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
48 void s_align_bytes ();
59 const pseudo_typeS md_pseudo_table
[] =
63 {"form", listing_psize
, 0},
64 {"little", little
, 0},
65 {"heading", listing_title
, 0},
66 {"import", s_ignore
, 0},
67 {"page", listing_eject
, 0},
68 {"program", s_ignore
, 0},
73 /*int md_reloc_size; */
75 int sh_relax
; /* set if -relax seen */
77 const char EXP_CHARS
[] = "eE";
79 /* Chars that mean this number is a floating point constant */
82 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
84 #define C(a,b) ENCODE_RELAX(a,b)
86 #define JREG 14 /* Register used as a temp when relaxing */
87 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
88 #define GET_WHAT(x) ((x>>4))
90 /* These are the two types of relaxable instrction */
100 #define UNDEF_WORD_DISP 4
106 /* Branch displacements are from the address of the branch plus
107 four, thus all minimum and maximum values have 4 added to them. */
110 #define COND8_LENGTH 2
112 /* There is one extra instruction before the branch, so we must add
113 two more bytes to account for it. */
114 #define COND12_F 4100
115 #define COND12_M -4090
116 #define COND12_LENGTH 6
118 /* ??? The minimum and maximum values are wrong, but this does not matter
119 since this relocation type is not supported yet. */
120 #define COND32_F (1<<30)
121 #define COND32_M -(1<<30)
122 #define COND32_LENGTH 14
124 #define UNCOND12_F 4098
125 #define UNCOND12_M -4092
126 #define UNCOND12_LENGTH 2
128 /* ??? The minimum and maximum values are wrong, but this does not matter
129 since this relocation type is not supported yet. */
130 #define UNCOND32_F (1<<30)
131 #define UNCOND32_M -(1<<30)
132 #define UNCOND32_LENGTH 14
134 const relax_typeS md_relax_table
[C (END
, 0)] = {
135 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
136 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
139 /* C (COND_JUMP, COND8) */
140 { COND8_F
, COND8_M
, COND8_LENGTH
, C (COND_JUMP
, COND12
) },
141 /* C (COND_JUMP, COND12) */
142 { COND12_F
, COND12_M
, COND12_LENGTH
, C (COND_JUMP
, COND32
), },
143 /* C (COND_JUMP, COND32) */
144 { COND32_F
, COND32_M
, COND32_LENGTH
, 0, },
145 { 0 }, { 0 }, { 0 }, { 0 },
146 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
149 /* C (UNCOND_JUMP, UNCOND12) */
150 { UNCOND12_F
, UNCOND12_M
, UNCOND12_LENGTH
, C (UNCOND_JUMP
, UNCOND32
), },
151 /* C (UNCOND_JUMP, UNCOND32) */
152 { UNCOND32_F
, UNCOND32_M
, UNCOND32_LENGTH
, 0, },
153 { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
154 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
157 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
160 This function is called once, at assembler startup time. This should
161 set up all the tables, etc that the MD part of the assembler needs
167 sh_opcode_info
*opcode
;
168 char *prev_name
= "";
170 opcode_hash_control
= hash_new ();
172 /* Insert unique names into hash table */
173 for (opcode
= sh_table
; opcode
->name
; opcode
++)
175 if (strcmp (prev_name
, opcode
->name
))
177 prev_name
= opcode
->name
;
178 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
182 /* Make all the opcodes with the same name point to the same
184 opcode
->name
= prev_name
;
191 static expressionS immediate
; /* absolute expression */
201 /* try and parse a reg name, returns number of chars consumed */
203 parse_reg (src
, mode
, reg
)
212 if (src
[2] >= '0' && src
[2] <= '5')
215 *reg
= 10 + src
[2] - '0';
219 if (src
[1] >= '0' && src
[1] <= '9')
222 *reg
= (src
[1] - '0');
227 if (src
[0] == 's' && src
[1] == 'r')
233 if (src
[0] == 's' && src
[1] == 'p')
240 if (src
[0] == 'p' && src
[1] == 'r')
245 if (src
[0] == 'p' && src
[1] == 'c')
250 if (src
[0] == 'g' && src
[1] == 'b' && src
[2] == 'r')
255 if (src
[0] == 'v' && src
[1] == 'b' && src
[2] == 'r')
261 if (src
[0] == 'm' && src
[1] == 'a' && src
[2] == 'c')
274 /* start-sanitize-sh3e */
275 if (src
[0] == 'f' && src
[1] == 'r')
279 if (src
[3] >= '0' && src
[3] <= '5')
282 *reg
= 10 + src
[3] - '0';
286 if (src
[2] >= '0' && src
[2] <= '9')
289 *reg
= (src
[2] - '0');
293 if (src
[0] == 'f' && src
[1] == 'p' && src
[2] == 'u' && src
[3] == 'l')
299 if (src
[0] == 'f' && src
[1] == 'p'
300 && src
[2] == 's' && src
[3] == 'c' && src
[4] == 'r')
305 /* end-sanitize-sh3e */
310 static symbolS
*dot()
314 /* JF: '.' is pseudo symbol with value of current location
315 in current segment. */
316 fake
= FAKE_LABEL_NAME
;
317 return symbol_new (fake
,
319 (valueT
) frag_now_fix (),
333 save
= input_line_pointer
;
334 input_line_pointer
= s
;
335 expression (&immediate
);
336 if (immediate
.X_op
== O_absent
)
337 as_bad ("missing operand");
338 new = input_line_pointer
;
339 input_line_pointer
= save
;
344 /* The many forms of operand:
347 @Rn Register indirect
360 pr, gbr, vbr, macl, mach
375 /* Must be predecrement */
378 len
= parse_reg (src
, &mode
, &(op
->reg
));
380 as_bad ("illegal register after @-");
385 else if (src
[0] == '(')
387 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
390 len
= parse_reg (src
, &mode
, &(op
->reg
));
391 if (len
&& mode
== A_REG_N
)
396 as_bad ("must be @(r0,...)");
400 /* Now can be rn or gbr */
401 len
= parse_reg (src
, &mode
, &(op
->reg
));
406 else if (mode
== A_REG_N
)
408 op
->type
= A_IND_R0_REG_N
;
412 as_bad ("syntax error in @(r0,...)");
417 /* Must be an @(disp,.. thing) */
418 src
= parse_exp (src
);
421 /* Now can be rn, gbr or pc */
422 len
= parse_reg (src
, &mode
, &op
->reg
);
427 op
->type
= A_DISP_REG_N
;
429 else if (mode
== A_GBR
)
431 op
->type
= A_DISP_GBR
;
433 else if (mode
== A_DISP_PC
)
435 /* Turn a plain @(4,pc) into @(.+4,pc) */
436 if (immediate
.X_op
== O_constant
) {
437 immediate
.X_add_symbol
= dot();
438 immediate
.X_op
= O_symbol
;
440 op
->type
= A_DISP_PC
;
444 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
449 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
454 as_bad ("expecting )");
460 src
+= parse_reg (src
, &mode
, &(op
->reg
));
463 as_bad ("illegal register after @");
479 get_operand (ptr
, op
)
490 *ptr
= parse_exp (src
);
495 else if (src
[0] == '@')
497 *ptr
= parse_at (src
, op
);
500 len
= parse_reg (src
, &mode
, &(op
->reg
));
509 /* Not a reg, the only thing left is a displacement */
510 *ptr
= parse_exp (src
);
511 op
->type
= A_DISP_PC
;
518 get_operands (info
, args
, operand
)
519 sh_opcode_info
*info
;
521 sh_operand_info
*operand
;
529 get_operand (&ptr
, operand
+ 0);
536 get_operand (&ptr
, operand
+ 1);
551 /* Passed a pointer to a list of opcodes which use different
552 addressing modes, return the opcode which matches the opcodes
558 get_specific (opcode
, operands
)
559 sh_opcode_info
*opcode
;
560 sh_operand_info
*operands
;
562 sh_opcode_info
*this_try
= opcode
;
563 char *name
= opcode
->name
;
569 if (this_try
->name
!= name
)
571 /* We've looked so far down the table that we've run out of
572 opcodes with the same name */
575 /* look at both operands needed by the opcodes and provided by
576 the user - since an arg test will often fail on the same arg
577 again and again, we'll try and test the last failing arg the
578 first on each opcode try */
580 for (n
= 0; this_try
->arg
[n
]; n
++)
582 sh_operand_info
*user
= operands
+ arg_to_test
;
583 sh_arg_type arg
= this_try
->arg
[arg_to_test
];
594 if (user
->type
!= arg
)
598 /* opcode needs r0 */
599 if (user
->type
!= A_REG_N
|| user
->reg
!= 0)
603 if (user
->type
!= A_R0_GBR
|| user
->reg
!= 0)
613 /* start-sanitize-sh3e */
617 /* end-sanitize-sh3e */
618 /* Opcode needs rn */
619 if (user
->type
!= arg
)
626 if (user
->type
!= arg
)
636 /* Opcode needs rn */
637 if (user
->type
!= arg
- A_REG_M
+ A_REG_N
)
642 /* start-sanitize-sh3e */
646 /* Opcode needs rn */
647 if (user
->type
!= arg
- F_REG_M
+ F_REG_N
)
651 /* end-sanitize-sh3e */
654 printf ("unhandled %d\n", arg
);
657 /* If we did 0, test 1 next, else 0 */
658 arg_to_test
= 1 - arg_to_test
;
668 check (operand
, low
, high
)
669 expressionS
*operand
;
673 if (operand
->X_op
!= O_constant
674 || operand
->X_add_number
< low
675 || operand
->X_add_number
> high
)
677 as_bad ("operand must be absolute in range %d..%d", low
, high
);
679 return operand
->X_add_number
;
684 insert (where
, how
, pcrel
)
689 fix_new_exp (frag_now
,
690 where
- frag_now
->fr_literal
,
699 sh_opcode_info
*opcode
;
701 int high_byte
= shl
? 1 : 0 ;
704 if (opcode
->arg
[0] == A_BDISP8
)
706 p
= frag_var (rs_machine_dependent
,
707 md_relax_table
[C (COND_JUMP
, COND32
)].rlx_length
,
708 md_relax_table
[C (COND_JUMP
, COND8
)].rlx_length
,
710 immediate
.X_add_symbol
,
711 immediate
.X_add_number
,
713 p
[high_byte
] = (opcode
->nibbles
[0] << 4) | (opcode
->nibbles
[1]);
715 else if (opcode
->arg
[0] == A_BDISP12
)
717 p
= frag_var (rs_machine_dependent
,
718 md_relax_table
[C (UNCOND_JUMP
, UNCOND32
)].rlx_length
,
719 md_relax_table
[C (UNCOND_JUMP
, UNCOND12
)].rlx_length
,
721 immediate
.X_add_symbol
,
722 immediate
.X_add_number
,
724 p
[high_byte
] = (opcode
->nibbles
[0] << 4);
729 /* Now we know what sort of opcodes it is, lets build the bytes -
732 build_Mytes (opcode
, operand
)
733 sh_opcode_info
*opcode
;
734 sh_operand_info
*operand
;
739 char *output
= frag_more (2);
740 int low_byte
= shl
? 0 : 1;
746 for (index
= 0; index
< 4; index
++)
748 sh_nibble_type i
= opcode
->nibbles
[index
];
764 insert (output
+ low_byte
, R_SH_IMM4
, 0);
767 insert (output
+ low_byte
, R_SH_IMM4BY4
, 0);
770 insert (output
+ low_byte
, R_SH_IMM4BY2
, 0);
773 insert (output
+ low_byte
, R_SH_IMM4
, 0);
776 insert (output
+ low_byte
, R_SH_IMM8BY4
, 0);
779 insert (output
+ low_byte
, R_SH_IMM8BY2
, 0);
782 insert (output
+ low_byte
, R_SH_IMM8
, 0);
785 insert (output
, R_SH_PCRELIMM8BY4
, 1);
788 insert (output
, R_SH_PCRELIMM8BY2
, 1);
791 printf ("failed for %d\n", i
);
796 output
[1] = (nbuf
[0] << 4) | (nbuf
[1]);
797 output
[0] = (nbuf
[2] << 4) | (nbuf
[3]);
800 output
[0] = (nbuf
[0] << 4) | (nbuf
[1]);
801 output
[1] = (nbuf
[2] << 4) | (nbuf
[3]);
805 /* This is the guts of the machine-dependent assembler. STR points to a
806 machine dependent instruction. This function is supposed to emit
807 the frags/bytes it assembles to.
814 unsigned char *op_start
;
815 unsigned char *op_end
;
816 sh_operand_info operand
[2];
817 sh_opcode_info
*opcode
;
820 /* Drop leading whitespace */
824 /* find the op code end */
825 for (op_start
= op_end
= (unsigned char *) (str
);
828 && !is_end_of_line
[*op_end
] && *op_end
!= ' ';
831 name
[nlen
] = op_start
[nlen
];
838 as_bad ("can't find opcode ");
841 opcode
= (sh_opcode_info
*) hash_find (opcode_hash_control
, name
);
845 as_bad ("unknown opcode");
849 if (opcode
->arg
[0] == A_BDISP12
850 || opcode
->arg
[0] == A_BDISP8
)
852 parse_exp (op_end
+ 1);
853 build_relax (opcode
);
857 if (opcode
->arg
[0] != A_END
)
859 get_operands (opcode
, op_end
, operand
);
861 opcode
= get_specific (opcode
, operand
);
865 /* Couldn't find an opcode which matched the operands */
866 char *where
= frag_more (2);
870 as_bad ("invalid operands for opcode");
874 build_Mytes (opcode
, operand
);
880 DEFUN (tc_crawl_symbol_chain
, (headers
),
881 object_headers
* headers
)
883 printf ("call to tc_crawl_symbol_chain \n");
887 DEFUN (md_undefined_symbol
, (name
),
894 DEFUN (tc_headers_hook
, (headers
),
895 object_headers
* headers
)
897 printf ("call to tc_headers_hook \n");
900 /* Various routines to kill one day */
901 /* Equal to MAX_PRECISION in atof-ieee.c */
902 #define MAX_LITTLENUMS 6
904 /* Turn a string in input_line_pointer into a floating point constant of type
905 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
906 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
909 md_atof (type
, litP
, sizeP
)
915 LITTLENUM_TYPE words
[4];
931 return "bad call to md_atof";
934 t
= atof_ieee (input_line_pointer
, type
, words
);
936 input_line_pointer
= t
;
942 for (i
= prec
- 1; i
>= 0; i
--)
944 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
950 for (i
= 0; i
< prec
; i
++)
952 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
960 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
961 call instruction. It refers to a label of the instruction which
962 loads the register which the call uses. We use it to generate a
963 special reloc for the linker. */
972 as_warn (".uses pseudo-op seen when not relaxing");
976 if (ex
.X_op
!= O_symbol
|| ex
.X_add_number
!= 0)
978 as_bad ("bad .uses format");
979 ignore_rest_of_line ();
983 fix_new_exp (frag_now
, frag_now_fix (), 2, &ex
, 1, R_SH_USES
);
985 demand_empty_rest_of_line ();
988 CONST
char *md_shortopts
= "";
989 struct option md_longopts
[] = {
991 #define OPTION_RELAX (OPTION_MD_BASE)
992 #define OPTION_LITTLE (OPTION_MD_BASE+1)
994 {"relax", no_argument
, NULL
, OPTION_RELAX
},
995 {"little", no_argument
, NULL
, OPTION_LITTLE
},
996 {NULL
, no_argument
, NULL
, 0}
998 size_t md_longopts_size
= sizeof(md_longopts
);
1001 md_parse_option (c
, arg
)
1022 md_show_usage (stream
)
1027 -little generate little endian code\n\
1028 -relax alter jump instructions for long displacements\n");
1031 int md_short_jump_size
;
1034 tc_Nout_fix_to_chars ()
1036 printf ("call to tc_Nout_fix_to_chars \n");
1041 md_create_short_jump (ptr
, from_Nddr
, to_Nddr
, frag
, to_symbol
)
1048 as_fatal ("failed sanity check.");
1052 md_create_long_jump (ptr
, from_Nddr
, to_Nddr
, frag
, to_symbol
)
1054 addressT from_Nddr
, to_Nddr
;
1058 as_fatal ("failed sanity check.");
1061 /* This is function is called after the symbol table has been
1062 completed, but before md_convert_frag has been called. If we have
1063 seen any .uses pseudo-ops, they point to an instruction which loads
1064 a register with the address of a function. We look through the
1065 fixups to find where the function address is being loaded from. We
1066 then generate a COUNT reloc giving the number of times that
1067 function address is referred to. The linker uses this information
1068 when doing relaxing, to decide when it can eliminate the stored
1069 function address entirely. */
1072 sh_coff_frob_file ()
1079 for (iseg
= SEG_E0
; iseg
< SEG_UNKNOWN
; iseg
++)
1083 for (fix
= segment_info
[iseg
].fix_root
; fix
!= NULL
; fix
= fix
->fx_next
)
1092 if (fix
->fx_r_type
!= R_SH_USES
)
1095 /* The R_SH_USES reloc should refer to a defined local
1096 symbol in the same section. */
1097 sym
= fix
->fx_addsy
;
1099 || fix
->fx_subsy
!= NULL
1100 || fix
->fx_addnumber
!= 0
1101 || S_GET_SEGMENT (sym
) != iseg
1102 || S_GET_STORAGE_CLASS (sym
) == C_EXT
)
1104 as_warn_where (fix
->fx_file
, fix
->fx_line
,
1105 ".uses does not refer to a local symbol in the same section");
1109 /* Look through the fixups again, this time looking for one
1110 at the same location as sym. */
1111 val
= S_GET_VALUE (sym
);
1112 paddr
= segment_info
[iseg
].scnhdr
.s_paddr
;
1113 for (fscan
= segment_info
[iseg
].fix_root
;
1115 fscan
= fscan
->fx_next
)
1116 if (val
== paddr
+ fscan
->fx_frag
->fr_address
+ fscan
->fx_where
)
1120 as_warn_where (fix
->fx_file
, fix
->fx_line
,
1121 "can't find fixup pointed to by .uses");
1125 if (fscan
->fx_tcbit
)
1127 /* We've already done this one. */
1131 /* fscan should also be a fixup to a local symbol in the same
1133 sym
= fscan
->fx_addsy
;
1135 || fscan
->fx_subsy
!= NULL
1136 || fscan
->fx_addnumber
!= 0
1137 || S_GET_SEGMENT (sym
) != iseg
1138 || S_GET_STORAGE_CLASS (sym
) == C_EXT
)
1140 as_warn_where (fix
->fx_file
, fix
->fx_line
,
1141 ".uses target does not refer to a local symbol in the same section");
1145 /* Now we look through all the fixups of all the sections,
1146 counting the number of times we find a reference to sym. */
1148 for (iscan
= SEG_E0
; iscan
< SEG_UNKNOWN
; iscan
++)
1150 paddr
= segment_info
[iscan
].scnhdr
.s_paddr
;
1151 for (fscan
= segment_info
[iscan
].fix_root
;
1153 fscan
= fscan
->fx_next
)
1155 if (fscan
->fx_addsy
== sym
)
1158 fscan
->fx_tcbit
= 1;
1166 /* Generate a R_SH_COUNT fixup at the location of sym. We
1167 have already adjusted the value of sym to include the
1168 fragment address, so we undo that adjustment here. */
1169 subseg_change (iseg
, 0);
1170 fix_new (sym
->sy_frag
, S_GET_VALUE (sym
) - sym
->sy_frag
->fr_address
,
1171 4, &abs_symbol
, count
, 0, R_SH_COUNT
);
1176 /* Called after relaxing. Set the correct sizes of the fragments, and
1177 create relocs so that md_apply_fix will fill in the correct values. */
1180 md_convert_frag (headers
, seg
, fragP
)
1181 object_headers
*headers
;
1187 switch (fragP
->fr_subtype
)
1189 case C (COND_JUMP
, COND8
):
1190 subseg_change (seg
, 0);
1191 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
, fragP
->fr_offset
,
1192 1, R_SH_PCDISP8BY2
);
1197 case C (UNCOND_JUMP
, UNCOND12
):
1198 subseg_change (seg
, 0);
1199 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
, fragP
->fr_offset
,
1205 case C (UNCOND_JUMP
, UNCOND32
):
1206 case C (UNCOND_JUMP
, UNDEF_WORD_DISP
):
1207 if (fragP
->fr_symbol
== NULL
)
1208 as_bad ("at 0x%lx, displacement overflows 12-bit field",
1209 (unsigned long) fragP
->fr_address
);
1211 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1212 (unsigned long) fragP
->fr_address
,
1213 S_IS_DEFINED (fragP
->fr_symbol
) ? "" : "un",
1214 S_GET_NAME (fragP
->fr_symbol
));
1216 #if 0 /* This code works, but generates poor code and the compiler
1217 should never produce a sequence that requires it to be used. */
1219 /* A jump wont fit in 12 bits, make code which looks like
1225 int t
= buffer
[0] & 0x10;
1227 buffer
[highbyte
] = 0xa0; /* branch over move and disp */
1228 buffer
[lowbyte
] = 3;
1229 buffer
[highbyte
+2] = 0xd0 | JREG
; /* Build mov insn */
1230 buffer
[lowbyte
+2] = 0x00;
1232 buffer
[highbyte
+4] = 0; /* space for 32 bit jump disp */
1233 buffer
[lowbyte
+4] = 0;
1234 buffer
[highbyte
+6] = 0;
1235 buffer
[lowbyte
+6] = 0;
1237 buffer
[highbyte
+8] = 0x40 | JREG
; /* Build jmp @JREG */
1238 buffer
[lowbyte
+8] = t
? 0xb : 0x2b;
1240 buffer
[highbyte
+10] = 0x20; /* build nop */
1241 buffer
[lowbyte
+10] = 0x0b;
1243 /* Make reloc for the long disp */
1251 fragP
->fr_fix
+= UNCOND32_LENGTH
;
1258 case C (COND_JUMP
, COND12
):
1259 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1261 unsigned char *buffer
=
1262 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
1263 int highbyte
= shl
? 1 : 0;
1264 int lowbyte
= shl
? 0 : 1;
1266 /* Toggle the true/false bit of the bcond. */
1267 buffer
[highbyte
] ^= 0x2;
1269 /* Build a relocation to six bytes farther on. */
1270 subseg_change (seg
, 0);
1271 fix_new (fragP
, fragP
->fr_fix
, 2,
1272 segment_info
[seg
].dot
,
1273 fragP
->fr_address
+ fragP
->fr_fix
+ 6,
1274 1, R_SH_PCDISP8BY2
);
1276 /* Set up a jump instruction. */
1277 buffer
[highbyte
+ 2] = 0xa0;
1278 buffer
[lowbyte
+ 2] = 0;
1279 fix_new (fragP
, fragP
->fr_fix
+ 2, 2, fragP
->fr_symbol
,
1280 fragP
->fr_offset
, 1, R_SH_PCDISP
);
1282 /* Fill in a NOP instruction. */
1283 buffer
[highbyte
+ 4] = 0x0;
1284 buffer
[lowbyte
+ 4] = 0x9;
1292 case C (COND_JUMP
, COND32
):
1293 case C (COND_JUMP
, UNDEF_WORD_DISP
):
1294 if (fragP
->fr_symbol
== NULL
)
1295 as_bad ("at %0xlx, displacement overflows 8-bit field",
1296 (unsigned long) fragP
->fr_address
);
1298 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1299 (unsigned long) fragP
->fr_address
,
1300 S_IS_DEFINED (fragP
->fr_symbol
) ? "" : "un",
1301 S_GET_NAME (fragP
->fr_symbol
));
1303 #if 0 /* This code works, but generates poor code, and the compiler
1304 should never produce a sequence that requires it to be used. */
1306 /* A bcond won't fit and it won't go into a 12 bit
1307 displacement either, the code sequence looks like:
1316 buffer
[0] ^= 0x2; /* Toggle T/F bit */
1318 buffer
[1] = 5; /* branch over mov, jump, nop and ptr */
1319 buffer
[2] = 0xd0 | JREG
; /* Build mov insn */
1321 buffer
[4] = 0x40 | JREG
; /* Build jmp @JREG */
1323 buffer
[6] = 0x20; /* build nop */
1325 buffer
[8] = 0; /* space for 32 bit jump disp */
1331 /* Make reloc for the long disp */
1339 fragP
->fr_fix
+= COND32_LENGTH
;
1350 if (donerelax
&& !sh_relax
)
1351 as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
1352 (unsigned long) fragP
->fr_address
,
1353 fragP
->fr_symbol
? S_GET_NAME(fragP
->fr_symbol
): "",
1354 (unsigned long) fragP
->fr_offset
);
1358 DEFUN (md_section_align
, (seg
, size
),
1362 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
1363 & (-1 << section_alignment
[(int) seg
]));
1367 /* When relaxing, we need to output a reloc for any .align directive
1368 that requests alignment to a four byte boundary or larger. */
1371 sh_handle_align (frag
)
1375 && frag
->fr_type
== rs_align
1376 && frag
->fr_address
+ frag
->fr_fix
> 0
1377 && frag
->fr_offset
> 1)
1378 fix_new (frag
, frag
->fr_fix
, 2, &abs_symbol
, frag
->fr_offset
, 0,
1382 /* This macro decides whether a particular reloc is an entry in a
1383 switch table. It is used when relaxing, because the linker needs
1384 to know about all such entries so that it can adjust them if
1387 #define SWITCH_TABLE(fix) \
1388 ((fix)->fx_addsy != NULL \
1389 && (fix)->fx_subsy != NULL \
1390 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
1391 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
1392 && ((fix)->fx_r_type == R_SH_IMM32 \
1393 || (fix)->fx_r_type == R_SH_IMM16 \
1394 || ((fix)->fx_r_type == 0 \
1395 && ((fix)->fx_size == 2 \
1396 || (fix)->fx_size == 4))))
1398 /* See whether we need to force a relocation into the output file.
1399 This is used to force out switch and PC relative relocations when
1403 sh_force_relocation (fix
)
1409 return (fix
->fx_pcrel
1410 || SWITCH_TABLE (fix
)
1411 || fix
->fx_r_type
== R_SH_COUNT
1412 || fix
->fx_r_type
== R_SH_ALIGN
);
1415 /* Apply a fixup to the object file. */
1418 md_apply_fix (fixP
, val
)
1422 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1423 int lowbyte
= shl
? 0 : 1;
1424 int highbyte
= shl
? 1 : 0;
1426 if (fixP
->fx_r_type
== 0)
1428 if (fixP
->fx_size
== 2)
1429 fixP
->fx_r_type
= R_SH_IMM16
;
1430 else if (fixP
->fx_size
== 4)
1431 fixP
->fx_r_type
= R_SH_IMM32
;
1432 else if (fixP
->fx_size
== 1)
1433 fixP
->fx_r_type
= R_SH_IMM8
;
1438 switch (fixP
->fx_r_type
)
1441 *buf
= (*buf
& 0xf0) | (val
& 0xf);
1445 *buf
= (*buf
& 0xf0) | ((val
>> 1) & 0xf);
1449 *buf
= (*buf
& 0xf0) | ((val
>> 2) & 0xf);
1464 case R_SH_PCRELIMM8BY4
:
1465 /* The lower two bits of the PC are cleared before the
1466 displacement is added in. We can assume that the destination
1467 is on a 4 byte bounday. If this instruction is also on a 4
1468 byte boundary, then we want
1470 and target - here is a multiple of 4.
1471 Otherwise, we are on a 2 byte boundary, and we want
1472 (target - (here - 2)) / 4
1473 and target - here is not a multiple of 4. Computing
1474 (target - (here - 2)) / 4 == (target - here + 2) / 4
1475 works for both cases, since in the first case the addition of
1476 2 will be removed by the division. target - here is in the
1478 val
= (val
+ 2) / 4;
1480 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, "pcrel too far");
1484 case R_SH_PCRELIMM8BY2
:
1487 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, "pcrel too far");
1491 case R_SH_PCDISP8BY2
:
1493 if (val
< -0x80 || val
> 0x7f)
1494 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, "pcrel too far");
1500 if (val
< -0x800 || val
>= 0x7ff)
1501 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, "pcrel too far");
1502 buf
[lowbyte
] = val
& 0xff;
1503 buf
[highbyte
] |= (val
>> 8) & 0xf;
1537 /* Pass the value into sh_coff_reloc_mangle. */
1538 fixP
->fx_addnumber
= val
;
1543 /* Nothing to do here. */
1551 int md_long_jump_size
;
1553 /* Called just before address relaxation. Return the length
1554 by which a fragment must grow to reach it's destination. */
1557 md_estimate_size_before_relax (fragP
, segment_type
)
1558 register fragS
*fragP
;
1559 register segT segment_type
;
1561 switch (fragP
->fr_subtype
)
1563 case C (UNCOND_JUMP
, UNDEF_DISP
):
1564 /* used to be a branch to somewhere which was unknown */
1565 if (!fragP
->fr_symbol
)
1567 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNCOND12
);
1568 fragP
->fr_var
= md_relax_table
[C (UNCOND_JUMP
, UNCOND12
)].rlx_length
;
1570 else if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1572 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNCOND12
);
1573 fragP
->fr_var
= md_relax_table
[C (UNCOND_JUMP
, UNCOND12
)].rlx_length
;
1577 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNDEF_WORD_DISP
);
1578 fragP
->fr_var
= md_relax_table
[C (UNCOND_JUMP
, UNCOND32
)].rlx_length
;
1579 return md_relax_table
[C (UNCOND_JUMP
, UNCOND32
)].rlx_length
;
1585 case C (COND_JUMP
, UNDEF_DISP
):
1586 /* used to be a branch to somewhere which was unknown */
1587 if (fragP
->fr_symbol
1588 && S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1590 /* Got a symbol and it's defined in this segment, become byte
1591 sized - maybe it will fix up */
1592 fragP
->fr_subtype
= C (COND_JUMP
, COND8
);
1593 fragP
->fr_var
= md_relax_table
[C (COND_JUMP
, COND8
)].rlx_length
;
1595 else if (fragP
->fr_symbol
)
1597 /* Its got a segment, but its not ours, so it will always be long */
1598 fragP
->fr_subtype
= C (COND_JUMP
, UNDEF_WORD_DISP
);
1599 fragP
->fr_var
= md_relax_table
[C (COND_JUMP
, COND32
)].rlx_length
;
1600 return md_relax_table
[C (COND_JUMP
, COND32
)].rlx_length
;
1604 /* We know the abs value */
1605 fragP
->fr_subtype
= C (COND_JUMP
, COND8
);
1606 fragP
->fr_var
= md_relax_table
[C (COND_JUMP
, COND8
)].rlx_length
;
1611 return fragP
->fr_var
;
1614 /* Put number into target byte order */
1617 md_number_to_chars (ptr
, use
, nbytes
)
1623 number_to_chars_littleendian (ptr
, use
, nbytes
);
1625 number_to_chars_bigendian (ptr
, use
, nbytes
);
1629 md_pcrel_from (fixP
)
1632 return fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
+ 2;
1636 tc_coff_sizemachdep (frag
)
1639 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1644 /* Adjust a reloc for the SH. This is similar to the generic code,
1645 but does some minor tweaking. */
1648 sh_coff_reloc_mangle (seg
, fix
, intr
, paddr
)
1649 segment_info_type
*seg
;
1651 struct internal_reloc
*intr
;
1654 symbolS
*symbol_ptr
= fix
->fx_addsy
;
1657 intr
->r_vaddr
= paddr
+ fix
->fx_frag
->fr_address
+ fix
->fx_where
;
1659 if (! SWITCH_TABLE (fix
))
1661 intr
->r_type
= fix
->fx_r_type
;
1668 if (fix
->fx_r_type
== R_SH_IMM16
)
1669 intr
->r_type
= R_SH_SWITCH16
;
1670 else if (fix
->fx_r_type
== R_SH_IMM32
)
1671 intr
->r_type
= R_SH_SWITCH32
;
1675 /* For a switch reloc, we set r_offset to the difference between
1676 the reloc address and the subtrahend. When the linker is
1677 doing relaxing, it can use the determine the starting and
1678 ending points of the switch difference expression. */
1679 intr
->r_offset
= intr
->r_vaddr
- S_GET_VALUE (fix
->fx_subsy
);
1682 /* PC relative relocs are always against the current section. */
1683 if (symbol_ptr
== NULL
)
1685 switch (fix
->fx_r_type
)
1687 case R_SH_PCRELIMM8BY2
:
1688 case R_SH_PCRELIMM8BY4
:
1689 case R_SH_PCDISP8BY2
:
1692 symbol_ptr
= seg
->dot
;
1699 if (fix
->fx_r_type
== R_SH_USES
)
1701 /* We can't store the offset in the object file, since this
1702 reloc does not take up any space, so we store it in r_offset.
1703 The fx_addnumber field was set in md_apply_fix. */
1704 intr
->r_offset
= fix
->fx_addnumber
;
1706 else if (fix
->fx_r_type
== R_SH_COUNT
)
1708 /* We can't store the count in the object file, since this reloc
1709 does not take up any space, so we store it in r_offset. The
1710 fx_offset field was set when the fixup was created in
1711 sh_coff_frob_file. */
1712 intr
->r_offset
= fix
->fx_offset
;
1713 /* This reloc is always absolute. */
1716 else if (fix
->fx_r_type
== R_SH_ALIGN
)
1718 /* Store the alignment in the r_offset field. */
1719 intr
->r_offset
= fix
->fx_offset
;
1720 /* This reloc is always absolute. */
1724 /* Turn the segment of the symbol into an offset. */
1725 if (symbol_ptr
!= NULL
)
1727 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1729 intr
->r_symndx
= dot
->sy_number
;
1731 intr
->r_symndx
= symbol_ptr
->sy_number
;
1734 intr
->r_symndx
= -1;