OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gas/] [cgen.c] - Blame information for rev 853

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
/* GAS interface for targets using CGEN: Cpu tools GENerator.
2
   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3
   2006, 2007, 2009 Free Software Foundation, Inc.
4
 
5
   This file is part of GAS, the GNU Assembler.
6
 
7
   GAS is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3, or (at your option)
10
   any later version.
11
 
12
   GAS is distributed in the hope that it will be useful, but WITHOUT
13
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15
   License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with GAS; see the file COPYING.  If not, write to the Free Software
19
   Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
 
21
#include <setjmp.h>
22
#include "as.h"
23
#include "symcat.h"
24
#include "cgen-desc.h"
25
#include "subsegs.h"
26
#include "cgen.h"
27
#include "dwarf2dbg.h"
28
 
29
#include "symbols.h"
30
#include "struc-symbol.h"
31
 
32
#ifdef OBJ_COMPLEX_RELC
33
static expressionS * make_right_shifted_expr
34
  (expressionS *, const int, const int);
35
 
36
static unsigned long gas_cgen_encode_addend
37
  (const unsigned long, const unsigned long, const unsigned long, \
38
   const unsigned long, const unsigned long, const unsigned long, \
39
   const unsigned long);
40
 
41
static char * weak_operand_overflow_check
42
  (const expressionS *, const CGEN_OPERAND *);
43
 
44
static void queue_fixup_recursively
45
  (const int, const int, expressionS *, \
46
   const CGEN_MAYBE_MULTI_IFLD *, const int, const int);
47
 
48
static int rightshift = 0;
49
#endif
50
static void queue_fixup (int, int, expressionS *);
51
 
52
/* Opcode table descriptor, must be set by md_begin.  */
53
 
54
CGEN_CPU_DESC gas_cgen_cpu_desc;
55
 
56
/* Callback to insert a register into the symbol table.
57
   A target may choose to let GAS parse the registers.
58
   ??? Not currently used.  */
59
 
60
void
61
cgen_asm_record_register (name, number)
62
     char *name;
63
     int number;
64
{
65
  /* Use symbol_create here instead of symbol_new so we don't try to
66
     output registers into the object file's symbol table.  */
67
  symbol_table_insert (symbol_create (name, reg_section,
68
                                      number, &zero_address_frag));
69
}
70
 
71
/* We need to keep a list of fixups.  We can't simply generate them as
72
   we go, because that would require us to first create the frag, and
73
   that would screw up references to ``.''.
74
 
75
   This is used by cpu's with simple operands.  It keeps knowledge of what
76
   an `expressionS' is and what a `fixup' is out of CGEN which for the time
77
   being is preferable.
78
 
79
   OPINDEX is the index in the operand table.
80
   OPINFO is something the caller chooses to help in reloc determination.  */
81
 
82
struct fixup
83
{
84
  int opindex;
85
  int opinfo;
86
  expressionS exp;
87
  struct cgen_maybe_multi_ifield * field;
88
  int msb_field_p;
89
};
90
 
91
static struct fixup fixups[GAS_CGEN_MAX_FIXUPS];
92
static int num_fixups;
93
 
94
/* Prepare to parse an instruction.
95
   ??? May wish to make this static and delete calls in md_assemble.  */
96
 
97
void
98
gas_cgen_init_parse ()
99
{
100
  num_fixups = 0;
101
}
102
 
103
/* Queue a fixup.  */
104
 
105
static void
106
queue_fixup (opindex, opinfo, expP)
107
     int           opindex;
108
     int           opinfo;
109
     expressionS * expP;
110
{
111
  /* We need to generate a fixup for this expression.  */
112
  if (num_fixups >= GAS_CGEN_MAX_FIXUPS)
113
    as_fatal (_("too many fixups"));
114
  fixups[num_fixups].exp     = *expP;
115
  fixups[num_fixups].opindex = opindex;
116
  fixups[num_fixups].opinfo  = opinfo;
117
  ++ num_fixups;
118
}
119
 
120
/* The following functions allow fixup chains to be stored, retrieved,
121
   and swapped.  They are a generalization of a pre-existing scheme
122
   for storing, restoring and swapping fixup chains that was used by
123
   the m32r port.  The functionality is essentially the same, only
124
   instead of only being able to store a single fixup chain, an entire
125
   array of fixup chains can be stored.  It is the user's responsibility
126
   to keep track of how many fixup chains have been stored and which
127
   elements of the array they are in.
128
 
129
   The algorithms used are the same as in the old scheme.  Other than the
130
   "array-ness" of the whole thing, the functionality is identical to the
131
   old scheme.
132
 
133
   gas_cgen_initialize_saved_fixups_array():
134
      Sets num_fixups_in_chain to 0 for each element. Call this from
135
      md_begin() if you plan to use these functions and you want the
136
      fixup count in each element to be set to 0 initially.  This is
137
      not necessary, but it's included just in case.  It performs
138
      the same function for each element in the array of fixup chains
139
      that gas_init_parse() performs for the current fixups.
140
 
141
   gas_cgen_save_fixups (element):
142
      element - element number of the array you wish to store the fixups
143
                to.  No mechanism is built in for tracking what element
144
                was last stored to.
145
 
146
   gas_cgen_restore_fixups (element):
147
      element - element number of the array you wish to restore the fixups
148
                from.
149
 
150
   gas_cgen_swap_fixups(int element):
151
       element - swap the current fixups with those in this element number.
152
*/
153
 
