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