OSDN Git Service

dummy import to prevent merge lossage
[pf3gnuchains/gcc-fork.git] / gcc / config / a29k / a29k.c
1 /* Subroutines used for code generation on AMD Am29000.
2    Copyright (C) 1987, 88, 90-94, 1995, 1997 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@nyu.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include "rtl.h"
25 #include "regs.h"
26 #include "hard-reg-set.h"
27 #include "real.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "expr.h"
36 #include "obstack.h"
37 #include "tree.h"
38 #include "reload.h"
39
40 #define min(A,B)        ((A) < (B) ? (A) : (B))
41
42 /* This gives the size in words of the register stack for the current
43    procedure.  */
44
45 static int a29k_regstack_size;
46
47 /* True if the current procedure has a call instruction.  */
48
49 static int a29k_makes_calls;
50
51 /* This points to the last insn of the insn prologue.  It is set when
52    an insn without a filled delay slot is found near the start of the
53    function.  */
54
55 static char *a29k_last_prologue_insn;
56
57 /* This points to the first insn that will be in the epilogue.  It is null if
58    no epilogue is required.  */
59
60 static char *a29k_first_epilogue_insn;
61
62 /* This is nonzero if a a29k_first_epilogue_insn was put in a delay slot.  It
63    indicates that an intermediate label needs to be written.  */
64
65 static int a29k_first_epilogue_insn_used;
66
67 /* Location to hold the name of the current function.  We need this prolog to
68    contain the tag words prior to the declaration.  So the name must be stored
69    away.  */
70
71 char *a29k_function_name;
72
73 /* Mapping of registers to debug register numbers.  The only change is
74    for the frame pointer and the register numbers used for the incoming
75    arguments.  */
76
77 int a29k_debug_reg_map[FIRST_PSEUDO_REGISTER];
78
79 /* Save information from a "cmpxx" operation until the branch or scc is
80    emitted.  */
81
82 rtx a29k_compare_op0, a29k_compare_op1;
83 int a29k_compare_fp_p;
84
85 /* Gives names for registers.  */
86 extern char *reg_names[];
87 \f
88 /* Returns 1 if OP is a 8-bit constant. */
89
90 int
91 cint_8_operand (op, mode)
92      register rtx op;
93      enum machine_mode mode;
94 {
95   return GET_CODE (op) == CONST_INT && (INTVAL (op) & 0xffffff00) == 0;
96 }
97
98 /* Returns 1 if OP is a 16-bit constant.  */
99
100 int
101 cint_16_operand (op, mode)
102      rtx op;
103      enum machine_mode mode;
104 {
105   return GET_CODE (op) == CONST_INT && (INTVAL (op) & 0xffff0000) == 0;
106 }
107
108 /* Returns 1 if OP is a constant that cannot be moved in a single insn.  */
109
110 int
111 long_const_operand (op, mode)
112      register rtx op;
113      enum machine_mode mode;
114 {
115   if (! CONSTANT_P (op))
116     return 0;
117
118   if (TARGET_29050 && GET_CODE (op) == CONST_INT
119       && (INTVAL (op) & 0xffff) == 0)
120     return 0;
121
122   return (GET_CODE (op) != CONST_INT
123           || ((INTVAL (op) & 0xffff0000) != 0
124               && (INTVAL (op) & 0xffff0000) != 0xffff0000
125               && INTVAL (op) != 0x80000000));
126 }
127 \f
128 /* The following four functions detect constants of 0, 8, 16, and 24 used as
129    a position in ZERO_EXTRACT operations.  They can either be the appropriate
130    constant integer or a shift (which will be produced by combine).  */
131
132 static int
133 shift_constant_operand (op, mode, val)
134      rtx op;
135      enum machine_mode mode;
136      int val;
137 {
138   return ((GET_CODE (op) == CONST_INT && INTVAL (op) == val)
139           || (GET_CODE (op) == ASHIFT
140               && GET_CODE (XEXP (op, 0)) == CONST_INT
141               && INTVAL (XEXP (op, 0)) == val / 8
142               && GET_CODE (XEXP (op, 1)) == CONST_INT
143               && INTVAL (XEXP (op, 1)) == 3));
144 }
145
146 int
147 const_0_operand (op, mode)
148      rtx op;
149      enum machine_mode mode;
150 {
151   return shift_constant_operand (op, mode, 0);
152 }
153
154 int
155 const_8_operand (op, mode)
156      rtx op;
157      enum machine_mode mode;
158 {
159   return shift_constant_operand (op, mode, 8);
160 }
161
162 int
163 const_16_operand (op, mode)
164      rtx op;
165      enum machine_mode mode;
166 {
167   return shift_constant_operand (op, mode, 16);
168 }
169
170 int
171 const_24_operand (op, mode)
172      rtx op;
173      enum machine_mode mode;
174 {
175   return shift_constant_operand (op, mode, 24);
176 }
177
178 /* Returns 1 if OP is a floating-point constant of the proper mode.  */
179
180 int
181 float_const_operand (op, mode)
182      rtx op;
183      enum machine_mode mode;
184 {
185   return GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == mode;
186 }
187
188 /* Returns 1 if OP is a floating-point constant of the proper mode or a
189    general-purpose register.  */
190
191 int
192 gpc_reg_or_float_constant_operand (op, mode)
193      rtx op;
194      enum machine_mode mode;
195 {
196   return float_const_operand (op, mode) || gpc_reg_operand (op, mode);
197 }
198
199 /* Returns 1 if OP is an integer constant of the proper mode or a
200    general-purpose register.  */
201
202 int
203 gpc_reg_or_integer_constant_operand (op, mode)
204      rtx op;
205      enum machine_mode mode;
206 {
207   return ((GET_MODE (op) == VOIDmode
208            && (GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE))
209           || gpc_reg_operand (op, mode));
210 }
211 \f     
212 /* Returns 1 if OP is a special machine register.  */
213
214 int
215 spec_reg_operand (op, mode)
216      rtx op;
217      enum machine_mode mode;
218 {
219   if (GET_CODE (op) != REG || GET_MODE (op) != mode)
220     return 0;
221
222   switch (GET_MODE_CLASS (mode))
223     {
224     case MODE_PARTIAL_INT:
225       return REGNO (op) >= R_BP && REGNO (op) <= R_CR;
226     case MODE_INT:
227       return REGNO (op) >= R_Q && REGNO (op) <= R_EXO;
228     default:
229       return 0;
230     }
231 }
232
233 /* Returns 1 if OP is an accumulator register.  */
234
235 int
236 accum_reg_operand (op, mode)
237      rtx op;
238      enum machine_mode mode;
239 {
240   return (GET_CODE (op) == REG
241           && REGNO (op) >= R_ACU (0) && REGNO (op) <= R_ACU (3));
242 }
243
244 /* Returns 1 if OP is a normal data register.  */
245
246 int
247 gpc_reg_operand (op, mode)
248      rtx op;
249      enum machine_mode mode;
250 {
251   int regno;
252
253   if (GET_MODE (op) != mode && mode != VOIDmode)
254     return 0;
255
256   if (GET_CODE (op) == REG)
257     regno = REGNO (op);
258   else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
259     {
260       regno = REGNO (SUBREG_REG (op));
261       if (regno < FIRST_PSEUDO_REGISTER)
262         regno += SUBREG_WORD (op);
263     }
264   else
265     return 0;
266
267   return (regno >= FIRST_PSEUDO_REGISTER || regno < R_BP
268           || (regno >= R_KR (0) && regno <= R_KR (31)));
269 }
270
271 /* Returns 1 if OP is either an 8-bit constant integer or a general register.
272    If a register, it must be in the proper mode unless MODE is VOIDmode.  */
273
274 int
275 srcb_operand (op, mode)
276       register rtx op;
277       enum machine_mode mode;
278 {
279   if (GET_CODE (op) == CONST_INT
280       && (mode == QImode
281           || (INTVAL (op) & 0xffffff00) == 0))
282     return 1;
283
284   if (GET_MODE (op) != mode && mode != VOIDmode)
285     return 0;
286
287   return gpc_reg_operand (op, mode);
288 }
289
290 int
291 cmplsrcb_operand (op, mode)
292       register rtx op;
293       enum machine_mode mode;
294 {
295   if (GET_CODE (op) == CONST_INT
296       && (mode == QImode
297           || (INTVAL (op) & 0xffffff00) == 0xffffff00))
298     return 1;
299
300   if (GET_MODE (op) != mode && mode != VOIDmode)
301     return 0;
302
303   return gpc_reg_operand (op, mode);
304 }
305
306 /* Return 1 if OP is either an immediate or a general register.  This is used
307    for the input operand of mtsr/mtrsim.  */
308
309 int
310 gpc_reg_or_immediate_operand (op, mode)
311      rtx op;
312      enum machine_mode mode;
313 {
314   return gpc_reg_operand (op, mode) || immediate_operand (op, mode);
315 }
316
317 /* Return 1 if OP can be used as the second operand of and AND insn.  This
318    includes srcb_operand and a constant whose complement fits in 8 bits.  */
319
320 int
321 and_operand (op, mode)
322      rtx op;
323      enum machine_mode mode;
324 {
325   return (srcb_operand (op, mode)
326           || (GET_CODE (op) == CONST_INT
327               && ((unsigned) ((~ INTVAL (op)) & GET_MODE_MASK (mode)) < 256)));
328 }
329
330 /* Return 1 if OP can be used as the second operand of an ADD insn.
331    This is the same as above, except we use negative, rather than
332    complement.   */
333
334 int
335 add_operand (op, mode)
336      rtx op;
337      enum machine_mode mode;
338 {
339   return (srcb_operand (op, mode)
340           || (GET_CODE (op) == CONST_INT
341               && ((unsigned) ((- INTVAL (op)) & GET_MODE_MASK (mode)) < 256)));
342 }
343
344 /* Return 1 if OP is a valid address in a CALL_INSN.  These are a SYMBOL_REF
345    to the current function, all SYMBOL_REFs if TARGET_SMALL_MEMORY, or
346    a sufficiently-small constant.  */
347
348 int
349 call_operand (op, mode)
350      rtx op;
351      enum machine_mode mode;
352 {
353   switch (GET_CODE (op))
354     {
355     case SYMBOL_REF:
356       return (TARGET_SMALL_MEMORY
357               || (! TARGET_LARGE_MEMORY
358                   && ((GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_FLAG (op))
359                       || ! strcmp (XSTR (op, 0), current_function_name))));
360
361     case CONST_INT:
362       return (unsigned HOST_WIDE_INT) INTVAL (op) < 0x40000;
363
364     default:
365       return 0;
366     }
367 }
368 \f
369 /* Return 1 if OP can be used as the input operand for a move insn.  */
370
371 int
372 in_operand (op, mode)
373      rtx op;
374      enum machine_mode mode;
375 {
376   rtx orig_op = op;
377
378   if (! general_operand (op, mode))
379     return 0;
380
381   while (GET_CODE (op) == SUBREG)
382     op = SUBREG_REG (op);
383
384   switch (GET_CODE (op))
385     {
386     case REG:
387       return 1;
388
389     case MEM:
390       return (GET_MODE_SIZE (mode) >= UNITS_PER_WORD || TARGET_DW_ENABLE);
391
392     case CONST_INT:
393       if (GET_MODE_CLASS (mode) != MODE_INT
394           && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
395         return 0;
396
397       return 1;
398
399     case CONST:
400     case SYMBOL_REF:
401     case LABEL_REF:
402       return (GET_MODE (op) == mode
403               || mode == SImode || mode == HImode || mode == QImode);
404
405     case CONST_DOUBLE:
406       return ((GET_MODE_CLASS (mode) == MODE_FLOAT
407                && mode == GET_MODE (op))
408               || (GET_MODE (op) == VOIDmode
409                   && GET_MODE_CLASS (mode) == MODE_INT));
410
411     default:
412       return 0;
413     }
414 }
415
416 /* Return 1 if OP can be used as the output operand for a move insn.  */
417
418 int
419 out_operand (op, mode)
420      rtx op;
421      enum machine_mode mode;
422 {
423   rtx orig_op = op;
424
425   if (! general_operand (op, mode))
426     return 0;
427
428   while (GET_CODE (op) == SUBREG)
429     op = SUBREG_REG (op);
430
431   if (GET_CODE (op) == REG)
432     return (gpc_reg_operand (orig_op, mode)
433             || spec_reg_operand (orig_op, mode)
434             || (GET_MODE_CLASS (mode) == MODE_FLOAT
435                 && accum_reg_operand (orig_op, mode)));
436
437   else if (GET_CODE (op) == MEM)
438     return (GET_MODE_SIZE (mode) >= UNITS_PER_WORD || TARGET_DW_ENABLE);
439   else
440     return 0;
441 }
442
443 /* Return 1 if OP is an item in memory, given that we are in reload.  */
444
445 int
446 reload_memory_operand (op, mode)
447      rtx op;
448      enum machine_mode mode;
449 {
450   int regno = true_regnum (op);
451
452   return (! CONSTANT_P (op)
453           && (regno == -1
454               || (GET_CODE (op) == REG
455                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
456 }
457
458 /* Given an object for which reload_memory_operand is true, return the address
459    of the operand, taking into account anything that reload may do.  */
460
461 rtx
462 a29k_get_reloaded_address (op)
463      rtx op;
464 {
465   if (GET_CODE (op) == SUBREG)
466     {
467       if (SUBREG_WORD (op) != 0)
468         abort ();
469
470       op = SUBREG_REG (op);
471     }
472
473   if (GET_CODE (op) == REG)
474     op = reg_equiv_mem[REGNO (op)];
475
476   return find_replacement (&XEXP (op, 0));
477 }
478 \f
479 /* Subfunction of the following function.  Update the flags of any MEM
480    found in part of X.  */
481
482 static void
483 a29k_set_memflags_1 (x, in_struct_p, volatile_p, unchanging_p)
484      rtx x;
485      int in_struct_p, volatile_p, unchanging_p;
486 {
487   int i;
488
489   switch (GET_CODE (x))
490     {
491     case SEQUENCE:
492     case PARALLEL:
493       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
494         a29k_set_memflags_1 (XVECEXP (x, 0, i), in_struct_p, volatile_p,
495                              unchanging_p);
496       break;
497
498     case INSN:
499       a29k_set_memflags_1 (PATTERN (x), in_struct_p, volatile_p,
500                            unchanging_p);
501       break;
502
503     case SET:
504       a29k_set_memflags_1 (SET_DEST (x), in_struct_p, volatile_p,
505                            unchanging_p);
506       a29k_set_memflags_1 (SET_SRC (x), in_struct_p, volatile_p, unchanging_p);
507       break;
508
509     case MEM:
510       MEM_IN_STRUCT_P (x) = in_struct_p;
511       MEM_VOLATILE_P (x) = volatile_p;
512       RTX_UNCHANGING_P (x) = unchanging_p;
513       break;
514     }
515 }
516
517 /* Given INSN, which is either an INSN or a SEQUENCE generated to
518    perform a memory operation, look for any MEMs in either a SET_DEST or
519    a SET_SRC and copy the in-struct, unchanging, and volatile flags from
520    REF into each of the MEMs found.  If REF is not a MEM, don't do
521    anything.  */
522
523 void
524 a29k_set_memflags (insn, ref)
525      rtx insn;
526      rtx ref;
527 {
528   /* Note that it is always safe to get these flags, though they won't
529      be what we think if REF is not a MEM.  */
530   int in_struct_p = MEM_IN_STRUCT_P (ref);
531   int volatile_p = MEM_VOLATILE_P (ref);
532   int unchanging_p = RTX_UNCHANGING_P (ref);
533
534   if (GET_CODE (ref) != MEM
535       || (! in_struct_p && ! volatile_p && ! unchanging_p))
536     return;
537
538   a29k_set_memflags_1 (insn, in_struct_p, volatile_p, unchanging_p);
539 }
540 \f
541 /* Return 1 if OP is a comparison operator that we have in floating-point.  */
542
543 int
544 fp_comparison_operator (op, mode)
545      rtx op;
546      enum machine_mode mode;
547 {
548   return ((mode == VOIDmode || mode == GET_MODE (op))
549           && (GET_CODE (op) == EQ || GET_CODE (op) == GT ||
550               GET_CODE (op) == GE));
551 }
552
553 /* Return 1 if OP is a valid branch comparison.  */
554
555 int
556 branch_operator (op, mode)
557      rtx op;
558      enum machine_mode mode;
559 {
560   return ((mode == VOIDmode || mode == GET_MODE (op))
561           && (GET_CODE (op) == GE || GET_CODE (op) == LT));
562 }
563 \f
564 /* Return 1 if OP is a load multiple operation.  It is known to be a
565    PARALLEL and the first three sections will be tested.  */
566
567 int
568 load_multiple_operation (op, mode)
569      rtx op;
570      enum machine_mode mode;
571 {
572   int count = XVECLEN (op, 0) - 2;
573   int dest_regno;
574   rtx src_addr;
575   int i;
576
577   /* Perform a quick check so we don't blow up below.  */
578   if (count <= 1
579       || GET_CODE (XVECEXP (op, 0, 0)) != SET
580       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
581       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
582     return 0;
583
584   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
585   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
586
587   for (i = 1; i < count; i++)
588     {
589       rtx elt = XVECEXP (op, 0, i + 2);
590
591       if (GET_CODE (elt) != SET
592           || GET_CODE (SET_DEST (elt)) != REG
593           || GET_MODE (SET_DEST (elt)) != SImode
594           || REGNO (SET_DEST (elt)) != dest_regno + i
595           || GET_CODE (SET_SRC (elt)) != MEM
596           || GET_MODE (SET_SRC (elt)) != SImode
597           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
598           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
599           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
600           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
601         return 0;
602     }
603
604   return 1;
605 }
606
607 /* Similar, but tests for store multiple.  */
608
609 int
610 store_multiple_operation (op, mode)
611      rtx op;
612      enum machine_mode mode;
613 {
614   int num_special = TARGET_NO_STOREM_BUG ? 2 : 1;
615   int count = XVECLEN (op, 0) - num_special;
616   int src_regno;
617   rtx dest_addr;
618   int i;
619
620   /* Perform a quick check so we don't blow up below.  */
621   if (count <= 1
622       || GET_CODE (XVECEXP (op, 0, 0)) != SET
623       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
624       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
625     return 0;
626
627   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
628   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
629
630   for (i = 1; i < count; i++)
631     {
632       rtx elt = XVECEXP (op, 0, i + num_special);
633
634       if (GET_CODE (elt) != SET
635           || GET_CODE (SET_SRC (elt)) != REG
636           || GET_MODE (SET_SRC (elt)) != SImode
637           || REGNO (SET_SRC (elt)) != src_regno + i
638           || GET_CODE (SET_DEST (elt)) != MEM
639           || GET_MODE (SET_DEST (elt)) != SImode
640           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
641           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
642           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
643           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
644         return 0;
645     }
646
647   return 1;
648 }
649 \f
650 /* Given a special register REG and MASK, a value being masked against a
651    quantity to which the special register is set, return 1 if the masking
652    operation is built-in to the setting of that special register.  */
653
654 int
655 masks_bits_for_special (reg, mask)
656      rtx reg;
657      rtx mask;
658 {
659    int needed_mask_value;
660
661   if (GET_CODE (reg) != REG || GET_CODE (mask) != CONST_INT)
662     abort ();
663
664   switch (REGNO (reg))
665     {
666     case R_BP:
667     case R_INT:
668       needed_mask_value = 3;
669       break;
670
671     case R_FC:
672       needed_mask_value = 31;
673       break;
674
675     case R_CR:
676     case R_LRU:
677       needed_mask_value = 255;
678       break;
679
680     case R_FPE:
681       needed_mask_value = 511;
682       break;
683
684     case R_MMU:
685       needed_mask_value = 0x3ff;
686       break;
687
688     case R_OPS:
689     case R_CPS:
690     case R_RBP:
691     case R_FPS:
692       needed_mask_value = 0xffff;
693       break;
694
695     case R_VAB:
696       needed_mask_value = 0xffff0000;
697       break;
698
699     case R_Q:
700     case R_CFG:
701     case R_CHA:
702     case R_CHD:
703     case R_CHC:
704     case R_TMC:
705     case R_TMR:
706     case R_PC0:
707     case R_PC1:
708     case R_PC2:
709       return 0;
710
711     default:
712       abort ();
713     }
714
715    return (INTVAL (mask) & ~ needed_mask_value) == 0;
716 }
717 \f
718 /* Return nonzero if this label is that of the return point, but there is
719    a non-null epilogue.  */
720
721 int
722 epilogue_operand (op, mode)
723      rtx op;
724      enum machine_mode mode;
725 {
726   return next_active_insn (op) == 0 && a29k_first_epilogue_insn != 0;
727 }
728 \f
729 /* Return the register class of a scratch register needed to copy IN into
730    or out of a register in CLASS in MODE.  If it can be done directly,
731    NO_REGS is returned.  */
732
733 enum reg_class
734 secondary_reload_class (class, mode, in)
735      enum reg_class class;
736      enum machine_mode mode;
737      rtx in;
738 {
739   int regno = -1;
740   enum rtx_code code = GET_CODE (in);
741
742   if (! CONSTANT_P (in))
743     {
744       regno = true_regnum (in);
745
746       /* A pseudo is the same as memory.  */
747       if (regno == -1 || regno >= FIRST_PSEUDO_REGISTER)
748         code = MEM;
749     }
750
751   /* If we are transferring between memory and a multi-word mode, we need
752      CR.  */
753
754   if (code == MEM && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
755     return CR_REGS;
756
757   /* If between memory and a mode smaller than a word without DW being
758      enabled, we need BP.  */
759
760   if (code == MEM && ! TARGET_DW_ENABLE
761       && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
762     return BP_REGS;
763
764   /* Otherwise, we can place anything into GENERAL_REGS and can put
765      GENERAL_REGS into anything.  */
766   if (class == GENERAL_REGS
767       || (regno != -1
768           && (regno < R_BP
769               || (regno >= R_KR (0) && regno <= R_KR (31)))))
770     return NO_REGS;
771
772   /* We can place 16-bit constants into a special register.  */
773   if (code == CONST_INT
774       && (GET_MODE_BITSIZE (mode) <= 16 || (unsigned) INTVAL (in) <= 65535)
775       && (class == BP_REGS || class == Q_REGS || class == SPECIAL_REGS))
776     return NO_REGS;
777
778   /* Otherwise, we need GENERAL_REGS.  */
779   return GENERAL_REGS;
780 }
781 \f
782 /* START is the zero-based incoming argument register index used (0 is 160,
783    i.e., the first incoming argument register) and COUNT is the number used.
784
785    Mark the corresponding incoming registers as neither fixed nor call used.
786    For each register used for incoming arguments, we have one less local
787    register that can be used.  So also mark some high-numbered registers as
788    fixed.
789
790    Return the first register number to use for the argument.  */
791
792 int
793 incoming_reg (start, count)
794      int start;
795      int count;
796 {
797   int i;
798
799   /* We only use 16 argument registers, so truncate at the end of the
800      area.  */
801   if (start + count > 16)
802     count = 16 - start;
803
804   if (! TARGET_NO_REUSE_ARGS)
805     /* Mark all the used registers as not fixed and saved over calls.  */
806     for (i = R_AR (start); i < R_AR (start + count); i++)
807       {
808         fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 0;
809         CLEAR_HARD_REG_BIT (fixed_reg_set, i);
810         CLEAR_HARD_REG_BIT (call_used_reg_set, i);
811         CLEAR_HARD_REG_BIT (call_fixed_reg_set, i);
812       }
813
814   /* Shorten the maximum size of the frame.
815      Remember that R_AR(-1,-2) are place holders for the caller's lr0,lr1.
816      Make sure to keep the frame rounded to an even boundary.  Rounding up
817      to an 8 byte boundary will use a slot.  Otherwise a frame with 121 local
818      regs and 5 arguments will overrun the stack (121+1 + 5 + 2 > 128).  */
819   /* ??? An alternative would be to never allocate one reg.  */
820   for (i = (R_AR (0) - 2 - start - count) & ~1; i < R_AR (0) - 2 - start; i++)
821     {
822       fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
823       SET_HARD_REG_BIT (fixed_reg_set, i);
824       SET_HARD_REG_BIT (call_used_reg_set, i);
825       SET_HARD_REG_BIT (call_fixed_reg_set, i);
826     }
827
828   return R_AR (start);
829 }
830 \f
831 /* Add CLOBBERs to CALL_INSN_FUNCTION_USAGE chain of INSN indicating
832    that LR2 up to, but not including, OP are clobbered.  If OP is
833    zero, indicate all parameter registers are clobbered.  */
834
835 void
836 a29k_clobbers_to (insn, op)
837      rtx insn;
838      rtx op;
839 {
840   int i;
841   int high_regno;
842
843   if (op == 0)
844     high_regno = R_LR (18);
845   else if (GET_CODE (op) != REG || REGNO (op) < R_LR (0)
846            || REGNO (op) > R_LR (18))
847     abort ();
848   else
849     high_regno = REGNO (op);
850
851   for (i = R_LR (2); i < high_regno; i++)
852     CALL_INSN_FUNCTION_USAGE (insn)
853       = gen_rtx (EXPR_LIST, VOIDmode,
854                  gen_rtx (CLOBBER, VOIDmode, gen_rtx (REG, SImode, i)),
855                  CALL_INSN_FUNCTION_USAGE (insn));
856 }
857 \f
858 /* These routines are used in finding insns to fill delay slots in the
859    epilogue.  */
860
861 /* Return 1 if the current function will adjust the register stack.  */
862
863 int
864 needs_regstack_p ()
865 {
866   int i;
867   rtx insn;
868
869   if (frame_pointer_needed)
870     return 1;
871
872   /* If any local register is used, we need to adjust the regstack.  */
873   for (i = R_LR (127); i >= R_LR (0); i --)
874     if (regs_ever_live[i])
875       return 1;
876
877   /* We need a register stack if we make any calls.  */
878   for (insn = get_insns (); insn; insn = next_insn (insn))
879     if (GET_CODE (insn) == CALL_INSN
880         || (GET_CODE (insn) == INSN
881             && GET_CODE (PATTERN (insn)) == SEQUENCE
882             && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == CALL_INSN))
883       return 1;
884
885   /* Otherwise, we don't.  */
886   return 0;
887 }
888
889 /* Return 1 if X uses a local register.  */
890
891 int
892 uses_local_reg_p (x)
893      rtx x;
894 {
895   char *fmt;
896   int i, j;
897
898   switch (GET_CODE (x))
899     {
900     case REG:
901       return REGNO (x) >= R_LR (0) && REGNO (x) <= R_FP;
902
903     case CONST_INT:
904     case CONST:
905     case PC:
906     case CC0:
907     case LABEL_REF:
908     case SYMBOL_REF:
909       return 0;
910     }
911
912   fmt = GET_RTX_FORMAT (GET_CODE (x));
913   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
914     {
915       if (fmt[i] == 'e')
916         {
917           if (uses_local_reg_p (XEXP (x, i)))
918             return 1;
919         }
920       else if (fmt[i] == 'E')
921         {
922           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
923             if (uses_local_reg_p (XVECEXP (x, i, j)))
924               return 1;
925         }
926     }
927
928   return 0;
929 }
930
931 /* Returns 1 if this function is known to have a null epilogue.  */
932
933 int
934 null_epilogue ()
935 {
936   return (reload_completed && ! needs_regstack_p ()
937           && get_frame_size () == 0
938           && current_function_pretend_args_size == 0);
939 }
940 \f
941 /* Write out the assembler form of an operand.  Recognize the following
942    special options:
943
944         %N means write the low-order 8 bits of the negative of the constant
945         %Q means write a QImode operand (truncate constants to 8 bits)
946         %M means write the low-order 16 bits of the constant
947         %m means write the low-order 16 bits shifted left 16 bits
948         %C means write the low-order 8 bits of the complement of the constant
949         %b means write `f' is this is a reversed condition, `t' otherwise
950         %B means write `t' is this is a reversed condition, `f' otherwise
951         %J means write the 29k opcode part for a comparison operation
952         %e means write the label with an extra `X' is this is the epilogue
953                        otherwise the normal label name
954         %E means write nothing if this insn has a delay slot,
955                        a nop unless this is the epilogue label, in which case
956                        write the first epilogue insn
957         %F means write just the normal operand if the insn has a delay slot;
958                        otherwise, this is a recursive call so output the
959                        symbol + 4 and write the first prologue insn in the
960                        delay slot.
961         %L means write the register number plus one ("low order" register)
962                        or the low-order part of a multi-word constant
963         %O means write the register number plus two
964         %P means write the register number plus three ("low order" of TImode)
965         %S means write the number of words in the mode of the operand,
966                        minus one (for CR)
967         %V means write the number of elements in a PARALLEL minus 1
968         %# means write nothing if we have a delay slot, "\n\tnop" otherwise
969         %* means write the register name for TPC.  */
970
971 void
972 print_operand (file, x, code)
973      FILE *file;
974      rtx x;
975      char code;
976 {
977   char buf[100];
978
979   /* These macros test for integers and extract the low-order bits.  */
980 #define INT_P(X)  \
981 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
982  && GET_MODE (X) == VOIDmode)
983
984 #define INT_LOWPART(X) \
985   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
986
987   switch (code)
988     {
989     case 'Q':
990       if (GET_CODE (x) == REG)
991         break;
992       else if (! INT_P (x))
993         output_operand_lossage ("invalid %%Q value");
994       fprintf (file, "%d", INT_LOWPART (x) & 0xff);
995       return;
996
997     case 'C':
998       if (! INT_P (x))
999         output_operand_lossage ("invalid %%C value");
1000       fprintf (file, "%d", (~ INT_LOWPART (x)) & 0xff);
1001       return;
1002
1003     case 'N':
1004       if (! INT_P (x))
1005         output_operand_lossage ("invalid %%N value");
1006       fprintf (file, "%d", (- INT_LOWPART (x)) & 0xff);
1007       return;
1008
1009     case 'M':
1010       if (! INT_P (x))
1011         output_operand_lossage ("invalid %%M value");
1012       fprintf (file, "%d", INT_LOWPART (x) & 0xffff);
1013       return;
1014
1015     case 'm':
1016       if (! INT_P (x))
1017         output_operand_lossage ("invalid %%m value");
1018       fprintf (file, "%d", (INT_LOWPART (x) & 0xffff) << 16);
1019       return;
1020
1021     case 'b':
1022       if (GET_CODE (x) == GE)
1023         fprintf (file, "f");
1024       else
1025         fprintf (file, "t");
1026       return;
1027
1028     case 'B':
1029       if (GET_CODE (x) == GE)
1030         fprintf (file, "t");
1031       else
1032         fprintf (file, "f");
1033       return;
1034
1035     case 'J':
1036       /* It so happens that the RTX names for the conditions are the same as
1037          the 29k's insns except for "ne", which requires "neq".  */
1038       fprintf (file, GET_RTX_NAME (GET_CODE (x)));
1039       if (GET_CODE (x) == NE)
1040         fprintf (file, "q");
1041       return;
1042
1043     case 'e':
1044       if (optimize && flag_delayed_branch
1045           && a29k_last_prologue_insn == 0 && epilogue_operand (x, VOIDmode)
1046           && dbr_sequence_length () == 0)
1047         {
1048           /* We need to output the label number of the last label in the
1049              function, which is not necessarily X since there might be
1050              a USE insn in between.  First go forward to the last insn, then
1051              back up to a label.  */
1052           while (NEXT_INSN (x) != 0)
1053             x = NEXT_INSN (x);
1054
1055           while (GET_CODE (x) != CODE_LABEL)
1056             x = PREV_INSN (x);
1057
1058           ASM_GENERATE_INTERNAL_LABEL (buf, "LX", CODE_LABEL_NUMBER (x));
1059           assemble_name (file, buf);
1060         }
1061       else
1062         output_asm_label (x);
1063       return;
1064
1065     case 'E':
1066       if (dbr_sequence_length ())
1067         ;
1068       else if (a29k_last_prologue_insn)
1069         {
1070           fprintf (file, "\n\t%s", a29k_last_prologue_insn);
1071           a29k_last_prologue_insn = 0;
1072         }
1073       else if (optimize && flag_delayed_branch
1074                && epilogue_operand (x, VOIDmode))
1075         {
1076           fprintf (file, "\n\t%s", a29k_first_epilogue_insn);
1077           a29k_first_epilogue_insn_used = 1;
1078         }
1079       else
1080         fprintf (file, "\n\tnop");
1081       return;
1082       
1083     case 'F':
1084       output_addr_const (file, x);
1085       if (dbr_sequence_length () == 0)
1086         {
1087           /* If this doesn't have its delay slot filled, see if we need to
1088              put the last insn of the prolog in it.  If not, see if this is
1089              a recursive call.  If so, we can put the first insn of its
1090              prolog in the delay slot.  Otherwise, write a nop.  */
1091           if (a29k_last_prologue_insn)
1092             {
1093               fprintf (file, "\n\t%s", a29k_last_prologue_insn);
1094               a29k_last_prologue_insn = 0;
1095             }
1096           else if (GET_CODE (x) == SYMBOL_REF
1097               && ! strcmp (XSTR (x, 0), current_function_name))
1098             fprintf (file, "+4\n\t%s,%d",
1099                      a29k_regstack_size >= 64 ? "const gr121" : "sub gr1,gr1",
1100                      a29k_regstack_size * 4);
1101           else
1102             fprintf (file, "\n\tnop");
1103         }
1104       return;
1105
1106     case 'L':
1107       if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
1108         {
1109           union real_extract u;
1110
1111           bcopy ((char *) &CONST_DOUBLE_LOW (x), (char *) &u, sizeof u);
1112           fprintf (file, "$double1(%.20e)", u.d);
1113         }
1114       else if (GET_CODE (x) == REG)
1115         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
1116       else
1117         output_operand_lossage ("invalid %%L value");
1118       return;
1119
1120     case 'O':
1121       if (GET_CODE (x) != REG)
1122         output_operand_lossage ("invalid %%O value");
1123       fprintf (file, "%s", reg_names[REGNO (x) + 2]);
1124       return;
1125
1126     case 'P':
1127       if (GET_CODE (x) != REG)
1128         output_operand_lossage ("invalid %%P value");
1129       fprintf (file, "%s", reg_names[REGNO (x) + 3]);
1130       return;
1131
1132     case 'S':
1133       fprintf (file, "%d", (GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD)-1);
1134       return;
1135
1136     case 'V':
1137       if (GET_CODE (x) != PARALLEL)
1138         output_operand_lossage ("invalid %%V value");
1139       fprintf (file, "%d", XVECLEN (x, 0) - 2);
1140       return;
1141
1142     case '#':
1143       if (dbr_sequence_length () == 0)
1144         {
1145           if (a29k_last_prologue_insn)
1146             {
1147               fprintf (file, "\n\t%s", a29k_last_prologue_insn);
1148               a29k_last_prologue_insn = 0;
1149             }
1150           else
1151             fprintf (file, "\n\tnop");
1152         }
1153       return;
1154
1155     case '*':
1156       fprintf (file, "%s", reg_names [R_TPC]);
1157       return;
1158     }
1159
1160   if (GET_CODE (x) == REG)
1161     fprintf (file, "%s", reg_names [REGNO (x)]);
1162
1163   else if (GET_CODE (x) == MEM)
1164     output_address (XEXP (x, 0));
1165
1166   else if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == SUBREG
1167            && GET_CODE (SUBREG_REG (XEXP (x, 0))) == CONST_DOUBLE)
1168     {
1169       union real_extract u;
1170
1171       if (GET_MODE (SUBREG_REG (XEXP (x, 0))) == SFmode)
1172         fprintf (file, "$float");
1173       else
1174         fprintf (file, "$double%d", SUBREG_WORD (XEXP (x, 0)));
1175       bcopy ((char *) &CONST_DOUBLE_LOW (SUBREG_REG (XEXP (x, 0))),
1176              (char *) &u, sizeof u);
1177       fprintf (file, "(%.20e)", u.d);
1178     }
1179
1180   else if (GET_CODE (x) == CONST_DOUBLE
1181            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1182     {
1183       union real_extract u;
1184
1185       bcopy ((char *) &CONST_DOUBLE_LOW (x), (char *) &u, sizeof u);
1186       fprintf (file, "$%s(%.20e)",
1187                GET_MODE (x) == SFmode ? "float" : "double0", u.d);
1188     }
1189
1190   else
1191     output_addr_const (file, x);
1192 }
1193 \f
1194 /* This page contains routines to output function prolog and epilog code. */
1195
1196 /* Compute the size of the register stack, and determine if there are any
1197    call instructions.  */
1198
1199 static void
1200 compute_regstack_size ()
1201 {
1202   int i;
1203   rtx insn;
1204
1205   /* See if we make any calls.  We need to set lr1 if so.  */
1206   a29k_makes_calls = 0;
1207   for (insn = get_insns (); insn; insn = next_insn (insn))
1208     if (GET_CODE (insn) == CALL_INSN
1209         || (GET_CODE (insn) == INSN
1210             && GET_CODE (PATTERN (insn)) == SEQUENCE
1211             && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == CALL_INSN))
1212       {
1213         a29k_makes_calls = 1;
1214         break;
1215       }
1216
1217   /* Find the highest local register used.  */
1218   for (i = R_LR (127); i >= R_LR (0); i--)
1219     if (regs_ever_live[i])
1220       break;
1221
1222   a29k_regstack_size = i - (R_LR (0) - 1);
1223
1224   /* If calling routines, ensure we count lr0 & lr1.  */
1225   if (a29k_makes_calls && a29k_regstack_size < 2)
1226     a29k_regstack_size = 2;
1227
1228   /* Count frame pointer and align to 8 byte boundary (even number of
1229      registers).  */
1230   a29k_regstack_size += frame_pointer_needed;
1231   if (a29k_regstack_size & 1) a29k_regstack_size++;
1232 }
1233
1234 /*  Sets register names for incoming arguments and frame pointer.
1235     This can't be computed until after register allocation.  */
1236
1237 void
1238 a29k_compute_reg_names ()
1239 {
1240   int i;
1241
1242   compute_regstack_size ();
1243
1244   /* Set the names and numbers of the frame pointer and incoming argument
1245      registers.  */
1246
1247   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1248     a29k_debug_reg_map[i] = i;
1249
1250   reg_names[FRAME_POINTER_REGNUM] = reg_names[R_LR (a29k_regstack_size - 1)];
1251   a29k_debug_reg_map[FRAME_POINTER_REGNUM] = R_LR (a29k_regstack_size - 1);
1252
1253   for (i = 0; i < 16; i++)
1254     {
1255       reg_names[R_AR (i)] = reg_names[R_LR (a29k_regstack_size + i + 2)];
1256       a29k_debug_reg_map[R_AR (i)] = R_LR (a29k_regstack_size + i + 2);
1257     }
1258
1259   /* If using kernel register map, swap numbers for kernel and user
1260      registers.  */
1261   if (TARGET_KERNEL_REGISTERS)
1262     for (i = 0; i < 32; i++)
1263       {
1264         int tem = a29k_debug_reg_map[i];
1265         a29k_debug_reg_map[i] = a29k_debug_reg_map[R_KR (i)];
1266         a29k_debug_reg_map[R_KR (i)] = tem;
1267       }
1268 }
1269
1270 /* Output function prolog code to file FILE.  Memory stack size is SIZE.  */
1271
1272 void
1273 output_prolog (file, size)
1274      FILE *file;
1275      int size;
1276 {
1277   int i;
1278   int arg_count = 0;
1279   rtx insn;
1280   unsigned int tag_word;
1281
1282   /* See how many incoming arguments we have in registers.  */
1283   for (i = R_AR (0); i < R_AR (16); i++)
1284     if (! fixed_regs[i])
1285       arg_count++;
1286
1287   /* The argument count includes the caller's lr0 and lr1.  */
1288   arg_count += 2;
1289
1290   /* Compute memory stack size.  Add in number of bytes that the we should
1291      push and pretend the caller did and the size of outgoing arguments.
1292      Then round to a doubleword boundary.  */
1293   size += (current_function_pretend_args_size
1294            + current_function_outgoing_args_size);
1295   size = (size + 7) & ~7;
1296
1297   /* Write header words.  See if one or two word form.  */
1298   tag_word = (frame_pointer_needed ? 0x400000 : 0) + (arg_count << 16);
1299
1300   if (size / 8 > 0xff)
1301     fprintf (file, "\t.word %d, 0x%0x\n", (size / 8) << 2,
1302              0x800000 + tag_word);
1303   else
1304     fprintf (file, "\t.word 0x%0x\n", tag_word + ((size / 8) << 3));
1305
1306   /* Define the function name.  */
1307   assemble_name (file, a29k_function_name);
1308   fprintf (file, ":\n");
1309
1310   /* Push the register stack by the proper amount.  There are two possible
1311      ways to do this.  */
1312   if (a29k_regstack_size >= 256/4)
1313     fprintf (file, "\tconst %s,%d\n\tsub gr1,gr1,%s\n",
1314              reg_names[R_TAV], a29k_regstack_size * 4, reg_names[R_TAV]);
1315   else if (a29k_regstack_size)
1316     fprintf (file, "\tsub gr1,gr1,%d\n", a29k_regstack_size * 4);
1317
1318   /* Test that the registers are available.  */
1319   if (a29k_regstack_size)
1320     fprintf (file, "\tasgeu V_%sSPILL,gr1,%s\n",
1321              TARGET_KERNEL_REGISTERS ? "K" : "", reg_names[R_RAB]);
1322
1323   /* Set up frame pointer, if one is needed.  */
1324   if (frame_pointer_needed)
1325     fprintf (file, "\tsll %s,%s,0\n", reg_names[FRAME_POINTER_REGNUM],
1326              reg_names[R_MSP]);
1327
1328   /* Make room for any frame space.  There are three ways to do this.  */
1329   if (size >= 256)
1330     {
1331       fprintf (file, "\tconst %s,%d\n", reg_names[R_TAV], size);
1332       if (size >= 65536)
1333         fprintf (file, "\tconsth %s,%d\n", reg_names[R_TAV], size);
1334       if (TARGET_STACK_CHECK)
1335         fprintf (file, "\tcall %s,__msp_check\n", reg_names[R_TPC]);
1336       fprintf (file, "\tsub %s,%s,%s\n",
1337                reg_names[R_MSP], reg_names[R_MSP], reg_names[R_TAV]);
1338     }
1339   else if (size)
1340     {
1341       if (TARGET_STACK_CHECK)
1342         fprintf (file, "\tcall %s,__msp_check\n", reg_names[R_TPC]);
1343       fprintf (file, "\tsub %s,%s,%d\n",
1344                reg_names[R_MSP], reg_names[R_MSP], size);
1345     }
1346
1347   /* If this routine will make calls, set lr1.  If we see an insn that
1348      can use a delay slot before a call or jump, save this insn for that
1349      slot (this condition is equivalent to seeing if we have an insn that
1350      needs delay slots before an insn that has a filled delay slot).  */
1351   a29k_last_prologue_insn = 0;
1352   if (a29k_makes_calls)
1353     {
1354       i = (a29k_regstack_size + arg_count) * 4;
1355       if (i >= 256)
1356         fprintf (file, "\tconst %s,%d\n\tadd lr1,gr1,%s\n",
1357                  reg_names[R_TAV], i, reg_names[R_TAV]);
1358       else
1359         {
1360           if (optimize && flag_delayed_branch)
1361             for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1362               {
1363                 if (GET_CODE (insn) == CODE_LABEL
1364                     || (GET_CODE (insn) == INSN
1365                         && GET_CODE (PATTERN (insn)) == SEQUENCE))
1366                   break;
1367
1368                 if (GET_CODE (insn) == NOTE
1369                     || (GET_CODE (insn) == INSN
1370                         && (GET_CODE (PATTERN (insn)) == USE
1371                             || GET_CODE (PATTERN (insn)) == CLOBBER)))
1372                   continue;
1373
1374                 if (num_delay_slots (insn) > 0)
1375                   {
1376                     a29k_last_prologue_insn = (char *) oballoc (100);
1377                     sprintf (a29k_last_prologue_insn, "add lr1,gr1,%d", i);
1378                     break;
1379                   }
1380               }
1381
1382           if (a29k_last_prologue_insn == 0)
1383             fprintf (file, "\tadd lr1,gr1,%d\n", i);
1384         }
1385     }
1386
1387   /* Compute the first insn of the epilogue.  */
1388   a29k_first_epilogue_insn_used = 0;
1389
1390   if (size == 0 && a29k_regstack_size == 0 && ! frame_pointer_needed)
1391     a29k_first_epilogue_insn = 0;
1392   else
1393     a29k_first_epilogue_insn = (char *) oballoc (100);
1394
1395   if (frame_pointer_needed)
1396     sprintf (a29k_first_epilogue_insn, "sll %s,%s,0",
1397              reg_names[R_MSP], reg_names[FRAME_POINTER_REGNUM]);
1398   else if (a29k_regstack_size)
1399     {
1400       if (a29k_regstack_size >= 256 / 4)
1401         sprintf (a29k_first_epilogue_insn, "const %s,%d",
1402                  reg_names[R_TAV], a29k_regstack_size * 4);
1403       else
1404         sprintf (a29k_first_epilogue_insn, "add gr1,gr1,%d",
1405                  a29k_regstack_size * 4);
1406     }
1407   else if (size)
1408     {
1409       if (size >= 256)
1410         sprintf (a29k_first_epilogue_insn, "const %s,%d",
1411                  reg_names[R_TAV], size);
1412       else
1413         sprintf (a29k_first_epilogue_insn, "add %s,%s,%d",
1414                  reg_names[R_MSP], reg_names[R_MSP], size);
1415     }
1416 }
1417 \f
1418 /* Call this after writing what might be the first instruction of the
1419    epilogue.  If that first insn was used in a delay slot, an intermediate
1420    label is written.  */
1421
1422 static void
1423 check_epilogue_internal_label (file)
1424      FILE *file;
1425 {
1426   rtx insn;
1427
1428   if (! a29k_first_epilogue_insn_used)
1429     return;
1430
1431   for (insn = get_last_insn ();
1432        GET_CODE (insn) != CODE_LABEL;
1433        insn = PREV_INSN (insn))
1434     ;
1435
1436   ASM_OUTPUT_INTERNAL_LABEL (file, "LX", CODE_LABEL_NUMBER (insn));
1437   a29k_first_epilogue_insn_used = 0;
1438 }
1439
1440 /* Output the epilog of the last procedure to file FILE.  SIZE is the memory
1441    stack size.  The register stack size is in the variable
1442    A29K_REGSTACK_SIZE.  */
1443
1444 void
1445 output_epilog (file, size)
1446      FILE *file;
1447      int size;
1448 {
1449   rtx insn;
1450   int locals_unavailable = 0;   /* True until after first insn
1451                                    after gr1 update. */
1452
1453   /* If we hit a BARRIER before a real insn or CODE_LABEL, we don't
1454      need to do anything because we are never jumped to.  */
1455   insn = get_last_insn ();
1456   if (GET_CODE (insn) == NOTE)
1457     insn = prev_nonnote_insn (insn);
1458
1459   if (insn && GET_CODE (insn) == BARRIER)
1460     return;
1461
1462   /* If a frame pointer was needed we must restore the memory stack pointer
1463      before adjusting the register stack.  */
1464   if (frame_pointer_needed)
1465     {
1466       fprintf (file, "\tsll %s,%s,0\n",
1467                reg_names[R_MSP], reg_names[FRAME_POINTER_REGNUM]);
1468       check_epilogue_internal_label (file);
1469     }
1470
1471   /* Restore the register stack.  There are two ways to do this.  */
1472   if (a29k_regstack_size)
1473     {
1474       if (a29k_regstack_size >= 256/4)
1475         {
1476           fprintf (file, "\tconst %s,%d\n",
1477                    reg_names[R_TAV], a29k_regstack_size * 4);
1478           check_epilogue_internal_label (file);
1479           fprintf (file, "\tadd gr1,gr1,%s\n", reg_names[R_TAV]);
1480         }
1481       else
1482         {
1483           fprintf (file, "\tadd gr1,gr1,%d\n", a29k_regstack_size * 4);
1484           check_epilogue_internal_label (file);
1485         }
1486       locals_unavailable = 1;
1487     }
1488
1489   /* Restore the memory stack pointer if there is no frame pointer.
1490      Adjust the size to include any pretend arguments and pushed
1491      arguments and round to doubleword boundary.  */
1492   size += (current_function_pretend_args_size
1493            + current_function_outgoing_args_size);
1494   size = (size + 7) & ~7;
1495
1496   if (size && ! frame_pointer_needed)
1497     {
1498       if (size >= 256)
1499         {
1500           fprintf (file, "\tconst %s,%d\n", reg_names[R_TAV], size);
1501           check_epilogue_internal_label (file);
1502           locals_unavailable = 0;
1503           if (size >= 65536)
1504             fprintf (file, "\tconsth %s,%d\n", reg_names[R_TAV], size);
1505           fprintf (file, "\tadd %s,%s,%s\n",
1506                    reg_names[R_MSP], reg_names[R_MSP], reg_names[R_TAV]);
1507         }
1508       else
1509         {
1510           fprintf (file, "\tadd %s,%s,%d\n",
1511                    reg_names[R_MSP], reg_names[R_MSP], size);
1512           check_epilogue_internal_label (file);
1513           locals_unavailable = 0;
1514         }
1515     }
1516
1517   if (locals_unavailable)
1518     {
1519       /* If we have an insn for this delay slot, write it.  */
1520       if (current_function_epilogue_delay_list)
1521         final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
1522                          file, 1, -2, 1);
1523       else
1524         fprintf (file, "\tnop\n");
1525     }
1526
1527   fprintf (file, "\tjmpi lr0\n");
1528   if (a29k_regstack_size)
1529     fprintf (file, "\tasleu V_%sFILL,lr1,%s\n",
1530              TARGET_KERNEL_REGISTERS ? "K" : "", reg_names[R_RFB]);
1531   else if (current_function_epilogue_delay_list)
1532     final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
1533                      file, 1, -2, 1);
1534   else
1535     fprintf (file, "\tnop\n");
1536 }