]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/cgen.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gas / cgen.c
CommitLineData
841eff9e 1/* GAS interface for targets using CGEN: Cpu tools GENerator.
310addc4 2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
841eff9e
DE
3
4This file is part of GAS, the GNU Assembler.
5
6GAS is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GAS is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GAS; see the file COPYING. If not, write to the Free Software
18Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
1002d8ed 20#include <setjmp.h>
841eff9e 21#include "ansidecl.h"
0f829c8e 22#include "libiberty.h"
841eff9e 23#include "bfd.h"
48401fcf 24#include "symcat.h"
310addc4 25#include "cgen-desc.h"
841eff9e
DE
26#include "as.h"
27#include "subsegs.h"
defc70bf 28#include "cgen.h"
841eff9e 29
0f829c8e
CM
30/* Opcode table descriptor, must be set by md_begin. */
31
310addc4 32CGEN_CPU_DESC gas_cgen_cpu_desc;
0f829c8e 33
841eff9e
DE
34/* Callback to insert a register into the symbol table.
35 A target may choose to let GAS parse the registers.
36 ??? Not currently used. */
37
38void
39cgen_asm_record_register (name, number)
ebde3f62 40 char * name;
defc70bf 41 int number;
841eff9e
DE
42{
43 /* Use symbol_create here instead of symbol_new so we don't try to
44 output registers into the object file's symbol table. */
45 symbol_table_insert (symbol_create (name, reg_section,
ebde3f62 46 number, & zero_address_frag));
841eff9e
DE
47}
48
49/* We need to keep a list of fixups. We can't simply generate them as
50 we go, because that would require us to first create the frag, and
51 that would screw up references to ``.''.
52
53 This is used by cpu's with simple operands. It keeps knowledge of what
54 an `expressionS' is and what a `fixup' is out of CGEN which for the time
55 being is preferable.
56
57 OPINDEX is the index in the operand table.
58 OPINFO is something the caller chooses to help in reloc determination. */
59
60struct fixup
61{
defc70bf
DE
62 int opindex;
63 int opinfo;
841eff9e
DE
64 expressionS exp;
65};
66
0f829c8e 67static struct fixup fixups [GAS_CGEN_MAX_FIXUPS];
defc70bf 68static int num_fixups;
841eff9e 69
f3f00e94
DE
70/* Prepare to parse an instruction.
71 ??? May wish to make this static and delete calls in md_assemble. */
72
841eff9e 73void
0f829c8e 74gas_cgen_init_parse ()
841eff9e
DE
75{
76 num_fixups = 0;
77}
78
79/* Queue a fixup. */
80
95effc2b 81static void
0f829c8e 82queue_fixup (opindex, opinfo, expP)
ebde3f62
NC
83 int opindex;
84 expressionS * expP;
841eff9e
DE
85{
86 /* We need to generate a fixup for this expression. */
0f829c8e 87 if (num_fixups >= GAS_CGEN_MAX_FIXUPS)
48401fcf 88 as_fatal (_("too many fixups"));
ebde3f62 89 fixups[num_fixups].exp = * expP;
841eff9e 90 fixups[num_fixups].opindex = opindex;
ebde3f62
NC
91 fixups[num_fixups].opinfo = opinfo;
92 ++ num_fixups;
841eff9e
DE
93}
94
95effc2b
DE
95/* The following three functions allow a backup of the fixup chain to be made,
96 and to have this backup be swapped with the current chain. This allows
97 certain ports, eg the m32r, to swap two instructions and swap their fixups
98 at the same time. */
0f829c8e
CM
99/* ??? I think with cgen_asm_finish_insn (or something else) there is no
100 more need for this. */
101
102static struct fixup saved_fixups [GAS_CGEN_MAX_FIXUPS];
defc70bf 103static int saved_num_fixups;
95effc2b
DE
104
105void
0f829c8e 106gas_cgen_save_fixups ()
95effc2b
DE
107{
108 saved_num_fixups = num_fixups;
109
110 memcpy (saved_fixups, fixups, sizeof (fixups[0]) * num_fixups);
111
112 num_fixups = 0;
113}
114
115void
0f829c8e 116gas_cgen_restore_fixups ()
95effc2b
DE
117{
118 num_fixups = saved_num_fixups;
119
120 memcpy (fixups, saved_fixups, sizeof (fixups[0]) * num_fixups);
121
122 saved_num_fixups = 0;
123}
124
125void
0f829c8e 126gas_cgen_swap_fixups ()
95effc2b 127{
defc70bf 128 int tmp;
95effc2b
DE
129 struct fixup tmp_fixup;
130
131 if (num_fixups == 0)
132 {
0f829c8e 133 gas_cgen_restore_fixups ();
95effc2b
DE
134 }
135 else if (saved_num_fixups == 0)
136 {
0f829c8e 137 gas_cgen_save_fixups ();
95effc2b
DE
138 }
139 else
140 {
141 tmp = saved_num_fixups;
142 saved_num_fixups = num_fixups;
143 num_fixups = tmp;
144
0f829c8e 145 for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;)
95effc2b
DE
146 {
147 tmp_fixup = saved_fixups [tmp];
148 saved_fixups [tmp] = fixups [tmp];
149 fixups [tmp] = tmp_fixup;
150 }
151 }
152}
153
841eff9e
DE
154/* Default routine to record a fixup.
155 This is a cover function to fix_new.
156 It exists because we record INSN with the fixup.
157
158 FRAG and WHERE are their respective arguments to fix_new_exp.
159 LENGTH is in bits.
160 OPINFO is something the caller chooses to help in reloc determination.
161
162 At this point we do not use a bfd_reloc_code_real_type for
163 operands residing in the insn, but instead just use the
164 operand index. This lets us easily handle fixups for any
165 operand type. We pick a BFD reloc type in md_apply_fix. */
166
167fixS *
0f829c8e 168gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
ebde3f62
NC
169 fragS * frag;
170 int where;
171 const CGEN_INSN * insn;
172 int length;
173 const CGEN_OPERAND * operand;
174 int opinfo;
175 symbolS * symbol;
176 offsetT offset;
841eff9e 177{
ebde3f62 178 fixS * fixP;
841eff9e
DE
179
180 /* It may seem strange to use operand->attrs and not insn->attrs here,
181 but it is the operand that has a pc relative relocation. */
182
183 fixP = fix_new (frag, where, length / 8, symbol, offset,
310addc4
DE
184 CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
185 (bfd_reloc_code_real_type)
186 ((int) BFD_RELOC_UNUSED
187 + CGEN_OPERAND_INDEX (gas_cgen_cpu_desc, operand)));
ebde3f62 188 fixP->tc_fix_data.insn = (PTR) insn;
841eff9e
DE
189 fixP->tc_fix_data.opinfo = opinfo;
190
191 return fixP;
192}
193
194/* Default routine to record a fixup given an expression.
195 This is a cover function to fix_new_exp.
196 It exists because we record INSN with the fixup.
197
198 FRAG and WHERE are their respective arguments to fix_new_exp.
199 LENGTH is in bits.
200 OPINFO is something the caller chooses to help in reloc determination.
201
202 At this point we do not use a bfd_reloc_code_real_type for
203 operands residing in the insn, but instead just use the
204 operand index. This lets us easily handle fixups for any
205 operand type. We pick a BFD reloc type in md_apply_fix. */
206
207fixS *
0f829c8e 208gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
ebde3f62
NC
209 fragS * frag;
210 int where;
211 const CGEN_INSN * insn;
212 int length;
213 const CGEN_OPERAND * operand;
214 int opinfo;
215 expressionS * exp;
841eff9e 216{
ebde3f62 217 fixS * fixP;
841eff9e
DE
218
219 /* It may seem strange to use operand->attrs and not insn->attrs here,
220 but it is the operand that has a pc relative relocation. */
221
222 fixP = fix_new_exp (frag, where, length / 8, exp,
310addc4
DE
223 CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
224 (bfd_reloc_code_real_type)
225 ((int) BFD_RELOC_UNUSED
226 + CGEN_OPERAND_INDEX (gas_cgen_cpu_desc, operand)));
841eff9e
DE
227 fixP->tc_fix_data.insn = (PTR) insn;
228 fixP->tc_fix_data.opinfo = opinfo;
229
230 return fixP;
231}
232
1002d8ed
DE
233/* Used for communication between the next two procedures. */
234static jmp_buf expr_jmp_buf;
235
841eff9e
DE
236/* Callback for cgen interface. Parse the expression at *STRP.
237 The result is an error message or NULL for success (in which case
238 *STRP is advanced past the parsed text).
f3f00e94
DE
239 WANT is an indication of what the caller is looking for.
240 If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
241 a table entry with the insn, reset the queued fixups counter.
242 An enum cgen_parse_operand_result is stored in RESULTP.
243 OPINDEX is the operand's table entry index.
841eff9e
DE
244 OPINFO is something the caller chooses to help in reloc determination.
245 The resulting value is stored in VALUEP. */
246
247const char *
310addc4
DE
248gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP)
249 CGEN_CPU_DESC cd;
defc70bf
DE
250 enum cgen_parse_operand_type want;
251 const char ** strP;
252 int opindex;
253 int opinfo;
ebde3f62 254 enum cgen_parse_operand_result * resultP;
defc70bf 255 bfd_vma * valueP;
841eff9e 256{
1002d8ed 257#ifdef __STDC__
defc70bf
DE
258 /* These are volatile to survive the setjmp. */
259 char * volatile hold;
1002d8ed
DE
260 enum cgen_parse_operand_result * volatile resultP_1;
261#else
defc70bf
DE
262 static char * hold;
263 static enum cgen_parse_operand_result * resultP_1;
1002d8ed 264#endif
defc70bf
DE
265 const char * errmsg = NULL;
266 expressionS exp;
841eff9e 267
f3f00e94
DE
268 if (want == CGEN_PARSE_OPERAND_INIT)
269 {
0f829c8e 270 gas_cgen_init_parse ();
f3f00e94
DE
271 return NULL;
272 }
273
1002d8ed 274 resultP_1 = resultP;
841eff9e 275 hold = input_line_pointer;
ebde3f62 276 input_line_pointer = (char *) * strP;
1002d8ed
DE
277
278 /* We rely on md_operand to longjmp back to us.
0f829c8e 279 This is done via gas_cgen_md_operand. */
1002d8ed
DE
280 if (setjmp (expr_jmp_buf) != 0)
281 {
282 input_line_pointer = (char *) hold;
ebde3f62 283 * resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
1002d8ed
DE
284 return "illegal operand";
285 }
286
ebde3f62 287 expression (& exp);
1002d8ed 288
ebde3f62 289 * strP = input_line_pointer;
841eff9e
DE
290 input_line_pointer = hold;
291
f3f00e94
DE
292 /* FIXME: Need to check `want'. */
293
841eff9e
DE
294 switch (exp.X_op)
295 {
296 case O_illegal :
48401fcf 297 errmsg = _("illegal operand");
ebde3f62 298 * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
841eff9e
DE
299 break;
300 case O_absent :
48401fcf 301 errmsg = _("missing operand");
ebde3f62 302 * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
841eff9e
DE
303 break;
304 case O_constant :
ebde3f62
NC
305 * valueP = exp.X_add_number;
306 * resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
841eff9e
DE
307 break;
308 case O_register :
ebde3f62
NC
309 * valueP = exp.X_add_number;
310 * resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
841eff9e
DE
311 break;
312 default :
0f829c8e 313 queue_fixup (opindex, opinfo, & exp);
ebde3f62
NC
314 * valueP = 0;
315 * resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
841eff9e
DE
316 break;
317 }
318
319 return errmsg;
320}
321
1002d8ed
DE
322/* md_operand handler to catch unrecognized expressions and halt the
323 parsing process so the next entry can be tried.
324
325 ??? This could be done differently by adding code to `expression'. */
326
327void
0f829c8e 328gas_cgen_md_operand (expressionP)
ebde3f62 329 expressionS * expressionP;
1002d8ed
DE
330{
331 longjmp (expr_jmp_buf, 1);
332}
333
841eff9e
DE
334/* Finish assembling instruction INSN.
335 BUF contains what we've built up so far.
95effc2b 336 LENGTH is the size of the insn in bits.
defc70bf
DE
337 RELAX_P is non-zero if relaxable insns should be emitted as such.
338 Otherwise they're emitted in non-relaxable forms.
b817384c 339 The "result" is stored in RESULT if non-NULL. */
841eff9e 340
defc70bf 341void
0f829c8e 342gas_cgen_finish_insn (insn, buf, length, relax_p, result)
ebde3f62 343 const CGEN_INSN * insn;
310addc4 344 CGEN_INSN_BYTES_PTR buf;
defc70bf
DE
345 unsigned int length;
346 int relax_p;
b70d5374 347 finished_insnS * result;
841eff9e 348{
defc70bf
DE
349 int i;
350 int relax_operand;
351 char * f;
841eff9e
DE
352 unsigned int byte_len = length / 8;
353
354 /* ??? Target foo issues various warnings here, so one might want to provide
355 a hook here. However, our caller is defined in tc-foo.c so there
356 shouldn't be a need for a hook. */
310addc4 357
841eff9e
DE
358 /* Write out the instruction.
359 It is important to fetch enough space in one call to `frag_more'.
360 We use (f - frag_now->fr_literal) to compute where we are and we
361 don't want frag_now to change between calls.
362
363 Relaxable instructions: We need to ensure we allocate enough
364 space for the largest insn. */
365
310addc4 366 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX))
841eff9e
DE
367 abort (); /* These currently shouldn't get here. */
368
369 /* Is there a relaxable insn with the relaxable operand needing a fixup? */
370
371 relax_operand = -1;
310addc4 372 if (relax_p && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE))
841eff9e
DE
373 {
374 /* Scan the fixups for the operand affected by relaxing
375 (i.e. the branch address). */
376
ebde3f62 377 for (i = 0; i < num_fixups; ++ i)
841eff9e 378 {
310addc4
DE
379 if (CGEN_OPERAND_ATTR_VALUE (& CGEN_CPU_OPERAND_TABLE (gas_cgen_cpu_desc) [fixups[i].opindex],
380 CGEN_OPERAND_RELAX))
841eff9e
DE
381 {
382 relax_operand = i;
383 break;
384 }
385 }
386 }
387
388 if (relax_operand != -1)
389 {
defc70bf 390 int max_len;
ebde3f62 391 fragS * old_frag;
841eff9e
DE
392
393#ifdef TC_CGEN_MAX_RELAX
394 max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
395#else
396 max_len = CGEN_MAX_INSN_SIZE;
397#endif
398 /* Ensure variable part and fixed part are in same fragment. */
399 /* FIXME: Having to do this seems like a hack. */
400 frag_grow (max_len);
310addc4 401
841eff9e
DE
402 /* Allocate space for the fixed part. */
403 f = frag_more (byte_len);
310addc4 404
841eff9e
DE
405 /* Create a relaxable fragment for this instruction. */
406 old_frag = frag_now;
95effc2b 407
841eff9e
DE
408 frag_var (rs_machine_dependent,
409 max_len - byte_len /* max chars */,
410 0 /* variable part already allocated */,
411 /* FIXME: When we machine generate the relax table,
412 machine generate a macro to compute subtype. */
413 1 /* subtype */,
414 fixups[relax_operand].exp.X_add_symbol,
415 fixups[relax_operand].exp.X_add_number,
416 f);
310addc4 417
841eff9e 418 /* Record the operand number with the fragment so md_convert_frag
0f829c8e 419 can use gas_cgen_md_record_fixup to record the appropriate reloc. */
ebde3f62 420 old_frag->fr_cgen.insn = insn;
1002d8ed 421 old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
ebde3f62 422 old_frag->fr_cgen.opinfo = fixups[relax_operand].opinfo;
defc70bf
DE
423 if (result)
424 result->frag = old_frag;
841eff9e
DE
425 }
426 else
defc70bf
DE
427 {
428 f = frag_more (byte_len);
429 if (result)
430 result->frag = frag_now;
431 }
841eff9e
DE
432
433 /* If we're recording insns as numbers (rather than a string of bytes),
434 target byte order handling is deferred until now. */
310addc4
DE
435#if CGEN_INT_INSN_P
436 cgen_put_insn_value (gas_cgen_cpu_desc, f, length, *buf);
841eff9e
DE
437#else
438 memcpy (f, buf, byte_len);
439#endif
440
441 /* Create any fixups. */
442 for (i = 0; i < num_fixups; ++i)
443 {
defc70bf
DE
444 fixS * fixP;
445
841eff9e
DE
446 /* Don't create fixups for these. That's done during relaxation.
447 We don't need to test for CGEN_INSN_RELAX as they can't get here
448 (see above). */
defc70bf 449 if (relax_p
310addc4
DE
450 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE)
451 && CGEN_OPERAND_ATTR_VALUE (& CGEN_CPU_OPERAND_TABLE (gas_cgen_cpu_desc) [fixups[i].opindex],
452 CGEN_OPERAND_RELAX))
841eff9e
DE
453 continue;
454
455#ifndef md_cgen_record_fixup_exp
0f829c8e 456#define md_cgen_record_fixup_exp gas_cgen_record_fixup_exp
841eff9e
DE
457#endif
458
defc70bf
DE
459 fixP = md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
460 insn, length,
310addc4 461 & CGEN_CPU_OPERAND_TABLE (gas_cgen_cpu_desc) [fixups[i].opindex],
defc70bf
DE
462 fixups[i].opinfo,
463 & fixups[i].exp);
464 if (result)
465 result->fixups[i] = fixP;
841eff9e 466 }
95effc2b 467
defc70bf
DE
468 if (result)
469 {
470 result->num_fixups = num_fixups;
471 result->addr = f;
472 }
841eff9e
DE
473}
474
475/* Apply a fixup to the object code. This is called for all the
476 fixups we generated by the call to fix_new_exp, above. In the call
477 above we used a reloc code which was the largest legal reloc code
478 plus the operand index. Here we undo that to recover the operand
479 index. At this point all symbol values should be fully resolved,
480 and we attempt to completely resolve the reloc. If we can not do
481 that, we determine the correct reloc code and put it back in the fixup. */
482
483/* FIXME: This function handles some of the fixups and bfd_install_relocation
484 handles the rest. bfd_install_relocation (or some other bfd function)
485 should handle them all. */
486
487int
0f829c8e 488gas_cgen_md_apply_fix3 (fixP, valueP, seg)
ebde3f62
NC
489 fixS * fixP;
490 valueT * valueP;
491 segT seg;
841eff9e 492{
48401fcf
TT
493 char * where = fixP->fx_frag->fr_literal + fixP->fx_where;
494 valueT value;
310addc4
DE
495 /* canonical name, since used a lot */
496 CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
497
841eff9e
DE
498 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
499 the symbol values. Since we are using BFD_ASSEMBLER, if we are
500 doing this relocation the code in write.c is going to call
501 bfd_install_relocation, which is also going to use the symbol
502 value. That means that if the reloc is fully resolved we want to
503 use *valuep since bfd_install_relocation is not being used.
504 However, if the reloc is not fully resolved we do not want to use
505 *valuep, and must use fx_offset instead. However, if the reloc
506 is PC relative, we do want to use *valuep since it includes the
507 result of md_pcrel_from. This is confusing. */
508
509 if (fixP->fx_addsy == (symbolS *) NULL)
510 {
ebde3f62 511 value = * valueP;
841eff9e
DE
512 fixP->fx_done = 1;
513 }
514 else if (fixP->fx_pcrel)
ebde3f62 515 value = * valueP;
841eff9e
DE
516 else
517 {
518 value = fixP->fx_offset;
519 if (fixP->fx_subsy != (symbolS *) NULL)
520 {
521 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
522 value -= S_GET_VALUE (fixP->fx_subsy);
523 else
524 {
525 /* We don't actually support subtracting a symbol. */
526 as_bad_where (fixP->fx_file, fixP->fx_line,
48401fcf 527 _("expression too complex"));
841eff9e
DE
528 }
529 }
530 }
531
532 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
533 {
310addc4
DE
534 int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
535 const CGEN_OPERAND *operand = & CGEN_CPU_OPERAND_TABLE (cd) [opindex];
536 const char *errmsg;
841eff9e 537 bfd_reloc_code_real_type reloc_type;
310addc4
DE
538 CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
539 const CGEN_INSN *insn = (CGEN_INSN *) fixP->tc_fix_data.insn;
0f829c8e 540
841eff9e
DE
541 /* If the reloc has been fully resolved finish the operand here. */
542 /* FIXME: This duplicates the capabilities of code in BFD. */
543 if (fixP->fx_done
544 /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
545 finish the job. Testing for pcrel is a temporary hack. */
546 || fixP->fx_pcrel)
547 {
310addc4
DE
548 CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
549 CGEN_CPU_SET_VMA_OPERAND (cd) (opindex, fields, (bfd_vma) value);
550
551#if CGEN_INT_INSN_P
552 {
553 CGEN_INSN_INT insn_value =
554 cgen_get_insn_value (cd, where, CGEN_INSN_BITSIZE (insn));
555
556 /* ??? 0 is passed for `pc' */
557 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
558 &insn_value, (bfd_vma) 0);
559 cgen_put_insn_value (cd, where, CGEN_INSN_BITSIZE (insn),
560 insn_value);
561 }
562#else
563 /* ??? 0 is passed for `pc' */
564 errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields, where,
565 (bfd_vma) 0);
566#endif
841eff9e 567 if (errmsg)
310addc4 568 as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
841eff9e
DE
569 }
570
571 if (fixP->fx_done)
572 return 1;
573
574 /* The operand isn't fully resolved. Determine a BFD reloc value
575 based on the operand information and leave it to
576 bfd_install_relocation. Note that this doesn't work when
577 partial_inplace == false. */
578
0f829c8e 579 reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
841eff9e
DE
580 if (reloc_type != BFD_RELOC_NONE)
581 {
582 fixP->fx_r_type = reloc_type;
583 }
584 else
585 {
586 as_bad_where (fixP->fx_file, fixP->fx_line,
48401fcf 587 _("unresolved expression that must be resolved"));
841eff9e
DE
588 fixP->fx_done = 1;
589 return 1;
590 }
591 }
592 else if (fixP->fx_done)
593 {
594 /* We're finished with this fixup. Install it because
595 bfd_install_relocation won't be called to do it. */
596 switch (fixP->fx_r_type)
597 {
598 case BFD_RELOC_8:
599 md_number_to_chars (where, value, 1);
600 break;
601 case BFD_RELOC_16:
602 md_number_to_chars (where, value, 2);
603 break;
604 case BFD_RELOC_32:
605 md_number_to_chars (where, value, 4);
606 break;
607 /* FIXME: later add support for 64 bits. */
608 default:
310addc4
DE
609 as_bad_where (fixP->fx_file, fixP->fx_line,
610 _("internal error: can't install fix for reloc type %d (`%s')"),
611 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
612 break;
841eff9e
DE
613 }
614 }
615 else
616 {
617 /* bfd_install_relocation will be called to finish things up. */
618 }
619
620 /* Tuck `value' away for use by tc_gen_reloc.
621 See the comment describing fx_addnumber in write.h.
622 This field is misnamed (or misused :-). */
623 fixP->fx_addnumber = value;
624
625 return 1;
626}
627
628/* Translate internal representation of relocation info to BFD target format.
629
630 FIXME: To what extent can we get all relevant targets to use this? */
631
632arelent *
0f829c8e 633gas_cgen_tc_gen_reloc (section, fixP)
ebde3f62
NC
634 asection * section;
635 fixS * fixP;
841eff9e 636{
ebde3f62 637 arelent * reloc;
841eff9e 638
0f829c8e 639 reloc = (arelent *) xmalloc (sizeof (arelent));
841eff9e
DE
640
641 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
642 if (reloc->howto == (reloc_howto_type *) NULL)
643 {
644 as_bad_where (fixP->fx_file, fixP->fx_line,
48401fcf 645 _("internal error: can't export reloc type %d (`%s')"),
841eff9e
DE
646 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
647 return NULL;
648 }
649
650 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
651
ebde3f62 652 reloc->sym_ptr_ptr = & fixP->fx_addsy->bsym;
841eff9e 653
0f829c8e 654 /* Use fx_offset for these cases */
310addc4 655 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
0f829c8e 656 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
310addc4 657 reloc->addend = fixP->fx_offset;
0f829c8e 658 else
310addc4
DE
659 reloc->addend = fixP->fx_addnumber;
660
661 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
841eff9e
DE
662 return reloc;
663}