OSDN Git Service

2001-07-26 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / i860 / i860.c
1 /* Subroutines for insn-output.c for Intel 860
2    Copyright (C) 1989, 1991, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Derived from sparc.c.
5
6    Written by Richard Stallman (rms@ai.mit.edu).
7
8    Hacked substantially by Ron Guilmette (rfg@netcom.com) to cater
9    to the whims of the System V Release 4 assembler.
10
11 This file is part of GNU CC.
12
13 GNU CC is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 GNU CC is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GNU CC; see the file COPYING.  If not, write to
25 the Free Software Foundation, 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.  */
27
28
29 #include "config.h"
30 #include "system.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "real.h"
37 #include "insn-config.h"
38 #include "conditions.h"
39 #include "output.h"
40 #include "recog.h"
41 #include "insn-attr.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "target-def.h"
47
48 static rtx find_addr_reg PARAMS ((rtx));
49 static int reg_clobbered_p PARAMS ((rtx, rtx));
50 static const char *singlemove_string PARAMS ((rtx *));
51 static const char *load_opcode PARAMS ((enum machine_mode, const char *, rtx));
52 static const char *store_opcode PARAMS ((enum machine_mode, const char *, rtx));
53 static void output_size_for_block_move PARAMS ((rtx, rtx, rtx));
54 static void i860_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
55 static void i860_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
56
57 #ifndef I860_REG_PREFIX
58 #define I860_REG_PREFIX ""
59 #endif
60
61 const char *i860_reg_prefix = I860_REG_PREFIX;
62
63 /* Save information from a "cmpxx" operation until the branch is emitted.  */
64
65 rtx i860_compare_op0, i860_compare_op1;
66 \f
67 /* Initialize the GCC target structure.  */
68 #undef TARGET_ASM_FUNCTION_PROLOGUE
69 #define TARGET_ASM_FUNCTION_PROLOGUE i860_output_function_prologue
70 #undef TARGET_ASM_FUNCTION_EPILOGUE
71 #define TARGET_ASM_FUNCTION_EPILOGUE i860_output_function_epilogue
72
73 struct gcc_target targetm = TARGET_INITIALIZER;
74 \f
75 /* Return non-zero if this pattern, can be evaluated safely, even if it
76    was not asked for.  */
77 int
78 safe_insn_src_p (op, mode)
79      rtx op;
80      enum machine_mode mode;
81 {
82   /* Just experimenting.  */
83
84   /* No floating point src is safe if it contains an arithmetic
85      operation, since that operation may trap.  */
86   switch (GET_CODE (op))
87     {
88     case CONST_INT:
89     case LABEL_REF:
90     case SYMBOL_REF:
91     case CONST:
92       return 1;
93
94     case REG:
95       return 1;
96
97     case MEM:
98       return CONSTANT_ADDRESS_P (XEXP (op, 0));
99
100       /* We never need to negate or complement constants.  */
101     case NEG:
102       return (mode != SFmode && mode != DFmode);
103     case NOT:
104     case ZERO_EXTEND:
105       return 1;
106
107     case EQ:
108     case NE:
109     case LT:
110     case GT:
111     case LE:
112     case GE:
113     case LTU:
114     case GTU:
115     case LEU:
116     case GEU:
117     case MINUS:
118     case PLUS:
119       return (mode != SFmode && mode != DFmode);
120     case AND:
121     case IOR:
122     case XOR:
123     case ASHIFT:
124     case ASHIFTRT:
125     case LSHIFTRT:
126       if ((GET_CODE (XEXP (op, 0)) == CONST_INT && ! SMALL_INT (XEXP (op, 0)))
127           || (GET_CODE (XEXP (op, 1)) == CONST_INT && ! SMALL_INT (XEXP (op, 1))))
128         return 0;
129       return 1;
130
131     default:
132       return 0;
133     }
134 }
135
136 /* Return 1 if REG is clobbered in IN.
137    Return 2 if REG is used in IN. 
138    Return 3 if REG is both used and clobbered in IN.
139    Return 0 if neither.  */
140
141 static int
142 reg_clobbered_p (reg, in)
143      rtx reg;
144      rtx in;
145 {
146   register enum rtx_code code;
147
148   if (in == 0)
149     return 0;
150
151   code = GET_CODE (in);
152
153   if (code == SET || code == CLOBBER)
154     {
155       rtx dest = SET_DEST (in);
156       int set = 0;
157       int used = 0;
158
159       while (GET_CODE (dest) == STRICT_LOW_PART
160              || GET_CODE (dest) == SUBREG
161              || GET_CODE (dest) == SIGN_EXTRACT
162              || GET_CODE (dest) == ZERO_EXTRACT)
163         dest = XEXP (dest, 0);
164
165       if (dest == reg)
166         set = 1;
167       else if (GET_CODE (dest) == REG
168                && refers_to_regno_p (REGNO (reg),
169                                      REGNO (reg) + HARD_REGNO_NREGS (reg, GET_MODE (reg)),
170                                      SET_DEST (in), 0))
171         {
172           set = 1;
173           /* Anything that sets just part of the register
174              is considered using as well as setting it.
175              But note that a straight SUBREG of a single-word value
176              clobbers the entire value.   */
177           if (dest != SET_DEST (in)
178               && ! (GET_CODE (SET_DEST (in)) == SUBREG
179                     || UNITS_PER_WORD >= GET_MODE_SIZE (GET_MODE (dest))))
180             used = 1;
181         }
182
183       if (code == SET)
184         {
185           if (set)
186             used = refers_to_regno_p (REGNO (reg),
187                                       REGNO (reg) + HARD_REGNO_NREGS (reg, GET_MODE (reg)),
188                                       SET_SRC (in), 0);
189           else
190             used = refers_to_regno_p (REGNO (reg),
191                                       REGNO (reg) + HARD_REGNO_NREGS (reg, GET_MODE (reg)),
192                                       in, 0);
193         }
194
195       return set + used * 2;
196     }
197
198   if (refers_to_regno_p (REGNO (reg),
199                          REGNO (reg) + HARD_REGNO_NREGS (reg, GET_MODE (reg)),
200                          in, 0))
201     return 2;
202   return 0;
203 }
204
205 /* Return non-zero if OP can be written to without screwing up
206    GCC's model of what's going on.  It is assumed that this operand
207    appears in the dest position of a SET insn in a conditional
208    branch's delay slot.  AFTER is the label to start looking from.  */
209 int
210 operand_clobbered_before_used_after (op, after)
211      rtx op;
212      rtx after;
213 {
214   /* Just experimenting.  */
215   if (GET_CODE (op) == CC0)
216     return 1;
217   if (GET_CODE (op) == REG)
218     {
219       rtx insn;
220
221       if (op == stack_pointer_rtx)
222         return 0;
223
224       /* Scan forward from the label, to see if the value of OP
225          is clobbered before the first use.  */
226
227       for (insn = NEXT_INSN (after); insn; insn = NEXT_INSN (insn))
228         {
229           if (GET_CODE (insn) == NOTE)
230             continue;
231           if (GET_CODE (insn) == INSN
232               || GET_CODE (insn) == JUMP_INSN
233               || GET_CODE (insn) == CALL_INSN)
234             {
235               switch (reg_clobbered_p (op, PATTERN (insn)))
236                 {
237                 default:
238                   return 0;
239                 case 1:
240                   return 1;
241                 case 0:
242                   break;
243                 }
244             }
245           /* If we reach another label without clobbering OP,
246              then we cannot safely write it here.  */
247           else if (GET_CODE (insn) == CODE_LABEL)
248             return 0;
249           if (GET_CODE (insn) == JUMP_INSN)
250             {
251               if (condjump_p (insn))
252                 return 0;
253               /* This is a jump insn which has already
254                  been mangled.  We can't tell what it does.  */
255               if (GET_CODE (PATTERN (insn)) == PARALLEL)
256                 return 0;
257               if (! JUMP_LABEL (insn))
258                 return 0;
259               /* Keep following jumps.  */
260               insn = JUMP_LABEL (insn);
261             }
262         }
263       return 1;
264     }
265
266   /* In both of these cases, the first insn executed
267      for this op will be a orh whatever%h,%?r0,%?r31,
268      which is tolerable.  */
269   if (GET_CODE (op) == MEM)
270     return (CONSTANT_ADDRESS_P (XEXP (op, 0)));
271
272   return 0;
273 }
274
275 /* Return non-zero if this pattern, as a source to a "SET",
276    is known to yield an instruction of unit size.  */
277 int
278 single_insn_src_p (op, mode)
279      rtx op;
280      enum machine_mode mode;
281 {
282   switch (GET_CODE (op))
283     {
284     case CONST_INT:
285       /* This is not always a single insn src, technically,
286          but output_delayed_branch knows how to deal with it.  */
287       return 1;
288
289     case SYMBOL_REF:
290     case CONST:
291       /* This is not a single insn src, technically,
292          but output_delayed_branch knows how to deal with it.  */
293       return 1;
294
295     case REG:
296       return 1;
297
298     case MEM:
299       return 1;
300
301       /* We never need to negate or complement constants.  */
302     case NEG:
303       return (mode != DFmode);
304     case NOT:
305     case ZERO_EXTEND:
306       return 1;
307
308     case PLUS:
309     case MINUS:
310       /* Detect cases that require multiple instructions.  */
311       if (CONSTANT_P (XEXP (op, 1))
312           && !(GET_CODE (XEXP (op, 1)) == CONST_INT
313                && SMALL_INT (XEXP (op, 1))))
314         return 0;
315     case EQ:
316     case NE:
317     case LT:
318     case GT:
319     case LE:
320     case GE:
321     case LTU:
322     case GTU:
323     case LEU:
324     case GEU:
325       /* Not doing floating point, since they probably
326          take longer than the branch slot they might fill.  */
327       return (mode != SFmode && mode != DFmode);
328
329     case AND:
330       if (GET_CODE (XEXP (op, 1)) == NOT)
331         {
332           rtx arg = XEXP (XEXP (op, 1), 0);
333           if (CONSTANT_P (arg)
334               && !(GET_CODE (arg) == CONST_INT
335                    && (SMALL_INT (arg)
336                        || (INTVAL (arg) & 0xffff) == 0)))
337             return 0;
338         }
339     case IOR:
340     case XOR:
341       /* Both small and round numbers take one instruction;
342          others take two.  */
343       if (CONSTANT_P (XEXP (op, 1))
344           && !(GET_CODE (XEXP (op, 1)) == CONST_INT
345                && (SMALL_INT (XEXP (op, 1))
346                    || (INTVAL (XEXP (op, 1)) & 0xffff) == 0)))
347         return 0;
348
349     case ASHIFT:
350     case ASHIFTRT:
351     case LSHIFTRT:
352       return 1;
353
354     case SUBREG:
355       if (SUBREG_BYTE (op) != 0)
356         return 0;
357       return single_insn_src_p (SUBREG_REG (op), mode);
358
359       /* Not doing floating point, since they probably
360          take longer than the branch slot they might fill.  */
361     case FLOAT_EXTEND:
362     case FLOAT_TRUNCATE:
363     case FLOAT:
364     case FIX:
365     case UNSIGNED_FLOAT:
366     case UNSIGNED_FIX:
367       return 0;
368
369     default:
370       return 0;
371     }
372 }
373 \f
374 /* Return non-zero only if OP is a register of mode MODE,
375    or const0_rtx.  */
376 int
377 reg_or_0_operand (op, mode)
378      rtx op;
379      enum machine_mode mode;
380 {
381   return (op == const0_rtx || register_operand (op, mode)
382           || op == CONST0_RTX (mode));
383 }
384
385 /* Return truth value of whether OP can be used as an operands in a three
386    address add/subtract insn (such as add %o1,7,%l2) of mode MODE.  */
387
388 int
389 arith_operand (op, mode)
390      rtx op;
391      enum machine_mode mode;
392 {
393   return (register_operand (op, mode)
394           || (GET_CODE (op) == CONST_INT && SMALL_INT (op)));
395 }
396
397 /* Return 1 if OP is a valid first operand for a logical insn of mode MODE.  */
398
399 int
400 logic_operand (op, mode)
401      rtx op;
402      enum machine_mode mode;
403 {
404   return (register_operand (op, mode)
405           || (GET_CODE (op) == CONST_INT && LOGIC_INT (op)));
406 }
407
408 /* Return 1 if OP is a valid first operand for a shift insn of mode MODE.  */
409
410 int
411 shift_operand (op, mode)
412      rtx op;
413      enum machine_mode mode;
414 {
415   return (register_operand (op, mode)
416           || (GET_CODE (op) == CONST_INT));
417 }
418
419 /* Return 1 if OP is a valid first operand for either a logical insn
420    or an add insn of mode MODE.  */
421
422 int
423 compare_operand (op, mode)
424      rtx op;
425      enum machine_mode mode;
426 {
427   return (register_operand (op, mode)
428           || (GET_CODE (op) == CONST_INT && SMALL_INT (op) && LOGIC_INT (op)));
429 }
430
431 /* Return truth value of whether OP can be used as the 5-bit immediate
432    operand of a bte or btne insn.  */
433
434 int
435 bte_operand (op, mode)
436      rtx op;
437      enum machine_mode mode;
438 {
439   return (register_operand (op, mode)
440           || (GET_CODE (op) == CONST_INT
441               && (unsigned) INTVAL (op) < 0x20));
442 }
443
444 /* Return 1 if OP is an indexed memory reference of mode MODE.  */
445
446 int
447 indexed_operand (op, mode)
448      rtx op;
449      enum machine_mode mode;
450 {
451   return (GET_CODE (op) == MEM && GET_MODE (op) == mode
452           && GET_CODE (XEXP (op, 0)) == PLUS
453           && GET_MODE (XEXP (op, 0)) == SImode
454           && register_operand (XEXP (XEXP (op, 0), 0), SImode)
455           && register_operand (XEXP (XEXP (op, 0), 1), SImode));
456 }
457
458 /* Return 1 if OP is a suitable source operand for a load insn
459    with mode MODE.  */
460
461 int
462 load_operand (op, mode)
463      rtx op;
464      enum machine_mode mode;
465 {
466   return (memory_operand (op, mode) || indexed_operand (op, mode));
467 }
468
469 /* Return truth value of whether OP is a integer which fits the
470    range constraining immediate operands in add/subtract insns.  */
471
472 int
473 small_int (op, mode)
474      rtx op;
475      enum machine_mode mode ATTRIBUTE_UNUSED;
476 {
477   return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
478 }
479
480 /* Return truth value of whether OP is a integer which fits the
481    range constraining immediate operands in logic insns.  */
482
483 int
484 logic_int (op, mode)
485      rtx op;
486      enum machine_mode mode ATTRIBUTE_UNUSED;
487 {
488   return (GET_CODE (op) == CONST_INT && LOGIC_INT (op));
489 }
490
491 /* Test for a valid operand for a call instruction.
492    Don't allow the arg pointer register or virtual regs
493    since they may change into reg + const, which the patterns
494    can't handle yet.  */
495
496 int
497 call_insn_operand (op, mode)
498      rtx op;
499      enum machine_mode mode ATTRIBUTE_UNUSED;
500 {
501   if (GET_CODE (op) == MEM
502       && (CONSTANT_ADDRESS_P (XEXP (op, 0))
503           || (GET_CODE (XEXP (op, 0)) == REG
504               && XEXP (op, 0) != arg_pointer_rtx
505               && !(REGNO (XEXP (op, 0)) >= FIRST_PSEUDO_REGISTER
506                    && REGNO (XEXP (op, 0)) <= LAST_VIRTUAL_REGISTER))))
507     return 1;
508   return 0;
509 }
510 \f
511 /* Return the best assembler insn template
512    for moving operands[1] into operands[0] as a fullword.  */
513
514 static const char *
515 singlemove_string (operands)
516      rtx *operands;
517 {
518   if (GET_CODE (operands[0]) == MEM)
519     {
520       if (GET_CODE (operands[1]) != MEM)
521         if (CONSTANT_ADDRESS_P (XEXP (operands[0], 0)))
522           {
523             if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
524                    && (cc_prev_status.flags & CC_HI_R31_ADJ)
525                    && cc_prev_status.mdep == XEXP (operands[0], 0)))
526               {
527                 CC_STATUS_INIT;
528                 output_asm_insn ("orh %h0,%?r0,%?r31", operands);
529               }
530             cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
531             cc_status.mdep = XEXP (operands[0], 0);
532             return "st.l %r1,%L0(%?r31)";
533           }
534         else
535           return "st.l %r1,%0";
536       else
537         abort ();
538 #if 0
539         {
540           rtx xoperands[2];
541
542           cc_status.flags &= ~CC_F0_IS_0;
543           xoperands[0] = gen_rtx_REG (SFmode, 32);
544           xoperands[1] = operands[1];
545           output_asm_insn (singlemove_string (xoperands), xoperands);
546           xoperands[1] = xoperands[0];
547           xoperands[0] = operands[0];
548           output_asm_insn (singlemove_string (xoperands), xoperands);
549           return "";
550         }
551 #endif
552     }
553   if (GET_CODE (operands[1]) == MEM)
554     {
555       if (CONSTANT_ADDRESS_P (XEXP (operands[1], 0)))
556         {
557           if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
558                  && (cc_prev_status.flags & CC_HI_R31_ADJ)
559                  && cc_prev_status.mdep == XEXP (operands[1], 0)))
560             {
561               CC_STATUS_INIT;
562               output_asm_insn ("orh %h1,%?r0,%?r31", operands);
563             }
564           cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
565           cc_status.mdep = XEXP (operands[1], 0);
566           return "ld.l %L1(%?r31),%0";
567         }
568       return "ld.l %m1,%0";
569     }
570  if (GET_CODE (operands[1]) == CONST_INT)
571    {
572      if (operands[1] == const0_rtx)
573       return "mov %?r0,%0";
574      if((INTVAL (operands[1]) & 0xffff0000) == 0)
575       return "or %L1,%?r0,%0";
576      if((INTVAL (operands[1]) & 0xffff8000) == 0xffff8000)
577       return "adds %1,%?r0,%0";
578      if((INTVAL (operands[1]) & 0x0000ffff) == 0)
579       return "orh %H1,%?r0,%0";
580
581      return "orh %H1,%?r0,%0\n\tor %L1,%0,%0";
582    }
583   return "mov %1,%0";
584 }
585 \f
586 /* Output assembler code to perform a doubleword move insn
587    with operands OPERANDS.  */
588
589 const char *
590 output_move_double (operands)
591      rtx *operands;
592 {
593   enum { REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP } optype0, optype1;
594   rtx latehalf[2];
595   rtx addreg0 = 0, addreg1 = 0;
596   int highest_first = 0;
597   int no_addreg1_decrement = 0;
598
599   /* First classify both operands.  */
600
601   if (REG_P (operands[0]))
602     optype0 = REGOP;
603   else if (offsettable_memref_p (operands[0]))
604     optype0 = OFFSOP;
605   else if (GET_CODE (operands[0]) == MEM)
606     optype0 = MEMOP;
607   else
608     optype0 = RNDOP;
609
610   if (REG_P (operands[1]))
611     optype1 = REGOP;
612   else if (CONSTANT_P (operands[1]))
613     optype1 = CNSTOP;
614   else if (offsettable_memref_p (operands[1]))
615     optype1 = OFFSOP;
616   else if (GET_CODE (operands[1]) == MEM)
617     optype1 = MEMOP;
618   else
619     optype1 = RNDOP;
620
621   /* Check for the cases that the operand constraints are not
622      supposed to allow to happen.  Abort if we get one,
623      because generating code for these cases is painful.  */
624
625   if (optype0 == RNDOP || optype1 == RNDOP)
626     abort ();
627
628   /* If an operand is an unoffsettable memory ref, find a register
629      we can increment temporarily to make it refer to the second word.  */
630
631   if (optype0 == MEMOP)
632     addreg0 = find_addr_reg (XEXP (operands[0], 0));
633
634   if (optype1 == MEMOP)
635     addreg1 = find_addr_reg (XEXP (operands[1], 0));
636
637 /* ??? Perhaps in some cases move double words
638    if there is a spare pair of floating regs.  */
639
640   /* Ok, we can do one word at a time.
641      Normally we do the low-numbered word first,
642      but if either operand is autodecrementing then we
643      do the high-numbered word first.
644
645      In either case, set up in LATEHALF the operands to use
646      for the high-numbered word and in some cases alter the
647      operands in OPERANDS to be suitable for the low-numbered word.  */
648
649   if (optype0 == REGOP)
650     latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
651   else if (optype0 == OFFSOP)
652     latehalf[0] = adjust_address (operands[0], SImode, 4);
653   else
654     latehalf[0] = operands[0];
655
656   if (optype1 == REGOP)
657     latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
658   else if (optype1 == OFFSOP)
659     latehalf[1] = adjust_address (operands[1], SImode, 4);
660   else if (optype1 == CNSTOP)
661     {
662       if (GET_CODE (operands[1]) == CONST_DOUBLE)
663         split_double (operands[1], &operands[1], &latehalf[1]);
664       else if (CONSTANT_P (operands[1]))
665         latehalf[1] = const0_rtx;
666     }
667   else
668     latehalf[1] = operands[1];
669
670   /* If the first move would clobber the source of the second one,
671      do them in the other order.
672
673      RMS says "This happens only for registers;
674      such overlap can't happen in memory unless the user explicitly
675      sets it up, and that is an undefined circumstance."
676
677      but it happens on the sparc when loading parameter registers,
678      so I am going to define that circumstance, and make it work
679      as expected.  */
680
681   if (optype0 == REGOP && optype1 == REGOP
682       && REGNO (operands[0]) == REGNO (latehalf[1]))
683     {
684       CC_STATUS_PARTIAL_INIT;
685       /* Make any unoffsettable addresses point at high-numbered word.  */
686       if (addreg0)
687         output_asm_insn ("adds 0x4,%0,%0", &addreg0);
688       if (addreg1)
689         output_asm_insn ("adds 0x4,%0,%0", &addreg1);
690
691       /* Do that word.  */
692       output_asm_insn (singlemove_string (latehalf), latehalf);
693
694       /* Undo the adds we just did.  */
695       if (addreg0)
696         output_asm_insn ("adds -0x4,%0,%0", &addreg0);
697       if (addreg1)
698         output_asm_insn ("adds -0x4,%0,%0", &addreg1);
699
700       /* Do low-numbered word.  */
701       return singlemove_string (operands);
702     }
703   else if (optype0 == REGOP && optype1 != REGOP
704            && reg_overlap_mentioned_p (operands[0], operands[1]))
705     {
706       /* If both halves of dest are used in the src memory address,
707          add the two regs and put them in the low reg (operands[0]).
708          Then it works to load latehalf first.  */
709       if (reg_mentioned_p (operands[0], XEXP (operands[1], 0))
710           && reg_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
711         {
712           rtx xops[2];
713           xops[0] = latehalf[0];
714           xops[1] = operands[0];
715           output_asm_insn ("adds %1,%0,%1", xops);
716           operands[1] = gen_rtx_MEM (DImode, operands[0]);
717           latehalf[1] = adjust_address (operands[1], SImode, 4);
718           addreg1 = 0;
719           highest_first = 1;
720         }
721       /* Only one register in the dest is used in the src memory address,
722          and this is the first register of the dest, so we want to do
723          the late half first here also.  */
724       else if (! reg_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
725         highest_first = 1;
726       /* Only one register in the dest is used in the src memory address,
727          and this is the second register of the dest, so we want to do
728          the late half last.  If addreg1 is set, and addreg1 is the same
729          register as latehalf, then we must suppress the trailing decrement,
730          because it would clobber the value just loaded.  */
731       else if (addreg1 && reg_mentioned_p (addreg1, latehalf[0]))
732         no_addreg1_decrement = 1;
733     }
734
735   /* Normal case: do the two words, low-numbered first.
736      Overlap case (highest_first set): do high-numbered word first.  */
737
738   if (! highest_first)
739     output_asm_insn (singlemove_string (operands), operands);
740
741   CC_STATUS_PARTIAL_INIT;
742   /* Make any unoffsettable addresses point at high-numbered word.  */
743   if (addreg0)
744     output_asm_insn ("adds 0x4,%0,%0", &addreg0);
745   if (addreg1)
746     output_asm_insn ("adds 0x4,%0,%0", &addreg1);
747
748   /* Do that word.  */
749   output_asm_insn (singlemove_string (latehalf), latehalf);
750
751   /* Undo the adds we just did.  */
752   if (addreg0)
753     output_asm_insn ("adds -0x4,%0,%0", &addreg0);
754   if (addreg1 && !no_addreg1_decrement)
755     output_asm_insn ("adds -0x4,%0,%0", &addreg1);
756
757   if (highest_first)
758     output_asm_insn (singlemove_string (operands), operands);
759
760   return "";
761 }
762 \f
763 const char *
764 output_fp_move_double (operands)
765      rtx *operands;
766 {
767   /* If the source operand is any sort of zero, use f0 instead.  */
768
769   if (operands[1] == CONST0_RTX (GET_MODE (operands[1])))
770     operands[1] = gen_rtx_REG (DFmode, F0_REGNUM);
771
772   if (FP_REG_P (operands[0]))
773     {
774       if (FP_REG_P (operands[1]))
775         return "fmov.dd %1,%0";
776       if (GET_CODE (operands[1]) == REG)
777         {
778           output_asm_insn ("ixfr %1,%0", operands);
779           operands[0] = gen_rtx_REG (VOIDmode, REGNO (operands[0]) + 1);
780           operands[1] = gen_rtx_REG (VOIDmode, REGNO (operands[1]) + 1);
781           return "ixfr %1,%0";
782         }
783       if (operands[1] == CONST0_RTX (DFmode))
784         return "fmov.dd f0,%0";
785       if (CONSTANT_ADDRESS_P (XEXP (operands[1], 0)))
786         {
787           if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
788                  && (cc_prev_status.flags & CC_HI_R31_ADJ)
789                  && cc_prev_status.mdep == XEXP (operands[1], 0)))
790             {
791               CC_STATUS_INIT;
792               output_asm_insn ("orh %h1,%?r0,%?r31", operands);
793             }
794           cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
795           cc_status.mdep = XEXP (operands[1], 0);
796           return "fld.d %L1(%?r31),%0";
797         }
798       return "fld.d %1,%0";
799     }
800   else if (FP_REG_P (operands[1]))
801     {
802       if (GET_CODE (operands[0]) == REG)
803         {
804           output_asm_insn ("fxfr %1,%0", operands);
805           operands[0] = gen_rtx_REG (VOIDmode, REGNO (operands[0]) + 1);
806           operands[1] = gen_rtx_REG (VOIDmode, REGNO (operands[1]) + 1);
807           return "fxfr %1,%0";
808         }
809       if (CONSTANT_ADDRESS_P (XEXP (operands[0], 0)))
810         {
811           if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
812                  && (cc_prev_status.flags & CC_HI_R31_ADJ)
813                  && cc_prev_status.mdep == XEXP (operands[0], 0)))
814             {
815               CC_STATUS_INIT;
816               output_asm_insn ("orh %h0,%?r0,%?r31", operands);
817             }
818           cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
819           cc_status.mdep = XEXP (operands[0], 0);
820           return "fst.d %1,%L0(%?r31)";
821         }
822       return "fst.d %1,%0";
823     }
824   else
825     abort ();
826   /* NOTREACHED */
827   return NULL;
828 }
829 \f
830 /* Return a REG that occurs in ADDR with coefficient 1.
831    ADDR can be effectively incremented by incrementing REG.  */
832
833 static rtx
834 find_addr_reg (addr)
835      rtx addr;
836 {
837   while (GET_CODE (addr) == PLUS)
838     {
839       if (GET_CODE (XEXP (addr, 0)) == REG)
840         addr = XEXP (addr, 0);
841       else if (GET_CODE (XEXP (addr, 1)) == REG)
842         addr = XEXP (addr, 1);
843       else if (CONSTANT_P (XEXP (addr, 0)))
844         addr = XEXP (addr, 1);
845       else if (CONSTANT_P (XEXP (addr, 1)))
846         addr = XEXP (addr, 0);
847       else
848         abort ();
849     }
850   if (GET_CODE (addr) == REG)
851     return addr;
852   abort ();
853   /* NOTREACHED */
854   return NULL;
855 }
856
857 /* Return a template for a load instruction with mode MODE and
858    arguments from the string ARGS.
859
860    This string is in static storage.   */
861
862 static const char *
863 load_opcode (mode, args, reg)
864      enum machine_mode mode;
865      const char *args;
866      rtx reg;
867 {
868   static char buf[30];
869   const char *opcode;
870
871   switch (mode)
872     {
873     case QImode:
874       opcode = "ld.b";
875       break;
876
877     case HImode:
878       opcode = "ld.s";
879       break;
880
881     case SImode:
882     case SFmode:
883       if (FP_REG_P (reg))
884         opcode = "fld.l";
885       else
886         opcode = "ld.l";
887       break;
888
889     case DImode:
890       if (!FP_REG_P (reg))
891         abort ();
892     case DFmode:
893       opcode = "fld.d";
894       break;
895
896     default:
897       abort ();
898     }
899
900   sprintf (buf, "%s %s", opcode, args);
901   return buf;
902 }
903
904 /* Return a template for a store instruction with mode MODE and
905    arguments from the string ARGS.
906
907    This string is in static storage.   */
908
909 static const char *
910 store_opcode (mode, args, reg)
911      enum machine_mode mode;
912      const char *args;
913      rtx reg;
914 {
915   static char buf[30];
916   const char *opcode;
917
918   switch (mode)
919     {
920     case QImode:
921       opcode = "st.b";
922       break;
923
924     case HImode:
925       opcode = "st.s";
926       break;
927
928     case SImode:
929     case SFmode:
930       if (FP_REG_P (reg))
931         opcode = "fst.l";
932       else
933         opcode = "st.l";
934       break;
935
936     case DImode:
937       if (!FP_REG_P (reg))
938         abort ();
939     case DFmode:
940       opcode = "fst.d";
941       break;
942
943     default:
944       abort ();
945     }
946
947   sprintf (buf, "%s %s", opcode, args);
948   return buf;
949 }
950 \f
951 /* Output a store-in-memory whose operands are OPERANDS[0,1].
952    OPERANDS[0] is a MEM, and OPERANDS[1] is a reg or zero.
953
954    This function returns a template for an insn.
955    This is in static storage.
956
957    It may also output some insns directly.
958    It may alter the values of operands[0] and operands[1].  */
959
960 const char *
961 output_store (operands)
962      rtx *operands;
963 {
964   enum machine_mode mode = GET_MODE (operands[0]);
965   rtx address = XEXP (operands[0], 0);
966
967   cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
968   cc_status.mdep = address;
969
970   if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
971          && (cc_prev_status.flags & CC_HI_R31_ADJ)
972          && address == cc_prev_status.mdep))
973     {
974       CC_STATUS_INIT;
975       output_asm_insn ("orh %h0,%?r0,%?r31", operands);
976       cc_prev_status.mdep = address;
977     }
978
979   /* Store zero in two parts when appropriate.  */
980   if (mode == DFmode && operands[1] == CONST0_RTX (DFmode))
981     return store_opcode (DFmode, "%r1,%L0(%?r31)", operands[1]);
982
983   /* Code below isn't smart enough to move a doubleword in two parts,
984      so use output_move_double to do that in the cases that require it.  */
985   if ((mode == DImode || mode == DFmode)
986       && ! FP_REG_P (operands[1]))
987     return output_move_double (operands);
988
989   return store_opcode (mode, "%r1,%L0(%?r31)", operands[1]);
990 }
991
992 /* Output a load-from-memory whose operands are OPERANDS[0,1].
993    OPERANDS[0] is a reg, and OPERANDS[1] is a mem.
994
995    This function returns a template for an insn.
996    This is in static storage.
997
998    It may also output some insns directly.
999    It may alter the values of operands[0] and operands[1].  */
1000
1001 const char *
1002 output_load (operands)
1003      rtx *operands;
1004 {
1005   enum machine_mode mode = GET_MODE (operands[0]);
1006   rtx address = XEXP (operands[1], 0);
1007
1008   /* We don't bother trying to see if we know %hi(address).
1009      This is because we are doing a load, and if we know the
1010      %hi value, we probably also know that value in memory.  */
1011   cc_status.flags |= CC_KNOW_HI_R31 | CC_HI_R31_ADJ;
1012   cc_status.mdep = address;
1013
1014   if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
1015          && (cc_prev_status.flags & CC_HI_R31_ADJ)
1016          && address == cc_prev_status.mdep
1017          && cc_prev_status.mdep == cc_status.mdep))
1018     {
1019       CC_STATUS_INIT;
1020       output_asm_insn ("orh %h1,%?r0,%?r31", operands);
1021       cc_prev_status.mdep = address;
1022     }
1023
1024   /* Code below isn't smart enough to move a doubleword in two parts,
1025      so use output_move_double to do that in the cases that require it.  */
1026   if ((mode == DImode || mode == DFmode)
1027       && ! FP_REG_P (operands[0]))
1028     return output_move_double (operands);
1029
1030   return load_opcode (mode, "%L1(%?r31),%0", operands[0]);
1031 }
1032 \f
1033 #if 0
1034 /* Load the address specified by OPERANDS[3] into the register
1035    specified by OPERANDS[0].
1036
1037    OPERANDS[3] may be the result of a sum, hence it could either be:
1038
1039    (1) CONST
1040    (2) REG
1041    (2) REG + CONST_INT
1042    (3) REG + REG + CONST_INT
1043    (4) REG + REG  (special case of 3).
1044
1045    Note that (3) is not a legitimate address.
1046    All cases are handled here.  */
1047
1048 void
1049 output_load_address (operands)
1050      rtx *operands;
1051 {
1052   rtx base, offset;
1053
1054   if (CONSTANT_P (operands[3]))
1055     {
1056       output_asm_insn ("mov %3,%0", operands);
1057       return;
1058     }
1059
1060   if (REG_P (operands[3]))
1061     {
1062       if (REGNO (operands[0]) != REGNO (operands[3]))
1063         output_asm_insn ("shl %?r0,%3,%0", operands);
1064       return;
1065     }
1066
1067   if (GET_CODE (operands[3]) != PLUS)
1068     abort ();
1069
1070   base = XEXP (operands[3], 0);
1071   offset = XEXP (operands[3], 1);
1072
1073   if (GET_CODE (base) == CONST_INT)
1074     {
1075       rtx tmp = base;
1076       base = offset;
1077       offset = tmp;
1078     }
1079
1080   if (GET_CODE (offset) != CONST_INT)
1081     {
1082       /* Operand is (PLUS (REG) (REG)).  */
1083       base = operands[3];
1084       offset = const0_rtx;
1085     }
1086
1087   if (REG_P (base))
1088     {
1089       operands[6] = base;
1090       operands[7] = offset;
1091       CC_STATUS_PARTIAL_INIT;
1092       if (SMALL_INT (offset))
1093         output_asm_insn ("adds %7,%6,%0", operands);
1094       else
1095         output_asm_insn ("mov %7,%0\n\tadds %0,%6,%0", operands);
1096     }
1097   else if (GET_CODE (base) == PLUS)
1098     {
1099       operands[6] = XEXP (base, 0);
1100       operands[7] = XEXP (base, 1);
1101       operands[8] = offset;
1102
1103       CC_STATUS_PARTIAL_INIT;
1104       if (SMALL_INT (offset))
1105         output_asm_insn ("adds %6,%7,%0\n\tadds %8,%0,%0", operands);
1106       else
1107         output_asm_insn ("mov %8,%0\n\tadds %0,%6,%0\n\tadds %0,%7,%0", operands);
1108     }
1109   else
1110     abort ();
1111 }
1112 #endif
1113
1114 /* Output code to place a size count SIZE in register REG.
1115    Because block moves are pipelined, we don't include the
1116    first element in the transfer of SIZE to REG.
1117    For this, we subtract ALIGN.  (Actually, I think it is not
1118    right to subtract on this machine, so right now we don't.)  */
1119
1120 static void
1121 output_size_for_block_move (size, reg, align)
1122      rtx size, reg, align;
1123 {
1124   rtx xoperands[3];
1125
1126   xoperands[0] = reg;
1127   xoperands[1] = size;
1128   xoperands[2] = align;
1129
1130 #if 1
1131   cc_status.flags &= ~ CC_KNOW_HI_R31;
1132   output_asm_insn (singlemove_string (xoperands), xoperands);
1133 #else
1134   if (GET_CODE (size) == REG)
1135     output_asm_insn ("sub %2,%1,%0", xoperands);
1136   else
1137     {
1138       xoperands[1] = GEN_INT (INTVAL (size) - INTVAL (align));
1139       cc_status.flags &= ~ CC_KNOW_HI_R31;
1140       output_asm_insn ("mov %1,%0", xoperands);
1141     }
1142 #endif
1143 }
1144
1145 /* Emit code to perform a block move.
1146
1147    OPERANDS[0] is the destination.
1148    OPERANDS[1] is the source.
1149    OPERANDS[2] is the size.
1150    OPERANDS[3] is the known safe alignment.
1151    OPERANDS[4..6] are pseudos we can safely clobber as temps.  */
1152
1153 const char *
1154 output_block_move (operands)
1155      rtx *operands;
1156 {
1157   /* A vector for our computed operands.  Note that load_output_address
1158      makes use of (and can clobber) up to the 8th element of this vector.  */
1159   rtx xoperands[10];
1160 #if 0
1161   rtx zoperands[10];
1162 #endif
1163   static int movstrsi_label = 0;
1164   int i;
1165   rtx temp1 = operands[4];
1166   rtx alignrtx = operands[3];
1167   int align = INTVAL (alignrtx);
1168   int chunk_size;
1169
1170   xoperands[0] = operands[0];
1171   xoperands[1] = operands[1];
1172   xoperands[2] = temp1;
1173
1174   /* We can't move more than four bytes at a time
1175      because we have only one register to move them through.  */
1176   if (align > 4)
1177     {
1178       align = 4;
1179       alignrtx = GEN_INT (4);
1180     }
1181
1182   /* Recognize special cases of block moves.  These occur
1183      when GNU C++ is forced to treat something as BLKmode
1184      to keep it in memory, when its mode could be represented
1185      with something smaller.
1186
1187      We cannot do this for global variables, since we don't know
1188      what pages they don't cross.  Sigh.  */
1189   if (GET_CODE (operands[2]) == CONST_INT
1190       && ! CONSTANT_ADDRESS_P (operands[0])
1191       && ! CONSTANT_ADDRESS_P (operands[1]))
1192     {
1193       int size = INTVAL (operands[2]);
1194       rtx op0 = xoperands[0];
1195       rtx op1 = xoperands[1];
1196
1197       if ((align & 3) == 0 && (size & 3) == 0 && (size >> 2) <= 16)
1198         {
1199           if (memory_address_p (SImode, plus_constant (op0, size))
1200               && memory_address_p (SImode, plus_constant (op1, size)))
1201             {
1202               cc_status.flags &= ~CC_KNOW_HI_R31;
1203               for (i = (size>>2)-1; i >= 0; i--)
1204                 {
1205                   xoperands[0] = plus_constant (op0, i * 4);
1206                   xoperands[1] = plus_constant (op1, i * 4);
1207                   output_asm_insn ("ld.l %a1,%?r31\n\tst.l %?r31,%a0",
1208                                    xoperands);
1209                 }
1210               return "";
1211             }
1212         }
1213       else if ((align & 1) == 0 && (size & 1) == 0 && (size >> 1) <= 16)
1214         {
1215           if (memory_address_p (HImode, plus_constant (op0, size))
1216               && memory_address_p (HImode, plus_constant (op1, size)))
1217             {
1218               cc_status.flags &= ~CC_KNOW_HI_R31;
1219               for (i = (size>>1)-1; i >= 0; i--)
1220                 {
1221                   xoperands[0] = plus_constant (op0, i * 2);
1222                   xoperands[1] = plus_constant (op1, i * 2);
1223                   output_asm_insn ("ld.s %a1,%?r31\n\tst.s %?r31,%a0",
1224                                    xoperands);
1225                 }
1226               return "";
1227             }
1228         }
1229       else if (size <= 16)
1230         {
1231           if (memory_address_p (QImode, plus_constant (op0, size))
1232               && memory_address_p (QImode, plus_constant (op1, size)))
1233             {
1234               cc_status.flags &= ~CC_KNOW_HI_R31;
1235               for (i = size-1; i >= 0; i--)
1236                 {
1237                   xoperands[0] = plus_constant (op0, i);
1238                   xoperands[1] = plus_constant (op1, i);
1239                   output_asm_insn ("ld.b %a1,%?r31\n\tst.b %?r31,%a0",
1240                                    xoperands);
1241                 }
1242               return "";
1243             }
1244         }
1245     }
1246
1247   /* Since we clobber untold things, nix the condition codes.  */
1248   CC_STATUS_INIT;
1249
1250   /* This is the size of the transfer.
1251      Either use the register which already contains the size,
1252      or use a free register (used by no operands).  */
1253   output_size_for_block_move (operands[2], operands[4], alignrtx);
1254
1255 #if 0
1256   /* Also emit code to decrement the size value by ALIGN.  */
1257   zoperands[0] = operands[0];
1258   zoperands[3] = plus_constant (operands[0], align);
1259   output_load_address (zoperands);
1260 #endif
1261
1262   /* Generate number for unique label.  */
1263
1264   xoperands[3] = GEN_INT (movstrsi_label++);
1265
1266   /* Calculate the size of the chunks we will be trying to move first.  */
1267
1268 #if 0
1269   if ((align & 3) == 0)
1270     chunk_size = 4;
1271   else if ((align & 1) == 0)
1272     chunk_size = 2;
1273   else
1274 #endif
1275     chunk_size = 1;
1276
1277   /* Copy the increment (negative) to a register for bla insn.  */
1278
1279   xoperands[4] = GEN_INT (- chunk_size);
1280   xoperands[5] = operands[5];
1281   output_asm_insn ("adds %4,%?r0,%5", xoperands);
1282
1283   /* Predecrement the loop counter.  This happens again also in the `bla'
1284      instruction which precedes the loop, but we need to have it done
1285      two times before we enter the loop because of the bizarre semantics
1286      of the bla instruction.  */
1287
1288   output_asm_insn ("adds %5,%2,%2", xoperands);
1289
1290   /* Check for the case where the original count was less than or equal to
1291      zero.  Avoid going through the loop at all if the original count was
1292      indeed less than or equal to zero.  Note that we treat the count as
1293      if it were a signed 32-bit quantity here, rather than an unsigned one,
1294      even though we really shouldn't.  We have to do this because of the
1295      semantics of the `ble' instruction, which assume that the count is
1296      a signed 32-bit value.  Anyway, in practice it won't matter because
1297      nobody is going to try to do a memcpy() of more than half of the
1298      entire address space (i.e. 2 gigabytes) anyway.  */
1299
1300   output_asm_insn ("bc .Le%3", xoperands);
1301
1302   /* Make available a register which is a temporary.  */
1303
1304   xoperands[6] = operands[6];
1305
1306   /* Now the actual loop.
1307      In xoperands, elements 1 and 0 are the input and output vectors.
1308      Element 2 is the loop index.  Element 5 is the increment.  */
1309
1310   output_asm_insn ("subs %1,%5,%1", xoperands);
1311   output_asm_insn ("bla %5,%2,.Lm%3", xoperands);
1312   output_asm_insn ("adds %0,%2,%6", xoperands);
1313   output_asm_insn ("\n.Lm%3:", xoperands);          /* Label for bla above.  */
1314   output_asm_insn ("\n.Ls%3:",  xoperands);         /* Loop start label. */
1315   output_asm_insn ("adds %5,%6,%6", xoperands);
1316
1317   /* NOTE:  The code here which is supposed to handle the cases where the
1318      sources and destinations are known to start on a 4 or 2 byte boundary
1319      are currently broken.  They fail to do anything about the overflow
1320      bytes which might still need to be copied even after we have copied
1321      some number of words or halfwords.  Thus, for now we use the lowest
1322      common denominator, i.e. the code which just copies some number of
1323      totally unaligned individual bytes.  (See the calculation of
1324      chunk_size above.  */
1325
1326   if (chunk_size == 4)
1327     {
1328       output_asm_insn ("ld.l %2(%1),%?r31", xoperands);
1329       output_asm_insn ("bla %5,%2,.Ls%3", xoperands);
1330       output_asm_insn ("st.l %?r31,8(%6)", xoperands);
1331     }
1332   else if (chunk_size == 2)
1333     {
1334       output_asm_insn ("ld.s %2(%1),%?r31", xoperands);
1335       output_asm_insn ("bla %5,%2,.Ls%3", xoperands);
1336       output_asm_insn ("st.s %?r31,4(%6)", xoperands);
1337     }
1338   else /* chunk_size == 1 */
1339     {
1340       output_asm_insn ("ld.b %2(%1),%?r31", xoperands);
1341       output_asm_insn ("bla %5,%2,.Ls%3", xoperands);
1342       output_asm_insn ("st.b %?r31,2(%6)", xoperands);
1343     }
1344   output_asm_insn ("\n.Le%3:", xoperands);          /* Here if count <= 0.  */
1345
1346   return "";
1347 }
1348 \f
1349 #if 0
1350 /* Output a delayed branch insn with the delay insn in its
1351    branch slot.  The delayed branch insn template is in TEMPLATE,
1352    with operands OPERANDS.  The insn in its delay slot is INSN.
1353
1354    As a special case, since we know that all memory transfers are via
1355    ld/st insns, if we see a (MEM (SYMBOL_REF ...)) we divide the memory
1356    reference around the branch as
1357
1358         orh ha%x,%?r0,%?r31
1359         b ...
1360         ld/st l%x(%?r31),...
1361
1362    As another special case, we handle loading (SYMBOL_REF ...) and
1363    other large constants around branches as well:
1364
1365         orh h%x,%?r0,%0
1366         b ...
1367         or l%x,%0,%1
1368
1369    */
1370 /* ??? Disabled because this re-recognition is incomplete and causes
1371    constrain_operands to segfault.  Anyone who cares should fix up
1372    the code to use the DBR pass.  */
1373
1374 const char *
1375 output_delayed_branch (template, operands, insn)
1376      const char *template;
1377      rtx *operands;
1378      rtx insn;
1379 {
1380   rtx src = XVECEXP (PATTERN (insn), 0, 1);
1381   rtx dest = XVECEXP (PATTERN (insn), 0, 0);
1382
1383   /* See if we are doing some branch together with setting some register
1384      to some 32-bit value which does (or may) have some of the high-order
1385      16 bits set.  If so, we need to set the register in two stages.  One
1386      stage must be done before the branch, and the other one can be done
1387      in the delay slot.  */
1388
1389   if ( (GET_CODE (src) == CONST_INT
1390         && ((unsigned) INTVAL (src) & (unsigned) 0xffff0000) != (unsigned) 0)
1391       || (GET_CODE (src) == SYMBOL_REF)
1392       || (GET_CODE (src) == LABEL_REF)
1393       || (GET_CODE (src) == CONST))
1394     {
1395       rtx xoperands[2];
1396       xoperands[0] = dest;
1397       xoperands[1] = src;
1398
1399       CC_STATUS_PARTIAL_INIT;
1400       /* Output the `orh' insn.  */
1401       output_asm_insn ("orh %H1,%?r0,%0", xoperands);
1402
1403       /* Output the branch instruction next.  */
1404       output_asm_insn (template, operands);
1405
1406       /* Now output the `or' insn.  */
1407       output_asm_insn ("or %L1,%0,%0", xoperands);
1408     }
1409   else if ((GET_CODE (src) == MEM
1410             && CONSTANT_ADDRESS_P (XEXP (src, 0)))
1411            || (GET_CODE (dest) == MEM
1412                && CONSTANT_ADDRESS_P (XEXP (dest, 0))))
1413     {
1414       rtx xoperands[2];
1415       const char *split_template;
1416       xoperands[0] = dest;
1417       xoperands[1] = src;
1418
1419       /* Output the `orh' insn.  */
1420       if (GET_CODE (src) == MEM)
1421         {
1422           if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
1423                  && (cc_prev_status.flags & CC_HI_R31_ADJ)
1424                  && cc_prev_status.mdep == XEXP (operands[1], 0)))
1425             {
1426               CC_STATUS_INIT;
1427               output_asm_insn ("orh %h1,%?r0,%?r31", xoperands);
1428             }
1429           split_template = load_opcode (GET_MODE (dest),
1430                                         "%L1(%?r31),%0", dest);
1431         }
1432       else
1433         {
1434           if (! ((cc_prev_status.flags & CC_KNOW_HI_R31)
1435                  && (cc_prev_status.flags & CC_HI_R31_ADJ)
1436                  && cc_prev_status.mdep == XEXP (operands[0], 0)))
1437             {
1438               CC_STATUS_INIT;
1439               output_asm_insn ("orh %h0,%?r0,%?r31", xoperands);
1440             }
1441           split_template = store_opcode (GET_MODE (dest),
1442                                          "%r1,%L0(%?r31)", src);
1443         }
1444
1445       /* Output the branch instruction next.  */
1446       output_asm_insn (template, operands);
1447
1448       /* Now output the load or store.
1449          No need to do a CC_STATUS_INIT, because we are branching anyway.  */
1450       output_asm_insn (split_template, xoperands);
1451     }
1452   else
1453     {
1454       int insn_code_number;
1455       rtx pat = gen_rtx_SET (VOIDmode, dest, src);
1456       rtx delay_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, pat, -1, 0, 0);
1457       int i;
1458
1459       /* Output the branch instruction first.  */
1460       output_asm_insn (template, operands);
1461
1462       /* Now recognize the insn which we put in its delay slot.
1463          We must do this after outputting the branch insn,
1464          since operands may just be a pointer to `recog_data.operand'.  */
1465       INSN_CODE (delay_insn) = insn_code_number
1466         = recog (pat, delay_insn, NULL);
1467       if (insn_code_number == -1)
1468         abort ();
1469
1470       for (i = 0; i < insn_data[insn_code_number].n_operands; i++)
1471         {
1472           if (GET_CODE (recog_data.operand[i]) == SUBREG)
1473             recog_data.operand[i] = alter_subreg (recog_data.operand[i]);
1474         }
1475
1476       insn_extract (delay_insn);
1477       if (! constrain_operands (1))
1478         fatal_insn_not_found (delay_insn);
1479
1480       template = get_insn_template (insn_code_number, delay_insn);
1481       output_asm_insn (template, recog_data.operand);
1482     }
1483   CC_STATUS_INIT;
1484   return "";
1485 }
1486
1487 /* Output a newly constructed insn DELAY_INSN.  */
1488 const char *
1489 output_delay_insn (delay_insn)
1490      rtx delay_insn;
1491 {
1492   const char *template;
1493   int insn_code_number;
1494   int i;
1495
1496   /* Now recognize the insn which we put in its delay slot.
1497      We must do this after outputting the branch insn,
1498      since operands may just be a pointer to `recog_data.operand'.  */
1499   insn_code_number = recog_memoized (delay_insn);
1500   if (insn_code_number == -1)
1501     abort ();
1502
1503   /* Extract the operands of this delay insn.  */
1504   INSN_CODE (delay_insn) = insn_code_number;
1505   insn_extract (delay_insn);
1506
1507   /* It is possible that this insn has not been properly scanned by final
1508      yet.  If this insn's operands don't appear in the peephole's
1509      actual operands, then they won't be fixed up by final, so we
1510      make sure they get fixed up here.  -- This is a kludge.  */
1511   for (i = 0; i < insn_data[insn_code_number].n_operands; i++)
1512     {
1513       if (GET_CODE (recog_data.operand[i]) == SUBREG)
1514         recog_data.operand[i] = alter_subreg (recog_data.operand[i]);
1515     }
1516
1517   if (! constrain_operands (1))
1518     abort ();
1519
1520   cc_prev_status = cc_status;
1521
1522   /* Update `cc_status' for this instruction.
1523      The instruction's output routine may change it further.
1524      If the output routine for a jump insn needs to depend
1525      on the cc status, it should look at cc_prev_status.  */
1526
1527   NOTICE_UPDATE_CC (PATTERN (delay_insn), delay_insn);
1528
1529   /* Now get the template for what this insn would
1530      have been, without the branch.  */
1531
1532   template = get_insn_template (insn_code_number, delay_insn);
1533   output_asm_insn (template, recog_data.operand);
1534   return "";
1535 }
1536 #endif
1537 \f
1538 /* Special routine to convert an SFmode value represented as a
1539    CONST_DOUBLE into its equivalent unsigned long bit pattern.
1540    We convert the value from a double precision floating-point
1541    value to single precision first, and thence to a bit-wise
1542    equivalent unsigned long value.  This routine is used when
1543    generating an immediate move of an SFmode value directly
1544    into a general register because the svr4 assembler doesn't
1545    grok floating literals in instruction operand contexts.  */
1546
1547 unsigned long
1548 sfmode_constant_to_ulong (x)
1549      rtx x;
1550 {
1551   REAL_VALUE_TYPE d;
1552   union { float f; unsigned long i; } u2;
1553
1554   if (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != SFmode)
1555     abort ();
1556
1557 #if TARGET_FLOAT_FORMAT != HOST_FLOAT_FORMAT
1558  error IEEE emulation needed
1559 #endif
1560   REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1561   u2.f = d;
1562   return u2.i;
1563 }
1564 \f
1565 /* This function generates the assembly code for function entry.
1566
1567    ASM_FILE is a stdio stream to output the code to.
1568    SIZE is an int: how many units of temporary storage to allocate.
1569
1570    Refer to the array `regs_ever_live' to determine which registers
1571    to save; `regs_ever_live[I]' is nonzero if register number I
1572    is ever used in the function.  This macro is responsible for
1573    knowing which registers should not be saved even if used.
1574
1575    NOTE: `frame_lower_bytes' is the count of bytes which will lie
1576    between the new `fp' value and the new `sp' value after the
1577    prologue is done.  `frame_upper_bytes' is the count of bytes
1578    that will lie between the new `fp' and the *old* `sp' value
1579    after the new `fp' is setup (in the prologue).  The upper
1580    part of each frame always includes at least 2 words (8 bytes)
1581    to hold the saved frame pointer and the saved return address.
1582
1583    The svr4 ABI for the i860 now requires that the values of the
1584    stack pointer and frame pointer registers be kept aligned to
1585    16-byte boundaries at all times.  We obey that restriction here.
1586
1587    The svr4 ABI for the i860 is entirely vague when it comes to specifying
1588    exactly where the "preserved" registers should be saved.  The native
1589    svr4 C compiler I now have doesn't help to clarify the requirements
1590    very much because it is plainly out-of-date and non-ABI-compliant
1591    (in at least one important way, i.e. how it generates function
1592    epilogues).
1593
1594    The native svr4 C compiler saves the "preserved" registers (i.e.
1595    r4-r15 and f2-f7) in the lower part of a frame (i.e. at negative
1596    offsets from the frame pointer).
1597
1598    Previous versions of GCC also saved the "preserved" registers in the
1599    "negative" part of the frame, but they saved them using positive
1600    offsets from the (adjusted) stack pointer (after it had been adjusted
1601    to allocate space for the new frame).  That's just plain wrong
1602    because if the current function calls alloca(), the stack pointer
1603    will get moved, and it will be impossible to restore the registers
1604    properly again after that.
1605
1606    Both compilers handled parameter registers (i.e. r16-r27 and f8-f15)
1607    by copying their values either into various "preserved" registers or
1608    into stack slots in the lower part of the current frame (as seemed
1609    appropriate, depending upon subsequent usage of these values).
1610
1611    Here we want to save the preserved registers at some offset from the
1612    frame pointer register so as to avoid any possible problems arising
1613    from calls to alloca().  We can either save them at small positive
1614    offsets from the frame pointer, or at small negative offsets from
1615    the frame pointer.  If we save them at small negative offsets from
1616    the frame pointer (i.e. in the lower part of the frame) then we
1617    must tell the rest of GCC (via STARTING_FRAME_OFFSET) exactly how
1618    many bytes of space we plan to use in the lower part of the frame
1619    for this purpose.  Since other parts of the compiler reference the
1620    value of STARTING_FRAME_OFFSET long before final() calls this function,
1621    we would have to go ahead and assume the worst-case storage requirements
1622    for saving all of the "preserved" registers (and use that number, i.e.
1623    `80', to define STARTING_FRAME_OFFSET) if we wanted to save them in
1624    the lower part of the frame.  That could potentially be very wasteful,
1625    and that wastefulness could really hamper people compiling for embedded
1626    i860 targets with very tight limits on stack space.  Thus, we choose
1627    here to save the preserved registers in the upper part of the
1628    frame, so that we can decide at the very last minute how much (or how
1629    little) space we must allocate for this purpose.
1630
1631    To satisfy the needs of the svr4 ABI "tdesc" scheme, preserved
1632    registers must always be saved so that the saved values of registers
1633    with higher numbers are at higher addresses.  We obey that restriction
1634    here.
1635
1636    There are two somewhat different ways that you can generate prologues
1637    here... i.e. pedantically ABI-compliant, and the "other" way.  The
1638    "other" way is more consistent with what is currently generated by the
1639    "native" svr4 C compiler for the i860.  That's important if you want
1640    to use the current (as of 8/91) incarnation of svr4 SDB for the i860.
1641    The SVR4 SDB for the i860 insists on having function prologues be
1642    non-ABI-compliant!
1643
1644    To get fully ABI-compliant prologues, define I860_STRICT_ABI_PROLOGUES
1645    in the i860svr4.h file.  (By default this is *not* defined).
1646
1647    The differences between the ABI-compliant and non-ABI-compliant prologues
1648    are that (a) the ABI version seems to require the use of *signed*
1649    (rather than unsigned) adds and subtracts, and (b) the ordering of
1650    the various steps (e.g. saving preserved registers, saving the
1651    return address, setting up the new frame pointer value) is different.
1652
1653    For strict ABI compliance, it seems to be the case that the very last
1654    thing that is supposed to happen in the prologue is getting the frame
1655    pointer set to its new value (but only after everything else has
1656    already been properly setup).  We do that here, but only if the symbol
1657    I860_STRICT_ABI_PROLOGUES is defined.
1658 */
1659
1660 #ifndef STACK_ALIGNMENT
1661 #define STACK_ALIGNMENT 16
1662 #endif
1663
1664 extern char call_used_regs[];
1665
1666 char *current_function_original_name;
1667
1668 static int must_preserve_r1;
1669 static unsigned must_preserve_bytes;
1670
1671 static void
1672 i860_output_function_prologue (asm_file, local_bytes)
1673      register FILE *asm_file;
1674      register HOST_WIDE_INT local_bytes;
1675 {
1676   register HOST_WIDE_INT frame_lower_bytes;
1677   register HOST_WIDE_INT frame_upper_bytes;
1678   register HOST_WIDE_INT total_fsize;
1679   register unsigned preserved_reg_bytes = 0;
1680   register unsigned i;
1681   register unsigned preserved_so_far = 0;
1682
1683   must_preserve_r1 = (optimize < 2 || ! leaf_function_p ());
1684   must_preserve_bytes = 4 + (must_preserve_r1 ? 4 : 0);
1685
1686   /* Count registers that need preserving.  Ignore r0.  It never needs
1687      preserving.  */
1688
1689   for (i = 1; i < FIRST_PSEUDO_REGISTER; i++)
1690     {
1691       if (regs_ever_live[i] && ! call_used_regs[i])
1692         preserved_reg_bytes += 4;
1693     }
1694
1695   /* Round-up the frame_lower_bytes so that it's a multiple of 16. */
1696
1697   frame_lower_bytes = (local_bytes + STACK_ALIGNMENT - 1) & -STACK_ALIGNMENT;
1698
1699   /* The upper part of each frame will contain the saved fp,
1700      the saved r1, and stack slots for all of the other "preserved"
1701      registers that we find we will need to save & restore. */
1702
1703   frame_upper_bytes = must_preserve_bytes + preserved_reg_bytes;
1704
1705   /* Round-up the frame_upper_bytes so that it's a multiple of 16. */
1706
1707   frame_upper_bytes
1708     = (frame_upper_bytes + STACK_ALIGNMENT - 1) & -STACK_ALIGNMENT;
1709
1710   total_fsize = frame_upper_bytes + frame_lower_bytes;
1711
1712 #ifndef I860_STRICT_ABI_PROLOGUES
1713
1714   /* There are two kinds of function prologues.
1715      You use the "small" version if the total frame size is
1716      small enough so that it can fit into an immediate 16-bit
1717      value in one instruction.  Otherwise, you use the "large"
1718      version of the function prologue.  */
1719
1720   if (total_fsize > 0x7fff)
1721     {
1722       /* Adjust the stack pointer.  The ABI sez to do this using `adds',
1723          but the native C compiler on svr4 uses `addu'.  */
1724
1725       fprintf (asm_file, "\taddu -%d,%ssp,%ssp\n",
1726         frame_upper_bytes, i860_reg_prefix, i860_reg_prefix);
1727
1728       /* Save the old frame pointer.  */
1729
1730       fprintf (asm_file, "\tst.l %sfp,0(%ssp)\n",
1731         i860_reg_prefix, i860_reg_prefix);
1732
1733       /* Setup the new frame pointer.  The ABI sez to do this after
1734          preserving registers (using adds), but that's not what the
1735          native C compiler on svr4 does.  */
1736
1737       fprintf (asm_file, "\taddu 0,%ssp,%sfp\n",
1738         i860_reg_prefix, i860_reg_prefix);
1739
1740       /* Get the value of frame_lower_bytes into r31.  */
1741
1742       fprintf (asm_file, "\torh %d,%sr0,%sr31\n",
1743         frame_lower_bytes >> 16, i860_reg_prefix, i860_reg_prefix);
1744       fprintf (asm_file, "\tor %d,%sr31,%sr31\n",
1745         frame_lower_bytes & 0xffff, i860_reg_prefix, i860_reg_prefix);
1746
1747       /* Now re-adjust the stack pointer using the value in r31.
1748          The ABI sez to do this with `subs' but SDB may prefer `subu'.  */
1749
1750       fprintf (asm_file, "\tsubu %ssp,%sr31,%ssp\n",
1751         i860_reg_prefix, i860_reg_prefix, i860_reg_prefix);
1752
1753       /* Preserve registers.  The ABI sez to do this before setting
1754          up the new frame pointer, but that's not what the native
1755          C compiler on svr4 does.  */
1756
1757       for (i = 1; i < 32; i++)
1758         if (regs_ever_live[i] && ! call_used_regs[i])
1759           fprintf (asm_file, "\tst.l %s%s,%d(%sfp)\n",
1760             i860_reg_prefix, reg_names[i],
1761             must_preserve_bytes  + (4 * preserved_so_far++),
1762             i860_reg_prefix);
1763
1764       for (i = 32; i < 64; i++)
1765         if (regs_ever_live[i] && ! call_used_regs[i])
1766           fprintf (asm_file, "\tfst.l %s%s,%d(%sfp)\n",
1767             i860_reg_prefix, reg_names[i],
1768             must_preserve_bytes + (4 * preserved_so_far++),
1769             i860_reg_prefix);
1770
1771       /* Save the return address.  */
1772
1773       if (must_preserve_r1)
1774         fprintf (asm_file, "\tst.l %sr1,4(%sfp)\n",
1775           i860_reg_prefix, i860_reg_prefix);
1776     }
1777   else
1778     {
1779       /* Adjust the stack pointer.  The ABI sez to do this using `adds',
1780          but the native C compiler on svr4 uses `addu'.  */
1781
1782       fprintf (asm_file, "\taddu -%d,%ssp,%ssp\n",
1783         total_fsize, i860_reg_prefix, i860_reg_prefix);
1784
1785       /* Save the old frame pointer.  */
1786
1787       fprintf (asm_file, "\tst.l %sfp,%d(%ssp)\n",
1788         i860_reg_prefix, frame_lower_bytes, i860_reg_prefix);
1789
1790       /* Setup the new frame pointer.  The ABI sez to do this after
1791          preserving registers and after saving the return address,
1792         (and its saz to do this using adds), but that's not what the
1793          native C compiler on svr4 does.  */
1794
1795       fprintf (asm_file, "\taddu %d,%ssp,%sfp\n",
1796         frame_lower_bytes, i860_reg_prefix, i860_reg_prefix);
1797
1798       /* Preserve registers.  The ABI sez to do this before setting
1799          up the new frame pointer, but that's not what the native
1800          compiler on svr4 does.  */
1801
1802       for (i = 1; i < 32; i++)
1803         if (regs_ever_live[i] && ! call_used_regs[i])
1804           fprintf (asm_file, "\tst.l %s%s,%d(%sfp)\n",
1805             i860_reg_prefix, reg_names[i],
1806             must_preserve_bytes + (4 * preserved_so_far++),
1807             i860_reg_prefix);
1808
1809       for (i = 32; i < 64; i++)
1810         if (regs_ever_live[i] && ! call_used_regs[i])
1811           fprintf (asm_file, "\tfst.l %s%s,%d(%sfp)\n",
1812             i860_reg_prefix, reg_names[i],
1813             must_preserve_bytes + (4 * preserved_so_far++),
1814             i860_reg_prefix);
1815
1816       /* Save the return address.  The ABI sez to do this earlier,
1817          and also via an offset from %sp, but the native C compiler
1818          on svr4 does it later (i.e. now) and uses an offset from
1819          %fp.  */
1820
1821       if (must_preserve_r1)
1822         fprintf (asm_file, "\tst.l %sr1,4(%sfp)\n",
1823           i860_reg_prefix, i860_reg_prefix);
1824     }
1825
1826 #else /* defined(I860_STRICT_ABI_PROLOGUES) */
1827
1828   /* There are two kinds of function prologues.
1829      You use the "small" version if the total frame size is
1830      small enough so that it can fit into an immediate 16-bit
1831      value in one instruction.  Otherwise, you use the "large"
1832      version of the function prologue.  */
1833
1834   if (total_fsize > 0x7fff)
1835     {
1836       /* Adjust the stack pointer (thereby allocating a new frame).  */
1837
1838       fprintf (asm_file, "\tadds -%d,%ssp,%ssp\n",
1839         frame_upper_bytes, i860_reg_prefix, i860_reg_prefix);
1840
1841       /* Save the caller's frame pointer.  */
1842
1843       fprintf (asm_file, "\tst.l %sfp,0(%ssp)\n",
1844         i860_reg_prefix, i860_reg_prefix);
1845
1846       /* Save return address.  */
1847
1848       if (must_preserve_r1)
1849         fprintf (asm_file, "\tst.l %sr1,4(%ssp)\n",
1850           i860_reg_prefix, i860_reg_prefix);
1851
1852       /* Get the value of frame_lower_bytes into r31 for later use.  */
1853
1854       fprintf (asm_file, "\torh %d,%sr0,%sr31\n",
1855         frame_lower_bytes >> 16, i860_reg_prefix, i860_reg_prefix);
1856       fprintf (asm_file, "\tor %d,%sr31,%sr31\n",
1857         frame_lower_bytes & 0xffff, i860_reg_prefix, i860_reg_prefix);
1858
1859       /* Now re-adjust the stack pointer using the value in r31.  */
1860
1861       fprintf (asm_file, "\tsubs %ssp,%sr31,%ssp\n",
1862         i860_reg_prefix, i860_reg_prefix, i860_reg_prefix);
1863
1864       /* Pre-compute value to be used as the new frame pointer.  */
1865
1866       fprintf (asm_file, "\tadds %ssp,%sr31,%sr31\n",
1867         i860_reg_prefix, i860_reg_prefix, i860_reg_prefix);
1868
1869       /* Preserve registers.  */
1870
1871       for (i = 1; i < 32; i++)
1872         if (regs_ever_live[i] && ! call_used_regs[i])
1873           fprintf (asm_file, "\tst.l %s%s,%d(%sr31)\n",
1874             i860_reg_prefix, reg_names[i],
1875             must_preserve_bytes + (4 * preserved_so_far++),
1876             i860_reg_prefix);
1877
1878       for (i = 32; i < 64; i++)
1879         if (regs_ever_live[i] && ! call_used_regs[i])
1880           fprintf (asm_file, "\tfst.l %s%s,%d(%sr31)\n",
1881             i860_reg_prefix, reg_names[i],
1882             must_preserve_bytes + (4 * preserved_so_far++),
1883             i860_reg_prefix);
1884
1885       /* Actually set the new value of the frame pointer.  */
1886
1887       fprintf (asm_file, "\tmov %sr31,%sfp\n",
1888         i860_reg_prefix, i860_reg_prefix);
1889     }
1890   else
1891     {
1892       /* Adjust the stack pointer.  */
1893
1894       fprintf (asm_file, "\tadds -%d,%ssp,%ssp\n",
1895         total_fsize, i860_reg_prefix, i860_reg_prefix);
1896
1897       /* Save the caller's frame pointer.  */
1898
1899       fprintf (asm_file, "\tst.l %sfp,%d(%ssp)\n",
1900         i860_reg_prefix, frame_lower_bytes, i860_reg_prefix);
1901
1902       /* Save the return address.  */
1903
1904       if (must_preserve_r1)
1905         fprintf (asm_file, "\tst.l %sr1,%d(%ssp)\n",
1906           i860_reg_prefix, frame_lower_bytes + 4, i860_reg_prefix);
1907
1908       /* Preserve registers.  */
1909
1910       for (i = 1; i < 32; i++)
1911         if (regs_ever_live[i] && ! call_used_regs[i])
1912           fprintf (asm_file, "\tst.l %s%s,%d(%ssp)\n",
1913             i860_reg_prefix, reg_names[i],
1914             frame_lower_bytes + must_preserve_bytes + (4 * preserved_so_far++),
1915             i860_reg_prefix);
1916
1917       for (i = 32; i < 64; i++)
1918         if (regs_ever_live[i] && ! call_used_regs[i])
1919           fprintf (asm_file, "\tfst.l %s%s,%d(%ssp)\n",
1920             i860_reg_prefix, reg_names[i],
1921             frame_lower_bytes + must_preserve_bytes + (4 * preserved_so_far++),
1922             i860_reg_prefix);
1923
1924       /* Setup the new frame pointer.  */
1925
1926       fprintf (asm_file, "\tadds %d,%ssp,%sfp\n",
1927         frame_lower_bytes, i860_reg_prefix, i860_reg_prefix);
1928     }
1929 #endif /* defined(I860_STRICT_ABI_PROLOGUES) */
1930
1931 #ifdef ASM_OUTPUT_PROLOGUE_SUFFIX
1932   ASM_OUTPUT_PROLOGUE_SUFFIX (asm_file);
1933 #endif /* defined(ASM_OUTPUT_PROLOGUE_SUFFIX) */
1934 }
1935 \f
1936 /* This function generates the assembly code for function exit.
1937
1938    ASM_FILE is a stdio stream to output the code to.
1939    SIZE is an int: how many units of temporary storage to allocate.
1940
1941    The function epilogue should not depend on the current stack pointer!
1942    It should use the frame pointer only.  This is mandatory because
1943    of alloca; we also take advantage of it to omit stack adjustments
1944    before returning.
1945
1946    Note that when we go to restore the preserved register values we must
1947    not try to address their slots by using offsets from the stack pointer.
1948    That's because the stack pointer may have been moved during the function
1949    execution due to a call to alloca().  Rather, we must restore all
1950    preserved registers via offsets from the frame pointer value.
1951
1952    Note also that when the current frame is being "popped" (by adjusting
1953    the value of the stack pointer) on function exit, we must (for the
1954    sake of alloca) set the new value of the stack pointer based upon
1955    the current value of the frame pointer.  We can't just add what we
1956    believe to be the (static) frame size to the stack pointer because
1957    if we did that, and alloca() had been called during this function,
1958    we would end up returning *without* having fully deallocated all of
1959    the space grabbed by alloca.  If that happened, and a function
1960    containing one or more alloca() calls was called over and over again,
1961    then the stack would grow without limit!
1962
1963    Finally note that the epilogues generated here are completely ABI
1964    compliant.  They go out of their way to insure that the value in
1965    the frame pointer register is never less than the value in the stack
1966    pointer register.  It's not clear why this relationship needs to be
1967    maintained at all times, but maintaining it only costs one extra
1968    instruction, so what the hell.
1969 */
1970
1971 /* This corresponds to a version 4 TDESC structure. Lower numbered
1972    versions successively omit the last word of the structure. We
1973    don't try to handle version 5 here. */
1974
1975 typedef struct TDESC_flags {
1976         int version:4;
1977         int reg_packing:1;
1978         int callable_block:1;
1979         int reserved:4;
1980         int fregs:6;    /* fp regs 2-7 */
1981         int iregs:16;   /* regs 0-15 */
1982 } TDESC_flags;
1983
1984 typedef struct TDESC {
1985         TDESC_flags flags;
1986         int integer_reg_offset;         /* same as must_preserve_bytes */
1987         int floating_point_reg_offset;
1988         unsigned int positive_frame_size;       /* same as frame_upper_bytes */
1989         unsigned int negative_frame_size;       /* same as frame_lower_bytes */
1990 } TDESC;
1991
1992 static void
1993 i860_output_function_epilogue (asm_file, local_bytes)
1994      register FILE *asm_file;
1995      register HOST_WIDE_INT local_bytes;
1996 {
1997   register HOST_WIDE_INT frame_upper_bytes;
1998   register HOST_WIDE_INT frame_lower_bytes;
1999   register HOST_WIDE_INT preserved_reg_bytes = 0;
2000   register unsigned i;
2001   register unsigned restored_so_far = 0;
2002   register unsigned int_restored;
2003   register unsigned mask;
2004   unsigned intflags=0;
2005   register TDESC_flags *flags = (TDESC_flags *) &intflags;
2006
2007   flags->version = 4;
2008   flags->reg_packing = 1;
2009   flags->iregs = 8;     /* old fp always gets saved */
2010
2011   /* Round-up the frame_lower_bytes so that it's a multiple of 16. */
2012
2013   frame_lower_bytes = (local_bytes + STACK_ALIGNMENT - 1) & -STACK_ALIGNMENT;
2014
2015   /* Count the number of registers that were preserved in the prologue.
2016      Ignore r0.  It is never preserved.  */
2017
2018   for (i = 1; i < FIRST_PSEUDO_REGISTER; i++)
2019     {
2020       if (regs_ever_live[i] && ! call_used_regs[i])
2021         preserved_reg_bytes += 4;
2022     }
2023
2024   /* The upper part of each frame will contain only saved fp,
2025      the saved r1, and stack slots for all of the other "preserved"
2026      registers that we find we will need to save & restore. */
2027
2028   frame_upper_bytes = must_preserve_bytes + preserved_reg_bytes;
2029
2030   /* Round-up frame_upper_bytes so that t is a multiple of 16. */
2031
2032   frame_upper_bytes
2033     = (frame_upper_bytes + STACK_ALIGNMENT - 1) & -STACK_ALIGNMENT;
2034
2035   /* Restore all of the "preserved" registers that need restoring.  */
2036
2037   mask = 2;
2038
2039   for (i = 1; i < 32; i++, mask<<=1)
2040     if (regs_ever_live[i] && ! call_used_regs[i]) {
2041       fprintf (asm_file, "\tld.l %d(%sfp),%s%s\n",
2042         must_preserve_bytes + (4 * restored_so_far++),
2043         i860_reg_prefix, i860_reg_prefix, reg_names[i]);
2044       if (i > 3 && i < 16)
2045         flags->iregs |= mask;
2046     }
2047
2048   int_restored = restored_so_far;
2049   mask = 1;
2050
2051   for (i = 32; i < 64; i++) {
2052     if (regs_ever_live[i] && ! call_used_regs[i]) {
2053       fprintf (asm_file, "\tfld.l %d(%sfp),%s%s\n",
2054         must_preserve_bytes + (4 * restored_so_far++),
2055         i860_reg_prefix, i860_reg_prefix, reg_names[i]);
2056       if (i > 33 && i < 40)
2057         flags->fregs |= mask;
2058     }
2059     if (i > 33 && i < 40)
2060       mask<<=1;
2061   }
2062
2063   /* Get the value we plan to use to restore the stack pointer into r31.  */
2064
2065   fprintf (asm_file, "\tadds %d,%sfp,%sr31\n",
2066     frame_upper_bytes, i860_reg_prefix, i860_reg_prefix);
2067
2068   /* Restore the return address and the old frame pointer.  */
2069
2070   if (must_preserve_r1) {
2071     fprintf (asm_file, "\tld.l 4(%sfp),%sr1\n",
2072       i860_reg_prefix, i860_reg_prefix);
2073     flags->iregs |= 2;
2074   }
2075
2076   fprintf (asm_file, "\tld.l 0(%sfp),%sfp\n",
2077     i860_reg_prefix, i860_reg_prefix);
2078
2079   /* Return and restore the old stack pointer value.  */
2080
2081   fprintf (asm_file, "\tbri %sr1\n\tmov %sr31,%ssp\n",
2082     i860_reg_prefix, i860_reg_prefix, i860_reg_prefix);
2083
2084 #ifdef  OUTPUT_TDESC    /* Output an ABI-compliant TDESC entry */
2085   if (! frame_lower_bytes) {
2086     flags->version--;
2087     if (! frame_upper_bytes) {
2088       flags->version--;
2089       if (restored_so_far == int_restored)      /* No FP saves */
2090         flags->version--;
2091     }
2092   }
2093   assemble_name(asm_file,current_function_original_name);
2094   fputs(".TDESC:\n", asm_file);
2095   fprintf(asm_file, "%s 0x%0x\n", ASM_LONG, intflags);
2096   fprintf(asm_file, "%s %d\n", ASM_LONG,
2097         int_restored ? must_preserve_bytes : 0);
2098   if (flags->version > 1) {
2099     fprintf(asm_file, "%s %d\n", ASM_LONG,
2100         (restored_so_far == int_restored) ? 0 : must_preserve_bytes +
2101           (4 * int_restored));
2102     if (flags->version > 2) {
2103       fprintf(asm_file, "%s %d\n", ASM_LONG, frame_upper_bytes);
2104       if (flags->version > 3)
2105         fprintf(asm_file, "%s %d\n", ASM_LONG, frame_lower_bytes);
2106     }
2107   }
2108   tdesc_section();
2109   fprintf(asm_file, "%s ", ASM_LONG);
2110   assemble_name(asm_file, current_function_original_name);
2111   fprintf(asm_file, "\n%s ", ASM_LONG);
2112   assemble_name(asm_file, current_function_original_name);
2113   fputs(".TDESC\n", asm_file);
2114   text_section();
2115 #endif
2116 }
2117 \f
2118
2119 /* Expand a library call to __builtin_saveregs.  */
2120 rtx
2121 i860_saveregs ()
2122 {
2123   rtx fn = gen_rtx_SYMBOL_REF (Pmode, "__builtin_saveregs");
2124   rtx save = gen_reg_rtx (Pmode);
2125   rtx valreg = LIBCALL_VALUE (Pmode);
2126   rtx ret;
2127
2128   /* The return value register overlaps the first argument register.
2129      Save and restore it around the call.  */
2130   emit_move_insn (save, valreg);
2131   ret = emit_library_call_value (fn, NULL_RTX, 1, Pmode, 0);
2132   if (GET_CODE (ret) != REG || REGNO (ret) < FIRST_PSEUDO_REGISTER)
2133     ret = copy_to_reg (ret);
2134   emit_move_insn (valreg, save);
2135
2136   return ret;
2137 }
2138
2139 tree
2140 i860_build_va_list ()
2141 {
2142   tree field_ireg_used, field_freg_used, field_reg_base, field_mem_ptr;
2143   tree record;
2144
2145   record = make_node (RECORD_TYPE);
2146
2147   field_ireg_used = build_decl (FIELD_DECL, get_identifier ("__ireg_used"),
2148                                 unsigned_type_node);
2149   field_freg_used = build_decl (FIELD_DECL, get_identifier ("__freg_used"),
2150                                 unsigned_type_node);
2151   field_reg_base = build_decl (FIELD_DECL, get_identifier ("__reg_base"),
2152                                ptr_type_node);
2153   field_mem_ptr = build_decl (FIELD_DECL, get_identifier ("__mem_ptr"),
2154                               ptr_type_node);
2155
2156   DECL_FIELD_CONTEXT (field_ireg_used) = record;
2157   DECL_FIELD_CONTEXT (field_freg_used) = record;
2158   DECL_FIELD_CONTEXT (field_reg_base) = record;
2159   DECL_FIELD_CONTEXT (field_mem_ptr) = record;
2160
2161 #ifdef I860_SVR4_VA_LIST
2162   TYPE_FIELDS (record) = field_ireg_used;
2163   TREE_CHAIN (field_ireg_used) = field_freg_used;
2164   TREE_CHAIN (field_freg_used) = field_reg_base;
2165   TREE_CHAIN (field_reg_base) = field_mem_ptr;
2166 #else
2167   TYPE_FIELDS (record) = field_reg_base;
2168   TREE_CHAIN (field_reg_base) = field_mem_ptr;
2169   TREE_CHAIN (field_mem_ptr) = field_ireg_used;
2170   TREE_CHAIN (field_ireg_used) = field_freg_used;
2171 #endif
2172
2173   layout_type (record);
2174   return record;
2175 }
2176
2177 void
2178 i860_va_start (stdarg_p, valist, nextarg)
2179      int stdarg_p;
2180      tree valist;
2181      rtx nextarg;
2182 {
2183   tree saveregs, t;
2184
2185   saveregs = make_tree (build_pointer_type (va_list_type_node),
2186                         expand_builtin_saveregs ());
2187   saveregs = build1 (INDIRECT_REF, va_list_type_node, saveregs);
2188
2189   if (stdarg_p)
2190     {
2191       tree field_ireg_used, field_freg_used, field_reg_base, field_mem_ptr;
2192       tree ireg_used, freg_used, reg_base, mem_ptr;
2193
2194 #ifdef I860_SVR4_VA_LIST
2195       field_ireg_used = TYPE_FIELDS (va_list_type_node);
2196       field_freg_used = TREE_CHAIN (field_ireg_used);
2197       field_reg_base = TREE_CHAIN (field_freg_used);
2198       field_mem_ptr = TREE_CHAIN (field_reg_base);
2199 #else
2200       field_reg_base = TYPE_FIELDS (va_list_type_node);
2201       field_mem_ptr = TREE_CHAIN (field_reg_base);
2202       field_ireg_used = TREE_CHAIN (field_mem_ptr);
2203       field_freg_used = TREE_CHAIN (field_ireg_used);
2204 #endif
2205
2206       ireg_used = build (COMPONENT_REF, TREE_TYPE (field_ireg_used),
2207                          valist, field_ireg_used);
2208       freg_used = build (COMPONENT_REF, TREE_TYPE (field_freg_used),
2209                          valist, field_freg_used);
2210       reg_base = build (COMPONENT_REF, TREE_TYPE (field_reg_base),
2211                         valist, field_reg_base);
2212       mem_ptr = build (COMPONENT_REF, TREE_TYPE (field_mem_ptr),
2213                        valist, field_mem_ptr);
2214
2215       t = build_int_2 (current_function_args_info.ints, 0);
2216       t = build (MODIFY_EXPR, TREE_TYPE (ireg_used), ireg_used, t);
2217       TREE_SIDE_EFFECTS (t) = 1;
2218       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2219       
2220       t = build_int_2 (ROUNDUP (current_function_args_info.floats, 8), 0);
2221       t = build (MODIFY_EXPR, TREE_TYPE (freg_used), freg_used, t);
2222       TREE_SIDE_EFFECTS (t) = 1;
2223       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2224       
2225       t = build (COMPONENT_REF, TREE_TYPE (field_reg_base),
2226                  saveregs, field_reg_base);
2227       t = build (MODIFY_EXPR, TREE_TYPE (reg_base), reg_base, t);
2228       TREE_SIDE_EFFECTS (t) = 1;
2229       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2230
2231       t = make_tree (ptr_type_node, nextarg);
2232       t = build (MODIFY_EXPR, TREE_TYPE (mem_ptr), mem_ptr, t);
2233       TREE_SIDE_EFFECTS (t) = 1;
2234       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2235     }
2236   else
2237     {
2238       t = build (MODIFY_EXPR, va_list_type_node, valist, saveregs);
2239       TREE_SIDE_EFFECTS (t) = 1;
2240       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2241     }
2242 }
2243
2244 #define NUM_PARM_FREGS  8
2245 #define NUM_PARM_IREGS  12
2246 #ifdef I860_SVR4_VARARGS
2247 #define FREG_OFFSET 0
2248 #define IREG_OFFSET (NUM_PARM_FREGS * UNITS_PER_WORD)
2249 #else
2250 #define FREG_OFFSET (NUM_PARM_IREGS * UNITS_PER_WORD)
2251 #define IREG_OFFSET 0
2252 #endif
2253
2254 rtx
2255 i860_va_arg (valist, type)
2256      tree valist, type;
2257 {
2258   tree field_ireg_used, field_freg_used, field_reg_base, field_mem_ptr;
2259   tree type_ptr_node, t;
2260   rtx lab_over = NULL_RTX;
2261   rtx ret, val;
2262   HOST_WIDE_INT align;
2263
2264 #ifdef I860_SVR4_VA_LIST
2265   field_ireg_used = TYPE_FIELDS (va_list_type_node);
2266   field_freg_used = TREE_CHAIN (field_ireg_used);
2267   field_reg_base = TREE_CHAIN (field_freg_used);
2268   field_mem_ptr = TREE_CHAIN (field_reg_base);
2269 #else
2270   field_reg_base = TYPE_FIELDS (va_list_type_node);
2271   field_mem_ptr = TREE_CHAIN (field_reg_base);
2272   field_ireg_used = TREE_CHAIN (field_mem_ptr);
2273   field_freg_used = TREE_CHAIN (field_ireg_used);
2274 #endif
2275
2276   field_ireg_used = build (COMPONENT_REF, TREE_TYPE (field_ireg_used),
2277                            valist, field_ireg_used);
2278   field_freg_used = build (COMPONENT_REF, TREE_TYPE (field_freg_used),
2279                            valist, field_freg_used);
2280   field_reg_base = build (COMPONENT_REF, TREE_TYPE (field_reg_base),
2281                           valist, field_reg_base);
2282   field_mem_ptr = build (COMPONENT_REF, TREE_TYPE (field_mem_ptr),
2283                          valist, field_mem_ptr);
2284
2285   ret = gen_reg_rtx (Pmode);
2286   type_ptr_node = build_pointer_type (type);
2287
2288   if (! AGGREGATE_TYPE_P (type))
2289     {
2290       int nparm, incr, ofs;
2291       tree field;
2292       rtx lab_false;
2293
2294       if (FLOAT_TYPE_P (type))
2295         {
2296           field = field_freg_used;
2297           nparm = NUM_PARM_FREGS;
2298           incr = 2;
2299           ofs = FREG_OFFSET;
2300         }
2301       else
2302         {
2303           field = field_ireg_used;
2304           nparm = NUM_PARM_IREGS;
2305           incr = int_size_in_bytes (type) / UNITS_PER_WORD;
2306           ofs = IREG_OFFSET;
2307         }
2308
2309       lab_false = gen_label_rtx ();
2310       lab_over = gen_label_rtx ();
2311
2312       emit_cmp_and_jump_insns (expand_expr (field, NULL_RTX, 0, 0),
2313                                GEN_INT (nparm - incr), GT, const0_rtx,
2314                                TYPE_MODE (TREE_TYPE (field)),
2315                                TREE_UNSIGNED (field), 0, lab_false);
2316
2317       t = fold (build (POSTINCREMENT_EXPR, TREE_TYPE (field), field,
2318                        build_int_2 (incr, 0)));
2319       TREE_SIDE_EFFECTS (t) = 1;
2320
2321       t = fold (build (MULT_EXPR, TREE_TYPE (field), field,
2322                        build_int_2 (UNITS_PER_WORD, 0)));
2323       TREE_SIDE_EFFECTS (t) = 1;
2324       
2325       t = fold (build (PLUS_EXPR, ptr_type_node, field_reg_base,
2326                        fold (build (PLUS_EXPR, TREE_TYPE (field), t,
2327                                     build_int_2 (ofs, 0)))));
2328       TREE_SIDE_EFFECTS (t) = 1;
2329       
2330       val = expand_expr (t, ret, VOIDmode, EXPAND_NORMAL);
2331       if (val != ret)
2332         emit_move_insn (ret, val);
2333
2334       emit_jump_insn (gen_jump (lab_over));
2335       emit_barrier ();
2336       emit_label (lab_false);
2337     }
2338
2339   align = TYPE_ALIGN (type);
2340   if (align < BITS_PER_WORD)
2341     align = BITS_PER_WORD;
2342   align /= BITS_PER_UNIT;
2343
2344   t = build (PLUS_EXPR, ptr_type_node, field_mem_ptr,
2345              build_int_2 (align - 1, 0));
2346   t = build (BIT_AND_EXPR, ptr_type_node, t, build_int_2 (-align, -1));
2347
2348   val = expand_expr (t, ret, VOIDmode, EXPAND_NORMAL);
2349   if (val != ret)
2350     emit_move_insn (ret, val);
2351
2352   t = fold (build (PLUS_EXPR, ptr_type_node,
2353                    make_tree (ptr_type_node, ret),
2354                    build_int_2 (int_size_in_bytes (type), 0)));
2355   t = build (MODIFY_EXPR, ptr_type_node, field_mem_ptr, t);
2356   TREE_SIDE_EFFECTS (t) = 1;
2357   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2358
2359   if (lab_over)
2360     emit_label (lab_over);
2361
2362   return ret;
2363 }