2007-06-04 Olivier Hainque <hainque@adacore.com>
[binutils.git] / gas / config / tc-openrisc.c
blobb27e022fecd1d66517c7ab0b93f382217ce01044
1 /* tc-openrisc.c -- Assembler for the OpenRISC family.
2 Copyright 2001, 2002, 2003, 2005, 2006 Free Software Foundation.
3 Contributed by Johan Rydberg, jrydberg@opencores.org
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)
10 any later version.
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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "as.h"
23 #include "subsegs.h"
24 #include "symcat.h"
25 #include "opcodes/openrisc-desc.h"
26 #include "opcodes/openrisc-opc.h"
27 #include "cgen.h"
29 /* Structure to hold all of the different components describing
30 an individual instruction. */
31 typedef struct openrisc_insn openrisc_insn;
33 struct openrisc_insn
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];
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";
60 #define OPENRISC_SHORTOPTS "m:"
61 const char * md_shortopts = OPENRISC_SHORTOPTS;
63 struct option md_longopts[] =
65 {NULL, no_argument, NULL, 0}
67 size_t md_longopts_size = sizeof (md_longopts);
69 unsigned long openrisc_machine = 0; /* default */
71 int
72 md_parse_option (int c ATTRIBUTE_UNUSED, char * arg ATTRIBUTE_UNUSED)
74 return 0;
77 void
78 md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
82 static void
83 ignore_pseudo (int val ATTRIBUTE_UNUSED)
85 discard_rest_of_line ();
88 const char openrisc_comment_chars [] = ";#";
90 /* The target specific pseudo-ops which we support. */
91 const pseudo_typeS md_pseudo_table[] =
93 { "word", cons, 4 },
94 { "proc", ignore_pseudo, 0 },
95 { "endproc", ignore_pseudo, 0 },
96 { NULL, NULL, 0 }
101 void
102 md_begin (void)
104 /* Initialize the `cgen' interface. */
106 /* Set the machine number and endian. */
107 gas_cgen_cpu_desc = openrisc_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
108 CGEN_CPU_OPEN_ENDIAN,
109 CGEN_ENDIAN_BIG,
110 CGEN_CPU_OPEN_END);
111 openrisc_cgen_init_asm (gas_cgen_cpu_desc);
113 /* This is a callback from cgen to gas to parse operands. */
114 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
117 void
118 md_assemble (char * str)
120 static int last_insn_had_delay_slot = 0;
121 openrisc_insn insn;
122 char * errmsg;
124 /* Initialize GAS's cgen interface for a new instruction. */
125 gas_cgen_init_parse ();
127 insn.insn = openrisc_cgen_assemble_insn
128 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
130 if (!insn.insn)
132 as_bad (errmsg);
133 return;
136 /* Doesn't really matter what we pass for RELAX_P here. */
137 gas_cgen_finish_insn (insn.insn, insn.buffer,
138 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
140 last_insn_had_delay_slot
141 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
145 /* The syntax in the manual says constants begin with '#'.
146 We just ignore it. */
148 void
149 md_operand (expressionS * expressionP)
151 if (* input_line_pointer == '#')
153 input_line_pointer ++;
154 expression (expressionP);
158 valueT
159 md_section_align (segT segment, valueT size)
161 int align = bfd_get_section_alignment (stdoutput, segment);
162 return ((size + (1 << align) - 1) & (-1 << align));
165 symbolS *
166 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
168 return 0;
172 /* Interface to relax_segment. */
174 /* FIXME: Look through this. */
176 const relax_typeS md_relax_table[] =
178 /* The fields are:
179 1) most positive reach of this state,
180 2) most negative reach of this state,
181 3) how many bytes this mode will add to the size of the current frag
182 4) which index into the table to try if we can't fit into this one. */
184 /* The first entry must be unused because an `rlx_more' value of zero ends
185 each list. */
186 {1, 1, 0, 0},
188 /* The displacement used by GAS is from the end of the 2 byte insn,
189 so we subtract 2 from the following. */
190 /* 16 bit insn, 8 bit disp -> 10 bit range.
191 This doesn't handle a branch in the right slot at the border:
192 the "& -4" isn't taken into account. It's not important enough to
193 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
194 case). */
195 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
196 /* 32 bit insn, 24 bit disp -> 26 bit range. */
197 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
198 /* Same thing, but with leading nop for alignment. */
199 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
202 /* Return an initial guess of the length by which a fragment must grow to
203 hold a branch to reach its destination.
204 Also updates fr_type/fr_subtype as necessary.
206 Called just before doing relaxation.
207 Any symbol that is now undefined will not become defined.
208 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
209 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
210 Although it may not be explicit in the frag, pretend fr_var starts with a
211 0 value. */
214 md_estimate_size_before_relax (fragS * fragP, segT segment)
216 /* The only thing we have to handle here are symbols outside of the
217 current segment. They may be undefined or in a different segment in
218 which case linker scripts may place them anywhere.
219 However, we can't finish the fragment here and emit the reloc as insn
220 alignment requirements may move the insn about. */
222 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
224 /* The symbol is undefined in this segment.
225 Change the relaxation subtype to the max allowable and leave
226 all further handling to md_convert_frag. */
227 fragP->fr_subtype = 2;
230 const CGEN_INSN * insn;
231 int i;
233 /* Update the recorded insn.
234 Fortunately we don't have to look very far.
235 FIXME: Change this to record in the instruction the next higher
236 relaxable insn to use. */
237 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
239 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
240 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
241 == 0)
242 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
243 break;
245 if (i == 4)
246 abort ();
248 fragP->fr_cgen.insn = insn;
249 return 2;
253 return md_relax_table[fragP->fr_subtype].rlx_length;
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.
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. */
263 void
264 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
265 segT sec ATTRIBUTE_UNUSED,
266 fragS * fragP ATTRIBUTE_UNUSED)
268 /* FIXME */
272 /* Functions concerning relocs. */
274 /* The location from which a PC relative jump should be calculated,
275 given a PC relative reloc. */
277 long
278 md_pcrel_from_section (fixS * fixP, segT sec)
280 if (fixP->fx_addsy != (symbolS *) NULL
281 && (! S_IS_DEFINED (fixP->fx_addsy)
282 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
283 /* The symbol is undefined (or is defined but not in this section).
284 Let the linker figure it out. */
285 return 0;
287 return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
291 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
292 Returns BFD_RELOC_NONE if no reloc type can be found.
293 *FIXP may be modified if desired. */
295 bfd_reloc_code_real_type
296 md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
297 const CGEN_OPERAND * operand,
298 fixS * fixP)
300 bfd_reloc_code_real_type type;
302 switch (operand->type)
304 case OPENRISC_OPERAND_ABS_26:
305 fixP->fx_pcrel = 0;
306 type = BFD_RELOC_OPENRISC_ABS_26;
307 goto emit;
308 case OPENRISC_OPERAND_DISP_26:
309 fixP->fx_pcrel = 1;
310 type = BFD_RELOC_OPENRISC_REL_26;
311 goto emit;
313 case OPENRISC_OPERAND_HI16:
314 type = BFD_RELOC_HI16;
315 goto emit;
317 case OPENRISC_OPERAND_LO16:
318 type = BFD_RELOC_LO16;
319 goto emit;
321 emit:
322 return type;
324 default : /* avoid -Wall warning */
325 break;
328 return BFD_RELOC_NONE;
331 /* Write a value out to the object file, using the appropriate endianness. */
333 void
334 md_number_to_chars (char * buf, valueT val, int n)
336 number_to_chars_bigendian (buf, val, n);
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.
344 /* Equal to MAX_PRECISION in atof-ieee.c */
345 #define MAX_LITTLENUMS 6
347 char *
348 md_atof (int type, char * litP, int * sizeP)
350 int i;
351 int prec;
352 LITTLENUM_TYPE words [MAX_LITTLENUMS];
353 char * t;
355 switch (type)
357 case 'f':
358 case 'F':
359 case 's':
360 case 'S':
361 prec = 2;
362 break;
364 case 'd':
365 case 'D':
366 case 'r':
367 case 'R':
368 prec = 4;
369 break;
371 /* FIXME: Some targets allow other format chars for bigger sizes here. */
373 default:
374 * sizeP = 0;
375 return _("Bad call to md_atof()");
378 t = atof_ieee (input_line_pointer, type, words);
379 if (t)
380 input_line_pointer = t;
381 * sizeP = prec * sizeof (LITTLENUM_TYPE);
383 for (i = 0; i < prec; i++)
385 md_number_to_chars (litP, (valueT) words[i],
386 sizeof (LITTLENUM_TYPE));
387 litP += sizeof (LITTLENUM_TYPE);
390 return 0;
393 bfd_boolean
394 openrisc_fix_adjustable (fixS * fixP)
396 /* We need the symbol name for the VTABLE entries. */
397 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
398 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
399 return 0;
401 return 1;