OSDN Git Service

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