OSDN Git Service

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