i960.c (i960_address_cost): MEMA operands with positive offsets < 4096 are free.
[gcc.git] / gcc / config / i960 / i960.c
1 /* Subroutines used for code generation on intel 80960.
2 Copyright (C) 1992, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 Contributed by Steven McGeady, Intel Corp.
4 Additional Work by Glenn Colon-Bonet, Jonathan Shapiro, Andy Wilson
5 Converted to GCC 2.0 by Jim Wilson and Michael Tiemann, Cygnus Support.
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "config.h"
25 #include <stdio.h>
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "tree.h"
37 #include "insn-codes.h"
38 #include "expr.h"
39 #include "except.h"
40 #include "function.h"
41 #include "recog.h"
42 #include <math.h>
43
44 /* Save the operands last given to a compare for use when we
45 generate a scc or bcc insn. */
46
47 rtx i960_compare_op0, i960_compare_op1;
48
49 /* Used to implement #pragma align/noalign. Initialized by OVERRIDE_OPTIONS
50 macro in i960.h. */
51
52 static int i960_maxbitalignment;
53 static int i960_last_maxbitalignment;
54
55 /* Used to implement switching between MEM and ALU insn types, for better
56 C series performance. */
57
58 enum insn_types i960_last_insn_type;
59
60 /* The leaf-procedure return register. Set only if this is a leaf routine. */
61
62 static int i960_leaf_ret_reg;
63
64 /* True if replacing tail calls with jumps is OK. */
65
66 static int tail_call_ok;
67
68 /* A string containing a list of insns to emit in the epilogue so as to
69 restore all registers saved by the prologue. Created by the prologue
70 code as it saves registers away. */
71
72 char epilogue_string[1000];
73
74 /* A unique number (per function) for return labels. */
75
76 static int ret_label = 0;
77
78 /* This is true if FNDECL is either a varargs or a stdarg function.
79 This is used to help identify functions that use an argument block. */
80
81 #define VARARGS_STDARG_FUNCTION(FNDECL) \
82 ((TYPE_ARG_TYPES (TREE_TYPE (FNDECL)) != 0 \
83 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (FNDECL)))) != void_type_node)) \
84 || current_function_varargs)
85
86 /* Handle pragmas for compatibility with Intel's compilers. */
87
88 /* ??? This is incomplete, since it does not handle all pragmas that the
89 intel compilers understand. */
90
91 int
92 process_pragma (finput, t)
93 FILE *finput;
94 tree t;
95 {
96 int i;
97 register int c;
98 register char *pname;
99
100 if (TREE_CODE (t) != IDENTIFIER_NODE)
101 return 0;
102
103 pname = IDENTIFIER_POINTER (t);
104
105 if (strcmp (pname, "align") == 0)
106 {
107 char buf[20];
108 char *s = buf;
109 int align;
110
111 do {
112 c = getc (finput);
113 } while (c == ' ' || c == '\t');
114
115 if (c == '(')
116 c = getc (finput);
117 while (c >= '0' && c <= '9')
118 {
119 if (s < buf + sizeof buf - 1)
120 *s++ = c;
121 c = getc (finput);
122 }
123 *s = '\0';
124
125 /* We had to read a non-numerical character to get out of the
126 while loop---often a newline. So, we have to put it back to
127 make sure we continue to parse everything properly. */
128 ungetc (c, finput);
129
130 align = atoi (buf);
131 switch (align)
132 {
133 case 0:
134 /* Return to last alignment. */
135 align = i960_last_maxbitalignment / 8;
136 /* Fall through. */
137 case 16:
138 case 8:
139 case 4:
140 case 2:
141 case 1:
142 i960_last_maxbitalignment = i960_maxbitalignment;
143 i960_maxbitalignment = align * 8;
144 break;
145
146 default:
147 /* Silently ignore bad values. */
148 break;
149 }
150
151 /* NOTE: ic960 R3.0 pragma align definition:
152
153 #pragma align [(size)] | (identifier=size[,...])
154 #pragma noalign [(identifier)[,...]]
155
156 (all parens are optional)
157
158 - size is [1,2,4,8,16]
159 - noalign means size==1
160 - applies only to component elements of a struct (and union?)
161 - identifier applies to structure tag (only)
162 - missing identifier means next struct
163
164 - alignment rules for bitfields need more investigation */
165
166 return 1;
167 }
168
169 /* Should be pragma 'far' or equivalent for callx/balx here. */
170
171 return 0;
172 }
173
174 /* Initialize variables before compiling any files. */
175
176 void
177 i960_initialize ()
178 {
179 if (TARGET_IC_COMPAT2_0)
180 {
181 i960_maxbitalignment = 8;
182 i960_last_maxbitalignment = 128;
183 }
184 else
185 {
186 i960_maxbitalignment = 128;
187 i960_last_maxbitalignment = 8;
188 }
189 }
190 \f
191 /* Return true if OP can be used as the source of an fp move insn. */
192
193 int
194 fpmove_src_operand (op, mode)
195 rtx op;
196 enum machine_mode mode;
197 {
198 return (GET_CODE (op) == CONST_DOUBLE || general_operand (op, mode));
199 }
200
201 #if 0
202 /* Return true if OP is a register or zero. */
203
204 int
205 reg_or_zero_operand (op, mode)
206 rtx op;
207 enum machine_mode mode;
208 {
209 return register_operand (op, mode) || op == const0_rtx;
210 }
211 #endif
212
213 /* Return truth value of whether OP can be used as an operands in a three
214 address arithmetic insn (such as add %o1,7,%l2) of mode MODE. */
215
216 int
217 arith_operand (op, mode)
218 rtx op;
219 enum machine_mode mode;
220 {
221 return (register_operand (op, mode) || literal (op, mode));
222 }
223
224 /* Return truth value of whether OP can be used as an operands in a three
225 address logic insn, possibly complementing OP, of mode MODE. */
226
227 int
228 logic_operand (op, mode)
229 rtx op;
230 enum machine_mode mode;
231 {
232 return (register_operand (op, mode)
233 || (GET_CODE (op) == CONST_INT
234 && INTVAL(op) >= -32 && INTVAL(op) < 32));
235 }
236
237 /* Return true if OP is a register or a valid floating point literal. */
238
239 int
240 fp_arith_operand (op, mode)
241 rtx op;
242 enum machine_mode mode;
243 {
244 return (register_operand (op, mode) || fp_literal (op, mode));
245 }
246
247 /* Return true is OP is a register or a valid signed integer literal. */
248
249 int
250 signed_arith_operand (op, mode)
251 rtx op;
252 enum machine_mode mode;
253 {
254 return (register_operand (op, mode) || signed_literal (op, mode));
255 }
256
257 /* Return truth value of whether OP is a integer which fits the
258 range constraining immediate operands in three-address insns. */
259
260 int
261 literal (op, mode)
262 rtx op;
263 enum machine_mode mode;
264 {
265 return ((GET_CODE (op) == CONST_INT) && INTVAL(op) >= 0 && INTVAL(op) < 32);
266 }
267
268 /* Return true if OP is a float constant of 1. */
269
270 int
271 fp_literal_one (op, mode)
272 rtx op;
273 enum machine_mode mode;
274 {
275 return (TARGET_NUMERICS && mode == GET_MODE (op) && op == CONST1_RTX (mode));
276 }
277
278 /* Return true if OP is a float constant of 0. */
279
280 int
281 fp_literal_zero (op, mode)
282 rtx op;
283 enum machine_mode mode;
284 {
285 return (TARGET_NUMERICS && mode == GET_MODE (op) && op == CONST0_RTX (mode));
286 }
287
288 /* Return true if OP is a valid floating point literal. */
289
290 int
291 fp_literal(op, mode)
292 rtx op;
293 enum machine_mode mode;
294 {
295 return fp_literal_zero (op, mode) || fp_literal_one (op, mode);
296 }
297
298 /* Return true if OP is a valid signed immediate constant. */
299
300 int
301 signed_literal(op, mode)
302 rtx op;
303 enum machine_mode mode;
304 {
305 return ((GET_CODE (op) == CONST_INT) && INTVAL(op) > -32 && INTVAL(op) < 32);
306 }
307
308 /* Return truth value of statement that OP is a symbolic memory
309 operand of mode MODE. */
310
311 int
312 symbolic_memory_operand (op, mode)
313 rtx op;
314 enum machine_mode mode;
315 {
316 if (GET_CODE (op) == SUBREG)
317 op = SUBREG_REG (op);
318 if (GET_CODE (op) != MEM)
319 return 0;
320 op = XEXP (op, 0);
321 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
322 || GET_CODE (op) == HIGH || GET_CODE (op) == LABEL_REF);
323 }
324
325 /* Return truth value of whether OP is EQ or NE. */
326
327 int
328 eq_or_neq (op, mode)
329 rtx op;
330 enum machine_mode mode;
331 {
332 return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
333 }
334
335 /* OP is an integer register or a constant. */
336
337 int
338 arith32_operand (op, mode)
339 rtx op;
340 enum machine_mode mode;
341 {
342 if (register_operand (op, mode))
343 return 1;
344 return (CONSTANT_P (op));
345 }
346
347 /* Return true if OP is an integer constant which is a power of 2. */
348
349 int
350 power2_operand (op,mode)
351 rtx op;
352 enum machine_mode mode;
353 {
354 if (GET_CODE (op) != CONST_INT)
355 return 0;
356
357 return exact_log2 (INTVAL (op)) >= 0;
358 }
359
360 /* Return true if OP is an integer constant which is the complement of a
361 power of 2. */
362
363 int
364 cmplpower2_operand (op, mode)
365 rtx op;
366 enum machine_mode mode;
367 {
368 if (GET_CODE (op) != CONST_INT)
369 return 0;
370
371 return exact_log2 (~ INTVAL (op)) >= 0;
372 }
373
374 /* If VAL has only one bit set, return the index of that bit. Otherwise
375 return -1. */
376
377 int
378 bitpos (val)
379 unsigned int val;
380 {
381 register int i;
382
383 for (i = 0; val != 0; i++, val >>= 1)
384 {
385 if (val & 1)
386 {
387 if (val != 1)
388 return -1;
389 return i;
390 }
391 }
392 return -1;
393 }
394
395 /* Return non-zero if OP is a mask, i.e. all one bits are consecutive.
396 The return value indicates how many consecutive non-zero bits exist
397 if this is a mask. This is the same as the next function, except that
398 it does not indicate what the start and stop bit positions are. */
399
400 int
401 is_mask (val)
402 unsigned int val;
403 {
404 register int start, end, i;
405
406 start = -1;
407 for (i = 0; val != 0; val >>= 1, i++)
408 {
409 if (val & 1)
410 {
411 if (start < 0)
412 start = i;
413
414 end = i;
415 continue;
416 }
417 /* Still looking for the first bit. */
418 if (start < 0)
419 continue;
420
421 /* We've seen the start of a bit sequence, and now a zero. There
422 must be more one bits, otherwise we would have exited the loop.
423 Therefore, it is not a mask. */
424 if (val)
425 return 0;
426 }
427
428 /* The bit string has ones from START to END bit positions only. */
429 return end - start + 1;
430 }
431
432 /* If VAL is a mask, then return nonzero, with S set to the starting bit
433 position and E set to the ending bit position of the mask. The return
434 value indicates how many consecutive bits exist in the mask. This is
435 the same as the previous function, except that it also indicates the
436 start and end bit positions of the mask. */
437
438 int
439 bitstr (val, s, e)
440 unsigned int val;
441 int *s, *e;
442 {
443 register int start, end, i;
444
445 start = -1;
446 end = -1;
447 for (i = 0; val != 0; val >>= 1, i++)
448 {
449 if (val & 1)
450 {
451 if (start < 0)
452 start = i;
453
454 end = i;
455 continue;
456 }
457
458 /* Still looking for the first bit. */
459 if (start < 0)
460 continue;
461
462 /* We've seen the start of a bit sequence, and now a zero. There
463 must be more one bits, otherwise we would have exited the loop.
464 Therefor, it is not a mask. */
465 if (val)
466 {
467 start = -1;
468 end = -1;
469 break;
470 }
471 }
472
473 /* The bit string has ones from START to END bit positions only. */
474 *s = start;
475 *e = end;
476 return ((start < 0) ? 0 : end - start + 1);
477 }
478 \f
479 /* Return the machine mode to use for a comparison. */
480
481 enum machine_mode
482 select_cc_mode (op, x)
483 RTX_CODE op;
484 rtx x;
485 {
486 if (op == GTU || op == LTU || op == GEU || op == LEU)
487 return CC_UNSmode;
488 return CCmode;
489 }
490
491 /* X and Y are two things to compare using CODE. Emit the compare insn and
492 return the rtx for register 36 in the proper mode. */
493
494 rtx
495 gen_compare_reg (code, x, y)
496 enum rtx_code code;
497 rtx x, y;
498 {
499 rtx cc_reg;
500 enum machine_mode ccmode = SELECT_CC_MODE (code, x, y);
501 enum machine_mode mode
502 = GET_MODE (x) == VOIDmode ? GET_MODE (y) : GET_MODE (x);
503
504 if (mode == SImode)
505 {
506 if (! arith_operand (x, mode))
507 x = force_reg (SImode, x);
508 if (! arith_operand (y, mode))
509 y = force_reg (SImode, y);
510 }
511
512 cc_reg = gen_rtx (REG, ccmode, 36);
513 emit_insn (gen_rtx (SET, VOIDmode, cc_reg,
514 gen_rtx (COMPARE, ccmode, x, y)));
515
516 return cc_reg;
517 }
518
519 /* For the i960, REG is cost 1, REG+immed CONST is cost 2, REG+REG is cost 2,
520 REG+nonimmed CONST is cost 4. REG+SYMBOL_REF, SYMBOL_REF, and similar
521 are 4. Indexed addresses are cost 6. */
522
523 /* ??? Try using just RTX_COST, i.e. not defining ADDRESS_COST. */
524
525 int
526 i960_address_cost (x)
527 rtx x;
528 {
529 #if 0
530 /* Handled before calling here. */
531 if (GET_CODE (x) == REG)
532 return 1;
533 #endif
534 /* This is a MEMA operand -- it's free. */
535 if (GET_CODE (x) == CONST_INT
536 && INTVAL (x) >= 0
537 && INTVAL (x) < 4096)
538 return 0;
539
540 if (GET_CODE (x) == PLUS)
541 {
542 rtx base = XEXP (x, 0);
543 rtx offset = XEXP (x, 1);
544
545 if (GET_CODE (base) == SUBREG)
546 base = SUBREG_REG (base);
547 if (GET_CODE (offset) == SUBREG)
548 offset = SUBREG_REG (offset);
549
550 if (GET_CODE (base) == REG)
551 {
552 if (GET_CODE (offset) == REG)
553 return 2;
554 if (GET_CODE (offset) == CONST_INT)
555 {
556 if ((unsigned)INTVAL (offset) < 2047)
557 return 2;
558 return 4;
559 }
560 if (CONSTANT_P (offset))
561 return 4;
562 }
563 if (GET_CODE (base) == PLUS || GET_CODE (base) == MULT)
564 return 6;
565
566 /* This is an invalid address. The return value doesn't matter, but
567 for convenience we make this more expensive than anything else. */
568 return 12;
569 }
570 if (GET_CODE (x) == MULT)
571 return 6;
572
573 /* Symbol_refs and other unrecognized addresses are cost 4. */
574 return 4;
575 }
576 \f
577 /* Emit insns to move operands[1] into operands[0].
578
579 Return 1 if we have written out everything that needs to be done to
580 do the move. Otherwise, return 0 and the caller will emit the move
581 normally. */
582
583 int
584 emit_move_sequence (operands, mode)
585 rtx *operands;
586 enum machine_mode mode;
587 {
588 /* We can only store registers to memory. */
589
590 if (GET_CODE (operands[0]) == MEM && GET_CODE (operands[1]) != REG)
591 operands[1] = force_reg (mode, operands[1]);
592
593 /* Storing multi-word values in unaligned hard registers to memory may
594 require a scratch since we have to store them a register at a time and
595 adding 4 to the memory address may not yield a valid insn. */
596 /* ??? We don't always need the scratch, but that would complicate things.
597 Maybe later. */
598 /* ??? We must also handle stores to pseudos here, because the pseudo may be
599 replaced with a MEM later. This would be cleaner if we didn't have
600 a separate pattern for unaligned DImode/TImode stores. */
601 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
602 && (GET_CODE (operands[0]) == MEM
603 || (GET_CODE (operands[0]) == REG
604 && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
605 && GET_CODE (operands[1]) == REG
606 && REGNO (operands[1]) < FIRST_PSEUDO_REGISTER
607 && ! HARD_REGNO_MODE_OK (REGNO (operands[1]), mode))
608 {
609 emit_insn (gen_rtx (PARALLEL, VOIDmode,
610 gen_rtvec (2,
611 gen_rtx (SET, VOIDmode,
612 operands[0], operands[1]),
613 gen_rtx (CLOBBER, VOIDmode,
614 gen_rtx (SCRATCH, Pmode)))));
615 return 1;
616 }
617
618 return 0;
619 }
620
621 /* Output assembler to move a double word value. */
622
623 char *
624 i960_output_move_double (dst, src)
625 rtx dst, src;
626 {
627 rtx operands[5];
628
629 if (GET_CODE (dst) == REG
630 && GET_CODE (src) == REG)
631 {
632 if ((REGNO (src) & 1)
633 || (REGNO (dst) & 1))
634 {
635 /* We normally copy the low-numbered register first. However, if
636 the second source register is the same as the first destination
637 register, we must copy in the opposite order. */
638 if (REGNO (src) + 1 == REGNO (dst))
639 return "mov %D1,%D0\n\tmov %1,%0";
640 else
641 return "mov %1,%0\n\tmov %D1,%D0";
642 }
643 else
644 return "movl %1,%0";
645 }
646 else if (GET_CODE (dst) == REG
647 && GET_CODE (src) == CONST_INT
648 && CONST_OK_FOR_LETTER_P (INTVAL (src), 'I'))
649 {
650 if (REGNO (dst) & 1)
651 return "mov %1,%0\n\tmov 0,%D0";
652 else
653 return "movl %1,%0";
654 }
655 else if (GET_CODE (dst) == REG
656 && GET_CODE (src) == MEM)
657 {
658 if (REGNO (dst) & 1)
659 {
660 /* One can optimize a few cases here, but you have to be
661 careful of clobbering registers used in the address and
662 edge conditions. */
663 operands[0] = dst;
664 operands[1] = src;
665 operands[2] = gen_rtx (REG, Pmode, REGNO (dst) + 1);
666 operands[3] = gen_rtx (MEM, word_mode, operands[2]);
667 operands[4] = adj_offsettable_operand (operands[3], UNITS_PER_WORD);
668 output_asm_insn ("lda %1,%2\n\tld %3,%0\n\tld %4,%D0", operands);
669 return "";
670 }
671 else
672 return "ldl %1,%0";
673 }
674 else if (GET_CODE (dst) == MEM
675 && GET_CODE (src) == REG)
676 {
677 if (REGNO (src) & 1)
678 {
679 /* This is handled by emit_move_sequence so we shouldn't get here. */
680 abort ();
681 }
682 return "stl %1,%0";
683 }
684 else
685 abort ();
686 }
687
688 /* Output assembler to move a quad word value. */
689
690 char *
691 i960_output_move_quad (dst, src)
692 rtx dst, src;
693 {
694 rtx operands[7];
695
696 if (GET_CODE (dst) == REG
697 && GET_CODE (src) == REG)
698 {
699 if ((REGNO (src) & 3)
700 || (REGNO (dst) & 3))
701 {
702 /* We normally copy starting with the low numbered register.
703 However, if there is an overlap such that the first dest reg
704 is <= the last source reg but not < the first source reg, we
705 must copy in the opposite order. */
706 if (REGNO (dst) <= REGNO (src) + 3
707 && REGNO (dst) >= REGNO (src))
708 return "mov %F1,%F0\n\tmov %E1,%E0\n\tmov %D1,%D0\n\tmov %1,%0";
709 else
710 return "mov %1,%0\n\tmov %D1,%D0\n\tmov %E1,%E0\n\tmov %F1,%F0";
711 }
712 else
713 return "movq %1,%0";
714 }
715 else if (GET_CODE (dst) == REG
716 && GET_CODE (src) == CONST_INT
717 && CONST_OK_FOR_LETTER_P (INTVAL (src), 'I'))
718 {
719 if (REGNO (dst) & 3)
720 return "mov %1,%0\n\tmov 0,%D0\n\tmov 0,%E0\n\tmov 0,%F0";
721 else
722 return "movq %1,%0";
723 }
724 else if (GET_CODE (dst) == REG
725 && GET_CODE (src) == MEM)
726 {
727 if (REGNO (dst) & 3)
728 {
729 /* One can optimize a few cases here, but you have to be
730 careful of clobbering registers used in the address and
731 edge conditions. */
732 operands[0] = dst;
733 operands[1] = src;
734 operands[2] = gen_rtx (REG, Pmode, REGNO (dst) + 3);
735 operands[3] = gen_rtx (MEM, word_mode, operands[2]);
736 operands[4] = adj_offsettable_operand (operands[3], UNITS_PER_WORD);
737 operands[5] = adj_offsettable_operand (operands[4], UNITS_PER_WORD);
738 operands[6] = adj_offsettable_operand (operands[5], UNITS_PER_WORD);
739 output_asm_insn ("lda %1,%2\n\tld %3,%0\n\tld %4,%D0\n\tld %5,%E0\n\tld %6,%F0", operands);
740 return "";
741 }
742 else
743 return "ldq %1,%0";
744 }
745 else if (GET_CODE (dst) == MEM
746 && GET_CODE (src) == REG)
747 {
748 if (REGNO (src) & 3)
749 {
750 /* This is handled by emit_move_sequence so we shouldn't get here. */
751 abort ();
752 }
753 return "stq %1,%0";
754 }
755 else
756 abort ();
757 }
758 \f
759 /* Emit insns to load a constant to non-floating point registers.
760 Uses several strategies to try to use as few insns as possible. */
761
762 char *
763 i960_output_ldconst (dst, src)
764 register rtx dst, src;
765 {
766 register int rsrc1;
767 register unsigned rsrc2;
768 enum machine_mode mode = GET_MODE (dst);
769 rtx operands[4];
770
771 operands[0] = operands[2] = dst;
772 operands[1] = operands[3] = src;
773
774 /* Anything that isn't a compile time constant, such as a SYMBOL_REF,
775 must be a ldconst insn. */
776
777 if (GET_CODE (src) != CONST_INT && GET_CODE (src) != CONST_DOUBLE)
778 {
779 output_asm_insn ("ldconst %1,%0", operands);
780 return "";
781 }
782 else if (mode == XFmode)
783 {
784 REAL_VALUE_TYPE d;
785 long value_long[3];
786 int i;
787
788 if (fp_literal_zero (src, XFmode))
789 return "movt 0,%0";
790
791 REAL_VALUE_FROM_CONST_DOUBLE (d, src);
792 REAL_VALUE_TO_TARGET_LONG_DOUBLE (d, value_long);
793
794 output_asm_insn ("# ldconst %1,%0",operands);
795
796 for (i = 0; i < 3; i++)
797 {
798 operands[0] = gen_rtx (REG, SImode, REGNO (dst) + i);
799 operands[1] = GEN_INT (value_long[i]);
800 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
801 operands);
802 }
803
804 return "";
805 }
806 else if (mode == DFmode)
807 {
808 rtx first, second;
809
810 if (fp_literal_zero (src, DFmode))
811 return "movl 0,%0";
812
813 split_double (src, &first, &second);
814
815 output_asm_insn ("# ldconst %1,%0",operands);
816
817 operands[0] = gen_rtx (REG, SImode, REGNO (dst));
818 operands[1] = first;
819 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
820 operands);
821 operands[0] = gen_rtx (REG, SImode, REGNO (dst) + 1);
822 operands[1] = second;
823 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
824 operands);
825 return "";
826 }
827 else if (mode == SFmode)
828 {
829 REAL_VALUE_TYPE d;
830 long value;
831
832 REAL_VALUE_FROM_CONST_DOUBLE (d, src);
833 REAL_VALUE_TO_TARGET_SINGLE (d, value);
834
835 output_asm_insn ("# ldconst %1,%0",operands);
836 operands[0] = gen_rtx (REG, SImode, REGNO (dst));
837 operands[1] = GEN_INT (value);
838 output_asm_insn (i960_output_ldconst (operands[0], operands[1]),
839 operands);
840 return "";
841 }
842 else if (mode == TImode)
843 {
844 /* ??? This is currently not handled at all. */
845 abort ();
846
847 /* Note: lowest order word goes in lowest numbered reg. */
848 rsrc1 = INTVAL (src);
849 if (rsrc1 >= 0 && rsrc1 < 32)
850 return "movq %1,%0";
851 else
852 output_asm_insn ("movq\t0,%0\t# ldconstq %1,%0",operands);
853 /* Go pick up the low-order word. */
854 }
855 else if (mode == DImode)
856 {
857 rtx upperhalf, lowerhalf, xoperands[2];
858
859 if (GET_CODE (src) == CONST_DOUBLE || GET_CODE (src) == CONST_INT)
860 split_double (src, &lowerhalf, &upperhalf);
861
862 else
863 abort ();
864
865 /* Note: lowest order word goes in lowest numbered reg. */
866 /* Numbers from 0 to 31 can be handled with a single insn. */
867 rsrc1 = INTVAL (lowerhalf);
868 if (upperhalf == const0_rtx && rsrc1 >= 0 && rsrc1 < 32)
869 return "movl %1,%0";
870
871 /* Output the upper half with a recursive call. */
872 xoperands[0] = gen_rtx (REG, SImode, REGNO (dst) + 1);
873 xoperands[1] = upperhalf;
874 output_asm_insn (i960_output_ldconst (xoperands[0], xoperands[1]),
875 xoperands);
876 /* The lower word is emitted as normally. */
877 }
878 else
879 {
880 rsrc1 = INTVAL (src);
881 if (mode == QImode)
882 {
883 if (rsrc1 > 0xff)
884 rsrc1 &= 0xff;
885 }
886 else if (mode == HImode)
887 {
888 if (rsrc1 > 0xffff)
889 rsrc1 &= 0xffff;
890 }
891 }
892
893 if (rsrc1 >= 0)
894 {
895 /* ldconst 0..31,X -> mov 0..31,X */
896 if (rsrc1 < 32)
897 {
898 if (i960_last_insn_type == I_TYPE_REG && TARGET_C_SERIES)
899 return "lda %1,%0";
900 return "mov %1,%0";
901 }
902
903 /* ldconst 32..63,X -> add 31,nn,X */
904 if (rsrc1 < 63)
905 {
906 if (i960_last_insn_type == I_TYPE_REG && TARGET_C_SERIES)
907 return "lda %1,%0";
908 operands[1] = GEN_INT (rsrc1 - 31);
909 output_asm_insn ("addo\t31,%1,%0\t# ldconst %3,%0", operands);
910 return "";
911 }
912 }
913 else if (rsrc1 < 0)
914 {
915 /* ldconst -1..-31 -> sub 0,0..31,X */
916 if (rsrc1 >= -31)
917 {
918 /* return 'sub -(%1),0,%0' */
919 operands[1] = GEN_INT (- rsrc1);
920 output_asm_insn ("subo\t%1,0,%0\t# ldconst %3,%0", operands);
921 return "";
922 }
923
924 /* ldconst -32 -> not 31,X */
925 if (rsrc1 == -32)
926 {
927 operands[1] = GEN_INT (~rsrc1);
928 output_asm_insn ("not\t%1,%0 # ldconst %3,%0", operands);
929 return "";
930 }
931 }
932
933 /* If const is a single bit. */
934 if (bitpos (rsrc1) >= 0)
935 {
936 operands[1] = GEN_INT (bitpos (rsrc1));
937 output_asm_insn ("setbit\t%1,0,%0\t# ldconst %3,%0", operands);
938 return "";
939 }
940
941 /* If const is a bit string of less than 6 bits (1..31 shifted). */
942 if (is_mask (rsrc1))
943 {
944 int s, e;
945
946 if (bitstr (rsrc1, &s, &e) < 6)
947 {
948 rsrc2 = ((unsigned int) rsrc1) >> s;
949 operands[1] = GEN_INT (rsrc2);
950 operands[2] = GEN_INT (s);
951 output_asm_insn ("shlo\t%2,%1,%0\t# ldconst %3,%0", operands);
952 return "";
953 }
954 }
955
956 /* Unimplemented cases:
957 const is in range 0..31 but rotated around end of word:
958 ror 31,3,g0 -> ldconst 0xe0000003,g0
959
960 and any 2 instruction cases that might be worthwhile */
961
962 output_asm_insn ("ldconst %1,%0", operands);
963 return "";
964 }
965
966 /* Determine if there is an opportunity for a bypass optimization.
967 Bypass succeeds on the 960K* if the destination of the previous
968 instruction is the second operand of the current instruction.
969 Bypass always succeeds on the C*.
970
971 Return 1 if the pattern should interchange the operands.
972
973 CMPBR_FLAG is true if this is for a compare-and-branch insn.
974 OP1 and OP2 are the two source operands of a 3 operand insn. */
975
976 int
977 i960_bypass (insn, op1, op2, cmpbr_flag)
978 register rtx insn, op1, op2;
979 int cmpbr_flag;
980 {
981 register rtx prev_insn, prev_dest;
982
983 if (TARGET_C_SERIES)
984 return 0;
985
986 /* Can't do this if op1 isn't a register. */
987 if (! REG_P (op1))
988 return 0;
989
990 /* Can't do this for a compare-and-branch if both ops aren't regs. */
991 if (cmpbr_flag && ! REG_P (op2))
992 return 0;
993
994 prev_insn = prev_real_insn (insn);
995
996 if (prev_insn && GET_CODE (prev_insn) == INSN
997 && GET_CODE (PATTERN (prev_insn)) == SET)
998 {
999 prev_dest = SET_DEST (PATTERN (prev_insn));
1000 if ((GET_CODE (prev_dest) == REG && REGNO (prev_dest) == REGNO (op1))
1001 || (GET_CODE (prev_dest) == SUBREG
1002 && GET_CODE (SUBREG_REG (prev_dest)) == REG
1003 && REGNO (SUBREG_REG (prev_dest)) == REGNO (op1)))
1004 return 1;
1005 }
1006 return 0;
1007 }
1008 \f
1009 /* Output the code which declares the function name. This also handles
1010 leaf routines, which have special requirements, and initializes some
1011 global variables. */
1012
1013 void
1014 i960_function_name_declare (file, name, fndecl)
1015 FILE *file;
1016 char *name;
1017 tree fndecl;
1018 {
1019 register int i, j;
1020 int leaf_proc_ok;
1021 rtx insn;
1022
1023 /* Increment global return label. */
1024
1025 ret_label++;
1026
1027 /* Compute whether tail calls and leaf routine optimizations can be performed
1028 for this function. */
1029
1030 if (TARGET_TAILCALL)
1031 tail_call_ok = 1;
1032 else
1033 tail_call_ok = 0;
1034
1035 if (TARGET_LEAFPROC)
1036 leaf_proc_ok = 1;
1037 else
1038 leaf_proc_ok = 0;
1039
1040 /* Even if nobody uses extra parms, can't have leafproc or tail calls if
1041 argblock, because argblock uses g14 implicitly. */
1042
1043 if (current_function_args_size != 0 || VARARGS_STDARG_FUNCTION (fndecl))
1044 {
1045 tail_call_ok = 0;
1046 leaf_proc_ok = 0;
1047 }
1048
1049 /* See if caller passes in an address to return value. */
1050
1051 if (aggregate_value_p (DECL_RESULT (fndecl)))
1052 {
1053 tail_call_ok = 0;
1054 leaf_proc_ok = 0;
1055 }
1056
1057 /* Can not use tail calls or make this a leaf routine if there is a non
1058 zero frame size. */
1059
1060 if (get_frame_size () != 0)
1061 leaf_proc_ok = 0;
1062
1063 /* I don't understand this condition, and do not think that it is correct.
1064 Apparently this is just checking whether the frame pointer is used, and
1065 we can't trust regs_ever_live[fp] since it is (almost?) always set. */
1066
1067 if (tail_call_ok)
1068 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1069 if (GET_CODE (insn) == INSN
1070 && reg_mentioned_p (frame_pointer_rtx, insn))
1071 {
1072 tail_call_ok = 0;
1073 break;
1074 }
1075
1076 /* Check for CALL insns. Can not be a leaf routine if there are any. */
1077
1078 if (leaf_proc_ok)
1079 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1080 if (GET_CODE (insn) == CALL_INSN)
1081 {
1082 leaf_proc_ok = 0;
1083 break;
1084 }
1085
1086 /* Can not be a leaf routine if any non-call clobbered registers are
1087 used in this function. */
1088
1089 if (leaf_proc_ok)
1090 for (i = 0, j = 0; i < FIRST_PSEUDO_REGISTER; i++)
1091 if (regs_ever_live[i]
1092 && ((! call_used_regs[i]) || (i > 7 && i < 12)))
1093 {
1094 /* Global registers. */
1095 if (i < 16 && i > 7 && i != 13)
1096 leaf_proc_ok = 0;
1097 /* Local registers. */
1098 else if (i < 32)
1099 leaf_proc_ok = 0;
1100 }
1101
1102 /* Now choose a leaf return register, if we can find one, and if it is
1103 OK for this to be a leaf routine. */
1104
1105 i960_leaf_ret_reg = -1;
1106
1107 if (optimize && leaf_proc_ok)
1108 {
1109 for (i960_leaf_ret_reg = -1, i = 0; i < 8; i++)
1110 if (regs_ever_live[i] == 0)
1111 {
1112 i960_leaf_ret_reg = i;
1113 regs_ever_live[i] = 1;
1114 break;
1115 }
1116 }
1117
1118 /* Do this after choosing the leaf return register, so it will be listed
1119 if one was chosen. */
1120
1121 fprintf (file, "\t# Function '%s'\n", (name[0] == '*' ? &name[1] : name));
1122 fprintf (file, "\t# Registers used: ");
1123
1124 for (i = 0, j = 0; i < FIRST_PSEUDO_REGISTER; i++)
1125 {
1126 if (regs_ever_live[i])
1127 {
1128 fprintf (file, "%s%s ", reg_names[i], call_used_regs[i] ? "" : "*");
1129
1130 if (i > 15 && j == 0)
1131 {
1132 fprintf (file,"\n\t#\t\t ");
1133 j++;
1134 }
1135 }
1136 }
1137
1138 fprintf (file, "\n");
1139
1140 if (i960_leaf_ret_reg >= 0)
1141 {
1142 /* Make it a leaf procedure. */
1143
1144 if (TREE_PUBLIC (fndecl))
1145 fprintf (file,"\t.globl\t%s.lf\n", (name[0] == '*' ? &name[1] : name));
1146
1147 fprintf (file, "\t.leafproc\t");
1148 assemble_name (file, name);
1149 fprintf (file, ",%s.lf\n", (name[0] == '*' ? &name[1] : name));
1150 ASM_OUTPUT_LABEL (file, name);
1151 fprintf (file, "\tlda LR%d,g14\n", ret_label);
1152 fprintf (file, "%s.lf:\n", (name[0] == '*' ? &name[1] : name));
1153 fprintf (file, "\tmov g14,g%d\n", i960_leaf_ret_reg);
1154
1155 if (TARGET_C_SERIES)
1156 {
1157 fprintf (file, "\tlda 0,g14\n");
1158 i960_last_insn_type = I_TYPE_MEM;
1159 }
1160 else
1161 {
1162 fprintf (file, "\tmov 0,g14\n");
1163 i960_last_insn_type = I_TYPE_REG;
1164 }
1165 }
1166 else
1167 {
1168 ASM_OUTPUT_LABEL (file, name);
1169 i960_last_insn_type = I_TYPE_CTRL;
1170 }
1171 }
1172 \f
1173 /* Compute and return the frame size. */
1174
1175 int
1176 compute_frame_size (size)
1177 int size;
1178 {
1179 int actual_fsize;
1180 int outgoing_args_size = current_function_outgoing_args_size;
1181
1182 /* The STARTING_FRAME_OFFSET is totally hidden to us as far
1183 as size is concerned. */
1184 actual_fsize = (size + 15) & -16;
1185 actual_fsize += (outgoing_args_size + 15) & -16;
1186
1187 return actual_fsize;
1188 }
1189
1190 /* Output code for the function prologue. */
1191
1192 void
1193 i960_function_prologue (file, size)
1194 FILE *file;
1195 unsigned int size;
1196 {
1197 register int i, j, nr;
1198 int n_iregs = 0;
1199 int rsize = 0;
1200 int actual_fsize, offset;
1201 char tmpstr[1000];
1202 /* -1 if reg must be saved on proc entry, 0 if available, 1 if saved
1203 somewhere. */
1204 int regs[FIRST_PSEUDO_REGISTER];
1205
1206 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1207 if (regs_ever_live[i]
1208 && ((! call_used_regs[i]) || (i > 7 && i < 12)))
1209 {
1210 regs[i] = -1;
1211 /* Count global registers that need saving. */
1212 if (i < 16)
1213 n_iregs++;
1214 }
1215 else
1216 regs[i] = 0;
1217
1218 epilogue_string[0] = '\0';
1219
1220 if (profile_flag || profile_block_flag)
1221 {
1222 /* When profiling, we may use registers 20 to 27 to save arguments, so
1223 they can't be used here for saving globals. J is the number of
1224 argument registers the mcount call will save. */
1225 for (j = 7; j >= 0 && ! regs_ever_live[j]; j--)
1226 ;
1227
1228 for (i = 20; i <= j + 20; i++)
1229 regs[i] = -1;
1230 }
1231
1232 /* First look for local registers to save globals in. */
1233 for (i = 0; i < 16; i++)
1234 {
1235 if (regs[i] == 0)
1236 continue;
1237
1238 /* Start at r4, not r3. */
1239 for (j = 20; j < 32; j++)
1240 {
1241 if (regs[j] != 0)
1242 continue;
1243
1244 regs[i] = 1;
1245 regs[j] = -1;
1246 regs_ever_live[j] = 1;
1247 nr = 1;
1248 if (i <= 14 && i % 2 == 0 && j <= 30 && j % 2 == 0
1249 && regs[i+1] != 0 && regs[j+1] == 0)
1250 {
1251 nr = 2;
1252 regs[i+1] = 1;
1253 regs[j+1] = -1;
1254 regs_ever_live[j+1] = 1;
1255 }
1256 if (nr == 2 && i <= 12 && i % 4 == 0 && j <= 28 && j % 4 == 0
1257 && regs[i+2] != 0 && regs[j+2] == 0)
1258 {
1259 nr = 3;
1260 regs[i+2] = 1;
1261 regs[j+2] = -1;
1262 regs_ever_live[j+2] = 1;
1263 }
1264 if (nr == 3 && regs[i+3] != 0 && regs[j+3] == 0)
1265 {
1266 nr = 4;
1267 regs[i+3] = 1;
1268 regs[j+3] = -1;
1269 regs_ever_live[j+3] = 1;
1270 }
1271
1272 fprintf (file, "\tmov%s %s,%s\n",
1273 ((nr == 4) ? "q" :
1274 (nr == 3) ? "t" :
1275 (nr == 2) ? "l" : ""),
1276 reg_names[i], reg_names[j]);
1277 sprintf (tmpstr, "\tmov%s %s,%s\n",
1278 ((nr == 4) ? "q" :
1279 (nr == 3) ? "t" :
1280 (nr == 2) ? "l" : ""),
1281 reg_names[j], reg_names[i]);
1282 strcat (epilogue_string, tmpstr);
1283
1284 n_iregs -= nr;
1285 i += nr-1;
1286 break;
1287 }
1288 }
1289
1290 /* N_iregs is now the number of global registers that haven't been saved
1291 yet. */
1292
1293 rsize = (n_iregs * 4);
1294 actual_fsize = compute_frame_size (size) + rsize;
1295 #if 0
1296 /* ??? The 1.2.1 compiler does this also. This is meant to round the frame
1297 size up to the nearest multiple of 16. I don't know whether this is
1298 necessary, or even desirable.
1299
1300 The frame pointer must be aligned, but the call instruction takes care of
1301 that. If we leave the stack pointer unaligned, we may save a little on
1302 dynamic stack allocation. And we don't lose, at least according to the
1303 i960CA manual. */
1304 actual_fsize = (actual_fsize + 15) & ~0xF;
1305 #endif
1306
1307 /* Allocate space for register save and locals. */
1308 if (actual_fsize > 0)
1309 {
1310 if (actual_fsize < 32)
1311 fprintf (file, "\taddo %d,sp,sp\n", actual_fsize);
1312 else
1313 fprintf (file, "\tlda\t%d(sp),sp\n", actual_fsize);
1314 }
1315
1316 /* Take hardware register save area created by the call instruction
1317 into account, but store them before the argument block area. */
1318 offset = 64 + actual_fsize - compute_frame_size (0) - rsize;
1319 /* Save registers on stack if needed. */
1320 for (i = 0, j = n_iregs; j > 0 && i < 16; i++)
1321 {
1322 if (regs[i] != -1)
1323 continue;
1324
1325 nr = 1;
1326
1327 if (i <= 14 && i % 2 == 0 && regs[i+1] == -1 && offset % 2 == 0)
1328 nr = 2;
1329
1330 if (nr == 2 && i <= 12 && i % 4 == 0 && regs[i+2] == -1
1331 && offset % 4 == 0)
1332 nr = 3;
1333
1334 if (nr == 3 && regs[i+3] == -1)
1335 nr = 4;
1336
1337 fprintf (file,"\tst%s %s,%d(fp)\n",
1338 ((nr == 4) ? "q" :
1339 (nr == 3) ? "t" :
1340 (nr == 2) ? "l" : ""),
1341 reg_names[i], offset);
1342 sprintf (tmpstr,"\tld%s %d(fp),%s\n",
1343 ((nr == 4) ? "q" :
1344 (nr == 3) ? "t" :
1345 (nr == 2) ? "l" : ""),
1346 offset, reg_names[i]);
1347 strcat (epilogue_string, tmpstr);
1348 i += nr-1;
1349 j -= nr;
1350 offset += nr * 4;
1351 }
1352
1353 if (actual_fsize == 0 && size == 0 && rsize == 0)
1354 return;
1355
1356 fprintf (file, "\t#Prologue stats:\n");
1357 fprintf (file, "\t# Total Frame Size: %d bytes\n", actual_fsize);
1358
1359 if (size)
1360 fprintf (file, "\t# Local Variable Size: %d bytes\n", size);
1361 if (rsize)
1362 fprintf (file, "\t# Register Save Size: %d regs, %d bytes\n",
1363 n_iregs, rsize);
1364 fprintf (file, "\t#End Prologue#\n");
1365 }
1366
1367 /* Output code for the function profiler. */
1368
1369 void
1370 output_function_profiler (file, labelno)
1371 FILE *file;
1372 int labelno;
1373 {
1374 /* The last used parameter register. */
1375 int last_parm_reg;
1376 int i, j, increment;
1377 int varargs_stdarg_function
1378 = VARARGS_STDARG_FUNCTION (current_function_decl);
1379
1380 /* Figure out the last used parameter register. The proper thing to do
1381 is to walk incoming args of the function. A function might have live
1382 parameter registers even if it has no incoming args. Note that we
1383 don't have to save parameter registers g8 to g11 because they are
1384 call preserved. */
1385
1386 /* See also output_function_prologue, which tries to use local registers
1387 for preserved call-saved global registers. */
1388
1389 for (last_parm_reg = 7;
1390 last_parm_reg >= 0 && ! regs_ever_live[last_parm_reg];
1391 last_parm_reg--)
1392 ;
1393
1394 /* Save parameter registers in regs r4 (20) to r11 (27). */
1395
1396 for (i = 0, j = 4; i <= last_parm_reg; i += increment, j += increment)
1397 {
1398 if (i % 4 == 0 && (last_parm_reg - i) >= 3)
1399 increment = 4;
1400 else if (i % 4 == 0 && (last_parm_reg - i) >= 2)
1401 increment = 3;
1402 else if (i % 2 == 0 && (last_parm_reg - i) >= 1)
1403 increment = 2;
1404 else
1405 increment = 1;
1406
1407 fprintf (file, "\tmov%s g%d,r%d\n",
1408 (increment == 4 ? "q" : increment == 3 ? "t"
1409 : increment == 2 ? "l": ""), i, j);
1410 }
1411
1412 /* If this function uses the arg pointer, then save it in r3 and then
1413 set it to zero. */
1414
1415 if (current_function_args_size != 0 || varargs_stdarg_function)
1416 fprintf (file, "\tmov g14,r3\n\tmov 0,g14\n");
1417
1418 /* Load location address into g0 and call mcount. */
1419
1420 fprintf (file, "\tlda\tLP%d,g0\n\tcallx\tmcount\n", labelno);
1421
1422 /* If this function uses the arg pointer, restore it. */
1423
1424 if (current_function_args_size != 0 || varargs_stdarg_function)
1425 fprintf (file, "\tmov r3,g14\n");
1426
1427 /* Restore parameter registers. */
1428
1429 for (i = 0, j = 4; i <= last_parm_reg; i += increment, j += increment)
1430 {
1431 if (i % 4 == 0 && (last_parm_reg - i) >= 3)
1432 increment = 4;
1433 else if (i % 4 == 0 && (last_parm_reg - i) >= 2)
1434 increment = 3;
1435 else if (i % 2 == 0 && (last_parm_reg - i) >= 1)
1436 increment = 2;
1437 else
1438 increment = 1;
1439
1440 fprintf (file, "\tmov%s r%d,g%d\n",
1441 (increment == 4 ? "q" : increment == 3 ? "t"
1442 : increment == 2 ? "l": ""), j, i);
1443 }
1444 }
1445
1446 /* Output code for the function epilogue. */
1447
1448 void
1449 i960_function_epilogue (file, size)
1450 FILE *file;
1451 unsigned int size;
1452 {
1453 if (i960_leaf_ret_reg >= 0)
1454 {
1455 fprintf (file, "LR%d: ret\n", ret_label);
1456 return;
1457 }
1458
1459 if (*epilogue_string == 0)
1460 {
1461 register rtx tmp;
1462
1463 /* Emit a return insn, but only if control can fall through to here. */
1464
1465 tmp = get_last_insn ();
1466 while (tmp)
1467 {
1468 if (GET_CODE (tmp) == BARRIER)
1469 return;
1470 if (GET_CODE (tmp) == CODE_LABEL)
1471 break;
1472 if (GET_CODE (tmp) == JUMP_INSN)
1473 {
1474 if (GET_CODE (PATTERN (tmp)) == RETURN)
1475 return;
1476 break;
1477 }
1478 if (GET_CODE (tmp) == NOTE)
1479 {
1480 tmp = PREV_INSN (tmp);
1481 continue;
1482 }
1483 break;
1484 }
1485 fprintf (file, "LR%d: ret\n", ret_label);
1486 return;
1487 }
1488
1489 fprintf (file, "LR%d:\n", ret_label);
1490
1491 fprintf (file, "\t#EPILOGUE#\n");
1492
1493 /* Output the string created by the prologue which will restore all
1494 registers saved by the prologue. */
1495
1496 if (epilogue_string[0] != '\0')
1497 fprintf (file, "%s", epilogue_string);
1498
1499 /* Must clear g14 on return if this function set it.
1500 Only varargs/stdarg functions modify g14. */
1501
1502 if (VARARGS_STDARG_FUNCTION (current_function_decl))
1503 fprintf (file, "\tmov 0,g14\n");
1504
1505 fprintf (file, "\tret\n");
1506 fprintf (file, "\t#End Epilogue#\n");
1507 }
1508
1509 /* Output code for a call insn. */
1510
1511 char *
1512 i960_output_call_insn (target, argsize_rtx, arg_pointer, insn)
1513 register rtx target, argsize_rtx, arg_pointer, insn;
1514 {
1515 int argsize = INTVAL (argsize_rtx);
1516 rtx nexti = next_real_insn (insn);
1517 rtx operands[2];
1518 int varargs_stdarg_function
1519 = VARARGS_STDARG_FUNCTION (current_function_decl);
1520
1521 operands[0] = target;
1522 operands[1] = arg_pointer;
1523
1524 if (current_function_args_size != 0 || varargs_stdarg_function)
1525 output_asm_insn ("mov g14,r3", operands);
1526
1527 if (argsize > 48)
1528 output_asm_insn ("lda %a1,g14", operands);
1529 else if (current_function_args_size != 0 || varargs_stdarg_function)
1530 output_asm_insn ("mov 0,g14", operands);
1531
1532 /* The code used to assume that calls to SYMBOL_REFs could not be more
1533 than 24 bits away (b vs bx, callj vs callx). This is not true. This
1534 feature is now implemented by relaxing in the GNU linker. It can convert
1535 bx to b if in range, and callx to calls/call/balx/bal as appropriate. */
1536
1537 /* Nexti could be zero if the called routine is volatile. */
1538 if (optimize && (*epilogue_string == 0) && argsize == 0 && tail_call_ok
1539 && (nexti == 0 || GET_CODE (PATTERN (nexti)) == RETURN))
1540 {
1541 /* Delete following return insn. */
1542 if (nexti && no_labels_between_p (insn, nexti))
1543 delete_insn (nexti);
1544 output_asm_insn ("bx %0", operands);
1545 return "# notreached";
1546 }
1547
1548 output_asm_insn ("callx %0", operands);
1549
1550 /* If the caller sets g14 to the address of the argblock, then the caller
1551 must clear it after the return. */
1552
1553 if (current_function_args_size != 0 || varargs_stdarg_function)
1554 output_asm_insn ("mov r3,g14", operands);
1555 else if (argsize > 48)
1556 output_asm_insn ("mov 0,g14", operands);
1557
1558 return "";
1559 }
1560
1561 /* Output code for a return insn. */
1562
1563 char *
1564 i960_output_ret_insn (insn)
1565 register rtx insn;
1566 {
1567 static char lbuf[20];
1568
1569 if (*epilogue_string != 0)
1570 {
1571 if (! TARGET_CODE_ALIGN && next_real_insn (insn) == 0)
1572 return "";
1573
1574 sprintf (lbuf, "b LR%d", ret_label);
1575 return lbuf;
1576 }
1577
1578 /* Must clear g14 on return if this function set it.
1579 Only varargs/stdarg functions modify g14. */
1580
1581 if (VARARGS_STDARG_FUNCTION (current_function_decl))
1582 output_asm_insn ("mov 0,g14", 0);
1583
1584 if (i960_leaf_ret_reg >= 0)
1585 {
1586 sprintf (lbuf, "bx (%s)", reg_names[i960_leaf_ret_reg]);
1587 return lbuf;
1588 }
1589 return "ret";
1590 }
1591 \f
1592 #if 0
1593 /* Return a character string representing the branch prediction
1594 opcode to be tacked on an instruction. This must at least
1595 return a null string. */
1596
1597 char *
1598 i960_br_predict_opcode (lab_ref, insn)
1599 rtx lab_ref, insn;
1600 {
1601 if (TARGET_BRANCH_PREDICT)
1602 {
1603 unsigned long label_uid;
1604
1605 if (GET_CODE (lab_ref) == CODE_LABEL)
1606 label_uid = INSN_UID (lab_ref);
1607 else if (GET_CODE (lab_ref) == LABEL_REF)
1608 label_uid = INSN_UID (XEXP (lab_ref, 0));
1609 else
1610 return ".f";
1611
1612 /* If not optimizing, then the insn_addresses array will not be
1613 valid. In this case, always return ".t" since most branches
1614 are taken. If optimizing, return .t for backward branches
1615 and .f for forward branches. */
1616 if (! optimize
1617 || insn_addresses[label_uid] < insn_addresses[INSN_UID (insn)])
1618 return ".t";
1619 return ".f";
1620 }
1621
1622 return "";
1623 }
1624 #endif
1625
1626 /* Print the operand represented by rtx X formatted by code CODE. */
1627
1628 void
1629 i960_print_operand (file, x, code)
1630 FILE *file;
1631 rtx x;
1632 char code;
1633 {
1634 enum rtx_code rtxcode = GET_CODE (x);
1635
1636 if (rtxcode == REG)
1637 {
1638 switch (code)
1639 {
1640 case 'D':
1641 /* Second reg of a double or quad. */
1642 fprintf (file, "%s", reg_names[REGNO (x)+1]);
1643 break;
1644
1645 case 'E':
1646 /* Third reg of a quad. */
1647 fprintf (file, "%s", reg_names[REGNO (x)+2]);
1648 break;
1649
1650 case 'F':
1651 /* Fourth reg of a quad. */
1652 fprintf (file, "%s", reg_names[REGNO (x)+3]);
1653 break;
1654
1655 case 0:
1656 fprintf (file, "%s", reg_names[REGNO (x)]);
1657 break;
1658
1659 default:
1660 abort ();
1661 }
1662 return;
1663 }
1664 else if (rtxcode == MEM)
1665 {
1666 output_address (XEXP (x, 0));
1667 return;
1668 }
1669 else if (rtxcode == CONST_INT)
1670 {
1671 HOST_WIDE_INT val = INTVAL (x);
1672 if (code == 'C')
1673 val = ~val;
1674 if (val > 9999 || val < -999)
1675 fprintf (file, "0x%x", val);
1676 else
1677 fprintf (file, "%d", val);
1678 return;
1679 }
1680 else if (rtxcode == CONST_DOUBLE)
1681 {
1682 REAL_VALUE_TYPE d;
1683 char dstr[30];
1684
1685 if (x == CONST0_RTX (GET_MODE (x)))
1686 {
1687 fprintf (file, "0f0.0");
1688 return;
1689 }
1690 else if (x == CONST1_RTX (GET_MODE (x)))
1691 {
1692 fprintf (file, "0f1.0");
1693 return;
1694 }
1695
1696 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1697 REAL_VALUE_TO_DECIMAL (d, "%#g", dstr);
1698 fprintf (file, "0f%s", dstr);
1699 return;
1700 }
1701
1702 switch(code)
1703 {
1704 case 'B':
1705 /* Branch or jump, depending on assembler. */
1706 if (TARGET_ASM_COMPAT)
1707 fputs ("j", file);
1708 else
1709 fputs ("b", file);
1710 break;
1711
1712 case 'S':
1713 /* Sign of condition. */
1714 if ((rtxcode == EQ) || (rtxcode == NE) || (rtxcode == GTU)
1715 || (rtxcode == LTU) || (rtxcode == GEU) || (rtxcode == LEU))
1716 fputs ("o", file);
1717 else if ((rtxcode == GT) || (rtxcode == LT)
1718 || (rtxcode == GE) || (rtxcode == LE))
1719 fputs ("i", file);
1720 else
1721 abort();
1722 break;
1723
1724 case 'I':
1725 /* Inverted condition. */
1726 rtxcode = reverse_condition (rtxcode);
1727 goto normal;
1728
1729 case 'X':
1730 /* Inverted condition w/ reversed operands. */
1731 rtxcode = reverse_condition (rtxcode);
1732 /* Fallthrough. */
1733
1734 case 'R':
1735 /* Reversed operand condition. */
1736 rtxcode = swap_condition (rtxcode);
1737 /* Fallthrough. */
1738
1739 case 'C':
1740 /* Normal condition. */
1741 normal:
1742 if (rtxcode == EQ) { fputs ("e", file); return; }
1743 else if (rtxcode == NE) { fputs ("ne", file); return; }
1744 else if (rtxcode == GT) { fputs ("g", file); return; }
1745 else if (rtxcode == GTU) { fputs ("g", file); return; }
1746 else if (rtxcode == LT) { fputs ("l", file); return; }
1747 else if (rtxcode == LTU) { fputs ("l", file); return; }
1748 else if (rtxcode == GE) { fputs ("ge", file); return; }
1749 else if (rtxcode == GEU) { fputs ("ge", file); return; }
1750 else if (rtxcode == LE) { fputs ("le", file); return; }
1751 else if (rtxcode == LEU) { fputs ("le", file); return; }
1752 else abort ();
1753 break;
1754
1755 case 0:
1756 output_addr_const (file, x);
1757 break;
1758
1759 default:
1760 abort ();
1761 }
1762
1763 return;
1764 }
1765 \f
1766 /* Print a memory address as an operand to reference that memory location.
1767
1768 This is exactly the same as legitimate_address_p, except that it the prints
1769 addresses instead of recognizing them. */
1770
1771 void
1772 i960_print_operand_addr (file, addr)
1773 FILE *file;
1774 register rtx addr;
1775 {
1776 rtx breg, ireg;
1777 rtx scale, offset;
1778
1779 ireg = 0;
1780 breg = 0;
1781 offset = 0;
1782 scale = const1_rtx;
1783
1784 if (GET_CODE (addr) == REG)
1785 breg = addr;
1786 else if (CONSTANT_P (addr))
1787 offset = addr;
1788 else if (GET_CODE (addr) == PLUS)
1789 {
1790 rtx op0, op1;
1791
1792 op0 = XEXP (addr, 0);
1793 op1 = XEXP (addr, 1);
1794
1795 if (GET_CODE (op0) == REG)
1796 {
1797 breg = op0;
1798 if (GET_CODE (op1) == REG)
1799 ireg = op1;
1800 else if (CONSTANT_P (op1))
1801 offset = op1;
1802 else
1803 abort ();
1804 }
1805 else if (GET_CODE (op0) == PLUS)
1806 {
1807 if (GET_CODE (XEXP (op0, 0)) == MULT)
1808 {
1809 ireg = XEXP (XEXP (op0, 0), 0);
1810 scale = XEXP (XEXP (op0, 0), 1);
1811 if (GET_CODE (XEXP (op0, 1)) == REG)
1812 {
1813 breg = XEXP (op0, 1);
1814 offset = op1;
1815 }
1816 else
1817 abort ();
1818 }
1819 else if (GET_CODE (XEXP (op0, 0)) == REG)
1820 {
1821 breg = XEXP (op0, 0);
1822 if (GET_CODE (XEXP (op0, 1)) == REG)
1823 {
1824 ireg = XEXP (op0, 1);
1825 offset = op1;
1826 }
1827 else
1828 abort ();
1829 }
1830 else
1831 abort ();
1832 }
1833 else if (GET_CODE (op0) == MULT)
1834 {
1835 ireg = XEXP (op0, 0);
1836 scale = XEXP (op0, 1);
1837 if (GET_CODE (op1) == REG)
1838 breg = op1;
1839 else if (CONSTANT_P (op1))
1840 offset = op1;
1841 else
1842 abort ();
1843 }
1844 else
1845 abort ();
1846 }
1847 else if (GET_CODE (addr) == MULT)
1848 {
1849 ireg = XEXP (addr, 0);
1850 scale = XEXP (addr, 1);
1851 }
1852 else
1853 abort ();
1854
1855 if (offset)
1856 output_addr_const (file, offset);
1857 if (breg)
1858 fprintf (file, "(%s)", reg_names[REGNO (breg)]);
1859 if (ireg)
1860 fprintf (file, "[%s*%d]", reg_names[REGNO (ireg)], INTVAL (scale));
1861 }
1862 \f
1863 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
1864 that is a valid memory address for an instruction.
1865 The MODE argument is the machine mode for the MEM expression
1866 that wants to use this address.
1867
1868 On 80960, legitimate addresses are:
1869 base ld (g0),r0
1870 disp (12 or 32 bit) ld foo,r0
1871 base + index ld (g0)[g1*1],r0
1872 base + displ ld 0xf00(g0),r0
1873 base + index*scale + displ ld 0xf00(g0)[g1*4],r0
1874 index*scale + base ld (g0)[g1*4],r0
1875 index*scale + displ ld 0xf00[g1*4],r0
1876 index*scale ld [g1*4],r0
1877 index + base + displ ld 0xf00(g0)[g1*1],r0
1878
1879 In each case, scale can be 1, 2, 4, 8, or 16. */
1880
1881 /* This is exactly the same as i960_print_operand_addr, except that
1882 it recognizes addresses instead of printing them.
1883
1884 It only recognizes address in canonical form. LEGITIMIZE_ADDRESS should
1885 convert common non-canonical forms to canonical form so that they will
1886 be recognized. */
1887
1888 /* These two macros allow us to accept either a REG or a SUBREG anyplace
1889 where a register is valid. */
1890
1891 #define RTX_OK_FOR_BASE_P(X, STRICT) \
1892 ((GET_CODE (X) == REG \
1893 && (STRICT ? REG_OK_FOR_BASE_P_STRICT (X) : REG_OK_FOR_BASE_P (X))) \
1894 || (GET_CODE (X) == SUBREG \
1895 && GET_CODE (SUBREG_REG (X)) == REG \
1896 && (STRICT ? REG_OK_FOR_BASE_P_STRICT (SUBREG_REG (X)) \
1897 : REG_OK_FOR_BASE_P (SUBREG_REG (X)))))
1898
1899 #define RTX_OK_FOR_INDEX_P(X, STRICT) \
1900 ((GET_CODE (X) == REG \
1901 && (STRICT ? REG_OK_FOR_INDEX_P_STRICT (X) : REG_OK_FOR_INDEX_P (X)))\
1902 || (GET_CODE (X) == SUBREG \
1903 && GET_CODE (SUBREG_REG (X)) == REG \
1904 && (STRICT ? REG_OK_FOR_INDEX_P_STRICT (SUBREG_REG (X)) \
1905 : REG_OK_FOR_INDEX_P (SUBREG_REG (X)))))
1906
1907 int
1908 legitimate_address_p (mode, addr, strict)
1909 enum machine_mode mode;
1910 register rtx addr;
1911 int strict;
1912 {
1913 if (RTX_OK_FOR_BASE_P (addr, strict))
1914 return 1;
1915 else if (CONSTANT_P (addr))
1916 return 1;
1917 else if (GET_CODE (addr) == PLUS)
1918 {
1919 rtx op0, op1;
1920
1921 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
1922 return 0;
1923
1924 op0 = XEXP (addr, 0);
1925 op1 = XEXP (addr, 1);
1926
1927 if (RTX_OK_FOR_BASE_P (op0, strict))
1928 {
1929 if (RTX_OK_FOR_INDEX_P (op1, strict))
1930 return 1;
1931 else if (CONSTANT_P (op1))
1932 return 1;
1933 else
1934 return 0;
1935 }
1936 else if (GET_CODE (op0) == PLUS)
1937 {
1938 if (GET_CODE (XEXP (op0, 0)) == MULT)
1939 {
1940 if (! (RTX_OK_FOR_INDEX_P (XEXP (XEXP (op0, 0), 0), strict)
1941 && SCALE_TERM_P (XEXP (XEXP (op0, 0), 1))))
1942 return 0;
1943
1944 if (RTX_OK_FOR_BASE_P (XEXP (op0, 1), strict)
1945 && CONSTANT_P (op1))
1946 return 1;
1947 else
1948 return 0;
1949 }
1950 else if (RTX_OK_FOR_BASE_P (XEXP (op0, 0), strict))
1951 {
1952 if (RTX_OK_FOR_INDEX_P (XEXP (op0, 1), strict)
1953 && CONSTANT_P (op1))
1954 return 1;
1955 else
1956 return 0;
1957 }
1958 else
1959 return 0;
1960 }
1961 else if (GET_CODE (op0) == MULT)
1962 {
1963 if (! (RTX_OK_FOR_INDEX_P (XEXP (op0, 0), strict)
1964 && SCALE_TERM_P (XEXP (op0, 1))))
1965 return 0;
1966
1967 if (RTX_OK_FOR_BASE_P (op1, strict))
1968 return 1;
1969 else if (CONSTANT_P (op1))
1970 return 1;
1971 else
1972 return 0;
1973 }
1974 else
1975 return 0;
1976 }
1977 else if (GET_CODE (addr) == MULT)
1978 {
1979 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
1980 return 0;
1981
1982 return (RTX_OK_FOR_INDEX_P (XEXP (addr, 0), strict)
1983 && SCALE_TERM_P (XEXP (addr, 1)));
1984 }
1985 else
1986 return 0;
1987 }
1988
1989 /* Try machine-dependent ways of modifying an illegitimate address
1990 to be legitimate. If we find one, return the new, valid address.
1991 This macro is used in only one place: `memory_address' in explow.c.
1992
1993 This converts some non-canonical addresses to canonical form so they
1994 can be recognized. */
1995
1996 rtx
1997 legitimize_address (x, oldx, mode)
1998 register rtx x;
1999 register rtx oldx;
2000 enum machine_mode mode;
2001 {
2002 if (GET_CODE (x) == SYMBOL_REF)
2003 {
2004 abort ();
2005 x = copy_to_reg (x);
2006 }
2007
2008 if (! TARGET_COMPLEX_ADDR && ! reload_completed)
2009 return x;
2010
2011 /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
2012 into (plus (plus (mult (reg) (const)) (reg)) (const)). This can be
2013 created by virtual register instantiation, register elimination, and
2014 similar optimizations. */
2015 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT
2016 && GET_CODE (XEXP (x, 1)) == PLUS)
2017 x = gen_rtx (PLUS, Pmode,
2018 gen_rtx (PLUS, Pmode, XEXP (x, 0), XEXP (XEXP (x, 1), 0)),
2019 XEXP (XEXP (x, 1), 1));
2020
2021 /* Canonicalize (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
2022 into (plus (plus (mult (reg) (const)) (reg)) (const)). */
2023 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
2024 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2025 && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
2026 && CONSTANT_P (XEXP (x, 1)))
2027 {
2028 rtx constant, other;
2029
2030 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2031 {
2032 constant = XEXP (x, 1);
2033 other = XEXP (XEXP (XEXP (x, 0), 1), 1);
2034 }
2035 else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
2036 {
2037 constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
2038 other = XEXP (x, 1);
2039 }
2040 else
2041 constant = 0;
2042
2043 if (constant)
2044 x = gen_rtx (PLUS, Pmode,
2045 gen_rtx (PLUS, Pmode, XEXP (XEXP (x, 0), 0),
2046 XEXP (XEXP (XEXP (x, 0), 1), 0)),
2047 plus_constant (other, INTVAL (constant)));
2048 }
2049
2050 return x;
2051 }
2052 \f
2053 #if 0
2054 /* Return the most stringent alignment that we are willing to consider
2055 objects of size SIZE and known alignment ALIGN as having. */
2056
2057 int
2058 i960_alignment (size, align)
2059 int size;
2060 int align;
2061 {
2062 int i;
2063
2064 if (! TARGET_STRICT_ALIGN)
2065 if (TARGET_IC_COMPAT2_0 || align >= 4)
2066 {
2067 i = i960_object_bytes_bitalign (size) / BITS_PER_UNIT;
2068 if (i > align)
2069 align = i;
2070 }
2071
2072 return align;
2073 }
2074 #endif
2075 \f
2076
2077 int
2078 hard_regno_mode_ok (regno, mode)
2079 int regno;
2080 enum machine_mode mode;
2081 {
2082 if (regno < 32)
2083 {
2084 switch (mode)
2085 {
2086 case CCmode: case CC_UNSmode: case CC_CHKmode:
2087 return 0;
2088
2089 case DImode: case DFmode:
2090 return (regno & 1) == 0;
2091
2092 case TImode: case XFmode:
2093 return (regno & 3) == 0;
2094
2095 default:
2096 return 1;
2097 }
2098 }
2099 else if (regno >= 32 && regno < 36)
2100 {
2101 switch (mode)
2102 {
2103 case SFmode: case DFmode: case XFmode:
2104 case SCmode: case DCmode:
2105 return 1;
2106
2107 default:
2108 return 0;
2109 }
2110 }
2111 else if (regno == 36)
2112 {
2113 switch (mode)
2114 {
2115 case CCmode: case CC_UNSmode: case CC_CHKmode:
2116 return 1;
2117
2118 default:
2119 return 0;
2120 }
2121 }
2122 else if (regno == 37)
2123 return 0;
2124
2125 abort ();
2126 }
2127
2128 \f
2129 /* Return the minimum alignment of an expression rtx X in bytes. This takes
2130 advantage of machine specific facts, such as knowing that the frame pointer
2131 is always 16 byte aligned. */
2132
2133 int
2134 i960_expr_alignment (x, size)
2135 rtx x;
2136 int size;
2137 {
2138 int align = 1;
2139
2140 if (x == 0)
2141 return 1;
2142
2143 switch (GET_CODE(x))
2144 {
2145 case CONST_INT:
2146 align = INTVAL(x);
2147
2148 if ((align & 0xf) == 0)
2149 align = 16;
2150 else if ((align & 0x7) == 0)
2151 align = 8;
2152 else if ((align & 0x3) == 0)
2153 align = 4;
2154 else if ((align & 0x1) == 0)
2155 align = 2;
2156 else
2157 align = 1;
2158 break;
2159
2160 case PLUS:
2161 align = MIN (i960_expr_alignment (XEXP (x, 0), size),
2162 i960_expr_alignment (XEXP (x, 1), size));
2163 break;
2164
2165 case SYMBOL_REF:
2166 /* If this is a valid program, objects are guaranteed to be
2167 correctly aligned for whatever size the reference actually is. */
2168 align = i960_object_bytes_bitalign (size) / BITS_PER_UNIT;
2169 break;
2170
2171 case REG:
2172 if (REGNO (x) == FRAME_POINTER_REGNUM)
2173 align = 16;
2174 break;
2175
2176 case ASHIFT:
2177 align = i960_expr_alignment (XEXP (x, 0));
2178
2179 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2180 {
2181 align = align << INTVAL (XEXP (x, 1));
2182 align = MIN (align, 16);
2183 }
2184 break;
2185
2186 case MULT:
2187 align = (i960_expr_alignment (XEXP (x, 0), size) *
2188 i960_expr_alignment (XEXP (x, 1), size));
2189
2190 align = MIN (align, 16);
2191 break;
2192 }
2193
2194 return align;
2195 }
2196
2197 /* Return true if it is possible to reference both BASE and OFFSET, which
2198 have alignment at least as great as 4 byte, as if they had alignment valid
2199 for an object of size SIZE. */
2200
2201 int
2202 i960_improve_align (base, offset, size)
2203 rtx base;
2204 rtx offset;
2205 int size;
2206 {
2207 int i, j;
2208
2209 /* We have at least a word reference to the object, so we know it has to
2210 be aligned at least to 4 bytes. */
2211
2212 i = MIN (i960_expr_alignment (base, 4),
2213 i960_expr_alignment (offset, 4));
2214
2215 i = MAX (i, 4);
2216
2217 /* We know the size of the request. If strict align is not enabled, we
2218 can guess that the alignment is OK for the requested size. */
2219
2220 if (! TARGET_STRICT_ALIGN)
2221 if ((j = (i960_object_bytes_bitalign (size) / BITS_PER_UNIT)) > i)
2222 i = j;
2223
2224 return (i >= size);
2225 }
2226
2227 /* Return true if it is possible to access BASE and OFFSET, which have 4 byte
2228 (SImode) alignment as if they had 16 byte (TImode) alignment. */
2229
2230 int
2231 i960_si_ti (base, offset)
2232 rtx base;
2233 rtx offset;
2234 {
2235 return i960_improve_align (base, offset, 16);
2236 }
2237
2238 /* Return true if it is possible to access BASE and OFFSET, which have 4 byte
2239 (SImode) alignment as if they had 8 byte (DImode) alignment. */
2240
2241 int
2242 i960_si_di (base, offset)
2243 rtx base;
2244 rtx offset;
2245 {
2246 return i960_improve_align (base, offset, 8);
2247 }
2248 \f
2249 /* Return raw values of size and alignment (in words) for the data
2250 type being accessed. These values will be rounded by the caller. */
2251
2252 static void
2253 i960_arg_size_and_align (mode, type, size_out, align_out)
2254 enum machine_mode mode;
2255 tree type;
2256 int *size_out;
2257 int *align_out;
2258 {
2259 int size, align;
2260
2261 /* Use formal alignment requirements of type being passed, except make
2262 it at least a word. If we don't have a type, this is a library call,
2263 and the parm has to be of scalar type. In this case, consider its
2264 formal alignment requirement to be its size in words. */
2265
2266 if (mode == BLKmode)
2267 size = (int_size_in_bytes (type) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2268 else if (mode == VOIDmode)
2269 {
2270 /* End of parm list. */
2271 if (type == 0 || TYPE_MODE (type) != VOIDmode)
2272 abort ();
2273 size = 1;
2274 }
2275 else
2276 size = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2277
2278 if (type == 0)
2279 {
2280 /* ??? This is a hack to properly correct the alignment of XFmode
2281 values without affecting anything else. */
2282 if (size == 3)
2283 align = 4;
2284 else
2285 align = size;
2286 }
2287 else if (TYPE_ALIGN (type) >= BITS_PER_WORD)
2288 align = TYPE_ALIGN (type) / BITS_PER_WORD;
2289 else
2290 align = 1;
2291
2292 *size_out = size;
2293 *align_out = align;
2294 }
2295
2296 /* On the 80960 the first 12 args are in registers and the rest are pushed.
2297 Any arg that is bigger than 4 words is placed on the stack and all
2298 subsequent arguments are placed on the stack.
2299
2300 Additionally, parameters with an alignment requirement stronger than
2301 a word must be aligned appropriately. Note that this means that a
2302 64 bit object with a 32 bit alignment is not 64 bit aligned and may be
2303 passed in an odd/even register pair. */
2304
2305 /* Update CUM to advance past an argument described by MODE and TYPE. */
2306
2307 void
2308 i960_function_arg_advance (cum, mode, type, named)
2309 CUMULATIVE_ARGS *cum;
2310 enum machine_mode mode;
2311 tree type;
2312 int named;
2313 {
2314 int size, align;
2315
2316 i960_arg_size_and_align (mode, type, &size, &align);
2317
2318 if (size > 4 || cum->ca_nstackparms != 0
2319 || (size + ROUND_PARM (cum->ca_nregparms, align)) > NPARM_REGS
2320 || MUST_PASS_IN_STACK (mode, type))
2321 {
2322 /* Indicate that all the registers are in use, even if all are not,
2323 so va_start will compute the right value. */
2324 cum->ca_nregparms = NPARM_REGS;
2325 cum->ca_nstackparms = ROUND_PARM (cum->ca_nstackparms, align) + size;
2326 }
2327 else
2328 cum->ca_nregparms = ROUND_PARM (cum->ca_nregparms, align) + size;
2329 }
2330
2331 /* Return the register that the argument described by MODE and TYPE is
2332 passed in, or else return 0 if it is passed on the stack. */
2333
2334 rtx
2335 i960_function_arg (cum, mode, type, named)
2336 CUMULATIVE_ARGS *cum;
2337 enum machine_mode mode;
2338 tree type;
2339 int named;
2340 {
2341 rtx ret;
2342 int size, align;
2343
2344 i960_arg_size_and_align (mode, type, &size, &align);
2345
2346 if (size > 4 || cum->ca_nstackparms != 0
2347 || (size + ROUND_PARM (cum->ca_nregparms, align)) > NPARM_REGS
2348 || MUST_PASS_IN_STACK (mode, type))
2349 {
2350 cum->ca_nstackparms = ROUND_PARM (cum->ca_nstackparms, align);
2351 ret = 0;
2352 }
2353 else
2354 {
2355 cum->ca_nregparms = ROUND_PARM (cum->ca_nregparms, align);
2356 ret = gen_rtx (REG, mode, cum->ca_nregparms);
2357 }
2358
2359 return ret;
2360 }
2361 \f
2362 /* Floating-point support. */
2363
2364 void
2365 i960_output_long_double (file, value)
2366 FILE *file;
2367 REAL_VALUE_TYPE value;
2368 {
2369 long value_long[3];
2370 char dstr[30];
2371
2372 REAL_VALUE_TO_TARGET_LONG_DOUBLE (value, value_long);
2373 REAL_VALUE_TO_DECIMAL (value, "%.20g", dstr);
2374
2375 fprintf (file,
2376 "\t.word\t0x%08lx\t\t# %s\n\t.word\t0x%08lx\n\t.word\t0x%08lx\n",
2377 value_long[0], dstr, value_long[1], value_long[2]);
2378 fprintf (file, "\t.word\t0x0\n");
2379 }
2380
2381 void
2382 i960_output_double (file, value)
2383 FILE *file;
2384 REAL_VALUE_TYPE value;
2385 {
2386 long value_long[2];
2387 char dstr[30];
2388
2389 REAL_VALUE_TO_TARGET_DOUBLE (value, value_long);
2390 REAL_VALUE_TO_DECIMAL (value, "%.20g", dstr);
2391
2392 fprintf (file, "\t.word\t0x%08lx\t\t# %s\n\t.word\t0x%08lx\n",
2393 value_long[0], dstr, value_long[1]);
2394 }
2395
2396 void
2397 i960_output_float (file, value)
2398 FILE *file;
2399 REAL_VALUE_TYPE value;
2400 {
2401 long value_long;
2402 char dstr[30];
2403
2404 REAL_VALUE_TO_TARGET_SINGLE (value, value_long);
2405 REAL_VALUE_TO_DECIMAL (value, "%.12g", dstr);
2406
2407 fprintf (file, "\t.word\t0x%08lx\t\t# %s (float)\n", value_long, dstr);
2408 }
2409 \f
2410 /* Return the number of bits that an object of size N bytes is aligned to. */
2411
2412 int
2413 i960_object_bytes_bitalign (n)
2414 int n;
2415 {
2416 if (n > 8) n = 128;
2417 else if (n > 4) n = 64;
2418 else if (n > 2) n = 32;
2419 else if (n > 1) n = 16;
2420 else n = 8;
2421
2422 return n;
2423 }
2424
2425 /* Compute the alignment for an aggregate type TSIZE.
2426 Alignment is MAX (greatest member alignment,
2427 MIN (pragma align, structure size alignment)). */
2428
2429 int
2430 i960_round_align (align, tsize)
2431 int align;
2432 tree tsize;
2433 {
2434 int new_align;
2435
2436 if (TREE_CODE (tsize) != INTEGER_CST)
2437 return align;
2438
2439 new_align = i960_object_bytes_bitalign (TREE_INT_CST_LOW (tsize)
2440 / BITS_PER_UNIT);
2441 /* Handle #pragma align. */
2442 if (new_align > i960_maxbitalignment)
2443 new_align = i960_maxbitalignment;
2444
2445 if (align < new_align)
2446 align = new_align;
2447
2448 return align;
2449 }
2450 \f
2451 /* Do any needed setup for a varargs function. For the i960, we must
2452 create a register parameter block if one doesn't exist, and then copy
2453 all register parameters to memory. */
2454
2455 void
2456 i960_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2457 CUMULATIVE_ARGS *cum;
2458 enum machine_mode mode;
2459 tree type;
2460 int *pretend_size;
2461 int no_rtl;
2462 {
2463 /* Note: for a varargs fn with only a va_alist argument, this is 0. */
2464 int first_reg = cum->ca_nregparms;
2465
2466 /* Copy only unnamed register arguments to memory. If there are
2467 any stack parms, there are no unnamed arguments in registers, and
2468 an argument block was already allocated by the caller.
2469 Remember that any arg bigger than 4 words is passed on the stack as
2470 are all subsequent args.
2471
2472 If there are no stack arguments but there are exactly NPARM_REGS
2473 registers, either there were no extra arguments or the caller
2474 allocated an argument block. */
2475
2476 if (cum->ca_nstackparms == 0 && first_reg < NPARM_REGS && !no_rtl)
2477 {
2478 rtx label = gen_label_rtx ();
2479 rtx regblock;
2480
2481 /* If arg_pointer_rtx == 0, no arguments were passed on the stack
2482 and we need to allocate a chunk to save the registers (if any
2483 arguments were passed on the stack the caller would allocate the
2484 48 bytes as well). We must allocate all 48 bytes (12*4) because
2485 va_start assumes it. */
2486 emit_insn (gen_cmpsi (arg_pointer_rtx, const0_rtx));
2487 emit_jump_insn (gen_bne (label));
2488 emit_insn (gen_rtx (SET, VOIDmode, arg_pointer_rtx,
2489 stack_pointer_rtx));
2490 emit_insn (gen_rtx (SET, VOIDmode, stack_pointer_rtx,
2491 memory_address (SImode,
2492 plus_constant (stack_pointer_rtx,
2493 48))));
2494 emit_label (label);
2495
2496 /* ??? Note that we unnecessarily store one extra register for stdarg
2497 fns. We could optimize this, but it's kept as for now. */
2498 regblock = gen_rtx (MEM, BLKmode,
2499 plus_constant (arg_pointer_rtx,
2500 first_reg * 4));
2501 move_block_from_reg (first_reg, regblock,
2502 NPARM_REGS - first_reg,
2503 (NPARM_REGS - first_reg) * UNITS_PER_WORD);
2504 }
2505 }
2506
2507 /* Calculate the final size of the reg parm stack space for the current
2508 function, based on how many bytes would be allocated on the stack. */
2509
2510 int
2511 i960_final_reg_parm_stack_space (const_size, var_size)
2512 int const_size;
2513 tree var_size;
2514 {
2515 if (var_size || const_size > 48)
2516 return 48;
2517 else
2518 return 0;
2519 }
2520
2521 /* Calculate the size of the reg parm stack space. This is a bit complicated
2522 on the i960. */
2523
2524 int
2525 i960_reg_parm_stack_space (fndecl)
2526 tree fndecl;
2527 {
2528 /* In this case, we are called from emit_library_call, and we don't need
2529 to pretend we have more space for parameters than what's apparent. */
2530 if (fndecl == 0)
2531 return 0;
2532
2533 /* In this case, we are called from locate_and_pad_parms when we're
2534 not IN_REGS, so we have an arg block. */
2535 if (fndecl != current_function_decl)
2536 return 48;
2537
2538 /* Otherwise, we have an arg block if the current function has more than
2539 48 bytes of parameters. */
2540 if (current_function_args_size != 0 || VARARGS_STDARG_FUNCTION (fndecl))
2541 return 48;
2542 else
2543 return 0;
2544 }
2545 \f
2546 /* Return the register class of a scratch register needed to copy IN into
2547 or out of a register in CLASS in MODE. If it can be done directly,
2548 NO_REGS is returned. */
2549
2550 enum reg_class
2551 secondary_reload_class (class, mode, in)
2552 enum reg_class class;
2553 enum machine_mode mode;
2554 rtx in;
2555 {
2556 int regno = -1;
2557
2558 if (GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
2559 regno = true_regnum (in);
2560
2561 /* We can place anything into LOCAL_OR_GLOBAL_REGS and can put
2562 LOCAL_OR_GLOBAL_REGS into anything. */
2563 if (class == LOCAL_OR_GLOBAL_REGS || class == LOCAL_REGS
2564 || class == GLOBAL_REGS || (regno >= 0 && regno < 32))
2565 return NO_REGS;
2566
2567 /* We can place any hard register, 0.0, and 1.0 into FP_REGS. */
2568 if (class == FP_REGS
2569 && ((regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
2570 || in == CONST0_RTX (mode) || in == CONST1_RTX (mode)))
2571 return NO_REGS;
2572
2573 return LOCAL_OR_GLOBAL_REGS;
2574 }
2575 \f
2576 /* Look at the opcode P, and set i96_last_insn_type to indicate which
2577 function unit it executed on. */
2578
2579 /* ??? This would make more sense as an attribute. */
2580
2581 void
2582 i960_scan_opcode (p)
2583 char *p;
2584 {
2585 switch (*p)
2586 {
2587 case 'a':
2588 case 'd':
2589 case 'e':
2590 case 'm':
2591 case 'n':
2592 case 'o':
2593 case 'r':
2594 /* Ret is not actually of type REG, but it won't matter, because no
2595 insn will ever follow it. */
2596 case 'u':
2597 case 'x':
2598 i960_last_insn_type = I_TYPE_REG;
2599 break;
2600
2601 case 'b':
2602 if (p[1] == 'x' || p[3] == 'x')
2603 i960_last_insn_type = I_TYPE_MEM;
2604 i960_last_insn_type = I_TYPE_CTRL;
2605 break;
2606
2607 case 'f':
2608 case 't':
2609 i960_last_insn_type = I_TYPE_CTRL;
2610 break;
2611
2612 case 'c':
2613 if (p[1] == 'a')
2614 {
2615 if (p[4] == 'x')
2616 i960_last_insn_type = I_TYPE_MEM;
2617 else
2618 i960_last_insn_type = I_TYPE_CTRL;
2619 }
2620 else if (p[1] == 'm')
2621 {
2622 if (p[3] == 'd')
2623 i960_last_insn_type = I_TYPE_REG;
2624 else if (p[4] == 'b' || p[4] == 'j')
2625 i960_last_insn_type = I_TYPE_CTRL;
2626 else
2627 i960_last_insn_type = I_TYPE_REG;
2628 }
2629 else
2630 i960_last_insn_type = I_TYPE_REG;
2631 break;
2632
2633 case 'l':
2634 i960_last_insn_type = I_TYPE_MEM;
2635 break;
2636
2637 case 's':
2638 if (p[1] == 't')
2639 i960_last_insn_type = I_TYPE_MEM;
2640 else
2641 i960_last_insn_type = I_TYPE_REG;
2642 break;
2643 }
2644 }