1 /* tc-fr30.c -- Assembler for the Fujitsu FR30. 2 Copyright (C) 1998-2014 Free Software Foundation, Inc. 3 4 This file is part of GAS, the GNU Assembler. 5 6 GAS is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 GAS is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GAS; see the file COPYING. If not, write to 18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, 19 Boston, MA 02110-1301, USA. */ 20 21 #include "as.h" 22 #include "safe-ctype.h" 23 #include "subsegs.h" 24 #include "symcat.h" 25 #include "opcodes/fr30-desc.h" 26 #include "opcodes/fr30-opc.h" 27 #include "cgen.h" 28 29 /* Structure to hold all of the different components describing 30 an individual instruction. */ 31 typedef struct 32 { 33 const CGEN_INSN * insn; 34 const CGEN_INSN * orig_insn; 35 CGEN_FIELDS fields; 36 #if CGEN_INT_INSN_P 37 CGEN_INSN_INT buffer [1]; 38 #define INSN_VALUE(buf) (*(buf)) 39 #else 40 unsigned char buffer [CGEN_MAX_INSN_SIZE]; 41 #define INSN_VALUE(buf) (buf) 42 #endif 43 char * addr; 44 fragS * frag; 45 int num_fixups; 46 fixS * fixups [GAS_CGEN_MAX_FIXUPS]; 47 int indices [MAX_OPERAND_INSTANCES]; 48 } 49 fr30_insn; 50 51 const char comment_chars[] = ";"; 52 const char line_comment_chars[] = "#"; 53 const char line_separator_chars[] = "|"; 54 const char EXP_CHARS[] = "eE"; 55 const char FLT_CHARS[] = "dD"; 56 57 #define FR30_SHORTOPTS "" 59 const char * md_shortopts = FR30_SHORTOPTS; 60 61 struct option md_longopts[] = 62 { 63 {NULL, no_argument, NULL, 0} 64 }; 65 size_t md_longopts_size = sizeof (md_longopts); 66 67 int 68 md_parse_option (int c ATTRIBUTE_UNUSED, 69 char *arg ATTRIBUTE_UNUSED) 70 { 71 switch (c) 72 { 73 default: 74 return 0; 75 } 76 return 1; 77 } 78 79 void 80 md_show_usage (FILE * stream) 81 { 82 fprintf (stream, _(" FR30 specific command line options:\n")); 83 } 84 85 /* The target specific pseudo-ops which we support. */ 86 const pseudo_typeS md_pseudo_table[] = 87 { 88 { "word", cons, 4 }, 89 { NULL, NULL, 0 } 90 }; 91 92 93 void 95 md_begin (void) 96 { 97 /* Initialize the `cgen' interface. */ 98 99 /* Set the machine number and endian. */ 100 gas_cgen_cpu_desc = fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0, 101 CGEN_CPU_OPEN_ENDIAN, 102 CGEN_ENDIAN_BIG, 103 CGEN_CPU_OPEN_END); 104 fr30_cgen_init_asm (gas_cgen_cpu_desc); 105 106 /* This is a callback from cgen to gas to parse operands. */ 107 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand); 108 } 109 110 void 111 md_assemble (char *str) 112 { 113 static int last_insn_had_delay_slot = 0; 114 fr30_insn insn; 115 char *errmsg; 116 117 /* Initialize GAS's cgen interface for a new instruction. */ 118 gas_cgen_init_parse (); 119 120 insn.insn = fr30_cgen_assemble_insn 121 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg); 122 123 if (!insn.insn) 124 { 125 as_bad ("%s", errmsg); 126 return; 127 } 128 129 /* Doesn't really matter what we pass for RELAX_P here. */ 130 gas_cgen_finish_insn (insn.insn, insn.buffer, 131 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL); 132 133 /* Warn about invalid insns in delay slots. */ 134 if (last_insn_had_delay_slot 135 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT)) 136 as_warn (_("Instruction %s not allowed in a delay slot."), 137 CGEN_INSN_NAME (insn.insn)); 138 139 last_insn_had_delay_slot 140 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT); 141 } 142 143 /* The syntax in the manual says constants begin with '#'. 144 We just ignore it. */ 145 146 void 147 md_operand (expressionS * expressionP) 148 { 149 if (* input_line_pointer == '#') 150 { 151 input_line_pointer ++; 152 expression (expressionP); 153 } 154 } 155 156 valueT 157 md_section_align (segT segment, valueT size) 158 { 159 int align = bfd_get_section_alignment (stdoutput, segment); 160 161 return ((size + (1 << align) - 1) & (-1 << align)); 162 } 163 164 symbolS * 165 md_undefined_symbol (char *name ATTRIBUTE_UNUSED) 166 { 167 return NULL; 168 } 169 170 /* Interface to relax_segment. */ 172 173 /* FIXME: Build table by hand, get it working, then machine generate. */ 174 175 const relax_typeS md_relax_table[] = 176 { 177 /* The fields are: 178 1) most positive reach of this state, 179 2) most negative reach of this state, 180 3) how many bytes this mode will add to the size of the current frag 181 4) which index into the table to try if we can't fit into this one. */ 182 183 /* The first entry must be unused because an `rlx_more' value of zero ends 184 each list. */ 185 {1, 1, 0, 0}, 186 187 /* The displacement used by GAS is from the end of the 2 byte insn, 188 so we subtract 2 from the following. */ 189 /* 16 bit insn, 8 bit disp -> 10 bit range. 190 This doesn't handle a branch in the right slot at the border: 191 the "& -4" isn't taken into account. It's not important enough to 192 complicate things over it, so we subtract an extra 2 (or + 2 in -ve 193 case). */ 194 {511 - 2 - 2, -512 - 2 + 2, 0, 2 }, 195 /* 32 bit insn, 24 bit disp -> 26 bit range. */ 196 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 }, 197 /* Same thing, but with leading nop for alignment. */ 198 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 } 199 }; 200 201 /* Return an initial guess of the length by which a fragment must grow to 202 hold a branch to reach its destination. 203 Also updates fr_type/fr_subtype as necessary. 204 205 Called just before doing relaxation. 206 Any symbol that is now undefined will not become defined. 207 The guess for fr_var is ACTUALLY the growth beyond fr_fix. 208 Whatever we do to grow fr_fix or fr_var contributes to our returned value. 209 Although it may not be explicit in the frag, pretend fr_var starts with a 210 0 value. */ 211 212 int 213 md_estimate_size_before_relax (fragS * fragP, segT segment) 214 { 215 /* The only thing we have to handle here are symbols outside of the 216 current segment. They may be undefined or in a different segment in 217 which case linker scripts may place them anywhere. 218 However, we can't finish the fragment here and emit the reloc as insn 219 alignment requirements may move the insn about. */ 220 221 if (S_GET_SEGMENT (fragP->fr_symbol) != segment) 222 { 223 /* The symbol is undefined in this segment. 224 Change the relaxation subtype to the max allowable and leave 225 all further handling to md_convert_frag. */ 226 fragP->fr_subtype = 2; 227 228 { 229 const CGEN_INSN * insn; 230 int i; 231 232 /* Update the recorded insn. 233 Fortunately we don't have to look very far. 234 FIXME: Change this to record in the instruction the next higher 235 relaxable insn to use. */ 236 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++) 237 { 238 if ((strcmp (CGEN_INSN_MNEMONIC (insn), 239 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn)) 240 == 0) 241 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED)) 242 break; 243 } 244 if (i == 4) 245 abort (); 246 247 fragP->fr_cgen.insn = insn; 248 return 2; 249 } 250 } 251 252 /* Return the size of the variable part of the frag. */ 253 return md_relax_table[fragP->fr_subtype].rlx_length; 254 } 255 256 /* *fragP has been relaxed to its final size, and now needs to have 257 the bytes inside it modified to conform to the new size. 258 259 Called after relaxation is finished. 260 fragP->fr_type == rs_machine_dependent. 261 fragP->fr_subtype is the subtype of what the address relaxed to. */ 262 263 void 264 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, 265 segT sec ATTRIBUTE_UNUSED, 266 fragS *fragP ATTRIBUTE_UNUSED) 267 { 268 } 269 270 /* Functions concerning relocs. */ 272 273 /* The location from which a PC relative jump should be calculated, 274 given a PC relative reloc. */ 275 276 long 277 md_pcrel_from_section (fixS * fixP, segT sec) 278 { 279 if (fixP->fx_addsy != (symbolS *) NULL 280 && (! S_IS_DEFINED (fixP->fx_addsy) 281 || S_GET_SEGMENT (fixP->fx_addsy) != sec)) 282 /* The symbol is undefined (or is defined but not in this section). 283 Let the linker figure it out. */ 284 return 0; 285 286 return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1; 287 } 288 289 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP. 290 Returns BFD_RELOC_NONE if no reloc type can be found. 291 *FIXP may be modified if desired. */ 292 293 bfd_reloc_code_real_type 294 md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED, 295 const CGEN_OPERAND *operand, 296 fixS *fixP) 297 { 298 switch (operand->type) 299 { 300 case FR30_OPERAND_LABEL9: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL; 301 case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL; 302 case FR30_OPERAND_DISP10: return BFD_RELOC_FR30_10_IN_8; 303 case FR30_OPERAND_DISP9: return BFD_RELOC_FR30_9_IN_8; 304 case FR30_OPERAND_DISP8: return BFD_RELOC_FR30_8_IN_8; 305 case FR30_OPERAND_UDISP6: return BFD_RELOC_FR30_6_IN_4; 306 case FR30_OPERAND_I8: return BFD_RELOC_8; 307 case FR30_OPERAND_I32: return BFD_RELOC_FR30_48; 308 case FR30_OPERAND_I20: return BFD_RELOC_FR30_20; 309 default : /* Avoid -Wall warning. */ 310 break; 311 } 312 313 return BFD_RELOC_NONE; 314 } 315 316 /* Write a value out to the object file, using the appropriate endianness. */ 318 319 void 320 md_number_to_chars (char * buf, valueT val, int n) 321 { 322 number_to_chars_bigendian (buf, val, n); 323 } 324 325 char * 326 md_atof (int type, char * litP, int * sizeP) 327 { 328 return ieee_md_atof (type, litP, sizeP, TRUE); 329 } 330 331 /* Worker function for fr30_is_colon_insn(). */ 332 static char 333 restore_colon (int advance_i_l_p_by) 334 { 335 char c; 336 337 /* Restore the colon, and advance input_line_pointer to 338 the end of the new symbol. */ 339 * input_line_pointer = ':'; 340 input_line_pointer += advance_i_l_p_by; 341 c = * input_line_pointer; 342 * input_line_pointer = 0; 343 344 return c; 345 } 346 347 /* Determines if the symbol starting at START and ending in 348 a colon that was at the location pointed to by INPUT_LINE_POINTER 349 (but which has now been replaced bu a NUL) is in fact an 350 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction. 351 If it is, then it restores the colon, advances INPUT_LINE_POINTER 352 to the real end of the instruction/symbol, and returns the character 353 that really terminated the symbol. Otherwise it returns 0. */ 354 char 355 fr30_is_colon_insn (char * start) 356 { 357 char * i_l_p = input_line_pointer; 358 359 /* Check to see if the symbol parsed so far is 'ldi'. */ 360 if ( (start[0] != 'l' && start[0] != 'L') 361 || (start[1] != 'd' && start[1] != 'D') 362 || (start[2] != 'i' && start[2] != 'I') 363 || start[3] != 0) 364 { 365 /* Nope - check to see a 'd' follows the colon. */ 366 if ( (i_l_p[1] == 'd' || i_l_p[1] == 'D') 367 && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n')) 368 { 369 /* Yup - it might be delay slot instruction. */ 370 int i; 371 static char * delay_insns [] = 372 { 373 "call", "jmp", "ret", "bra", "bno", 374 "beq", "bne", "bc", "bnc", "bn", 375 "bp", "bv", "bnv", "blt", "bge", 376 "ble", "bgt", "bls", "bhi" 377 }; 378 379 for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;) 380 { 381 char * insn = delay_insns[i]; 382 int len = strlen (insn); 383 384 if (start [len] != 0) 385 continue; 386 387 while (len --) 388 if (TOLOWER (start [len]) != insn [len]) 389 break; 390 391 if (len == -1) 392 return restore_colon (1); 393 } 394 } 395 396 /* Nope - it is a normal label. */ 397 return 0; 398 } 399 400 /* Check to see if the text following the colon is '8'. */ 401 if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t')) 402 return restore_colon (2); 403 404 /* Check to see if the text following the colon is '20'. */ 405 else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t')) 406 return restore_colon (3); 407 408 /* Check to see if the text following the colon is '32'. */ 409 else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t')) 410 return restore_colon (3); 411 412 return 0; 413 } 414 415 bfd_boolean 416 fr30_fix_adjustable (fixS * fixP) 417 { 418 /* We need the symbol name for the VTABLE entries. */ 419 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT 420 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 421 return 0; 422 423 return 1; 424 } 425