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