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