OSDN Git Service

* config/rs6000/rs6000.c (rs6000_legitimize_address): Force both
[pf3gnuchains/gcc-fork.git] / gcc / config / rs6000 / rs6000.c
1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
3    2000, 2001 Free Software Foundation, Inc.
4    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.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-attr.h"
32 #include "flags.h"
33 #include "recog.h"
34 #include "obstack.h"
35 #include "tree.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "except.h"
39 #include "function.h"
40 #include "output.h"
41 #include "basic-block.h"
42 #include "integrate.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "langhooks.h"
50
51 #ifndef TARGET_NO_PROTOTYPE
52 #define TARGET_NO_PROTOTYPE 0
53 #endif
54
55 #define min(A,B)        ((A) < (B) ? (A) : (B))
56 #define max(A,B)        ((A) > (B) ? (A) : (B))
57
58 /* Target cpu type */
59
60 enum processor_type rs6000_cpu;
61 struct rs6000_cpu_select rs6000_select[3] =
62 {
63   /* switch             name,                   tune    arch */
64   { (const char *)0,    "--with-cpu=",          1,      1 },
65   { (const char *)0,    "-mcpu=",               1,      1 },
66   { (const char *)0,    "-mtune=",              1,      0 },
67 };
68
69 /* Size of long double */
70 const char *rs6000_long_double_size_string;
71 int rs6000_long_double_type_size;
72
73 /* Whether -mabi=altivec has appeared */
74 int rs6000_altivec_abi;
75
76 /* Set to non-zero once AIX common-mode calls have been defined.  */
77 static int common_mode_defined;
78
79 /* Save information from a "cmpxx" operation until the branch or scc is
80    emitted.  */
81 rtx rs6000_compare_op0, rs6000_compare_op1;
82 int rs6000_compare_fp_p;
83
84 /* Label number of label created for -mrelocatable, to call to so we can
85    get the address of the GOT section */
86 int rs6000_pic_labelno;
87
88 #ifdef USING_ELFOS_H
89 /* Which abi to adhere to */
90 const char *rs6000_abi_name = RS6000_ABI_NAME;
91
92 /* Semantics of the small data area */
93 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
94
95 /* Which small data model to use */
96 const char *rs6000_sdata_name = (char *)0;
97
98 /* Counter for labels which are to be placed in .fixup.  */
99 int fixuplabelno = 0;
100 #endif
101
102 /* ABI enumeration available for subtarget to use.  */
103 enum rs6000_abi rs6000_current_abi;
104
105 /* ABI string from -mabi= option.  */
106 const char *rs6000_abi_string;
107
108 /* Debug flags */
109 const char *rs6000_debug_name;
110 int rs6000_debug_stack;         /* debug stack applications */
111 int rs6000_debug_arg;           /* debug argument handling */
112
113 /* Flag to say the TOC is initialized */
114 int toc_initialized;
115 char toc_label_name[10];
116
117 /* Alias set for saves and restores from the rs6000 stack.  */
118 static int rs6000_sr_alias_set;
119
120 static void rs6000_add_gc_roots PARAMS ((void));
121 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
122 static rtx expand_block_move_mem PARAMS ((enum machine_mode, rtx, rtx));
123 static void validate_condition_mode 
124   PARAMS ((enum rtx_code, enum machine_mode));
125 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
126 static void rs6000_maybe_dead PARAMS ((rtx));
127 static void rs6000_emit_stack_tie PARAMS ((void));
128 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
129 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
130 static unsigned rs6000_hash_constant PARAMS ((rtx));
131 static unsigned toc_hash_function PARAMS ((const void *));
132 static int toc_hash_eq PARAMS ((const void *, const void *));
133 static int toc_hash_mark_entry PARAMS ((void **, void *));
134 static void toc_hash_mark_table PARAMS ((void *));
135 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
136 static void rs6000_free_machine_status PARAMS ((struct function *));
137 static void rs6000_init_machine_status PARAMS ((struct function *));
138 static int rs6000_ra_ever_killed PARAMS ((void));
139 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
140 const struct attribute_spec rs6000_attribute_table[];
141 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
142 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
143 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
144   HOST_WIDE_INT, HOST_WIDE_INT));
145 #if TARGET_ELF
146 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
147                                                            int));
148 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
149 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
150 #endif
151 #ifdef OBJECT_FORMAT_COFF
152 static void xcoff_asm_named_section PARAMS ((const char *, unsigned int));
153 #endif
154 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
155 static int rs6000_adjust_priority PARAMS ((rtx, int));
156 static int rs6000_issue_rate PARAMS ((void));
157
158 static void rs6000_init_builtins PARAMS ((void));
159 static void altivec_init_builtins PARAMS ((void));
160 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
161 static rtx altivec_expand_builtin PARAMS ((tree, rtx));
162 static rtx altivec_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
163 static rtx altivec_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
164 static rtx altivec_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
165 static void rs6000_parse_abi_options PARAMS ((void));
166 static int first_altivec_reg_to_save PARAMS ((void));
167 static unsigned int compute_vrsave_mask PARAMS ((void));
168 static void is_altivec_return_reg PARAMS ((rtx, void *));
169 int vrsave_operation PARAMS ((rtx, enum machine_mode));
170 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *));
171 \f
172 /* Default register names.  */
173 char rs6000_reg_names[][8] =
174 {
175       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
176       "8",  "9", "10", "11", "12", "13", "14", "15",
177      "16", "17", "18", "19", "20", "21", "22", "23",
178      "24", "25", "26", "27", "28", "29", "30", "31",
179       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
180       "8",  "9", "10", "11", "12", "13", "14", "15",
181      "16", "17", "18", "19", "20", "21", "22", "23",
182      "24", "25", "26", "27", "28", "29", "30", "31",
183      "mq", "lr", "ctr","ap",
184       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
185       "xer",
186       /* AltiVec registers.  */
187       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
188       "8",  "9",  "10", "11", "12", "13", "14", "15",
189       "16", "17", "18", "19", "20", "21", "22", "23",
190       "24", "25", "26", "27", "28", "29", "30", "31",
191       "vrsave"
192 };
193
194 #ifdef TARGET_REGNAMES
195 static const char alt_reg_names[][8] =
196 {
197    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
198    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
199   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
200   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
201    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
202    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
203   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
204   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
205     "mq",    "lr",  "ctr",   "ap",
206   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
207    "xer",
208    /* AltiVec registers.  */
209    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
210    "%v8",  "%v9",  "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
211    "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
212    "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
213    "%vrsave"
214 };
215 #endif
216 \f
217 #ifndef MASK_STRICT_ALIGN
218 #define MASK_STRICT_ALIGN 0
219 #endif
220 \f
221 /* Initialize the GCC target structure.  */
222 #undef TARGET_ATTRIBUTE_TABLE
223 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
224
225 #undef TARGET_ASM_FUNCTION_PROLOGUE
226 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
227 #undef TARGET_ASM_FUNCTION_EPILOGUE
228 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
229
230 #if TARGET_ELF
231 #undef TARGET_SECTION_TYPE_FLAGS
232 #define TARGET_SECTION_TYPE_FLAGS  rs6000_elf_section_type_flags
233 #endif
234
235 #undef TARGET_SCHED_ISSUE_RATE
236 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
237 #undef TARGET_SCHED_ADJUST_COST
238 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
239 #undef TARGET_SCHED_ADJUST_PRIORITY
240 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
241
242 #undef TARGET_INIT_BUILTINS
243 #define TARGET_INIT_BUILTINS rs6000_init_builtins
244
245 #undef TARGET_EXPAND_BUILTIN
246 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
247
248 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
249 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
250
251 struct gcc_target targetm = TARGET_INITIALIZER;
252 \f
253 /* Override command line options.  Mostly we process the processor
254    type and sometimes adjust other TARGET_ options.  */
255
256 void
257 rs6000_override_options (default_cpu)
258      const char *default_cpu;
259 {
260   size_t i, j;
261   struct rs6000_cpu_select *ptr;
262
263   /* Simplify the entries below by making a mask for any POWER
264      variant and any PowerPC variant.  */
265
266 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
267 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
268                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
269 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
270
271   static struct ptt
272     {
273       const char *const name;           /* Canonical processor name.  */
274       const enum processor_type processor; /* Processor type enum value.  */
275       const int target_enable;  /* Target flags to enable.  */
276       const int target_disable; /* Target flags to disable.  */
277     } const processor_target_table[]
278       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
279             POWER_MASKS | POWERPC_MASKS},
280          {"power", PROCESSOR_POWER,
281             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
282             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
283          {"power2", PROCESSOR_POWER,
284             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
285             POWERPC_MASKS | MASK_NEW_MNEMONICS},
286          {"power3", PROCESSOR_PPC630,
287             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
288             POWER_MASKS | MASK_PPC_GPOPT},
289          {"powerpc", PROCESSOR_POWERPC,
290             MASK_POWERPC | MASK_NEW_MNEMONICS,
291             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
292          {"powerpc64", PROCESSOR_POWERPC64,
293             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
294             POWER_MASKS | POWERPC_OPT_MASKS},
295          {"rios", PROCESSOR_RIOS1,
296             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
297             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
298          {"rios1", PROCESSOR_RIOS1,
299             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
300             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
301          {"rsc", PROCESSOR_PPC601,
302             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
303             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
304          {"rsc1", PROCESSOR_PPC601,
305             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
306             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
307          {"rios2", PROCESSOR_RIOS2,
308             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
309             POWERPC_MASKS | MASK_NEW_MNEMONICS},
310          {"rs64a", PROCESSOR_RS64A,
311             MASK_POWERPC | MASK_NEW_MNEMONICS,
312             POWER_MASKS | POWERPC_OPT_MASKS},
313          {"401", PROCESSOR_PPC403,
314             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
315             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
316          {"403", PROCESSOR_PPC403,
317             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
318             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
319          {"405", PROCESSOR_PPC405,
320             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
321             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
322          {"505", PROCESSOR_MPCCORE,
323             MASK_POWERPC | MASK_NEW_MNEMONICS,
324             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
325          {"601", PROCESSOR_PPC601,
326             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
327             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
328          {"602", PROCESSOR_PPC603,
329             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
330             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
331          {"603", PROCESSOR_PPC603,
332             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
333             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
334          {"603e", PROCESSOR_PPC603,
335             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
336             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
337          {"ec603e", PROCESSOR_PPC603,
338             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
339             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
340          {"604", PROCESSOR_PPC604,
341             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
342             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
343          {"604e", PROCESSOR_PPC604e,
344             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
345             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
346          {"620", PROCESSOR_PPC620,
347             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
348             POWER_MASKS | MASK_PPC_GPOPT},
349          {"630", PROCESSOR_PPC630,
350             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
351             POWER_MASKS | MASK_PPC_GPOPT},
352          {"740", PROCESSOR_PPC750,
353             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
354             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
355          {"750", PROCESSOR_PPC750,
356             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
357             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
358          {"7400", PROCESSOR_PPC7400,
359             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
360             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
361          {"7450", PROCESSOR_PPC7450,
362             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
363             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
364          {"801", PROCESSOR_MPCCORE,
365             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
366             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
367          {"821", PROCESSOR_MPCCORE,
368             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
369             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
370          {"823", PROCESSOR_MPCCORE,
371             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
372             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
373          {"860", PROCESSOR_MPCCORE,
374             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
375             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64}};
376
377   size_t ptt_size = sizeof (processor_target_table) / sizeof (struct ptt);
378
379   /* Save current -mmultiple/-mno-multiple status.  */
380   int multiple = TARGET_MULTIPLE;
381   /* Save current -mstring/-mno-string status.  */
382   int string = TARGET_STRING;
383
384   /* Identify the processor type.  */
385   rs6000_select[0].string = default_cpu;
386   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
387
388   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
389     {
390       ptr = &rs6000_select[i];
391       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
392         {
393           for (j = 0; j < ptt_size; j++)
394             if (! strcmp (ptr->string, processor_target_table[j].name))
395               {
396                 if (ptr->set_tune_p)
397                   rs6000_cpu = processor_target_table[j].processor;
398
399                 if (ptr->set_arch_p)
400                   {
401                     target_flags |= processor_target_table[j].target_enable;
402                     target_flags &= ~processor_target_table[j].target_disable;
403                   }
404                 break;
405               }
406
407           if (j == ptt_size)
408             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
409         }
410     }
411
412   /* If we are optimizing big endian systems for space, use the store
413      multiple instructions.  */
414   if (BYTES_BIG_ENDIAN && optimize_size)
415     target_flags |= MASK_MULTIPLE;
416
417   /* If -mmultiple or -mno-multiple was explicitly used, don't
418      override with the processor default */
419   if (TARGET_MULTIPLE_SET)
420     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
421
422   /* If -mstring or -mno-string was explicitly used, don't override
423      with the processor default.  */
424   if (TARGET_STRING_SET)
425     target_flags = (target_flags & ~MASK_STRING) | string;
426
427   /* Don't allow -mmultiple or -mstring on little endian systems
428      unless the cpu is a 750, because the hardware doesn't support the
429      instructions used in little endian mode, and causes an alignment
430      trap.  The 750 does not cause an alignment trap (except when the
431      target is unaligned).  */
432
433   if (! BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
434     {
435       if (TARGET_MULTIPLE)
436         {
437           target_flags &= ~MASK_MULTIPLE;
438           if (TARGET_MULTIPLE_SET)
439             warning ("-mmultiple is not supported on little endian systems");
440         }
441
442       if (TARGET_STRING)
443         {
444           target_flags &= ~MASK_STRING;
445           if (TARGET_STRING_SET)
446             warning ("-mstring is not supported on little endian systems");
447         }
448     }
449
450   if (flag_pic && DEFAULT_ABI == ABI_AIX)
451     {
452       warning ("-f%s ignored (all code is position independent)",
453                (flag_pic > 1) ? "PIC" : "pic");
454       flag_pic = 0;
455     }
456
457 #ifdef XCOFF_DEBUGGING_INFO
458   if (flag_function_sections && (write_symbols != NO_DEBUG)
459       && DEFAULT_ABI == ABI_AIX)
460     {
461       warning ("-ffunction-sections disabled on AIX when debugging");
462       flag_function_sections = 0;
463     }
464
465   if (flag_data_sections && (DEFAULT_ABI == ABI_AIX))
466     {
467       warning ("-fdata-sections not supported on AIX");
468       flag_data_sections = 0;
469     }
470 #endif
471
472   /* Set debug flags */
473   if (rs6000_debug_name)
474     {
475       if (! strcmp (rs6000_debug_name, "all"))
476         rs6000_debug_stack = rs6000_debug_arg = 1;
477       else if (! strcmp (rs6000_debug_name, "stack"))
478         rs6000_debug_stack = 1;
479       else if (! strcmp (rs6000_debug_name, "arg"))
480         rs6000_debug_arg = 1;
481       else
482         error ("unknown -mdebug-%s switch", rs6000_debug_name);
483     }
484
485   /* Set size of long double */
486   rs6000_long_double_type_size = 64;
487   if (rs6000_long_double_size_string)
488     {
489       char *tail;
490       int size = strtol (rs6000_long_double_size_string, &tail, 10);
491       if (*tail != '\0' || (size != 64 && size != 128))
492         error ("Unknown switch -mlong-double-%s",
493                rs6000_long_double_size_string);
494       else
495         rs6000_long_double_type_size = size;
496     }
497
498   /* Handle -mabi= options.  */
499   rs6000_parse_abi_options ();
500
501 #ifdef TARGET_REGNAMES
502   /* If the user desires alternate register names, copy in the
503      alternate names now.  */
504   if (TARGET_REGNAMES)
505     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
506 #endif
507
508 #ifdef SUBTARGET_OVERRIDE_OPTIONS
509   SUBTARGET_OVERRIDE_OPTIONS;
510 #endif
511 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
512   SUBSUBTARGET_OVERRIDE_OPTIONS;
513 #endif
514
515   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
516      If -maix-struct-return or -msvr4-struct-return was explicitly
517      used, don't override with the ABI default.  */
518   if (!(target_flags & MASK_AIX_STRUCT_RET_SET))
519     {
520       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
521         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
522       else
523         target_flags |= MASK_AIX_STRUCT_RET;
524     }
525
526   /* Register global variables with the garbage collector.  */
527   rs6000_add_gc_roots ();
528
529   /* Allocate an alias set for register saves & restores from stack.  */
530   rs6000_sr_alias_set = new_alias_set ();
531
532   if (TARGET_TOC) 
533     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
534
535   /* Arrange to save and restore machine status around nested functions.  */
536   init_machine_status = rs6000_init_machine_status;
537   free_machine_status = rs6000_free_machine_status;
538 }
539
540 /* Handle -mabi= options.  */
541 static void
542 rs6000_parse_abi_options ()
543 {
544   if (rs6000_abi_string == 0)
545     return;
546   else if (! strcmp (rs6000_abi_string, "altivec"))
547     rs6000_altivec_abi = 1;
548   else
549     error ("unknown ABI specified: '%s'", rs6000_abi_string);
550 }
551
552 void
553 optimization_options (level, size)
554      int level ATTRIBUTE_UNUSED;
555      int size ATTRIBUTE_UNUSED;
556 {
557 }
558 \f
559 /* Do anything needed at the start of the asm file.  */
560
561 void
562 rs6000_file_start (file, default_cpu)
563      FILE *file;
564      const char *default_cpu;
565 {
566   size_t i;
567   char buffer[80];
568   const char *start = buffer;
569   struct rs6000_cpu_select *ptr;
570
571   if (flag_verbose_asm)
572     {
573       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
574       rs6000_select[0].string = default_cpu;
575
576       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
577         {
578           ptr = &rs6000_select[i];
579           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
580             {
581               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
582               start = "";
583             }
584         }
585
586 #ifdef USING_ELFOS_H
587       switch (rs6000_sdata)
588         {
589         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
590         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
591         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
592         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
593         }
594
595       if (rs6000_sdata && g_switch_value)
596         {
597           fprintf (file, "%s -G %d", start, g_switch_value);
598           start = "";
599         }
600 #endif
601
602       if (*start == '\0')
603         putc ('\n', file);
604     }
605 }
606
607 \f
608 /* Create a CONST_DOUBLE from a string.  */
609
610 struct rtx_def *
611 rs6000_float_const (string, mode)
612      const char *string;
613      enum machine_mode mode;
614 {
615   REAL_VALUE_TYPE value;
616   value = REAL_VALUE_ATOF (string, mode);
617   return immed_real_const_1 (value, mode);
618 }
619 \f
620 /* Return non-zero if this function is known to have a null epilogue.  */
621
622 int
623 direct_return ()
624 {
625   if (reload_completed)
626     {
627       rs6000_stack_t *info = rs6000_stack_info ();
628
629       if (info->first_gp_reg_save == 32
630           && info->first_fp_reg_save == 64
631           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
632           && ! info->lr_save_p
633           && ! info->cr_save_p
634           && info->vrsave_mask == 0
635           && ! info->push_p)
636         return 1;
637     }
638
639   return 0;
640 }
641
642 /* Returns 1 always.  */
643
644 int
645 any_operand (op, mode)
646      rtx op ATTRIBUTE_UNUSED;
647      enum machine_mode mode ATTRIBUTE_UNUSED;
648 {
649   return 1;
650 }
651
652 /* Returns 1 if op is the count register.  */
653 int
654 count_register_operand (op, mode)
655      rtx op;
656      enum machine_mode mode ATTRIBUTE_UNUSED;
657 {
658   if (GET_CODE (op) != REG)
659     return 0;
660
661   if (REGNO (op) == COUNT_REGISTER_REGNUM)
662     return 1;
663
664   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
665     return 1;
666
667   return 0;
668 }
669
670 int
671 xer_operand (op, mode)
672      rtx op;
673      enum machine_mode mode ATTRIBUTE_UNUSED;
674 {
675   if (GET_CODE (op) != REG)
676     return 0;
677
678   if (XER_REGNO_P (REGNO (op)))
679     return 1;
680
681   return 0;
682 }
683
684 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
685    by such constants completes more quickly. */
686
687 int
688 s8bit_cint_operand (op, mode)
689      rtx op;
690      enum machine_mode mode ATTRIBUTE_UNUSED;
691 {
692   return ( GET_CODE (op) == CONST_INT
693           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
694 }
695
696 /* Return 1 if OP is a constant that can fit in a D field.  */
697
698 int
699 short_cint_operand (op, mode)
700      rtx op;
701      enum machine_mode mode ATTRIBUTE_UNUSED;
702 {
703   return (GET_CODE (op) == CONST_INT
704           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
705 }
706
707 /* Similar for an unsigned D field.  */
708
709 int
710 u_short_cint_operand (op, mode)
711      rtx op;
712      enum machine_mode mode ATTRIBUTE_UNUSED;
713 {
714   return (GET_CODE (op) == CONST_INT
715           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'K'));
716 }
717
718 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
719
720 int
721 non_short_cint_operand (op, mode)
722      rtx op;
723      enum machine_mode mode ATTRIBUTE_UNUSED;
724 {
725   return (GET_CODE (op) == CONST_INT
726           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
727 }
728
729 /* Returns 1 if OP is a CONST_INT that is a positive value
730    and an exact power of 2.  */
731
732 int
733 exact_log2_cint_operand (op, mode)
734      rtx op;
735      enum machine_mode mode ATTRIBUTE_UNUSED;
736 {
737   return (GET_CODE (op) == CONST_INT
738           && INTVAL (op) > 0
739           && exact_log2 (INTVAL (op)) >= 0);
740 }
741
742 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
743    ctr, or lr).  */
744
745 int
746 gpc_reg_operand (op, mode)
747      rtx op;
748      enum machine_mode mode;
749 {
750   return (register_operand (op, mode)
751           && (GET_CODE (op) != REG
752               || (REGNO (op) >= ARG_POINTER_REGNUM 
753                   && !XER_REGNO_P (REGNO (op)))
754               || REGNO (op) < MQ_REGNO));
755 }
756
757 /* Returns 1 if OP is either a pseudo-register or a register denoting a
758    CR field.  */
759
760 int
761 cc_reg_operand (op, mode)
762      rtx op;
763      enum machine_mode mode;
764 {
765   return (register_operand (op, mode)
766           && (GET_CODE (op) != REG
767               || REGNO (op) >= FIRST_PSEUDO_REGISTER
768               || CR_REGNO_P (REGNO (op))));
769 }
770
771 /* Returns 1 if OP is either a pseudo-register or a register denoting a
772    CR field that isn't CR0.  */
773
774 int
775 cc_reg_not_cr0_operand (op, mode)
776      rtx op;
777      enum machine_mode mode;
778 {
779   return (register_operand (op, mode)
780           && (GET_CODE (op) != REG
781               || REGNO (op) >= FIRST_PSEUDO_REGISTER
782               || CR_REGNO_NOT_CR0_P (REGNO (op))));
783 }
784
785 /* Returns 1 if OP is either a constant integer valid for a D-field or
786    a non-special register.  If a register, it must be in the proper
787    mode unless MODE is VOIDmode.  */
788
789 int
790 reg_or_short_operand (op, mode)
791       rtx op;
792       enum machine_mode mode;
793 {
794   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
795 }
796
797 /* Similar, except check if the negation of the constant would be
798    valid for a D-field.  */
799
800 int
801 reg_or_neg_short_operand (op, mode)
802       rtx op;
803       enum machine_mode mode;
804 {
805   if (GET_CODE (op) == CONST_INT)
806     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
807
808   return gpc_reg_operand (op, mode);
809 }
810
811 /* Return 1 if the operand is either a register or an integer whose
812    high-order 16 bits are zero.  */
813
814 int
815 reg_or_u_short_operand (op, mode)
816      rtx op;
817      enum machine_mode mode;
818 {
819   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
820 }
821
822 /* Return 1 is the operand is either a non-special register or ANY
823    constant integer.  */
824
825 int
826 reg_or_cint_operand (op, mode)
827     rtx op;
828     enum machine_mode mode;
829 {
830   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
831 }
832
833 /* Return 1 is the operand is either a non-special register or ANY
834    32-bit signed constant integer.  */
835
836 int
837 reg_or_arith_cint_operand (op, mode)
838     rtx op;
839     enum machine_mode mode;
840 {
841   return (gpc_reg_operand (op, mode)
842           || (GET_CODE (op) == CONST_INT
843 #if HOST_BITS_PER_WIDE_INT != 32
844               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
845                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
846 #endif
847               ));
848 }
849
850 /* Return 1 is the operand is either a non-special register or a 32-bit
851    signed constant integer valid for 64-bit addition.  */
852
853 int
854 reg_or_add_cint64_operand (op, mode)
855     rtx op;
856     enum machine_mode mode;
857 {
858   return (gpc_reg_operand (op, mode)
859           || (GET_CODE (op) == CONST_INT
860               && INTVAL (op) < 0x7fff8000
861 #if HOST_BITS_PER_WIDE_INT != 32
862               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
863                   < 0x100000000ll)
864 #endif
865               ));
866 }
867
868 /* Return 1 is the operand is either a non-special register or a 32-bit
869    signed constant integer valid for 64-bit subtraction.  */
870
871 int
872 reg_or_sub_cint64_operand (op, mode)
873     rtx op;
874     enum machine_mode mode;
875 {
876   return (gpc_reg_operand (op, mode)
877           || (GET_CODE (op) == CONST_INT
878               && (- INTVAL (op)) < 0x7fff8000
879 #if HOST_BITS_PER_WIDE_INT != 32
880               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
881                   < 0x100000000ll)
882 #endif
883               ));
884 }
885
886 /* Return 1 is the operand is either a non-special register or ANY
887    32-bit unsigned constant integer.  */
888
889 int
890 reg_or_logical_cint_operand (op, mode)
891     rtx op;
892     enum machine_mode mode;
893 {
894   if (GET_CODE (op) == CONST_INT)
895     {
896       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
897         {
898           if (GET_MODE_BITSIZE (mode) <= 32)
899             abort ();
900
901           if (INTVAL (op) < 0)
902             return 0;
903         }
904
905       return ((INTVAL (op) & GET_MODE_MASK (mode)
906                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
907     }
908   else if (GET_CODE (op) == CONST_DOUBLE)
909     {
910       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
911           || mode != DImode)
912         abort ();
913
914       return CONST_DOUBLE_HIGH (op) == 0;
915     }
916   else 
917     return gpc_reg_operand (op, mode);
918 }
919
920 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
921
922 int
923 got_operand (op, mode)
924      rtx op;
925      enum machine_mode mode ATTRIBUTE_UNUSED;
926 {
927   return (GET_CODE (op) == SYMBOL_REF
928           || GET_CODE (op) == CONST
929           || GET_CODE (op) == LABEL_REF);
930 }
931
932 /* Return 1 if the operand is a simple references that can be loaded via
933    the GOT (labels involving addition aren't allowed).  */
934
935 int
936 got_no_const_operand (op, mode)
937      rtx op;
938      enum machine_mode mode ATTRIBUTE_UNUSED;
939 {
940   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
941 }
942
943 /* Return the number of instructions it takes to form a constant in an
944    integer register.  */
945
946 static int
947 num_insns_constant_wide (value)
948      HOST_WIDE_INT value;
949 {
950   /* signed constant loadable with {cal|addi} */
951   if (CONST_OK_FOR_LETTER_P (value, 'I'))
952     return 1;
953
954   /* constant loadable with {cau|addis} */
955   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
956     return 1;
957
958 #if HOST_BITS_PER_WIDE_INT == 64
959   else if (TARGET_POWERPC64)
960     {
961       HOST_WIDE_INT low  = value & 0xffffffff;
962       HOST_WIDE_INT high = value >> 32;
963
964       low = (low ^ 0x80000000) - 0x80000000;  /* sign extend */
965
966       if (high == 0 && (low & 0x80000000) == 0)
967         return 2;
968
969       else if (high == -1 && (low & 0x80000000) != 0)
970         return 2;
971
972       else if (! low)
973         return num_insns_constant_wide (high) + 1;
974
975       else
976         return (num_insns_constant_wide (high)
977                 + num_insns_constant_wide (low) + 1);
978     }
979 #endif
980
981   else
982     return 2;
983 }
984
985 int
986 num_insns_constant (op, mode)
987      rtx op;
988      enum machine_mode mode;
989 {
990   if (GET_CODE (op) == CONST_INT)
991     {
992 #if HOST_BITS_PER_WIDE_INT == 64
993       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
994           && mask64_operand (op, mode))
995             return 2;
996       else
997 #endif
998         return num_insns_constant_wide (INTVAL (op));
999     }
1000
1001   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1002     {
1003       long l;
1004       REAL_VALUE_TYPE rv;
1005
1006       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1007       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1008       return num_insns_constant_wide ((HOST_WIDE_INT)l);
1009     }
1010
1011   else if (GET_CODE (op) == CONST_DOUBLE)
1012     {
1013       HOST_WIDE_INT low;
1014       HOST_WIDE_INT high;
1015       long l[2];
1016       REAL_VALUE_TYPE rv;
1017       int endian = (WORDS_BIG_ENDIAN == 0);
1018
1019       if (mode == VOIDmode || mode == DImode)
1020         {
1021           high = CONST_DOUBLE_HIGH (op);
1022           low  = CONST_DOUBLE_LOW (op);
1023         }
1024       else
1025         {
1026           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1027           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1028           high = l[endian];
1029           low  = l[1 - endian];
1030         }
1031
1032       if (TARGET_32BIT)
1033         return (num_insns_constant_wide (low)
1034                 + num_insns_constant_wide (high));
1035
1036       else
1037         {
1038           if (high == 0 && (low & 0x80000000) == 0)
1039             return num_insns_constant_wide (low);
1040
1041           else if (high == -1 && (low & 0x80000000) != 0)
1042             return num_insns_constant_wide (low);
1043
1044           else if (mask64_operand (op, mode))
1045             return 2;
1046
1047           else if (low == 0)
1048             return num_insns_constant_wide (high) + 1;
1049
1050           else
1051             return (num_insns_constant_wide (high)
1052                     + num_insns_constant_wide (low) + 1);
1053         }
1054     }
1055
1056   else
1057     abort ();
1058 }
1059
1060 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1061    register with one instruction per word.  We only do this if we can
1062    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1063
1064 int
1065 easy_fp_constant (op, mode)
1066      rtx op;
1067      enum machine_mode mode;
1068 {
1069   if (GET_CODE (op) != CONST_DOUBLE
1070       || GET_MODE (op) != mode
1071       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1072     return 0;
1073
1074   /* Consider all constants with -msoft-float to be easy.  */
1075   if (TARGET_SOFT_FLOAT && mode != DImode)
1076     return 1;
1077
1078   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1079   if (flag_pic && DEFAULT_ABI == ABI_V4)
1080     return 0;
1081
1082 #ifdef TARGET_RELOCATABLE
1083   /* Similarly if we are using -mrelocatable, consider all constants
1084      to be hard.  */
1085   if (TARGET_RELOCATABLE)
1086     return 0;
1087 #endif
1088
1089   if (mode == DFmode)
1090     {
1091       long k[2];
1092       REAL_VALUE_TYPE rv;
1093
1094       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1095       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1096
1097       return (num_insns_constant_wide ((HOST_WIDE_INT)k[0]) == 1
1098               && num_insns_constant_wide ((HOST_WIDE_INT)k[1]) == 1);
1099     }
1100
1101   else if (mode == SFmode)
1102     {
1103       long l;
1104       REAL_VALUE_TYPE rv;
1105
1106       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1107       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1108
1109       return num_insns_constant_wide (l) == 1;
1110     }
1111
1112   else if (mode == DImode)
1113     return ((TARGET_POWERPC64
1114              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1115             || (num_insns_constant (op, DImode) <= 2));
1116
1117   else if (mode == SImode)
1118     return 1;
1119   else
1120     abort ();
1121 }
1122
1123 /* Return 1 if the operand is 0.0.  */
1124 int
1125 zero_fp_constant (op, mode)
1126      rtx op;
1127      enum machine_mode mode;
1128 {
1129   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1130 }
1131
1132 /* Return 1 if the operand is in volatile memory.  Note that during
1133    the RTL generation phase, memory_operand does not return TRUE for
1134    volatile memory references.  So this function allows us to
1135    recognize volatile references where its safe.  */
1136
1137 int
1138 volatile_mem_operand (op, mode)
1139      rtx op;
1140      enum machine_mode mode;
1141 {
1142   if (GET_CODE (op) != MEM)
1143     return 0;
1144
1145   if (!MEM_VOLATILE_P (op))
1146     return 0;
1147
1148   if (mode != GET_MODE (op))
1149     return 0;
1150
1151   if (reload_completed)
1152     return memory_operand (op, mode);
1153
1154   if (reload_in_progress)
1155     return strict_memory_address_p (mode, XEXP (op, 0));
1156
1157   return memory_address_p (mode, XEXP (op, 0));
1158 }
1159
1160 /* Return 1 if the operand is an offsettable memory operand.  */
1161
1162 int
1163 offsettable_mem_operand (op, mode)
1164      rtx op;
1165      enum machine_mode mode;
1166 {
1167   return ((GET_CODE (op) == MEM)
1168           && offsettable_address_p (reload_completed || reload_in_progress,
1169                                     mode, XEXP (op, 0)));
1170 }
1171
1172 /* Return 1 if the operand is either an easy FP constant (see above) or
1173    memory.  */
1174
1175 int
1176 mem_or_easy_const_operand (op, mode)
1177      rtx op;
1178      enum machine_mode mode;
1179 {
1180   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1181 }
1182
1183 /* Return 1 if the operand is either a non-special register or an item
1184    that can be used as the operand of a `mode' add insn.  */
1185
1186 int
1187 add_operand (op, mode)
1188     rtx op;
1189     enum machine_mode mode;
1190 {
1191   if (GET_CODE (op) == CONST_INT)
1192     return (CONST_OK_FOR_LETTER_P (INTVAL(op), 'I')
1193             || CONST_OK_FOR_LETTER_P (INTVAL(op), 'L'));
1194
1195   return gpc_reg_operand (op, mode);
1196 }
1197
1198 /* Return 1 if OP is a constant but not a valid add_operand.  */
1199
1200 int
1201 non_add_cint_operand (op, mode)
1202      rtx op;
1203      enum machine_mode mode ATTRIBUTE_UNUSED;
1204 {
1205   return (GET_CODE (op) == CONST_INT
1206           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000
1207           && ! CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1208 }
1209
1210 /* Return 1 if the operand is a non-special register or a constant that
1211    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1212
1213 int
1214 logical_operand (op, mode)
1215      rtx op;
1216      enum machine_mode mode;
1217 {
1218   HOST_WIDE_INT opl, oph;
1219
1220   if (gpc_reg_operand (op, mode))
1221     return 1;
1222
1223   if (GET_CODE (op) == CONST_INT)
1224     {
1225       opl = INTVAL (op) & GET_MODE_MASK (mode);
1226
1227 #if HOST_BITS_PER_WIDE_INT <= 32
1228       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1229         return 0;
1230 #endif
1231     }
1232   else if (GET_CODE (op) == CONST_DOUBLE)
1233     {
1234       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1235         abort ();
1236
1237       opl = CONST_DOUBLE_LOW (op);
1238       oph = CONST_DOUBLE_HIGH (op);
1239       if (oph != 0)
1240         return 0;
1241     }
1242   else
1243     return 0;
1244
1245   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1246           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1247 }
1248
1249 /* Return 1 if C is a constant that is not a logical operand (as
1250    above), but could be split into one.  */
1251
1252 int
1253 non_logical_cint_operand (op, mode)
1254      rtx op;
1255      enum machine_mode mode;
1256 {
1257   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1258           && ! logical_operand (op, mode)
1259           && reg_or_logical_cint_operand (op, mode));
1260 }
1261
1262 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1263    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1264    Reject all ones and all zeros, since these should have been optimized
1265    away and confuse the making of MB and ME.  */
1266
1267 int
1268 mask_operand (op, mode)
1269      rtx op;
1270      enum machine_mode mode ATTRIBUTE_UNUSED;
1271 {
1272   HOST_WIDE_INT c, lsb;
1273
1274   if (GET_CODE (op) != CONST_INT)
1275     return 0;
1276
1277   c = INTVAL (op);
1278
1279   /* We don't change the number of transitions by inverting,
1280      so make sure we start with the LS bit zero.  */
1281   if (c & 1)
1282     c = ~c;
1283
1284   /* Reject all zeros or all ones.  */
1285   if (c == 0)
1286     return 0;
1287
1288   /* Find the first transition.  */
1289   lsb = c & -c;
1290
1291   /* Invert to look for a second transition.  */
1292   c = ~c;
1293
1294   /* Erase first transition.  */
1295   c &= -lsb;
1296
1297   /* Find the second transition (if any).  */
1298   lsb = c & -c;
1299
1300   /* Match if all the bits above are 1's (or c is zero).  */
1301   return c == -lsb;
1302 }
1303
1304 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1305    It is if there are no more than one 1->0 or 0->1 transitions.
1306    Reject all ones and all zeros, since these should have been optimized
1307    away and confuse the making of MB and ME.  */
1308
1309 int
1310 mask64_operand (op, mode)
1311      rtx op;
1312      enum machine_mode mode;
1313 {
1314   if (GET_CODE (op) == CONST_INT)
1315     {
1316       HOST_WIDE_INT c, lsb;
1317
1318       /* We don't change the number of transitions by inverting,
1319          so make sure we start with the LS bit zero.  */
1320       c = INTVAL (op);
1321       if (c & 1)
1322         c = ~c;
1323
1324       /* Reject all zeros or all ones.  */
1325       if (c == 0)
1326         return 0;
1327
1328       /* Find the transition, and check that all bits above are 1's.  */
1329       lsb = c & -c;
1330       return c == -lsb;
1331     }
1332   else if (GET_CODE (op) == CONST_DOUBLE
1333            && (mode == VOIDmode || mode == DImode))
1334     {
1335       HOST_WIDE_INT low, high, lsb;
1336
1337       if (HOST_BITS_PER_WIDE_INT < 64)
1338         high = CONST_DOUBLE_HIGH (op);
1339
1340       low = CONST_DOUBLE_LOW (op);
1341       if (low & 1)
1342         {
1343           if (HOST_BITS_PER_WIDE_INT < 64)
1344             high = ~high;
1345           low = ~low;
1346         }
1347
1348       if (low == 0)
1349         {
1350           if (HOST_BITS_PER_WIDE_INT >= 64 || high == 0)
1351             return 0;
1352
1353           lsb = high & -high;
1354           return high == -lsb;
1355         }
1356
1357       lsb = low & -low;
1358       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
1359     }
1360   else
1361     return 0;
1362 }
1363
1364 /* Return 1 if the operand is either a non-special register or a constant
1365    that can be used as the operand of a PowerPC64 logical AND insn.  */
1366
1367 int
1368 and64_operand (op, mode)
1369     rtx op;
1370     enum machine_mode mode;
1371 {
1372   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1373     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
1374
1375   return (logical_operand (op, mode) || mask64_operand (op, mode));
1376 }
1377
1378 /* Return 1 if the operand is either a non-special register or a
1379    constant that can be used as the operand of an RS/6000 logical AND insn.  */
1380
1381 int
1382 and_operand (op, mode)
1383     rtx op;
1384     enum machine_mode mode;
1385 {
1386   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1387     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
1388
1389   return (logical_operand (op, mode) || mask_operand (op, mode));
1390 }
1391
1392 /* Return 1 if the operand is a general register or memory operand.  */
1393
1394 int
1395 reg_or_mem_operand (op, mode)
1396      rtx op;
1397      enum machine_mode mode;
1398 {
1399   return (gpc_reg_operand (op, mode)
1400           || memory_operand (op, mode)
1401           || volatile_mem_operand (op, mode));
1402 }
1403
1404 /* Return 1 if the operand is a general register or memory operand without
1405    pre_inc or pre_dec which produces invalid form of PowerPC lwa
1406    instruction.  */
1407
1408 int
1409 lwa_operand (op, mode)
1410      rtx op;
1411      enum machine_mode mode;
1412 {
1413   rtx inner = op;
1414
1415   if (reload_completed && GET_CODE (inner) == SUBREG)
1416     inner = SUBREG_REG (inner);
1417     
1418   return gpc_reg_operand (inner, mode)
1419     || (memory_operand (inner, mode)
1420         && GET_CODE (XEXP (inner, 0)) != PRE_INC
1421         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
1422         && (GET_CODE (XEXP (inner, 0)) != PLUS
1423             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
1424             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
1425 }
1426
1427 /* Return 1 if the operand, used inside a MEM, is a valid first argument
1428    to CALL.  This is a SYMBOL_REF or a pseudo-register, which will be
1429    forced to lr.  */
1430
1431 int
1432 call_operand (op, mode)
1433      rtx op;
1434      enum machine_mode mode;
1435 {
1436   if (mode != VOIDmode && GET_MODE (op) != mode)
1437     return 0;
1438
1439   return (GET_CODE (op) == SYMBOL_REF
1440           || (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER));
1441 }
1442
1443 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
1444    this file and the function is not weakly defined.  */
1445
1446 int
1447 current_file_function_operand (op, mode)
1448      rtx op;
1449      enum machine_mode mode ATTRIBUTE_UNUSED;
1450 {
1451   return (GET_CODE (op) == SYMBOL_REF
1452           && (SYMBOL_REF_FLAG (op)
1453               || (op == XEXP (DECL_RTL (current_function_decl), 0)
1454                   && ! DECL_WEAK (current_function_decl))));
1455 }
1456
1457 /* Return 1 if this operand is a valid input for a move insn.  */
1458
1459 int
1460 input_operand (op, mode)
1461      rtx op;
1462      enum machine_mode mode;
1463 {
1464   /* Memory is always valid.  */
1465   if (memory_operand (op, mode))
1466     return 1;
1467
1468   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
1469   if (GET_CODE (op) == CONSTANT_P_RTX)
1470     return 1;
1471
1472   /* For floating-point, easy constants are valid.  */
1473   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1474       && CONSTANT_P (op)
1475       && easy_fp_constant (op, mode))
1476     return 1;
1477
1478   /* Allow any integer constant.  */
1479   if (GET_MODE_CLASS (mode) == MODE_INT
1480       && (GET_CODE (op) == CONST_INT
1481           || GET_CODE (op) == CONST_DOUBLE))
1482     return 1;
1483
1484   /* For floating-point or multi-word mode, the only remaining valid type
1485      is a register.  */
1486   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1487       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1488     return register_operand (op, mode);
1489
1490   /* The only cases left are integral modes one word or smaller (we
1491      do not get called for MODE_CC values).  These can be in any
1492      register.  */
1493   if (register_operand (op, mode))
1494     return 1;
1495
1496   /* A SYMBOL_REF referring to the TOC is valid.  */
1497   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (op))
1498     return 1;
1499
1500   /* A constant pool expression (relative to the TOC) is valid */
1501   if (TOC_RELATIVE_EXPR_P (op))
1502     return 1;
1503
1504   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
1505      to be valid.  */
1506   if (DEFAULT_ABI == ABI_V4
1507       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
1508       && small_data_operand (op, Pmode))
1509     return 1;
1510
1511   return 0;
1512 }
1513
1514 /* Return 1 for an operand in small memory on V.4/eabi.  */
1515
1516 int
1517 small_data_operand (op, mode)
1518      rtx op ATTRIBUTE_UNUSED;
1519      enum machine_mode mode ATTRIBUTE_UNUSED;
1520 {
1521 #if TARGET_ELF
1522   rtx sym_ref;
1523
1524   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
1525     return 0;
1526
1527   if (DEFAULT_ABI != ABI_V4)
1528     return 0;
1529
1530   if (GET_CODE (op) == SYMBOL_REF)
1531     sym_ref = op;
1532
1533   else if (GET_CODE (op) != CONST
1534            || GET_CODE (XEXP (op, 0)) != PLUS
1535            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
1536            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
1537     return 0;
1538
1539   else
1540     {
1541       rtx sum = XEXP (op, 0);
1542       HOST_WIDE_INT summand;
1543
1544       /* We have to be careful here, because it is the referenced address
1545         that must be 32k from _SDA_BASE_, not just the symbol.  */
1546       summand = INTVAL (XEXP (sum, 1));
1547       if (summand < 0 || summand > g_switch_value)
1548        return 0;
1549
1550       sym_ref = XEXP (sum, 0);
1551     }
1552
1553   if (*XSTR (sym_ref, 0) != '@')
1554     return 0;
1555
1556   return 1;
1557
1558 #else
1559   return 0;
1560 #endif
1561 }
1562 \f
1563 static int 
1564 constant_pool_expr_1 (op, have_sym, have_toc) 
1565     rtx op;
1566     int *have_sym;
1567     int *have_toc;
1568 {
1569   switch (GET_CODE(op)) 
1570     {
1571     case SYMBOL_REF:
1572       if (CONSTANT_POOL_ADDRESS_P (op))
1573         {
1574           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
1575             {
1576               *have_sym = 1;
1577               return 1;
1578             }
1579           else
1580             return 0;
1581         }
1582       else if (! strcmp (XSTR (op, 0), toc_label_name))
1583         {
1584           *have_toc = 1;
1585           return 1;
1586         }
1587       else
1588         return 0;
1589     case PLUS:
1590     case MINUS:
1591       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc) &&
1592         constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc);
1593     case CONST:
1594       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
1595     case CONST_INT:
1596       return 1;
1597     default:
1598       return 0;
1599     }
1600 }
1601
1602 int
1603 constant_pool_expr_p (op)
1604     rtx op;
1605 {
1606   int have_sym = 0;
1607   int have_toc = 0;
1608   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
1609 }
1610
1611 int
1612 toc_relative_expr_p (op)
1613     rtx op;
1614 {
1615     int have_sym = 0;
1616     int have_toc = 0;
1617     return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
1618 }
1619
1620 /* Try machine-dependent ways of modifying an illegitimate address
1621    to be legitimate.  If we find one, return the new, valid address.
1622    This is used from only one place: `memory_address' in explow.c.
1623
1624    OLDX is the address as it was before break_out_memory_refs was
1625    called.  In some cases it is useful to look at this to decide what
1626    needs to be done.
1627
1628    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
1629
1630    It is always safe for this function to do nothing.  It exists to
1631    recognize opportunities to optimize the output.
1632
1633    On RS/6000, first check for the sum of a register with a constant
1634    integer that is out of range.  If so, generate code to add the
1635    constant with the low-order 16 bits masked to the register and force
1636    this result into another register (this can be done with `cau').
1637    Then generate an address of REG+(CONST&0xffff), allowing for the
1638    possibility of bit 16 being a one.
1639
1640    Then check for the sum of a register and something not constant, try to
1641    load the other things into a register and return the sum.  */
1642 rtx
1643 rs6000_legitimize_address (x, oldx, mode)
1644      rtx x;
1645      rtx oldx ATTRIBUTE_UNUSED;
1646      enum machine_mode mode;
1647 {
1648   if (GET_CODE (x) == PLUS 
1649       && GET_CODE (XEXP (x, 0)) == REG
1650       && GET_CODE (XEXP (x, 1)) == CONST_INT
1651       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
1652     { 
1653       HOST_WIDE_INT high_int, low_int;
1654       rtx sum;
1655       high_int = INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff);
1656       low_int = INTVAL (XEXP (x, 1)) & 0xffff;
1657       if (low_int & 0x8000)
1658         high_int += 0x10000, low_int |= ((HOST_WIDE_INT) -1) << 16;
1659       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
1660                                          GEN_INT (high_int)), 0);
1661       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
1662     }
1663   else if (GET_CODE (x) == PLUS 
1664            && GET_CODE (XEXP (x, 0)) == REG
1665            && GET_CODE (XEXP (x, 1)) != CONST_INT
1666            && GET_MODE_NUNITS (mode) == 1
1667            && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
1668            && (TARGET_POWERPC64 || mode != DImode)
1669            && mode != TImode)
1670     {
1671       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
1672                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
1673     }
1674   else if (ALTIVEC_VECTOR_MODE (mode))
1675     {
1676       rtx reg;
1677
1678       /* Make sure both operands are registers.  */
1679       if (GET_CODE (x) == PLUS)
1680         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
1681                              force_reg (Pmode, XEXP (x, 1)));
1682
1683       reg = force_reg (Pmode, x);
1684       return reg;
1685     }
1686   else if (TARGET_ELF && TARGET_32BIT && TARGET_NO_TOC && ! flag_pic
1687            && GET_CODE (x) != CONST_INT
1688            && GET_CODE (x) != CONST_DOUBLE 
1689            && CONSTANT_P (x)
1690            && GET_MODE_NUNITS (mode) == 1
1691            && (GET_MODE_BITSIZE (mode) <= 32
1692                || (TARGET_HARD_FLOAT && mode == DFmode)))
1693     {
1694       rtx reg = gen_reg_rtx (Pmode);
1695       emit_insn (gen_elf_high (reg, (x)));
1696       return gen_rtx_LO_SUM (Pmode, reg, (x));
1697     }
1698   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
1699            && ! flag_pic
1700            && GET_CODE (x) != CONST_INT
1701            && GET_CODE (x) != CONST_DOUBLE 
1702            && CONSTANT_P (x)
1703            && (TARGET_HARD_FLOAT || mode != DFmode)
1704            && mode != DImode 
1705            && mode != TImode)
1706     {
1707       rtx reg = gen_reg_rtx (Pmode);
1708       emit_insn (gen_macho_high (reg, (x)));
1709       return gen_rtx_LO_SUM (Pmode, reg, (x));
1710     }
1711   else if (TARGET_TOC 
1712            && CONSTANT_POOL_EXPR_P (x)
1713            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
1714     {
1715       return create_TOC_reference (x);
1716     }
1717   else
1718     return NULL_RTX;
1719 }
1720
1721 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
1722    that is a valid memory address for an instruction.
1723    The MODE argument is the machine mode for the MEM expression
1724    that wants to use this address.
1725
1726    On the RS/6000, there are four valid address: a SYMBOL_REF that
1727    refers to a constant pool entry of an address (or the sum of it
1728    plus a constant), a short (16-bit signed) constant plus a register,
1729    the sum of two registers, or a register indirect, possibly with an
1730    auto-increment.  For DFmode and DImode with an constant plus register,
1731    we must ensure that both words are addressable or PowerPC64 with offset
1732    word aligned.
1733
1734    For modes spanning multiple registers (DFmode in 32-bit GPRs,
1735    32-bit DImode, TImode), indexed addressing cannot be used because
1736    adjacent memory cells are accessed by adding word-sized offsets
1737    during assembly output.  */
1738 int
1739 rs6000_legitimate_address (mode, x, reg_ok_strict)
1740     enum machine_mode mode;
1741     rtx x;
1742     int reg_ok_strict;
1743 {
1744   if (LEGITIMATE_INDIRECT_ADDRESS_P (x, reg_ok_strict))
1745     return 1;
1746   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
1747       && TARGET_UPDATE
1748       && LEGITIMATE_INDIRECT_ADDRESS_P (XEXP (x, 0), reg_ok_strict))
1749     return 1;
1750   if (LEGITIMATE_SMALL_DATA_P (mode, x))
1751     return 1;
1752   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
1753     return 1;
1754   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
1755   if (! reg_ok_strict
1756       && GET_CODE (x) == PLUS
1757       && GET_CODE (XEXP (x, 0)) == REG
1758       && XEXP (x, 0) == virtual_stack_vars_rtx
1759       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1760     return 1;
1761   if (LEGITIMATE_OFFSET_ADDRESS_P (mode, x, reg_ok_strict))
1762     return 1;
1763   if (mode != TImode
1764       && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
1765       && (TARGET_POWERPC64 || mode != DImode)
1766       && LEGITIMATE_INDEXED_ADDRESS_P (x, reg_ok_strict))
1767     return 1;
1768   if (LEGITIMATE_LO_SUM_ADDRESS_P (mode, x, reg_ok_strict))
1769     return 1;
1770   return 0;
1771 }
1772 \f
1773 /* Try to output insns to set TARGET equal to the constant C if it can
1774    be done in less than N insns.  Do all computations in MODE.
1775    Returns the place where the output has been placed if it can be
1776    done and the insns have been emitted.  If it would take more than N
1777    insns, zero is returned and no insns and emitted.  */
1778
1779 rtx
1780 rs6000_emit_set_const (dest, mode, source, n)
1781      rtx dest, source;
1782      enum machine_mode mode;
1783      int n ATTRIBUTE_UNUSED;
1784 {
1785   HOST_WIDE_INT c0, c1;
1786
1787   if (mode == QImode || mode == HImode || mode == SImode)
1788     {
1789       if (dest == NULL)
1790         dest = gen_reg_rtx (mode);
1791       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
1792       return dest;
1793     }
1794
1795   if (GET_CODE (source) == CONST_INT)
1796     {
1797       c0 = INTVAL (source);
1798       c1 = -(c0 < 0);
1799     }
1800   else if (GET_CODE (source) == CONST_DOUBLE)
1801     {
1802 #if HOST_BITS_PER_WIDE_INT >= 64
1803       c0 = CONST_DOUBLE_LOW (source);
1804       c1 = -(c0 < 0);
1805 #else
1806       c0 = CONST_DOUBLE_LOW (source);
1807       c1 = CONST_DOUBLE_HIGH (source);
1808 #endif
1809     }
1810   else
1811     abort ();
1812
1813   return rs6000_emit_set_long_const (dest, c0, c1);
1814 }
1815
1816 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
1817    fall back to a straight forward decomposition.  We do this to avoid
1818    exponential run times encountered when looking for longer sequences
1819    with rs6000_emit_set_const.  */
1820 static rtx
1821 rs6000_emit_set_long_const (dest, c1, c2)
1822      rtx dest;
1823      HOST_WIDE_INT c1, c2;
1824 {
1825   if (!TARGET_POWERPC64)
1826     {
1827       rtx operand1, operand2;
1828
1829       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
1830                                         DImode);
1831       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
1832                                         DImode);
1833       emit_move_insn (operand1, GEN_INT (c1));
1834       emit_move_insn (operand2, GEN_INT (c2));
1835     }
1836   else
1837     {
1838       HOST_WIDE_INT d1, d2, d3, d4;
1839
1840   /* Decompose the entire word */
1841 #if HOST_BITS_PER_WIDE_INT >= 64
1842       if (c2 != -(c1 < 0))
1843         abort ();
1844       d1 = ((c1 & 0xffff) ^ 0x8000) - 0x8000;
1845       c1 -= d1;
1846       d2 = ((c1 & 0xffffffff) ^ 0x80000000) - 0x80000000;
1847       c1 = (c1 - d2) >> 32;
1848       d3 = ((c1 & 0xffff) ^ 0x8000) - 0x8000;
1849       c1 -= d3;
1850       d4 = ((c1 & 0xffffffff) ^ 0x80000000) - 0x80000000;
1851       if (c1 != d4)
1852         abort ();
1853 #else
1854       d1 = ((c1 & 0xffff) ^ 0x8000) - 0x8000;
1855       c1 -= d1;
1856       d2 = ((c1 & 0xffffffff) ^ 0x80000000) - 0x80000000;
1857       if (c1 != d2)
1858         abort ();
1859       c2 += (d2 < 0);
1860       d3 = ((c2 & 0xffff) ^ 0x8000) - 0x8000;
1861       c2 -= d3;
1862       d4 = ((c2 & 0xffffffff) ^ 0x80000000) - 0x80000000;
1863       if (c2 != d4)
1864         abort ();
1865 #endif
1866
1867       /* Construct the high word */
1868       if (d4 != 0)
1869         {
1870           emit_move_insn (dest, GEN_INT (d4));
1871           if (d3 != 0)
1872             emit_move_insn (dest,
1873                             gen_rtx_PLUS (DImode, dest, GEN_INT (d3)));
1874         }
1875       else
1876         emit_move_insn (dest, GEN_INT (d3));
1877
1878       /* Shift it into place */
1879       if (d3 != 0 || d4 != 0)
1880         emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
1881
1882       /* Add in the low bits.  */
1883       if (d2 != 0)
1884         emit_move_insn (dest, gen_rtx_PLUS (DImode, dest, GEN_INT (d2)));
1885       if (d1 != 0)
1886         emit_move_insn (dest, gen_rtx_PLUS (DImode, dest, GEN_INT (d1)));
1887     }
1888
1889   return dest;
1890 }
1891
1892 /* Emit a move from SOURCE to DEST in mode MODE.  */
1893 void
1894 rs6000_emit_move (dest, source, mode)
1895      rtx dest;
1896      rtx source;
1897      enum machine_mode mode;
1898 {
1899   rtx operands[2];
1900   operands[0] = dest;
1901   operands[1] = source;
1902   
1903   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
1904   if (GET_CODE (operands[1]) == CONST_DOUBLE
1905       && ! FLOAT_MODE_P (mode)
1906       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1907     {
1908       /* FIXME.  This should never happen.  */
1909       /* Since it seems that it does, do the safe thing and convert
1910          to a CONST_INT.  */
1911       operands[1] = 
1912         GEN_INT (trunc_int_for_mode (CONST_DOUBLE_LOW (operands[1]), mode));
1913     }
1914   if (GET_CODE (operands[1]) == CONST_DOUBLE
1915       && ! FLOAT_MODE_P (mode)
1916       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
1917            && CONST_DOUBLE_LOW (operands[1]) >= 0)
1918           || (CONST_DOUBLE_HIGH (operands[1]) == -1
1919               && CONST_DOUBLE_LOW (operands[1]) < 0)))
1920     abort ();
1921
1922   /* Check if GCC is setting up a block move that will end up using FP
1923      registers as temporaries.  We must make sure this is acceptable.  */
1924   if (GET_CODE (operands[0]) == MEM
1925       && GET_CODE (operands[1]) == MEM
1926       && mode == DImode
1927       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
1928           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
1929       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
1930                                             ? 32 : MEM_ALIGN (operands[0])))
1931             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
1932                                                ? 32 
1933                                                : MEM_ALIGN (operands[1]))))
1934       && ! MEM_VOLATILE_P (operands [0])
1935       && ! MEM_VOLATILE_P (operands [1]))
1936     {
1937       emit_move_insn (adjust_address (operands[0], SImode, 0),
1938                       adjust_address (operands[1], SImode, 0));
1939       emit_move_insn (adjust_address (operands[0], SImode, 4),
1940                       adjust_address (operands[1], SImode, 4));
1941       return;
1942     }
1943   
1944   if (! no_new_pseudos && GET_CODE (operands[0]) != REG)
1945     operands[1] = force_reg (mode, operands[1]);
1946
1947   if (mode == SFmode && ! TARGET_POWERPC && TARGET_HARD_FLOAT
1948       && GET_CODE (operands[0]) == MEM)
1949     {
1950       int regnum;
1951
1952       if (reload_in_progress || reload_completed)
1953         regnum = true_regnum (operands[1]);
1954       else if (GET_CODE (operands[1]) == REG)
1955         regnum = REGNO (operands[1]);
1956       else
1957         regnum = -1;
1958       
1959       /* If operands[1] is a register, on POWER it may have
1960          double-precision data in it, so truncate it to single
1961          precision.  */
1962       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
1963         {
1964           rtx newreg;
1965           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
1966           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
1967           operands[1] = newreg;
1968         }
1969     }
1970
1971   /* Handle the case where reload calls us with an invalid address;
1972      and the case of CONSTANT_P_RTX.  */
1973   if (! general_operand (operands[1], mode)
1974       || ! nonimmediate_operand (operands[0], mode)
1975       || GET_CODE (operands[1]) == CONSTANT_P_RTX)
1976     {
1977       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
1978       return;
1979     }
1980   
1981   /* FIXME:  In the long term, this switch statement should go away
1982      and be replaced by a sequence of tests based on things like
1983      mode == Pmode.  */
1984   switch (mode)
1985     {
1986     case HImode:
1987     case QImode:
1988       if (CONSTANT_P (operands[1])
1989           && GET_CODE (operands[1]) != CONST_INT)
1990         operands[1] = force_const_mem (mode, operands[1]);
1991       break;
1992
1993     case TFmode:
1994     case DFmode:
1995     case SFmode:
1996       if (CONSTANT_P (operands[1]) 
1997           && ! easy_fp_constant (operands[1], mode))
1998         operands[1] = force_const_mem (mode, operands[1]);
1999       break;
2000       
2001     case V16QImode:
2002     case V8HImode:
2003     case V4SFmode:
2004     case V4SImode:
2005       /* fixme: aldyh -- allow vector constants when they are implemented.  */
2006       if (CONSTANT_P (operands[1]))
2007         operands[1] = force_const_mem (mode, operands[1]);
2008       break;
2009       
2010     case SImode:
2011     case DImode:
2012       /* Use default pattern for address of ELF small data */
2013       if (TARGET_ELF
2014           && mode == Pmode
2015           && DEFAULT_ABI == ABI_V4
2016           && (GET_CODE (operands[1]) == SYMBOL_REF 
2017               || GET_CODE (operands[1]) == CONST)
2018           && small_data_operand (operands[1], mode))
2019         {
2020           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2021           return;
2022         }
2023
2024       if (DEFAULT_ABI == ABI_V4
2025           && mode == Pmode && mode == SImode
2026           && flag_pic == 1 && got_operand (operands[1], mode))
2027         {
2028           emit_insn (gen_movsi_got (operands[0], operands[1]));
2029           return;
2030         }
2031
2032       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
2033           && TARGET_NO_TOC && ! flag_pic
2034           && mode == Pmode
2035           && CONSTANT_P (operands[1])
2036           && GET_CODE (operands[1]) != HIGH
2037           && GET_CODE (operands[1]) != CONST_INT)
2038         {
2039           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
2040
2041           /* If this is a function address on -mcall-aixdesc,
2042              convert it to the address of the descriptor.  */
2043           if (DEFAULT_ABI == ABI_AIX
2044               && GET_CODE (operands[1]) == SYMBOL_REF
2045               && XSTR (operands[1], 0)[0] == '.')
2046             {
2047               const char *name = XSTR (operands[1], 0);
2048               rtx new_ref;
2049               while (*name == '.')
2050                 name++;
2051               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
2052               CONSTANT_POOL_ADDRESS_P (new_ref)
2053                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
2054               SYMBOL_REF_FLAG (new_ref) = SYMBOL_REF_FLAG (operands[1]);
2055               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
2056               operands[1] = new_ref;
2057             }
2058
2059           if (DEFAULT_ABI == ABI_DARWIN)
2060             {
2061               emit_insn (gen_macho_high (target, operands[1]));
2062               emit_insn (gen_macho_low (operands[0], target, operands[1]));
2063               return;
2064             }
2065
2066           emit_insn (gen_elf_high (target, operands[1]));
2067           emit_insn (gen_elf_low (operands[0], target, operands[1]));
2068           return;
2069         }
2070
2071       /* If this is a SYMBOL_REF that refers to a constant pool entry,
2072          and we have put it in the TOC, we just need to make a TOC-relative
2073          reference to it.  */
2074       if (TARGET_TOC
2075           && GET_CODE (operands[1]) == SYMBOL_REF
2076           && CONSTANT_POOL_EXPR_P (operands[1])
2077           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
2078                                               get_pool_mode (operands[1])))
2079         {
2080           operands[1] = create_TOC_reference (operands[1]);
2081         }
2082       else if (mode == Pmode
2083                && CONSTANT_P (operands[1])
2084                && ((GET_CODE (operands[1]) != CONST_INT
2085                     && ! easy_fp_constant (operands[1], mode))
2086                    || (GET_CODE (operands[1]) == CONST_INT
2087                        && num_insns_constant (operands[1], mode) > 2)
2088                    || (GET_CODE (operands[0]) == REG
2089                        && FP_REGNO_P (REGNO (operands[0]))))
2090                && GET_CODE (operands[1]) != HIGH
2091                && ! LEGITIMATE_CONSTANT_POOL_ADDRESS_P (operands[1])
2092                && ! TOC_RELATIVE_EXPR_P (operands[1]))
2093         {
2094           /* Emit a USE operation so that the constant isn't deleted if
2095              expensive optimizations are turned on because nobody
2096              references it.  This should only be done for operands that
2097              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
2098              This should not be done for operands that contain LABEL_REFs.
2099              For now, we just handle the obvious case.  */
2100           if (GET_CODE (operands[1]) != LABEL_REF)
2101             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
2102
2103 #if TARGET_MACHO
2104           /* Darwin uses a special PIC legitimizer.  */
2105           if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
2106             {
2107               operands[1] =
2108                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
2109                                                         operands[0]);
2110               if (operands[0] != operands[1])
2111                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2112               return;
2113             }
2114 #endif
2115
2116           /* If we are to limit the number of things we put in the TOC and
2117              this is a symbol plus a constant we can add in one insn,
2118              just put the symbol in the TOC and add the constant.  Don't do
2119              this if reload is in progress.  */
2120           if (GET_CODE (operands[1]) == CONST
2121               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
2122               && GET_CODE (XEXP (operands[1], 0)) == PLUS
2123               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
2124               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2125                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
2126               && ! side_effects_p (operands[0]))
2127             {
2128               rtx sym =
2129                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
2130               rtx other = XEXP (XEXP (operands[1], 0), 1);
2131
2132               sym = force_reg (mode, sym);
2133               if (mode == SImode)
2134                 emit_insn (gen_addsi3 (operands[0], sym, other));
2135               else
2136                 emit_insn (gen_adddi3 (operands[0], sym, other));
2137               return;
2138             }
2139
2140           operands[1] = force_const_mem (mode, operands[1]);
2141
2142           if (TARGET_TOC 
2143               && CONSTANT_POOL_EXPR_P (XEXP (operands[1], 0))
2144               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
2145                         get_pool_constant (XEXP (operands[1], 0)),
2146                         get_pool_mode (XEXP (operands[1], 0))))
2147             {
2148               operands[1]
2149                 = gen_rtx_MEM (mode,
2150                                create_TOC_reference (XEXP (operands[1], 0)));
2151               set_mem_alias_set (operands[1], get_TOC_alias_set ());
2152               RTX_UNCHANGING_P (operands[1]) = 1;
2153             }
2154         }
2155       break;
2156
2157     case TImode:
2158       if (GET_CODE (operands[0]) == MEM
2159           && GET_CODE (XEXP (operands[0], 0)) != REG
2160           && ! reload_in_progress)
2161         operands[0]
2162           = replace_equiv_address (operands[0],
2163                                    copy_addr_to_reg (XEXP (operands[0], 0)));
2164
2165       if (GET_CODE (operands[1]) == MEM
2166           && GET_CODE (XEXP (operands[1], 0)) != REG
2167           && ! reload_in_progress)
2168         operands[1]
2169           = replace_equiv_address (operands[1],
2170                                    copy_addr_to_reg (XEXP (operands[1], 0)));
2171       break;
2172
2173     default:
2174       abort ();
2175     }
2176
2177   /* Above, we may have called force_const_mem which may have returned
2178      an invalid address.  If we can, fix this up; otherwise, reload will
2179      have to deal with it.  */
2180   if (GET_CODE (operands[1]) == MEM
2181       && ! memory_address_p (mode, XEXP (operands[1], 0))
2182       && ! reload_in_progress)
2183     operands[1] = adjust_address (operands[1], mode, 0);
2184
2185   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2186   return;
2187 }
2188 \f
2189 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2190    for a call to a function whose data type is FNTYPE.
2191    For a library call, FNTYPE is 0.
2192
2193    For incoming args we set the number of arguments in the prototype large
2194    so we never return a PARALLEL.  */
2195
2196 void
2197 init_cumulative_args (cum, fntype, libname, incoming)
2198      CUMULATIVE_ARGS *cum;
2199      tree fntype;
2200      rtx libname ATTRIBUTE_UNUSED;
2201      int incoming;
2202 {
2203   static CUMULATIVE_ARGS zero_cumulative;
2204
2205   *cum = zero_cumulative;
2206   cum->words = 0;
2207   cum->fregno = FP_ARG_MIN_REG;
2208   cum->vregno = ALTIVEC_ARG_MIN_REG;
2209   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
2210   cum->call_cookie = CALL_NORMAL;
2211   cum->sysv_gregno = GP_ARG_MIN_REG;
2212
2213   if (incoming)
2214     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
2215
2216   else if (cum->prototype)
2217     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
2218                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
2219                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
2220
2221   else
2222     cum->nargs_prototype = 0;
2223
2224   cum->orig_nargs = cum->nargs_prototype;
2225
2226   /* Check for longcall's */
2227   if (fntype && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype)))
2228     cum->call_cookie = CALL_LONG;
2229
2230   if (TARGET_DEBUG_ARG)
2231     {
2232       fprintf (stderr, "\ninit_cumulative_args:");
2233       if (fntype)
2234         {
2235           tree ret_type = TREE_TYPE (fntype);
2236           fprintf (stderr, " ret code = %s,",
2237                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
2238         }
2239
2240       if (cum->call_cookie & CALL_LONG)
2241         fprintf (stderr, " longcall,");
2242
2243       fprintf (stderr, " proto = %d, nargs = %d\n",
2244                cum->prototype, cum->nargs_prototype);
2245     }
2246 }
2247 \f
2248 /* If defined, a C expression which determines whether, and in which
2249    direction, to pad out an argument with extra space.  The value
2250    should be of type `enum direction': either `upward' to pad above
2251    the argument, `downward' to pad below, or `none' to inhibit
2252    padding.
2253
2254    For the AIX ABI structs are always stored left shifted in their
2255    argument slot.  */
2256
2257 enum direction
2258 function_arg_padding (mode, type)
2259      enum machine_mode mode;
2260      tree type;
2261 {
2262   if (type != 0 && AGGREGATE_TYPE_P (type))
2263     return upward;
2264
2265   /* This is the default definition.  */
2266   return (! BYTES_BIG_ENDIAN
2267           ? upward
2268           : ((mode == BLKmode
2269               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2270                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
2271               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
2272              ? downward : upward));
2273 }
2274
2275 /* If defined, a C expression that gives the alignment boundary, in bits,
2276    of an argument with the specified mode and type.  If it is not defined, 
2277    PARM_BOUNDARY is used for all arguments.
2278    
2279    V.4 wants long longs to be double word aligned.  */
2280
2281 int
2282 function_arg_boundary (mode, type)
2283      enum machine_mode mode;
2284      tree type ATTRIBUTE_UNUSED;
2285 {
2286   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
2287     return 64;
2288   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2289     return 128;
2290   else
2291     return PARM_BOUNDARY;
2292 }
2293 \f
2294 /* Update the data in CUM to advance over an argument
2295    of mode MODE and data type TYPE.
2296    (TYPE is null for libcalls where that information may not be available.)  */
2297
2298 void
2299 function_arg_advance (cum, mode, type, named)
2300      CUMULATIVE_ARGS *cum;
2301      enum machine_mode mode;
2302      tree type;
2303      int named;
2304 {
2305   cum->nargs_prototype--;
2306
2307   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2308     {
2309       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
2310         cum->vregno++;
2311       else
2312         cum->words += RS6000_ARG_SIZE (mode, type);
2313     }
2314   else if (DEFAULT_ABI == ABI_V4)
2315     {
2316       if (TARGET_HARD_FLOAT
2317           && (mode == SFmode || mode == DFmode))
2318         {
2319           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2320             cum->fregno++;
2321           else
2322             {
2323               if (mode == DFmode)
2324                 cum->words += cum->words & 1;
2325               cum->words += RS6000_ARG_SIZE (mode, type);
2326             }
2327         }
2328       else
2329         {
2330           int n_words;
2331           int gregno = cum->sysv_gregno;
2332
2333           /* Aggregates and IEEE quad get passed by reference.  */
2334           if ((type && AGGREGATE_TYPE_P (type))
2335               || mode == TFmode)
2336             n_words = 1;
2337           else 
2338             n_words = RS6000_ARG_SIZE (mode, type);
2339
2340           /* Long long is put in odd registers.  */
2341           if (n_words == 2 && (gregno & 1) == 0)
2342             gregno += 1;
2343
2344           /* Long long is not split between registers and stack.  */
2345           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
2346             {
2347               /* Long long is aligned on the stack.  */
2348               if (n_words == 2)
2349                 cum->words += cum->words & 1;
2350               cum->words += n_words;
2351             }
2352
2353           /* Note: continuing to accumulate gregno past when we've started
2354              spilling to the stack indicates the fact that we've started
2355              spilling to the stack to expand_builtin_saveregs.  */
2356           cum->sysv_gregno = gregno + n_words;
2357         }
2358
2359       if (TARGET_DEBUG_ARG)
2360         {
2361           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2362                    cum->words, cum->fregno);
2363           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
2364                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
2365           fprintf (stderr, "mode = %4s, named = %d\n",
2366                    GET_MODE_NAME (mode), named);
2367         }
2368     }
2369   else
2370     {
2371       int align = (TARGET_32BIT && (cum->words & 1) != 0
2372                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2373
2374       cum->words += align + RS6000_ARG_SIZE (mode, type);
2375
2376       if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_HARD_FLOAT)
2377         cum->fregno++;
2378
2379       if (TARGET_DEBUG_ARG)
2380         {
2381           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2382                    cum->words, cum->fregno);
2383           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
2384                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
2385           fprintf (stderr, "named = %d, align = %d\n", named, align);
2386         }
2387     }
2388 }
2389 \f
2390 /* Determine where to put an argument to a function.
2391    Value is zero to push the argument on the stack,
2392    or a hard register in which to store the argument.
2393
2394    MODE is the argument's machine mode.
2395    TYPE is the data type of the argument (as a tree).
2396     This is null for libcalls where that information may
2397     not be available.
2398    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2399     the preceding args and about the function being called.
2400    NAMED is nonzero if this argument is a named parameter
2401     (otherwise it is an extra parameter matching an ellipsis).
2402
2403    On RS/6000 the first eight words of non-FP are normally in registers
2404    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
2405    Under V.4, the first 8 FP args are in registers.
2406
2407    If this is floating-point and no prototype is specified, we use
2408    both an FP and integer register (or possibly FP reg and stack).  Library
2409    functions (when TYPE is zero) always have the proper types for args,
2410    so we can pass the FP value just in one register.  emit_library_function
2411    doesn't support PARALLEL anyway.  */
2412
2413 struct rtx_def *
2414 function_arg (cum, mode, type, named)
2415      CUMULATIVE_ARGS *cum;
2416      enum machine_mode mode;
2417      tree type;
2418      int named;
2419 {
2420   enum rs6000_abi abi = DEFAULT_ABI;
2421
2422   /* Return a marker to indicate whether CR1 needs to set or clear the
2423      bit that V.4 uses to say fp args were passed in registers.
2424      Assume that we don't need the marker for software floating point,
2425      or compiler generated library calls.  */
2426   if (mode == VOIDmode)
2427     {
2428       if (abi == ABI_V4
2429           && TARGET_HARD_FLOAT
2430           && cum->nargs_prototype < 0
2431           && type && (cum->prototype || TARGET_NO_PROTOTYPE))
2432         {
2433           return GEN_INT (cum->call_cookie
2434                           | ((cum->fregno == FP_ARG_MIN_REG)
2435                              ? CALL_V4_SET_FP_ARGS
2436                              : CALL_V4_CLEAR_FP_ARGS));
2437         }
2438
2439       return GEN_INT (cum->call_cookie);
2440     }
2441
2442   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2443     {
2444       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
2445         return gen_rtx_REG (mode, cum->vregno);
2446       else
2447         return NULL;
2448     }
2449   else if (abi == ABI_V4)
2450     {
2451       if (TARGET_HARD_FLOAT
2452           && (mode == SFmode || mode == DFmode))
2453         {
2454           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2455             return gen_rtx_REG (mode, cum->fregno);
2456           else
2457             return NULL;
2458         }
2459       else
2460         {
2461           int n_words;
2462           int gregno = cum->sysv_gregno;
2463
2464           /* Aggregates and IEEE quad get passed by reference.  */
2465           if ((type && AGGREGATE_TYPE_P (type))
2466               || mode == TFmode)
2467             n_words = 1;
2468           else 
2469             n_words = RS6000_ARG_SIZE (mode, type);
2470
2471           /* Long long is put in odd registers.  */
2472           if (n_words == 2 && (gregno & 1) == 0)
2473             gregno += 1;
2474
2475           /* Long long is not split between registers and stack.  */
2476           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
2477             return gen_rtx_REG (mode, gregno);
2478           else
2479             return NULL;
2480         }
2481     }
2482   else
2483     {
2484       int align = (TARGET_32BIT && (cum->words & 1) != 0
2485                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2486       int align_words = cum->words + align;
2487
2488       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2489         return NULL_RTX;
2490
2491       if (USE_FP_FOR_ARG_P (*cum, mode, type))
2492         {
2493           if (! type
2494               || ((cum->nargs_prototype > 0)
2495                   /* IBM AIX extended its linkage convention definition always
2496                      to require FP args after register save area hole on the
2497                      stack.  */
2498                   && (DEFAULT_ABI != ABI_AIX
2499                       || ! TARGET_XL_CALL
2500                       || (align_words < GP_ARG_NUM_REG))))
2501             return gen_rtx_REG (mode, cum->fregno);
2502
2503           return gen_rtx_PARALLEL (mode,
2504             gen_rtvec (2,
2505                        gen_rtx_EXPR_LIST (VOIDmode,
2506                                 ((align_words >= GP_ARG_NUM_REG)
2507                                  ? NULL_RTX
2508                                  : (align_words
2509                                     + RS6000_ARG_SIZE (mode, type)
2510                                     > GP_ARG_NUM_REG
2511                                     /* If this is partially on the stack, then
2512                                        we only include the portion actually
2513                                        in registers here.  */
2514                                     ? gen_rtx_REG (SImode,
2515                                                GP_ARG_MIN_REG + align_words)
2516                                     : gen_rtx_REG (mode,
2517                                                GP_ARG_MIN_REG + align_words))),
2518                                 const0_rtx),
2519                        gen_rtx_EXPR_LIST (VOIDmode,
2520                                 gen_rtx_REG (mode, cum->fregno),
2521                                 const0_rtx)));
2522         }
2523       else if (align_words < GP_ARG_NUM_REG)
2524         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
2525       else
2526         return NULL_RTX;
2527     }
2528 }
2529 \f
2530 /* For an arg passed partly in registers and partly in memory,
2531    this is the number of registers used.
2532    For args passed entirely in registers or entirely in memory, zero.  */
2533
2534 int
2535 function_arg_partial_nregs (cum, mode, type, named)
2536      CUMULATIVE_ARGS *cum;
2537      enum machine_mode mode;
2538      tree type;
2539      int named ATTRIBUTE_UNUSED;
2540 {
2541   if (DEFAULT_ABI == ABI_V4)
2542     return 0;
2543
2544   if (USE_FP_FOR_ARG_P (*cum, mode, type)
2545       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
2546     {
2547       if (cum->nargs_prototype >= 0)
2548         return 0;
2549     }
2550
2551   if (cum->words < GP_ARG_NUM_REG
2552       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
2553     {
2554       int ret = GP_ARG_NUM_REG - cum->words;
2555       if (ret && TARGET_DEBUG_ARG)
2556         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
2557
2558       return ret;
2559     }
2560
2561   return 0;
2562 }
2563 \f
2564 /* A C expression that indicates when an argument must be passed by
2565    reference.  If nonzero for an argument, a copy of that argument is
2566    made in memory and a pointer to the argument is passed instead of
2567    the argument itself.  The pointer is passed in whatever way is
2568    appropriate for passing a pointer to that type.
2569
2570    Under V.4, structures and unions are passed by reference.  */
2571
2572 int
2573 function_arg_pass_by_reference (cum, mode, type, named)
2574      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
2575      enum machine_mode mode ATTRIBUTE_UNUSED;
2576      tree type;
2577      int named ATTRIBUTE_UNUSED;
2578 {
2579   if (DEFAULT_ABI == ABI_V4
2580       && ((type && AGGREGATE_TYPE_P (type))
2581           || mode == TFmode))
2582     {
2583       if (TARGET_DEBUG_ARG)
2584         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
2585
2586       return 1;
2587     }
2588
2589   return 0;
2590 }
2591 \f
2592 /* Perform any needed actions needed for a function that is receiving a
2593    variable number of arguments. 
2594
2595    CUM is as above.
2596
2597    MODE and TYPE are the mode and type of the current parameter.
2598
2599    PRETEND_SIZE is a variable that should be set to the amount of stack
2600    that must be pushed by the prolog to pretend that our caller pushed
2601    it.
2602
2603    Normally, this macro will push all remaining incoming registers on the
2604    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2605
2606 void
2607 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2608      CUMULATIVE_ARGS *cum;
2609      enum machine_mode mode;
2610      tree type;
2611      int *pretend_size;
2612      int no_rtl;
2613
2614 {
2615   CUMULATIVE_ARGS next_cum;
2616   int reg_size = TARGET_32BIT ? 4 : 8;
2617   rtx save_area = NULL_RTX, mem;
2618   int first_reg_offset, set;
2619   tree fntype;
2620   int stdarg_p;
2621
2622   fntype = TREE_TYPE (current_function_decl);
2623   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2624               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2625                   != void_type_node));
2626
2627   /* For varargs, we do not want to skip the dummy va_dcl argument.
2628      For stdargs, we do want to skip the last named argument.  */
2629   next_cum = *cum;
2630   if (stdarg_p)
2631     function_arg_advance (&next_cum, mode, type, 1);
2632
2633   if (DEFAULT_ABI == ABI_V4)
2634     {
2635       /* Indicate to allocate space on the stack for varargs save area.  */
2636       /* ??? Does this really have to be located at a magic spot on the
2637          stack, or can we allocate this with assign_stack_local instead.  */
2638       cfun->machine->sysv_varargs_p = 1;
2639       if (! no_rtl)
2640         save_area = plus_constant (virtual_stack_vars_rtx,
2641                                    - RS6000_VARARGS_SIZE);
2642
2643       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
2644     }
2645   else
2646     {
2647       first_reg_offset = next_cum.words;
2648       save_area = virtual_incoming_args_rtx;
2649       cfun->machine->sysv_varargs_p = 0;
2650
2651       if (MUST_PASS_IN_STACK (mode, type))
2652         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
2653     }
2654
2655   set = get_varargs_alias_set ();
2656   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
2657     {
2658       mem = gen_rtx_MEM (BLKmode,
2659                          plus_constant (save_area,
2660                                         first_reg_offset * reg_size)),
2661       set_mem_alias_set (mem, set);
2662       set_mem_align (mem, BITS_PER_WORD);
2663
2664       move_block_from_reg
2665         (GP_ARG_MIN_REG + first_reg_offset, mem,
2666          GP_ARG_NUM_REG - first_reg_offset,
2667          (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD);
2668
2669       /* ??? Does ABI_V4 need this at all?  */
2670       *pretend_size = (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD;
2671     }
2672
2673   /* Save FP registers if needed.  */
2674   if (DEFAULT_ABI == ABI_V4
2675       && TARGET_HARD_FLOAT && ! no_rtl
2676       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
2677     {
2678       int fregno = next_cum.fregno;
2679       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
2680       rtx lab = gen_label_rtx ();
2681       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
2682
2683       emit_jump_insn (gen_rtx_SET (VOIDmode,
2684                                    pc_rtx,
2685                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
2686                                             gen_rtx_NE (VOIDmode, cr1,
2687                                                         const0_rtx),
2688                                             gen_rtx_LABEL_REF (VOIDmode, lab),
2689                                             pc_rtx)));
2690
2691       while (fregno <= FP_ARG_V4_MAX_REG)
2692         {
2693           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
2694           set_mem_alias_set (mem, set);
2695           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
2696           fregno++;
2697           off += 8;
2698         }
2699
2700       emit_label (lab);
2701     }
2702 }
2703
2704 /* Create the va_list data type.  */
2705
2706 tree
2707 rs6000_build_va_list ()
2708 {
2709   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
2710
2711   /* For AIX, prefer 'char *' because that's what the system
2712      header files like.  */
2713   if (DEFAULT_ABI != ABI_V4)
2714     return build_pointer_type (char_type_node);
2715
2716   record = make_lang_type (RECORD_TYPE);
2717   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2718
2719   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
2720                       unsigned_char_type_node);
2721   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
2722                       unsigned_char_type_node);
2723   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
2724                       ptr_type_node);
2725   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
2726                       ptr_type_node);
2727
2728   DECL_FIELD_CONTEXT (f_gpr) = record;
2729   DECL_FIELD_CONTEXT (f_fpr) = record;
2730   DECL_FIELD_CONTEXT (f_ovf) = record;
2731   DECL_FIELD_CONTEXT (f_sav) = record;
2732
2733   TREE_CHAIN (record) = type_decl;
2734   TYPE_NAME (record) = type_decl;
2735   TYPE_FIELDS (record) = f_gpr;
2736   TREE_CHAIN (f_gpr) = f_fpr;
2737   TREE_CHAIN (f_fpr) = f_ovf;
2738   TREE_CHAIN (f_ovf) = f_sav;
2739
2740   layout_type (record);
2741
2742   /* The correct type is an array type of one element.  */
2743   return build_array_type (record, build_index_type (size_zero_node));
2744 }
2745
2746 /* Implement va_start.  */
2747
2748 void
2749 rs6000_va_start (stdarg_p, valist, nextarg)
2750      int stdarg_p;
2751      tree valist;
2752      rtx nextarg;
2753 {
2754   HOST_WIDE_INT words, n_gpr, n_fpr;
2755   tree f_gpr, f_fpr, f_ovf, f_sav;
2756   tree gpr, fpr, ovf, sav, t;
2757
2758   /* Only SVR4 needs something special.  */
2759   if (DEFAULT_ABI != ABI_V4)
2760     {
2761       std_expand_builtin_va_start (stdarg_p, valist, nextarg);
2762       return;
2763     }
2764
2765   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2766   f_fpr = TREE_CHAIN (f_gpr);
2767   f_ovf = TREE_CHAIN (f_fpr);
2768   f_sav = TREE_CHAIN (f_ovf);
2769
2770   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2771   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2772   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2773   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2774   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2775
2776   /* Count number of gp and fp argument registers used.  */
2777   words = current_function_args_info.words;
2778   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
2779   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
2780
2781   if (TARGET_DEBUG_ARG)
2782     {
2783       fputs ("va_start: words = ", stderr);
2784       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, words);
2785       fputs (", n_gpr = ", stderr);
2786       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_gpr);
2787       fputs (", n_fpr = ", stderr);
2788       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_fpr);
2789       putc ('\n', stderr);
2790     }
2791
2792   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
2793   TREE_SIDE_EFFECTS (t) = 1;
2794   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2795
2796   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
2797   TREE_SIDE_EFFECTS (t) = 1;
2798   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2799
2800   /* Find the overflow area.  */
2801   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
2802   if (words != 0)
2803     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
2804                build_int_2 (words * UNITS_PER_WORD, 0));
2805   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2806   TREE_SIDE_EFFECTS (t) = 1;
2807   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2808
2809   /* Find the register save area.  */
2810   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
2811   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
2812              build_int_2 (-RS6000_VARARGS_SIZE, -1));
2813   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2814   TREE_SIDE_EFFECTS (t) = 1;
2815   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2816 }
2817
2818 /* Implement va_arg.  */
2819
2820 rtx
2821 rs6000_va_arg (valist, type)
2822      tree valist, type;
2823 {
2824   tree f_gpr, f_fpr, f_ovf, f_sav;
2825   tree gpr, fpr, ovf, sav, reg, t, u;
2826   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
2827   rtx lab_false, lab_over, addr_rtx, r;
2828
2829   /* For AIX, the rule is that structures are passed left-aligned in
2830      their stack slot.  However, GCC does not presently do this:
2831      structures which are the same size as integer types are passed
2832      right-aligned, as if they were in fact integers.  This only
2833      matters for structures of size 1 or 2, or 4 when TARGET_64BIT.  */
2834   if (DEFAULT_ABI != ABI_V4)
2835     {
2836       HOST_WIDE_INT align, rounded_size;
2837       enum machine_mode mode;
2838       tree addr_tree;
2839
2840       /* Compute the rounded size of the type.  */
2841       align = PARM_BOUNDARY / BITS_PER_UNIT;
2842       rounded_size = (((int_size_in_bytes (type) + align - 1) / align)
2843                       * align);
2844
2845       addr_tree = valist;
2846
2847       mode = TYPE_MODE (type);
2848       if (mode != BLKmode)
2849         {
2850           HOST_WIDE_INT adj;
2851           adj = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_UNIT;
2852           if (rounded_size > align)
2853             adj = rounded_size;
2854           
2855           addr_tree = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
2856                              build_int_2 (rounded_size - adj, 0));
2857         }
2858
2859       addr_rtx = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2860       addr_rtx = copy_to_reg (addr_rtx);
2861       
2862       /* Compute new value for AP.  */
2863       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
2864                  build (PLUS_EXPR, TREE_TYPE (valist), valist,
2865                         build_int_2 (rounded_size, 0)));
2866       TREE_SIDE_EFFECTS (t) = 1;
2867       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2868       
2869       return addr_rtx;
2870     }
2871
2872   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2873   f_fpr = TREE_CHAIN (f_gpr);
2874   f_ovf = TREE_CHAIN (f_fpr);
2875   f_sav = TREE_CHAIN (f_ovf);
2876
2877   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2878   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2879   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2880   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2881   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2882
2883   size = int_size_in_bytes (type);
2884   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2885
2886   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
2887     {
2888       /* Aggregates and long doubles are passed by reference.  */
2889       indirect_p = 1;
2890       reg = gpr;
2891       n_reg = 1;
2892       sav_ofs = 0;
2893       sav_scale = 4;
2894       size = rsize = UNITS_PER_WORD;
2895     }
2896   else if (FLOAT_TYPE_P (type) && ! TARGET_SOFT_FLOAT)
2897     {
2898       /* FP args go in FP registers, if present.  */
2899       indirect_p = 0;
2900       reg = fpr;
2901       n_reg = 1;
2902       sav_ofs = 8*4;
2903       sav_scale = 8;
2904     }
2905   else
2906     {
2907       /* Otherwise into GP registers.  */
2908       indirect_p = 0;
2909       reg = gpr;
2910       n_reg = rsize;
2911       sav_ofs = 0;
2912       sav_scale = 4;
2913     }
2914
2915   /* Pull the value out of the saved registers ...  */
2916
2917   lab_false = gen_label_rtx ();
2918   lab_over = gen_label_rtx ();
2919   addr_rtx = gen_reg_rtx (Pmode);
2920
2921   emit_cmp_and_jump_insns (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
2922                            GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
2923                            lab_false);
2924
2925   /* Long long is aligned in the registers.  */
2926   if (n_reg > 1)
2927     {
2928       u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
2929                  build_int_2 (n_reg - 1, 0));
2930       u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
2931       u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
2932       TREE_SIDE_EFFECTS (u) = 1;
2933       expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
2934     }
2935
2936   if (sav_ofs)
2937     t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
2938   else
2939     t = sav;
2940
2941   u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg, build_int_2 (n_reg, 0));
2942   TREE_SIDE_EFFECTS (u) = 1;
2943
2944   u = build1 (CONVERT_EXPR, integer_type_node, u);
2945   TREE_SIDE_EFFECTS (u) = 1;
2946
2947   u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
2948   TREE_SIDE_EFFECTS (u) = 1;
2949
2950   t = build (PLUS_EXPR, ptr_type_node, t, u);
2951   TREE_SIDE_EFFECTS (t) = 1;
2952
2953   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
2954   if (r != addr_rtx)
2955     emit_move_insn (addr_rtx, r);
2956
2957   emit_jump_insn (gen_jump (lab_over));
2958   emit_barrier ();
2959   emit_label (lab_false);
2960
2961   /* ... otherwise out of the overflow area.  */
2962
2963   /* Make sure we don't find reg 7 for the next int arg.  */
2964   if (n_reg > 1)
2965     {
2966       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
2967       TREE_SIDE_EFFECTS (t) = 1;
2968       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2969     }
2970
2971   /* Care for on-stack alignment if needed.  */
2972   if (rsize <= 1)
2973     t = ovf;
2974   else
2975     {
2976       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (7, 0));
2977       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-8, -1));
2978     }
2979   t = save_expr (t);
2980
2981   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
2982   if (r != addr_rtx)
2983     emit_move_insn (addr_rtx, r);
2984
2985   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
2986   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2987   TREE_SIDE_EFFECTS (t) = 1;
2988   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2989
2990   emit_label (lab_over);
2991
2992   if (indirect_p)
2993     {
2994       r = gen_rtx_MEM (Pmode, addr_rtx);
2995       set_mem_alias_set (r, get_varargs_alias_set ());
2996       emit_move_insn (addr_rtx, r);
2997     }
2998
2999   return addr_rtx;
3000 }
3001
3002 /* Builtins.  */
3003
3004 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
3005 do {                                                                    \
3006   if ((MASK) & target_flags)                                            \
3007     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL);       \
3008 } while (0)
3009
3010 struct builtin_description
3011 {
3012   const unsigned int mask;
3013   const enum insn_code icode;
3014   const char *const name;
3015   const enum rs6000_builtins code;
3016 };
3017 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc) */
3018 static const struct builtin_description bdesc_3arg[] =
3019   {
3020     { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
3021     { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
3022     { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
3023     { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
3024     { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
3025     { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
3026     { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
3027     { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
3028     { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
3029     { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
3030     { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
3031     { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
3032     { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
3033     { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
3034     { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
3035     { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
3036     { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
3037     { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
3038     { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
3039   };
3040
3041 /* Simple binary operations: VECc = foo (VECa, VECb).  */
3042 static const struct builtin_description bdesc_2arg[] =
3043 {
3044   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
3045   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
3046   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
3047   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
3048   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
3049   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
3050   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
3051   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
3052   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
3053   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
3054   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
3055   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
3056   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
3057   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
3058   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
3059   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
3060   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
3061   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
3062   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
3063   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
3064   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
3065   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
3066   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
3067   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
3068   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
3069   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
3070   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
3071   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
3072   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
3073   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
3074   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
3075   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
3076   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
3077   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
3078   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
3079   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
3080   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
3081   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
3082   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
3083   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
3084   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
3085   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
3086   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
3087   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
3088   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
3089   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
3090   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
3091   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
3092   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
3093   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
3094   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
3095   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
3096   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
3097   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
3098   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
3099   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
3100   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
3101   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
3102   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
3103   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
3104   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
3105   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
3106   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
3107   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
3108   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
3109   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
3110   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
3111   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
3112   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
3113   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
3114   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
3115   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
3116   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
3117   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
3118   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
3119   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
3120   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
3121   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
3122   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
3123   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
3124   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
3125   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
3126   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
3127   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
3128   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
3129   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
3130   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
3131   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
3132   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
3133   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
3134   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
3135   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
3136   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
3137   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
3138   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
3139   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
3140   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
3141   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
3142   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
3143   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
3144   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
3145   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
3146   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
3147   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
3148   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
3149   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
3150   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
3151   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
3152   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
3153   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
3154   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
3155   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
3156   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
3157 };
3158 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
3159    foo (VECa).  */
3160 static const struct builtin_description bdesc_1arg[] =
3161 {
3162   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
3163   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
3164   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
3165   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
3166   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
3167   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
3168   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
3169   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
3170   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
3171   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
3172   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
3173 };
3174
3175 static rtx
3176 altivec_expand_unop_builtin (icode, arglist, target)
3177      enum insn_code icode;
3178      tree arglist;
3179      rtx target;
3180 {
3181   rtx pat;
3182   tree arg0 = TREE_VALUE (arglist);
3183   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3184   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3185   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3186
3187   if (! target
3188       || GET_MODE (target) != tmode
3189       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3190     target = gen_reg_rtx (tmode);
3191
3192   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3193     op0 = copy_to_mode_reg (mode0, op0);
3194
3195   pat = GEN_FCN (icode) (target, op0);
3196   if (! pat)
3197     return 0;
3198   emit_insn (pat);
3199
3200   return target;
3201 }
3202 static rtx
3203 altivec_expand_binop_builtin (icode, arglist, target)
3204      enum insn_code icode;
3205      tree arglist;
3206      rtx target;
3207 {
3208   rtx pat;
3209   tree arg0 = TREE_VALUE (arglist);
3210   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3211   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3212   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3213   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3214   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3215   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3216
3217   if (! target
3218       || GET_MODE (target) != tmode
3219       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3220     target = gen_reg_rtx (tmode);
3221
3222   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3223     op0 = copy_to_mode_reg (mode0, op0);
3224   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3225     op1 = copy_to_mode_reg (mode1, op1);
3226
3227   pat = GEN_FCN (icode) (target, op0, op1);
3228   if (! pat)
3229     return 0;
3230   emit_insn (pat);
3231
3232   return target;
3233 }
3234 static rtx
3235 altivec_expand_ternop_builtin (icode, arglist, target)
3236      enum insn_code icode;
3237      tree arglist;
3238      rtx target;
3239 {
3240   rtx pat;
3241   tree arg0 = TREE_VALUE (arglist);
3242   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3243   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3244   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3245   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3246   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3247   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3248   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3249   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3250   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
3251
3252   if (! target
3253       || GET_MODE (target) != tmode
3254       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3255     target = gen_reg_rtx (tmode);
3256
3257   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3258     op0 = copy_to_mode_reg (mode0, op0);
3259   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3260     op1 = copy_to_mode_reg (mode1, op1);
3261   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
3262     op2 = copy_to_mode_reg (mode2, op2);
3263
3264   pat = GEN_FCN (icode) (target, op0, op1, op2);
3265   if (! pat)
3266     return 0;
3267   emit_insn (pat);
3268
3269   return target;
3270 }
3271 static rtx
3272 altivec_expand_builtin (exp, target)
3273      tree exp;
3274      rtx target;
3275 {
3276   struct builtin_description *d;
3277   size_t i;
3278   enum insn_code icode;
3279   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3280   tree arglist = TREE_OPERAND (exp, 1);
3281   tree arg0, arg1;
3282   rtx op0, op1, pat;
3283   enum machine_mode tmode, mode0, mode1;
3284   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3285   
3286   switch (fcode)
3287     {
3288     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
3289       icode = CODE_FOR_altivec_lvx_16qi;
3290       arg0 = TREE_VALUE (arglist);
3291       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3292       tmode = insn_data[icode].operand[0].mode;
3293       mode0 = insn_data[icode].operand[1].mode;
3294
3295       if (! target
3296           || GET_MODE (target) != tmode
3297           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3298         target = gen_reg_rtx (tmode);
3299
3300       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3301         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3302
3303       pat = GEN_FCN (icode) (target, op0);
3304       if (! pat)
3305         return 0;
3306       emit_insn (pat);
3307       return target;
3308     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
3309       icode = CODE_FOR_altivec_lvx_8hi;
3310       arg0 = TREE_VALUE (arglist);
3311       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3312       tmode = insn_data[icode].operand[0].mode;
3313       mode0 = insn_data[icode].operand[1].mode;
3314
3315       if (! target
3316           || GET_MODE (target) != tmode
3317           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3318         target = gen_reg_rtx (tmode);
3319
3320       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3321         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3322
3323       pat = GEN_FCN (icode) (target, op0);
3324       if (! pat)
3325         return 0;
3326       emit_insn (pat);
3327       return target;
3328     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
3329       icode = CODE_FOR_altivec_lvx_4si;
3330       arg0 = TREE_VALUE (arglist);
3331       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3332       tmode = insn_data[icode].operand[0].mode;
3333       mode0 = insn_data[icode].operand[1].mode;
3334
3335       if (! target
3336           || GET_MODE (target) != tmode
3337           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3338         target = gen_reg_rtx (tmode);
3339
3340       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3341         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3342
3343       pat = GEN_FCN (icode) (target, op0);
3344       if (! pat)
3345         return 0;
3346       emit_insn (pat);
3347       return target;
3348     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
3349       icode = CODE_FOR_altivec_lvx_4sf;
3350       arg0 = TREE_VALUE (arglist);
3351       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3352       tmode = insn_data[icode].operand[0].mode;
3353       mode0 = insn_data[icode].operand[1].mode;
3354
3355       if (! target
3356           || GET_MODE (target) != tmode
3357           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3358         target = gen_reg_rtx (tmode);
3359
3360       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3361         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3362
3363       pat = GEN_FCN (icode) (target, op0);
3364       if (! pat)
3365         return 0;
3366       emit_insn (pat);
3367       return target;
3368
3369     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
3370       icode = CODE_FOR_altivec_stvx_16qi;
3371       arg0 = TREE_VALUE (arglist);
3372       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3373       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3374       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3375       mode0 = insn_data[icode].operand[0].mode;
3376       mode1 = insn_data[icode].operand[1].mode;
3377
3378       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3379         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3380       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3381         op1 = copy_to_mode_reg (mode1, op1);
3382
3383       pat = GEN_FCN (icode) (op0, op1);
3384       if (! pat)
3385         return 0;
3386       emit_insn (pat);
3387       return NULL_RTX;
3388     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
3389       icode = CODE_FOR_altivec_stvx_8hi;
3390       arg0 = TREE_VALUE (arglist);
3391       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3392       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3393       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3394       mode0 = insn_data[icode].operand[0].mode;
3395       mode1 = insn_data[icode].operand[1].mode;
3396
3397       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3398         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3399       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3400         op1 = copy_to_mode_reg (mode1, op1);
3401
3402       pat = GEN_FCN (icode) (op0, op1);
3403       if (! pat)
3404         return 0;
3405       emit_insn (pat);
3406       return NULL_RTX;
3407     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
3408       icode = CODE_FOR_altivec_stvx_4si;
3409       arg0 = TREE_VALUE (arglist);
3410       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3411       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3412       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3413       mode0 = insn_data[icode].operand[0].mode;
3414       mode1 = insn_data[icode].operand[1].mode;
3415
3416       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3417         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3418       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3419         op1 = copy_to_mode_reg (mode1, op1);
3420
3421       pat = GEN_FCN (icode) (op0, op1);
3422       if (! pat)
3423         return 0;
3424       emit_insn (pat);
3425       return NULL_RTX;
3426     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
3427       icode = CODE_FOR_altivec_stvx_4sf;
3428       arg0 = TREE_VALUE (arglist);
3429       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3430       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3431       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3432       mode0 = insn_data[icode].operand[0].mode;
3433       mode1 = insn_data[icode].operand[1].mode;
3434
3435       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3436         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3437       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3438         op1 = copy_to_mode_reg (mode1, op1);
3439
3440       pat = GEN_FCN (icode) (op0, op1);
3441       if (! pat)
3442         return 0;
3443       emit_insn (pat);
3444       return NULL_RTX;
3445     }
3446   /* Handle simple unary operations.  */
3447   d = (struct builtin_description *) bdesc_1arg;
3448   for (i = 0; i < sizeof (bdesc_1arg) / sizeof *d; i++, d++)
3449     if (d->code == fcode)
3450       return altivec_expand_unop_builtin (d->icode, arglist, target);
3451
3452   /* Handle simple binary operations.  */
3453   d = (struct builtin_description *) bdesc_2arg;
3454   for (i = 0; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
3455     if (d->code == fcode)
3456       return altivec_expand_binop_builtin (d->icode, arglist, target);
3457
3458   /* Handle simple ternary operations.  */
3459   d = (struct builtin_description *) bdesc_3arg;
3460   for (i = 0; i < sizeof  (bdesc_3arg) / sizeof *d; i++, d++)
3461     if (d->code == fcode)
3462       return altivec_expand_ternop_builtin (d->icode, arglist, target);
3463
3464   abort ();
3465   return NULL_RTX;
3466 }
3467
3468 /* Expand an expression EXP that calls a built-in function,
3469    with result going to TARGET if that's convenient
3470    (and in mode MODE if that's convenient).
3471    SUBTARGET may be used as the target for computing one of EXP's operands.
3472    IGNORE is nonzero if the value is to be ignored.  */
3473
3474 static rtx
3475 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
3476      tree exp;
3477      rtx target;
3478      rtx subtarget ATTRIBUTE_UNUSED;
3479      enum machine_mode mode ATTRIBUTE_UNUSED;
3480      int ignore ATTRIBUTE_UNUSED;
3481 {
3482   if (TARGET_ALTIVEC)
3483     return altivec_expand_builtin (exp, target);
3484
3485   abort ();
3486 }
3487
3488 static void
3489 rs6000_init_builtins ()
3490 {
3491   if (TARGET_ALTIVEC)
3492     altivec_init_builtins ();
3493 }
3494
3495 static void
3496 altivec_init_builtins (void)
3497 {
3498   struct builtin_description * d;
3499   size_t i;
3500
3501   tree endlink = void_list_node;
3502
3503   tree pint_type_node = build_pointer_type (integer_type_node);
3504   tree pshort_type_node = build_pointer_type (short_integer_type_node);
3505   tree pchar_type_node = build_pointer_type (char_type_node);
3506   tree pfloat_type_node = build_pointer_type (float_type_node);
3507   tree v4sf_ftype_v4sf_v4sf_v16qi
3508     = build_function_type (V4SF_type_node,
3509                            tree_cons (NULL_TREE, V4SF_type_node,
3510                                       tree_cons (NULL_TREE, V4SF_type_node,
3511                                                  tree_cons (NULL_TREE, 
3512                                                             V16QI_type_node,
3513                                                             endlink))));
3514   tree v4si_ftype_v4si_v4si_v16qi
3515     = build_function_type (V4SI_type_node,
3516                            tree_cons (NULL_TREE, V4SI_type_node,
3517                                       tree_cons (NULL_TREE, V4SI_type_node,
3518                                                  tree_cons (NULL_TREE, 
3519                                                             V16QI_type_node,
3520                                                             endlink))));
3521   tree v8hi_ftype_v8hi_v8hi_v16qi
3522     = build_function_type (V8HI_type_node,
3523                            tree_cons (NULL_TREE, V8HI_type_node,
3524                                       tree_cons (NULL_TREE, V8HI_type_node,
3525                                                  tree_cons (NULL_TREE, 
3526                                                             V16QI_type_node,
3527                                                             endlink))));
3528   tree v16qi_ftype_v16qi_v16qi_v16qi
3529     = build_function_type (V16QI_type_node,
3530                            tree_cons (NULL_TREE, V16QI_type_node,
3531                                       tree_cons (NULL_TREE, V16QI_type_node,
3532                                                  tree_cons (NULL_TREE, 
3533                                                             V16QI_type_node,
3534                                                             endlink))));
3535
3536   /* V4SI foo (char) */
3537   tree v4si_ftype_char
3538     = build_function_type (V4SI_type_node,
3539                            tree_cons (NULL_TREE, char_type_node, endlink));
3540
3541   /* V8HI foo (char) */
3542   tree v8hi_ftype_char
3543     = build_function_type (V8HI_type_node,
3544                            tree_cons (NULL_TREE, char_type_node, endlink));
3545
3546   /* V16QI foo (char) */
3547   tree v16qi_ftype_char
3548     = build_function_type (V16QI_type_node,
3549                            tree_cons (NULL_TREE, char_type_node, endlink));
3550   /* V4SF foo (V4SF) */
3551   tree v4sf_ftype_v4sf
3552     = build_function_type (V4SF_type_node,
3553                            tree_cons (NULL_TREE, V4SF_type_node, endlink));
3554
3555   /* V4SI foo (int *).  */
3556   tree v4si_ftype_pint
3557     = build_function_type (V4SI_type_node,
3558                            tree_cons (NULL_TREE, pint_type_node, endlink));
3559   /* V8HI foo (short *).  */
3560   tree v8hi_ftype_pshort
3561     = build_function_type (V8HI_type_node,
3562                            tree_cons (NULL_TREE, pshort_type_node, endlink));
3563   /* V16QI foo (char *).  */
3564   tree v16qi_ftype_pchar
3565     = build_function_type (V16QI_type_node,
3566                            tree_cons (NULL_TREE, pchar_type_node, endlink));
3567   /* V4SF foo (float *).  */
3568   tree v4sf_ftype_pfloat
3569     = build_function_type (V4SF_type_node,
3570                            tree_cons (NULL_TREE, pfloat_type_node, endlink));
3571
3572   /* void foo (int *, V4SI).  */
3573   tree void_ftype_pint_v4si
3574     = build_function_type (void_type_node,
3575                            tree_cons (NULL_TREE, pint_type_node,
3576                                       tree_cons (NULL_TREE, V4SI_type_node,
3577                                                  endlink)));
3578   /* void foo (short *, V8HI).  */
3579   tree void_ftype_pshort_v8hi
3580     = build_function_type (void_type_node,
3581                            tree_cons (NULL_TREE, pshort_type_node,
3582                                       tree_cons (NULL_TREE, V8HI_type_node,
3583                                                  endlink)));
3584   /* void foo (char *, V16QI).  */
3585   tree void_ftype_pchar_v16qi
3586     = build_function_type (void_type_node,
3587                            tree_cons (NULL_TREE, pchar_type_node,
3588                                       tree_cons (NULL_TREE, V16QI_type_node,
3589                                                  endlink)));
3590   /* void foo (float *, V4SF).  */
3591   tree void_ftype_pfloat_v4sf
3592     = build_function_type (void_type_node,
3593                            tree_cons (NULL_TREE, pfloat_type_node,
3594                                       tree_cons (NULL_TREE, V4SF_type_node,
3595                                                  endlink)));
3596
3597   tree v4si_ftype_v4si_v4si
3598     = build_function_type (V4SI_type_node,
3599                            tree_cons (NULL_TREE, V4SI_type_node,
3600                                       tree_cons (NULL_TREE, V4SI_type_node,
3601                                                  endlink)));
3602  /* These are really for the unsigned 5 bit literals */ 
3603   tree v4sf_ftype_v4si_char
3604     = build_function_type (V4SF_type_node,
3605                            tree_cons (NULL_TREE, V4SI_type_node,
3606                                       tree_cons (NULL_TREE, char_type_node,
3607                                                  endlink)));
3608   tree v4si_ftype_v4sf_char
3609     = build_function_type (V4SI_type_node,
3610                            tree_cons (NULL_TREE, V4SF_type_node,
3611                                       tree_cons (NULL_TREE, char_type_node,
3612                                                  endlink)));
3613   tree v4si_ftype_v4si_char
3614     = build_function_type (V4SI_type_node,
3615                            tree_cons (NULL_TREE, V4SI_type_node,
3616                                       tree_cons (NULL_TREE, char_type_node,
3617                                                  endlink)));
3618   tree v8hi_ftype_v8hi_char
3619     = build_function_type (V8HI_type_node,
3620                            tree_cons (NULL_TREE, V8HI_type_node,
3621                                       tree_cons (NULL_TREE, char_type_node,
3622                                                  endlink)));
3623   tree v16qi_ftype_v16qi_char
3624     = build_function_type (V16QI_type_node,
3625                            tree_cons (NULL_TREE, V16QI_type_node,
3626                                       tree_cons (NULL_TREE, char_type_node,
3627                                                  endlink)));
3628
3629   tree v4sf_ftype_v4sf_v4sf
3630     = build_function_type (V4SF_type_node,
3631                            tree_cons (NULL_TREE, V4SF_type_node,
3632                                       tree_cons (NULL_TREE, V4SF_type_node,
3633                                                  endlink)));
3634   tree v4sf_ftype_v4sf_v4sf_v4si
3635     = build_function_type (V4SF_type_node,
3636                            tree_cons (NULL_TREE, V4SF_type_node,
3637                                       tree_cons (NULL_TREE, V4SF_type_node,
3638                                                  tree_cons (NULL_TREE,
3639                                                             V4SI_type_node,
3640                                                             endlink))));
3641   tree v4sf_ftype_v4sf_v4sf_v4sf
3642     = build_function_type (V4SF_type_node,
3643                            tree_cons (NULL_TREE, V4SF_type_node,
3644                                       tree_cons (NULL_TREE, V4SF_type_node,
3645                                                  tree_cons (NULL_TREE, 
3646                                                             V4SF_type_node,
3647                                                             endlink))));
3648   tree v4si_ftype_v4si_v4si_v4si 
3649     = build_function_type (V4SI_type_node,
3650                            tree_cons (NULL_TREE, V4SI_type_node,
3651                                       tree_cons (NULL_TREE, V4SI_type_node,
3652                                                  tree_cons (NULL_TREE,
3653                                                             V4SI_type_node,
3654                                                             endlink))));
3655
3656   tree v8hi_ftype_v8hi_v8hi
3657     = build_function_type (V8HI_type_node,
3658                            tree_cons (NULL_TREE, V8HI_type_node,
3659                                       tree_cons (NULL_TREE, V8HI_type_node,
3660                                                  endlink)));
3661   tree v8hi_ftype_v8hi_v8hi_v8hi
3662     = build_function_type (V8HI_type_node,
3663                            tree_cons (NULL_TREE, V8HI_type_node,
3664                                       tree_cons (NULL_TREE, V8HI_type_node,
3665                                                  tree_cons (NULL_TREE, 
3666                                                             V8HI_type_node,
3667                                                             endlink))));
3668  tree v4si_ftype_v8hi_v8hi_v4si
3669     = build_function_type (V4SI_type_node,
3670                            tree_cons (NULL_TREE, V8HI_type_node,
3671                                       tree_cons (NULL_TREE, V8HI_type_node,
3672                                                  tree_cons (NULL_TREE,
3673                                                             V4SI_type_node,
3674                                                             endlink))));
3675  tree v4si_ftype_v16qi_v16qi_v4si
3676     = build_function_type (V4SI_type_node,
3677                            tree_cons (NULL_TREE, V16QI_type_node,
3678                                       tree_cons (NULL_TREE, V16QI_type_node,
3679                                                  tree_cons (NULL_TREE,
3680                                                             V4SI_type_node,
3681                                                             endlink))));
3682   
3683   tree v16qi_ftype_v16qi_v16qi
3684     = build_function_type (V16QI_type_node,
3685                            tree_cons (NULL_TREE, V16QI_type_node,
3686                                       tree_cons (NULL_TREE, V16QI_type_node,
3687                                                  endlink)));
3688   
3689   tree v4si_ftype_v4sf_v4sf
3690     = build_function_type (V4SI_type_node,
3691                            tree_cons (NULL_TREE, V4SF_type_node,
3692                                       tree_cons (NULL_TREE, V4SF_type_node,
3693                                                  endlink)));
3694
3695   tree v8hi_ftype_v16qi_v16qi
3696     = build_function_type (V8HI_type_node,
3697                            tree_cons (NULL_TREE, V16QI_type_node,
3698                                       tree_cons (NULL_TREE, V16QI_type_node,
3699                                                  endlink)));
3700
3701   tree v4si_ftype_v8hi_v8hi
3702     = build_function_type (V4SI_type_node,
3703                            tree_cons (NULL_TREE, V8HI_type_node,
3704                                       tree_cons (NULL_TREE, V8HI_type_node,
3705                                                  endlink)));
3706
3707   tree v8hi_ftype_v4si_v4si
3708     = build_function_type (V8HI_type_node,
3709                            tree_cons (NULL_TREE, V4SI_type_node,
3710                                       tree_cons (NULL_TREE, V4SI_type_node,
3711                                                  endlink)));
3712
3713   tree v16qi_ftype_v8hi_v8hi
3714     = build_function_type (V16QI_type_node,
3715                            tree_cons (NULL_TREE, V8HI_type_node,
3716                                       tree_cons (NULL_TREE, V8HI_type_node,
3717                                                  endlink)));
3718
3719   tree v4si_ftype_v16qi_v4si
3720     = build_function_type (V4SI_type_node,
3721                            tree_cons (NULL_TREE, V16QI_type_node,
3722                                       tree_cons (NULL_TREE, V4SI_type_node,
3723                                                  endlink)));
3724
3725   tree v4si_ftype_v8hi_v4si
3726     = build_function_type (V4SI_type_node,
3727                            tree_cons (NULL_TREE, V8HI_type_node,
3728                                       tree_cons (NULL_TREE, V4SI_type_node,
3729                                                  endlink)));
3730
3731   tree int_ftype_v4si_v4si
3732     = build_function_type (integer_type_node,
3733                            tree_cons (NULL_TREE, V4SI_type_node,
3734                                       tree_cons (NULL_TREE, V4SI_type_node,
3735                                                  endlink)));
3736
3737   tree int_ftype_v4sf_v4sf
3738     = build_function_type (integer_type_node,
3739                            tree_cons (NULL_TREE, V4SF_type_node,
3740                                       tree_cons (NULL_TREE, V4SF_type_node,
3741                                                  endlink)));
3742
3743   tree int_ftype_v16qi_v16qi
3744     = build_function_type (integer_type_node,
3745                            tree_cons (NULL_TREE, V16QI_type_node,
3746                                       tree_cons (NULL_TREE, V16QI_type_node,
3747                                                  endlink)));
3748
3749   tree int_ftype_v8hi_v8hi
3750     = build_function_type (integer_type_node,
3751                            tree_cons (NULL_TREE, V8HI_type_node,
3752                                       tree_cons (NULL_TREE, V8HI_type_node,
3753                                                  endlink)));
3754
3755   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pfloat, ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
3756   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf, ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
3757   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pint, ALTIVEC_BUILTIN_LD_INTERNAL_4si);
3758   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si, ALTIVEC_BUILTIN_ST_INTERNAL_4si);
3759   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pshort, ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
3760   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi, ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
3761   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pchar, ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
3762   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi, ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
3763
3764   /* Add the simple ternary operators. */
3765   d = (struct builtin_description *) bdesc_3arg;
3766   for (i = 0; i < sizeof (bdesc_3arg) / sizeof *d; i++, d++)
3767     {
3768       
3769       enum machine_mode mode0, mode1, mode2, mode3;
3770       tree type;
3771
3772       if (d->name == 0)
3773         continue;
3774       
3775       mode0 = insn_data[d->icode].operand[0].mode;
3776       mode1 = insn_data[d->icode].operand[1].mode;
3777       mode2 = insn_data[d->icode].operand[2].mode;
3778       mode3 = insn_data[d->icode].operand[3].mode;
3779       
3780       /* When all four are of the same mode.  */
3781       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
3782         {
3783           switch (mode0)
3784             {
3785             case V4SImode:
3786               type = v4si_ftype_v4si_v4si_v4si;
3787               break;
3788             case V4SFmode:
3789               type = v4sf_ftype_v4sf_v4sf_v4sf;
3790               break;
3791             case V8HImode:
3792               type = v8hi_ftype_v8hi_v8hi_v8hi;
3793               break;          
3794             case V16QImode:
3795               type = v16qi_ftype_v16qi_v16qi_v16qi;
3796               break;          
3797             default:
3798               abort();        
3799             }
3800         }
3801       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
3802         {
3803           switch (mode0)
3804             {
3805             case V4SImode:
3806               type = v4si_ftype_v4si_v4si_v16qi;
3807               break;
3808             case V4SFmode:
3809               type = v4sf_ftype_v4sf_v4sf_v16qi;
3810               break;
3811             case V8HImode:
3812               type = v8hi_ftype_v8hi_v8hi_v16qi;
3813               break;          
3814             case V16QImode:
3815               type = v16qi_ftype_v16qi_v16qi_v16qi;
3816               break;          
3817             default:
3818               abort();        
3819             }
3820         }
3821       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
3822                && mode3 == V4SImode)
3823           type = v4si_ftype_v16qi_v16qi_v4si;
3824       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
3825                && mode3 == V4SImode)
3826           type = v4si_ftype_v8hi_v8hi_v4si;
3827       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
3828                && mode3 == V4SImode)
3829           type = v4sf_ftype_v4sf_v4sf_v4si;
3830       else
3831         abort ();
3832
3833       def_builtin (d->mask, d->name, type, d->code);
3834     }
3835
3836   /* Add the simple binary operators.  */
3837   d = (struct builtin_description *) bdesc_2arg;
3838   for (i = 0; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
3839     {
3840       enum machine_mode mode0, mode1, mode2;
3841       tree type;
3842
3843       if (d->name == 0)
3844         continue;
3845       
3846       mode0 = insn_data[d->icode].operand[0].mode;
3847       mode1 = insn_data[d->icode].operand[1].mode;
3848       mode2 = insn_data[d->icode].operand[2].mode;
3849
3850       /* When all three operands are of the same mode.  */
3851       if (mode0 == mode1 && mode1 == mode2)
3852         {
3853           switch (mode0)
3854             {
3855             case V4SFmode:
3856               type = v4sf_ftype_v4sf_v4sf;
3857               break;
3858             case V4SImode:
3859               type = v4si_ftype_v4si_v4si;
3860               break;
3861             case V16QImode:
3862               type = v16qi_ftype_v16qi_v16qi;
3863               break;
3864             case V8HImode:
3865               type = v8hi_ftype_v8hi_v8hi;
3866               break;
3867             default:
3868               abort ();
3869             }
3870         }
3871
3872       /* A few other combos we really don't want to do manually.  */
3873
3874       /* vint, vfloat, vfloat.  */
3875       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
3876         type = v4si_ftype_v4sf_v4sf;
3877
3878       /* vshort, vchar, vchar.  */
3879       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
3880         type = v8hi_ftype_v16qi_v16qi;
3881
3882       /* vint, vshort, vshort.  */
3883       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
3884         type = v4si_ftype_v8hi_v8hi;
3885
3886       /* vshort, vint, vint.  */
3887       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
3888         type = v8hi_ftype_v4si_v4si;
3889
3890       /* vchar, vshort, vshort.  */
3891       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
3892         type = v16qi_ftype_v8hi_v8hi;
3893
3894       /* vint, vchar, vint.  */
3895       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
3896         type = v4si_ftype_v16qi_v4si;
3897
3898       /* vint, vshort, vint.  */
3899       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
3900         type = v4si_ftype_v8hi_v4si;
3901       
3902       /* vint, vint, 5 bit literal.  */
3903       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
3904         type = v4si_ftype_v4si_char;
3905       
3906       /* vshort, vshort, 5 bit literal.  */
3907       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
3908         type = v8hi_ftype_v8hi_char;
3909       
3910       /* vchar, vchar, 5 bit literal.  */
3911       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
3912         type = v16qi_ftype_v16qi_char;
3913
3914       /* vfloat, vint, 5 bit literal.  */
3915       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
3916         type = v4sf_ftype_v4si_char;
3917       
3918       /* vint, vfloat, 5 bit literal.  */
3919       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
3920         type = v4si_ftype_v4sf_char;
3921
3922       /* fixme: aldyh */
3923       /* int, x, x.  */
3924       else if (mode0 == SImode)
3925         {
3926           switch (mode1)
3927             {
3928             case V4SImode:
3929               type = int_ftype_v4si_v4si;
3930               break;
3931             case V4SFmode:
3932               type = int_ftype_v4sf_v4sf;
3933               break;
3934             case V16QImode:
3935               type = int_ftype_v16qi_v16qi;
3936               break;
3937             case V8HImode:
3938               type = int_ftype_v8hi_v8hi;
3939               break;
3940             default:
3941               abort ();
3942             }
3943         }
3944
3945       else
3946         abort ();
3947
3948       def_builtin (d->mask, d->name, type, d->code);
3949     }
3950   /* Add the simple unary operators.  */
3951   d = (struct builtin_description *) bdesc_1arg;
3952   for (i = 0; i < sizeof (bdesc_1arg) / sizeof *d; i++, d++)
3953     {
3954       enum machine_mode mode0, mode1;
3955       tree type;
3956
3957       if (d->name == 0)
3958         continue;
3959       
3960       mode0 = insn_data[d->icode].operand[0].mode;
3961       mode1 = insn_data[d->icode].operand[1].mode;
3962
3963       if (mode0 == V4SImode && mode1 == QImode)
3964         type = v4si_ftype_char;
3965       else if (mode0 == V8HImode && mode1 == QImode)
3966         type = v8hi_ftype_char;
3967       else if (mode0 == V16QImode && mode1 == QImode)
3968         type = v16qi_ftype_char;
3969       else if (mode0 == V4SFmode && mode1 == V4SFmode)
3970         type = v4sf_ftype_v4sf;
3971       else
3972         abort ();
3973
3974       def_builtin (d->mask, d->name, type, d->code);
3975     }
3976 }
3977
3978 \f
3979 /* Generate a memory reference for expand_block_move, copying volatile,
3980    and other bits from an original memory reference.  */
3981
3982 static rtx
3983 expand_block_move_mem (mode, addr, orig_mem)
3984      enum machine_mode mode;
3985      rtx addr;
3986      rtx orig_mem;
3987 {
3988   rtx mem = gen_rtx_MEM (mode, addr);
3989
3990   MEM_COPY_ATTRIBUTES (mem, orig_mem);
3991   return mem;
3992 }
3993
3994 /* Expand a block move operation, and return 1 if successful.  Return 0
3995    if we should let the compiler generate normal code.
3996
3997    operands[0] is the destination
3998    operands[1] is the source
3999    operands[2] is the length
4000    operands[3] is the alignment */
4001
4002 #define MAX_MOVE_REG 4
4003
4004 int
4005 expand_block_move (operands)
4006      rtx operands[];
4007 {
4008   rtx orig_dest = operands[0];
4009   rtx orig_src  = operands[1];
4010   rtx bytes_rtx = operands[2];
4011   rtx align_rtx = operands[3];
4012   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
4013   int align;
4014   int bytes;
4015   int offset;
4016   int num_reg;
4017   int i;
4018   rtx src_reg;
4019   rtx dest_reg;
4020   rtx src_addr;
4021   rtx dest_addr;
4022   rtx tmp_reg;
4023   rtx stores[MAX_MOVE_REG];
4024   int move_bytes;
4025
4026   /* If this is not a fixed size move, just call memcpy */
4027   if (! constp)
4028     return 0;
4029
4030   /* If this is not a fixed size alignment, abort */
4031   if (GET_CODE (align_rtx) != CONST_INT)
4032     abort ();
4033   align = INTVAL (align_rtx);
4034
4035   /* Anything to move? */
4036   bytes = INTVAL (bytes_rtx);
4037   if (bytes <= 0)
4038     return 1;
4039
4040   /* Don't support real large moves.  If string instructions are not used,
4041      then don't generate more than 8 loads.  */
4042   if (TARGET_STRING)
4043     {
4044       if (bytes > 8*4)
4045         return 0;
4046     }
4047   else if (! STRICT_ALIGNMENT)
4048     {
4049       if (TARGET_POWERPC64 && align >= 4)
4050         {
4051           if (bytes > 8*8)
4052             return 0;
4053         }
4054       else
4055         if (bytes > 8*4)
4056           return 0;
4057     }
4058   else if (bytes > 8*align)
4059     return 0;
4060
4061   /* Move the address into scratch registers.  */
4062   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
4063   src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));
4064
4065   if (TARGET_STRING)    /* string instructions are available */
4066     {
4067       for ( ; bytes > 0; bytes -= move_bytes)
4068         {
4069           if (bytes > 24                /* move up to 32 bytes at a time */
4070               && ! fixed_regs[5]
4071               && ! fixed_regs[6]
4072               && ! fixed_regs[7]
4073               && ! fixed_regs[8]
4074               && ! fixed_regs[9]
4075               && ! fixed_regs[10]
4076               && ! fixed_regs[11]
4077               && ! fixed_regs[12])
4078             {
4079               move_bytes = (bytes > 32) ? 32 : bytes;
4080               emit_insn (gen_movstrsi_8reg (expand_block_move_mem (BLKmode,
4081                                                                    dest_reg,
4082                                                                    orig_dest),
4083                                             expand_block_move_mem (BLKmode,
4084                                                                    src_reg,
4085                                                                    orig_src),
4086                                             GEN_INT ((move_bytes == 32)
4087                                                      ? 0 : move_bytes),
4088                                             align_rtx));
4089             }
4090           else if (bytes > 16   /* move up to 24 bytes at a time */
4091                    && ! fixed_regs[5]
4092                    && ! fixed_regs[6]
4093                    && ! fixed_regs[7]
4094                    && ! fixed_regs[8]
4095                    && ! fixed_regs[9]
4096                    && ! fixed_regs[10])
4097             {
4098               move_bytes = (bytes > 24) ? 24 : bytes;
4099               emit_insn (gen_movstrsi_6reg (expand_block_move_mem (BLKmode,
4100                                                                    dest_reg,
4101                                                                    orig_dest),
4102                                             expand_block_move_mem (BLKmode,
4103                                                                    src_reg,
4104                                                                    orig_src),
4105                                             GEN_INT (move_bytes),
4106                                             align_rtx));
4107             }
4108           else if (bytes > 8    /* move up to 16 bytes at a time */
4109                    && ! fixed_regs[5]
4110                    && ! fixed_regs[6]
4111                    && ! fixed_regs[7]
4112                    && ! fixed_regs[8])
4113             {
4114               move_bytes = (bytes > 16) ? 16 : bytes;
4115               emit_insn (gen_movstrsi_4reg (expand_block_move_mem (BLKmode,
4116                                                                    dest_reg,
4117                                                                    orig_dest),
4118                                             expand_block_move_mem (BLKmode,
4119                                                                    src_reg,
4120                                                                    orig_src),
4121                                             GEN_INT (move_bytes),
4122                                             align_rtx));
4123             }
4124           else if (bytes >= 8 && TARGET_POWERPC64
4125                    /* 64-bit loads and stores require word-aligned
4126                       displacements.  */
4127                    && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
4128             {
4129               move_bytes = 8;
4130               tmp_reg = gen_reg_rtx (DImode);
4131               emit_move_insn (tmp_reg,
4132                               expand_block_move_mem (DImode,
4133                                                      src_reg, orig_src));
4134               emit_move_insn (expand_block_move_mem (DImode,
4135                                                      dest_reg, orig_dest),
4136                               tmp_reg);
4137             }
4138           else if (bytes > 4 && !TARGET_POWERPC64)
4139             {                   /* move up to 8 bytes at a time */
4140               move_bytes = (bytes > 8) ? 8 : bytes;
4141               emit_insn (gen_movstrsi_2reg (expand_block_move_mem (BLKmode,
4142                                                                    dest_reg,
4143                                                                    orig_dest),
4144                                             expand_block_move_mem (BLKmode,
4145                                                                    src_reg,
4146                                                                    orig_src),
4147                                             GEN_INT (move_bytes),
4148                                             align_rtx));
4149             }
4150           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
4151             {                   /* move 4 bytes */
4152               move_bytes = 4;
4153               tmp_reg = gen_reg_rtx (SImode);
4154               emit_move_insn (tmp_reg,
4155                               expand_block_move_mem (SImode,
4156                                                      src_reg, orig_src));
4157               emit_move_insn (expand_block_move_mem (SImode,
4158                                                      dest_reg, orig_dest),
4159                               tmp_reg);
4160             }
4161           else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
4162             {                   /* move 2 bytes */
4163               move_bytes = 2;
4164               tmp_reg = gen_reg_rtx (HImode);
4165               emit_move_insn (tmp_reg,
4166                               expand_block_move_mem (HImode,
4167                                                      src_reg, orig_src));
4168               emit_move_insn (expand_block_move_mem (HImode,
4169                                                      dest_reg, orig_dest),
4170                               tmp_reg);
4171             }
4172           else if (bytes == 1)  /* move 1 byte */
4173             {
4174               move_bytes = 1;
4175               tmp_reg = gen_reg_rtx (QImode);
4176               emit_move_insn (tmp_reg,
4177                               expand_block_move_mem (QImode,
4178                                                      src_reg, orig_src));
4179               emit_move_insn (expand_block_move_mem (QImode,
4180                                                      dest_reg, orig_dest),
4181                               tmp_reg);
4182             }
4183           else
4184             {                   /* move up to 4 bytes at a time */
4185               move_bytes = (bytes > 4) ? 4 : bytes;
4186               emit_insn (gen_movstrsi_1reg (expand_block_move_mem (BLKmode,
4187                                                                    dest_reg,
4188                                                                    orig_dest),
4189                                             expand_block_move_mem (BLKmode,
4190                                                                    src_reg,
4191                                                                    orig_src),
4192                                             GEN_INT (move_bytes),
4193                                             align_rtx));
4194             }
4195
4196           if (bytes > move_bytes)
4197             {
4198               if (! TARGET_POWERPC64)
4199                 {
4200                   emit_insn (gen_addsi3 (src_reg, src_reg,
4201                                          GEN_INT (move_bytes)));
4202                   emit_insn (gen_addsi3 (dest_reg, dest_reg,
4203                                          GEN_INT (move_bytes)));
4204                 }
4205               else
4206                 {
4207                   emit_insn (gen_adddi3 (src_reg, src_reg,
4208                                          GEN_INT (move_bytes)));
4209                   emit_insn (gen_adddi3 (dest_reg, dest_reg,
4210                                          GEN_INT (move_bytes)));
4211                 }
4212             }
4213         }
4214     }
4215
4216   else                  /* string instructions not available */
4217     {
4218       num_reg = offset = 0;
4219       for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
4220         {
4221           /* Calculate the correct offset for src/dest */
4222           if (offset == 0)
4223             {
4224               src_addr  = src_reg;
4225               dest_addr = dest_reg;
4226             }
4227           else
4228             {
4229               src_addr = plus_constant (src_reg, offset);
4230               dest_addr = plus_constant (dest_reg, offset);
4231             }
4232
4233           /* Generate the appropriate load and store, saving the stores
4234              for later.  */
4235           if (bytes >= 8 && TARGET_POWERPC64
4236               /* 64-bit loads and stores require word-aligned
4237                  displacements.  */
4238               && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
4239             {
4240               move_bytes = 8;
4241               tmp_reg = gen_reg_rtx (DImode);
4242               emit_insn (gen_movdi (tmp_reg,
4243                                     expand_block_move_mem (DImode,
4244                                                            src_addr,
4245                                                            orig_src)));
4246               stores[num_reg++] = gen_movdi (expand_block_move_mem (DImode,
4247                                                                     dest_addr,
4248                                                                     orig_dest),
4249                                              tmp_reg);
4250             }
4251           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
4252             {
4253               move_bytes = 4;
4254               tmp_reg = gen_reg_rtx (SImode);
4255               emit_insn (gen_movsi (tmp_reg,
4256                                     expand_block_move_mem (SImode,
4257                                                            src_addr,
4258                                                            orig_src)));
4259               stores[num_reg++] = gen_movsi (expand_block_move_mem (SImode,
4260                                                                     dest_addr,
4261                                                                     orig_dest),
4262                                              tmp_reg);
4263             }
4264           else if (bytes >= 2 && (align >= 2 || ! STRICT_ALIGNMENT))
4265             {
4266               move_bytes = 2;
4267               tmp_reg = gen_reg_rtx (HImode);
4268               emit_insn (gen_movhi (tmp_reg,
4269                                     expand_block_move_mem (HImode,
4270                                                            src_addr,
4271                                                            orig_src)));
4272               stores[num_reg++] = gen_movhi (expand_block_move_mem (HImode,
4273                                                                     dest_addr,
4274                                                                     orig_dest),
4275                                              tmp_reg);
4276             }
4277           else
4278             {
4279               move_bytes = 1;
4280               tmp_reg = gen_reg_rtx (QImode);
4281               emit_insn (gen_movqi (tmp_reg,
4282                                     expand_block_move_mem (QImode,
4283                                                            src_addr,
4284                                                            orig_src)));
4285               stores[num_reg++] = gen_movqi (expand_block_move_mem (QImode,
4286                                                                     dest_addr,
4287                                                                     orig_dest),
4288                                              tmp_reg);
4289             }
4290
4291           if (num_reg >= MAX_MOVE_REG)
4292             {
4293               for (i = 0; i < num_reg; i++)
4294                 emit_insn (stores[i]);
4295               num_reg = 0;
4296             }
4297         }
4298
4299       for (i = 0; i < num_reg; i++)
4300         emit_insn (stores[i]);
4301     }
4302
4303   return 1;
4304 }
4305
4306 \f
4307 /* Return 1 if OP is a load multiple operation.  It is known to be a
4308    PARALLEL and the first section will be tested.  */
4309
4310 int
4311 load_multiple_operation (op, mode)
4312      rtx op;
4313      enum machine_mode mode ATTRIBUTE_UNUSED;
4314 {
4315   int count = XVECLEN (op, 0);
4316   unsigned int dest_regno;
4317   rtx src_addr;
4318   int i;
4319
4320   /* Perform a quick check so we don't blow up below.  */
4321   if (count <= 1
4322       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4323       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
4324       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
4325     return 0;
4326
4327   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
4328   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
4329
4330   for (i = 1; i < count; i++)
4331     {
4332       rtx elt = XVECEXP (op, 0, i);
4333
4334       if (GET_CODE (elt) != SET
4335           || GET_CODE (SET_DEST (elt)) != REG
4336           || GET_MODE (SET_DEST (elt)) != SImode
4337           || REGNO (SET_DEST (elt)) != dest_regno + i
4338           || GET_CODE (SET_SRC (elt)) != MEM
4339           || GET_MODE (SET_SRC (elt)) != SImode
4340           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
4341           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
4342           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
4343           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
4344         return 0;
4345     }
4346
4347   return 1;
4348 }
4349
4350 /* Similar, but tests for store multiple.  Here, the second vector element
4351    is a CLOBBER.  It will be tested later.  */
4352
4353 int
4354 store_multiple_operation (op, mode)
4355      rtx op;
4356      enum machine_mode mode ATTRIBUTE_UNUSED;
4357 {
4358   int count = XVECLEN (op, 0) - 1;
4359   unsigned int src_regno;
4360   rtx dest_addr;
4361   int i;
4362
4363   /* Perform a quick check so we don't blow up below.  */
4364   if (count <= 1
4365       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4366       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
4367       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
4368     return 0;
4369
4370   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
4371   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
4372
4373   for (i = 1; i < count; i++)
4374     {
4375       rtx elt = XVECEXP (op, 0, i + 1);
4376
4377       if (GET_CODE (elt) != SET
4378           || GET_CODE (SET_SRC (elt)) != REG
4379           || GET_MODE (SET_SRC (elt)) != SImode
4380           || REGNO (SET_SRC (elt)) != src_regno + i
4381           || GET_CODE (SET_DEST (elt)) != MEM
4382           || GET_MODE (SET_DEST (elt)) != SImode
4383           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
4384           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
4385           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
4386           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
4387         return 0;
4388     }
4389
4390   return 1;
4391 }
4392
4393 /* Return 1 for a parallel vrsave operation.  */
4394
4395 int
4396 vrsave_operation (op, mode)
4397      rtx op;
4398      enum machine_mode mode ATTRIBUTE_UNUSED;
4399 {
4400   int count = XVECLEN (op, 0);
4401   unsigned int dest_regno, src_regno;
4402   int i;
4403
4404   if (count <= 1
4405       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4406       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
4407       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
4408     return 0;
4409
4410   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
4411   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
4412
4413   if (dest_regno != VRSAVE_REGNO
4414       && src_regno != VRSAVE_REGNO)
4415     return 0;
4416
4417   for (i = 1; i < count; i++)
4418     {
4419       rtx elt = XVECEXP (op, 0, i);
4420
4421       if (GET_CODE (elt) != CLOBBER)
4422         return 0;
4423     }
4424
4425   return 1;
4426 }
4427
4428 /* Return 1 for an PARALLEL suitable for mtcrf.  */
4429
4430 int
4431 mtcrf_operation (op, mode)
4432      rtx op;
4433      enum machine_mode mode ATTRIBUTE_UNUSED;
4434 {
4435   int count = XVECLEN (op, 0);
4436   int i;
4437   rtx src_reg;
4438
4439   /* Perform a quick check so we don't blow up below.  */
4440   if (count < 1
4441       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4442       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
4443       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
4444     return 0;
4445   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
4446   
4447   if (GET_CODE (src_reg) != REG
4448       || GET_MODE (src_reg) != SImode
4449       || ! INT_REGNO_P (REGNO (src_reg)))
4450     return 0;
4451
4452   for (i = 0; i < count; i++)
4453     {
4454       rtx exp = XVECEXP (op, 0, i);
4455       rtx unspec;
4456       int maskval;
4457       
4458       if (GET_CODE (exp) != SET
4459           || GET_CODE (SET_DEST (exp)) != REG
4460           || GET_MODE (SET_DEST (exp)) != CCmode
4461           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
4462         return 0;
4463       unspec = SET_SRC (exp);
4464       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
4465       
4466       if (GET_CODE (unspec) != UNSPEC
4467           || XINT (unspec, 1) != 20
4468           || XVECLEN (unspec, 0) != 2
4469           || XVECEXP (unspec, 0, 0) != src_reg
4470           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
4471           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
4472         return 0;
4473     }
4474   return 1;
4475 }
4476
4477 /* Return 1 for an PARALLEL suitable for lmw.  */
4478
4479 int
4480 lmw_operation (op, mode)
4481      rtx op;
4482      enum machine_mode mode ATTRIBUTE_UNUSED;
4483 {
4484   int count = XVECLEN (op, 0);
4485   unsigned int dest_regno;
4486   rtx src_addr;
4487   unsigned int base_regno;
4488   HOST_WIDE_INT offset;
4489   int i;
4490
4491   /* Perform a quick check so we don't blow up below.  */
4492   if (count <= 1
4493       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4494       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
4495       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
4496     return 0;
4497
4498   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
4499   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
4500
4501   if (dest_regno > 31
4502       || count != 32 - (int) dest_regno)
4503     return 0;
4504
4505   if (LEGITIMATE_INDIRECT_ADDRESS_P (src_addr, 0))
4506     {
4507       offset = 0;
4508       base_regno = REGNO (src_addr);
4509       if (base_regno == 0)
4510         return 0;
4511     }
4512   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, src_addr, 0))
4513     {
4514       offset = INTVAL (XEXP (src_addr, 1));
4515       base_regno = REGNO (XEXP (src_addr, 0));
4516     }
4517   else
4518     return 0;
4519
4520   for (i = 0; i < count; i++)
4521     {
4522       rtx elt = XVECEXP (op, 0, i);
4523       rtx newaddr;
4524       rtx addr_reg;
4525       HOST_WIDE_INT newoffset;
4526
4527       if (GET_CODE (elt) != SET
4528           || GET_CODE (SET_DEST (elt)) != REG
4529           || GET_MODE (SET_DEST (elt)) != SImode
4530           || REGNO (SET_DEST (elt)) != dest_regno + i
4531           || GET_CODE (SET_SRC (elt)) != MEM
4532           || GET_MODE (SET_SRC (elt)) != SImode)
4533         return 0;
4534       newaddr = XEXP (SET_SRC (elt), 0);
4535       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
4536         {
4537           newoffset = 0;
4538           addr_reg = newaddr;
4539         }
4540       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
4541         {
4542           addr_reg = XEXP (newaddr, 0);
4543           newoffset = INTVAL (XEXP (newaddr, 1));
4544         }
4545       else
4546         return 0;
4547       if (REGNO (addr_reg) != base_regno
4548           || newoffset != offset + 4 * i)
4549         return 0;
4550     }
4551
4552   return 1;
4553 }
4554
4555 /* Return 1 for an PARALLEL suitable for stmw.  */
4556
4557 int
4558 stmw_operation (op, mode)
4559      rtx op;
4560      enum machine_mode mode ATTRIBUTE_UNUSED;
4561 {
4562   int count = XVECLEN (op, 0);
4563   unsigned int src_regno;
4564   rtx dest_addr;
4565   unsigned int base_regno;
4566   HOST_WIDE_INT offset;
4567   int i;
4568
4569   /* Perform a quick check so we don't blow up below.  */
4570   if (count <= 1
4571       || GET_CODE (XVECEXP (op, 0, 0)) != SET
4572       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
4573       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
4574     return 0;
4575
4576   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
4577   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
4578
4579   if (src_regno > 31
4580       || count != 32 - (int) src_regno)
4581     return 0;
4582
4583   if (LEGITIMATE_INDIRECT_ADDRESS_P (dest_addr, 0))
4584     {
4585       offset = 0;
4586       base_regno = REGNO (dest_addr);
4587       if (base_regno == 0)
4588         return 0;
4589     }
4590   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, dest_addr, 0))
4591     {
4592       offset = INTVAL (XEXP (dest_addr, 1));
4593       base_regno = REGNO (XEXP (dest_addr, 0));
4594     }
4595   else
4596     return 0;
4597
4598   for (i = 0; i < count; i++)
4599     {
4600       rtx elt = XVECEXP (op, 0, i);
4601       rtx newaddr;
4602       rtx addr_reg;
4603       HOST_WIDE_INT newoffset;
4604
4605       if (GET_CODE (elt) != SET
4606           || GET_CODE (SET_SRC (elt)) != REG
4607           || GET_MODE (SET_SRC (elt)) != SImode
4608           || REGNO (SET_SRC (elt)) != src_regno + i
4609           || GET_CODE (SET_DEST (elt)) != MEM
4610           || GET_MODE (SET_DEST (elt)) != SImode)
4611         return 0;
4612       newaddr = XEXP (SET_DEST (elt), 0);
4613       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
4614         {
4615           newoffset = 0;
4616           addr_reg = newaddr;
4617         }
4618       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
4619         {
4620           addr_reg = XEXP (newaddr, 0);
4621           newoffset = INTVAL (XEXP (newaddr, 1));
4622         }
4623       else
4624         return 0;
4625       if (REGNO (addr_reg) != base_regno
4626           || newoffset != offset + 4 * i)
4627         return 0;
4628     }
4629
4630   return 1;
4631 }
4632 \f
4633 /* A validation routine: say whether CODE, a condition code, and MODE
4634    match.  The other alternatives either don't make sense or should
4635    never be generated.  */
4636
4637 static void
4638 validate_condition_mode (code, mode)
4639      enum rtx_code code;
4640      enum machine_mode mode;
4641 {
4642   if (GET_RTX_CLASS (code) != '<' 
4643       || GET_MODE_CLASS (mode) != MODE_CC)
4644     abort ();
4645
4646   /* These don't make sense.  */
4647   if ((code == GT || code == LT || code == GE || code == LE)
4648       && mode == CCUNSmode)
4649     abort ();
4650
4651   if ((code == GTU || code == LTU || code == GEU || code == LEU)
4652       && mode != CCUNSmode)
4653     abort ();
4654
4655   if (mode != CCFPmode
4656       && (code == ORDERED || code == UNORDERED
4657           || code == UNEQ || code == LTGT
4658           || code == UNGT || code == UNLT
4659           || code == UNGE || code == UNLE))
4660     abort ();
4661   
4662   /* These should never be generated except for 
4663      flag_unsafe_math_optimizations.  */
4664   if (mode == CCFPmode
4665       && ! flag_unsafe_math_optimizations
4666       && (code == LE || code == GE
4667           || code == UNEQ || code == LTGT
4668           || code == UNGT || code == UNLT))
4669     abort ();
4670
4671   /* These are invalid; the information is not there.  */
4672   if (mode == CCEQmode 
4673       && code != EQ && code != NE)
4674     abort ();
4675 }
4676
4677 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
4678    We only check the opcode against the mode of the CC value here.  */
4679
4680 int
4681 branch_comparison_operator (op, mode)
4682      rtx op;
4683      enum machine_mode mode ATTRIBUTE_UNUSED;
4684 {
4685   enum rtx_code code = GET_CODE (op);
4686   enum machine_mode cc_mode;
4687
4688   if (GET_RTX_CLASS (code) != '<')
4689     return 0;
4690
4691   cc_mode = GET_MODE (XEXP (op, 0));
4692   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
4693     return 0;
4694
4695   validate_condition_mode (code, cc_mode);
4696
4697   return 1;
4698 }
4699
4700 /* Return 1 if OP is a comparison operation that is valid for a branch
4701    insn and which is true if the corresponding bit in the CC register
4702    is set.  */
4703
4704 int
4705 branch_positive_comparison_operator (op, mode)
4706      rtx op;
4707      enum machine_mode mode;
4708 {
4709   enum rtx_code code;
4710
4711   if (! branch_comparison_operator (op, mode))
4712     return 0;
4713
4714   code = GET_CODE (op);
4715   return (code == EQ || code == LT || code == GT
4716           || code == LTU || code == GTU
4717           || code == UNORDERED);
4718 }
4719
4720 /* Return 1 if OP is a comparison operation that is valid for an scc insn.
4721    We check the opcode against the mode of the CC value and disallow EQ or
4722    NE comparisons for integers.  */
4723
4724 int
4725 scc_comparison_operator (op, mode)
4726      rtx op;
4727      enum machine_mode mode;
4728 {
4729   enum rtx_code code = GET_CODE (op);
4730   enum machine_mode cc_mode;
4731
4732   if (GET_MODE (op) != mode && mode != VOIDmode)
4733     return 0;
4734
4735   if (GET_RTX_CLASS (code) != '<')
4736     return 0;
4737
4738   cc_mode = GET_MODE (XEXP (op, 0));
4739   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
4740     return 0;
4741
4742   validate_condition_mode (code, cc_mode);
4743
4744   if (code == NE && cc_mode != CCFPmode)
4745     return 0;
4746
4747   return 1;
4748 }
4749
4750 int
4751 trap_comparison_operator (op, mode)
4752     rtx op;
4753     enum machine_mode mode;
4754 {
4755   if (mode != VOIDmode && mode != GET_MODE (op))
4756     return 0;
4757   return GET_RTX_CLASS (GET_CODE (op)) == '<';
4758 }
4759
4760 int
4761 boolean_operator (op, mode)
4762     rtx op;
4763     enum machine_mode mode ATTRIBUTE_UNUSED;
4764 {
4765   enum rtx_code code = GET_CODE (op);
4766   return (code == AND || code == IOR || code == XOR);
4767 }
4768
4769 int
4770 boolean_or_operator (op, mode)
4771     rtx op;
4772     enum machine_mode mode ATTRIBUTE_UNUSED;
4773 {
4774   enum rtx_code code = GET_CODE (op);
4775   return (code == IOR || code == XOR);
4776 }
4777
4778 int
4779 min_max_operator (op, mode)
4780     rtx op;
4781     enum machine_mode mode ATTRIBUTE_UNUSED;
4782 {
4783   enum rtx_code code = GET_CODE (op);
4784   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
4785 }
4786 \f
4787 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
4788    mask required to convert the result of a rotate insn into a shift
4789    left insn of SHIFTOP bits.  Both are known to be CONST_INT.  */
4790
4791 int
4792 includes_lshift_p (shiftop, andop)
4793      rtx shiftop;
4794      rtx andop;
4795 {
4796   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
4797
4798   shift_mask <<= INTVAL (shiftop);
4799
4800   return (INTVAL (andop) & ~shift_mask) == 0;
4801 }
4802
4803 /* Similar, but for right shift.  */
4804
4805 int
4806 includes_rshift_p (shiftop, andop)
4807      rtx shiftop;
4808      rtx andop;
4809 {
4810   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
4811
4812   shift_mask >>= INTVAL (shiftop);
4813
4814   return (INTVAL (andop) & ~shift_mask) == 0;
4815 }
4816
4817 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
4818    to perform a left shift.  It must have exactly SHIFTOP least
4819    signifigant 0's, then one or more 1's, then zero or more 0's.  */
4820
4821 int
4822 includes_rldic_lshift_p (shiftop, andop)
4823      rtx shiftop;
4824      rtx andop;
4825 {
4826   if (GET_CODE (andop) == CONST_INT)
4827     {
4828       HOST_WIDE_INT c, lsb, shift_mask;
4829
4830       c = INTVAL (andop);
4831       if (c == 0 || c == ~0)
4832         return 0;
4833
4834       shift_mask = ~0;
4835       shift_mask <<= INTVAL (shiftop);
4836
4837       /* Find the least signifigant one bit.  */
4838       lsb = c & -c;
4839
4840       /* It must coincide with the LSB of the shift mask.  */
4841       if (-lsb != shift_mask)
4842         return 0;
4843
4844       /* Invert to look for the next transition (if any).  */
4845       c = ~c;
4846
4847       /* Remove the low group of ones (originally low group of zeros).  */
4848       c &= -lsb;
4849
4850       /* Again find the lsb, and check we have all 1's above.  */
4851       lsb = c & -c;
4852       return c == -lsb;
4853     }
4854   else if (GET_CODE (andop) == CONST_DOUBLE
4855            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
4856     {
4857       HOST_WIDE_INT low, high, lsb;
4858       HOST_WIDE_INT shift_mask_low, shift_mask_high;
4859
4860       low = CONST_DOUBLE_LOW (andop);
4861       if (HOST_BITS_PER_WIDE_INT < 64)
4862         high = CONST_DOUBLE_HIGH (andop);
4863
4864       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
4865           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
4866         return 0;
4867
4868       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
4869         {
4870           shift_mask_high = ~0;
4871           if (INTVAL (shiftop) > 32)
4872             shift_mask_high <<= INTVAL (shiftop) - 32;
4873
4874           lsb = high & -high;
4875
4876           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
4877             return 0;
4878
4879           high = ~high;
4880           high &= -lsb;
4881
4882           lsb = high & -high;
4883           return high == -lsb;
4884         }
4885
4886       shift_mask_low = ~0;
4887       shift_mask_low <<= INTVAL (shiftop);
4888
4889       lsb = low & -low;
4890
4891       if (-lsb != shift_mask_low)
4892         return 0;
4893
4894       if (HOST_BITS_PER_WIDE_INT < 64)
4895         high = ~high;
4896       low = ~low;
4897       low &= -lsb;
4898
4899       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
4900         {
4901           lsb = high & -high;
4902           return high == -lsb;
4903         }
4904
4905       lsb = low & -low;
4906       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
4907     }
4908   else
4909     return 0;
4910 }
4911
4912 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
4913    to perform a left shift.  It must have SHIFTOP or more least
4914    signifigant 0's, with the remainder of the word 1's.  */
4915
4916 int
4917 includes_rldicr_lshift_p (shiftop, andop)
4918      rtx shiftop;
4919      rtx andop;
4920 {
4921   if (GET_CODE (andop) == CONST_INT)
4922     {
4923       HOST_WIDE_INT c, lsb, shift_mask;
4924
4925       shift_mask = ~0;
4926       shift_mask <<= INTVAL (shiftop);
4927       c = INTVAL (andop);
4928
4929       /* Find the least signifigant one bit.  */
4930       lsb = c & -c;
4931
4932       /* It must be covered by the shift mask.
4933          This test also rejects c == 0.  */
4934       if ((lsb & shift_mask) == 0)
4935         return 0;
4936
4937       /* Check we have all 1's above the transition, and reject all 1's.  */
4938       return c == -lsb && lsb != 1;
4939     }
4940   else if (GET_CODE (andop) == CONST_DOUBLE
4941            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
4942     {
4943       HOST_WIDE_INT low, lsb, shift_mask_low;
4944
4945       low = CONST_DOUBLE_LOW (andop);
4946
4947       if (HOST_BITS_PER_WIDE_INT < 64)
4948         {
4949           HOST_WIDE_INT high, shift_mask_high;
4950
4951           high = CONST_DOUBLE_HIGH (andop);
4952
4953           if (low == 0)
4954             {
4955               shift_mask_high = ~0;
4956               if (INTVAL (shiftop) > 32)
4957                 shift_mask_high <<= INTVAL (shiftop) - 32;
4958
4959               lsb = high & -high;
4960
4961               if ((lsb & shift_mask_high) == 0)
4962                 return 0;
4963
4964               return high == -lsb;
4965             }
4966           if (high != ~0)
4967             return 0;
4968         }
4969
4970       shift_mask_low = ~0;
4971       shift_mask_low <<= INTVAL (shiftop);
4972
4973       lsb = low & -low;
4974
4975       if ((lsb & shift_mask_low) == 0)
4976         return 0;
4977
4978       return low == -lsb && lsb != 1;
4979     }
4980   else
4981     return 0;
4982 }
4983
4984 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
4985    for lfq and stfq insns.
4986
4987    Note reg1 and reg2 *must* be hard registers.  To be sure we will
4988    abort if we are passed pseudo registers.  */
4989
4990 int
4991 registers_ok_for_quad_peep (reg1, reg2)
4992      rtx reg1, reg2;
4993 {
4994   /* We might have been passed a SUBREG.  */
4995   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
4996     return 0;
4997
4998   return (REGNO (reg1) == REGNO (reg2) - 1);
4999 }
5000
5001 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
5002    addr1 and addr2 must be in consecutive memory locations
5003    (addr2 == addr1 + 8).  */
5004
5005 int
5006 addrs_ok_for_quad_peep (addr1, addr2)
5007      rtx addr1;
5008      rtx addr2;
5009 {
5010   unsigned int reg1;
5011   int offset1;
5012
5013   /* Extract an offset (if used) from the first addr.  */
5014   if (GET_CODE (addr1) == PLUS)
5015     {
5016       /* If not a REG, return zero.  */
5017       if (GET_CODE (XEXP (addr1, 0)) != REG)
5018         return 0;
5019       else
5020         {
5021           reg1 = REGNO (XEXP (addr1, 0));
5022           /* The offset must be constant!  */
5023           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
5024             return 0;
5025           offset1 = INTVAL (XEXP (addr1, 1));
5026         }
5027     }
5028   else if (GET_CODE (addr1) != REG)
5029     return 0;
5030   else
5031     {
5032       reg1 = REGNO (addr1);
5033       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
5034       offset1 = 0;
5035     }
5036
5037   /* Make sure the second address is a (mem (plus (reg) (const_int))).  */
5038   if (GET_CODE (addr2) != PLUS)
5039     return 0;
5040
5041   if (GET_CODE (XEXP (addr2, 0)) != REG
5042       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
5043     return 0;
5044
5045   if (reg1 != REGNO (XEXP (addr2, 0)))
5046     return 0;
5047
5048   /* The offset for the second addr must be 8 more than the first addr.  */
5049   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
5050     return 0;
5051
5052   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
5053      instructions.  */
5054   return 1;
5055 }
5056 \f
5057 /* Return the register class of a scratch register needed to copy IN into
5058    or out of a register in CLASS in MODE.  If it can be done directly,
5059    NO_REGS is returned.  */
5060
5061 enum reg_class
5062 secondary_reload_class (class, mode, in)
5063      enum reg_class class;
5064      enum machine_mode mode ATTRIBUTE_UNUSED;
5065      rtx in;
5066 {
5067   int regno;
5068
5069   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN && flag_pic))
5070     {
5071       /* We cannot copy a symbolic operand directly into anything
5072          other than BASE_REGS for TARGET_ELF.  So indicate that a
5073          register from BASE_REGS is needed as an intermediate
5074          register.
5075          
5076          On Darwin, pic addresses require a load from memory, which
5077          needs a base register.  */
5078       if (class != BASE_REGS
5079           && (GET_CODE (in) == SYMBOL_REF
5080               || GET_CODE (in) == HIGH
5081               || GET_CODE (in) == LABEL_REF
5082               || GET_CODE (in) == CONST))
5083         return BASE_REGS;
5084     }
5085
5086   if (GET_CODE (in) == REG)
5087     {
5088       regno = REGNO (in);
5089       if (regno >= FIRST_PSEUDO_REGISTER)
5090         {
5091           regno = true_regnum (in);
5092           if (regno >= FIRST_PSEUDO_REGISTER)
5093             regno = -1;
5094         }
5095     }
5096   else if (GET_CODE (in) == SUBREG)
5097     {
5098       regno = true_regnum (in);
5099       if (regno >= FIRST_PSEUDO_REGISTER)
5100         regno = -1;
5101     }
5102   else
5103     regno = -1;
5104
5105   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
5106      into anything.  */
5107   if (class == GENERAL_REGS || class == BASE_REGS
5108       || (regno >= 0 && INT_REGNO_P (regno)))
5109     return NO_REGS;
5110
5111   /* Constants, memory, and FP registers can go into FP registers.  */
5112   if ((regno == -1 || FP_REGNO_P (regno))
5113       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
5114     return NO_REGS;
5115
5116   /* Memory, and AltiVec registers can go into AltiVec registers.  */
5117   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
5118       && class == ALTIVEC_REGS)
5119     return NO_REGS;
5120
5121   /* We can copy among the CR registers.  */
5122   if ((class == CR_REGS || class == CR0_REGS)
5123       && regno >= 0 && CR_REGNO_P (regno))
5124     return NO_REGS;
5125
5126   /* Otherwise, we need GENERAL_REGS.  */
5127   return GENERAL_REGS;
5128 }
5129 \f
5130 /* Given a comparison operation, return the bit number in CCR to test.  We
5131    know this is a valid comparison.  
5132
5133    SCC_P is 1 if this is for an scc.  That means that %D will have been
5134    used instead of %C, so the bits will be in different places.
5135
5136    Return -1 if OP isn't a valid comparison for some reason.  */
5137
5138 int
5139 ccr_bit (op, scc_p)
5140      rtx op;
5141      int scc_p;
5142 {
5143   enum rtx_code code = GET_CODE (op);
5144   enum machine_mode cc_mode;
5145   int cc_regnum;
5146   int base_bit;
5147   rtx reg;
5148
5149   if (GET_RTX_CLASS (code) != '<')
5150     return -1;
5151
5152   reg = XEXP (op, 0);
5153
5154   if (GET_CODE (reg) != REG
5155       || ! CR_REGNO_P (REGNO (reg)))
5156     abort ();
5157
5158   cc_mode = GET_MODE (reg);
5159   cc_regnum = REGNO (reg);
5160   base_bit = 4 * (cc_regnum - CR0_REGNO);
5161
5162   validate_condition_mode (code, cc_mode);
5163
5164   switch (code)
5165     {
5166     case NE:
5167       return scc_p ? base_bit + 3 : base_bit + 2;
5168     case EQ:
5169       return base_bit + 2;
5170     case GT:  case GTU:  case UNLE:
5171       return base_bit + 1;
5172     case LT:  case LTU:  case UNGE:
5173       return base_bit;
5174     case ORDERED:  case UNORDERED:
5175       return base_bit + 3;
5176
5177     case GE:  case GEU:
5178       /* If scc, we will have done a cror to put the bit in the
5179          unordered position.  So test that bit.  For integer, this is ! LT
5180          unless this is an scc insn.  */
5181       return scc_p ? base_bit + 3 : base_bit;
5182
5183     case LE:  case LEU:
5184       return scc_p ? base_bit + 3 : base_bit + 1;
5185
5186     default:
5187       abort ();
5188     }
5189 }
5190 \f
5191 /* Return the GOT register.  */
5192
5193 struct rtx_def *
5194 rs6000_got_register (value)
5195      rtx value ATTRIBUTE_UNUSED;
5196 {
5197   /* The second flow pass currently (June 1999) can't update
5198      regs_ever_live without disturbing other parts of the compiler, so
5199      update it here to make the prolog/epilogue code happy.  */
5200   if (no_new_pseudos && ! regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5201     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5202
5203   current_function_uses_pic_offset_table = 1;
5204
5205   return pic_offset_table_rtx;
5206 }
5207 \f
5208 /* Functions to init, mark and free struct machine_function.
5209    These will be called, via pointer variables,
5210    from push_function_context and pop_function_context.  */
5211
5212 static void
5213 rs6000_init_machine_status (p)
5214      struct function *p;
5215 {
5216   p->machine = (machine_function *) xcalloc (1, sizeof (machine_function));
5217 }
5218
5219 static void
5220 rs6000_free_machine_status (p)
5221      struct function *p;
5222 {
5223   if (p->machine == NULL)
5224     return;
5225
5226   free (p->machine);
5227   p->machine = NULL;
5228 }
5229
5230 \f
5231 /* Print an operand.  Recognize special options, documented below.  */
5232
5233 #if TARGET_ELF
5234 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
5235 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
5236 #else
5237 #define SMALL_DATA_RELOC "sda21"
5238 #define SMALL_DATA_REG 0
5239 #endif
5240
5241 void
5242 print_operand (file, x, code)
5243     FILE *file;
5244     rtx x;
5245     int code;
5246 {
5247   int i;
5248   HOST_WIDE_INT val;
5249
5250   /* These macros test for integers and extract the low-order bits.  */
5251 #define INT_P(X)  \
5252 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
5253  && GET_MODE (X) == VOIDmode)
5254
5255 #define INT_LOWPART(X) \
5256   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
5257
5258   switch (code)
5259     {
5260     case '.':
5261       /* Write out an instruction after the call which may be replaced
5262          with glue code by the loader.  This depends on the AIX version.  */
5263       asm_fprintf (file, RS6000_CALL_GLUE);
5264       return;
5265
5266       /* %a is output_address.  */
5267
5268     case 'A':
5269       /* If X is a constant integer whose low-order 5 bits are zero,
5270          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
5271          in the AIX assembler where "sri" with a zero shift count
5272          write a trash instruction.  */
5273       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
5274         putc ('l', file);
5275       else
5276         putc ('r', file);
5277       return;
5278
5279     case 'b':
5280       /* If constant, low-order 16 bits of constant, unsigned.
5281          Otherwise, write normally.  */
5282       if (INT_P (x))
5283         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
5284       else
5285         print_operand (file, x, 0);
5286       return;
5287
5288     case 'B':
5289       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
5290          for 64-bit mask direction.  */
5291       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
5292       return;
5293
5294       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
5295          output_operand.  */
5296
5297     case 'D':
5298       /* There used to be a comment for 'C' reading "This is an
5299            optional cror needed for certain floating-point
5300            comparisons.  Otherwise write nothing."  */
5301
5302       /* Similar, except that this is for an scc, so we must be able to
5303          encode the test in a single bit that is one.  We do the above
5304          for any LE, GE, GEU, or LEU and invert the bit for NE.  */
5305       if (GET_CODE (x) == LE || GET_CODE (x) == GE
5306           || GET_CODE (x) == LEU || GET_CODE (x) == GEU)
5307         {
5308           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
5309
5310           fprintf (file, "cror %d,%d,%d\n\t", base_bit + 3,
5311                    base_bit + 2,
5312                    base_bit + (GET_CODE (x) == GE || GET_CODE (x) == GEU));
5313         }
5314
5315       else if (GET_CODE (x) == NE)
5316         {
5317           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
5318
5319           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 3,
5320                    base_bit + 2, base_bit + 2);
5321         }
5322       return;
5323
5324     case 'E':
5325       /* X is a CR register.  Print the number of the EQ bit of the CR */
5326       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
5327         output_operand_lossage ("invalid %%E value");
5328       else
5329         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
5330       return;
5331
5332     case 'f':
5333       /* X is a CR register.  Print the shift count needed to move it
5334          to the high-order four bits.  */
5335       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
5336         output_operand_lossage ("invalid %%f value");
5337       else
5338         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
5339       return;
5340
5341     case 'F':
5342       /* Similar, but print the count for the rotate in the opposite
5343          direction.  */
5344       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
5345         output_operand_lossage ("invalid %%F value");
5346       else
5347         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
5348       return;
5349
5350     case 'G':
5351       /* X is a constant integer.  If it is negative, print "m",
5352          otherwise print "z".  This is to make a aze or ame insn.  */
5353       if (GET_CODE (x) != CONST_INT)
5354         output_operand_lossage ("invalid %%G value");
5355       else if (INTVAL (x) >= 0)
5356         putc ('z', file);
5357       else
5358         putc ('m', file);
5359       return;
5360
5361     case 'h':
5362       /* If constant, output low-order five bits.  Otherwise, write
5363          normally.  */
5364       if (INT_P (x))
5365         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
5366       else
5367         print_operand (file, x, 0);
5368       return;
5369
5370     case 'H':
5371       /* If constant, output low-order six bits.  Otherwise, write
5372          normally.  */
5373       if (INT_P (x))
5374         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
5375       else
5376         print_operand (file, x, 0);
5377       return;
5378
5379     case 'I':
5380       /* Print `i' if this is a constant, else nothing.  */
5381       if (INT_P (x))
5382         putc ('i', file);
5383       return;
5384
5385     case 'j':
5386       /* Write the bit number in CCR for jump.  */
5387       i = ccr_bit (x, 0);
5388       if (i == -1)
5389         output_operand_lossage ("invalid %%j code");
5390       else
5391         fprintf (file, "%d", i);
5392       return;
5393
5394     case 'J':
5395       /* Similar, but add one for shift count in rlinm for scc and pass
5396          scc flag to `ccr_bit'.  */
5397       i = ccr_bit (x, 1);
5398       if (i == -1)
5399         output_operand_lossage ("invalid %%J code");
5400       else
5401         /* If we want bit 31, write a shift count of zero, not 32.  */
5402         fprintf (file, "%d", i == 31 ? 0 : i + 1);
5403       return;
5404
5405     case 'k':
5406       /* X must be a constant.  Write the 1's complement of the
5407          constant.  */
5408       if (! INT_P (x))
5409         output_operand_lossage ("invalid %%k value");
5410       else
5411         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
5412       return;
5413
5414     case 'K':
5415       /* X must be a symbolic constant on ELF.  Write an
5416          expression suitable for an 'addi' that adds in the low 16
5417          bits of the MEM.  */
5418       if (GET_CODE (x) != CONST)
5419         {
5420           print_operand_address (file, x);
5421           fputs ("@l", file);
5422         }
5423       else
5424         {
5425           if (GET_CODE (XEXP (x, 0)) != PLUS
5426               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
5427                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
5428               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
5429             output_operand_lossage ("invalid %%K value");
5430           print_operand_address (file, XEXP (XEXP (x, 0), 0));
5431           fputs ("@l", file);
5432           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
5433         }
5434       return;
5435
5436       /* %l is output_asm_label.  */
5437
5438     case 'L':
5439       /* Write second word of DImode or DFmode reference.  Works on register
5440          or non-indexed memory only.  */
5441       if (GET_CODE (x) == REG)
5442         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
5443       else if (GET_CODE (x) == MEM)
5444         {
5445           /* Handle possible auto-increment.  Since it is pre-increment and
5446              we have already done it, we can just use an offset of word.  */
5447           if (GET_CODE (XEXP (x, 0)) == PRE_INC
5448               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
5449             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
5450                                            UNITS_PER_WORD));
5451           else
5452             output_address (XEXP (adjust_address_nv (x, SImode,
5453                                                      UNITS_PER_WORD),
5454                                   0));
5455
5456           if (small_data_operand (x, GET_MODE (x)))
5457             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
5458                      reg_names[SMALL_DATA_REG]);
5459         }
5460       return;
5461                             
5462     case 'm':
5463       /* MB value for a mask operand.  */
5464       if (! mask_operand (x, VOIDmode))
5465         output_operand_lossage ("invalid %%m value");
5466
5467       val = INT_LOWPART (x);
5468
5469       /* If the high bit is set and the low bit is not, the value is zero.
5470          If the high bit is zero, the value is the first 1 bit we find from
5471          the left.  */
5472       if ((val & 0x80000000) && ((val & 1) == 0))
5473         {
5474           putc ('0', file);
5475           return;
5476         }
5477       else if ((val & 0x80000000) == 0)
5478         {
5479           for (i = 1; i < 32; i++)
5480             if ((val <<= 1) & 0x80000000)
5481               break;
5482           fprintf (file, "%d", i);
5483           return;
5484         }
5485           
5486       /* Otherwise, look for the first 0 bit from the right.  The result is its
5487          number plus 1. We know the low-order bit is one.  */
5488       for (i = 0; i < 32; i++)
5489         if (((val >>= 1) & 1) == 0)
5490           break;
5491
5492       /* If we ended in ...01, i would be 0.  The correct value is 31, so
5493          we want 31 - i.  */
5494       fprintf (file, "%d", 31 - i);
5495       return;
5496
5497     case 'M':
5498       /* ME value for a mask operand.  */
5499       if (! mask_operand (x, VOIDmode))
5500         output_operand_lossage ("invalid %%M value");
5501
5502       val = INT_LOWPART (x);
5503
5504       /* If the low bit is set and the high bit is not, the value is 31.
5505          If the low bit is zero, the value is the first 1 bit we find from
5506          the right.  */
5507       if ((val & 1) && ((val & 0x80000000) == 0))
5508         {
5509           fputs ("31", file);
5510           return;
5511         }
5512       else if ((val & 1) == 0)
5513         {
5514           for (i = 0; i < 32; i++)
5515             if ((val >>= 1) & 1)
5516               break;
5517
5518           /* If we had ....10, i would be 0.  The result should be
5519              30, so we need 30 - i.  */
5520           fprintf (file, "%d", 30 - i);
5521           return;
5522         }
5523           
5524       /* Otherwise, look for the first 0 bit from the left.  The result is its
5525          number minus 1. We know the high-order bit is one.  */
5526       for (i = 0; i < 32; i++)
5527         if (((val <<= 1) & 0x80000000) == 0)
5528           break;
5529
5530       fprintf (file, "%d", i);
5531       return;
5532
5533       /* %n outputs the negative of its operand.  */
5534
5535     case 'N':
5536       /* Write the number of elements in the vector times 4.  */
5537       if (GET_CODE (x) != PARALLEL)
5538         output_operand_lossage ("invalid %%N value");
5539       else
5540         fprintf (file, "%d", XVECLEN (x, 0) * 4);
5541       return;
5542
5543     case 'O':
5544       /* Similar, but subtract 1 first.  */
5545       if (GET_CODE (x) != PARALLEL)
5546         output_operand_lossage ("invalid %%O value");
5547       else
5548         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
5549       return;
5550
5551     case 'p':
5552       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
5553       if (! INT_P (x)
5554           || INT_LOWPART (x) < 0
5555           || (i = exact_log2 (INT_LOWPART (x))) < 0)
5556         output_operand_lossage ("invalid %%p value");
5557       else
5558         fprintf (file, "%d", i);
5559       return;
5560
5561     case 'P':
5562       /* The operand must be an indirect memory reference.  The result
5563          is the register number.  */
5564       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
5565           || REGNO (XEXP (x, 0)) >= 32)
5566         output_operand_lossage ("invalid %%P value");
5567       else
5568         fprintf (file, "%d", REGNO (XEXP (x, 0)));
5569       return;
5570
5571     case 'q':
5572       /* This outputs the logical code corresponding to a boolean
5573          expression.  The expression may have one or both operands
5574          negated (if one, only the first one).  For condition register
5575          logical operations, it will also treat the negated
5576          CR codes as NOTs, but not handle NOTs of them.  */
5577       {
5578         const char *const *t = 0;
5579         const char *s;
5580         enum rtx_code code = GET_CODE (x);
5581         static const char * const tbl[3][3] = {
5582           { "and", "andc", "nor" },
5583           { "or", "orc", "nand" },
5584           { "xor", "eqv", "xor" } };
5585
5586         if (code == AND)
5587           t = tbl[0];
5588         else if (code == IOR)
5589           t = tbl[1];
5590         else if (code == XOR)
5591           t = tbl[2];
5592         else
5593           output_operand_lossage ("invalid %%q value");
5594
5595         if (GET_CODE (XEXP (x, 0)) != NOT)
5596           s = t[0];
5597         else
5598           {
5599             if (GET_CODE (XEXP (x, 1)) == NOT)
5600               s = t[2];
5601             else
5602               s = t[1];
5603           }
5604         
5605         fputs (s, file);
5606       }
5607       return;
5608
5609     case 'R':
5610       /* X is a CR register.  Print the mask for `mtcrf'.  */
5611       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
5612         output_operand_lossage ("invalid %%R value");
5613       else
5614         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
5615       return;
5616
5617     case 's':
5618       /* Low 5 bits of 32 - value */
5619       if (! INT_P (x))
5620         output_operand_lossage ("invalid %%s value");
5621       else
5622         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
5623       return;
5624
5625     case 'S':
5626       /* PowerPC64 mask position.  All 0's and all 1's are excluded.
5627          CONST_INT 32-bit mask is considered sign-extended so any
5628          transition must occur within the CONST_INT, not on the boundary.  */
5629       if (! mask64_operand (x, VOIDmode))
5630         output_operand_lossage ("invalid %%S value");
5631
5632       val = INT_LOWPART (x);
5633
5634       if (val & 1)      /* Clear Left */
5635         {
5636           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5637             if (!((val >>= 1) & 1))
5638               break;
5639
5640 #if HOST_BITS_PER_WIDE_INT == 32
5641           if (GET_CODE (x) == CONST_DOUBLE && i == 32)
5642             {
5643               val = CONST_DOUBLE_HIGH (x);
5644
5645               if (val == 0)
5646                 --i;
5647               else
5648                 for (i = 32; i < 64; i++)
5649                   if (!((val >>= 1) & 1))
5650                     break;
5651             }
5652 #endif
5653         /* i = index of last set bit from right
5654            mask begins at 63 - i from left */
5655           if (i > 63)
5656             output_operand_lossage ("%%S computed all 1's mask");
5657
5658           fprintf (file, "%d", 63 - i);
5659           return;
5660         }
5661       else      /* Clear Right */
5662         {
5663           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5664             if ((val >>= 1) & 1)
5665               break;
5666
5667 #if HOST_BITS_PER_WIDE_INT == 32
5668         if (GET_CODE (x) == CONST_DOUBLE && i == 32)
5669           {
5670             val = CONST_DOUBLE_HIGH (x);
5671
5672             if (val == (HOST_WIDE_INT) -1)
5673               --i;
5674             else
5675               for (i = 32; i < 64; i++)
5676                 if ((val >>= 1) & 1)
5677                   break;
5678           }
5679 #endif
5680         /* i = index of last clear bit from right
5681            mask ends at 62 - i from left */
5682           if (i > 62)
5683             output_operand_lossage ("%%S computed all 0's mask");
5684
5685           fprintf (file, "%d", 62 - i);
5686           return;
5687         }
5688
5689     case 'T':
5690       /* Print the symbolic name of a branch target register.  */
5691       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
5692                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
5693         output_operand_lossage ("invalid %%T value");
5694       else if (REGNO (x) == LINK_REGISTER_REGNUM)
5695         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
5696       else
5697         fputs ("ctr", file);
5698       return;
5699
5700     case 'u':
5701       /* High-order 16 bits of constant for use in unsigned operand.  */
5702       if (! INT_P (x))
5703         output_operand_lossage ("invalid %%u value");
5704       else
5705         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
5706                  (INT_LOWPART (x) >> 16) & 0xffff);
5707       return;
5708
5709     case 'v':
5710       /* High-order 16 bits of constant for use in signed operand.  */
5711       if (! INT_P (x))
5712         output_operand_lossage ("invalid %%v value");
5713       else
5714         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
5715                  (INT_LOWPART (x) >> 16) & 0xffff);
5716       return;
5717
5718     case 'U':
5719       /* Print `u' if this has an auto-increment or auto-decrement.  */
5720       if (GET_CODE (x) == MEM
5721           && (GET_CODE (XEXP (x, 0)) == PRE_INC
5722               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
5723         putc ('u', file);
5724       return;
5725
5726     case 'V':
5727       /* Print the trap code for this operand.  */
5728       switch (GET_CODE (x))
5729         {
5730         case EQ:
5731           fputs ("eq", file);   /* 4 */
5732           break;
5733         case NE:
5734           fputs ("ne", file);   /* 24 */
5735           break;
5736         case LT:
5737           fputs ("lt", file);   /* 16 */
5738           break;
5739         case LE:
5740           fputs ("le", file);   /* 20 */
5741           break;
5742         case GT:
5743           fputs ("gt", file);   /* 8 */
5744           break;
5745         case GE:
5746           fputs ("ge", file);   /* 12 */
5747           break;
5748         case LTU:
5749           fputs ("llt", file);  /* 2 */
5750           break;
5751         case LEU:
5752           fputs ("lle", file);  /* 6 */
5753           break;
5754         case GTU:
5755           fputs ("lgt", file);  /* 1 */
5756           break;
5757         case GEU:
5758           fputs ("lge", file);  /* 5 */
5759           break;
5760         default:
5761           abort ();
5762         }
5763       break;
5764
5765     case 'w':
5766       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
5767          normally.  */
5768       if (INT_P (x))
5769         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
5770                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
5771       else
5772         print_operand (file, x, 0);
5773       return;
5774
5775     case 'W':
5776       /* MB value for a PowerPC64 rldic operand.  */
5777       val = (GET_CODE (x) == CONST_INT
5778              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
5779
5780       if (val < 0)
5781         i = -1;
5782       else
5783         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5784           if ((val <<= 1) < 0)
5785             break;
5786
5787 #if HOST_BITS_PER_WIDE_INT == 32
5788       if (GET_CODE (x) == CONST_INT && i >= 0)
5789         i += 32;  /* zero-extend high-part was all 0's */
5790       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
5791         {
5792           val = CONST_DOUBLE_LOW (x);
5793
5794           if (val == 0)
5795             abort ();
5796           else if (val < 0)
5797             --i;
5798           else
5799             for ( ; i < 64; i++)
5800               if ((val <<= 1) < 0)
5801                 break;
5802         }
5803 #endif
5804
5805       fprintf (file, "%d", i + 1);
5806       return;
5807
5808     case 'X':
5809       if (GET_CODE (x) == MEM
5810           && LEGITIMATE_INDEXED_ADDRESS_P (XEXP (x, 0), 0))
5811         putc ('x', file);
5812       return;
5813
5814     case 'Y':
5815       /* Like 'L', for third word of TImode  */
5816       if (GET_CODE (x) == REG)
5817         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
5818       else if (GET_CODE (x) == MEM)
5819         {
5820           if (GET_CODE (XEXP (x, 0)) == PRE_INC
5821               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
5822             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
5823           else
5824             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
5825           if (small_data_operand (x, GET_MODE (x)))
5826             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
5827                      reg_names[SMALL_DATA_REG]);
5828         }
5829       return;
5830                             
5831     case 'z':
5832       /* X is a SYMBOL_REF.  Write out the name preceded by a
5833          period and without any trailing data in brackets.  Used for function
5834          names.  If we are configured for System V (or the embedded ABI) on
5835          the PowerPC, do not emit the period, since those systems do not use
5836          TOCs and the like.  */
5837       if (GET_CODE (x) != SYMBOL_REF)
5838         abort ();
5839
5840       if (XSTR (x, 0)[0] != '.')
5841         {
5842           switch (DEFAULT_ABI)
5843             {
5844             default:
5845               abort ();
5846
5847             case ABI_AIX:
5848               putc ('.', file);
5849               break;
5850
5851             case ABI_V4:
5852             case ABI_AIX_NODESC:
5853             case ABI_DARWIN:
5854               break;
5855             }
5856         }
5857 #if TARGET_AIX
5858       RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
5859 #else
5860       assemble_name (file, XSTR (x, 0));
5861 #endif
5862       return;
5863
5864     case 'Z':
5865       /* Like 'L', for last word of TImode.  */
5866       if (GET_CODE (x) == REG)
5867         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
5868       else if (GET_CODE (x) == MEM)
5869         {
5870           if (GET_CODE (XEXP (x, 0)) == PRE_INC
5871               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
5872             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
5873           else
5874             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
5875           if (small_data_operand (x, GET_MODE (x)))
5876             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
5877                      reg_names[SMALL_DATA_REG]);
5878         }
5879       return;
5880
5881       /* Print AltiVec memory operand.  */
5882     case 'y':
5883       {
5884         rtx tmp;
5885
5886         if (GET_CODE (x) != MEM)
5887           abort ();
5888
5889         tmp = XEXP (x, 0);
5890
5891         if (GET_CODE (tmp) == REG)
5892           fprintf (file, "0, %s", reg_names[REGNO (tmp)]);
5893         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
5894           {
5895             if (REGNO (XEXP (tmp, 0)) == 0)
5896               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
5897                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
5898             else
5899               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
5900                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
5901           }
5902         else
5903           abort ();
5904         break;
5905       }
5906                             
5907     case 0:
5908       if (GET_CODE (x) == REG)
5909         fprintf (file, "%s", reg_names[REGNO (x)]);
5910       else if (GET_CODE (x) == MEM)
5911         {
5912           /* We need to handle PRE_INC and PRE_DEC here, since we need to
5913              know the width from the mode.  */
5914           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
5915             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
5916                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
5917           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
5918             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
5919                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
5920           else
5921             output_address (XEXP (x, 0));
5922         }
5923       else
5924         output_addr_const (file, x);
5925       return;
5926
5927     default:
5928       output_operand_lossage ("invalid %%xn code");
5929     }
5930 }
5931 \f
5932 /* Print the address of an operand.  */
5933
5934 void
5935 print_operand_address (file, x)
5936      FILE *file;
5937      rtx x;
5938 {
5939   if (GET_CODE (x) == REG)
5940     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
5941   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
5942            || GET_CODE (x) == LABEL_REF)
5943     {
5944       output_addr_const (file, x);
5945       if (small_data_operand (x, GET_MODE (x)))
5946         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
5947                  reg_names[SMALL_DATA_REG]);
5948       else if (TARGET_TOC)
5949         abort ();
5950     }
5951   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
5952     {
5953       if (REGNO (XEXP (x, 0)) == 0)
5954         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
5955                  reg_names[ REGNO (XEXP (x, 0)) ]);
5956       else
5957         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
5958                  reg_names[ REGNO (XEXP (x, 1)) ]);
5959     }
5960   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
5961     {
5962       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (x, 1)));
5963       fprintf (file, "(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
5964     }
5965 #if TARGET_ELF
5966   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
5967            && CONSTANT_P (XEXP (x, 1)))
5968     {
5969       output_addr_const (file, XEXP (x, 1));
5970       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
5971     }
5972 #endif
5973 #if TARGET_MACHO
5974   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
5975            && CONSTANT_P (XEXP (x, 1)))
5976     {
5977       fprintf (file, "lo16(");
5978       output_addr_const (file, XEXP (x, 1));
5979       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
5980     }
5981 #endif
5982   else if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
5983     {
5984       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
5985         {
5986           rtx contains_minus = XEXP (x, 1);
5987           rtx minus, symref;
5988           const char *name;
5989           
5990           /* Find the (minus (sym) (toc)) buried in X, and temporarily
5991              turn it into (sym) for output_addr_const.  */
5992           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
5993             contains_minus = XEXP (contains_minus, 0);
5994
5995           minus = XEXP (contains_minus, 0);
5996           symref = XEXP (minus, 0);
5997           XEXP (contains_minus, 0) = symref;
5998           if (TARGET_ELF)
5999             {
6000               char *newname;
6001
6002               name = XSTR (symref, 0);
6003               newname = alloca (strlen (name) + sizeof ("@toc"));
6004               strcpy (newname, name);
6005               strcat (newname, "@toc");
6006               XSTR (symref, 0) = newname;
6007             }
6008           output_addr_const (file, XEXP (x, 1));
6009           if (TARGET_ELF)
6010             XSTR (symref, 0) = name;
6011           XEXP (contains_minus, 0) = minus;
6012         }
6013       else
6014         output_addr_const (file, XEXP (x, 1));
6015
6016       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
6017     }
6018   else
6019     abort ();
6020 }
6021 \f
6022 enum rtx_code
6023 rs6000_reverse_condition (mode, code)
6024      enum machine_mode mode;
6025      enum rtx_code code;
6026 {
6027   /* Reversal of FP compares takes care -- an ordered compare
6028      becomes an unordered compare and vice versa.  */
6029   if (mode == CCFPmode)
6030     return reverse_condition_maybe_unordered (code);
6031   else
6032     return reverse_condition (code);
6033 }
6034
6035 /* Generate a compare for CODE.  Return a brand-new rtx that
6036    represents the result of the compare.  */
6037
6038 static rtx
6039 rs6000_generate_compare (code)
6040      enum rtx_code code;
6041 {
6042   enum machine_mode comp_mode;
6043   rtx compare_result;
6044
6045   if (rs6000_compare_fp_p)
6046     comp_mode = CCFPmode;
6047   else if (code == GTU || code == LTU
6048           || code == GEU || code == LEU)
6049     comp_mode = CCUNSmode;
6050   else
6051     comp_mode = CCmode;
6052
6053   /* First, the compare.  */
6054   compare_result = gen_reg_rtx (comp_mode);
6055   emit_insn (gen_rtx_SET (VOIDmode, compare_result,
6056                           gen_rtx_COMPARE (comp_mode,
6057                                            rs6000_compare_op0, 
6058                                            rs6000_compare_op1)));
6059   
6060   /* Some kinds of FP comparisons need an OR operation;
6061      except for flag_unsafe_math_optimizations we don't bother.  */
6062   if (rs6000_compare_fp_p
6063       && ! flag_unsafe_math_optimizations
6064       && (code == LE || code == GE
6065           || code == UNEQ || code == LTGT
6066           || code == UNGT || code == UNLT))
6067     {
6068       enum rtx_code or1, or2;
6069       rtx or1_rtx, or2_rtx, compare2_rtx;
6070       rtx or_result = gen_reg_rtx (CCEQmode);
6071       
6072       switch (code)
6073         {
6074         case LE: or1 = LT;  or2 = EQ;  break;
6075         case GE: or1 = GT;  or2 = EQ;  break;
6076         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
6077         case LTGT: or1 = LT;  or2 = GT;  break;
6078         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
6079         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
6080         default:  abort ();
6081         }
6082       validate_condition_mode (or1, comp_mode);
6083       validate_condition_mode (or2, comp_mode);
6084       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
6085       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
6086       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
6087                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
6088                                       const_true_rtx);
6089       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
6090
6091       compare_result = or_result;
6092       code = EQ;
6093     }
6094
6095   validate_condition_mode (code, GET_MODE (compare_result));
6096   
6097   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
6098 }
6099
6100
6101 /* Emit the RTL for an sCOND pattern.  */
6102
6103 void
6104 rs6000_emit_sCOND (code, result)
6105      enum rtx_code code;
6106      rtx result;
6107 {
6108   rtx condition_rtx;
6109   enum machine_mode op_mode;
6110
6111   condition_rtx = rs6000_generate_compare (code);
6112
6113   op_mode = GET_MODE (rs6000_compare_op0);
6114   if (op_mode == VOIDmode)
6115     op_mode = GET_MODE (rs6000_compare_op1);
6116
6117   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
6118     {
6119       PUT_MODE (condition_rtx, DImode);
6120       convert_move (result, condition_rtx, 0);
6121     }
6122   else
6123     {
6124       PUT_MODE (condition_rtx, SImode);
6125       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
6126     }
6127 }
6128
6129 /* Emit a branch of kind CODE to location LOC.  */
6130
6131 void
6132 rs6000_emit_cbranch (code, loc)
6133      enum rtx_code code;
6134      rtx loc;
6135 {
6136   rtx condition_rtx, loc_ref;
6137
6138   condition_rtx = rs6000_generate_compare (code);
6139   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
6140   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
6141                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
6142                                                      loc_ref, pc_rtx)));
6143 }
6144
6145 /* Return the string to output a conditional branch to LABEL, which is
6146    the operand number of the label, or -1 if the branch is really a
6147    conditional return.  
6148
6149    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
6150    condition code register and its mode specifies what kind of
6151    comparison we made.
6152
6153    REVERSED is non-zero if we should reverse the sense of the comparison.
6154
6155    INSN is the insn.  */
6156
6157 char *
6158 output_cbranch (op, label, reversed, insn)
6159      rtx op;
6160      const char * label;
6161      int reversed;
6162      rtx insn;
6163 {
6164   static char string[64];
6165   enum rtx_code code = GET_CODE (op);
6166   rtx cc_reg = XEXP (op, 0);
6167   enum machine_mode mode = GET_MODE (cc_reg);
6168   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
6169   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
6170   int really_reversed = reversed ^ need_longbranch;
6171   char *s = string;
6172   const char *ccode;
6173   const char *pred;
6174   rtx note;
6175
6176   validate_condition_mode (code, mode);
6177
6178   /* Work out which way this really branches.  We could use
6179      reverse_condition_maybe_unordered here always but this
6180      makes the resulting assembler clearer.  */
6181   if (really_reversed)
6182     code = rs6000_reverse_condition (mode, code);
6183
6184   switch (code)
6185     {
6186       /* Not all of these are actually distinct opcodes, but
6187          we distinguish them for clarity of the resulting assembler.  */
6188     case NE: case LTGT:
6189       ccode = "ne"; break;
6190     case EQ: case UNEQ:
6191       ccode = "eq"; break;
6192     case GE: case GEU: 
6193       ccode = "ge"; break;
6194     case GT: case GTU: case UNGT: 
6195       ccode = "gt"; break;
6196     case LE: case LEU: 
6197       ccode = "le"; break;
6198     case LT: case LTU: case UNLT: 
6199       ccode = "lt"; break;
6200     case UNORDERED: ccode = "un"; break;
6201     case ORDERED: ccode = "nu"; break;
6202     case UNGE: ccode = "nl"; break;
6203     case UNLE: ccode = "ng"; break;
6204     default:
6205       abort ();
6206     }
6207   
6208   /* Maybe we have a guess as to how likely the branch is.  
6209      The old mnemonics don't have a way to specify this information.  */
6210   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
6211   if (note != NULL_RTX)
6212     {
6213       /* PROB is the difference from 50%.  */
6214       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
6215       
6216       /* For branches that are very close to 50%, assume not-taken.  */
6217       if (abs (prob) > REG_BR_PROB_BASE / 20
6218           && ((prob > 0) ^ need_longbranch))
6219         pred = "+";
6220       else
6221         pred = "-";
6222     }
6223   else
6224     pred = "";
6225
6226   if (label == NULL)
6227     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
6228   else
6229     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
6230
6231   /* We need to escape any '%' characters in the reg_names string.
6232      Assume they'd only be the first character...  */
6233   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
6234     *s++ = '%';
6235   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
6236
6237   if (label != NULL)
6238     {
6239       /* If the branch distance was too far, we may have to use an
6240          unconditional branch to go the distance.  */
6241       if (need_longbranch)
6242         s += sprintf (s, ",$+8\n\tb %s", label);
6243       else
6244         s += sprintf (s, ",%s", label);
6245     }
6246
6247   return string;
6248 }
6249
6250 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
6251    operands of the last comparison is nonzero/true, FALSE_COND if it
6252    is zero/false.  Return 0 if the hardware has no such operation.  */
6253
6254 int
6255 rs6000_emit_cmove (dest, op, true_cond, false_cond)
6256      rtx dest;
6257      rtx op;
6258      rtx true_cond;
6259      rtx false_cond;
6260 {
6261   enum rtx_code code = GET_CODE (op);
6262   rtx op0 = rs6000_compare_op0;
6263   rtx op1 = rs6000_compare_op1;
6264   REAL_VALUE_TYPE c1;
6265   enum machine_mode mode = GET_MODE (op0);
6266   rtx temp;
6267
6268   /* First, work out if the hardware can do this at all, or
6269      if it's too slow...  */
6270   /* If the comparison is an integer one, since we only have fsel
6271      it'll be cheaper to use a branch.  */
6272   if (! rs6000_compare_fp_p)
6273     return 0;
6274
6275   /* Eliminate half of the comparisons by switching operands, this
6276      makes the remaining code simpler.  */
6277   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
6278       || code == LTGT || code == LT)
6279     {
6280       code = reverse_condition_maybe_unordered (code);
6281       temp = true_cond;
6282       true_cond = false_cond;
6283       false_cond = temp;
6284     }
6285
6286   /* UNEQ and LTGT take four instructions for a comparison with zero,
6287      it'll probably be faster to use a branch here too.  */
6288   if (code == UNEQ)
6289     return 0;
6290   
6291   if (GET_CODE (op1) == CONST_DOUBLE)
6292     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
6293     
6294   /* We're going to try to implement comparions by performing
6295      a subtract, then comparing against zero.  Unfortunately,
6296      Inf - Inf is NaN which is not zero, and so if we don't
6297      know that the the operand is finite and the comparison
6298      would treat EQ different to UNORDERED, we can't do it.  */
6299   if (! flag_unsafe_math_optimizations
6300       && code != GT && code != UNGE
6301       && (GET_CODE (op1) != CONST_DOUBLE || target_isinf (c1))
6302       /* Constructs of the form (a OP b ? a : b) are safe.  */
6303       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
6304           || (! rtx_equal_p (op0, true_cond) 
6305               && ! rtx_equal_p (op1, true_cond))))
6306     return 0;
6307   /* At this point we know we can use fsel.  */
6308
6309   /* Reduce the comparison to a comparison against zero.  */
6310   temp = gen_reg_rtx (mode);
6311   emit_insn (gen_rtx_SET (VOIDmode, temp,
6312                           gen_rtx_MINUS (mode, op0, op1)));
6313   op0 = temp;
6314   op1 = CONST0_RTX (mode);
6315
6316   /* If we don't care about NaNs we can reduce some of the comparisons
6317      down to faster ones.  */
6318   if (flag_unsafe_math_optimizations)
6319     switch (code)
6320       {
6321       case GT:
6322         code = LE;
6323         temp = true_cond;
6324         true_cond = false_cond;
6325         false_cond = temp;
6326         break;
6327       case UNGE:
6328         code = GE;
6329         break;
6330       case UNEQ:
6331         code = EQ;
6332         break;
6333       default:
6334         break;
6335       }
6336
6337   /* Now, reduce everything down to a GE.  */
6338   switch (code)
6339     {
6340     case GE:
6341       break;
6342
6343     case LE:
6344       temp = gen_reg_rtx (mode);
6345       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
6346       op0 = temp;
6347       break;
6348
6349     case ORDERED:
6350       temp = gen_reg_rtx (mode);
6351       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (mode, op0)));
6352       op0 = temp;
6353       break;
6354
6355     case EQ:
6356       temp = gen_reg_rtx (mode);
6357       emit_insn (gen_rtx_SET (VOIDmode, temp, 
6358                               gen_rtx_NEG (mode,
6359                                            gen_rtx_ABS (mode, op0))));
6360       op0 = temp;
6361       break;
6362
6363     case UNGE:
6364       temp = gen_reg_rtx (mode);
6365       emit_insn (gen_rtx_SET (VOIDmode, temp,
6366                               gen_rtx_IF_THEN_ELSE (mode, 
6367                                                     gen_rtx_GE (VOIDmode,
6368                                                                 op0, op1),
6369                                                     true_cond, false_cond)));
6370       false_cond = temp;
6371       true_cond = false_cond;
6372
6373       temp = gen_reg_rtx (mode);
6374       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
6375       op0 = temp;
6376       break;
6377
6378     case GT:
6379       temp = gen_reg_rtx (mode);
6380       emit_insn (gen_rtx_SET (VOIDmode, temp,
6381                               gen_rtx_IF_THEN_ELSE (mode, 
6382                                                     gen_rtx_GE (VOIDmode,
6383                                                                 op0, op1),
6384                                                     true_cond, false_cond)));
6385       true_cond = temp;
6386       false_cond = true_cond;
6387
6388       temp = gen_reg_rtx (mode);
6389       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
6390       op0 = temp;
6391       break;
6392
6393     default:
6394       abort ();
6395     }
6396
6397   emit_insn (gen_rtx_SET (VOIDmode, dest,
6398                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
6399                                                 gen_rtx_GE (VOIDmode,
6400                                                             op0, op1),
6401                                                 true_cond, false_cond)));
6402   return 1;
6403 }
6404
6405 void
6406 rs6000_emit_minmax (dest, code, op0, op1)
6407      rtx dest;
6408      enum rtx_code code;
6409      rtx op0;
6410      rtx op1;
6411 {
6412   enum machine_mode mode = GET_MODE (op0);
6413   rtx target;
6414   if (code == SMAX || code == UMAX)
6415     target = emit_conditional_move (dest, GE, op0, op1, mode, 
6416                                     op0, op1, mode, 0);
6417   else
6418     target = emit_conditional_move (dest, GE, op0, op1, mode, 
6419                                     op1, op0, mode, 0);
6420   if (target == NULL_RTX)
6421     abort ();
6422   if (target != dest)
6423     emit_move_insn (dest, target);
6424 }
6425 \f
6426 /* This page contains routines that are used to determine what the
6427    function prologue and epilogue code will do and write them out.  */
6428
6429 /* Return the first fixed-point register that is required to be
6430    saved. 32 if none.  */
6431
6432 int
6433 first_reg_to_save ()
6434 {
6435   int first_reg;
6436
6437   /* Find lowest numbered live register.  */
6438   for (first_reg = 13; first_reg <= 31; first_reg++)
6439     if (regs_ever_live[first_reg] 
6440         && (! call_used_regs[first_reg]
6441             || (first_reg == PIC_OFFSET_TABLE_REGNUM
6442                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
6443                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
6444       break;
6445
6446   if (profile_flag)
6447     {
6448       /* AIX must save/restore every register that contains a parameter
6449          before/after the .__mcount call plus an additional register
6450          for the static chain, if needed; use registers from 30 down to 22
6451          to do this.  */
6452       if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN)
6453         {
6454           int last_parm_reg, profile_first_reg;
6455
6456           /* Figure out last used parameter register.  The proper thing
6457              to do is to walk incoming args of the function.  A function
6458              might have live parameter registers even if it has no
6459              incoming args.  */
6460           for (last_parm_reg = 10;
6461                last_parm_reg > 2 && ! regs_ever_live [last_parm_reg];
6462                last_parm_reg--)
6463             ;
6464
6465           /* Calculate first reg for saving parameter registers
6466              and static chain.
6467              Skip reg 31 which may contain the frame pointer.  */
6468           profile_first_reg = (33 - last_parm_reg
6469                                - (current_function_needs_context ? 1 : 0));
6470 #if TARGET_MACHO
6471           /* Need to skip another reg to account for R31 being PICBASE
6472              (when flag_pic is set) or R30 being used as the frame
6473              pointer (when flag_pic is not set).  */
6474           --profile_first_reg;
6475 #endif
6476           /* Do not save frame pointer if no parameters needs to be saved.  */
6477           if (profile_first_reg == 31)
6478             profile_first_reg = 32;
6479
6480           if (first_reg > profile_first_reg)
6481             first_reg = profile_first_reg;
6482         }
6483
6484       /* SVR4 may need one register to preserve the static chain.  */
6485       else if (current_function_needs_context)
6486         {
6487           /* Skip reg 31 which may contain the frame pointer.  */
6488           if (first_reg > 30)
6489             first_reg = 30;
6490         }
6491     }
6492
6493 #if TARGET_MACHO
6494   if (flag_pic && current_function_uses_pic_offset_table &&
6495       (first_reg > PIC_OFFSET_TABLE_REGNUM))
6496     return PIC_OFFSET_TABLE_REGNUM;
6497 #endif
6498
6499   return first_reg;
6500 }
6501
6502 /* Similar, for FP regs.  */
6503
6504 int
6505 first_fp_reg_to_save ()
6506 {
6507   int first_reg;
6508
6509   /* Find lowest numbered live register.  */
6510   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
6511     if (regs_ever_live[first_reg])
6512       break;
6513
6514   return first_reg;
6515 }
6516
6517 /* Similar, for AltiVec regs.  */
6518
6519 static int
6520 first_altivec_reg_to_save ()
6521 {
6522   int i;
6523
6524   /* Stack frame remains as is unless we are in AltiVec ABI.  */
6525   if (! TARGET_ALTIVEC_ABI)
6526     return LAST_ALTIVEC_REGNO + 1;
6527
6528   /* Find lowest numbered live register.  */
6529   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
6530     if (regs_ever_live[i])
6531       break;
6532
6533   return i;
6534 }
6535
6536 /* Return a 32-bit mask of the AltiVec registers we need to set in
6537    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
6538    the 32-bit word is 0.  */
6539
6540 static unsigned int
6541 compute_vrsave_mask ()
6542 {
6543   unsigned int i, mask = 0;
6544
6545   /* First, find out if we use _any_ altivec registers.  */
6546   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6547     if (regs_ever_live[i])
6548       mask |= ALTIVEC_REG_BIT (i);
6549
6550   if (mask == 0)
6551     return mask;
6552
6553   /* Next, add all registers that are call-clobbered.  We do this
6554      because post-reload register optimizers such as regrename_optimize
6555      may choose to use them.  They never change the register class
6556      chosen by reload, so cannot create new uses of altivec registers
6557      if there were none before, so the early exit above is safe.  */
6558   /* ??? Alternately, we could define HARD_REGNO_RENAME_OK to disallow
6559      altivec registers not saved in the mask, which might well make the
6560      adjustments below more effective in eliding the save/restore of
6561      VRSAVE in small functions.  */
6562   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6563     if (call_used_regs[i])
6564       mask |= ALTIVEC_REG_BIT (i);
6565
6566   /* Next, remove the argument registers from the set.  These must
6567      be in the VRSAVE mask set by the caller, so we don't need to add
6568      them in again.  More importantly, the mask we compute here is
6569      used to generate CLOBBERs in the set_vrsave insn, and we do not
6570      wish the argument registers to die.  */
6571   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
6572     mask &= ~ALTIVEC_REG_BIT (i);
6573
6574   /* Similarly, remove the return value from the set.  */
6575   {
6576     bool yes = false;
6577     diddle_return_value (is_altivec_return_reg, &yes);
6578     if (yes)
6579       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
6580   }
6581
6582   return mask;
6583 }
6584
6585 static void
6586 is_altivec_return_reg (reg, xyes)
6587      rtx reg;
6588      void *xyes;
6589 {
6590   bool *yes = (bool *) xyes;
6591   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
6592     *yes = true;
6593 }
6594
6595 \f
6596 /* Calculate the stack information for the current function.  This is
6597    complicated by having two separate calling sequences, the AIX calling
6598    sequence and the V.4 calling sequence.
6599
6600    AIX (and Darwin/Mac OS X) stack frames look like:
6601                                                           32-bit  64-bit
6602         SP----> +---------------------------------------+
6603                 | back chain to caller                  | 0       0
6604                 +---------------------------------------+
6605                 | saved CR                              | 4       8 (8-11)
6606                 +---------------------------------------+
6607                 | saved LR                              | 8       16
6608                 +---------------------------------------+
6609                 | reserved for compilers                | 12      24
6610                 +---------------------------------------+
6611                 | reserved for binders                  | 16      32
6612                 +---------------------------------------+
6613                 | saved TOC pointer                     | 20      40
6614                 +---------------------------------------+
6615                 | Parameter save area (P)               | 24      48
6616                 +---------------------------------------+
6617                 | Alloca space (A)                      | 24+P    etc.
6618                 +---------------------------------------+
6619                 | Local variable space (L)              | 24+P+A
6620                 +---------------------------------------+
6621                 | Float/int conversion temporary (X)    | 24+P+A+L
6622                 +---------------------------------------+
6623                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
6624                 +---------------------------------------+
6625                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
6626                 +---------------------------------------+
6627                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
6628                 +---------------------------------------+
6629                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
6630                 +---------------------------------------+
6631                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
6632                 +---------------------------------------+
6633         old SP->| back chain to caller's caller         |
6634                 +---------------------------------------+
6635
6636    The required alignment for AIX configurations is two words (i.e., 8
6637    or 16 bytes).
6638
6639
6640    V.4 stack frames look like:
6641
6642         SP----> +---------------------------------------+
6643                 | back chain to caller                  | 0
6644                 +---------------------------------------+
6645                 | caller's saved LR                     | 4
6646                 +---------------------------------------+
6647                 | Parameter save area (P)               | 8
6648                 +---------------------------------------+
6649                 | Alloca space (A)                      | 8+P
6650                 +---------------------------------------+    
6651                 | Varargs save area (V)                 | 8+P+A
6652                 +---------------------------------------+    
6653                 | Local variable space (L)              | 8+P+A+V
6654                 +---------------------------------------+    
6655                 | Float/int conversion temporary (X)    | 8+P+A+V+L
6656                 +---------------------------------------+
6657                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
6658                 +---------------------------------------+
6659                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
6660                 +---------------------------------------+
6661                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
6662                 +---------------------------------------+
6663                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
6664                 +---------------------------------------+    
6665                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
6666                 +---------------------------------------+    
6667                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
6668                 +---------------------------------------+
6669         old SP->| back chain to caller's caller         |
6670                 +---------------------------------------+
6671
6672    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
6673    given.  (But note below and in sysv4.h that we require only 8 and
6674    may round up the size of our stack frame anyways.  The historical
6675    reason is early versions of powerpc-linux which didn't properly
6676    align the stack at program startup.  A happy side-effect is that
6677    -mno-eabi libraries can be used with -meabi programs.)
6678
6679    The EABI configuration defaults to the V.4 layout, unless
6680    -mcall-aix is used, in which case the AIX layout is used.  However,
6681    the stack alignment requirements may differ.  If -mno-eabi is not
6682    given, the required stack alignment is 8 bytes; if -mno-eabi is
6683    given, the required alignment is 16 bytes.  (But see V.4 comment
6684    above.)  */
6685
6686 #ifndef ABI_STACK_BOUNDARY
6687 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
6688 #endif
6689
6690 rs6000_stack_t *
6691 rs6000_stack_info ()
6692 {
6693   static rs6000_stack_t info, zero_info;
6694   rs6000_stack_t *info_ptr = &info;
6695   int reg_size = TARGET_POWERPC64 ? 8 : 4;
6696   enum rs6000_abi abi;
6697   int ehrd_size;
6698   int total_raw_size;
6699
6700   /* Zero all fields portably.  */
6701   info = zero_info;
6702
6703   /* Select which calling sequence.  */
6704   info_ptr->abi = abi = DEFAULT_ABI;
6705
6706   /* Calculate which registers need to be saved & save area size.  */
6707   info_ptr->first_gp_reg_save = first_reg_to_save ();
6708   /* Assume that we will have to save PIC_OFFSET_TABLE_REGNUM, 
6709      even if it currently looks like we won't.  */
6710   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
6711        || (flag_pic == 1 && abi == ABI_V4)
6712        || (flag_pic && abi == ABI_DARWIN))
6713       && info_ptr->first_gp_reg_save > PIC_OFFSET_TABLE_REGNUM)
6714     info_ptr->gp_size = reg_size * (32 - PIC_OFFSET_TABLE_REGNUM);
6715   else
6716     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
6717
6718   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
6719   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
6720
6721   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
6722   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
6723                                  - info_ptr->first_altivec_reg_save);
6724
6725   /* Does this function call anything?  */
6726   info_ptr->calls_p = (! current_function_is_leaf
6727                        || cfun->machine->ra_needs_full_frame);
6728
6729   /* Determine if we need to save the link register.  */
6730   if (rs6000_ra_ever_killed ()
6731       || (DEFAULT_ABI == ABI_AIX && profile_flag)
6732 #ifdef TARGET_RELOCATABLE
6733       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
6734 #endif
6735       || (info_ptr->first_fp_reg_save != 64
6736           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
6737       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
6738       || (abi == ABI_V4 && current_function_calls_alloca)
6739       || (DEFAULT_ABI == ABI_DARWIN
6740           && flag_pic
6741           && current_function_uses_pic_offset_table)
6742       || info_ptr->calls_p)
6743     {
6744       info_ptr->lr_save_p = 1;
6745       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
6746     }
6747
6748   /* Determine if we need to save the condition code registers.  */
6749   if (regs_ever_live[CR2_REGNO] 
6750       || regs_ever_live[CR3_REGNO]
6751       || regs_ever_live[CR4_REGNO])
6752     {
6753       info_ptr->cr_save_p = 1;
6754       if (abi == ABI_V4)
6755         info_ptr->cr_size = reg_size;
6756     }
6757
6758   /* If the current function calls __builtin_eh_return, then we need
6759      to allocate stack space for registers that will hold data for
6760      the exception handler.  */
6761   if (current_function_calls_eh_return)
6762     {
6763       unsigned int i;
6764       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
6765         continue;
6766       ehrd_size = i * UNITS_PER_WORD;
6767     }
6768   else
6769     ehrd_size = 0;
6770
6771   /* Determine various sizes.  */
6772   info_ptr->reg_size     = reg_size;
6773   info_ptr->fixed_size   = RS6000_SAVE_AREA;
6774   info_ptr->varargs_size = RS6000_VARARGS_AREA;
6775   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
6776   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
6777                                          8);
6778
6779   if (TARGET_ALTIVEC_ABI)
6780     {
6781       info_ptr->vrsave_mask = compute_vrsave_mask ();
6782       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
6783     }
6784   else
6785     {
6786       info_ptr->vrsave_mask = 0;
6787       info_ptr->vrsave_size = 0;
6788     }
6789
6790   /* Calculate the offsets.  */
6791   switch (abi)
6792     {
6793     case ABI_NONE:
6794     default:
6795       abort ();
6796
6797     case ABI_AIX:
6798     case ABI_AIX_NODESC:
6799     case ABI_DARWIN:
6800       info_ptr->fp_save_offset   = - info_ptr->fp_size;
6801       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
6802
6803       if (TARGET_ALTIVEC_ABI)
6804         {
6805           info_ptr->vrsave_save_offset
6806             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
6807
6808           /* Align stack so vector save area is on a quadword boundary.  */
6809           if (info_ptr->altivec_size != 0)
6810             info_ptr->altivec_padding_size
6811               = 16 - (-info_ptr->vrsave_save_offset % 16);
6812           else
6813             info_ptr->altivec_padding_size = 0;
6814
6815           info_ptr->altivec_save_offset
6816             = info_ptr->vrsave_save_offset
6817             - info_ptr->altivec_padding_size
6818             - info_ptr->altivec_size;
6819
6820           /* Adjust for AltiVec case.  */
6821           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
6822         }
6823       else
6824         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
6825       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
6826       info_ptr->lr_save_offset   = 2*reg_size;
6827       break;
6828
6829     case ABI_V4:
6830       info_ptr->fp_save_offset   = - info_ptr->fp_size;
6831       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
6832       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
6833
6834       if (TARGET_ALTIVEC_ABI)
6835         {
6836           info_ptr->vrsave_save_offset
6837             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
6838
6839           /* Align stack so vector save area is on a quadword boundary.  */
6840           if (info_ptr->altivec_size != 0)
6841             info_ptr->altivec_padding_size
6842               = 16 - (-info_ptr->vrsave_save_offset % 16);
6843           else
6844             info_ptr->altivec_padding_size = 0;
6845
6846           info_ptr->altivec_save_offset
6847             = info_ptr->vrsave_save_offset
6848             - info_ptr->altivec_padding_size
6849             - info_ptr->altivec_size;
6850
6851           /* Adjust for AltiVec case.  */
6852           info_ptr->toc_save_offset
6853             = info_ptr->altivec_save_offset - info_ptr->toc_size;
6854         }
6855       else
6856         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
6857       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
6858       info_ptr->lr_save_offset   = reg_size;
6859       break;
6860     }
6861
6862   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
6863                                          + info_ptr->gp_size
6864                                          + info_ptr->altivec_size
6865                                          + info_ptr->altivec_padding_size
6866                                          + info_ptr->vrsave_size
6867                                          + ehrd_size
6868                                          + info_ptr->cr_size
6869                                          + info_ptr->lr_size
6870                                          + info_ptr->vrsave_size
6871                                          + info_ptr->toc_size,
6872                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
6873                                          ? 16 : 8);
6874
6875   total_raw_size         = (info_ptr->vars_size
6876                             + info_ptr->parm_size
6877                             + info_ptr->save_size
6878                             + info_ptr->varargs_size
6879                             + info_ptr->fixed_size);
6880
6881   info_ptr->total_size =
6882     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
6883
6884   /* Determine if we need to allocate any stack frame:
6885
6886      For AIX we need to push the stack if a frame pointer is needed
6887      (because the stack might be dynamically adjusted), if we are
6888      debugging, if we make calls, or if the sum of fp_save, gp_save,
6889      and local variables are more than the space needed to save all
6890      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
6891      + 18*8 = 288 (GPR13 reserved).
6892
6893      For V.4 we don't have the stack cushion that AIX uses, but assume
6894      that the debugger can handle stackless frames.  */
6895
6896   if (info_ptr->calls_p)
6897     info_ptr->push_p = 1;
6898
6899   else if (abi == ABI_V4)
6900     info_ptr->push_p = (total_raw_size > info_ptr->fixed_size
6901                         || info_ptr->calls_p);
6902
6903   else
6904     info_ptr->push_p = (frame_pointer_needed
6905                         || (abi != ABI_DARWIN && write_symbols != NO_DEBUG)
6906                         || ((total_raw_size - info_ptr->fixed_size)
6907                             > (TARGET_32BIT ? 220 : 288)));
6908
6909   /* Zero offsets if we're not saving those registers.  */
6910   if (info_ptr->fp_size == 0)
6911     info_ptr->fp_save_offset = 0;
6912
6913   if (info_ptr->gp_size == 0)
6914     info_ptr->gp_save_offset = 0;
6915
6916   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
6917     info_ptr->altivec_save_offset = 0;
6918
6919   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
6920     info_ptr->vrsave_save_offset = 0;
6921
6922   if (! info_ptr->lr_save_p)
6923     info_ptr->lr_save_offset = 0;
6924
6925   if (! info_ptr->cr_save_p)
6926     info_ptr->cr_save_offset = 0;
6927
6928   if (! info_ptr->toc_save_p)
6929     info_ptr->toc_save_offset = 0;
6930
6931   return info_ptr;
6932 }
6933
6934 void
6935 debug_stack_info (info)
6936      rs6000_stack_t *info;
6937 {
6938   const char *abi_string;
6939
6940   if (! info)
6941     info = rs6000_stack_info ();
6942
6943   fprintf (stderr, "\nStack information for function %s:\n",
6944            ((current_function_decl && DECL_NAME (current_function_decl))
6945             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
6946             : "<unknown>"));
6947
6948   switch (info->abi)
6949     {
6950     default:             abi_string = "Unknown";        break;
6951     case ABI_NONE:       abi_string = "NONE";           break;
6952     case ABI_AIX:
6953     case ABI_AIX_NODESC: abi_string = "AIX";            break;
6954     case ABI_DARWIN:     abi_string = "Darwin";         break;
6955     case ABI_V4:         abi_string = "V.4";            break;
6956     }
6957
6958   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
6959
6960   if (TARGET_ALTIVEC_ABI)
6961     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
6962
6963   if (info->first_gp_reg_save != 32)
6964     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
6965
6966   if (info->first_fp_reg_save != 64)
6967     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
6968
6969   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
6970     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
6971              info->first_altivec_reg_save);
6972
6973   if (info->lr_save_p)
6974     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
6975
6976   if (info->cr_save_p)
6977     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
6978
6979   if (info->toc_save_p)
6980     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
6981
6982   if (info->vrsave_mask)
6983     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
6984
6985   if (info->push_p)
6986     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
6987
6988   if (info->calls_p)
6989     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
6990
6991   if (info->gp_save_offset)
6992     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
6993
6994   if (info->fp_save_offset)
6995     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
6996
6997   if (info->altivec_save_offset)
6998     fprintf (stderr, "\taltivec_save_offset = %5d\n",
6999              info->altivec_save_offset);
7000
7001   if (info->vrsave_save_offset)
7002     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
7003              info->vrsave_save_offset);
7004
7005   if (info->lr_save_offset)
7006     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
7007
7008   if (info->cr_save_offset)
7009     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
7010
7011   if (info->toc_save_offset)
7012     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
7013
7014   if (info->varargs_save_offset)
7015     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
7016
7017   if (info->total_size)
7018     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
7019
7020   if (info->varargs_size)
7021     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
7022
7023   if (info->vars_size)
7024     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
7025
7026   if (info->parm_size)
7027     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
7028
7029   if (info->fixed_size)
7030     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
7031
7032   if (info->gp_size)
7033     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
7034
7035   if (info->fp_size)
7036     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
7037
7038   if (info->altivec_size)
7039     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
7040
7041   if (info->vrsave_size)
7042     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
7043
7044   if (info->altivec_padding_size)
7045     fprintf (stderr, "\taltivec_padding_size= %5d\n",
7046              info->altivec_padding_size);
7047
7048   if (info->lr_size)
7049     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
7050
7051   if (info->cr_size)
7052     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
7053
7054   if (info->toc_size)
7055     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
7056
7057   if (info->save_size)
7058     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
7059
7060   if (info->reg_size != 4)
7061     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
7062
7063   fprintf (stderr, "\n");
7064 }
7065
7066 rtx
7067 rs6000_return_addr (count, frame)
7068      int count;
7069      rtx frame;
7070 {
7071   /* Currently we don't optimize very well between prolog and body
7072      code and for PIC code the code can be actually quite bad, so
7073      don't try to be too clever here.  */
7074   if (count != 0
7075       || flag_pic != 0
7076       || DEFAULT_ABI == ABI_AIX
7077       || DEFAULT_ABI == ABI_AIX_NODESC)
7078     {
7079       cfun->machine->ra_needs_full_frame = 1;
7080
7081       return
7082         gen_rtx_MEM
7083           (Pmode,
7084            memory_address
7085            (Pmode,
7086             plus_constant (copy_to_reg
7087                            (gen_rtx_MEM (Pmode,
7088                                          memory_address (Pmode, frame))),
7089                            RETURN_ADDRESS_OFFSET)));
7090     }
7091
7092   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
7093 }
7094
7095 static int
7096 rs6000_ra_ever_killed ()
7097 {
7098   rtx top;
7099
7100 #ifdef ASM_OUTPUT_MI_THUNK
7101   if (current_function_is_thunk)
7102     return 0;
7103 #endif
7104   if (!has_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM)
7105       || cfun->machine->ra_needs_full_frame)
7106     return regs_ever_live[LINK_REGISTER_REGNUM];
7107
7108   push_topmost_sequence ();
7109   top = get_insns ();
7110   pop_topmost_sequence ();
7111
7112   return reg_set_between_p (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
7113                             top, NULL_RTX);
7114 }
7115 \f
7116 /* Add a REG_MAYBE_DEAD note to the insn.  */
7117 static void
7118 rs6000_maybe_dead (insn)
7119      rtx insn;
7120 {
7121   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
7122                                         const0_rtx,
7123                                         REG_NOTES (insn));
7124 }
7125
7126 /* Emit instructions needed to load the TOC register.
7127    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
7128    a constant pool; or for SVR4 -fpic.  */
7129
7130 void
7131 rs6000_emit_load_toc_table (fromprolog)
7132      int fromprolog;
7133 {
7134   rtx dest;
7135   dest = gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
7136
7137   if (TARGET_ELF && DEFAULT_ABI != ABI_AIX)
7138     {
7139       if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
7140         {
7141           rtx temp = (fromprolog 
7142                       ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
7143                       : gen_reg_rtx (Pmode));
7144           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_pic_si (temp)));
7145           rs6000_maybe_dead (emit_move_insn (dest, temp));
7146         }
7147       else if (flag_pic == 2)
7148         {
7149           char buf[30];
7150           rtx tempLR = (fromprolog 
7151                         ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
7152                         : gen_reg_rtx (Pmode));
7153           rtx temp0 = (fromprolog
7154                         ? gen_rtx_REG (Pmode, 0)
7155                         : gen_reg_rtx (Pmode));
7156           rtx symF;
7157
7158           /* possibly create the toc section */
7159           if (! toc_initialized)
7160             {
7161               toc_section ();
7162               function_section (current_function_decl);
7163             }
7164   
7165           if (fromprolog)
7166             {
7167               rtx symL;
7168           
7169               ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
7170               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7171
7172               ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
7173               symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7174
7175               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR, 
7176                                                                    symF)));
7177               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
7178               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
7179                                                                    symL,
7180                                                                    symF)));
7181             }
7182           else
7183             {
7184               rtx tocsym;
7185               static int reload_toc_labelno = 0;
7186
7187               tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
7188
7189               ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
7190               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7191
7192               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1b (tempLR, 
7193                                                                     symF, 
7194                                                                     tocsym)));
7195               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
7196               rs6000_maybe_dead (emit_move_insn (temp0, 
7197                                                  gen_rtx_MEM (Pmode, dest)));
7198             }
7199           rs6000_maybe_dead (emit_insn (gen_addsi3 (dest, temp0, dest)));
7200         }
7201       else if (flag_pic == 0 && TARGET_MINIMAL_TOC)
7202         {
7203           /* This is for AIX code running in non-PIC ELF.  */
7204           char buf[30];
7205           rtx realsym;
7206           ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
7207           realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7208           
7209           rs6000_maybe_dead (emit_insn (gen_elf_high (dest, realsym)));
7210           rs6000_maybe_dead (emit_insn (gen_elf_low (dest, dest, realsym)));
7211         }
7212       else
7213         abort ();
7214     }
7215   else
7216     {
7217       if (TARGET_32BIT)
7218         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_si (dest)));
7219       else
7220         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_di (dest)));
7221     }
7222 }
7223
7224 int   
7225 get_TOC_alias_set ()
7226 {
7227     static int set = -1;
7228     if (set == -1)
7229       set = new_alias_set ();
7230     return set;
7231 }   
7232
7233 /* This retuns nonzero if the current function uses the TOC.  This is
7234    determined by the presence of (unspec ... 7), which is generated by
7235    the various load_toc_* patterns.  */
7236
7237 int
7238 uses_TOC () 
7239 {
7240     rtx insn;
7241
7242     for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7243       if (INSN_P (insn))
7244         {
7245           rtx pat = PATTERN (insn);
7246           int i;
7247
7248           if (GET_CODE (pat) == PARALLEL) 
7249             for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
7250               if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == UNSPEC 
7251                  && XINT (XVECEXP (PATTERN (insn), 0, i), 1) == 7)
7252                   return 1;
7253         }
7254     return 0;
7255 }
7256
7257 rtx
7258 create_TOC_reference (symbol) 
7259     rtx symbol;
7260 {
7261   return gen_rtx_PLUS (Pmode, 
7262            gen_rtx_REG (Pmode, TOC_REGISTER),
7263              gen_rtx_CONST (Pmode, 
7264                gen_rtx_MINUS (Pmode, symbol, 
7265                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
7266 }
7267
7268 #if TARGET_AIX
7269 /* __throw will restore its own return address to be the same as the
7270    return address of the function that the throw is being made to.
7271    This is unfortunate, because we want to check the original
7272    return address to see if we need to restore the TOC.
7273    So we have to squirrel it away here.  
7274    This is used only in compiling __throw and __rethrow. 
7275
7276    Most of this code should be removed by CSE.  */
7277 static rtx insn_after_throw;
7278
7279 /* This does the saving...  */
7280 void
7281 rs6000_aix_emit_builtin_unwind_init ()
7282 {
7283   rtx mem;
7284   rtx stack_top = gen_reg_rtx (Pmode);
7285   rtx opcode_addr = gen_reg_rtx (Pmode);
7286
7287   insn_after_throw = gen_reg_rtx (SImode);
7288
7289   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
7290   emit_move_insn (stack_top, mem);
7291
7292   mem = gen_rtx_MEM (Pmode, 
7293                      gen_rtx_PLUS (Pmode, stack_top, 
7294                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
7295   emit_move_insn (opcode_addr, mem);
7296   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
7297 }
7298
7299 /* Emit insns to _restore_ the TOC register, at runtime (specifically
7300    in _eh.o).  Only used on AIX.
7301
7302    The idea is that on AIX, function calls look like this:
7303         bl  somefunction-trampoline
7304         lwz r2,20(sp)
7305
7306    and later,
7307         somefunction-trampoline:
7308         stw r2,20(sp)
7309          ... load function address in the count register ...
7310         bctr
7311    or like this, if the linker determines that this is not a cross-module call
7312    and so the TOC need not be restored:
7313         bl  somefunction
7314         nop
7315    or like this, if the compiler could determine that this is not a
7316    cross-module call:
7317         bl  somefunction
7318    now, the tricky bit here is that register 2 is saved and restored
7319    by the _linker_, so we can't readily generate debugging information
7320    for it.  So we need to go back up the call chain looking at the
7321    insns at return addresses to see which calls saved the TOC register
7322    and so see where it gets restored from.
7323
7324    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
7325    just before the actual epilogue.
7326
7327    On the bright side, this incurs no space or time overhead unless an
7328    exception is thrown, except for the extra code in libgcc.a.  
7329
7330    The parameter STACKSIZE is a register containing (at runtime)
7331    the amount to be popped off the stack in addition to the stack frame
7332    of this routine (which will be __throw or __rethrow, and so is
7333    guaranteed to have a stack frame).  */
7334
7335 void
7336 rs6000_emit_eh_toc_restore (stacksize)
7337      rtx stacksize;
7338 {
7339   rtx top_of_stack;
7340   rtx bottom_of_stack = gen_reg_rtx (Pmode);
7341   rtx tocompare = gen_reg_rtx (SImode);
7342   rtx opcode = gen_reg_rtx (SImode);
7343   rtx opcode_addr = gen_reg_rtx (Pmode);
7344   rtx mem;
7345   rtx loop_start = gen_label_rtx ();
7346   rtx no_toc_restore_needed = gen_label_rtx ();
7347   rtx loop_exit = gen_label_rtx ();
7348   
7349   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
7350   set_mem_alias_set (mem, rs6000_sr_alias_set);
7351   emit_move_insn (bottom_of_stack, mem);
7352
7353   top_of_stack = expand_binop (Pmode, add_optab, 
7354                                bottom_of_stack, stacksize,
7355                                NULL_RTX, 1, OPTAB_WIDEN);
7356
7357   emit_move_insn (tocompare, 
7358                   GEN_INT (trunc_int_for_mode (TARGET_32BIT 
7359                                                ? 0x80410014 
7360                                                : 0xE8410028, SImode)));
7361
7362   if (insn_after_throw == NULL_RTX)
7363     abort ();
7364   emit_move_insn (opcode, insn_after_throw);
7365   
7366   emit_note (NULL, NOTE_INSN_LOOP_BEG);
7367   emit_label (loop_start);
7368   
7369   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
7370                            SImode, NULL_RTX, NULL_RTX,
7371                            no_toc_restore_needed);
7372   
7373   mem = gen_rtx_MEM (Pmode, 
7374                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
7375                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
7376   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
7377
7378   emit_label (no_toc_restore_needed);
7379   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
7380                            Pmode, NULL_RTX, NULL_RTX,
7381                            loop_exit);
7382
7383   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
7384   set_mem_alias_set (mem, rs6000_sr_alias_set);
7385   emit_move_insn (bottom_of_stack, mem);
7386   
7387   mem = gen_rtx_MEM (Pmode, 
7388                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
7389                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
7390   emit_move_insn (opcode_addr, mem);
7391   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
7392
7393   emit_note (NULL, NOTE_INSN_LOOP_CONT);
7394   emit_jump (loop_start);
7395   emit_note (NULL, NOTE_INSN_LOOP_END);
7396   emit_label (loop_exit);
7397 }
7398 #endif /* TARGET_AIX */
7399 \f
7400 /* This ties together stack memory (MEM with an alias set of
7401    rs6000_sr_alias_set) and the change to the stack pointer.  */
7402
7403 static void
7404 rs6000_emit_stack_tie ()
7405 {
7406   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
7407
7408   set_mem_alias_set (mem, rs6000_sr_alias_set);
7409   emit_insn (gen_stack_tie (mem));
7410 }
7411
7412 /* Emit the correct code for allocating stack space, as insns.
7413    If COPY_R12, make sure a copy of the old frame is left in r12.
7414    The generated code may use hard register 0 as a temporary.  */
7415
7416 static void
7417 rs6000_emit_allocate_stack (size, copy_r12)
7418      HOST_WIDE_INT size;
7419      int copy_r12;
7420 {
7421   rtx insn;
7422   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
7423   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
7424   rtx todec = GEN_INT (-size);
7425
7426   if (current_function_limit_stack)
7427     {
7428       if (REG_P (stack_limit_rtx)
7429           && REGNO (stack_limit_rtx) > 1 
7430           && REGNO (stack_limit_rtx) <= 31)
7431         {
7432           emit_insn (Pmode == SImode
7433                      ? gen_addsi3 (tmp_reg,
7434                                    stack_limit_rtx,
7435                                    GEN_INT (size))
7436                      : gen_adddi3 (tmp_reg,
7437                                    stack_limit_rtx,
7438                                    GEN_INT (size)));
7439           
7440           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
7441                                     const0_rtx));
7442         }
7443       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
7444                && TARGET_32BIT
7445                && DEFAULT_ABI == ABI_V4)
7446         {
7447           rtx toload = gen_rtx_CONST (VOIDmode,
7448                                       gen_rtx_PLUS (Pmode, 
7449                                                     stack_limit_rtx, 
7450                                                     GEN_INT (size)));
7451           
7452           emit_insn (gen_elf_high (tmp_reg, toload));
7453           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
7454           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
7455                                     const0_rtx));
7456         }
7457       else
7458         warning ("stack limit expression is not supported");
7459     }
7460
7461   if (copy_r12 || ! TARGET_UPDATE)
7462     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
7463
7464   if (TARGET_UPDATE)
7465     {
7466       if (size > 32767)
7467         {
7468           /* Need a note here so that try_split doesn't get confused.  */
7469           if (get_last_insn() == NULL_RTX)
7470             emit_note (0, NOTE_INSN_DELETED);
7471           insn = emit_move_insn (tmp_reg, todec);
7472           try_split (PATTERN (insn), insn, 0);
7473           todec = tmp_reg;
7474         }
7475       
7476       if (Pmode == SImode)
7477         insn = emit_insn (gen_movsi_update (stack_reg, stack_reg, 
7478                                             todec, stack_reg));
7479       else
7480         insn = emit_insn (gen_movdi_update (stack_reg, stack_reg, 
7481                                             todec, stack_reg));
7482     }
7483   else
7484     {
7485       if (Pmode == SImode)
7486         insn = emit_insn (gen_addsi3 (stack_reg, stack_reg, todec));
7487       else
7488         insn = emit_insn (gen_adddi3 (stack_reg, stack_reg, todec));
7489       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
7490                       gen_rtx_REG (Pmode, 12));
7491     }
7492   
7493   RTX_FRAME_RELATED_P (insn) = 1;
7494   REG_NOTES (insn) = 
7495     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
7496                        gen_rtx_SET (VOIDmode, stack_reg, 
7497                                     gen_rtx_PLUS (Pmode, stack_reg,
7498                                                   GEN_INT (-size))),
7499                        REG_NOTES (insn));
7500 }
7501
7502 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
7503    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
7504    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
7505    deduce these equivalences by itself so it wasn't necessary to hold
7506    its hand so much.  */
7507
7508 static void
7509 rs6000_frame_related (insn, reg, val, reg2, rreg)
7510      rtx insn;
7511      rtx reg;
7512      HOST_WIDE_INT val;
7513      rtx reg2;
7514      rtx rreg;
7515 {
7516   rtx real, temp;
7517
7518   real = copy_rtx (PATTERN (insn));
7519
7520   real = replace_rtx (real, reg, 
7521                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
7522                                                         STACK_POINTER_REGNUM),
7523                                     GEN_INT (val)));
7524   
7525   /* We expect that 'real' is either a SET or a PARALLEL containing
7526      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
7527      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
7528
7529   if (GET_CODE (real) == SET)
7530     {
7531       rtx set = real;
7532       
7533       temp = simplify_rtx (SET_SRC (set));
7534       if (temp)
7535         SET_SRC (set) = temp;
7536       temp = simplify_rtx (SET_DEST (set));
7537       if (temp)
7538         SET_DEST (set) = temp;
7539       if (GET_CODE (SET_DEST (set)) == MEM)
7540         {
7541           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
7542           if (temp)
7543             XEXP (SET_DEST (set), 0) = temp;
7544         }
7545     }
7546   else if (GET_CODE (real) == PARALLEL)
7547     {
7548       int i;
7549       for (i = 0; i < XVECLEN (real, 0); i++)
7550         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
7551           {
7552             rtx set = XVECEXP (real, 0, i);
7553             
7554             temp = simplify_rtx (SET_SRC (set));
7555             if (temp)
7556               SET_SRC (set) = temp;
7557             temp = simplify_rtx (SET_DEST (set));
7558             if (temp)
7559               SET_DEST (set) = temp;
7560             if (GET_CODE (SET_DEST (set)) == MEM)
7561               {
7562                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
7563                 if (temp)
7564                   XEXP (SET_DEST (set), 0) = temp;
7565               }
7566             RTX_FRAME_RELATED_P (set) = 1;
7567           }
7568     }
7569   else
7570     abort ();
7571   
7572   if (reg2 != NULL_RTX)
7573     real = replace_rtx (real, reg2, rreg);
7574   
7575   RTX_FRAME_RELATED_P (insn) = 1;
7576   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
7577                                         real,
7578                                         REG_NOTES (insn));
7579 }
7580
7581 /* Returns an insn that has a vrsave set operation with the
7582    appropriate CLOBBERs.  */
7583
7584 static rtx
7585 generate_set_vrsave (reg, info)
7586      rtx reg;
7587      rs6000_stack_t *info;
7588 {
7589   int nclobs, i;
7590   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
7591
7592   clobs[0] = gen_set_vrsave (reg);
7593
7594   nclobs = 1;
7595
7596   /* CLOBBER the registers in the mask.  */
7597
7598   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7599     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
7600       clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
7601                                          gen_rtx_REG (V4SImode, i));
7602
7603   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
7604
7605   for (i = 0; i < nclobs; ++i)
7606     XVECEXP (insn, 0, i) = clobs[i];
7607
7608   return insn;
7609 }
7610
7611 /* Emit function prologue as insns.  */
7612
7613 void
7614 rs6000_emit_prologue ()
7615 {
7616   rs6000_stack_t *info = rs6000_stack_info ();
7617   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
7618   int reg_size = TARGET_POWERPC64 ? 8 : 4;
7619   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
7620   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
7621   rtx frame_reg_rtx = sp_reg_rtx;
7622   rtx cr_save_rtx = NULL;
7623   rtx insn;
7624   int saving_FPRs_inline;
7625   int using_store_multiple;
7626   HOST_WIDE_INT sp_offset = 0;
7627   
7628   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
7629                           && info->first_gp_reg_save < 31);
7630   saving_FPRs_inline = (info->first_fp_reg_save == 64
7631                         || FP_SAVE_INLINE (info->first_fp_reg_save));
7632
7633   /* For V.4, update stack before we do any saving and set back pointer.  */
7634   if (info->push_p && DEFAULT_ABI == ABI_V4)
7635     {
7636       if (info->total_size < 32767)
7637         sp_offset = info->total_size;
7638       else
7639         frame_reg_rtx = frame_ptr_rtx;
7640       rs6000_emit_allocate_stack (info->total_size, 
7641                                   (frame_reg_rtx != sp_reg_rtx
7642                                    && (info->cr_save_p
7643                                        || info->lr_save_p
7644                                        || info->first_fp_reg_save < 64
7645                                        || info->first_gp_reg_save < 32
7646                                        )));
7647       if (frame_reg_rtx != sp_reg_rtx)
7648         rs6000_emit_stack_tie ();
7649     }
7650
7651   /* If we use the link register, get it into r0.  */
7652   if (info->lr_save_p)
7653     emit_move_insn (gen_rtx_REG (Pmode, 0),
7654                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
7655
7656   /* If we need to save CR, put it into r12.  */
7657   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
7658     {
7659       cr_save_rtx = gen_rtx_REG (SImode, 12);
7660       emit_insn (gen_movesi_from_cr (cr_save_rtx));
7661     }
7662
7663   /* Do any required saving of fpr's.  If only one or two to save, do
7664      it ourselves.  Otherwise, call function.  */
7665   if (saving_FPRs_inline)
7666     {
7667       int i;
7668       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
7669         if ((regs_ever_live[info->first_fp_reg_save+i] 
7670              && ! call_used_regs[info->first_fp_reg_save+i]))
7671           {
7672             rtx addr, reg, mem;
7673             reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
7674             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
7675                                  GEN_INT (info->fp_save_offset 
7676                                           + sp_offset 
7677                                           + 8 * i));
7678             mem = gen_rtx_MEM (DFmode, addr);
7679             set_mem_alias_set (mem, rs6000_sr_alias_set);
7680
7681             insn = emit_move_insn (mem, reg);
7682             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7683                                   NULL_RTX, NULL_RTX);
7684           }
7685     }
7686   else if (info->first_fp_reg_save != 64)
7687     {
7688       int i;
7689       char rname[30];
7690       const char *alloc_rname;
7691       rtvec p;
7692       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
7693       
7694       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
7695                                           gen_rtx_REG (Pmode, 
7696                                                        LINK_REGISTER_REGNUM));
7697       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
7698                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
7699       alloc_rname = ggc_strdup (rname);
7700       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
7701                                       gen_rtx_SYMBOL_REF (Pmode,
7702                                                           alloc_rname));
7703       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
7704         {
7705           rtx addr, reg, mem;
7706           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
7707           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
7708                                GEN_INT (info->fp_save_offset 
7709                                         + sp_offset + 8*i));
7710           mem = gen_rtx_MEM (DFmode, addr);
7711           set_mem_alias_set (mem, rs6000_sr_alias_set);
7712
7713           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
7714         }
7715       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
7716       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7717                             NULL_RTX, NULL_RTX);
7718     }
7719
7720   /* Save GPRs.  This is done as a PARALLEL if we are using
7721      the store-multiple instructions.  */
7722   if (using_store_multiple)
7723     {
7724       rtvec p, dwarfp;
7725       int i;
7726       p = rtvec_alloc (32 - info->first_gp_reg_save);
7727       dwarfp = rtvec_alloc (32 - info->first_gp_reg_save);
7728       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
7729         {
7730           rtx addr, reg, mem;
7731           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
7732           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
7733                                GEN_INT (info->gp_save_offset 
7734                                         + sp_offset 
7735                                         + reg_size * i));
7736           mem = gen_rtx_MEM (reg_mode, addr);
7737           set_mem_alias_set (mem, rs6000_sr_alias_set);
7738
7739           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
7740         }
7741       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
7742       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7743                             NULL_RTX, NULL_RTX);
7744     }
7745   else
7746     {
7747       int i;
7748       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
7749         if ((regs_ever_live[info->first_gp_reg_save+i] 
7750              && ! call_used_regs[info->first_gp_reg_save+i])
7751             || (i+info->first_gp_reg_save == PIC_OFFSET_TABLE_REGNUM
7752                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
7753                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
7754           {
7755             rtx addr, reg, mem;
7756             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
7757             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
7758                                  GEN_INT (info->gp_save_offset 
7759                                           + sp_offset 
7760                                           + reg_size * i));
7761             mem = gen_rtx_MEM (reg_mode, addr);
7762             set_mem_alias_set (mem, rs6000_sr_alias_set);
7763
7764             insn = emit_move_insn (mem, reg);
7765             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7766                                   NULL_RTX, NULL_RTX);
7767           }
7768     }
7769
7770   /* ??? There's no need to emit actual instructions here, but it's the
7771      easiest way to get the frame unwind information emitted.  */
7772   if (current_function_calls_eh_return)
7773     {
7774       unsigned int i, regno;
7775
7776       for (i = 0; ; ++i)
7777         {
7778           rtx addr, reg, mem;
7779
7780           regno = EH_RETURN_DATA_REGNO (i);
7781           if (regno == INVALID_REGNUM)
7782             break;
7783
7784           reg = gen_rtx_REG (reg_mode, regno);
7785           addr = plus_constant (frame_reg_rtx,
7786                                 info->ehrd_offset + sp_offset
7787                                 + reg_size * (int) i);
7788           mem = gen_rtx_MEM (reg_mode, addr);
7789           set_mem_alias_set (mem, rs6000_sr_alias_set);
7790
7791           insn = emit_move_insn (mem, reg);
7792           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7793                                 NULL_RTX, NULL_RTX);
7794         }
7795     }
7796
7797   /* Save lr if we used it.  */
7798   if (info->lr_save_p)
7799     {
7800       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
7801                                GEN_INT (info->lr_save_offset + sp_offset));
7802       rtx reg = gen_rtx_REG (Pmode, 0);
7803       rtx mem = gen_rtx_MEM (Pmode, addr);
7804       /* This should not be of rs6000_sr_alias_set, because of
7805          __builtin_return_address.  */
7806       
7807       insn = emit_move_insn (mem, reg);
7808       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7809                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
7810     }
7811
7812   /* Save CR if we use any that must be preserved.  */
7813   if (info->cr_save_p)
7814     {
7815       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
7816                                GEN_INT (info->cr_save_offset + sp_offset));
7817       rtx mem = gen_rtx_MEM (SImode, addr);
7818
7819       set_mem_alias_set (mem, rs6000_sr_alias_set);
7820
7821       /* If r12 was used to hold the original sp, copy cr into r0 now
7822          that it's free.  */
7823       if (REGNO (frame_reg_rtx) == 12)
7824         {
7825           cr_save_rtx = gen_rtx_REG (SImode, 0);
7826           emit_insn (gen_movesi_from_cr (cr_save_rtx));
7827         }
7828       insn = emit_move_insn (mem, cr_save_rtx);
7829
7830       /* Now, there's no way that dwarf2out_frame_debug_expr is going
7831          to understand '(unspec:SI [(reg:CC 68) ...] 19)'.  But that's
7832          OK.  All we have to do is specify that _one_ condition code
7833          register is saved in this stack slot.  The thrower's epilogue
7834          will then restore all the call-saved registers.
7835          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
7836       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7837                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
7838     }
7839
7840   /* Update stack and set back pointer unless this is V.4, 
7841      for which it was done previously.  */
7842   if (info->push_p && DEFAULT_ABI != ABI_V4)
7843     rs6000_emit_allocate_stack (info->total_size, FALSE);
7844
7845   /* Save AltiVec registers if needed.  */
7846   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
7847     {
7848       int i;
7849
7850       /* There should be a non inline version of this, for when we
7851          are saving lots of vector registers.  */
7852       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
7853         if (regs_ever_live[i] && ! call_used_regs[i])
7854           {
7855             rtx addr, areg, savereg, mem;
7856
7857             savereg = gen_rtx_REG (V4SImode, i);
7858
7859             areg = gen_rtx_REG (Pmode, 0);
7860             emit_move_insn
7861               (areg, GEN_INT (info->altivec_save_offset
7862                               + sp_offset
7863                               + 16 * (i - info->first_altivec_reg_save)));
7864
7865             /* AltiVec addressing mode is [reg+reg].  */
7866             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
7867             mem = gen_rtx_MEM (V4SImode, addr);
7868             set_mem_alias_set (mem, rs6000_sr_alias_set);
7869
7870             insn = emit_move_insn (mem, savereg);
7871             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
7872                                   NULL_RTX, NULL_RTX);
7873           }
7874     }
7875
7876   /* VRSAVE is a bit vector representing which AltiVec registers
7877      are used.  The OS uses this to determine which vector
7878      registers to save on a context switch.  We need to save
7879      VRSAVE on the stack frame, add whatever AltiVec registers we
7880      used in this function, and do the corresponding magic in the
7881      epilogue.  */
7882
7883   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
7884     {
7885       rtx reg, addr, mem;
7886
7887       /* Get VRSAVE onto a GPR.  */
7888       reg = gen_rtx_REG (SImode, 12);
7889       emit_insn (gen_get_vrsave (reg));
7890
7891       /* Save VRSAVE.  */
7892       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
7893                            GEN_INT (info->vrsave_save_offset + sp_offset));
7894       mem = gen_rtx_MEM (SImode, addr);
7895       set_mem_alias_set (mem, rs6000_sr_alias_set);
7896       insn = emit_move_insn (mem, reg);
7897       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
7898                             NULL_RTX, NULL_RTX);
7899
7900       /* Include the registers in the mask.  */
7901       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
7902
7903       insn = emit_insn (generate_set_vrsave (reg, info));
7904
7905       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
7906                             NULL_RTX, NULL_RTX);
7907     }
7908
7909   /* Set frame pointer, if needed.  */
7910   if (frame_pointer_needed)
7911     {
7912       insn = emit_move_insn (gen_rtx_REG (reg_mode, FRAME_POINTER_REGNUM), 
7913                              sp_reg_rtx);
7914       RTX_FRAME_RELATED_P (insn) = 1;
7915     }
7916
7917   /* If we are using PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
7918   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
7919       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
7920           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM]))
7921   {
7922     /* If emit_load_toc_table will use the link register, we need to save
7923        it.  We use R11 for this purpose because emit_load_toc_table
7924        can use register 0.  This allows us to use a plain 'blr' to return
7925        from the procedure more often.  */
7926     int save_LR_around_toc_setup = (TARGET_ELF && flag_pic != 0
7927                                     && ! info->lr_save_p
7928                                     && EXIT_BLOCK_PTR->pred != NULL);
7929     if (save_LR_around_toc_setup)
7930       emit_move_insn (gen_rtx_REG (Pmode, 11), 
7931                       gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
7932     
7933     rs6000_emit_load_toc_table (TRUE);
7934
7935     if (save_LR_around_toc_setup)
7936       emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
7937                       gen_rtx_REG (Pmode, 11));
7938   }
7939
7940   if (DEFAULT_ABI == ABI_DARWIN
7941       && flag_pic && current_function_uses_pic_offset_table)
7942     {
7943       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
7944
7945       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest)));
7946
7947       rs6000_maybe_dead (
7948         emit_move_insn (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM),
7949                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
7950     }
7951 }
7952
7953 /* Write function prologue.  */
7954
7955 static void
7956 rs6000_output_function_prologue (file, size)
7957      FILE *file;
7958      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
7959 {
7960   rs6000_stack_t *info = rs6000_stack_info ();
7961
7962   if (TARGET_DEBUG_STACK)
7963     debug_stack_info (info);
7964
7965   /* Write .extern for any function we will call to save and restore
7966      fp values.  */
7967   if (info->first_fp_reg_save < 64
7968       && !FP_SAVE_INLINE (info->first_fp_reg_save))
7969     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
7970              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
7971              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
7972              RESTORE_FP_SUFFIX);
7973
7974   /* Write .extern for AIX common mode routines, if needed.  */
7975   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
7976     {
7977       fputs ("\t.extern __mulh\n", file);
7978       fputs ("\t.extern __mull\n", file);
7979       fputs ("\t.extern __divss\n", file);
7980       fputs ("\t.extern __divus\n", file);
7981       fputs ("\t.extern __quoss\n", file);
7982       fputs ("\t.extern __quous\n", file);
7983       common_mode_defined = 1;
7984     }
7985
7986   if (! HAVE_prologue)
7987     {
7988       start_sequence ();
7989       
7990       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
7991          the "toplevel" insn chain.  */
7992       emit_note (0, NOTE_INSN_DELETED);
7993       rs6000_emit_prologue ();
7994       emit_note (0, NOTE_INSN_DELETED);
7995       
7996       if (TARGET_DEBUG_STACK)
7997         debug_rtx_list (get_insns (), 100);
7998       final (get_insns (), file, FALSE, FALSE);
7999       end_sequence ();
8000     }
8001
8002   rs6000_pic_labelno++;
8003 }
8004   
8005 /* Emit function epilogue as insns.
8006
8007    At present, dwarf2out_frame_debug_expr doesn't understand
8008    register restores, so we don't bother setting RTX_FRAME_RELATED_P
8009    anywhere in the epilogue.  Most of the insns below would in any case
8010    need special notes to explain where r11 is in relation to the stack.  */
8011
8012 void
8013 rs6000_emit_epilogue (sibcall)
8014      int sibcall;
8015 {
8016   rs6000_stack_t *info;
8017   int restoring_FPRs_inline;
8018   int using_load_multiple;
8019   int using_mfcr_multiple;
8020   int use_backchain_to_restore_sp;
8021   int sp_offset = 0;
8022   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
8023   rtx frame_reg_rtx = sp_reg_rtx;
8024   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
8025   int reg_size = TARGET_POWERPC64 ? 8 : 4;
8026   int i;
8027
8028   info = rs6000_stack_info ();
8029   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
8030                          && info->first_gp_reg_save < 31);
8031   restoring_FPRs_inline = (sibcall
8032                            || current_function_calls_eh_return
8033                            || info->first_fp_reg_save == 64
8034                            || FP_SAVE_INLINE (info->first_fp_reg_save));
8035   use_backchain_to_restore_sp = (frame_pointer_needed 
8036                                  || current_function_calls_alloca
8037                                  || info->total_size > 32767);
8038   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
8039                          || rs6000_cpu == PROCESSOR_PPC603
8040                          || rs6000_cpu == PROCESSOR_PPC750
8041                          || optimize_size);
8042
8043   /* If we have a frame pointer, a call to alloca,  or a large stack
8044      frame, restore the old stack pointer using the backchain.  Otherwise,
8045      we know what size to update it with.  */
8046   if (use_backchain_to_restore_sp)
8047     {
8048       /* Under V.4, don't reset the stack pointer until after we're done
8049          loading the saved registers.  */
8050       if (DEFAULT_ABI == ABI_V4)
8051         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
8052
8053       emit_move_insn (frame_reg_rtx,
8054                       gen_rtx_MEM (Pmode, sp_reg_rtx));
8055       
8056     }
8057   else if (info->push_p)
8058     {
8059       if (DEFAULT_ABI == ABI_V4)
8060         sp_offset = info->total_size;
8061       else
8062         {
8063           emit_insn (TARGET_32BIT
8064                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
8065                                    GEN_INT (info->total_size))
8066                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
8067                                    GEN_INT (info->total_size)));
8068         }
8069     }
8070   
8071   /* Get the old lr if we saved it.  */
8072   if (info->lr_save_p)
8073     {
8074       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8075                                GEN_INT (info->lr_save_offset + sp_offset));
8076       rtx mem = gen_rtx_MEM (Pmode, addr);
8077
8078       set_mem_alias_set (mem, rs6000_sr_alias_set);
8079
8080       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
8081     }
8082   
8083   /* Get the old cr if we saved it.  */
8084   if (info->cr_save_p)
8085     {
8086       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8087                                GEN_INT (info->cr_save_offset + sp_offset));
8088       rtx mem = gen_rtx_MEM (SImode, addr);
8089
8090       set_mem_alias_set (mem, rs6000_sr_alias_set);
8091
8092       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
8093     }
8094   
8095   /* Set LR here to try to overlap restores below.  */
8096   if (info->lr_save_p)
8097     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
8098                     gen_rtx_REG (Pmode, 0));
8099   
8100   /* Load exception handler data registers, if needed.  */
8101   if (current_function_calls_eh_return)
8102     {
8103       unsigned int i, regno;
8104
8105       for (i = 0; ; ++i)
8106         {
8107           rtx addr, mem;
8108
8109           regno = EH_RETURN_DATA_REGNO (i);
8110           if (regno == INVALID_REGNUM)
8111             break;
8112
8113           addr = plus_constant (frame_reg_rtx,
8114                                 info->ehrd_offset + sp_offset
8115                                 + reg_size * (int) i);
8116           mem = gen_rtx_MEM (reg_mode, addr);
8117           set_mem_alias_set (mem, rs6000_sr_alias_set);
8118
8119           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
8120         }
8121     }
8122   
8123   /* Restore GPRs.  This is done as a PARALLEL if we are using
8124      the load-multiple instructions.  */
8125   if (using_load_multiple)
8126     {
8127       rtvec p;
8128       p = rtvec_alloc (32 - info->first_gp_reg_save);
8129       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8130         {
8131           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
8132                                    GEN_INT (info->gp_save_offset 
8133                                             + sp_offset 
8134                                             + reg_size * i));
8135           rtx mem = gen_rtx_MEM (reg_mode, addr);
8136
8137           set_mem_alias_set (mem, rs6000_sr_alias_set);
8138
8139           RTVEC_ELT (p, i) = 
8140             gen_rtx_SET (VOIDmode,
8141                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
8142                          mem);
8143         }
8144       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8145     }
8146   else
8147     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8148       if ((regs_ever_live[info->first_gp_reg_save+i] 
8149            && ! call_used_regs[info->first_gp_reg_save+i])
8150           || (i+info->first_gp_reg_save == PIC_OFFSET_TABLE_REGNUM
8151               && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8152                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
8153         {
8154           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
8155                                    GEN_INT (info->gp_save_offset 
8156                                             + sp_offset 
8157                                             + reg_size * i));
8158           rtx mem = gen_rtx_MEM (reg_mode, addr);
8159
8160           set_mem_alias_set (mem, rs6000_sr_alias_set);
8161
8162           emit_move_insn (gen_rtx_REG (reg_mode, 
8163                                        info->first_gp_reg_save + i),
8164                           mem);
8165         }
8166
8167   /* Restore fpr's if we need to do it without calling a function.  */
8168   if (restoring_FPRs_inline)
8169     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8170       if ((regs_ever_live[info->first_fp_reg_save+i] 
8171            && ! call_used_regs[info->first_fp_reg_save+i]))
8172         {
8173           rtx addr, mem;
8174           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8175                                GEN_INT (info->fp_save_offset 
8176                                         + sp_offset 
8177                                         + 8 * i));
8178           mem = gen_rtx_MEM (DFmode, addr);
8179           set_mem_alias_set (mem, rs6000_sr_alias_set);
8180
8181           emit_move_insn (gen_rtx_REG (DFmode, 
8182                                        info->first_fp_reg_save + i),
8183                           mem);
8184         }
8185
8186   /* Restore AltiVec registers if needed.  */
8187   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
8188     {
8189       int i;
8190
8191       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
8192         if (regs_ever_live[i] && ! call_used_regs[i])
8193           {
8194             rtx addr, areg, mem;
8195
8196             areg = gen_rtx_REG (Pmode, 0);
8197             emit_move_insn
8198               (areg, GEN_INT (info->altivec_save_offset
8199                               + sp_offset
8200                               + 16 * (i - info->first_altivec_reg_save)));
8201
8202             /* AltiVec addressing mode is [reg+reg].  */
8203             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
8204             mem = gen_rtx_MEM (V4SImode, addr);
8205             set_mem_alias_set (mem, rs6000_sr_alias_set);
8206
8207             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
8208           }
8209     }
8210
8211   /* Restore VRSAVE if needed.  */
8212   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
8213     {
8214       rtx addr, mem, reg;
8215
8216       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8217                            GEN_INT (info->vrsave_save_offset + sp_offset));
8218       mem = gen_rtx_MEM (SImode, addr);
8219       set_mem_alias_set (mem, rs6000_sr_alias_set);
8220       reg = gen_rtx_REG (SImode, 12);
8221       emit_move_insn (reg, mem);
8222
8223       emit_insn (generate_set_vrsave (reg, info));
8224     }
8225
8226   /* If we saved cr, restore it here.  Just those that were used.  */
8227   if (info->cr_save_p)
8228     {
8229       rtx r12_rtx = gen_rtx_REG (SImode, 12);
8230       int count = 0;
8231       
8232       if (using_mfcr_multiple)
8233         {
8234           for (i = 0; i < 8; i++)
8235             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
8236               count++;
8237           if (count == 0)
8238             abort ();
8239         }
8240
8241       if (using_mfcr_multiple && count > 1)
8242         {
8243           rtvec p;
8244           int ndx;
8245           
8246           p = rtvec_alloc (count);
8247
8248           ndx = 0;
8249           for (i = 0; i < 8; i++)
8250             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
8251               {
8252                 rtvec r = rtvec_alloc (2);
8253                 RTVEC_ELT (r, 0) = r12_rtx;
8254                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
8255                 RTVEC_ELT (p, ndx) =
8256                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
8257                                gen_rtx_UNSPEC (CCmode, r, 20));
8258                 ndx++;
8259               }
8260           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8261           if (ndx != count)
8262             abort ();
8263         }
8264       else
8265         for (i = 0; i < 8; i++)
8266           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
8267             {
8268               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
8269                                                            CR0_REGNO+i),
8270                                               r12_rtx));
8271             }
8272     }
8273
8274   /* If this is V.4, unwind the stack pointer after all of the loads
8275      have been done.  We need to emit a block here so that sched
8276      doesn't decide to move the sp change before the register restores
8277      (which may not have any obvious dependency on the stack).  This
8278      doesn't hurt performance, because there is no scheduling that can
8279      be done after this point.  */
8280   if (DEFAULT_ABI == ABI_V4)
8281     {
8282       if (frame_reg_rtx != sp_reg_rtx)
8283           rs6000_emit_stack_tie ();
8284
8285       if (use_backchain_to_restore_sp)
8286         {
8287           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
8288         }
8289       else if (sp_offset != 0)
8290         {
8291           emit_insn (Pmode == SImode
8292                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
8293                                    GEN_INT (sp_offset))
8294                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
8295                                    GEN_INT (sp_offset)));
8296         }
8297     }
8298
8299   if (current_function_calls_eh_return)
8300     {
8301       rtx sa = EH_RETURN_STACKADJ_RTX;
8302       emit_insn (Pmode == SImode
8303                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
8304                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
8305     }
8306
8307   if (!sibcall)
8308     {
8309       rtvec p;
8310       if (! restoring_FPRs_inline)
8311         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
8312       else
8313         p = rtvec_alloc (2);
8314
8315       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
8316       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
8317                                       gen_rtx_REG (Pmode, 
8318                                                    LINK_REGISTER_REGNUM));
8319
8320       /* If we have to restore more than two FP registers, branch to the
8321          restore function.  It will return to our caller.  */
8322       if (! restoring_FPRs_inline)
8323         {
8324           int i;
8325           char rname[30];
8326           const char *alloc_rname;
8327
8328           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
8329                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
8330           alloc_rname = ggc_strdup (rname);
8331           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
8332                                           gen_rtx_SYMBOL_REF (Pmode,
8333                                                               alloc_rname));
8334
8335           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8336             {
8337               rtx addr, mem;
8338               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
8339                                    GEN_INT (info->fp_save_offset + 8*i));
8340               mem = gen_rtx_MEM (DFmode, addr);
8341               set_mem_alias_set (mem, rs6000_sr_alias_set);
8342
8343               RTVEC_ELT (p, i+3) = 
8344                 gen_rtx_SET (VOIDmode,
8345                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
8346                              mem);
8347             }
8348         }
8349       
8350       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
8351     }
8352 }
8353
8354 /* Write function epilogue.  */
8355
8356 static void
8357 rs6000_output_function_epilogue (file, size)
8358      FILE *file;
8359      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
8360 {
8361   rs6000_stack_t *info = rs6000_stack_info ();
8362
8363   if (! HAVE_epilogue)
8364     {
8365       rtx insn = get_last_insn ();
8366       /* If the last insn was a BARRIER, we don't have to write anything except
8367          the trace table.  */
8368       if (GET_CODE (insn) == NOTE)
8369         insn = prev_nonnote_insn (insn);
8370       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
8371         {
8372           /* This is slightly ugly, but at least we don't have two
8373              copies of the epilogue-emitting code.  */
8374           start_sequence ();
8375
8376           /* A NOTE_INSN_DELETED is supposed to be at the start
8377              and end of the "toplevel" insn chain.  */
8378           emit_note (0, NOTE_INSN_DELETED);
8379           rs6000_emit_epilogue (FALSE);
8380           emit_note (0, NOTE_INSN_DELETED);
8381
8382           if (TARGET_DEBUG_STACK)
8383             debug_rtx_list (get_insns (), 100);
8384           final (get_insns (), file, FALSE, FALSE);
8385           end_sequence ();
8386         }
8387     }
8388
8389   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
8390      on its format.
8391
8392      We don't output a traceback table if -finhibit-size-directive was
8393      used.  The documentation for -finhibit-size-directive reads
8394      ``don't output a @code{.size} assembler directive, or anything
8395      else that would cause trouble if the function is split in the
8396      middle, and the two halves are placed at locations far apart in
8397      memory.''  The traceback table has this property, since it
8398      includes the offset from the start of the function to the
8399      traceback table itself.
8400
8401      System V.4 Powerpc's (and the embedded ABI derived from it) use a
8402      different traceback table.  */
8403   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive)
8404     {
8405       const char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
8406       const char *language_string = lang_hooks.name;
8407       int fixed_parms, float_parms, parm_info;
8408       int i;
8409
8410       while (*fname == '.')     /* V.4 encodes . in the name */
8411         fname++;
8412
8413       /* Need label immediately before tbtab, so we can compute its offset
8414          from the function start.  */
8415       if (*fname == '*')
8416         ++fname;
8417       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
8418       ASM_OUTPUT_LABEL (file, fname);
8419
8420       /* The .tbtab pseudo-op can only be used for the first eight
8421          expressions, since it can't handle the possibly variable
8422          length fields that follow.  However, if you omit the optional
8423          fields, the assembler outputs zeros for all optional fields
8424          anyways, giving each variable length field is minimum length
8425          (as defined in sys/debug.h).  Thus we can not use the .tbtab
8426          pseudo-op at all.  */
8427
8428       /* An all-zero word flags the start of the tbtab, for debuggers
8429          that have to find it by searching forward from the entry
8430          point or from the current pc.  */
8431       fputs ("\t.long 0\n", file);
8432
8433       /* Tbtab format type.  Use format type 0.  */
8434       fputs ("\t.byte 0,", file);
8435
8436       /* Language type.  Unfortunately, there doesn't seem to be any
8437          official way to get this info, so we use language_string.  C
8438          is 0.  C++ is 9.  No number defined for Obj-C, so use the
8439          value for C for now.  There is no official value for Java,
8440          although IBM appears to be using 13.  There is no official value
8441          for Chill, so we've chosen 44 pseudo-randomly.  */
8442       if (! strcmp (language_string, "GNU C")
8443           || ! strcmp (language_string, "GNU Objective-C"))
8444         i = 0;
8445       else if (! strcmp (language_string, "GNU F77"))
8446         i = 1;
8447       else if (! strcmp (language_string, "GNU Ada"))
8448         i = 3;
8449       else if (! strcmp (language_string, "GNU Pascal"))
8450         i = 2;
8451       else if (! strcmp (language_string, "GNU C++"))
8452         i = 9;
8453       else if (! strcmp (language_string, "GNU Java"))
8454         i = 13;
8455       else if (! strcmp (language_string, "GNU CHILL"))
8456         i = 44;
8457       else
8458         abort ();
8459       fprintf (file, "%d,", i);
8460
8461       /* 8 single bit fields: global linkage (not set for C extern linkage,
8462          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
8463          from start of procedure stored in tbtab, internal function, function
8464          has controlled storage, function has no toc, function uses fp,
8465          function logs/aborts fp operations.  */
8466       /* Assume that fp operations are used if any fp reg must be saved.  */
8467       fprintf (file, "%d,", (1 << 5) | ((info->first_fp_reg_save != 64) << 1));
8468
8469       /* 6 bitfields: function is interrupt handler, name present in
8470          proc table, function calls alloca, on condition directives
8471          (controls stack walks, 3 bits), saves condition reg, saves
8472          link reg.  */
8473       /* The `function calls alloca' bit seems to be set whenever reg 31 is
8474          set up as a frame pointer, even when there is no alloca call.  */
8475       fprintf (file, "%d,",
8476                ((1 << 6) | (frame_pointer_needed << 5)
8477                 | (info->cr_save_p << 1) | (info->lr_save_p)));
8478
8479       /* 3 bitfields: saves backchain, spare bit, number of fpr saved
8480          (6 bits).  */
8481       fprintf (file, "%d,",
8482                (info->push_p << 7) | (64 - info->first_fp_reg_save));
8483
8484       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
8485       fprintf (file, "%d,", (32 - first_reg_to_save ()));
8486
8487       {
8488         /* Compute the parameter info from the function decl argument
8489            list.  */
8490         tree decl;
8491         int next_parm_info_bit;
8492
8493         next_parm_info_bit = 31;
8494         parm_info = 0;
8495         fixed_parms = 0;
8496         float_parms = 0;
8497
8498         for (decl = DECL_ARGUMENTS (current_function_decl);
8499              decl; decl = TREE_CHAIN (decl))
8500           {
8501             rtx parameter = DECL_INCOMING_RTL (decl);
8502             enum machine_mode mode = GET_MODE (parameter);
8503
8504             if (GET_CODE (parameter) == REG)
8505               {
8506                 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8507                   {
8508                     int bits;
8509
8510                     float_parms++;
8511
8512                     if (mode == SFmode)
8513                       bits = 0x2;
8514                     else if (mode == DFmode)
8515                       bits = 0x3;
8516                     else
8517                       abort ();
8518
8519                     /* If only one bit will fit, don't or in this entry.  */
8520                     if (next_parm_info_bit > 0)
8521                       parm_info |= (bits << (next_parm_info_bit - 1));
8522                     next_parm_info_bit -= 2;
8523                   }
8524                 else
8525                   {
8526                     fixed_parms += ((GET_MODE_SIZE (mode)
8527                                      + (UNITS_PER_WORD - 1))
8528                                     / UNITS_PER_WORD);
8529                     next_parm_info_bit -= 1;
8530                   }
8531               }
8532           }
8533       }
8534
8535       /* Number of fixed point parameters.  */
8536       /* This is actually the number of words of fixed point parameters; thus
8537          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
8538       fprintf (file, "%d,", fixed_parms);
8539
8540       /* 2 bitfields: number of floating point parameters (7 bits), parameters
8541          all on stack.  */
8542       /* This is actually the number of fp registers that hold parameters;
8543          and thus the maximum value is 13.  */
8544       /* Set parameters on stack bit if parameters are not in their original
8545          registers, regardless of whether they are on the stack?  Xlc
8546          seems to set the bit when not optimizing.  */
8547       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
8548
8549       /* Optional fields follow.  Some are variable length.  */
8550
8551       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
8552          11 double float.  */
8553       /* There is an entry for each parameter in a register, in the order that
8554          they occur in the parameter list.  Any intervening arguments on the
8555          stack are ignored.  If the list overflows a long (max possible length
8556          34 bits) then completely leave off all elements that don't fit.  */
8557       /* Only emit this long if there was at least one parameter.  */
8558       if (fixed_parms || float_parms)
8559         fprintf (file, "\t.long %d\n", parm_info);
8560
8561       /* Offset from start of code to tb table.  */
8562       fputs ("\t.long ", file);
8563       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
8564 #if TARGET_AIX
8565       RS6000_OUTPUT_BASENAME (file, fname);
8566 #else
8567       assemble_name (file, fname);
8568 #endif
8569       fputs ("-.", file);
8570 #if TARGET_AIX
8571       RS6000_OUTPUT_BASENAME (file, fname);
8572 #else
8573       assemble_name (file, fname);
8574 #endif
8575       putc ('\n', file);
8576
8577       /* Interrupt handler mask.  */
8578       /* Omit this long, since we never set the interrupt handler bit
8579          above.  */
8580
8581       /* Number of CTL (controlled storage) anchors.  */
8582       /* Omit this long, since the has_ctl bit is never set above.  */
8583
8584       /* Displacement into stack of each CTL anchor.  */
8585       /* Omit this list of longs, because there are no CTL anchors.  */
8586
8587       /* Length of function name.  */
8588       fprintf (file, "\t.short %d\n", (int) strlen (fname));
8589
8590       /* Function name.  */
8591       assemble_string (fname, strlen (fname));
8592
8593       /* Register for alloca automatic storage; this is always reg 31.
8594          Only emit this if the alloca bit was set above.  */
8595       if (frame_pointer_needed)
8596         fputs ("\t.byte 31\n", file);
8597     }
8598 }
8599 \f
8600 /* A C compound statement that outputs the assembler code for a thunk
8601    function, used to implement C++ virtual function calls with
8602    multiple inheritance.  The thunk acts as a wrapper around a virtual
8603    function, adjusting the implicit object parameter before handing
8604    control off to the real function.
8605
8606    First, emit code to add the integer DELTA to the location that
8607    contains the incoming first argument.  Assume that this argument
8608    contains a pointer, and is the one used to pass the `this' pointer
8609    in C++.  This is the incoming argument *before* the function
8610    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
8611    values of all other incoming arguments.
8612
8613    After the addition, emit code to jump to FUNCTION, which is a
8614    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
8615    not touch the return address.  Hence returning from FUNCTION will
8616    return to whoever called the current `thunk'.
8617
8618    The effect must be as if FUNCTION had been called directly with the
8619    adjusted first argument.  This macro is responsible for emitting
8620    all of the code for a thunk function; output_function_prologue()
8621    and output_function_epilogue() are not invoked.
8622
8623    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
8624    been extracted from it.)  It might possibly be useful on some
8625    targets, but probably not.
8626
8627    If you do not define this macro, the target-independent code in the
8628    C++ frontend will generate a less efficient heavyweight thunk that
8629    calls FUNCTION instead of jumping to it.  The generic approach does
8630    not support varargs.  */
8631
8632 void
8633 output_mi_thunk (file, thunk_fndecl, delta, function)
8634      FILE *file;
8635      tree thunk_fndecl ATTRIBUTE_UNUSED;
8636      int delta;
8637      tree function;
8638 {
8639   const char *this_reg =
8640     reg_names[ aggregate_value_p (TREE_TYPE (TREE_TYPE (function))) ? 4 : 3 ];
8641   const char *prefix;
8642   const char *fname;
8643   const char *r0         = reg_names[0];
8644   const char *toc        = reg_names[2];
8645   const char *schain     = reg_names[11];
8646   const char *r12        = reg_names[12];
8647   char buf[512];
8648   static int labelno = 0;
8649
8650   /* Small constants that can be done by one add instruction.  */
8651   if (delta >= -32768 && delta <= 32767)
8652     {
8653       if (! TARGET_NEW_MNEMONICS)
8654         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta, this_reg);
8655       else
8656         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta);
8657     }
8658
8659   /* Large constants that can be done by one addis instruction.  */
8660   else if ((delta & 0xffff) == 0 && num_insns_constant_wide (delta) == 1)
8661     asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
8662                  delta >> 16);
8663
8664   /* 32-bit constants that can be done by an add and addis instruction.  */
8665   else if (TARGET_32BIT || num_insns_constant_wide (delta) == 1)
8666     {
8667       /* Break into two pieces, propagating the sign bit from the low
8668          word to the upper word.  */
8669       int delta_high = delta >> 16;
8670       int delta_low  = delta & 0xffff;
8671       if ((delta_low & 0x8000) != 0)
8672         {
8673           delta_high++;
8674           delta_low = (delta_low ^ 0x8000) - 0x8000;    /* sign extend */
8675         }
8676
8677       asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
8678                    delta_high);
8679
8680       if (! TARGET_NEW_MNEMONICS)
8681         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta_low, this_reg);
8682       else
8683         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta_low);
8684     }
8685
8686   /* 64-bit constants, fixme */
8687   else
8688     abort ();
8689
8690   /* Get the prefix in front of the names.  */
8691   switch (DEFAULT_ABI)
8692     {
8693     default:
8694       abort ();
8695
8696     case ABI_AIX:
8697       prefix = ".";
8698       break;
8699
8700     case ABI_V4:
8701     case ABI_AIX_NODESC:
8702       prefix = "";
8703       break;
8704     }
8705
8706   /* If the function is compiled in this module, jump to it directly.
8707      Otherwise, load up its address and jump to it.  */
8708
8709   fname = XSTR (XEXP (DECL_RTL (function), 0), 0);
8710
8711   if (current_file_function_operand (XEXP (DECL_RTL (function), 0), VOIDmode)
8712       && ! lookup_attribute ("longcall",
8713                              TYPE_ATTRIBUTES (TREE_TYPE (function))))
8714     {
8715       fprintf (file, "\tb %s", prefix);
8716       assemble_name (file, fname);
8717       if (DEFAULT_ABI == ABI_V4 && flag_pic) fputs ("@local", file);
8718       putc ('\n', file);
8719     }
8720
8721   else
8722     {
8723       switch (DEFAULT_ABI)
8724         {
8725         default:
8726           abort ();
8727
8728         case ABI_AIX:
8729           /* Set up a TOC entry for the function.  */
8730           ASM_GENERATE_INTERNAL_LABEL (buf, "Lthunk", labelno);
8731           toc_section ();
8732           ASM_OUTPUT_INTERNAL_LABEL (file, "Lthunk", labelno);
8733           labelno++;
8734
8735           if (TARGET_MINIMAL_TOC)
8736             fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
8737           else
8738             {
8739               fputs ("\t.tc ", file);
8740               assemble_name (file, fname);
8741               fputs ("[TC],", file);
8742             }
8743           assemble_name (file, fname);
8744           putc ('\n', file);
8745           text_section ();
8746           if (TARGET_MINIMAL_TOC)
8747             asm_fprintf (file, (TARGET_32BIT)
8748                          ? "\t{l|lwz} %s,%s(%s)\n" : "\tld %s,%s(%s)\n", r12,
8749                          TARGET_ELF ? ".LCTOC0@toc" : ".LCTOC..1", toc);
8750           asm_fprintf (file, (TARGET_32BIT) ? "\t{l|lwz} %s," : "\tld %s,", r12);
8751           assemble_name (file, buf);
8752           if (TARGET_ELF && TARGET_MINIMAL_TOC)
8753             fputs ("-(.LCTOC1)", file);
8754           asm_fprintf (file, "(%s)\n", TARGET_MINIMAL_TOC ? r12 : toc);
8755           asm_fprintf (file,
8756                        (TARGET_32BIT) ? "\t{l|lwz} %s,0(%s)\n" : "\tld %s,0(%s)\n",
8757                        r0, r12);
8758
8759           asm_fprintf (file,
8760                        (TARGET_32BIT) ? "\t{l|lwz} %s,4(%s)\n" : "\tld %s,8(%s)\n",
8761                        toc, r12);
8762
8763           asm_fprintf (file, "\tmtctr %s\n", r0);
8764           asm_fprintf (file,
8765                        (TARGET_32BIT) ? "\t{l|lwz} %s,8(%s)\n" : "\tld %s,16(%s)\n",
8766                        schain, r12);
8767
8768           asm_fprintf (file, "\tbctr\n");
8769           break;
8770
8771         case ABI_AIX_NODESC:
8772         case ABI_V4:
8773           fprintf (file, "\tb %s", prefix);
8774           assemble_name (file, fname);
8775           if (flag_pic) fputs ("@plt", file);
8776           putc ('\n', file);
8777           break;
8778
8779 #if TARGET_MACHO
8780         case ABI_DARWIN:
8781           fprintf (file, "\tb %s", prefix);
8782           if (flag_pic && !machopic_name_defined_p (fname))
8783             assemble_name (file, machopic_stub_name (fname));
8784           else
8785             assemble_name (file, fname);
8786           putc ('\n', file);
8787           break;
8788 #endif
8789         }
8790     }
8791 }
8792
8793 \f
8794 /* A quick summary of the various types of 'constant-pool tables'
8795    under PowerPC:
8796
8797    Target       Flags           Name            One table per   
8798    AIX          (none)          AIX TOC         object file
8799    AIX          -mfull-toc      AIX TOC         object file
8800    AIX          -mminimal-toc   AIX minimal TOC translation unit
8801    SVR4/EABI    (none)          SVR4 SDATA      object file
8802    SVR4/EABI    -fpic           SVR4 pic        object file
8803    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
8804    SVR4/EABI    -mrelocatable   EABI TOC        function
8805    SVR4/EABI    -maix           AIX TOC         object file
8806    SVR4/EABI    -maix -mminimal-toc 
8807                                 AIX minimal TOC translation unit
8808
8809    Name                 Reg.    Set by  entries       contains:
8810                                         made by  addrs? fp?     sum?
8811
8812    AIX TOC              2       crt0    as       Y      option  option
8813    AIX minimal TOC      30      prolog  gcc      Y      Y       option
8814    SVR4 SDATA           13      crt0    gcc      N      Y       N
8815    SVR4 pic             30      prolog  ld       Y      not yet N
8816    SVR4 PIC             30      prolog  gcc      Y      option  option
8817    EABI TOC             30      prolog  gcc      Y      option  option
8818
8819 */
8820
8821 /* Hash table stuff for keeping track of TOC entries.  */
8822
8823 struct toc_hash_struct 
8824 {
8825   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
8826      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
8827   rtx key;
8828   enum machine_mode key_mode;
8829   int labelno;
8830 };
8831
8832 static htab_t toc_hash_table;
8833
8834 /* Hash functions for the hash table.  */
8835
8836 static unsigned
8837 rs6000_hash_constant (k)
8838      rtx k;
8839 {
8840   unsigned result = (GET_CODE (k) << 3) ^ GET_MODE (k);
8841   const char *format = GET_RTX_FORMAT (GET_CODE (k));
8842   int flen = strlen (format);
8843   int fidx;
8844   
8845   if (GET_CODE (k) == LABEL_REF)
8846     return result * 1231 + X0INT (XEXP (k, 0), 3);
8847
8848   if (GET_CODE (k) == CONST_DOUBLE)
8849     fidx = 1;
8850   else if (GET_CODE (k) == CODE_LABEL)
8851     fidx = 3;
8852   else
8853     fidx = 0;
8854
8855   for (; fidx < flen; fidx++)
8856     switch (format[fidx])
8857       {
8858       case 's':
8859         {
8860           unsigned i, len;
8861           const char *str = XSTR (k, fidx);
8862           len = strlen (str);
8863           result = result * 613 + len;
8864           for (i = 0; i < len; i++)
8865             result = result * 613 + (unsigned) str[i];
8866           break;
8867         }
8868       case 'u':
8869       case 'e':
8870         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
8871         break;
8872       case 'i':
8873       case 'n':
8874         result = result * 613 + (unsigned) XINT (k, fidx);
8875         break;
8876       case 'w':
8877         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
8878           result = result * 613 + (unsigned) XWINT (k, fidx);
8879         else
8880           {
8881             size_t i;
8882             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
8883               result = result * 613 + (unsigned) (XWINT (k, fidx)
8884                                                   >> CHAR_BIT * i);
8885           }
8886         break;
8887       default:
8888         abort ();
8889       }
8890   return result;
8891 }
8892
8893 static unsigned
8894 toc_hash_function (hash_entry)
8895      const void * hash_entry;
8896 {
8897   const struct toc_hash_struct *thc = 
8898     (const struct toc_hash_struct *) hash_entry;
8899   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
8900 }
8901
8902 /* Compare H1 and H2 for equivalence.  */
8903
8904 static int
8905 toc_hash_eq (h1, h2)
8906      const void * h1;
8907      const void * h2;
8908 {
8909   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
8910   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
8911
8912   if (((const struct toc_hash_struct *) h1)->key_mode
8913       != ((const struct toc_hash_struct *) h2)->key_mode)
8914     return 0;
8915
8916   /* Gotcha:  One of these const_doubles will be in memory.
8917      The other may be on the constant-pool chain.
8918      So rtx_equal_p will think they are different...  */
8919   if (r1 == r2)
8920     return 1;
8921   if (GET_CODE (r1) != GET_CODE (r2)
8922       || GET_MODE (r1) != GET_MODE (r2))
8923     return 0;
8924   if (GET_CODE (r1) == CONST_DOUBLE)
8925     {
8926       int format_len = strlen (GET_RTX_FORMAT (CONST_DOUBLE));
8927       int i;
8928       for (i = 1; i < format_len; i++)
8929         if (XWINT (r1, i) != XWINT (r2, i))
8930           return 0;
8931       
8932       return 1;
8933     }
8934   else if (GET_CODE (r1) == LABEL_REF)
8935     return (CODE_LABEL_NUMBER (XEXP (r1, 0)) 
8936             == CODE_LABEL_NUMBER (XEXP (r2, 0)));
8937   else
8938     return rtx_equal_p (r1, r2);
8939 }
8940
8941 /* Mark the hash table-entry HASH_ENTRY.  */
8942
8943 static int
8944 toc_hash_mark_entry (hash_slot, unused)
8945      void ** hash_slot;
8946      void * unused ATTRIBUTE_UNUSED;
8947 {
8948   const struct toc_hash_struct * hash_entry = 
8949     *(const struct toc_hash_struct **) hash_slot;
8950   rtx r = hash_entry->key;
8951   ggc_set_mark (hash_entry);
8952   /* For CODE_LABELS, we don't want to drag in the whole insn chain...  */
8953   if (GET_CODE (r) == LABEL_REF)
8954     {
8955       ggc_set_mark (r);
8956       ggc_set_mark (XEXP (r, 0));
8957     }
8958   else
8959     ggc_mark_rtx (r);
8960   return 1;
8961 }
8962
8963 /* Mark all the elements of the TOC hash-table *HT.  */
8964
8965 static void
8966 toc_hash_mark_table (vht)
8967      void *vht;
8968 {
8969   htab_t *ht = vht;
8970   
8971   htab_traverse (*ht, toc_hash_mark_entry, (void *)0);
8972 }
8973
8974 /* These are the names given by the C++ front-end to vtables, and
8975    vtable-like objects.  Ideally, this logic should not be here;
8976    instead, there should be some programmatic way of inquiring as
8977    to whether or not an object is a vtable.  */
8978
8979 #define VTABLE_NAME_P(NAME)                             \
8980   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
8981   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
8982   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
8983   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
8984
8985 void
8986 rs6000_output_symbol_ref (file, x)
8987      FILE *file;
8988      rtx x;
8989 {
8990   /* Currently C++ toc references to vtables can be emitted before it
8991      is decided whether the vtable is public or private.  If this is
8992      the case, then the linker will eventually complain that there is
8993      a reference to an unknown section.  Thus, for vtables only, 
8994      we emit the TOC reference to reference the symbol and not the
8995      section.  */
8996   const char *name = XSTR (x, 0);
8997
8998   if (VTABLE_NAME_P (name)) 
8999     {
9000       RS6000_OUTPUT_BASENAME (file, name);
9001     }
9002   else
9003     assemble_name (file, name);
9004 }
9005
9006 /* Output a TOC entry.  We derive the entry name from what is being
9007    written.  */
9008
9009 void
9010 output_toc (file, x, labelno, mode)
9011      FILE *file;
9012      rtx x;
9013      int labelno;
9014      enum machine_mode mode;
9015 {
9016   char buf[256];
9017   const char *name = buf;
9018   const char *real_name;
9019   rtx base = x;
9020   int offset = 0;
9021
9022   if (TARGET_NO_TOC)
9023     abort ();
9024
9025   /* When the linker won't eliminate them, don't output duplicate
9026      TOC entries (this happens on AIX if there is any kind of TOC,
9027      and on SVR4 under -fPIC or -mrelocatable).  */
9028   if (TARGET_TOC)
9029     {
9030       struct toc_hash_struct *h;
9031       void * * found;
9032       
9033       h = ggc_alloc (sizeof (*h));
9034       h->key = x;
9035       h->key_mode = mode;
9036       h->labelno = labelno;
9037       
9038       found = htab_find_slot (toc_hash_table, h, 1);
9039       if (*found == NULL)
9040         *found = h;
9041       else  /* This is indeed a duplicate.  
9042                Set this label equal to that label.  */
9043         {
9044           fputs ("\t.set ", file);
9045           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
9046           fprintf (file, "%d,", labelno);
9047           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
9048           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
9049                                               found)->labelno));
9050           return;
9051         }
9052     }
9053
9054   /* If we're going to put a double constant in the TOC, make sure it's
9055      aligned properly when strict alignment is on.  */
9056   if (GET_CODE (x) == CONST_DOUBLE
9057       && STRICT_ALIGNMENT
9058       && GET_MODE_BITSIZE (mode) >= 64
9059       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
9060     ASM_OUTPUT_ALIGN (file, 3);
9061   }
9062
9063   ASM_OUTPUT_INTERNAL_LABEL (file, "LC", labelno);
9064
9065   /* Handle FP constants specially.  Note that if we have a minimal
9066      TOC, things we put here aren't actually in the TOC, so we can allow
9067      FP constants.  */
9068   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
9069     {
9070       REAL_VALUE_TYPE rv;
9071       long k[2];
9072
9073       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
9074       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
9075
9076       if (TARGET_64BIT)
9077         {
9078           if (TARGET_MINIMAL_TOC)
9079             fputs (DOUBLE_INT_ASM_OP, file);
9080           else
9081             fprintf (file, "\t.tc FD_%lx_%lx[TC],", k[0], k[1]);
9082           fprintf (file, "0x%lx%08lx\n", k[0], k[1]);
9083           return;
9084         }
9085       else
9086         {
9087           if (TARGET_MINIMAL_TOC)
9088             fputs ("\t.long ", file);
9089           else
9090             fprintf (file, "\t.tc FD_%lx_%lx[TC],", k[0], k[1]);
9091           fprintf (file, "0x%lx,0x%lx\n", k[0], k[1]);
9092           return;
9093         }
9094     }
9095   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
9096     {
9097       REAL_VALUE_TYPE rv;
9098       long l;
9099
9100       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
9101       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
9102
9103       if (TARGET_64BIT)
9104         {
9105           if (TARGET_MINIMAL_TOC)
9106             fputs (DOUBLE_INT_ASM_OP, file);
9107           else
9108             fprintf (file, "\t.tc FS_%lx[TC],", l);
9109           fprintf (file, "0x%lx00000000\n", l);
9110           return;
9111         }
9112       else
9113         {
9114           if (TARGET_MINIMAL_TOC)
9115             fputs ("\t.long ", file);
9116           else
9117             fprintf (file, "\t.tc FS_%lx[TC],", l);
9118           fprintf (file, "0x%lx\n", l);
9119           return;
9120         }
9121     }
9122   else if (GET_MODE (x) == VOIDmode
9123            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
9124     {
9125       unsigned HOST_WIDE_INT low;
9126       HOST_WIDE_INT high;
9127
9128       if (GET_CODE (x) == CONST_DOUBLE)
9129         {
9130           low = CONST_DOUBLE_LOW (x);
9131           high = CONST_DOUBLE_HIGH (x);
9132         }
9133       else
9134 #if HOST_BITS_PER_WIDE_INT == 32
9135         {
9136           low = INTVAL (x);
9137           high = (low & 0x80000000) ? ~0 : 0;
9138         }
9139 #else
9140         {
9141           low = INTVAL (x) & 0xffffffff;
9142           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
9143         }
9144 #endif
9145
9146       /* TOC entries are always Pmode-sized, but since this
9147          is a bigendian machine then if we're putting smaller
9148          integer constants in the TOC we have to pad them.
9149          (This is still a win over putting the constants in
9150          a separate constant pool, because then we'd have
9151          to have both a TOC entry _and_ the actual constant.)
9152
9153          For a 32-bit target, CONST_INT values are loaded and shifted
9154          entirely within `low' and can be stored in one TOC entry.  */
9155
9156       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
9157         abort ();/* It would be easy to make this work, but it doesn't now.  */
9158
9159       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
9160         lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
9161                        POINTER_SIZE, &low, &high, 0);
9162
9163       if (TARGET_64BIT)
9164         {
9165           if (TARGET_MINIMAL_TOC)
9166             fputs (DOUBLE_INT_ASM_OP, file);
9167           else
9168             fprintf (file, "\t.tc ID_%lx_%lx[TC],", (long)high, (long)low);
9169           fprintf (file, "0x%lx%08lx\n", (long) high, (long) low);
9170           return;
9171         }
9172       else
9173         {
9174           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
9175             {
9176               if (TARGET_MINIMAL_TOC)
9177                 fputs ("\t.long ", file);
9178               else
9179                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
9180                          (long)high, (long)low);
9181               fprintf (file, "0x%lx,0x%lx\n", (long) high, (long) low);
9182             }
9183           else
9184             {
9185               if (TARGET_MINIMAL_TOC)
9186                 fputs ("\t.long ", file);
9187               else
9188                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low);
9189               fprintf (file, "0x%lx\n", (long) low);
9190             }
9191           return;
9192         }
9193     }
9194
9195   if (GET_CODE (x) == CONST)
9196     {
9197       if (GET_CODE (XEXP (x, 0)) != PLUS)
9198         abort ();
9199
9200       base = XEXP (XEXP (x, 0), 0);
9201       offset = INTVAL (XEXP (XEXP (x, 0), 1));
9202     }
9203   
9204   if (GET_CODE (base) == SYMBOL_REF)
9205     name = XSTR (base, 0);
9206   else if (GET_CODE (base) == LABEL_REF)
9207     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
9208   else if (GET_CODE (base) == CODE_LABEL)
9209     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
9210   else
9211     abort ();
9212
9213   STRIP_NAME_ENCODING (real_name, name);
9214   if (TARGET_MINIMAL_TOC)
9215     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
9216   else
9217     {
9218       fprintf (file, "\t.tc %s", real_name);
9219
9220       if (offset < 0)
9221         fprintf (file, ".N%d", - offset);
9222       else if (offset)
9223         fprintf (file, ".P%d", offset);
9224
9225       fputs ("[TC],", file);
9226     }
9227
9228   /* Currently C++ toc references to vtables can be emitted before it
9229      is decided whether the vtable is public or private.  If this is
9230      the case, then the linker will eventually complain that there is
9231      a TOC reference to an unknown section.  Thus, for vtables only,
9232      we emit the TOC reference to reference the symbol and not the
9233      section.  */
9234   if (VTABLE_NAME_P (name))
9235     {
9236       RS6000_OUTPUT_BASENAME (file, name);
9237       if (offset < 0)
9238         fprintf (file, "%d", offset);
9239       else if (offset > 0)
9240         fprintf (file, "+%d", offset);
9241     }
9242   else
9243     output_addr_const (file, x);
9244   putc ('\n', file);
9245 }
9246 \f
9247 /* Output an assembler pseudo-op to write an ASCII string of N characters
9248    starting at P to FILE.
9249
9250    On the RS/6000, we have to do this using the .byte operation and
9251    write out special characters outside the quoted string.
9252    Also, the assembler is broken; very long strings are truncated,
9253    so we must artificially break them up early.  */
9254
9255 void
9256 output_ascii (file, p, n)
9257      FILE *file;
9258      const char *p;
9259      int n;
9260 {
9261   char c;
9262   int i, count_string;
9263   const char *for_string = "\t.byte \"";
9264   const char *for_decimal = "\t.byte ";
9265   const char *to_close = NULL;
9266
9267   count_string = 0;
9268   for (i = 0; i < n; i++)
9269     {
9270       c = *p++;
9271       if (c >= ' ' && c < 0177)
9272         {
9273           if (for_string)
9274             fputs (for_string, file);
9275           putc (c, file);
9276
9277           /* Write two quotes to get one.  */
9278           if (c == '"')
9279             {
9280               putc (c, file);
9281               ++count_string;
9282             }
9283
9284           for_string = NULL;
9285           for_decimal = "\"\n\t.byte ";
9286           to_close = "\"\n";
9287           ++count_string;
9288
9289           if (count_string >= 512)
9290             {
9291               fputs (to_close, file);
9292
9293               for_string = "\t.byte \"";
9294               for_decimal = "\t.byte ";
9295               to_close = NULL;
9296               count_string = 0;
9297             }
9298         }
9299       else
9300         {
9301           if (for_decimal)
9302             fputs (for_decimal, file);
9303           fprintf (file, "%d", c);
9304
9305           for_string = "\n\t.byte \"";
9306           for_decimal = ", ";
9307           to_close = "\n";
9308           count_string = 0;
9309         }
9310     }
9311
9312   /* Now close the string if we have written one.  Then end the line.  */
9313   if (to_close)
9314     fputs (to_close, file);
9315 }
9316 \f
9317 /* Generate a unique section name for FILENAME for a section type
9318    represented by SECTION_DESC.  Output goes into BUF.
9319
9320    SECTION_DESC can be any string, as long as it is different for each
9321    possible section type.
9322
9323    We name the section in the same manner as xlc.  The name begins with an
9324    underscore followed by the filename (after stripping any leading directory
9325    names) with the last period replaced by the string SECTION_DESC.  If
9326    FILENAME does not contain a period, SECTION_DESC is appended to the end of
9327    the name.  */
9328
9329 void
9330 rs6000_gen_section_name (buf, filename, section_desc)
9331      char **buf;
9332      const char *filename;
9333      const char *section_desc;
9334 {
9335   const char *q, *after_last_slash, *last_period = 0;
9336   char *p;
9337   int len;
9338
9339   after_last_slash = filename;
9340   for (q = filename; *q; q++)
9341     {
9342       if (*q == '/')
9343         after_last_slash = q + 1;
9344       else if (*q == '.')
9345         last_period = q;
9346     }
9347
9348   len = strlen (after_last_slash) + strlen (section_desc) + 2;
9349   *buf = (char *) permalloc (len);
9350
9351   p = *buf;
9352   *p++ = '_';
9353
9354   for (q = after_last_slash; *q; q++)
9355     {
9356       if (q == last_period)
9357         {
9358           strcpy (p, section_desc);
9359           p += strlen (section_desc);
9360         }
9361
9362       else if (ISALNUM (*q))
9363         *p++ = *q;
9364     }
9365
9366   if (last_period == 0)
9367     strcpy (p, section_desc);
9368   else
9369     *p = '\0';
9370 }
9371 \f
9372 /* Emit profile function.  */
9373
9374 void
9375 output_profile_hook (labelno)
9376      int labelno;
9377 {
9378   if (DEFAULT_ABI == ABI_AIX)
9379     {
9380       char buf[30];
9381       const char *label_name;
9382       rtx fun;
9383
9384       labelno += 1;
9385
9386       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
9387       STRIP_NAME_ENCODING (label_name, ggc_strdup (buf));
9388       fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
9389
9390       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
9391                          fun, Pmode);
9392     }
9393   else if (DEFAULT_ABI == ABI_DARWIN)
9394     {
9395       const char *mcount_name = RS6000_MCOUNT;
9396       int caller_addr_regno = LINK_REGISTER_REGNUM;
9397
9398       /* Be conservative and always set this, at least for now.  */
9399       current_function_uses_pic_offset_table = 1;
9400
9401 #if TARGET_MACHO
9402       /* For PIC code, set up a stub and collect the caller's address
9403          from r0, which is where the prologue puts it.  */
9404       if (flag_pic)
9405         {
9406           mcount_name = machopic_stub_name (mcount_name);
9407           if (current_function_uses_pic_offset_table)
9408             caller_addr_regno = 0;
9409         }
9410 #endif
9411       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
9412                          0, VOIDmode, 1,
9413                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
9414     }
9415 }
9416
9417 /* Write function profiler code.  */
9418
9419 void
9420 output_function_profiler (file, labelno)
9421   FILE *file;
9422   int labelno;
9423 {
9424   char buf[100];
9425
9426   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
9427   switch (DEFAULT_ABI)
9428     {
9429     default:
9430       abort ();
9431
9432     case ABI_V4:
9433     case ABI_AIX_NODESC:
9434       fprintf (file, "\tmflr %s\n", reg_names[0]);
9435       if (flag_pic == 1)
9436         {
9437           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
9438           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
9439                        reg_names[0], reg_names[1]);
9440           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
9441           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
9442           assemble_name (file, buf);
9443           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
9444         }
9445       else if (flag_pic > 1)
9446         {
9447           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
9448                        reg_names[0], reg_names[1]);
9449           /* Now, we need to get the address of the label.  */
9450           fputs ("\tbl 1f\n\t.long ", file);
9451           assemble_name (file, buf);
9452           fputs ("-.\n1:", file);
9453           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
9454           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
9455                        reg_names[0], reg_names[11]);
9456           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
9457                        reg_names[0], reg_names[0], reg_names[11]);
9458         }
9459       else
9460         {
9461           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
9462           assemble_name (file, buf);
9463           fputs ("@ha\n", file);
9464           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
9465                        reg_names[0], reg_names[1]);
9466           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
9467           assemble_name (file, buf);
9468           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
9469         }
9470
9471       if (current_function_needs_context)
9472         asm_fprintf (file, "\tmr %s,%s\n",
9473                      reg_names[30], reg_names[STATIC_CHAIN_REGNUM]);
9474       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
9475       if (current_function_needs_context)
9476         asm_fprintf (file, "\tmr %s,%s\n",
9477                      reg_names[STATIC_CHAIN_REGNUM], reg_names[30]);
9478       break;
9479
9480     case ABI_AIX:
9481     case ABI_DARWIN:
9482       /* Don't do anything, done in output_profile_hook ().  */
9483       break;
9484
9485     }
9486 }
9487
9488 /* Adjust the cost of a scheduling dependency.  Return the new cost of
9489    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
9490
9491 static int
9492 rs6000_adjust_cost (insn, link, dep_insn, cost)
9493      rtx insn;
9494      rtx link;
9495      rtx dep_insn ATTRIBUTE_UNUSED;
9496      int cost;
9497 {
9498   if (! recog_memoized (insn))
9499     return 0;
9500
9501   if (REG_NOTE_KIND (link) != 0)
9502     return 0;
9503
9504   if (REG_NOTE_KIND (link) == 0)
9505     {
9506       /* Data dependency; DEP_INSN writes a register that INSN reads
9507          some cycles later.  */
9508       switch (get_attr_type (insn))
9509         {
9510         case TYPE_JMPREG:
9511           /* Tell the first scheduling pass about the latency between
9512              a mtctr and bctr (and mtlr and br/blr).  The first
9513              scheduling pass will not know about this latency since
9514              the mtctr instruction, which has the latency associated
9515              to it, will be generated by reload.  */
9516           return TARGET_POWER ? 5 : 4;
9517         case TYPE_BRANCH:
9518           /* Leave some extra cycles between a compare and its
9519              dependent branch, to inhibit expensive mispredicts.  */
9520           if ((rs6000_cpu_attr == CPU_PPC750
9521                || rs6000_cpu_attr == CPU_PPC7400
9522                || rs6000_cpu_attr == CPU_PPC7450)
9523               && recog_memoized (dep_insn)
9524               && (INSN_CODE (dep_insn) >= 0)
9525               && (get_attr_type (dep_insn) == TYPE_COMPARE
9526                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
9527                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
9528                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL))
9529             return cost + 2;
9530         default:
9531           break;
9532         }
9533       /* Fall out to return default cost.  */
9534     }
9535
9536   return cost;
9537 }
9538
9539 /* A C statement (sans semicolon) to update the integer scheduling
9540    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
9541    INSN earlier, increase the priority to execute INSN later.  Do not
9542    define this macro if you do not need to adjust the scheduling
9543    priorities of insns.  */
9544
9545 static int
9546 rs6000_adjust_priority (insn, priority)
9547      rtx insn ATTRIBUTE_UNUSED;
9548      int priority;
9549 {
9550   /* On machines (like the 750) which have asymmetric integer units,
9551      where one integer unit can do multiply and divides and the other
9552      can't, reduce the priority of multiply/divide so it is scheduled
9553      before other integer operations.  */
9554
9555 #if 0
9556   if (! INSN_P (insn))
9557     return priority;
9558
9559   if (GET_CODE (PATTERN (insn)) == USE)
9560     return priority;
9561
9562   switch (rs6000_cpu_attr) {
9563   case CPU_PPC750:
9564     switch (get_attr_type (insn))
9565       {
9566       default:
9567         break;
9568
9569       case TYPE_IMUL:
9570       case TYPE_IDIV:
9571         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
9572                  priority, priority);
9573         if (priority >= 0 && priority < 0x01000000)
9574           priority >>= 3;
9575         break;
9576       }
9577   }
9578 #endif
9579
9580   return priority;
9581 }
9582
9583 /* Return how many instructions the machine can issue per cycle.  */
9584
9585 static int
9586 rs6000_issue_rate ()
9587 {
9588   switch (rs6000_cpu_attr) {
9589   case CPU_RIOS1:  /* ? */
9590   case CPU_RS64A:
9591   case CPU_PPC601: /* ? */
9592   case CPU_PPC7450:
9593     return 3;
9594   case CPU_PPC603:
9595   case CPU_PPC750:
9596   case CPU_PPC7400:
9597     return 2; 
9598   case CPU_RIOS2:
9599   case CPU_PPC604:
9600   case CPU_PPC604E:
9601   case CPU_PPC620:
9602   case CPU_PPC630:
9603     return 4;
9604   default:
9605     return 1;
9606   }
9607 }
9608
9609 \f
9610 /* Length in units of the trampoline for entering a nested function.  */
9611
9612 int
9613 rs6000_trampoline_size ()
9614 {
9615   int ret = 0;
9616
9617   switch (DEFAULT_ABI)
9618     {
9619     default:
9620       abort ();
9621
9622     case ABI_AIX:
9623       ret = (TARGET_32BIT) ? 12 : 24;
9624       break;
9625
9626     case ABI_DARWIN:
9627     case ABI_V4:
9628     case ABI_AIX_NODESC:
9629       ret = (TARGET_32BIT) ? 40 : 48;
9630       break;
9631     }
9632
9633   return ret;
9634 }
9635
9636 /* Emit RTL insns to initialize the variable parts of a trampoline.
9637    FNADDR is an RTX for the address of the function's pure code.
9638    CXT is an RTX for the static chain value for the function.  */
9639
9640 void
9641 rs6000_initialize_trampoline (addr, fnaddr, cxt)
9642      rtx addr;
9643      rtx fnaddr;
9644      rtx cxt;
9645 {
9646   enum machine_mode pmode = Pmode;
9647   int regsize = (TARGET_32BIT) ? 4 : 8;
9648   rtx ctx_reg = force_reg (pmode, cxt);
9649
9650   switch (DEFAULT_ABI)
9651     {
9652     default:
9653       abort ();
9654
9655 /* Macros to shorten the code expansions below.  */
9656 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
9657 #define MEM_PLUS(addr,offset) \
9658   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
9659
9660     /* Under AIX, just build the 3 word function descriptor */
9661     case ABI_AIX:
9662       {
9663         rtx fn_reg = gen_reg_rtx (pmode);
9664         rtx toc_reg = gen_reg_rtx (pmode);
9665         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
9666         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
9667         emit_move_insn (MEM_DEREF (addr), fn_reg);
9668         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
9669         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
9670       }
9671       break;
9672
9673     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
9674     case ABI_DARWIN:
9675     case ABI_V4:
9676     case ABI_AIX_NODESC:
9677       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
9678                          FALSE, VOIDmode, 4,
9679                          addr, pmode,
9680                          GEN_INT (rs6000_trampoline_size ()), SImode,
9681                          fnaddr, pmode,
9682                          ctx_reg, pmode);
9683       break;
9684     }
9685
9686   return;
9687 }
9688
9689 \f
9690 /* Table of valid machine attributes.  */
9691
9692 const struct attribute_spec rs6000_attribute_table[] =
9693 {
9694   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
9695   { "longcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
9696   { NULL,       0, 0, false, false, false, NULL }
9697 };
9698
9699 /* Handle a "longcall" attribute; arguments as in struct
9700    attribute_spec.handler.  */
9701
9702 static tree
9703 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
9704      tree *node;
9705      tree name;
9706      tree args ATTRIBUTE_UNUSED;
9707      int flags ATTRIBUTE_UNUSED;
9708      bool *no_add_attrs;
9709 {
9710   if (TREE_CODE (*node) != FUNCTION_TYPE
9711       && TREE_CODE (*node) != FIELD_DECL
9712       && TREE_CODE (*node) != TYPE_DECL)
9713     {
9714       warning ("`%s' attribute only applies to functions",
9715                IDENTIFIER_POINTER (name));
9716       *no_add_attrs = true;
9717     }
9718
9719   return NULL_TREE;
9720 }
9721
9722 /* Return a reference suitable for calling a function with the
9723    longcall attribute.  */
9724
9725 struct rtx_def *
9726 rs6000_longcall_ref (call_ref)
9727      rtx call_ref;
9728 {
9729   const char *call_name;
9730   tree node;
9731
9732   if (GET_CODE (call_ref) != SYMBOL_REF)
9733     return call_ref;
9734
9735   /* System V adds '.' to the internal name, so skip them.  */
9736   call_name = XSTR (call_ref, 0);
9737   if (*call_name == '.')
9738     {
9739       while (*call_name == '.')
9740         call_name++;
9741
9742       node = get_identifier (call_name);
9743       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
9744     }
9745
9746   return force_reg (Pmode, call_ref);
9747 }
9748
9749 \f
9750 /* A C statement or statements to switch to the appropriate section
9751    for output of RTX in mode MODE.  You can assume that RTX is some
9752    kind of constant in RTL.  The argument MODE is redundant except in
9753    the case of a `const_int' rtx.  Select the section by calling
9754    `text_section' or one of the alternatives for other sections.
9755
9756    Do not define this macro if you put all constants in the read-only
9757    data section.  */
9758
9759 #ifdef USING_ELFOS_H
9760
9761 void
9762 rs6000_select_rtx_section (mode, x)
9763      enum machine_mode mode;
9764      rtx x;
9765 {
9766   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
9767     toc_section ();
9768   else
9769     const_section ();
9770 }
9771
9772 /* A C statement or statements to switch to the appropriate
9773    section for output of DECL.  DECL is either a `VAR_DECL' node
9774    or a constant of some sort.  RELOC indicates whether forming
9775    the initial value of DECL requires link-time relocations.  */
9776
9777 void
9778 rs6000_select_section (decl, reloc)
9779      tree decl;
9780      int reloc;
9781 {
9782   int size = int_size_in_bytes (TREE_TYPE (decl));
9783   int needs_sdata;
9784   int readonly;
9785   static void (* const sec_funcs[4]) PARAMS ((void)) = {
9786     &const_section,
9787     &sdata2_section,
9788     &data_section,
9789     &sdata_section
9790   };
9791   
9792   needs_sdata = (size > 0 
9793                  && size <= g_switch_value
9794                  && rs6000_sdata != SDATA_NONE
9795                  && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
9796
9797   if (TREE_CODE (decl) == STRING_CST)
9798     readonly = ! flag_writable_strings;
9799   else if (TREE_CODE (decl) == VAR_DECL)
9800     readonly = (! (flag_pic && reloc)
9801                 && TREE_READONLY (decl)
9802                 && ! TREE_SIDE_EFFECTS (decl)
9803                 && DECL_INITIAL (decl)
9804                 && DECL_INITIAL (decl) != error_mark_node
9805                 && TREE_CONSTANT (DECL_INITIAL (decl)));
9806   else if (TREE_CODE (decl) == CONSTRUCTOR)
9807     readonly = (! (flag_pic && reloc)
9808                 && ! TREE_SIDE_EFFECTS (decl)
9809                 && TREE_CONSTANT (decl));
9810   else
9811     readonly = 1;
9812   if (needs_sdata && rs6000_sdata != SDATA_EABI)
9813     readonly = 0;
9814   
9815   (*sec_funcs[(readonly ? 0 : 2) + (needs_sdata ? 1 : 0)])();
9816 }
9817
9818 /* A C statement to build up a unique section name, expressed as a
9819    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
9820    RELOC indicates whether the initial value of EXP requires
9821    link-time relocations.  If you do not define this macro, GCC will use
9822    the symbol name prefixed by `.' as the section name.  Note - this
9823    macro can now be called for uninitialized data items as well as
9824    initialised data and functions.  */
9825
9826 void
9827 rs6000_unique_section (decl, reloc)
9828      tree decl;
9829      int reloc;
9830 {
9831   int len;
9832   int sec;
9833   const char *name;
9834   char *string;
9835   const char *prefix;
9836
9837   static const char *const prefixes[7][2] =
9838   {
9839     { ".rodata.", ".gnu.linkonce.r." },
9840     { ".sdata2.", ".gnu.linkonce.s2." },
9841     { ".data.",   ".gnu.linkonce.d." },
9842     { ".sdata.",  ".gnu.linkonce.s." },
9843     { ".bss.",    ".gnu.linkonce.b." },
9844     { ".sbss.",   ".gnu.linkonce.sb." },
9845     { ".text.",   ".gnu.linkonce.t." }
9846   };
9847
9848   if (TREE_CODE (decl) == FUNCTION_DECL)
9849     sec = 6;
9850   else
9851     {
9852       int readonly;
9853       int needs_sdata;
9854       int size;
9855
9856       readonly = 1;
9857       if (TREE_CODE (decl) == STRING_CST)
9858         readonly = ! flag_writable_strings;
9859       else if (TREE_CODE (decl) == VAR_DECL)
9860         readonly = (! (flag_pic && reloc)
9861                     && TREE_READONLY (decl)
9862                     && ! TREE_SIDE_EFFECTS (decl)
9863                     && TREE_CONSTANT (DECL_INITIAL (decl)));
9864
9865       size = int_size_in_bytes (TREE_TYPE (decl));
9866       needs_sdata = (size > 0 
9867                      && size <= g_switch_value
9868                      && rs6000_sdata != SDATA_NONE
9869                      && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
9870
9871       if (DECL_INITIAL (decl) == 0
9872           || DECL_INITIAL (decl) == error_mark_node)
9873         sec = 4;
9874       else if (! readonly)
9875         sec = 2;
9876       else
9877         sec = 0;
9878
9879       if (needs_sdata)
9880         {
9881           /* .sdata2 is only for EABI.  */
9882           if (sec == 0 && rs6000_sdata != SDATA_EABI)
9883             sec = 2;
9884           sec += 1;
9885         }
9886     }
9887
9888   STRIP_NAME_ENCODING (name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9889   prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
9890   len    = strlen (name) + strlen (prefix);
9891   string = alloca (len + 1);
9892   
9893   sprintf (string, "%s%s", prefix, name);
9894   
9895   DECL_SECTION_NAME (decl) = build_string (len, string);
9896 }
9897
9898 \f
9899 /* If we are referencing a function that is static or is known to be
9900    in this file, make the SYMBOL_REF special.  We can use this to indicate
9901    that we can branch to this function without emitting a no-op after the
9902    call.  For real AIX calling sequences, we also replace the
9903    function name with the real name (1 or 2 leading .'s), rather than
9904    the function descriptor name.  This saves a lot of overriding code
9905    to read the prefixes.  */
9906
9907 void
9908 rs6000_encode_section_info (decl)
9909      tree decl;
9910 {
9911   if (TREE_CODE (decl) == FUNCTION_DECL)
9912     {
9913       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
9914       if ((TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
9915           && ! DECL_WEAK (decl))
9916         SYMBOL_REF_FLAG (sym_ref) = 1;
9917
9918       if (DEFAULT_ABI == ABI_AIX)
9919         {
9920           size_t len1 = (DEFAULT_ABI == ABI_AIX) ? 1 : 2;
9921           size_t len2 = strlen (XSTR (sym_ref, 0));
9922           char *str = alloca (len1 + len2 + 1);
9923           str[0] = '.';
9924           str[1] = '.';
9925           memcpy (str + len1, XSTR (sym_ref, 0), len2 + 1);
9926
9927           XSTR (sym_ref, 0) = ggc_alloc_string (str, len1 + len2);
9928         }
9929     }
9930   else if (rs6000_sdata != SDATA_NONE
9931            && DEFAULT_ABI == ABI_V4
9932            && TREE_CODE (decl) == VAR_DECL)
9933     {
9934       int size = int_size_in_bytes (TREE_TYPE (decl));
9935       tree section_name = DECL_SECTION_NAME (decl);
9936       const char *name = (char *)0;
9937       int len = 0;
9938
9939       if (section_name)
9940         {
9941           if (TREE_CODE (section_name) == STRING_CST)
9942             {
9943               name = TREE_STRING_POINTER (section_name);
9944               len = TREE_STRING_LENGTH (section_name);
9945             }
9946           else
9947             abort ();
9948         }
9949
9950       if ((size > 0 && size <= g_switch_value)
9951           || (name
9952               && ((len == sizeof (".sdata") - 1
9953                    && strcmp (name, ".sdata") == 0)
9954                   || (len == sizeof (".sdata2") - 1
9955                       && strcmp (name, ".sdata2") == 0)
9956                   || (len == sizeof (".sbss") - 1
9957                       && strcmp (name, ".sbss") == 0)
9958                   || (len == sizeof (".sbss2") - 1
9959                       && strcmp (name, ".sbss2") == 0)
9960                   || (len == sizeof (".PPC.EMB.sdata0") - 1
9961                       && strcmp (name, ".PPC.EMB.sdata0") == 0)
9962                   || (len == sizeof (".PPC.EMB.sbss0") - 1
9963                       && strcmp (name, ".PPC.EMB.sbss0") == 0))))
9964         {
9965           rtx sym_ref = XEXP (DECL_RTL (decl), 0);
9966           size_t len = strlen (XSTR (sym_ref, 0));
9967           char *str = alloca (len + 2);
9968
9969           str[0] = '@';
9970           memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
9971           XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
9972         }
9973     }
9974 }
9975
9976 #endif /* USING_ELFOS_H */
9977
9978 \f
9979 /* Return a REG that occurs in ADDR with coefficient 1.
9980    ADDR can be effectively incremented by incrementing REG.
9981
9982    r0 is special and we must not select it as an address
9983    register by this routine since our caller will try to
9984    increment the returned register via an "la" instruction.  */
9985
9986 struct rtx_def *
9987 find_addr_reg (addr)
9988      rtx addr;
9989 {
9990   while (GET_CODE (addr) == PLUS)
9991     {
9992       if (GET_CODE (XEXP (addr, 0)) == REG
9993           && REGNO (XEXP (addr, 0)) != 0)
9994         addr = XEXP (addr, 0);
9995       else if (GET_CODE (XEXP (addr, 1)) == REG
9996                && REGNO (XEXP (addr, 1)) != 0)
9997         addr = XEXP (addr, 1);
9998       else if (CONSTANT_P (XEXP (addr, 0)))
9999         addr = XEXP (addr, 1);
10000       else if (CONSTANT_P (XEXP (addr, 1)))
10001         addr = XEXP (addr, 0);
10002       else
10003         abort ();
10004     }
10005   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
10006     return addr;
10007   abort ();
10008 }
10009
10010 void
10011 rs6000_fatal_bad_address (op)
10012   rtx op;
10013 {
10014   fatal_insn ("bad address", op);
10015 }
10016
10017 /* Called to register all of our global variables with the garbage
10018    collector.  */
10019
10020 static void
10021 rs6000_add_gc_roots ()
10022 {
10023   ggc_add_rtx_root (&rs6000_compare_op0, 1);
10024   ggc_add_rtx_root (&rs6000_compare_op1, 1);
10025
10026   toc_hash_table = htab_create (1021, toc_hash_function, toc_hash_eq, NULL);
10027   ggc_add_root (&toc_hash_table, 1, sizeof (toc_hash_table), 
10028                 toc_hash_mark_table);
10029
10030 #if TARGET_MACHO
10031   machopic_add_gc_roots ();
10032 #endif
10033 }
10034
10035 #if TARGET_MACHO
10036
10037 #if 0
10038 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
10039    reference and a constant.  */
10040
10041 int
10042 symbolic_operand (op)
10043      rtx op;
10044 {
10045   switch (GET_CODE (op))
10046     {
10047     case SYMBOL_REF:
10048     case LABEL_REF:
10049       return 1;
10050     case CONST:
10051       op = XEXP (op, 0);
10052       return (GET_CODE (op) == SYMBOL_REF ||
10053               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
10054                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
10055               && GET_CODE (XEXP (op, 1)) == CONST_INT);
10056     default:
10057       return 0;
10058     }
10059 }
10060 #endif
10061
10062 #ifdef RS6000_LONG_BRANCH
10063
10064 static tree stub_list = 0;
10065
10066 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
10067    procedure calls to the linked list.  */
10068
10069 void 
10070 add_compiler_stub (label_name, function_name, line_number)
10071      tree label_name;
10072      tree function_name;
10073      int line_number;
10074 {
10075   tree stub = build_tree_list (function_name, label_name);
10076   TREE_TYPE (stub) = build_int_2 (line_number, 0);
10077   TREE_CHAIN (stub) = stub_list;
10078   stub_list = stub;
10079 }
10080
10081 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
10082 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
10083 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
10084
10085 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
10086    handling procedure calls from the linked list and initializes the
10087    linked list.  */
10088
10089 void
10090 output_compiler_stub ()
10091 {
10092   char tmp_buf[256];
10093   char label_buf[256];
10094   char *label;
10095   tree tmp_stub, stub;
10096
10097   if (!flag_pic)
10098     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
10099       {
10100         fprintf (asm_out_file,
10101                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
10102
10103 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
10104         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
10105           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
10106 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
10107
10108         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
10109           strcpy (label_buf,
10110                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
10111         else
10112           {
10113             label_buf[0] = '_';
10114             strcpy (label_buf+1,
10115                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
10116           }
10117
10118         strcpy (tmp_buf, "lis r12,hi16(");
10119         strcat (tmp_buf, label_buf);
10120         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
10121         strcat (tmp_buf, label_buf);
10122         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
10123         output_asm_insn (tmp_buf, 0);
10124
10125 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
10126         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
10127           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
10128 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
10129       }
10130
10131   stub_list = 0;
10132 }
10133
10134 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
10135    already there or not.  */
10136
10137 int
10138 no_previous_def (function_name)
10139      tree function_name;
10140 {
10141   tree stub;
10142   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
10143     if (function_name == STUB_FUNCTION_NAME (stub))
10144       return 0;
10145   return 1;
10146 }
10147
10148 /* GET_PREV_LABEL gets the label name from the previous definition of
10149    the function.  */
10150
10151 tree
10152 get_prev_label (function_name)
10153      tree function_name;
10154 {
10155   tree stub;
10156   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
10157     if (function_name == STUB_FUNCTION_NAME (stub))
10158       return STUB_LABEL_NAME (stub);
10159   return 0;
10160 }
10161
10162 /* INSN is either a function call or a millicode call.  It may have an
10163    unconditional jump in its delay slot.  
10164
10165    CALL_DEST is the routine we are calling.  */
10166
10167 char *
10168 output_call (insn, call_dest, operand_number)
10169      rtx insn;
10170      rtx call_dest;
10171      int operand_number;
10172 {
10173   static char buf[256];
10174   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
10175     {
10176       tree labelname;
10177       tree funname = get_identifier (XSTR (call_dest, 0));
10178       
10179       if (no_previous_def (funname))
10180         {
10181           int line_number;
10182           rtx label_rtx = gen_label_rtx ();
10183           char *label_buf, temp_buf[256];
10184           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
10185                                        CODE_LABEL_NUMBER (label_rtx));
10186           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
10187           labelname = get_identifier (label_buf);
10188           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
10189           if (insn)
10190             line_number = NOTE_LINE_NUMBER (insn);
10191           add_compiler_stub (labelname, funname, line_number);
10192         }
10193       else
10194         labelname = get_prev_label (funname);
10195
10196       sprintf (buf, "jbsr %%z%d,%.246s",
10197                operand_number, IDENTIFIER_POINTER (labelname));
10198       return buf;
10199     }
10200   else
10201     {
10202       sprintf (buf, "bl %%z%d", operand_number);
10203       return buf;
10204     }
10205 }
10206
10207 #endif /* RS6000_LONG_BRANCH */
10208
10209 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
10210   do {                                                          \
10211     const char *const symbol_ = (SYMBOL);                       \
10212     char *buffer_ = (BUF);                                      \
10213     if (symbol_[0] == '"')                                      \
10214       {                                                         \
10215         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
10216       }                                                         \
10217     else if (name_needs_quotes(symbol_))                        \
10218       {                                                         \
10219         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
10220       }                                                         \
10221     else                                                        \
10222       {                                                         \
10223         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
10224       }                                                         \
10225   } while (0)
10226
10227
10228 /* Generate PIC and indirect symbol stubs.  */
10229
10230 void
10231 machopic_output_stub (file, symb, stub)
10232      FILE *file;
10233      const char *symb, *stub;
10234 {
10235   unsigned int length;
10236   char *symbol_name, *lazy_ptr_name;
10237   char *local_label_0;
10238   static int label = 0;
10239
10240   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
10241   STRIP_NAME_ENCODING (symb, symb);
10242
10243   label += 1;
10244
10245   length = strlen (symb);
10246   symbol_name = alloca (length + 32);
10247   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
10248
10249   lazy_ptr_name = alloca (length + 32);
10250   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
10251
10252   local_label_0 = alloca (length + 32);
10253   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
10254
10255   if (flag_pic == 2)
10256     machopic_picsymbol_stub_section ();
10257   else
10258     machopic_symbol_stub_section ();
10259
10260   fprintf (file, "%s:\n", stub);
10261   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
10262
10263   if (flag_pic == 2)
10264     {
10265       fprintf (file, "\tmflr r0\n");
10266       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
10267       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
10268       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
10269                lazy_ptr_name, local_label_0);
10270       fprintf (file, "\tmtlr r0\n");
10271       fprintf (file, "\tlwz r12,lo16(%s-%s)(r11)\n",
10272                lazy_ptr_name, local_label_0);
10273       fprintf (file, "\tmtctr r12\n");
10274       fprintf (file, "\taddi r11,r11,lo16(%s-%s)\n",
10275                lazy_ptr_name, local_label_0);
10276       fprintf (file, "\tbctr\n");
10277     }
10278   else
10279     fprintf (file, "non-pure not supported\n");
10280   
10281   machopic_lazy_symbol_ptr_section ();
10282   fprintf (file, "%s:\n", lazy_ptr_name);
10283   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
10284   fprintf (file, "\t.long dyld_stub_binding_helper\n");
10285 }
10286
10287 /* Legitimize PIC addresses.  If the address is already
10288    position-independent, we return ORIG.  Newly generated
10289    position-independent addresses go into a reg.  This is REG if non
10290    zero, otherwise we allocate register(s) as necessary.  */
10291
10292 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
10293
10294 rtx
10295 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
10296      rtx orig;
10297      enum machine_mode mode;
10298      rtx reg;
10299 {
10300   rtx base, offset;
10301
10302   if (reg == NULL && ! reload_in_progress && ! reload_completed)
10303     reg = gen_reg_rtx (Pmode);
10304
10305   if (GET_CODE (orig) == CONST)
10306     {
10307       if (GET_CODE (XEXP (orig, 0)) == PLUS
10308           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
10309         return orig;
10310
10311       if (GET_CODE (XEXP (orig, 0)) == PLUS)
10312         {
10313           base =
10314             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
10315                                                     Pmode, reg);
10316           offset =
10317             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
10318                                                     Pmode, reg);
10319         }
10320       else
10321         abort ();
10322
10323       if (GET_CODE (offset) == CONST_INT)
10324         {
10325           if (SMALL_INT (offset))
10326             return plus_constant (base, INTVAL (offset));
10327           else if (! reload_in_progress && ! reload_completed)
10328             offset = force_reg (Pmode, offset);
10329           else
10330             {
10331               rtx mem = force_const_mem (Pmode, orig);
10332               return machopic_legitimize_pic_address (mem, Pmode, reg);
10333             }
10334         }
10335       return gen_rtx (PLUS, Pmode, base, offset);
10336     }
10337
10338   /* Fall back on generic machopic code.  */
10339   return machopic_legitimize_pic_address (orig, mode, reg);
10340 }
10341
10342 /* This is just a placeholder to make linking work without having to
10343    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
10344    ever needed for Darwin (not too likely!) this would have to get a
10345    real definition.  */
10346
10347 void
10348 toc_section ()
10349 {
10350 }
10351
10352 #endif /* TARGET_MACHO */
10353
10354 #if TARGET_ELF
10355 static unsigned int
10356 rs6000_elf_section_type_flags (decl, name, reloc)
10357      tree decl;
10358      const char *name;
10359      int reloc;
10360 {
10361   unsigned int flags = default_section_type_flags (decl, name, reloc);
10362
10363   if (TARGET_RELOCATABLE)
10364     flags |= SECTION_WRITE;
10365
10366   return flags;
10367 }
10368
10369 /* Record an element in the table of global constructors.  SYMBOL is
10370    a SYMBOL_REF of the function to be called; PRIORITY is a number
10371    between 0 and MAX_INIT_PRIORITY.
10372
10373    This differs from default_named_section_asm_out_constructor in
10374    that we have special handling for -mrelocatable.  */
10375
10376 static void
10377 rs6000_elf_asm_out_constructor (symbol, priority)
10378      rtx symbol;
10379      int priority;
10380 {
10381   const char *section = ".ctors";
10382   char buf[16];
10383
10384   if (priority != DEFAULT_INIT_PRIORITY)
10385     {
10386       sprintf (buf, ".ctors.%.5u",
10387                /* Invert the numbering so the linker puts us in the proper
10388                   order; constructors are run from right to left, and the
10389                   linker sorts in increasing order.  */
10390                MAX_INIT_PRIORITY - priority);
10391       section = buf;
10392     }
10393
10394   named_section_flags (section, SECTION_WRITE);
10395   assemble_align (POINTER_SIZE);
10396
10397   if (TARGET_RELOCATABLE)
10398     {
10399       fputs ("\t.long (", asm_out_file);
10400       output_addr_const (asm_out_file, symbol);
10401       fputs (")@fixup\n", asm_out_file);
10402     }
10403   else
10404     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
10405 }
10406
10407 static void
10408 rs6000_elf_asm_out_destructor (symbol, priority)
10409      rtx symbol;
10410      int priority;
10411 {
10412   const char *section = ".dtors";
10413   char buf[16];
10414
10415   if (priority != DEFAULT_INIT_PRIORITY)
10416     {
10417       sprintf (buf, ".dtors.%.5u",
10418                /* Invert the numbering so the linker puts us in the proper
10419                   order; constructors are run from right to left, and the
10420                   linker sorts in increasing order.  */
10421                MAX_INIT_PRIORITY - priority);
10422       section = buf;
10423     }
10424
10425   named_section_flags (section, SECTION_WRITE);
10426   assemble_align (POINTER_SIZE);
10427
10428   if (TARGET_RELOCATABLE)
10429     {
10430       fputs ("\t.long (", asm_out_file);
10431       output_addr_const (asm_out_file, symbol);
10432       fputs (")@fixup\n", asm_out_file);
10433     }
10434   else
10435     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
10436 }
10437 #endif
10438
10439 #ifdef OBJECT_FORMAT_COFF
10440 static void
10441 xcoff_asm_named_section (name, flags)
10442      const char *name;
10443      unsigned int flags ATTRIBUTE_UNUSED;
10444 {
10445   fprintf (asm_out_file, "\t.csect %s\n", name);
10446 }
10447 #endif