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