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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [config/] [v850/] [v850.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Subroutines for insn-output.c for NEC V850 series
2
   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3
   Free Software Foundation, Inc.
4
   Contributed by Jeff Law (law@cygnus.com).
5
 
6
   This file is part of GCC.
7
 
8
   GCC is free software; you can redistribute it and/or modify it
9
   under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 2, or (at your option)
11
   any later version.
12
 
13
   GCC is distributed in the hope that it will be useful, but WITHOUT
14
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16
   for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with GCC; see the file COPYING.  If not, write to the Free
20
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21
   02110-1301, USA.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "coretypes.h"
26
#include "tm.h"
27
#include "tree.h"
28
#include "rtl.h"
29
#include "regs.h"
30
#include "hard-reg-set.h"
31
#include "real.h"
32
#include "insn-config.h"
33
#include "conditions.h"
34
#include "output.h"
35
#include "insn-attr.h"
36
#include "flags.h"
37
#include "recog.h"
38
#include "expr.h"
39
#include "function.h"
40
#include "toplev.h"
41
#include "ggc.h"
42
#include "integrate.h"
43
#include "tm_p.h"
44
#include "target.h"
45
#include "target-def.h"
46
 
47
#ifndef streq
48
#define streq(a,b) (strcmp (a, b) == 0)
49
#endif
50
 
51
/* Function prototypes for stupid compilers:  */
52
static bool v850_handle_option       (size_t, const char *, int);
53
static void const_double_split       (rtx, HOST_WIDE_INT *, HOST_WIDE_INT *);
54
static int  const_costs_int          (HOST_WIDE_INT, int);
55
static int  const_costs              (rtx, enum rtx_code);
56
static bool v850_rtx_costs           (rtx, int, int, int *);
57
static void substitute_ep_register   (rtx, rtx, int, int, rtx *, rtx *);
58
static void v850_reorg               (void);
59
static int  ep_memory_offset         (enum machine_mode, int);
60
static void v850_set_data_area       (tree, v850_data_area);
61
const struct attribute_spec v850_attribute_table[];
62
static tree v850_handle_interrupt_attribute (tree *, tree, tree, int, bool *);
63
static tree v850_handle_data_area_attribute (tree *, tree, tree, int, bool *);
64
static void v850_insert_attributes   (tree, tree *);
65
static void v850_select_section (tree, int, unsigned HOST_WIDE_INT);
66
static void v850_encode_data_area    (tree, rtx);
67
static void v850_encode_section_info (tree, rtx, int);
68
static bool v850_return_in_memory    (tree, tree);
69
static void v850_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
70
                                         tree, int *, int);
71
static bool v850_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
72
                                    tree, bool);
73
static int v850_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
74
                                   tree, bool);
75
 
76
/* Information about the various small memory areas.  */
77
struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
78
{
79
  /* name       max     physical max */
80
  { "tda",      0,               256 },
81
  { "sda",      0,               65536 },
82
  { "zda",      0,               32768 },
83
};
84
 
85
/* Names of the various data areas used on the v850.  */
86
tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
87
tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
88
 
89
/* Track the current data area set by the data area pragma (which
90
   can be nested).  Tested by check_default_data_area.  */
91
data_area_stack_element * data_area_stack = NULL;
92
 
93
/* True if we don't need to check any more if the current
94
   function is an interrupt handler.  */
95
static int v850_interrupt_cache_p = FALSE;
96
 
97
/* Whether current function is an interrupt handler.  */
98
static int v850_interrupt_p = FALSE;
99
 
100
/* Initialize the GCC target structure.  */
101
#undef TARGET_ASM_ALIGNED_HI_OP
102
#define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
103
 
104
#undef TARGET_ATTRIBUTE_TABLE
105
#define TARGET_ATTRIBUTE_TABLE v850_attribute_table
106
 
107
#undef TARGET_INSERT_ATTRIBUTES
108
#define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
109
 
110
#undef  TARGET_ASM_SELECT_SECTION
111
#define TARGET_ASM_SELECT_SECTION  v850_select_section
112
 
113
#undef TARGET_ENCODE_SECTION_INFO
114
#define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
115
 
116
#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
117
#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
118
 
119
#undef TARGET_DEFAULT_TARGET_FLAGS
120
#define TARGET_DEFAULT_TARGET_FLAGS (MASK_DEFAULT | MASK_APP_REGS)
121
#undef TARGET_HANDLE_OPTION
122
#define TARGET_HANDLE_OPTION v850_handle_option
123
 
124
#undef TARGET_RTX_COSTS
125
#define TARGET_RTX_COSTS v850_rtx_costs
126
 
127
#undef TARGET_ADDRESS_COST
128
#define TARGET_ADDRESS_COST hook_int_rtx_0
129
 
130
#undef TARGET_MACHINE_DEPENDENT_REORG
131
#define TARGET_MACHINE_DEPENDENT_REORG v850_reorg
132
 
133
#undef TARGET_PROMOTE_PROTOTYPES
134
#define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
135
 
136
#undef TARGET_RETURN_IN_MEMORY
137
#define TARGET_RETURN_IN_MEMORY v850_return_in_memory
138
 
139
#undef TARGET_PASS_BY_REFERENCE
140
#define TARGET_PASS_BY_REFERENCE v850_pass_by_reference
141
 
142
#undef TARGET_CALLEE_COPIES
143
#define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
144
 
145
#undef TARGET_SETUP_INCOMING_VARARGS
146
#define TARGET_SETUP_INCOMING_VARARGS v850_setup_incoming_varargs
147
 
148
#undef TARGET_ARG_PARTIAL_BYTES
149
#define TARGET_ARG_PARTIAL_BYTES v850_arg_partial_bytes
150
 
151
struct gcc_target targetm = TARGET_INITIALIZER;
152
 
153
/* Set the maximum size of small memory area TYPE to the value given
154
   by VALUE.  Return true if VALUE was syntactically correct.  VALUE
155
   starts with the argument separator: either "-" or "=".  */
156
 
157
static bool
158
v850_handle_memory_option (enum small_memory_type type, const char *value)
159
{
160
  int i, size;
161
 
162
  if (*value != '-' && *value != '=')
163
    return false;
164
 
165
  value++;
166
  for (i = 0; value[i]; i++)
167
    if (!ISDIGIT (value[i]))
168
      return false;
169
 
170
  size = atoi (value);
171
  if (size > small_memory[type].physical_max)
172
    error ("value passed to %<-m%s%> is too large", small_memory[type].name);
173
  else
174
    small_memory[type].max = size;
175
  return true;
176
}
177
 
178
/* Implement TARGET_HANDLE_OPTION.  */
179
 
180
static bool
181
v850_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
182
{
183
  switch (code)
184
    {
185
    case OPT_mspace:
186
      target_flags |= MASK_EP | MASK_PROLOG_FUNCTION;
187
      return true;
188
 
189
    case OPT_mv850:
190
      target_flags &= ~(MASK_CPU ^ MASK_V850);
191
      return true;
192
 
193
    case OPT_mv850e:
194
    case OPT_mv850e1:
195
      target_flags &= ~(MASK_CPU ^ MASK_V850E);
196
      return true;
197
 
198
    case OPT_mtda:
199
      return v850_handle_memory_option (SMALL_MEMORY_TDA, arg);
200
 
201
    case OPT_msda:
202
      return v850_handle_memory_option (SMALL_MEMORY_SDA, arg);
203
 
204
    case OPT_mzda:
205
      return v850_handle_memory_option (SMALL_MEMORY_ZDA, arg);
206
 
207
    default:
208
      return true;
209
    }
210
}
211
 
212
static bool
213
v850_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
214
                        enum machine_mode mode, tree type,
215
                        bool named ATTRIBUTE_UNUSED)
216
{
217
  unsigned HOST_WIDE_INT size;
218
 
219
  if (type)
220
    size = int_size_in_bytes (type);
221
  else
222
    size = GET_MODE_SIZE (mode);
223
 
224
  return size > 8;
225
}
226
 
227
/* Return an RTX to represent where a value with mode MODE will be returned
228
   from a function.  If the result is 0, the argument is pushed.  */
229
 
230
rtx
231
function_arg (CUMULATIVE_ARGS * cum,
232
              enum machine_mode mode,
233
              tree type,
234
              int named)
235
{
236
  rtx result = 0;
237
  int size, align;
238
 
239
  if (TARGET_GHS && !named)
240
    return NULL_RTX;
241
 
242
  if (mode == BLKmode)
243
    size = int_size_in_bytes (type);
244
  else
245
    size = GET_MODE_SIZE (mode);
246
 
247
  if (size < 1)
248
    return 0;
249
 
250
  if (type)
251
    align = TYPE_ALIGN (type) / BITS_PER_UNIT;
252
  else
253
    align = size;
254
 
255
  cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
256
 
257
  if (cum->nbytes > 4 * UNITS_PER_WORD)
258
    return 0;
259
 
260
  if (type == NULL_TREE
261
      && cum->nbytes + size > 4 * UNITS_PER_WORD)
262
    return 0;
263
 
264
  switch (cum->nbytes / UNITS_PER_WORD)
265
    {
266
    case 0:
267
      result = gen_rtx_REG (mode, 6);
268
      break;
269
    case 1:
270
      result = gen_rtx_REG (mode, 7);
271
      break;
272
    case 2:
273
      result = gen_rtx_REG (mode, 8);
274
      break;
275
    case 3:
276
      result = gen_rtx_REG (mode, 9);
277
      break;
278
    default:
279
      result = 0;
280
    }
281
 
282
  return result;
283
}
284
 
285
 
286
/* Return the number of bytes which must be put into registers
287
   for values which are part in registers and part in memory.  */
288
 
289
static int
290
v850_arg_partial_bytes (CUMULATIVE_ARGS * cum, enum machine_mode mode,
291
                        tree type, bool named)
292
{
293
  int size, align;
294
 
295
  if (TARGET_GHS && !named)
296
    return 0;
297
 
298
  if (mode == BLKmode)
299
    size = int_size_in_bytes (type);
300
  else
301
    size = GET_MODE_SIZE (mode);
302
 
303
  if (type)
304
    align = TYPE_ALIGN (type) / BITS_PER_UNIT;
305
  else
306
    align = size;
307
 
308
  cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
309
 
310
  if (cum->nbytes > 4 * UNITS_PER_WORD)
311
    return 0;
312
 
313
  if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
314
    return 0;
315
 
316
  if (type == NULL_TREE
317
      && cum->nbytes + size > 4 * UNITS_PER_WORD)
318
    return 0;
319
 
320
  return 4 * UNITS_PER_WORD - cum->nbytes;
321
}
322
 
323
 
324
/* Return the high and low words of a CONST_DOUBLE */
325
 
326
static void
327
const_double_split (rtx x, HOST_WIDE_INT * p_high, HOST_WIDE_INT * p_low)
328
{
329
  if (GET_CODE (x) == CONST_DOUBLE)
330
    {
331
      long t[2];
332
      REAL_VALUE_TYPE rv;
333
 
334
      switch (GET_MODE (x))
335
        {
336
        case DFmode:
337
          REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
338
          REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
339
          *p_high = t[1];       /* since v850 is little endian */
340
          *p_low = t[0]; /* high is second word */
341
          return;
342
 
343
        case SFmode:
344
          REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
345
          REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
346
          *p_low = 0;
347
          return;
348
 
349
        case VOIDmode:
350
        case DImode:
351
          *p_high = CONST_DOUBLE_HIGH (x);
352
          *p_low  = CONST_DOUBLE_LOW (x);
353
          return;
354
 
355
        default:
356
          break;
357
        }
358
    }
359
 
360
  fatal_insn ("const_double_split got a bad insn:", x);
361
}
362
 
363
 
364
/* Return the cost of the rtx R with code CODE.  */
365
 
366
static int
367
const_costs_int (HOST_WIDE_INT value, int zero_cost)
368
{
369
  if (CONST_OK_FOR_I (value))
370
      return zero_cost;
371
  else if (CONST_OK_FOR_J (value))
372
    return 1;
373
  else if (CONST_OK_FOR_K (value))
374
    return 2;
375
  else
376
    return 4;
377
}
378
 