154
struct saved_fixups
155
{
156
  struct fixup fixup_chain[GAS_CGEN_MAX_FIXUPS];
157
  int num_fixups_in_chain;
158
};
159
 
160
static struct saved_fixups stored_fixups[MAX_SAVED_FIXUP_CHAINS];
161
 
162
void
163
gas_cgen_initialize_saved_fixups_array ()
164
{
165
  int i = 0;
166
 
167
  while (i < MAX_SAVED_FIXUP_CHAINS)
168
    stored_fixups[i++].num_fixups_in_chain = 0;
169
}
170
 
171
void
172
gas_cgen_save_fixups (i)
173
     int i;
174
{
175
  if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
176
    {
177
      as_fatal ("index into stored_fixups[] out of bounds");
178
      return;
179
    }
180
 
181
  stored_fixups[i].num_fixups_in_chain = num_fixups;
182
  memcpy (stored_fixups[i].fixup_chain, fixups,
183
          sizeof (fixups[0]) * num_fixups);
184
  num_fixups = 0;
185
}
186
 
187
void
188
gas_cgen_restore_fixups (i)
189
     int i;
190
{
191
  if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
192
    {
193
      as_fatal ("index into stored_fixups[] out of bounds");
194
      return;
195
    }
196
 
197
  num_fixups = stored_fixups[i].num_fixups_in_chain;
198
  memcpy (fixups, stored_fixups[i].fixup_chain,
199
          (sizeof (stored_fixups[i].fixup_chain[0])) * num_fixups);
200
  stored_fixups[i].num_fixups_in_chain = 0;
201
}
202
 
203
void
204
gas_cgen_swap_fixups (i)
205
     int i;
206
{
207
  if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
208
    {
209
      as_fatal ("index into stored_fixups[] out of bounds");
210
      return;
211
    }
212
 
213
  if (num_fixups == 0)
214
    gas_cgen_restore_fixups (i);
215
 
216
  else if (stored_fixups[i].num_fixups_in_chain == 0)
217
    gas_cgen_save_fixups (i);
218
 
219
  else
220
    {
221
      int tmp;
222
      struct fixup tmp_fixup;
223
 
224
      tmp = stored_fixups[i].num_fixups_in_chain;
225
      stored_fixups[i].num_fixups_in_chain = num_fixups;
226
      num_fixups = tmp;
227
 
228
      for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;)
229
        {
230
          tmp_fixup = stored_fixups[i].fixup_chain [tmp];
231
          stored_fixups[i].fixup_chain[tmp] = fixups [tmp];
232
          fixups [tmp] = tmp_fixup;
233
        }
234
    }
235
}
236
 
237
/* Default routine to record a fixup.
238
   This is a cover function to fix_new.
239
   It exists because we record INSN with the fixup.
240
 
241
   FRAG and WHERE are their respective arguments to fix_new_exp.
242
   LENGTH is in bits.
243
   OPINFO is something the caller chooses to help in reloc determination.
244
 
245
   At this point we do not use a bfd_reloc_code_real_type for
246
   operands residing in the insn, but instead just use the
247
   operand index.  This lets us easily handle fixups for any
248
   operand type.  We pick a BFD reloc type in md_apply_fix.  */
249
 
250
fixS *
251
gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
252
     fragS *              frag;
253
     int                  where;
254
     const CGEN_INSN *    insn;
255
     int                  length;
256
     const CGEN_OPERAND * operand;
257
     int                  opinfo;
258
     symbolS *            symbol;
259
     offsetT              offset;
260
{
261
  fixS *fixP;
262
 
263
  /* It may seem strange to use operand->attrs and not insn->attrs here,
264
     but it is the operand that has a pc relative relocation.  */
265
  fixP = fix_new (frag, where, length / 8, symbol, offset,
266
                  CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
267
                  (bfd_reloc_code_real_type)
268
                    ((int) BFD_RELOC_UNUSED
269
                     + (int) operand->type));
270
  fixP->fx_cgen.insn = insn;
271
  fixP->fx_cgen.opinfo = opinfo;
272
  fixP->fx_cgen.field = NULL;
273
  fixP->fx_cgen.msb_field_p = 0;
274
 
275
  return fixP;
276
}
277
 
