Fi xexecute/bcp-1.c: Treat CONSTANT_P_RTX as a general operand in movsi.
[official-gcc.git] / gcc / config / v850 / v850.c
blob93fa83080186c7916aacccdf4d65abfd0ed8ee8e
1 /* Subroutines for insn-output.c for NEC V850 series
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Jeff Law (law@cygnus.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <stdio.h>
23 #include <ctype.h>
24 #include "config.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "expr.h"
37 #include "tree.h"
38 #include "obstack.h"
40 /* True if the current function has anonymous arguments. */
41 int current_function_anonymous_args;
43 /* Information about the various small memory areas. */
44 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
46 /* name value max physical max */
47 { "tda", (char *)0, 0, 256 },
48 { "sda", (char *)0, 0, 65536 },
49 { "zda", (char *)0, 0, 32768 },
52 /* True if we don't need to check any more if the current
53 function is an interrupt handler */
54 static int v850_interrupt_cache_p = FALSE;
56 /* Whether current function is an interrupt handler. */
57 static int v850_interrupt_p = FALSE;
60 /* Sometimes certain combinations of command options do not make
61 sense on a particular target machine. You can define a macro
62 `OVERRIDE_OPTIONS' to take account of this. This macro, if
63 defined, is executed once just after all the command options have
64 been parsed.
66 Don't use this macro to turn on various extra optimizations for
67 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
69 void
70 override_options ()
72 int i;
73 extern int atoi ();
75 /* Parse -m{s,t,z}da=nnn switches */
76 for (i = 0; i < (int)SMALL_MEMORY_max; i++)
78 if (small_memory[i].value)
80 if (!isdigit (*small_memory[i].value))
81 error ("%s=%s is not numeric.",
82 small_memory[i].name,
83 small_memory[i].value);
84 else
86 small_memory[i].max = atoi (small_memory[i].value);
87 if (small_memory[i].max > small_memory[i].physical_max)
88 error ("%s=%s is too large.",
89 small_memory[i].name,
90 small_memory[i].value);
97 /* Output assembly code for the start of the file. */
99 void
100 asm_file_start (file)
101 FILE *file;
103 output_file_directive (file, main_input_filename);
107 /* Return an RTX to represent where a value with mode MODE will be returned
108 from a function. If the result is 0, the argument is pushed. */
111 function_arg (cum, mode, type, named)
112 CUMULATIVE_ARGS *cum;
113 enum machine_mode mode;
114 tree type;
115 int named;
117 rtx result = 0;
118 int size, align;
120 if (TARGET_GHS && !named)
121 return NULL_RTX;
123 if (mode == BLKmode)
124 size = int_size_in_bytes (type);
125 else
126 size = GET_MODE_SIZE (mode);
128 if (type)
129 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
130 else
131 align = size;
133 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
135 if (cum->nbytes > 4 * UNITS_PER_WORD)
136 return 0;
138 if (type == NULL_TREE
139 && cum->nbytes + size > 4 * UNITS_PER_WORD)
140 return 0;
142 switch (cum->nbytes / UNITS_PER_WORD)
144 case 0:
145 result = gen_rtx (REG, mode, 6);
146 break;
147 case 1:
148 result = gen_rtx (REG, mode, 7);
149 break;
150 case 2:
151 result = gen_rtx (REG, mode, 8);
152 break;
153 case 3:
154 result = gen_rtx (REG, mode, 9);
155 break;
156 default:
157 result = 0;
160 return result;
164 /* Return the number of words which must be put into registers
165 for values which are part in registers and part in memory. */
168 function_arg_partial_nregs (cum, mode, type, named)
169 CUMULATIVE_ARGS *cum;
170 enum machine_mode mode;
171 tree type;
172 int named;
174 int size, align;
176 if (TARGET_GHS && !named)
177 return 0;
179 if (mode == BLKmode)
180 size = int_size_in_bytes (type);
181 else
182 size = GET_MODE_SIZE (mode);
184 if (type)
185 align = TYPE_ALIGN (type) / BITS_PER_UNIT;
186 else
187 align = size;
189 cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
191 if (cum->nbytes > 4 * UNITS_PER_WORD)
192 return 0;
194 if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
195 return 0;
197 if (type == NULL_TREE
198 && cum->nbytes + size > 4 * UNITS_PER_WORD)
199 return 0;
201 return (4 * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
205 /* Return the high and low words of a CONST_DOUBLE */
207 static void
208 const_double_split (x, p_high, p_low)
209 rtx x;
210 HOST_WIDE_INT *p_high;
211 HOST_WIDE_INT *p_low;
213 if (GET_CODE (x) == CONST_DOUBLE)
215 long t[2];
216 REAL_VALUE_TYPE rv;
218 switch (GET_MODE (x))
220 case DFmode:
221 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
222 REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
223 *p_high = t[1]; /* since v850 is little endian */
224 *p_low = t[0]; /* high is second word */
225 return;
227 case SFmode:
228 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
229 REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
230 *p_low = 0;
231 return;
233 case VOIDmode:
234 case DImode:
235 *p_high = CONST_DOUBLE_HIGH (x);
236 *p_low = CONST_DOUBLE_LOW (x);
237 return;
241 fatal_insn ("const_double_split got a bad insn:", x);
245 /* Return the cost of the rtx R with code CODE. */
247 static int
248 const_costs_int (value, zero_cost)
249 HOST_WIDE_INT value;
250 int zero_cost;
252 if (CONST_OK_FOR_I (value))
253 return zero_cost;
254 else if (CONST_OK_FOR_J (value))
255 return 1;
256 else if (CONST_OK_FOR_K (value))
257 return 2;
258 else
259 return 4;
263 const_costs (r, c)
264 rtx r;
265 enum rtx_code c;
267 HOST_WIDE_INT high, low;
269 switch (c)
271 case CONST_INT:
272 return const_costs_int (INTVAL (r), 0);
274 case CONST_DOUBLE:
275 const_double_split (r, &high, &low);
276 if (GET_MODE (r) == SFmode)
277 return const_costs_int (high, 1);
278 else
279 return const_costs_int (high, 1) + const_costs_int (low, 1);
281 case SYMBOL_REF:
282 case LABEL_REF:
283 case CONST:
284 return 2;
286 case HIGH:
287 return 1;
289 default:
290 return 4;
295 /* Print operand X using operand code CODE to assembly language output file
296 FILE. */
298 void
299 print_operand (file, x, code)
300 FILE *file;
301 rtx x;
302 int code;
304 HOST_WIDE_INT high, low;
306 switch (code)
308 case 'b':
309 case 'B':
310 case 'c':
311 case 'C':
312 switch ((code == 'B' || code == 'C')
313 ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
315 case NE:
316 if (code == 'c' || code == 'C')
317 fprintf (file, "nz");
318 else
319 fprintf (file, "ne");
320 break;
321 case EQ:
322 if (code == 'c' || code == 'C')
323 fprintf (file, "z");
324 else
325 fprintf (file, "e");
326 break;
327 case GE:
328 fprintf (file, "ge");
329 break;
330 case GT:
331 fprintf (file, "gt");
332 break;
333 case LE:
334 fprintf (file, "le");
335 break;
336 case LT:
337 fprintf (file, "lt");
338 break;
339 case GEU:
340 fprintf (file, "nl");
341 break;
342 case GTU:
343 fprintf (file, "h");
344 break;
345 case LEU:
346 fprintf (file, "nh");
347 break;
348 case LTU:
349 fprintf (file, "l");
350 break;
351 default:
352 abort ();
354 break;
355 case 'F': /* high word of CONST_DOUBLE */
356 if (GET_CODE (x) == CONST_INT)
357 fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
358 else if (GET_CODE (x) == CONST_DOUBLE)
360 const_double_split (x, &high, &low);
361 fprintf (file, "%ld", (long) high);
363 else
364 abort ();
365 break;
366 case 'G': /* low word of CONST_DOUBLE */
367 if (GET_CODE (x) == CONST_INT)
368 fprintf (file, "%ld", (long) INTVAL (x));
369 else if (GET_CODE (x) == CONST_DOUBLE)
371 const_double_split (x, &high, &low);
372 fprintf (file, "%ld", (long) low);
374 else
375 abort ();
376 break;
377 case 'L':
378 fprintf (file, "%d\n", INTVAL (x) & 0xffff);
379 break;
380 case 'M':
381 fprintf (file, "%d", exact_log2 (INTVAL (x)));
382 break;
383 case 'O':
384 if (special_symbolref_operand (x, VOIDmode))
386 char* name;
388 if (GET_CODE (x) == SYMBOL_REF)
389 name = XSTR (x, 0);
390 else if (GET_CODE (x) == CONST)
391 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
392 else
393 abort ();
395 if (ZDA_NAME_P (name))
396 fprintf (file, "zdaoff");
397 else if (SDA_NAME_P (name))
398 fprintf (file, "sdaoff");
399 else if (TDA_NAME_P (name))
400 fprintf (file, "tdaoff");
401 else
402 abort ();
404 else
405 abort ();
406 break;
407 case 'P':
408 if (special_symbolref_operand (x, VOIDmode))
409 output_addr_const (file, x);
410 else
411 abort ();
412 break;
413 case 'Q':
414 if (special_symbolref_operand (x, VOIDmode))
416 char* name;
418 if (GET_CODE (x) == SYMBOL_REF)
419 name = XSTR (x, 0);
420 else if (GET_CODE (x) == CONST)
421 name = XSTR (XEXP (XEXP (x, 0), 0), 0);
422 else
423 abort ();
425 if (ZDA_NAME_P (name))
426 fprintf (file, "r0");
427 else if (SDA_NAME_P (name))
428 fprintf (file, "gp");
429 else if (TDA_NAME_P (name))
430 fprintf (file, "ep");
431 else
432 abort ();
434 else
435 abort ();
436 break;
437 case 'R': /* 2nd word of a double. */
438 switch (GET_CODE (x))
440 case REG:
441 fprintf (file, reg_names[REGNO (x) + 1]);
442 break;
443 case MEM:
444 print_operand_address (file,
445 XEXP (adj_offsettable_operand (x, 4), 0));
446 break;
448 break;
449 case 'S':
451 /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
452 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
453 fputs ("s", file);
455 break;
457 case 'T':
459 /* Like an 'S' operand above, but for unsigned loads only. */
460 if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
461 fputs ("s", file);
463 break;
465 case 'W': /* print the instruction suffix */
466 switch (GET_MODE (x))
468 default:
469 abort ();
471 case QImode: fputs (".b", file); break;
472 case HImode: fputs (".h", file); break;
473 case SImode: fputs (".w", file); break;
474 case SFmode: fputs (".w", file); break;
476 break;
477 case '.': /* register r0 */
478 fputs (reg_names[0], file);
479 break;
480 case 'z': /* reg or zero */
481 if (x == const0_rtx)
482 fputs (reg_names[0], file);
483 else if (GET_CODE (x) == REG)
484 fputs (reg_names[REGNO (x)], file);
485 else
486 abort ();
487 break;
488 default:
489 switch (GET_CODE (x))
491 case MEM:
492 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
493 output_address (gen_rtx (PLUS, SImode,
494 gen_rtx (REG, SImode, 0),
495 XEXP (x, 0)));
496 else
497 output_address (XEXP (x, 0));
498 break;
500 case REG:
501 fputs (reg_names[REGNO (x)], file);
502 break;
503 case SUBREG:
504 fputs (reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)], file);
505 break;
506 case CONST_INT:
507 case SYMBOL_REF:
508 case CONST:
509 case LABEL_REF:
510 case CODE_LABEL:
511 print_operand_address (file, x);
512 break;
513 default:
514 abort ();
516 break;
522 /* Output assembly language output for the address ADDR to FILE. */
524 void
525 print_operand_address (file, addr)
526 FILE *file;
527 rtx addr;
529 switch (GET_CODE (addr))
531 case REG:
532 fprintf (file, "0[");
533 print_operand (file, addr, 0);
534 fprintf (file, "]");
535 break;
536 case LO_SUM:
537 if (GET_CODE (XEXP (addr, 0)) == REG)
539 /* reg,foo */
540 fprintf (file, "lo(");
541 print_operand (file, XEXP (addr, 1), 0);
542 fprintf (file, ")[");
543 print_operand (file, XEXP (addr, 0), 0);
544 fprintf (file, "]");
546 break;
547 case PLUS:
548 if (GET_CODE (XEXP (addr, 0)) == REG
549 || GET_CODE (XEXP (addr, 0)) == SUBREG)
551 /* reg,foo */
552 print_operand (file, XEXP (addr, 1), 0);
553 fprintf (file, "[");
554 print_operand (file, XEXP (addr, 0), 0);
555 fprintf (file, "]");
557 else
559 print_operand (file, XEXP (addr, 0), 0);
560 fprintf (file, "+");
561 print_operand (file, XEXP (addr, 1), 0);
563 break;
564 case SYMBOL_REF:
565 if (ENCODED_NAME_P (XSTR (addr, 0)))
567 char* name = XSTR (addr, 0);
568 char* off_name;
569 char* reg_name;
571 if (ZDA_NAME_P (name))
573 off_name = "zdaoff";
574 reg_name = "r0";
576 else if (SDA_NAME_P (name))
578 off_name = "sdaoff";
579 reg_name = "gp";
581 else if (TDA_NAME_P (name))
583 off_name = "tdaoff";
584 reg_name = "ep";
586 else
587 abort ();
589 fprintf (file, "%s(", off_name);
590 output_addr_const (file, addr);
591 fprintf (file, ")[%s]", reg_name);
593 else
594 output_addr_const (file, addr);
595 break;
596 case CONST:
597 if (special_symbolref_operand (addr, VOIDmode))
599 char* name = XSTR (XEXP (XEXP (addr, 0), 0), 0);
600 char* off_name;
601 char* reg_name;
603 if (ZDA_NAME_P (name))
605 off_name = "zdaoff";
606 reg_name = "r0";
608 else if (SDA_NAME_P (name))
610 off_name = "sdaoff";
611 reg_name = "gp";
613 else if (TDA_NAME_P (name))
615 off_name = "tdaoff";
616 reg_name = "ep";
618 else
619 abort ();
621 fprintf (file, "%s(", off_name);
622 output_addr_const (file, addr);
623 fprintf (file, ")[%s]", reg_name);
625 else
626 output_addr_const (file, addr);
627 break;
628 default:
629 output_addr_const (file, addr);
630 break;
635 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
636 point value. */
638 char *
639 output_move_single (operands)
640 rtx *operands;
642 rtx dst = operands[0];
643 rtx src = operands[1];
645 if (REG_P (dst))
647 if (REG_P (src))
648 return "mov %1,%0";
650 else if (GET_CODE (src) == CONST_INT)
652 HOST_WIDE_INT value = INTVAL (src);
654 if (CONST_OK_FOR_J (value)) /* signed 5 bit immediate */
655 return "mov %1,%0";
657 else if (CONST_OK_FOR_K (value)) /* signed 16 bit immediate */
658 return "movea lo(%1),%.,%0";
660 else if (CONST_OK_FOR_L (value)) /* upper 16 bits were set */
661 return "movhi hi(%1),%.,%0";
663 else /* random constant */
664 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
667 else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
669 HOST_WIDE_INT high, low;
671 const_double_split (src, &high, &low);
672 if (CONST_OK_FOR_J (high)) /* signed 5 bit immediate */
673 return "mov %F1,%0";
675 else if (CONST_OK_FOR_K (high)) /* signed 16 bit immediate */
676 return "movea lo(%F1),%.,%0";
678 else if (CONST_OK_FOR_L (high)) /* upper 16 bits were set */
679 return "movhi hi(%F1),%.,%0";
681 else /* random constant */
682 return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
685 else if (GET_CODE (src) == MEM)
686 return "%S1ld%W1 %1,%0";
688 else if (special_symbolref_operand (src, VOIDmode))
689 return "movea %O1(%P1),%Q1,%0";
691 else if (GET_CODE (src) == LABEL_REF
692 || GET_CODE (src) == SYMBOL_REF
693 || GET_CODE (src) == CONST)
695 return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
698 else if (GET_CODE (src) == HIGH)
699 return "movhi hi(%1),%.,%0";
701 else if (GET_CODE (src) == LO_SUM)
703 operands[2] = XEXP (src, 0);
704 operands[3] = XEXP (src, 1);
705 return "movea lo(%3),%2,%0";
709 else if (GET_CODE (dst) == MEM)
711 if (REG_P (src))
712 return "%S0st%W0 %1,%0";
714 else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
715 return "%S0st%W0 %.,%0";
717 else if (GET_CODE (src) == CONST_DOUBLE
718 && CONST0_RTX (GET_MODE (dst)) == src)
719 return "%S0st%W0 %.,%0";
722 fatal_insn ("output_move_single:", gen_rtx (SET, VOIDmode, dst, src));
723 return "";
727 /* Return appropriate code to load up an 8 byte integer or floating point value */
729 char *
730 output_move_double (operands)
731 rtx *operands;
733 enum machine_mode mode = GET_MODE (operands[0]);
734 rtx dst = operands[0];
735 rtx src = operands[1];
737 if (register_operand (dst, mode)
738 && register_operand (src, mode))
740 if (REGNO (src) + 1 == REGNO (dst))
741 return "mov %R1,%R0\n\tmov %1,%0";
742 else
743 return "mov %1,%0\n\tmov %R1,%R0";
746 /* Storing 0 */
747 if (GET_CODE (dst) == MEM
748 && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
749 || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
750 return "st.w %.,%0\n\tst.w %.,%R0";
752 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
754 HOST_WIDE_INT high_low[2];
755 int i;
756 rtx xop[10];
758 if (GET_CODE (src) == CONST_DOUBLE)
759 const_double_split (src, &high_low[1], &high_low[0]);
760 else
762 high_low[0] = INTVAL (src);
763 high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
766 for (i = 0; i < 2; i++)
768 xop[0] = gen_rtx (REG, SImode, REGNO (dst)+i);
769 xop[1] = GEN_INT (high_low[i]);
770 output_asm_insn (output_move_single (xop), xop);
773 return "";
776 if (GET_CODE (src) == MEM)
778 int ptrreg = -1;
779 int dreg = REGNO (dst);
780 rtx inside = XEXP (src, 0);
782 if (GET_CODE (inside) == REG)
783 ptrreg = REGNO (inside);
784 else if (GET_CODE (inside) == SUBREG)
785 ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
786 else if (GET_CODE (inside) == PLUS)
787 ptrreg = REGNO (XEXP (inside, 0));
788 else if (GET_CODE (inside) == LO_SUM)
789 ptrreg = REGNO (XEXP (inside, 0));
791 if (dreg == ptrreg)
792 return "ld.w %R1,%R0\n\tld.w %1,%0";
795 if (GET_CODE (src) == MEM)
796 return "ld.w %1,%0\n\tld.w %R1,%R0";
798 if (GET_CODE (dst) == MEM)
799 return "st.w %1,%0\n\tst.w %R1,%R0";
801 return "mov %1,%0\n\tmov %R1,%R0";
805 /* Return maximum offset supported for a short EP memory reference of mode
806 MODE and signedness UNSIGNEDP. */
809 ep_memory_offset (mode, unsignedp)
810 enum machine_mode mode;
811 int unsignedp;
813 int max_offset = 0;
815 switch (mode)
817 case QImode:
818 max_offset = (1 << 7);
819 break;
821 case HImode:
822 max_offset = (1 << 8);
823 break;
825 case SImode:
826 case SFmode:
827 max_offset = (1 << 8);
828 break;
831 return max_offset;
834 /* Return true if OP is a valid short EP memory reference */
837 ep_memory_operand (op, mode, unsigned_load)
838 rtx op;
839 enum machine_mode mode;
840 int unsigned_load;
842 rtx addr, op0, op1;
843 int max_offset;
844 int mask;
846 if (GET_CODE (op) != MEM)
847 return FALSE;
849 max_offset = ep_memory_offset (mode, unsigned_load);
851 mask = GET_MODE_SIZE (mode) - 1;
853 addr = XEXP (op, 0);
854 if (GET_CODE (addr) == CONST)
855 addr = XEXP (addr, 0);
857 switch (GET_CODE (addr))
859 default:
860 break;
862 case SYMBOL_REF:
863 return TDA_NAME_P (XSTR (addr, 0));
865 case REG:
866 return REGNO (addr) == EP_REGNUM;
868 case PLUS:
869 op0 = XEXP (addr, 0);
870 op1 = XEXP (addr, 1);
871 if (GET_CODE (op1) == CONST_INT
872 && INTVAL (op1) < max_offset
873 && (INTVAL (op1) & mask) == 0)
875 if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
876 return TRUE;
878 if (GET_CODE (op0) == SYMBOL_REF && TDA_NAME_P (XSTR (op0, 0)))
879 return TRUE;
881 break;
884 return FALSE;
887 /* Return true if OP is either a register or 0 */
890 reg_or_0_operand (op, mode)
891 rtx op;
892 enum machine_mode mode;
894 if (GET_CODE (op) == CONST_INT)
895 return INTVAL (op) == 0;
897 else if (GET_CODE (op) == CONST_DOUBLE)
898 return CONST_DOUBLE_OK_FOR_G (op);
900 else
901 return register_operand (op, mode);
904 /* Return true if OP is either a register or a signed five bit integer */
907 reg_or_int5_operand (op, mode)
908 rtx op;
909 enum machine_mode mode;
911 if (GET_CODE (op) == CONST_INT)
912 return CONST_OK_FOR_J (INTVAL (op));
914 else
915 return register_operand (op, mode);
918 /* Return true if OP is a valid call operand. */
921 call_address_operand (op, mode)
922 rtx op;
923 enum machine_mode mode;
925 /* Only registers are valid call operands if TARGET_LONG_CALLS. */
926 if (TARGET_LONG_CALLS)
927 return GET_CODE (op) == REG;
928 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
932 special_symbolref_operand (op, mode)
933 rtx op;
934 enum machine_mode mode;
936 if (GET_CODE (op) == SYMBOL_REF)
937 return ENCODED_NAME_P (XSTR (op, 0));
939 else if (GET_CODE (op) == CONST)
940 return (GET_CODE (XEXP (op, 0)) == PLUS
941 && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
942 && ENCODED_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0))
943 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
944 && CONST_OK_FOR_K (INTVAL (XEXP (XEXP (op, 0), 1))));
946 return FALSE;
950 movsi_source_operand (op, mode)
951 rtx op;
952 enum machine_mode mode;
954 /* Some constants, as well as symbolic operands
955 must be done with HIGH & LO_SUM patterns. */
956 if (CONSTANT_P (op)
957 && GET_CODE (op) != HIGH
958 && GET_CODE (op) != CONSTANT_P_RTX
959 && !(GET_CODE (op) == CONST_INT
960 && (CONST_OK_FOR_J (INTVAL (op))
961 || CONST_OK_FOR_K (INTVAL (op))
962 || CONST_OK_FOR_L (INTVAL (op)))))
963 return special_symbolref_operand (op, mode);
964 else
965 return general_operand (op, mode);
969 power_of_two_operand (op, mode)
970 rtx op;
971 enum machine_mode mode;
973 if (GET_CODE (op) != CONST_INT)
974 return 0;
976 if (exact_log2 (INTVAL (op)) == -1)
977 return 0;
978 return 1;
982 not_power_of_two_operand (op, mode)
983 rtx op;
984 enum machine_mode mode;
986 unsigned int mask;
988 if (mode == QImode)
989 mask = 0xff;
990 else if (mode == HImode)
991 mask = 0xffff;
992 else if (mode == SImode)
993 mask = 0xffffffff;
994 else
995 return 0;
997 if (GET_CODE (op) != CONST_INT)
998 return 0;
1000 if (exact_log2 (~INTVAL (op) & mask) == -1)
1001 return 0;
1002 return 1;
1006 /* Substitute memory references involving a pointer, to use the ep pointer,
1007 taking care to save and preserve the ep. */
1009 static void
1010 substitute_ep_register (first_insn, last_insn, uses, regno, p_r1, p_ep)
1011 rtx first_insn;
1012 rtx last_insn;
1013 int uses;
1014 int regno;
1015 rtx *p_r1;
1016 rtx *p_ep;
1018 rtx reg = gen_rtx (REG, Pmode, regno);
1019 rtx insn;
1020 int i;
1022 if (!*p_r1)
1024 regs_ever_live[1] = 1;
1025 *p_r1 = gen_rtx (REG, Pmode, 1);
1026 *p_ep = gen_rtx (REG, Pmode, 30);
1029 if (TARGET_DEBUG)
1030 fprintf (stderr, "Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1031 2 * (uses - 3), uses, reg_names[regno],
1032 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1033 INSN_UID (first_insn), INSN_UID (last_insn));
1035 if (GET_CODE (first_insn) == NOTE)
1036 first_insn = next_nonnote_insn (first_insn);
1038 last_insn = next_nonnote_insn (last_insn);
1039 for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1041 if (GET_CODE (insn) == INSN)
1043 rtx pattern = single_set (insn);
1045 /* Replace the memory references. */
1046 if (pattern)
1048 rtx *p_mem;
1049 /* Memory operands are signed by default. */
1050 int unsignedp = FALSE;
1052 if (GET_CODE (SET_DEST (pattern)) == MEM
1053 && GET_CODE (SET_SRC (pattern)) == MEM)
1054 p_mem = (rtx *)0;
1056 else if (GET_CODE (SET_DEST (pattern)) == MEM)
1057 p_mem = &SET_DEST (pattern);
1059 else if (GET_CODE (SET_SRC (pattern)) == MEM)
1060 p_mem = &SET_SRC (pattern);
1062 else
1063 p_mem = (rtx *)0;
1065 if (p_mem)
1067 rtx addr = XEXP (*p_mem, 0);
1069 if (GET_CODE (addr) == REG && REGNO (addr) == regno)
1070 *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1072 else if (GET_CODE (addr) == PLUS
1073 && GET_CODE (XEXP (addr, 0)) == REG
1074 && REGNO (XEXP (addr, 0)) == regno
1075 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1076 && (((unsigned)INTVAL (XEXP (addr, 1)))
1077 < ep_memory_offset (GET_MODE (*p_mem),
1078 unsignedp)))
1079 *p_mem = change_address (*p_mem, VOIDmode,
1080 gen_rtx (PLUS, Pmode,
1081 *p_ep, XEXP (addr, 1)));
1087 /* Optimize back to back cases of ep <- r1 & r1 <- ep. */
1088 insn = prev_nonnote_insn (first_insn);
1089 if (insn && GET_CODE (insn) == INSN
1090 && GET_CODE (PATTERN (insn)) == SET
1091 && SET_DEST (PATTERN (insn)) == *p_ep
1092 && SET_SRC (PATTERN (insn)) == *p_r1)
1093 delete_insn (insn);
1094 else
1095 emit_insn_before (gen_rtx (SET, Pmode, *p_r1, *p_ep), first_insn);
1097 emit_insn_before (gen_rtx (SET, Pmode, *p_ep, reg), first_insn);
1098 emit_insn_before (gen_rtx (SET, Pmode, *p_ep, *p_r1), last_insn);
1102 /* In rare cases, correct code generation requires extra machine
1103 dependent processing between the second jump optimization pass and
1104 delayed branch scheduling. On those machines, define this macro
1105 as a C statement to act on the code starting at INSN.
1107 On the 850, we use it to implement the -mep mode to copy heavily used
1108 pointers to ep to use the implicit addressing */
1110 void v850_reorg (start_insn)
1111 rtx start_insn;
1113 struct {
1114 int uses;
1115 rtx first_insn;
1116 rtx last_insn;
1117 } regs[FIRST_PSEUDO_REGISTER];
1119 int i;
1120 int use_ep = FALSE;
1121 rtx r1 = NULL_RTX;
1122 rtx ep = NULL_RTX;
1123 rtx insn;
1124 rtx pattern;
1126 /* If not ep mode, just return now */
1127 if (!TARGET_EP)
1128 return;
1130 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1132 regs[i].uses = 0;
1133 regs[i].first_insn = NULL_RTX;
1134 regs[i].last_insn = NULL_RTX;
1137 for (insn = start_insn; insn != NULL_RTX; insn = NEXT_INSN (insn))
1139 switch (GET_CODE (insn))
1141 /* End of basic block */
1142 default:
1143 if (!use_ep)
1145 int max_uses = -1;
1146 int max_regno = -1;
1148 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1150 if (max_uses < regs[i].uses)
1152 max_uses = regs[i].uses;
1153 max_regno = i;
1157 if (max_uses > 3)
1158 substitute_ep_register (regs[max_regno].first_insn,
1159 regs[max_regno].last_insn,
1160 max_uses, max_regno, &r1, &ep);
1163 use_ep = FALSE;
1164 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1166 regs[i].uses = 0;
1167 regs[i].first_insn = NULL_RTX;
1168 regs[i].last_insn = NULL_RTX;
1170 break;
1172 case NOTE:
1173 break;
1175 case INSN:
1176 pattern = single_set (insn);
1178 /* See if there are any memory references we can shorten */
1179 if (pattern)
1181 rtx src = SET_SRC (pattern);
1182 rtx dest = SET_DEST (pattern);
1183 rtx mem;
1184 /* Memory operands are signed by default. */
1185 int unsignedp = FALSE;
1187 if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1188 mem = NULL_RTX;
1190 else if (GET_CODE (dest) == MEM)
1191 mem = dest;
1193 else if (GET_CODE (src) == MEM)
1194 mem = src;
1196 else
1197 mem = NULL_RTX;
1199 if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1200 use_ep = TRUE;
1202 else if (!use_ep && mem
1203 && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1205 rtx addr = XEXP (mem, 0);
1206 int regno = -1;
1207 int short_p;
1209 if (GET_CODE (addr) == REG)
1211 short_p = TRUE;
1212 regno = REGNO (addr);
1215 else if (GET_CODE (addr) == PLUS
1216 && GET_CODE (XEXP (addr, 0)) == REG
1217 && GET_CODE (XEXP (addr, 1)) == CONST_INT
1218 && (((unsigned)INTVAL (XEXP (addr, 1)))
1219 < ep_memory_offset (GET_MODE (mem), unsignedp)))
1221 short_p = TRUE;
1222 regno = REGNO (XEXP (addr, 0));
1225 else
1226 short_p = FALSE;
1228 if (short_p)
1230 regs[regno].uses++;
1231 regs[regno].last_insn = insn;
1232 if (!regs[regno].first_insn)
1233 regs[regno].first_insn = insn;
1237 /* Loading up a register in the basic block zaps any savings
1238 for the register */
1239 if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
1241 enum machine_mode mode = GET_MODE (dest);
1242 int word = 0;
1243 int regno;
1244 int endregno;
1246 while (GET_CODE (dest) == SUBREG)
1248 word = SUBREG_WORD (dest);
1249 dest = SUBREG_REG (dest);
1252 regno = REGNO (dest) + word;
1253 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1255 if (!use_ep)
1257 /* See if we can use the pointer before this
1258 modification. */
1259 int max_uses = -1;
1260 int max_regno = -1;
1262 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1264 if (max_uses < regs[i].uses)
1266 max_uses = regs[i].uses;
1267 max_regno = i;
1271 if (max_uses > 3
1272 && max_regno >= regno
1273 && max_regno < endregno)
1275 substitute_ep_register (regs[max_regno].first_insn,
1276 regs[max_regno].last_insn,
1277 max_uses, max_regno, &r1, &ep);
1279 /* Since we made a substitution, zap all remembered
1280 registers. */
1281 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1283 regs[i].uses = 0;
1284 regs[i].first_insn = NULL_RTX;
1285 regs[i].last_insn = NULL_RTX;
1290 for (i = regno; i < endregno; i++)
1292 regs[i].uses = 0;
1293 regs[i].first_insn = NULL_RTX;
1294 regs[i].last_insn = NULL_RTX;
1303 /* # of registers saved by the interrupt handler. */
1304 #define INTERRUPT_FIXED_NUM 4
1306 /* # of bytes for registers saved by the interrupt handler. */
1307 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1309 /* # of registers saved in register parameter area. */
1310 #define INTERRUPT_REGPARM_NUM 4
1311 /* # of words saved for other registers. */
1312 #define INTERRUPT_ALL_SAVE_NUM \
1313 (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1315 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1318 compute_register_save_size (p_reg_saved)
1319 long *p_reg_saved;
1321 int size = 0;
1322 int i;
1323 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1324 int call_p = regs_ever_live[31];
1325 long reg_saved = 0;
1327 /* Count the return pointer if we need to save it. */
1328 if (profile_flag && !call_p)
1329 regs_ever_live[31] = call_p = 1;
1331 /* Count space for the register saves. */
1332 if (interrupt_handler)
1334 for (i = 0; i <= 31; i++)
1335 switch (i)
1337 default:
1338 if (regs_ever_live[i] || call_p)
1340 size += 4;
1341 reg_saved |= 1L << i;
1343 break;
1345 /* We don't save/restore r0 or the stack pointer */
1346 case 0:
1347 case STACK_POINTER_REGNUM:
1348 break;
1350 /* For registers with fixed use, we save them, set them to the
1351 appropriate value, and then restore them.
1352 These registers are handled specially, so don't list them
1353 on the list of registers to save in the prologue. */
1354 case 1: /* temp used to hold ep */
1355 case 4: /* gp */
1356 case 10: /* temp used to call interrupt save/restore */
1357 case EP_REGNUM: /* ep */
1358 size += 4;
1359 break;
1363 else
1364 for (i = 0; i <= 31; i++)
1365 if (regs_ever_live[i] && ((! call_used_regs[i]) || i == 31))
1367 size += 4;
1368 reg_saved |= 1L << i;
1371 if (p_reg_saved)
1372 *p_reg_saved = reg_saved;
1374 return size;
1378 compute_frame_size (size, p_reg_saved)
1379 int size;
1380 long *p_reg_saved;
1382 extern int current_function_outgoing_args_size;
1384 return (size
1385 + compute_register_save_size (p_reg_saved)
1386 + current_function_outgoing_args_size);
1390 void
1391 expand_prologue ()
1393 unsigned int i;
1394 int offset;
1395 unsigned int size = get_frame_size ();
1396 unsigned int actual_fsize;
1397 unsigned int init_stack_alloc = 0;
1398 rtx save_regs[32];
1399 rtx save_all;
1400 int num_save;
1401 int default_stack;
1402 int code;
1403 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1404 long reg_saved = 0;
1406 actual_fsize = compute_frame_size (size, &reg_saved);
1408 /* Save/setup global registers for interrupt functions right now */
1409 if (interrupt_handler)
1411 emit_insn (gen_save_interrupt ());
1412 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1413 if (((1L << 31) & reg_saved) != 0)
1414 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1417 /* Save arg registers to the stack if necessary. */
1418 else if (current_function_anonymous_args)
1420 if (TARGET_PROLOG_FUNCTION)
1421 emit_insn (gen_save_r6_r9 ());
1422 else
1424 offset = 0;
1425 for (i = 6; i < 10; i++)
1427 emit_move_insn (gen_rtx (MEM, SImode,
1428 plus_constant (stack_pointer_rtx,
1429 offset)),
1430 gen_rtx (REG, SImode, i));
1431 offset += 4;
1436 /* Identify all of the saved registers */
1437 num_save = 0;
1438 default_stack = 0;
1439 for (i = 1; i < 31; i++)
1441 if (((1L << i) & reg_saved) != 0)
1442 save_regs[num_save++] = gen_rtx (REG, Pmode, i);
1445 /* If the return pointer is saved, the helper functions also allocate
1446 16 bytes of stack for arguments to be saved in. */
1447 if (((1L << 31) & reg_saved) != 0)
1449 save_regs[num_save++] = gen_rtx (REG, Pmode, 31);
1450 default_stack = 16;
1453 /* See if we have an insn that allocates stack space and saves the particular
1454 registers we want to. */
1455 save_all = NULL_RTX;
1456 if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1458 int alloc_stack = (4 * num_save) + default_stack;
1459 int unalloc_stack = actual_fsize - alloc_stack;
1460 int save_func_len = 4;
1461 int save_normal_len;
1463 if (unalloc_stack)
1464 save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1466 /* see if we would have used ep to save the stack */
1467 if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1468 save_normal_len = (3 * 2) + (2 * num_save);
1469 else
1470 save_normal_len = 4 * num_save;
1472 save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1474 /* Don't bother checking if we don't actually save any space.
1475 This happens for instance if one register is saved and additional
1476 stack space is allocated. */
1477 if (save_func_len < save_normal_len)
1479 save_all = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (num_save + (TARGET_V850 ? 2 : 1)));
1480 XVECEXP (save_all, 0, 0) = gen_rtx (SET, VOIDmode,
1481 stack_pointer_rtx,
1482 gen_rtx (PLUS, Pmode,
1483 stack_pointer_rtx,
1484 GEN_INT (-alloc_stack)));
1486 if (TARGET_V850)
1488 XVECEXP (save_all, 0, num_save+1)
1489 = gen_rtx (CLOBBER, VOIDmode, gen_rtx (REG, Pmode, 10));
1492 offset = - default_stack;
1493 for (i = 0; i < num_save; i++)
1495 XVECEXP (save_all, 0, i+1)
1496 = gen_rtx (SET, VOIDmode,
1497 gen_rtx (MEM, Pmode,
1498 plus_constant (stack_pointer_rtx, offset)),
1499 save_regs[i]);
1500 offset -= 4;
1503 code = recog (save_all, NULL_RTX, NULL_PTR);
1504 if (code >= 0)
1506 rtx insn = emit_insn (save_all);
1507 INSN_CODE (insn) = code;
1508 actual_fsize -= alloc_stack;
1510 if (TARGET_DEBUG)
1511 fprintf (stderr, "Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1512 save_normal_len - save_func_len,
1513 save_normal_len, save_func_len,
1514 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1516 else
1517 save_all = NULL_RTX;
1521 /* If no prolog save function is available, store the registers the old fashioned
1522 way (one by one). */
1523 if (!save_all)
1525 /* Special case interrupt functions that save all registers for a call. */
1526 if (interrupt_handler && ((1L << 31) & reg_saved) != 0)
1527 emit_insn (gen_save_all_interrupt ());
1529 else
1531 /* If the stack is too big, allocate it in chunks so we can do the
1532 register saves. We use the register save size so we use the ep
1533 register. */
1534 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1535 init_stack_alloc = compute_register_save_size (NULL);
1536 else
1537 init_stack_alloc = actual_fsize;
1539 /* Save registers at the beginning of the stack frame */
1540 offset = init_stack_alloc - 4;
1542 if (init_stack_alloc)
1543 emit_insn (gen_addsi3 (stack_pointer_rtx,
1544 stack_pointer_rtx,
1545 GEN_INT (-init_stack_alloc)));
1547 /* Save the return pointer first. */
1548 if (num_save > 0 && REGNO (save_regs[num_save-1]) == 31)
1550 emit_move_insn (gen_rtx (MEM, SImode,
1551 plus_constant (stack_pointer_rtx,
1552 offset)),
1553 save_regs[--num_save]);
1554 offset -= 4;
1557 for (i = 0; i < num_save; i++)
1559 emit_move_insn (gen_rtx (MEM, SImode,
1560 plus_constant (stack_pointer_rtx,
1561 offset)),
1562 save_regs[i]);
1563 offset -= 4;
1568 /* Allocate the rest of the stack that was not allocated above (either it is
1569 > 32K or we just called a function to save the registers and needed more
1570 stack. */
1571 if (actual_fsize > init_stack_alloc)
1573 int diff = actual_fsize - init_stack_alloc;
1574 if (CONST_OK_FOR_K (diff))
1575 emit_insn (gen_addsi3 (stack_pointer_rtx,
1576 stack_pointer_rtx,
1577 GEN_INT (-diff)));
1578 else
1580 rtx reg = gen_rtx (REG, Pmode, 12);
1581 emit_move_insn (reg, GEN_INT (-diff));
1582 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1586 /* If we need a frame pointer, set it up now. */
1587 if (frame_pointer_needed)
1588 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1592 void
1593 expand_epilogue ()
1595 unsigned int i;
1596 int offset;
1597 unsigned int size = get_frame_size ();
1598 long reg_saved = 0;
1599 unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1600 unsigned int init_stack_free = 0;
1601 rtx restore_regs[32];
1602 rtx restore_all;
1603 int num_restore;
1604 int default_stack;
1605 int code;
1606 int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1608 /* Eliminate the initial stack stored by interrupt functions. */
1609 if (interrupt_handler)
1611 actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1612 if (((1L << 31) & reg_saved) != 0)
1613 actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1616 /* Cut off any dynamic stack created. */
1617 if (frame_pointer_needed)
1618 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1620 /* Identify all of the saved registers */
1621 num_restore = 0;
1622 default_stack = 0;
1623 for (i = 1; i < 31; i++)
1625 if (((1L << i) & reg_saved) != 0)
1626 restore_regs[num_restore++] = gen_rtx (REG, Pmode, i);
1629 /* If the return pointer is saved, the helper functions also allocate
1630 16 bytes of stack for arguments to be saved in. */
1631 if (((1L << 31) & reg_saved) != 0)
1633 restore_regs[num_restore++] = gen_rtx (REG, Pmode, 31);
1634 default_stack = 16;
1637 /* See if we have an insn that restores the particular registers we
1638 want to. */
1639 restore_all = NULL_RTX;
1640 if (TARGET_PROLOG_FUNCTION && num_restore > 0 && actual_fsize >= default_stack
1641 && !interrupt_handler)
1643 int alloc_stack = (4 * num_restore) + default_stack;
1644 int unalloc_stack = actual_fsize - alloc_stack;
1645 int restore_func_len = 4;
1646 int restore_normal_len;
1648 if (unalloc_stack)
1649 restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1651 /* see if we would have used ep to restore the registers */
1652 if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1653 restore_normal_len = (3 * 2) + (2 * num_restore);
1654 else
1655 restore_normal_len = 4 * num_restore;
1657 restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1659 /* Don't bother checking if we don't actually save any space. */
1660 if (restore_func_len < restore_normal_len)
1662 restore_all = gen_rtx (PARALLEL, VOIDmode,
1663 rtvec_alloc (num_restore + 2));
1664 XVECEXP (restore_all, 0, 0) = gen_rtx (RETURN, VOIDmode);
1665 XVECEXP (restore_all, 0, 1)
1666 = gen_rtx (SET, VOIDmode, stack_pointer_rtx,
1667 gen_rtx (PLUS, Pmode,
1668 stack_pointer_rtx,
1669 GEN_INT (alloc_stack)));
1671 offset = alloc_stack - 4;
1672 for (i = 0; i < num_restore; i++)
1674 XVECEXP (restore_all, 0, i+2)
1675 = gen_rtx (SET, VOIDmode,
1676 restore_regs[i],
1677 gen_rtx (MEM, Pmode,
1678 plus_constant (stack_pointer_rtx, offset)));
1679 offset -= 4;
1682 code = recog (restore_all, NULL_RTX, NULL_PTR);
1683 if (code >= 0)
1685 rtx insn;
1687 actual_fsize -= alloc_stack;
1688 if (actual_fsize)
1690 if (CONST_OK_FOR_K (actual_fsize))
1691 emit_insn (gen_addsi3 (stack_pointer_rtx,
1692 stack_pointer_rtx,
1693 GEN_INT (actual_fsize)));
1694 else
1696 rtx reg = gen_rtx (REG, Pmode, 12);
1697 emit_move_insn (reg, GEN_INT (actual_fsize));
1698 emit_insn (gen_addsi3 (stack_pointer_rtx,
1699 stack_pointer_rtx,
1700 reg));
1704 insn = emit_jump_insn (restore_all);
1705 INSN_CODE (insn) = code;
1707 if (TARGET_DEBUG)
1708 fprintf (stderr, "Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1709 restore_normal_len - restore_func_len,
1710 restore_normal_len, restore_func_len,
1711 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1713 else
1714 restore_all = NULL_RTX;
1718 /* If no epilog save function is available, restore the registers the
1719 old fashioned way (one by one). */
1720 if (!restore_all)
1722 /* If the stack is large, we need to cut it down in 2 pieces. */
1723 if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1724 init_stack_free = 4 * num_restore;
1725 else
1726 init_stack_free = actual_fsize;
1728 /* Deallocate the rest of the stack if it is > 32K or if extra stack
1729 was allocated for an interrupt handler that makes a call. */
1730 if (actual_fsize > init_stack_free || (interrupt_handler && actual_fsize))
1732 int diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1733 if (CONST_OK_FOR_K (diff))
1734 emit_insn (gen_addsi3 (stack_pointer_rtx,
1735 stack_pointer_rtx,
1736 GEN_INT (diff)));
1737 else
1739 rtx reg = gen_rtx (REG, Pmode, 12);
1740 emit_move_insn (reg, GEN_INT (diff));
1741 emit_insn (gen_addsi3 (stack_pointer_rtx,
1742 stack_pointer_rtx,
1743 reg));
1747 /* Special case interrupt functions that save all registers
1748 for a call. */
1749 if (interrupt_handler && ((1L << 31) & reg_saved) != 0)
1750 emit_insn (gen_restore_all_interrupt ());
1751 else
1753 /* Restore registers from the beginning of the stack frame */
1754 offset = init_stack_free - 4;
1756 /* Restore the return pointer first. */
1757 if (num_restore > 0 && REGNO (restore_regs[num_restore-1]) == 31)
1759 emit_move_insn (restore_regs[--num_restore],
1760 gen_rtx (MEM, SImode,
1761 plus_constant (stack_pointer_rtx,
1762 offset)));
1763 offset -= 4;
1766 for (i = 0; i < num_restore; i++)
1768 emit_move_insn (restore_regs[i],
1769 gen_rtx (MEM, SImode,
1770 plus_constant (stack_pointer_rtx,
1771 offset)));
1773 offset -= 4;
1776 /* Cut back the remainder of the stack. */
1777 if (init_stack_free)
1778 emit_insn (gen_addsi3 (stack_pointer_rtx,
1779 stack_pointer_rtx,
1780 GEN_INT (init_stack_free)));
1783 /* And return or use reti for interrupt handlers. */
1784 if (interrupt_handler)
1785 emit_jump_insn (gen_restore_interrupt ());
1786 else if (actual_fsize)
1787 emit_jump_insn (gen_return_internal ());
1788 else
1789 emit_jump_insn (gen_return ());
1792 current_function_anonymous_args = 0;
1793 v850_interrupt_cache_p = FALSE;
1794 v850_interrupt_p = FALSE;
1798 /* Update the condition code from the insn. */
1800 void
1801 notice_update_cc (body, insn)
1802 rtx body;
1803 rtx insn;
1805 switch (get_attr_cc (insn))
1807 case CC_NONE:
1808 /* Insn does not affect CC at all. */
1809 break;
1811 case CC_NONE_0HIT:
1812 /* Insn does not change CC, but the 0'th operand has been changed. */
1813 if (cc_status.value1 != 0
1814 && reg_overlap_mentioned_p (recog_operand[0], cc_status.value1))
1815 cc_status.value1 = 0;
1816 break;
1818 case CC_SET_ZN:
1819 /* Insn sets the Z,N flags of CC to recog_operand[0].
1820 V,C is in an unusable state. */
1821 CC_STATUS_INIT;
1822 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1823 cc_status.value1 = recog_operand[0];
1824 break;
1826 case CC_SET_ZNV:
1827 /* Insn sets the Z,N,V flags of CC to recog_operand[0].
1828 C is in an unusable state. */
1829 CC_STATUS_INIT;
1830 cc_status.flags |= CC_NO_CARRY;
1831 cc_status.value1 = recog_operand[0];
1832 break;
1834 case CC_COMPARE:
1835 /* The insn is a compare instruction. */
1836 CC_STATUS_INIT;
1837 cc_status.value1 = SET_SRC (body);
1838 break;
1840 case CC_CLOBBER:
1841 /* Insn doesn't leave CC in a usable state. */
1842 CC_STATUS_INIT;
1843 break;
1848 /* Return nonzero if ATTR is a valid attribute for DECL.
1849 ATTRIBUTES are any existing attributes and ARGS are the arguments
1850 supplied with ATTR.
1852 Supported attributes:
1854 interrupt_handler or interrupt: output a prologue and epilogue suitable
1855 for an interrupt handler. */
1858 v850_valid_machine_decl_attribute (decl, attributes, attr, args)
1859 tree decl;
1860 tree attributes;
1861 tree attr;
1862 tree args;
1864 if (args != NULL_TREE)
1865 return 0;
1867 if (is_attribute_p ("interrupt_handler", attr)
1868 || is_attribute_p ("interrupt", attr))
1869 return TREE_CODE (decl) == FUNCTION_DECL;
1871 return 0;
1875 /* Return nonzero if FUNC is an interrupt function as specified
1876 by the "interrupt" attribute. */
1879 v850_interrupt_function_p (func)
1880 tree func;
1882 tree a;
1883 int ret = 0;
1885 if (v850_interrupt_cache_p)
1886 return v850_interrupt_p;
1888 if (TREE_CODE (func) != FUNCTION_DECL)
1889 return 0;
1891 a = lookup_attribute ("interrupt_handler", DECL_MACHINE_ATTRIBUTES (func));
1892 if (a != NULL_TREE)
1893 ret = 1;
1895 else
1897 a = lookup_attribute ("interrupt", DECL_MACHINE_ATTRIBUTES (func));
1898 ret = a != NULL_TREE;
1901 /* Its not safe to trust global variables until after function inlining has
1902 been done. */
1903 if (reload_completed | reload_in_progress)
1904 v850_interrupt_p = ret;
1906 return ret;
1910 extern struct obstack *saveable_obstack;
1912 v850_encode_data_area (decl)
1913 tree decl;
1915 char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1916 int len = strlen (str);
1917 char *newstr;
1919 /* In the Cygnus sources we actually do something; this is just
1920 here to make merges easier. */
1921 return;
1924 /* Return true if the given RTX is a register which can be restored
1925 by a function epilogue. */
1927 register_is_ok_for_epilogue (op, mode)
1928 rtx op;
1929 enum machine_mode mode;
1931 /* The save/restore routines can only cope with registers 2, and 20 - 31 */
1932 return (GET_CODE (op) == REG)
1933 && (((REGNO (op) >= 20) && REGNO (op) <= 31)
1934 || REGNO (op) == 2);
1937 /* Return non-zero if the given RTX is suitable for collapsing into
1938 jump to a function epilogue. */
1940 pattern_is_ok_for_epilogue (op, mode)
1941 rtx op;
1942 enum machine_mode mode;
1944 int count = XVECLEN (op, 0);
1945 int i;
1947 /* If there are no registers to restore then the function epilogue
1948 is not suitable. */
1949 if (count <= 2)
1950 return 0;
1952 /* The pattern matching has already established that we are performing a
1953 function epilogue and that we are popping at least one register. We must
1954 now check the remaining entries in the vector to make sure that they are
1955 also register pops. There is no good reason why there should ever be
1956 anything else in this vector, but being paranoid always helps...
1958 The test below performs the C equivalent of this machine description
1959 pattern match:
1961 (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
1962 (mem:SI (plus:SI (reg:SI 3) (match_operand:SI n "immediate_operand" "i"))))
1965 for (i = 3; i < count; i++)
1967 rtx vector_element = XVECEXP (op, 0, i);
1968 rtx dest;
1969 rtx src;
1970 rtx plus;
1972 if (GET_CODE (vector_element) != SET)
1973 return 0;
1975 dest = SET_DEST (vector_element);
1976 src = SET_SRC (vector_element);
1978 if (GET_CODE (dest) != REG
1979 || GET_MODE (dest) != SImode
1980 || ! register_is_ok_for_epilogue (dest, SImode)
1981 || GET_CODE (src) != MEM
1982 || GET_MODE (src) != SImode)
1983 return 0;
1985 plus = XEXP (src, 0);
1987 if (GET_CODE (plus) != PLUS
1988 || GET_CODE (XEXP (plus, 0)) != REG
1989 || GET_MODE (XEXP (plus, 0)) != SImode
1990 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
1991 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
1992 return 0;
1995 return 1;
1998 /* Construct a JR instruction to a routine that will perform the equivalent of
1999 the RTL passed in as an argument. This RTL is a function epilogue that
2000 pops registers off the stack and possibly releases some extra stack space
2001 as well. The code has already verified that the RTL matches these
2002 requirements. */
2003 char *
2004 construct_restore_jr (op)
2005 rtx op;
2007 int count = XVECLEN (op, 0);
2008 int stack_bytes;
2009 unsigned long int mask;
2010 unsigned long int first;
2011 unsigned long int last;
2012 int i;
2013 static char buff [100]; /* XXX */
2015 if (count <= 2)
2017 error ("Bogus JR construction: %d\n", count);
2018 return NULL;
2021 /* Work out how many bytes to pop off the stack before retrieving
2022 registers. */
2023 if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2024 abort ();
2025 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2026 abort ();
2027 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2028 abort ();
2030 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2032 /* Each pop will remove 4 bytes from the stack... */
2033 stack_bytes -= (count - 2) * 4;
2035 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2036 if (stack_bytes != 0 && stack_bytes != 16)
2038 error ("Bad amount of stack space removal: %d", stack_bytes);
2039 return NULL;
2042 /* Now compute the bit mask of registers to push. */
2043 mask = 0;
2044 for (i = 2; i < count; i++)
2046 rtx vector_element = XVECEXP (op, 0, i);
2048 if (GET_CODE (vector_element) != SET)
2049 abort ();
2050 if (GET_CODE (SET_DEST (vector_element)) != REG)
2051 abort ();
2052 if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2053 abort ();
2055 mask |= 1 << REGNO (SET_DEST (vector_element));
2058 /* Scan for the first register to pop. */
2059 for (first = 0; first < 32; first++)
2061 if (mask & (1 << first))
2062 break;
2065 if (first >= 32)
2066 abort ();
2068 /* Discover the last register to pop. */
2069 if (mask & (1 << 31))
2071 if (stack_bytes != 16)
2072 abort ();
2074 last = 31;
2076 else
2078 if (stack_bytes != 0)
2079 abort ();
2080 if ((mask & (1 << 29)) == 0)
2081 abort ();
2083 last = 29;
2086 /* Note, it is possible to have gaps in the register mask.
2087 We ignore this here, and generate a JR anyway. We will
2088 be popping more registers thatn is strictly necessary, but
2089 it does save code space. */
2091 if (first == last)
2092 sprintf (buff, "jr __return_%s", reg_names [first]);
2093 else
2094 sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2096 return buff;
2100 /* Return non-zero if the given RTX is suitable for collapsing into
2101 a jump to a function prologue. */
2103 pattern_is_ok_for_prologue (op, mode)
2104 rtx op;
2105 enum machine_mode mode;
2107 int count = XVECLEN (op, 0);
2108 int i;
2109 rtx vector_element;
2111 /* If there are no registers to save then the function prologue
2112 is not suitable. */
2113 if (count <= 2)
2114 return 0;
2116 /* The pattern matching has already established that we are adjusting the
2117 stack and pushing at least one register. We must now check that the
2118 remaining entries in the vector to make sure that they are also register
2119 pushes, except for the last entry which should be a CLOBBER of r10.
2121 The test below performs the C equivalent of this machine description
2122 pattern match:
2124 (set (mem:SI (plus:SI (reg:SI 3)
2125 (match_operand:SI 2 "immediate_operand" "i")))
2126 (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
2130 for (i = 2; i < count - 1; i++)
2132 rtx dest;
2133 rtx src;
2134 rtx plus;
2136 vector_element = XVECEXP (op, 0, i);
2138 if (GET_CODE (vector_element) != SET)
2139 return 0;
2141 dest = SET_DEST (vector_element);
2142 src = SET_SRC (vector_element);
2144 if (GET_CODE (dest) != MEM
2145 || GET_MODE (dest) != SImode
2146 || GET_CODE (src) != REG
2147 || GET_MODE (src) != SImode
2148 || ! register_is_ok_for_epilogue (src, SImode))
2149 return 0;
2151 plus = XEXP (dest, 0);
2153 if ( GET_CODE (plus) != PLUS
2154 || GET_CODE (XEXP (plus, 0)) != REG
2155 || GET_MODE (XEXP (plus, 0)) != SImode
2156 || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2157 || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2158 return 0;
2160 /* If the register is being pushed somewhere other than the stack
2161 space just acquired by the first operand then abandon this quest.
2162 Note: the test is <= because both values are negative. */
2163 if (INTVAL (XEXP (plus, 1))
2164 <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
2166 return 0;
2170 /* Make sure that the last entry in the vector is a clobber. */
2171 vector_element = XVECEXP (op, 0, i);
2173 if (GET_CODE (vector_element) != CLOBBER
2174 || GET_CODE (XEXP (vector_element, 0)) != REG
2175 || REGNO (XEXP (vector_element, 0)) != 10)
2176 return 0;
2178 return 1;
2181 /* Construct a JARL instruction to a routine that will perform the equivalent
2182 of the RTL passed as a parameter. This RTL is a function prologue that
2183 saves some of the registers r20 - r31 onto the stack, and possibly acquires
2184 some stack space as well. The code has already verified that the RTL
2185 matches these requirements. */
2186 char *
2187 construct_save_jarl (op)
2188 rtx op;
2190 int count = XVECLEN (op, 0);
2191 int stack_bytes;
2192 unsigned long int mask;
2193 unsigned long int first;
2194 unsigned long int last;
2195 int i;
2196 static char buff [100]; /* XXX */
2198 if (count <= 2)
2200 error ("Bogus JARL construction: %d\n", count);
2201 return NULL;
2204 /* Paranoia. */
2205 if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2206 abort ();
2207 if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2208 abort ();
2209 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2210 abort ();
2211 if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2212 abort ();
2214 /* Work out how many bytes to push onto the stack after storing the
2215 registers. */
2216 stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2218 /* Each push will put 4 bytes from the stack... */
2219 stack_bytes += (count - 2) * 4;
2221 /* Make sure that the amount we are popping either 0 or 16 bytes. */
2222 if (stack_bytes != 0 && stack_bytes != -16)
2224 error ("Bad amount of stack space removal: %d", stack_bytes);
2225 return NULL;
2228 /* Now compute the bit mask of registers to push. */
2229 mask = 0;
2230 for (i = 1; i < count - 1; i++)
2232 rtx vector_element = XVECEXP (op, 0, i);
2234 if (GET_CODE (vector_element) != SET)
2235 abort ();
2236 if (GET_CODE (SET_SRC (vector_element)) != REG)
2237 abort ();
2238 if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2239 abort ();
2241 mask |= 1 << REGNO (SET_SRC (vector_element));
2244 /* Scan for the first register to push. */
2245 for (first = 0; first < 32; first++)
2247 if (mask & (1 << first))
2248 break;
2251 if (first >= 32)
2252 abort ();
2254 /* Discover the last register to push. */
2255 if (mask & (1 << 31))
2257 if (stack_bytes != -16)
2258 abort ();
2260 last = 31;
2262 else
2264 if (stack_bytes != 0)
2265 abort ();
2266 if ((mask & (1 << 29)) == 0)
2267 abort ();
2269 last = 29;
2272 /* Note, it is possible to have gaps in the register mask.
2273 We ignore this here, and generate a JARL anyway. We will
2274 be pushing more registers thatn is strictly necessary, but
2275 it does save code space. */
2277 if (first == last)
2278 sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2279 else
2280 sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2281 reg_names [last]);
2283 return buff;