OSDN Git Service

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