278
/* Default routine to record a fixup given an expression.
279
   This is a cover function to fix_new_exp.
280
   It exists because we record INSN with the fixup.
281
 
282
   FRAG and WHERE are their respective arguments to fix_new_exp.
283
   LENGTH is in bits.
284
   OPINFO is something the caller chooses to help in reloc determination.
285
 
286
   At this point we do not use a bfd_reloc_code_real_type for
287
   operands residing in the insn, but instead just use the
288
   operand index.  This lets us easily handle fixups for any
289
   operand type.  We pick a BFD reloc type in md_apply_fix.  */
290
 
291
fixS *
292
gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
293
     fragS *              frag;
294
     int                  where;
295
     const CGEN_INSN *    insn;
296
     int                  length;
297
     const CGEN_OPERAND * operand;
298
     int                  opinfo;
299
     expressionS *        exp;
300
{
301
  fixS *fixP;
302
 
303
  /* It may seem strange to use operand->attrs and not insn->attrs here,
304
     but it is the operand that has a pc relative relocation.  */
305
  fixP = fix_new_exp (frag, where, length / 8, exp,
306
                      CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
307
                      (bfd_reloc_code_real_type)
308
                        ((int) BFD_RELOC_UNUSED
309
                         + (int) operand->type));
310
  fixP->fx_cgen.insn = insn;
311
  fixP->fx_cgen.opinfo = opinfo;
312
  fixP->fx_cgen.field = NULL;
313
  fixP->fx_cgen.msb_field_p = 0;
314
 
315
  return fixP;
316
}
317
 
318
#ifdef OBJ_COMPLEX_RELC
319
static symbolS *
320
expr_build_binary (operatorT op, symbolS * s1, symbolS * s2)
321
{
322
  expressionS e;
323
 
324
  e.X_op = op;
325
  e.X_add_symbol = s1;
326
  e.X_op_symbol = s2;
327
  e.X_add_number = 0;
328
  return make_expr_symbol (& e);
329
}
330
#endif
331
 
332
/* Used for communication between the next two procedures.  */
333
static jmp_buf expr_jmp_buf;
334
static int expr_jmp_buf_p;
335
 
336
/* Callback for cgen interface.  Parse the expression at *STRP.
337
   The result is an error message or NULL for success (in which case
338
   *STRP is advanced past the parsed text).
339
   WANT is an indication of what the caller is looking for.
340
   If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
341
   a table entry with the insn, reset the queued fixups counter.
342
   An enum cgen_parse_operand_result is stored in RESULTP.
343
   OPINDEX is the operand's table entry index.
344
   OPINFO is something the caller chooses to help in reloc determination.
345
   The resulting value is stored in VALUEP.  */
346
 
347
const char *
348
gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP)
349
 
350
#ifdef OBJ_COMPLEX_RELC
351
     CGEN_CPU_DESC cd;
352
#else
353
     CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
354
#endif
355
     enum cgen_parse_operand_type want;
356
     const char **strP;
357
     int opindex;
358
     int opinfo;
359
     enum cgen_parse_operand_result *resultP;
360
     bfd_vma *valueP;
