1 /* GAS interface for targets using CGEN: Cpu tools GENerator.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
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 the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "libiberty.h"
26 #include "cgen-desc.h"
30 #include "dwarf2dbg.h"
32 static void queue_fixup (int, int, expressionS *);
34 /* Opcode table descriptor, must be set by md_begin. */
36 CGEN_CPU_DESC gas_cgen_cpu_desc;
38 /* Callback to insert a register into the symbol table.
39 A target may choose to let GAS parse the registers.
40 ??? Not currently used. */
43 cgen_asm_record_register (name, number)
47 /* Use symbol_create here instead of symbol_new so we don't try to
48 output registers into the object file's symbol table. */
49 symbol_table_insert (symbol_create (name, reg_section,
50 number, &zero_address_frag));
53 /* We need to keep a list of fixups. We can't simply generate them as
54 we go, because that would require us to first create the frag, and
55 that would screw up references to ``.''.
57 This is used by cpu's with simple operands. It keeps knowledge of what
58 an `expressionS' is and what a `fixup' is out of CGEN which for the time
61 OPINDEX is the index in the operand table.
62 OPINFO is something the caller chooses to help in reloc determination. */
71 static struct fixup fixups[GAS_CGEN_MAX_FIXUPS];
72 static int num_fixups;
74 /* Prepare to parse an instruction.
75 ??? May wish to make this static and delete calls in md_assemble. */
78 gas_cgen_init_parse ()
86 queue_fixup (opindex, opinfo, expP)
91 /* We need to generate a fixup for this expression. */
92 if (num_fixups >= GAS_CGEN_MAX_FIXUPS)
93 as_fatal (_("too many fixups"));
94 fixups[num_fixups].exp = *expP;
95 fixups[num_fixups].opindex = opindex;
96 fixups[num_fixups].opinfo = opinfo;
100 /* The following functions allow fixup chains to be stored, retrieved,
101 and swapped. They are a generalization of a pre-existing scheme
102 for storing, restoring and swapping fixup chains that was used by
103 the m32r port. The functionality is essentially the same, only
104 instead of only being able to store a single fixup chain, an entire
105 array of fixup chains can be stored. It is the user's responsibility
106 to keep track of how many fixup chains have been stored and which
107 elements of the array they are in.
109 The algorithms used are the same as in the old scheme. Other than the
110 "array-ness" of the whole thing, the functionality is identical to the
113 gas_cgen_initialize_saved_fixups_array():
114 Sets num_fixups_in_chain to 0 for each element. Call this from
115 md_begin() if you plan to use these functions and you want the
116 fixup count in each element to be set to 0 initially. This is
117 not necessary, but it's included just in case. It performs
118 the same function for each element in the array of fixup chains
119 that gas_init_parse() performs for the current fixups.
121 gas_cgen_save_fixups (element):
122 element - element number of the array you wish to store the fixups
123 to. No mechanism is built in for tracking what element
126 gas_cgen_restore_fixups (element):
127 element - element number of the array you wish to restore the fixups
130 gas_cgen_swap_fixups(int element):
131 element - swap the current fixups with those in this element number.
136 struct fixup fixup_chain[GAS_CGEN_MAX_FIXUPS];
137 int num_fixups_in_chain;
140 static struct saved_fixups stored_fixups[MAX_SAVED_FIXUP_CHAINS];
143 gas_cgen_initialize_saved_fixups_array ()
147 while (i < MAX_SAVED_FIXUP_CHAINS)
148 stored_fixups[i++].num_fixups_in_chain = 0;
152 gas_cgen_save_fixups (i)
155 if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
157 as_fatal ("index into stored_fixups[] out of bounds");
161 stored_fixups[i].num_fixups_in_chain = num_fixups;
162 memcpy (stored_fixups[i].fixup_chain, fixups,
163 sizeof (fixups[0]) * num_fixups);
168 gas_cgen_restore_fixups (i)
171 if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
173 as_fatal ("index into stored_fixups[] out of bounds");
177 num_fixups = stored_fixups[i].num_fixups_in_chain;
178 memcpy (fixups, stored_fixups[i].fixup_chain,
179 (sizeof (stored_fixups[i].fixup_chain[0])) * num_fixups);
180 stored_fixups[i].num_fixups_in_chain = 0;
184 gas_cgen_swap_fixups (i)
187 if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
189 as_fatal ("index into stored_fixups[] out of bounds");
194 gas_cgen_restore_fixups (i);
196 else if (stored_fixups[i].num_fixups_in_chain == 0)
197 gas_cgen_save_fixups (i);
202 struct fixup tmp_fixup;
204 tmp = stored_fixups[i].num_fixups_in_chain;
205 stored_fixups[i].num_fixups_in_chain = num_fixups;
208 for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;)
210 tmp_fixup = stored_fixups[i].fixup_chain [tmp];
211 stored_fixups[i].fixup_chain[tmp] = fixups [tmp];
212 fixups [tmp] = tmp_fixup;
217 /* Default routine to record a fixup.
218 This is a cover function to fix_new.
219 It exists because we record INSN with the fixup.
221 FRAG and WHERE are their respective arguments to fix_new_exp.
223 OPINFO is something the caller chooses to help in reloc determination.
225 At this point we do not use a bfd_reloc_code_real_type for
226 operands residing in the insn, but instead just use the
227 operand index. This lets us easily handle fixups for any
228 operand type. We pick a BFD reloc type in md_apply_fix3. */
231 gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
234 const CGEN_INSN * insn;
236 const CGEN_OPERAND * operand;
243 /* It may seem strange to use operand->attrs and not insn->attrs here,
244 but it is the operand that has a pc relative relocation. */
245 fixP = fix_new (frag, where, length / 8, symbol, offset,
246 CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
247 (bfd_reloc_code_real_type)
248 ((int) BFD_RELOC_UNUSED
249 + (int) operand->type));
250 fixP->fx_cgen.insn = insn;
251 fixP->fx_cgen.opinfo = opinfo;
256 /* Default routine to record a fixup given an expression.
257 This is a cover function to fix_new_exp.
258 It exists because we record INSN with the fixup.
260 FRAG and WHERE are their respective arguments to fix_new_exp.
262 OPINFO is something the caller chooses to help in reloc determination.
264 At this point we do not use a bfd_reloc_code_real_type for
265 operands residing in the insn, but instead just use the
266 operand index. This lets us easily handle fixups for any
267 operand type. We pick a BFD reloc type in md_apply_fix3. */
270 gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
273 const CGEN_INSN * insn;
275 const CGEN_OPERAND * operand;
281 /* It may seem strange to use operand->attrs and not insn->attrs here,
282 but it is the operand that has a pc relative relocation. */
283 fixP = fix_new_exp (frag, where, length / 8, exp,
284 CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
285 (bfd_reloc_code_real_type)
286 ((int) BFD_RELOC_UNUSED
287 + (int) operand->type));
288 fixP->fx_cgen.insn = insn;
289 fixP->fx_cgen.opinfo = opinfo;
294 /* Used for communication between the next two procedures. */
295 static jmp_buf expr_jmp_buf;
296 static int expr_jmp_buf_p;
298 /* Callback for cgen interface. Parse the expression at *STRP.
299 The result is an error message or NULL for success (in which case
300 *STRP is advanced past the parsed text).
301 WANT is an indication of what the caller is looking for.
302 If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
303 a table entry with the insn, reset the queued fixups counter.
304 An enum cgen_parse_operand_result is stored in RESULTP.
305 OPINDEX is the operand's table entry index.
306 OPINFO is something the caller chooses to help in reloc determination.
307 The resulting value is stored in VALUEP. */
310 gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP)
311 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
312 enum cgen_parse_operand_type want;
316 enum cgen_parse_operand_result *resultP;
320 /* These are volatile to survive the setjmp. */
321 char * volatile hold;
322 enum cgen_parse_operand_result * volatile resultP_1;
325 static enum cgen_parse_operand_result *resultP_1;
330 if (want == CGEN_PARSE_OPERAND_INIT)
332 gas_cgen_init_parse ();
337 hold = input_line_pointer;
338 input_line_pointer = (char *) *strP;
340 /* We rely on md_operand to longjmp back to us.
341 This is done via gas_cgen_md_operand. */
342 if (setjmp (expr_jmp_buf) != 0)
345 input_line_pointer = (char *) hold;
346 *resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
347 return _("illegal operand");
355 *strP = input_line_pointer;
356 input_line_pointer = hold;
358 #ifdef TC_CGEN_PARSE_FIX_EXP
359 opinfo = TC_CGEN_PARSE_FIX_EXP (opinfo, & exp);
362 /* FIXME: Need to check `want'. */
367 errmsg = _("illegal operand");
368 *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
371 errmsg = _("missing operand");
372 *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
375 *valueP = exp.X_add_number;
376 *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
379 *valueP = exp.X_add_number;
380 *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
383 queue_fixup (opindex, opinfo, &exp);
385 *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
392 /* md_operand handler to catch unrecognized expressions and halt the
393 parsing process so the next entry can be tried.
395 ??? This could be done differently by adding code to `expression'. */
398 gas_cgen_md_operand (expressionP)
399 expressionS *expressionP ATTRIBUTE_UNUSED;
401 /* Don't longjmp if we're not called from within cgen_parse_operand(). */
403 longjmp (expr_jmp_buf, 1);
406 /* Finish assembling instruction INSN.
407 BUF contains what we've built up so far.
408 LENGTH is the size of the insn in bits.
409 RELAX_P is non-zero if relaxable insns should be emitted as such.
410 Otherwise they're emitted in non-relaxable forms.
411 The "result" is stored in RESULT if non-NULL. */
414 gas_cgen_finish_insn (insn, buf, length, relax_p, result)
415 const CGEN_INSN *insn;
416 CGEN_INSN_BYTES_PTR buf;
419 finished_insnS *result;
424 unsigned int byte_len = length / 8;
426 /* ??? Target foo issues various warnings here, so one might want to provide
427 a hook here. However, our caller is defined in tc-foo.c so there
428 shouldn't be a need for a hook. */
430 /* Write out the instruction.
431 It is important to fetch enough space in one call to `frag_more'.
432 We use (f - frag_now->fr_literal) to compute where we are and we
433 don't want frag_now to change between calls.
435 Relaxable instructions: We need to ensure we allocate enough
436 space for the largest insn. */
438 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
439 /* These currently shouldn't get here. */
442 /* Is there a relaxable insn with the relaxable operand needing a fixup? */
445 if (relax_p && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE))
447 /* Scan the fixups for the operand affected by relaxing
448 (i.e. the branch address). */
450 for (i = 0; i < num_fixups; ++i)
452 if (CGEN_OPERAND_ATTR_VALUE (cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex),
461 if (relax_operand != -1)
469 #ifdef TC_CGEN_MAX_RELAX
470 max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
472 max_len = CGEN_MAX_INSN_SIZE;
474 /* Ensure variable part and fixed part are in same fragment. */
475 /* FIXME: Having to do this seems like a hack. */
478 /* Allocate space for the fixed part. */
479 f = frag_more (byte_len);
481 /* Create a relaxable fragment for this instruction. */
484 exp = &fixups[relax_operand].exp;
485 sym = exp->X_add_symbol;
486 off = exp->X_add_number;
487 if (exp->X_op != O_constant && exp->X_op != O_symbol)
489 /* Handle complex expressions. */
490 sym = make_expr_symbol (exp);
494 frag_var (rs_machine_dependent,
495 max_len - byte_len /* max chars */,
496 0 /* variable part already allocated */,
497 /* FIXME: When we machine generate the relax table,
498 machine generate a macro to compute subtype. */
504 /* Record the operand number with the fragment so md_convert_frag
505 can use gas_cgen_md_record_fixup to record the appropriate reloc. */
506 old_frag->fr_cgen.insn = insn;
507 old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
508 old_frag->fr_cgen.opinfo = fixups[relax_operand].opinfo;
510 result->frag = old_frag;
514 f = frag_more (byte_len);
516 result->frag = frag_now;
519 /* If we're recording insns as numbers (rather than a string of bytes),
520 target byte order handling is deferred until now. */
522 cgen_put_insn_value (gas_cgen_cpu_desc, f, length, *buf);
524 memcpy (f, buf, byte_len);
527 /* Emit DWARF2 debugging information. */
528 dwarf2_emit_insn (byte_len);
530 /* Create any fixups. */
531 for (i = 0; i < num_fixups; ++i)
534 const CGEN_OPERAND *operand =
535 cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex);
537 /* Don't create fixups for these. That's done during relaxation.
538 We don't need to test for CGEN_INSN_RELAXED as they can't get here
541 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE)
542 && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_RELAX))
545 #ifndef md_cgen_record_fixup_exp
546 #define md_cgen_record_fixup_exp gas_cgen_record_fixup_exp
549 fixP = md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
550 insn, length, operand,
554 result->fixups[i] = fixP;
559 result->num_fixups = num_fixups;
564 /* Apply a fixup to the object code. This is called for all the
565 fixups we generated by the call to fix_new_exp, above. In the call
566 above we used a reloc code which was the largest legal reloc code
567 plus the operand index. Here we undo that to recover the operand
568 index. At this point all symbol values should be fully resolved,
569 and we attempt to completely resolve the reloc. If we can not do
570 that, we determine the correct reloc code and put it back in the fixup. */
572 /* FIXME: This function handles some of the fixups and bfd_install_relocation
573 handles the rest. bfd_install_relocation (or some other bfd function)
574 should handle them all. */
577 gas_cgen_md_apply_fix3 (fixP, valP, seg)
580 segT seg ATTRIBUTE_UNUSED;
582 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
583 valueT value = * valP;
584 /* Canonical name, since used a lot. */
585 CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
587 if (fixP->fx_addsy == (symbolS *) NULL)
590 /* We don't actually support subtracting a symbol. */
591 if (fixP->fx_subsy != (symbolS *) NULL)
592 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
594 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
596 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
597 const CGEN_OPERAND *operand = cgen_operand_lookup_by_num (cd, opindex);
599 bfd_reloc_code_real_type reloc_type;
600 CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
601 const CGEN_INSN *insn = fixP->fx_cgen.insn;
603 /* If the reloc has been fully resolved finish the operand here. */
604 /* FIXME: This duplicates the capabilities of code in BFD. */
606 /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
607 finish the job. Testing for pcrel is a temporary hack. */
610 CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
611 CGEN_CPU_SET_VMA_OPERAND (cd) (cd, opindex, fields, (bfd_vma) value);
615 CGEN_INSN_INT insn_value =
616 cgen_get_insn_value (cd, where, CGEN_INSN_BITSIZE (insn));
618 /* ??? 0 is passed for `pc'. */
619 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
620 &insn_value, (bfd_vma) 0);
621 cgen_put_insn_value (cd, where, CGEN_INSN_BITSIZE (insn),
625 /* ??? 0 is passed for `pc'. */
626 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields, where,
630 as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
636 /* The operand isn't fully resolved. Determine a BFD reloc value
637 based on the operand information and leave it to
638 bfd_install_relocation. Note that this doesn't work when
639 partial_inplace == false. */
641 reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
643 if (reloc_type != BFD_RELOC_NONE)
644 fixP->fx_r_type = reloc_type;
647 as_bad_where (fixP->fx_file, fixP->fx_line,
648 _("unresolved expression that must be resolved"));
653 else if (fixP->fx_done)
655 /* We're finished with this fixup. Install it because
656 bfd_install_relocation won't be called to do it. */
657 switch (fixP->fx_r_type)
660 md_number_to_chars (where, value, 1);
663 md_number_to_chars (where, value, 2);
666 md_number_to_chars (where, value, 4);
669 md_number_to_chars (where, value, 8);
672 as_bad_where (fixP->fx_file, fixP->fx_line,
673 _("internal error: can't install fix for reloc type %d (`%s')"),
674 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
679 bfd_install_relocation will be called to finish things up. */
681 /* Tuck `value' away for use by tc_gen_reloc.
682 See the comment describing fx_addnumber in write.h.
683 This field is misnamed (or misused :-). */
684 fixP->fx_addnumber = value;
687 /* Translate internal representation of relocation info to BFD target format.
689 FIXME: To what extent can we get all relevant targets to use this? */
692 gas_cgen_tc_gen_reloc (section, fixP)
693 asection * section ATTRIBUTE_UNUSED;
698 reloc = (arelent *) xmalloc (sizeof (arelent));
700 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
701 if (reloc->howto == (reloc_howto_type *) NULL)
703 as_bad_where (fixP->fx_file, fixP->fx_line,
704 _("relocation is not supported"));
708 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
710 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
711 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
713 /* Use fx_offset for these cases. */
714 if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
715 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
716 reloc->addend = fixP->fx_offset;
718 reloc->addend = fixP->fx_addnumber;
720 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
724 /* Perform any cgen specific initialisation.
725 Called after gas_cgen_cpu_desc has been created. */
730 if (flag_signed_overflow_ok)
731 cgen_set_signed_overflow_ok (gas_cgen_cpu_desc);
733 cgen_clear_signed_overflow_ok (gas_cgen_cpu_desc);