OSDN Git Service

* arm.c (arm_return_in_memory): Add handling for vector return types.
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005  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).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84                                            int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101                                rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125                                  tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145                                   tree, bool);
146
147 #ifdef OBJECT_FORMAT_ELF
148 static void arm_elf_asm_constructor (rtx, int);
149 #endif
150 #ifndef ARM_PE
151 static void arm_encode_section_info (tree, rtx, int);
152 #endif
153
154 static void arm_file_end (void);
155
156 #ifdef AOF_ASSEMBLER
157 static void aof_globalize_label (FILE *, const char *);
158 static void aof_dump_imports (FILE *);
159 static void aof_dump_pic_table (FILE *);
160 static void aof_file_start (void);
161 static void aof_file_end (void);
162 #endif
163 static rtx arm_struct_value_rtx (tree, int);
164 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
165                                         tree, int *, int);
166 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
167                                    enum machine_mode, tree, bool);
168 static bool arm_promote_prototypes (tree);
169 static bool arm_default_short_enums (void);
170 static bool arm_align_anon_bitfield (void);
171 static bool arm_return_in_msb (tree);
172 static bool arm_must_pass_in_stack (enum machine_mode, tree);
173
174 static tree arm_cxx_guard_type (void);
175 static bool arm_cxx_guard_mask_bit (void);
176 static tree arm_get_cookie_size (tree);
177 static bool arm_cookie_has_size (void);
178 static bool arm_cxx_cdtor_returns_this (void);
179 static bool arm_cxx_key_method_may_be_inline (void);
180 static void arm_cxx_determine_class_data_visibility (tree);
181 static bool arm_cxx_class_data_always_comdat (void);
182 static bool arm_cxx_use_aeabi_atexit (void);
183 static void arm_init_libfuncs (void);
184 static bool arm_handle_option (size_t, const char *, int);
185 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
186 \f
187 /* Initialize the GCC target structure.  */
188 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
189 #undef  TARGET_MERGE_DECL_ATTRIBUTES
190 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
191 #endif
192
193 #undef  TARGET_ATTRIBUTE_TABLE
194 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
195
196 #undef TARGET_ASM_FILE_END
197 #define TARGET_ASM_FILE_END arm_file_end
198
199 #ifdef AOF_ASSEMBLER
200 #undef  TARGET_ASM_BYTE_OP
201 #define TARGET_ASM_BYTE_OP "\tDCB\t"
202 #undef  TARGET_ASM_ALIGNED_HI_OP
203 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
204 #undef  TARGET_ASM_ALIGNED_SI_OP
205 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
206 #undef TARGET_ASM_GLOBALIZE_LABEL
207 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
208 #undef TARGET_ASM_FILE_START
209 #define TARGET_ASM_FILE_START aof_file_start
210 #undef TARGET_ASM_FILE_END
211 #define TARGET_ASM_FILE_END aof_file_end
212 #else
213 #undef  TARGET_ASM_ALIGNED_SI_OP
214 #define TARGET_ASM_ALIGNED_SI_OP NULL
215 #undef  TARGET_ASM_INTEGER
216 #define TARGET_ASM_INTEGER arm_assemble_integer
217 #endif
218
219 #undef  TARGET_ASM_FUNCTION_PROLOGUE
220 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
221
222 #undef  TARGET_ASM_FUNCTION_EPILOGUE
223 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
224
225 #undef  TARGET_DEFAULT_TARGET_FLAGS
226 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
227 #undef  TARGET_HANDLE_OPTION
228 #define TARGET_HANDLE_OPTION arm_handle_option
229
230 #undef  TARGET_COMP_TYPE_ATTRIBUTES
231 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
232
233 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
234 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
235
236 #undef  TARGET_SCHED_ADJUST_COST
237 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
238
239 #undef TARGET_ENCODE_SECTION_INFO
240 #ifdef ARM_PE
241 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
242 #else
243 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
244 #endif
245
246 #undef  TARGET_STRIP_NAME_ENCODING
247 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
248
249 #undef  TARGET_ASM_INTERNAL_LABEL
250 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
251
252 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
253 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
254
255 #undef  TARGET_ASM_OUTPUT_MI_THUNK
256 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
257 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
258 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
259
260 /* This will be overridden in arm_override_options.  */
261 #undef  TARGET_RTX_COSTS
262 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
263 #undef  TARGET_ADDRESS_COST
264 #define TARGET_ADDRESS_COST arm_address_cost
265
266 #undef TARGET_SHIFT_TRUNCATION_MASK
267 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
268 #undef TARGET_VECTOR_MODE_SUPPORTED_P
269 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
270
271 #undef  TARGET_MACHINE_DEPENDENT_REORG
272 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
273
274 #undef  TARGET_INIT_BUILTINS
275 #define TARGET_INIT_BUILTINS  arm_init_builtins
276 #undef  TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
278
279 #undef TARGET_INIT_LIBFUNCS
280 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
281
282 #undef TARGET_PROMOTE_FUNCTION_ARGS
283 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
288 #undef TARGET_PASS_BY_REFERENCE
289 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
290 #undef TARGET_ARG_PARTIAL_BYTES
291 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
292
293 #undef TARGET_STRUCT_VALUE_RTX
294 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
295
296 #undef  TARGET_SETUP_INCOMING_VARARGS
297 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
298
299 #undef TARGET_DEFAULT_SHORT_ENUMS
300 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
301
302 #undef TARGET_ALIGN_ANON_BITFIELD
303 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
304
305 #undef TARGET_CXX_GUARD_TYPE
306 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
307
308 #undef TARGET_CXX_GUARD_MASK_BIT
309 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
310
311 #undef TARGET_CXX_GET_COOKIE_SIZE
312 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
313
314 #undef TARGET_CXX_COOKIE_HAS_SIZE
315 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
316
317 #undef TARGET_CXX_CDTOR_RETURNS_THIS
318 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
319
320 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
321 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
322
323 #undef TARGET_CXX_USE_AEABI_ATEXIT
324 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
325
326 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
327 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
328   arm_cxx_determine_class_data_visibility
329
330 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
331 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
332
333 #undef TARGET_RETURN_IN_MSB
334 #define TARGET_RETURN_IN_MSB arm_return_in_msb
335
336 #undef TARGET_MUST_PASS_IN_STACK
337 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
338
339 struct gcc_target targetm = TARGET_INITIALIZER;
340 \f
341 /* Obstack for minipool constant handling.  */
342 static struct obstack minipool_obstack;
343 static char *         minipool_startobj;
344
345 /* The maximum number of insns skipped which
346    will be conditionalised if possible.  */
347 static int max_insns_skipped = 5;
348
349 extern FILE * asm_out_file;
350
351 /* True if we are currently building a constant table.  */
352 int making_const_table;
353
354 /* Define the information needed to generate branch insns.  This is
355    stored from the compare operation.  */
356 rtx arm_compare_op0, arm_compare_op1;
357
358 /* The processor for which instructions should be scheduled.  */
359 enum processor_type arm_tune = arm_none;
360
361 /* Which floating point model to use.  */
362 enum arm_fp_model arm_fp_model;
363
364 /* Which floating point hardware is available.  */
365 enum fputype arm_fpu_arch;
366
367 /* Which floating point hardware to schedule for.  */
368 enum fputype arm_fpu_tune;
369
370 /* Whether to use floating point hardware.  */
371 enum float_abi_type arm_float_abi;
372
373 /* Which ABI to use.  */
374 enum arm_abi_type arm_abi;
375
376 /* Set by the -mfpu=... option.  */
377 static const char * target_fpu_name = NULL;
378
379 /* Set by the -mfpe=... option.  */
380 static const char * target_fpe_name = NULL;
381
382 /* Set by the -mfloat-abi=... option.  */
383 static const char * target_float_abi_name = NULL;
384
385 /* Set by the -mabi=... option.  */
386 static const char * target_abi_name = NULL;
387
388 /* Used to parse -mstructure_size_boundary command line option.  */
389 static const char * structure_size_string = NULL;
390 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
391
392 /* Used for Thumb call_via trampolines.  */
393 rtx thumb_call_via_label[14];
394 static int thumb_call_reg_needed;
395
396 /* Bit values used to identify processor capabilities.  */
397 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
398 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
399 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
400 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
401 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
402 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
403 #define FL_THUMB      (1 << 6)        /* Thumb aware */
404 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
405 #define FL_STRONG     (1 << 8)        /* StrongARM */
406 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
407 #define FL_XSCALE     (1 << 10)       /* XScale */
408 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
409 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
410                                          media instructions.  */
411 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
412 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
413                                          Note: ARM6 & 7 derivatives only.  */
414
415 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
416
417 #define FL_FOR_ARCH2    0
418 #define FL_FOR_ARCH3    FL_MODE32
419 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
420 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
421 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
422 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
423 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
424 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
425 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
426 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
427 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
428 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
429 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
430 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
431 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
432
433 /* The bits in this mask specify which
434    instructions we are allowed to generate.  */
435 static unsigned long insn_flags = 0;
436
437 /* The bits in this mask specify which instruction scheduling options should
438    be used.  */
439 static unsigned long tune_flags = 0;
440
441 /* The following are used in the arm.md file as equivalents to bits
442    in the above two flag variables.  */
443
444 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
445 int arm_arch3m = 0;
446
447 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
448 int arm_arch4 = 0;
449
450 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
451 int arm_arch4t = 0;
452
453 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
454 int arm_arch5 = 0;
455
456 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
457 int arm_arch5e = 0;
458
459 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
460 int arm_arch6 = 0;
461
462 /* Nonzero if this chip can benefit from load scheduling.  */
463 int arm_ld_sched = 0;
464
465 /* Nonzero if this chip is a StrongARM.  */
466 int arm_tune_strongarm = 0;
467
468 /* Nonzero if this chip is a Cirrus variant.  */
469 int arm_arch_cirrus = 0;
470
471 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
472 int arm_arch_iwmmxt = 0;
473
474 /* Nonzero if this chip is an XScale.  */
475 int arm_arch_xscale = 0;
476
477 /* Nonzero if tuning for XScale  */
478 int arm_tune_xscale = 0;
479
480 /* Nonzero if we want to tune for stores that access the write-buffer. 
481    This typically means an ARM6 or ARM7 with MMU or MPU.  */
482 int arm_tune_wbuf = 0;
483
484 /* Nonzero if generating Thumb instructions.  */
485 int thumb_code = 0;
486
487 /* Nonzero if we should define __THUMB_INTERWORK__ in the
488    preprocessor.
489    XXX This is a bit of a hack, it's intended to help work around
490    problems in GLD which doesn't understand that armv5t code is
491    interworking clean.  */
492 int arm_cpp_interwork = 0;
493
494 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
495    must report the mode of the memory reference from PRINT_OPERAND to
496    PRINT_OPERAND_ADDRESS.  */
497 enum machine_mode output_memory_reference_mode;
498
499 /* The register number to be used for the PIC offset register.  */
500 static const char * arm_pic_register_string = NULL;
501 int arm_pic_register = INVALID_REGNUM;
502
503 /* Set to 1 when a return insn is output, this means that the epilogue
504    is not needed.  */
505 int return_used_this_function;
506
507 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
508    the next function.  */
509 static int after_arm_reorg = 0;
510
511 /* The maximum number of insns to be used when loading a constant.  */
512 static int arm_constant_limit = 3;
513
514 /* For an explanation of these variables, see final_prescan_insn below.  */
515 int arm_ccfsm_state;
516 enum arm_cond_code arm_current_cc;
517 rtx arm_target_insn;
518 int arm_target_label;
519
520 /* The condition codes of the ARM, and the inverse function.  */
521 static const char * const arm_condition_codes[] =
522 {
523   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
524   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
525 };
526
527 #define streq(string1, string2) (strcmp (string1, string2) == 0)
528 \f
529 /* Initialization code.  */
530
531 struct processors
532 {
533   const char *const name;
534   enum processor_type core;
535   const char *arch;
536   const unsigned long flags;
537   bool (* rtx_costs) (rtx, int, int, int *);
538 };
539
540 /* Not all of these give usefully different compilation alternatives,
541    but there is no simple way of generalizing them.  */
542 static const struct processors all_cores[] =
543 {
544   /* ARM Cores */
545 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
546   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
547 #include "arm-cores.def"
548 #undef ARM_CORE
549   {NULL, arm_none, NULL, 0, NULL}
550 };
551
552 static const struct processors all_architectures[] =
553 {
554   /* ARM Architectures */
555   /* We don't specify rtx_costs here as it will be figured out
556      from the core.  */
557
558   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
559   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
560   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
561   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
562   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
563   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
564      implementations that support it, so we will leave it out for now.  */
565   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
566   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
567   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
568   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
569   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
570   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
571   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
572   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
573   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
574   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
575   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
576   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
577   {NULL, arm_none, NULL, 0 , NULL}
578 };
579
580 struct arm_cpu_select
581 {
582   const char *              string;
583   const char *              name;
584   const struct processors * processors;
585 };
586
587 /* This is a magic structure.  The 'string' field is magically filled in
588    with a pointer to the value specified by the user on the command line
589    assuming that the user has specified such a value.  */
590
591 static struct arm_cpu_select arm_select[] =
592 {
593   /* string       name            processors  */
594   { NULL,       "-mcpu=",       all_cores  },
595   { NULL,       "-march=",      all_architectures },
596   { NULL,       "-mtune=",      all_cores }
597 };
598
599 /* Defines representing the indexes into the above table.  */
600 #define ARM_OPT_SET_CPU 0
601 #define ARM_OPT_SET_ARCH 1
602 #define ARM_OPT_SET_TUNE 2
603
604 /* The name of the proprocessor macro to define for this architecture.  */
605
606 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
607
608 struct fpu_desc
609 {
610   const char * name;
611   enum fputype fpu;
612 };
613
614
615 /* Available values for for -mfpu=.  */
616
617 static const struct fpu_desc all_fpus[] =
618 {
619   {"fpa",       FPUTYPE_FPA},
620   {"fpe2",      FPUTYPE_FPA_EMU2},
621   {"fpe3",      FPUTYPE_FPA_EMU2},
622   {"maverick",  FPUTYPE_MAVERICK},
623   {"vfp",       FPUTYPE_VFP}
624 };
625
626
627 /* Floating point models used by the different hardware.
628    See fputype in arm.h.  */
629
630 static const enum fputype fp_model_for_fpu[] =
631 {
632   /* No FP hardware.  */
633   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
634   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
635   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
636   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
637   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
638   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
639 };
640
641
642 struct float_abi
643 {
644   const char * name;
645   enum float_abi_type abi_type;
646 };
647
648
649 /* Available values for -mfloat-abi=.  */
650
651 static const struct float_abi all_float_abis[] =
652 {
653   {"soft",      ARM_FLOAT_ABI_SOFT},
654   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
655   {"hard",      ARM_FLOAT_ABI_HARD}
656 };
657
658
659 struct abi_name
660 {
661   const char *name;
662   enum arm_abi_type abi_type;
663 };
664
665
666 /* Available values for -mabi=.  */
667
668 static const struct abi_name arm_all_abis[] =
669 {
670   {"apcs-gnu",    ARM_ABI_APCS},
671   {"atpcs",   ARM_ABI_ATPCS},
672   {"aapcs",   ARM_ABI_AAPCS},
673   {"iwmmxt",  ARM_ABI_IWMMXT}
674 };
675
676 /* Return the number of bits set in VALUE.  */
677 static unsigned
678 bit_count (unsigned long value)
679 {
680   unsigned long count = 0;
681
682   while (value)
683     {
684       count++;
685       value &= value - 1;  /* Clear the least-significant set bit.  */
686     }
687
688   return count;
689 }
690
691 /* Set up library functions unique to ARM.  */
692
693 static void
694 arm_init_libfuncs (void)
695 {
696   /* There are no special library functions unless we are using the
697      ARM BPABI.  */
698   if (!TARGET_BPABI)
699     return;
700
701   /* The functions below are described in Section 4 of the "Run-Time
702      ABI for the ARM architecture", Version 1.0.  */
703
704   /* Double-precision floating-point arithmetic.  Table 2.  */
705   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
706   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
707   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
708   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
709   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
710
711   /* Double-precision comparisons.  Table 3.  */
712   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
713   set_optab_libfunc (ne_optab, DFmode, NULL);
714   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
715   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
716   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
717   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
718   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
719
720   /* Single-precision floating-point arithmetic.  Table 4.  */
721   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
722   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
723   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
724   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
725   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
726
727   /* Single-precision comparisons.  Table 5.  */
728   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
729   set_optab_libfunc (ne_optab, SFmode, NULL);
730   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
731   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
732   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
733   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
734   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
735
736   /* Floating-point to integer conversions.  Table 6.  */
737   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
738   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
739   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
740   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
741   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
742   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
743   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
744   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
745
746   /* Conversions between floating types.  Table 7.  */
747   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
748   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
749
750   /* Integer to floating-point conversions.  Table 8.  */
751   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
752   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
753   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
754   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
755   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
756   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
757   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
758   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
759
760   /* Long long.  Table 9.  */
761   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
762   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
763   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
764   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
765   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
766   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
767   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
768   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
769
770   /* Integer (32/32->32) division.  \S 4.3.1.  */
771   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
772   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
773
774   /* The divmod functions are designed so that they can be used for
775      plain division, even though they return both the quotient and the
776      remainder.  The quotient is returned in the usual location (i.e.,
777      r0 for SImode, {r0, r1} for DImode), just as would be expected
778      for an ordinary division routine.  Because the AAPCS calling
779      conventions specify that all of { r0, r1, r2, r3 } are
780      callee-saved registers, there is no need to tell the compiler
781      explicitly that those registers are clobbered by these
782      routines.  */
783   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
784   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
785   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
786   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
787
788   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
789      divmod libcalls instead.  */
790   set_optab_libfunc (smod_optab, DImode, NULL);
791   set_optab_libfunc (umod_optab, DImode, NULL);
792   set_optab_libfunc (smod_optab, SImode, NULL);
793   set_optab_libfunc (umod_optab, SImode, NULL);
794 }
795
796 /* Implement TARGET_HANDLE_OPTION.  */
797
798 static bool
799 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
800 {
801   switch (code)
802     {
803     case OPT_mabi_:
804       target_abi_name = arg;
805       return true;
806
807     case OPT_march_:
808       arm_select[1].string = arg;
809       return true;
810
811     case OPT_mcpu_:
812       arm_select[0].string = arg;
813       return true;
814
815     case OPT_mfloat_abi_:
816       target_float_abi_name = arg;
817       return true;
818
819     case OPT_mfp_:
820     case OPT_mfpe_:
821       target_fpe_name = arg;
822       return true;
823
824     case OPT_mfpu_:
825       target_fpu_name = arg;
826       return true;
827
828     case OPT_mhard_float:
829       target_float_abi_name = "hard";
830       return true;
831
832     case OPT_mpic_register_:
833       arm_pic_register_string = arg;
834       return true;
835
836     case OPT_msoft_float:
837       target_float_abi_name = "soft";
838       return true;
839
840     case OPT_mstructure_size_boundary_:
841       structure_size_string = arg;
842       return true;
843
844     case OPT_mtune_:
845       arm_select[2].string = arg;
846       return true;
847
848     default:
849       return true;
850     }
851 }
852
853 /* Fix up any incompatible options that the user has specified.
854    This has now turned into a maze.  */
855 void
856 arm_override_options (void)
857 {
858   unsigned i;
859   enum processor_type target_arch_cpu = arm_none;
860
861   /* Set up the flags based on the cpu/architecture selected by the user.  */
862   for (i = ARRAY_SIZE (arm_select); i--;)
863     {
864       struct arm_cpu_select * ptr = arm_select + i;
865
866       if (ptr->string != NULL && ptr->string[0] != '\0')
867         {
868           const struct processors * sel;
869
870           for (sel = ptr->processors; sel->name != NULL; sel++)
871             if (streq (ptr->string, sel->name))
872               {
873                 /* Set the architecture define.  */
874                 if (i != ARM_OPT_SET_TUNE)
875                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
876
877                 /* Determine the processor core for which we should
878                    tune code-generation.  */
879                 if (/* -mcpu= is a sensible default.  */
880                     i == ARM_OPT_SET_CPU
881                     /* -mtune= overrides -mcpu= and -march=.  */
882                     || i == ARM_OPT_SET_TUNE)
883                   arm_tune = (enum processor_type) (sel - ptr->processors);
884
885                 /* Remember the CPU associated with this architecture.
886                    If no other option is used to set the CPU type,
887                    we'll use this to guess the most suitable tuning
888                    options.  */
889                 if (i == ARM_OPT_SET_ARCH)
890                   target_arch_cpu = sel->core;
891                 
892                 if (i != ARM_OPT_SET_TUNE)
893                   {
894                     /* If we have been given an architecture and a processor
895                        make sure that they are compatible.  We only generate
896                        a warning though, and we prefer the CPU over the
897                        architecture.  */
898                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
899                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
900                                ptr->string);
901
902                     insn_flags = sel->flags;
903                   }
904
905                 break;
906               }
907
908           if (sel->name == NULL)
909             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
910         }
911     }
912
913   /* Guess the tuning options from the architecture if necessary.  */
914   if (arm_tune == arm_none)
915     arm_tune = target_arch_cpu;
916
917   /* If the user did not specify a processor, choose one for them.  */
918   if (insn_flags == 0)
919     {
920       const struct processors * sel;
921       unsigned int        sought;
922       enum processor_type cpu;
923
924       cpu = TARGET_CPU_DEFAULT;
925       if (cpu == arm_none)
926         {
927 #ifdef SUBTARGET_CPU_DEFAULT
928           /* Use the subtarget default CPU if none was specified by
929              configure.  */
930           cpu = SUBTARGET_CPU_DEFAULT;
931 #endif
932           /* Default to ARM6.  */
933           if (cpu == arm_none)
934             cpu = arm6;
935         }
936       sel = &all_cores[cpu];
937
938       insn_flags = sel->flags;
939
940       /* Now check to see if the user has specified some command line
941          switch that require certain abilities from the cpu.  */
942       sought = 0;
943
944       if (TARGET_INTERWORK || TARGET_THUMB)
945         {
946           sought |= (FL_THUMB | FL_MODE32);
947
948           /* There are no ARM processors that support both APCS-26 and
949              interworking.  Therefore we force FL_MODE26 to be removed
950              from insn_flags here (if it was set), so that the search
951              below will always be able to find a compatible processor.  */
952           insn_flags &= ~FL_MODE26;
953         }
954
955       if (sought != 0 && ((sought & insn_flags) != sought))
956         {
957           /* Try to locate a CPU type that supports all of the abilities
958              of the default CPU, plus the extra abilities requested by
959              the user.  */
960           for (sel = all_cores; sel->name != NULL; sel++)
961             if ((sel->flags & sought) == (sought | insn_flags))
962               break;
963
964           if (sel->name == NULL)
965             {
966               unsigned current_bit_count = 0;
967               const struct processors * best_fit = NULL;
968
969               /* Ideally we would like to issue an error message here
970                  saying that it was not possible to find a CPU compatible
971                  with the default CPU, but which also supports the command
972                  line options specified by the programmer, and so they
973                  ought to use the -mcpu=<name> command line option to
974                  override the default CPU type.
975
976                  If we cannot find a cpu that has both the
977                  characteristics of the default cpu and the given
978                  command line options we scan the array again looking
979                  for a best match.  */
980               for (sel = all_cores; sel->name != NULL; sel++)
981                 if ((sel->flags & sought) == sought)
982                   {
983                     unsigned count;
984
985                     count = bit_count (sel->flags & insn_flags);
986
987                     if (count >= current_bit_count)
988                       {
989                         best_fit = sel;
990                         current_bit_count = count;
991                       }
992                   }
993
994               gcc_assert (best_fit);
995               sel = best_fit;
996             }
997
998           insn_flags = sel->flags;
999         }
1000       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1001       if (arm_tune == arm_none)
1002         arm_tune = (enum processor_type) (sel - all_cores);
1003     }
1004
1005   /* The processor for which we should tune should now have been
1006      chosen.  */
1007   gcc_assert (arm_tune != arm_none);
1008
1009   tune_flags = all_cores[(int)arm_tune].flags;
1010   if (optimize_size)
1011     targetm.rtx_costs = arm_size_rtx_costs;
1012   else
1013     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1014
1015   /* Make sure that the processor choice does not conflict with any of the
1016      other command line choices.  */
1017   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1018     {
1019       warning (0, "target CPU does not support interworking" );
1020       target_flags &= ~MASK_INTERWORK;
1021     }
1022
1023   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1024     {
1025       warning (0, "target CPU does not support THUMB instructions");
1026       target_flags &= ~MASK_THUMB;
1027     }
1028
1029   if (TARGET_APCS_FRAME && TARGET_THUMB)
1030     {
1031       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1032       target_flags &= ~MASK_APCS_FRAME;
1033     }
1034
1035   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1036      from here where no function is being compiled currently.  */
1037   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1038     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1039
1040   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1041     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1042
1043   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1044     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1045
1046   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1047     {
1048       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1049       target_flags |= MASK_APCS_FRAME;
1050     }
1051
1052   if (TARGET_POKE_FUNCTION_NAME)
1053     target_flags |= MASK_APCS_FRAME;
1054
1055   if (TARGET_APCS_REENT && flag_pic)
1056     error ("-fpic and -mapcs-reent are incompatible");
1057
1058   if (TARGET_APCS_REENT)
1059     warning (0, "APCS reentrant code not supported.  Ignored");
1060
1061   /* If this target is normally configured to use APCS frames, warn if they
1062      are turned off and debugging is turned on.  */
1063   if (TARGET_ARM
1064       && write_symbols != NO_DEBUG
1065       && !TARGET_APCS_FRAME
1066       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1067     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1068
1069   /* If stack checking is disabled, we can use r10 as the PIC register,
1070      which keeps r9 available.  */
1071   if (flag_pic)
1072     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1073
1074   if (TARGET_APCS_FLOAT)
1075     warning (0, "passing floating point arguments in fp regs not yet supported");
1076
1077   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1078   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1079   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1080   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1081   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1082   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1083   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1084   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1085   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1086
1087   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1088   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1089   thumb_code = (TARGET_ARM == 0);
1090   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1091   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1092   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1093
1094   /* V5 code we generate is completely interworking capable, so we turn off
1095      TARGET_INTERWORK here to avoid many tests later on.  */
1096
1097   /* XXX However, we must pass the right pre-processor defines to CPP
1098      or GLD can get confused.  This is a hack.  */
1099   if (TARGET_INTERWORK)
1100     arm_cpp_interwork = 1;
1101
1102   if (arm_arch5)
1103     target_flags &= ~MASK_INTERWORK;
1104
1105   if (target_abi_name)
1106     {
1107       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1108         {
1109           if (streq (arm_all_abis[i].name, target_abi_name))
1110             {
1111               arm_abi = arm_all_abis[i].abi_type;
1112               break;
1113             }
1114         }
1115       if (i == ARRAY_SIZE (arm_all_abis))
1116         error ("invalid ABI option: -mabi=%s", target_abi_name);
1117     }
1118   else
1119     arm_abi = ARM_DEFAULT_ABI;
1120
1121   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1122     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1123
1124   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1125     error ("iwmmxt abi requires an iwmmxt capable cpu");
1126
1127   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1128   if (target_fpu_name == NULL && target_fpe_name != NULL)
1129     {
1130       if (streq (target_fpe_name, "2"))
1131         target_fpu_name = "fpe2";
1132       else if (streq (target_fpe_name, "3"))
1133         target_fpu_name = "fpe3";
1134       else
1135         error ("invalid floating point emulation option: -mfpe=%s",
1136                target_fpe_name);
1137     }
1138   if (target_fpu_name != NULL)
1139     {
1140       /* The user specified a FPU.  */
1141       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1142         {
1143           if (streq (all_fpus[i].name, target_fpu_name))
1144             {
1145               arm_fpu_arch = all_fpus[i].fpu;
1146               arm_fpu_tune = arm_fpu_arch;
1147               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1148               break;
1149             }
1150         }
1151       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1152         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1153     }
1154   else
1155     {
1156 #ifdef FPUTYPE_DEFAULT
1157       /* Use the default if it is specified for this platform.  */
1158       arm_fpu_arch = FPUTYPE_DEFAULT;
1159       arm_fpu_tune = FPUTYPE_DEFAULT;
1160 #else
1161       /* Pick one based on CPU type.  */
1162       /* ??? Some targets assume FPA is the default.
1163       if ((insn_flags & FL_VFP) != 0)
1164         arm_fpu_arch = FPUTYPE_VFP;
1165       else
1166       */
1167       if (arm_arch_cirrus)
1168         arm_fpu_arch = FPUTYPE_MAVERICK;
1169       else
1170         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1171 #endif
1172       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1173         arm_fpu_tune = FPUTYPE_FPA;
1174       else
1175         arm_fpu_tune = arm_fpu_arch;
1176       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1177       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1178     }
1179
1180   if (target_float_abi_name != NULL)
1181     {
1182       /* The user specified a FP ABI.  */
1183       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1184         {
1185           if (streq (all_float_abis[i].name, target_float_abi_name))
1186             {
1187               arm_float_abi = all_float_abis[i].abi_type;
1188               break;
1189             }
1190         }
1191       if (i == ARRAY_SIZE (all_float_abis))
1192         error ("invalid floating point abi: -mfloat-abi=%s",
1193                target_float_abi_name);
1194     }
1195   else
1196     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1197
1198   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1199     sorry ("-mfloat-abi=hard and VFP");
1200
1201   /* If soft-float is specified then don't use FPU.  */
1202   if (TARGET_SOFT_FLOAT)
1203     arm_fpu_arch = FPUTYPE_NONE;
1204
1205   /* For arm2/3 there is no need to do any scheduling if there is only
1206      a floating point emulator, or we are doing software floating-point.  */
1207   if ((TARGET_SOFT_FLOAT
1208        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1209        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1210       && (tune_flags & FL_MODE32) == 0)
1211     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1212
1213   /* Override the default structure alignment for AAPCS ABI.  */
1214   if (arm_abi == ARM_ABI_AAPCS)
1215     arm_structure_size_boundary = 8;
1216
1217   if (structure_size_string != NULL)
1218     {
1219       int size = strtol (structure_size_string, NULL, 0);
1220
1221       if (size == 8 || size == 32
1222           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1223         arm_structure_size_boundary = size;
1224       else
1225         warning (0, "structure size boundary can only be set to %s",
1226                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1227     }
1228
1229   if (arm_pic_register_string != NULL)
1230     {
1231       int pic_register = decode_reg_name (arm_pic_register_string);
1232
1233       if (!flag_pic)
1234         warning (0, "-mpic-register= is useless without -fpic");
1235
1236       /* Prevent the user from choosing an obviously stupid PIC register.  */
1237       else if (pic_register < 0 || call_used_regs[pic_register]
1238                || pic_register == HARD_FRAME_POINTER_REGNUM
1239                || pic_register == STACK_POINTER_REGNUM
1240                || pic_register >= PC_REGNUM)
1241         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1242       else
1243         arm_pic_register = pic_register;
1244     }
1245
1246   if (TARGET_THUMB && flag_schedule_insns)
1247     {
1248       /* Don't warn since it's on by default in -O2.  */
1249       flag_schedule_insns = 0;
1250     }
1251
1252   if (optimize_size)
1253     {
1254       /* There's some dispute as to whether this should be 1 or 2.  However,
1255          experiments seem to show that in pathological cases a setting of
1256          1 degrades less severely than a setting of 2.  This could change if
1257          other parts of the compiler change their behavior.  */
1258       arm_constant_limit = 1;
1259
1260       /* If optimizing for size, bump the number of instructions that we
1261          are prepared to conditionally execute (even on a StrongARM).  */
1262       max_insns_skipped = 6;
1263     }
1264   else
1265     {
1266       /* For processors with load scheduling, it never costs more than
1267          2 cycles to load a constant, and the load scheduler may well
1268          reduce that to 1.  */
1269       if (arm_ld_sched)
1270         arm_constant_limit = 1;
1271
1272       /* On XScale the longer latency of a load makes it more difficult
1273          to achieve a good schedule, so it's faster to synthesize
1274          constants that can be done in two insns.  */
1275       if (arm_tune_xscale)
1276         arm_constant_limit = 2;
1277
1278       /* StrongARM has early execution of branches, so a sequence
1279          that is worth skipping is shorter.  */
1280       if (arm_tune_strongarm)
1281         max_insns_skipped = 3;
1282     }
1283
1284   /* Register global variables with the garbage collector.  */
1285   arm_add_gc_roots ();
1286 }
1287
1288 static void
1289 arm_add_gc_roots (void)
1290 {
1291   gcc_obstack_init(&minipool_obstack);
1292   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1293 }
1294 \f
1295 /* A table of known ARM exception types.
1296    For use with the interrupt function attribute.  */
1297
1298 typedef struct
1299 {
1300   const char *const arg;
1301   const unsigned long return_value;
1302 }
1303 isr_attribute_arg;
1304
1305 static const isr_attribute_arg isr_attribute_args [] =
1306 {
1307   { "IRQ",   ARM_FT_ISR },
1308   { "irq",   ARM_FT_ISR },
1309   { "FIQ",   ARM_FT_FIQ },
1310   { "fiq",   ARM_FT_FIQ },
1311   { "ABORT", ARM_FT_ISR },
1312   { "abort", ARM_FT_ISR },
1313   { "ABORT", ARM_FT_ISR },
1314   { "abort", ARM_FT_ISR },
1315   { "UNDEF", ARM_FT_EXCEPTION },
1316   { "undef", ARM_FT_EXCEPTION },
1317   { "SWI",   ARM_FT_EXCEPTION },
1318   { "swi",   ARM_FT_EXCEPTION },
1319   { NULL,    ARM_FT_NORMAL }
1320 };
1321
1322 /* Returns the (interrupt) function type of the current
1323    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1324
1325 static unsigned long
1326 arm_isr_value (tree argument)
1327 {
1328   const isr_attribute_arg * ptr;
1329   const char *              arg;
1330
1331   /* No argument - default to IRQ.  */
1332   if (argument == NULL_TREE)
1333     return ARM_FT_ISR;
1334
1335   /* Get the value of the argument.  */
1336   if (TREE_VALUE (argument) == NULL_TREE
1337       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1338     return ARM_FT_UNKNOWN;
1339
1340   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1341
1342   /* Check it against the list of known arguments.  */
1343   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1344     if (streq (arg, ptr->arg))
1345       return ptr->return_value;
1346
1347   /* An unrecognized interrupt type.  */
1348   return ARM_FT_UNKNOWN;
1349 }
1350
1351 /* Computes the type of the current function.  */
1352
1353 static unsigned long
1354 arm_compute_func_type (void)
1355 {
1356   unsigned long type = ARM_FT_UNKNOWN;
1357   tree a;
1358   tree attr;
1359
1360   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1361
1362   /* Decide if the current function is volatile.  Such functions
1363      never return, and many memory cycles can be saved by not storing
1364      register values that will never be needed again.  This optimization
1365      was added to speed up context switching in a kernel application.  */
1366   if (optimize > 0
1367       && TREE_NOTHROW (current_function_decl)
1368       && TREE_THIS_VOLATILE (current_function_decl))
1369     type |= ARM_FT_VOLATILE;
1370
1371   if (cfun->static_chain_decl != NULL)
1372     type |= ARM_FT_NESTED;
1373
1374   attr = DECL_ATTRIBUTES (current_function_decl);
1375
1376   a = lookup_attribute ("naked", attr);
1377   if (a != NULL_TREE)
1378     type |= ARM_FT_NAKED;
1379
1380   a = lookup_attribute ("isr", attr);
1381   if (a == NULL_TREE)
1382     a = lookup_attribute ("interrupt", attr);
1383
1384   if (a == NULL_TREE)
1385     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1386   else
1387     type |= arm_isr_value (TREE_VALUE (a));
1388
1389   return type;
1390 }
1391
1392 /* Returns the type of the current function.  */
1393
1394 unsigned long
1395 arm_current_func_type (void)
1396 {
1397   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1398     cfun->machine->func_type = arm_compute_func_type ();
1399
1400   return cfun->machine->func_type;
1401 }
1402 \f
1403 /* Return 1 if it is possible to return using a single instruction.
1404    If SIBLING is non-null, this is a test for a return before a sibling
1405    call.  SIBLING is the call insn, so we can examine its register usage.  */
1406
1407 int
1408 use_return_insn (int iscond, rtx sibling)
1409 {
1410   int regno;
1411   unsigned int func_type;
1412   unsigned long saved_int_regs;
1413   unsigned HOST_WIDE_INT stack_adjust;
1414   arm_stack_offsets *offsets;
1415
1416   /* Never use a return instruction before reload has run.  */
1417   if (!reload_completed)
1418     return 0;
1419
1420   func_type = arm_current_func_type ();
1421
1422   /* Naked functions and volatile functions need special
1423      consideration.  */
1424   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1425     return 0;
1426
1427   /* So do interrupt functions that use the frame pointer.  */
1428   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1429     return 0;
1430
1431   offsets = arm_get_frame_offsets ();
1432   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1433
1434   /* As do variadic functions.  */
1435   if (current_function_pretend_args_size
1436       || cfun->machine->uses_anonymous_args
1437       /* Or if the function calls __builtin_eh_return () */
1438       || current_function_calls_eh_return
1439       /* Or if the function calls alloca */
1440       || current_function_calls_alloca
1441       /* Or if there is a stack adjustment.  However, if the stack pointer
1442          is saved on the stack, we can use a pre-incrementing stack load.  */
1443       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1444     return 0;
1445
1446   saved_int_regs = arm_compute_save_reg_mask ();
1447
1448   /* Unfortunately, the insn
1449
1450        ldmib sp, {..., sp, ...}
1451
1452      triggers a bug on most SA-110 based devices, such that the stack
1453      pointer won't be correctly restored if the instruction takes a
1454      page fault.  We work around this problem by popping r3 along with
1455      the other registers, since that is never slower than executing
1456      another instruction.
1457
1458      We test for !arm_arch5 here, because code for any architecture
1459      less than this could potentially be run on one of the buggy
1460      chips.  */
1461   if (stack_adjust == 4 && !arm_arch5)
1462     {
1463       /* Validate that r3 is a call-clobbered register (always true in
1464          the default abi) ...  */
1465       if (!call_used_regs[3])
1466         return 0;
1467
1468       /* ... that it isn't being used for a return value (always true
1469          until we implement return-in-regs), or for a tail-call
1470          argument ...  */
1471       if (sibling)
1472         {
1473           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1474
1475           if (find_regno_fusage (sibling, USE, 3))
1476             return 0;
1477         }
1478
1479       /* ... and that there are no call-saved registers in r0-r2
1480          (always true in the default ABI).  */
1481       if (saved_int_regs & 0x7)
1482         return 0;
1483     }
1484
1485   /* Can't be done if interworking with Thumb, and any registers have been
1486      stacked.  */
1487   if (TARGET_INTERWORK && saved_int_regs != 0)
1488     return 0;
1489
1490   /* On StrongARM, conditional returns are expensive if they aren't
1491      taken and multiple registers have been stacked.  */
1492   if (iscond && arm_tune_strongarm)
1493     {
1494       /* Conditional return when just the LR is stored is a simple
1495          conditional-load instruction, that's not expensive.  */
1496       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1497         return 0;
1498
1499       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1500         return 0;
1501     }
1502
1503   /* If there are saved registers but the LR isn't saved, then we need
1504      two instructions for the return.  */
1505   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1506     return 0;
1507
1508   /* Can't be done if any of the FPA regs are pushed,
1509      since this also requires an insn.  */
1510   if (TARGET_HARD_FLOAT && TARGET_FPA)
1511     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1512       if (regs_ever_live[regno] && !call_used_regs[regno])
1513         return 0;
1514
1515   /* Likewise VFP regs.  */
1516   if (TARGET_HARD_FLOAT && TARGET_VFP)
1517     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1518       if (regs_ever_live[regno] && !call_used_regs[regno])
1519         return 0;
1520
1521   if (TARGET_REALLY_IWMMXT)
1522     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1523       if (regs_ever_live[regno] && ! call_used_regs [regno])
1524         return 0;
1525
1526   return 1;
1527 }
1528
1529 /* Return TRUE if int I is a valid immediate ARM constant.  */
1530
1531 int
1532 const_ok_for_arm (HOST_WIDE_INT i)
1533 {
1534   int lowbit;
1535   
1536   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1537      be all zero, or all one.  */
1538   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1539       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1540           != ((~(unsigned HOST_WIDE_INT) 0)
1541               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1542     return FALSE;
1543
1544   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1545   
1546   /* Fast return for 0 and small values.  We must do this for zero, since
1547      the code below can't handle that one case.  */
1548   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1549     return TRUE;
1550
1551   /* Get the number of trailing zeros, rounded down to the nearest even
1552      number.  */
1553   lowbit = (ffs ((int) i) - 1) & ~1;
1554
1555   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1556     return TRUE;
1557   else if (lowbit <= 4
1558            && ((i & ~0xc000003f) == 0
1559                || (i & ~0xf000000f) == 0
1560                || (i & ~0xfc000003) == 0))
1561     return TRUE;
1562
1563   return FALSE;
1564 }
1565
1566 /* Return true if I is a valid constant for the operation CODE.  */
1567 static int
1568 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1569 {
1570   if (const_ok_for_arm (i))
1571     return 1;
1572
1573   switch (code)
1574     {
1575     case PLUS:
1576       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1577
1578     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1579     case XOR:
1580     case IOR:
1581       return 0;
1582
1583     case AND:
1584       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1585
1586     default:
1587       gcc_unreachable ();
1588     }
1589 }
1590
1591 /* Emit a sequence of insns to handle a large constant.
1592    CODE is the code of the operation required, it can be any of SET, PLUS,
1593    IOR, AND, XOR, MINUS;
1594    MODE is the mode in which the operation is being performed;
1595    VAL is the integer to operate on;
1596    SOURCE is the other operand (a register, or a null-pointer for SET);
1597    SUBTARGETS means it is safe to create scratch registers if that will
1598    either produce a simpler sequence, or we will want to cse the values.
1599    Return value is the number of insns emitted.  */
1600
1601 int
1602 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1603                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1604 {
1605   rtx cond;
1606
1607   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1608     cond = COND_EXEC_TEST (PATTERN (insn));
1609   else
1610     cond = NULL_RTX;
1611
1612   if (subtargets || code == SET
1613       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1614           && REGNO (target) != REGNO (source)))
1615     {
1616       /* After arm_reorg has been called, we can't fix up expensive
1617          constants by pushing them into memory so we must synthesize
1618          them in-line, regardless of the cost.  This is only likely to
1619          be more costly on chips that have load delay slots and we are
1620          compiling without running the scheduler (so no splitting
1621          occurred before the final instruction emission).
1622
1623          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1624       */
1625       if (!after_arm_reorg
1626           && !cond
1627           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1628                                 1, 0)
1629               > arm_constant_limit + (code != SET)))
1630         {
1631           if (code == SET)
1632             {
1633               /* Currently SET is the only monadic value for CODE, all
1634                  the rest are diadic.  */
1635               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1636               return 1;
1637             }
1638           else
1639             {
1640               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1641
1642               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1643               /* For MINUS, the value is subtracted from, since we never
1644                  have subtraction of a constant.  */
1645               if (code == MINUS)
1646                 emit_insn (gen_rtx_SET (VOIDmode, target,
1647                                         gen_rtx_MINUS (mode, temp, source)));
1648               else
1649                 emit_insn (gen_rtx_SET (VOIDmode, target,
1650                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1651               return 2;
1652             }
1653         }
1654     }
1655
1656   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1657                            1);
1658 }
1659
1660 static int
1661 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1662 {
1663   HOST_WIDE_INT temp1;
1664   int num_insns = 0;
1665   do
1666     {
1667       int end;
1668
1669       if (i <= 0)
1670         i += 32;
1671       if (remainder & (3 << (i - 2)))
1672         {
1673           end = i - 8;
1674           if (end < 0)
1675             end += 32;
1676           temp1 = remainder & ((0x0ff << end)
1677                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1678           remainder &= ~temp1;
1679           num_insns++;
1680           i -= 6;
1681         }
1682       i -= 2;
1683     } while (remainder);
1684   return num_insns;
1685 }
1686
1687 /* Emit an instruction with the indicated PATTERN.  If COND is
1688    non-NULL, conditionalize the execution of the instruction on COND
1689    being true.  */
1690
1691 static void
1692 emit_constant_insn (rtx cond, rtx pattern)
1693 {
1694   if (cond)
1695     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1696   emit_insn (pattern);
1697 }
1698
1699 /* As above, but extra parameter GENERATE which, if clear, suppresses
1700    RTL generation.  */
1701
1702 static int
1703 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1704                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1705                   int generate)
1706 {
1707   int can_invert = 0;
1708   int can_negate = 0;
1709   int can_negate_initial = 0;
1710   int can_shift = 0;
1711   int i;
1712   int num_bits_set = 0;
1713   int set_sign_bit_copies = 0;
1714   int clear_sign_bit_copies = 0;
1715   int clear_zero_bit_copies = 0;
1716   int set_zero_bit_copies = 0;
1717   int insns = 0;
1718   unsigned HOST_WIDE_INT temp1, temp2;
1719   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1720
1721   /* Find out which operations are safe for a given CODE.  Also do a quick
1722      check for degenerate cases; these can occur when DImode operations
1723      are split.  */
1724   switch (code)
1725     {
1726     case SET:
1727       can_invert = 1;
1728       can_shift = 1;
1729       can_negate = 1;
1730       break;
1731
1732     case PLUS:
1733       can_negate = 1;
1734       can_negate_initial = 1;
1735       break;
1736
1737     case IOR:
1738       if (remainder == 0xffffffff)
1739         {
1740           if (generate)
1741             emit_constant_insn (cond,
1742                                 gen_rtx_SET (VOIDmode, target,
1743                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1744           return 1;
1745         }
1746       if (remainder == 0)
1747         {
1748           if (reload_completed && rtx_equal_p (target, source))
1749             return 0;
1750           if (generate)
1751             emit_constant_insn (cond,
1752                                 gen_rtx_SET (VOIDmode, target, source));
1753           return 1;
1754         }
1755       break;
1756
1757     case AND:
1758       if (remainder == 0)
1759         {
1760           if (generate)
1761             emit_constant_insn (cond,
1762                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1763           return 1;
1764         }
1765       if (remainder == 0xffffffff)
1766         {
1767           if (reload_completed && rtx_equal_p (target, source))
1768             return 0;
1769           if (generate)
1770             emit_constant_insn (cond,
1771                                 gen_rtx_SET (VOIDmode, target, source));
1772           return 1;
1773         }
1774       can_invert = 1;
1775       break;
1776
1777     case XOR:
1778       if (remainder == 0)
1779         {
1780           if (reload_completed && rtx_equal_p (target, source))
1781             return 0;
1782           if (generate)
1783             emit_constant_insn (cond,
1784                                 gen_rtx_SET (VOIDmode, target, source));
1785           return 1;
1786         }
1787       
1788       /* We don't know how to handle other cases yet.  */
1789       gcc_assert (remainder == 0xffffffff);
1790       
1791       if (generate)
1792         emit_constant_insn (cond,
1793                             gen_rtx_SET (VOIDmode, target,
1794                                          gen_rtx_NOT (mode, source)));
1795       return 1;
1796
1797     case MINUS:
1798       /* We treat MINUS as (val - source), since (source - val) is always
1799          passed as (source + (-val)).  */
1800       if (remainder == 0)
1801         {
1802           if (generate)
1803             emit_constant_insn (cond,
1804                                 gen_rtx_SET (VOIDmode, target,
1805                                              gen_rtx_NEG (mode, source)));
1806           return 1;
1807         }
1808       if (const_ok_for_arm (val))
1809         {
1810           if (generate)
1811             emit_constant_insn (cond,
1812                                 gen_rtx_SET (VOIDmode, target,
1813                                              gen_rtx_MINUS (mode, GEN_INT (val),
1814                                                             source)));
1815           return 1;
1816         }
1817       can_negate = 1;
1818
1819       break;
1820
1821     default:
1822       gcc_unreachable ();
1823     }
1824
1825   /* If we can do it in one insn get out quickly.  */
1826   if (const_ok_for_arm (val)
1827       || (can_negate_initial && const_ok_for_arm (-val))
1828       || (can_invert && const_ok_for_arm (~val)))
1829     {
1830       if (generate)
1831         emit_constant_insn (cond,
1832                             gen_rtx_SET (VOIDmode, target,
1833                                          (source
1834                                           ? gen_rtx_fmt_ee (code, mode, source,
1835                                                             GEN_INT (val))
1836                                           : GEN_INT (val))));
1837       return 1;
1838     }
1839
1840   /* Calculate a few attributes that may be useful for specific
1841      optimizations.  */
1842   for (i = 31; i >= 0; i--)
1843     {
1844       if ((remainder & (1 << i)) == 0)
1845         clear_sign_bit_copies++;
1846       else
1847         break;
1848     }
1849
1850   for (i = 31; i >= 0; i--)
1851     {
1852       if ((remainder & (1 << i)) != 0)
1853         set_sign_bit_copies++;
1854       else
1855         break;
1856     }
1857
1858   for (i = 0; i <= 31; i++)
1859     {
1860       if ((remainder & (1 << i)) == 0)
1861         clear_zero_bit_copies++;
1862       else
1863         break;
1864     }
1865
1866   for (i = 0; i <= 31; i++)
1867     {
1868       if ((remainder & (1 << i)) != 0)
1869         set_zero_bit_copies++;
1870       else
1871         break;
1872     }
1873
1874   switch (code)
1875     {
1876     case SET:
1877       /* See if we can do this by sign_extending a constant that is known
1878          to be negative.  This is a good, way of doing it, since the shift
1879          may well merge into a subsequent insn.  */
1880       if (set_sign_bit_copies > 1)
1881         {
1882           if (const_ok_for_arm
1883               (temp1 = ARM_SIGN_EXTEND (remainder
1884                                         << (set_sign_bit_copies - 1))))
1885             {
1886               if (generate)
1887                 {
1888                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1889                   emit_constant_insn (cond,
1890                                       gen_rtx_SET (VOIDmode, new_src,
1891                                                    GEN_INT (temp1)));
1892                   emit_constant_insn (cond,
1893                                       gen_ashrsi3 (target, new_src,
1894                                                    GEN_INT (set_sign_bit_copies - 1)));
1895                 }
1896               return 2;
1897             }
1898           /* For an inverted constant, we will need to set the low bits,
1899              these will be shifted out of harm's way.  */
1900           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1901           if (const_ok_for_arm (~temp1))
1902             {
1903               if (generate)
1904                 {
1905                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1906                   emit_constant_insn (cond,
1907                                       gen_rtx_SET (VOIDmode, new_src,
1908                                                    GEN_INT (temp1)));
1909                   emit_constant_insn (cond,
1910                                       gen_ashrsi3 (target, new_src,
1911                                                    GEN_INT (set_sign_bit_copies - 1)));
1912                 }
1913               return 2;
1914             }
1915         }
1916
1917       /* See if we can calculate the value as the difference between two
1918          valid immediates.  */
1919       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1920         {
1921           int topshift = clear_sign_bit_copies & ~1;
1922
1923           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1924                                    & (0xff000000 >> topshift));
1925
1926           /* If temp1 is zero, then that means the 9 most significant
1927              bits of remainder were 1 and we've caused it to overflow.
1928              When topshift is 0 we don't need to do anything since we
1929              can borrow from 'bit 32'.  */
1930           if (temp1 == 0 && topshift != 0)
1931             temp1 = 0x80000000 >> (topshift - 1);
1932
1933           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1934           
1935           if (const_ok_for_arm (temp2))
1936             {
1937               if (generate)
1938                 {
1939                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1940                   emit_constant_insn (cond,
1941                                       gen_rtx_SET (VOIDmode, new_src,
1942                                                    GEN_INT (temp1)));
1943                   emit_constant_insn (cond,
1944                                       gen_addsi3 (target, new_src,
1945                                                   GEN_INT (-temp2)));
1946                 }
1947
1948               return 2;
1949             }
1950         }
1951
1952       /* See if we can generate this by setting the bottom (or the top)
1953          16 bits, and then shifting these into the other half of the
1954          word.  We only look for the simplest cases, to do more would cost
1955          too much.  Be careful, however, not to generate this when the
1956          alternative would take fewer insns.  */
1957       if (val & 0xffff0000)
1958         {
1959           temp1 = remainder & 0xffff0000;
1960           temp2 = remainder & 0x0000ffff;
1961
1962           /* Overlaps outside this range are best done using other methods.  */
1963           for (i = 9; i < 24; i++)
1964             {
1965               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1966                   && !const_ok_for_arm (temp2))
1967                 {
1968                   rtx new_src = (subtargets
1969                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1970                                  : target);
1971                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1972                                             source, subtargets, generate);
1973                   source = new_src;
1974                   if (generate)
1975                     emit_constant_insn
1976                       (cond,
1977                        gen_rtx_SET
1978                        (VOIDmode, target,
1979                         gen_rtx_IOR (mode,
1980                                      gen_rtx_ASHIFT (mode, source,
1981                                                      GEN_INT (i)),
1982                                      source)));
1983                   return insns + 1;
1984                 }
1985             }
1986
1987           /* Don't duplicate cases already considered.  */
1988           for (i = 17; i < 24; i++)
1989             {
1990               if (((temp1 | (temp1 >> i)) == remainder)
1991                   && !const_ok_for_arm (temp1))
1992                 {
1993                   rtx new_src = (subtargets
1994                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1995                                  : target);
1996                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1997                                             source, subtargets, generate);
1998                   source = new_src;
1999                   if (generate)
2000                     emit_constant_insn
2001                       (cond,
2002                        gen_rtx_SET (VOIDmode, target,
2003                                     gen_rtx_IOR
2004                                     (mode,
2005                                      gen_rtx_LSHIFTRT (mode, source,
2006                                                        GEN_INT (i)),
2007                                      source)));
2008                   return insns + 1;
2009                 }
2010             }
2011         }
2012       break;
2013
2014     case IOR:
2015     case XOR:
2016       /* If we have IOR or XOR, and the constant can be loaded in a
2017          single instruction, and we can find a temporary to put it in,
2018          then this can be done in two instructions instead of 3-4.  */
2019       if (subtargets
2020           /* TARGET can't be NULL if SUBTARGETS is 0 */
2021           || (reload_completed && !reg_mentioned_p (target, source)))
2022         {
2023           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2024             {
2025               if (generate)
2026                 {
2027                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2028
2029                   emit_constant_insn (cond,
2030                                       gen_rtx_SET (VOIDmode, sub,
2031                                                    GEN_INT (val)));
2032                   emit_constant_insn (cond,
2033                                       gen_rtx_SET (VOIDmode, target,
2034                                                    gen_rtx_fmt_ee (code, mode,
2035                                                                    source, sub)));
2036                 }
2037               return 2;
2038             }
2039         }
2040
2041       if (code == XOR)
2042         break;
2043
2044       if (set_sign_bit_copies > 8
2045           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2046         {
2047           if (generate)
2048             {
2049               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2050               rtx shift = GEN_INT (set_sign_bit_copies);
2051
2052               emit_constant_insn
2053                 (cond,
2054                  gen_rtx_SET (VOIDmode, sub,
2055                               gen_rtx_NOT (mode,
2056                                            gen_rtx_ASHIFT (mode,
2057                                                            source,
2058                                                            shift))));
2059               emit_constant_insn
2060                 (cond,
2061                  gen_rtx_SET (VOIDmode, target,
2062                               gen_rtx_NOT (mode,
2063                                            gen_rtx_LSHIFTRT (mode, sub,
2064                                                              shift))));
2065             }
2066           return 2;
2067         }
2068
2069       if (set_zero_bit_copies > 8
2070           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2071         {
2072           if (generate)
2073             {
2074               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2075               rtx shift = GEN_INT (set_zero_bit_copies);
2076
2077               emit_constant_insn
2078                 (cond,
2079                  gen_rtx_SET (VOIDmode, sub,
2080                               gen_rtx_NOT (mode,
2081                                            gen_rtx_LSHIFTRT (mode,
2082                                                              source,
2083                                                              shift))));
2084               emit_constant_insn
2085                 (cond,
2086                  gen_rtx_SET (VOIDmode, target,
2087                               gen_rtx_NOT (mode,
2088                                            gen_rtx_ASHIFT (mode, sub,
2089                                                            shift))));
2090             }
2091           return 2;
2092         }
2093
2094       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2095         {
2096           if (generate)
2097             {
2098               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2099               emit_constant_insn (cond,
2100                                   gen_rtx_SET (VOIDmode, sub,
2101                                                gen_rtx_NOT (mode, source)));
2102               source = sub;
2103               if (subtargets)
2104                 sub = gen_reg_rtx (mode);
2105               emit_constant_insn (cond,
2106                                   gen_rtx_SET (VOIDmode, sub,
2107                                                gen_rtx_AND (mode, source,
2108                                                             GEN_INT (temp1))));
2109               emit_constant_insn (cond,
2110                                   gen_rtx_SET (VOIDmode, target,
2111                                                gen_rtx_NOT (mode, sub)));
2112             }
2113           return 3;
2114         }
2115       break;
2116
2117     case AND:
2118       /* See if two shifts will do 2 or more insn's worth of work.  */
2119       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2120         {
2121           HOST_WIDE_INT shift_mask = ((0xffffffff
2122                                        << (32 - clear_sign_bit_copies))
2123                                       & 0xffffffff);
2124
2125           if ((remainder | shift_mask) != 0xffffffff)
2126             {
2127               if (generate)
2128                 {
2129                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2130                   insns = arm_gen_constant (AND, mode, cond,
2131                                             remainder | shift_mask,
2132                                             new_src, source, subtargets, 1);
2133                   source = new_src;
2134                 }
2135               else
2136                 {
2137                   rtx targ = subtargets ? NULL_RTX : target;
2138                   insns = arm_gen_constant (AND, mode, cond,
2139                                             remainder | shift_mask,
2140                                             targ, source, subtargets, 0);
2141                 }
2142             }
2143
2144           if (generate)
2145             {
2146               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2147               rtx shift = GEN_INT (clear_sign_bit_copies);
2148
2149               emit_insn (gen_ashlsi3 (new_src, source, shift));
2150               emit_insn (gen_lshrsi3 (target, new_src, shift));
2151             }
2152
2153           return insns + 2;
2154         }
2155
2156       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2157         {
2158           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2159
2160           if ((remainder | shift_mask) != 0xffffffff)
2161             {
2162               if (generate)
2163                 {
2164                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2165
2166                   insns = arm_gen_constant (AND, mode, cond,
2167                                             remainder | shift_mask,
2168                                             new_src, source, subtargets, 1);
2169                   source = new_src;
2170                 }
2171               else
2172                 {
2173                   rtx targ = subtargets ? NULL_RTX : target;
2174
2175                   insns = arm_gen_constant (AND, mode, cond,
2176                                             remainder | shift_mask,
2177                                             targ, source, subtargets, 0);
2178                 }
2179             }
2180
2181           if (generate)
2182             {
2183               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2184               rtx shift = GEN_INT (clear_zero_bit_copies);
2185
2186               emit_insn (gen_lshrsi3 (new_src, source, shift));
2187               emit_insn (gen_ashlsi3 (target, new_src, shift));
2188             }
2189
2190           return insns + 2;
2191         }
2192
2193       break;
2194
2195     default:
2196       break;
2197     }
2198
2199   for (i = 0; i < 32; i++)
2200     if (remainder & (1 << i))
2201       num_bits_set++;
2202
2203   if (code == AND || (can_invert && num_bits_set > 16))
2204     remainder = (~remainder) & 0xffffffff;
2205   else if (code == PLUS && num_bits_set > 16)
2206     remainder = (-remainder) & 0xffffffff;
2207   else
2208     {
2209       can_invert = 0;
2210       can_negate = 0;
2211     }
2212
2213   /* Now try and find a way of doing the job in either two or three
2214      instructions.
2215      We start by looking for the largest block of zeros that are aligned on
2216      a 2-bit boundary, we then fill up the temps, wrapping around to the
2217      top of the word when we drop off the bottom.
2218      In the worst case this code should produce no more than four insns.  */
2219   {
2220     int best_start = 0;
2221     int best_consecutive_zeros = 0;
2222
2223     for (i = 0; i < 32; i += 2)
2224       {
2225         int consecutive_zeros = 0;
2226
2227         if (!(remainder & (3 << i)))
2228           {
2229             while ((i < 32) && !(remainder & (3 << i)))
2230               {
2231                 consecutive_zeros += 2;
2232                 i += 2;
2233               }
2234             if (consecutive_zeros > best_consecutive_zeros)
2235               {
2236                 best_consecutive_zeros = consecutive_zeros;
2237                 best_start = i - consecutive_zeros;
2238               }
2239             i -= 2;
2240           }
2241       }
2242
2243     /* So long as it won't require any more insns to do so, it's
2244        desirable to emit a small constant (in bits 0...9) in the last
2245        insn.  This way there is more chance that it can be combined with
2246        a later addressing insn to form a pre-indexed load or store
2247        operation.  Consider:
2248
2249                *((volatile int *)0xe0000100) = 1;
2250                *((volatile int *)0xe0000110) = 2;
2251
2252        We want this to wind up as:
2253
2254                 mov rA, #0xe0000000
2255                 mov rB, #1
2256                 str rB, [rA, #0x100]
2257                 mov rB, #2
2258                 str rB, [rA, #0x110]
2259
2260        rather than having to synthesize both large constants from scratch.
2261
2262        Therefore, we calculate how many insns would be required to emit
2263        the constant starting from `best_start', and also starting from
2264        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2265        yield a shorter sequence, we may as well use zero.  */
2266     if (best_start != 0
2267         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2268         && (count_insns_for_constant (remainder, 0) <=
2269             count_insns_for_constant (remainder, best_start)))
2270       best_start = 0;
2271
2272     /* Now start emitting the insns.  */
2273     i = best_start;
2274     do
2275       {
2276         int end;
2277
2278         if (i <= 0)
2279           i += 32;
2280         if (remainder & (3 << (i - 2)))
2281           {
2282             end = i - 8;
2283             if (end < 0)
2284               end += 32;
2285             temp1 = remainder & ((0x0ff << end)
2286                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2287             remainder &= ~temp1;
2288
2289             if (generate)
2290               {
2291                 rtx new_src, temp1_rtx;
2292
2293                 if (code == SET || code == MINUS)
2294                   {
2295                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2296                     if (can_invert && code != MINUS)
2297                       temp1 = ~temp1;
2298                   }
2299                 else
2300                   {
2301                     if (remainder && subtargets)
2302                       new_src = gen_reg_rtx (mode);
2303                     else
2304                       new_src = target;
2305                     if (can_invert)
2306                       temp1 = ~temp1;
2307                     else if (can_negate)
2308                       temp1 = -temp1;
2309                   }
2310
2311                 temp1 = trunc_int_for_mode (temp1, mode);
2312                 temp1_rtx = GEN_INT (temp1);
2313
2314                 if (code == SET)
2315                   ;
2316                 else if (code == MINUS)
2317                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2318                 else
2319                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2320
2321                 emit_constant_insn (cond,
2322                                     gen_rtx_SET (VOIDmode, new_src,
2323                                                  temp1_rtx));
2324                 source = new_src;
2325               }
2326
2327             if (code == SET)
2328               {
2329                 can_invert = 0;
2330                 code = PLUS;
2331               }
2332             else if (code == MINUS)
2333               code = PLUS;
2334
2335             insns++;
2336             i -= 6;
2337           }
2338         i -= 2;
2339       }
2340     while (remainder);
2341   }
2342
2343   return insns;
2344 }
2345
2346 /* Canonicalize a comparison so that we are more likely to recognize it.
2347    This can be done for a few constant compares, where we can make the
2348    immediate value easier to load.  */
2349
2350 enum rtx_code
2351 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2352 {
2353   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2354
2355   switch (code)
2356     {
2357     case EQ:
2358     case NE:
2359       return code;
2360
2361     case GT:
2362     case LE:
2363       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2364           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2365         {
2366           *op1 = GEN_INT (i + 1);
2367           return code == GT ? GE : LT;
2368         }
2369       break;
2370
2371     case GE:
2372     case LT:
2373       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2374           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2375         {
2376           *op1 = GEN_INT (i - 1);
2377           return code == GE ? GT : LE;
2378         }
2379       break;
2380
2381     case GTU:
2382     case LEU:
2383       if (i != ~((unsigned HOST_WIDE_INT) 0)
2384           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2385         {
2386           *op1 = GEN_INT (i + 1);
2387           return code == GTU ? GEU : LTU;
2388         }
2389       break;
2390
2391     case GEU:
2392     case LTU:
2393       if (i != 0
2394           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2395         {
2396           *op1 = GEN_INT (i - 1);
2397           return code == GEU ? GTU : LEU;
2398         }
2399       break;
2400
2401     default:
2402       gcc_unreachable ();
2403     }
2404
2405   return code;
2406 }
2407
2408
2409 /* Define how to find the value returned by a function.  */
2410
2411 rtx
2412 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2413 {
2414   enum machine_mode mode;
2415   int unsignedp ATTRIBUTE_UNUSED;
2416   rtx r ATTRIBUTE_UNUSED;
2417
2418   mode = TYPE_MODE (type);
2419   /* Promote integer types.  */
2420   if (INTEGRAL_TYPE_P (type))
2421     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2422
2423   /* Promotes small structs returned in a register to full-word size
2424      for big-endian AAPCS.  */
2425   if (arm_return_in_msb (type))
2426     {
2427       HOST_WIDE_INT size = int_size_in_bytes (type);
2428       if (size % UNITS_PER_WORD != 0)
2429         {
2430           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2431           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2432         }
2433     }
2434   
2435   return LIBCALL_VALUE(mode);
2436 }
2437
2438 /* Determine the amount of memory needed to store the possible return 
2439    registers of an untyped call.  */
2440 int
2441 arm_apply_result_size (void)
2442 {
2443   int size = 16;
2444
2445   if (TARGET_ARM)
2446     {
2447       if (TARGET_HARD_FLOAT_ABI)
2448         {
2449           if (TARGET_FPA)
2450             size += 12;
2451           if (TARGET_MAVERICK)
2452             size += 8;
2453         }
2454       if (TARGET_IWMMXT_ABI)
2455         size += 8;
2456     }
2457
2458   return size;
2459 }
2460
2461 /* Decide whether a type should be returned in memory (true)
2462    or in a register (false).  This is called by the macro
2463    RETURN_IN_MEMORY.  */
2464 int
2465 arm_return_in_memory (tree type)
2466 {
2467   HOST_WIDE_INT size;
2468
2469   if (!AGGREGATE_TYPE_P (type) &&
2470       (TREE_CODE (type) != VECTOR_TYPE) &&
2471       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2472     /* All simple types are returned in registers.
2473        For AAPCS, complex types are treated the same as aggregates.  */
2474     return 0;
2475
2476   size = int_size_in_bytes (type);
2477
2478   if (arm_abi != ARM_ABI_APCS)
2479     {
2480       /* ATPCS and later return aggregate types in memory only if they are
2481          larger than a word (or are variable size).  */
2482       return (size < 0 || size > UNITS_PER_WORD);
2483     }
2484
2485   /* To maximize backwards compatibility with previous versions of gcc,
2486      return vectors up to 4 words in registers.  */
2487   if (TREE_CODE (type) == VECTOR_TYPE)
2488     return (size < 0 || size > (4 * UNITS_PER_WORD));
2489
2490   /* For the arm-wince targets we choose to be compatible with Microsoft's
2491      ARM and Thumb compilers, which always return aggregates in memory.  */
2492 #ifndef ARM_WINCE
2493   /* All structures/unions bigger than one word are returned in memory.
2494      Also catch the case where int_size_in_bytes returns -1.  In this case
2495      the aggregate is either huge or of variable size, and in either case
2496      we will want to return it via memory and not in a register.  */
2497   if (size < 0 || size > UNITS_PER_WORD)
2498     return 1;
2499
2500   if (TREE_CODE (type) == RECORD_TYPE)
2501     {
2502       tree field;
2503
2504       /* For a struct the APCS says that we only return in a register
2505          if the type is 'integer like' and every addressable element
2506          has an offset of zero.  For practical purposes this means
2507          that the structure can have at most one non bit-field element
2508          and that this element must be the first one in the structure.  */
2509
2510       /* Find the first field, ignoring non FIELD_DECL things which will
2511          have been created by C++.  */
2512       for (field = TYPE_FIELDS (type);
2513            field && TREE_CODE (field) != FIELD_DECL;
2514            field = TREE_CHAIN (field))
2515         continue;
2516
2517       if (field == NULL)
2518         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2519
2520       /* Check that the first field is valid for returning in a register.  */
2521
2522       /* ... Floats are not allowed */
2523       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2524         return 1;
2525
2526       /* ... Aggregates that are not themselves valid for returning in
2527          a register are not allowed.  */
2528       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2529         return 1;
2530
2531       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2532          since they are not addressable.  */
2533       for (field = TREE_CHAIN (field);
2534            field;
2535            field = TREE_CHAIN (field))
2536         {
2537           if (TREE_CODE (field) != FIELD_DECL)
2538             continue;
2539
2540           if (!DECL_BIT_FIELD_TYPE (field))
2541             return 1;
2542         }
2543
2544       return 0;
2545     }
2546
2547   if (TREE_CODE (type) == UNION_TYPE)
2548     {
2549       tree field;
2550
2551       /* Unions can be returned in registers if every element is
2552          integral, or can be returned in an integer register.  */
2553       for (field = TYPE_FIELDS (type);
2554            field;
2555            field = TREE_CHAIN (field))
2556         {
2557           if (TREE_CODE (field) != FIELD_DECL)
2558             continue;
2559
2560           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2561             return 1;
2562
2563           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2564             return 1;
2565         }
2566
2567       return 0;
2568     }
2569 #endif /* not ARM_WINCE */
2570
2571   /* Return all other types in memory.  */
2572   return 1;
2573 }
2574
2575 /* Indicate whether or not words of a double are in big-endian order.  */
2576
2577 int
2578 arm_float_words_big_endian (void)
2579 {
2580   if (TARGET_MAVERICK)
2581     return 0;
2582
2583   /* For FPA, float words are always big-endian.  For VFP, floats words
2584      follow the memory system mode.  */
2585
2586   if (TARGET_FPA)
2587     {
2588       return 1;
2589     }
2590
2591   if (TARGET_VFP)
2592     return (TARGET_BIG_END ? 1 : 0);
2593
2594   return 1;
2595 }
2596
2597 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2598    for a call to a function whose data type is FNTYPE.
2599    For a library call, FNTYPE is NULL.  */
2600 void
2601 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2602                           rtx libname  ATTRIBUTE_UNUSED,
2603                           tree fndecl ATTRIBUTE_UNUSED)
2604 {
2605   /* On the ARM, the offset starts at 0.  */
2606   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2607   pcum->iwmmxt_nregs = 0;
2608   pcum->can_split = true;
2609
2610   pcum->call_cookie = CALL_NORMAL;
2611
2612   if (TARGET_LONG_CALLS)
2613     pcum->call_cookie = CALL_LONG;
2614
2615   /* Check for long call/short call attributes.  The attributes
2616      override any command line option.  */
2617   if (fntype)
2618     {
2619       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2620         pcum->call_cookie = CALL_SHORT;
2621       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2622         pcum->call_cookie = CALL_LONG;
2623     }
2624
2625   /* Varargs vectors are treated the same as long long.
2626      named_count avoids having to change the way arm handles 'named' */
2627   pcum->named_count = 0;
2628   pcum->nargs = 0;
2629
2630   if (TARGET_REALLY_IWMMXT && fntype)
2631     {
2632       tree fn_arg;
2633
2634       for (fn_arg = TYPE_ARG_TYPES (fntype);
2635            fn_arg;
2636            fn_arg = TREE_CHAIN (fn_arg))
2637         pcum->named_count += 1;
2638
2639       if (! pcum->named_count)
2640         pcum->named_count = INT_MAX;
2641     }
2642 }
2643
2644
2645 /* Return true if mode/type need doubleword alignment.  */
2646 bool
2647 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2648 {
2649   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2650           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2651 }
2652
2653
2654 /* Determine where to put an argument to a function.
2655    Value is zero to push the argument on the stack,
2656    or a hard register in which to store the argument.
2657
2658    MODE is the argument's machine mode.
2659    TYPE is the data type of the argument (as a tree).
2660     This is null for libcalls where that information may
2661     not be available.
2662    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2663     the preceding args and about the function being called.
2664    NAMED is nonzero if this argument is a named parameter
2665     (otherwise it is an extra parameter matching an ellipsis).  */
2666
2667 rtx
2668 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2669                   tree type, int named)
2670 {
2671   int nregs;
2672
2673   /* Varargs vectors are treated the same as long long.
2674      named_count avoids having to change the way arm handles 'named' */
2675   if (TARGET_IWMMXT_ABI
2676       && arm_vector_mode_supported_p (mode)
2677       && pcum->named_count > pcum->nargs + 1)
2678     {
2679       if (pcum->iwmmxt_nregs <= 9)
2680         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2681       else
2682         {
2683           pcum->can_split = false;
2684           return NULL_RTX;
2685         }
2686     }
2687
2688   /* Put doubleword aligned quantities in even register pairs.  */
2689   if (pcum->nregs & 1
2690       && ARM_DOUBLEWORD_ALIGN
2691       && arm_needs_doubleword_align (mode, type))
2692     pcum->nregs++;
2693
2694   if (mode == VOIDmode)
2695     /* Compute operand 2 of the call insn.  */
2696     return GEN_INT (pcum->call_cookie);
2697
2698   /* Only allow splitting an arg between regs and memory if all preceding
2699      args were allocated to regs.  For args passed by reference we only count
2700      the reference pointer.  */
2701   if (pcum->can_split)
2702     nregs = 1;
2703   else
2704     nregs = ARM_NUM_REGS2 (mode, type);
2705
2706   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2707     return NULL_RTX;
2708
2709   return gen_rtx_REG (mode, pcum->nregs);
2710 }
2711
2712 static int
2713 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2714                        tree type, bool named ATTRIBUTE_UNUSED)
2715 {
2716   int nregs = pcum->nregs;
2717
2718   if (arm_vector_mode_supported_p (mode))
2719     return 0;
2720
2721   if (NUM_ARG_REGS > nregs
2722       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2723       && pcum->can_split)
2724     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2725
2726   return 0;
2727 }
2728
2729 /* Variable sized types are passed by reference.  This is a GCC
2730    extension to the ARM ABI.  */
2731
2732 static bool
2733 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2734                        enum machine_mode mode ATTRIBUTE_UNUSED,
2735                        tree type, bool named ATTRIBUTE_UNUSED)
2736 {
2737   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2738 }
2739 \f
2740 /* Encode the current state of the #pragma [no_]long_calls.  */
2741 typedef enum
2742 {
2743   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2744   LONG,         /* #pragma long_calls is in effect.  */
2745   SHORT         /* #pragma no_long_calls is in effect.  */
2746 } arm_pragma_enum;
2747
2748 static arm_pragma_enum arm_pragma_long_calls = OFF;
2749
2750 void
2751 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2752 {
2753   arm_pragma_long_calls = LONG;
2754 }
2755
2756 void
2757 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2758 {
2759   arm_pragma_long_calls = SHORT;
2760 }
2761
2762 void
2763 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2764 {
2765   arm_pragma_long_calls = OFF;
2766 }
2767 \f
2768 /* Table of machine attributes.  */
2769 const struct attribute_spec arm_attribute_table[] =
2770 {
2771   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2772   /* Function calls made to this symbol must be done indirectly, because
2773      it may lie outside of the 26 bit addressing range of a normal function
2774      call.  */
2775   { "long_call",    0, 0, false, true,  true,  NULL },
2776   /* Whereas these functions are always known to reside within the 26 bit
2777      addressing range.  */
2778   { "short_call",   0, 0, false, true,  true,  NULL },
2779   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2780   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2781   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2782   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2783 #ifdef ARM_PE
2784   /* ARM/PE has three new attributes:
2785      interfacearm - ?
2786      dllexport - for exporting a function/variable that will live in a dll
2787      dllimport - for importing a function/variable from a dll
2788
2789      Microsoft allows multiple declspecs in one __declspec, separating
2790      them with spaces.  We do NOT support this.  Instead, use __declspec
2791      multiple times.
2792   */
2793   { "dllimport",    0, 0, true,  false, false, NULL },
2794   { "dllexport",    0, 0, true,  false, false, NULL },
2795   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2796 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2797   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2798   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2799   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2800 #endif
2801   { NULL,           0, 0, false, false, false, NULL }
2802 };
2803
2804 /* Handle an attribute requiring a FUNCTION_DECL;
2805    arguments as in struct attribute_spec.handler.  */
2806 static tree
2807 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2808                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2809 {
2810   if (TREE_CODE (*node) != FUNCTION_DECL)
2811     {
2812       warning (0, "%qs attribute only applies to functions",
2813                IDENTIFIER_POINTER (name));
2814       *no_add_attrs = true;
2815     }
2816
2817   return NULL_TREE;
2818 }
2819
2820 /* Handle an "interrupt" or "isr" attribute;
2821    arguments as in struct attribute_spec.handler.  */
2822 static tree
2823 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2824                           bool *no_add_attrs)
2825 {
2826   if (DECL_P (*node))
2827     {
2828       if (TREE_CODE (*node) != FUNCTION_DECL)
2829         {
2830           warning (0, "%qs attribute only applies to functions",
2831                    IDENTIFIER_POINTER (name));
2832           *no_add_attrs = true;
2833         }
2834       /* FIXME: the argument if any is checked for type attributes;
2835          should it be checked for decl ones?  */
2836     }
2837   else
2838     {
2839       if (TREE_CODE (*node) == FUNCTION_TYPE
2840           || TREE_CODE (*node) == METHOD_TYPE)
2841         {
2842           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2843             {
2844               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2845               *no_add_attrs = true;
2846             }
2847         }
2848       else if (TREE_CODE (*node) == POINTER_TYPE
2849                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2850                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2851                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2852         {
2853           *node = build_variant_type_copy (*node);
2854           TREE_TYPE (*node) = build_type_attribute_variant
2855             (TREE_TYPE (*node),
2856              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2857           *no_add_attrs = true;
2858         }
2859       else
2860         {
2861           /* Possibly pass this attribute on from the type to a decl.  */
2862           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2863                        | (int) ATTR_FLAG_FUNCTION_NEXT
2864                        | (int) ATTR_FLAG_ARRAY_NEXT))
2865             {
2866               *no_add_attrs = true;
2867               return tree_cons (name, args, NULL_TREE);
2868             }
2869           else
2870             {
2871               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2872             }
2873         }
2874     }
2875
2876   return NULL_TREE;
2877 }
2878
2879 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2880 /* Handle the "notshared" attribute.  This attribute is another way of
2881    requesting hidden visibility.  ARM's compiler supports
2882    "__declspec(notshared)"; we support the same thing via an
2883    attribute.  */
2884
2885 static tree
2886 arm_handle_notshared_attribute (tree *node, 
2887                                 tree name ATTRIBUTE_UNUSED, 
2888                                 tree args ATTRIBUTE_UNUSED, 
2889                                 int flags ATTRIBUTE_UNUSED, 
2890                                 bool *no_add_attrs)
2891 {
2892   tree decl = TYPE_NAME (*node);
2893
2894   if (decl)
2895     {
2896       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2897       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2898       *no_add_attrs = false;
2899     }
2900   return NULL_TREE;
2901 }
2902 #endif
2903
2904 /* Return 0 if the attributes for two types are incompatible, 1 if they
2905    are compatible, and 2 if they are nearly compatible (which causes a
2906    warning to be generated).  */
2907 static int
2908 arm_comp_type_attributes (tree type1, tree type2)
2909 {
2910   int l1, l2, s1, s2;
2911
2912   /* Check for mismatch of non-default calling convention.  */
2913   if (TREE_CODE (type1) != FUNCTION_TYPE)
2914     return 1;
2915
2916   /* Check for mismatched call attributes.  */
2917   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2918   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2919   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2920   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2921
2922   /* Only bother to check if an attribute is defined.  */
2923   if (l1 | l2 | s1 | s2)
2924     {
2925       /* If one type has an attribute, the other must have the same attribute.  */
2926       if ((l1 != l2) || (s1 != s2))
2927         return 0;
2928
2929       /* Disallow mixed attributes.  */
2930       if ((l1 & s2) || (l2 & s1))
2931         return 0;
2932     }
2933
2934   /* Check for mismatched ISR attribute.  */
2935   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2936   if (! l1)
2937     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2938   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2939   if (! l2)
2940     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2941   if (l1 != l2)
2942     return 0;
2943
2944   return 1;
2945 }
2946
2947 /*  Encode long_call or short_call attribute by prefixing
2948     symbol name in DECL with a special character FLAG.  */
2949 void
2950 arm_encode_call_attribute (tree decl, int flag)
2951 {
2952   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2953   int          len = strlen (str);
2954   char *       newstr;
2955
2956   /* Do not allow weak functions to be treated as short call.  */
2957   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2958     return;
2959
2960   newstr = alloca (len + 2);
2961   newstr[0] = flag;
2962   strcpy (newstr + 1, str);
2963
2964   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2965   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2966 }
2967
2968 /*  Assigns default attributes to newly defined type.  This is used to
2969     set short_call/long_call attributes for function types of
2970     functions defined inside corresponding #pragma scopes.  */
2971 static void
2972 arm_set_default_type_attributes (tree type)
2973 {
2974   /* Add __attribute__ ((long_call)) to all functions, when
2975      inside #pragma long_calls or __attribute__ ((short_call)),
2976      when inside #pragma no_long_calls.  */
2977   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2978     {
2979       tree type_attr_list, attr_name;
2980       type_attr_list = TYPE_ATTRIBUTES (type);
2981
2982       if (arm_pragma_long_calls == LONG)
2983         attr_name = get_identifier ("long_call");
2984       else if (arm_pragma_long_calls == SHORT)
2985         attr_name = get_identifier ("short_call");
2986       else
2987         return;
2988
2989       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2990       TYPE_ATTRIBUTES (type) = type_attr_list;
2991     }
2992 }
2993 \f
2994 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2995    defined within the current compilation unit.  If this cannot be
2996    determined, then 0 is returned.  */
2997 static int
2998 current_file_function_operand (rtx sym_ref)
2999 {
3000   /* This is a bit of a fib.  A function will have a short call flag
3001      applied to its name if it has the short call attribute, or it has
3002      already been defined within the current compilation unit.  */
3003   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3004     return 1;
3005
3006   /* The current function is always defined within the current compilation
3007      unit.  If it s a weak definition however, then this may not be the real
3008      definition of the function, and so we have to say no.  */
3009   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3010       && !DECL_WEAK (current_function_decl))
3011     return 1;
3012
3013   /* We cannot make the determination - default to returning 0.  */
3014   return 0;
3015 }
3016
3017 /* Return nonzero if a 32 bit "long_call" should be generated for
3018    this call.  We generate a long_call if the function:
3019
3020         a.  has an __attribute__((long call))
3021      or b.  is within the scope of a #pragma long_calls
3022      or c.  the -mlong-calls command line switch has been specified
3023          .  and either:
3024                 1. -ffunction-sections is in effect
3025              or 2. the current function has __attribute__ ((section))
3026              or 3. the target function has __attribute__ ((section))
3027
3028    However we do not generate a long call if the function:
3029
3030         d.  has an __attribute__ ((short_call))
3031      or e.  is inside the scope of a #pragma no_long_calls
3032      or f.  is defined within the current compilation unit.
3033
3034    This function will be called by C fragments contained in the machine
3035    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3036    rtl operands.  CALL_SYMBOL is used to distinguish between
3037    two different callers of the function.  It is set to 1 in the
3038    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3039    and "call_value" patterns.  This is because of the difference in the
3040    SYM_REFs passed by these patterns.  */
3041 int
3042 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3043 {
3044   if (!call_symbol)
3045     {
3046       if (GET_CODE (sym_ref) != MEM)
3047         return 0;
3048
3049       sym_ref = XEXP (sym_ref, 0);
3050     }
3051
3052   if (GET_CODE (sym_ref) != SYMBOL_REF)
3053     return 0;
3054
3055   if (call_cookie & CALL_SHORT)
3056     return 0;
3057
3058   if (TARGET_LONG_CALLS)
3059     {
3060       if (flag_function_sections
3061           || DECL_SECTION_NAME (current_function_decl))
3062         /* c.3 is handled by the definition of the
3063            ARM_DECLARE_FUNCTION_SIZE macro.  */
3064         return 1;
3065     }
3066
3067   if (current_file_function_operand (sym_ref))
3068     return 0;
3069
3070   return (call_cookie & CALL_LONG)
3071     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3072     || TARGET_LONG_CALLS;
3073 }
3074
3075 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3076 static bool
3077 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3078 {
3079   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3080
3081   if (cfun->machine->sibcall_blocked)
3082     return false;
3083
3084   /* Never tailcall something for which we have no decl, or if we
3085      are in Thumb mode.  */
3086   if (decl == NULL || TARGET_THUMB)
3087     return false;
3088
3089   /* Get the calling method.  */
3090   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3091     call_type = CALL_SHORT;
3092   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3093     call_type = CALL_LONG;
3094
3095   /* Cannot tail-call to long calls, since these are out of range of
3096      a branch instruction.  However, if not compiling PIC, we know
3097      we can reach the symbol if it is in this compilation unit.  */
3098   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3099     return false;
3100
3101   /* If we are interworking and the function is not declared static
3102      then we can't tail-call it unless we know that it exists in this
3103      compilation unit (since it might be a Thumb routine).  */
3104   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3105     return false;
3106
3107   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3108   if (IS_INTERRUPT (arm_current_func_type ()))
3109     return false;
3110
3111   /* Everything else is ok.  */
3112   return true;
3113 }
3114
3115 \f
3116 /* Addressing mode support functions.  */
3117
3118 /* Return nonzero if X is a legitimate immediate operand when compiling
3119    for PIC.  */
3120 int
3121 legitimate_pic_operand_p (rtx x)
3122 {
3123   if (CONSTANT_P (x)
3124       && flag_pic
3125       && (GET_CODE (x) == SYMBOL_REF
3126           || (GET_CODE (x) == CONST
3127               && GET_CODE (XEXP (x, 0)) == PLUS
3128               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3129     return 0;
3130
3131   return 1;
3132 }
3133
3134 rtx
3135 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3136 {
3137   if (GET_CODE (orig) == SYMBOL_REF
3138       || GET_CODE (orig) == LABEL_REF)
3139     {
3140 #ifndef AOF_ASSEMBLER
3141       rtx pic_ref, address;
3142 #endif
3143       rtx insn;
3144       int subregs = 0;
3145
3146       if (reg == 0)
3147         {
3148           gcc_assert (!no_new_pseudos);
3149           reg = gen_reg_rtx (Pmode);
3150
3151           subregs = 1;
3152         }
3153
3154 #ifdef AOF_ASSEMBLER
3155       /* The AOF assembler can generate relocations for these directly, and
3156          understands that the PIC register has to be added into the offset.  */
3157       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3158 #else
3159       if (subregs)
3160         address = gen_reg_rtx (Pmode);
3161       else
3162         address = reg;
3163
3164       if (TARGET_ARM)
3165         emit_insn (gen_pic_load_addr_arm (address, orig));
3166       else
3167         emit_insn (gen_pic_load_addr_thumb (address, orig));
3168
3169       if ((GET_CODE (orig) == LABEL_REF
3170            || (GET_CODE (orig) == SYMBOL_REF &&
3171                SYMBOL_REF_LOCAL_P (orig)))
3172           && NEED_GOT_RELOC)
3173         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3174       else
3175         {
3176           pic_ref = gen_const_mem (Pmode,
3177                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3178                                                  address));
3179         }
3180
3181       insn = emit_move_insn (reg, pic_ref);
3182 #endif
3183       current_function_uses_pic_offset_table = 1;
3184       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3185          by loop.  */
3186       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3187                                             REG_NOTES (insn));
3188       return reg;
3189     }
3190   else if (GET_CODE (orig) == CONST)
3191     {
3192       rtx base, offset;
3193
3194       if (GET_CODE (XEXP (orig, 0)) == PLUS
3195           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3196         return orig;
3197
3198       if (reg == 0)
3199         {
3200           gcc_assert (!no_new_pseudos);
3201           reg = gen_reg_rtx (Pmode);
3202         }
3203
3204       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3205       
3206       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3207       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3208                                        base == reg ? 0 : reg);
3209
3210       if (GET_CODE (offset) == CONST_INT)
3211         {
3212           /* The base register doesn't really matter, we only want to
3213              test the index for the appropriate mode.  */
3214           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3215             {
3216               gcc_assert (!no_new_pseudos);
3217               offset = force_reg (Pmode, offset);
3218             }
3219
3220           if (GET_CODE (offset) == CONST_INT)
3221             return plus_constant (base, INTVAL (offset));
3222         }
3223
3224       if (GET_MODE_SIZE (mode) > 4
3225           && (GET_MODE_CLASS (mode) == MODE_INT
3226               || TARGET_SOFT_FLOAT))
3227         {
3228           emit_insn (gen_addsi3 (reg, base, offset));
3229           return reg;
3230         }
3231
3232       return gen_rtx_PLUS (Pmode, base, offset);
3233     }
3234
3235   return orig;
3236 }
3237
3238
3239 /* Find a spare low register to use during the prolog of a function.  */
3240
3241 static int
3242 thumb_find_work_register (unsigned long pushed_regs_mask)
3243 {
3244   int reg;
3245
3246   /* Check the argument registers first as these are call-used.  The
3247      register allocation order means that sometimes r3 might be used
3248      but earlier argument registers might not, so check them all.  */
3249   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3250     if (!regs_ever_live[reg])
3251       return reg;
3252
3253   /* Before going on to check the call-saved registers we can try a couple
3254      more ways of deducing that r3 is available.  The first is when we are
3255      pushing anonymous arguments onto the stack and we have less than 4
3256      registers worth of fixed arguments(*).  In this case r3 will be part of
3257      the variable argument list and so we can be sure that it will be
3258      pushed right at the start of the function.  Hence it will be available
3259      for the rest of the prologue.
3260      (*): ie current_function_pretend_args_size is greater than 0.  */
3261   if (cfun->machine->uses_anonymous_args
3262       && current_function_pretend_args_size > 0)
3263     return LAST_ARG_REGNUM;
3264
3265   /* The other case is when we have fixed arguments but less than 4 registers
3266      worth.  In this case r3 might be used in the body of the function, but
3267      it is not being used to convey an argument into the function.  In theory
3268      we could just check current_function_args_size to see how many bytes are
3269      being passed in argument registers, but it seems that it is unreliable.
3270      Sometimes it will have the value 0 when in fact arguments are being
3271      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3272      check the args_info.nregs field as well.  The problem with this field is
3273      that it makes no allowances for arguments that are passed to the
3274      function but which are not used.  Hence we could miss an opportunity
3275      when a function has an unused argument in r3.  But it is better to be
3276      safe than to be sorry.  */
3277   if (! cfun->machine->uses_anonymous_args
3278       && current_function_args_size >= 0
3279       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3280       && cfun->args_info.nregs < 4)
3281     return LAST_ARG_REGNUM;
3282   
3283   /* Otherwise look for a call-saved register that is going to be pushed.  */
3284   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3285     if (pushed_regs_mask & (1 << reg))
3286       return reg;
3287
3288   /* Something went wrong - thumb_compute_save_reg_mask()
3289      should have arranged for a suitable register to be pushed.  */
3290   gcc_unreachable ();
3291 }
3292
3293
3294 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3295    low register.  */
3296
3297 void
3298 arm_load_pic_register (unsigned int scratch)
3299 {
3300 #ifndef AOF_ASSEMBLER
3301   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3302   rtx global_offset_table;
3303
3304   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3305     return;
3306
3307   gcc_assert (flag_pic);
3308
3309   l1 = gen_label_rtx ();
3310
3311   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3312   /* On the ARM the PC register contains 'dot + 8' at the time of the
3313      addition, on the Thumb it is 'dot + 4'.  */
3314   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3315   if (GOT_PCREL)
3316     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3317                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3318   else
3319     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3320
3321   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3322
3323   if (TARGET_ARM)
3324     {
3325       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3326       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3327     }
3328   else
3329     {
3330       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3331         {
3332           /* We will have pushed the pic register, so should always be
3333              able to find a work register.  */
3334           pic_tmp = gen_rtx_REG (SImode, scratch);
3335           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3336           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3337         }
3338       else
3339         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3340       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3341     }
3342
3343   /* Need to emit this whether or not we obey regdecls,
3344      since setjmp/longjmp can cause life info to screw up.  */
3345   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3346 #endif /* AOF_ASSEMBLER */
3347 }
3348
3349
3350 /* Return nonzero if X is valid as an ARM state addressing register.  */
3351 static int
3352 arm_address_register_rtx_p (rtx x, int strict_p)
3353 {
3354   int regno;
3355
3356   if (GET_CODE (x) != REG)
3357     return 0;
3358
3359   regno = REGNO (x);
3360
3361   if (strict_p)
3362     return ARM_REGNO_OK_FOR_BASE_P (regno);
3363
3364   return (regno <= LAST_ARM_REGNUM
3365           || regno >= FIRST_PSEUDO_REGISTER
3366           || regno == FRAME_POINTER_REGNUM
3367           || regno == ARG_POINTER_REGNUM);
3368 }
3369
3370 /* Return nonzero if X is a valid ARM state address operand.  */
3371 int
3372 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3373                           int strict_p)
3374 {
3375   bool use_ldrd;
3376   enum rtx_code code = GET_CODE (x);
3377
3378   if (arm_address_register_rtx_p (x, strict_p))
3379     return 1;
3380
3381   use_ldrd = (TARGET_LDRD
3382               && (mode == DImode
3383                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3384
3385   if (code == POST_INC || code == PRE_DEC
3386       || ((code == PRE_INC || code == POST_DEC)
3387           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3388     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3389
3390   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3391            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3392            && GET_CODE (XEXP (x, 1)) == PLUS
3393            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3394     {
3395       rtx addend = XEXP (XEXP (x, 1), 1);
3396
3397       /* Don't allow ldrd post increment by register because it's hard
3398          to fixup invalid register choices.  */
3399       if (use_ldrd
3400           && GET_CODE (x) == POST_MODIFY
3401           && GET_CODE (addend) == REG)
3402         return 0;
3403
3404       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3405               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3406     }
3407
3408   /* After reload constants split into minipools will have addresses
3409      from a LABEL_REF.  */
3410   else if (reload_completed
3411            && (code == LABEL_REF
3412                || (code == CONST
3413                    && GET_CODE (XEXP (x, 0)) == PLUS
3414                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3415                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3416     return 1;
3417
3418   else if (mode == TImode)
3419     return 0;
3420
3421   else if (code == PLUS)
3422     {
3423       rtx xop0 = XEXP (x, 0);
3424       rtx xop1 = XEXP (x, 1);
3425
3426       return ((arm_address_register_rtx_p (xop0, strict_p)
3427                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3428               || (arm_address_register_rtx_p (xop1, strict_p)
3429                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3430     }
3431
3432 #if 0
3433   /* Reload currently can't handle MINUS, so disable this for now */
3434   else if (GET_CODE (x) == MINUS)
3435     {
3436       rtx xop0 = XEXP (x, 0);
3437       rtx xop1&nb