361
{
362
#ifdef __STDC__
363
  /* These are volatile to survive the setjmp.  */
364
  char * volatile hold;
365
  enum cgen_parse_operand_result * volatile resultP_1;
366
  volatile int opinfo_1;
367
#else
368
  static char *hold;
369
  static enum cgen_parse_operand_result *resultP_1;
370
  int opinfo_1;
371
#endif
372
  const char *errmsg;
373
  expressionS exp;
374
 
375
#ifdef OBJ_COMPLEX_RELC
376
  volatile int              signed_p = 0;
377
  symbolS *                 stmp = NULL;
378
  bfd_reloc_code_real_type  reloc_type;
379
  const CGEN_OPERAND *      operand;
380
  fixS                      dummy_fixup;
381
#endif
382
  if (want == CGEN_PARSE_OPERAND_INIT)
383
    {
384
      gas_cgen_init_parse ();
385
      return NULL;
386
    }
387
 
388
  resultP_1 = resultP;
389
  hold = input_line_pointer;
390
  input_line_pointer = (char *) *strP;
391
  opinfo_1 = opinfo;
392
 
393
  /* We rely on md_operand to longjmp back to us.
394
     This is done via gas_cgen_md_operand.  */
395
  if (setjmp (expr_jmp_buf) != 0)
396
    {
397
      expr_jmp_buf_p = 0;
398
      input_line_pointer = (char *) hold;
399
      *resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
400
      return _("illegal operand");
401
    }
402
 
403
  expr_jmp_buf_p = 1;
404
  expression (&exp);
405
  expr_jmp_buf_p = 0;
406
  errmsg = NULL;
407
 
408
  *strP = input_line_pointer;
409
  input_line_pointer = hold;
410
 
411
#ifdef TC_CGEN_PARSE_FIX_EXP
412
  opinfo_1 = TC_CGEN_PARSE_FIX_EXP (opinfo_1, & exp);
413
#endif 
414
 
415
  /* FIXME: Need to check `want'.  */
416
 
417
  switch (exp.X_op)
418
    {
419
    case O_illegal:
420
      errmsg = _("illegal operand");
421
      *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
422
      break;
423
    case O_absent:
424
      errmsg = _("missing operand");
425
      *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
426
      break;
427
    case O_constant:
428
      if (want == CGEN_PARSE_OPERAND_SYMBOLIC)
429
        goto de_fault;
430
      *valueP = exp.X_add_number;
431
      *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
432
      break;
433
    case O_register:
434
      *valueP = exp.X_add_number;
435
      *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
436
      break;
437
    de_fault:
438
    default:
439
#ifdef OBJ_COMPLEX_RELC
440
      /* Look up operand, check to see if there's an obvious
441
         overflow (this helps disambiguate some insn parses).  */
442
      operand = cgen_operand_lookup_by_num (cd, opindex);
443
      errmsg = weak_operand_overflow_check (& exp, operand);
444
 
445
      if (! errmsg)
446
        {
447
          /* Fragment the expression as necessary, and queue a reloc.  */
448
          memset (& dummy_fixup, 0, sizeof (fixS));
449
 
450
          reloc_type = md_cgen_lookup_reloc (0, operand, & dummy_fixup);
451
 
452
          if (exp.X_op == O_symbol
453
              && reloc_type == BFD_RELOC_RELC
454
              && exp.X_add_symbol->sy_value.X_op == O_constant
455
              && (!exp.X_add_symbol->bsym
456
                  || (exp.X_add_symbol->bsym->section != expr_section
457
                      && exp.X_add_symbol->bsym->section != absolute_section
458
                      && exp.X_add_symbol->bsym->section != undefined_section)))
459
            {
460
              /* Local labels will have been (eagerly) turned into constants
461
                 by now, due to the inappropriately deep insight of the
462
                 expression parser.  Unfortunately make_expr_symbol
463
                 prematurely dives into the symbol evaluator, and in this
464
                 case it gets a bad answer, so we manually create the
465
                 expression symbol we want here.  */
466
              stmp = symbol_create (FAKE_LABEL_NAME, expr_section, 0,
467
                                    & zero_address_frag);
468
              symbol_set_value_expression (stmp, & exp);
469
            }
470
          else
471
            stmp = make_expr_symbol (& exp);
472
 
473
          /* If this is a pc-relative RELC operand, we
474
             need to subtract "." from the expression.  */
475
          if (reloc_type == BFD_RELOC_RELC
476
              && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR))
477
            stmp = expr_build_binary (O_subtract, stmp, expr_build_dot ());
478
 
479
          /* FIXME: this is not a perfect heuristic for figuring out
480
             whether an operand is signed: it only works when the operand
481
             is an immediate. it's not terribly likely that any other
482
             values will be signed relocs, but it's possible. */
483
          if (operand && (operand->hw_type == HW_H_SINT))
484
            signed_p = 1;
485
 
486
          if (stmp->bsym && (stmp->bsym->section == expr_section))
487
            {
488
              if (signed_p)
489
                stmp->bsym->flags |= BSF_SRELC;
490
              else
491
                stmp->bsym->flags |= BSF_RELC;
492
            }
493
 
494
          /* Now package it all up for the fixup emitter.  */
495
          exp.X_op = O_symbol;
496
          exp.X_op_symbol = 0;
497
          exp.X_add_symbol = stmp;
498
          exp.X_add_number = 0;
499
 
500
          /* Re-init rightshift quantity, just in case.  */
501
          rightshift = operand->length;
502
          queue_fixup_recursively (opindex, opinfo_1, & exp,
503
                                   (reloc_type == BFD_RELOC_RELC) ?
504
                                   & (operand->index_fields) : 0,
505
                                   signed_p, -1);
506
        }
507
      * resultP = errmsg
508
        ? CGEN_PARSE_OPERAND_RESULT_ERROR
509
        : CGEN_PARSE_OPERAND_RESULT_QUEUED;
510
      *valueP = 0;
511
#else
512
      queue_fixup (opindex, opinfo_1, &exp);
513
      *valueP = 0;
514
      *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
515
#endif      
516
      break;
517
    }
518
 
519
  return errmsg;
520
}
521
 
522
/* md_operand handler to catch unrecognized expressions and halt the
523
   parsing process so the next entry can be tried.
524
 
525
   ??? This could be done differently by adding code to `expression'.  */
526
 
527
void
528
gas_cgen_md_operand (expressionP)
529
     expressionS *expressionP ATTRIBUTE_UNUSED;
530
{
531
  /* Don't longjmp if we're not called from within cgen_parse_operand().  */
532
  if (expr_jmp_buf_p)
533
    longjmp (expr_jmp_buf, 1);
534
}
535
 