379
static int
380
const_costs (rtx r, enum rtx_code c)
381
{
382
  HOST_WIDE_INT high, low;
383
 
384
  switch (c)
385
    {
386
    case CONST_INT:
387
      return const_costs_int (INTVAL (r), 0);
388
 
389
    case CONST_DOUBLE:
390
      const_double_split (r, &high, &low);
391
      if (GET_MODE (r) == SFmode)
392
        return const_costs_int (high, 1);
393
      else
394
        return const_costs_int (high, 1) + const_costs_int (low, 1);
395
 
396
    case SYMBOL_REF:
397
    case LABEL_REF:
398
    case CONST:
399
      return 2;
400
 
401
    case HIGH:
402
      return 1;
403
 
404
    default:
405
      return 4;
406
    }
407
}
408
 
409
static bool
410
v850_rtx_costs (rtx x,
411
                int code,
412
                int outer_code ATTRIBUTE_UNUSED,
413
                int * total)
414
{
415
  switch (code)
416
    {
417
    case CONST_INT:
418
    case CONST_DOUBLE:
419
    case CONST:
420
    case SYMBOL_REF:
421
    case LABEL_REF:
422
      *total = COSTS_N_INSNS (const_costs (x, code));
423
      return true;
424
 
425
    case MOD:
426
    case DIV:
427
    case UMOD:
428
    case UDIV:
429
      if (TARGET_V850E && optimize_size)
430
        *total = 6;
431
      else
432
        *total = 60;
433
      return true;
434
 
435
    case MULT:
436
      if (TARGET_V850E
437
          && (   GET_MODE (x) == SImode
438
              || GET_MODE (x) == HImode
439
              || GET_MODE (x) == QImode))
440
        {
441
          if (GET_CODE (XEXP (x, 1)) == REG)
442
            *total = 4;
443
          else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
444
            {
445
              if (CONST_OK_FOR_O (INTVAL (XEXP (x, 1))))
446
                *total = 6;
447
              else if (CONST_OK_FOR_K (INTVAL (XEXP (x, 1))))
448
                *total = 10;
449
            }
450
        }
451
      else
452
        *total = 20;
453
      return true;
454
 
455
    default:
456
      return false;
457
    }
458
}
459
 
460
/* Print operand X using operand code CODE to assembly language output file
461
   FILE.  */
462
 
463
void
464
print_operand (FILE * file, rtx x, int code)
465
{
466
  HOST_WIDE_INT high, low;
467
 
468
  switch (code)
469
    {
470
    case 'c':
471
      /* We use 'c' operands with symbols for .vtinherit */
472
      if (GET_CODE (x) == SYMBOL_REF)
473
        {
474
          output_addr_const(file, x);
475
          break;
476
        }
477
      /* fall through */
478
    case 'b':
479
    case 'B':
480
    case 'C':
481
      switch ((code == 'B' || code == 'C')
482
              ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
483
        {
484
          case NE:
485
            if (code == 'c' || code == 'C')
486
              fprintf (file, "nz");
487
            else
488
              fprintf (file, "ne");
489
            break;
490
          case EQ:
491
            if (code == 'c' || code == 'C')
492
              fprintf (file, "z");
493
            else
494
              fprintf (file, "e");
495
            break;
496
          case GE:
497
            fprintf (file, "ge");
498
            break;
499
          case GT:
500
            fprintf (file, "gt");
501
            break;
502
          case LE:
503
            fprintf (file, "le");
504
            break;
505
          case LT:
506
            fprintf (file, "lt");
507
            break;
508
          case GEU:
509
            fprintf (file, "nl");
510
            break;
511
          case GTU:
512
            fprintf (file, "h");
513
            break;
514
          case LEU:
515
            fprintf (file, "nh");
516
            break;
517
          case LTU:
518
            fprintf (file, "l");
519
            break;
520
          default:
521
            gcc_unreachable ();
522
        }
523
      break;
524
    case 'F':                   /* high word of CONST_DOUBLE */
525
      switch (GET_CODE (x))
526
        {
527
        case CONST_INT:
528
          fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
529
          break;
530
 
531
        case CONST_DOUBLE:
532
          const_double_split (x, &high, &low);
533
          fprintf (file, "%ld", (long) high);
534
          break;
535
 
536
        default:
537
          gcc_unreachable ();
538
        }
539
      break;
540
    case 'G':                   /* low word of CONST_DOUBLE */
541
      switch (GET_CODE (x))
542
        {
543
        case CONST_INT:
544
          fprintf (file, "%ld", (long) INTVAL (x));
545
          break;
546
 
547
        case CONST_DOUBLE:
548
          const_double_split (x, &high, &low);
549
          fprintf (file, "%ld", (long) low);
550
          break;
551
 
552
        default:
553
          gcc_unreachable ();
554
        }
555
      break;
556
    case 'L':
557
      fprintf (file, "%d\n", (int)(INTVAL (x) & 0xffff));
558
      break;
559
    case 'M':
560
      fprintf (file, "%d", exact_log2 (INTVAL (x)));
561
      break;
562
    case 'O':
563
      gcc_assert (special_symbolref_operand (x, VOIDmode));
564
 
565
      if (GET_CODE (x) == CONST)
566
        x = XEXP (XEXP (x, 0), 0);
567
      else
568
        gcc_assert (GET_CODE (x) == SYMBOL_REF);
569
 
570
      if (SYMBOL_REF_ZDA_P (x))
571
        fprintf (file, "zdaoff");
572
      else if (SYMBOL_REF_SDA_P (x))
573
        fprintf (file, "sdaoff");
574
      else if (SYMBOL_REF_TDA_P (x))
575
        fprintf (file, "tdaoff");
576
      else
577
        gcc_unreachable ();
578
      break;
579
    case 'P':
580
      gcc_assert (special_symbolref_operand (x, VOIDmode));
581
      output_addr_const (file, x);
582
      break;
583
    case 'Q':
584
      gcc_assert (special_symbolref_operand (x, VOIDmode));
585
 
586
      if (GET_CODE (x) == CONST)
587
        x = XEXP (XEXP (x, 0), 0);
588
      else
589
        gcc_assert (GET_CODE (x) == SYMBOL_REF);
590
 
591
      if (SYMBOL_REF_ZDA_P (x))
592
        fprintf (file, "r0");
593
      else if (SYMBOL_REF_SDA_P (x))
594
        fprintf (file, "gp");
595
      else if (SYMBOL_REF_TDA_P (x))
596
        fprintf (file, "ep");
597
      else
598
        gcc_unreachable ();
599
      break;
600
    case 'R':           /* 2nd word of a double.  */
601
      switch (GET_CODE (x))
602
        {
603
        case REG:
604
          fprintf (file, reg_names[REGNO (x) + 1]);
605
          break;
606
        case MEM:
607
          x = XEXP (adjust_address (x, SImode, 4), 0);
608
          print_operand_address (file, x);
609
          if (GET_CODE (x) == CONST_INT)
610
            fprintf (file, "[r0]");
611
          break;
612
 
613
        default:
614
          break;
615
        }
616
      break;
617
    case 'S':
618
      {
619
        /* If it's a reference to a TDA variable, use sst/sld vs. st/ld.  */
620
        if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
621
          fputs ("s", file);
622
 
623
        break;
624
      }
625
    case 'T':
626
      {
627
        /* Like an 'S' operand above, but for unsigned loads only.  */
628
        if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
629
          fputs ("s", file);
630
 
631
        break;
632
      }
633
    case 'W':                   /* print the instruction suffix */
634
      switch (GET_MODE (x))
635
        {
636
        default:
637
          gcc_unreachable ();
638
 
639
        case QImode: fputs (".b", file); break;
640
        case HImode: fputs (".h", file); break;
641
        case SImode: fputs (".w", file); break;
642
        case SFmode: fputs (".w", file); break;
643
        }
644
      break;
645
    case '.':                   /* register r0 */
646
      fputs (reg_names[0], file);
647
      break;
648
    case 'z':                   /* reg or zero */
649
      if (GET_CODE (x) == REG)
650
        fputs (reg_names[REGNO (x)], file);
651
      else
652
        {
653
          gcc_assert (x == const0_rtx);
654
          fputs (reg_names[0], file);
655
        }
656
      break;
657
    default:
658
      switch (GET_CODE (x))
659
        {
660
        case MEM:
661
          if (GET_CODE (XEXP (x, 0)) == CONST_INT)
662
            output_address (gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, 0),
663
                                          XEXP (x, 0)));
664
          else
665
            output_address (XEXP (x, 0));
666
          break;
667
 
668
        case REG:
669
          fputs (reg_names[REGNO (x)], file);
670
          break;
671
        case SUBREG:
672
          fputs (reg_names[subreg_regno (x)], file);
673
          break;
674
        case CONST_INT:
675
        case SYMBOL_REF:
676
        case CONST:
677
        case LABEL_REF:
678
        case CODE_LABEL:
679
          print_operand_address (file, x);
680
          break;
681
        default:
682
          gcc_unreachable ();
683
        }
684
      break;
685
 
686
    }
687
}
688
 
689
 
690
/* Output assembly language output for the address ADDR to FILE.  */
691
 
692
void
693
print_operand_address (FILE * file, rtx addr)
694
{
695
  switch (GET_CODE (addr))
696
    {
697
    case REG:
698
      fprintf (file, "0[");
699
      print_operand (file, addr, 0);
700
      fprintf (file, "]");
701
      break;
702
    case LO_SUM:
703
      if (GET_CODE (XEXP (addr, 0)) == REG)
704
        {
705
          /* reg,foo */
706
          fprintf (file, "lo(");
707
          print_operand (file, XEXP (addr, 1), 0);
708
          fprintf (file, ")[");
709
          print_operand (file, XEXP (addr, 0), 0);
710
          fprintf (file, "]");
711
        }
712
      break;
713
    case PLUS:
714
      if (GET_CODE (XEXP (addr, 0)) == REG
715
          || GET_CODE (XEXP (addr, 0)) == SUBREG)
716
        {
717
          /* reg,foo */
718
          print_operand (file, XEXP (addr, 1), 0);
719
          fprintf (file, "[");
720
          print_operand (file, XEXP (addr, 0), 0);
721
          fprintf (file, "]");
722
        }
723
      else
724
        {
725
          print_operand (file, XEXP (addr, 0), 0);
726
          fprintf (file, "+");
727
          print_operand (file, XEXP (addr, 1), 0);
728
        }
729
      break;
730
    case SYMBOL_REF:
731
      {
732
        const char *off_name = NULL;
733
        const char *reg_name = NULL;
734
 
735
        if (SYMBOL_REF_ZDA_P (addr))
736
          {
737
            off_name = "zdaoff";
738
            reg_name = "r0";
739
          }
740
        else if (SYMBOL_REF_SDA_P (addr))
741
          {
742
            off_name = "sdaoff";
743
            reg_name = "gp";
744
          }
745
        else if (SYMBOL_REF_TDA_P (addr))
746
          {
747
            off_name = "tdaoff";
748
            reg_name = "ep";
749
          }
750
 
751
        if (off_name)
752
          fprintf (file, "%s(", off_name);
753
        output_addr_const (file, addr);
754
        if (reg_name)
755
          fprintf (file, ")[%s]", reg_name);
756
      }
757
      break;
758
    case CONST:
759
      if (special_symbolref_operand (addr, VOIDmode))
760
        {
761
          rtx x = XEXP (XEXP (addr, 0), 0);
762
          const char *off_name;
763
          const char *reg_name;
764
 
765
          if (SYMBOL_REF_ZDA_P (x))
766
            {
767
              off_name = "zdaoff";
768
              reg_name = "r0";
769
            }
770
          else if (SYMBOL_REF_SDA_P (x))
771
            {
772
              off_name = "sdaoff";
773
              reg_name = "gp";
774
            }
775
          else if (SYMBOL_REF_TDA_P (x))
776
            {
777
              off_name = "tdaoff";
778
              reg_name = "ep";
779
            }
780
          else
781
            gcc_unreachable ();
782
 
783
          fprintf (file, "%s(", off_name);
784
          output_addr_const (file, addr);
785
          fprintf (file, ")[%s]", reg_name);
786
        }
787
      else
788
        output_addr_const (file, addr);
789
      break;
790
    default:
791
      output_addr_const (file, addr);
792
      break;
793
    }
794
}
795
 
