2009-06-03 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / config / v850 / v850.c
blobf9e8a7dd8b03c56407effd7f8d31659bc7a0d6a7
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).
6 This file is part of GCC.
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.
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.
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/>. */
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 "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "recog.h"
37 #include "expr.h"
38 #include "function.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"
47 #ifndef streq
48 #define streq(a,b) (strcmp (a, b) == 0)
49 #endif
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 const struct attribute_spec v850_attribute_table[];
62 static tree v850_handle_interrupt_attribute (tree *, tree, tree, int, bool *);
63 static tree v850_handle_data_area_attribute (tree *, tree, tree, int, bool *);
64 static void v850_insert_attributes (tree, tree *);
65 static void v850_asm_init_sections (void);
66 static section *v850_select_section (tree, int, unsigned HOST_WIDE_INT);
67 static void v850_encode_data_area (tree, rtx);
68 static void v850_encode_section_info (tree, rtx, int);
69 static bool v850_return_in_memory (const_tree, const_tree);
70 static void v850_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
71 tree, int *, int);
72 static bool v850_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
73 const_tree, bool);
74 static int v850_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
75 tree, bool);
77 /* Information about the various small memory areas. */
78 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
80 /* name max physical max */
81 { "tda", 0, 256 },
82 { "sda", 0, 65536 },
83 { "zda", 0, 32768 },
86 /* Names of the various data areas used on the v850. */
87 tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
88 tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
90 /* Track the current data area set by the data area pragma (which
91 can be nested). Tested by check_default_data_area. */
92 data_area_stack_element * data_area_stack = NULL;
94 /* True if we don't need to check any more if the current
95 function is an interrupt handler. */
96 static int v850_interrupt_cache_p = FALSE;
98 /* Whether current function is an interrupt handler. */
99 static int v850_interrupt_p = FALSE;
101 static GTY(()) section *rosdata_section;
102 static GTY(()) section *rozdata_section;
103 static GTY(()) section *tdata_section;
104 static GTY(()) section *zdata_section;
105 static GTY(()) section *zbss_section;
107 /* Initialize the GCC target structure. */
108 #undef TARGET_ASM_ALIGNED_HI_OP
109 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
111 #undef TARGET_ATTRIBUTE_TABLE
112 #define TARGET_ATTRIBUTE_TABLE v850_attribute_table
114 #undef TARGET_INSERT_ATTRIBUTES
115 #define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
117 #undef TARGET_ASM_SELECT_SECTION
118 #define TARGET_ASM_SELECT_SECTION v850_select_section
120 /* The assembler supports switchable .bss sections, but
121 v850_select_section doesn't yet make use of them. */
122 #undef TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
123 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS false
125 #undef TARGET_ENCODE_SECTION_INFO
126 #define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
128 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
129 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
131 #undef TARGET_DEFAULT_TARGET_FLAGS
132 #define TARGET_DEFAULT_TARGET_FLAGS (MASK_DEFAULT | MASK_APP_REGS)
133 #undef TARGET_HANDLE_OPTION
134 #define TARGET_HANDLE_OPTION v850_handle_option
136 #undef TARGET_RTX_COSTS
137 #define TARGET_RTX_COSTS v850_rtx_costs
139 #undef TARGET_ADDRESS_COST
140 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
142 #undef TARGET_MACHINE_DEPENDENT_REORG
143 #define TARGET_MACHINE_DEPENDENT_REORG v850_reorg
145 #undef TARGET_PROMOTE_PROTOTYPES
146 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
148 #undef TARGET_RETURN_IN_MEMORY
149 #define TARGET_RETURN_IN_MEMORY v850_return_in_memory
151 #undef TARGET_PASS_BY_REFERENCE
152 #define TARGET_PASS_BY_REFERENCE v850_pass_by_reference
154 #undef TARGET_CALLEE_COPIES
155 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
157 #undef TARGET_SETUP_INCOMING_VARARGS
158 #define TARGET_SETUP_INCOMING_VARARGS v850_setup_incoming_varargs
160 #undef TARGET_ARG_PARTIAL_BYTES
161 #define TARGET_ARG_PARTIAL_BYTES v850_arg_partial_bytes
163 struct gcc_target targetm = TARGET_INITIALIZER;
165 /* Set the maximum size of small memory area TYPE to the value given
166 by VALUE. Return true if VALUE was syntactically correct. VALUE
167 starts with the argument separator: either "-" or "=". */
169 static bool
170 v850_handle_memory_option (enum small_memory_type type, const char *value)
172 int i, size;
174 if (*value != '-' && *value != '=')
175 return false;
177 value++;
178 for (i = 0; value[i]; i++)
179 if (!ISDIGIT (value[i]))
180 return false;
182 size = atoi (value);
183 if (size > small_memory[type].physical_max)
184 error ("value passed to %<-m%s%> is too large", small_memory[type].name);
185 else
186 small_memory[type].max = size;
187 return true;
190 /* Implement TARGET_HANDLE_OPTION. */
192 static bool
193 v850_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
195 switch (code)
197 case OPT_mspace:
198 target_flags |= MASK_EP | MASK_PROLOG_FUNCTION;
199 return true;
201 case OPT_mv850:
202 target_flags &= ~(MASK_CPU ^ MASK_V850);
203 return true;
205 case OPT_mv850e:
206 case OPT_mv850e1:
207 target_flags &= ~(MASK_CPU ^ MASK_V850E);
208 return true;
210 case OPT_mtda:
211 return v850_handle_memory_option (SMALL_MEMORY_TDA, arg);
213 case OPT_msda:
214 return v850_handle_memory_option (SMALL_MEMORY_SDA, arg);
216 case OPT_mzda:
217 return v850_handle_memory_option (SMALL_MEMORY_ZDA, arg);
219 default:
220 return true;
224 static bool
225 v850_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
226 enum machine_mode mode, const_tree type,
227 bool named ATTRIBUTE_UNUSED)
229 unsigned HOST_WIDE_INT size;
231 if (type)
232 size = int_size_in_bytes (type);
233 else
234 size = GET_MODE_SIZE (mode);
236 return size > 8;
239 /* Return an RTX to represent where a value with mode MODE will be returned
240 from a function. If the result is 0, the argument is pushed. */
243 function_arg (CUMULATIVE_ARGS * cum,
244 enum machine_mode mode,
245 tree type,
246 int named)
248 rtx result = 0;
249 int size, align;
251 if (TARGET_GHS && !named)
252 return NULL_RTX;
254 if (mode == BLKmode)
255 size = int_size_in_bytes (type);
256 else
257 size = GET_MODE_SIZE (mode);
259 if (size < 1)
260 return 0;
262 if (type)
263 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
264 else
265 align = size;
267 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
269 if (cum->nbytes > 4 * UNITS_PER_WORD)
270 return 0;
272 if (type == NULL_TREE
273 && cum->nbytes + size > 4 * UNITS_PER_WORD)
274 return 0;
276 switch (cum->nbytes / UNITS_PER_WORD)
278 case 0:
279 result = gen_rtx_REG (mode, 6);
280 break;
281 case 1:
282 result = gen_rtx_REG (mode, 7);
283 break;
284 case 2:
285 result = gen_rtx_REG (mode, 8);
286 break;
287 case 3:
288 result = gen_rtx_REG (mode, 9);
289 break;
290 default:
291 result = 0;
294 return result;
298 /* Return the number of bytes which must be put into registers
299 for values which are part in registers and part in memory. */
301 static int
302 v850_arg_partial_bytes (CUMULATIVE_ARGS * cum, enum machine_mode mode,
303 tree type, bool named)
305 int size, align;
307 if (TARGET_GHS && !named)
308 return 0;
310 if (mode == BLKmode)
311 size = int_size_in_bytes (type);
312 else
313 size = GET_MODE_SIZE (mode);
315 if (type)
316 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
317 else
318 align = size;
320 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
322 if (cum->nbytes > 4 * UNITS_PER_WORD)
323 return 0;
325 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
326 return 0;
328 if (type == NULL_TREE
329 && cum->nbytes + size > 4 * UNITS_PER_WORD)
330 return 0;
332 return 4 * UNITS_PER_WORD - cum->nbytes;
336 /* Return the high and low words of a CONST_DOUBLE */
338 static void
339 const_double_split (rtx x, HOST_WIDE_INT * p_high, HOST_WIDE_INT * p_low)
341 if (GET_CODE (x) == CONST_DOUBLE)
343 long t[2];
344 REAL_VALUE_TYPE rv;
346 switch (GET_MODE (x))
348 case DFmode:
349 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
350 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
351 *p_high = t[1]; /* since v850 is little endian */
352 *p_low = t[0]; /* high is second word */
353 return;
355 case SFmode:
356 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
357 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
358 *p_low = 0;
359 return;
361 case VOIDmode:
362 case DImode:
363 *p_high = CONST_DOUBLE_HIGH (x);
364 *p_low = CONST_DOUBLE_LOW (x);
365 return;
367 default:
368 break;
372 fatal_insn ("const_double_split got a bad insn:", x);
376 /* Return the cost of the rtx R with code CODE. */
378 static int
379 const_costs_int (HOST_WIDE_INT value, int zero_cost)
381 if (CONST_OK_FOR_I (value))
382 return zero_cost;
383 else if (CONST_OK_FOR_J (value))
384 return 1;
385 else if (CONST_OK_FOR_K (value))
386 return 2;
387 else
388 return 4;
391 static int
392 const_costs (rtx r, enum rtx_code c)
394 HOST_WIDE_INT high, low;
396 switch (c)
398 case CONST_INT:
399 return const_costs_int (INTVAL (r), 0);
401 case CONST_DOUBLE:
402 const_double_split (r, &high, &low);
403 if (GET_MODE (r) == SFmode)
404 return const_costs_int (high, 1);
405 else
406 return const_costs_int (high, 1) + const_costs_int (low, 1);
408 case SYMBOL_REF:
409 case LABEL_REF:
410 case CONST:
411 return 2;
413 case HIGH:
414 return 1;
416 default:
417 return 4;
421 static bool
422 v850_rtx_costs (rtx x,
423 int code,
424 int outer_code ATTRIBUTE_UNUSED,
425 int * total, bool speed)
427 switch (code)
429 case CONST_INT:
430 case CONST_DOUBLE:
431 case CONST:
432 case SYMBOL_REF:
433 case LABEL_REF:
434 *total = COSTS_N_INSNS (const_costs (x, code));
435 return true;
437 case MOD:
438 case DIV:
439 case UMOD:
440 case UDIV:
441 if (TARGET_V850E && !speed)
442 *total = 6;
443 else
444 *total = 60;
445 return true;
447 case MULT:
448 if (TARGET_V850E
449 && ( GET_MODE (x) == SImode
450 || GET_MODE (x) == HImode
451 || GET_MODE (x) == QImode))
453 if (GET_CODE (XEXP (x, 1)) == REG)
454 *total = 4;
455 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
457 if (CONST_OK_FOR_O (INTVAL (XEXP (x, 1))))
458 *total = 6;
459 else if (CONST_OK_FOR_K (INTVAL (XEXP (x, 1))))
460 *total = 10;
463 else
464 *total = 20;
465 return true;
467 case ZERO_EXTRACT:
468 if (outer_code == COMPARE)
469 *total = 0;
470 return false;
472 default:
473 return false;
477 /* Print operand X using operand code CODE to assembly language output file
478 FILE. */
480 void
481 print_operand (FILE * file, rtx x, int code)
483 HOST_WIDE_INT high, low;
485 switch (code)
487 case 'c':
488 /* We use 'c' operands with symbols for .vtinherit */
489 if (GET_CODE (x) == SYMBOL_REF)
491 output_addr_const(file, x);
492 break;
494 /* fall through */
495 case 'b':
496 case 'B':
497 case 'C':
498 switch ((code == 'B' || code == 'C')
499 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
501 case NE:
502 if (code == 'c' || code == 'C')
503 fprintf (file, "nz");
504 else
505 fprintf (file, "ne");
506 break;
507 case EQ:
508 if (code == 'c' || code == 'C')
509 fprintf (file, "z");
510 else
511 fprintf (file, "e");
512 break;
513 case GE:
514 fprintf (file, "ge");
515 break;
516 case GT:
517 fprintf (file, "gt");
518 break;
519 case LE:
520 fprintf (file, "le");
521 break;
522 case LT:
523 fprintf (file, "lt");
524 break;
525 case GEU:
526 fprintf (file, "nl");
527 break;
528 case GTU:
529 fprintf (file, "h");
530 break;
531 case LEU:
532 fprintf (file, "nh");
533 break;
534 case LTU:
535 fprintf (file, "l");
536 break;
537 default:
538 gcc_unreachable ();
540 break;
541 case 'F': /* high word of CONST_DOUBLE */
542 switch (GET_CODE (x))
544 case CONST_INT:
545 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
546 break;
548 case CONST_DOUBLE:
549 const_double_split (x, &high, &low);
550 fprintf (file, "%ld", (long) high);
551 break;
553 default:
554 gcc_unreachable ();
556 break;
557 case 'G': /* low word of CONST_DOUBLE */
558 switch (GET_CODE (x))
560 case CONST_INT:
561 fprintf (file, "%ld", (long) INTVAL (x));
562 break;
564 case CONST_DOUBLE:
565 const_double_split (x, &high, &low);
566 fprintf (file, "%ld", (long) low);
567 break;
569 default:
570 gcc_unreachable ();
572 break;
573 case 'L':
574 fprintf (file, "%d\n", (int)(INTVAL (x) & 0xffff));
575 break;
576 case 'M':
577 fprintf (file, "%d", exact_log2 (INTVAL (x)));
578 break;
579 case 'O':
580 gcc_assert (special_symbolref_operand (x, VOIDmode));
582 if (GET_CODE (x) == CONST)
583 x = XEXP (XEXP (x, 0), 0);
584 else
585 gcc_assert (GET_CODE (x) == SYMBOL_REF);
587 if (SYMBOL_REF_ZDA_P (x))
588 fprintf (file, "zdaoff");
589 else if (SYMBOL_REF_SDA_P (x))
590 fprintf (file, "sdaoff");
591 else if (SYMBOL_REF_TDA_P (x))
592 fprintf (file, "tdaoff");
593 else
594 gcc_unreachable ();
595 break;
596 case 'P':
597 gcc_assert (special_symbolref_operand (x, VOIDmode));
598 output_addr_const (file, x);
599 break;
600 case 'Q':
601 gcc_assert (special_symbolref_operand (x, VOIDmode));
603 if (GET_CODE (x) == CONST)
604 x = XEXP (XEXP (x, 0), 0);
605 else
606 gcc_assert (GET_CODE (x) == SYMBOL_REF);
608 if (SYMBOL_REF_ZDA_P (x))
609 fprintf (file, "r0");
610 else if (SYMBOL_REF_SDA_P (x))
611 fprintf (file, "gp");
612 else if (SYMBOL_REF_TDA_P (x))
613 fprintf (file, "ep");
614 else
615 gcc_unreachable ();
616 break;
617 case 'R': /* 2nd word of a double. */
618 switch (GET_CODE (x))
620 case REG:
621 fprintf (file, reg_names[REGNO (x) + 1]);
622 break;
623 case MEM:
624 x = XEXP (adjust_address (x, SImode, 4), 0);
625 print_operand_address (file, x);
626 if (GET_CODE (x) == CONST_INT)
627 fprintf (file, "[r0]");
628 break;
630 default:
631 break;
633 break;
634 case 'S':
636 /* If it's a reference to a TDA variable, use sst/sld vs. st/ld. */
637 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
638 fputs ("s", file);
640 break;
642 case 'T':
644 /* Like an 'S' operand above, but for unsigned loads only. */
645 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
646 fputs ("s", file);
648 break;
650 case 'W': /* print the instruction suffix */
651 switch (GET_MODE (x))
653 default:
654 gcc_unreachable ();
656 case QImode: fputs (".b", file); break;
657 case HImode: fputs (".h", file); break;
658 case SImode: fputs (".w", file); break;
659 case SFmode: fputs (".w", file); break;
661 break;
662 case '.': /* register r0 */
663 fputs (reg_names[0], file);
664 break;
665 case 'z': /* reg or zero */
666 if (GET_CODE (x) == REG)
667 fputs (reg_names[REGNO (x)], file);
668 else
670 gcc_assert (x == const0_rtx);
671 fputs (reg_names[0], file);
673 break;
674 default:
675 switch (GET_CODE (x))
677 case MEM:
678 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
679 output_address (gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, 0),
680 XEXP (x, 0)));
681 else
682 output_address (XEXP (x, 0));
683 break;
685 case REG:
686 fputs (reg_names[REGNO (x)], file);
687 break;
688 case SUBREG:
689 fputs (reg_names[subreg_regno (x)], file);
690 break;
691 case CONST_INT:
692 case SYMBOL_REF:
693 case CONST:
694 case LABEL_REF:
695 case CODE_LABEL:
696 print_operand_address (file, x);
697 break;
698 default:
699 gcc_unreachable ();
701 break;
707 /* Output assembly language output for the address ADDR to FILE. */
709 void
710 print_operand_address (FILE * file, rtx addr)
712 switch (GET_CODE (addr))
714 case REG:
715 fprintf (file, "0[");
716 print_operand (file, addr, 0);
717 fprintf (file, "]");
718 break;
719 case LO_SUM:
720 if (GET_CODE (XEXP (addr, 0)) == REG)
722 /* reg,foo */
723 fprintf (file, "lo(");
724 print_operand (file, XEXP (addr, 1), 0);
725 fprintf (file, ")[");
726 print_operand (file, XEXP (addr, 0), 0);
727 fprintf (file, "]");
729 break;
730 case PLUS:
731 if (GET_CODE (XEXP (addr, 0)) == REG
732 || GET_CODE (XEXP (addr, 0)) == SUBREG)
734 /* reg,foo */
735 print_operand (file, XEXP (addr, 1), 0);
736 fprintf (file, "[");
737 print_operand (file, XEXP (addr, 0), 0);
738 fprintf (file, "]");
740 else
742 print_operand (file, XEXP (addr, 0), 0);
743 fprintf (file, "+");
744 print_operand (file, XEXP (addr, 1), 0);
746 break;
747 case SYMBOL_REF:
749 const char *off_name = NULL;
750 const char *reg_name = NULL;
752 if (SYMBOL_REF_ZDA_P (addr))
754 off_name = "zdaoff";
755 reg_name = "r0";
757 else if (SYMBOL_REF_SDA_P (addr))
759 off_name = "sdaoff";
760 reg_name = "gp";
762 else if (SYMBOL_REF_TDA_P (addr))
764 off_name = "tdaoff";
765 reg_name = "ep";
768 if (off_name)
769 fprintf (file, "%s(", off_name);
770 output_addr_const (file, addr);
771 if (reg_name)
772 fprintf (file, ")[%s]", reg_name);
774 break;
775 case CONST:
776 if (special_symbolref_operand (addr, VOIDmode))
778 rtx x = XEXP (XEXP (addr, 0), 0);
779 const char *off_name;
780 const char *reg_name;
782 if (SYMBOL_REF_ZDA_P (x))
784 off_name = "zdaoff";
785 reg_name = "r0";
787 else if (SYMBOL_REF_SDA_P (x))
789 off_name = "sdaoff";
790 reg_name = "gp";
792 else if (SYMBOL_REF_TDA_P (x))
794 off_name = "tdaoff";
795 reg_name = "ep";
797 else
798 gcc_unreachable ();
800 fprintf (file, "%s(", off_name);
801 output_addr_const (file, addr);
802 fprintf (file, ")[%s]", reg_name);
804 else
805 output_addr_const (file, addr);
806 break;
807 default:
808 output_addr_const (file, addr);
809 break;
813 /* When assemble_integer is used to emit the offsets for a switch
814 table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
815 output_addr_const will normally barf at this, but it is OK to omit
816 the truncate and just emit the difference of the two labels. The
817 .hword directive will automatically handle the truncation for us.
819 Returns 1 if rtx was handled, 0 otherwise. */
822 v850_output_addr_const_extra (FILE * file, rtx x)
824 if (GET_CODE (x) != TRUNCATE)
825 return 0;
827 x = XEXP (x, 0);
829 /* We must also handle the case where the switch table was passed a
830 constant value and so has been collapsed. In this case the first
831 label will have been deleted. In such a case it is OK to emit
832 nothing, since the table will not be used.
833 (cf gcc.c-torture/compile/990801-1.c). */
834 if (GET_CODE (x) == MINUS
835 && GET_CODE (XEXP (x, 0)) == LABEL_REF
836 && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
837 && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
838 return 1;
840 output_addr_const (file, x);
841 return 1;
844 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
845 point value. */
847 const char *
848 output_move_single (rtx * operands)
850 rtx dst = operands[0];
851 rtx src = operands[1];
853 if (REG_P (dst))
855 if (REG_P (src))
856 return "mov %1,%0";
858 else if (GET_CODE (src) == CONST_INT)
860 HOST_WIDE_INT value = INTVAL (src);
862 if (CONST_OK_FOR_J (value)) /* Signed 5-bit immediate. */
863 return "mov %1,%0";
865 else if (CONST_OK_FOR_K (value)) /* Signed 16-bit immediate. */
866 return "movea lo(%1),%.,%0";
868 else if (CONST_OK_FOR_L (value)) /* Upper 16 bits were set. */
869 return "movhi hi(%1),%.,%0";
871 /* A random constant. */
872 else if (TARGET_V850E)
873 return "mov %1,%0";
874 else
875 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
878 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
880 HOST_WIDE_INT high, low;
882 const_double_split (src, &high, &low);
884 if (CONST_OK_FOR_J (high)) /* Signed 5-bit immediate. */
885 return "mov %F1,%0";
887 else if (CONST_OK_FOR_K (high)) /* Signed 16-bit immediate. */
888 return "movea lo(%F1),%.,%0";
890 else if (CONST_OK_FOR_L (high)) /* Upper 16 bits were set. */
891 return "movhi hi(%F1),%.,%0";
893 /* A random constant. */
894 else if (TARGET_V850E)
895 return "mov %F1,%0";
897 else
898 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
901 else if (GET_CODE (src) == MEM)
902 return "%S1ld%W1 %1,%0";
904 else if (special_symbolref_operand (src, VOIDmode))
905 return "movea %O1(%P1),%Q1,%0";
907 else if (GET_CODE (src) == LABEL_REF
908 || GET_CODE (src) == SYMBOL_REF
909 || GET_CODE (src) == CONST)
911 if (TARGET_V850E)
912 return "mov hilo(%1),%0";
913 else
914 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
917 else if (GET_CODE (src) == HIGH)
918 return "movhi hi(%1),%.,%0";
920 else if (GET_CODE (src) == LO_SUM)
922 operands[2] = XEXP (src, 0);
923 operands[3] = XEXP (src, 1);
924 return "movea lo(%3),%2,%0";
928 else if (GET_CODE (dst) == MEM)
930 if (REG_P (src))
931 return "%S0st%W0 %1,%0";
933 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
934 return "%S0st%W0 %.,%0";
936 else if (GET_CODE (src) == CONST_DOUBLE
937 && CONST0_RTX (GET_MODE (dst)) == src)
938 return "%S0st%W0 %.,%0";
941 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
942 return "";
946 /* Return maximum offset supported for a short EP memory reference of mode
947 MODE and signedness UNSIGNEDP. */
949 static int
950 ep_memory_offset (enum machine_mode mode, int unsignedp ATTRIBUTE_UNUSED)
952 int max_offset = 0;
954 switch (mode)
956 case QImode:
957 if (TARGET_SMALL_SLD)
958 max_offset = (1 << 4);
959 else if (TARGET_V850E
960 && ( ( unsignedp && ! TARGET_US_BIT_SET)
961 || (! unsignedp && TARGET_US_BIT_SET)))
962 max_offset = (1 << 4);
963 else
964 max_offset = (1 << 7);
965 break;
967 case HImode:
968 if (TARGET_SMALL_SLD)
969 max_offset = (1 << 5);
970 else if (TARGET_V850E
971 && ( ( unsignedp && ! TARGET_US_BIT_SET)
972 || (! unsignedp && TARGET_US_BIT_SET)))
973 max_offset = (1 << 5);
974 else
975 max_offset = (1 << 8);
976 break;
978 case SImode:
979 case SFmode:
980 max_offset = (1 << 8);
981 break;
983 default:
984 break;
987 return max_offset;
990 /* Return true if OP is a valid short EP memory reference */
993 ep_memory_operand (rtx op, enum machine_mode mode, int unsigned_load)
995 rtx addr, op0, op1;
996 int max_offset;
997 int mask;
999 /* If we are not using the EP register on a per-function basis
1000 then do not allow this optimization at all. This is to
1001 prevent the use of the SLD/SST instructions which cannot be
1002 guaranteed to work properly due to a hardware bug. */
1003 if (!TARGET_EP)
1004 return FALSE;
1006 if (GET_CODE (op) != MEM)
1007 return FALSE;
1009 max_offset = ep_memory_offset (mode, unsigned_load);
1011 mask = GET_MODE_SIZE (mode) - 1;
1013 addr = XEXP (op, 0);
1014 if (GET_CODE (addr) == CONST)
1015 addr = XEXP (addr, 0);
1017 switch (GET_CODE (addr))
1019 default:
1020 break;
1022 case SYMBOL_REF:
1023 return SYMBOL_REF_TDA_P (addr);
1025 case REG:
1026 return REGNO (addr) == EP_REGNUM;
1028 case PLUS:
1029 op0 = XEXP (addr, 0);
1030 op1 = XEXP (addr, 1);
1031 if (GET_CODE (op1) == CONST_INT
1032 && INTVAL (op1) < max_offset
1033 && INTVAL (op1) >= 0
1034 && (INTVAL (op1) & mask) == 0)
1036 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1037 return TRUE;
1039 if (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_TDA_P (op0))
1040 return TRUE;
1042 break;
1045 return FALSE;
1048 /* Substitute memory references involving a pointer, to use the ep pointer,
1049 taking care to save and preserve the ep. */
1051 static void
1052 substitute_ep_register (rtx first_insn,
1053 rtx last_insn,
1054 int uses,
1055 int regno,
1056 rtx * p_r1,
1057 rtx * p_ep)
1059 rtx reg = gen_rtx_REG (Pmode, regno);
1060 rtx insn;
1062 if (!*p_r1)
1064 df_set_regs_ever_live (1, true);
1065 *p_r1 = gen_rtx_REG (Pmode, 1);
1066 *p_ep = gen_rtx_REG (Pmode, 30);
1069 if (TARGET_DEBUG)
1070 fprintf (stderr, "\
1071 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1072 2 * (uses - 3), uses, reg_names[regno],
1073 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1074 INSN_UID (first_insn), INSN_UID (last_insn));
1076 if (GET_CODE (first_insn) == NOTE)
1077 first_insn = next_nonnote_insn (first_insn);
1079 last_insn = next_nonnote_insn (last_insn);
1080 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1082 if (GET_CODE (insn) == INSN)
1084 rtx pattern = single_set (insn);
1086 /* Replace the memory references. */
1087 if (pattern)
1089 rtx *p_mem;
1090 /* Memory operands are signed by default. */
1091 int unsignedp = FALSE;
1093 if (GET_CODE (SET_DEST (pattern)) == MEM
1094 && GET_CODE (SET_SRC (pattern)) == MEM)
1095 p_mem = (rtx *)0;
1097 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1098 p_mem = &SET_DEST (pattern);
1100 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1101 p_mem = &SET_SRC (pattern);
1103 else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1104 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1105 p_mem = &XEXP (SET_SRC (pattern), 0);
1107 else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1108 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1110 p_mem = &XEXP (SET_SRC (pattern), 0);
1111 unsignedp = TRUE;
1113 else
1114 p_mem = (rtx *)0;
1116 if (p_mem)
1118 rtx addr = XEXP (*p_mem, 0);
1120 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1121 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1123 else if (GET_CODE (addr) == PLUS
1124 && GET_CODE (XEXP (addr, 0)) == REG
1125 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1126 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1127 && ((INTVAL (XEXP (addr, 1)))
1128 < ep_memory_offset (GET_MODE (*p_mem),
1129 unsignedp))
1130 && ((INTVAL (XEXP (addr, 1))) >= 0))
1131 *p_mem = change_address (*p_mem, VOIDmode,
1132 gen_rtx_PLUS (Pmode,
1133 *p_ep,
1134 XEXP (addr, 1)));
1140 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1141 insn = prev_nonnote_insn (first_insn);
1142 if (insn && GET_CODE (insn) == INSN
1143 && GET_CODE (PATTERN (insn)) == SET
1144 && SET_DEST (PATTERN (insn)) == *p_ep
1145 && SET_SRC (PATTERN (insn)) == *p_r1)
1146 delete_insn (insn);
1147 else
1148 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1150 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1151 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1155 /* TARGET_MACHINE_DEPENDENT_REORG. On the 850, we use it to implement
1156 the -mep mode to copy heavily used pointers to ep to use the implicit
1157 addressing. */
1159 static void
1160 v850_reorg (void)
1162 struct
1164 int uses;
1165 rtx first_insn;
1166 rtx last_insn;
1168 regs[FIRST_PSEUDO_REGISTER];
1170 int i;
1171 int use_ep = FALSE;
1172 rtx r1 = NULL_RTX;
1173 rtx ep = NULL_RTX;
1174 rtx insn;
1175 rtx pattern;
1177 /* If not ep mode, just return now. */
1178 if (!TARGET_EP)
1179 return;
1181 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1183 regs[i].uses = 0;
1184 regs[i].first_insn = NULL_RTX;
1185 regs[i].last_insn = NULL_RTX;
1188 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1190 switch (GET_CODE (insn))
1192 /* End of basic block */
1193 default:
1194 if (!use_ep)
1196 int max_uses = -1;
1197 int max_regno = -1;
1199 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1201 if (max_uses < regs[i].uses)
1203 max_uses = regs[i].uses;
1204 max_regno = i;
1208 if (max_uses > 3)
1209 substitute_ep_register (regs[max_regno].first_insn,
1210 regs[max_regno].last_insn,
1211 max_uses, max_regno, &r1, &ep);
1214 use_ep = FALSE;
1215 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1217 regs[i].uses = 0;
1218 regs[i].first_insn = NULL_RTX;
1219 regs[i].last_insn = NULL_RTX;
1221 break;
1223 case NOTE:
1224 break;
1226 case INSN:
1227 pattern = single_set (insn);
1229 /* See if there are any memory references we can shorten */
1230 if (pattern)
1232 rtx src = SET_SRC (pattern);
1233 rtx dest = SET_DEST (pattern);
1234 rtx mem;
1235 /* Memory operands are signed by default. */
1236 int unsignedp = FALSE;
1238 /* We might have (SUBREG (MEM)) here, so just get rid of the
1239 subregs to make this code simpler. */
1240 if (GET_CODE (dest) == SUBREG
1241 && (GET_CODE (SUBREG_REG (dest)) == MEM
1242 || GET_CODE (SUBREG_REG (dest)) == REG))
1243 alter_subreg (&dest);
1244 if (GET_CODE (src) == SUBREG
1245 && (GET_CODE (SUBREG_REG (src)) == MEM
1246 || GET_CODE (SUBREG_REG (src)) == REG))
1247 alter_subreg (&src);
1249 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1250 mem = NULL_RTX;
1252 else if (GET_CODE (dest) == MEM)
1253 mem = dest;
1255 else if (GET_CODE (src) == MEM)
1256 mem = src;
1258 else if (GET_CODE (src) == SIGN_EXTEND
1259 && GET_CODE (XEXP (src, 0)) == MEM)
1260 mem = XEXP (src, 0);
1262 else if (GET_CODE (src) == ZERO_EXTEND
1263 && GET_CODE (XEXP (src, 0)) == MEM)
1265 mem = XEXP (src, 0);
1266 unsignedp = TRUE;
1268 else
1269 mem = NULL_RTX;
1271 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1272 use_ep = TRUE;
1274 else if (!use_ep && mem
1275 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1277 rtx addr = XEXP (mem, 0);
1278 int regno = -1;
1279 int short_p;
1281 if (GET_CODE (addr) == REG)
1283 short_p = TRUE;
1284 regno = REGNO (addr);
1287 else if (GET_CODE (addr) == PLUS
1288 && GET_CODE (XEXP (addr, 0)) == REG
1289 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1290 && ((INTVAL (XEXP (addr, 1)))
1291 < ep_memory_offset (GET_MODE (mem), unsignedp))
1292 && ((INTVAL (XEXP (addr, 1))) >= 0))
1294 short_p = TRUE;
1295 regno = REGNO (XEXP (addr, 0));
1298 else
1299 short_p = FALSE;
1301 if (short_p)
1303 regs[regno].uses++;
1304 regs[regno].last_insn = insn;
1305 if (!regs[regno].first_insn)
1306 regs[regno].first_insn = insn;
1310 /* Loading up a register in the basic block zaps any savings
1311 for the register */
1312 if (GET_CODE (dest) == REG)
1314 enum machine_mode mode = GET_MODE (dest);
1315 int regno;
1316 int endregno;
1318 regno = REGNO (dest);
1319 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1321 if (!use_ep)
1323 /* See if we can use the pointer before this
1324 modification. */
1325 int max_uses = -1;
1326 int max_regno = -1;
1328 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1330 if (max_uses < regs[i].uses)
1332 max_uses = regs[i].uses;
1333 max_regno = i;
1337 if (max_uses > 3
1338 && max_regno >= regno
1339 && max_regno < endregno)
1341 substitute_ep_register (regs[max_regno].first_insn,
1342 regs[max_regno].last_insn,
1343 max_uses, max_regno, &r1,
1344 &ep);
1346 /* Since we made a substitution, zap all remembered
1347 registers. */
1348 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1350 regs[i].uses = 0;
1351 regs[i].first_insn = NULL_RTX;
1352 regs[i].last_insn = NULL_RTX;
1357 for (i = regno; i < endregno; i++)
1359 regs[i].uses = 0;
1360 regs[i].first_insn = NULL_RTX;
1361 regs[i].last_insn = NULL_RTX;
1370 /* # of registers saved by the interrupt handler. */
1371 #define INTERRUPT_FIXED_NUM 4
1373 /* # of bytes for registers saved by the interrupt handler. */
1374 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1376 /* # of registers saved in register parameter area. */
1377 #define INTERRUPT_REGPARM_NUM 4
1378 /* # of words saved for other registers. */
1379 #define INTERRUPT_ALL_SAVE_NUM \
1380 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1382 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1385 compute_register_save_size (long * p_reg_saved)
1387 int size = 0;
1388 int i;
1389 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1390 int call_p = df_regs_ever_live_p (LINK_POINTER_REGNUM);
1391 long reg_saved = 0;
1393 /* Count the return pointer if we need to save it. */
1394 if (crtl->profile && !call_p)
1396 df_set_regs_ever_live (LINK_POINTER_REGNUM, true);
1397 call_p = 1;
1400 /* Count space for the register saves. */
1401 if (interrupt_handler)
1403 for (i = 0; i <= 31; i++)
1404 switch (i)
1406 default:
1407 if (df_regs_ever_live_p (i) || call_p)
1409 size += 4;
1410 reg_saved |= 1L << i;
1412 break;
1414 /* We don't save/restore r0 or the stack pointer */
1415 case 0:
1416 case STACK_POINTER_REGNUM:
1417 break;
1419 /* For registers with fixed use, we save them, set them to the
1420 appropriate value, and then restore them.
1421 These registers are handled specially, so don't list them
1422 on the list of registers to save in the prologue. */
1423 case 1: /* temp used to hold ep */
1424 case 4: /* gp */
1425 case 10: /* temp used to call interrupt save/restore */
1426 case EP_REGNUM: /* ep */
1427 size += 4;
1428 break;
1431 else
1433 /* Find the first register that needs to be saved. */
1434 for (i = 0; i <= 31; i++)
1435 if (df_regs_ever_live_p (i) && ((! call_used_regs[i])
1436 || i == LINK_POINTER_REGNUM))
1437 break;
1439 /* If it is possible that an out-of-line helper function might be
1440 used to generate the prologue for the current function, then we
1441 need to cover the possibility that such a helper function will
1442 be used, despite the fact that there might be gaps in the list of
1443 registers that need to be saved. To detect this we note that the
1444 helper functions always push at least register r29 (provided
1445 that the function is not an interrupt handler). */
1447 if (TARGET_PROLOG_FUNCTION
1448 && (i == 2 || ((i >= 20) && (i < 30))))
1450 if (i == 2)
1452 size += 4;
1453 reg_saved |= 1L << i;
1455 i = 20;
1458 /* Helper functions save all registers between the starting
1459 register and the last register, regardless of whether they
1460 are actually used by the function or not. */
1461 for (; i <= 29; i++)
1463 size += 4;
1464 reg_saved |= 1L << i;
1467 if (df_regs_ever_live_p (LINK_POINTER_REGNUM))
1469 size += 4;
1470 reg_saved |= 1L << LINK_POINTER_REGNUM;
1473 else
1475 for (; i <= 31; i++)
1476 if (df_regs_ever_live_p (i) && ((! call_used_regs[i])
1477 || i == LINK_POINTER_REGNUM))
1479 size += 4;
1480 reg_saved |= 1L << i;
1485 if (p_reg_saved)
1486 *p_reg_saved = reg_saved;
1488 return size;
1492 compute_frame_size (int size, long * p_reg_saved)
1494 return (size
1495 + compute_register_save_size (p_reg_saved)
1496 + crtl->outgoing_args_size);
1500 void
1501 expand_prologue (void)
1503 unsigned int i;
1504 int offset;
1505 unsigned int size = get_frame_size ();
1506 unsigned int actual_fsize;
1507 unsigned int init_stack_alloc = 0;
1508 rtx save_regs[32];
1509 rtx save_all;
1510 unsigned int num_save;
1511 unsigned int default_stack;
1512 int code;
1513 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1514 long reg_saved = 0;
1516 actual_fsize = compute_frame_size (size, &reg_saved);
1518 /* Save/setup global registers for interrupt functions right now. */
1519 if (interrupt_handler)
1521 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1522 emit_insn (gen_callt_save_interrupt ());
1523 else
1524 emit_insn (gen_save_interrupt ());
1526 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1528 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1529 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1532 /* Save arg registers to the stack if necessary. */
1533 else if (crtl->args.info.anonymous_args)
1535 if (TARGET_PROLOG_FUNCTION && TARGET_V850E && !TARGET_DISABLE_CALLT)
1536 emit_insn (gen_save_r6_r9_v850e ());
1537 else if (TARGET_PROLOG_FUNCTION && ! TARGET_LONG_CALLS)
1538 emit_insn (gen_save_r6_r9 ());
1539 else
1541 offset = 0;
1542 for (i = 6; i < 10; i++)
1544 emit_move_insn (gen_rtx_MEM (SImode,
1545 plus_constant (stack_pointer_rtx,
1546 offset)),
1547 gen_rtx_REG (SImode, i));
1548 offset += 4;
1553 /* Identify all of the saved registers. */
1554 num_save = 0;
1555 default_stack = 0;
1556 for (i = 1; i < 31; i++)
1558 if (((1L << i) & reg_saved) != 0)
1559 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1562 /* If the return pointer is saved, the helper functions also allocate
1563 16 bytes of stack for arguments to be saved in. */
1564 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1566 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1567 default_stack = 16;
1570 /* See if we have an insn that allocates stack space and saves the particular
1571 registers we want to. */
1572 save_all = NULL_RTX;
1573 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1575 int alloc_stack = (4 * num_save) + default_stack;
1576 int unalloc_stack = actual_fsize - alloc_stack;
1577 int save_func_len = 4;
1578 int save_normal_len;
1580 if (unalloc_stack)
1581 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1583 /* see if we would have used ep to save the stack */
1584 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1585 save_normal_len = (3 * 2) + (2 * num_save);
1586 else
1587 save_normal_len = 4 * num_save;
1589 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1591 /* Don't bother checking if we don't actually save any space.
1592 This happens for instance if one register is saved and additional
1593 stack space is allocated. */
1594 if (save_func_len < save_normal_len)
1596 save_all = gen_rtx_PARALLEL
1597 (VOIDmode,
1598 rtvec_alloc (num_save + 1
1599 + (TARGET_V850 ? (TARGET_LONG_CALLS ? 2 : 1) : 0)));
1601 XVECEXP (save_all, 0, 0)
1602 = gen_rtx_SET (VOIDmode,
1603 stack_pointer_rtx,
1604 plus_constant (stack_pointer_rtx, -alloc_stack));
1606 offset = - default_stack;
1607 for (i = 0; i < num_save; i++)
1609 XVECEXP (save_all, 0, i+1)
1610 = gen_rtx_SET (VOIDmode,
1611 gen_rtx_MEM (Pmode,
1612 plus_constant (stack_pointer_rtx,
1613 offset)),
1614 save_regs[i]);
1615 offset -= 4;
1618 if (TARGET_V850)
1620 XVECEXP (save_all, 0, num_save + 1)
1621 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1623 if (TARGET_LONG_CALLS)
1624 XVECEXP (save_all, 0, num_save + 2)
1625 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
1628 code = recog (save_all, NULL_RTX, NULL);
1629 if (code >= 0)
1631 rtx insn = emit_insn (save_all);
1632 INSN_CODE (insn) = code;
1633 actual_fsize -= alloc_stack;
1635 if (TARGET_DEBUG)
1636 fprintf (stderr, "\
1637 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1638 save_normal_len - save_func_len,
1639 save_normal_len, save_func_len,
1640 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1642 else
1643 save_all = NULL_RTX;
1647 /* If no prolog save function is available, store the registers the old
1648 fashioned way (one by one). */
1649 if (!save_all)
1651 /* Special case interrupt functions that save all registers for a call. */
1652 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1654 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1655 emit_insn (gen_callt_save_all_interrupt ());
1656 else
1657 emit_insn (gen_save_all_interrupt ());
1659 else
1661 /* If the stack is too big, allocate it in chunks so we can do the
1662 register saves. We use the register save size so we use the ep
1663 register. */
1664 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1665 init_stack_alloc = compute_register_save_size (NULL);
1666 else
1667 init_stack_alloc = actual_fsize;
1669 /* Save registers at the beginning of the stack frame. */
1670 offset = init_stack_alloc - 4;
1672 if (init_stack_alloc)
1673 emit_insn (gen_addsi3 (stack_pointer_rtx,
1674 stack_pointer_rtx,
1675 GEN_INT (- (signed) init_stack_alloc)));
1677 /* Save the return pointer first. */
1678 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1680 emit_move_insn (gen_rtx_MEM (SImode,
1681 plus_constant (stack_pointer_rtx,
1682 offset)),
1683 save_regs[--num_save]);
1684 offset -= 4;
1687 for (i = 0; i < num_save; i++)
1689 emit_move_insn (gen_rtx_MEM (SImode,
1690 plus_constant (stack_pointer_rtx,
1691 offset)),
1692 save_regs[i]);
1693 offset -= 4;
1698 /* Allocate the rest of the stack that was not allocated above (either it is
1699 > 32K or we just called a function to save the registers and needed more
1700 stack. */
1701 if (actual_fsize > init_stack_alloc)
1703 int diff = actual_fsize - init_stack_alloc;
1704 if (CONST_OK_FOR_K (diff))
1705 emit_insn (gen_addsi3 (stack_pointer_rtx,
1706 stack_pointer_rtx,
1707 GEN_INT (-diff)));
1708 else
1710 rtx reg = gen_rtx_REG (Pmode, 12);
1711 emit_move_insn (reg, GEN_INT (-diff));
1712 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1716 /* If we need a frame pointer, set it up now. */
1717 if (frame_pointer_needed)
1718 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1722 void
1723 expand_epilogue (void)
1725 unsigned int i;
1726 int offset;
1727 unsigned int size = get_frame_size ();
1728 long reg_saved = 0;
1729 int actual_fsize = compute_frame_size (size, &reg_saved);
1730 unsigned int init_stack_free = 0;
1731 rtx restore_regs[32];
1732 rtx restore_all;
1733 unsigned int num_restore;
1734 unsigned int default_stack;
1735 int code;
1736 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1738 /* Eliminate the initial stack stored by interrupt functions. */
1739 if (interrupt_handler)
1741 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1742 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1743 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1746 /* Cut off any dynamic stack created. */
1747 if (frame_pointer_needed)
1748 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1750 /* Identify all of the saved registers. */
1751 num_restore = 0;
1752 default_stack = 0;
1753 for (i = 1; i < 31; i++)
1755 if (((1L << i) & reg_saved) != 0)
1756 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1759 /* If the return pointer is saved, the helper functions also allocate
1760 16 bytes of stack for arguments to be saved in. */
1761 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1763 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1764 default_stack = 16;
1767 /* See if we have an insn that restores the particular registers we
1768 want to. */
1769 restore_all = NULL_RTX;
1771 if (TARGET_PROLOG_FUNCTION
1772 && num_restore > 0
1773 && actual_fsize >= (signed) default_stack
1774 && !interrupt_handler)
1776 int alloc_stack = (4 * num_restore) + default_stack;
1777 int unalloc_stack = actual_fsize - alloc_stack;
1778 int restore_func_len = 4;
1779 int restore_normal_len;
1781 if (unalloc_stack)
1782 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1784 /* See if we would have used ep to restore the registers. */
1785 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1786 restore_normal_len = (3 * 2) + (2 * num_restore);
1787 else
1788 restore_normal_len = 4 * num_restore;
1790 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1792 /* Don't bother checking if we don't actually save any space. */
1793 if (restore_func_len < restore_normal_len)
1795 restore_all = gen_rtx_PARALLEL (VOIDmode,
1796 rtvec_alloc (num_restore + 2));
1797 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1798 XVECEXP (restore_all, 0, 1)
1799 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1800 gen_rtx_PLUS (Pmode,
1801 stack_pointer_rtx,
1802 GEN_INT (alloc_stack)));
1804 offset = alloc_stack - 4;
1805 for (i = 0; i < num_restore; i++)
1807 XVECEXP (restore_all, 0, i+2)
1808 = gen_rtx_SET (VOIDmode,
1809 restore_regs[i],
1810 gen_rtx_MEM (Pmode,
1811 plus_constant (stack_pointer_rtx,
1812 offset)));
1813 offset -= 4;
1816 code = recog (restore_all, NULL_RTX, NULL);
1818 if (code >= 0)
1820 rtx insn;
1822 actual_fsize -= alloc_stack;
1823 if (actual_fsize)
1825 if (CONST_OK_FOR_K (actual_fsize))
1826 emit_insn (gen_addsi3 (stack_pointer_rtx,
1827 stack_pointer_rtx,
1828 GEN_INT (actual_fsize)));
1829 else
1831 rtx reg = gen_rtx_REG (Pmode, 12);
1832 emit_move_insn (reg, GEN_INT (actual_fsize));
1833 emit_insn (gen_addsi3 (stack_pointer_rtx,
1834 stack_pointer_rtx,
1835 reg));
1839 insn = emit_jump_insn (restore_all);
1840 INSN_CODE (insn) = code;
1842 if (TARGET_DEBUG)
1843 fprintf (stderr, "\
1844 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1845 restore_normal_len - restore_func_len,
1846 restore_normal_len, restore_func_len,
1847 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1849 else
1850 restore_all = NULL_RTX;
1854 /* If no epilogue save function is available, restore the registers the
1855 old fashioned way (one by one). */
1856 if (!restore_all)
1858 /* If the stack is large, we need to cut it down in 2 pieces. */
1859 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1860 init_stack_free = 4 * num_restore;
1861 else
1862 init_stack_free = (signed) actual_fsize;
1864 /* Deallocate the rest of the stack if it is > 32K. */
1865 if (actual_fsize > init_stack_free)
1867 int diff;
1869 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1871 if (CONST_OK_FOR_K (diff))
1872 emit_insn (gen_addsi3 (stack_pointer_rtx,
1873 stack_pointer_rtx,
1874 GEN_INT (diff)));
1875 else
1877 rtx reg = gen_rtx_REG (Pmode, 12);
1878 emit_move_insn (reg, GEN_INT (diff));
1879 emit_insn (gen_addsi3 (stack_pointer_rtx,
1880 stack_pointer_rtx,
1881 reg));
1885 /* Special case interrupt functions that save all registers
1886 for a call. */
1887 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1889 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1890 emit_insn (gen_callt_restore_all_interrupt ());
1891 else
1892 emit_insn (gen_restore_all_interrupt ());
1894 else
1896 /* Restore registers from the beginning of the stack frame. */
1897 offset = init_stack_free - 4;
1899 /* Restore the return pointer first. */
1900 if (num_restore > 0
1901 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
1903 emit_move_insn (restore_regs[--num_restore],
1904 gen_rtx_MEM (SImode,
1905 plus_constant (stack_pointer_rtx,
1906 offset)));
1907 offset -= 4;
1910 for (i = 0; i < num_restore; i++)
1912 emit_move_insn (restore_regs[i],
1913 gen_rtx_MEM (SImode,
1914 plus_constant (stack_pointer_rtx,
1915 offset)));
1917 emit_use (restore_regs[i]);
1918 offset -= 4;
1921 /* Cut back the remainder of the stack. */
1922 if (init_stack_free)
1923 emit_insn (gen_addsi3 (stack_pointer_rtx,
1924 stack_pointer_rtx,
1925 GEN_INT (init_stack_free)));
1928 /* And return or use reti for interrupt handlers. */
1929 if (interrupt_handler)
1931 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1932 emit_insn (gen_callt_return_interrupt ());
1933 else
1934 emit_jump_insn (gen_return_interrupt ());
1936 else if (actual_fsize)
1937 emit_jump_insn (gen_return_internal ());
1938 else
1939 emit_jump_insn (gen_return_simple ());
1942 v850_interrupt_cache_p = FALSE;
1943 v850_interrupt_p = FALSE;
1947 /* Update the condition code from the insn. */
1949 void
1950 notice_update_cc (rtx body, rtx insn)
1952 switch (get_attr_cc (insn))
1954 case CC_NONE:
1955 /* Insn does not affect CC at all. */
1956 break;
1958 case CC_NONE_0HIT:
1959 /* Insn does not change CC, but the 0'th operand has been changed. */
1960 if (cc_status.value1 != 0
1961 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1962 cc_status.value1 = 0;
1963 break;
1965 case CC_SET_ZN:
1966 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1967 V,C is in an unusable state. */
1968 CC_STATUS_INIT;
1969 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1970 cc_status.value1 = recog_data.operand[0];
1971 break;
1973 case CC_SET_ZNV:
1974 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1975 C is in an unusable state. */
1976 CC_STATUS_INIT;
1977 cc_status.flags |= CC_NO_CARRY;
1978 cc_status.value1 = recog_data.operand[0];
1979 break;
1981 case CC_COMPARE:
1982 /* The insn is a compare instruction. */
1983 CC_STATUS_INIT;
1984 cc_status.value1 = SET_SRC (body);
1985 break;
1987 case CC_CLOBBER:
1988 /* Insn doesn't leave CC in a usable state. */
1989 CC_STATUS_INIT;
1990 break;
1994 /* Retrieve the data area that has been chosen for the given decl. */
1996 v850_data_area
1997 v850_get_data_area (tree decl)
1999 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2000 return DATA_AREA_SDA;
2002 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2003 return DATA_AREA_TDA;
2005 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2006 return DATA_AREA_ZDA;
2008 return DATA_AREA_NORMAL;
2011 /* Store the indicated data area in the decl's attributes. */
2013 static void
2014 v850_set_data_area (tree decl, v850_data_area data_area)
2016 tree name;
2018 switch (data_area)
2020 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2021 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2022 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2023 default:
2024 return;
2027 DECL_ATTRIBUTES (decl) = tree_cons
2028 (name, NULL, DECL_ATTRIBUTES (decl));
2031 const struct attribute_spec v850_attribute_table[] =
2033 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2034 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2035 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2036 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2037 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2038 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2039 { NULL, 0, 0, false, false, false, NULL }
2042 /* Handle an "interrupt" attribute; arguments as in
2043 struct attribute_spec.handler. */
2044 static tree
2045 v850_handle_interrupt_attribute (tree * node,
2046 tree name,
2047 tree args ATTRIBUTE_UNUSED,
2048 int flags ATTRIBUTE_UNUSED,
2049 bool * no_add_attrs)
2051 if (TREE_CODE (*node) != FUNCTION_DECL)
2053 warning (OPT_Wattributes, "%qE attribute only applies to functions",
2054 name);
2055 *no_add_attrs = true;
2058 return NULL_TREE;
2061 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2062 struct attribute_spec.handler. */
2063 static tree
2064 v850_handle_data_area_attribute (tree* node,
2065 tree name,
2066 tree args ATTRIBUTE_UNUSED,
2067 int flags ATTRIBUTE_UNUSED,
2068 bool * no_add_attrs)
2070 v850_data_area data_area;
2071 v850_data_area area;
2072 tree decl = *node;
2074 /* Implement data area attribute. */
2075 if (is_attribute_p ("sda", name))
2076 data_area = DATA_AREA_SDA;
2077 else if (is_attribute_p ("tda", name))
2078 data_area = DATA_AREA_TDA;
2079 else if (is_attribute_p ("zda", name))
2080 data_area = DATA_AREA_ZDA;
2081 else
2082 gcc_unreachable ();
2084 switch (TREE_CODE (decl))
2086 case VAR_DECL:
2087 if (current_function_decl != NULL_TREE)
2089 error ("%Jdata area attributes cannot be specified for "
2090 "local variables", decl);
2091 *no_add_attrs = true;
2094 /* Drop through. */
2096 case FUNCTION_DECL:
2097 area = v850_get_data_area (decl);
2098 if (area != DATA_AREA_NORMAL && data_area != area)
2100 error ("data area of %q+D conflicts with previous declaration",
2101 decl);
2102 *no_add_attrs = true;
2104 break;
2106 default:
2107 break;
2110 return NULL_TREE;
2114 /* Return nonzero if FUNC is an interrupt function as specified
2115 by the "interrupt" attribute. */
2118 v850_interrupt_function_p (tree func)
2120 tree a;
2121 int ret = 0;
2123 if (v850_interrupt_cache_p)
2124 return v850_interrupt_p;
2126 if (TREE_CODE (func) != FUNCTION_DECL)
2127 return 0;
2129 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2130 if (a != NULL_TREE)
2131 ret = 1;
2133 else
2135 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2136 ret = a != NULL_TREE;
2139 /* Its not safe to trust global variables until after function inlining has
2140 been done. */
2141 if (reload_completed | reload_in_progress)
2142 v850_interrupt_p = ret;
2144 return ret;
2148 static void
2149 v850_encode_data_area (tree decl, rtx symbol)
2151 int flags;
2153 /* Map explicit sections into the appropriate attribute */
2154 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2156 if (DECL_SECTION_NAME (decl))
2158 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2160 if (streq (name, ".zdata") || streq (name, ".zbss"))
2161 v850_set_data_area (decl, DATA_AREA_ZDA);
2163 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2164 v850_set_data_area (decl, DATA_AREA_SDA);
2166 else if (streq (name, ".tdata"))
2167 v850_set_data_area (decl, DATA_AREA_TDA);
2170 /* If no attribute, support -m{zda,sda,tda}=n */
2171 else
2173 int size = int_size_in_bytes (TREE_TYPE (decl));
2174 if (size <= 0)
2177 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2178 v850_set_data_area (decl, DATA_AREA_TDA);
2180 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2181 v850_set_data_area (decl, DATA_AREA_SDA);
2183 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2184 v850_set_data_area (decl, DATA_AREA_ZDA);
2187 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2188 return;
2191 flags = SYMBOL_REF_FLAGS (symbol);
2192 switch (v850_get_data_area (decl))
2194 case DATA_AREA_ZDA: flags |= SYMBOL_FLAG_ZDA; break;
2195 case DATA_AREA_TDA: flags |= SYMBOL_FLAG_TDA; break;
2196 case DATA_AREA_SDA: flags |= SYMBOL_FLAG_SDA; break;
2197 default: gcc_unreachable ();
2199 SYMBOL_REF_FLAGS (symbol) = flags;
2202 static void
2203 v850_encode_section_info (tree decl, rtx rtl, int first)
2205 default_encode_section_info (decl, rtl, first);
2207 if (TREE_CODE (decl) == VAR_DECL
2208 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2209 v850_encode_data_area (decl, XEXP (rtl, 0));
2212 /* Construct a JR instruction to a routine that will perform the equivalent of
2213 the RTL passed in as an argument. This RTL is a function epilogue that
2214 pops registers off the stack and possibly releases some extra stack space
2215 as well. The code has already verified that the RTL matches these
2216 requirements. */
2217 char *
2218 construct_restore_jr (rtx op)
2220 int count = XVECLEN (op, 0);
2221 int stack_bytes;
2222 unsigned long int mask;
2223 unsigned long int first;
2224 unsigned long int last;
2225 int i;
2226 static char buff [100]; /* XXX */
2228 if (count <= 2)
2230 error ("bogus JR construction: %d", count);
2231 return NULL;
2234 /* Work out how many bytes to pop off the stack before retrieving
2235 registers. */
2236 gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2237 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2238 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2240 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2242 /* Each pop will remove 4 bytes from the stack.... */
2243 stack_bytes -= (count - 2) * 4;
2245 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2246 if (stack_bytes != 0 && stack_bytes != 16)
2248 error ("bad amount of stack space removal: %d", stack_bytes);
2249 return NULL;
2252 /* Now compute the bit mask of registers to push. */
2253 mask = 0;
2254 for (i = 2; i < count; i++)
2256 rtx vector_element = XVECEXP (op, 0, i);
2258 gcc_assert (GET_CODE (vector_element) == SET);
2259 gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2260 gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2261 SImode));
2263 mask |= 1 << REGNO (SET_DEST (vector_element));
2266 /* Scan for the first register to pop. */
2267 for (first = 0; first < 32; first++)
2269 if (mask & (1 << first))
2270 break;
2273 gcc_assert (first < 32);
2275 /* Discover the last register to pop. */
2276 if (mask & (1 << LINK_POINTER_REGNUM))
2278 gcc_assert (stack_bytes == 16);
2280 last = LINK_POINTER_REGNUM;
2282 else
2284 gcc_assert (!stack_bytes);
2285 gcc_assert (mask & (1 << 29));
2287 last = 29;
2290 /* Note, it is possible to have gaps in the register mask.
2291 We ignore this here, and generate a JR anyway. We will
2292 be popping more registers than is strictly necessary, but
2293 it does save code space. */
2295 if (TARGET_LONG_CALLS)
2297 char name[40];
2299 if (first == last)
2300 sprintf (name, "__return_%s", reg_names [first]);
2301 else
2302 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2304 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2305 name, name);
2307 else
2309 if (first == last)
2310 sprintf (buff, "jr __return_%s", reg_names [first]);
2311 else
2312 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2315 return buff;
2319 /* Construct a JARL instruction to a routine that will perform the equivalent
2320 of the RTL passed as a parameter. This RTL is a function prologue that
2321 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2322 some stack space as well. The code has already verified that the RTL
2323 matches these requirements. */
2324 char *
2325 construct_save_jarl (rtx op)
2327 int count = XVECLEN (op, 0);
2328 int stack_bytes;
2329 unsigned long int mask;
2330 unsigned long int first;
2331 unsigned long int last;
2332 int i;
2333 static char buff [100]; /* XXX */
2335 if (count <= 2)
2337 error ("bogus JARL construction: %d\n", count);
2338 return NULL;
2341 /* Paranoia. */
2342 gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2343 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2344 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) == REG);
2345 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2347 /* Work out how many bytes to push onto the stack after storing the
2348 registers. */
2349 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2351 /* Each push will put 4 bytes from the stack.... */
2352 stack_bytes += (count - (TARGET_LONG_CALLS ? 3 : 2)) * 4;
2354 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2355 if (stack_bytes != 0 && stack_bytes != -16)
2357 error ("bad amount of stack space removal: %d", stack_bytes);
2358 return NULL;
2361 /* Now compute the bit mask of registers to push. */
2362 mask = 0;
2363 for (i = 1; i < count - (TARGET_LONG_CALLS ? 2 : 1); i++)
2365 rtx vector_element = XVECEXP (op, 0, i);
2367 gcc_assert (GET_CODE (vector_element) == SET);
2368 gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2369 gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2370 SImode));
2372 mask |= 1 << REGNO (SET_SRC (vector_element));
2375 /* Scan for the first register to push. */
2376 for (first = 0; first < 32; first++)
2378 if (mask & (1 << first))
2379 break;
2382 gcc_assert (first < 32);
2384 /* Discover the last register to push. */
2385 if (mask & (1 << LINK_POINTER_REGNUM))
2387 gcc_assert (stack_bytes == -16);
2389 last = LINK_POINTER_REGNUM;
2391 else
2393 gcc_assert (!stack_bytes);
2394 gcc_assert (mask & (1 << 29));
2396 last = 29;
2399 /* Note, it is possible to have gaps in the register mask.
2400 We ignore this here, and generate a JARL anyway. We will
2401 be pushing more registers than is strictly necessary, but
2402 it does save code space. */
2404 if (TARGET_LONG_CALLS)
2406 char name[40];
2408 if (first == last)
2409 sprintf (name, "__save_%s", reg_names [first]);
2410 else
2411 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2413 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2414 name, name);
2416 else
2418 if (first == last)
2419 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2420 else
2421 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2422 reg_names [last]);
2425 return buff;
2428 extern tree last_assemble_variable_decl;
2429 extern int size_directive_output;
2431 /* A version of asm_output_aligned_bss() that copes with the special
2432 data areas of the v850. */
2433 void
2434 v850_output_aligned_bss (FILE * file,
2435 tree decl,
2436 const char * name,
2437 unsigned HOST_WIDE_INT size,
2438 int align)
2440 switch (v850_get_data_area (decl))
2442 case DATA_AREA_ZDA:
2443 switch_to_section (zbss_section);
2444 break;
2446 case DATA_AREA_SDA:
2447 switch_to_section (sbss_section);
2448 break;
2450 case DATA_AREA_TDA:
2451 switch_to_section (tdata_section);
2453 default:
2454 switch_to_section (bss_section);
2455 break;
2458 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2459 #ifdef ASM_DECLARE_OBJECT_NAME
2460 last_assemble_variable_decl = decl;
2461 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2462 #else
2463 /* Standard thing is just output label for the object. */
2464 ASM_OUTPUT_LABEL (file, name);
2465 #endif /* ASM_DECLARE_OBJECT_NAME */
2466 ASM_OUTPUT_SKIP (file, size ? size : 1);
2469 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2470 void
2471 v850_output_common (FILE * file,
2472 tree decl,
2473 const char * name,
2474 int size,
2475 int align)
2477 if (decl == NULL_TREE)
2479 fprintf (file, "%s", COMMON_ASM_OP);
2481 else
2483 switch (v850_get_data_area (decl))
2485 case DATA_AREA_ZDA:
2486 fprintf (file, "%s", ZCOMMON_ASM_OP);
2487 break;
2489 case DATA_AREA_SDA:
2490 fprintf (file, "%s", SCOMMON_ASM_OP);
2491 break;
2493 case DATA_AREA_TDA:
2494 fprintf (file, "%s", TCOMMON_ASM_OP);
2495 break;
2497 default:
2498 fprintf (file, "%s", COMMON_ASM_OP);
2499 break;
2503 assemble_name (file, name);
2504 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2507 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2508 void
2509 v850_output_local (FILE * file,
2510 tree decl,
2511 const char * name,
2512 int size,
2513 int align)
2515 fprintf (file, "%s", LOCAL_ASM_OP);
2516 assemble_name (file, name);
2517 fprintf (file, "\n");
2519 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2522 /* Add data area to the given declaration if a ghs data area pragma is
2523 currently in effect (#pragma ghs startXXX/endXXX). */
2524 static void
2525 v850_insert_attributes (tree decl, tree * attr_ptr ATTRIBUTE_UNUSED )
2527 if (data_area_stack
2528 && data_area_stack->data_area
2529 && current_function_decl == NULL_TREE
2530 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2531 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2532 v850_set_data_area (decl, data_area_stack->data_area);
2534 /* Initialize the default names of the v850 specific sections,
2535 if this has not been done before. */
2537 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2539 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2540 = build_string (sizeof (".sdata")-1, ".sdata");
2542 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2543 = build_string (sizeof (".rosdata")-1, ".rosdata");
2545 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2546 = build_string (sizeof (".tdata")-1, ".tdata");
2548 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2549 = build_string (sizeof (".zdata")-1, ".zdata");
2551 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2552 = build_string (sizeof (".rozdata")-1, ".rozdata");
2555 if (current_function_decl == NULL_TREE
2556 && (TREE_CODE (decl) == VAR_DECL
2557 || TREE_CODE (decl) == CONST_DECL
2558 || TREE_CODE (decl) == FUNCTION_DECL)
2559 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2560 && !DECL_SECTION_NAME (decl))
2562 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2563 tree chosen_section;
2565 if (TREE_CODE (decl) == FUNCTION_DECL)
2566 kind = GHS_SECTION_KIND_TEXT;
2567 else
2569 /* First choose a section kind based on the data area of the decl. */
2570 switch (v850_get_data_area (decl))
2572 default:
2573 gcc_unreachable ();
2575 case DATA_AREA_SDA:
2576 kind = ((TREE_READONLY (decl))
2577 ? GHS_SECTION_KIND_ROSDATA
2578 : GHS_SECTION_KIND_SDATA);
2579 break;
2581 case DATA_AREA_TDA:
2582 kind = GHS_SECTION_KIND_TDATA;
2583 break;
2585 case DATA_AREA_ZDA:
2586 kind = ((TREE_READONLY (decl))
2587 ? GHS_SECTION_KIND_ROZDATA
2588 : GHS_SECTION_KIND_ZDATA);
2589 break;
2591 case DATA_AREA_NORMAL: /* default data area */
2592 if (TREE_READONLY (decl))
2593 kind = GHS_SECTION_KIND_RODATA;
2594 else if (DECL_INITIAL (decl))
2595 kind = GHS_SECTION_KIND_DATA;
2596 else
2597 kind = GHS_SECTION_KIND_BSS;
2601 /* Now, if the section kind has been explicitly renamed,
2602 then attach a section attribute. */
2603 chosen_section = GHS_current_section_names [(int) kind];
2605 /* Otherwise, if this kind of section needs an explicit section
2606 attribute, then also attach one. */
2607 if (chosen_section == NULL)
2608 chosen_section = GHS_default_section_names [(int) kind];
2610 if (chosen_section)
2612 /* Only set the section name if specified by a pragma, because
2613 otherwise it will force those variables to get allocated storage
2614 in this module, rather than by the linker. */
2615 DECL_SECTION_NAME (decl) = chosen_section;
2620 /* Construct a DISPOSE instruction that is the equivalent of
2621 the given RTX. We have already verified that this should
2622 be possible. */
2624 char *
2625 construct_dispose_instruction (rtx op)
2627 int count = XVECLEN (op, 0);
2628 int stack_bytes;
2629 unsigned long int mask;
2630 int i;
2631 static char buff[ 100 ]; /* XXX */
2632 int use_callt = 0;
2634 if (count <= 2)
2636 error ("bogus DISPOSE construction: %d", count);
2637 return NULL;
2640 /* Work out how many bytes to pop off the
2641 stack before retrieving registers. */
2642 gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2643 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2644 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2646 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2648 /* Each pop will remove 4 bytes from the stack.... */
2649 stack_bytes -= (count - 2) * 4;
2651 /* Make sure that the amount we are popping
2652 will fit into the DISPOSE instruction. */
2653 if (stack_bytes > 128)
2655 error ("too much stack space to dispose of: %d", stack_bytes);
2656 return NULL;
2659 /* Now compute the bit mask of registers to push. */
2660 mask = 0;
2662 for (i = 2; i < count; i++)
2664 rtx vector_element = XVECEXP (op, 0, i);
2666 gcc_assert (GET_CODE (vector_element) == SET);
2667 gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2668 gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2669 SImode));
2671 if (REGNO (SET_DEST (vector_element)) == 2)
2672 use_callt = 1;
2673 else
2674 mask |= 1 << REGNO (SET_DEST (vector_element));
2677 if (! TARGET_DISABLE_CALLT
2678 && (use_callt || stack_bytes == 0 || stack_bytes == 16))
2680 if (use_callt)
2682 sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
2683 return buff;
2685 else
2687 for (i = 20; i < 32; i++)
2688 if (mask & (1 << i))
2689 break;
2691 if (i == 31)
2692 sprintf (buff, "callt ctoff(__callt_return_r31c)");
2693 else
2694 sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
2695 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2698 else
2700 static char regs [100]; /* XXX */
2701 int done_one;
2703 /* Generate the DISPOSE instruction. Note we could just issue the
2704 bit mask as a number as the assembler can cope with this, but for
2705 the sake of our readers we turn it into a textual description. */
2706 regs[0] = 0;
2707 done_one = 0;
2709 for (i = 20; i < 32; i++)
2711 if (mask & (1 << i))
2713 int first;
2715 if (done_one)
2716 strcat (regs, ", ");
2717 else
2718 done_one = 1;
2720 first = i;
2721 strcat (regs, reg_names[ first ]);
2723 for (i++; i < 32; i++)
2724 if ((mask & (1 << i)) == 0)
2725 break;
2727 if (i > first + 1)
2729 strcat (regs, " - ");
2730 strcat (regs, reg_names[ i - 1 ] );
2735 sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
2738 return buff;
2741 /* Construct a PREPARE instruction that is the equivalent of
2742 the given RTL. We have already verified that this should
2743 be possible. */
2745 char *
2746 construct_prepare_instruction (rtx op)
2748 int count = XVECLEN (op, 0);
2749 int stack_bytes;
2750 unsigned long int mask;
2751 int i;
2752 static char buff[ 100 ]; /* XXX */
2753 int use_callt = 0;
2755 if (count <= 1)
2757 error ("bogus PREPEARE construction: %d", count);
2758 return NULL;
2761 /* Work out how many bytes to push onto
2762 the stack after storing the registers. */
2763 gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2764 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2765 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2767 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2769 /* Each push will put 4 bytes from the stack. */
2770 stack_bytes += (count - 1) * 4;
2772 /* Make sure that the amount we are popping
2773 will fit into the DISPOSE instruction. */
2774 if (stack_bytes < -128)
2776 error ("too much stack space to prepare: %d", stack_bytes);
2777 return NULL;
2780 /* Now compute the bit mask of registers to push. */
2781 mask = 0;
2782 for (i = 1; i < count; i++)
2784 rtx vector_element = XVECEXP (op, 0, i);
2786 gcc_assert (GET_CODE (vector_element) == SET);
2787 gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2788 gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2789 SImode));
2791 if (REGNO (SET_SRC (vector_element)) == 2)
2792 use_callt = 1;
2793 else
2794 mask |= 1 << REGNO (SET_SRC (vector_element));
2797 if ((! TARGET_DISABLE_CALLT)
2798 && (use_callt || stack_bytes == 0 || stack_bytes == -16))
2800 if (use_callt)
2802 sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
2803 return buff;
2806 for (i = 20; i < 32; i++)
2807 if (mask & (1 << i))
2808 break;
2810 if (i == 31)
2811 sprintf (buff, "callt ctoff(__callt_save_r31c)");
2812 else
2813 sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
2814 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2816 else
2818 static char regs [100]; /* XXX */
2819 int done_one;
2822 /* Generate the PREPARE instruction. Note we could just issue the
2823 bit mask as a number as the assembler can cope with this, but for
2824 the sake of our readers we turn it into a textual description. */
2825 regs[0] = 0;
2826 done_one = 0;
2828 for (i = 20; i < 32; i++)
2830 if (mask & (1 << i))
2832 int first;
2834 if (done_one)
2835 strcat (regs, ", ");
2836 else
2837 done_one = 1;
2839 first = i;
2840 strcat (regs, reg_names[ first ]);
2842 for (i++; i < 32; i++)
2843 if ((mask & (1 << i)) == 0)
2844 break;
2846 if (i > first + 1)
2848 strcat (regs, " - ");
2849 strcat (regs, reg_names[ i - 1 ] );
2854 sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
2857 return buff;
2860 /* Return an RTX indicating where the return address to the
2861 calling function can be found. */
2864 v850_return_addr (int count)
2866 if (count != 0)
2867 return const0_rtx;
2869 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
2872 /* Implement TARGET_ASM_INIT_SECTIONS. */
2874 static void
2875 v850_asm_init_sections (void)
2877 rosdata_section
2878 = get_unnamed_section (0, output_section_asm_op,
2879 "\t.section .rosdata,\"a\"");
2881 rozdata_section
2882 = get_unnamed_section (0, output_section_asm_op,
2883 "\t.section .rozdata,\"a\"");
2885 tdata_section
2886 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
2887 "\t.section .tdata,\"aw\"");
2889 zdata_section
2890 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
2891 "\t.section .zdata,\"aw\"");
2893 zbss_section
2894 = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
2895 output_section_asm_op,
2896 "\t.section .zbss,\"aw\"");
2899 static section *
2900 v850_select_section (tree exp,
2901 int reloc ATTRIBUTE_UNUSED,
2902 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
2904 if (TREE_CODE (exp) == VAR_DECL)
2906 int is_const;
2907 if (!TREE_READONLY (exp)
2908 || TREE_SIDE_EFFECTS (exp)
2909 || !DECL_INITIAL (exp)
2910 || (DECL_INITIAL (exp) != error_mark_node
2911 && !TREE_CONSTANT (DECL_INITIAL (exp))))
2912 is_const = FALSE;
2913 else
2914 is_const = TRUE;
2916 switch (v850_get_data_area (exp))
2918 case DATA_AREA_ZDA:
2919 return is_const ? rozdata_section : zdata_section;
2921 case DATA_AREA_TDA:
2922 return tdata_section;
2924 case DATA_AREA_SDA:
2925 return is_const ? rosdata_section : sdata_section;
2927 default:
2928 return is_const ? readonly_data_section : data_section;
2931 return readonly_data_section;
2934 /* Worker function for TARGET_RETURN_IN_MEMORY. */
2936 static bool
2937 v850_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2939 /* Return values > 8 bytes in length in memory. */
2940 return int_size_in_bytes (type) > 8 || TYPE_MODE (type) == BLKmode;
2943 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
2945 static void
2946 v850_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
2947 enum machine_mode mode ATTRIBUTE_UNUSED,
2948 tree type ATTRIBUTE_UNUSED,
2949 int *pretend_arg_size ATTRIBUTE_UNUSED,
2950 int second_time ATTRIBUTE_UNUSED)
2952 ca->anonymous_args = (!TARGET_GHS ? 1 : 0);
2955 #include "gt-v850.h"