1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
3 Copyright (C) 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 Written By Steve Chamberlain
31 #include "../opcodes/sh-opc.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
45 void s_align_bytes ();
54 const pseudo_typeS md_pseudo_table[] =
58 {"form", listing_psize, 0},
60 {"heading", listing_title, 0},
61 {"import", s_ignore, 0},
62 {"page", listing_eject, 0},
63 {"program", s_ignore, 0},
67 /*int md_reloc_size; */
69 static int relax; /* set if -relax seen */
71 const char EXP_CHARS[] = "eE";
73 /* Chars that mean this number is a floating point constant */
76 const char FLT_CHARS[] = "rRsSfFdDxXpP";
78 #define C(a,b) ENCODE_RELAX(a,b)
80 #define JREG 14 /* Register used as a temp when relaxing */
81 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
82 #define GET_WHAT(x) ((x>>4))
84 /* These are the two types of relaxable instrction */
94 #define UNDEF_WORD_DISP 4
102 #define COND8_LENGTH 2
103 #define COND12_F (4094 - 4) /* -4 since there are two extra */
104 /* instructions needed */
105 #define COND12_M -4096
106 #define COND12_LENGTH 6
107 #define COND32_F (1<<30)
108 #define COND32_M -(1<<30)
109 #define COND32_LENGTH 14
111 #define COND8_RANGE(x) ((x) > COND8_M && (x) < COND8_F)
112 #define COND12_RANGE(x) ((x) > COND12_M && (x) < COND12_F)
114 #define UNCOND12_F 4094
115 #define UNCOND12_M -4096
116 #define UNCOND12_LENGTH 2
118 #define UNCOND32_F (1<<30)
119 #define UNCOND32_M -(1<<30)
120 #define UNCOND32_LENGTH 14
123 const relax_typeS md_relax_table[C (END, 0)] = {
124 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
125 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
128 /* C (COND_JUMP, COND8) */
129 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
130 /* C (COND_JUMP, COND12) */
131 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
132 /* C (COND_JUMP, COND32) */
133 { COND32_F, COND32_M, COND32_LENGTH, 0, },
134 { 0 }, { 0 }, { 0 }, { 0 },
135 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
138 /* C (UNCOND_JUMP, UNCOND12) */
139 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
140 /* C (UNCOND_JUMP, UNCOND32) */
141 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
142 { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
143 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
146 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
149 This function is called once, at assembler startup time. This should
150 set up all the tables, etc that the MD part of the assembler needs
156 sh_opcode_info *opcode;
157 char *prev_name = "";
158 register relax_typeS *table;
160 opcode_hash_control = hash_new ();
162 /* Insert unique names into hash table */
163 for (opcode = sh_table; opcode->name; opcode++)
165 if (strcmp (prev_name, opcode->name))
167 prev_name = opcode->name;
168 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
172 /* Make all the opcodes with the same name point to the same
174 opcode->name = prev_name;
181 static expressionS immediate; /* absolute expression */
191 /* try and parse a reg name, returns number of chars consumed */
193 parse_reg (src, mode, reg)
202 if (src[2] >= '0' && src[2] <= '5')
205 *reg = 10 + src[2] - '0';
209 if (src[1] >= '0' && src[1] <= '9')
212 *reg = (src[1] - '0');
217 if (src[0] == 's' && src[1] == 'r')
223 if (src[0] == 's' && src[1] == 'p')
230 if (src[0] == 'p' && src[1] == 'r')
235 if (src[0] == 'p' && src[1] == 'c')
240 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r')
245 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r')
251 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c')
268 static symbolS *dot()
272 /* JF: '.' is pseudo symbol with value of current location
273 in current segment. */
274 fake = FAKE_LABEL_NAME;
275 return symbol_new (fake,
277 (valueT) frag_now_fix (),
291 save = input_line_pointer;
292 input_line_pointer = s;
293 expression (&immediate);
294 if (immediate.X_op == O_absent)
295 as_bad ("missing operand");
296 new = input_line_pointer;
297 input_line_pointer = save;
302 /* The many forms of operand:
305 @Rn Register indirect
318 pr, gbr, vbr, macl, mach
333 /* Must be predecrement */
336 len = parse_reg (src, &mode, &(op->reg));
338 as_bad ("illegal register after @-");
343 else if (src[0] == '(')
345 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
348 len = parse_reg (src, &mode, &(op->reg));
349 if (len && mode == A_REG_N)
354 as_bad ("must be @(r0,...)");
358 /* Now can be rn or gbr */
359 len = parse_reg (src, &mode, &(op->reg));
364 else if (mode == A_REG_N)
366 op->type = A_IND_R0_REG_N;
370 as_bad ("syntax error in @(r0,...)");
375 /* Must be an @(disp,.. thing) */
376 src = parse_exp (src);
379 /* Now can be rn, gbr or pc */
380 len = parse_reg (src, &mode, &op->reg);
385 op->type = A_DISP_REG_N;
387 else if (mode == A_GBR)
389 op->type = A_DISP_GBR;
391 else if (mode == A_DISP_PC)
393 /* Turn a plain @(4,pc) into @(.+4,pc) */
394 if (immediate.X_op == O_constant) {
395 immediate.X_add_symbol = dot();
396 immediate.X_op = O_symbol;
398 op->type = A_DISP_PC;
402 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
407 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
412 as_bad ("expecting )");
418 src += parse_reg (src, &mode, &(op->reg));
421 as_bad ("illegal register after @");
437 get_operand (ptr, op)
448 *ptr = parse_exp (src);
453 else if (src[0] == '@')
455 *ptr = parse_at (src, op);
458 len = parse_reg (src, &mode, &(op->reg));
467 /* Not a reg, the only thing left is a displacement */
468 *ptr = parse_exp (src);
469 op->type = A_DISP_PC;
476 get_operands (info, args, operand)
477 sh_opcode_info *info;
479 sh_operand_info *operand;
487 get_operand (&ptr, operand + 0);
494 get_operand (&ptr, operand + 1);
509 /* Passed a pointer to a list of opcodes which use different
510 addressing modes, return the opcode which matches the opcodes
516 get_specific (opcode, operands)
517 sh_opcode_info *opcode;
518 sh_operand_info *operands;
520 sh_opcode_info *this_try = opcode;
521 char *name = opcode->name;
527 if (this_try->name != name)
529 /* We've looked so far down the table that we've run out of
530 opcodes with the same name */
533 /* look at both operands needed by the opcodes and provided by
534 the user - since an arg test will often fail on the same arg
535 again and again, we'll try and test the last failing arg the
536 first on each opcode try */
538 for (n = 0; this_try->arg[n]; n++)
540 sh_operand_info *user = operands + arg_to_test;
541 sh_arg_type arg = this_try->arg[arg_to_test];
552 if (user->type != arg)
556 /* opcode needs r0 */
557 if (user->type != A_REG_N || user->reg != 0)
561 if (user->type != A_R0_GBR || user->reg != 0)
571 /* Opcode needs rn */
572 if (user->type != arg)
579 if (user->type != arg)
589 /* Opcode needs rn */
590 if (user->type != arg - A_REG_M + A_REG_N)
595 printf ("unhandled %d\n", arg);
598 /* If we did 0, test 1 next, else 0 */
599 arg_to_test = 1 - arg_to_test;
609 check (operand, low, high)
610 expressionS *operand;
614 if (operand->X_op != O_constant
615 || operand->X_add_number < low
616 || operand->X_add_number > high)
618 as_bad ("operand must be absolute in range %d..%d", low, high);
620 return operand->X_add_number;
625 insert (where, how, pcrel)
630 fix_new_exp (frag_now,
631 where - frag_now->fr_literal,
641 sh_opcode_info *opcode;
644 int high_byte = shl ? 1 : 0 ;
647 if (opcode->arg[0] == A_BDISP8)
649 p = frag_var (rs_machine_dependent,
650 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
651 len = md_relax_table[C (COND_JUMP, COND8)].rlx_length,
653 immediate.X_add_symbol,
654 immediate.X_add_number,
656 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
658 else if (opcode->arg[0] == A_BDISP12)
660 p = frag_var (rs_machine_dependent,
661 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
662 len = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
664 immediate.X_add_symbol,
665 immediate.X_add_number,
667 p[high_byte] = (opcode->nibbles[0] << 4);
672 /* Now we know what sort of opcodes it is, lets build the bytes -
675 build_Mytes (opcode, operand)
676 sh_opcode_info *opcode;
677 sh_operand_info *operand;
682 char *output = frag_more (2);
683 int low_byte = shl ? 0 : 1;
689 for (index = 0; index < 4; index++)
691 sh_nibble_type i = opcode->nibbles[index];
707 insert (output + low_byte, R_SH_IMM4, 0);
710 insert (output + low_byte, R_SH_IMM4BY4, 0);
713 insert (output + low_byte, R_SH_IMM4BY2, 0);
716 insert (output + low_byte, R_SH_IMM4, 0);
719 insert (output + low_byte, R_SH_IMM8BY4, 0);
722 insert (output + low_byte, R_SH_IMM8BY2, 0);
725 insert (output + low_byte, R_SH_IMM8, 0);
728 insert (output, R_SH_PCRELIMM8BY4, 1);
731 insert (output, R_SH_PCRELIMM8BY2, 1);
734 printf ("failed for %d\n", i);
739 output[1] = (nbuf[0] << 4) | (nbuf[1]);
740 output[0] = (nbuf[2] << 4) | (nbuf[3]);
743 output[0] = (nbuf[0] << 4) | (nbuf[1]);
744 output[1] = (nbuf[2] << 4) | (nbuf[3]);
748 /* This is the guts of the machine-dependent assembler. STR points to a
749 machine dependent instruction. This function is supposed to emit
750 the frags/bytes it assembles to.
757 unsigned char *op_start;
758 unsigned char *op_end;
759 sh_operand_info operand[2];
760 sh_opcode_info *opcode;
764 /* Drop leading whitespace */
768 /* find the op code end */
769 for (op_start = op_end = (unsigned char *) (str);
772 && !is_end_of_line[*op_end] && *op_end != ' ';
775 name[nlen] = op_start[nlen];
782 as_bad ("can't find opcode ");
785 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
789 as_bad ("unknown opcode");
793 if (opcode->arg[0] == A_BDISP12
794 || opcode->arg[0] == A_BDISP8)
796 parse_exp (op_end + 1);
797 build_relax (opcode);
801 if (opcode->arg[0] != A_END)
803 get_operands (opcode, op_end, operand);
805 opcode = get_specific (opcode, operand);
809 /* Couldn't find an opcode which matched the operands */
810 char *where = frag_more (2);
814 as_bad ("invalid operands for opcode");
818 build_Mytes (opcode, operand);
824 DEFUN (tc_crawl_symbol_chain, (headers),
825 object_headers * headers)
827 printf ("call to tc_crawl_symbol_chain \n");
831 DEFUN (md_undefined_symbol, (name),
838 DEFUN (tc_headers_hook, (headers),
839 object_headers * headers)
841 printf ("call to tc_headers_hook \n");
844 /* Various routines to kill one day */
845 /* Equal to MAX_PRECISION in atof-ieee.c */
846 #define MAX_LITTLENUMS 6
848 /* Turn a string in input_line_pointer into a floating point constant of type
849 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
850 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
853 md_atof (type, litP, sizeP)
859 LITTLENUM_TYPE words[4];
875 return "bad call to md_atof";
878 t = atof_ieee (input_line_pointer, type, words);
880 input_line_pointer = t;
886 for (i = prec - 1; i >= 0; i--)
888 md_number_to_chars (litP, (valueT) words[i], 2);
894 for (i = 0; i < prec; i++)
896 md_number_to_chars (litP, (valueT) words[i], 2);
906 CONST char *md_shortopts = "";
907 struct option md_longopts[] = {
909 #define OPTION_RELAX (OPTION_MD_BASE)
910 #define OPTION_LITTLE (OPTION_MD_BASE+1)
912 {"relax", no_argument, NULL, OPTION_RELAX},
913 {"little", no_argument, NULL, OPTION_LITTLE},
914 {NULL, no_argument, NULL, 0}
916 size_t md_longopts_size = sizeof(md_longopts);
919 md_parse_option (c, arg)
940 md_show_usage (stream)
945 -relax alter jump instructions for long displacements\n");
948 int md_short_jump_size;
951 tc_Nout_fix_to_chars ()
953 printf ("call to tc_Nout_fix_to_chars \n");
958 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
965 as_fatal ("failed sanity check.");
969 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
971 addressT from_Nddr, to_Nddr;
975 as_fatal ("failed sanity check.");
979 called after relaxing, change the frags so they know how big they are
982 md_convert_frag (headers, fragP)
983 object_headers *headers;
987 unsigned char *buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
989 int highbyte = shl ? 1 : 0;
990 int lowbyte = shl ? 0 : 1;
991 int targ_addr = ((fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0) + fragP->fr_offset);
992 switch (fragP->fr_subtype)
994 case C (COND_JUMP, COND8):
996 /* Get the address of the end of the instruction */
997 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
999 int disp = targ_addr - next_inst - 2;
1002 md_number_to_chars (buffer + lowbyte, disp & 0xff, 1);
1008 case C (UNCOND_JUMP, UNCOND12):
1010 /* Get the address of the end of the instruction */
1011 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1014 int disp = targ_addr - next_inst - 2;
1017 t = buffer[highbyte] & 0xf0;
1018 md_number_to_chars (buffer, disp & 0xfff, 2);
1019 buffer[highbyte] = (buffer[highbyte] & 0xf) | t;
1025 case C (UNCOND_JUMP, UNCOND32):
1026 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1028 /* A jump wont fit in 12 bits, make code which looks like
1036 fragP->fr_fix + fragP->fr_address + UNCOND32_LENGTH;
1038 int disp = targ_addr - next_inst;
1039 int t = buffer[0] & 0x10;
1043 buffer[highbyte] = 0xa0; /* branch over move and disp */
1044 buffer[lowbyte] = 3;
1045 buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1046 buffer[lowbyte+2] = 0x00;
1048 buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
1049 buffer[lowbyte+4] = 0;
1050 buffer[highbyte+6] = 0;
1051 buffer[lowbyte+6] = 0;
1053 buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1054 buffer[lowbyte+8] = t ? 0xb : 0x2b;
1056 buffer[highbyte+10] = 0x20; /* build nop */
1057 buffer[lowbyte+10] = 0x0b;
1059 /* Make reloc for the long disp */
1067 fragP->fr_fix += UNCOND32_LENGTH;
1074 case C (COND_JUMP, COND12):
1076 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1078 fragP->fr_fix + fragP->fr_address + 6;
1080 int disp = targ_addr - next_inst;
1082 md_number_to_chars (buffer + 2, disp & 0xfff, 2);
1083 buffer[highbyte] ^= 0x2; /* Toggle T/F bit */
1084 buffer[lowbyte] = 1; /* branch over jump and nop */
1085 buffer[highbyte+2] = (buffer[highbyte+2] & 0xf) | 0xa0; /* Build jump insn */
1086 buffer[lowbyte+5] = 0x20; /* Build nop */
1087 buffer[lowbyte+5] = 0x0b;
1094 case C (COND_JUMP, COND32):
1095 case C (COND_JUMP, UNDEF_WORD_DISP):
1097 /* A bcond won't fit and it won't go into a 12 bit
1098 displacement either, the code sequence looks like:
1108 fragP->fr_fix + fragP->fr_address + COND32_LENGTH;
1110 int disp = targ_addr - next_inst;
1113 buffer[0] ^= 0x2; /* Toggle T/F bit */
1115 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1116 buffer[2] = 0xd0 | JREG; /* Build mov insn */
1118 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
1120 buffer[6] = 0x20; /* build nop */
1122 buffer[8] = 0; /* space for 32 bit jump disp */
1128 /* Make reloc for the long disp */
1136 fragP->fr_fix += COND32_LENGTH;
1146 if (donerelax && !relax)
1148 as_warn ("Offset doesn't fit at 0x%x, trying to get to %s+0x%x",
1150 fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1157 DEFUN (md_section_align, (seg, size),
1161 return ((size + (1 << section_alignment[(int) seg]) - 1)
1162 & (-1 << section_alignment[(int) seg]));
1167 md_apply_fix (fixP, val)
1171 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1172 int addr = fixP->fx_frag->fr_address + fixP->fx_where;
1173 int lowbyte = shl ? 0 : 1;
1174 if (fixP->fx_r_type == 0)
1176 if (fixP->fx_size == 2)
1177 fixP->fx_r_type = R_SH_IMM16;
1179 fixP->fx_r_type = R_SH_IMM32;
1182 switch (fixP->fx_r_type)
1186 *buf = (*buf & 0xf0) | (val & 0xf);
1190 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1194 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1209 case R_SH_PCRELIMM8BY4:
1213 as_warn ("non aligned displacement at %x\n", addr);
1215 /* val -= (addr + 4); */
1222 as_warn ("pcrel too far at %x\n", addr);
1226 case R_SH_PCRELIMM8BY2:
1228 /* if ((val & 0x1) != shl)
1229 as_bad ("odd displacement at %x\n", addr);*/
1230 /* val -= (addr + 4); */
1235 as_warn ("pcrel too far at %x\n", addr);
1275 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1279 int md_long_jump_size;
1282 called just before address relaxation, return the length
1283 by which a fragment must grow to reach it's destination
1286 md_estimate_size_before_relax (fragP, segment_type)
1287 register fragS *fragP;
1288 register segT segment_type;
1290 switch (fragP->fr_subtype)
1292 case C (UNCOND_JUMP, UNDEF_DISP):
1293 /* used to be a branch to somewhere which was unknown */
1294 if (!fragP->fr_symbol)
1296 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1297 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1299 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1301 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1302 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1306 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1307 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1308 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1314 case C (COND_JUMP, UNDEF_DISP):
1315 /* used to be a branch to somewhere which was unknown */
1316 if (fragP->fr_symbol
1317 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1319 /* Got a symbol and it's defined in this segment, become byte
1320 sized - maybe it will fix up */
1321 fragP->fr_subtype = C (COND_JUMP, COND8);
1322 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1324 else if (fragP->fr_symbol)
1326 /* Its got a segment, but its not ours, so it will always be long */
1327 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1328 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1329 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1333 /* We know the abs value */
1334 fragP->fr_subtype = C (COND_JUMP, COND8);
1335 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1340 return fragP->fr_var;
1343 /* Put number into target byte order */
1346 md_number_to_chars (ptr, use, nbytes)
1352 number_to_chars_littleendian (ptr, use, nbytes);
1354 number_to_chars_bigendian (ptr, use, nbytes);
1358 md_pcrel_from (fixP)
1361 int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1 ;
1366 tc_coff_fix2rtype (fix_ptr)
1369 if (fix_ptr->fx_r_type == RELOC_32)
1371 /* cons likes to create reloc32's whatever the size of the reloc..
1373 switch (fix_ptr->fx_size)
1389 tc_coff_sizemachdep (frag)
1392 return md_relax_table[frag->fr_subtype].rlx_length;
1396 sh_init_after_args()