Replace occurrences of #elif with #if...#endif.
[official-gcc.git] / gcc / config / i960 / i960.c
blob8c7c8d40b1244d1db05cc584ce8070d2ba7e548a
1 /* Subroutines used for code generation on intel 80960.
2 Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Steven McGeady, Intel Corp.
5 Additional Work by Glenn Colon-Bonet, Jonathan Shapiro, Andy Wilson
6 Converted to GCC 2.0 by Jim Wilson and Michael Tiemann, Cygnus Support.
8 This file is part of GNU CC.
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include <math.h>
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "expr.h"
39 #include "except.h"
40 #include "function.h"
41 #include "recog.h"
42 #include "toplev.h"
43 #include "cpplib.h"
44 #include "c-pragma.h"
45 #include "c-lex.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
50 static void i960_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
51 static void i960_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
53 /* Save the operands last given to a compare for use when we
54 generate a scc or bcc insn. */
56 rtx i960_compare_op0, i960_compare_op1;
58 /* Used to implement #pragma align/noalign. Initialized by OVERRIDE_OPTIONS
59 macro in i960.h. */
61 int i960_maxbitalignment;
62 int i960_last_maxbitalignment;
64 /* Used to implement switching between MEM and ALU insn types, for better
65 C series performance. */
67 enum insn_types i960_last_insn_type;
69 /* The leaf-procedure return register. Set only if this is a leaf routine. */
71 static int i960_leaf_ret_reg;
73 /* True if replacing tail calls with jumps is OK. */
75 static int tail_call_ok;
77 /* A string containing a list of insns to emit in the epilogue so as to
78 restore all registers saved by the prologue. Created by the prologue
79 code as it saves registers away. */
81 char epilogue_string[1000];
83 /* A unique number (per function) for return labels. */
85 static int ret_label = 0;
87 /* This is true if FNDECL is either a varargs or a stdarg function.
88 This is used to help identify functions that use an argument block. */
90 #define VARARGS_STDARG_FUNCTION(FNDECL) \
91 ((TYPE_ARG_TYPES (TREE_TYPE (FNDECL)) != 0 \
92 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (FNDECL)))) != void_type_node)) \
93 || current_function_varargs)
95 /* Initialize the GCC target structure. */
96 #undef TARGET_ASM_FUNCTION_PROLOGUE
97 #define TARGET_ASM_FUNCTION_PROLOGUE i960_output_function_prologue
98 #undef TARGET_ASM_FUNCTION_EPILOGUE
99 #define TARGET_ASM_FUNCTION_EPILOGUE i960_output_function_epilogue
101 struct gcc_target targetm = TARGET_INITIALIZER;
103 /* Initialize variables before compiling any files. */
105 void
106 i960_initialize ()
108 if (TARGET_IC_COMPAT2_0)
110 i960_maxbitalignment = 8;
111 i960_last_maxbitalignment = 128;
113 else
115 i960_maxbitalignment = 128;
116 i960_last_maxbitalignment = 8;
120 /* Return true if OP can be used as the source of an fp move insn. */
123 fpmove_src_operand (op, mode)
124 rtx op;
125 enum machine_mode mode;
127 return (GET_CODE (op) == CONST_DOUBLE || general_operand (op, mode));
130 #if 0
131 /* Return true if OP is a register or zero. */
134 reg_or_zero_operand (op, mode)
135 rtx op;
136 enum machine_mode mode;
138 return register_operand (op, mode) || op == const0_rtx;
140 #endif
142 /* Return truth value of whether OP can be used as an operands in a three
143 address arithmetic insn (such as add %o1,7,%l2) of mode MODE. */
146 arith_operand (op, mode)
147 rtx op;
148 enum machine_mode mode;
150 return (register_operand (op, mode) || literal (op, mode));
153 /* Return truth value of whether OP can be used as an operands in a three
154 address logic insn, possibly complementing OP, of mode MODE. */
157 logic_operand (op, mode)
158 rtx op;
159 enum machine_mode mode;
161 return (register_operand (op, mode)
162 || (GET_CODE (op) == CONST_INT
163 && INTVAL(op) >= -32 && INTVAL(op) < 32));
166 /* Return true if OP is a register or a valid floating point literal. */
169 fp_arith_operand (op, mode)
170 rtx op;
171 enum machine_mode mode;
173 return (register_operand (op, mode) || fp_literal (op, mode));
176 /* Return true if OP is a register or a valid signed integer literal. */
179 signed_arith_operand (op, mode)
180 rtx op;
181 enum machine_mode mode;
183 return (register_operand (op, mode) || signed_literal (op, mode));
186 /* Return truth value of whether OP is a integer which fits the
187 range constraining immediate operands in three-address insns. */
190 literal (op, mode)
191 rtx op;
192 enum machine_mode mode ATTRIBUTE_UNUSED;
194 return ((GET_CODE (op) == CONST_INT) && INTVAL(op) >= 0 && INTVAL(op) < 32);
197 /* Return true if OP is a float constant of 1. */
200 fp_literal_one (op, mode)
201 rtx op;
202 enum machine_mode mode;
204 return (TARGET_NUMERICS && mode == GET_MODE (op) && op == CONST1_RTX (mode));
207 /* Return true if OP is a float constant of 0. */
210 fp_literal_zero (op, mode)
211 rtx op;
212 enum machine_mode mode;
214 return (TARGET_NUMERICS && mode == GET_MODE (op) && op == CONST0_RTX (mode));
217 /* Return true if OP is a valid floating point literal. */
220 fp_literal(op, mode)
221 rtx op;
222 enum machine_mode mode;
224 return fp_literal_zero (op, mode) || fp_literal_one (op, mode);
227 /* Return true if OP is a valid signed immediate constant. */
230 signed_literal(op, mode)
231 rtx op;
232 enum machine_mode mode ATTRIBUTE_UNUSED;
234 return ((GET_CODE (op) == CONST_INT) && INTVAL(op) > -32 && INTVAL(op) < 32);
237 /* Return truth value of statement that OP is a symbolic memory
238 operand of mode MODE. */
241 symbolic_memory_operand (op, mode)
242 rtx op;
243 enum machine_mode mode ATTRIBUTE_UNUSED;
245 if (GET_CODE (op) == SUBREG)
246 op = SUBREG_REG (op);
247 if (GET_CODE (op) != MEM)
248 return 0;
249 op = XEXP (op, 0);
250 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
251 || GET_CODE (op) == HIGH || GET_CODE (op) == LABEL_REF);
254 /* Return truth value of whether OP is EQ or NE. */
257 eq_or_neq (op, mode)
258 rtx op;
259 enum machine_mode mode ATTRIBUTE_UNUSED;
261 return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
264 /* OP is an integer register or a constant. */
267 arith32_operand (op, mode)
268 rtx op;
269 enum machine_mode mode;
271 if (register_operand (op, mode))
272 return 1;
273 return (CONSTANT_P (op));
276 /* Return true if OP is an integer constant which is a power of 2. */
279 power2_operand (op,mode)
280 rtx op;
281 enum machine_mode mode ATTRIBUTE_UNUSED;
283 if (GET_CODE (op) != CONST_INT)
284 return 0;
286 return exact_log2 (INTVAL (op)) >= 0;
289 /* Return true if OP is an integer constant which is the complement of a
290 power of 2. */
293 cmplpower2_operand (op, mode)
294 rtx op;
295 enum machine_mode mode ATTRIBUTE_UNUSED;
297 if (GET_CODE (op) != CONST_INT)
298 return 0;
300 return exact_log2 (~ INTVAL (op)) >= 0;
303 /* If VAL has only one bit set, return the index of that bit. Otherwise
304 return -1. */
307 bitpos (val)
308 unsigned int val;
310 register int i;
312 for (i = 0; val != 0; i++, val >>= 1)
314 if (val & 1)
316 if (val != 1)
317 return -1;
318 return i;
321 return -1;
324 /* Return non-zero if OP is a mask, i.e. all one bits are consecutive.
325 The return value indicates how many consecutive non-zero bits exist
326 if this is a mask. This is the same as the next function, except that
327 it does not indicate what the start and stop bit positions are. */
330 is_mask (val)
331 unsigned int val;
333 register int start, end, i;
335 start = -1;
336 for (i = 0; val != 0; val >>= 1, i++)
338 if (val & 1)
340 if (start < 0)
341 start = i;
343 end = i;
344 continue;
346 /* Still looking for the first bit. */
347 if (start < 0)
348 continue;
350 /* We've seen the start of a bit sequence, and now a zero. There
351 must be more one bits, otherwise we would have exited the loop.
352 Therefore, it is not a mask. */
353 if (val)
354 return 0;
357 /* The bit string has ones from START to END bit positions only. */
358 return end - start + 1;
361 /* If VAL is a mask, then return nonzero, with S set to the starting bit
362 position and E set to the ending bit position of the mask. The return
363 value indicates how many consecutive bits exist in the mask. This is
364 the same as the previous function, except that it also indicates the
365 start and end bit positions of the mask. */
368 bitstr (val, s, e)
369 unsigned int val;
370 int *s, *e;
372 register int start, end, i;
374 start = -1;
375 end = -1;
376 for (i = 0; val != 0; val >>= 1, i++)
378 if (val & 1)
380 if (start < 0)
381 start = i;
383 end = i;
384 continue;
387 /* Still looking for the first bit. */
388 if (start < 0)
389 continue;
391 /* We've seen the start of a bit sequence, and now a zero. There
392 must be more one bits, otherwise we would have exited the loop.
393 Therefor, it is not a mask. */
394 if (val)
396 start = -1;
397 end = -1;
398 break;
402 /* The bit string has ones from START to END bit positions only. */
403 *s = start;
404 *e = end;
405 return ((start < 0) ? 0 : end - start + 1);
408 /* Return the machine mode to use for a comparison. */
410 enum machine_mode
411 select_cc_mode (op, x)
412 RTX_CODE op;
413 rtx x ATTRIBUTE_UNUSED;
415 if (op == GTU || op == LTU || op == GEU || op == LEU)
416 return CC_UNSmode;
417 return CCmode;
420 /* X and Y are two things to compare using CODE. Emit the compare insn and
421 return the rtx for register 36 in the proper mode. */
424 gen_compare_reg (code, x, y)
425 enum rtx_code code;
426 rtx x, y;
428 rtx cc_reg;
429 enum machine_mode ccmode = SELECT_CC_MODE (code, x, y);
430 enum machine_mode mode
431 = GET_MODE (x) == VOIDmode ? GET_MODE (y) : GET_MODE (x);
433 if (mode == SImode)
435 if (! arith_operand (x, mode))
436 x = force_reg (SImode, x);
437 if (! arith_operand (y, mode))
438 y = force_reg (SImode, y);
441 cc_reg = gen_rtx_REG (ccmode, 36);
442 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
443 gen_rtx_COMPARE (ccmode, x, y)));
445 return cc_reg;
448 /* For the i960, REG is cost 1, REG+immed CONST is cost 2, REG+REG is cost 2,
449 REG+nonimmed CONST is cost 4. REG+SYMBOL_REF, SYMBOL_REF, and similar
450 are 4. Indexed addresses are cost 6. */
452 /* ??? Try using just RTX_COST, i.e. not defining ADDRESS_COST. */
455 i960_address_cost (x)
456 rtx x;
458 #if 0
459 /* Handled before calling here. */
460 if (GET_CODE (x) == REG)
461 return 1;
462 #endif
463 /* This is a MEMA operand -- it's free. */
464 if (GET_CODE (x) == CONST_INT
465 && INTVAL (x) >= 0
466 && INTVAL (x) < 4096)
467 return 0;
469 if (GET_CODE (x) == PLUS)
471 rtx base = XEXP (x, 0);
472 rtx offset = XEXP (x, 1);
474 if (GET_CODE (base) == SUBREG)
475 base = SUBREG_REG (base);
476 if (GET_CODE (offset) == SUBREG)
477 offset = SUBREG_REG (offset);
479 if (GET_CODE (base) == REG)
481 if (GET_CODE (offset) == REG)
482 return 2;
483 if (GET_CODE (offset) == CONST_INT)
485 if ((unsigned)INTVAL (offset) < 2047)
486 return 2;
487 return 4;
489 if (CONSTANT_P (offset))
490 return 4;
492 if (GET_CODE (base) == PLUS || GET_CODE (base) == MULT)
493 return 6;
495 /* This is an invalid address. The return value doesn't matter, but
496 for convenience we make this more expensive than anything else. */
497 return 12;
499 if (GET_CODE (x) == MULT)
500 return 6;
502 /* Symbol_refs and other unrecognized addresses are cost 4. */
503 return 4;
506 /* Emit insns to move operands[1] into operands[0].
508 Return 1 if we have written out everything that needs to be done to
509 do the move. Otherwise, return 0 and the caller will emit the move
510 normally. */
513 emit_move_sequence (operands, mode)
514 rtx *operands;
515 enum machine_mode mode;
517 /* We can only store registers to memory. */
519 if (GET_CODE (operands[0]) == MEM && GET_CODE (operands[1]) != REG
520 && (operands[1] != const0_rtx || current_function_args_size
521 || current_function_varargs || current_function_stdarg
522 || rtx_equal_function_value_matters))
523 /* Here we use the same test as movsi+1 pattern -- see i960.md. */
524 operands[1] = force_reg (mode, operands[1]);
526 /* Storing multi-word values in unaligned hard registers to memory may
527 require a scratch since we have to store them a register at a time and
528 adding 4 to the memory address may not yield a valid insn. */
529 /* ??? We don't always need the scratch, but that would complicate things.
530 Maybe later. */
531 /* ??? We must also handle stores to pseudos here, because the pseudo may be
532 replaced with a MEM later. This would be cleaner if we didn't have
533 a separate pattern for unaligned DImode/TImode stores. */
534 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
535 && (GET_CODE (operands[0]) == MEM
536 || (GET_CODE (operands[0]) == REG
537 && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
538 && GET_CODE (operands[1]) == REG
539 && REGNO (operands[1]) < FIRST_PSEUDO_REGISTER
540 && ! HARD_REGNO_MODE_OK (REGNO (operands[1]), mode))
542 emit_insn (gen_rtx_PARALLEL
543 (VOIDmode,
544 gen_rtvec (2,
545 gen_rtx_SET (VOIDmode, operands[0], operands[1]),
546 gen_rtx_CLOBBER (VOIDmode,
547 gen_rtx_SCRATCH (Pmode)))));
548 return 1;
551 return 0;
554 /* Output assembler to move a double word value. */
556 const char *
557 i960_output_move_double (dst, src)
558 rtx dst, src;
560 rtx operands[5];
562 if (GET_CODE (dst) == REG
563 && GET_CODE (src) == REG)
565 if ((REGNO (src) & 1)
566 || (REGNO (dst) & 1))
568 /* We normally copy the low-numbered register first. However, if
569 the second source register is the same as the first destination
570 register, we must copy in the opposite order. */
571 if (REGNO (src) + 1 == REGNO (dst))
572 return "mov %D1,%D0\n\tmov %1,%0";
573 else
574 return "mov %1,%0\n\tmov %D1,%D0";
576 else
577 return "movl %1,%0";
579 else if (GET_CODE (dst) == REG
580 && GET_CODE (src) == CONST_INT
581 && CONST_OK_FOR_LETTER_P (INTVAL (src), 'I'))
583 if (REGNO (dst) & 1)
584 return "mov %1,%0\n\tmov 0,%D0";
585 else
586 return "movl %1,%0";
588 else if (GET_CODE (dst) == REG
589 && GET_CODE (src) == MEM)
591 if (REGNO (dst) & 1)
593 /* One can optimize a few cases here, but you have to be
594 careful of clobbering registers used in the address and
595 edge conditions. */
596 operands[0] = dst;
597 operands[1] = src;
598 operands[2] = gen_rtx_REG (Pmode, REGNO (dst) + 1);
599 operands[3] = gen_rtx_MEM (word_mode, operands[2]);
600 operands[4] = adjust_address (operands[3], word_mode,
601 UNITS_PER_WORD);
602 output_asm_insn
603 ("lda %1,%2\n\tld %3,%0\n\tld %4,%D0", operands);
604 return "";
606 else
607 return "ldl %1,%0";
609 else if (GET_CODE (dst) == MEM
610 && GET_CODE (src) == REG)
612 if (REGNO (src) & 1)
614 operands[0] = dst;
615 operands[1] = adjust_address (dst, word_mode, UNITS_PER_WORD);
616 if (! memory_address_p (word_mode, XEXP (operands[1], 0)))
617 abort ();
618 operands[2] = src;
619 output_asm_insn ("st %2,%0\n\tst %D2,%1", operands);
620 return "";
622 return "stl %1,%0";
624 else
625 abort ();
628 /* Output assembler to move a double word zero. */
630 const char *
631 i960_output_move_double_zero (dst)
632 rtx dst;
634 rtx operands[2];
636 operands[0] = dst;
638 operands[1] = adjust_address (dst, word_mode, 4);
639 output_asm_insn ("st g14,%0\n\tst g14,%1", operands);
641 return "";
644 /* Output assembler to move a quad word value. */
646 const char *
647 i960_output_move_quad (dst, src)
648 rtx dst, src;
650 rtx operands[7];
652 if (GET_CODE (dst) == REG
653 && GET_CODE (src) == REG)
655 if ((REGNO (src) & 3)
656 || (REGNO (dst) & 3))
658 /* We normally copy starting with the low numbered register.
659 However, if there is an overlap such that the first dest reg
660 is <= the last source reg but not < the first source reg, we
661 must copy in the opposite order. */
662 if (REGNO (dst) <= REGNO (src) + 3
663 && REGNO (dst) >= REGNO (src))
664 return "mov %F1,%F0\n\tmov %E1,%E0\n\tmov %D1,%D0\n\tmov %1,%0";
665 else
666 return "mov %1,%0\n\tmov %D1,%D0\n\tmov %E1,%E0\n\tmov %F1,%F0";
668 else
669 return "movq %1,%0";
671 else if (GET_CODE (dst) == REG
672 && GET_CODE (src) == CONST_INT
673 && CONST_OK_FOR_LETTER_P (INTVAL (src), 'I'))
675 if (REGNO (dst) & 3)
676 return "mov %1,%0\n\tmov 0,%D0\n\tmov 0,%E0\n\tmov 0,%F0";
677 else
678 return "movq %1,%0";
680 else if (GET_CODE (dst) == REG
681 && GET_CODE (src) == MEM)
683 if (REGNO (dst) & 3)
685 /* One can optimize a few cases here, but you have to be
686 careful of clobbering registers used in the address and
687 edge conditions. */
688 operands[0] = dst;
689 operands[1] = src;
690 operands[2] = gen_rtx_REG (Pmode, REGNO (dst) + 3);
691 operands[3] = gen_rtx_MEM (word_mode, operands[2]);
692 operands[4]
693 = adjust_address (operands[3], word_mode, UNITS_PER_WORD);
694 operands[5]
695 = adjust_address (operands[4], word_mode, UNITS_PER_WORD);
696 operands[6]
697 = adjust_address (operands[5], word_mode, UNITS_PER_WORD);
698 output_asm_insn ("lda %1,%2\n\tld %3,%0\n\tld %4,%D0\n\tld %5,%E0\n\tld %6,%F0", operands);
699 return "";
701 else
702 return "ldq %1,%0";
704 else if (GET_CODE (dst) == MEM
705 && GET_CODE (src) == REG)
707 if (REGNO (src) & 3)
709 operands[0] = dst;
710 operands[1] = adjust_address (dst, word_mode, UNITS_PER_WORD);
711 operands[2] = adjust_address (dst, word_mode, 2 * UNITS_PER_WORD);
712 operands[3] = adjust_address (dst, word_mode, 3 * UNITS_PER_WORD);
713 if (! memory_address_p (word_mode, XEXP (operands[3], 0)))
714 abort ();
715 operands[4] = src;
716 output_asm_insn ("st %4,%0\n\tst %D4,%1\n\tst %E4,%2\n\tst %F4,%3", operands);
717 return "";
719 return "stq %1,%0";
721 else
722 abort ();
725 /* Output assembler to move a quad word zero. */
727 const char *
728 i960_output_move_quad_zero (dst)
729 rtx dst;
731 rtx operands[4];
733 operands[0] = dst;
735 operands[1] = adjust_address (dst, word_mode, 4);
736 operands[2] = adjust_address (dst, word_mode, 8);
737 operands[3] = adjust_address (dst, word_mode, 12);
738 output_asm_insn ("st g14,%0\n\tst g14,%1\n\tst g14,%2\n\tst g14,%3", operands);
740 return "";
744 /* Emit insns to load a constant to non-floating point registers.
745 Uses several strategies to try to use as few insns as possible. */
747 const char *
748 i960_output_ldconst (dst, src)
749 register rtx dst, src;
751 register int rsrc1;
752 register unsigned rsrc2;
753 enum machine_mode mode = GET_MODE (dst);
754 rtx operands[4];
756 operands[0] = operands[2] = dst;
757 operands[1] = operands[3] = src;
759 /* Anything that isn't a compile time constant, such as a SYMBOL_REF,
760 must be a ldconst insn. */
762 if (GET_CODE (src) != CONST_INT && GET_CODE (src) != CONST_DOUBLE)
764 output_asm_insn ("ldconst %1,%0", operands);
765 return "";
767 else if (mode == XFmode)
769 REAL_VALUE_TYPE d;
770 long value_long[3];
771 int i;
773 if (fp_literal_zero (src, XFmode))
774 return "movt 0,%0";
776 REAL_VALUE_FROM_CONST_DOUBLE (d, src);
777 REAL_VALUE_TO_TARGET_LONG_DOUBLE (d, value_long);
779 output_asm_insn ("# ldconst %1,%0",operands);
781 for (i = 0; i < 3; i++)
783 operands[0] = gen_rtx_REG (SImode, REGNO (dst) + i);
784 operands[1] = GEN_INT (value_long[i]);
785 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
786 operands);
789 return "";
791 else if (mode == DFmode)
793 rtx first, second;
795 if (fp_literal_zero (src, DFmode))
796 return "movl 0,%0";
798 split_double (src, &first, &second);
800 output_asm_insn ("# ldconst %1,%0",operands);
802 operands[0] = gen_rtx_REG (SImode, REGNO (dst));
803 operands[1] = first;
804 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
805 operands);
806 operands[0] = gen_rtx_REG (SImode, REGNO (dst) + 1);
807 operands[1] = second;
808 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
809 operands);
810 return "";
812 else if (mode == SFmode)
814 REAL_VALUE_TYPE d;
815 long value;
817 REAL_VALUE_FROM_CONST_DOUBLE (d, src);
818 REAL_VALUE_TO_TARGET_SINGLE (d, value);
820 output_asm_insn ("# ldconst %1,%0",operands);
821 operands[0] = gen_rtx_REG (SImode, REGNO (dst));
822 operands[1] = GEN_INT (value);
823 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
824 operands);
825 return "";
827 else if (mode == TImode)
829 /* ??? This is currently not handled at all. */
830 abort ();
832 /* Note: lowest order word goes in lowest numbered reg. */
833 rsrc1 = INTVAL (src);
834 if (rsrc1 >= 0 && rsrc1 < 32)
835 return "movq %1,%0";
836 else
837 output_asm_insn ("movq\t0,%0\t# ldconstq %1,%0",operands);
838 /* Go pick up the low-order word. */
840 else if (mode == DImode)
842 rtx upperhalf, lowerhalf, xoperands[2];
844 if (GET_CODE (src) == CONST_DOUBLE || GET_CODE (src) == CONST_INT)
845 split_double (src, &lowerhalf, &upperhalf);
847 else
848 abort ();
850 /* Note: lowest order word goes in lowest numbered reg. */
851 /* Numbers from 0 to 31 can be handled with a single insn. */
852 rsrc1 = INTVAL (lowerhalf);
853 if (upperhalf == const0_rtx && rsrc1 >= 0 && rsrc1 < 32)
854 return "movl %1,%0";
856 /* Output the upper half with a recursive call. */
857 xoperands[0] = gen_rtx_REG (SImode, REGNO (dst) + 1);
858 xoperands[1] = upperhalf;
859 output_asm_insn (i960_output_ldconst (xoperands[0], xoperands[1]),
860 xoperands);
861 /* The lower word is emitted as normally. */
863 else
865 rsrc1 = INTVAL (src);
866 if (mode == QImode)
868 if (rsrc1 > 0xff)
869 rsrc1 &= 0xff;
871 else if (mode == HImode)
873 if (rsrc1 > 0xffff)
874 rsrc1 &= 0xffff;
878 if (rsrc1 >= 0)
880 /* ldconst 0..31,X -> mov 0..31,X */
881 if (rsrc1 < 32)
883 if (i960_last_insn_type == I_TYPE_REG && TARGET_C_SERIES)
884 return "lda %1,%0";
885 return "mov %1,%0";
888 /* ldconst 32..63,X -> add 31,nn,X */
889 if (rsrc1 < 63)
891 if (i960_last_insn_type == I_TYPE_REG && TARGET_C_SERIES)
892 return "lda %1,%0";
893 operands[1] = GEN_INT (rsrc1 - 31);
894 output_asm_insn ("addo\t31,%1,%0\t# ldconst %3,%0", operands);
895 return "";
898 else if (rsrc1 < 0)
900 /* ldconst -1..-31 -> sub 0,0..31,X */
901 if (rsrc1 >= -31)
903 /* return 'sub -(%1),0,%0' */
904 operands[1] = GEN_INT (- rsrc1);
905 output_asm_insn ("subo\t%1,0,%0\t# ldconst %3,%0", operands);
906 return "";
909 /* ldconst -32 -> not 31,X */
910 if (rsrc1 == -32)
912 operands[1] = GEN_INT (~rsrc1);
913 output_asm_insn ("not\t%1,%0 # ldconst %3,%0", operands);
914 return "";
918 /* If const is a single bit. */
919 if (bitpos (rsrc1) >= 0)
921 operands[1] = GEN_INT (bitpos (rsrc1));
922 output_asm_insn ("setbit\t%1,0,%0\t# ldconst %3,%0", operands);
923 return "";
926 /* If const is a bit string of less than 6 bits (1..31 shifted). */
927 if (is_mask (rsrc1))
929 int s, e;
931 if (bitstr (rsrc1, &s, &e) < 6)
933 rsrc2 = ((unsigned int) rsrc1) >> s;
934 operands[1] = GEN_INT (rsrc2);
935 operands[2] = GEN_INT (s);
936 output_asm_insn ("shlo\t%2,%1,%0\t# ldconst %3,%0", operands);
937 return "";
941 /* Unimplemented cases:
942 const is in range 0..31 but rotated around end of word:
943 ror 31,3,g0 -> ldconst 0xe0000003,g0
945 and any 2 instruction cases that might be worthwhile */
947 output_asm_insn ("ldconst %1,%0", operands);
948 return "";
951 /* Determine if there is an opportunity for a bypass optimization.
952 Bypass succeeds on the 960K* if the destination of the previous
953 instruction is the second operand of the current instruction.
954 Bypass always succeeds on the C*.
956 Return 1 if the pattern should interchange the operands.
958 CMPBR_FLAG is true if this is for a compare-and-branch insn.
959 OP1 and OP2 are the two source operands of a 3 operand insn. */
962 i960_bypass (insn, op1, op2, cmpbr_flag)
963 register rtx insn, op1, op2;
964 int cmpbr_flag;
966 register rtx prev_insn, prev_dest;
968 if (TARGET_C_SERIES)
969 return 0;
971 /* Can't do this if op1 isn't a register. */
972 if (! REG_P (op1))
973 return 0;
975 /* Can't do this for a compare-and-branch if both ops aren't regs. */
976 if (cmpbr_flag && ! REG_P (op2))
977 return 0;
979 prev_insn = prev_real_insn (insn);
981 if (prev_insn && GET_CODE (prev_insn) == INSN
982 && GET_CODE (PATTERN (prev_insn)) == SET)
984 prev_dest = SET_DEST (PATTERN (prev_insn));
985 if ((GET_CODE (prev_dest) == REG && REGNO (prev_dest) == REGNO (op1))
986 || (GET_CODE (prev_dest) == SUBREG
987 && GET_CODE (SUBREG_REG (prev_dest)) == REG
988 && REGNO (SUBREG_REG (prev_dest)) == REGNO (op1)))
989 return 1;
991 return 0;
994 /* Output the code which declares the function name. This also handles
995 leaf routines, which have special requirements, and initializes some
996 global variables. */
998 void
999 i960_function_name_declare (file, name, fndecl)
1000 FILE *file;
1001 const char *name;
1002 tree fndecl;
1004 register int i, j;
1005 int leaf_proc_ok;
1006 rtx insn;
1008 /* Increment global return label. */
1010 ret_label++;
1012 /* Compute whether tail calls and leaf routine optimizations can be performed
1013 for this function. */
1015 if (TARGET_TAILCALL)
1016 tail_call_ok = 1;
1017 else
1018 tail_call_ok = 0;
1020 if (TARGET_LEAFPROC)
1021 leaf_proc_ok = 1;
1022 else
1023 leaf_proc_ok = 0;
1025 /* Even if nobody uses extra parms, can't have leafproc or tail calls if
1026 argblock, because argblock uses g14 implicitly. */
1028 if (current_function_args_size != 0 || VARARGS_STDARG_FUNCTION (fndecl))
1030 tail_call_ok = 0;
1031 leaf_proc_ok = 0;
1034 /* See if caller passes in an address to return value. */
1036 if (aggregate_value_p (DECL_RESULT (fndecl)))
1038 tail_call_ok = 0;
1039 leaf_proc_ok = 0;
1042 /* Can not use tail calls or make this a leaf routine if there is a non
1043 zero frame size. */
1045 if (get_frame_size () != 0)
1046 leaf_proc_ok = 0;
1048 /* I don't understand this condition, and do not think that it is correct.
1049 Apparently this is just checking whether the frame pointer is used, and
1050 we can't trust regs_ever_live[fp] since it is (almost?) always set. */
1052 if (tail_call_ok)
1053 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1054 if (GET_CODE (insn) == INSN
1055 && reg_mentioned_p (frame_pointer_rtx, insn))
1057 tail_call_ok = 0;
1058 break;
1061 /* Check for CALL insns. Can not be a leaf routine if there are any. */
1063 if (leaf_proc_ok)
1064 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1065 if (GET_CODE (insn) == CALL_INSN)
1067 leaf_proc_ok = 0;
1068 break;
1071 /* Can not be a leaf routine if any non-call clobbered registers are
1072 used in this function. */
1074 if (leaf_proc_ok)
1075 for (i = 0, j = 0; i < FIRST_PSEUDO_REGISTER; i++)
1076 if (regs_ever_live[i]
1077 && ((! call_used_regs[i]) || (i > 7 && i < 12)))
1079 /* Global registers. */
1080 if (i < 16 && i > 7 && i != 13)
1081 leaf_proc_ok = 0;
1082 /* Local registers. */
1083 else if (i < 32)
1084 leaf_proc_ok = 0;
1087 /* Now choose a leaf return register, if we can find one, and if it is
1088 OK for this to be a leaf routine. */
1090 i960_leaf_ret_reg = -1;
1092 if (optimize && leaf_proc_ok)
1094 for (i960_leaf_ret_reg = -1, i = 0; i < 8; i++)
1095 if (regs_ever_live[i] == 0)
1097 i960_leaf_ret_reg = i;
1098 regs_ever_live[i] = 1;
1099 break;
1103 /* Do this after choosing the leaf return register, so it will be listed
1104 if one was chosen. */
1106 fprintf (file, "\t# Function '%s'\n", (name[0] == '*' ? &name[1] : name));
1107 fprintf (file, "\t# Registers used: ");
1109 for (i = 0, j = 0; i < FIRST_PSEUDO_REGISTER; i++)
1111 if (regs_ever_live[i])
1113 fprintf (file, "%s%s ", reg_names[i], call_used_regs[i] ? "" : "*");
1115 if (i > 15 && j == 0)
1117 fprintf (file,"\n\t#\t\t ");
1118 j++;
1123 fprintf (file, "\n");
1125 if (i960_leaf_ret_reg >= 0)
1127 /* Make it a leaf procedure. */
1129 if (TREE_PUBLIC (fndecl))
1130 fprintf (file,"\t.globl\t%s.lf\n", (name[0] == '*' ? &name[1] : name));
1132 fprintf (file, "\t.leafproc\t");
1133 assemble_name (file, name);
1134 fprintf (file, ",%s.lf\n", (name[0] == '*' ? &name[1] : name));
1135 ASM_OUTPUT_LABEL (file, name);
1136 fprintf (file, "\tlda Li960R%d,g14\n", ret_label);
1137 fprintf (file, "%s.lf:\n", (name[0] == '*' ? &name[1] : name));
1138 fprintf (file, "\tmov g14,g%d\n", i960_leaf_ret_reg);
1140 if (TARGET_C_SERIES)
1142 fprintf (file, "\tlda 0,g14\n");
1143 i960_last_insn_type = I_TYPE_MEM;
1145 else
1147 fprintf (file, "\tmov 0,g14\n");
1148 i960_last_insn_type = I_TYPE_REG;
1151 else
1153 ASM_OUTPUT_LABEL (file, name);
1154 i960_last_insn_type = I_TYPE_CTRL;
1158 /* Compute and return the frame size. */
1161 compute_frame_size (size)
1162 int size;
1164 int actual_fsize;
1165 int outgoing_args_size = current_function_outgoing_args_size;
1167 /* The STARTING_FRAME_OFFSET is totally hidden to us as far
1168 as size is concerned. */
1169 actual_fsize = (size + 15) & -16;
1170 actual_fsize += (outgoing_args_size + 15) & -16;
1172 return actual_fsize;
1175 /* Here register group is range of registers which can be moved by
1176 one i960 instruction. */
1178 struct reg_group
1180 char start_reg;
1181 char length;
1184 static int i960_form_reg_groups PARAMS ((int, int, int *, int, struct reg_group *));
1185 static int i960_reg_group_compare PARAMS ((const void *, const void *));
1186 static int i960_split_reg_group PARAMS ((struct reg_group *, int, int));
1187 static void i960_arg_size_and_align PARAMS ((enum machine_mode, tree, int *, int *));
1189 /* The following functions forms the biggest as possible register
1190 groups with registers in STATE. REGS contain states of the
1191 registers in range [start, finish_reg). The function returns the
1192 number of groups formed. */
1193 static int
1194 i960_form_reg_groups (start_reg, finish_reg, regs, state, reg_groups)
1195 int start_reg;
1196 int finish_reg;
1197 int *regs;
1198 int state;
1199 struct reg_group *reg_groups;
1201 int i;
1202 int nw = 0;
1204 for (i = start_reg; i < finish_reg; )
1206 if (regs [i] != state)
1208 i++;
1209 continue;
1211 else if (i % 2 != 0 || regs [i + 1] != state)
1212 reg_groups [nw].length = 1;
1213 else if (i % 4 != 0 || regs [i + 2] != state)
1214 reg_groups [nw].length = 2;
1215 else if (regs [i + 3] != state)
1216 reg_groups [nw].length = 3;
1217 else
1218 reg_groups [nw].length = 4;
1219 reg_groups [nw].start_reg = i;
1220 i += reg_groups [nw].length;
1221 nw++;
1223 return nw;
1226 /* We sort register winodws in descending order by length. */
1227 static int
1228 i960_reg_group_compare (group1, group2)
1229 const void *group1;
1230 const void *group2;
1232 const struct reg_group *w1 = group1;
1233 const struct reg_group *w2 = group2;
1235 if (w1->length > w2->length)
1236 return -1;
1237 else if (w1->length < w2->length)
1238 return 1;
1239 else
1240 return 0;
1243 /* Split the first register group in REG_GROUPS on subgroups one of
1244 which will contain SUBGROUP_LENGTH registers. The function
1245 returns new number of winodws. */
1246 static int
1247 i960_split_reg_group (reg_groups, nw, subgroup_length)
1248 struct reg_group *reg_groups;
1249 int nw;
1250 int subgroup_length;
1252 if (subgroup_length < reg_groups->length - subgroup_length)
1253 /* This guarantees correct alignments of the two subgroups for
1254 i960 (see spliting for the group length 2, 3, 4). More
1255 generalized algorithm would require splitting the group more
1256 two subgroups. */
1257 subgroup_length = reg_groups->length - subgroup_length;
1258 /* More generalized algorithm would require to try merging
1259 subgroups here. But in case i960 it always results in failure
1260 because of register group alignment. */
1261 reg_groups[nw].length = reg_groups->length - subgroup_length;
1262 reg_groups[nw].start_reg = reg_groups->start_reg + subgroup_length;
1263 nw++;
1264 reg_groups->length = subgroup_length;
1265 qsort (reg_groups, nw, sizeof (struct reg_group), i960_reg_group_compare);
1266 return nw;
1269 /* Output code for the function prologue. */
1271 static void
1272 i960_output_function_prologue (file, size)
1273 FILE *file;
1274 HOST_WIDE_INT size;
1276 register int i, j, nr;
1277 int n_saved_regs = 0;
1278 int n_remaining_saved_regs;
1279 HOST_WIDE_INT lvar_size;
1280 HOST_WIDE_INT actual_fsize, offset;
1281 int gnw, lnw;
1282 struct reg_group *g, *l;
1283 char tmpstr[1000];
1284 /* -1 if reg must be saved on proc entry, 0 if available, 1 if saved
1285 somewhere. */
1286 int regs[FIRST_PSEUDO_REGISTER];
1287 /* All global registers (which must be saved) divided by groups. */
1288 struct reg_group global_reg_groups [16];
1289 /* All local registers (which are available) divided by groups. */
1290 struct reg_group local_reg_groups [16];
1293 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1294 if (regs_ever_live[i]
1295 && ((! call_used_regs[i]) || (i > 7 && i < 12))
1296 /* No need to save the static chain pointer. */
1297 && ! (i == STATIC_CHAIN_REGNUM && current_function_needs_context))
1299 regs[i] = -1;
1300 /* Count global registers that need saving. */
1301 if (i < 16)
1302 n_saved_regs++;
1304 else
1305 regs[i] = 0;
1307 n_remaining_saved_regs = n_saved_regs;
1309 epilogue_string[0] = '\0';
1311 if (profile_flag || profile_block_flag)
1313 /* When profiling, we may use registers 20 to 27 to save arguments, so
1314 they can't be used here for saving globals. J is the number of
1315 argument registers the mcount call will save. */
1316 for (j = 7; j >= 0 && ! regs_ever_live[j]; j--)
1319 for (i = 20; i <= j + 20; i++)
1320 regs[i] = -1;
1323 gnw = i960_form_reg_groups (0, 16, regs, -1, global_reg_groups);
1324 lnw = i960_form_reg_groups (19, 32, regs, 0, local_reg_groups);
1325 qsort (global_reg_groups, gnw, sizeof (struct reg_group),
1326 i960_reg_group_compare);
1327 qsort (local_reg_groups, lnw, sizeof (struct reg_group),
1328 i960_reg_group_compare);
1329 for (g = global_reg_groups, l = local_reg_groups; lnw != 0 && gnw != 0;)
1331 if (g->length == l->length)
1333 fprintf (file, "\tmov%s %s,%s\n",
1334 ((g->length == 4) ? "q" :
1335 (g->length == 3) ? "t" :
1336 (g->length == 2) ? "l" : ""),
1337 reg_names[(unsigned char) g->start_reg],
1338 reg_names[(unsigned char) l->start_reg]);
1339 sprintf (tmpstr, "\tmov%s %s,%s\n",
1340 ((g->length == 4) ? "q" :
1341 (g->length == 3) ? "t" :
1342 (g->length == 2) ? "l" : ""),
1343 reg_names[(unsigned char) l->start_reg],
1344 reg_names[(unsigned char) g->start_reg]);
1345 strcat (epilogue_string, tmpstr);
1346 n_remaining_saved_regs -= g->length;
1347 for (i = 0; i < g->length; i++)
1349 regs [i + g->start_reg] = 1;
1350 regs [i + l->start_reg] = -1;
1351 regs_ever_live [i + l->start_reg] = 1;
1353 g++;
1354 l++;
1355 gnw--;
1356 lnw--;
1358 else if (g->length > l->length)
1359 gnw = i960_split_reg_group (g, gnw, l->length);
1360 else
1361 lnw = i960_split_reg_group (l, lnw, g->length);
1364 actual_fsize = compute_frame_size (size) + 4 * n_remaining_saved_regs;
1365 #if 0
1366 /* ??? The 1.2.1 compiler does this also. This is meant to round the frame
1367 size up to the nearest multiple of 16. I don't know whether this is
1368 necessary, or even desirable.
1370 The frame pointer must be aligned, but the call instruction takes care of
1371 that. If we leave the stack pointer unaligned, we may save a little on
1372 dynamic stack allocation. And we don't lose, at least according to the
1373 i960CA manual. */
1374 actual_fsize = (actual_fsize + 15) & ~0xF;
1375 #endif
1377 /* Check stack limit if necessary. */
1378 if (current_function_limit_stack)
1380 rtx min_stack = stack_limit_rtx;
1381 if (actual_fsize != 0)
1382 min_stack = plus_constant (stack_limit_rtx, -actual_fsize);
1384 /* Now, emulate a little bit of reload. We want to turn 'min_stack'
1385 into an arith_operand. Use register 20 as the temporary. */
1386 if (legitimate_address_p (Pmode, min_stack, 1)
1387 && !arith_operand (min_stack, Pmode))
1389 rtx tmp = gen_rtx_MEM (Pmode, min_stack);
1390 fputs ("\tlda\t", file);
1391 i960_print_operand (file, tmp, 0);
1392 fputs (",r4\n", file);
1393 min_stack = gen_rtx_REG (Pmode, 20);
1395 if (arith_operand (min_stack, Pmode))
1397 fputs ("\tcmpo\tsp,", file);
1398 i960_print_operand (file, min_stack, 0);
1399 fputs ("\n\tfaultge.f\n", file);
1401 else
1402 warning ("stack limit expression is not supported");
1405 /* Allocate space for register save and locals. */
1406 if (actual_fsize > 0)
1408 if (actual_fsize < 32)
1409 fprintf (file, "\taddo %d,sp,sp\n", actual_fsize);
1410 else
1411 fprintf (file, "\tlda\t%d(sp),sp\n", actual_fsize);
1414 /* Take hardware register save area created by the call instruction
1415 into account, but store them before the argument block area. */
1416 lvar_size = actual_fsize - compute_frame_size (0) - n_remaining_saved_regs * 4;
1417 offset = STARTING_FRAME_OFFSET + lvar_size;
1418 /* Save registers on stack if needed. */
1419 /* ??? Is it worth to use the same algorithm as one for saving
1420 global registers in local registers? */
1421 for (i = 0, j = n_remaining_saved_regs; j > 0 && i < 16; i++)
1423 if (regs[i] != -1)
1424 continue;
1426 nr = 1;
1428 if (i <= 14 && i % 2 == 0 && regs[i+1] == -1 && offset % 2 == 0)
1429 nr = 2;
1431 if (nr == 2 && i <= 12 && i % 4 == 0 && regs[i+2] == -1
1432 && offset % 4 == 0)
1433 nr = 3;
1435 if (nr == 3 && regs[i+3] == -1)
1436 nr = 4;
1438 fprintf (file,"\tst%s %s,%d(fp)\n",
1439 ((nr == 4) ? "q" :
1440 (nr == 3) ? "t" :
1441 (nr == 2) ? "l" : ""),
1442 reg_names[i], offset);
1443 sprintf (tmpstr,"\tld%s %d(fp),%s\n",
1444 ((nr == 4) ? "q" :
1445 (nr == 3) ? "t" :
1446 (nr == 2) ? "l" : ""),
1447 offset, reg_names[i]);
1448 strcat (epilogue_string, tmpstr);
1449 i += nr-1;
1450 j -= nr;
1451 offset += nr * 4;
1454 if (actual_fsize == 0)
1455 return;
1457 fprintf (file, "\t#Prologue stats:\n");
1458 fprintf (file, "\t# Total Frame Size: %d bytes\n", actual_fsize);
1460 if (lvar_size)
1461 fprintf (file, "\t# Local Variable Size: %d bytes\n", lvar_size);
1462 if (n_saved_regs)
1463 fprintf (file, "\t# Register Save Size: %d regs, %d bytes\n",
1464 n_saved_regs, n_saved_regs * 4);
1465 fprintf (file, "\t#End Prologue#\n");
1468 /* Output code for the function profiler. */
1470 void
1471 output_function_profiler (file, labelno)
1472 FILE *file;
1473 int labelno;
1475 /* The last used parameter register. */
1476 int last_parm_reg;
1477 int i, j, increment;
1478 int varargs_stdarg_function
1479 = VARARGS_STDARG_FUNCTION (current_function_decl);
1481 /* Figure out the last used parameter register. The proper thing to do
1482 is to walk incoming args of the function. A function might have live
1483 parameter registers even if it has no incoming args. Note that we
1484 don't have to save parameter registers g8 to g11 because they are
1485 call preserved. */
1487 /* See also output_function_prologue, which tries to use local registers
1488 for preserved call-saved global registers. */
1490 for (last_parm_reg = 7;
1491 last_parm_reg >= 0 && ! regs_ever_live[last_parm_reg];
1492 last_parm_reg--)
1495 /* Save parameter registers in regs r4 (20) to r11 (27). */
1497 for (i = 0, j = 4; i <= last_parm_reg; i += increment, j += increment)
1499 if (i % 4 == 0 && (last_parm_reg - i) >= 3)
1500 increment = 4;
1501 else if (i % 4 == 0 && (last_parm_reg - i) >= 2)
1502 increment = 3;
1503 else if (i % 2 == 0 && (last_parm_reg - i) >= 1)
1504 increment = 2;
1505 else
1506 increment = 1;
1508 fprintf (file, "\tmov%s g%d,r%d\n",
1509 (increment == 4 ? "q" : increment == 3 ? "t"
1510 : increment == 2 ? "l": ""), i, j);
1513 /* If this function uses the arg pointer, then save it in r3 and then
1514 set it to zero. */
1516 if (current_function_args_size != 0 || varargs_stdarg_function)
1517 fprintf (file, "\tmov g14,r3\n\tmov 0,g14\n");
1519 /* Load location address into g0 and call mcount. */
1521 fprintf (file, "\tlda\tLP%d,g0\n\tcallx\tmcount\n", labelno);
1523 /* If this function uses the arg pointer, restore it. */
1525 if (current_function_args_size != 0 || varargs_stdarg_function)
1526 fprintf (file, "\tmov r3,g14\n");
1528 /* Restore parameter registers. */
1530 for (i = 0, j = 4; i <= last_parm_reg; i += increment, j += increment)
1532 if (i % 4 == 0 && (last_parm_reg - i) >= 3)
1533 increment = 4;
1534 else if (i % 4 == 0 && (last_parm_reg - i) >= 2)
1535 increment = 3;
1536 else if (i % 2 == 0 && (last_parm_reg - i) >= 1)
1537 increment = 2;
1538 else
1539 increment = 1;
1541 fprintf (file, "\tmov%s r%d,g%d\n",
1542 (increment == 4 ? "q" : increment == 3 ? "t"
1543 : increment == 2 ? "l": ""), j, i);
1547 /* Output code for the function epilogue. */
1549 static void
1550 i960_output_function_epilogue (file, size)
1551 FILE *file;
1552 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
1554 if (i960_leaf_ret_reg >= 0)
1556 fprintf (file, "Li960R%d: ret\n", ret_label);
1557 return;
1560 if (*epilogue_string == 0)
1562 register rtx tmp;
1564 /* Emit a return insn, but only if control can fall through to here. */
1566 tmp = get_last_insn ();
1567 while (tmp)
1569 if (GET_CODE (tmp) == BARRIER)
1570 return;
1571 if (GET_CODE (tmp) == CODE_LABEL)
1572 break;
1573 if (GET_CODE (tmp) == JUMP_INSN)
1575 if (GET_CODE (PATTERN (tmp)) == RETURN)
1576 return;
1577 break;
1579 if (GET_CODE (tmp) == NOTE)
1581 tmp = PREV_INSN (tmp);
1582 continue;
1584 break;
1586 fprintf (file, "Li960R%d: ret\n", ret_label);
1587 return;
1590 fprintf (file, "Li960R%d:\n", ret_label);
1592 fprintf (file, "\t#EPILOGUE#\n");
1594 /* Output the string created by the prologue which will restore all
1595 registers saved by the prologue. */
1597 if (epilogue_string[0] != '\0')
1598 fprintf (file, "%s", epilogue_string);
1600 /* Must clear g14 on return if this function set it.
1601 Only varargs/stdarg functions modify g14. */
1603 if (VARARGS_STDARG_FUNCTION (current_function_decl))
1604 fprintf (file, "\tmov 0,g14\n");
1606 fprintf (file, "\tret\n");
1607 fprintf (file, "\t#End Epilogue#\n");
1610 /* Output code for a call insn. */
1612 const char *
1613 i960_output_call_insn (target, argsize_rtx, arg_pointer, insn)
1614 register rtx target, argsize_rtx, arg_pointer, insn;
1616 int argsize = INTVAL (argsize_rtx);
1617 rtx nexti = next_real_insn (insn);
1618 rtx operands[2];
1619 int varargs_stdarg_function
1620 = VARARGS_STDARG_FUNCTION (current_function_decl);
1622 operands[0] = target;
1623 operands[1] = arg_pointer;
1625 if (current_function_args_size != 0 || varargs_stdarg_function)
1626 output_asm_insn ("mov g14,r3", operands);
1628 if (argsize > 48)
1629 output_asm_insn ("lda %a1,g14", operands);
1630 else if (current_function_args_size != 0 || varargs_stdarg_function)
1631 output_asm_insn ("mov 0,g14", operands);
1633 /* The code used to assume that calls to SYMBOL_REFs could not be more
1634 than 24 bits away (b vs bx, callj vs callx). This is not true. This
1635 feature is now implemented by relaxing in the GNU linker. It can convert
1636 bx to b if in range, and callx to calls/call/balx/bal as appropriate. */
1638 /* Nexti could be zero if the called routine is volatile. */
1639 if (optimize && (*epilogue_string == 0) && argsize == 0 && tail_call_ok
1640 && (nexti == 0 || GET_CODE (PATTERN (nexti)) == RETURN))
1642 /* Delete following return insn. */
1643 if (nexti && no_labels_between_p (insn, nexti))
1644 delete_insn (nexti);
1645 output_asm_insn ("bx %0", operands);
1646 return "# notreached";
1649 output_asm_insn ("callx %0", operands);
1651 /* If the caller sets g14 to the address of the argblock, then the caller
1652 must clear it after the return. */
1654 if (current_function_args_size != 0 || varargs_stdarg_function)
1655 output_asm_insn ("mov r3,g14", operands);
1656 else if (argsize > 48)
1657 output_asm_insn ("mov 0,g14", operands);
1659 return "";
1662 /* Output code for a return insn. */
1664 const char *
1665 i960_output_ret_insn (insn)
1666 register rtx insn;
1668 static char lbuf[20];
1670 if (*epilogue_string != 0)
1672 if (! TARGET_CODE_ALIGN && next_real_insn (insn) == 0)
1673 return "";
1675 sprintf (lbuf, "b Li960R%d", ret_label);
1676 return lbuf;
1679 /* Must clear g14 on return if this function set it.
1680 Only varargs/stdarg functions modify g14. */
1682 if (VARARGS_STDARG_FUNCTION (current_function_decl))
1683 output_asm_insn ("mov 0,g14", 0);
1685 if (i960_leaf_ret_reg >= 0)
1687 sprintf (lbuf, "bx (%s)", reg_names[i960_leaf_ret_reg]);
1688 return lbuf;
1690 return "ret";
1693 /* Print the operand represented by rtx X formatted by code CODE. */
1695 void
1696 i960_print_operand (file, x, code)
1697 FILE *file;
1698 rtx x;
1699 int code;
1701 enum rtx_code rtxcode = x ? GET_CODE (x) : NIL;
1703 if (rtxcode == REG)
1705 switch (code)
1707 case 'D':
1708 /* Second reg of a double or quad. */
1709 fprintf (file, "%s", reg_names[REGNO (x)+1]);
1710 break;
1712 case 'E':
1713 /* Third reg of a quad. */
1714 fprintf (file, "%s", reg_names[REGNO (x)+2]);
1715 break;
1717 case 'F':
1718 /* Fourth reg of a quad. */
1719 fprintf (file, "%s", reg_names[REGNO (x)+3]);
1720 break;
1722 case 0:
1723 fprintf (file, "%s", reg_names[REGNO (x)]);
1724 break;
1726 default:
1727 abort ();
1729 return;
1731 else if (rtxcode == MEM)
1733 output_address (XEXP (x, 0));
1734 return;
1736 else if (rtxcode == CONST_INT)
1738 HOST_WIDE_INT val = INTVAL (x);
1739 if (code == 'C')
1740 val = ~val;
1741 if (val > 9999 || val < -999)
1742 fprintf (file, "0x%x", val);
1743 else
1744 fprintf (file, "%d", val);
1745 return;
1747 else if (rtxcode == CONST_DOUBLE)
1749 REAL_VALUE_TYPE d;
1750 char dstr[30];
1752 if (x == CONST0_RTX (GET_MODE (x)))
1754 fprintf (file, "0f0.0");
1755 return;
1757 else if (x == CONST1_RTX (GET_MODE (x)))
1759 fprintf (file, "0f1.0");
1760 return;
1763 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1764 REAL_VALUE_TO_DECIMAL (d, "%#g", dstr);
1765 fprintf (file, "0f%s", dstr);
1766 return;
1769 switch(code)
1771 case 'B':
1772 /* Branch or jump, depending on assembler. */
1773 if (TARGET_ASM_COMPAT)
1774 fputs ("j", file);
1775 else
1776 fputs ("b", file);
1777 break;
1779 case 'S':
1780 /* Sign of condition. */
1781 if ((rtxcode == EQ) || (rtxcode == NE) || (rtxcode == GTU)
1782 || (rtxcode == LTU) || (rtxcode == GEU) || (rtxcode == LEU))
1783 fputs ("o", file);
1784 else if ((rtxcode == GT) || (rtxcode == LT)
1785 || (rtxcode == GE) || (rtxcode == LE))
1786 fputs ("i", file);
1787 else
1788 abort();
1789 break;
1791 case 'I':
1792 /* Inverted condition. */
1793 rtxcode = reverse_condition (rtxcode);
1794 goto normal;
1796 case 'X':
1797 /* Inverted condition w/ reversed operands. */
1798 rtxcode = reverse_condition (rtxcode);
1799 /* Fallthrough. */
1801 case 'R':
1802 /* Reversed operand condition. */
1803 rtxcode = swap_condition (rtxcode);
1804 /* Fallthrough. */
1806 case 'C':
1807 /* Normal condition. */
1808 normal:
1809 if (rtxcode == EQ) { fputs ("e", file); return; }
1810 else if (rtxcode == NE) { fputs ("ne", file); return; }
1811 else if (rtxcode == GT) { fputs ("g", file); return; }
1812 else if (rtxcode == GTU) { fputs ("g", file); return; }
1813 else if (rtxcode == LT) { fputs ("l", file); return; }
1814 else if (rtxcode == LTU) { fputs ("l", file); return; }
1815 else if (rtxcode == GE) { fputs ("ge", file); return; }
1816 else if (rtxcode == GEU) { fputs ("ge", file); return; }
1817 else if (rtxcode == LE) { fputs ("le", file); return; }
1818 else if (rtxcode == LEU) { fputs ("le", file); return; }
1819 else abort ();
1820 break;
1822 case '+':
1823 /* For conditional branches, substitute ".t" or ".f". */
1824 if (TARGET_BRANCH_PREDICT)
1826 x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
1827 if (x)
1829 int pred_val = INTVAL (XEXP (x, 0));
1830 fputs ((pred_val < REG_BR_PROB_BASE / 2 ? ".f" : ".t"), file);
1833 break;
1835 case 0:
1836 output_addr_const (file, x);
1837 break;
1839 default:
1840 abort ();
1843 return;
1846 /* Print a memory address as an operand to reference that memory location.
1848 This is exactly the same as legitimate_address_p, except that it the prints
1849 addresses instead of recognizing them. */
1851 void
1852 i960_print_operand_addr (file, addr)
1853 FILE *file;
1854 register rtx addr;
1856 rtx breg, ireg;
1857 rtx scale, offset;
1859 ireg = 0;
1860 breg = 0;
1861 offset = 0;
1862 scale = const1_rtx;
1864 if (GET_CODE (addr) == REG)
1865 breg = addr;
1866 else if (CONSTANT_P (addr))
1867 offset = addr;
1868 else if (GET_CODE (addr) == PLUS)
1870 rtx op0, op1;
1872 op0 = XEXP (addr, 0);
1873 op1 = XEXP (addr, 1);
1875 if (GET_CODE (op0) == REG)
1877 breg = op0;
1878 if (GET_CODE (op1) == REG)
1879 ireg = op1;
1880 else if (CONSTANT_P (op1))
1881 offset = op1;
1882 else
1883 abort ();
1885 else if (GET_CODE (op0) == PLUS)
1887 if (GET_CODE (XEXP (op0, 0)) == MULT)
1889 ireg = XEXP (XEXP (op0, 0), 0);
1890 scale = XEXP (XEXP (op0, 0), 1);
1891 if (GET_CODE (XEXP (op0, 1)) == REG)
1893 breg = XEXP (op0, 1);
1894 offset = op1;
1896 else
1897 abort ();
1899 else if (GET_CODE (XEXP (op0, 0)) == REG)
1901 breg = XEXP (op0, 0);
1902 if (GET_CODE (XEXP (op0, 1)) == REG)
1904 ireg = XEXP (op0, 1);
1905 offset = op1;
1907 else
1908 abort ();
1910 else
1911 abort ();
1913 else if (GET_CODE (op0) == MULT)
1915 ireg = XEXP (op0, 0);
1916 scale = XEXP (op0, 1);
1917 if (GET_CODE (op1) == REG)
1918 breg = op1;
1919 else if (CONSTANT_P (op1))
1920 offset = op1;
1921 else
1922 abort ();
1924 else
1925 abort ();
1927 else if (GET_CODE (addr) == MULT)
1929 ireg = XEXP (addr, 0);
1930 scale = XEXP (addr, 1);
1932 else
1933 abort ();
1935 if (offset)
1936 output_addr_const (file, offset);
1937 if (breg)
1938 fprintf (file, "(%s)", reg_names[REGNO (breg)]);
1939 if (ireg)
1940 fprintf (file, "[%s*%d]", reg_names[REGNO (ireg)], INTVAL (scale));
1943 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
1944 that is a valid memory address for an instruction.
1945 The MODE argument is the machine mode for the MEM expression
1946 that wants to use this address.
1948 On 80960, legitimate addresses are:
1949 base ld (g0),r0
1950 disp (12 or 32 bit) ld foo,r0
1951 base + index ld (g0)[g1*1],r0
1952 base + displ ld 0xf00(g0),r0
1953 base + index*scale + displ ld 0xf00(g0)[g1*4],r0
1954 index*scale + base ld (g0)[g1*4],r0
1955 index*scale + displ ld 0xf00[g1*4],r0
1956 index*scale ld [g1*4],r0
1957 index + base + displ ld 0xf00(g0)[g1*1],r0
1959 In each case, scale can be 1, 2, 4, 8, or 16. */
1961 /* This is exactly the same as i960_print_operand_addr, except that
1962 it recognizes addresses instead of printing them.
1964 It only recognizes address in canonical form. LEGITIMIZE_ADDRESS should
1965 convert common non-canonical forms to canonical form so that they will
1966 be recognized. */
1968 /* These two macros allow us to accept either a REG or a SUBREG anyplace
1969 where a register is valid. */
1971 #define RTX_OK_FOR_BASE_P(X, STRICT) \
1972 ((GET_CODE (X) == REG \
1973 && (STRICT ? REG_OK_FOR_BASE_P_STRICT (X) : REG_OK_FOR_BASE_P (X))) \
1974 || (GET_CODE (X) == SUBREG \
1975 && GET_CODE (SUBREG_REG (X)) == REG \
1976 && (STRICT ? REG_OK_FOR_BASE_P_STRICT (SUBREG_REG (X)) \
1977 : REG_OK_FOR_BASE_P (SUBREG_REG (X)))))
1979 #define RTX_OK_FOR_INDEX_P(X, STRICT) \
1980 ((GET_CODE (X) == REG \
1981 && (STRICT ? REG_OK_FOR_INDEX_P_STRICT (X) : REG_OK_FOR_INDEX_P (X)))\
1982 || (GET_CODE (X) == SUBREG \
1983 && GET_CODE (SUBREG_REG (X)) == REG \
1984 && (STRICT ? REG_OK_FOR_INDEX_P_STRICT (SUBREG_REG (X)) \
1985 : REG_OK_FOR_INDEX_P (SUBREG_REG (X)))))
1988 legitimate_address_p (mode, addr, strict)
1989 enum machine_mode mode ATTRIBUTE_UNUSED;
1990 register rtx addr;
1991 int strict;
1993 if (RTX_OK_FOR_BASE_P (addr, strict))
1994 return 1;
1995 else if (CONSTANT_P (addr))
1996 return 1;
1997 else if (GET_CODE (addr) == PLUS)
1999 rtx op0, op1;
2001 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
2002 return 0;
2004 op0 = XEXP (addr, 0);
2005 op1 = XEXP (addr, 1);
2007 if (RTX_OK_FOR_BASE_P (op0, strict))
2009 if (RTX_OK_FOR_INDEX_P (op1, strict))
2010 return 1;
2011 else if (CONSTANT_P (op1))
2012 return 1;
2013 else
2014 return 0;
2016 else if (GET_CODE (op0) == PLUS)
2018 if (GET_CODE (XEXP (op0, 0)) == MULT)
2020 if (! (RTX_OK_FOR_INDEX_P (XEXP (XEXP (op0, 0), 0), strict)
2021 && SCALE_TERM_P (XEXP (XEXP (op0, 0), 1))))
2022 return 0;
2024 if (RTX_OK_FOR_BASE_P (XEXP (op0, 1), strict)
2025 && CONSTANT_P (op1))
2026 return 1;
2027 else
2028 return 0;
2030 else if (RTX_OK_FOR_BASE_P (XEXP (op0, 0), strict))
2032 if (RTX_OK_FOR_INDEX_P (XEXP (op0, 1), strict)
2033 && CONSTANT_P (op1))
2034 return 1;
2035 else
2036 return 0;
2038 else
2039 return 0;
2041 else if (GET_CODE (op0) == MULT)
2043 if (! (RTX_OK_FOR_INDEX_P (XEXP (op0, 0), strict)
2044 && SCALE_TERM_P (XEXP (op0, 1))))
2045 return 0;
2047 if (RTX_OK_FOR_BASE_P (op1, strict))
2048 return 1;
2049 else if (CONSTANT_P (op1))
2050 return 1;
2051 else
2052 return 0;
2054 else
2055 return 0;
2057 else if (GET_CODE (addr) == MULT)
2059 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
2060 return 0;
2062 return (RTX_OK_FOR_INDEX_P (XEXP (addr, 0), strict)
2063 && SCALE_TERM_P (XEXP (addr, 1)));
2065 else
2066 return 0;
2069 /* Try machine-dependent ways of modifying an illegitimate address
2070 to be legitimate. If we find one, return the new, valid address.
2071 This macro is used in only one place: `memory_address' in explow.c.
2073 This converts some non-canonical addresses to canonical form so they
2074 can be recognized. */
2077 legitimize_address (x, oldx, mode)
2078 register rtx x;
2079 register rtx oldx ATTRIBUTE_UNUSED;
2080 enum machine_mode mode ATTRIBUTE_UNUSED;
2082 if (GET_CODE (x) == SYMBOL_REF)
2084 abort ();
2085 x = copy_to_reg (x);
2088 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
2089 return x;
2091 /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
2092 into (plus (plus (mult (reg) (const)) (reg)) (const)). This can be
2093 created by virtual register instantiation, register elimination, and
2094 similar optimizations. */
2095 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT
2096 && GET_CODE (XEXP (x, 1)) == PLUS)
2097 x = gen_rtx_PLUS (Pmode,
2098 gen_rtx_PLUS (Pmode, XEXP (x, 0), XEXP (XEXP (x, 1), 0)),
2099 XEXP (XEXP (x, 1), 1));
2101 /* Canonicalize (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
2102 into (plus (plus (mult (reg) (const)) (reg)) (const)). */
2103 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
2104 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2105 && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
2106 && CONSTANT_P (XEXP (x, 1)))
2108 rtx constant, other;
2110 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2112 constant = XEXP (x, 1);
2113 other = XEXP (XEXP (XEXP (x, 0), 1), 1);
2115 else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
2117 constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
2118 other = XEXP (x, 1);
2120 else
2121 constant = 0;
2123 if (constant)
2124 x = gen_rtx_PLUS (Pmode,
2125 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
2126 XEXP (XEXP (XEXP (x, 0), 1), 0)),
2127 plus_constant (other, INTVAL (constant)));
2130 return x;
2133 #if 0
2134 /* Return the most stringent alignment that we are willing to consider
2135 objects of size SIZE and known alignment ALIGN as having. */
2138 i960_alignment (size, align)
2139 int size;
2140 int align;
2142 int i;
2144 if (! TARGET_STRICT_ALIGN)
2145 if (TARGET_IC_COMPAT2_0 || align >= 4)
2147 i = i960_object_bytes_bitalign (size) / BITS_PER_UNIT;
2148 if (i > align)
2149 align = i;
2152 return align;
2154 #endif
2158 hard_regno_mode_ok (regno, mode)
2159 int regno;
2160 enum machine_mode mode;
2162 if (regno < 32)
2164 switch (mode)
2166 case CCmode: case CC_UNSmode: case CC_CHKmode:
2167 return 0;
2169 case DImode: case DFmode:
2170 return (regno & 1) == 0;
2172 case TImode: case XFmode:
2173 return (regno & 3) == 0;
2175 default:
2176 return 1;
2179 else if (regno >= 32 && regno < 36)
2181 switch (mode)
2183 case SFmode: case DFmode: case XFmode:
2184 case SCmode: case DCmode:
2185 return 1;
2187 default:
2188 return 0;
2191 else if (regno == 36)
2193 switch (mode)
2195 case CCmode: case CC_UNSmode: case CC_CHKmode:
2196 return 1;
2198 default:
2199 return 0;
2202 else if (regno == 37)
2203 return 0;
2205 abort ();
2209 /* Return the minimum alignment of an expression rtx X in bytes. This takes
2210 advantage of machine specific facts, such as knowing that the frame pointer
2211 is always 16 byte aligned. */
2214 i960_expr_alignment (x, size)
2215 rtx x;
2216 int size;
2218 int align = 1;
2220 if (x == 0)
2221 return 1;
2223 switch (GET_CODE(x))
2225 case CONST_INT:
2226 align = INTVAL(x);
2228 if ((align & 0xf) == 0)
2229 align = 16;
2230 else if ((align & 0x7) == 0)
2231 align = 8;
2232 else if ((align & 0x3) == 0)
2233 align = 4;
2234 else if ((align & 0x1) == 0)
2235 align = 2;
2236 else
2237 align = 1;
2238 break;
2240 case PLUS:
2241 align = MIN (i960_expr_alignment (XEXP (x, 0), size),
2242 i960_expr_alignment (XEXP (x, 1), size));
2243 break;
2245 case SYMBOL_REF:
2246 /* If this is a valid program, objects are guaranteed to be
2247 correctly aligned for whatever size the reference actually is. */
2248 align = i960_object_bytes_bitalign (size) / BITS_PER_UNIT;
2249 break;
2251 case REG:
2252 if (REGNO (x) == FRAME_POINTER_REGNUM)
2253 align = 16;
2254 break;
2256 case ASHIFT:
2257 align = i960_expr_alignment (XEXP (x, 0), size);
2259 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2261 align = align << INTVAL (XEXP (x, 1));
2262 align = MIN (align, 16);
2264 break;
2266 case MULT:
2267 align = (i960_expr_alignment (XEXP (x, 0), size) *
2268 i960_expr_alignment (XEXP (x, 1), size));
2270 align = MIN (align, 16);
2271 break;
2272 default:
2273 break;
2276 return align;
2279 /* Return true if it is possible to reference both BASE and OFFSET, which
2280 have alignment at least as great as 4 byte, as if they had alignment valid
2281 for an object of size SIZE. */
2284 i960_improve_align (base, offset, size)
2285 rtx base;
2286 rtx offset;
2287 int size;
2289 int i, j;
2291 /* We have at least a word reference to the object, so we know it has to
2292 be aligned at least to 4 bytes. */
2294 i = MIN (i960_expr_alignment (base, 4),
2295 i960_expr_alignment (offset, 4));
2297 i = MAX (i, 4);
2299 /* We know the size of the request. If strict align is not enabled, we
2300 can guess that the alignment is OK for the requested size. */
2302 if (! TARGET_STRICT_ALIGN)
2303 if ((j = (i960_object_bytes_bitalign (size) / BITS_PER_UNIT)) > i)
2304 i = j;
2306 return (i >= size);
2309 /* Return true if it is possible to access BASE and OFFSET, which have 4 byte
2310 (SImode) alignment as if they had 16 byte (TImode) alignment. */
2313 i960_si_ti (base, offset)
2314 rtx base;
2315 rtx offset;
2317 return i960_improve_align (base, offset, 16);
2320 /* Return true if it is possible to access BASE and OFFSET, which have 4 byte
2321 (SImode) alignment as if they had 8 byte (DImode) alignment. */
2324 i960_si_di (base, offset)
2325 rtx base;
2326 rtx offset;
2328 return i960_improve_align (base, offset, 8);
2331 /* Return raw values of size and alignment (in words) for the data
2332 type being accessed. These values will be rounded by the caller. */
2334 static void
2335 i960_arg_size_and_align (mode, type, size_out, align_out)
2336 enum machine_mode mode;
2337 tree type;
2338 int *size_out;
2339 int *align_out;
2341 int size, align;
2343 /* Use formal alignment requirements of type being passed, except make
2344 it at least a word. If we don't have a type, this is a library call,
2345 and the parm has to be of scalar type. In this case, consider its
2346 formal alignment requirement to be its size in words. */
2348 if (mode == BLKmode)
2349 size = (int_size_in_bytes (type) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2350 else if (mode == VOIDmode)
2352 /* End of parm list. */
2353 if (type == 0 || TYPE_MODE (type) != VOIDmode)
2354 abort ();
2355 size = 1;
2357 else
2358 size = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2360 if (type == 0)
2362 /* ??? This is a hack to properly correct the alignment of XFmode
2363 values without affecting anything else. */
2364 if (size == 3)
2365 align = 4;
2366 else
2367 align = size;
2369 else if (TYPE_ALIGN (type) >= BITS_PER_WORD)
2370 align = TYPE_ALIGN (type) / BITS_PER_WORD;
2371 else
2372 align = 1;
2374 *size_out = size;
2375 *align_out = align;
2378 /* On the 80960 the first 12 args are in registers and the rest are pushed.
2379 Any arg that is bigger than 4 words is placed on the stack and all
2380 subsequent arguments are placed on the stack.
2382 Additionally, parameters with an alignment requirement stronger than
2383 a word must be aligned appropriately. Note that this means that a
2384 64 bit object with a 32 bit alignment is not 64 bit aligned and may be
2385 passed in an odd/even register pair. */
2387 /* Update CUM to advance past an argument described by MODE and TYPE. */
2389 void
2390 i960_function_arg_advance (cum, mode, type, named)
2391 CUMULATIVE_ARGS *cum;
2392 enum machine_mode mode;
2393 tree type;
2394 int named ATTRIBUTE_UNUSED;
2396 int size, align;
2398 i960_arg_size_and_align (mode, type, &size, &align);
2400 if (size > 4 || cum->ca_nstackparms != 0
2401 || (size + ROUND_PARM (cum->ca_nregparms, align)) > NPARM_REGS
2402 || MUST_PASS_IN_STACK (mode, type))
2404 /* Indicate that all the registers are in use, even if all are not,
2405 so va_start will compute the right value. */
2406 cum->ca_nregparms = NPARM_REGS;
2407 cum->ca_nstackparms = ROUND_PARM (cum->ca_nstackparms, align) + size;
2409 else
2410 cum->ca_nregparms = ROUND_PARM (cum->ca_nregparms, align) + size;
2413 /* Return the register that the argument described by MODE and TYPE is
2414 passed in, or else return 0 if it is passed on the stack. */
2417 i960_function_arg (cum, mode, type, named)
2418 CUMULATIVE_ARGS *cum;
2419 enum machine_mode mode;
2420 tree type;
2421 int named ATTRIBUTE_UNUSED;
2423 rtx ret;
2424 int size, align;
2426 if (mode == VOIDmode)
2427 return 0;
2429 i960_arg_size_and_align (mode, type, &size, &align);
2431 if (size > 4 || cum->ca_nstackparms != 0
2432 || (size + ROUND_PARM (cum->ca_nregparms, align)) > NPARM_REGS
2433 || MUST_PASS_IN_STACK (mode, type))
2435 cum->ca_nstackparms = ROUND_PARM (cum->ca_nstackparms, align);
2436 ret = 0;
2438 else
2440 cum->ca_nregparms = ROUND_PARM (cum->ca_nregparms, align);
2441 ret = gen_rtx_REG (mode, cum->ca_nregparms);
2444 return ret;
2447 /* Floating-point support. */
2449 void
2450 i960_output_long_double (file, value)
2451 FILE *file;
2452 REAL_VALUE_TYPE value;
2454 long value_long[3];
2455 char dstr[30];
2457 REAL_VALUE_TO_TARGET_LONG_DOUBLE (value, value_long);
2458 REAL_VALUE_TO_DECIMAL (value, "%.20g", dstr);
2460 fprintf (file,
2461 "\t.word\t0x%08lx\t\t# %s\n\t.word\t0x%08lx\n\t.word\t0x%08lx\n",
2462 value_long[0], dstr, value_long[1], value_long[2]);
2463 fprintf (file, "\t.word\t0x0\n");
2466 void
2467 i960_output_double (file, value)
2468 FILE *file;
2469 REAL_VALUE_TYPE value;
2471 long value_long[2];
2472 char dstr[30];
2474 REAL_VALUE_TO_TARGET_DOUBLE (value, value_long);
2475 REAL_VALUE_TO_DECIMAL (value, "%.20g", dstr);
2477 fprintf (file, "\t.word\t0x%08lx\t\t# %s\n\t.word\t0x%08lx\n",
2478 value_long[0], dstr, value_long[1]);
2481 void
2482 i960_output_float (file, value)
2483 FILE *file;
2484 REAL_VALUE_TYPE value;
2486 long value_long;
2487 char dstr[30];
2489 REAL_VALUE_TO_TARGET_SINGLE (value, value_long);
2490 REAL_VALUE_TO_DECIMAL (value, "%.12g", dstr);
2492 fprintf (file, "\t.word\t0x%08lx\t\t# %s (float)\n", value_long, dstr);
2495 /* Return the number of bits that an object of size N bytes is aligned to. */
2498 i960_object_bytes_bitalign (n)
2499 int n;
2501 if (n > 8) n = 128;
2502 else if (n > 4) n = 64;
2503 else if (n > 2) n = 32;
2504 else if (n > 1) n = 16;
2505 else n = 8;
2507 return n;
2510 /* Compute the alignment for an aggregate type TSIZE.
2511 Alignment is MAX (greatest member alignment,
2512 MIN (pragma align, structure size alignment)). */
2515 i960_round_align (align, tsize)
2516 int align;
2517 tree tsize;
2519 int new_align;
2521 if (! tsize || TREE_CODE (tsize) != INTEGER_CST)
2522 return align;
2524 new_align = i960_object_bytes_bitalign (TREE_INT_CST_LOW (tsize)
2525 / BITS_PER_UNIT);
2526 /* Handle #pragma align. */
2527 if (new_align > i960_maxbitalignment)
2528 new_align = i960_maxbitalignment;
2530 if (align < new_align)
2531 align = new_align;
2533 return align;
2536 /* Do any needed setup for a varargs function. For the i960, we must
2537 create a register parameter block if one doesn't exist, and then copy
2538 all register parameters to memory. */
2540 void
2541 i960_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2542 CUMULATIVE_ARGS *cum;
2543 enum machine_mode mode ATTRIBUTE_UNUSED;
2544 tree type ATTRIBUTE_UNUSED;
2545 int *pretend_size ATTRIBUTE_UNUSED;
2546 int no_rtl;
2548 /* Note: for a varargs fn with only a va_alist argument, this is 0. */
2549 int first_reg = cum->ca_nregparms;
2551 /* Copy only unnamed register arguments to memory. If there are
2552 any stack parms, there are no unnamed arguments in registers, and
2553 an argument block was already allocated by the caller.
2554 Remember that any arg bigger than 4 words is passed on the stack as
2555 are all subsequent args.
2557 If there are no stack arguments but there are exactly NPARM_REGS
2558 registers, either there were no extra arguments or the caller
2559 allocated an argument block. */
2561 if (cum->ca_nstackparms == 0 && first_reg < NPARM_REGS && !no_rtl)
2563 rtx label = gen_label_rtx ();
2564 rtx regblock;
2566 /* If arg_pointer_rtx == 0, no arguments were passed on the stack
2567 and we need to allocate a chunk to save the registers (if any
2568 arguments were passed on the stack the caller would allocate the
2569 48 bytes as well). We must allocate all 48 bytes (12*4) because
2570 va_start assumes it. */
2571 emit_insn (gen_cmpsi (arg_pointer_rtx, const0_rtx));
2572 emit_jump_insn (gen_bne (label));
2573 emit_insn (gen_rtx_SET (VOIDmode, arg_pointer_rtx,
2574 stack_pointer_rtx));
2575 emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
2576 memory_address (SImode,
2577 plus_constant (stack_pointer_rtx,
2578 48))));
2579 emit_label (label);
2581 /* ??? Note that we unnecessarily store one extra register for stdarg
2582 fns. We could optimize this, but it's kept as for now. */
2583 regblock = gen_rtx_MEM (BLKmode,
2584 plus_constant (arg_pointer_rtx, first_reg * 4));
2585 set_mem_alias_set (regblock, get_varargs_alias_set ());
2586 set_mem_align (regblock, BITS_PER_WORD);
2587 move_block_from_reg (first_reg, regblock,
2588 NPARM_REGS - first_reg,
2589 (NPARM_REGS - first_reg) * UNITS_PER_WORD);
2593 /* Define the `__builtin_va_list' type for the ABI. */
2595 tree
2596 i960_build_va_list ()
2598 return build_array_type (unsigned_type_node,
2599 build_index_type (size_one_node));
2602 /* Implement `va_start' for varargs and stdarg. */
2604 void
2605 i960_va_start (stdarg_p, valist, nextarg)
2606 int stdarg_p ATTRIBUTE_UNUSED;
2607 tree valist;
2608 rtx nextarg ATTRIBUTE_UNUSED;
2610 tree s, t, base, num;
2612 /* The array type always decays to a pointer before we get here, so we
2613 can't use ARRAY_REF. */
2614 base = build1 (INDIRECT_REF, unsigned_type_node, valist);
2615 num = build1 (INDIRECT_REF, unsigned_type_node,
2616 build (PLUS_EXPR, unsigned_type_node, valist,
2617 TYPE_SIZE_UNIT (TREE_TYPE (valist))));
2619 s = make_tree (unsigned_type_node, arg_pointer_rtx);
2620 t = build (MODIFY_EXPR, unsigned_type_node, base, s);
2621 TREE_SIDE_EFFECTS (t) = 1;
2622 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2624 s = build_int_2 ((current_function_args_info.ca_nregparms
2625 + current_function_args_info.ca_nstackparms) * 4, 0);
2626 t = build (MODIFY_EXPR, unsigned_type_node, num, s);
2627 TREE_SIDE_EFFECTS (t) = 1;
2628 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2631 /* Implement `va_arg'. */
2634 i960_va_arg (valist, type)
2635 tree valist, type;
2637 HOST_WIDE_INT siz, ali;
2638 tree base, num, pad, next, this, t1, t2, int48;
2639 rtx addr_rtx;
2641 /* The array type always decays to a pointer before we get here, so we
2642 can't use ARRAY_REF. */
2643 base = build1 (INDIRECT_REF, unsigned_type_node, valist);
2644 num = build1 (INDIRECT_REF, unsigned_type_node,
2645 build (PLUS_EXPR, unsigned_type_node, valist,
2646 TYPE_SIZE_UNIT (TREE_TYPE (valist))));
2648 /* Round up sizeof(type) to a word. */
2649 siz = (int_size_in_bytes (type) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
2651 /* Round up alignment to a word. */
2652 ali = TYPE_ALIGN (type);
2653 if (ali < BITS_PER_WORD)
2654 ali = BITS_PER_WORD;
2655 ali /= BITS_PER_UNIT;
2657 /* Align NUM appropriate for the argument. */
2658 pad = fold (build (PLUS_EXPR, unsigned_type_node, num,
2659 build_int_2 (ali - 1, 0)));
2660 pad = fold (build (BIT_AND_EXPR, unsigned_type_node, pad,
2661 build_int_2 (-ali, -1)));
2662 pad = save_expr (pad);
2664 /* Increment VPAD past this argument. */
2665 next = fold (build (PLUS_EXPR, unsigned_type_node, pad,
2666 build_int_2 (siz, 0)));
2667 next = save_expr (next);
2669 /* Find the offset for the current argument. Mind peculiar overflow
2670 from registers to stack. */
2671 int48 = build_int_2 (48, 0);
2672 if (siz > 16)
2673 t2 = integer_one_node;
2674 else
2675 t2 = fold (build (GT_EXPR, integer_type_node, next, int48));
2676 t1 = fold (build (LE_EXPR, integer_type_node, num, int48));
2677 t1 = fold (build (TRUTH_AND_EXPR, integer_type_node, t1, t2));
2678 this = fold (build (COND_EXPR, unsigned_type_node, t1, int48, pad));
2680 /* Find the address for the current argument. */
2681 t1 = fold (build (PLUS_EXPR, unsigned_type_node, base, this));
2682 t1 = build1 (NOP_EXPR, ptr_type_node, t1);
2683 addr_rtx = expand_expr (t1, NULL_RTX, Pmode, EXPAND_NORMAL);
2685 /* Increment NUM. */
2686 t1 = build (MODIFY_EXPR, unsigned_type_node, num, next);
2687 TREE_SIDE_EFFECTS (t1) = 1;
2688 expand_expr (t1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2690 return addr_rtx;
2693 /* Calculate the final size of the reg parm stack space for the current
2694 function, based on how many bytes would be allocated on the stack. */
2697 i960_final_reg_parm_stack_space (const_size, var_size)
2698 int const_size;
2699 tree var_size;
2701 if (var_size || const_size > 48)
2702 return 48;
2703 else
2704 return 0;
2707 /* Calculate the size of the reg parm stack space. This is a bit complicated
2708 on the i960. */
2711 i960_reg_parm_stack_space (fndecl)
2712 tree fndecl;
2714 /* In this case, we are called from emit_library_call, and we don't need
2715 to pretend we have more space for parameters than what's apparent. */
2716 if (fndecl == 0)
2717 return 0;
2719 /* In this case, we are called from locate_and_pad_parms when we're
2720 not IN_REGS, so we have an arg block. */
2721 if (fndecl != current_function_decl)
2722 return 48;
2724 /* Otherwise, we have an arg block if the current function has more than
2725 48 bytes of parameters. */
2726 if (current_function_args_size != 0 || VARARGS_STDARG_FUNCTION (fndecl))
2727 return 48;
2728 else
2729 return 0;
2732 /* Return the register class of a scratch register needed to copy IN into
2733 or out of a register in CLASS in MODE. If it can be done directly,
2734 NO_REGS is returned. */
2736 enum reg_class
2737 secondary_reload_class (class, mode, in)
2738 enum reg_class class;
2739 enum machine_mode mode;
2740 rtx in;
2742 int regno = -1;
2744 if (GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
2745 regno = true_regnum (in);
2747 /* We can place anything into LOCAL_OR_GLOBAL_REGS and can put
2748 LOCAL_OR_GLOBAL_REGS into anything. */
2749 if (class == LOCAL_OR_GLOBAL_REGS || class == LOCAL_REGS
2750 || class == GLOBAL_REGS || (regno >= 0 && regno < 32))
2751 return NO_REGS;
2753 /* We can place any hard register, 0.0, and 1.0 into FP_REGS. */
2754 if (class == FP_REGS
2755 && ((regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
2756 || in == CONST0_RTX (mode) || in == CONST1_RTX (mode)))
2757 return NO_REGS;
2759 return LOCAL_OR_GLOBAL_REGS;
2762 /* Look at the opcode P, and set i96_last_insn_type to indicate which
2763 function unit it executed on. */
2765 /* ??? This would make more sense as an attribute. */
2767 void
2768 i960_scan_opcode (p)
2769 const char *p;
2771 switch (*p)
2773 case 'a':
2774 case 'd':
2775 case 'e':
2776 case 'm':
2777 case 'n':
2778 case 'o':
2779 case 'r':
2780 /* Ret is not actually of type REG, but it won't matter, because no
2781 insn will ever follow it. */
2782 case 'u':
2783 case 'x':
2784 i960_last_insn_type = I_TYPE_REG;
2785 break;
2787 case 'b':
2788 if (p[1] == 'x' || p[3] == 'x')
2789 i960_last_insn_type = I_TYPE_MEM;
2790 i960_last_insn_type = I_TYPE_CTRL;
2791 break;
2793 case 'f':
2794 case 't':
2795 i960_last_insn_type = I_TYPE_CTRL;
2796 break;
2798 case 'c':
2799 if (p[1] == 'a')
2801 if (p[4] == 'x')
2802 i960_last_insn_type = I_TYPE_MEM;
2803 else
2804 i960_last_insn_type = I_TYPE_CTRL;
2806 else if (p[1] == 'm')
2808 if (p[3] == 'd')
2809 i960_last_insn_type = I_TYPE_REG;
2810 else if (p[4] == 'b' || p[4] == 'j')
2811 i960_last_insn_type = I_TYPE_CTRL;
2812 else
2813 i960_last_insn_type = I_TYPE_REG;
2815 else
2816 i960_last_insn_type = I_TYPE_REG;
2817 break;
2819 case 'l':
2820 i960_last_insn_type = I_TYPE_MEM;
2821 break;
2823 case 's':
2824 if (p[1] == 't')
2825 i960_last_insn_type = I_TYPE_MEM;
2826 else
2827 i960_last_insn_type = I_TYPE_REG;
2828 break;