536
/* Finish assembling instruction INSN.
537
   BUF contains what we've built up so far.
538
   LENGTH is the size of the insn in bits.
539
   RELAX_P is non-zero if relaxable insns should be emitted as such.
540
   Otherwise they're emitted in non-relaxable forms.
541
   The "result" is stored in RESULT if non-NULL.  */
542
 
543
void
544
gas_cgen_finish_insn (insn, buf, length, relax_p, result)
545
     const CGEN_INSN *insn;
546
     CGEN_INSN_BYTES_PTR buf;
547
     unsigned int length;
548
     int relax_p;
549
     finished_insnS *result;
550
{
551
  int i;
552
  int relax_operand;
553
  char *f;
554
  unsigned int byte_len = length / 8;
555
 
556
  /* ??? Target foo issues various warnings here, so one might want to provide
557
     a hook here.  However, our caller is defined in tc-foo.c so there
558
     shouldn't be a need for a hook.  */
559
 
560
  /* Write out the instruction.
561
     It is important to fetch enough space in one call to `frag_more'.
562
     We use (f - frag_now->fr_literal) to compute where we are and we
563
     don't want frag_now to change between calls.
564
 
565
     Relaxable instructions: We need to ensure we allocate enough
566
     space for the largest insn.  */
567
 
568
  if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
569
    /* These currently shouldn't get here.  */
570
    abort ();
571
 
572
  /* Is there a relaxable insn with the relaxable operand needing a fixup?  */
573
 
574
  relax_operand = -1;
575
  if (relax_p && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE))
576
    {
577
      /* Scan the fixups for the operand affected by relaxing
578
         (i.e. the branch address).  */
579
 
580
      for (i = 0; i < num_fixups; ++i)
581
        {
582
          if (CGEN_OPERAND_ATTR_VALUE (cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex),
583
                                       CGEN_OPERAND_RELAX))
584
            {
585
              relax_operand = i;
586
              break;
587
            }
588
        }
589
    }
590
 
591
  if (relax_operand != -1)
592
    {
593
      int max_len;
594
      fragS *old_frag;
595
      expressionS *exp;
596
      symbolS *sym;
597
      offsetT off;
598
 
599
#ifdef TC_CGEN_MAX_RELAX
600
      max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
601
#else
602
      max_len = CGEN_MAX_INSN_SIZE;
603
#endif
604
      /* Ensure variable part and fixed part are in same fragment.  */
605
      /* FIXME: Having to do this seems like a hack.  */
606
      frag_grow (max_len);
607
 
608
      /* Allocate space for the fixed part.  */
609
      f = frag_more (byte_len);
610
 
611
      /* Create a relaxable fragment for this instruction.  */
612
      old_frag = frag_now;
613
 
614
      exp = &fixups[relax_operand].exp;
615
      sym = exp->X_add_symbol;
616
      off = exp->X_add_number;
617
      if (exp->X_op != O_constant && exp->X_op != O_symbol)
618
        {
619
          /* Handle complex expressions.  */
620
          sym = make_expr_symbol (exp);
621
          off = 0;
622
        }
623
 
624
      frag_var (rs_machine_dependent,
625
                max_len - byte_len /* max chars */,
626
 
627
                /* FIXME: When we machine generate the relax table,
628
                   machine generate a macro to compute subtype.  */
629
                1 /* subtype */,
630
                sym,
631
                off,
632
                f);
633
 
634
      /* Record the operand number with the fragment so md_convert_frag
635
         can use gas_cgen_md_record_fixup to record the appropriate reloc.  */
636
      old_frag->fr_cgen.insn    = insn;
637
      old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
638
      old_frag->fr_cgen.opinfo  = fixups[relax_operand].opinfo;
639
      if (result)
640
        result->frag = old_frag;
641
    }
642
  else
643
    {
644
      f = frag_more (byte_len);
645
      if (result)
646
        result->frag = frag_now;
647
    }
648
 
649
  /* If we're recording insns as numbers (rather than a string of bytes),
650
     target byte order handling is deferred until now.  */
651
#if CGEN_INT_INSN_P
652
  cgen_put_insn_value (gas_cgen_cpu_desc, (unsigned char *) f, length, *buf);
653
#else
654
  memcpy (f, buf, byte_len);
655
#endif
656
 
657
  /* Emit DWARF2 debugging information.  */
658
  dwarf2_emit_insn (byte_len);
659
 
660
  /* Create any fixups.  */
661
  for (i = 0; i < num_fixups; ++i)
662
    {
663
      fixS *fixP;
664
      const CGEN_OPERAND *operand =
665
        cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex);
666
 
667
      /* Don't create fixups for these.  That's done during relaxation.
668
         We don't need to test for CGEN_INSN_RELAXED as they can't get here
669
         (see above).  */
670
      if (relax_p
671
          && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE)
672
          && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_RELAX))
673
        continue;
674
 
