Dead
[official-gcc.git] / gomp-20050608-branch / gcc / config / v850 / v850.c
blob7629a7af3a7c293bcc4b03f9db73debe68539d95
1 /* Subroutines for insn-output.c for NEC V850 series
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "toplev.h"
41 #include "ggc.h"
42 #include "integrate.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "target-def.h"
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 *);
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 (tree, 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 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 #undef TARGET_ENCODE_SECTION_INFO
121 #define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
123 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
124 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
126 #undef TARGET_DEFAULT_TARGET_FLAGS
127 #define TARGET_DEFAULT_TARGET_FLAGS (MASK_DEFAULT | MASK_APP_REGS)
128 #undef TARGET_HANDLE_OPTION
129 #define TARGET_HANDLE_OPTION v850_handle_option
131 #undef TARGET_RTX_COSTS
132 #define TARGET_RTX_COSTS v850_rtx_costs
134 #undef TARGET_ADDRESS_COST
135 #define TARGET_ADDRESS_COST hook_int_rtx_0
137 #undef TARGET_MACHINE_DEPENDENT_REORG
138 #define TARGET_MACHINE_DEPENDENT_REORG v850_reorg
140 #undef TARGET_PROMOTE_PROTOTYPES
141 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
143 #undef TARGET_RETURN_IN_MEMORY
144 #define TARGET_RETURN_IN_MEMORY v850_return_in_memory
146 #undef TARGET_PASS_BY_REFERENCE
147 #define TARGET_PASS_BY_REFERENCE v850_pass_by_reference
149 #undef TARGET_CALLEE_COPIES
150 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
152 #undef TARGET_SETUP_INCOMING_VARARGS
153 #define TARGET_SETUP_INCOMING_VARARGS v850_setup_incoming_varargs
155 #undef TARGET_ARG_PARTIAL_BYTES
156 #define TARGET_ARG_PARTIAL_BYTES v850_arg_partial_bytes
158 struct gcc_target targetm = TARGET_INITIALIZER;
160 /* Set the maximum size of small memory area TYPE to the value given
161 by VALUE. Return true if VALUE was syntactically correct. VALUE
162 starts with the argument separator: either "-" or "=". */
164 static bool
165 v850_handle_memory_option (enum small_memory_type type, const char *value)
167 int i, size;
169 if (*value != '-' && *value != '=')
170 return false;
172 value++;
173 for (i = 0; value[i]; i++)
174 if (!ISDIGIT (value[i]))
175 return false;
177 size = atoi (value);
178 if (size > small_memory[type].physical_max)
179 error ("value passed to %<-m%s%> is too large", small_memory[type].name);
180 else
181 small_memory[type].max = size;
182 return true;
185 /* Implement TARGET_HANDLE_OPTION. */
187 static bool
188 v850_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
190 switch (code)
192 case OPT_mspace:
193 target_flags |= MASK_EP | MASK_PROLOG_FUNCTION;
194 return true;
196 case OPT_mv850:
197 target_flags &= ~(MASK_CPU ^ MASK_V850);
198 return true;
200 case OPT_mv850e:
201 case OPT_mv850e1:
202 target_flags &= ~(MASK_CPU ^ MASK_V850E);
203 return true;
205 case OPT_mtda:
206 return v850_handle_memory_option (SMALL_MEMORY_TDA, arg);
208 case OPT_msda:
209 return v850_handle_memory_option (SMALL_MEMORY_SDA, arg);
211 case OPT_mzda:
212 return v850_handle_memory_option (SMALL_MEMORY_ZDA, arg);
214 default:
215 return true;
219 static bool
220 v850_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
221 enum machine_mode mode, tree type,
222 bool named ATTRIBUTE_UNUSED)
224 unsigned HOST_WIDE_INT size;
226 if (type)
227 size = int_size_in_bytes (type);
228 else
229 size = GET_MODE_SIZE (mode);
231 return size > 8;
234 /* Return an RTX to represent where a value with mode MODE will be returned
235 from a function. If the result is 0, the argument is pushed. */
238 function_arg (CUMULATIVE_ARGS * cum,
239 enum machine_mode mode,
240 tree type,
241 int named)
243 rtx result = 0;
244 int size, align;
246 if (TARGET_GHS && !named)
247 return NULL_RTX;
249 if (mode == BLKmode)
250 size = int_size_in_bytes (type);
251 else
252 size = GET_MODE_SIZE (mode);
254 if (size < 1)
255 return 0;
257 if (type)
258 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
259 else
260 align = size;
262 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
264 if (cum->nbytes > 4 * UNITS_PER_WORD)
265 return 0;
267 if (type == NULL_TREE
268 && cum->nbytes + size > 4 * UNITS_PER_WORD)
269 return 0;
271 switch (cum->nbytes / UNITS_PER_WORD)
273 case 0:
274 result = gen_rtx_REG (mode, 6);
275 break;
276 case 1:
277 result = gen_rtx_REG (mode, 7);
278 break;
279 case 2:
280 result = gen_rtx_REG (mode, 8);
281 break;
282 case 3:
283 result = gen_rtx_REG (mode, 9);
284 break;
285 default:
286 result = 0;
289 return result;
293 /* Return the number of bytes which must be put into registers
294 for values which are part in registers and part in memory. */
296 static int
297 v850_arg_partial_bytes (CUMULATIVE_ARGS * cum, enum machine_mode mode,
298 tree type, bool named)
300 int size, align;
302 if (TARGET_GHS && !named)
303 return 0;
305 if (mode == BLKmode)
306 size = int_size_in_bytes (type);
307 else
308 size = GET_MODE_SIZE (mode);
310 if (type)
311 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
312 else
313 align = size;
315 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
317 if (cum->nbytes > 4 * UNITS_PER_WORD)
318 return 0;
320 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
321 return 0;
323 if (type == NULL_TREE
324 && cum->nbytes + size > 4 * UNITS_PER_WORD)
325 return 0;
327 return 4 * UNITS_PER_WORD - cum->nbytes;
331 /* Return the high and low words of a CONST_DOUBLE */
333 static void
334 const_double_split (rtx x, HOST_WIDE_INT * p_high, HOST_WIDE_INT * p_low)
336 if (GET_CODE (x) == CONST_DOUBLE)
338 long t[2];
339 REAL_VALUE_TYPE rv;
341 switch (GET_MODE (x))
343 case DFmode:
344 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
345 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
346 *p_high = t[1]; /* since v850 is little endian */
347 *p_low = t[0]; /* high is second word */
348 return;
350 case SFmode:
351 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
352 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
353 *p_low = 0;
354 return;
356 case VOIDmode:
357 case DImode:
358 *p_high = CONST_DOUBLE_HIGH (x);
359 *p_low = CONST_DOUBLE_LOW (x);
360 return;
362 default:
363 break;
367 fatal_insn ("const_double_split got a bad insn:", x);
371 /* Return the cost of the rtx R with code CODE. */
373 static int
374 const_costs_int (HOST_WIDE_INT value, int zero_cost)
376 if (CONST_OK_FOR_I (value))
377 return zero_cost;
378 else if (CONST_OK_FOR_J (value))
379 return 1;
380 else if (CONST_OK_FOR_K (value))
381 return 2;
382 else
383 return 4;
386 static int
387 const_costs (rtx r, enum rtx_code c)
389 HOST_WIDE_INT high, low;
391 switch (c)
393 case CONST_INT:
394 return const_costs_int (INTVAL (r), 0);
396 case CONST_DOUBLE:
397 const_double_split (r, &high, &low);
398 if (GET_MODE (r) == SFmode)
399 return const_costs_int (high, 1);
400 else
401 return const_costs_int (high, 1) + const_costs_int (low, 1);
403 case SYMBOL_REF:
404 case LABEL_REF:
405 case CONST:
406 return 2;
408 case HIGH:
409 return 1;
411 default:
412 return 4;
416 static bool
417 v850_rtx_costs (rtx x,
418 int code,
419 int outer_code ATTRIBUTE_UNUSED,
420 int * total)
422 switch (code)
424 case CONST_INT:
425 case CONST_DOUBLE:
426 case CONST:
427 case SYMBOL_REF:
428 case LABEL_REF:
429 *total = COSTS_N_INSNS (const_costs (x, code));
430 return true;
432 case MOD:
433 case DIV:
434 case UMOD:
435 case UDIV:
436 if (TARGET_V850E && optimize_size)
437 *total = 6;
438 else
439 *total = 60;
440 return true;
442 case MULT:
443 if (TARGET_V850E
444 && ( GET_MODE (x) == SImode
445 || GET_MODE (x) == HImode
446 || GET_MODE (x) == QImode))
448 if (GET_CODE (XEXP (x, 1)) == REG)
449 *total = 4;
450 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
452 if (CONST_OK_FOR_O (INTVAL (XEXP (x, 1))))
453 *total = 6;
454 else if (CONST_OK_FOR_K (INTVAL (XEXP (x, 1))))
455 *total = 10;
458 else
459 *total = 20;
460 return true;
462 default:
463 return false;
467 /* Print operand X using operand code CODE to assembly language output file
468 FILE. */
470 void
471 print_operand (FILE * file, rtx x, int code)
473 HOST_WIDE_INT high, low;
475 switch (code)
477 case 'c':
478 /* We use 'c' operands with symbols for .vtinherit */
479 if (GET_CODE (x) == SYMBOL_REF)
481 output_addr_const(file, x);
482 break;
484 /* fall through */
485 case 'b':
486 case 'B':
487 case 'C':
488 switch ((code == 'B' || code == 'C')
489 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
491 case NE:
492 if (code == 'c' || code == 'C')
493 fprintf (file, "nz");
494 else
495 fprintf (file, "ne");
496 break;
497 case EQ:
498 if (code == 'c' || code == 'C')
499 fprintf (file, "z");
500 else
501 fprintf (file, "e");
502 break;
503 case GE:
504 fprintf (file, "ge");
505 break;
506 case GT:
507 fprintf (file, "gt");
508 break;
509 case LE:
510 fprintf (file, "le");
511 break;
512 case LT:
513 fprintf (file, "lt");
514 break;
515 case GEU:
516 fprintf (file, "nl");
517 break;
518 case GTU:
519 fprintf (file, "h");
520 break;
521 case LEU:
522 fprintf (file, "nh");
523 break;
524 case LTU:
525 fprintf (file, "l");
526 break;
527 default:
528 gcc_unreachable ();
530 break;
531 case 'F': /* high word of CONST_DOUBLE */
532 switch (GET_CODE (x))
534 case CONST_INT:
535 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
536 break;
538 case CONST_DOUBLE:
539 const_double_split (x, &high, &low);
540 fprintf (file, "%ld", (long) high);
541 break;
543 default:
544 gcc_unreachable ();
546 break;
547 case 'G': /* low word of CONST_DOUBLE */
548 switch (GET_CODE (x))
550 case CONST_INT:
551 fprintf (file, "%ld", (long) INTVAL (x));
552 break;
554 case CONST_DOUBLE:
555 const_double_split (x, &high, &low);
556 fprintf (file, "%ld", (long) low);
557 break;
559 default:
560 gcc_unreachable ();
562 break;
563 case 'L':
564 fprintf (file, "%d\n", (int)(INTVAL (x) & 0xffff));
565 break;
566 case 'M':
567 fprintf (file, "%d", exact_log2 (INTVAL (x)));
568 break;
569 case 'O':
570 gcc_assert (special_symbolref_operand (x, VOIDmode));
572 if (GET_CODE (x) == CONST)
573 x = XEXP (XEXP (x, 0), 0);
574 else
575 gcc_assert (GET_CODE (x) == SYMBOL_REF);
577 if (SYMBOL_REF_ZDA_P (x))
578 fprintf (file, "zdaoff");
579 else if (SYMBOL_REF_SDA_P (x))
580 fprintf (file, "sdaoff");
581 else if (SYMBOL_REF_TDA_P (x))
582 fprintf (file, "tdaoff");
583 else
584 gcc_unreachable ();
585 break;
586 case 'P':
587 gcc_assert (special_symbolref_operand (x, VOIDmode));
588 output_addr_const (file, x);
589 break;
590 case 'Q':
591 gcc_assert (special_symbolref_operand (x, VOIDmode));
593 if (GET_CODE (x) == CONST)
594 x = XEXP (XEXP (x, 0), 0);
595 else
596 gcc_assert (GET_CODE (x) == SYMBOL_REF);
598 if (SYMBOL_REF_ZDA_P (x))
599 fprintf (file, "r0");
600 else if (SYMBOL_REF_SDA_P (x))
601 fprintf (file, "gp");
602 else if (SYMBOL_REF_TDA_P (x))
603 fprintf (file, "ep");
604 else
605 gcc_unreachable ();
606 break;
607 case 'R': /* 2nd word of a double. */
608 switch (GET_CODE (x))
610 case REG:
611 fprintf (file, reg_names[REGNO (x) + 1]);
612 break;
613 case MEM:
614 x = XEXP (adjust_address (x, SImode, 4), 0);
615 print_operand_address (file, x);
616 if (GET_CODE (x) == CONST_INT)
617 fprintf (file, "[r0]");
618 break;
620 default:
621 break;
623 break;
624 case 'S':
626 /* If it's a reference to a TDA variable, use sst/sld vs. st/ld. */
627 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
628 fputs ("s", file);
630 break;
632 case 'T':
634 /* Like an 'S' operand above, but for unsigned loads only. */
635 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
636 fputs ("s", file);
638 break;
640 case 'W': /* print the instruction suffix */
641 switch (GET_MODE (x))
643 default:
644 gcc_unreachable ();
646 case QImode: fputs (".b", file); break;
647 case HImode: fputs (".h", file); break;
648 case SImode: fputs (".w", file); break;
649 case SFmode: fputs (".w", file); break;
651 break;
652 case '.': /* register r0 */
653 fputs (reg_names[0], file);
654 break;
655 case 'z': /* reg or zero */
656 if (GET_CODE (x) == REG)
657 fputs (reg_names[REGNO (x)], file);
658 else
660 gcc_assert (x == const0_rtx);
661 fputs (reg_names[0], file);
663 break;
664 default:
665 switch (GET_CODE (x))
667 case MEM:
668 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
669 output_address (gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, 0),
670 XEXP (x, 0)));
671 else
672 output_address (XEXP (x, 0));
673 break;
675 case REG:
676 fputs (reg_names[REGNO (x)], file);
677 break;
678 case SUBREG:
679 fputs (reg_names[subreg_regno (x)], file);
680 break;
681 case CONST_INT:
682 case SYMBOL_REF:
683 case CONST:
684 case LABEL_REF:
685 case CODE_LABEL:
686 print_operand_address (file, x);
687 break;
688 default:
689 gcc_unreachable ();
691 break;
697 /* Output assembly language output for the address ADDR to FILE. */
699 void
700 print_operand_address (FILE * file, rtx addr)
702 switch (GET_CODE (addr))
704 case REG:
705 fprintf (file, "0[");
706 print_operand (file, addr, 0);
707 fprintf (file, "]");
708 break;
709 case LO_SUM:
710 if (GET_CODE (XEXP (addr, 0)) == REG)
712 /* reg,foo */
713 fprintf (file, "lo(");
714 print_operand (file, XEXP (addr, 1), 0);
715 fprintf (file, ")[");
716 print_operand (file, XEXP (addr, 0), 0);
717 fprintf (file, "]");
719 break;
720 case PLUS:
721 if (GET_CODE (XEXP (addr, 0)) == REG
722 || GET_CODE (XEXP (addr, 0)) == SUBREG)
724 /* reg,foo */
725 print_operand (file, XEXP (addr, 1), 0);
726 fprintf (file, "[");
727 print_operand (file, XEXP (addr, 0), 0);
728 fprintf (file, "]");
730 else
732 print_operand (file, XEXP (addr, 0), 0);
733 fprintf (file, "+");
734 print_operand (file, XEXP (addr, 1), 0);
736 break;
737 case SYMBOL_REF:
739 const char *off_name = NULL;
740 const char *reg_name = NULL;
742 if (SYMBOL_REF_ZDA_P (addr))
744 off_name = "zdaoff";
745 reg_name = "r0";
747 else if (SYMBOL_REF_SDA_P (addr))
749 off_name = "sdaoff";
750 reg_name = "gp";
752 else if (SYMBOL_REF_TDA_P (addr))
754 off_name = "tdaoff";
755 reg_name = "ep";
758 if (off_name)
759 fprintf (file, "%s(", off_name);
760 output_addr_const (file, addr);
761 if (reg_name)
762 fprintf (file, ")[%s]", reg_name);
764 break;
765 case CONST:
766 if (special_symbolref_operand (addr, VOIDmode))
768 rtx x = XEXP (XEXP (addr, 0), 0);
769 const char *off_name;
770 const char *reg_name;
772 if (SYMBOL_REF_ZDA_P (x))
774 off_name = "zdaoff";
775 reg_name = "r0";
777 else if (SYMBOL_REF_SDA_P (x))
779 off_name = "sdaoff";
780 reg_name = "gp";
782 else if (SYMBOL_REF_TDA_P (x))
784 off_name = "tdaoff";
785 reg_name = "ep";
787 else
788 gcc_unreachable ();
790 fprintf (file, "%s(", off_name);
791 output_addr_const (file, addr);
792 fprintf (file, ")[%s]", reg_name);
794 else
795 output_addr_const (file, addr);
796 break;
797 default:
798 output_addr_const (file, addr);
799 break;
803 /* When assemble_integer is used to emit the offsets for a switch
804 table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
805 output_addr_const will normally barf at this, but it is OK to omit
806 the truncate and just emit the difference of the two labels. The
807 .hword directive will automatically handle the truncation for us.
809 Returns 1 if rtx was handled, 0 otherwise. */
812 v850_output_addr_const_extra (FILE * file, rtx x)
814 if (GET_CODE (x) != TRUNCATE)
815 return 0;
817 x = XEXP (x, 0);
819 /* We must also handle the case where the switch table was passed a
820 constant value and so has been collapsed. In this case the first
821 label will have been deleted. In such a case it is OK to emit
822 nothing, since the table will not be used.
823 (cf gcc.c-torture/compile/990801-1.c). */
824 if (GET_CODE (x) == MINUS
825 && GET_CODE (XEXP (x, 0)) == LABEL_REF
826 && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
827 && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
828 return 1;
830 output_addr_const (file, x);
831 return 1;
834 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
835 point value. */
837 const char *
838 output_move_single (rtx * operands)
840 rtx dst = operands[0];
841 rtx src = operands[1];
843 if (REG_P (dst))
845 if (REG_P (src))
846 return "mov %1,%0";
848 else if (GET_CODE (src) == CONST_INT)
850 HOST_WIDE_INT value = INTVAL (src);
852 if (CONST_OK_FOR_J (value)) /* Signed 5 bit immediate. */
853 return "mov %1,%0";
855 else if (CONST_OK_FOR_K (value)) /* Signed 16 bit immediate. */
856 return "movea lo(%1),%.,%0";
858 else if (CONST_OK_FOR_L (value)) /* Upper 16 bits were set. */
859 return "movhi hi(%1),%.,%0";
861 /* A random constant. */
862 else if (TARGET_V850E)
863 return "mov %1,%0";
864 else
865 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
868 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
870 HOST_WIDE_INT high, low;
872 const_double_split (src, &high, &low);
874 if (CONST_OK_FOR_J (high)) /* Signed 5 bit immediate. */
875 return "mov %F1,%0";
877 else if (CONST_OK_FOR_K (high)) /* Signed 16 bit immediate. */
878 return "movea lo(%F1),%.,%0";
880 else if (CONST_OK_FOR_L (high)) /* Upper 16 bits were set. */
881 return "movhi hi(%F1),%.,%0";
883 /* A random constant. */
884 else if (TARGET_V850E)
885 return "mov %F1,%0";
887 else
888 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
891 else if (GET_CODE (src) == MEM)
892 return "%S1ld%W1 %1,%0";
894 else if (special_symbolref_operand (src, VOIDmode))
895 return "movea %O1(%P1),%Q1,%0";
897 else if (GET_CODE (src) == LABEL_REF
898 || GET_CODE (src) == SYMBOL_REF
899 || GET_CODE (src) == CONST)
901 if (TARGET_V850E)
902 return "mov hilo(%1),%0";
903 else
904 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
907 else if (GET_CODE (src) == HIGH)
908 return "movhi hi(%1),%.,%0";
910 else if (GET_CODE (src) == LO_SUM)
912 operands[2] = XEXP (src, 0);
913 operands[3] = XEXP (src, 1);
914 return "movea lo(%3),%2,%0";
918 else if (GET_CODE (dst) == MEM)
920 if (REG_P (src))
921 return "%S0st%W0 %1,%0";
923 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
924 return "%S0st%W0 %.,%0";
926 else if (GET_CODE (src) == CONST_DOUBLE
927 && CONST0_RTX (GET_MODE (dst)) == src)
928 return "%S0st%W0 %.,%0";
931 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
932 return "";
936 /* Return appropriate code to load up an 8 byte integer or
937 floating point value */
939 const char *
940 output_move_double (rtx * operands)
942 enum machine_mode mode = GET_MODE (operands[0]);
943 rtx dst = operands[0];
944 rtx src = operands[1];
946 if (register_operand (dst, mode)
947 && register_operand (src, mode))
949 if (REGNO (src) + 1 == REGNO (dst))
950 return "mov %R1,%R0\n\tmov %1,%0";
951 else
952 return "mov %1,%0\n\tmov %R1,%R0";
955 /* Storing 0 */
956 if (GET_CODE (dst) == MEM
957 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
958 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
959 return "st.w %.,%0\n\tst.w %.,%R0";
961 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
963 HOST_WIDE_INT high_low[2];
964 int i;
965 rtx xop[10];
967 if (GET_CODE (src) == CONST_DOUBLE)
968 const_double_split (src, &high_low[1], &high_low[0]);
969 else
971 high_low[0] = INTVAL (src);
972 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
975 for (i = 0; i < 2; i++)
977 xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
978 xop[1] = GEN_INT (high_low[i]);
979 output_asm_insn (output_move_single (xop), xop);
982 return "";
985 if (GET_CODE (src) == MEM)
987 int ptrreg = -1;
988 int dreg = REGNO (dst);
989 rtx inside = XEXP (src, 0);
991 if (GET_CODE (inside) == REG)
992 ptrreg = REGNO (inside);
993 else if (GET_CODE (inside) == SUBREG)
994 ptrreg = subreg_regno (inside);
995 else if (GET_CODE (inside) == PLUS)
996 ptrreg = REGNO (XEXP (inside, 0));
997 else if (GET_CODE (inside) == LO_SUM)
998 ptrreg = REGNO (XEXP (inside, 0));
1000 if (dreg == ptrreg)
1001 return "ld.w %R1,%R0\n\tld.w %1,%0";
1004 if (GET_CODE (src) == MEM)
1005 return "ld.w %1,%0\n\tld.w %R1,%R0";
1007 if (GET_CODE (dst) == MEM)
1008 return "st.w %1,%0\n\tst.w %R1,%R0";
1010 return "mov %1,%0\n\tmov %R1,%R0";
1014 /* Return maximum offset supported for a short EP memory reference of mode
1015 MODE and signedness UNSIGNEDP. */
1017 static int
1018 ep_memory_offset (enum machine_mode mode, int unsignedp ATTRIBUTE_UNUSED)
1020 int max_offset = 0;
1022 switch (mode)
1024 case QImode:
1025 if (TARGET_SMALL_SLD)
1026 max_offset = (1 << 4);
1027 else if (TARGET_V850E
1028 && ( ( unsignedp && ! TARGET_US_BIT_SET)
1029 || (! unsignedp && TARGET_US_BIT_SET)))
1030 max_offset = (1 << 4);
1031 else
1032 max_offset = (1 << 7);
1033 break;
1035 case HImode:
1036 if (TARGET_SMALL_SLD)
1037 max_offset = (1 << 5);
1038 else if (TARGET_V850E
1039 && ( ( unsignedp && ! TARGET_US_BIT_SET)
1040 || (! unsignedp && TARGET_US_BIT_SET)))
1041 max_offset = (1 << 5);
1042 else
1043 max_offset = (1 << 8);
1044 break;
1046 case SImode:
1047 case SFmode:
1048 max_offset = (1 << 8);
1049 break;
1051 default:
1052 break;
1055 return max_offset;
1058 /* Return true if OP is a valid short EP memory reference */
1061 ep_memory_operand (rtx op, enum machine_mode mode, int unsigned_load)
1063 rtx addr, op0, op1;
1064 int max_offset;
1065 int mask;
1067 /* If we are not using the EP register on a per-function basis
1068 then do not allow this optimisation at all. This is to
1069 prevent the use of the SLD/SST instructions which cannot be
1070 guaranteed to work properly due to a hardware bug. */
1071 if (!TARGET_EP)
1072 return FALSE;
1074 if (GET_CODE (op) != MEM)
1075 return FALSE;
1077 max_offset = ep_memory_offset (mode, unsigned_load);
1079 mask = GET_MODE_SIZE (mode) - 1;
1081 addr = XEXP (op, 0);
1082 if (GET_CODE (addr) == CONST)
1083 addr = XEXP (addr, 0);
1085 switch (GET_CODE (addr))
1087 default:
1088 break;
1090 case SYMBOL_REF:
1091 return SYMBOL_REF_TDA_P (addr);
1093 case REG:
1094 return REGNO (addr) == EP_REGNUM;
1096 case PLUS:
1097 op0 = XEXP (addr, 0);
1098 op1 = XEXP (addr, 1);
1099 if (GET_CODE (op1) == CONST_INT
1100 && INTVAL (op1) < max_offset
1101 && INTVAL (op1) >= 0
1102 && (INTVAL (op1) & mask) == 0)
1104 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1105 return TRUE;
1107 if (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_TDA_P (op0))
1108 return TRUE;
1110 break;
1113 return FALSE;
1116 /* Substitute memory references involving a pointer, to use the ep pointer,
1117 taking care to save and preserve the ep. */
1119 static void
1120 substitute_ep_register (rtx first_insn,
1121 rtx last_insn,
1122 int uses,
1123 int regno,
1124 rtx * p_r1,
1125 rtx * p_ep)
1127 rtx reg = gen_rtx_REG (Pmode, regno);
1128 rtx insn;
1130 if (!*p_r1)
1132 regs_ever_live[1] = 1;
1133 *p_r1 = gen_rtx_REG (Pmode, 1);
1134 *p_ep = gen_rtx_REG (Pmode, 30);
1137 if (TARGET_DEBUG)
1138 fprintf (stderr, "\
1139 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1140 2 * (uses - 3), uses, reg_names[regno],
1141 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1142 INSN_UID (first_insn), INSN_UID (last_insn));
1144 if (GET_CODE (first_insn) == NOTE)
1145 first_insn = next_nonnote_insn (first_insn);
1147 last_insn = next_nonnote_insn (last_insn);
1148 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1150 if (GET_CODE (insn) == INSN)
1152 rtx pattern = single_set (insn);
1154 /* Replace the memory references. */
1155 if (pattern)
1157 rtx *p_mem;
1158 /* Memory operands are signed by default. */
1159 int unsignedp = FALSE;
1161 if (GET_CODE (SET_DEST (pattern)) == MEM
1162 && GET_CODE (SET_SRC (pattern)) == MEM)
1163 p_mem = (rtx *)0;
1165 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1166 p_mem = &SET_DEST (pattern);
1168 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1169 p_mem = &SET_SRC (pattern);
1171 else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1172 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1173 p_mem = &XEXP (SET_SRC (pattern), 0);
1175 else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1176 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1178 p_mem = &XEXP (SET_SRC (pattern), 0);
1179 unsignedp = TRUE;
1181 else
1182 p_mem = (rtx *)0;
1184 if (p_mem)
1186 rtx addr = XEXP (*p_mem, 0);
1188 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1189 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1191 else if (GET_CODE (addr) == PLUS
1192 && GET_CODE (XEXP (addr, 0)) == REG
1193 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1194 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1195 && ((INTVAL (XEXP (addr, 1)))
1196 < ep_memory_offset (GET_MODE (*p_mem),
1197 unsignedp))
1198 && ((INTVAL (XEXP (addr, 1))) >= 0))
1199 *p_mem = change_address (*p_mem, VOIDmode,
1200 gen_rtx_PLUS (Pmode,
1201 *p_ep,
1202 XEXP (addr, 1)));
1208 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1209 insn = prev_nonnote_insn (first_insn);
1210 if (insn && GET_CODE (insn) == INSN
1211 && GET_CODE (PATTERN (insn)) == SET
1212 && SET_DEST (PATTERN (insn)) == *p_ep
1213 && SET_SRC (PATTERN (insn)) == *p_r1)
1214 delete_insn (insn);
1215 else
1216 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1218 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1219 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1223 /* TARGET_MACHINE_DEPENDENT_REORG. On the 850, we use it to implement
1224 the -mep mode to copy heavily used pointers to ep to use the implicit
1225 addressing. */
1227 static void
1228 v850_reorg (void)
1230 struct
1232 int uses;
1233 rtx first_insn;
1234 rtx last_insn;
1236 regs[FIRST_PSEUDO_REGISTER];
1238 int i;
1239 int use_ep = FALSE;
1240 rtx r1 = NULL_RTX;
1241 rtx ep = NULL_RTX;
1242 rtx insn;
1243 rtx pattern;
1245 /* If not ep mode, just return now. */
1246 if (!TARGET_EP)
1247 return;
1249 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1251 regs[i].uses = 0;
1252 regs[i].first_insn = NULL_RTX;
1253 regs[i].last_insn = NULL_RTX;
1256 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1258 switch (GET_CODE (insn))
1260 /* End of basic block */
1261 default:
1262 if (!use_ep)
1264 int max_uses = -1;
1265 int max_regno = -1;
1267 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1269 if (max_uses < regs[i].uses)
1271 max_uses = regs[i].uses;
1272 max_regno = i;
1276 if (max_uses > 3)
1277 substitute_ep_register (regs[max_regno].first_insn,
1278 regs[max_regno].last_insn,
1279 max_uses, max_regno, &r1, &ep);
1282 use_ep = FALSE;
1283 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1285 regs[i].uses = 0;
1286 regs[i].first_insn = NULL_RTX;
1287 regs[i].last_insn = NULL_RTX;
1289 break;
1291 case NOTE:
1292 break;
1294 case INSN:
1295 pattern = single_set (insn);
1297 /* See if there are any memory references we can shorten */
1298 if (pattern)
1300 rtx src = SET_SRC (pattern);
1301 rtx dest = SET_DEST (pattern);
1302 rtx mem;
1303 /* Memory operands are signed by default. */
1304 int unsignedp = FALSE;
1306 /* We might have (SUBREG (MEM)) here, so just get rid of the
1307 subregs to make this code simpler. */
1308 if (GET_CODE (dest) == SUBREG
1309 && (GET_CODE (SUBREG_REG (dest)) == MEM
1310 || GET_CODE (SUBREG_REG (dest)) == REG))
1311 alter_subreg (&dest);
1312 if (GET_CODE (src) == SUBREG
1313 && (GET_CODE (SUBREG_REG (src)) == MEM
1314 || GET_CODE (SUBREG_REG (src)) == REG))
1315 alter_subreg (&src);
1317 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1318 mem = NULL_RTX;
1320 else if (GET_CODE (dest) == MEM)
1321 mem = dest;
1323 else if (GET_CODE (src) == MEM)
1324 mem = src;
1326 else if (GET_CODE (src) == SIGN_EXTEND
1327 && GET_CODE (XEXP (src, 0)) == MEM)
1328 mem = XEXP (src, 0);
1330 else if (GET_CODE (src) == ZERO_EXTEND
1331 && GET_CODE (XEXP (src, 0)) == MEM)
1333 mem = XEXP (src, 0);
1334 unsignedp = TRUE;
1336 else
1337 mem = NULL_RTX;
1339 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1340 use_ep = TRUE;
1342 else if (!use_ep && mem
1343 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1345 rtx addr = XEXP (mem, 0);
1346 int regno = -1;
1347 int short_p;
1349 if (GET_CODE (addr) == REG)
1351 short_p = TRUE;
1352 regno = REGNO (addr);
1355 else if (GET_CODE (addr) == PLUS
1356 && GET_CODE (XEXP (addr, 0)) == REG
1357 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1358 && ((INTVAL (XEXP (addr, 1)))
1359 < ep_memory_offset (GET_MODE (mem), unsignedp))
1360 && ((INTVAL (XEXP (addr, 1))) >= 0))
1362 short_p = TRUE;
1363 regno = REGNO (XEXP (addr, 0));
1366 else
1367 short_p = FALSE;
1369 if (short_p)
1371 regs[regno].uses++;
1372 regs[regno].last_insn = insn;
1373 if (!regs[regno].first_insn)
1374 regs[regno].first_insn = insn;
1378 /* Loading up a register in the basic block zaps any savings
1379 for the register */
1380 if (GET_CODE (dest) == REG)
1382 enum machine_mode mode = GET_MODE (dest);
1383 int regno;
1384 int endregno;
1386 regno = REGNO (dest);
1387 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1389 if (!use_ep)
1391 /* See if we can use the pointer before this
1392 modification. */
1393 int max_uses = -1;
1394 int max_regno = -1;
1396 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1398 if (max_uses < regs[i].uses)
1400 max_uses = regs[i].uses;
1401 max_regno = i;
1405 if (max_uses > 3
1406 && max_regno >= regno
1407 && max_regno < endregno)
1409 substitute_ep_register (regs[max_regno].first_insn,
1410 regs[max_regno].last_insn,
1411 max_uses, max_regno, &r1,
1412 &ep);
1414 /* Since we made a substitution, zap all remembered
1415 registers. */
1416 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1418 regs[i].uses = 0;
1419 regs[i].first_insn = NULL_RTX;
1420 regs[i].last_insn = NULL_RTX;
1425 for (i = regno; i < endregno; i++)
1427 regs[i].uses = 0;
1428 regs[i].first_insn = NULL_RTX;
1429 regs[i].last_insn = NULL_RTX;
1438 /* # of registers saved by the interrupt handler. */
1439 #define INTERRUPT_FIXED_NUM 4
1441 /* # of bytes for registers saved by the interrupt handler. */
1442 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1444 /* # of registers saved in register parameter area. */
1445 #define INTERRUPT_REGPARM_NUM 4
1446 /* # of words saved for other registers. */
1447 #define INTERRUPT_ALL_SAVE_NUM \
1448 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1450 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1453 compute_register_save_size (long * p_reg_saved)
1455 int size = 0;
1456 int i;
1457 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1458 int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1459 long reg_saved = 0;
1461 /* Count the return pointer if we need to save it. */
1462 if (current_function_profile && !call_p)
1463 regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1465 /* Count space for the register saves. */
1466 if (interrupt_handler)
1468 for (i = 0; i <= 31; i++)
1469 switch (i)
1471 default:
1472 if (regs_ever_live[i] || call_p)
1474 size += 4;
1475 reg_saved |= 1L << i;
1477 break;
1479 /* We don't save/restore r0 or the stack pointer */
1480 case 0:
1481 case STACK_POINTER_REGNUM:
1482 break;
1484 /* For registers with fixed use, we save them, set them to the
1485 appropriate value, and then restore them.
1486 These registers are handled specially, so don't list them
1487 on the list of registers to save in the prologue. */
1488 case 1: /* temp used to hold ep */
1489 case 4: /* gp */
1490 case 10: /* temp used to call interrupt save/restore */
1491 case EP_REGNUM: /* ep */
1492 size += 4;
1493 break;
1496 else
1498 /* Find the first register that needs to be saved. */
1499 for (i = 0; i <= 31; i++)
1500 if (regs_ever_live[i] && ((! call_used_regs[i])
1501 || i == LINK_POINTER_REGNUM))
1502 break;
1504 /* If it is possible that an out-of-line helper function might be
1505 used to generate the prologue for the current function, then we
1506 need to cover the possibility that such a helper function will
1507 be used, despite the fact that there might be gaps in the list of
1508 registers that need to be saved. To detect this we note that the
1509 helper functions always push at least register r29 (provided
1510 that the function is not an interrupt handler). */
1512 if (TARGET_PROLOG_FUNCTION
1513 && (i == 2 || ((i >= 20) && (i < 30))))
1515 if (i == 2)
1517 size += 4;
1518 reg_saved |= 1L << i;
1520 i = 20;
1523 /* Helper functions save all registers between the starting
1524 register and the last register, regardless of whether they
1525 are actually used by the function or not. */
1526 for (; i <= 29; i++)
1528 size += 4;
1529 reg_saved |= 1L << i;
1532 if (regs_ever_live [LINK_POINTER_REGNUM])
1534 size += 4;
1535 reg_saved |= 1L << LINK_POINTER_REGNUM;
1538 else
1540 for (; i <= 31; i++)
1541 if (regs_ever_live[i] && ((! call_used_regs[i])
1542 || i == LINK_POINTER_REGNUM))
1544 size += 4;
1545 reg_saved |= 1L << i;
1550 if (p_reg_saved)
1551 *p_reg_saved = reg_saved;
1553 return size;
1557 compute_frame_size (int size, long * p_reg_saved)
1559 return (size
1560 + compute_register_save_size (p_reg_saved)
1561 + current_function_outgoing_args_size);
1565 void
1566 expand_prologue (void)
1568 unsigned int i;
1569 int offset;
1570 unsigned int size = get_frame_size ();
1571 unsigned int actual_fsize;
1572 unsigned int init_stack_alloc = 0;
1573 rtx save_regs[32];
1574 rtx save_all;
1575 unsigned int num_save;
1576 unsigned int default_stack;
1577 int code;
1578 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1579 long reg_saved = 0;
1581 actual_fsize = compute_frame_size (size, &reg_saved);
1583 /* Save/setup global registers for interrupt functions right now. */
1584 if (interrupt_handler)
1586 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1587 emit_insn (gen_callt_save_interrupt ());
1588 else
1589 emit_insn (gen_save_interrupt ());
1591 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1593 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1594 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1597 /* Save arg registers to the stack if necessary. */
1598 else if (current_function_args_info.anonymous_args)
1600 if (TARGET_PROLOG_FUNCTION && TARGET_V850E && !TARGET_DISABLE_CALLT)
1601 emit_insn (gen_save_r6_r9_v850e ());
1602 else if (TARGET_PROLOG_FUNCTION && ! TARGET_LONG_CALLS)
1603 emit_insn (gen_save_r6_r9 ());
1604 else
1606 offset = 0;
1607 for (i = 6; i < 10; i++)
1609 emit_move_insn (gen_rtx_MEM (SImode,
1610 plus_constant (stack_pointer_rtx,
1611 offset)),
1612 gen_rtx_REG (SImode, i));
1613 offset += 4;
1618 /* Identify all of the saved registers. */
1619 num_save = 0;
1620 default_stack = 0;
1621 for (i = 1; i < 31; i++)
1623 if (((1L << i) & reg_saved) != 0)
1624 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1627 /* If the return pointer is saved, the helper functions also allocate
1628 16 bytes of stack for arguments to be saved in. */
1629 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1631 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1632 default_stack = 16;
1635 /* See if we have an insn that allocates stack space and saves the particular
1636 registers we want to. */
1637 save_all = NULL_RTX;
1638 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1640 int alloc_stack = (4 * num_save) + default_stack;
1641 int unalloc_stack = actual_fsize - alloc_stack;
1642 int save_func_len = 4;
1643 int save_normal_len;
1645 if (unalloc_stack)
1646 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1648 /* see if we would have used ep to save the stack */
1649 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1650 save_normal_len = (3 * 2) + (2 * num_save);
1651 else
1652 save_normal_len = 4 * num_save;
1654 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1656 /* Don't bother checking if we don't actually save any space.
1657 This happens for instance if one register is saved and additional
1658 stack space is allocated. */
1659 if (save_func_len < save_normal_len)
1661 save_all = gen_rtx_PARALLEL
1662 (VOIDmode,
1663 rtvec_alloc (num_save + 1
1664 + (TARGET_V850 ? (TARGET_LONG_CALLS ? 2 : 1) : 0)));
1666 XVECEXP (save_all, 0, 0)
1667 = gen_rtx_SET (VOIDmode,
1668 stack_pointer_rtx,
1669 plus_constant (stack_pointer_rtx, -alloc_stack));
1671 offset = - default_stack;
1672 for (i = 0; i < num_save; i++)
1674 XVECEXP (save_all, 0, i+1)
1675 = gen_rtx_SET (VOIDmode,
1676 gen_rtx_MEM (Pmode,
1677 plus_constant (stack_pointer_rtx,
1678 offset)),
1679 save_regs[i]);
1680 offset -= 4;
1683 if (TARGET_V850)
1685 XVECEXP (save_all, 0, num_save + 1)
1686 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1688 if (TARGET_LONG_CALLS)
1689 XVECEXP (save_all, 0, num_save + 2)
1690 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
1693 code = recog (save_all, NULL_RTX, NULL);
1694 if (code >= 0)
1696 rtx insn = emit_insn (save_all);
1697 INSN_CODE (insn) = code;
1698 actual_fsize -= alloc_stack;
1700 if (TARGET_DEBUG)
1701 fprintf (stderr, "\
1702 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1703 save_normal_len - save_func_len,
1704 save_normal_len, save_func_len,
1705 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1707 else
1708 save_all = NULL_RTX;
1712 /* If no prolog save function is available, store the registers the old
1713 fashioned way (one by one). */
1714 if (!save_all)
1716 /* Special case interrupt functions that save all registers for a call. */
1717 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1719 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1720 emit_insn (gen_callt_save_all_interrupt ());
1721 else
1722 emit_insn (gen_save_all_interrupt ());
1724 else
1726 /* If the stack is too big, allocate it in chunks so we can do the
1727 register saves. We use the register save size so we use the ep
1728 register. */
1729 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1730 init_stack_alloc = compute_register_save_size (NULL);
1731 else
1732 init_stack_alloc = actual_fsize;
1734 /* Save registers at the beginning of the stack frame. */
1735 offset = init_stack_alloc - 4;
1737 if (init_stack_alloc)
1738 emit_insn (gen_addsi3 (stack_pointer_rtx,
1739 stack_pointer_rtx,
1740 GEN_INT (-init_stack_alloc)));
1742 /* Save the return pointer first. */
1743 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1745 emit_move_insn (gen_rtx_MEM (SImode,
1746 plus_constant (stack_pointer_rtx,
1747 offset)),
1748 save_regs[--num_save]);
1749 offset -= 4;
1752 for (i = 0; i < num_save; i++)
1754 emit_move_insn (gen_rtx_MEM (SImode,
1755 plus_constant (stack_pointer_rtx,
1756 offset)),
1757 save_regs[i]);
1758 offset -= 4;
1763 /* Allocate the rest of the stack that was not allocated above (either it is
1764 > 32K or we just called a function to save the registers and needed more
1765 stack. */
1766 if (actual_fsize > init_stack_alloc)
1768 int diff = actual_fsize - init_stack_alloc;
1769 if (CONST_OK_FOR_K (diff))
1770 emit_insn (gen_addsi3 (stack_pointer_rtx,
1771 stack_pointer_rtx,
1772 GEN_INT (-diff)));
1773 else
1775 rtx reg = gen_rtx_REG (Pmode, 12);
1776 emit_move_insn (reg, GEN_INT (-diff));
1777 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1781 /* If we need a frame pointer, set it up now. */
1782 if (frame_pointer_needed)
1783 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1787 void
1788 expand_epilogue (void)
1790 unsigned int i;
1791 int offset;
1792 unsigned int size = get_frame_size ();
1793 long reg_saved = 0;
1794 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1795 unsigned int init_stack_free = 0;
1796 rtx restore_regs[32];
1797 rtx restore_all;
1798 unsigned int num_restore;
1799 unsigned int default_stack;
1800 int code;
1801 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1803 /* Eliminate the initial stack stored by interrupt functions. */
1804 if (interrupt_handler)
1806 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1807 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1808 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1811 /* Cut off any dynamic stack created. */
1812 if (frame_pointer_needed)
1813 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1815 /* Identify all of the saved registers. */
1816 num_restore = 0;
1817 default_stack = 0;
1818 for (i = 1; i < 31; i++)
1820 if (((1L << i) & reg_saved) != 0)
1821 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1824 /* If the return pointer is saved, the helper functions also allocate
1825 16 bytes of stack for arguments to be saved in. */
1826 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1828 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1829 default_stack = 16;
1832 /* See if we have an insn that restores the particular registers we
1833 want to. */
1834 restore_all = NULL_RTX;
1836 if (TARGET_PROLOG_FUNCTION
1837 && num_restore > 0
1838 && actual_fsize >= default_stack
1839 && !interrupt_handler)
1841 int alloc_stack = (4 * num_restore) + default_stack;
1842 int unalloc_stack = actual_fsize - alloc_stack;
1843 int restore_func_len = 4;
1844 int restore_normal_len;
1846 if (unalloc_stack)
1847 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1849 /* See if we would have used ep to restore the registers. */
1850 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1851 restore_normal_len = (3 * 2) + (2 * num_restore);
1852 else
1853 restore_normal_len = 4 * num_restore;
1855 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1857 /* Don't bother checking if we don't actually save any space. */
1858 if (restore_func_len < restore_normal_len)
1860 restore_all = gen_rtx_PARALLEL (VOIDmode,
1861 rtvec_alloc (num_restore + 2));
1862 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1863 XVECEXP (restore_all, 0, 1)
1864 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1865 gen_rtx_PLUS (Pmode,
1866 stack_pointer_rtx,
1867 GEN_INT (alloc_stack)));
1869 offset = alloc_stack - 4;
1870 for (i = 0; i < num_restore; i++)
1872 XVECEXP (restore_all, 0, i+2)
1873 = gen_rtx_SET (VOIDmode,
1874 restore_regs[i],
1875 gen_rtx_MEM (Pmode,
1876 plus_constant (stack_pointer_rtx,
1877 offset)));
1878 offset -= 4;
1881 code = recog (restore_all, NULL_RTX, NULL);
1883 if (code >= 0)
1885 rtx insn;
1887 actual_fsize -= alloc_stack;
1888 if (actual_fsize)
1890 if (CONST_OK_FOR_K (actual_fsize))
1891 emit_insn (gen_addsi3 (stack_pointer_rtx,
1892 stack_pointer_rtx,
1893 GEN_INT (actual_fsize)));
1894 else
1896 rtx reg = gen_rtx_REG (Pmode, 12);
1897 emit_move_insn (reg, GEN_INT (actual_fsize));
1898 emit_insn (gen_addsi3 (stack_pointer_rtx,
1899 stack_pointer_rtx,
1900 reg));
1904 insn = emit_jump_insn (restore_all);
1905 INSN_CODE (insn) = code;
1907 if (TARGET_DEBUG)
1908 fprintf (stderr, "\
1909 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1910 restore_normal_len - restore_func_len,
1911 restore_normal_len, restore_func_len,
1912 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1914 else
1915 restore_all = NULL_RTX;
1919 /* If no epilog save function is available, restore the registers the
1920 old fashioned way (one by one). */
1921 if (!restore_all)
1923 /* If the stack is large, we need to cut it down in 2 pieces. */
1924 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1925 init_stack_free = 4 * num_restore;
1926 else
1927 init_stack_free = actual_fsize;
1929 /* Deallocate the rest of the stack if it is > 32K. */
1930 if (actual_fsize > init_stack_free)
1932 int diff;
1934 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1936 if (CONST_OK_FOR_K (diff))
1937 emit_insn (gen_addsi3 (stack_pointer_rtx,
1938 stack_pointer_rtx,
1939 GEN_INT (diff)));
1940 else
1942 rtx reg = gen_rtx_REG (Pmode, 12);
1943 emit_move_insn (reg, GEN_INT (diff));
1944 emit_insn (gen_addsi3 (stack_pointer_rtx,
1945 stack_pointer_rtx,
1946 reg));
1950 /* Special case interrupt functions that save all registers
1951 for a call. */
1952 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1954 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1955 emit_insn (gen_callt_restore_all_interrupt ());
1956 else
1957 emit_insn (gen_restore_all_interrupt ());
1959 else
1961 /* Restore registers from the beginning of the stack frame. */
1962 offset = init_stack_free - 4;
1964 /* Restore the return pointer first. */
1965 if (num_restore > 0
1966 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
1968 emit_move_insn (restore_regs[--num_restore],
1969 gen_rtx_MEM (SImode,
1970 plus_constant (stack_pointer_rtx,
1971 offset)));
1972 offset -= 4;
1975 for (i = 0; i < num_restore; i++)
1977 emit_move_insn (restore_regs[i],
1978 gen_rtx_MEM (SImode,
1979 plus_constant (stack_pointer_rtx,
1980 offset)));
1982 emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
1983 offset -= 4;
1986 /* Cut back the remainder of the stack. */
1987 if (init_stack_free)
1988 emit_insn (gen_addsi3 (stack_pointer_rtx,
1989 stack_pointer_rtx,
1990 GEN_INT (init_stack_free)));
1993 /* And return or use reti for interrupt handlers. */
1994 if (interrupt_handler)
1996 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1997 emit_insn (gen_callt_return_interrupt ());
1998 else
1999 emit_jump_insn (gen_return_interrupt ());
2001 else if (actual_fsize)
2002 emit_jump_insn (gen_return_internal ());
2003 else
2004 emit_jump_insn (gen_return ());
2007 v850_interrupt_cache_p = FALSE;
2008 v850_interrupt_p = FALSE;
2012 /* Update the condition code from the insn. */
2014 void
2015 notice_update_cc (rtx body, rtx insn)
2017 switch (get_attr_cc (insn))
2019 case CC_NONE:
2020 /* Insn does not affect CC at all. */
2021 break;
2023 case CC_NONE_0HIT:
2024 /* Insn does not change CC, but the 0'th operand has been changed. */
2025 if (cc_status.value1 != 0
2026 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2027 cc_status.value1 = 0;
2028 break;
2030 case CC_SET_ZN:
2031 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2032 V,C is in an unusable state. */
2033 CC_STATUS_INIT;
2034 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2035 cc_status.value1 = recog_data.operand[0];
2036 break;
2038 case CC_SET_ZNV:
2039 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2040 C is in an unusable state. */
2041 CC_STATUS_INIT;
2042 cc_status.flags |= CC_NO_CARRY;
2043 cc_status.value1 = recog_data.operand[0];
2044 break;
2046 case CC_COMPARE:
2047 /* The insn is a compare instruction. */
2048 CC_STATUS_INIT;
2049 cc_status.value1 = SET_SRC (body);
2050 break;
2052 case CC_CLOBBER:
2053 /* Insn doesn't leave CC in a usable state. */
2054 CC_STATUS_INIT;
2055 break;
2059 /* Retrieve the data area that has been chosen for the given decl. */
2061 v850_data_area
2062 v850_get_data_area (tree decl)
2064 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2065 return DATA_AREA_SDA;
2067 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2068 return DATA_AREA_TDA;
2070 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2071 return DATA_AREA_ZDA;
2073 return DATA_AREA_NORMAL;
2076 /* Store the indicated data area in the decl's attributes. */
2078 static void
2079 v850_set_data_area (tree decl, v850_data_area data_area)
2081 tree name;
2083 switch (data_area)
2085 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2086 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2087 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2088 default:
2089 return;
2092 DECL_ATTRIBUTES (decl) = tree_cons
2093 (name, NULL, DECL_ATTRIBUTES (decl));
2096 const struct attribute_spec v850_attribute_table[] =
2098 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2099 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2100 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2101 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2102 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2103 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2104 { NULL, 0, 0, false, false, false, NULL }
2107 /* Handle an "interrupt" attribute; arguments as in
2108 struct attribute_spec.handler. */
2109 static tree
2110 v850_handle_interrupt_attribute (tree * node,
2111 tree name,
2112 tree args ATTRIBUTE_UNUSED,
2113 int flags ATTRIBUTE_UNUSED,
2114 bool * no_add_attrs)
2116 if (TREE_CODE (*node) != FUNCTION_DECL)
2118 warning (OPT_Wattributes, "%qs attribute only applies to functions",
2119 IDENTIFIER_POINTER (name));
2120 *no_add_attrs = true;
2123 return NULL_TREE;
2126 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2127 struct attribute_spec.handler. */
2128 static tree
2129 v850_handle_data_area_attribute (tree* node,
2130 tree name,
2131 tree args ATTRIBUTE_UNUSED,
2132 int flags ATTRIBUTE_UNUSED,
2133 bool * no_add_attrs)
2135 v850_data_area data_area;
2136 v850_data_area area;
2137 tree decl = *node;
2139 /* Implement data area attribute. */
2140 if (is_attribute_p ("sda", name))
2141 data_area = DATA_AREA_SDA;
2142 else if (is_attribute_p ("tda", name))
2143 data_area = DATA_AREA_TDA;
2144 else if (is_attribute_p ("zda", name))
2145 data_area = DATA_AREA_ZDA;
2146 else
2147 gcc_unreachable ();
2149 switch (TREE_CODE (decl))
2151 case VAR_DECL:
2152 if (current_function_decl != NULL_TREE)
2154 error ("%Jdata area attributes cannot be specified for "
2155 "local variables", decl);
2156 *no_add_attrs = true;
2159 /* Drop through. */
2161 case FUNCTION_DECL:
2162 area = v850_get_data_area (decl);
2163 if (area != DATA_AREA_NORMAL && data_area != area)
2165 error ("data area of %q+D conflicts with previous declaration",
2166 decl);
2167 *no_add_attrs = true;
2169 break;
2171 default:
2172 break;
2175 return NULL_TREE;
2179 /* Return nonzero if FUNC is an interrupt function as specified
2180 by the "interrupt" attribute. */
2183 v850_interrupt_function_p (tree func)
2185 tree a;
2186 int ret = 0;
2188 if (v850_interrupt_cache_p)
2189 return v850_interrupt_p;
2191 if (TREE_CODE (func) != FUNCTION_DECL)
2192 return 0;
2194 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2195 if (a != NULL_TREE)
2196 ret = 1;
2198 else
2200 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2201 ret = a != NULL_TREE;
2204 /* Its not safe to trust global variables until after function inlining has
2205 been done. */
2206 if (reload_completed | reload_in_progress)
2207 v850_interrupt_p = ret;
2209 return ret;
2213 static void
2214 v850_encode_data_area (tree decl, rtx symbol)
2216 int flags;
2218 /* Map explicit sections into the appropriate attribute */
2219 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2221 if (DECL_SECTION_NAME (decl))
2223 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2225 if (streq (name, ".zdata") || streq (name, ".zbss"))
2226 v850_set_data_area (decl, DATA_AREA_ZDA);
2228 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2229 v850_set_data_area (decl, DATA_AREA_SDA);
2231 else if (streq (name, ".tdata"))
2232 v850_set_data_area (decl, DATA_AREA_TDA);
2235 /* If no attribute, support -m{zda,sda,tda}=n */
2236 else
2238 int size = int_size_in_bytes (TREE_TYPE (decl));
2239 if (size <= 0)
2242 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2243 v850_set_data_area (decl, DATA_AREA_TDA);
2245 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2246 v850_set_data_area (decl, DATA_AREA_SDA);
2248 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2249 v850_set_data_area (decl, DATA_AREA_ZDA);
2252 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2253 return;
2256 flags = SYMBOL_REF_FLAGS (symbol);
2257 switch (v850_get_data_area (decl))
2259 case DATA_AREA_ZDA: flags |= SYMBOL_FLAG_ZDA; break;
2260 case DATA_AREA_TDA: flags |= SYMBOL_FLAG_TDA; break;
2261 case DATA_AREA_SDA: flags |= SYMBOL_FLAG_SDA; break;
2262 default: gcc_unreachable ();
2264 SYMBOL_REF_FLAGS (symbol) = flags;
2267 static void
2268 v850_encode_section_info (tree decl, rtx rtl, int first)
2270 default_encode_section_info (decl, rtl, first);
2272 if (TREE_CODE (decl) == VAR_DECL
2273 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2274 v850_encode_data_area (decl, XEXP (rtl, 0));
2277 /* Construct a JR instruction to a routine that will perform the equivalent of
2278 the RTL passed in as an argument. This RTL is a function epilogue that
2279 pops registers off the stack and possibly releases some extra stack space
2280 as well. The code has already verified that the RTL matches these
2281 requirements. */
2282 char *
2283 construct_restore_jr (rtx op)
2285 int count = XVECLEN (op, 0);
2286 int stack_bytes;
2287 unsigned long int mask;
2288 unsigned long int first;
2289 unsigned long int last;
2290 int i;
2291 static char buff [100]; /* XXX */
2293 if (count <= 2)
2295 error ("bogus JR construction: %d", count);
2296 return NULL;
2299 /* Work out how many bytes to pop off the stack before retrieving
2300 registers. */
2301 gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2302 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2303 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2305 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2307 /* Each pop will remove 4 bytes from the stack.... */
2308 stack_bytes -= (count - 2) * 4;
2310 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2311 if (stack_bytes != 0 && stack_bytes != 16)
2313 error ("bad amount of stack space removal: %d", stack_bytes);
2314 return NULL;
2317 /* Now compute the bit mask of registers to push. */
2318 mask = 0;
2319 for (i = 2; i < count; i++)
2321 rtx vector_element = XVECEXP (op, 0, i);
2323 gcc_assert (GET_CODE (vector_element) == SET);
2324 gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2325 gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2326 SImode));
2328 mask |= 1 << REGNO (SET_DEST (vector_element));
2331 /* Scan for the first register to pop. */
2332 for (first = 0; first < 32; first++)
2334 if (mask & (1 << first))
2335 break;
2338 gcc_assert (first < 32);
2340 /* Discover the last register to pop. */
2341 if (mask & (1 << LINK_POINTER_REGNUM))
2343 gcc_assert (stack_bytes == 16);
2345 last = LINK_POINTER_REGNUM;
2347 else
2349 gcc_assert (!stack_bytes);
2350 gcc_assert (mask & (1 << 29));
2352 last = 29;
2355 /* Note, it is possible to have gaps in the register mask.
2356 We ignore this here, and generate a JR anyway. We will
2357 be popping more registers than is strictly necessary, but
2358 it does save code space. */
2360 if (TARGET_LONG_CALLS)
2362 char name[40];
2364 if (first == last)
2365 sprintf (name, "__return_%s", reg_names [first]);
2366 else
2367 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2369 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2370 name, name);
2372 else
2374 if (first == last)
2375 sprintf (buff, "jr __return_%s", reg_names [first]);
2376 else
2377 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2380 return buff;
2384 /* Construct a JARL instruction to a routine that will perform the equivalent
2385 of the RTL passed as a parameter. This RTL is a function prologue that
2386 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2387 some stack space as well. The code has already verified that the RTL
2388 matches these requirements. */
2389 char *
2390 construct_save_jarl (rtx op)
2392 int count = XVECLEN (op, 0);
2393 int stack_bytes;
2394 unsigned long int mask;
2395 unsigned long int first;
2396 unsigned long int last;
2397 int i;
2398 static char buff [100]; /* XXX */
2400 if (count <= 2)
2402 error ("bogus JARL construction: %d\n", count);
2403 return NULL;
2406 /* Paranoia. */
2407 gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2408 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2409 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) == REG);
2410 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2412 /* Work out how many bytes to push onto the stack after storing the
2413 registers. */
2414 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2416 /* Each push will put 4 bytes from the stack.... */
2417 stack_bytes += (count - (TARGET_LONG_CALLS ? 3 : 2)) * 4;
2419 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2420 if (stack_bytes != 0 && stack_bytes != -16)
2422 error ("bad amount of stack space removal: %d", stack_bytes);
2423 return NULL;
2426 /* Now compute the bit mask of registers to push. */
2427 mask = 0;
2428 for (i = 1; i < count - (TARGET_LONG_CALLS ? 2 : 1); i++)
2430 rtx vector_element = XVECEXP (op, 0, i);
2432 gcc_assert (GET_CODE (vector_element) == SET);
2433 gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2434 gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2435 SImode));
2437 mask |= 1 << REGNO (SET_SRC (vector_element));
2440 /* Scan for the first register to push. */
2441 for (first = 0; first < 32; first++)
2443 if (mask & (1 << first))
2444 break;
2447 gcc_assert (first < 32);
2449 /* Discover the last register to push. */
2450 if (mask & (1 << LINK_POINTER_REGNUM))
2452 gcc_assert (stack_bytes == -16);
2454 last = LINK_POINTER_REGNUM;
2456 else
2458 gcc_assert (!stack_bytes);
2459 gcc_assert (mask & (1 << 29));
2461 last = 29;
2464 /* Note, it is possible to have gaps in the register mask.
2465 We ignore this here, and generate a JARL anyway. We will
2466 be pushing more registers than is strictly necessary, but
2467 it does save code space. */
2469 if (TARGET_LONG_CALLS)
2471 char name[40];
2473 if (first == last)
2474 sprintf (name, "__save_%s", reg_names [first]);
2475 else
2476 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2478 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2479 name, name);
2481 else
2483 if (first == last)
2484 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2485 else
2486 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2487 reg_names [last]);
2490 return buff;
2493 extern tree last_assemble_variable_decl;
2494 extern int size_directive_output;
2496 /* A version of asm_output_aligned_bss() that copes with the special
2497 data areas of the v850. */
2498 void
2499 v850_output_aligned_bss (FILE * file,
2500 tree decl,
2501 const char * name,
2502 unsigned HOST_WIDE_INT size,
2503 int align)
2505 switch (v850_get_data_area (decl))
2507 case DATA_AREA_ZDA:
2508 switch_to_section (zbss_section);
2509 break;
2511 case DATA_AREA_SDA:
2512 switch_to_section (sbss_section);
2513 break;
2515 case DATA_AREA_TDA:
2516 switch_to_section (tdata_section);
2518 default:
2519 switch_to_section (bss_section);
2520 break;
2523 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2524 #ifdef ASM_DECLARE_OBJECT_NAME
2525 last_assemble_variable_decl = decl;
2526 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2527 #else
2528 /* Standard thing is just output label for the object. */
2529 ASM_OUTPUT_LABEL (file, name);
2530 #endif /* ASM_DECLARE_OBJECT_NAME */
2531 ASM_OUTPUT_SKIP (file, size ? size : 1);
2534 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2535 void
2536 v850_output_common (FILE * file,
2537 tree decl,
2538 const char * name,
2539 int size,
2540 int align)
2542 if (decl == NULL_TREE)
2544 fprintf (file, "%s", COMMON_ASM_OP);
2546 else
2548 switch (v850_get_data_area (decl))
2550 case DATA_AREA_ZDA:
2551 fprintf (file, "%s", ZCOMMON_ASM_OP);
2552 break;
2554 case DATA_AREA_SDA:
2555 fprintf (file, "%s", SCOMMON_ASM_OP);
2556 break;
2558 case DATA_AREA_TDA:
2559 fprintf (file, "%s", TCOMMON_ASM_OP);
2560 break;
2562 default:
2563 fprintf (file, "%s", COMMON_ASM_OP);
2564 break;
2568 assemble_name (file, name);
2569 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2572 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2573 void
2574 v850_output_local (FILE * file,
2575 tree decl,
2576 const char * name,
2577 int size,
2578 int align)
2580 fprintf (file, "%s", LOCAL_ASM_OP);
2581 assemble_name (file, name);
2582 fprintf (file, "\n");
2584 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2587 /* Add data area to the given declaration if a ghs data area pragma is
2588 currently in effect (#pragma ghs startXXX/endXXX). */
2589 static void
2590 v850_insert_attributes (tree decl, tree * attr_ptr ATTRIBUTE_UNUSED )
2592 if (data_area_stack
2593 && data_area_stack->data_area
2594 && current_function_decl == NULL_TREE
2595 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2596 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2597 v850_set_data_area (decl, data_area_stack->data_area);
2599 /* Initialize the default names of the v850 specific sections,
2600 if this has not been done before. */
2602 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2604 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2605 = build_string (sizeof (".sdata")-1, ".sdata");
2607 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2608 = build_string (sizeof (".rosdata")-1, ".rosdata");
2610 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2611 = build_string (sizeof (".tdata")-1, ".tdata");
2613 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2614 = build_string (sizeof (".zdata")-1, ".zdata");
2616 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2617 = build_string (sizeof (".rozdata")-1, ".rozdata");
2620 if (current_function_decl == NULL_TREE
2621 && (TREE_CODE (decl) == VAR_DECL
2622 || TREE_CODE (decl) == CONST_DECL
2623 || TREE_CODE (decl) == FUNCTION_DECL)
2624 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2625 && !DECL_SECTION_NAME (decl))
2627 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2628 tree chosen_section;
2630 if (TREE_CODE (decl) == FUNCTION_DECL)
2631 kind = GHS_SECTION_KIND_TEXT;
2632 else
2634 /* First choose a section kind based on the data area of the decl. */
2635 switch (v850_get_data_area (decl))
2637 default:
2638 gcc_unreachable ();
2640 case DATA_AREA_SDA:
2641 kind = ((TREE_READONLY (decl))
2642 ? GHS_SECTION_KIND_ROSDATA
2643 : GHS_SECTION_KIND_SDATA);
2644 break;
2646 case DATA_AREA_TDA:
2647 kind = GHS_SECTION_KIND_TDATA;
2648 break;
2650 case DATA_AREA_ZDA:
2651 kind = ((TREE_READONLY (decl))
2652 ? GHS_SECTION_KIND_ROZDATA
2653 : GHS_SECTION_KIND_ZDATA);
2654 break;
2656 case DATA_AREA_NORMAL: /* default data area */
2657 if (TREE_READONLY (decl))
2658 kind = GHS_SECTION_KIND_RODATA;
2659 else if (DECL_INITIAL (decl))
2660 kind = GHS_SECTION_KIND_DATA;
2661 else
2662 kind = GHS_SECTION_KIND_BSS;
2666 /* Now, if the section kind has been explicitly renamed,
2667 then attach a section attribute. */
2668 chosen_section = GHS_current_section_names [(int) kind];
2670 /* Otherwise, if this kind of section needs an explicit section
2671 attribute, then also attach one. */
2672 if (chosen_section == NULL)
2673 chosen_section = GHS_default_section_names [(int) kind];
2675 if (chosen_section)
2677 /* Only set the section name if specified by a pragma, because
2678 otherwise it will force those variables to get allocated storage
2679 in this module, rather than by the linker. */
2680 DECL_SECTION_NAME (decl) = chosen_section;
2685 /* Construct a DISPOSE instruction that is the equivalent of
2686 the given RTX. We have already verified that this should
2687 be possible. */
2689 char *
2690 construct_dispose_instruction (rtx op)
2692 int count = XVECLEN (op, 0);
2693 int stack_bytes;
2694 unsigned long int mask;
2695 int i;
2696 static char buff[ 100 ]; /* XXX */
2697 int use_callt = 0;
2699 if (count <= 2)
2701 error ("bogus DISPOSE construction: %d", count);
2702 return NULL;
2705 /* Work out how many bytes to pop off the
2706 stack before retrieving registers. */
2707 gcc_assert (GET_CODE (XVECEXP (op, 0, 1)) == SET);
2708 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS);
2709 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) == CONST_INT);
2711 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2713 /* Each pop will remove 4 bytes from the stack.... */
2714 stack_bytes -= (count - 2) * 4;
2716 /* Make sure that the amount we are popping
2717 will fit into the DISPOSE instruction. */
2718 if (stack_bytes > 128)
2720 error ("too much stack space to dispose of: %d", stack_bytes);
2721 return NULL;
2724 /* Now compute the bit mask of registers to push. */
2725 mask = 0;
2727 for (i = 2; i < count; i++)
2729 rtx vector_element = XVECEXP (op, 0, i);
2731 gcc_assert (GET_CODE (vector_element) == SET);
2732 gcc_assert (GET_CODE (SET_DEST (vector_element)) == REG);
2733 gcc_assert (register_is_ok_for_epilogue (SET_DEST (vector_element),
2734 SImode));
2736 if (REGNO (SET_DEST (vector_element)) == 2)
2737 use_callt = 1;
2738 else
2739 mask |= 1 << REGNO (SET_DEST (vector_element));
2742 if (! TARGET_DISABLE_CALLT
2743 && (use_callt || stack_bytes == 0 || stack_bytes == 16))
2745 if (use_callt)
2747 sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
2748 return buff;
2750 else
2752 for (i = 20; i < 32; i++)
2753 if (mask & (1 << i))
2754 break;
2756 if (i == 31)
2757 sprintf (buff, "callt ctoff(__callt_return_r31c)");
2758 else
2759 sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
2760 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2763 else
2765 static char regs [100]; /* XXX */
2766 int done_one;
2768 /* Generate the DISPOSE instruction. Note we could just issue the
2769 bit mask as a number as the assembler can cope with this, but for
2770 the sake of our readers we turn it into a textual description. */
2771 regs[0] = 0;
2772 done_one = 0;
2774 for (i = 20; i < 32; i++)
2776 if (mask & (1 << i))
2778 int first;
2780 if (done_one)
2781 strcat (regs, ", ");
2782 else
2783 done_one = 1;
2785 first = i;
2786 strcat (regs, reg_names[ first ]);
2788 for (i++; i < 32; i++)
2789 if ((mask & (1 << i)) == 0)
2790 break;
2792 if (i > first + 1)
2794 strcat (regs, " - ");
2795 strcat (regs, reg_names[ i - 1 ] );
2800 sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
2803 return buff;
2806 /* Construct a PREPARE instruction that is the equivalent of
2807 the given RTL. We have already verified that this should
2808 be possible. */
2810 char *
2811 construct_prepare_instruction (rtx op)
2813 int count = XVECLEN (op, 0);
2814 int stack_bytes;
2815 unsigned long int mask;
2816 int i;
2817 static char buff[ 100 ]; /* XXX */
2818 int use_callt = 0;
2820 if (count <= 1)
2822 error ("bogus PREPEARE construction: %d", count);
2823 return NULL;
2826 /* Work out how many bytes to push onto
2827 the stack after storing the registers. */
2828 gcc_assert (GET_CODE (XVECEXP (op, 0, 0)) == SET);
2829 gcc_assert (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) == PLUS);
2830 gcc_assert (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) == CONST_INT);
2832 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2834 /* Each push will put 4 bytes from the stack. */
2835 stack_bytes += (count - 1) * 4;
2837 /* Make sure that the amount we are popping
2838 will fit into the DISPOSE instruction. */
2839 if (stack_bytes < -128)
2841 error ("too much stack space to prepare: %d", stack_bytes);
2842 return NULL;
2845 /* Now compute the bit mask of registers to push. */
2846 mask = 0;
2847 for (i = 1; i < count; i++)
2849 rtx vector_element = XVECEXP (op, 0, i);
2851 gcc_assert (GET_CODE (vector_element) == SET);
2852 gcc_assert (GET_CODE (SET_SRC (vector_element)) == REG);
2853 gcc_assert (register_is_ok_for_epilogue (SET_SRC (vector_element),
2854 SImode));
2856 if (REGNO (SET_SRC (vector_element)) == 2)
2857 use_callt = 1;
2858 else
2859 mask |= 1 << REGNO (SET_SRC (vector_element));
2862 if ((! TARGET_DISABLE_CALLT)
2863 && (use_callt || stack_bytes == 0 || stack_bytes == -16))
2865 if (use_callt)
2867 sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
2868 return buff;
2871 for (i = 20; i < 32; i++)
2872 if (mask & (1 << i))
2873 break;
2875 if (i == 31)
2876 sprintf (buff, "callt ctoff(__callt_save_r31c)");
2877 else
2878 sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
2879 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2881 else
2883 static char regs [100]; /* XXX */
2884 int done_one;
2887 /* Generate the PREPARE instruction. Note we could just issue the
2888 bit mask as a number as the assembler can cope with this, but for
2889 the sake of our readers we turn it into a textual description. */
2890 regs[0] = 0;
2891 done_one = 0;
2893 for (i = 20; i < 32; i++)
2895 if (mask & (1 << i))
2897 int first;
2899 if (done_one)
2900 strcat (regs, ", ");
2901 else
2902 done_one = 1;
2904 first = i;
2905 strcat (regs, reg_names[ first ]);
2907 for (i++; i < 32; i++)
2908 if ((mask & (1 << i)) == 0)
2909 break;
2911 if (i > first + 1)
2913 strcat (regs, " - ");
2914 strcat (regs, reg_names[ i - 1 ] );
2919 sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
2922 return buff;
2925 /* Return an RTX indicating where the return address to the
2926 calling function can be found. */
2929 v850_return_addr (int count)
2931 if (count != 0)
2932 return const0_rtx;
2934 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
2937 /* Implement TARGET_ASM_INIT_SECTIONS. */
2939 static void
2940 v850_asm_init_sections (void)
2942 rosdata_section
2943 = get_unnamed_section (0, output_section_asm_op,
2944 "\t.section .rosdata,\"a\"");
2946 rozdata_section
2947 = get_unnamed_section (0, output_section_asm_op,
2948 "\t.section .rozdata,\"a\"");
2950 tdata_section
2951 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
2952 "\t.section .tdata,\"aw\"");
2954 zdata_section
2955 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
2956 "\t.section .zdata,\"aw\"");
2958 zbss_section
2959 = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
2960 output_section_asm_op,
2961 "\t.section .zbss,\"aw\"");
2964 static section *
2965 v850_select_section (tree exp,
2966 int reloc ATTRIBUTE_UNUSED,
2967 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
2969 if (TREE_CODE (exp) == VAR_DECL)
2971 int is_const;
2972 if (!TREE_READONLY (exp)
2973 || TREE_SIDE_EFFECTS (exp)
2974 || !DECL_INITIAL (exp)
2975 || (DECL_INITIAL (exp) != error_mark_node
2976 && !TREE_CONSTANT (DECL_INITIAL (exp))))
2977 is_const = FALSE;
2978 else
2979 is_const = TRUE;
2981 switch (v850_get_data_area (exp))
2983 case DATA_AREA_ZDA:
2984 return is_const ? rozdata_section : zdata_section;
2986 case DATA_AREA_TDA:
2987 return tdata_section;
2989 case DATA_AREA_SDA:
2990 return is_const ? rosdata_section : sdata_section;
2992 default:
2993 return is_const ? readonly_data_section : data_section;
2996 return readonly_data_section;
2999 /* Worker function for TARGET_RETURN_IN_MEMORY. */
3001 static bool
3002 v850_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3004 /* Return values > 8 bytes in length in memory. */
3005 return int_size_in_bytes (type) > 8 || TYPE_MODE (type) == BLKmode;
3008 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
3010 static void
3011 v850_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
3012 enum machine_mode mode ATTRIBUTE_UNUSED,
3013 tree type ATTRIBUTE_UNUSED,
3014 int *pretend_arg_size ATTRIBUTE_UNUSED,
3015 int second_time ATTRIBUTE_UNUSED)
3017 ca->anonymous_args = (!TARGET_GHS ? 1 : 0);
3020 #include "gt-v850.h"