1 /* GAS interface for targets using CGEN: Cpu tools GENerator.
2 Copyright (C) 1996, 1997 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)
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. */
26 /* Callback to insert a register into the symbol table.
27 A target may choose to let GAS parse the registers.
28 ??? Not currently used. */
31 cgen_asm_record_register (name, number)
35 /* Use symbol_create here instead of symbol_new so we don't try to
36 output registers into the object file's symbol table. */
37 symbol_table_insert (symbol_create (name, reg_section,
38 number, &zero_address_frag));
41 /* We need to keep a list of fixups. We can't simply generate them as
42 we go, because that would require us to first create the frag, and
43 that would screw up references to ``.''.
45 This is used by cpu's with simple operands. It keeps knowledge of what
46 an `expressionS' is and what a `fixup' is out of CGEN which for the time
49 OPINDEX is the index in the operand table.
50 OPINFO is something the caller chooses to help in reloc determination. */
61 static struct fixup fixups[MAX_FIXUPS];
62 static int num_fixups;
64 /* Prepare to parse an instruction.
65 ??? May wish to make this static and delete calls in md_assemble. */
68 cgen_asm_init_parse ()
76 cgen_queue_fixup (opindex, opinfo, expP)
80 /* We need to generate a fixup for this expression. */
81 if (num_fixups >= MAX_FIXUPS)
82 as_fatal ("too many fixups");
83 fixups[num_fixups].exp = *expP;
84 fixups[num_fixups].opindex = opindex;
85 fixups[num_fixups].opinfo = opinfo;
89 /* Default routine to record a fixup.
90 This is a cover function to fix_new.
91 It exists because we record INSN with the fixup.
93 FRAG and WHERE are their respective arguments to fix_new_exp.
95 OPINFO is something the caller chooses to help in reloc determination.
97 At this point we do not use a bfd_reloc_code_real_type for
98 operands residing in the insn, but instead just use the
99 operand index. This lets us easily handle fixups for any
100 operand type. We pick a BFD reloc type in md_apply_fix. */
103 cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
106 const struct cgen_insn *insn;
108 const struct cgen_operand *operand;
115 /* It may seem strange to use operand->attrs and not insn->attrs here,
116 but it is the operand that has a pc relative relocation. */
118 fixP = fix_new (frag, where, length / 8, symbol, offset,
119 CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
120 (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
121 fixP->tc_fix_data.insn = (PTR) insn;
122 fixP->tc_fix_data.opinfo = opinfo;
127 /* Default routine to record a fixup given an expression.
128 This is a cover function to fix_new_exp.
129 It exists because we record INSN with the fixup.
131 FRAG and WHERE are their respective arguments to fix_new_exp.
133 OPINFO is something the caller chooses to help in reloc determination.
135 At this point we do not use a bfd_reloc_code_real_type for
136 operands residing in the insn, but instead just use the
137 operand index. This lets us easily handle fixups for any
138 operand type. We pick a BFD reloc type in md_apply_fix. */
141 cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
144 const struct cgen_insn *insn;
146 const struct cgen_operand *operand;
152 /* It may seem strange to use operand->attrs and not insn->attrs here,
153 but it is the operand that has a pc relative relocation. */
155 fixP = fix_new_exp (frag, where, length / 8, exp,
156 CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
157 (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
158 fixP->tc_fix_data.insn = (PTR) insn;
159 fixP->tc_fix_data.opinfo = opinfo;
164 /* Callback for cgen interface. Parse the expression at *STRP.
165 The result is an error message or NULL for success (in which case
166 *STRP is advanced past the parsed text).
167 WANT is an indication of what the caller is looking for.
168 If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
169 a table entry with the insn, reset the queued fixups counter.
170 An enum cgen_parse_operand_result is stored in RESULTP.
171 OPINDEX is the operand's table entry index.
172 OPINFO is something the caller chooses to help in reloc determination.
173 The resulting value is stored in VALUEP. */
176 cgen_parse_operand (want, strP, opindex, opinfo, resultP, valueP)
177 enum cgen_parse_operand_type want;
181 enum cgen_parse_operand_result *resultP;
185 const char *errmsg = NULL;
188 if (want == CGEN_PARSE_OPERAND_INIT)
190 cgen_asm_init_parse ();
194 hold = input_line_pointer;
195 input_line_pointer = (char *) *strP;
197 *strP = input_line_pointer;
198 input_line_pointer = hold;
200 /* FIXME: Need to check `want'. */
205 errmsg = "illegal operand";
206 *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
209 errmsg = "missing operand";
210 *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
213 *valueP = exp.X_add_number;
214 *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
217 *valueP = exp.X_add_number;
218 *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
221 cgen_queue_fixup (opindex, opinfo, &exp);
223 *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
230 /* Finish assembling instruction INSN.
231 BUF contains what we've built up so far.
232 LENGTH is the size of the insn in bits. */
235 cgen_asm_finish_insn (insn, buf, length)
236 const struct cgen_insn *insn;
240 int i, relax_operand;
242 unsigned int byte_len = length / 8;
244 /* ??? Target foo issues various warnings here, so one might want to provide
245 a hook here. However, our caller is defined in tc-foo.c so there
246 shouldn't be a need for a hook. */
248 /* Write out the instruction.
249 It is important to fetch enough space in one call to `frag_more'.
250 We use (f - frag_now->fr_literal) to compute where we are and we
251 don't want frag_now to change between calls.
253 Relaxable instructions: We need to ensure we allocate enough
254 space for the largest insn. */
256 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
257 abort (); /* These currently shouldn't get here. */
259 /* Is there a relaxable insn with the relaxable operand needing a fixup? */
262 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0)
264 /* Scan the fixups for the operand affected by relaxing
265 (i.e. the branch address). */
267 for (i = 0; i < num_fixups; ++i)
269 if (CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
270 CGEN_OPERAND_RELAX) != 0)
278 if (relax_operand != -1)
283 #ifdef TC_CGEN_MAX_RELAX
284 max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
286 max_len = CGEN_MAX_INSN_SIZE;
288 /* Ensure variable part and fixed part are in same fragment. */
289 /* FIXME: Having to do this seems like a hack. */
291 /* Allocate space for the fixed part. */
292 f = frag_more (byte_len);
293 /* Create a relaxable fragment for this instruction. */
295 frag_var (rs_machine_dependent,
296 max_len - byte_len /* max chars */,
297 0 /* variable part already allocated */,
298 /* FIXME: When we machine generate the relax table,
299 machine generate a macro to compute subtype. */
301 fixups[relax_operand].exp.X_add_symbol,
302 fixups[relax_operand].exp.X_add_number,
304 /* Record the operand number with the fragment so md_convert_frag
305 can use cgen_md_record_fixup to record the appropriate reloc. */
306 /* FIXME: fr_targ.cgen is used pending deciding whether to
307 allow a target to add members to fragS. For more info
308 see the comment above fr_targ in as.h. */
309 old_frag->fr_targ.cgen.insn = insn;
310 old_frag->fr_targ.cgen.opindex = fixups[relax_operand].opindex;
311 old_frag->fr_targ.cgen.opinfo = fixups[relax_operand].opinfo;
314 f = frag_more (byte_len);
316 /* If we're recording insns as numbers (rather than a string of bytes),
317 target byte order handling is deferred until now. */
318 #if 0 /*def CGEN_INT_INSN*/
322 if (cgen_big_endian_p)
323 bfd_putb16 ((bfd_vma) *buf, f);
325 bfd_putl16 ((bfd_vma) *buf, f);
328 if (cgen_big_endian_p)
329 bfd_putb32 ((bfd_vma) *buf, f);
331 bfd_putl32 ((bfd_vma) *buf, f);
337 memcpy (f, buf, byte_len);
340 /* Create any fixups. */
341 for (i = 0; i < num_fixups; ++i)
343 /* Don't create fixups for these. That's done during relaxation.
344 We don't need to test for CGEN_INSN_RELAX as they can't get here
346 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0
347 && CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
348 CGEN_OPERAND_RELAX) != 0)
351 #ifndef md_cgen_record_fixup_exp
352 #define md_cgen_record_fixup_exp cgen_record_fixup_exp
355 md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
357 & CGEN_SYM (operand_table) [fixups[i].opindex],
363 /* Apply a fixup to the object code. This is called for all the
364 fixups we generated by the call to fix_new_exp, above. In the call
365 above we used a reloc code which was the largest legal reloc code
366 plus the operand index. Here we undo that to recover the operand
367 index. At this point all symbol values should be fully resolved,
368 and we attempt to completely resolve the reloc. If we can not do
369 that, we determine the correct reloc code and put it back in the fixup. */
371 /* FIXME: This function handles some of the fixups and bfd_install_relocation
372 handles the rest. bfd_install_relocation (or some other bfd function)
373 should handle them all. */
376 cgen_md_apply_fix3 (fixP, valueP, seg)
381 char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
384 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
385 the symbol values. Since we are using BFD_ASSEMBLER, if we are
386 doing this relocation the code in write.c is going to call
387 bfd_install_relocation, which is also going to use the symbol
388 value. That means that if the reloc is fully resolved we want to
389 use *valuep since bfd_install_relocation is not being used.
390 However, if the reloc is not fully resolved we do not want to use
391 *valuep, and must use fx_offset instead. However, if the reloc
392 is PC relative, we do want to use *valuep since it includes the
393 result of md_pcrel_from. This is confusing. */
395 if (fixP->fx_addsy == (symbolS *) NULL)
400 else if (fixP->fx_pcrel)
404 value = fixP->fx_offset;
405 if (fixP->fx_subsy != (symbolS *) NULL)
407 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
408 value -= S_GET_VALUE (fixP->fx_subsy);
411 /* We don't actually support subtracting a symbol. */
412 as_bad_where (fixP->fx_file, fixP->fx_line,
413 "expression too complex");
418 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
420 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
421 const struct cgen_operand *operand = & CGEN_SYM (operand_table) [opindex];
423 bfd_reloc_code_real_type reloc_type;
424 struct cgen_fields fields;
425 const struct cgen_insn *insn = (struct cgen_insn *) fixP->tc_fix_data.insn;
427 /* If the reloc has been fully resolved finish the operand here. */
428 /* FIXME: This duplicates the capabilities of code in BFD. */
430 /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
431 finish the job. Testing for pcrel is a temporary hack. */
434 /* This may seem like overkill, and using bfd_install_relocation or
435 some such may be preferable, but this is simple. */
436 CGEN_FIELDS_BITSIZE (&fields) = CGEN_INSN_BITSIZE (insn);
437 CGEN_SYM (set_operand) (opindex, &value, &fields);
438 errmsg = CGEN_SYM (validate_operand) (opindex, &fields);
440 as_warn_where (fixP->fx_file, fixP->fx_line, "%s\n", errmsg);
441 CGEN_SYM (insert_operand) (opindex, &fields, where);
447 /* The operand isn't fully resolved. Determine a BFD reloc value
448 based on the operand information and leave it to
449 bfd_install_relocation. Note that this doesn't work when
450 partial_inplace == false. */
452 reloc_type = CGEN_SYM (lookup_reloc) (insn, operand, fixP);
453 if (reloc_type != BFD_RELOC_NONE)
455 fixP->fx_r_type = reloc_type;
459 as_bad_where (fixP->fx_file, fixP->fx_line,
460 "unresolved expression that must be resolved");
465 else if (fixP->fx_done)
467 /* We're finished with this fixup. Install it because
468 bfd_install_relocation won't be called to do it. */
469 switch (fixP->fx_r_type)
472 md_number_to_chars (where, value, 1);
475 md_number_to_chars (where, value, 2);
478 md_number_to_chars (where, value, 4);
480 /* FIXME: later add support for 64 bits. */
487 /* bfd_install_relocation will be called to finish things up. */
490 /* Tuck `value' away for use by tc_gen_reloc.
491 See the comment describing fx_addnumber in write.h.
492 This field is misnamed (or misused :-). */
493 fixP->fx_addnumber = value;
498 /* Translate internal representation of relocation info to BFD target format.
500 FIXME: To what extent can we get all relevant targets to use this? */
503 cgen_tc_gen_reloc (section, fixP)
509 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
511 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
512 if (reloc->howto == (reloc_howto_type *) NULL)
514 as_bad_where (fixP->fx_file, fixP->fx_line,
515 "internal error: can't export reloc type %d (`%s')",
516 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
520 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
522 reloc->sym_ptr_ptr = &fixP->fx_addsy->bsym;
523 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
524 reloc->addend = fixP->fx_addnumber;