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