(output_return_instruction): Do not create the conditional part of the return
[official-gcc.git] / gcc / config / arm / arm.c
blob75dd8b2543a060b1af8d99eb9fe9b530c9892259
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5 and Martin Simmons (@harleqn.co.uk).
6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
8 This file is part of GNU CC.
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "obstack.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "reload.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "toplev.h"
43 #include "recog.h"
44 #include "ggc.h"
45 #include "except.h"
46 #include "c-pragma.h"
47 #include "integrate.h"
48 #include "tm_p.h"
49 #include "target.h"
50 #include "target-def.h"
52 /* Forward definitions of types. */
53 typedef struct minipool_node Mnode;
54 typedef struct minipool_fixup Mfix;
56 /* In order to improve the layout of the prototypes below
57 some short type abbreviations are defined here. */
58 #define Hint HOST_WIDE_INT
59 #define Mmode enum machine_mode
60 #define Ulong unsigned long
61 #define Ccstar const char *
63 const struct attribute_spec arm_attribute_table[];
65 /* Forward function declarations. */
66 static void arm_add_gc_roots PARAMS ((void));
67 static int arm_gen_constant PARAMS ((enum rtx_code, Mmode, Hint, rtx, rtx, int, int));
68 static Ulong bit_count PARAMS ((signed int));
69 static int const_ok_for_op PARAMS ((Hint, enum rtx_code));
70 static int eliminate_lr2ip PARAMS ((rtx *));
71 static rtx emit_multi_reg_push PARAMS ((int));
72 static rtx emit_sfm PARAMS ((int, int));
73 #ifndef AOF_ASSEMBLER
74 static bool arm_assemble_integer PARAMS ((rtx, unsigned int, int));
75 #endif
76 static Ccstar fp_const_from_val PARAMS ((REAL_VALUE_TYPE *));
77 static arm_cc get_arm_condition_code PARAMS ((rtx));
78 static void init_fpa_table PARAMS ((void));
79 static Hint int_log2 PARAMS ((Hint));
80 static rtx is_jump_table PARAMS ((rtx));
81 static Ccstar output_multi_immediate PARAMS ((rtx *, Ccstar, Ccstar, int, Hint));
82 static void print_multi_reg PARAMS ((FILE *, Ccstar, int, int));
83 static Mmode select_dominance_cc_mode PARAMS ((rtx, rtx, Hint));
84 static Ccstar shift_op PARAMS ((rtx, Hint *));
85 static void arm_init_machine_status PARAMS ((struct function *));
86 static void arm_mark_machine_status PARAMS ((struct function *));
87 static void arm_free_machine_status PARAMS ((struct function *));
88 static int number_of_first_bit_set PARAMS ((int));
89 static void replace_symbols_in_block PARAMS ((tree, rtx, rtx));
90 static void thumb_exit PARAMS ((FILE *, int, rtx));
91 static void thumb_pushpop PARAMS ((FILE *, int, int));
92 static Ccstar thumb_condition_code PARAMS ((rtx, int));
93 static rtx is_jump_table PARAMS ((rtx));
94 static Hint get_jump_table_size PARAMS ((rtx));
95 static Mnode * move_minipool_fix_forward_ref PARAMS ((Mnode *, Mnode *, Hint));
96 static Mnode * add_minipool_forward_ref PARAMS ((Mfix *));
97 static Mnode * move_minipool_fix_backward_ref PARAMS ((Mnode *, Mnode *, Hint));
98 static Mnode * add_minipool_backward_ref PARAMS ((Mfix *));
99 static void assign_minipool_offsets PARAMS ((Mfix *));
100 static void arm_print_value PARAMS ((FILE *, rtx));
101 static void dump_minipool PARAMS ((rtx));
102 static int arm_barrier_cost PARAMS ((rtx));
103 static Mfix * create_fix_barrier PARAMS ((Mfix *, Hint));
104 static void push_minipool_barrier PARAMS ((rtx, Hint));
105 static void push_minipool_fix PARAMS ((rtx, Hint, rtx *, Mmode, rtx));
106 static void note_invalid_constants PARAMS ((rtx, Hint));
107 static int current_file_function_operand PARAMS ((rtx));
108 static Ulong arm_compute_save_reg0_reg12_mask PARAMS ((void));
109 static Ulong arm_compute_save_reg_mask PARAMS ((void));
110 static Ulong arm_isr_value PARAMS ((tree));
111 static Ulong arm_compute_func_type PARAMS ((void));
112 static tree arm_handle_fndecl_attribute PARAMS ((tree *, tree, tree, int, bool *));
113 static tree arm_handle_isr_attribute PARAMS ((tree *, tree, tree, int, bool *));
114 static void arm_output_function_epilogue PARAMS ((FILE *, Hint));
115 static void arm_output_function_prologue PARAMS ((FILE *, Hint));
116 static void thumb_output_function_prologue PARAMS ((FILE *, Hint));
117 static int arm_comp_type_attributes PARAMS ((tree, tree));
118 static void arm_set_default_type_attributes PARAMS ((tree));
119 static int arm_adjust_cost PARAMS ((rtx, rtx, rtx, int));
120 #ifdef OBJECT_FORMAT_ELF
121 static void arm_elf_asm_named_section PARAMS ((const char *, unsigned int));
122 #endif
124 #undef Hint
125 #undef Mmode
126 #undef Ulong
127 #undef Ccstar
129 /* Initialize the GCC target structure. */
130 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
131 #undef TARGET_MERGE_DECL_ATTRIBUTES
132 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
133 #endif
135 #undef TARGET_ATTRIBUTE_TABLE
136 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
138 #ifdef AOF_ASSEMBLER
139 #undef TARGET_ASM_BYTE_OP
140 #define TARGET_ASM_BYTE_OP "\tDCB\t"
141 #undef TARGET_ASM_ALIGNED_HI_OP
142 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
143 #undef TARGET_ASM_ALIGNED_SI_OP
144 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
145 #else
146 #undef TARGET_ASM_ALIGNED_SI_OP
147 #define TARGET_ASM_ALIGNED_SI_OP NULL
148 #undef TARGET_ASM_INTEGER
149 #define TARGET_ASM_INTEGER arm_assemble_integer
150 #endif
152 #undef TARGET_ASM_FUNCTION_PROLOGUE
153 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
155 #undef TARGET_ASM_FUNCTION_EPILOGUE
156 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
158 #undef TARGET_COMP_TYPE_ATTRIBUTES
159 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
161 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
162 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
164 #undef TARGET_INIT_BUILTINS
165 #define TARGET_INIT_BUILTINS arm_init_builtins
167 #undef TARGET_EXPAND_BUILTIN
168 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
170 #undef TARGET_SCHED_ADJUST_COST
171 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
173 struct gcc_target targetm = TARGET_INITIALIZER;
175 /* Obstack for minipool constant handling. */
176 static struct obstack minipool_obstack;
177 static char * minipool_startobj;
179 #define obstack_chunk_alloc xmalloc
180 #define obstack_chunk_free free
182 /* The maximum number of insns skipped which
183 will be conditionalised if possible. */
184 static int max_insns_skipped = 5;
186 extern FILE * asm_out_file;
188 /* True if we are currently building a constant table. */
189 int making_const_table;
191 /* Define the information needed to generate branch insns. This is
192 stored from the compare operation. */
193 rtx arm_compare_op0, arm_compare_op1;
195 /* What type of floating point are we tuning for? */
196 enum floating_point_type arm_fpu;
198 /* What type of floating point instructions are available? */
199 enum floating_point_type arm_fpu_arch;
201 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode. */
202 enum prog_mode_type arm_prgmode;
204 /* Set by the -mfp=... option. */
205 const char * target_fp_name = NULL;
207 /* Used to parse -mstructure_size_boundary command line option. */
208 const char * structure_size_string = NULL;
209 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
211 /* Bit values used to identify processor capabilities. */
212 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
213 #define FL_FAST_MULT (1 << 1) /* Fast multiply */
214 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
215 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
216 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
217 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
218 #define FL_THUMB (1 << 6) /* Thumb aware */
219 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
220 #define FL_STRONG (1 << 8) /* StrongARM */
221 #define FL_ARCH5E (1 << 9) /* DSP extenstions to v5 */
222 #define FL_XSCALE (1 << 10) /* XScale */
224 /* The bits in this mask specify which
225 instructions we are allowed to generate. */
226 static int insn_flags = 0;
228 /* The bits in this mask specify which instruction scheduling options should
229 be used. Note - there is an overlap with the FL_FAST_MULT. For some
230 hardware we want to be able to generate the multiply instructions, but to
231 tune as if they were not present in the architecture. */
232 static int tune_flags = 0;
234 /* The following are used in the arm.md file as equivalents to bits
235 in the above two flag variables. */
237 /* Nonzero if this is an "M" variant of the processor. */
238 int arm_fast_multiply = 0;
240 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
241 int arm_arch4 = 0;
243 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
244 int arm_arch5 = 0;
246 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
247 int arm_arch5e = 0;
249 /* Nonzero if this chip can benefit from load scheduling. */
250 int arm_ld_sched = 0;
252 /* Nonzero if this chip is a StrongARM. */
253 int arm_is_strong = 0;
255 /* Nonzero if this chip is an XScale. */
256 int arm_is_xscale = 0;
258 /* Nonzero if this chip is an ARM6 or an ARM7. */
259 int arm_is_6_or_7 = 0;
261 /* Nonzero if generating Thumb instructions. */
262 int thumb_code = 0;
264 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
265 must report the mode of the memory reference from PRINT_OPERAND to
266 PRINT_OPERAND_ADDRESS. */
267 enum machine_mode output_memory_reference_mode;
269 /* The register number to be used for the PIC offset register. */
270 const char * arm_pic_register_string = NULL;
271 int arm_pic_register = INVALID_REGNUM;
273 /* Set to 1 when a return insn is output, this means that the epilogue
274 is not needed. */
275 int return_used_this_function;
277 /* Set to 1 after arm_reorg has started. Reset to start at the start of
278 the next function. */
279 static int after_arm_reorg = 0;
281 /* The maximum number of insns to be used when loading a constant. */
282 static int arm_constant_limit = 3;
284 /* For an explanation of these variables, see final_prescan_insn below. */
285 int arm_ccfsm_state;
286 enum arm_cond_code arm_current_cc;
287 rtx arm_target_insn;
288 int arm_target_label;
290 /* The condition codes of the ARM, and the inverse function. */
291 static const char * const arm_condition_codes[] =
293 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
294 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
297 #define streq(string1, string2) (strcmp (string1, string2) == 0)
299 /* Initialization code. */
301 struct processors
303 const char *const name;
304 const unsigned int flags;
307 /* Not all of these give usefully different compilation alternatives,
308 but there is no simple way of generalizing them. */
309 static const struct processors all_cores[] =
311 /* ARM Cores */
313 {"arm2", FL_CO_PROC | FL_MODE26 },
314 {"arm250", FL_CO_PROC | FL_MODE26 },
315 {"arm3", FL_CO_PROC | FL_MODE26 },
316 {"arm6", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
317 {"arm60", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
318 {"arm600", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
319 {"arm610", FL_MODE26 | FL_MODE32 },
320 {"arm620", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
321 {"arm7", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
322 /* arm7m doesn't exist on its own, but only with D, (and I), but
323 those don't alter the code, so arm7m is sometimes used. */
324 {"arm7m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
325 {"arm7d", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
326 {"arm7dm", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
327 {"arm7di", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
328 {"arm7dmi", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
329 {"arm70", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
330 {"arm700", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
331 {"arm700i", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
332 {"arm710", FL_MODE26 | FL_MODE32 },
333 {"arm710t", FL_MODE26 | FL_MODE32 | FL_THUMB },
334 {"arm720", FL_MODE26 | FL_MODE32 },
335 {"arm720t", FL_MODE26 | FL_MODE32 | FL_THUMB },
336 {"arm740t", FL_MODE26 | FL_MODE32 | FL_THUMB },
337 {"arm710c", FL_MODE26 | FL_MODE32 },
338 {"arm7100", FL_MODE26 | FL_MODE32 },
339 {"arm7500", FL_MODE26 | FL_MODE32 },
340 /* Doesn't have an external co-proc, but does have embedded fpu. */
341 {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
342 {"arm7tdmi", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
343 {"arm8", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
344 {"arm810", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
345 {"arm9", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
346 {"arm920", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
347 {"arm920t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
348 {"arm940t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
349 {"arm9tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
350 {"arm9e", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
351 {"strongarm", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
352 {"strongarm110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
353 {"strongarm1100", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
354 {"strongarm1110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
355 {"arm10tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_ARCH5 },
356 {"arm1020t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_ARCH5 },
357 {"xscale", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE },
359 {NULL, 0}
362 static const struct processors all_architectures[] =
364 /* ARM Architectures */
366 { "armv2", FL_CO_PROC | FL_MODE26 },
367 { "armv2a", FL_CO_PROC | FL_MODE26 },
368 { "armv3", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
369 { "armv3m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
370 { "armv4", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
371 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
372 implementations that support it, so we will leave it out for now. */
373 { "armv4t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
374 { "armv5", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
375 { "armv5t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
376 { "armv5te", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E },
377 { NULL, 0 }
380 /* This is a magic stucture. The 'string' field is magically filled in
381 with a pointer to the value specified by the user on the command line
382 assuming that the user has specified such a value. */
384 struct arm_cpu_select arm_select[] =
386 /* string name processors */
387 { NULL, "-mcpu=", all_cores },
388 { NULL, "-march=", all_architectures },
389 { NULL, "-mtune=", all_cores }
392 /* Return the number of bits set in value' */
393 static unsigned long
394 bit_count (value)
395 signed int value;
397 unsigned long count = 0;
399 while (value)
401 value &= ~(value & -value);
402 ++count;
405 return count;
408 /* Fix up any incompatible options that the user has specified.
409 This has now turned into a maze. */
410 void
411 arm_override_options ()
413 unsigned i;
415 /* Set up the flags based on the cpu/architecture selected by the user. */
416 for (i = ARRAY_SIZE (arm_select); i--;)
418 struct arm_cpu_select * ptr = arm_select + i;
420 if (ptr->string != NULL && ptr->string[0] != '\0')
422 const struct processors * sel;
424 for (sel = ptr->processors; sel->name != NULL; sel++)
425 if (streq (ptr->string, sel->name))
427 if (i == 2)
428 tune_flags = sel->flags;
429 else
431 /* If we have been given an architecture and a processor
432 make sure that they are compatible. We only generate
433 a warning though, and we prefer the CPU over the
434 architecture. */
435 if (insn_flags != 0 && (insn_flags ^ sel->flags))
436 warning ("switch -mcpu=%s conflicts with -march= switch",
437 ptr->string);
439 insn_flags = sel->flags;
442 break;
445 if (sel->name == NULL)
446 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
450 /* If the user did not specify a processor, choose one for them. */
451 if (insn_flags == 0)
453 const struct processors * sel;
454 unsigned int sought;
455 static const struct cpu_default
457 const int cpu;
458 const char *const name;
460 cpu_defaults[] =
462 { TARGET_CPU_arm2, "arm2" },
463 { TARGET_CPU_arm6, "arm6" },
464 { TARGET_CPU_arm610, "arm610" },
465 { TARGET_CPU_arm710, "arm710" },
466 { TARGET_CPU_arm7m, "arm7m" },
467 { TARGET_CPU_arm7500fe, "arm7500fe" },
468 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
469 { TARGET_CPU_arm8, "arm8" },
470 { TARGET_CPU_arm810, "arm810" },
471 { TARGET_CPU_arm9, "arm9" },
472 { TARGET_CPU_strongarm, "strongarm" },
473 { TARGET_CPU_xscale, "xscale" },
474 { TARGET_CPU_generic, "arm" },
475 { 0, 0 }
477 const struct cpu_default * def;
479 /* Find the default. */
480 for (def = cpu_defaults; def->name; def++)
481 if (def->cpu == TARGET_CPU_DEFAULT)
482 break;
484 /* Make sure we found the default CPU. */
485 if (def->name == NULL)
486 abort ();
488 /* Find the default CPU's flags. */
489 for (sel = all_cores; sel->name != NULL; sel++)
490 if (streq (def->name, sel->name))
491 break;
493 if (sel->name == NULL)
494 abort ();
496 insn_flags = sel->flags;
498 /* Now check to see if the user has specified some command line
499 switch that require certain abilities from the cpu. */
500 sought = 0;
502 if (TARGET_INTERWORK || TARGET_THUMB)
504 sought |= (FL_THUMB | FL_MODE32);
506 /* Force apcs-32 to be used for interworking. */
507 target_flags |= ARM_FLAG_APCS_32;
509 /* There are no ARM processors that support both APCS-26 and
510 interworking. Therefore we force FL_MODE26 to be removed
511 from insn_flags here (if it was set), so that the search
512 below will always be able to find a compatible processor. */
513 insn_flags &= ~FL_MODE26;
515 else if (!TARGET_APCS_32)
516 sought |= FL_MODE26;
518 if (sought != 0 && ((sought & insn_flags) != sought))
520 /* Try to locate a CPU type that supports all of the abilities
521 of the default CPU, plus the extra abilities requested by
522 the user. */
523 for (sel = all_cores; sel->name != NULL; sel++)
524 if ((sel->flags & sought) == (sought | insn_flags))
525 break;
527 if (sel->name == NULL)
529 unsigned int current_bit_count = 0;
530 const struct processors * best_fit = NULL;
532 /* Ideally we would like to issue an error message here
533 saying that it was not possible to find a CPU compatible
534 with the default CPU, but which also supports the command
535 line options specified by the programmer, and so they
536 ought to use the -mcpu=<name> command line option to
537 override the default CPU type.
539 Unfortunately this does not work with multilibing. We
540 need to be able to support multilibs for -mapcs-26 and for
541 -mthumb-interwork and there is no CPU that can support both
542 options. Instead if we cannot find a cpu that has both the
543 characteristics of the default cpu and the given command line
544 options we scan the array again looking for a best match. */
545 for (sel = all_cores; sel->name != NULL; sel++)
546 if ((sel->flags & sought) == sought)
548 unsigned int count;
550 count = bit_count (sel->flags & insn_flags);
552 if (count >= current_bit_count)
554 best_fit = sel;
555 current_bit_count = count;
559 if (best_fit == NULL)
560 abort ();
561 else
562 sel = best_fit;
565 insn_flags = sel->flags;
569 /* If tuning has not been specified, tune for whichever processor or
570 architecture has been selected. */
571 if (tune_flags == 0)
572 tune_flags = insn_flags;
574 /* Make sure that the processor choice does not conflict with any of the
575 other command line choices. */
576 if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
578 /* If APCS-32 was not the default then it must have been set by the
579 user, so issue a warning message. If the user has specified
580 "-mapcs-32 -mcpu=arm2" then we loose here. */
581 if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
582 warning ("target CPU does not support APCS-32" );
583 target_flags &= ~ARM_FLAG_APCS_32;
585 else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
587 warning ("target CPU does not support APCS-26" );
588 target_flags |= ARM_FLAG_APCS_32;
591 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
593 warning ("target CPU does not support interworking" );
594 target_flags &= ~ARM_FLAG_INTERWORK;
597 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
599 warning ("target CPU does not support THUMB instructions");
600 target_flags &= ~ARM_FLAG_THUMB;
603 if (TARGET_APCS_FRAME && TARGET_THUMB)
605 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
606 target_flags &= ~ARM_FLAG_APCS_FRAME;
609 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
610 from here where no function is being compiled currently. */
611 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
612 && TARGET_ARM)
613 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
615 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
616 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
618 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
619 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
621 /* If interworking is enabled then APCS-32 must be selected as well. */
622 if (TARGET_INTERWORK)
624 if (!TARGET_APCS_32)
625 warning ("interworking forces APCS-32 to be used" );
626 target_flags |= ARM_FLAG_APCS_32;
629 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
631 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
632 target_flags |= ARM_FLAG_APCS_FRAME;
635 if (TARGET_POKE_FUNCTION_NAME)
636 target_flags |= ARM_FLAG_APCS_FRAME;
638 if (TARGET_APCS_REENT && flag_pic)
639 error ("-fpic and -mapcs-reent are incompatible");
641 if (TARGET_APCS_REENT)
642 warning ("APCS reentrant code not supported. Ignored");
644 /* If this target is normally configured to use APCS frames, warn if they
645 are turned off and debugging is turned on. */
646 if (TARGET_ARM
647 && write_symbols != NO_DEBUG
648 && !TARGET_APCS_FRAME
649 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
650 warning ("-g with -mno-apcs-frame may not give sensible debugging");
652 /* If stack checking is disabled, we can use r10 as the PIC register,
653 which keeps r9 available. */
654 if (flag_pic)
655 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
657 if (TARGET_APCS_FLOAT)
658 warning ("passing floating point arguments in fp regs not yet supported");
660 /* Initialise boolean versions of the flags, for use in the arm.md file. */
661 arm_fast_multiply = (insn_flags & FL_FAST_MULT) != 0;
662 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
663 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
664 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
665 arm_is_xscale = (insn_flags & FL_XSCALE) != 0;
667 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
668 arm_is_strong = (tune_flags & FL_STRONG) != 0;
669 thumb_code = (TARGET_ARM == 0);
670 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
671 && !(tune_flags & FL_ARCH4))) != 0;
673 /* Default value for floating point code... if no co-processor
674 bus, then schedule for emulated floating point. Otherwise,
675 assume the user has an FPA.
676 Note: this does not prevent use of floating point instructions,
677 -msoft-float does that. */
678 arm_fpu = (tune_flags & FL_CO_PROC) ? FP_HARD : FP_SOFT3;
680 if (target_fp_name)
682 if (streq (target_fp_name, "2"))
683 arm_fpu_arch = FP_SOFT2;
684 else if (streq (target_fp_name, "3"))
685 arm_fpu_arch = FP_SOFT3;
686 else
687 error ("invalid floating point emulation option: -mfpe-%s",
688 target_fp_name);
690 else
691 arm_fpu_arch = FP_DEFAULT;
693 if (TARGET_FPE && arm_fpu != FP_HARD)
694 arm_fpu = FP_SOFT2;
696 /* For arm2/3 there is no need to do any scheduling if there is only
697 a floating point emulator, or we are doing software floating-point. */
698 if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD)
699 && (tune_flags & FL_MODE32) == 0)
700 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
702 arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
704 if (structure_size_string != NULL)
706 int size = strtol (structure_size_string, NULL, 0);
708 if (size == 8 || size == 32)
709 arm_structure_size_boundary = size;
710 else
711 warning ("structure size boundary can only be set to 8 or 32");
714 if (arm_pic_register_string != NULL)
716 int pic_register = decode_reg_name (arm_pic_register_string);
718 if (!flag_pic)
719 warning ("-mpic-register= is useless without -fpic");
721 /* Prevent the user from choosing an obviously stupid PIC register. */
722 else if (pic_register < 0 || call_used_regs[pic_register]
723 || pic_register == HARD_FRAME_POINTER_REGNUM
724 || pic_register == STACK_POINTER_REGNUM
725 || pic_register >= PC_REGNUM)
726 error ("unable to use '%s' for PIC register", arm_pic_register_string);
727 else
728 arm_pic_register = pic_register;
731 if (TARGET_THUMB && flag_schedule_insns)
733 /* Don't warn since it's on by default in -O2. */
734 flag_schedule_insns = 0;
737 /* If optimizing for space, don't synthesize constants.
738 For processors with load scheduling, it never costs more than 2 cycles
739 to load a constant, and the load scheduler may well reduce that to 1. */
740 if (optimize_size || (tune_flags & FL_LDSCHED))
741 arm_constant_limit = 1;
743 if (arm_is_xscale)
744 arm_constant_limit = 2;
746 /* If optimizing for size, bump the number of instructions that we
747 are prepared to conditionally execute (even on a StrongARM).
748 Otherwise for the StrongARM, which has early execution of branches,
749 a sequence that is worth skipping is shorter. */
750 if (optimize_size)
751 max_insns_skipped = 6;
752 else if (arm_is_strong)
753 max_insns_skipped = 3;
755 /* Register global variables with the garbage collector. */
756 arm_add_gc_roots ();
759 static void
760 arm_add_gc_roots ()
762 ggc_add_rtx_root (&arm_compare_op0, 1);
763 ggc_add_rtx_root (&arm_compare_op1, 1);
764 ggc_add_rtx_root (&arm_target_insn, 1); /* Not sure this is really a root. */
766 gcc_obstack_init(&minipool_obstack);
767 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
770 /* A table of known ARM exception types.
771 For use with the interrupt function attribute. */
773 typedef struct
775 const char *const arg;
776 const unsigned long return_value;
778 isr_attribute_arg;
780 static const isr_attribute_arg isr_attribute_args [] =
782 { "IRQ", ARM_FT_ISR },
783 { "irq", ARM_FT_ISR },
784 { "FIQ", ARM_FT_FIQ },
785 { "fiq", ARM_FT_FIQ },
786 { "ABORT", ARM_FT_ISR },
787 { "abort", ARM_FT_ISR },
788 { "ABORT", ARM_FT_ISR },
789 { "abort", ARM_FT_ISR },
790 { "UNDEF", ARM_FT_EXCEPTION },
791 { "undef", ARM_FT_EXCEPTION },
792 { "SWI", ARM_FT_EXCEPTION },
793 { "swi", ARM_FT_EXCEPTION },
794 { NULL, ARM_FT_NORMAL }
797 /* Returns the (interrupt) function type of the current
798 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
800 static unsigned long
801 arm_isr_value (argument)
802 tree argument;
804 const isr_attribute_arg * ptr;
805 const char * arg;
807 /* No argument - default to IRQ. */
808 if (argument == NULL_TREE)
809 return ARM_FT_ISR;
811 /* Get the value of the argument. */
812 if (TREE_VALUE (argument) == NULL_TREE
813 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
814 return ARM_FT_UNKNOWN;
816 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
818 /* Check it against the list of known arguments. */
819 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr ++)
820 if (streq (arg, ptr->arg))
821 return ptr->return_value;
823 /* An unrecognised interrupt type. */
824 return ARM_FT_UNKNOWN;
827 /* Computes the type of the current function. */
829 static unsigned long
830 arm_compute_func_type ()
832 unsigned long type = ARM_FT_UNKNOWN;
833 tree a;
834 tree attr;
836 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
837 abort ();
839 /* Decide if the current function is volatile. Such functions
840 never return, and many memory cycles can be saved by not storing
841 register values that will never be needed again. This optimization
842 was added to speed up context switching in a kernel application. */
843 if (optimize > 0
844 && current_function_nothrow
845 && TREE_THIS_VOLATILE (current_function_decl))
846 type |= ARM_FT_VOLATILE;
848 if (current_function_needs_context)
849 type |= ARM_FT_NESTED;
851 attr = DECL_ATTRIBUTES (current_function_decl);
853 a = lookup_attribute ("naked", attr);
854 if (a != NULL_TREE)
855 type |= ARM_FT_NAKED;
857 if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
858 type |= ARM_FT_EXCEPTION_HANDLER;
859 else
861 a = lookup_attribute ("isr", attr);
862 if (a == NULL_TREE)
863 a = lookup_attribute ("interrupt", attr);
865 if (a == NULL_TREE)
866 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
867 else
868 type |= arm_isr_value (TREE_VALUE (a));
871 return type;
874 /* Returns the type of the current function. */
876 unsigned long
877 arm_current_func_type ()
879 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
880 cfun->machine->func_type = arm_compute_func_type ();
882 return cfun->machine->func_type;
885 /* Return 1 if it is possible to return using a single instruction. */
888 use_return_insn (iscond)
889 int iscond;
891 int regno;
892 unsigned int func_type;
894 /* Never use a return instruction before reload has run. */
895 if (!reload_completed)
896 return 0;
898 func_type = arm_current_func_type ();
900 /* Naked functions and volatile functions need special
901 consideration. */
902 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
903 return 0;
905 /* As do variadic functions. */
906 if (current_function_pretend_args_size
907 || cfun->machine->uses_anonymous_args
908 /* Of if the function calls __builtin_eh_return () */
909 || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
910 /* Or if there is no frame pointer and there is a stack adjustment. */
911 || ((get_frame_size () + current_function_outgoing_args_size != 0)
912 && !frame_pointer_needed))
913 return 0;
915 /* Can't be done if interworking with Thumb, and any registers have been
916 stacked. Similarly, on StrongARM, conditional returns are expensive
917 if they aren't taken and registers have been stacked. */
918 if (iscond && arm_is_strong && frame_pointer_needed)
919 return 0;
921 if ((iscond && arm_is_strong)
922 || TARGET_INTERWORK)
924 for (regno = 0; regno <= LAST_ARM_REGNUM; regno++)
925 if (regs_ever_live[regno] && !call_used_regs[regno])
926 return 0;
928 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
929 return 0;
932 /* Can't be done if any of the FPU regs are pushed,
933 since this also requires an insn. */
934 if (TARGET_HARD_FLOAT)
935 for (regno = FIRST_ARM_FP_REGNUM; regno <= LAST_ARM_FP_REGNUM; regno++)
936 if (regs_ever_live[regno] && !call_used_regs[regno])
937 return 0;
939 return 1;
942 /* Return TRUE if int I is a valid immediate ARM constant. */
945 const_ok_for_arm (i)
946 HOST_WIDE_INT i;
948 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
950 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
951 be all zero, or all one. */
952 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
953 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
954 != ((~(unsigned HOST_WIDE_INT) 0)
955 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
956 return FALSE;
958 /* Fast return for 0 and powers of 2 */
959 if ((i & (i - 1)) == 0)
960 return TRUE;
964 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
965 return TRUE;
966 mask =
967 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
968 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
970 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
972 return FALSE;
975 /* Return true if I is a valid constant for the operation CODE. */
976 static int
977 const_ok_for_op (i, code)
978 HOST_WIDE_INT i;
979 enum rtx_code code;
981 if (const_ok_for_arm (i))
982 return 1;
984 switch (code)
986 case PLUS:
987 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
989 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
990 case XOR:
991 case IOR:
992 return 0;
994 case AND:
995 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
997 default:
998 abort ();
1002 /* Emit a sequence of insns to handle a large constant.
1003 CODE is the code of the operation required, it can be any of SET, PLUS,
1004 IOR, AND, XOR, MINUS;
1005 MODE is the mode in which the operation is being performed;
1006 VAL is the integer to operate on;
1007 SOURCE is the other operand (a register, or a null-pointer for SET);
1008 SUBTARGETS means it is safe to create scratch registers if that will
1009 either produce a simpler sequence, or we will want to cse the values.
1010 Return value is the number of insns emitted. */
1013 arm_split_constant (code, mode, val, target, source, subtargets)
1014 enum rtx_code code;
1015 enum machine_mode mode;
1016 HOST_WIDE_INT val;
1017 rtx target;
1018 rtx source;
1019 int subtargets;
1021 if (subtargets || code == SET
1022 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1023 && REGNO (target) != REGNO (source)))
1025 /* After arm_reorg has been called, we can't fix up expensive
1026 constants by pushing them into memory so we must synthesise
1027 them in-line, regardless of the cost. This is only likely to
1028 be more costly on chips that have load delay slots and we are
1029 compiling without running the scheduler (so no splitting
1030 occurred before the final instruction emission).
1032 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1034 if (!after_arm_reorg
1035 && (arm_gen_constant (code, mode, val, target, source, 1, 0)
1036 > arm_constant_limit + (code != SET)))
1038 if (code == SET)
1040 /* Currently SET is the only monadic value for CODE, all
1041 the rest are diadic. */
1042 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1043 return 1;
1045 else
1047 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1049 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1050 /* For MINUS, the value is subtracted from, since we never
1051 have subtraction of a constant. */
1052 if (code == MINUS)
1053 emit_insn (gen_rtx_SET (VOIDmode, target,
1054 gen_rtx_MINUS (mode, temp, source)));
1055 else
1056 emit_insn (gen_rtx_SET (VOIDmode, target,
1057 gen_rtx (code, mode, source, temp)));
1058 return 2;
1063 return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
1066 static int
1067 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1069 HOST_WIDE_INT temp1;
1070 int num_insns = 0;
1073 int end;
1075 if (i <= 0)
1076 i += 32;
1077 if (remainder & (3 << (i - 2)))
1079 end = i - 8;
1080 if (end < 0)
1081 end += 32;
1082 temp1 = remainder & ((0x0ff << end)
1083 | ((i < end) ? (0xff >> (32 - end)) : 0));
1084 remainder &= ~temp1;
1085 num_insns++;
1086 i -= 6;
1088 i -= 2;
1089 } while (remainder);
1090 return num_insns;
1093 /* As above, but extra parameter GENERATE which, if clear, suppresses
1094 RTL generation. */
1096 static int
1097 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
1098 enum rtx_code code;
1099 enum machine_mode mode;
1100 HOST_WIDE_INT val;
1101 rtx target;
1102 rtx source;
1103 int subtargets;
1104 int generate;
1106 int can_invert = 0;
1107 int can_negate = 0;
1108 int can_negate_initial = 0;
1109 int can_shift = 0;
1110 int i;
1111 int num_bits_set = 0;
1112 int set_sign_bit_copies = 0;
1113 int clear_sign_bit_copies = 0;
1114 int clear_zero_bit_copies = 0;
1115 int set_zero_bit_copies = 0;
1116 int insns = 0;
1117 unsigned HOST_WIDE_INT temp1, temp2;
1118 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1120 /* Find out which operations are safe for a given CODE. Also do a quick
1121 check for degenerate cases; these can occur when DImode operations
1122 are split. */
1123 switch (code)
1125 case SET:
1126 can_invert = 1;
1127 can_shift = 1;
1128 can_negate = 1;
1129 break;
1131 case PLUS:
1132 can_negate = 1;
1133 can_negate_initial = 1;
1134 break;
1136 case IOR:
1137 if (remainder == 0xffffffff)
1139 if (generate)
1140 emit_insn (gen_rtx_SET (VOIDmode, target,
1141 GEN_INT (ARM_SIGN_EXTEND (val))));
1142 return 1;
1144 if (remainder == 0)
1146 if (reload_completed && rtx_equal_p (target, source))
1147 return 0;
1148 if (generate)
1149 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1150 return 1;
1152 break;
1154 case AND:
1155 if (remainder == 0)
1157 if (generate)
1158 emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
1159 return 1;
1161 if (remainder == 0xffffffff)
1163 if (reload_completed && rtx_equal_p (target, source))
1164 return 0;
1165 if (generate)
1166 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1167 return 1;
1169 can_invert = 1;
1170 break;
1172 case XOR:
1173 if (remainder == 0)
1175 if (reload_completed && rtx_equal_p (target, source))
1176 return 0;
1177 if (generate)
1178 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1179 return 1;
1181 if (remainder == 0xffffffff)
1183 if (generate)
1184 emit_insn (gen_rtx_SET (VOIDmode, target,
1185 gen_rtx_NOT (mode, source)));
1186 return 1;
1189 /* We don't know how to handle this yet below. */
1190 abort ();
1192 case MINUS:
1193 /* We treat MINUS as (val - source), since (source - val) is always
1194 passed as (source + (-val)). */
1195 if (remainder == 0)
1197 if (generate)
1198 emit_insn (gen_rtx_SET (VOIDmode, target,
1199 gen_rtx_NEG (mode, source)));
1200 return 1;
1202 if (const_ok_for_arm (val))
1204 if (generate)
1205 emit_insn (gen_rtx_SET (VOIDmode, target,
1206 gen_rtx_MINUS (mode, GEN_INT (val),
1207 source)));
1208 return 1;
1210 can_negate = 1;
1212 break;
1214 default:
1215 abort ();
1218 /* If we can do it in one insn get out quickly. */
1219 if (const_ok_for_arm (val)
1220 || (can_negate_initial && const_ok_for_arm (-val))
1221 || (can_invert && const_ok_for_arm (~val)))
1223 if (generate)
1224 emit_insn (gen_rtx_SET (VOIDmode, target,
1225 (source ? gen_rtx (code, mode, source,
1226 GEN_INT (val))
1227 : GEN_INT (val))));
1228 return 1;
1231 /* Calculate a few attributes that may be useful for specific
1232 optimizations. */
1233 for (i = 31; i >= 0; i--)
1235 if ((remainder & (1 << i)) == 0)
1236 clear_sign_bit_copies++;
1237 else
1238 break;
1241 for (i = 31; i >= 0; i--)
1243 if ((remainder & (1 << i)) != 0)
1244 set_sign_bit_copies++;
1245 else
1246 break;
1249 for (i = 0; i <= 31; i++)
1251 if ((remainder & (1 << i)) == 0)
1252 clear_zero_bit_copies++;
1253 else
1254 break;
1257 for (i = 0; i <= 31; i++)
1259 if ((remainder & (1 << i)) != 0)
1260 set_zero_bit_copies++;
1261 else
1262 break;
1265 switch (code)
1267 case SET:
1268 /* See if we can do this by sign_extending a constant that is known
1269 to be negative. This is a good, way of doing it, since the shift
1270 may well merge into a subsequent insn. */
1271 if (set_sign_bit_copies > 1)
1273 if (const_ok_for_arm
1274 (temp1 = ARM_SIGN_EXTEND (remainder
1275 << (set_sign_bit_copies - 1))))
1277 if (generate)
1279 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1280 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1281 GEN_INT (temp1)));
1282 emit_insn (gen_ashrsi3 (target, new_src,
1283 GEN_INT (set_sign_bit_copies - 1)));
1285 return 2;
1287 /* For an inverted constant, we will need to set the low bits,
1288 these will be shifted out of harm's way. */
1289 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1290 if (const_ok_for_arm (~temp1))
1292 if (generate)
1294 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1295 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1296 GEN_INT (temp1)));
1297 emit_insn (gen_ashrsi3 (target, new_src,
1298 GEN_INT (set_sign_bit_copies - 1)));
1300 return 2;
1304 /* See if we can generate this by setting the bottom (or the top)
1305 16 bits, and then shifting these into the other half of the
1306 word. We only look for the simplest cases, to do more would cost
1307 too much. Be careful, however, not to generate this when the
1308 alternative would take fewer insns. */
1309 if (val & 0xffff0000)
1311 temp1 = remainder & 0xffff0000;
1312 temp2 = remainder & 0x0000ffff;
1314 /* Overlaps outside this range are best done using other methods. */
1315 for (i = 9; i < 24; i++)
1317 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1318 && !const_ok_for_arm (temp2))
1320 rtx new_src = (subtargets
1321 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1322 : target);
1323 insns = arm_gen_constant (code, mode, temp2, new_src,
1324 source, subtargets, generate);
1325 source = new_src;
1326 if (generate)
1327 emit_insn (gen_rtx_SET
1328 (VOIDmode, target,
1329 gen_rtx_IOR (mode,
1330 gen_rtx_ASHIFT (mode, source,
1331 GEN_INT (i)),
1332 source)));
1333 return insns + 1;
1337 /* Don't duplicate cases already considered. */
1338 for (i = 17; i < 24; i++)
1340 if (((temp1 | (temp1 >> i)) == remainder)
1341 && !const_ok_for_arm (temp1))
1343 rtx new_src = (subtargets
1344 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1345 : target);
1346 insns = arm_gen_constant (code, mode, temp1, new_src,
1347 source, subtargets, generate);
1348 source = new_src;
1349 if (generate)
1350 emit_insn
1351 (gen_rtx_SET (VOIDmode, target,
1352 gen_rtx_IOR
1353 (mode,
1354 gen_rtx_LSHIFTRT (mode, source,
1355 GEN_INT (i)),
1356 source)));
1357 return insns + 1;
1361 break;
1363 case IOR:
1364 case XOR:
1365 /* If we have IOR or XOR, and the constant can be loaded in a
1366 single instruction, and we can find a temporary to put it in,
1367 then this can be done in two instructions instead of 3-4. */
1368 if (subtargets
1369 /* TARGET can't be NULL if SUBTARGETS is 0 */
1370 || (reload_completed && !reg_mentioned_p (target, source)))
1372 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1374 if (generate)
1376 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1378 emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
1379 emit_insn (gen_rtx_SET (VOIDmode, target,
1380 gen_rtx (code, mode, source, sub)));
1382 return 2;
1386 if (code == XOR)
1387 break;
1389 if (set_sign_bit_copies > 8
1390 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1392 if (generate)
1394 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1395 rtx shift = GEN_INT (set_sign_bit_copies);
1397 emit_insn (gen_rtx_SET (VOIDmode, sub,
1398 gen_rtx_NOT (mode,
1399 gen_rtx_ASHIFT (mode,
1400 source,
1401 shift))));
1402 emit_insn (gen_rtx_SET (VOIDmode, target,
1403 gen_rtx_NOT (mode,
1404 gen_rtx_LSHIFTRT (mode, sub,
1405 shift))));
1407 return 2;
1410 if (set_zero_bit_copies > 8
1411 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1413 if (generate)
1415 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1416 rtx shift = GEN_INT (set_zero_bit_copies);
1418 emit_insn (gen_rtx_SET (VOIDmode, sub,
1419 gen_rtx_NOT (mode,
1420 gen_rtx_LSHIFTRT (mode,
1421 source,
1422 shift))));
1423 emit_insn (gen_rtx_SET (VOIDmode, target,
1424 gen_rtx_NOT (mode,
1425 gen_rtx_ASHIFT (mode, sub,
1426 shift))));
1428 return 2;
1431 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1433 if (generate)
1435 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1436 emit_insn (gen_rtx_SET (VOIDmode, sub,
1437 gen_rtx_NOT (mode, source)));
1438 source = sub;
1439 if (subtargets)
1440 sub = gen_reg_rtx (mode);
1441 emit_insn (gen_rtx_SET (VOIDmode, sub,
1442 gen_rtx_AND (mode, source,
1443 GEN_INT (temp1))));
1444 emit_insn (gen_rtx_SET (VOIDmode, target,
1445 gen_rtx_NOT (mode, sub)));
1447 return 3;
1449 break;
1451 case AND:
1452 /* See if two shifts will do 2 or more insn's worth of work. */
1453 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1455 HOST_WIDE_INT shift_mask = ((0xffffffff
1456 << (32 - clear_sign_bit_copies))
1457 & 0xffffffff);
1459 if ((remainder | shift_mask) != 0xffffffff)
1461 if (generate)
1463 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1464 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1465 new_src, source, subtargets, 1);
1466 source = new_src;
1468 else
1470 rtx targ = subtargets ? NULL_RTX : target;
1471 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1472 targ, source, subtargets, 0);
1476 if (generate)
1478 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1479 rtx shift = GEN_INT (clear_sign_bit_copies);
1481 emit_insn (gen_ashlsi3 (new_src, source, shift));
1482 emit_insn (gen_lshrsi3 (target, new_src, shift));
1485 return insns + 2;
1488 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1490 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1492 if ((remainder | shift_mask) != 0xffffffff)
1494 if (generate)
1496 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1498 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1499 new_src, source, subtargets, 1);
1500 source = new_src;
1502 else
1504 rtx targ = subtargets ? NULL_RTX : target;
1506 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1507 targ, source, subtargets, 0);
1511 if (generate)
1513 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1514 rtx shift = GEN_INT (clear_zero_bit_copies);
1516 emit_insn (gen_lshrsi3 (new_src, source, shift));
1517 emit_insn (gen_ashlsi3 (target, new_src, shift));
1520 return insns + 2;
1523 break;
1525 default:
1526 break;
1529 for (i = 0; i < 32; i++)
1530 if (remainder & (1 << i))
1531 num_bits_set++;
1533 if (code == AND || (can_invert && num_bits_set > 16))
1534 remainder = (~remainder) & 0xffffffff;
1535 else if (code == PLUS && num_bits_set > 16)
1536 remainder = (-remainder) & 0xffffffff;
1537 else
1539 can_invert = 0;
1540 can_negate = 0;
1543 /* Now try and find a way of doing the job in either two or three
1544 instructions.
1545 We start by looking for the largest block of zeros that are aligned on
1546 a 2-bit boundary, we then fill up the temps, wrapping around to the
1547 top of the word when we drop off the bottom.
1548 In the worst case this code should produce no more than four insns. */
1550 int best_start = 0;
1551 int best_consecutive_zeros = 0;
1553 for (i = 0; i < 32; i += 2)
1555 int consecutive_zeros = 0;
1557 if (!(remainder & (3 << i)))
1559 while ((i < 32) && !(remainder & (3 << i)))
1561 consecutive_zeros += 2;
1562 i += 2;
1564 if (consecutive_zeros > best_consecutive_zeros)
1566 best_consecutive_zeros = consecutive_zeros;
1567 best_start = i - consecutive_zeros;
1569 i -= 2;
1573 /* So long as it won't require any more insns to do so, it's
1574 desirable to emit a small constant (in bits 0...9) in the last
1575 insn. This way there is more chance that it can be combined with
1576 a later addressing insn to form a pre-indexed load or store
1577 operation. Consider:
1579 *((volatile int *)0xe0000100) = 1;
1580 *((volatile int *)0xe0000110) = 2;
1582 We want this to wind up as:
1584 mov rA, #0xe0000000
1585 mov rB, #1
1586 str rB, [rA, #0x100]
1587 mov rB, #2
1588 str rB, [rA, #0x110]
1590 rather than having to synthesize both large constants from scratch.
1592 Therefore, we calculate how many insns would be required to emit
1593 the constant starting from `best_start', and also starting from
1594 zero (ie with bit 31 first to be output). If `best_start' doesn't
1595 yield a shorter sequence, we may as well use zero. */
1596 if (best_start != 0
1597 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1598 && (count_insns_for_constant (remainder, 0) <=
1599 count_insns_for_constant (remainder, best_start)))
1600 best_start = 0;
1602 /* Now start emitting the insns. */
1603 i = best_start;
1606 int end;
1608 if (i <= 0)
1609 i += 32;
1610 if (remainder & (3 << (i - 2)))
1612 end = i - 8;
1613 if (end < 0)
1614 end += 32;
1615 temp1 = remainder & ((0x0ff << end)
1616 | ((i < end) ? (0xff >> (32 - end)) : 0));
1617 remainder &= ~temp1;
1619 if (generate)
1621 rtx new_src, temp1_rtx;
1623 if (code == SET || code == MINUS)
1625 new_src = (subtargets ? gen_reg_rtx (mode) : target);
1626 if (can_invert && code != MINUS)
1627 temp1 = ~temp1;
1629 else
1631 if (remainder && subtargets)
1632 new_src = gen_reg_rtx (mode);
1633 else
1634 new_src = target;
1635 if (can_invert)
1636 temp1 = ~temp1;
1637 else if (can_negate)
1638 temp1 = -temp1;
1641 temp1 = trunc_int_for_mode (temp1, mode);
1642 temp1_rtx = GEN_INT (temp1);
1644 if (code == SET)
1646 else if (code == MINUS)
1647 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
1648 else
1649 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
1651 emit_insn (gen_rtx_SET (VOIDmode, new_src, temp1_rtx));
1652 source = new_src;
1655 if (code == SET)
1657 can_invert = 0;
1658 code = PLUS;
1660 else if (code == MINUS)
1661 code = PLUS;
1663 insns++;
1664 i -= 6;
1666 i -= 2;
1668 while (remainder);
1671 return insns;
1674 /* Canonicalize a comparison so that we are more likely to recognize it.
1675 This can be done for a few constant compares, where we can make the
1676 immediate value easier to load. */
1678 enum rtx_code
1679 arm_canonicalize_comparison (code, op1)
1680 enum rtx_code code;
1681 rtx * op1;
1683 unsigned HOST_WIDE_INT i = INTVAL (*op1);
1685 switch (code)
1687 case EQ:
1688 case NE:
1689 return code;
1691 case GT:
1692 case LE:
1693 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1694 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1696 *op1 = GEN_INT (i + 1);
1697 return code == GT ? GE : LT;
1699 break;
1701 case GE:
1702 case LT:
1703 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1704 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1706 *op1 = GEN_INT (i - 1);
1707 return code == GE ? GT : LE;
1709 break;
1711 case GTU:
1712 case LEU:
1713 if (i != ~((unsigned HOST_WIDE_INT) 0)
1714 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1716 *op1 = GEN_INT (i + 1);
1717 return code == GTU ? GEU : LTU;
1719 break;
1721 case GEU:
1722 case LTU:
1723 if (i != 0
1724 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1726 *op1 = GEN_INT (i - 1);
1727 return code == GEU ? GTU : LEU;
1729 break;
1731 default:
1732 abort ();
1735 return code;
1738 /* Decide whether a type should be returned in memory (true)
1739 or in a register (false). This is called by the macro
1740 RETURN_IN_MEMORY. */
1743 arm_return_in_memory (type)
1744 tree type;
1746 if (!AGGREGATE_TYPE_P (type))
1747 /* All simple types are returned in registers. */
1748 return 0;
1750 /* For the arm-wince targets we choose to be compitable with Microsoft's
1751 ARM and Thumb compilers, which always return aggregates in memory. */
1752 #ifndef ARM_WINCE
1753 /* All structures/unions bigger than one word are returned in memory.
1754 Also catch the case where int_size_in_bytes returns -1. In this case
1755 the aggregate is either huge or of varaible size, and in either case
1756 we will want to return it via memory and not in a register. */
1757 if (((unsigned int) int_size_in_bytes (type)) > UNITS_PER_WORD)
1758 return 1;
1760 if (TREE_CODE (type) == RECORD_TYPE)
1762 tree field;
1764 /* For a struct the APCS says that we only return in a register
1765 if the type is 'integer like' and every addressable element
1766 has an offset of zero. For practical purposes this means
1767 that the structure can have at most one non bit-field element
1768 and that this element must be the first one in the structure. */
1770 /* Find the first field, ignoring non FIELD_DECL things which will
1771 have been created by C++. */
1772 for (field = TYPE_FIELDS (type);
1773 field && TREE_CODE (field) != FIELD_DECL;
1774 field = TREE_CHAIN (field))
1775 continue;
1777 if (field == NULL)
1778 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
1780 /* Check that the first field is valid for returning in a register. */
1782 /* ... Floats are not allowed */
1783 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1784 return 1;
1786 /* ... Aggregates that are not themselves valid for returning in
1787 a register are not allowed. */
1788 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1789 return 1;
1791 /* Now check the remaining fields, if any. Only bitfields are allowed,
1792 since they are not addressable. */
1793 for (field = TREE_CHAIN (field);
1794 field;
1795 field = TREE_CHAIN (field))
1797 if (TREE_CODE (field) != FIELD_DECL)
1798 continue;
1800 if (!DECL_BIT_FIELD_TYPE (field))
1801 return 1;
1804 return 0;
1807 if (TREE_CODE (type) == UNION_TYPE)
1809 tree field;
1811 /* Unions can be returned in registers if every element is
1812 integral, or can be returned in an integer register. */
1813 for (field = TYPE_FIELDS (type);
1814 field;
1815 field = TREE_CHAIN (field))
1817 if (TREE_CODE (field) != FIELD_DECL)
1818 continue;
1820 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1821 return 1;
1823 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1824 return 1;
1827 return 0;
1829 #endif /* not ARM_WINCE */
1831 /* Return all other types in memory. */
1832 return 1;
1835 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1836 for a call to a function whose data type is FNTYPE.
1837 For a library call, FNTYPE is NULL. */
1838 void
1839 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1840 CUMULATIVE_ARGS * pcum;
1841 tree fntype;
1842 rtx libname ATTRIBUTE_UNUSED;
1843 int indirect ATTRIBUTE_UNUSED;
1845 /* On the ARM, the offset starts at 0. */
1846 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype))) ? 1 : 0);
1848 pcum->call_cookie = CALL_NORMAL;
1850 if (TARGET_LONG_CALLS)
1851 pcum->call_cookie = CALL_LONG;
1853 /* Check for long call/short call attributes. The attributes
1854 override any command line option. */
1855 if (fntype)
1857 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1858 pcum->call_cookie = CALL_SHORT;
1859 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1860 pcum->call_cookie = CALL_LONG;
1864 /* Determine where to put an argument to a function.
1865 Value is zero to push the argument on the stack,
1866 or a hard register in which to store the argument.
1868 MODE is the argument's machine mode.
1869 TYPE is the data type of the argument (as a tree).
1870 This is null for libcalls where that information may
1871 not be available.
1872 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1873 the preceding args and about the function being called.
1874 NAMED is nonzero if this argument is a named parameter
1875 (otherwise it is an extra parameter matching an ellipsis). */
1878 arm_function_arg (pcum, mode, type, named)
1879 CUMULATIVE_ARGS * pcum;
1880 enum machine_mode mode;
1881 tree type ATTRIBUTE_UNUSED;
1882 int named;
1884 if (mode == VOIDmode)
1885 /* Compute operand 2 of the call insn. */
1886 return GEN_INT (pcum->call_cookie);
1888 if (!named || pcum->nregs >= NUM_ARG_REGS)
1889 return NULL_RTX;
1891 return gen_rtx_REG (mode, pcum->nregs);
1894 /* Encode the current state of the #pragma [no_]long_calls. */
1895 typedef enum
1897 OFF, /* No #pramgma [no_]long_calls is in effect. */
1898 LONG, /* #pragma long_calls is in effect. */
1899 SHORT /* #pragma no_long_calls is in effect. */
1900 } arm_pragma_enum;
1902 static arm_pragma_enum arm_pragma_long_calls = OFF;
1904 void
1905 arm_pr_long_calls (pfile)
1906 cpp_reader * pfile ATTRIBUTE_UNUSED;
1908 arm_pragma_long_calls = LONG;
1911 void
1912 arm_pr_no_long_calls (pfile)
1913 cpp_reader * pfile ATTRIBUTE_UNUSED;
1915 arm_pragma_long_calls = SHORT;
1918 void
1919 arm_pr_long_calls_off (pfile)
1920 cpp_reader * pfile ATTRIBUTE_UNUSED;
1922 arm_pragma_long_calls = OFF;
1925 /* Table of machine attributes. */
1926 const struct attribute_spec arm_attribute_table[] =
1928 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1929 /* Function calls made to this symbol must be done indirectly, because
1930 it may lie outside of the 26 bit addressing range of a normal function
1931 call. */
1932 { "long_call", 0, 0, false, true, true, NULL },
1933 /* Whereas these functions are always known to reside within the 26 bit
1934 addressing range. */
1935 { "short_call", 0, 0, false, true, true, NULL },
1936 /* Interrupt Service Routines have special prologue and epilogue requirements. */
1937 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
1938 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
1939 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
1940 #ifdef ARM_PE
1941 /* ARM/PE has three new attributes:
1942 interfacearm - ?
1943 dllexport - for exporting a function/variable that will live in a dll
1944 dllimport - for importing a function/variable from a dll
1946 Microsoft allows multiple declspecs in one __declspec, separating
1947 them with spaces. We do NOT support this. Instead, use __declspec
1948 multiple times.
1950 { "dllimport", 0, 0, true, false, false, NULL },
1951 { "dllexport", 0, 0, true, false, false, NULL },
1952 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
1953 #endif
1954 { NULL, 0, 0, false, false, false, NULL }
1957 /* Handle an attribute requiring a FUNCTION_DECL;
1958 arguments as in struct attribute_spec.handler. */
1960 static tree
1961 arm_handle_fndecl_attribute (node, name, args, flags, no_add_attrs)
1962 tree * node;
1963 tree name;
1964 tree args ATTRIBUTE_UNUSED;
1965 int flags ATTRIBUTE_UNUSED;
1966 bool * no_add_attrs;
1968 if (TREE_CODE (*node) != FUNCTION_DECL)
1970 warning ("`%s' attribute only applies to functions",
1971 IDENTIFIER_POINTER (name));
1972 *no_add_attrs = true;
1975 return NULL_TREE;
1978 /* Handle an "interrupt" or "isr" attribute;
1979 arguments as in struct attribute_spec.handler. */
1981 static tree
1982 arm_handle_isr_attribute (node, name, args, flags, no_add_attrs)
1983 tree * node;
1984 tree name;
1985 tree args;
1986 int flags;
1987 bool * no_add_attrs;
1989 if (DECL_P (*node))
1991 if (TREE_CODE (*node) != FUNCTION_DECL)
1993 warning ("`%s' attribute only applies to functions",
1994 IDENTIFIER_POINTER (name));
1995 *no_add_attrs = true;
1997 /* FIXME: the argument if any is checked for type attributes;
1998 should it be checked for decl ones? */
2000 else
2002 if (TREE_CODE (*node) == FUNCTION_TYPE
2003 || TREE_CODE (*node) == METHOD_TYPE)
2005 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2007 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2008 *no_add_attrs = true;
2011 else if (TREE_CODE (*node) == POINTER_TYPE
2012 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2013 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2014 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2016 *node = build_type_copy (*node);
2017 TREE_TYPE (*node) = build_type_attribute_variant
2018 (TREE_TYPE (*node),
2019 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2020 *no_add_attrs = true;
2022 else
2024 /* Possibly pass this attribute on from the type to a decl. */
2025 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2026 | (int) ATTR_FLAG_FUNCTION_NEXT
2027 | (int) ATTR_FLAG_ARRAY_NEXT))
2029 *no_add_attrs = true;
2030 return tree_cons (name, args, NULL_TREE);
2032 else
2034 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2039 return NULL_TREE;
2042 /* Return 0 if the attributes for two types are incompatible, 1 if they
2043 are compatible, and 2 if they are nearly compatible (which causes a
2044 warning to be generated). */
2046 static int
2047 arm_comp_type_attributes (type1, type2)
2048 tree type1;
2049 tree type2;
2051 int l1, l2, s1, s2;
2053 /* Check for mismatch of non-default calling convention. */
2054 if (TREE_CODE (type1) != FUNCTION_TYPE)
2055 return 1;
2057 /* Check for mismatched call attributes. */
2058 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2059 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2060 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2061 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2063 /* Only bother to check if an attribute is defined. */
2064 if (l1 | l2 | s1 | s2)
2066 /* If one type has an attribute, the other must have the same attribute. */
2067 if ((l1 != l2) || (s1 != s2))
2068 return 0;
2070 /* Disallow mixed attributes. */
2071 if ((l1 & s2) || (l2 & s1))
2072 return 0;
2075 /* Check for mismatched ISR attribute. */
2076 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2077 if (! l1)
2078 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2079 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2080 if (! l2)
2081 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2082 if (l1 != l2)
2083 return 0;
2085 return 1;
2088 /* Encode long_call or short_call attribute by prefixing
2089 symbol name in DECL with a special character FLAG. */
2091 void
2092 arm_encode_call_attribute (decl, flag)
2093 tree decl;
2094 int flag;
2096 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2097 int len = strlen (str);
2098 char * newstr;
2100 /* Do not allow weak functions to be treated as short call. */
2101 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2102 return;
2104 newstr = alloca (len + 2);
2105 newstr[0] = flag;
2106 strcpy (newstr + 1, str);
2108 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2109 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2112 /* Assigns default attributes to newly defined type. This is used to
2113 set short_call/long_call attributes for function types of
2114 functions defined inside corresponding #pragma scopes. */
2116 static void
2117 arm_set_default_type_attributes (type)
2118 tree type;
2120 /* Add __attribute__ ((long_call)) to all functions, when
2121 inside #pragma long_calls or __attribute__ ((short_call)),
2122 when inside #pragma no_long_calls. */
2123 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2125 tree type_attr_list, attr_name;
2126 type_attr_list = TYPE_ATTRIBUTES (type);
2128 if (arm_pragma_long_calls == LONG)
2129 attr_name = get_identifier ("long_call");
2130 else if (arm_pragma_long_calls == SHORT)
2131 attr_name = get_identifier ("short_call");
2132 else
2133 return;
2135 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2136 TYPE_ATTRIBUTES (type) = type_attr_list;
2140 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2141 defined within the current compilation unit. If this caanot be
2142 determined, then 0 is returned. */
2144 static int
2145 current_file_function_operand (sym_ref)
2146 rtx sym_ref;
2148 /* This is a bit of a fib. A function will have a short call flag
2149 applied to its name if it has the short call attribute, or it has
2150 already been defined within the current compilation unit. */
2151 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2152 return 1;
2154 /* The current function is always defined within the current compilation
2155 unit. if it s a weak definition however, then this may not be the real
2156 definition of the function, and so we have to say no. */
2157 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2158 && !DECL_WEAK (current_function_decl))
2159 return 1;
2161 /* We cannot make the determination - default to returning 0. */
2162 return 0;
2165 /* Return non-zero if a 32 bit "long_call" should be generated for
2166 this call. We generate a long_call if the function:
2168 a. has an __attribute__((long call))
2169 or b. is within the scope of a #pragma long_calls
2170 or c. the -mlong-calls command line switch has been specified
2172 However we do not generate a long call if the function:
2174 d. has an __attribute__ ((short_call))
2175 or e. is inside the scope of a #pragma no_long_calls
2176 or f. has an __attribute__ ((section))
2177 or g. is defined within the current compilation unit.
2179 This function will be called by C fragments contained in the machine
2180 description file. CALL_REF and CALL_COOKIE correspond to the matched
2181 rtl operands. CALL_SYMBOL is used to distinguish between
2182 two different callers of the function. It is set to 1 in the
2183 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2184 and "call_value" patterns. This is because of the difference in the
2185 SYM_REFs passed by these patterns. */
2188 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
2189 rtx sym_ref;
2190 int call_cookie;
2191 int call_symbol;
2193 if (!call_symbol)
2195 if (GET_CODE (sym_ref) != MEM)
2196 return 0;
2198 sym_ref = XEXP (sym_ref, 0);
2201 if (GET_CODE (sym_ref) != SYMBOL_REF)
2202 return 0;
2204 if (call_cookie & CALL_SHORT)
2205 return 0;
2207 if (TARGET_LONG_CALLS && flag_function_sections)
2208 return 1;
2210 if (current_file_function_operand (sym_ref))
2211 return 0;
2213 return (call_cookie & CALL_LONG)
2214 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2215 || TARGET_LONG_CALLS;
2218 /* Return non-zero if it is ok to make a tail-call to DECL. */
2221 arm_function_ok_for_sibcall (decl)
2222 tree decl;
2224 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2226 /* Never tailcall something for which we have no decl, or if we
2227 are in Thumb mode. */
2228 if (decl == NULL || TARGET_THUMB)
2229 return 0;
2231 /* Get the calling method. */
2232 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2233 call_type = CALL_SHORT;
2234 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2235 call_type = CALL_LONG;
2237 /* Cannot tail-call to long calls, since these are out of range of
2238 a branch instruction. However, if not compiling PIC, we know
2239 we can reach the symbol if it is in this compilation unit. */
2240 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2241 return 0;
2243 /* If we are interworking and the function is not declared static
2244 then we can't tail-call it unless we know that it exists in this
2245 compilation unit (since it might be a Thumb routine). */
2246 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2247 return 0;
2249 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2250 if (IS_INTERRUPT (arm_current_func_type ()))
2251 return 0;
2253 /* Everything else is ok. */
2254 return 1;
2259 legitimate_pic_operand_p (x)
2260 rtx x;
2262 if (CONSTANT_P (x)
2263 && flag_pic
2264 && (GET_CODE (x) == SYMBOL_REF
2265 || (GET_CODE (x) == CONST
2266 && GET_CODE (XEXP (x, 0)) == PLUS
2267 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2268 return 0;
2270 return 1;
2274 legitimize_pic_address (orig, mode, reg)
2275 rtx orig;
2276 enum machine_mode mode;
2277 rtx reg;
2279 if (GET_CODE (orig) == SYMBOL_REF
2280 || GET_CODE (orig) == LABEL_REF)
2282 #ifndef AOF_ASSEMBLER
2283 rtx pic_ref, address;
2284 #endif
2285 rtx insn;
2286 int subregs = 0;
2288 if (reg == 0)
2290 if (no_new_pseudos)
2291 abort ();
2292 else
2293 reg = gen_reg_rtx (Pmode);
2295 subregs = 1;
2298 #ifdef AOF_ASSEMBLER
2299 /* The AOF assembler can generate relocations for these directly, and
2300 understands that the PIC register has to be added into the offset. */
2301 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2302 #else
2303 if (subregs)
2304 address = gen_reg_rtx (Pmode);
2305 else
2306 address = reg;
2308 if (TARGET_ARM)
2309 emit_insn (gen_pic_load_addr_arm (address, orig));
2310 else
2311 emit_insn (gen_pic_load_addr_thumb (address, orig));
2313 if ((GET_CODE (orig) == LABEL_REF
2314 || (GET_CODE (orig) == SYMBOL_REF &&
2315 ENCODED_SHORT_CALL_ATTR_P (XSTR (orig, 0))))
2316 && NEED_GOT_RELOC)
2317 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2318 else
2320 pic_ref = gen_rtx_MEM (Pmode,
2321 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2322 address));
2323 RTX_UNCHANGING_P (pic_ref) = 1;
2326 insn = emit_move_insn (reg, pic_ref);
2327 #endif
2328 current_function_uses_pic_offset_table = 1;
2329 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2330 by loop. */
2331 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2332 REG_NOTES (insn));
2333 return reg;
2335 else if (GET_CODE (orig) == CONST)
2337 rtx base, offset;
2339 if (GET_CODE (XEXP (orig, 0)) == PLUS
2340 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2341 return orig;
2343 if (reg == 0)
2345 if (no_new_pseudos)
2346 abort ();
2347 else
2348 reg = gen_reg_rtx (Pmode);
2351 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2353 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2354 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2355 base == reg ? 0 : reg);
2357 else
2358 abort ();
2360 if (GET_CODE (offset) == CONST_INT)
2362 /* The base register doesn't really matter, we only want to
2363 test the index for the appropriate mode. */
2364 ARM_GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
2366 if (!no_new_pseudos)
2367 offset = force_reg (Pmode, offset);
2368 else
2369 abort ();
2371 win:
2372 if (GET_CODE (offset) == CONST_INT)
2373 return plus_constant (base, INTVAL (offset));
2376 if (GET_MODE_SIZE (mode) > 4
2377 && (GET_MODE_CLASS (mode) == MODE_INT
2378 || TARGET_SOFT_FLOAT))
2380 emit_insn (gen_addsi3 (reg, base, offset));
2381 return reg;
2384 return gen_rtx_PLUS (Pmode, base, offset);
2387 return orig;
2390 /* Generate code to load the PIC register. PROLOGUE is true if
2391 called from arm_expand_prologue (in which case we want the
2392 generated insns at the start of the function); false if called
2393 by an exception receiver that needs the PIC register reloaded
2394 (in which case the insns are just dumped at the current location). */
2396 void
2397 arm_finalize_pic (prologue)
2398 int prologue ATTRIBUTE_UNUSED;
2400 #ifndef AOF_ASSEMBLER
2401 rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2402 rtx global_offset_table;
2404 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2405 return;
2407 if (!flag_pic)
2408 abort ();
2410 start_sequence ();
2411 l1 = gen_label_rtx ();
2413 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2414 /* On the ARM the PC register contains 'dot + 8' at the time of the
2415 addition, on the Thumb it is 'dot + 4'. */
2416 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2417 if (GOT_PCREL)
2418 pic_tmp2 = gen_rtx_CONST (VOIDmode,
2419 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2420 else
2421 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2423 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2425 if (TARGET_ARM)
2427 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2428 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2430 else
2432 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2433 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2436 seq = gen_sequence ();
2437 end_sequence ();
2438 if (prologue)
2439 emit_insn_after (seq, get_insns ());
2440 else
2441 emit_insn (seq);
2443 /* Need to emit this whether or not we obey regdecls,
2444 since setjmp/longjmp can cause life info to screw up. */
2445 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2446 #endif /* AOF_ASSEMBLER */
2449 #define REG_OR_SUBREG_REG(X) \
2450 (GET_CODE (X) == REG \
2451 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
2453 #define REG_OR_SUBREG_RTX(X) \
2454 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
2456 #ifndef COSTS_N_INSNS
2457 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
2458 #endif
2461 arm_rtx_costs (x, code, outer)
2462 rtx x;
2463 enum rtx_code code;
2464 enum rtx_code outer;
2466 enum machine_mode mode = GET_MODE (x);
2467 enum rtx_code subcode;
2468 int extra_cost;
2470 if (TARGET_THUMB)
2472 switch (code)
2474 case ASHIFT:
2475 case ASHIFTRT:
2476 case LSHIFTRT:
2477 case ROTATERT:
2478 case PLUS:
2479 case MINUS:
2480 case COMPARE:
2481 case NEG:
2482 case NOT:
2483 return COSTS_N_INSNS (1);
2485 case MULT:
2486 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2488 int cycles = 0;
2489 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
2491 while (i)
2493 i >>= 2;
2494 cycles++;
2496 return COSTS_N_INSNS (2) + cycles;
2498 return COSTS_N_INSNS (1) + 16;
2500 case SET:
2501 return (COSTS_N_INSNS (1)
2502 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
2503 + GET_CODE (SET_DEST (x)) == MEM));
2505 case CONST_INT:
2506 if (outer == SET)
2508 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2509 return 0;
2510 if (thumb_shiftable_const (INTVAL (x)))
2511 return COSTS_N_INSNS (2);
2512 return COSTS_N_INSNS (3);
2514 else if (outer == PLUS
2515 && INTVAL (x) < 256 && INTVAL (x) > -256)
2516 return 0;
2517 else if (outer == COMPARE
2518 && (unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2519 return 0;
2520 else if (outer == ASHIFT || outer == ASHIFTRT
2521 || outer == LSHIFTRT)
2522 return 0;
2523 return COSTS_N_INSNS (2);
2525 case CONST:
2526 case CONST_DOUBLE:
2527 case LABEL_REF:
2528 case SYMBOL_REF:
2529 return COSTS_N_INSNS (3);
2531 case UDIV:
2532 case UMOD:
2533 case DIV:
2534 case MOD:
2535 return 100;
2537 case TRUNCATE:
2538 return 99;
2540 case AND:
2541 case XOR:
2542 case IOR:
2543 /* XXX guess. */
2544 return 8;
2546 case ADDRESSOF:
2547 case MEM:
2548 /* XXX another guess. */
2549 /* Memory costs quite a lot for the first word, but subsequent words
2550 load at the equivalent of a single insn each. */
2551 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2552 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2554 case IF_THEN_ELSE:
2555 /* XXX a guess. */
2556 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2557 return 14;
2558 return 2;
2560 case ZERO_EXTEND:
2561 /* XXX still guessing. */
2562 switch (GET_MODE (XEXP (x, 0)))
2564 case QImode:
2565 return (1 + (mode == DImode ? 4 : 0)
2566 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2568 case HImode:
2569 return (4 + (mode == DImode ? 4 : 0)
2570 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2572 case SImode:
2573 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2575 default:
2576 return 99;
2579 default:
2580 return 99;
2581 #if 0
2582 case FFS:
2583 case FLOAT:
2584 case FIX:
2585 case UNSIGNED_FIX:
2586 /* XXX guess */
2587 fprintf (stderr, "unexpected code for thumb in rtx_costs: %s\n",
2588 rtx_name[code]);
2589 abort ();
2590 #endif
2594 switch (code)
2596 case MEM:
2597 /* Memory costs quite a lot for the first word, but subsequent words
2598 load at the equivalent of a single insn each. */
2599 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2600 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2602 case DIV:
2603 case MOD:
2604 return 100;
2606 case ROTATE:
2607 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
2608 return 4;
2609 /* Fall through */
2610 case ROTATERT:
2611 if (mode != SImode)
2612 return 8;
2613 /* Fall through */
2614 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
2615 if (mode == DImode)
2616 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
2617 + ((GET_CODE (XEXP (x, 0)) == REG
2618 || (GET_CODE (XEXP (x, 0)) == SUBREG
2619 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2620 ? 0 : 8));
2621 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
2622 || (GET_CODE (XEXP (x, 0)) == SUBREG
2623 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2624 ? 0 : 4)
2625 + ((GET_CODE (XEXP (x, 1)) == REG
2626 || (GET_CODE (XEXP (x, 1)) == SUBREG
2627 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
2628 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
2629 ? 0 : 4));
2631 case MINUS:
2632 if (mode == DImode)
2633 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
2634 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2635 || (GET_CODE (XEXP (x, 0)) == CONST_INT
2636 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
2637 ? 0 : 8));
2639 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2640 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2641 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2642 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2643 ? 0 : 8)
2644 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2645 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
2646 && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
2647 ? 0 : 8));
2649 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
2650 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
2651 && REG_OR_SUBREG_REG (XEXP (x, 1))))
2652 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
2653 || subcode == ASHIFTRT || subcode == LSHIFTRT
2654 || subcode == ROTATE || subcode == ROTATERT
2655 || (subcode == MULT
2656 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
2657 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
2658 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
2659 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
2660 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
2661 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
2662 && REG_OR_SUBREG_REG (XEXP (x, 0))))
2663 return 1;
2664 /* Fall through */
2666 case PLUS:
2667 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2668 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2669 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2670 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2671 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2672 ? 0 : 8));
2674 /* Fall through */
2675 case AND: case XOR: case IOR:
2676 extra_cost = 0;
2678 /* Normally the frame registers will be spilt into reg+const during
2679 reload, so it is a bad idea to combine them with other instructions,
2680 since then they might not be moved outside of loops. As a compromise
2681 we allow integration with ops that have a constant as their second
2682 operand. */
2683 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
2684 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
2685 && GET_CODE (XEXP (x, 1)) != CONST_INT)
2686 || (REG_OR_SUBREG_REG (XEXP (x, 0))
2687 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
2688 extra_cost = 4;
2690 if (mode == DImode)
2691 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2692 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2693 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2694 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2695 ? 0 : 8));
2697 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
2698 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
2699 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2700 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2701 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2702 ? 0 : 4));
2704 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
2705 return (1 + extra_cost
2706 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
2707 || subcode == LSHIFTRT || subcode == ASHIFTRT
2708 || subcode == ROTATE || subcode == ROTATERT
2709 || (subcode == MULT
2710 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2711 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
2712 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
2713 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
2714 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
2715 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
2716 ? 0 : 4));
2718 return 8;
2720 case MULT:
2721 /* There is no point basing this on the tuning, since it is always the
2722 fast variant if it exists at all. */
2723 if (arm_fast_multiply && mode == DImode
2724 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
2725 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2726 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
2727 return 8;
2729 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2730 || mode == DImode)
2731 return 30;
2733 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2735 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
2736 & (unsigned HOST_WIDE_INT) 0xffffffff);
2737 int add_cost = const_ok_for_arm (i) ? 4 : 8;
2738 int j;
2740 /* Tune as appropriate. */
2741 int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
2743 for (j = 0; i && j < 32; j += booth_unit_size)
2745 i >>= booth_unit_size;
2746 add_cost += 2;
2749 return add_cost;
2752 return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
2753 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
2754 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
2756 case TRUNCATE:
2757 if (arm_fast_multiply && mode == SImode
2758 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
2759 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2760 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
2761 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
2762 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
2763 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
2764 return 8;
2765 return 99;
2767 case NEG:
2768 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2769 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
2770 /* Fall through */
2771 case NOT:
2772 if (mode == DImode)
2773 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2775 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2777 case IF_THEN_ELSE:
2778 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2779 return 14;
2780 return 2;
2782 case COMPARE:
2783 return 1;
2785 case ABS:
2786 return 4 + (mode == DImode ? 4 : 0);
2788 case SIGN_EXTEND:
2789 if (GET_MODE (XEXP (x, 0)) == QImode)
2790 return (4 + (mode == DImode ? 4 : 0)
2791 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2792 /* Fall through */
2793 case ZERO_EXTEND:
2794 switch (GET_MODE (XEXP (x, 0)))
2796 case QImode:
2797 return (1 + (mode == DImode ? 4 : 0)
2798 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2800 case HImode:
2801 return (4 + (mode == DImode ? 4 : 0)
2802 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2804 case SImode:
2805 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2807 default:
2808 break;
2810 abort ();
2812 case CONST_INT:
2813 if (const_ok_for_arm (INTVAL (x)))
2814 return outer == SET ? 2 : -1;
2815 else if (outer == AND
2816 && const_ok_for_arm (~INTVAL (x)))
2817 return -1;
2818 else if ((outer == COMPARE
2819 || outer == PLUS || outer == MINUS)
2820 && const_ok_for_arm (-INTVAL (x)))
2821 return -1;
2822 else
2823 return 5;
2825 case CONST:
2826 case LABEL_REF:
2827 case SYMBOL_REF:
2828 return 6;
2830 case CONST_DOUBLE:
2831 if (const_double_rtx_ok_for_fpu (x))
2832 return outer == SET ? 2 : -1;
2833 else if ((outer == COMPARE || outer == PLUS)
2834 && neg_const_double_rtx_ok_for_fpu (x))
2835 return -1;
2836 return 7;
2838 default:
2839 return 99;
2843 static int
2844 arm_adjust_cost (insn, link, dep, cost)
2845 rtx insn;
2846 rtx link;
2847 rtx dep;
2848 int cost;
2850 rtx i_pat, d_pat;
2852 /* Some true dependencies can have a higher cost depending
2853 on precisely how certain input operands are used. */
2854 if (arm_is_xscale
2855 && REG_NOTE_KIND (link) == 0
2856 && recog_memoized (insn) < 0
2857 && recog_memoized (dep) < 0)
2859 int shift_opnum = get_attr_shift (insn);
2860 enum attr_type attr_type = get_attr_type (dep);
2862 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
2863 operand for INSN. If we have a shifted input operand and the
2864 instruction we depend on is another ALU instruction, then we may
2865 have to account for an additional stall. */
2866 if (shift_opnum != 0 && attr_type == TYPE_NORMAL)
2868 rtx shifted_operand;
2869 int opno;
2871 /* Get the shifted operand. */
2872 extract_insn (insn);
2873 shifted_operand = recog_data.operand[shift_opnum];
2875 /* Iterate over all the operands in DEP. If we write an operand
2876 that overlaps with SHIFTED_OPERAND, then we have increase the
2877 cost of this dependency. */
2878 extract_insn (dep);
2879 preprocess_constraints ();
2880 for (opno = 0; opno < recog_data.n_operands; opno++)
2882 /* We can ignore strict inputs. */
2883 if (recog_data.operand_type[opno] == OP_IN)
2884 continue;
2886 if (reg_overlap_mentioned_p (recog_data.operand[opno],
2887 shifted_operand))
2888 return 2;
2893 /* XXX This is not strictly true for the FPA. */
2894 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
2895 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
2896 return 0;
2898 /* Call insns don't incur a stall, even if they follow a load. */
2899 if (REG_NOTE_KIND (link) == 0
2900 && GET_CODE (insn) == CALL_INSN)
2901 return 1;
2903 if ((i_pat = single_set (insn)) != NULL
2904 && GET_CODE (SET_SRC (i_pat)) == MEM
2905 && (d_pat = single_set (dep)) != NULL
2906 && GET_CODE (SET_DEST (d_pat)) == MEM)
2908 /* This is a load after a store, there is no conflict if the load reads
2909 from a cached area. Assume that loads from the stack, and from the
2910 constant pool are cached, and that others will miss. This is a
2911 hack. */
2913 if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
2914 || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2915 || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2916 || reg_mentioned_p (hard_frame_pointer_rtx,
2917 XEXP (SET_SRC (i_pat), 0)))
2918 return 1;
2921 return cost;
2924 /* This code has been fixed for cross compilation. */
2926 static int fpa_consts_inited = 0;
2928 static const char * const strings_fpa[8] =
2930 "0", "1", "2", "3",
2931 "4", "5", "0.5", "10"
2934 static REAL_VALUE_TYPE values_fpa[8];
2936 static void
2937 init_fpa_table ()
2939 int i;
2940 REAL_VALUE_TYPE r;
2942 for (i = 0; i < 8; i++)
2944 r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
2945 values_fpa[i] = r;
2948 fpa_consts_inited = 1;
2951 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2954 const_double_rtx_ok_for_fpu (x)
2955 rtx x;
2957 REAL_VALUE_TYPE r;
2958 int i;
2960 if (!fpa_consts_inited)
2961 init_fpa_table ();
2963 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2964 if (REAL_VALUE_MINUS_ZERO (r))
2965 return 0;
2967 for (i = 0; i < 8; i++)
2968 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2969 return 1;
2971 return 0;
2974 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2977 neg_const_double_rtx_ok_for_fpu (x)
2978 rtx x;
2980 REAL_VALUE_TYPE r;
2981 int i;
2983 if (!fpa_consts_inited)
2984 init_fpa_table ();
2986 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2987 r = REAL_VALUE_NEGATE (r);
2988 if (REAL_VALUE_MINUS_ZERO (r))
2989 return 0;
2991 for (i = 0; i < 8; i++)
2992 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2993 return 1;
2995 return 0;
2998 /* Predicates for `match_operand' and `match_operator'. */
3000 /* s_register_operand is the same as register_operand, but it doesn't accept
3001 (SUBREG (MEM)...).
3003 This function exists because at the time it was put in it led to better
3004 code. SUBREG(MEM) always needs a reload in the places where
3005 s_register_operand is used, and this seemed to lead to excessive
3006 reloading. */
3009 s_register_operand (op, mode)
3010 rtx op;
3011 enum machine_mode mode;
3013 if (GET_MODE (op) != mode && mode != VOIDmode)
3014 return 0;
3016 if (GET_CODE (op) == SUBREG)
3017 op = SUBREG_REG (op);
3019 /* We don't consider registers whose class is NO_REGS
3020 to be a register operand. */
3021 /* XXX might have to check for lo regs only for thumb ??? */
3022 return (GET_CODE (op) == REG
3023 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3024 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3027 /* A hard register operand (even before reload. */
3030 arm_hard_register_operand (op, mode)
3031 rtx op;
3032 enum machine_mode mode;
3034 if (GET_MODE (op) != mode && mode != VOIDmode)
3035 return 0;
3037 return (GET_CODE (op) == REG
3038 && REGNO (op) < FIRST_PSEUDO_REGISTER);
3041 /* Only accept reg, subreg(reg), const_int. */
3044 reg_or_int_operand (op, mode)
3045 rtx op;
3046 enum machine_mode mode;
3048 if (GET_CODE (op) == CONST_INT)
3049 return 1;
3051 if (GET_MODE (op) != mode && mode != VOIDmode)
3052 return 0;
3054 if (GET_CODE (op) == SUBREG)
3055 op = SUBREG_REG (op);
3057 /* We don't consider registers whose class is NO_REGS
3058 to be a register operand. */
3059 return (GET_CODE (op) == REG
3060 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3061 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3064 /* Return 1 if OP is an item in memory, given that we are in reload. */
3067 arm_reload_memory_operand (op, mode)
3068 rtx op;
3069 enum machine_mode mode ATTRIBUTE_UNUSED;
3071 int regno = true_regnum (op);
3073 return (!CONSTANT_P (op)
3074 && (regno == -1
3075 || (GET_CODE (op) == REG
3076 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
3079 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
3080 memory access (architecture V4).
3081 MODE is QImode if called when computing constraints, or VOIDmode when
3082 emitting patterns. In this latter case we cannot use memory_operand()
3083 because it will fail on badly formed MEMs, which is precisly what we are
3084 trying to catch. */
3087 bad_signed_byte_operand (op, mode)
3088 rtx op;
3089 enum machine_mode mode ATTRIBUTE_UNUSED;
3091 #if 0
3092 if ((mode == QImode && !memory_operand (op, mode)) || GET_CODE (op) != MEM)
3093 return 0;
3094 #endif
3095 if (GET_CODE (op) != MEM)
3096 return 0;
3098 op = XEXP (op, 0);
3100 /* A sum of anything more complex than reg + reg or reg + const is bad. */
3101 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3102 && (!s_register_operand (XEXP (op, 0), VOIDmode)
3103 || (!s_register_operand (XEXP (op, 1), VOIDmode)
3104 && GET_CODE (XEXP (op, 1)) != CONST_INT)))
3105 return 1;
3107 /* Big constants are also bad. */
3108 if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
3109 && (INTVAL (XEXP (op, 1)) > 0xff
3110 || -INTVAL (XEXP (op, 1)) > 0xff))
3111 return 1;
3113 /* Everything else is good, or can will automatically be made so. */
3114 return 0;
3117 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
3120 arm_rhs_operand (op, mode)
3121 rtx op;
3122 enum machine_mode mode;
3124 return (s_register_operand (op, mode)
3125 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
3128 /* Return TRUE for valid operands for the
3129 rhs of an ARM instruction, or a load. */
3132 arm_rhsm_operand (op, mode)
3133 rtx op;
3134 enum machine_mode mode;
3136 return (s_register_operand (op, mode)
3137 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
3138 || memory_operand (op, mode));
3141 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
3142 constant that is valid when negated. */
3145 arm_add_operand (op, mode)
3146 rtx op;
3147 enum machine_mode mode;
3149 if (TARGET_THUMB)
3150 return thumb_cmp_operand (op, mode);
3152 return (s_register_operand (op, mode)
3153 || (GET_CODE (op) == CONST_INT
3154 && (const_ok_for_arm (INTVAL (op))
3155 || const_ok_for_arm (-INTVAL (op)))));
3159 arm_not_operand (op, mode)
3160 rtx op;
3161 enum machine_mode mode;
3163 return (s_register_operand (op, mode)
3164 || (GET_CODE (op) == CONST_INT
3165 && (const_ok_for_arm (INTVAL (op))
3166 || const_ok_for_arm (~INTVAL (op)))));
3169 /* Return TRUE if the operand is a memory reference which contains an
3170 offsettable address. */
3173 offsettable_memory_operand (op, mode)
3174 rtx op;
3175 enum machine_mode mode;
3177 if (mode == VOIDmode)
3178 mode = GET_MODE (op);
3180 return (mode == GET_MODE (op)
3181 && GET_CODE (op) == MEM
3182 && offsettable_address_p (reload_completed | reload_in_progress,
3183 mode, XEXP (op, 0)));
3186 /* Return TRUE if the operand is a memory reference which is, or can be
3187 made word aligned by adjusting the offset. */
3190 alignable_memory_operand (op, mode)
3191 rtx op;
3192 enum machine_mode mode;
3194 rtx reg;
3196 if (mode == VOIDmode)
3197 mode = GET_MODE (op);
3199 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
3200 return 0;
3202 op = XEXP (op, 0);
3204 return ((GET_CODE (reg = op) == REG
3205 || (GET_CODE (op) == SUBREG
3206 && GET_CODE (reg = SUBREG_REG (op)) == REG)
3207 || (GET_CODE (op) == PLUS
3208 && GET_CODE (XEXP (op, 1)) == CONST_INT
3209 && (GET_CODE (reg = XEXP (op, 0)) == REG
3210 || (GET_CODE (XEXP (op, 0)) == SUBREG
3211 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
3212 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
3215 /* Similar to s_register_operand, but does not allow hard integer
3216 registers. */
3219 f_register_operand (op, mode)
3220 rtx op;
3221 enum machine_mode mode;
3223 if (GET_MODE (op) != mode && mode != VOIDmode)
3224 return 0;
3226 if (GET_CODE (op) == SUBREG)
3227 op = SUBREG_REG (op);
3229 /* We don't consider registers whose class is NO_REGS
3230 to be a register operand. */
3231 return (GET_CODE (op) == REG
3232 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3233 || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
3236 /* Return TRUE for valid operands for the rhs of an FPU instruction. */
3239 fpu_rhs_operand (op, mode)
3240 rtx op;
3241 enum machine_mode mode;
3243 if (s_register_operand (op, mode))
3244 return TRUE;
3246 if (GET_MODE (op) != mode && mode != VOIDmode)
3247 return FALSE;
3249 if (GET_CODE (op) == CONST_DOUBLE)
3250 return const_double_rtx_ok_for_fpu (op);
3252 return FALSE;
3256 fpu_add_operand (op, mode)
3257 rtx op;
3258 enum machine_mode mode;
3260 if (s_register_operand (op, mode))
3261 return TRUE;
3263 if (GET_MODE (op) != mode && mode != VOIDmode)
3264 return FALSE;
3266 if (GET_CODE (op) == CONST_DOUBLE)
3267 return (const_double_rtx_ok_for_fpu (op)
3268 || neg_const_double_rtx_ok_for_fpu (op));
3270 return FALSE;
3273 /* Return nonzero if OP is a constant power of two. */
3276 power_of_two_operand (op, mode)
3277 rtx op;
3278 enum machine_mode mode ATTRIBUTE_UNUSED;
3280 if (GET_CODE (op) == CONST_INT)
3282 HOST_WIDE_INT value = INTVAL (op);
3284 return value != 0 && (value & (value - 1)) == 0;
3287 return FALSE;
3290 /* Return TRUE for a valid operand of a DImode operation.
3291 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
3292 Note that this disallows MEM(REG+REG), but allows
3293 MEM(PRE/POST_INC/DEC(REG)). */
3296 di_operand (op, mode)
3297 rtx op;
3298 enum machine_mode mode;
3300 if (s_register_operand (op, mode))
3301 return TRUE;
3303 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
3304 return FALSE;
3306 if (GET_CODE (op) == SUBREG)
3307 op = SUBREG_REG (op);
3309 switch (GET_CODE (op))
3311 case CONST_DOUBLE:
3312 case CONST_INT:
3313 return TRUE;
3315 case MEM:
3316 return memory_address_p (DImode, XEXP (op, 0));
3318 default:
3319 return FALSE;
3323 /* Like di_operand, but don't accept constants. */
3326 nonimmediate_di_operand (op, mode)
3327 rtx op;
3328 enum machine_mode mode;
3330 if (s_register_operand (op, mode))
3331 return TRUE;
3333 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
3334 return FALSE;
3336 if (GET_CODE (op) == SUBREG)
3337 op = SUBREG_REG (op);
3339 if (GET_CODE (op) == MEM)
3340 return memory_address_p (DImode, XEXP (op, 0));
3342 return FALSE;
3345 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
3346 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
3347 Note that this disallows MEM(REG+REG), but allows
3348 MEM(PRE/POST_INC/DEC(REG)). */
3351 soft_df_operand (op, mode)
3352 rtx op;
3353 enum machine_mode mode;
3355 if (s_register_operand (op, mode))
3356 return TRUE;
3358 if (mode != VOIDmode && GET_MODE (op) != mode)
3359 return FALSE;
3361 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
3362 return FALSE;
3364 if (GET_CODE (op) == SUBREG)
3365 op = SUBREG_REG (op);
3367 switch (GET_CODE (op))
3369 case CONST_DOUBLE:
3370 return TRUE;
3372 case MEM:
3373 return memory_address_p (DFmode, XEXP (op, 0));
3375 default:
3376 return FALSE;
3380 /* Like soft_df_operand, but don't accept constants. */
3383 nonimmediate_soft_df_operand (op, mode)
3384 rtx op;
3385 enum machine_mode mode;
3387 if (s_register_operand (op, mode))
3388 return TRUE;
3390 if (mode != VOIDmode && GET_MODE (op) != mode)
3391 return FALSE;
3393 if (GET_CODE (op) == SUBREG)
3394 op = SUBREG_REG (op);
3396 if (GET_CODE (op) == MEM)
3397 return memory_address_p (DFmode, XEXP (op, 0));
3398 return FALSE;
3401 /* Return TRUE for valid index operands. */
3404 index_operand (op, mode)
3405 rtx op;
3406 enum machine_mode mode;
3408 return (s_register_operand (op, mode)
3409 || (immediate_operand (op, mode)
3410 && (GET_CODE (op) != CONST_INT
3411 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
3414 /* Return TRUE for valid shifts by a constant. This also accepts any
3415 power of two on the (somewhat overly relaxed) assumption that the
3416 shift operator in this case was a mult. */
3419 const_shift_operand (op, mode)
3420 rtx op;
3421 enum machine_mode mode;
3423 return (power_of_two_operand (op, mode)
3424 || (immediate_operand (op, mode)
3425 && (GET_CODE (op) != CONST_INT
3426 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
3429 /* Return TRUE for arithmetic operators which can be combined with a multiply
3430 (shift). */
3433 shiftable_operator (x, mode)
3434 rtx x;
3435 enum machine_mode mode;
3437 enum rtx_code code;
3439 if (GET_MODE (x) != mode)
3440 return FALSE;
3442 code = GET_CODE (x);
3444 return (code == PLUS || code == MINUS
3445 || code == IOR || code == XOR || code == AND);
3448 /* Return TRUE for binary logical operators. */
3451 logical_binary_operator (x, mode)
3452 rtx x;
3453 enum machine_mode mode;
3455 enum rtx_code code;
3457 if (GET_MODE (x) != mode)
3458 return FALSE;
3460 code = GET_CODE (x);
3462 return (code == IOR || code == XOR || code == AND);
3465 /* Return TRUE for shift operators. */
3468 shift_operator (x, mode)
3469 rtx x;
3470 enum machine_mode mode;
3472 enum rtx_code code;
3474 if (GET_MODE (x) != mode)
3475 return FALSE;
3477 code = GET_CODE (x);
3479 if (code == MULT)
3480 return power_of_two_operand (XEXP (x, 1), mode);
3482 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
3483 || code == ROTATERT);
3486 /* Return TRUE if x is EQ or NE. */
3489 equality_operator (x, mode)
3490 rtx x;
3491 enum machine_mode mode ATTRIBUTE_UNUSED;
3493 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
3496 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ. */
3499 arm_comparison_operator (x, mode)
3500 rtx x;
3501 enum machine_mode mode;
3503 return (comparison_operator (x, mode)
3504 && GET_CODE (x) != LTGT
3505 && GET_CODE (x) != UNEQ);
3508 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
3511 minmax_operator (x, mode)
3512 rtx x;
3513 enum machine_mode mode;
3515 enum rtx_code code = GET_CODE (x);
3517 if (GET_MODE (x) != mode)
3518 return FALSE;
3520 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
3523 /* Return TRUE if this is the condition code register, if we aren't given
3524 a mode, accept any class CCmode register. */
3527 cc_register (x, mode)
3528 rtx x;
3529 enum machine_mode mode;
3531 if (mode == VOIDmode)
3533 mode = GET_MODE (x);
3535 if (GET_MODE_CLASS (mode) != MODE_CC)
3536 return FALSE;
3539 if ( GET_MODE (x) == mode
3540 && GET_CODE (x) == REG
3541 && REGNO (x) == CC_REGNUM)
3542 return TRUE;
3544 return FALSE;
3547 /* Return TRUE if this is the condition code register, if we aren't given
3548 a mode, accept any class CCmode register which indicates a dominance
3549 expression. */
3552 dominant_cc_register (x, mode)
3553 rtx x;
3554 enum machine_mode mode;
3556 if (mode == VOIDmode)
3558 mode = GET_MODE (x);
3560 if (GET_MODE_CLASS (mode) != MODE_CC)
3561 return FALSE;
3564 if ( mode != CC_DNEmode && mode != CC_DEQmode
3565 && mode != CC_DLEmode && mode != CC_DLTmode
3566 && mode != CC_DGEmode && mode != CC_DGTmode
3567 && mode != CC_DLEUmode && mode != CC_DLTUmode
3568 && mode != CC_DGEUmode && mode != CC_DGTUmode)
3569 return FALSE;
3571 return cc_register (x, mode);
3574 /* Return TRUE if X references a SYMBOL_REF. */
3577 symbol_mentioned_p (x)
3578 rtx x;
3580 const char * fmt;
3581 int i;
3583 if (GET_CODE (x) == SYMBOL_REF)
3584 return 1;
3586 fmt = GET_RTX_FORMAT (GET_CODE (x));
3588 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3590 if (fmt[i] == 'E')
3592 int j;
3594 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3595 if (symbol_mentioned_p (XVECEXP (x, i, j)))
3596 return 1;
3598 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
3599 return 1;
3602 return 0;
3605 /* Return TRUE if X references a LABEL_REF. */
3608 label_mentioned_p (x)
3609 rtx x;
3611 const char * fmt;
3612 int i;
3614 if (GET_CODE (x) == LABEL_REF)
3615 return 1;
3617 fmt = GET_RTX_FORMAT (GET_CODE (x));
3618 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3620 if (fmt[i] == 'E')
3622 int j;
3624 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3625 if (label_mentioned_p (XVECEXP (x, i, j)))
3626 return 1;
3628 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
3629 return 1;
3632 return 0;
3635 enum rtx_code
3636 minmax_code (x)
3637 rtx x;
3639 enum rtx_code code = GET_CODE (x);
3641 if (code == SMAX)
3642 return GE;
3643 else if (code == SMIN)
3644 return LE;
3645 else if (code == UMIN)
3646 return LEU;
3647 else if (code == UMAX)
3648 return GEU;
3650 abort ();
3653 /* Return 1 if memory locations are adjacent. */
3656 adjacent_mem_locations (a, b)
3657 rtx a, b;
3659 if ((GET_CODE (XEXP (a, 0)) == REG
3660 || (GET_CODE (XEXP (a, 0)) == PLUS
3661 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
3662 && (GET_CODE (XEXP (b, 0)) == REG
3663 || (GET_CODE (XEXP (b, 0)) == PLUS
3664 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
3666 int val0 = 0, val1 = 0;
3667 int reg0, reg1;
3669 if (GET_CODE (XEXP (a, 0)) == PLUS)
3671 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
3672 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
3674 else
3675 reg0 = REGNO (XEXP (a, 0));
3677 if (GET_CODE (XEXP (b, 0)) == PLUS)
3679 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
3680 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
3682 else
3683 reg1 = REGNO (XEXP (b, 0));
3685 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
3687 return 0;
3690 /* Return 1 if OP is a load multiple operation. It is known to be
3691 parallel and the first section will be tested. */
3694 load_multiple_operation (op, mode)
3695 rtx op;
3696 enum machine_mode mode ATTRIBUTE_UNUSED;
3698 HOST_WIDE_INT count = XVECLEN (op, 0);
3699 int dest_regno;
3700 rtx src_addr;
3701 HOST_WIDE_INT i = 1, base = 0;
3702 rtx elt;
3704 if (count <= 1
3705 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3706 return 0;
3708 /* Check to see if this might be a write-back. */
3709 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3711 i++;
3712 base = 1;
3714 /* Now check it more carefully. */
3715 if (GET_CODE (SET_DEST (elt)) != REG
3716 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3717 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3718 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3719 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
3720 return 0;
3723 /* Perform a quick check so we don't blow up below. */
3724 if (count <= i
3725 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3726 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
3727 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
3728 return 0;
3730 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
3731 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
3733 for (; i < count; i++)
3735 elt = XVECEXP (op, 0, i);
3737 if (GET_CODE (elt) != SET
3738 || GET_CODE (SET_DEST (elt)) != REG
3739 || GET_MODE (SET_DEST (elt)) != SImode
3740 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
3741 || GET_CODE (SET_SRC (elt)) != MEM
3742 || GET_MODE (SET_SRC (elt)) != SImode
3743 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
3744 || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
3745 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
3746 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
3747 return 0;
3750 return 1;
3753 /* Return 1 if OP is a store multiple operation. It is known to be
3754 parallel and the first section will be tested. */
3757 store_multiple_operation (op, mode)
3758 rtx op;
3759 enum machine_mode mode ATTRIBUTE_UNUSED;
3761 HOST_WIDE_INT count = XVECLEN (op, 0);
3762 int src_regno;
3763 rtx dest_addr;
3764 HOST_WIDE_INT i = 1, base = 0;
3765 rtx elt;
3767 if (count <= 1
3768 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3769 return 0;
3771 /* Check to see if this might be a write-back. */
3772 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3774 i++;
3775 base = 1;
3777 /* Now check it more carefully. */
3778 if (GET_CODE (SET_DEST (elt)) != REG
3779 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3780 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3781 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3782 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
3783 return 0;
3786 /* Perform a quick check so we don't blow up below. */
3787 if (count <= i
3788 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3789 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
3790 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
3791 return 0;
3793 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
3794 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
3796 for (; i < count; i++)
3798 elt = XVECEXP (op, 0, i);
3800 if (GET_CODE (elt) != SET
3801 || GET_CODE (SET_SRC (elt)) != REG
3802 || GET_MODE (SET_SRC (elt)) != SImode
3803 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
3804 || GET_CODE (SET_DEST (elt)) != MEM
3805 || GET_MODE (SET_DEST (elt)) != SImode
3806 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
3807 || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
3808 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
3809 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
3810 return 0;
3813 return 1;
3817 load_multiple_sequence (operands, nops, regs, base, load_offset)
3818 rtx * operands;
3819 int nops;
3820 int * regs;
3821 int * base;
3822 HOST_WIDE_INT * load_offset;
3824 int unsorted_regs[4];
3825 HOST_WIDE_INT unsorted_offsets[4];
3826 int order[4];
3827 int base_reg = -1;
3828 int i;
3830 /* Can only handle 2, 3, or 4 insns at present,
3831 though could be easily extended if required. */
3832 if (nops < 2 || nops > 4)
3833 abort ();
3835 /* Loop over the operands and check that the memory references are
3836 suitable (ie immediate offsets from the same base register). At
3837 the same time, extract the target register, and the memory
3838 offsets. */
3839 for (i = 0; i < nops; i++)
3841 rtx reg;
3842 rtx offset;
3844 /* Convert a subreg of a mem into the mem itself. */
3845 if (GET_CODE (operands[nops + i]) == SUBREG)
3846 operands[nops + i] = alter_subreg (operands + (nops + i));
3848 if (GET_CODE (operands[nops + i]) != MEM)
3849 abort ();
3851 /* Don't reorder volatile memory references; it doesn't seem worth
3852 looking for the case where the order is ok anyway. */
3853 if (MEM_VOLATILE_P (operands[nops + i]))
3854 return 0;
3856 offset = const0_rtx;
3858 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3859 || (GET_CODE (reg) == SUBREG
3860 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3861 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3862 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3863 == REG)
3864 || (GET_CODE (reg) == SUBREG
3865 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3866 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3867 == CONST_INT)))
3869 if (i == 0)
3871 base_reg = REGNO (reg);
3872 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3873 ? REGNO (operands[i])
3874 : REGNO (SUBREG_REG (operands[i])));
3875 order[0] = 0;
3877 else
3879 if (base_reg != (int) REGNO (reg))
3880 /* Not addressed from the same base register. */
3881 return 0;
3883 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3884 ? REGNO (operands[i])
3885 : REGNO (SUBREG_REG (operands[i])));
3886 if (unsorted_regs[i] < unsorted_regs[order[0]])
3887 order[0] = i;
3890 /* If it isn't an integer register, or if it overwrites the
3891 base register but isn't the last insn in the list, then
3892 we can't do this. */
3893 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
3894 || (i != nops - 1 && unsorted_regs[i] == base_reg))
3895 return 0;
3897 unsorted_offsets[i] = INTVAL (offset);
3899 else
3900 /* Not a suitable memory address. */
3901 return 0;
3904 /* All the useful information has now been extracted from the
3905 operands into unsorted_regs and unsorted_offsets; additionally,
3906 order[0] has been set to the lowest numbered register in the
3907 list. Sort the registers into order, and check that the memory
3908 offsets are ascending and adjacent. */
3910 for (i = 1; i < nops; i++)
3912 int j;
3914 order[i] = order[i - 1];
3915 for (j = 0; j < nops; j++)
3916 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3917 && (order[i] == order[i - 1]
3918 || unsorted_regs[j] < unsorted_regs[order[i]]))
3919 order[i] = j;
3921 /* Have we found a suitable register? if not, one must be used more
3922 than once. */
3923 if (order[i] == order[i - 1])
3924 return 0;
3926 /* Is the memory address adjacent and ascending? */
3927 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3928 return 0;
3931 if (base)
3933 *base = base_reg;
3935 for (i = 0; i < nops; i++)
3936 regs[i] = unsorted_regs[order[i]];
3938 *load_offset = unsorted_offsets[order[0]];
3941 if (unsorted_offsets[order[0]] == 0)
3942 return 1; /* ldmia */
3944 if (unsorted_offsets[order[0]] == 4)
3945 return 2; /* ldmib */
3947 if (unsorted_offsets[order[nops - 1]] == 0)
3948 return 3; /* ldmda */
3950 if (unsorted_offsets[order[nops - 1]] == -4)
3951 return 4; /* ldmdb */
3953 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
3954 if the offset isn't small enough. The reason 2 ldrs are faster
3955 is because these ARMs are able to do more than one cache access
3956 in a single cycle. The ARM9 and StrongARM have Harvard caches,
3957 whilst the ARM8 has a double bandwidth cache. This means that
3958 these cores can do both an instruction fetch and a data fetch in
3959 a single cycle, so the trick of calculating the address into a
3960 scratch register (one of the result regs) and then doing a load
3961 multiple actually becomes slower (and no smaller in code size).
3962 That is the transformation
3964 ldr rd1, [rbase + offset]
3965 ldr rd2, [rbase + offset + 4]
3969 add rd1, rbase, offset
3970 ldmia rd1, {rd1, rd2}
3972 produces worse code -- '3 cycles + any stalls on rd2' instead of
3973 '2 cycles + any stalls on rd2'. On ARMs with only one cache
3974 access per cycle, the first sequence could never complete in less
3975 than 6 cycles, whereas the ldm sequence would only take 5 and
3976 would make better use of sequential accesses if not hitting the
3977 cache.
3979 We cheat here and test 'arm_ld_sched' which we currently know to
3980 only be true for the ARM8, ARM9 and StrongARM. If this ever
3981 changes, then the test below needs to be reworked. */
3982 if (nops == 2 && arm_ld_sched)
3983 return 0;
3985 /* Can't do it without setting up the offset, only do this if it takes
3986 no more than one insn. */
3987 return (const_ok_for_arm (unsorted_offsets[order[0]])
3988 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
3991 const char *
3992 emit_ldm_seq (operands, nops)
3993 rtx * operands;
3994 int nops;
3996 int regs[4];
3997 int base_reg;
3998 HOST_WIDE_INT offset;
3999 char buf[100];
4000 int i;
4002 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
4004 case 1:
4005 strcpy (buf, "ldm%?ia\t");
4006 break;
4008 case 2:
4009 strcpy (buf, "ldm%?ib\t");
4010 break;
4012 case 3:
4013 strcpy (buf, "ldm%?da\t");
4014 break;
4016 case 4:
4017 strcpy (buf, "ldm%?db\t");
4018 break;
4020 case 5:
4021 if (offset >= 0)
4022 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
4023 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
4024 (long) offset);
4025 else
4026 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
4027 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
4028 (long) -offset);
4029 output_asm_insn (buf, operands);
4030 base_reg = regs[0];
4031 strcpy (buf, "ldm%?ia\t");
4032 break;
4034 default:
4035 abort ();
4038 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
4039 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
4041 for (i = 1; i < nops; i++)
4042 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
4043 reg_names[regs[i]]);
4045 strcat (buf, "}\t%@ phole ldm");
4047 output_asm_insn (buf, operands);
4048 return "";
4052 store_multiple_sequence (operands, nops, regs, base, load_offset)
4053 rtx * operands;
4054 int nops;
4055 int * regs;
4056 int * base;
4057 HOST_WIDE_INT * load_offset;
4059 int unsorted_regs[4];
4060 HOST_WIDE_INT unsorted_offsets[4];
4061 int order[4];
4062 int base_reg = -1;
4063 int i;
4065 /* Can only handle 2, 3, or 4 insns at present, though could be easily
4066 extended if required. */
4067 if (nops < 2 || nops > 4)
4068 abort ();
4070 /* Loop over the operands and check that the memory references are
4071 suitable (ie immediate offsets from the same base register). At
4072 the same time, extract the target register, and the memory
4073 offsets. */
4074 for (i = 0; i < nops; i++)
4076 rtx reg;
4077 rtx offset;
4079 /* Convert a subreg of a mem into the mem itself. */
4080 if (GET_CODE (operands[nops + i]) == SUBREG)
4081 operands[nops + i] = alter_subreg (operands + (nops + i));
4083 if (GET_CODE (operands[nops + i]) != MEM)
4084 abort ();
4086 /* Don't reorder volatile memory references; it doesn't seem worth
4087 looking for the case where the order is ok anyway. */
4088 if (MEM_VOLATILE_P (operands[nops + i]))
4089 return 0;
4091 offset = const0_rtx;
4093 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
4094 || (GET_CODE (reg) == SUBREG
4095 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4096 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
4097 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
4098 == REG)
4099 || (GET_CODE (reg) == SUBREG
4100 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4101 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
4102 == CONST_INT)))
4104 if (i == 0)
4106 base_reg = REGNO (reg);
4107 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
4108 ? REGNO (operands[i])
4109 : REGNO (SUBREG_REG (operands[i])));
4110 order[0] = 0;
4112 else
4114 if (base_reg != (int) REGNO (reg))
4115 /* Not addressed from the same base register. */
4116 return 0;
4118 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
4119 ? REGNO (operands[i])
4120 : REGNO (SUBREG_REG (operands[i])));
4121 if (unsorted_regs[i] < unsorted_regs[order[0]])
4122 order[0] = i;
4125 /* If it isn't an integer register, then we can't do this. */
4126 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
4127 return 0;
4129 unsorted_offsets[i] = INTVAL (offset);
4131 else
4132 /* Not a suitable memory address. */
4133 return 0;
4136 /* All the useful information has now been extracted from the
4137 operands into unsorted_regs and unsorted_offsets; additionally,
4138 order[0] has been set to the lowest numbered register in the
4139 list. Sort the registers into order, and check that the memory
4140 offsets are ascending and adjacent. */
4142 for (i = 1; i < nops; i++)
4144 int j;
4146 order[i] = order[i - 1];
4147 for (j = 0; j < nops; j++)
4148 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
4149 && (order[i] == order[i - 1]
4150 || unsorted_regs[j] < unsorted_regs[order[i]]))
4151 order[i] = j;
4153 /* Have we found a suitable register? if not, one must be used more
4154 than once. */
4155 if (order[i] == order[i - 1])
4156 return 0;
4158 /* Is the memory address adjacent and ascending? */
4159 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
4160 return 0;
4163 if (base)
4165 *base = base_reg;
4167 for (i = 0; i < nops; i++)
4168 regs[i] = unsorted_regs[order[i]];
4170 *load_offset = unsorted_offsets[order[0]];
4173 if (unsorted_offsets[order[0]] == 0)
4174 return 1; /* stmia */
4176 if (unsorted_offsets[order[0]] == 4)
4177 return 2; /* stmib */
4179 if (unsorted_offsets[order[nops - 1]] == 0)
4180 return 3; /* stmda */
4182 if (unsorted_offsets[order[nops - 1]] == -4)
4183 return 4; /* stmdb */
4185 return 0;
4188 const char *
4189 emit_stm_seq (operands, nops)
4190 rtx * operands;
4191 int nops;
4193 int regs[4];
4194 int base_reg;
4195 HOST_WIDE_INT offset;
4196 char buf[100];
4197 int i;
4199 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
4201 case 1:
4202 strcpy (buf, "stm%?ia\t");
4203 break;
4205 case 2:
4206 strcpy (buf, "stm%?ib\t");
4207 break;
4209 case 3:
4210 strcpy (buf, "stm%?da\t");
4211 break;
4213 case 4:
4214 strcpy (buf, "stm%?db\t");
4215 break;
4217 default:
4218 abort ();
4221 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
4222 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
4224 for (i = 1; i < nops; i++)
4225 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
4226 reg_names[regs[i]]);
4228 strcat (buf, "}\t%@ phole stm");
4230 output_asm_insn (buf, operands);
4231 return "";
4235 multi_register_push (op, mode)
4236 rtx op;
4237 enum machine_mode mode ATTRIBUTE_UNUSED;
4239 if (GET_CODE (op) != PARALLEL
4240 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
4241 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
4242 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
4243 return 0;
4245 return 1;
4248 /* Routines for use in generating RTL. */
4251 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
4252 in_struct_p, scalar_p)
4253 int base_regno;
4254 int count;
4255 rtx from;
4256 int up;
4257 int write_back;
4258 int unchanging_p;
4259 int in_struct_p;
4260 int scalar_p;
4262 int i = 0, j;
4263 rtx result;
4264 int sign = up ? 1 : -1;
4265 rtx mem;
4267 /* XScale has load-store double instructions, but they have stricter
4268 alignment requirements than load-store multiple, so we can not
4269 use them.
4271 For XScale ldm requires 2 + NREGS cycles to complete and blocks
4272 the pipeline until completion.
4274 NREGS CYCLES
4280 An ldr instruction takes 1-3 cycles, but does not block the
4281 pipeline.
4283 NREGS CYCLES
4284 1 1-3
4285 2 2-6
4286 3 3-9
4287 4 4-12
4289 Best case ldr will always win. However, the more ldr instructions
4290 we issue, the less likely we are to be able to schedule them well.
4291 Using ldr instructions also increases code size.
4293 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
4294 for counts of 3 or 4 regs. */
4295 if (arm_is_xscale && count <= 2 && ! optimize_size)
4297 rtx seq;
4299 start_sequence ();
4301 for (i = 0; i < count; i++)
4303 mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
4304 RTX_UNCHANGING_P (mem) = unchanging_p;
4305 MEM_IN_STRUCT_P (mem) = in_struct_p;
4306 MEM_SCALAR_P (mem) = scalar_p;
4307 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
4310 if (write_back)
4311 emit_move_insn (from, plus_constant (from, count * 4 * sign));
4313 seq = gen_sequence ();
4314 end_sequence ();
4316 return seq;
4319 result = gen_rtx_PARALLEL (VOIDmode,
4320 rtvec_alloc (count + (write_back ? 1 : 0)));
4321 if (write_back)
4323 XVECEXP (result, 0, 0)
4324 = gen_rtx_SET (GET_MODE (from), from,
4325 plus_constant (from, count * 4 * sign));
4326 i = 1;
4327 count++;
4330 for (j = 0; i < count; i++, j++)
4332 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
4333 RTX_UNCHANGING_P (mem) = unchanging_p;
4334 MEM_IN_STRUCT_P (mem) = in_struct_p;
4335 MEM_SCALAR_P (mem) = scalar_p;
4336 XVECEXP (result, 0, i)
4337 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
4340 return result;
4344 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
4345 in_struct_p, scalar_p)
4346 int base_regno;
4347 int count;
4348 rtx to;
4349 int up;
4350 int write_back;
4351 int unchanging_p;
4352 int in_struct_p;
4353 int scalar_p;
4355 int i = 0, j;
4356 rtx result;
4357 int sign = up ? 1 : -1;
4358 rtx mem;
4360 /* See arm_gen_load_multiple for discussion of
4361 the pros/cons of ldm/stm usage for XScale. */
4362 if (arm_is_xscale && count <= 2 && ! optimize_size)
4364 rtx seq;
4366 start_sequence ();
4368 for (i = 0; i < count; i++)
4370 mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
4371 RTX_UNCHANGING_P (mem) = unchanging_p;
4372 MEM_IN_STRUCT_P (mem) = in_struct_p;
4373 MEM_SCALAR_P (mem) = scalar_p;
4374 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
4377 if (write_back)
4378 emit_move_insn (to, plus_constant (to, count * 4 * sign));
4380 seq = gen_sequence ();
4381 end_sequence ();
4383 return seq;
4386 result = gen_rtx_PARALLEL (VOIDmode,
4387 rtvec_alloc (count + (write_back ? 1 : 0)));
4388 if (write_back)
4390 XVECEXP (result, 0, 0)
4391 = gen_rtx_SET (GET_MODE (to), to,
4392 plus_constant (to, count * 4 * sign));
4393 i = 1;
4394 count++;
4397 for (j = 0; i < count; i++, j++)
4399 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
4400 RTX_UNCHANGING_P (mem) = unchanging_p;
4401 MEM_IN_STRUCT_P (mem) = in_struct_p;
4402 MEM_SCALAR_P (mem) = scalar_p;
4404 XVECEXP (result, 0, i)
4405 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
4408 return result;
4412 arm_gen_movstrqi (operands)
4413 rtx * operands;
4415 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
4416 int i;
4417 rtx src, dst;
4418 rtx st_src, st_dst, fin_src, fin_dst;
4419 rtx part_bytes_reg = NULL;
4420 rtx mem;
4421 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
4422 int dst_scalar_p, src_scalar_p;
4424 if (GET_CODE (operands[2]) != CONST_INT
4425 || GET_CODE (operands[3]) != CONST_INT
4426 || INTVAL (operands[2]) > 64
4427 || INTVAL (operands[3]) & 3)
4428 return 0;
4430 st_dst = XEXP (operands[0], 0);
4431 st_src = XEXP (operands[1], 0);
4433 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
4434 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
4435 dst_scalar_p = MEM_SCALAR_P (operands[0]);
4436 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
4437 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
4438 src_scalar_p = MEM_SCALAR_P (operands[1]);
4440 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
4441 fin_src = src = copy_to_mode_reg (SImode, st_src);
4443 in_words_to_go = NUM_INTS (INTVAL (operands[2]));
4444 out_words_to_go = INTVAL (operands[2]) / 4;
4445 last_bytes = INTVAL (operands[2]) & 3;
4447 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
4448 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
4450 for (i = 0; in_words_to_go >= 2; i+=4)
4452 if (in_words_to_go > 4)
4453 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
4454 src_unchanging_p,
4455 src_in_struct_p,
4456 src_scalar_p));
4457 else
4458 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
4459 FALSE, src_unchanging_p,
4460 src_in_struct_p, src_scalar_p));
4462 if (out_words_to_go)
4464 if (out_words_to_go > 4)
4465 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
4466 dst_unchanging_p,
4467 dst_in_struct_p,
4468 dst_scalar_p));
4469 else if (out_words_to_go != 1)
4470 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
4471 dst, TRUE,
4472 (last_bytes == 0
4473 ? FALSE : TRUE),
4474 dst_unchanging_p,
4475 dst_in_struct_p,
4476 dst_scalar_p));
4477 else
4479 mem = gen_rtx_MEM (SImode, dst);
4480 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4481 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4482 MEM_SCALAR_P (mem) = dst_scalar_p;
4483 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
4484 if (last_bytes != 0)
4485 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
4489 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
4490 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
4493 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
4494 if (out_words_to_go)
4496 rtx sreg;
4498 mem = gen_rtx_MEM (SImode, src);
4499 RTX_UNCHANGING_P (mem) = src_unchanging_p;
4500 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
4501 MEM_SCALAR_P (mem) = src_scalar_p;
4502 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
4503 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
4505 mem = gen_rtx_MEM (SImode, dst);
4506 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4507 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4508 MEM_SCALAR_P (mem) = dst_scalar_p;
4509 emit_move_insn (mem, sreg);
4510 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
4511 in_words_to_go--;
4513 if (in_words_to_go) /* Sanity check */
4514 abort ();
4517 if (in_words_to_go)
4519 if (in_words_to_go < 0)
4520 abort ();
4522 mem = gen_rtx_MEM (SImode, src);
4523 RTX_UNCHANGING_P (mem) = src_unchanging_p;
4524 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
4525 MEM_SCALAR_P (mem) = src_scalar_p;
4526 part_bytes_reg = copy_to_mode_reg (SImode, mem);
4529 if (last_bytes && part_bytes_reg == NULL)
4530 abort ();
4532 if (BYTES_BIG_ENDIAN && last_bytes)
4534 rtx tmp = gen_reg_rtx (SImode);
4536 /* The bytes we want are in the top end of the word. */
4537 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
4538 GEN_INT (8 * (4 - last_bytes))));
4539 part_bytes_reg = tmp;
4541 while (last_bytes)
4543 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
4544 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4545 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4546 MEM_SCALAR_P (mem) = dst_scalar_p;
4547 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
4549 if (--last_bytes)
4551 tmp = gen_reg_rtx (SImode);
4552 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
4553 part_bytes_reg = tmp;
4558 else
4560 if (last_bytes > 1)
4562 mem = gen_rtx_MEM (HImode, dst);
4563 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4564 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4565 MEM_SCALAR_P (mem) = dst_scalar_p;
4566 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
4567 last_bytes -= 2;
4568 if (last_bytes)
4570 rtx tmp = gen_reg_rtx (SImode);
4572 emit_insn (gen_addsi3 (dst, dst, GEN_INT (2)));
4573 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
4574 part_bytes_reg = tmp;
4578 if (last_bytes)
4580 mem = gen_rtx_MEM (QImode, dst);
4581 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4582 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4583 MEM_SCALAR_P (mem) = dst_scalar_p;
4584 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
4588 return 1;
4591 /* Generate a memory reference for a half word, such that it will be loaded
4592 into the top 16 bits of the word. We can assume that the address is
4593 known to be alignable and of the form reg, or plus (reg, const). */
4596 arm_gen_rotated_half_load (memref)
4597 rtx memref;
4599 HOST_WIDE_INT offset = 0;
4600 rtx base = XEXP (memref, 0);
4602 if (GET_CODE (base) == PLUS)
4604 offset = INTVAL (XEXP (base, 1));
4605 base = XEXP (base, 0);
4608 /* If we aren't allowed to generate unaligned addresses, then fail. */
4609 if (TARGET_MMU_TRAPS
4610 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
4611 return NULL;
4613 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
4615 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
4616 return base;
4618 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
4621 /* Select a dominance comparison mode if possible. We support three forms.
4622 COND_OR == 0 => (X && Y)
4623 COND_OR == 1 => ((! X( || Y)
4624 COND_OR == 2 => (X || Y)
4625 If we are unable to support a dominance comparsison we return CC mode.
4626 This will then fail to match for the RTL expressions that generate this
4627 call. */
4629 static enum machine_mode
4630 select_dominance_cc_mode (x, y, cond_or)
4631 rtx x;
4632 rtx y;
4633 HOST_WIDE_INT cond_or;
4635 enum rtx_code cond1, cond2;
4636 int swapped = 0;
4638 /* Currently we will probably get the wrong result if the individual
4639 comparisons are not simple. This also ensures that it is safe to
4640 reverse a comparison if necessary. */
4641 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
4642 != CCmode)
4643 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
4644 != CCmode))
4645 return CCmode;
4647 /* The if_then_else variant of this tests the second condition if the
4648 first passes, but is true if the first fails. Reverse the first
4649 condition to get a true "inclusive-or" expression. */
4650 if (cond_or == 1)
4651 cond1 = reverse_condition (cond1);
4653 /* If the comparisons are not equal, and one doesn't dominate the other,
4654 then we can't do this. */
4655 if (cond1 != cond2
4656 && !comparison_dominates_p (cond1, cond2)
4657 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
4658 return CCmode;
4660 if (swapped)
4662 enum rtx_code temp = cond1;
4663 cond1 = cond2;
4664 cond2 = temp;
4667 switch (cond1)
4669 case EQ:
4670 if (cond2 == EQ || !cond_or)
4671 return CC_DEQmode;
4673 switch (cond2)
4675 case LE: return CC_DLEmode;
4676 case LEU: return CC_DLEUmode;
4677 case GE: return CC_DGEmode;
4678 case GEU: return CC_DGEUmode;
4679 default: break;
4682 break;
4684 case LT:
4685 if (cond2 == LT || !cond_or)
4686 return CC_DLTmode;
4687 if (cond2 == LE)
4688 return CC_DLEmode;
4689 if (cond2 == NE)
4690 return CC_DNEmode;
4691 break;
4693 case GT:
4694 if (cond2 == GT || !cond_or)
4695 return CC_DGTmode;
4696 if (cond2 == GE)
4697 return CC_DGEmode;
4698 if (cond2 == NE)
4699 return CC_DNEmode;
4700 break;
4702 case LTU:
4703 if (cond2 == LTU || !cond_or)
4704 return CC_DLTUmode;
4705 if (cond2 == LEU)
4706 return CC_DLEUmode;
4707 if (cond2 == NE)
4708 return CC_DNEmode;
4709 break;
4711 case GTU:
4712 if (cond2 == GTU || !cond_or)
4713 return CC_DGTUmode;
4714 if (cond2 == GEU)
4715 return CC_DGEUmode;
4716 if (cond2 == NE)
4717 return CC_DNEmode;
4718 break;
4720 /* The remaining cases only occur when both comparisons are the
4721 same. */
4722 case NE:
4723 return CC_DNEmode;
4725 case LE:
4726 return CC_DLEmode;
4728 case GE:
4729 return CC_DGEmode;
4731 case LEU:
4732 return CC_DLEUmode;
4734 case GEU:
4735 return CC_DGEUmode;
4737 default:
4738 break;
4741 abort ();
4744 enum machine_mode
4745 arm_select_cc_mode (op, x, y)
4746 enum rtx_code op;
4747 rtx x;
4748 rtx y;
4750 /* All floating point compares return CCFP if it is an equality
4751 comparison, and CCFPE otherwise. */
4752 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
4754 switch (op)
4756 case EQ:
4757 case NE:
4758 case UNORDERED:
4759 case ORDERED:
4760 case UNLT:
4761 case UNLE:
4762 case UNGT:
4763 case UNGE:
4764 case UNEQ:
4765 case LTGT:
4766 return CCFPmode;
4768 case LT:
4769 case LE:
4770 case GT:
4771 case GE:
4772 return CCFPEmode;
4774 default:
4775 abort ();
4779 /* A compare with a shifted operand. Because of canonicalization, the
4780 comparison will have to be swapped when we emit the assembler. */
4781 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
4782 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4783 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
4784 || GET_CODE (x) == ROTATERT))
4785 return CC_SWPmode;
4787 /* This is a special case that is used by combine to allow a
4788 comparison of a shifted byte load to be split into a zero-extend
4789 followed by a comparison of the shifted integer (only valid for
4790 equalities and unsigned inequalities). */
4791 if (GET_MODE (x) == SImode
4792 && GET_CODE (x) == ASHIFT
4793 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
4794 && GET_CODE (XEXP (x, 0)) == SUBREG
4795 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
4796 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
4797 && (op == EQ || op == NE
4798 || op == GEU || op == GTU || op == LTU || op == LEU)
4799 && GET_CODE (y) == CONST_INT)
4800 return CC_Zmode;
4802 /* A construct for a conditional compare, if the false arm contains
4803 0, then both conditions must be true, otherwise either condition
4804 must be true. Not all conditions are possible, so CCmode is
4805 returned if it can't be done. */
4806 if (GET_CODE (x) == IF_THEN_ELSE
4807 && (XEXP (x, 2) == const0_rtx
4808 || XEXP (x, 2) == const1_rtx)
4809 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4810 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4811 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
4812 INTVAL (XEXP (x, 2)));
4814 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
4815 if (GET_CODE (x) == AND
4816 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4817 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4818 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 0);
4820 if (GET_CODE (x) == IOR
4821 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4822 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4823 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 2);
4825 /* An operation that sets the condition codes as a side-effect, the
4826 V flag is not set correctly, so we can only use comparisons where
4827 this doesn't matter. (For LT and GE we can use "mi" and "pl"
4828 instead. */
4829 if (GET_MODE (x) == SImode
4830 && y == const0_rtx
4831 && (op == EQ || op == NE || op == LT || op == GE)
4832 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
4833 || GET_CODE (x) == AND || GET_CODE (x) == IOR
4834 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
4835 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
4836 || GET_CODE (x) == LSHIFTRT
4837 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4838 || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
4839 return CC_NOOVmode;
4841 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
4842 return CC_Zmode;
4844 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
4845 && GET_CODE (x) == PLUS
4846 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
4847 return CC_Cmode;
4849 return CCmode;
4852 /* X and Y are two things to compare using CODE. Emit the compare insn and
4853 return the rtx for register 0 in the proper mode. FP means this is a
4854 floating point compare: I don't think that it is needed on the arm. */
4857 arm_gen_compare_reg (code, x, y)
4858 enum rtx_code code;
4859 rtx x, y;
4861 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
4862 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
4864 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
4865 gen_rtx_COMPARE (mode, x, y)));
4867 return cc_reg;
4870 void
4871 arm_reload_in_hi (operands)
4872 rtx * operands;
4874 rtx ref = operands[1];
4875 rtx base, scratch;
4876 HOST_WIDE_INT offset = 0;
4878 if (GET_CODE (ref) == SUBREG)
4880 offset = SUBREG_BYTE (ref);
4881 ref = SUBREG_REG (ref);
4884 if (GET_CODE (ref) == REG)
4886 /* We have a pseudo which has been spilt onto the stack; there
4887 are two cases here: the first where there is a simple
4888 stack-slot replacement and a second where the stack-slot is
4889 out of range, or is used as a subreg. */
4890 if (reg_equiv_mem[REGNO (ref)])
4892 ref = reg_equiv_mem[REGNO (ref)];
4893 base = find_replacement (&XEXP (ref, 0));
4895 else
4896 /* The slot is out of range, or was dressed up in a SUBREG. */
4897 base = reg_equiv_address[REGNO (ref)];
4899 else
4900 base = find_replacement (&XEXP (ref, 0));
4902 /* Handle the case where the address is too complex to be offset by 1. */
4903 if (GET_CODE (base) == MINUS
4904 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4906 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4908 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4909 base = base_plus;
4911 else if (GET_CODE (base) == PLUS)
4913 /* The addend must be CONST_INT, or we would have dealt with it above. */
4914 HOST_WIDE_INT hi, lo;
4916 offset += INTVAL (XEXP (base, 1));
4917 base = XEXP (base, 0);
4919 /* Rework the address into a legal sequence of insns. */
4920 /* Valid range for lo is -4095 -> 4095 */
4921 lo = (offset >= 0
4922 ? (offset & 0xfff)
4923 : -((-offset) & 0xfff));
4925 /* Corner case, if lo is the max offset then we would be out of range
4926 once we have added the additional 1 below, so bump the msb into the
4927 pre-loading insn(s). */
4928 if (lo == 4095)
4929 lo &= 0x7ff;
4931 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
4932 ^ (HOST_WIDE_INT) 0x80000000)
4933 - (HOST_WIDE_INT) 0x80000000);
4935 if (hi + lo != offset)
4936 abort ();
4938 if (hi != 0)
4940 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4942 /* Get the base address; addsi3 knows how to handle constants
4943 that require more than one insn. */
4944 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4945 base = base_plus;
4946 offset = lo;
4950 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4951 emit_insn (gen_zero_extendqisi2 (scratch,
4952 gen_rtx_MEM (QImode,
4953 plus_constant (base,
4954 offset))));
4955 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
4956 gen_rtx_MEM (QImode,
4957 plus_constant (base,
4958 offset + 1))));
4959 if (!BYTES_BIG_ENDIAN)
4960 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4961 gen_rtx_IOR (SImode,
4962 gen_rtx_ASHIFT
4963 (SImode,
4964 gen_rtx_SUBREG (SImode, operands[0], 0),
4965 GEN_INT (8)),
4966 scratch)));
4967 else
4968 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4969 gen_rtx_IOR (SImode,
4970 gen_rtx_ASHIFT (SImode, scratch,
4971 GEN_INT (8)),
4972 gen_rtx_SUBREG (SImode, operands[0],
4973 0))));
4976 /* Handle storing a half-word to memory during reload by synthesising as two
4977 byte stores. Take care not to clobber the input values until after we
4978 have moved them somewhere safe. This code assumes that if the DImode
4979 scratch in operands[2] overlaps either the input value or output address
4980 in some way, then that value must die in this insn (we absolutely need
4981 two scratch registers for some corner cases). */
4983 void
4984 arm_reload_out_hi (operands)
4985 rtx * operands;
4987 rtx ref = operands[0];
4988 rtx outval = operands[1];
4989 rtx base, scratch;
4990 HOST_WIDE_INT offset = 0;
4992 if (GET_CODE (ref) == SUBREG)
4994 offset = SUBREG_BYTE (ref);
4995 ref = SUBREG_REG (ref);
4998 if (GET_CODE (ref) == REG)
5000 /* We have a pseudo which has been spilt onto the stack; there
5001 are two cases here: the first where there is a simple
5002 stack-slot replacement and a second where the stack-slot is
5003 out of range, or is used as a subreg. */
5004 if (reg_equiv_mem[REGNO (ref)])
5006 ref = reg_equiv_mem[REGNO (ref)];
5007 base = find_replacement (&XEXP (ref, 0));
5009 else
5010 /* The slot is out of range, or was dressed up in a SUBREG. */
5011 base = reg_equiv_address[REGNO (ref)];
5013 else
5014 base = find_replacement (&XEXP (ref, 0));
5016 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
5018 /* Handle the case where the address is too complex to be offset by 1. */
5019 if (GET_CODE (base) == MINUS
5020 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
5022 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5024 /* Be careful not to destroy OUTVAL. */
5025 if (reg_overlap_mentioned_p (base_plus, outval))
5027 /* Updating base_plus might destroy outval, see if we can
5028 swap the scratch and base_plus. */
5029 if (!reg_overlap_mentioned_p (scratch, outval))
5031 rtx tmp = scratch;
5032 scratch = base_plus;
5033 base_plus = tmp;
5035 else
5037 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5039 /* Be conservative and copy OUTVAL into the scratch now,
5040 this should only be necessary if outval is a subreg
5041 of something larger than a word. */
5042 /* XXX Might this clobber base? I can't see how it can,
5043 since scratch is known to overlap with OUTVAL, and
5044 must be wider than a word. */
5045 emit_insn (gen_movhi (scratch_hi, outval));
5046 outval = scratch_hi;
5050 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
5051 base = base_plus;
5053 else if (GET_CODE (base) == PLUS)
5055 /* The addend must be CONST_INT, or we would have dealt with it above. */
5056 HOST_WIDE_INT hi, lo;
5058 offset += INTVAL (XEXP (base, 1));
5059 base = XEXP (base, 0);
5061 /* Rework the address into a legal sequence of insns. */
5062 /* Valid range for lo is -4095 -> 4095 */
5063 lo = (offset >= 0
5064 ? (offset & 0xfff)
5065 : -((-offset) & 0xfff));
5067 /* Corner case, if lo is the max offset then we would be out of range
5068 once we have added the additional 1 below, so bump the msb into the
5069 pre-loading insn(s). */
5070 if (lo == 4095)
5071 lo &= 0x7ff;
5073 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
5074 ^ (HOST_WIDE_INT) 0x80000000)
5075 - (HOST_WIDE_INT) 0x80000000);
5077 if (hi + lo != offset)
5078 abort ();
5080 if (hi != 0)
5082 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5084 /* Be careful not to destroy OUTVAL. */
5085 if (reg_overlap_mentioned_p (base_plus, outval))
5087 /* Updating base_plus might destroy outval, see if we
5088 can swap the scratch and base_plus. */
5089 if (!reg_overlap_mentioned_p (scratch, outval))
5091 rtx tmp = scratch;
5092 scratch = base_plus;
5093 base_plus = tmp;
5095 else
5097 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5099 /* Be conservative and copy outval into scratch now,
5100 this should only be necessary if outval is a
5101 subreg of something larger than a word. */
5102 /* XXX Might this clobber base? I can't see how it
5103 can, since scratch is known to overlap with
5104 outval. */
5105 emit_insn (gen_movhi (scratch_hi, outval));
5106 outval = scratch_hi;
5110 /* Get the base address; addsi3 knows how to handle constants
5111 that require more than one insn. */
5112 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
5113 base = base_plus;
5114 offset = lo;
5118 if (BYTES_BIG_ENDIAN)
5120 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
5121 plus_constant (base, offset + 1)),
5122 gen_lowpart (QImode, outval)));
5123 emit_insn (gen_lshrsi3 (scratch,
5124 gen_rtx_SUBREG (SImode, outval, 0),
5125 GEN_INT (8)));
5126 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5127 gen_lowpart (QImode, scratch)));
5129 else
5131 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5132 gen_lowpart (QImode, outval)));
5133 emit_insn (gen_lshrsi3 (scratch,
5134 gen_rtx_SUBREG (SImode, outval, 0),
5135 GEN_INT (8)));
5136 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
5137 plus_constant (base, offset + 1)),
5138 gen_lowpart (QImode, scratch)));
5142 /* Print a symbolic form of X to the debug file, F. */
5144 static void
5145 arm_print_value (f, x)
5146 FILE * f;
5147 rtx x;
5149 switch (GET_CODE (x))
5151 case CONST_INT:
5152 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
5153 return;
5155 case CONST_DOUBLE:
5156 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
5157 return;
5159 case CONST_STRING:
5160 fprintf (f, "\"%s\"", XSTR (x, 0));
5161 return;
5163 case SYMBOL_REF:
5164 fprintf (f, "`%s'", XSTR (x, 0));
5165 return;
5167 case LABEL_REF:
5168 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
5169 return;
5171 case CONST:
5172 arm_print_value (f, XEXP (x, 0));
5173 return;
5175 case PLUS:
5176 arm_print_value (f, XEXP (x, 0));
5177 fprintf (f, "+");
5178 arm_print_value (f, XEXP (x, 1));
5179 return;
5181 case PC:
5182 fprintf (f, "pc");
5183 return;
5185 default:
5186 fprintf (f, "????");
5187 return;
5191 /* Routines for manipulation of the constant pool. */
5193 /* Arm instructions cannot load a large constant directly into a
5194 register; they have to come from a pc relative load. The constant
5195 must therefore be placed in the addressable range of the pc
5196 relative load. Depending on the precise pc relative load
5197 instruction the range is somewhere between 256 bytes and 4k. This
5198 means that we often have to dump a constant inside a function, and
5199 generate code to branch around it.
5201 It is important to minimize this, since the branches will slow
5202 things down and make the code larger.
5204 Normally we can hide the table after an existing unconditional
5205 branch so that there is no interruption of the flow, but in the
5206 worst case the code looks like this:
5208 ldr rn, L1
5210 b L2
5211 align
5212 L1: .long value
5216 ldr rn, L3
5218 b L4
5219 align
5220 L3: .long value
5224 We fix this by performing a scan after scheduling, which notices
5225 which instructions need to have their operands fetched from the
5226 constant table and builds the table.
5228 The algorithm starts by building a table of all the constants that
5229 need fixing up and all the natural barriers in the function (places
5230 where a constant table can be dropped without breaking the flow).
5231 For each fixup we note how far the pc-relative replacement will be
5232 able to reach and the offset of the instruction into the function.
5234 Having built the table we then group the fixes together to form
5235 tables that are as large as possible (subject to addressing
5236 constraints) and emit each table of constants after the last
5237 barrier that is within range of all the instructions in the group.
5238 If a group does not contain a barrier, then we forcibly create one
5239 by inserting a jump instruction into the flow. Once the table has
5240 been inserted, the insns are then modified to reference the
5241 relevant entry in the pool.
5243 Possible enhancements to the algorithm (not implemented) are:
5245 1) For some processors and object formats, there may be benefit in
5246 aligning the pools to the start of cache lines; this alignment
5247 would need to be taken into account when calculating addressability
5248 of a pool. */
5250 /* These typedefs are located at the start of this file, so that
5251 they can be used in the prototypes there. This comment is to
5252 remind readers of that fact so that the following structures
5253 can be understood more easily.
5255 typedef struct minipool_node Mnode;
5256 typedef struct minipool_fixup Mfix; */
5258 struct minipool_node
5260 /* Doubly linked chain of entries. */
5261 Mnode * next;
5262 Mnode * prev;
5263 /* The maximum offset into the code that this entry can be placed. While
5264 pushing fixes for forward references, all entries are sorted in order
5265 of increasing max_address. */
5266 HOST_WIDE_INT max_address;
5267 /* Similarly for an entry inserted for a backwards ref. */
5268 HOST_WIDE_INT min_address;
5269 /* The number of fixes referencing this entry. This can become zero
5270 if we "unpush" an entry. In this case we ignore the entry when we
5271 come to emit the code. */
5272 int refcount;
5273 /* The offset from the start of the minipool. */
5274 HOST_WIDE_INT offset;
5275 /* The value in table. */
5276 rtx value;
5277 /* The mode of value. */
5278 enum machine_mode mode;
5279 int fix_size;
5282 struct minipool_fixup
5284 Mfix * next;
5285 rtx insn;
5286 HOST_WIDE_INT address;
5287 rtx * loc;
5288 enum machine_mode mode;
5289 int fix_size;
5290 rtx value;
5291 Mnode * minipool;
5292 HOST_WIDE_INT forwards;
5293 HOST_WIDE_INT backwards;
5296 /* Fixes less than a word need padding out to a word boundary. */
5297 #define MINIPOOL_FIX_SIZE(mode) \
5298 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
5300 static Mnode * minipool_vector_head;
5301 static Mnode * minipool_vector_tail;
5302 static rtx minipool_vector_label;
5304 /* The linked list of all minipool fixes required for this function. */
5305 Mfix * minipool_fix_head;
5306 Mfix * minipool_fix_tail;
5307 /* The fix entry for the current minipool, once it has been placed. */
5308 Mfix * minipool_barrier;
5310 /* Determines if INSN is the start of a jump table. Returns the end
5311 of the TABLE or NULL_RTX. */
5313 static rtx
5314 is_jump_table (insn)
5315 rtx insn;
5317 rtx table;
5319 if (GET_CODE (insn) == JUMP_INSN
5320 && JUMP_LABEL (insn) != NULL
5321 && ((table = next_real_insn (JUMP_LABEL (insn)))
5322 == next_real_insn (insn))
5323 && table != NULL
5324 && GET_CODE (table) == JUMP_INSN
5325 && (GET_CODE (PATTERN (table)) == ADDR_VEC
5326 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
5327 return table;
5329 return NULL_RTX;
5332 #ifndef JUMP_TABLES_IN_TEXT_SECTION
5333 #define JUMP_TABLES_IN_TEXT_SECTION 0
5334 #endif
5336 static HOST_WIDE_INT
5337 get_jump_table_size (insn)
5338 rtx insn;
5340 /* ADDR_VECs only take room if read-only data does into the text
5341 section. */
5342 if (JUMP_TABLES_IN_TEXT_SECTION
5343 #if !defined(READONLY_DATA_SECTION)
5344 || 1
5345 #endif
5348 rtx body = PATTERN (insn);
5349 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
5351 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
5354 return 0;
5357 /* Move a minipool fix MP from its current location to before MAX_MP.
5358 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
5359 contrains may need updating. */
5361 static Mnode *
5362 move_minipool_fix_forward_ref (mp, max_mp, max_address)
5363 Mnode * mp;
5364 Mnode * max_mp;
5365 HOST_WIDE_INT max_address;
5367 /* This should never be true and the code below assumes these are
5368 different. */
5369 if (mp == max_mp)
5370 abort ();
5372 if (max_mp == NULL)
5374 if (max_address < mp->max_address)
5375 mp->max_address = max_address;
5377 else
5379 if (max_address > max_mp->max_address - mp->fix_size)
5380 mp->max_address = max_mp->max_address - mp->fix_size;
5381 else
5382 mp->max_address = max_address;
5384 /* Unlink MP from its current position. Since max_mp is non-null,
5385 mp->prev must be non-null. */
5386 mp->prev->next = mp->next;
5387 if (mp->next != NULL)
5388 mp->next->prev = mp->prev;
5389 else
5390 minipool_vector_tail = mp->prev;
5392 /* Re-insert it before MAX_MP. */
5393 mp->next = max_mp;
5394 mp->prev = max_mp->prev;
5395 max_mp->prev = mp;
5397 if (mp->prev != NULL)
5398 mp->prev->next = mp;
5399 else
5400 minipool_vector_head = mp;
5403 /* Save the new entry. */
5404 max_mp = mp;
5406 /* Scan over the preceding entries and adjust their addresses as
5407 required. */
5408 while (mp->prev != NULL
5409 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
5411 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
5412 mp = mp->prev;
5415 return max_mp;
5418 /* Add a constant to the minipool for a forward reference. Returns the
5419 node added or NULL if the constant will not fit in this pool. */
5421 static Mnode *
5422 add_minipool_forward_ref (fix)
5423 Mfix * fix;
5425 /* If set, max_mp is the first pool_entry that has a lower
5426 constraint than the one we are trying to add. */
5427 Mnode * max_mp = NULL;
5428 HOST_WIDE_INT max_address = fix->address + fix->forwards;
5429 Mnode * mp;
5431 /* If this fix's address is greater than the address of the first
5432 entry, then we can't put the fix in this pool. We subtract the
5433 size of the current fix to ensure that if the table is fully
5434 packed we still have enough room to insert this value by suffling
5435 the other fixes forwards. */
5436 if (minipool_vector_head &&
5437 fix->address >= minipool_vector_head->max_address - fix->fix_size)
5438 return NULL;
5440 /* Scan the pool to see if a constant with the same value has
5441 already been added. While we are doing this, also note the
5442 location where we must insert the constant if it doesn't already
5443 exist. */
5444 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5446 if (GET_CODE (fix->value) == GET_CODE (mp->value)
5447 && fix->mode == mp->mode
5448 && (GET_CODE (fix->value) != CODE_LABEL
5449 || (CODE_LABEL_NUMBER (fix->value)
5450 == CODE_LABEL_NUMBER (mp->value)))
5451 && rtx_equal_p (fix->value, mp->value))
5453 /* More than one fix references this entry. */
5454 mp->refcount++;
5455 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
5458 /* Note the insertion point if necessary. */
5459 if (max_mp == NULL
5460 && mp->max_address > max_address)
5461 max_mp = mp;
5464 /* The value is not currently in the minipool, so we need to create
5465 a new entry for it. If MAX_MP is NULL, the entry will be put on
5466 the end of the list since the placement is less constrained than
5467 any existing entry. Otherwise, we insert the new fix before
5468 MAX_MP and, if neceesary, adjust the constraints on the other
5469 entries. */
5470 mp = xmalloc (sizeof (* mp));
5471 mp->fix_size = fix->fix_size;
5472 mp->mode = fix->mode;
5473 mp->value = fix->value;
5474 mp->refcount = 1;
5475 /* Not yet required for a backwards ref. */
5476 mp->min_address = -65536;
5478 if (max_mp == NULL)
5480 mp->max_address = max_address;
5481 mp->next = NULL;
5482 mp->prev = minipool_vector_tail;
5484 if (mp->prev == NULL)
5486 minipool_vector_head = mp;
5487 minipool_vector_label = gen_label_rtx ();
5489 else
5490 mp->prev->next = mp;
5492 minipool_vector_tail = mp;
5494 else
5496 if (max_address > max_mp->max_address - mp->fix_size)
5497 mp->max_address = max_mp->max_address - mp->fix_size;
5498 else
5499 mp->max_address = max_address;
5501 mp->next = max_mp;
5502 mp->prev = max_mp->prev;
5503 max_mp->prev = mp;
5504 if (mp->prev != NULL)
5505 mp->prev->next = mp;
5506 else
5507 minipool_vector_head = mp;
5510 /* Save the new entry. */
5511 max_mp = mp;
5513 /* Scan over the preceding entries and adjust their addresses as
5514 required. */
5515 while (mp->prev != NULL
5516 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
5518 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
5519 mp = mp->prev;
5522 return max_mp;
5525 static Mnode *
5526 move_minipool_fix_backward_ref (mp, min_mp, min_address)
5527 Mnode * mp;
5528 Mnode * min_mp;
5529 HOST_WIDE_INT min_address;
5531 HOST_WIDE_INT offset;
5533 /* This should never be true, and the code below assumes these are
5534 different. */
5535 if (mp == min_mp)
5536 abort ();
5538 if (min_mp == NULL)
5540 if (min_address > mp->min_address)
5541 mp->min_address = min_address;
5543 else
5545 /* We will adjust this below if it is too loose. */
5546 mp->min_address = min_address;
5548 /* Unlink MP from its current position. Since min_mp is non-null,
5549 mp->next must be non-null. */
5550 mp->next->prev = mp->prev;
5551 if (mp->prev != NULL)
5552 mp->prev->next = mp->next;
5553 else
5554 minipool_vector_head = mp->next;
5556 /* Reinsert it after MIN_MP. */
5557 mp->prev = min_mp;
5558 mp->next = min_mp->next;
5559 min_mp->next = mp;
5560 if (mp->next != NULL)
5561 mp->next->prev = mp;
5562 else
5563 minipool_vector_tail = mp;
5566 min_mp = mp;
5568 offset = 0;
5569 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5571 mp->offset = offset;
5572 if (mp->refcount > 0)
5573 offset += mp->fix_size;
5575 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
5576 mp->next->min_address = mp->min_address + mp->fix_size;
5579 return min_mp;
5582 /* Add a constant to the minipool for a backward reference. Returns the
5583 node added or NULL if the constant will not fit in this pool.
5585 Note that the code for insertion for a backwards reference can be
5586 somewhat confusing because the calculated offsets for each fix do
5587 not take into account the size of the pool (which is still under
5588 construction. */
5590 static Mnode *
5591 add_minipool_backward_ref (fix)
5592 Mfix * fix;
5594 /* If set, min_mp is the last pool_entry that has a lower constraint
5595 than the one we are trying to add. */
5596 Mnode * min_mp = NULL;
5597 /* This can be negative, since it is only a constraint. */
5598 HOST_WIDE_INT min_address = fix->address - fix->backwards;
5599 Mnode * mp;
5601 /* If we can't reach the current pool from this insn, or if we can't
5602 insert this entry at the end of the pool without pushing other
5603 fixes out of range, then we don't try. This ensures that we
5604 can't fail later on. */
5605 if (min_address >= minipool_barrier->address
5606 || (minipool_vector_tail->min_address + fix->fix_size
5607 >= minipool_barrier->address))
5608 return NULL;
5610 /* Scan the pool to see if a constant with the same value has
5611 already been added. While we are doing this, also note the
5612 location where we must insert the constant if it doesn't already
5613 exist. */
5614 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
5616 if (GET_CODE (fix->value) == GET_CODE (mp->value)
5617 && fix->mode == mp->mode
5618 && (GET_CODE (fix->value) != CODE_LABEL
5619 || (CODE_LABEL_NUMBER (fix->value)
5620 == CODE_LABEL_NUMBER (mp->value)))
5621 && rtx_equal_p (fix->value, mp->value)
5622 /* Check that there is enough slack to move this entry to the
5623 end of the table (this is conservative). */
5624 && (mp->max_address
5625 > (minipool_barrier->address
5626 + minipool_vector_tail->offset
5627 + minipool_vector_tail->fix_size)))
5629 mp->refcount++;
5630 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
5633 if (min_mp != NULL)
5634 mp->min_address += fix->fix_size;
5635 else
5637 /* Note the insertion point if necessary. */
5638 if (mp->min_address < min_address)
5639 min_mp = mp;
5640 else if (mp->max_address
5641 < minipool_barrier->address + mp->offset + fix->fix_size)
5643 /* Inserting before this entry would push the fix beyond
5644 its maximum address (which can happen if we have
5645 re-located a forwards fix); force the new fix to come
5646 after it. */
5647 min_mp = mp;
5648 min_address = mp->min_address + fix->fix_size;
5653 /* We need to create a new entry. */
5654 mp = xmalloc (sizeof (* mp));
5655 mp->fix_size = fix->fix_size;
5656 mp->mode = fix->mode;
5657 mp->value = fix->value;
5658 mp->refcount = 1;
5659 mp->max_address = minipool_barrier->address + 65536;
5661 mp->min_address = min_address;
5663 if (min_mp == NULL)
5665 mp->prev = NULL;
5666 mp->next = minipool_vector_head;
5668 if (mp->next == NULL)
5670 minipool_vector_tail = mp;
5671 minipool_vector_label = gen_label_rtx ();
5673 else
5674 mp->next->prev = mp;
5676 minipool_vector_head = mp;
5678 else
5680 mp->next = min_mp->next;
5681 mp->prev = min_mp;
5682 min_mp->next = mp;
5684 if (mp->next != NULL)
5685 mp->next->prev = mp;
5686 else
5687 minipool_vector_tail = mp;
5690 /* Save the new entry. */
5691 min_mp = mp;
5693 if (mp->prev)
5694 mp = mp->prev;
5695 else
5696 mp->offset = 0;
5698 /* Scan over the following entries and adjust their offsets. */
5699 while (mp->next != NULL)
5701 if (mp->next->min_address < mp->min_address + mp->fix_size)
5702 mp->next->min_address = mp->min_address + mp->fix_size;
5704 if (mp->refcount)
5705 mp->next->offset = mp->offset + mp->fix_size;
5706 else
5707 mp->next->offset = mp->offset;
5709 mp = mp->next;
5712 return min_mp;
5715 static void
5716 assign_minipool_offsets (barrier)
5717 Mfix * barrier;
5719 HOST_WIDE_INT offset = 0;
5720 Mnode * mp;
5722 minipool_barrier = barrier;
5724 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5726 mp->offset = offset;
5728 if (mp->refcount > 0)
5729 offset += mp->fix_size;
5733 /* Output the literal table */
5734 static void
5735 dump_minipool (scan)
5736 rtx scan;
5738 Mnode * mp;
5739 Mnode * nmp;
5741 if (rtl_dump_file)
5742 fprintf (rtl_dump_file,
5743 ";; Emitting minipool after insn %u; address %ld\n",
5744 INSN_UID (scan), (unsigned long) minipool_barrier->address);
5746 scan = emit_label_after (gen_label_rtx (), scan);
5747 scan = emit_insn_after (gen_align_4 (), scan);
5748 scan = emit_label_after (minipool_vector_label, scan);
5750 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
5752 if (mp->refcount > 0)
5754 if (rtl_dump_file)
5756 fprintf (rtl_dump_file,
5757 ";; Offset %u, min %ld, max %ld ",
5758 (unsigned) mp->offset, (unsigned long) mp->min_address,
5759 (unsigned long) mp->max_address);
5760 arm_print_value (rtl_dump_file, mp->value);
5761 fputc ('\n', rtl_dump_file);
5764 switch (mp->fix_size)
5766 #ifdef HAVE_consttable_1
5767 case 1:
5768 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
5769 break;
5771 #endif
5772 #ifdef HAVE_consttable_2
5773 case 2:
5774 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
5775 break;
5777 #endif
5778 #ifdef HAVE_consttable_4
5779 case 4:
5780 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
5781 break;
5783 #endif
5784 #ifdef HAVE_consttable_8
5785 case 8:
5786 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
5787 break;
5789 #endif
5790 default:
5791 abort ();
5792 break;
5796 nmp = mp->next;
5797 free (mp);
5800 minipool_vector_head = minipool_vector_tail = NULL;
5801 scan = emit_insn_after (gen_consttable_end (), scan);
5802 scan = emit_barrier_after (scan);
5805 /* Return the cost of forcibly inserting a barrier after INSN. */
5807 static int
5808 arm_barrier_cost (insn)
5809 rtx insn;
5811 /* Basing the location of the pool on the loop depth is preferable,
5812 but at the moment, the basic block information seems to be
5813 corrupt by this stage of the compilation. */
5814 int base_cost = 50;
5815 rtx next = next_nonnote_insn (insn);
5817 if (next != NULL && GET_CODE (next) == CODE_LABEL)
5818 base_cost -= 20;
5820 switch (GET_CODE (insn))
5822 case CODE_LABEL:
5823 /* It will always be better to place the table before the label, rather
5824 than after it. */
5825 return 50;
5827 case INSN:
5828 case CALL_INSN:
5829 return base_cost;
5831 case JUMP_INSN:
5832 return base_cost - 10;
5834 default:
5835 return base_cost + 10;
5839 /* Find the best place in the insn stream in the range
5840 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
5841 Create the barrier by inserting a jump and add a new fix entry for
5842 it. */
5844 static Mfix *
5845 create_fix_barrier (fix, max_address)
5846 Mfix * fix;
5847 HOST_WIDE_INT max_address;
5849 HOST_WIDE_INT count = 0;
5850 rtx barrier;
5851 rtx from = fix->insn;
5852 rtx selected = from;
5853 int selected_cost;
5854 HOST_WIDE_INT selected_address;
5855 Mfix * new_fix;
5856 HOST_WIDE_INT max_count = max_address - fix->address;
5857 rtx label = gen_label_rtx ();
5859 selected_cost = arm_barrier_cost (from);
5860 selected_address = fix->address;
5862 while (from && count < max_count)
5864 rtx tmp;
5865 int new_cost;
5867 /* This code shouldn't have been called if there was a natural barrier
5868 within range. */
5869 if (GET_CODE (from) == BARRIER)
5870 abort ();
5872 /* Count the length of this insn. */
5873 count += get_attr_length (from);
5875 /* If there is a jump table, add its length. */
5876 tmp = is_jump_table (from);
5877 if (tmp != NULL)
5879 count += get_jump_table_size (tmp);
5881 /* Jump tables aren't in a basic block, so base the cost on
5882 the dispatch insn. If we select this location, we will
5883 still put the pool after the table. */
5884 new_cost = arm_barrier_cost (from);
5886 if (count < max_count && new_cost <= selected_cost)
5888 selected = tmp;
5889 selected_cost = new_cost;
5890 selected_address = fix->address + count;
5893 /* Continue after the dispatch table. */
5894 from = NEXT_INSN (tmp);
5895 continue;
5898 new_cost = arm_barrier_cost (from);
5900 if (count < max_count && new_cost <= selected_cost)
5902 selected = from;
5903 selected_cost = new_cost;
5904 selected_address = fix->address + count;
5907 from = NEXT_INSN (from);
5910 /* Create a new JUMP_INSN that branches around a barrier. */
5911 from = emit_jump_insn_after (gen_jump (label), selected);
5912 JUMP_LABEL (from) = label;
5913 barrier = emit_barrier_after (from);
5914 emit_label_after (label, barrier);
5916 /* Create a minipool barrier entry for the new barrier. */
5917 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
5918 new_fix->insn = barrier;
5919 new_fix->address = selected_address;
5920 new_fix->next = fix->next;
5921 fix->next = new_fix;
5923 return new_fix;
5926 /* Record that there is a natural barrier in the insn stream at
5927 ADDRESS. */
5928 static void
5929 push_minipool_barrier (insn, address)
5930 rtx insn;
5931 HOST_WIDE_INT address;
5933 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
5935 fix->insn = insn;
5936 fix->address = address;
5938 fix->next = NULL;
5939 if (minipool_fix_head != NULL)
5940 minipool_fix_tail->next = fix;
5941 else
5942 minipool_fix_head = fix;
5944 minipool_fix_tail = fix;
5947 /* Record INSN, which will need fixing up to load a value from the
5948 minipool. ADDRESS is the offset of the insn since the start of the
5949 function; LOC is a pointer to the part of the insn which requires
5950 fixing; VALUE is the constant that must be loaded, which is of type
5951 MODE. */
5952 static void
5953 push_minipool_fix (insn, address, loc, mode, value)
5954 rtx insn;
5955 HOST_WIDE_INT address;
5956 rtx * loc;
5957 enum machine_mode mode;
5958 rtx value;
5960 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
5962 #ifdef AOF_ASSEMBLER
5963 /* PIC symbol refereneces need to be converted into offsets into the
5964 based area. */
5965 /* XXX This shouldn't be done here. */
5966 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
5967 value = aof_pic_entry (value);
5968 #endif /* AOF_ASSEMBLER */
5970 fix->insn = insn;
5971 fix->address = address;
5972 fix->loc = loc;
5973 fix->mode = mode;
5974 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
5975 fix->value = value;
5976 fix->forwards = get_attr_pool_range (insn);
5977 fix->backwards = get_attr_neg_pool_range (insn);
5978 fix->minipool = NULL;
5980 /* If an insn doesn't have a range defined for it, then it isn't
5981 expecting to be reworked by this code. Better to abort now than
5982 to generate duff assembly code. */
5983 if (fix->forwards == 0 && fix->backwards == 0)
5984 abort ();
5986 if (rtl_dump_file)
5988 fprintf (rtl_dump_file,
5989 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
5990 GET_MODE_NAME (mode),
5991 INSN_UID (insn), (unsigned long) address,
5992 -1 * (long)fix->backwards, (long)fix->forwards);
5993 arm_print_value (rtl_dump_file, fix->value);
5994 fprintf (rtl_dump_file, "\n");
5997 /* Add it to the chain of fixes. */
5998 fix->next = NULL;
6000 if (minipool_fix_head != NULL)
6001 minipool_fix_tail->next = fix;
6002 else
6003 minipool_fix_head = fix;
6005 minipool_fix_tail = fix;
6008 /* Scan INSN and note any of its operands that need fixing. */
6010 static void
6011 note_invalid_constants (insn, address)
6012 rtx insn;
6013 HOST_WIDE_INT address;
6015 int opno;
6017 extract_insn (insn);
6019 if (!constrain_operands (1))
6020 fatal_insn_not_found (insn);
6022 /* Fill in recog_op_alt with information about the constraints of this
6023 insn. */
6024 preprocess_constraints ();
6026 for (opno = 0; opno < recog_data.n_operands; opno++)
6028 /* Things we need to fix can only occur in inputs. */
6029 if (recog_data.operand_type[opno] != OP_IN)
6030 continue;
6032 /* If this alternative is a memory reference, then any mention
6033 of constants in this alternative is really to fool reload
6034 into allowing us to accept one there. We need to fix them up
6035 now so that we output the right code. */
6036 if (recog_op_alt[opno][which_alternative].memory_ok)
6038 rtx op = recog_data.operand[opno];
6040 if (CONSTANT_P (op))
6041 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6042 recog_data.operand_mode[opno], op);
6043 #if 0
6044 /* RWE: Now we look correctly at the operands for the insn,
6045 this shouldn't be needed any more. */
6046 #ifndef AOF_ASSEMBLER
6047 /* XXX Is this still needed? */
6048 else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == UNSPEC_PIC_SYM)
6049 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6050 recog_data.operand_mode[opno],
6051 XVECEXP (op, 0, 0));
6052 #endif
6053 #endif
6054 else if (GET_CODE (op) == MEM
6055 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
6056 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
6057 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6058 recog_data.operand_mode[opno],
6059 get_pool_constant (XEXP (op, 0)));
6064 void
6065 arm_reorg (first)
6066 rtx first;
6068 rtx insn;
6069 HOST_WIDE_INT address = 0;
6070 Mfix * fix;
6072 minipool_fix_head = minipool_fix_tail = NULL;
6074 /* The first insn must always be a note, or the code below won't
6075 scan it properly. */
6076 if (GET_CODE (first) != NOTE)
6077 abort ();
6079 /* Scan all the insns and record the operands that will need fixing. */
6080 for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
6082 if (GET_CODE (insn) == BARRIER)
6083 push_minipool_barrier (insn, address);
6084 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
6085 || GET_CODE (insn) == JUMP_INSN)
6087 rtx table;
6089 note_invalid_constants (insn, address);
6090 address += get_attr_length (insn);
6092 /* If the insn is a vector jump, add the size of the table
6093 and skip the table. */
6094 if ((table = is_jump_table (insn)) != NULL)
6096 address += get_jump_table_size (table);
6097 insn = table;
6102 fix = minipool_fix_head;
6104 /* Now scan the fixups and perform the required changes. */
6105 while (fix)
6107 Mfix * ftmp;
6108 Mfix * fdel;
6109 Mfix * last_added_fix;
6110 Mfix * last_barrier = NULL;
6111 Mfix * this_fix;
6113 /* Skip any further barriers before the next fix. */
6114 while (fix && GET_CODE (fix->insn) == BARRIER)
6115 fix = fix->next;
6117 /* No more fixes. */
6118 if (fix == NULL)
6119 break;
6121 last_added_fix = NULL;
6123 for (ftmp = fix; ftmp; ftmp = ftmp->next)
6125 if (GET_CODE (ftmp->insn) == BARRIER)
6127 if (ftmp->address >= minipool_vector_head->max_address)
6128 break;
6130 last_barrier = ftmp;
6132 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
6133 break;
6135 last_added_fix = ftmp; /* Keep track of the last fix added. */
6138 /* If we found a barrier, drop back to that; any fixes that we
6139 could have reached but come after the barrier will now go in
6140 the next mini-pool. */
6141 if (last_barrier != NULL)
6143 /* Reduce the refcount for those fixes that won't go into this
6144 pool after all. */
6145 for (fdel = last_barrier->next;
6146 fdel && fdel != ftmp;
6147 fdel = fdel->next)
6149 fdel->minipool->refcount--;
6150 fdel->minipool = NULL;
6153 ftmp = last_barrier;
6155 else
6157 /* ftmp is first fix that we can't fit into this pool and
6158 there no natural barriers that we could use. Insert a
6159 new barrier in the code somewhere between the previous
6160 fix and this one, and arrange to jump around it. */
6161 HOST_WIDE_INT max_address;
6163 /* The last item on the list of fixes must be a barrier, so
6164 we can never run off the end of the list of fixes without
6165 last_barrier being set. */
6166 if (ftmp == NULL)
6167 abort ();
6169 max_address = minipool_vector_head->max_address;
6170 /* Check that there isn't another fix that is in range that
6171 we couldn't fit into this pool because the pool was
6172 already too large: we need to put the pool before such an
6173 instruction. */
6174 if (ftmp->address < max_address)
6175 max_address = ftmp->address;
6177 last_barrier = create_fix_barrier (last_added_fix, max_address);
6180 assign_minipool_offsets (last_barrier);
6182 while (ftmp)
6184 if (GET_CODE (ftmp->insn) != BARRIER
6185 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
6186 == NULL))
6187 break;
6189 ftmp = ftmp->next;
6192 /* Scan over the fixes we have identified for this pool, fixing them
6193 up and adding the constants to the pool itself. */
6194 for (this_fix = fix; this_fix && ftmp != this_fix;
6195 this_fix = this_fix->next)
6196 if (GET_CODE (this_fix->insn) != BARRIER)
6198 rtx addr
6199 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
6200 minipool_vector_label),
6201 this_fix->minipool->offset);
6202 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
6205 dump_minipool (last_barrier->insn);
6206 fix = ftmp;
6209 /* From now on we must synthesize any constants that we can't handle
6210 directly. This can happen if the RTL gets split during final
6211 instruction generation. */
6212 after_arm_reorg = 1;
6214 /* Free the minipool memory. */
6215 obstack_free (&minipool_obstack, minipool_startobj);
6218 /* Routines to output assembly language. */
6220 /* If the rtx is the correct value then return the string of the number.
6221 In this way we can ensure that valid double constants are generated even
6222 when cross compiling. */
6224 const char *
6225 fp_immediate_constant (x)
6226 rtx x;
6228 REAL_VALUE_TYPE r;
6229 int i;
6231 if (!fpa_consts_inited)
6232 init_fpa_table ();
6234 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6235 for (i = 0; i < 8; i++)
6236 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
6237 return strings_fpa[i];
6239 abort ();
6242 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
6244 static const char *
6245 fp_const_from_val (r)
6246 REAL_VALUE_TYPE * r;
6248 int i;
6250 if (!fpa_consts_inited)
6251 init_fpa_table ();
6253 for (i = 0; i < 8; i++)
6254 if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
6255 return strings_fpa[i];
6257 abort ();
6260 /* Output the operands of a LDM/STM instruction to STREAM.
6261 MASK is the ARM register set mask of which only bits 0-15 are important.
6262 REG is the base register, either the frame pointer or the stack pointer,
6263 INSTR is the possibly suffixed load or store instruction. */
6265 static void
6266 print_multi_reg (stream, instr, reg, mask)
6267 FILE * stream;
6268 const char * instr;
6269 int reg;
6270 int mask;
6272 int i;
6273 int not_first = FALSE;
6275 fputc ('\t', stream);
6276 asm_fprintf (stream, instr, reg);
6277 fputs (", {", stream);
6279 for (i = 0; i <= LAST_ARM_REGNUM; i++)
6280 if (mask & (1 << i))
6282 if (not_first)
6283 fprintf (stream, ", ");
6285 asm_fprintf (stream, "%r", i);
6286 not_first = TRUE;
6289 fprintf (stream, "}%s\n", TARGET_APCS_32 ? "" : "^");
6292 /* Output a 'call' insn. */
6294 const char *
6295 output_call (operands)
6296 rtx * operands;
6298 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
6300 if (REGNO (operands[0]) == LR_REGNUM)
6302 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
6303 output_asm_insn ("mov%?\t%0, %|lr", operands);
6306 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6308 if (TARGET_INTERWORK)
6309 output_asm_insn ("bx%?\t%0", operands);
6310 else
6311 output_asm_insn ("mov%?\t%|pc, %0", operands);
6313 return "";
6316 static int
6317 eliminate_lr2ip (x)
6318 rtx * x;
6320 int something_changed = 0;
6321 rtx x0 = * x;
6322 int code = GET_CODE (x0);
6323 int i, j;
6324 const char * fmt;
6326 switch (code)
6328 case REG:
6329 if (REGNO (x0) == LR_REGNUM)
6331 *x = gen_rtx_REG (SImode, IP_REGNUM);
6332 return 1;
6334 return 0;
6335 default:
6336 /* Scan through the sub-elements and change any references there. */
6337 fmt = GET_RTX_FORMAT (code);
6339 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6340 if (fmt[i] == 'e')
6341 something_changed |= eliminate_lr2ip (&XEXP (x0, i));
6342 else if (fmt[i] == 'E')
6343 for (j = 0; j < XVECLEN (x0, i); j++)
6344 something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
6346 return something_changed;
6350 /* Output a 'call' insn that is a reference in memory. */
6352 const char *
6353 output_call_mem (operands)
6354 rtx * operands;
6356 operands[0] = copy_rtx (operands[0]); /* Be ultra careful. */
6357 /* Handle calls using lr by using ip (which may be clobbered in subr anyway). */
6358 if (eliminate_lr2ip (&operands[0]))
6359 output_asm_insn ("mov%?\t%|ip, %|lr", operands);
6361 if (TARGET_INTERWORK)
6363 output_asm_insn ("ldr%?\t%|ip, %0", operands);
6364 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6365 output_asm_insn ("bx%?\t%|ip", operands);
6367 else
6369 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6370 output_asm_insn ("ldr%?\t%|pc, %0", operands);
6373 return "";
6377 /* Output a move from arm registers to an fpu registers.
6378 OPERANDS[0] is an fpu register.
6379 OPERANDS[1] is the first registers of an arm register pair. */
6381 const char *
6382 output_mov_long_double_fpu_from_arm (operands)
6383 rtx * operands;
6385 int arm_reg0 = REGNO (operands[1]);
6386 rtx ops[3];
6388 if (arm_reg0 == IP_REGNUM)
6389 abort ();
6391 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6392 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6393 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
6395 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
6396 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
6398 return "";
6401 /* Output a move from an fpu register to arm registers.
6402 OPERANDS[0] is the first registers of an arm register pair.
6403 OPERANDS[1] is an fpu register. */
6405 const char *
6406 output_mov_long_double_arm_from_fpu (operands)
6407 rtx * operands;
6409 int arm_reg0 = REGNO (operands[0]);
6410 rtx ops[3];
6412 if (arm_reg0 == IP_REGNUM)
6413 abort ();
6415 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6416 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6417 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
6419 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
6420 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
6421 return "";
6424 /* Output a move from arm registers to arm registers of a long double
6425 OPERANDS[0] is the destination.
6426 OPERANDS[1] is the source. */
6428 const char *
6429 output_mov_long_double_arm_from_arm (operands)
6430 rtx * operands;
6432 /* We have to be careful here because the two might overlap. */
6433 int dest_start = REGNO (operands[0]);
6434 int src_start = REGNO (operands[1]);
6435 rtx ops[2];
6436 int i;
6438 if (dest_start < src_start)
6440 for (i = 0; i < 3; i++)
6442 ops[0] = gen_rtx_REG (SImode, dest_start + i);
6443 ops[1] = gen_rtx_REG (SImode, src_start + i);
6444 output_asm_insn ("mov%?\t%0, %1", ops);
6447 else
6449 for (i = 2; i >= 0; i--)
6451 ops[0] = gen_rtx_REG (SImode, dest_start + i);
6452 ops[1] = gen_rtx_REG (SImode, src_start + i);
6453 output_asm_insn ("mov%?\t%0, %1", ops);
6457 return "";
6461 /* Output a move from arm registers to an fpu registers.
6462 OPERANDS[0] is an fpu register.
6463 OPERANDS[1] is the first registers of an arm register pair. */
6465 const char *
6466 output_mov_double_fpu_from_arm (operands)
6467 rtx * operands;
6469 int arm_reg0 = REGNO (operands[1]);
6470 rtx ops[2];
6472 if (arm_reg0 == IP_REGNUM)
6473 abort ();
6475 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6476 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6477 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
6478 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
6479 return "";
6482 /* Output a move from an fpu register to arm registers.
6483 OPERANDS[0] is the first registers of an arm register pair.
6484 OPERANDS[1] is an fpu register. */
6486 const char *
6487 output_mov_double_arm_from_fpu (operands)
6488 rtx * operands;
6490 int arm_reg0 = REGNO (operands[0]);
6491 rtx ops[2];
6493 if (arm_reg0 == IP_REGNUM)
6494 abort ();
6496 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6497 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6498 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
6499 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
6500 return "";
6503 /* Output a move between double words.
6504 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
6505 or MEM<-REG and all MEMs must be offsettable addresses. */
6507 const char *
6508 output_move_double (operands)
6509 rtx * operands;
6511 enum rtx_code code0 = GET_CODE (operands[0]);
6512 enum rtx_code code1 = GET_CODE (operands[1]);
6513 rtx otherops[3];
6515 if (code0 == REG)
6517 int reg0 = REGNO (operands[0]);
6519 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
6521 if (code1 == REG)
6523 int reg1 = REGNO (operands[1]);
6524 if (reg1 == IP_REGNUM)
6525 abort ();
6527 /* Ensure the second source is not overwritten. */
6528 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
6529 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
6530 else
6531 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
6533 else if (code1 == CONST_DOUBLE)
6535 if (GET_MODE (operands[1]) == DFmode)
6537 REAL_VALUE_TYPE r;
6538 long l[2];
6540 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
6541 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
6542 otherops[1] = GEN_INT (l[1]);
6543 operands[1] = GEN_INT (l[0]);
6545 else if (GET_MODE (operands[1]) != VOIDmode)
6546 abort ();
6547 else if (WORDS_BIG_ENDIAN)
6549 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
6550 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
6552 else
6554 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
6555 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
6558 output_mov_immediate (operands);
6559 output_mov_immediate (otherops);
6561 else if (code1 == CONST_INT)
6563 #if HOST_BITS_PER_WIDE_INT > 32
6564 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
6565 what the upper word is. */
6566 if (WORDS_BIG_ENDIAN)
6568 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
6569 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
6571 else
6573 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
6574 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
6576 #else
6577 /* Sign extend the intval into the high-order word. */
6578 if (WORDS_BIG_ENDIAN)
6580 otherops[1] = operands[1];
6581 operands[1] = (INTVAL (operands[1]) < 0
6582 ? constm1_rtx : const0_rtx);
6584 else
6585 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
6586 #endif
6587 output_mov_immediate (otherops);
6588 output_mov_immediate (operands);
6590 else if (code1 == MEM)
6592 switch (GET_CODE (XEXP (operands[1], 0)))
6594 case REG:
6595 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
6596 break;
6598 case PRE_INC:
6599 abort (); /* Should never happen now. */
6600 break;
6602 case PRE_DEC:
6603 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
6604 break;
6606 case POST_INC:
6607 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
6608 break;
6610 case POST_DEC:
6611 abort (); /* Should never happen now. */
6612 break;
6614 case LABEL_REF:
6615 case CONST:
6616 output_asm_insn ("adr%?\t%0, %1", operands);
6617 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
6618 break;
6620 default:
6621 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
6622 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
6624 otherops[0] = operands[0];
6625 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
6626 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
6628 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
6630 if (GET_CODE (otherops[2]) == CONST_INT)
6632 switch (INTVAL (otherops[2]))
6634 case -8:
6635 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
6636 return "";
6637 case -4:
6638 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
6639 return "";
6640 case 4:
6641 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
6642 return "";
6645 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
6646 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
6647 else
6648 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6650 else
6651 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6653 else
6654 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
6656 return "ldm%?ia\t%0, %M0";
6658 else
6660 otherops[1] = adjust_address (operands[1], VOIDmode, 4);
6661 /* Take care of overlapping base/data reg. */
6662 if (reg_mentioned_p (operands[0], operands[1]))
6664 output_asm_insn ("ldr%?\t%0, %1", otherops);
6665 output_asm_insn ("ldr%?\t%0, %1", operands);
6667 else
6669 output_asm_insn ("ldr%?\t%0, %1", operands);
6670 output_asm_insn ("ldr%?\t%0, %1", otherops);
6675 else
6676 abort (); /* Constraints should prevent this. */
6678 else if (code0 == MEM && code1 == REG)
6680 if (REGNO (operands[1]) == IP_REGNUM)
6681 abort ();
6683 switch (GET_CODE (XEXP (operands[0], 0)))
6685 case REG:
6686 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
6687 break;
6689 case PRE_INC:
6690 abort (); /* Should never happen now. */
6691 break;
6693 case PRE_DEC:
6694 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
6695 break;
6697 case POST_INC:
6698 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
6699 break;
6701 case POST_DEC:
6702 abort (); /* Should never happen now. */
6703 break;
6705 case PLUS:
6706 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
6708 switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
6710 case -8:
6711 output_asm_insn ("stm%?db\t%m0, %M1", operands);
6712 return "";
6714 case -4:
6715 output_asm_insn ("stm%?da\t%m0, %M1", operands);
6716 return "";
6718 case 4:
6719 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
6720 return "";
6723 /* Fall through */
6725 default:
6726 otherops[0] = adjust_address (operands[0], VOIDmode, 4);
6727 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
6728 output_asm_insn ("str%?\t%1, %0", operands);
6729 output_asm_insn ("str%?\t%1, %0", otherops);
6732 else
6733 /* Constraints should prevent this. */
6734 abort ();
6736 return "";
6740 /* Output an arbitrary MOV reg, #n.
6741 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
6743 const char *
6744 output_mov_immediate (operands)
6745 rtx * operands;
6747 HOST_WIDE_INT n = INTVAL (operands[1]);
6749 /* Try to use one MOV. */
6750 if (const_ok_for_arm (n))
6751 output_asm_insn ("mov%?\t%0, %1", operands);
6753 /* Try to use one MVN. */
6754 else if (const_ok_for_arm (~n))
6756 operands[1] = GEN_INT (~n);
6757 output_asm_insn ("mvn%?\t%0, %1", operands);
6759 else
6761 int n_ones = 0;
6762 int i;
6764 /* If all else fails, make it out of ORRs or BICs as appropriate. */
6765 for (i = 0; i < 32; i ++)
6766 if (n & 1 << i)
6767 n_ones ++;
6769 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
6770 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
6771 else
6772 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
6775 return "";
6778 /* Output an ADD r, s, #n where n may be too big for one instruction.
6779 If adding zero to one register, output nothing. */
6781 const char *
6782 output_add_immediate (operands)
6783 rtx * operands;
6785 HOST_WIDE_INT n = INTVAL (operands[2]);
6787 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
6789 if (n < 0)
6790 output_multi_immediate (operands,
6791 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
6792 -n);
6793 else
6794 output_multi_immediate (operands,
6795 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
6799 return "";
6802 /* Output a multiple immediate operation.
6803 OPERANDS is the vector of operands referred to in the output patterns.
6804 INSTR1 is the output pattern to use for the first constant.
6805 INSTR2 is the output pattern to use for subsequent constants.
6806 IMMED_OP is the index of the constant slot in OPERANDS.
6807 N is the constant value. */
6809 static const char *
6810 output_multi_immediate (operands, instr1, instr2, immed_op, n)
6811 rtx * operands;
6812 const char * instr1;
6813 const char * instr2;
6814 int immed_op;
6815 HOST_WIDE_INT n;
6817 #if HOST_BITS_PER_WIDE_INT > 32
6818 n &= 0xffffffff;
6819 #endif
6821 if (n == 0)
6823 /* Quick and easy output. */
6824 operands[immed_op] = const0_rtx;
6825 output_asm_insn (instr1, operands);
6827 else
6829 int i;
6830 const char * instr = instr1;
6832 /* Note that n is never zero here (which would give no output). */
6833 for (i = 0; i < 32; i += 2)
6835 if (n & (3 << i))
6837 operands[immed_op] = GEN_INT (n & (255 << i));
6838 output_asm_insn (instr, operands);
6839 instr = instr2;
6840 i += 6;
6845 return "";
6848 /* Return the appropriate ARM instruction for the operation code.
6849 The returned result should not be overwritten. OP is the rtx of the
6850 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
6851 was shifted. */
6853 const char *
6854 arithmetic_instr (op, shift_first_arg)
6855 rtx op;
6856 int shift_first_arg;
6858 switch (GET_CODE (op))
6860 case PLUS:
6861 return "add";
6863 case MINUS:
6864 return shift_first_arg ? "rsb" : "sub";
6866 case IOR:
6867 return "orr";
6869 case XOR:
6870 return "eor";
6872 case AND:
6873 return "and";
6875 default:
6876 abort ();
6880 /* Ensure valid constant shifts and return the appropriate shift mnemonic
6881 for the operation code. The returned result should not be overwritten.
6882 OP is the rtx code of the shift.
6883 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6884 shift. */
6886 static const char *
6887 shift_op (op, amountp)
6888 rtx op;
6889 HOST_WIDE_INT *amountp;
6891 const char * mnem;
6892 enum rtx_code code = GET_CODE (op);
6894 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
6895 *amountp = -1;
6896 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
6897 *amountp = INTVAL (XEXP (op, 1));
6898 else
6899 abort ();
6901 switch (code)
6903 case ASHIFT:
6904 mnem = "asl";
6905 break;
6907 case ASHIFTRT:
6908 mnem = "asr";
6909 break;
6911 case LSHIFTRT:
6912 mnem = "lsr";
6913 break;
6915 case ROTATERT:
6916 mnem = "ror";
6917 break;
6919 case MULT:
6920 /* We never have to worry about the amount being other than a
6921 power of 2, since this case can never be reloaded from a reg. */
6922 if (*amountp != -1)
6923 *amountp = int_log2 (*amountp);
6924 else
6925 abort ();
6926 return "asl";
6928 default:
6929 abort ();
6932 if (*amountp != -1)
6934 /* This is not 100% correct, but follows from the desire to merge
6935 multiplication by a power of 2 with the recognizer for a
6936 shift. >=32 is not a valid shift for "asl", so we must try and
6937 output a shift that produces the correct arithmetical result.
6938 Using lsr #32 is identical except for the fact that the carry bit
6939 is not set correctly if we set the flags; but we never use the
6940 carry bit from such an operation, so we can ignore that. */
6941 if (code == ROTATERT)
6942 /* Rotate is just modulo 32. */
6943 *amountp &= 31;
6944 else if (*amountp != (*amountp & 31))
6946 if (code == ASHIFT)
6947 mnem = "lsr";
6948 *amountp = 32;
6951 /* Shifts of 0 are no-ops. */
6952 if (*amountp == 0)
6953 return NULL;
6956 return mnem;
6959 /* Obtain the shift from the POWER of two. */
6961 static HOST_WIDE_INT
6962 int_log2 (power)
6963 HOST_WIDE_INT power;
6965 HOST_WIDE_INT shift = 0;
6967 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
6969 if (shift > 31)
6970 abort ();
6971 shift ++;
6974 return shift;
6977 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
6978 /bin/as is horribly restrictive. */
6979 #define MAX_ASCII_LEN 51
6981 void
6982 output_ascii_pseudo_op (stream, p, len)
6983 FILE * stream;
6984 const unsigned char * p;
6985 int len;
6987 int i;
6988 int len_so_far = 0;
6990 fputs ("\t.ascii\t\"", stream);
6992 for (i = 0; i < len; i++)
6994 int c = p[i];
6996 if (len_so_far >= MAX_ASCII_LEN)
6998 fputs ("\"\n\t.ascii\t\"", stream);
6999 len_so_far = 0;
7002 switch (c)
7004 case TARGET_TAB:
7005 fputs ("\\t", stream);
7006 len_so_far += 2;
7007 break;
7009 case TARGET_FF:
7010 fputs ("\\f", stream);
7011 len_so_far += 2;
7012 break;
7014 case TARGET_BS:
7015 fputs ("\\b", stream);
7016 len_so_far += 2;
7017 break;
7019 case TARGET_CR:
7020 fputs ("\\r", stream);
7021 len_so_far += 2;
7022 break;
7024 case TARGET_NEWLINE:
7025 fputs ("\\n", stream);
7026 c = p [i + 1];
7027 if ((c >= ' ' && c <= '~')
7028 || c == TARGET_TAB)
7029 /* This is a good place for a line break. */
7030 len_so_far = MAX_ASCII_LEN;
7031 else
7032 len_so_far += 2;
7033 break;
7035 case '\"':
7036 case '\\':
7037 putc ('\\', stream);
7038 len_so_far++;
7039 /* drop through. */
7041 default:
7042 if (c >= ' ' && c <= '~')
7044 putc (c, stream);
7045 len_so_far++;
7047 else
7049 fprintf (stream, "\\%03o", c);
7050 len_so_far += 4;
7052 break;
7056 fputs ("\"\n", stream);
7059 /* Compute the register sabe mask for registers 0 through 12
7060 inclusive. This code is used by both arm_compute_save_reg_mask
7061 and arm_compute_initial_elimination_offset. */
7063 static unsigned long
7064 arm_compute_save_reg0_reg12_mask ()
7066 unsigned long func_type = arm_current_func_type ();
7067 unsigned int save_reg_mask = 0;
7068 unsigned int reg;
7070 if (IS_INTERRUPT (func_type))
7072 unsigned int max_reg;
7073 /* Interrupt functions must not corrupt any registers,
7074 even call clobbered ones. If this is a leaf function
7075 we can just examine the registers used by the RTL, but
7076 otherwise we have to assume that whatever function is
7077 called might clobber anything, and so we have to save
7078 all the call-clobbered registers as well. */
7079 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
7080 /* FIQ handlers have registers r8 - r12 banked, so
7081 we only need to check r0 - r7, Normal ISRs only
7082 bank r14 and r15, so we must check up to r12.
7083 r13 is the stack pointer which is always preserved,
7084 so we do not need to consider it here. */
7085 max_reg = 7;
7086 else
7087 max_reg = 12;
7089 for (reg = 0; reg <= max_reg; reg++)
7090 if (regs_ever_live[reg]
7091 || (! current_function_is_leaf && call_used_regs [reg]))
7092 save_reg_mask |= (1 << reg);
7094 else
7096 /* In the normal case we only need to save those registers
7097 which are call saved and which are used by this function. */
7098 for (reg = 0; reg <= 10; reg++)
7099 if (regs_ever_live[reg] && ! call_used_regs [reg])
7100 save_reg_mask |= (1 << reg);
7102 /* Handle the frame pointer as a special case. */
7103 if (! TARGET_APCS_FRAME
7104 && ! frame_pointer_needed
7105 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
7106 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
7107 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
7109 /* If we aren't loading the PIC register,
7110 don't stack it even though it may be live. */
7111 if (flag_pic
7112 && ! TARGET_SINGLE_PIC_BASE
7113 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
7114 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
7117 return save_reg_mask;
7120 /* Compute a bit mask of which registers need to be
7121 saved on the stack for the current function. */
7123 static unsigned long
7124 arm_compute_save_reg_mask ()
7126 unsigned int save_reg_mask = 0;
7127 unsigned long func_type = arm_current_func_type ();
7129 if (IS_NAKED (func_type))
7130 /* This should never really happen. */
7131 return 0;
7133 /* If we are creating a stack frame, then we must save the frame pointer,
7134 IP (which will hold the old stack pointer), LR and the PC. */
7135 if (frame_pointer_needed)
7136 save_reg_mask |=
7137 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
7138 | (1 << IP_REGNUM)
7139 | (1 << LR_REGNUM)
7140 | (1 << PC_REGNUM);
7142 /* Volatile functions do not return, so there
7143 is no need to save any other registers. */
7144 if (IS_VOLATILE (func_type))
7145 return save_reg_mask;
7147 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
7149 /* Decide if we need to save the link register.
7150 Interrupt routines have their own banked link register,
7151 so they never need to save it.
7152 Otherwise if we do not use the link register we do not need to save
7153 it. If we are pushing other registers onto the stack however, we
7154 can save an instruction in the epilogue by pushing the link register
7155 now and then popping it back into the PC. This incurs extra memory
7156 accesses though, so we only do it when optimising for size, and only
7157 if we know that we will not need a fancy return sequence. */
7158 if (regs_ever_live [LR_REGNUM]
7159 || (save_reg_mask
7160 && optimize_size
7161 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
7162 save_reg_mask |= 1 << LR_REGNUM;
7164 if (cfun->machine->lr_save_eliminated)
7165 save_reg_mask &= ~ (1 << LR_REGNUM);
7167 return save_reg_mask;
7170 /* Generate a function exit sequence. If REALLY_RETURN is true, then do
7171 everything bar the final return instruction. */
7173 const char *
7174 output_return_instruction (operand, really_return, reverse)
7175 rtx operand;
7176 int really_return;
7177 int reverse;
7179 char conditional[10];
7180 char instr[100];
7181 int reg;
7182 unsigned long live_regs_mask;
7183 unsigned long func_type;
7185 func_type = arm_current_func_type ();
7187 if (IS_NAKED (func_type))
7188 return "";
7190 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
7192 /* If this function was declared non-returning, and we have found a tail
7193 call, then we have to trust that the called function won't return. */
7194 if (really_return)
7196 rtx ops[2];
7198 /* Otherwise, trap an attempted return by aborting. */
7199 ops[0] = operand;
7200 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
7201 : "abort");
7202 assemble_external_libcall (ops[1]);
7203 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
7206 return "";
7209 if (current_function_calls_alloca && !really_return)
7210 abort ();
7212 /* Construct the conditional part of the instruction(s) to be emitted. */
7213 if (operand)
7214 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
7215 else
7216 * conditional = 0;
7218 return_used_this_function = 1;
7220 live_regs_mask = arm_compute_save_reg_mask ();
7222 if (live_regs_mask)
7224 const char * return_reg;
7226 /* If we do not have any special requirements for function exit
7227 (eg interworking, or ISR) then we can load the return address
7228 directly into the PC. Otherwise we must load it into LR. */
7229 if (really_return
7230 && ! TARGET_INTERWORK)
7231 return_reg = reg_names[PC_REGNUM];
7232 else
7233 return_reg = reg_names[LR_REGNUM];
7235 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
7236 /* There are two possible reasons for the IP register being saved.
7237 Either a stack frame was created, in which case IP contains the
7238 old stack pointer, or an ISR routine corrupted it. If this in an
7239 ISR routine then just restore IP, otherwise restore IP into SP. */
7240 if (! IS_INTERRUPT (func_type))
7242 live_regs_mask &= ~ (1 << IP_REGNUM);
7243 live_regs_mask |= (1 << SP_REGNUM);
7246 /* On some ARM architectures it is faster to use LDR rather than
7247 LDM to load a single register. On other architectures, the
7248 cost is the same. In 26 bit mode, or for exception handlers,
7249 we have to use LDM to load the PC so that the CPSR is also
7250 restored. */
7251 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
7253 if (live_regs_mask == (unsigned int)(1 << reg))
7254 break;
7256 if (reg <= LAST_ARM_REGNUM
7257 && (reg != LR_REGNUM
7258 || ! really_return
7259 || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
7261 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
7262 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
7264 else
7266 char *p;
7267 int first = 1;
7269 /* Generate the load multiple instruction to restore the registers. */
7270 if (frame_pointer_needed)
7271 sprintf (instr, "ldm%sea\t%%|fp, {", conditional);
7272 else
7273 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
7275 p = instr + strlen (instr);
7277 for (reg = 0; reg <= SP_REGNUM; reg++)
7278 if (live_regs_mask & (1 << reg))
7280 int l = strlen (reg_names[reg]);
7282 if (first)
7283 first = 0;
7284 else
7286 memcpy (p, ", ", 2);
7287 p += 2;
7290 memcpy (p, "%|", 2);
7291 memcpy (p + 2, reg_names[reg], l);
7292 p += l + 2;
7295 if (live_regs_mask & (1 << LR_REGNUM))
7297 int l = strlen (return_reg);
7299 if (! first)
7301 memcpy (p, ", ", 2);
7302 p += 2;
7305 memcpy (p, "%|", 2);
7306 memcpy (p + 2, return_reg, l);
7307 strcpy (p + 2 + l, ((TARGET_APCS_32
7308 && !IS_INTERRUPT (func_type))
7309 || !really_return)
7310 ? "}" : "}^");
7312 else
7313 strcpy (p, "}");
7316 output_asm_insn (instr, & operand);
7318 /* See if we need to generate an extra instruction to
7319 perform the actual function return. */
7320 if (really_return
7321 && func_type != ARM_FT_INTERWORKED
7322 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
7324 /* The return has already been handled
7325 by loading the LR into the PC. */
7326 really_return = 0;
7330 if (really_return)
7332 switch ((int) ARM_FUNC_TYPE (func_type))
7334 case ARM_FT_ISR:
7335 case ARM_FT_FIQ:
7336 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
7337 break;
7339 case ARM_FT_INTERWORKED:
7340 sprintf (instr, "bx%s\t%%|lr", conditional);
7341 break;
7343 case ARM_FT_EXCEPTION:
7344 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
7345 break;
7347 default:
7348 /* ARMv5 implementations always provide BX, so interworking
7349 is the default unless APCS-26 is in use. */
7350 if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
7351 sprintf (instr, "bx%s\t%%|lr", conditional);
7352 else
7353 sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
7354 conditional, TARGET_APCS_32 ? "" : "s");
7355 break;
7358 output_asm_insn (instr, & operand);
7361 return "";
7364 /* Write the function name into the code section, directly preceding
7365 the function prologue.
7367 Code will be output similar to this:
7369 .ascii "arm_poke_function_name", 0
7370 .align
7372 .word 0xff000000 + (t1 - t0)
7373 arm_poke_function_name
7374 mov ip, sp
7375 stmfd sp!, {fp, ip, lr, pc}
7376 sub fp, ip, #4
7378 When performing a stack backtrace, code can inspect the value
7379 of 'pc' stored at 'fp' + 0. If the trace function then looks
7380 at location pc - 12 and the top 8 bits are set, then we know
7381 that there is a function name embedded immediately preceding this
7382 location and has length ((pc[-3]) & 0xff000000).
7384 We assume that pc is declared as a pointer to an unsigned long.
7386 It is of no benefit to output the function name if we are assembling
7387 a leaf function. These function types will not contain a stack
7388 backtrace structure, therefore it is not possible to determine the
7389 function name. */
7391 void
7392 arm_poke_function_name (stream, name)
7393 FILE * stream;
7394 const char * name;
7396 unsigned long alignlength;
7397 unsigned long length;
7398 rtx x;
7400 length = strlen (name) + 1;
7401 alignlength = ROUND_UP (length);
7403 ASM_OUTPUT_ASCII (stream, name, length);
7404 ASM_OUTPUT_ALIGN (stream, 2);
7405 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
7406 assemble_aligned_integer (UNITS_PER_WORD, x);
7409 /* Place some comments into the assembler stream
7410 describing the current function. */
7412 static void
7413 arm_output_function_prologue (f, frame_size)
7414 FILE * f;
7415 HOST_WIDE_INT frame_size;
7417 unsigned long func_type;
7419 if (!TARGET_ARM)
7421 thumb_output_function_prologue (f, frame_size);
7422 return;
7425 /* Sanity check. */
7426 if (arm_ccfsm_state || arm_target_insn)
7427 abort ();
7429 func_type = arm_current_func_type ();
7431 switch ((int) ARM_FUNC_TYPE (func_type))
7433 default:
7434 case ARM_FT_NORMAL:
7435 break;
7436 case ARM_FT_INTERWORKED:
7437 asm_fprintf (f, "\t%@ Function supports interworking.\n");
7438 break;
7439 case ARM_FT_EXCEPTION_HANDLER:
7440 asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
7441 break;
7442 case ARM_FT_ISR:
7443 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
7444 break;
7445 case ARM_FT_FIQ:
7446 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
7447 break;
7448 case ARM_FT_EXCEPTION:
7449 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
7450 break;
7453 if (IS_NAKED (func_type))
7454 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
7456 if (IS_VOLATILE (func_type))
7457 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
7459 if (IS_NESTED (func_type))
7460 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
7462 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
7463 current_function_args_size,
7464 current_function_pretend_args_size, frame_size);
7466 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
7467 frame_pointer_needed,
7468 cfun->machine->uses_anonymous_args);
7470 if (cfun->machine->lr_save_eliminated)
7471 asm_fprintf (f, "\t%@ link register save eliminated.\n");
7473 #ifdef AOF_ASSEMBLER
7474 if (flag_pic)
7475 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
7476 #endif
7478 return_used_this_function = 0;
7481 const char *
7482 arm_output_epilogue (really_return)
7483 int really_return;
7485 int reg;
7486 unsigned long saved_regs_mask;
7487 unsigned long func_type;
7488 /* Floats_offset is the offset from the "virtual" frame. In an APCS
7489 frame that is $fp + 4 for a non-variadic function. */
7490 int floats_offset = 0;
7491 rtx operands[3];
7492 int frame_size = get_frame_size ();
7493 FILE * f = asm_out_file;
7494 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
7496 /* If we have already generated the return instruction
7497 then it is futile to generate anything else. */
7498 if (use_return_insn (FALSE) && return_used_this_function)
7499 return "";
7501 func_type = arm_current_func_type ();
7503 if (IS_NAKED (func_type))
7504 /* Naked functions don't have epilogues. */
7505 return "";
7507 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
7509 rtx op;
7511 /* A volatile function should never return. Call abort. */
7512 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
7513 assemble_external_libcall (op);
7514 output_asm_insn ("bl\t%a0", &op);
7516 return "";
7519 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
7520 && ! really_return)
7521 /* If we are throwing an exception, then we really must
7522 be doing a return, so we can't tail-call. */
7523 abort ();
7525 saved_regs_mask = arm_compute_save_reg_mask ();
7527 /* XXX We should adjust floats_offset for any anonymous args, and then
7528 re-adjust vfp_offset below to compensate. */
7530 /* Compute how far away the floats will be. */
7531 for (reg = 0; reg <= LAST_ARM_REGNUM; reg ++)
7532 if (saved_regs_mask & (1 << reg))
7533 floats_offset += 4;
7535 if (frame_pointer_needed)
7537 int vfp_offset = 4;
7539 if (arm_fpu_arch == FP_SOFT2)
7541 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
7542 if (regs_ever_live[reg] && !call_used_regs[reg])
7544 floats_offset += 12;
7545 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
7546 reg, FP_REGNUM, floats_offset - vfp_offset);
7549 else
7551 int start_reg = LAST_ARM_FP_REGNUM;
7553 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
7555 if (regs_ever_live[reg] && !call_used_regs[reg])
7557 floats_offset += 12;
7559 /* We can't unstack more than four registers at once. */
7560 if (start_reg - reg == 3)
7562 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
7563 reg, FP_REGNUM, floats_offset - vfp_offset);
7564 start_reg = reg - 1;
7567 else
7569 if (reg != start_reg)
7570 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7571 reg + 1, start_reg - reg,
7572 FP_REGNUM, floats_offset - vfp_offset);
7573 start_reg = reg - 1;
7577 /* Just in case the last register checked also needs unstacking. */
7578 if (reg != start_reg)
7579 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7580 reg + 1, start_reg - reg,
7581 FP_REGNUM, floats_offset - vfp_offset);
7584 /* saved_regs_mask should contain the IP, which at the time of stack
7585 frame generation actually contains the old stack pointer. So a
7586 quick way to unwind the stack is just pop the IP register directly
7587 into the stack pointer. */
7588 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
7589 abort ();
7590 saved_regs_mask &= ~ (1 << IP_REGNUM);
7591 saved_regs_mask |= (1 << SP_REGNUM);
7593 /* There are two registers left in saved_regs_mask - LR and PC. We
7594 only need to restore the LR register (the return address), but to
7595 save time we can load it directly into the PC, unless we need a
7596 special function exit sequence, or we are not really returning. */
7597 if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
7598 /* Delete the LR from the register mask, so that the LR on
7599 the stack is loaded into the PC in the register mask. */
7600 saved_regs_mask &= ~ (1 << LR_REGNUM);
7601 else
7602 saved_regs_mask &= ~ (1 << PC_REGNUM);
7604 print_multi_reg (f, "ldmea\t%r", FP_REGNUM, saved_regs_mask);
7606 if (IS_INTERRUPT (func_type))
7607 /* Interrupt handlers will have pushed the
7608 IP onto the stack, so restore it now. */
7609 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, 1 << IP_REGNUM);
7611 else
7613 /* Restore stack pointer if necessary. */
7614 if (frame_size + current_function_outgoing_args_size != 0)
7616 operands[0] = operands[1] = stack_pointer_rtx;
7617 operands[2] = GEN_INT (frame_size
7618 + current_function_outgoing_args_size);
7619 output_add_immediate (operands);
7622 if (arm_fpu_arch == FP_SOFT2)
7624 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7625 if (regs_ever_live[reg] && !call_used_regs[reg])
7626 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
7627 reg, SP_REGNUM);
7629 else
7631 int start_reg = FIRST_ARM_FP_REGNUM;
7633 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7635 if (regs_ever_live[reg] && !call_used_regs[reg])
7637 if (reg - start_reg == 3)
7639 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
7640 start_reg, SP_REGNUM);
7641 start_reg = reg + 1;
7644 else
7646 if (reg != start_reg)
7647 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7648 start_reg, reg - start_reg,
7649 SP_REGNUM);
7651 start_reg = reg + 1;
7655 /* Just in case the last register checked also needs unstacking. */
7656 if (reg != start_reg)
7657 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7658 start_reg, reg - start_reg, SP_REGNUM);
7661 /* If we can, restore the LR into the PC. */
7662 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
7663 && really_return
7664 && current_function_pretend_args_size == 0
7665 && saved_regs_mask & (1 << LR_REGNUM))
7667 saved_regs_mask &= ~ (1 << LR_REGNUM);
7668 saved_regs_mask |= (1 << PC_REGNUM);
7671 /* Load the registers off the stack. If we only have one register
7672 to load use the LDR instruction - it is faster. */
7673 if (saved_regs_mask == (1 << LR_REGNUM))
7675 /* The exception handler ignores the LR, so we do
7676 not really need to load it off the stack. */
7677 if (eh_ofs)
7678 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
7679 else
7680 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
7682 else if (saved_regs_mask)
7683 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
7685 if (current_function_pretend_args_size)
7687 /* Unwind the pre-pushed regs. */
7688 operands[0] = operands[1] = stack_pointer_rtx;
7689 operands[2] = GEN_INT (current_function_pretend_args_size);
7690 output_add_immediate (operands);
7694 #if 0
7695 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER)
7696 /* Adjust the stack to remove the exception handler stuff. */
7697 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7698 REGNO (eh_ofs));
7699 #endif
7701 if (! really_return
7702 || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
7703 && current_function_pretend_args_size == 0
7704 && saved_regs_mask & (1 << PC_REGNUM)))
7705 return "";
7707 /* Generate the return instruction. */
7708 switch ((int) ARM_FUNC_TYPE (func_type))
7710 case ARM_FT_EXCEPTION_HANDLER:
7711 /* Even in 26-bit mode we do a mov (rather than a movs)
7712 because we don't have the PSR bits set in the address. */
7713 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
7714 break;
7716 case ARM_FT_ISR:
7717 case ARM_FT_FIQ:
7718 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
7719 break;
7721 case ARM_FT_EXCEPTION:
7722 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7723 break;
7725 case ARM_FT_INTERWORKED:
7726 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
7727 break;
7729 default:
7730 if (frame_pointer_needed)
7731 /* If we used the frame pointer then the return adddress
7732 will have been loaded off the stack directly into the
7733 PC, so there is no need to issue a MOV instruction
7734 here. */
7736 else if (current_function_pretend_args_size == 0
7737 && (saved_regs_mask & (1 << LR_REGNUM)))
7738 /* Similarly we may have been able to load LR into the PC
7739 even if we did not create a stack frame. */
7741 else if (TARGET_APCS_32)
7742 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7743 else
7744 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7745 break;
7748 return "";
7751 static void
7752 arm_output_function_epilogue (file, frame_size)
7753 FILE *file ATTRIBUTE_UNUSED;
7754 HOST_WIDE_INT frame_size;
7756 if (TARGET_THUMB)
7758 /* ??? Probably not safe to set this here, since it assumes that a
7759 function will be emitted as assembly immediately after we generate
7760 RTL for it. This does not happen for inline functions. */
7761 return_used_this_function = 0;
7763 else
7765 if (use_return_insn (FALSE)
7766 && return_used_this_function
7767 && (frame_size + current_function_outgoing_args_size) != 0
7768 && !frame_pointer_needed)
7769 abort ();
7771 /* Reset the ARM-specific per-function variables. */
7772 after_arm_reorg = 0;
7776 /* Generate and emit an insn that we will recognize as a push_multi.
7777 Unfortunately, since this insn does not reflect very well the actual
7778 semantics of the operation, we need to annotate the insn for the benefit
7779 of DWARF2 frame unwind information. */
7781 static rtx
7782 emit_multi_reg_push (mask)
7783 int mask;
7785 int num_regs = 0;
7786 int num_dwarf_regs;
7787 int i, j;
7788 rtx par;
7789 rtx dwarf;
7790 int dwarf_par_index;
7791 rtx tmp, reg;
7793 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7794 if (mask & (1 << i))
7795 num_regs++;
7797 if (num_regs == 0 || num_regs > 16)
7798 abort ();
7800 /* We don't record the PC in the dwarf frame information. */
7801 num_dwarf_regs = num_regs;
7802 if (mask & (1 << PC_REGNUM))
7803 num_dwarf_regs--;
7805 /* For the body of the insn we are going to generate an UNSPEC in
7806 parallel with several USEs. This allows the insn to be recognised
7807 by the push_multi pattern in the arm.md file. The insn looks
7808 something like this:
7810 (parallel [
7811 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
7812 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
7813 (use (reg:SI 11 fp))
7814 (use (reg:SI 12 ip))
7815 (use (reg:SI 14 lr))
7816 (use (reg:SI 15 pc))
7819 For the frame note however, we try to be more explicit and actually
7820 show each register being stored into the stack frame, plus a (single)
7821 decrement of the stack pointer. We do it this way in order to be
7822 friendly to the stack unwinding code, which only wants to see a single
7823 stack decrement per instruction. The RTL we generate for the note looks
7824 something like this:
7826 (sequence [
7827 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
7828 (set (mem:SI (reg:SI sp)) (reg:SI r4))
7829 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
7830 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
7831 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
7834 This sequence is used both by the code to support stack unwinding for
7835 exceptions handlers and the code to generate dwarf2 frame debugging. */
7837 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
7838 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
7839 RTX_FRAME_RELATED_P (dwarf) = 1;
7840 dwarf_par_index = 1;
7842 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7844 if (mask & (1 << i))
7846 reg = gen_rtx_REG (SImode, i);
7848 XVECEXP (par, 0, 0)
7849 = gen_rtx_SET (VOIDmode,
7850 gen_rtx_MEM (BLKmode,
7851 gen_rtx_PRE_DEC (BLKmode,
7852 stack_pointer_rtx)),
7853 gen_rtx_UNSPEC (BLKmode,
7854 gen_rtvec (1, reg),
7855 UNSPEC_PUSH_MULT));
7857 if (i != PC_REGNUM)
7859 tmp = gen_rtx_SET (VOIDmode,
7860 gen_rtx_MEM (SImode, stack_pointer_rtx),
7861 reg);
7862 RTX_FRAME_RELATED_P (tmp) = 1;
7863 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
7864 dwarf_par_index++;
7867 break;
7871 for (j = 1, i++; j < num_regs; i++)
7873 if (mask & (1 << i))
7875 reg = gen_rtx_REG (SImode, i);
7877 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
7879 if (i != PC_REGNUM)
7881 tmp = gen_rtx_SET (VOIDmode,
7882 gen_rtx_MEM (SImode,
7883 plus_constant (stack_pointer_rtx,
7884 4 * j)),
7885 reg);
7886 RTX_FRAME_RELATED_P (tmp) = 1;
7887 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
7890 j++;
7894 par = emit_insn (par);
7896 tmp = gen_rtx_SET (SImode,
7897 stack_pointer_rtx,
7898 gen_rtx_PLUS (SImode,
7899 stack_pointer_rtx,
7900 GEN_INT (-4 * num_regs)));
7901 RTX_FRAME_RELATED_P (tmp) = 1;
7902 XVECEXP (dwarf, 0, 0) = tmp;
7904 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7905 REG_NOTES (par));
7906 return par;
7909 static rtx
7910 emit_sfm (base_reg, count)
7911 int base_reg;
7912 int count;
7914 rtx par;
7915 rtx dwarf;
7916 rtx tmp, reg;
7917 int i;
7919 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7920 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7921 RTX_FRAME_RELATED_P (dwarf) = 1;
7923 reg = gen_rtx_REG (XFmode, base_reg++);
7925 XVECEXP (par, 0, 0)
7926 = gen_rtx_SET (VOIDmode,
7927 gen_rtx_MEM (BLKmode,
7928 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7929 gen_rtx_UNSPEC (BLKmode,
7930 gen_rtvec (1, reg),
7931 UNSPEC_PUSH_MULT));
7933 = gen_rtx_SET (VOIDmode,
7934 gen_rtx_MEM (XFmode,
7935 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7936 reg);
7937 RTX_FRAME_RELATED_P (tmp) = 1;
7938 XVECEXP (dwarf, 0, count - 1) = tmp;
7940 for (i = 1; i < count; i++)
7942 reg = gen_rtx_REG (XFmode, base_reg++);
7943 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7945 tmp = gen_rtx_SET (VOIDmode,
7946 gen_rtx_MEM (XFmode,
7947 gen_rtx_PRE_DEC (BLKmode,
7948 stack_pointer_rtx)),
7949 reg);
7950 RTX_FRAME_RELATED_P (tmp) = 1;
7951 XVECEXP (dwarf, 0, count - i - 1) = tmp;
7954 par = emit_insn (par);
7955 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7956 REG_NOTES (par));
7957 return par;
7960 /* Compute the distance from register FROM to register TO.
7961 These can be the arg pointer (26), the soft frame pointer (25),
7962 the stack pointer (13) or the hard frame pointer (11).
7963 Typical stack layout looks like this:
7965 old stack pointer -> | |
7966 ----
7967 | | \
7968 | | saved arguments for
7969 | | vararg functions
7970 | | /
7972 hard FP & arg pointer -> | | \
7973 | | stack
7974 | | frame
7975 | | /
7977 | | \
7978 | | call saved
7979 | | registers
7980 soft frame pointer -> | | /
7982 | | \
7983 | | local
7984 | | variables
7985 | | /
7987 | | \
7988 | | outgoing
7989 | | arguments
7990 current stack pointer -> | | /
7993 For a given funciton some or all of these stack compomnents
7994 may not be needed, giving rise to the possibility of
7995 eliminating some of the registers.
7997 The values returned by this function must reflect the behaviour
7998 of arm_expand_prologue() and arm_compute_save_reg_mask().
8000 The sign of the number returned reflects the direction of stack
8001 growth, so the values are positive for all eliminations except
8002 from the soft frame pointer to the hard frame pointer. */
8004 unsigned int
8005 arm_compute_initial_elimination_offset (from, to)
8006 unsigned int from;
8007 unsigned int to;
8009 unsigned int local_vars = (get_frame_size () + 3) & ~3;
8010 unsigned int outgoing_args = current_function_outgoing_args_size;
8011 unsigned int stack_frame;
8012 unsigned int call_saved_registers;
8013 unsigned long func_type;
8015 func_type = arm_current_func_type ();
8017 /* Volatile functions never return, so there is
8018 no need to save call saved registers. */
8019 call_saved_registers = 0;
8020 if (! IS_VOLATILE (func_type))
8022 unsigned int reg_mask;
8023 unsigned int reg;
8025 /* Make sure that we compute which registers will be saved
8026 on the stack using the same algorithm that is used by
8027 arm_compute_save_reg_mask(). */
8028 reg_mask = arm_compute_save_reg0_reg12_mask ();
8030 /* Now count the number of bits set in save_reg_mask.
8031 For each set bit we need 4 bytes of stack space. */
8032 while (reg_mask)
8034 call_saved_registers += 4;
8035 reg_mask = reg_mask & ~ (reg_mask & - reg_mask);
8038 if (regs_ever_live[LR_REGNUM]
8039 /* If a stack frame is going to be created, the LR will
8040 be saved as part of that, so we do not need to allow
8041 for it here. */
8042 && ! frame_pointer_needed)
8043 call_saved_registers += 4;
8045 /* If the hard floating point registers are going to be
8046 used then they must be saved on the stack as well.
8047 Each register occupies 12 bytes of stack space. */
8048 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg ++)
8049 if (regs_ever_live[reg] && ! call_used_regs[reg])
8050 call_saved_registers += 12;
8053 /* The stack frame contains 4 registers - the old frame pointer,
8054 the old stack pointer, the return address and PC of the start
8055 of the function. */
8056 stack_frame = frame_pointer_needed ? 16 : 0;
8058 /* OK, now we have enough information to compute the distances.
8059 There must be an entry in these switch tables for each pair
8060 of registers in ELIMINABLE_REGS, even if some of the entries
8061 seem to be redundant or useless. */
8062 switch (from)
8064 case ARG_POINTER_REGNUM:
8065 switch (to)
8067 case THUMB_HARD_FRAME_POINTER_REGNUM:
8068 return 0;
8070 case FRAME_POINTER_REGNUM:
8071 /* This is the reverse of the soft frame pointer
8072 to hard frame pointer elimination below. */
8073 if (call_saved_registers == 0 && stack_frame == 0)
8074 return 0;
8075 return (call_saved_registers + stack_frame - 4);
8077 case ARM_HARD_FRAME_POINTER_REGNUM:
8078 /* If there is no stack frame then the hard
8079 frame pointer and the arg pointer coincide. */
8080 if (stack_frame == 0 && call_saved_registers != 0)
8081 return 0;
8082 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
8083 return (frame_pointer_needed
8084 && current_function_needs_context
8085 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
8087 case STACK_POINTER_REGNUM:
8088 /* If nothing has been pushed on the stack at all
8089 then this will return -4. This *is* correct! */
8090 return call_saved_registers + stack_frame + local_vars + outgoing_args - 4;
8092 default:
8093 abort ();
8095 break;
8097 case FRAME_POINTER_REGNUM:
8098 switch (to)
8100 case THUMB_HARD_FRAME_POINTER_REGNUM:
8101 return 0;
8103 case ARM_HARD_FRAME_POINTER_REGNUM:
8104 /* The hard frame pointer points to the top entry in the
8105 stack frame. The soft frame pointer to the bottom entry
8106 in the stack frame. If there is no stack frame at all,
8107 then they are identical. */
8108 if (call_saved_registers == 0 && stack_frame == 0)
8109 return 0;
8110 return - (call_saved_registers + stack_frame - 4);
8112 case STACK_POINTER_REGNUM:
8113 return local_vars + outgoing_args;
8115 default:
8116 abort ();
8118 break;
8120 default:
8121 /* You cannot eliminate from the stack pointer.
8122 In theory you could eliminate from the hard frame
8123 pointer to the stack pointer, but this will never
8124 happen, since if a stack frame is not needed the
8125 hard frame pointer will never be used. */
8126 abort ();
8130 /* Generate the prologue instructions for entry into an ARM function. */
8132 void
8133 arm_expand_prologue ()
8135 int reg;
8136 rtx amount;
8137 rtx insn;
8138 rtx ip_rtx;
8139 unsigned long live_regs_mask;
8140 unsigned long func_type;
8141 int fp_offset = 0;
8142 int saved_pretend_args = 0;
8143 unsigned int args_to_push;
8145 func_type = arm_current_func_type ();
8147 /* Naked functions don't have prologues. */
8148 if (IS_NAKED (func_type))
8149 return;
8151 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
8152 args_to_push = current_function_pretend_args_size;
8154 /* Compute which register we will have to save onto the stack. */
8155 live_regs_mask = arm_compute_save_reg_mask ();
8157 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
8159 if (frame_pointer_needed)
8161 if (IS_INTERRUPT (func_type))
8163 /* Interrupt functions must not corrupt any registers.
8164 Creating a frame pointer however, corrupts the IP
8165 register, so we must push it first. */
8166 insn = emit_multi_reg_push (1 << IP_REGNUM);
8168 /* Do not set RTX_FRAME_RELATED_P on this insn.
8169 The dwarf stack unwinding code only wants to see one
8170 stack decrement per function, and this is not it. If
8171 this instruction is labeled as being part of the frame
8172 creation sequence then dwarf2out_frame_debug_expr will
8173 abort when it encounters the assignment of IP to FP
8174 later on, since the use of SP here establishes SP as
8175 the CFA register and not IP.
8177 Anyway this instruction is not really part of the stack
8178 frame creation although it is part of the prologue. */
8180 else if (IS_NESTED (func_type))
8182 /* The Static chain register is the same as the IP register
8183 used as a scratch register during stack frame creation.
8184 To get around this need to find somewhere to store IP
8185 whilst the frame is being created. We try the following
8186 places in order:
8188 1. The last argument register.
8189 2. A slot on the stack above the frame. (This only
8190 works if the function is not a varargs function).
8191 3. Register r3, after pushing the argument registers
8192 onto the stack.
8194 Note - we only need to tell the dwarf2 backend about the SP
8195 adjustment in the second variant; the static chain register
8196 doesn't need to be unwound, as it doesn't contain a value
8197 inherited from the caller. */
8199 if (regs_ever_live[3] == 0)
8201 insn = gen_rtx_REG (SImode, 3);
8202 insn = gen_rtx_SET (SImode, insn, ip_rtx);
8203 insn = emit_insn (insn);
8205 else if (args_to_push == 0)
8207 rtx dwarf;
8208 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
8209 insn = gen_rtx_MEM (SImode, insn);
8210 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
8211 insn = emit_insn (insn);
8213 fp_offset = 4;
8215 /* Just tell the dwarf backend that we adjusted SP. */
8216 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8217 gen_rtx_PLUS (SImode, stack_pointer_rtx,
8218 GEN_INT (-fp_offset)));
8219 RTX_FRAME_RELATED_P (insn) = 1;
8220 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8221 dwarf, REG_NOTES (insn));
8223 else
8225 /* Store the args on the stack. */
8226 if (cfun->machine->uses_anonymous_args)
8227 insn = emit_multi_reg_push
8228 ((0xf0 >> (args_to_push / 4)) & 0xf);
8229 else
8230 insn = emit_insn
8231 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8232 GEN_INT (- args_to_push)));
8234 RTX_FRAME_RELATED_P (insn) = 1;
8236 saved_pretend_args = 1;
8237 fp_offset = args_to_push;
8238 args_to_push = 0;
8240 /* Now reuse r3 to preserve IP. */
8241 insn = gen_rtx_REG (SImode, 3);
8242 insn = gen_rtx_SET (SImode, insn, ip_rtx);
8243 (void) emit_insn (insn);
8247 if (fp_offset)
8249 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
8250 insn = gen_rtx_SET (SImode, ip_rtx, insn);
8252 else
8253 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
8255 insn = emit_insn (insn);
8256 RTX_FRAME_RELATED_P (insn) = 1;
8259 if (args_to_push)
8261 /* Push the argument registers, or reserve space for them. */
8262 if (cfun->machine->uses_anonymous_args)
8263 insn = emit_multi_reg_push
8264 ((0xf0 >> (args_to_push / 4)) & 0xf);
8265 else
8266 insn = emit_insn
8267 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8268 GEN_INT (- args_to_push)));
8269 RTX_FRAME_RELATED_P (insn) = 1;
8272 /* If this is an interrupt service routine, and the link register is
8273 going to be pushed, subtracting four now will mean that the
8274 function return can be done with a single instruction. */
8275 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
8276 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8278 emit_insn (gen_rtx_SET (SImode,
8279 gen_rtx_REG (SImode, LR_REGNUM),
8280 gen_rtx_PLUS (SImode,
8281 gen_rtx_REG (SImode, LR_REGNUM),
8282 GEN_INT (-4))));
8285 if (live_regs_mask)
8287 insn = emit_multi_reg_push (live_regs_mask);
8288 RTX_FRAME_RELATED_P (insn) = 1;
8291 if (! IS_VOLATILE (func_type))
8293 /* Save any floating point call-saved registers used by this function. */
8294 if (arm_fpu_arch == FP_SOFT2)
8296 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
8297 if (regs_ever_live[reg] && !call_used_regs[reg])
8299 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
8300 insn = gen_rtx_MEM (XFmode, insn);
8301 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
8302 gen_rtx_REG (XFmode, reg)));
8303 RTX_FRAME_RELATED_P (insn) = 1;
8306 else
8308 int start_reg = LAST_ARM_FP_REGNUM;
8310 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
8312 if (regs_ever_live[reg] && !call_used_regs[reg])
8314 if (start_reg - reg == 3)
8316 insn = emit_sfm (reg, 4);
8317 RTX_FRAME_RELATED_P (insn) = 1;
8318 start_reg = reg - 1;
8321 else
8323 if (start_reg != reg)
8325 insn = emit_sfm (reg + 1, start_reg - reg);
8326 RTX_FRAME_RELATED_P (insn) = 1;
8328 start_reg = reg - 1;
8332 if (start_reg != reg)
8334 insn = emit_sfm (reg + 1, start_reg - reg);
8335 RTX_FRAME_RELATED_P (insn) = 1;
8340 if (frame_pointer_needed)
8342 /* Create the new frame pointer. */
8343 insn = GEN_INT (-(4 + args_to_push + fp_offset));
8344 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
8345 RTX_FRAME_RELATED_P (insn) = 1;
8347 if (IS_NESTED (func_type))
8349 /* Recover the static chain register. */
8350 if (regs_ever_live [3] == 0
8351 || saved_pretend_args)
8352 insn = gen_rtx_REG (SImode, 3);
8353 else /* if (current_function_pretend_args_size == 0) */
8355 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx, GEN_INT (4));
8356 insn = gen_rtx_MEM (SImode, insn);
8359 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
8360 /* Add a USE to stop propagate_one_insn() from barfing. */
8361 emit_insn (gen_prologue_use (ip_rtx));
8365 amount = GEN_INT (-(get_frame_size ()
8366 + current_function_outgoing_args_size));
8368 if (amount != const0_rtx)
8370 /* This add can produce multiple insns for a large constant, so we
8371 need to get tricky. */
8372 rtx last = get_last_insn ();
8373 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8374 amount));
8377 last = last ? NEXT_INSN (last) : get_insns ();
8378 RTX_FRAME_RELATED_P (last) = 1;
8380 while (last != insn);
8382 /* If the frame pointer is needed, emit a special barrier that
8383 will prevent the scheduler from moving stores to the frame
8384 before the stack adjustment. */
8385 if (frame_pointer_needed)
8387 rtx unspec = gen_rtx_UNSPEC (SImode,
8388 gen_rtvec (2, stack_pointer_rtx,
8389 hard_frame_pointer_rtx),
8390 UNSPEC_PRLG_STK);
8392 insn = emit_insn (gen_rtx_CLOBBER (VOIDmode,
8393 gen_rtx_MEM (BLKmode, unspec)));
8397 /* If we are profiling, make sure no instructions are scheduled before
8398 the call to mcount. Similarly if the user has requested no
8399 scheduling in the prolog. */
8400 if (current_function_profile || TARGET_NO_SCHED_PRO)
8401 emit_insn (gen_blockage ());
8403 /* If the link register is being kept alive, with the return address in it,
8404 then make sure that it does not get reused by the ce2 pass. */
8405 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
8407 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
8408 cfun->machine->lr_save_eliminated = 1;
8412 /* If CODE is 'd', then the X is a condition operand and the instruction
8413 should only be executed if the condition is true.
8414 if CODE is 'D', then the X is a condition operand and the instruction
8415 should only be executed if the condition is false: however, if the mode
8416 of the comparison is CCFPEmode, then always execute the instruction -- we
8417 do this because in these circumstances !GE does not necessarily imply LT;
8418 in these cases the instruction pattern will take care to make sure that
8419 an instruction containing %d will follow, thereby undoing the effects of
8420 doing this instruction unconditionally.
8421 If CODE is 'N' then X is a floating point operand that must be negated
8422 before output.
8423 If CODE is 'B' then output a bitwise inverted value of X (a const int).
8424 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
8426 void
8427 arm_print_operand (stream, x, code)
8428 FILE * stream;
8429 rtx x;
8430 int code;
8432 switch (code)
8434 case '@':
8435 fputs (ASM_COMMENT_START, stream);
8436 return;
8438 case '_':
8439 fputs (user_label_prefix, stream);
8440 return;
8442 case '|':
8443 fputs (REGISTER_PREFIX, stream);
8444 return;
8446 case '?':
8447 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
8449 if (TARGET_THUMB || current_insn_predicate != NULL)
8450 abort ();
8452 fputs (arm_condition_codes[arm_current_cc], stream);
8454 else if (current_insn_predicate)
8456 enum arm_cond_code code;
8458 if (TARGET_THUMB)
8459 abort ();
8461 code = get_arm_condition_code (current_insn_predicate);
8462 fputs (arm_condition_codes[code], stream);
8464 return;
8466 case 'N':
8468 REAL_VALUE_TYPE r;
8469 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8470 r = REAL_VALUE_NEGATE (r);
8471 fprintf (stream, "%s", fp_const_from_val (&r));
8473 return;
8475 case 'B':
8476 if (GET_CODE (x) == CONST_INT)
8478 HOST_WIDE_INT val;
8479 val = ARM_SIGN_EXTEND (~INTVAL (x));
8480 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
8482 else
8484 putc ('~', stream);
8485 output_addr_const (stream, x);
8487 return;
8489 case 'i':
8490 fprintf (stream, "%s", arithmetic_instr (x, 1));
8491 return;
8493 case 'I':
8494 fprintf (stream, "%s", arithmetic_instr (x, 0));
8495 return;
8497 case 'S':
8499 HOST_WIDE_INT val;
8500 const char * shift = shift_op (x, &val);
8502 if (shift)
8504 fprintf (stream, ", %s ", shift_op (x, &val));
8505 if (val == -1)
8506 arm_print_operand (stream, XEXP (x, 1), 0);
8507 else
8509 fputc ('#', stream);
8510 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
8514 return;
8516 /* An explanation of the 'Q', 'R' and 'H' register operands:
8518 In a pair of registers containing a DI or DF value the 'Q'
8519 operand returns the register number of the register containing
8520 the least signficant part of the value. The 'R' operand returns
8521 the register number of the register containing the most
8522 significant part of the value.
8524 The 'H' operand returns the higher of the two register numbers.
8525 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
8526 same as the 'Q' operand, since the most signficant part of the
8527 value is held in the lower number register. The reverse is true
8528 on systems where WORDS_BIG_ENDIAN is false.
8530 The purpose of these operands is to distinguish between cases
8531 where the endian-ness of the values is important (for example
8532 when they are added together), and cases where the endian-ness
8533 is irrelevant, but the order of register operations is important.
8534 For example when loading a value from memory into a register
8535 pair, the endian-ness does not matter. Provided that the value
8536 from the lower memory address is put into the lower numbered
8537 register, and the value from the higher address is put into the
8538 higher numbered register, the load will work regardless of whether
8539 the value being loaded is big-wordian or little-wordian. The
8540 order of the two register loads can matter however, if the address
8541 of the memory location is actually held in one of the registers
8542 being overwritten by the load. */
8543 case 'Q':
8544 if (REGNO (x) > LAST_ARM_REGNUM)
8545 abort ();
8546 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
8547 return;
8549 case 'R':
8550 if (REGNO (x) > LAST_ARM_REGNUM)
8551 abort ();
8552 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
8553 return;
8555 case 'H':
8556 if (REGNO (x) > LAST_ARM_REGNUM)
8557 abort ();
8558 asm_fprintf (stream, "%r", REGNO (x) + 1);
8559 return;
8561 case 'm':
8562 asm_fprintf (stream, "%r",
8563 GET_CODE (XEXP (x, 0)) == REG
8564 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
8565 return;
8567 case 'M':
8568 asm_fprintf (stream, "{%r-%r}",
8569 REGNO (x),
8570 REGNO (x) + NUM_REGS (GET_MODE (x)) - 1);
8571 return;
8573 case 'd':
8574 /* CONST_TRUE_RTX means always -- that's the default. */
8575 if (x == const_true_rtx)
8576 return;
8578 if (TARGET_ARM)
8579 fputs (arm_condition_codes[get_arm_condition_code (x)],
8580 stream);
8581 else
8582 fputs (thumb_condition_code (x, 0), stream);
8583 return;
8585 case 'D':
8586 /* CONST_TRUE_RTX means not always -- ie never. We shouldn't ever
8587 want to do that. */
8588 if (x == const_true_rtx)
8589 abort ();
8591 if (TARGET_ARM)
8592 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
8593 (get_arm_condition_code (x))],
8594 stream);
8595 else
8596 fputs (thumb_condition_code (x, 1), stream);
8597 return;
8599 default:
8600 if (x == 0)
8601 abort ();
8603 if (GET_CODE (x) == REG)
8604 asm_fprintf (stream, "%r", REGNO (x));
8605 else if (GET_CODE (x) == MEM)
8607 output_memory_reference_mode = GET_MODE (x);
8608 output_address (XEXP (x, 0));
8610 else if (GET_CODE (x) == CONST_DOUBLE)
8611 fprintf (stream, "#%s", fp_immediate_constant (x));
8612 else if (GET_CODE (x) == NEG)
8613 abort (); /* This should never happen now. */
8614 else
8616 fputc ('#', stream);
8617 output_addr_const (stream, x);
8622 #ifndef AOF_ASSEMBLER
8623 /* Target hook for assembling integer objects. The ARM version needs to
8624 handle word-sized values specially. */
8626 static bool
8627 arm_assemble_integer (x, size, aligned_p)
8628 rtx x;
8629 unsigned int size;
8630 int aligned_p;
8632 if (size == UNITS_PER_WORD && aligned_p)
8634 fputs ("\t.word\t", asm_out_file);
8635 output_addr_const (asm_out_file, x);
8637 /* Mark symbols as position independent. We only do this in the
8638 .text segment, not in the .data segment. */
8639 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
8640 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
8642 if (GET_CODE (x) == SYMBOL_REF
8643 && (CONSTANT_POOL_ADDRESS_P (x)
8644 || ENCODED_SHORT_CALL_ATTR_P (XSTR (x, 0))))
8645 fputs ("(GOTOFF)", asm_out_file);
8646 else if (GET_CODE (x) == LABEL_REF)
8647 fputs ("(GOTOFF)", asm_out_file);
8648 else
8649 fputs ("(GOT)", asm_out_file);
8651 fputc ('\n', asm_out_file);
8652 return true;
8655 return default_assemble_integer (x, size, aligned_p);
8657 #endif
8659 /* A finite state machine takes care of noticing whether or not instructions
8660 can be conditionally executed, and thus decrease execution time and code
8661 size by deleting branch instructions. The fsm is controlled by
8662 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
8664 /* The state of the fsm controlling condition codes are:
8665 0: normal, do nothing special
8666 1: make ASM_OUTPUT_OPCODE not output this instruction
8667 2: make ASM_OUTPUT_OPCODE not output this instruction
8668 3: make instructions conditional
8669 4: make instructions conditional
8671 State transitions (state->state by whom under condition):
8672 0 -> 1 final_prescan_insn if the `target' is a label
8673 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
8674 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
8675 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
8676 3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
8677 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
8678 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
8679 (the target insn is arm_target_insn).
8681 If the jump clobbers the conditions then we use states 2 and 4.
8683 A similar thing can be done with conditional return insns.
8685 XXX In case the `target' is an unconditional branch, this conditionalising
8686 of the instructions always reduces code size, but not always execution
8687 time. But then, I want to reduce the code size to somewhere near what
8688 /bin/cc produces. */
8690 /* Returns the index of the ARM condition code string in
8691 `arm_condition_codes'. COMPARISON should be an rtx like
8692 `(eq (...) (...))'. */
8694 static enum arm_cond_code
8695 get_arm_condition_code (comparison)
8696 rtx comparison;
8698 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
8699 int code;
8700 enum rtx_code comp_code = GET_CODE (comparison);
8702 if (GET_MODE_CLASS (mode) != MODE_CC)
8703 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
8704 XEXP (comparison, 1));
8706 switch (mode)
8708 case CC_DNEmode: code = ARM_NE; goto dominance;
8709 case CC_DEQmode: code = ARM_EQ; goto dominance;
8710 case CC_DGEmode: code = ARM_GE; goto dominance;
8711 case CC_DGTmode: code = ARM_GT; goto dominance;
8712 case CC_DLEmode: code = ARM_LE; goto dominance;
8713 case CC_DLTmode: code = ARM_LT; goto dominance;
8714 case CC_DGEUmode: code = ARM_CS; goto dominance;
8715 case CC_DGTUmode: code = ARM_HI; goto dominance;
8716 case CC_DLEUmode: code = ARM_LS; goto dominance;
8717 case CC_DLTUmode: code = ARM_CC;
8719 dominance:
8720 if (comp_code != EQ && comp_code != NE)
8721 abort ();
8723 if (comp_code == EQ)
8724 return ARM_INVERSE_CONDITION_CODE (code);
8725 return code;
8727 case CC_NOOVmode:
8728 switch (comp_code)
8730 case NE: return ARM_NE;
8731 case EQ: return ARM_EQ;
8732 case GE: return ARM_PL;
8733 case LT: return ARM_MI;
8734 default: abort ();
8737 case CC_Zmode:
8738 switch (comp_code)
8740 case NE: return ARM_NE;
8741 case EQ: return ARM_EQ;
8742 default: abort ();
8745 case CCFPEmode:
8746 case CCFPmode:
8747 /* These encodings assume that AC=1 in the FPA system control
8748 byte. This allows us to handle all cases except UNEQ and
8749 LTGT. */
8750 switch (comp_code)
8752 case GE: return ARM_GE;
8753 case GT: return ARM_GT;
8754 case LE: return ARM_LS;
8755 case LT: return ARM_MI;
8756 case NE: return ARM_NE;
8757 case EQ: return ARM_EQ;
8758 case ORDERED: return ARM_VC;
8759 case UNORDERED: return ARM_VS;
8760 case UNLT: return ARM_LT;
8761 case UNLE: return ARM_LE;
8762 case UNGT: return ARM_HI;
8763 case UNGE: return ARM_PL;
8764 /* UNEQ and LTGT do not have a representation. */
8765 case UNEQ: /* Fall through. */
8766 case LTGT: /* Fall through. */
8767 default: abort ();
8770 case CC_SWPmode:
8771 switch (comp_code)
8773 case NE: return ARM_NE;
8774 case EQ: return ARM_EQ;
8775 case GE: return ARM_LE;
8776 case GT: return ARM_LT;
8777 case LE: return ARM_GE;
8778 case LT: return ARM_GT;
8779 case GEU: return ARM_LS;
8780 case GTU: return ARM_CC;
8781 case LEU: return ARM_CS;
8782 case LTU: return ARM_HI;
8783 default: abort ();
8786 case CC_Cmode:
8787 switch (comp_code)
8789 case LTU: return ARM_CS;
8790 case GEU: return ARM_CC;
8791 default: abort ();
8794 case CCmode:
8795 switch (comp_code)
8797 case NE: return ARM_NE;
8798 case EQ: return ARM_EQ;
8799 case GE: return ARM_GE;
8800 case GT: return ARM_GT;
8801 case LE: return ARM_LE;
8802 case LT: return ARM_LT;
8803 case GEU: return ARM_CS;
8804 case GTU: return ARM_HI;
8805 case LEU: return ARM_LS;
8806 case LTU: return ARM_CC;
8807 default: abort ();
8810 default: abort ();
8813 abort ();
8817 void
8818 arm_final_prescan_insn (insn)
8819 rtx insn;
8821 /* BODY will hold the body of INSN. */
8822 rtx body = PATTERN (insn);
8824 /* This will be 1 if trying to repeat the trick, and things need to be
8825 reversed if it appears to fail. */
8826 int reverse = 0;
8828 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
8829 taken are clobbered, even if the rtl suggests otherwise. It also
8830 means that we have to grub around within the jump expression to find
8831 out what the conditions are when the jump isn't taken. */
8832 int jump_clobbers = 0;
8834 /* If we start with a return insn, we only succeed if we find another one. */
8835 int seeking_return = 0;
8837 /* START_INSN will hold the insn from where we start looking. This is the
8838 first insn after the following code_label if REVERSE is true. */
8839 rtx start_insn = insn;
8841 /* If in state 4, check if the target branch is reached, in order to
8842 change back to state 0. */
8843 if (arm_ccfsm_state == 4)
8845 if (insn == arm_target_insn)
8847 arm_target_insn = NULL;
8848 arm_ccfsm_state = 0;
8850 return;
8853 /* If in state 3, it is possible to repeat the trick, if this insn is an
8854 unconditional branch to a label, and immediately following this branch
8855 is the previous target label which is only used once, and the label this
8856 branch jumps to is not too far off. */
8857 if (arm_ccfsm_state == 3)
8859 if (simplejump_p (insn))
8861 start_insn = next_nonnote_insn (start_insn);
8862 if (GET_CODE (start_insn) == BARRIER)
8864 /* XXX Isn't this always a barrier? */
8865 start_insn = next_nonnote_insn (start_insn);
8867 if (GET_CODE (start_insn) == CODE_LABEL
8868 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
8869 && LABEL_NUSES (start_insn) == 1)
8870 reverse = TRUE;
8871 else
8872 return;
8874 else if (GET_CODE (body) == RETURN)
8876 start_insn = next_nonnote_insn (start_insn);
8877 if (GET_CODE (start_insn) == BARRIER)
8878 start_insn = next_nonnote_insn (start_insn);
8879 if (GET_CODE (start_insn) == CODE_LABEL
8880 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
8881 && LABEL_NUSES (start_insn) == 1)
8883 reverse = TRUE;
8884 seeking_return = 1;
8886 else
8887 return;
8889 else
8890 return;
8893 if (arm_ccfsm_state != 0 && !reverse)
8894 abort ();
8895 if (GET_CODE (insn) != JUMP_INSN)
8896 return;
8898 /* This jump might be paralleled with a clobber of the condition codes
8899 the jump should always come first */
8900 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
8901 body = XVECEXP (body, 0, 0);
8903 #if 0
8904 /* If this is a conditional return then we don't want to know */
8905 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
8906 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
8907 && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
8908 || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
8909 return;
8910 #endif
8912 if (reverse
8913 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
8914 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
8916 int insns_skipped;
8917 int fail = FALSE, succeed = FALSE;
8918 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
8919 int then_not_else = TRUE;
8920 rtx this_insn = start_insn, label = 0;
8922 /* If the jump cannot be done with one instruction, we cannot
8923 conditionally execute the instruction in the inverse case. */
8924 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
8926 jump_clobbers = 1;
8927 return;
8930 /* Register the insn jumped to. */
8931 if (reverse)
8933 if (!seeking_return)
8934 label = XEXP (SET_SRC (body), 0);
8936 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
8937 label = XEXP (XEXP (SET_SRC (body), 1), 0);
8938 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
8940 label = XEXP (XEXP (SET_SRC (body), 2), 0);
8941 then_not_else = FALSE;
8943 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
8944 seeking_return = 1;
8945 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
8947 seeking_return = 1;
8948 then_not_else = FALSE;
8950 else
8951 abort ();
8953 /* See how many insns this branch skips, and what kind of insns. If all
8954 insns are okay, and the label or unconditional branch to the same
8955 label is not too far away, succeed. */
8956 for (insns_skipped = 0;
8957 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
8959 rtx scanbody;
8961 this_insn = next_nonnote_insn (this_insn);
8962 if (!this_insn)
8963 break;
8965 switch (GET_CODE (this_insn))
8967 case CODE_LABEL:
8968 /* Succeed if it is the target label, otherwise fail since
8969 control falls in from somewhere else. */
8970 if (this_insn == label)
8972 if (jump_clobbers)
8974 arm_ccfsm_state = 2;
8975 this_insn = next_nonnote_insn (this_insn);
8977 else
8978 arm_ccfsm_state = 1;
8979 succeed = TRUE;
8981 else
8982 fail = TRUE;
8983 break;
8985 case BARRIER:
8986 /* Succeed if the following insn is the target label.
8987 Otherwise fail.
8988 If return insns are used then the last insn in a function
8989 will be a barrier. */
8990 this_insn = next_nonnote_insn (this_insn);
8991 if (this_insn && this_insn == label)
8993 if (jump_clobbers)
8995 arm_ccfsm_state = 2;
8996 this_insn = next_nonnote_insn (this_insn);
8998 else
8999 arm_ccfsm_state = 1;
9000 succeed = TRUE;
9002 else
9003 fail = TRUE;
9004 break;
9006 case CALL_INSN:
9007 /* If using 32-bit addresses the cc is not preserved over
9008 calls. */
9009 if (TARGET_APCS_32)
9011 /* Succeed if the following insn is the target label,
9012 or if the following two insns are a barrier and
9013 the target label. */
9014 this_insn = next_nonnote_insn (this_insn);
9015 if (this_insn && GET_CODE (this_insn) == BARRIER)
9016 this_insn = next_nonnote_insn (this_insn);
9018 if (this_insn && this_insn == label
9019 && insns_skipped < max_insns_skipped)
9021 if (jump_clobbers)
9023 arm_ccfsm_state = 2;
9024 this_insn = next_nonnote_insn (this_insn);
9026 else
9027 arm_ccfsm_state = 1;
9028 succeed = TRUE;
9030 else
9031 fail = TRUE;
9033 break;
9035 case JUMP_INSN:
9036 /* If this is an unconditional branch to the same label, succeed.
9037 If it is to another label, do nothing. If it is conditional,
9038 fail. */
9039 /* XXX Probably, the tests for SET and the PC are unnecessary. */
9041 scanbody = PATTERN (this_insn);
9042 if (GET_CODE (scanbody) == SET
9043 && GET_CODE (SET_DEST (scanbody)) == PC)
9045 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
9046 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
9048 arm_ccfsm_state = 2;
9049 succeed = TRUE;
9051 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
9052 fail = TRUE;
9054 /* Fail if a conditional return is undesirable (eg on a
9055 StrongARM), but still allow this if optimizing for size. */
9056 else if (GET_CODE (scanbody) == RETURN
9057 && !use_return_insn (TRUE)
9058 && !optimize_size)
9059 fail = TRUE;
9060 else if (GET_CODE (scanbody) == RETURN
9061 && seeking_return)
9063 arm_ccfsm_state = 2;
9064 succeed = TRUE;
9066 else if (GET_CODE (scanbody) == PARALLEL)
9068 switch (get_attr_conds (this_insn))
9070 case CONDS_NOCOND:
9071 break;
9072 default:
9073 fail = TRUE;
9074 break;
9077 else
9078 fail = TRUE; /* Unrecognized jump (eg epilogue). */
9080 break;
9082 case INSN:
9083 /* Instructions using or affecting the condition codes make it
9084 fail. */
9085 scanbody = PATTERN (this_insn);
9086 if (!(GET_CODE (scanbody) == SET
9087 || GET_CODE (scanbody) == PARALLEL)
9088 || get_attr_conds (this_insn) != CONDS_NOCOND)
9089 fail = TRUE;
9090 break;
9092 default:
9093 break;
9096 if (succeed)
9098 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
9099 arm_target_label = CODE_LABEL_NUMBER (label);
9100 else if (seeking_return || arm_ccfsm_state == 2)
9102 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
9104 this_insn = next_nonnote_insn (this_insn);
9105 if (this_insn && (GET_CODE (this_insn) == BARRIER
9106 || GET_CODE (this_insn) == CODE_LABEL))
9107 abort ();
9109 if (!this_insn)
9111 /* Oh, dear! we ran off the end.. give up */
9112 recog (PATTERN (insn), insn, NULL);
9113 arm_ccfsm_state = 0;
9114 arm_target_insn = NULL;
9115 return;
9117 arm_target_insn = this_insn;
9119 else
9120 abort ();
9121 if (jump_clobbers)
9123 if (reverse)
9124 abort ();
9125 arm_current_cc =
9126 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
9127 0), 0), 1));
9128 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
9129 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9130 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
9131 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9133 else
9135 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
9136 what it was. */
9137 if (!reverse)
9138 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
9139 0));
9142 if (reverse || then_not_else)
9143 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9146 /* Restore recog_data (getting the attributes of other insns can
9147 destroy this array, but final.c assumes that it remains intact
9148 across this call; since the insn has been recognized already we
9149 call recog direct). */
9150 recog (PATTERN (insn), insn, NULL);
9154 /* Returns true if REGNO is a valid register
9155 for holding a quantity of tyoe MODE. */
9158 arm_hard_regno_mode_ok (regno, mode)
9159 unsigned int regno;
9160 enum machine_mode mode;
9162 if (GET_MODE_CLASS (mode) == MODE_CC)
9163 return regno == CC_REGNUM;
9165 if (TARGET_THUMB)
9166 /* For the Thumb we only allow values bigger than SImode in
9167 registers 0 - 6, so that there is always a second low
9168 register available to hold the upper part of the value.
9169 We probably we ought to ensure that the register is the
9170 start of an even numbered register pair. */
9171 return (NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
9173 if (regno <= LAST_ARM_REGNUM)
9174 /* We allow any value to be stored in the general regisetrs. */
9175 return 1;
9177 if ( regno == FRAME_POINTER_REGNUM
9178 || regno == ARG_POINTER_REGNUM)
9179 /* We only allow integers in the fake hard registers. */
9180 return GET_MODE_CLASS (mode) == MODE_INT;
9182 /* The only registers left are the FPU registers
9183 which we only allow to hold FP values. */
9184 return GET_MODE_CLASS (mode) == MODE_FLOAT
9185 && regno >= FIRST_ARM_FP_REGNUM
9186 && regno <= LAST_ARM_FP_REGNUM;
9190 arm_regno_class (regno)
9191 int regno;
9193 if (TARGET_THUMB)
9195 if (regno == STACK_POINTER_REGNUM)
9196 return STACK_REG;
9197 if (regno == CC_REGNUM)
9198 return CC_REG;
9199 if (regno < 8)
9200 return LO_REGS;
9201 return HI_REGS;
9204 if ( regno <= LAST_ARM_REGNUM
9205 || regno == FRAME_POINTER_REGNUM
9206 || regno == ARG_POINTER_REGNUM)
9207 return GENERAL_REGS;
9209 if (regno == CC_REGNUM)
9210 return NO_REGS;
9212 return FPU_REGS;
9215 /* Handle a special case when computing the offset
9216 of an argument from the frame pointer. */
9219 arm_debugger_arg_offset (value, addr)
9220 int value;
9221 rtx addr;
9223 rtx insn;
9225 /* We are only interested if dbxout_parms() failed to compute the offset. */
9226 if (value != 0)
9227 return 0;
9229 /* We can only cope with the case where the address is held in a register. */
9230 if (GET_CODE (addr) != REG)
9231 return 0;
9233 /* If we are using the frame pointer to point at the argument, then
9234 an offset of 0 is correct. */
9235 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
9236 return 0;
9238 /* If we are using the stack pointer to point at the
9239 argument, then an offset of 0 is correct. */
9240 if ((TARGET_THUMB || !frame_pointer_needed)
9241 && REGNO (addr) == SP_REGNUM)
9242 return 0;
9244 /* Oh dear. The argument is pointed to by a register rather
9245 than being held in a register, or being stored at a known
9246 offset from the frame pointer. Since GDB only understands
9247 those two kinds of argument we must translate the address
9248 held in the register into an offset from the frame pointer.
9249 We do this by searching through the insns for the function
9250 looking to see where this register gets its value. If the
9251 register is initialised from the frame pointer plus an offset
9252 then we are in luck and we can continue, otherwise we give up.
9254 This code is exercised by producing debugging information
9255 for a function with arguments like this:
9257 double func (double a, double b, int c, double d) {return d;}
9259 Without this code the stab for parameter 'd' will be set to
9260 an offset of 0 from the frame pointer, rather than 8. */
9262 /* The if() statement says:
9264 If the insn is a normal instruction
9265 and if the insn is setting the value in a register
9266 and if the register being set is the register holding the address of the argument
9267 and if the address is computing by an addition
9268 that involves adding to a register
9269 which is the frame pointer
9270 a constant integer
9272 then... */
9274 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9276 if ( GET_CODE (insn) == INSN
9277 && GET_CODE (PATTERN (insn)) == SET
9278 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
9279 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
9280 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
9281 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
9282 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
9285 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
9287 break;
9291 if (value == 0)
9293 debug_rtx (addr);
9294 warning ("unable to compute real location of stacked parameter");
9295 value = 8; /* XXX magic hack */
9298 return value;
9301 #define def_builtin(NAME, TYPE, CODE) \
9302 builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL)
9304 void
9305 arm_init_builtins ()
9307 tree endlink = void_list_node;
9308 tree int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
9309 tree pchar_type_node = build_pointer_type (char_type_node);
9311 tree int_ftype_int, void_ftype_pchar;
9313 /* void func (void *) */
9314 void_ftype_pchar
9315 = build_function_type (void_type_node,
9316 tree_cons (NULL_TREE, pchar_type_node, endlink));
9318 /* int func (int) */
9319 int_ftype_int
9320 = build_function_type (integer_type_node, int_endlink);
9322 /* Initialize arm V5 builtins. */
9323 if (arm_arch5)
9324 def_builtin ("__builtin_clz", int_ftype_int, ARM_BUILTIN_CLZ);
9327 /* Expand an expression EXP that calls a built-in function,
9328 with result going to TARGET if that's convenient
9329 (and in mode MODE if that's convenient).
9330 SUBTARGET may be used as the target for computing one of EXP's operands.
9331 IGNORE is nonzero if the value is to be ignored. */
9334 arm_expand_builtin (exp, target, subtarget, mode, ignore)
9335 tree exp;
9336 rtx target;
9337 rtx subtarget ATTRIBUTE_UNUSED;
9338 enum machine_mode mode ATTRIBUTE_UNUSED;
9339 int ignore ATTRIBUTE_UNUSED;
9341 enum insn_code icode;
9342 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
9343 tree arglist = TREE_OPERAND (exp, 1);
9344 tree arg0;
9345 rtx op0, pat;
9346 enum machine_mode tmode, mode0;
9347 int fcode = DECL_FUNCTION_CODE (fndecl);
9349 switch (fcode)
9351 default:
9352 break;
9354 case ARM_BUILTIN_CLZ:
9355 icode = CODE_FOR_clz;
9356 arg0 = TREE_VALUE (arglist);
9357 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
9358 tmode = insn_data[icode].operand[0].mode;
9359 mode0 = insn_data[icode].operand[1].mode;
9361 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9362 op0 = copy_to_mode_reg (mode0, op0);
9363 if (target == 0
9364 || GET_MODE (target) != tmode
9365 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9366 target = gen_reg_rtx (tmode);
9367 pat = GEN_FCN (icode) (target, op0);
9368 if (! pat)
9369 return 0;
9370 emit_insn (pat);
9371 return target;
9374 /* @@@ Should really do something sensible here. */
9375 return NULL_RTX;
9378 /* Recursively search through all of the blocks in a function
9379 checking to see if any of the variables created in that
9380 function match the RTX called 'orig'. If they do then
9381 replace them with the RTX called 'new'. */
9383 static void
9384 replace_symbols_in_block (block, orig, new)
9385 tree block;
9386 rtx orig;
9387 rtx new;
9389 for (; block; block = BLOCK_CHAIN (block))
9391 tree sym;
9393 if (!TREE_USED (block))
9394 continue;
9396 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
9398 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
9399 || DECL_IGNORED_P (sym)
9400 || TREE_CODE (sym) != VAR_DECL
9401 || DECL_EXTERNAL (sym)
9402 || !rtx_equal_p (DECL_RTL (sym), orig)
9404 continue;
9406 SET_DECL_RTL (sym, new);
9409 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
9413 /* Return the number (counting from 0) of
9414 the least significant set bit in MASK. */
9416 #ifdef __GNUC__
9417 inline
9418 #endif
9419 static int
9420 number_of_first_bit_set (mask)
9421 int mask;
9423 int bit;
9425 for (bit = 0;
9426 (mask & (1 << bit)) == 0;
9427 ++bit)
9428 continue;
9430 return bit;
9433 /* Generate code to return from a thumb function.
9434 If 'reg_containing_return_addr' is -1, then the return address is
9435 actually on the stack, at the stack pointer. */
9436 static void
9437 thumb_exit (f, reg_containing_return_addr, eh_ofs)
9438 FILE * f;
9439 int reg_containing_return_addr;
9440 rtx eh_ofs;
9442 unsigned regs_available_for_popping;
9443 unsigned regs_to_pop;
9444 int pops_needed;
9445 unsigned available;
9446 unsigned required;
9447 int mode;
9448 int size;
9449 int restore_a4 = FALSE;
9451 /* Compute the registers we need to pop. */
9452 regs_to_pop = 0;
9453 pops_needed = 0;
9455 /* There is an assumption here, that if eh_ofs is not NULL, the
9456 normal return address will have been pushed. */
9457 if (reg_containing_return_addr == -1 || eh_ofs)
9459 /* When we are generating a return for __builtin_eh_return,
9460 reg_containing_return_addr must specify the return regno. */
9461 if (eh_ofs && reg_containing_return_addr == -1)
9462 abort ();
9464 regs_to_pop |= 1 << LR_REGNUM;
9465 ++pops_needed;
9468 if (TARGET_BACKTRACE)
9470 /* Restore the (ARM) frame pointer and stack pointer. */
9471 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
9472 pops_needed += 2;
9475 /* If there is nothing to pop then just emit the BX instruction and
9476 return. */
9477 if (pops_needed == 0)
9479 if (eh_ofs)
9480 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9482 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9483 return;
9485 /* Otherwise if we are not supporting interworking and we have not created
9486 a backtrace structure and the function was not entered in ARM mode then
9487 just pop the return address straight into the PC. */
9488 else if (!TARGET_INTERWORK
9489 && !TARGET_BACKTRACE
9490 && !is_called_in_ARM_mode (current_function_decl))
9492 if (eh_ofs)
9494 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
9495 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9496 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9498 else
9499 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
9501 return;
9504 /* Find out how many of the (return) argument registers we can corrupt. */
9505 regs_available_for_popping = 0;
9507 /* If returning via __builtin_eh_return, the bottom three registers
9508 all contain information needed for the return. */
9509 if (eh_ofs)
9510 size = 12;
9511 else
9513 #ifdef RTX_CODE
9514 /* If we can deduce the registers used from the function's
9515 return value. This is more reliable that examining
9516 regs_ever_live[] because that will be set if the register is
9517 ever used in the function, not just if the register is used
9518 to hold a return value. */
9520 if (current_function_return_rtx != 0)
9521 mode = GET_MODE (current_function_return_rtx);
9522 else
9523 #endif
9524 mode = DECL_MODE (DECL_RESULT (current_function_decl));
9526 size = GET_MODE_SIZE (mode);
9528 if (size == 0)
9530 /* In a void function we can use any argument register.
9531 In a function that returns a structure on the stack
9532 we can use the second and third argument registers. */
9533 if (mode == VOIDmode)
9534 regs_available_for_popping =
9535 (1 << ARG_REGISTER (1))
9536 | (1 << ARG_REGISTER (2))
9537 | (1 << ARG_REGISTER (3));
9538 else
9539 regs_available_for_popping =
9540 (1 << ARG_REGISTER (2))
9541 | (1 << ARG_REGISTER (3));
9543 else if (size <= 4)
9544 regs_available_for_popping =
9545 (1 << ARG_REGISTER (2))
9546 | (1 << ARG_REGISTER (3));
9547 else if (size <= 8)
9548 regs_available_for_popping =
9549 (1 << ARG_REGISTER (3));
9552 /* Match registers to be popped with registers into which we pop them. */
9553 for (available = regs_available_for_popping,
9554 required = regs_to_pop;
9555 required != 0 && available != 0;
9556 available &= ~(available & - available),
9557 required &= ~(required & - required))
9558 -- pops_needed;
9560 /* If we have any popping registers left over, remove them. */
9561 if (available > 0)
9562 regs_available_for_popping &= ~available;
9564 /* Otherwise if we need another popping register we can use
9565 the fourth argument register. */
9566 else if (pops_needed)
9568 /* If we have not found any free argument registers and
9569 reg a4 contains the return address, we must move it. */
9570 if (regs_available_for_popping == 0
9571 && reg_containing_return_addr == LAST_ARG_REGNUM)
9573 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
9574 reg_containing_return_addr = LR_REGNUM;
9576 else if (size > 12)
9578 /* Register a4 is being used to hold part of the return value,
9579 but we have dire need of a free, low register. */
9580 restore_a4 = TRUE;
9582 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
9585 if (reg_containing_return_addr != LAST_ARG_REGNUM)
9587 /* The fourth argument register is available. */
9588 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
9590 --pops_needed;
9594 /* Pop as many registers as we can. */
9595 thumb_pushpop (f, regs_available_for_popping, FALSE);
9597 /* Process the registers we popped. */
9598 if (reg_containing_return_addr == -1)
9600 /* The return address was popped into the lowest numbered register. */
9601 regs_to_pop &= ~(1 << LR_REGNUM);
9603 reg_containing_return_addr =
9604 number_of_first_bit_set (regs_available_for_popping);
9606 /* Remove this register for the mask of available registers, so that
9607 the return address will not be corrupted by futher pops. */
9608 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
9611 /* If we popped other registers then handle them here. */
9612 if (regs_available_for_popping)
9614 int frame_pointer;
9616 /* Work out which register currently contains the frame pointer. */
9617 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
9619 /* Move it into the correct place. */
9620 asm_fprintf (f, "\tmov\t%r, %r\n",
9621 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
9623 /* (Temporarily) remove it from the mask of popped registers. */
9624 regs_available_for_popping &= ~(1 << frame_pointer);
9625 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
9627 if (regs_available_for_popping)
9629 int stack_pointer;
9631 /* We popped the stack pointer as well,
9632 find the register that contains it. */
9633 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
9635 /* Move it into the stack register. */
9636 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
9638 /* At this point we have popped all necessary registers, so
9639 do not worry about restoring regs_available_for_popping
9640 to its correct value:
9642 assert (pops_needed == 0)
9643 assert (regs_available_for_popping == (1 << frame_pointer))
9644 assert (regs_to_pop == (1 << STACK_POINTER)) */
9646 else
9648 /* Since we have just move the popped value into the frame
9649 pointer, the popping register is available for reuse, and
9650 we know that we still have the stack pointer left to pop. */
9651 regs_available_for_popping |= (1 << frame_pointer);
9655 /* If we still have registers left on the stack, but we no longer have
9656 any registers into which we can pop them, then we must move the return
9657 address into the link register and make available the register that
9658 contained it. */
9659 if (regs_available_for_popping == 0 && pops_needed > 0)
9661 regs_available_for_popping |= 1 << reg_containing_return_addr;
9663 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
9664 reg_containing_return_addr);
9666 reg_containing_return_addr = LR_REGNUM;
9669 /* If we have registers left on the stack then pop some more.
9670 We know that at most we will want to pop FP and SP. */
9671 if (pops_needed > 0)
9673 int popped_into;
9674 int move_to;
9676 thumb_pushpop (f, regs_available_for_popping, FALSE);
9678 /* We have popped either FP or SP.
9679 Move whichever one it is into the correct register. */
9680 popped_into = number_of_first_bit_set (regs_available_for_popping);
9681 move_to = number_of_first_bit_set (regs_to_pop);
9683 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
9685 regs_to_pop &= ~(1 << move_to);
9687 --pops_needed;
9690 /* If we still have not popped everything then we must have only
9691 had one register available to us and we are now popping the SP. */
9692 if (pops_needed > 0)
9694 int popped_into;
9696 thumb_pushpop (f, regs_available_for_popping, FALSE);
9698 popped_into = number_of_first_bit_set (regs_available_for_popping);
9700 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
9702 assert (regs_to_pop == (1 << STACK_POINTER))
9703 assert (pops_needed == 1)
9707 /* If necessary restore the a4 register. */
9708 if (restore_a4)
9710 if (reg_containing_return_addr != LR_REGNUM)
9712 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
9713 reg_containing_return_addr = LR_REGNUM;
9716 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
9719 if (eh_ofs)
9720 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9722 /* Return to caller. */
9723 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9726 /* Emit code to push or pop registers to or from the stack. */
9728 static void
9729 thumb_pushpop (f, mask, push)
9730 FILE * f;
9731 int mask;
9732 int push;
9734 int regno;
9735 int lo_mask = mask & 0xFF;
9737 if (lo_mask == 0 && !push && (mask & (1 << 15)))
9739 /* Special case. Do not generate a POP PC statement here, do it in
9740 thumb_exit() */
9741 thumb_exit (f, -1, NULL_RTX);
9742 return;
9745 fprintf (f, "\t%s\t{", push ? "push" : "pop");
9747 /* Look at the low registers first. */
9748 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
9750 if (lo_mask & 1)
9752 asm_fprintf (f, "%r", regno);
9754 if ((lo_mask & ~1) != 0)
9755 fprintf (f, ", ");
9759 if (push && (mask & (1 << LR_REGNUM)))
9761 /* Catch pushing the LR. */
9762 if (mask & 0xFF)
9763 fprintf (f, ", ");
9765 asm_fprintf (f, "%r", LR_REGNUM);
9767 else if (!push && (mask & (1 << PC_REGNUM)))
9769 /* Catch popping the PC. */
9770 if (TARGET_INTERWORK || TARGET_BACKTRACE)
9772 /* The PC is never poped directly, instead
9773 it is popped into r3 and then BX is used. */
9774 fprintf (f, "}\n");
9776 thumb_exit (f, -1, NULL_RTX);
9778 return;
9780 else
9782 if (mask & 0xFF)
9783 fprintf (f, ", ");
9785 asm_fprintf (f, "%r", PC_REGNUM);
9789 fprintf (f, "}\n");
9792 void
9793 thumb_final_prescan_insn (insn)
9794 rtx insn;
9796 if (flag_print_asm_name)
9797 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
9798 INSN_ADDRESSES (INSN_UID (insn)));
9802 thumb_shiftable_const (val)
9803 unsigned HOST_WIDE_INT val;
9805 unsigned HOST_WIDE_INT mask = 0xff;
9806 int i;
9808 if (val == 0) /* XXX */
9809 return 0;
9811 for (i = 0; i < 25; i++)
9812 if ((val & (mask << i)) == val)
9813 return 1;
9815 return 0;
9818 /* Returns non-zero if the current function contains,
9819 or might contain a far jump. */
9822 thumb_far_jump_used_p (int in_prologue)
9824 rtx insn;
9826 /* This test is only important for leaf functions. */
9827 /* assert (!leaf_function_p ()); */
9829 /* If we have already decided that far jumps may be used,
9830 do not bother checking again, and always return true even if
9831 it turns out that they are not being used. Once we have made
9832 the decision that far jumps are present (and that hence the link
9833 register will be pushed onto the stack) we cannot go back on it. */
9834 if (cfun->machine->far_jump_used)
9835 return 1;
9837 /* If this function is not being called from the prologue/epilogue
9838 generation code then it must be being called from the
9839 INITIAL_ELIMINATION_OFFSET macro. */
9840 if (!in_prologue)
9842 /* In this case we know that we are being asked about the elimination
9843 of the arg pointer register. If that register is not being used,
9844 then there are no arguments on the stack, and we do not have to
9845 worry that a far jump might force the prologue to push the link
9846 register, changing the stack offsets. In this case we can just
9847 return false, since the presence of far jumps in the function will
9848 not affect stack offsets.
9850 If the arg pointer is live (or if it was live, but has now been
9851 eliminated and so set to dead) then we do have to test to see if
9852 the function might contain a far jump. This test can lead to some
9853 false negatives, since before reload is completed, then length of
9854 branch instructions is not known, so gcc defaults to returning their
9855 longest length, which in turn sets the far jump attribute to true.
9857 A false negative will not result in bad code being generated, but it
9858 will result in a needless push and pop of the link register. We
9859 hope that this does not occur too often. */
9860 if (regs_ever_live [ARG_POINTER_REGNUM])
9861 cfun->machine->arg_pointer_live = 1;
9862 else if (!cfun->machine->arg_pointer_live)
9863 return 0;
9866 /* Check to see if the function contains a branch
9867 insn with the far jump attribute set. */
9868 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9870 if (GET_CODE (insn) == JUMP_INSN
9871 /* Ignore tablejump patterns. */
9872 && GET_CODE (PATTERN (insn)) != ADDR_VEC
9873 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
9874 && get_attr_far_jump (insn) == FAR_JUMP_YES
9877 /* Record the fact that we have decied that
9878 the function does use far jumps. */
9879 cfun->machine->far_jump_used = 1;
9880 return 1;
9884 return 0;
9887 /* Return non-zero if FUNC must be entered in ARM mode. */
9890 is_called_in_ARM_mode (func)
9891 tree func;
9893 if (TREE_CODE (func) != FUNCTION_DECL)
9894 abort ();
9896 /* Ignore the problem about functions whoes address is taken. */
9897 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
9898 return TRUE;
9900 #ifdef ARM_PE
9901 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
9902 #else
9903 return FALSE;
9904 #endif
9907 /* The bits which aren't usefully expanded as rtl. */
9909 const char *
9910 thumb_unexpanded_epilogue ()
9912 int regno;
9913 int live_regs_mask = 0;
9914 int high_regs_pushed = 0;
9915 int leaf_function = leaf_function_p ();
9916 int had_to_push_lr;
9917 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9919 if (return_used_this_function)
9920 return "";
9922 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
9923 if (regs_ever_live[regno] && !call_used_regs[regno]
9924 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9925 live_regs_mask |= 1 << regno;
9927 for (regno = 8; regno < 13; regno++)
9929 if (regs_ever_live[regno] && !call_used_regs[regno]
9930 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9931 high_regs_pushed++;
9934 /* The prolog may have pushed some high registers to use as
9935 work registers. eg the testuite file:
9936 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
9937 compiles to produce:
9938 push {r4, r5, r6, r7, lr}
9939 mov r7, r9
9940 mov r6, r8
9941 push {r6, r7}
9942 as part of the prolog. We have to undo that pushing here. */
9944 if (high_regs_pushed)
9946 int mask = live_regs_mask;
9947 int next_hi_reg;
9948 int size;
9949 int mode;
9951 #ifdef RTX_CODE
9952 /* If we can deduce the registers used from the function's return value.
9953 This is more reliable that examining regs_ever_live[] because that
9954 will be set if the register is ever used in the function, not just if
9955 the register is used to hold a return value. */
9957 if (current_function_return_rtx != 0)
9958 mode = GET_MODE (current_function_return_rtx);
9959 else
9960 #endif
9961 mode = DECL_MODE (DECL_RESULT (current_function_decl));
9963 size = GET_MODE_SIZE (mode);
9965 /* Unless we are returning a type of size > 12 register r3 is
9966 available. */
9967 if (size < 13)
9968 mask |= 1 << 3;
9970 if (mask == 0)
9971 /* Oh dear! We have no low registers into which we can pop
9972 high registers! */
9973 internal_error
9974 ("no low registers available for popping high registers");
9976 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
9977 if (regs_ever_live[next_hi_reg] && !call_used_regs[next_hi_reg]
9978 && !(TARGET_SINGLE_PIC_BASE && (next_hi_reg == arm_pic_register)))
9979 break;
9981 while (high_regs_pushed)
9983 /* Find lo register(s) into which the high register(s) can
9984 be popped. */
9985 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
9987 if (mask & (1 << regno))
9988 high_regs_pushed--;
9989 if (high_regs_pushed == 0)
9990 break;
9993 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
9995 /* Pop the values into the low register(s). */
9996 thumb_pushpop (asm_out_file, mask, 0);
9998 /* Move the value(s) into the high registers. */
9999 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10001 if (mask & (1 << regno))
10003 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
10004 regno);
10006 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
10007 if (regs_ever_live[next_hi_reg]
10008 && !call_used_regs[next_hi_reg]
10009 && !(TARGET_SINGLE_PIC_BASE
10010 && (next_hi_reg == arm_pic_register)))
10011 break;
10017 had_to_push_lr = (live_regs_mask || !leaf_function
10018 || thumb_far_jump_used_p (1));
10020 if (TARGET_BACKTRACE
10021 && ((live_regs_mask & 0xFF) == 0)
10022 && regs_ever_live [LAST_ARG_REGNUM] != 0)
10024 /* The stack backtrace structure creation code had to
10025 push R7 in order to get a work register, so we pop
10026 it now. */
10027 live_regs_mask |= (1 << LAST_LO_REGNUM);
10030 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
10032 if (had_to_push_lr
10033 && !is_called_in_ARM_mode (current_function_decl)
10034 && !eh_ofs)
10035 live_regs_mask |= 1 << PC_REGNUM;
10037 /* Either no argument registers were pushed or a backtrace
10038 structure was created which includes an adjusted stack
10039 pointer, so just pop everything. */
10040 if (live_regs_mask)
10041 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
10043 if (eh_ofs)
10044 thumb_exit (asm_out_file, 2, eh_ofs);
10045 /* We have either just popped the return address into the
10046 PC or it is was kept in LR for the entire function or
10047 it is still on the stack because we do not want to
10048 return by doing a pop {pc}. */
10049 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
10050 thumb_exit (asm_out_file,
10051 (had_to_push_lr
10052 && is_called_in_ARM_mode (current_function_decl)) ?
10053 -1 : LR_REGNUM, NULL_RTX);
10055 else
10057 /* Pop everything but the return address. */
10058 live_regs_mask &= ~(1 << PC_REGNUM);
10060 if (live_regs_mask)
10061 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
10063 if (had_to_push_lr)
10064 /* Get the return address into a temporary register. */
10065 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0);
10067 /* Remove the argument registers that were pushed onto the stack. */
10068 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
10069 SP_REGNUM, SP_REGNUM,
10070 current_function_pretend_args_size);
10072 if (eh_ofs)
10073 thumb_exit (asm_out_file, 2, eh_ofs);
10074 else
10075 thumb_exit (asm_out_file,
10076 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
10079 return "";
10082 /* Functions to save and restore machine-specific function data. */
10084 static void
10085 arm_mark_machine_status (p)
10086 struct function * p;
10088 machine_function *machine = p->machine;
10090 if (machine)
10091 ggc_mark_rtx (machine->eh_epilogue_sp_ofs);
10094 static void
10095 arm_init_machine_status (p)
10096 struct function * p;
10098 p->machine =
10099 (machine_function *) xcalloc (1, sizeof (machine_function));
10101 #if ARM_FT_UNKNOWWN != 0
10102 ((machine_function *) p->machine)->func_type = ARM_FT_UNKNOWN;
10103 #endif
10106 static void
10107 arm_free_machine_status (p)
10108 struct function * p;
10110 if (p->machine)
10112 free (p->machine);
10113 p->machine = NULL;
10117 /* Return an RTX indicating where the return address to the
10118 calling function can be found. */
10121 arm_return_addr (count, frame)
10122 int count;
10123 rtx frame ATTRIBUTE_UNUSED;
10125 if (count != 0)
10126 return NULL_RTX;
10128 if (TARGET_APCS_32)
10129 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
10130 else
10132 rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
10133 GEN_INT (RETURN_ADDR_MASK26));
10134 return get_func_hard_reg_initial_val (cfun, lr);
10138 /* Do anything needed before RTL is emitted for each function. */
10140 void
10141 arm_init_expanders ()
10143 /* Arrange to initialize and mark the machine per-function status. */
10144 init_machine_status = arm_init_machine_status;
10145 mark_machine_status = arm_mark_machine_status;
10146 free_machine_status = arm_free_machine_status;
10149 /* Generate the rest of a function's prologue. */
10151 void
10152 thumb_expand_prologue ()
10154 HOST_WIDE_INT amount = (get_frame_size ()
10155 + current_function_outgoing_args_size);
10156 unsigned long func_type;
10158 func_type = arm_current_func_type ();
10160 /* Naked functions don't have prologues. */
10161 if (IS_NAKED (func_type))
10162 return;
10164 if (IS_INTERRUPT (func_type))
10166 error ("interrupt Service Routines cannot be coded in Thumb mode");
10167 return;
10170 if (frame_pointer_needed)
10171 emit_insn (gen_movsi (hard_frame_pointer_rtx, stack_pointer_rtx));
10173 if (amount)
10175 amount = ROUND_UP (amount);
10177 if (amount < 512)
10178 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10179 GEN_INT (- amount)));
10180 else
10182 int regno;
10183 rtx reg;
10185 /* The stack decrement is too big for an immediate value in a single
10186 insn. In theory we could issue multiple subtracts, but after
10187 three of them it becomes more space efficient to place the full
10188 value in the constant pool and load into a register. (Also the
10189 ARM debugger really likes to see only one stack decrement per
10190 function). So instead we look for a scratch register into which
10191 we can load the decrement, and then we subtract this from the
10192 stack pointer. Unfortunately on the thumb the only available
10193 scratch registers are the argument registers, and we cannot use
10194 these as they may hold arguments to the function. Instead we
10195 attempt to locate a call preserved register which is used by this
10196 function. If we can find one, then we know that it will have
10197 been pushed at the start of the prologue and so we can corrupt
10198 it now. */
10199 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
10200 if (regs_ever_live[regno]
10201 && !call_used_regs[regno] /* Paranoia */
10202 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register))
10203 && !(frame_pointer_needed
10204 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
10205 break;
10207 if (regno > LAST_LO_REGNUM) /* Very unlikely */
10209 rtx spare = gen_rtx (REG, SImode, IP_REGNUM);
10211 /* Choose an arbitary, non-argument low register. */
10212 reg = gen_rtx (REG, SImode, LAST_LO_REGNUM);
10214 /* Save it by copying it into a high, scratch register. */
10215 emit_insn (gen_movsi (spare, reg));
10216 /* Add a USE to stop propagate_one_insn() from barfing. */
10217 emit_insn (gen_prologue_use (spare));
10219 /* Decrement the stack. */
10220 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
10221 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10222 reg));
10224 /* Restore the low register's original value. */
10225 emit_insn (gen_movsi (reg, spare));
10227 /* Emit a USE of the restored scratch register, so that flow
10228 analysis will not consider the restore redundant. The
10229 register won't be used again in this function and isn't
10230 restored by the epilogue. */
10231 emit_insn (gen_prologue_use (reg));
10233 else
10235 reg = gen_rtx (REG, SImode, regno);
10237 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
10238 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10239 reg));
10244 if (current_function_profile || TARGET_NO_SCHED_PRO)
10245 emit_insn (gen_blockage ());
10248 void
10249 thumb_expand_epilogue ()
10251 HOST_WIDE_INT amount = (get_frame_size ()
10252 + current_function_outgoing_args_size);
10254 /* Naked functions don't have prologues. */
10255 if (IS_NAKED (arm_current_func_type ()))
10256 return;
10258 if (frame_pointer_needed)
10259 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
10260 else if (amount)
10262 amount = ROUND_UP (amount);
10264 if (amount < 512)
10265 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10266 GEN_INT (amount)));
10267 else
10269 /* r3 is always free in the epilogue. */
10270 rtx reg = gen_rtx (REG, SImode, LAST_ARG_REGNUM);
10272 emit_insn (gen_movsi (reg, GEN_INT (amount)));
10273 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
10277 /* Emit a USE (stack_pointer_rtx), so that
10278 the stack adjustment will not be deleted. */
10279 emit_insn (gen_prologue_use (stack_pointer_rtx));
10281 if (current_function_profile || TARGET_NO_SCHED_PRO)
10282 emit_insn (gen_blockage ());
10285 static void
10286 thumb_output_function_prologue (f, size)
10287 FILE * f;
10288 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
10290 int live_regs_mask = 0;
10291 int high_regs_pushed = 0;
10292 int regno;
10294 if (IS_NAKED (arm_current_func_type ()))
10295 return;
10297 if (is_called_in_ARM_mode (current_function_decl))
10299 const char * name;
10301 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
10302 abort ();
10303 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
10304 abort ();
10305 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10307 /* Generate code sequence to switch us into Thumb mode. */
10308 /* The .code 32 directive has already been emitted by
10309 ASM_DECLARE_FUNCTION_NAME. */
10310 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
10311 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
10313 /* Generate a label, so that the debugger will notice the
10314 change in instruction sets. This label is also used by
10315 the assembler to bypass the ARM code when this function
10316 is called from a Thumb encoded function elsewhere in the
10317 same file. Hence the definition of STUB_NAME here must
10318 agree with the definition in gas/config/tc-arm.c */
10320 #define STUB_NAME ".real_start_of"
10322 asm_fprintf (f, "\t.code\t16\n");
10323 #ifdef ARM_PE
10324 if (arm_dllexport_name_p (name))
10325 name = arm_strip_name_encoding (name);
10326 #endif
10327 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
10328 asm_fprintf (f, "\t.thumb_func\n");
10329 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
10332 if (current_function_pretend_args_size)
10334 if (cfun->machine->uses_anonymous_args)
10336 int num_pushes;
10338 asm_fprintf (f, "\tpush\t{");
10340 num_pushes = NUM_INTS (current_function_pretend_args_size);
10342 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
10343 regno <= LAST_ARG_REGNUM;
10344 regno++)
10345 asm_fprintf (f, "%r%s", regno,
10346 regno == LAST_ARG_REGNUM ? "" : ", ");
10348 asm_fprintf (f, "}\n");
10350 else
10351 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
10352 SP_REGNUM, SP_REGNUM,
10353 current_function_pretend_args_size);
10356 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10357 if (regs_ever_live[regno] && !call_used_regs[regno]
10358 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
10359 live_regs_mask |= 1 << regno;
10361 if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p (1))
10362 live_regs_mask |= 1 << LR_REGNUM;
10364 if (TARGET_BACKTRACE)
10366 int offset;
10367 int work_register = 0;
10368 int wr;
10370 /* We have been asked to create a stack backtrace structure.
10371 The code looks like this:
10373 0 .align 2
10374 0 func:
10375 0 sub SP, #16 Reserve space for 4 registers.
10376 2 push {R7} Get a work register.
10377 4 add R7, SP, #20 Get the stack pointer before the push.
10378 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
10379 8 mov R7, PC Get hold of the start of this code plus 12.
10380 10 str R7, [SP, #16] Store it.
10381 12 mov R7, FP Get hold of the current frame pointer.
10382 14 str R7, [SP, #4] Store it.
10383 16 mov R7, LR Get hold of the current return address.
10384 18 str R7, [SP, #12] Store it.
10385 20 add R7, SP, #16 Point at the start of the backtrace structure.
10386 22 mov FP, R7 Put this value into the frame pointer. */
10388 if ((live_regs_mask & 0xFF) == 0)
10390 /* See if the a4 register is free. */
10392 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
10393 work_register = LAST_ARG_REGNUM;
10394 else /* We must push a register of our own */
10395 live_regs_mask |= (1 << LAST_LO_REGNUM);
10398 if (work_register == 0)
10400 /* Select a register from the list that will be pushed to
10401 use as our work register. */
10402 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
10403 if ((1 << work_register) & live_regs_mask)
10404 break;
10407 asm_fprintf
10408 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
10409 SP_REGNUM, SP_REGNUM);
10411 if (live_regs_mask)
10412 thumb_pushpop (f, live_regs_mask, 1);
10414 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
10415 if (wr & live_regs_mask)
10416 offset += 4;
10418 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
10419 offset + 16 + current_function_pretend_args_size);
10421 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10422 offset + 4);
10424 /* Make sure that the instruction fetching the PC is in the right place
10425 to calculate "start of backtrace creation code + 12". */
10426 if (live_regs_mask)
10428 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
10429 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10430 offset + 12);
10431 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
10432 ARM_HARD_FRAME_POINTER_REGNUM);
10433 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10434 offset);
10436 else
10438 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
10439 ARM_HARD_FRAME_POINTER_REGNUM);
10440 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10441 offset);
10442 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
10443 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10444 offset + 12);
10447 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
10448 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10449 offset + 8);
10450 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
10451 offset + 12);
10452 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
10453 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
10455 else if (live_regs_mask)
10456 thumb_pushpop (f, live_regs_mask, 1);
10458 for (regno = 8; regno < 13; regno++)
10460 if (regs_ever_live[regno] && !call_used_regs[regno]
10461 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
10462 high_regs_pushed++;
10465 if (high_regs_pushed)
10467 int pushable_regs = 0;
10468 int mask = live_regs_mask & 0xff;
10469 int next_hi_reg;
10471 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
10473 if (regs_ever_live[next_hi_reg] && !call_used_regs[next_hi_reg]
10474 && !(TARGET_SINGLE_PIC_BASE
10475 && (next_hi_reg == arm_pic_register)))
10476 break;
10479 pushable_regs = mask;
10481 if (pushable_regs == 0)
10483 /* Desperation time -- this probably will never happen. */
10484 if (regs_ever_live[LAST_ARG_REGNUM]
10485 || !call_used_regs[LAST_ARG_REGNUM])
10486 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
10487 mask = 1 << LAST_ARG_REGNUM;
10490 while (high_regs_pushed > 0)
10492 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
10494 if (mask & (1 << regno))
10496 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
10498 high_regs_pushed--;
10500 if (high_regs_pushed)
10501 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
10502 next_hi_reg--)
10504 if (regs_ever_live[next_hi_reg]
10505 && !call_used_regs[next_hi_reg]
10506 && !(TARGET_SINGLE_PIC_BASE
10507 && (next_hi_reg == arm_pic_register)))
10508 break;
10510 else
10512 mask &= ~((1 << regno) - 1);
10513 break;
10518 thumb_pushpop (f, mask, 1);
10521 if (pushable_regs == 0
10522 && (regs_ever_live[LAST_ARG_REGNUM]
10523 || !call_used_regs[LAST_ARG_REGNUM]))
10524 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
10528 /* Handle the case of a double word load into a low register from
10529 a computed memory address. The computed address may involve a
10530 register which is overwritten by the load. */
10532 const char *
10533 thumb_load_double_from_address (operands)
10534 rtx *operands;
10536 rtx addr;
10537 rtx base;
10538 rtx offset;
10539 rtx arg1;
10540 rtx arg2;
10542 if (GET_CODE (operands[0]) != REG)
10543 abort ();
10545 if (GET_CODE (operands[1]) != MEM)
10546 abort ();
10548 /* Get the memory address. */
10549 addr = XEXP (operands[1], 0);
10551 /* Work out how the memory address is computed. */
10552 switch (GET_CODE (addr))
10554 case REG:
10555 operands[2] = gen_rtx (MEM, SImode,
10556 plus_constant (XEXP (operands[1], 0), 4));
10558 if (REGNO (operands[0]) == REGNO (addr))
10560 output_asm_insn ("ldr\t%H0, %2", operands);
10561 output_asm_insn ("ldr\t%0, %1", operands);
10563 else
10565 output_asm_insn ("ldr\t%0, %1", operands);
10566 output_asm_insn ("ldr\t%H0, %2", operands);
10568 break;
10570 case CONST:
10571 /* Compute <address> + 4 for the high order load. */
10572 operands[2] = gen_rtx (MEM, SImode,
10573 plus_constant (XEXP (operands[1], 0), 4));
10575 output_asm_insn ("ldr\t%0, %1", operands);
10576 output_asm_insn ("ldr\t%H0, %2", operands);
10577 break;
10579 case PLUS:
10580 arg1 = XEXP (addr, 0);
10581 arg2 = XEXP (addr, 1);
10583 if (CONSTANT_P (arg1))
10584 base = arg2, offset = arg1;
10585 else
10586 base = arg1, offset = arg2;
10588 if (GET_CODE (base) != REG)
10589 abort ();
10591 /* Catch the case of <address> = <reg> + <reg> */
10592 if (GET_CODE (offset) == REG)
10594 int reg_offset = REGNO (offset);
10595 int reg_base = REGNO (base);
10596 int reg_dest = REGNO (operands[0]);
10598 /* Add the base and offset registers together into the
10599 higher destination register. */
10600 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
10601 reg_dest + 1, reg_base, reg_offset);
10603 /* Load the lower destination register from the address in
10604 the higher destination register. */
10605 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
10606 reg_dest, reg_dest + 1);
10608 /* Load the higher destination register from its own address
10609 plus 4. */
10610 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
10611 reg_dest + 1, reg_dest + 1);
10613 else
10615 /* Compute <address> + 4 for the high order load. */
10616 operands[2] = gen_rtx (MEM, SImode,
10617 plus_constant (XEXP (operands[1], 0), 4));
10619 /* If the computed address is held in the low order register
10620 then load the high order register first, otherwise always
10621 load the low order register first. */
10622 if (REGNO (operands[0]) == REGNO (base))
10624 output_asm_insn ("ldr\t%H0, %2", operands);
10625 output_asm_insn ("ldr\t%0, %1", operands);
10627 else
10629 output_asm_insn ("ldr\t%0, %1", operands);
10630 output_asm_insn ("ldr\t%H0, %2", operands);
10633 break;
10635 case LABEL_REF:
10636 /* With no registers to worry about we can just load the value
10637 directly. */
10638 operands[2] = gen_rtx (MEM, SImode,
10639 plus_constant (XEXP (operands[1], 0), 4));
10641 output_asm_insn ("ldr\t%H0, %2", operands);
10642 output_asm_insn ("ldr\t%0, %1", operands);
10643 break;
10645 default:
10646 abort ();
10647 break;
10650 return "";
10654 const char *
10655 thumb_output_move_mem_multiple (n, operands)
10656 int n;
10657 rtx * operands;
10659 rtx tmp;
10661 switch (n)
10663 case 2:
10664 if (REGNO (operands[4]) > REGNO (operands[5]))
10666 tmp = operands[4];
10667 operands[4] = operands[5];
10668 operands[5] = tmp;
10670 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
10671 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
10672 break;
10674 case 3:
10675 if (REGNO (operands[4]) > REGNO (operands[5]))
10677 tmp = operands[4];
10678 operands[4] = operands[5];
10679 operands[5] = tmp;
10681 if (REGNO (operands[5]) > REGNO (operands[6]))
10683 tmp = operands[5];
10684 operands[5] = operands[6];
10685 operands[6] = tmp;
10687 if (REGNO (operands[4]) > REGNO (operands[5]))
10689 tmp = operands[4];
10690 operands[4] = operands[5];
10691 operands[5] = tmp;
10694 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
10695 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
10696 break;
10698 default:
10699 abort ();
10702 return "";
10705 /* Routines for generating rtl. */
10707 void
10708 thumb_expand_movstrqi (operands)
10709 rtx * operands;
10711 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
10712 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
10713 HOST_WIDE_INT len = INTVAL (operands[2]);
10714 HOST_WIDE_INT offset = 0;
10716 while (len >= 12)
10718 emit_insn (gen_movmem12b (out, in, out, in));
10719 len -= 12;
10722 if (len >= 8)
10724 emit_insn (gen_movmem8b (out, in, out, in));
10725 len -= 8;
10728 if (len >= 4)
10730 rtx reg = gen_reg_rtx (SImode);
10731 emit_insn (gen_movsi (reg, gen_rtx (MEM, SImode, in)));
10732 emit_insn (gen_movsi (gen_rtx (MEM, SImode, out), reg));
10733 len -= 4;
10734 offset += 4;
10737 if (len >= 2)
10739 rtx reg = gen_reg_rtx (HImode);
10740 emit_insn (gen_movhi (reg, gen_rtx (MEM, HImode,
10741 plus_constant (in, offset))));
10742 emit_insn (gen_movhi (gen_rtx (MEM, HImode, plus_constant (out, offset)),
10743 reg));
10744 len -= 2;
10745 offset += 2;
10748 if (len)
10750 rtx reg = gen_reg_rtx (QImode);
10751 emit_insn (gen_movqi (reg, gen_rtx (MEM, QImode,
10752 plus_constant (in, offset))));
10753 emit_insn (gen_movqi (gen_rtx (MEM, QImode, plus_constant (out, offset)),
10754 reg));
10759 thumb_cmp_operand (op, mode)
10760 rtx op;
10761 enum machine_mode mode;
10763 return ((GET_CODE (op) == CONST_INT
10764 && (unsigned HOST_WIDE_INT) (INTVAL (op)) < 256)
10765 || register_operand (op, mode));
10768 static const char *
10769 thumb_condition_code (x, invert)
10770 rtx x;
10771 int invert;
10773 static const char * const conds[] =
10775 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
10776 "hi", "ls", "ge", "lt", "gt", "le"
10778 int val;
10780 switch (GET_CODE (x))
10782 case EQ: val = 0; break;
10783 case NE: val = 1; break;
10784 case GEU: val = 2; break;
10785 case LTU: val = 3; break;
10786 case GTU: val = 8; break;
10787 case LEU: val = 9; break;
10788 case GE: val = 10; break;
10789 case LT: val = 11; break;
10790 case GT: val = 12; break;
10791 case LE: val = 13; break;
10792 default:
10793 abort ();
10796 return conds[val ^ invert];
10799 /* Handle storing a half-word to memory during reload. */
10801 void
10802 thumb_reload_out_hi (operands)
10803 rtx * operands;
10805 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
10808 /* Handle storing a half-word to memory during reload. */
10810 void
10811 thumb_reload_in_hi (operands)
10812 rtx * operands ATTRIBUTE_UNUSED;
10814 abort ();
10817 /* Return the length of a function name prefix
10818 that starts with the character 'c'. */
10820 static int
10821 arm_get_strip_length (char c)
10823 switch (c)
10825 ARM_NAME_ENCODING_LENGTHS
10826 default: return 0;
10830 /* Return a pointer to a function's name with any
10831 and all prefix encodings stripped from it. */
10833 const char *
10834 arm_strip_name_encoding (const char * name)
10836 int skip;
10838 while ((skip = arm_get_strip_length (* name)))
10839 name += skip;
10841 return name;
10844 #ifdef AOF_ASSEMBLER
10845 /* Special functions only needed when producing AOF syntax assembler. */
10847 rtx aof_pic_label = NULL_RTX;
10848 struct pic_chain
10850 struct pic_chain * next;
10851 const char * symname;
10854 static struct pic_chain * aof_pic_chain = NULL;
10857 aof_pic_entry (x)
10858 rtx x;
10860 struct pic_chain ** chainp;
10861 int offset;
10863 if (aof_pic_label == NULL_RTX)
10865 /* We mark this here and not in arm_add_gc_roots() to avoid
10866 polluting even more code with ifdefs, and because it never
10867 contains anything useful until we assign to it here. */
10868 ggc_add_rtx_root (&aof_pic_label, 1);
10869 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
10872 for (offset = 0, chainp = &aof_pic_chain; *chainp;
10873 offset += 4, chainp = &(*chainp)->next)
10874 if ((*chainp)->symname == XSTR (x, 0))
10875 return plus_constant (aof_pic_label, offset);
10877 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
10878 (*chainp)->next = NULL;
10879 (*chainp)->symname = XSTR (x, 0);
10880 return plus_constant (aof_pic_label, offset);
10883 void
10884 aof_dump_pic_table (f)
10885 FILE * f;
10887 struct pic_chain * chain;
10889 if (aof_pic_chain == NULL)
10890 return;
10892 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
10893 PIC_OFFSET_TABLE_REGNUM,
10894 PIC_OFFSET_TABLE_REGNUM);
10895 fputs ("|x$adcons|\n", f);
10897 for (chain = aof_pic_chain; chain; chain = chain->next)
10899 fputs ("\tDCD\t", f);
10900 assemble_name (f, chain->symname);
10901 fputs ("\n", f);
10905 int arm_text_section_count = 1;
10907 char *
10908 aof_text_section ()
10910 static char buf[100];
10911 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
10912 arm_text_section_count++);
10913 if (flag_pic)
10914 strcat (buf, ", PIC, REENTRANT");
10915 return buf;
10918 static int arm_data_section_count = 1;
10920 char *
10921 aof_data_section ()
10923 static char buf[100];
10924 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
10925 return buf;
10928 /* The AOF assembler is religiously strict about declarations of
10929 imported and exported symbols, so that it is impossible to declare
10930 a function as imported near the beginning of the file, and then to
10931 export it later on. It is, however, possible to delay the decision
10932 until all the functions in the file have been compiled. To get
10933 around this, we maintain a list of the imports and exports, and
10934 delete from it any that are subsequently defined. At the end of
10935 compilation we spit the remainder of the list out before the END
10936 directive. */
10938 struct import
10940 struct import * next;
10941 const char * name;
10944 static struct import * imports_list = NULL;
10946 void
10947 aof_add_import (name)
10948 const char * name;
10950 struct import * new;
10952 for (new = imports_list; new; new = new->next)
10953 if (new->name == name)
10954 return;
10956 new = (struct import *) xmalloc (sizeof (struct import));
10957 new->next = imports_list;
10958 imports_list = new;
10959 new->name = name;
10962 void
10963 aof_delete_import (name)
10964 const char * name;
10966 struct import ** old;
10968 for (old = &imports_list; *old; old = & (*old)->next)
10970 if ((*old)->name == name)
10972 *old = (*old)->next;
10973 return;
10978 int arm_main_function = 0;
10980 void
10981 aof_dump_imports (f)
10982 FILE * f;
10984 /* The AOF assembler needs this to cause the startup code to be extracted
10985 from the library. Brining in __main causes the whole thing to work
10986 automagically. */
10987 if (arm_main_function)
10989 text_section ();
10990 fputs ("\tIMPORT __main\n", f);
10991 fputs ("\tDCD __main\n", f);
10994 /* Now dump the remaining imports. */
10995 while (imports_list)
10997 fprintf (f, "\tIMPORT\t");
10998 assemble_name (f, imports_list->name);
10999 fputc ('\n', f);
11000 imports_list = imports_list->next;
11003 #endif /* AOF_ASSEMBLER */
11005 #ifdef OBJECT_FORMAT_ELF
11006 /* Switch to an arbitrary section NAME with attributes as specified
11007 by FLAGS. ALIGN specifies any known alignment requirements for
11008 the section; 0 if the default should be used.
11010 Differs from the default elf version only in the prefix character
11011 used before the section type. */
11013 static void
11014 arm_elf_asm_named_section (name, flags)
11015 const char *name;
11016 unsigned int flags;
11018 char flagchars[8], *f = flagchars;
11019 const char *type;
11021 if (!(flags & SECTION_DEBUG))
11022 *f++ = 'a';
11023 if (flags & SECTION_WRITE)
11024 *f++ = 'w';
11025 if (flags & SECTION_CODE)
11026 *f++ = 'x';
11027 if (flags & SECTION_SMALL)
11028 *f++ = 's';
11029 if (flags & SECTION_MERGE)
11030 *f++ = 'M';
11031 if (flags & SECTION_STRINGS)
11032 *f++ = 'S';
11033 *f = '\0';
11035 if (flags & SECTION_BSS)
11036 type = "nobits";
11037 else
11038 type = "progbits";
11040 if (flags & SECTION_ENTSIZE)
11041 fprintf (asm_out_file, "\t.section\t%s,\"%s\",%%%s,%d\n",
11042 name, flagchars, type, flags & SECTION_ENTSIZE);
11043 else
11044 fprintf (asm_out_file, "\t.section\t%s,\"%s\",%%%s\n",
11045 name, flagchars, type);
11047 #endif