796
/* When assemble_integer is used to emit the offsets for a switch
797
   table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
798
   output_addr_const will normally barf at this, but it is OK to omit
799
   the truncate and just emit the difference of the two labels.  The
800
   .hword directive will automatically handle the truncation for us.
801
 
802
   Returns 1 if rtx was handled, 0 otherwise.  */
803
 
804
int
805
v850_output_addr_const_extra (FILE * file, rtx x)
806
{
807
  if (GET_CODE (x) != TRUNCATE)
808
    return 0;
809
 
810
  x = XEXP (x, 0);
811
 
812
  /* We must also handle the case where the switch table was passed a
813
     constant value and so has been collapsed.  In this case the first
814
     label will have been deleted.  In such a case it is OK to emit
815
     nothing, since the table will not be used.
816
     (cf gcc.c-torture/compile/990801-1.c).  */
817
  if (GET_CODE (x) == MINUS
818
      && GET_CODE (XEXP (x, 0)) == LABEL_REF
819
      && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
820
      && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
821
    return 1;
822
 
823
  output_addr_const (file, x);
824
  return 1;
825
}
826
 
827
/* Return appropriate code to load up a 1, 2, or 4 integer/floating
828
   point value.  */
829
 
830
const char *
831
output_move_single (rtx * operands)
832
{
833
  rtx dst = operands[0];
834
  rtx src = operands[1];
835
 
836
  if (REG_P (dst))
837
    {
838
      if (REG_P (src))
839
        return "mov %1,%0";
840
 
841
      else if (GET_CODE (src) == CONST_INT)
842
        {
843
          HOST_WIDE_INT value = INTVAL (src);
844
 
845
          if (CONST_OK_FOR_J (value))           /* Signed 5 bit immediate.  */
846
            return "mov %1,%0";
847
 
848
          else if (CONST_OK_FOR_K (value))      /* Signed 16 bit immediate.  */
849
            return "movea lo(%1),%.,%0";
850
 
851
          else if (CONST_OK_FOR_L (value))      /* Upper 16 bits were set.  */
852
            return "movhi hi(%1),%.,%0";
853
 
854
          /* A random constant.  */
855
          else if (TARGET_V850E)
856
              return "mov %1,%0";
857
          else
858
            return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
859
        }
860
 
861
      else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
862
        {
863
          HOST_WIDE_INT high, low;
864
 
865
          const_double_split (src, &high, &low);
866
 
867
          if (CONST_OK_FOR_J (high))            /* Signed 5 bit immediate.  */
868
            return "mov %F1,%0";
869
 
870
          else if (CONST_OK_FOR_K (high))       /* Signed 16 bit immediate.  */
871
            return "movea lo(%F1),%.,%0";
872
 
873
          else if (CONST_OK_FOR_L (high))       /* Upper 16 bits were set.  */
874
            return "movhi hi(%F1),%.,%0";
875
 
876
          /* A random constant.  */
877
          else if (TARGET_V850E)
878
              return "mov %F1,%0";
879
 
880
          else
881
            return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
882
        }
883
 
884
      else if (GET_CODE (src) == MEM)
885
        return "%S1ld%W1 %1,%0";
886
 
887
      else if (special_symbolref_operand (src, VOIDmode))
888
        return "movea %O1(%P1),%Q1,%0";
889
 
890
      else if (GET_CODE (src) == LABEL_REF
891
               || GET_CODE (src) == SYMBOL_REF
892
               || GET_CODE (src) == CONST)
893
        {
894
          if (TARGET_V850E)
895
            return "mov hilo(%1),%0";
896
          else
897
            return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
898
        }
899
 
900
      else if (GET_CODE (src) == HIGH)
901
        return "movhi hi(%1),%.,%0";
902
 
903
      else if (GET_CODE (src) == LO_SUM)
904
        {
905
          operands[2] = XEXP (src, 0);
906
          operands[3] = XEXP (src, 1);
907
          return "movea lo(%3),%2,%0";
908
        }
909
    }
910
 
911
  else if (GET_CODE (dst) == MEM)
912
    {
913
      if (REG_P (src))
914
        return "%S0st%W0 %1,%0";
915
 
916
      else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
917
        return "%S0st%W0 %.,%0";
918
 
919
      else if (GET_CODE (src) == CONST_DOUBLE
920
               && CONST0_RTX (GET_MODE (dst)) == src)
921
        return "%S0st%W0 %.,%0";
922
    }
923
 
924
  fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
925
  return "";
926
}
927
 
928
 
929
/* Return appropriate code to load up an 8 byte integer or
930
   floating point value */
931
 
932
const char *
933
output_move_double (rtx * operands)
934
{
935
  enum machine_mode mode = GET_MODE (operands[0]);
936
  rtx dst = operands[0];
937
  rtx src = operands[1];
938
 
939
  if (register_operand (dst, mode)
940
      && register_operand (src, mode))
941
    {
942
      if (REGNO (src) + 1 == REGNO (dst))
943
        return "mov %R1,%R0\n\tmov %1,%0";
944
      else
945
        return "mov %1,%0\n\tmov %R1,%R0";
946
    }
947
 
948
  /* Storing 0 */
949
  if (GET_CODE (dst) == MEM
950
      && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
951
          || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
952
    return "st.w %.,%0\n\tst.w %.,%R0";
953
 
954
  if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
955
    {
956
      HOST_WIDE_INT high_low[2];
957
      int i;
958
      rtx xop[10];
959
 
960
      if (GET_CODE (src) == CONST_DOUBLE)
961
        const_double_split (src, &high_low[1], &high_low[0]);
962
      else
963
        {
964
          high_low[0] = INTVAL (src);
965
          high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
966
        }
967
 
968
      for (i = 0; i < 2; i++)
969
        {
970
          xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
971
          xop[1] = GEN_INT (high_low[i]);
972
          output_asm_insn (output_move_single (xop), xop);
973
        }
974
 
975
      return "";
976
    }
977
 
978
  if (GET_CODE (src) == MEM)
979
    {
980
      int ptrreg = -1;
981
      int dreg = REGNO (dst);
982
      rtx inside = XEXP (src, 0);
983
 
984
      if (GET_CODE (inside) == REG)
985
        ptrreg = REGNO (inside);
986
      else if (GET_CODE (inside) == SUBREG)
987
        ptrreg = subreg_regno (inside);
988
      else if (GET_CODE (inside) == PLUS)
989
        ptrreg = REGNO (XEXP (inside, 0));
990
      else if (GET_CODE (inside) == LO_SUM)
991
        ptrreg = REGNO (XEXP (inside, 0));
992
 
993
      if (dreg == ptrreg)
994
        return "ld.w %R1,%R0\n\tld.w %1,%0";
995
    }
996
 
997
  if (GET_CODE (src) == MEM)
998
    return "ld.w %1,%0\n\tld.w %R1,%R0";
999
 
1000
  if (GET_CODE (dst) == MEM)
1001
    return "st.w %1,%0\n\tst.w %R1,%R0";
1002
 
1003
  return "mov %1,%0\n\tmov %R1,%R0";
1004
}
1005
 
1006
 
1007
/* Return maximum offset supported for a short EP memory reference of mode
1008
   MODE and signedness UNSIGNEDP.  */
1009
 
1010
static int
1011
ep_memory_offset (enum machine_mode mode, int unsignedp ATTRIBUTE_UNUSED)
1012
{
1013
  int max_offset = 0;
1014
 
1015
  switch (mode)
1016
    {
1017
    case QImode:
1018
      if (TARGET_SMALL_SLD)
1019
        max_offset = (1 << 4);
1020
      else if (TARGET_V850E
1021
               && (   (  unsignedp && ! TARGET_US_BIT_SET)
1022
                   || (! unsignedp &&   TARGET_US_BIT_SET)))
1023
        max_offset = (1 << 4);
1024
      else
1025
        max_offset = (1 << 7);
1026
      break;
1027
 
1028
    case HImode:
1029
      if (TARGET_SMALL_SLD)
1030
        max_offset = (1 << 5);
1031
      else if (TARGET_V850E
1032
               && (   (  unsignedp && ! TARGET_US_BIT_SET)
1033
                   || (! unsignedp &&   TARGET_US_BIT_SET)))
1034
        max_offset = (1 << 5);
1035
      else
1036
        max_offset = (1 << 8);
1037
      break;
1038
 
1039
    case SImode:
1040
    case SFmode:
1041
      max_offset = (1 << 8);
1042
      break;
1043
 
1044
    default:
1045
      break;
1046
    }
1047
 
1048
  return max_offset;
1049
}
1050
 
1051
/* Return true if OP is a valid short EP memory reference */
1052
 
1053
int
1054
ep_memory_operand (rtx op, enum machine_mode mode, int unsigned_load)
1055
{
1056
  rtx addr, op0, op1;
1057
  int max_offset;
1058
  int mask;
1059
 
1060
  /* If we are not using the EP register on a per-function basis
1061
     then do not allow this optimisation at all.  This is to
1062
     prevent the use of the SLD/SST instructions which cannot be
1063
     guaranteed to work properly due to a hardware bug.  */
1064
  if (!TARGET_EP)
1065
    return FALSE;
1066
 
1067
  if (GET_CODE (op) != MEM)
1068
    return FALSE;
1069
 
1070
  max_offset = ep_memory_offset (mode, unsigned_load);
1071
 
1072
  mask = GET_MODE_SIZE (mode) - 1;
1073
 
1074
  addr = XEXP (op, 0);
1075
  if (GET_CODE (addr) == CONST)
1076
    addr = XEXP (addr, 0);
1077
 
1078
  switch (GET_CODE (addr))
1079
    {
1080
    default:
1081
      break;
1082
 
1083
    case SYMBOL_REF:
1084
      return SYMBOL_REF_TDA_P (addr);
1085
 
1086
    case REG:
1087
      return REGNO (addr) == EP_REGNUM;
1088
 
1089
    case PLUS:
1090
      op0 = XEXP (addr, 0);
1091
      op1 = XEXP (addr, 1);
1092
      if (GET_CODE (op1) == CONST_INT
1093
          && INTVAL (op1) < max_offset
1094
          && INTVAL (op1) >= 0
1095
          && (INTVAL (op1) & mask) == 0)
1096
        {
1097
          if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1098
            return TRUE;
1099
 
1100
          if (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_TDA_P (op0))
1101
            return TRUE;
1102
        }
1103
      break;
1104
    }
1105
 
1106
  return FALSE;
1107
}
1108
 
1109
/* Substitute memory references involving a pointer, to use the ep pointer,
1110
   taking care to save and preserve the ep.  */
1111
 
1112
static void
1113
substitute_ep_register (rtx first_insn,
1114
                        rtx last_insn,
1115
                        int uses,
1116
                        int regno,
1117
                        rtx * p_r1,
1118
                        rtx * p_ep)
1119
{
1120
  rtx reg = gen_rtx_REG (Pmode, regno);
1121
  rtx insn;
1122
 
1123
  if (!*p_r1)
1124
    {
1125
      regs_ever_live[1] = 1;
1126
      *p_r1 = gen_rtx_REG (Pmode, 1);
1127
      *p_ep = gen_rtx_REG (Pmode, 30);
1128
    }
1129
 
1130
  if (TARGET_DEBUG)
1131
    fprintf (stderr, "\
1132
Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1133
             2 * (uses - 3), uses, reg_names[regno],
1134
             IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1135
             INSN_UID (first_insn), INSN_UID (last_insn));
1136
 
1137
  if (GET_CODE (first_insn) == NOTE)
1138
    first_insn = next_nonnote_insn (first_insn);
1139
 
1140
  last_insn = next_nonnote_insn (last_insn);
1141
  for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1142
    {
1143
      if (GET_CODE (insn) == INSN)
1144
        {
1145
          rtx pattern = single_set (insn);
1146
 
1147
          /* Replace the memory references.  */
1148
          if (pattern)
1149
            {
1150
              rtx *p_mem;
1151
              /* Memory operands are signed by default.  */
1152
              int unsignedp = FALSE;
1153
 
1154
              if (GET_CODE (SET_DEST (pattern)) == MEM
1155
                  && GET_CODE (SET_SRC (pattern)) == MEM)
1156
                p_mem = (rtx *)0;
1157
 
1158
              else if (GET_CODE (SET_DEST (pattern)) == MEM)
1159
                p_mem = &SET_DEST (pattern);
1160
 
1161
              else if (GET_CODE (SET_SRC (pattern)) == MEM)
1162
                p_mem = &SET_SRC (pattern);
1163
 
1164
              else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1165
                       && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1166
                p_mem = &XEXP (SET_SRC (pattern), 0);
1167
 
1168
              else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1169
                       && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1170
                {
1171
                  p_mem = &XEXP (SET_SRC (pattern), 0);
1172
                  unsignedp = TRUE;
1173
                }
1174
              else
1175
                p_mem = (rtx *)0;
1176
 
1177
              if (p_mem)
1178
                {
1179
                  rtx addr = XEXP (*p_mem, 0);
1180
 
1181
                  if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1182
                    *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1183
 
1184
                  else if (GET_CODE (addr) == PLUS
1185
                           && GET_CODE (XEXP (addr, 0)) == REG
1186
                           && REGNO (XEXP (addr, 0)) == (unsigned) regno
1187
                           && GET_CODE (XEXP (addr, 1)) == CONST_INT
1188
                           && ((INTVAL (XEXP (addr, 1)))
1189
                               < ep_memory_offset (GET_MODE (*p_mem),
1190
                                                   unsignedp))
1191
                           && ((INTVAL (XEXP (addr, 1))) >= 0))
1192
                    *p_mem = change_address (*p_mem, VOIDmode,
1193
                                             gen_rtx_PLUS (Pmode,
1194
                                                           *p_ep,
1195
                                                           XEXP (addr, 1)));
1196
                }
1197
            }
1198
        }
1199
    }
1200
 
1201
  /* Optimize back to back cases of ep <- r1 & r1 <- ep.  */
1202
  insn = prev_nonnote_insn (first_insn);
1203
  if (insn && GET_CODE (insn) == INSN
1204
      && GET_CODE (PATTERN (insn)) == SET
1205
      && SET_DEST (PATTERN (insn)) == *p_ep
1206
      && SET_SRC (PATTERN (insn)) == *p_r1)
1207
    delete_insn (insn);
1208
  else
1209
    emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1210
 
1211
  emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1212
  emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1213
}
1214
 
1215
 
1216
/* TARGET_MACHINE_DEPENDENT_REORG.  On the 850, we use it to implement
1217
   the -mep mode to copy heavily used pointers to ep to use the implicit
1218
   addressing.  */
1219
 
1220
static void
1221
v850_reorg (void)
1222
{
1223
  struct
1224
  {
1225
    int uses;
1226
    rtx first_insn;
1227
    rtx last_insn;
1228
  }
1229
  regs[FIRST_PSEUDO_REGISTER];
1230
 
1231
  int i;
1232
  int use_ep = FALSE;
1233
  rtx r1 = NULL_RTX;
1234
  rtx ep = NULL_RTX;
1235
  rtx insn;
1236
  rtx pattern;
1237
 
1238
  /* If not ep mode, just return now.  */
1239
  if (!TARGET_EP)
1240
    return;
1241
 
1242
  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1243
    {
1244
      regs[i].uses = 0;
1245
      regs[i].first_insn = NULL_RTX;
1246
      regs[i].last_insn = NULL_RTX;
1247
    }
1248
 
1249
  for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1250
    {
1251
      switch (GET_CODE (insn))
1252
        {
1253
          /* End of basic block */
1254
        default:
1255
          if (!use_ep)
1256
            {
1257
              int max_uses = -1;
1258
              int max_regno = -1;
1259
 
1260
              for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1261
                {
1262
                  if (max_uses < regs[i].uses)
1263
                    {
1264
                      max_uses = regs[i].uses;
1265
                      max_regno = i;
1266
                    }
1267
                }
1268
 
1269
              if (max_uses > 3)
1270
                substitute_ep_register (regs[max_regno].first_insn,
1271
                                        regs[max_regno].last_insn,
1272
                                        max_uses, max_regno, &r1, &ep);
1273
            }
1274
 
1275
          use_ep = FALSE;
1276
          for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1277
            {
1278
              regs[i].uses = 0;
1279
              regs[i].first_insn = NULL_RTX;
1280
              regs[i].last_insn = NULL_RTX;
1281
            }
1282
          break;
1283
 
1284
        case NOTE:
1285
          break;
1286
 
1287
        case INSN:
1288
          pattern = single_set (insn);
1289
 
1290
          /* See if there are any memory references we can shorten */
1291
          if (pattern)
1292
            {
1293
              rtx src = SET_SRC (pattern);
1294
              rtx dest = SET_DEST (pattern);
1295
              rtx mem;
1296
              /* Memory operands are signed by default.  */
1297
              int unsignedp = FALSE;
1298
 
1299
              /* We might have (SUBREG (MEM)) here, so just get rid of the
1300
                 subregs to make this code simpler.  */
1301
              if (GET_CODE (dest) == SUBREG
1302
                  && (GET_CODE (SUBREG_REG (dest)) == MEM
1303
                      || GET_CODE (SUBREG_REG (dest)) == REG))
1304
                alter_subreg (&dest);
1305
              if (GET_CODE (src) == SUBREG
1306
                  && (GET_CODE (SUBREG_REG (src)) == MEM
1307
                      || GET_CODE (SUBREG_REG (src)) == REG))
1308
                alter_subreg (&src);
1309
 
1310
              if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1311
                mem = NULL_RTX;
1312
 
1313
              else if (GET_CODE (dest) == MEM)
1314
                mem = dest;
1315
 
1316
              else if (GET_CODE (src) == MEM)
1317
                mem = src;
1318
 
1319
              else if (GET_CODE (src) == SIGN_EXTEND
1320
                       && GET_CODE (XEXP (src, 0)) == MEM)
1321
                mem = XEXP (src, 0);
1322
 
1323
              else if (GET_CODE (src) == ZERO_EXTEND
1324
                       && GET_CODE (XEXP (src, 0)) == MEM)
1325
                {
1326
                  mem = XEXP (src, 0);
1327
                  unsignedp = TRUE;
1328
                }
1329
              else
1330
                mem = NULL_RTX;
1331
 
1332
              if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1333
                use_ep = TRUE;
1334
 
1335
              else if (!use_ep && mem
1336
                       && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1337
                {
1338
                  rtx addr = XEXP (mem, 0);
1339
                  int regno = -1;
1340
                  int short_p;
1341
 
1342
                  if (GET_CODE (addr) == REG)
1343
                    {
1344
                      short_p = TRUE;
1345
                      regno = REGNO (addr);
1346
                    }
1347
 
1348
                  else if (GET_CODE (addr) == PLUS
1349
                           && GET_CODE (XEXP (addr, 0)) == REG
1350
                           && GET_CODE (XEXP (addr, 1)) == CONST_INT
1351
                           && ((INTVAL (XEXP (addr, 1)))
1352
                               < ep_memory_offset (GET_MODE (mem), unsignedp))
1353
                           && ((INTVAL (XEXP (addr, 1))) >= 0))
1354
                    {
1355
                      short_p = TRUE;
1356
                      regno = REGNO (XEXP (addr, 0));
1357
                    }
1358
 
1359
                  else
1360
                    short_p = FALSE;
1361
 
1362
                  if (short_p)
1363
                    {
1364
                      regs[regno].uses++;
1365
                      regs[regno].last_insn = insn;
1366
                      if (!regs[regno].first_insn)
1367
                        regs[regno].first_insn = insn;
1368
                    }
1369
                }
1370
 
1371
              /* Loading up a register in the basic block zaps any savings
1372
                 for the register */
1373
              if (GET_CODE (dest) == REG)
1374
                {
1375
                  enum machine_mode mode = GET_MODE (dest);
1376
                  int regno;
1377
                  int endregno;
1378
 
1379
                  regno = REGNO (dest);
1380
                  endregno = regno + HARD_REGNO_NREGS (regno, mode);
1381
 
1382
                  if (!use_ep)
1383
                    {
1384
                      /* See if we can use the pointer before this
1385
                         modification.  */
1386
                      int max_uses = -1;
1387
                      int max_regno = -1;
1388
 
1389
                      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1390
                        {
1391
                          if (max_uses < regs[i].uses)
1392
                            {
1393
                              max_uses = regs[i].uses;
1394
                              max_regno = i;
1395
                            }
1396
                        }
1397
 
1398
                      if (max_uses > 3
1399
                          && max_regno >= regno
1400
                          && max_regno < endregno)
1401
                        {
1402
                          substitute_ep_register (regs[max_regno].first_insn,
1403
                                                  regs[max_regno].last_insn,
1404
                                                  max_uses, max_regno, &r1,
1405
                                                  &ep);
1406
 
1407
                          /* Since we made a substitution, zap all remembered
1408
                             registers.  */
1409
                          for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1410
                            {
1411
                              regs[i].uses = 0;
1412
                              regs[i].first_insn = NULL_RTX;
1413
                              regs[i].last_insn = NULL_RTX;
1414
                            }
1415
                        }
1416
                    }
1417
 
1418
                  for (i = regno; i < endregno; i++)
1419
                    {
1420
                      regs[i].uses = 0;
1421
                      regs[i].first_insn = NULL_RTX;
1422
                      regs[i].last_insn = NULL_RTX;
1423
                    }
1424
                }
1425
            }
1426
        }
1427
    }
1428
}
1429
 
1430
 
1431
/* # of registers saved by the interrupt handler.  */
1432
#define INTERRUPT_FIXED_NUM 4
1433
 
1434
/* # of bytes for registers saved by the interrupt handler.  */
1435
#define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1436
 
1437
/* # of registers saved in register parameter area.  */
1438
#define INTERRUPT_REGPARM_NUM 4
1439
/* # of words saved for other registers.  */
1440
#define INTERRUPT_ALL_SAVE_NUM \
1441
  (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1442
 
1443
#define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1444
 
1445
int
1446
compute_register_save_size (long * p_reg_saved)
1447
{
1448
  int size = 0;
1449
  int i;
1450
  int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1451
  int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1452
  long reg_saved = 0;
1453
 
1454
  /* Count the return pointer if we need to save it.  */
1455
  if (current_function_profile && !call_p)
1456
    regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1457
 
1458
  /* Count space for the register saves.  */
1459
  if (interrupt_handler)
1460
    {
1461
      for (i = 0; i <= 31; i++)
1462
        switch (i)
1463
          {
1464
          default:
1465
            if (regs_ever_live[i] || call_p)
1466
              {
1467
                size += 4;
1468
                reg_saved |= 1L << i;
1469
              }
1470
            break;
1471
 
1472
            /* We don't save/restore r0 or the stack pointer */
1473
          case 0:
1474
          case STACK_POINTER_REGNUM:
1475
            break;
1476
 
1477
            /* For registers with fixed use, we save them, set them to the
1478
               appropriate value, and then restore them.
1479
               These registers are handled specially, so don't list them
1480
               on the list of registers to save in the prologue.  */
1481
          case 1:               /* temp used to hold ep */
1482
          case 4:               /* gp */
1483
          case 10:              /* temp used to call interrupt save/restore */
1484
          case EP_REGNUM:       /* ep */
1485
            size += 4;
1486
            break;
1487
          }
1488
    }
1489
  else
1490
    {
1491
      /* Find the first register that needs to be saved.  */
1492
      for (i = 0; i <= 31; i++)
1493
        if (regs_ever_live[i] && ((! call_used_regs[i])
1494
                                  || i == LINK_POINTER_REGNUM))
1495
          break;
1496
 
1497
      /* If it is possible that an out-of-line helper function might be
1498
         used to generate the prologue for the current function, then we
1499
         need to cover the possibility that such a helper function will
1500
         be used, despite the fact that there might be gaps in the list of
1501
         registers that need to be saved.  To detect this we note that the
1502
         helper functions always push at least register r29 (provided
1503
         that the function is not an interrupt handler).  */
1504
 
1505
      if (TARGET_PROLOG_FUNCTION
1506
          && (i == 2 || ((i >= 20) && (i < 30))))
1507
        {
1508
          if (i == 2)
1509
            {
1510
              size += 4;
1511
              reg_saved |= 1L << i;
1512
 
1513
              i = 20;
1514
            }
1515
 
1516
          /* Helper functions save all registers between the starting
1517
             register and the last register, regardless of whether they
1518
             are actually used by the function or not.  */
1519
          for (; i <= 29; i++)
1520
            {
1521
              size += 4;
1522
              reg_saved |= 1L << i;
1523
            }
1524
 
1525
          if (regs_ever_live [LINK_POINTER_REGNUM])
1526
            {
1527
              size += 4;
1528
              reg_saved |= 1L << LINK_POINTER_REGNUM;
1529
            }
1530
        }
1531
      else
1532
        {
1533
          for (; i <= 31; i++)
1534
            if (regs_ever_live[i] && ((! call_used_regs[i])
1535
                                      || i == LINK_POINTER_REGNUM))
1536
              {
1537
                size += 4;
1538
                reg_saved |= 1L << i;
1539
              }
1540
        }
1541
    }
1542
 
1543
  if (p_reg_saved)
1544
    *p_reg_saved = reg_saved;
1545
 
1546
  return size;
1547
}
1548
 
1549
int
1550
compute_frame_size (int size, long * p_reg_saved)
1551
{
1552
  return (size
1553
          + compute_register_save_size (p_reg_saved)
1554
          + current_function_outgoing_args_size);
1555
}
1556
 
1557
 
1558
void
1559
expand_prologue (void)
1560
{
1561
  unsigned int i;
1562
  int offset;
1563
  unsigned int size = get_frame_size ();
1564
  unsigned int actual_fsize;
1565
  unsigned int init_stack_alloc = 0;
1566
  rtx save_regs[32];
1567
  rtx save_all;
1568
  unsigned int num_save;
1569
  unsigned int default_stack;
1570
  int code;
1571
  int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1572
  long reg_saved = 0;
1573
 
1574
  actual_fsize = compute_frame_size (size, &reg_saved);
1575
 
1576
  /* Save/setup global registers for interrupt functions right now.  */
1577
  if (interrupt_handler)
1578
    {
1579
      if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1580
        emit_insn (gen_callt_save_interrupt ());
1581
      else
1582
        emit_insn (gen_save_interrupt ());
1583
 
1584
      actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1585
 
1586
      if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1587
        actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1588
    }
1589
 
1590
  /* Save arg registers to the stack if necessary.  */
1591
  else if (current_function_args_info.anonymous_args)
1592
    {
1593
      if (TARGET_PROLOG_FUNCTION && TARGET_V850E && !TARGET_DISABLE_CALLT)
1594
        emit_insn (gen_save_r6_r9_v850e ());
1595
      else if (TARGET_PROLOG_FUNCTION && ! TARGET_LONG_CALLS)
1596
        emit_insn (gen_save_r6_r9 ());
1597
      else
1598
        {
1599
          offset = 0;
1600
          for (i = 6; i < 10; i++)
1601
            {
1602
              emit_move_insn (gen_rtx_MEM (SImode,
1603
                                           plus_constant (stack_pointer_rtx,
1604
                                                          offset)),
1605
                              gen_rtx_REG (SImode, i));
1606
              offset += 4;
1607
            }
1608
        }
1609
    }
1610
 
1611
  /* Identify all of the saved registers.  */
1612
  num_save = 0;
1613
  default_stack = 0;
1614
  for (i = 1; i < 31; i++)
1615
    {
1616
      if (((1L << i) & reg_saved) != 0)
1617
        save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1618
    }
1619
 
1620
  /* If the return pointer is saved, the helper functions also allocate
1621
     16 bytes of stack for arguments to be saved in.  */
1622
  if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1623
    {
1624
      save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1625
      default_stack = 16;
1626
    }
1627
 
1628
  /* See if we have an insn that allocates stack space and saves the particular
1629
     registers we want to.  */
1630
  save_all = NULL_RTX;
1631
  if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1632
    {
1633
      int alloc_stack = (4 * num_save) + default_stack;
1634
      int unalloc_stack = actual_fsize - alloc_stack;
1635
      int save_func_len = 4;
1636
      int save_normal_len;
1637
 
1638
      if (unalloc_stack)
1639
        save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1640
 
1641
      /* see if we would have used ep to save the stack */
1642
      if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1643
        save_normal_len = (3 * 2) + (2 * num_save);
1644
      else
1645
        save_normal_len = 4 * num_save;
1646
 
1647
      save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1648
 
1649
      /* Don't bother checking if we don't actually save any space.
1650
         This happens for instance if one register is saved and additional
1651
         stack space is allocated.  */
1652
      if (save_func_len < save_normal_len)
1653
        {
1654
          save_all = gen_rtx_PARALLEL
1655
            (VOIDmode,
1656
             rtvec_alloc (num_save + 1
1657
                          + (TARGET_V850 ? (TARGET_LONG_CALLS ? 2 : 1) : 0)));
1658
 
1659
          XVECEXP (save_all, 0, 0)
1660
            = gen_rtx_SET (VOIDmode,
1661
                           stack_pointer_rtx,
1662
                           plus_constant (stack_pointer_rtx, -alloc_stack));
1663
 
1664
          offset = - default_stack;
1665
          for (i = 0; i < num_save; i++)
1666
            {
1667
              XVECEXP (save_all, 0, i+1)
1668
                = gen_rtx_SET (VOIDmode,
1669
                               gen_rtx_MEM (Pmode,
1670
                                            plus_constant (stack_pointer_rtx,
1671
                                                           offset)),
1672
                               save_regs[i]);
1673
              offset -= 4;
1674
            }
1675
 
1676
          if (TARGET_V850)
1677
            {
1678
              XVECEXP (save_all, 0, num_save + 1)
1679
                = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1680
 
1681
              if (TARGET_LONG_CALLS)
1682
                XVECEXP (save_all, 0, num_save + 2)
1683
                  = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
1684
            }
1685
 
1686
          code = recog (save_all, NULL_RTX, NULL);
1687
          if (code >= 0)
1688
            {
1689
              rtx insn = emit_insn (save_all);
1690
              INSN_CODE (insn) = code;
1691
              actual_fsize -= alloc_stack;
1692
 
1693
              if (TARGET_DEBUG)
1694
                fprintf (stderr, "\
1695
Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1696
                         save_normal_len - save_func_len,
1697
                         save_normal_len, save_func_len,
1698
                         IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1699
            }
1700
          else
1701
            save_all = NULL_RTX;
1702
        }
1703
    }
1704
 
1705
  /* If no prolog save function is available, store the registers the old
1706
     fashioned way (one by one).  */
1707
  if (!save_all)
1708
    {
1709
      /* Special case interrupt functions that save all registers for a call.  */
1710
      if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1711
        {
1712
          if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1713
            emit_insn (gen_callt_save_all_interrupt ());
1714
          else
1715
            emit_insn (gen_save_all_interrupt ());
1716
        }
1717
      else
1718
        {
1719
          /* If the stack is too big, allocate it in chunks so we can do the
1720
             register saves.  We use the register save size so we use the ep
1721
             register.  */
1722
          if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1723
            init_stack_alloc = compute_register_save_size (NULL);
1724
          else
1725
            init_stack_alloc = actual_fsize;
1726
 
1727
          /* Save registers at the beginning of the stack frame.  */
1728
          offset = init_stack_alloc - 4;
1729
 
1730
          if (init_stack_alloc)
1731
            emit_insn (gen_addsi3 (stack_pointer_rtx,
1732
                                   stack_pointer_rtx,
1733
                                   GEN_INT (-init_stack_alloc)));
1734
 
1735
          /* Save the return pointer first.  */
1736
          if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1737
            {
1738
              emit_move_insn (gen_rtx_MEM (SImode,
1739
                                           plus_constant (stack_pointer_rtx,
1740
                                                          offset)),
1741
                              save_regs[--num_save]);
1742
              offset -= 4;
1743
            }
1744
 
1745
          for (i = 0; i < num_save; i++)
1746
            {
1747
              emit_move_insn (gen_rtx_MEM (SImode,
1748
                                           plus_constant (stack_pointer_rtx,
1749
                                                          offset)),
1750
                              save_regs[i]);
1751
              offset -= 4;
1752
            }
1753
        }
1754
    }
1755
 
1756
  /* Allocate the rest of the stack that was not allocated above (either it is
1757
     > 32K or we just called a function to save the registers and needed more
1758
     stack.  */
1759
  if (actual_fsize > init_stack_alloc)
1760
    {
1761
      int diff = actual_fsize - init_stack_alloc;
1762
      if (CONST_OK_FOR_K (diff))
1763
        emit_insn (gen_addsi3 (stack_pointer_rtx,
1764
                               stack_pointer_rtx,
1765
                               GEN_INT (-diff)));
1766
      else
1767
        {
1768
          rtx reg = gen_rtx_REG (Pmode, 12);
1769
          emit_move_insn (reg, GEN_INT (-diff));
1770
          emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1771
        }
1772
    }
1773
 
1774
  /* If we need a frame pointer, set it up now.  */
1775
  if (frame_pointer_needed)
1776
    emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1777
}
1778
 
1779
 
1780
void
1781
expand_epilogue (void)
1782
{
1783
  unsigned int i;
1784
  int offset;
1785
  unsigned int size = get_frame_size ();
1786
  long reg_saved = 0;
1787
  unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1788
  unsigned int init_stack_free = 0;
1789
  rtx restore_regs[32];
1790
  rtx restore_all;
1791
  unsigned int num_restore;
1792
  unsigned int default_stack;
1793
  int code;
1794
  int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1795
 
1796
  /* Eliminate the initial stack stored by interrupt functions.  */
1797
  if (interrupt_handler)
1798
    {
1799
      actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1800
      if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1801
        actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1802
    }
1803
 
1804
  /* Cut off any dynamic stack created.  */
1805
  if (frame_pointer_needed)
1806
    emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1807
 
1808
  /* Identify all of the saved registers.  */
1809
  num_restore = 0;
1810
  default_stack = 0;
1811
  for (i = 1; i < 31; i++)
1812
    {
1813
      if (((1L << i) & reg_saved) != 0)
1814
        restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1815
    }
1816
 
1817
  /* If the return pointer is saved, the helper functions also allocate
1818
     16 bytes of stack for arguments to be saved in.  */
1819
  if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1820
    {
1821
      restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1822
      default_stack = 16;
1823
    }
1824
 
1825
  /* See if we have an insn that restores the particular registers we
1826
     want to.  */
1827
  restore_all = NULL_RTX;
1828
 
1829
  if (TARGET_PROLOG_FUNCTION
1830
      && num_restore > 0
1831
      && actual_fsize >= default_stack
1832
      && !interrupt_handler)
1833
    {
1834
      int alloc_stack = (4 * num_restore) + default_stack;
1835
      int unalloc_stack = actual_fsize - alloc_stack;
1836
      int restore_func_len = 4;
1837
      int restore_normal_len;
1838
 
1839
      if (unalloc_stack)
1840
        restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1841
 
1842
      /* See if we would have used ep to restore the registers.  */
1843
      if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1844
        restore_normal_len = (3 * 2) + (2 * num_restore);
1845
      else
1846
        restore_normal_len = 4 * num_restore;
1847
 
1848
      restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1849
 
1850
      /* Don't bother checking if we don't actually save any space.  */
1851
      if (restore_func_len < restore_normal_len)
1852
        {
1853
          restore_all = gen_rtx_PARALLEL (VOIDmode,
1854
                                          rtvec_alloc (num_restore + 2));
1855
          XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1856
          XVECEXP (restore_all, 0, 1)
1857
            = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1858
                            gen_rtx_PLUS (Pmode,
1859
                                          stack_pointer_rtx,
1860
                                          GEN_INT (alloc_stack)));
1861
 
1862
          offset = alloc_stack - 4;
1863
          for (i = 0; i < num_restore; i++)
1864
            {
1865
              XVECEXP (restore_all, 0, i+2)
1866
                = gen_rtx_SET (VOIDmode,
1867
                               restore_regs[i],
1868
                               gen_rtx_MEM (Pmode,
1869
                                            plus_constant (stack_pointer_rtx,
1870
                                                           offset)));
1871
              offset -= 4;
1872
            }
1873
 
1874
          code = recog (restore_all, NULL_RTX, NULL);
1875
 
1876
          if (code >= 0)
1877
            {
1878
              rtx insn;
1879
 
1880
              actual_fsize -= alloc_stack;
1881
              if (actual_fsize)
1882
                {
1883
                  if (CONST_OK_FOR_K (actual_fsize))
1884
                    emit_insn (gen_addsi3 (stack_pointer_rtx,
1885
                                           stack_pointer_rtx,
1886
                                           GEN_INT (actual_fsize)));
1887
                  else
1888
                    {
1889
                      rtx reg = gen_rtx_REG (Pmode, 12);
1890
                      emit_move_insn (reg, GEN_INT (actual_fsize));
1891
                      emit_insn (gen_addsi3 (stack_pointer_rtx,
1892
                                             stack_pointer_rtx,
1893
                                             reg));
1894
                    }
1895
                }
1896
 
1897
              insn = emit_jump_insn (restore_all);
1898
              INSN_CODE (insn) = code;
1899
 
1900
              if (TARGET_DEBUG)
1901
                fprintf (stderr, "\
1902
Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1903
                         restore_normal_len - restore_func_len,
1904
                         restore_normal_len, restore_func_len,
1905
                         IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1906
            }
1907
          else
1908
            restore_all = NULL_RTX;
1909
        }
1910
    }
1911
 
1912
  /* If no epilog save function is available, restore the registers the
1913
     old fashioned way (one by one).  */
1914
  if (!restore_all)
1915
    {
1916
      /* If the stack is large, we need to cut it down in 2 pieces.  */
1917
      if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1918
        init_stack_free = 4 * num_restore;
1919
      else
1920
        init_stack_free = actual_fsize;
1921
 
1922
      /* Deallocate the rest of the stack if it is > 32K.  */
1923
      if (actual_fsize > init_stack_free)
1924
        {
1925
          int diff;
1926
 
1927
          diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1928
 
1929
          if (CONST_OK_FOR_K (diff))
1930
            emit_insn (gen_addsi3 (stack_pointer_rtx,
1931
                                   stack_pointer_rtx,
1932
                                   GEN_INT (diff)));
1933
          else
1934
            {
1935
              rtx reg = gen_rtx_REG (Pmode, 12);
1936
              emit_move_insn (reg, GEN_INT (diff));
1937
              emit_insn (gen_addsi3 (stack_pointer_rtx,
1938
                                     stack_pointer_rtx,
1939
                                     reg));
1940
            }
1941
        }
1942
 
1943
      /* Special case interrupt functions that save all registers
1944
         for a call.  */
1945
      if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1946
        {
1947
          if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1948
            emit_insn (gen_callt_restore_all_interrupt ());
1949
          else
1950
            emit_insn (gen_restore_all_interrupt ());
1951
        }
1952
      else
1953
        {
1954
          /* Restore registers from the beginning of the stack frame.  */
1955
          offset = init_stack_free - 4;
1956
 
1957
          /* Restore the return pointer first.  */
1958
          if (num_restore > 0
1959
              && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
1960
            {
1961
              emit_move_insn (restore_regs[--num_restore],
1962
                              gen_rtx_MEM (SImode,
1963
                                           plus_constant (stack_pointer_rtx,
1964
                                                          offset)));
1965
              offset -= 4;
1966
            }
1967
 
1968
          for (i = 0; i < num_restore; i++)
1969
            {
1970
              emit_move_insn (restore_regs[i],
1971
                              gen_rtx_MEM (SImode,
1972
                                           plus_constant (stack_pointer_rtx,
1973
                                                          offset)));
1974
 
1975
              emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
1976
              offset -= 4;
1977
            }
1978
 
1979
          /* Cut back the remainder of the stack.  */
1980
          if (init_stack_free)
1981
            emit_insn (gen_addsi3 (stack_pointer_rtx,
1982
                                   stack_pointer_rtx,
1983
                                   GEN_INT (init_stack_free)));
1984
        }
1985
 
1986
      /* And return or use reti for interrupt handlers.  */
1987
      if (interrupt_handler)
1988
        {
1989
          if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1990
            emit_insn (gen_callt_return_interrupt ());
1991
          else
1992
            emit_jump_insn (gen_return_interrupt ());
1993
         }
1994
      else if (actual_fsize)
1995
        emit_jump_insn (gen_return_internal ());
1996
      else
1997
        emit_jump_insn (gen_return ());
1998
    }
1999
 
2000
  v850_interrupt_cache_p = FALSE;
2001
  v850_interrupt_p = FALSE;
2002
}
2003
 
2004
 
2005
/* Update the condition code from the insn.  */
2006
 
2007
void
2008
notice_update_cc (rtx body, rtx insn)
2009
{
2010
  switch (get_attr_cc (insn))
2011
    {
2012
    case CC_NONE:
2013
      /* Insn does not affect CC at all.  */
2014
      break;
2015
 
2016
    case CC_NONE_0HIT:
2017
      /* Insn does not change CC, but the 0'th operand has been changed.  */
2018
      if (cc_status.value1 != 0
2019
          && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2020
        cc_status.value1 = 0;
2021
      break;
2022
 
2023
    case CC_SET_ZN:
2024
      /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2025
         V,C is in an unusable state.  */
2026
      CC_STATUS_INIT;
2027
      cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2028
      cc_status.value1 = recog_data.operand[0];
2029
      break;
2030
 
2031
    case CC_SET_ZNV:
2032
      /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2033
         C is in an unusable state.  */
2034
      CC_STATUS_INIT;
2035
      cc_status.flags |= CC_NO_CARRY;
2036
      cc_status.value1 = recog_data.operand[0];
2037
      break;
2038
 
2039
    case CC_COMPARE:
2040
      /* The insn is a compare instruction.  */
2041
      CC_STATUS_INIT;
2042
      cc_status.value1 = SET_SRC (body);
2043
      break;
2044
 
2045
    case CC_CLOBBER:
2046
      /* Insn doesn't leave CC in a usable state.  */
2047
      CC_STATUS_INIT;
2048
      break;
2049
    }
2050
}
2051
 
2052
/* Retrieve the data area that has been chosen for the given decl.  */
2053
 
2054
v850_data_area
2055
v850_get_data_area (tree decl)
2056
{
2057
  if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2058
    return DATA_AREA_SDA;
2059
 
2060
  if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2061
    return DATA_AREA_TDA;
2062
 
2063
  if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2064
    return DATA_AREA_ZDA;
2065
 
2066
  return DATA_AREA_NORMAL;
2067
}
2068
 
2069
/* Store the indicated data area in the decl's attributes.  */
2070
 
2071
static void
2072
v850_set_data_area (tree decl, v850_data_area data_area)
2073
{
2074
  tree name;
2075
 
2076
  switch (data_area)
2077
    {
2078
    case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2079
    case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2080
    case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2081
    default:
2082
      return;
2083
    }
2084
 
2085
  DECL_ATTRIBUTES (decl) = tree_cons
2086
    (name, NULL, DECL_ATTRIBUTES (decl));
2087
}
2088
 
2089
const struct attribute_spec v850_attribute_table[] =
2090
{
2091
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2092
  { "interrupt_handler", 0, 0, true,  false, false, v850_handle_interrupt_attribute },
2093
  { "interrupt",         0, 0, true,  false, false, v850_handle_interrupt_attribute },
2094
  { "sda",               0, 0, true,  false, false, v850_handle_data_area_attribute },
2095
  { "tda",               0, 0, true,  false, false, v850_handle_data_area_attribute },
2096
  { "zda",               0, 0, true,  false, false, v850_handle_data_area_attribute },
2097
  { NULL,                0, 0, false, false, false, NULL }
2098
};
2099
 
2100
/* Handle an "interrupt" attribute; arguments as in
2101
   struct attribute_spec.handler.  */
2102
static tree
2103
v850_handle_interrupt_attribute (tree * node,
2104
                                 tree name,
2105
                                 tree args ATTRIBUTE_UNUSED,
2106
                                 int flags ATTRIBUTE_UNUSED,
2107
                                 bool * no_add_attrs)
2108
{
2109
  if (TREE_CODE (*node) != FUNCTION_DECL)
2110
    {
2111
      warning (OPT_Wattributes, "%qs attribute only applies to functions",
2112
               IDENTIFIER_POINTER (name));
2113
      *no_add_attrs = true;
2114
    }
2115
 
2116
  return NULL_TREE;
2117
}
2118
 
2119
/* Handle a "sda", "tda" or "zda" attribute; arguments as in
2120
   struct attribute_spec.handler.  */
2121
static tree
2122
v850_handle_data_area_attribute (tree* node,
2123
                                 tree name,
2124
                                 tree args ATTRIBUTE_UNUSED,
2125
                                 int flags ATTRIBUTE_UNUSED,
2126
                                 bool * no_add_attrs)
2127
{
2128
  v850_data_area data_area;
2129
  v850_data_area area;
2130
  tree decl = *node;
2131
 
2132
  /* Implement data area attribute.  */
2133
  if (is_attribute_p ("sda", name))
2134
    data_area = DATA_AREA_SDA;
2135
  else if (is_attribute_p ("tda", name))
2136
    data_area = DATA_AREA_TDA;
2137
  else if (is_attribute_p ("zda", name))
2138
    data_area = DATA_AREA_ZDA;
2139
  else
2140
    gcc_unreachable ();
2141
 
2142
  switch (TREE_CODE (decl))
2143
    {
2144
    case VAR_DECL:
2145
      if (current_function_decl != NULL_TREE)
2146
        {
2147
          error ("%Jdata area attributes cannot be specified for "
2148
                 "local variables", decl);
2149
          *no_add_attrs = true;
2150
        }
2151
 
2152
      /* Drop through.  */
2153
 
2154
    case FUNCTION_DECL:
2155
      area = v850_get_data_area (decl);
2156
      if (area != DATA_AREA_NORMAL && data_area != area)
2157
        {
2158
          error ("data area of %q+D conflicts with previous declaration",
2159
                 decl);
2160
          *no_add_attrs = true;
2161
        }
2162
      break;
2163
 
2164
    default:
2165
      break;
2166
    }
2167
 
2168
  return NULL_TREE;
2169
}
2170
 
2171
 
2172
/* Return nonzero if FUNC is an interrupt function as specified
2173
   by the "interrupt" attribute.  */
2174
 
2175
int
2176
v850_interrupt_function_p (tree func)
2177
{
2178
  tree a;
2179
  int ret = 0;
2180
 
2181
  if (v850_interrupt_cache_p)
2182
    return v850_interrupt_p;
2183
 
2184
  if (TREE_CODE (func) != FUNCTION_DECL)
2185
    return 0;
2186
 
2187
  a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2188
  if (a != NULL_TREE)
2189
    ret = 1;
2190
 
2191
  else
2192
    {
2193
      a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2194
      ret = a != NULL_TREE;
2195
    }
2196
 
2197
  /* Its not safe to trust global variables until after function inlining has
2198
     been done.  */
2199
  if (reload_completed | reload_in_progress)
2200
    v850_interrupt_p = ret;
2201
 
2202
  return ret;
2203
}
2204
 
2205
 
2206
static void
2207
v850_encode_data_area (tree decl, rtx symbol)
2208
{
2209
  int flags;
2210
 
2211
  /* Map explicit sections into the appropriate attribute */
2212
  if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2213
    {
2214
      if (DECL_SECTION_NAME (decl))
2215
        {
2216
          const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2217
 
2218
          if (streq (name, ".zdata") || streq (name, ".zbss"))
2219
            v850_set_data_area (decl, DATA_AREA_ZDA);
2220
 
2221
          else if (streq (name, ".sdata") || streq (name, ".sbss"))
2222
            v850_set_data_area (decl, DATA_AREA_SDA);
2223
 
2224
          else if (streq (name, ".tdata"))
2225
            v850_set_data_area (decl, DATA_AREA_TDA);
2226
        }
2227
 
2228
      /* If no attribute, support -m{zda,sda,tda}=n */
2229
      else
2230
        {
2231
          int size = int_size_in_bytes (TREE_TYPE (decl));
2232
          if (size <= 0)
2233
            ;
2234
 
2235
          else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2236
            v850_set_data_area (decl, DATA_AREA_TDA);
2237
 
2238
          else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2239
            v850_set_data_area (decl, DATA_AREA_SDA);
2240
 
2241
          else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2242
            v850_set_data_area (decl, DATA_AREA_ZDA);
2243
        }
2244
 
2245
      if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2246
        return;
2247
    }
2248
 
2249
  flags = SYMBOL_REF_FLAGS (symbol);
2250
  switch (v850_get_data_area (decl))
2251
    {
2252
    case DATA_AREA_ZDA: flags |= SYMBOL_FLAG_ZDA; break;
2253
    case DATA_AREA_TDA: flags |= SYMBOL_FLAG_TDA; break;
2254
    case DATA_AREA_SDA: flags |= SYMBOL_FLAG_SDA; break;
2255
    default: gcc_unreachable ();
2256
    }
2257
  SYMBOL_REF_FLAGS (symbol) = flags;
2258
}
2259
 
2260
static void
2261
v850_encode_section_info (tree decl, rtx rtl, int first)
2262
{
2263
  default_encode_section_info (decl, rtl, first);
2264
 
2265
  if (TREE_CODE (decl) == VAR_DECL
2266
      && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2267
    v850_encode_data_area (decl, XEXP (rtl, 0));
2268
}
2269
 
2270
/* Construct a JR instruction to a routine that will perform the equivalent of
2271
   the RTL passed in as an argument.  This RTL is a function epilogue that
2272
   pops registers off the stack and possibly releases some extra stack space
2273
   as well.  The code has already verified that the RTL matches these
2274
   requirements.  */
2275
char *
2276
construct_restore_jr (rtx op)
2277
{
2278
  int count = XVECLEN (op, 0);
2279
  int stack_bytes;
2280
  unsigned long int mask;
2281
  unsigned long int first;
2282
  unsigned long int last;
2283
  int i;
2284
  static char buff [100]; /* XXX */
2285
 
2286
  if (count <= 2)
2287
    {
2288
      error ("bogus JR construction: %d", count);
2289
      return NULL;
2290
    }
2291
 
2292
  /* Work out how many bytes to pop off the stack before retrieving
2293
     registers.  */
2294
  gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2295
  gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2296
  gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2297
 
2298
  stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2299
 
2300
  /* Each pop will remove 4 bytes from the stack....  */
2301
  stack_bytes -= (count - 2) * 4;
2302
 
2303
  /* Make sure that the amount we are popping either 0 or 16 bytes.  */
2304
  if (stack_bytes != 0 && stack_bytes != 16)
2305
    {
2306
      error ("bad amount of stack space removal: %d", stack_bytes);
2307
      return NULL;
2308
    }
2309
 
2310
  /* Now compute the bit mask of registers to push.  */
2311
  mask = 0;
2312
  for (i = 2; i < count; i++)
2313
    {
2314
      rtx vector_element = XVECEXP (op, 0, i);
2315
 
2316
      gcc_assert (GET_CODE (vector_element) == SET);
2317
      gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2318
      gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2319
                                               SImode));
2320
 
2321
      mask |= 1 << REGNO (SET_DEST (vector_element));
2322
    }
2323
 
2324
  /* Scan for the first register to pop.  */
2325
  for (first = 0; first < 32; first++)
2326
    {
2327
      if (mask & (1 << first))
2328
        break;
2329
    }
2330
 
2331
  gcc_assert (first < 32);
2332
 
2333
  /* Discover the last register to pop.  */
2334
  if (mask & (1 << LINK_POINTER_REGNUM))
2335
    {
2336
      gcc_assert (stack_bytes == 16);
2337
 
2338
      last = LINK_POINTER_REGNUM;
2339
    }
2340
  else
2341
    {
2342
      gcc_assert (!stack_bytes);
2343
      gcc_assert (mask & (1 << 29));
2344
 
2345
      last = 29;
2346
    }
2347
 
2348
  /* Note, it is possible to have gaps in the register mask.
2349
     We ignore this here, and generate a JR anyway.  We will
2350
     be popping more registers than is strictly necessary, but
2351
     it does save code space.  */
2352
 
2353
  if (TARGET_LONG_CALLS)
2354
    {
2355
      char name[40];
2356
 
2357
      if (first == last)
2358
        sprintf (name, "__return_%s", reg_names [first]);
2359
      else
2360
        sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2361
 
2362
      sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2363
               name, name);
2364
    }
2365
  else
2366
    {
2367
      if (first == last)
2368
        sprintf (buff, "jr __return_%s", reg_names [first]);
2369
      else
2370
        sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2371
    }
2372
 
2373
  return buff;
2374
}
2375
 
2376
 
2377
/* Construct a JARL instruction to a routine that will perform the equivalent
2378
   of the RTL passed as a parameter.  This RTL is a function prologue that
2379
   saves some of the registers r20 - r31 onto the stack, and possibly acquires
2380
   some stack space as well.  The code has already verified that the RTL
2381
   matches these requirements.  */
2382
char *
2383
construct_save_jarl (rtx op)
2384
{
2385
  int count = XVECLEN (op, 0);
2386
  int stack_bytes;
2387
  unsigned long int mask;
2388
  unsigned long int first;
2389
  unsigned long int last;
2390
  int i;
2391
  static char buff [100]; /* XXX */
2392
 
2393
  if (count <= 2)
2394
    {
2395
      error ("bogus JARL construction: %d\n", count);
2396
      return NULL;
2397
    }
2398
 
2399
  /* Paranoia.  */
2400
  gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2401
  gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2402
  gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) == REG);
2403
  gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2404
 
2405
  /* Work out how many bytes to push onto the stack after storing the
2406
     registers.  */
2407
  stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2408
 
2409
  /* Each push will put 4 bytes from the stack....  */
2410
  stack_bytes += (count - (TARGET_LONG_CALLS ? 3 : 2)) * 4;
2411
 
2412
  /* Make sure that the amount we are popping either 0 or 16 bytes.  */
2413
  if (stack_bytes != 0 && stack_bytes != -16)
2414
    {
2415
      error ("bad amount of stack space removal: %d", stack_bytes);
2416
      return NULL;
2417
    }
2418
 
2419
  /* Now compute the bit mask of registers to push.  */
2420
  mask = 0;
2421
  for (i = 1; i < count - (TARGET_LONG_CALLS ? 2 : 1); i++)
2422
    {
2423
      rtx vector_element = XVECEXP (op, 0, i);
2424
 
2425
      gcc_assert (GET_CODE (vector_element) == SET);
2426
      gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2427
      gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2428
                                               SImode));
2429
 
2430
      mask |= 1 << REGNO (SET_SRC (vector_element));
2431
    }
2432
 
2433
  /* Scan for the first register to push.  */
2434
  for (first = 0; first < 32; first++)
2435
    {
2436
      if (mask & (1 << first))
2437
        break;
2438
    }
2439
 
2440
  gcc_assert (first < 32);
2441
 
2442
  /* Discover the last register to push.  */
2443
  if (mask & (1 << LINK_POINTER_REGNUM))
2444
    {
2445
      gcc_assert (stack_bytes == -16);
2446
 
2447
      last = LINK_POINTER_REGNUM;
2448
    }
2449
  else
2450
    {
2451
      gcc_assert (!stack_bytes);
2452
      gcc_assert (mask & (1 << 29));
2453
 
2454
      last = 29;
2455
    }
2456
 
2457
  /* Note, it is possible to have gaps in the register mask.
2458
     We ignore this here, and generate a JARL anyway.  We will
2459
     be pushing more registers than is strictly necessary, but
2460
     it does save code space.  */
2461
 
2462
  if (TARGET_LONG_CALLS)
2463
    {
2464
      char name[40];
2465
 
2466
      if (first == last)
2467
        sprintf (name, "__save_%s", reg_names [first]);
2468
      else
2469
        sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2470
 
2471
      sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2472
               name, name);
2473
    }
2474
  else
2475
    {
2476
      if (first == last)
2477
        sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2478
      else
2479
        sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2480
                 reg_names [last]);
2481
    }
