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