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 ();
57 target_big_endian = 0;
60 const pseudo_typeS md_pseudo_table[] =
64 {"form", listing_psize, 0},
65 {"little", little, 0},
66 {"heading", listing_title, 0},
67 {"import", s_ignore, 0},
68 {"page", listing_eject, 0},
69 {"program", s_ignore, 0},
74 /*int md_reloc_size; */
76 int sh_relax; /* set if -relax seen */
78 const char EXP_CHARS[] = "eE";
80 /* Chars that mean this number is a floating point constant */
83 const char FLT_CHARS[] = "rRsSfFdDxXpP";
85 #define C(a,b) ENCODE_RELAX(a,b)
87 #define JREG 14 /* Register used as a temp when relaxing */
88 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
89 #define GET_WHAT(x) ((x>>4))
91 /* These are the two types of relaxable instrction */
101 #define UNDEF_WORD_DISP 4
107 /* Branch displacements are from the address of the branch plus
108 four, thus all minimum and maximum values have 4 added to them. */
111 #define COND8_LENGTH 2
113 /* There is one extra instruction before the branch, so we must add
114 two more bytes to account for it. */
115 #define COND12_F 4100
116 #define COND12_M -4090
117 #define COND12_LENGTH 6
119 /* ??? The minimum and maximum values are wrong, but this does not matter
120 since this relocation type is not supported yet. */
121 #define COND32_F (1<<30)
122 #define COND32_M -(1<<30)
123 #define COND32_LENGTH 14
125 #define UNCOND12_F 4098
126 #define UNCOND12_M -4092
127 #define UNCOND12_LENGTH 2
129 /* ??? The minimum and maximum values are wrong, but this does not matter
130 since this relocation type is not supported yet. */
131 #define UNCOND32_F (1<<30)
132 #define UNCOND32_M -(1<<30)
133 #define UNCOND32_LENGTH 14
135 const relax_typeS md_relax_table[C (END, 0)] = {
136 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
137 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
140 /* C (COND_JUMP, COND8) */
141 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
142 /* C (COND_JUMP, COND12) */
143 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
144 /* C (COND_JUMP, COND32) */
145 { COND32_F, COND32_M, COND32_LENGTH, 0, },
146 { 0 }, { 0 }, { 0 }, { 0 },
147 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
150 /* C (UNCOND_JUMP, UNCOND12) */
151 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
152 /* C (UNCOND_JUMP, UNCOND32) */
153 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
154 { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
155 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
158 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
161 This function is called once, at assembler startup time. This should
162 set up all the tables, etc that the MD part of the assembler needs
168 sh_opcode_info *opcode;
169 char *prev_name = "";
172 target_big_endian = 1;
174 opcode_hash_control = hash_new ();
176 /* Insert unique names into hash table */
177 for (opcode = sh_table; opcode->name; opcode++)
179 if (strcmp (prev_name, opcode->name))
181 prev_name = opcode->name;
182 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
186 /* Make all the opcodes with the same name point to the same
188 opcode->name = prev_name;
197 static expressionS immediate; /* absolute expression */
207 /* try and parse a reg name, returns number of chars consumed */
209 parse_reg (src, mode, reg)
216 if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0)
219 *reg = (src[1] - '0');
228 if (src[2] >= '0' && src[2] <= '5')
231 *reg = 10 + src[2] - '0';
235 if (src[1] >= '0' && src[1] <= '9')
238 *reg = (src[1] - '0');
243 if (src[0] == 's' && src[1] == 's' && src[2] == 'r')
249 if (src[0] == 's' && src[1] == 'p' && src[2] == 'c')
255 if (src[0] == 's' && src[1] == 'r')
261 if (src[0] == 's' && src[1] == 'p')
268 if (src[0] == 'p' && src[1] == 'r')
273 if (src[0] == 'p' && src[1] == 'c')
278 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r')
283 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r')
289 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c')
302 /* start-sanitize-sh3e */
303 if (src[0] == 'f' && src[1] == 'r')
307 if (src[3] >= '0' && src[3] <= '5')
310 *reg = 10 + src[3] - '0';
314 if (src[2] >= '0' && src[2] <= '9')
317 *reg = (src[2] - '0');
321 if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l')
327 if (src[0] == 'f' && src[1] == 'p'
328 && src[2] == 's' && src[3] == 'c' && src[4] == 'r')
333 /* end-sanitize-sh3e */
338 static symbolS *dot()
342 /* JF: '.' is pseudo symbol with value of current location
343 in current segment. */
344 fake = FAKE_LABEL_NAME;
345 return symbol_new (fake,
347 (valueT) frag_now_fix (),
361 save = input_line_pointer;
362 input_line_pointer = s;
363 expression (&immediate);
364 if (immediate.X_op == O_absent)
365 as_bad ("missing operand");
366 new = input_line_pointer;
367 input_line_pointer = save;
372 /* The many forms of operand:
375 @Rn Register indirect
388 pr, gbr, vbr, macl, mach
403 /* Must be predecrement */
406 len = parse_reg (src, &mode, &(op->reg));
408 as_bad ("illegal register after @-");
413 else if (src[0] == '(')
415 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
418 len = parse_reg (src, &mode, &(op->reg));
419 if (len && mode == A_REG_N)
424 as_bad ("must be @(r0,...)");
428 /* Now can be rn or gbr */
429 len = parse_reg (src, &mode, &(op->reg));
434 else if (mode == A_REG_N)
436 op->type = A_IND_R0_REG_N;
440 as_bad ("syntax error in @(r0,...)");
445 /* Must be an @(disp,.. thing) */
446 src = parse_exp (src);
449 /* Now can be rn, gbr or pc */
450 len = parse_reg (src, &mode, &op->reg);
455 op->type = A_DISP_REG_N;
457 else if (mode == A_GBR)
459 op->type = A_DISP_GBR;
461 else if (mode == A_DISP_PC)
463 /* Turn a plain @(4,pc) into @(.+4,pc) */
464 if (immediate.X_op == O_constant) {
465 immediate.X_add_symbol = dot();
466 immediate.X_op = O_symbol;
468 op->type = A_DISP_PC;
472 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
477 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
482 as_bad ("expecting )");
488 src += parse_reg (src, &mode, &(op->reg));
491 as_bad ("illegal register after @");
507 get_operand (ptr, op)
518 *ptr = parse_exp (src);
523 else if (src[0] == '@')
525 *ptr = parse_at (src, op);
528 len = parse_reg (src, &mode, &(op->reg));
537 /* Not a reg, the only thing left is a displacement */
538 *ptr = parse_exp (src);
539 op->type = A_DISP_PC;
546 get_operands (info, args, operand)
547 sh_opcode_info *info;
549 sh_operand_info *operand;
557 get_operand (&ptr, operand + 0);
564 get_operand (&ptr, operand + 1);
565 /* start-sanitize-sh3e */
572 get_operand (&ptr, operand + 2);
578 /* end-sanitize-sh3e */
583 /* start-sanitize-sh3e */
585 /* end-sanitize-sh3e */
592 /* start-sanitize-sh3e */
594 /* end-sanitize-sh3e */
599 /* Passed a pointer to a list of opcodes which use different
600 addressing modes, return the opcode which matches the opcodes
606 get_specific (opcode, operands)
607 sh_opcode_info *opcode;
608 sh_operand_info *operands;
610 sh_opcode_info *this_try = opcode;
611 char *name = opcode->name;
616 if (this_try->name != name)
618 /* We've looked so far down the table that we've run out of
619 opcodes with the same name */
622 /* look at both operands needed by the opcodes and provided by
623 the user - since an arg test will often fail on the same arg
624 again and again, we'll try and test the last failing arg the
625 first on each opcode try */
627 for (n = 0; this_try->arg[n]; n++)
629 sh_operand_info *user = operands + n;
630 sh_arg_type arg = this_try->arg[n];
641 if (user->type != arg)
645 /* opcode needs r0 */
646 if (user->type != A_REG_N || user->reg != 0)
650 if (user->type != A_R0_GBR || user->reg != 0)
653 /* start-sanitize-sh3e */
655 if (user->type != F_REG_N || user->reg != 0)
658 /* end-sanitize-sh3e */
666 /* start-sanitize-sh3e */
670 /* end-sanitize-sh3e */
671 /* Opcode needs rn */
672 if (user->type != arg)
681 if (user->type != arg)
686 if (user->type != arg)
697 /* Opcode needs rn */
698 if (user->type != arg - A_REG_M + A_REG_N)
703 /* start-sanitize-sh3e */
707 /* Opcode needs rn */
708 if (user->type != arg - F_REG_M + F_REG_N)
712 /* end-sanitize-sh3e */
715 printf ("unhandled %d\n", arg);
727 check (operand, low, high)
728 expressionS *operand;
732 if (operand->X_op != O_constant
733 || operand->X_add_number < low
734 || operand->X_add_number > high)
736 as_bad ("operand must be absolute in range %d..%d", low, high);
738 return operand->X_add_number;
743 insert (where, how, pcrel)
748 fix_new_exp (frag_now,
749 where - frag_now->fr_literal,
758 sh_opcode_info *opcode;
760 int high_byte = target_big_endian ? 0 : 1;
763 if (opcode->arg[0] == A_BDISP8)
765 p = frag_var (rs_machine_dependent,
766 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
767 md_relax_table[C (COND_JUMP, COND8)].rlx_length,
769 immediate.X_add_symbol,
770 immediate.X_add_number,
772 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
774 else if (opcode->arg[0] == A_BDISP12)
776 p = frag_var (rs_machine_dependent,
777 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
778 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
780 immediate.X_add_symbol,
781 immediate.X_add_number,
783 p[high_byte] = (opcode->nibbles[0] << 4);
788 /* Now we know what sort of opcodes it is, lets build the bytes -
791 build_Mytes (opcode, operand)
792 sh_opcode_info *opcode;
793 sh_operand_info *operand;
798 char *output = frag_more (2);
799 int low_byte = target_big_endian ? 1 : 0;
805 for (index = 0; index < 4; index++)
807 sh_nibble_type i = opcode->nibbles[index];
823 nbuf[index] = reg_b | 0x08;
826 insert (output + low_byte, R_SH_IMM4, 0);
829 insert (output + low_byte, R_SH_IMM4BY4, 0);
832 insert (output + low_byte, R_SH_IMM4BY2, 0);
835 insert (output + low_byte, R_SH_IMM4, 0);
838 insert (output + low_byte, R_SH_IMM8BY4, 0);
841 insert (output + low_byte, R_SH_IMM8BY2, 0);
844 insert (output + low_byte, R_SH_IMM8, 0);
847 insert (output, R_SH_PCRELIMM8BY4, 1);
850 insert (output, R_SH_PCRELIMM8BY2, 1);
853 printf ("failed for %d\n", i);
857 if (! target_big_endian) {
858 output[1] = (nbuf[0] << 4) | (nbuf[1]);
859 output[0] = (nbuf[2] << 4) | (nbuf[3]);
862 output[0] = (nbuf[0] << 4) | (nbuf[1]);
863 output[1] = (nbuf[2] << 4) | (nbuf[3]);
867 /* This is the guts of the machine-dependent assembler. STR points to a
868 machine dependent instruction. This function is supposed to emit
869 the frags/bytes it assembles to.
876 unsigned char *op_start;
877 unsigned char *op_end;
878 sh_operand_info operand[3];
879 sh_opcode_info *opcode;
882 /* Drop leading whitespace */
886 /* find the op code end */
887 for (op_start = op_end = (unsigned char *) (str);
890 && !is_end_of_line[*op_end] && *op_end != ' ';
893 name[nlen] = op_start[nlen];
900 as_bad ("can't find opcode ");
903 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
907 as_bad ("unknown opcode");
911 if (opcode->arg[0] == A_BDISP12
912 || opcode->arg[0] == A_BDISP8)
914 parse_exp (op_end + 1);
915 build_relax (opcode);
919 if (opcode->arg[0] != A_END)
921 get_operands (opcode, op_end, operand);
923 opcode = get_specific (opcode, operand);
927 /* Couldn't find an opcode which matched the operands */
928 char *where = frag_more (2);
932 as_bad ("invalid operands for opcode");
936 build_Mytes (opcode, operand);
942 DEFUN (tc_crawl_symbol_chain, (headers),
943 object_headers * headers)
945 printf ("call to tc_crawl_symbol_chain \n");
949 DEFUN (md_undefined_symbol, (name),
956 DEFUN (tc_headers_hook, (headers),
957 object_headers * headers)
959 printf ("call to tc_headers_hook \n");
962 /* Various routines to kill one day */
963 /* Equal to MAX_PRECISION in atof-ieee.c */
964 #define MAX_LITTLENUMS 6
966 /* Turn a string in input_line_pointer into a floating point constant of type
967 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
968 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
971 md_atof (type, litP, sizeP)
977 LITTLENUM_TYPE words[4];
993 return "bad call to md_atof";
996 t = atof_ieee (input_line_pointer, type, words);
998 input_line_pointer = t;
1002 if (! target_big_endian)
1004 for (i = prec - 1; i >= 0; i--)
1006 md_number_to_chars (litP, (valueT) words[i], 2);
1012 for (i = 0; i < prec; i++)
1014 md_number_to_chars (litP, (valueT) words[i], 2);
1022 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
1023 call instruction. It refers to a label of the instruction which
1024 loads the register which the call uses. We use it to generate a
1025 special reloc for the linker. */
1034 as_warn (".uses pseudo-op seen when not relaxing");
1038 if (ex.X_op != O_symbol || ex.X_add_number != 0)
1040 as_bad ("bad .uses format");
1041 ignore_rest_of_line ();
1045 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, R_SH_USES);
1047 demand_empty_rest_of_line ();
1050 CONST char *md_shortopts = "";
1051 struct option md_longopts[] = {
1053 #define OPTION_RELAX (OPTION_MD_BASE)
1054 #define OPTION_LITTLE (OPTION_MD_BASE+1)
1056 {"relax", no_argument, NULL, OPTION_RELAX},
1057 {"little", no_argument, NULL, OPTION_LITTLE},
1058 {NULL, no_argument, NULL, 0}
1060 size_t md_longopts_size = sizeof(md_longopts);
1063 md_parse_option (c, arg)
1074 target_big_endian = 0;
1085 md_show_usage (stream)
1090 -little generate little endian code\n\
1091 -relax alter jump instructions for long displacements\n");
1094 int md_short_jump_size;
1097 tc_Nout_fix_to_chars ()
1099 printf ("call to tc_Nout_fix_to_chars \n");
1104 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1111 as_fatal ("failed sanity check.");
1115 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1117 addressT from_Nddr, to_Nddr;
1121 as_fatal ("failed sanity check.");
1124 /* This is function is called after the symbol table has been
1125 completed, but before md_convert_frag has been called. If we have
1126 seen any .uses pseudo-ops, they point to an instruction which loads
1127 a register with the address of a function. We look through the
1128 fixups to find where the function address is being loaded from. We
1129 then generate a COUNT reloc giving the number of times that
1130 function address is referred to. The linker uses this information
1131 when doing relaxing, to decide when it can eliminate the stored
1132 function address entirely. */
1135 sh_coff_frob_file ()
1142 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1146 for (fix = segment_info[iseg].fix_root; fix != NULL; fix = fix->fx_next)
1155 if (fix->fx_r_type != R_SH_USES)
1158 /* The R_SH_USES reloc should refer to a defined local
1159 symbol in the same section. */
1160 sym = fix->fx_addsy;
1162 || fix->fx_subsy != NULL
1163 || fix->fx_addnumber != 0
1164 || S_GET_SEGMENT (sym) != iseg
1165 || S_GET_STORAGE_CLASS (sym) == C_EXT)
1167 as_warn_where (fix->fx_file, fix->fx_line,
1168 ".uses does not refer to a local symbol in the same section");
1172 /* Look through the fixups again, this time looking for one
1173 at the same location as sym. */
1174 val = S_GET_VALUE (sym);
1175 paddr = segment_info[iseg].scnhdr.s_paddr;
1176 for (fscan = segment_info[iseg].fix_root;
1178 fscan = fscan->fx_next)
1179 if (val == paddr + fscan->fx_frag->fr_address + fscan->fx_where)
1183 as_warn_where (fix->fx_file, fix->fx_line,
1184 "can't find fixup pointed to by .uses");
1188 if (fscan->fx_tcbit)
1190 /* We've already done this one. */
1194 /* fscan should also be a fixup to a local symbol in the same
1196 sym = fscan->fx_addsy;
1198 || fscan->fx_subsy != NULL
1199 || fscan->fx_addnumber != 0
1200 || S_GET_SEGMENT (sym) != iseg
1201 || S_GET_STORAGE_CLASS (sym) == C_EXT)
1203 as_warn_where (fix->fx_file, fix->fx_line,
1204 ".uses target does not refer to a local symbol in the same section");
1208 /* Now we look through all the fixups of all the sections,
1209 counting the number of times we find a reference to sym. */
1211 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1213 paddr = segment_info[iscan].scnhdr.s_paddr;
1214 for (fscan = segment_info[iscan].fix_root;
1216 fscan = fscan->fx_next)
1218 if (fscan->fx_addsy == sym)
1221 fscan->fx_tcbit = 1;
1229 /* Generate a R_SH_COUNT fixup at the location of sym. We
1230 have already adjusted the value of sym to include the
1231 fragment address, so we undo that adjustment here. */
1232 subseg_change (iseg, 0);
1233 fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
1234 4, &abs_symbol, count, 0, R_SH_COUNT);
1239 /* Called after relaxing. Set the correct sizes of the fragments, and
1240 create relocs so that md_apply_fix will fill in the correct values. */
1243 md_convert_frag (headers, seg, fragP)
1244 object_headers *headers;
1250 switch (fragP->fr_subtype)
1252 case C (COND_JUMP, COND8):
1253 subseg_change (seg, 0);
1254 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1255 1, R_SH_PCDISP8BY2);
1260 case C (UNCOND_JUMP, UNCOND12):
1261 subseg_change (seg, 0);
1262 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1268 case C (UNCOND_JUMP, UNCOND32):
1269 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1270 if (fragP->fr_symbol == NULL)
1271 as_bad ("at 0x%lx, displacement overflows 12-bit field",
1272 (unsigned long) fragP->fr_address);
1274 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1275 (unsigned long) fragP->fr_address,
1276 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1277 S_GET_NAME (fragP->fr_symbol));
1279 #if 0 /* This code works, but generates poor code and the compiler
1280 should never produce a sequence that requires it to be used. */
1282 /* A jump wont fit in 12 bits, make code which looks like
1288 int t = buffer[0] & 0x10;
1290 buffer[highbyte] = 0xa0; /* branch over move and disp */
1291 buffer[lowbyte] = 3;
1292 buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1293 buffer[lowbyte+2] = 0x00;
1295 buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
1296 buffer[lowbyte+4] = 0;
1297 buffer[highbyte+6] = 0;
1298 buffer[lowbyte+6] = 0;
1300 buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1301 buffer[lowbyte+8] = t ? 0xb : 0x2b;
1303 buffer[highbyte+10] = 0x20; /* build nop */
1304 buffer[lowbyte+10] = 0x0b;
1306 /* Make reloc for the long disp */
1314 fragP->fr_fix += UNCOND32_LENGTH;
1321 case C (COND_JUMP, COND12):
1322 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1324 unsigned char *buffer =
1325 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1326 int highbyte = target_big_endian ? 0 : 1;
1327 int lowbyte = target_big_endian ? 1 : 0;
1329 /* Toggle the true/false bit of the bcond. */
1330 buffer[highbyte] ^= 0x2;
1332 /* Build a relocation to six bytes farther on. */
1333 subseg_change (seg, 0);
1334 fix_new (fragP, fragP->fr_fix, 2,
1335 segment_info[seg].dot,
1336 fragP->fr_address + fragP->fr_fix + 6,
1337 1, R_SH_PCDISP8BY2);
1339 /* Set up a jump instruction. */
1340 buffer[highbyte + 2] = 0xa0;
1341 buffer[lowbyte + 2] = 0;
1342 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1343 fragP->fr_offset, 1, R_SH_PCDISP);
1345 /* Fill in a NOP instruction. */
1346 buffer[highbyte + 4] = 0x0;
1347 buffer[lowbyte + 4] = 0x9;
1355 case C (COND_JUMP, COND32):
1356 case C (COND_JUMP, UNDEF_WORD_DISP):
1357 if (fragP->fr_symbol == NULL)
1358 as_bad ("at %0xlx, displacement overflows 8-bit field",
1359 (unsigned long) fragP->fr_address);
1361 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1362 (unsigned long) fragP->fr_address,
1363 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1364 S_GET_NAME (fragP->fr_symbol));
1366 #if 0 /* This code works, but generates poor code, and the compiler
1367 should never produce a sequence that requires it to be used. */
1369 /* A bcond won't fit and it won't go into a 12 bit
1370 displacement either, the code sequence looks like:
1379 buffer[0] ^= 0x2; /* Toggle T/F bit */
1381 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1382 buffer[2] = 0xd0 | JREG; /* Build mov insn */
1384 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
1386 buffer[6] = 0x20; /* build nop */
1388 buffer[8] = 0; /* space for 32 bit jump disp */
1394 /* Make reloc for the long disp */
1402 fragP->fr_fix += COND32_LENGTH;
1413 if (donerelax && !sh_relax)
1414 as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
1415 (unsigned long) fragP->fr_address,
1416 fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1417 (unsigned long) fragP->fr_offset);
1421 DEFUN (md_section_align, (seg, size),
1425 return ((size + (1 << section_alignment[(int) seg]) - 1)
1426 & (-1 << section_alignment[(int) seg]));
1430 /* When relaxing, we need to output a reloc for any .align directive
1431 that requests alignment to a four byte boundary or larger. */
1434 sh_handle_align (frag)
1438 && frag->fr_type == rs_align
1439 && frag->fr_address + frag->fr_fix > 0
1440 && frag->fr_offset > 1)
1441 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1445 /* This macro decides whether a particular reloc is an entry in a
1446 switch table. It is used when relaxing, because the linker needs
1447 to know about all such entries so that it can adjust them if
1450 #define SWITCH_TABLE(fix) \
1451 ((fix)->fx_addsy != NULL \
1452 && (fix)->fx_subsy != NULL \
1453 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
1454 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
1455 && ((fix)->fx_r_type == R_SH_IMM32 \
1456 || (fix)->fx_r_type == R_SH_IMM16 \
1457 || ((fix)->fx_r_type == 0 \
1458 && ((fix)->fx_size == 2 \
1459 || (fix)->fx_size == 4))))
1461 /* See whether we need to force a relocation into the output file.
1462 This is used to force out switch and PC relative relocations when
1466 sh_force_relocation (fix)
1472 return (fix->fx_pcrel
1473 || SWITCH_TABLE (fix)
1474 || fix->fx_r_type == R_SH_COUNT
1475 || fix->fx_r_type == R_SH_ALIGN);
1478 /* Apply a fixup to the object file. */
1481 md_apply_fix (fixP, val)
1485 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1486 int lowbyte = target_big_endian ? 1 : 0;
1487 int highbyte = target_big_endian ? 0 : 1;
1489 if (fixP->fx_r_type == 0)
1491 if (fixP->fx_size == 2)
1492 fixP->fx_r_type = R_SH_IMM16;
1493 else if (fixP->fx_size == 4)
1494 fixP->fx_r_type = R_SH_IMM32;
1495 else if (fixP->fx_size == 1)
1496 fixP->fx_r_type = R_SH_IMM8;
1501 switch (fixP->fx_r_type)
1504 *buf = (*buf & 0xf0) | (val & 0xf);
1508 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1512 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1527 case R_SH_PCRELIMM8BY4:
1528 /* The lower two bits of the PC are cleared before the
1529 displacement is added in. We can assume that the destination
1530 is on a 4 byte bounday. If this instruction is also on a 4
1531 byte boundary, then we want
1533 and target - here is a multiple of 4.
1534 Otherwise, we are on a 2 byte boundary, and we want
1535 (target - (here - 2)) / 4
1536 and target - here is not a multiple of 4. Computing
1537 (target - (here - 2)) / 4 == (target - here + 2) / 4
1538 works for both cases, since in the first case the addition of
1539 2 will be removed by the division. target - here is in the
1541 val = (val + 2) / 4;
1543 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1547 case R_SH_PCRELIMM8BY2:
1550 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1554 case R_SH_PCDISP8BY2:
1556 if (val < -0x80 || val > 0x7f)
1557 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1563 if (val < -0x800 || val >= 0x7ff)
1564 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1565 buf[lowbyte] = val & 0xff;
1566 buf[highbyte] |= (val >> 8) & 0xf;
1570 if (! target_big_endian)
1587 if (! target_big_endian)
1600 /* Pass the value into sh_coff_reloc_mangle. */
1601 fixP->fx_addnumber = val;
1606 /* Nothing to do here. */
1614 int md_long_jump_size;
1616 /* Called just before address relaxation. Return the length
1617 by which a fragment must grow to reach it's destination. */
1620 md_estimate_size_before_relax (fragP, segment_type)
1621 register fragS *fragP;
1622 register segT segment_type;
1624 switch (fragP->fr_subtype)
1626 case C (UNCOND_JUMP, UNDEF_DISP):
1627 /* used to be a branch to somewhere which was unknown */
1628 if (!fragP->fr_symbol)
1630 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1631 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1633 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1635 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1636 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1640 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1641 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1642 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1648 case C (COND_JUMP, UNDEF_DISP):
1649 /* used to be a branch to somewhere which was unknown */
1650 if (fragP->fr_symbol
1651 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1653 /* Got a symbol and it's defined in this segment, become byte
1654 sized - maybe it will fix up */
1655 fragP->fr_subtype = C (COND_JUMP, COND8);
1656 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1658 else if (fragP->fr_symbol)
1660 /* Its got a segment, but its not ours, so it will always be long */
1661 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1662 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1663 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1667 /* We know the abs value */
1668 fragP->fr_subtype = C (COND_JUMP, COND8);
1669 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1674 return fragP->fr_var;
1677 /* Put number into target byte order */
1680 md_number_to_chars (ptr, use, nbytes)
1685 if (! target_big_endian)
1686 number_to_chars_littleendian (ptr, use, nbytes);
1688 number_to_chars_bigendian (ptr, use, nbytes);
1692 md_pcrel_from (fixP)
1695 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
1699 tc_coff_sizemachdep (frag)
1702 return md_relax_table[frag->fr_subtype].rlx_length;
1707 /* Adjust a reloc for the SH. This is similar to the generic code,
1708 but does some minor tweaking. */
1711 sh_coff_reloc_mangle (seg, fix, intr, paddr)
1712 segment_info_type *seg;
1714 struct internal_reloc *intr;
1717 symbolS *symbol_ptr = fix->fx_addsy;
1720 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
1722 if (! SWITCH_TABLE (fix))
1724 intr->r_type = fix->fx_r_type;
1731 if (fix->fx_r_type == R_SH_IMM16)
1732 intr->r_type = R_SH_SWITCH16;
1733 else if (fix->fx_r_type == R_SH_IMM32)
1734 intr->r_type = R_SH_SWITCH32;
1738 /* For a switch reloc, we set r_offset to the difference between
1739 the reloc address and the subtrahend. When the linker is
1740 doing relaxing, it can use the determine the starting and
1741 ending points of the switch difference expression. */
1742 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
1745 /* PC relative relocs are always against the current section. */
1746 if (symbol_ptr == NULL)
1748 switch (fix->fx_r_type)
1750 case R_SH_PCRELIMM8BY2:
1751 case R_SH_PCRELIMM8BY4:
1752 case R_SH_PCDISP8BY2:
1755 symbol_ptr = seg->dot;
1762 if (fix->fx_r_type == R_SH_USES)
1764 /* We can't store the offset in the object file, since this
1765 reloc does not take up any space, so we store it in r_offset.
1766 The fx_addnumber field was set in md_apply_fix. */
1767 intr->r_offset = fix->fx_addnumber;
1769 else if (fix->fx_r_type == R_SH_COUNT)
1771 /* We can't store the count in the object file, since this reloc
1772 does not take up any space, so we store it in r_offset. The
1773 fx_offset field was set when the fixup was created in
1774 sh_coff_frob_file. */
1775 intr->r_offset = fix->fx_offset;
1776 /* This reloc is always absolute. */
1779 else if (fix->fx_r_type == R_SH_ALIGN)
1781 /* Store the alignment in the r_offset field. */
1782 intr->r_offset = fix->fx_offset;
1783 /* This reloc is always absolute. */
1787 /* Turn the segment of the symbol into an offset. */
1788 if (symbol_ptr != NULL)
1790 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1792 intr->r_symndx = dot->sy_number;
1794 intr->r_symndx = symbol_ptr->sy_number;
1797 intr->r_symndx = -1;