2482
 
2483
  return buff;
2484
}
2485
 
2486
extern tree last_assemble_variable_decl;
2487
extern int size_directive_output;
2488
 
2489
/* A version of asm_output_aligned_bss() that copes with the special
2490
   data areas of the v850.  */
2491
void
2492
v850_output_aligned_bss (FILE * file,
2493
                         tree decl,
2494
                         const char * name,
2495
                         unsigned HOST_WIDE_INT size,
2496
                         int align)
2497
{
2498
  switch (v850_get_data_area (decl))
2499
    {
2500
    case DATA_AREA_ZDA:
2501
      zbss_section ();
2502
      break;
2503
 
2504
    case DATA_AREA_SDA:
2505
      sbss_section ();
2506
      break;
2507
 
2508
    case DATA_AREA_TDA:
2509
      tdata_section ();
2510
 
2511
    default:
2512
      bss_section ();
2513
      break;
2514
    }
2515
 
2516
  ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2517
#ifdef ASM_DECLARE_OBJECT_NAME
2518
  last_assemble_variable_decl = decl;
2519
  ASM_DECLARE_OBJECT_NAME (file, name, decl);
2520
#else
2521
  /* Standard thing is just output label for the object.  */
2522
  ASM_OUTPUT_LABEL (file, name);
2523
#endif /* ASM_DECLARE_OBJECT_NAME */
2524
  ASM_OUTPUT_SKIP (file, size ? size : 1);
2525
}
2526
 
