OSDN Git Service

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