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