OSDN Git Service

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