OSDN Git Service

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