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"
34 const char comment_chars[] = "!";
35 const char line_separator_chars[] = ";";
36 const char line_comment_chars[] = "!";
38 /* This table describes all the machine specific pseudo-ops the assembler
39 has to support. The fields are:
40 pseudo-op name without dot
41 function to call to execute this pseudo-op
42 Integer arg to pass to the function
46 void s_align_bytes ();
48 const pseudo_typeS md_pseudo_table[] =
52 {"form", listing_psize, 0},
53 {"heading", listing_title, 0},
54 {"import", s_ignore, 0},
55 {"page", listing_eject, 0},
56 {"program", s_ignore, 0},
60 /*int md_reloc_size;*/
62 static int relax; /* set if -relax seen */
64 const char EXP_CHARS[] = "eE";
66 /* Chars that mean this number is a floating point constant */
69 const char FLT_CHARS[] = "rRsSfFdDxXpP";
71 #define C(a,b) ENCODE_RELAX(a,b)
73 #define JREG 14 /* Register used as a temp when relaxing */
74 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
75 #define GET_WHAT(x) ((x>>4))
77 /* These are the two types of relaxable instrction */
87 #define UNDEF_WORD_DISP 4
95 #define COND8_LENGTH 2
96 #define COND12_F (4094 - 4) /* -4 since there are two extra */
97 /* instructions needed */
98 #define COND12_M -4096
99 #define COND12_LENGTH 6
100 #define COND32_F (1<<30)
101 #define COND32_M -(1<<30)
102 #define COND32_LENGTH 14
104 #define COND8_RANGE(x) ((x) > COND8_M && (x) < COND8_F)
105 #define COND12_RANGE(x) ((x) > COND12_M && (x) < COND12_F)
107 #define UNCOND12_F 4094
108 #define UNCOND12_M -4096
109 #define UNCOND12_LENGTH 2
111 #define UNCOND32_F (1<<30)
112 #define UNCOND32_M -(1<<30)
113 #define UNCOND32_LENGTH 14
116 const relax_typeS md_relax_table[C (END, 0)];
118 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
121 This function is called once, at assembler startup time. This should
122 set up all the tables, etc that the MD part of the assembler needs
128 sh_opcode_info *opcode;
129 char *prev_name = "";
131 opcode_hash_control = hash_new ();
133 /* Insert unique names into hash table */
134 for (opcode = sh_table; opcode->name; opcode++)
136 if (strcmp (prev_name, opcode->name))
138 prev_name = opcode->name;
139 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
143 /* Make all the opcodes with the same name point to the same
145 opcode->name = prev_name;
149 /* Initialize the relax table */
150 md_relax_table[C (COND_JUMP, COND8)].rlx_forward = COND8_F;
151 md_relax_table[C (COND_JUMP, COND8)].rlx_backward = COND8_M;
152 md_relax_table[C (COND_JUMP, COND8)].rlx_length = COND8_LENGTH;
153 md_relax_table[C (COND_JUMP, COND8)].rlx_more = C (COND_JUMP, COND12);
155 md_relax_table[C (COND_JUMP, COND12)].rlx_forward = COND12_F;
156 md_relax_table[C (COND_JUMP, COND12)].rlx_backward = COND12_M;
157 md_relax_table[C (COND_JUMP, COND12)].rlx_length = COND12_LENGTH;
158 md_relax_table[C (COND_JUMP, COND12)].rlx_more = C (COND_JUMP, COND32);
160 md_relax_table[C (COND_JUMP, COND32)].rlx_forward = COND32_F;
161 md_relax_table[C (COND_JUMP, COND32)].rlx_backward = COND32_M;
162 md_relax_table[C (COND_JUMP, COND32)].rlx_length = COND32_LENGTH;
163 md_relax_table[C (COND_JUMP, COND32)].rlx_more = 0;
166 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_forward = UNCOND12_F;
167 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_backward = UNCOND12_M;
168 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length = UNCOND12_LENGTH;
169 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_more = C (UNCOND_JUMP, UNCOND32);
171 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_forward = UNCOND32_F;
172 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_backward = UNCOND32_M;
173 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length = UNCOND32_LENGTH;
174 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_more = 0;
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')
277 save = input_line_pointer;
280 input_line_pointer = s;
282 seg = expr (0, &immediate);
283 new = input_line_pointer;
284 input_line_pointer = save;
285 if (SEG_NORMAL (seg))
296 as_bad ("Missing operand");
299 as_bad ("Don't understand operand of type %s", segment_name (seg));
305 /* The many forms of operand:
308 @Rn Register indirect
321 pr, gbr, vbr, macl, mach
336 /* Must be predecrement */
339 len = parse_reg (src, &mode, &(op->reg));
341 as_bad ("illegal register after @-");
346 else if (src[0] == '(')
348 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
351 len = parse_reg (src, &mode, &(op->reg));
352 if (len && mode == A_REG_N)
357 as_bad ("must be @(r0,...)");
361 /* Now can be rn or gbr */
362 len = parse_reg (src, &mode, &(op->reg));
367 else if (mode == A_REG_N)
369 op->type = A_IND_R0_REG_N;
373 as_bad ("syntax error in @(r0,...)");
378 /* Must be an @(disp,.. thing) */
379 src = parse_exp (src);
382 /* Now can be rn, gbr or pc */
383 len = parse_reg (src, &mode, &op->reg);
388 op->type = A_DISP_REG_N;
390 else if (mode == A_GBR)
392 op->type = A_DISP_GBR;
394 else if (mode == A_DISP_PC)
396 op->type = A_DISP_PC;
400 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
405 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
410 as_bad ("expecting )");
416 src += parse_reg (src, &mode, &(op->reg));
419 as_bad ("illegal register after @");
435 get_operand (ptr, op)
446 *ptr = parse_exp (src);
451 else if (src[0] == '@')
453 *ptr = parse_at (src, op);
456 len = parse_reg (src, &mode, &(op->reg));
465 /* Not a reg, the only thing left is a displacement */
466 *ptr = parse_exp (src);
467 op->type = A_DISP_PC;
474 get_operands (info, args, operand)
475 sh_opcode_info *info;
477 sh_operand_info *operand;
485 get_operand (&ptr, operand + 0);
492 get_operand (&ptr, operand + 1);
507 /* Passed a pointer to a list of opcodes which use different
508 addressing modes, return the opcode which matches the opcodes
514 get_specific (opcode, operands)
515 sh_opcode_info *opcode;
516 sh_operand_info *operands;
518 sh_opcode_info *this_try = opcode;
519 char *name = opcode->name;
525 if (this_try->name != name)
527 /* We've looked so far down the table that we've run out of
528 opcodes with the same name */
531 /* look at both operands needed by the opcodes and provided by
532 the user - since an arg test will often fail on the same arg
533 again and again, we'll try and test the last failing arg the
534 first on each opcode try */
536 for (n = 0; this_try->arg[n]; n++)
538 sh_operand_info *user = operands + arg_to_test;
539 sh_arg_type arg = this_try->arg[arg_to_test];
550 if (user->type != arg)
554 /* opcode needs r0 */
555 if (user->type != A_REG_N || user->reg != 0)
559 if (user->type != A_R0_GBR || user->reg != 0)
569 /* Opcode needs rn */
570 if (user->type != arg)
577 if (user->type != arg)
587 /* Opcode needs rn */
588 if (user->type != arg - A_REG_M + A_REG_N)
593 printf ("unhandled %d\n", arg);
596 /* If we did 0, test 1 next, else 0 */
597 arg_to_test = 1 - arg_to_test;
607 check (operand, low, high)
608 expressionS *operand;
612 if (operand->X_seg != SEG_ABSOLUTE
613 || operand->X_add_number < low
614 || operand->X_add_number > high)
616 as_bad ("operand must be absolute in range %d..%d", low, high);
618 return operand->X_add_number;
623 insert (where, how, pcrel)
629 where - frag_now->fr_literal,
631 immediate.X_add_symbol,
632 immediate.X_subtract_symbol,
633 immediate.X_add_number,
641 sh_opcode_info *opcode;
645 if (opcode->arg[0] == A_BDISP8)
647 p = frag_var (rs_machine_dependent,
648 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
649 len = md_relax_table[C (COND_JUMP, COND8)].rlx_length,
651 immediate.X_add_symbol,
652 immediate.X_add_number,
654 p[0] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
656 else if (opcode->arg[0] == A_BDISP12)
658 p = frag_var (rs_machine_dependent,
659 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
660 len = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
662 immediate.X_add_symbol,
663 immediate.X_add_number,
665 p[0] = (opcode->nibbles[0] << 4);
670 /* Now we know what sort of opcodes it is, lets build the bytes -
673 build_Mytes (opcode, operand)
674 sh_opcode_info *opcode;
675 sh_operand_info *operand;
680 char *output = frag_more (2);
687 for (index = 0; index < 4; index++)
689 sh_nibble_type i = opcode->nibbles[index];
705 insert (output + 1, R_SH_IMM4, 0);
708 insert (output + 1, R_SH_IMM4BY4, 0);
711 insert (output + 1, R_SH_IMM4BY2, 0);
714 insert (output + 1, R_SH_IMM4, 0);
717 insert (output + 1, R_SH_IMM8BY4, 0);
720 insert (output + 1, R_SH_IMM8BY2, 0);
723 insert (output + 1, R_SH_IMM8, 0);
726 insert (output + 1, R_SH_PCRELIMM8BY4, 0);
729 insert (output + 1, R_SH_PCRELIMM8BY2, 0);
732 printf ("failed for %d\n", i);
736 output[0] = (nbuf[0] << 4) | (nbuf[1]);
737 output[1] = (nbuf[2] << 4) | (nbuf[3]);
740 /* This is the guts of the machine-dependent assembler. STR points to a
741 machine dependent instruction. This function is supposed to emit
742 the frags/bytes it assembles to.
749 unsigned char *op_start;
750 unsigned char *op_end;
751 sh_operand_info operand[2];
752 sh_opcode_info *opcode;
757 /* Drop leading whitespace */
761 /* find the op code end */
762 for (name = op_start = op_end = (unsigned char *) (str);
764 !is_end_of_line[*op_end] && *op_end != ' ';
771 if (op_end == op_start)
773 as_bad ("can't find opcode ");
776 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
780 as_bad ("unknown opcode");
784 if (opcode->arg[0] == A_BDISP12
785 || opcode->arg[0] == A_BDISP8)
787 input_line_pointer = parse_exp (op_end + 1);
788 build_relax (opcode);
792 input_line_pointer = get_operands (opcode, op_end, operand);
794 opcode = get_specific (opcode, operand);
798 /* Couldn't find an opcode which matched the operands */
799 char *where = frag_more (2);
803 as_bad ("invalid operands for opcode");
807 build_Mytes (opcode, operand);
813 DEFUN (tc_crawl_symbol_chain, (headers),
814 object_headers * headers)
816 printf ("call to tc_crawl_symbol_chain \n");
820 DEFUN (md_undefined_symbol, (name),
827 DEFUN (tc_headers_hook, (headers),
828 object_headers * headers)
830 printf ("call to tc_headers_hook \n");
838 /* Various routines to kill one day */
839 /* Equal to MAX_PRECISION in atof-ieee.c */
840 #define MAX_LITTLENUMS 6
842 /* Turn a string in input_line_pointer into a floating point constant of type
843 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
844 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
847 md_atof (type, litP, sizeP)
853 LITTLENUM_TYPE words[MAX_LITTLENUMS];
854 LITTLENUM_TYPE *wordP;
886 return "Bad call to MD_NTOF()";
888 t = atof_ieee (input_line_pointer, type, words);
890 input_line_pointer = t;
892 *sizeP = prec * sizeof (LITTLENUM_TYPE);
893 for (wordP = words; prec--;)
895 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
896 litP += sizeof (LITTLENUM_TYPE);
898 return ""; /* Someone should teach Dean about null pointers */
902 md_parse_option (argP, cntP, vecP)
908 if (!strcmp (*argP, "relax"))
916 int md_short_jump_size;
919 tc_Nout_fix_to_chars ()
921 printf ("call to tc_Nout_fix_to_chars \n");
926 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
933 as_fatal ("failed sanity check.");
937 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
939 addressT from_Nddr, to_Nddr;
943 as_fatal ("failed sanity check.");
947 called after relaxing, change the frags so they know how big they are
950 md_convert_frag (headers, fragP)
951 object_headers *headers;
955 unsigned char *buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
957 int targ_addr = ((fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0) + fragP->fr_offset);
958 switch (fragP->fr_subtype)
960 case C (COND_JUMP, COND8):
962 /* Get the address of the end of the instruction */
963 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
965 int disp = targ_addr - next_inst - 2;
967 md_number_to_chars (buffer + 1, disp, 1);
973 case C (UNCOND_JUMP, UNCOND12):
975 /* Get the address of the end of the instruction */
976 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
979 int disp = targ_addr - next_inst - 2;
982 t = buffer[0] & 0xf0;
983 md_number_to_chars (buffer, disp, 2);
984 buffer[0] = (buffer[0] & 0xf) | t;
990 case C (UNCOND_JUMP, UNCOND32):
991 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
993 /* A jump wont fit in 12 bits, make code which looks like
1001 fragP->fr_fix + fragP->fr_address + UNCOND32_LENGTH;
1003 int disp = targ_addr - next_inst;
1004 int t = buffer[0] & 0x10;
1008 buffer[0] = 0xa0; /* branch over move and disp */
1010 buffer[2] = 0xd0 | JREG;/* Build mov insn */
1013 buffer[4] = 0; /* space for 32 bit jump disp */
1018 buffer[10] = 0x40 | JREG; /* Build jmp @JREG */
1019 buffer[11] = t ? 0xb : 0x2b;
1021 buffer[12] = 0x20; /* build nop */
1024 /* Make reloc for the long disp */
1033 fragP->fr_fix += UNCOND32_LENGTH;
1040 case C (COND_JUMP, COND12):
1042 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1044 fragP->fr_fix + fragP->fr_address + 6;
1046 int disp = targ_addr - next_inst;
1048 md_number_to_chars (buffer + 2, disp, 2);
1049 buffer[0] ^= 0x2; /* Toggle T/F bit */
1050 buffer[1] = 1; /* branch over jump and nop */
1051 buffer[2] = (buffer[2] & 0xf) | 0xa0; /* Build jump insn */
1052 buffer[4] = 0x20; /* Build nop */
1060 case C (COND_JUMP, COND32):
1061 case C (COND_JUMP, UNDEF_WORD_DISP):
1063 /* A bcond won't fit and it won't go into a 12 bit
1064 displacement either, the code sequence looks like:
1074 fragP->fr_fix + fragP->fr_address + COND32_LENGTH;
1076 int disp = targ_addr - next_inst;
1079 buffer[0] ^= 0x2; /* Toggle T/F bit */
1081 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1082 buffer[2] = 0xd0 | JREG;/* Build mov insn */
1084 buffer[4] = 0x40 | JREG;/* Build jmp @JREG */
1086 buffer[6] = 0x20; /* build nop */
1088 buffer[8] = 0; /* space for 32 bit jump disp */
1094 /* Make reloc for the long disp */
1103 fragP->fr_fix += COND32_LENGTH;
1113 if (donerelax && !relax)
1115 as_bad ("Offset doesn't fit at 0x%x, trying to get to 0x%x",
1123 DEFUN (md_section_align, (seg, size),
1127 return ((size + (1 << section_alignment[(int) seg]) - 1)
1128 & (-1 << section_alignment[(int) seg]));
1133 md_apply_fix (fixP, val)
1137 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1138 int addr = fixP->fx_frag->fr_address + fixP->fx_where;
1139 if (fixP->fx_r_type == 0)
1141 fixP->fx_r_type = R_SH_IMM32;
1144 switch (fixP->fx_r_type)
1148 *buf = (*buf & 0xf0) | (val & 0xf);
1152 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1156 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1171 case R_SH_PCRELIMM8BY4:
1175 as_warn ("non aligned displacement at %x\n", addr);
1180 as_warn ("pcrel too far at %x\n", addr);
1185 case R_SH_PCRELIMM8BY2:
1188 as_bad ("odd displacement at %x\n", addr);
1193 as_warn ("pcrel too far at %x\n", addr);
1210 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1214 int md_long_jump_size;
1217 called just before address relaxation, return the length
1218 by which a fragment must grow to reach it's destination
1221 md_estimate_size_before_relax (fragP, segment_type)
1222 register fragS *fragP;
1223 register segT segment_type;
1225 switch (fragP->fr_subtype)
1227 case C (UNCOND_JUMP, UNDEF_DISP):
1228 /* used to be a branch to somewhere which was unknown */
1229 if (!fragP->fr_symbol)
1231 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1232 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1234 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1236 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1237 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1241 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1242 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1243 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1249 case C (COND_JUMP, UNDEF_DISP):
1250 /* used to be a branch to somewhere which was unknown */
1251 if (fragP->fr_symbol
1252 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1254 /* Got a symbol and it's defined in this segment, become byte
1255 sized - maybe it will fix up */
1256 fragP->fr_subtype = C (COND_JUMP, COND8);
1257 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1259 else if (fragP->fr_symbol)
1261 /* Its got a segment, but its not ours, so it will always be long */
1262 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1263 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1264 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1268 /* We know the abs value */
1269 fragP->fr_subtype = C (COND_JUMP, COND8);
1270 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1275 return fragP->fr_var;
1278 /* Put number into target byte order */
1281 md_number_to_chars (ptr, use, nbytes)
1289 *ptr++ = (use >> 24) & 0xff;
1291 *ptr++ = (use >> 16) & 0xff;
1293 *ptr++ = (use >> 8) & 0xff;
1295 *ptr++ = (use >> 0) & 0xff;
1302 md_pcrel_from (fixP)
1306 int gap = fixP->fx_size + fixP->fx_where +
1307 fixP->fx_frag->fr_address;
1312 tc_coff_symbol_emit_hook ()
1317 tc_coff_fix2rtype (fix_ptr)
1320 return fix_ptr->fx_r_type;
1324 tc_reloc_mangle (fix_ptr, intr, base)
1326 struct internal_reloc *intr;
1330 symbolS *symbol_ptr;
1332 symbol_ptr = fix_ptr->fx_addsy;
1334 /* If this relocation is attached to a symbol then it's ok
1336 if (fix_ptr->fx_r_type == RELOC_32)
1338 /* cons likes to create reloc32's whatever the size of the reloc..
1340 switch (fix_ptr->fx_size)
1343 intr->r_type = R_IMM16;
1346 intr->r_type = R_IMM8;
1354 intr->r_type = fix_ptr->fx_r_type;
1357 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1358 intr->r_offset = fix_ptr->fx_offset;
1360 /* Turn the segment of the symbol into an offset. */
1365 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1368 intr->r_offset += S_GET_VALUE (symbol_ptr);
1369 intr->r_symndx = dot->sy_number;
1373 intr->r_symndx = symbol_ptr->sy_number;
1378 intr->r_symndx = -1;
1383 tc_coff_sizemachdep (frag)
1386 return md_relax_table[frag->fr_subtype].rlx_length;
1390 /* end of tc-sh.c */