2527
/* Called via the macro ASM_OUTPUT_DECL_COMMON */
2528
void
2529
v850_output_common (FILE * file,
2530
                    tree decl,
2531
                    const char * name,
2532
                    int size,
2533
                    int align)
2534
{
2535
  if (decl == NULL_TREE)
2536
    {
2537
      fprintf (file, "%s", COMMON_ASM_OP);
2538
    }
2539
  else
2540
    {
2541
      switch (v850_get_data_area (decl))
2542
        {
2543
        case DATA_AREA_ZDA:
2544
          fprintf (file, "%s", ZCOMMON_ASM_OP);
2545
          break;
2546
 
2547
        case DATA_AREA_SDA:
2548
          fprintf (file, "%s", SCOMMON_ASM_OP);
2549
          break;
2550
 
2551
        case DATA_AREA_TDA:
2552
          fprintf (file, "%s", TCOMMON_ASM_OP);
2553
          break;
2554
 
2555
        default:
2556
          fprintf (file, "%s", COMMON_ASM_OP);
2557
          break;
2558
        }
2559
    }
2560
 
2561
  assemble_name (file, name);
2562
  fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2563
}
2564
 
2565
/* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2566
void
2567
v850_output_local (FILE * file,
2568
                   tree decl,
2569
                   const char * name,
2570
                   int size,
2571
                   int align)
2572
{
2573
  fprintf (file, "%s", LOCAL_ASM_OP);
2574
  assemble_name (file, name);
2575
  fprintf (file, "\n");
2576
 
2577
  ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2578
}
2579
 
2580
/* Add data area to the given declaration if a ghs data area pragma is
2581
   currently in effect (#pragma ghs startXXX/endXXX).  */
