OSDN Git Service

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