675
#ifndef md_cgen_record_fixup_exp
676
#define md_cgen_record_fixup_exp gas_cgen_record_fixup_exp
677
#endif
678
 
679
      fixP = md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
680
                                       insn, length, operand,
681
                                       fixups[i].opinfo,
682
                                       &fixups[i].exp);
683
      fixP->fx_cgen.field = fixups[i].field;
684
      fixP->fx_cgen.msb_field_p = fixups[i].msb_field_p;
685
      if (result)
686
        result->fixups[i] = fixP;
687
    }
688
 
689
  if (result)
690
    {
691
      result->num_fixups = num_fixups;
692
      result->addr = f;
693
    }
694
}
695
 
696
#ifdef OBJ_COMPLEX_RELC
697
/* Queue many fixups, recursively. If the field is a multi-ifield,
698
   repeatedly queue its sub-parts, right shifted to fit into the field (we
699
   assume here multi-fields represent a left-to-right, MSB0-LSB0
700
   reading). */
701
 
702
static void
703
queue_fixup_recursively (const int                      opindex,
704
                         const int                      opinfo,
705
                         expressionS *                  expP,
706
                         const CGEN_MAYBE_MULTI_IFLD *  field,
707
                         const int                      signed_p,
708
                         const int                      part_of_multi)
709
{
710
  if (field && field->count)
711
    {
712
      int i;
713
 
714
      for (i = 0; i < field->count; ++ i)
715
        queue_fixup_recursively (opindex, opinfo, expP,
716
                                 & (field->val.multi[i]), signed_p, i);
717
    }
718
  else
719
    {
720
      expressionS * new_exp = expP;
721
 
722
#ifdef DEBUG
723
      printf ("queueing fixup for field %s\n",
724
              (field ? field->val.leaf->name : "??"));
725
      print_symbol_value (expP->X_add_symbol);
726
#endif
727
      if (field && part_of_multi != -1)
728
        {
729
          rightshift -= field->val.leaf->length;
730
 
731
          /* Shift reloc value by number of bits remaining after this
732
             field.  */
733
          if (rightshift)
734
            new_exp = make_right_shifted_expr (expP, rightshift, signed_p);
735
        }
736
 
737
      /* Truncate reloc values to length, *after* leftmost one.  */
738
      fixups[num_fixups].msb_field_p = (part_of_multi <= 0);
739
      fixups[num_fixups].field = (CGEN_MAYBE_MULTI_IFLD *) field;
740
 
741
      queue_fixup (opindex, opinfo, new_exp);
742
    }
743
}
744
 
745
/* Encode the self-describing RELC reloc format's addend.  */
746
 
747
static unsigned long
748
gas_cgen_encode_addend (const unsigned long start,    /* in bits */
749
                        const unsigned long len,      /* in bits */
750
                        const unsigned long oplen,    /* in bits */
751
                        const unsigned long wordsz,   /* in bytes */
752
                        const unsigned long chunksz,  /* in bytes */
753
                        const unsigned long signed_p,
754
                        const unsigned long trunc_p)
755
{
756
  unsigned long res = 0L;
757
 
758
  res |= start    & 0x3F;
759
  res |= (oplen   & 0x3F) << 6;
760
  res |= (len     & 0x3F) << 12;
761
  res |= (wordsz  & 0xF)  << 18;
762
  res |= (chunksz & 0xF)  << 22;
763
  res |= (CGEN_INSN_LSB0_P ? 1 : 0) << 27;
764
  res |= signed_p << 28;
765
  res |= trunc_p << 29;
766
 
767
  return res;
768
}
769
 
770
/* Purpose: make a weak check that the expression doesn't overflow the
771
   operand it's to be inserted into.
772
 
773
   Rationale: some insns used to use %operators to disambiguate during a
774
   parse. when these %operators are translated to expressions by the macro
775
   expander, the ambiguity returns. we attempt to disambiguate by field
776
   size.
777
 
778
   Method: check to see if the expression's top node is an O_and operator,
779
   and the mask is larger than the operand length. This would be an
780
   overflow, so signal it by returning an error string. Any other case is
781
   ambiguous, so we assume it's OK and return NULL.  */
782
 
783
static char *
784
weak_operand_overflow_check (const expressionS *  exp,
785
                             const CGEN_OPERAND * operand)