2582
static void
2583
v850_insert_attributes (tree decl, tree * attr_ptr ATTRIBUTE_UNUSED )
2584
{
2585
  if (data_area_stack
2586
      && data_area_stack->data_area
2587
      && current_function_decl == NULL_TREE
2588
      && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2589
      && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2590
    v850_set_data_area (decl, data_area_stack->data_area);
2591
 
2592
  /* Initialize the default names of the v850 specific sections,
2593
     if this has not been done before.  */
2594
 
2595
  if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2596
    {
2597
      GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2598
        = build_string (sizeof (".sdata")-1, ".sdata");
2599
 
2600
      GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2601
        = build_string (sizeof (".rosdata")-1, ".rosdata");
2602
 
2603
      GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2604
        = build_string (sizeof (".tdata")-1, ".tdata");
2605
 
2606
      GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2607
        = build_string (sizeof (".zdata")-1, ".zdata");
2608
 
2609
      GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2610
        = build_string (sizeof (".rozdata")-1, ".rozdata");
2611
    }
2612
 
2613
  if (current_function_decl == NULL_TREE
2614
      && (TREE_CODE (decl) == VAR_DECL
2615
          || TREE_CODE (decl) == CONST_DECL
2616
          || TREE_CODE (decl) == FUNCTION_DECL)
2617
      && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2618
      && !DECL_SECTION_NAME (decl))
2619
    {
2620
      enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2621
      tree chosen_section;
2622
 
2623
      if (TREE_CODE (decl) == FUNCTION_DECL)
2624
        kind = GHS_SECTION_KIND_TEXT;
2625
      else
2626
        {
2627
          /* First choose a section kind based on the data area of the decl.  */
2628
          switch (v850_get_data_area (decl))
2629
            {
2630
            default:
2631
              gcc_unreachable ();
2632
 
2633
            case DATA_AREA_SDA:
2634
              kind = ((TREE_READONLY (decl))
2635
                      ? GHS_SECTION_KIND_ROSDATA
2636
                      : GHS_SECTION_KIND_SDATA);
2637
              break;
2638
 
2639
            case DATA_AREA_TDA:
2640
              kind = GHS_SECTION_KIND_TDATA;
2641
              break;
2642
 
2643
            case DATA_AREA_ZDA:
2644
              kind = ((TREE_READONLY (decl))
2645
                      ? GHS_SECTION_KIND_ROZDATA
2646
                      : GHS_SECTION_KIND_ZDATA);
2647
              break;
2648
 
2649
            case DATA_AREA_NORMAL:               /* default data area */
2650
              if (TREE_READONLY (decl))
2651
                kind = GHS_SECTION_KIND_RODATA;
2652
              else if (DECL_INITIAL (decl))
2653
                kind = GHS_SECTION_KIND_DATA;
2654
              else
2655
                kind = GHS_SECTION_KIND_BSS;
2656
            }
2657
        }
2658
 
2659
      /* Now, if the section kind has been explicitly renamed,
2660
         then attach a section attribute.  */
2661
      chosen_section = GHS_current_section_names [(int) kind];
2662
 
2663
      /* Otherwise, if this kind of section needs an explicit section
2664
         attribute, then also attach one.  */
2665
      if (chosen_section == NULL)
2666
        chosen_section = GHS_default_section_names [(int) kind];
2667
 
2668
      if (chosen_section)
2669
        {
2670
          /* Only set the section name if specified by a pragma, because
2671
             otherwise it will force those variables to get allocated storage
2672
             in this module, rather than by the linker.  */
2673
          DECL_SECTION_NAME (decl) = chosen_section;
2674
        }
2675
    }
2676
}
2677
 
2678
/* Construct a DISPOSE instruction that is the equivalent of
2679
   the given RTX.  We have already verified that this should
2680
   be possible.  */
2681
 
2682
char *
2683
construct_dispose_instruction (rtx op)
2684
{
2685
  int                count = XVECLEN (op, 0);
2686
  int                stack_bytes;
2687
  unsigned long int  mask;
2688
  int                i;
2689
  static char        buff[ 100 ]; /* XXX */
2690
  int                use_callt = 0;
2691
 
2692
  if (count <= 2)
2693
    {
2694
      error ("bogus DISPOSE construction: %d", count);
2695
      return NULL;
2696
    }
2697
 
2698
  /* Work out how many bytes to pop off the
2699
     stack before retrieving registers.  */
2700
  gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2701
  gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2702
  gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2703
 
2704
  stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2705
 
2706
  /* Each pop will remove 4 bytes from the stack....  */
2707
  stack_bytes -= (count - 2) * 4;
2708
 
2709
  /* Make sure that the amount we are popping
2710
     will fit into the DISPOSE instruction.  */
2711
  if (stack_bytes > 128)
2712
    {
2713
      error ("too much stack space to dispose of: %d", stack_bytes);
2714
      return NULL;
2715
    }
2716
 
2717
  /* Now compute the bit mask of registers to push.  */
2718
  mask = 0;
2719
 
2720
  for (i = 2; i < count; i++)
2721
    {
2722
      rtx vector_element = XVECEXP (op, 0, i);
2723
 
2724
      gcc_assert (GET_CODE (vector_element) == SET);
2725
      gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2726
      gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2727
                                               SImode));
2728
 
2729
      if (REGNO (SET_DEST (vector_element)) == 2)
2730
        use_callt = 1;
2731
      else
2732
        mask |= 1 << REGNO (SET_DEST (vector_element));
2733
    }
