* target.h (asm_out.byte_op, asm_out.aligned_op, asm_out.unaligned_op,
[official-gcc.git] / gcc / config / v850 / v850.c
blob3c9ac92e23bbfaa21bb5a0202436321202ea7902
1 /* Subroutines for insn-output.c for NEC V850 series
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Jeff Law (law@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it 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 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "expr.h"
37 #include "function.h"
38 #include "toplev.h"
39 #include "cpplib.h"
40 #include "c-lex.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 PARAMS ((rtx, HOST_WIDE_INT *, HOST_WIDE_INT *));
53 static int const_costs_int PARAMS ((HOST_WIDE_INT, int));
54 static void substitute_ep_register PARAMS ((rtx, rtx, int, int, rtx *, rtx *));
55 static int ep_memory_offset PARAMS ((enum machine_mode, int));
56 static void v850_set_data_area PARAMS ((tree, v850_data_area));
57 const struct attribute_spec v850_attribute_table[];
58 static tree v850_handle_interrupt_attribute PARAMS ((tree *, tree, tree, int, bool *));
59 static tree v850_handle_data_area_attribute PARAMS ((tree *, tree, tree, int, bool *));
60 static void v850_insert_attributes PARAMS ((tree, tree *));
62 /* True if the current function has anonymous arguments. */
63 int current_function_anonymous_args;
65 /* Information about the various small memory areas. */
66 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
68 /* name value max physical max */
69 { "tda", (char *)0, 0, 256 },
70 { "sda", (char *)0, 0, 65536 },
71 { "zda", (char *)0, 0, 32768 },
74 /* Names of the various data areas used on the v850. */
75 tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
76 tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
78 /* Track the current data area set by the data area pragma (which
79 can be nested). Tested by check_default_data_area. */
80 data_area_stack_element * data_area_stack = NULL;
82 /* True if we don't need to check any more if the current
83 function is an interrupt handler. */
84 static int v850_interrupt_cache_p = FALSE;
86 /* Whether current function is an interrupt handler. */
87 static int v850_interrupt_p = FALSE;
89 /* Initialize the GCC target structure. */
90 #undef TARGET_ASM_ALIGNED_HI_OP
91 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
93 #undef TARGET_ATTRIBUTE_TABLE
94 #define TARGET_ATTRIBUTE_TABLE v850_attribute_table
96 #undef TARGET_INSERT_ATTRIBUTES
97 #define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
99 struct gcc_target targetm = TARGET_INITIALIZER;
101 /* Sometimes certain combinations of command options do not make
102 sense on a particular target machine. You can define a macro
103 `OVERRIDE_OPTIONS' to take account of this. This macro, if
104 defined, is executed once just after all the command options have
105 been parsed.
107 Don't use this macro to turn on various extra optimizations for
108 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
110 void
111 override_options ()
113 int i;
114 extern int atoi PARAMS ((const char *));
116 /* Parse -m{s,t,z}da=nnn switches */
117 for (i = 0; i < (int)SMALL_MEMORY_max; i++)
119 if (small_memory[i].value)
121 if (!ISDIGIT (*small_memory[i].value))
122 error ("%s=%s is not numeric",
123 small_memory[i].name,
124 small_memory[i].value);
125 else
127 small_memory[i].max = atoi (small_memory[i].value);
128 if (small_memory[i].max > small_memory[i].physical_max)
129 error ("%s=%s is too large",
130 small_memory[i].name,
131 small_memory[i].value);
138 /* Output assembly code for the start of the file. */
140 void
141 asm_file_start (file)
142 FILE *file;
144 output_file_directive (file, main_input_filename);
148 /* Return an RTX to represent where a value with mode MODE will be returned
149 from a function. If the result is 0, the argument is pushed. */
152 function_arg (cum, mode, type, named)
153 CUMULATIVE_ARGS *cum;
154 enum machine_mode mode;
155 tree type;
156 int named;
158 rtx result = 0;
159 int size, align;
161 if (TARGET_GHS && !named)
162 return NULL_RTX;
164 if (mode == BLKmode)
165 size = int_size_in_bytes (type);
166 else
167 size = GET_MODE_SIZE (mode);
169 if (type)
170 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
171 else
172 align = size;
174 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
176 if (cum->nbytes > 4 * UNITS_PER_WORD)
177 return 0;
179 if (type == NULL_TREE
180 && cum->nbytes + size > 4 * UNITS_PER_WORD)
181 return 0;
183 switch (cum->nbytes / UNITS_PER_WORD)
185 case 0:
186 result = gen_rtx_REG (mode, 6);
187 break;
188 case 1:
189 result = gen_rtx_REG (mode, 7);
190 break;
191 case 2:
192 result = gen_rtx_REG (mode, 8);
193 break;
194 case 3:
195 result = gen_rtx_REG (mode, 9);
196 break;
197 default:
198 result = 0;
201 return result;
205 /* Return the number of words which must be put into registers
206 for values which are part in registers and part in memory. */
209 function_arg_partial_nregs (cum, mode, type, named)
210 CUMULATIVE_ARGS *cum;
211 enum machine_mode mode;
212 tree type;
213 int named;
215 int size, align;
217 if (TARGET_GHS && !named)
218 return 0;
220 if (mode == BLKmode)
221 size = int_size_in_bytes (type);
222 else
223 size = GET_MODE_SIZE (mode);
225 if (type)
226 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
227 else
228 align = size;
230 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
232 if (cum->nbytes > 4 * UNITS_PER_WORD)
233 return 0;
235 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
236 return 0;
238 if (type == NULL_TREE
239 && cum->nbytes + size > 4 * UNITS_PER_WORD)
240 return 0;
242 return (4 * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
246 /* Return the high and low words of a CONST_DOUBLE */
248 static void
249 const_double_split (x, p_high, p_low)
250 rtx x;
251 HOST_WIDE_INT *p_high;
252 HOST_WIDE_INT *p_low;
254 if (GET_CODE (x) == CONST_DOUBLE)
256 long t[2];
257 REAL_VALUE_TYPE rv;
259 switch (GET_MODE (x))
261 case DFmode:
262 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
263 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
264 *p_high = t[1]; /* since v850 is little endian */
265 *p_low = t[0]; /* high is second word */
266 return;
268 case SFmode:
269 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
270 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
271 *p_low = 0;
272 return;
274 case VOIDmode:
275 case DImode:
276 *p_high = CONST_DOUBLE_HIGH (x);
277 *p_low = CONST_DOUBLE_LOW (x);
278 return;
280 default:
281 break;
285 fatal_insn ("const_double_split got a bad insn:", x);
289 /* Return the cost of the rtx R with code CODE. */
291 static int
292 const_costs_int (value, zero_cost)
293 HOST_WIDE_INT value;
294 int zero_cost;
296 if (CONST_OK_FOR_I (value))
297 return zero_cost;
298 else if (CONST_OK_FOR_J (value))
299 return 1;
300 else if (CONST_OK_FOR_K (value))
301 return 2;
302 else
303 return 4;
307 const_costs (r, c)
308 rtx r;
309 enum rtx_code c;
311 HOST_WIDE_INT high, low;
313 switch (c)
315 case CONST_INT:
316 return const_costs_int (INTVAL (r), 0);
318 case CONST_DOUBLE:
319 const_double_split (r, &high, &low);
320 if (GET_MODE (r) == SFmode)
321 return const_costs_int (high, 1);
322 else
323 return const_costs_int (high, 1) + const_costs_int (low, 1);
325 case SYMBOL_REF:
326 case LABEL_REF:
327 case CONST:
328 return 2;
330 case HIGH:
331 return 1;
333 default:
334 return 4;
339 /* Print operand X using operand code CODE to assembly language output file
340 FILE. */
342 void
343 print_operand (file, x, code)
344 FILE *file;
345 rtx x;
346 int code;
348 HOST_WIDE_INT high, low;
350 switch (code)
352 case 'c':
353 /* We use 'c' operands with symbols for .vtinherit */
354 if (GET_CODE (x) == SYMBOL_REF)
356 output_addr_const(file, x);
357 break;
359 /* fall through */
360 case 'b':
361 case 'B':
362 case 'C':
363 switch ((code == 'B' || code == 'C')
364 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
366 case NE:
367 if (code == 'c' || code == 'C')
368 fprintf (file, "nz");
369 else
370 fprintf (file, "ne");
371 break;
372 case EQ:
373 if (code == 'c' || code == 'C')
374 fprintf (file, "z");
375 else
376 fprintf (file, "e");
377 break;
378 case GE:
379 fprintf (file, "ge");
380 break;
381 case GT:
382 fprintf (file, "gt");
383 break;
384 case LE:
385 fprintf (file, "le");
386 break;
387 case LT:
388 fprintf (file, "lt");
389 break;
390 case GEU:
391 fprintf (file, "nl");
392 break;
393 case GTU:
394 fprintf (file, "h");
395 break;
396 case LEU:
397 fprintf (file, "nh");
398 break;
399 case LTU:
400 fprintf (file, "l");
401 break;
402 default:
403 abort ();
405 break;
406 case 'F': /* high word of CONST_DOUBLE */
407 if (GET_CODE (x) == CONST_INT)
408 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
409 else if (GET_CODE (x) == CONST_DOUBLE)
411 const_double_split (x, &high, &low);
412 fprintf (file, "%ld", (long) high);
414 else
415 abort ();
416 break;
417 case 'G': /* low word of CONST_DOUBLE */
418 if (GET_CODE (x) == CONST_INT)
419 fprintf (file, "%ld", (long) INTVAL (x));
420 else if (GET_CODE (x) == CONST_DOUBLE)
422 const_double_split (x, &high, &low);
423 fprintf (file, "%ld", (long) low);
425 else
426 abort ();
427 break;
428 case 'L':
429 fprintf (file, "%d\n", INTVAL (x) & 0xffff);
430 break;
431 case 'M':
432 fprintf (file, "%d", exact_log2 (INTVAL (x)));
433 break;
434 case 'O':
435 if (special_symbolref_operand (x, VOIDmode))
437 const char *name;
439 if (GET_CODE (x) == SYMBOL_REF)
440 name = XSTR (x, 0);
441 else if (GET_CODE (x) == CONST)
442 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
443 else
444 abort ();
446 if (ZDA_NAME_P (name))
447 fprintf (file, "zdaoff");
448 else if (SDA_NAME_P (name))
449 fprintf (file, "sdaoff");
450 else if (TDA_NAME_P (name))
451 fprintf (file, "tdaoff");
452 else
453 abort ();
455 else
456 abort ();
457 break;
458 case 'P':
459 if (special_symbolref_operand (x, VOIDmode))
460 output_addr_const (file, x);
461 else
462 abort ();
463 break;
464 case 'Q':
465 if (special_symbolref_operand (x, VOIDmode))
467 const char *name;
469 if (GET_CODE (x) == SYMBOL_REF)
470 name = XSTR (x, 0);
471 else if (GET_CODE (x) == CONST)
472 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
473 else
474 abort ();
476 if (ZDA_NAME_P (name))
477 fprintf (file, "r0");
478 else if (SDA_NAME_P (name))
479 fprintf (file, "gp");
480 else if (TDA_NAME_P (name))
481 fprintf (file, "ep");
482 else
483 abort ();
485 else
486 abort ();
487 break;
488 case 'R': /* 2nd word of a double. */
489 switch (GET_CODE (x))
491 case REG:
492 fprintf (file, reg_names[REGNO (x) + 1]);
493 break;
494 case MEM:
495 x = XEXP (adjust_address (x, SImode, 4), 0);
496 print_operand_address (file, x);
497 if (GET_CODE (x) == CONST_INT)
498 fprintf (file, "[r0]");
499 break;
501 default:
502 break;
504 break;
505 case 'S':
507 /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
508 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
509 fputs ("s", file);
511 break;
513 case 'T':
515 /* Like an 'S' operand above, but for unsigned loads only. */
516 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
517 fputs ("s", file);
519 break;
521 case 'W': /* print the instruction suffix */
522 switch (GET_MODE (x))
524 default:
525 abort ();
527 case QImode: fputs (".b", file); break;
528 case HImode: fputs (".h", file); break;
529 case SImode: fputs (".w", file); break;
530 case SFmode: fputs (".w", file); break;
532 break;
533 case '.': /* register r0 */
534 fputs (reg_names[0], file);
535 break;
536 case 'z': /* reg or zero */
537 if (x == const0_rtx)
538 fputs (reg_names[0], file);
539 else if (GET_CODE (x) == REG)
540 fputs (reg_names[REGNO (x)], file);
541 else
542 abort ();
543 break;
544 default:
545 switch (GET_CODE (x))
547 case MEM:
548 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
549 output_address (gen_rtx_PLUS (SImode, gen_rtx (REG, SImode, 0),
550 XEXP (x, 0)));
551 else
552 output_address (XEXP (x, 0));
553 break;
555 case REG:
556 fputs (reg_names[REGNO (x)], file);
557 break;
558 case SUBREG:
559 fputs (reg_names[subreg_regno (x)], file);
560 break;
561 case CONST_INT:
562 case SYMBOL_REF:
563 case CONST:
564 case LABEL_REF:
565 case CODE_LABEL:
566 print_operand_address (file, x);
567 break;
568 default:
569 abort ();
571 break;
577 /* Output assembly language output for the address ADDR to FILE. */
579 void
580 print_operand_address (file, addr)
581 FILE *file;
582 rtx addr;
584 switch (GET_CODE (addr))
586 case REG:
587 fprintf (file, "0[");
588 print_operand (file, addr, 0);
589 fprintf (file, "]");
590 break;
591 case LO_SUM:
592 if (GET_CODE (XEXP (addr, 0)) == REG)
594 /* reg,foo */
595 fprintf (file, "lo(");
596 print_operand (file, XEXP (addr, 1), 0);
597 fprintf (file, ")[");
598 print_operand (file, XEXP (addr, 0), 0);
599 fprintf (file, "]");
601 break;
602 case PLUS:
603 if (GET_CODE (XEXP (addr, 0)) == REG
604 || GET_CODE (XEXP (addr, 0)) == SUBREG)
606 /* reg,foo */
607 print_operand (file, XEXP (addr, 1), 0);
608 fprintf (file, "[");
609 print_operand (file, XEXP (addr, 0), 0);
610 fprintf (file, "]");
612 else
614 print_operand (file, XEXP (addr, 0), 0);
615 fprintf (file, "+");
616 print_operand (file, XEXP (addr, 1), 0);
618 break;
619 case SYMBOL_REF:
620 if (ENCODED_NAME_P (XSTR (addr, 0)))
622 const char *name = XSTR (addr, 0);
623 const char *off_name;
624 const char *reg_name;
626 if (ZDA_NAME_P (name))
628 off_name = "zdaoff";
629 reg_name = "r0";
631 else if (SDA_NAME_P (name))
633 off_name = "sdaoff";
634 reg_name = "gp";
636 else if (TDA_NAME_P (name))
638 off_name = "tdaoff";
639 reg_name = "ep";
641 else
642 abort ();
644 fprintf (file, "%s(", off_name);
645 output_addr_const (file, addr);
646 fprintf (file, ")[%s]", reg_name);
648 else
649 output_addr_const (file, addr);
650 break;
651 case CONST:
652 if (special_symbolref_operand (addr, VOIDmode))
654 const char *name = XSTR (XEXP (XEXP (addr, 0), 0), 0);
655 const char *off_name;
656 const char *reg_name;
658 if (ZDA_NAME_P (name))
660 off_name = "zdaoff";
661 reg_name = "r0";
663 else if (SDA_NAME_P (name))
665 off_name = "sdaoff";
666 reg_name = "gp";
668 else if (TDA_NAME_P (name))
670 off_name = "tdaoff";
671 reg_name = "ep";
673 else
674 abort ();
676 fprintf (file, "%s(", off_name);
677 output_addr_const (file, addr);
678 fprintf (file, ")[%s]", reg_name);
680 else
681 output_addr_const (file, addr);
682 break;
683 default:
684 output_addr_const (file, addr);
685 break;
690 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
691 point value. */
693 const char *
694 output_move_single (operands)
695 rtx *operands;
697 rtx dst = operands[0];
698 rtx src = operands[1];
700 if (REG_P (dst))
702 if (REG_P (src))
703 return "mov %1,%0";
705 else if (GET_CODE (src) == CONST_INT)
707 HOST_WIDE_INT value = INTVAL (src);
709 if (CONST_OK_FOR_J (value)) /* signed 5 bit immediate */
710 return "mov %1,%0";
712 else if (CONST_OK_FOR_K (value)) /* signed 16 bit immediate */
713 return "movea lo(%1),%.,%0";
715 else if (CONST_OK_FOR_L (value)) /* upper 16 bits were set */
716 return "movhi hi(%1),%.,%0";
718 else /* random constant */
719 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
722 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
724 HOST_WIDE_INT high, low;
726 const_double_split (src, &high, &low);
727 if (CONST_OK_FOR_J (high)) /* signed 5 bit immediate */
728 return "mov %F1,%0";
730 else if (CONST_OK_FOR_K (high)) /* signed 16 bit immediate */
731 return "movea lo(%F1),%.,%0";
733 else if (CONST_OK_FOR_L (high)) /* upper 16 bits were set */
734 return "movhi hi(%F1),%.,%0";
736 else /* random constant */
737 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
740 else if (GET_CODE (src) == MEM)
741 return "%S1ld%W1 %1,%0";
743 else if (special_symbolref_operand (src, VOIDmode))
744 return "movea %O1(%P1),%Q1,%0";
746 else if (GET_CODE (src) == LABEL_REF
747 || GET_CODE (src) == SYMBOL_REF
748 || GET_CODE (src) == CONST)
750 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
753 else if (GET_CODE (src) == HIGH)
754 return "movhi hi(%1),%.,%0";
756 else if (GET_CODE (src) == LO_SUM)
758 operands[2] = XEXP (src, 0);
759 operands[3] = XEXP (src, 1);
760 return "movea lo(%3),%2,%0";
764 else if (GET_CODE (dst) == MEM)
766 if (REG_P (src))
767 return "%S0st%W0 %1,%0";
769 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
770 return "%S0st%W0 %.,%0";
772 else if (GET_CODE (src) == CONST_DOUBLE
773 && CONST0_RTX (GET_MODE (dst)) == src)
774 return "%S0st%W0 %.,%0";
777 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
778 return "";
782 /* Return appropriate code to load up an 8 byte integer or
783 floating point value */
785 const char *
786 output_move_double (operands)
787 rtx *operands;
789 enum machine_mode mode = GET_MODE (operands[0]);
790 rtx dst = operands[0];
791 rtx src = operands[1];
793 if (register_operand (dst, mode)
794 && register_operand (src, mode))
796 if (REGNO (src) + 1 == REGNO (dst))
797 return "mov %R1,%R0\n\tmov %1,%0";
798 else
799 return "mov %1,%0\n\tmov %R1,%R0";
802 /* Storing 0 */
803 if (GET_CODE (dst) == MEM
804 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
805 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
806 return "st.w %.,%0\n\tst.w %.,%R0";
808 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
810 HOST_WIDE_INT high_low[2];
811 int i;
812 rtx xop[10];
814 if (GET_CODE (src) == CONST_DOUBLE)
815 const_double_split (src, &high_low[1], &high_low[0]);
816 else
818 high_low[0] = INTVAL (src);
819 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
822 for (i = 0; i < 2; i++)
824 xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
825 xop[1] = GEN_INT (high_low[i]);
826 output_asm_insn (output_move_single (xop), xop);
829 return "";
832 if (GET_CODE (src) == MEM)
834 int ptrreg = -1;
835 int dreg = REGNO (dst);
836 rtx inside = XEXP (src, 0);
838 if (GET_CODE (inside) == REG)
839 ptrreg = REGNO (inside);
840 else if (GET_CODE (inside) == SUBREG)
841 ptrreg = subreg_regno (inside);
842 else if (GET_CODE (inside) == PLUS)
843 ptrreg = REGNO (XEXP (inside, 0));
844 else if (GET_CODE (inside) == LO_SUM)
845 ptrreg = REGNO (XEXP (inside, 0));
847 if (dreg == ptrreg)
848 return "ld.w %R1,%R0\n\tld.w %1,%0";
851 if (GET_CODE (src) == MEM)
852 return "ld.w %1,%0\n\tld.w %R1,%R0";
854 if (GET_CODE (dst) == MEM)
855 return "st.w %1,%0\n\tst.w %R1,%R0";
857 return "mov %1,%0\n\tmov %R1,%R0";
861 /* Return maximum offset supported for a short EP memory reference of mode
862 MODE and signedness UNSIGNEDP. */
864 static int
865 ep_memory_offset (mode, unsignedp)
866 enum machine_mode mode;
867 int ATTRIBUTE_UNUSED unsignedp;
869 int max_offset = 0;
871 switch (mode)
873 case QImode:
874 max_offset = (1 << 7);
875 break;
877 case HImode:
878 max_offset = (1 << 8);
879 break;
881 case SImode:
882 case SFmode:
883 max_offset = (1 << 8);
884 break;
886 default:
887 break;
890 return max_offset;
893 /* Return true if OP is a valid short EP memory reference */
896 ep_memory_operand (op, mode, unsigned_load)
897 rtx op;
898 enum machine_mode mode;
899 int unsigned_load;
901 rtx addr, op0, op1;
902 int max_offset;
903 int mask;
905 if (GET_CODE (op) != MEM)
906 return FALSE;
908 max_offset = ep_memory_offset (mode, unsigned_load);
910 mask = GET_MODE_SIZE (mode) - 1;
912 addr = XEXP (op, 0);
913 if (GET_CODE (addr) == CONST)
914 addr = XEXP (addr, 0);
916 switch (GET_CODE (addr))
918 default:
919 break;
921 case SYMBOL_REF:
922 return TDA_NAME_P (XSTR (addr, 0));
924 case REG:
925 return REGNO (addr) == EP_REGNUM;
927 case PLUS:
928 op0 = XEXP (addr, 0);
929 op1 = XEXP (addr, 1);
930 if (GET_CODE (op1) == CONST_INT
931 && INTVAL (op1) < max_offset
932 && INTVAL (op1) >= 0
933 && (INTVAL (op1) & mask) == 0)
935 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
936 return TRUE;
938 if (GET_CODE (op0) == SYMBOL_REF && TDA_NAME_P (XSTR (op0, 0)))
939 return TRUE;
941 break;
944 return FALSE;
947 /* Return true if OP is either a register or 0 */
950 reg_or_0_operand (op, mode)
951 rtx op;
952 enum machine_mode mode;
954 if (GET_CODE (op) == CONST_INT)
955 return INTVAL (op) == 0;
957 else if (GET_CODE (op) == CONST_DOUBLE)
958 return CONST_DOUBLE_OK_FOR_G (op);
960 else
961 return register_operand (op, mode);
964 /* Return true if OP is either a register or a signed five bit integer */
967 reg_or_int5_operand (op, mode)
968 rtx op;
969 enum machine_mode mode;
971 if (GET_CODE (op) == CONST_INT)
972 return CONST_OK_FOR_J (INTVAL (op));
974 else
975 return register_operand (op, mode);
978 /* Return true if OP is a valid call operand. */
981 call_address_operand (op, mode)
982 rtx op;
983 enum machine_mode ATTRIBUTE_UNUSED mode;
985 /* Only registers are valid call operands if TARGET_LONG_CALLS. */
986 if (TARGET_LONG_CALLS)
987 return GET_CODE (op) == REG;
988 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
992 special_symbolref_operand (op, mode)
993 rtx op;
994 enum machine_mode ATTRIBUTE_UNUSED mode;
996 if (GET_CODE (op) == SYMBOL_REF)
997 return ENCODED_NAME_P (XSTR (op, 0));
999 else if (GET_CODE (op) == CONST)
1000 return (GET_CODE (XEXP (op, 0)) == PLUS
1001 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
1002 && ENCODED_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0))
1003 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
1004 && CONST_OK_FOR_K (INTVAL (XEXP (XEXP (op, 0), 1))));
1006 return FALSE;
1010 movsi_source_operand (op, mode)
1011 rtx op;
1012 enum machine_mode mode;
1014 /* Some constants, as well as symbolic operands
1015 must be done with HIGH & LO_SUM patterns. */
1016 if (CONSTANT_P (op)
1017 && GET_CODE (op) != HIGH
1018 && GET_CODE (op) != CONSTANT_P_RTX
1019 && !(GET_CODE (op) == CONST_INT
1020 && (CONST_OK_FOR_J (INTVAL (op))
1021 || CONST_OK_FOR_K (INTVAL (op))
1022 || CONST_OK_FOR_L (INTVAL (op)))))
1023 return special_symbolref_operand (op, mode);
1024 else
1025 return general_operand (op, mode);
1029 power_of_two_operand (op, mode)
1030 rtx op;
1031 enum machine_mode ATTRIBUTE_UNUSED mode;
1033 if (GET_CODE (op) != CONST_INT)
1034 return 0;
1036 if (exact_log2 (INTVAL (op)) == -1)
1037 return 0;
1038 return 1;
1042 not_power_of_two_operand (op, mode)
1043 rtx op;
1044 enum machine_mode mode;
1046 unsigned int mask;
1048 if (mode == QImode)
1049 mask = 0xff;
1050 else if (mode == HImode)
1051 mask = 0xffff;
1052 else if (mode == SImode)
1053 mask = 0xffffffff;
1054 else
1055 return 0;
1057 if (GET_CODE (op) != CONST_INT)
1058 return 0;
1060 if (exact_log2 (~INTVAL (op) & mask) == -1)
1061 return 0;
1062 return 1;
1066 /* Substitute memory references involving a pointer, to use the ep pointer,
1067 taking care to save and preserve the ep. */
1069 static void
1070 substitute_ep_register (first_insn, last_insn, uses, regno, p_r1, p_ep)
1071 rtx first_insn;
1072 rtx last_insn;
1073 int uses;
1074 int regno;
1075 rtx *p_r1;
1076 rtx *p_ep;
1078 rtx reg = gen_rtx_REG (Pmode, regno);
1079 rtx insn;
1081 if (!*p_r1)
1083 regs_ever_live[1] = 1;
1084 *p_r1 = gen_rtx_REG (Pmode, 1);
1085 *p_ep = gen_rtx_REG (Pmode, 30);
1088 if (TARGET_DEBUG)
1089 fprintf (stderr, "\
1090 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1091 2 * (uses - 3), uses, reg_names[regno],
1092 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1093 INSN_UID (first_insn), INSN_UID (last_insn));
1095 if (GET_CODE (first_insn) == NOTE)
1096 first_insn = next_nonnote_insn (first_insn);
1098 last_insn = next_nonnote_insn (last_insn);
1099 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1101 if (GET_CODE (insn) == INSN)
1103 rtx pattern = single_set (insn);
1105 /* Replace the memory references. */
1106 if (pattern)
1108 rtx *p_mem;
1109 /* Memory operands are signed by default. */
1110 int unsignedp = FALSE;
1112 if (GET_CODE (SET_DEST (pattern)) == MEM
1113 && GET_CODE (SET_SRC (pattern)) == MEM)
1114 p_mem = (rtx *)0;
1116 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1117 p_mem = &SET_DEST (pattern);
1119 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1120 p_mem = &SET_SRC (pattern);
1122 else
1123 p_mem = (rtx *)0;
1125 if (p_mem)
1127 rtx addr = XEXP (*p_mem, 0);
1129 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1130 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1132 else if (GET_CODE (addr) == PLUS
1133 && GET_CODE (XEXP (addr, 0)) == REG
1134 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1135 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1136 && ((INTVAL (XEXP (addr, 1)))
1137 < ep_memory_offset (GET_MODE (*p_mem),
1138 unsignedp))
1139 && ((INTVAL (XEXP (addr, 1))) >= 0))
1140 *p_mem = change_address (*p_mem, VOIDmode,
1141 gen_rtx_PLUS (Pmode,
1142 *p_ep,
1143 XEXP (addr, 1)));
1149 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1150 insn = prev_nonnote_insn (first_insn);
1151 if (insn && GET_CODE (insn) == INSN
1152 && GET_CODE (PATTERN (insn)) == SET
1153 && SET_DEST (PATTERN (insn)) == *p_ep
1154 && SET_SRC (PATTERN (insn)) == *p_r1)
1155 delete_insn (insn);
1156 else
1157 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1159 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1160 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1164 /* In rare cases, correct code generation requires extra machine
1165 dependent processing between the second jump optimization pass and
1166 delayed branch scheduling. On those machines, define this macro
1167 as a C statement to act on the code starting at INSN.
1169 On the 850, we use it to implement the -mep mode to copy heavily used
1170 pointers to ep to use the implicit addressing. */
1172 void v850_reorg (start_insn)
1173 rtx start_insn;
1175 struct
1177 int uses;
1178 rtx first_insn;
1179 rtx last_insn;
1181 regs[FIRST_PSEUDO_REGISTER];
1183 int i;
1184 int use_ep = FALSE;
1185 rtx r1 = NULL_RTX;
1186 rtx ep = NULL_RTX;
1187 rtx insn;
1188 rtx pattern;
1190 /* If not ep mode, just return now. */
1191 if (!TARGET_EP)
1192 return;
1194 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1196 regs[i].uses = 0;
1197 regs[i].first_insn = NULL_RTX;
1198 regs[i].last_insn = NULL_RTX;
1201 for (insn = start_insn; insn != NULL_RTX; insn = NEXT_INSN (insn))
1203 switch (GET_CODE (insn))
1205 /* End of basic block */
1206 default:
1207 if (!use_ep)
1209 int max_uses = -1;
1210 int max_regno = -1;
1212 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1214 if (max_uses < regs[i].uses)
1216 max_uses = regs[i].uses;
1217 max_regno = i;
1221 if (max_uses > 3)
1222 substitute_ep_register (regs[max_regno].first_insn,
1223 regs[max_regno].last_insn,
1224 max_uses, max_regno, &r1, &ep);
1227 use_ep = FALSE;
1228 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1230 regs[i].uses = 0;
1231 regs[i].first_insn = NULL_RTX;
1232 regs[i].last_insn = NULL_RTX;
1234 break;
1236 case NOTE:
1237 break;
1239 case INSN:
1240 pattern = single_set (insn);
1242 /* See if there are any memory references we can shorten */
1243 if (pattern)
1245 rtx src = SET_SRC (pattern);
1246 rtx dest = SET_DEST (pattern);
1247 rtx mem;
1248 /* Memory operands are signed by default. */
1249 int unsignedp = FALSE;
1251 /* We might have (SUBREG (MEM)) here, so just get rid of the
1252 subregs to make this code simpler. It is safe to call
1253 alter_subreg any time after reload. */
1254 if (GET_CODE (dest) == SUBREG)
1255 dest = alter_subreg (dest);
1256 if (GET_CODE (src) == SUBREG)
1257 src = alter_subreg (src);
1259 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1260 mem = NULL_RTX;
1262 else if (GET_CODE (dest) == MEM)
1263 mem = dest;
1265 else if (GET_CODE (src) == MEM)
1266 mem = src;
1268 else
1269 mem = NULL_RTX;
1271 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1272 use_ep = TRUE;
1274 else if (!use_ep && mem
1275 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1277 rtx addr = XEXP (mem, 0);
1278 int regno = -1;
1279 int short_p;
1281 if (GET_CODE (addr) == REG)
1283 short_p = TRUE;
1284 regno = REGNO (addr);
1287 else if (GET_CODE (addr) == PLUS
1288 && GET_CODE (XEXP (addr, 0)) == REG
1289 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1290 && ((INTVAL (XEXP (addr, 1)))
1291 < ep_memory_offset (GET_MODE (mem), unsignedp))
1292 && ((INTVAL (XEXP (addr, 1))) >= 0))
1294 short_p = TRUE;
1295 regno = REGNO (XEXP (addr, 0));
1298 else
1299 short_p = FALSE;
1301 if (short_p)
1303 regs[regno].uses++;
1304 regs[regno].last_insn = insn;
1305 if (!regs[regno].first_insn)
1306 regs[regno].first_insn = insn;
1310 /* Loading up a register in the basic block zaps any savings
1311 for the register */
1312 if (GET_CODE (dest) == REG)
1314 enum machine_mode mode = GET_MODE (dest);
1315 int regno;
1316 int endregno;
1318 regno = REGNO (dest);
1319 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1321 if (!use_ep)
1323 /* See if we can use the pointer before this
1324 modification. */
1325 int max_uses = -1;
1326 int max_regno = -1;
1328 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1330 if (max_uses < regs[i].uses)
1332 max_uses = regs[i].uses;
1333 max_regno = i;
1337 if (max_uses > 3
1338 && max_regno >= regno
1339 && max_regno < endregno)
1341 substitute_ep_register (regs[max_regno].first_insn,
1342 regs[max_regno].last_insn,
1343 max_uses, max_regno, &r1,
1344 &ep);
1346 /* Since we made a substitution, zap all remembered
1347 registers. */
1348 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1350 regs[i].uses = 0;
1351 regs[i].first_insn = NULL_RTX;
1352 regs[i].last_insn = NULL_RTX;
1357 for (i = regno; i < endregno; i++)
1359 regs[i].uses = 0;
1360 regs[i].first_insn = NULL_RTX;
1361 regs[i].last_insn = NULL_RTX;
1370 /* # of registers saved by the interrupt handler. */
1371 #define INTERRUPT_FIXED_NUM 4
1373 /* # of bytes for registers saved by the interrupt handler. */
1374 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1376 /* # of registers saved in register parameter area. */
1377 #define INTERRUPT_REGPARM_NUM 4
1378 /* # of words saved for other registers. */
1379 #define INTERRUPT_ALL_SAVE_NUM \
1380 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1382 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1385 compute_register_save_size (p_reg_saved)
1386 long *p_reg_saved;
1388 int size = 0;
1389 int i;
1390 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1391 int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1392 long reg_saved = 0;
1394 /* Count the return pointer if we need to save it. */
1395 if (profile_flag && !call_p)
1396 regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1398 /* Count space for the register saves. */
1399 if (interrupt_handler)
1401 for (i = 0; i <= 31; i++)
1402 switch (i)
1404 default:
1405 if (regs_ever_live[i] || call_p)
1407 size += 4;
1408 reg_saved |= 1L << i;
1410 break;
1412 /* We don't save/restore r0 or the stack pointer */
1413 case 0:
1414 case STACK_POINTER_REGNUM:
1415 break;
1417 /* For registers with fixed use, we save them, set them to the
1418 appropriate value, and then restore them.
1419 These registers are handled specially, so don't list them
1420 on the list of registers to save in the prologue. */
1421 case 1: /* temp used to hold ep */
1422 case 4: /* gp */
1423 case 10: /* temp used to call interrupt save/restore */
1424 case EP_REGNUM: /* ep */
1425 size += 4;
1426 break;
1429 else
1431 /* Find the first register that needs to be saved. */
1432 for (i = 0; i <= 31; i++)
1433 if (regs_ever_live[i] && ((! call_used_regs[i])
1434 || i == LINK_POINTER_REGNUM))
1435 break;
1437 /* If it is possible that an out-of-line helper function might be
1438 used to generate the prologue for the current function, then we
1439 need to cover the possibility that such a helper function will
1440 be used, despite the fact that there might be gaps in the list of
1441 registers that need to be saved. To detect this we note that the
1442 helper functions always push at least register r29 if the link
1443 register is not used, and at least registers r27 - r31 if the
1444 link register is used (and provided that the function is not an
1445 interrupt handler). */
1447 if (TARGET_PROLOG_FUNCTION
1448 && (i == 2 || i >= 20)
1449 && regs_ever_live[LINK_POINTER_REGNUM] ? (i < 28) : (i < 30))
1451 if (i == 2)
1453 size += 4;
1454 reg_saved |= 1L << i;
1456 i = 20;
1459 /* Helper functions save all registers between the starting
1460 register and the last register, regardless of whether they
1461 are actually used by the function or not. */
1462 for (; i <= 29; i++)
1464 size += 4;
1465 reg_saved |= 1L << i;
1468 if (regs_ever_live [LINK_POINTER_REGNUM])
1470 size += 4;
1471 reg_saved |= 1L << LINK_POINTER_REGNUM;
1474 else
1476 for (; i <= 31; i++)
1477 if (regs_ever_live[i] && ((! call_used_regs[i])
1478 || i == LINK_POINTER_REGNUM))
1480 size += 4;
1481 reg_saved |= 1L << i;
1486 if (p_reg_saved)
1487 *p_reg_saved = reg_saved;
1489 return size;
1493 compute_frame_size (size, p_reg_saved)
1494 int size;
1495 long *p_reg_saved;
1497 return (size
1498 + compute_register_save_size (p_reg_saved)
1499 + current_function_outgoing_args_size);
1503 void
1504 expand_prologue ()
1506 unsigned int i;
1507 int offset;
1508 unsigned int size = get_frame_size ();
1509 unsigned int actual_fsize;
1510 unsigned int init_stack_alloc = 0;
1511 rtx save_regs[32];
1512 rtx save_all;
1513 unsigned int num_save;
1514 unsigned int default_stack;
1515 int code;
1516 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1517 long reg_saved = 0;
1519 actual_fsize = compute_frame_size (size, &reg_saved);
1521 /* Save/setup global registers for interrupt functions right now. */
1522 if (interrupt_handler)
1524 emit_insn (gen_save_interrupt ());
1526 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1528 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1529 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1532 /* Save arg registers to the stack if necessary. */
1533 else if (current_function_anonymous_args)
1535 if (TARGET_PROLOG_FUNCTION)
1537 emit_insn (gen_save_r6_r9 ());
1539 else
1541 offset = 0;
1542 for (i = 6; i < 10; i++)
1544 emit_move_insn (gen_rtx_MEM (SImode,
1545 plus_constant (stack_pointer_rtx,
1546 offset)),
1547 gen_rtx_REG (SImode, i));
1548 offset += 4;
1553 /* Identify all of the saved registers. */
1554 num_save = 0;
1555 default_stack = 0;
1556 for (i = 1; i < 31; i++)
1558 if (((1L << i) & reg_saved) != 0)
1559 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1562 /* If the return pointer is saved, the helper functions also allocate
1563 16 bytes of stack for arguments to be saved in. */
1564 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1566 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1567 default_stack = 16;
1570 /* See if we have an insn that allocates stack space and saves the particular
1571 registers we want to. */
1572 save_all = NULL_RTX;
1573 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1575 int alloc_stack = (4 * num_save) + default_stack;
1576 int unalloc_stack = actual_fsize - alloc_stack;
1577 int save_func_len = 4;
1578 int save_normal_len;
1580 if (unalloc_stack)
1581 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1583 /* see if we would have used ep to save the stack */
1584 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1585 save_normal_len = (3 * 2) + (2 * num_save);
1586 else
1587 save_normal_len = 4 * num_save;
1589 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1591 /* Don't bother checking if we don't actually save any space.
1592 This happens for instance if one register is saved and additional
1593 stack space is allocated. */
1594 if (save_func_len < save_normal_len)
1596 save_all = gen_rtx_PARALLEL
1597 (VOIDmode,
1598 rtvec_alloc (num_save + (TARGET_V850 ? 2 : 1)));
1600 XVECEXP (save_all, 0, 0)
1601 = gen_rtx_SET (VOIDmode,
1602 stack_pointer_rtx,
1603 plus_constant (stack_pointer_rtx, -alloc_stack));
1605 if (TARGET_V850)
1607 XVECEXP (save_all, 0, num_save+1)
1608 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1611 offset = - default_stack;
1612 for (i = 0; i < num_save; i++)
1614 XVECEXP (save_all, 0, i+1)
1615 = gen_rtx_SET (VOIDmode,
1616 gen_rtx_MEM (Pmode,
1617 plus_constant (stack_pointer_rtx,
1618 offset)),
1619 save_regs[i]);
1620 offset -= 4;
1623 code = recog (save_all, NULL_RTX, NULL);
1624 if (code >= 0)
1626 rtx insn = emit_insn (save_all);
1627 INSN_CODE (insn) = code;
1628 actual_fsize -= alloc_stack;
1630 if (TARGET_DEBUG)
1631 fprintf (stderr, "\
1632 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1633 save_normal_len - save_func_len,
1634 save_normal_len, save_func_len,
1635 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1637 else
1638 save_all = NULL_RTX;
1642 /* If no prolog save function is available, store the registers the old
1643 fashioned way (one by one). */
1644 if (!save_all)
1646 /* Special case interrupt functions that save all registers for a call. */
1647 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1649 emit_insn (gen_save_all_interrupt ());
1651 else
1653 /* If the stack is too big, allocate it in chunks so we can do the
1654 register saves. We use the register save size so we use the ep
1655 register. */
1656 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1657 init_stack_alloc = compute_register_save_size (NULL);
1658 else
1659 init_stack_alloc = actual_fsize;
1661 /* Save registers at the beginning of the stack frame */
1662 offset = init_stack_alloc - 4;
1664 if (init_stack_alloc)
1665 emit_insn (gen_addsi3 (stack_pointer_rtx,
1666 stack_pointer_rtx,
1667 GEN_INT (-init_stack_alloc)));
1669 /* Save the return pointer first. */
1670 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1672 emit_move_insn (gen_rtx_MEM (SImode,
1673 plus_constant (stack_pointer_rtx,
1674 offset)),
1675 save_regs[--num_save]);
1676 offset -= 4;
1679 for (i = 0; i < num_save; i++)
1681 emit_move_insn (gen_rtx_MEM (SImode,
1682 plus_constant (stack_pointer_rtx,
1683 offset)),
1684 save_regs[i]);
1685 offset -= 4;
1690 /* Allocate the rest of the stack that was not allocated above (either it is
1691 > 32K or we just called a function to save the registers and needed more
1692 stack. */
1693 if (actual_fsize > init_stack_alloc)
1695 int diff = actual_fsize - init_stack_alloc;
1696 if (CONST_OK_FOR_K (diff))
1697 emit_insn (gen_addsi3 (stack_pointer_rtx,
1698 stack_pointer_rtx,
1699 GEN_INT (-diff)));
1700 else
1702 rtx reg = gen_rtx_REG (Pmode, 12);
1703 emit_move_insn (reg, GEN_INT (-diff));
1704 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1708 /* If we need a frame pointer, set it up now. */
1709 if (frame_pointer_needed)
1710 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1714 void
1715 expand_epilogue ()
1717 unsigned int i;
1718 int offset;
1719 unsigned int size = get_frame_size ();
1720 long reg_saved = 0;
1721 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1722 unsigned int init_stack_free = 0;
1723 rtx restore_regs[32];
1724 rtx restore_all;
1725 unsigned int num_restore;
1726 unsigned int default_stack;
1727 int code;
1728 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1730 /* Eliminate the initial stack stored by interrupt functions. */
1731 if (interrupt_handler)
1733 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1734 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1735 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1738 /* Cut off any dynamic stack created. */
1739 if (frame_pointer_needed)
1740 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1742 /* Identify all of the saved registers. */
1743 num_restore = 0;
1744 default_stack = 0;
1745 for (i = 1; i < 31; i++)
1747 if (((1L << i) & reg_saved) != 0)
1748 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1751 /* If the return pointer is saved, the helper functions also allocate
1752 16 bytes of stack for arguments to be saved in. */
1753 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1755 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1756 default_stack = 16;
1759 /* See if we have an insn that restores the particular registers we
1760 want to. */
1761 restore_all = NULL_RTX;
1763 if (TARGET_PROLOG_FUNCTION
1764 && num_restore > 0
1765 && actual_fsize >= default_stack
1766 && !interrupt_handler)
1768 int alloc_stack = (4 * num_restore) + default_stack;
1769 int unalloc_stack = actual_fsize - alloc_stack;
1770 int restore_func_len = 4;
1771 int restore_normal_len;
1773 if (unalloc_stack)
1774 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1776 /* See if we would have used ep to restore the registers. */
1777 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1778 restore_normal_len = (3 * 2) + (2 * num_restore);
1779 else
1780 restore_normal_len = 4 * num_restore;
1782 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1784 /* Don't bother checking if we don't actually save any space. */
1785 if (restore_func_len < restore_normal_len)
1787 restore_all = gen_rtx_PARALLEL (VOIDmode,
1788 rtvec_alloc (num_restore + 2));
1789 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1790 XVECEXP (restore_all, 0, 1)
1791 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1792 gen_rtx_PLUS (Pmode,
1793 stack_pointer_rtx,
1794 GEN_INT (alloc_stack)));
1796 offset = alloc_stack - 4;
1797 for (i = 0; i < num_restore; i++)
1799 XVECEXP (restore_all, 0, i+2)
1800 = gen_rtx_SET (VOIDmode,
1801 restore_regs[i],
1802 gen_rtx_MEM (Pmode,
1803 plus_constant (stack_pointer_rtx,
1804 offset)));
1805 offset -= 4;
1808 code = recog (restore_all, NULL_RTX, NULL);
1810 if (code >= 0)
1812 rtx insn;
1814 actual_fsize -= alloc_stack;
1815 if (actual_fsize)
1817 if (CONST_OK_FOR_K (actual_fsize))
1818 emit_insn (gen_addsi3 (stack_pointer_rtx,
1819 stack_pointer_rtx,
1820 GEN_INT (actual_fsize)));
1821 else
1823 rtx reg = gen_rtx_REG (Pmode, 12);
1824 emit_move_insn (reg, GEN_INT (actual_fsize));
1825 emit_insn (gen_addsi3 (stack_pointer_rtx,
1826 stack_pointer_rtx,
1827 reg));
1831 insn = emit_jump_insn (restore_all);
1832 INSN_CODE (insn) = code;
1834 if (TARGET_DEBUG)
1835 fprintf (stderr, "\
1836 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1837 restore_normal_len - restore_func_len,
1838 restore_normal_len, restore_func_len,
1839 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1841 else
1842 restore_all = NULL_RTX;
1846 /* If no epilog save function is available, restore the registers the
1847 old fashioned way (one by one). */
1848 if (!restore_all)
1850 /* If the stack is large, we need to cut it down in 2 pieces. */
1851 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1852 init_stack_free = 4 * num_restore;
1853 else
1854 init_stack_free = actual_fsize;
1856 /* Deallocate the rest of the stack if it is > 32K. */
1857 if (actual_fsize > init_stack_free)
1859 int diff;
1861 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1863 if (CONST_OK_FOR_K (diff))
1864 emit_insn (gen_addsi3 (stack_pointer_rtx,
1865 stack_pointer_rtx,
1866 GEN_INT (diff)));
1867 else
1869 rtx reg = gen_rtx_REG (Pmode, 12);
1870 emit_move_insn (reg, GEN_INT (diff));
1871 emit_insn (gen_addsi3 (stack_pointer_rtx,
1872 stack_pointer_rtx,
1873 reg));
1877 /* Special case interrupt functions that save all registers
1878 for a call. */
1879 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1881 emit_insn (gen_restore_all_interrupt ());
1883 else
1885 /* Restore registers from the beginning of the stack frame. */
1886 offset = init_stack_free - 4;
1888 /* Restore the return pointer first. */
1889 if (num_restore > 0
1890 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
1892 emit_move_insn (restore_regs[--num_restore],
1893 gen_rtx_MEM (SImode,
1894 plus_constant (stack_pointer_rtx,
1895 offset)));
1896 offset -= 4;
1899 for (i = 0; i < num_restore; i++)
1901 emit_move_insn (restore_regs[i],
1902 gen_rtx_MEM (SImode,
1903 plus_constant (stack_pointer_rtx,
1904 offset)));
1906 emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
1907 offset -= 4;
1910 /* Cut back the remainder of the stack. */
1911 if (init_stack_free)
1912 emit_insn (gen_addsi3 (stack_pointer_rtx,
1913 stack_pointer_rtx,
1914 GEN_INT (init_stack_free)));
1917 /* And return or use reti for interrupt handlers. */
1918 if (interrupt_handler)
1919 emit_jump_insn (gen_restore_interrupt ());
1920 else if (actual_fsize)
1921 emit_jump_insn (gen_return_internal ());
1922 else
1923 emit_jump_insn (gen_return ());
1926 current_function_anonymous_args = 0;
1927 v850_interrupt_cache_p = FALSE;
1928 v850_interrupt_p = FALSE;
1932 /* Update the condition code from the insn. */
1934 void
1935 notice_update_cc (body, insn)
1936 rtx body;
1937 rtx insn;
1939 switch (get_attr_cc (insn))
1941 case CC_NONE:
1942 /* Insn does not affect CC at all. */
1943 break;
1945 case CC_NONE_0HIT:
1946 /* Insn does not change CC, but the 0'th operand has been changed. */
1947 if (cc_status.value1 != 0
1948 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1949 cc_status.value1 = 0;
1950 break;
1952 case CC_SET_ZN:
1953 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1954 V,C is in an unusable state. */
1955 CC_STATUS_INIT;
1956 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1957 cc_status.value1 = recog_data.operand[0];
1958 break;
1960 case CC_SET_ZNV:
1961 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1962 C is in an unusable state. */
1963 CC_STATUS_INIT;
1964 cc_status.flags |= CC_NO_CARRY;
1965 cc_status.value1 = recog_data.operand[0];
1966 break;
1968 case CC_COMPARE:
1969 /* The insn is a compare instruction. */
1970 CC_STATUS_INIT;
1971 cc_status.value1 = SET_SRC (body);
1972 break;
1974 case CC_CLOBBER:
1975 /* Insn doesn't leave CC in a usable state. */
1976 CC_STATUS_INIT;
1977 break;
1981 /* Retrieve the data area that has been chosen for the given decl. */
1983 v850_data_area
1984 v850_get_data_area (decl)
1985 tree decl;
1987 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
1988 return DATA_AREA_SDA;
1990 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
1991 return DATA_AREA_TDA;
1993 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
1994 return DATA_AREA_ZDA;
1996 return DATA_AREA_NORMAL;
1999 /* Store the indicated data area in the decl's attributes. */
2001 static void
2002 v850_set_data_area (decl, data_area)
2003 tree decl;
2004 v850_data_area data_area;
2006 tree name;
2008 switch (data_area)
2010 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2011 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2012 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2013 default:
2014 return;
2017 DECL_ATTRIBUTES (decl) = tree_cons
2018 (name, NULL, DECL_ATTRIBUTES (decl));
2021 const struct attribute_spec v850_attribute_table[] =
2023 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2024 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2025 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2026 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2027 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2028 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2029 { NULL, 0, 0, false, false, false, NULL }
2032 /* Handle an "interrupt" attribute; arguments as in
2033 struct attribute_spec.handler. */
2034 static tree
2035 v850_handle_interrupt_attribute (node, name, args, flags, no_add_attrs)
2036 tree *node;
2037 tree name;
2038 tree args ATTRIBUTE_UNUSED;
2039 int flags ATTRIBUTE_UNUSED;
2040 bool *no_add_attrs;
2042 if (TREE_CODE (*node) != FUNCTION_DECL)
2044 warning ("`%s' attribute only applies to functions",
2045 IDENTIFIER_POINTER (name));
2046 *no_add_attrs = true;
2049 return NULL_TREE;
2052 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2053 struct attribute_spec.handler. */
2054 static tree
2055 v850_handle_data_area_attribute (node, name, args, flags, no_add_attrs)
2056 tree *node;
2057 tree name;
2058 tree args ATTRIBUTE_UNUSED;
2059 int flags ATTRIBUTE_UNUSED;
2060 bool *no_add_attrs;
2062 v850_data_area data_area;
2063 v850_data_area area;
2064 tree decl = *node;
2066 /* Implement data area attribute. */
2067 if (is_attribute_p ("sda", name))
2068 data_area = DATA_AREA_SDA;
2069 else if (is_attribute_p ("tda", name))
2070 data_area = DATA_AREA_TDA;
2071 else if (is_attribute_p ("zda", name))
2072 data_area = DATA_AREA_ZDA;
2073 else
2074 abort ();
2076 switch (TREE_CODE (decl))
2078 case VAR_DECL:
2079 if (current_function_decl != NULL_TREE)
2081 error_with_decl (decl, "\
2082 a data area attribute cannot be specified for local variables");
2083 *no_add_attrs = true;
2086 /* Drop through. */
2088 case FUNCTION_DECL:
2089 area = v850_get_data_area (decl);
2090 if (area != DATA_AREA_NORMAL && data_area != area)
2092 error_with_decl (decl, "\
2093 data area of '%s' conflicts with previous declaration");
2094 *no_add_attrs = true;
2096 break;
2098 default:
2099 break;
2102 return NULL_TREE;
2106 /* Return nonzero if FUNC is an interrupt function as specified
2107 by the "interrupt" attribute. */
2110 v850_interrupt_function_p (func)
2111 tree func;
2113 tree a;
2114 int ret = 0;
2116 if (v850_interrupt_cache_p)
2117 return v850_interrupt_p;
2119 if (TREE_CODE (func) != FUNCTION_DECL)
2120 return 0;
2122 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2123 if (a != NULL_TREE)
2124 ret = 1;
2126 else
2128 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2129 ret = a != NULL_TREE;
2132 /* Its not safe to trust global variables until after function inlining has
2133 been done. */
2134 if (reload_completed | reload_in_progress)
2135 v850_interrupt_p = ret;
2137 return ret;
2141 void
2142 v850_encode_data_area (decl)
2143 tree decl;
2145 const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2146 int len = strlen (str);
2147 char * newstr;
2149 /* Map explict sections into the appropriate attribute */
2150 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2152 if (DECL_SECTION_NAME (decl))
2154 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2156 if (streq (name, ".zdata") || streq (name, ".zbss"))
2157 v850_set_data_area (decl, DATA_AREA_ZDA);
2159 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2160 v850_set_data_area (decl, DATA_AREA_SDA);
2162 else if (streq (name, ".tdata"))
2163 v850_set_data_area (decl, DATA_AREA_TDA);
2166 /* If no attribute, support -m{zda,sda,tda}=n */
2167 else
2169 int size = int_size_in_bytes (TREE_TYPE (decl));
2170 if (size <= 0)
2173 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2174 v850_set_data_area (decl, DATA_AREA_TDA);
2176 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2177 v850_set_data_area (decl, DATA_AREA_SDA);
2179 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2180 v850_set_data_area (decl, DATA_AREA_ZDA);
2183 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2184 return;
2187 newstr = alloca (len + 2);
2189 strcpy (newstr + 1, str);
2191 switch (v850_get_data_area (decl))
2193 case DATA_AREA_ZDA: *newstr = ZDA_NAME_FLAG_CHAR; break;
2194 case DATA_AREA_TDA: *newstr = TDA_NAME_FLAG_CHAR; break;
2195 case DATA_AREA_SDA: *newstr = SDA_NAME_FLAG_CHAR; break;
2196 default: abort ();
2199 XSTR (XEXP (DECL_RTL (decl), 0), 0) = ggc_alloc_string (newstr, len + 2);
2202 /* Return true if the given RTX is a register which can be restored
2203 by a function epilogue. */
2205 register_is_ok_for_epilogue (op, mode)
2206 rtx op;
2207 enum machine_mode ATTRIBUTE_UNUSED mode;
2209 /* The save/restore routines can only cope with registers 2, and 20 - 31 */
2210 return (GET_CODE (op) == REG)
2211 && (((REGNO (op) >= 20) && REGNO (op) <= 31)
2212 || REGNO (op) == 2);
2215 /* Return non-zero if the given RTX is suitable for collapsing into
2216 jump to a function epilogue. */
2218 pattern_is_ok_for_epilogue (op, mode)
2219 rtx op;
2220 enum machine_mode ATTRIBUTE_UNUSED mode;
2222 int count = XVECLEN (op, 0);
2223 int i;
2225 /* If there are no registers to restore then the function epilogue
2226 is not suitable. */
2227 if (count <= 2)
2228 return 0;
2230 /* The pattern matching has already established that we are performing a
2231 function epilogue and that we are popping at least one register. We must
2232 now check the remaining entries in the vector to make sure that they are
2233 also register pops. There is no good reason why there should ever be
2234 anything else in this vector, but being paranoid always helps...
2236 The test below performs the C equivalent of this machine description
2237 pattern match:
2239 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
2240 (mem:SI (plus:SI (reg:SI 3) (match_operand:SI n "immediate_operand" "i"))))
2243 for (i = 3; i < count; i++)
2245 rtx vector_element = XVECEXP (op, 0, i);
2246 rtx dest;
2247 rtx src;
2248 rtx plus;
2250 if (GET_CODE (vector_element) != SET)
2251 return 0;
2253 dest = SET_DEST (vector_element);
2254 src = SET_SRC (vector_element);
2256 if (GET_CODE (dest) != REG
2257 || GET_MODE (dest) != SImode
2258 || ! register_is_ok_for_epilogue (dest, SImode)
2259 || GET_CODE (src) != MEM
2260 || GET_MODE (src) != SImode)
2261 return 0;
2263 plus = XEXP (src, 0);
2265 if (GET_CODE (plus) != PLUS
2266 || GET_CODE (XEXP (plus, 0)) != REG
2267 || GET_MODE (XEXP (plus, 0)) != SImode
2268 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2269 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2270 return 0;
2273 return 1;
2276 /* Construct a JR instruction to a routine that will perform the equivalent of
2277 the RTL passed in as an argument. This RTL is a function epilogue that
2278 pops registers off the stack and possibly releases some extra stack space
2279 as well. The code has already verified that the RTL matches these
2280 requirements. */
2281 char *
2282 construct_restore_jr (op)
2283 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\n", count);
2296 return NULL;
2299 /* Work out how many bytes to pop off the stack before retrieving
2300 registers. */
2301 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2302 abort ();
2303 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2304 abort ();
2305 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2306 abort ();
2308 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2310 /* Each pop will remove 4 bytes from the stack... */
2311 stack_bytes -= (count - 2) * 4;
2313 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2314 if (stack_bytes != 0 && stack_bytes != 16)
2316 error ("bad amount of stack space removal: %d", stack_bytes);
2317 return NULL;
2320 /* Now compute the bit mask of registers to push. */
2321 mask = 0;
2322 for (i = 2; i < count; i++)
2324 rtx vector_element = XVECEXP (op, 0, i);
2326 if (GET_CODE (vector_element) != SET)
2327 abort ();
2328 if (GET_CODE (SET_DEST (vector_element)) != REG)
2329 abort ();
2330 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2331 abort ();
2333 mask |= 1 << REGNO (SET_DEST (vector_element));
2336 /* Scan for the first register to pop. */
2337 for (first = 0; first < 32; first++)
2339 if (mask & (1 << first))
2340 break;
2343 if (first >= 32)
2344 abort ();
2346 /* Discover the last register to pop. */
2347 if (mask & (1 << LINK_POINTER_REGNUM))
2349 if (stack_bytes != 16)
2350 abort ();
2352 last = LINK_POINTER_REGNUM;
2354 else
2356 if (stack_bytes != 0)
2357 abort ();
2359 if ((mask & (1 << 29)) == 0)
2360 abort ();
2362 last = 29;
2365 /* Note, it is possible to have gaps in the register mask.
2366 We ignore this here, and generate a JR anyway. We will
2367 be popping more registers than is strictly necessary, but
2368 it does save code space. */
2370 if (TARGET_LONG_CALLS)
2372 char name[40];
2374 if (first == last)
2375 sprintf (name, "__return_%s", reg_names [first]);
2376 else
2377 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2379 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2380 name, name);
2382 else
2384 if (first == last)
2385 sprintf (buff, "jr __return_%s", reg_names [first]);
2386 else
2387 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2390 return buff;
2394 /* Return non-zero if the given RTX is suitable for collapsing into
2395 a jump to a function prologue. */
2397 pattern_is_ok_for_prologue (op, mode)
2398 rtx op;
2399 enum machine_mode ATTRIBUTE_UNUSED mode;
2401 int count = XVECLEN (op, 0);
2402 int i;
2403 rtx vector_element;
2405 /* If there are no registers to save then the function prologue
2406 is not suitable. */
2407 if (count <= 2)
2408 return 0;
2410 /* The pattern matching has already established that we are adjusting the
2411 stack and pushing at least one register. We must now check that the
2412 remaining entries in the vector to make sure that they are also register
2413 pushes, except for the last entry which should be a CLOBBER of r10.
2415 The test below performs the C equivalent of this machine description
2416 pattern match:
2418 (set (mem:SI (plus:SI (reg:SI 3)
2419 (match_operand:SI 2 "immediate_operand" "i")))
2420 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
2424 for (i = 2; i < count - 1; i++)
2426 rtx dest;
2427 rtx src;
2428 rtx plus;
2430 vector_element = XVECEXP (op, 0, i);
2432 if (GET_CODE (vector_element) != SET)
2433 return 0;
2435 dest = SET_DEST (vector_element);
2436 src = SET_SRC (vector_element);
2438 if (GET_CODE (dest) != MEM
2439 || GET_MODE (dest) != SImode
2440 || GET_CODE (src) != REG
2441 || GET_MODE (src) != SImode
2442 || ! register_is_ok_for_epilogue (src, SImode))
2443 return 0;
2445 plus = XEXP (dest, 0);
2447 if ( GET_CODE (plus) != PLUS
2448 || GET_CODE (XEXP (plus, 0)) != REG
2449 || GET_MODE (XEXP (plus, 0)) != SImode
2450 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2451 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2452 return 0;
2454 /* If the register is being pushed somewhere other than the stack
2455 space just acquired by the first operand then abandon this quest.
2456 Note: the test is <= because both values are negative. */
2457 if (INTVAL (XEXP (plus, 1))
2458 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
2460 return 0;
2464 /* Make sure that the last entry in the vector is a clobber. */
2465 vector_element = XVECEXP (op, 0, i);
2467 if (GET_CODE (vector_element) != CLOBBER
2468 || GET_CODE (XEXP (vector_element, 0)) != REG
2469 || REGNO (XEXP (vector_element, 0)) != 10)
2470 return 0;
2472 return 1;
2475 /* Construct a JARL instruction to a routine that will perform the equivalent
2476 of the RTL passed as a parameter. This RTL is a function prologue that
2477 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2478 some stack space as well. The code has already verified that the RTL
2479 matches these requirements. */
2480 char *
2481 construct_save_jarl (op)
2482 rtx op;
2484 int count = XVECLEN (op, 0);
2485 int stack_bytes;
2486 unsigned long int mask;
2487 unsigned long int first;
2488 unsigned long int last;
2489 int i;
2490 static char buff [100]; /* XXX */
2492 if (count <= 2)
2494 error ("bogus JARL construction: %d\n", count);
2495 return NULL;
2498 /* Paranoia. */
2499 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2500 abort ();
2501 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2502 abort ();
2503 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2504 abort ();
2505 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2506 abort ();
2508 /* Work out how many bytes to push onto the stack after storing the
2509 registers. */
2510 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2512 /* Each push will put 4 bytes from the stack... */
2513 stack_bytes += (count - 2) * 4;
2515 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2516 if (stack_bytes != 0 && stack_bytes != -16)
2518 error ("bad amount of stack space removal: %d", stack_bytes);
2519 return NULL;
2522 /* Now compute the bit mask of registers to push. */
2523 mask = 0;
2524 for (i = 1; i < count - 1; i++)
2526 rtx vector_element = XVECEXP (op, 0, i);
2528 if (GET_CODE (vector_element) != SET)
2529 abort ();
2530 if (GET_CODE (SET_SRC (vector_element)) != REG)
2531 abort ();
2532 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2533 abort ();
2535 mask |= 1 << REGNO (SET_SRC (vector_element));
2538 /* Scan for the first register to push. */
2539 for (first = 0; first < 32; first++)
2541 if (mask & (1 << first))
2542 break;
2545 if (first >= 32)
2546 abort ();
2548 /* Discover the last register to push. */
2549 if (mask & (1 << LINK_POINTER_REGNUM))
2551 if (stack_bytes != -16)
2552 abort ();
2554 last = LINK_POINTER_REGNUM;
2556 else
2558 if (stack_bytes != 0)
2559 abort ();
2560 if ((mask & (1 << 29)) == 0)
2561 abort ();
2563 last = 29;
2566 /* Note, it is possible to have gaps in the register mask.
2567 We ignore this here, and generate a JARL anyway. We will
2568 be pushing more registers than is strictly necessary, but
2569 it does save code space. */
2571 if (TARGET_LONG_CALLS)
2573 char name[40];
2575 if (first == last)
2576 sprintf (name, "__save_%s", reg_names [first]);
2577 else
2578 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2580 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2581 name, name);
2583 else
2585 if (first == last)
2586 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2587 else
2588 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2589 reg_names [last]);
2592 return buff;
2595 extern tree last_assemble_variable_decl;
2596 extern int size_directive_output;
2598 /* A version of asm_output_aligned_bss() that copes with the special
2599 data areas of the v850. */
2600 void
2601 v850_output_aligned_bss (file, decl, name, size, align)
2602 FILE * file;
2603 tree decl;
2604 const char * name;
2605 int size;
2606 int align;
2608 ASM_GLOBALIZE_LABEL (file, name);
2610 switch (v850_get_data_area (decl))
2612 case DATA_AREA_ZDA:
2613 zbss_section ();
2614 break;
2616 case DATA_AREA_SDA:
2617 sbss_section ();
2618 break;
2620 case DATA_AREA_TDA:
2621 tdata_section ();
2623 default:
2624 bss_section ();
2625 break;
2628 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2629 #ifdef ASM_DECLARE_OBJECT_NAME
2630 last_assemble_variable_decl = decl;
2631 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2632 #else
2633 /* Standard thing is just output label for the object. */
2634 ASM_OUTPUT_LABEL (file, name);
2635 #endif /* ASM_DECLARE_OBJECT_NAME */
2636 ASM_OUTPUT_SKIP (file, size ? size : 1);
2639 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2640 void
2641 v850_output_common (file, decl, name, size, align)
2642 FILE * file;
2643 tree decl;
2644 const char * name;
2645 int size;
2646 int align;
2648 if (decl == NULL_TREE)
2650 fprintf (file, "%s", COMMON_ASM_OP);
2652 else
2654 switch (v850_get_data_area (decl))
2656 case DATA_AREA_ZDA:
2657 fprintf (file, "%s", ZCOMMON_ASM_OP);
2658 break;
2660 case DATA_AREA_SDA:
2661 fprintf (file, "%s", SCOMMON_ASM_OP);
2662 break;
2664 case DATA_AREA_TDA:
2665 fprintf (file, "%s", TCOMMON_ASM_OP);
2666 break;
2668 default:
2669 fprintf (file, "%s", COMMON_ASM_OP);
2670 break;
2674 assemble_name (file, name);
2675 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2678 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2679 void
2680 v850_output_local (file, decl, name, size, align)
2681 FILE * file;
2682 tree decl;
2683 const char * name;
2684 int size;
2685 int align;
2687 fprintf (file, "%s", LOCAL_ASM_OP);
2688 assemble_name (file, name);
2689 fprintf (file, "\n");
2691 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2694 /* Add data area to the given declaration if a ghs data area pragma is
2695 currently in effect (#pragma ghs startXXX/endXXX). */
2696 static void
2697 v850_insert_attributes (decl, attr_ptr)
2698 tree decl;
2699 tree *attr_ptr ATTRIBUTE_UNUSED;
2701 if (data_area_stack
2702 && data_area_stack->data_area
2703 && current_function_decl == NULL_TREE
2704 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2705 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2706 v850_set_data_area (decl, data_area_stack->data_area);
2708 /* Initialise the default names of the v850 specific sections,
2709 if this has not been done before. */
2711 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2713 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2714 = build_string (sizeof (".sdata")-1, ".sdata");
2716 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2717 = build_string (sizeof (".rosdata")-1, ".rosdata");
2719 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2720 = build_string (sizeof (".tdata")-1, ".tdata");
2722 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2723 = build_string (sizeof (".zdata")-1, ".zdata");
2725 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2726 = build_string (sizeof (".rozdata")-1, ".rozdata");
2729 if (current_function_decl == NULL_TREE
2730 && (TREE_CODE (decl) == VAR_DECL
2731 || TREE_CODE (decl) == CONST_DECL
2732 || TREE_CODE (decl) == FUNCTION_DECL)
2733 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2734 && !DECL_SECTION_NAME (decl))
2736 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2737 tree chosen_section;
2739 if (TREE_CODE (decl) == FUNCTION_DECL)
2740 kind = GHS_SECTION_KIND_TEXT;
2741 else
2743 /* First choose a section kind based on the data area of the decl. */
2744 switch (v850_get_data_area (decl))
2746 default:
2747 abort ();
2749 case DATA_AREA_SDA:
2750 kind = ((TREE_READONLY (decl))
2751 ? GHS_SECTION_KIND_ROSDATA
2752 : GHS_SECTION_KIND_SDATA);
2753 break;
2755 case DATA_AREA_TDA:
2756 kind = GHS_SECTION_KIND_TDATA;
2757 break;
2759 case DATA_AREA_ZDA:
2760 kind = ((TREE_READONLY (decl))
2761 ? GHS_SECTION_KIND_ROZDATA
2762 : GHS_SECTION_KIND_ZDATA);
2763 break;
2765 case DATA_AREA_NORMAL: /* default data area */
2766 if (TREE_READONLY (decl))
2767 kind = GHS_SECTION_KIND_RODATA;
2768 else if (DECL_INITIAL (decl))
2769 kind = GHS_SECTION_KIND_DATA;
2770 else
2771 kind = GHS_SECTION_KIND_BSS;
2775 /* Now, if the section kind has been explicitly renamed,
2776 then attach a section attribute. */
2777 chosen_section = GHS_current_section_names [(int) kind];
2779 /* Otherwise, if this kind of section needs an explicit section
2780 attribute, then also attach one. */
2781 if (chosen_section == NULL)
2782 chosen_section = GHS_default_section_names [(int) kind];
2784 if (chosen_section)
2786 /* Only set the section name if specified by a pragma, because
2787 otherwise it will force those variables to get allocated storage
2788 in this module, rather than by the linker. */
2789 DECL_SECTION_NAME (decl) = chosen_section;
2794 /* Implement `va_arg'. */
2797 v850_va_arg (valist, type)
2798 tree valist, type;
2800 HOST_WIDE_INT size, rsize;
2801 tree addr, incr;
2802 rtx addr_rtx;
2803 int indirect;
2805 /* Round up sizeof(type) to a word. */
2806 size = int_size_in_bytes (type);
2807 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
2808 indirect = 0;
2810 if (size > 8)
2812 size = rsize = UNITS_PER_WORD;
2813 indirect = 1;
2816 addr = save_expr (valist);
2817 incr = fold (build (PLUS_EXPR, ptr_type_node, addr,
2818 build_int_2 (rsize, 0)));
2820 incr = build (MODIFY_EXPR, ptr_type_node, valist, incr);
2821 TREE_SIDE_EFFECTS (incr) = 1;
2822 expand_expr (incr, const0_rtx, VOIDmode, EXPAND_NORMAL);
2824 addr_rtx = expand_expr (addr, NULL, Pmode, EXPAND_NORMAL);
2826 if (indirect)
2828 addr_rtx = force_reg (Pmode, addr_rtx);
2829 addr_rtx = gen_rtx_MEM (Pmode, addr_rtx);
2830 set_mem_alias_set (addr_rtx, get_varargs_alias_set ());
2833 return addr_rtx;
2836 /* Return an RTX indicating where the return address to the
2837 calling function can be found. */
2840 v850_return_addr (count)
2841 int count;
2843 if (count != 0)
2844 return const0_rtx;
2846 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);