* Added hooks to new dejagnu sky tests, removed old test from run.
[binutils-gdb.git] / gas / cgen.c
blob3e1885ec12658a01dad356d8497e270ceedd6744
1 /* GAS interface for targets using CGEN: Cpu tools GENerator.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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 2, or (at your option)
9 any later version.
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.
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 the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <setjmp.h>
21 #include "ansidecl.h"
22 #include "bfd.h"
23 #include "symcat.h"
24 #include "cgen-opc.h"
25 #include "as.h"
26 #include "subsegs.h"
28 /* Callback to insert a register into the symbol table.
29 A target may choose to let GAS parse the registers.
30 ??? Not currently used. */
32 void
33 cgen_asm_record_register (name, number)
34 char * name;
35 int number;
37 /* Use symbol_create here instead of symbol_new so we don't try to
38 output registers into the object file's symbol table. */
39 symbol_table_insert (symbol_create (name, reg_section,
40 number, & zero_address_frag));
43 /* We need to keep a list of fixups. We can't simply generate them as
44 we go, because that would require us to first create the frag, and
45 that would screw up references to ``.''.
47 This is used by cpu's with simple operands. It keeps knowledge of what
48 an `expressionS' is and what a `fixup' is out of CGEN which for the time
49 being is preferable.
51 OPINDEX is the index in the operand table.
52 OPINFO is something the caller chooses to help in reloc determination. */
54 struct fixup
56 int opindex;
57 int opinfo;
58 expressionS exp;
61 #define MAX_FIXUPS 5
63 static struct fixup fixups [MAX_FIXUPS];
64 static int num_fixups;
66 /* Prepare to parse an instruction.
67 ??? May wish to make this static and delete calls in md_assemble. */
69 void
70 cgen_asm_init_parse ()
72 num_fixups = 0;
75 /* Queue a fixup. */
77 static void
78 cgen_queue_fixup (opindex, opinfo, expP)
79 int opindex;
80 expressionS * expP;
82 /* We need to generate a fixup for this expression. */
83 if (num_fixups >= MAX_FIXUPS)
84 as_fatal (_("too many fixups"));
85 fixups[num_fixups].exp = * expP;
86 fixups[num_fixups].opindex = opindex;
87 fixups[num_fixups].opinfo = opinfo;
88 ++ num_fixups;
91 /* The following three functions allow a backup of the fixup chain to be made,
92 and to have this backup be swapped with the current chain. This allows
93 certain ports, eg the m32r, to swap two instructions and swap their fixups
94 at the same time. */
95 static struct fixup saved_fixups [MAX_FIXUPS];
96 static int saved_num_fixups;
98 void
99 cgen_save_fixups ()
101 saved_num_fixups = num_fixups;
103 memcpy (saved_fixups, fixups, sizeof (fixups[0]) * num_fixups);
105 num_fixups = 0;
108 void
109 cgen_restore_fixups ()
111 num_fixups = saved_num_fixups;
113 memcpy (fixups, saved_fixups, sizeof (fixups[0]) * num_fixups);
115 saved_num_fixups = 0;
118 void
119 cgen_swap_fixups ()
121 int tmp;
122 struct fixup tmp_fixup;
124 if (num_fixups == 0)
126 cgen_restore_fixups ();
128 else if (saved_num_fixups == 0)
130 cgen_save_fixups ();
132 else
134 tmp = saved_num_fixups;
135 saved_num_fixups = num_fixups;
136 num_fixups = tmp;
138 for (tmp = MAX_FIXUPS; tmp--;)
140 tmp_fixup = saved_fixups [tmp];
141 saved_fixups [tmp] = fixups [tmp];
142 fixups [tmp] = tmp_fixup;
147 /* Default routine to record a fixup.
148 This is a cover function to fix_new.
149 It exists because we record INSN with the fixup.
151 FRAG and WHERE are their respective arguments to fix_new_exp.
152 LENGTH is in bits.
153 OPINFO is something the caller chooses to help in reloc determination.
155 At this point we do not use a bfd_reloc_code_real_type for
156 operands residing in the insn, but instead just use the
157 operand index. This lets us easily handle fixups for any
158 operand type. We pick a BFD reloc type in md_apply_fix. */
160 fixS *
161 cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
162 fragS * frag;
163 int where;
164 const CGEN_INSN * insn;
165 int length;
166 const CGEN_OPERAND * operand;
167 int opinfo;
168 symbolS * symbol;
169 offsetT offset;
171 fixS * fixP;
173 /* It may seem strange to use operand->attrs and not insn->attrs here,
174 but it is the operand that has a pc relative relocation. */
176 fixP = fix_new (frag, where, length / 8, symbol, offset,
177 CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
178 (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
179 fixP->tc_fix_data.insn = (PTR) insn;
180 fixP->tc_fix_data.opinfo = opinfo;
182 return fixP;
185 /* Default routine to record a fixup given an expression.
186 This is a cover function to fix_new_exp.
187 It exists because we record INSN with the fixup.
189 FRAG and WHERE are their respective arguments to fix_new_exp.
190 LENGTH is in bits.
191 OPINFO is something the caller chooses to help in reloc determination.
193 At this point we do not use a bfd_reloc_code_real_type for
194 operands residing in the insn, but instead just use the
195 operand index. This lets us easily handle fixups for any
196 operand type. We pick a BFD reloc type in md_apply_fix. */
198 fixS *
199 cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
200 fragS * frag;
201 int where;
202 const CGEN_INSN * insn;
203 int length;
204 const CGEN_OPERAND * operand;
205 int opinfo;
206 expressionS * exp;
208 fixS * fixP;
210 /* It may seem strange to use operand->attrs and not insn->attrs here,
211 but it is the operand that has a pc relative relocation. */
213 fixP = fix_new_exp (frag, where, length / 8, exp,
214 CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
215 (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
216 fixP->tc_fix_data.insn = (PTR) insn;
217 fixP->tc_fix_data.opinfo = opinfo;
219 return fixP;
222 /* Used for communication between the next two procedures. */
223 static jmp_buf expr_jmp_buf;
225 /* Callback for cgen interface. Parse the expression at *STRP.
226 The result is an error message or NULL for success (in which case
227 *STRP is advanced past the parsed text).
228 WANT is an indication of what the caller is looking for.
229 If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
230 a table entry with the insn, reset the queued fixups counter.
231 An enum cgen_parse_operand_result is stored in RESULTP.
232 OPINDEX is the operand's table entry index.
233 OPINFO is something the caller chooses to help in reloc determination.
234 The resulting value is stored in VALUEP. */
236 const char *
237 cgen_parse_operand (want, strP, opindex, opinfo, resultP, valueP)
238 enum cgen_parse_operand_type want;
239 const char ** strP;
240 int opindex;
241 int opinfo;
242 enum cgen_parse_operand_result * resultP;
243 bfd_vma * valueP;
245 #ifdef __STDC__
246 /* These is volatile to survive the setjmp. */
247 char * volatile hold;
248 enum cgen_parse_operand_result * volatile resultP_1;
249 #else
250 static char * hold;
251 static enum cgen_parse_operand_result * resultP_1;
252 #endif
253 const char * errmsg = NULL;
254 expressionS exp;
256 if (want == CGEN_PARSE_OPERAND_INIT)
258 cgen_asm_init_parse ();
259 return NULL;
262 resultP_1 = resultP;
263 hold = input_line_pointer;
264 input_line_pointer = (char *) * strP;
266 /* We rely on md_operand to longjmp back to us.
267 This is done via cgen_md_operand. */
268 if (setjmp (expr_jmp_buf) != 0)
270 input_line_pointer = (char *) hold;
271 * resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
272 return "illegal operand";
275 expression (& exp);
277 * strP = input_line_pointer;
278 input_line_pointer = hold;
280 /* FIXME: Need to check `want'. */
282 switch (exp.X_op)
284 case O_illegal :
285 errmsg = _("illegal operand");
286 * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
287 break;
288 case O_absent :
289 errmsg = _("missing operand");
290 * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
291 break;
292 case O_constant :
293 * valueP = exp.X_add_number;
294 * resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
295 break;
296 case O_register :
297 * valueP = exp.X_add_number;
298 * resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
299 break;
300 default :
301 cgen_queue_fixup (opindex, opinfo, & exp);
302 * valueP = 0;
303 * resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
304 break;
307 return errmsg;
310 /* md_operand handler to catch unrecognized expressions and halt the
311 parsing process so the next entry can be tried.
313 ??? This could be done differently by adding code to `expression'. */
315 void
316 cgen_md_operand (expressionP)
317 expressionS * expressionP;
319 longjmp (expr_jmp_buf, 1);
322 /* Finish assembling instruction INSN.
323 BUF contains what we've built up so far.
324 LENGTH is the size of the insn in bits.
325 Returns the address of the buffer containing the assembled instruction,
326 in case the caller needs to modify it for some reason. */
328 char *
329 cgen_asm_finish_insn (insn, buf, length)
330 const CGEN_INSN * insn;
331 cgen_insn_t * buf;
332 unsigned int length;
334 int i;
335 int relax_operand;
336 char * f;
337 unsigned int byte_len = length / 8;
339 /* ??? Target foo issues various warnings here, so one might want to provide
340 a hook here. However, our caller is defined in tc-foo.c so there
341 shouldn't be a need for a hook. */
343 /* Write out the instruction.
344 It is important to fetch enough space in one call to `frag_more'.
345 We use (f - frag_now->fr_literal) to compute where we are and we
346 don't want frag_now to change between calls.
348 Relaxable instructions: We need to ensure we allocate enough
349 space for the largest insn. */
351 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
352 abort (); /* These currently shouldn't get here. */
354 /* Is there a relaxable insn with the relaxable operand needing a fixup? */
356 relax_operand = -1;
357 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0)
359 /* Scan the fixups for the operand affected by relaxing
360 (i.e. the branch address). */
362 for (i = 0; i < num_fixups; ++ i)
364 if (CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
365 CGEN_OPERAND_RELAX) != 0)
367 relax_operand = i;
368 break;
373 if (relax_operand != -1)
375 int max_len;
376 fragS * old_frag;
378 #ifdef TC_CGEN_MAX_RELAX
379 max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
380 #else
381 max_len = CGEN_MAX_INSN_SIZE;
382 #endif
383 /* Ensure variable part and fixed part are in same fragment. */
384 /* FIXME: Having to do this seems like a hack. */
385 frag_grow (max_len);
387 /* Allocate space for the fixed part. */
388 f = frag_more (byte_len);
390 /* Create a relaxable fragment for this instruction. */
391 old_frag = frag_now;
393 frag_var (rs_machine_dependent,
394 max_len - byte_len /* max chars */,
395 0 /* variable part already allocated */,
396 /* FIXME: When we machine generate the relax table,
397 machine generate a macro to compute subtype. */
398 1 /* subtype */,
399 fixups[relax_operand].exp.X_add_symbol,
400 fixups[relax_operand].exp.X_add_number,
403 /* Record the operand number with the fragment so md_convert_frag
404 can use cgen_md_record_fixup to record the appropriate reloc. */
405 old_frag->fr_cgen.insn = insn;
406 old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
407 old_frag->fr_cgen.opinfo = fixups[relax_operand].opinfo;
409 else
410 f = frag_more (byte_len);
412 /* If we're recording insns as numbers (rather than a string of bytes),
413 target byte order handling is deferred until now. */
414 #if 0 /*def CGEN_INT_INSN*/
415 switch (length)
417 case 16:
418 if (cgen_big_endian_p)
419 bfd_putb16 ((bfd_vma) * buf, f);
420 else
421 bfd_putl16 ((bfd_vma) * buf, f);
422 break;
423 case 32:
424 if (cgen_big_endian_p)
425 bfd_putb32 ((bfd_vma) * buf, f);
426 else
427 bfd_putl32 ((bfd_vma) * buf, f);
428 break;
429 default:
430 abort ();
432 #else
433 memcpy (f, buf, byte_len);
434 #endif
436 /* Create any fixups. */
437 for (i = 0; i < num_fixups; ++i)
439 /* Don't create fixups for these. That's done during relaxation.
440 We don't need to test for CGEN_INSN_RELAX as they can't get here
441 (see above). */
442 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0
443 && CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
444 CGEN_OPERAND_RELAX) != 0)
445 continue;
447 #ifndef md_cgen_record_fixup_exp
448 #define md_cgen_record_fixup_exp cgen_record_fixup_exp
449 #endif
451 md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
452 insn, length,
453 & CGEN_SYM (operand_table) [fixups[i].opindex],
454 fixups[i].opinfo,
455 & fixups[i].exp);
458 return f;
461 /* Apply a fixup to the object code. This is called for all the
462 fixups we generated by the call to fix_new_exp, above. In the call
463 above we used a reloc code which was the largest legal reloc code
464 plus the operand index. Here we undo that to recover the operand
465 index. At this point all symbol values should be fully resolved,
466 and we attempt to completely resolve the reloc. If we can not do
467 that, we determine the correct reloc code and put it back in the fixup. */
469 /* FIXME: This function handles some of the fixups and bfd_install_relocation
470 handles the rest. bfd_install_relocation (or some other bfd function)
471 should handle them all. */
474 cgen_md_apply_fix3 (fixP, valueP, seg)
475 fixS * fixP;
476 valueT * valueP;
477 segT seg;
479 char * where = fixP->fx_frag->fr_literal + fixP->fx_where;
480 valueT value;
482 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
483 the symbol values. Since we are using BFD_ASSEMBLER, if we are
484 doing this relocation the code in write.c is going to call
485 bfd_install_relocation, which is also going to use the symbol
486 value. That means that if the reloc is fully resolved we want to
487 use *valuep since bfd_install_relocation is not being used.
488 However, if the reloc is not fully resolved we do not want to use
489 *valuep, and must use fx_offset instead. However, if the reloc
490 is PC relative, we do want to use *valuep since it includes the
491 result of md_pcrel_from. This is confusing. */
493 if (fixP->fx_addsy == (symbolS *) NULL)
495 value = * valueP;
496 fixP->fx_done = 1;
498 else if (fixP->fx_pcrel)
499 value = * valueP;
500 else
502 value = fixP->fx_offset;
503 if (fixP->fx_subsy != (symbolS *) NULL)
505 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
506 value -= S_GET_VALUE (fixP->fx_subsy);
507 else
509 /* We don't actually support subtracting a symbol. */
510 as_bad_where (fixP->fx_file, fixP->fx_line,
511 _("expression too complex"));
516 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
518 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
519 const CGEN_OPERAND * operand = & CGEN_SYM (operand_table) [opindex];
520 const char * errmsg;
521 bfd_reloc_code_real_type reloc_type;
522 CGEN_FIELDS fields;
523 const CGEN_INSN * insn = (CGEN_INSN *) fixP->tc_fix_data.insn;
525 /* If the reloc has been fully resolved finish the operand here. */
526 /* FIXME: This duplicates the capabilities of code in BFD. */
527 if (fixP->fx_done
528 /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
529 finish the job. Testing for pcrel is a temporary hack. */
530 || fixP->fx_pcrel)
532 CGEN_FIELDS_BITSIZE (& fields) = CGEN_INSN_BITSIZE (insn);
533 CGEN_SYM (set_operand) (opindex, & value, & fields);
534 errmsg = CGEN_SYM (insert_operand) (opindex, & fields, where);
535 if (errmsg)
536 as_warn_where (fixP->fx_file, fixP->fx_line, "%s\n", errmsg);
539 if (fixP->fx_done)
540 return 1;
542 /* The operand isn't fully resolved. Determine a BFD reloc value
543 based on the operand information and leave it to
544 bfd_install_relocation. Note that this doesn't work when
545 partial_inplace == false. */
547 reloc_type = CGEN_SYM (lookup_reloc) (insn, operand, fixP);
548 if (reloc_type != BFD_RELOC_NONE)
550 fixP->fx_r_type = reloc_type;
552 else
554 as_bad_where (fixP->fx_file, fixP->fx_line,
555 _("unresolved expression that must be resolved"));
556 fixP->fx_done = 1;
557 return 1;
560 else if (fixP->fx_done)
562 /* We're finished with this fixup. Install it because
563 bfd_install_relocation won't be called to do it. */
564 switch (fixP->fx_r_type)
566 case BFD_RELOC_8:
567 md_number_to_chars (where, value, 1);
568 break;
569 case BFD_RELOC_16:
570 md_number_to_chars (where, value, 2);
571 break;
572 case BFD_RELOC_32:
573 md_number_to_chars (where, value, 4);
574 break;
575 /* FIXME: later add support for 64 bits. */
576 default:
577 abort ();
580 else
582 /* bfd_install_relocation will be called to finish things up. */
585 /* Tuck `value' away for use by tc_gen_reloc.
586 See the comment describing fx_addnumber in write.h.
587 This field is misnamed (or misused :-). */
588 fixP->fx_addnumber = value;
590 return 1;
593 /* Translate internal representation of relocation info to BFD target format.
595 FIXME: To what extent can we get all relevant targets to use this? */
597 arelent *
598 cgen_tc_gen_reloc (section, fixP)
599 asection * section;
600 fixS * fixP;
602 arelent * reloc;
604 reloc = (arelent *) bfd_alloc (stdoutput, sizeof (arelent));
606 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
607 if (reloc->howto == (reloc_howto_type *) NULL)
609 as_bad_where (fixP->fx_file, fixP->fx_line,
610 _("internal error: can't export reloc type %d (`%s')"),
611 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
612 return NULL;
615 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
617 reloc->sym_ptr_ptr = & fixP->fx_addsy->bsym;
618 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
619 reloc->addend = fixP->fx_addnumber;
621 return reloc;