786
{
787
  const unsigned long len = operand->length;
788
  unsigned long mask;
789
  unsigned long opmask = (((1L << (len - 1)) - 1) << 1) | 1;
790
 
791
  if (!exp)
792
    return NULL;
793
 
794
  if (exp->X_op != O_bit_and)
795
    {
796
      /* Check for implicit overflow flag.  */
797
      if (CGEN_OPERAND_ATTR_VALUE
798
          (operand, CGEN_OPERAND_RELOC_IMPLIES_OVERFLOW))
799
        return _("a reloc on this operand implies an overflow");
800
      return NULL;
801
    }
802
 
803
  mask = exp->X_add_number;
804
 
805
  if (exp->X_add_symbol &&
806
      exp->X_add_symbol->sy_value.X_op == O_constant)
807
    mask |= exp->X_add_symbol->sy_value.X_add_number;
808
 
809
  if (exp->X_op_symbol &&
810
      exp->X_op_symbol->sy_value.X_op == O_constant)
811
    mask |= exp->X_op_symbol->sy_value.X_add_number;
812
 
813
  /* Want to know if mask covers more bits than opmask.
814
     this is the same as asking if mask has any bits not in opmask,
815
     or whether (mask & ~opmask) is nonzero.  */
816
  if (mask && (mask & ~opmask))
817
    {
818
#ifdef DEBUG
819
      printf ("overflow: (mask = %8.8x, ~opmask = %8.8x, AND = %8.8x)\n",
820
              mask, ~opmask, (mask & ~opmask));
821
#endif
822
      return _("operand mask overflow");
823
    }
824
 
825
  return NULL;
826
}
827
 
828
 
829
static expressionS *
830
make_right_shifted_expr (expressionS * exp,
831
                         const int     amount,
832
                         const int     signed_p)
833
{
834
  symbolS * stmp = 0;
835
  expressionS * new_exp;
836
 
837
  stmp = expr_build_binary (O_right_shift,
838
                            make_expr_symbol (exp),
839
                            expr_build_uconstant (amount));
840
 
841
  if (signed_p)
842
    stmp->bsym->flags |= BSF_SRELC;
843
  else
844
    stmp->bsym->flags |= BSF_RELC;
845
 
846
  /* Then wrap that in a "symbol expr" for good measure.  */
847
  new_exp = xmalloc (sizeof (expressionS));
848
  memset (new_exp, 0, sizeof (expressionS));
849
  new_exp->X_op = O_symbol;
850
  new_exp->X_op_symbol = 0;
851
  new_exp->X_add_symbol = stmp;
852
  new_exp->X_add_number = 0;
853
 
854
  return new_exp;
855
}
856
#endif
857
/* Apply a fixup to the object code.  This is called for all the
858
   fixups we generated by the call to fix_new_exp, above.  In the call
859
   above we used a reloc code which was the largest legal reloc code
860
   plus the operand index.  Here we undo that to recover the operand
861
   index.  At this point all symbol values should be fully resolved,
862
   and we attempt to completely resolve the reloc.  If we can not do
863
   that, we determine the correct reloc code and put it back in the fixup.  */
864
 
865
/* FIXME: This function handles some of the fixups and bfd_install_relocation
866
   handles the rest.  bfd_install_relocation (or some other bfd function)
867
   should handle them all.  */
868
 
869
void
870
gas_cgen_md_apply_fix (fixP, valP, seg)
871
     fixS *   fixP;
872
     valueT * valP;
873
     segT     seg ATTRIBUTE_UNUSED;
874
{
875
  char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
876
  valueT value = * valP;
877
  /* Canonical name, since used a lot.  */
878
  CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
879
 
880
  if (fixP->fx_addsy == (symbolS *) NULL)
881
    fixP->fx_done = 1;
882
 
883
  /* We don't actually support subtracting a symbol.  */
884
  if (fixP->fx_subsy != (symbolS *) NULL)
885
    as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
886
 
887
  if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
888
    {
889
      int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
890
      const CGEN_OPERAND *operand = cgen_operand_lookup_by_num (cd, opindex);
891
      const char *errmsg;
892
      bfd_reloc_code_real_type reloc_type;
893
      CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
894
      const CGEN_INSN *insn = fixP->fx_cgen.insn;
895
      int start;
896
      int length;
897
      int signed_p = 0;
898
 
899
      if (fixP->fx_cgen.field)
900
        {
901
          /* Use the twisty little pointer path
902
             back to the ifield if it exists.  */
903
          start = fixP->fx_cgen.field->val.leaf->start;
904
          length = fixP->fx_cgen.field->val.leaf->length;
905
        }
906
      else
907
        {
908
          /* Or the far less useful operand-size guesstimate.  */
909
          start = operand->start;
910
          length = operand->length;
911
        }
912
 
913
      /* FIXME: this is not a perfect heuristic for figuring out
914
         whether an operand is signed: it only works when the operand
915
         is an immediate. it's not terribly likely that any other
916
         values will be signed relocs, but it's possible. */
917
      if (operand && (operand->hw_type == HW_H_SINT))
918
        signed_p = 1;
919
 
920
      /* If the reloc has been fully resolved finish the operand here.  */
921
      /* FIXME: This duplicates the capabilities of code in BFD.  */
922
      if (fixP->fx_done
923
          /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
924
             finish the job.  Testing for pcrel is a temporary hack.  */
925
          || fixP->fx_pcrel)
926
        {
927
          CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
928
          CGEN_CPU_SET_VMA_OPERAND (cd) (cd, opindex, fields, (bfd_vma) value);
929
 
930
#if CGEN_INT_INSN_P
931
          {
932
            CGEN_INSN_INT insn_value =
933
              cgen_get_insn_value (cd, (unsigned char *) where,
934
                                   CGEN_INSN_BITSIZE (insn));
935
 
936
            /* ??? 0 is passed for `pc'.  */
937
            errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
938
                                                   &insn_value, (bfd_vma) 0);
939
            cgen_put_insn_value (cd, (unsigned char *) where,
940
                                 CGEN_INSN_BITSIZE (insn), insn_value);
941
          }
942
#else
943
          /* ??? 0 is passed for `pc'.  */
944
          errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
945
                                                 (unsigned char *) where,
946
                                                 (bfd_vma) 0);
947
#endif
948
          if (errmsg)
949
            as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
950
        }
