OSDN Git Service

* alpha.c (alpha_sa_mask [VMS]): Don't include $26 in the mask.
[pf3gnuchains/gcc-fork.git] / gcc / config / alpha / alpha.c
1 /* Subroutines used for code generation on the DEC Alpha.
2    Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include <stdio.h>
24 #include "config.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "expr.h"
38 #include "obstack.h"
39 #include "tree.h"
40 #include "except.h"
41 #include "function.h"
42
43 /* External data.  */
44 extern char *version_string;
45 extern int rtx_equal_function_value_matters;
46
47 /* Specify which cpu to schedule for. */
48
49 enum processor_type alpha_cpu;
50
51 /* Specify how accurate floating-point traps need to be.  */
52
53 enum alpha_trap_precision alpha_tp;
54
55 /* Specify the floating-point rounding mode.  */
56
57 enum alpha_fp_rounding_mode alpha_fprm;
58
59 /* Specify which things cause traps.  */
60
61 enum alpha_fp_trap_mode alpha_fptm;
62
63 /* Strings decoded into the above options.  */
64
65 char *alpha_cpu_string;         /* -mcpu=ev[4|5] */
66 char *alpha_tp_string;          /* -mtrap-precision=[p|s|i] */
67 char *alpha_fprm_string;        /* -mfp-rounding-mode=[n|m|c|d] */
68 char *alpha_fptm_string;        /* -mfp-trap-mode=[n|u|su|sui] */
69
70 /* Save information from a "cmpxx" operation until the branch or scc is
71    emitted.  */
72
73 rtx alpha_compare_op0, alpha_compare_op1;
74 int alpha_compare_fp_p;
75
76 /* Save the name of the current function as used by the assembler.  This
77    is used by the epilogue.  */
78
79 char *alpha_function_name;
80
81 /* Non-zero if inside of a function, because the Alpha asm can't
82    handle .files inside of functions.  */
83
84 static int inside_function = FALSE;
85
86 /* Nonzero if the current function needs gp.  */
87
88 int alpha_function_needs_gp;
89
90 /* If non-null, this rtx holds the return address for the function.  */
91
92 static rtx alpha_return_addr_rtx;
93
94 /* Declarations of static functions.  */
95 static void alpha_set_memflags_1  PROTO((rtx, int, int, int));
96 static rtx alpha_emit_set_const_1 PROTO((rtx, enum machine_mode,
97                                          HOST_WIDE_INT, int));
98 static void add_long_const      PROTO((FILE *, HOST_WIDE_INT, int, int, int));
99
100 /* Compute the size of the save area in the stack.  */
101 static void alpha_sa_mask       PROTO((unsigned long *imaskP,
102                                        unsigned long *fmaskP));
103
104 /* Strip type information.  */
105 #define CURRENT_FUNCTION_ARGS_INFO  \
106 (TARGET_OPEN_VMS ? current_function_args_info & 0xff \
107  : current_function_args_info)
108
109 /* Some helpful register info.  */
110 #define REG_PV 27
111 #define REG_RA 26
112
113 \f
114 /* Parse target option strings. */
115
116 void
117 override_options ()
118 {
119   alpha_cpu
120     = TARGET_CPU_DEFAULT & MASK_CPU_EV5 ? PROCESSOR_EV5 : PROCESSOR_EV4;
121
122   if (alpha_cpu_string)
123     {
124       if (! strcmp (alpha_cpu_string, "ev4")
125           || ! strcmp (alpha_cpu_string, "21064"))
126         alpha_cpu = PROCESSOR_EV4;
127       else if (! strcmp (alpha_cpu_string, "ev5")
128                || ! strcmp (alpha_cpu_string, "21164"))
129         alpha_cpu = PROCESSOR_EV5;
130       else if (! strcmp (alpha_cpu_string, "ev56")
131                || ! strcmp (alpha_cpu_string, "21164a"))
132         {
133           alpha_cpu = PROCESSOR_EV5;
134           target_flags |= MASK_BYTE_OPS;
135         }
136       else
137         error ("bad value `%s' for -mcpu switch", alpha_cpu_string);
138     }
139
140   alpha_tp = ALPHA_TP_PROG;
141   alpha_fprm = ALPHA_FPRM_NORM;
142   alpha_fptm = ALPHA_FPTM_N;
143
144   if (TARGET_IEEE)
145     {
146       alpha_tp = ALPHA_TP_INSN;
147       alpha_fptm = ALPHA_FPTM_SU;
148     }
149
150   if (TARGET_IEEE_WITH_INEXACT)
151     {
152       alpha_tp = ALPHA_TP_INSN;
153       alpha_fptm = ALPHA_FPTM_SUI;
154     }
155
156   if (alpha_tp_string)
157     {
158       if (! strcmp (alpha_tp_string, "p"))
159         alpha_tp = ALPHA_TP_PROG;
160       else if (! strcmp (alpha_tp_string, "f"))
161         alpha_tp = ALPHA_TP_FUNC;
162       else if (! strcmp (alpha_tp_string, "i"))
163         alpha_tp = ALPHA_TP_INSN;
164       else
165         error ("bad value `%s' for -mtrap-precision switch", alpha_tp_string);
166     }
167
168   if (alpha_fprm_string)
169     {
170       if (! strcmp (alpha_fprm_string, "n"))
171         alpha_fprm = ALPHA_FPRM_NORM;
172       else if (! strcmp (alpha_fprm_string, "m"))
173         alpha_fprm = ALPHA_FPRM_MINF;
174       else if (! strcmp (alpha_fprm_string, "c"))
175         alpha_fprm = ALPHA_FPRM_CHOP;
176       else if (! strcmp (alpha_fprm_string,"d"))
177         alpha_fprm = ALPHA_FPRM_DYN;
178       else
179         error ("bad value `%s' for -mfp-rounding-mode switch",
180                alpha_fprm_string);
181     }
182
183   if (alpha_fptm_string)
184     {
185       if (strcmp (alpha_fptm_string, "n") == 0)
186         alpha_fptm = ALPHA_FPTM_N;
187       else if (strcmp (alpha_fptm_string, "u") == 0)
188         alpha_fptm = ALPHA_FPTM_U;
189       else if (strcmp (alpha_fptm_string, "su") == 0)
190         alpha_fptm = ALPHA_FPTM_SU;
191       else if (strcmp (alpha_fptm_string, "sui") == 0)
192         alpha_fptm = ALPHA_FPTM_SUI;
193       else
194         error ("bad value `%s' for -mfp-trap-mode switch", alpha_fptm_string);
195     }
196
197   /* Do some sanity checks on the above option. */
198
199   if ((alpha_fptm == ALPHA_FPTM_SU || alpha_fptm == ALPHA_FPTM_SUI)
200       && alpha_tp != ALPHA_TP_INSN)
201     {
202       warning ("fp software completion requires -mtrap-precision=i");
203       alpha_tp = ALPHA_TP_INSN;
204     }
205
206   if (TARGET_FLOAT_VAX)
207     {
208       if (alpha_fprm == ALPHA_FPRM_MINF || alpha_fprm == ALPHA_FPRM_DYN)
209         {
210           warning ("rounding mode not supported for VAX floats");
211           alpha_fprm = ALPHA_FPRM_NORM;
212         }
213       if (alpha_fptm == ALPHA_FPTM_SUI)
214         {
215           warning ("trap mode not supported for VAX floats");
216           alpha_fptm = ALPHA_FPTM_SU;
217         }
218     }
219 }
220 \f
221 /* Returns 1 if VALUE is a mask that contains full bytes of zero or ones.  */
222
223 int
224 zap_mask (value)
225      HOST_WIDE_INT value;
226 {
227   int i;
228
229   for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
230        i++, value >>= 8)
231     if ((value & 0xff) != 0 && (value & 0xff) != 0xff)
232       return 0;
233
234   return 1;
235 }
236
237 /* Returns 1 if OP is either the constant zero or a register.  If a
238    register, it must be in the proper mode unless MODE is VOIDmode.  */
239
240 int
241 reg_or_0_operand (op, mode)
242       register rtx op;
243       enum machine_mode mode;
244 {
245   return op == const0_rtx || register_operand (op, mode);
246 }
247
248 /* Return 1 if OP is a constant in the range of 0-63 (for a shift) or
249    any register.  */
250
251 int
252 reg_or_6bit_operand (op, mode)
253      register rtx op;
254      enum machine_mode mode;
255 {
256   return ((GET_CODE (op) == CONST_INT
257            && (unsigned HOST_WIDE_INT) INTVAL (op) < 64)
258           || register_operand (op, mode));
259 }
260
261
262 /* Return 1 if OP is an 8-bit constant or any register.  */
263
264 int
265 reg_or_8bit_operand (op, mode)
266      register rtx op;
267      enum machine_mode mode;
268 {
269   return ((GET_CODE (op) == CONST_INT
270            && (unsigned HOST_WIDE_INT) INTVAL (op) < 0x100)
271           || register_operand (op, mode));
272 }
273
274 /* Return 1 if OP is an 8-bit constant.  */
275
276 int
277 cint8_operand (op, mode)
278      register rtx op;
279      enum machine_mode mode;
280 {
281   return (GET_CODE (op) == CONST_INT
282           && (unsigned HOST_WIDE_INT) INTVAL (op) < 0x100);
283 }
284
285 /* Return 1 if the operand is a valid second operand to an add insn.  */
286
287 int
288 add_operand (op, mode)
289      register rtx op;
290      enum machine_mode mode;
291 {
292   if (GET_CODE (op) == CONST_INT)
293     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'K')
294             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L')
295             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'O'));
296
297   return register_operand (op, mode);
298 }
299
300 /* Return 1 if the operand is a valid second operand to a sign-extending
301    add insn.  */
302
303 int
304 sext_add_operand (op, mode)
305      register rtx op;
306      enum machine_mode mode;
307 {
308   if (GET_CODE (op) == CONST_INT)
309     return ((unsigned HOST_WIDE_INT) INTVAL (op) < 255
310             || (unsigned HOST_WIDE_INT) (- INTVAL (op)) < 255);
311
312   return register_operand (op, mode);
313 }
314
315 /* Return 1 if OP is the constant 4 or 8.  */
316
317 int
318 const48_operand (op, mode)
319      register rtx op;
320      enum machine_mode mode;
321 {
322   return (GET_CODE (op) == CONST_INT
323           && (INTVAL (op) == 4 || INTVAL (op) == 8));
324 }
325
326 /* Return 1 if OP is a valid first operand to an AND insn.  */
327
328 int
329 and_operand (op, mode)
330      register rtx op;
331      enum machine_mode mode;
332 {
333   if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == VOIDmode)
334     return (zap_mask (CONST_DOUBLE_LOW (op))
335             && zap_mask (CONST_DOUBLE_HIGH (op)));
336
337   if (GET_CODE (op) == CONST_INT)
338     return ((unsigned HOST_WIDE_INT) INTVAL (op) < 0x100
339             || (unsigned HOST_WIDE_INT) ~ INTVAL (op) < 0x100
340             || zap_mask (INTVAL (op)));
341
342   return register_operand (op, mode);
343 }
344
345 /* Return 1 if OP is a valid first operand to an IOR or XOR insn.  */
346
347 int
348 or_operand (op, mode)
349      register rtx op;
350      enum machine_mode mode;
351 {
352   if (GET_CODE (op) == CONST_INT)
353     return ((unsigned HOST_WIDE_INT) INTVAL (op) < 0x100
354             || (unsigned HOST_WIDE_INT) ~ INTVAL (op) < 0x100);
355
356   return register_operand (op, mode);
357 }
358
359 /* Return 1 if OP is a constant that is the width, in bits, of an integral
360    mode smaller than DImode.  */
361
362 int
363 mode_width_operand (op, mode)
364      register rtx op;
365      enum machine_mode mode;
366 {
367   return (GET_CODE (op) == CONST_INT
368           && (INTVAL (op) == 8 || INTVAL (op) == 16 || INTVAL (op) == 32));
369 }
370
371 /* Return 1 if OP is a constant that is the width of an integral machine mode
372    smaller than an integer.  */
373
374 int
375 mode_mask_operand (op, mode)
376      register rtx op;
377      enum machine_mode mode;
378 {
379 #if HOST_BITS_PER_WIDE_INT == 32
380   if (GET_CODE (op) == CONST_DOUBLE)
381     return CONST_DOUBLE_HIGH (op) == 0 && CONST_DOUBLE_LOW (op) == -1;
382 #endif
383
384   return (GET_CODE (op) == CONST_INT
385           && (INTVAL (op) == 0xff
386               || INTVAL (op) == 0xffff
387 #if HOST_BITS_PER_WIDE_INT == 64
388               || INTVAL (op) == 0xffffffff
389 #endif
390               ));
391 }
392
393 /* Return 1 if OP is a multiple of 8 less than 64.  */
394
395 int
396 mul8_operand (op, mode)
397      register rtx op;
398      enum machine_mode mode;
399 {
400   return (GET_CODE (op) == CONST_INT
401           && (unsigned HOST_WIDE_INT) INTVAL (op) < 64
402           && (INTVAL (op) & 7) == 0);
403 }
404
405 /* Return 1 if OP is the constant zero in floating-point.  */
406
407 int
408 fp0_operand (op, mode)
409      register rtx op;
410      enum machine_mode mode;
411 {
412   return (GET_MODE (op) == mode
413           && GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode));
414 }
415
416 /* Return 1 if OP is the floating-point constant zero or a register.  */
417
418 int
419 reg_or_fp0_operand (op, mode)
420      register rtx op;
421      enum machine_mode mode;
422 {
423   return fp0_operand (op, mode) || register_operand (op, mode);
424 }
425
426 /* Return 1 if OP is a register or a constant integer.  */
427
428
429 int
430 reg_or_cint_operand (op, mode)
431     register rtx op;
432     enum machine_mode mode;
433 {
434      return GET_CODE (op) == CONST_INT || register_operand (op, mode);
435 }
436
437 /* Return 1 if OP is something that can be reloaded into a register;
438    if it is a MEM, it need not be valid.  */
439
440 int
441 some_operand (op, mode)
442      register rtx op;
443      enum machine_mode mode;
444 {
445   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && mode != GET_MODE (op))
446     return 0;
447
448   switch (GET_CODE (op))
449     {
450     case REG:  case MEM:  case CONST_DOUBLE:
451     case CONST_INT:  case LABEL_REF:  case SYMBOL_REF:  case CONST:
452       return 1;
453
454     case SUBREG:
455       return some_operand (SUBREG_REG (op), VOIDmode);
456     }
457
458   return 0;
459 }
460
461 /* Return 1 if OP is a valid operand for the source of a move insn.  */
462
463 int
464 input_operand (op, mode)
465      register rtx op;
466      enum machine_mode mode;
467 {
468   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && mode != GET_MODE (op))
469     return 0;
470
471   if (GET_MODE_CLASS (mode) == MODE_FLOAT && GET_MODE (op) != mode)
472     return 0;
473
474   switch (GET_CODE (op))
475     {
476     case LABEL_REF:
477     case SYMBOL_REF:
478     case CONST:
479         /* This handles both the Windows/NT and OSF cases.  */
480       return mode == ptr_mode || mode == DImode;
481
482     case REG:
483       return 1;
484
485     case SUBREG:
486       if (register_operand (op, mode))
487         return 1;
488       /* ... fall through ... */
489     case MEM:
490       return ((TARGET_BYTE_OPS || (mode != HImode && mode != QImode))
491               && general_operand (op, mode));
492
493     case CONST_DOUBLE:
494       return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
495
496     case CONST_INT:
497       return mode == QImode || mode == HImode || add_operand (op, mode);
498     }
499
500   return 0;
501 }
502
503 /* Return 1 if OP is a SYMBOL_REF for a function known to be in this
504    file.  */
505
506 int
507 current_file_function_operand (op, mode)
508      rtx op;
509      enum machine_mode mode;
510 {
511   return (GET_CODE (op) == SYMBOL_REF
512           && ! profile_flag && ! profile_block_flag
513           && (SYMBOL_REF_FLAG (op)
514               || op == XEXP (DECL_RTL (current_function_decl), 0)));
515 }
516
517 /* Return 1 if OP is a valid operand for the MEM of a CALL insn.  */
518
519 int
520 call_operand (op, mode)
521      rtx op;
522      enum machine_mode mode;
523 {
524   if (mode != Pmode)
525     return 0;
526
527   return (GET_CODE (op) == SYMBOL_REF
528           || (GET_CODE (op) == REG && (TARGET_OPEN_VMS || REGNO (op) == 27)));
529 }
530
531 /* Return 1 if OP is a valid Alpha comparison operator.  Here we know which
532    comparisons are valid in which insn.  */
533
534 int
535 alpha_comparison_operator (op, mode)
536      register rtx op;
537      enum machine_mode mode;
538 {
539   enum rtx_code code = GET_CODE (op);
540
541   if (mode != GET_MODE (op) || GET_RTX_CLASS (code) != '<')
542     return 0;
543
544   return (code == EQ || code == LE || code == LT
545           || (mode == DImode && (code == LEU || code == LTU)));
546 }
547
548 /* Return 1 if OP is a valid Alpha swapped comparison operator.  */
549
550 int
551 alpha_swapped_comparison_operator (op, mode)
552      register rtx op;
553      enum machine_mode mode;
554 {
555   enum rtx_code code = GET_CODE (op);
556
557   if (mode != GET_MODE (op) || GET_RTX_CLASS (code) != '<')
558     return 0;
559
560   code = swap_condition (code);
561   return (code == EQ || code == LE || code == LT
562           || (mode == DImode && (code == LEU || code == LTU)));
563 }
564
565 /* Return 1 if OP is a signed comparison operation.  */
566
567 int
568 signed_comparison_operator (op, mode)
569      register rtx op;
570      enum machine_mode mode;
571 {
572   switch (GET_CODE (op))
573     {
574     case EQ:  case NE:  case LE:  case LT:  case GE:   case GT:
575       return 1;
576     }
577
578   return 0;
579 }
580
581 /* Return 1 if this is a divide or modulus operator.  */
582
583 int
584 divmod_operator (op, mode)
585      register rtx op;
586      enum machine_mode mode;
587 {
588   switch (GET_CODE (op))
589     {
590     case DIV:  case MOD:  case UDIV:  case UMOD:
591       return 1;
592     }
593
594   return 0;
595 }
596
597 /* Return 1 if this memory address is a known aligned register plus
598    a constant.  It must be a valid address.  This means that we can do
599    this as an aligned reference plus some offset.
600
601    Take into account what reload will do.
602
603    We could say that out-of-range stack slots are alignable, but that would
604    complicate get_aligned_mem and it isn't worth the trouble since few
605    functions have large stack space.  */
606
607 int
608 aligned_memory_operand (op, mode)
609      register rtx op;
610      enum machine_mode mode;
611 {
612   if (GET_CODE (op) == SUBREG)
613     {
614       if (GET_MODE (op) != mode)
615         return 0;
616       op = SUBREG_REG (op);
617       mode = GET_MODE (op);
618     }
619
620   if (reload_in_progress && GET_CODE (op) == REG
621       && REGNO (op) >= FIRST_PSEUDO_REGISTER)
622     op = reg_equiv_mem[REGNO (op)];
623
624   if (GET_CODE (op) != MEM || GET_MODE (op) != mode
625       || ! memory_address_p (mode, XEXP (op, 0)))
626     return 0;
627
628   op = XEXP (op, 0);
629
630   if (GET_CODE (op) == PLUS)
631     op = XEXP (op, 0);
632
633   return (GET_CODE (op) == REG
634           && REGNO_POINTER_ALIGN (REGNO (op)) >= 4);
635 }
636
637 /* Similar, but return 1 if OP is a MEM which is not alignable.  */
638
639 int
640 unaligned_memory_operand (op, mode)
641      register rtx op;
642      enum machine_mode mode;
643 {
644   if (GET_CODE (op) == SUBREG)
645     {
646       if (GET_MODE (op) != mode)
647         return 0;
648       op = SUBREG_REG (op);
649       mode = GET_MODE (op);
650     }
651
652   if (reload_in_progress && GET_CODE (op) == REG
653       && REGNO (op) >= FIRST_PSEUDO_REGISTER)
654     op = reg_equiv_mem[REGNO (op)];
655
656   if (GET_CODE (op) != MEM || GET_MODE (op) != mode)
657     return 0;
658
659   op = XEXP (op, 0);
660
661   if (! memory_address_p (mode, op))
662     return 1;
663
664   if (GET_CODE (op) == PLUS)
665     op = XEXP (op, 0);
666
667   return (GET_CODE (op) != REG
668           || REGNO_POINTER_ALIGN (REGNO (op)) < 4);
669 }
670
671 /* Return 1 if OP is either a register or an unaligned memory location.  */
672
673 int
674 reg_or_unaligned_mem_operand (op, mode)
675      rtx op;
676      enum machine_mode mode;
677 {
678   return register_operand (op, mode) || unaligned_memory_operand (op, mode);
679 }
680
681 /* Return 1 if OP is any memory location.  During reload a pseudo matches.  */
682
683 int
684 any_memory_operand (op, mode)
685      register rtx op;
686      enum machine_mode mode;
687 {
688   return (GET_CODE (op) == MEM
689           || (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
690           || (reload_in_progress && GET_CODE (op) == REG
691               && REGNO (op) >= FIRST_PSEUDO_REGISTER)
692           || (reload_in_progress && GET_CODE (op) == SUBREG
693               && GET_CODE (SUBREG_REG (op)) == REG
694               && REGNO (SUBREG_REG (op)) >= FIRST_PSEUDO_REGISTER));
695 }
696
697 /* REF is an alignable memory location.  Place an aligned SImode
698    reference into *PALIGNED_MEM and the number of bits to shift into
699    *PBITNUM.  */
700
701 void
702 get_aligned_mem (ref, paligned_mem, pbitnum)
703      rtx ref;
704      rtx *paligned_mem, *pbitnum;
705 {
706   rtx base;
707   HOST_WIDE_INT offset = 0;
708
709   if (GET_CODE (ref) == SUBREG)
710     {
711       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
712       if (BYTES_BIG_ENDIAN)
713         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
714                    - MIN (UNITS_PER_WORD,
715                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
716       ref = SUBREG_REG (ref);
717     }
718
719   if (GET_CODE (ref) == REG)
720     ref = reg_equiv_mem[REGNO (ref)];
721
722   if (reload_in_progress)
723     base = find_replacement (&XEXP (ref, 0));
724   else
725     base = XEXP (ref, 0);
726
727   if (GET_CODE (base) == PLUS)
728     offset += INTVAL (XEXP (base, 1)), base = XEXP (base, 0);
729
730   *paligned_mem = gen_rtx (MEM, SImode,
731                            plus_constant (base, offset & ~3));
732   MEM_IN_STRUCT_P (*paligned_mem) = MEM_IN_STRUCT_P (ref);
733   MEM_VOLATILE_P (*paligned_mem) = MEM_VOLATILE_P (ref);
734   RTX_UNCHANGING_P (*paligned_mem) = RTX_UNCHANGING_P (ref);
735
736   *pbitnum = GEN_INT ((offset & 3) * 8);
737 }
738
739 /* Similar, but just get the address.  Handle the two reload cases.  
740    Add EXTRA_OFFSET to the address we return.  */
741
742 rtx
743 get_unaligned_address (ref, extra_offset)
744      rtx ref;
745      int extra_offset;
746 {
747   rtx base;
748   HOST_WIDE_INT offset = 0;
749
750   if (GET_CODE (ref) == SUBREG)
751     {
752       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
753       if (BYTES_BIG_ENDIAN)
754         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
755                    - MIN (UNITS_PER_WORD,
756                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
757       ref = SUBREG_REG (ref);
758     }
759
760   if (GET_CODE (ref) == REG)
761     ref = reg_equiv_mem[REGNO (ref)];
762
763   if (reload_in_progress)
764     base = find_replacement (&XEXP (ref, 0));
765   else
766     base = XEXP (ref, 0);
767
768   if (GET_CODE (base) == PLUS)
769     offset += INTVAL (XEXP (base, 1)), base = XEXP (base, 0);
770
771   return plus_constant (base, offset + extra_offset);
772 }
773 \f
774 /* Subfunction of the following function.  Update the flags of any MEM
775    found in part of X.  */
776
777 static void
778 alpha_set_memflags_1 (x, in_struct_p, volatile_p, unchanging_p)
779      rtx x;
780      int in_struct_p, volatile_p, unchanging_p;
781 {
782   int i;
783
784   switch (GET_CODE (x))
785     {
786     case SEQUENCE:
787     case PARALLEL:
788       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
789         alpha_set_memflags_1 (XVECEXP (x, 0, i), in_struct_p, volatile_p,
790                               unchanging_p);
791       break;
792
793     case INSN:
794       alpha_set_memflags_1 (PATTERN (x), in_struct_p, volatile_p,
795                             unchanging_p);
796       break;
797
798     case SET:
799       alpha_set_memflags_1 (SET_DEST (x), in_struct_p, volatile_p,
800                             unchanging_p);
801       alpha_set_memflags_1 (SET_SRC (x), in_struct_p, volatile_p,
802                             unchanging_p);
803       break;
804
805     case MEM:
806       MEM_IN_STRUCT_P (x) = in_struct_p;
807       MEM_VOLATILE_P (x) = volatile_p;
808       RTX_UNCHANGING_P (x) = unchanging_p;
809       break;
810     }
811 }
812
813 /* Given INSN, which is either an INSN or a SEQUENCE generated to
814    perform a memory operation, look for any MEMs in either a SET_DEST or
815    a SET_SRC and copy the in-struct, unchanging, and volatile flags from
816    REF into each of the MEMs found.  If REF is not a MEM, don't do
817    anything.  */
818
819 void
820 alpha_set_memflags (insn, ref)
821      rtx insn;
822      rtx ref;
823 {
824   /* Note that it is always safe to get these flags, though they won't
825      be what we think if REF is not a MEM.  */
826   int in_struct_p = MEM_IN_STRUCT_P (ref);
827   int volatile_p = MEM_VOLATILE_P (ref);
828   int unchanging_p = RTX_UNCHANGING_P (ref);
829
830   if (GET_CODE (ref) != MEM
831       || (! in_struct_p && ! volatile_p && ! unchanging_p))
832     return;
833
834   alpha_set_memflags_1 (insn, in_struct_p, volatile_p, unchanging_p);
835 }
836 \f
837 /* Try to output insns to set TARGET equal to the constant C if it can be
838    done in less than N insns.  Do all computations in MODE.  Returns the place
839    where the output has been placed if it can be done and the insns have been
840    emitted.  If it would take more than N insns, zero is returned and no
841    insns and emitted.  */
842
843 rtx
844 alpha_emit_set_const (target, mode, c, n)
845      rtx target;
846      enum machine_mode mode;
847      HOST_WIDE_INT c;
848      int n;
849 {
850   rtx pat;
851   int i;
852
853   /* Try 1 insn, then 2, then up to N. */
854   for (i = 1; i <= n; i++)
855     if ((pat = alpha_emit_set_const_1 (target, mode, c, i)) != 0)
856       return pat;
857
858   return 0;
859 }
860
861 /* Internal routine for the above to check for N or below insns.  */
862
863 static rtx
864 alpha_emit_set_const_1 (target, mode, c, n)
865      rtx target;
866      enum machine_mode mode;
867      HOST_WIDE_INT c;
868      int n;
869 {
870   HOST_WIDE_INT new = c;
871   int i, bits;
872   /* Use a pseudo if highly optimizing and still generating RTL.  */
873   rtx subtarget
874     = (flag_expensive_optimizations && rtx_equal_function_value_matters
875        ? 0 : target);
876   rtx temp;
877
878 #if HOST_BITS_PER_WIDE_INT == 64
879   /* We are only called for SImode and DImode.  If this is SImode, ensure that
880      we are sign extended to a full word.  This does not make any sense when
881      cross-compiling on a narrow machine.  */
882
883   if (mode == SImode)
884     c = (c & 0xffffffff) - 2 * (c & 0x80000000);
885 #endif
886
887   /* If this is a sign-extended 32-bit constant, we can do this in at most
888      three insns, so do it if we have enough insns left.  We always have
889      a sign-extended 32-bit constant when compiling on a narrow machine. 
890      Note that we cannot handle the constant 0x80000000.  */
891
892   if ((HOST_BITS_PER_WIDE_INT != 64
893        || c >> 31 == -1 || c >> 31 == 0)
894       && c != 0x80000000U)
895     {
896       HOST_WIDE_INT low = (c & 0xffff) - 2 * (c & 0x8000);
897       HOST_WIDE_INT tmp1 = c - low;
898       HOST_WIDE_INT high
899         = ((tmp1 >> 16) & 0xffff) - 2 * ((tmp1 >> 16) & 0x8000);
900       HOST_WIDE_INT extra = 0;
901
902       /* If HIGH will be interpreted as negative but the constant is
903          positive, we must adjust it to do two ldha insns.  */
904
905       if ((high & 0x8000) != 0 && c >= 0)
906         {
907           extra = 0x4000;
908           tmp1 -= 0x40000000;
909           high = ((tmp1 >> 16) & 0xffff) - 2 * ((tmp1 >> 16) & 0x8000);
910         }
911
912       if (c == low || (low == 0 && extra == 0))
913         return copy_to_suggested_reg (GEN_INT (c), target, mode);
914       else if (n >= 2 + (extra != 0))
915         {
916           temp = copy_to_suggested_reg (GEN_INT (low), subtarget, mode);
917
918           if (extra != 0)
919             temp = expand_binop (mode, add_optab, temp, GEN_INT (extra << 16),
920                                  subtarget, 0, OPTAB_WIDEN);
921
922           return expand_binop (mode, add_optab, temp, GEN_INT (high << 16),
923                                target, 0, OPTAB_WIDEN);
924         }
925     }
926
927   /* If we couldn't do it that way, try some other methods.  But if we have
928      no instructions left, don't bother.  Likewise, if this is SImode and
929      we can't make pseudos, we can't do anything since the expand_binop
930      and expand_unop calls will widen and try to make pseudos.  */
931
932   if (n == 1
933       || (mode == SImode && ! rtx_equal_function_value_matters))
934     return 0;
935
936 #if HOST_BITS_PER_WIDE_INT == 64
937   /* First, see if can load a value into the target that is the same as the
938      constant except that all bytes that are 0 are changed to be 0xff.  If we
939      can, then we can do a ZAPNOT to obtain the desired constant.  */
940
941   for (i = 0; i < 64; i += 8)
942     if ((new & ((HOST_WIDE_INT) 0xff << i)) == 0)
943       new |= (HOST_WIDE_INT) 0xff << i;
944
945   /* We are only called for SImode and DImode.  If this is SImode, ensure that
946      we are sign extended to a full word.  */
947
948   if (mode == SImode)
949     new = (new & 0xffffffff) - 2 * (new & 0x80000000);
950
951   if (new != c
952       && (temp = alpha_emit_set_const (subtarget, mode, new, n - 1)) != 0)
953     return expand_binop (mode, and_optab, temp, GEN_INT (c | ~ new),
954                          target, 0, OPTAB_WIDEN);
955 #endif
956
957   /* Next, see if we can load a related constant and then shift and possibly
958      negate it to get the constant we want.  Try this once each increasing
959      numbers of insns.  */
960
961   for (i = 1; i < n; i++)
962     {
963       /* First try complementing.  */
964       if ((temp = alpha_emit_set_const (subtarget, mode, ~ c, i)) != 0)
965         return expand_unop (mode, one_cmpl_optab, temp, target, 0);
966
967       /* Next try to form a constant and do a left shift.  We can do this
968          if some low-order bits are zero; the exact_log2 call below tells
969          us that information.  The bits we are shifting out could be any
970          value, but here we'll just try the 0- and sign-extended forms of
971          the constant.  To try to increase the chance of having the same
972          constant in more than one insn, start at the highest number of
973          bits to shift, but try all possibilities in case a ZAPNOT will
974          be useful.  */
975
976       if ((bits = exact_log2 (c & - c)) > 0)
977         for (; bits > 0; bits--)
978           if ((temp = (alpha_emit_set_const
979                        (subtarget, mode,
980                         (unsigned HOST_WIDE_INT) c >> bits, i))) != 0
981               || ((temp = (alpha_emit_set_const
982                           (subtarget, mode,
983                            ((unsigned HOST_WIDE_INT) c) >> bits, i)))
984                   != 0))
985             return expand_binop (mode, ashl_optab, temp, GEN_INT (bits),
986                                  target, 0, OPTAB_WIDEN);
987
988       /* Now try high-order zero bits.  Here we try the shifted-in bits as
989          all zero and all ones.  Be careful to avoid shifting outside the
990          mode and to avoid shifting outside the host wide int size.  */
991
992       if ((bits = (MIN (HOST_BITS_PER_WIDE_INT, GET_MODE_SIZE (mode) * 8)
993                    - floor_log2 (c) - 1)) > 0)
994         for (; bits > 0; bits--)
995           if ((temp = alpha_emit_set_const (subtarget, mode,
996                                             c << bits, i)) != 0
997               || ((temp = (alpha_emit_set_const
998                            (subtarget, mode,
999                             ((c << bits) | (((HOST_WIDE_INT) 1 << bits) - 1)),
1000                             i)))
1001                   != 0))
1002             return expand_binop (mode, lshr_optab, temp, GEN_INT (bits),
1003                                  target, 1, OPTAB_WIDEN);
1004
1005       /* Now try high-order 1 bits.  We get that with a sign-extension.
1006          But one bit isn't enough here.  Be careful to avoid shifting outside
1007          the mode and to avoid shifting outside the host wide int size. */
1008       
1009       if ((bits = (MIN (HOST_BITS_PER_WIDE_INT, GET_MODE_SIZE (mode) * 8)
1010                    - floor_log2 (~ c) - 2)) > 0)
1011         for (; bits > 0; bits--)
1012           if ((temp = alpha_emit_set_const (subtarget, mode,
1013                                             c << bits, i)) != 0
1014               || ((temp = (alpha_emit_set_const
1015                            (subtarget, mode,
1016                             ((c << bits) | (((HOST_WIDE_INT) 1 << bits) - 1)),
1017                             i)))
1018                   != 0))
1019             return expand_binop (mode, ashr_optab, temp, GEN_INT (bits),
1020                                  target, 0, OPTAB_WIDEN);
1021     }
1022
1023   return 0;
1024 }
1025
1026 #if HOST_BITS_PER_WIDE_INT == 64
1027 /* Having failed to find a 3 insn sequence in alpha_emit_set_const,
1028    fall back to a straight forward decomposition.  We do this to avoid
1029    exponential run times encountered when looking for longer sequences
1030    with alpha_emit_set_const.  */
1031
1032 rtx
1033 alpha_emit_set_long_const (target, c)
1034      rtx target;
1035      HOST_WIDE_INT c;
1036 {
1037   /* Use a pseudo if highly optimizing and still generating RTL.  */
1038   rtx subtarget
1039     = (flag_expensive_optimizations && rtx_equal_function_value_matters
1040        ? 0 : target);
1041   HOST_WIDE_INT d1, d2, d3, d4;
1042   rtx r1, r2;
1043
1044   /* Decompose the entire word */
1045   d1 = ((c & 0xffff) ^ 0x8000) - 0x8000;
1046   c -= d1;
1047   d2 = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
1048   c = (c - d2) >> 32;
1049   d3 = ((c & 0xffff) ^ 0x8000) - 0x8000;
1050   c -= d3;
1051   d4 = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
1052
1053   if (c - d4 != 0)
1054     abort();
1055
1056   /* Construct the high word */
1057   if (d3 == 0)
1058     r1 = copy_to_suggested_reg (GEN_INT (d4), subtarget, DImode);
1059   else if (d4 == 0)
1060     r1 = copy_to_suggested_reg (GEN_INT (d3), subtarget, DImode);
1061   else
1062     r1 = expand_binop (DImode, add_optab, GEN_INT (d3), GEN_INT (d4),
1063                        subtarget, 0, OPTAB_WIDEN);
1064
1065   /* Shift it into place */
1066   r2 = expand_binop (DImode, ashl_optab, r1, GEN_INT (32), 
1067                      subtarget, 0, OPTAB_WIDEN);
1068
1069   if (subtarget == 0 && d1 == d3 && d2 == d4)
1070     r1 = expand_binop (DImode, add_optab, r1, r2, subtarget, 0, OPTAB_WIDEN);
1071   else
1072     {
1073       r1 = r2;
1074
1075       /* Add in the low word */
1076       if (d2 != 0)
1077         r1 = expand_binop (DImode, add_optab, r1, GEN_INT (d2),
1078                            subtarget, 0, OPTAB_WIDEN);
1079       if (d1 != 0)
1080         r1 = expand_binop (DImode, add_optab, r1, GEN_INT (d1),
1081                            subtarget, 0, OPTAB_WIDEN);
1082     }
1083
1084   if (subtarget == 0)
1085     r1 = copy_to_suggested_reg(r1, target, DImode);
1086
1087   return r1;
1088 }
1089 #endif /* HOST_BITS_PER_WIDE_INT == 64 */
1090
1091 /* Rewrite a comparison against zero CMP of the form
1092    (CODE (cc0) (const_int 0)) so it can be written validly in
1093    a conditional move (if_then_else CMP ...).
1094    If both of the operands that set cc0 are non-zero we must emit
1095    an insn to perform the compare (it can't be done within
1096    the conditional move). */
1097 rtx
1098 alpha_emit_conditional_move (cmp, mode)
1099      rtx cmp;
1100      enum machine_mode mode;
1101 {
1102   enum rtx_code code = GET_CODE (cmp);
1103   enum rtx_code cmov_code = NE;
1104   rtx op0 = alpha_compare_op0;
1105   rtx op1 = alpha_compare_op1;
1106   enum machine_mode cmp_mode
1107     = (GET_MODE (op0) == VOIDmode ? DImode : GET_MODE (op0));
1108   enum machine_mode cmp_op_mode = alpha_compare_fp_p ? DFmode : DImode;
1109   rtx tem;
1110
1111   if (alpha_compare_fp_p != FLOAT_MODE_P (mode))
1112     return 0;
1113
1114   /* We may be able to use a conditional move directly.
1115      This avoids emitting spurious compares. */
1116   if (signed_comparison_operator (cmp, cmp_op_mode)
1117       && (op0 == CONST0_RTX (cmp_mode) || op1 == CONST0_RTX (cmp_mode)))
1118     return gen_rtx (code, VOIDmode, op0, op1);
1119
1120   /* We can't put the comparison insides a conditional move;
1121      emit a compare instruction and put that inside the
1122      conditional move.  Make sure we emit only comparisons we have;
1123      swap or reverse as necessary.  */
1124
1125   switch (code)
1126     {
1127     case EQ:  case LE:  case LT:  case LEU:  case LTU:
1128       /* We have these compares: */
1129       break;
1130
1131     case NE:
1132       /* This must be reversed. */
1133       code = reverse_condition (code);
1134       cmov_code = EQ;
1135       break;
1136
1137     case GE:  case GT:  case GEU:  case GTU:
1138       /* These must be swapped.  Make sure the new first operand is in
1139          a register.  */
1140       code = swap_condition (code);
1141       tem = op0, op0 = op1, op1 = tem;
1142       op0 = force_reg (cmp_mode, op0);
1143       break;
1144
1145     default:
1146       abort ();
1147     }
1148
1149   tem = gen_reg_rtx (cmp_op_mode);
1150   emit_move_insn (tem, gen_rtx (code, cmp_op_mode, op0, op1));
1151   return gen_rtx (cmov_code, VOIDmode, tem, CONST0_RTX (cmp_op_mode));
1152 }
1153 \f
1154 /* Adjust the cost of a scheduling dependency.  Return the new cost of
1155    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
1156
1157 int
1158 alpha_adjust_cost (insn, link, dep_insn, cost)
1159      rtx insn;
1160      rtx link;
1161      rtx dep_insn;
1162      int cost;
1163 {
1164   rtx set, set_src;
1165
1166   /* If the dependence is an anti-dependence, there is no cost.  For an
1167      output dependence, there is sometimes a cost, but it doesn't seem
1168      worth handling those few cases.  */
1169
1170   if (REG_NOTE_KIND (link) != 0)
1171     return 0;
1172
1173   if (alpha_cpu == PROCESSOR_EV5)
1174     {
1175       /* On EV5, "A special bypass provides an effective latency of 0
1176          cycles for an ICMP or ILOG insn producing the test operand of an
1177          IBR or CMOV insn." */
1178
1179       if (recog_memoized (dep_insn) >= 0
1180           && (get_attr_type (dep_insn) == TYPE_ICMP
1181               || get_attr_type (dep_insn) == TYPE_ILOG)
1182           && recog_memoized (insn) >= 0
1183           && (get_attr_type (insn) == TYPE_IBR
1184               || (get_attr_type (insn) == TYPE_CMOV
1185                   && !((set = single_set (dep_insn)) != 0
1186                        && GET_CODE (PATTERN (insn)) == SET
1187                        && (set_src = SET_SRC (PATTERN (insn)),
1188                            GET_CODE (set_src) == IF_THEN_ELSE)
1189                        && (set = SET_DEST (set),
1190                            rtx_equal_p (set, XEXP (set_src, 1))
1191                            || rtx_equal_p (set, XEXP (set_src, 2)))))))
1192         return 0;
1193
1194       /* On EV5 it takes longer to get data to the multiplier than to
1195          anywhere else, so increase costs.  */
1196
1197       if (recog_memoized (insn) >= 0
1198           && recog_memoized (dep_insn) >= 0
1199           && (get_attr_type (insn) == TYPE_IMULL
1200               || get_attr_type (insn) == TYPE_IMULQ
1201               || get_attr_type (insn) == TYPE_IMULH)
1202           && (set = single_set (dep_insn)) != 0
1203           && GET_CODE (PATTERN (insn)) == SET
1204           && (set_src = SET_SRC (PATTERN (insn)),
1205               GET_CODE (set_src) == MULT)
1206           && (set = SET_DEST (set),
1207               rtx_equal_p (set, XEXP (set_src, 0))
1208               || rtx_equal_p (set, XEXP (set_src, 1))))
1209         {
1210           switch (get_attr_type (insn))
1211             {
1212             case TYPE_LD:
1213             case TYPE_CMOV:
1214             case TYPE_IMULL:
1215             case TYPE_IMULQ:
1216             case TYPE_IMULH:
1217               return cost + 1;
1218             case TYPE_JSR:
1219             case TYPE_IADD:
1220             case TYPE_ILOG:
1221             case TYPE_SHIFT:
1222             case TYPE_ICMP:
1223               return cost + 2;
1224             }
1225         }
1226     }
1227   else
1228     {
1229       /* On EV4, if INSN is a store insn and DEP_INSN is setting the data
1230          being stored, we can sometimes lower the cost.  */
1231
1232       if (recog_memoized (insn) >= 0 && get_attr_type (insn) == TYPE_ST
1233           && (set = single_set (dep_insn)) != 0
1234           && GET_CODE (PATTERN (insn)) == SET
1235           && rtx_equal_p (SET_DEST (set), SET_SRC (PATTERN (insn))))
1236         {
1237           switch (get_attr_type (dep_insn))
1238             {
1239             case TYPE_LD:
1240               /* No savings here.  */
1241               return cost;
1242
1243             case TYPE_IMULL:
1244             case TYPE_IMULQ:
1245             case TYPE_IMULH:
1246               /* In these cases, we save one cycle.  */
1247               return cost - 1;
1248
1249             default:
1250               /* In all other cases, we save two cycles.  */
1251               return MAX (0, cost - 2);
1252             }
1253         }
1254
1255       /* Another case that needs adjustment is an arithmetic or logical
1256          operation.  It's cost is usually one cycle, but we default it to
1257          two in the MD file.  The only case that it is actually two is
1258          for the address in loads and stores.  */
1259
1260       if (recog_memoized (dep_insn) >= 0
1261           && (get_attr_type (dep_insn) == TYPE_IADD
1262               || get_attr_type (dep_insn) == TYPE_ILOG))
1263         {
1264           switch (get_attr_type (insn))
1265             {
1266             case TYPE_LD:
1267             case TYPE_ST:
1268               return cost;
1269             default:
1270               return 1;
1271             }
1272         }
1273
1274       /* The final case is when a compare feeds into an integer branch;
1275          the cost is only one cycle in that case.  */
1276
1277       if (recog_memoized (dep_insn) >= 0
1278           && get_attr_type (dep_insn) == TYPE_ICMP
1279           && recog_memoized (insn) >= 0
1280           && get_attr_type (insn) == TYPE_IBR)
1281         return 1;
1282     }
1283
1284   /* Otherwise, return the default cost. */
1285   return cost;
1286 }
1287 \f
1288 /* Functions to save and restore alpha_return_addr_rtx.  */
1289
1290 struct machine_function
1291 {
1292   rtx ra_rtx;
1293 };
1294
1295 static void
1296 alpha_save_machine_status (p)
1297      struct function *p;
1298 {
1299   struct machine_function *machine =
1300     (struct machine_function *) xmalloc (sizeof (struct machine_function));
1301
1302   p->machine = machine;
1303   machine->ra_rtx = alpha_return_addr_rtx;
1304 }
1305
1306 static void
1307 alpha_restore_machine_status (p)
1308      struct function *p;
1309 {
1310   struct machine_function *machine = p->machine;
1311
1312   alpha_return_addr_rtx = machine->ra_rtx;
1313
1314   free (machine);
1315   p->machine = (struct machine_function *)0;
1316 }
1317
1318 /* Do anything needed before RTL is emitted for each function.  */
1319
1320 void
1321 alpha_init_expanders ()
1322 {
1323   alpha_return_addr_rtx = NULL_RTX;
1324
1325   /* Arrange to save and restore machine status around nested functions.  */
1326   save_machine_status = alpha_save_machine_status;
1327   restore_machine_status = alpha_restore_machine_status;
1328 }
1329
1330 /* Start the ball rolling with RETURN_ADDR_RTX.  */
1331
1332 rtx
1333 alpha_return_addr (count, frame)
1334      int count;
1335      rtx frame;
1336 {
1337   rtx init, first;
1338
1339   if (count != 0)
1340     return const0_rtx;
1341
1342   if (alpha_return_addr_rtx)
1343     return alpha_return_addr_rtx;
1344
1345   /* No rtx yet.  Invent one, and initialize it from $26 in the prologue.  */
1346   alpha_return_addr_rtx = gen_reg_rtx (Pmode);
1347   init = gen_rtx (SET, Pmode, alpha_return_addr_rtx, gen_rtx (REG, Pmode, 26));
1348
1349   /* Emit the insn to the prologue with the other argument copies.  */
1350   push_topmost_sequence ();
1351   emit_insn_after (init, get_insns ());
1352   pop_topmost_sequence ();
1353
1354   return alpha_return_addr_rtx;
1355 }
1356
1357 static int
1358 alpha_ra_ever_killed ()
1359 {
1360   rtx i, ra;
1361
1362   if (!alpha_return_addr_rtx)
1363     return regs_ever_live[REG_RA];
1364
1365   return reg_set_between_p (gen_rtx (REG, REG_RA), get_insns(), NULL_RTX);
1366 }
1367
1368 \f
1369 /* Print an operand.  Recognize special options, documented below.  */
1370
1371 void
1372 print_operand (file, x, code)
1373     FILE *file;
1374     rtx x;
1375     char code;
1376 {
1377   int i;
1378
1379   switch (code)
1380     {
1381     case '&':
1382       /* Generates fp-rounding mode suffix: nothing for normal, 'c' for
1383          chopped, 'm' for minus-infinity, and 'd' for dynamic rounding
1384          mode.  alpha_fprm controls which suffix is generated.  */
1385       switch (alpha_fprm)
1386         {
1387         case ALPHA_FPRM_NORM:
1388           break;
1389         case ALPHA_FPRM_MINF: 
1390           fputc ('m', file);
1391           break;
1392         case ALPHA_FPRM_CHOP:
1393           fputc ('c', file);
1394           break;
1395         case ALPHA_FPRM_DYN:
1396           fputc ('d', file);
1397           break;
1398         }
1399       break;
1400
1401     case '\'':
1402       /* Generates trap-mode suffix for instructions that accept the su
1403          suffix only (cmpt et al).  */
1404       if (alpha_tp == ALPHA_TP_INSN)
1405         fputs ("su", file);
1406       break;
1407
1408     case ')':
1409       /* Generates trap-mode suffix for instructions that accept the u, su,
1410          and sui suffix.  This is the bulk of the IEEE floating point
1411          instructions (addt et al).  */
1412       switch (alpha_fptm)
1413         {
1414         case ALPHA_FPTM_N:
1415           break;
1416         case ALPHA_FPTM_U:
1417           fputc ('u', file);
1418           break;
1419         case ALPHA_FPTM_SU:
1420           fputs ("su", file);
1421           break;
1422         case ALPHA_FPTM_SUI:
1423           fputs ("sui", file);
1424           break;
1425         }
1426       break;
1427
1428     case '+':
1429       /* Generates trap-mode suffix for instructions that accept the sui
1430          suffix (cvtqt and cvtqs).  */
1431       switch (alpha_fptm)
1432         {
1433         case ALPHA_FPTM_N: case ALPHA_FPTM_U:
1434         case ALPHA_FPTM_SU:     /* cvtqt/cvtqs can't cause underflow */
1435           break;
1436         case ALPHA_FPTM_SUI:
1437           fputs ("sui", file);
1438           break;
1439         }
1440       break;
1441
1442     case ',':
1443       /* Generates single precision instruction suffix.  */
1444       fprintf (file, "%c", (TARGET_FLOAT_VAX?'f':'s'));
1445       break;
1446
1447     case '-':
1448       /* Generates double precision instruction suffix.  */
1449       fprintf (file, "%c", (TARGET_FLOAT_VAX?'g':'t'));
1450       break;
1451
1452     case 'r':
1453       /* If this operand is the constant zero, write it as "$31".  */
1454       if (GET_CODE (x) == REG)
1455         fprintf (file, "%s", reg_names[REGNO (x)]);
1456       else if (x == CONST0_RTX (GET_MODE (x)))
1457         fprintf (file, "$31");
1458       else
1459         output_operand_lossage ("invalid %%r value");
1460
1461       break;
1462
1463     case 'R':
1464       /* Similar, but for floating-point.  */
1465       if (GET_CODE (x) == REG)
1466         fprintf (file, "%s", reg_names[REGNO (x)]);
1467       else if (x == CONST0_RTX (GET_MODE (x)))
1468         fprintf (file, "$f31");
1469       else
1470         output_operand_lossage ("invalid %%R value");
1471
1472       break;
1473
1474     case 'N':
1475       /* Write the 1's complement of a constant.  */
1476       if (GET_CODE (x) != CONST_INT)
1477         output_operand_lossage ("invalid %%N value");
1478
1479       fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
1480       break;
1481
1482     case 'P':
1483       /* Write 1 << C, for a constant C.  */
1484       if (GET_CODE (x) != CONST_INT)
1485         output_operand_lossage ("invalid %%P value");
1486
1487       fprintf (file, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) 1 << INTVAL (x));
1488       break;
1489
1490     case 'h':
1491       /* Write the high-order 16 bits of a constant, sign-extended.  */
1492       if (GET_CODE (x) != CONST_INT)
1493         output_operand_lossage ("invalid %%h value");
1494
1495       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) >> 16);
1496       break;
1497
1498     case 'L':
1499       /* Write the low-order 16 bits of a constant, sign-extended.  */
1500       if (GET_CODE (x) != CONST_INT)
1501         output_operand_lossage ("invalid %%L value");
1502
1503       fprintf (file, HOST_WIDE_INT_PRINT_DEC,
1504                (INTVAL (x) & 0xffff) - 2 * (INTVAL (x) & 0x8000));
1505       break;
1506
1507     case 'm':
1508       /* Write mask for ZAP insn.  */
1509       if (GET_CODE (x) == CONST_DOUBLE)
1510         {
1511           HOST_WIDE_INT mask = 0;
1512           HOST_WIDE_INT value;
1513
1514           value = CONST_DOUBLE_LOW (x);
1515           for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1516                i++, value >>= 8)
1517             if (value & 0xff)
1518               mask |= (1 << i);
1519
1520           value = CONST_DOUBLE_HIGH (x);
1521           for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1522                i++, value >>= 8)
1523             if (value & 0xff)
1524               mask |= (1 << (i + sizeof (int)));
1525
1526           fprintf (file, HOST_WIDE_INT_PRINT_DEC, mask & 0xff);
1527         }
1528
1529       else if (GET_CODE (x) == CONST_INT)
1530         {
1531           HOST_WIDE_INT mask = 0, value = INTVAL (x);
1532
1533           for (i = 0; i < 8; i++, value >>= 8)
1534             if (value & 0xff)
1535               mask |= (1 << i);
1536
1537           fprintf (file, HOST_WIDE_INT_PRINT_DEC, mask);
1538         }
1539       else
1540         output_operand_lossage ("invalid %%m value");
1541       break;
1542
1543     case 'M':
1544       /* 'b', 'w', or 'l' as the value of the constant.  */
1545       if (GET_CODE (x) != CONST_INT
1546           || (INTVAL (x) != 8 && INTVAL (x) != 16 && INTVAL (x) != 32))
1547         output_operand_lossage ("invalid %%M value");
1548
1549       fprintf (file, "%s",
1550                INTVAL (x) == 8 ? "b" : INTVAL (x) == 16 ? "w" : "l");
1551       break;
1552
1553     case 'U':
1554       /* Similar, except do it from the mask.  */
1555       if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0xff)
1556         fprintf (file, "b");
1557       else if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0xffff)
1558         fprintf (file, "w");
1559 #if HOST_BITS_PER_WIDE_INT == 32
1560       else if (GET_CODE (x) == CONST_DOUBLE
1561                && CONST_DOUBLE_HIGH (x) == 0
1562                && CONST_DOUBLE_LOW (x) == -1)
1563         fprintf (file, "l");
1564 #else
1565       else if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0xffffffff)
1566         fprintf (file, "l");
1567 #endif
1568       else
1569         output_operand_lossage ("invalid %%U value");
1570       break;
1571
1572     case 's':
1573       /* Write the constant value divided by 8.  */
1574       if (GET_CODE (x) != CONST_INT
1575           && (unsigned HOST_WIDE_INT) INTVAL (x) >= 64
1576           && (INTVAL (x) & 7) != 8)
1577         output_operand_lossage ("invalid %%s value");
1578
1579       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) / 8);
1580       break;
1581
1582     case 'S':
1583       /* Same, except compute (64 - c) / 8 */
1584
1585       if (GET_CODE (x) != CONST_INT
1586           && (unsigned HOST_WIDE_INT) INTVAL (x) >= 64
1587           && (INTVAL (x) & 7) != 8)
1588         output_operand_lossage ("invalid %%s value");
1589
1590       fprintf (file, HOST_WIDE_INT_PRINT_DEC, (64 - INTVAL (x)) / 8);
1591       break;
1592
1593     case 'C': case 'D': case 'c': case 'd':
1594       /* Write out comparison name.  */
1595       {
1596         enum rtx_code c = GET_CODE (x);
1597
1598         if (GET_RTX_CLASS (c) != '<')
1599           output_operand_lossage ("invalid %%C value");
1600
1601         if (code == 'D')
1602           c = reverse_condition (c);
1603         else if (code == 'c')
1604           c = swap_condition (c);
1605         else if (code == 'd')
1606           c = swap_condition (reverse_condition (c));
1607
1608         if (c == LEU)
1609           fprintf (file, "ule");
1610         else if (c == LTU)
1611           fprintf (file, "ult");
1612         else
1613           fprintf (file, "%s", GET_RTX_NAME (c));
1614       }
1615       break;
1616
1617     case 'E':
1618       /* Write the divide or modulus operator.  */
1619       switch (GET_CODE (x))
1620         {
1621         case DIV:
1622           fprintf (file, "div%s", GET_MODE (x) == SImode ? "l" : "q");
1623           break;
1624         case UDIV:
1625           fprintf (file, "div%su", GET_MODE (x) == SImode ? "l" : "q");
1626           break;
1627         case MOD:
1628           fprintf (file, "rem%s", GET_MODE (x) == SImode ? "l" : "q");
1629           break;
1630         case UMOD:
1631           fprintf (file, "rem%su", GET_MODE (x) == SImode ? "l" : "q");
1632           break;
1633         default:
1634           output_operand_lossage ("invalid %%E value");
1635           break;
1636         }
1637       break;
1638
1639     case 'A':
1640       /* Write "_u" for unaligned access.  */
1641       if (GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) == AND)
1642         fprintf (file, "_u");
1643       break;
1644
1645     case 0:
1646       if (GET_CODE (x) == REG)
1647         fprintf (file, "%s", reg_names[REGNO (x)]);
1648       else if (GET_CODE (x) == MEM)
1649         output_address (XEXP (x, 0));
1650       else
1651         output_addr_const (file, x);
1652       break;
1653
1654     default:
1655       output_operand_lossage ("invalid %%xn code");
1656     }
1657 }
1658 \f
1659 /* Do what is necessary for `va_start'.  The argument is ignored;
1660    We look at the current function to determine if stdarg or varargs
1661    is used and fill in an initial va_list.  A pointer to this constructor
1662    is returned.  */
1663
1664 struct rtx_def *
1665 alpha_builtin_saveregs (arglist)
1666      tree arglist;
1667 {
1668   rtx block, addr, dest, argsize;
1669   tree fntype = TREE_TYPE (current_function_decl);
1670   int stdarg = (TYPE_ARG_TYPES (fntype) != 0
1671                 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1672                     != void_type_node));
1673
1674   /* Compute the current position into the args, taking into account
1675      both registers and memory.  Both of these are already included in
1676      current_function_args_info.  */
1677
1678   argsize = GEN_INT (CURRENT_FUNCTION_ARGS_INFO * UNITS_PER_WORD);
1679
1680   /* For Unix, SETUP_INCOMING_VARARGS moves the starting address base up by 48,
1681      storing fp arg registers in the first 48 bytes, and the integer arg
1682      registers in the next 48 bytes.  This is only done, however, if any
1683      integer registers need to be stored.
1684
1685      If no integer registers need be stored, then we must subtract 48 in
1686      order to account for the integer arg registers which are counted in
1687      argsize above, but which are not actually stored on the stack.  */
1688
1689   if (TARGET_OPEN_VMS)
1690     addr = plus_constant (virtual_incoming_args_rtx,
1691                           CURRENT_FUNCTION_ARGS_INFO <= 5 + stdarg
1692                           ? UNITS_PER_WORD : - 6 * UNITS_PER_WORD);
1693   else
1694     addr = (CURRENT_FUNCTION_ARGS_INFO <= 5 + stdarg
1695             ? plus_constant (virtual_incoming_args_rtx,
1696                              6 * UNITS_PER_WORD)
1697             : plus_constant (virtual_incoming_args_rtx,
1698                              - (6 * UNITS_PER_WORD)));
1699
1700   /* For VMS, we include the argsize, while on Unix, it's handled as
1701      a separate field.  */
1702   if (TARGET_OPEN_VMS)
1703     addr = plus_constant (addr, INTVAL (argsize));
1704
1705   addr = force_operand (addr, NULL_RTX);
1706
1707 #ifdef POINTERS_EXTEND_UNSIGNED
1708   addr = convert_memory_address (ptr_mode, addr);
1709 #endif
1710
1711   if (TARGET_OPEN_VMS)
1712     return addr;
1713   else
1714     {
1715       /* Allocate the va_list constructor */
1716       block = assign_stack_local (BLKmode, 2 * UNITS_PER_WORD, BITS_PER_WORD);
1717       RTX_UNCHANGING_P (block) = 1;
1718       RTX_UNCHANGING_P (XEXP (block, 0)) = 1;
1719
1720       /* Store the address of the first integer register in the __base
1721          member.  */
1722
1723       dest = change_address (block, ptr_mode, XEXP (block, 0));
1724       emit_move_insn (dest, addr);
1725
1726       if (flag_check_memory_usage)
1727         emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3, dest,
1728                            ptr_mode, GEN_INT (GET_MODE_SIZE (ptr_mode)),
1729                            TYPE_MODE (sizetype),
1730                            GEN_INT (MEMORY_USE_RW), QImode);
1731   
1732       /* Store the argsize as the __va_offset member.  */
1733       dest = change_address (block, TYPE_MODE (integer_type_node),
1734                              plus_constant (XEXP (block, 0),
1735                                             POINTER_SIZE/BITS_PER_UNIT));
1736       emit_move_insn (dest, argsize);
1737
1738       if (flag_check_memory_usage)
1739         emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3, dest,
1740                            ptr_mode,
1741                            GEN_INT (GET_MODE_SIZE
1742                                     (TYPE_MODE (integer_type_node))),
1743                            TYPE_MODE (sizetype),
1744                            GEN_INT (MEMORY_USE_RW), QImode);
1745
1746       /* Return the address of the va_list constructor, but don't put it in a
1747          register.  Doing so would fail when not optimizing and produce worse
1748          code when optimizing.  */
1749       return XEXP (block, 0);
1750     }
1751 }
1752 \f
1753 /* This page contains routines that are used to determine what the function
1754    prologue and epilogue code will do and write them out.  */
1755
1756 /* Compute the size of the save area in the stack.  */
1757
1758 #if OPEN_VMS
1759
1760 /* These variables are used for communication between the following functions.
1761    They indicate various things about the current function being compiled
1762    that are used to tell what kind of prologue, epilogue and procedure
1763    descriptior to generate. */
1764
1765 /* Nonzero if we need a stack procedure.  */
1766 static int is_stack_procedure;
1767
1768 /* Register number (either FP or SP) that is used to unwind the frame.  */
1769 static int unwind_regno;
1770
1771 /* Register number used to save FP.  We need not have one for RA since
1772    we don't modify it for register procedures.  This is only defined
1773    for register frame procedures.  */
1774 static int save_fp_regno;
1775
1776 /* Register number used to reference objects off our PV.  */
1777 static int base_regno;
1778
1779 /*  Compute register masks for saved registers.  */
1780
1781 static void
1782 alpha_sa_mask (imaskP, fmaskP)
1783     unsigned long *imaskP;
1784     unsigned long *fmaskP;
1785 {
1786   unsigned long imask = 0;
1787   unsigned long fmask = 0;
1788   int i;
1789
1790   if (is_stack_procedure)
1791     imask |= (1L << HARD_FRAME_POINTER_REGNUM);
1792
1793   /* One for every register we have to save.  */
1794
1795   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1796     if (! fixed_regs[i] && ! call_used_regs[i]
1797         && regs_ever_live[i] && i != REG_RA)
1798       {
1799         if (i < 32)
1800           imask |= (1L << i);
1801         else
1802           fmask |= (1L << (i - 32));
1803       }
1804
1805   *imaskP = imask;
1806   *fmaskP = fmask;
1807
1808   return;
1809 }
1810
1811 int
1812 alpha_sa_size ()
1813 {
1814   int sa_size = 0;
1815   HOST_WIDE_INT stack_needed;
1816   int i;
1817
1818   /* One for every register we have to save.  */
1819
1820   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1821     if (! fixed_regs[i] && ! call_used_regs[i]
1822         && regs_ever_live[i] && i != REG_RA)
1823       sa_size++;
1824
1825   /* Start by assuming we can use a register procedure if we don't make any
1826      calls (REG_RA not used) or need to save any registers and a stack
1827      procedure if we do.  */
1828   is_stack_procedure = sa_size != 0 || alpha_ra_ever_killed ();
1829
1830   /* Decide whether to refer to objects off our PV via FP or PV.
1831      If we need need FP for something else or if we receive a nonlocal
1832      goto (which expects PV to contain the value), we must use PV.
1833      Otherwise, start by assuming we can use FP.  */
1834   base_regno = (frame_pointer_needed || current_function_has_nonlocal_label
1835                 || is_stack_procedure
1836                 || current_function_outgoing_args_size
1837                 ? REG_PV : HARD_FRAME_POINTER_REGNUM);
1838
1839   /* If we want to copy PV into FP, we need to find some register in which to
1840      save FP.  */
1841
1842   save_fp_regno = -1;
1843
1844   if (base_regno == HARD_FRAME_POINTER_REGNUM)
1845     for (i = 0; i < 32; i++)
1846       if (! fixed_regs[i] && call_used_regs[i] && ! regs_ever_live[i])
1847         save_fp_regno = i;
1848
1849   if (save_fp_regno == -1)
1850     base_regno = REG_PV, is_stack_procedure = 1;
1851
1852   /* Stack unwinding should be done via FP unless we use it for PV.  */
1853   unwind_regno
1854     = base_regno == REG_PV ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM;
1855
1856   /* If this is a stack procedure, allow space for saving FP and RA.  */
1857   if (is_stack_procedure)
1858     sa_size += 2;
1859
1860   return sa_size * 8;
1861 }
1862
1863 int
1864 alpha_pv_save_size ()
1865 {
1866   alpha_sa_size ();
1867   return is_stack_procedure ? 8 : 0;
1868 }
1869
1870 int
1871 alpha_using_fp ()
1872 {
1873   alpha_sa_size ();
1874   return unwind_regno == HARD_FRAME_POINTER_REGNUM;
1875 }
1876
1877 #else /* ! OPEN_VMS */
1878
1879 int
1880 alpha_sa_size ()
1881 {
1882   int size = 0;
1883   int i;
1884
1885   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1886     if (! fixed_regs[i] && ! call_used_regs[i]
1887         && regs_ever_live[i] && i != REG_RA)
1888       size++;
1889
1890   /* If some registers were saved but not reg 26, reg 26 must also
1891      be saved, so leave space for it.  */
1892   if (size != 0 || alpha_ra_ever_killed ())
1893     size++;
1894
1895   /* Our size must be even (multiple of 16 bytes).  */
1896   if (size & 1)
1897     size ++;
1898
1899   return size * 8;
1900 }
1901
1902 #endif /* ! OPEN_VMS */
1903
1904 /* Return 1 if this function can directly return via $26.  */
1905
1906 int
1907 direct_return ()
1908 {
1909   return (! TARGET_OPEN_VMS && reload_completed && alpha_sa_size () == 0
1910           && get_frame_size () == 0
1911           && current_function_outgoing_args_size == 0
1912           && current_function_pretend_args_size == 0);
1913 }
1914
1915 /* Write a version stamp.  Don't write anything if we are running as a
1916    cross-compiler.  Otherwise, use the versions in /usr/include/stamp.h.  */
1917
1918 #if !defined(CROSS_COMPILE) && !defined(_WIN32) && !defined(__linux__) && !defined(VMS)
1919 #include <stamp.h>
1920 #endif
1921
1922 void
1923 alpha_write_verstamp (file)
1924      FILE *file;
1925 {
1926 #ifdef MS_STAMP
1927   fprintf (file, "\t.verstamp %d %d\n", MS_STAMP, LS_STAMP);
1928 #endif
1929 }
1930 \f
1931 /* Write code to add constant C to register number IN_REG (possibly 31)
1932    and put the result into OUT_REG.  Use TEMP_REG as a scratch register;
1933    usually this will be OUT_REG, but should not be if OUT_REG is 
1934    STACK_POINTER_REGNUM, since it must be updated in a single instruction.
1935    Write the code to FILE.  */
1936
1937 static void
1938 add_long_const (file, c, in_reg, out_reg, temp_reg)
1939      FILE *file;
1940      HOST_WIDE_INT c;
1941      int in_reg, out_reg, temp_reg;
1942 {
1943   HOST_WIDE_INT low = (c & 0xffff) - 2 * (c & 0x8000);
1944   HOST_WIDE_INT tmp1 = c - low;
1945   HOST_WIDE_INT high = ((tmp1 >> 16) & 0xffff) - 2 * ((tmp1 >> 16) & 0x8000);
1946   HOST_WIDE_INT extra = 0;
1947
1948   /* We don't have code to write out constants larger than 32 bits.  */
1949 #if HOST_BITS_PER_LONG_INT == 64
1950   if ((unsigned HOST_WIDE_INT) c >> 32 != 0)
1951     abort ();
1952 #endif
1953
1954   /* If HIGH will be interpreted as negative, we must adjust it to do two
1955      ldha insns.  Note that we will never be building a negative constant
1956      here.  */
1957
1958   if (high & 0x8000)
1959     {
1960       extra = 0x4000;
1961       tmp1 -= 0x40000000;
1962       high = ((tmp1 >> 16) & 0xffff) - 2 * ((tmp1 >> 16) & 0x8000);
1963     }
1964
1965   if (low != 0)
1966     {
1967       int result_reg = (extra == 0 && high == 0) ? out_reg : temp_reg;
1968
1969       if (low >= 0 && low < 255)
1970         fprintf (file, "\taddq $%d,%d,$%d\n", in_reg, low, result_reg);
1971       else
1972         fprintf (file, "\tlda $%d,%d($%d)\n", result_reg, low, in_reg);
1973
1974       in_reg = result_reg;
1975     }
1976
1977   if (extra)
1978     {
1979       int result_reg = (high == 0) ? out_reg : temp_reg;
1980
1981       fprintf (file, "\tldah $%d,%d($%d)\n", result_reg, extra, in_reg);
1982       in_reg = result_reg;
1983     }
1984
1985   if (high)
1986     fprintf (file, "\tldah $%d,%d($%d)\n", out_reg, high, in_reg);
1987 }
1988
1989 /* Write function prologue.  */
1990
1991 #if OPEN_VMS
1992
1993 /* On vms we have two kinds of functions:
1994
1995    - stack frame (PROC_STACK)
1996         these are 'normal' functions with local vars and which are
1997         calling other functions
1998    - register frame (PROC_REGISTER)
1999         keeps all data in registers, needs no stack
2000
2001    We must pass this to the assembler so it can generate the
2002    proper pdsc (procedure descriptor)
2003    This is done with the '.pdesc' command.
2004
2005    size is the stack size needed for local variables.  */
2006
2007 void
2008 output_prolog (file, size)
2009      FILE *file;
2010      HOST_WIDE_INT size;
2011 {
2012   unsigned long imask = 0;
2013   unsigned long fmask = 0;
2014   /* Stack space needed for pushing registers clobbered by us.  */
2015   HOST_WIDE_INT sa_size;
2016   /* Complete stack size needed.  */
2017   HOST_WIDE_INT frame_size;
2018   /* Offset from base reg to register save area.  */
2019   int rsa_offset = 8;
2020   /* Offset during register save.  */
2021   int reg_offset;
2022   /* Label for the procedure entry.  */
2023   char *entry_label = (char *) alloca (strlen (alpha_function_name) + 5);
2024   int i;
2025
2026   sa_size = alpha_sa_size ();
2027   frame_size
2028     = ALPHA_ROUND (sa_size 
2029                    + (is_stack_procedure ? 8 : 0)
2030                    + size + current_function_pretend_args_size);
2031
2032   /* Issue function start and label.  */
2033   fprintf (file, "\t.ent ");
2034   assemble_name (file, alpha_function_name);
2035   fprintf (file, "\n");
2036   sprintf (entry_label, "%s..en", alpha_function_name);
2037   ASM_OUTPUT_LABEL (file, entry_label);
2038   inside_function = TRUE;
2039
2040   fprintf (file, "\t.base $%d\n", base_regno);
2041
2042   /* Calculate register masks for clobbered registers.  */
2043
2044   if (is_stack_procedure)
2045     alpha_sa_mask (&imask, &fmask);
2046
2047   /* Adjust the stack by the frame size.  If the frame size is > 4096
2048      bytes, we need to be sure we probe somewhere in the first and last
2049      4096 bytes (we can probably get away without the latter test) and
2050      every 8192 bytes in between.  If the frame size is > 32768, we
2051      do this in a loop.  Otherwise, we generate the explicit probe
2052      instructions. 
2053
2054      Note that we are only allowed to adjust sp once in the prologue.  */
2055
2056   if (frame_size < 32768)
2057     {
2058       if (frame_size > 4096)
2059         {
2060           int probed = 4096;
2061
2062           fprintf (file, "\tstq $31,-%d($30)\n", probed);
2063
2064           while (probed + 8192 < frame_size)
2065             fprintf (file, "\tstq $31,-%d($30)\n", probed += 8192);
2066
2067           /* We only have to do this probe if we aren't saving registers.  */
2068           if (sa_size == 0 && probed + 4096 < frame_size)
2069             fprintf (file, "\tstq $31,-%d($30)\n", frame_size);
2070         }
2071
2072       if (frame_size != 0)
2073           fprintf (file, "\tlda $30,-%d($30)\n", frame_size);
2074     }
2075   else
2076     {
2077       /* Here we generate code to set R4 to SP + 4096 and set R23 to the
2078          number of 8192 byte blocks to probe.  We then probe each block
2079          in the loop and then set SP to the proper location.  If the
2080          amount remaining is > 4096, we have to do one more probe if we
2081          are not saving any registers.  */
2082
2083       HOST_WIDE_INT blocks = (frame_size + 4096) / 8192;
2084       HOST_WIDE_INT leftover = frame_size + 4096 - blocks * 8192;
2085
2086       add_long_const (file, blocks, 31, 23, 23);
2087
2088       fprintf (file, "\tlda $22,4096($30)\n");
2089
2090       assemble_name (file, alpha_function_name);
2091       fprintf (file, "..sc:\n");
2092
2093       fprintf (file, "\tstq $31,-8192($22)\n");
2094       fprintf (file, "\tsubq $23,1,$23\n");
2095       fprintf (file, "\tlda $22,-8192($22)\n");
2096
2097       fprintf (file, "\tbne $23,");
2098       assemble_name (file, alpha_function_name);
2099       fprintf (file, "..sc\n");
2100
2101       if (leftover > 4096 && sa_size == 0)
2102         fprintf (file, "\tstq $31,-%d($22)\n", leftover);
2103
2104       fprintf (file, "\tlda $30,-%d($22)\n", leftover);
2105     }
2106
2107   if (is_stack_procedure)
2108     {
2109       int reg_offset = rsa_offset;
2110
2111       /* Store R26 (RA) first.  */
2112       fprintf (file, "\tstq $26,%d($30)\n", reg_offset);
2113       reg_offset += 8;
2114
2115       /* Store integer regs. according to mask.  */
2116       for (i = 0; i < 32; i++)
2117         if (imask & (1L<<i))
2118           {
2119             fprintf (file, "\tstq $%d,%d($30)\n", i, reg_offset);
2120             reg_offset += 8;
2121           }
2122
2123       /* Print the register mask and do floating-point saves.  */
2124
2125       if (imask)
2126         fprintf (file, "\t.mask 0x%x,0\n", imask);
2127
2128       for (i = 0; i < 32; i++)
2129         {
2130           if (fmask & (1L << i))
2131             {
2132               fprintf (file, "\tstt $f%d,%d($30)\n", i, reg_offset);
2133               reg_offset += 8;
2134             }
2135         }
2136
2137       /* Print the floating-point mask, if we've saved any fp register.  */
2138       if (fmask)
2139         fprintf (file, "\t.fmask 0x%x,0\n", fmask);
2140
2141       fprintf (file, "\tstq $27,0($30)\n");
2142     }
2143   else 
2144     {
2145       fprintf (file, "\t.fp_save $%d\n", save_fp_regno);
2146       fprintf (file, "\tbis $%d,$%d,$%d\n", HARD_FRAME_POINTER_REGNUM,
2147                HARD_FRAME_POINTER_REGNUM, save_fp_regno);
2148     }
2149
2150   if (base_regno != REG_PV)
2151     fprintf (file, "\tbis $%d,$%d,$%d\n", REG_PV, REG_PV, base_regno);
2152
2153   if (unwind_regno == HARD_FRAME_POINTER_REGNUM)
2154     fprintf (file, "\tbis $%d,$%d,$%d\n", STACK_POINTER_REGNUM,
2155              STACK_POINTER_REGNUM, HARD_FRAME_POINTER_REGNUM);
2156
2157   /* Describe our frame.  */
2158   fprintf (file, "\t.frame $%d,%d,$26,%d\n", 
2159            unwind_regno, frame_size, rsa_offset);
2160
2161   /* If we have to allocate space for outgoing args, do it now.  */
2162   if (current_function_outgoing_args_size != 0)
2163     fprintf (file, "\tlda $%d,%d($%d)\n", STACK_POINTER_REGNUM,
2164              - ALPHA_ROUND (current_function_outgoing_args_size),
2165              HARD_FRAME_POINTER_REGNUM);
2166
2167   fprintf (file, "\t.prologue\n");
2168
2169   link_section ();
2170   fprintf (file, "\t.align 3\n");
2171   ASM_OUTPUT_LABEL (file, alpha_function_name);
2172   fprintf (file, "\t.pdesc ");
2173   assemble_name (file, alpha_function_name);
2174   fprintf (file, "..en,%s\n", is_stack_procedure ? "stack" : "reg");
2175   alpha_need_linkage (alpha_function_name, 1);
2176   text_section ();
2177
2178   return;
2179 }
2180
2181 /* Write function epilogue.  */
2182
2183 void
2184 output_epilog (file, size)
2185      FILE *file;
2186      int size;
2187 {
2188   unsigned long imask = 0;
2189   unsigned long fmask = 0;
2190   /* Stack space needed for pushing registers clobbered by us.  */
2191   HOST_WIDE_INT sa_size = alpha_sa_size ();
2192   /* Complete stack size needed.  */
2193   HOST_WIDE_INT frame_size
2194     = ALPHA_ROUND (sa_size
2195                    + (is_stack_procedure ? 8 : 0)
2196                    + size + current_function_pretend_args_size);
2197   int i;
2198   rtx insn = get_last_insn ();
2199
2200   /* If the last insn was a BARRIER, we don't have to write anything except
2201      the .end pseudo-op.  */
2202
2203   if (GET_CODE (insn) == NOTE)
2204     insn = prev_nonnote_insn (insn);
2205
2206   if (insn == 0 || GET_CODE (insn) != BARRIER)
2207     {
2208       /* Restore clobbered registers, load FP last.  */
2209
2210       if (is_stack_procedure)
2211         {
2212           int rsa_offset = 8;
2213           int reg_offset;
2214           int fp_offset;
2215
2216           if (unwind_regno == HARD_FRAME_POINTER_REGNUM)
2217             fprintf (file, "\tbis $%d,$%d,$%d\n", HARD_FRAME_POINTER_REGNUM,
2218                      HARD_FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM);
2219
2220           alpha_sa_mask (&imask, &fmask);
2221
2222           /* Start reloading registers after RA.  */
2223           reg_offset = rsa_offset + 8;
2224
2225           for (i = 0; i < 32; i++)
2226             if (imask & (1L<<i))
2227               {
2228                 if (i == HARD_FRAME_POINTER_REGNUM)
2229                   fp_offset = reg_offset;
2230                 else
2231                   fprintf (file, "\tldq $%d,%d($30)\n",
2232                                   i, reg_offset);
2233                 reg_offset += 8;
2234               }
2235
2236           for (i = 0; i < 32; i++)
2237             if (fmask & (1L << i))
2238               {
2239                 fprintf (file, "\tldt $f%d,%d($30)\n", i, reg_offset);
2240                 reg_offset += 8;
2241               }
2242
2243           /* Restore R26 (RA).  */
2244           fprintf (file, "\tldq $26,%d($30)\n", rsa_offset);
2245
2246           /* Restore R29 (FP).  */
2247           fprintf (file, "\tldq $29,%d($30)\n", fp_offset);
2248         }
2249       else
2250         fprintf (file, "\tbis $%d,$%d,$%d\n", save_fp_regno, save_fp_regno,
2251                  HARD_FRAME_POINTER_REGNUM);
2252
2253       if (frame_size != 0)
2254         {
2255           if (frame_size < 32768)
2256             fprintf (file, "\tlda $30,%d($30)\n", frame_size);
2257           else
2258             {
2259               long high = frame_size >> 16;
2260               long low = frame_size & 0xffff;
2261               if (low & 0x8000)
2262                 {
2263                   high++;
2264                   low = -32768 + (low & 0x7fff);
2265                 }
2266               fprintf (file, "\tldah $2,%ld($31)\n", high);
2267               fprintf (file, "\tlda $2,%ld($2)\n", low);
2268               fprintf (file, "\taddq $30,$2,$30\n");
2269             }
2270         }
2271
2272       /* Finally return to the caller.  */
2273       fprintf (file, "\tret $31,($26),1\n");
2274     }
2275
2276   /* End the function.  */
2277   fprintf (file, "\t.end ");
2278   assemble_name (file,  alpha_function_name);
2279   fprintf (file, "\n");
2280   inside_function = FALSE;
2281
2282   /* Show that we know this function if it is called again.  */
2283   SYMBOL_REF_FLAG (XEXP (DECL_RTL (current_function_decl), 0)) = 1;
2284 }
2285
2286 #else /* !OPEN_VMS */
2287
2288 static int
2289 alpha_does_function_need_gp ()
2290 {
2291   rtx insn;
2292
2293   /* We never need a GP for Windows/NT.  */
2294   if (TARGET_WINDOWS_NT)
2295     return 0;
2296
2297 #ifdef TARGET_PROFILING_NEEDS_GP
2298   if (profile_flag)
2299     return 1;
2300 #endif
2301
2302   /* If we need a GP (we have a LDSYM insn or a CALL_INSN), load it first. 
2303      Even if we are a static function, we still need to do this in case
2304      our address is taken and passed to something like qsort.  */
2305
2306   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2307     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2308         && GET_CODE (PATTERN (insn)) != USE
2309         && GET_CODE (PATTERN (insn)) != CLOBBER)
2310       {
2311         enum attr_type type = get_attr_type (insn);
2312         if (type == TYPE_LDSYM || type == TYPE_JSR)
2313           return 1;
2314       }
2315
2316   return 0;
2317 }
2318
2319 void
2320 output_prolog (file, size)
2321      FILE *file;
2322      int size;
2323 {
2324   HOST_WIDE_INT out_args_size
2325     = ALPHA_ROUND (current_function_outgoing_args_size);
2326   HOST_WIDE_INT sa_size = alpha_sa_size ();
2327   HOST_WIDE_INT frame_size
2328     = (out_args_size + sa_size
2329        + ALPHA_ROUND (size + current_function_pretend_args_size));
2330   HOST_WIDE_INT reg_offset = out_args_size;
2331   HOST_WIDE_INT start_reg_offset = reg_offset;
2332   HOST_WIDE_INT actual_start_reg_offset = start_reg_offset;
2333   int int_reg_save_area_size = 0;
2334   unsigned reg_mask = 0;
2335   int i, sa_reg;
2336
2337   /* Ecoff can handle multiple .file directives, so put out file and lineno.
2338      We have to do that before the .ent directive as we cannot switch
2339      files within procedures with native ecoff because line numbers are
2340      linked to procedure descriptors.
2341      Outputting the lineno helps debugging of one line functions as they
2342      would otherwise get no line number at all. Please note that we would
2343      like to put out last_linenum from final.c, but it is not accessible.  */
2344
2345   if (write_symbols == SDB_DEBUG)
2346     {
2347       ASM_OUTPUT_SOURCE_FILENAME (file,
2348                                   DECL_SOURCE_FILE (current_function_decl));
2349       if (debug_info_level != DINFO_LEVEL_TERSE)
2350         ASM_OUTPUT_SOURCE_LINE (file,
2351                                 DECL_SOURCE_LINE (current_function_decl));
2352     }
2353
2354   /* The assembly language programmer's guide states that the second argument
2355      to the .ent directive, the lex_level, is ignored by the assembler,
2356      so we might as well omit it.  */
2357      
2358   if (!flag_inhibit_size_directive)
2359     {
2360       fprintf (file, "\t.ent ");
2361       assemble_name (file, alpha_function_name);
2362       fprintf (file, "\n");
2363     }
2364   ASM_OUTPUT_LABEL (file, alpha_function_name);
2365   inside_function = TRUE;
2366
2367   if (TARGET_IEEE_CONFORMANT && !flag_inhibit_size_directive)
2368     /* Set flags in procedure descriptor to request IEEE-conformant
2369        math-library routines.  The value we set it to is PDSC_EXC_IEEE
2370        (/usr/include/pdsc.h). */
2371     fprintf (file, "\t.eflag 48\n");
2372
2373   /* Set up offsets to alpha virtual arg/local debugging pointer.  */
2374
2375   alpha_auto_offset = -frame_size + current_function_pretend_args_size;
2376   alpha_arg_offset = -frame_size + 48;
2377
2378   alpha_function_needs_gp = alpha_does_function_need_gp ();
2379
2380   if (TARGET_WINDOWS_NT == 0)
2381     {
2382       if (alpha_function_needs_gp)
2383         fprintf (file, "\tldgp $29,0($27)\n");
2384
2385       /* Put a label after the GP load so we can enter the function at it.  */
2386       assemble_name (file, alpha_function_name);
2387       fprintf (file, "..ng:\n");
2388     }
2389
2390   /* Adjust the stack by the frame size.  If the frame size is > 4096
2391      bytes, we need to be sure we probe somewhere in the first and last
2392      4096 bytes (we can probably get away without the latter test) and
2393      every 8192 bytes in between.  If the frame size is > 32768, we
2394      do this in a loop.  Otherwise, we generate the explicit probe
2395      instructions. 
2396
2397      Note that we are only allowed to adjust sp once in the prologue.  */
2398
2399   if (frame_size < 32768)
2400     {
2401       if (frame_size > 4096)
2402         {
2403           int probed = 4096;
2404
2405           fprintf (file, "\tstq $31,-%d($30)\n", probed);
2406
2407           while (probed + 8192 < frame_size)
2408             fprintf (file, "\tstq $31,-%d($30)\n", probed += 8192);
2409
2410           /* We only have to do this probe if we aren't saving registers.  */
2411           if (sa_size == 0 && probed + 4096 < frame_size)
2412             fprintf (file, "\tstq $31,-%d($30)\n", frame_size);
2413         }
2414
2415       if (frame_size != 0)
2416         fprintf (file, "\tlda $30,-%d($30)\n", frame_size);
2417     }
2418   else
2419     {
2420       /* Here we generate code to set R4 to SP + 4096 and set R5 to the
2421          number of 8192 byte blocks to probe.  We then probe each block
2422          in the loop and then set SP to the proper location.  If the
2423          amount remaining is > 4096, we have to do one more probe if we
2424          are not saving any registers.  */
2425
2426       HOST_WIDE_INT blocks = (frame_size + 4096) / 8192;
2427       HOST_WIDE_INT leftover = frame_size + 4096 - blocks * 8192;
2428
2429       add_long_const (file, blocks, 31, 5, 5);
2430
2431       fprintf (file, "\tlda $4,4096($30)\n");
2432
2433       assemble_name (file, alpha_function_name);
2434       fprintf (file, "..sc:\n");
2435
2436       fprintf (file, "\tstq $31,-8192($4)\n");
2437       fprintf (file, "\tsubq $5,1,$5\n");
2438       fprintf (file, "\tlda $4,-8192($4)\n");
2439
2440       fprintf (file, "\tbne $5,");
2441       assemble_name (file, alpha_function_name);
2442       fprintf (file, "..sc\n");
2443
2444       if (leftover > 4096 && sa_size == 0)
2445         fprintf (file, "\tstq $31,-%d($4)\n", leftover);
2446
2447       fprintf (file, "\tlda $30,-%d($4)\n", leftover);
2448     }
2449
2450   /* Describe our frame.  */
2451   if (!flag_inhibit_size_directive)
2452     {
2453       fprintf (file, "\t.frame $%d,%d,$26,%d\n", 
2454                (frame_pointer_needed
2455                 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM),
2456                frame_size, current_function_pretend_args_size);
2457     }
2458
2459   /* Cope with very large offsets to the register save area.  */
2460   sa_reg = 30;
2461   if (reg_offset + sa_size > 0x8000)
2462     {
2463       int low = ((reg_offset & 0xffff) ^ 0x8000) - 0x8000;
2464       if (low + sa_size <= 0x8000)
2465         {
2466           add_long_const (file, reg_offset - low, 30, 24, 24);
2467           reg_offset = low;
2468         }
2469       else
2470         {
2471           add_long_const (file, reg_offset, 30, 24, 24);
2472           reg_offset = 0;
2473         }
2474       sa_reg = 24;
2475     }
2476     
2477   /* Save register RA if any other register needs to be saved.  */
2478   if (sa_size != 0)
2479     {
2480       reg_mask |= 1 << REG_RA;
2481       fprintf (file, "\tstq $26,%d($%d)\n", reg_offset, sa_reg);
2482       reg_offset += 8;
2483       int_reg_save_area_size += 8;
2484     }
2485
2486   /* Now save any other used integer registers required to be saved.  */
2487   for (i = 0; i < 32; i++)
2488     if (! fixed_regs[i] && ! call_used_regs[i]
2489         && regs_ever_live[i] && i != REG_RA)
2490       {
2491         reg_mask |= 1 << i;
2492         fprintf (file, "\tstq $%d,%d($%d)\n", i, reg_offset, sa_reg);
2493         reg_offset += 8;
2494         int_reg_save_area_size += 8;
2495       }
2496
2497   /* Print the register mask and do floating-point saves.  */
2498   if (reg_mask && !flag_inhibit_size_directive)
2499     fprintf (file, "\t.mask 0x%x,%d\n", reg_mask,
2500              actual_start_reg_offset - frame_size);
2501
2502   start_reg_offset = reg_offset;
2503   reg_mask = 0;
2504
2505   for (i = 0; i < 32; i++)
2506     if (! fixed_regs[i + 32] && ! call_used_regs[i + 32]
2507         && regs_ever_live[i + 32])
2508       {
2509         reg_mask |= 1 << i;
2510         fprintf (file, "\tstt $f%d,%d($%d)\n", i, reg_offset, sa_reg);
2511         reg_offset += 8;
2512       }
2513
2514   /* Print the floating-point mask, if we've saved any fp register.  */
2515   if (reg_mask && !flag_inhibit_size_directive)
2516     fprintf (file, "\t.fmask 0x%x,%d\n", reg_mask,
2517              actual_start_reg_offset - frame_size + int_reg_save_area_size);
2518
2519   /* If we need a frame pointer, set it from the stack pointer.  Note that
2520      this must always be the last instruction in the prologue.  */
2521   if (frame_pointer_needed)
2522     fprintf (file, "\tbis $30,$30,$15\n");
2523
2524   /* End the prologue and say if we used gp.  */
2525   if (!flag_inhibit_size_directive)
2526     fprintf (file, "\t.prologue %d\n", alpha_function_needs_gp);
2527 }
2528
2529 /* Write function epilogue.  */
2530
2531 void
2532 output_epilog (file, size)
2533      FILE *file;
2534      int size;
2535 {
2536   rtx insn = get_last_insn ();
2537   HOST_WIDE_INT out_args_size
2538     = ALPHA_ROUND (current_function_outgoing_args_size);
2539   HOST_WIDE_INT sa_size = alpha_sa_size ();
2540   HOST_WIDE_INT frame_size
2541     = (out_args_size + sa_size
2542        + ALPHA_ROUND (size + current_function_pretend_args_size));
2543   HOST_WIDE_INT reg_offset = out_args_size;
2544   HOST_WIDE_INT frame_size_from_reg_save = frame_size - reg_offset;
2545   int restore_fp
2546     = frame_pointer_needed && regs_ever_live[HARD_FRAME_POINTER_REGNUM];
2547   int i;
2548
2549   /* If the last insn was a BARRIER, we don't have to write anything except
2550      the .end pseudo-op.  */
2551   if (GET_CODE (insn) == NOTE)
2552     insn = prev_nonnote_insn (insn);
2553   if (insn == 0 || GET_CODE (insn) != BARRIER)
2554     {
2555       int fp_offset = 0;
2556       int sa_reg;
2557
2558       /* If we have a frame pointer, restore SP from it.  */
2559       if (frame_pointer_needed)
2560         fprintf (file, "\tbis $15,$15,$30\n");
2561
2562       /* Cope with large offsets to the register save area.  */
2563       sa_reg = 30;
2564       if (reg_offset + sa_size > 0x8000)
2565         {
2566           int low = ((reg_offset & 0xffff) ^ 0x8000) - 0x8000;
2567           if (low + sa_size <= 0x8000)
2568             {
2569               add_long_const (file, reg_offset - low, 30, 24, 24);
2570               reg_offset = low;
2571             }
2572           else
2573             {
2574               add_long_const (file, reg_offset, 30, 24, 24);
2575               reg_offset = 0;
2576             }
2577           sa_reg = 24;
2578         }
2579
2580       /* Restore all the registers, starting with the return address
2581          register.  */
2582       if (sa_size != 0)
2583         {
2584           fprintf (file, "\tldq $26,%d($%d)\n", reg_offset, sa_reg);
2585           reg_offset += 8;
2586         }
2587
2588       /* Now restore any other used integer registers that that we saved,
2589          except for FP if it is being used as FP, since it must be
2590          restored last.  */
2591
2592       for (i = 0; i < 32; i++)
2593         if (! fixed_regs[i] && ! call_used_regs[i] && regs_ever_live[i]
2594             && i != 26)
2595           {
2596             if (i == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
2597               fp_offset = reg_offset;
2598             else
2599               fprintf (file, "\tldq $%d,%d($%d)\n", i, reg_offset, sa_reg);
2600             reg_offset += 8;
2601           }
2602
2603       for (i = 0; i < 32; i++)
2604         if (! fixed_regs[i + 32] && ! call_used_regs[i + 32]
2605             && regs_ever_live[i + 32])
2606           {
2607             fprintf (file, "\tldt $f%d,%d($%d)\n", i, reg_offset, sa_reg);
2608             reg_offset += 8;
2609           }
2610
2611       /* If the stack size is large and we have a frame pointer, compute the
2612          size of the stack into a register because the old FP restore, stack
2613          pointer adjust, and return are required to be consecutive
2614          instructions.   */
2615       if (frame_size > 32767 && restore_fp)
2616         add_long_const (file, frame_size, 31, 1, 1);
2617
2618       /* If we needed a frame pointer and we have to restore it, do it
2619          now.  This must be done in one instruction immediately
2620          before the SP update.  */
2621       if (restore_fp && fp_offset)
2622         fprintf (file, "\tldq $15,%d($%d)\n", fp_offset, sa_reg);
2623
2624       /* Now update the stack pointer, if needed.  Only one instruction must
2625          modify the stack pointer.  It must be the last instruction in the
2626          sequence and must be an ADDQ or LDA instruction.  If the frame
2627          pointer was loaded above, we may only put one instruction here.  */
2628
2629       if (frame_size > 32768 && restore_fp)
2630         fprintf  (file, "\taddq $1,$30,$30\n");
2631       else
2632         add_long_const (file, frame_size, 30, 30, 1);
2633
2634       /* Finally return to the caller.  */
2635       fprintf (file, "\tret $31,($26),1\n");
2636     }
2637
2638   /* End the function.  */
2639   if (!flag_inhibit_size_directive)
2640     {
2641       fprintf (file, "\t.end ");
2642       assemble_name (file, alpha_function_name);
2643       fprintf (file, "\n");
2644     }
2645   inside_function = FALSE;
2646
2647   /* Show that we know this function if it is called again.  */
2648   SYMBOL_REF_FLAG (XEXP (DECL_RTL (current_function_decl), 0)) = 1;
2649 }
2650 #endif /* !OPEN_VMS */
2651 \f
2652 /* Debugging support.  */
2653
2654 #include "gstab.h"
2655
2656 /* Count the number of sdb related labels are generated (to find block
2657    start and end boundaries).  */
2658
2659 int sdb_label_count = 0;
2660
2661 /* Next label # for each statement.  */
2662
2663 static int sym_lineno = 0;
2664
2665 /* Count the number of .file directives, so that .loc is up to date.  */
2666
2667 static int num_source_filenames = 0;
2668
2669 /* Name of the file containing the current function.  */
2670
2671 static char *current_function_file = "";
2672
2673 /* Offsets to alpha virtual arg/local debugging pointers.  */
2674
2675 long alpha_arg_offset;
2676 long alpha_auto_offset;
2677 \f
2678 /* Emit a new filename to a stream.  */
2679
2680 void
2681 alpha_output_filename (stream, name)
2682      FILE *stream;
2683      char *name;
2684 {
2685   static int first_time = TRUE;
2686   char ltext_label_name[100];
2687
2688   if (first_time)
2689     {
2690       first_time = FALSE;
2691       ++num_source_filenames;
2692       current_function_file = name;
2693       fprintf (stream, "\t.file\t%d ", num_source_filenames);
2694       output_quoted_string (stream, name);
2695       fprintf (stream, "\n");
2696       if (!TARGET_GAS && write_symbols == DBX_DEBUG)
2697         fprintf (stream, "\t#@stabs\n");
2698     }
2699
2700   else if (write_symbols == DBX_DEBUG)
2701     {
2702       ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
2703       fprintf (stream, "%s ", ASM_STABS_OP);
2704       output_quoted_string (stream, name);
2705       fprintf (stream, ",%d,0,0,%s\n", N_SOL, &ltext_label_name[1]);
2706     }
2707
2708   else if (name != current_function_file
2709       && strcmp (name, current_function_file) != 0)
2710     {
2711       if (inside_function && ! TARGET_GAS)
2712         fprintf (stream, "\t#.file\t%d ", num_source_filenames);
2713       else
2714         {
2715           ++num_source_filenames;
2716           current_function_file = name;
2717           fprintf (stream, "\t.file\t%d ", num_source_filenames);
2718         }
2719
2720       output_quoted_string (stream, name);
2721       fprintf (stream, "\n");
2722     }
2723 }
2724 \f
2725 /* Emit a linenumber to a stream.  */
2726
2727 void
2728 alpha_output_lineno (stream, line)
2729      FILE *stream;
2730      int line;
2731 {
2732   if (write_symbols == DBX_DEBUG)
2733     {
2734       /* mips-tfile doesn't understand .stabd directives.  */
2735       ++sym_lineno;
2736       fprintf (stream, "$LM%d:\n\t%s %d,0,%d,$LM%d\n",
2737                sym_lineno, ASM_STABN_OP, N_SLINE, line, sym_lineno);
2738     }
2739   else
2740     fprintf (stream, "\n\t.loc\t%d %d\n", num_source_filenames, line);
2741 }
2742 \f
2743 /* Structure to show the current status of registers and memory.  */
2744
2745 struct shadow_summary
2746 {
2747   struct {
2748     unsigned long i     : 31;   /* Mask of int regs */
2749     unsigned long fp    : 31;   /* Mask of fp regs */
2750     unsigned long mem   :  1;   /* mem == imem | fpmem */
2751   } used, defd;
2752 };
2753
2754 /* Summary the effects of expression X on the machine.  Update SUM, a pointer
2755    to the summary structure.  SET is nonzero if the insn is setting the
2756    object, otherwise zero.  */
2757
2758 static void
2759 summarize_insn (x, sum, set)
2760      rtx x;
2761      struct shadow_summary *sum;
2762      int set;
2763 {
2764   char *format_ptr;
2765   int i, j;
2766
2767   if (x == 0)
2768     return;
2769
2770   switch (GET_CODE (x))
2771     {
2772       /* ??? Note that this case would be incorrect if the Alpha had a
2773          ZERO_EXTRACT in SET_DEST.  */
2774     case SET:
2775       summarize_insn (SET_SRC (x), sum, 0);
2776       summarize_insn (SET_DEST (x), sum, 1);
2777       break;
2778
2779     case CLOBBER:
2780       summarize_insn (XEXP (x, 0), sum, 1);
2781       break;
2782
2783     case USE:
2784       summarize_insn (XEXP (x, 0), sum, 0);
2785       break;
2786
2787     case PARALLEL:
2788       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2789         summarize_insn (XVECEXP (x, 0, i), sum, 0);
2790       break;
2791
2792     case REG:
2793       {
2794         int regno = REGNO (x);
2795         unsigned long mask = 1UL << (regno % 32);
2796
2797         if (regno == 31 || regno == 63)
2798           break;
2799
2800         if (set)
2801           {
2802             if (regno < 32)
2803               sum->defd.i |= mask;
2804             else
2805               sum->defd.fp |= mask;
2806           }
2807         else
2808           {
2809             if (regno < 32)
2810               sum->used.i  |= mask;
2811             else
2812               sum->used.fp |= mask;
2813           }
2814         }
2815       break;
2816
2817     case MEM:
2818       if (set)
2819         sum->defd.mem = 1;
2820       else
2821         sum->used.mem = 1;
2822
2823       /* Find the regs used in memory address computation: */
2824       summarize_insn (XEXP (x, 0), sum, 0);
2825       break;
2826
2827     case SUBREG:
2828       summarize_insn (SUBREG_REG (x), sum, 0);
2829       break;
2830
2831     case CONST_INT:   case CONST_DOUBLE:
2832     case SYMBOL_REF:  case LABEL_REF:     case CONST:
2833       break;
2834
2835       /* Handle common unary and binary ops for efficiency.  */
2836     case COMPARE:  case PLUS:    case MINUS:   case MULT:      case DIV:
2837     case MOD:      case UDIV:    case UMOD:    case AND:       case IOR:
2838     case XOR:      case ASHIFT:  case ROTATE:  case ASHIFTRT:  case LSHIFTRT:
2839     case ROTATERT: case SMIN:    case SMAX:    case UMIN:      case UMAX:
2840     case NE:       case EQ:      case GE:      case GT:        case LE:
2841     case LT:       case GEU:     case GTU:     case LEU:       case LTU:
2842       summarize_insn (XEXP (x, 0), sum, 0);
2843       summarize_insn (XEXP (x, 1), sum, 0);
2844       break;
2845
2846     case NEG:  case NOT:  case SIGN_EXTEND:  case ZERO_EXTEND:
2847     case TRUNCATE:  case FLOAT_EXTEND:  case FLOAT_TRUNCATE:  case FLOAT:
2848     case FIX:  case UNSIGNED_FLOAT:  case UNSIGNED_FIX:  case ABS:
2849     case SQRT:  case FFS: 
2850       summarize_insn (XEXP (x, 0), sum, 0);
2851       break;
2852
2853     default:
2854       format_ptr = GET_RTX_FORMAT (GET_CODE (x));
2855       for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2856         switch (format_ptr[i])
2857           {
2858           case 'e':
2859             summarize_insn (XEXP (x, i), sum, 0);
2860             break;
2861
2862           case 'E':
2863             for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2864               summarize_insn (XVECEXP (x, i, j), sum, 0);
2865             break;
2866
2867           default:
2868             abort ();
2869           }
2870     }
2871 }
2872
2873 /* Ensure a sufficient number of `trapb' insns are in the code when the user
2874    requests code with a trap precision of functions or instructions.
2875
2876    In naive mode, when the user requests a trap-precision of "instruction", a
2877    trapb is needed after every instruction that may generate a trap (and after
2878    jsr/bsr instructions, because called functions may import a trap from the
2879    caller).  This ensures that the code is resumption safe but it is also slow.
2880
2881    When optimizations are turned on, we delay issuing a trapb as long as
2882    possible.  In this context, a trap shadow is the sequence of instructions
2883    that starts with a (potentially) trap generating instruction and extends to
2884    the next trapb or call_pal instruction (but GCC never generates call_pal by
2885    itself).  We can delay (and therefore sometimes omit) a trapb subject to the
2886    following conditions:
2887
2888    (a) On entry to the trap shadow, if any Alpha register or memory location
2889    contains a value that is used as an operand value by some instruction in
2890    the trap shadow (live on entry), then no instruction in the trap shadow
2891    may modify the register or memory location.
2892
2893    (b) Within the trap shadow, the computation of the base register for a
2894    memory load or store instruction may not involve using the result
2895    of an instruction that might generate an UNPREDICTABLE result.
2896
2897    (c) Within the trap shadow, no register may be used more than once as a
2898    destination register.  (This is to make life easier for the trap-handler.)
2899
2900    (d) The trap shadow may not include any branch instructions.  */
2901
2902 static void
2903 alpha_handle_trap_shadows (insns)
2904      rtx insns;
2905 {
2906   struct shadow_summary shadow;
2907   int trap_pending, exception_nesting;
2908   rtx i;
2909
2910   if (alpha_tp == ALPHA_TP_PROG && !flag_exceptions)
2911     return;
2912
2913   trap_pending = 0;
2914   exception_nesting = 0;
2915   shadow.used.i = 0;
2916   shadow.used.fp = 0;
2917   shadow.used.mem = 0;
2918   shadow.defd = shadow.used;
2919   
2920   for (i = insns; i ; i = NEXT_INSN (i))
2921     {
2922       if (GET_CODE (i) == NOTE)
2923         {
2924           switch (NOTE_LINE_NUMBER (i))
2925             {
2926             case NOTE_INSN_EH_REGION_BEG:
2927               exception_nesting++;
2928               if (trap_pending)
2929                 goto close_shadow;
2930               break;
2931
2932             case NOTE_INSN_EH_REGION_END:
2933               exception_nesting--;
2934               if (trap_pending)
2935                 goto close_shadow;
2936               break;
2937
2938             case NOTE_INSN_EPILOGUE_BEG:
2939               if (trap_pending && alpha_tp >= ALPHA_TP_FUNC)
2940                 goto close_shadow;
2941               break;
2942             }
2943         }
2944       else if (trap_pending)
2945         {
2946           if (alpha_tp == ALPHA_TP_FUNC)
2947             {
2948               if (GET_CODE (i) == JUMP_INSN
2949                   && GET_CODE (PATTERN (i)) == RETURN)
2950                 goto close_shadow;
2951             }
2952           else if (alpha_tp == ALPHA_TP_INSN)
2953             {
2954               if (optimize > 0)
2955                 {
2956                   struct shadow_summary sum;
2957
2958                   sum.used.i = 0;
2959                   sum.used.fp = 0;
2960                   sum.used.mem = 0;
2961                   sum.defd = shadow.used;
2962
2963                   switch (GET_CODE (i))
2964                     {
2965                     case INSN:
2966                       /* Annoyingly, get_attr_trap will abort on USE.  */
2967                       if (GET_CODE (PATTERN (i)) == USE)
2968                         break;
2969
2970                       summarize_insn (PATTERN (i), &sum, 0);
2971
2972                       if ((sum.defd.i & shadow.defd.i)
2973                           || (sum.defd.fp & shadow.defd.fp))
2974                         {
2975                           /* (c) would be violated */
2976                           goto close_shadow;
2977                         }
2978
2979                       /* Combine shadow with summary of current insn: */
2980                       shadow.used.i   |= sum.used.i;
2981                       shadow.used.fp  |= sum.used.fp;
2982                       shadow.used.mem |= sum.used.mem;
2983                       shadow.defd.i   |= sum.defd.i;
2984                       shadow.defd.fp  |= sum.defd.fp;
2985                       shadow.defd.mem |= sum.defd.mem;
2986
2987                       if ((sum.defd.i & shadow.used.i)
2988                           || (sum.defd.fp & shadow.used.fp)
2989                           || (sum.defd.mem & shadow.used.mem))
2990                         {
2991                           /* (a) would be violated (also takes care of (b))  */
2992                           if (get_attr_trap (i) == TRAP_YES
2993                               && ((sum.defd.i & sum.used.i)
2994                                   || (sum.defd.fp & sum.used.fp)))
2995                             abort ();
2996
2997                           goto close_shadow;
2998                         }
2999                       break;
3000
3001                     case JUMP_INSN:
3002                     case CALL_INSN:
3003                     case CODE_LABEL:
3004                       goto close_shadow;
3005
3006                     default:
3007                       abort ();
3008                     }
3009                 }
3010               else
3011                 {
3012                 close_shadow:
3013                   emit_insn_before (gen_trapb (), i);
3014                   trap_pending = 0;
3015                   shadow.used.i = 0;
3016                   shadow.used.fp = 0;
3017                   shadow.used.mem = 0;
3018                   shadow.defd = shadow.used;
3019                 }
3020             }
3021         }
3022
3023       if (exception_nesting > 0 || alpha_tp >= ALPHA_TP_FUNC)
3024         if (GET_CODE (i) == INSN
3025             && GET_CODE (PATTERN (i)) != USE
3026             && get_attr_trap (i) == TRAP_YES)
3027           {
3028             if (optimize && !trap_pending)
3029               summarize_insn (PATTERN (i), &shadow, 0);
3030             trap_pending = 1;
3031           }
3032     }
3033 }
3034
3035 /* Machine dependant reorg pass.  */
3036
3037 void
3038 alpha_reorg (insns)
3039      rtx insns;
3040 {
3041   alpha_handle_trap_shadows (insns);
3042 }
3043
3044 \f
3045 /* Check a floating-point value for validity for a particular machine mode.  */
3046
3047 static char *float_strings[] =
3048 {
3049   /* These are for FLOAT_VAX.  */
3050    "1.70141173319264430e+38", /* 2^127 (2^24 - 1) / 2^24 */
3051   "-1.70141173319264430e+38",
3052    "2.93873587705571877e-39", /* 2^-128 */
3053   "-2.93873587705571877e-39",
3054   /* These are for the default broken IEEE mode, which traps
3055      on infinity or denormal numbers.  */
3056    "3.402823466385288598117e+38", /* 2^128 (1 - 2^-24) */
3057   "-3.402823466385288598117e+38",
3058    "1.1754943508222875079687e-38", /* 2^-126 */
3059   "-1.1754943508222875079687e-38",
3060 };
3061
3062 static REAL_VALUE_TYPE float_values[8];
3063 static int inited_float_values = 0;
3064
3065 int
3066 check_float_value (mode, d, overflow)
3067      enum machine_mode mode;
3068      REAL_VALUE_TYPE *d;
3069      int overflow;
3070 {
3071
3072   if (TARGET_IEEE || TARGET_IEEE_CONFORMANT || TARGET_IEEE_WITH_INEXACT)
3073     return 0;
3074
3075   if (inited_float_values == 0)
3076     {
3077       int i;
3078       for (i = 0; i < 8; i++)
3079         float_values[i] = REAL_VALUE_ATOF (float_strings[i], DFmode);
3080
3081       inited_float_values = 1;
3082     }
3083
3084   if (mode == SFmode)
3085     {
3086       REAL_VALUE_TYPE r;
3087       REAL_VALUE_TYPE *fvptr;
3088
3089       if (TARGET_FLOAT_VAX)
3090         fvptr = &float_values[0];
3091       else
3092         fvptr = &float_values[4];
3093
3094       bcopy ((char *) d, (char *) &r, sizeof (REAL_VALUE_TYPE));
3095       if (REAL_VALUES_LESS (fvptr[0], r))
3096         {
3097           bcopy ((char *) &fvptr[0], (char *) d,
3098                  sizeof (REAL_VALUE_TYPE));
3099           return 1;
3100         }
3101       else if (REAL_VALUES_LESS (r, fvptr[1]))
3102         {
3103           bcopy ((char *) &fvptr[1], (char *) d,
3104                  sizeof (REAL_VALUE_TYPE));
3105           return 1;
3106         }
3107       else if (REAL_VALUES_LESS (dconst0, r)
3108                 && REAL_VALUES_LESS (r, fvptr[2]))
3109         {
3110           bcopy ((char *) &dconst0, (char *) d, sizeof (REAL_VALUE_TYPE));
3111           return 1;
3112         }
3113       else if (REAL_VALUES_LESS (r, dconst0)
3114                 && REAL_VALUES_LESS (fvptr[3], r))
3115         {
3116           bcopy ((char *) &dconst0, (char *) d, sizeof (REAL_VALUE_TYPE));
3117           return 1;
3118         }
3119     }
3120
3121   return 0;
3122 }
3123
3124 #if OPEN_VMS
3125
3126 void *
3127 function_arg (cum, mode, type, named)
3128     CUMULATIVE_ARGS *cum;
3129     enum machine_mode mode;
3130     tree type;
3131     int named;
3132 {
3133   int arg;
3134
3135   if (mode == VOIDmode)         /* final call, return argument information  */
3136     {
3137       return GEN_INT (*cum);
3138     }
3139
3140   arg = *cum & 0xff;
3141
3142   switch (mode)
3143     {
3144       case SFmode:
3145         *cum |= (((TARGET_FLOAT_VAX)?1:4) << ((arg * 3)+8));      /* 4 = AI$K_AR_FS, IEEE single */
3146         break;
3147       case DFmode:
3148         *cum |= (((TARGET_FLOAT_VAX)?3:5) << ((arg * 3)+8));      /* 5 = AI$K_AR_FT, IEEE double */
3149         break;
3150       case TFmode:
3151         *cum |= (7 << ((arg * 3)+8));        /* 5 = AI$K_AR_FT, IEEE double */
3152         break;
3153       default:
3154         break;
3155     }
3156
3157   return (arg < 6 && ! MUST_PASS_IN_STACK (mode, type)
3158          ? gen_rtx(REG, mode,
3159                    (*cum & 0xff) + 16 + ((TARGET_FPREGS
3160                           && (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3161                               || GET_MODE_CLASS (mode) == MODE_FLOAT))
3162                          * 32))
3163          : 0);
3164 }
3165
3166 /* Structure to collect function names for final output
3167    in link section.  */
3168
3169 enum links_kind {KIND_UNUSED, KIND_LOCAL, KIND_EXTERN};
3170
3171
3172 struct alpha_links {
3173   struct alpha_links *next;
3174   char *name;
3175   enum links_kind kind;
3176 };
3177
3178 static struct alpha_links *alpha_links_base = 0;
3179
3180 /* Make (or fake) .linkage entry for function call.
3181
3182    IS_LOCAL is 0 if name is used in call, 1 if name is used in definition.  */
3183
3184 void
3185 alpha_need_linkage (name, is_local)
3186     char *name;
3187     int is_local;
3188 {
3189   rtx x;
3190   struct alpha_links *lptr, *nptr;
3191
3192   if (name[0] == '*')
3193     name++;
3194
3195   /* Is this name already defined ?  */
3196
3197   for (lptr = alpha_links_base; lptr; lptr = lptr->next)
3198     if (strcmp (lptr->name, name) == 0)
3199       {
3200         if (is_local)
3201           {
3202             /* Defined here but external assumed.  */
3203             if (lptr->kind == KIND_EXTERN)
3204               lptr->kind = KIND_LOCAL;
3205           }
3206         else
3207           {
3208             /* Used here but unused assumed.  */
3209             if (lptr->kind == KIND_UNUSED)
3210               lptr->kind = KIND_LOCAL;
3211           }
3212         return;
3213       }
3214
3215   nptr = (struct alpha_links *) xmalloc (sizeof (struct alpha_links));
3216   nptr->next = alpha_links_base;
3217   nptr->name = xstrdup (name);
3218
3219   /* Assume external if no definition.  */
3220   nptr->kind = (is_local ? KIND_UNUSED : KIND_EXTERN);
3221
3222   /* Ensure we have an IDENTIFIER so assemble_name can mark is used.  */
3223   get_identifier (name);
3224
3225   alpha_links_base = nptr;
3226
3227   return;
3228 }
3229
3230
3231 void
3232 alpha_write_linkage (stream)
3233     FILE *stream;
3234 {
3235   struct alpha_links *lptr, *nptr;
3236
3237   readonly_section ();
3238
3239   fprintf (stream, "\t.align 3\n");
3240
3241   for (lptr = alpha_links_base; lptr; lptr = nptr)
3242     {
3243       nptr = lptr->next;
3244
3245       if (lptr->kind == KIND_UNUSED
3246           || ! TREE_SYMBOL_REFERENCED (get_identifier (lptr->name)))
3247         continue;
3248
3249       fprintf (stream, "%s..lk:\n", lptr->name);
3250       if (lptr->kind == KIND_LOCAL)   
3251         {
3252           /*  Local and used, build linkage pair.  */
3253           fprintf (stream, "\t.quad %s..en\n", lptr->name);
3254           fprintf (stream, "\t.quad %s\n", lptr->name);
3255         }
3256       else
3257         /* External and used, request linkage pair.  */
3258         fprintf (stream, "\t.linkage %s\n", lptr->name);
3259     }
3260 }
3261
3262 #else
3263
3264 void
3265 alpha_need_linkage (name, is_local)
3266      char *name;
3267      int is_local;
3268 {
3269 }
3270
3271 #endif /* OPEN_VMS */
3272