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