OSDN Git Service

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