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