2734
 
2735
  if (! TARGET_DISABLE_CALLT
2736
      && (use_callt || stack_bytes == 0 || stack_bytes == 16))
2737
    {
2738
      if (use_callt)
2739
        {
2740
          sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
2741
          return buff;
2742
        }
2743
      else
2744
        {
2745
          for (i = 20; i < 32; i++)
2746
            if (mask & (1 << i))
2747
              break;
2748
 
2749
          if (i == 31)
2750
            sprintf (buff, "callt ctoff(__callt_return_r31c)");
2751
          else
2752
            sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
2753
                     i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2754
        }
2755
    }
2756
  else
2757
    {
2758
      static char        regs [100]; /* XXX */
2759
      int                done_one;
2760
 
2761
      /* Generate the DISPOSE instruction.  Note we could just issue the
2762
         bit mask as a number as the assembler can cope with this, but for
2763
         the sake of our readers we turn it into a textual description.  */
2764
      regs[0] = 0;
2765
      done_one = 0;
2766
 
2767
      for (i = 20; i < 32; i++)
2768
        {
2769
          if (mask & (1 << i))
2770
            {
2771
              int first;
2772
 
2773
              if (done_one)
2774
                strcat (regs, ", ");
2775
              else
2776
                done_one = 1;
2777
 
2778
              first = i;
2779
              strcat (regs, reg_names[ first ]);
2780
 
2781
              for (i++; i < 32; i++)
2782
                if ((mask & (1 << i)) == 0)
2783
                  break;
2784
 
2785
              if (i > first + 1)
2786
                {
2787
                  strcat (regs, " - ");
2788
                  strcat (regs, reg_names[ i - 1 ] );
2789
                }
2790
            }
2791
        }
2792
 
2793
      sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
2794
    }
