OSDN Git Service

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