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. */
27 /* Callback to insert a register into the symbol table.
28 A target may choose to let GAS parse the registers.
29 ??? Not currently used. */
32 cgen_asm_record_register (name
, number
)
36 /* Use symbol_create here instead of symbol_new so we don't try to
37 output registers into the object file's symbol table. */
38 symbol_table_insert (symbol_create (name
, reg_section
,
39 number
, &zero_address_frag
));
42 /* We need to keep a list of fixups. We can't simply generate them as
43 we go, because that would require us to first create the frag, and
44 that would screw up references to ``.''.
46 This is used by cpu's with simple operands. It keeps knowledge of what
47 an `expressionS' is and what a `fixup' is out of CGEN which for the time
50 OPINDEX is the index in the operand table.
51 OPINFO is something the caller chooses to help in reloc determination. */
62 static struct fixup fixups
[MAX_FIXUPS
];
63 static int num_fixups
;
65 /* Prepare to parse an instruction.
66 ??? May wish to make this static and delete calls in md_assemble. */
69 cgen_asm_init_parse ()
77 cgen_queue_fixup (opindex
, opinfo
, expP
)
81 /* We need to generate a fixup for this expression. */
82 if (num_fixups
>= MAX_FIXUPS
)
83 as_fatal ("too many fixups");
84 fixups
[num_fixups
].exp
= *expP
;
85 fixups
[num_fixups
].opindex
= opindex
;
86 fixups
[num_fixups
].opinfo
= opinfo
;
90 /* Default routine to record a fixup.
91 This is a cover function to fix_new.
92 It exists because we record INSN with the fixup.
94 FRAG and WHERE are their respective arguments to fix_new_exp.
96 OPINFO is something the caller chooses to help in reloc determination.
98 At this point we do not use a bfd_reloc_code_real_type for
99 operands residing in the insn, but instead just use the
100 operand index. This lets us easily handle fixups for any
101 operand type. We pick a BFD reloc type in md_apply_fix. */
104 cgen_record_fixup (frag
, where
, insn
, length
, operand
, opinfo
, symbol
, offset
)
107 const CGEN_INSN
*insn
;
109 const CGEN_OPERAND
*operand
;
116 /* It may seem strange to use operand->attrs and not insn->attrs here,
117 but it is the operand that has a pc relative relocation. */
119 fixP
= fix_new (frag
, where
, length
/ 8, symbol
, offset
,
120 CGEN_OPERAND_ATTR (operand
, CGEN_OPERAND_PCREL_ADDR
) != 0,
121 (bfd_reloc_code_real_type
) ((int) BFD_RELOC_UNUSED
+ CGEN_OPERAND_INDEX (operand
)));
122 fixP
->tc_fix_data
.insn
= (PTR
) insn
;
123 fixP
->tc_fix_data
.opinfo
= opinfo
;
128 /* Default routine to record a fixup given an expression.
129 This is a cover function to fix_new_exp.
130 It exists because we record INSN with the fixup.
132 FRAG and WHERE are their respective arguments to fix_new_exp.
134 OPINFO is something the caller chooses to help in reloc determination.
136 At this point we do not use a bfd_reloc_code_real_type for
137 operands residing in the insn, but instead just use the
138 operand index. This lets us easily handle fixups for any
139 operand type. We pick a BFD reloc type in md_apply_fix. */
142 cgen_record_fixup_exp (frag
, where
, insn
, length
, operand
, opinfo
, exp
)
145 const CGEN_INSN
*insn
;
147 const CGEN_OPERAND
*operand
;
153 /* It may seem strange to use operand->attrs and not insn->attrs here,
154 but it is the operand that has a pc relative relocation. */
156 fixP
= fix_new_exp (frag
, where
, length
/ 8, exp
,
157 CGEN_OPERAND_ATTR (operand
, CGEN_OPERAND_PCREL_ADDR
) != 0,
158 (bfd_reloc_code_real_type
) ((int) BFD_RELOC_UNUSED
+ CGEN_OPERAND_INDEX (operand
)));
159 fixP
->tc_fix_data
.insn
= (PTR
) insn
;
160 fixP
->tc_fix_data
.opinfo
= opinfo
;
165 /* Used for communication between the next two procedures. */
166 static jmp_buf expr_jmp_buf
;
168 /* Callback for cgen interface. Parse the expression at *STRP.
169 The result is an error message or NULL for success (in which case
170 *STRP is advanced past the parsed text).
171 WANT is an indication of what the caller is looking for.
172 If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
173 a table entry with the insn, reset the queued fixups counter.
174 An enum cgen_parse_operand_result is stored in RESULTP.
175 OPINDEX is the operand's table entry index.
176 OPINFO is something the caller chooses to help in reloc determination.
177 The resulting value is stored in VALUEP. */
180 cgen_parse_operand (want
, strP
, opindex
, opinfo
, resultP
, valueP
)
181 enum cgen_parse_operand_type want
;
185 enum cgen_parse_operand_result
*resultP
;
189 /* These is volatile to survive the setjmp. */
190 char * volatile hold
;
191 enum cgen_parse_operand_result
* volatile resultP_1
;
194 static enum cgen_parse_operand_result
*resultP_1
;
196 const char *errmsg
= NULL
;
199 if (want
== CGEN_PARSE_OPERAND_INIT
)
201 cgen_asm_init_parse ();
206 hold
= input_line_pointer
;
207 input_line_pointer
= (char *) *strP
;
209 /* We rely on md_operand to longjmp back to us.
210 This is done via cgen_md_operand. */
211 if (setjmp (expr_jmp_buf
) != 0)
213 input_line_pointer
= (char *) hold
;
214 *resultP_1
= CGEN_PARSE_OPERAND_RESULT_ERROR
;
215 return "illegal operand";
220 *strP
= input_line_pointer
;
221 input_line_pointer
= hold
;
223 /* FIXME: Need to check `want'. */
228 errmsg
= "illegal operand";
229 *resultP
= CGEN_PARSE_OPERAND_RESULT_ERROR
;
232 errmsg
= "missing operand";
233 *resultP
= CGEN_PARSE_OPERAND_RESULT_ERROR
;
236 *valueP
= exp
.X_add_number
;
237 *resultP
= CGEN_PARSE_OPERAND_RESULT_NUMBER
;
240 *valueP
= exp
.X_add_number
;
241 *resultP
= CGEN_PARSE_OPERAND_RESULT_REGISTER
;
244 cgen_queue_fixup (opindex
, opinfo
, &exp
);
246 *resultP
= CGEN_PARSE_OPERAND_RESULT_QUEUED
;
253 /* md_operand handler to catch unrecognized expressions and halt the
254 parsing process so the next entry can be tried.
256 ??? This could be done differently by adding code to `expression'. */
259 cgen_md_operand (expressionP
)
260 expressionS
*expressionP
;
262 longjmp (expr_jmp_buf
, 1);
265 /* Finish assembling instruction INSN.
266 BUF contains what we've built up so far.
267 LENGTH is the size of the insn in bits. */
270 cgen_asm_finish_insn (insn
, buf
, length
)
271 const CGEN_INSN
*insn
;
275 int i
, relax_operand
;
277 unsigned int byte_len
= length
/ 8;
279 /* ??? Target foo issues various warnings here, so one might want to provide
280 a hook here. However, our caller is defined in tc-foo.c so there
281 shouldn't be a need for a hook. */
283 /* Write out the instruction.
284 It is important to fetch enough space in one call to `frag_more'.
285 We use (f - frag_now->fr_literal) to compute where we are and we
286 don't want frag_now to change between calls.
288 Relaxable instructions: We need to ensure we allocate enough
289 space for the largest insn. */
291 if (CGEN_INSN_ATTR (insn
, CGEN_INSN_RELAX
) != 0)
292 abort (); /* These currently shouldn't get here. */
294 /* Is there a relaxable insn with the relaxable operand needing a fixup? */
297 if (CGEN_INSN_ATTR (insn
, CGEN_INSN_RELAXABLE
) != 0)
299 /* Scan the fixups for the operand affected by relaxing
300 (i.e. the branch address). */
302 for (i
= 0; i
< num_fixups
; ++i
)
304 if (CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table
) [fixups
[i
].opindex
],
305 CGEN_OPERAND_RELAX
) != 0)
313 if (relax_operand
!= -1)
318 #ifdef TC_CGEN_MAX_RELAX
319 max_len
= TC_CGEN_MAX_RELAX (insn
, byte_len
);
321 max_len
= CGEN_MAX_INSN_SIZE
;
323 /* Ensure variable part and fixed part are in same fragment. */
324 /* FIXME: Having to do this seems like a hack. */
326 /* Allocate space for the fixed part. */
327 f
= frag_more (byte_len
);
328 /* Create a relaxable fragment for this instruction. */
330 frag_var (rs_machine_dependent
,
331 max_len
- byte_len
/* max chars */,
332 0 /* variable part already allocated */,
333 /* FIXME: When we machine generate the relax table,
334 machine generate a macro to compute subtype. */
336 fixups
[relax_operand
].exp
.X_add_symbol
,
337 fixups
[relax_operand
].exp
.X_add_number
,
339 /* Record the operand number with the fragment so md_convert_frag
340 can use cgen_md_record_fixup to record the appropriate reloc. */
341 old_frag
->fr_cgen
.insn
= insn
;
342 old_frag
->fr_cgen
.opindex
= fixups
[relax_operand
].opindex
;
343 old_frag
->fr_cgen
.opinfo
= fixups
[relax_operand
].opinfo
;
346 f
= frag_more (byte_len
);
348 /* If we're recording insns as numbers (rather than a string of bytes),
349 target byte order handling is deferred until now. */
350 #if 0 /*def CGEN_INT_INSN*/
354 if (cgen_big_endian_p
)
355 bfd_putb16 ((bfd_vma
) *buf
, f
);
357 bfd_putl16 ((bfd_vma
) *buf
, f
);
360 if (cgen_big_endian_p
)
361 bfd_putb32 ((bfd_vma
) *buf
, f
);
363 bfd_putl32 ((bfd_vma
) *buf
, f
);
369 memcpy (f
, buf
, byte_len
);
372 /* Create any fixups. */
373 for (i
= 0; i
< num_fixups
; ++i
)
375 /* Don't create fixups for these. That's done during relaxation.
376 We don't need to test for CGEN_INSN_RELAX as they can't get here
378 if (CGEN_INSN_ATTR (insn
, CGEN_INSN_RELAXABLE
) != 0
379 && CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table
) [fixups
[i
].opindex
],
380 CGEN_OPERAND_RELAX
) != 0)
383 #ifndef md_cgen_record_fixup_exp
384 #define md_cgen_record_fixup_exp cgen_record_fixup_exp
387 md_cgen_record_fixup_exp (frag_now
, f
- frag_now
->fr_literal
,
389 & CGEN_SYM (operand_table
) [fixups
[i
].opindex
],
395 /* Apply a fixup to the object code. This is called for all the
396 fixups we generated by the call to fix_new_exp, above. In the call
397 above we used a reloc code which was the largest legal reloc code
398 plus the operand index. Here we undo that to recover the operand
399 index. At this point all symbol values should be fully resolved,
400 and we attempt to completely resolve the reloc. If we can not do
401 that, we determine the correct reloc code and put it back in the fixup. */
403 /* FIXME: This function handles some of the fixups and bfd_install_relocation
404 handles the rest. bfd_install_relocation (or some other bfd function)
405 should handle them all. */
408 cgen_md_apply_fix3 (fixP
, valueP
, seg
)
413 char *where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
416 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
417 the symbol values. Since we are using BFD_ASSEMBLER, if we are
418 doing this relocation the code in write.c is going to call
419 bfd_install_relocation, which is also going to use the symbol
420 value. That means that if the reloc is fully resolved we want to
421 use *valuep since bfd_install_relocation is not being used.
422 However, if the reloc is not fully resolved we do not want to use
423 *valuep, and must use fx_offset instead. However, if the reloc
424 is PC relative, we do want to use *valuep since it includes the
425 result of md_pcrel_from. This is confusing. */
427 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
432 else if (fixP
->fx_pcrel
)
436 value
= fixP
->fx_offset
;
437 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
439 if (S_GET_SEGMENT (fixP
->fx_subsy
) == absolute_section
)
440 value
-= S_GET_VALUE (fixP
->fx_subsy
);
443 /* We don't actually support subtracting a symbol. */
444 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
445 "expression too complex");
450 if ((int) fixP
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
452 int opindex
= (int) fixP
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
453 const CGEN_OPERAND
*operand
= & CGEN_SYM (operand_table
) [opindex
];
455 bfd_reloc_code_real_type reloc_type
;
457 const CGEN_INSN
*insn
= (CGEN_INSN
*) fixP
->tc_fix_data
.insn
;
459 /* If the reloc has been fully resolved finish the operand here. */
460 /* FIXME: This duplicates the capabilities of code in BFD. */
462 /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
463 finish the job. Testing for pcrel is a temporary hack. */
466 /* This may seem like overkill, and using bfd_install_relocation or
467 some such may be preferable, but this is simple. */
468 CGEN_FIELDS_BITSIZE (&fields
) = CGEN_INSN_BITSIZE (insn
);
469 CGEN_SYM (set_operand
) (opindex
, &value
, &fields
);
470 errmsg
= CGEN_SYM (validate_operand
) (opindex
, &fields
);
472 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, "%s\n", errmsg
);
473 CGEN_SYM (insert_operand
) (opindex
, &fields
, where
);
479 /* The operand isn't fully resolved. Determine a BFD reloc value
480 based on the operand information and leave it to
481 bfd_install_relocation. Note that this doesn't work when
482 partial_inplace == false. */
484 reloc_type
= CGEN_SYM (lookup_reloc
) (insn
, operand
, fixP
);
485 if (reloc_type
!= BFD_RELOC_NONE
)
487 fixP
->fx_r_type
= reloc_type
;
491 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
492 "unresolved expression that must be resolved");
497 else if (fixP
->fx_done
)
499 /* We're finished with this fixup. Install it because
500 bfd_install_relocation won't be called to do it. */
501 switch (fixP
->fx_r_type
)
504 md_number_to_chars (where
, value
, 1);
507 md_number_to_chars (where
, value
, 2);
510 md_number_to_chars (where
, value
, 4);
512 /* FIXME: later add support for 64 bits. */
519 /* bfd_install_relocation will be called to finish things up. */
522 /* Tuck `value' away for use by tc_gen_reloc.
523 See the comment describing fx_addnumber in write.h.
524 This field is misnamed (or misused :-). */
525 fixP
->fx_addnumber
= value
;
530 /* Translate internal representation of relocation info to BFD target format.
532 FIXME: To what extent can we get all relevant targets to use this? */
535 cgen_tc_gen_reloc (section
, fixP
)
541 reloc
= (arelent
*) bfd_alloc (stdoutput
, sizeof (arelent
));
543 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
544 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
546 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
547 "internal error: can't export reloc type %d (`%s')",
548 fixP
->fx_r_type
, bfd_get_reloc_code_name (fixP
->fx_r_type
));
552 assert (!fixP
->fx_pcrel
== !reloc
->howto
->pc_relative
);
554 reloc
->sym_ptr_ptr
= &fixP
->fx_addsy
->bsym
;
555 reloc
->address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
556 reloc
->addend
= fixP
->fx_addnumber
;