2795
 
2796
  return buff;
2797
}
2798
 
2799
/* Construct a PREPARE instruction that is the equivalent of
2800
   the given RTL.  We have already verified that this should
2801
   be possible.  */
2802
 
2803
char *
2804
construct_prepare_instruction (rtx op)
2805
{
2806
  int                count = XVECLEN (op, 0);
2807
  int                stack_bytes;
2808
  unsigned long int  mask;
2809
  int                i;
2810
  static char        buff[ 100 ]; /* XXX */
2811
  int                use_callt = 0;
2812
 
2813
  if (count <= 1)
2814
    {
2815
      error ("bogus PREPEARE construction: %d", count);
2816
      return NULL;
2817
    }
2818
 
2819
  /* Work out how many bytes to push onto
2820
     the stack after storing the registers.  */
2821
  gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2822
  gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2823
  gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2824
 
2825
  stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2826
 
2827
  /* Each push will put 4 bytes from the stack.  */
2828
  stack_bytes += (count - 1) * 4;
2829
 
2830
  /* Make sure that the amount we are popping
2831
     will fit into the DISPOSE instruction.  */
2832
  if (stack_bytes < -128)
2833
    {
2834
      error ("too much stack space to prepare: %d", stack_bytes);
2835
      return NULL;
2836
    }
2837
 
2838
  /* Now compute the bit mask of registers to push.  */
2839
  mask = 0;
2840
  for (i = 1; i < count; i++)
2841
    {
2842
      rtx vector_element = XVECEXP (op, 0, i);
2843
 
2844
      gcc_assert (GET_CODE (vector_element) == SET);
2845
      gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2846
      gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2847
                                               SImode));
2848
 
2849
      if (REGNO (SET_SRC (vector_element)) == 2)
2850
        use_callt = 1;
2851
      else
2852
        mask |= 1 << REGNO (SET_SRC (vector_element));
2853
    }
2854
 
2855
  if ((! TARGET_DISABLE_CALLT)
2856
      && (use_callt || stack_bytes == 0 || stack_bytes == -16))
2857
    {
2858
      if (use_callt)
2859
        {
2860
          sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
2861
          return buff;
2862
        }
2863
 
2864
      for (i = 20; i < 32; i++)
2865
        if (mask & (1 << i))
2866
          break;
2867
 
2868
      if (i == 31)
2869
        sprintf (buff, "callt ctoff(__callt_save_r31c)");
2870
      else
2871
        sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
2872
                 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2873
    }
2874
  else
2875
    {
2876
      static char        regs [100]; /* XXX */
2877
      int                done_one;
2878
 
2879
 
2880
      /* Generate the PREPARE instruction.  Note we could just issue the
2881
         bit mask as a number as the assembler can cope with this, but for
2882
         the sake of our readers we turn it into a textual description.  */
2883
      regs[0] = 0;
2884
      done_one = 0;
2885
 
2886
      for (i = 20; i < 32; i++)
2887
        {
2888
          if (mask & (1 << i))
2889
            {
2890
              int first;
2891
 
2892
              if (done_one)
2893
                strcat (regs, ", ");
2894
              else
2895
                done_one = 1;
2896
 
2897
              first = i;
2898
              strcat (regs, reg_names[ first ]);
2899
 
2900
              for (i++; i < 32; i++)
2901
                if ((mask & (1 << i)) == 0)
2902
                  break;
2903
 
2904
              if (i > first + 1)
2905
                {
2906
                  strcat (regs, " - ");
2907
                  strcat (regs, reg_names[ i - 1 ] );
2908
                }
2909
            }
2910
        }
2911
 
2912
      sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
2913
    }
2914
 
2915
  return buff;
2916
}
2917
 
2918
/* Return an RTX indicating where the return address to the
2919
   calling function can be found.  */
2920
 
2921
rtx
2922
v850_return_addr (int count)
2923
{
2924
  if (count != 0)
2925
    return const0_rtx;
2926
 
2927
  return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
2928
}
2929
 
2930
static void
2931
v850_select_section (tree exp,
2932
                     int reloc ATTRIBUTE_UNUSED,
2933
                     unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
2934
{
2935
  if (TREE_CODE (exp) == VAR_DECL)
2936
    {
2937
      int is_const;
2938
      if (!TREE_READONLY (exp)
2939
          || TREE_SIDE_EFFECTS (exp)
2940
          || !DECL_INITIAL (exp)
2941
          || (DECL_INITIAL (exp) != error_mark_node
2942
              && !TREE_CONSTANT (DECL_INITIAL (exp))))
2943
        is_const = FALSE;
2944
      else
2945
        is_const = TRUE;
2946
 
2947
      switch (v850_get_data_area (exp))
2948
        {
2949
        case DATA_AREA_ZDA:
2950
          if (is_const)
2951
            rozdata_section ();
2952
          else
2953
            zdata_section ();
2954
          break;
2955
 
2956
        case DATA_AREA_TDA:
2957
          tdata_section ();
2958
          break;
2959
 
2960
        case DATA_AREA_SDA:
2961
          if (is_const)
2962
            rosdata_section ();
2963
          else
2964
            sdata_section ();
2965
          break;
2966
 
2967
        default:
2968
          if (is_const)
2969
            readonly_data_section ();
2970
          else
2971
            data_section ();
2972
          break;
2973
        }
2974
    }
2975
  else
2976
    readonly_data_section ();
2977
}
2978
 
2979
/* Worker function for TARGET_RETURN_IN_MEMORY.  */
2980
 
2981
static bool
2982
v850_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
2983
{
2984
  /* Return values > 8 bytes in length in memory.  */
2985
  return int_size_in_bytes (type) > 8 || TYPE_MODE (type) == BLKmode;
2986
}
2987
 
2988
/* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
2989
 
2990
static void
2991
v850_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
2992
                             enum machine_mode mode ATTRIBUTE_UNUSED,
2993
                             tree type ATTRIBUTE_UNUSED,
2994
                             int *pretend_arg_size ATTRIBUTE_UNUSED,
2995
                             int second_time ATTRIBUTE_UNUSED)
2996
{
2997
  ca->anonymous_args = (!TARGET_GHS ? 1 : 0);
2998
}

powered by: WebSVN 2.1.0

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