951
 
952
      if (fixP->fx_done)
953
        return;
954
 
955
      /* The operand isn't fully resolved.  Determine a BFD reloc value
956
         based on the operand information and leave it to
957
         bfd_install_relocation.  Note that this doesn't work when
958
         partial_inplace == false.  */
959
 
960
      reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
961
#ifdef OBJ_COMPLEX_RELC
962
      if (reloc_type == BFD_RELOC_RELC)
963
        {
964
          /* Change addend to "self-describing" form,
965
             for BFD to handle in the linker.  */
966
          value = gas_cgen_encode_addend (start, operand->length,
967
                                          length, fixP->fx_size,
968
                                          cd->insn_chunk_bitsize / 8,
969
                                          signed_p,
970
                                          ! (fixP->fx_cgen.msb_field_p));
971
        }
972
#endif
973
 
974
      if (reloc_type != BFD_RELOC_NONE)
975
        fixP->fx_r_type = reloc_type;
976
      else
977
        {
978
          as_bad_where (fixP->fx_file, fixP->fx_line,
979
                        _("unresolved expression that must be resolved"));
980
          fixP->fx_done = 1;
981
          return;
982
        }
983
    }
984
  else if (fixP->fx_done)
985
    {
986
      /* We're finished with this fixup.  Install it because
987
         bfd_install_relocation won't be called to do it.  */
988
      switch (fixP->fx_r_type)
989
        {
990
        case BFD_RELOC_8:
991
          md_number_to_chars (where, value, 1);
992
          break;
993
        case BFD_RELOC_16:
994
          md_number_to_chars (where, value, 2);
995
          break;
996
        case BFD_RELOC_32:
997
          md_number_to_chars (where, value, 4);
998
          break;
999
        case BFD_RELOC_64:
1000
          md_number_to_chars (where, value, 8);
1001
          break;
1002
        default:
1003
          as_bad_where (fixP->fx_file, fixP->fx_line,
1004
                        _("internal error: can't install fix for reloc type %d (`%s')"),
1005
                        fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
1006
          break;
1007
        }
1008
    }
1009
  /* else
1010
     bfd_install_relocation will be called to finish things up.  */
1011
 
1012
  /* Tuck `value' away for use by tc_gen_reloc.
1013
     See the comment describing fx_addnumber in write.h.
1014
     This field is misnamed (or misused :-).  */
1015
  fixP->fx_addnumber = value;
1016
}
1017
 
1018
/* Translate internal representation of relocation info to BFD target format.
1019
 
1020
   FIXME: To what extent can we get all relevant targets to use this?  */
1021
 
1022
arelent *
1023
gas_cgen_tc_gen_reloc (section, fixP)
1024
     asection * section ATTRIBUTE_UNUSED;
1025
     fixS *     fixP;
1026
{
1027
  arelent *reloc;
1028
  reloc = (arelent *) xmalloc (sizeof (arelent));
1029
 
1030
  reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1031
  if (reloc->howto == (reloc_howto_type *) NULL)
1032
    {
1033
      as_bad_where (fixP->fx_file, fixP->fx_line,
1034
                    _("relocation is not supported"));
1035
      return NULL;
1036
    }
1037
 
1038
  gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1039
 
1040
  reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1041
  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
1042
 
1043
  /* Use fx_offset for these cases.  */
1044
  if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
1045
      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
1046
    reloc->addend = fixP->fx_offset;
1047
  else
1048
    reloc->addend = fixP->fx_addnumber;
1049
 
1050
  reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1051
  return reloc;
1052
}
1053
 
1054
/* Perform any cgen specific initialisation.
1055
   Called after gas_cgen_cpu_desc has been created.  */
1056
 
1057
void
1058
gas_cgen_begin ()
1059
{
1060
  if (flag_signed_overflow_ok)
1061
    cgen_set_signed_overflow_ok (gas_cgen_cpu_desc);
1062
  else
1063
    cgen_clear_signed_overflow_ok (gas_cgen_cpu_desc);
1064
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.