OSDN Git Service

* sparc/sparc.c (output_double_int): Output DI mode values
[pf3gnuchains/gcc-fork.git] / gcc / config / sparc / sparc.c
1 /* Subroutines for insn-output.c for Sun SPARC.
2    Copyright (C) 1987, 88, 89, 92-96, 1997 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4    64 bit SPARC V9 support by Michael Tiemann, Jim Wilson, and Doug Evans,
5    at Cygnus Support.
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 #include <stdio.h>
25 #include "config.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "expr.h"
38 #include "recog.h"
39
40 /* 1 if the caller has placed an "unimp" insn immediately after the call.
41    This is used in v8 code when calling a function that returns a structure.
42    v9 doesn't have this.  Be careful to have this test be the same as that
43    used on the call.  */
44
45 #define SKIP_CALLERS_UNIMP_P  \
46 (!TARGET_ARCH64 && current_function_returns_struct                      \
47  && ! integer_zerop (DECL_SIZE (DECL_RESULT (current_function_decl)))   \
48  && (TREE_CODE (DECL_SIZE (DECL_RESULT (current_function_decl)))        \
49      == INTEGER_CST))
50
51 /* Global variables for machine-dependent things.  */
52
53 /* Size of frame.  Need to know this to emit return insns from leaf procedures.
54    ACTUAL_FSIZE is set by compute_frame_size() which is called during the
55    reload pass.  This is important as the value is later used in insn
56    scheduling (to see what can go in a delay slot).
57    APPARENT_FSIZE is the size of the stack less the register save area and less
58    the outgoing argument area.  It is used when saving call preserved regs.  */
59 static int apparent_fsize;
60 static int actual_fsize;
61
62 /* Save the operands last given to a compare for use when we
63    generate a scc or bcc insn.  */
64
65 rtx sparc_compare_op0, sparc_compare_op1;
66
67 /* We may need an epilogue if we spill too many registers.
68    If this is non-zero, then we branch here for the epilogue.  */
69 static rtx leaf_label;
70
71 #ifdef LEAF_REGISTERS
72
73 /* Vector to say how input registers are mapped to output
74    registers.  FRAME_POINTER_REGNUM cannot be remapped by
75    this function to eliminate it.  You must use -fomit-frame-pointer
76    to get that.  */
77 char leaf_reg_remap[] =
78 { 0, 1, 2, 3, 4, 5, 6, 7,
79   -1, -1, -1, -1, -1, -1, 14, -1,
80   -1, -1, -1, -1, -1, -1, -1, -1,
81   8, 9, 10, 11, 12, 13, -1, 15,
82
83   32, 33, 34, 35, 36, 37, 38, 39,
84   40, 41, 42, 43, 44, 45, 46, 47,
85   48, 49, 50, 51, 52, 53, 54, 55,
86   56, 57, 58, 59, 60, 61, 62, 63,
87   64, 65, 66, 67, 68, 69, 70, 71,
88   72, 73, 74, 75, 76, 77, 78, 79,
89   80, 81, 82, 83, 84, 85, 86, 87,
90   88, 89, 90, 91, 92, 93, 94, 95,
91   96, 97, 98, 99, 100};
92
93 #endif
94
95 /* Name of where we pretend to think the frame pointer points.
96    Normally, this is "%fp", but if we are in a leaf procedure,
97    this is "%sp+something".  We record "something" separately as it may be
98    too big for reg+constant addressing.  */
99
100 static char *frame_base_name;
101 static int frame_base_offset;
102
103 static rtx find_addr_reg ();
104 static void sparc_init_modes ();
105 \f
106 /* Option handling.  */
107
108 /* Record options as passed by user.  */
109 char *sparc_align_loops_string;
110 char *sparc_align_jumps_string;
111 char *sparc_align_funcs_string;
112
113 /* Parsed values, as a power of two.  */
114 int sparc_align_loops;
115 int sparc_align_jumps;
116 int sparc_align_funcs;
117
118 struct sparc_cpu_select sparc_select[] =
119 {
120   /* switch     name,           tune    arch */
121   { (char *)0,  "default",      1,      1 },
122   { (char *)0,  "-mcpu=",       1,      1 },
123   { (char *)0,  "-mtune=",      1,      0 },
124   { 0, 0 }
125 };
126
127 /* CPU type.  This is set from TARGET_CPU_DEFAULT and -m{cpu,tune}=xxx.  */
128 enum processor_type sparc_cpu;
129
130 /* Validate and override various options, and do some machine dependent
131    initialization.  */
132
133 void
134 sparc_override_options ()
135 {
136   /* Map TARGET_CPU_DEFAULT to value for -m{arch,tune}=.  */
137   static struct cpu_default {
138     int cpu;
139     char *name;
140   } cpu_default[] = {
141     { TARGET_CPU_sparc, "cypress" },
142     { TARGET_CPU_v8, "v8" },
143     { TARGET_CPU_supersparc, "supersparc" },
144     { TARGET_CPU_sparclet, "tsc701" },
145     { TARGET_CPU_sparclite, "f930" },
146     { TARGET_CPU_ultrasparc, "ultrasparc" },
147     { 0 }
148   };
149   struct cpu_default *def;
150   /* Table of values for -m{cpu,tune}=.  */
151   static struct cpu_table {
152     char *name;
153     enum processor_type processor;
154     int disable;
155     int enable;
156   } cpu_table[] = {
157     { "v7",         PROCESSOR_V7, MASK_ISA, 0 },
158     { "cypress",    PROCESSOR_CYPRESS, MASK_ISA, 0 },
159     { "v8",         PROCESSOR_V8, MASK_ISA, MASK_V8 },
160     /* TI TMS390Z55 supersparc */
161     { "supersparc", PROCESSOR_SUPERSPARC, MASK_ISA, MASK_V8 },
162     { "sparclite",  PROCESSOR_SPARCLITE, MASK_ISA, MASK_SPARCLITE },
163     /* The Fujitsu MB86930 is the original sparclite chip, with no fpu.
164        The Fujitsu MB86934 is the recent sparclite chip, with an fpu.  */
165     { "f930",       PROCESSOR_F930, MASK_ISA|MASK_FPU, MASK_SPARCLITE },
166     { "f934",       PROCESSOR_F934, MASK_ISA, MASK_SPARCLITE|MASK_FPU },
167     { "sparclet",   PROCESSOR_SPARCLET, MASK_ISA, MASK_SPARCLET },
168     /* TEMIC sparclet */
169     { "tsc701",     PROCESSOR_TSC701, MASK_ISA, MASK_SPARCLET },
170     /* "v9" is used to specify a true 64 bit architecture.
171        "v8plus" is what Sun calls Solaris2 running on UltraSPARC's.  */
172     { "v8plus",     PROCESSOR_V8PLUS, MASK_ISA, MASK_V9 },
173 #if SPARC_ARCH64
174     { "v9",         PROCESSOR_V9, MASK_ISA, MASK_V9 },
175 #endif
176     /* TI ultrasparc */
177     { "ultrasparc", PROCESSOR_ULTRASPARC, MASK_ISA, MASK_V9 },
178     { 0 }
179   };
180   struct cpu_table *cpu;
181   struct sparc_cpu_select *sel;
182
183   int fpu = TARGET_FPU; /* save current -mfpu status */
184
185   /* Set the default.  */
186   for (def = &cpu_default[0]; def->name; ++def)
187     if (def->cpu == TARGET_CPU_DEFAULT)
188       break;
189   if (! def->name)
190     abort ();
191   sparc_select[0].string = def->name;
192
193   for (sel = &sparc_select[0]; sel->name; ++sel)
194     {
195       if (sel->string)
196         {
197           for (cpu = &cpu_table[0]; cpu->name; ++cpu)
198             if (! strcmp (sel->string, cpu->name))
199               {
200                 if (sel->set_tune_p)
201                   sparc_cpu = cpu->processor;
202
203                 if (sel->set_arch_p)
204                   {
205                     target_flags &= ~cpu->disable;
206                     target_flags |= cpu->enable;
207                   }
208                 break;
209               }
210
211           if (! cpu->name)
212             error ("bad value (%s) for %s switch", sel->string, sel->name);
213         }
214     }
215
216   /* If -mfpu or -mno-fpu was explicitly used, don't override with
217      the processor default.  */
218   if (TARGET_FPU_SET)
219     target_flags = (target_flags & ~MASK_FPU) | fpu;
220
221   /* Use the deprecated v8 insns for sparc64 in 32 bit mode.  */
222   if (TARGET_V9 && TARGET_ARCH32)
223     target_flags |= MASK_DEPRECATED_V8_INSNS;
224
225   /* Validate -malign-loops= value, or provide default.  */
226   if (sparc_align_loops_string)
227     {
228       sparc_align_loops = exact_log2 (atoi (sparc_align_loops_string));
229       if (sparc_align_loops < 2 || sparc_align_loops > 7)
230         fatal ("-malign-loops=%s is not between 4 and 128 or is not a power of two",
231                sparc_align_loops_string);
232     }
233   else
234     {
235       /* ??? This relies on ASM_OUTPUT_ALIGN to not emit the alignment if
236          its 0.  This sounds a bit kludgey.  */
237       sparc_align_loops = 0;
238     }
239
240   /* Validate -malign-jumps= value, or provide default.  */
241   if (sparc_align_jumps_string)
242     {
243       sparc_align_jumps = exact_log2 (atoi (sparc_align_jumps_string));
244       if (sparc_align_jumps < 2 || sparc_align_loops > 7)
245         fatal ("-malign-jumps=%s is not between 4 and 128 or is not a power of two",
246                sparc_align_jumps_string);
247     }
248   else
249     {
250       /* ??? This relies on ASM_OUTPUT_ALIGN to not emit the alignment if
251          its 0.  This sounds a bit kludgey.  */
252       sparc_align_jumps = 0;
253     }
254
255   /* Validate -malign-functions= value, or provide default. */
256   if (sparc_align_funcs_string)
257     {
258       sparc_align_funcs = exact_log2 (atoi (sparc_align_funcs_string));
259       if (sparc_align_funcs < 2 || sparc_align_loops > 7)
260         fatal ("-malign-functions=%s is not between 4 and 128 or is not a power of two",
261                sparc_align_funcs_string);
262     }
263   else
264     sparc_align_funcs = DEFAULT_SPARC_ALIGN_FUNCS;
265
266   /* Do various machine dependent initializations.  */
267   sparc_init_modes ();
268 }
269 \f
270 /* Float conversions (v9 only).
271
272    The floating point registers cannot hold DImode values because SUBREG's
273    on them get the wrong register.   "(subreg:SI (reg:DI M int-reg) 0)" is the
274    same as "(subreg:SI (reg:DI N float-reg) 1)", but gcc doesn't know how to
275    turn the "0" to a "1".  Therefore, we must explicitly do the conversions
276    to/from int/fp regs.  `sparc64_fpconv_stack_slot' is the address of an
277    8 byte stack slot used during the transfer.
278    ??? I could have used [%fp-16] but I didn't want to add yet another
279    dependence on this.  */
280 /* ??? Can we use assign_stack_temp here?  */
281
282 static rtx fpconv_stack_temp;
283
284 /* Called once for each function.  */
285
286 void
287 sparc64_init_expanders ()
288 {
289   fpconv_stack_temp = NULL_RTX;
290 }
291
292 /* Assign a stack temp for fp/int DImode conversions.  */
293
294 rtx
295 sparc64_fpconv_stack_temp ()
296 {
297   if (fpconv_stack_temp == NULL_RTX)
298       fpconv_stack_temp =
299         assign_stack_local (DImode, GET_MODE_SIZE (DImode), 0);
300
301     return fpconv_stack_temp;
302 }
303 \f
304 /* Miscellaneous utilities.  */
305
306 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
307    or branch on register contents instructions.  */
308
309 int
310 v9_regcmp_p (code)
311      enum rtx_code code;
312 {
313   return (code == EQ || code == NE || code == GE || code == LT
314           || code == LE || code == GT);
315 }
316 \f
317 /* Operand constraints.  */
318
319 /* Return non-zero only if OP is a register of mode MODE,
320    or const0_rtx.  Don't allow const0_rtx if TARGET_LIVE_G0 because
321    %g0 may contain anything.  */
322
323 int
324 reg_or_0_operand (op, mode)
325      rtx op;
326      enum machine_mode mode;
327 {
328   if (register_operand (op, mode))
329     return 1;
330   if (TARGET_LIVE_G0)
331     return 0;
332   if (op == const0_rtx)
333     return 1;
334   if (GET_MODE (op) == VOIDmode && GET_CODE (op) == CONST_DOUBLE
335       && CONST_DOUBLE_HIGH (op) == 0
336       && CONST_DOUBLE_LOW (op) == 0)
337     return 1;
338   if (GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
339       && GET_CODE (op) == CONST_DOUBLE
340       && fp_zero_operand (op))
341     return 1;
342   return 0;
343 }
344
345 /* Nonzero if OP is a floating point value with value 0.0.  */
346
347 int
348 fp_zero_operand (op)
349      rtx op;
350 {
351   REAL_VALUE_TYPE r;
352
353   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
354   return (REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r));
355 }
356
357 /* Nonzero if OP is an integer register.  */
358
359 int
360 intreg_operand (op, mode)
361      rtx op;
362      enum machine_mode mode;
363 {
364   return (register_operand (op, SImode)
365           || (TARGET_ARCH64 && register_operand (op, DImode)));
366 }
367
368 /* Nonzero if OP is a floating point condition code register.  */
369
370 int
371 fcc_reg_operand (op, mode)
372      rtx op;
373      enum machine_mode mode;
374 {
375   /* This can happen when recog is called from combine.  Op may be a MEM.
376      Fail instead of calling abort in this case.  */
377   if (GET_CODE (op) != REG)
378     return 0;
379
380   if (mode != VOIDmode && mode != GET_MODE (op))
381     return 0;
382   if (mode == VOIDmode
383       && (GET_MODE (op) != CCFPmode && GET_MODE (op) != CCFPEmode))
384     return 0;
385
386 #if 0   /* ??? ==> 1 when %fcc0-3 are pseudos first.  See gen_compare_reg().  */
387   if (reg_renumber == 0)
388     return REGNO (op) >= FIRST_PSEUDO_REGISTER;
389   return REGNO_OK_FOR_CCFP_P (REGNO (op));
390 #else
391   return (unsigned) REGNO (op) - SPARC_FIRST_V9_FCC_REG < 4;
392 #endif
393 }
394
395 /* Nonzero if OP is an integer or floating point condition code register.  */
396
397 int
398 icc_or_fcc_reg_operand (op, mode)
399      rtx op;
400      enum machine_mode mode;
401 {
402   if (GET_CODE (op) == REG && REGNO (op) == SPARC_ICC_REG)
403     {
404       if (mode != VOIDmode && mode != GET_MODE (op))
405         return 0;
406       if (mode == VOIDmode
407           && GET_MODE (op) != CCmode && GET_MODE (op) != CCXmode)
408         return 0;
409       return 1;
410     }
411
412   return fcc_reg_operand (op, mode);
413 }
414
415 /* Nonzero if OP can appear as the dest of a RESTORE insn.  */
416 int
417 restore_operand (op, mode)
418      rtx op;
419      enum machine_mode mode;
420 {
421   return (GET_CODE (op) == REG && GET_MODE (op) == mode
422           && (REGNO (op) < 8 || (REGNO (op) >= 24 && REGNO (op) < 32)));
423 }
424
425 /* Call insn on SPARC can take a PC-relative constant address, or any regular
426    memory address.  */
427
428 int
429 call_operand (op, mode)
430      rtx op;
431      enum machine_mode mode;
432 {
433   if (GET_CODE (op) != MEM)
434     abort ();
435   op = XEXP (op, 0);
436   return (symbolic_operand (op, mode) || memory_address_p (Pmode, op));
437 }
438
439 int
440 call_operand_address (op, mode)
441      rtx op;
442      enum machine_mode mode;
443 {
444   return (symbolic_operand (op, mode) || memory_address_p (Pmode, op));
445 }
446
447 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
448    reference and a constant.  */
449
450 int
451 symbolic_operand (op, mode)
452      register rtx op;
453      enum machine_mode mode;
454 {
455   switch (GET_CODE (op))
456     {
457     case SYMBOL_REF:
458     case LABEL_REF:
459       return 1;
460
461     case CONST:
462       op = XEXP (op, 0);
463       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
464                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
465               && GET_CODE (XEXP (op, 1)) == CONST_INT);
466
467       /* ??? This clause seems to be irrelevant.  */
468     case CONST_DOUBLE:
469       return GET_MODE (op) == mode;
470
471     default:
472       return 0;
473     }
474 }
475
476 /* Return truth value of statement that OP is a symbolic memory
477    operand of mode MODE.  */
478
479 int
480 symbolic_memory_operand (op, mode)
481      rtx op;
482      enum machine_mode mode;
483 {
484   if (GET_CODE (op) == SUBREG)
485     op = SUBREG_REG (op);
486   if (GET_CODE (op) != MEM)
487     return 0;
488   op = XEXP (op, 0);
489   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
490           || GET_CODE (op) == HIGH || GET_CODE (op) == LABEL_REF);
491 }
492
493 /* Return truth value of statement that OP is a LABEL_REF of mode MODE.  */
494
495 int
496 label_ref_operand (op, mode)
497      rtx op;
498      enum machine_mode mode;
499 {
500   if (GET_CODE (op) != LABEL_REF)
501     return 0;
502   if (GET_MODE (op) != mode)
503     return 0;
504   return 1;
505 }
506
507 /* Return 1 if the operand is an argument used in generating pic references
508    in either the medium/low or medium/anywhere code models of sparc64.  */
509
510 int
511 sp64_medium_pic_operand (op, mode)
512      rtx op;
513      enum machine_mode mode;
514 {
515   /* Check for (const (minus (symbol_ref:GOT)
516                              (const (minus (label) (pc))))).  */
517   if (GET_CODE (op) != CONST)
518     return 0;
519   op = XEXP (op, 0);
520   if (GET_CODE (op) != MINUS)
521     return 0;
522   if (GET_CODE (XEXP (op, 0)) != SYMBOL_REF)
523     return 0;
524   /* ??? Ensure symbol is GOT.  */
525   if (GET_CODE (XEXP (op, 1)) != CONST)
526     return 0;
527   if (GET_CODE (XEXP (XEXP (op, 1), 0)) != MINUS)
528     return 0;
529   return 1;
530 }
531
532 /* Return 1 if the operand is a data segment reference.  This includes
533    the readonly data segment, or in other words anything but the text segment.
534    This is needed in the medium/anywhere code model on v9.  These values
535    are accessed with MEDANY_BASE_REG.  */
536
537 int
538 data_segment_operand (op, mode)
539      rtx op;
540      enum machine_mode mode;
541 {
542   switch (GET_CODE (op))
543     {
544     case SYMBOL_REF :
545       return ! SYMBOL_REF_FLAG (op);
546     case PLUS :
547       /* Assume canonical format of symbol + constant.
548          Fall through.  */
549     case CONST :
550       return data_segment_operand (XEXP (op, 0));
551     default :
552       return 0;
553     }
554 }
555
556 /* Return 1 if the operand is a text segment reference.
557    This is needed in the medium/anywhere code model on v9.  */
558
559 int
560 text_segment_operand (op, mode)
561      rtx op;
562      enum machine_mode mode;
563 {
564   switch (GET_CODE (op))
565     {
566     case LABEL_REF :
567       return 1;
568     case SYMBOL_REF :
569       return SYMBOL_REF_FLAG (op);
570     case PLUS :
571       /* Assume canonical format of symbol + constant.
572          Fall through.  */
573     case CONST :
574       return text_segment_operand (XEXP (op, 0));
575     default :
576       return 0;
577     }
578 }
579
580 /* Return 1 if the operand is either a register or a memory operand that is
581    not symbolic.  */
582
583 int
584 reg_or_nonsymb_mem_operand (op, mode)
585     register rtx op;
586     enum machine_mode mode;
587 {
588   if (register_operand (op, mode))
589     return 1;
590
591   if (memory_operand (op, mode) && ! symbolic_memory_operand (op, mode))
592     return 1;
593
594   return 0;
595 }
596
597 int
598 sparc_operand (op, mode)
599      rtx op;
600      enum machine_mode mode;
601 {
602   if (register_operand (op, mode))
603     return 1;
604   if (GET_CODE (op) == CONST_INT)
605     return SMALL_INT (op);
606   if (GET_MODE (op) != mode)
607     return 0;
608   if (GET_CODE (op) == SUBREG)
609     op = SUBREG_REG (op);
610   if (GET_CODE (op) != MEM)
611     return 0;
612
613   op = XEXP (op, 0);
614   if (GET_CODE (op) == LO_SUM)
615     return (GET_CODE (XEXP (op, 0)) == REG
616             && symbolic_operand (XEXP (op, 1), Pmode));
617   return memory_address_p (mode, op);
618 }
619
620 int
621 move_operand (op, mode)
622      rtx op;
623      enum machine_mode mode;
624 {
625   if (mode == DImode && arith_double_operand (op, mode))
626     return 1;
627   if (register_operand (op, mode))
628     return 1;
629   if (GET_CODE (op) == CONST_INT)
630     return SMALL_INT (op) || SPARC_SETHI_P (INTVAL (op));
631
632   if (GET_MODE (op) != mode)
633     return 0;
634   if (GET_CODE (op) == SUBREG)
635     op = SUBREG_REG (op);
636   if (GET_CODE (op) != MEM)
637     return 0;
638   op = XEXP (op, 0);
639   if (GET_CODE (op) == LO_SUM)
640     return (register_operand (XEXP (op, 0), Pmode)
641             && CONSTANT_P (XEXP (op, 1)));
642   return memory_address_p (mode, op);
643 }
644
645 int
646 splittable_symbolic_memory_operand (op, mode)
647      rtx op;
648      enum machine_mode mode;
649 {
650   if (GET_CODE (op) != MEM)
651     return 0;
652   if (! symbolic_operand (XEXP (op, 0), Pmode))
653     return 0;
654   return 1;
655 }
656
657 int
658 splittable_immediate_memory_operand (op, mode)
659      rtx op;
660      enum machine_mode mode;
661 {
662   if (GET_CODE (op) != MEM)
663     return 0;
664   if (! immediate_operand (XEXP (op, 0), Pmode))
665     return 0;
666   return 1;
667 }
668
669 /* Return truth value of whether OP is EQ or NE.  */
670
671 int
672 eq_or_neq (op, mode)
673      rtx op;
674      enum machine_mode mode;
675 {
676   return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
677 }
678
679 /* Return 1 if this is a comparison operator, but not an EQ, NE, GEU,
680    or LTU for non-floating-point.  We handle those specially.  */
681
682 int
683 normal_comp_operator (op, mode)
684      rtx op;
685      enum machine_mode mode;
686 {
687   enum rtx_code code = GET_CODE (op);
688
689   if (GET_RTX_CLASS (code) != '<')
690     return 0;
691
692   if (GET_MODE (XEXP (op, 0)) == CCFPmode
693       || GET_MODE (XEXP (op, 0)) == CCFPEmode)
694     return 1;
695
696   return (code != NE && code != EQ && code != GEU && code != LTU);
697 }
698
699 /* Return 1 if this is a comparison operator.  This allows the use of
700    MATCH_OPERATOR to recognize all the branch insns.  */
701
702 int
703 noov_compare_op (op, mode)
704     register rtx op;
705     enum machine_mode mode;
706 {
707   enum rtx_code code = GET_CODE (op);
708
709   if (GET_RTX_CLASS (code) != '<')
710     return 0;
711
712   if (GET_MODE (XEXP (op, 0)) == CC_NOOVmode)
713     /* These are the only branches which work with CC_NOOVmode.  */
714     return (code == EQ || code == NE || code == GE || code == LT);
715   return 1;
716 }
717
718 /* Nonzero if OP is a comparison operator suitable for use in v9
719    conditional move or branch on register contents instructions.  */
720
721 int
722 v9_regcmp_op (op, mode)
723      register rtx op;
724      enum machine_mode mode;
725 {
726   enum rtx_code code = GET_CODE (op);
727
728   if (GET_RTX_CLASS (code) != '<')
729     return 0;
730
731   return v9_regcmp_p (code);
732 }
733
734 /* Return 1 if this is a SIGN_EXTEND or ZERO_EXTEND operation.  */
735
736 int
737 extend_op (op, mode)
738      rtx op;
739      enum machine_mode mode;
740 {
741   return GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND;
742 }
743
744 /* Return nonzero if OP is an operator of mode MODE which can set
745    the condition codes explicitly.  We do not include PLUS and MINUS
746    because these require CC_NOOVmode, which we handle explicitly.  */
747
748 int
749 cc_arithop (op, mode)
750      rtx op;
751      enum machine_mode mode;
752 {
753   if (GET_CODE (op) == AND
754       || GET_CODE (op) == IOR
755       || GET_CODE (op) == XOR)
756     return 1;
757
758   return 0;
759 }
760
761 /* Return nonzero if OP is an operator of mode MODE which can bitwise
762    complement its second operand and set the condition codes explicitly.  */
763
764 int
765 cc_arithopn (op, mode)
766      rtx op;
767      enum machine_mode mode;
768 {
769   /* XOR is not here because combine canonicalizes (xor (not ...) ...)
770      and (xor ... (not ...)) to (not (xor ...)).   */
771   return (GET_CODE (op) == AND
772           || GET_CODE (op) == IOR);
773 }
774 \f
775 /* Return true if OP is a register, or is a CONST_INT that can fit in a
776    signed 13 bit immediate field.  This is an acceptable SImode operand for
777    most 3 address instructions.  */
778
779 int
780 arith_operand (op, mode)
781      rtx op;
782      enum machine_mode mode;
783 {
784   return (register_operand (op, mode)
785           || (GET_CODE (op) == CONST_INT && SMALL_INT (op)));
786 }
787
788 /* Return true if OP is a register, or is a CONST_INT that can fit in a
789    signed 11 bit immediate field.  This is an acceptable SImode operand for
790    the movcc instructions.  */
791
792 int
793 arith11_operand (op, mode)
794      rtx op;
795      enum machine_mode mode;
796 {
797   return (register_operand (op, mode)
798           || (GET_CODE (op) == CONST_INT && SPARC_SIMM11_P (INTVAL (op))));
799 }
800
801 /* Return true if OP is a register, or is a CONST_INT that can fit in a
802    signed 10 bit immediate field.  This is an acceptable SImode operand for
803    the movrcc instructions.  */
804
805 int
806 arith10_operand (op, mode)
807      rtx op;
808      enum machine_mode mode;
809 {
810   return (register_operand (op, mode)
811           || (GET_CODE (op) == CONST_INT && SPARC_SIMM10_P (INTVAL (op))));
812 }
813
814 /* Return true if OP is a register, is a CONST_INT that fits in a 13 bit
815    immediate field, or is a CONST_DOUBLE whose both parts fit in a 13 bit
816    immediate field.
817    v9: Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
818    can fit in a 13 bit immediate field.  This is an acceptable DImode operand
819    for most 3 address instructions.  */
820
821 int
822 arith_double_operand (op, mode)
823      rtx op;
824      enum machine_mode mode;
825 {
826   return (register_operand (op, mode)
827           || (GET_CODE (op) == CONST_INT && SMALL_INT (op))
828           || (! TARGET_ARCH64
829               && GET_CODE (op) == CONST_DOUBLE
830               && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x1000) < 0x2000
831               && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_HIGH (op) + 0x1000) < 0x2000)
832           || (TARGET_ARCH64
833               && GET_CODE (op) == CONST_DOUBLE
834               && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x1000) < 0x2000
835               && ((CONST_DOUBLE_HIGH (op) == -1
836                    && (CONST_DOUBLE_LOW (op) & 0x1000) == 0x1000)
837                   || (CONST_DOUBLE_HIGH (op) == 0
838                       && (CONST_DOUBLE_LOW (op) & 0x1000) == 0))));
839 }
840
841 /* Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
842    can fit in an 11 bit immediate field.  This is an acceptable DImode
843    operand for the movcc instructions.  */
844 /* ??? Replace with arith11_operand?  */
845
846 int
847 arith11_double_operand (op, mode)
848      rtx op;
849      enum machine_mode mode;
850 {
851   return (register_operand (op, mode)
852           || (GET_CODE (op) == CONST_DOUBLE
853               && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
854               && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x400) < 0x800
855               && ((CONST_DOUBLE_HIGH (op) == -1
856                    && (CONST_DOUBLE_LOW (op) & 0x400) == 0x400)
857                   || (CONST_DOUBLE_HIGH (op) == 0
858                       && (CONST_DOUBLE_LOW (op) & 0x400) == 0)))
859           || (GET_CODE (op) == CONST_INT
860               && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
861               && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x400) < 0x800));
862 }
863
864 /* Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
865    can fit in an 10 bit immediate field.  This is an acceptable DImode
866    operand for the movrcc instructions.  */
867 /* ??? Replace with arith10_operand?  */
868
869 int
870 arith10_double_operand (op, mode)
871      rtx op;
872      enum machine_mode mode;
873 {
874   return (register_operand (op, mode)
875           || (GET_CODE (op) == CONST_DOUBLE
876               && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
877               && (unsigned) (CONST_DOUBLE_LOW (op) + 0x200) < 0x400
878               && ((CONST_DOUBLE_HIGH (op) == -1
879                    && (CONST_DOUBLE_LOW (op) & 0x200) == 0x200)
880                   || (CONST_DOUBLE_HIGH (op) == 0
881                       && (CONST_DOUBLE_LOW (op) & 0x200) == 0)))
882           || (GET_CODE (op) == CONST_INT
883               && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
884               && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x200) < 0x400));
885 }
886
887 /* Return truth value of whether OP is a integer which fits the
888    range constraining immediate operands in most three-address insns,
889    which have a 13 bit immediate field.  */
890
891 int
892 small_int (op, mode)
893      rtx op;
894      enum machine_mode mode;
895 {
896   return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
897 }
898
899 /* Recognize operand values for the umul instruction.  That instruction sign
900    extends immediate values just like all other sparc instructions, but
901    interprets the extended result as an unsigned number.  */
902
903 int
904 uns_small_int (op, mode)
905      rtx op;
906      enum machine_mode mode;
907 {
908 #if HOST_BITS_PER_WIDE_INT > 32
909   /* All allowed constants will fit a CONST_INT.  */
910   return (GET_CODE (op) == CONST_INT
911           && ((INTVAL (op) >= 0 && INTVAL (op) < 0x1000)
912               || (INTVAL (op) >= 0xFFFFF000 && INTVAL (op) < 0x100000000L)));
913 #else
914   return ((GET_CODE (op) == CONST_INT && (unsigned) INTVAL (op) < 0x1000)
915           || (GET_CODE (op) == CONST_DOUBLE
916               && CONST_DOUBLE_HIGH (op) == 0
917               && (unsigned) CONST_DOUBLE_LOW (op) - 0xFFFFF000 < 0x1000));
918 #endif
919 }
920
921 int
922 uns_arith_operand (op, mode)
923      rtx op;
924      enum machine_mode mode;
925 {
926   return register_operand (op, mode) || uns_small_int (op, mode);
927 }
928
929 /* Return truth value of statement that OP is a call-clobbered register.  */
930 int
931 clobbered_register (op, mode)
932      rtx op;
933      enum machine_mode mode;
934 {
935   return (GET_CODE (op) == REG && call_used_regs[REGNO (op)]);
936 }
937 \f
938 /* X and Y are two things to compare using CODE.  Emit the compare insn and
939    return the rtx for the cc reg in the proper mode.  */
940
941 rtx
942 gen_compare_reg (code, x, y)
943      enum rtx_code code;
944      rtx x, y;
945 {
946   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
947   rtx cc_reg;
948
949   /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
950      fcc regs (cse can't tell they're really call clobbered regs and will
951      remove a duplicate comparison even if there is an intervening function
952      call - it will then try to reload the cc reg via an int reg which is why
953      we need the movcc patterns).  It is possible to provide the movcc
954      patterns by using the ldxfsr/stxfsr v9 insns.  I tried it: you need two
955      registers (say %g1,%g5) and it takes about 6 insns.  A better fix would be
956      to tell cse that CCFPE mode registers (even pseudos) are call
957      clobbered.  */
958
959   /* ??? This is an experiment.  Rather than making changes to cse which may
960      or may not be easy/clean, we do our own cse.  This is possible because
961      we will generate hard registers.  Cse knows they're call clobbered (it
962      doesn't know the same thing about pseudos). If we guess wrong, no big
963      deal, but if we win, great!  */
964
965   if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
966 #if 1 /* experiment */
967     {
968       int reg;
969       /* We cycle through the registers to ensure they're all exercised.  */
970       static int next_fcc_reg = 0;
971       /* Previous x,y for each fcc reg.  */
972       static rtx prev_args[4][2];
973
974       /* Scan prev_args for x,y.  */
975       for (reg = 0; reg < 4; reg++)
976         if (prev_args[reg][0] == x && prev_args[reg][1] == y)
977           break;
978       if (reg == 4)
979         {
980           reg = next_fcc_reg;
981           prev_args[reg][0] = x;
982           prev_args[reg][1] = y;
983           next_fcc_reg = (next_fcc_reg + 1) & 3;
984         }
985       cc_reg = gen_rtx (REG, mode, reg + SPARC_FIRST_V9_FCC_REG);
986     }
987 #else
988     cc_reg = gen_reg_rtx (mode);
989 #endif /* ! experiment */
990   else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
991     cc_reg = gen_rtx (REG, mode, SPARC_FCC_REG);
992   else
993     cc_reg = gen_rtx (REG, mode, SPARC_ICC_REG);
994
995   emit_insn (gen_rtx (SET, VOIDmode, cc_reg,
996                       gen_rtx (COMPARE, mode, x, y)));
997
998   return cc_reg;
999 }
1000
1001 /* This function is used for v9 only.
1002    CODE is the code for an Scc's comparison.
1003    OPERANDS[0] is the target of the Scc insn.
1004    OPERANDS[1] is the value we compare against const0_rtx (which hasn't
1005    been generated yet).
1006
1007    This function is needed to turn
1008
1009            (set (reg:SI 110)
1010                (gt (reg:CCX 100 %icc)
1011                    (const_int 0)))
1012    into
1013            (set (reg:SI 110)
1014                (gt:DI (reg:CCX 100 %icc)
1015                    (const_int 0)))
1016
1017    IE: The instruction recognizer needs to see the mode of the comparison to
1018    find the right instruction. We could use "gt:DI" right in the
1019    define_expand, but leaving it out allows us to handle DI, SI, etc.
1020
1021    We refer to the global sparc compare operands sparc_compare_op0 and
1022    sparc_compare_op1.  */
1023
1024 int
1025 gen_v9_scc (compare_code, operands)
1026      enum rtx_code compare_code;
1027      register rtx *operands;
1028 {
1029   rtx temp, op0, op1;
1030
1031   if (! TARGET_ARCH64
1032       && (GET_MODE (sparc_compare_op0) == DImode
1033           || GET_MODE (operands[0]) == DImode))
1034     return 0;
1035
1036   /* Handle the case where operands[0] == sparc_compare_op0.
1037      We "early clobber" the result.  */
1038   if (REGNO (operands[0]) == REGNO (sparc_compare_op0))
1039     {
1040       op0 = gen_reg_rtx (GET_MODE (sparc_compare_op0));
1041       emit_move_insn (op0, sparc_compare_op0);
1042     }
1043   else
1044     op0 = sparc_compare_op0;
1045   /* For consistency in the following.  */
1046   op1 = sparc_compare_op1;
1047
1048   /* Try to use the movrCC insns.  */
1049   if (TARGET_ARCH64
1050       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
1051       && op1 == const0_rtx
1052       && v9_regcmp_p (compare_code))
1053     {
1054       /* Special case for op0 != 0.  This can be done with one instruction if
1055          operands[0] == sparc_compare_op0.  We don't assume they are equal
1056          now though.  */
1057
1058       if (compare_code == NE
1059           && GET_MODE (operands[0]) == DImode
1060           && GET_MODE (op0) == DImode)
1061         {
1062           emit_insn (gen_rtx (SET, VOIDmode, operands[0], op0));
1063           emit_insn (gen_rtx (SET, VOIDmode, operands[0],
1064                               gen_rtx (IF_THEN_ELSE, DImode,
1065                                        gen_rtx (compare_code, DImode,
1066                                                 op0, const0_rtx),
1067                                        const1_rtx,
1068                                        operands[0])));
1069           return 1;
1070         }
1071
1072       emit_insn (gen_rtx (SET, VOIDmode, operands[0], const0_rtx));
1073       if (GET_MODE (op0) != DImode)
1074         {
1075           temp = gen_reg_rtx (DImode);
1076           convert_move (temp, op0, 0);
1077         }
1078       else
1079         temp = op0;
1080       emit_insn (gen_rtx (SET, VOIDmode, operands[0],
1081                           gen_rtx (IF_THEN_ELSE, GET_MODE (operands[0]),
1082                                    gen_rtx (compare_code, DImode,
1083                                             temp, const0_rtx),
1084                                    const1_rtx,
1085                                    operands[0])));
1086       return 1;
1087     }
1088   else
1089     {
1090       operands[1] = gen_compare_reg (compare_code, op0, op1);
1091
1092       switch (GET_MODE (operands[1]))
1093         {
1094           case CCmode :
1095           case CCXmode :
1096           case CCFPEmode :
1097           case CCFPmode :
1098             break;
1099           default :
1100             abort ();
1101         }
1102       emit_insn (gen_rtx (SET, VOIDmode, operands[0], const0_rtx));
1103       emit_insn (gen_rtx (SET, VOIDmode, operands[0],
1104                           gen_rtx (IF_THEN_ELSE, GET_MODE (operands[0]),
1105                                    gen_rtx (compare_code,
1106                                             GET_MODE (operands[1]),
1107                                             operands[1], const0_rtx),
1108                                     const1_rtx, operands[0])));
1109       return 1;
1110     }
1111 }
1112
1113 /* Emit a conditional jump insn for the v9 architecture using comparison code
1114    CODE and jump target LABEL.
1115    This function exists to take advantage of the v9 brxx insns.  */
1116
1117 void
1118 emit_v9_brxx_insn (code, op0, label)
1119      enum rtx_code code;
1120      rtx op0, label;
1121 {
1122   emit_jump_insn (gen_rtx (SET, VOIDmode,
1123                            pc_rtx,
1124                            gen_rtx (IF_THEN_ELSE, VOIDmode,
1125                                     gen_rtx (code, GET_MODE (op0),
1126                                              op0, const0_rtx),
1127                                     gen_rtx (LABEL_REF, VOIDmode, label),
1128                                     pc_rtx)));
1129 }
1130 \f
1131 /* Return nonzero if a return peephole merging return with
1132    setting of output register is ok.  */
1133 int
1134 leaf_return_peephole_ok ()
1135 {
1136   return (actual_fsize == 0);
1137 }
1138
1139 /* Return nonzero if TRIAL can go into the function epilogue's
1140    delay slot.  SLOT is the slot we are trying to fill.  */
1141
1142 int
1143 eligible_for_epilogue_delay (trial, slot)
1144      rtx trial;
1145      int slot;
1146 {
1147   rtx pat, src;
1148
1149   if (slot >= 1)
1150     return 0;
1151
1152   if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
1153     return 0;
1154
1155   if (get_attr_length (trial) != 1)
1156     return 0;
1157
1158   /* If %g0 is live, there are lots of things we can't handle.
1159      Rather than trying to find them all now, let's punt and only
1160      optimize things as necessary.  */
1161   if (TARGET_LIVE_G0)
1162     return 0;
1163
1164   /* In the case of a true leaf function, anything can go into the delay slot.
1165      A delay slot only exists however if the frame size is zero, otherwise
1166      we will put an insn to adjust the stack after the return.  */
1167   if (leaf_function)
1168     {
1169       if (leaf_return_peephole_ok ())
1170         return ((get_attr_in_uncond_branch_delay (trial)
1171                  == IN_BRANCH_DELAY_TRUE));
1172       return 0;
1173     }
1174
1175   /* If only trivial `restore' insns work, nothing can go in the
1176      delay slot.  */
1177   else if (TARGET_BROKEN_SAVERESTORE)
1178     return 0;
1179
1180   pat = PATTERN (trial);
1181
1182   /* Otherwise, only operations which can be done in tandem with
1183      a `restore' insn can go into the delay slot.  */
1184   if (GET_CODE (SET_DEST (pat)) != REG
1185       || REGNO (SET_DEST (pat)) >= 32
1186       || REGNO (SET_DEST (pat)) < 24)
1187     return 0;
1188
1189   /* The set of insns matched here must agree precisely with the set of
1190      patterns paired with a RETURN in sparc.md.  */
1191
1192   src = SET_SRC (pat);
1193
1194   /* This matches "*return_[qhs]".  */
1195   if (arith_operand (src, GET_MODE (src)))
1196     return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
1197     
1198   /* This matches "*return_di".  */
1199   else if (arith_double_operand (src, GET_MODE (src)))
1200     return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
1201
1202   /* This matches "*return_sf_no_fpu".  */
1203   else if (! TARGET_FPU && restore_operand (SET_DEST (pat), SFmode)
1204            && register_operand (src, SFmode))
1205     return 1;
1206
1207   /* This matches "*return_addsi".  */
1208   else if (GET_CODE (src) == PLUS
1209            && arith_operand (XEXP (src, 0), SImode)
1210            && arith_operand (XEXP (src, 1), SImode)
1211            && (register_operand (XEXP (src, 0), SImode)
1212                || register_operand (XEXP (src, 1), SImode)))
1213     return 1;
1214
1215   /* This matches "*return_adddi".  */
1216   else if (GET_CODE (src) == PLUS
1217            && arith_double_operand (XEXP (src, 0), DImode)
1218            && arith_double_operand (XEXP (src, 1), DImode)
1219            && (register_operand (XEXP (src, 0), DImode)
1220                || register_operand (XEXP (src, 1), DImode)))
1221     return 1;
1222
1223   /* This matches "*return_subsi".  */
1224   else if (GET_CODE (src) == MINUS
1225       && register_operand (XEXP (src, 0), SImode)
1226       && small_int (XEXP (src, 1), VOIDmode)
1227       && INTVAL (XEXP (src, 1)) != -4096)
1228     return 1;
1229
1230   return 0;
1231 }
1232
1233 int
1234 short_branch (uid1, uid2)
1235      int uid1, uid2;
1236 {
1237   unsigned int delta = insn_addresses[uid1] - insn_addresses[uid2];
1238   if (delta + 1024 < 2048)
1239     return 1;
1240   /* warning ("long branch, distance %d", delta); */
1241   return 0;
1242 }
1243
1244 /* Return non-zero if REG is not used after INSN.
1245    We assume REG is a reload reg, and therefore does
1246    not live past labels or calls or jumps.  */
1247 int
1248 reg_unused_after (reg, insn)
1249      rtx reg;
1250      rtx insn;
1251 {
1252   enum rtx_code code, prev_code = UNKNOWN;
1253
1254   while (insn = NEXT_INSN (insn))
1255     {
1256       if (prev_code == CALL_INSN && call_used_regs[REGNO (reg)])
1257         return 1;
1258
1259       code = GET_CODE (insn);
1260       if (GET_CODE (insn) == CODE_LABEL)
1261         return 1;
1262
1263       if (GET_RTX_CLASS (code) == 'i')
1264         {
1265           rtx set = single_set (insn);
1266           int in_src = set && reg_overlap_mentioned_p (reg, SET_SRC (set));
1267           if (set && in_src)
1268             return 0;
1269           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1270             return 1;
1271           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
1272             return 0;
1273         }
1274       prev_code = code;
1275     }
1276   return 1;
1277 }
1278 \f
1279 /* The rtx for the global offset table which is a special form
1280    that *is* a position independent symbolic constant.  */
1281 static rtx pic_pc_rtx;
1282
1283 /* Ensure that we are not using patterns that are not OK with PIC.  */
1284
1285 int
1286 check_pic (i)
1287      int i;
1288 {
1289   switch (flag_pic)
1290     {
1291     case 1:
1292       if (GET_CODE (recog_operand[i]) == SYMBOL_REF
1293           || (GET_CODE (recog_operand[i]) == CONST
1294               && ! rtx_equal_p (pic_pc_rtx, recog_operand[i])))
1295         abort ();
1296     case 2:
1297     default:
1298       return 1;
1299     }
1300 }
1301
1302 /* Return true if X is an address which needs a temporary register when 
1303    reloaded while generating PIC code.  */
1304
1305 int
1306 pic_address_needs_scratch (x)
1307      rtx x;
1308 {
1309   /* An address which is a symbolic plus a non SMALL_INT needs a temp reg.  */
1310   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
1311       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1312       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1313       && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
1314     return 1;
1315
1316   return 0;
1317 }
1318
1319 /* Legitimize PIC addresses.  If the address is already position-independent,
1320    we return ORIG.  Newly generated position-independent addresses go into a
1321    reg.  This is REG if non zero, otherwise we allocate register(s) as
1322    necessary.  */
1323
1324 rtx
1325 legitimize_pic_address (orig, mode, reg)
1326      rtx orig;
1327      enum machine_mode mode;
1328      rtx reg;
1329 {
1330   if (GET_CODE (orig) == SYMBOL_REF)
1331     {
1332       rtx pic_ref, address;
1333       rtx insn;
1334
1335       if (reg == 0)
1336         {
1337           if (reload_in_progress || reload_completed)
1338             abort ();
1339           else
1340             reg = gen_reg_rtx (Pmode);
1341         }
1342
1343       if (flag_pic == 2)
1344         {
1345           /* If not during reload, allocate another temp reg here for loading
1346              in the address, so that these instructions can be optimized
1347              properly.  */
1348           rtx temp_reg = ((reload_in_progress || reload_completed)
1349                           ? reg : gen_reg_rtx (Pmode));
1350
1351           /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
1352              won't get confused into thinking that these two instructions
1353              are loading in the true address of the symbol.  If in the
1354              future a PIC rtx exists, that should be used instead.  */
1355           emit_insn (gen_pic_sethi_si (temp_reg, orig));
1356           emit_insn (gen_pic_lo_sum_si (temp_reg, temp_reg, orig));
1357
1358           address = temp_reg;
1359         }
1360       else
1361         address = orig;
1362
1363       pic_ref = gen_rtx (MEM, Pmode,
1364                          gen_rtx (PLUS, Pmode,
1365                                   pic_offset_table_rtx, address));
1366       current_function_uses_pic_offset_table = 1;
1367       RTX_UNCHANGING_P (pic_ref) = 1;
1368       insn = emit_move_insn (reg, pic_ref);
1369       /* Put a REG_EQUAL note on this insn, so that it can be optimized
1370          by loop.  */
1371       REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL, orig,
1372                                   REG_NOTES (insn));
1373       return reg;
1374     }
1375   else if (GET_CODE (orig) == CONST)
1376     {
1377       rtx base, offset;
1378
1379       if (GET_CODE (XEXP (orig, 0)) == PLUS
1380           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1381         return orig;
1382
1383       if (reg == 0)
1384         {
1385           if (reload_in_progress || reload_completed)
1386             abort ();
1387           else
1388             reg = gen_reg_rtx (Pmode);
1389         }
1390
1391       if (GET_CODE (XEXP (orig, 0)) == PLUS)
1392         {
1393           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1394           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1395                                          base == reg ? 0 : reg);
1396         }
1397       else
1398         abort ();
1399
1400       if (GET_CODE (offset) == CONST_INT)
1401         {
1402           if (SMALL_INT (offset))
1403             return plus_constant_for_output (base, INTVAL (offset));
1404           else if (! reload_in_progress && ! reload_completed)
1405             offset = force_reg (Pmode, offset);
1406           else
1407             /* If we reach here, then something is seriously wrong.  */
1408             abort ();
1409         }
1410       return gen_rtx (PLUS, Pmode, base, offset);
1411     }
1412   else if (GET_CODE (orig) == LABEL_REF)
1413     /* ??? Why do we do this?  */
1414     current_function_uses_pic_offset_table = 1;
1415
1416   return orig;
1417 }
1418
1419 /* Set up PIC-specific rtl.  This should not cause any insns
1420    to be emitted.  */
1421
1422 void
1423 initialize_pic ()
1424 {
1425 }
1426
1427 /* Emit special PIC prologues and epilogues.  */
1428
1429 void
1430 finalize_pic ()
1431 {
1432   /* The table we use to reference PIC data.  */
1433   rtx global_offset_table;
1434   /* Labels to get the PC in the prologue of this function.  */
1435   rtx l1, l2;
1436   rtx seq;
1437   int orig_flag_pic = flag_pic;
1438
1439   if (current_function_uses_pic_offset_table == 0)
1440     return;
1441
1442   if (! flag_pic)
1443     abort ();
1444
1445   flag_pic = 0;
1446
1447   start_sequence ();
1448
1449   l1 = gen_label_rtx ();
1450
1451   /* Initialize every time through, since we can't easily
1452      know this to be permanent.  */
1453   global_offset_table = gen_rtx (SYMBOL_REF, Pmode, "_GLOBAL_OFFSET_TABLE_");
1454   pic_pc_rtx = gen_rtx (CONST, Pmode,
1455                         gen_rtx (MINUS, Pmode,
1456                                  global_offset_table,
1457                                  gen_rtx (CONST, Pmode,
1458                                           gen_rtx (MINUS, Pmode,
1459                                                    gen_rtx (LABEL_REF, VOIDmode, l1),
1460                                                    pc_rtx))));
1461
1462   /* sparc64: the RDPC instruction doesn't pair, and puts 4 bubbles in the
1463      pipe to boot.  So don't use it here, especially when we're
1464      doing a save anyway because of %l7.  */
1465
1466   l2 = gen_label_rtx ();
1467   emit_label (l1);
1468
1469   /* Iff we are doing delay branch optimization, slot the sethi up
1470      here so that it will fill the delay slot of the call.  */
1471   if (flag_delayed_branch)
1472     emit_insn (gen_rtx (SET, VOIDmode, pic_offset_table_rtx,
1473                         gen_rtx (HIGH, Pmode, pic_pc_rtx)));
1474
1475   /* Note that we pun calls and jumps here!  */
1476   emit_jump_insn (gen_get_pc_via_call (l2, l1));
1477
1478   emit_label (l2);
1479
1480   if (!flag_delayed_branch)
1481     emit_insn (gen_rtx (SET, VOIDmode, pic_offset_table_rtx,
1482                         gen_rtx (HIGH, Pmode, pic_pc_rtx)));
1483
1484   emit_insn (gen_rtx (SET, VOIDmode,
1485                       pic_offset_table_rtx,
1486                       gen_rtx (LO_SUM, Pmode,
1487                                pic_offset_table_rtx, pic_pc_rtx)));
1488   emit_insn (gen_rtx (SET, VOIDmode,
1489                       pic_offset_table_rtx,
1490                       gen_rtx (PLUS, Pmode,
1491                                pic_offset_table_rtx,
1492                                gen_rtx (REG, Pmode, 15))));
1493
1494   /* emit_insn (gen_rtx (ASM_INPUT, VOIDmode, "!#PROLOGUE# 1")); */
1495   LABEL_PRESERVE_P (l1) = 1;
1496   LABEL_PRESERVE_P (l2) = 1;
1497
1498   flag_pic = orig_flag_pic;
1499
1500   seq = gen_sequence ();
1501   end_sequence ();
1502   emit_insn_after (seq, get_insns ());
1503
1504   /* Need to emit this whether or not we obey regdecls,
1505      since setjmp/longjmp can cause life info to screw up.  */
1506   emit_insn (gen_rtx (USE, VOIDmode, pic_offset_table_rtx));
1507 }
1508 \f
1509 /* Emit insns to move operands[1] into operands[0].
1510
1511    Return 1 if we have written out everything that needs to be done to
1512    do the move.  Otherwise, return 0 and the caller will emit the move
1513    normally.  */
1514
1515 int
1516 emit_move_sequence (operands, mode)
1517      rtx *operands;
1518      enum machine_mode mode;
1519 {
1520   register rtx operand0 = operands[0];
1521   register rtx operand1 = operands[1];
1522
1523   if (CONSTANT_P (operand1) && flag_pic
1524       && pic_address_needs_scratch (operand1))
1525     operands[1] = operand1 = legitimize_pic_address (operand1, mode, 0);
1526
1527   /* Handle most common case first: storing into a register.  */
1528   if (register_operand (operand0, mode))
1529     {
1530       if (register_operand (operand1, mode)
1531           || (GET_CODE (operand1) == CONST_INT && SMALL_INT (operand1))
1532           || (GET_CODE (operand1) == CONST_DOUBLE
1533               && arith_double_operand (operand1, DImode))
1534           || (GET_CODE (operand1) == HIGH && GET_MODE (operand1) != DImode)
1535           /* Only `general_operands' can come here, so MEM is ok.  */
1536           || GET_CODE (operand1) == MEM)
1537         {
1538           /* Run this case quickly.  */
1539           emit_insn (gen_rtx (SET, VOIDmode, operand0, operand1));
1540           return 1;
1541         }
1542     }
1543   else if (GET_CODE (operand0) == MEM)
1544     {
1545       if (register_operand (operand1, mode)
1546           || (operand1 == const0_rtx && ! TARGET_LIVE_G0))
1547         {
1548           /* Run this case quickly.  */
1549           emit_insn (gen_rtx (SET, VOIDmode, operand0, operand1));
1550           return 1;
1551         }
1552       if (! reload_in_progress)
1553         {
1554           operands[0] = validize_mem (operand0);
1555           operands[1] = operand1 = force_reg (mode, operand1);
1556         }
1557     }
1558
1559   if (GET_CODE (operand1) == LABEL_REF
1560       && mode == SImode && flag_pic)
1561     {
1562       if (TARGET_ARCH64)
1563         abort ();
1564       emit_insn (gen_move_pic_label_si (operand0, operand1));
1565       return 1;
1566     }
1567   /* Non-pic LABEL_REF's in sparc64 are expensive to do the normal way,
1568      so always use special code.  */
1569   else if (GET_CODE (operand1) == LABEL_REF
1570            && mode == DImode)
1571     {
1572       if (! TARGET_ARCH64)
1573         abort ();
1574       emit_insn (gen_move_label_di (operand0, operand1));
1575       return 1;
1576     }
1577   /* DImode HIGH values in sparc64 need a clobber added.  */
1578   else if (TARGET_ARCH64
1579       && GET_CODE (operand1) == HIGH && GET_MODE (operand1) == DImode)
1580     {
1581       emit_insn (gen_sethi_di_sp64 (operand0, XEXP (operand1, 0)));
1582       return 1;
1583     }
1584   /* Simplify the source if we need to.  */
1585   else if (GET_CODE (operand1) != HIGH && immediate_operand (operand1, mode))
1586     {
1587       if (flag_pic && symbolic_operand (operand1, mode))
1588         {
1589           rtx temp_reg = reload_in_progress ? operand0 : 0;
1590
1591           operands[1] = legitimize_pic_address (operand1, mode, temp_reg);
1592         }
1593       else if (GET_CODE (operand1) == CONST_INT
1594                ? (! SMALL_INT (operand1)
1595                   && ! SPARC_SETHI_P (INTVAL (operand1)))
1596                : GET_CODE (operand1) == CONST_DOUBLE
1597                ? ! arith_double_operand (operand1, DImode)
1598                : 1)
1599         {
1600           /* For DImode values, temp must be operand0 because of the way
1601              HI and LO_SUM work.  The LO_SUM operator only copies half of
1602              the LSW from the dest of the HI operator.  If the LO_SUM dest is
1603              not the same as the HI dest, then the MSW of the LO_SUM dest will
1604              never be set.
1605
1606              ??? The real problem here is that the ...(HI:DImode pattern emits
1607              multiple instructions, and the ...(LO_SUM:DImode pattern emits
1608              one instruction.  This fails, because the compiler assumes that
1609              LO_SUM copies all bits of the first operand to its dest.  Better
1610              would be to have the HI pattern emit one instruction and the
1611              LO_SUM pattern multiple instructions.  Even better would be
1612              to use four rtl insns.  */
1613           rtx temp = ((reload_in_progress || mode == DImode)
1614                       ? operand0 : gen_reg_rtx (mode));
1615
1616           if (TARGET_ARCH64 && mode == DImode)
1617             emit_insn (gen_sethi_di_sp64 (temp, operand1));
1618           else
1619             emit_insn (gen_rtx (SET, VOIDmode, temp,
1620                                 gen_rtx (HIGH, mode, operand1)));
1621
1622           if (GET_CODE (operand1) == CONST_INT)
1623             operand1 = GEN_INT (INTVAL (operand1) & 0xffffffff);
1624           else if (GET_CODE (operand1) == CONST_DOUBLE)
1625             operand1 = GEN_INT (CONST_DOUBLE_LOW (operand1) & 0xffffffff);
1626           operands[1] = gen_rtx (LO_SUM, mode, temp, operand1);
1627         }
1628     }
1629
1630   /* Now have insn-emit do whatever it normally does.  */
1631   return 0;
1632 }
1633 \f
1634 /* Return the best assembler insn template
1635    for moving operands[1] into operands[0] as a 4 byte quantity.
1636
1637    This isn't intended to be very smart.  It is up to the caller to
1638    choose the best way to do things.
1639
1640    Note that OPERANDS may be modified to suit the returned string.  */
1641
1642 char *
1643 singlemove_string (operands)
1644      rtx *operands;
1645 {
1646   if (GET_CODE (operands[0]) == MEM)
1647     {
1648       if (GET_CODE (operands[1]) != MEM)
1649         return "st %r1,%0";
1650       else
1651         abort ();
1652     }
1653   else if (GET_CODE (operands[1]) == MEM)
1654     return "ld %1,%0";
1655   else if (GET_CODE (operands[1]) == CONST_DOUBLE)
1656     {
1657       REAL_VALUE_TYPE r;
1658       long i;
1659
1660       /* Must be SFmode, otherwise this doesn't make sense.  */
1661       if (GET_MODE (operands[1]) != SFmode)
1662         abort ();
1663
1664       REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
1665       REAL_VALUE_TO_TARGET_SINGLE (r, i);
1666       operands[1] = GEN_INT (i);
1667
1668       if (CONST_OK_FOR_LETTER_P (i, 'I'))
1669         return "mov %1,%0";
1670       else if ((i & 0x000003FF) != 0)
1671         return "sethi %%hi(%a1),%0\n\tor %0,%%lo(%a1),%0";
1672       else
1673         return "sethi %%hi(%a1),%0";
1674     }
1675   else if (GET_CODE (operands[1]) == CONST_INT
1676            && ! CONST_OK_FOR_LETTER_P (INTVAL (operands[1]), 'I'))
1677     {
1678       HOST_WIDE_INT i = INTVAL (operands[1]);
1679
1680       /* If all low order 10 bits are clear, then we only need a single
1681          sethi insn to load the constant.  */
1682       /* FIXME: Use SETHI_P.  */
1683       if ((i & 0x000003FF) != 0)
1684         return "sethi %%hi(%a1),%0\n\tor %0,%%lo(%a1),%0";
1685       else
1686         return "sethi %%hi(%a1),%0";
1687     }
1688   /* Operand 1 must be a register, or a 'I' type CONST_INT.  */
1689   return "mov %1,%0";
1690 }
1691
1692 /* Return the best assembler insn template
1693    for moving operands[1] into operands[0] as an 8 byte quantity.
1694
1695    This isn't intended to be very smart.  It is up to the caller to
1696    choose the best way to do things.
1697
1698    Note that OPERANDS may be modified to suit the returned string.  */
1699
1700 char *
1701 doublemove_string (operands)
1702      rtx *operands;
1703 {
1704   rtx op0 = operands[0], op1 = operands[1];
1705
1706   if (GET_CODE (op0) == MEM)
1707     {
1708       if (GET_CODE (op1) == REG)
1709         {
1710           if (FP_REG_P (op1))
1711             return "std %1,%0";
1712           return TARGET_ARCH64 ? "stx %1,%0" : "std %1,%0";
1713         }
1714       if (TARGET_ARCH64
1715           && (op1 == const0_rtx
1716               || (GET_MODE (op1) != VOIDmode
1717                   && op1 == CONST0_RTX (GET_MODE (op1)))))
1718         return "stx %r1,%0";
1719       abort ();
1720     }
1721   else if (GET_CODE (op1) == MEM)
1722     {
1723       if (GET_CODE (op0) != REG)
1724         abort ();
1725       if (FP_REG_P (op0))
1726         return "ldd %1,%0";
1727       return TARGET_ARCH64 ? "ldx %1,%0" : "ldd %1,%0";
1728     }
1729   else if (GET_CODE (operands[1]) == CONST_DOUBLE)
1730     {
1731       /* ??? Unfinished, and maybe not needed.  */
1732       abort ();
1733     }
1734   else if (GET_CODE (operands[1]) == CONST_INT
1735            && ! CONST_OK_FOR_LETTER_P (INTVAL (operands[1]), 'I'))
1736     {
1737       /* ??? Unfinished, and maybe not needed.  */
1738       abort ();
1739     }
1740   /* Operand 1 must be a register, or a 'I' type CONST_INT.  */
1741   return "mov %1,%0";
1742 }
1743
1744 /* Return non-zero if it is OK to assume that the given memory operand is
1745    aligned at least to a 8-byte boundary.  This should only be called
1746    for memory accesses whose size is 8 bytes or larger.  */
1747
1748 int
1749 mem_aligned_8 (mem)
1750      register rtx mem;
1751 {
1752   register rtx addr;
1753   register rtx base;
1754   register rtx offset;
1755
1756   if (GET_CODE (mem) != MEM)
1757     return 0;   /* It's gotta be a MEM! */
1758
1759   addr = XEXP (mem, 0);
1760
1761   /* Now that all misaligned double parms are copied on function entry,
1762      we can assume any 64-bit object is 64-bit aligned except those which
1763      are at unaligned offsets from the stack or frame pointer.  If the
1764      TARGET_UNALIGNED_DOUBLES switch is given, we do not make this
1765      assumption.  */
1766
1767   /* See what register we use in the address.  */
1768   base = 0;
1769   if (GET_CODE (addr) == PLUS)
1770     {
1771       if (GET_CODE (XEXP (addr, 0)) == REG
1772           && GET_CODE (XEXP (addr, 1)) == CONST_INT)
1773         {
1774           base = XEXP (addr, 0);
1775           offset = XEXP (addr, 1);
1776         }
1777     }
1778   else if (GET_CODE (addr) == REG)
1779     {
1780       base = addr;
1781       offset = const0_rtx;
1782     }
1783
1784   /* If it's the stack or frame pointer, check offset alignment.
1785      We can have improper alignment in the function entry code.  */
1786   if (base
1787       && (REGNO (base) == FRAME_POINTER_REGNUM
1788           || REGNO (base) == STACK_POINTER_REGNUM))
1789     {
1790       if (((INTVAL (offset) - SPARC_STACK_BIAS) & 0x7) == 0)
1791         return 1;
1792     }
1793   /* Anything else we know is properly aligned unless TARGET_UNALIGNED_DOUBLES
1794      is true, in which case we can only assume that an access is aligned if
1795      it is to a constant address, or the address involves a LO_SUM.
1796
1797      We used to assume an address was aligned if MEM_IN_STRUCT_P was true.
1798      That assumption was deleted so that gcc generated code can be used with
1799      memory allocators that only guarantee 4 byte alignment.  */
1800   else if (! TARGET_UNALIGNED_DOUBLES || CONSTANT_P (addr)
1801            || GET_CODE (addr) == LO_SUM)
1802     return 1;
1803
1804   /* An obviously unaligned address.  */
1805   return 0;
1806 }
1807
1808 enum optype { REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP };
1809
1810 /* Output assembler code to perform a doubleword move insn
1811    with operands OPERANDS.  This is very similar to the following
1812    output_move_quad function.  */
1813
1814 char *
1815 output_move_double (operands)
1816      rtx *operands;
1817 {
1818   register rtx op0 = operands[0];
1819   register rtx op1 = operands[1];
1820   register enum optype optype0;
1821   register enum optype optype1;
1822   rtx latehalf[2];
1823   rtx addreg0 = 0;
1824   rtx addreg1 = 0;
1825   int highest_first = 0;
1826   int no_addreg1_decrement = 0;
1827
1828   /* First classify both operands.  */
1829
1830   if (REG_P (op0))
1831     optype0 = REGOP;
1832   else if (offsettable_memref_p (op0))
1833     optype0 = OFFSOP;
1834   else if (GET_CODE (op0) == MEM)
1835     optype0 = MEMOP;
1836   else
1837     optype0 = RNDOP;
1838
1839   if (REG_P (op1))
1840     optype1 = REGOP;
1841   else if (CONSTANT_P (op1))
1842     optype1 = CNSTOP;
1843   else if (offsettable_memref_p (op1))
1844     optype1 = OFFSOP;
1845   else if (GET_CODE (op1) == MEM)
1846     optype1 = MEMOP;
1847   else
1848     optype1 = RNDOP;
1849
1850   /* Check for the cases that the operand constraints are not
1851      supposed to allow to happen.  Abort if we get one,
1852      because generating code for these cases is painful.  */
1853
1854   if (optype0 == RNDOP || optype1 == RNDOP
1855       || (optype0 == MEM && optype1 == MEM))
1856     abort ();
1857
1858   /* If an operand is an unoffsettable memory ref, find a register
1859      we can increment temporarily to make it refer to the second word.  */
1860
1861   if (optype0 == MEMOP)
1862     addreg0 = find_addr_reg (XEXP (op0, 0));
1863
1864   if (optype1 == MEMOP)
1865     addreg1 = find_addr_reg (XEXP (op1, 0));
1866
1867   /* Ok, we can do one word at a time.
1868      Set up in LATEHALF the operands to use for the
1869      high-numbered (least significant) word and in some cases alter the
1870      operands in OPERANDS to be suitable for the low-numbered word.  */
1871
1872   if (optype0 == REGOP)
1873     latehalf[0] = gen_rtx (REG, SImode, REGNO (op0) + 1);
1874   else if (optype0 == OFFSOP)
1875     latehalf[0] = adj_offsettable_operand (op0, 4);
1876   else
1877     latehalf[0] = op0;
1878
1879   if (optype1 == REGOP)
1880     latehalf[1] = gen_rtx (REG, SImode, REGNO (op1) + 1);
1881   else if (optype1 == OFFSOP)
1882     latehalf[1] = adj_offsettable_operand (op1, 4);
1883   else if (optype1 == CNSTOP)
1884     {
1885       if (TARGET_ARCH64)
1886         {
1887           if (arith_double_operand (op1, DImode))
1888             {
1889               operands[1] = gen_rtx (CONST_INT, VOIDmode,
1890                                      CONST_DOUBLE_LOW (op1));
1891               return "mov %1,%0";
1892             }
1893           else
1894             {
1895               /* The only way to handle CONST_DOUBLEs or other 64 bit
1896                  constants here is to use a temporary, such as is done
1897                  for the V9 DImode sethi insn pattern.  This is not
1898                  a practical solution, so abort if we reach here.
1899                  The md file should always force such constants to
1900                  memory.  */
1901               abort ();
1902             }
1903         }
1904       else
1905         split_double (op1, &operands[1], &latehalf[1]);
1906     }
1907   else
1908     latehalf[1] = op1;
1909
1910   /* Easy case: try moving both words at once.  Check for moving between
1911      an even/odd register pair and a memory location.  */
1912   if ((optype0 == REGOP && optype1 != REGOP && optype1 != CNSTOP
1913        && (TARGET_ARCH64 || (REGNO (op0) & 1) == 0))
1914       || (optype0 != REGOP && optype0 != CNSTOP && optype1 == REGOP
1915           && (TARGET_ARCH64 || (REGNO (op1) & 1) == 0)))
1916     {
1917       register rtx mem,reg;
1918
1919       if (optype0 == REGOP)
1920         mem = op1, reg = op0;
1921       else
1922         mem = op0, reg = op1;
1923
1924       /* In v9, ldd can be used for word aligned addresses, so technically
1925          some of this logic is unneeded.  We still avoid ldd if the address
1926          is obviously unaligned though.  */
1927
1928       if (mem_aligned_8 (mem)
1929           /* If this is a floating point register higher than %f31,
1930              then we *must* use an aligned load, since `ld' will not accept
1931              the register number.  */
1932           || (TARGET_V9 && REGNO (reg) >= 64))
1933         {
1934           if (FP_REG_P (reg) || ! TARGET_ARCH64)
1935             return (mem == op1 ? "ldd %1,%0" : "std %1,%0");
1936           else
1937             return (mem == op1 ? "ldx %1,%0" : "stx %1,%0");
1938         }
1939     }
1940
1941   if (TARGET_ARCH64)
1942     {
1943       if (optype0 == REGOP && optype1 == REGOP)
1944         {
1945           if (FP_REG_P (op0))
1946             return "fmovd %1,%0";
1947           else
1948             return "mov %1,%0";
1949         }
1950     }
1951
1952   /* If the first move would clobber the source of the second one,
1953      do them in the other order.  */
1954
1955   /* Overlapping registers.  */
1956   if (optype0 == REGOP && optype1 == REGOP
1957       && REGNO (op0) == REGNO (latehalf[1]))
1958     {
1959       /* Do that word.  */
1960       output_asm_insn (singlemove_string (latehalf), latehalf);
1961       /* Do low-numbered word.  */
1962       return singlemove_string (operands);
1963     }
1964   /* Loading into a register which overlaps a register used in the address.  */
1965   else if (optype0 == REGOP && optype1 != REGOP
1966            && reg_overlap_mentioned_p (op0, op1))
1967     {
1968       /* If both halves of dest are used in the src memory address,
1969          add the two regs and put them in the low reg (op0).
1970          Then it works to load latehalf first.  */
1971       if (reg_mentioned_p (op0, XEXP (op1, 0))
1972           && reg_mentioned_p (latehalf[0], XEXP (op1, 0)))
1973         {
1974           rtx xops[2];
1975           xops[0] = latehalf[0];
1976           xops[1] = op0;
1977           output_asm_insn ("add %1,%0,%1", xops);
1978           operands[1] = gen_rtx (MEM, DImode, op0);
1979           latehalf[1] = adj_offsettable_operand (operands[1], 4);
1980           addreg1 = 0;
1981           highest_first = 1;
1982         }
1983       /* Only one register in the dest is used in the src memory address,
1984          and this is the first register of the dest, so we want to do
1985          the late half first here also.  */
1986       else if (! reg_mentioned_p (latehalf[0], XEXP (op1, 0)))
1987         highest_first = 1;
1988       /* Only one register in the dest is used in the src memory address,
1989          and this is the second register of the dest, so we want to do
1990          the late half last.  If addreg1 is set, and addreg1 is the same
1991          register as latehalf, then we must suppress the trailing decrement,
1992          because it would clobber the value just loaded.  */
1993       else if (addreg1 && reg_mentioned_p (addreg1, latehalf[0]))
1994         no_addreg1_decrement = 1;
1995     }
1996
1997   /* Normal case: do the two words, low-numbered first.
1998      Overlap case (highest_first set): do high-numbered word first.  */
1999
2000   if (! highest_first)
2001     output_asm_insn (singlemove_string (operands), operands);
2002
2003   /* Make any unoffsettable addresses point at high-numbered word.  */
2004   if (addreg0)
2005     output_asm_insn ("add %0,0x4,%0", &addreg0);
2006   if (addreg1)
2007     output_asm_insn ("add %0,0x4,%0", &addreg1);
2008
2009   /* Do that word.  */
2010   output_asm_insn (singlemove_string (latehalf), latehalf);
2011
2012   /* Undo the adds we just did.  */
2013   if (addreg0)
2014     output_asm_insn ("add %0,-0x4,%0", &addreg0);
2015   if (addreg1 && ! no_addreg1_decrement)
2016     output_asm_insn ("add %0,-0x4,%0", &addreg1);
2017
2018   if (highest_first)
2019     output_asm_insn (singlemove_string (operands), operands);
2020
2021   return "";
2022 }
2023
2024 /* Output assembler code to perform a quadword move insn
2025    with operands OPERANDS.  This is very similar to the preceding
2026    output_move_double function.  */
2027
2028 char *
2029 output_move_quad (operands)
2030      rtx *operands;
2031 {
2032   register rtx op0 = operands[0];
2033   register rtx op1 = operands[1];
2034   register enum optype optype0;
2035   register enum optype optype1;
2036   rtx wordpart[4][2];
2037   rtx addreg0 = 0;
2038   rtx addreg1 = 0;
2039
2040   /* First classify both operands.  */
2041
2042   if (REG_P (op0))
2043     optype0 = REGOP;
2044   else if (offsettable_memref_p (op0))
2045     optype0 = OFFSOP;
2046   else if (GET_CODE (op0) == MEM)
2047     optype0 = MEMOP;
2048   else
2049     optype0 = RNDOP;
2050
2051   if (REG_P (op1))
2052     optype1 = REGOP;
2053   else if (CONSTANT_P (op1))
2054     optype1 = CNSTOP;
2055   else if (offsettable_memref_p (op1))
2056     optype1 = OFFSOP;
2057   else if (GET_CODE (op1) == MEM)
2058     optype1 = MEMOP;
2059   else
2060     optype1 = RNDOP;
2061
2062   /* Check for the cases that the operand constraints are not
2063      supposed to allow to happen.  Abort if we get one,
2064      because generating code for these cases is painful.  */
2065
2066   if (optype0 == RNDOP || optype1 == RNDOP
2067       || (optype0 == MEM && optype1 == MEM))
2068     abort ();
2069
2070   /* If an operand is an unoffsettable memory ref, find a register
2071      we can increment temporarily to make it refer to the later words.  */
2072
2073   if (optype0 == MEMOP)
2074     addreg0 = find_addr_reg (XEXP (op0, 0));
2075
2076   if (optype1 == MEMOP)
2077     addreg1 = find_addr_reg (XEXP (op1, 0));
2078
2079   /* Ok, we can do one word at a time.
2080      Set up in wordpart the operands to use for each word of the arguments.  */
2081
2082   if (optype0 == REGOP)
2083     {
2084       wordpart[0][0] = gen_rtx (REG, word_mode, REGNO (op0) + 0);
2085       wordpart[1][0] = gen_rtx (REG, word_mode, REGNO (op0) + 1);
2086       if (TARGET_ARCH32)
2087         {
2088           wordpart[2][0] = gen_rtx (REG, word_mode, REGNO (op0) + 2);
2089           wordpart[3][0] = gen_rtx (REG, word_mode, REGNO (op0) + 3);
2090         }
2091     }
2092   else if (optype0 == OFFSOP)
2093     {
2094       wordpart[0][0] = adj_offsettable_operand (op0, 0);
2095       if (TARGET_ARCH32)
2096         {
2097           wordpart[1][0] = adj_offsettable_operand (op0, 4);
2098           wordpart[2][0] = adj_offsettable_operand (op0, 8);
2099           wordpart[3][0] = adj_offsettable_operand (op0, 12);
2100         }
2101       else
2102         wordpart[1][0] = adj_offsettable_operand (op0, 8);
2103     }
2104   else
2105     {
2106       wordpart[0][0] = op0;
2107       wordpart[1][0] = op0;
2108       wordpart[2][0] = op0;
2109       wordpart[3][0] = op0;
2110     }
2111
2112   if (optype1 == REGOP)
2113     {
2114       wordpart[0][1] = gen_rtx (REG, word_mode, REGNO (op1) + 0);
2115       wordpart[1][1] = gen_rtx (REG, word_mode, REGNO (op1) + 1);
2116       if (TARGET_ARCH32)
2117         {
2118           wordpart[2][1] = gen_rtx (REG, word_mode, REGNO (op1) + 2);
2119           wordpart[3][1] = gen_rtx (REG, word_mode, REGNO (op1) + 3);
2120         }
2121     }
2122   else if (optype1 == OFFSOP)
2123     {
2124       wordpart[0][1] = adj_offsettable_operand (op1, 0);
2125       if (TARGET_ARCH32)
2126         {
2127           wordpart[1][1] = adj_offsettable_operand (op1, 4);
2128           wordpart[2][1] = adj_offsettable_operand (op1, 8);
2129           wordpart[3][1] = adj_offsettable_operand (op1, 12);
2130         }
2131       else
2132         wordpart[1][1] = adj_offsettable_operand (op1, 8);
2133     }
2134   else if (optype1 == CNSTOP)
2135     {
2136       REAL_VALUE_TYPE r;
2137       long l[4];
2138
2139       /* This only works for TFmode floating point constants.  */
2140       if (GET_CODE (op1) != CONST_DOUBLE || GET_MODE (op1) != TFmode)
2141         abort ();
2142
2143       REAL_VALUE_FROM_CONST_DOUBLE (r, op1);
2144       REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
2145       
2146       wordpart[0][1] = GEN_INT (l[0]);
2147       wordpart[1][1] = GEN_INT (l[1]);
2148       wordpart[2][1] = GEN_INT (l[2]);
2149       wordpart[3][1] = GEN_INT (l[3]);
2150     }
2151   else
2152     {
2153       wordpart[0][1] = op1;
2154       wordpart[1][1] = op1;
2155       wordpart[2][1] = op1;
2156       wordpart[3][1] = op1;
2157     }
2158
2159   /* Easy case: try moving the quad as two pairs.  Check for moving between
2160      an even/odd register pair and a memory location.
2161      Also handle new v9 fp regs here.  */
2162   /* ??? Should also handle the case of non-offsettable addresses here.
2163      We can at least do the first pair as a ldd/std, and then do the third
2164      and fourth words individually.  */
2165   if ((optype0 == REGOP && optype1 == OFFSOP && (REGNO (op0) & 1) == 0)
2166       || (optype0 == OFFSOP && optype1 == REGOP && (REGNO (op1) & 1) == 0))
2167     {
2168       rtx mem, reg;
2169
2170       if (optype0 == REGOP)
2171         mem = op1, reg = op0;
2172       else
2173         mem = op0, reg = op1;
2174
2175       if (mem_aligned_8 (mem)
2176           /* If this is a floating point register higher than %f31,
2177              then we *must* use an aligned load, since `ld' will not accept
2178              the register number.  */
2179           || (TARGET_V9 && REGNO (reg) >= SPARC_FIRST_V9_FP_REG))
2180         {
2181           if (TARGET_V9 && FP_REG_P (reg) && TARGET_HARD_QUAD)
2182             {
2183               if ((REGNO (reg) & 3) != 0)
2184                 abort ();
2185               /* ??? Can `mem' have an inappropriate alignment here?  */
2186               return (mem == op1 ? "ldq %1,%0" : "stq %1,%0");
2187             }
2188           operands[2] = adj_offsettable_operand (mem, 8);
2189           /* ??? In arch64 case, shouldn't we use ldd/std for fp regs.  */
2190           if (mem == op1)
2191             return TARGET_ARCH64 ? "ldx %1,%0;ldx %2,%R0" : "ldd %1,%0;ldd %2,%S0";
2192           else
2193             return TARGET_ARCH64 ? "stx %1,%0;stx %R1,%2" : "std %1,%0;std %S1,%2";
2194         }
2195     }
2196
2197   /* If the first move would clobber the source of the second one,
2198      do them in the other order.  */
2199
2200   /* Overlapping registers?  */
2201   if (TARGET_ARCH32)
2202     {
2203       if (optype0 == REGOP && optype1 == REGOP
2204           && (REGNO (op0) == REGNO (wordpart[1][3])
2205               || REGNO (op0) == REGNO (wordpart[1][2])
2206               || REGNO (op0) == REGNO (wordpart[1][1])))
2207         {
2208           /* Do fourth word.  */
2209           output_asm_insn (singlemove_string (wordpart[3]), wordpart[3]);
2210           /* Do the third word.  */
2211           output_asm_insn (singlemove_string (wordpart[2]), wordpart[2]);
2212           /* Do the second word.  */
2213           output_asm_insn (singlemove_string (wordpart[1]), wordpart[1]);
2214           /* Do lowest-numbered word.  */
2215           output_asm_insn (singlemove_string (wordpart[0]), wordpart[0]);
2216           return "";
2217         }
2218     }
2219   else /* TARGET_ARCH64 */
2220     {
2221       if (optype0 == REGOP && optype1 == REGOP
2222           && REGNO (op0) == REGNO (wordpart[1][1]))
2223         {
2224           output_asm_insn ("mov %1,%0", wordpart[1]);
2225           output_asm_insn ("mov %1,%0", wordpart[0]);
2226           return "";
2227         }
2228     }
2229
2230   /* Loading into a register which overlaps a register used in the address.  */
2231   if (optype0 == REGOP && optype1 != REGOP
2232       && reg_overlap_mentioned_p (op0, op1))
2233     {
2234       /* ??? Not implemented yet.  This is a bit complicated, because we
2235          must load which ever part overlaps the address last.  If the address
2236          is a double-reg address, then there are two parts which need to
2237          be done last, which is impossible.  We would need a scratch register
2238          in that case.  */
2239       abort ();
2240     }
2241
2242   /* Normal case: move the words in lowest to highest address order.  */
2243
2244   if (TARGET_ARCH32)
2245     {
2246       output_asm_insn (singlemove_string (wordpart[0]), wordpart[0]);
2247
2248       /* Make any unoffsettable addresses point at the second word.  */
2249       if (addreg0)
2250         output_asm_insn ("add %0,0x4,%0", &addreg0);
2251       if (addreg1)
2252         output_asm_insn ("add %0,0x4,%0", &addreg1);
2253
2254       /* Do the second word.  */
2255       output_asm_insn (singlemove_string (wordpart[1]), wordpart[1]);
2256
2257       /* Make any unoffsettable addresses point at the third word.  */
2258       if (addreg0)
2259         output_asm_insn ("add %0,0x4,%0", &addreg0);
2260       if (addreg1)
2261         output_asm_insn ("add %0,0x4,%0", &addreg1);
2262
2263       /* Do the third word.  */
2264       output_asm_insn (singlemove_string (wordpart[2]), wordpart[2]);
2265
2266       /* Make any unoffsettable addresses point at the fourth word.  */
2267       if (addreg0)
2268         output_asm_insn ("add %0,0x4,%0", &addreg0);
2269       if (addreg1)
2270         output_asm_insn ("add %0,0x4,%0", &addreg1);
2271
2272       /* Do the fourth word.  */
2273       output_asm_insn (singlemove_string (wordpart[3]), wordpart[3]);
2274
2275       /* Undo the adds we just did.  */
2276       if (addreg0)
2277         output_asm_insn ("add %0,-0xc,%0", &addreg0);
2278       if (addreg1)
2279         output_asm_insn ("add %0,-0xc,%0", &addreg1);
2280     }
2281   else /* TARGET_ARCH64 */
2282     {
2283       output_asm_insn (doublemove_string (wordpart[0]), wordpart[0]);
2284
2285       /* Make any unoffsettable addresses point at the second word.  */
2286       if (addreg0)
2287         output_asm_insn ("add %0,0x8,%0", &addreg0);
2288       if (addreg1)
2289         output_asm_insn ("add %0,0x8,%0", &addreg1);
2290
2291       /* Do the second word.  */
2292       output_asm_insn (doublemove_string (wordpart[1]), wordpart[1]);
2293
2294       /* Undo the adds we just did.  */
2295       if (addreg0)
2296         output_asm_insn ("add %0,-0x8,%0", &addreg0);
2297       if (addreg1)
2298         output_asm_insn ("add %0,-0x8,%0", &addreg1);
2299     }
2300
2301   return "";
2302 }
2303 \f
2304 /* Output assembler code to perform a doubleword move insn with operands
2305    OPERANDS, one of which must be a floating point register.  */
2306
2307 char *
2308 output_fp_move_double (operands)
2309      rtx *operands;
2310 {
2311   if (FP_REG_P (operands[0]))
2312     {
2313       if (FP_REG_P (operands[1]))
2314         {
2315           if (TARGET_V9)
2316             return "fmovd %1,%0";
2317           else
2318             return "fmovs %1,%0\n\tfmovs %R1,%R0";
2319         }
2320       else if (GET_CODE (operands[1]) == REG)
2321         abort ();
2322       else
2323         return output_move_double (operands);
2324     }
2325   else if (FP_REG_P (operands[1]))
2326     {
2327       if (GET_CODE (operands[0]) == REG)
2328         abort ();
2329       else
2330         return output_move_double (operands);
2331     }
2332   else abort ();
2333 }
2334
2335 /* Output assembler code to perform a quadword move insn with operands
2336    OPERANDS, one of which must be a floating point register.  */
2337
2338 char *
2339 output_fp_move_quad (operands)
2340      rtx *operands;
2341 {
2342   register rtx op0 = operands[0];
2343   register rtx op1 = operands[1];
2344
2345   if (FP_REG_P (op0))
2346     {
2347       if (FP_REG_P (op1))
2348         {
2349           if (TARGET_V9 && TARGET_HARD_QUAD)
2350             return "fmovq %1,%0";
2351           else
2352             return "fmovs %1,%0\n\tfmovs %R1,%R0\n\tfmovs %S1,%S0\n\tfmovs %T1,%T0";
2353         }
2354       else if (GET_CODE (op1) == REG)
2355         abort ();
2356       else
2357         return output_move_quad (operands);
2358     }
2359   else if (FP_REG_P (op1))
2360     {
2361       if (GET_CODE (op0) == REG)
2362         abort ();
2363       else
2364         return output_move_quad (operands);
2365     }
2366   else
2367     abort ();
2368 }
2369 \f
2370 /* Return a REG that occurs in ADDR with coefficient 1.
2371    ADDR can be effectively incremented by incrementing REG.  */
2372
2373 static rtx
2374 find_addr_reg (addr)
2375      rtx addr;
2376 {
2377   while (GET_CODE (addr) == PLUS)
2378     {
2379       /* We absolutely can not fudge the frame pointer here, because the
2380          frame pointer must always be 8 byte aligned.  It also confuses
2381          debuggers.  */
2382       if (GET_CODE (XEXP (addr, 0)) == REG
2383           && REGNO (XEXP (addr, 0)) != FRAME_POINTER_REGNUM)
2384         addr = XEXP (addr, 0);
2385       else if (GET_CODE (XEXP (addr, 1)) == REG
2386                && REGNO (XEXP (addr, 1)) != FRAME_POINTER_REGNUM)
2387         addr = XEXP (addr, 1);
2388       else if (CONSTANT_P (XEXP (addr, 0)))
2389         addr = XEXP (addr, 1);
2390       else if (CONSTANT_P (XEXP (addr, 1)))
2391         addr = XEXP (addr, 0);
2392       else
2393         abort ();
2394     }
2395   if (GET_CODE (addr) == REG)
2396     return addr;
2397   abort ();
2398 }
2399
2400 #if 0 /* not currently used */
2401
2402 void
2403 output_sized_memop (opname, mode, signedp)
2404      char *opname;
2405      enum machine_mode mode;
2406      int signedp;
2407 {
2408   static char *ld_size_suffix_u[] = { "ub", "uh", "", "?", "d" };
2409   static char *ld_size_suffix_s[] = { "sb", "sh", "", "?", "d" };
2410   static char *st_size_suffix[] = { "b", "h", "", "?", "d" };
2411   char **opnametab, *modename;
2412
2413   if (opname[0] == 'l')
2414     if (signedp)
2415       opnametab = ld_size_suffix_s;
2416     else
2417       opnametab = ld_size_suffix_u;
2418   else
2419     opnametab = st_size_suffix;
2420   modename = opnametab[GET_MODE_SIZE (mode) >> 1];
2421
2422   fprintf (asm_out_file, "\t%s%s", opname, modename);
2423 }
2424
2425 void
2426 output_move_with_extension (operands)
2427      rtx *operands;
2428 {
2429   if (GET_MODE (operands[2]) == HImode)
2430     output_asm_insn ("sll %2,0x10,%0", operands);
2431   else if (GET_MODE (operands[2]) == QImode)
2432     output_asm_insn ("sll %2,0x18,%0", operands);
2433   else
2434     abort ();
2435 }
2436 #endif /* not currently used */
2437 \f
2438 #if 0
2439 /* ??? These are only used by the movstrsi pattern, but we get better code
2440    in general without that, because emit_block_move can do just as good a
2441    job as this function does when alignment and size are known.  When they
2442    aren't known, a call to strcpy may be faster anyways, because it is
2443    likely to be carefully crafted assembly language code, and below we just
2444    do a byte-wise copy.
2445
2446    Also, emit_block_move expands into multiple read/write RTL insns, which
2447    can then be optimized, whereas our movstrsi pattern can not be optimized
2448    at all.  */
2449
2450 /* Load the address specified by OPERANDS[3] into the register
2451    specified by OPERANDS[0].
2452
2453    OPERANDS[3] may be the result of a sum, hence it could either be:
2454
2455    (1) CONST
2456    (2) REG
2457    (2) REG + CONST_INT
2458    (3) REG + REG + CONST_INT
2459    (4) REG + REG  (special case of 3).
2460
2461    Note that (3) is not a legitimate address.
2462    All cases are handled here.  */
2463
2464 void
2465 output_load_address (operands)
2466      rtx *operands;
2467 {
2468   rtx base, offset;
2469
2470   if (CONSTANT_P (operands[3]))
2471     {
2472       output_asm_insn ("set %3,%0", operands);
2473       return;
2474     }
2475
2476   if (REG_P (operands[3]))
2477     {
2478       if (REGNO (operands[0]) != REGNO (operands[3]))
2479         output_asm_insn ("mov %3,%0", operands);
2480       return;
2481     }
2482
2483   if (GET_CODE (operands[3]) != PLUS)
2484     abort ();
2485
2486   base = XEXP (operands[3], 0);
2487   offset = XEXP (operands[3], 1);
2488
2489   if (GET_CODE (base) == CONST_INT)
2490     {
2491       rtx tmp = base;
2492       base = offset;
2493       offset = tmp;
2494     }
2495
2496   if (GET_CODE (offset) != CONST_INT)
2497     {
2498       /* Operand is (PLUS (REG) (REG)).  */
2499       base = operands[3];
2500       offset = const0_rtx;
2501     }
2502
2503   if (REG_P (base))
2504     {
2505       operands[6] = base;
2506       operands[7] = offset;
2507       if (SMALL_INT (offset))
2508         output_asm_insn ("add %6,%7,%0", operands);
2509       else
2510         output_asm_insn ("set %7,%0\n\tadd %0,%6,%0", operands);
2511     }
2512   else if (GET_CODE (base) == PLUS)
2513     {
2514       operands[6] = XEXP (base, 0);
2515       operands[7] = XEXP (base, 1);
2516       operands[8] = offset;
2517
2518       if (SMALL_INT (offset))
2519         output_asm_insn ("add %6,%7,%0\n\tadd %0,%8,%0", operands);
2520       else
2521         output_asm_insn ("set %8,%0\n\tadd %0,%6,%0\n\tadd %0,%7,%0", operands);
2522     }
2523   else
2524     abort ();
2525 }
2526
2527 /* Output code to place a size count SIZE in register REG.
2528    ALIGN is the size of the unit of transfer.
2529
2530    Because block moves are pipelined, we don't include the
2531    first element in the transfer of SIZE to REG.  */
2532
2533 static void
2534 output_size_for_block_move (size, reg, align)
2535      rtx size, reg;
2536      rtx align;
2537 {
2538   rtx xoperands[3];
2539
2540   xoperands[0] = reg;
2541   xoperands[1] = size;
2542   xoperands[2] = align;
2543   if (GET_CODE (size) == REG)
2544     output_asm_insn ("sub %1,%2,%0", xoperands);
2545   else
2546     {
2547       xoperands[1]
2548         = gen_rtx (CONST_INT, VOIDmode, INTVAL (size) - INTVAL (align));
2549       output_asm_insn ("set %1,%0", xoperands);
2550     }
2551 }
2552
2553 /* Emit code to perform a block move.
2554
2555    OPERANDS[0] is the destination.
2556    OPERANDS[1] is the source.
2557    OPERANDS[2] is the size.
2558    OPERANDS[3] is the alignment safe to use.
2559    OPERANDS[4] is a register we can safely clobber as a temp.  */
2560
2561 char *
2562 output_block_move (operands)
2563      rtx *operands;
2564 {
2565   /* A vector for our computed operands.  Note that load_output_address
2566      makes use of (and can clobber) up to the 8th element of this vector.  */
2567   rtx xoperands[10];
2568   rtx zoperands[10];
2569   static int movstrsi_label = 0;
2570   int i;
2571   rtx temp1 = operands[4];
2572   rtx sizertx = operands[2];
2573   rtx alignrtx = operands[3];
2574   int align = INTVAL (alignrtx);
2575   char label3[30], label5[30];
2576
2577   xoperands[0] = operands[0];
2578   xoperands[1] = operands[1];
2579   xoperands[2] = temp1;
2580
2581   /* We can't move more than this many bytes at a time because we have only
2582      one register, %g1, to move them through.  */
2583   if (align > UNITS_PER_WORD)
2584     {
2585       align = UNITS_PER_WORD;
2586       alignrtx = gen_rtx (CONST_INT, VOIDmode, UNITS_PER_WORD);
2587     }
2588
2589   /* We consider 8 ld/st pairs, for a total of 16 inline insns to be
2590      reasonable here.  (Actually will emit a maximum of 18 inline insns for
2591      the case of size == 31 and align == 4).  */
2592
2593   if (GET_CODE (sizertx) == CONST_INT && (INTVAL (sizertx) / align) <= 8
2594       && memory_address_p (QImode, plus_constant_for_output (xoperands[0],
2595                                                              INTVAL (sizertx)))
2596       && memory_address_p (QImode, plus_constant_for_output (xoperands[1],
2597                                                              INTVAL (sizertx))))
2598     {
2599       int size = INTVAL (sizertx);
2600       int offset = 0;
2601
2602       /* We will store different integers into this particular RTX.  */
2603       xoperands[2] = rtx_alloc (CONST_INT);
2604       PUT_MODE (xoperands[2], VOIDmode);
2605
2606       /* This case is currently not handled.  Abort instead of generating
2607          bad code.  */
2608       if (align > UNITS_PER_WORD)
2609         abort ();
2610
2611       if (TARGET_ARCH64 && align >= 8)
2612         {
2613           for (i = (size >> 3) - 1; i >= 0; i--)
2614             {
2615               INTVAL (xoperands[2]) = (i << 3) + offset;
2616               output_asm_insn ("ldx [%a1+%2],%%g1\n\tstx %%g1,[%a0+%2]",
2617                                xoperands);
2618             }
2619           offset += (size & ~0x7);
2620           size = size & 0x7;
2621           if (size == 0)
2622             return "";
2623         }
2624
2625       if (align >= 4)
2626         {
2627           for (i = (size >> 2) - 1; i >= 0; i--)
2628             {
2629               INTVAL (xoperands[2]) = (i << 2) + offset;
2630               output_asm_insn ("ld [%a1+%2],%%g1\n\tst %%g1,[%a0+%2]",
2631                                xoperands);
2632             }
2633           offset += (size & ~0x3);
2634           size = size & 0x3;
2635           if (size == 0)
2636             return "";
2637         }
2638
2639       if (align >= 2)
2640         {
2641           for (i = (size >> 1) - 1; i >= 0; i--)
2642             {
2643               INTVAL (xoperands[2]) = (i << 1) + offset;
2644               output_asm_insn ("lduh [%a1+%2],%%g1\n\tsth %%g1,[%a0+%2]",
2645                                xoperands);
2646             }
2647           offset += (size & ~0x1);
2648           size = size & 0x1;
2649           if (size == 0)
2650             return "";
2651         }
2652
2653       if (align >= 1)
2654         {
2655           for (i = size - 1; i >= 0; i--)
2656             {
2657               INTVAL (xoperands[2]) = i + offset;
2658               output_asm_insn ("ldub [%a1+%2],%%g1\n\tstb %%g1,[%a0+%2]",
2659                                xoperands);
2660             }
2661           return "";
2662         }
2663
2664       /* We should never reach here.  */
2665       abort ();
2666     }
2667
2668   /* If the size isn't known to be a multiple of the alignment,
2669      we have to do it in smaller pieces.  If we could determine that
2670      the size was a multiple of 2 (or whatever), we could be smarter
2671      about this.  */
2672   if (GET_CODE (sizertx) != CONST_INT)
2673     align = 1;
2674   else
2675     {
2676       int size = INTVAL (sizertx);
2677       while (size % align)
2678         align >>= 1;
2679     }
2680
2681   if (align != INTVAL (alignrtx))
2682     alignrtx = gen_rtx (CONST_INT, VOIDmode, align);
2683
2684   xoperands[3] = gen_rtx (CONST_INT, VOIDmode, movstrsi_label++);
2685   xoperands[4] = gen_rtx (CONST_INT, VOIDmode, align);
2686   xoperands[5] = gen_rtx (CONST_INT, VOIDmode, movstrsi_label++);
2687
2688   ASM_GENERATE_INTERNAL_LABEL (label3, "Lm", INTVAL (xoperands[3]));
2689   ASM_GENERATE_INTERNAL_LABEL (label5, "Lm", INTVAL (xoperands[5]));
2690
2691   /* This is the size of the transfer.  Emit code to decrement the size
2692      value by ALIGN, and store the result in the temp1 register.  */
2693   output_size_for_block_move (sizertx, temp1, alignrtx);
2694
2695   /* Must handle the case when the size is zero or negative, so the first thing
2696      we do is compare the size against zero, and only copy bytes if it is
2697      zero or greater.  Note that we have already subtracted off the alignment
2698      once, so we must copy 1 alignment worth of bytes if the size is zero
2699      here.
2700
2701      The SUN assembler complains about labels in branch delay slots, so we
2702      do this before outputting the load address, so that there will always
2703      be a harmless insn between the branch here and the next label emitted
2704      below.  */
2705
2706   {
2707     char pattern[100];
2708
2709     sprintf (pattern, "cmp %%2,0\n\tbl %s", &label5[1]);
2710     output_asm_insn (pattern, xoperands);
2711   }
2712
2713   zoperands[0] = operands[0];
2714   zoperands[3] = plus_constant_for_output (operands[0], align);
2715   output_load_address (zoperands);
2716
2717   /* ??? This might be much faster if the loops below were preconditioned
2718      and unrolled.
2719
2720      That is, at run time, copy enough bytes one at a time to ensure that the
2721      target and source addresses are aligned to the the largest possible
2722      alignment.  Then use a preconditioned unrolled loop to copy say 16
2723      bytes at a time.  Then copy bytes one at a time until finish the rest.  */
2724
2725   /* Output the first label separately, so that it is spaced properly.  */
2726
2727   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "Lm", INTVAL (xoperands[3]));
2728
2729   {
2730     char pattern[200];
2731     register char *ld_suffix = ((align == 1) ? "ub" : (align == 2) ? "uh"
2732                                 : (align == 8 && TARGET_ARCH64) ? "x" : "");
2733     register char *st_suffix = ((align == 1) ? "b" : (align == 2) ? "h"
2734                                 : (align == 8 && TARGET_ARCH64) ? "x" : "");
2735
2736     sprintf (pattern, "ld%s [%%1+%%2],%%%%g1\n\tsubcc %%2,%%4,%%2\n\tbge %s\n\tst%s %%%%g1,[%%0+%%2]\n%s:", ld_suffix, &label3[1], st_suffix, &label5[1]);
2737     output_asm_insn (pattern, xoperands);
2738   }
2739
2740   return "";
2741 }
2742 #endif
2743 \f
2744 /* Output reasonable peephole for set-on-condition-code insns.
2745    Note that these insns assume a particular way of defining
2746    labels.  Therefore, *both* sparc.h and this function must
2747    be changed if a new syntax is needed.    */
2748
2749 char *
2750 output_scc_insn (operands, insn)
2751      rtx operands[];
2752      rtx insn;
2753 {
2754   static char string[100];
2755   rtx label = 0, next = insn;
2756   int need_label = 0;
2757
2758   /* Try doing a jump optimization which jump.c can't do for us
2759      because we did not expose that setcc works by using branches.
2760
2761      If this scc insn is followed by an unconditional branch, then have
2762      the jump insn emitted here jump to that location, instead of to
2763      the end of the scc sequence as usual.  */
2764
2765   do
2766     {
2767       if (GET_CODE (next) == CODE_LABEL)
2768         label = next;
2769       next = NEXT_INSN (next);
2770       if (next == 0)
2771         break;
2772     }
2773   while (GET_CODE (next) == NOTE || GET_CODE (next) == CODE_LABEL);
2774
2775   /* If we are in a sequence, and the following insn is a sequence also,
2776      then just following the current insn's next field will take us to the
2777      first insn of the next sequence, which is the wrong place.  We don't
2778      want to optimize with a branch that has had its delay slot filled.
2779      Avoid this by verifying that NEXT_INSN (PREV_INSN (next)) == next
2780      which fails only if NEXT is such a branch.  */
2781
2782   if (next && GET_CODE (next) == JUMP_INSN && simplejump_p (next)
2783       && (! final_sequence || NEXT_INSN (PREV_INSN (next)) == next))
2784     label = JUMP_LABEL (next);
2785   /* If not optimizing, jump label fields are not set.  To be safe, always
2786      check here to whether label is still zero.  */
2787   if (label == 0)
2788     {
2789       label = gen_label_rtx ();
2790       need_label = 1;
2791     }
2792
2793   LABEL_NUSES (label) += 1;
2794
2795   /* operands[3] is an unused slot.  */
2796   operands[3] = label;
2797
2798   /* If we are in a delay slot, assume it is the delay slot of an fpcc
2799      insn since our type isn't allowed anywhere else.  */
2800
2801   /* ??? Fpcc instructions no longer have delay slots, so this code is
2802      probably obsolete.  */
2803
2804   /* The fastest way to emit code for this is an annulled branch followed
2805      by two move insns.  This will take two cycles if the branch is taken,
2806      and three cycles if the branch is not taken.
2807
2808      However, if we are in the delay slot of another branch, this won't work,
2809      because we can't put a branch in the delay slot of another branch.
2810      The above sequence would effectively take 3 or 4 cycles respectively
2811      since a no op would have be inserted between the two branches.
2812      In this case, we want to emit a move, annulled branch, and then the
2813      second move.  This sequence always takes 3 cycles, and hence is faster
2814      when we are in a branch delay slot.  */
2815
2816   if (final_sequence)
2817     {
2818       strcpy (string, "mov 0,%0\n\t");
2819       strcat (string, output_cbranch (operands[2], 3, 0, 1, 0));
2820       strcat (string, "\n\tmov 1,%0");
2821     }
2822   else
2823     {
2824       strcpy (string, output_cbranch (operands[2], 3, 0, 1, 0));
2825       strcat (string, "\n\tmov 1,%0\n\tmov 0,%0");
2826     }
2827
2828   if (need_label)
2829     strcat (string, "\n%l3:");
2830
2831   return string;
2832 }
2833 \f
2834 /* Vectors to keep interesting information about registers where it can easily
2835    be got.  We use to use the actual mode value as the bit number, but there
2836    are more than 32 modes now.  Instead we use two tables: one indexed by
2837    hard register number, and one indexed by mode.  */
2838
2839 /* The purpose of sparc_mode_class is to shrink the range of modes so that
2840    they all fit (as bit numbers) in a 32 bit word (again).  Each real mode is
2841    mapped into one sparc_mode_class mode.  */
2842
2843 enum sparc_mode_class {
2844   S_MODE, D_MODE, T_MODE, O_MODE,
2845   SF_MODE, DF_MODE, TF_MODE, OF_MODE,
2846   CC_MODE, CCFP_MODE
2847 };
2848
2849 /* Modes for single-word and smaller quantities.  */
2850 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
2851
2852 /* Modes for double-word and smaller quantities.  */
2853 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
2854
2855 /* Modes for quad-word and smaller quantities.  */
2856 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
2857
2858 /* Modes for single-float quantities.  We must allow any single word or
2859    smaller quantity.  This is because the fix/float conversion instructions
2860    take integer inputs/outputs from the float registers.  */
2861 #define SF_MODES (S_MODES)
2862
2863 /* Modes for double-float and smaller quantities.  */
2864 #define DF_MODES (S_MODES | D_MODES)
2865
2866 /* ??? Sparc64 fp regs cannot hold DImode values.  */
2867 #define DF_MODES64 (SF_MODES | DF_MODE /* | D_MODE*/)
2868
2869 /* Modes for double-float only quantities.  */
2870 /* ??? Sparc64 fp regs cannot hold DImode values.
2871    See fix_truncsfdi2.  */
2872 #define DF_ONLY_MODES ((1 << (int) DF_MODE) /*| (1 << (int) D_MODE)*/)
2873
2874 /* Modes for double-float and larger quantities.  */
2875 #define DF_UP_MODES (DF_ONLY_MODES | TF_ONLY_MODES)
2876
2877 /* Modes for quad-float only quantities.  */
2878 #define TF_ONLY_MODES (1 << (int) TF_MODE)
2879
2880 /* Modes for quad-float and smaller quantities.  */
2881 #define TF_MODES (DF_MODES | TF_ONLY_MODES)
2882
2883 /* ??? Sparc64 fp regs cannot hold DImode values.
2884    See fix_truncsfdi2.  */
2885 #define TF_MODES64 (DF_MODES64 | TF_ONLY_MODES)
2886
2887 /* Modes for condition codes.  */
2888 #define CC_MODES (1 << (int) CC_MODE)
2889 #define CCFP_MODES (1 << (int) CCFP_MODE)
2890
2891 /* Value is 1 if register/mode pair is acceptable on sparc.
2892    The funny mixture of D and T modes is because integer operations
2893    do not specially operate on tetra quantities, so non-quad-aligned
2894    registers can hold quadword quantities (except %o4 and %i4 because
2895    they cross fixed registers).  */
2896
2897 /* This points to either the 32 bit or the 64 bit version.  */
2898 int *hard_regno_mode_classes;
2899
2900 static int hard_32bit_mode_classes[] = {
2901   S_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
2902   T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
2903   T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
2904   T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
2905
2906   TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
2907   TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
2908   TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
2909   TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
2910
2911   /* FP regs f32 to f63.  Only the even numbered registers actually exist,
2912      and none can hold SFmode/SImode values.  */
2913   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2914   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2915   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2916   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2917
2918   /* %fcc[0123] */
2919   CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
2920
2921   /* %icc */
2922   CC_MODES
2923 };
2924
2925 static int hard_64bit_mode_classes[] = {
2926   D_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
2927   T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
2928   T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
2929   T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
2930
2931   TF_MODES64, SF_MODES, DF_MODES64, SF_MODES, TF_MODES64, SF_MODES, DF_MODES64, SF_MODES,
2932   TF_MODES64, SF_MODES, DF_MODES64, SF_MODES, TF_MODES64, SF_MODES, DF_MODES64, SF_MODES,
2933   TF_MODES64, SF_MODES, DF_MODES64, SF_MODES, TF_MODES64, SF_MODES, DF_MODES64, SF_MODES,
2934   TF_MODES64, SF_MODES, DF_MODES64, SF_MODES, TF_MODES64, SF_MODES, DF_MODES64, SF_MODES,
2935
2936   /* FP regs f32 to f63.  Only the even numbered registers actually exist,
2937      and none can hold SFmode/SImode values.  */
2938   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2939   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2940   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2941   DF_UP_MODES, 0, DF_ONLY_MODES, 0, DF_UP_MODES, 0, DF_ONLY_MODES, 0,
2942
2943   /* %fcc[0123] */
2944   CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
2945
2946   /* %icc */
2947   CC_MODES
2948 };
2949
2950 int sparc_mode_class [NUM_MACHINE_MODES];
2951
2952 enum reg_class sparc_regno_reg_class[FIRST_PSEUDO_REGISTER];
2953
2954 static void
2955 sparc_init_modes ()
2956 {
2957   int i;
2958
2959   for (i = 0; i < NUM_MACHINE_MODES; i++)
2960     {
2961       switch (GET_MODE_CLASS (i))
2962         {
2963         case MODE_INT:
2964         case MODE_PARTIAL_INT:
2965         case MODE_COMPLEX_INT:
2966           if (GET_MODE_SIZE (i) <= 4)
2967             sparc_mode_class[i] = 1 << (int) S_MODE;
2968           else if (GET_MODE_SIZE (i) == 8)
2969             sparc_mode_class[i] = 1 << (int) D_MODE;
2970           else if (GET_MODE_SIZE (i) == 16)
2971             sparc_mode_class[i] = 1 << (int) T_MODE;
2972           else if (GET_MODE_SIZE (i) == 32)
2973             sparc_mode_class[i] = 1 << (int) O_MODE;
2974           else 
2975             sparc_mode_class[i] = 0;
2976           break;
2977         case MODE_FLOAT:
2978         case MODE_COMPLEX_FLOAT:
2979           if (GET_MODE_SIZE (i) <= 4)
2980             sparc_mode_class[i] = 1 << (int) SF_MODE;
2981           else if (GET_MODE_SIZE (i) == 8)
2982             sparc_mode_class[i] = 1 << (int) DF_MODE;
2983           else if (GET_MODE_SIZE (i) == 16)
2984             sparc_mode_class[i] = 1 << (int) TF_MODE;
2985           else if (GET_MODE_SIZE (i) == 32)
2986             sparc_mode_class[i] = 1 << (int) OF_MODE;
2987           else 
2988             sparc_mode_class[i] = 0;
2989           break;
2990         case MODE_CC:
2991         default:
2992           /* mode_class hasn't been initialized yet for EXTRA_CC_MODES, so
2993              we must explicitly check for them here.  */
2994           if (i == (int) CCFPmode || i == (int) CCFPEmode)
2995             sparc_mode_class[i] = 1 << (int) CCFP_MODE;
2996           else if (i == (int) CCmode || i == (int) CC_NOOVmode
2997                    || i == (int) CCXmode || i == (int) CCX_NOOVmode)
2998             sparc_mode_class[i] = 1 << (int) CC_MODE;
2999           else
3000             sparc_mode_class[i] = 0;
3001           break;
3002         }
3003     }
3004
3005   if (TARGET_ARCH64)
3006     hard_regno_mode_classes = hard_64bit_mode_classes;
3007   else
3008     hard_regno_mode_classes = hard_32bit_mode_classes;
3009
3010   /* Initialize the array used by REGNO_REG_CLASS.  */
3011   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3012     {
3013       if (i < 32)
3014         sparc_regno_reg_class[i] = GENERAL_REGS;
3015       else if (i < 64)
3016         sparc_regno_reg_class[i] = FP_REGS;
3017       else if (i < 96)
3018         sparc_regno_reg_class[i] = EXTRA_FP_REGS;
3019       else if (i < 100)
3020         sparc_regno_reg_class[i] = FPCC_REGS;
3021       else
3022         sparc_regno_reg_class[i] = NO_REGS;
3023     }
3024 }
3025 \f
3026 /* Save non call used registers from LOW to HIGH at BASE+OFFSET.
3027    N_REGS is the number of 4-byte regs saved thus far.  This applies even to
3028    v9 int regs as it simplifies the code.  */
3029
3030 static int
3031 save_regs (file, low, high, base, offset, n_regs, real_offset)
3032      FILE *file;
3033      int low, high;
3034      char *base;
3035      int offset;
3036      int n_regs;
3037      int real_offset;
3038 {
3039   int i;
3040
3041   if (TARGET_ARCH64 && high <= 32)
3042     {
3043       for (i = low; i < high; i++)
3044         {
3045           if (regs_ever_live[i] && ! call_used_regs[i])
3046             {
3047               fprintf (file, "\tstx %s,[%s+%d]\n",
3048                        reg_names[i], base, offset + 4 * n_regs);
3049               if (dwarf2out_do_frame ())
3050                 dwarf2out_reg_save ("", i, real_offset + 4 * n_regs);
3051               n_regs += 2;
3052             }
3053         }
3054     }
3055   else
3056     {
3057       for (i = low; i < high; i += 2)
3058         {
3059           if (regs_ever_live[i] && ! call_used_regs[i])
3060             if (regs_ever_live[i+1] && ! call_used_regs[i+1])
3061               {
3062                 fprintf (file, "\tstd %s,[%s+%d]\n",
3063                          reg_names[i], base, offset + 4 * n_regs);
3064                 if (dwarf2out_do_frame ())
3065                   {
3066                     char *l = dwarf2out_cfi_label ();
3067                     dwarf2out_reg_save (l, i, real_offset + 4 * n_regs);
3068                     dwarf2out_reg_save (l, i+1, real_offset + 4 * n_regs + 4);
3069                   }
3070                 n_regs += 2;
3071               }
3072             else
3073               {
3074                 fprintf (file, "\tst %s,[%s+%d]\n",
3075                          reg_names[i], base, offset + 4 * n_regs);
3076                 if (dwarf2out_do_frame ())
3077                   dwarf2out_reg_save ("", i, real_offset + 4 * n_regs);
3078                 n_regs += 2;
3079               }
3080           else if (regs_ever_live[i+1] && ! call_used_regs[i+1])
3081             {
3082               fprintf (file, "\tst %s,[%s+%d]\n",
3083                        reg_names[i+1], base, offset + 4 * n_regs + 4);
3084               if (dwarf2out_do_frame ())
3085                 dwarf2out_reg_save ("", i + 1, real_offset + 4 * n_regs + 4);
3086               n_regs += 2;
3087             }
3088         }
3089     }
3090   return n_regs;
3091 }
3092
3093 /* Restore non call used registers from LOW to HIGH at BASE+OFFSET.
3094
3095    N_REGS is the number of 4-byte regs saved thus far.  This applies even to
3096    v9 int regs as it simplifies the code.  */
3097
3098 static int
3099 restore_regs (file, low, high, base, offset, n_regs)
3100      FILE *file;
3101      int low, high;
3102      char *base;
3103      int offset;
3104      int n_regs;
3105 {
3106   int i;
3107
3108   if (TARGET_ARCH64 && high <= 32)
3109     {
3110       for (i = low; i < high; i++)
3111         {
3112           if (regs_ever_live[i] && ! call_used_regs[i])
3113             fprintf (file, "\tldx [%s+%d], %s\n",
3114               base, offset + 4 * n_regs, reg_names[i]),
3115             n_regs += 2;
3116         }
3117     }
3118   else
3119     {
3120       for (i = low; i < high; i += 2)
3121         {
3122           if (regs_ever_live[i] && ! call_used_regs[i])
3123             if (regs_ever_live[i+1] && ! call_used_regs[i+1])
3124               fprintf (file, "\tldd [%s+%d], %s\n",
3125                        base, offset + 4 * n_regs, reg_names[i]),
3126               n_regs += 2;
3127             else
3128               fprintf (file, "\tld [%s+%d],%s\n",
3129                        base, offset + 4 * n_regs, reg_names[i]),
3130               n_regs += 2;
3131           else if (regs_ever_live[i+1] && ! call_used_regs[i+1])
3132             fprintf (file, "\tld [%s+%d],%s\n",
3133                      base, offset + 4 * n_regs + 4, reg_names[i+1]),
3134             n_regs += 2;
3135         }
3136     }
3137   return n_regs;
3138 }
3139
3140 /* Static variables we want to share between prologue and epilogue.  */
3141
3142 /* Number of live general or floating point registers needed to be saved
3143    (as 4-byte quantities).  This is only done if TARGET_EPILOGUE.  */
3144 static int num_gfregs;
3145
3146 /* Compute the frame size required by the function.  This function is called
3147    during the reload pass and also by output_function_prologue().  */
3148
3149 int
3150 compute_frame_size (size, leaf_function)
3151      int size;
3152      int leaf_function;
3153 {
3154   int n_regs = 0, i;
3155   int outgoing_args_size = (current_function_outgoing_args_size
3156                             + REG_PARM_STACK_SPACE (current_function_decl));
3157
3158   if (TARGET_EPILOGUE)
3159     {
3160       /* N_REGS is the number of 4-byte regs saved thus far.  This applies
3161          even to v9 int regs to be consistent with save_regs/restore_regs.  */
3162
3163       if (TARGET_ARCH64)
3164         {
3165           for (i = 0; i < 8; i++)
3166             if (regs_ever_live[i] && ! call_used_regs[i])
3167               n_regs += 2;
3168         }
3169       else
3170         {
3171           for (i = 0; i < 8; i += 2)
3172             if ((regs_ever_live[i] && ! call_used_regs[i])
3173                 || (regs_ever_live[i+1] && ! call_used_regs[i+1]))
3174               n_regs += 2;
3175         }
3176
3177       for (i = 32; i < (TARGET_V9 ? 96 : 64); i += 2)
3178         if ((regs_ever_live[i] && ! call_used_regs[i])
3179             || (regs_ever_live[i+1] && ! call_used_regs[i+1]))
3180           n_regs += 2;
3181     }
3182
3183   /* Set up values for use in `function_epilogue'.  */
3184   num_gfregs = n_regs;
3185
3186   if (leaf_function && n_regs == 0
3187       && size == 0 && current_function_outgoing_args_size == 0)
3188     {
3189       actual_fsize = apparent_fsize = 0;
3190     }
3191   else
3192     {
3193       /* We subtract STARTING_FRAME_OFFSET, remember it's negative.
3194          The stack bias (if any) is taken out to undo its effects.  */
3195       apparent_fsize = (size - STARTING_FRAME_OFFSET + SPARC_STACK_BIAS + 7) & -8;
3196       apparent_fsize += n_regs * 4;
3197       actual_fsize = apparent_fsize + ((outgoing_args_size + 7) & -8);
3198     }
3199
3200   /* Make sure nothing can clobber our register windows.
3201      If a SAVE must be done, or there is a stack-local variable,
3202      the register window area must be allocated.
3203      ??? For v8 we apparently need an additional 8 bytes of reserved space.  */
3204   if (leaf_function == 0 || size > 0)
3205     actual_fsize += (16 * UNITS_PER_WORD) + (TARGET_ARCH64 ? 0 : 8);
3206
3207   return SPARC_STACK_ALIGN (actual_fsize);
3208 }
3209
3210 /* Build a (32 bit) big number in a register.  */
3211 /* ??? We may be able to use the set macro here too.  */
3212
3213 static void
3214 build_big_number (file, num, reg)
3215      FILE *file;
3216      int num;
3217      char *reg;
3218 {
3219   if (num >= 0 || ! TARGET_ARCH64)
3220     {
3221       fprintf (file, "\tsethi %%hi(%d),%s\n", num, reg);
3222       if ((num & 0x3ff) != 0)
3223         fprintf (file, "\tor %s,%%lo(%d),%s\n", reg, num, reg);
3224     }
3225   else /* num < 0 && TARGET_ARCH64 */
3226     {
3227       /* Sethi does not sign extend, so we must use a little trickery
3228          to use it for negative numbers.  Invert the constant before
3229          loading it in, then use xor immediate to invert the loaded bits
3230          (along with the upper 32 bits) to the desired constant.  This
3231          works because the sethi and immediate fields overlap.  */
3232       int asize = num;
3233       int inv = ~asize;
3234       int low = -0x400 + (asize & 0x3FF);
3235           
3236       fprintf (file, "\tsethi %%hi(%d),%s\n\txor %s,%d,%s\n",
3237                inv, reg, reg, low, reg);
3238     }
3239 }
3240
3241 /* Output code for the function prologue.  */
3242
3243 void
3244 output_function_prologue (file, size, leaf_function)
3245      FILE *file;
3246      int size;
3247      int leaf_function;
3248 {
3249   /* Need to use actual_fsize, since we are also allocating
3250      space for our callee (and our own register save area).  */
3251   actual_fsize = compute_frame_size (size, leaf_function);
3252
3253   if (leaf_function)
3254     {
3255       frame_base_name = "%sp";
3256       frame_base_offset = actual_fsize + SPARC_STACK_BIAS;
3257     }
3258   else
3259     {
3260       frame_base_name = "%fp";
3261       frame_base_offset = SPARC_STACK_BIAS;
3262     }
3263
3264   /* This is only for the human reader.  */
3265   fprintf (file, "\t%s#PROLOGUE# 0\n", ASM_COMMENT_START);
3266
3267   if (actual_fsize == 0)
3268     /* do nothing.  */ ;
3269   else if (! leaf_function && ! TARGET_BROKEN_SAVERESTORE)
3270     {
3271       if (actual_fsize <= 4096)
3272         fprintf (file, "\tsave %%sp,-%d,%%sp\n", actual_fsize);
3273       else if (actual_fsize <= 8192)
3274         {
3275           fprintf (file, "\tsave %%sp,-4096,%%sp\n");
3276           fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize - 4096);
3277         }
3278       else
3279         {
3280           build_big_number (file, -actual_fsize, "%g1");
3281           fprintf (file, "\tsave %%sp,%%g1,%%sp\n");
3282         }
3283     }
3284   else if (! leaf_function && TARGET_BROKEN_SAVERESTORE)
3285     {
3286       /* We assume the environment will properly handle or otherwise avoid
3287          trouble associated with an interrupt occuring after the `save' or
3288          trap occuring during it.  */
3289       fprintf (file, "\tsave\n");
3290
3291       if (actual_fsize <= 4096)
3292         fprintf (file, "\tadd %%fp,-%d,%%sp\n", actual_fsize);
3293       else if (actual_fsize <= 8192)
3294         {
3295           fprintf (file, "\tadd %%fp,-4096,%%sp\n");
3296           fprintf (file, "\tadd %%fp,-%d,%%sp\n", actual_fsize - 4096);
3297         }
3298       else
3299         {
3300           build_big_number (file, -actual_fsize, "%g1");
3301           fprintf (file, "\tadd %%fp,%%g1,%%sp\n");
3302         }
3303     }
3304   else /* leaf function */
3305     {
3306       if (actual_fsize <= 4096)
3307         fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize);
3308       else if (actual_fsize <= 8192)
3309         {
3310           fprintf (file, "\tadd %%sp,-4096,%%sp\n");
3311           fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize - 4096);
3312         }
3313       else
3314         {
3315           build_big_number (file, -actual_fsize, "%g1");
3316           fprintf (file, "\tadd %%sp,%%g1,%%sp\n");
3317         }
3318     }
3319
3320   if (dwarf2out_do_frame () && actual_fsize)
3321     {
3322       char *label = dwarf2out_cfi_label ();
3323
3324       /* The canonical frame address refers to the top of the frame.  */
3325       dwarf2out_def_cfa (label, (leaf_function ? STACK_POINTER_REGNUM
3326                                  : FRAME_POINTER_REGNUM),
3327                          frame_base_offset);
3328
3329       if (! leaf_function)
3330         {
3331           /* Note the register window save.  This tells the unwinder that
3332              it needs to restore the window registers from the previous
3333              frame's window save area at 0(cfa).  */
3334           dwarf2out_window_save (label);
3335
3336           /* The return address (-8) is now in %i7.  */
3337           dwarf2out_return_reg (label, 31);
3338         }
3339     }
3340
3341   /* If doing anything with PIC, do it now.  */
3342   if (! flag_pic)
3343     fprintf (file, "\t%s#PROLOGUE# 1\n", ASM_COMMENT_START);
3344
3345   /* Call saved registers are saved just above the outgoing argument area.  */
3346   if (num_gfregs)
3347     {
3348       int offset, real_offset, n_regs;
3349       char *base;
3350
3351       real_offset = -apparent_fsize;
3352       offset = -apparent_fsize + frame_base_offset;
3353       if (offset < -4096 || offset + num_gfregs * 4 > 4096)
3354         {
3355           /* ??? This might be optimized a little as %g1 might already have a
3356              value close enough that a single add insn will do.  */
3357           /* ??? Although, all of this is probably only a temporary fix
3358              because if %g1 can hold a function result, then
3359              output_function_epilogue will lose (the result will get
3360              clobbered).  */
3361           build_big_number (file, offset, "%g1");
3362           fprintf (file, "\tadd %s,%%g1,%%g1\n", frame_base_name);
3363           base = "%g1";
3364           offset = 0;
3365         }
3366       else
3367         {
3368           base = frame_base_name;
3369         }
3370
3371       if (TARGET_EPILOGUE && ! leaf_function)
3372         /* ??? Originally saved regs 0-15 here.  */
3373         n_regs = save_regs (file, 0, 8, base, offset, 0, real_offset);
3374       else if (leaf_function)
3375         /* ??? Originally saved regs 0-31 here.  */
3376         n_regs = save_regs (file, 0, 8, base, offset, 0, real_offset);
3377       if (TARGET_EPILOGUE)
3378         save_regs (file, 32, TARGET_V9 ? 96 : 64, base, offset, n_regs,
3379                    real_offset);
3380     }
3381
3382   leaf_label = 0;
3383   if (leaf_function && actual_fsize != 0)
3384     {
3385       /* warning ("leaf procedure with frame size %d", actual_fsize); */
3386       if (! TARGET_EPILOGUE)
3387         leaf_label = gen_label_rtx ();
3388     }
3389 }
3390
3391 /* Output code for the function epilogue.  */
3392
3393 void
3394 output_function_epilogue (file, size, leaf_function)
3395      FILE *file;
3396      int size;
3397      int leaf_function;
3398 {
3399   char *ret;
3400
3401   if (leaf_label)
3402     {
3403       emit_label_after (leaf_label, get_last_insn ());
3404       final_scan_insn (get_last_insn (), file, 0, 0, 1);
3405     }
3406
3407 #ifdef FUNCTION_BLOCK_PROFILER_EXIT
3408   else if (profile_block_flag == 2)
3409     {
3410       FUNCTION_BLOCK_PROFILER_EXIT(file);
3411     }
3412 #endif
3413
3414   /* Restore any call saved registers.  */
3415   if (num_gfregs)
3416     {
3417       int offset, n_regs;
3418       char *base;
3419
3420       offset = -apparent_fsize + frame_base_offset;
3421       if (offset < -4096 || offset + num_gfregs * 4 > 4096 - 8 /*double*/)
3422         {
3423           build_big_number (file, offset, "%g1");
3424           fprintf (file, "\tadd %s,%%g1,%%g1\n", frame_base_name);
3425           base = "%g1";
3426           offset = 0;
3427         }
3428       else
3429         {
3430           base = frame_base_name;
3431         }
3432
3433       if (TARGET_EPILOGUE && ! leaf_function)
3434         /* ??? Originally saved regs 0-15 here.  */
3435         n_regs = restore_regs (file, 0, 8, base, offset, 0);
3436       else if (leaf_function)
3437         /* ??? Originally saved regs 0-31 here.  */
3438         n_regs = restore_regs (file, 0, 8, base, offset, 0);
3439       if (TARGET_EPILOGUE)
3440         restore_regs (file, 32, TARGET_V9 ? 96 : 64, base, offset, n_regs);
3441     }
3442
3443   /* Work out how to skip the caller's unimp instruction if required.  */
3444   if (leaf_function)
3445     ret = (SKIP_CALLERS_UNIMP_P ? "jmp %o7+12" : "retl");
3446   else
3447     ret = (SKIP_CALLERS_UNIMP_P ? "jmp %i7+12" : "ret");
3448
3449   if (TARGET_EPILOGUE || leaf_label)
3450     {
3451       int old_target_epilogue = TARGET_EPILOGUE;
3452       target_flags &= ~old_target_epilogue;
3453
3454       if (! leaf_function)
3455         {
3456           /* If we wound up with things in our delay slot, flush them here.  */
3457           if (current_function_epilogue_delay_list)
3458             {
3459               rtx insn = emit_jump_insn_after (gen_rtx (RETURN, VOIDmode),
3460                                                get_last_insn ());
3461               PATTERN (insn) = gen_rtx (PARALLEL, VOIDmode,
3462                                         gen_rtvec (2,
3463                                                    PATTERN (XEXP (current_function_epilogue_delay_list, 0)),
3464                                                    PATTERN (insn)));
3465               final_scan_insn (insn, file, 1, 0, 1);
3466             }
3467           else
3468             fprintf (file, "\t%s\n\trestore\n", ret);
3469         }
3470       /* All of the following cases are for leaf functions.  */
3471       else if (current_function_epilogue_delay_list)
3472         {
3473           /* eligible_for_epilogue_delay_slot ensures that if this is a
3474              leaf function, then we will only have insn in the delay slot
3475              if the frame size is zero, thus no adjust for the stack is
3476              needed here.  */
3477           if (actual_fsize != 0)
3478             abort ();
3479           fprintf (file, "\t%s\n", ret);
3480           final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
3481                            file, 1, 0, 1);
3482         }
3483       /* Output 'nop' instead of 'sub %sp,-0,%sp' when no frame, so as to
3484          avoid generating confusing assembly language output.  */
3485       else if (actual_fsize == 0)
3486         fprintf (file, "\t%s\n\tnop\n", ret);
3487       else if (actual_fsize <= 4096)
3488         fprintf (file, "\t%s\n\tsub %%sp,-%d,%%sp\n", ret, actual_fsize);
3489       else if (actual_fsize <= 8192)
3490         fprintf (file, "\tsub %%sp,-4096,%%sp\n\t%s\n\tsub %%sp,-%d,%%sp\n",
3491                  ret, actual_fsize - 4096);
3492       else if ((actual_fsize & 0x3ff) == 0)
3493         fprintf (file, "\tsethi %%hi(%d),%%g1\n\t%s\n\tadd %%sp,%%g1,%%sp\n",
3494                  actual_fsize, ret);
3495       else               
3496         fprintf (file, "\tsethi %%hi(%d),%%g1\n\tor %%g1,%%lo(%d),%%g1\n\t%s\n\tadd %%sp,%%g1,%%sp\n",
3497                  actual_fsize, actual_fsize, ret);
3498       target_flags |= old_target_epilogue;
3499     }
3500 }
3501 \f
3502 /* Functions for handling argument passing.
3503
3504    For v8 the first six args are normally in registers and the rest are
3505    pushed.  Any arg that starts within the first 6 words is at least
3506    partially passed in a register unless its data type forbids.
3507
3508    For v9, the argument registers are laid out as an array of 16 elements
3509    and arguments are added sequentially.  The first 6 int args and up to the
3510    first 16 fp args (depending on size) are passed in regs.
3511
3512    Slot    Stack   Integral   Float   Float in structure   Double   Long Double
3513    ----    -----   --------   -----   ------------------   ------   -----------
3514     15   [SP+248]              %f31       %f30,%f31         %d30
3515     14   [SP+240]              %f29       %f28,%f29         %d28       %q28
3516     13   [SP+232]              %f27       %f26,%f27         %d26
3517     12   [SP+224]              %f25       %f24,%f25         %d24       %q24
3518     11   [SP+216]              %f23       %f22,%f23         %d22
3519     10   [SP+208]              %f21       %f20,%f21         %d20       %q20
3520      9   [SP+200]              %f19       %f18,%f19         %d18
3521      8   [SP+192]              %f17       %f16,%f17         %d16       %q16
3522      7   [SP+184]              %f15       %f14,%f15         %d14
3523      6   [SP+176]              %f13       %f12,%f13         %d12       %q12
3524      5   [SP+168]     %o5      %f11       %f10,%f11         %d10
3525      4   [SP+160]     %o4       %f9        %f8,%f9           %d8        %q8
3526      3   [SP+152]     %o3       %f7        %f6,%f7           %d6
3527      2   [SP+144]     %o2       %f5        %f4,%f5           %d4        %q4
3528      1   [SP+136]     %o1       %f3        %f2,%f3           %d2
3529      0   [SP+128]     %o0       %f1        %f0,%f1           %d0        %q0
3530
3531    Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
3532
3533    Integral arguments are always passed as 64 bit quantities appropriately
3534    extended.
3535
3536    Passing of floating point values is handled as follows.
3537    If a prototype is in scope:
3538      If the value is in a named argument (i.e. not a stdarg function or a
3539      value not part of the `...') then the value is passed in the appropriate
3540      fp reg.
3541      If the value is part of the `...' and is passed in one of the first 6
3542      slots then the value is passed in the appropriate int reg.
3543      If the value is part of the `...' and is not passed in one of the first 6
3544      slots then the value is passed in memory.
3545    If a prototype is not in scope:
3546      If the value is one of the first 6 arguments the value is passed in the
3547      appropriate integer reg and the appropriate fp reg.
3548      If the value is not one of the first 6 arguments the value is passed in
3549      the appropriate fp reg and in memory.
3550    */
3551
3552 /* Maximum number of int regs for args.  */
3553 #define SPARC_INT_ARG_MAX 6
3554 /* Maximum number of fp regs for args.  */
3555 #define SPARC_FP_ARG_MAX 16
3556
3557 #define ROUND_ADVANCE(SIZE) (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
3558
3559 /* Handle the INIT_CUMULATIVE_ARGS macro.
3560    Initialize a variable CUM of type CUMULATIVE_ARGS
3561    for a call to a function whose data type is FNTYPE.
3562    For a library call, FNTYPE is 0.  */
3563
3564 void
3565 init_cumulative_args (cum, fntype, libname, indirect)
3566      CUMULATIVE_ARGS *cum;
3567      tree fntype, libname;
3568      int indirect;
3569 {
3570   cum->words = 0;
3571   cum->prototype_p = fntype && TYPE_ARG_TYPES (fntype);
3572   cum->libcall_p = fntype == 0;
3573 }
3574
3575 /* Compute the slot number to pass an argument in.
3576    Returns the slot number or -1 if passing on the stack.
3577
3578    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3579     the preceding args and about the function being called.
3580    MODE is the argument's machine mode.
3581    TYPE is the data type of the argument (as a tree).
3582     This is null for libcalls where that information may
3583     not be available.
3584    NAMED is nonzero if this argument is a named parameter
3585     (otherwise it is an extra parameter matching an ellipsis).
3586    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
3587    *PREGNO records the register number to use if scalar type.
3588    *PPADDING records the amount of padding needed in words.  */
3589
3590 static int
3591 function_arg_slotno (cum, mode, type, named, incoming_p, pregno, ppadding)
3592      const CUMULATIVE_ARGS *cum;
3593      enum machine_mode mode;
3594      tree type;
3595      int named;
3596      int incoming_p;
3597      int *pregno;
3598      int *ppadding;
3599 {
3600   int regbase = (incoming_p
3601                  ? SPARC_INCOMING_INT_ARG_FIRST
3602                  : SPARC_OUTGOING_INT_ARG_FIRST);
3603   int slotno = cum->words;
3604   int regno;
3605
3606   *ppadding = 0;
3607
3608   if (type != 0 && TREE_ADDRESSABLE (type))
3609     return -1;
3610   if (TARGET_ARCH32
3611       && type != 0 && mode == BLKmode
3612       && TYPE_ALIGN (type) % PARM_BOUNDARY != 0)
3613     return -1;
3614
3615   switch (mode)
3616     {
3617     case VOIDmode :
3618       /* MODE is VOIDmode when generating the actual call.
3619          See emit_call_1.  */
3620       return -1;
3621
3622     case QImode : case CQImode :
3623     case HImode : case CHImode :
3624     case SImode : case CSImode :
3625     case DImode : case CDImode :
3626       if (slotno >= SPARC_INT_ARG_MAX)
3627         return -1;
3628       regno = regbase + slotno;
3629       break;
3630
3631     case SFmode : case SCmode :
3632     case DFmode : case DCmode :
3633     case TFmode : case TCmode :
3634       if (TARGET_ARCH32)
3635         {
3636           if (slotno >= SPARC_INT_ARG_MAX)
3637             return -1;
3638           regno = regbase + slotno;
3639         }
3640       else
3641         {
3642           if ((mode == TFmode || mode == TCmode)
3643               && (slotno & 1) != 0)
3644             slotno++, *ppadding = 1;
3645           if (TARGET_FPU && named)
3646             {
3647               if (slotno >= SPARC_FP_ARG_MAX)
3648                 return 0;
3649               regno = SPARC_FP_ARG_FIRST + slotno * 2;
3650               if (mode == SFmode)
3651                 regno++;
3652             }
3653           else
3654             {
3655               if (slotno >= SPARC_INT_ARG_MAX)
3656                 return -1;
3657               regno = regbase + slotno;
3658             }
3659         }
3660       break;
3661
3662     case BLKmode :
3663       /* For sparc64, objects requiring 16 byte alignment get it.  */
3664       if (TARGET_ARCH64)
3665         {
3666           if (type && TYPE_ALIGN (type) == 128 && (slotno & 1) != 0)
3667             slotno++, *ppadding = 1;
3668         }
3669
3670       if (TARGET_ARCH32
3671           || type && TREE_CODE (type) == UNION_TYPE)
3672         {
3673           if (slotno >= SPARC_INT_ARG_MAX)
3674             return -1;
3675           regno = regbase + slotno;
3676         }
3677       else
3678         {
3679           tree field;
3680           int intregs_p = 0, fpregs_p = 0;
3681           /* The ABI obviously doesn't specify how packed
3682              structures are passed.  These are defined to be passed
3683              in int regs if possible, otherwise memory.  */
3684           int packed_p = 0;
3685
3686           /* First see what kinds of registers we need.  */
3687           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3688             {
3689               if (TREE_CODE (field) == FIELD_DECL)
3690                 {
3691                   if (TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3692                       && TARGET_FPU)
3693                     fpregs_p = 1;
3694                   else
3695                     intregs_p = 1;
3696                   if (DECL_PACKED (field))
3697                     packed_p = 1;
3698                 }
3699             }
3700           if (packed_p || !named)
3701             fpregs_p = 0, intregs_p = 1;
3702
3703           /* If all arg slots are filled, then must pass on stack.  */
3704           if (fpregs_p && slotno >= SPARC_FP_ARG_MAX)
3705             return -1;
3706           /* If there are only int args and all int arg slots are filled,
3707              then must pass on stack.  */
3708           if (!fpregs_p && intregs_p && slotno >= SPARC_INT_ARG_MAX)
3709             return -1;
3710           /* Note that even if all int arg slots are filled, fp members may
3711              still be passed in regs if such regs are available.
3712              *PREGNO isn't set because there may be more than one, it's up
3713              to the caller to compute them.  */
3714           return slotno;
3715         }
3716       break;
3717
3718     default :
3719       abort ();
3720     }
3721
3722   *pregno = regno;
3723   return slotno;
3724 }
3725
3726 /* Handle the FUNCTION_ARG macro.
3727    Determine where to put an argument to a function.
3728    Value is zero to push the argument on the stack,
3729    or a hard register in which to store the argument.
3730
3731    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3732     the preceding args and about the function being called.
3733    MODE is the argument's machine mode.
3734    TYPE is the data type of the argument (as a tree).
3735     This is null for libcalls where that information may
3736     not be available.
3737    NAMED is nonzero if this argument is a named parameter
3738     (otherwise it is an extra parameter matching an ellipsis).
3739    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.  */
3740
3741 rtx
3742 function_arg (cum, mode, type, named, incoming_p)
3743      const CUMULATIVE_ARGS *cum;
3744      enum machine_mode mode;
3745      tree type;
3746      int named;
3747      int incoming_p;
3748 {
3749   int regbase = (incoming_p
3750                  ? SPARC_INCOMING_INT_ARG_FIRST
3751                  : SPARC_OUTGOING_INT_ARG_FIRST);
3752   int slotno, regno, padding;
3753   rtx reg;
3754
3755   slotno = function_arg_slotno (cum, mode, type, named, incoming_p,
3756                                 &regno, &padding);
3757
3758   if (slotno == -1)
3759     return 0;
3760
3761   if (TARGET_ARCH32)
3762     {
3763       reg = gen_rtx (REG, mode, regno);
3764       return reg;
3765     }
3766
3767   /* v9 fp args in reg slots beyond the int reg slots get passed in regs
3768      but also have the slot allocated for them.
3769      If no prototype is in scope fp values in register slots get passed
3770      in two places, either fp regs and int regs or fp regs and memory.  */
3771   if ((GET_MODE_CLASS (mode) == MODE_FLOAT
3772        || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3773       && SPARC_FP_REG_P (regno))
3774     {
3775       reg = gen_rtx (REG, mode, regno);
3776       if (cum->prototype_p || cum->libcall_p)
3777         {
3778           /* "* 2" because fp reg numbers are recorded in 4 byte
3779              quantities.  */
3780           /* ??? This will cause the value to be passed in the fp reg and
3781              in the stack.  When a prototype exists we want to pass the
3782              value in the reg but reserve space on the stack.  That's an
3783              optimization, and is defered [for a bit].  */
3784           if ((regno - SPARC_FP_ARG_FIRST) >= SPARC_INT_ARG_MAX * 2)
3785             return gen_rtx (PARALLEL, mode,
3786                             gen_rtvec (2,
3787                                        gen_rtx (EXPR_LIST, VOIDmode,
3788                                                 NULL_RTX, const0_rtx),
3789                                        gen_rtx (EXPR_LIST, VOIDmode,
3790                                                 reg, const0_rtx)));
3791           else
3792             return reg;
3793         }
3794       else
3795         {
3796           if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
3797             {
3798               int regbase = (incoming_p
3799                              ? SPARC_INCOMING_INT_ARG_FIRST
3800                              : SPARC_OUTGOING_INT_ARG_FIRST);
3801               int intreg = regbase + (regno - SPARC_FP_ARG_FIRST) / 2;
3802               return gen_rtx (PARALLEL, mode,
3803                               gen_rtvec (2,
3804                                          gen_rtx (EXPR_LIST, VOIDmode,
3805                                                   gen_rtx (REG, mode, intreg),
3806                                                   const0_rtx),
3807                                          gen_rtx (EXPR_LIST, VOIDmode,
3808                                                   reg, const0_rtx)));
3809             }
3810           else
3811             return gen_rtx (PARALLEL, mode,
3812                             gen_rtvec (2,
3813                                        gen_rtx (EXPR_LIST, VOIDmode,
3814                                                 NULL_RTX, const0_rtx),
3815                                        gen_rtx (EXPR_LIST, VOIDmode,
3816                                                 reg, const0_rtx)));
3817         }
3818     }
3819   else if (type && TREE_CODE (type) == RECORD_TYPE)
3820     {
3821       /* Structures up to 16 bytes in size are passed in arg slots on the
3822          stack and are promoted to registers where possible.  */
3823       tree field;
3824       rtx ret;
3825       int i;
3826       int nregs;
3827       /* Starting bit position of a sequence of integer fields, counted from
3828          msb of left most byte, -1 if last field wasn't an int.  */
3829       /* ??? This isn't entirely necessary, some simplification
3830          may be possible.  */
3831       int start_int_bitpos;
3832       /* Current bitpos in struct, counted from msb of left most byte.  */
3833       int bitpos, this_slotno;
3834       /* The ABI obviously doesn't specify how packed
3835          structures are passed.  These are defined to be passed
3836          in int regs if possible, otherwise memory.  */
3837       int packed_p = 0;
3838
3839       if (int_size_in_bytes (type) > 16)
3840         abort (); /* shouldn't get here */
3841
3842       /* We need to compute how many registers are needed so we can allocate
3843          the PARALLEL but before we can do that we need to know whether there
3844          are any packed fields.  If there are, int regs are used regardless of
3845          whether there are fp values present.  */
3846       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3847         {
3848           if (TREE_CODE (field) == FIELD_DECL
3849               && DECL_PACKED (field))
3850             {
3851               packed_p = 1;
3852               break;
3853             }
3854         }
3855
3856       /* Compute how many registers we need.  */
3857       nregs = 0;
3858       start_int_bitpos = -1;
3859       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3860         {
3861           bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
3862           this_slotno = slotno + bitpos / BITS_PER_WORD;
3863           if (TREE_CODE (field) == FIELD_DECL)
3864             {
3865               if (TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3866                   && TARGET_FPU
3867                   && ! packed_p
3868                   && named)
3869                 {
3870                   /* There's no need to check this_slotno < SPARC_FP_ARG MAX.
3871                      If it wasn't true we wouldn't be here.  */
3872                   nregs++;
3873                   start_int_bitpos = -1;
3874                 }
3875               else if (this_slotno < SPARC_INT_ARG_MAX)
3876                 {
3877                   if (start_int_bitpos == -1)
3878                     {
3879                       nregs++;
3880                       start_int_bitpos = bitpos;
3881                     }
3882                   else
3883                     {
3884                       if (bitpos % BITS_PER_WORD == 0)
3885                         nregs++;
3886                     }
3887                 }
3888             }
3889         }
3890       if (nregs == 0)
3891         abort ();
3892
3893       ret = gen_rtx (PARALLEL, BLKmode, rtvec_alloc (nregs + 1));
3894
3895       /* ??? This causes the entire struct to be passed in memory.
3896          This isn't necessary, but is left for later.  */
3897       XVECEXP (ret, 0, 0) = gen_rtx (EXPR_LIST, VOIDmode, NULL_RTX,
3898                                      const0_rtx);
3899
3900       /* Fill in the entries.  */
3901       start_int_bitpos = -1;
3902       for (i = 1, field = TYPE_FIELDS (type);
3903            field;
3904            field = TREE_CHAIN (field))
3905         {
3906           bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
3907           this_slotno = slotno + bitpos / BITS_PER_WORD;
3908           if (TREE_CODE (field) == FIELD_DECL)
3909             {
3910               if (TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3911                   && TARGET_FPU
3912                   && ! packed_p
3913                   && named)
3914                 {
3915                   reg = gen_rtx (REG, DECL_MODE (field),
3916                                  (SPARC_FP_ARG_FIRST + this_slotno * 2
3917                                   + (DECL_MODE (field) == SFmode
3918                                      && (bitpos & 32) != 0)));
3919                   XVECEXP (ret, 0, i) = gen_rtx (EXPR_LIST, VOIDmode, reg,
3920                                                  GEN_INT (bitpos / BITS_PER_UNIT));
3921                   i++;
3922                   start_int_bitpos = -1;
3923                 }
3924               else
3925                 {
3926                   if (this_slotno < SPARC_INT_ARG_MAX
3927                       && (start_int_bitpos == -1
3928                           || bitpos % BITS_PER_WORD == 0))
3929                     {
3930                       enum machine_mode mode;
3931
3932                       /* If this is the trailing part of a word, only load
3933                          that much into the register.  Otherwise load the
3934                          whole register.  Note that in the latter case we may
3935                          pick up unwanted bits.  It's not a problem at the
3936                          moment but may wish to revisit.  */
3937                       if (bitpos % BITS_PER_WORD != 0)
3938                         mode = mode_for_size (BITS_PER_WORD - bitpos % BITS_PER_WORD,
3939                                               MODE_INT, 0);
3940                       else
3941                         mode = word_mode;
3942
3943                       regno = regbase + this_slotno;
3944                       reg = gen_rtx (REG, mode, regno);
3945                       XVECEXP (ret, 0, i) = gen_rtx (EXPR_LIST, VOIDmode, reg,
3946                                                      GEN_INT (bitpos / BITS_PER_UNIT));
3947                       i++;
3948                       if (start_int_bitpos == -1)
3949                         start_int_bitpos = bitpos;
3950                     }
3951                 }
3952             }
3953         }
3954       if (i != nregs + 1)
3955         abort ();
3956
3957       return ret;
3958     }
3959   else if (type && TREE_CODE (type) == UNION_TYPE)
3960     {
3961       enum machine_mode mode;
3962       int bytes = int_size_in_bytes (type);
3963
3964       if (bytes > 16)
3965         abort ();
3966
3967       mode = mode_for_size (bytes * BITS_PER_UNIT, MODE_INT, 0);
3968       reg = gen_rtx (REG, mode, regno);
3969     }
3970   else
3971     {
3972       /* Scalar or complex int.  */
3973       reg = gen_rtx (REG, mode, regno);
3974     }
3975
3976   return reg;
3977 }
3978
3979 /* Handle the FUNCTION_ARG_PARTIAL_NREGS macro.
3980    For an arg passed partly in registers and partly in memory,
3981    this is the number of registers used.
3982    For args passed entirely in registers or entirely in memory, zero.
3983
3984    Any arg that starts in the first 6 regs but won't entirely fit in them
3985    needs partial registers on v8.  On v9, structures with integer
3986    values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
3987    values that begin in the last fp reg [where "last fp reg" varies with the
3988    mode] will be split between that reg and memory.  */
3989
3990 int
3991 function_arg_partial_nregs (cum, mode, type, named)
3992      const CUMULATIVE_ARGS *cum;
3993      enum machine_mode mode;
3994      tree type;
3995      int named;
3996 {
3997   int slotno, regno, padding;
3998
3999   /* We pass 0 for incoming_p here, it doesn't matter.  */
4000   slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
4001
4002   if (slotno == -1)
4003     return 0;
4004
4005   if (TARGET_ARCH32)
4006     {
4007       if ((slotno + (mode == BLKmode
4008                      ? ROUND_ADVANCE (int_size_in_bytes (type))
4009                      : ROUND_ADVANCE (GET_MODE_SIZE (mode))))
4010           > NPARM_REGS (SImode))
4011         return NPARM_REGS (SImode) - slotno;
4012       return 0;
4013     }
4014   else
4015     {
4016       if (type && AGGREGATE_TYPE_P (type))
4017         {
4018           int size = int_size_in_bytes (type);
4019           int align = TYPE_ALIGN (type);
4020
4021           if (align == 16)
4022             slotno += slotno & 1;
4023           if (size > 8 && size <= 16
4024               && slotno == SPARC_INT_ARG_MAX - 1)
4025             return 1;
4026         }
4027       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
4028                || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4029                    && ! TARGET_FPU))
4030         {
4031           if (GET_MODE_ALIGNMENT (mode) == 128)
4032             {
4033               slotno += slotno & 1;
4034               if (slotno == SPARC_INT_ARG_MAX - 2)
4035                 return 1;
4036             }
4037           else
4038             {
4039               if (slotno == SPARC_INT_ARG_MAX - 1)
4040                 return 1;
4041             }
4042         }
4043       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4044         {
4045           if (GET_MODE_ALIGNMENT (mode) == 128)
4046             slotno += slotno & 1;
4047           if ((slotno + GET_MODE_SIZE (mode) / UNITS_PER_WORD)
4048               > SPARC_FP_ARG_MAX)
4049             return 1;
4050         }
4051       return 0;
4052     }
4053 }
4054
4055 /* Handle the FUNCTION_ARG_PASS_BY_REFERENCE macro.
4056    !v9: The SPARC ABI stipulates passing struct arguments (of any size) and
4057    quad-precision floats by invisible reference.
4058    v9: aggregates greater than 16 bytes are passed by reference.
4059    For Pascal, also pass arrays by reference.  */
4060
4061 int
4062 function_arg_pass_by_reference (cum, mode, type, named)
4063      const CUMULATIVE_ARGS *cum;
4064      enum machine_mode mode;
4065      tree type;
4066      int named;
4067 {
4068   if (TARGET_ARCH32)
4069     {
4070       return (type && AGGREGATE_TYPE_P (type)
4071               || mode == TFmode || mode == TCmode);
4072     }
4073   else
4074     {
4075       return ((type && TREE_CODE (type) == ARRAY_TYPE)
4076               || (type && AGGREGATE_TYPE_P (type)
4077                   && int_size_in_bytes (type) > 16));
4078     }
4079 }
4080
4081 /* Handle the FUNCTION_ARG_ADVANCE macro.
4082    Update the data in CUM to advance over an argument
4083    of mode MODE and data type TYPE.
4084    TYPE is null for libcalls where that information may not be available.  */
4085
4086 void
4087 function_arg_advance (cum, mode, type, named)
4088      CUMULATIVE_ARGS *cum;
4089      enum machine_mode mode;
4090      tree type;
4091      int named;
4092 {
4093   int slotno, regno, padding;
4094
4095   /* We pass 0 for incoming_p here, it doesn't matter.  */
4096   slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
4097
4098   /* If register required leading padding, add it.  */
4099   if (slotno != -1)
4100     cum->words += padding;
4101
4102   if (TARGET_ARCH32)
4103     {
4104       cum->words += (mode != BLKmode
4105                      ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
4106                      : ROUND_ADVANCE (int_size_in_bytes (type)));
4107     }
4108   else
4109     {
4110       if (type && AGGREGATE_TYPE_P (type))
4111         {
4112           int size = int_size_in_bytes (type);
4113
4114           if (size <= 8)
4115             ++cum->words;
4116           else if (size <= 16)
4117             cum->words += 2;
4118           else /* passed by reference */
4119             ++cum->words;
4120         }
4121       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
4122         {
4123           cum->words += 2;
4124         }
4125       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4126         {
4127           cum->words += GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4128         }
4129       else
4130         {
4131           cum->words += (mode != BLKmode
4132                          ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
4133                          : ROUND_ADVANCE (int_size_in_bytes (type)));
4134         }
4135     }
4136 }
4137
4138 /* Handle the FUNCTION_ARG_PADDING macro.
4139    For the 64 bit ABI structs are always stored left shifted in their
4140    argument slot.  */
4141
4142 enum direction
4143 function_arg_padding (mode, type)
4144      enum machine_mode mode;
4145      tree type;
4146 {
4147   if (TARGET_ARCH64 && type && TREE_CODE (type) == RECORD_TYPE)
4148     {
4149       return upward;
4150     }
4151
4152   /* This is the default definition.  */
4153   return (! BYTES_BIG_ENDIAN
4154           ? upward
4155           : ((mode == BLKmode
4156               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4157                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
4158               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
4159              ? downward : upward));
4160 }
4161 \f
4162 /* Do what is necessary for `va_start'.  The argument is ignored.
4163
4164    We look at the current function to determine if stdarg or varargs
4165    is used and return the address of the first unnamed parameter.  */
4166
4167 rtx
4168 sparc_builtin_saveregs (arglist)
4169      tree arglist;
4170 {
4171   tree fntype = TREE_TYPE (current_function_decl);
4172   int stdarg = (TYPE_ARG_TYPES (fntype) != 0
4173                 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4174                     != void_type_node));
4175   int first_reg = current_function_args_info.words;
4176   rtx address;
4177   int regno;
4178
4179   for (regno = first_reg; regno < NPARM_REGS (word_mode); regno++)
4180     emit_move_insn (gen_rtx (MEM, word_mode,
4181                              gen_rtx (PLUS, Pmode,
4182                                       frame_pointer_rtx,
4183                                       GEN_INT (STACK_POINTER_OFFSET
4184                                                + UNITS_PER_WORD * regno))),
4185                     gen_rtx (REG, word_mode,
4186                              BASE_INCOMING_ARG_REG (word_mode) + regno));
4187
4188   address = gen_rtx (PLUS, Pmode,
4189                      frame_pointer_rtx,
4190                      GEN_INT (STACK_POINTER_OFFSET
4191                               + UNITS_PER_WORD * first_reg));
4192
4193   if (flag_check_memory_usage
4194       && first_reg < NPARM_REGS (word_mode))
4195     emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
4196                        address, ptr_mode,
4197                        GEN_INT (UNITS_PER_WORD 
4198                                 * (NPARM_REGS (word_mode) - first_reg)),
4199                        TYPE_MODE (sizetype), GEN_INT (MEMORY_USE_RW), QImode);
4200
4201   return address;
4202 }
4203 \f
4204 /* Return the string to output a conditional branch to LABEL, which is
4205    the operand number of the label.  OP is the conditional expression.
4206    XEXP (OP, 0) is assumed to be a condition code register (integer or
4207    floating point) and its mode specifies what kind of comparison we made.
4208
4209    REVERSED is non-zero if we should reverse the sense of the comparison.
4210
4211    ANNUL is non-zero if we should generate an annulling branch.
4212
4213    NOOP is non-zero if we have to follow this branch by a noop.  */
4214
4215 char *
4216 output_cbranch (op, label, reversed, annul, noop)
4217      rtx op;
4218      int label;
4219      int reversed, annul, noop;
4220 {
4221   static char string[20];
4222   enum rtx_code code = GET_CODE (op);
4223   rtx cc_reg = XEXP (op, 0);
4224   enum machine_mode mode = GET_MODE (cc_reg);
4225   static char v8_labelno[] = " %lX";
4226   static char v9_icc_labelno[] = " %%icc,%lX";
4227   static char v9_xcc_labelno[] = " %%xcc,%lX";
4228   static char v9_fcc_labelno[] = " %%fccX,%lY";
4229   char *labelno;
4230   int labeloff;
4231
4232   /* ??? !v9: FP branches cannot be preceded by another floating point insn.
4233      Because there is currently no concept of pre-delay slots, we can fix
4234      this only by always emitting a nop before a floating point branch.  */
4235
4236   if ((mode == CCFPmode || mode == CCFPEmode) && ! TARGET_V9)
4237     strcpy (string, "nop\n\t");
4238   else
4239     string[0] = '\0';
4240
4241   /* If not floating-point or if EQ or NE, we can just reverse the code.  */
4242   if (reversed
4243       && ((mode != CCFPmode && mode != CCFPEmode) || code == EQ || code == NE))
4244     code = reverse_condition (code), reversed = 0;
4245
4246   /* Start by writing the branch condition.  */
4247   switch (code)
4248     {
4249     case NE:
4250       if (mode == CCFPmode || mode == CCFPEmode)
4251         strcat (string, "fbne");
4252       else
4253         strcpy (string, "bne");
4254       break;
4255
4256     case EQ:
4257       if (mode == CCFPmode || mode == CCFPEmode)
4258         strcat (string, "fbe");
4259       else
4260         strcpy (string, "be");
4261       break;
4262
4263     case GE:
4264       if (mode == CCFPmode || mode == CCFPEmode)
4265         {
4266           if (reversed)
4267             strcat (string, "fbul");
4268           else
4269             strcat (string, "fbge");
4270         }
4271       else if (mode == CC_NOOVmode)
4272         strcpy (string, "bpos");
4273       else
4274         strcpy (string, "bge");
4275       break;
4276
4277     case GT:
4278       if (mode == CCFPmode || mode == CCFPEmode)
4279         {
4280           if (reversed)
4281             strcat (string, "fbule");
4282           else
4283             strcat (string, "fbg");
4284         }
4285       else
4286         strcpy (string, "bg");
4287       break;
4288
4289     case LE:
4290       if (mode == CCFPmode || mode == CCFPEmode)
4291         {
4292           if (reversed)
4293             strcat (string, "fbug");
4294           else
4295             strcat (string, "fble");
4296         }
4297       else
4298         strcpy (string, "ble");
4299       break;
4300
4301     case LT:
4302       if (mode == CCFPmode || mode == CCFPEmode)
4303         {
4304           if (reversed)
4305             strcat (string, "fbuge");
4306           else
4307             strcat (string, "fbl");
4308         }
4309       else if (mode == CC_NOOVmode)
4310         strcpy (string, "bneg");
4311       else
4312         strcpy (string, "bl");
4313       break;
4314
4315     case GEU:
4316       strcpy (string, "bgeu");
4317       break;
4318
4319     case GTU:
4320       strcpy (string, "bgu");
4321       break;
4322
4323     case LEU:
4324       strcpy (string, "bleu");
4325       break;
4326
4327     case LTU:
4328       strcpy (string, "blu");
4329       break;
4330     }
4331
4332   /* Now add the annulling, the label, and a possible noop.  */
4333   if (annul)
4334     strcat (string, ",a");
4335
4336   /* ??? If v9, optional prediction bit ",pt" or ",pf" goes here.  */
4337
4338   if (! TARGET_V9)
4339     {
4340       labeloff = 3;
4341       labelno = v8_labelno;
4342     }
4343   else
4344     {
4345       labeloff = 9;
4346       if (mode == CCFPmode || mode == CCFPEmode)
4347         {
4348           labeloff = 10;
4349           labelno = v9_fcc_labelno;
4350           /* Set the char indicating the number of the fcc reg to use.  */
4351           labelno[6] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
4352         }
4353       else if (mode == CCXmode || mode == CCX_NOOVmode)
4354         labelno = v9_xcc_labelno;
4355       else
4356         labelno = v9_icc_labelno;
4357     }
4358   /* Set the char indicating the number of the operand containing the
4359      label_ref.  */
4360   labelno[labeloff] = label + '0';
4361   strcat (string, labelno);
4362
4363   if (noop)
4364     strcat (string, "\n\tnop");
4365
4366   return string;
4367 }
4368
4369 /* Return the string to output a conditional branch to LABEL, testing
4370    register REG.  LABEL is the operand number of the label; REG is the
4371    operand number of the reg.  OP is the conditional expression.  The mode
4372    of REG says what kind of comparison we made.
4373
4374    REVERSED is non-zero if we should reverse the sense of the comparison.
4375
4376    ANNUL is non-zero if we should generate an annulling branch.
4377
4378    NOOP is non-zero if we have to follow this branch by a noop.  */
4379
4380 char *
4381 output_v9branch (op, reg, label, reversed, annul, noop)
4382      rtx op;
4383      int reg, label;
4384      int reversed, annul, noop;
4385 {
4386   static char string[20];
4387   enum rtx_code code = GET_CODE (op);
4388   enum machine_mode mode = GET_MODE (XEXP (op, 0));
4389   static char labelno[] = " %X,%lX";
4390
4391   /* If not floating-point or if EQ or NE, we can just reverse the code.  */
4392   if (reversed)
4393     code = reverse_condition (code), reversed = 0;
4394
4395   /* Only 64 bit versions of these instructions exist.  */
4396   if (mode != DImode)
4397     abort ();
4398
4399   /* Start by writing the branch condition.  */
4400
4401   switch (code)
4402     {
4403     case NE:
4404       strcpy (string, "brnz");
4405       break;
4406
4407     case EQ:
4408       strcpy (string, "brz");
4409       break;
4410
4411     case GE:
4412       strcpy (string, "brgez");
4413       break;
4414
4415     case LT:
4416       strcpy (string, "brlz");
4417       break;
4418
4419     case LE:
4420       strcpy (string, "brlez");
4421       break;
4422
4423     case GT:
4424       strcpy (string, "brgz");
4425       break;
4426
4427     default:
4428       abort ();
4429     }
4430
4431   /* Now add the annulling, reg, label, and nop.  */
4432   if (annul)
4433     strcat (string, ",a");
4434
4435   /* ??? Optional prediction bit ",pt" or ",pf" goes here.  */
4436
4437   labelno[2] = reg + '0';
4438   labelno[6] = label + '0';
4439   strcat (string, labelno);
4440
4441   if (noop)
4442     strcat (string, "\n\tnop");
4443
4444   return string;
4445 }
4446
4447 /* Output assembler code to return from a function.  */
4448
4449 /* ??? v9: Update to use the new `return' instruction.  Also, add patterns to
4450    md file for the `return' instruction.  */
4451
4452 char *
4453 output_return (operands)
4454      rtx *operands;
4455 {
4456   if (leaf_label)
4457     {
4458       operands[0] = leaf_label;
4459       return "b,a %l0";
4460     }
4461   else if (leaf_function)
4462     {
4463       /* If we didn't allocate a frame pointer for the current function,
4464          the stack pointer might have been adjusted.  Output code to
4465          restore it now.  */
4466
4467       operands[0] = gen_rtx (CONST_INT, VOIDmode, actual_fsize);
4468
4469       /* Use sub of negated value in first two cases instead of add to
4470          allow actual_fsize == 4096.  */
4471
4472       if (actual_fsize <= 4096)
4473         {
4474           if (SKIP_CALLERS_UNIMP_P)
4475             return "jmp %%o7+12\n\tsub %%sp,-%0,%%sp";
4476           else
4477             return "retl\n\tsub %%sp,-%0,%%sp";
4478         }
4479       else if (actual_fsize <= 8192)
4480         {
4481           operands[0] = gen_rtx (CONST_INT, VOIDmode, actual_fsize - 4096);
4482           if (SKIP_CALLERS_UNIMP_P)
4483             return "sub %%sp,-4096,%%sp\n\tjmp %%o7+12\n\tsub %%sp,-%0,%%sp";
4484           else
4485             return "sub %%sp,-4096,%%sp\n\tretl\n\tsub %%sp,-%0,%%sp";
4486         }
4487       else if (SKIP_CALLERS_UNIMP_P)
4488         {
4489           if ((actual_fsize & 0x3ff) != 0)
4490             return "sethi %%hi(%a0),%%g1\n\tor %%g1,%%lo(%a0),%%g1\n\tjmp %%o7+12\n\tadd %%sp,%%g1,%%sp";
4491           else
4492             return "sethi %%hi(%a0),%%g1\n\tjmp %%o7+12\n\tadd %%sp,%%g1,%%sp";
4493         }
4494       else
4495         {
4496           if ((actual_fsize & 0x3ff) != 0)
4497             return "sethi %%hi(%a0),%%g1\n\tor %%g1,%%lo(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
4498           else
4499             return "sethi %%hi(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
4500         }
4501     }
4502   else
4503     {
4504       if (SKIP_CALLERS_UNIMP_P)
4505         return "jmp %%i7+12\n\trestore";
4506       else
4507         return "ret\n\trestore";
4508     }
4509 }
4510 \f
4511 /* Leaf functions and non-leaf functions have different needs.  */
4512
4513 static int
4514 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
4515
4516 static int
4517 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
4518
4519 static int *reg_alloc_orders[] = {
4520   reg_leaf_alloc_order,
4521   reg_nonleaf_alloc_order};
4522
4523 void
4524 order_regs_for_local_alloc ()
4525 {
4526   static int last_order_nonleaf = 1;
4527
4528   if (regs_ever_live[15] != last_order_nonleaf)
4529     {
4530       last_order_nonleaf = !last_order_nonleaf;
4531       bcopy ((char *) reg_alloc_orders[last_order_nonleaf],
4532              (char *) reg_alloc_order, FIRST_PSEUDO_REGISTER * sizeof (int));
4533     }
4534 }
4535 \f
4536 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
4537    This makes them candidates for using ldd and std insns. 
4538
4539    Note reg1 and reg2 *must* be hard registers.  To be sure we will
4540    abort if we are passed pseudo registers.  */
4541
4542 int
4543 registers_ok_for_ldd_peep (reg1, reg2)
4544      rtx reg1, reg2;
4545 {
4546   /* We might have been passed a SUBREG.  */
4547   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
4548     return 0;
4549
4550   if (REGNO (reg1) % 2 != 0)
4551     return 0;
4552
4553   return (REGNO (reg1) == REGNO (reg2) - 1);
4554 }
4555
4556 /* Return 1 if addr1 and addr2 are suitable for use in an ldd or 
4557    std insn.
4558
4559    This can only happen when addr1 and addr2 are consecutive memory
4560    locations (addr1 + 4 == addr2).  addr1 must also be aligned on a 
4561    64 bit boundary (addr1 % 8 == 0).  
4562
4563    We know %sp and %fp are kept aligned on a 64 bit boundary.  Other
4564    registers are assumed to *never* be properly aligned and are 
4565    rejected.
4566
4567    Knowing %sp and %fp are kept aligned on a 64 bit boundary, we 
4568    need only check that the offset for addr1 % 8 == 0.  */
4569
4570 int
4571 addrs_ok_for_ldd_peep (addr1, addr2)
4572       rtx addr1, addr2;
4573 {
4574   int reg1, offset1;
4575
4576   /* Extract a register number and offset (if used) from the first addr.  */
4577   if (GET_CODE (addr1) == PLUS)
4578     {
4579       /* If not a REG, return zero.  */
4580       if (GET_CODE (XEXP (addr1, 0)) != REG)
4581         return 0;
4582       else
4583         {
4584           reg1 = REGNO (XEXP (addr1, 0));
4585           /* The offset must be constant!  */
4586           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
4587             return 0;
4588           offset1 = INTVAL (XEXP (addr1, 1));
4589         }
4590     }
4591   else if (GET_CODE (addr1) != REG)
4592     return 0;
4593   else
4594     {
4595       reg1 = REGNO (addr1);
4596       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
4597       offset1 = 0;
4598     }
4599
4600   /* Make sure the second address is a (mem (plus (reg) (const_int).  */
4601   if (GET_CODE (addr2) != PLUS)
4602     return 0;
4603
4604   if (GET_CODE (XEXP (addr2, 0)) != REG
4605       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
4606     return 0;
4607
4608   /* Only %fp and %sp are allowed.  Additionally both addresses must
4609      use the same register.  */
4610   if (reg1 != FRAME_POINTER_REGNUM && reg1 != STACK_POINTER_REGNUM)
4611     return 0;
4612
4613   if (reg1 != REGNO (XEXP (addr2, 0)))
4614     return 0;
4615
4616   /* The first offset must be evenly divisible by 8 to ensure the 
4617      address is 64 bit aligned.  */
4618   if (offset1 % 8 != 0)
4619     return 0;
4620
4621   /* The offset for the second addr must be 4 more than the first addr.  */
4622   if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
4623     return 0;
4624
4625   /* All the tests passed.  addr1 and addr2 are valid for ldd and std
4626      instructions.  */
4627   return 1;
4628 }
4629
4630 /* Return 1 if reg is a pseudo, or is the first register in 
4631    a hard register pair.  This makes it a candidate for use in
4632    ldd and std insns.  */
4633
4634 int
4635 register_ok_for_ldd (reg)
4636      rtx reg;
4637 {
4638   /* We might have been passed a SUBREG.  */
4639   if (GET_CODE (reg) != REG) 
4640     return 0;
4641
4642   if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
4643     return (REGNO (reg) % 2 == 0);
4644   else 
4645     return 1;
4646 }
4647 \f
4648 /* Print operand X (an rtx) in assembler syntax to file FILE.
4649    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
4650    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
4651
4652 void
4653 print_operand (file, x, code)
4654      FILE *file;
4655      rtx x;
4656      int code;
4657 {
4658   switch (code)
4659     {
4660     case '#':
4661       /* Output a 'nop' if there's nothing for the delay slot.  */
4662       if (dbr_sequence_length () == 0)
4663         fputs ("\n\tnop", file);
4664       return;
4665     case '*':
4666       /* Output an annul flag if there's nothing for the delay slot and we
4667          are optimizing.  This is always used with '(' below.  */
4668       /* Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
4669          this is a dbx bug.  So, we only do this when optimizing.  */
4670       if (dbr_sequence_length () == 0 && optimize)
4671         fputs (",a", file);
4672       return;
4673     case '(':
4674       /* Output a 'nop' if there's nothing for the delay slot and we are
4675          not optimizing.  This is always used with '*' above.  */
4676       if (dbr_sequence_length () == 0 && ! optimize)
4677         fputs ("\n\tnop", file);
4678       return;
4679     case '_':
4680       /* Output the Medium/Anywhere code model base register.  */
4681       fputs (MEDANY_BASE_REG, file);
4682       return;
4683     case '@':
4684       /* Print out what we are using as the frame pointer.  This might
4685          be %fp, or might be %sp+offset.  */
4686       /* ??? What if offset is too big? Perhaps the caller knows it isn't? */
4687       fprintf (file, "%s+%d", frame_base_name, frame_base_offset);
4688       return;
4689     case 'Y':
4690       /* Adjust the operand to take into account a RESTORE operation.  */
4691       if (GET_CODE (x) != REG)
4692         output_operand_lossage ("Invalid %%Y operand");
4693       else if (REGNO (x) < 8)
4694         fputs (reg_names[REGNO (x)], file);
4695       else if (REGNO (x) >= 24 && REGNO (x) < 32)
4696         fputs (reg_names[REGNO (x)-16], file);
4697       else
4698         output_operand_lossage ("Invalid %%Y operand");
4699       return;
4700     case 'L':
4701       /* Print out the low order register name of a register pair.  */
4702       if (WORDS_BIG_ENDIAN)
4703         fputs (reg_names[REGNO (x)+1], file);
4704       else
4705         fputs (reg_names[REGNO (x)], file);
4706       return;
4707     case 'H':
4708       /* Print out the high order register name of a register pair.  */
4709       if (WORDS_BIG_ENDIAN)
4710         fputs (reg_names[REGNO (x)], file);
4711       else
4712         fputs (reg_names[REGNO (x)+1], file);
4713       return;
4714     case 'R':
4715       /* Print out the second register name of a register pair or quad.
4716          I.e., R (%o0) => %o1.  */
4717       fputs (reg_names[REGNO (x)+1], file);
4718       return;
4719     case 'S':
4720       /* Print out the third register name of a register quad.
4721          I.e., S (%o0) => %o2.  */
4722       fputs (reg_names[REGNO (x)+2], file);
4723       return;
4724     case 'T':
4725       /* Print out the fourth register name of a register quad.
4726          I.e., T (%o0) => %o3.  */
4727       fputs (reg_names[REGNO (x)+3], file);
4728       return;
4729     case 'x':
4730       /* Print a condition code register.  */
4731       if (REGNO (x) == SPARC_ICC_REG)
4732         {
4733           /* We don't handle CC[X]_NOOVmode because they're not supposed
4734              to occur here.  */
4735           if (GET_MODE (x) == CCmode)
4736             fputs ("%icc", file);
4737           else if (GET_MODE (x) == CCXmode)
4738             fputs ("%xcc", file);
4739           else
4740             abort ();
4741         }
4742       else
4743         /* %fccN register */
4744         fputs (reg_names[REGNO (x)], file);
4745       return;
4746     case 'm':
4747       /* Print the operand's address only.  */
4748       output_address (XEXP (x, 0));
4749       return;
4750     case 'r':
4751       /* In this case we need a register.  Use %g0 if the
4752          operand is const0_rtx.  */
4753       if (x == const0_rtx
4754           || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
4755         {
4756           fputs ("%g0", file);
4757           return;
4758         }
4759       else
4760         break;
4761
4762     case 'A':
4763       switch (GET_CODE (x))
4764         {
4765         case IOR: fputs ("or", file); break;
4766         case AND: fputs ("and", file); break;
4767         case XOR: fputs ("xor", file); break;
4768         default: output_operand_lossage ("Invalid %%A operand");
4769         }
4770       return;
4771
4772     case 'B':
4773       switch (GET_CODE (x))
4774         {
4775         case IOR: fputs ("orn", file); break;
4776         case AND: fputs ("andn", file); break;
4777         case XOR: fputs ("xnor", file); break;
4778         default: output_operand_lossage ("Invalid %%B operand");
4779         }
4780       return;
4781
4782       /* These are used by the conditional move instructions.  */
4783     case 'c' :
4784     case 'C':
4785       {
4786         enum rtx_code rc = (code == 'c'
4787                             ? reverse_condition (GET_CODE (x))
4788                             : GET_CODE (x));
4789         switch (rc)
4790           {
4791           case NE: fputs ("ne", file); break;
4792           case EQ: fputs ("e", file); break;
4793           case GE: fputs ("ge", file); break;
4794           case GT: fputs ("g", file); break;
4795           case LE: fputs ("le", file); break;
4796           case LT: fputs ("l", file); break;
4797           case GEU: fputs ("geu", file); break;
4798           case GTU: fputs ("gu", file); break;
4799           case LEU: fputs ("leu", file); break;
4800           case LTU: fputs ("lu", file); break;
4801           default: output_operand_lossage (code == 'c'
4802                                            ? "Invalid %%c operand"
4803                                            : "Invalid %%C operand");
4804           }
4805         return;
4806       }
4807
4808       /* These are used by the movr instruction pattern.  */
4809     case 'd':
4810     case 'D':
4811       {
4812         enum rtx_code rc = (code == 'd'
4813                             ? reverse_condition (GET_CODE (x))
4814                             : GET_CODE (x));
4815         switch (rc)
4816           {
4817           case NE: fputs ("ne", file); break;
4818           case EQ: fputs ("e", file); break;
4819           case GE: fputs ("gez", file); break;
4820           case LT: fputs ("lz", file); break;
4821           case LE: fputs ("lez", file); break;
4822           case GT: fputs ("gz", file); break;
4823           default: output_operand_lossage (code == 'd'
4824                                            ? "Invalid %%d operand"
4825                                            : "Invalid %%D operand");
4826           }
4827         return;
4828       }
4829
4830     case 'b':
4831       {
4832         /* Print a sign-extended character.  */
4833         int i = INTVAL (x) & 0xff;
4834         if (i & 0x80)
4835           i |= 0xffffff00;
4836         fprintf (file, "%d", i);
4837         return;
4838       }
4839
4840     case 'f':
4841       /* Operand must be a MEM; write its address.  */
4842       if (GET_CODE (x) != MEM)
4843         output_operand_lossage ("Invalid %%f operand");
4844       output_address (XEXP (x, 0));
4845       return;
4846
4847     case 0:
4848       /* Do nothing special.  */
4849       break;
4850
4851     default:
4852       /* Undocumented flag.  */
4853       output_operand_lossage ("invalid operand output code");
4854     }
4855
4856   if (GET_CODE (x) == REG)
4857     fputs (reg_names[REGNO (x)], file);
4858   else if (GET_CODE (x) == MEM)
4859     {
4860       fputc ('[', file);
4861         /* Poor Sun assembler doesn't understand absolute addressing.  */
4862       if (CONSTANT_P (XEXP (x, 0))
4863           && ! TARGET_LIVE_G0)
4864         fputs ("%g0+", file);
4865       output_address (XEXP (x, 0));
4866       fputc (']', file);
4867     }
4868   else if (GET_CODE (x) == HIGH)
4869     {
4870       fputs ("%hi(", file);
4871       output_addr_const (file, XEXP (x, 0));
4872       fputc (')', file);
4873     }
4874   else if (GET_CODE (x) == LO_SUM)
4875     {
4876       print_operand (file, XEXP (x, 0), 0);
4877       fputs ("+%lo(", file);
4878       output_addr_const (file, XEXP (x, 1));
4879       fputc (')', file);
4880     }
4881   else if (GET_CODE (x) == CONST_DOUBLE
4882            && (GET_MODE (x) == VOIDmode
4883                || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
4884     {
4885       if (CONST_DOUBLE_HIGH (x) == 0)
4886         fprintf (file, "%u", CONST_DOUBLE_LOW (x));
4887       else if (CONST_DOUBLE_HIGH (x) == -1
4888                && CONST_DOUBLE_LOW (x) < 0)
4889         fprintf (file, "%d", CONST_DOUBLE_LOW (x));
4890       else
4891         output_operand_lossage ("long long constant not a valid immediate operand");
4892     }
4893   else if (GET_CODE (x) == CONST_DOUBLE)
4894     output_operand_lossage ("floating point constant not a valid immediate operand");
4895   else { output_addr_const (file, x); }
4896 }
4897 \f
4898 /* This function outputs assembler code for VALUE to FILE, where VALUE is
4899    a 64 bit (DImode) value.  */
4900
4901 /* ??? If there is a 64 bit counterpart to .word that the assembler
4902    understands, then using that would simply this code greatly.  */
4903 /* ??? We only output .xword's for symbols and only then in environments
4904    where the assembler can handle them.  */
4905
4906 void
4907 output_double_int (file, value)
4908      FILE *file;
4909      rtx value;
4910 {
4911   if (GET_CODE (value) == CONST_INT)
4912     {
4913       /* ??? This has endianness issues.  */
4914 #if HOST_BITS_PER_WIDE_INT == 64
4915       HOST_WIDE_INT xword = INTVAL (value);
4916       HOST_WIDE_INT high, low;
4917
4918       high = (xword >> 32) & 0xffffffff;
4919       low  = xword & 0xffffffff;
4920       ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode, high));
4921       ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode, low));
4922 #else
4923       if (INTVAL (value) < 0)
4924         ASM_OUTPUT_INT (file, constm1_rtx);
4925       else
4926         ASM_OUTPUT_INT (file, const0_rtx);
4927       ASM_OUTPUT_INT (file, value);
4928 #endif
4929     }
4930   else if (GET_CODE (value) == CONST_DOUBLE)
4931     {
4932       ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode,
4933                                      CONST_DOUBLE_HIGH (value)));
4934       ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode,
4935                                      CONST_DOUBLE_LOW (value)));
4936     }
4937   else if (GET_CODE (value) == SYMBOL_REF
4938            || GET_CODE (value) == CONST
4939            || GET_CODE (value) == PLUS
4940            || (TARGET_ARCH64 &&
4941                (GET_CODE (value) == LABEL_REF
4942                 || GET_CODE (value) == CODE_LABEL
4943                 || GET_CODE (value) == MINUS)))
4944     {
4945       if (!TARGET_V9 || TARGET_MEDLOW)
4946         {
4947           ASM_OUTPUT_INT (file, const0_rtx);
4948           ASM_OUTPUT_INT (file, value);
4949         }
4950       else
4951         {
4952           fprintf (file, "\t%s\t", ASM_LONGLONG);
4953           output_addr_const (file, value);
4954           fprintf (file, "\n");
4955         }
4956     }
4957   else
4958     abort ();
4959 }
4960 \f
4961 /* Return the value of a code used in the .proc pseudo-op that says
4962    what kind of result this function returns.  For non-C types, we pick
4963    the closest C type.  */
4964
4965 #ifndef CHAR_TYPE_SIZE
4966 #define CHAR_TYPE_SIZE BITS_PER_UNIT
4967 #endif
4968
4969 #ifndef SHORT_TYPE_SIZE
4970 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
4971 #endif
4972
4973 #ifndef INT_TYPE_SIZE
4974 #define INT_TYPE_SIZE BITS_PER_WORD
4975 #endif
4976
4977 #ifndef LONG_TYPE_SIZE
4978 #define LONG_TYPE_SIZE BITS_PER_WORD
4979 #endif
4980
4981 #ifndef LONG_LONG_TYPE_SIZE
4982 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
4983 #endif
4984
4985 #ifndef FLOAT_TYPE_SIZE
4986 #define FLOAT_TYPE_SIZE BITS_PER_WORD
4987 #endif
4988
4989 #ifndef DOUBLE_TYPE_SIZE
4990 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
4991 #endif
4992
4993 #ifndef LONG_DOUBLE_TYPE_SIZE
4994 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
4995 #endif
4996
4997 unsigned long
4998 sparc_type_code (type)
4999      register tree type;
5000 {
5001   register unsigned long qualifiers = 0;
5002   register unsigned shift = 6;
5003
5004   /* Only the first 30 bits of the qualifier are valid.  We must refrain from
5005      setting more, since some assemblers will give an error for this.  Also,
5006      we must be careful to avoid shifts of 32 bits or more to avoid getting
5007      unpredictable results.  */
5008
5009   for (;;)
5010     {
5011       switch (TREE_CODE (type))
5012         {
5013         case ERROR_MARK:
5014           return qualifiers;
5015   
5016         case ARRAY_TYPE:
5017           if (shift < 30)
5018             qualifiers |= (3 << shift);
5019           shift += 2;
5020           type = TREE_TYPE (type);
5021           break;
5022
5023         case FUNCTION_TYPE:
5024         case METHOD_TYPE:
5025           if (shift < 30)
5026             qualifiers |= (2 << shift);
5027           shift += 2;
5028           type = TREE_TYPE (type);
5029           break;
5030
5031         case POINTER_TYPE:
5032         case REFERENCE_TYPE:
5033         case OFFSET_TYPE:
5034           if (shift < 30)
5035             qualifiers |= (1 << shift);
5036           shift += 2;
5037           type = TREE_TYPE (type);
5038           break;
5039
5040         case RECORD_TYPE:
5041           return (qualifiers | 8);
5042
5043         case UNION_TYPE:
5044         case QUAL_UNION_TYPE:
5045           return (qualifiers | 9);
5046
5047         case ENUMERAL_TYPE:
5048           return (qualifiers | 10);
5049
5050         case VOID_TYPE:
5051           return (qualifiers | 16);
5052
5053         case INTEGER_TYPE:
5054           /* If this is a range type, consider it to be the underlying
5055              type.  */
5056           if (TREE_TYPE (type) != 0)
5057             {
5058               type = TREE_TYPE (type);
5059               break;
5060             }
5061
5062           /* Carefully distinguish all the standard types of C,
5063              without messing up if the language is not C.  We do this by
5064              testing TYPE_PRECISION and TREE_UNSIGNED.  The old code used to
5065              look at both the names and the above fields, but that's redundant.
5066              Any type whose size is between two C types will be considered
5067              to be the wider of the two types.  Also, we do not have a
5068              special code to use for "long long", so anything wider than
5069              long is treated the same.  Note that we can't distinguish
5070              between "int" and "long" in this code if they are the same
5071              size, but that's fine, since neither can the assembler.  */
5072
5073           if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
5074             return (qualifiers | (TREE_UNSIGNED (type) ? 12 : 2));
5075   
5076           else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
5077             return (qualifiers | (TREE_UNSIGNED (type) ? 13 : 3));
5078   
5079           else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
5080             return (qualifiers | (TREE_UNSIGNED (type) ? 14 : 4));
5081   
5082           else
5083             return (qualifiers | (TREE_UNSIGNED (type) ? 15 : 5));
5084   
5085         case REAL_TYPE:
5086           /* Carefully distinguish all the standard types of C,
5087              without messing up if the language is not C.  */
5088
5089           if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
5090             return (qualifiers | 6);
5091
5092           else 
5093             return (qualifiers | 7);
5094   
5095         case COMPLEX_TYPE:      /* GNU Fortran COMPLEX type.  */
5096           /* ??? We need to distinguish between double and float complex types,
5097              but I don't know how yet because I can't reach this code from
5098              existing front-ends.  */
5099           return (qualifiers | 7);      /* Who knows? */
5100
5101         case CHAR_TYPE:         /* GNU Pascal CHAR type.  Not used in C.  */
5102         case BOOLEAN_TYPE:      /* GNU Fortran BOOLEAN type.  */
5103         case FILE_TYPE:         /* GNU Pascal FILE type.  */
5104         case SET_TYPE:          /* GNU Pascal SET type.  */
5105         case LANG_TYPE:         /* ? */
5106           return qualifiers;
5107   
5108         default:
5109           abort ();             /* Not a type! */
5110         }
5111     }
5112 }
5113 \f
5114 /* Nested function support.  */
5115
5116 /* Emit RTL insns to initialize the variable parts of a trampoline.
5117    FNADDR is an RTX for the address of the function's pure code.
5118    CXT is an RTX for the static chain value for the function.
5119
5120    This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
5121    (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
5122    (to store insns).  This is a bit excessive.  Perhaps a different
5123    mechanism would be better here.
5124
5125    Emit enough FLUSH insns to synchronize the data and instruction caches.  */
5126
5127 void
5128 sparc_initialize_trampoline (tramp, fnaddr, cxt)
5129      rtx tramp, fnaddr, cxt;
5130 {
5131   rtx high_cxt = expand_shift (RSHIFT_EXPR, SImode, cxt,
5132                               size_int (10), 0, 1);
5133   rtx high_fn = expand_shift (RSHIFT_EXPR, SImode, fnaddr,
5134                              size_int (10), 0, 1);
5135   rtx low_cxt = expand_and (cxt, gen_rtx (CONST_INT, VOIDmode, 0x3ff), 0);
5136   rtx low_fn = expand_and (fnaddr, gen_rtx (CONST_INT, VOIDmode, 0x3ff), 0);
5137   rtx g1_sethi = gen_rtx (HIGH, SImode,
5138                           gen_rtx (CONST_INT, VOIDmode, 0x03000000));
5139   rtx g2_sethi = gen_rtx (HIGH, SImode,
5140                           gen_rtx (CONST_INT, VOIDmode, 0x05000000));
5141   rtx g1_ori = gen_rtx (HIGH, SImode,
5142                         gen_rtx (CONST_INT, VOIDmode, 0x82106000));
5143   rtx g2_ori = gen_rtx (HIGH, SImode,
5144                         gen_rtx (CONST_INT, VOIDmode, 0x8410A000));
5145   rtx tem = gen_reg_rtx (SImode);
5146   emit_move_insn (tem, g1_sethi);
5147   emit_insn (gen_iorsi3 (high_fn, high_fn, tem));
5148   emit_move_insn (gen_rtx (MEM, SImode, plus_constant (tramp, 0)), high_fn);
5149   emit_move_insn (tem, g1_ori);
5150   emit_insn (gen_iorsi3 (low_fn, low_fn, tem));
5151   emit_move_insn (gen_rtx (MEM, SImode, plus_constant (tramp, 4)), low_fn);
5152   emit_move_insn (tem, g2_sethi);
5153   emit_insn (gen_iorsi3 (high_cxt, high_cxt, tem));
5154   emit_move_insn (gen_rtx (MEM, SImode, plus_constant (tramp, 8)), high_cxt);
5155   emit_move_insn (tem, g2_ori);
5156   emit_insn (gen_iorsi3 (low_cxt, low_cxt, tem));
5157   emit_move_insn (gen_rtx (MEM, SImode, plus_constant (tramp, 16)), low_cxt);
5158   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, SImode, tramp))));
5159   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, SImode,
5160                                                plus_constant (tramp, 8)))));
5161   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, SImode,
5162                                                plus_constant (tramp, 16)))));
5163 }
5164
5165 /* The 64 bit version is simpler because it makes more sense to load the
5166    values as "immediate" data out of the trampoline.  It's also easier since
5167    we can read the PC without clobbering a register.  */
5168
5169 void
5170 sparc64_initialize_trampoline (tramp, fnaddr, cxt)
5171      rtx tramp, fnaddr, cxt;
5172 {
5173   emit_move_insn (gen_rtx (MEM, DImode, plus_constant (tramp, 24)), cxt);
5174   emit_move_insn (gen_rtx (MEM, DImode, plus_constant (tramp, 32)), fnaddr);
5175   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, DImode, tramp))));
5176   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, DImode,
5177                                                plus_constant (tramp, 8)))));
5178   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, DImode,
5179                                                plus_constant (tramp, 16)))));
5180   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, DImode,
5181                                                plus_constant (tramp, 24)))));
5182   emit_insn (gen_flush (validize_mem (gen_rtx (MEM, DImode,
5183                                                plus_constant (tramp, 32)))));
5184 }
5185 \f
5186 /* Subroutines to support a flat (single) register window calling
5187    convention.  */
5188
5189 /* Single-register window sparc stack frames look like:
5190
5191              Before call                        After call
5192         +-----------------------+       +-----------------------+
5193    high |                       |       |                       |
5194    mem  |  caller's temps.      |       |  caller's temps.      |
5195         |                       |       |                       |
5196         +-----------------------+       +-----------------------+
5197         |                       |       |                       |
5198         |  arguments on stack.  |       |  arguments on stack.  |
5199         |                       |       |                       |
5200         +-----------------------+FP+92->+-----------------------+
5201         |  6 words to save      |       |  6 words to save      |
5202         |  arguments passed     |       |  arguments passed     |
5203         |  in registers, even   |       |  in registers, even   |
5204         |  if not passed.       |       |  if not passed.       |
5205  SP+68->+-----------------------+FP+68->+-----------------------+
5206         | 1 word struct addr    |       | 1 word struct addr    |
5207         +-----------------------+FP+64->+-----------------------+
5208         |                       |       |                       |
5209         | 16 word reg save area |       | 16 word reg save area |
5210         |                       |       |                       |
5211     SP->+-----------------------+   FP->+-----------------------+
5212                                         | 4 word area for       |
5213                                         | fp/alu reg moves      |
5214                                  FP-16->+-----------------------+
5215                                         |                       |
5216                                         |  local variables      |
5217                                         |                       |
5218                                         +-----------------------+
5219                                         |                       |
5220                                         |  fp register save     |
5221                                         |                       |
5222                                         +-----------------------+
5223                                         |                       |
5224                                         |  gp register save     |
5225                                         |                       |
5226                                         +-----------------------+
5227                                         |                       |
5228                                         |  alloca allocations   |
5229                                         |                       |
5230                                         +-----------------------+
5231                                         |                       |
5232                                         |  arguments on stack   |
5233                                         |                       |
5234                                  SP+92->+-----------------------+
5235                                         |  6 words to save      |
5236                                         |  arguments passed     |
5237                                         |  in registers, even   |
5238    low                                  |  if not passed.       |
5239    memory                        SP+68->+-----------------------+
5240                                         | 1 word struct addr    |
5241                                  SP+64->+-----------------------+
5242                                         |                       |
5243                                         I 16 word reg save area |
5244                                         |                       |
5245                                     SP->+-----------------------+  */
5246
5247 /* Structure to be filled in by sparc_flat_compute_frame_size with register
5248    save masks, and offsets for the current function.  */
5249
5250 struct sparc_frame_info
5251 {
5252   unsigned long total_size;     /* # bytes that the entire frame takes up.  */
5253   unsigned long var_size;       /* # bytes that variables take up.  */
5254   unsigned long args_size;      /* # bytes that outgoing arguments take up.  */
5255   unsigned long extra_size;     /* # bytes of extra gunk.  */
5256   unsigned int  gp_reg_size;    /* # bytes needed to store gp regs.  */
5257   unsigned int  fp_reg_size;    /* # bytes needed to store fp regs.  */
5258   unsigned long gmask;          /* Mask of saved gp registers.  */
5259   unsigned long fmask;          /* Mask of saved fp registers.  */
5260   unsigned long reg_offset;     /* Offset from new sp to store regs.  */
5261   int           initialized;    /* Nonzero if frame size already calculated.  */
5262 };
5263
5264 /* Current frame information calculated by sparc_flat_compute_frame_size.  */
5265 struct sparc_frame_info current_frame_info;
5266
5267 /* Zero structure to initialize current_frame_info.  */
5268 struct sparc_frame_info zero_frame_info;
5269
5270 /* Tell prologue and epilogue if register REGNO should be saved / restored.  */
5271
5272 #define RETURN_ADDR_REGNUM 15
5273 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
5274 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
5275
5276 #define MUST_SAVE_REGISTER(regno) \
5277  ((regs_ever_live[regno] && !call_used_regs[regno])             \
5278   || (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)    \
5279   || (regno == RETURN_ADDR_REGNUM && regs_ever_live[RETURN_ADDR_REGNUM]))
5280
5281 /* Return the bytes needed to compute the frame pointer from the current
5282    stack pointer.  */
5283
5284 unsigned long
5285 sparc_flat_compute_frame_size (size)
5286      int size;                  /* # of var. bytes allocated.  */
5287 {
5288   int regno;
5289   unsigned long total_size;     /* # bytes that the entire frame takes up.  */
5290   unsigned long var_size;       /* # bytes that variables take up.  */
5291   unsigned long args_size;      /* # bytes that outgoing arguments take up.  */
5292   unsigned long extra_size;     /* # extra bytes.  */
5293   unsigned int  gp_reg_size;    /* # bytes needed to store gp regs.  */
5294   unsigned int  fp_reg_size;    /* # bytes needed to store fp regs.  */
5295   unsigned long gmask;          /* Mask of saved gp registers.  */
5296   unsigned long fmask;          /* Mask of saved fp registers.  */
5297   unsigned long reg_offset;     /* Offset to register save area.  */
5298   int           need_aligned_p; /* 1 if need the save area 8 byte aligned.  */
5299
5300   /* This is the size of the 16 word reg save area, 1 word struct addr
5301      area, and 4 word fp/alu register copy area.  */
5302   extra_size     = -STARTING_FRAME_OFFSET + FIRST_PARM_OFFSET(0);
5303   var_size       = size;
5304   /* Also include the size needed for the 6 parameter registers.  */
5305   args_size      = current_function_outgoing_args_size + 24;
5306   total_size     = var_size + args_size + extra_size;
5307   gp_reg_size    = 0;
5308   fp_reg_size    = 0;
5309   gmask          = 0;
5310   fmask          = 0;
5311   reg_offset     = 0;
5312   need_aligned_p = 0;
5313
5314   /* Calculate space needed for gp registers.  */
5315   for (regno = 1; regno <= 31; regno++)
5316     {
5317       if (MUST_SAVE_REGISTER (regno))
5318         {
5319           /* If we need to save two regs in a row, ensure there's room to bump
5320              up the address to align it to a doubleword boundary.  */
5321           if ((regno & 0x1) == 0 && MUST_SAVE_REGISTER (regno+1))
5322             {
5323               if (gp_reg_size % 8 != 0)
5324                 gp_reg_size += 4;
5325               gp_reg_size += 2 * UNITS_PER_WORD;
5326               gmask |= 3 << regno;
5327               regno++;
5328               need_aligned_p = 1;
5329             }
5330           else
5331             {
5332               gp_reg_size += UNITS_PER_WORD;
5333               gmask |= 1 << regno;
5334             }
5335         }
5336     }
5337
5338   /* Calculate space needed for fp registers.  */
5339   for (regno = 32; regno <= 63; regno++)
5340     {
5341       if (regs_ever_live[regno] && !call_used_regs[regno])
5342         {
5343           fp_reg_size += UNITS_PER_WORD;
5344           fmask |= 1 << (regno - 32);
5345         }
5346     }
5347
5348   if (gmask || fmask)
5349     {
5350       int n;
5351       reg_offset = FIRST_PARM_OFFSET(0) + args_size;
5352       /* Ensure save area is 8 byte aligned if we need it.  */
5353       n = reg_offset % 8;
5354       if (need_aligned_p && n != 0)
5355         {
5356           total_size += 8 - n;
5357           reg_offset += 8 - n;
5358         }
5359       total_size += gp_reg_size + fp_reg_size;
5360     }
5361
5362   /* ??? This looks a little suspicious.  Clarify.  */
5363   if (total_size == extra_size)
5364     total_size = extra_size = 0;
5365
5366   total_size = SPARC_STACK_ALIGN (total_size);
5367
5368   /* Save other computed information.  */
5369   current_frame_info.total_size  = total_size;
5370   current_frame_info.var_size    = var_size;
5371   current_frame_info.args_size   = args_size;
5372   current_frame_info.extra_size  = extra_size;
5373   current_frame_info.gp_reg_size = gp_reg_size;
5374   current_frame_info.fp_reg_size = fp_reg_size;
5375   current_frame_info.gmask       = gmask;
5376   current_frame_info.fmask       = fmask;
5377   current_frame_info.reg_offset  = reg_offset;
5378   current_frame_info.initialized = reload_completed;
5379
5380   /* Ok, we're done.  */
5381   return total_size;
5382 }
5383 \f
5384 /* Save/restore registers in GMASK and FMASK at register BASE_REG plus offset
5385    OFFSET.
5386
5387    BASE_REG must be 8 byte aligned.  This allows us to test OFFSET for
5388    appropriate alignment and use DOUBLEWORD_OP when we can.  We assume
5389    [BASE_REG+OFFSET] will always be a valid address.
5390
5391    WORD_OP is either "st" for save, "ld" for restore.
5392    DOUBLEWORD_OP is either "std" for save, "ldd" for restore.  */
5393
5394 void
5395 sparc_flat_save_restore (file, base_reg, offset, gmask, fmask, word_op,
5396                          doubleword_op, base_offset)
5397      FILE *file;
5398      char *base_reg;
5399      unsigned int offset;
5400      unsigned long gmask;
5401      unsigned long fmask;
5402      char *word_op;
5403      char *doubleword_op;
5404      unsigned long base_offset;
5405 {
5406   int regno;
5407
5408   if (gmask == 0 && fmask == 0)
5409     return;
5410
5411   /* Save registers starting from high to low.  We've already saved the
5412      previous frame pointer and previous return address for the debugger's
5413      sake.  The debugger allows us to not need a nop in the epilog if at least
5414      one register is reloaded in addition to return address.  */
5415
5416   if (gmask)
5417     {
5418       for (regno = 1; regno <= 31; regno++)
5419         {
5420           if ((gmask & (1L << regno)) != 0)
5421             {
5422               if ((regno & 0x1) == 0 && ((gmask & (1L << (regno+1))) != 0))
5423                 {
5424                   /* We can save two registers in a row.  If we're not at a
5425                      double word boundary, move to one.
5426                      sparc_flat_compute_frame_size ensures there's room to do
5427                      this.  */
5428                   if (offset % 8 != 0)
5429                     offset += UNITS_PER_WORD;
5430
5431                   if (word_op[0] == 's')
5432                     {
5433                       fprintf (file, "\t%s %s,[%s+%d]\n",
5434                                doubleword_op, reg_names[regno],
5435                                base_reg, offset);
5436                       if (dwarf2out_do_frame ())
5437                         {
5438                           char *l = dwarf2out_cfi_label ();
5439                           dwarf2out_reg_save (l, regno, offset + base_offset);
5440                           dwarf2out_reg_save
5441                             (l, regno+1, offset+base_offset + UNITS_PER_WORD);
5442                         }
5443                     }
5444                   else
5445                     fprintf (file, "\t%s [%s+%d],%s\n",
5446                              doubleword_op, base_reg, offset,
5447                              reg_names[regno]);
5448
5449                   offset += 2 * UNITS_PER_WORD;
5450                   regno++;
5451                 }
5452               else
5453                 {
5454                   if (word_op[0] == 's')
5455                     {
5456                       fprintf (file, "\t%s %s,[%s+%d]\n",
5457                                word_op, reg_names[regno],
5458                                base_reg, offset);
5459                       if (dwarf2out_do_frame ())
5460                         dwarf2out_reg_save ("", regno, offset + base_offset);
5461                     }
5462                   else
5463                     fprintf (file, "\t%s [%s+%d],%s\n",
5464                              word_op, base_reg, offset, reg_names[regno]);
5465
5466                   offset += UNITS_PER_WORD;
5467                 }
5468             }
5469         }
5470     }
5471
5472   if (fmask)
5473     {
5474       for (regno = 32; regno <= 63; regno++)
5475         {
5476           if ((fmask & (1L << (regno - 32))) != 0)
5477             {
5478               if (word_op[0] == 's')
5479                 {
5480                   fprintf (file, "\t%s %s,[%s+%d]\n",
5481                            word_op, reg_names[regno],
5482                            base_reg, offset);
5483                   if (dwarf2out_do_frame ())
5484                     dwarf2out_reg_save ("", regno, offset + base_offset);
5485                 }
5486               else
5487                 fprintf (file, "\t%s [%s+%d],%s\n",
5488                          word_op, base_reg, offset, reg_names[regno]);
5489
5490               offset += UNITS_PER_WORD;
5491             }
5492         }
5493     }
5494 }
5495 \f
5496 /* Set up the stack and frame (if desired) for the function.  */
5497
5498 void
5499 sparc_flat_output_function_prologue (file, size)
5500      FILE *file;
5501      int size;
5502 {
5503   char *sp_str = reg_names[STACK_POINTER_REGNUM];
5504   unsigned long gmask = current_frame_info.gmask;
5505
5506   /* This is only for the human reader.  */
5507   fprintf (file, "\t%s#PROLOGUE# 0\n", ASM_COMMENT_START);
5508   fprintf (file, "\t%s# vars= %d, regs= %d/%d, args= %d, extra= %d\n",
5509            ASM_COMMENT_START,
5510            current_frame_info.var_size,
5511            current_frame_info.gp_reg_size / 4,
5512            current_frame_info.fp_reg_size / 4,
5513            current_function_outgoing_args_size,
5514            current_frame_info.extra_size);
5515
5516   size = SPARC_STACK_ALIGN (size);
5517   size = (! current_frame_info.initialized
5518           ? sparc_flat_compute_frame_size (size)
5519           : current_frame_info.total_size);
5520
5521   /* These cases shouldn't happen.  Catch them now.  */
5522   if (size == 0 && (gmask || current_frame_info.fmask))
5523     abort ();
5524
5525   /* Allocate our stack frame by decrementing %sp.
5526      At present, the only algorithm gdb can use to determine if this is a
5527      flat frame is if we always set %i7 if we set %sp.  This can be optimized
5528      in the future by putting in some sort of debugging information that says
5529      this is a `flat' function.  However, there is still the case of debugging
5530      code without such debugging information (including cases where most fns
5531      have such info, but there is one that doesn't).  So, always do this now
5532      so we don't get a lot of code out there that gdb can't handle.
5533      If the frame pointer isn't needn't then that's ok - gdb won't be able to
5534      distinguish us from a non-flat function but there won't (and shouldn't)
5535      be any differences anyway.  The return pc is saved (if necessary) right
5536      after %i7 so gdb won't have to look too far to find it.  */
5537   if (size > 0)
5538     {
5539       unsigned int reg_offset = current_frame_info.reg_offset;
5540       char *fp_str = reg_names[FRAME_POINTER_REGNUM];
5541       char *t1_str = "%g1";
5542
5543       /* Things get a little tricky if local variables take up more than ~4096
5544          bytes and outgoing arguments take up more than ~4096 bytes.  When that
5545          happens, the register save area can't be accessed from either end of
5546          the frame.  Handle this by decrementing %sp to the start of the gp
5547          register save area, save the regs, update %i7, and then set %sp to its
5548          final value.  Given that we only have one scratch register to play
5549          with it is the cheapest solution, and it helps gdb out as it won't
5550          slow down recognition of flat functions.
5551          Don't change the order of insns emitted here without checking with
5552          the gdb folk first.  */
5553
5554       /* Is the entire register save area offsettable from %sp?  */
5555       if (reg_offset < 4096 - 64 * UNITS_PER_WORD)
5556         {
5557           if (size <= 4096)
5558             {
5559               fprintf (file, "\tadd %s,%d,%s\n",
5560                        sp_str, -size, sp_str);
5561               if (gmask & FRAME_POINTER_MASK)
5562                 {
5563                   fprintf (file, "\tst %s,[%s+%d]\n",
5564                            fp_str, sp_str, reg_offset);
5565                   fprintf (file, "\tsub %s,%d,%s\t%s# set up frame pointer\n",
5566                            sp_str, -size, fp_str, ASM_COMMENT_START);
5567                   reg_offset += 4;
5568                 }
5569             }
5570           else
5571             {
5572               fprintf (file, "\tset %d,%s\n\tsub %s,%s,%s\n",
5573                        size, t1_str, sp_str, t1_str, sp_str);
5574               if (gmask & FRAME_POINTER_MASK)
5575                 {
5576                   fprintf (file, "\tst %s,[%s+%d]\n",
5577                            fp_str, sp_str, reg_offset);
5578                   fprintf (file, "\tadd %s,%s,%s\t%s# set up frame pointer\n",
5579                            sp_str, t1_str, fp_str, ASM_COMMENT_START);
5580                   reg_offset += 4;
5581                 }
5582             }
5583           if (dwarf2out_do_frame ())
5584             {
5585               char *l = dwarf2out_cfi_label ();
5586               if (gmask & FRAME_POINTER_MASK)
5587                 {
5588                   dwarf2out_reg_save (l, FRAME_POINTER_REGNUM,
5589                                       reg_offset - 4 - size);
5590                   dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, 0);
5591                 }
5592               else
5593                 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size);
5594             }
5595           if (gmask & RETURN_ADDR_MASK)
5596             {
5597               fprintf (file, "\tst %s,[%s+%d]\n",
5598                        reg_names[RETURN_ADDR_REGNUM], sp_str, reg_offset);
5599               if (dwarf2out_do_frame ())
5600                 dwarf2out_return_save ("", reg_offset - size);
5601               reg_offset += 4;
5602             }
5603           sparc_flat_save_restore (file, sp_str, reg_offset,
5604                                    gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
5605                                    current_frame_info.fmask,
5606                                    "st", "std", -size);
5607         }
5608       else
5609         {
5610           /* Subtract %sp in two steps, but make sure there is always a
5611              64 byte register save area, and %sp is properly aligned.  */
5612           /* Amount to decrement %sp by, the first time.  */
5613           unsigned int size1 = ((size - reg_offset + 64) + 15) & -16;
5614           /* Offset to register save area from %sp.  */
5615           unsigned int offset = size1 - (size - reg_offset);
5616           
5617           if (size1 <= 4096)
5618             {
5619               fprintf (file, "\tadd %s,%d,%s\n",
5620                        sp_str, -size1, sp_str);
5621               if (gmask & FRAME_POINTER_MASK)
5622                 {
5623                   fprintf (file, "\tst %s,[%s+%d]\n\tsub %s,%d,%s\t%s# set up frame pointer\n",
5624                            fp_str, sp_str, offset, sp_str, -size1, fp_str,
5625                            ASM_COMMENT_START);
5626                   offset += 4;
5627                 }
5628             }
5629           else
5630             {
5631               fprintf (file, "\tset %d,%s\n\tsub %s,%s,%s\n",
5632                        size1, t1_str, sp_str, t1_str, sp_str);
5633               if (gmask & FRAME_POINTER_MASK)
5634                 {
5635                   fprintf (file, "\tst %s,[%s+%d]\n\tadd %s,%s,%s\t%s# set up frame pointer\n",
5636                            fp_str, sp_str, offset, sp_str, t1_str, fp_str,
5637                            ASM_COMMENT_START);
5638                   offset += 4;
5639                 }
5640             }
5641           if (dwarf2out_do_frame ())
5642             {
5643               char *l = dwarf2out_cfi_label ();
5644               if (gmask & FRAME_POINTER_MASK)
5645                 {
5646                   dwarf2out_reg_save (l, FRAME_POINTER_REGNUM,
5647                                       offset - 4 - size1);
5648                   dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, 0);
5649                 }
5650               else
5651                 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size1);
5652             }
5653           if (gmask & RETURN_ADDR_MASK)
5654             {
5655               fprintf (file, "\tst %s,[%s+%d]\n",
5656                        reg_names[RETURN_ADDR_REGNUM], sp_str, offset);
5657               if (dwarf2out_do_frame ())
5658                 /* offset - size1 == reg_offset - size
5659                    if reg_offset were updated above like offset.  */
5660                 dwarf2out_return_save ("", offset - size1);
5661               offset += 4;
5662             }
5663           sparc_flat_save_restore (file, sp_str, offset,
5664                                    gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
5665                                    current_frame_info.fmask,
5666                                    "st", "std", -size1);
5667           fprintf (file, "\tset %d,%s\n\tsub %s,%s,%s\n",
5668                    size - size1, t1_str, sp_str, t1_str, sp_str);
5669           if (dwarf2out_do_frame ())
5670             if (! (gmask & FRAME_POINTER_MASK))
5671               dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, size);
5672         }
5673     }
5674
5675   fprintf (file, "\t%s#PROLOGUE# 1\n", ASM_COMMENT_START);
5676 }
5677 \f
5678 /* Do any necessary cleanup after a function to restore stack, frame,
5679    and regs. */
5680
5681 void
5682 sparc_flat_output_function_epilogue (file, size)
5683      FILE *file;
5684      int size;
5685 {
5686   rtx epilogue_delay = current_function_epilogue_delay_list;
5687   int noepilogue = FALSE;
5688
5689   /* This is only for the human reader.  */
5690   fprintf (file, "\t%s#EPILOGUE#\n", ASM_COMMENT_START);
5691
5692   /* The epilogue does not depend on any registers, but the stack
5693      registers, so we assume that if we have 1 pending nop, it can be
5694      ignored, and 2 it must be filled (2 nops occur for integer
5695      multiply and divide).  */
5696
5697   size = SPARC_STACK_ALIGN (size);
5698   size = (!current_frame_info.initialized
5699            ? sparc_flat_compute_frame_size (size)
5700            : current_frame_info.total_size);
5701
5702   if (size == 0 && epilogue_delay == 0)
5703     {
5704       rtx insn = get_last_insn ();
5705
5706       /* If the last insn was a BARRIER, we don't have to write any code
5707          because a jump (aka return) was put there.  */
5708       if (GET_CODE (insn) == NOTE)
5709         insn = prev_nonnote_insn (insn);
5710       if (insn && GET_CODE (insn) == BARRIER)
5711         noepilogue = TRUE;
5712     }
5713
5714   if (!noepilogue)
5715     {
5716       unsigned int reg_offset = current_frame_info.reg_offset;
5717       unsigned int size1;
5718       char *sp_str = reg_names[STACK_POINTER_REGNUM];
5719       char *fp_str = reg_names[FRAME_POINTER_REGNUM];
5720       char *t1_str = "%g1";
5721
5722       /* In the reload sequence, we don't need to fill the load delay
5723          slots for most of the loads, also see if we can fill the final
5724          delay slot if not otherwise filled by the reload sequence.  */
5725
5726       if (size > 4095)
5727         fprintf (file, "\tset %d,%s\n", size, t1_str);
5728
5729       if (frame_pointer_needed)
5730         {
5731           if (size > 4095)
5732             fprintf (file,"\tsub %s,%s,%s\t\t%s# sp not trusted here\n",
5733                      fp_str, t1_str, sp_str, ASM_COMMENT_START);
5734           else
5735             fprintf (file,"\tsub %s,%d,%s\t\t%s# sp not trusted here\n",
5736                      fp_str, size, sp_str, ASM_COMMENT_START);
5737         }
5738
5739       /* Is the entire register save area offsettable from %sp?  */
5740       if (reg_offset < 4096 - 64 * UNITS_PER_WORD)
5741         {
5742           size1 = 0;
5743         }
5744       else
5745         {
5746           /* Restore %sp in two steps, but make sure there is always a
5747              64 byte register save area, and %sp is properly aligned.  */
5748           /* Amount to increment %sp by, the first time.  */
5749           size1 = ((reg_offset - 64 - 16) + 15) & -16;
5750           /* Offset to register save area from %sp.  */
5751           reg_offset = size1 - reg_offset;
5752
5753           fprintf (file, "\tset %d,%s\n\tadd %s,%s,%s\n",
5754                    size1, t1_str, sp_str, t1_str, sp_str);
5755         }
5756
5757       /* We must restore the frame pointer and return address reg first
5758          because they are treated specially by the prologue output code.  */
5759       if (current_frame_info.gmask & FRAME_POINTER_MASK)
5760         {
5761           fprintf (file, "\tld [%s+%d],%s\n",
5762                    sp_str, reg_offset, fp_str);
5763           reg_offset += 4;
5764         }
5765       if (current_frame_info.gmask & RETURN_ADDR_MASK)
5766         {
5767           fprintf (file, "\tld [%s+%d],%s\n",
5768                    sp_str, reg_offset, reg_names[RETURN_ADDR_REGNUM]);
5769           reg_offset += 4;
5770         }
5771
5772       /* Restore any remaining saved registers.  */
5773       sparc_flat_save_restore (file, sp_str, reg_offset,
5774                                current_frame_info.gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
5775                                current_frame_info.fmask,
5776                                "ld", "ldd", 0);
5777
5778       /* If we had to increment %sp in two steps, record it so the second
5779          restoration in the epilogue finishes up.  */
5780       if (size1 > 0)
5781         {
5782           size -= size1;
5783           if (size > 4095)
5784             fprintf (file, "\tset %d,%s\n",
5785                      size, t1_str);
5786         }
5787
5788       if (current_function_returns_struct)
5789         fprintf (file, "\tjmp %%o7+12\n");
5790       else
5791         fprintf (file, "\tretl\n");
5792
5793       /* If the only register saved is the return address, we need a
5794          nop, unless we have an instruction to put into it.  Otherwise
5795          we don't since reloading multiple registers doesn't reference
5796          the register being loaded.  */
5797
5798       if (epilogue_delay)
5799         {
5800           if (size)
5801             abort ();
5802           final_scan_insn (XEXP (epilogue_delay, 0), file, 1, -2, 1);
5803         }
5804
5805       else if (size > 4095)
5806         fprintf (file, "\tadd %s,%s,%s\n", sp_str, t1_str, sp_str);
5807
5808       else if (size > 0)
5809         fprintf (file, "\tadd %s,%d,%s\n", sp_str, size, sp_str);
5810
5811       else
5812         fprintf (file, "\tnop\n");
5813     }
5814
5815   /* Reset state info for each function.  */
5816   current_frame_info = zero_frame_info;
5817 }
5818 \f
5819 /* Define the number of delay slots needed for the function epilogue.
5820
5821    On the sparc, we need a slot if either no stack has been allocated,
5822    or the only register saved is the return register.  */
5823
5824 int
5825 sparc_flat_epilogue_delay_slots ()
5826 {
5827   if (!current_frame_info.initialized)
5828     (void) sparc_flat_compute_frame_size (get_frame_size ());
5829
5830   if (current_frame_info.total_size == 0)
5831     return 1;
5832
5833   return 0;
5834 }
5835
5836 /* Return true is TRIAL is a valid insn for the epilogue delay slot.
5837    Any single length instruction which doesn't reference the stack or frame
5838    pointer is OK.  */
5839
5840 int
5841 sparc_flat_eligible_for_epilogue_delay (trial, slot)
5842      rtx trial;
5843      int slot;
5844 {
5845   rtx pat = PATTERN (trial);
5846
5847   if (get_attr_length (trial) != 1)
5848     return 0;
5849
5850   /* If %g0 is live, there are lots of things we can't handle.
5851      Rather than trying to find them all now, let's punt and only
5852      optimize things as necessary.  */
5853   if (TARGET_LIVE_G0)
5854     return 0;
5855
5856   if (! reg_mentioned_p (stack_pointer_rtx, pat)
5857       && ! reg_mentioned_p (frame_pointer_rtx, pat))
5858     return 1;
5859
5860   return 0;
5861 }
5862 \f
5863 /* Adjust the cost of a scheduling dependency.  Return the new cost of
5864    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
5865
5866 int
5867 supersparc_adjust_cost (insn, link, dep_insn, cost)
5868      rtx insn;
5869      rtx link;
5870      rtx dep_insn;
5871      int cost;
5872 {
5873   enum attr_type insn_type;
5874
5875   if (! recog_memoized (insn))
5876     return 0;
5877
5878   insn_type = get_attr_type (insn);
5879
5880   if (REG_NOTE_KIND (link) == 0)
5881     {
5882       /* Data dependency; DEP_INSN writes a register that INSN reads some
5883          cycles later.  */
5884
5885       /* if a load, then the dependence must be on the memory address;
5886          add an extra 'cycle'.  Note that the cost could be two cycles
5887          if the reg was written late in an instruction group; we can't tell
5888          here.  */
5889       if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
5890         return cost + 3;
5891
5892       /* Get the delay only if the address of the store is the dependence.  */
5893       if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
5894         {
5895           rtx pat = PATTERN(insn);
5896           rtx dep_pat = PATTERN (dep_insn);
5897
5898           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
5899             return cost;  /* This shouldn't happen!  */
5900
5901           /* The dependency between the two instructions was on the data that
5902              is being stored.  Assume that this implies that the address of the
5903              store is not dependent.  */
5904           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
5905             return cost;
5906
5907           return cost + 3;  /* An approximation.  */
5908         }
5909
5910       /* A shift instruction cannot receive its data from an instruction
5911          in the same cycle; add a one cycle penalty.  */
5912       if (insn_type == TYPE_SHIFT)
5913         return cost + 3;   /* Split before cascade into shift.  */
5914     }
5915   else
5916     {
5917       /* Anti- or output- dependency; DEP_INSN reads/writes a register that
5918          INSN writes some cycles later.  */
5919
5920       /* These are only significant for the fpu unit; writing a fp reg before
5921          the fpu has finished with it stalls the processor.  */
5922
5923       /* Reusing an integer register causes no problems.  */
5924       if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
5925         return 0;
5926     }
5927         
5928   return cost;
5929 }