OSDN Git Service

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