This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gas / cgen.c
1 /* GAS interface for targets using CGEN: Cpu tools GENerator.
2    Copyright (C) 1996, 1997, 1998 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 "bfd.h"
23 #include "cgen-opc.h"
24 #include "as.h"
25 #include "subsegs.h"
26
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.  */
30
31 void
32 cgen_asm_record_register (name, number)
33      char * name;
34      int    number;
35 {
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));
40 }
41
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 ``.''.
45
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
48    being is preferable.
49
50    OPINDEX is the index in the operand table.
51    OPINFO is something the caller chooses to help in reloc determination.  */
52
53 struct fixup
54 {
55   int         opindex;
56   int         opinfo;
57   expressionS exp;
58 };
59
60 #define MAX_FIXUPS 5
61
62 static struct fixup fixups [MAX_FIXUPS];
63 static int          num_fixups;
64
65 /* Prepare to parse an instruction.
66    ??? May wish to make this static and delete calls in md_assemble.  */
67
68 void
69 cgen_asm_init_parse ()
70 {
71   num_fixups = 0;
72 }
73
74 /* Queue a fixup.  */
75
76 static void
77 cgen_queue_fixup (opindex, opinfo, expP)
78      int           opindex;
79      expressionS * expP;
80 {
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;
87   ++ num_fixups;
88 }
89
90 /* The following three functions allow a backup of the fixup chain to be made,
91    and to have this backup be swapped with the current chain.  This allows
92    certain ports, eg the m32r, to swap two instructions and swap their fixups
93    at the same time.  */
94 static struct fixup saved_fixups [MAX_FIXUPS];
95 static int          saved_num_fixups;
96
97 void
98 cgen_save_fixups ()
99 {
100   saved_num_fixups = num_fixups;
101   
102   memcpy (saved_fixups, fixups, sizeof (fixups[0]) * num_fixups);
103
104   num_fixups = 0;
105 }
106
107 void
108 cgen_restore_fixups ()
109 {
110   num_fixups = saved_num_fixups;
111   
112   memcpy (fixups, saved_fixups, sizeof (fixups[0]) * num_fixups);
113
114   saved_num_fixups = 0;
115 }
116
117 void
118 cgen_swap_fixups ()
119 {
120   int          tmp;
121   struct fixup tmp_fixup;
122
123   if (num_fixups == 0)
124     {
125       cgen_restore_fixups ();
126     }
127   else if (saved_num_fixups == 0)
128     {
129       cgen_save_fixups ();
130     }
131   else
132     {
133       tmp = saved_num_fixups;
134       saved_num_fixups = num_fixups;
135       num_fixups = tmp;
136       
137       for (tmp = MAX_FIXUPS; tmp--;)
138         {
139           tmp_fixup          = saved_fixups [tmp];
140           saved_fixups [tmp] = fixups [tmp];
141           fixups [tmp]       = tmp_fixup;
142         }
143     }
144 }
145
146 /* Default routine to record a fixup.
147    This is a cover function to fix_new.
148    It exists because we record INSN with the fixup.
149
150    FRAG and WHERE are their respective arguments to fix_new_exp.
151    LENGTH is in bits.
152    OPINFO is something the caller chooses to help in reloc determination.
153
154    At this point we do not use a bfd_reloc_code_real_type for
155    operands residing in the insn, but instead just use the
156    operand index.  This lets us easily handle fixups for any
157    operand type.  We pick a BFD reloc type in md_apply_fix.  */
158
159 fixS *
160 cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
161      fragS *              frag;
162      int                  where;
163      const CGEN_INSN *    insn;
164      int                  length;
165      const CGEN_OPERAND * operand;
166      int                  opinfo;
167      symbolS *            symbol;
168      offsetT              offset;
169 {
170   fixS * fixP;
171
172   /* It may seem strange to use operand->attrs and not insn->attrs here,
173      but it is the operand that has a pc relative relocation.  */
174
175   fixP = fix_new (frag, where, length / 8, symbol, offset,
176                   CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
177                   (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
178   fixP->tc_fix_data.insn   = (PTR) insn;
179   fixP->tc_fix_data.opinfo = opinfo;
180
181   return fixP;
182 }
183
184 /* Default routine to record a fixup given an expression.
185    This is a cover function to fix_new_exp.
186    It exists because we record INSN with the fixup.
187
188    FRAG and WHERE are their respective arguments to fix_new_exp.
189    LENGTH is in bits.
190    OPINFO is something the caller chooses to help in reloc determination.
191
192    At this point we do not use a bfd_reloc_code_real_type for
193    operands residing in the insn, but instead just use the
194    operand index.  This lets us easily handle fixups for any
195    operand type.  We pick a BFD reloc type in md_apply_fix.  */
196
197 fixS *
198 cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
199      fragS *              frag;
200      int                  where;
201      const CGEN_INSN *    insn;
202      int                  length;
203      const CGEN_OPERAND * operand;
204      int                  opinfo;
205      expressionS *        exp;
206 {
207   fixS * fixP;
208
209   /* It may seem strange to use operand->attrs and not insn->attrs here,
210      but it is the operand that has a pc relative relocation.  */
211
212   fixP = fix_new_exp (frag, where, length / 8, exp,
213                       CGEN_OPERAND_ATTR (operand, CGEN_OPERAND_PCREL_ADDR) != 0,
214                       (bfd_reloc_code_real_type) ((int) BFD_RELOC_UNUSED + CGEN_OPERAND_INDEX (operand)));
215   fixP->tc_fix_data.insn = (PTR) insn;
216   fixP->tc_fix_data.opinfo = opinfo;
217
218   return fixP;
219 }
220
221 /* Used for communication between the next two procedures.  */
222 static jmp_buf expr_jmp_buf;
223
224 /* Callback for cgen interface.  Parse the expression at *STRP.
225    The result is an error message or NULL for success (in which case
226    *STRP is advanced past the parsed text).
227    WANT is an indication of what the caller is looking for.
228    If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
229    a table entry with the insn, reset the queued fixups counter.
230    An enum cgen_parse_operand_result is stored in RESULTP.
231    OPINDEX is the operand's table entry index.
232    OPINFO is something the caller chooses to help in reloc determination.
233    The resulting value is stored in VALUEP.  */
234
235 const char *
236 cgen_parse_operand (want, strP, opindex, opinfo, resultP, valueP)
237      enum cgen_parse_operand_type     want;
238      const char **                    strP;
239      int                              opindex;
240      int                              opinfo;
241      enum cgen_parse_operand_result * resultP;
242      bfd_vma *                        valueP;
243 {
244 #ifdef __STDC__
245   /* These is volatile to survive the setjmp.  */
246   char * volatile                           hold;
247   enum cgen_parse_operand_result * volatile resultP_1;
248 #else
249   static char *                             hold;
250   static enum cgen_parse_operand_result *   resultP_1;
251 #endif
252   const char *                              errmsg = NULL;
253   expressionS                               exp;
254
255   if (want == CGEN_PARSE_OPERAND_INIT)
256     {
257       cgen_asm_init_parse ();
258       return NULL;
259     }
260
261   resultP_1 = resultP;
262   hold = input_line_pointer;
263   input_line_pointer = (char *) * strP;
264
265   /* We rely on md_operand to longjmp back to us.
266      This is done via cgen_md_operand.  */
267   if (setjmp (expr_jmp_buf) != 0)
268     {
269       input_line_pointer = (char *) hold;
270       * resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
271       return "illegal operand";
272     }
273
274   expression (& exp);
275
276   * strP = input_line_pointer;
277   input_line_pointer = hold;
278
279   /* FIXME: Need to check `want'.  */
280
281   switch (exp.X_op)
282     {
283     case O_illegal :
284       errmsg = "illegal operand";
285       * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
286       break;
287     case O_absent :
288       errmsg = "missing operand";
289       * resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
290       break;
291     case O_constant :
292       * valueP = exp.X_add_number;
293       * resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
294       break;
295     case O_register :
296       * valueP = exp.X_add_number;
297       * resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
298       break;
299     default :
300       cgen_queue_fixup (opindex, opinfo, & exp);
301       * valueP = 0;
302       * resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
303       break;
304     }
305
306   return errmsg;
307 }
308
309 /* md_operand handler to catch unrecognized expressions and halt the
310    parsing process so the next entry can be tried.
311
312    ??? This could be done differently by adding code to `expression'.  */
313
314 void
315 cgen_md_operand (expressionP)
316      expressionS * expressionP;
317 {
318   longjmp (expr_jmp_buf, 1);
319 }
320
321 /* Finish assembling instruction INSN.
322    BUF contains what we've built up so far.
323    LENGTH is the size of the insn in bits.
324    Returns the address of the buffer containing the assembled instruction,
325    in case the caller needs to modify it for some reason. */
326
327 char *
328 cgen_asm_finish_insn (insn, buf, length)
329      const CGEN_INSN * insn;
330      cgen_insn_t *     buf;
331      unsigned int      length;
332 {
333   int          i;
334   int          relax_operand;
335   char *       f;
336   unsigned int byte_len = length / 8;
337
338   /* ??? Target foo issues various warnings here, so one might want to provide
339      a hook here.  However, our caller is defined in tc-foo.c so there
340      shouldn't be a need for a hook.  */
341   
342   /* Write out the instruction.
343      It is important to fetch enough space in one call to `frag_more'.
344      We use (f - frag_now->fr_literal) to compute where we are and we
345      don't want frag_now to change between calls.
346
347      Relaxable instructions: We need to ensure we allocate enough
348      space for the largest insn.  */
349
350   if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
351     abort (); /* These currently shouldn't get here.  */
352
353   /* Is there a relaxable insn with the relaxable operand needing a fixup?  */
354
355   relax_operand = -1;
356   if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0)
357     {
358       /* Scan the fixups for the operand affected by relaxing
359          (i.e. the branch address).  */
360
361       for (i = 0; i < num_fixups; ++ i)
362         {
363           if (CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
364                                  CGEN_OPERAND_RELAX) != 0)
365             {
366               relax_operand = i;
367               break;
368             }
369         }
370     }
371
372   if (relax_operand != -1)
373     {
374       int     max_len;
375       fragS * old_frag;
376
377 #ifdef TC_CGEN_MAX_RELAX
378       max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
379 #else
380       max_len = CGEN_MAX_INSN_SIZE;
381 #endif
382       /* Ensure variable part and fixed part are in same fragment.  */
383       /* FIXME: Having to do this seems like a hack.  */
384       frag_grow (max_len);
385       
386       /* Allocate space for the fixed part.  */
387       f = frag_more (byte_len);
388       
389       /* Create a relaxable fragment for this instruction.  */
390       old_frag = frag_now;
391
392       frag_var (rs_machine_dependent,
393                 max_len - byte_len /* max chars */,
394                 0 /* variable part already allocated */,
395                 /* FIXME: When we machine generate the relax table,
396                    machine generate a macro to compute subtype.  */
397                 1 /* subtype */,
398                 fixups[relax_operand].exp.X_add_symbol,
399                 fixups[relax_operand].exp.X_add_number,
400                 f);
401       
402       /* Record the operand number with the fragment so md_convert_frag
403          can use cgen_md_record_fixup to record the appropriate reloc.  */
404       old_frag->fr_cgen.insn    = insn;
405       old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
406       old_frag->fr_cgen.opinfo  = fixups[relax_operand].opinfo;
407     }
408   else
409     f = frag_more (byte_len);
410
411   /* If we're recording insns as numbers (rather than a string of bytes),
412      target byte order handling is deferred until now.  */
413 #if 0 /*def CGEN_INT_INSN*/
414   switch (length)
415     {
416     case 16:
417       if (cgen_big_endian_p)
418         bfd_putb16 ((bfd_vma) * buf, f);
419       else
420         bfd_putl16 ((bfd_vma) * buf, f);
421       break;
422     case 32:
423       if (cgen_big_endian_p)
424         bfd_putb32 ((bfd_vma) * buf, f);
425       else
426         bfd_putl32 ((bfd_vma) * buf, f);
427       break;
428     default:
429       abort ();
430     }
431 #else
432   memcpy (f, buf, byte_len);
433 #endif
434
435   /* Create any fixups.  */
436   for (i = 0; i < num_fixups; ++i)
437     {
438       /* Don't create fixups for these.  That's done during relaxation.
439          We don't need to test for CGEN_INSN_RELAX as they can't get here
440          (see above).  */
441       if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAXABLE) != 0
442           && CGEN_OPERAND_ATTR (& CGEN_SYM (operand_table) [fixups[i].opindex],
443                                 CGEN_OPERAND_RELAX) != 0)
444         continue;
445
446 #ifndef md_cgen_record_fixup_exp
447 #define md_cgen_record_fixup_exp cgen_record_fixup_exp
448 #endif
449
450       md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
451                                 insn, length,
452                                 & CGEN_SYM (operand_table) [fixups[i].opindex],
453                                 fixups[i].opinfo,
454                                 & fixups[i].exp);
455     }
456
457   return f;
458 }
459
460 /* Apply a fixup to the object code.  This is called for all the
461    fixups we generated by the call to fix_new_exp, above.  In the call
462    above we used a reloc code which was the largest legal reloc code
463    plus the operand index.  Here we undo that to recover the operand
464    index.  At this point all symbol values should be fully resolved,
465    and we attempt to completely resolve the reloc.  If we can not do
466    that, we determine the correct reloc code and put it back in the fixup.  */
467
468 /* FIXME: This function handles some of the fixups and bfd_install_relocation
469    handles the rest.  bfd_install_relocation (or some other bfd function)
470    should handle them all.  */
471
472 int
473 cgen_md_apply_fix3 (fixP, valueP, seg)
474      fixS *   fixP;
475      valueT * valueP;
476      segT     seg;
477 {
478   char *      where = fixP->fx_frag->fr_literal + fixP->fx_where;
479   valueT      value;
480
481   /* FIXME FIXME FIXME: The value we are passed in *valuep includes
482      the symbol values.  Since we are using BFD_ASSEMBLER, if we are
483      doing this relocation the code in write.c is going to call
484      bfd_install_relocation, which is also going to use the symbol
485      value.  That means that if the reloc is fully resolved we want to
486      use *valuep since bfd_install_relocation is not being used.
487      However, if the reloc is not fully resolved we do not want to use
488      *valuep, and must use fx_offset instead.  However, if the reloc
489      is PC relative, we do want to use *valuep since it includes the
490      result of md_pcrel_from.  This is confusing.  */
491
492   if (fixP->fx_addsy == (symbolS *) NULL)
493     {
494       value = * valueP;
495       fixP->fx_done = 1;
496     }
497   else if (fixP->fx_pcrel)
498     value = * valueP;
499   else
500     {
501       value = fixP->fx_offset;
502       if (fixP->fx_subsy != (symbolS *) NULL)
503         {
504           if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
505             value -= S_GET_VALUE (fixP->fx_subsy);
506           else
507             {
508               /* We don't actually support subtracting a symbol.  */
509               as_bad_where (fixP->fx_file, fixP->fx_line,
510                             "expression too complex");
511             }
512         }
513     }
514
515   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
516     {
517       int                      opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
518       const CGEN_OPERAND *     operand = & CGEN_SYM (operand_table) [opindex];
519       const char *             errmsg;
520       bfd_reloc_code_real_type reloc_type;
521       CGEN_FIELDS              fields;
522       const CGEN_INSN *        insn = (CGEN_INSN *) fixP->tc_fix_data.insn;
523
524       /* If the reloc has been fully resolved finish the operand here.  */
525       /* FIXME: This duplicates the capabilities of code in BFD.  */
526       if (fixP->fx_done
527           /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
528              finish the job.  Testing for pcrel is a temporary hack.  */
529           || fixP->fx_pcrel)
530         {
531           /* This may seem like overkill, and using bfd_install_relocation or
532              some such may be preferable, but this is simple.  */
533           CGEN_FIELDS_BITSIZE (& fields) = CGEN_INSN_BITSIZE (insn);
534           CGEN_SYM (set_operand) (opindex, & value, & fields);
535           errmsg = CGEN_SYM (validate_operand) (opindex, & fields);
536           if (errmsg)
537             as_warn_where (fixP->fx_file, fixP->fx_line, "%s\n", errmsg);
538           CGEN_SYM (insert_operand) (opindex, & fields, where);
539         }
540
541       if (fixP->fx_done)
542         return 1;
543
544       /* The operand isn't fully resolved.  Determine a BFD reloc value
545          based on the operand information and leave it to
546          bfd_install_relocation.  Note that this doesn't work when
547          partial_inplace == false.  */
548
549       reloc_type = CGEN_SYM (lookup_reloc) (insn, operand, fixP);
550       if (reloc_type != BFD_RELOC_NONE)
551         {
552           fixP->fx_r_type = reloc_type;
553         }
554       else
555         {
556           as_bad_where (fixP->fx_file, fixP->fx_line,
557                         "unresolved expression that must be resolved");
558           fixP->fx_done = 1;
559           return 1;
560         }
561     }
562   else if (fixP->fx_done)
563     {
564       /* We're finished with this fixup.  Install it because
565          bfd_install_relocation won't be called to do it.  */
566       switch (fixP->fx_r_type)
567         {
568         case BFD_RELOC_8:
569           md_number_to_chars (where, value, 1);
570           break;
571         case BFD_RELOC_16:
572           md_number_to_chars (where, value, 2);
573           break;
574         case BFD_RELOC_32:
575           md_number_to_chars (where, value, 4);
576           break;
577         /* FIXME: later add support for 64 bits.  */
578         default:
579           abort ();
580         }
581     }
582   else
583     {
584       /* bfd_install_relocation will be called to finish things up.  */
585     }
586
587   /* Tuck `value' away for use by tc_gen_reloc.
588      See the comment describing fx_addnumber in write.h.
589      This field is misnamed (or misused :-).  */
590   fixP->fx_addnumber = value;
591
592   return 1;
593 }
594
595 /* Translate internal representation of relocation info to BFD target format.
596
597    FIXME: To what extent can we get all relevant targets to use this?  */
598
599 arelent *
600 cgen_tc_gen_reloc (section, fixP)
601      asection * section;
602      fixS *     fixP;
603 {
604   arelent * reloc;
605
606   reloc = (arelent *) bfd_alloc (stdoutput, sizeof (arelent));
607
608   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
609   if (reloc->howto == (reloc_howto_type *) NULL)
610     {
611       as_bad_where (fixP->fx_file, fixP->fx_line,
612                     "internal error: can't export reloc type %d (`%s')",
613                     fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
614       return NULL;
615     }
616
617   assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
618
619   reloc->sym_ptr_ptr = & fixP->fx_addsy->bsym;
620   reloc->address     = fixP->fx_frag->fr_address + fixP->fx_where;
621   reloc->addend      = fixP->fx_addnumber;
622
623   return reloc;
624 }