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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [gcc/] [config/] [v850/] [v850.c] - Blame information for rev 294

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

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

powered by: WebSVN 2.1.0

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