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 = "";
159 opcode_hash_control = hash_new ();
161 /* Insert unique names into hash table */
162 for (opcode = sh_table; opcode->name; opcode++)
164 if (strcmp (prev_name, opcode->name))
166 prev_name = opcode->name;
167 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
171 /* Make all the opcodes with the same name point to the same
173 opcode->name = prev_name;
180 static expressionS immediate; /* absolute expression */
190 /* try and parse a reg name, returns number of chars consumed */
192 parse_reg (src, mode, reg)
201 if (src[2] >= '0' && src[2] <= '5')
204 *reg = 10 + src[2] - '0';
208 if (src[1] >= '0' && src[1] <= '9')
211 *reg = (src[1] - '0');
216 if (src[0] == 's' && src[1] == 'r')
222 if (src[0] == 's' && src[1] == 'p')
229 if (src[0] == 'p' && src[1] == 'r')
234 if (src[0] == 'p' && src[1] == 'c')
239 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r')
244 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r')
250 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c')
267 static symbolS *dot()
271 /* JF: '.' is pseudo symbol with value of current location
272 in current segment. */
273 fake = FAKE_LABEL_NAME;
274 return symbol_new (fake,
276 (valueT) frag_now_fix (),
290 save = input_line_pointer;
291 input_line_pointer = s;
292 expression (&immediate);
293 if (immediate.X_op == O_absent)
294 as_bad ("missing operand");
295 new = input_line_pointer;
296 input_line_pointer = save;
301 /* The many forms of operand:
304 @Rn Register indirect
317 pr, gbr, vbr, macl, mach
332 /* Must be predecrement */
335 len = parse_reg (src, &mode, &(op->reg));
337 as_bad ("illegal register after @-");
342 else if (src[0] == '(')
344 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
347 len = parse_reg (src, &mode, &(op->reg));
348 if (len && mode == A_REG_N)
353 as_bad ("must be @(r0,...)");
357 /* Now can be rn or gbr */
358 len = parse_reg (src, &mode, &(op->reg));
363 else if (mode == A_REG_N)
365 op->type = A_IND_R0_REG_N;
369 as_bad ("syntax error in @(r0,...)");
374 /* Must be an @(disp,.. thing) */
375 src = parse_exp (src);
378 /* Now can be rn, gbr or pc */
379 len = parse_reg (src, &mode, &op->reg);
384 op->type = A_DISP_REG_N;
386 else if (mode == A_GBR)
388 op->type = A_DISP_GBR;
390 else if (mode == A_DISP_PC)
392 /* Turn a plain @(4,pc) into @(.+4,pc) */
393 if (immediate.X_op == O_constant) {
394 immediate.X_add_symbol = dot();
395 immediate.X_op = O_symbol;
397 op->type = A_DISP_PC;
401 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
406 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
411 as_bad ("expecting )");
417 src += parse_reg (src, &mode, &(op->reg));
420 as_bad ("illegal register after @");
436 get_operand (ptr, op)
447 *ptr = parse_exp (src);
452 else if (src[0] == '@')
454 *ptr = parse_at (src, op);
457 len = parse_reg (src, &mode, &(op->reg));
466 /* Not a reg, the only thing left is a displacement */
467 *ptr = parse_exp (src);
468 op->type = A_DISP_PC;
475 get_operands (info, args, operand)
476 sh_opcode_info *info;
478 sh_operand_info *operand;
486 get_operand (&ptr, operand + 0);
493 get_operand (&ptr, operand + 1);
508 /* Passed a pointer to a list of opcodes which use different
509 addressing modes, return the opcode which matches the opcodes
515 get_specific (opcode, operands)
516 sh_opcode_info *opcode;
517 sh_operand_info *operands;
519 sh_opcode_info *this_try = opcode;
520 char *name = opcode->name;
526 if (this_try->name != name)
528 /* We've looked so far down the table that we've run out of
529 opcodes with the same name */
532 /* look at both operands needed by the opcodes and provided by
533 the user - since an arg test will often fail on the same arg
534 again and again, we'll try and test the last failing arg the
535 first on each opcode try */
537 for (n = 0; this_try->arg[n]; n++)
539 sh_operand_info *user = operands + arg_to_test;
540 sh_arg_type arg = this_try->arg[arg_to_test];
551 if (user->type != arg)
555 /* opcode needs r0 */
556 if (user->type != A_REG_N || user->reg != 0)
560 if (user->type != A_R0_GBR || user->reg != 0)
570 /* Opcode needs rn */
571 if (user->type != arg)
578 if (user->type != arg)
588 /* Opcode needs rn */
589 if (user->type != arg - A_REG_M + A_REG_N)
594 printf ("unhandled %d\n", arg);
597 /* If we did 0, test 1 next, else 0 */
598 arg_to_test = 1 - arg_to_test;
608 check (operand, low, high)
609 expressionS *operand;
613 if (operand->X_op != O_constant
614 || operand->X_add_number < low
615 || operand->X_add_number > high)
617 as_bad ("operand must be absolute in range %d..%d", low, high);
619 return operand->X_add_number;
624 insert (where, how, pcrel)
629 fix_new_exp (frag_now,
630 where - frag_now->fr_literal,
640 sh_opcode_info *opcode;
643 int high_byte = shl ? 1 : 0 ;
646 if (opcode->arg[0] == A_BDISP8)
648 p = frag_var (rs_machine_dependent,
649 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
650 len = md_relax_table[C (COND_JUMP, COND8)].rlx_length,
652 immediate.X_add_symbol,
653 immediate.X_add_number,
655 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
657 else if (opcode->arg[0] == A_BDISP12)
659 p = frag_var (rs_machine_dependent,
660 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
661 len = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
663 immediate.X_add_symbol,
664 immediate.X_add_number,
666 p[high_byte] = (opcode->nibbles[0] << 4);
671 /* Now we know what sort of opcodes it is, lets build the bytes -
674 build_Mytes (opcode, operand)
675 sh_opcode_info *opcode;
676 sh_operand_info *operand;
681 char *output = frag_more (2);
682 int low_byte = shl ? 0 : 1;
688 for (index = 0; index < 4; index++)
690 sh_nibble_type i = opcode->nibbles[index];
706 insert (output + low_byte, R_SH_IMM4, 0);
709 insert (output + low_byte, R_SH_IMM4BY4, 0);
712 insert (output + low_byte, R_SH_IMM4BY2, 0);
715 insert (output + low_byte, R_SH_IMM4, 0);
718 insert (output + low_byte, R_SH_IMM8BY4, 0);
721 insert (output + low_byte, R_SH_IMM8BY2, 0);
724 insert (output + low_byte, R_SH_IMM8, 0);
727 insert (output, R_SH_PCRELIMM8BY4, 1);
730 insert (output, R_SH_PCRELIMM8BY2, 1);
733 printf ("failed for %d\n", i);
738 output[1] = (nbuf[0] << 4) | (nbuf[1]);
739 output[0] = (nbuf[2] << 4) | (nbuf[3]);
742 output[0] = (nbuf[0] << 4) | (nbuf[1]);
743 output[1] = (nbuf[2] << 4) | (nbuf[3]);
747 /* This is the guts of the machine-dependent assembler. STR points to a
748 machine dependent instruction. This function is supposed to emit
749 the frags/bytes it assembles to.
756 unsigned char *op_start;
757 unsigned char *op_end;
758 sh_operand_info operand[2];
759 sh_opcode_info *opcode;
762 /* Drop leading whitespace */
766 /* find the op code end */
767 for (op_start = op_end = (unsigned char *) (str);
770 && !is_end_of_line[*op_end] && *op_end != ' ';
773 name[nlen] = op_start[nlen];
780 as_bad ("can't find opcode ");
783 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
787 as_bad ("unknown opcode");
791 if (opcode->arg[0] == A_BDISP12
792 || opcode->arg[0] == A_BDISP8)
794 parse_exp (op_end + 1);
795 build_relax (opcode);
799 if (opcode->arg[0] != A_END)
801 get_operands (opcode, op_end, operand);
803 opcode = get_specific (opcode, operand);
807 /* Couldn't find an opcode which matched the operands */
808 char *where = frag_more (2);
812 as_bad ("invalid operands for opcode");
816 build_Mytes (opcode, operand);
822 DEFUN (tc_crawl_symbol_chain, (headers),
823 object_headers * headers)
825 printf ("call to tc_crawl_symbol_chain \n");
829 DEFUN (md_undefined_symbol, (name),
836 DEFUN (tc_headers_hook, (headers),
837 object_headers * headers)
839 printf ("call to tc_headers_hook \n");
842 /* Various routines to kill one day */
843 /* Equal to MAX_PRECISION in atof-ieee.c */
844 #define MAX_LITTLENUMS 6
846 /* Turn a string in input_line_pointer into a floating point constant of type
847 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
848 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
851 md_atof (type, litP, sizeP)
857 LITTLENUM_TYPE words[4];
873 return "bad call to md_atof";
876 t = atof_ieee (input_line_pointer, type, words);
878 input_line_pointer = t;
884 for (i = prec - 1; i >= 0; i--)
886 md_number_to_chars (litP, (valueT) words[i], 2);
892 for (i = 0; i < prec; i++)
894 md_number_to_chars (litP, (valueT) words[i], 2);
904 CONST char *md_shortopts = "";
905 struct option md_longopts[] = {
907 #define OPTION_RELAX (OPTION_MD_BASE)
908 #define OPTION_LITTLE (OPTION_MD_BASE+1)
910 {"relax", no_argument, NULL, OPTION_RELAX},
911 {"little", no_argument, NULL, OPTION_LITTLE},
912 {NULL, no_argument, NULL, 0}
914 size_t md_longopts_size = sizeof(md_longopts);
917 md_parse_option (c, arg)
938 md_show_usage (stream)
943 -relax alter jump instructions for long displacements\n");
946 int md_short_jump_size;
949 tc_Nout_fix_to_chars ()
951 printf ("call to tc_Nout_fix_to_chars \n");
956 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
963 as_fatal ("failed sanity check.");
967 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
969 addressT from_Nddr, to_Nddr;
973 as_fatal ("failed sanity check.");
977 called after relaxing, change the frags so they know how big they are
980 md_convert_frag (headers, fragP)
981 object_headers *headers;
985 unsigned char *buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
987 int highbyte = shl ? 1 : 0;
988 int lowbyte = shl ? 0 : 1;
989 int targ_addr = ((fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0) + fragP->fr_offset);
990 switch (fragP->fr_subtype)
992 case C (COND_JUMP, COND8):
994 /* Get the address of the end of the instruction */
995 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
997 int disp = targ_addr - next_inst - 2;
1000 md_number_to_chars (buffer + lowbyte, disp & 0xff, 1);
1006 case C (UNCOND_JUMP, UNCOND12):
1008 /* Get the address of the end of the instruction */
1009 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1012 int disp = targ_addr - next_inst - 2;
1015 t = buffer[highbyte] & 0xf0;
1016 md_number_to_chars (buffer, disp & 0xfff, 2);
1017 buffer[highbyte] = (buffer[highbyte] & 0xf) | t;
1023 case C (UNCOND_JUMP, UNCOND32):
1024 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1026 /* A jump wont fit in 12 bits, make code which looks like
1034 fragP->fr_fix + fragP->fr_address + UNCOND32_LENGTH;
1036 int disp = targ_addr - next_inst;
1037 int t = buffer[0] & 0x10;
1041 buffer[highbyte] = 0xa0; /* branch over move and disp */
1042 buffer[lowbyte] = 3;
1043 buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1044 buffer[lowbyte+2] = 0x00;
1046 buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
1047 buffer[lowbyte+4] = 0;
1048 buffer[highbyte+6] = 0;
1049 buffer[lowbyte+6] = 0;
1051 buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1052 buffer[lowbyte+8] = t ? 0xb : 0x2b;
1054 buffer[highbyte+10] = 0x20; /* build nop */
1055 buffer[lowbyte+10] = 0x0b;
1057 /* Make reloc for the long disp */
1065 fragP->fr_fix += UNCOND32_LENGTH;
1072 case C (COND_JUMP, COND12):
1074 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1076 fragP->fr_fix + fragP->fr_address + 6;
1078 int disp = targ_addr - next_inst;
1080 md_number_to_chars (buffer + 2, disp & 0xfff, 2);
1081 buffer[highbyte] ^= 0x2; /* Toggle T/F bit */
1082 buffer[lowbyte] = 1; /* branch over jump and nop */
1083 buffer[highbyte+2] = (buffer[highbyte+2] & 0xf) | 0xa0; /* Build jump insn */
1084 buffer[lowbyte+5] = 0x20; /* Build nop */
1085 buffer[lowbyte+5] = 0x0b;
1092 case C (COND_JUMP, COND32):
1093 case C (COND_JUMP, UNDEF_WORD_DISP):
1095 /* A bcond won't fit and it won't go into a 12 bit
1096 displacement either, the code sequence looks like:
1106 fragP->fr_fix + fragP->fr_address + COND32_LENGTH;
1108 int disp = targ_addr - next_inst;
1111 buffer[0] ^= 0x2; /* Toggle T/F bit */
1113 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1114 buffer[2] = 0xd0 | JREG; /* Build mov insn */
1116 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
1118 buffer[6] = 0x20; /* build nop */
1120 buffer[8] = 0; /* space for 32 bit jump disp */
1126 /* Make reloc for the long disp */
1134 fragP->fr_fix += COND32_LENGTH;
1144 if (donerelax && !relax)
1146 as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%x",
1147 (unsigned long) fragP->fr_address,
1148 fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1155 DEFUN (md_section_align, (seg, size),
1159 return ((size + (1 << section_alignment[(int) seg]) - 1)
1160 & (-1 << section_alignment[(int) seg]));
1165 md_apply_fix (fixP, val)
1169 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1170 int addr = fixP->fx_frag->fr_address + fixP->fx_where;
1171 int lowbyte = shl ? 0 : 1;
1172 if (fixP->fx_r_type == 0)
1174 if (fixP->fx_size == 2)
1175 fixP->fx_r_type = R_SH_IMM16;
1177 fixP->fx_r_type = R_SH_IMM32;
1180 switch (fixP->fx_r_type)
1184 *buf = (*buf & 0xf0) | (val & 0xf);
1188 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1192 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1207 case R_SH_PCRELIMM8BY4:
1211 as_warn ("non aligned displacement at %x\n", addr);
1213 /* val -= (addr + 4); */
1220 as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1224 case R_SH_PCRELIMM8BY2:
1226 /* if ((val & 0x1) != shl)
1227 as_bad ("odd displacement at %x\n", addr);*/
1228 /* val -= (addr + 4); */
1233 as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1273 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1277 int md_long_jump_size;
1280 called just before address relaxation, return the length
1281 by which a fragment must grow to reach it's destination
1284 md_estimate_size_before_relax (fragP, segment_type)
1285 register fragS *fragP;
1286 register segT segment_type;
1288 switch (fragP->fr_subtype)
1290 case C (UNCOND_JUMP, UNDEF_DISP):
1291 /* used to be a branch to somewhere which was unknown */
1292 if (!fragP->fr_symbol)
1294 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1295 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1297 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1299 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1300 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1304 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1305 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1306 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1312 case C (COND_JUMP, UNDEF_DISP):
1313 /* used to be a branch to somewhere which was unknown */
1314 if (fragP->fr_symbol
1315 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1317 /* Got a symbol and it's defined in this segment, become byte
1318 sized - maybe it will fix up */
1319 fragP->fr_subtype = C (COND_JUMP, COND8);
1320 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1322 else if (fragP->fr_symbol)
1324 /* Its got a segment, but its not ours, so it will always be long */
1325 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1326 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1327 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1331 /* We know the abs value */
1332 fragP->fr_subtype = C (COND_JUMP, COND8);
1333 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1338 return fragP->fr_var;
1341 /* Put number into target byte order */
1344 md_number_to_chars (ptr, use, nbytes)
1350 number_to_chars_littleendian (ptr, use, nbytes);
1352 number_to_chars_bigendian (ptr, use, nbytes);
1356 md_pcrel_from (fixP)
1359 int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1 ;
1364 tc_coff_fix2rtype (fix_ptr)
1367 if (fix_ptr->fx_r_type == RELOC_32)
1369 /* cons likes to create reloc32's whatever the size of the reloc..
1371 switch (fix_ptr->fx_size)
1387 tc_coff_sizemachdep (frag)
1390 return md_relax_table[frag->fr_subtype].rlx_length;