* optabs.c (expand_binop): Make sure the first subword's result
[official-gcc.git] / gcc / config / v850 / v850.c
blob9026fa850a234f65431f09b4809328d935d19723
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 void const_double_split (rtx, HOST_WIDE_INT *, HOST_WIDE_INT *);
53 static int const_costs_int (HOST_WIDE_INT, int);
54 static int const_costs (rtx, enum rtx_code);
55 static bool v850_rtx_costs (rtx, int, int, int *);
56 static void substitute_ep_register (rtx, rtx, int, int, rtx *, rtx *);
57 static void v850_reorg (void);
58 static int ep_memory_offset (enum machine_mode, int);
59 static void v850_set_data_area (tree, v850_data_area);
60 const struct attribute_spec v850_attribute_table[];
61 static tree v850_handle_interrupt_attribute (tree *, tree, tree, int, bool *);
62 static tree v850_handle_data_area_attribute (tree *, tree, tree, int, bool *);
63 static void v850_insert_attributes (tree, tree *);
64 static void v850_select_section (tree, int, unsigned HOST_WIDE_INT);
65 static void v850_encode_data_area (tree, rtx);
66 static void v850_encode_section_info (tree, rtx, int);
67 static bool v850_return_in_memory (tree, tree);
68 static void v850_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
69 tree, int *, int);
70 static bool v850_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
71 tree, bool);
72 static int v850_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
73 tree, bool);
75 /* Information about the various small memory areas. */
76 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
78 /* name value max physical max */
79 { "tda", (char *)0, 0, 256 },
80 { "sda", (char *)0, 0, 65536 },
81 { "zda", (char *)0, 0, 32768 },
84 /* Names of the various data areas used on the v850. */
85 tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
86 tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
88 /* Track the current data area set by the data area pragma (which
89 can be nested). Tested by check_default_data_area. */
90 data_area_stack_element * data_area_stack = NULL;
92 /* True if we don't need to check any more if the current
93 function is an interrupt handler. */
94 static int v850_interrupt_cache_p = FALSE;
96 /* Whether current function is an interrupt handler. */
97 static int v850_interrupt_p = FALSE;
99 /* Initialize the GCC target structure. */
100 #undef TARGET_ASM_ALIGNED_HI_OP
101 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
103 #undef TARGET_ATTRIBUTE_TABLE
104 #define TARGET_ATTRIBUTE_TABLE v850_attribute_table
106 #undef TARGET_INSERT_ATTRIBUTES
107 #define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
109 #undef TARGET_ASM_SELECT_SECTION
110 #define TARGET_ASM_SELECT_SECTION v850_select_section
112 #undef TARGET_ENCODE_SECTION_INFO
113 #define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
115 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
116 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
118 #undef TARGET_RTX_COSTS
119 #define TARGET_RTX_COSTS v850_rtx_costs
121 #undef TARGET_ADDRESS_COST
122 #define TARGET_ADDRESS_COST hook_int_rtx_0
124 #undef TARGET_MACHINE_DEPENDENT_REORG
125 #define TARGET_MACHINE_DEPENDENT_REORG v850_reorg
127 #undef TARGET_PROMOTE_PROTOTYPES
128 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
130 #undef TARGET_RETURN_IN_MEMORY
131 #define TARGET_RETURN_IN_MEMORY v850_return_in_memory
133 #undef TARGET_PASS_BY_REFERENCE
134 #define TARGET_PASS_BY_REFERENCE v850_pass_by_reference
136 #undef TARGET_CALLEE_COPIES
137 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
139 #undef TARGET_SETUP_INCOMING_VARARGS
140 #define TARGET_SETUP_INCOMING_VARARGS v850_setup_incoming_varargs
142 #undef TARGET_ARG_PARTIAL_BYTES
143 #define TARGET_ARG_PARTIAL_BYTES v850_arg_partial_bytes
145 struct gcc_target targetm = TARGET_INITIALIZER;
147 /* Sometimes certain combinations of command options do not make
148 sense on a particular target machine. You can define a macro
149 `OVERRIDE_OPTIONS' to take account of this. This macro, if
150 defined, is executed once just after all the command options have
151 been parsed.
153 Don't use this macro to turn on various extra optimizations for
154 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
156 void
157 override_options (void)
159 int i;
160 extern int atoi (const char *);
162 /* Parse -m{s,t,z}da=nnn switches */
163 for (i = 0; i < (int)SMALL_MEMORY_max; i++)
165 if (small_memory[i].value)
167 if (!ISDIGIT (*small_memory[i].value))
168 error ("%s=%s is not numeric",
169 small_memory[i].name,
170 small_memory[i].value);
171 else
173 small_memory[i].max = atoi (small_memory[i].value);
174 if (small_memory[i].max > small_memory[i].physical_max)
175 error ("%s=%s is too large",
176 small_memory[i].name,
177 small_memory[i].value);
182 /* Make sure that the US_BIT_SET mask has been correctly initialized. */
183 if ((target_flags & MASK_US_MASK_SET) == 0)
185 target_flags |= MASK_US_MASK_SET;
186 target_flags &= ~MASK_US_BIT_SET;
191 static bool
192 v850_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
193 enum machine_mode mode, tree type,
194 bool named ATTRIBUTE_UNUSED)
196 unsigned HOST_WIDE_INT size;
198 if (type)
199 size = int_size_in_bytes (type);
200 else
201 size = GET_MODE_SIZE (mode);
203 return size > 8;
206 /* Return an RTX to represent where a value with mode MODE will be returned
207 from a function. If the result is 0, the argument is pushed. */
210 function_arg (CUMULATIVE_ARGS * cum,
211 enum machine_mode mode,
212 tree type,
213 int named)
215 rtx result = 0;
216 int size, align;
218 if (TARGET_GHS && !named)
219 return NULL_RTX;
221 if (mode == BLKmode)
222 size = int_size_in_bytes (type);
223 else
224 size = GET_MODE_SIZE (mode);
226 if (size < 1)
227 return 0;
229 if (type)
230 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
231 else
232 align = size;
234 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
236 if (cum->nbytes > 4 * UNITS_PER_WORD)
237 return 0;
239 if (type == NULL_TREE
240 && cum->nbytes + size > 4 * UNITS_PER_WORD)
241 return 0;
243 switch (cum->nbytes / UNITS_PER_WORD)
245 case 0:
246 result = gen_rtx_REG (mode, 6);
247 break;
248 case 1:
249 result = gen_rtx_REG (mode, 7);
250 break;
251 case 2:
252 result = gen_rtx_REG (mode, 8);
253 break;
254 case 3:
255 result = gen_rtx_REG (mode, 9);
256 break;
257 default:
258 result = 0;
261 return result;
265 /* Return the number of bytes which must be put into registers
266 for values which are part in registers and part in memory. */
268 static int
269 v850_arg_partial_bytes (CUMULATIVE_ARGS * cum, enum machine_mode mode,
270 tree type, bool named)
272 int size, align;
274 if (TARGET_GHS && !named)
275 return 0;
277 if (mode == BLKmode)
278 size = int_size_in_bytes (type);
279 else
280 size = GET_MODE_SIZE (mode);
282 if (type)
283 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
284 else
285 align = size;
287 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
289 if (cum->nbytes > 4 * UNITS_PER_WORD)
290 return 0;
292 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
293 return 0;
295 if (type == NULL_TREE
296 && cum->nbytes + size > 4 * UNITS_PER_WORD)
297 return 0;
299 return 4 * UNITS_PER_WORD - cum->nbytes;
303 /* Return the high and low words of a CONST_DOUBLE */
305 static void
306 const_double_split (rtx x, HOST_WIDE_INT * p_high, HOST_WIDE_INT * p_low)
308 if (GET_CODE (x) == CONST_DOUBLE)
310 long t[2];
311 REAL_VALUE_TYPE rv;
313 switch (GET_MODE (x))
315 case DFmode:
316 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
317 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
318 *p_high = t[1]; /* since v850 is little endian */
319 *p_low = t[0]; /* high is second word */
320 return;
322 case SFmode:
323 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
324 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
325 *p_low = 0;
326 return;
328 case VOIDmode:
329 case DImode:
330 *p_high = CONST_DOUBLE_HIGH (x);
331 *p_low = CONST_DOUBLE_LOW (x);
332 return;
334 default:
335 break;
339 fatal_insn ("const_double_split got a bad insn:", x);
343 /* Return the cost of the rtx R with code CODE. */
345 static int
346 const_costs_int (HOST_WIDE_INT value, int zero_cost)
348 if (CONST_OK_FOR_I (value))
349 return zero_cost;
350 else if (CONST_OK_FOR_J (value))
351 return 1;
352 else if (CONST_OK_FOR_K (value))
353 return 2;
354 else
355 return 4;
358 static int
359 const_costs (rtx r, enum rtx_code c)
361 HOST_WIDE_INT high, low;
363 switch (c)
365 case CONST_INT:
366 return const_costs_int (INTVAL (r), 0);
368 case CONST_DOUBLE:
369 const_double_split (r, &high, &low);
370 if (GET_MODE (r) == SFmode)
371 return const_costs_int (high, 1);
372 else
373 return const_costs_int (high, 1) + const_costs_int (low, 1);
375 case SYMBOL_REF:
376 case LABEL_REF:
377 case CONST:
378 return 2;
380 case HIGH:
381 return 1;
383 default:
384 return 4;
388 static bool
389 v850_rtx_costs (rtx x,
390 int code,
391 int outer_code ATTRIBUTE_UNUSED,
392 int * total)
394 switch (code)
396 case CONST_INT:
397 case CONST_DOUBLE:
398 case CONST:
399 case SYMBOL_REF:
400 case LABEL_REF:
401 *total = COSTS_N_INSNS (const_costs (x, code));
402 return true;
404 case MOD:
405 case DIV:
406 case UMOD:
407 case UDIV:
408 if (TARGET_V850E && optimize_size)
409 *total = 6;
410 else
411 *total = 60;
412 return true;
414 case MULT:
415 if (TARGET_V850E
416 && ( GET_MODE (x) == SImode
417 || GET_MODE (x) == HImode
418 || GET_MODE (x) == QImode))
420 if (GET_CODE (XEXP (x, 1)) == REG)
421 *total = 4;
422 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
424 if (CONST_OK_FOR_O (INTVAL (XEXP (x, 1))))
425 *total = 6;
426 else if (CONST_OK_FOR_K (INTVAL (XEXP (x, 1))))
427 *total = 10;
430 else
431 *total = 20;
432 return true;
434 default:
435 return false;
439 /* Print operand X using operand code CODE to assembly language output file
440 FILE. */
442 void
443 print_operand (FILE * file, rtx x, int code)
445 HOST_WIDE_INT high, low;
447 switch (code)
449 case 'c':
450 /* We use 'c' operands with symbols for .vtinherit */
451 if (GET_CODE (x) == SYMBOL_REF)
453 output_addr_const(file, x);
454 break;
456 /* fall through */
457 case 'b':
458 case 'B':
459 case 'C':
460 switch ((code == 'B' || code == 'C')
461 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
463 case NE:
464 if (code == 'c' || code == 'C')
465 fprintf (file, "nz");
466 else
467 fprintf (file, "ne");
468 break;
469 case EQ:
470 if (code == 'c' || code == 'C')
471 fprintf (file, "z");
472 else
473 fprintf (file, "e");
474 break;
475 case GE:
476 fprintf (file, "ge");
477 break;
478 case GT:
479 fprintf (file, "gt");
480 break;
481 case LE:
482 fprintf (file, "le");
483 break;
484 case LT:
485 fprintf (file, "lt");
486 break;
487 case GEU:
488 fprintf (file, "nl");
489 break;
490 case GTU:
491 fprintf (file, "h");
492 break;
493 case LEU:
494 fprintf (file, "nh");
495 break;
496 case LTU:
497 fprintf (file, "l");
498 break;
499 default:
500 abort ();
502 break;
503 case 'F': /* high word of CONST_DOUBLE */
504 if (GET_CODE (x) == CONST_INT)
505 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
506 else if (GET_CODE (x) == CONST_DOUBLE)
508 const_double_split (x, &high, &low);
509 fprintf (file, "%ld", (long) high);
511 else
512 abort ();
513 break;
514 case 'G': /* low word of CONST_DOUBLE */
515 if (GET_CODE (x) == CONST_INT)
516 fprintf (file, "%ld", (long) INTVAL (x));
517 else if (GET_CODE (x) == CONST_DOUBLE)
519 const_double_split (x, &high, &low);
520 fprintf (file, "%ld", (long) low);
522 else
523 abort ();
524 break;
525 case 'L':
526 fprintf (file, "%d\n", (int)(INTVAL (x) & 0xffff));
527 break;
528 case 'M':
529 fprintf (file, "%d", exact_log2 (INTVAL (x)));
530 break;
531 case 'O':
532 if (special_symbolref_operand (x, VOIDmode))
534 if (GET_CODE (x) == SYMBOL_REF)
536 else if (GET_CODE (x) == CONST)
537 x = XEXP (XEXP (x, 0), 0);
538 else
539 abort ();
541 if (SYMBOL_REF_ZDA_P (x))
542 fprintf (file, "zdaoff");
543 else if (SYMBOL_REF_SDA_P (x))
544 fprintf (file, "sdaoff");
545 else if (SYMBOL_REF_TDA_P (x))
546 fprintf (file, "tdaoff");
547 else
548 abort ();
550 else
551 abort ();
552 break;
553 case 'P':
554 if (special_symbolref_operand (x, VOIDmode))
555 output_addr_const (file, x);
556 else
557 abort ();
558 break;
559 case 'Q':
560 if (special_symbolref_operand (x, VOIDmode))
562 if (GET_CODE (x) == SYMBOL_REF)
564 else if (GET_CODE (x) == CONST)
565 x = XEXP (XEXP (x, 0), 0);
566 else
567 abort ();
569 if (SYMBOL_REF_ZDA_P (x))
570 fprintf (file, "r0");
571 else if (SYMBOL_REF_SDA_P (x))
572 fprintf (file, "gp");
573 else if (SYMBOL_REF_TDA_P (x))
574 fprintf (file, "ep");
575 else
576 abort ();
578 else
579 abort ();
580 break;
581 case 'R': /* 2nd word of a double. */
582 switch (GET_CODE (x))
584 case REG:
585 fprintf (file, reg_names[REGNO (x) + 1]);
586 break;
587 case MEM:
588 x = XEXP (adjust_address (x, SImode, 4), 0);
589 print_operand_address (file, x);
590 if (GET_CODE (x) == CONST_INT)
591 fprintf (file, "[r0]");
592 break;
594 default:
595 break;
597 break;
598 case 'S':
600 /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
601 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
602 fputs ("s", file);
604 break;
606 case 'T':
608 /* Like an 'S' operand above, but for unsigned loads only. */
609 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
610 fputs ("s", file);
612 break;
614 case 'W': /* print the instruction suffix */
615 switch (GET_MODE (x))
617 default:
618 abort ();
620 case QImode: fputs (".b", file); break;
621 case HImode: fputs (".h", file); break;
622 case SImode: fputs (".w", file); break;
623 case SFmode: fputs (".w", file); break;
625 break;
626 case '.': /* register r0 */
627 fputs (reg_names[0], file);
628 break;
629 case 'z': /* reg or zero */
630 if (x == const0_rtx)
631 fputs (reg_names[0], file);
632 else if (GET_CODE (x) == REG)
633 fputs (reg_names[REGNO (x)], file);
634 else
635 abort ();
636 break;
637 default:
638 switch (GET_CODE (x))
640 case MEM:
641 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
642 output_address (gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, 0),
643 XEXP (x, 0)));
644 else
645 output_address (XEXP (x, 0));
646 break;
648 case REG:
649 fputs (reg_names[REGNO (x)], file);
650 break;
651 case SUBREG:
652 fputs (reg_names[subreg_regno (x)], file);
653 break;
654 case CONST_INT:
655 case SYMBOL_REF:
656 case CONST:
657 case LABEL_REF:
658 case CODE_LABEL:
659 print_operand_address (file, x);
660 break;
661 default:
662 abort ();
664 break;
670 /* Output assembly language output for the address ADDR to FILE. */
672 void
673 print_operand_address (FILE * file, rtx addr)
675 switch (GET_CODE (addr))
677 case REG:
678 fprintf (file, "0[");
679 print_operand (file, addr, 0);
680 fprintf (file, "]");
681 break;
682 case LO_SUM:
683 if (GET_CODE (XEXP (addr, 0)) == REG)
685 /* reg,foo */
686 fprintf (file, "lo(");
687 print_operand (file, XEXP (addr, 1), 0);
688 fprintf (file, ")[");
689 print_operand (file, XEXP (addr, 0), 0);
690 fprintf (file, "]");
692 break;
693 case PLUS:
694 if (GET_CODE (XEXP (addr, 0)) == REG
695 || GET_CODE (XEXP (addr, 0)) == SUBREG)
697 /* reg,foo */
698 print_operand (file, XEXP (addr, 1), 0);
699 fprintf (file, "[");
700 print_operand (file, XEXP (addr, 0), 0);
701 fprintf (file, "]");
703 else
705 print_operand (file, XEXP (addr, 0), 0);
706 fprintf (file, "+");
707 print_operand (file, XEXP (addr, 1), 0);
709 break;
710 case SYMBOL_REF:
712 const char *off_name = NULL;
713 const char *reg_name = NULL;
715 if (SYMBOL_REF_ZDA_P (addr))
717 off_name = "zdaoff";
718 reg_name = "r0";
720 else if (SYMBOL_REF_SDA_P (addr))
722 off_name = "sdaoff";
723 reg_name = "gp";
725 else if (SYMBOL_REF_TDA_P (addr))
727 off_name = "tdaoff";
728 reg_name = "ep";
731 if (off_name)
732 fprintf (file, "%s(", off_name);
733 output_addr_const (file, addr);
734 if (reg_name)
735 fprintf (file, ")[%s]", reg_name);
737 break;
738 case CONST:
739 if (special_symbolref_operand (addr, VOIDmode))
741 rtx x = XEXP (XEXP (addr, 0), 0);
742 const char *off_name;
743 const char *reg_name;
745 if (SYMBOL_REF_ZDA_P (x))
747 off_name = "zdaoff";
748 reg_name = "r0";
750 else if (SYMBOL_REF_SDA_P (x))
752 off_name = "sdaoff";
753 reg_name = "gp";
755 else if (SYMBOL_REF_TDA_P (x))
757 off_name = "tdaoff";
758 reg_name = "ep";
760 else
761 abort ();
763 fprintf (file, "%s(", off_name);
764 output_addr_const (file, addr);
765 fprintf (file, ")[%s]", reg_name);
767 else
768 output_addr_const (file, addr);
769 break;
770 default:
771 output_addr_const (file, addr);
772 break;
776 /* When assemble_integer is used to emit the offsets for a switch
777 table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
778 output_addr_const will normally barf at this, but it is OK to omit
779 the truncate and just emit the difference of the two labels. The
780 .hword directive will automatically handle the truncation for us.
782 Returns 1 if rtx was handled, 0 otherwise. */
785 v850_output_addr_const_extra (FILE * file, rtx x)
787 if (GET_CODE (x) != TRUNCATE)
788 return 0;
790 x = XEXP (x, 0);
792 /* We must also handle the case where the switch table was passed a
793 constant value and so has been collapsed. In this case the first
794 label will have been deleted. In such a case it is OK to emit
795 nothing, since the table will not be used.
796 (cf gcc.c-torture/compile/990801-1.c). */
797 if (GET_CODE (x) == MINUS
798 && GET_CODE (XEXP (x, 0)) == LABEL_REF
799 && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
800 && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
801 return 1;
803 output_addr_const (file, x);
804 return 1;
807 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
808 point value. */
810 const char *
811 output_move_single (rtx * operands)
813 rtx dst = operands[0];
814 rtx src = operands[1];
816 if (REG_P (dst))
818 if (REG_P (src))
819 return "mov %1,%0";
821 else if (GET_CODE (src) == CONST_INT)
823 HOST_WIDE_INT value = INTVAL (src);
825 if (CONST_OK_FOR_J (value)) /* Signed 5 bit immediate. */
826 return "mov %1,%0";
828 else if (CONST_OK_FOR_K (value)) /* Signed 16 bit immediate. */
829 return "movea lo(%1),%.,%0";
831 else if (CONST_OK_FOR_L (value)) /* Upper 16 bits were set. */
832 return "movhi hi(%1),%.,%0";
834 /* A random constant. */
835 else if (TARGET_V850E)
836 return "mov %1,%0";
837 else
838 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
841 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
843 HOST_WIDE_INT high, low;
845 const_double_split (src, &high, &low);
847 if (CONST_OK_FOR_J (high)) /* Signed 5 bit immediate. */
848 return "mov %F1,%0";
850 else if (CONST_OK_FOR_K (high)) /* Signed 16 bit immediate. */
851 return "movea lo(%F1),%.,%0";
853 else if (CONST_OK_FOR_L (high)) /* Upper 16 bits were set. */
854 return "movhi hi(%F1),%.,%0";
856 /* A random constant. */
857 else if (TARGET_V850E)
858 return "mov %F1,%0";
860 else
861 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
864 else if (GET_CODE (src) == MEM)
865 return "%S1ld%W1 %1,%0";
867 else if (special_symbolref_operand (src, VOIDmode))
868 return "movea %O1(%P1),%Q1,%0";
870 else if (GET_CODE (src) == LABEL_REF
871 || GET_CODE (src) == SYMBOL_REF
872 || GET_CODE (src) == CONST)
874 if (TARGET_V850E)
875 return "mov hilo(%1),%0";
876 else
877 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
880 else if (GET_CODE (src) == HIGH)
881 return "movhi hi(%1),%.,%0";
883 else if (GET_CODE (src) == LO_SUM)
885 operands[2] = XEXP (src, 0);
886 operands[3] = XEXP (src, 1);
887 return "movea lo(%3),%2,%0";
891 else if (GET_CODE (dst) == MEM)
893 if (REG_P (src))
894 return "%S0st%W0 %1,%0";
896 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
897 return "%S0st%W0 %.,%0";
899 else if (GET_CODE (src) == CONST_DOUBLE
900 && CONST0_RTX (GET_MODE (dst)) == src)
901 return "%S0st%W0 %.,%0";
904 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
905 return "";
909 /* Return appropriate code to load up an 8 byte integer or
910 floating point value */
912 const char *
913 output_move_double (rtx * operands)
915 enum machine_mode mode = GET_MODE (operands[0]);
916 rtx dst = operands[0];
917 rtx src = operands[1];
919 if (register_operand (dst, mode)
920 && register_operand (src, mode))
922 if (REGNO (src) + 1 == REGNO (dst))
923 return "mov %R1,%R0\n\tmov %1,%0";
924 else
925 return "mov %1,%0\n\tmov %R1,%R0";
928 /* Storing 0 */
929 if (GET_CODE (dst) == MEM
930 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
931 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
932 return "st.w %.,%0\n\tst.w %.,%R0";
934 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
936 HOST_WIDE_INT high_low[2];
937 int i;
938 rtx xop[10];
940 if (GET_CODE (src) == CONST_DOUBLE)
941 const_double_split (src, &high_low[1], &high_low[0]);
942 else
944 high_low[0] = INTVAL (src);
945 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
948 for (i = 0; i < 2; i++)
950 xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
951 xop[1] = GEN_INT (high_low[i]);
952 output_asm_insn (output_move_single (xop), xop);
955 return "";
958 if (GET_CODE (src) == MEM)
960 int ptrreg = -1;
961 int dreg = REGNO (dst);
962 rtx inside = XEXP (src, 0);
964 if (GET_CODE (inside) == REG)
965 ptrreg = REGNO (inside);
966 else if (GET_CODE (inside) == SUBREG)
967 ptrreg = subreg_regno (inside);
968 else if (GET_CODE (inside) == PLUS)
969 ptrreg = REGNO (XEXP (inside, 0));
970 else if (GET_CODE (inside) == LO_SUM)
971 ptrreg = REGNO (XEXP (inside, 0));
973 if (dreg == ptrreg)
974 return "ld.w %R1,%R0\n\tld.w %1,%0";
977 if (GET_CODE (src) == MEM)
978 return "ld.w %1,%0\n\tld.w %R1,%R0";
980 if (GET_CODE (dst) == MEM)
981 return "st.w %1,%0\n\tst.w %R1,%R0";
983 return "mov %1,%0\n\tmov %R1,%R0";
987 /* Return maximum offset supported for a short EP memory reference of mode
988 MODE and signedness UNSIGNEDP. */
990 static int
991 ep_memory_offset (enum machine_mode mode, int unsignedp ATTRIBUTE_UNUSED)
993 int max_offset = 0;
995 switch (mode)
997 case QImode:
998 if (TARGET_SMALL_SLD)
999 max_offset = (1 << 4);
1000 else if (TARGET_V850E
1001 && ( ( unsignedp && ! TARGET_US_BIT_SET)
1002 || (! unsignedp && TARGET_US_BIT_SET)))
1003 max_offset = (1 << 4);
1004 else
1005 max_offset = (1 << 7);
1006 break;
1008 case HImode:
1009 if (TARGET_SMALL_SLD)
1010 max_offset = (1 << 5);
1011 else if (TARGET_V850E
1012 && ( ( unsignedp && ! TARGET_US_BIT_SET)
1013 || (! unsignedp && TARGET_US_BIT_SET)))
1014 max_offset = (1 << 5);
1015 else
1016 max_offset = (1 << 8);
1017 break;
1019 case SImode:
1020 case SFmode:
1021 max_offset = (1 << 8);
1022 break;
1024 default:
1025 break;
1028 return max_offset;
1031 /* Return true if OP is a valid short EP memory reference */
1034 ep_memory_operand (rtx op, enum machine_mode mode, int unsigned_load)
1036 rtx addr, op0, op1;
1037 int max_offset;
1038 int mask;
1040 if (GET_CODE (op) != MEM)
1041 return FALSE;
1043 max_offset = ep_memory_offset (mode, unsigned_load);
1045 mask = GET_MODE_SIZE (mode) - 1;
1047 addr = XEXP (op, 0);
1048 if (GET_CODE (addr) == CONST)
1049 addr = XEXP (addr, 0);
1051 switch (GET_CODE (addr))
1053 default:
1054 break;
1056 case SYMBOL_REF:
1057 return SYMBOL_REF_TDA_P (addr);
1059 case REG:
1060 return REGNO (addr) == EP_REGNUM;
1062 case PLUS:
1063 op0 = XEXP (addr, 0);
1064 op1 = XEXP (addr, 1);
1065 if (GET_CODE (op1) == CONST_INT
1066 && INTVAL (op1) < max_offset
1067 && INTVAL (op1) >= 0
1068 && (INTVAL (op1) & mask) == 0)
1070 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1071 return TRUE;
1073 if (GET_CODE (op0) == SYMBOL_REF && SYMBOL_REF_TDA_P (op0))
1074 return TRUE;
1076 break;
1079 return FALSE;
1082 /* Substitute memory references involving a pointer, to use the ep pointer,
1083 taking care to save and preserve the ep. */
1085 static void
1086 substitute_ep_register (rtx first_insn,
1087 rtx last_insn,
1088 int uses,
1089 int regno,
1090 rtx * p_r1,
1091 rtx * p_ep)
1093 rtx reg = gen_rtx_REG (Pmode, regno);
1094 rtx insn;
1096 if (!*p_r1)
1098 regs_ever_live[1] = 1;
1099 *p_r1 = gen_rtx_REG (Pmode, 1);
1100 *p_ep = gen_rtx_REG (Pmode, 30);
1103 if (TARGET_DEBUG)
1104 fprintf (stderr, "\
1105 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1106 2 * (uses - 3), uses, reg_names[regno],
1107 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1108 INSN_UID (first_insn), INSN_UID (last_insn));
1110 if (GET_CODE (first_insn) == NOTE)
1111 first_insn = next_nonnote_insn (first_insn);
1113 last_insn = next_nonnote_insn (last_insn);
1114 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1116 if (GET_CODE (insn) == INSN)
1118 rtx pattern = single_set (insn);
1120 /* Replace the memory references. */
1121 if (pattern)
1123 rtx *p_mem;
1124 /* Memory operands are signed by default. */
1125 int unsignedp = FALSE;
1127 if (GET_CODE (SET_DEST (pattern)) == MEM
1128 && GET_CODE (SET_SRC (pattern)) == MEM)
1129 p_mem = (rtx *)0;
1131 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1132 p_mem = &SET_DEST (pattern);
1134 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1135 p_mem = &SET_SRC (pattern);
1137 else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1138 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1139 p_mem = &XEXP (SET_SRC (pattern), 0);
1141 else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1142 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1144 p_mem = &XEXP (SET_SRC (pattern), 0);
1145 unsignedp = TRUE;
1147 else
1148 p_mem = (rtx *)0;
1150 if (p_mem)
1152 rtx addr = XEXP (*p_mem, 0);
1154 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1155 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1157 else if (GET_CODE (addr) == PLUS
1158 && GET_CODE (XEXP (addr, 0)) == REG
1159 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1160 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1161 && ((INTVAL (XEXP (addr, 1)))
1162 < ep_memory_offset (GET_MODE (*p_mem),
1163 unsignedp))
1164 && ((INTVAL (XEXP (addr, 1))) >= 0))
1165 *p_mem = change_address (*p_mem, VOIDmode,
1166 gen_rtx_PLUS (Pmode,
1167 *p_ep,
1168 XEXP (addr, 1)));
1174 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1175 insn = prev_nonnote_insn (first_insn);
1176 if (insn && GET_CODE (insn) == INSN
1177 && GET_CODE (PATTERN (insn)) == SET
1178 && SET_DEST (PATTERN (insn)) == *p_ep
1179 && SET_SRC (PATTERN (insn)) == *p_r1)
1180 delete_insn (insn);
1181 else
1182 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1184 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1185 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1189 /* TARGET_MACHINE_DEPENDENT_REORG. On the 850, we use it to implement
1190 the -mep mode to copy heavily used pointers to ep to use the implicit
1191 addressing. */
1193 static void
1194 v850_reorg (void)
1196 struct
1198 int uses;
1199 rtx first_insn;
1200 rtx last_insn;
1202 regs[FIRST_PSEUDO_REGISTER];
1204 int i;
1205 int use_ep = FALSE;
1206 rtx r1 = NULL_RTX;
1207 rtx ep = NULL_RTX;
1208 rtx insn;
1209 rtx pattern;
1211 /* If not ep mode, just return now. */
1212 if (!TARGET_EP)
1213 return;
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;
1222 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1224 switch (GET_CODE (insn))
1226 /* End of basic block */
1227 default:
1228 if (!use_ep)
1230 int max_uses = -1;
1231 int max_regno = -1;
1233 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1235 if (max_uses < regs[i].uses)
1237 max_uses = regs[i].uses;
1238 max_regno = i;
1242 if (max_uses > 3)
1243 substitute_ep_register (regs[max_regno].first_insn,
1244 regs[max_regno].last_insn,
1245 max_uses, max_regno, &r1, &ep);
1248 use_ep = FALSE;
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;
1255 break;
1257 case NOTE:
1258 break;
1260 case INSN:
1261 pattern = single_set (insn);
1263 /* See if there are any memory references we can shorten */
1264 if (pattern)
1266 rtx src = SET_SRC (pattern);
1267 rtx dest = SET_DEST (pattern);
1268 rtx mem;
1269 /* Memory operands are signed by default. */
1270 int unsignedp = FALSE;
1272 /* We might have (SUBREG (MEM)) here, so just get rid of the
1273 subregs to make this code simpler. */
1274 if (GET_CODE (dest) == SUBREG
1275 && (GET_CODE (SUBREG_REG (dest)) == MEM
1276 || GET_CODE (SUBREG_REG (dest)) == REG))
1277 alter_subreg (&dest);
1278 if (GET_CODE (src) == SUBREG
1279 && (GET_CODE (SUBREG_REG (src)) == MEM
1280 || GET_CODE (SUBREG_REG (src)) == REG))
1281 alter_subreg (&src);
1283 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1284 mem = NULL_RTX;
1286 else if (GET_CODE (dest) == MEM)
1287 mem = dest;
1289 else if (GET_CODE (src) == MEM)
1290 mem = src;
1292 else if (GET_CODE (src) == SIGN_EXTEND
1293 && GET_CODE (XEXP (src, 0)) == MEM)
1294 mem = XEXP (src, 0);
1296 else if (GET_CODE (src) == ZERO_EXTEND
1297 && GET_CODE (XEXP (src, 0)) == MEM)
1299 mem = XEXP (src, 0);
1300 unsignedp = TRUE;
1302 else
1303 mem = NULL_RTX;
1305 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1306 use_ep = TRUE;
1308 else if (!use_ep && mem
1309 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1311 rtx addr = XEXP (mem, 0);
1312 int regno = -1;
1313 int short_p;
1315 if (GET_CODE (addr) == REG)
1317 short_p = TRUE;
1318 regno = REGNO (addr);
1321 else if (GET_CODE (addr) == PLUS
1322 && GET_CODE (XEXP (addr, 0)) == REG
1323 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1324 && ((INTVAL (XEXP (addr, 1)))
1325 < ep_memory_offset (GET_MODE (mem), unsignedp))
1326 && ((INTVAL (XEXP (addr, 1))) >= 0))
1328 short_p = TRUE;
1329 regno = REGNO (XEXP (addr, 0));
1332 else
1333 short_p = FALSE;
1335 if (short_p)
1337 regs[regno].uses++;
1338 regs[regno].last_insn = insn;
1339 if (!regs[regno].first_insn)
1340 regs[regno].first_insn = insn;
1344 /* Loading up a register in the basic block zaps any savings
1345 for the register */
1346 if (GET_CODE (dest) == REG)
1348 enum machine_mode mode = GET_MODE (dest);
1349 int regno;
1350 int endregno;
1352 regno = REGNO (dest);
1353 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1355 if (!use_ep)
1357 /* See if we can use the pointer before this
1358 modification. */
1359 int max_uses = -1;
1360 int max_regno = -1;
1362 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1364 if (max_uses < regs[i].uses)
1366 max_uses = regs[i].uses;
1367 max_regno = i;
1371 if (max_uses > 3
1372 && max_regno >= regno
1373 && max_regno < endregno)
1375 substitute_ep_register (regs[max_regno].first_insn,
1376 regs[max_regno].last_insn,
1377 max_uses, max_regno, &r1,
1378 &ep);
1380 /* Since we made a substitution, zap all remembered
1381 registers. */
1382 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1384 regs[i].uses = 0;
1385 regs[i].first_insn = NULL_RTX;
1386 regs[i].last_insn = NULL_RTX;
1391 for (i = regno; i < endregno; i++)
1393 regs[i].uses = 0;
1394 regs[i].first_insn = NULL_RTX;
1395 regs[i].last_insn = NULL_RTX;
1404 /* # of registers saved by the interrupt handler. */
1405 #define INTERRUPT_FIXED_NUM 4
1407 /* # of bytes for registers saved by the interrupt handler. */
1408 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1410 /* # of registers saved in register parameter area. */
1411 #define INTERRUPT_REGPARM_NUM 4
1412 /* # of words saved for other registers. */
1413 #define INTERRUPT_ALL_SAVE_NUM \
1414 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1416 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1419 compute_register_save_size (long * p_reg_saved)
1421 int size = 0;
1422 int i;
1423 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1424 int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1425 long reg_saved = 0;
1427 /* Count the return pointer if we need to save it. */
1428 if (current_function_profile && !call_p)
1429 regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1431 /* Count space for the register saves. */
1432 if (interrupt_handler)
1434 for (i = 0; i <= 31; i++)
1435 switch (i)
1437 default:
1438 if (regs_ever_live[i] || call_p)
1440 size += 4;
1441 reg_saved |= 1L << i;
1443 break;
1445 /* We don't save/restore r0 or the stack pointer */
1446 case 0:
1447 case STACK_POINTER_REGNUM:
1448 break;
1450 /* For registers with fixed use, we save them, set them to the
1451 appropriate value, and then restore them.
1452 These registers are handled specially, so don't list them
1453 on the list of registers to save in the prologue. */
1454 case 1: /* temp used to hold ep */
1455 case 4: /* gp */
1456 case 10: /* temp used to call interrupt save/restore */
1457 case EP_REGNUM: /* ep */
1458 size += 4;
1459 break;
1462 else
1464 /* Find the first register that needs to be saved. */
1465 for (i = 0; i <= 31; i++)
1466 if (regs_ever_live[i] && ((! call_used_regs[i])
1467 || i == LINK_POINTER_REGNUM))
1468 break;
1470 /* If it is possible that an out-of-line helper function might be
1471 used to generate the prologue for the current function, then we
1472 need to cover the possibility that such a helper function will
1473 be used, despite the fact that there might be gaps in the list of
1474 registers that need to be saved. To detect this we note that the
1475 helper functions always push at least register r29 (provided
1476 that the function is not an interrupt handler). */
1478 if (TARGET_PROLOG_FUNCTION
1479 && (i == 2 || ((i >= 20) && (i < 30))))
1481 if (i == 2)
1483 size += 4;
1484 reg_saved |= 1L << i;
1486 i = 20;
1489 /* Helper functions save all registers between the starting
1490 register and the last register, regardless of whether they
1491 are actually used by the function or not. */
1492 for (; i <= 29; i++)
1494 size += 4;
1495 reg_saved |= 1L << i;
1498 if (regs_ever_live [LINK_POINTER_REGNUM])
1500 size += 4;
1501 reg_saved |= 1L << LINK_POINTER_REGNUM;
1504 else
1506 for (; i <= 31; i++)
1507 if (regs_ever_live[i] && ((! call_used_regs[i])
1508 || i == LINK_POINTER_REGNUM))
1510 size += 4;
1511 reg_saved |= 1L << i;
1516 if (p_reg_saved)
1517 *p_reg_saved = reg_saved;
1519 return size;
1523 compute_frame_size (int size, long * p_reg_saved)
1525 return (size
1526 + compute_register_save_size (p_reg_saved)
1527 + current_function_outgoing_args_size);
1531 void
1532 expand_prologue (void)
1534 unsigned int i;
1535 int offset;
1536 unsigned int size = get_frame_size ();
1537 unsigned int actual_fsize;
1538 unsigned int init_stack_alloc = 0;
1539 rtx save_regs[32];
1540 rtx save_all;
1541 unsigned int num_save;
1542 unsigned int default_stack;
1543 int code;
1544 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1545 long reg_saved = 0;
1547 actual_fsize = compute_frame_size (size, &reg_saved);
1549 /* Save/setup global registers for interrupt functions right now. */
1550 if (interrupt_handler)
1552 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1553 emit_insn (gen_callt_save_interrupt ());
1554 else
1555 emit_insn (gen_save_interrupt ());
1557 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1559 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1560 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1563 /* Save arg registers to the stack if necessary. */
1564 else if (current_function_args_info.anonymous_args)
1566 if (TARGET_PROLOG_FUNCTION && TARGET_V850E && !TARGET_DISABLE_CALLT)
1567 emit_insn (gen_save_r6_r9_v850e ());
1568 else if (TARGET_PROLOG_FUNCTION && ! TARGET_LONG_CALLS)
1569 emit_insn (gen_save_r6_r9 ());
1570 else
1572 offset = 0;
1573 for (i = 6; i < 10; i++)
1575 emit_move_insn (gen_rtx_MEM (SImode,
1576 plus_constant (stack_pointer_rtx,
1577 offset)),
1578 gen_rtx_REG (SImode, i));
1579 offset += 4;
1584 /* Identify all of the saved registers. */
1585 num_save = 0;
1586 default_stack = 0;
1587 for (i = 1; i < 31; i++)
1589 if (((1L << i) & reg_saved) != 0)
1590 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1593 /* If the return pointer is saved, the helper functions also allocate
1594 16 bytes of stack for arguments to be saved in. */
1595 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1597 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1598 default_stack = 16;
1601 /* See if we have an insn that allocates stack space and saves the particular
1602 registers we want to. */
1603 save_all = NULL_RTX;
1604 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1606 int alloc_stack = (4 * num_save) + default_stack;
1607 int unalloc_stack = actual_fsize - alloc_stack;
1608 int save_func_len = 4;
1609 int save_normal_len;
1611 if (unalloc_stack)
1612 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1614 /* see if we would have used ep to save the stack */
1615 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1616 save_normal_len = (3 * 2) + (2 * num_save);
1617 else
1618 save_normal_len = 4 * num_save;
1620 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1622 /* Don't bother checking if we don't actually save any space.
1623 This happens for instance if one register is saved and additional
1624 stack space is allocated. */
1625 if (save_func_len < save_normal_len)
1627 save_all = gen_rtx_PARALLEL
1628 (VOIDmode,
1629 rtvec_alloc (num_save + 1
1630 + (TARGET_V850 ? (TARGET_LONG_CALLS ? 2 : 1) : 0)));
1632 XVECEXP (save_all, 0, 0)
1633 = gen_rtx_SET (VOIDmode,
1634 stack_pointer_rtx,
1635 plus_constant (stack_pointer_rtx, -alloc_stack));
1637 offset = - default_stack;
1638 for (i = 0; i < num_save; i++)
1640 XVECEXP (save_all, 0, i+1)
1641 = gen_rtx_SET (VOIDmode,
1642 gen_rtx_MEM (Pmode,
1643 plus_constant (stack_pointer_rtx,
1644 offset)),
1645 save_regs[i]);
1646 offset -= 4;
1649 if (TARGET_V850)
1651 XVECEXP (save_all, 0, num_save + 1)
1652 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1654 if (TARGET_LONG_CALLS)
1655 XVECEXP (save_all, 0, num_save + 2)
1656 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
1659 code = recog (save_all, NULL_RTX, NULL);
1660 if (code >= 0)
1662 rtx insn = emit_insn (save_all);
1663 INSN_CODE (insn) = code;
1664 actual_fsize -= alloc_stack;
1666 if (TARGET_DEBUG)
1667 fprintf (stderr, "\
1668 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1669 save_normal_len - save_func_len,
1670 save_normal_len, save_func_len,
1671 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1673 else
1674 save_all = NULL_RTX;
1678 /* If no prolog save function is available, store the registers the old
1679 fashioned way (one by one). */
1680 if (!save_all)
1682 /* Special case interrupt functions that save all registers for a call. */
1683 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1685 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1686 emit_insn (gen_callt_save_all_interrupt ());
1687 else
1688 emit_insn (gen_save_all_interrupt ());
1690 else
1692 /* If the stack is too big, allocate it in chunks so we can do the
1693 register saves. We use the register save size so we use the ep
1694 register. */
1695 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1696 init_stack_alloc = compute_register_save_size (NULL);
1697 else
1698 init_stack_alloc = actual_fsize;
1700 /* Save registers at the beginning of the stack frame. */
1701 offset = init_stack_alloc - 4;
1703 if (init_stack_alloc)
1704 emit_insn (gen_addsi3 (stack_pointer_rtx,
1705 stack_pointer_rtx,
1706 GEN_INT (-init_stack_alloc)));
1708 /* Save the return pointer first. */
1709 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1711 emit_move_insn (gen_rtx_MEM (SImode,
1712 plus_constant (stack_pointer_rtx,
1713 offset)),
1714 save_regs[--num_save]);
1715 offset -= 4;
1718 for (i = 0; i < num_save; i++)
1720 emit_move_insn (gen_rtx_MEM (SImode,
1721 plus_constant (stack_pointer_rtx,
1722 offset)),
1723 save_regs[i]);
1724 offset -= 4;
1729 /* Allocate the rest of the stack that was not allocated above (either it is
1730 > 32K or we just called a function to save the registers and needed more
1731 stack. */
1732 if (actual_fsize > init_stack_alloc)
1734 int diff = actual_fsize - init_stack_alloc;
1735 if (CONST_OK_FOR_K (diff))
1736 emit_insn (gen_addsi3 (stack_pointer_rtx,
1737 stack_pointer_rtx,
1738 GEN_INT (-diff)));
1739 else
1741 rtx reg = gen_rtx_REG (Pmode, 12);
1742 emit_move_insn (reg, GEN_INT (-diff));
1743 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1747 /* If we need a frame pointer, set it up now. */
1748 if (frame_pointer_needed)
1749 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1753 void
1754 expand_epilogue (void)
1756 unsigned int i;
1757 int offset;
1758 unsigned int size = get_frame_size ();
1759 long reg_saved = 0;
1760 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1761 unsigned int init_stack_free = 0;
1762 rtx restore_regs[32];
1763 rtx restore_all;
1764 unsigned int num_restore;
1765 unsigned int default_stack;
1766 int code;
1767 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1769 /* Eliminate the initial stack stored by interrupt functions. */
1770 if (interrupt_handler)
1772 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1773 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1774 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1777 /* Cut off any dynamic stack created. */
1778 if (frame_pointer_needed)
1779 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1781 /* Identify all of the saved registers. */
1782 num_restore = 0;
1783 default_stack = 0;
1784 for (i = 1; i < 31; i++)
1786 if (((1L << i) & reg_saved) != 0)
1787 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1790 /* If the return pointer is saved, the helper functions also allocate
1791 16 bytes of stack for arguments to be saved in. */
1792 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1794 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1795 default_stack = 16;
1798 /* See if we have an insn that restores the particular registers we
1799 want to. */
1800 restore_all = NULL_RTX;
1802 if (TARGET_PROLOG_FUNCTION
1803 && num_restore > 0
1804 && actual_fsize >= default_stack
1805 && !interrupt_handler)
1807 int alloc_stack = (4 * num_restore) + default_stack;
1808 int unalloc_stack = actual_fsize - alloc_stack;
1809 int restore_func_len = 4;
1810 int restore_normal_len;
1812 if (unalloc_stack)
1813 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1815 /* See if we would have used ep to restore the registers. */
1816 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1817 restore_normal_len = (3 * 2) + (2 * num_restore);
1818 else
1819 restore_normal_len = 4 * num_restore;
1821 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1823 /* Don't bother checking if we don't actually save any space. */
1824 if (restore_func_len < restore_normal_len)
1826 restore_all = gen_rtx_PARALLEL (VOIDmode,
1827 rtvec_alloc (num_restore + 2));
1828 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1829 XVECEXP (restore_all, 0, 1)
1830 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1831 gen_rtx_PLUS (Pmode,
1832 stack_pointer_rtx,
1833 GEN_INT (alloc_stack)));
1835 offset = alloc_stack - 4;
1836 for (i = 0; i < num_restore; i++)
1838 XVECEXP (restore_all, 0, i+2)
1839 = gen_rtx_SET (VOIDmode,
1840 restore_regs[i],
1841 gen_rtx_MEM (Pmode,
1842 plus_constant (stack_pointer_rtx,
1843 offset)));
1844 offset -= 4;
1847 code = recog (restore_all, NULL_RTX, NULL);
1849 if (code >= 0)
1851 rtx insn;
1853 actual_fsize -= alloc_stack;
1854 if (actual_fsize)
1856 if (CONST_OK_FOR_K (actual_fsize))
1857 emit_insn (gen_addsi3 (stack_pointer_rtx,
1858 stack_pointer_rtx,
1859 GEN_INT (actual_fsize)));
1860 else
1862 rtx reg = gen_rtx_REG (Pmode, 12);
1863 emit_move_insn (reg, GEN_INT (actual_fsize));
1864 emit_insn (gen_addsi3 (stack_pointer_rtx,
1865 stack_pointer_rtx,
1866 reg));
1870 insn = emit_jump_insn (restore_all);
1871 INSN_CODE (insn) = code;
1873 if (TARGET_DEBUG)
1874 fprintf (stderr, "\
1875 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1876 restore_normal_len - restore_func_len,
1877 restore_normal_len, restore_func_len,
1878 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1880 else
1881 restore_all = NULL_RTX;
1885 /* If no epilog save function is available, restore the registers the
1886 old fashioned way (one by one). */
1887 if (!restore_all)
1889 /* If the stack is large, we need to cut it down in 2 pieces. */
1890 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1891 init_stack_free = 4 * num_restore;
1892 else
1893 init_stack_free = actual_fsize;
1895 /* Deallocate the rest of the stack if it is > 32K. */
1896 if (actual_fsize > init_stack_free)
1898 int diff;
1900 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1902 if (CONST_OK_FOR_K (diff))
1903 emit_insn (gen_addsi3 (stack_pointer_rtx,
1904 stack_pointer_rtx,
1905 GEN_INT (diff)));
1906 else
1908 rtx reg = gen_rtx_REG (Pmode, 12);
1909 emit_move_insn (reg, GEN_INT (diff));
1910 emit_insn (gen_addsi3 (stack_pointer_rtx,
1911 stack_pointer_rtx,
1912 reg));
1916 /* Special case interrupt functions that save all registers
1917 for a call. */
1918 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1920 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1921 emit_insn (gen_callt_restore_all_interrupt ());
1922 else
1923 emit_insn (gen_restore_all_interrupt ());
1925 else
1927 /* Restore registers from the beginning of the stack frame. */
1928 offset = init_stack_free - 4;
1930 /* Restore the return pointer first. */
1931 if (num_restore > 0
1932 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
1934 emit_move_insn (restore_regs[--num_restore],
1935 gen_rtx_MEM (SImode,
1936 plus_constant (stack_pointer_rtx,
1937 offset)));
1938 offset -= 4;
1941 for (i = 0; i < num_restore; i++)
1943 emit_move_insn (restore_regs[i],
1944 gen_rtx_MEM (SImode,
1945 plus_constant (stack_pointer_rtx,
1946 offset)));
1948 emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
1949 offset -= 4;
1952 /* Cut back the remainder of the stack. */
1953 if (init_stack_free)
1954 emit_insn (gen_addsi3 (stack_pointer_rtx,
1955 stack_pointer_rtx,
1956 GEN_INT (init_stack_free)));
1959 /* And return or use reti for interrupt handlers. */
1960 if (interrupt_handler)
1962 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1963 emit_insn (gen_callt_return_interrupt ());
1964 else
1965 emit_jump_insn (gen_return_interrupt ());
1967 else if (actual_fsize)
1968 emit_jump_insn (gen_return_internal ());
1969 else
1970 emit_jump_insn (gen_return ());
1973 v850_interrupt_cache_p = FALSE;
1974 v850_interrupt_p = FALSE;
1978 /* Update the condition code from the insn. */
1980 void
1981 notice_update_cc (rtx body, rtx insn)
1983 switch (get_attr_cc (insn))
1985 case CC_NONE:
1986 /* Insn does not affect CC at all. */
1987 break;
1989 case CC_NONE_0HIT:
1990 /* Insn does not change CC, but the 0'th operand has been changed. */
1991 if (cc_status.value1 != 0
1992 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1993 cc_status.value1 = 0;
1994 break;
1996 case CC_SET_ZN:
1997 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1998 V,C is in an unusable state. */
1999 CC_STATUS_INIT;
2000 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2001 cc_status.value1 = recog_data.operand[0];
2002 break;
2004 case CC_SET_ZNV:
2005 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2006 C is in an unusable state. */
2007 CC_STATUS_INIT;
2008 cc_status.flags |= CC_NO_CARRY;
2009 cc_status.value1 = recog_data.operand[0];
2010 break;
2012 case CC_COMPARE:
2013 /* The insn is a compare instruction. */
2014 CC_STATUS_INIT;
2015 cc_status.value1 = SET_SRC (body);
2016 break;
2018 case CC_CLOBBER:
2019 /* Insn doesn't leave CC in a usable state. */
2020 CC_STATUS_INIT;
2021 break;
2025 /* Retrieve the data area that has been chosen for the given decl. */
2027 v850_data_area
2028 v850_get_data_area (tree decl)
2030 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2031 return DATA_AREA_SDA;
2033 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2034 return DATA_AREA_TDA;
2036 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2037 return DATA_AREA_ZDA;
2039 return DATA_AREA_NORMAL;
2042 /* Store the indicated data area in the decl's attributes. */
2044 static void
2045 v850_set_data_area (tree decl, v850_data_area data_area)
2047 tree name;
2049 switch (data_area)
2051 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2052 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2053 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2054 default:
2055 return;
2058 DECL_ATTRIBUTES (decl) = tree_cons
2059 (name, NULL, DECL_ATTRIBUTES (decl));
2062 const struct attribute_spec v850_attribute_table[] =
2064 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2065 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2066 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2067 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2068 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2069 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2070 { NULL, 0, 0, false, false, false, NULL }
2073 /* Handle an "interrupt" attribute; arguments as in
2074 struct attribute_spec.handler. */
2075 static tree
2076 v850_handle_interrupt_attribute (tree * node,
2077 tree name,
2078 tree args ATTRIBUTE_UNUSED,
2079 int flags ATTRIBUTE_UNUSED,
2080 bool * no_add_attrs)
2082 if (TREE_CODE (*node) != FUNCTION_DECL)
2084 warning ("%qs attribute only applies to functions",
2085 IDENTIFIER_POINTER (name));
2086 *no_add_attrs = true;
2089 return NULL_TREE;
2092 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2093 struct attribute_spec.handler. */
2094 static tree
2095 v850_handle_data_area_attribute (tree* node,
2096 tree name,
2097 tree args ATTRIBUTE_UNUSED,
2098 int flags ATTRIBUTE_UNUSED,
2099 bool * no_add_attrs)
2101 v850_data_area data_area;
2102 v850_data_area area;
2103 tree decl = *node;
2105 /* Implement data area attribute. */
2106 if (is_attribute_p ("sda", name))
2107 data_area = DATA_AREA_SDA;
2108 else if (is_attribute_p ("tda", name))
2109 data_area = DATA_AREA_TDA;
2110 else if (is_attribute_p ("zda", name))
2111 data_area = DATA_AREA_ZDA;
2112 else
2113 abort ();
2115 switch (TREE_CODE (decl))
2117 case VAR_DECL:
2118 if (current_function_decl != NULL_TREE)
2120 error ("%Jdata area attributes cannot be specified for "
2121 "local variables", decl, decl);
2122 *no_add_attrs = true;
2125 /* Drop through. */
2127 case FUNCTION_DECL:
2128 area = v850_get_data_area (decl);
2129 if (area != DATA_AREA_NORMAL && data_area != area)
2131 error ("%Jdata area of '%D' conflicts with previous declaration",
2132 decl, decl);
2133 *no_add_attrs = true;
2135 break;
2137 default:
2138 break;
2141 return NULL_TREE;
2145 /* Return nonzero if FUNC is an interrupt function as specified
2146 by the "interrupt" attribute. */
2149 v850_interrupt_function_p (tree func)
2151 tree a;
2152 int ret = 0;
2154 if (v850_interrupt_cache_p)
2155 return v850_interrupt_p;
2157 if (TREE_CODE (func) != FUNCTION_DECL)
2158 return 0;
2160 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2161 if (a != NULL_TREE)
2162 ret = 1;
2164 else
2166 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2167 ret = a != NULL_TREE;
2170 /* Its not safe to trust global variables until after function inlining has
2171 been done. */
2172 if (reload_completed | reload_in_progress)
2173 v850_interrupt_p = ret;
2175 return ret;
2179 static void
2180 v850_encode_data_area (tree decl, rtx symbol)
2182 int flags;
2184 /* Map explicit sections into the appropriate attribute */
2185 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2187 if (DECL_SECTION_NAME (decl))
2189 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2191 if (streq (name, ".zdata") || streq (name, ".zbss"))
2192 v850_set_data_area (decl, DATA_AREA_ZDA);
2194 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2195 v850_set_data_area (decl, DATA_AREA_SDA);
2197 else if (streq (name, ".tdata"))
2198 v850_set_data_area (decl, DATA_AREA_TDA);
2201 /* If no attribute, support -m{zda,sda,tda}=n */
2202 else
2204 int size = int_size_in_bytes (TREE_TYPE (decl));
2205 if (size <= 0)
2208 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2209 v850_set_data_area (decl, DATA_AREA_TDA);
2211 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2212 v850_set_data_area (decl, DATA_AREA_SDA);
2214 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2215 v850_set_data_area (decl, DATA_AREA_ZDA);
2218 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2219 return;
2222 flags = SYMBOL_REF_FLAGS (symbol);
2223 switch (v850_get_data_area (decl))
2225 case DATA_AREA_ZDA: flags |= SYMBOL_FLAG_ZDA; break;
2226 case DATA_AREA_TDA: flags |= SYMBOL_FLAG_TDA; break;
2227 case DATA_AREA_SDA: flags |= SYMBOL_FLAG_SDA; break;
2228 default: abort ();
2230 SYMBOL_REF_FLAGS (symbol) = flags;
2233 static void
2234 v850_encode_section_info (tree decl, rtx rtl, int first)
2236 default_encode_section_info (decl, rtl, first);
2238 if (TREE_CODE (decl) == VAR_DECL
2239 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2240 v850_encode_data_area (decl, XEXP (rtl, 0));
2243 /* Construct a JR instruction to a routine that will perform the equivalent of
2244 the RTL passed in as an argument. This RTL is a function epilogue that
2245 pops registers off the stack and possibly releases some extra stack space
2246 as well. The code has already verified that the RTL matches these
2247 requirements. */
2248 char *
2249 construct_restore_jr (rtx op)
2251 int count = XVECLEN (op, 0);
2252 int stack_bytes;
2253 unsigned long int mask;
2254 unsigned long int first;
2255 unsigned long int last;
2256 int i;
2257 static char buff [100]; /* XXX */
2259 if (count <= 2)
2261 error ("bogus JR construction: %d\n", count);
2262 return NULL;
2265 /* Work out how many bytes to pop off the stack before retrieving
2266 registers. */
2267 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2268 abort ();
2269 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2270 abort ();
2271 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2272 abort ();
2274 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2276 /* Each pop will remove 4 bytes from the stack.... */
2277 stack_bytes -= (count - 2) * 4;
2279 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2280 if (stack_bytes != 0 && stack_bytes != 16)
2282 error ("bad amount of stack space removal: %d", stack_bytes);
2283 return NULL;
2286 /* Now compute the bit mask of registers to push. */
2287 mask = 0;
2288 for (i = 2; i < count; i++)
2290 rtx vector_element = XVECEXP (op, 0, i);
2292 if (GET_CODE (vector_element) != SET)
2293 abort ();
2294 if (GET_CODE (SET_DEST (vector_element)) != REG)
2295 abort ();
2296 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2297 abort ();
2299 mask |= 1 << REGNO (SET_DEST (vector_element));
2302 /* Scan for the first register to pop. */
2303 for (first = 0; first < 32; first++)
2305 if (mask & (1 << first))
2306 break;
2309 if (first >= 32)
2310 abort ();
2312 /* Discover the last register to pop. */
2313 if (mask & (1 << LINK_POINTER_REGNUM))
2315 if (stack_bytes != 16)
2316 abort ();
2318 last = LINK_POINTER_REGNUM;
2320 else
2322 if (stack_bytes != 0)
2323 abort ();
2325 if ((mask & (1 << 29)) == 0)
2326 abort ();
2328 last = 29;
2331 /* Note, it is possible to have gaps in the register mask.
2332 We ignore this here, and generate a JR anyway. We will
2333 be popping more registers than is strictly necessary, but
2334 it does save code space. */
2336 if (TARGET_LONG_CALLS)
2338 char name[40];
2340 if (first == last)
2341 sprintf (name, "__return_%s", reg_names [first]);
2342 else
2343 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2345 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2346 name, name);
2348 else
2350 if (first == last)
2351 sprintf (buff, "jr __return_%s", reg_names [first]);
2352 else
2353 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2356 return buff;
2360 /* Construct a JARL instruction to a routine that will perform the equivalent
2361 of the RTL passed as a parameter. This RTL is a function prologue that
2362 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2363 some stack space as well. The code has already verified that the RTL
2364 matches these requirements. */
2365 char *
2366 construct_save_jarl (rtx op)
2368 int count = XVECLEN (op, 0);
2369 int stack_bytes;
2370 unsigned long int mask;
2371 unsigned long int first;
2372 unsigned long int last;
2373 int i;
2374 static char buff [100]; /* XXX */
2376 if (count <= 2)
2378 error ("bogus JARL construction: %d\n", count);
2379 return NULL;
2382 /* Paranoia. */
2383 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2384 abort ();
2385 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2386 abort ();
2387 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2388 abort ();
2389 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2390 abort ();
2392 /* Work out how many bytes to push onto the stack after storing the
2393 registers. */
2394 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2396 /* Each push will put 4 bytes from the stack.... */
2397 stack_bytes += (count - (TARGET_LONG_CALLS ? 3 : 2)) * 4;
2399 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2400 if (stack_bytes != 0 && stack_bytes != -16)
2402 error ("bad amount of stack space removal: %d", stack_bytes);
2403 return NULL;
2406 /* Now compute the bit mask of registers to push. */
2407 mask = 0;
2408 for (i = 1; i < count - (TARGET_LONG_CALLS ? 2 : 1); i++)
2410 rtx vector_element = XVECEXP (op, 0, i);
2412 if (GET_CODE (vector_element) != SET)
2413 abort ();
2414 if (GET_CODE (SET_SRC (vector_element)) != REG)
2415 abort ();
2416 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2417 abort ();
2419 mask |= 1 << REGNO (SET_SRC (vector_element));
2422 /* Scan for the first register to push. */
2423 for (first = 0; first < 32; first++)
2425 if (mask & (1 << first))
2426 break;
2429 if (first >= 32)
2430 abort ();
2432 /* Discover the last register to push. */
2433 if (mask & (1 << LINK_POINTER_REGNUM))
2435 if (stack_bytes != -16)
2436 abort ();
2438 last = LINK_POINTER_REGNUM;
2440 else
2442 if (stack_bytes != 0)
2443 abort ();
2444 if ((mask & (1 << 29)) == 0)
2445 abort ();
2447 last = 29;
2450 /* Note, it is possible to have gaps in the register mask.
2451 We ignore this here, and generate a JARL anyway. We will
2452 be pushing more registers than is strictly necessary, but
2453 it does save code space. */
2455 if (TARGET_LONG_CALLS)
2457 char name[40];
2459 if (first == last)
2460 sprintf (name, "__save_%s", reg_names [first]);
2461 else
2462 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2464 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2465 name, name);
2467 else
2469 if (first == last)
2470 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2471 else
2472 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2473 reg_names [last]);
2476 return buff;
2479 extern tree last_assemble_variable_decl;
2480 extern int size_directive_output;
2482 /* A version of asm_output_aligned_bss() that copes with the special
2483 data areas of the v850. */
2484 void
2485 v850_output_aligned_bss (FILE * file,
2486 tree decl,
2487 const char * name,
2488 int size,
2489 int align)
2491 switch (v850_get_data_area (decl))
2493 case DATA_AREA_ZDA:
2494 zbss_section ();
2495 break;
2497 case DATA_AREA_SDA:
2498 sbss_section ();
2499 break;
2501 case DATA_AREA_TDA:
2502 tdata_section ();
2504 default:
2505 bss_section ();
2506 break;
2509 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2510 #ifdef ASM_DECLARE_OBJECT_NAME
2511 last_assemble_variable_decl = decl;
2512 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2513 #else
2514 /* Standard thing is just output label for the object. */
2515 ASM_OUTPUT_LABEL (file, name);
2516 #endif /* ASM_DECLARE_OBJECT_NAME */
2517 ASM_OUTPUT_SKIP (file, size ? size : 1);
2520 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2521 void
2522 v850_output_common (FILE * file,
2523 tree decl,
2524 const char * name,
2525 int size,
2526 int align)
2528 if (decl == NULL_TREE)
2530 fprintf (file, "%s", COMMON_ASM_OP);
2532 else
2534 switch (v850_get_data_area (decl))
2536 case DATA_AREA_ZDA:
2537 fprintf (file, "%s", ZCOMMON_ASM_OP);
2538 break;
2540 case DATA_AREA_SDA:
2541 fprintf (file, "%s", SCOMMON_ASM_OP);
2542 break;
2544 case DATA_AREA_TDA:
2545 fprintf (file, "%s", TCOMMON_ASM_OP);
2546 break;
2548 default:
2549 fprintf (file, "%s", COMMON_ASM_OP);
2550 break;
2554 assemble_name (file, name);
2555 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2558 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2559 void
2560 v850_output_local (FILE * file,
2561 tree decl,
2562 const char * name,
2563 int size,
2564 int align)
2566 fprintf (file, "%s", LOCAL_ASM_OP);
2567 assemble_name (file, name);
2568 fprintf (file, "\n");
2570 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2573 /* Add data area to the given declaration if a ghs data area pragma is
2574 currently in effect (#pragma ghs startXXX/endXXX). */
2575 static void
2576 v850_insert_attributes (tree decl, tree * attr_ptr ATTRIBUTE_UNUSED )
2578 if (data_area_stack
2579 && data_area_stack->data_area
2580 && current_function_decl == NULL_TREE
2581 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2582 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2583 v850_set_data_area (decl, data_area_stack->data_area);
2585 /* Initialize the default names of the v850 specific sections,
2586 if this has not been done before. */
2588 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2590 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2591 = build_string (sizeof (".sdata")-1, ".sdata");
2593 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2594 = build_string (sizeof (".rosdata")-1, ".rosdata");
2596 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2597 = build_string (sizeof (".tdata")-1, ".tdata");
2599 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2600 = build_string (sizeof (".zdata")-1, ".zdata");
2602 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2603 = build_string (sizeof (".rozdata")-1, ".rozdata");
2606 if (current_function_decl == NULL_TREE
2607 && (TREE_CODE (decl) == VAR_DECL
2608 || TREE_CODE (decl) == CONST_DECL
2609 || TREE_CODE (decl) == FUNCTION_DECL)
2610 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2611 && !DECL_SECTION_NAME (decl))
2613 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2614 tree chosen_section;
2616 if (TREE_CODE (decl) == FUNCTION_DECL)
2617 kind = GHS_SECTION_KIND_TEXT;
2618 else
2620 /* First choose a section kind based on the data area of the decl. */
2621 switch (v850_get_data_area (decl))
2623 default:
2624 abort ();
2626 case DATA_AREA_SDA:
2627 kind = ((TREE_READONLY (decl))
2628 ? GHS_SECTION_KIND_ROSDATA
2629 : GHS_SECTION_KIND_SDATA);
2630 break;
2632 case DATA_AREA_TDA:
2633 kind = GHS_SECTION_KIND_TDATA;
2634 break;
2636 case DATA_AREA_ZDA:
2637 kind = ((TREE_READONLY (decl))
2638 ? GHS_SECTION_KIND_ROZDATA
2639 : GHS_SECTION_KIND_ZDATA);
2640 break;
2642 case DATA_AREA_NORMAL: /* default data area */
2643 if (TREE_READONLY (decl))
2644 kind = GHS_SECTION_KIND_RODATA;
2645 else if (DECL_INITIAL (decl))
2646 kind = GHS_SECTION_KIND_DATA;
2647 else
2648 kind = GHS_SECTION_KIND_BSS;
2652 /* Now, if the section kind has been explicitly renamed,
2653 then attach a section attribute. */
2654 chosen_section = GHS_current_section_names [(int) kind];
2656 /* Otherwise, if this kind of section needs an explicit section
2657 attribute, then also attach one. */
2658 if (chosen_section == NULL)
2659 chosen_section = GHS_default_section_names [(int) kind];
2661 if (chosen_section)
2663 /* Only set the section name if specified by a pragma, because
2664 otherwise it will force those variables to get allocated storage
2665 in this module, rather than by the linker. */
2666 DECL_SECTION_NAME (decl) = chosen_section;
2671 /* Construct a DISPOSE instruction that is the equivalent of
2672 the given RTX. We have already verified that this should
2673 be possible. */
2675 char *
2676 construct_dispose_instruction (rtx op)
2678 int count = XVECLEN (op, 0);
2679 int stack_bytes;
2680 unsigned long int mask;
2681 int i;
2682 static char buff[ 100 ]; /* XXX */
2683 int use_callt = 0;
2685 if (count <= 2)
2687 error ("Bogus DISPOSE construction: %d\n", count);
2688 return NULL;
2691 /* Work out how many bytes to pop off the
2692 stack before retrieving registers. */
2693 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2694 abort ();
2695 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2696 abort ();
2697 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2698 abort ();
2700 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2702 /* Each pop will remove 4 bytes from the stack.... */
2703 stack_bytes -= (count - 2) * 4;
2705 /* Make sure that the amount we are popping
2706 will fit into the DISPOSE instruction. */
2707 if (stack_bytes > 128)
2709 error ("Too much stack space to dispose of: %d", stack_bytes);
2710 return NULL;
2713 /* Now compute the bit mask of registers to push. */
2714 mask = 0;
2716 for (i = 2; i < count; i++)
2718 rtx vector_element = XVECEXP (op, 0, i);
2720 if (GET_CODE (vector_element) != SET)
2721 abort ();
2722 if (GET_CODE (SET_DEST (vector_element)) != REG)
2723 abort ();
2724 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2725 abort ();
2727 if (REGNO (SET_DEST (vector_element)) == 2)
2728 use_callt = 1;
2729 else
2730 mask |= 1 << REGNO (SET_DEST (vector_element));
2733 if (! TARGET_DISABLE_CALLT
2734 && (use_callt || stack_bytes == 0 || stack_bytes == 16))
2736 if (use_callt)
2738 sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
2739 return buff;
2741 else
2743 for (i = 20; i < 32; i++)
2744 if (mask & (1 << i))
2745 break;
2747 if (i == 31)
2748 sprintf (buff, "callt ctoff(__callt_return_r31c)");
2749 else
2750 sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
2751 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2754 else
2756 static char regs [100]; /* XXX */
2757 int done_one;
2759 /* Generate the DISPOSE instruction. Note we could just issue the
2760 bit mask as a number as the assembler can cope with this, but for
2761 the sake of our readers we turn it into a textual description. */
2762 regs[0] = 0;
2763 done_one = 0;
2765 for (i = 20; i < 32; i++)
2767 if (mask & (1 << i))
2769 int first;
2771 if (done_one)
2772 strcat (regs, ", ");
2773 else
2774 done_one = 1;
2776 first = i;
2777 strcat (regs, reg_names[ first ]);
2779 for (i++; i < 32; i++)
2780 if ((mask & (1 << i)) == 0)
2781 break;
2783 if (i > first + 1)
2785 strcat (regs, " - ");
2786 strcat (regs, reg_names[ i - 1 ] );
2791 sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
2794 return buff;
2797 /* Construct a PREPARE instruction that is the equivalent of
2798 the given RTL. We have already verified that this should
2799 be possible. */
2801 char *
2802 construct_prepare_instruction (rtx op)
2804 int count = XVECLEN (op, 0);
2805 int stack_bytes;
2806 unsigned long int mask;
2807 int i;
2808 static char buff[ 100 ]; /* XXX */
2809 int use_callt = 0;
2811 if (count <= 1)
2813 error ("Bogus PREPEARE construction: %d\n", count);
2814 return NULL;
2817 /* Work out how many bytes to push onto
2818 the stack after storing the registers. */
2819 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2820 abort ();
2821 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2822 abort ();
2823 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2824 abort ();
2826 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2828 /* Each push will put 4 bytes from the stack. */
2829 stack_bytes += (count - 1) * 4;
2831 /* Make sure that the amount we are popping
2832 will fit into the DISPOSE instruction. */
2833 if (stack_bytes < -128)
2835 error ("Too much stack space to prepare: %d", stack_bytes);
2836 return NULL;
2839 /* Now compute the bit mask of registers to push. */
2840 mask = 0;
2841 for (i = 1; i < count; i++)
2843 rtx vector_element = XVECEXP (op, 0, i);
2845 if (GET_CODE (vector_element) != SET)
2846 abort ();
2847 if (GET_CODE (SET_SRC (vector_element)) != REG)
2848 abort ();
2849 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2850 abort ();
2852 if (REGNO (SET_SRC (vector_element)) == 2)
2853 use_callt = 1;
2854 else
2855 mask |= 1 << REGNO (SET_SRC (vector_element));
2858 if ((! TARGET_DISABLE_CALLT)
2859 && (use_callt || stack_bytes == 0 || stack_bytes == -16))
2861 if (use_callt)
2863 sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
2864 return buff;
2867 for (i = 20; i < 32; i++)
2868 if (mask & (1 << i))
2869 break;
2871 if (i == 31)
2872 sprintf (buff, "callt ctoff(__callt_save_r31c)");
2873 else
2874 sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
2875 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
2877 else
2879 static char regs [100]; /* XXX */
2880 int done_one;
2883 /* Generate the PREPARE instruction. Note we could just issue the
2884 bit mask as a number as the assembler can cope with this, but for
2885 the sake of our readers we turn it into a textual description. */
2886 regs[0] = 0;
2887 done_one = 0;
2889 for (i = 20; i < 32; i++)
2891 if (mask & (1 << i))
2893 int first;
2895 if (done_one)
2896 strcat (regs, ", ");
2897 else
2898 done_one = 1;
2900 first = i;
2901 strcat (regs, reg_names[ first ]);
2903 for (i++; i < 32; i++)
2904 if ((mask & (1 << i)) == 0)
2905 break;
2907 if (i > first + 1)
2909 strcat (regs, " - ");
2910 strcat (regs, reg_names[ i - 1 ] );
2915 sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
2918 return buff;
2921 /* Return an RTX indicating where the return address to the
2922 calling function can be found. */
2925 v850_return_addr (int count)
2927 if (count != 0)
2928 return const0_rtx;
2930 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
2933 static void
2934 v850_select_section (tree exp,
2935 int reloc ATTRIBUTE_UNUSED,
2936 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
2938 if (TREE_CODE (exp) == VAR_DECL)
2940 int is_const;
2941 if (!TREE_READONLY (exp)
2942 || TREE_SIDE_EFFECTS (exp)
2943 || !DECL_INITIAL (exp)
2944 || (DECL_INITIAL (exp) != error_mark_node
2945 && !TREE_CONSTANT (DECL_INITIAL (exp))))
2946 is_const = FALSE;
2947 else
2948 is_const = TRUE;
2950 switch (v850_get_data_area (exp))
2952 case DATA_AREA_ZDA:
2953 if (is_const)
2954 rozdata_section ();
2955 else
2956 zdata_section ();
2957 break;
2959 case DATA_AREA_TDA:
2960 tdata_section ();
2961 break;
2963 case DATA_AREA_SDA:
2964 if (is_const)
2965 rosdata_section ();
2966 else
2967 sdata_section ();
2968 break;
2970 default:
2971 if (is_const)
2972 readonly_data_section ();
2973 else
2974 data_section ();
2975 break;
2978 else
2979 readonly_data_section ();
2982 /* Worker function for TARGET_RETURN_IN_MEMORY. */
2984 static bool
2985 v850_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
2987 /* Return values > 8 bytes in length in memory. */
2988 return int_size_in_bytes (type) > 8 || TYPE_MODE (type) == BLKmode;
2991 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
2993 static void
2994 v850_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
2995 enum machine_mode mode ATTRIBUTE_UNUSED,
2996 tree type ATTRIBUTE_UNUSED,
2997 int *pretend_arg_size ATTRIBUTE_UNUSED,
2998 int second_time ATTRIBUTE_UNUSED)
3000 ca->anonymous_args = (!TARGET_GHS ? 1 : 0);