OSDN Git Service

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