OSDN Git Service

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