Fix prologue/epilogue generation for ARM ISR routines.
[official-gcc.git] / gcc / config / arm / arm.c
blobd4a81f3d170cfbccfe393e76d808cbc492a4d6d8
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 /* Forward function declarations. */
64 static void arm_add_gc_roots PARAMS ((void));
65 static int arm_gen_constant PARAMS ((enum rtx_code, Mmode, Hint, rtx, rtx, int, int));
66 static Ulong bit_count PARAMS ((signed int));
67 static int const_ok_for_op PARAMS ((Hint, enum rtx_code));
68 static int eliminate_lr2ip PARAMS ((rtx *));
69 static rtx emit_multi_reg_push PARAMS ((int));
70 static rtx emit_sfm PARAMS ((int, int));
71 #ifndef AOF_ASSEMBLER
72 static bool arm_assemble_integer PARAMS ((rtx, unsigned int,
73 int));
74 #endif
75 static Ccstar fp_const_from_val PARAMS ((REAL_VALUE_TYPE *));
76 static arm_cc get_arm_condition_code PARAMS ((rtx));
77 static void init_fpa_table PARAMS ((void));
78 static Hint int_log2 PARAMS ((Hint));
79 static rtx is_jump_table PARAMS ((rtx));
80 static Ccstar output_multi_immediate PARAMS ((rtx *, Ccstar, Ccstar, int, Hint));
81 static void print_multi_reg PARAMS ((FILE *, Ccstar, int, int));
82 static Mmode select_dominance_cc_mode PARAMS ((rtx, rtx, Hint));
83 static Ccstar shift_op PARAMS ((rtx, Hint *));
84 static void arm_init_machine_status PARAMS ((struct function *));
85 static void arm_mark_machine_status PARAMS ((struct function *));
86 static void arm_free_machine_status PARAMS ((struct function *));
87 static int number_of_first_bit_set PARAMS ((int));
88 static void replace_symbols_in_block PARAMS ((tree, rtx, rtx));
89 static void thumb_exit PARAMS ((FILE *, int, rtx));
90 static void thumb_pushpop PARAMS ((FILE *, int, int));
91 static Ccstar thumb_condition_code PARAMS ((rtx, int));
92 static rtx is_jump_table PARAMS ((rtx));
93 static Hint get_jump_table_size PARAMS ((rtx));
94 static Mnode * move_minipool_fix_forward_ref PARAMS ((Mnode *, Mnode *, Hint));
95 static Mnode * add_minipool_forward_ref PARAMS ((Mfix *));
96 static Mnode * move_minipool_fix_backward_ref PARAMS ((Mnode *, Mnode *, Hint));
97 static Mnode * add_minipool_backward_ref PARAMS ((Mfix *));
98 static void assign_minipool_offsets PARAMS ((Mfix *));
99 static void arm_print_value PARAMS ((FILE *, rtx));
100 static void dump_minipool PARAMS ((rtx));
101 static int arm_barrier_cost PARAMS ((rtx));
102 static Mfix * create_fix_barrier PARAMS ((Mfix *, Hint));
103 static void push_minipool_barrier PARAMS ((rtx, Hint));
104 static void push_minipool_fix PARAMS ((rtx, Hint, rtx *, Mmode, rtx));
105 static void note_invalid_constants PARAMS ((rtx, Hint));
106 static int current_file_function_operand PARAMS ((rtx));
107 static Ulong arm_compute_save_reg0_reg12_mask PARAMS ((void));
108 static Ulong arm_compute_save_reg_mask PARAMS ((void));
109 static Ulong arm_isr_value PARAMS ((tree));
110 static Ulong arm_compute_func_type PARAMS ((void));
111 static tree arm_handle_fndecl_attribute PARAMS ((tree *, tree, tree, int, bool *));
112 static tree arm_handle_isr_attribute PARAMS ((tree *, tree, tree, int, bool *));
113 const struct attribute_spec arm_attribute_table[];
114 static void arm_output_function_epilogue PARAMS ((FILE *,
115 HOST_WIDE_INT));
116 static void arm_output_function_prologue PARAMS ((FILE *,
117 HOST_WIDE_INT));
118 static void thumb_output_function_prologue PARAMS ((FILE *,
119 HOST_WIDE_INT));
120 static int arm_comp_type_attributes PARAMS ((tree, tree));
121 static void arm_set_default_type_attributes PARAMS ((tree));
122 #ifdef OBJECT_FORMAT_ELF
123 static void arm_elf_asm_named_section PARAMS ((const char *,
124 unsigned int));
125 #endif
126 static int arm_adjust_cost PARAMS ((rtx, rtx, rtx, int));
128 #undef Hint
129 #undef Mmode
130 #undef Ulong
131 #undef Ccstar
133 /* Initialize the GCC target structure. */
134 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
135 #undef TARGET_MERGE_DECL_ATTRIBUTES
136 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
137 #endif
139 #undef TARGET_ATTRIBUTE_TABLE
140 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
142 #ifdef AOF_ASSEMBLER
143 #undef TARGET_ASM_BYTE_OP
144 #define TARGET_ASM_BYTE_OP "\tDCB\t"
145 #undef TARGET_ASM_ALIGNED_HI_OP
146 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
147 #undef TARGET_ASM_ALIGNED_SI_OP
148 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
149 #else
150 #undef TARGET_ASM_ALIGNED_SI_OP
151 #define TARGET_ASM_ALIGNED_SI_OP NULL
152 #undef TARGET_ASM_INTEGER
153 #define TARGET_ASM_INTEGER arm_assemble_integer
154 #endif
156 #undef TARGET_ASM_FUNCTION_PROLOGUE
157 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
159 #undef TARGET_ASM_FUNCTION_EPILOGUE
160 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
162 #undef TARGET_COMP_TYPE_ATTRIBUTES
163 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
165 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
166 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
168 #undef TARGET_INIT_BUILTINS
169 #define TARGET_INIT_BUILTINS arm_init_builtins
171 #undef TARGET_EXPAND_BUILTIN
172 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
174 #undef TARGET_SCHED_ADJUST_COST
175 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
177 struct gcc_target targetm = TARGET_INITIALIZER;
179 /* Obstack for minipool constant handling. */
180 static struct obstack minipool_obstack;
181 static char *minipool_startobj;
183 #define obstack_chunk_alloc xmalloc
184 #define obstack_chunk_free free
186 /* The maximum number of insns skipped which will be conditionalised if
187 possible. */
188 static int max_insns_skipped = 5;
190 extern FILE * asm_out_file;
192 /* True if we are currently building a constant table. */
193 int making_const_table;
195 /* Define the information needed to generate branch insns. This is
196 stored from the compare operation. */
197 rtx arm_compare_op0, arm_compare_op1;
199 /* What type of floating point are we tuning for? */
200 enum floating_point_type arm_fpu;
202 /* What type of floating point instructions are available? */
203 enum floating_point_type arm_fpu_arch;
205 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode. */
206 enum prog_mode_type arm_prgmode;
208 /* Set by the -mfp=... option. */
209 const char * target_fp_name = NULL;
211 /* Used to parse -mstructure_size_boundary command line option. */
212 const char * structure_size_string = NULL;
213 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
215 /* Bit values used to identify processor capabilities. */
216 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
217 #define FL_FAST_MULT (1 << 1) /* Fast multiply */
218 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
219 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
220 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
221 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
222 #define FL_THUMB (1 << 6) /* Thumb aware */
223 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
224 #define FL_STRONG (1 << 8) /* StrongARM */
225 #define FL_ARCH5E (1 << 9) /* DSP extenstions to v5 */
226 #define FL_XSCALE (1 << 10) /* XScale */
228 /* The bits in this mask specify which instructions we are
229 allowed to generate. */
230 static int insn_flags = 0;
232 /* The bits in this mask specify which instruction scheduling options should
233 be used. Note - there is an overlap with the FL_FAST_MULT. For some
234 hardware we want to be able to generate the multiply instructions, but to
235 tune as if they were not present in the architecture. */
236 static int tune_flags = 0;
238 /* The following are used in the arm.md file as equivalents to bits
239 in the above two flag variables. */
241 /* Nonzero if this is an "M" variant of the processor. */
242 int arm_fast_multiply = 0;
244 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
245 int arm_arch4 = 0;
247 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
248 int arm_arch5 = 0;
250 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
251 int arm_arch5e = 0;
253 /* Nonzero if this chip can benefit from load scheduling. */
254 int arm_ld_sched = 0;
256 /* Nonzero if this chip is a StrongARM. */
257 int arm_is_strong = 0;
259 /* Nonzero if this chip is an XScale. */
260 int arm_is_xscale = 0;
262 /* Nonzero if this chip is an ARM6 or an ARM7. */
263 int arm_is_6_or_7 = 0;
265 /* Nonzero if generating Thumb instructions. */
266 int thumb_code = 0;
268 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
269 must report the mode of the memory reference from PRINT_OPERAND to
270 PRINT_OPERAND_ADDRESS. */
271 enum machine_mode output_memory_reference_mode;
273 /* Nonzero if the prologue must setup `fp'. */
274 int current_function_anonymous_args;
276 /* The register number to be used for the PIC offset register. */
277 const char * arm_pic_register_string = NULL;
278 int arm_pic_register = 9;
280 /* Set to 1 when a return insn is output, this means that the epilogue
281 is not needed. */
282 int return_used_this_function;
284 /* Set to 1 after arm_reorg has started. Reset to start at the start of
285 the next function. */
286 static int after_arm_reorg = 0;
288 /* The maximum number of insns to be used when loading a constant. */
289 static int arm_constant_limit = 3;
291 /* For an explanation of these variables, see final_prescan_insn below. */
292 int arm_ccfsm_state;
293 enum arm_cond_code arm_current_cc;
294 rtx arm_target_insn;
295 int arm_target_label;
297 /* The condition codes of the ARM, and the inverse function. */
298 static const char *const arm_condition_codes[] =
300 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
301 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
304 #define streq(string1, string2) (strcmp (string1, string2) == 0)
306 /* Initialization code. */
308 struct processors
310 const char *const name;
311 const unsigned int flags;
314 /* Not all of these give usefully different compilation alternatives,
315 but there is no simple way of generalizing them. */
316 static const struct processors all_cores[] =
318 /* ARM Cores */
320 {"arm2", FL_CO_PROC | FL_MODE26 },
321 {"arm250", FL_CO_PROC | FL_MODE26 },
322 {"arm3", FL_CO_PROC | FL_MODE26 },
323 {"arm6", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
324 {"arm60", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
325 {"arm600", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
326 {"arm610", FL_MODE26 | FL_MODE32 },
327 {"arm620", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
328 {"arm7", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
329 /* arm7m doesn't exist on its own, but only with D, (and I), but
330 those don't alter the code, so arm7m is sometimes used. */
331 {"arm7m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
332 {"arm7d", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
333 {"arm7dm", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
334 {"arm7di", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
335 {"arm7dmi", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
336 {"arm70", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
337 {"arm700", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
338 {"arm700i", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
339 {"arm710", FL_MODE26 | FL_MODE32 },
340 {"arm710t", FL_MODE26 | FL_MODE32 | FL_THUMB },
341 {"arm720", FL_MODE26 | FL_MODE32 },
342 {"arm720t", FL_MODE26 | FL_MODE32 | FL_THUMB },
343 {"arm740t", FL_MODE26 | FL_MODE32 | FL_THUMB },
344 {"arm710c", FL_MODE26 | FL_MODE32 },
345 {"arm7100", FL_MODE26 | FL_MODE32 },
346 {"arm7500", FL_MODE26 | FL_MODE32 },
347 /* Doesn't have an external co-proc, but does have embedded fpu. */
348 {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
349 {"arm7tdmi", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
350 {"arm8", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
351 {"arm810", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
352 {"arm9", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
353 {"arm920", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
354 {"arm920t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
355 {"arm940t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
356 {"arm9tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
357 {"arm9e", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
358 {"strongarm", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
359 {"strongarm110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
360 {"strongarm1100", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
361 {"strongarm1110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
362 {"arm10tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_ARCH5 },
363 {"arm1020t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_ARCH5 },
364 {"xscale", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE },
366 {NULL, 0}
369 static const struct processors all_architectures[] =
371 /* ARM Architectures */
373 { "armv2", FL_CO_PROC | FL_MODE26 },
374 { "armv2a", FL_CO_PROC | FL_MODE26 },
375 { "armv3", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
376 { "armv3m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
377 { "armv4", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
378 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
379 implementations that support it, so we will leave it out for now. */
380 { "armv4t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
381 { "armv5", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
382 { "armv5t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
383 { "armv5te", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E },
384 { NULL, 0 }
387 /* This is a magic stucture. The 'string' field is magically filled in
388 with a pointer to the value specified by the user on the command line
389 assuming that the user has specified such a value. */
391 struct arm_cpu_select arm_select[] =
393 /* string name processors */
394 { NULL, "-mcpu=", all_cores },
395 { NULL, "-march=", all_architectures },
396 { NULL, "-mtune=", all_cores }
399 /* Return the number of bits set in value' */
400 static unsigned long
401 bit_count (value)
402 signed int value;
404 unsigned long count = 0;
406 while (value)
408 value &= ~(value & -value);
409 ++count;
412 return count;
415 /* Fix up any incompatible options that the user has specified.
416 This has now turned into a maze. */
417 void
418 arm_override_options ()
420 unsigned i;
422 /* Set up the flags based on the cpu/architecture selected by the user. */
423 for (i = ARRAY_SIZE (arm_select); i--;)
425 struct arm_cpu_select * ptr = arm_select + i;
427 if (ptr->string != NULL && ptr->string[0] != '\0')
429 const struct processors * sel;
431 for (sel = ptr->processors; sel->name != NULL; sel++)
432 if (streq (ptr->string, sel->name))
434 if (i == 2)
435 tune_flags = sel->flags;
436 else
438 /* If we have been given an architecture and a processor
439 make sure that they are compatible. We only generate
440 a warning though, and we prefer the CPU over the
441 architecture. */
442 if (insn_flags != 0 && (insn_flags ^ sel->flags))
443 warning ("switch -mcpu=%s conflicts with -march= switch",
444 ptr->string);
446 insn_flags = sel->flags;
449 break;
452 if (sel->name == NULL)
453 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
457 /* If the user did not specify a processor, choose one for them. */
458 if (insn_flags == 0)
460 const struct processors * sel;
461 unsigned int sought;
462 static const struct cpu_default
464 const int cpu;
465 const char *const name;
467 cpu_defaults[] =
469 { TARGET_CPU_arm2, "arm2" },
470 { TARGET_CPU_arm6, "arm6" },
471 { TARGET_CPU_arm610, "arm610" },
472 { TARGET_CPU_arm710, "arm710" },
473 { TARGET_CPU_arm7m, "arm7m" },
474 { TARGET_CPU_arm7500fe, "arm7500fe" },
475 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
476 { TARGET_CPU_arm8, "arm8" },
477 { TARGET_CPU_arm810, "arm810" },
478 { TARGET_CPU_arm9, "arm9" },
479 { TARGET_CPU_strongarm, "strongarm" },
480 { TARGET_CPU_xscale, "xscale" },
481 { TARGET_CPU_generic, "arm" },
482 { 0, 0 }
484 const struct cpu_default * def;
486 /* Find the default. */
487 for (def = cpu_defaults; def->name; def++)
488 if (def->cpu == TARGET_CPU_DEFAULT)
489 break;
491 /* Make sure we found the default CPU. */
492 if (def->name == NULL)
493 abort ();
495 /* Find the default CPU's flags. */
496 for (sel = all_cores; sel->name != NULL; sel++)
497 if (streq (def->name, sel->name))
498 break;
500 if (sel->name == NULL)
501 abort ();
503 insn_flags = sel->flags;
505 /* Now check to see if the user has specified some command line
506 switch that require certain abilities from the cpu. */
507 sought = 0;
509 if (TARGET_INTERWORK || TARGET_THUMB)
511 sought |= (FL_THUMB | FL_MODE32);
513 /* Force apcs-32 to be used for interworking. */
514 target_flags |= ARM_FLAG_APCS_32;
516 /* There are no ARM processors that support both APCS-26 and
517 interworking. Therefore we force FL_MODE26 to be removed
518 from insn_flags here (if it was set), so that the search
519 below will always be able to find a compatible processor. */
520 insn_flags &= ~FL_MODE26;
522 else if (!TARGET_APCS_32)
523 sought |= FL_MODE26;
525 if (sought != 0 && ((sought & insn_flags) != sought))
527 /* Try to locate a CPU type that supports all of the abilities
528 of the default CPU, plus the extra abilities requested by
529 the user. */
530 for (sel = all_cores; sel->name != NULL; sel++)
531 if ((sel->flags & sought) == (sought | insn_flags))
532 break;
534 if (sel->name == NULL)
536 unsigned int current_bit_count = 0;
537 const struct processors * best_fit = NULL;
539 /* Ideally we would like to issue an error message here
540 saying that it was not possible to find a CPU compatible
541 with the default CPU, but which also supports the command
542 line options specified by the programmer, and so they
543 ought to use the -mcpu=<name> command line option to
544 override the default CPU type.
546 Unfortunately this does not work with multilibing. We
547 need to be able to support multilibs for -mapcs-26 and for
548 -mthumb-interwork and there is no CPU that can support both
549 options. Instead if we cannot find a cpu that has both the
550 characteristics of the default cpu and the given command line
551 options we scan the array again looking for a best match. */
552 for (sel = all_cores; sel->name != NULL; sel++)
553 if ((sel->flags & sought) == sought)
555 unsigned int count;
557 count = bit_count (sel->flags & insn_flags);
559 if (count >= current_bit_count)
561 best_fit = sel;
562 current_bit_count = count;
566 if (best_fit == NULL)
567 abort ();
568 else
569 sel = best_fit;
572 insn_flags = sel->flags;
576 /* If tuning has not been specified, tune for whichever processor or
577 architecture has been selected. */
578 if (tune_flags == 0)
579 tune_flags = insn_flags;
581 /* Make sure that the processor choice does not conflict with any of the
582 other command line choices. */
583 if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
585 /* If APCS-32 was not the default then it must have been set by the
586 user, so issue a warning message. If the user has specified
587 "-mapcs-32 -mcpu=arm2" then we loose here. */
588 if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
589 warning ("target CPU does not support APCS-32" );
590 target_flags &= ~ARM_FLAG_APCS_32;
592 else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
594 warning ("target CPU does not support APCS-26" );
595 target_flags |= ARM_FLAG_APCS_32;
598 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
600 warning ("target CPU does not support interworking" );
601 target_flags &= ~ARM_FLAG_INTERWORK;
604 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
606 warning ("target CPU does not support THUMB instructions");
607 target_flags &= ~ARM_FLAG_THUMB;
610 if (TARGET_APCS_FRAME && TARGET_THUMB)
612 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
613 target_flags &= ~ARM_FLAG_APCS_FRAME;
616 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
617 from here where no function is being compiled currently. */
618 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
619 && TARGET_ARM)
620 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
622 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
623 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
625 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
626 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
628 /* If interworking is enabled then APCS-32 must be selected as well. */
629 if (TARGET_INTERWORK)
631 if (!TARGET_APCS_32)
632 warning ("interworking forces APCS-32 to be used" );
633 target_flags |= ARM_FLAG_APCS_32;
636 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
638 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
639 target_flags |= ARM_FLAG_APCS_FRAME;
642 if (TARGET_POKE_FUNCTION_NAME)
643 target_flags |= ARM_FLAG_APCS_FRAME;
645 if (TARGET_APCS_REENT && flag_pic)
646 error ("-fpic and -mapcs-reent are incompatible");
648 if (TARGET_APCS_REENT)
649 warning ("APCS reentrant code not supported. Ignored");
651 /* If this target is normally configured to use APCS frames, warn if they
652 are turned off and debugging is turned on. */
653 if (TARGET_ARM
654 && write_symbols != NO_DEBUG
655 && !TARGET_APCS_FRAME
656 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
657 warning ("-g with -mno-apcs-frame may not give sensible debugging");
659 /* If stack checking is disabled, we can use r10 as the PIC register,
660 which keeps r9 available. */
661 if (flag_pic && !TARGET_APCS_STACK)
662 arm_pic_register = 10;
664 if (TARGET_APCS_FLOAT)
665 warning ("passing floating point arguments in fp regs not yet supported");
667 /* Initialise boolean versions of the flags, for use in the arm.md file. */
668 arm_fast_multiply = (insn_flags & FL_FAST_MULT) != 0;
669 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
670 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
671 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
672 arm_is_xscale = (insn_flags & FL_XSCALE) != 0;
674 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
675 arm_is_strong = (tune_flags & FL_STRONG) != 0;
676 thumb_code = (TARGET_ARM == 0);
677 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
678 && !(tune_flags & FL_ARCH4))) != 0;
680 /* Default value for floating point code... if no co-processor
681 bus, then schedule for emulated floating point. Otherwise,
682 assume the user has an FPA.
683 Note: this does not prevent use of floating point instructions,
684 -msoft-float does that. */
685 arm_fpu = (tune_flags & FL_CO_PROC) ? FP_HARD : FP_SOFT3;
687 if (target_fp_name)
689 if (streq (target_fp_name, "2"))
690 arm_fpu_arch = FP_SOFT2;
691 else if (streq (target_fp_name, "3"))
692 arm_fpu_arch = FP_SOFT3;
693 else
694 error ("invalid floating point emulation option: -mfpe-%s",
695 target_fp_name);
697 else
698 arm_fpu_arch = FP_DEFAULT;
700 if (TARGET_FPE && arm_fpu != FP_HARD)
701 arm_fpu = FP_SOFT2;
703 /* For arm2/3 there is no need to do any scheduling if there is only
704 a floating point emulator, or we are doing software floating-point. */
705 if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD)
706 && (tune_flags & FL_MODE32) == 0)
707 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
709 arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
711 if (structure_size_string != NULL)
713 int size = strtol (structure_size_string, NULL, 0);
715 if (size == 8 || size == 32)
716 arm_structure_size_boundary = size;
717 else
718 warning ("structure size boundary can only be set to 8 or 32");
721 if (arm_pic_register_string != NULL)
723 int pic_register;
725 if (!flag_pic)
726 warning ("-mpic-register= is useless without -fpic");
728 pic_register = decode_reg_name (arm_pic_register_string);
730 /* Prevent the user from choosing an obviously stupid PIC register. */
731 if (pic_register < 0 || call_used_regs[pic_register]
732 || pic_register == HARD_FRAME_POINTER_REGNUM
733 || pic_register == STACK_POINTER_REGNUM
734 || pic_register >= PC_REGNUM)
735 error ("unable to use '%s' for PIC register", arm_pic_register_string);
736 else
737 arm_pic_register = pic_register;
740 if (TARGET_THUMB && flag_schedule_insns)
742 /* Don't warn since it's on by default in -O2. */
743 flag_schedule_insns = 0;
746 /* If optimizing for space, don't synthesize constants.
747 For processors with load scheduling, it never costs more than 2 cycles
748 to load a constant, and the load scheduler may well reduce that to 1. */
749 if (optimize_size || (tune_flags & FL_LDSCHED))
750 arm_constant_limit = 1;
752 if (arm_is_xscale)
753 arm_constant_limit = 2;
755 /* If optimizing for size, bump the number of instructions that we
756 are prepared to conditionally execute (even on a StrongARM).
757 Otherwise for the StrongARM, which has early execution of branches,
758 a sequence that is worth skipping is shorter. */
759 if (optimize_size)
760 max_insns_skipped = 6;
761 else if (arm_is_strong)
762 max_insns_skipped = 3;
764 /* Register global variables with the garbage collector. */
765 arm_add_gc_roots ();
768 static void
769 arm_add_gc_roots ()
771 ggc_add_rtx_root (&arm_compare_op0, 1);
772 ggc_add_rtx_root (&arm_compare_op1, 1);
773 ggc_add_rtx_root (&arm_target_insn, 1); /* Not sure this is really a root. */
775 gcc_obstack_init(&minipool_obstack);
776 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
779 /* A table of known ARM exception types.
780 For use with the interrupt function attribute. */
782 typedef struct
784 const char *const arg;
785 const unsigned long return_value;
787 isr_attribute_arg;
789 static const isr_attribute_arg isr_attribute_args [] =
791 { "IRQ", ARM_FT_ISR },
792 { "irq", ARM_FT_ISR },
793 { "FIQ", ARM_FT_FIQ },
794 { "fiq", ARM_FT_FIQ },
795 { "ABORT", ARM_FT_ISR },
796 { "abort", ARM_FT_ISR },
797 { "ABORT", ARM_FT_ISR },
798 { "abort", ARM_FT_ISR },
799 { "UNDEF", ARM_FT_EXCEPTION },
800 { "undef", ARM_FT_EXCEPTION },
801 { "SWI", ARM_FT_EXCEPTION },
802 { "swi", ARM_FT_EXCEPTION },
803 { NULL, ARM_FT_NORMAL }
806 /* Returns the (interrupt) function type of the current
807 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
809 static unsigned long
810 arm_isr_value (argument)
811 tree argument;
813 const isr_attribute_arg * ptr;
814 const char * arg;
816 /* No argument - default to IRQ. */
817 if (argument == NULL_TREE)
818 return ARM_FT_ISR;
820 /* Get the value of the argument. */
821 if (TREE_VALUE (argument) == NULL_TREE
822 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
823 return ARM_FT_UNKNOWN;
825 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
827 /* Check it against the list of known arguments. */
828 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr ++)
829 if (strcmp (arg, ptr->arg) == 0)
830 return ptr->return_value;
832 /* An unrecognised interrupt type. */
833 return ARM_FT_UNKNOWN;
836 /* Computes the type of the current function. */
838 static unsigned long
839 arm_compute_func_type ()
841 unsigned long type = ARM_FT_UNKNOWN;
842 tree a;
843 tree attr;
845 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
846 abort ();
848 /* Decide if the current function is volatile. Such functions
849 never return, and many memory cycles can be saved by not storing
850 register values that will never be needed again. This optimization
851 was added to speed up context switching in a kernel application. */
852 if (optimize > 0
853 && current_function_nothrow
854 && TREE_THIS_VOLATILE (current_function_decl))
855 type |= ARM_FT_VOLATILE;
857 if (current_function_needs_context)
858 type |= ARM_FT_NESTED;
860 attr = DECL_ATTRIBUTES (current_function_decl);
862 a = lookup_attribute ("naked", attr);
863 if (a != NULL_TREE)
864 type |= ARM_FT_NAKED;
866 if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
867 type |= ARM_FT_EXCEPTION_HANDLER;
868 else
870 a = lookup_attribute ("isr", attr);
871 if (a == NULL_TREE)
872 a = lookup_attribute ("interrupt", attr);
874 if (a == NULL_TREE)
875 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
876 else
877 type |= arm_isr_value (TREE_VALUE (a));
880 return type;
883 /* Returns the type of the current function. */
885 unsigned long
886 arm_current_func_type ()
888 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
889 cfun->machine->func_type = arm_compute_func_type ();
891 return cfun->machine->func_type;
894 /* Return 1 if it is possible to return using a single instruction. */
897 use_return_insn (iscond)
898 int iscond;
900 int regno;
901 unsigned int func_type;
903 /* Never use a return instruction before reload has run. */
904 if (!reload_completed)
905 return 0;
907 func_type = arm_current_func_type ();
909 /* Naked functions, volatile functiond and interrupt
910 functions all need special consideration. */
911 if (func_type & (ARM_FT_INTERRUPT | ARM_FT_VOLATILE | ARM_FT_NAKED))
912 return 0;
914 /* As do variadic functions. */
915 if (current_function_pretend_args_size
916 || current_function_anonymous_args
917 /* Of if the function calls __builtin_eh_return () */
918 || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
919 /* Or if there is no frame pointer and there is a stack adjustment. */
920 || ((get_frame_size () + current_function_outgoing_args_size != 0)
921 && !frame_pointer_needed))
922 return 0;
924 /* Can't be done if interworking with Thumb, and any registers have been
925 stacked. Similarly, on StrongARM, conditional returns are expensive
926 if they aren't taken and registers have been stacked. */
927 if (iscond && arm_is_strong && frame_pointer_needed)
928 return 0;
930 if ((iscond && arm_is_strong)
931 || TARGET_INTERWORK)
933 for (regno = 0; regno <= LAST_ARM_REGNUM; regno++)
934 if (regs_ever_live[regno] && !call_used_regs[regno])
935 return 0;
937 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
938 return 0;
941 /* Can't be done if any of the FPU regs are pushed,
942 since this also requires an insn. */
943 if (TARGET_HARD_FLOAT)
944 for (regno = FIRST_ARM_FP_REGNUM; regno <= LAST_ARM_FP_REGNUM; regno++)
945 if (regs_ever_live[regno] && !call_used_regs[regno])
946 return 0;
948 return 1;
951 /* Return TRUE if int I is a valid immediate ARM constant. */
954 const_ok_for_arm (i)
955 HOST_WIDE_INT i;
957 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
959 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
960 be all zero, or all one. */
961 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
962 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
963 != ((~(unsigned HOST_WIDE_INT) 0)
964 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
965 return FALSE;
967 /* Fast return for 0 and powers of 2 */
968 if ((i & (i - 1)) == 0)
969 return TRUE;
973 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
974 return TRUE;
975 mask =
976 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
977 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
979 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
981 return FALSE;
984 /* Return true if I is a valid constant for the operation CODE. */
985 static int
986 const_ok_for_op (i, code)
987 HOST_WIDE_INT i;
988 enum rtx_code code;
990 if (const_ok_for_arm (i))
991 return 1;
993 switch (code)
995 case PLUS:
996 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
998 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
999 case XOR:
1000 case IOR:
1001 return 0;
1003 case AND:
1004 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1006 default:
1007 abort ();
1011 /* Emit a sequence of insns to handle a large constant.
1012 CODE is the code of the operation required, it can be any of SET, PLUS,
1013 IOR, AND, XOR, MINUS;
1014 MODE is the mode in which the operation is being performed;
1015 VAL is the integer to operate on;
1016 SOURCE is the other operand (a register, or a null-pointer for SET);
1017 SUBTARGETS means it is safe to create scratch registers if that will
1018 either produce a simpler sequence, or we will want to cse the values.
1019 Return value is the number of insns emitted. */
1022 arm_split_constant (code, mode, val, target, source, subtargets)
1023 enum rtx_code code;
1024 enum machine_mode mode;
1025 HOST_WIDE_INT val;
1026 rtx target;
1027 rtx source;
1028 int subtargets;
1030 if (subtargets || code == SET
1031 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1032 && REGNO (target) != REGNO (source)))
1034 /* After arm_reorg has been called, we can't fix up expensive
1035 constants by pushing them into memory so we must synthesise
1036 them in-line, regardless of the cost. This is only likely to
1037 be more costly on chips that have load delay slots and we are
1038 compiling without running the scheduler (so no splitting
1039 occurred before the final instruction emission).
1041 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1043 if (!after_arm_reorg
1044 && (arm_gen_constant (code, mode, val, target, source, 1, 0)
1045 > arm_constant_limit + (code != SET)))
1047 if (code == SET)
1049 /* Currently SET is the only monadic value for CODE, all
1050 the rest are diadic. */
1051 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1052 return 1;
1054 else
1056 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1058 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1059 /* For MINUS, the value is subtracted from, since we never
1060 have subtraction of a constant. */
1061 if (code == MINUS)
1062 emit_insn (gen_rtx_SET (VOIDmode, target,
1063 gen_rtx_MINUS (mode, temp, source)));
1064 else
1065 emit_insn (gen_rtx_SET (VOIDmode, target,
1066 gen_rtx (code, mode, source, temp)));
1067 return 2;
1072 return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
1075 static int
1076 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1078 HOST_WIDE_INT temp1;
1079 int num_insns = 0;
1082 int end;
1084 if (i <= 0)
1085 i += 32;
1086 if (remainder & (3 << (i - 2)))
1088 end = i - 8;
1089 if (end < 0)
1090 end += 32;
1091 temp1 = remainder & ((0x0ff << end)
1092 | ((i < end) ? (0xff >> (32 - end)) : 0));
1093 remainder &= ~temp1;
1094 num_insns++;
1095 i -= 6;
1097 i -= 2;
1098 } while (remainder);
1099 return num_insns;
1102 /* As above, but extra parameter GENERATE which, if clear, suppresses
1103 RTL generation. */
1104 static int
1105 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
1106 enum rtx_code code;
1107 enum machine_mode mode;
1108 HOST_WIDE_INT val;
1109 rtx target;
1110 rtx source;
1111 int subtargets;
1112 int generate;
1114 int can_invert = 0;
1115 int can_negate = 0;
1116 int can_negate_initial = 0;
1117 int can_shift = 0;
1118 int i;
1119 int num_bits_set = 0;
1120 int set_sign_bit_copies = 0;
1121 int clear_sign_bit_copies = 0;
1122 int clear_zero_bit_copies = 0;
1123 int set_zero_bit_copies = 0;
1124 int insns = 0;
1125 unsigned HOST_WIDE_INT temp1, temp2;
1126 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1128 /* Find out which operations are safe for a given CODE. Also do a quick
1129 check for degenerate cases; these can occur when DImode operations
1130 are split. */
1131 switch (code)
1133 case SET:
1134 can_invert = 1;
1135 can_shift = 1;
1136 can_negate = 1;
1137 break;
1139 case PLUS:
1140 can_negate = 1;
1141 can_negate_initial = 1;
1142 break;
1144 case IOR:
1145 if (remainder == 0xffffffff)
1147 if (generate)
1148 emit_insn (gen_rtx_SET (VOIDmode, target,
1149 GEN_INT (ARM_SIGN_EXTEND (val))));
1150 return 1;
1152 if (remainder == 0)
1154 if (reload_completed && rtx_equal_p (target, source))
1155 return 0;
1156 if (generate)
1157 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1158 return 1;
1160 break;
1162 case AND:
1163 if (remainder == 0)
1165 if (generate)
1166 emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
1167 return 1;
1169 if (remainder == 0xffffffff)
1171 if (reload_completed && rtx_equal_p (target, source))
1172 return 0;
1173 if (generate)
1174 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1175 return 1;
1177 can_invert = 1;
1178 break;
1180 case XOR:
1181 if (remainder == 0)
1183 if (reload_completed && rtx_equal_p (target, source))
1184 return 0;
1185 if (generate)
1186 emit_insn (gen_rtx_SET (VOIDmode, target, source));
1187 return 1;
1189 if (remainder == 0xffffffff)
1191 if (generate)
1192 emit_insn (gen_rtx_SET (VOIDmode, target,
1193 gen_rtx_NOT (mode, source)));
1194 return 1;
1197 /* We don't know how to handle this yet below. */
1198 abort ();
1200 case MINUS:
1201 /* We treat MINUS as (val - source), since (source - val) is always
1202 passed as (source + (-val)). */
1203 if (remainder == 0)
1205 if (generate)
1206 emit_insn (gen_rtx_SET (VOIDmode, target,
1207 gen_rtx_NEG (mode, source)));
1208 return 1;
1210 if (const_ok_for_arm (val))
1212 if (generate)
1213 emit_insn (gen_rtx_SET (VOIDmode, target,
1214 gen_rtx_MINUS (mode, GEN_INT (val),
1215 source)));
1216 return 1;
1218 can_negate = 1;
1220 break;
1222 default:
1223 abort ();
1226 /* If we can do it in one insn get out quickly. */
1227 if (const_ok_for_arm (val)
1228 || (can_negate_initial && const_ok_for_arm (-val))
1229 || (can_invert && const_ok_for_arm (~val)))
1231 if (generate)
1232 emit_insn (gen_rtx_SET (VOIDmode, target,
1233 (source ? gen_rtx (code, mode, source,
1234 GEN_INT (val))
1235 : GEN_INT (val))));
1236 return 1;
1239 /* Calculate a few attributes that may be useful for specific
1240 optimizations. */
1241 for (i = 31; i >= 0; i--)
1243 if ((remainder & (1 << i)) == 0)
1244 clear_sign_bit_copies++;
1245 else
1246 break;
1249 for (i = 31; i >= 0; i--)
1251 if ((remainder & (1 << i)) != 0)
1252 set_sign_bit_copies++;
1253 else
1254 break;
1257 for (i = 0; i <= 31; i++)
1259 if ((remainder & (1 << i)) == 0)
1260 clear_zero_bit_copies++;
1261 else
1262 break;
1265 for (i = 0; i <= 31; i++)
1267 if ((remainder & (1 << i)) != 0)
1268 set_zero_bit_copies++;
1269 else
1270 break;
1273 switch (code)
1275 case SET:
1276 /* See if we can do this by sign_extending a constant that is known
1277 to be negative. This is a good, way of doing it, since the shift
1278 may well merge into a subsequent insn. */
1279 if (set_sign_bit_copies > 1)
1281 if (const_ok_for_arm
1282 (temp1 = ARM_SIGN_EXTEND (remainder
1283 << (set_sign_bit_copies - 1))))
1285 if (generate)
1287 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1288 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1289 GEN_INT (temp1)));
1290 emit_insn (gen_ashrsi3 (target, new_src,
1291 GEN_INT (set_sign_bit_copies - 1)));
1293 return 2;
1295 /* For an inverted constant, we will need to set the low bits,
1296 these will be shifted out of harm's way. */
1297 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1298 if (const_ok_for_arm (~temp1))
1300 if (generate)
1302 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1303 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1304 GEN_INT (temp1)));
1305 emit_insn (gen_ashrsi3 (target, new_src,
1306 GEN_INT (set_sign_bit_copies - 1)));
1308 return 2;
1312 /* See if we can generate this by setting the bottom (or the top)
1313 16 bits, and then shifting these into the other half of the
1314 word. We only look for the simplest cases, to do more would cost
1315 too much. Be careful, however, not to generate this when the
1316 alternative would take fewer insns. */
1317 if (val & 0xffff0000)
1319 temp1 = remainder & 0xffff0000;
1320 temp2 = remainder & 0x0000ffff;
1322 /* Overlaps outside this range are best done using other methods. */
1323 for (i = 9; i < 24; i++)
1325 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1326 && !const_ok_for_arm (temp2))
1328 rtx new_src = (subtargets
1329 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1330 : target);
1331 insns = arm_gen_constant (code, mode, temp2, new_src,
1332 source, subtargets, generate);
1333 source = new_src;
1334 if (generate)
1335 emit_insn (gen_rtx_SET
1336 (VOIDmode, target,
1337 gen_rtx_IOR (mode,
1338 gen_rtx_ASHIFT (mode, source,
1339 GEN_INT (i)),
1340 source)));
1341 return insns + 1;
1345 /* Don't duplicate cases already considered. */
1346 for (i = 17; i < 24; i++)
1348 if (((temp1 | (temp1 >> i)) == remainder)
1349 && !const_ok_for_arm (temp1))
1351 rtx new_src = (subtargets
1352 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1353 : target);
1354 insns = arm_gen_constant (code, mode, temp1, new_src,
1355 source, subtargets, generate);
1356 source = new_src;
1357 if (generate)
1358 emit_insn
1359 (gen_rtx_SET (VOIDmode, target,
1360 gen_rtx_IOR
1361 (mode,
1362 gen_rtx_LSHIFTRT (mode, source,
1363 GEN_INT (i)),
1364 source)));
1365 return insns + 1;
1369 break;
1371 case IOR:
1372 case XOR:
1373 /* If we have IOR or XOR, and the constant can be loaded in a
1374 single instruction, and we can find a temporary to put it in,
1375 then this can be done in two instructions instead of 3-4. */
1376 if (subtargets
1377 /* TARGET can't be NULL if SUBTARGETS is 0 */
1378 || (reload_completed && !reg_mentioned_p (target, source)))
1380 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1382 if (generate)
1384 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1386 emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
1387 emit_insn (gen_rtx_SET (VOIDmode, target,
1388 gen_rtx (code, mode, source, sub)));
1390 return 2;
1394 if (code == XOR)
1395 break;
1397 if (set_sign_bit_copies > 8
1398 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1400 if (generate)
1402 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1403 rtx shift = GEN_INT (set_sign_bit_copies);
1405 emit_insn (gen_rtx_SET (VOIDmode, sub,
1406 gen_rtx_NOT (mode,
1407 gen_rtx_ASHIFT (mode,
1408 source,
1409 shift))));
1410 emit_insn (gen_rtx_SET (VOIDmode, target,
1411 gen_rtx_NOT (mode,
1412 gen_rtx_LSHIFTRT (mode, sub,
1413 shift))));
1415 return 2;
1418 if (set_zero_bit_copies > 8
1419 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1421 if (generate)
1423 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1424 rtx shift = GEN_INT (set_zero_bit_copies);
1426 emit_insn (gen_rtx_SET (VOIDmode, sub,
1427 gen_rtx_NOT (mode,
1428 gen_rtx_LSHIFTRT (mode,
1429 source,
1430 shift))));
1431 emit_insn (gen_rtx_SET (VOIDmode, target,
1432 gen_rtx_NOT (mode,
1433 gen_rtx_ASHIFT (mode, sub,
1434 shift))));
1436 return 2;
1439 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1441 if (generate)
1443 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1444 emit_insn (gen_rtx_SET (VOIDmode, sub,
1445 gen_rtx_NOT (mode, source)));
1446 source = sub;
1447 if (subtargets)
1448 sub = gen_reg_rtx (mode);
1449 emit_insn (gen_rtx_SET (VOIDmode, sub,
1450 gen_rtx_AND (mode, source,
1451 GEN_INT (temp1))));
1452 emit_insn (gen_rtx_SET (VOIDmode, target,
1453 gen_rtx_NOT (mode, sub)));
1455 return 3;
1457 break;
1459 case AND:
1460 /* See if two shifts will do 2 or more insn's worth of work. */
1461 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1463 HOST_WIDE_INT shift_mask = ((0xffffffff
1464 << (32 - clear_sign_bit_copies))
1465 & 0xffffffff);
1467 if ((remainder | shift_mask) != 0xffffffff)
1469 if (generate)
1471 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1472 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1473 new_src, source, subtargets, 1);
1474 source = new_src;
1476 else
1478 rtx targ = subtargets ? NULL_RTX : target;
1479 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1480 targ, source, subtargets, 0);
1484 if (generate)
1486 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1487 rtx shift = GEN_INT (clear_sign_bit_copies);
1489 emit_insn (gen_ashlsi3 (new_src, source, shift));
1490 emit_insn (gen_lshrsi3 (target, new_src, shift));
1493 return insns + 2;
1496 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1498 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1500 if ((remainder | shift_mask) != 0xffffffff)
1502 if (generate)
1504 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1506 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1507 new_src, source, subtargets, 1);
1508 source = new_src;
1510 else
1512 rtx targ = subtargets ? NULL_RTX : target;
1514 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1515 targ, source, subtargets, 0);
1519 if (generate)
1521 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1522 rtx shift = GEN_INT (clear_zero_bit_copies);
1524 emit_insn (gen_lshrsi3 (new_src, source, shift));
1525 emit_insn (gen_ashlsi3 (target, new_src, shift));
1528 return insns + 2;
1531 break;
1533 default:
1534 break;
1537 for (i = 0; i < 32; i++)
1538 if (remainder & (1 << i))
1539 num_bits_set++;
1541 if (code == AND || (can_invert && num_bits_set > 16))
1542 remainder = (~remainder) & 0xffffffff;
1543 else if (code == PLUS && num_bits_set > 16)
1544 remainder = (-remainder) & 0xffffffff;
1545 else
1547 can_invert = 0;
1548 can_negate = 0;
1551 /* Now try and find a way of doing the job in either two or three
1552 instructions.
1553 We start by looking for the largest block of zeros that are aligned on
1554 a 2-bit boundary, we then fill up the temps, wrapping around to the
1555 top of the word when we drop off the bottom.
1556 In the worst case this code should produce no more than four insns. */
1558 int best_start = 0;
1559 int best_consecutive_zeros = 0;
1561 for (i = 0; i < 32; i += 2)
1563 int consecutive_zeros = 0;
1565 if (!(remainder & (3 << i)))
1567 while ((i < 32) && !(remainder & (3 << i)))
1569 consecutive_zeros += 2;
1570 i += 2;
1572 if (consecutive_zeros > best_consecutive_zeros)
1574 best_consecutive_zeros = consecutive_zeros;
1575 best_start = i - consecutive_zeros;
1577 i -= 2;
1581 /* So long as it won't require any more insns to do so, it's
1582 desirable to emit a small constant (in bits 0...9) in the last
1583 insn. This way there is more chance that it can be combined with
1584 a later addressing insn to form a pre-indexed load or store
1585 operation. Consider:
1587 *((volatile int *)0xe0000100) = 1;
1588 *((volatile int *)0xe0000110) = 2;
1590 We want this to wind up as:
1592 mov rA, #0xe0000000
1593 mov rB, #1
1594 str rB, [rA, #0x100]
1595 mov rB, #2
1596 str rB, [rA, #0x110]
1598 rather than having to synthesize both large constants from scratch.
1600 Therefore, we calculate how many insns would be required to emit
1601 the constant starting from `best_start', and also starting from
1602 zero (ie with bit 31 first to be output). If `best_start' doesn't
1603 yield a shorter sequence, we may as well use zero. */
1604 if (best_start != 0
1605 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1606 && (count_insns_for_constant (remainder, 0) <=
1607 count_insns_for_constant (remainder, best_start)))
1608 best_start = 0;
1610 /* Now start emitting the insns. */
1611 i = best_start;
1614 int end;
1616 if (i <= 0)
1617 i += 32;
1618 if (remainder & (3 << (i - 2)))
1620 end = i - 8;
1621 if (end < 0)
1622 end += 32;
1623 temp1 = remainder & ((0x0ff << end)
1624 | ((i < end) ? (0xff >> (32 - end)) : 0));
1625 remainder &= ~temp1;
1627 if (generate)
1629 rtx new_src;
1631 if (code == SET)
1632 emit_insn (gen_rtx_SET (VOIDmode,
1633 new_src = (subtargets
1634 ? gen_reg_rtx (mode)
1635 : target),
1636 GEN_INT (can_invert
1637 ? ~temp1 : temp1)));
1638 else if (code == MINUS)
1639 emit_insn (gen_rtx_SET (VOIDmode,
1640 new_src = (subtargets
1641 ? gen_reg_rtx (mode)
1642 : target),
1643 gen_rtx (code, mode, GEN_INT (temp1),
1644 source)));
1645 else
1646 emit_insn (gen_rtx_SET (VOIDmode,
1647 new_src = (remainder
1648 ? (subtargets
1649 ? gen_reg_rtx (mode)
1650 : target)
1651 : target),
1652 gen_rtx (code, mode, source,
1653 GEN_INT (can_invert ? ~temp1
1654 : (can_negate
1655 ? -temp1
1656 : temp1)))));
1657 source = new_src;
1660 if (code == SET)
1662 can_invert = 0;
1663 code = PLUS;
1665 else if (code == MINUS)
1666 code = PLUS;
1668 insns++;
1669 i -= 6;
1671 i -= 2;
1672 } while (remainder);
1674 return insns;
1677 /* Canonicalize a comparison so that we are more likely to recognize it.
1678 This can be done for a few constant compares, where we can make the
1679 immediate value easier to load. */
1680 enum rtx_code
1681 arm_canonicalize_comparison (code, op1)
1682 enum rtx_code code;
1683 rtx * op1;
1685 unsigned HOST_WIDE_INT i = INTVAL (*op1);
1687 switch (code)
1689 case EQ:
1690 case NE:
1691 return code;
1693 case GT:
1694 case LE:
1695 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1696 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1698 *op1 = GEN_INT (i + 1);
1699 return code == GT ? GE : LT;
1701 break;
1703 case GE:
1704 case LT:
1705 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1706 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1708 *op1 = GEN_INT (i - 1);
1709 return code == GE ? GT : LE;
1711 break;
1713 case GTU:
1714 case LEU:
1715 if (i != ~((unsigned HOST_WIDE_INT) 0)
1716 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1718 *op1 = GEN_INT (i + 1);
1719 return code == GTU ? GEU : LTU;
1721 break;
1723 case GEU:
1724 case LTU:
1725 if (i != 0
1726 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1728 *op1 = GEN_INT (i - 1);
1729 return code == GEU ? GTU : LEU;
1731 break;
1733 default:
1734 abort ();
1737 return code;
1740 /* Decide whether a type should be returned in memory (true)
1741 or in a register (false). This is called by the macro
1742 RETURN_IN_MEMORY. */
1744 arm_return_in_memory (type)
1745 tree type;
1747 if (!AGGREGATE_TYPE_P (type))
1748 /* All simple types are returned in registers. */
1749 return 0;
1751 /* For the arm-wince targets we choose to be compitable with Microsoft's
1752 ARM and Thumb compilers, which always return aggregates in memory. */
1753 #ifndef ARM_WINCE
1754 /* All structures/unions bigger than one word are returned in memory.
1755 Also catch the case where int_size_in_bytes returns -1. In this case
1756 the aggregate is either huge or of varaible size, and in either case
1757 we will want to return it via memory and not in a register. */
1758 if (((unsigned int) int_size_in_bytes (type)) > UNITS_PER_WORD)
1759 return 1;
1761 if (TREE_CODE (type) == RECORD_TYPE)
1763 tree field;
1765 /* For a struct the APCS says that we only return in a register
1766 if the type is 'integer like' and every addressable element
1767 has an offset of zero. For practical purposes this means
1768 that the structure can have at most one non bit-field element
1769 and that this element must be the first one in the structure. */
1771 /* Find the first field, ignoring non FIELD_DECL things which will
1772 have been created by C++. */
1773 for (field = TYPE_FIELDS (type);
1774 field && TREE_CODE (field) != FIELD_DECL;
1775 field = TREE_CHAIN (field))
1776 continue;
1778 if (field == NULL)
1779 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
1781 /* Check that the first field is valid for returning in a register. */
1783 /* ... Floats are not allowed */
1784 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1785 return 1;
1787 /* ... Aggregates that are not themselves valid for returning in
1788 a register are not allowed. */
1789 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1790 return 1;
1792 /* Now check the remaining fields, if any. Only bitfields are allowed,
1793 since they are not addressable. */
1794 for (field = TREE_CHAIN (field);
1795 field;
1796 field = TREE_CHAIN (field))
1798 if (TREE_CODE (field) != FIELD_DECL)
1799 continue;
1801 if (!DECL_BIT_FIELD_TYPE (field))
1802 return 1;
1805 return 0;
1808 if (TREE_CODE (type) == UNION_TYPE)
1810 tree field;
1812 /* Unions can be returned in registers if every element is
1813 integral, or can be returned in an integer register. */
1814 for (field = TYPE_FIELDS (type);
1815 field;
1816 field = TREE_CHAIN (field))
1818 if (TREE_CODE (field) != FIELD_DECL)
1819 continue;
1821 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1822 return 1;
1824 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1825 return 1;
1828 return 0;
1830 #endif /* not ARM_WINCE */
1832 /* Return all other types in memory. */
1833 return 1;
1836 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1837 for a call to a function whose data type is FNTYPE.
1838 For a library call, FNTYPE is NULL. */
1839 void
1840 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1841 CUMULATIVE_ARGS * pcum;
1842 tree fntype;
1843 rtx libname ATTRIBUTE_UNUSED;
1844 int indirect ATTRIBUTE_UNUSED;
1846 /* On the ARM, the offset starts at 0. */
1847 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype))) ? 1 : 0);
1849 pcum->call_cookie = CALL_NORMAL;
1851 if (TARGET_LONG_CALLS)
1852 pcum->call_cookie = CALL_LONG;
1854 /* Check for long call/short call attributes. The attributes
1855 override any command line option. */
1856 if (fntype)
1858 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1859 pcum->call_cookie = CALL_SHORT;
1860 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1861 pcum->call_cookie = CALL_LONG;
1865 /* Determine where to put an argument to a function.
1866 Value is zero to push the argument on the stack,
1867 or a hard register in which to store the argument.
1869 MODE is the argument's machine mode.
1870 TYPE is the data type of the argument (as a tree).
1871 This is null for libcalls where that information may
1872 not be available.
1873 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1874 the preceding args and about the function being called.
1875 NAMED is nonzero if this argument is a named parameter
1876 (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;
1926 /* Table of machine attributes. */
1927 const struct attribute_spec arm_attribute_table[] =
1929 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1930 /* Function calls made to this symbol must be done indirectly, because
1931 it may lie outside of the 26 bit addressing range of a normal function
1932 call. */
1933 { "long_call", 0, 0, false, true, true, NULL },
1934 /* Whereas these functions are always known to reside within the 26 bit
1935 addressing range. */
1936 { "short_call", 0, 0, false, true, true, NULL },
1937 /* Interrupt Service Routines have special prologue and epilogue requirements. */
1938 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
1939 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
1940 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
1941 #ifdef ARM_PE
1942 /* ARM/PE has three new attributes:
1943 interfacearm - ?
1944 dllexport - for exporting a function/variable that will live in a dll
1945 dllimport - for importing a function/variable from a dll
1947 Microsoft allows multiple declspecs in one __declspec, separating
1948 them with spaces. We do NOT support this. Instead, use __declspec
1949 multiple times.
1951 { "dllimport", 0, 0, true, false, false, NULL },
1952 { "dllexport", 0, 0, true, false, false, NULL },
1953 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
1954 #endif
1955 { NULL, 0, 0, false, false, false, NULL }
1958 /* Handle an attribute requiring a FUNCTION_DECL;
1959 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. */
1980 static tree
1981 arm_handle_isr_attribute (node, name, args, flags, no_add_attrs)
1982 tree *node;
1983 tree name;
1984 tree args;
1985 int flags;
1986 bool *no_add_attrs;
1988 if (DECL_P (*node))
1990 if (TREE_CODE (*node) != FUNCTION_DECL)
1992 warning ("`%s' attribute only applies to functions",
1993 IDENTIFIER_POINTER (name));
1994 *no_add_attrs = true;
1996 /* FIXME: the argument if any is checked for type attributes;
1997 should it be checked for decl ones? */
1999 else
2001 if (TREE_CODE (*node) == FUNCTION_TYPE
2002 || TREE_CODE (*node) == METHOD_TYPE)
2004 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2006 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2007 *no_add_attrs = true;
2010 else if (TREE_CODE (*node) == POINTER_TYPE
2011 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2012 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2013 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2015 *node = build_type_copy (*node);
2016 TREE_TYPE (*node) = build_type_attribute_variant (TREE_TYPE (*node),
2017 tree_cons (name,
2018 args,
2019 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). */
2045 static int
2046 arm_comp_type_attributes (type1, type2)
2047 tree type1;
2048 tree type2;
2050 int l1, l2, s1, s2;
2052 /* Check for mismatch of non-default calling convention. */
2053 if (TREE_CODE (type1) != FUNCTION_TYPE)
2054 return 1;
2056 /* Check for mismatched call attributes. */
2057 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2058 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2059 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2060 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2062 /* Only bother to check if an attribute is defined. */
2063 if (l1 | l2 | s1 | s2)
2065 /* If one type has an attribute, the other must have the same attribute. */
2066 if ((l1 != l2) || (s1 != s2))
2067 return 0;
2069 /* Disallow mixed attributes. */
2070 if ((l1 & s2) || (l2 & s1))
2071 return 0;
2074 /* Check for mismatched ISR attribute. */
2075 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2076 if (! l1)
2077 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2078 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2079 if (! l2)
2080 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2081 if (l1 != l2)
2082 return 0;
2084 return 1;
2087 /* Encode long_call or short_call attribute by prefixing
2088 symbol name in DECL with a special character FLAG. */
2089 void
2090 arm_encode_call_attribute (decl, flag)
2091 tree decl;
2092 int flag;
2094 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2095 int len = strlen (str);
2096 char * newstr;
2098 if (TREE_CODE (decl) != FUNCTION_DECL)
2099 return;
2101 /* Do not allow weak functions to be treated as short call. */
2102 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2103 return;
2105 newstr = alloca (len + 2);
2106 newstr[0] = flag;
2107 strcpy (newstr + 1, str);
2109 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2110 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2113 /* Assigns default attributes to newly defined type. This is used to
2114 set short_call/long_call attributes for function types of
2115 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. */
2143 static int
2144 current_file_function_operand (sym_ref)
2145 rtx sym_ref;
2147 /* This is a bit of a fib. A function will have a short call flag
2148 applied to its name if it has the short call attribute, or it has
2149 already been defined within the current compilation unit. */
2150 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2151 return 1;
2153 /* The current function is always defined within the current compilation
2154 unit. if it s a weak definition however, then this may not be the real
2155 definition of the function, and so we have to say no. */
2156 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2157 && !DECL_WEAK (current_function_decl))
2158 return 1;
2160 /* We cannot make the determination - default to returning 0. */
2161 return 0;
2164 /* Return non-zero if a 32 bit "long_call" should be generated for
2165 this call. We generate a long_call if the function:
2167 a. has an __attribute__((long call))
2168 or b. is within the scope of a #pragma long_calls
2169 or c. the -mlong-calls command line switch has been specified
2171 However we do not generate a long call if the function:
2173 d. has an __attribute__ ((short_call))
2174 or e. is inside the scope of a #pragma no_long_calls
2175 or f. has an __attribute__ ((section))
2176 or g. is defined within the current compilation unit.
2178 This function will be called by C fragments contained in the machine
2179 description file. CALL_REF and CALL_COOKIE correspond to the matched
2180 rtl operands. CALL_SYMBOL is used to distinguish between
2181 two different callers of the function. It is set to 1 in the
2182 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2183 and "call_value" patterns. This is because of the difference in the
2184 SYM_REFs passed by these patterns. */
2186 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
2187 rtx sym_ref;
2188 int call_cookie;
2189 int call_symbol;
2191 if (!call_symbol)
2193 if (GET_CODE (sym_ref) != MEM)
2194 return 0;
2196 sym_ref = XEXP (sym_ref, 0);
2199 if (GET_CODE (sym_ref) != SYMBOL_REF)
2200 return 0;
2202 if (call_cookie & CALL_SHORT)
2203 return 0;
2205 if (TARGET_LONG_CALLS && flag_function_sections)
2206 return 1;
2208 if (current_file_function_operand (sym_ref))
2209 return 0;
2211 return (call_cookie & CALL_LONG)
2212 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2213 || TARGET_LONG_CALLS;
2216 /* Return non-zero if it is ok to make a tail-call to DECL. */
2218 arm_function_ok_for_sibcall (decl)
2219 tree decl;
2221 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2223 /* Never tailcall something for which we have no decl, or if we
2224 are in Thumb mode. */
2225 if (decl == NULL || TARGET_THUMB)
2226 return 0;
2228 /* Get the calling method. */
2229 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2230 call_type = CALL_SHORT;
2231 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2232 call_type = CALL_LONG;
2234 /* Cannot tail-call to long calls, since these are out of range of
2235 a branch instruction. However, if not compiling PIC, we know
2236 we can reach the symbol if it is in this compilation unit. */
2237 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2238 return 0;
2240 /* If we are interworking and the function is not declared static
2241 then we can't tail-call it unless we know that it exists in this
2242 compilation unit (since it might be a Thumb routine). */
2243 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2244 return 0;
2246 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2247 if (IS_INTERRUPT (arm_current_func_type ()))
2248 return 0;
2250 /* Everything else is ok. */
2251 return 1;
2256 legitimate_pic_operand_p (x)
2257 rtx x;
2259 if (CONSTANT_P (x)
2260 && flag_pic
2261 && (GET_CODE (x) == SYMBOL_REF
2262 || (GET_CODE (x) == CONST
2263 && GET_CODE (XEXP (x, 0)) == PLUS
2264 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2265 return 0;
2267 return 1;
2271 legitimize_pic_address (orig, mode, reg)
2272 rtx orig;
2273 enum machine_mode mode;
2274 rtx reg;
2276 if (GET_CODE (orig) == SYMBOL_REF
2277 || GET_CODE (orig) == LABEL_REF)
2279 #ifndef AOF_ASSEMBLER
2280 rtx pic_ref, address;
2281 #endif
2282 rtx insn;
2283 int subregs = 0;
2285 if (reg == 0)
2287 if (no_new_pseudos)
2288 abort ();
2289 else
2290 reg = gen_reg_rtx (Pmode);
2292 subregs = 1;
2295 #ifdef AOF_ASSEMBLER
2296 /* The AOF assembler can generate relocations for these directly, and
2297 understands that the PIC register has to be added into the offset. */
2298 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2299 #else
2300 if (subregs)
2301 address = gen_reg_rtx (Pmode);
2302 else
2303 address = reg;
2305 if (TARGET_ARM)
2306 emit_insn (gen_pic_load_addr_arm (address, orig));
2307 else
2308 emit_insn (gen_pic_load_addr_thumb (address, orig));
2310 if (GET_CODE (orig) == LABEL_REF && NEED_GOT_RELOC)
2311 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2312 else
2314 pic_ref = gen_rtx_MEM (Pmode,
2315 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2316 address));
2317 RTX_UNCHANGING_P (pic_ref) = 1;
2320 insn = emit_move_insn (reg, pic_ref);
2321 #endif
2322 current_function_uses_pic_offset_table = 1;
2323 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2324 by loop. */
2325 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2326 REG_NOTES (insn));
2327 return reg;
2329 else if (GET_CODE (orig) == CONST)
2331 rtx base, offset;
2333 if (GET_CODE (XEXP (orig, 0)) == PLUS
2334 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2335 return orig;
2337 if (reg == 0)
2339 if (no_new_pseudos)
2340 abort ();
2341 else
2342 reg = gen_reg_rtx (Pmode);
2345 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2347 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2348 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2349 base == reg ? 0 : reg);
2351 else
2352 abort ();
2354 if (GET_CODE (offset) == CONST_INT)
2356 /* The base register doesn't really matter, we only want to
2357 test the index for the appropriate mode. */
2358 ARM_GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
2360 if (!no_new_pseudos)
2361 offset = force_reg (Pmode, offset);
2362 else
2363 abort ();
2365 win:
2366 if (GET_CODE (offset) == CONST_INT)
2367 return plus_constant (base, INTVAL (offset));
2370 if (GET_MODE_SIZE (mode) > 4
2371 && (GET_MODE_CLASS (mode) == MODE_INT
2372 || TARGET_SOFT_FLOAT))
2374 emit_insn (gen_addsi3 (reg, base, offset));
2375 return reg;
2378 return gen_rtx_PLUS (Pmode, base, offset);
2381 return orig;
2384 /* Generate code to load the PIC register. PROLOGUE is true if
2385 called from arm_expand_prologue (in which case we want the
2386 generated insns at the start of the function); false if called
2387 by an exception receiver that needs the PIC register reloaded
2388 (in which case the insns are just dumped at the current location). */
2390 void
2391 arm_finalize_pic (prologue)
2392 int prologue ATTRIBUTE_UNUSED;
2394 #ifndef AOF_ASSEMBLER
2395 rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2396 rtx global_offset_table;
2398 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2399 return;
2401 if (!flag_pic)
2402 abort ();
2404 start_sequence ();
2405 l1 = gen_label_rtx ();
2407 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2408 /* On the ARM the PC register contains 'dot + 8' at the time of the
2409 addition, on the Thumb it is 'dot + 4'. */
2410 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2411 if (GOT_PCREL)
2412 pic_tmp2 = gen_rtx_CONST (VOIDmode,
2413 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2414 else
2415 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2417 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2419 if (TARGET_ARM)
2421 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2422 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2424 else
2426 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2427 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2430 seq = gen_sequence ();
2431 end_sequence ();
2432 if (prologue)
2433 emit_insn_after (seq, get_insns ());
2434 else
2435 emit_insn (seq);
2437 /* Need to emit this whether or not we obey regdecls,
2438 since setjmp/longjmp can cause life info to screw up. */
2439 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2440 #endif /* AOF_ASSEMBLER */
2443 #define REG_OR_SUBREG_REG(X) \
2444 (GET_CODE (X) == REG \
2445 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
2447 #define REG_OR_SUBREG_RTX(X) \
2448 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
2450 #ifndef COSTS_N_INSNS
2451 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
2452 #endif
2455 arm_rtx_costs (x, code, outer)
2456 rtx x;
2457 enum rtx_code code;
2458 enum rtx_code outer;
2460 enum machine_mode mode = GET_MODE (x);
2461 enum rtx_code subcode;
2462 int extra_cost;
2464 if (TARGET_THUMB)
2466 switch (code)
2468 case ASHIFT:
2469 case ASHIFTRT:
2470 case LSHIFTRT:
2471 case ROTATERT:
2472 case PLUS:
2473 case MINUS:
2474 case COMPARE:
2475 case NEG:
2476 case NOT:
2477 return COSTS_N_INSNS (1);
2479 case MULT:
2480 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2482 int cycles = 0;
2483 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
2485 while (i)
2487 i >>= 2;
2488 cycles++;
2490 return COSTS_N_INSNS (2) + cycles;
2492 return COSTS_N_INSNS (1) + 16;
2494 case SET:
2495 return (COSTS_N_INSNS (1)
2496 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
2497 + GET_CODE (SET_DEST (x)) == MEM));
2499 case CONST_INT:
2500 if (outer == SET)
2502 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2503 return 0;
2504 if (thumb_shiftable_const (INTVAL (x)))
2505 return COSTS_N_INSNS (2);
2506 return COSTS_N_INSNS (3);
2508 else if (outer == PLUS
2509 && INTVAL (x) < 256 && INTVAL (x) > -256)
2510 return 0;
2511 else if (outer == COMPARE
2512 && (unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2513 return 0;
2514 else if (outer == ASHIFT || outer == ASHIFTRT
2515 || outer == LSHIFTRT)
2516 return 0;
2517 return COSTS_N_INSNS (2);
2519 case CONST:
2520 case CONST_DOUBLE:
2521 case LABEL_REF:
2522 case SYMBOL_REF:
2523 return COSTS_N_INSNS (3);
2525 case UDIV:
2526 case UMOD:
2527 case DIV:
2528 case MOD:
2529 return 100;
2531 case TRUNCATE:
2532 return 99;
2534 case AND:
2535 case XOR:
2536 case IOR:
2537 /* XXX guess. */
2538 return 8;
2540 case ADDRESSOF:
2541 case MEM:
2542 /* XXX another guess. */
2543 /* Memory costs quite a lot for the first word, but subsequent words
2544 load at the equivalent of a single insn each. */
2545 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2546 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2548 case IF_THEN_ELSE:
2549 /* XXX a guess. */
2550 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2551 return 14;
2552 return 2;
2554 case ZERO_EXTEND:
2555 /* XXX still guessing. */
2556 switch (GET_MODE (XEXP (x, 0)))
2558 case QImode:
2559 return (1 + (mode == DImode ? 4 : 0)
2560 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2562 case HImode:
2563 return (4 + (mode == DImode ? 4 : 0)
2564 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2566 case SImode:
2567 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2569 default:
2570 return 99;
2573 default:
2574 return 99;
2575 #if 0
2576 case FFS:
2577 case FLOAT:
2578 case FIX:
2579 case UNSIGNED_FIX:
2580 /* XXX guess */
2581 fprintf (stderr, "unexpected code for thumb in rtx_costs: %s\n",
2582 rtx_name[code]);
2583 abort ();
2584 #endif
2588 switch (code)
2590 case MEM:
2591 /* Memory costs quite a lot for the first word, but subsequent words
2592 load at the equivalent of a single insn each. */
2593 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2594 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2596 case DIV:
2597 case MOD:
2598 return 100;
2600 case ROTATE:
2601 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
2602 return 4;
2603 /* Fall through */
2604 case ROTATERT:
2605 if (mode != SImode)
2606 return 8;
2607 /* Fall through */
2608 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
2609 if (mode == DImode)
2610 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
2611 + ((GET_CODE (XEXP (x, 0)) == REG
2612 || (GET_CODE (XEXP (x, 0)) == SUBREG
2613 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2614 ? 0 : 8));
2615 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
2616 || (GET_CODE (XEXP (x, 0)) == SUBREG
2617 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2618 ? 0 : 4)
2619 + ((GET_CODE (XEXP (x, 1)) == REG
2620 || (GET_CODE (XEXP (x, 1)) == SUBREG
2621 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
2622 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
2623 ? 0 : 4));
2625 case MINUS:
2626 if (mode == DImode)
2627 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
2628 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2629 || (GET_CODE (XEXP (x, 0)) == CONST_INT
2630 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
2631 ? 0 : 8));
2633 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2634 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2635 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2636 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2637 ? 0 : 8)
2638 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2639 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
2640 && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
2641 ? 0 : 8));
2643 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
2644 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
2645 && REG_OR_SUBREG_REG (XEXP (x, 1))))
2646 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
2647 || subcode == ASHIFTRT || subcode == LSHIFTRT
2648 || subcode == ROTATE || subcode == ROTATERT
2649 || (subcode == MULT
2650 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
2651 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
2652 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
2653 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
2654 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
2655 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
2656 && REG_OR_SUBREG_REG (XEXP (x, 0))))
2657 return 1;
2658 /* Fall through */
2660 case PLUS:
2661 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2662 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2663 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2664 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2665 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2666 ? 0 : 8));
2668 /* Fall through */
2669 case AND: case XOR: case IOR:
2670 extra_cost = 0;
2672 /* Normally the frame registers will be spilt into reg+const during
2673 reload, so it is a bad idea to combine them with other instructions,
2674 since then they might not be moved outside of loops. As a compromise
2675 we allow integration with ops that have a constant as their second
2676 operand. */
2677 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
2678 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
2679 && GET_CODE (XEXP (x, 1)) != CONST_INT)
2680 || (REG_OR_SUBREG_REG (XEXP (x, 0))
2681 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
2682 extra_cost = 4;
2684 if (mode == DImode)
2685 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2686 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2687 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2688 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2689 ? 0 : 8));
2691 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
2692 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
2693 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2694 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2695 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2696 ? 0 : 4));
2698 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
2699 return (1 + extra_cost
2700 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
2701 || subcode == LSHIFTRT || subcode == ASHIFTRT
2702 || subcode == ROTATE || subcode == ROTATERT
2703 || (subcode == MULT
2704 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2705 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
2706 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
2707 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
2708 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
2709 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
2710 ? 0 : 4));
2712 return 8;
2714 case MULT:
2715 /* There is no point basing this on the tuning, since it is always the
2716 fast variant if it exists at all. */
2717 if (arm_fast_multiply && mode == DImode
2718 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
2719 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2720 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
2721 return 8;
2723 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2724 || mode == DImode)
2725 return 30;
2727 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2729 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
2730 & (unsigned HOST_WIDE_INT) 0xffffffff);
2731 int add_cost = const_ok_for_arm (i) ? 4 : 8;
2732 int j;
2734 /* Tune as appropriate. */
2735 int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
2737 for (j = 0; i && j < 32; j += booth_unit_size)
2739 i >>= booth_unit_size;
2740 add_cost += 2;
2743 return add_cost;
2746 return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
2747 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
2748 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
2750 case TRUNCATE:
2751 if (arm_fast_multiply && mode == SImode
2752 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
2753 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2754 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
2755 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
2756 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
2757 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
2758 return 8;
2759 return 99;
2761 case NEG:
2762 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2763 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
2764 /* Fall through */
2765 case NOT:
2766 if (mode == DImode)
2767 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2769 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2771 case IF_THEN_ELSE:
2772 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2773 return 14;
2774 return 2;
2776 case COMPARE:
2777 return 1;
2779 case ABS:
2780 return 4 + (mode == DImode ? 4 : 0);
2782 case SIGN_EXTEND:
2783 if (GET_MODE (XEXP (x, 0)) == QImode)
2784 return (4 + (mode == DImode ? 4 : 0)
2785 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2786 /* Fall through */
2787 case ZERO_EXTEND:
2788 switch (GET_MODE (XEXP (x, 0)))
2790 case QImode:
2791 return (1 + (mode == DImode ? 4 : 0)
2792 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2794 case HImode:
2795 return (4 + (mode == DImode ? 4 : 0)
2796 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2798 case SImode:
2799 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2801 default:
2802 break;
2804 abort ();
2806 case CONST_INT:
2807 if (const_ok_for_arm (INTVAL (x)))
2808 return outer == SET ? 2 : -1;
2809 else if (outer == AND
2810 && const_ok_for_arm (~INTVAL (x)))
2811 return -1;
2812 else if ((outer == COMPARE
2813 || outer == PLUS || outer == MINUS)
2814 && const_ok_for_arm (-INTVAL (x)))
2815 return -1;
2816 else
2817 return 5;
2819 case CONST:
2820 case LABEL_REF:
2821 case SYMBOL_REF:
2822 return 6;
2824 case CONST_DOUBLE:
2825 if (const_double_rtx_ok_for_fpu (x))
2826 return outer == SET ? 2 : -1;
2827 else if ((outer == COMPARE || outer == PLUS)
2828 && neg_const_double_rtx_ok_for_fpu (x))
2829 return -1;
2830 return 7;
2832 default:
2833 return 99;
2837 static int
2838 arm_adjust_cost (insn, link, dep, cost)
2839 rtx insn;
2840 rtx link;
2841 rtx dep;
2842 int cost;
2844 rtx i_pat, d_pat;
2846 /* Some true dependencies can have a higher cost depending
2847 on precisely how certain input operands are used. */
2848 if (arm_is_xscale
2849 && REG_NOTE_KIND (link) == 0
2850 && recog_memoized (insn) < 0
2851 && recog_memoized (dep) < 0)
2853 int shift_opnum = get_attr_shift (insn);
2854 enum attr_type attr_type = get_attr_type (dep);
2856 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
2857 operand for INSN. If we have a shifted input operand and the
2858 instruction we depend on is another ALU instruction, then we may
2859 have to account for an additional stall. */
2860 if (shift_opnum != 0 && attr_type == TYPE_NORMAL)
2862 rtx shifted_operand;
2863 int opno;
2865 /* Get the shifted operand. */
2866 extract_insn (insn);
2867 shifted_operand = recog_data.operand[shift_opnum];
2869 /* Iterate over all the operands in DEP. If we write an operand
2870 that overlaps with SHIFTED_OPERAND, then we have increase the
2871 cost of this dependency. */
2872 extract_insn (dep);
2873 preprocess_constraints ();
2874 for (opno = 0; opno < recog_data.n_operands; opno++)
2876 /* We can ignore strict inputs. */
2877 if (recog_data.operand_type[opno] == OP_IN)
2878 continue;
2880 if (reg_overlap_mentioned_p (recog_data.operand[opno],
2881 shifted_operand))
2882 return 2;
2887 /* XXX This is not strictly true for the FPA. */
2888 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
2889 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
2890 return 0;
2892 /* Call insns don't incur a stall, even if they follow a load. */
2893 if (REG_NOTE_KIND (link) == 0
2894 && GET_CODE (insn) == CALL_INSN)
2895 return 1;
2897 if ((i_pat = single_set (insn)) != NULL
2898 && GET_CODE (SET_SRC (i_pat)) == MEM
2899 && (d_pat = single_set (dep)) != NULL
2900 && GET_CODE (SET_DEST (d_pat)) == MEM)
2902 /* This is a load after a store, there is no conflict if the load reads
2903 from a cached area. Assume that loads from the stack, and from the
2904 constant pool are cached, and that others will miss. This is a
2905 hack. */
2907 if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
2908 || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2909 || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2910 || reg_mentioned_p (hard_frame_pointer_rtx,
2911 XEXP (SET_SRC (i_pat), 0)))
2912 return 1;
2915 return cost;
2918 /* This code has been fixed for cross compilation. */
2920 static int fpa_consts_inited = 0;
2922 static const char *const strings_fpa[8] =
2924 "0", "1", "2", "3",
2925 "4", "5", "0.5", "10"
2928 static REAL_VALUE_TYPE values_fpa[8];
2930 static void
2931 init_fpa_table ()
2933 int i;
2934 REAL_VALUE_TYPE r;
2936 for (i = 0; i < 8; i++)
2938 r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
2939 values_fpa[i] = r;
2942 fpa_consts_inited = 1;
2945 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2948 const_double_rtx_ok_for_fpu (x)
2949 rtx x;
2951 REAL_VALUE_TYPE r;
2952 int i;
2954 if (!fpa_consts_inited)
2955 init_fpa_table ();
2957 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2958 if (REAL_VALUE_MINUS_ZERO (r))
2959 return 0;
2961 for (i = 0; i < 8; i++)
2962 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2963 return 1;
2965 return 0;
2968 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2971 neg_const_double_rtx_ok_for_fpu (x)
2972 rtx x;
2974 REAL_VALUE_TYPE r;
2975 int i;
2977 if (!fpa_consts_inited)
2978 init_fpa_table ();
2980 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2981 r = REAL_VALUE_NEGATE (r);
2982 if (REAL_VALUE_MINUS_ZERO (r))
2983 return 0;
2985 for (i = 0; i < 8; i++)
2986 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2987 return 1;
2989 return 0;
2992 /* Predicates for `match_operand' and `match_operator'. */
2994 /* s_register_operand is the same as register_operand, but it doesn't accept
2995 (SUBREG (MEM)...).
2997 This function exists because at the time it was put in it led to better
2998 code. SUBREG(MEM) always needs a reload in the places where
2999 s_register_operand is used, and this seemed to lead to excessive
3000 reloading. */
3003 s_register_operand (op, mode)
3004 register rtx op;
3005 enum machine_mode mode;
3007 if (GET_MODE (op) != mode && mode != VOIDmode)
3008 return 0;
3010 if (GET_CODE (op) == SUBREG)
3011 op = SUBREG_REG (op);
3013 /* We don't consider registers whose class is NO_REGS
3014 to be a register operand. */
3015 /* XXX might have to check for lo regs only for thumb ??? */
3016 return (GET_CODE (op) == REG
3017 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3018 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3021 /* A hard register operand (even before reload. */
3023 arm_hard_register_operand (op, mode)
3024 register rtx op;
3025 enum machine_mode mode;
3027 if (GET_MODE (op) != mode && mode != VOIDmode)
3028 return 0;
3030 return (GET_CODE (op) == REG
3031 && REGNO (op) < FIRST_PSEUDO_REGISTER);
3034 /* Only accept reg, subreg(reg), const_int. */
3037 reg_or_int_operand (op, mode)
3038 register rtx op;
3039 enum machine_mode mode;
3041 if (GET_CODE (op) == CONST_INT)
3042 return 1;
3044 if (GET_MODE (op) != mode && mode != VOIDmode)
3045 return 0;
3047 if (GET_CODE (op) == SUBREG)
3048 op = SUBREG_REG (op);
3050 /* We don't consider registers whose class is NO_REGS
3051 to be a register operand. */
3052 return (GET_CODE (op) == REG
3053 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3054 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3057 /* Return 1 if OP is an item in memory, given that we are in reload. */
3060 arm_reload_memory_operand (op, mode)
3061 rtx op;
3062 enum machine_mode mode ATTRIBUTE_UNUSED;
3064 int regno = true_regnum (op);
3066 return (!CONSTANT_P (op)
3067 && (regno == -1
3068 || (GET_CODE (op) == REG
3069 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
3072 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
3073 memory access (architecture V4).
3074 MODE is QImode if called when computing constraints, or VOIDmode when
3075 emitting patterns. In this latter case we cannot use memory_operand()
3076 because it will fail on badly formed MEMs, which is precisly what we are
3077 trying to catch. */
3079 bad_signed_byte_operand (op, mode)
3080 rtx op;
3081 enum machine_mode mode ATTRIBUTE_UNUSED;
3083 #if 0
3084 if ((mode == QImode && !memory_operand (op, mode)) || GET_CODE (op) != MEM)
3085 return 0;
3086 #endif
3087 if (GET_CODE (op) != MEM)
3088 return 0;
3090 op = XEXP (op, 0);
3092 /* A sum of anything more complex than reg + reg or reg + const is bad. */
3093 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3094 && (!s_register_operand (XEXP (op, 0), VOIDmode)
3095 || (!s_register_operand (XEXP (op, 1), VOIDmode)
3096 && GET_CODE (XEXP (op, 1)) != CONST_INT)))
3097 return 1;
3099 /* Big constants are also bad. */
3100 if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
3101 && (INTVAL (XEXP (op, 1)) > 0xff
3102 || -INTVAL (XEXP (op, 1)) > 0xff))
3103 return 1;
3105 /* Everything else is good, or can will automatically be made so. */
3106 return 0;
3109 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
3112 arm_rhs_operand (op, mode)
3113 rtx op;
3114 enum machine_mode mode;
3116 return (s_register_operand (op, mode)
3117 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
3120 /* Return TRUE for valid operands for the rhs of an ARM instruction, or a load.
3124 arm_rhsm_operand (op, mode)
3125 rtx op;
3126 enum machine_mode mode;
3128 return (s_register_operand (op, mode)
3129 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
3130 || memory_operand (op, mode));
3133 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
3134 constant that is valid when negated. */
3137 arm_add_operand (op, mode)
3138 rtx op;
3139 enum machine_mode mode;
3141 if (TARGET_THUMB)
3142 return thumb_cmp_operand (op, mode);
3144 return (s_register_operand (op, mode)
3145 || (GET_CODE (op) == CONST_INT
3146 && (const_ok_for_arm (INTVAL (op))
3147 || const_ok_for_arm (-INTVAL (op)))));
3151 arm_not_operand (op, mode)
3152 rtx op;
3153 enum machine_mode mode;
3155 return (s_register_operand (op, mode)
3156 || (GET_CODE (op) == CONST_INT
3157 && (const_ok_for_arm (INTVAL (op))
3158 || const_ok_for_arm (~INTVAL (op)))));
3161 /* Return TRUE if the operand is a memory reference which contains an
3162 offsettable address. */
3164 offsettable_memory_operand (op, mode)
3165 register rtx op;
3166 enum machine_mode mode;
3168 if (mode == VOIDmode)
3169 mode = GET_MODE (op);
3171 return (mode == GET_MODE (op)
3172 && GET_CODE (op) == MEM
3173 && offsettable_address_p (reload_completed | reload_in_progress,
3174 mode, XEXP (op, 0)));
3177 /* Return TRUE if the operand is a memory reference which is, or can be
3178 made word aligned by adjusting the offset. */
3180 alignable_memory_operand (op, mode)
3181 register rtx op;
3182 enum machine_mode mode;
3184 rtx reg;
3186 if (mode == VOIDmode)
3187 mode = GET_MODE (op);
3189 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
3190 return 0;
3192 op = XEXP (op, 0);
3194 return ((GET_CODE (reg = op) == REG
3195 || (GET_CODE (op) == SUBREG
3196 && GET_CODE (reg = SUBREG_REG (op)) == REG)
3197 || (GET_CODE (op) == PLUS
3198 && GET_CODE (XEXP (op, 1)) == CONST_INT
3199 && (GET_CODE (reg = XEXP (op, 0)) == REG
3200 || (GET_CODE (XEXP (op, 0)) == SUBREG
3201 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
3202 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
3205 /* Similar to s_register_operand, but does not allow hard integer
3206 registers. */
3208 f_register_operand (op, mode)
3209 register rtx op;
3210 enum machine_mode mode;
3212 if (GET_MODE (op) != mode && mode != VOIDmode)
3213 return 0;
3215 if (GET_CODE (op) == SUBREG)
3216 op = SUBREG_REG (op);
3218 /* We don't consider registers whose class is NO_REGS
3219 to be a register operand. */
3220 return (GET_CODE (op) == REG
3221 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3222 || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
3225 /* Return TRUE for valid operands for the rhs of an FPU instruction. */
3228 fpu_rhs_operand (op, mode)
3229 rtx op;
3230 enum machine_mode mode;
3232 if (s_register_operand (op, mode))
3233 return TRUE;
3235 if (GET_MODE (op) != mode && mode != VOIDmode)
3236 return FALSE;
3238 if (GET_CODE (op) == CONST_DOUBLE)
3239 return const_double_rtx_ok_for_fpu (op);
3241 return FALSE;
3245 fpu_add_operand (op, mode)
3246 rtx op;
3247 enum machine_mode mode;
3249 if (s_register_operand (op, mode))
3250 return TRUE;
3252 if (GET_MODE (op) != mode && mode != VOIDmode)
3253 return FALSE;
3255 if (GET_CODE (op) == CONST_DOUBLE)
3256 return (const_double_rtx_ok_for_fpu (op)
3257 || neg_const_double_rtx_ok_for_fpu (op));
3259 return FALSE;
3262 /* Return nonzero if OP is a constant power of two. */
3265 power_of_two_operand (op, mode)
3266 rtx op;
3267 enum machine_mode mode ATTRIBUTE_UNUSED;
3269 if (GET_CODE (op) == CONST_INT)
3271 HOST_WIDE_INT value = INTVAL (op);
3272 return value != 0 && (value & (value - 1)) == 0;
3274 return FALSE;
3277 /* Return TRUE for a valid operand of a DImode operation.
3278 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
3279 Note that this disallows MEM(REG+REG), but allows
3280 MEM(PRE/POST_INC/DEC(REG)). */
3283 di_operand (op, mode)
3284 rtx op;
3285 enum machine_mode mode;
3287 if (s_register_operand (op, mode))
3288 return TRUE;
3290 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
3291 return FALSE;
3293 if (GET_CODE (op) == SUBREG)
3294 op = SUBREG_REG (op);
3296 switch (GET_CODE (op))
3298 case CONST_DOUBLE:
3299 case CONST_INT:
3300 return TRUE;
3302 case MEM:
3303 return memory_address_p (DImode, XEXP (op, 0));
3305 default:
3306 return FALSE;
3310 /* Like di_operand, but don't accept constants. */
3312 nonimmediate_di_operand (op, mode)
3313 rtx op;
3314 enum machine_mode mode;
3316 if (s_register_operand (op, mode))
3317 return TRUE;
3319 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
3320 return FALSE;
3322 if (GET_CODE (op) == SUBREG)
3323 op = SUBREG_REG (op);
3325 if (GET_CODE (op) == MEM)
3326 return memory_address_p (DImode, XEXP (op, 0));
3328 return FALSE;
3331 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
3332 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
3333 Note that this disallows MEM(REG+REG), but allows
3334 MEM(PRE/POST_INC/DEC(REG)). */
3337 soft_df_operand (op, mode)
3338 rtx op;
3339 enum machine_mode mode;
3341 if (s_register_operand (op, mode))
3342 return TRUE;
3344 if (mode != VOIDmode && GET_MODE (op) != mode)
3345 return FALSE;
3347 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
3348 return FALSE;
3350 if (GET_CODE (op) == SUBREG)
3351 op = SUBREG_REG (op);
3353 switch (GET_CODE (op))
3355 case CONST_DOUBLE:
3356 return TRUE;
3358 case MEM:
3359 return memory_address_p (DFmode, XEXP (op, 0));
3361 default:
3362 return FALSE;
3366 /* Like soft_df_operand, but don't accept constants. */
3368 nonimmediate_soft_df_operand (op, mode)
3369 rtx op;
3370 enum machine_mode mode;
3372 if (s_register_operand (op, mode))
3373 return TRUE;
3375 if (mode != VOIDmode && GET_MODE (op) != mode)
3376 return FALSE;
3378 if (GET_CODE (op) == SUBREG)
3379 op = SUBREG_REG (op);
3381 if (GET_CODE (op) == MEM)
3382 return memory_address_p (DFmode, XEXP (op, 0));
3383 return FALSE;
3386 /* Return TRUE for valid index operands. */
3388 index_operand (op, mode)
3389 rtx op;
3390 enum machine_mode mode;
3392 return (s_register_operand (op, mode)
3393 || (immediate_operand (op, mode)
3394 && (GET_CODE (op) != CONST_INT
3395 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
3398 /* Return TRUE for valid shifts by a constant. This also accepts any
3399 power of two on the (somewhat overly relaxed) assumption that the
3400 shift operator in this case was a mult. */
3403 const_shift_operand (op, mode)
3404 rtx op;
3405 enum machine_mode mode;
3407 return (power_of_two_operand (op, mode)
3408 || (immediate_operand (op, mode)
3409 && (GET_CODE (op) != CONST_INT
3410 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
3413 /* Return TRUE for arithmetic operators which can be combined with a multiply
3414 (shift). */
3417 shiftable_operator (x, mode)
3418 rtx x;
3419 enum machine_mode mode;
3421 if (GET_MODE (x) != mode)
3422 return FALSE;
3423 else
3425 enum rtx_code code = GET_CODE (x);
3427 return (code == PLUS || code == MINUS
3428 || code == IOR || code == XOR || code == AND);
3432 /* Return TRUE for binary logical operators. */
3435 logical_binary_operator (x, mode)
3436 rtx x;
3437 enum machine_mode mode;
3439 if (GET_MODE (x) != mode)
3440 return FALSE;
3441 else
3443 enum rtx_code code = GET_CODE (x);
3445 return (code == IOR || code == XOR || code == AND);
3449 /* Return TRUE for shift operators. */
3452 shift_operator (x, mode)
3453 rtx x;
3454 enum machine_mode mode;
3456 if (GET_MODE (x) != mode)
3457 return FALSE;
3458 else
3460 enum rtx_code code = GET_CODE (x);
3462 if (code == MULT)
3463 return power_of_two_operand (XEXP (x, 1), mode);
3465 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
3466 || code == ROTATERT);
3470 /* Return TRUE if x is EQ or NE. */
3472 equality_operator (x, mode)
3473 rtx x;
3474 enum machine_mode mode ATTRIBUTE_UNUSED;
3476 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
3479 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ. */
3481 arm_comparison_operator (x, mode)
3482 rtx x;
3483 enum machine_mode mode;
3485 return (comparison_operator (x, mode)
3486 && GET_CODE (x) != LTGT
3487 && GET_CODE (x) != UNEQ);
3490 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
3492 minmax_operator (x, mode)
3493 rtx x;
3494 enum machine_mode mode;
3496 enum rtx_code code = GET_CODE (x);
3498 if (GET_MODE (x) != mode)
3499 return FALSE;
3501 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
3504 /* Return TRUE if this is the condition code register, if we aren't given
3505 a mode, accept any class CCmode register. */
3507 cc_register (x, mode)
3508 rtx x;
3509 enum machine_mode mode;
3511 if (mode == VOIDmode)
3513 mode = GET_MODE (x);
3515 if (GET_MODE_CLASS (mode) != MODE_CC)
3516 return FALSE;
3519 if ( GET_MODE (x) == mode
3520 && GET_CODE (x) == REG
3521 && REGNO (x) == CC_REGNUM)
3522 return TRUE;
3524 return FALSE;
3527 /* Return TRUE if this is the condition code register, if we aren't given
3528 a mode, accept any class CCmode register which indicates a dominance
3529 expression. */
3531 dominant_cc_register (x, mode)
3532 rtx x;
3533 enum machine_mode mode;
3535 if (mode == VOIDmode)
3537 mode = GET_MODE (x);
3539 if (GET_MODE_CLASS (mode) != MODE_CC)
3540 return FALSE;
3543 if ( mode != CC_DNEmode && mode != CC_DEQmode
3544 && mode != CC_DLEmode && mode != CC_DLTmode
3545 && mode != CC_DGEmode && mode != CC_DGTmode
3546 && mode != CC_DLEUmode && mode != CC_DLTUmode
3547 && mode != CC_DGEUmode && mode != CC_DGTUmode)
3548 return FALSE;
3550 return cc_register (x, mode);
3553 /* Return TRUE if X references a SYMBOL_REF. */
3555 symbol_mentioned_p (x)
3556 rtx x;
3558 register const char * fmt;
3559 register int i;
3561 if (GET_CODE (x) == SYMBOL_REF)
3562 return 1;
3564 fmt = GET_RTX_FORMAT (GET_CODE (x));
3566 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3568 if (fmt[i] == 'E')
3570 register int j;
3572 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3573 if (symbol_mentioned_p (XVECEXP (x, i, j)))
3574 return 1;
3576 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
3577 return 1;
3580 return 0;
3583 /* Return TRUE if X references a LABEL_REF. */
3585 label_mentioned_p (x)
3586 rtx x;
3588 register const char * fmt;
3589 register int i;
3591 if (GET_CODE (x) == LABEL_REF)
3592 return 1;
3594 fmt = GET_RTX_FORMAT (GET_CODE (x));
3595 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3597 if (fmt[i] == 'E')
3599 register int j;
3601 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3602 if (label_mentioned_p (XVECEXP (x, i, j)))
3603 return 1;
3605 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
3606 return 1;
3609 return 0;
3612 enum rtx_code
3613 minmax_code (x)
3614 rtx x;
3616 enum rtx_code code = GET_CODE (x);
3618 if (code == SMAX)
3619 return GE;
3620 else if (code == SMIN)
3621 return LE;
3622 else if (code == UMIN)
3623 return LEU;
3624 else if (code == UMAX)
3625 return GEU;
3627 abort ();
3630 /* Return 1 if memory locations are adjacent. */
3632 adjacent_mem_locations (a, b)
3633 rtx a, b;
3635 int val0 = 0, val1 = 0;
3636 int reg0, reg1;
3638 if ((GET_CODE (XEXP (a, 0)) == REG
3639 || (GET_CODE (XEXP (a, 0)) == PLUS
3640 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
3641 && (GET_CODE (XEXP (b, 0)) == REG
3642 || (GET_CODE (XEXP (b, 0)) == PLUS
3643 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
3645 if (GET_CODE (XEXP (a, 0)) == PLUS)
3647 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
3648 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
3650 else
3651 reg0 = REGNO (XEXP (a, 0));
3652 if (GET_CODE (XEXP (b, 0)) == PLUS)
3654 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
3655 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
3657 else
3658 reg1 = REGNO (XEXP (b, 0));
3659 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
3661 return 0;
3664 /* Return 1 if OP is a load multiple operation. It is known to be
3665 parallel and the first section will be tested. */
3667 load_multiple_operation (op, mode)
3668 rtx op;
3669 enum machine_mode mode ATTRIBUTE_UNUSED;
3671 HOST_WIDE_INT count = XVECLEN (op, 0);
3672 int dest_regno;
3673 rtx src_addr;
3674 HOST_WIDE_INT i = 1, base = 0;
3675 rtx elt;
3677 if (count <= 1
3678 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3679 return 0;
3681 /* Check to see if this might be a write-back. */
3682 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3684 i++;
3685 base = 1;
3687 /* Now check it more carefully. */
3688 if (GET_CODE (SET_DEST (elt)) != REG
3689 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3690 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3691 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3692 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
3693 return 0;
3696 /* Perform a quick check so we don't blow up below. */
3697 if (count <= i
3698 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3699 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
3700 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
3701 return 0;
3703 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
3704 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
3706 for (; i < count; i++)
3708 elt = XVECEXP (op, 0, i);
3710 if (GET_CODE (elt) != SET
3711 || GET_CODE (SET_DEST (elt)) != REG
3712 || GET_MODE (SET_DEST (elt)) != SImode
3713 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
3714 || GET_CODE (SET_SRC (elt)) != MEM
3715 || GET_MODE (SET_SRC (elt)) != SImode
3716 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
3717 || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
3718 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
3719 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
3720 return 0;
3723 return 1;
3726 /* Return 1 if OP is a store multiple operation. It is known to be
3727 parallel and the first section will be tested. */
3729 store_multiple_operation (op, mode)
3730 rtx op;
3731 enum machine_mode mode ATTRIBUTE_UNUSED;
3733 HOST_WIDE_INT count = XVECLEN (op, 0);
3734 int src_regno;
3735 rtx dest_addr;
3736 HOST_WIDE_INT i = 1, base = 0;
3737 rtx elt;
3739 if (count <= 1
3740 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3741 return 0;
3743 /* Check to see if this might be a write-back. */
3744 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3746 i++;
3747 base = 1;
3749 /* Now check it more carefully. */
3750 if (GET_CODE (SET_DEST (elt)) != REG
3751 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3752 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3753 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3754 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
3755 return 0;
3758 /* Perform a quick check so we don't blow up below. */
3759 if (count <= i
3760 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3761 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
3762 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
3763 return 0;
3765 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
3766 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
3768 for (; i < count; i++)
3770 elt = XVECEXP (op, 0, i);
3772 if (GET_CODE (elt) != SET
3773 || GET_CODE (SET_SRC (elt)) != REG
3774 || GET_MODE (SET_SRC (elt)) != SImode
3775 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
3776 || GET_CODE (SET_DEST (elt)) != MEM
3777 || GET_MODE (SET_DEST (elt)) != SImode
3778 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
3779 || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
3780 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
3781 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
3782 return 0;
3785 return 1;
3789 load_multiple_sequence (operands, nops, regs, base, load_offset)
3790 rtx * operands;
3791 int nops;
3792 int * regs;
3793 int * base;
3794 HOST_WIDE_INT * load_offset;
3796 int unsorted_regs[4];
3797 HOST_WIDE_INT unsorted_offsets[4];
3798 int order[4];
3799 int base_reg = -1;
3800 int i;
3802 /* Can only handle 2, 3, or 4 insns at present, though could be easily
3803 extended if required. */
3804 if (nops < 2 || nops > 4)
3805 abort ();
3807 /* Loop over the operands and check that the memory references are
3808 suitable (ie immediate offsets from the same base register). At
3809 the same time, extract the target register, and the memory
3810 offsets. */
3811 for (i = 0; i < nops; i++)
3813 rtx reg;
3814 rtx offset;
3816 /* Convert a subreg of a mem into the mem itself. */
3817 if (GET_CODE (operands[nops + i]) == SUBREG)
3818 operands[nops + i] = alter_subreg (operands + (nops + i));
3820 if (GET_CODE (operands[nops + i]) != MEM)
3821 abort ();
3823 /* Don't reorder volatile memory references; it doesn't seem worth
3824 looking for the case where the order is ok anyway. */
3825 if (MEM_VOLATILE_P (operands[nops + i]))
3826 return 0;
3828 offset = const0_rtx;
3830 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3831 || (GET_CODE (reg) == SUBREG
3832 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3833 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3834 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3835 == REG)
3836 || (GET_CODE (reg) == SUBREG
3837 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3838 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3839 == CONST_INT)))
3841 if (i == 0)
3843 base_reg = REGNO (reg);
3844 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3845 ? REGNO (operands[i])
3846 : REGNO (SUBREG_REG (operands[i])));
3847 order[0] = 0;
3849 else
3851 if (base_reg != (int) REGNO (reg))
3852 /* Not addressed from the same base register. */
3853 return 0;
3855 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3856 ? REGNO (operands[i])
3857 : REGNO (SUBREG_REG (operands[i])));
3858 if (unsorted_regs[i] < unsorted_regs[order[0]])
3859 order[0] = i;
3862 /* If it isn't an integer register, or if it overwrites the
3863 base register but isn't the last insn in the list, then
3864 we can't do this. */
3865 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
3866 || (i != nops - 1 && unsorted_regs[i] == base_reg))
3867 return 0;
3869 unsorted_offsets[i] = INTVAL (offset);
3871 else
3872 /* Not a suitable memory address. */
3873 return 0;
3876 /* All the useful information has now been extracted from the
3877 operands into unsorted_regs and unsorted_offsets; additionally,
3878 order[0] has been set to the lowest numbered register in the
3879 list. Sort the registers into order, and check that the memory
3880 offsets are ascending and adjacent. */
3882 for (i = 1; i < nops; i++)
3884 int j;
3886 order[i] = order[i - 1];
3887 for (j = 0; j < nops; j++)
3888 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3889 && (order[i] == order[i - 1]
3890 || unsorted_regs[j] < unsorted_regs[order[i]]))
3891 order[i] = j;
3893 /* Have we found a suitable register? if not, one must be used more
3894 than once. */
3895 if (order[i] == order[i - 1])
3896 return 0;
3898 /* Is the memory address adjacent and ascending? */
3899 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3900 return 0;
3903 if (base)
3905 *base = base_reg;
3907 for (i = 0; i < nops; i++)
3908 regs[i] = unsorted_regs[order[i]];
3910 *load_offset = unsorted_offsets[order[0]];
3913 if (unsorted_offsets[order[0]] == 0)
3914 return 1; /* ldmia */
3916 if (unsorted_offsets[order[0]] == 4)
3917 return 2; /* ldmib */
3919 if (unsorted_offsets[order[nops - 1]] == 0)
3920 return 3; /* ldmda */
3922 if (unsorted_offsets[order[nops - 1]] == -4)
3923 return 4; /* ldmdb */
3925 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
3926 if the offset isn't small enough. The reason 2 ldrs are faster
3927 is because these ARMs are able to do more than one cache access
3928 in a single cycle. The ARM9 and StrongARM have Harvard caches,
3929 whilst the ARM8 has a double bandwidth cache. This means that
3930 these cores can do both an instruction fetch and a data fetch in
3931 a single cycle, so the trick of calculating the address into a
3932 scratch register (one of the result regs) and then doing a load
3933 multiple actually becomes slower (and no smaller in code size).
3934 That is the transformation
3936 ldr rd1, [rbase + offset]
3937 ldr rd2, [rbase + offset + 4]
3941 add rd1, rbase, offset
3942 ldmia rd1, {rd1, rd2}
3944 produces worse code -- '3 cycles + any stalls on rd2' instead of
3945 '2 cycles + any stalls on rd2'. On ARMs with only one cache
3946 access per cycle, the first sequence could never complete in less
3947 than 6 cycles, whereas the ldm sequence would only take 5 and
3948 would make better use of sequential accesses if not hitting the
3949 cache.
3951 We cheat here and test 'arm_ld_sched' which we currently know to
3952 only be true for the ARM8, ARM9 and StrongARM. If this ever
3953 changes, then the test below needs to be reworked. */
3954 if (nops == 2 && arm_ld_sched)
3955 return 0;
3957 /* Can't do it without setting up the offset, only do this if it takes
3958 no more than one insn. */
3959 return (const_ok_for_arm (unsorted_offsets[order[0]])
3960 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
3963 const char *
3964 emit_ldm_seq (operands, nops)
3965 rtx * operands;
3966 int nops;
3968 int regs[4];
3969 int base_reg;
3970 HOST_WIDE_INT offset;
3971 char buf[100];
3972 int i;
3974 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3976 case 1:
3977 strcpy (buf, "ldm%?ia\t");
3978 break;
3980 case 2:
3981 strcpy (buf, "ldm%?ib\t");
3982 break;
3984 case 3:
3985 strcpy (buf, "ldm%?da\t");
3986 break;
3988 case 4:
3989 strcpy (buf, "ldm%?db\t");
3990 break;
3992 case 5:
3993 if (offset >= 0)
3994 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3995 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3996 (long) offset);
3997 else
3998 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3999 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
4000 (long) -offset);
4001 output_asm_insn (buf, operands);
4002 base_reg = regs[0];
4003 strcpy (buf, "ldm%?ia\t");
4004 break;
4006 default:
4007 abort ();
4010 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
4011 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
4013 for (i = 1; i < nops; i++)
4014 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
4015 reg_names[regs[i]]);
4017 strcat (buf, "}\t%@ phole ldm");
4019 output_asm_insn (buf, operands);
4020 return "";
4024 store_multiple_sequence (operands, nops, regs, base, load_offset)
4025 rtx * operands;
4026 int nops;
4027 int * regs;
4028 int * base;
4029 HOST_WIDE_INT * load_offset;
4031 int unsorted_regs[4];
4032 HOST_WIDE_INT unsorted_offsets[4];
4033 int order[4];
4034 int base_reg = -1;
4035 int i;
4037 /* Can only handle 2, 3, or 4 insns at present, though could be easily
4038 extended if required. */
4039 if (nops < 2 || nops > 4)
4040 abort ();
4042 /* Loop over the operands and check that the memory references are
4043 suitable (ie immediate offsets from the same base register). At
4044 the same time, extract the target register, and the memory
4045 offsets. */
4046 for (i = 0; i < nops; i++)
4048 rtx reg;
4049 rtx offset;
4051 /* Convert a subreg of a mem into the mem itself. */
4052 if (GET_CODE (operands[nops + i]) == SUBREG)
4053 operands[nops + i] = alter_subreg (operands + (nops + i));
4055 if (GET_CODE (operands[nops + i]) != MEM)
4056 abort ();
4058 /* Don't reorder volatile memory references; it doesn't seem worth
4059 looking for the case where the order is ok anyway. */
4060 if (MEM_VOLATILE_P (operands[nops + i]))
4061 return 0;
4063 offset = const0_rtx;
4065 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
4066 || (GET_CODE (reg) == SUBREG
4067 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4068 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
4069 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
4070 == REG)
4071 || (GET_CODE (reg) == SUBREG
4072 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4073 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
4074 == CONST_INT)))
4076 if (i == 0)
4078 base_reg = REGNO (reg);
4079 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
4080 ? REGNO (operands[i])
4081 : REGNO (SUBREG_REG (operands[i])));
4082 order[0] = 0;
4084 else
4086 if (base_reg != (int) REGNO (reg))
4087 /* Not addressed from the same base register. */
4088 return 0;
4090 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
4091 ? REGNO (operands[i])
4092 : REGNO (SUBREG_REG (operands[i])));
4093 if (unsorted_regs[i] < unsorted_regs[order[0]])
4094 order[0] = i;
4097 /* If it isn't an integer register, then we can't do this. */
4098 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
4099 return 0;
4101 unsorted_offsets[i] = INTVAL (offset);
4103 else
4104 /* Not a suitable memory address. */
4105 return 0;
4108 /* All the useful information has now been extracted from the
4109 operands into unsorted_regs and unsorted_offsets; additionally,
4110 order[0] has been set to the lowest numbered register in the
4111 list. Sort the registers into order, and check that the memory
4112 offsets are ascending and adjacent. */
4114 for (i = 1; i < nops; i++)
4116 int j;
4118 order[i] = order[i - 1];
4119 for (j = 0; j < nops; j++)
4120 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
4121 && (order[i] == order[i - 1]
4122 || unsorted_regs[j] < unsorted_regs[order[i]]))
4123 order[i] = j;
4125 /* Have we found a suitable register? if not, one must be used more
4126 than once. */
4127 if (order[i] == order[i - 1])
4128 return 0;
4130 /* Is the memory address adjacent and ascending? */
4131 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
4132 return 0;
4135 if (base)
4137 *base = base_reg;
4139 for (i = 0; i < nops; i++)
4140 regs[i] = unsorted_regs[order[i]];
4142 *load_offset = unsorted_offsets[order[0]];
4145 if (unsorted_offsets[order[0]] == 0)
4146 return 1; /* stmia */
4148 if (unsorted_offsets[order[0]] == 4)
4149 return 2; /* stmib */
4151 if (unsorted_offsets[order[nops - 1]] == 0)
4152 return 3; /* stmda */
4154 if (unsorted_offsets[order[nops - 1]] == -4)
4155 return 4; /* stmdb */
4157 return 0;
4160 const char *
4161 emit_stm_seq (operands, nops)
4162 rtx * operands;
4163 int nops;
4165 int regs[4];
4166 int base_reg;
4167 HOST_WIDE_INT offset;
4168 char buf[100];
4169 int i;
4171 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
4173 case 1:
4174 strcpy (buf, "stm%?ia\t");
4175 break;
4177 case 2:
4178 strcpy (buf, "stm%?ib\t");
4179 break;
4181 case 3:
4182 strcpy (buf, "stm%?da\t");
4183 break;
4185 case 4:
4186 strcpy (buf, "stm%?db\t");
4187 break;
4189 default:
4190 abort ();
4193 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
4194 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
4196 for (i = 1; i < nops; i++)
4197 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
4198 reg_names[regs[i]]);
4200 strcat (buf, "}\t%@ phole stm");
4202 output_asm_insn (buf, operands);
4203 return "";
4207 multi_register_push (op, mode)
4208 rtx op;
4209 enum machine_mode mode ATTRIBUTE_UNUSED;
4211 if (GET_CODE (op) != PARALLEL
4212 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
4213 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
4214 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
4215 return 0;
4217 return 1;
4220 /* Routines for use in generating RTL. */
4222 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
4223 in_struct_p, scalar_p)
4224 int base_regno;
4225 int count;
4226 rtx from;
4227 int up;
4228 int write_back;
4229 int unchanging_p;
4230 int in_struct_p;
4231 int scalar_p;
4233 int i = 0, j;
4234 rtx result;
4235 int sign = up ? 1 : -1;
4236 rtx mem;
4238 /* XScale has load-store double instructions, but they have stricter
4239 alignment requirements than load-store multiple, so we can not
4240 use them.
4242 For XScale ldm requires 2 + NREGS cycles to complete and blocks
4243 the pipeline until completion.
4245 NREGS CYCLES
4251 An ldr instruction takes 1-3 cycles, but does not block the
4252 pipeline.
4254 NREGS CYCLES
4255 1 1-3
4256 2 2-6
4257 3 3-9
4258 4 4-12
4260 Best case ldr will always win. However, the more ldr instructions
4261 we issue, the less likely we are to be able to schedule them well.
4262 Using ldr instructions also increases code size.
4264 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
4265 for counts of 3 or 4 regs. */
4266 if (arm_is_xscale && count <= 2 && ! optimize_size)
4268 rtx seq;
4270 start_sequence ();
4272 for (i = 0; i < count; i++)
4274 mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
4275 RTX_UNCHANGING_P (mem) = unchanging_p;
4276 MEM_IN_STRUCT_P (mem) = in_struct_p;
4277 MEM_SCALAR_P (mem) = scalar_p;
4278 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
4281 if (write_back)
4282 emit_move_insn (from, plus_constant (from, count * 4 * sign));
4284 seq = gen_sequence ();
4285 end_sequence ();
4287 return seq;
4290 result = gen_rtx_PARALLEL (VOIDmode,
4291 rtvec_alloc (count + (write_back ? 1 : 0)));
4292 if (write_back)
4294 XVECEXP (result, 0, 0)
4295 = gen_rtx_SET (GET_MODE (from), from,
4296 plus_constant (from, count * 4 * sign));
4297 i = 1;
4298 count++;
4301 for (j = 0; i < count; i++, j++)
4303 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 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 XVECEXP (result, 0, i)
4308 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
4311 return result;
4315 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
4316 in_struct_p, scalar_p)
4317 int base_regno;
4318 int count;
4319 rtx to;
4320 int up;
4321 int write_back;
4322 int unchanging_p;
4323 int in_struct_p;
4324 int scalar_p;
4326 int i = 0, j;
4327 rtx result;
4328 int sign = up ? 1 : -1;
4329 rtx mem;
4331 /* See arm_gen_load_multiple for discussion of
4332 the pros/cons of ldm/stm usage for XScale. */
4333 if (arm_is_xscale && count <= 2 && ! optimize_size)
4335 rtx seq;
4337 start_sequence ();
4339 for (i = 0; i < count; i++)
4341 mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
4342 RTX_UNCHANGING_P (mem) = unchanging_p;
4343 MEM_IN_STRUCT_P (mem) = in_struct_p;
4344 MEM_SCALAR_P (mem) = scalar_p;
4345 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
4348 if (write_back)
4349 emit_move_insn (to, plus_constant (to, count * 4 * sign));
4351 seq = gen_sequence ();
4352 end_sequence ();
4354 return seq;
4357 result = gen_rtx_PARALLEL (VOIDmode,
4358 rtvec_alloc (count + (write_back ? 1 : 0)));
4359 if (write_back)
4361 XVECEXP (result, 0, 0)
4362 = gen_rtx_SET (GET_MODE (to), to,
4363 plus_constant (to, count * 4 * sign));
4364 i = 1;
4365 count++;
4368 for (j = 0; i < count; i++, j++)
4370 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 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;
4375 XVECEXP (result, 0, i)
4376 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
4379 return result;
4383 arm_gen_movstrqi (operands)
4384 rtx * operands;
4386 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
4387 int i;
4388 rtx src, dst;
4389 rtx st_src, st_dst, fin_src, fin_dst;
4390 rtx part_bytes_reg = NULL;
4391 rtx mem;
4392 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
4393 int dst_scalar_p, src_scalar_p;
4395 if (GET_CODE (operands[2]) != CONST_INT
4396 || GET_CODE (operands[3]) != CONST_INT
4397 || INTVAL (operands[2]) > 64
4398 || INTVAL (operands[3]) & 3)
4399 return 0;
4401 st_dst = XEXP (operands[0], 0);
4402 st_src = XEXP (operands[1], 0);
4404 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
4405 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
4406 dst_scalar_p = MEM_SCALAR_P (operands[0]);
4407 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
4408 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
4409 src_scalar_p = MEM_SCALAR_P (operands[1]);
4411 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
4412 fin_src = src = copy_to_mode_reg (SImode, st_src);
4414 in_words_to_go = NUM_INTS (INTVAL (operands[2]));
4415 out_words_to_go = INTVAL (operands[2]) / 4;
4416 last_bytes = INTVAL (operands[2]) & 3;
4418 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
4419 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
4421 for (i = 0; in_words_to_go >= 2; i+=4)
4423 if (in_words_to_go > 4)
4424 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
4425 src_unchanging_p,
4426 src_in_struct_p,
4427 src_scalar_p));
4428 else
4429 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
4430 FALSE, src_unchanging_p,
4431 src_in_struct_p, src_scalar_p));
4433 if (out_words_to_go)
4435 if (out_words_to_go > 4)
4436 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
4437 dst_unchanging_p,
4438 dst_in_struct_p,
4439 dst_scalar_p));
4440 else if (out_words_to_go != 1)
4441 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
4442 dst, TRUE,
4443 (last_bytes == 0
4444 ? FALSE : TRUE),
4445 dst_unchanging_p,
4446 dst_in_struct_p,
4447 dst_scalar_p));
4448 else
4450 mem = gen_rtx_MEM (SImode, dst);
4451 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4452 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4453 MEM_SCALAR_P (mem) = dst_scalar_p;
4454 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
4455 if (last_bytes != 0)
4456 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
4460 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
4461 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
4464 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
4465 if (out_words_to_go)
4467 rtx sreg;
4469 mem = gen_rtx_MEM (SImode, src);
4470 RTX_UNCHANGING_P (mem) = src_unchanging_p;
4471 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
4472 MEM_SCALAR_P (mem) = src_scalar_p;
4473 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
4474 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
4476 mem = gen_rtx_MEM (SImode, dst);
4477 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4478 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4479 MEM_SCALAR_P (mem) = dst_scalar_p;
4480 emit_move_insn (mem, sreg);
4481 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
4482 in_words_to_go--;
4484 if (in_words_to_go) /* Sanity check */
4485 abort ();
4488 if (in_words_to_go)
4490 if (in_words_to_go < 0)
4491 abort ();
4493 mem = gen_rtx_MEM (SImode, src);
4494 RTX_UNCHANGING_P (mem) = src_unchanging_p;
4495 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
4496 MEM_SCALAR_P (mem) = src_scalar_p;
4497 part_bytes_reg = copy_to_mode_reg (SImode, mem);
4500 if (last_bytes && part_bytes_reg == NULL)
4501 abort ();
4503 if (BYTES_BIG_ENDIAN && last_bytes)
4505 rtx tmp = gen_reg_rtx (SImode);
4507 /* The bytes we want are in the top end of the word. */
4508 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
4509 GEN_INT (8 * (4 - last_bytes))));
4510 part_bytes_reg = tmp;
4512 while (last_bytes)
4514 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
4515 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4516 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4517 MEM_SCALAR_P (mem) = dst_scalar_p;
4518 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
4520 if (--last_bytes)
4522 tmp = gen_reg_rtx (SImode);
4523 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
4524 part_bytes_reg = tmp;
4529 else
4531 if (last_bytes > 1)
4533 mem = gen_rtx_MEM (HImode, dst);
4534 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4535 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4536 MEM_SCALAR_P (mem) = dst_scalar_p;
4537 emit_move_insn (mem, gen_rtx_SUBREG (HImode, part_bytes_reg, 0));
4538 last_bytes -= 2;
4539 if (last_bytes)
4541 rtx tmp = gen_reg_rtx (SImode);
4543 emit_insn (gen_addsi3 (dst, dst, GEN_INT (2)));
4544 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
4545 part_bytes_reg = tmp;
4549 if (last_bytes)
4551 mem = gen_rtx_MEM (QImode, dst);
4552 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4553 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4554 MEM_SCALAR_P (mem) = dst_scalar_p;
4555 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
4559 return 1;
4562 /* Generate a memory reference for a half word, such that it will be loaded
4563 into the top 16 bits of the word. We can assume that the address is
4564 known to be alignable and of the form reg, or plus (reg, const). */
4566 arm_gen_rotated_half_load (memref)
4567 rtx memref;
4569 HOST_WIDE_INT offset = 0;
4570 rtx base = XEXP (memref, 0);
4572 if (GET_CODE (base) == PLUS)
4574 offset = INTVAL (XEXP (base, 1));
4575 base = XEXP (base, 0);
4578 /* If we aren't allowed to generate unaligned addresses, then fail. */
4579 if (TARGET_MMU_TRAPS
4580 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
4581 return NULL;
4583 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
4585 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
4586 return base;
4588 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
4591 /* Select a dominance comparison mode if possible. We support three forms.
4592 COND_OR == 0 => (X && Y)
4593 COND_OR == 1 => ((! X( || Y)
4594 COND_OR == 2 => (X || Y)
4595 If we are unable to support a dominance comparsison we return CC mode.
4596 This will then fail to match for the RTL expressions that generate this
4597 call. */
4599 static enum machine_mode
4600 select_dominance_cc_mode (x, y, cond_or)
4601 rtx x;
4602 rtx y;
4603 HOST_WIDE_INT cond_or;
4605 enum rtx_code cond1, cond2;
4606 int swapped = 0;
4608 /* Currently we will probably get the wrong result if the individual
4609 comparisons are not simple. This also ensures that it is safe to
4610 reverse a comparison if necessary. */
4611 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
4612 != CCmode)
4613 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
4614 != CCmode))
4615 return CCmode;
4617 /* The if_then_else variant of this tests the second condition if the
4618 first passes, but is true if the first fails. Reverse the first
4619 condition to get a true "inclusive-or" expression. */
4620 if (cond_or == 1)
4621 cond1 = reverse_condition (cond1);
4623 /* If the comparisons are not equal, and one doesn't dominate the other,
4624 then we can't do this. */
4625 if (cond1 != cond2
4626 && !comparison_dominates_p (cond1, cond2)
4627 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
4628 return CCmode;
4630 if (swapped)
4632 enum rtx_code temp = cond1;
4633 cond1 = cond2;
4634 cond2 = temp;
4637 switch (cond1)
4639 case EQ:
4640 if (cond2 == EQ || !cond_or)
4641 return CC_DEQmode;
4643 switch (cond2)
4645 case LE: return CC_DLEmode;
4646 case LEU: return CC_DLEUmode;
4647 case GE: return CC_DGEmode;
4648 case GEU: return CC_DGEUmode;
4649 default: break;
4652 break;
4654 case LT:
4655 if (cond2 == LT || !cond_or)
4656 return CC_DLTmode;
4657 if (cond2 == LE)
4658 return CC_DLEmode;
4659 if (cond2 == NE)
4660 return CC_DNEmode;
4661 break;
4663 case GT:
4664 if (cond2 == GT || !cond_or)
4665 return CC_DGTmode;
4666 if (cond2 == GE)
4667 return CC_DGEmode;
4668 if (cond2 == NE)
4669 return CC_DNEmode;
4670 break;
4672 case LTU:
4673 if (cond2 == LTU || !cond_or)
4674 return CC_DLTUmode;
4675 if (cond2 == LEU)
4676 return CC_DLEUmode;
4677 if (cond2 == NE)
4678 return CC_DNEmode;
4679 break;
4681 case GTU:
4682 if (cond2 == GTU || !cond_or)
4683 return CC_DGTUmode;
4684 if (cond2 == GEU)
4685 return CC_DGEUmode;
4686 if (cond2 == NE)
4687 return CC_DNEmode;
4688 break;
4690 /* The remaining cases only occur when both comparisons are the
4691 same. */
4692 case NE:
4693 return CC_DNEmode;
4695 case LE:
4696 return CC_DLEmode;
4698 case GE:
4699 return CC_DGEmode;
4701 case LEU:
4702 return CC_DLEUmode;
4704 case GEU:
4705 return CC_DGEUmode;
4707 default:
4708 break;
4711 abort ();
4714 enum machine_mode
4715 arm_select_cc_mode (op, x, y)
4716 enum rtx_code op;
4717 rtx x;
4718 rtx y;
4720 /* All floating point compares return CCFP if it is an equality
4721 comparison, and CCFPE otherwise. */
4722 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
4724 switch (op)
4726 case EQ:
4727 case NE:
4728 case UNORDERED:
4729 case ORDERED:
4730 case UNLT:
4731 case UNLE:
4732 case UNGT:
4733 case UNGE:
4734 case UNEQ:
4735 case LTGT:
4736 return CCFPmode;
4738 case LT:
4739 case LE:
4740 case GT:
4741 case GE:
4742 return CCFPEmode;
4744 default:
4745 abort ();
4749 /* A compare with a shifted operand. Because of canonicalization, the
4750 comparison will have to be swapped when we emit the assembler. */
4751 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
4752 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4753 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
4754 || GET_CODE (x) == ROTATERT))
4755 return CC_SWPmode;
4757 /* This is a special case that is used by combine to allow a
4758 comparison of a shifted byte load to be split into a zero-extend
4759 followed by a comparison of the shifted integer (only valid for
4760 equalities and unsigned inequalities). */
4761 if (GET_MODE (x) == SImode
4762 && GET_CODE (x) == ASHIFT
4763 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
4764 && GET_CODE (XEXP (x, 0)) == SUBREG
4765 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
4766 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
4767 && (op == EQ || op == NE
4768 || op == GEU || op == GTU || op == LTU || op == LEU)
4769 && GET_CODE (y) == CONST_INT)
4770 return CC_Zmode;
4772 /* A construct for a conditional compare, if the false arm contains
4773 0, then both conditions must be true, otherwise either condition
4774 must be true. Not all conditions are possible, so CCmode is
4775 returned if it can't be done. */
4776 if (GET_CODE (x) == IF_THEN_ELSE
4777 && (XEXP (x, 2) == const0_rtx
4778 || XEXP (x, 2) == const1_rtx)
4779 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4780 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4781 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
4782 INTVAL (XEXP (x, 2)));
4784 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
4785 if (GET_CODE (x) == AND
4786 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4787 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4788 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 0);
4790 if (GET_CODE (x) == IOR
4791 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4792 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4793 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 2);
4795 /* An operation that sets the condition codes as a side-effect, the
4796 V flag is not set correctly, so we can only use comparisons where
4797 this doesn't matter. (For LT and GE we can use "mi" and "pl"
4798 instead. */
4799 if (GET_MODE (x) == SImode
4800 && y == const0_rtx
4801 && (op == EQ || op == NE || op == LT || op == GE)
4802 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
4803 || GET_CODE (x) == AND || GET_CODE (x) == IOR
4804 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
4805 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
4806 || GET_CODE (x) == LSHIFTRT
4807 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4808 || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
4809 return CC_NOOVmode;
4811 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
4812 return CC_Zmode;
4814 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
4815 && GET_CODE (x) == PLUS
4816 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
4817 return CC_Cmode;
4819 return CCmode;
4822 /* X and Y are two things to compare using CODE. Emit the compare insn and
4823 return the rtx for register 0 in the proper mode. FP means this is a
4824 floating point compare: I don't think that it is needed on the arm. */
4827 arm_gen_compare_reg (code, x, y)
4828 enum rtx_code code;
4829 rtx x, y;
4831 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
4832 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
4834 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
4835 gen_rtx_COMPARE (mode, x, y)));
4837 return cc_reg;
4840 void
4841 arm_reload_in_hi (operands)
4842 rtx * operands;
4844 rtx ref = operands[1];
4845 rtx base, scratch;
4846 HOST_WIDE_INT offset = 0;
4848 if (GET_CODE (ref) == SUBREG)
4850 offset = SUBREG_BYTE (ref);
4851 ref = SUBREG_REG (ref);
4854 if (GET_CODE (ref) == REG)
4856 /* We have a pseudo which has been spilt onto the stack; there
4857 are two cases here: the first where there is a simple
4858 stack-slot replacement and a second where the stack-slot is
4859 out of range, or is used as a subreg. */
4860 if (reg_equiv_mem[REGNO (ref)])
4862 ref = reg_equiv_mem[REGNO (ref)];
4863 base = find_replacement (&XEXP (ref, 0));
4865 else
4866 /* The slot is out of range, or was dressed up in a SUBREG. */
4867 base = reg_equiv_address[REGNO (ref)];
4869 else
4870 base = find_replacement (&XEXP (ref, 0));
4872 /* Handle the case where the address is too complex to be offset by 1. */
4873 if (GET_CODE (base) == MINUS
4874 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4876 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4878 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4879 base = base_plus;
4881 else if (GET_CODE (base) == PLUS)
4883 /* The addend must be CONST_INT, or we would have dealt with it above. */
4884 HOST_WIDE_INT hi, lo;
4886 offset += INTVAL (XEXP (base, 1));
4887 base = XEXP (base, 0);
4889 /* Rework the address into a legal sequence of insns. */
4890 /* Valid range for lo is -4095 -> 4095 */
4891 lo = (offset >= 0
4892 ? (offset & 0xfff)
4893 : -((-offset) & 0xfff));
4895 /* Corner case, if lo is the max offset then we would be out of range
4896 once we have added the additional 1 below, so bump the msb into the
4897 pre-loading insn(s). */
4898 if (lo == 4095)
4899 lo &= 0x7ff;
4901 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
4902 ^ (HOST_WIDE_INT) 0x80000000)
4903 - (HOST_WIDE_INT) 0x80000000);
4905 if (hi + lo != offset)
4906 abort ();
4908 if (hi != 0)
4910 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4912 /* Get the base address; addsi3 knows how to handle constants
4913 that require more than one insn. */
4914 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4915 base = base_plus;
4916 offset = lo;
4920 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4921 emit_insn (gen_zero_extendqisi2 (scratch,
4922 gen_rtx_MEM (QImode,
4923 plus_constant (base,
4924 offset))));
4925 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
4926 gen_rtx_MEM (QImode,
4927 plus_constant (base,
4928 offset + 1))));
4929 if (!BYTES_BIG_ENDIAN)
4930 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4931 gen_rtx_IOR (SImode,
4932 gen_rtx_ASHIFT
4933 (SImode,
4934 gen_rtx_SUBREG (SImode, operands[0], 0),
4935 GEN_INT (8)),
4936 scratch)));
4937 else
4938 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4939 gen_rtx_IOR (SImode,
4940 gen_rtx_ASHIFT (SImode, scratch,
4941 GEN_INT (8)),
4942 gen_rtx_SUBREG (SImode, operands[0],
4943 0))));
4946 /* Handle storing a half-word to memory during reload by synthesising as two
4947 byte stores. Take care not to clobber the input values until after we
4948 have moved them somewhere safe. This code assumes that if the DImode
4949 scratch in operands[2] overlaps either the input value or output address
4950 in some way, then that value must die in this insn (we absolutely need
4951 two scratch registers for some corner cases). */
4952 void
4953 arm_reload_out_hi (operands)
4954 rtx * operands;
4956 rtx ref = operands[0];
4957 rtx outval = operands[1];
4958 rtx base, scratch;
4959 HOST_WIDE_INT offset = 0;
4961 if (GET_CODE (ref) == SUBREG)
4963 offset = SUBREG_BYTE (ref);
4964 ref = SUBREG_REG (ref);
4968 if (GET_CODE (ref) == REG)
4970 /* We have a pseudo which has been spilt onto the stack; there
4971 are two cases here: the first where there is a simple
4972 stack-slot replacement and a second where the stack-slot is
4973 out of range, or is used as a subreg. */
4974 if (reg_equiv_mem[REGNO (ref)])
4976 ref = reg_equiv_mem[REGNO (ref)];
4977 base = find_replacement (&XEXP (ref, 0));
4979 else
4980 /* The slot is out of range, or was dressed up in a SUBREG. */
4981 base = reg_equiv_address[REGNO (ref)];
4983 else
4984 base = find_replacement (&XEXP (ref, 0));
4986 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4988 /* Handle the case where the address is too complex to be offset by 1. */
4989 if (GET_CODE (base) == MINUS
4990 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4992 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4994 /* Be careful not to destroy OUTVAL. */
4995 if (reg_overlap_mentioned_p (base_plus, outval))
4997 /* Updating base_plus might destroy outval, see if we can
4998 swap the scratch and base_plus. */
4999 if (!reg_overlap_mentioned_p (scratch, outval))
5001 rtx tmp = scratch;
5002 scratch = base_plus;
5003 base_plus = tmp;
5005 else
5007 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5009 /* Be conservative and copy OUTVAL into the scratch now,
5010 this should only be necessary if outval is a subreg
5011 of something larger than a word. */
5012 /* XXX Might this clobber base? I can't see how it can,
5013 since scratch is known to overlap with OUTVAL, and
5014 must be wider than a word. */
5015 emit_insn (gen_movhi (scratch_hi, outval));
5016 outval = scratch_hi;
5020 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
5021 base = base_plus;
5023 else if (GET_CODE (base) == PLUS)
5025 /* The addend must be CONST_INT, or we would have dealt with it above. */
5026 HOST_WIDE_INT hi, lo;
5028 offset += INTVAL (XEXP (base, 1));
5029 base = XEXP (base, 0);
5031 /* Rework the address into a legal sequence of insns. */
5032 /* Valid range for lo is -4095 -> 4095 */
5033 lo = (offset >= 0
5034 ? (offset & 0xfff)
5035 : -((-offset) & 0xfff));
5037 /* Corner case, if lo is the max offset then we would be out of range
5038 once we have added the additional 1 below, so bump the msb into the
5039 pre-loading insn(s). */
5040 if (lo == 4095)
5041 lo &= 0x7ff;
5043 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
5044 ^ (HOST_WIDE_INT) 0x80000000)
5045 - (HOST_WIDE_INT) 0x80000000);
5047 if (hi + lo != offset)
5048 abort ();
5050 if (hi != 0)
5052 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5054 /* Be careful not to destroy OUTVAL. */
5055 if (reg_overlap_mentioned_p (base_plus, outval))
5057 /* Updating base_plus might destroy outval, see if we
5058 can swap the scratch and base_plus. */
5059 if (!reg_overlap_mentioned_p (scratch, outval))
5061 rtx tmp = scratch;
5062 scratch = base_plus;
5063 base_plus = tmp;
5065 else
5067 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5069 /* Be conservative and copy outval into scratch now,
5070 this should only be necessary if outval is a
5071 subreg of something larger than a word. */
5072 /* XXX Might this clobber base? I can't see how it
5073 can, since scratch is known to overlap with
5074 outval. */
5075 emit_insn (gen_movhi (scratch_hi, outval));
5076 outval = scratch_hi;
5080 /* Get the base address; addsi3 knows how to handle constants
5081 that require more than one insn. */
5082 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
5083 base = base_plus;
5084 offset = lo;
5088 if (BYTES_BIG_ENDIAN)
5090 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
5091 plus_constant (base, offset + 1)),
5092 gen_rtx_SUBREG (QImode, outval, 0)));
5093 emit_insn (gen_lshrsi3 (scratch,
5094 gen_rtx_SUBREG (SImode, outval, 0),
5095 GEN_INT (8)));
5096 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5097 gen_rtx_SUBREG (QImode, scratch, 0)));
5099 else
5101 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
5102 gen_rtx_SUBREG (QImode, outval, 0)));
5103 emit_insn (gen_lshrsi3 (scratch,
5104 gen_rtx_SUBREG (SImode, outval, 0),
5105 GEN_INT (8)));
5106 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
5107 plus_constant (base, offset + 1)),
5108 gen_rtx_SUBREG (QImode, scratch, 0)));
5112 /* Print a symbolic form of X to the debug file, F. */
5113 static void
5114 arm_print_value (f, x)
5115 FILE * f;
5116 rtx x;
5118 switch (GET_CODE (x))
5120 case CONST_INT:
5121 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
5122 return;
5124 case CONST_DOUBLE:
5125 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
5126 return;
5128 case CONST_STRING:
5129 fprintf (f, "\"%s\"", XSTR (x, 0));
5130 return;
5132 case SYMBOL_REF:
5133 fprintf (f, "`%s'", XSTR (x, 0));
5134 return;
5136 case LABEL_REF:
5137 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
5138 return;
5140 case CONST:
5141 arm_print_value (f, XEXP (x, 0));
5142 return;
5144 case PLUS:
5145 arm_print_value (f, XEXP (x, 0));
5146 fprintf (f, "+");
5147 arm_print_value (f, XEXP (x, 1));
5148 return;
5150 case PC:
5151 fprintf (f, "pc");
5152 return;
5154 default:
5155 fprintf (f, "????");
5156 return;
5160 /* Routines for manipulation of the constant pool. */
5162 /* Arm instructions cannot load a large constant directly into a
5163 register; they have to come from a pc relative load. The constant
5164 must therefore be placed in the addressable range of the pc
5165 relative load. Depending on the precise pc relative load
5166 instruction the range is somewhere between 256 bytes and 4k. This
5167 means that we often have to dump a constant inside a function, and
5168 generate code to branch around it.
5170 It is important to minimize this, since the branches will slow
5171 things down and make the code larger.
5173 Normally we can hide the table after an existing unconditional
5174 branch so that there is no interruption of the flow, but in the
5175 worst case the code looks like this:
5177 ldr rn, L1
5179 b L2
5180 align
5181 L1: .long value
5185 ldr rn, L3
5187 b L4
5188 align
5189 L3: .long value
5193 We fix this by performing a scan after scheduling, which notices
5194 which instructions need to have their operands fetched from the
5195 constant table and builds the table.
5197 The algorithm starts by building a table of all the constants that
5198 need fixing up and all the natural barriers in the function (places
5199 where a constant table can be dropped without breaking the flow).
5200 For each fixup we note how far the pc-relative replacement will be
5201 able to reach and the offset of the instruction into the function.
5203 Having built the table we then group the fixes together to form
5204 tables that are as large as possible (subject to addressing
5205 constraints) and emit each table of constants after the last
5206 barrier that is within range of all the instructions in the group.
5207 If a group does not contain a barrier, then we forcibly create one
5208 by inserting a jump instruction into the flow. Once the table has
5209 been inserted, the insns are then modified to reference the
5210 relevant entry in the pool.
5212 Possible enhancements to the algorithm (not implemented) are:
5214 1) For some processors and object formats, there may be benefit in
5215 aligning the pools to the start of cache lines; this alignment
5216 would need to be taken into account when calculating addressability
5217 of a pool. */
5219 /* These typedefs are located at the start of this file, so that
5220 they can be used in the prototypes there. This comment is to
5221 remind readers of that fact so that the following structures
5222 can be understood more easily.
5224 typedef struct minipool_node Mnode;
5225 typedef struct minipool_fixup Mfix; */
5227 struct minipool_node
5229 /* Doubly linked chain of entries. */
5230 Mnode * next;
5231 Mnode * prev;
5232 /* The maximum offset into the code that this entry can be placed. While
5233 pushing fixes for forward references, all entries are sorted in order
5234 of increasing max_address. */
5235 HOST_WIDE_INT max_address;
5236 /* Similarly for an entry inserted for a backwards ref. */
5237 HOST_WIDE_INT min_address;
5238 /* The number of fixes referencing this entry. This can become zero
5239 if we "unpush" an entry. In this case we ignore the entry when we
5240 come to emit the code. */
5241 int refcount;
5242 /* The offset from the start of the minipool. */
5243 HOST_WIDE_INT offset;
5244 /* The value in table. */
5245 rtx value;
5246 /* The mode of value. */
5247 enum machine_mode mode;
5248 int fix_size;
5251 struct minipool_fixup
5253 Mfix * next;
5254 rtx insn;
5255 HOST_WIDE_INT address;
5256 rtx * loc;
5257 enum machine_mode mode;
5258 int fix_size;
5259 rtx value;
5260 Mnode * minipool;
5261 HOST_WIDE_INT forwards;
5262 HOST_WIDE_INT backwards;
5265 /* Fixes less than a word need padding out to a word boundary. */
5266 #define MINIPOOL_FIX_SIZE(mode) \
5267 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
5269 static Mnode * minipool_vector_head;
5270 static Mnode * minipool_vector_tail;
5271 static rtx minipool_vector_label;
5273 /* The linked list of all minipool fixes required for this function. */
5274 Mfix * minipool_fix_head;
5275 Mfix * minipool_fix_tail;
5276 /* The fix entry for the current minipool, once it has been placed. */
5277 Mfix * minipool_barrier;
5279 /* Determines if INSN is the start of a jump table. Returns the end
5280 of the TABLE or NULL_RTX. */
5281 static rtx
5282 is_jump_table (insn)
5283 rtx insn;
5285 rtx table;
5287 if (GET_CODE (insn) == JUMP_INSN
5288 && JUMP_LABEL (insn) != NULL
5289 && ((table = next_real_insn (JUMP_LABEL (insn)))
5290 == next_real_insn (insn))
5291 && table != NULL
5292 && GET_CODE (table) == JUMP_INSN
5293 && (GET_CODE (PATTERN (table)) == ADDR_VEC
5294 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
5295 return table;
5297 return NULL_RTX;
5300 static HOST_WIDE_INT
5301 get_jump_table_size (insn)
5302 rtx insn;
5304 rtx body = PATTERN (insn);
5305 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
5307 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
5310 /* Move a minipool fix MP from its current location to before MAX_MP.
5311 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
5312 contrains may need updating. */
5313 static Mnode *
5314 move_minipool_fix_forward_ref (mp, max_mp, max_address)
5315 Mnode * mp;
5316 Mnode * max_mp;
5317 HOST_WIDE_INT max_address;
5319 /* This should never be true and the code below assumes these are
5320 different. */
5321 if (mp == max_mp)
5322 abort ();
5324 if (max_mp == NULL)
5326 if (max_address < mp->max_address)
5327 mp->max_address = max_address;
5329 else
5331 if (max_address > max_mp->max_address - mp->fix_size)
5332 mp->max_address = max_mp->max_address - mp->fix_size;
5333 else
5334 mp->max_address = max_address;
5336 /* Unlink MP from its current position. Since max_mp is non-null,
5337 mp->prev must be non-null. */
5338 mp->prev->next = mp->next;
5339 if (mp->next != NULL)
5340 mp->next->prev = mp->prev;
5341 else
5342 minipool_vector_tail = mp->prev;
5344 /* Re-insert it before MAX_MP. */
5345 mp->next = max_mp;
5346 mp->prev = max_mp->prev;
5347 max_mp->prev = mp;
5349 if (mp->prev != NULL)
5350 mp->prev->next = mp;
5351 else
5352 minipool_vector_head = mp;
5355 /* Save the new entry. */
5356 max_mp = mp;
5358 /* Scan over the preceding entries and adjust their addresses as
5359 required. */
5360 while (mp->prev != NULL
5361 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
5363 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
5364 mp = mp->prev;
5367 return max_mp;
5370 /* Add a constant to the minipool for a forward reference. Returns the
5371 node added or NULL if the constant will not fit in this pool. */
5372 static Mnode *
5373 add_minipool_forward_ref (fix)
5374 Mfix * fix;
5376 /* If set, max_mp is the first pool_entry that has a lower
5377 constraint than the one we are trying to add. */
5378 Mnode * max_mp = NULL;
5379 HOST_WIDE_INT max_address = fix->address + fix->forwards;
5380 Mnode * mp;
5382 /* If this fix's address is greater than the address of the first
5383 entry, then we can't put the fix in this pool. We subtract the
5384 size of the current fix to ensure that if the table is fully
5385 packed we still have enough room to insert this value by suffling
5386 the other fixes forwards. */
5387 if (minipool_vector_head &&
5388 fix->address >= minipool_vector_head->max_address - fix->fix_size)
5389 return NULL;
5391 /* Scan the pool to see if a constant with the same value has
5392 already been added. While we are doing this, also note the
5393 location where we must insert the constant if it doesn't already
5394 exist. */
5395 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5397 if (GET_CODE (fix->value) == GET_CODE (mp->value)
5398 && fix->mode == mp->mode
5399 && (GET_CODE (fix->value) != CODE_LABEL
5400 || (CODE_LABEL_NUMBER (fix->value)
5401 == CODE_LABEL_NUMBER (mp->value)))
5402 && rtx_equal_p (fix->value, mp->value))
5404 /* More than one fix references this entry. */
5405 mp->refcount++;
5406 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
5409 /* Note the insertion point if necessary. */
5410 if (max_mp == NULL
5411 && mp->max_address > max_address)
5412 max_mp = mp;
5415 /* The value is not currently in the minipool, so we need to create
5416 a new entry for it. If MAX_MP is NULL, the entry will be put on
5417 the end of the list since the placement is less constrained than
5418 any existing entry. Otherwise, we insert the new fix before
5419 MAX_MP and, if neceesary, adjust the constraints on the other
5420 entries. */
5421 mp = xmalloc (sizeof (* mp));
5422 mp->fix_size = fix->fix_size;
5423 mp->mode = fix->mode;
5424 mp->value = fix->value;
5425 mp->refcount = 1;
5426 /* Not yet required for a backwards ref. */
5427 mp->min_address = -65536;
5429 if (max_mp == NULL)
5431 mp->max_address = max_address;
5432 mp->next = NULL;
5433 mp->prev = minipool_vector_tail;
5435 if (mp->prev == NULL)
5437 minipool_vector_head = mp;
5438 minipool_vector_label = gen_label_rtx ();
5440 else
5441 mp->prev->next = mp;
5443 minipool_vector_tail = mp;
5445 else
5447 if (max_address > max_mp->max_address - mp->fix_size)
5448 mp->max_address = max_mp->max_address - mp->fix_size;
5449 else
5450 mp->max_address = max_address;
5452 mp->next = max_mp;
5453 mp->prev = max_mp->prev;
5454 max_mp->prev = mp;
5455 if (mp->prev != NULL)
5456 mp->prev->next = mp;
5457 else
5458 minipool_vector_head = mp;
5461 /* Save the new entry. */
5462 max_mp = mp;
5464 /* Scan over the preceding entries and adjust their addresses as
5465 required. */
5466 while (mp->prev != NULL
5467 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
5469 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
5470 mp = mp->prev;
5473 return max_mp;
5476 static Mnode *
5477 move_minipool_fix_backward_ref (mp, min_mp, min_address)
5478 Mnode * mp;
5479 Mnode * min_mp;
5480 HOST_WIDE_INT min_address;
5482 HOST_WIDE_INT offset;
5484 /* This should never be true, and the code below assumes these are
5485 different. */
5486 if (mp == min_mp)
5487 abort ();
5489 if (min_mp == NULL)
5491 if (min_address > mp->min_address)
5492 mp->min_address = min_address;
5494 else
5496 /* We will adjust this below if it is too loose. */
5497 mp->min_address = min_address;
5499 /* Unlink MP from its current position. Since min_mp is non-null,
5500 mp->next must be non-null. */
5501 mp->next->prev = mp->prev;
5502 if (mp->prev != NULL)
5503 mp->prev->next = mp->next;
5504 else
5505 minipool_vector_head = mp->next;
5507 /* Reinsert it after MIN_MP. */
5508 mp->prev = min_mp;
5509 mp->next = min_mp->next;
5510 min_mp->next = mp;
5511 if (mp->next != NULL)
5512 mp->next->prev = mp;
5513 else
5514 minipool_vector_tail = mp;
5517 min_mp = mp;
5519 offset = 0;
5520 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5522 mp->offset = offset;
5523 if (mp->refcount > 0)
5524 offset += mp->fix_size;
5526 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
5527 mp->next->min_address = mp->min_address + mp->fix_size;
5530 return min_mp;
5533 /* Add a constant to the minipool for a backward reference. Returns the
5534 node added or NULL if the constant will not fit in this pool.
5536 Note that the code for insertion for a backwards reference can be
5537 somewhat confusing because the calculated offsets for each fix do
5538 not take into account the size of the pool (which is still under
5539 construction. */
5540 static Mnode *
5541 add_minipool_backward_ref (fix)
5542 Mfix * fix;
5544 /* If set, min_mp is the last pool_entry that has a lower constraint
5545 than the one we are trying to add. */
5546 Mnode * min_mp = NULL;
5547 /* This can be negative, since it is only a constraint. */
5548 HOST_WIDE_INT min_address = fix->address - fix->backwards;
5549 Mnode * mp;
5551 /* If we can't reach the current pool from this insn, or if we can't
5552 insert this entry at the end of the pool without pushing other
5553 fixes out of range, then we don't try. This ensures that we
5554 can't fail later on. */
5555 if (min_address >= minipool_barrier->address
5556 || (minipool_vector_tail->min_address + fix->fix_size
5557 >= minipool_barrier->address))
5558 return NULL;
5560 /* Scan the pool to see if a constant with the same value has
5561 already been added. While we are doing this, also note the
5562 location where we must insert the constant if it doesn't already
5563 exist. */
5564 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
5566 if (GET_CODE (fix->value) == GET_CODE (mp->value)
5567 && fix->mode == mp->mode
5568 && (GET_CODE (fix->value) != CODE_LABEL
5569 || (CODE_LABEL_NUMBER (fix->value)
5570 == CODE_LABEL_NUMBER (mp->value)))
5571 && rtx_equal_p (fix->value, mp->value)
5572 /* Check that there is enough slack to move this entry to the
5573 end of the table (this is conservative). */
5574 && (mp->max_address
5575 > (minipool_barrier->address
5576 + minipool_vector_tail->offset
5577 + minipool_vector_tail->fix_size)))
5579 mp->refcount++;
5580 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
5583 if (min_mp != NULL)
5584 mp->min_address += fix->fix_size;
5585 else
5587 /* Note the insertion point if necessary. */
5588 if (mp->min_address < min_address)
5589 min_mp = mp;
5590 else if (mp->max_address
5591 < minipool_barrier->address + mp->offset + fix->fix_size)
5593 /* Inserting before this entry would push the fix beyond
5594 its maximum address (which can happen if we have
5595 re-located a forwards fix); force the new fix to come
5596 after it. */
5597 min_mp = mp;
5598 min_address = mp->min_address + fix->fix_size;
5603 /* We need to create a new entry. */
5604 mp = xmalloc (sizeof (* mp));
5605 mp->fix_size = fix->fix_size;
5606 mp->mode = fix->mode;
5607 mp->value = fix->value;
5608 mp->refcount = 1;
5609 mp->max_address = minipool_barrier->address + 65536;
5611 mp->min_address = min_address;
5613 if (min_mp == NULL)
5615 mp->prev = NULL;
5616 mp->next = minipool_vector_head;
5618 if (mp->next == NULL)
5620 minipool_vector_tail = mp;
5621 minipool_vector_label = gen_label_rtx ();
5623 else
5624 mp->next->prev = mp;
5626 minipool_vector_head = mp;
5628 else
5630 mp->next = min_mp->next;
5631 mp->prev = min_mp;
5632 min_mp->next = mp;
5634 if (mp->next != NULL)
5635 mp->next->prev = mp;
5636 else
5637 minipool_vector_tail = mp;
5640 /* Save the new entry. */
5641 min_mp = mp;
5643 if (mp->prev)
5644 mp = mp->prev;
5645 else
5646 mp->offset = 0;
5648 /* Scan over the following entries and adjust their offsets. */
5649 while (mp->next != NULL)
5651 if (mp->next->min_address < mp->min_address + mp->fix_size)
5652 mp->next->min_address = mp->min_address + mp->fix_size;
5654 if (mp->refcount)
5655 mp->next->offset = mp->offset + mp->fix_size;
5656 else
5657 mp->next->offset = mp->offset;
5659 mp = mp->next;
5662 return min_mp;
5665 static void
5666 assign_minipool_offsets (barrier)
5667 Mfix * barrier;
5669 HOST_WIDE_INT offset = 0;
5670 Mnode * mp;
5672 minipool_barrier = barrier;
5674 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5676 mp->offset = offset;
5678 if (mp->refcount > 0)
5679 offset += mp->fix_size;
5683 /* Output the literal table */
5684 static void
5685 dump_minipool (scan)
5686 rtx scan;
5688 Mnode * mp;
5689 Mnode * nmp;
5691 if (rtl_dump_file)
5692 fprintf (rtl_dump_file,
5693 ";; Emitting minipool after insn %u; address %ld\n",
5694 INSN_UID (scan), (unsigned long) minipool_barrier->address);
5696 scan = emit_label_after (gen_label_rtx (), scan);
5697 scan = emit_insn_after (gen_align_4 (), scan);
5698 scan = emit_label_after (minipool_vector_label, scan);
5700 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
5702 if (mp->refcount > 0)
5704 if (rtl_dump_file)
5706 fprintf (rtl_dump_file,
5707 ";; Offset %u, min %ld, max %ld ",
5708 (unsigned) mp->offset, (unsigned long) mp->min_address,
5709 (unsigned long) mp->max_address);
5710 arm_print_value (rtl_dump_file, mp->value);
5711 fputc ('\n', rtl_dump_file);
5714 switch (mp->fix_size)
5716 #ifdef HAVE_consttable_1
5717 case 1:
5718 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
5719 break;
5721 #endif
5722 #ifdef HAVE_consttable_2
5723 case 2:
5724 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
5725 break;
5727 #endif
5728 #ifdef HAVE_consttable_4
5729 case 4:
5730 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
5731 break;
5733 #endif
5734 #ifdef HAVE_consttable_8
5735 case 8:
5736 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
5737 break;
5739 #endif
5740 default:
5741 abort ();
5742 break;
5746 nmp = mp->next;
5747 free (mp);
5750 minipool_vector_head = minipool_vector_tail = NULL;
5751 scan = emit_insn_after (gen_consttable_end (), scan);
5752 scan = emit_barrier_after (scan);
5755 /* Return the cost of forcibly inserting a barrier after INSN. */
5756 static int
5757 arm_barrier_cost (insn)
5758 rtx insn;
5760 /* Basing the location of the pool on the loop depth is preferable,
5761 but at the moment, the basic block information seems to be
5762 corrupt by this stage of the compilation. */
5763 int base_cost = 50;
5764 rtx next = next_nonnote_insn (insn);
5766 if (next != NULL && GET_CODE (next) == CODE_LABEL)
5767 base_cost -= 20;
5769 switch (GET_CODE (insn))
5771 case CODE_LABEL:
5772 /* It will always be better to place the table before the label, rather
5773 than after it. */
5774 return 50;
5776 case INSN:
5777 case CALL_INSN:
5778 return base_cost;
5780 case JUMP_INSN:
5781 return base_cost - 10;
5783 default:
5784 return base_cost + 10;
5788 /* Find the best place in the insn stream in the range
5789 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
5790 Create the barrier by inserting a jump and add a new fix entry for
5791 it. */
5792 static Mfix *
5793 create_fix_barrier (fix, max_address)
5794 Mfix * fix;
5795 HOST_WIDE_INT max_address;
5797 HOST_WIDE_INT count = 0;
5798 rtx barrier;
5799 rtx from = fix->insn;
5800 rtx selected = from;
5801 int selected_cost;
5802 HOST_WIDE_INT selected_address;
5803 Mfix * new_fix;
5804 HOST_WIDE_INT max_count = max_address - fix->address;
5805 rtx label = gen_label_rtx ();
5807 selected_cost = arm_barrier_cost (from);
5808 selected_address = fix->address;
5810 while (from && count < max_count)
5812 rtx tmp;
5813 int new_cost;
5815 /* This code shouldn't have been called if there was a natural barrier
5816 within range. */
5817 if (GET_CODE (from) == BARRIER)
5818 abort ();
5820 /* Count the length of this insn. */
5821 count += get_attr_length (from);
5823 /* If there is a jump table, add its length. */
5824 tmp = is_jump_table (from);
5825 if (tmp != NULL)
5827 count += get_jump_table_size (tmp);
5829 /* Jump tables aren't in a basic block, so base the cost on
5830 the dispatch insn. If we select this location, we will
5831 still put the pool after the table. */
5832 new_cost = arm_barrier_cost (from);
5834 if (count < max_count && new_cost <= selected_cost)
5836 selected = tmp;
5837 selected_cost = new_cost;
5838 selected_address = fix->address + count;
5841 /* Continue after the dispatch table. */
5842 from = NEXT_INSN (tmp);
5843 continue;
5846 new_cost = arm_barrier_cost (from);
5848 if (count < max_count && new_cost <= selected_cost)
5850 selected = from;
5851 selected_cost = new_cost;
5852 selected_address = fix->address + count;
5855 from = NEXT_INSN (from);
5858 /* Create a new JUMP_INSN that branches around a barrier. */
5859 from = emit_jump_insn_after (gen_jump (label), selected);
5860 JUMP_LABEL (from) = label;
5861 barrier = emit_barrier_after (from);
5862 emit_label_after (label, barrier);
5864 /* Create a minipool barrier entry for the new barrier. */
5865 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
5866 new_fix->insn = barrier;
5867 new_fix->address = selected_address;
5868 new_fix->next = fix->next;
5869 fix->next = new_fix;
5871 return new_fix;
5874 /* Record that there is a natural barrier in the insn stream at
5875 ADDRESS. */
5876 static void
5877 push_minipool_barrier (insn, address)
5878 rtx insn;
5879 HOST_WIDE_INT address;
5881 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
5883 fix->insn = insn;
5884 fix->address = address;
5886 fix->next = NULL;
5887 if (minipool_fix_head != NULL)
5888 minipool_fix_tail->next = fix;
5889 else
5890 minipool_fix_head = fix;
5892 minipool_fix_tail = fix;
5895 /* Record INSN, which will need fixing up to load a value from the
5896 minipool. ADDRESS is the offset of the insn since the start of the
5897 function; LOC is a pointer to the part of the insn which requires
5898 fixing; VALUE is the constant that must be loaded, which is of type
5899 MODE. */
5900 static void
5901 push_minipool_fix (insn, address, loc, mode, value)
5902 rtx insn;
5903 HOST_WIDE_INT address;
5904 rtx * loc;
5905 enum machine_mode mode;
5906 rtx value;
5908 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
5910 #ifdef AOF_ASSEMBLER
5911 /* PIC symbol refereneces need to be converted into offsets into the
5912 based area. */
5913 /* XXX This shouldn't be done here. */
5914 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
5915 value = aof_pic_entry (value);
5916 #endif /* AOF_ASSEMBLER */
5918 fix->insn = insn;
5919 fix->address = address;
5920 fix->loc = loc;
5921 fix->mode = mode;
5922 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
5923 fix->value = value;
5924 fix->forwards = get_attr_pool_range (insn);
5925 fix->backwards = get_attr_neg_pool_range (insn);
5926 fix->minipool = NULL;
5928 /* If an insn doesn't have a range defined for it, then it isn't
5929 expecting to be reworked by this code. Better to abort now than
5930 to generate duff assembly code. */
5931 if (fix->forwards == 0 && fix->backwards == 0)
5932 abort ();
5934 if (rtl_dump_file)
5936 fprintf (rtl_dump_file,
5937 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
5938 GET_MODE_NAME (mode),
5939 INSN_UID (insn), (unsigned long) address,
5940 -1 * (long)fix->backwards, (long)fix->forwards);
5941 arm_print_value (rtl_dump_file, fix->value);
5942 fprintf (rtl_dump_file, "\n");
5945 /* Add it to the chain of fixes. */
5946 fix->next = NULL;
5948 if (minipool_fix_head != NULL)
5949 minipool_fix_tail->next = fix;
5950 else
5951 minipool_fix_head = fix;
5953 minipool_fix_tail = fix;
5956 /* Scan INSN and note any of its operands that need fixing. */
5957 static void
5958 note_invalid_constants (insn, address)
5959 rtx insn;
5960 HOST_WIDE_INT address;
5962 int opno;
5964 extract_insn (insn);
5966 if (!constrain_operands (1))
5967 fatal_insn_not_found (insn);
5969 /* Fill in recog_op_alt with information about the constraints of this
5970 insn. */
5971 preprocess_constraints ();
5973 for (opno = 0; opno < recog_data.n_operands; opno++)
5975 /* Things we need to fix can only occur in inputs. */
5976 if (recog_data.operand_type[opno] != OP_IN)
5977 continue;
5979 /* If this alternative is a memory reference, then any mention
5980 of constants in this alternative is really to fool reload
5981 into allowing us to accept one there. We need to fix them up
5982 now so that we output the right code. */
5983 if (recog_op_alt[opno][which_alternative].memory_ok)
5985 rtx op = recog_data.operand[opno];
5987 if (CONSTANT_P (op))
5988 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
5989 recog_data.operand_mode[opno], op);
5990 #if 0
5991 /* RWE: Now we look correctly at the operands for the insn,
5992 this shouldn't be needed any more. */
5993 #ifndef AOF_ASSEMBLER
5994 /* XXX Is this still needed? */
5995 else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == UNSPEC_PIC_SYM)
5996 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
5997 recog_data.operand_mode[opno],
5998 XVECEXP (op, 0, 0));
5999 #endif
6000 #endif
6001 else if (GET_CODE (op) == MEM
6002 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
6003 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
6004 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6005 recog_data.operand_mode[opno],
6006 get_pool_constant (XEXP (op, 0)));
6011 void
6012 arm_reorg (first)
6013 rtx first;
6015 rtx insn;
6016 HOST_WIDE_INT address = 0;
6017 Mfix * fix;
6019 minipool_fix_head = minipool_fix_tail = NULL;
6021 /* The first insn must always be a note, or the code below won't
6022 scan it properly. */
6023 if (GET_CODE (first) != NOTE)
6024 abort ();
6026 /* Scan all the insns and record the operands that will need fixing. */
6027 for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
6029 if (GET_CODE (insn) == BARRIER)
6030 push_minipool_barrier (insn, address);
6031 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
6032 || GET_CODE (insn) == JUMP_INSN)
6034 rtx table;
6036 note_invalid_constants (insn, address);
6037 address += get_attr_length (insn);
6039 /* If the insn is a vector jump, add the size of the table
6040 and skip the table. */
6041 if ((table = is_jump_table (insn)) != NULL)
6043 address += get_jump_table_size (table);
6044 insn = table;
6049 fix = minipool_fix_head;
6051 /* Now scan the fixups and perform the required changes. */
6052 while (fix)
6054 Mfix * ftmp;
6055 Mfix * fdel;
6056 Mfix * last_added_fix;
6057 Mfix * last_barrier = NULL;
6058 Mfix * this_fix;
6060 /* Skip any further barriers before the next fix. */
6061 while (fix && GET_CODE (fix->insn) == BARRIER)
6062 fix = fix->next;
6064 /* No more fixes. */
6065 if (fix == NULL)
6066 break;
6068 last_added_fix = NULL;
6070 for (ftmp = fix; ftmp; ftmp = ftmp->next)
6072 if (GET_CODE (ftmp->insn) == BARRIER)
6074 if (ftmp->address >= minipool_vector_head->max_address)
6075 break;
6077 last_barrier = ftmp;
6079 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
6080 break;
6082 last_added_fix = ftmp; /* Keep track of the last fix added. */
6085 /* If we found a barrier, drop back to that; any fixes that we
6086 could have reached but come after the barrier will now go in
6087 the next mini-pool. */
6088 if (last_barrier != NULL)
6090 /* Reduce the refcount for those fixes that won't go into this
6091 pool after all. */
6092 for (fdel = last_barrier->next;
6093 fdel && fdel != ftmp;
6094 fdel = fdel->next)
6096 fdel->minipool->refcount--;
6097 fdel->minipool = NULL;
6100 ftmp = last_barrier;
6102 else
6104 /* ftmp is first fix that we can't fit into this pool and
6105 there no natural barriers that we could use. Insert a
6106 new barrier in the code somewhere between the previous
6107 fix and this one, and arrange to jump around it. */
6108 HOST_WIDE_INT max_address;
6110 /* The last item on the list of fixes must be a barrier, so
6111 we can never run off the end of the list of fixes without
6112 last_barrier being set. */
6113 if (ftmp == NULL)
6114 abort ();
6116 max_address = minipool_vector_head->max_address;
6117 /* Check that there isn't another fix that is in range that
6118 we couldn't fit into this pool because the pool was
6119 already too large: we need to put the pool before such an
6120 instruction. */
6121 if (ftmp->address < max_address)
6122 max_address = ftmp->address;
6124 last_barrier = create_fix_barrier (last_added_fix, max_address);
6127 assign_minipool_offsets (last_barrier);
6129 while (ftmp)
6131 if (GET_CODE (ftmp->insn) != BARRIER
6132 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
6133 == NULL))
6134 break;
6136 ftmp = ftmp->next;
6139 /* Scan over the fixes we have identified for this pool, fixing them
6140 up and adding the constants to the pool itself. */
6141 for (this_fix = fix; this_fix && ftmp != this_fix;
6142 this_fix = this_fix->next)
6143 if (GET_CODE (this_fix->insn) != BARRIER)
6145 rtx addr
6146 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
6147 minipool_vector_label),
6148 this_fix->minipool->offset);
6149 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
6152 dump_minipool (last_barrier->insn);
6153 fix = ftmp;
6156 /* From now on we must synthesize any constants that we can't handle
6157 directly. This can happen if the RTL gets split during final
6158 instruction generation. */
6159 after_arm_reorg = 1;
6161 /* Free the minipool memory. */
6162 obstack_free (&minipool_obstack, minipool_startobj);
6165 /* Routines to output assembly language. */
6167 /* If the rtx is the correct value then return the string of the number.
6168 In this way we can ensure that valid double constants are generated even
6169 when cross compiling. */
6170 const char *
6171 fp_immediate_constant (x)
6172 rtx x;
6174 REAL_VALUE_TYPE r;
6175 int i;
6177 if (!fpa_consts_inited)
6178 init_fpa_table ();
6180 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6181 for (i = 0; i < 8; i++)
6182 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
6183 return strings_fpa[i];
6185 abort ();
6188 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
6189 static const char *
6190 fp_const_from_val (r)
6191 REAL_VALUE_TYPE * r;
6193 int i;
6195 if (!fpa_consts_inited)
6196 init_fpa_table ();
6198 for (i = 0; i < 8; i++)
6199 if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
6200 return strings_fpa[i];
6202 abort ();
6205 /* Output the operands of a LDM/STM instruction to STREAM.
6206 MASK is the ARM register set mask of which only bits 0-15 are important.
6207 REG is the base register, either the frame pointer or the stack pointer,
6208 INSTR is the possibly suffixed load or store instruction. */
6210 static void
6211 print_multi_reg (stream, instr, reg, mask)
6212 FILE * stream;
6213 const char * instr;
6214 int reg;
6215 int mask;
6217 int i;
6218 int not_first = FALSE;
6220 fputc ('\t', stream);
6221 asm_fprintf (stream, instr, reg);
6222 fputs (", {", stream);
6224 for (i = 0; i <= LAST_ARM_REGNUM; i++)
6225 if (mask & (1 << i))
6227 if (not_first)
6228 fprintf (stream, ", ");
6230 asm_fprintf (stream, "%r", i);
6231 not_first = TRUE;
6234 fprintf (stream, "}%s\n", TARGET_APCS_32 ? "" : "^");
6237 /* Output a 'call' insn. */
6239 const char *
6240 output_call (operands)
6241 rtx * operands;
6243 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
6245 if (REGNO (operands[0]) == LR_REGNUM)
6247 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
6248 output_asm_insn ("mov%?\t%0, %|lr", operands);
6251 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6253 if (TARGET_INTERWORK)
6254 output_asm_insn ("bx%?\t%0", operands);
6255 else
6256 output_asm_insn ("mov%?\t%|pc, %0", operands);
6258 return "";
6261 static int
6262 eliminate_lr2ip (x)
6263 rtx * x;
6265 int something_changed = 0;
6266 rtx x0 = * x;
6267 int code = GET_CODE (x0);
6268 register int i, j;
6269 register const char * fmt;
6271 switch (code)
6273 case REG:
6274 if (REGNO (x0) == LR_REGNUM)
6276 *x = gen_rtx_REG (SImode, IP_REGNUM);
6277 return 1;
6279 return 0;
6280 default:
6281 /* Scan through the sub-elements and change any references there. */
6282 fmt = GET_RTX_FORMAT (code);
6284 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6285 if (fmt[i] == 'e')
6286 something_changed |= eliminate_lr2ip (&XEXP (x0, i));
6287 else if (fmt[i] == 'E')
6288 for (j = 0; j < XVECLEN (x0, i); j++)
6289 something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
6291 return something_changed;
6295 /* Output a 'call' insn that is a reference in memory. */
6297 const char *
6298 output_call_mem (operands)
6299 rtx * operands;
6301 operands[0] = copy_rtx (operands[0]); /* Be ultra careful. */
6302 /* Handle calls using lr by using ip (which may be clobbered in subr anyway). */
6303 if (eliminate_lr2ip (&operands[0]))
6304 output_asm_insn ("mov%?\t%|ip, %|lr", operands);
6306 if (TARGET_INTERWORK)
6308 output_asm_insn ("ldr%?\t%|ip, %0", operands);
6309 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6310 output_asm_insn ("bx%?\t%|ip", operands);
6312 else
6314 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
6315 output_asm_insn ("ldr%?\t%|pc, %0", operands);
6318 return "";
6322 /* Output a move from arm registers to an fpu registers.
6323 OPERANDS[0] is an fpu register.
6324 OPERANDS[1] is the first registers of an arm register pair. */
6326 const char *
6327 output_mov_long_double_fpu_from_arm (operands)
6328 rtx * operands;
6330 int arm_reg0 = REGNO (operands[1]);
6331 rtx ops[3];
6333 if (arm_reg0 == IP_REGNUM)
6334 abort ();
6336 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6337 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6338 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
6340 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
6341 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
6343 return "";
6346 /* Output a move from an fpu register to arm registers.
6347 OPERANDS[0] is the first registers of an arm register pair.
6348 OPERANDS[1] is an fpu register. */
6350 const char *
6351 output_mov_long_double_arm_from_fpu (operands)
6352 rtx * operands;
6354 int arm_reg0 = REGNO (operands[0]);
6355 rtx ops[3];
6357 if (arm_reg0 == IP_REGNUM)
6358 abort ();
6360 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6361 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6362 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
6364 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
6365 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
6366 return "";
6369 /* Output a move from arm registers to arm registers of a long double
6370 OPERANDS[0] is the destination.
6371 OPERANDS[1] is the source. */
6372 const char *
6373 output_mov_long_double_arm_from_arm (operands)
6374 rtx * operands;
6376 /* We have to be careful here because the two might overlap. */
6377 int dest_start = REGNO (operands[0]);
6378 int src_start = REGNO (operands[1]);
6379 rtx ops[2];
6380 int i;
6382 if (dest_start < src_start)
6384 for (i = 0; i < 3; i++)
6386 ops[0] = gen_rtx_REG (SImode, dest_start + i);
6387 ops[1] = gen_rtx_REG (SImode, src_start + i);
6388 output_asm_insn ("mov%?\t%0, %1", ops);
6391 else
6393 for (i = 2; i >= 0; i--)
6395 ops[0] = gen_rtx_REG (SImode, dest_start + i);
6396 ops[1] = gen_rtx_REG (SImode, src_start + i);
6397 output_asm_insn ("mov%?\t%0, %1", ops);
6401 return "";
6405 /* Output a move from arm registers to an fpu registers.
6406 OPERANDS[0] is an fpu register.
6407 OPERANDS[1] is the first registers of an arm register pair. */
6409 const char *
6410 output_mov_double_fpu_from_arm (operands)
6411 rtx * operands;
6413 int arm_reg0 = REGNO (operands[1]);
6414 rtx ops[2];
6416 if (arm_reg0 == IP_REGNUM)
6417 abort ();
6419 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6420 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6421 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
6422 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
6423 return "";
6426 /* Output a move from an fpu register to arm registers.
6427 OPERANDS[0] is the first registers of an arm register pair.
6428 OPERANDS[1] is an fpu register. */
6430 const char *
6431 output_mov_double_arm_from_fpu (operands)
6432 rtx * operands;
6434 int arm_reg0 = REGNO (operands[0]);
6435 rtx ops[2];
6437 if (arm_reg0 == IP_REGNUM)
6438 abort ();
6440 ops[0] = gen_rtx_REG (SImode, arm_reg0);
6441 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
6442 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
6443 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
6444 return "";
6447 /* Output a move between double words.
6448 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
6449 or MEM<-REG and all MEMs must be offsettable addresses. */
6451 const char *
6452 output_move_double (operands)
6453 rtx * operands;
6455 enum rtx_code code0 = GET_CODE (operands[0]);
6456 enum rtx_code code1 = GET_CODE (operands[1]);
6457 rtx otherops[3];
6459 if (code0 == REG)
6461 int reg0 = REGNO (operands[0]);
6463 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
6465 if (code1 == REG)
6467 int reg1 = REGNO (operands[1]);
6468 if (reg1 == IP_REGNUM)
6469 abort ();
6471 /* Ensure the second source is not overwritten. */
6472 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
6473 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
6474 else
6475 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
6477 else if (code1 == CONST_DOUBLE)
6479 if (GET_MODE (operands[1]) == DFmode)
6481 long l[2];
6482 union real_extract u;
6484 memcpy (&u, &CONST_DOUBLE_LOW (operands[1]), sizeof (u));
6485 REAL_VALUE_TO_TARGET_DOUBLE (u.d, l);
6486 otherops[1] = GEN_INT (l[1]);
6487 operands[1] = GEN_INT (l[0]);
6489 else if (GET_MODE (operands[1]) != VOIDmode)
6490 abort ();
6491 else if (WORDS_BIG_ENDIAN)
6494 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
6495 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
6497 else
6500 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
6501 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
6504 output_mov_immediate (operands);
6505 output_mov_immediate (otherops);
6507 else if (code1 == CONST_INT)
6509 #if HOST_BITS_PER_WIDE_INT > 32
6510 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
6511 what the upper word is. */
6512 if (WORDS_BIG_ENDIAN)
6514 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
6515 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
6517 else
6519 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
6520 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
6522 #else
6523 /* Sign extend the intval into the high-order word. */
6524 if (WORDS_BIG_ENDIAN)
6526 otherops[1] = operands[1];
6527 operands[1] = (INTVAL (operands[1]) < 0
6528 ? constm1_rtx : const0_rtx);
6530 else
6531 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
6532 #endif
6533 output_mov_immediate (otherops);
6534 output_mov_immediate (operands);
6536 else if (code1 == MEM)
6538 switch (GET_CODE (XEXP (operands[1], 0)))
6540 case REG:
6541 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
6542 break;
6544 case PRE_INC:
6545 abort (); /* Should never happen now. */
6546 break;
6548 case PRE_DEC:
6549 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
6550 break;
6552 case POST_INC:
6553 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
6554 break;
6556 case POST_DEC:
6557 abort (); /* Should never happen now. */
6558 break;
6560 case LABEL_REF:
6561 case CONST:
6562 output_asm_insn ("adr%?\t%0, %1", operands);
6563 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
6564 break;
6566 default:
6567 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
6568 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
6570 otherops[0] = operands[0];
6571 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
6572 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
6573 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
6575 if (GET_CODE (otherops[2]) == CONST_INT)
6577 switch (INTVAL (otherops[2]))
6579 case -8:
6580 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
6581 return "";
6582 case -4:
6583 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
6584 return "";
6585 case 4:
6586 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
6587 return "";
6589 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
6590 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
6591 else
6592 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6594 else
6595 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6597 else
6598 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
6600 return "ldm%?ia\t%0, %M0";
6602 else
6604 otherops[1] = adjust_address (operands[1], VOIDmode, 4);
6605 /* Take care of overlapping base/data reg. */
6606 if (reg_mentioned_p (operands[0], operands[1]))
6608 output_asm_insn ("ldr%?\t%0, %1", otherops);
6609 output_asm_insn ("ldr%?\t%0, %1", operands);
6611 else
6613 output_asm_insn ("ldr%?\t%0, %1", operands);
6614 output_asm_insn ("ldr%?\t%0, %1", otherops);
6619 else
6620 abort (); /* Constraints should prevent this. */
6622 else if (code0 == MEM && code1 == REG)
6624 if (REGNO (operands[1]) == IP_REGNUM)
6625 abort ();
6627 switch (GET_CODE (XEXP (operands[0], 0)))
6629 case REG:
6630 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
6631 break;
6633 case PRE_INC:
6634 abort (); /* Should never happen now. */
6635 break;
6637 case PRE_DEC:
6638 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
6639 break;
6641 case POST_INC:
6642 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
6643 break;
6645 case POST_DEC:
6646 abort (); /* Should never happen now. */
6647 break;
6649 case PLUS:
6650 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
6652 switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
6654 case -8:
6655 output_asm_insn ("stm%?db\t%m0, %M1", operands);
6656 return "";
6658 case -4:
6659 output_asm_insn ("stm%?da\t%m0, %M1", operands);
6660 return "";
6662 case 4:
6663 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
6664 return "";
6667 /* Fall through */
6669 default:
6670 otherops[0] = adjust_address (operands[0], VOIDmode, 4);
6671 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
6672 output_asm_insn ("str%?\t%1, %0", operands);
6673 output_asm_insn ("str%?\t%1, %0", otherops);
6676 else
6677 abort (); /* Constraints should prevent this */
6679 return "";
6683 /* Output an arbitrary MOV reg, #n.
6684 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
6686 const char *
6687 output_mov_immediate (operands)
6688 rtx * operands;
6690 HOST_WIDE_INT n = INTVAL (operands[1]);
6691 int n_ones = 0;
6692 int i;
6694 /* Try to use one MOV */
6695 if (const_ok_for_arm (n))
6697 output_asm_insn ("mov%?\t%0, %1", operands);
6698 return "";
6701 /* Try to use one MVN */
6702 if (const_ok_for_arm (~n))
6704 operands[1] = GEN_INT (~n);
6705 output_asm_insn ("mvn%?\t%0, %1", operands);
6706 return "";
6709 /* If all else fails, make it out of ORRs or BICs as appropriate. */
6711 for (i=0; i < 32; i++)
6712 if (n & 1 << i)
6713 n_ones++;
6715 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
6716 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~n);
6717 else
6718 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
6720 return "";
6724 /* Output an ADD r, s, #n where n may be too big for one instruction. If
6725 adding zero to one register, output nothing. */
6727 const char *
6728 output_add_immediate (operands)
6729 rtx * operands;
6731 HOST_WIDE_INT n = INTVAL (operands[2]);
6733 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
6735 if (n < 0)
6736 output_multi_immediate (operands,
6737 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
6738 -n);
6739 else
6740 output_multi_immediate (operands,
6741 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
6745 return "";
6748 /* Output a multiple immediate operation.
6749 OPERANDS is the vector of operands referred to in the output patterns.
6750 INSTR1 is the output pattern to use for the first constant.
6751 INSTR2 is the output pattern to use for subsequent constants.
6752 IMMED_OP is the index of the constant slot in OPERANDS.
6753 N is the constant value. */
6755 static const char *
6756 output_multi_immediate (operands, instr1, instr2, immed_op, n)
6757 rtx * operands;
6758 const char * instr1;
6759 const char * instr2;
6760 int immed_op;
6761 HOST_WIDE_INT n;
6763 #if HOST_BITS_PER_WIDE_INT > 32
6764 n &= 0xffffffff;
6765 #endif
6767 if (n == 0)
6769 operands[immed_op] = const0_rtx;
6770 output_asm_insn (instr1, operands); /* Quick and easy output. */
6772 else
6774 int i;
6775 const char * instr = instr1;
6777 /* Note that n is never zero here (which would give no output). */
6778 for (i = 0; i < 32; i += 2)
6780 if (n & (3 << i))
6782 operands[immed_op] = GEN_INT (n & (255 << i));
6783 output_asm_insn (instr, operands);
6784 instr = instr2;
6785 i += 6;
6790 return "";
6794 /* Return the appropriate ARM instruction for the operation code.
6795 The returned result should not be overwritten. OP is the rtx of the
6796 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
6797 was shifted. */
6799 const char *
6800 arithmetic_instr (op, shift_first_arg)
6801 rtx op;
6802 int shift_first_arg;
6804 switch (GET_CODE (op))
6806 case PLUS:
6807 return "add";
6809 case MINUS:
6810 return shift_first_arg ? "rsb" : "sub";
6812 case IOR:
6813 return "orr";
6815 case XOR:
6816 return "eor";
6818 case AND:
6819 return "and";
6821 default:
6822 abort ();
6827 /* Ensure valid constant shifts and return the appropriate shift mnemonic
6828 for the operation code. The returned result should not be overwritten.
6829 OP is the rtx code of the shift.
6830 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6831 shift. */
6833 static const char *
6834 shift_op (op, amountp)
6835 rtx op;
6836 HOST_WIDE_INT *amountp;
6838 const char * mnem;
6839 enum rtx_code code = GET_CODE (op);
6841 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
6842 *amountp = -1;
6843 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
6844 *amountp = INTVAL (XEXP (op, 1));
6845 else
6846 abort ();
6848 switch (code)
6850 case ASHIFT:
6851 mnem = "asl";
6852 break;
6854 case ASHIFTRT:
6855 mnem = "asr";
6856 break;
6858 case LSHIFTRT:
6859 mnem = "lsr";
6860 break;
6862 case ROTATERT:
6863 mnem = "ror";
6864 break;
6866 case MULT:
6867 /* We never have to worry about the amount being other than a
6868 power of 2, since this case can never be reloaded from a reg. */
6869 if (*amountp != -1)
6870 *amountp = int_log2 (*amountp);
6871 else
6872 abort ();
6873 return "asl";
6875 default:
6876 abort ();
6879 if (*amountp != -1)
6881 /* This is not 100% correct, but follows from the desire to merge
6882 multiplication by a power of 2 with the recognizer for a
6883 shift. >=32 is not a valid shift for "asl", so we must try and
6884 output a shift that produces the correct arithmetical result.
6885 Using lsr #32 is identical except for the fact that the carry bit
6886 is not set correctly if we set the flags; but we never use the
6887 carry bit from such an operation, so we can ignore that. */
6888 if (code == ROTATERT)
6889 *amountp &= 31; /* Rotate is just modulo 32 */
6890 else if (*amountp != (*amountp & 31))
6892 if (code == ASHIFT)
6893 mnem = "lsr";
6894 *amountp = 32;
6897 /* Shifts of 0 are no-ops. */
6898 if (*amountp == 0)
6899 return NULL;
6902 return mnem;
6906 /* Obtain the shift from the POWER of two. */
6907 static HOST_WIDE_INT
6908 int_log2 (power)
6909 HOST_WIDE_INT power;
6911 HOST_WIDE_INT shift = 0;
6913 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
6915 if (shift > 31)
6916 abort ();
6917 shift++;
6920 return shift;
6923 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
6924 /bin/as is horribly restrictive. */
6925 #define MAX_ASCII_LEN 51
6927 void
6928 output_ascii_pseudo_op (stream, p, len)
6929 FILE * stream;
6930 const unsigned char * p;
6931 int len;
6933 int i;
6934 int len_so_far = 0;
6936 fputs ("\t.ascii\t\"", stream);
6938 for (i = 0; i < len; i++)
6940 register int c = p[i];
6942 if (len_so_far >= MAX_ASCII_LEN)
6944 fputs ("\"\n\t.ascii\t\"", stream);
6945 len_so_far = 0;
6948 switch (c)
6950 case TARGET_TAB:
6951 fputs ("\\t", stream);
6952 len_so_far += 2;
6953 break;
6955 case TARGET_FF:
6956 fputs ("\\f", stream);
6957 len_so_far += 2;
6958 break;
6960 case TARGET_BS:
6961 fputs ("\\b", stream);
6962 len_so_far += 2;
6963 break;
6965 case TARGET_CR:
6966 fputs ("\\r", stream);
6967 len_so_far += 2;
6968 break;
6970 case TARGET_NEWLINE:
6971 fputs ("\\n", stream);
6972 c = p [i + 1];
6973 if ((c >= ' ' && c <= '~')
6974 || c == TARGET_TAB)
6975 /* This is a good place for a line break. */
6976 len_so_far = MAX_ASCII_LEN;
6977 else
6978 len_so_far += 2;
6979 break;
6981 case '\"':
6982 case '\\':
6983 putc ('\\', stream);
6984 len_so_far++;
6985 /* drop through. */
6987 default:
6988 if (c >= ' ' && c <= '~')
6990 putc (c, stream);
6991 len_so_far++;
6993 else
6995 fprintf (stream, "\\%03o", c);
6996 len_so_far += 4;
6998 break;
7002 fputs ("\"\n", stream);
7005 /* Compute the register sabe mask for registers 0 through 12
7006 inclusive. This code is used by both arm_compute_save_reg_mask
7007 and arm_compute_initial_elimination_offset. */
7009 static unsigned long
7010 arm_compute_save_reg0_reg12_mask ()
7012 unsigned long func_type = arm_current_func_type ();
7013 unsigned int save_reg_mask = 0;
7014 unsigned int reg;
7016 if (IS_INTERRUPT (func_type))
7018 unsigned int max_reg;
7019 /* Interrupt functions must not corrupt any registers,
7020 even call clobbered ones. If this is a leaf function
7021 we can just examine the registers used by the RTL, but
7022 otherwise we have to assume that whatever function is
7023 called might clobber anything, and so we have to save
7024 all the call-clobbered registers as well. */
7025 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
7026 /* FIQ handlers have registers r8 - r12 banked, so
7027 we only need to check r0 - r7, Normal ISRs only
7028 bank r14 and r15, so we must check up to r12.
7029 r13 is the stack pointer which is always preserved,
7030 so we do not need to consider it here. */
7031 max_reg = 7;
7032 else
7033 max_reg = 12;
7035 for (reg = 0; reg <= max_reg; reg++)
7036 if (regs_ever_live[reg]
7037 || (! current_function_is_leaf && call_used_regs [reg]))
7038 save_reg_mask |= (1 << reg);
7040 else
7042 /* In the normal case we only need to save those registers
7043 which are call saved and which are used by this function. */
7044 for (reg = 0; reg <= 10; reg++)
7045 if (regs_ever_live[reg] && ! call_used_regs [reg])
7046 save_reg_mask |= (1 << reg);
7048 /* Handle the frame pointer as a special case. */
7049 if (! TARGET_APCS_FRAME
7050 && ! frame_pointer_needed
7051 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
7052 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
7053 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
7055 /* If we aren't loading the PIC register,
7056 don't stack it even though it may be live. */
7057 if (flag_pic
7058 && ! TARGET_SINGLE_PIC_BASE
7059 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
7060 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
7063 return save_reg_mask;
7066 /* Compute a bit mask of which registers need to be
7067 saved on the stack for the current function. */
7069 static unsigned long
7070 arm_compute_save_reg_mask ()
7072 unsigned int save_reg_mask = 0;
7073 unsigned long func_type = arm_current_func_type ();
7075 if (IS_NAKED (func_type))
7076 /* This should never really happen. */
7077 return 0;
7079 /* If we are creating a stack frame, then we must save the frame pointer,
7080 IP (which will hold the old stack pointer), LR and the PC. */
7081 if (frame_pointer_needed)
7082 save_reg_mask |=
7083 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
7084 | (1 << IP_REGNUM)
7085 | (1 << LR_REGNUM)
7086 | (1 << PC_REGNUM);
7088 /* Volatile functions do not return, so there
7089 is no need to save any other registers. */
7090 if (IS_VOLATILE (func_type))
7091 return save_reg_mask;
7093 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
7095 /* Decide if we need to save the link register.
7096 Interrupt routines have their own banked link register,
7097 so they never need to save it.
7098 Otheriwse if we do not use the link register we do not need to save
7099 it. If we are pushing other registers onto the stack however, we
7100 can save an instruction in the epilogue by pushing the link register
7101 now and then popping it back into the PC. This incurs extra memory
7102 accesses though, so we only do it when optimising for size, and only
7103 if we know that we will not need a fancy return sequence. */
7104 if (! IS_INTERRUPT (func_type)
7105 && (regs_ever_live [LR_REGNUM]
7106 || (save_reg_mask
7107 && optimize_size
7108 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)))
7109 save_reg_mask |= 1 << LR_REGNUM;
7111 if (cfun->machine->lr_save_eliminated)
7112 save_reg_mask &= ~ (1 << LR_REGNUM);
7114 return save_reg_mask;
7117 /* Generate a function exit sequence. If REALLY_RETURN is true, then do
7118 everything bar the final return instruction. */
7120 const char *
7121 output_return_instruction (operand, really_return, reverse)
7122 rtx operand;
7123 int really_return;
7124 int reverse;
7126 char conditional[10];
7127 char instr[100];
7128 int reg;
7129 unsigned long live_regs_mask;
7130 unsigned long func_type;
7132 func_type = arm_current_func_type ();
7134 if (IS_NAKED (func_type))
7135 return "";
7137 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
7139 /* If this function was declared non-returning, and we have found a tail
7140 call, then we have to trust that the called function won't return. */
7141 if (really_return)
7143 rtx ops[2];
7145 /* Otherwise, trap an attempted return by aborting. */
7146 ops[0] = operand;
7147 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
7148 : "abort");
7149 assemble_external_libcall (ops[1]);
7150 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
7153 return "";
7156 if (current_function_calls_alloca && !really_return)
7157 abort ();
7159 /* Construct the conditional part of the instruction(s) to be emitted. */
7160 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
7162 return_used_this_function = 1;
7164 live_regs_mask = arm_compute_save_reg_mask ();
7166 /* On some ARM architectures it is faster to use LDR rather than LDM to
7167 load a single register. On other architectures, the cost is the same.
7168 In 26 bit mode we have to use LDM in order to be able to restore the CPSR. */
7169 if ((live_regs_mask == (1 << LR_REGNUM))
7170 && ! TARGET_INTERWORK
7171 && ! IS_INTERRUPT (func_type)
7172 && (! really_return || TARGET_APCS_32))
7174 if (! really_return)
7175 sprintf (instr, "ldr%s\t%%|lr, [%%|sp], #4", conditional);
7176 else
7177 sprintf (instr, "ldr%s\t%%|pc, [%%|sp], #4", conditional);
7179 else if (live_regs_mask)
7181 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
7182 /* There are two possible reasons for the IP register being saved.
7183 Either a stack frame was created, in which case IP contains the
7184 old stack pointer, or an ISR routine corrupted it. If this in an
7185 ISR routine then just restore IP, otherwise restore IP into SP. */
7186 if (! IS_INTERRUPT (func_type))
7188 live_regs_mask &= ~ (1 << IP_REGNUM);
7189 live_regs_mask |= (1 << SP_REGNUM);
7192 /* Generate the load multiple instruction to restore the registers. */
7193 if (frame_pointer_needed)
7194 sprintf (instr, "ldm%sea\t%%|fp, {", conditional);
7195 else
7196 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
7198 for (reg = 0; reg <= SP_REGNUM; reg++)
7199 if (live_regs_mask & (1 << reg))
7201 strcat (instr, "%|");
7202 strcat (instr, reg_names[reg]);
7203 strcat (instr, ", ");
7206 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
7208 /* If we are not restoring the LR register then we will
7209 have added one too many commas to the list above.
7210 Replace it with a closing brace. */
7211 instr [strlen (instr) - 2] = '}';
7213 else
7215 strcat (instr, "%|");
7217 /* At this point there should only be one or two registers left in
7218 live_regs_mask: always LR, and possibly PC if we created a stack
7219 frame. LR contains the return address. If we do not have any
7220 special requirements for function exit (eg interworking, or ISR)
7221 then we can load this value directly into the PC and save an
7222 instruction. */
7223 if (! TARGET_INTERWORK
7224 && ! IS_INTERRUPT (func_type)
7225 && really_return)
7226 strcat (instr, reg_names [PC_REGNUM]);
7227 else
7228 strcat (instr, reg_names [LR_REGNUM]);
7230 strcat (instr, (TARGET_APCS_32 || !really_return) ? "}" : "}^");
7233 if (really_return)
7235 /* See if we need to generate an extra instruction to
7236 perform the actual function return. */
7237 switch ((int) ARM_FUNC_TYPE (func_type))
7239 case ARM_FT_ISR:
7240 case ARM_FT_FIQ:
7241 output_asm_insn (instr, & operand);
7243 strcpy (instr, "sub");
7244 strcat (instr, conditional);
7245 strcat (instr, "s\t%|pc, %|lr, #4");
7246 break;
7248 case ARM_FT_EXCEPTION:
7249 output_asm_insn (instr, & operand);
7251 strcpy (instr, "mov");
7252 strcat (instr, conditional);
7253 strcat (instr, "s\t%|pc, %|lr");
7254 break;
7256 case ARM_FT_INTERWORKED:
7257 output_asm_insn (instr, & operand);
7259 strcpy (instr, "bx");
7260 strcat (instr, conditional);
7261 strcat (instr, "\t%|lr");
7262 break;
7264 default:
7265 /* The return has already been handled
7266 by loading the LR into the PC. */
7267 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
7269 output_asm_insn (instr, & operand);
7271 strcpy (instr, "mov");
7272 strcat (instr, conditional);
7273 if (! TARGET_APCS_32)
7274 strcat (instr, "s");
7275 strcat (instr, "\t%|pc, %|lr");
7277 break;
7281 else if (really_return)
7283 switch ((int) ARM_FUNC_TYPE (func_type))
7285 case ARM_FT_ISR:
7286 case ARM_FT_FIQ:
7287 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
7288 break;
7290 case ARM_FT_INTERWORKED:
7291 sprintf (instr, "bx%s\t%%|lr", conditional);
7292 break;
7294 case ARM_FT_EXCEPTION:
7295 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
7296 break;
7298 default:
7299 sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
7300 conditional, TARGET_APCS_32 ? "" : "s");
7301 break;
7304 else
7305 /* Nothing to load off the stack, and
7306 no return instruction to generate. */
7307 return "";
7309 output_asm_insn (instr, & operand);
7311 return "";
7314 /* Write the function name into the code section, directly preceding
7315 the function prologue.
7317 Code will be output similar to this:
7319 .ascii "arm_poke_function_name", 0
7320 .align
7322 .word 0xff000000 + (t1 - t0)
7323 arm_poke_function_name
7324 mov ip, sp
7325 stmfd sp!, {fp, ip, lr, pc}
7326 sub fp, ip, #4
7328 When performing a stack backtrace, code can inspect the value
7329 of 'pc' stored at 'fp' + 0. If the trace function then looks
7330 at location pc - 12 and the top 8 bits are set, then we know
7331 that there is a function name embedded immediately preceding this
7332 location and has length ((pc[-3]) & 0xff000000).
7334 We assume that pc is declared as a pointer to an unsigned long.
7336 It is of no benefit to output the function name if we are assembling
7337 a leaf function. These function types will not contain a stack
7338 backtrace structure, therefore it is not possible to determine the
7339 function name. */
7341 void
7342 arm_poke_function_name (stream, name)
7343 FILE * stream;
7344 const char * name;
7346 unsigned long alignlength;
7347 unsigned long length;
7348 rtx x;
7350 length = strlen (name) + 1;
7351 alignlength = ROUND_UP (length);
7353 ASM_OUTPUT_ASCII (stream, name, length);
7354 ASM_OUTPUT_ALIGN (stream, 2);
7355 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
7356 assemble_aligned_integer (UNITS_PER_WORD, x);
7359 /* Place some comments into the assembler stream
7360 describing the current function. */
7362 static void
7363 arm_output_function_prologue (f, frame_size)
7364 FILE * f;
7365 HOST_WIDE_INT frame_size;
7367 unsigned long func_type;
7369 if (!TARGET_ARM)
7371 thumb_output_function_prologue (f, frame_size);
7372 return;
7375 /* Sanity check. */
7376 if (arm_ccfsm_state || arm_target_insn)
7377 abort ();
7379 func_type = arm_current_func_type ();
7381 switch ((int) ARM_FUNC_TYPE (func_type))
7383 default:
7384 case ARM_FT_NORMAL:
7385 break;
7386 case ARM_FT_INTERWORKED:
7387 asm_fprintf (f, "\t%@ Function supports interworking.\n");
7388 break;
7389 case ARM_FT_EXCEPTION_HANDLER:
7390 asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
7391 break;
7392 case ARM_FT_ISR:
7393 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
7394 break;
7395 case ARM_FT_FIQ:
7396 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
7397 break;
7398 case ARM_FT_EXCEPTION:
7399 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
7400 break;
7403 if (IS_NAKED (func_type))
7404 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
7406 if (IS_VOLATILE (func_type))
7407 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
7409 if (IS_NESTED (func_type))
7410 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
7412 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
7413 current_function_args_size,
7414 current_function_pretend_args_size, frame_size);
7416 asm_fprintf (f, "\t%@ frame_needed = %d, current_function_anonymous_args = %d\n",
7417 frame_pointer_needed,
7418 current_function_anonymous_args);
7420 if (cfun->machine->lr_save_eliminated)
7421 asm_fprintf (f, "\t%@ link register save eliminated.\n");
7423 #ifdef AOF_ASSEMBLER
7424 if (flag_pic)
7425 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
7426 #endif
7428 return_used_this_function = 0;
7431 const char *
7432 arm_output_epilogue (really_return)
7433 int really_return;
7435 int reg;
7436 unsigned long saved_regs_mask;
7437 unsigned long func_type;
7438 /* If we need this, then it will always be at least this much. */
7439 int floats_offset = 12;
7440 rtx operands[3];
7441 int frame_size = get_frame_size ();
7442 FILE * f = asm_out_file;
7443 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
7445 /* If we have already generated the return instruction
7446 then it is futile to generate anything else. */
7447 if (use_return_insn (FALSE) && return_used_this_function)
7448 return "";
7450 func_type = arm_current_func_type ();
7452 if (IS_NAKED (func_type))
7453 /* Naked functions don't have epilogues. */
7454 return "";
7456 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
7458 rtx op;
7460 /* A volatile function should never return. Call abort. */
7461 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
7462 assemble_external_libcall (op);
7463 output_asm_insn ("bl\t%a0", &op);
7465 return "";
7468 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
7469 && ! really_return)
7470 /* If we are throwing an exception, then we really must
7471 be doing a return, so we can't tail-call. */
7472 abort ();
7474 saved_regs_mask = arm_compute_save_reg_mask ();
7476 /* Compute how far away the floats will be. */
7477 for (reg = 0; reg <= LAST_ARM_REGNUM; reg ++)
7478 if (saved_regs_mask & (1 << reg))
7479 floats_offset += 4;
7481 if (frame_pointer_needed)
7483 if (arm_fpu_arch == FP_SOFT2)
7485 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
7486 if (regs_ever_live[reg] && !call_used_regs[reg])
7488 floats_offset += 12;
7489 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
7490 reg, FP_REGNUM, floats_offset);
7493 else
7495 int start_reg = LAST_ARM_FP_REGNUM;
7497 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
7499 if (regs_ever_live[reg] && !call_used_regs[reg])
7501 floats_offset += 12;
7503 /* We can't unstack more than four registers at once. */
7504 if (start_reg - reg == 3)
7506 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
7507 reg, FP_REGNUM, floats_offset);
7508 start_reg = reg - 1;
7511 else
7513 if (reg != start_reg)
7514 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7515 reg + 1, start_reg - reg,
7516 FP_REGNUM, floats_offset);
7517 start_reg = reg - 1;
7521 /* Just in case the last register checked also needs unstacking. */
7522 if (reg != start_reg)
7523 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7524 reg + 1, start_reg - reg,
7525 FP_REGNUM, floats_offset);
7528 /* saved_regs_mask should contain the IP, which at the time of stack
7529 frame generation actually contains the old stack pointer. So a
7530 quick way to unwind the stack is just pop the IP register directly
7531 into the stack pointer. */
7532 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
7533 abort ();
7534 saved_regs_mask &= ~ (1 << IP_REGNUM);
7535 saved_regs_mask |= (1 << SP_REGNUM);
7537 /* There are two registers left in saved_regs_mask - LR and PC. We
7538 only need to restore the LR register (the return address), but to
7539 save time we can load it directly into the PC, unless we need a
7540 special function exit sequence, or we are not really returning. */
7541 if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
7542 /* Delete the LR from the register mask, so that the LR on
7543 the stack is loaded into the PC in the register mask. */
7544 saved_regs_mask &= ~ (1 << LR_REGNUM);
7545 else
7546 saved_regs_mask &= ~ (1 << PC_REGNUM);
7548 print_multi_reg (f, "ldmea\t%r", FP_REGNUM, saved_regs_mask);
7550 if (IS_INTERRUPT (func_type))
7551 /* Interrupt handlers will have pushed the
7552 IP onto the stack, so restore it now. */
7553 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, 1 << IP_REGNUM);
7555 else
7557 /* Restore stack pointer if necessary. */
7558 if (frame_size + current_function_outgoing_args_size != 0)
7560 operands[0] = operands[1] = stack_pointer_rtx;
7561 operands[2] = GEN_INT (frame_size
7562 + current_function_outgoing_args_size);
7563 output_add_immediate (operands);
7566 if (arm_fpu_arch == FP_SOFT2)
7568 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7569 if (regs_ever_live[reg] && !call_used_regs[reg])
7570 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
7571 reg, SP_REGNUM);
7573 else
7575 int start_reg = FIRST_ARM_FP_REGNUM;
7577 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7579 if (regs_ever_live[reg] && !call_used_regs[reg])
7581 if (reg - start_reg == 3)
7583 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
7584 start_reg, SP_REGNUM);
7585 start_reg = reg + 1;
7588 else
7590 if (reg != start_reg)
7591 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7592 start_reg, reg - start_reg,
7593 SP_REGNUM);
7595 start_reg = reg + 1;
7599 /* Just in case the last register checked also needs unstacking. */
7600 if (reg != start_reg)
7601 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7602 start_reg, reg - start_reg, SP_REGNUM);
7605 /* If we can, restore the LR into the PC. */
7606 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
7607 && really_return
7608 && current_function_pretend_args_size == 0
7609 && saved_regs_mask & (1 << LR_REGNUM))
7611 saved_regs_mask &= ~ (1 << LR_REGNUM);
7612 saved_regs_mask |= (1 << PC_REGNUM);
7615 /* Load the registers off the stack. If we only have one register
7616 to load use the LDR instruction - it is faster. */
7617 if (saved_regs_mask == (1 << LR_REGNUM))
7619 /* The excpetion handler ignores the LR, so we do
7620 not really need to load it off the stack. */
7621 if (eh_ofs)
7622 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
7623 else
7624 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
7626 else if (saved_regs_mask)
7627 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
7629 if (current_function_pretend_args_size)
7631 /* Unwind the pre-pushed regs. */
7632 operands[0] = operands[1] = stack_pointer_rtx;
7633 operands[2] = GEN_INT (current_function_pretend_args_size);
7634 output_add_immediate (operands);
7638 #if 0
7639 if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER)
7640 /* Adjust the stack to remove the exception handler stuff. */
7641 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7642 REGNO (eh_ofs));
7643 #endif
7645 if (! really_return)
7646 return "";
7648 /* Generate the return instruction. */
7649 switch ((int) ARM_FUNC_TYPE (func_type))
7651 case ARM_FT_EXCEPTION_HANDLER:
7652 /* Even in 26-bit mode we do a mov (rather than a movs)
7653 because we don't have the PSR bits set in the address. */
7654 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
7655 break;
7657 case ARM_FT_ISR:
7658 case ARM_FT_FIQ:
7659 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
7660 break;
7662 case ARM_FT_EXCEPTION:
7663 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7664 break;
7666 case ARM_FT_INTERWORKED:
7667 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
7668 break;
7670 default:
7671 if (frame_pointer_needed)
7672 /* If we used the frame pointer then the return adddress
7673 will have been loaded off the stack directly into the
7674 PC, so there is no need to issue a MOV instruction
7675 here. */
7677 else if (current_function_pretend_args_size == 0
7678 && (saved_regs_mask & (1 << LR_REGNUM)))
7679 /* Similarly we may have been able to load LR into the PC
7680 even if we did not create a stack frame. */
7682 else if (TARGET_APCS_32)
7683 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7684 else
7685 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
7686 break;
7689 return "";
7692 static void
7693 arm_output_function_epilogue (file, frame_size)
7694 FILE *file ATTRIBUTE_UNUSED;
7695 HOST_WIDE_INT frame_size;
7697 if (TARGET_THUMB)
7699 /* ??? Probably not safe to set this here, since it assumes that a
7700 function will be emitted as assembly immediately after we generate
7701 RTL for it. This does not happen for inline functions. */
7702 return_used_this_function = 0;
7704 else
7706 if (use_return_insn (FALSE)
7707 && return_used_this_function
7708 && (frame_size + current_function_outgoing_args_size) != 0
7709 && !frame_pointer_needed)
7710 abort ();
7712 /* Reset the ARM-specific per-function variables. */
7713 current_function_anonymous_args = 0;
7714 after_arm_reorg = 0;
7718 /* Generate and emit an insn that we will recognize as a push_multi.
7719 Unfortunately, since this insn does not reflect very well the actual
7720 semantics of the operation, we need to annotate the insn for the benefit
7721 of DWARF2 frame unwind information. */
7723 static rtx
7724 emit_multi_reg_push (mask)
7725 int mask;
7727 int num_regs = 0;
7728 int num_dwarf_regs;
7729 int i, j;
7730 rtx par;
7731 rtx dwarf;
7732 int dwarf_par_index;
7733 rtx tmp, reg;
7735 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7736 if (mask & (1 << i))
7737 num_regs++;
7739 if (num_regs == 0 || num_regs > 16)
7740 abort ();
7742 /* We don't record the PC in the dwarf frame information. */
7743 num_dwarf_regs = num_regs;
7744 if (mask & (1 << PC_REGNUM))
7745 num_dwarf_regs--;
7747 /* For the body of the insn we are going to generate an UNSPEC in
7748 parallel with several USEs. This allows the insn to be recognised
7749 by the push_multi pattern in the arm.md file. The insn looks
7750 something like this:
7752 (parallel [
7753 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
7754 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
7755 (use (reg:SI 11 fp))
7756 (use (reg:SI 12 ip))
7757 (use (reg:SI 14 lr))
7758 (use (reg:SI 15 pc))
7761 For the frame note however, we try to be more explicit and actually
7762 show each register being stored into the stack frame, plus a (single)
7763 decrement of the stack pointer. We do it this way in order to be
7764 friendly to the stack unwinding code, which only wants to see a single
7765 stack decrement per instruction. The RTL we generate for the note looks
7766 something like this:
7768 (sequence [
7769 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
7770 (set (mem:SI (reg:SI sp)) (reg:SI r4))
7771 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
7772 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
7773 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
7776 This sequence is used both by the code to support stack unwinding for
7777 exceptions handlers and the code to generate dwarf2 frame debugging. */
7779 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
7780 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
7781 RTX_FRAME_RELATED_P (dwarf) = 1;
7782 dwarf_par_index = 1;
7784 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7786 if (mask & (1 << i))
7788 reg = gen_rtx_REG (SImode, i);
7790 XVECEXP (par, 0, 0)
7791 = gen_rtx_SET (VOIDmode,
7792 gen_rtx_MEM (BLKmode,
7793 gen_rtx_PRE_DEC (BLKmode,
7794 stack_pointer_rtx)),
7795 gen_rtx_UNSPEC (BLKmode,
7796 gen_rtvec (1, reg),
7797 UNSPEC_PUSH_MULT));
7799 if (i != PC_REGNUM)
7801 tmp = gen_rtx_SET (VOIDmode,
7802 gen_rtx_MEM (SImode, stack_pointer_rtx),
7803 reg);
7804 RTX_FRAME_RELATED_P (tmp) = 1;
7805 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
7806 dwarf_par_index++;
7809 break;
7813 for (j = 1, i++; j < num_regs; i++)
7815 if (mask & (1 << i))
7817 reg = gen_rtx_REG (SImode, i);
7819 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
7821 if (i != PC_REGNUM)
7823 tmp = gen_rtx_SET (VOIDmode,
7824 gen_rtx_MEM (SImode,
7825 plus_constant (stack_pointer_rtx,
7826 4 * j)),
7827 reg);
7828 RTX_FRAME_RELATED_P (tmp) = 1;
7829 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
7832 j++;
7836 par = emit_insn (par);
7838 tmp = gen_rtx_SET (SImode,
7839 stack_pointer_rtx,
7840 gen_rtx_PLUS (SImode,
7841 stack_pointer_rtx,
7842 GEN_INT (-4 * num_regs)));
7843 RTX_FRAME_RELATED_P (tmp) = 1;
7844 XVECEXP (dwarf, 0, 0) = tmp;
7846 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7847 REG_NOTES (par));
7848 return par;
7851 static rtx
7852 emit_sfm (base_reg, count)
7853 int base_reg;
7854 int count;
7856 rtx par;
7857 rtx dwarf;
7858 rtx tmp, reg;
7859 int i;
7861 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7862 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7863 RTX_FRAME_RELATED_P (dwarf) = 1;
7865 reg = gen_rtx_REG (XFmode, base_reg++);
7867 XVECEXP (par, 0, 0)
7868 = gen_rtx_SET (VOIDmode,
7869 gen_rtx_MEM (BLKmode,
7870 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7871 gen_rtx_UNSPEC (BLKmode,
7872 gen_rtvec (1, reg),
7873 UNSPEC_PUSH_MULT));
7875 = gen_rtx_SET (VOIDmode,
7876 gen_rtx_MEM (XFmode,
7877 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7878 reg);
7879 RTX_FRAME_RELATED_P (tmp) = 1;
7880 XVECEXP (dwarf, 0, count - 1) = tmp;
7882 for (i = 1; i < count; i++)
7884 reg = gen_rtx_REG (XFmode, base_reg++);
7885 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7887 tmp = gen_rtx_SET (VOIDmode,
7888 gen_rtx_MEM (XFmode,
7889 gen_rtx_PRE_DEC (BLKmode,
7890 stack_pointer_rtx)),
7891 reg);
7892 RTX_FRAME_RELATED_P (tmp) = 1;
7893 XVECEXP (dwarf, 0, count - i - 1) = tmp;
7896 par = emit_insn (par);
7897 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7898 REG_NOTES (par));
7899 return par;
7902 /* Compute the distance from register FROM to register TO.
7903 These can be the arg pointer (26), the soft frame pointer (25),
7904 the stack pointer (13) or the hard frame pointer (11).
7905 Typical stack layout looks like this:
7907 old stack pointer -> | |
7908 ----
7909 | | \
7910 | | saved arguments for
7911 | | vararg functions
7912 | | /
7914 hard FP & arg pointer -> | | \
7915 | | stack
7916 | | frame
7917 | | /
7919 | | \
7920 | | call saved
7921 | | registers
7922 soft frame pointer -> | | /
7924 | | \
7925 | | local
7926 | | variables
7927 | | /
7929 | | \
7930 | | outgoing
7931 | | arguments
7932 current stack pointer -> | | /
7935 For a given funciton some or all of these stack compomnents
7936 may not be needed, giving rise to the possibility of
7937 eliminating some of the registers.
7939 The values returned by this function must reflect the behaviour
7940 of arm_expand_prologue() and arm_compute_save_reg_mask().
7942 The sign of the number returned reflects the direction of stack
7943 growth, so the values are positive for all eliminations except
7944 from the soft frame pointer to the hard frame pointer. */
7946 unsigned int
7947 arm_compute_initial_elimination_offset (from, to)
7948 unsigned int from;
7949 unsigned int to;
7951 unsigned int local_vars = (get_frame_size () + 3) & ~3;
7952 unsigned int outgoing_args = current_function_outgoing_args_size;
7953 unsigned int stack_frame;
7954 unsigned int call_saved_registers;
7955 unsigned long func_type;
7957 func_type = arm_current_func_type ();
7959 /* Volatile functions never return, so there is
7960 no need to save call saved registers. */
7961 call_saved_registers = 0;
7962 if (! IS_VOLATILE (func_type))
7964 unsigned int reg_mask;
7965 unsigned int reg;
7967 /* Makre sure that we compute which registers will be saved
7968 on the stack using the same algorithm that is used by
7969 arm_compute_save_reg_mask(). */
7970 reg_mask = arm_compute_save_reg0_reg12_mask ();
7972 /* Now count the number of bits set in save_reg_mask.
7973 For each set bit we need 4 bytes of stack space. */
7975 while (reg_mask)
7977 call_saved_registers += 4;
7978 reg_mask = reg_mask & ~ (reg_mask & - reg_mask);
7981 if (regs_ever_live[LR_REGNUM]
7982 /* If a stack frame is going to be created, the LR will
7983 be saved as part of that, so we do not need to allow
7984 for it here. */
7985 && ! frame_pointer_needed)
7986 call_saved_registers += 4;
7988 /* If the hard floating point registers are going to be
7989 used then they must be saved on the stack as well.
7990 Each register occupies 12 bytes of stack space. */
7991 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg ++)
7992 if (regs_ever_live[reg] && ! call_used_regs[reg])
7993 call_saved_registers += 12;
7996 /* The stack frame contains 4 registers - the old frame pointer,
7997 the old stack pointer, the return address and PC of the start
7998 of the function. */
7999 stack_frame = frame_pointer_needed ? 16 : 0;
8001 /* OK, now we have enough information to compute the distances.
8002 There must be an entry in these switch tables for each pair
8003 of registers in ELIMINABLE_REGS, even if some of the entries
8004 seem to be redundant or useless. */
8005 switch (from)
8007 case ARG_POINTER_REGNUM:
8008 switch (to)
8010 case THUMB_HARD_FRAME_POINTER_REGNUM:
8011 return 0;
8013 case FRAME_POINTER_REGNUM:
8014 /* This is the reverse of the soft frame pointer
8015 to hard frame pointer elimination below. */
8016 if (call_saved_registers == 0 && stack_frame == 0)
8017 return 0;
8018 return (call_saved_registers + stack_frame - 4);
8020 case ARM_HARD_FRAME_POINTER_REGNUM:
8021 /* If there is no stack frame then the hard
8022 frame pointer and the arg pointer coincide. */
8023 if (stack_frame == 0 && call_saved_registers != 0)
8024 return 0;
8025 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
8026 return (frame_pointer_needed
8027 && current_function_needs_context
8028 && ! current_function_anonymous_args) ? 4 : 0;
8030 case STACK_POINTER_REGNUM:
8031 /* If nothing has been pushed on the stack at all
8032 then this will return -4. This *is* correct! */
8033 return call_saved_registers + stack_frame + local_vars + outgoing_args - 4;
8035 default:
8036 abort ();
8038 break;
8040 case FRAME_POINTER_REGNUM:
8041 switch (to)
8043 case THUMB_HARD_FRAME_POINTER_REGNUM:
8044 return 0;
8046 case ARM_HARD_FRAME_POINTER_REGNUM:
8047 /* The hard frame pointer points to the top entry in the
8048 stack frame. The soft frame pointer to the bottom entry
8049 in the stack frame. If there is no stack frame at all,
8050 then they are identical. */
8051 if (call_saved_registers == 0 && stack_frame == 0)
8052 return 0;
8053 return - (call_saved_registers + stack_frame - 4);
8055 case STACK_POINTER_REGNUM:
8056 return local_vars + outgoing_args;
8058 default:
8059 abort ();
8061 break;
8063 default:
8064 /* You cannot eliminate from the stack pointer.
8065 In theory you could eliminate from the hard frame
8066 pointer to the stack pointer, but this will never
8067 happen, since if a stack frame is not needed the
8068 hard frame pointer will never be used. */
8069 abort ();
8073 /* Generate the prologue instructions for entry into an ARM function. */
8075 void
8076 arm_expand_prologue ()
8078 int reg;
8079 rtx amount;
8080 rtx insn;
8081 rtx ip_rtx;
8082 unsigned long live_regs_mask;
8083 unsigned long func_type;
8084 int fp_offset = 0;
8085 int saved_pretend_args = 0;
8086 unsigned int args_to_push;
8088 func_type = arm_current_func_type ();
8090 /* Naked functions don't have prologues. */
8091 if (IS_NAKED (func_type))
8092 return;
8094 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
8095 args_to_push = current_function_pretend_args_size;
8097 /* Compute which register we will have to save onto the stack. */
8098 live_regs_mask = arm_compute_save_reg_mask ();
8100 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
8102 if (frame_pointer_needed)
8104 if (IS_INTERRUPT (func_type))
8106 /* Interrupt functions must not corrupt any registers.
8107 Creating a frame pointer however, corrupts the IP
8108 register, so we must push it first. */
8109 insn = emit_multi_reg_push (1 << IP_REGNUM);
8111 /* Do not set RTX_FRAME_RELATED_P on this insn.
8112 The dwarf stack unwinding code only wants to see one
8113 stack decrement per function, and this is not it. If
8114 this instruction is labeled as being part of the frame
8115 creation sequence then dwarf2out_frame_debug_expr will
8116 abort when it encounters the assignment of IP to FP
8117 later on, since the use of SP here establishes SP as
8118 the CFA register and not IP.
8120 Anyway this instruction is not really part of the stack
8121 frame creation although it is part of the prologue. */
8123 else if (IS_NESTED (func_type))
8125 /* The Static chain register is the same as the IP register
8126 used as a scratch register during stack frame creation.
8127 To get around this need to find somewhere to store IP
8128 whilst the frame is being created. We try the following
8129 places in order:
8131 1. The last argument register.
8132 2. A slot on the stack above the frame. (This only
8133 works if the function is not a varargs function).
8134 3. Register r3, after pushing the argument registers
8135 onto the stack.
8137 Note - we only need to tell the dwarf2 backend about the SP
8138 adjustment in the second variant; the static chain register
8139 doesn't need to be unwound, as it doesn't contain a value
8140 inherited from the caller. */
8142 if (regs_ever_live[3] == 0)
8144 insn = gen_rtx_REG (SImode, 3);
8145 insn = gen_rtx_SET (SImode, insn, ip_rtx);
8146 insn = emit_insn (insn);
8148 else if (args_to_push == 0)
8150 rtx dwarf;
8151 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
8152 insn = gen_rtx_MEM (SImode, insn);
8153 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
8154 insn = emit_insn (insn);
8156 fp_offset = 4;
8158 /* Just tell the dwarf backend that we adjusted SP. */
8159 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8160 gen_rtx_PLUS (SImode, stack_pointer_rtx,
8161 GEN_INT (-fp_offset)));
8162 RTX_FRAME_RELATED_P (insn) = 1;
8163 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8164 dwarf, REG_NOTES (insn));
8166 else
8168 /* Store the args on the stack. */
8169 if (current_function_anonymous_args)
8170 insn = emit_multi_reg_push
8171 ((0xf0 >> (args_to_push / 4)) & 0xf);
8172 else
8173 insn = emit_insn
8174 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8175 GEN_INT (- args_to_push)));
8177 RTX_FRAME_RELATED_P (insn) = 1;
8179 saved_pretend_args = 1;
8180 fp_offset = args_to_push;
8181 args_to_push = 0;
8183 /* Now reuse r3 to preserve IP. */
8184 insn = gen_rtx_REG (SImode, 3);
8185 insn = gen_rtx_SET (SImode, insn, ip_rtx);
8186 (void) emit_insn (insn);
8190 if (fp_offset)
8192 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
8193 insn = gen_rtx_SET (SImode, ip_rtx, insn);
8195 else
8196 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
8198 insn = emit_insn (insn);
8199 RTX_FRAME_RELATED_P (insn) = 1;
8202 if (args_to_push)
8204 /* Push the argument registers, or reserve space for them. */
8205 if (current_function_anonymous_args)
8206 insn = emit_multi_reg_push
8207 ((0xf0 >> (args_to_push / 4)) & 0xf);
8208 else
8209 insn = emit_insn
8210 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8211 GEN_INT (- args_to_push)));
8212 RTX_FRAME_RELATED_P (insn) = 1;
8215 if (live_regs_mask)
8217 insn = emit_multi_reg_push (live_regs_mask);
8218 RTX_FRAME_RELATED_P (insn) = 1;
8221 if (! IS_VOLATILE (func_type))
8223 /* Save any floating point call-saved registers used by this function. */
8224 if (arm_fpu_arch == FP_SOFT2)
8226 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
8227 if (regs_ever_live[reg] && !call_used_regs[reg])
8229 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
8230 insn = gen_rtx_MEM (XFmode, insn);
8231 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
8232 gen_rtx_REG (XFmode, reg)));
8233 RTX_FRAME_RELATED_P (insn) = 1;
8236 else
8238 int start_reg = LAST_ARM_FP_REGNUM;
8240 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
8242 if (regs_ever_live[reg] && !call_used_regs[reg])
8244 if (start_reg - reg == 3)
8246 insn = emit_sfm (reg, 4);
8247 RTX_FRAME_RELATED_P (insn) = 1;
8248 start_reg = reg - 1;
8251 else
8253 if (start_reg != reg)
8255 insn = emit_sfm (reg + 1, start_reg - reg);
8256 RTX_FRAME_RELATED_P (insn) = 1;
8258 start_reg = reg - 1;
8262 if (start_reg != reg)
8264 insn = emit_sfm (reg + 1, start_reg - reg);
8265 RTX_FRAME_RELATED_P (insn) = 1;
8270 if (frame_pointer_needed)
8272 /* Create the new frame pointer. */
8273 insn = GEN_INT (-(4 + args_to_push + fp_offset));
8274 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
8275 RTX_FRAME_RELATED_P (insn) = 1;
8277 if (IS_NESTED (func_type))
8279 /* Recover the static chain register. */
8280 if (regs_ever_live [3] == 0
8281 || saved_pretend_args)
8283 insn = gen_rtx_REG (SImode, 3);
8284 insn = gen_rtx_SET (SImode, ip_rtx, insn);
8285 (void) emit_insn (insn);
8287 else /* if (current_function_pretend_args_size == 0) */
8289 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx, GEN_INT (4));
8290 insn = gen_rtx_MEM (SImode, insn);
8291 insn = gen_rtx_SET (SImode, ip_rtx, insn);
8292 (void) emit_insn (insn);
8297 amount = GEN_INT (-(get_frame_size ()
8298 + current_function_outgoing_args_size));
8300 if (amount != const0_rtx)
8302 /* This add can produce multiple insns for a large constant, so we
8303 need to get tricky. */
8304 rtx last = get_last_insn ();
8305 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8306 amount));
8309 last = last ? NEXT_INSN (last) : get_insns ();
8310 RTX_FRAME_RELATED_P (last) = 1;
8312 while (last != insn);
8314 /* If the frame pointer is needed, emit a special barrier that
8315 will prevent the scheduler from moving stores to the frame
8316 before the stack adjustment. */
8317 if (frame_pointer_needed)
8319 rtx unspec = gen_rtx_UNSPEC (SImode,
8320 gen_rtvec (2, stack_pointer_rtx,
8321 hard_frame_pointer_rtx),
8322 UNSPEC_PRLG_STK);
8324 insn = emit_insn (gen_rtx_CLOBBER (VOIDmode,
8325 gen_rtx_MEM (BLKmode, unspec)));
8329 /* If we are profiling, make sure no instructions are scheduled before
8330 the call to mcount. Similarly if the user has requested no
8331 scheduling in the prolog. */
8332 if (profile_flag || TARGET_NO_SCHED_PRO)
8333 emit_insn (gen_blockage ());
8335 /* If the link register is being kept alive, with the return address in it,
8336 then make sure that it does not get reused by the ce2 pass. */
8337 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
8339 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
8340 cfun->machine->lr_save_eliminated = 1;
8344 /* If CODE is 'd', then the X is a condition operand and the instruction
8345 should only be executed if the condition is true.
8346 if CODE is 'D', then the X is a condition operand and the instruction
8347 should only be executed if the condition is false: however, if the mode
8348 of the comparison is CCFPEmode, then always execute the instruction -- we
8349 do this because in these circumstances !GE does not necessarily imply LT;
8350 in these cases the instruction pattern will take care to make sure that
8351 an instruction containing %d will follow, thereby undoing the effects of
8352 doing this instruction unconditionally.
8353 If CODE is 'N' then X is a floating point operand that must be negated
8354 before output.
8355 If CODE is 'B' then output a bitwise inverted value of X (a const int).
8356 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
8358 void
8359 arm_print_operand (stream, x, code)
8360 FILE * stream;
8361 rtx x;
8362 int code;
8364 switch (code)
8366 case '@':
8367 fputs (ASM_COMMENT_START, stream);
8368 return;
8370 case '_':
8371 fputs (user_label_prefix, stream);
8372 return;
8374 case '|':
8375 fputs (REGISTER_PREFIX, stream);
8376 return;
8378 case '?':
8379 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
8381 if (TARGET_THUMB || current_insn_predicate != NULL)
8382 abort ();
8384 fputs (arm_condition_codes[arm_current_cc], stream);
8386 else if (current_insn_predicate)
8388 enum arm_cond_code code;
8390 if (TARGET_THUMB)
8391 abort ();
8393 code = get_arm_condition_code (current_insn_predicate);
8394 fputs (arm_condition_codes[code], stream);
8396 return;
8398 case 'N':
8400 REAL_VALUE_TYPE r;
8401 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8402 r = REAL_VALUE_NEGATE (r);
8403 fprintf (stream, "%s", fp_const_from_val (&r));
8405 return;
8407 case 'B':
8408 if (GET_CODE (x) == CONST_INT)
8410 HOST_WIDE_INT val;
8411 val = ARM_SIGN_EXTEND (~INTVAL (x));
8412 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
8414 else
8416 putc ('~', stream);
8417 output_addr_const (stream, x);
8419 return;
8421 case 'i':
8422 fprintf (stream, "%s", arithmetic_instr (x, 1));
8423 return;
8425 case 'I':
8426 fprintf (stream, "%s", arithmetic_instr (x, 0));
8427 return;
8429 case 'S':
8431 HOST_WIDE_INT val;
8432 const char * shift = shift_op (x, &val);
8434 if (shift)
8436 fprintf (stream, ", %s ", shift_op (x, &val));
8437 if (val == -1)
8438 arm_print_operand (stream, XEXP (x, 1), 0);
8439 else
8441 fputc ('#', stream);
8442 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
8446 return;
8448 /* An explanation of the 'Q', 'R' and 'H' register operands:
8450 In a pair of registers containing a DI or DF value the 'Q'
8451 operand returns the register number of the register containing
8452 the least signficant part of the value. The 'R' operand returns
8453 the register number of the register containing the most
8454 significant part of the value.
8456 The 'H' operand returns the higher of the two register numbers.
8457 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
8458 same as the 'Q' operand, since the most signficant part of the
8459 value is held in the lower number register. The reverse is true
8460 on systems where WORDS_BIG_ENDIAN is false.
8462 The purpose of these operands is to distinguish between cases
8463 where the endian-ness of the values is important (for example
8464 when they are added together), and cases where the endian-ness
8465 is irrelevant, but the order of register operations is important.
8466 For example when loading a value from memory into a register
8467 pair, the endian-ness does not matter. Provided that the value
8468 from the lower memory address is put into the lower numbered
8469 register, and the value from the higher address is put into the
8470 higher numbered register, the load will work regardless of whether
8471 the value being loaded is big-wordian or little-wordian. The
8472 order of the two register loads can matter however, if the address
8473 of the memory location is actually held in one of the registers
8474 being overwritten by the load. */
8475 case 'Q':
8476 if (REGNO (x) > LAST_ARM_REGNUM)
8477 abort ();
8478 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
8479 return;
8481 case 'R':
8482 if (REGNO (x) > LAST_ARM_REGNUM)
8483 abort ();
8484 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
8485 return;
8487 case 'H':
8488 if (REGNO (x) > LAST_ARM_REGNUM)
8489 abort ();
8490 asm_fprintf (stream, "%r", REGNO (x) + 1);
8491 return;
8493 case 'm':
8494 asm_fprintf (stream, "%r",
8495 GET_CODE (XEXP (x, 0)) == REG
8496 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
8497 return;
8499 case 'M':
8500 asm_fprintf (stream, "{%r-%r}",
8501 REGNO (x),
8502 REGNO (x) + NUM_REGS (GET_MODE (x)) - 1);
8503 return;
8505 case 'd':
8506 if (!x)
8507 return;
8509 if (TARGET_ARM)
8510 fputs (arm_condition_codes[get_arm_condition_code (x)],
8511 stream);
8512 else
8513 fputs (thumb_condition_code (x, 0), stream);
8514 return;
8516 case 'D':
8517 if (!x)
8518 return;
8520 if (TARGET_ARM)
8521 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
8522 (get_arm_condition_code (x))],
8523 stream);
8524 else
8525 fputs (thumb_condition_code (x, 1), stream);
8526 return;
8528 default:
8529 if (x == 0)
8530 abort ();
8532 if (GET_CODE (x) == REG)
8533 asm_fprintf (stream, "%r", REGNO (x));
8534 else if (GET_CODE (x) == MEM)
8536 output_memory_reference_mode = GET_MODE (x);
8537 output_address (XEXP (x, 0));
8539 else if (GET_CODE (x) == CONST_DOUBLE)
8540 fprintf (stream, "#%s", fp_immediate_constant (x));
8541 else if (GET_CODE (x) == NEG)
8542 abort (); /* This should never happen now. */
8543 else
8545 fputc ('#', stream);
8546 output_addr_const (stream, x);
8551 #ifndef AOF_ASSEMBLER
8552 /* Target hook for assembling integer objects. The ARM version needs to
8553 handle word-sized values specially. */
8555 static bool
8556 arm_assemble_integer (x, size, aligned_p)
8557 rtx x;
8558 unsigned int size;
8559 int aligned_p;
8561 if (size == UNITS_PER_WORD && aligned_p)
8563 fputs ("\t.word\t", asm_out_file);
8564 output_addr_const (asm_out_file, x);
8566 /* Mark symbols as position independent. We only do this in the
8567 .text segment, not in the .data segment. */
8568 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
8569 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
8571 if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8572 fputs ("(GOTOFF)", asm_out_file);
8573 else if (GET_CODE (x) == LABEL_REF)
8574 fputs ("(GOTOFF)", asm_out_file);
8575 else
8576 fputs ("(GOT)", asm_out_file);
8578 fputc ('\n', asm_out_file);
8579 return true;
8581 return default_assemble_integer (x, size, aligned_p);
8583 #endif
8585 /* A finite state machine takes care of noticing whether or not instructions
8586 can be conditionally executed, and thus decrease execution time and code
8587 size by deleting branch instructions. The fsm is controlled by
8588 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
8590 /* The state of the fsm controlling condition codes are:
8591 0: normal, do nothing special
8592 1: make ASM_OUTPUT_OPCODE not output this instruction
8593 2: make ASM_OUTPUT_OPCODE not output this instruction
8594 3: make instructions conditional
8595 4: make instructions conditional
8597 State transitions (state->state by whom under condition):
8598 0 -> 1 final_prescan_insn if the `target' is a label
8599 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
8600 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
8601 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
8602 3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
8603 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
8604 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
8605 (the target insn is arm_target_insn).
8607 If the jump clobbers the conditions then we use states 2 and 4.
8609 A similar thing can be done with conditional return insns.
8611 XXX In case the `target' is an unconditional branch, this conditionalising
8612 of the instructions always reduces code size, but not always execution
8613 time. But then, I want to reduce the code size to somewhere near what
8614 /bin/cc produces. */
8616 /* Returns the index of the ARM condition code string in
8617 `arm_condition_codes'. COMPARISON should be an rtx like
8618 `(eq (...) (...))'. */
8620 static enum arm_cond_code
8621 get_arm_condition_code (comparison)
8622 rtx comparison;
8624 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
8625 register int code;
8626 register enum rtx_code comp_code = GET_CODE (comparison);
8628 if (GET_MODE_CLASS (mode) != MODE_CC)
8629 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
8630 XEXP (comparison, 1));
8632 switch (mode)
8634 case CC_DNEmode: code = ARM_NE; goto dominance;
8635 case CC_DEQmode: code = ARM_EQ; goto dominance;
8636 case CC_DGEmode: code = ARM_GE; goto dominance;
8637 case CC_DGTmode: code = ARM_GT; goto dominance;
8638 case CC_DLEmode: code = ARM_LE; goto dominance;
8639 case CC_DLTmode: code = ARM_LT; goto dominance;
8640 case CC_DGEUmode: code = ARM_CS; goto dominance;
8641 case CC_DGTUmode: code = ARM_HI; goto dominance;
8642 case CC_DLEUmode: code = ARM_LS; goto dominance;
8643 case CC_DLTUmode: code = ARM_CC;
8645 dominance:
8646 if (comp_code != EQ && comp_code != NE)
8647 abort ();
8649 if (comp_code == EQ)
8650 return ARM_INVERSE_CONDITION_CODE (code);
8651 return code;
8653 case CC_NOOVmode:
8654 switch (comp_code)
8656 case NE: return ARM_NE;
8657 case EQ: return ARM_EQ;
8658 case GE: return ARM_PL;
8659 case LT: return ARM_MI;
8660 default: abort ();
8663 case CC_Zmode:
8664 switch (comp_code)
8666 case NE: return ARM_NE;
8667 case EQ: return ARM_EQ;
8668 default: abort ();
8671 case CCFPEmode:
8672 case CCFPmode:
8673 /* These encodings assume that AC=1 in the FPA system control
8674 byte. This allows us to handle all cases except UNEQ and
8675 LTGT. */
8676 switch (comp_code)
8678 case GE: return ARM_GE;
8679 case GT: return ARM_GT;
8680 case LE: return ARM_LS;
8681 case LT: return ARM_MI;
8682 case NE: return ARM_NE;
8683 case EQ: return ARM_EQ;
8684 case ORDERED: return ARM_VC;
8685 case UNORDERED: return ARM_VS;
8686 case UNLT: return ARM_LT;
8687 case UNLE: return ARM_LE;
8688 case UNGT: return ARM_HI;
8689 case UNGE: return ARM_PL;
8690 /* UNEQ and LTGT do not have a representation. */
8691 case UNEQ: /* Fall through. */
8692 case LTGT: /* Fall through. */
8693 default: abort ();
8696 case CC_SWPmode:
8697 switch (comp_code)
8699 case NE: return ARM_NE;
8700 case EQ: return ARM_EQ;
8701 case GE: return ARM_LE;
8702 case GT: return ARM_LT;
8703 case LE: return ARM_GE;
8704 case LT: return ARM_GT;
8705 case GEU: return ARM_LS;
8706 case GTU: return ARM_CC;
8707 case LEU: return ARM_CS;
8708 case LTU: return ARM_HI;
8709 default: abort ();
8712 case CC_Cmode:
8713 switch (comp_code)
8715 case LTU: return ARM_CS;
8716 case GEU: return ARM_CC;
8717 default: abort ();
8720 case CCmode:
8721 switch (comp_code)
8723 case NE: return ARM_NE;
8724 case EQ: return ARM_EQ;
8725 case GE: return ARM_GE;
8726 case GT: return ARM_GT;
8727 case LE: return ARM_LE;
8728 case LT: return ARM_LT;
8729 case GEU: return ARM_CS;
8730 case GTU: return ARM_HI;
8731 case LEU: return ARM_LS;
8732 case LTU: return ARM_CC;
8733 default: abort ();
8736 default: abort ();
8739 abort ();
8743 void
8744 arm_final_prescan_insn (insn)
8745 rtx insn;
8747 /* BODY will hold the body of INSN. */
8748 register rtx body = PATTERN (insn);
8750 /* This will be 1 if trying to repeat the trick, and things need to be
8751 reversed if it appears to fail. */
8752 int reverse = 0;
8754 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
8755 taken are clobbered, even if the rtl suggests otherwise. It also
8756 means that we have to grub around within the jump expression to find
8757 out what the conditions are when the jump isn't taken. */
8758 int jump_clobbers = 0;
8760 /* If we start with a return insn, we only succeed if we find another one. */
8761 int seeking_return = 0;
8763 /* START_INSN will hold the insn from where we start looking. This is the
8764 first insn after the following code_label if REVERSE is true. */
8765 rtx start_insn = insn;
8767 /* If in state 4, check if the target branch is reached, in order to
8768 change back to state 0. */
8769 if (arm_ccfsm_state == 4)
8771 if (insn == arm_target_insn)
8773 arm_target_insn = NULL;
8774 arm_ccfsm_state = 0;
8776 return;
8779 /* If in state 3, it is possible to repeat the trick, if this insn is an
8780 unconditional branch to a label, and immediately following this branch
8781 is the previous target label which is only used once, and the label this
8782 branch jumps to is not too far off. */
8783 if (arm_ccfsm_state == 3)
8785 if (simplejump_p (insn))
8787 start_insn = next_nonnote_insn (start_insn);
8788 if (GET_CODE (start_insn) == BARRIER)
8790 /* XXX Isn't this always a barrier? */
8791 start_insn = next_nonnote_insn (start_insn);
8793 if (GET_CODE (start_insn) == CODE_LABEL
8794 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
8795 && LABEL_NUSES (start_insn) == 1)
8796 reverse = TRUE;
8797 else
8798 return;
8800 else if (GET_CODE (body) == RETURN)
8802 start_insn = next_nonnote_insn (start_insn);
8803 if (GET_CODE (start_insn) == BARRIER)
8804 start_insn = next_nonnote_insn (start_insn);
8805 if (GET_CODE (start_insn) == CODE_LABEL
8806 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
8807 && LABEL_NUSES (start_insn) == 1)
8809 reverse = TRUE;
8810 seeking_return = 1;
8812 else
8813 return;
8815 else
8816 return;
8819 if (arm_ccfsm_state != 0 && !reverse)
8820 abort ();
8821 if (GET_CODE (insn) != JUMP_INSN)
8822 return;
8824 /* This jump might be paralleled with a clobber of the condition codes
8825 the jump should always come first */
8826 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
8827 body = XVECEXP (body, 0, 0);
8829 #if 0
8830 /* If this is a conditional return then we don't want to know */
8831 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
8832 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
8833 && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
8834 || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
8835 return;
8836 #endif
8838 if (reverse
8839 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
8840 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
8842 int insns_skipped;
8843 int fail = FALSE, succeed = FALSE;
8844 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
8845 int then_not_else = TRUE;
8846 rtx this_insn = start_insn, label = 0;
8848 /* If the jump cannot be done with one instruction, we cannot
8849 conditionally execute the instruction in the inverse case. */
8850 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
8852 jump_clobbers = 1;
8853 return;
8856 /* Register the insn jumped to. */
8857 if (reverse)
8859 if (!seeking_return)
8860 label = XEXP (SET_SRC (body), 0);
8862 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
8863 label = XEXP (XEXP (SET_SRC (body), 1), 0);
8864 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
8866 label = XEXP (XEXP (SET_SRC (body), 2), 0);
8867 then_not_else = FALSE;
8869 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
8870 seeking_return = 1;
8871 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
8873 seeking_return = 1;
8874 then_not_else = FALSE;
8876 else
8877 abort ();
8879 /* See how many insns this branch skips, and what kind of insns. If all
8880 insns are okay, and the label or unconditional branch to the same
8881 label is not too far away, succeed. */
8882 for (insns_skipped = 0;
8883 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
8885 rtx scanbody;
8887 this_insn = next_nonnote_insn (this_insn);
8888 if (!this_insn)
8889 break;
8891 switch (GET_CODE (this_insn))
8893 case CODE_LABEL:
8894 /* Succeed if it is the target label, otherwise fail since
8895 control falls in from somewhere else. */
8896 if (this_insn == label)
8898 if (jump_clobbers)
8900 arm_ccfsm_state = 2;
8901 this_insn = next_nonnote_insn (this_insn);
8903 else
8904 arm_ccfsm_state = 1;
8905 succeed = TRUE;
8907 else
8908 fail = TRUE;
8909 break;
8911 case BARRIER:
8912 /* Succeed if the following insn is the target label.
8913 Otherwise fail.
8914 If return insns are used then the last insn in a function
8915 will be a barrier. */
8916 this_insn = next_nonnote_insn (this_insn);
8917 if (this_insn && this_insn == label)
8919 if (jump_clobbers)
8921 arm_ccfsm_state = 2;
8922 this_insn = next_nonnote_insn (this_insn);
8924 else
8925 arm_ccfsm_state = 1;
8926 succeed = TRUE;
8928 else
8929 fail = TRUE;
8930 break;
8932 case CALL_INSN:
8933 /* If using 32-bit addresses the cc is not preserved over
8934 calls. */
8935 if (TARGET_APCS_32)
8937 /* Succeed if the following insn is the target label,
8938 or if the following two insns are a barrier and
8939 the target label. */
8940 this_insn = next_nonnote_insn (this_insn);
8941 if (this_insn && GET_CODE (this_insn) == BARRIER)
8942 this_insn = next_nonnote_insn (this_insn);
8944 if (this_insn && this_insn == label
8945 && insns_skipped < max_insns_skipped)
8947 if (jump_clobbers)
8949 arm_ccfsm_state = 2;
8950 this_insn = next_nonnote_insn (this_insn);
8952 else
8953 arm_ccfsm_state = 1;
8954 succeed = TRUE;
8956 else
8957 fail = TRUE;
8959 break;
8961 case JUMP_INSN:
8962 /* If this is an unconditional branch to the same label, succeed.
8963 If it is to another label, do nothing. If it is conditional,
8964 fail. */
8965 /* XXX Probably, the tests for SET and the PC are unnecessary. */
8967 scanbody = PATTERN (this_insn);
8968 if (GET_CODE (scanbody) == SET
8969 && GET_CODE (SET_DEST (scanbody)) == PC)
8971 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
8972 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
8974 arm_ccfsm_state = 2;
8975 succeed = TRUE;
8977 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
8978 fail = TRUE;
8980 /* Fail if a conditional return is undesirable (eg on a
8981 StrongARM), but still allow this if optimizing for size. */
8982 else if (GET_CODE (scanbody) == RETURN
8983 && !use_return_insn (TRUE)
8984 && !optimize_size)
8985 fail = TRUE;
8986 else if (GET_CODE (scanbody) == RETURN
8987 && seeking_return)
8989 arm_ccfsm_state = 2;
8990 succeed = TRUE;
8992 else if (GET_CODE (scanbody) == PARALLEL)
8994 switch (get_attr_conds (this_insn))
8996 case CONDS_NOCOND:
8997 break;
8998 default:
8999 fail = TRUE;
9000 break;
9003 else
9004 fail = TRUE; /* Unrecognized jump (eg epilogue). */
9006 break;
9008 case INSN:
9009 /* Instructions using or affecting the condition codes make it
9010 fail. */
9011 scanbody = PATTERN (this_insn);
9012 if (!(GET_CODE (scanbody) == SET
9013 || GET_CODE (scanbody) == PARALLEL)
9014 || get_attr_conds (this_insn) != CONDS_NOCOND)
9015 fail = TRUE;
9016 break;
9018 default:
9019 break;
9022 if (succeed)
9024 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
9025 arm_target_label = CODE_LABEL_NUMBER (label);
9026 else if (seeking_return || arm_ccfsm_state == 2)
9028 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
9030 this_insn = next_nonnote_insn (this_insn);
9031 if (this_insn && (GET_CODE (this_insn) == BARRIER
9032 || GET_CODE (this_insn) == CODE_LABEL))
9033 abort ();
9035 if (!this_insn)
9037 /* Oh, dear! we ran off the end.. give up */
9038 recog (PATTERN (insn), insn, NULL);
9039 arm_ccfsm_state = 0;
9040 arm_target_insn = NULL;
9041 return;
9043 arm_target_insn = this_insn;
9045 else
9046 abort ();
9047 if (jump_clobbers)
9049 if (reverse)
9050 abort ();
9051 arm_current_cc =
9052 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
9053 0), 0), 1));
9054 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
9055 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9056 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
9057 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9059 else
9061 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
9062 what it was. */
9063 if (!reverse)
9064 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
9065 0));
9068 if (reverse || then_not_else)
9069 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
9072 /* Restore recog_data (getting the attributes of other insns can
9073 destroy this array, but final.c assumes that it remains intact
9074 across this call; since the insn has been recognized already we
9075 call recog direct). */
9076 recog (PATTERN (insn), insn, NULL);
9081 arm_regno_class (regno)
9082 int regno;
9084 if (TARGET_THUMB)
9086 if (regno == STACK_POINTER_REGNUM)
9087 return STACK_REG;
9088 if (regno == CC_REGNUM)
9089 return CC_REG;
9090 if (regno < 8)
9091 return LO_REGS;
9092 return HI_REGS;
9095 if ( regno <= LAST_ARM_REGNUM
9096 || regno == FRAME_POINTER_REGNUM
9097 || regno == ARG_POINTER_REGNUM)
9098 return GENERAL_REGS;
9100 if (regno == CC_REGNUM)
9101 return NO_REGS;
9103 return FPU_REGS;
9106 /* Handle a special case when computing the offset
9107 of an argument from the frame pointer. */
9109 arm_debugger_arg_offset (value, addr)
9110 int value;
9111 rtx addr;
9113 rtx insn;
9115 /* We are only interested if dbxout_parms() failed to compute the offset. */
9116 if (value != 0)
9117 return 0;
9119 /* We can only cope with the case where the address is held in a register. */
9120 if (GET_CODE (addr) != REG)
9121 return 0;
9123 /* If we are using the frame pointer to point at the argument, then
9124 an offset of 0 is correct. */
9125 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
9126 return 0;
9128 /* If we are using the stack pointer to point at the
9129 argument, then an offset of 0 is correct. */
9130 if ((TARGET_THUMB || !frame_pointer_needed)
9131 && REGNO (addr) == SP_REGNUM)
9132 return 0;
9134 /* Oh dear. The argument is pointed to by a register rather
9135 than being held in a register, or being stored at a known
9136 offset from the frame pointer. Since GDB only understands
9137 those two kinds of argument we must translate the address
9138 held in the register into an offset from the frame pointer.
9139 We do this by searching through the insns for the function
9140 looking to see where this register gets its value. If the
9141 register is initialised from the frame pointer plus an offset
9142 then we are in luck and we can continue, otherwise we give up.
9144 This code is exercised by producing debugging information
9145 for a function with arguments like this:
9147 double func (double a, double b, int c, double d) {return d;}
9149 Without this code the stab for parameter 'd' will be set to
9150 an offset of 0 from the frame pointer, rather than 8. */
9152 /* The if() statement says:
9154 If the insn is a normal instruction
9155 and if the insn is setting the value in a register
9156 and if the register being set is the register holding the address of the argument
9157 and if the address is computing by an addition
9158 that involves adding to a register
9159 which is the frame pointer
9160 a constant integer
9162 then... */
9164 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9166 if ( GET_CODE (insn) == INSN
9167 && GET_CODE (PATTERN (insn)) == SET
9168 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
9169 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
9170 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
9171 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
9172 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
9175 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
9177 break;
9181 if (value == 0)
9183 debug_rtx (addr);
9184 warning ("unable to compute real location of stacked parameter");
9185 value = 8; /* XXX magic hack */
9188 return value;
9191 #define def_builtin(NAME, TYPE, CODE) \
9192 builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL)
9194 void
9195 arm_init_builtins ()
9197 tree endlink = void_list_node;
9198 tree int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
9199 tree pchar_type_node = build_pointer_type (char_type_node);
9201 tree int_ftype_int, void_ftype_pchar;
9203 /* void func (void *) */
9204 void_ftype_pchar
9205 = build_function_type (void_type_node,
9206 tree_cons (NULL_TREE, pchar_type_node, endlink));
9208 /* int func (int) */
9209 int_ftype_int
9210 = build_function_type (integer_type_node, int_endlink);
9212 /* Initialize arm V5 builtins. */
9213 if (arm_arch5)
9214 def_builtin ("__builtin_clz", int_ftype_int, ARM_BUILTIN_CLZ);
9217 /* Expand an expression EXP that calls a built-in function,
9218 with result going to TARGET if that's convenient
9219 (and in mode MODE if that's convenient).
9220 SUBTARGET may be used as the target for computing one of EXP's operands.
9221 IGNORE is nonzero if the value is to be ignored. */
9224 arm_expand_builtin (exp, target, subtarget, mode, ignore)
9225 tree exp;
9226 rtx target;
9227 rtx subtarget ATTRIBUTE_UNUSED;
9228 enum machine_mode mode ATTRIBUTE_UNUSED;
9229 int ignore ATTRIBUTE_UNUSED;
9231 enum insn_code icode;
9232 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
9233 tree arglist = TREE_OPERAND (exp, 1);
9234 tree arg0;
9235 rtx op0, pat;
9236 enum machine_mode tmode, mode0;
9237 int fcode = DECL_FUNCTION_CODE (fndecl);
9239 switch (fcode)
9241 default:
9242 break;
9244 case ARM_BUILTIN_CLZ:
9245 icode = CODE_FOR_clz;
9246 arg0 = TREE_VALUE (arglist);
9247 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
9248 tmode = insn_data[icode].operand[0].mode;
9249 mode0 = insn_data[icode].operand[1].mode;
9251 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9252 op0 = copy_to_mode_reg (mode0, op0);
9253 if (target == 0
9254 || GET_MODE (target) != tmode
9255 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9256 target = gen_reg_rtx (tmode);
9257 pat = GEN_FCN (icode) (target, op0);
9258 if (! pat)
9259 return 0;
9260 emit_insn (pat);
9261 return target;
9264 /* @@@ Should really do something sensible here. */
9265 return NULL_RTX;
9268 /* Recursively search through all of the blocks in a function
9269 checking to see if any of the variables created in that
9270 function match the RTX called 'orig'. If they do then
9271 replace them with the RTX called 'new'. */
9273 static void
9274 replace_symbols_in_block (block, orig, new)
9275 tree block;
9276 rtx orig;
9277 rtx new;
9279 for (; block; block = BLOCK_CHAIN (block))
9281 tree sym;
9283 if (!TREE_USED (block))
9284 continue;
9286 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
9288 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
9289 || DECL_IGNORED_P (sym)
9290 || TREE_CODE (sym) != VAR_DECL
9291 || DECL_EXTERNAL (sym)
9292 || !rtx_equal_p (DECL_RTL (sym), orig)
9294 continue;
9296 SET_DECL_RTL (sym, new);
9299 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
9303 /* Return the number (counting from 0) of the least significant set
9304 bit in MASK. */
9305 #ifdef __GNUC__
9306 inline
9307 #endif
9308 static int
9309 number_of_first_bit_set (mask)
9310 int mask;
9312 int bit;
9314 for (bit = 0;
9315 (mask & (1 << bit)) == 0;
9316 ++bit)
9317 continue;
9319 return bit;
9322 /* Generate code to return from a thumb function.
9323 If 'reg_containing_return_addr' is -1, then the return address is
9324 actually on the stack, at the stack pointer. */
9325 static void
9326 thumb_exit (f, reg_containing_return_addr, eh_ofs)
9327 FILE * f;
9328 int reg_containing_return_addr;
9329 rtx eh_ofs;
9331 unsigned regs_available_for_popping;
9332 unsigned regs_to_pop;
9333 int pops_needed;
9334 unsigned available;
9335 unsigned required;
9336 int mode;
9337 int size;
9338 int restore_a4 = FALSE;
9340 /* Compute the registers we need to pop. */
9341 regs_to_pop = 0;
9342 pops_needed = 0;
9344 /* There is an assumption here, that if eh_ofs is not NULL, the
9345 normal return address will have been pushed. */
9346 if (reg_containing_return_addr == -1 || eh_ofs)
9348 /* When we are generating a return for __builtin_eh_return,
9349 reg_containing_return_addr must specify the return regno. */
9350 if (eh_ofs && reg_containing_return_addr == -1)
9351 abort ();
9353 regs_to_pop |= 1 << LR_REGNUM;
9354 ++pops_needed;
9357 if (TARGET_BACKTRACE)
9359 /* Restore the (ARM) frame pointer and stack pointer. */
9360 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
9361 pops_needed += 2;
9364 /* If there is nothing to pop then just emit the BX instruction and
9365 return. */
9366 if (pops_needed == 0)
9368 if (eh_ofs)
9369 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9371 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9372 return;
9374 /* Otherwise if we are not supporting interworking and we have not created
9375 a backtrace structure and the function was not entered in ARM mode then
9376 just pop the return address straight into the PC. */
9377 else if (!TARGET_INTERWORK
9378 && !TARGET_BACKTRACE
9379 && !is_called_in_ARM_mode (current_function_decl))
9381 if (eh_ofs)
9383 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
9384 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9385 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9387 else
9388 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
9390 return;
9393 /* Find out how many of the (return) argument registers we can corrupt. */
9394 regs_available_for_popping = 0;
9396 /* If returning via __builtin_eh_return, the bottom three registers
9397 all contain information needed for the return. */
9398 if (eh_ofs)
9399 size = 12;
9400 else
9402 #ifdef RTX_CODE
9403 /* If we can deduce the registers used from the function's
9404 return value. This is more reliable that examining
9405 regs_ever_live[] because that will be set if the register is
9406 ever used in the function, not just if the register is used
9407 to hold a return value. */
9409 if (current_function_return_rtx != 0)
9410 mode = GET_MODE (current_function_return_rtx);
9411 else
9412 #endif
9413 mode = DECL_MODE (DECL_RESULT (current_function_decl));
9415 size = GET_MODE_SIZE (mode);
9417 if (size == 0)
9419 /* In a void function we can use any argument register.
9420 In a function that returns a structure on the stack
9421 we can use the second and third argument registers. */
9422 if (mode == VOIDmode)
9423 regs_available_for_popping =
9424 (1 << ARG_REGISTER (1))
9425 | (1 << ARG_REGISTER (2))
9426 | (1 << ARG_REGISTER (3));
9427 else
9428 regs_available_for_popping =
9429 (1 << ARG_REGISTER (2))
9430 | (1 << ARG_REGISTER (3));
9432 else if (size <= 4)
9433 regs_available_for_popping =
9434 (1 << ARG_REGISTER (2))
9435 | (1 << ARG_REGISTER (3));
9436 else if (size <= 8)
9437 regs_available_for_popping =
9438 (1 << ARG_REGISTER (3));
9441 /* Match registers to be popped with registers into which we pop them. */
9442 for (available = regs_available_for_popping,
9443 required = regs_to_pop;
9444 required != 0 && available != 0;
9445 available &= ~(available & - available),
9446 required &= ~(required & - required))
9447 -- pops_needed;
9449 /* If we have any popping registers left over, remove them. */
9450 if (available > 0)
9451 regs_available_for_popping &= ~available;
9453 /* Otherwise if we need another popping register we can use
9454 the fourth argument register. */
9455 else if (pops_needed)
9457 /* If we have not found any free argument registers and
9458 reg a4 contains the return address, we must move it. */
9459 if (regs_available_for_popping == 0
9460 && reg_containing_return_addr == LAST_ARG_REGNUM)
9462 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
9463 reg_containing_return_addr = LR_REGNUM;
9465 else if (size > 12)
9467 /* Register a4 is being used to hold part of the return value,
9468 but we have dire need of a free, low register. */
9469 restore_a4 = TRUE;
9471 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
9474 if (reg_containing_return_addr != LAST_ARG_REGNUM)
9476 /* The fourth argument register is available. */
9477 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
9479 --pops_needed;
9483 /* Pop as many registers as we can. */
9484 thumb_pushpop (f, regs_available_for_popping, FALSE);
9486 /* Process the registers we popped. */
9487 if (reg_containing_return_addr == -1)
9489 /* The return address was popped into the lowest numbered register. */
9490 regs_to_pop &= ~(1 << LR_REGNUM);
9492 reg_containing_return_addr =
9493 number_of_first_bit_set (regs_available_for_popping);
9495 /* Remove this register for the mask of available registers, so that
9496 the return address will not be corrupted by futher pops. */
9497 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
9500 /* If we popped other registers then handle them here. */
9501 if (regs_available_for_popping)
9503 int frame_pointer;
9505 /* Work out which register currently contains the frame pointer. */
9506 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
9508 /* Move it into the correct place. */
9509 asm_fprintf (f, "\tmov\t%r, %r\n",
9510 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
9512 /* (Temporarily) remove it from the mask of popped registers. */
9513 regs_available_for_popping &= ~(1 << frame_pointer);
9514 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
9516 if (regs_available_for_popping)
9518 int stack_pointer;
9520 /* We popped the stack pointer as well,
9521 find the register that contains it. */
9522 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
9524 /* Move it into the stack register. */
9525 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
9527 /* At this point we have popped all necessary registers, so
9528 do not worry about restoring regs_available_for_popping
9529 to its correct value:
9531 assert (pops_needed == 0)
9532 assert (regs_available_for_popping == (1 << frame_pointer))
9533 assert (regs_to_pop == (1 << STACK_POINTER)) */
9535 else
9537 /* Since we have just move the popped value into the frame
9538 pointer, the popping register is available for reuse, and
9539 we know that we still have the stack pointer left to pop. */
9540 regs_available_for_popping |= (1 << frame_pointer);
9544 /* If we still have registers left on the stack, but we no longer have
9545 any registers into which we can pop them, then we must move the return
9546 address into the link register and make available the register that
9547 contained it. */
9548 if (regs_available_for_popping == 0 && pops_needed > 0)
9550 regs_available_for_popping |= 1 << reg_containing_return_addr;
9552 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
9553 reg_containing_return_addr);
9555 reg_containing_return_addr = LR_REGNUM;
9558 /* If we have registers left on the stack then pop some more.
9559 We know that at most we will want to pop FP and SP. */
9560 if (pops_needed > 0)
9562 int popped_into;
9563 int move_to;
9565 thumb_pushpop (f, regs_available_for_popping, FALSE);
9567 /* We have popped either FP or SP.
9568 Move whichever one it is into the correct register. */
9569 popped_into = number_of_first_bit_set (regs_available_for_popping);
9570 move_to = number_of_first_bit_set (regs_to_pop);
9572 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
9574 regs_to_pop &= ~(1 << move_to);
9576 --pops_needed;
9579 /* If we still have not popped everything then we must have only
9580 had one register available to us and we are now popping the SP. */
9581 if (pops_needed > 0)
9583 int popped_into;
9585 thumb_pushpop (f, regs_available_for_popping, FALSE);
9587 popped_into = number_of_first_bit_set (regs_available_for_popping);
9589 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
9591 assert (regs_to_pop == (1 << STACK_POINTER))
9592 assert (pops_needed == 1)
9596 /* If necessary restore the a4 register. */
9597 if (restore_a4)
9599 if (reg_containing_return_addr != LR_REGNUM)
9601 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
9602 reg_containing_return_addr = LR_REGNUM;
9605 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
9608 if (eh_ofs)
9609 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
9611 /* Return to caller. */
9612 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
9615 /* Emit code to push or pop registers to or from the stack. */
9616 static void
9617 thumb_pushpop (f, mask, push)
9618 FILE * f;
9619 int mask;
9620 int push;
9622 int regno;
9623 int lo_mask = mask & 0xFF;
9625 if (lo_mask == 0 && !push && (mask & (1 << 15)))
9627 /* Special case. Do not generate a POP PC statement here, do it in
9628 thumb_exit() */
9629 thumb_exit (f, -1, NULL_RTX);
9630 return;
9633 fprintf (f, "\t%s\t{", push ? "push" : "pop");
9635 /* Look at the low registers first. */
9636 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
9638 if (lo_mask & 1)
9640 asm_fprintf (f, "%r", regno);
9642 if ((lo_mask & ~1) != 0)
9643 fprintf (f, ", ");
9647 if (push && (mask & (1 << LR_REGNUM)))
9649 /* Catch pushing the LR. */
9650 if (mask & 0xFF)
9651 fprintf (f, ", ");
9653 asm_fprintf (f, "%r", LR_REGNUM);
9655 else if (!push && (mask & (1 << PC_REGNUM)))
9657 /* Catch popping the PC. */
9658 if (TARGET_INTERWORK || TARGET_BACKTRACE)
9660 /* The PC is never poped directly, instead
9661 it is popped into r3 and then BX is used. */
9662 fprintf (f, "}\n");
9664 thumb_exit (f, -1, NULL_RTX);
9666 return;
9668 else
9670 if (mask & 0xFF)
9671 fprintf (f, ", ");
9673 asm_fprintf (f, "%r", PC_REGNUM);
9677 fprintf (f, "}\n");
9680 void
9681 thumb_final_prescan_insn (insn)
9682 rtx insn;
9684 if (flag_print_asm_name)
9685 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
9686 INSN_ADDRESSES (INSN_UID (insn)));
9690 thumb_shiftable_const (val)
9691 unsigned HOST_WIDE_INT val;
9693 unsigned HOST_WIDE_INT mask = 0xff;
9694 int i;
9696 if (val == 0) /* XXX */
9697 return 0;
9699 for (i = 0; i < 25; i++)
9700 if ((val & (mask << i)) == val)
9701 return 1;
9703 return 0;
9706 /* Returns non-zero if the current function contains,
9707 or might contain a far jump. */
9709 thumb_far_jump_used_p (int in_prologue)
9711 rtx insn;
9713 /* This test is only important for leaf functions. */
9714 /* assert (!leaf_function_p ()); */
9716 /* If we have already decided that far jumps may be used,
9717 do not bother checking again, and always return true even if
9718 it turns out that they are not being used. Once we have made
9719 the decision that far jumps are present (and that hence the link
9720 register will be pushed onto the stack) we cannot go back on it. */
9721 if (cfun->machine->far_jump_used)
9722 return 1;
9724 /* If this function is not being called from the prologue/epilogue
9725 generation code then it must be being called from the
9726 INITIAL_ELIMINATION_OFFSET macro. */
9727 if (!in_prologue)
9729 /* In this case we know that we are being asked about the elimination
9730 of the arg pointer register. If that register is not being used,
9731 then there are no arguments on the stack, and we do not have to
9732 worry that a far jump might force the prologue to push the link
9733 register, changing the stack offsets. In this case we can just
9734 return false, since the presence of far jumps in the function will
9735 not affect stack offsets.
9737 If the arg pointer is live (or if it was live, but has now been
9738 eliminated and so set to dead) then we do have to test to see if
9739 the function might contain a far jump. This test can lead to some
9740 false negatives, since before reload is completed, then length of
9741 branch instructions is not known, so gcc defaults to returning their
9742 longest length, which in turn sets the far jump attribute to true.
9744 A false negative will not result in bad code being generated, but it
9745 will result in a needless push and pop of the link register. We
9746 hope that this does not occur too often. */
9747 if (regs_ever_live [ARG_POINTER_REGNUM])
9748 cfun->machine->arg_pointer_live = 1;
9749 else if (!cfun->machine->arg_pointer_live)
9750 return 0;
9753 /* Check to see if the function contains a branch
9754 insn with the far jump attribute set. */
9755 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9757 if (GET_CODE (insn) == JUMP_INSN
9758 /* Ignore tablejump patterns. */
9759 && GET_CODE (PATTERN (insn)) != ADDR_VEC
9760 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
9761 && get_attr_far_jump (insn) == FAR_JUMP_YES
9764 /* Record the fact that we have decied that
9765 the function does use far jumps. */
9766 cfun->machine->far_jump_used = 1;
9767 return 1;
9771 return 0;
9774 /* Return non-zero if FUNC must be entered in ARM mode. */
9776 is_called_in_ARM_mode (func)
9777 tree func;
9779 if (TREE_CODE (func) != FUNCTION_DECL)
9780 abort ();
9782 /* Ignore the problem about functions whoes address is taken. */
9783 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
9784 return TRUE;
9786 #ifdef ARM_PE
9787 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
9788 #else
9789 return FALSE;
9790 #endif
9793 /* The bits which aren't usefully expanded as rtl. */
9795 const char *
9796 thumb_unexpanded_epilogue ()
9798 int regno;
9799 int live_regs_mask = 0;
9800 int high_regs_pushed = 0;
9801 int leaf_function = leaf_function_p ();
9802 int had_to_push_lr;
9803 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9805 if (return_used_this_function)
9806 return "";
9808 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
9809 if (regs_ever_live[regno] && !call_used_regs[regno]
9810 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9811 live_regs_mask |= 1 << regno;
9813 for (regno = 8; regno < 13; regno++)
9815 if (regs_ever_live[regno] && !call_used_regs[regno]
9816 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9817 high_regs_pushed++;
9820 /* The prolog may have pushed some high registers to use as
9821 work registers. eg the testuite file:
9822 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
9823 compiles to produce:
9824 push {r4, r5, r6, r7, lr}
9825 mov r7, r9
9826 mov r6, r8
9827 push {r6, r7}
9828 as part of the prolog. We have to undo that pushing here. */
9830 if (high_regs_pushed)
9832 int mask = live_regs_mask;
9833 int next_hi_reg;
9834 int size;
9835 int mode;
9837 #ifdef RTX_CODE
9838 /* If we can deduce the registers used from the function's return value.
9839 This is more reliable that examining regs_ever_live[] because that
9840 will be set if the register is ever used in the function, not just if
9841 the register is used to hold a return value. */
9843 if (current_function_return_rtx != 0)
9844 mode = GET_MODE (current_function_return_rtx);
9845 else
9846 #endif
9847 mode = DECL_MODE (DECL_RESULT (current_function_decl));
9849 size = GET_MODE_SIZE (mode);
9851 /* Unless we are returning a type of size > 12 register r3 is
9852 available. */
9853 if (size < 13)
9854 mask |= 1 << 3;
9856 if (mask == 0)
9857 /* Oh dear! We have no low registers into which we can pop
9858 high registers! */
9859 internal_error
9860 ("no low registers available for popping high registers");
9862 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
9863 if (regs_ever_live[next_hi_reg] && !call_used_regs[next_hi_reg]
9864 && !(TARGET_SINGLE_PIC_BASE && (next_hi_reg == arm_pic_register)))
9865 break;
9867 while (high_regs_pushed)
9869 /* Find lo register(s) into which the high register(s) can
9870 be popped. */
9871 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
9873 if (mask & (1 << regno))
9874 high_regs_pushed--;
9875 if (high_regs_pushed == 0)
9876 break;
9879 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
9881 /* Pop the values into the low register(s). */
9882 thumb_pushpop (asm_out_file, mask, 0);
9884 /* Move the value(s) into the high registers. */
9885 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
9887 if (mask & (1 << regno))
9889 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
9890 regno);
9892 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
9893 if (regs_ever_live[next_hi_reg]
9894 && !call_used_regs[next_hi_reg]
9895 && !(TARGET_SINGLE_PIC_BASE
9896 && (next_hi_reg == arm_pic_register)))
9897 break;
9903 had_to_push_lr = (live_regs_mask || !leaf_function
9904 || thumb_far_jump_used_p (1));
9906 if (TARGET_BACKTRACE
9907 && ((live_regs_mask & 0xFF) == 0)
9908 && regs_ever_live [LAST_ARG_REGNUM] != 0)
9910 /* The stack backtrace structure creation code had to
9911 push R7 in order to get a work register, so we pop
9912 it now. */
9913 live_regs_mask |= (1 << LAST_LO_REGNUM);
9916 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
9918 if (had_to_push_lr
9919 && !is_called_in_ARM_mode (current_function_decl)
9920 && !eh_ofs)
9921 live_regs_mask |= 1 << PC_REGNUM;
9923 /* Either no argument registers were pushed or a backtrace
9924 structure was created which includes an adjusted stack
9925 pointer, so just pop everything. */
9926 if (live_regs_mask)
9927 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
9929 if (eh_ofs)
9930 thumb_exit (asm_out_file, 2, eh_ofs);
9931 /* We have either just popped the return address into the
9932 PC or it is was kept in LR for the entire function or
9933 it is still on the stack because we do not want to
9934 return by doing a pop {pc}. */
9935 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
9936 thumb_exit (asm_out_file,
9937 (had_to_push_lr
9938 && is_called_in_ARM_mode (current_function_decl)) ?
9939 -1 : LR_REGNUM, NULL_RTX);
9941 else
9943 /* Pop everything but the return address. */
9944 live_regs_mask &= ~(1 << PC_REGNUM);
9946 if (live_regs_mask)
9947 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
9949 if (had_to_push_lr)
9950 /* Get the return address into a temporary register. */
9951 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0);
9953 /* Remove the argument registers that were pushed onto the stack. */
9954 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
9955 SP_REGNUM, SP_REGNUM,
9956 current_function_pretend_args_size);
9958 if (eh_ofs)
9959 thumb_exit (asm_out_file, 2, eh_ofs);
9960 else
9961 thumb_exit (asm_out_file,
9962 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
9965 return "";
9968 /* Functions to save and restore machine-specific function data. */
9970 static void
9971 arm_mark_machine_status (p)
9972 struct function * p;
9974 machine_function *machine = p->machine;
9976 if (machine)
9977 ggc_mark_rtx (machine->eh_epilogue_sp_ofs);
9980 static void
9981 arm_init_machine_status (p)
9982 struct function * p;
9984 p->machine =
9985 (machine_function *) xcalloc (1, sizeof (machine_function));
9987 #if ARM_FT_UNKNOWWN != 0
9988 ((machine_function *) p->machine)->func_type = ARM_FT_UNKNOWN;
9989 #endif
9992 static void
9993 arm_free_machine_status (p)
9994 struct function * p;
9996 if (p->machine)
9998 free (p->machine);
9999 p->machine = NULL;
10003 /* Return an RTX indicating where the return address to the
10004 calling function can be found. */
10006 arm_return_addr (count, frame)
10007 int count;
10008 rtx frame ATTRIBUTE_UNUSED;
10010 if (count != 0)
10011 return NULL_RTX;
10013 if (TARGET_APCS_32)
10014 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
10015 else
10017 rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
10018 GEN_INT (RETURN_ADDR_MASK26));
10019 return get_func_hard_reg_initial_val (cfun, lr);
10023 /* Do anything needed before RTL is emitted for each function. */
10024 void
10025 arm_init_expanders ()
10027 /* Arrange to initialize and mark the machine per-function status. */
10028 init_machine_status = arm_init_machine_status;
10029 mark_machine_status = arm_mark_machine_status;
10030 free_machine_status = arm_free_machine_status;
10033 /* Generate the rest of a function's prologue. */
10034 void
10035 thumb_expand_prologue ()
10037 HOST_WIDE_INT amount = (get_frame_size ()
10038 + current_function_outgoing_args_size);
10039 unsigned long func_type;
10041 func_type = arm_current_func_type ();
10043 /* Naked functions don't have prologues. */
10044 if (IS_NAKED (func_type))
10045 return;
10047 if (IS_INTERRUPT (func_type))
10049 error ("interrupt Service Routines cannot be coded in Thumb mode");
10050 return;
10053 if (frame_pointer_needed)
10054 emit_insn (gen_movsi (hard_frame_pointer_rtx, stack_pointer_rtx));
10056 if (amount)
10058 amount = ROUND_UP (amount);
10060 if (amount < 512)
10061 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10062 GEN_INT (-amount)));
10063 else
10065 int regno;
10066 rtx reg;
10068 /* The stack decrement is too big for an immediate value in a single
10069 insn. In theory we could issue multiple subtracts, but after
10070 three of them it becomes more space efficient to place the full
10071 value in the constant pool and load into a register. (Also the
10072 ARM debugger really likes to see only one stack decrement per
10073 function). So instead we look for a scratch register into which
10074 we can load the decrement, and then we subtract this from the
10075 stack pointer. Unfortunately on the thumb the only available
10076 scratch registers are the argument registers, and we cannot use
10077 these as they may hold arguments to the function. Instead we
10078 attempt to locate a call preserved register which is used by this
10079 function. If we can find one, then we know that it will have
10080 been pushed at the start of the prologue and so we can corrupt
10081 it now. */
10082 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
10083 if (regs_ever_live[regno]
10084 && !call_used_regs[regno] /* Paranoia */
10085 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register))
10086 && !(frame_pointer_needed
10087 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
10088 break;
10090 if (regno > LAST_LO_REGNUM) /* Very unlikely */
10092 rtx spare = gen_rtx (REG, SImode, IP_REGNUM);
10094 /* Choose an arbitary, non-argument low register. */
10095 reg = gen_rtx (REG, SImode, LAST_LO_REGNUM);
10097 /* Save it by copying it into a high, scratch register. */
10098 emit_insn (gen_movsi (spare, reg));
10100 /* Decrement the stack. */
10101 emit_insn (gen_movsi (reg, GEN_INT (-amount)));
10102 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10103 reg));
10105 /* Restore the low register's original value. */
10106 emit_insn (gen_movsi (reg, spare));
10108 /* Emit a USE of the restored scratch register, so that flow
10109 analysis will not consider the restore redundant. The
10110 register won't be used again in this function and isn't
10111 restored by the epilogue. */
10112 emit_insn (gen_rtx_USE (VOIDmode, reg));
10114 else
10116 reg = gen_rtx (REG, SImode, regno);
10118 emit_insn (gen_movsi (reg, GEN_INT (-amount)));
10119 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10120 reg));
10125 if (profile_flag || TARGET_NO_SCHED_PRO)
10126 emit_insn (gen_blockage ());
10129 void
10130 thumb_expand_epilogue ()
10132 HOST_WIDE_INT amount = (get_frame_size ()
10133 + current_function_outgoing_args_size);
10135 /* Naked functions don't have prologues. */
10136 if (IS_NAKED (arm_current_func_type ()))
10137 return;
10139 if (frame_pointer_needed)
10140 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
10141 else if (amount)
10143 amount = ROUND_UP (amount);
10145 if (amount < 512)
10146 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10147 GEN_INT (amount)));
10148 else
10150 /* r3 is always free in the epilogue. */
10151 rtx reg = gen_rtx (REG, SImode, LAST_ARG_REGNUM);
10153 emit_insn (gen_movsi (reg, GEN_INT (amount)));
10154 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
10158 /* Emit a USE (stack_pointer_rtx), so that
10159 the stack adjustment will not be deleted. */
10160 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
10162 if (profile_flag || TARGET_NO_SCHED_PRO)
10163 emit_insn (gen_blockage ());
10166 static void
10167 thumb_output_function_prologue (f, size)
10168 FILE * f;
10169 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
10171 int live_regs_mask = 0;
10172 int high_regs_pushed = 0;
10173 int regno;
10175 if (IS_NAKED (arm_current_func_type ()))
10176 return;
10178 if (is_called_in_ARM_mode (current_function_decl))
10180 const char * name;
10182 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
10183 abort ();
10184 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
10185 abort ();
10186 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10188 /* Generate code sequence to switch us into Thumb mode. */
10189 /* The .code 32 directive has already been emitted by
10190 ASM_DECLARE_FUNCTION_NAME. */
10191 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
10192 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
10194 /* Generate a label, so that the debugger will notice the
10195 change in instruction sets. This label is also used by
10196 the assembler to bypass the ARM code when this function
10197 is called from a Thumb encoded function elsewhere in the
10198 same file. Hence the definition of STUB_NAME here must
10199 agree with the definition in gas/config/tc-arm.c */
10201 #define STUB_NAME ".real_start_of"
10203 asm_fprintf (f, "\t.code\t16\n");
10204 #ifdef ARM_PE
10205 if (arm_dllexport_name_p (name))
10206 name = arm_strip_name_encoding (name);
10207 #endif
10208 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
10209 asm_fprintf (f, "\t.thumb_func\n");
10210 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
10213 if (current_function_pretend_args_size)
10215 if (current_function_anonymous_args)
10217 int num_pushes;
10219 asm_fprintf (f, "\tpush\t{");
10221 num_pushes = NUM_INTS (current_function_pretend_args_size);
10223 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
10224 regno <= LAST_ARG_REGNUM;
10225 regno++)
10226 asm_fprintf (f, "%r%s", regno,
10227 regno == LAST_ARG_REGNUM ? "" : ", ");
10229 asm_fprintf (f, "}\n");
10231 else
10232 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
10233 SP_REGNUM, SP_REGNUM,
10234 current_function_pretend_args_size);
10237 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10238 if (regs_ever_live[regno] && !call_used_regs[regno]
10239 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
10240 live_regs_mask |= 1 << regno;
10242 if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p (1))
10243 live_regs_mask |= 1 << LR_REGNUM;
10245 if (TARGET_BACKTRACE)
10247 int offset;
10248 int work_register = 0;
10249 int wr;
10251 /* We have been asked to create a stack backtrace structure.
10252 The code looks like this:
10254 0 .align 2
10255 0 func:
10256 0 sub SP, #16 Reserve space for 4 registers.
10257 2 push {R7} Get a work register.
10258 4 add R7, SP, #20 Get the stack pointer before the push.
10259 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
10260 8 mov R7, PC Get hold of the start of this code plus 12.
10261 10 str R7, [SP, #16] Store it.
10262 12 mov R7, FP Get hold of the current frame pointer.
10263 14 str R7, [SP, #4] Store it.
10264 16 mov R7, LR Get hold of the current return address.
10265 18 str R7, [SP, #12] Store it.
10266 20 add R7, SP, #16 Point at the start of the backtrace structure.
10267 22 mov FP, R7 Put this value into the frame pointer. */
10269 if ((live_regs_mask & 0xFF) == 0)
10271 /* See if the a4 register is free. */
10273 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
10274 work_register = LAST_ARG_REGNUM;
10275 else /* We must push a register of our own */
10276 live_regs_mask |= (1 << LAST_LO_REGNUM);
10279 if (work_register == 0)
10281 /* Select a register from the list that will be pushed to
10282 use as our work register. */
10283 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
10284 if ((1 << work_register) & live_regs_mask)
10285 break;
10288 asm_fprintf
10289 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
10290 SP_REGNUM, SP_REGNUM);
10292 if (live_regs_mask)
10293 thumb_pushpop (f, live_regs_mask, 1);
10295 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
10296 if (wr & live_regs_mask)
10297 offset += 4;
10299 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
10300 offset + 16 + current_function_pretend_args_size);
10302 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10303 offset + 4);
10305 /* Make sure that the instruction fetching the PC is in the right place
10306 to calculate "start of backtrace creation code + 12". */
10307 if (live_regs_mask)
10309 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
10310 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10311 offset + 12);
10312 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
10313 ARM_HARD_FRAME_POINTER_REGNUM);
10314 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10315 offset);
10317 else
10319 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
10320 ARM_HARD_FRAME_POINTER_REGNUM);
10321 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10322 offset);
10323 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
10324 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10325 offset + 12);
10328 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
10329 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
10330 offset + 8);
10331 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
10332 offset + 12);
10333 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
10334 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
10336 else if (live_regs_mask)
10337 thumb_pushpop (f, live_regs_mask, 1);
10339 for (regno = 8; regno < 13; regno++)
10341 if (regs_ever_live[regno] && !call_used_regs[regno]
10342 && !(TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
10343 high_regs_pushed++;
10346 if (high_regs_pushed)
10348 int pushable_regs = 0;
10349 int mask = live_regs_mask & 0xff;
10350 int next_hi_reg;
10352 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
10354 if (regs_ever_live[next_hi_reg] && !call_used_regs[next_hi_reg]
10355 && !(TARGET_SINGLE_PIC_BASE
10356 && (next_hi_reg == arm_pic_register)))
10357 break;
10360 pushable_regs = mask;
10362 if (pushable_regs == 0)
10364 /* Desperation time -- this probably will never happen. */
10365 if (regs_ever_live[LAST_ARG_REGNUM]
10366 || !call_used_regs[LAST_ARG_REGNUM])
10367 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
10368 mask = 1 << LAST_ARG_REGNUM;
10371 while (high_regs_pushed > 0)
10373 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
10375 if (mask & (1 << regno))
10377 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
10379 high_regs_pushed--;
10381 if (high_regs_pushed)
10382 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
10383 next_hi_reg--)
10385 if (regs_ever_live[next_hi_reg]
10386 && !call_used_regs[next_hi_reg]
10387 && !(TARGET_SINGLE_PIC_BASE
10388 && (next_hi_reg == arm_pic_register)))
10389 break;
10391 else
10393 mask &= ~((1 << regno) - 1);
10394 break;
10399 thumb_pushpop (f, mask, 1);
10402 if (pushable_regs == 0
10403 && (regs_ever_live[LAST_ARG_REGNUM]
10404 || !call_used_regs[LAST_ARG_REGNUM]))
10405 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
10409 /* Handle the case of a double word load into a low register from
10410 a computed memory address. The computed address may involve a
10411 register which is overwritten by the load. */
10413 const char *
10414 thumb_load_double_from_address (operands)
10415 rtx *operands;
10417 rtx addr;
10418 rtx base;
10419 rtx offset;
10420 rtx arg1;
10421 rtx arg2;
10423 if (GET_CODE (operands[0]) != REG)
10424 abort ();
10426 if (GET_CODE (operands[1]) != MEM)
10427 abort ();
10429 /* Get the memory address. */
10430 addr = XEXP (operands[1], 0);
10432 /* Work out how the memory address is computed. */
10433 switch (GET_CODE (addr))
10435 case REG:
10436 operands[2] = gen_rtx (MEM, SImode,
10437 plus_constant (XEXP (operands[1], 0), 4));
10439 if (REGNO (operands[0]) == REGNO (addr))
10441 output_asm_insn ("ldr\t%H0, %2", operands);
10442 output_asm_insn ("ldr\t%0, %1", operands);
10444 else
10446 output_asm_insn ("ldr\t%0, %1", operands);
10447 output_asm_insn ("ldr\t%H0, %2", operands);
10449 break;
10451 case CONST:
10452 /* Compute <address> + 4 for the high order load. */
10453 operands[2] = gen_rtx (MEM, SImode,
10454 plus_constant (XEXP (operands[1], 0), 4));
10456 output_asm_insn ("ldr\t%0, %1", operands);
10457 output_asm_insn ("ldr\t%H0, %2", operands);
10458 break;
10460 case PLUS:
10461 arg1 = XEXP (addr, 0);
10462 arg2 = XEXP (addr, 1);
10464 if (CONSTANT_P (arg1))
10465 base = arg2, offset = arg1;
10466 else
10467 base = arg1, offset = arg2;
10469 if (GET_CODE (base) != REG)
10470 abort ();
10472 /* Catch the case of <address> = <reg> + <reg> */
10473 if (GET_CODE (offset) == REG)
10475 int reg_offset = REGNO (offset);
10476 int reg_base = REGNO (base);
10477 int reg_dest = REGNO (operands[0]);
10479 /* Add the base and offset registers together into the
10480 higher destination register. */
10481 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
10482 reg_dest + 1, reg_base, reg_offset);
10484 /* Load the lower destination register from the address in
10485 the higher destination register. */
10486 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
10487 reg_dest, reg_dest + 1);
10489 /* Load the higher destination register from its own address
10490 plus 4. */
10491 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
10492 reg_dest + 1, reg_dest + 1);
10494 else
10496 /* Compute <address> + 4 for the high order load. */
10497 operands[2] = gen_rtx (MEM, SImode,
10498 plus_constant (XEXP (operands[1], 0), 4));
10500 /* If the computed address is held in the low order register
10501 then load the high order register first, otherwise always
10502 load the low order register first. */
10503 if (REGNO (operands[0]) == REGNO (base))
10505 output_asm_insn ("ldr\t%H0, %2", operands);
10506 output_asm_insn ("ldr\t%0, %1", operands);
10508 else
10510 output_asm_insn ("ldr\t%0, %1", operands);
10511 output_asm_insn ("ldr\t%H0, %2", operands);
10514 break;
10516 case LABEL_REF:
10517 /* With no registers to worry about we can just load the value
10518 directly. */
10519 operands[2] = gen_rtx (MEM, SImode,
10520 plus_constant (XEXP (operands[1], 0), 4));
10522 output_asm_insn ("ldr\t%H0, %2", operands);
10523 output_asm_insn ("ldr\t%0, %1", operands);
10524 break;
10526 default:
10527 abort ();
10528 break;
10531 return "";
10535 const char *
10536 thumb_output_move_mem_multiple (n, operands)
10537 int n;
10538 rtx * operands;
10540 rtx tmp;
10542 switch (n)
10544 case 2:
10545 if (REGNO (operands[4]) > REGNO (operands[5]))
10547 tmp = operands[4];
10548 operands[4] = operands[5];
10549 operands[5] = tmp;
10551 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
10552 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
10553 break;
10555 case 3:
10556 if (REGNO (operands[4]) > REGNO (operands[5]))
10558 tmp = operands[4];
10559 operands[4] = operands[5];
10560 operands[5] = tmp;
10562 if (REGNO (operands[5]) > REGNO (operands[6]))
10564 tmp = operands[5];
10565 operands[5] = operands[6];
10566 operands[6] = tmp;
10568 if (REGNO (operands[4]) > REGNO (operands[5]))
10570 tmp = operands[4];
10571 operands[4] = operands[5];
10572 operands[5] = tmp;
10575 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
10576 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
10577 break;
10579 default:
10580 abort ();
10583 return "";
10586 /* Routines for generating rtl */
10588 void
10589 thumb_expand_movstrqi (operands)
10590 rtx * operands;
10592 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
10593 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
10594 HOST_WIDE_INT len = INTVAL (operands[2]);
10595 HOST_WIDE_INT offset = 0;
10597 while (len >= 12)
10599 emit_insn (gen_movmem12b (out, in, out, in));
10600 len -= 12;
10603 if (len >= 8)
10605 emit_insn (gen_movmem8b (out, in, out, in));
10606 len -= 8;
10609 if (len >= 4)
10611 rtx reg = gen_reg_rtx (SImode);
10612 emit_insn (gen_movsi (reg, gen_rtx (MEM, SImode, in)));
10613 emit_insn (gen_movsi (gen_rtx (MEM, SImode, out), reg));
10614 len -= 4;
10615 offset += 4;
10618 if (len >= 2)
10620 rtx reg = gen_reg_rtx (HImode);
10621 emit_insn (gen_movhi (reg, gen_rtx (MEM, HImode,
10622 plus_constant (in, offset))));
10623 emit_insn (gen_movhi (gen_rtx (MEM, HImode, plus_constant (out, offset)),
10624 reg));
10625 len -= 2;
10626 offset += 2;
10629 if (len)
10631 rtx reg = gen_reg_rtx (QImode);
10632 emit_insn (gen_movqi (reg, gen_rtx (MEM, QImode,
10633 plus_constant (in, offset))));
10634 emit_insn (gen_movqi (gen_rtx (MEM, QImode, plus_constant (out, offset)),
10635 reg));
10640 thumb_cmp_operand (op, mode)
10641 rtx op;
10642 enum machine_mode mode;
10644 return ((GET_CODE (op) == CONST_INT
10645 && (unsigned HOST_WIDE_INT) (INTVAL (op)) < 256)
10646 || register_operand (op, mode));
10649 static const char *
10650 thumb_condition_code (x, invert)
10651 rtx x;
10652 int invert;
10654 static const char *const conds[] =
10656 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
10657 "hi", "ls", "ge", "lt", "gt", "le"
10659 int val;
10661 switch (GET_CODE (x))
10663 case EQ: val = 0; break;
10664 case NE: val = 1; break;
10665 case GEU: val = 2; break;
10666 case LTU: val = 3; break;
10667 case GTU: val = 8; break;
10668 case LEU: val = 9; break;
10669 case GE: val = 10; break;
10670 case LT: val = 11; break;
10671 case GT: val = 12; break;
10672 case LE: val = 13; break;
10673 default:
10674 abort ();
10677 return conds[val ^ invert];
10680 /* Handle storing a half-word to memory during reload. */
10681 void
10682 thumb_reload_out_hi (operands)
10683 rtx * operands;
10685 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
10688 /* Handle storing a half-word to memory during reload. */
10689 void
10690 thumb_reload_in_hi (operands)
10691 rtx * operands ATTRIBUTE_UNUSED;
10693 abort ();
10696 /* Return the length of a function name prefix
10697 that starts with the character 'c'. */
10698 static int
10699 arm_get_strip_length (char c)
10701 switch (c)
10703 ARM_NAME_ENCODING_LENGTHS
10704 default: return 0;
10708 /* Return a pointer to a function's name with any
10709 and all prefix encodings stripped from it. */
10710 const char *
10711 arm_strip_name_encoding (const char * name)
10713 int skip;
10715 while ((skip = arm_get_strip_length (* name)))
10716 name += skip;
10718 return name;
10721 #ifdef AOF_ASSEMBLER
10722 /* Special functions only needed when producing AOF syntax assembler. */
10724 rtx aof_pic_label = NULL_RTX;
10725 struct pic_chain
10727 struct pic_chain * next;
10728 const char * symname;
10731 static struct pic_chain * aof_pic_chain = NULL;
10734 aof_pic_entry (x)
10735 rtx x;
10737 struct pic_chain ** chainp;
10738 int offset;
10740 if (aof_pic_label == NULL_RTX)
10742 /* We mark this here and not in arm_add_gc_roots() to avoid
10743 polluting even more code with ifdefs, and because it never
10744 contains anything useful until we assign to it here. */
10745 ggc_add_rtx_root (&aof_pic_label, 1);
10746 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
10749 for (offset = 0, chainp = &aof_pic_chain; *chainp;
10750 offset += 4, chainp = &(*chainp)->next)
10751 if ((*chainp)->symname == XSTR (x, 0))
10752 return plus_constant (aof_pic_label, offset);
10754 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
10755 (*chainp)->next = NULL;
10756 (*chainp)->symname = XSTR (x, 0);
10757 return plus_constant (aof_pic_label, offset);
10760 void
10761 aof_dump_pic_table (f)
10762 FILE * f;
10764 struct pic_chain * chain;
10766 if (aof_pic_chain == NULL)
10767 return;
10769 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
10770 PIC_OFFSET_TABLE_REGNUM,
10771 PIC_OFFSET_TABLE_REGNUM);
10772 fputs ("|x$adcons|\n", f);
10774 for (chain = aof_pic_chain; chain; chain = chain->next)
10776 fputs ("\tDCD\t", f);
10777 assemble_name (f, chain->symname);
10778 fputs ("\n", f);
10782 int arm_text_section_count = 1;
10784 char *
10785 aof_text_section ()
10787 static char buf[100];
10788 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
10789 arm_text_section_count++);
10790 if (flag_pic)
10791 strcat (buf, ", PIC, REENTRANT");
10792 return buf;
10795 static int arm_data_section_count = 1;
10797 char *
10798 aof_data_section ()
10800 static char buf[100];
10801 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
10802 return buf;
10805 /* The AOF assembler is religiously strict about declarations of
10806 imported and exported symbols, so that it is impossible to declare
10807 a function as imported near the beginning of the file, and then to
10808 export it later on. It is, however, possible to delay the decision
10809 until all the functions in the file have been compiled. To get
10810 around this, we maintain a list of the imports and exports, and
10811 delete from it any that are subsequently defined. At the end of
10812 compilation we spit the remainder of the list out before the END
10813 directive. */
10815 struct import
10817 struct import * next;
10818 const char * name;
10821 static struct import * imports_list = NULL;
10823 void
10824 aof_add_import (name)
10825 const char * name;
10827 struct import * new;
10829 for (new = imports_list; new; new = new->next)
10830 if (new->name == name)
10831 return;
10833 new = (struct import *) xmalloc (sizeof (struct import));
10834 new->next = imports_list;
10835 imports_list = new;
10836 new->name = name;
10839 void
10840 aof_delete_import (name)
10841 const char * name;
10843 struct import ** old;
10845 for (old = &imports_list; *old; old = & (*old)->next)
10847 if ((*old)->name == name)
10849 *old = (*old)->next;
10850 return;
10855 int arm_main_function = 0;
10857 void
10858 aof_dump_imports (f)
10859 FILE * f;
10861 /* The AOF assembler needs this to cause the startup code to be extracted
10862 from the library. Brining in __main causes the whole thing to work
10863 automagically. */
10864 if (arm_main_function)
10866 text_section ();
10867 fputs ("\tIMPORT __main\n", f);
10868 fputs ("\tDCD __main\n", f);
10871 /* Now dump the remaining imports. */
10872 while (imports_list)
10874 fprintf (f, "\tIMPORT\t");
10875 assemble_name (f, imports_list->name);
10876 fputc ('\n', f);
10877 imports_list = imports_list->next;
10880 #endif /* AOF_ASSEMBLER */
10882 #ifdef OBJECT_FORMAT_ELF
10883 /* Switch to an arbitrary section NAME with attributes as specified
10884 by FLAGS. ALIGN specifies any known alignment requirements for
10885 the section; 0 if the default should be used.
10887 Differs from the default elf version only in the prefix character
10888 used before the section type. */
10890 static void
10891 arm_elf_asm_named_section (name, flags)
10892 const char *name;
10893 unsigned int flags;
10895 char flagchars[8], *f = flagchars;
10896 const char *type;
10898 if (!(flags & SECTION_DEBUG))
10899 *f++ = 'a';
10900 if (flags & SECTION_WRITE)
10901 *f++ = 'w';
10902 if (flags & SECTION_CODE)
10903 *f++ = 'x';
10904 if (flags & SECTION_SMALL)
10905 *f++ = 's';
10906 if (flags & SECTION_MERGE)
10907 *f++ = 'M';
10908 if (flags & SECTION_STRINGS)
10909 *f++ = 'S';
10910 *f = '\0';
10912 if (flags & SECTION_BSS)
10913 type = "nobits";
10914 else
10915 type = "progbits";
10917 if (flags & SECTION_ENTSIZE)
10918 fprintf (asm_out_file, "\t.section\t%s,\"%s\",%%%s,%d\n",
10919 name, flagchars, type, flags & SECTION_ENTSIZE);
10920 else
10921 fprintf (asm_out_file, "\t.section\t%s,\"%s\",%%%s\n",
10922 name, flagchars, type);
10924 #endif