* config/v850/v850.h (MULDI3_LIBCALL, UCMPDI2_LIBCALL, CMPDI2_LIBCALL,
[official-gcc.git] / gcc / config / v850 / v850.c
blob86e80e9887ff18c1355db1fa1486111be7afbc2f
1 /* Subroutines for insn-output.c for NEC V850 series
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 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 GNU CC 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 GNU CC; 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 "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 "ggc.h"
40 #include "integrate.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
45 #ifndef streq
46 #define streq(a,b) (strcmp (a, b) == 0)
47 #endif
49 /* Function prototypes for stupid compilers: */
50 static void const_double_split PARAMS ((rtx, HOST_WIDE_INT *, HOST_WIDE_INT *));
51 static int const_costs_int PARAMS ((HOST_WIDE_INT, int));
52 static void substitute_ep_register PARAMS ((rtx, rtx, int, int, rtx *, rtx *));
53 static int ep_memory_offset PARAMS ((enum machine_mode, int));
54 static void v850_set_data_area PARAMS ((tree, v850_data_area));
55 const struct attribute_spec v850_attribute_table[];
56 static tree v850_handle_interrupt_attribute PARAMS ((tree *, tree, tree, int, bool *));
57 static tree v850_handle_data_area_attribute PARAMS ((tree *, tree, tree, int, bool *));
58 static void v850_insert_attributes PARAMS ((tree, tree *));
59 static void v850_select_section PARAMS ((tree, int, unsigned HOST_WIDE_INT));
60 static void v850_encode_data_area PARAMS ((tree));
61 static void v850_encode_section_info PARAMS ((tree, int));
62 static const char *v850_strip_name_encoding PARAMS ((const char *));
64 /* True if the current function has anonymous arguments. */
65 int current_function_anonymous_args;
67 /* Information about the various small memory areas. */
68 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
70 /* name value max physical max */
71 { "tda", (char *)0, 0, 256 },
72 { "sda", (char *)0, 0, 65536 },
73 { "zda", (char *)0, 0, 32768 },
76 /* Names of the various data areas used on the v850. */
77 tree GHS_default_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
78 tree GHS_current_section_names [(int) COUNT_OF_GHS_SECTION_KINDS];
80 /* Track the current data area set by the data area pragma (which
81 can be nested). Tested by check_default_data_area. */
82 data_area_stack_element * data_area_stack = NULL;
84 /* True if we don't need to check any more if the current
85 function is an interrupt handler. */
86 static int v850_interrupt_cache_p = FALSE;
88 /* Whether current function is an interrupt handler. */
89 static int v850_interrupt_p = FALSE;
91 /* Initialize the GCC target structure. */
92 #undef TARGET_ASM_ALIGNED_HI_OP
93 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
95 #undef TARGET_ATTRIBUTE_TABLE
96 #define TARGET_ATTRIBUTE_TABLE v850_attribute_table
98 #undef TARGET_INSERT_ATTRIBUTES
99 #define TARGET_INSERT_ATTRIBUTES v850_insert_attributes
101 #undef TARGET_ASM_SELECT_SECTION
102 #define TARGET_ASM_SELECT_SECTION v850_select_section
104 #undef TARGET_ENCODE_SECTION_INFO
105 #define TARGET_ENCODE_SECTION_INFO v850_encode_section_info
106 #undef TARGET_STRIP_NAME_ENCODING
107 #define TARGET_STRIP_NAME_ENCODING v850_strip_name_encoding
109 struct gcc_target targetm = TARGET_INITIALIZER;
111 /* Sometimes certain combinations of command options do not make
112 sense on a particular target machine. You can define a macro
113 `OVERRIDE_OPTIONS' to take account of this. This macro, if
114 defined, is executed once just after all the command options have
115 been parsed.
117 Don't use this macro to turn on various extra optimizations for
118 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
120 void
121 override_options ()
123 int i;
124 extern int atoi PARAMS ((const char *));
126 /* Parse -m{s,t,z}da=nnn switches */
127 for (i = 0; i < (int)SMALL_MEMORY_max; i++)
129 if (small_memory[i].value)
131 if (!ISDIGIT (*small_memory[i].value))
132 error ("%s=%s is not numeric",
133 small_memory[i].name,
134 small_memory[i].value);
135 else
137 small_memory[i].max = atoi (small_memory[i].value);
138 if (small_memory[i].max > small_memory[i].physical_max)
139 error ("%s=%s is too large",
140 small_memory[i].name,
141 small_memory[i].value);
146 /* Make sure that the US_BIT_SET mask has been correctly initialised. */
147 if ((target_flags & MASK_US_MASK_SET) == 0)
149 target_flags |= MASK_US_MASK_SET;
150 target_flags &= ~MASK_US_BIT_SET;
155 /* Output assembly code for the start of the file. */
157 void
158 asm_file_start (file)
159 FILE *file;
161 output_file_directive (file, main_input_filename);
165 /* Return an RTX to represent where a value with mode MODE will be returned
166 from a function. If the result is 0, the argument is pushed. */
169 function_arg (cum, mode, type, named)
170 CUMULATIVE_ARGS *cum;
171 enum machine_mode mode;
172 tree type;
173 int named;
175 rtx result = 0;
176 int size, align;
178 if (TARGET_GHS && !named)
179 return NULL_RTX;
181 if (mode == BLKmode)
182 size = int_size_in_bytes (type);
183 else
184 size = GET_MODE_SIZE (mode);
186 if (size < 1)
187 return 0;
189 if (type)
190 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
191 else
192 align = size;
194 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
196 if (cum->nbytes > 4 * UNITS_PER_WORD)
197 return 0;
199 if (type == NULL_TREE
200 && cum->nbytes + size > 4 * UNITS_PER_WORD)
201 return 0;
203 switch (cum->nbytes / UNITS_PER_WORD)
205 case 0:
206 result = gen_rtx_REG (mode, 6);
207 break;
208 case 1:
209 result = gen_rtx_REG (mode, 7);
210 break;
211 case 2:
212 result = gen_rtx_REG (mode, 8);
213 break;
214 case 3:
215 result = gen_rtx_REG (mode, 9);
216 break;
217 default:
218 result = 0;
221 return result;
225 /* Return the number of words which must be put into registers
226 for values which are part in registers and part in memory. */
229 function_arg_partial_nregs (cum, mode, type, named)
230 CUMULATIVE_ARGS *cum;
231 enum machine_mode mode;
232 tree type;
233 int named;
235 int size, align;
237 if (TARGET_GHS && !named)
238 return 0;
240 if (mode == BLKmode)
241 size = int_size_in_bytes (type);
242 else
243 size = GET_MODE_SIZE (mode);
245 if (type)
246 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
247 else
248 align = size;
250 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
252 if (cum->nbytes > 4 * UNITS_PER_WORD)
253 return 0;
255 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
256 return 0;
258 if (type == NULL_TREE
259 && cum->nbytes + size > 4 * UNITS_PER_WORD)
260 return 0;
262 return (4 * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
266 /* Return the high and low words of a CONST_DOUBLE */
268 static void
269 const_double_split (x, p_high, p_low)
270 rtx x;
271 HOST_WIDE_INT *p_high;
272 HOST_WIDE_INT *p_low;
274 if (GET_CODE (x) == CONST_DOUBLE)
276 long t[2];
277 REAL_VALUE_TYPE rv;
279 switch (GET_MODE (x))
281 case DFmode:
282 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
283 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
284 *p_high = t[1]; /* since v850 is little endian */
285 *p_low = t[0]; /* high is second word */
286 return;
288 case SFmode:
289 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
290 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
291 *p_low = 0;
292 return;
294 case VOIDmode:
295 case DImode:
296 *p_high = CONST_DOUBLE_HIGH (x);
297 *p_low = CONST_DOUBLE_LOW (x);
298 return;
300 default:
301 break;
305 fatal_insn ("const_double_split got a bad insn:", x);
309 /* Return the cost of the rtx R with code CODE. */
311 static int
312 const_costs_int (value, zero_cost)
313 HOST_WIDE_INT value;
314 int zero_cost;
316 if (CONST_OK_FOR_I (value))
317 return zero_cost;
318 else if (CONST_OK_FOR_J (value))
319 return 1;
320 else if (CONST_OK_FOR_K (value))
321 return 2;
322 else
323 return 4;
327 const_costs (r, c)
328 rtx r;
329 enum rtx_code c;
331 HOST_WIDE_INT high, low;
333 switch (c)
335 case CONST_INT:
336 return const_costs_int (INTVAL (r), 0);
338 case CONST_DOUBLE:
339 const_double_split (r, &high, &low);
340 if (GET_MODE (r) == SFmode)
341 return const_costs_int (high, 1);
342 else
343 return const_costs_int (high, 1) + const_costs_int (low, 1);
345 case SYMBOL_REF:
346 case LABEL_REF:
347 case CONST:
348 return 2;
350 case HIGH:
351 return 1;
353 default:
354 return 4;
359 /* Print operand X using operand code CODE to assembly language output file
360 FILE. */
362 void
363 print_operand (file, x, code)
364 FILE *file;
365 rtx x;
366 int code;
368 HOST_WIDE_INT high, low;
370 switch (code)
372 case 'c':
373 /* We use 'c' operands with symbols for .vtinherit */
374 if (GET_CODE (x) == SYMBOL_REF)
376 output_addr_const(file, x);
377 break;
379 /* fall through */
380 case 'b':
381 case 'B':
382 case 'C':
383 switch ((code == 'B' || code == 'C')
384 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
386 case NE:
387 if (code == 'c' || code == 'C')
388 fprintf (file, "nz");
389 else
390 fprintf (file, "ne");
391 break;
392 case EQ:
393 if (code == 'c' || code == 'C')
394 fprintf (file, "z");
395 else
396 fprintf (file, "e");
397 break;
398 case GE:
399 fprintf (file, "ge");
400 break;
401 case GT:
402 fprintf (file, "gt");
403 break;
404 case LE:
405 fprintf (file, "le");
406 break;
407 case LT:
408 fprintf (file, "lt");
409 break;
410 case GEU:
411 fprintf (file, "nl");
412 break;
413 case GTU:
414 fprintf (file, "h");
415 break;
416 case LEU:
417 fprintf (file, "nh");
418 break;
419 case LTU:
420 fprintf (file, "l");
421 break;
422 default:
423 abort ();
425 break;
426 case 'F': /* high word of CONST_DOUBLE */
427 if (GET_CODE (x) == CONST_INT)
428 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
429 else if (GET_CODE (x) == CONST_DOUBLE)
431 const_double_split (x, &high, &low);
432 fprintf (file, "%ld", (long) high);
434 else
435 abort ();
436 break;
437 case 'G': /* low word of CONST_DOUBLE */
438 if (GET_CODE (x) == CONST_INT)
439 fprintf (file, "%ld", (long) INTVAL (x));
440 else if (GET_CODE (x) == CONST_DOUBLE)
442 const_double_split (x, &high, &low);
443 fprintf (file, "%ld", (long) low);
445 else
446 abort ();
447 break;
448 case 'L':
449 fprintf (file, "%d\n", INTVAL (x) & 0xffff);
450 break;
451 case 'M':
452 fprintf (file, "%d", exact_log2 (INTVAL (x)));
453 break;
454 case 'O':
455 if (special_symbolref_operand (x, VOIDmode))
457 const char *name;
459 if (GET_CODE (x) == SYMBOL_REF)
460 name = XSTR (x, 0);
461 else if (GET_CODE (x) == CONST)
462 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
463 else
464 abort ();
466 if (ZDA_NAME_P (name))
467 fprintf (file, "zdaoff");
468 else if (SDA_NAME_P (name))
469 fprintf (file, "sdaoff");
470 else if (TDA_NAME_P (name))
471 fprintf (file, "tdaoff");
472 else
473 abort ();
475 else
476 abort ();
477 break;
478 case 'P':
479 if (special_symbolref_operand (x, VOIDmode))
480 output_addr_const (file, x);
481 else
482 abort ();
483 break;
484 case 'Q':
485 if (special_symbolref_operand (x, VOIDmode))
487 const char *name;
489 if (GET_CODE (x) == SYMBOL_REF)
490 name = XSTR (x, 0);
491 else if (GET_CODE (x) == CONST)
492 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
493 else
494 abort ();
496 if (ZDA_NAME_P (name))
497 fprintf (file, "r0");
498 else if (SDA_NAME_P (name))
499 fprintf (file, "gp");
500 else if (TDA_NAME_P (name))
501 fprintf (file, "ep");
502 else
503 abort ();
505 else
506 abort ();
507 break;
508 case 'R': /* 2nd word of a double. */
509 switch (GET_CODE (x))
511 case REG:
512 fprintf (file, reg_names[REGNO (x) + 1]);
513 break;
514 case MEM:
515 x = XEXP (adjust_address (x, SImode, 4), 0);
516 print_operand_address (file, x);
517 if (GET_CODE (x) == CONST_INT)
518 fprintf (file, "[r0]");
519 break;
521 default:
522 break;
524 break;
525 case 'S':
527 /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
528 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
529 fputs ("s", file);
531 break;
533 case 'T':
535 /* Like an 'S' operand above, but for unsigned loads only. */
536 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
537 fputs ("s", file);
539 break;
541 case 'W': /* print the instruction suffix */
542 switch (GET_MODE (x))
544 default:
545 abort ();
547 case QImode: fputs (".b", file); break;
548 case HImode: fputs (".h", file); break;
549 case SImode: fputs (".w", file); break;
550 case SFmode: fputs (".w", file); break;
552 break;
553 case '.': /* register r0 */
554 fputs (reg_names[0], file);
555 break;
556 case 'z': /* reg or zero */
557 if (x == const0_rtx)
558 fputs (reg_names[0], file);
559 else if (GET_CODE (x) == REG)
560 fputs (reg_names[REGNO (x)], file);
561 else
562 abort ();
563 break;
564 default:
565 switch (GET_CODE (x))
567 case MEM:
568 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
569 output_address (gen_rtx_PLUS (SImode, gen_rtx (REG, SImode, 0),
570 XEXP (x, 0)));
571 else
572 output_address (XEXP (x, 0));
573 break;
575 case REG:
576 fputs (reg_names[REGNO (x)], file);
577 break;
578 case SUBREG:
579 fputs (reg_names[subreg_regno (x)], file);
580 break;
581 case CONST_INT:
582 case SYMBOL_REF:
583 case CONST:
584 case LABEL_REF:
585 case CODE_LABEL:
586 print_operand_address (file, x);
587 break;
588 default:
589 abort ();
591 break;
597 /* Output assembly language output for the address ADDR to FILE. */
599 void
600 print_operand_address (file, addr)
601 FILE *file;
602 rtx addr;
604 switch (GET_CODE (addr))
606 case REG:
607 fprintf (file, "0[");
608 print_operand (file, addr, 0);
609 fprintf (file, "]");
610 break;
611 case LO_SUM:
612 if (GET_CODE (XEXP (addr, 0)) == REG)
614 /* reg,foo */
615 fprintf (file, "lo(");
616 print_operand (file, XEXP (addr, 1), 0);
617 fprintf (file, ")[");
618 print_operand (file, XEXP (addr, 0), 0);
619 fprintf (file, "]");
621 break;
622 case PLUS:
623 if (GET_CODE (XEXP (addr, 0)) == REG
624 || GET_CODE (XEXP (addr, 0)) == SUBREG)
626 /* reg,foo */
627 print_operand (file, XEXP (addr, 1), 0);
628 fprintf (file, "[");
629 print_operand (file, XEXP (addr, 0), 0);
630 fprintf (file, "]");
632 else
634 print_operand (file, XEXP (addr, 0), 0);
635 fprintf (file, "+");
636 print_operand (file, XEXP (addr, 1), 0);
638 break;
639 case SYMBOL_REF:
640 if (ENCODED_NAME_P (XSTR (addr, 0)))
642 const char *name = XSTR (addr, 0);
643 const char *off_name;
644 const char *reg_name;
646 if (ZDA_NAME_P (name))
648 off_name = "zdaoff";
649 reg_name = "r0";
651 else if (SDA_NAME_P (name))
653 off_name = "sdaoff";
654 reg_name = "gp";
656 else if (TDA_NAME_P (name))
658 off_name = "tdaoff";
659 reg_name = "ep";
661 else
662 abort ();
664 fprintf (file, "%s(", off_name);
665 output_addr_const (file, addr);
666 fprintf (file, ")[%s]", reg_name);
668 else
669 output_addr_const (file, addr);
670 break;
671 case CONST:
672 if (special_symbolref_operand (addr, VOIDmode))
674 const char *name = XSTR (XEXP (XEXP (addr, 0), 0), 0);
675 const char *off_name;
676 const char *reg_name;
678 if (ZDA_NAME_P (name))
680 off_name = "zdaoff";
681 reg_name = "r0";
683 else if (SDA_NAME_P (name))
685 off_name = "sdaoff";
686 reg_name = "gp";
688 else if (TDA_NAME_P (name))
690 off_name = "tdaoff";
691 reg_name = "ep";
693 else
694 abort ();
696 fprintf (file, "%s(", off_name);
697 output_addr_const (file, addr);
698 fprintf (file, ")[%s]", reg_name);
700 else
701 output_addr_const (file, addr);
702 break;
703 default:
704 output_addr_const (file, addr);
705 break;
709 /* When assemble_integer is used to emit the offsets for a switch
710 table it can encounter (TRUNCATE:HI (MINUS:SI (LABEL_REF:SI) (LABEL_REF:SI))).
711 output_addr_const will normally barf at this, but it is OK to omit
712 the truncate and just emit the difference of the two labels. The
713 .hword directive will automatically handle the truncation for us.
715 Returns 1 if rtx was handled, 0 otherwise. */
718 v850_output_addr_const_extra (file, x)
719 FILE * file;
720 rtx x;
722 if (GET_CODE (x) != TRUNCATE)
723 return 0;
725 x = XEXP (x, 0);
727 /* We must also handle the case where the switch table was passed a
728 constant value and so has been collapsed. In this case the first
729 label will have been deleted. In such a case it is OK to emit
730 nothing, since the table will not be used.
731 (cf gcc.c-torture/compile/990801-1.c). */
732 if (GET_CODE (x) == MINUS
733 && GET_CODE (XEXP (x, 0)) == LABEL_REF
734 && GET_CODE (XEXP (XEXP (x, 0), 0)) == CODE_LABEL
735 && INSN_DELETED_P (XEXP (XEXP (x, 0), 0)))
736 return 1;
738 output_addr_const (file, x);
739 return 1;
742 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
743 point value. */
745 const char *
746 output_move_single (operands)
747 rtx *operands;
749 rtx dst = operands[0];
750 rtx src = operands[1];
752 if (REG_P (dst))
754 if (REG_P (src))
755 return "mov %1,%0";
757 else if (GET_CODE (src) == CONST_INT)
759 HOST_WIDE_INT value = INTVAL (src);
761 if (CONST_OK_FOR_J (value)) /* Signed 5 bit immediate. */
762 return "mov %1,%0";
764 else if (CONST_OK_FOR_K (value)) /* Signed 16 bit immediate. */
765 return "movea lo(%1),%.,%0";
767 else if (CONST_OK_FOR_L (value)) /* Upper 16 bits were set. */
768 return "movhi hi(%1),%.,%0";
770 /* A random constant. */
771 else if (TARGET_V850E)
772 return "mov %1,%0";
773 else
774 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
777 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
779 HOST_WIDE_INT high, low;
781 const_double_split (src, &high, &low);
783 if (CONST_OK_FOR_J (high)) /* Signed 5 bit immediate. */
784 return "mov %F1,%0";
786 else if (CONST_OK_FOR_K (high)) /* Signed 16 bit immediate. */
787 return "movea lo(%F1),%.,%0";
789 else if (CONST_OK_FOR_L (high)) /* Upper 16 bits were set. */
790 return "movhi hi(%F1),%.,%0";
792 /* A random constant. */
793 else if (TARGET_V850E)
794 return "mov %F1,%0";
796 else
797 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
800 else if (GET_CODE (src) == MEM)
801 return "%S1ld%W1 %1,%0";
803 else if (special_symbolref_operand (src, VOIDmode))
804 return "movea %O1(%P1),%Q1,%0";
806 else if (GET_CODE (src) == LABEL_REF
807 || GET_CODE (src) == SYMBOL_REF
808 || GET_CODE (src) == CONST)
810 if (TARGET_V850E)
811 return "mov hilo(%1),%0";
812 else
813 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
816 else if (GET_CODE (src) == HIGH)
817 return "movhi hi(%1),%.,%0";
819 else if (GET_CODE (src) == LO_SUM)
821 operands[2] = XEXP (src, 0);
822 operands[3] = XEXP (src, 1);
823 return "movea lo(%3),%2,%0";
827 else if (GET_CODE (dst) == MEM)
829 if (REG_P (src))
830 return "%S0st%W0 %1,%0";
832 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
833 return "%S0st%W0 %.,%0";
835 else if (GET_CODE (src) == CONST_DOUBLE
836 && CONST0_RTX (GET_MODE (dst)) == src)
837 return "%S0st%W0 %.,%0";
840 fatal_insn ("output_move_single:", gen_rtx_SET (VOIDmode, dst, src));
841 return "";
845 /* Return appropriate code to load up an 8 byte integer or
846 floating point value */
848 const char *
849 output_move_double (operands)
850 rtx *operands;
852 enum machine_mode mode = GET_MODE (operands[0]);
853 rtx dst = operands[0];
854 rtx src = operands[1];
856 if (register_operand (dst, mode)
857 && register_operand (src, mode))
859 if (REGNO (src) + 1 == REGNO (dst))
860 return "mov %R1,%R0\n\tmov %1,%0";
861 else
862 return "mov %1,%0\n\tmov %R1,%R0";
865 /* Storing 0 */
866 if (GET_CODE (dst) == MEM
867 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
868 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
869 return "st.w %.,%0\n\tst.w %.,%R0";
871 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
873 HOST_WIDE_INT high_low[2];
874 int i;
875 rtx xop[10];
877 if (GET_CODE (src) == CONST_DOUBLE)
878 const_double_split (src, &high_low[1], &high_low[0]);
879 else
881 high_low[0] = INTVAL (src);
882 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
885 for (i = 0; i < 2; i++)
887 xop[0] = gen_rtx_REG (SImode, REGNO (dst)+i);
888 xop[1] = GEN_INT (high_low[i]);
889 output_asm_insn (output_move_single (xop), xop);
892 return "";
895 if (GET_CODE (src) == MEM)
897 int ptrreg = -1;
898 int dreg = REGNO (dst);
899 rtx inside = XEXP (src, 0);
901 if (GET_CODE (inside) == REG)
902 ptrreg = REGNO (inside);
903 else if (GET_CODE (inside) == SUBREG)
904 ptrreg = subreg_regno (inside);
905 else if (GET_CODE (inside) == PLUS)
906 ptrreg = REGNO (XEXP (inside, 0));
907 else if (GET_CODE (inside) == LO_SUM)
908 ptrreg = REGNO (XEXP (inside, 0));
910 if (dreg == ptrreg)
911 return "ld.w %R1,%R0\n\tld.w %1,%0";
914 if (GET_CODE (src) == MEM)
915 return "ld.w %1,%0\n\tld.w %R1,%R0";
917 if (GET_CODE (dst) == MEM)
918 return "st.w %1,%0\n\tst.w %R1,%R0";
920 return "mov %1,%0\n\tmov %R1,%R0";
924 /* Return maximum offset supported for a short EP memory reference of mode
925 MODE and signedness UNSIGNEDP. */
927 static int
928 ep_memory_offset (mode, unsignedp)
929 enum machine_mode mode;
930 int ATTRIBUTE_UNUSED unsignedp;
932 int max_offset = 0;
934 switch (mode)
936 case QImode:
937 if (TARGET_SMALL_SLD)
938 max_offset = (1 << 4);
939 else if (TARGET_V850E
940 && ( ( unsignedp && ! TARGET_US_BIT_SET)
941 || (! unsignedp && TARGET_US_BIT_SET)))
942 max_offset = (1 << 4);
943 else
944 max_offset = (1 << 7);
945 break;
947 case HImode:
948 if (TARGET_SMALL_SLD)
949 max_offset = (1 << 5);
950 else if (TARGET_V850E
951 && ( ( unsignedp && ! TARGET_US_BIT_SET)
952 || (! unsignedp && TARGET_US_BIT_SET)))
953 max_offset = (1 << 5);
954 else
955 max_offset = (1 << 8);
956 break;
958 case SImode:
959 case SFmode:
960 max_offset = (1 << 8);
961 break;
963 default:
964 break;
967 return max_offset;
970 /* Return true if OP is a valid short EP memory reference */
973 ep_memory_operand (op, mode, unsigned_load)
974 rtx op;
975 enum machine_mode mode;
976 int unsigned_load;
978 rtx addr, op0, op1;
979 int max_offset;
980 int mask;
982 if (GET_CODE (op) != MEM)
983 return FALSE;
985 max_offset = ep_memory_offset (mode, unsigned_load);
987 mask = GET_MODE_SIZE (mode) - 1;
989 addr = XEXP (op, 0);
990 if (GET_CODE (addr) == CONST)
991 addr = XEXP (addr, 0);
993 switch (GET_CODE (addr))
995 default:
996 break;
998 case SYMBOL_REF:
999 return TDA_NAME_P (XSTR (addr, 0));
1001 case REG:
1002 return REGNO (addr) == EP_REGNUM;
1004 case PLUS:
1005 op0 = XEXP (addr, 0);
1006 op1 = XEXP (addr, 1);
1007 if (GET_CODE (op1) == CONST_INT
1008 && INTVAL (op1) < max_offset
1009 && INTVAL (op1) >= 0
1010 && (INTVAL (op1) & mask) == 0)
1012 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
1013 return TRUE;
1015 if (GET_CODE (op0) == SYMBOL_REF && TDA_NAME_P (XSTR (op0, 0)))
1016 return TRUE;
1018 break;
1021 return FALSE;
1024 /* Return true if OP is either a register or 0 */
1027 reg_or_0_operand (op, mode)
1028 rtx op;
1029 enum machine_mode mode;
1031 if (GET_CODE (op) == CONST_INT)
1032 return INTVAL (op) == 0;
1034 else if (GET_CODE (op) == CONST_DOUBLE)
1035 return CONST_DOUBLE_OK_FOR_G (op);
1037 else
1038 return register_operand (op, mode);
1041 /* Return true if OP is either a register or a signed five bit integer */
1044 reg_or_int5_operand (op, mode)
1045 rtx op;
1046 enum machine_mode mode;
1048 if (GET_CODE (op) == CONST_INT)
1049 return CONST_OK_FOR_J (INTVAL (op));
1051 else
1052 return register_operand (op, mode);
1055 /* Return true if OP is either a register or a signed nine bit integer. */
1058 reg_or_int9_operand (op, mode)
1059 rtx op;
1060 enum machine_mode mode;
1062 if (GET_CODE (op) == CONST_INT)
1063 return CONST_OK_FOR_O (INTVAL (op));
1065 return register_operand (op, mode);
1068 /* Return true if OP is either a register or a const integer. */
1071 reg_or_const_operand (op, mode)
1072 rtx op;
1073 enum machine_mode mode;
1075 if (GET_CODE (op) == CONST_INT)
1076 return TRUE;
1078 return register_operand (op, mode);
1081 /* Return true if OP is a valid call operand. */
1084 call_address_operand (op, mode)
1085 rtx op;
1086 enum machine_mode ATTRIBUTE_UNUSED mode;
1088 /* Only registers are valid call operands if TARGET_LONG_CALLS. */
1089 if (TARGET_LONG_CALLS)
1090 return GET_CODE (op) == REG;
1091 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
1095 special_symbolref_operand (op, mode)
1096 rtx op;
1097 enum machine_mode ATTRIBUTE_UNUSED mode;
1099 if (GET_CODE (op) == SYMBOL_REF)
1100 return ENCODED_NAME_P (XSTR (op, 0));
1102 else if (GET_CODE (op) == CONST)
1103 return (GET_CODE (XEXP (op, 0)) == PLUS
1104 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
1105 && ENCODED_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0))
1106 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
1107 && CONST_OK_FOR_K (INTVAL (XEXP (XEXP (op, 0), 1))));
1109 return FALSE;
1113 movsi_source_operand (op, mode)
1114 rtx op;
1115 enum machine_mode mode;
1117 /* Some constants, as well as symbolic operands
1118 must be done with HIGH & LO_SUM patterns. */
1119 if (CONSTANT_P (op)
1120 && GET_CODE (op) != HIGH
1121 && GET_CODE (op) != CONSTANT_P_RTX
1122 && !(GET_CODE (op) == CONST_INT
1123 && (CONST_OK_FOR_J (INTVAL (op))
1124 || CONST_OK_FOR_K (INTVAL (op))
1125 || CONST_OK_FOR_L (INTVAL (op)))))
1126 return special_symbolref_operand (op, mode);
1127 else
1128 return general_operand (op, mode);
1132 power_of_two_operand (op, mode)
1133 rtx op;
1134 enum machine_mode ATTRIBUTE_UNUSED mode;
1136 if (GET_CODE (op) != CONST_INT)
1137 return 0;
1139 if (exact_log2 (INTVAL (op)) == -1)
1140 return 0;
1141 return 1;
1145 not_power_of_two_operand (op, mode)
1146 rtx op;
1147 enum machine_mode mode;
1149 unsigned int mask;
1151 if (mode == QImode)
1152 mask = 0xff;
1153 else if (mode == HImode)
1154 mask = 0xffff;
1155 else if (mode == SImode)
1156 mask = 0xffffffff;
1157 else
1158 return 0;
1160 if (GET_CODE (op) != CONST_INT)
1161 return 0;
1163 if (exact_log2 (~INTVAL (op) & mask) == -1)
1164 return 0;
1165 return 1;
1169 /* Substitute memory references involving a pointer, to use the ep pointer,
1170 taking care to save and preserve the ep. */
1172 static void
1173 substitute_ep_register (first_insn, last_insn, uses, regno, p_r1, p_ep)
1174 rtx first_insn;
1175 rtx last_insn;
1176 int uses;
1177 int regno;
1178 rtx *p_r1;
1179 rtx *p_ep;
1181 rtx reg = gen_rtx_REG (Pmode, regno);
1182 rtx insn;
1184 if (!*p_r1)
1186 regs_ever_live[1] = 1;
1187 *p_r1 = gen_rtx_REG (Pmode, 1);
1188 *p_ep = gen_rtx_REG (Pmode, 30);
1191 if (TARGET_DEBUG)
1192 fprintf (stderr, "\
1193 Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1194 2 * (uses - 3), uses, reg_names[regno],
1195 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1196 INSN_UID (first_insn), INSN_UID (last_insn));
1198 if (GET_CODE (first_insn) == NOTE)
1199 first_insn = next_nonnote_insn (first_insn);
1201 last_insn = next_nonnote_insn (last_insn);
1202 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1204 if (GET_CODE (insn) == INSN)
1206 rtx pattern = single_set (insn);
1208 /* Replace the memory references. */
1209 if (pattern)
1211 rtx *p_mem;
1212 /* Memory operands are signed by default. */
1213 int unsignedp = FALSE;
1215 if (GET_CODE (SET_DEST (pattern)) == MEM
1216 && GET_CODE (SET_SRC (pattern)) == MEM)
1217 p_mem = (rtx *)0;
1219 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1220 p_mem = &SET_DEST (pattern);
1222 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1223 p_mem = &SET_SRC (pattern);
1225 else if (GET_CODE (SET_SRC (pattern)) == SIGN_EXTEND
1226 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1227 p_mem = &XEXP (SET_SRC (pattern), 0);
1229 else if (GET_CODE (SET_SRC (pattern)) == ZERO_EXTEND
1230 && GET_CODE (XEXP (SET_SRC (pattern), 0)) == MEM)
1232 p_mem = &XEXP (SET_SRC (pattern), 0);
1233 unsignedp = TRUE;
1235 else
1236 p_mem = (rtx *)0;
1238 if (p_mem)
1240 rtx addr = XEXP (*p_mem, 0);
1242 if (GET_CODE (addr) == REG && REGNO (addr) == (unsigned) regno)
1243 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1245 else if (GET_CODE (addr) == PLUS
1246 && GET_CODE (XEXP (addr, 0)) == REG
1247 && REGNO (XEXP (addr, 0)) == (unsigned) regno
1248 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1249 && ((INTVAL (XEXP (addr, 1)))
1250 < ep_memory_offset (GET_MODE (*p_mem),
1251 unsignedp))
1252 && ((INTVAL (XEXP (addr, 1))) >= 0))
1253 *p_mem = change_address (*p_mem, VOIDmode,
1254 gen_rtx_PLUS (Pmode,
1255 *p_ep,
1256 XEXP (addr, 1)));
1262 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1263 insn = prev_nonnote_insn (first_insn);
1264 if (insn && GET_CODE (insn) == INSN
1265 && GET_CODE (PATTERN (insn)) == SET
1266 && SET_DEST (PATTERN (insn)) == *p_ep
1267 && SET_SRC (PATTERN (insn)) == *p_r1)
1268 delete_insn (insn);
1269 else
1270 emit_insn_before (gen_rtx_SET (Pmode, *p_r1, *p_ep), first_insn);
1272 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, reg), first_insn);
1273 emit_insn_before (gen_rtx_SET (Pmode, *p_ep, *p_r1), last_insn);
1277 /* In rare cases, correct code generation requires extra machine
1278 dependent processing between the second jump optimization pass and
1279 delayed branch scheduling. On those machines, define this macro
1280 as a C statement to act on the code starting at INSN.
1282 On the 850, we use it to implement the -mep mode to copy heavily used
1283 pointers to ep to use the implicit addressing. */
1285 void v850_reorg (start_insn)
1286 rtx start_insn;
1288 struct
1290 int uses;
1291 rtx first_insn;
1292 rtx last_insn;
1294 regs[FIRST_PSEUDO_REGISTER];
1296 int i;
1297 int use_ep = FALSE;
1298 rtx r1 = NULL_RTX;
1299 rtx ep = NULL_RTX;
1300 rtx insn;
1301 rtx pattern;
1303 /* If not ep mode, just return now. */
1304 if (!TARGET_EP)
1305 return;
1307 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1309 regs[i].uses = 0;
1310 regs[i].first_insn = NULL_RTX;
1311 regs[i].last_insn = NULL_RTX;
1314 for (insn = start_insn; insn != NULL_RTX; insn = NEXT_INSN (insn))
1316 switch (GET_CODE (insn))
1318 /* End of basic block */
1319 default:
1320 if (!use_ep)
1322 int max_uses = -1;
1323 int max_regno = -1;
1325 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1327 if (max_uses < regs[i].uses)
1329 max_uses = regs[i].uses;
1330 max_regno = i;
1334 if (max_uses > 3)
1335 substitute_ep_register (regs[max_regno].first_insn,
1336 regs[max_regno].last_insn,
1337 max_uses, max_regno, &r1, &ep);
1340 use_ep = FALSE;
1341 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1343 regs[i].uses = 0;
1344 regs[i].first_insn = NULL_RTX;
1345 regs[i].last_insn = NULL_RTX;
1347 break;
1349 case NOTE:
1350 break;
1352 case INSN:
1353 pattern = single_set (insn);
1355 /* See if there are any memory references we can shorten */
1356 if (pattern)
1358 rtx src = SET_SRC (pattern);
1359 rtx dest = SET_DEST (pattern);
1360 rtx mem;
1361 /* Memory operands are signed by default. */
1362 int unsignedp = FALSE;
1364 /* We might have (SUBREG (MEM)) here, so just get rid of the
1365 subregs to make this code simpler. */
1366 if (GET_CODE (dest) == SUBREG
1367 && (GET_CODE (SUBREG_REG (dest)) == MEM
1368 || GET_CODE (SUBREG_REG (dest)) == REG))
1369 alter_subreg (&dest);
1370 if (GET_CODE (src) == SUBREG
1371 && (GET_CODE (SUBREG_REG (src)) == MEM
1372 || GET_CODE (SUBREG_REG (src)) == REG))
1373 alter_subreg (&src);
1375 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1376 mem = NULL_RTX;
1378 else if (GET_CODE (dest) == MEM)
1379 mem = dest;
1381 else if (GET_CODE (src) == MEM)
1382 mem = src;
1384 else if (GET_CODE (src) == SIGN_EXTEND
1385 && GET_CODE (XEXP (src, 0)) == MEM)
1386 mem = XEXP (src, 0);
1388 else if (GET_CODE (src) == ZERO_EXTEND
1389 && GET_CODE (XEXP (src, 0)) == MEM)
1391 mem = XEXP (src, 0);
1392 unsignedp = TRUE;
1394 else
1395 mem = NULL_RTX;
1397 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1398 use_ep = TRUE;
1400 else if (!use_ep && mem
1401 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1403 rtx addr = XEXP (mem, 0);
1404 int regno = -1;
1405 int short_p;
1407 if (GET_CODE (addr) == REG)
1409 short_p = TRUE;
1410 regno = REGNO (addr);
1413 else if (GET_CODE (addr) == PLUS
1414 && GET_CODE (XEXP (addr, 0)) == REG
1415 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1416 && ((INTVAL (XEXP (addr, 1)))
1417 < ep_memory_offset (GET_MODE (mem), unsignedp))
1418 && ((INTVAL (XEXP (addr, 1))) >= 0))
1420 short_p = TRUE;
1421 regno = REGNO (XEXP (addr, 0));
1424 else
1425 short_p = FALSE;
1427 if (short_p)
1429 regs[regno].uses++;
1430 regs[regno].last_insn = insn;
1431 if (!regs[regno].first_insn)
1432 regs[regno].first_insn = insn;
1436 /* Loading up a register in the basic block zaps any savings
1437 for the register */
1438 if (GET_CODE (dest) == REG)
1440 enum machine_mode mode = GET_MODE (dest);
1441 int regno;
1442 int endregno;
1444 regno = REGNO (dest);
1445 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1447 if (!use_ep)
1449 /* See if we can use the pointer before this
1450 modification. */
1451 int max_uses = -1;
1452 int max_regno = -1;
1454 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1456 if (max_uses < regs[i].uses)
1458 max_uses = regs[i].uses;
1459 max_regno = i;
1463 if (max_uses > 3
1464 && max_regno >= regno
1465 && max_regno < endregno)
1467 substitute_ep_register (regs[max_regno].first_insn,
1468 regs[max_regno].last_insn,
1469 max_uses, max_regno, &r1,
1470 &ep);
1472 /* Since we made a substitution, zap all remembered
1473 registers. */
1474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1476 regs[i].uses = 0;
1477 regs[i].first_insn = NULL_RTX;
1478 regs[i].last_insn = NULL_RTX;
1483 for (i = regno; i < endregno; i++)
1485 regs[i].uses = 0;
1486 regs[i].first_insn = NULL_RTX;
1487 regs[i].last_insn = NULL_RTX;
1496 /* # of registers saved by the interrupt handler. */
1497 #define INTERRUPT_FIXED_NUM 4
1499 /* # of bytes for registers saved by the interrupt handler. */
1500 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1502 /* # of registers saved in register parameter area. */
1503 #define INTERRUPT_REGPARM_NUM 4
1504 /* # of words saved for other registers. */
1505 #define INTERRUPT_ALL_SAVE_NUM \
1506 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1508 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1511 compute_register_save_size (p_reg_saved)
1512 long *p_reg_saved;
1514 int size = 0;
1515 int i;
1516 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1517 int call_p = regs_ever_live [LINK_POINTER_REGNUM];
1518 long reg_saved = 0;
1520 /* Count the return pointer if we need to save it. */
1521 if (current_function_profile && !call_p)
1522 regs_ever_live [LINK_POINTER_REGNUM] = call_p = 1;
1524 /* Count space for the register saves. */
1525 if (interrupt_handler)
1527 for (i = 0; i <= 31; i++)
1528 switch (i)
1530 default:
1531 if (regs_ever_live[i] || call_p)
1533 size += 4;
1534 reg_saved |= 1L << i;
1536 break;
1538 /* We don't save/restore r0 or the stack pointer */
1539 case 0:
1540 case STACK_POINTER_REGNUM:
1541 break;
1543 /* For registers with fixed use, we save them, set them to the
1544 appropriate value, and then restore them.
1545 These registers are handled specially, so don't list them
1546 on the list of registers to save in the prologue. */
1547 case 1: /* temp used to hold ep */
1548 case 4: /* gp */
1549 case 10: /* temp used to call interrupt save/restore */
1550 case EP_REGNUM: /* ep */
1551 size += 4;
1552 break;
1555 else
1557 /* Find the first register that needs to be saved. */
1558 for (i = 0; i <= 31; i++)
1559 if (regs_ever_live[i] && ((! call_used_regs[i])
1560 || i == LINK_POINTER_REGNUM))
1561 break;
1563 /* If it is possible that an out-of-line helper function might be
1564 used to generate the prologue for the current function, then we
1565 need to cover the possibility that such a helper function will
1566 be used, despite the fact that there might be gaps in the list of
1567 registers that need to be saved. To detect this we note that the
1568 helper functions always push at least register r29 (provided
1569 that the function is not an interrupt handler). */
1571 if (TARGET_PROLOG_FUNCTION
1572 && (i == 2 || ((i >= 20) && (i < 30))))
1574 if (i == 2)
1576 size += 4;
1577 reg_saved |= 1L << i;
1579 i = 20;
1582 /* Helper functions save all registers between the starting
1583 register and the last register, regardless of whether they
1584 are actually used by the function or not. */
1585 for (; i <= 29; i++)
1587 size += 4;
1588 reg_saved |= 1L << i;
1591 if (regs_ever_live [LINK_POINTER_REGNUM])
1593 size += 4;
1594 reg_saved |= 1L << LINK_POINTER_REGNUM;
1597 else
1599 for (; i <= 31; i++)
1600 if (regs_ever_live[i] && ((! call_used_regs[i])
1601 || i == LINK_POINTER_REGNUM))
1603 size += 4;
1604 reg_saved |= 1L << i;
1609 if (p_reg_saved)
1610 *p_reg_saved = reg_saved;
1612 return size;
1616 compute_frame_size (size, p_reg_saved)
1617 int size;
1618 long *p_reg_saved;
1620 return (size
1621 + compute_register_save_size (p_reg_saved)
1622 + current_function_outgoing_args_size);
1626 void
1627 expand_prologue ()
1629 unsigned int i;
1630 int offset;
1631 unsigned int size = get_frame_size ();
1632 unsigned int actual_fsize;
1633 unsigned int init_stack_alloc = 0;
1634 rtx save_regs[32];
1635 rtx save_all;
1636 unsigned int num_save;
1637 unsigned int default_stack;
1638 int code;
1639 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1640 long reg_saved = 0;
1642 actual_fsize = compute_frame_size (size, &reg_saved);
1644 /* Save/setup global registers for interrupt functions right now. */
1645 if (interrupt_handler)
1647 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1648 emit_insn (gen_callt_save_interrupt ());
1649 else
1650 emit_insn (gen_save_interrupt ());
1652 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1654 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1655 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1658 /* Save arg registers to the stack if necessary. */
1659 else if (current_function_anonymous_args)
1661 if (TARGET_PROLOG_FUNCTION)
1663 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1664 emit_insn (gen_save_r6_r9_v850e ());
1665 else
1666 emit_insn (gen_save_r6_r9 ());
1668 else
1670 offset = 0;
1671 for (i = 6; i < 10; i++)
1673 emit_move_insn (gen_rtx_MEM (SImode,
1674 plus_constant (stack_pointer_rtx,
1675 offset)),
1676 gen_rtx_REG (SImode, i));
1677 offset += 4;
1682 /* Identify all of the saved registers. */
1683 num_save = 0;
1684 default_stack = 0;
1685 for (i = 1; i < 31; i++)
1687 if (((1L << i) & reg_saved) != 0)
1688 save_regs[num_save++] = gen_rtx_REG (Pmode, i);
1691 /* If the return pointer is saved, the helper functions also allocate
1692 16 bytes of stack for arguments to be saved in. */
1693 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1695 save_regs[num_save++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1696 default_stack = 16;
1699 /* See if we have an insn that allocates stack space and saves the particular
1700 registers we want to. */
1701 save_all = NULL_RTX;
1702 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1704 int alloc_stack = (4 * num_save) + default_stack;
1705 int unalloc_stack = actual_fsize - alloc_stack;
1706 int save_func_len = 4;
1707 int save_normal_len;
1709 if (unalloc_stack)
1710 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1712 /* see if we would have used ep to save the stack */
1713 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1714 save_normal_len = (3 * 2) + (2 * num_save);
1715 else
1716 save_normal_len = 4 * num_save;
1718 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1720 /* Don't bother checking if we don't actually save any space.
1721 This happens for instance if one register is saved and additional
1722 stack space is allocated. */
1723 if (save_func_len < save_normal_len)
1725 save_all = gen_rtx_PARALLEL
1726 (VOIDmode,
1727 rtvec_alloc (num_save + (TARGET_V850 ? 2 : 1)));
1729 XVECEXP (save_all, 0, 0)
1730 = gen_rtx_SET (VOIDmode,
1731 stack_pointer_rtx,
1732 plus_constant (stack_pointer_rtx, -alloc_stack));
1734 if (TARGET_V850)
1736 XVECEXP (save_all, 0, num_save+1)
1737 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 10));
1740 offset = - default_stack;
1741 for (i = 0; i < num_save; i++)
1743 XVECEXP (save_all, 0, i+1)
1744 = gen_rtx_SET (VOIDmode,
1745 gen_rtx_MEM (Pmode,
1746 plus_constant (stack_pointer_rtx,
1747 offset)),
1748 save_regs[i]);
1749 offset -= 4;
1752 code = recog (save_all, NULL_RTX, NULL);
1753 if (code >= 0)
1755 rtx insn = emit_insn (save_all);
1756 INSN_CODE (insn) = code;
1757 actual_fsize -= alloc_stack;
1759 if (TARGET_DEBUG)
1760 fprintf (stderr, "\
1761 Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1762 save_normal_len - save_func_len,
1763 save_normal_len, save_func_len,
1764 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1766 else
1767 save_all = NULL_RTX;
1771 /* If no prolog save function is available, store the registers the old
1772 fashioned way (one by one). */
1773 if (!save_all)
1775 /* Special case interrupt functions that save all registers for a call. */
1776 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1778 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
1779 emit_insn (gen_callt_save_all_interrupt ());
1780 else
1781 emit_insn (gen_save_all_interrupt ());
1783 else
1785 /* If the stack is too big, allocate it in chunks so we can do the
1786 register saves. We use the register save size so we use the ep
1787 register. */
1788 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1789 init_stack_alloc = compute_register_save_size (NULL);
1790 else
1791 init_stack_alloc = actual_fsize;
1793 /* Save registers at the beginning of the stack frame */
1794 offset = init_stack_alloc - 4;
1796 if (init_stack_alloc)
1797 emit_insn (gen_addsi3 (stack_pointer_rtx,
1798 stack_pointer_rtx,
1799 GEN_INT (-init_stack_alloc)));
1801 /* Save the return pointer first. */
1802 if (num_save > 0 && REGNO (save_regs[num_save-1]) == LINK_POINTER_REGNUM)
1804 emit_move_insn (gen_rtx_MEM (SImode,
1805 plus_constant (stack_pointer_rtx,
1806 offset)),
1807 save_regs[--num_save]);
1808 offset -= 4;
1811 for (i = 0; i < num_save; i++)
1813 emit_move_insn (gen_rtx_MEM (SImode,
1814 plus_constant (stack_pointer_rtx,
1815 offset)),
1816 save_regs[i]);
1817 offset -= 4;
1822 /* Allocate the rest of the stack that was not allocated above (either it is
1823 > 32K or we just called a function to save the registers and needed more
1824 stack. */
1825 if (actual_fsize > init_stack_alloc)
1827 int diff = actual_fsize - init_stack_alloc;
1828 if (CONST_OK_FOR_K (diff))
1829 emit_insn (gen_addsi3 (stack_pointer_rtx,
1830 stack_pointer_rtx,
1831 GEN_INT (-diff)));
1832 else
1834 rtx reg = gen_rtx_REG (Pmode, 12);
1835 emit_move_insn (reg, GEN_INT (-diff));
1836 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1840 /* If we need a frame pointer, set it up now. */
1841 if (frame_pointer_needed)
1842 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1846 void
1847 expand_epilogue ()
1849 unsigned int i;
1850 int offset;
1851 unsigned int size = get_frame_size ();
1852 long reg_saved = 0;
1853 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1854 unsigned int init_stack_free = 0;
1855 rtx restore_regs[32];
1856 rtx restore_all;
1857 unsigned int num_restore;
1858 unsigned int default_stack;
1859 int code;
1860 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1862 /* Eliminate the initial stack stored by interrupt functions. */
1863 if (interrupt_handler)
1865 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1866 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1867 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1870 /* Cut off any dynamic stack created. */
1871 if (frame_pointer_needed)
1872 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1874 /* Identify all of the saved registers. */
1875 num_restore = 0;
1876 default_stack = 0;
1877 for (i = 1; i < 31; i++)
1879 if (((1L << i) & reg_saved) != 0)
1880 restore_regs[num_restore++] = gen_rtx_REG (Pmode, i);
1883 /* If the return pointer is saved, the helper functions also allocate
1884 16 bytes of stack for arguments to be saved in. */
1885 if (((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
1887 restore_regs[num_restore++] = gen_rtx_REG (Pmode, LINK_POINTER_REGNUM);
1888 default_stack = 16;
1891 /* See if we have an insn that restores the particular registers we
1892 want to. */
1893 restore_all = NULL_RTX;
1895 if (TARGET_PROLOG_FUNCTION
1896 && num_restore > 0
1897 && actual_fsize >= default_stack
1898 && !interrupt_handler)
1900 int alloc_stack = (4 * num_restore) + default_stack;
1901 int unalloc_stack = actual_fsize - alloc_stack;
1902 int restore_func_len = 4;
1903 int restore_normal_len;
1905 if (unalloc_stack)
1906 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1908 /* See if we would have used ep to restore the registers. */
1909 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1910 restore_normal_len = (3 * 2) + (2 * num_restore);
1911 else
1912 restore_normal_len = 4 * num_restore;
1914 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1916 /* Don't bother checking if we don't actually save any space. */
1917 if (restore_func_len < restore_normal_len)
1919 restore_all = gen_rtx_PARALLEL (VOIDmode,
1920 rtvec_alloc (num_restore + 2));
1921 XVECEXP (restore_all, 0, 0) = gen_rtx_RETURN (VOIDmode);
1922 XVECEXP (restore_all, 0, 1)
1923 = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
1924 gen_rtx_PLUS (Pmode,
1925 stack_pointer_rtx,
1926 GEN_INT (alloc_stack)));
1928 offset = alloc_stack - 4;
1929 for (i = 0; i < num_restore; i++)
1931 XVECEXP (restore_all, 0, i+2)
1932 = gen_rtx_SET (VOIDmode,
1933 restore_regs[i],
1934 gen_rtx_MEM (Pmode,
1935 plus_constant (stack_pointer_rtx,
1936 offset)));
1937 offset -= 4;
1940 code = recog (restore_all, NULL_RTX, NULL);
1942 if (code >= 0)
1944 rtx insn;
1946 actual_fsize -= alloc_stack;
1947 if (actual_fsize)
1949 if (CONST_OK_FOR_K (actual_fsize))
1950 emit_insn (gen_addsi3 (stack_pointer_rtx,
1951 stack_pointer_rtx,
1952 GEN_INT (actual_fsize)));
1953 else
1955 rtx reg = gen_rtx_REG (Pmode, 12);
1956 emit_move_insn (reg, GEN_INT (actual_fsize));
1957 emit_insn (gen_addsi3 (stack_pointer_rtx,
1958 stack_pointer_rtx,
1959 reg));
1963 insn = emit_jump_insn (restore_all);
1964 INSN_CODE (insn) = code;
1966 if (TARGET_DEBUG)
1967 fprintf (stderr, "\
1968 Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1969 restore_normal_len - restore_func_len,
1970 restore_normal_len, restore_func_len,
1971 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1973 else
1974 restore_all = NULL_RTX;
1978 /* If no epilog save function is available, restore the registers the
1979 old fashioned way (one by one). */
1980 if (!restore_all)
1982 /* If the stack is large, we need to cut it down in 2 pieces. */
1983 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1984 init_stack_free = 4 * num_restore;
1985 else
1986 init_stack_free = actual_fsize;
1988 /* Deallocate the rest of the stack if it is > 32K. */
1989 if (actual_fsize > init_stack_free)
1991 int diff;
1993 diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1995 if (CONST_OK_FOR_K (diff))
1996 emit_insn (gen_addsi3 (stack_pointer_rtx,
1997 stack_pointer_rtx,
1998 GEN_INT (diff)));
1999 else
2001 rtx reg = gen_rtx_REG (Pmode, 12);
2002 emit_move_insn (reg, GEN_INT (diff));
2003 emit_insn (gen_addsi3 (stack_pointer_rtx,
2004 stack_pointer_rtx,
2005 reg));
2009 /* Special case interrupt functions that save all registers
2010 for a call. */
2011 if (interrupt_handler && ((1L << LINK_POINTER_REGNUM) & reg_saved) != 0)
2013 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
2014 emit_insn (gen_callt_restore_all_interrupt ());
2015 else
2016 emit_insn (gen_restore_all_interrupt ());
2018 else
2020 /* Restore registers from the beginning of the stack frame. */
2021 offset = init_stack_free - 4;
2023 /* Restore the return pointer first. */
2024 if (num_restore > 0
2025 && REGNO (restore_regs [num_restore - 1]) == LINK_POINTER_REGNUM)
2027 emit_move_insn (restore_regs[--num_restore],
2028 gen_rtx_MEM (SImode,
2029 plus_constant (stack_pointer_rtx,
2030 offset)));
2031 offset -= 4;
2034 for (i = 0; i < num_restore; i++)
2036 emit_move_insn (restore_regs[i],
2037 gen_rtx_MEM (SImode,
2038 plus_constant (stack_pointer_rtx,
2039 offset)));
2041 emit_insn (gen_rtx_USE (VOIDmode, restore_regs[i]));
2042 offset -= 4;
2045 /* Cut back the remainder of the stack. */
2046 if (init_stack_free)
2047 emit_insn (gen_addsi3 (stack_pointer_rtx,
2048 stack_pointer_rtx,
2049 GEN_INT (init_stack_free)));
2052 /* And return or use reti for interrupt handlers. */
2053 if (interrupt_handler)
2055 if (TARGET_V850E && ! TARGET_DISABLE_CALLT)
2056 emit_insn (gen_callt_return_interrupt ());
2057 else
2058 emit_jump_insn (gen_return_interrupt ());
2060 else if (actual_fsize)
2061 emit_jump_insn (gen_return_internal ());
2062 else
2063 emit_jump_insn (gen_return ());
2066 current_function_anonymous_args = 0;
2067 v850_interrupt_cache_p = FALSE;
2068 v850_interrupt_p = FALSE;
2072 /* Update the condition code from the insn. */
2074 void
2075 notice_update_cc (body, insn)
2076 rtx body;
2077 rtx insn;
2079 switch (get_attr_cc (insn))
2081 case CC_NONE:
2082 /* Insn does not affect CC at all. */
2083 break;
2085 case CC_NONE_0HIT:
2086 /* Insn does not change CC, but the 0'th operand has been changed. */
2087 if (cc_status.value1 != 0
2088 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2089 cc_status.value1 = 0;
2090 break;
2092 case CC_SET_ZN:
2093 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2094 V,C is in an unusable state. */
2095 CC_STATUS_INIT;
2096 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2097 cc_status.value1 = recog_data.operand[0];
2098 break;
2100 case CC_SET_ZNV:
2101 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2102 C is in an unusable state. */
2103 CC_STATUS_INIT;
2104 cc_status.flags |= CC_NO_CARRY;
2105 cc_status.value1 = recog_data.operand[0];
2106 break;
2108 case CC_COMPARE:
2109 /* The insn is a compare instruction. */
2110 CC_STATUS_INIT;
2111 cc_status.value1 = SET_SRC (body);
2112 break;
2114 case CC_CLOBBER:
2115 /* Insn doesn't leave CC in a usable state. */
2116 CC_STATUS_INIT;
2117 break;
2121 /* Retrieve the data area that has been chosen for the given decl. */
2123 v850_data_area
2124 v850_get_data_area (decl)
2125 tree decl;
2127 if (lookup_attribute ("sda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2128 return DATA_AREA_SDA;
2130 if (lookup_attribute ("tda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2131 return DATA_AREA_TDA;
2133 if (lookup_attribute ("zda", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2134 return DATA_AREA_ZDA;
2136 return DATA_AREA_NORMAL;
2139 /* Store the indicated data area in the decl's attributes. */
2141 static void
2142 v850_set_data_area (decl, data_area)
2143 tree decl;
2144 v850_data_area data_area;
2146 tree name;
2148 switch (data_area)
2150 case DATA_AREA_SDA: name = get_identifier ("sda"); break;
2151 case DATA_AREA_TDA: name = get_identifier ("tda"); break;
2152 case DATA_AREA_ZDA: name = get_identifier ("zda"); break;
2153 default:
2154 return;
2157 DECL_ATTRIBUTES (decl) = tree_cons
2158 (name, NULL, DECL_ATTRIBUTES (decl));
2161 const struct attribute_spec v850_attribute_table[] =
2163 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2164 { "interrupt_handler", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2165 { "interrupt", 0, 0, true, false, false, v850_handle_interrupt_attribute },
2166 { "sda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2167 { "tda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2168 { "zda", 0, 0, true, false, false, v850_handle_data_area_attribute },
2169 { NULL, 0, 0, false, false, false, NULL }
2172 /* Handle an "interrupt" attribute; arguments as in
2173 struct attribute_spec.handler. */
2174 static tree
2175 v850_handle_interrupt_attribute (node, name, args, flags, no_add_attrs)
2176 tree *node;
2177 tree name;
2178 tree args ATTRIBUTE_UNUSED;
2179 int flags ATTRIBUTE_UNUSED;
2180 bool *no_add_attrs;
2182 if (TREE_CODE (*node) != FUNCTION_DECL)
2184 warning ("`%s' attribute only applies to functions",
2185 IDENTIFIER_POINTER (name));
2186 *no_add_attrs = true;
2189 return NULL_TREE;
2192 /* Handle a "sda", "tda" or "zda" attribute; arguments as in
2193 struct attribute_spec.handler. */
2194 static tree
2195 v850_handle_data_area_attribute (node, name, args, flags, no_add_attrs)
2196 tree *node;
2197 tree name;
2198 tree args ATTRIBUTE_UNUSED;
2199 int flags ATTRIBUTE_UNUSED;
2200 bool *no_add_attrs;
2202 v850_data_area data_area;
2203 v850_data_area area;
2204 tree decl = *node;
2206 /* Implement data area attribute. */
2207 if (is_attribute_p ("sda", name))
2208 data_area = DATA_AREA_SDA;
2209 else if (is_attribute_p ("tda", name))
2210 data_area = DATA_AREA_TDA;
2211 else if (is_attribute_p ("zda", name))
2212 data_area = DATA_AREA_ZDA;
2213 else
2214 abort ();
2216 switch (TREE_CODE (decl))
2218 case VAR_DECL:
2219 if (current_function_decl != NULL_TREE)
2221 error_with_decl (decl, "\
2222 a data area attribute cannot be specified for local variables");
2223 *no_add_attrs = true;
2226 /* Drop through. */
2228 case FUNCTION_DECL:
2229 area = v850_get_data_area (decl);
2230 if (area != DATA_AREA_NORMAL && data_area != area)
2232 error_with_decl (decl, "\
2233 data area of '%s' conflicts with previous declaration");
2234 *no_add_attrs = true;
2236 break;
2238 default:
2239 break;
2242 return NULL_TREE;
2246 /* Return nonzero if FUNC is an interrupt function as specified
2247 by the "interrupt" attribute. */
2250 v850_interrupt_function_p (func)
2251 tree func;
2253 tree a;
2254 int ret = 0;
2256 if (v850_interrupt_cache_p)
2257 return v850_interrupt_p;
2259 if (TREE_CODE (func) != FUNCTION_DECL)
2260 return 0;
2262 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
2263 if (a != NULL_TREE)
2264 ret = 1;
2266 else
2268 a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
2269 ret = a != NULL_TREE;
2272 /* Its not safe to trust global variables until after function inlining has
2273 been done. */
2274 if (reload_completed | reload_in_progress)
2275 v850_interrupt_p = ret;
2277 return ret;
2281 static void
2282 v850_encode_data_area (decl)
2283 tree decl;
2285 const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2286 int len = strlen (str);
2287 char * newstr;
2289 /* Map explict sections into the appropriate attribute */
2290 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2292 if (DECL_SECTION_NAME (decl))
2294 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
2296 if (streq (name, ".zdata") || streq (name, ".zbss"))
2297 v850_set_data_area (decl, DATA_AREA_ZDA);
2299 else if (streq (name, ".sdata") || streq (name, ".sbss"))
2300 v850_set_data_area (decl, DATA_AREA_SDA);
2302 else if (streq (name, ".tdata"))
2303 v850_set_data_area (decl, DATA_AREA_TDA);
2306 /* If no attribute, support -m{zda,sda,tda}=n */
2307 else
2309 int size = int_size_in_bytes (TREE_TYPE (decl));
2310 if (size <= 0)
2313 else if (size <= small_memory [(int) SMALL_MEMORY_TDA].max)
2314 v850_set_data_area (decl, DATA_AREA_TDA);
2316 else if (size <= small_memory [(int) SMALL_MEMORY_SDA].max)
2317 v850_set_data_area (decl, DATA_AREA_SDA);
2319 else if (size <= small_memory [(int) SMALL_MEMORY_ZDA].max)
2320 v850_set_data_area (decl, DATA_AREA_ZDA);
2323 if (v850_get_data_area (decl) == DATA_AREA_NORMAL)
2324 return;
2327 newstr = alloca (len + 2);
2329 strcpy (newstr + 1, str);
2331 switch (v850_get_data_area (decl))
2333 case DATA_AREA_ZDA: *newstr = ZDA_NAME_FLAG_CHAR; break;
2334 case DATA_AREA_TDA: *newstr = TDA_NAME_FLAG_CHAR; break;
2335 case DATA_AREA_SDA: *newstr = SDA_NAME_FLAG_CHAR; break;
2336 default: abort ();
2339 XSTR (XEXP (DECL_RTL (decl), 0), 0) = ggc_alloc_string (newstr, len + 2);
2342 static void
2343 v850_encode_section_info (decl, first)
2344 tree decl;
2345 int first;
2347 if (first && TREE_CODE (decl) == VAR_DECL
2348 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2349 v850_encode_data_area (decl);
2352 static const char *
2353 v850_strip_name_encoding (str)
2354 const char *str;
2356 return str + (ENCODED_NAME_P (str) || *str == '*');
2359 /* Return true if the given RTX is a register which can be restored
2360 by a function epilogue. */
2362 register_is_ok_for_epilogue (op, mode)
2363 rtx op;
2364 enum machine_mode ATTRIBUTE_UNUSED mode;
2366 /* The save/restore routines can only cope with registers 20 - 31. */
2367 return ((GET_CODE (op) == REG)
2368 && (((REGNO (op) >= 20) && REGNO (op) <= 31)));
2371 /* Return non-zero if the given RTX is suitable for collapsing into
2372 jump to a function epilogue. */
2374 pattern_is_ok_for_epilogue (op, mode)
2375 rtx op;
2376 enum machine_mode ATTRIBUTE_UNUSED mode;
2378 int count = XVECLEN (op, 0);
2379 int i;
2381 /* If there are no registers to restore then the function epilogue
2382 is not suitable. */
2383 if (count <= 2)
2384 return 0;
2386 /* The pattern matching has already established that we are performing a
2387 function epilogue and that we are popping at least one register. We must
2388 now check the remaining entries in the vector to make sure that they are
2389 also register pops. There is no good reason why there should ever be
2390 anything else in this vector, but being paranoid always helps...
2392 The test below performs the C equivalent of this machine description
2393 pattern match:
2395 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
2396 (mem:SI (plus:SI (reg:SI 3) (match_operand:SI n "immediate_operand" "i"))))
2399 for (i = 3; i < count; i++)
2401 rtx vector_element = XVECEXP (op, 0, i);
2402 rtx dest;
2403 rtx src;
2404 rtx plus;
2406 if (GET_CODE (vector_element) != SET)
2407 return 0;
2409 dest = SET_DEST (vector_element);
2410 src = SET_SRC (vector_element);
2412 if (GET_CODE (dest) != REG
2413 || GET_MODE (dest) != SImode
2414 || ! register_is_ok_for_epilogue (dest, SImode)
2415 || GET_CODE (src) != MEM
2416 || GET_MODE (src) != SImode)
2417 return 0;
2419 plus = XEXP (src, 0);
2421 if (GET_CODE (plus) != PLUS
2422 || GET_CODE (XEXP (plus, 0)) != REG
2423 || GET_MODE (XEXP (plus, 0)) != SImode
2424 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2425 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2426 return 0;
2429 return 1;
2432 /* Construct a JR instruction to a routine that will perform the equivalent of
2433 the RTL passed in as an argument. This RTL is a function epilogue that
2434 pops registers off the stack and possibly releases some extra stack space
2435 as well. The code has already verified that the RTL matches these
2436 requirements. */
2437 char *
2438 construct_restore_jr (op)
2439 rtx op;
2441 int count = XVECLEN (op, 0);
2442 int stack_bytes;
2443 unsigned long int mask;
2444 unsigned long int first;
2445 unsigned long int last;
2446 int i;
2447 static char buff [100]; /* XXX */
2449 if (count <= 2)
2451 error ("bogus JR construction: %d\n", count);
2452 return NULL;
2455 /* Work out how many bytes to pop off the stack before retrieving
2456 registers. */
2457 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2458 abort ();
2459 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2460 abort ();
2461 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2462 abort ();
2464 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2466 /* Each pop will remove 4 bytes from the stack... */
2467 stack_bytes -= (count - 2) * 4;
2469 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2470 if (stack_bytes != 0 && stack_bytes != 16)
2472 error ("bad amount of stack space removal: %d", stack_bytes);
2473 return NULL;
2476 /* Now compute the bit mask of registers to push. */
2477 mask = 0;
2478 for (i = 2; i < count; i++)
2480 rtx vector_element = XVECEXP (op, 0, i);
2482 if (GET_CODE (vector_element) != SET)
2483 abort ();
2484 if (GET_CODE (SET_DEST (vector_element)) != REG)
2485 abort ();
2486 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2487 abort ();
2489 mask |= 1 << REGNO (SET_DEST (vector_element));
2492 /* Scan for the first register to pop. */
2493 for (first = 0; first < 32; first++)
2495 if (mask & (1 << first))
2496 break;
2499 if (first >= 32)
2500 abort ();
2502 /* Discover the last register to pop. */
2503 if (mask & (1 << LINK_POINTER_REGNUM))
2505 if (stack_bytes != 16)
2506 abort ();
2508 last = LINK_POINTER_REGNUM;
2510 else
2512 if (stack_bytes != 0)
2513 abort ();
2515 if ((mask & (1 << 29)) == 0)
2516 abort ();
2518 last = 29;
2521 /* Note, it is possible to have gaps in the register mask.
2522 We ignore this here, and generate a JR anyway. We will
2523 be popping more registers than is strictly necessary, but
2524 it does save code space. */
2526 if (TARGET_LONG_CALLS)
2528 char name[40];
2530 if (first == last)
2531 sprintf (name, "__return_%s", reg_names [first]);
2532 else
2533 sprintf (name, "__return_%s_%s", reg_names [first], reg_names [last]);
2535 sprintf (buff, "movhi hi(%s), r0, r6\n\tmovea lo(%s), r6, r6\n\tjmp r6",
2536 name, name);
2538 else
2540 if (first == last)
2541 sprintf (buff, "jr __return_%s", reg_names [first]);
2542 else
2543 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2546 return buff;
2550 /* Return non-zero if the given RTX is suitable for collapsing into
2551 a jump to a function prologue. */
2553 pattern_is_ok_for_prologue (op, mode)
2554 rtx op;
2555 enum machine_mode ATTRIBUTE_UNUSED mode;
2557 int count = XVECLEN (op, 0);
2558 int i;
2559 rtx vector_element;
2561 /* If there are no registers to save then the function prologue
2562 is not suitable. */
2563 if (count <= 2)
2564 return 0;
2566 /* The pattern matching has already established that we are adjusting the
2567 stack and pushing at least one register. We must now check that the
2568 remaining entries in the vector to make sure that they are also register
2569 pushes, except for the last entry which should be a CLOBBER of r10.
2571 The test below performs the C equivalent of this machine description
2572 pattern match:
2574 (set (mem:SI (plus:SI (reg:SI 3)
2575 (match_operand:SI 2 "immediate_operand" "i")))
2576 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
2580 for (i = 2; i < count - 1; i++)
2582 rtx dest;
2583 rtx src;
2584 rtx plus;
2586 vector_element = XVECEXP (op, 0, i);
2588 if (GET_CODE (vector_element) != SET)
2589 return 0;
2591 dest = SET_DEST (vector_element);
2592 src = SET_SRC (vector_element);
2594 if (GET_CODE (dest) != MEM
2595 || GET_MODE (dest) != SImode
2596 || GET_CODE (src) != REG
2597 || GET_MODE (src) != SImode
2598 || ! register_is_ok_for_epilogue (src, SImode))
2599 return 0;
2601 plus = XEXP (dest, 0);
2603 if ( GET_CODE (plus) != PLUS
2604 || GET_CODE (XEXP (plus, 0)) != REG
2605 || GET_MODE (XEXP (plus, 0)) != SImode
2606 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2607 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2608 return 0;
2610 /* If the register is being pushed somewhere other than the stack
2611 space just acquired by the first operand then abandon this quest.
2612 Note: the test is <= because both values are negative. */
2613 if (INTVAL (XEXP (plus, 1))
2614 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
2616 return 0;
2620 /* Make sure that the last entry in the vector is a clobber. */
2621 vector_element = XVECEXP (op, 0, i);
2623 if (GET_CODE (vector_element) != CLOBBER
2624 || GET_CODE (XEXP (vector_element, 0)) != REG
2625 || REGNO (XEXP (vector_element, 0)) != 10)
2626 return 0;
2628 return 1;
2631 /* Construct a JARL instruction to a routine that will perform the equivalent
2632 of the RTL passed as a parameter. This RTL is a function prologue that
2633 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2634 some stack space as well. The code has already verified that the RTL
2635 matches these requirements. */
2636 char *
2637 construct_save_jarl (op)
2638 rtx op;
2640 int count = XVECLEN (op, 0);
2641 int stack_bytes;
2642 unsigned long int mask;
2643 unsigned long int first;
2644 unsigned long int last;
2645 int i;
2646 static char buff [100]; /* XXX */
2648 if (count <= 2)
2650 error ("bogus JARL construction: %d\n", count);
2651 return NULL;
2654 /* Paranoia. */
2655 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2656 abort ();
2657 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2658 abort ();
2659 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2660 abort ();
2661 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2662 abort ();
2664 /* Work out how many bytes to push onto the stack after storing the
2665 registers. */
2666 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2668 /* Each push will put 4 bytes from the stack... */
2669 stack_bytes += (count - 2) * 4;
2671 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2672 if (stack_bytes != 0 && stack_bytes != -16)
2674 error ("bad amount of stack space removal: %d", stack_bytes);
2675 return NULL;
2678 /* Now compute the bit mask of registers to push. */
2679 mask = 0;
2680 for (i = 1; i < count - 1; i++)
2682 rtx vector_element = XVECEXP (op, 0, i);
2684 if (GET_CODE (vector_element) != SET)
2685 abort ();
2686 if (GET_CODE (SET_SRC (vector_element)) != REG)
2687 abort ();
2688 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2689 abort ();
2691 mask |= 1 << REGNO (SET_SRC (vector_element));
2694 /* Scan for the first register to push. */
2695 for (first = 0; first < 32; first++)
2697 if (mask & (1 << first))
2698 break;
2701 if (first >= 32)
2702 abort ();
2704 /* Discover the last register to push. */
2705 if (mask & (1 << LINK_POINTER_REGNUM))
2707 if (stack_bytes != -16)
2708 abort ();
2710 last = LINK_POINTER_REGNUM;
2712 else
2714 if (stack_bytes != 0)
2715 abort ();
2716 if ((mask & (1 << 29)) == 0)
2717 abort ();
2719 last = 29;
2722 /* Note, it is possible to have gaps in the register mask.
2723 We ignore this here, and generate a JARL anyway. We will
2724 be pushing more registers than is strictly necessary, but
2725 it does save code space. */
2727 if (TARGET_LONG_CALLS)
2729 char name[40];
2731 if (first == last)
2732 sprintf (name, "__save_%s", reg_names [first]);
2733 else
2734 sprintf (name, "__save_%s_%s", reg_names [first], reg_names [last]);
2736 sprintf (buff, "movhi hi(%s), r0, r11\n\tmovea lo(%s), r11, r11\n\tjarl .+4, r10\n\tadd 4, r10\n\tjmp r11",
2737 name, name);
2739 else
2741 if (first == last)
2742 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2743 else
2744 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2745 reg_names [last]);
2748 return buff;
2751 extern tree last_assemble_variable_decl;
2752 extern int size_directive_output;
2754 /* A version of asm_output_aligned_bss() that copes with the special
2755 data areas of the v850. */
2756 void
2757 v850_output_aligned_bss (file, decl, name, size, align)
2758 FILE * file;
2759 tree decl;
2760 const char * name;
2761 int size;
2762 int align;
2764 (*targetm.asm_out.globalize_label) (file, name);
2766 switch (v850_get_data_area (decl))
2768 case DATA_AREA_ZDA:
2769 zbss_section ();
2770 break;
2772 case DATA_AREA_SDA:
2773 sbss_section ();
2774 break;
2776 case DATA_AREA_TDA:
2777 tdata_section ();
2779 default:
2780 bss_section ();
2781 break;
2784 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2785 #ifdef ASM_DECLARE_OBJECT_NAME
2786 last_assemble_variable_decl = decl;
2787 ASM_DECLARE_OBJECT_NAME (file, name, decl);
2788 #else
2789 /* Standard thing is just output label for the object. */
2790 ASM_OUTPUT_LABEL (file, name);
2791 #endif /* ASM_DECLARE_OBJECT_NAME */
2792 ASM_OUTPUT_SKIP (file, size ? size : 1);
2795 /* Called via the macro ASM_OUTPUT_DECL_COMMON */
2796 void
2797 v850_output_common (file, decl, name, size, align)
2798 FILE * file;
2799 tree decl;
2800 const char * name;
2801 int size;
2802 int align;
2804 if (decl == NULL_TREE)
2806 fprintf (file, "%s", COMMON_ASM_OP);
2808 else
2810 switch (v850_get_data_area (decl))
2812 case DATA_AREA_ZDA:
2813 fprintf (file, "%s", ZCOMMON_ASM_OP);
2814 break;
2816 case DATA_AREA_SDA:
2817 fprintf (file, "%s", SCOMMON_ASM_OP);
2818 break;
2820 case DATA_AREA_TDA:
2821 fprintf (file, "%s", TCOMMON_ASM_OP);
2822 break;
2824 default:
2825 fprintf (file, "%s", COMMON_ASM_OP);
2826 break;
2830 assemble_name (file, name);
2831 fprintf (file, ",%u,%u\n", size, align / BITS_PER_UNIT);
2834 /* Called via the macro ASM_OUTPUT_DECL_LOCAL */
2835 void
2836 v850_output_local (file, decl, name, size, align)
2837 FILE * file;
2838 tree decl;
2839 const char * name;
2840 int size;
2841 int align;
2843 fprintf (file, "%s", LOCAL_ASM_OP);
2844 assemble_name (file, name);
2845 fprintf (file, "\n");
2847 ASM_OUTPUT_ALIGNED_DECL_COMMON (file, decl, name, size, align);
2850 /* Add data area to the given declaration if a ghs data area pragma is
2851 currently in effect (#pragma ghs startXXX/endXXX). */
2852 static void
2853 v850_insert_attributes (decl, attr_ptr)
2854 tree decl;
2855 tree *attr_ptr ATTRIBUTE_UNUSED;
2857 if (data_area_stack
2858 && data_area_stack->data_area
2859 && current_function_decl == NULL_TREE
2860 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
2861 && v850_get_data_area (decl) == DATA_AREA_NORMAL)
2862 v850_set_data_area (decl, data_area_stack->data_area);
2864 /* Initialise the default names of the v850 specific sections,
2865 if this has not been done before. */
2867 if (GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA] == NULL)
2869 GHS_default_section_names [(int) GHS_SECTION_KIND_SDATA]
2870 = build_string (sizeof (".sdata")-1, ".sdata");
2872 GHS_default_section_names [(int) GHS_SECTION_KIND_ROSDATA]
2873 = build_string (sizeof (".rosdata")-1, ".rosdata");
2875 GHS_default_section_names [(int) GHS_SECTION_KIND_TDATA]
2876 = build_string (sizeof (".tdata")-1, ".tdata");
2878 GHS_default_section_names [(int) GHS_SECTION_KIND_ZDATA]
2879 = build_string (sizeof (".zdata")-1, ".zdata");
2881 GHS_default_section_names [(int) GHS_SECTION_KIND_ROZDATA]
2882 = build_string (sizeof (".rozdata")-1, ".rozdata");
2885 if (current_function_decl == NULL_TREE
2886 && (TREE_CODE (decl) == VAR_DECL
2887 || TREE_CODE (decl) == CONST_DECL
2888 || TREE_CODE (decl) == FUNCTION_DECL)
2889 && (!DECL_EXTERNAL (decl) || DECL_INITIAL (decl))
2890 && !DECL_SECTION_NAME (decl))
2892 enum GHS_section_kind kind = GHS_SECTION_KIND_DEFAULT;
2893 tree chosen_section;
2895 if (TREE_CODE (decl) == FUNCTION_DECL)
2896 kind = GHS_SECTION_KIND_TEXT;
2897 else
2899 /* First choose a section kind based on the data area of the decl. */
2900 switch (v850_get_data_area (decl))
2902 default:
2903 abort ();
2905 case DATA_AREA_SDA:
2906 kind = ((TREE_READONLY (decl))
2907 ? GHS_SECTION_KIND_ROSDATA
2908 : GHS_SECTION_KIND_SDATA);
2909 break;
2911 case DATA_AREA_TDA:
2912 kind = GHS_SECTION_KIND_TDATA;
2913 break;
2915 case DATA_AREA_ZDA:
2916 kind = ((TREE_READONLY (decl))
2917 ? GHS_SECTION_KIND_ROZDATA
2918 : GHS_SECTION_KIND_ZDATA);
2919 break;
2921 case DATA_AREA_NORMAL: /* default data area */
2922 if (TREE_READONLY (decl))
2923 kind = GHS_SECTION_KIND_RODATA;
2924 else if (DECL_INITIAL (decl))
2925 kind = GHS_SECTION_KIND_DATA;
2926 else
2927 kind = GHS_SECTION_KIND_BSS;
2931 /* Now, if the section kind has been explicitly renamed,
2932 then attach a section attribute. */
2933 chosen_section = GHS_current_section_names [(int) kind];
2935 /* Otherwise, if this kind of section needs an explicit section
2936 attribute, then also attach one. */
2937 if (chosen_section == NULL)
2938 chosen_section = GHS_default_section_names [(int) kind];
2940 if (chosen_section)
2942 /* Only set the section name if specified by a pragma, because
2943 otherwise it will force those variables to get allocated storage
2944 in this module, rather than by the linker. */
2945 DECL_SECTION_NAME (decl) = chosen_section;
2950 /* Return non-zero if the given RTX is suitable
2951 for collapsing into a DISPOSE instruction. */
2954 pattern_is_ok_for_dispose (op, mode)
2955 rtx op;
2956 enum machine_mode mode ATTRIBUTE_UNUSED;
2958 int count = XVECLEN (op, 0);
2959 int i;
2961 /* If there are no registers to restore then
2962 the dispose instruction is not suitable. */
2963 if (count <= 2)
2964 return 0;
2966 /* The pattern matching has already established that we are performing a
2967 function epilogue and that we are popping at least one register. We must
2968 now check the remaining entries in the vector to make sure that they are
2969 also register pops. There is no good reason why there should ever be
2970 anything else in this vector, but being paranoid always helps...
2972 The test below performs the C equivalent of this machine description
2973 pattern match:
2975 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
2976 (mem:SI (plus:SI (reg:SI 3)
2977 (match_operand:SI n "immediate_operand" "i"))))
2980 for (i = 3; i < count; i++)
2982 rtx vector_element = XVECEXP (op, 0, i);
2983 rtx dest;
2984 rtx src;
2985 rtx plus;
2987 if (GET_CODE (vector_element) != SET)
2988 return 0;
2990 dest = SET_DEST (vector_element);
2991 src = SET_SRC (vector_element);
2993 if ( GET_CODE (dest) != REG
2994 || GET_MODE (dest) != SImode
2995 || ! register_is_ok_for_epilogue (dest, SImode)
2996 || GET_CODE (src) != MEM
2997 || GET_MODE (src) != SImode)
2998 return 0;
3000 plus = XEXP (src, 0);
3002 if ( GET_CODE (plus) != PLUS
3003 || GET_CODE (XEXP (plus, 0)) != REG
3004 || GET_MODE (XEXP (plus, 0)) != SImode
3005 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
3006 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
3007 return 0;
3010 return 1;
3013 /* Construct a DISPOSE instruction that is the equivalent of
3014 the given RTX. We have already verified that this should
3015 be possible. */
3017 char *
3018 construct_dispose_instruction (op)
3019 rtx op;
3021 int count = XVECLEN (op, 0);
3022 int stack_bytes;
3023 unsigned long int mask;
3024 int i;
3025 static char buff[ 100 ]; /* XXX */
3026 int use_callt = 0;
3028 if (count <= 2)
3030 error ("Bogus DISPOSE construction: %d\n", count);
3031 return NULL;
3034 /* Work out how many bytes to pop off the
3035 stack before retrieving registers. */
3036 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
3037 abort ();
3038 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
3039 abort ();
3040 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
3041 abort ();
3043 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
3045 /* Each pop will remove 4 bytes from the stack... */
3046 stack_bytes -= (count - 2) * 4;
3048 /* Make sure that the amount we are popping
3049 will fit into the DISPOSE instruction. */
3050 if (stack_bytes > 128)
3052 error ("Too much stack space to dispose of: %d", stack_bytes);
3053 return NULL;
3056 /* Now compute the bit mask of registers to push. */
3057 mask = 0;
3059 for (i = 2; i < count; i++)
3061 rtx vector_element = XVECEXP (op, 0, i);
3063 if (GET_CODE (vector_element) != SET)
3064 abort ();
3065 if (GET_CODE (SET_DEST (vector_element)) != REG)
3066 abort ();
3067 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
3068 abort ();
3070 if (REGNO (SET_DEST (vector_element)) == 2)
3071 use_callt = 1;
3072 else
3073 mask |= 1 << REGNO (SET_DEST (vector_element));
3076 if (! TARGET_DISABLE_CALLT
3077 && (use_callt || stack_bytes == 0 || stack_bytes == 16))
3079 if (use_callt)
3081 sprintf (buff, "callt ctoff(__callt_return_r2_r%d)", (mask & (1 << 31)) ? 31 : 29);
3082 return buff;
3084 else
3086 for (i = 20; i < 32; i++)
3087 if (mask & (1 << i))
3088 break;
3090 if (i == 31)
3091 sprintf (buff, "callt ctoff(__callt_return_r31c)");
3092 else
3093 sprintf (buff, "callt ctoff(__callt_return_r%d_r%d%s)",
3094 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
3097 else
3099 static char regs [100]; /* XXX */
3100 int done_one;
3102 /* Generate the DISPOSE instruction. Note we could just issue the
3103 bit mask as a number as the assembler can cope with this, but for
3104 the sake of our readers we turn it into a textual description. */
3105 regs[0] = 0;
3106 done_one = 0;
3108 for (i = 20; i < 32; i++)
3110 if (mask & (1 << i))
3112 int first;
3114 if (done_one)
3115 strcat (regs, ", ");
3116 else
3117 done_one = 1;
3119 first = i;
3120 strcat (regs, reg_names[ first ]);
3122 for (i++; i < 32; i++)
3123 if ((mask & (1 << i)) == 0)
3124 break;
3126 if (i > first + 1)
3128 strcat (regs, " - ");
3129 strcat (regs, reg_names[ i - 1 ] );
3134 sprintf (buff, "dispose %d {%s}, r31", stack_bytes / 4, regs);
3137 return buff;
3140 /* Return non-zero if the given RTX is suitable
3141 for collapsing into a PREPARE instruction. */
3144 pattern_is_ok_for_prepare (op, mode)
3145 rtx op;
3146 enum machine_mode mode ATTRIBUTE_UNUSED;
3148 int count = XVECLEN (op, 0);
3149 int i;
3151 /* If there are no registers to restore then the prepare instruction
3152 is not suitable. */
3153 if (count <= 1)
3154 return 0;
3156 /* The pattern matching has already established that we are adjusting the
3157 stack and pushing at least one register. We must now check that the
3158 remaining entries in the vector to make sure that they are also register
3159 pushes.
3161 The test below performs the C equivalent of this machine description
3162 pattern match:
3164 (set (mem:SI (plus:SI (reg:SI 3)
3165 (match_operand:SI 2 "immediate_operand" "i")))
3166 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
3170 for (i = 2; i < count; i++)
3172 rtx vector_element = XVECEXP (op, 0, i);
3173 rtx dest;
3174 rtx src;
3175 rtx plus;
3177 if (GET_CODE (vector_element) != SET)
3178 return 0;
3180 dest = SET_DEST (vector_element);
3181 src = SET_SRC (vector_element);
3183 if ( GET_CODE (dest) != MEM
3184 || GET_MODE (dest) != SImode
3185 || GET_CODE (src) != REG
3186 || GET_MODE (src) != SImode
3187 || ! register_is_ok_for_epilogue (src, SImode)
3189 return 0;
3191 plus = XEXP (dest, 0);
3193 if ( GET_CODE (plus) != PLUS
3194 || GET_CODE (XEXP (plus, 0)) != REG
3195 || GET_MODE (XEXP (plus, 0)) != SImode
3196 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
3197 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
3198 return 0;
3200 /* If the register is being pushed somewhere other than the stack
3201 space just aquired by the first operand then abandon this quest.
3202 Note: the test is <= becuase both values are negative. */
3203 if (INTVAL (XEXP (plus, 1))
3204 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
3205 return 0;
3208 return 1;
3211 /* Construct a PREPARE instruction that is the equivalent of
3212 the given RTL. We have already verified that this should
3213 be possible. */
3215 char *
3216 construct_prepare_instruction (op)
3217 rtx op;
3219 int count = XVECLEN (op, 0);
3220 int stack_bytes;
3221 unsigned long int mask;
3222 int i;
3223 static char buff[ 100 ]; /* XXX */
3224 int use_callt = 0;
3226 if (count <= 1)
3228 error ("Bogus PREPEARE construction: %d\n", count);
3229 return NULL;
3232 /* Work out how many bytes to push onto
3233 the stack after storing the registers. */
3234 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
3235 abort ();
3236 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
3237 abort ();
3238 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
3239 abort ();
3241 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
3243 /* Each push will put 4 bytes from the stack. */
3244 stack_bytes += (count - 1) * 4;
3246 /* Make sure that the amount we are popping
3247 will fit into the DISPOSE instruction. */
3248 if (stack_bytes < -128)
3250 error ("Too much stack space to prepare: %d", stack_bytes);
3251 return NULL;
3254 /* Now compute the bit mask of registers to push. */
3255 mask = 0;
3256 for (i = 1; i < count; i++)
3258 rtx vector_element = XVECEXP (op, 0, i);
3260 if (GET_CODE (vector_element) != SET)
3261 abort ();
3262 if (GET_CODE (SET_SRC (vector_element)) != REG)
3263 abort ();
3264 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
3265 abort ();
3267 if (REGNO (SET_SRC (vector_element)) == 2)
3268 use_callt = 1;
3269 else
3270 mask |= 1 << REGNO (SET_SRC (vector_element));
3273 if ((! TARGET_DISABLE_CALLT)
3274 && (use_callt || stack_bytes == 0 || stack_bytes == -16))
3276 if (use_callt)
3278 sprintf (buff, "callt ctoff(__callt_save_r2_r%d)", (mask & (1 << 31)) ? 31 : 29 );
3279 return buff;
3282 for (i = 20; i < 32; i++)
3283 if (mask & (1 << i))
3284 break;
3286 if (i == 31)
3287 sprintf (buff, "callt ctoff(__callt_save_r31c)");
3288 else
3289 sprintf (buff, "callt ctoff(__callt_save_r%d_r%d%s)",
3290 i, (mask & (1 << 31)) ? 31 : 29, stack_bytes ? "c" : "");
3292 else
3294 static char regs [100]; /* XXX */
3295 int done_one;
3298 /* Generate the PREPARE instruction. Note we could just issue the
3299 bit mask as a number as the assembler can cope with this, but for
3300 the sake of our readers we turn it into a textual description. */
3301 regs[0] = 0;
3302 done_one = 0;
3304 for (i = 20; i < 32; i++)
3306 if (mask & (1 << i))
3308 int first;
3310 if (done_one)
3311 strcat (regs, ", ");
3312 else
3313 done_one = 1;
3315 first = i;
3316 strcat (regs, reg_names[ first ]);
3318 for (i++; i < 32; i++)
3319 if ((mask & (1 << i)) == 0)
3320 break;
3322 if (i > first + 1)
3324 strcat (regs, " - ");
3325 strcat (regs, reg_names[ i - 1 ] );
3330 sprintf (buff, "prepare {%s}, %d", regs, (- stack_bytes) / 4);
3333 return buff;
3336 /* Implement `va_arg'. */
3339 v850_va_arg (valist, type)
3340 tree valist, type;
3342 HOST_WIDE_INT size, rsize;
3343 tree addr, incr;
3344 rtx addr_rtx;
3345 int indirect;
3347 /* Round up sizeof(type) to a word. */
3348 size = int_size_in_bytes (type);
3349 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
3350 indirect = 0;
3352 if (size > 8)
3354 size = rsize = UNITS_PER_WORD;
3355 indirect = 1;
3358 addr = save_expr (valist);
3359 incr = fold (build (PLUS_EXPR, ptr_type_node, addr,
3360 build_int_2 (rsize, 0)));
3362 incr = build (MODIFY_EXPR, ptr_type_node, valist, incr);
3363 TREE_SIDE_EFFECTS (incr) = 1;
3364 expand_expr (incr, const0_rtx, VOIDmode, EXPAND_NORMAL);
3366 addr_rtx = expand_expr (addr, NULL, Pmode, EXPAND_NORMAL);
3368 if (indirect)
3370 addr_rtx = force_reg (Pmode, addr_rtx);
3371 addr_rtx = gen_rtx_MEM (Pmode, addr_rtx);
3372 set_mem_alias_set (addr_rtx, get_varargs_alias_set ());
3375 return addr_rtx;
3378 /* Return an RTX indicating where the return address to the
3379 calling function can be found. */
3382 v850_return_addr (count)
3383 int count;
3385 if (count != 0)
3386 return const0_rtx;
3388 return get_hard_reg_initial_val (Pmode, LINK_POINTER_REGNUM);
3391 static void
3392 v850_select_section (exp, reloc, align)
3393 tree exp;
3394 int reloc ATTRIBUTE_UNUSED;
3395 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
3397 if (TREE_CODE (exp) == VAR_DECL)
3399 int is_const;
3400 if (!TREE_READONLY (exp)
3401 || TREE_SIDE_EFFECTS (exp)
3402 || !DECL_INITIAL (exp)
3403 || (DECL_INITIAL (exp) != error_mark_node
3404 && !TREE_CONSTANT (DECL_INITIAL (exp))))
3405 is_const = FALSE;
3406 else
3407 is_const = TRUE;
3409 switch (v850_get_data_area (exp))
3411 case DATA_AREA_ZDA:
3412 if (is_const)
3413 rozdata_section ();
3414 else
3415 zdata_section ();
3416 break;
3418 case DATA_AREA_TDA:
3419 tdata_section ();
3420 break;
3422 case DATA_AREA_SDA:
3423 if (is_const)
3424 rosdata_section ();
3425 else
3426 sdata_section ();
3427 break;
3429 default:
3430 if (is_const)
3431 readonly_data_section ();
3432 else
3433 data_section ();
3434 break;
3437 else if (TREE_CODE (exp) == STRING_CST)
3439 if (! flag_writable_strings)
3440 readonly_data_section ();
3441 else
3442 data_section ();
3444 else
3445 readonly_data_section ();