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