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