OSDN Git Service

PR target/23436
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23    Boston, MA 02110-1301, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 static int arm_size_return_regs (void);
77 #ifndef AOF_ASSEMBLER
78 static bool arm_assemble_integer (rtx, unsigned int, int);
79 #endif
80 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
81 static arm_cc get_arm_condition_code (rtx);
82 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
83 static rtx is_jump_table (rtx);
84 static const char *output_multi_immediate (rtx *, const char *, const char *,
85                                            int, HOST_WIDE_INT);
86 static const char *shift_op (rtx, HOST_WIDE_INT *);
87 static struct machine_function *arm_init_machine_status (void);
88 static void thumb_exit (FILE *, int);
89 static rtx is_jump_table (rtx);
90 static HOST_WIDE_INT get_jump_table_size (rtx);
91 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
92 static Mnode *add_minipool_forward_ref (Mfix *);
93 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_backward_ref (Mfix *);
95 static void assign_minipool_offsets (Mfix *);
96 static void arm_print_value (FILE *, rtx);
97 static void dump_minipool (rtx);
98 static int arm_barrier_cost (rtx);
99 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
100 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
101 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
102                                rtx);
103 static void arm_reorg (void);
104 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
105 static int current_file_function_operand (rtx);
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 thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
118 static int arm_comp_type_attributes (tree, 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 int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
146                                   tree, bool);
147
148 #ifdef OBJECT_FORMAT_ELF
149 static void arm_elf_asm_constructor (rtx, int);
150 #endif
151 #ifndef ARM_PE
152 static void arm_encode_section_info (tree, rtx, int);
153 #endif
154
155 static void arm_file_end (void);
156
157 #ifdef AOF_ASSEMBLER
158 static void aof_globalize_label (FILE *, const char *);
159 static void aof_dump_imports (FILE *);
160 static void aof_dump_pic_table (FILE *);
161 static void aof_file_start (void);
162 static void aof_file_end (void);
163 #endif
164 static rtx arm_struct_value_rtx (tree, int);
165 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
166                                         tree, int *, int);
167 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
168                                    enum machine_mode, tree, bool);
169 static bool arm_promote_prototypes (tree);
170 static bool arm_default_short_enums (void);
171 static bool arm_align_anon_bitfield (void);
172 static bool arm_return_in_msb (tree);
173 static bool arm_must_pass_in_stack (enum machine_mode, tree);
174 #ifdef TARGET_UNWIND_INFO
175 static void arm_unwind_emit (FILE *, rtx);
176 static bool arm_output_ttype (rtx);
177 #endif
178
179 static tree arm_cxx_guard_type (void);
180 static bool arm_cxx_guard_mask_bit (void);
181 static tree arm_get_cookie_size (tree);
182 static bool arm_cookie_has_size (void);
183 static bool arm_cxx_cdtor_returns_this (void);
184 static bool arm_cxx_key_method_may_be_inline (void);
185 static void arm_cxx_determine_class_data_visibility (tree);
186 static bool arm_cxx_class_data_always_comdat (void);
187 static bool arm_cxx_use_aeabi_atexit (void);
188 static void arm_init_libfuncs (void);
189 static bool arm_handle_option (size_t, const char *, int);
190 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
191 \f
192 /* Initialize the GCC target structure.  */
193 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
194 #undef  TARGET_MERGE_DECL_ATTRIBUTES
195 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
196 #endif
197
198 #undef  TARGET_ATTRIBUTE_TABLE
199 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
200
201 #undef TARGET_ASM_FILE_END
202 #define TARGET_ASM_FILE_END arm_file_end
203
204 #ifdef AOF_ASSEMBLER
205 #undef  TARGET_ASM_BYTE_OP
206 #define TARGET_ASM_BYTE_OP "\tDCB\t"
207 #undef  TARGET_ASM_ALIGNED_HI_OP
208 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
209 #undef  TARGET_ASM_ALIGNED_SI_OP
210 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
211 #undef TARGET_ASM_GLOBALIZE_LABEL
212 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
213 #undef TARGET_ASM_FILE_START
214 #define TARGET_ASM_FILE_START aof_file_start
215 #undef TARGET_ASM_FILE_END
216 #define TARGET_ASM_FILE_END aof_file_end
217 #else
218 #undef  TARGET_ASM_ALIGNED_SI_OP
219 #define TARGET_ASM_ALIGNED_SI_OP NULL
220 #undef  TARGET_ASM_INTEGER
221 #define TARGET_ASM_INTEGER arm_assemble_integer
222 #endif
223
224 #undef  TARGET_ASM_FUNCTION_PROLOGUE
225 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
226
227 #undef  TARGET_ASM_FUNCTION_EPILOGUE
228 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
229
230 #undef  TARGET_DEFAULT_TARGET_FLAGS
231 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
232 #undef  TARGET_HANDLE_OPTION
233 #define TARGET_HANDLE_OPTION arm_handle_option
234
235 #undef  TARGET_COMP_TYPE_ATTRIBUTES
236 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
237
238 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
239 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
240
241 #undef  TARGET_SCHED_ADJUST_COST
242 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
243
244 #undef TARGET_ENCODE_SECTION_INFO
245 #ifdef ARM_PE
246 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
247 #else
248 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
249 #endif
250
251 #undef  TARGET_STRIP_NAME_ENCODING
252 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
253
254 #undef  TARGET_ASM_INTERNAL_LABEL
255 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
256
257 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
258 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
259
260 #undef  TARGET_ASM_OUTPUT_MI_THUNK
261 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
262 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
263 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
264
265 /* This will be overridden in arm_override_options.  */
266 #undef  TARGET_RTX_COSTS
267 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
268 #undef  TARGET_ADDRESS_COST
269 #define TARGET_ADDRESS_COST arm_address_cost
270
271 #undef TARGET_SHIFT_TRUNCATION_MASK
272 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
273 #undef TARGET_VECTOR_MODE_SUPPORTED_P
274 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
275
276 #undef  TARGET_MACHINE_DEPENDENT_REORG
277 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
278
279 #undef  TARGET_INIT_BUILTINS
280 #define TARGET_INIT_BUILTINS  arm_init_builtins
281 #undef  TARGET_EXPAND_BUILTIN
282 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
283
284 #undef TARGET_INIT_LIBFUNCS
285 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
286
287 #undef TARGET_PROMOTE_FUNCTION_ARGS
288 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
289 #undef TARGET_PROMOTE_FUNCTION_RETURN
290 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
291 #undef TARGET_PROMOTE_PROTOTYPES
292 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
293 #undef TARGET_PASS_BY_REFERENCE
294 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
295 #undef TARGET_ARG_PARTIAL_BYTES
296 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
297
298 #undef TARGET_STRUCT_VALUE_RTX
299 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
300
301 #undef  TARGET_SETUP_INCOMING_VARARGS
302 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
303
304 #undef TARGET_DEFAULT_SHORT_ENUMS
305 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
306
307 #undef TARGET_ALIGN_ANON_BITFIELD
308 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
309
310 #undef TARGET_CXX_GUARD_TYPE
311 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
312
313 #undef TARGET_CXX_GUARD_MASK_BIT
314 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
315
316 #undef TARGET_CXX_GET_COOKIE_SIZE
317 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
318
319 #undef TARGET_CXX_COOKIE_HAS_SIZE
320 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
321
322 #undef TARGET_CXX_CDTOR_RETURNS_THIS
323 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
324
325 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
326 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
327
328 #undef TARGET_CXX_USE_AEABI_ATEXIT
329 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
330
331 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
332 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
333   arm_cxx_determine_class_data_visibility
334
335 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
336 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
337
338 #undef TARGET_RETURN_IN_MSB
339 #define TARGET_RETURN_IN_MSB arm_return_in_msb
340
341 #undef TARGET_MUST_PASS_IN_STACK
342 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
343
344 #ifdef TARGET_UNWIND_INFO
345 #undef TARGET_UNWIND_EMIT
346 #define TARGET_UNWIND_EMIT arm_unwind_emit
347
348 /* EABI unwinding tables use a different format for the typeinfo tables.  */
349 #undef TARGET_ASM_TTYPE
350 #define TARGET_ASM_TTYPE arm_output_ttype
351
352 #undef TARGET_ARM_EABI_UNWINDER
353 #define TARGET_ARM_EABI_UNWINDER true
354 #endif /* TARGET_UNWIND_INFO */
355
356 struct gcc_target targetm = TARGET_INITIALIZER;
357 \f
358 /* Obstack for minipool constant handling.  */
359 static struct obstack minipool_obstack;
360 static char *         minipool_startobj;
361
362 /* The maximum number of insns skipped which
363    will be conditionalised if possible.  */
364 static int max_insns_skipped = 5;
365
366 extern FILE * asm_out_file;
367
368 /* True if we are currently building a constant table.  */
369 int making_const_table;
370
371 /* Define the information needed to generate branch insns.  This is
372    stored from the compare operation.  */
373 rtx arm_compare_op0, arm_compare_op1;
374
375 /* The processor for which instructions should be scheduled.  */
376 enum processor_type arm_tune = arm_none;
377
378 /* Which floating point model to use.  */
379 enum arm_fp_model arm_fp_model;
380
381 /* Which floating point hardware is available.  */
382 enum fputype arm_fpu_arch;
383
384 /* Which floating point hardware to schedule for.  */
385 enum fputype arm_fpu_tune;
386
387 /* Whether to use floating point hardware.  */
388 enum float_abi_type arm_float_abi;
389
390 /* Which ABI to use.  */
391 enum arm_abi_type arm_abi;
392
393 /* Used to parse -mstructure_size_boundary command line option.  */
394 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
395
396 /* Used for Thumb call_via trampolines.  */
397 rtx thumb_call_via_label[14];
398 static int thumb_call_reg_needed;
399
400 /* Bit values used to identify processor capabilities.  */
401 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
402 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
403 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
404 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
405 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
406 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
407 #define FL_THUMB      (1 << 6)        /* Thumb aware */
408 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
409 #define FL_STRONG     (1 << 8)        /* StrongARM */
410 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
411 #define FL_XSCALE     (1 << 10)       /* XScale */
412 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
413 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
414                                          media instructions.  */
415 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
416 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
417                                          Note: ARM6 & 7 derivatives only.  */
418
419 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
420
421 #define FL_FOR_ARCH2    0
422 #define FL_FOR_ARCH3    FL_MODE32
423 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
424 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
425 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
426 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
427 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
428 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
429 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
430 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
431 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
432 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
433 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
434 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
435 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
436
437 /* The bits in this mask specify which
438    instructions we are allowed to generate.  */
439 static unsigned long insn_flags = 0;
440
441 /* The bits in this mask specify which instruction scheduling options should
442    be used.  */
443 static unsigned long tune_flags = 0;
444
445 /* The following are used in the arm.md file as equivalents to bits
446    in the above two flag variables.  */
447
448 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
449 int arm_arch3m = 0;
450
451 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
452 int arm_arch4 = 0;
453
454 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
455 int arm_arch4t = 0;
456
457 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
458 int arm_arch5 = 0;
459
460 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
461 int arm_arch5e = 0;
462
463 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
464 int arm_arch6 = 0;
465
466 /* Nonzero if this chip can benefit from load scheduling.  */
467 int arm_ld_sched = 0;
468
469 /* Nonzero if this chip is a StrongARM.  */
470 int arm_tune_strongarm = 0;
471
472 /* Nonzero if this chip is a Cirrus variant.  */
473 int arm_arch_cirrus = 0;
474
475 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
476 int arm_arch_iwmmxt = 0;
477
478 /* Nonzero if this chip is an XScale.  */
479 int arm_arch_xscale = 0;
480
481 /* Nonzero if tuning for XScale  */
482 int arm_tune_xscale = 0;
483
484 /* Nonzero if we want to tune for stores that access the write-buffer. 
485    This typically means an ARM6 or ARM7 with MMU or MPU.  */
486 int arm_tune_wbuf = 0;
487
488 /* Nonzero if generating Thumb instructions.  */
489 int thumb_code = 0;
490
491 /* Nonzero if we should define __THUMB_INTERWORK__ in the
492    preprocessor.
493    XXX This is a bit of a hack, it's intended to help work around
494    problems in GLD which doesn't understand that armv5t code is
495    interworking clean.  */
496 int arm_cpp_interwork = 0;
497
498 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
499    must report the mode of the memory reference from PRINT_OPERAND to
500    PRINT_OPERAND_ADDRESS.  */
501 enum machine_mode output_memory_reference_mode;
502
503 /* The register number to be used for the PIC offset register.  */
504 int arm_pic_register = INVALID_REGNUM;
505
506 /* Set to 1 when a return insn is output, this means that the epilogue
507    is not needed.  */
508 int return_used_this_function;
509
510 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
511    the next function.  */
512 static int after_arm_reorg = 0;
513
514 /* The maximum number of insns to be used when loading a constant.  */
515 static int arm_constant_limit = 3;
516
517 /* For an explanation of these variables, see final_prescan_insn below.  */
518 int arm_ccfsm_state;
519 enum arm_cond_code arm_current_cc;
520 rtx arm_target_insn;
521 int arm_target_label;
522
523 /* The condition codes of the ARM, and the inverse function.  */
524 static const char * const arm_condition_codes[] =
525 {
526   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
527   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
528 };
529
530 #define streq(string1, string2) (strcmp (string1, string2) == 0)
531 \f
532 /* Initialization code.  */
533
534 struct processors
535 {
536   const char *const name;
537   enum processor_type core;
538   const char *arch;
539   const unsigned long flags;
540   bool (* rtx_costs) (rtx, int, int, int *);
541 };
542
543 /* Not all of these give usefully different compilation alternatives,
544    but there is no simple way of generalizing them.  */
545 static const struct processors all_cores[] =
546 {
547   /* ARM Cores */
548 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
549   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
550 #include "arm-cores.def"
551 #undef ARM_CORE
552   {NULL, arm_none, NULL, 0, NULL}
553 };
554
555 static const struct processors all_architectures[] =
556 {
557   /* ARM Architectures */
558   /* We don't specify rtx_costs here as it will be figured out
559      from the core.  */
560
561   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
562   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
563   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
564   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
565   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
566   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
567      implementations that support it, so we will leave it out for now.  */
568   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
569   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
570   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
571   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
572   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
573   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
574   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
575   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
576   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
577   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
578   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
579   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
580   {NULL, arm_none, NULL, 0 , NULL}
581 };
582
583 struct arm_cpu_select
584 {
585   const char *              string;
586   const char *              name;
587   const struct processors * processors;
588 };
589
590 /* This is a magic structure.  The 'string' field is magically filled in
591    with a pointer to the value specified by the user on the command line
592    assuming that the user has specified such a value.  */
593
594 static struct arm_cpu_select arm_select[] =
595 {
596   /* string       name            processors  */
597   { NULL,       "-mcpu=",       all_cores  },
598   { NULL,       "-march=",      all_architectures },
599   { NULL,       "-mtune=",      all_cores }
600 };
601
602 /* Defines representing the indexes into the above table.  */
603 #define ARM_OPT_SET_CPU 0
604 #define ARM_OPT_SET_ARCH 1
605 #define ARM_OPT_SET_TUNE 2
606
607 /* The name of the proprocessor macro to define for this architecture.  */
608
609 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
610
611 struct fpu_desc
612 {
613   const char * name;
614   enum fputype fpu;
615 };
616
617
618 /* Available values for for -mfpu=.  */
619
620 static const struct fpu_desc all_fpus[] =
621 {
622   {"fpa",       FPUTYPE_FPA},
623   {"fpe2",      FPUTYPE_FPA_EMU2},
624   {"fpe3",      FPUTYPE_FPA_EMU2},
625   {"maverick",  FPUTYPE_MAVERICK},
626   {"vfp",       FPUTYPE_VFP}
627 };
628
629
630 /* Floating point models used by the different hardware.
631    See fputype in arm.h.  */
632
633 static const enum fputype fp_model_for_fpu[] =
634 {
635   /* No FP hardware.  */
636   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
637   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
638   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
639   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
640   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
641   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
642 };
643
644
645 struct float_abi
646 {
647   const char * name;
648   enum float_abi_type abi_type;
649 };
650
651
652 /* Available values for -mfloat-abi=.  */
653
654 static const struct float_abi all_float_abis[] =
655 {
656   {"soft",      ARM_FLOAT_ABI_SOFT},
657   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
658   {"hard",      ARM_FLOAT_ABI_HARD}
659 };
660
661
662 struct abi_name
663 {
664   const char *name;
665   enum arm_abi_type abi_type;
666 };
667
668
669 /* Available values for -mabi=.  */
670
671 static const struct abi_name arm_all_abis[] =
672 {
673   {"apcs-gnu",    ARM_ABI_APCS},
674   {"atpcs",   ARM_ABI_ATPCS},
675   {"aapcs",   ARM_ABI_AAPCS},
676   {"iwmmxt",  ARM_ABI_IWMMXT}
677 };
678
679 /* Return the number of bits set in VALUE.  */
680 static unsigned
681 bit_count (unsigned long value)
682 {
683   unsigned long count = 0;
684
685   while (value)
686     {
687       count++;
688       value &= value - 1;  /* Clear the least-significant set bit.  */
689     }
690
691   return count;
692 }
693
694 /* Set up library functions unique to ARM.  */
695
696 static void
697 arm_init_libfuncs (void)
698 {
699   /* There are no special library functions unless we are using the
700      ARM BPABI.  */
701   if (!TARGET_BPABI)
702     return;
703
704   /* The functions below are described in Section 4 of the "Run-Time
705      ABI for the ARM architecture", Version 1.0.  */
706
707   /* Double-precision floating-point arithmetic.  Table 2.  */
708   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
709   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
710   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
711   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
712   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
713
714   /* Double-precision comparisons.  Table 3.  */
715   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
716   set_optab_libfunc (ne_optab, DFmode, NULL);
717   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
718   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
719   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
720   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
721   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
722
723   /* Single-precision floating-point arithmetic.  Table 4.  */
724   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
725   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
726   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
727   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
728   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
729
730   /* Single-precision comparisons.  Table 5.  */
731   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
732   set_optab_libfunc (ne_optab, SFmode, NULL);
733   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
734   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
735   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
736   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
737   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
738
739   /* Floating-point to integer conversions.  Table 6.  */
740   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
741   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
742   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
743   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
744   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
745   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
746   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
747   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
748
749   /* Conversions between floating types.  Table 7.  */
750   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
751   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
752
753   /* Integer to floating-point conversions.  Table 8.  */
754   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
755   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
756   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
757   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
758   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
759   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
760   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
761   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
762
763   /* Long long.  Table 9.  */
764   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
765   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
766   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
767   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
768   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
769   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
770   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
771   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
772
773   /* Integer (32/32->32) division.  \S 4.3.1.  */
774   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
775   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
776
777   /* The divmod functions are designed so that they can be used for
778      plain division, even though they return both the quotient and the
779      remainder.  The quotient is returned in the usual location (i.e.,
780      r0 for SImode, {r0, r1} for DImode), just as would be expected
781      for an ordinary division routine.  Because the AAPCS calling
782      conventions specify that all of { r0, r1, r2, r3 } are
783      callee-saved registers, there is no need to tell the compiler
784      explicitly that those registers are clobbered by these
785      routines.  */
786   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
787   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
788   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
789   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
790
791   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
792      divmod libcalls instead.  */
793   set_optab_libfunc (smod_optab, DImode, NULL);
794   set_optab_libfunc (umod_optab, DImode, NULL);
795   set_optab_libfunc (smod_optab, SImode, NULL);
796   set_optab_libfunc (umod_optab, SImode, NULL);
797 }
798
799 /* Implement TARGET_HANDLE_OPTION.  */
800
801 static bool
802 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
803 {
804   switch (code)
805     {
806     case OPT_march_:
807       arm_select[1].string = arg;
808       return true;
809
810     case OPT_mcpu_:
811       arm_select[0].string = arg;
812       return true;
813
814     case OPT_mhard_float:
815       target_float_abi_name = "hard";
816       return true;
817
818     case OPT_msoft_float:
819       target_float_abi_name = "soft";
820       return true;
821
822     case OPT_mtune_:
823       arm_select[2].string = arg;
824       return true;
825
826     default:
827       return true;
828     }
829 }
830
831 /* Fix up any incompatible options that the user has specified.
832    This has now turned into a maze.  */
833 void
834 arm_override_options (void)
835 {
836   unsigned i;
837   enum processor_type target_arch_cpu = arm_none;
838
839   /* Set up the flags based on the cpu/architecture selected by the user.  */
840   for (i = ARRAY_SIZE (arm_select); i--;)
841     {
842       struct arm_cpu_select * ptr = arm_select + i;
843
844       if (ptr->string != NULL && ptr->string[0] != '\0')
845         {
846           const struct processors * sel;
847
848           for (sel = ptr->processors; sel->name != NULL; sel++)
849             if (streq (ptr->string, sel->name))
850               {
851                 /* Set the architecture define.  */
852                 if (i != ARM_OPT_SET_TUNE)
853                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
854
855                 /* Determine the processor core for which we should
856                    tune code-generation.  */
857                 if (/* -mcpu= is a sensible default.  */
858                     i == ARM_OPT_SET_CPU
859                     /* -mtune= overrides -mcpu= and -march=.  */
860                     || i == ARM_OPT_SET_TUNE)
861                   arm_tune = (enum processor_type) (sel - ptr->processors);
862
863                 /* Remember the CPU associated with this architecture.
864                    If no other option is used to set the CPU type,
865                    we'll use this to guess the most suitable tuning
866                    options.  */
867                 if (i == ARM_OPT_SET_ARCH)
868                   target_arch_cpu = sel->core;
869                 
870                 if (i != ARM_OPT_SET_TUNE)
871                   {
872                     /* If we have been given an architecture and a processor
873                        make sure that they are compatible.  We only generate
874                        a warning though, and we prefer the CPU over the
875                        architecture.  */
876                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
877                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
878                                ptr->string);
879
880                     insn_flags = sel->flags;
881                   }
882
883                 break;
884               }
885
886           if (sel->name == NULL)
887             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
888         }
889     }
890
891   /* Guess the tuning options from the architecture if necessary.  */
892   if (arm_tune == arm_none)
893     arm_tune = target_arch_cpu;
894
895   /* If the user did not specify a processor, choose one for them.  */
896   if (insn_flags == 0)
897     {
898       const struct processors * sel;
899       unsigned int        sought;
900       enum processor_type cpu;
901
902       cpu = TARGET_CPU_DEFAULT;
903       if (cpu == arm_none)
904         {
905 #ifdef SUBTARGET_CPU_DEFAULT
906           /* Use the subtarget default CPU if none was specified by
907              configure.  */
908           cpu = SUBTARGET_CPU_DEFAULT;
909 #endif
910           /* Default to ARM6.  */
911           if (cpu == arm_none)
912             cpu = arm6;
913         }
914       sel = &all_cores[cpu];
915
916       insn_flags = sel->flags;
917
918       /* Now check to see if the user has specified some command line
919          switch that require certain abilities from the cpu.  */
920       sought = 0;
921
922       if (TARGET_INTERWORK || TARGET_THUMB)
923         {
924           sought |= (FL_THUMB | FL_MODE32);
925
926           /* There are no ARM processors that support both APCS-26 and
927              interworking.  Therefore we force FL_MODE26 to be removed
928              from insn_flags here (if it was set), so that the search
929              below will always be able to find a compatible processor.  */
930           insn_flags &= ~FL_MODE26;
931         }
932
933       if (sought != 0 && ((sought & insn_flags) != sought))
934         {
935           /* Try to locate a CPU type that supports all of the abilities
936              of the default CPU, plus the extra abilities requested by
937              the user.  */
938           for (sel = all_cores; sel->name != NULL; sel++)
939             if ((sel->flags & sought) == (sought | insn_flags))
940               break;
941
942           if (sel->name == NULL)
943             {
944               unsigned current_bit_count = 0;
945               const struct processors * best_fit = NULL;
946
947               /* Ideally we would like to issue an error message here
948                  saying that it was not possible to find a CPU compatible
949                  with the default CPU, but which also supports the command
950                  line options specified by the programmer, and so they
951                  ought to use the -mcpu=<name> command line option to
952                  override the default CPU type.
953
954                  If we cannot find a cpu that has both the
955                  characteristics of the default cpu and the given
956                  command line options we scan the array again looking
957                  for a best match.  */
958               for (sel = all_cores; sel->name != NULL; sel++)
959                 if ((sel->flags & sought) == sought)
960                   {
961                     unsigned count;
962
963                     count = bit_count (sel->flags & insn_flags);
964
965                     if (count >= current_bit_count)
966                       {
967                         best_fit = sel;
968                         current_bit_count = count;
969                       }
970                   }
971
972               gcc_assert (best_fit);
973               sel = best_fit;
974             }
975
976           insn_flags = sel->flags;
977         }
978       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
979       if (arm_tune == arm_none)
980         arm_tune = (enum processor_type) (sel - all_cores);
981     }
982
983   /* The processor for which we should tune should now have been
984      chosen.  */
985   gcc_assert (arm_tune != arm_none);
986
987   tune_flags = all_cores[(int)arm_tune].flags;
988   if (optimize_size)
989     targetm.rtx_costs = arm_size_rtx_costs;
990   else
991     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
992
993   /* Make sure that the processor choice does not conflict with any of the
994      other command line choices.  */
995   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
996     {
997       warning (0, "target CPU does not support interworking" );
998       target_flags &= ~MASK_INTERWORK;
999     }
1000
1001   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1002     {
1003       warning (0, "target CPU does not support THUMB instructions");
1004       target_flags &= ~MASK_THUMB;
1005     }
1006
1007   if (TARGET_APCS_FRAME && TARGET_THUMB)
1008     {
1009       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1010       target_flags &= ~MASK_APCS_FRAME;
1011     }
1012
1013   /* Callee super interworking implies thumb interworking.  Adding
1014      this to the flags here simplifies the logic elsewhere.  */
1015   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1016       target_flags |= MASK_INTERWORK;
1017
1018   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1019      from here where no function is being compiled currently.  */
1020   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1021     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1022
1023   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1024     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1025
1026   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1027     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1028
1029   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1030     {
1031       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1032       target_flags |= MASK_APCS_FRAME;
1033     }
1034
1035   if (TARGET_POKE_FUNCTION_NAME)
1036     target_flags |= MASK_APCS_FRAME;
1037
1038   if (TARGET_APCS_REENT && flag_pic)
1039     error ("-fpic and -mapcs-reent are incompatible");
1040
1041   if (TARGET_APCS_REENT)
1042     warning (0, "APCS reentrant code not supported.  Ignored");
1043
1044   /* If this target is normally configured to use APCS frames, warn if they
1045      are turned off and debugging is turned on.  */
1046   if (TARGET_ARM
1047       && write_symbols != NO_DEBUG
1048       && !TARGET_APCS_FRAME
1049       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1050     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1051
1052   /* If stack checking is disabled, we can use r10 as the PIC register,
1053      which keeps r9 available.  */
1054   if (flag_pic)
1055     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1056
1057   if (TARGET_APCS_FLOAT)
1058     warning (0, "passing floating point arguments in fp regs not yet supported");
1059
1060   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1061   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1062   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1063   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1064   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1065   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1066   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1067   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1068   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1069
1070   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1071   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1072   thumb_code = (TARGET_ARM == 0);
1073   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1074   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1075   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1076
1077   /* V5 code we generate is completely interworking capable, so we turn off
1078      TARGET_INTERWORK here to avoid many tests later on.  */
1079
1080   /* XXX However, we must pass the right pre-processor defines to CPP
1081      or GLD can get confused.  This is a hack.  */
1082   if (TARGET_INTERWORK)
1083     arm_cpp_interwork = 1;
1084
1085   if (arm_arch5)
1086     target_flags &= ~MASK_INTERWORK;
1087
1088   if (target_abi_name)
1089     {
1090       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1091         {
1092           if (streq (arm_all_abis[i].name, target_abi_name))
1093             {
1094               arm_abi = arm_all_abis[i].abi_type;
1095               break;
1096             }
1097         }
1098       if (i == ARRAY_SIZE (arm_all_abis))
1099         error ("invalid ABI option: -mabi=%s", target_abi_name);
1100     }
1101   else
1102     arm_abi = ARM_DEFAULT_ABI;
1103
1104   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1105     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1106
1107   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1108     error ("iwmmxt abi requires an iwmmxt capable cpu");
1109
1110   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1111   if (target_fpu_name == NULL && target_fpe_name != NULL)
1112     {
1113       if (streq (target_fpe_name, "2"))
1114         target_fpu_name = "fpe2";
1115       else if (streq (target_fpe_name, "3"))
1116         target_fpu_name = "fpe3";
1117       else
1118         error ("invalid floating point emulation option: -mfpe=%s",
1119                target_fpe_name);
1120     }
1121   if (target_fpu_name != NULL)
1122     {
1123       /* The user specified a FPU.  */
1124       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1125         {
1126           if (streq (all_fpus[i].name, target_fpu_name))
1127             {
1128               arm_fpu_arch = all_fpus[i].fpu;
1129               arm_fpu_tune = arm_fpu_arch;
1130               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1131               break;
1132             }
1133         }
1134       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1135         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1136     }
1137   else
1138     {
1139 #ifdef FPUTYPE_DEFAULT
1140       /* Use the default if it is specified for this platform.  */
1141       arm_fpu_arch = FPUTYPE_DEFAULT;
1142       arm_fpu_tune = FPUTYPE_DEFAULT;
1143 #else
1144       /* Pick one based on CPU type.  */
1145       /* ??? Some targets assume FPA is the default.
1146       if ((insn_flags & FL_VFP) != 0)
1147         arm_fpu_arch = FPUTYPE_VFP;
1148       else
1149       */
1150       if (arm_arch_cirrus)
1151         arm_fpu_arch = FPUTYPE_MAVERICK;
1152       else
1153         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1154 #endif
1155       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1156         arm_fpu_tune = FPUTYPE_FPA;
1157       else
1158         arm_fpu_tune = arm_fpu_arch;
1159       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1160       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1161     }
1162
1163   if (target_float_abi_name != NULL)
1164     {
1165       /* The user specified a FP ABI.  */
1166       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1167         {
1168           if (streq (all_float_abis[i].name, target_float_abi_name))
1169             {
1170               arm_float_abi = all_float_abis[i].abi_type;
1171               break;
1172             }
1173         }
1174       if (i == ARRAY_SIZE (all_float_abis))
1175         error ("invalid floating point abi: -mfloat-abi=%s",
1176                target_float_abi_name);
1177     }
1178   else
1179     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1180
1181   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1182     sorry ("-mfloat-abi=hard and VFP");
1183
1184   /* If soft-float is specified then don't use FPU.  */
1185   if (TARGET_SOFT_FLOAT)
1186     arm_fpu_arch = FPUTYPE_NONE;
1187
1188   /* For arm2/3 there is no need to do any scheduling if there is only
1189      a floating point emulator, or we are doing software floating-point.  */
1190   if ((TARGET_SOFT_FLOAT
1191        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1192        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1193       && (tune_flags & FL_MODE32) == 0)
1194     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1195
1196   /* Override the default structure alignment for AAPCS ABI.  */
1197   if (arm_abi == ARM_ABI_AAPCS)
1198     arm_structure_size_boundary = 8;
1199
1200   if (structure_size_string != NULL)
1201     {
1202       int size = strtol (structure_size_string, NULL, 0);
1203
1204       if (size == 8 || size == 32
1205           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1206         arm_structure_size_boundary = size;
1207       else
1208         warning (0, "structure size boundary can only be set to %s",
1209                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1210     }
1211
1212   if (arm_pic_register_string != NULL)
1213     {
1214       int pic_register = decode_reg_name (arm_pic_register_string);
1215
1216       if (!flag_pic)
1217         warning (0, "-mpic-register= is useless without -fpic");
1218
1219       /* Prevent the user from choosing an obviously stupid PIC register.  */
1220       else if (pic_register < 0 || call_used_regs[pic_register]
1221                || pic_register == HARD_FRAME_POINTER_REGNUM
1222                || pic_register == STACK_POINTER_REGNUM
1223                || pic_register >= PC_REGNUM)
1224         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1225       else
1226         arm_pic_register = pic_register;
1227     }
1228
1229   if (TARGET_THUMB && flag_schedule_insns)
1230     {
1231       /* Don't warn since it's on by default in -O2.  */
1232       flag_schedule_insns = 0;
1233     }
1234
1235   if (optimize_size)
1236     {
1237       /* There's some dispute as to whether this should be 1 or 2.  However,
1238          experiments seem to show that in pathological cases a setting of
1239          1 degrades less severely than a setting of 2.  This could change if
1240          other parts of the compiler change their behavior.  */
1241       arm_constant_limit = 1;
1242
1243       /* If optimizing for size, bump the number of instructions that we
1244          are prepared to conditionally execute (even on a StrongARM).  */
1245       max_insns_skipped = 6;
1246     }
1247   else
1248     {
1249       /* For processors with load scheduling, it never costs more than
1250          2 cycles to load a constant, and the load scheduler may well
1251          reduce that to 1.  */
1252       if (arm_ld_sched)
1253         arm_constant_limit = 1;
1254
1255       /* On XScale the longer latency of a load makes it more difficult
1256          to achieve a good schedule, so it's faster to synthesize
1257          constants that can be done in two insns.  */
1258       if (arm_tune_xscale)
1259         arm_constant_limit = 2;
1260
1261       /* StrongARM has early execution of branches, so a sequence
1262          that is worth skipping is shorter.  */
1263       if (arm_tune_strongarm)
1264         max_insns_skipped = 3;
1265     }
1266
1267   /* Register global variables with the garbage collector.  */
1268   arm_add_gc_roots ();
1269 }
1270
1271 static void
1272 arm_add_gc_roots (void)
1273 {
1274   gcc_obstack_init(&minipool_obstack);
1275   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1276 }
1277 \f
1278 /* A table of known ARM exception types.
1279    For use with the interrupt function attribute.  */
1280
1281 typedef struct
1282 {
1283   const char *const arg;
1284   const unsigned long return_value;
1285 }
1286 isr_attribute_arg;
1287
1288 static const isr_attribute_arg isr_attribute_args [] =
1289 {
1290   { "IRQ",   ARM_FT_ISR },
1291   { "irq",   ARM_FT_ISR },
1292   { "FIQ",   ARM_FT_FIQ },
1293   { "fiq",   ARM_FT_FIQ },
1294   { "ABORT", ARM_FT_ISR },
1295   { "abort", ARM_FT_ISR },
1296   { "ABORT", ARM_FT_ISR },
1297   { "abort", ARM_FT_ISR },
1298   { "UNDEF", ARM_FT_EXCEPTION },
1299   { "undef", ARM_FT_EXCEPTION },
1300   { "SWI",   ARM_FT_EXCEPTION },
1301   { "swi",   ARM_FT_EXCEPTION },
1302   { NULL,    ARM_FT_NORMAL }
1303 };
1304
1305 /* Returns the (interrupt) function type of the current
1306    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1307
1308 static unsigned long
1309 arm_isr_value (tree argument)
1310 {
1311   const isr_attribute_arg * ptr;
1312   const char *              arg;
1313
1314   /* No argument - default to IRQ.  */
1315   if (argument == NULL_TREE)
1316     return ARM_FT_ISR;
1317
1318   /* Get the value of the argument.  */
1319   if (TREE_VALUE (argument) == NULL_TREE
1320       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1321     return ARM_FT_UNKNOWN;
1322
1323   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1324
1325   /* Check it against the list of known arguments.  */
1326   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1327     if (streq (arg, ptr->arg))
1328       return ptr->return_value;
1329
1330   /* An unrecognized interrupt type.  */
1331   return ARM_FT_UNKNOWN;
1332 }
1333
1334 /* Computes the type of the current function.  */
1335
1336 static unsigned long
1337 arm_compute_func_type (void)
1338 {
1339   unsigned long type = ARM_FT_UNKNOWN;
1340   tree a;
1341   tree attr;
1342
1343   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1344
1345   /* Decide if the current function is volatile.  Such functions
1346      never return, and many memory cycles can be saved by not storing
1347      register values that will never be needed again.  This optimization
1348      was added to speed up context switching in a kernel application.  */
1349   if (optimize > 0
1350       && TREE_NOTHROW (current_function_decl)
1351       && TREE_THIS_VOLATILE (current_function_decl))
1352     type |= ARM_FT_VOLATILE;
1353
1354   if (cfun->static_chain_decl != NULL)
1355     type |= ARM_FT_NESTED;
1356
1357   attr = DECL_ATTRIBUTES (current_function_decl);
1358
1359   a = lookup_attribute ("naked", attr);
1360   if (a != NULL_TREE)
1361     type |= ARM_FT_NAKED;
1362
1363   a = lookup_attribute ("isr", attr);
1364   if (a == NULL_TREE)
1365     a = lookup_attribute ("interrupt", attr);
1366
1367   if (a == NULL_TREE)
1368     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1369   else
1370     type |= arm_isr_value (TREE_VALUE (a));
1371
1372   return type;
1373 }
1374
1375 /* Returns the type of the current function.  */
1376
1377 unsigned long
1378 arm_current_func_type (void)
1379 {
1380   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1381     cfun->machine->func_type = arm_compute_func_type ();
1382
1383   return cfun->machine->func_type;
1384 }
1385 \f
1386 /* Return 1 if it is possible to return using a single instruction.
1387    If SIBLING is non-null, this is a test for a return before a sibling
1388    call.  SIBLING is the call insn, so we can examine its register usage.  */
1389
1390 int
1391 use_return_insn (int iscond, rtx sibling)
1392 {
1393   int regno;
1394   unsigned int func_type;
1395   unsigned long saved_int_regs;
1396   unsigned HOST_WIDE_INT stack_adjust;
1397   arm_stack_offsets *offsets;
1398
1399   /* Never use a return instruction before reload has run.  */
1400   if (!reload_completed)
1401     return 0;
1402
1403   func_type = arm_current_func_type ();
1404
1405   /* Naked functions and volatile functions need special
1406      consideration.  */
1407   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1408     return 0;
1409
1410   /* So do interrupt functions that use the frame pointer.  */
1411   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1412     return 0;
1413
1414   offsets = arm_get_frame_offsets ();
1415   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1416
1417   /* As do variadic functions.  */
1418   if (current_function_pretend_args_size
1419       || cfun->machine->uses_anonymous_args
1420       /* Or if the function calls __builtin_eh_return () */
1421       || current_function_calls_eh_return
1422       /* Or if the function calls alloca */
1423       || current_function_calls_alloca
1424       /* Or if there is a stack adjustment.  However, if the stack pointer
1425          is saved on the stack, we can use a pre-incrementing stack load.  */
1426       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1427     return 0;
1428
1429   saved_int_regs = arm_compute_save_reg_mask ();
1430
1431   /* Unfortunately, the insn
1432
1433        ldmib sp, {..., sp, ...}
1434
1435      triggers a bug on most SA-110 based devices, such that the stack
1436      pointer won't be correctly restored if the instruction takes a
1437      page fault.  We work around this problem by popping r3 along with
1438      the other registers, since that is never slower than executing
1439      another instruction.
1440
1441      We test for !arm_arch5 here, because code for any architecture
1442      less than this could potentially be run on one of the buggy
1443      chips.  */
1444   if (stack_adjust == 4 && !arm_arch5)
1445     {
1446       /* Validate that r3 is a call-clobbered register (always true in
1447          the default abi) ...  */
1448       if (!call_used_regs[3])
1449         return 0;
1450
1451       /* ... that it isn't being used for a return value ... */
1452       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1453         return 0;
1454
1455       /* ... or for a tail-call argument ...  */
1456       if (sibling)
1457         {
1458           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1459
1460           if (find_regno_fusage (sibling, USE, 3))
1461             return 0;
1462         }
1463
1464       /* ... and that there are no call-saved registers in r0-r2
1465          (always true in the default ABI).  */
1466       if (saved_int_regs & 0x7)
1467         return 0;
1468     }
1469
1470   /* Can't be done if interworking with Thumb, and any registers have been
1471      stacked.  */
1472   if (TARGET_INTERWORK && saved_int_regs != 0)
1473     return 0;
1474
1475   /* On StrongARM, conditional returns are expensive if they aren't
1476      taken and multiple registers have been stacked.  */
1477   if (iscond && arm_tune_strongarm)
1478     {
1479       /* Conditional return when just the LR is stored is a simple
1480          conditional-load instruction, that's not expensive.  */
1481       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1482         return 0;
1483
1484       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1485         return 0;
1486     }
1487
1488   /* If there are saved registers but the LR isn't saved, then we need
1489      two instructions for the return.  */
1490   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1491     return 0;
1492
1493   /* Can't be done if any of the FPA regs are pushed,
1494      since this also requires an insn.  */
1495   if (TARGET_HARD_FLOAT && TARGET_FPA)
1496     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1497       if (regs_ever_live[regno] && !call_used_regs[regno])
1498         return 0;
1499
1500   /* Likewise VFP regs.  */
1501   if (TARGET_HARD_FLOAT && TARGET_VFP)
1502     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1503       if (regs_ever_live[regno] && !call_used_regs[regno])
1504         return 0;
1505
1506   if (TARGET_REALLY_IWMMXT)
1507     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1508       if (regs_ever_live[regno] && ! call_used_regs [regno])
1509         return 0;
1510
1511   return 1;
1512 }
1513
1514 /* Return TRUE if int I is a valid immediate ARM constant.  */
1515
1516 int
1517 const_ok_for_arm (HOST_WIDE_INT i)
1518 {
1519   int lowbit;
1520   
1521   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1522      be all zero, or all one.  */
1523   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1524       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1525           != ((~(unsigned HOST_WIDE_INT) 0)
1526               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1527     return FALSE;
1528
1529   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1530   
1531   /* Fast return for 0 and small values.  We must do this for zero, since
1532      the code below can't handle that one case.  */
1533   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1534     return TRUE;
1535
1536   /* Get the number of trailing zeros, rounded down to the nearest even
1537      number.  */
1538   lowbit = (ffs ((int) i) - 1) & ~1;
1539
1540   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1541     return TRUE;
1542   else if (lowbit <= 4
1543            && ((i & ~0xc000003f) == 0
1544                || (i & ~0xf000000f) == 0
1545                || (i & ~0xfc000003) == 0))
1546     return TRUE;
1547
1548   return FALSE;
1549 }
1550
1551 /* Return true if I is a valid constant for the operation CODE.  */
1552 static int
1553 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1554 {
1555   if (const_ok_for_arm (i))
1556     return 1;
1557
1558   switch (code)
1559     {
1560     case PLUS:
1561       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1562
1563     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1564     case XOR:
1565     case IOR:
1566       return 0;
1567
1568     case AND:
1569       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1570
1571     default:
1572       gcc_unreachable ();
1573     }
1574 }
1575
1576 /* Emit a sequence of insns to handle a large constant.
1577    CODE is the code of the operation required, it can be any of SET, PLUS,
1578    IOR, AND, XOR, MINUS;
1579    MODE is the mode in which the operation is being performed;
1580    VAL is the integer to operate on;
1581    SOURCE is the other operand (a register, or a null-pointer for SET);
1582    SUBTARGETS means it is safe to create scratch registers if that will
1583    either produce a simpler sequence, or we will want to cse the values.
1584    Return value is the number of insns emitted.  */
1585
1586 int
1587 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1588                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1589 {
1590   rtx cond;
1591
1592   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1593     cond = COND_EXEC_TEST (PATTERN (insn));
1594   else
1595     cond = NULL_RTX;
1596
1597   if (subtargets || code == SET
1598       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1599           && REGNO (target) != REGNO (source)))
1600     {
1601       /* After arm_reorg has been called, we can't fix up expensive
1602          constants by pushing them into memory so we must synthesize
1603          them in-line, regardless of the cost.  This is only likely to
1604          be more costly on chips that have load delay slots and we are
1605          compiling without running the scheduler (so no splitting
1606          occurred before the final instruction emission).
1607
1608          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1609       */
1610       if (!after_arm_reorg
1611           && !cond
1612           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1613                                 1, 0)
1614               > arm_constant_limit + (code != SET)))
1615         {
1616           if (code == SET)
1617             {
1618               /* Currently SET is the only monadic value for CODE, all
1619                  the rest are diadic.  */
1620               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1621               return 1;
1622             }
1623           else
1624             {
1625               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1626
1627               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1628               /* For MINUS, the value is subtracted from, since we never
1629                  have subtraction of a constant.  */
1630               if (code == MINUS)
1631                 emit_insn (gen_rtx_SET (VOIDmode, target,
1632                                         gen_rtx_MINUS (mode, temp, source)));
1633               else
1634                 emit_insn (gen_rtx_SET (VOIDmode, target,
1635                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1636               return 2;
1637             }
1638         }
1639     }
1640
1641   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1642                            1);
1643 }
1644
1645 static int
1646 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1647 {
1648   HOST_WIDE_INT temp1;
1649   int num_insns = 0;
1650   do
1651     {
1652       int end;
1653
1654       if (i <= 0)
1655         i += 32;
1656       if (remainder & (3 << (i - 2)))
1657         {
1658           end = i - 8;
1659           if (end < 0)
1660             end += 32;
1661           temp1 = remainder & ((0x0ff << end)
1662                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1663           remainder &= ~temp1;
1664           num_insns++;
1665           i -= 6;
1666         }
1667       i -= 2;
1668     } while (remainder);
1669   return num_insns;
1670 }
1671
1672 /* Emit an instruction with the indicated PATTERN.  If COND is
1673    non-NULL, conditionalize the execution of the instruction on COND
1674    being true.  */
1675
1676 static void
1677 emit_constant_insn (rtx cond, rtx pattern)
1678 {
1679   if (cond)
1680     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1681   emit_insn (pattern);
1682 }
1683
1684 /* As above, but extra parameter GENERATE which, if clear, suppresses
1685    RTL generation.  */
1686
1687 static int
1688 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1689                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1690                   int generate)
1691 {
1692   int can_invert = 0;
1693   int can_negate = 0;
1694   int can_negate_initial = 0;
1695   int can_shift = 0;
1696   int i;
1697   int num_bits_set = 0;
1698   int set_sign_bit_copies = 0;
1699   int clear_sign_bit_copies = 0;
1700   int clear_zero_bit_copies = 0;
1701   int set_zero_bit_copies = 0;
1702   int insns = 0;
1703   unsigned HOST_WIDE_INT temp1, temp2;
1704   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1705
1706   /* Find out which operations are safe for a given CODE.  Also do a quick
1707      check for degenerate cases; these can occur when DImode operations
1708      are split.  */
1709   switch (code)
1710     {
1711     case SET:
1712       can_invert = 1;
1713       can_shift = 1;
1714       can_negate = 1;
1715       break;
1716
1717     case PLUS:
1718       can_negate = 1;
1719       can_negate_initial = 1;
1720       break;
1721
1722     case IOR:
1723       if (remainder == 0xffffffff)
1724         {
1725           if (generate)
1726             emit_constant_insn (cond,
1727                                 gen_rtx_SET (VOIDmode, target,
1728                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1729           return 1;
1730         }
1731       if (remainder == 0)
1732         {
1733           if (reload_completed && rtx_equal_p (target, source))
1734             return 0;
1735           if (generate)
1736             emit_constant_insn (cond,
1737                                 gen_rtx_SET (VOIDmode, target, source));
1738           return 1;
1739         }
1740       break;
1741
1742     case AND:
1743       if (remainder == 0)
1744         {
1745           if (generate)
1746             emit_constant_insn (cond,
1747                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1748           return 1;
1749         }
1750       if (remainder == 0xffffffff)
1751         {
1752           if (reload_completed && rtx_equal_p (target, source))
1753             return 0;
1754           if (generate)
1755             emit_constant_insn (cond,
1756                                 gen_rtx_SET (VOIDmode, target, source));
1757           return 1;
1758         }
1759       can_invert = 1;
1760       break;
1761
1762     case XOR:
1763       if (remainder == 0)
1764         {
1765           if (reload_completed && rtx_equal_p (target, source))
1766             return 0;
1767           if (generate)
1768             emit_constant_insn (cond,
1769                                 gen_rtx_SET (VOIDmode, target, source));
1770           return 1;
1771         }
1772       
1773       /* We don't know how to handle other cases yet.  */
1774       gcc_assert (remainder == 0xffffffff);
1775       
1776       if (generate)
1777         emit_constant_insn (cond,
1778                             gen_rtx_SET (VOIDmode, target,
1779                                          gen_rtx_NOT (mode, source)));
1780       return 1;
1781
1782     case MINUS:
1783       /* We treat MINUS as (val - source), since (source - val) is always
1784          passed as (source + (-val)).  */
1785       if (remainder == 0)
1786         {
1787           if (generate)
1788             emit_constant_insn (cond,
1789                                 gen_rtx_SET (VOIDmode, target,
1790                                              gen_rtx_NEG (mode, source)));
1791           return 1;
1792         }
1793       if (const_ok_for_arm (val))
1794         {
1795           if (generate)
1796             emit_constant_insn (cond,
1797                                 gen_rtx_SET (VOIDmode, target,
1798                                              gen_rtx_MINUS (mode, GEN_INT (val),
1799                                                             source)));
1800           return 1;
1801         }
1802       can_negate = 1;
1803
1804       break;
1805
1806     default:
1807       gcc_unreachable ();
1808     }
1809
1810   /* If we can do it in one insn get out quickly.  */
1811   if (const_ok_for_arm (val)
1812       || (can_negate_initial && const_ok_for_arm (-val))
1813       || (can_invert && const_ok_for_arm (~val)))
1814     {
1815       if (generate)
1816         emit_constant_insn (cond,
1817                             gen_rtx_SET (VOIDmode, target,
1818                                          (source
1819                                           ? gen_rtx_fmt_ee (code, mode, source,
1820                                                             GEN_INT (val))
1821                                           : GEN_INT (val))));
1822       return 1;
1823     }
1824
1825   /* Calculate a few attributes that may be useful for specific
1826      optimizations.  */
1827   for (i = 31; i >= 0; i--)
1828     {
1829       if ((remainder & (1 << i)) == 0)
1830         clear_sign_bit_copies++;
1831       else
1832         break;
1833     }
1834
1835   for (i = 31; i >= 0; i--)
1836     {
1837       if ((remainder & (1 << i)) != 0)
1838         set_sign_bit_copies++;
1839       else
1840         break;
1841     }
1842
1843   for (i = 0; i <= 31; i++)
1844     {
1845       if ((remainder & (1 << i)) == 0)
1846         clear_zero_bit_copies++;
1847       else
1848         break;
1849     }
1850
1851   for (i = 0; i <= 31; i++)
1852     {
1853       if ((remainder & (1 << i)) != 0)
1854         set_zero_bit_copies++;
1855       else
1856         break;
1857     }
1858
1859   switch (code)
1860     {
1861     case SET:
1862       /* See if we can do this by sign_extending a constant that is known
1863          to be negative.  This is a good, way of doing it, since the shift
1864          may well merge into a subsequent insn.  */
1865       if (set_sign_bit_copies > 1)
1866         {
1867           if (const_ok_for_arm
1868               (temp1 = ARM_SIGN_EXTEND (remainder
1869                                         << (set_sign_bit_copies - 1))))
1870             {
1871               if (generate)
1872                 {
1873                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1874                   emit_constant_insn (cond,
1875                                       gen_rtx_SET (VOIDmode, new_src,
1876                                                    GEN_INT (temp1)));
1877                   emit_constant_insn (cond,
1878                                       gen_ashrsi3 (target, new_src,
1879                                                    GEN_INT (set_sign_bit_copies - 1)));
1880                 }
1881               return 2;
1882             }
1883           /* For an inverted constant, we will need to set the low bits,
1884              these will be shifted out of harm's way.  */
1885           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1886           if (const_ok_for_arm (~temp1))
1887             {
1888               if (generate)
1889                 {
1890                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1891                   emit_constant_insn (cond,
1892                                       gen_rtx_SET (VOIDmode, new_src,
1893                                                    GEN_INT (temp1)));
1894                   emit_constant_insn (cond,
1895                                       gen_ashrsi3 (target, new_src,
1896                                                    GEN_INT (set_sign_bit_copies - 1)));
1897                 }
1898               return 2;
1899             }
1900         }
1901
1902       /* See if we can calculate the value as the difference between two
1903          valid immediates.  */
1904       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1905         {
1906           int topshift = clear_sign_bit_copies & ~1;
1907
1908           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1909                                    & (0xff000000 >> topshift));
1910
1911           /* If temp1 is zero, then that means the 9 most significant
1912              bits of remainder were 1 and we've caused it to overflow.
1913              When topshift is 0 we don't need to do anything since we
1914              can borrow from 'bit 32'.  */
1915           if (temp1 == 0 && topshift != 0)
1916             temp1 = 0x80000000 >> (topshift - 1);
1917
1918           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1919           
1920           if (const_ok_for_arm (temp2))
1921             {
1922               if (generate)
1923                 {
1924                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1925                   emit_constant_insn (cond,
1926                                       gen_rtx_SET (VOIDmode, new_src,
1927                                                    GEN_INT (temp1)));
1928                   emit_constant_insn (cond,
1929                                       gen_addsi3 (target, new_src,
1930                                                   GEN_INT (-temp2)));
1931                 }
1932
1933               return 2;
1934             }
1935         }
1936
1937       /* See if we can generate this by setting the bottom (or the top)
1938          16 bits, and then shifting these into the other half of the
1939          word.  We only look for the simplest cases, to do more would cost
1940          too much.  Be careful, however, not to generate this when the
1941          alternative would take fewer insns.  */
1942       if (val & 0xffff0000)
1943         {
1944           temp1 = remainder & 0xffff0000;
1945           temp2 = remainder & 0x0000ffff;
1946
1947           /* Overlaps outside this range are best done using other methods.  */
1948           for (i = 9; i < 24; i++)
1949             {
1950               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1951                   && !const_ok_for_arm (temp2))
1952                 {
1953                   rtx new_src = (subtargets
1954                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1955                                  : target);
1956                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1957                                             source, subtargets, generate);
1958                   source = new_src;
1959                   if (generate)
1960                     emit_constant_insn
1961                       (cond,
1962                        gen_rtx_SET
1963                        (VOIDmode, target,
1964                         gen_rtx_IOR (mode,
1965                                      gen_rtx_ASHIFT (mode, source,
1966                                                      GEN_INT (i)),
1967                                      source)));
1968                   return insns + 1;
1969                 }
1970             }
1971
1972           /* Don't duplicate cases already considered.  */
1973           for (i = 17; i < 24; i++)
1974             {
1975               if (((temp1 | (temp1 >> i)) == remainder)
1976                   && !const_ok_for_arm (temp1))
1977                 {
1978                   rtx new_src = (subtargets
1979                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1980                                  : target);
1981                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1982                                             source, subtargets, generate);
1983                   source = new_src;
1984                   if (generate)
1985                     emit_constant_insn
1986                       (cond,
1987                        gen_rtx_SET (VOIDmode, target,
1988                                     gen_rtx_IOR
1989                                     (mode,
1990                                      gen_rtx_LSHIFTRT (mode, source,
1991                                                        GEN_INT (i)),
1992                                      source)));
1993                   return insns + 1;
1994                 }
1995             }
1996         }
1997       break;
1998
1999     case IOR:
2000     case XOR:
2001       /* If we have IOR or XOR, and the constant can be loaded in a
2002          single instruction, and we can find a temporary to put it in,
2003          then this can be done in two instructions instead of 3-4.  */
2004       if (subtargets
2005           /* TARGET can't be NULL if SUBTARGETS is 0 */
2006           || (reload_completed && !reg_mentioned_p (target, source)))
2007         {
2008           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2009             {
2010               if (generate)
2011                 {
2012                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2013
2014                   emit_constant_insn (cond,
2015                                       gen_rtx_SET (VOIDmode, sub,
2016                                                    GEN_INT (val)));
2017                   emit_constant_insn (cond,
2018                                       gen_rtx_SET (VOIDmode, target,
2019                                                    gen_rtx_fmt_ee (code, mode,
2020                                                                    source, sub)));
2021                 }
2022               return 2;
2023             }
2024         }
2025
2026       if (code == XOR)
2027         break;
2028
2029       if (set_sign_bit_copies > 8
2030           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2031         {
2032           if (generate)
2033             {
2034               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2035               rtx shift = GEN_INT (set_sign_bit_copies);
2036
2037               emit_constant_insn
2038                 (cond,
2039                  gen_rtx_SET (VOIDmode, sub,
2040                               gen_rtx_NOT (mode,
2041                                            gen_rtx_ASHIFT (mode,
2042                                                            source,
2043                                                            shift))));
2044               emit_constant_insn
2045                 (cond,
2046                  gen_rtx_SET (VOIDmode, target,
2047                               gen_rtx_NOT (mode,
2048                                            gen_rtx_LSHIFTRT (mode, sub,
2049                                                              shift))));
2050             }
2051           return 2;
2052         }
2053
2054       if (set_zero_bit_copies > 8
2055           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2056         {
2057           if (generate)
2058             {
2059               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2060               rtx shift = GEN_INT (set_zero_bit_copies);
2061
2062               emit_constant_insn
2063                 (cond,
2064                  gen_rtx_SET (VOIDmode, sub,
2065                               gen_rtx_NOT (mode,
2066                                            gen_rtx_LSHIFTRT (mode,
2067                                                              source,
2068                                                              shift))));
2069               emit_constant_insn
2070                 (cond,
2071                  gen_rtx_SET (VOIDmode, target,
2072                               gen_rtx_NOT (mode,
2073                                            gen_rtx_ASHIFT (mode, sub,
2074                                                            shift))));
2075             }
2076           return 2;
2077         }
2078
2079       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2080         {
2081           if (generate)
2082             {
2083               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2084               emit_constant_insn (cond,
2085                                   gen_rtx_SET (VOIDmode, sub,
2086                                                gen_rtx_NOT (mode, source)));
2087               source = sub;
2088               if (subtargets)
2089                 sub = gen_reg_rtx (mode);
2090               emit_constant_insn (cond,
2091                                   gen_rtx_SET (VOIDmode, sub,
2092                                                gen_rtx_AND (mode, source,
2093                                                             GEN_INT (temp1))));
2094               emit_constant_insn (cond,
2095                                   gen_rtx_SET (VOIDmode, target,
2096                                                gen_rtx_NOT (mode, sub)));
2097             }
2098           return 3;
2099         }
2100       break;
2101
2102     case AND:
2103       /* See if two shifts will do 2 or more insn's worth of work.  */
2104       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2105         {
2106           HOST_WIDE_INT shift_mask = ((0xffffffff
2107                                        << (32 - clear_sign_bit_copies))
2108                                       & 0xffffffff);
2109
2110           if ((remainder | shift_mask) != 0xffffffff)
2111             {
2112               if (generate)
2113                 {
2114                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2115                   insns = arm_gen_constant (AND, mode, cond,
2116                                             remainder | shift_mask,
2117                                             new_src, source, subtargets, 1);
2118                   source = new_src;
2119                 }
2120               else
2121                 {
2122                   rtx targ = subtargets ? NULL_RTX : target;
2123                   insns = arm_gen_constant (AND, mode, cond,
2124                                             remainder | shift_mask,
2125                                             targ, source, subtargets, 0);
2126                 }
2127             }
2128
2129           if (generate)
2130             {
2131               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2132               rtx shift = GEN_INT (clear_sign_bit_copies);
2133
2134               emit_insn (gen_ashlsi3 (new_src, source, shift));
2135               emit_insn (gen_lshrsi3 (target, new_src, shift));
2136             }
2137
2138           return insns + 2;
2139         }
2140
2141       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2142         {
2143           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2144
2145           if ((remainder | shift_mask) != 0xffffffff)
2146             {
2147               if (generate)
2148                 {
2149                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2150
2151                   insns = arm_gen_constant (AND, mode, cond,
2152                                             remainder | shift_mask,
2153                                             new_src, source, subtargets, 1);
2154                   source = new_src;
2155                 }
2156               else
2157                 {
2158                   rtx targ = subtargets ? NULL_RTX : target;
2159
2160                   insns = arm_gen_constant (AND, mode, cond,
2161                                             remainder | shift_mask,
2162                                             targ, source, subtargets, 0);
2163                 }
2164             }
2165
2166           if (generate)
2167             {
2168               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2169               rtx shift = GEN_INT (clear_zero_bit_copies);
2170
2171               emit_insn (gen_lshrsi3 (new_src, source, shift));
2172               emit_insn (gen_ashlsi3 (target, new_src, shift));
2173             }
2174
2175           return insns + 2;
2176         }
2177
2178       break;
2179
2180     default:
2181       break;
2182     }
2183
2184   for (i = 0; i < 32; i++)
2185     if (remainder & (1 << i))
2186       num_bits_set++;
2187
2188   if (code == AND || (can_invert && num_bits_set > 16))
2189     remainder = (~remainder) & 0xffffffff;
2190   else if (code == PLUS && num_bits_set > 16)
2191     remainder = (-remainder) & 0xffffffff;
2192   else
2193     {
2194       can_invert = 0;
2195       can_negate = 0;
2196     }
2197
2198   /* Now try and find a way of doing the job in either two or three
2199      instructions.
2200      We start by looking for the largest block of zeros that are aligned on
2201      a 2-bit boundary, we then fill up the temps, wrapping around to the
2202      top of the word when we drop off the bottom.
2203      In the worst case this code should produce no more than four insns.  */
2204   {
2205     int best_start = 0;
2206     int best_consecutive_zeros = 0;
2207
2208     for (i = 0; i < 32; i += 2)
2209       {
2210         int consecutive_zeros = 0;
2211
2212         if (!(remainder & (3 << i)))
2213           {
2214             while ((i < 32) && !(remainder & (3 << i)))
2215               {
2216                 consecutive_zeros += 2;
2217                 i += 2;
2218               }
2219             if (consecutive_zeros > best_consecutive_zeros)
2220               {
2221                 best_consecutive_zeros = consecutive_zeros;
2222                 best_start = i - consecutive_zeros;
2223               }
2224             i -= 2;
2225           }
2226       }
2227
2228     /* So long as it won't require any more insns to do so, it's
2229        desirable to emit a small constant (in bits 0...9) in the last
2230        insn.  This way there is more chance that it can be combined with
2231        a later addressing insn to form a pre-indexed load or store
2232        operation.  Consider:
2233
2234                *((volatile int *)0xe0000100) = 1;
2235                *((volatile int *)0xe0000110) = 2;
2236
2237        We want this to wind up as:
2238
2239                 mov rA, #0xe0000000
2240                 mov rB, #1
2241                 str rB, [rA, #0x100]
2242                 mov rB, #2
2243                 str rB, [rA, #0x110]
2244
2245        rather than having to synthesize both large constants from scratch.
2246
2247        Therefore, we calculate how many insns would be required to emit
2248        the constant starting from `best_start', and also starting from
2249        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2250        yield a shorter sequence, we may as well use zero.  */
2251     if (best_start != 0
2252         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2253         && (count_insns_for_constant (remainder, 0) <=
2254             count_insns_for_constant (remainder, best_start)))
2255       best_start = 0;
2256
2257     /* Now start emitting the insns.  */
2258     i = best_start;
2259     do
2260       {
2261         int end;
2262
2263         if (i <= 0)
2264           i += 32;
2265         if (remainder & (3 << (i - 2)))
2266           {
2267             end = i - 8;
2268             if (end < 0)
2269               end += 32;
2270             temp1 = remainder & ((0x0ff << end)
2271                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2272             remainder &= ~temp1;
2273
2274             if (generate)
2275               {
2276                 rtx new_src, temp1_rtx;
2277
2278                 if (code == SET || code == MINUS)
2279                   {
2280                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2281                     if (can_invert && code != MINUS)
2282                       temp1 = ~temp1;
2283                   }
2284                 else
2285                   {
2286                     if (remainder && subtargets)
2287                       new_src = gen_reg_rtx (mode);
2288                     else
2289                       new_src = target;
2290                     if (can_invert)
2291                       temp1 = ~temp1;
2292                     else if (can_negate)
2293                       temp1 = -temp1;
2294                   }
2295
2296                 temp1 = trunc_int_for_mode (temp1, mode);
2297                 temp1_rtx = GEN_INT (temp1);
2298
2299                 if (code == SET)
2300                   ;
2301                 else if (code == MINUS)
2302                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2303                 else
2304                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2305
2306                 emit_constant_insn (cond,
2307                                     gen_rtx_SET (VOIDmode, new_src,
2308                                                  temp1_rtx));
2309                 source = new_src;
2310               }
2311
2312             if (code == SET)
2313               {
2314                 can_invert = 0;
2315                 code = PLUS;
2316               }
2317             else if (code == MINUS)
2318               code = PLUS;
2319
2320             insns++;
2321             i -= 6;
2322           }
2323         i -= 2;
2324       }
2325     while (remainder);
2326   }
2327
2328   return insns;
2329 }
2330
2331 /* Canonicalize a comparison so that we are more likely to recognize it.
2332    This can be done for a few constant compares, where we can make the
2333    immediate value easier to load.  */
2334
2335 enum rtx_code
2336 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2337                              rtx * op1)
2338 {
2339   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2340   unsigned HOST_WIDE_INT maxval;
2341   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2342
2343   switch (code)
2344     {
2345     case EQ:
2346     case NE:
2347       return code;
2348
2349     case GT:
2350     case LE:
2351       if (i != maxval
2352           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2353         {
2354           *op1 = GEN_INT (i + 1);
2355           return code == GT ? GE : LT;
2356         }
2357       break;
2358
2359     case GE:
2360     case LT:
2361       if (i != ~maxval
2362           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2363         {
2364           *op1 = GEN_INT (i - 1);
2365           return code == GE ? GT : LE;
2366         }
2367       break;
2368
2369     case GTU:
2370     case LEU:
2371       if (i != ~((unsigned HOST_WIDE_INT) 0)
2372           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2373         {
2374           *op1 = GEN_INT (i + 1);
2375           return code == GTU ? GEU : LTU;
2376         }
2377       break;
2378
2379     case GEU:
2380     case LTU:
2381       if (i != 0
2382           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2383         {
2384           *op1 = GEN_INT (i - 1);
2385           return code == GEU ? GTU : LEU;
2386         }
2387       break;
2388
2389     default:
2390       gcc_unreachable ();
2391     }
2392
2393   return code;
2394 }
2395
2396
2397 /* Define how to find the value returned by a function.  */
2398
2399 rtx
2400 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2401 {
2402   enum machine_mode mode;
2403   int unsignedp ATTRIBUTE_UNUSED;
2404   rtx r ATTRIBUTE_UNUSED;
2405
2406   mode = TYPE_MODE (type);
2407   /* Promote integer types.  */
2408   if (INTEGRAL_TYPE_P (type))
2409     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2410
2411   /* Promotes small structs returned in a register to full-word size
2412      for big-endian AAPCS.  */
2413   if (arm_return_in_msb (type))
2414     {
2415       HOST_WIDE_INT size = int_size_in_bytes (type);
2416       if (size % UNITS_PER_WORD != 0)
2417         {
2418           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2419           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2420         }
2421     }
2422   
2423   return LIBCALL_VALUE(mode);
2424 }
2425
2426 /* Determine the amount of memory needed to store the possible return 
2427    registers of an untyped call.  */
2428 int
2429 arm_apply_result_size (void)
2430 {
2431   int size = 16;
2432
2433   if (TARGET_ARM)
2434     {
2435       if (TARGET_HARD_FLOAT_ABI)
2436         {
2437           if (TARGET_FPA)
2438             size += 12;
2439           if (TARGET_MAVERICK)
2440             size += 8;
2441         }
2442       if (TARGET_IWMMXT_ABI)
2443         size += 8;
2444     }
2445
2446   return size;
2447 }
2448
2449 /* Decide whether a type should be returned in memory (true)
2450    or in a register (false).  This is called by the macro
2451    RETURN_IN_MEMORY.  */
2452 int
2453 arm_return_in_memory (tree type)
2454 {
2455   HOST_WIDE_INT size;
2456
2457   if (!AGGREGATE_TYPE_P (type) &&
2458       (TREE_CODE (type) != VECTOR_TYPE) &&
2459       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2460     /* All simple types are returned in registers.
2461        For AAPCS, complex types are treated the same as aggregates.  */
2462     return 0;
2463
2464   size = int_size_in_bytes (type);
2465
2466   if (arm_abi != ARM_ABI_APCS)
2467     {
2468       /* ATPCS and later return aggregate types in memory only if they are
2469          larger than a word (or are variable size).  */
2470       return (size < 0 || size > UNITS_PER_WORD);
2471     }
2472
2473   /* To maximize backwards compatibility with previous versions of gcc,
2474      return vectors up to 4 words in registers.  */
2475   if (TREE_CODE (type) == VECTOR_TYPE)
2476     return (size < 0 || size > (4 * UNITS_PER_WORD));
2477
2478   /* For the arm-wince targets we choose to be compatible with Microsoft's
2479      ARM and Thumb compilers, which always return aggregates in memory.  */
2480 #ifndef ARM_WINCE
2481   /* All structures/unions bigger than one word are returned in memory.
2482      Also catch the case where int_size_in_bytes returns -1.  In this case
2483      the aggregate is either huge or of variable size, and in either case
2484      we will want to return it via memory and not in a register.  */
2485   if (size < 0 || size > UNITS_PER_WORD)
2486     return 1;
2487
2488   if (TREE_CODE (type) == RECORD_TYPE)
2489     {
2490       tree field;
2491
2492       /* For a struct the APCS says that we only return in a register
2493          if the type is 'integer like' and every addressable element
2494          has an offset of zero.  For practical purposes this means
2495          that the structure can have at most one non bit-field element
2496          and that this element must be the first one in the structure.  */
2497
2498       /* Find the first field, ignoring non FIELD_DECL things which will
2499          have been created by C++.  */
2500       for (field = TYPE_FIELDS (type);
2501            field && TREE_CODE (field) != FIELD_DECL;
2502            field = TREE_CHAIN (field))
2503         continue;
2504
2505       if (field == NULL)
2506         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2507
2508       /* Check that the first field is valid for returning in a register.  */
2509
2510       /* ... Floats are not allowed */
2511       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2512         return 1;
2513
2514       /* ... Aggregates that are not themselves valid for returning in
2515          a register are not allowed.  */
2516       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2517         return 1;
2518
2519       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2520          since they are not addressable.  */
2521       for (field = TREE_CHAIN (field);
2522            field;
2523            field = TREE_CHAIN (field))
2524         {
2525           if (TREE_CODE (field) != FIELD_DECL)
2526             continue;
2527
2528           if (!DECL_BIT_FIELD_TYPE (field))
2529             return 1;
2530         }
2531
2532       return 0;
2533     }
2534
2535   if (TREE_CODE (type) == UNION_TYPE)
2536     {
2537       tree field;
2538
2539       /* Unions can be returned in registers if every element is
2540          integral, or can be returned in an integer register.  */
2541       for (field = TYPE_FIELDS (type);
2542            field;
2543            field = TREE_CHAIN (field))
2544         {
2545           if (TREE_CODE (field) != FIELD_DECL)
2546             continue;
2547
2548           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2549             return 1;
2550
2551           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2552             return 1;
2553         }
2554
2555       return 0;
2556     }
2557 #endif /* not ARM_WINCE */
2558
2559   /* Return all other types in memory.  */
2560   return 1;
2561 }
2562
2563 /* Indicate whether or not words of a double are in big-endian order.  */
2564
2565 int
2566 arm_float_words_big_endian (void)
2567 {
2568   if (TARGET_MAVERICK)
2569     return 0;
2570
2571   /* For FPA, float words are always big-endian.  For VFP, floats words
2572      follow the memory system mode.  */
2573
2574   if (TARGET_FPA)
2575     {
2576       return 1;
2577     }
2578
2579   if (TARGET_VFP)
2580     return (TARGET_BIG_END ? 1 : 0);
2581
2582   return 1;
2583 }
2584
2585 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2586    for a call to a function whose data type is FNTYPE.
2587    For a library call, FNTYPE is NULL.  */
2588 void
2589 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2590                           rtx libname  ATTRIBUTE_UNUSED,
2591                           tree fndecl ATTRIBUTE_UNUSED)
2592 {
2593   /* On the ARM, the offset starts at 0.  */
2594   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2595   pcum->iwmmxt_nregs = 0;
2596   pcum->can_split = true;
2597
2598   pcum->call_cookie = CALL_NORMAL;
2599
2600   if (TARGET_LONG_CALLS)
2601     pcum->call_cookie = CALL_LONG;
2602
2603   /* Check for long call/short call attributes.  The attributes
2604      override any command line option.  */
2605   if (fntype)
2606     {
2607       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2608         pcum->call_cookie = CALL_SHORT;
2609       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2610         pcum->call_cookie = CALL_LONG;
2611     }
2612
2613   /* Varargs vectors are treated the same as long long.
2614      named_count avoids having to change the way arm handles 'named' */
2615   pcum->named_count = 0;
2616   pcum->nargs = 0;
2617
2618   if (TARGET_REALLY_IWMMXT && fntype)
2619     {
2620       tree fn_arg;
2621
2622       for (fn_arg = TYPE_ARG_TYPES (fntype);
2623            fn_arg;
2624            fn_arg = TREE_CHAIN (fn_arg))
2625         pcum->named_count += 1;
2626
2627       if (! pcum->named_count)
2628         pcum->named_count = INT_MAX;
2629     }
2630 }
2631
2632
2633 /* Return true if mode/type need doubleword alignment.  */
2634 bool
2635 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2636 {
2637   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2638           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2639 }
2640
2641
2642 /* Determine where to put an argument to a function.
2643    Value is zero to push the argument on the stack,
2644    or a hard register in which to store the argument.
2645
2646    MODE is the argument's machine mode.
2647    TYPE is the data type of the argument (as a tree).
2648     This is null for libcalls where that information may
2649     not be available.
2650    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2651     the preceding args and about the function being called.
2652    NAMED is nonzero if this argument is a named parameter
2653     (otherwise it is an extra parameter matching an ellipsis).  */
2654
2655 rtx
2656 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2657                   tree type, int named)
2658 {
2659   int nregs;
2660
2661   /* Varargs vectors are treated the same as long long.
2662      named_count avoids having to change the way arm handles 'named' */
2663   if (TARGET_IWMMXT_ABI
2664       && arm_vector_mode_supported_p (mode)
2665       && pcum->named_count > pcum->nargs + 1)
2666     {
2667       if (pcum->iwmmxt_nregs <= 9)
2668         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2669       else
2670         {
2671           pcum->can_split = false;
2672           return NULL_RTX;
2673         }
2674     }
2675
2676   /* Put doubleword aligned quantities in even register pairs.  */
2677   if (pcum->nregs & 1
2678       && ARM_DOUBLEWORD_ALIGN
2679       && arm_needs_doubleword_align (mode, type))
2680     pcum->nregs++;
2681
2682   if (mode == VOIDmode)
2683     /* Compute operand 2 of the call insn.  */
2684     return GEN_INT (pcum->call_cookie);
2685
2686   /* Only allow splitting an arg between regs and memory if all preceding
2687      args were allocated to regs.  For args passed by reference we only count
2688      the reference pointer.  */
2689   if (pcum->can_split)
2690     nregs = 1;
2691   else
2692     nregs = ARM_NUM_REGS2 (mode, type);
2693
2694   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2695     return NULL_RTX;
2696
2697   return gen_rtx_REG (mode, pcum->nregs);
2698 }
2699
2700 static int
2701 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2702                        tree type, bool named ATTRIBUTE_UNUSED)
2703 {
2704   int nregs = pcum->nregs;
2705
2706   if (arm_vector_mode_supported_p (mode))
2707     return 0;
2708
2709   if (NUM_ARG_REGS > nregs
2710       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2711       && pcum->can_split)
2712     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2713
2714   return 0;
2715 }
2716
2717 /* Variable sized types are passed by reference.  This is a GCC
2718    extension to the ARM ABI.  */
2719
2720 static bool
2721 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2722                        enum machine_mode mode ATTRIBUTE_UNUSED,
2723                        tree type, bool named ATTRIBUTE_UNUSED)
2724 {
2725   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2726 }
2727 \f
2728 /* Encode the current state of the #pragma [no_]long_calls.  */
2729 typedef enum
2730 {
2731   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2732   LONG,         /* #pragma long_calls is in effect.  */
2733   SHORT         /* #pragma no_long_calls is in effect.  */
2734 } arm_pragma_enum;
2735
2736 static arm_pragma_enum arm_pragma_long_calls = OFF;
2737
2738 void
2739 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2740 {
2741   arm_pragma_long_calls = LONG;
2742 }
2743
2744 void
2745 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2746 {
2747   arm_pragma_long_calls = SHORT;
2748 }
2749
2750 void
2751 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2752 {
2753   arm_pragma_long_calls = OFF;
2754 }
2755 \f
2756 /* Table of machine attributes.  */
2757 const struct attribute_spec arm_attribute_table[] =
2758 {
2759   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2760   /* Function calls made to this symbol must be done indirectly, because
2761      it may lie outside of the 26 bit addressing range of a normal function
2762      call.  */
2763   { "long_call",    0, 0, false, true,  true,  NULL },
2764   /* Whereas these functions are always known to reside within the 26 bit
2765      addressing range.  */
2766   { "short_call",   0, 0, false, true,  true,  NULL },
2767   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2768   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2769   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2770   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2771 #ifdef ARM_PE
2772   /* ARM/PE has three new attributes:
2773      interfacearm - ?
2774      dllexport - for exporting a function/variable that will live in a dll
2775      dllimport - for importing a function/variable from a dll
2776
2777      Microsoft allows multiple declspecs in one __declspec, separating
2778      them with spaces.  We do NOT support this.  Instead, use __declspec
2779      multiple times.
2780   */
2781   { "dllimport",    0, 0, true,  false, false, NULL },
2782   { "dllexport",    0, 0, true,  false, false, NULL },
2783   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2784 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2785   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2786   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2787   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2788 #endif
2789   { NULL,           0, 0, false, false, false, NULL }
2790 };
2791
2792 /* Handle an attribute requiring a FUNCTION_DECL;
2793    arguments as in struct attribute_spec.handler.  */
2794 static tree
2795 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2796                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2797 {
2798   if (TREE_CODE (*node) != FUNCTION_DECL)
2799     {
2800       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2801                IDENTIFIER_POINTER (name));
2802       *no_add_attrs = true;
2803     }
2804
2805   return NULL_TREE;
2806 }
2807
2808 /* Handle an "interrupt" or "isr" attribute;
2809    arguments as in struct attribute_spec.handler.  */
2810 static tree
2811 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2812                           bool *no_add_attrs)
2813 {
2814   if (DECL_P (*node))
2815     {
2816       if (TREE_CODE (*node) != FUNCTION_DECL)
2817         {
2818           warning (OPT_Wattributes, "%qs attribute only applies to functions",
2819                    IDENTIFIER_POINTER (name));
2820           *no_add_attrs = true;
2821         }
2822       /* FIXME: the argument if any is checked for type attributes;
2823          should it be checked for decl ones?  */
2824     }
2825   else
2826     {
2827       if (TREE_CODE (*node) == FUNCTION_TYPE
2828           || TREE_CODE (*node) == METHOD_TYPE)
2829         {
2830           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2831             {
2832               warning (OPT_Wattributes, "%qs attribute ignored",
2833                        IDENTIFIER_POINTER (name));
2834               *no_add_attrs = true;
2835             }
2836         }
2837       else if (TREE_CODE (*node) == POINTER_TYPE
2838                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2839                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2840                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2841         {
2842           *node = build_variant_type_copy (*node);
2843           TREE_TYPE (*node) = build_type_attribute_variant
2844             (TREE_TYPE (*node),
2845              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2846           *no_add_attrs = true;
2847         }
2848       else
2849         {
2850           /* Possibly pass this attribute on from the type to a decl.  */
2851           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2852                        | (int) ATTR_FLAG_FUNCTION_NEXT
2853                        | (int) ATTR_FLAG_ARRAY_NEXT))
2854             {
2855               *no_add_attrs = true;
2856               return tree_cons (name, args, NULL_TREE);
2857             }
2858           else
2859             {
2860               warning (OPT_Wattributes, "%qs attribute ignored",
2861                        IDENTIFIER_POINTER (name));
2862             }
2863         }
2864     }
2865
2866   return NULL_TREE;
2867 }
2868
2869 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2870 /* Handle the "notshared" attribute.  This attribute is another way of
2871    requesting hidden visibility.  ARM's compiler supports
2872    "__declspec(notshared)"; we support the same thing via an
2873    attribute.  */
2874
2875 static tree
2876 arm_handle_notshared_attribute (tree *node, 
2877                                 tree name ATTRIBUTE_UNUSED, 
2878                                 tree args ATTRIBUTE_UNUSED, 
2879                                 int flags ATTRIBUTE_UNUSED, 
2880                                 bool *no_add_attrs)
2881 {
2882   tree decl = TYPE_NAME (*node);
2883
2884   if (decl)
2885     {
2886       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2887       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2888       *no_add_attrs = false;
2889     }
2890   return NULL_TREE;
2891 }
2892 #endif
2893
2894 /* Return 0 if the attributes for two types are incompatible, 1 if they
2895    are compatible, and 2 if they are nearly compatible (which causes a
2896    warning to be generated).  */
2897 static int
2898 arm_comp_type_attributes (tree type1, tree type2)
2899 {
2900   int l1, l2, s1, s2;
2901
2902   /* Check for mismatch of non-default calling convention.  */
2903   if (TREE_CODE (type1) != FUNCTION_TYPE)
2904     return 1;
2905
2906   /* Check for mismatched call attributes.  */
2907   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2908   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2909   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2910   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2911
2912   /* Only bother to check if an attribute is defined.  */
2913   if (l1 | l2 | s1 | s2)
2914     {
2915       /* If one type has an attribute, the other must have the same attribute.  */
2916       if ((l1 != l2) || (s1 != s2))
2917         return 0;
2918
2919       /* Disallow mixed attributes.  */
2920       if ((l1 & s2) || (l2 & s1))
2921         return 0;
2922     }
2923
2924   /* Check for mismatched ISR attribute.  */
2925   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2926   if (! l1)
2927     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2928   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2929   if (! l2)
2930     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2931   if (l1 != l2)
2932     return 0;
2933
2934   return 1;
2935 }
2936
2937 /*  Encode long_call or short_call attribute by prefixing
2938     symbol name in DECL with a special character FLAG.  */
2939 void
2940 arm_encode_call_attribute (tree decl, int flag)
2941 {
2942   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2943   int          len = strlen (str);
2944   char *       newstr;
2945
2946   /* Do not allow weak functions to be treated as short call.  */
2947   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2948     return;
2949
2950   newstr = alloca (len + 2);
2951   newstr[0] = flag;
2952   strcpy (newstr + 1, str);
2953
2954   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2955   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2956 }
2957
2958 /*  Assigns default attributes to newly defined type.  This is used to
2959     set short_call/long_call attributes for function types of
2960     functions defined inside corresponding #pragma scopes.  */
2961 static void
2962 arm_set_default_type_attributes (tree type)
2963 {
2964   /* Add __attribute__ ((long_call)) to all functions, when
2965      inside #pragma long_calls or __attribute__ ((short_call)),
2966      when inside #pragma no_long_calls.  */
2967   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2968     {
2969       tree type_attr_list, attr_name;
2970       type_attr_list = TYPE_ATTRIBUTES (type);
2971
2972       if (arm_pragma_long_calls == LONG)
2973         attr_name = get_identifier ("long_call");
2974       else if (arm_pragma_long_calls == SHORT)
2975         attr_name = get_identifier ("short_call");
2976       else
2977         return;
2978
2979       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2980       TYPE_ATTRIBUTES (type) = type_attr_list;
2981     }
2982 }
2983 \f
2984 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2985    defined within the current compilation unit.  If this cannot be
2986    determined, then 0 is returned.  */
2987 static int
2988 current_file_function_operand (rtx sym_ref)
2989 {
2990   /* This is a bit of a fib.  A function will have a short call flag
2991      applied to its name if it has the short call attribute, or it has
2992      already been defined within the current compilation unit.  */
2993   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2994     return 1;
2995
2996   /* The current function is always defined within the current compilation
2997      unit.  If it s a weak definition however, then this may not be the real
2998      definition of the function, and so we have to say no.  */
2999   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3000       && !DECL_WEAK (current_function_decl))
3001     return 1;
3002
3003   /* We cannot make the determination - default to returning 0.  */
3004   return 0;
3005 }
3006
3007 /* Return nonzero if a 32 bit "long_call" should be generated for
3008    this call.  We generate a long_call if the function:
3009
3010         a.  has an __attribute__((long call))
3011      or b.  is within the scope of a #pragma long_calls
3012      or c.  the -mlong-calls command line switch has been specified
3013          .  and either:
3014                 1. -ffunction-sections is in effect
3015              or 2. the current function has __attribute__ ((section))
3016              or 3. the target function has __attribute__ ((section))
3017
3018    However we do not generate a long call if the function:
3019
3020         d.  has an __attribute__ ((short_call))
3021      or e.  is inside the scope of a #pragma no_long_calls
3022      or f.  is defined within the current compilation unit.
3023
3024    This function will be called by C fragments contained in the machine
3025    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3026    rtl operands.  CALL_SYMBOL is used to distinguish between
3027    two different callers of the function.  It is set to 1 in the
3028    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3029    and "call_value" patterns.  This is because of the difference in the
3030    SYM_REFs passed by these patterns.  */
3031 int
3032 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3033 {
3034   if (!call_symbol)
3035     {
3036       if (GET_CODE (sym_ref) != MEM)
3037         return 0;
3038
3039       sym_ref = XEXP (sym_ref, 0);
3040     }
3041
3042   if (GET_CODE (sym_ref) != SYMBOL_REF)
3043     return 0;
3044
3045   if (call_cookie & CALL_SHORT)
3046     return 0;
3047
3048   if (TARGET_LONG_CALLS)
3049     {
3050       if (flag_function_sections
3051           || DECL_SECTION_NAME (current_function_decl))
3052         /* c.3 is handled by the definition of the
3053            ARM_DECLARE_FUNCTION_SIZE macro.  */
3054         return 1;
3055     }
3056
3057   if (current_file_function_operand (sym_ref))
3058     return 0;
3059
3060   return (call_cookie & CALL_LONG)
3061     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3062     || TARGET_LONG_CALLS;
3063 }
3064
3065 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3066 static bool
3067 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3068 {
3069   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3070
3071   if (cfun->machine->sibcall_blocked)
3072     return false;
3073
3074   /* Never tailcall something for which we have no decl, or if we
3075      are in Thumb mode.  */
3076   if (decl == NULL || TARGET_THUMB)
3077     return false;
3078
3079   /* Get the calling method.  */
3080   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3081     call_type = CALL_SHORT;
3082   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3083     call_type = CALL_LONG;
3084
3085   /* Cannot tail-call to long calls, since these are out of range of
3086      a branch instruction.  However, if not compiling PIC, we know
3087      we can reach the symbol if it is in this compilation unit.  */
3088   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3089     return false;
3090
3091   /* If we are interworking and the function is not declared static
3092      then we can't tail-call it unless we know that it exists in this
3093      compilation unit (since it might be a Thumb routine).  */
3094   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3095     return false;
3096
3097   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3098   if (IS_INTERRUPT (arm_current_func_type ()))
3099     return false;
3100
3101   /* Everything else is ok.  */
3102   return true;
3103 }
3104
3105 \f
3106 /* Addressing mode support functions.  */
3107
3108 /* Return nonzero if X is a legitimate immediate operand when compiling
3109    for PIC.  */
3110 int
3111 legitimate_pic_operand_p (rtx x)
3112 {
3113   if (CONSTANT_P (x)
3114       && flag_pic
3115       && (GET_CODE (x) == SYMBOL_REF
3116           || (GET_CODE (x) == CONST
3117               && GET_CODE (XEXP (x, 0)) == PLUS
3118               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3119     return 0;
3120
3121   return 1;
3122 }
3123
3124 rtx
3125 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3126 {
3127   if (GET_CODE (orig) == SYMBOL_REF
3128       || GET_CODE (orig) == LABEL_REF)
3129     {
3130 #ifndef AOF_ASSEMBLER
3131       rtx pic_ref, address;
3132 #endif
3133       rtx insn;
3134       int subregs = 0;
3135
3136       if (reg == 0)
3137         {
3138           gcc_assert (!no_new_pseudos);
3139           reg = gen_reg_rtx (Pmode);
3140
3141           subregs = 1;
3142         }
3143
3144 #ifdef AOF_ASSEMBLER
3145       /* The AOF assembler can generate relocations for these directly, and
3146          understands that the PIC register has to be added into the offset.  */
3147       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3148 #else
3149       if (subregs)
3150         address = gen_reg_rtx (Pmode);
3151       else
3152         address = reg;
3153
3154       if (TARGET_ARM)
3155         emit_insn (gen_pic_load_addr_arm (address, orig));
3156       else
3157         emit_insn (gen_pic_load_addr_thumb (address, orig));
3158
3159       if ((GET_CODE (orig) == LABEL_REF
3160            || (GET_CODE (orig) == SYMBOL_REF &&
3161                SYMBOL_REF_LOCAL_P (orig)))
3162           && NEED_GOT_RELOC)
3163         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3164       else
3165         {
3166           pic_ref = gen_const_mem (Pmode,
3167                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3168                                                  address));
3169         }
3170
3171       insn = emit_move_insn (reg, pic_ref);
3172 #endif
3173       current_function_uses_pic_offset_table = 1;
3174       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3175          by loop.  */
3176       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3177                                             REG_NOTES (insn));
3178       return reg;
3179     }
3180   else if (GET_CODE (orig) == CONST)
3181     {
3182       rtx base, offset;
3183
3184       if (GET_CODE (XEXP (orig, 0)) == PLUS
3185           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3186         return orig;
3187
3188       if (reg == 0)
3189         {
3190           gcc_assert (!no_new_pseudos);
3191           reg = gen_reg_rtx (Pmode);
3192         }
3193
3194       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3195       
3196       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3197       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3198                                        base == reg ? 0 : reg);
3199
3200       if (GET_CODE (offset) == CONST_INT)
3201         {
3202           /* The base register doesn't really matter, we only want to
3203              test the index for the appropriate mode.  */
3204           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3205             {
3206               gcc_assert (!no_new_pseudos);
3207               offset = force_reg (Pmode, offset);
3208             }
3209
3210           if (GET_CODE (offset) == CONST_INT)
3211             return plus_constant (base, INTVAL (offset));
3212         }
3213
3214       if (GET_MODE_SIZE (mode) > 4
3215           && (GET_MODE_CLASS (mode) == MODE_INT
3216               || TARGET_SOFT_FLOAT))
3217         {
3218           emit_insn (gen_addsi3 (reg, base, offset));
3219           return reg;
3220         }
3221
3222       return gen_rtx_PLUS (Pmode, base, offset);
3223     }
3224
3225   return orig;
3226 }
3227
3228
3229 /* Find a spare low register to use during the prolog of a function.  */
3230
3231 static int
3232 thumb_find_work_register (unsigned long pushed_regs_mask)
3233 {
3234   int reg;
3235
3236   /* Check the argument registers first as these are call-used.  The
3237      register allocation order means that sometimes r3 might be used
3238      but earlier argument registers might not, so check them all.  */
3239   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3240     if (!regs_ever_live[reg])
3241       return reg;
3242
3243   /* Before going on to check the call-saved registers we can try a couple
3244      more ways of deducing that r3 is available.  The first is when we are
3245      pushing anonymous arguments onto the stack and we have less than 4
3246      registers worth of fixed arguments(*).  In this case r3 will be part of
3247      the variable argument list and so we can be sure that it will be
3248      pushed right at the start of the function.  Hence it will be available
3249      for the rest of the prologue.
3250      (*): ie current_function_pretend_args_size is greater than 0.  */
3251   if (cfun->machine->uses_anonymous_args
3252       && current_function_pretend_args_size > 0)
3253     return LAST_ARG_REGNUM;
3254
3255   /* The other case is when we have fixed arguments but less than 4 registers
3256      worth.  In this case r3 might be used in the body of the function, but
3257      it is not being used to convey an argument into the function.  In theory
3258      we could just check current_function_args_size to see how many bytes are
3259      being passed in argument registers, but it seems that it is unreliable.
3260      Sometimes it will have the value 0 when in fact arguments are being
3261      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3262      check the args_info.nregs field as well.  The problem with this field is
3263      that it makes no allowances for arguments that are passed to the
3264      function but which are not used.  Hence we could miss an opportunity
3265      when a function has an unused argument in r3.  But it is better to be
3266      safe than to be sorry.  */
3267   if (! cfun->machine->uses_anonymous_args
3268       && current_function_args_size >= 0
3269       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3270       && cfun->args_info.nregs < 4)
3271     return LAST_ARG_REGNUM;
3272   
3273   /* Otherwise look for a call-saved register that is going to be pushed.  */
3274   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3275     if (pushed_regs_mask & (1 << reg))
3276       return reg;
3277
3278   /* Something went wrong - thumb_compute_save_reg_mask()
3279      should have arranged for a suitable register to be pushed.  */
3280   gcc_unreachable ();
3281 }
3282
3283
3284 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3285    low register.  */
3286
3287 void
3288 arm_load_pic_register (unsigned int scratch)
3289 {
3290 #ifndef AOF_ASSEMBLER
3291   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3292   rtx global_offset_table;
3293
3294   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3295     return;
3296
3297   gcc_assert (flag_pic);
3298
3299   l1 = gen_label_rtx ();
3300
3301   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3302   /* On the ARM the PC register contains 'dot + 8' at the time of the
3303      addition, on the Thumb it is 'dot + 4'.  */
3304   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3305   if (GOT_PCREL)
3306     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3307                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3308   else
3309     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3310
3311   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3312
3313   if (TARGET_ARM)
3314     {
3315       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3316       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3317     }
3318   else
3319     {
3320       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3321         {
3322           /* We will have pushed the pic register, so should always be
3323              able to find a work register.  */
3324           pic_tmp = gen_rtx_REG (SImode, scratch);
3325           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3326           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3327         }
3328       else
3329         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3330       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3331     }
3332
3333   /* Need to emit this whether or not we obey regdecls,
3334      since setjmp/longjmp can cause life info to screw up.  */
3335   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3336 #endif /* AOF_ASSEMBLER */
3337 }
3338
3339
3340 /* Return nonzero if X is valid as an ARM state addressing register.  */
3341 static int
3342 arm_address_register_rtx_p (rtx x, int strict_p)
3343 {
3344   int regno;
3345
3346   if (GET_CODE (x) != REG)
3347     return 0;
3348
3349   regno = REGNO (x);
3350
3351   if (strict_p)
3352     return ARM_REGNO_OK_FOR_BASE_P (regno);
3353
3354   return (regno <= LAST_ARM_REGNUM
3355           || regno >= FIRST_PSEUDO_REGISTER
3356           || regno == FRAME_POINTER_REGNUM
3357           || regno == ARG_POINTER_REGNUM);
3358 }
3359
3360 /* Return nonzero if X is a valid ARM state address operand.  */
3361 int
3362 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3363                           int strict_p)
3364 {
3365   bool use_ldrd;
3366   enum rtx_code code = GET_CODE (x);
3367
3368   if (arm_address_register_rtx_p (x, strict_p))
3369     return 1;
3370
3371   use_ldrd = (TARGET_LDRD
3372               && (mode == DImode
3373                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3374
3375   if (code == POST_INC || code == PRE_DEC
3376       || ((code == PRE_INC || code == POST_DEC)
3377           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3378     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3379
3380   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3381            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3382            && GET_CODE (XEXP (x, 1)) == PLUS
3383            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3384     {
3385       rtx addend = XEXP (XEXP (x, 1), 1);
3386
3387       /* Don't allow ldrd post increment by register because it's hard
3388          to fixup invalid register choices.  */
3389       if (use_ldrd
3390           && GET_CODE (x) == POST_MODIFY
3391           && GET_CODE (addend) == REG)
3392         return 0;
3393
3394       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3395               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3396     }
3397
3398   /* After reload constants split into minipools will have addresses
3399      from a LABEL_REF.  */
3400   else if (reload_completed
3401            && (code == LABEL_REF
3402                || (code == CONST
3403                    && GET_CODE (XEXP (x, 0)) == PLUS
3404                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3405                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3406     return 1;
3407
3408   else if (mode == TImode)
3409     return 0;
3410
3411   else if (code == PLUS)
3412     {
3413       rtx xop0 = XEXP (x, 0);
3414       rtx xop1 = XEXP (x, 1);
3415
3416       return ((arm_address_register_rtx_p (xop0, strict_p)
3417                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3418               || (arm_address_register_rtx_p (xop1, strict_p)
3419                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3420     }
3421
3422 #if 0
3423   /* Reload currently can't handle MINUS, so disable this for now */
3424   else if (GET_CODE (x) == MINUS)
3425     {
3426       rtx xop0 = XEXP (x, 0);
3427       rtx xop1 = XEXP (x, 1);
3428
3429       return (arm_address_register_rtx_p (xop0, strict_p)
3430               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3431     }
3432 #endif
3433
3434   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3435            && code == SYMBOL_REF
3436            && CONSTANT_POOL_ADDRESS_P (x)
3437            && ! (flag_pic
3438                  && symbol_mentioned_p (get_pool_constant (x))))
3439     return 1;
3440
3441   return 0;
3442 }
3443
3444 /* Return nonzero if INDEX is valid for an address index operand in
3445    ARM state.  */
3446 static int
3447 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3448                         int strict_p)
3449 {
3450   HOST_WIDE_INT range;
3451   enum rtx_code code = GET_CODE (index);
3452
3453   /* Standard coprocessor addressing modes.  */
3454   if (TARGET_HARD_FLOAT
3455       && (TARGET_FPA || TARGET_MAVERICK)
3456       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3457           || (TARGET_MAVERICK && mode == DImode)))
3458     return (code == CONST_INT && INTVAL (index) < 1024
3459             && INTVAL (index) > -1024
3460             && (INTVAL (index) & 3) == 0);
3461
3462   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3463     return (code == CONST_INT
3464             && INTVAL (index) < 1024
3465             && INTVAL (index) > -1024
3466             && (INTVAL (index) & 3) == 0);
3467
3468   if (arm_address_register_rtx_p (index, strict_p)
3469       && (GET_MODE_SIZE (mode) <= 4))
3470     return 1;
3471
3472   if (mode == DImode || mode == DFmode)
3473     {
3474       if (code == CONST_INT)
3475         {
3476           HOST_WIDE_INT val = INTVAL (index);
3477
3478           if (TARGET_LDRD)
3479             return val > -256 && val < 256;
3480           else
3481             return val > -4096 && val < 4092;
3482         }
3483
3484       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3485     }
3486
3487   if (GET_MODE_SIZE (mode) <= 4
3488       && ! (arm_arch4
3489             && (mode == HImode
3490                 || (mode == QImode && outer == SIGN_EXTEND))))
3491     {
3492       if (code == MULT)
3493         {
3494           rtx xiop0 = XEXP (index, 0);
3495           rtx xiop1 = XEXP (index, 1);
3496
3497           return ((arm_address_register_rtx_p (xiop0, strict_p)
3498                    && power_of_two_operand (xiop1, SImode))
3499                   || (arm_address_register_rtx_p (xiop1, strict_p)
3500                       && power_of_two_operand (xiop0, SImode)));
3501         }
3502       else if (code == LSHIFTRT || code == ASHIFTRT
3503                || code == ASHIFT || code == ROTATERT)
3504         {
3505           rtx op = XEXP (index, 1);
3506
3507           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3508                   && GET_CODE (op) == CONST_INT
3509                   && INTVAL (op) > 0
3510                   && INTVAL (op) <= 31);
3511         }
3512     }
3513
3514   /* For ARM v4 we may be doing a sign-extend operation during the
3515      load.  */
3516   if (arm_arch4)
3517     {
3518       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3519         range = 256;
3520       else
3521         range = 4096;
3522     }
3523   else
3524     range = (mode == HImode) ? 4095 : 4096;
3525
3526   return (code == CONST_INT
3527           && INTVAL (index) < range
3528           && INTVAL (index) > -range);
3529 }
3530
3531 /* Return nonzero if X is valid as a Thumb state base register.  */
3532 static int
3533 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3534 {
3535   int regno;
3536
3537   if (GET_CODE (x) != REG)
3538     return 0;
3539
3540   regno = REGNO (x);
3541
3542   if (strict_p)
3543     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3544
3545   return (regno <= LAST_LO_REGNUM
3546           || regno > LAST_VIRTUAL_REGISTER
3547           || regno == FRAME_POINTER_REGNUM
3548           || (GET_MODE_SIZE (mode) >= 4
3549               && (regno == STACK_POINTER_REGNUM
3550                   || regno >= FIRST_PSEUDO_REGISTER
3551                   || x == hard_frame_pointer_rtx
3552                   || x == arg_pointer_rtx)));
3553 }
3554
3555 /* Return nonzero if x is a legitimate index register.  This is the case
3556    for any base register that can access a QImode object.  */
3557 inline static int
3558 thumb_index_register_rtx_p (rtx x, int strict_p)
3559 {
3560   return thumb_base_register_rtx_p (x, QImode, strict_p);
3561 }
3562
3563 /* Return nonzero if x is a legitimate Thumb-state address.
3564
3565    The AP may be eliminated to either the SP or the FP, so we use the
3566    least common denominator, e.g. SImode, and offsets from 0 to 64.
3567
3568    ??? Verify whether the above is the right approach.
3569
3570    ??? Also, the FP may be eliminated to the SP, so perhaps that
3571    needs special handling also.
3572
3573    ??? Look at how the mips16 port solves this problem.  It probably uses
3574    better ways to solve some of these problems.
3575
3576    Although it is not incorrect, we don't accept QImode and HImode
3577    addresses based on the frame pointer or arg pointer until the
3578    reload pass starts.  This is so that eliminating such addresses
3579    into stack based ones won't produce impossible code.  */
3580 int
3581 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3582 {
3583   /* ??? Not clear if this is right.  Experiment.  */
3584   if (GET_MODE_SIZE (mode) < 4
3585       && !(reload_in_progress || reload_completed)
3586       && (reg_mentioned_p (frame_pointer_rtx, x)
3587           || reg_mentioned_p (arg_pointer_rtx, x)
3588           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3589           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3590           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3591           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3592     return 0;
3593
3594   /* Accept any base register.  SP only in SImode or larger.  */
3595   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3596     return 1;
3597
3598   /* This is PC relative data before arm_reorg runs.  */
3599   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3600            && GET_CODE (x) == SYMBOL_REF
3601            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3602     return 1;
3603
3604   /* This is PC relative data after arm_reorg runs.  */
3605   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3606            && (GET_CODE (x) == LABEL_REF
3607                || (GET_CODE (x) == CONST
3608                    && GET_CODE (XEXP (x, 0)) == PLUS
3609                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3610                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3611     return 1;
3612
3613   /* Post-inc indexing only supported for SImode and larger.  */
3614   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3615            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3616     return 1;
3617
3618   else if (GET_CODE (x) == PLUS)
3619     {
3620       /* REG+REG address can be any two index registers.  */
3621       /* We disallow FRAME+REG addressing since we know that FRAME
3622          will be replaced with STACK, and SP relative addressing only
3623          permits SP+OFFSET.  */
3624       if (GET_MODE_SIZE (mode) <= 4
3625           && XEXP (x, 0) != frame_pointer_rtx
3626           && XEXP (x, 1) != frame_pointer_rtx
3627           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3628           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3629         return 1;
3630
3631       /* REG+const has 5-7 bit offset for non-SP registers.  */
3632       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3633                 || XEXP (x, 0) == arg_pointer_rtx)
3634                && GET_CODE (XEXP (x, 1)) == CONST_INT
3635                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3636         return 1;
3637
3638       /* REG+const has 10 bit offset for SP, but only SImode and
3639          larger is supported.  */
3640       /* ??? Should probably check for DI/DFmode overflow here
3641          just like GO_IF_LEGITIMATE_OFFSET does.  */
3642       else if (GET_CODE (XEXP (x, 0)) == REG
3643                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3644                && GET_MODE_SIZE (mode) >= 4
3645                && GET_CODE (XEXP (x, 1)) == CONST_INT
3646                && INTVAL (XEXP (x, 1)) >= 0
3647                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3648                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3649         return 1;
3650
3651       else if (GET_CODE (XEXP (x, 0)) == REG
3652                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3653                && GET_MODE_SIZE (mode) >= 4
3654                && GET_CODE (XEXP (x, 1)) == CONST_INT
3655                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3656         return 1;
3657     }
3658
3659   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3660            && GET_MODE_SIZE (mode) == 4
3661            && GET_CODE (x) == SYMBOL_REF
3662            && CONSTANT_POOL_ADDRESS_P (x)
3663            && !(flag_pic
3664                 && symbol_mentioned_p (get_pool_constant (x))))
3665     return 1;
3666
3667   return 0;
3668 }
3669
3670 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3671    instruction of mode MODE.  */
3672 int
3673 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3674 {
3675   switch (GET_MODE_SIZE (mode))
3676     {
3677     case 1:
3678       return val >= 0 && val < 32;
3679
3680     case 2:
3681       return val >= 0 && val < 64 && (val & 1) == 0;
3682
3683     default:
3684       return (val >= 0
3685               && (val + GET_MODE_SIZE (mode)) <= 128
3686               && (val & 3) == 0);
3687     }
3688 }
3689
3690 /* Try machine-dependent ways of modifying an illegitimate address
3691    to be legitimate.  If we find one, return the new, valid address.  */
3692 rtx
3693 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3694 {
3695   if (GET_CODE (x) == PLUS)
3696     {
3697       rtx xop0 = XEXP (x, 0);
3698       rtx xop1 = XEXP (x, 1);
3699
3700       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3701         xop0 = force_reg (SImode, xop0);
3702
3703       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3704         xop1 = force_reg (SImode, xop1);
3705
3706       if (ARM_BASE_REGISTER_RTX_P (xop0)
3707           && GET_CODE (xop1) == CONST_INT)
3708         {
3709           HOST_WIDE_INT n, low_n;
3710           rtx base_reg, val;
3711           n = INTVAL (xop1);
3712
3713           /* VFP addressing modes actually allow greater offsets, but for
3714              now we just stick with the lowest common denominator.  */
3715           if (mode == DImode
3716               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3717             {
3718               low_n = n & 0x0f;
3719               n &= ~0x0f;
3720               if (low_n > 4)
3721                 {
3722                   n += 16;
3723                   low_n -= 16;
3724                 }
3725             }
3726           else
3727             {
3728               low_n = ((mode) == TImode ? 0
3729                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3730               n -= low_n;
3731             }
3732
3733           base_reg = gen_reg_rtx (SImode);
3734           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3735                                              GEN_INT (n)), NULL_RTX);
3736           emit_move_insn (base_reg, val);
3737           x = (low_n == 0 ? base_reg
3738                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3739         }
3740       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3741         x = gen_rtx_PLUS (SImode, xop0, xop1);
3742     }
3743
3744   /* XXX We don't allow MINUS any more -- see comment in
3745      arm_legitimate_address_p ().  */
3746   else if (GET_CODE (x) == MINUS)
3747     {
3748       rtx xop0 = XEXP (x, 0);
3749       rtx xop1 = XEXP (x, 1);
3750
3751       if (CONSTANT_P (xop0))
3752         xop0 = force_reg (SImode, xop0);
3753
3754       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3755         xop1 = force_reg (SImode, xop1);
3756
3757       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3758         x = gen_rtx_MINUS (SImode, xop0, xop1);
3759     }
3760
3761   if (flag_pic)
3762     {
3763       /* We need to find and carefully transform any SYMBOL and LABEL
3764          references; so go back to the original address expression.  */
3765       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3766
3767       if (new_x != orig_x)
3768         x = new_x;
3769     }
3770
3771   return x;
3772 }
3773
3774
3775 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3776    to be legitimate.  If we find one, return the new, valid address.  */
3777 rtx
3778 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3779 {
3780   if (GET_CODE (x) == PLUS
3781       && GET_CODE (XEXP (x, 1)) == CONST_INT
3782       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3783           || INTVAL (XEXP (x, 1)) < 0))
3784     {
3785       rtx xop0 = XEXP (x, 0);
3786       rtx xop1 = XEXP (x, 1);
3787       HOST_WIDE_INT offset = INTVAL (xop1);
3788
3789       /* Try and fold the offset into a biasing of the base register and
3790          then offsetting that.  Don't do this when optimizing for space
3791          since it can cause too many CSEs.  */
3792       if (optimize_size && offset >= 0
3793           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3794         {
3795           HOST_WIDE_INT delta;
3796
3797           if (offset >= 256)
3798             delta = offset - (256 - GET_MODE_SIZE (mode));
3799           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3800             delta = 31 * GET_MODE_SIZE (mode);
3801           else
3802             delta = offset & (~31 * GET_MODE_SIZE (mode));
3803
3804           xop0 = force_operand (plus_constant (xop0, offset - delta),
3805                                 NULL_RTX);
3806           x = plus_constant (xop0, delta);
3807         }
3808       else if (offset < 0 && offset > -256)
3809         /* Small negative offsets are best done with a subtract before the
3810            dereference, forcing these into a register normally takes two
3811            instructions.  */
3812         x = force_operand (x, NULL_RTX);
3813       else
3814         {
3815           /* For the remaining cases, force the constant into a register.  */
3816           xop1 = force_reg (SImode, xop1);
3817           x = gen_rtx_PLUS (SImode, xop0, xop1);
3818         }
3819     }
3820   else if (GET_CODE (x) == PLUS
3821            && s_register_operand (XEXP (x, 1), SImode)
3822            && !s_register_operand (XEXP (x, 0), SImode))
3823     {
3824       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3825
3826       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3827     }
3828
3829   if (flag_pic)
3830     {
3831       /* We need to find and carefully transform any SYMBOL and LABEL
3832          references; so go back to the original address expression.  */
3833       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3834
3835       if (new_x != orig_x)
3836         x = new_x;
3837     }
3838
3839   return x;
3840 }
3841
3842 rtx
3843 thumb_legitimize_reload_address(rtx *x_p,
3844                                 enum machine_mode mode,
3845                                 int opnum, int type,
3846                                 int ind_levels ATTRIBUTE_UNUSED)
3847 {
3848   rtx x = *x_p;
3849   
3850   if (GET_CODE (x) == PLUS
3851       && GET_MODE_SIZE (mode) < 4
3852       && REG_P (XEXP (x, 0))
3853       && XEXP (x, 0) == stack_pointer_rtx
3854       && GET_CODE (XEXP (x, 1)) == CONST_INT
3855       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3856     {
3857       rtx orig_x = x;
3858
3859       x = copy_rtx (x);
3860       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
3861                    Pmode, VOIDmode, 0, 0, opnum, type);
3862       return x;
3863     }
3864
3865   /* If both registers are hi-regs, then it's better to reload the
3866      entire expression rather than each register individually.  That
3867      only requires one reload register rather than two.  */
3868   if (GET_CODE (x) == PLUS
3869       && REG_P (XEXP (x, 0))
3870       && REG_P (XEXP (x, 1))
3871       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
3872       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
3873     {
3874       rtx orig_x = x;
3875
3876       x = copy_rtx (x);
3877       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
3878                    Pmode, VOIDmode, 0, 0, opnum, type);
3879       return x;
3880     }
3881
3882   return NULL;
3883 }
3884
3885 \f
3886
3887 #define REG_OR_SUBREG_REG(X)                                            \
3888   (GET_CODE (X) == REG                                                  \
3889    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3890
3891 #define REG_OR_SUBREG_RTX(X)                    \
3892    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3893
3894 #ifndef COSTS_N_INSNS
3895 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3896 #endif
3897 static inline int
3898 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3899 {
3900   enum machine_mode mode = GET_MODE (x);
3901
3902   switch (code)
3903     {
3904     case ASHIFT:
3905     case ASHIFTRT:
3906     case LSHIFTRT:
3907     case ROTATERT:
3908     case PLUS:
3909     case MINUS:
3910     case COMPARE:
3911     case NEG:
3912     case NOT:
3913       return COSTS_N_INSNS (1);
3914
3915     case MULT:
3916       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3917         {
3918           int cycles = 0;
3919           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3920
3921           while (i)
3922             {
3923               i >>= 2;
3924               cycles++;
3925             }
3926           return COSTS_N_INSNS (2) + cycles;
3927         }
3928       return COSTS_N_INSNS (1) + 16;
3929
3930     case SET:
3931       return (COSTS_N_INSNS (1)
3932               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3933                      + GET_CODE (SET_DEST (x)) == MEM));
3934
3935     case CONST_INT:
3936       if (outer == SET)
3937         {
3938           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3939             return 0;
3940           if (thumb_shiftable_const (INTVAL (x)))
3941             return COSTS_N_INSNS (2);
3942           return COSTS_N_INSNS (3);
3943         }
3944       else if ((outer == PLUS || outer == COMPARE)
3945                && INTVAL (x) < 256 && INTVAL (x) > -256)
3946         return 0;
3947       else if (outer == AND
3948                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3949         return COSTS_N_INSNS (1);
3950       else if (outer == ASHIFT || outer == ASHIFTRT
3951                || outer == LSHIFTRT)
3952         return 0;
3953       return COSTS_N_INSNS (2);
3954
3955     case CONST:
3956     case CONST_DOUBLE:
3957     case LABEL_REF:
3958     case SYMBOL_REF:
3959       return COSTS_N_INSNS (3);
3960
3961     case UDIV:
3962     case UMOD:
3963     case DIV:
3964     case MOD:
3965       return 100;
3966
3967     case TRUNCATE:
3968       return 99;
3969
3970     case AND:
3971     case XOR:
3972     case IOR:
3973       /* XXX guess.  */
3974       return 8;
3975
3976     case MEM:
3977       /* XXX another guess.  */
3978       /* Memory costs quite a lot for the first word, but subsequent words
3979          load at the equivalent of a single insn each.  */
3980       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3981               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3982                  ? 4 : 0));
3983
3984     case IF_THEN_ELSE:
3985       /* XXX a guess.  */
3986       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3987         return 14;
3988       return 2;
3989
3990     case ZERO_EXTEND:
3991       /* XXX still guessing.  */
3992       switch (GET_MODE (XEXP (x, 0)))
3993         {
3994         case QImode:
3995           return (1 + (mode == DImode ? 4 : 0)
3996                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3997
3998         case HImode:
3999           return (4 + (mode == DImode ? 4 : 0)
4000                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4001
4002         case SImode:
4003           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4004
4005         default:
4006           return 99;
4007         }
4008
4009     default:
4010       return 99;
4011     }
4012 }
4013
4014
4015 /* Worker routine for arm_rtx_costs.  */
4016 static inline int
4017 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4018 {
4019   enum machine_mode mode = GET_MODE (x);
4020   enum rtx_code subcode;
4021   int extra_cost;
4022
4023   switch (code)
4024     {
4025     case MEM:
4026       /* Memory costs quite a lot for the first word, but subsequent words
4027          load at the equivalent of a single insn each.  */
4028       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4029               + (GET_CODE (x) == SYMBOL_REF
4030                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4031
4032     case DIV:
4033     case MOD:
4034     case UDIV:
4035     case UMOD:
4036       return optimize_size ? COSTS_N_INSNS (2) : 100;
4037
4038     case ROTATE:
4039       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4040         return 4;
4041       /* Fall through */
4042     case ROTATERT:
4043       if (mode != SImode)
4044         return 8;
4045       /* Fall through */
4046     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4047       if (mode == DImode)
4048         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4049                 + ((GET_CODE (XEXP (x, 0)) == REG
4050                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4051                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4052                    ? 0 : 8));
4053       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4054                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4055                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4056                    ? 0 : 4)
4057               + ((GET_CODE (XEXP (x, 1)) == REG
4058                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4059                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4060                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4061                  ? 0 : 4));
4062
4063     case MINUS:
4064       if (mode == DImode)
4065         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4066                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4067                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4068                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4069                    ? 0 : 8));
4070
4071       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4072         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4073                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4074                           && arm_const_double_rtx (XEXP (x, 1))))
4075                      ? 0 : 8)
4076                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4077                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4078                         && arm_const_double_rtx (XEXP (x, 0))))
4079                    ? 0 : 8));
4080
4081       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4082             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4083             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4084           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4085                || subcode == ASHIFTRT || subcode == LSHIFTRT
4086                || subcode == ROTATE || subcode == ROTATERT
4087                || (subcode == MULT
4088                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4089                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4090                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4091               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4092               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4093                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4094               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4095         return 1;
4096       /* Fall through */
4097
4098     case PLUS:
4099       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4100         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4101                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4102                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4103                         && arm_const_double_rtx (XEXP (x, 1))))
4104                    ? 0 : 8));
4105
4106       /* Fall through */
4107     case AND: case XOR: case IOR:
4108       extra_cost = 0;
4109
4110       /* Normally the frame registers will be spilt into reg+const during
4111          reload, so it is a bad idea to combine them with other instructions,
4112          since then they might not be moved outside of loops.  As a compromise
4113          we allow integration with ops that have a constant as their second
4114          operand.  */
4115       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4116            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4117            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4118           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4119               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4120         extra_cost = 4;
4121
4122       if (mode == DImode)
4123         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4124                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4125                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4126                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4127                    ? 0 : 8));
4128
4129       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4130         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4131                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4132                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4133                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4134                    ? 0 : 4));
4135
4136       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4137         return (1 + extra_cost
4138                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4139                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4140                      || subcode == ROTATE || subcode == ROTATERT
4141                      || (subcode == MULT
4142                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4143                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4144                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4145                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4146                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4147                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4148                    ? 0 : 4));
4149
4150       return 8;
4151
4152     case MULT:
4153       /* This should have been handled by the CPU specific routines.  */
4154       gcc_unreachable ();
4155
4156     case TRUNCATE:
4157       if (arm_arch3m && mode == SImode
4158           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4159           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4160           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4161               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4162           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4163               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4164         return 8;
4165       return 99;
4166
4167     case NEG:
4168       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4169         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4170       /* Fall through */
4171     case NOT:
4172       if (mode == DImode)
4173         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4174
4175       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4176
4177     case IF_THEN_ELSE:
4178       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4179         return 14;
4180       return 2;
4181
4182     case COMPARE:
4183       return 1;
4184
4185     case ABS:
4186       return 4 + (mode == DImode ? 4 : 0);
4187
4188     case SIGN_EXTEND:
4189       if (GET_MODE (XEXP (x, 0)) == QImode)
4190         return (4 + (mode == DImode ? 4 : 0)
4191                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4192       /* Fall through */
4193     case ZERO_EXTEND:
4194       switch (GET_MODE (XEXP (x, 0)))
4195         {
4196         case QImode:
4197           return (1 + (mode == DImode ? 4 : 0)
4198                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4199
4200         case HImode:
4201           return (4 + (mode == DImode ? 4 : 0)
4202                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4203
4204         case SImode:
4205           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4206
4207         case V8QImode:
4208         case V4HImode:
4209         case V2SImode:
4210         case V4QImode:
4211         case V2HImode:
4212             return 1;
4213
4214         default:
4215           gcc_unreachable ();
4216         }
4217       gcc_unreachable ();
4218
4219     case CONST_INT:
4220       if (const_ok_for_arm (INTVAL (x)))
4221         return outer == SET ? 2 : -1;
4222       else if (outer == AND
4223                && const_ok_for_arm (~INTVAL (x)))
4224         return -1;
4225       else if ((outer == COMPARE
4226                 || outer == PLUS || outer == MINUS)
4227                && const_ok_for_arm (-INTVAL (x)))
4228         return -1;
4229       else
4230         return 5;
4231
4232     case CONST:
4233     case LABEL_REF:
4234     case SYMBOL_REF:
4235       return 6;
4236
4237     case CONST_DOUBLE:
4238       if (arm_const_double_rtx (x))
4239         return outer == SET ? 2 : -1;
4240       else if ((outer == COMPARE || outer == PLUS)
4241                && neg_const_double_rtx_ok_for_fpa (x))
4242         return -1;
4243       return 7;
4244
4245     default:
4246       return 99;
4247     }
4248 }
4249
4250 /* RTX costs when optimizing for size.  */
4251 static bool
4252 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4253 {
4254   enum machine_mode mode = GET_MODE (x);
4255
4256   if (TARGET_THUMB)
4257     {
4258       /* XXX TBD.  For now, use the standard costs.  */
4259       *total = thumb_rtx_costs (x, code, outer_code);
4260       return true;
4261     }
4262
4263   switch (code)
4264     {
4265     case MEM:
4266       /* A memory access costs 1 insn if the mode is small, or the address is
4267          a single register, otherwise it costs one insn per word.  */
4268       if (REG_P (XEXP (x, 0)))
4269         *total = COSTS_N_INSNS (1);
4270       else
4271         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4272       return true;
4273
4274     case DIV:
4275     case MOD:
4276     case UDIV:
4277     case UMOD:
4278       /* Needs a libcall, so it costs about this.  */
4279       *total = COSTS_N_INSNS (2);
4280       return false;
4281
4282     case ROTATE:
4283       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4284         {
4285           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4286           return true;
4287         }
4288       /* Fall through */
4289     case ROTATERT:
4290     case ASHIFT:
4291     case LSHIFTRT:
4292     case ASHIFTRT:
4293       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4294         {
4295           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4296           return true;
4297         }
4298       else if (mode == SImode)
4299         {
4300           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4301           /* Slightly disparage register shifts, but not by much.  */
4302           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4303             *total += 1 + rtx_cost (XEXP (x, 1), code);
4304           return true;
4305         }
4306
4307       /* Needs a libcall.  */
4308       *total = COSTS_N_INSNS (2);
4309       return false;
4310
4311     case MINUS:
4312       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4313         {
4314           *total = COSTS_N_INSNS (1);
4315           return false;
4316         }
4317
4318       if (mode == SImode)
4319         {
4320           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4321           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4322
4323           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4324               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4325               || subcode1 == ROTATE || subcode1 == ROTATERT
4326               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4327               || subcode1 == ASHIFTRT)
4328             {
4329               /* It's just the cost of the two operands.  */
4330               *total = 0;
4331               return false;
4332             }
4333
4334           *total = COSTS_N_INSNS (1);
4335           return false;
4336         }
4337
4338       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4339       return false;
4340
4341     case PLUS:
4342       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4343         {
4344           *total = COSTS_N_INSNS (1);
4345           return false;
4346         }
4347
4348       /* Fall through */
4349     case AND: case XOR: case IOR:
4350       if (mode == SImode)
4351         {
4352           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4353
4354           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4355               || subcode == LSHIFTRT || subcode == ASHIFTRT
4356               || (code == AND && subcode == NOT))
4357             {
4358               /* It's just the cost of the two operands.  */
4359               *total = 0;
4360               return false;
4361             }
4362         }
4363
4364       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4365       return false;
4366
4367     case MULT:
4368       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4369       return false;
4370
4371     case NEG:
4372       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4373         *total = COSTS_N_INSNS (1);
4374       /* Fall through */
4375     case NOT:
4376       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4377
4378       return false;
4379
4380     case IF_THEN_ELSE:
4381       *total = 0;
4382       return false;
4383
4384     case COMPARE:
4385       if (cc_register (XEXP (x, 0), VOIDmode))
4386         * total = 0;
4387       else
4388         *total = COSTS_N_INSNS (1);
4389       return false;
4390
4391     case ABS:
4392       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4393         *total = COSTS_N_INSNS (1);
4394       else
4395         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4396       return false;
4397
4398     case SIGN_EXTEND:
4399       *total = 0;
4400       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4401         {
4402           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4403             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4404         }
4405       if (mode == DImode)
4406         *total += COSTS_N_INSNS (1);
4407       return false;
4408
4409     case ZERO_EXTEND:
4410       *total = 0;
4411       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4412         {
4413           switch (GET_MODE (XEXP (x, 0)))
4414             {
4415             case QImode:
4416               *total += COSTS_N_INSNS (1);
4417               break;
4418
4419             case HImode:
4420               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4421
4422             case SImode:
4423               break;
4424
4425             default:
4426               *total += COSTS_N_INSNS (2);
4427             }
4428         }
4429
4430       if (mode == DImode)
4431         *total += COSTS_N_INSNS (1);
4432
4433       return false;
4434
4435     case CONST_INT:
4436       if (const_ok_for_arm (INTVAL (x)))
4437         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4438       else if (const_ok_for_arm (~INTVAL (x)))
4439         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4440       else if (const_ok_for_arm (-INTVAL (x)))
4441         {
4442           if (outer_code == COMPARE || outer_code == PLUS
4443               || outer_code == MINUS)
4444             *total = 0;
4445           else
4446             *total = COSTS_N_INSNS (1);
4447         }
4448       else
4449         *total = COSTS_N_INSNS (2);
4450       return true;
4451
4452     case CONST:
4453     case LABEL_REF:
4454     case SYMBOL_REF:
4455       *total = COSTS_N_INSNS (2);
4456       return true;
4457
4458     case CONST_DOUBLE:
4459       *total = COSTS_N_INSNS (4);
4460       return true;
4461
4462     default:
4463       if (mode != VOIDmode)
4464         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4465       else
4466         *total = COSTS_N_INSNS (4); /* How knows?  */
4467       return false;
4468     }
4469 }
4470
4471 /* RTX costs for cores with a slow MUL implementation.  */
4472
4473 static bool
4474 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4475 {
4476   enum machine_mode mode = GET_MODE (x);
4477
4478   if (TARGET_THUMB)
4479     {
4480       *total = thumb_rtx_costs (x, code, outer_code);
4481       return true;
4482     }
4483
4484   switch (code)
4485     {
4486     case MULT:
4487       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4488           || mode == DImode)
4489         {
4490           *total = 30;
4491           return true;
4492         }
4493
4494       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4495         {
4496           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4497                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4498           int cost, const_ok = const_ok_for_arm (i);
4499           int j, booth_unit_size;
4500
4501           /* Tune as appropriate.  */
4502           cost = const_ok ? 4 : 8;
4503           booth_unit_size = 2;
4504           for (j = 0; i && j < 32; j += booth_unit_size)
4505             {
4506               i >>= booth_unit_size;
4507               cost += 2;
4508             }
4509
4510           *total = cost;
4511           return true;
4512         }
4513
4514       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4515                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4516       return true;
4517
4518     default:
4519       *total = arm_rtx_costs_1 (x, code, outer_code);
4520       return true;
4521     }
4522 }
4523
4524
4525 /* RTX cost for cores with a fast multiply unit (M variants).  */
4526
4527 static bool
4528 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4529 {
4530   enum machine_mode mode = GET_MODE (x);
4531
4532   if (TARGET_THUMB)
4533     {
4534       *total = thumb_rtx_costs (x, code, outer_code);
4535       return true;
4536     }
4537
4538   switch (code)
4539     {
4540     case MULT:
4541       /* There is no point basing this on the tuning, since it is always the
4542          fast variant if it exists at all.  */
4543       if (mode == DImode
4544           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4545           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4546               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4547         {
4548           *total = 8;
4549           return true;
4550         }
4551
4552
4553       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4554           || mode == DImode)
4555         {
4556           *total = 30;
4557           return true;
4558         }
4559
4560       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4561         {
4562           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4563                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4564           int cost, const_ok = const_ok_for_arm (i);
4565           int j, booth_unit_size;
4566
4567           /* Tune as appropriate.  */
4568           cost = const_ok ? 4 : 8;
4569           booth_unit_size = 8;
4570           for (j = 0; i && j < 32; j += booth_unit_size)
4571             {
4572               i >>= booth_unit_size;
4573               cost += 2;
4574             }
4575
4576           *total = cost;
4577           return true;
4578         }
4579
4580       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4581                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4582       return true;
4583
4584     default:
4585       *total = arm_rtx_costs_1 (x, code, outer_code);
4586       return true;
4587     }
4588 }
4589
4590
4591 /* RTX cost for XScale CPUs.  */
4592
4593 static bool
4594 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4595 {
4596   enum machine_mode mode = GET_MODE (x);
4597
4598   if (TARGET_THUMB)
4599     {
4600       *total = thumb_rtx_costs (x, code, outer_code);
4601       return true;
4602     }
4603
4604   switch (code)
4605     {
4606     case MULT:
4607       /* There is no point basing this on the tuning, since it is always the
4608          fast variant if it exists at all.  */
4609       if (mode == DImode
4610           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4611           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4612               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4613         {
4614           *total = 8;
4615           return true;
4616         }
4617
4618
4619       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4620           || mode == DImode)
4621         {
4622           *total = 30;
4623           return true;
4624         }
4625
4626       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4627         {
4628           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4629                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4630           int cost, const_ok = const_ok_for_arm (i);
4631           unsigned HOST_WIDE_INT masked_const;
4632
4633           /* The cost will be related to two insns.
4634              First a load of the constant (MOV or LDR), then a multiply.  */
4635           cost = 2;
4636           if (! const_ok)
4637             cost += 1;      /* LDR is probably more expensive because
4638                                of longer result latency.  */
4639           masked_const = i & 0xffff8000;
4640           if (masked_const != 0 && masked_const != 0xffff8000)
4641             {
4642               masked_const = i & 0xf8000000;
4643               if (masked_const == 0 || masked_const == 0xf8000000)
4644                 cost += 1;
4645               else
4646                 cost += 2;
4647             }
4648           *total = cost;
4649           return true;
4650         }
4651
4652       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4653                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4654       return true;
4655
4656     case COMPARE:
4657       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4658          will stall until the multiplication is complete.  */
4659       if (GET_CODE (XEXP (x, 0)) == MULT)
4660         *total = 4 + rtx_cost (XEXP (x, 0), code);
4661       else
4662         *total = arm_rtx_costs_1 (x, code, outer_code);
4663       return true;
4664
4665     default:
4666       *total = arm_rtx_costs_1 (x, code, outer_code);
4667       return true;
4668     }
4669 }
4670
4671
4672 /* RTX costs for 9e (and later) cores.  */
4673
4674 static bool
4675 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4676 {
4677   enum machine_mode mode = GET_MODE (x);
4678   int nonreg_cost;
4679   int cost;
4680
4681   if (TARGET_THUMB)
4682     {
4683       switch (code)
4684         {
4685         case MULT:
4686           *total = COSTS_N_INSNS (3);
4687           return true;
4688
4689         default:
4690           *total = thumb_rtx_costs (x, code, outer_code);
4691           return true;
4692         }
4693     }
4694
4695   switch (code)
4696     {
4697     case MULT:
4698       /* There is no point basing this on the tuning, since it is always the
4699          fast variant if it exists at all.  */
4700       if (mode == DImode
4701           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4702           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4703               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4704         {
4705           *total = 3;
4706           return true;
4707         }
4708
4709
4710       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4711         {
4712           *total = 30;
4713           return true;
4714         }
4715       if (mode == DImode)
4716         {
4717           cost = 7;
4718           nonreg_cost = 8;
4719         }
4720       else
4721         {
4722           cost = 2;
4723           nonreg_cost = 4;
4724         }
4725
4726
4727       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4728                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4729       return true;
4730
4731     default:
4732       *total = arm_rtx_costs_1 (x, code, outer_code);
4733       return true;
4734     }
4735 }
4736 /* All address computations that can be done are free, but rtx cost returns
4737    the same for practically all of them.  So we weight the different types
4738    of address here in the order (most pref first):
4739    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4740 static inline int
4741 arm_arm_address_cost (rtx x)
4742 {
4743   enum rtx_code c  = GET_CODE (x);
4744
4745   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4746     return 0;
4747   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4748     return 10;
4749
4750   if (c == PLUS || c == MINUS)
4751     {
4752       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4753         return 2;
4754
4755       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4756         return 3;
4757
4758       return 4;
4759     }
4760
4761   return 6;
4762 }
4763
4764 static inline int
4765 arm_thumb_address_cost (rtx x)
4766 {
4767   enum rtx_code c  = GET_CODE (x);
4768
4769   if (c == REG)
4770     return 1;
4771   if (c == PLUS
4772       && GET_CODE (XEXP (x, 0)) == REG
4773       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4774     return 1;
4775
4776   return 2;
4777 }
4778
4779 static int
4780 arm_address_cost (rtx x)
4781 {
4782   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4783 }
4784
4785 static int
4786 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4787 {
4788   rtx i_pat, d_pat;
4789
4790   /* Some true dependencies can have a higher cost depending
4791      on precisely how certain input operands are used.  */
4792   if (arm_tune_xscale
4793       && REG_NOTE_KIND (link) == 0
4794       && recog_memoized (insn) >= 0
4795       && recog_memoized (dep) >= 0)
4796     {
4797       int shift_opnum = get_attr_shift (insn);
4798       enum attr_type attr_type = get_attr_type (dep);
4799
4800       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4801          operand for INSN.  If we have a shifted input operand and the
4802          instruction we depend on is another ALU instruction, then we may
4803          have to account for an additional stall.  */
4804       if (shift_opnum != 0
4805           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4806         {
4807           rtx shifted_operand;
4808           int opno;
4809
4810           /* Get the shifted operand.  */
4811           extract_insn (insn);
4812           shifted_operand = recog_data.operand[shift_opnum];
4813
4814           /* Iterate over all the operands in DEP.  If we write an operand
4815              that overlaps with SHIFTED_OPERAND, then we have increase the
4816              cost of this dependency.  */
4817           extract_insn (dep);
4818           preprocess_constraints ();
4819           for (opno = 0; opno < recog_data.n_operands; opno++)
4820             {
4821               /* We can ignore strict inputs.  */
4822               if (recog_data.operand_type[opno] == OP_IN)
4823                 continue;
4824
4825               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4826                                            shifted_operand))
4827                 return 2;
4828             }
4829         }
4830     }
4831
4832   /* XXX This is not strictly true for the FPA.  */
4833   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4834       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4835     return 0;
4836
4837   /* Call insns don't incur a stall, even if they follow a load.  */
4838   if (REG_NOTE_KIND (link) == 0
4839       && GET_CODE (insn) == CALL_INSN)
4840     return 1;
4841
4842   if ((i_pat = single_set (insn)) != NULL
4843       && GET_CODE (SET_SRC (i_pat)) == MEM
4844       && (d_pat = single_set (dep)) != NULL
4845       && GET_CODE (SET_DEST (d_pat)) == MEM)
4846     {
4847       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4848       /* This is a load after a store, there is no conflict if the load reads
4849          from a cached area.  Assume that loads from the stack, and from the
4850          constant pool are cached, and that others will miss.  This is a
4851          hack.  */
4852
4853       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4854           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4855           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4856           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4857         return 1;
4858     }
4859
4860   return cost;
4861 }
4862
4863 static int fp_consts_inited = 0;
4864
4865 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4866 static const char * const strings_fp[8] =
4867 {
4868   "0",   "1",   "2",   "3",
4869   "4",   "5",   "0.5", "10"
4870 };
4871
4872 static REAL_VALUE_TYPE values_fp[8];
4873
4874 static void
4875 init_fp_table (void)
4876 {
4877   int i;
4878   REAL_VALUE_TYPE r;
4879
4880   if (TARGET_VFP)
4881     fp_consts_inited = 1;
4882   else
4883     fp_consts_inited = 8;
4884
4885   for (i = 0; i < fp_consts_inited; i++)
4886     {
4887       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4888       values_fp[i] = r;
4889     }
4890 }
4891
4892 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4893 int
4894 arm_const_double_rtx (rtx x)
4895 {
4896   REAL_VALUE_TYPE r;
4897   int i;
4898
4899   if (!fp_consts_inited)
4900     init_fp_table ();
4901
4902   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4903   if (REAL_VALUE_MINUS_ZERO (r))
4904     return 0;
4905
4906   for (i = 0; i < fp_consts_inited; i++)
4907     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4908       return 1;
4909
4910   return 0;
4911 }
4912
4913 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4914 int
4915 neg_const_double_rtx_ok_for_fpa (rtx x)
4916 {
4917   REAL_VALUE_TYPE r;
4918   int i;
4919
4920   if (!fp_consts_inited)
4921     init_fp_table ();
4922
4923   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4924   r = REAL_VALUE_NEGATE (r);
4925   if (REAL_VALUE_MINUS_ZERO (r))
4926     return 0;
4927
4928   for (i = 0; i < 8; i++)
4929     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4930       return 1;
4931
4932   return 0;
4933 }
4934 \f
4935 /* Predicates for `match_operand' and `match_operator'.  */
4936
4937 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4938 int
4939 cirrus_memory_offset (rtx op)
4940 {
4941   /* Reject eliminable registers.  */
4942   if (! (reload_in_progress || reload_completed)
4943       && (   reg_mentioned_p (frame_pointer_rtx, op)
4944           || reg_mentioned_p (arg_pointer_rtx, op)
4945           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4946           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4947           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4948           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4949     return 0;
4950
4951   if (GET_CODE (op) == MEM)
4952     {
4953       rtx ind;
4954
4955       ind = XEXP (op, 0);
4956
4957       /* Match: (mem (reg)).  */
4958       if (GET_CODE (ind) == REG)
4959         return 1;
4960
4961       /* Match:
4962          (mem (plus (reg)
4963                     (const))).  */
4964       if (GET_CODE (ind) == PLUS
4965           && GET_CODE (XEXP (ind, 0)) == REG
4966           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4967           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4968         return 1;
4969     }
4970
4971   return 0;
4972 }
4973
4974 /* Return TRUE if OP is a valid coprocessor memory address pattern.
4975    WB if true if writeback address modes are allowed.  */
4976
4977 int
4978 arm_coproc_mem_operand (rtx op, bool wb)
4979 {
4980   rtx ind;
4981
4982   /* Reject eliminable registers.  */
4983   if (! (reload_in_progress || reload_completed)
4984       && (   reg_mentioned_p (frame_pointer_rtx, op)
4985           || reg_mentioned_p (arg_pointer_rtx, op)
4986           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4987           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4988           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4989           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4990     return FALSE;
4991
4992   /* Constants are converted into offsets from labels.  */
4993   if (GET_CODE (op) != MEM)
4994     return FALSE;
4995
4996   ind = XEXP (op, 0);
4997
4998   if (reload_completed
4999       && (GET_CODE (ind) == LABEL_REF
5000           || (GET_CODE (ind) == CONST
5001               && GET_CODE (XEXP (ind, 0)) == PLUS
5002               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5003               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5004     return TRUE;
5005
5006   /* Match: (mem (reg)).  */
5007   if (GET_CODE (ind) == REG)
5008     return arm_address_register_rtx_p (ind, 0);
5009
5010   /* Autoincremment addressing modes.  */
5011   if (wb
5012       && (GET_CODE (ind) == PRE_INC
5013           || GET_CODE (ind) == POST_INC
5014           || GET_CODE (ind) == PRE_DEC
5015           || GET_CODE (ind) == POST_DEC))
5016     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5017
5018   if (wb
5019       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5020       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5021       && GET_CODE (XEXP (ind, 1)) == PLUS
5022       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5023     ind = XEXP (ind, 1);
5024
5025   /* Match:
5026      (plus (reg)
5027            (const)).  */
5028   if (GET_CODE (ind) == PLUS
5029       && GET_CODE (XEXP (ind, 0)) == REG
5030       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5031       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5032       && INTVAL (XEXP (ind, 1)) > -1024
5033       && INTVAL (XEXP (ind, 1)) <  1024
5034       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5035     return TRUE;
5036
5037   return FALSE;
5038 }
5039
5040 /* Return true if X is a register that will be eliminated later on.  */
5041 int
5042 arm_eliminable_register (rtx x)
5043 {
5044   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5045                        || REGNO (x) == ARG_POINTER_REGNUM
5046                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5047                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5048 }
5049
5050 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5051    VFP registers.  Otherwise return NO_REGS.  */
5052
5053 enum reg_class
5054 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5055 {
5056   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5057     return NO_REGS;
5058
5059   return GENERAL_REGS;
5060 }
5061
5062 /* Values which must be returned in the most-significant end of the return
5063    register.  */
5064
5065 static bool
5066 arm_return_in_msb (tree valtype)
5067 {
5068   return (TARGET_AAPCS_BASED
5069           && BYTES_BIG_ENDIAN
5070           && (AGGREGATE_TYPE_P (valtype)
5071               || TREE_CODE (valtype) == COMPLEX_TYPE));
5072 }
5073
5074 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5075    Use by the Cirrus Maverick code which has to workaround
5076    a hardware bug triggered by such instructions.  */
5077 static bool
5078 arm_memory_load_p (rtx insn)
5079 {
5080   rtx body, lhs, rhs;;
5081
5082   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5083     return false;
5084
5085   body = PATTERN (insn);
5086
5087   if (GET_CODE (body) != SET)
5088     return false;
5089
5090   lhs = XEXP (body, 0);
5091   rhs = XEXP (body, 1);
5092
5093   lhs = REG_OR_SUBREG_RTX (lhs);
5094
5095   /* If the destination is not a general purpose
5096      register we do not have to worry.  */
5097   if (GET_CODE (lhs) != REG
5098       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5099     return false;
5100
5101   /* As well as loads from memory we also have to react
5102      to loads of invalid constants which will be turned
5103      into loads from the minipool.  */
5104   return (GET_CODE (rhs) == MEM
5105           || GET_CODE (rhs) == SYMBOL_REF
5106           || note_invalid_constants (insn, -1, false));
5107 }
5108
5109 /* Return TRUE if INSN is a Cirrus instruction.  */
5110 static bool
5111 arm_cirrus_insn_p (rtx insn)
5112 {
5113   enum attr_cirrus attr;
5114
5115   /* get_attr cannot accept USE or CLOBBER.  */
5116   if (!insn
5117       || GET_CODE (insn) != INSN
5118       || GET_CODE (PATTERN (insn)) == USE
5119       || GET_CODE (PATTERN (insn)) == CLOBBER)
5120     return 0;
5121
5122   attr = get_attr_cirrus (insn);
5123
5124   return attr != CIRRUS_NOT;
5125 }
5126
5127 /* Cirrus reorg for invalid instruction combinations.  */
5128 static void
5129 cirrus_reorg (rtx first)
5130 {
5131   enum attr_cirrus attr;
5132   rtx body = PATTERN (first);
5133   rtx t;
5134   int nops;
5135
5136   /* Any branch must be followed by 2 non Cirrus instructions.  */
5137   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5138     {
5139       nops = 0;
5140       t = next_nonnote_insn (first);
5141
5142       if (arm_cirrus_insn_p (t))
5143         ++ nops;
5144
5145       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5146         ++ nops;
5147
5148       while (nops --)
5149         emit_insn_after (gen_nop (), first);
5150
5151       return;
5152     }
5153
5154   /* (float (blah)) is in parallel with a clobber.  */
5155   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5156     body = XVECEXP (body, 0, 0);
5157
5158   if (GET_CODE (body) == SET)
5159     {
5160       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5161
5162       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5163          be followed by a non Cirrus insn.  */
5164       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5165         {
5166           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5167             emit_insn_after (gen_nop (), first);
5168
5169           return;
5170         }
5171       else if (arm_memory_load_p (first))
5172         {
5173           unsigned int arm_regno;
5174
5175           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5176              ldr/cfmv64hr combination where the Rd field is the same
5177              in both instructions must be split with a non Cirrus
5178              insn.  Example:
5179
5180              ldr r0, blah
5181              nop
5182              cfmvsr mvf0, r0.  */
5183
5184           /* Get Arm register number for ldr insn.  */
5185           if (GET_CODE (lhs) == REG)
5186             arm_regno = REGNO (lhs);
5187           else
5188             {
5189               gcc_assert (GET_CODE (rhs) == REG);
5190               arm_regno = REGNO (rhs);
5191             }
5192
5193           /* Next insn.  */
5194           first = next_nonnote_insn (first);
5195
5196           if (! arm_cirrus_insn_p (first))
5197             return;
5198
5199           body = PATTERN (first);
5200
5201           /* (float (blah)) is in parallel with a clobber.  */
5202           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5203             body = XVECEXP (body, 0, 0);
5204
5205           if (GET_CODE (body) == FLOAT)
5206             body = XEXP (body, 0);
5207
5208           if (get_attr_cirrus (first) == CIRRUS_MOVE
5209               && GET_CODE (XEXP (body, 1)) == REG
5210               && arm_regno == REGNO (XEXP (body, 1)))
5211             emit_insn_after (gen_nop (), first);
5212
5213           return;
5214         }
5215     }
5216
5217   /* get_attr cannot accept USE or CLOBBER.  */
5218   if (!first
5219       || GET_CODE (first) != INSN
5220       || GET_CODE (PATTERN (first)) == USE
5221       || GET_CODE (PATTERN (first)) == CLOBBER)
5222     return;
5223
5224   attr = get_attr_cirrus (first);
5225
5226   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5227      must be followed by a non-coprocessor instruction.  */
5228   if (attr == CIRRUS_COMPARE)
5229     {
5230       nops = 0;
5231
5232       t = next_nonnote_insn (first);
5233
5234       if (arm_cirrus_insn_p (t))
5235         ++ nops;
5236
5237       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5238         ++ nops;
5239
5240       while (nops --)
5241         emit_insn_after (gen_nop (), first);
5242
5243       return;
5244     }
5245 }
5246
5247 /* Return TRUE if X references a SYMBOL_REF.  */
5248 int
5249 symbol_mentioned_p (rtx x)
5250 {
5251   const char * fmt;
5252   int i;
5253
5254   if (GET_CODE (x) == SYMBOL_REF)
5255     return 1;
5256
5257   fmt = GET_RTX_FORMAT (GET_CODE (x));
5258
5259   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5260     {
5261       if (fmt[i] == 'E')
5262         {
5263           int j;
5264
5265           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5266             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5267               return 1;
5268         }
5269       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5270         return 1;
5271     }
5272
5273   return 0;
5274 }
5275
5276 /* Return TRUE if X references a LABEL_REF.  */
5277 int
5278 label_mentioned_p (rtx x)
5279 {
5280   const char * fmt;
5281   int i;
5282
5283   if (GET_CODE (x) == LABEL_REF)
5284     return 1;
5285
5286   fmt = GET_RTX_FORMAT (GET_CODE (x));
5287   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5288     {
5289       if (fmt[i] == 'E')
5290         {
5291           int j;
5292
5293           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5294             if (label_mentioned_p (XVECEXP (x, i, j)))
5295               return 1;
5296         }
5297       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5298         return 1;
5299     }
5300
5301   return 0;
5302 }
5303
5304 enum rtx_code
5305 minmax_code (rtx x)
5306 {
5307   enum rtx_code code = GET_CODE (x);
5308
5309   switch (code)
5310     {
5311     case SMAX:
5312       return GE;
5313     case SMIN:
5314       return LE;
5315     case UMIN:
5316       return LEU;
5317     case UMAX:
5318       return GEU;
5319     default:
5320       gcc_unreachable ();
5321     }
5322 }
5323
5324 /* Return 1 if memory locations are adjacent.  */
5325 int
5326 adjacent_mem_locations (rtx a, rtx b)
5327 {
5328   /* We don't guarantee to preserve the order of these memory refs.  */
5329   if (volatile_refs_p (a) || volatile_refs_p (b))
5330     return 0;
5331
5332   if ((GET_CODE (XEXP (a, 0)) == REG
5333        || (GET_CODE (XEXP (a, 0)) == PLUS
5334            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5335       && (GET_CODE (XEXP (b, 0)) == REG
5336           || (GET_CODE (XEXP (b, 0)) == PLUS
5337               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5338     {
5339       HOST_WIDE_INT val0 = 0, val1 = 0;
5340       rtx reg0, reg1;
5341       int val_diff;
5342
5343       if (GET_CODE (XEXP (a, 0)) == PLUS)
5344         {
5345           reg0 = XEXP (XEXP (a, 0), 0);
5346           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5347         }
5348       else
5349         reg0 = XEXP (a, 0);
5350
5351       if (GET_CODE (XEXP (b, 0)) == PLUS)
5352         {
5353           reg1 = XEXP (XEXP (b, 0), 0);
5354           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5355         }
5356       else
5357         reg1 = XEXP (b, 0);
5358
5359       /* Don't accept any offset that will require multiple
5360          instructions to handle, since this would cause the
5361          arith_adjacentmem pattern to output an overlong sequence.  */
5362       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5363         return 0;
5364
5365       /* Don't allow an eliminable register: register elimination can make
5366          the offset too large.  */
5367       if (arm_eliminable_register (reg0))
5368         return 0;
5369
5370       val_diff = val1 - val0;
5371
5372       if (arm_ld_sched)
5373         {
5374           /* If the target has load delay slots, then there's no benefit
5375              to using an ldm instruction unless the offset is zero and
5376              we are optimizing for size.  */
5377           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5378                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5379                   && (val_diff == 4 || val_diff == -4));
5380         }
5381
5382       return ((REGNO (reg0) == REGNO (reg1))
5383               && (val_diff == 4 || val_diff == -4));
5384     }
5385
5386   return 0;
5387 }
5388
5389 int
5390 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5391                         HOST_WIDE_INT *load_offset)
5392 {
5393   int unsorted_regs[4];
5394   HOST_WIDE_INT unsorted_offsets[4];
5395   int order[4];
5396   int base_reg = -1;
5397   int i;
5398
5399   /* Can only handle 2, 3, or 4 insns at present,
5400      though could be easily extended if required.  */
5401   gcc_assert (nops >= 2 && nops <= 4);
5402
5403   /* Loop over the operands and check that the memory references are
5404      suitable (i.e. immediate offsets from the same base register).  At
5405      the same time, extract the target register, and the memory
5406      offsets.  */
5407   for (i = 0; i < nops; i++)
5408     {
5409       rtx reg;
5410       rtx offset;
5411
5412       /* Convert a subreg of a mem into the mem itself.  */
5413       if (GET_CODE (operands[nops + i]) == SUBREG)
5414         operands[nops + i] = alter_subreg (operands + (nops + i));
5415
5416       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5417
5418       /* Don't reorder volatile memory references; it doesn't seem worth
5419          looking for the case where the order is ok anyway.  */
5420       if (MEM_VOLATILE_P (operands[nops + i]))
5421         return 0;
5422
5423       offset = const0_rtx;
5424
5425       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5426            || (GET_CODE (reg) == SUBREG
5427                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5428           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5429               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5430                    == REG)
5431                   || (GET_CODE (reg) == SUBREG
5432                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5433               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5434                   == CONST_INT)))
5435         {
5436           if (i == 0)
5437             {
5438               base_reg = REGNO (reg);
5439               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5440                                   ? REGNO (operands[i])
5441                                   : REGNO (SUBREG_REG (operands[i])));
5442               order[0] = 0;
5443             }
5444           else
5445             {
5446               if (base_reg != (int) REGNO (reg))
5447                 /* Not addressed from the same base register.  */
5448                 return 0;
5449
5450               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5451                                   ? REGNO (operands[i])
5452                                   : REGNO (SUBREG_REG (operands[i])));
5453               if (unsorted_regs[i] < unsorted_regs[order[0]])
5454                 order[0] = i;
5455             }
5456
5457           /* If it isn't an integer register, or if it overwrites the
5458              base register but isn't the last insn in the list, then
5459              we can't do this.  */
5460           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5461               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5462             return 0;
5463
5464           unsorted_offsets[i] = INTVAL (offset);
5465         }
5466       else
5467         /* Not a suitable memory address.  */
5468         return 0;
5469     }
5470
5471   /* All the useful information has now been extracted from the
5472      operands into unsorted_regs and unsorted_offsets; additionally,
5473      order[0] has been set to the lowest numbered register in the
5474      list.  Sort the registers into order, and check that the memory
5475      offsets are ascending and adjacent.  */
5476
5477   for (i = 1; i < nops; i++)
5478     {
5479       int j;
5480
5481       order[i] = order[i - 1];
5482       for (j = 0; j < nops; j++)
5483         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5484             && (order[i] == order[i - 1]
5485                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5486           order[i] = j;
5487
5488       /* Have we found a suitable register? if not, one must be used more
5489          than once.  */
5490       if (order[i] == order[i - 1])
5491         return 0;
5492
5493       /* Is the memory address adjacent and ascending? */
5494       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5495         return 0;
5496     }
5497
5498   if (base)
5499     {
5500       *base = base_reg;
5501
5502       for (i = 0; i < nops; i++)
5503         regs[i] = unsorted_regs[order[i]];
5504
5505       *load_offset = unsorted_offsets[order[0]];
5506     }
5507
5508   if (unsorted_offsets[order[0]] == 0)
5509     return 1; /* ldmia */
5510
5511   if (unsorted_offsets[order[0]] == 4)
5512     return 2; /* ldmib */
5513
5514   if (unsorted_offsets[order[nops - 1]] == 0)
5515     return 3; /* ldmda */
5516
5517   if (unsorted_offsets[order[nops - 1]] == -4)
5518     return 4; /* ldmdb */
5519
5520   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5521      if the offset isn't small enough.  The reason 2 ldrs are faster
5522      is because these ARMs are able to do more than one cache access
5523      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5524      whilst the ARM8 has a double bandwidth cache.  This means that
5525      these cores can do both an instruction fetch and a data fetch in
5526      a single cycle, so the trick of calculating the address into a
5527      scratch register (one of the result regs) and then doing a load
5528      multiple actually becomes slower (and no smaller in code size).
5529      That is the transformation
5530
5531         ldr     rd1, [rbase + offset]
5532         ldr     rd2, [rbase + offset + 4]
5533
5534      to
5535
5536         add     rd1, rbase, offset
5537         ldmia   rd1, {rd1, rd2}
5538
5539      produces worse code -- '3 cycles + any stalls on rd2' instead of
5540      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5541      access per cycle, the first sequence could never complete in less
5542      than 6 cycles, whereas the ldm sequence would only take 5 and
5543      would make better use of sequential accesses if not hitting the
5544      cache.
5545
5546      We cheat here and test 'arm_ld_sched' which we currently know to
5547      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5548      changes, then the test below needs to be reworked.  */
5549   if (nops == 2 && arm_ld_sched)
5550     return 0;
5551
5552   /* Can't do it without setting up the offset, only do this if it takes
5553      no more than one insn.  */
5554   return (const_ok_for_arm (unsorted_offsets[order[0]])
5555           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5556 }
5557
5558 const char *
5559 emit_ldm_seq (rtx *operands, int nops)
5560 {
5561   int regs[4];
5562   int base_reg;
5563   HOST_WIDE_INT offset;
5564   char buf[100];
5565   int i;
5566
5567   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5568     {
5569     case 1:
5570       strcpy (buf, "ldm%?ia\t");
5571       break;
5572
5573     case 2:
5574       strcpy (buf, "ldm%?ib\t");
5575       break;
5576
5577     case 3:
5578       strcpy (buf, "ldm%?da\t");
5579       break;
5580
5581     case 4:
5582       strcpy (buf, "ldm%?db\t");
5583       break;
5584
5585     case 5:
5586       if (offset >= 0)
5587         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5588                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5589                  (long) offset);
5590       else
5591         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5592                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5593                  (long) -offset);
5594       output_asm_insn (buf, operands);
5595       base_reg = regs[0];
5596       strcpy (buf, "ldm%?ia\t");
5597       break;
5598
5599     default:
5600       gcc_unreachable ();
5601     }
5602
5603   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5604            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5605
5606   for (i = 1; i < nops; i++)
5607     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5608              reg_names[regs[i]]);
5609
5610   strcat (buf, "}\t%@ phole ldm");
5611
5612   output_asm_insn (buf, operands);
5613   return "";
5614 }
5615
5616 int
5617 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5618                          HOST_WIDE_INT * load_offset)
5619 {
5620   int unsorted_regs[4];
5621   HOST_WIDE_INT unsorted_offsets[4];
5622   int order[4];
5623   int base_reg = -1;
5624   int i;
5625
5626   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5627      extended if required.  */
5628   gcc_assert (nops >= 2 && nops <= 4);
5629
5630   /* Loop over the operands and check that the memory references are
5631      suitable (i.e. immediate offsets from the same base register).  At
5632      the same time, extract the target register, and the memory
5633      offsets.  */
5634   for (i = 0; i < nops; i++)
5635     {
5636       rtx reg;
5637       rtx offset;
5638
5639       /* Convert a subreg of a mem into the mem itself.  */
5640       if (GET_CODE (operands[nops + i]) == SUBREG)
5641         operands[nops + i] = alter_subreg (operands + (nops + i));
5642
5643       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5644
5645       /* Don't reorder volatile memory references; it doesn't seem worth
5646          looking for the case where the order is ok anyway.  */
5647       if (MEM_VOLATILE_P (operands[nops + i]))
5648         return 0;
5649
5650       offset = const0_rtx;
5651
5652       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5653            || (GET_CODE (reg) == SUBREG
5654                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5655           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5656               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5657                    == REG)
5658                   || (GET_CODE (reg) == SUBREG
5659                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5660               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5661                   == CONST_INT)))
5662         {
5663           if (i == 0)
5664             {
5665               base_reg = REGNO (reg);
5666               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5667                                   ? REGNO (operands[i])
5668                                   : REGNO (SUBREG_REG (operands[i])));
5669               order[0] = 0;
5670             }
5671           else
5672             {
5673               if (base_reg != (int) REGNO (reg))
5674                 /* Not addressed from the same base register.  */
5675                 return 0;
5676
5677               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5678                                   ? REGNO (operands[i])
5679                                   : REGNO (SUBREG_REG (operands[i])));
5680               if (unsorted_regs[i] < unsorted_regs[order[0]])
5681                 order[0] = i;
5682             }
5683
5684           /* If it isn't an integer register, then we can't do this.  */
5685           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5686             return 0;
5687
5688           unsorted_offsets[i] = INTVAL (offset);
5689         }
5690       else
5691         /* Not a suitable memory address.  */
5692         return 0;
5693     }
5694
5695   /* All the useful information has now been extracted from the
5696      operands into unsorted_regs and unsorted_offsets; additionally,
5697      order[0] has been set to the lowest numbered register in the
5698      list.  Sort the registers into order, and check that the memory
5699      offsets are ascending and adjacent.  */
5700
5701   for (i = 1; i < nops; i++)
5702     {
5703       int j;
5704
5705       order[i] = order[i - 1];
5706       for (j = 0; j < nops; j++)
5707         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5708             && (order[i] == order[i - 1]
5709                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5710           order[i] = j;
5711
5712       /* Have we found a suitable register? if not, one must be used more
5713          than once.  */
5714       if (order[i] == order[i - 1])
5715         return 0;
5716
5717       /* Is the memory address adjacent and ascending? */
5718       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5719         return 0;
5720     }
5721
5722   if (base)
5723     {
5724       *base = base_reg;
5725
5726       for (i = 0; i < nops; i++)
5727         regs[i] = unsorted_regs[order[i]];
5728
5729       *load_offset = unsorted_offsets[order[0]];
5730     }
5731
5732   if (unsorted_offsets[order[0]] == 0)
5733     return 1; /* stmia */
5734
5735   if (unsorted_offsets[order[0]] == 4)
5736     return 2; /* stmib */
5737
5738   if (unsorted_offsets[order[nops - 1]] == 0)
5739     return 3; /* stmda */
5740
5741   if (unsorted_offsets[order[nops - 1]] == -4)
5742     return 4; /* stmdb */
5743
5744   return 0;
5745 }
5746
5747 const char *
5748 emit_stm_seq (rtx *operands, int nops)
5749 {
5750   int regs[4];
5751   int base_reg;
5752   HOST_WIDE_INT offset;
5753   char buf[100];
5754   int i;
5755
5756   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5757     {
5758     case 1:
5759       strcpy (buf, "stm%?ia\t");
5760       break;
5761
5762     case 2:
5763       strcpy (buf, "stm%?ib\t");
5764       break;
5765
5766     case 3:
5767       strcpy (buf, "stm%?da\t");
5768       break;
5769
5770     case 4:
5771       strcpy (buf, "stm%?db\t");
5772       break;
5773
5774     default:
5775       gcc_unreachable ();
5776     }
5777
5778   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5779            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5780
5781   for (i = 1; i < nops; i++)
5782     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5783              reg_names[regs[i]]);
5784
5785   strcat (buf, "}\t%@ phole stm");
5786
5787   output_asm_insn (buf, operands);
5788   return "";
5789 }
5790
5791 \f
5792 /* Routines for use in generating RTL.  */
5793
5794 rtx
5795 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5796                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5797 {
5798   HOST_WIDE_INT offset = *offsetp;
5799   int i = 0, j;
5800   rtx result;
5801   int sign = up ? 1 : -1;
5802   rtx mem, addr;
5803
5804   /* XScale has load-store double instructions, but they have stricter
5805      alignment requirements than load-store multiple, so we cannot
5806      use them.
5807
5808      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5809      the pipeline until completion.
5810
5811         NREGS           CYCLES
5812           1               3
5813           2               4
5814           3               5
5815           4               6
5816
5817      An ldr instruction takes 1-3 cycles, but does not block the
5818      pipeline.
5819
5820         NREGS           CYCLES
5821           1              1-3
5822           2              2-6
5823           3              3-9
5824           4              4-12
5825
5826      Best case ldr will always win.  However, the more ldr instructions
5827      we issue, the less likely we are to be able to schedule them well.
5828      Using ldr instructions also increases code size.
5829
5830      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5831      for counts of 3 or 4 regs.  */
5832   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5833     {
5834       rtx seq;
5835
5836       start_sequence ();
5837
5838       for (i = 0; i < count; i++)
5839         {
5840           addr = plus_constant (from, i * 4 * sign);
5841           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5842           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5843           offset += 4 * sign;
5844         }
5845
5846       if (write_back)
5847         {
5848           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5849           *offsetp = offset;
5850         }
5851
5852       seq = get_insns ();
5853       end_sequence ();
5854
5855       return seq;
5856     }
5857
5858   result = gen_rtx_PARALLEL (VOIDmode,
5859                              rtvec_alloc (count + (write_back ? 1 : 0)));
5860   if (write_back)
5861     {
5862       XVECEXP (result, 0, 0)
5863         = gen_rtx_SET (GET_MODE (from), from,
5864                        plus_constant (from, count * 4 * sign));
5865       i = 1;
5866       count++;
5867     }
5868
5869   for (j = 0; i < count; i++, j++)
5870     {
5871       addr = plus_constant (from, j * 4 * sign);
5872       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5873       XVECEXP (result, 0, i)
5874         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5875       offset += 4 * sign;
5876     }
5877
5878   if (write_back)
5879     *offsetp = offset;
5880
5881   return result;
5882 }
5883
5884 rtx
5885 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5886                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5887 {
5888   HOST_WIDE_INT offset = *offsetp;
5889   int i = 0, j;
5890   rtx result;
5891   int sign = up ? 1 : -1;
5892   rtx mem, addr;
5893
5894   /* See arm_gen_load_multiple for discussion of
5895      the pros/cons of ldm/stm usage for XScale.  */
5896   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5897     {
5898       rtx seq;
5899
5900       start_sequence ();
5901
5902       for (i = 0; i < count; i++)
5903         {
5904           addr = plus_constant (to, i * 4 * sign);
5905           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5906           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5907           offset += 4 * sign;
5908         }
5909
5910       if (write_back)
5911         {
5912           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5913           *offsetp = offset;
5914         }
5915
5916       seq = get_insns ();
5917       end_sequence ();
5918
5919       return seq;
5920     }
5921
5922   result = gen_rtx_PARALLEL (VOIDmode,
5923                              rtvec_alloc (count + (write_back ? 1 : 0)));
5924   if (write_back)
5925     {
5926       XVECEXP (result, 0, 0)
5927         = gen_rtx_SET (GET_MODE (to), to,
5928                        plus_constant (to, count * 4 * sign));
5929       i = 1;
5930       count++;
5931     }
5932
5933   for (j = 0; i < count; i++, j++)
5934     {
5935       addr = plus_constant (to, j * 4 * sign);
5936       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5937       XVECEXP (result, 0, i)
5938         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5939       offset += 4 * sign;
5940     }
5941
5942   if (write_back)
5943     *offsetp = offset;
5944
5945   return result;
5946 }
5947
5948 int
5949 arm_gen_movmemqi (rtx *operands)
5950 {
5951   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5952   HOST_WIDE_INT srcoffset, dstoffset;
5953   int i;
5954   rtx src, dst, srcbase, dstbase;
5955   rtx part_bytes_reg = NULL;
5956   rtx mem;
5957
5958   if (GET_CODE (operands[2]) != CONST_INT
5959       || GET_CODE (operands[3]) != CONST_INT
5960       || INTVAL (operands[2]) > 64
5961       || INTVAL (operands[3]) & 3)
5962     return 0;
5963
5964   dstbase = operands[0];
5965   srcbase = operands[1];
5966
5967   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5968   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5969
5970   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5971   out_words_to_go = INTVAL (operands[2]) / 4;
5972   last_bytes = INTVAL (operands[2]) & 3;
5973   dstoffset = srcoffset = 0;
5974
5975   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5976     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5977
5978   for (i = 0; in_words_to_go >= 2; i+=4)
5979     {
5980       if (in_words_to_go > 4)
5981         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5982                                           srcbase, &srcoffset));
5983       else
5984         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5985                                           FALSE, srcbase, &srcoffset));
5986
5987       if (out_words_to_go)
5988         {
5989           if (out_words_to_go > 4)
5990             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5991                                                dstbase, &dstoffset));
5992           else if (out_words_to_go != 1)
5993             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5994                                                dst, TRUE,
5995                                                (last_bytes == 0
5996                                                 ? FALSE : TRUE),
5997                                                dstbase, &dstoffset));
5998           else
5999             {
6000               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6001               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6002               if (last_bytes != 0)
6003                 {
6004                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6005                   dstoffset += 4;
6006                 }
6007             }
6008         }
6009
6010       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6011       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6012     }
6013
6014   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6015   if (out_words_to_go)
6016     {
6017       rtx sreg;
6018
6019       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6020       sreg = copy_to_reg (mem);
6021
6022       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6023       emit_move_insn (mem, sreg);
6024       in_words_to_go--;
6025
6026       gcc_assert (!in_words_to_go);     /* Sanity check */
6027     }
6028
6029   if (in_words_to_go)
6030     {
6031       gcc_assert (in_words_to_go > 0);
6032
6033       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6034       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6035     }
6036
6037   gcc_assert (!last_bytes || part_bytes_reg);
6038
6039   if (BYTES_BIG_ENDIAN && last_bytes)
6040     {
6041       rtx tmp = gen_reg_rtx (SImode);
6042
6043       /* The bytes we want are in the top end of the word.  */
6044       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6045                               GEN_INT (8 * (4 - last_bytes))));
6046       part_bytes_reg = tmp;
6047
6048       while (last_bytes)
6049         {
6050           mem = adjust_automodify_address (dstbase, QImode,
6051                                            plus_constant (dst, last_bytes - 1),
6052                                            dstoffset + last_bytes - 1);
6053           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6054
6055           if (--last_bytes)
6056             {
6057               tmp = gen_reg_rtx (SImode);
6058               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6059               part_bytes_reg = tmp;
6060             }
6061         }
6062
6063     }
6064   else
6065     {
6066       if (last_bytes > 1)
6067         {
6068           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6069           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6070           last_bytes -= 2;
6071           if (last_bytes)
6072             {
6073               rtx tmp = gen_reg_rtx (SImode);
6074               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6075               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6076               part_bytes_reg = tmp;
6077               dstoffset += 2;
6078             }
6079         }
6080
6081       if (last_bytes)
6082         {
6083           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6084           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6085         }
6086     }
6087
6088   return 1;
6089 }
6090
6091 /* Generate a memory reference for a half word, such that it will be loaded
6092    into the top 16 bits of the word.  We can assume that the address is
6093    known to be alignable and of the form reg, or plus (reg, const).  */
6094
6095 rtx
6096 arm_gen_rotated_half_load (rtx memref)
6097 {
6098   HOST_WIDE_INT offset = 0;
6099   rtx base = XEXP (memref, 0);
6100
6101   if (GET_CODE (base) == PLUS)
6102     {
6103       offset = INTVAL (XEXP (base, 1));
6104       base = XEXP (base, 0);
6105     }
6106
6107   /* If we aren't allowed to generate unaligned addresses, then fail.  */
6108   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
6109     return NULL;
6110
6111   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6112
6113   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6114     return base;
6115
6116   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6117 }
6118
6119 /* Select a dominance comparison mode if possible for a test of the general
6120    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6121    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6122    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6123    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6124    In all cases OP will be either EQ or NE, but we don't need to know which
6125    here.  If we are unable to support a dominance comparison we return
6126    CC mode.  This will then fail to match for the RTL expressions that
6127    generate this call.  */
6128 enum machine_mode
6129 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6130 {
6131   enum rtx_code cond1, cond2;
6132   int swapped = 0;
6133
6134   /* Currently we will probably get the wrong result if the individual
6135      comparisons are not simple.  This also ensures that it is safe to
6136      reverse a comparison if necessary.  */
6137   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6138        != CCmode)
6139       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6140           != CCmode))
6141     return CCmode;
6142
6143   /* The if_then_else variant of this tests the second condition if the
6144      first passes, but is true if the first fails.  Reverse the first
6145      condition to get a true "inclusive-or" expression.  */
6146   if (cond_or == DOM_CC_NX_OR_Y)
6147     cond1 = reverse_condition (cond1);
6148
6149   /* If the comparisons are not equal, and one doesn't dominate the other,
6150      then we can't do this.  */
6151   if (cond1 != cond2
6152       && !comparison_dominates_p (cond1, cond2)
6153       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6154     return CCmode;
6155
6156   if (swapped)
6157     {
6158       enum rtx_code temp = cond1;
6159       cond1 = cond2;
6160       cond2 = temp;
6161     }
6162
6163   switch (cond1)
6164     {
6165     case EQ:
6166       if (cond_or == DOM_CC_X_AND_Y)
6167         return CC_DEQmode;
6168
6169       switch (cond2)
6170         {
6171         case EQ: return CC_DEQmode;
6172         case LE: return CC_DLEmode;
6173         case LEU: return CC_DLEUmode;
6174         case GE: return CC_DGEmode;
6175         case GEU: return CC_DGEUmode;
6176         default: gcc_unreachable ();
6177         }
6178
6179     case LT:
6180       if (cond_or == DOM_CC_X_AND_Y)
6181         return CC_DLTmode;
6182       
6183       switch (cond2)
6184         {
6185         case  LT:
6186             return CC_DLTmode;
6187         case LE:
6188           return CC_DLEmode;
6189         case NE:
6190           return CC_DNEmode;
6191         default:
6192           gcc_unreachable ();
6193         }
6194
6195     case GT:
6196       if (cond_or == DOM_CC_X_AND_Y)
6197         return CC_DGTmode;
6198
6199       switch (cond2)
6200         {
6201         case GT:
6202           return CC_DGTmode;
6203         case GE:
6204           return CC_DGEmode;
6205         case NE:
6206           return CC_DNEmode;
6207         default:
6208           gcc_unreachable ();
6209         }
6210
6211     case LTU:
6212       if (cond_or == DOM_CC_X_AND_Y)
6213         return CC_DLTUmode;
6214
6215       switch (cond2)
6216         {
6217         case LTU:
6218           return CC_DLTUmode;
6219         case LEU:
6220           return CC_DLEUmode;
6221         case NE:
6222           return CC_DNEmode;
6223         default:
6224           gcc_unreachable ();
6225         }
6226
6227     case GTU:
6228       if (cond_or == DOM_CC_X_AND_Y)
6229         return CC_DGTUmode;
6230       
6231       switch (cond2)
6232         {
6233         case GTU:
6234           return CC_DGTUmode;
6235         case GEU:
6236           return CC_DGEUmode;
6237         case NE:
6238           return CC_DNEmode;
6239         default:
6240           gcc_unreachable ();
6241         }
6242
6243     /* The remaining cases only occur when both comparisons are the
6244        same.  */
6245     case NE:
6246       gcc_assert (cond1 == cond2);
6247       return CC_DNEmode;
6248
6249     case LE:
6250       gcc_assert (cond1 == cond2);
6251       return CC_DLEmode;
6252
6253     case GE:
6254       gcc_assert (cond1 == cond2);
6255       return CC_DGEmode;
6256
6257     case LEU:
6258       gcc_assert (cond1 == cond2);
6259       return CC_DLEUmode;
6260
6261     case GEU:
6262       gcc_assert (cond1 == cond2);
6263       return CC_DGEUmode;
6264
6265     default:
6266       gcc_unreachable ();
6267     }
6268 }
6269
6270 enum machine_mode
6271 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6272 {
6273   /* All floating point compares return CCFP if it is an equality
6274      comparison, and CCFPE otherwise.  */
6275   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6276     {
6277       switch (op)
6278         {
6279         case EQ:
6280         case NE:
6281         case UNORDERED:
6282         case ORDERED:
6283         case UNLT:
6284         case UNLE:
6285         case UNGT:
6286         case UNGE:
6287         case UNEQ:
6288         case LTGT:
6289           return CCFPmode;
6290
6291         case LT:
6292         case LE:
6293         case GT:
6294         case GE:
6295           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6296             return CCFPmode;
6297           return CCFPEmode;
6298
6299         default:
6300           gcc_unreachable ();
6301         }
6302     }
6303
6304   /* A compare with a shifted operand.  Because of canonicalization, the
6305      comparison will have to be swapped when we emit the assembler.  */
6306   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6307       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6308           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6309           || GET_CODE (x) == ROTATERT))
6310     return CC_SWPmode;
6311
6312   /* This operation is performed swapped, but since we only rely on the Z
6313      flag we don't need an additional mode.  */
6314   if (GET_MODE (y) == SImode && REG_P (y)
6315       && GET_CODE (x) == NEG
6316       && (op == EQ || op == NE))
6317     return CC_Zmode;
6318
6319   /* This is a special case that is used by combine to allow a
6320      comparison of a shifted byte load to be split into a zero-extend
6321      followed by a comparison of the shifted integer (only valid for
6322      equalities and unsigned inequalities).  */
6323   if (GET_MODE (x) == SImode
6324       && GET_CODE (x) == ASHIFT
6325       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6326       && GET_CODE (XEXP (x, 0)) == SUBREG
6327       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6328       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6329       && (op == EQ || op == NE
6330           || op == GEU || op == GTU || op == LTU || op == LEU)
6331       && GET_CODE (y) == CONST_INT)
6332     return CC_Zmode;
6333
6334   /* A construct for a conditional compare, if the false arm contains
6335      0, then both conditions must be true, otherwise either condition
6336      must be true.  Not all conditions are possible, so CCmode is
6337      returned if it can't be done.  */
6338   if (GET_CODE (x) == IF_THEN_ELSE
6339       && (XEXP (x, 2) == const0_rtx
6340           || XEXP (x, 2) == const1_rtx)
6341       && COMPARISON_P (XEXP (x, 0))
6342       && COMPARISON_P (XEXP (x, 1)))
6343     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6344                                          INTVAL (XEXP (x, 2)));
6345
6346   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6347   if (GET_CODE (x) == AND
6348       && COMPARISON_P (XEXP (x, 0))
6349       && COMPARISON_P (XEXP (x, 1)))
6350     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6351                                          DOM_CC_X_AND_Y);
6352
6353   if (GET_CODE (x) == IOR
6354       && COMPARISON_P (XEXP (x, 0))
6355       && COMPARISON_P (XEXP (x, 1)))
6356     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6357                                          DOM_CC_X_OR_Y);
6358
6359   /* An operation (on Thumb) where we want to test for a single bit.
6360      This is done by shifting that bit up into the top bit of a
6361      scratch register; we can then branch on the sign bit.  */
6362   if (TARGET_THUMB
6363       && GET_MODE (x) == SImode
6364       && (op == EQ || op == NE)
6365       && (GET_CODE (x) == ZERO_EXTRACT))
6366     return CC_Nmode;
6367
6368   /* An operation that sets the condition codes as a side-effect, the
6369      V flag is not set correctly, so we can only use comparisons where
6370      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6371      instead.)  */
6372   if (GET_MODE (x) == SImode
6373       && y == const0_rtx
6374       && (op == EQ || op == NE || op == LT || op == GE)
6375       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6376           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6377           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6378           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6379           || GET_CODE (x) == LSHIFTRT
6380           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6381           || GET_CODE (x) == ROTATERT
6382           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6383     return CC_NOOVmode;
6384
6385   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6386     return CC_Zmode;
6387
6388   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6389       && GET_CODE (x) == PLUS
6390       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6391     return CC_Cmode;
6392
6393   return CCmode;
6394 }
6395
6396 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6397    return the rtx for register 0 in the proper mode.  FP means this is a
6398    floating point compare: I don't think that it is needed on the arm.  */
6399 rtx
6400 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6401 {
6402   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6403   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6404
6405   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6406                           gen_rtx_COMPARE (mode, x, y)));
6407
6408   return cc_reg;
6409 }
6410
6411 /* Generate a sequence of insns that will generate the correct return
6412    address mask depending on the physical architecture that the program
6413    is running on.  */
6414 rtx
6415 arm_gen_return_addr_mask (void)
6416 {
6417   rtx reg = gen_reg_rtx (Pmode);
6418
6419   emit_insn (gen_return_addr_mask (reg));
6420   return reg;
6421 }
6422
6423 void
6424 arm_reload_in_hi (rtx *operands)
6425 {
6426   rtx ref = operands[1];
6427   rtx base, scratch;
6428   HOST_WIDE_INT offset = 0;
6429
6430   if (GET_CODE (ref) == SUBREG)
6431     {
6432       offset = SUBREG_BYTE (ref);
6433       ref = SUBREG_REG (ref);
6434     }
6435
6436   if (GET_CODE (ref) == REG)
6437     {
6438       /* We have a pseudo which has been spilt onto the stack; there
6439          are two cases here: the first where there is a simple
6440          stack-slot replacement and a second where the stack-slot is
6441          out of range, or is used as a subreg.  */
6442       if (reg_equiv_mem[REGNO (ref)])
6443         {
6444           ref = reg_equiv_mem[REGNO (ref)];
6445           base = find_replacement (&XEXP (ref, 0));
6446         }
6447       else
6448         /* The slot is out of range, or was dressed up in a SUBREG.  */
6449         base = reg_equiv_address[REGNO (ref)];
6450     }
6451   else
6452     base = find_replacement (&XEXP (ref, 0));
6453
6454   /* Handle the case where the address is too complex to be offset by 1.  */
6455   if (GET_CODE (base) == MINUS
6456       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6457     {
6458       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6459
6460       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6461       base = base_plus;
6462     }
6463   else if (GET_CODE (base) == PLUS)
6464     {
6465       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6466       HOST_WIDE_INT hi, lo;
6467
6468       offset += INTVAL (XEXP (base, 1));
6469       base = XEXP (base, 0);
6470
6471       /* Rework the address into a legal sequence of insns.  */
6472       /* Valid range for lo is -4095 -> 4095 */
6473       lo = (offset >= 0
6474             ? (offset & 0xfff)
6475             : -((-offset) & 0xfff));
6476
6477       /* Corner case, if lo is the max offset then we would be out of range
6478          once we have added the additional 1 below, so bump the msb into the
6479          pre-loading insn(s).  */
6480       if (lo == 4095)
6481         lo &= 0x7ff;
6482
6483       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6484              ^ (HOST_WIDE_INT) 0x80000000)
6485             - (HOST_WIDE_INT) 0x80000000);
6486
6487       gcc_assert (hi + lo == offset);
6488
6489       if (hi != 0)
6490         {
6491           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6492
6493           /* Get the base address; addsi3 knows how to handle constants
6494              that require more than one insn.  */
6495           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6496           base = base_plus;
6497           offset = lo;
6498         }
6499     }
6500
6501   /* Operands[2] may overlap operands[0] (though it won't overlap
6502      operands[1]), that's why we asked for a DImode reg -- so we can
6503      use the bit that does not overlap.  */
6504   if (REGNO (operands[2]) == REGNO (operands[0]))
6505     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6506   else
6507     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6508
6509   emit_insn (gen_zero_extendqisi2 (scratch,
6510                                    gen_rtx_MEM (QImode,
6511                                                 plus_constant (base,
6512                                                                offset))));
6513   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6514                                    gen_rtx_MEM (QImode,
6515                                                 plus_constant (base,
6516                                                                offset + 1))));
6517   if (!BYTES_BIG_ENDIAN)
6518     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6519                         gen_rtx_IOR (SImode,
6520                                      gen_rtx_ASHIFT
6521                                      (SImode,
6522                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6523                                       GEN_INT (8)),
6524                                      scratch)));
6525   else
6526     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6527                             gen_rtx_IOR (SImode,
6528                                          gen_rtx_ASHIFT (SImode, scratch,
6529                                                          GEN_INT (8)),
6530                                          gen_rtx_SUBREG (SImode, operands[0],
6531                                                          0))));
6532 }
6533
6534 /* Handle storing a half-word to memory during reload by synthesizing as two
6535    byte stores.  Take care not to clobber the input values until after we
6536    have moved them somewhere safe.  This code assumes that if the DImode
6537    scratch in operands[2] overlaps either the input value or output address
6538    in some way, then that value must die in this insn (we absolutely need
6539    two scratch registers for some corner cases).  */
6540 void
6541 arm_reload_out_hi (rtx *operands)
6542 {
6543   rtx ref = operands[0];
6544   rtx outval = operands[1];
6545   rtx base, scratch;
6546   HOST_WIDE_INT offset = 0;
6547
6548   if (GET_CODE (ref) == SUBREG)
6549     {
6550       offset = SUBREG_BYTE (ref);
6551       ref = SUBREG_REG (ref);
6552     }
6553
6554   if (GET_CODE (ref) == REG)
6555     {
6556       /* We have a pseudo which has been spilt onto the stack; there
6557          are two cases here: the first where there is a simple
6558          stack-slot replacement and a second where the stack-slot is
6559          out of range, or is used as a subreg.  */
6560       if (reg_equiv_mem[REGNO (ref)])
6561         {
6562           ref = reg_equiv_mem[REGNO (ref)];
6563           base = find_replacement (&XEXP (ref, 0));
6564         }
6565       else
6566         /* The slot is out of range, or was dressed up in a SUBREG.  */
6567         base = reg_equiv_address[REGNO (ref)];
6568     }
6569   else
6570     base = find_replacement (&XEXP (ref, 0));
6571
6572   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6573
6574   /* Handle the case where the address is too complex to be offset by 1.  */
6575   if (GET_CODE (base) == MINUS
6576       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6577     {
6578       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6579
6580       /* Be careful not to destroy OUTVAL.  */
6581       if (reg_overlap_mentioned_p (base_plus, outval))
6582         {
6583           /* Updating base_plus might destroy outval, see if we can
6584              swap the scratch and base_plus.  */
6585           if (!reg_overlap_mentioned_p (scratch, outval))
6586             {
6587               rtx tmp = scratch;
6588               scratch = base_plus;
6589               base_plus = tmp;
6590             }
6591           else
6592             {
6593               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6594
6595               /* Be conservative and copy OUTVAL into the scratch now,
6596                  this should only be necessary if outval is a subreg
6597                  of something larger than a word.  */
6598               /* XXX Might this clobber base?  I can't see how it can,
6599                  since scratch is known to overlap with OUTVAL, and
6600                  must be wider than a word.  */
6601               emit_insn (gen_movhi (scratch_hi, outval));
6602               outval = scratch_hi;
6603             }
6604         }
6605
6606       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6607       base = base_plus;
6608     }
6609   else if (GET_CODE (base) == PLUS)
6610     {
6611       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6612       HOST_WIDE_INT hi, lo;
6613
6614       offset += INTVAL (XEXP (base, 1));
6615       base = XEXP (base, 0);
6616
6617       /* Rework the address into a legal sequence of insns.  */
6618       /* Valid range for lo is -4095 -> 4095 */
6619       lo = (offset >= 0
6620             ? (offset & 0xfff)
6621             : -((-offset) & 0xfff));
6622
6623       /* Corner case, if lo is the max offset then we would be out of range
6624          once we have added the additional 1 below, so bump the msb into the
6625          pre-loading insn(s).  */
6626       if (lo == 4095)
6627         lo &= 0x7ff;
6628
6629       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6630              ^ (HOST_WIDE_INT) 0x80000000)
6631             - (HOST_WIDE_INT) 0x80000000);
6632
6633       gcc_assert (hi + lo == offset);
6634
6635       if (hi != 0)
6636         {
6637           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6638
6639           /* Be careful not to destroy OUTVAL.  */
6640           if (reg_overlap_mentioned_p (base_plus, outval))
6641             {
6642               /* Updating base_plus might destroy outval, see if we
6643                  can swap the scratch and base_plus.  */
6644               if (!reg_overlap_mentioned_p (scratch, outval))
6645                 {
6646                   rtx tmp = scratch;
6647                   scratch = base_plus;
6648                   base_plus = tmp;
6649                 }
6650               else
6651                 {
6652                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6653
6654                   /* Be conservative and copy outval into scratch now,
6655                      this should only be necessary if outval is a
6656                      subreg of something larger than a word.  */
6657                   /* XXX Might this clobber base?  I can't see how it
6658                      can, since scratch is known to overlap with
6659                      outval.  */
6660                   emit_insn (gen_movhi (scratch_hi, outval));
6661                   outval = scratch_hi;
6662                 }
6663             }
6664
6665           /* Get the base address; addsi3 knows how to handle constants
6666              that require more than one insn.  */
6667           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6668           base = base_plus;
6669           offset = lo;
6670         }
6671     }
6672
6673   if (BYTES_BIG_ENDIAN)
6674     {
6675       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6676                                          plus_constant (base, offset + 1)),
6677                             gen_lowpart (QImode, outval)));
6678       emit_insn (gen_lshrsi3 (scratch,
6679                               gen_rtx_SUBREG (SImode, outval, 0),
6680                               GEN_INT (8)));
6681       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6682                             gen_lowpart (QImode, scratch)));
6683     }
6684   else
6685     {
6686       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6687                             gen_lowpart (QImode, outval)));
6688       emit_insn (gen_lshrsi3 (scratch,
6689                               gen_rtx_SUBREG (SImode, outval, 0),
6690                               GEN_INT (8)));
6691       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6692                                          plus_constant (base, offset + 1)),
6693                             gen_lowpart (QImode, scratch)));
6694     }
6695 }
6696
6697 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
6698    (padded to the size of a word) should be passed in a register.  */
6699
6700 static bool
6701 arm_must_pass_in_stack (enum machine_mode mode, tree type)
6702 {
6703   if (TARGET_AAPCS_BASED)
6704     return must_pass_in_stack_var_size (mode, type);
6705   else
6706     return must_pass_in_stack_var_size_or_pad (mode, type);
6707 }
6708
6709
6710 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
6711    Return true if an argument passed on the stack should be padded upwards,
6712    i.e. if the least-significant byte has useful data.
6713    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
6714    aggregate types are placed in the lowest memory address.  */
6715
6716 bool
6717 arm_pad_arg_upward (enum machine_mode mode, tree type)
6718 {
6719   if (!TARGET_AAPCS_BASED)
6720     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
6721
6722   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
6723     return false;
6724
6725   return true;
6726 }
6727
6728
6729 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
6730    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
6731    byte of the register has useful data, and return the opposite if the
6732    most significant byte does.
6733    For AAPCS, small aggregates and small complex types are always padded
6734    upwards.  */
6735
6736 bool
6737 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
6738                     tree type, int first ATTRIBUTE_UNUSED)
6739 {
6740   if (TARGET_AAPCS_BASED
6741       && BYTES_BIG_ENDIAN
6742       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6743       && int_size_in_bytes (type) <= 4)
6744     return true;
6745
6746   /* Otherwise, use default padding.  */
6747   return !BYTES_BIG_ENDIAN;
6748 }
6749
6750
6751 \f
6752 /* Print a symbolic form of X to the debug file, F.  */
6753 static void
6754 arm_print_value (FILE *f, rtx x)
6755 {
6756   switch (GET_CODE (x))
6757     {
6758     case CONST_INT:
6759       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6760       return;
6761
6762     case CONST_DOUBLE:
6763       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6764       return;
6765
6766     case CONST_VECTOR:
6767       {
6768         int i;
6769
6770         fprintf (f, "<");
6771         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6772           {
6773             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6774             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6775               fputc (',', f);
6776           }
6777         fprintf (f, ">");
6778       }
6779       return;
6780
6781     case CONST_STRING:
6782       fprintf (f, "\"%s\"", XSTR (x, 0));
6783       return;
6784
6785     case SYMBOL_REF:
6786       fprintf (f, "`%s'", XSTR (x, 0));
6787       return;
6788
6789     case LABEL_REF:
6790       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6791       return;
6792
6793     case CONST:
6794       arm_print_value (f, XEXP (x, 0));
6795       return;
6796
6797     case PLUS:
6798       arm_print_value (f, XEXP (x, 0));
6799       fprintf (f, "+");
6800       arm_print_value (f, XEXP (x, 1));
6801       return;
6802
6803     case PC:
6804       fprintf (f, "pc");
6805       return;
6806
6807     default:
6808       fprintf (f, "????");
6809       return;
6810     }
6811 }
6812 \f
6813 /* Routines for manipulation of the constant pool.  */
6814
6815 /* Arm instructions cannot load a large constant directly into a
6816    register; they have to come from a pc relative load.  The constant
6817    must therefore be placed in the addressable range of the pc
6818    relative load.  Depending on the precise pc relative load
6819    instruction the range is somewhere between 256 bytes and 4k.  This
6820    means that we often have to dump a constant inside a function, and
6821    generate code to branch around it.
6822
6823    It is important to minimize this, since the branches will slow
6824    things down and make the code larger.
6825
6826    Normally we can hide the table after an existing unconditional
6827    branch so that there is no interruption of the flow, but in the
6828    worst case the code looks like this:
6829
6830         ldr     rn, L1
6831         ...
6832         b       L2
6833         align
6834         L1:     .long value
6835         L2:
6836         ...
6837
6838         ldr     rn, L3
6839         ...
6840         b       L4
6841         align
6842         L3:     .long value
6843         L4:
6844         ...
6845
6846    We fix this by performing a scan after scheduling, which notices
6847    which instructions need to have their operands fetched from the
6848    constant table and builds the table.
6849
6850    The algorithm starts by building a table of all the constants that
6851    need fixing up and all the natural barriers in the function (places
6852    where a constant table can be dropped without breaking the flow).
6853    For each fixup we note how far the pc-relative replacement will be
6854    able to reach and the offset of the instruction into the function.
6855
6856    Having built the table we then group the fixes together to form
6857    tables that are as large as possible (subject to addressing
6858    constraints) and emit each table of constants after the last
6859    barrier that is within range of all the instructions in the group.
6860    If a group does not contain a barrier, then we forcibly create one
6861    by inserting a jump instruction into the flow.  Once the table has
6862    been inserted, the insns are then modified to reference the
6863    relevant entry in the pool.
6864
6865    Possible enhancements to the algorithm (not implemented) are:
6866
6867    1) For some processors and object formats, there may be benefit in
6868    aligning the pools to the start of cache lines; this alignment
6869    would need to be taken into account when calculating addressability
6870    of a pool.  */
6871
6872 /* These typedefs are located at the start of this file, so that
6873    they can be used in the prototypes there.  This comment is to
6874    remind readers of that fact so that the following structures
6875    can be understood more easily.
6876
6877      typedef struct minipool_node    Mnode;
6878      typedef struct minipool_fixup   Mfix;  */
6879
6880 struct minipool_node
6881 {
6882   /* Doubly linked chain of entries.  */
6883   Mnode * next;
6884   Mnode * prev;
6885   /* The maximum offset into the code that this entry can be placed.  While
6886      pushing fixes for forward references, all entries are sorted in order
6887      of increasing max_address.  */
6888   HOST_WIDE_INT max_address;
6889   /* Similarly for an entry inserted for a backwards ref.  */
6890   HOST_WIDE_INT min_address;
6891   /* The number of fixes referencing this entry.  This can become zero
6892      if we "unpush" an entry.  In this case we ignore the entry when we
6893      come to emit the code.  */
6894   int refcount;
6895   /* The offset from the start of the minipool.  */
6896   HOST_WIDE_INT offset;
6897   /* The value in table.  */
6898   rtx value;
6899   /* The mode of value.  */
6900   enum machine_mode mode;
6901   /* The size of the value.  With iWMMXt enabled
6902      sizes > 4 also imply an alignment of 8-bytes.  */
6903   int fix_size;
6904 };
6905
6906 struct minipool_fixup
6907 {
6908   Mfix *            next;
6909   rtx               insn;
6910   HOST_WIDE_INT     address;
6911   rtx *             loc;
6912   enum machine_mode mode;
6913   int               fix_size;
6914   rtx               value;
6915   Mnode *           minipool;
6916   HOST_WIDE_INT     forwards;
6917   HOST_WIDE_INT     backwards;
6918 };
6919
6920 /* Fixes less than a word need padding out to a word boundary.  */
6921 #define MINIPOOL_FIX_SIZE(mode) \
6922   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6923
6924 static Mnode *  minipool_vector_head;
6925 static Mnode *  minipool_vector_tail;
6926 static rtx      minipool_vector_label;
6927
6928 /* The linked list of all minipool fixes required for this function.  */
6929 Mfix *          minipool_fix_head;
6930 Mfix *          minipool_fix_tail;
6931 /* The fix entry for the current minipool, once it has been placed.  */
6932 Mfix *          minipool_barrier;
6933
6934 /* Determines if INSN is the start of a jump table.  Returns the end
6935    of the TABLE or NULL_RTX.  */
6936 static rtx
6937 is_jump_table (rtx insn)
6938 {
6939   rtx table;
6940
6941   if (GET_CODE (insn) == JUMP_INSN
6942       && JUMP_LABEL (insn) != NULL
6943       && ((table = next_real_insn (JUMP_LABEL (insn)))
6944           == next_real_insn (insn))
6945       && table != NULL
6946       && GET_CODE (table) == JUMP_INSN
6947       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6948           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6949     return table;
6950
6951   return NULL_RTX;
6952 }
6953
6954 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6955 #define JUMP_TABLES_IN_TEXT_SECTION 0
6956 #endif
6957
6958 static HOST_WIDE_INT
6959 get_jump_table_size (rtx insn)
6960 {
6961   /* ADDR_VECs only take room if read-only data does into the text
6962      section.  */
6963   if (JUMP_TABLES_IN_TEXT_SECTION
6964 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6965       || 1
6966 #endif
6967       )
6968     {
6969       rtx body = PATTERN (insn);
6970       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6971
6972       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6973     }
6974
6975   return 0;
6976 }
6977
6978 /* Move a minipool fix MP from its current location to before MAX_MP.
6979    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6980    constraints may need updating.  */
6981 static Mnode *
6982 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6983                                HOST_WIDE_INT max_address)
6984 {
6985   /* The code below assumes these are different.  */
6986   gcc_assert (mp != max_mp);
6987
6988   if (max_mp == NULL)
6989     {
6990       if (max_address < mp->max_address)
6991         mp->max_address = max_address;
6992     }
6993   else
6994     {
6995       if (max_address > max_mp->max_address - mp->fix_size)
6996         mp->max_address = max_mp->max_address - mp->fix_size;
6997       else
6998         mp->max_address = max_address;
6999
7000       /* Unlink MP from its current position.  Since max_mp is non-null,
7001        mp->prev must be non-null.  */
7002       mp->prev->next = mp->next;
7003       if (mp->next != NULL)
7004         mp->next->prev = mp->prev;
7005       else
7006         minipool_vector_tail = mp->prev;
7007
7008       /* Re-insert it before MAX_MP.  */
7009       mp->next = max_mp;
7010       mp->prev = max_mp->prev;
7011       max_mp->prev = mp;
7012
7013       if (mp->prev != NULL)
7014         mp->prev->next = mp;
7015       else
7016         minipool_vector_head = mp;
7017     }
7018
7019   /* Save the new entry.  */
7020   max_mp = mp;
7021
7022   /* Scan over the preceding entries and adjust their addresses as
7023      required.  */
7024   while (mp->prev != NULL
7025          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7026     {
7027       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7028       mp = mp->prev;
7029     }
7030
7031   return max_mp;
7032 }
7033
7034 /* Add a constant to the minipool for a forward reference.  Returns the
7035    node added or NULL if the constant will not fit in this pool.  */
7036 static Mnode *
7037 add_minipool_forward_ref (Mfix *fix)
7038 {
7039   /* If set, max_mp is the first pool_entry that has a lower
7040      constraint than the one we are trying to add.  */
7041   Mnode *       max_mp = NULL;
7042   HOST_WIDE_INT max_address = fix->address + fix->forwards;
7043   Mnode *       mp;
7044
7045   /* If this fix's address is greater than the address of the first
7046      entry, then we can't put the fix in this pool.  We subtract the
7047      size of the current fix to ensure that if the table is fully
7048      packed we still have enough room to insert this value by shuffling
7049      the other fixes forwards.  */
7050   if (minipool_vector_head &&
7051       fix->address >= minipool_vector_head->max_address - fix->fix_size)
7052     return NULL;
7053
7054   /* Scan the pool to see if a constant with the same value has
7055      already been added.  While we are doing this, also note the
7056      location where we must insert the constant if it doesn't already
7057      exist.  */
7058   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7059     {
7060       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7061           && fix->mode == mp->mode
7062           && (GET_CODE (fix->value) != CODE_LABEL
7063               || (CODE_LABEL_NUMBER (fix->value)
7064                   == CODE_LABEL_NUMBER (mp->value)))
7065           && rtx_equal_p (fix->value, mp->value))
7066         {
7067           /* More than one fix references this entry.  */
7068           mp->refcount++;
7069           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7070         }
7071
7072       /* Note the insertion point if necessary.  */
7073       if (max_mp == NULL
7074           && mp->max_address > max_address)
7075         max_mp = mp;
7076
7077       /* If we are inserting an 8-bytes aligned quantity and
7078          we have not already found an insertion point, then
7079          make sure that all such 8-byte aligned quantities are
7080          placed at the start of the pool.  */
7081       if (ARM_DOUBLEWORD_ALIGN
7082           && max_mp == NULL
7083           && fix->fix_size == 8
7084           && mp->fix_size != 8)
7085         {
7086           max_mp = mp;
7087           max_address = mp->max_address;
7088         }
7089     }
7090
7091   /* The value is not currently in the minipool, so we need to create
7092      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7093      the end of the list since the placement is less constrained than
7094      any existing entry.  Otherwise, we insert the new fix before
7095      MAX_MP and, if necessary, adjust the constraints on the other
7096      entries.  */
7097   mp = xmalloc (sizeof (* mp));
7098   mp->fix_size = fix->fix_size;
7099   mp->mode = fix->mode;
7100   mp->value = fix->value;
7101   mp->refcount = 1;
7102   /* Not yet required for a backwards ref.  */
7103   mp->min_address = -65536;
7104
7105   if (max_mp == NULL)
7106     {
7107       mp->max_address = max_address;
7108       mp->next = NULL;
7109       mp->prev = minipool_vector_tail;
7110
7111       if (mp->prev == NULL)
7112         {
7113           minipool_vector_head = mp;
7114           minipool_vector_label = gen_label_rtx ();
7115         }
7116       else
7117         mp->prev->next = mp;
7118
7119       minipool_vector_tail = mp;
7120     }
7121   else
7122     {
7123       if (max_address > max_mp->max_address - mp->fix_size)
7124         mp->max_address = max_mp->max_address - mp->fix_size;
7125       else
7126         mp->max_address = max_address;
7127
7128       mp->next = max_mp;
7129       mp->prev = max_mp->prev;
7130       max_mp->prev = mp;
7131       if (mp->prev != NULL)
7132         mp->prev->next = mp;
7133       else
7134         minipool_vector_head = mp;
7135     }
7136
7137   /* Save the new entry.  */
7138   max_mp = mp;
7139
7140   /* Scan over the preceding entries and adjust their addresses as
7141      required.  */
7142   while (mp->prev != NULL
7143          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7144     {
7145       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7146       mp = mp->prev;
7147     }
7148
7149   return max_mp;
7150 }
7151
7152 static Mnode *
7153 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7154                                 HOST_WIDE_INT  min_address)
7155 {
7156   HOST_WIDE_INT offset;
7157
7158   /* The code below assumes these are different.  */
7159   gcc_assert (mp != min_mp);
7160
7161   if (min_mp == NULL)
7162     {
7163       if (min_address > mp->min_address)
7164         mp->min_address = min_address;
7165     }
7166   else
7167     {
7168       /* We will adjust this below if it is too loose.  */
7169       mp->min_address = min_address;
7170
7171       /* Unlink MP from its current position.  Since min_mp is non-null,
7172          mp->next must be non-null.  */
7173       mp->next->prev = mp->prev;
7174       if (mp->prev != NULL)
7175         mp->prev->next = mp->next;
7176       else
7177         minipool_vector_head = mp->next;
7178
7179       /* Reinsert it after MIN_MP.  */
7180       mp->prev = min_mp;
7181       mp->next = min_mp->next;
7182       min_mp->next = mp;
7183       if (mp->next != NULL)
7184         mp->next->prev = mp;
7185       else
7186         minipool_vector_tail = mp;
7187     }
7188
7189   min_mp = mp;
7190
7191   offset = 0;
7192   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7193     {
7194       mp->offset = offset;
7195       if (mp->refcount > 0)
7196         offset += mp->fix_size;
7197
7198       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7199         mp->next->min_address = mp->min_address + mp->fix_size;
7200     }
7201
7202   return min_mp;
7203 }
7204
7205 /* Add a constant to the minipool for a backward reference.  Returns the
7206    node added or NULL if the constant will not fit in this pool.
7207
7208    Note that the code for insertion for a backwards reference can be
7209    somewhat confusing because the calculated offsets for each fix do
7210    not take into account the size of the pool (which is still under
7211    construction.  */
7212 static Mnode *
7213 add_minipool_backward_ref (Mfix *fix)
7214 {
7215   /* If set, min_mp is the last pool_entry that has a lower constraint
7216      than the one we are trying to add.  */
7217   Mnode *min_mp = NULL;
7218   /* This can be negative, since it is only a constraint.  */
7219   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7220   Mnode *mp;
7221
7222   /* If we can't reach the current pool from this insn, or if we can't
7223      insert this entry at the end of the pool without pushing other
7224      fixes out of range, then we don't try.  This ensures that we
7225      can't fail later on.  */
7226   if (min_address >= minipool_barrier->address
7227       || (minipool_vector_tail->min_address + fix->fix_size
7228           >= minipool_barrier->address))
7229     return NULL;
7230
7231   /* Scan the pool to see if a constant with the same value has
7232      already been added.  While we are doing this, also note the
7233      location where we must insert the constant if it doesn't already
7234      exist.  */
7235   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7236     {
7237       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7238           && fix->mode == mp->mode
7239           && (GET_CODE (fix->value) != CODE_LABEL
7240               || (CODE_LABEL_NUMBER (fix->value)
7241                   == CODE_LABEL_NUMBER (mp->value)))
7242           && rtx_equal_p (fix->value, mp->value)
7243           /* Check that there is enough slack to move this entry to the
7244              end of the table (this is conservative).  */
7245           && (mp->max_address
7246               > (minipool_barrier->address
7247                  + minipool_vector_tail->offset
7248                  + minipool_vector_tail->fix_size)))
7249         {
7250           mp->refcount++;
7251           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7252         }
7253
7254       if (min_mp != NULL)
7255         mp->min_address += fix->fix_size;
7256       else
7257         {
7258           /* Note the insertion point if necessary.  */
7259           if (mp->min_address < min_address)
7260             {
7261               /* For now, we do not allow the insertion of 8-byte alignment
7262                  requiring nodes anywhere but at the start of the pool.  */
7263               if (ARM_DOUBLEWORD_ALIGN
7264                   && fix->fix_size == 8 && mp->fix_size != 8)
7265                 return NULL;
7266               else
7267                 min_mp = mp;
7268             }
7269           else if (mp->max_address
7270                    < minipool_barrier->address + mp->offset + fix->fix_size)
7271             {
7272               /* Inserting before this entry would push the fix beyond
7273                  its maximum address (which can happen if we have
7274                  re-located a forwards fix); force the new fix to come
7275                  after it.  */
7276               min_mp = mp;
7277               min_address = mp->min_address + fix->fix_size;
7278             }
7279           /* If we are inserting an 8-bytes aligned quantity and
7280              we have not already found an insertion point, then
7281              make sure that all such 8-byte aligned quantities are
7282              placed at the start of the pool.  */
7283           else if (ARM_DOUBLEWORD_ALIGN
7284                    && min_mp == NULL
7285                    && fix->fix_size == 8
7286                    && mp->fix_size < 8)
7287             {
7288               min_mp = mp;
7289               min_address = mp->min_address + fix->fix_size;
7290             }
7291         }
7292     }
7293
7294   /* We need to create a new entry.  */
7295   mp = xmalloc (sizeof (* mp));
7296   mp->fix_size = fix->fix_size;
7297   mp->mode = fix->mode;
7298   mp->value = fix->value;
7299   mp->refcount = 1;
7300   mp->max_address = minipool_barrier->address + 65536;
7301
7302   mp->min_address = min_address;
7303
7304   if (min_mp == NULL)
7305     {
7306       mp->prev = NULL;
7307       mp->next = minipool_vector_head;
7308
7309       if (mp->next == NULL)
7310         {
7311           minipool_vector_tail = mp;
7312           minipool_vector_label = gen_label_rtx ();
7313         }
7314       else
7315         mp->next->prev = mp;
7316
7317       minipool_vector_head = mp;
7318     }
7319   else
7320     {
7321       mp->next = min_mp->next;
7322       mp->prev = min_mp;
7323       min_mp->next = mp;
7324
7325       if (mp->next != NULL)
7326         mp->next->prev = mp;
7327       else
7328         minipool_vector_tail = mp;
7329     }
7330
7331   /* Save the new entry.  */
7332   min_mp = mp;
7333
7334   if (mp->prev)
7335     mp = mp->prev;
7336   else
7337     mp->offset = 0;
7338
7339   /* Scan over the following entries and adjust their offsets.  */
7340   while (mp->next != NULL)
7341     {
7342       if (mp->next->min_address < mp->min_address + mp->fix_size)
7343         mp->next->min_address = mp->min_address + mp->fix_size;
7344
7345       if (mp->refcount)
7346         mp->next->offset = mp->offset + mp->fix_size;
7347       else
7348         mp->next->offset = mp->offset;
7349
7350       mp = mp->next;
7351     }
7352
7353   return min_mp;
7354 }
7355
7356 static void
7357 assign_minipool_offsets (Mfix *barrier)
7358 {
7359   HOST_WIDE_INT offset = 0;
7360   Mnode *mp;
7361
7362   minipool_barrier = barrier;
7363
7364   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7365     {
7366       mp->offset = offset;
7367
7368       if (mp->refcount > 0)
7369         offset += mp->fix_size;
7370     }
7371 }
7372
7373 /* Output the literal table */
7374 static void
7375 dump_minipool (rtx scan)
7376 {
7377   Mnode * mp;
7378   Mnode * nmp;
7379   int align64 = 0;
7380
7381   if (ARM_DOUBLEWORD_ALIGN)
7382     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7383       if (mp->refcount > 0 && mp->fix_size == 8)
7384         {
7385           align64 = 1;
7386           break;
7387         }
7388
7389   if (dump_file)
7390     fprintf (dump_file,
7391              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7392              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7393
7394   scan = emit_label_after (gen_label_rtx (), scan);
7395   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7396   scan = emit_label_after (minipool_vector_label, scan);
7397
7398   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7399     {
7400       if (mp->refcount > 0)
7401         {
7402           if (dump_file)
7403             {
7404               fprintf (dump_file,
7405                        ";;  Offset %u, min %ld, max %ld ",
7406                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7407                        (unsigned long) mp->max_address);
7408               arm_print_value (dump_file, mp->value);
7409               fputc ('\n', dump_file);
7410             }
7411
7412           switch (mp->fix_size)
7413             {
7414 #ifdef HAVE_consttable_1
7415             case 1:
7416               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7417               break;
7418
7419 #endif
7420 #ifdef HAVE_consttable_2
7421             case 2:
7422               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7423               break;
7424
7425 #endif
7426 #ifdef HAVE_consttable_4
7427             case 4:
7428               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7429               break;
7430
7431 #endif
7432 #ifdef HAVE_consttable_8
7433             case 8:
7434               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7435               break;
7436
7437 #endif
7438             default:
7439               gcc_unreachable ();
7440             }
7441         }
7442
7443       nmp = mp->next;
7444       free (mp);
7445     }
7446
7447   minipool_vector_head = minipool_vector_tail = NULL;
7448   scan = emit_insn_after (gen_consttable_end (), scan);
7449   scan = emit_barrier_after (scan);
7450 }
7451
7452 /* Return the cost of forcibly inserting a barrier after INSN.  */
7453 static int
7454 arm_barrier_cost (rtx insn)
7455 {
7456   /* Basing the location of the pool on the loop depth is preferable,
7457      but at the moment, the basic block information seems to be
7458      corrupt by this stage of the compilation.  */
7459   int base_cost = 50;
7460   rtx next = next_nonnote_insn (insn);
7461
7462   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7463     base_cost -= 20;
7464
7465   switch (GET_CODE (insn))
7466     {
7467     case CODE_LABEL:
7468       /* It will always be better to place the table before the label, rather
7469          than after it.  */
7470       return 50;
7471
7472     case INSN:
7473     case CALL_INSN:
7474       return base_cost;
7475
7476     case JUMP_INSN:
7477       return base_cost - 10;
7478
7479     default:
7480       return base_cost + 10;
7481     }
7482 }
7483
7484 /* Find the best place in the insn stream in the range
7485    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7486    Create the barrier by inserting a jump and add a new fix entry for
7487    it.  */
7488 static Mfix *
7489 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7490 {
7491   HOST_WIDE_INT count = 0;
7492   rtx barrier;
7493   rtx from = fix->insn;
7494   rtx selected = from;
7495   int selected_cost;
7496   HOST_WIDE_INT selected_address;
7497   Mfix * new_fix;
7498   HOST_WIDE_INT max_count = max_address - fix->address;
7499   rtx label = gen_label_rtx ();
7500
7501   selected_cost = arm_barrier_cost (from);
7502   selected_address = fix->address;
7503
7504   while (from && count < max_count)
7505     {
7506       rtx tmp;
7507       int new_cost;
7508
7509       /* This code shouldn't have been called if there was a natural barrier
7510          within range.  */
7511       gcc_assert (GET_CODE (from) != BARRIER);
7512
7513       /* Count the length of this insn.  */
7514       count += get_attr_length (from);
7515
7516       /* If there is a jump table, add its length.  */
7517       tmp = is_jump_table (from);
7518       if (tmp != NULL)
7519         {
7520           count += get_jump_table_size (tmp);
7521
7522           /* Jump tables aren't in a basic block, so base the cost on
7523              the dispatch insn.  If we select this location, we will
7524              still put the pool after the table.  */
7525           new_cost = arm_barrier_cost (from);
7526
7527           if (count < max_count && new_cost <= selected_cost)
7528             {
7529               selected = tmp;
7530               selected_cost = new_cost;
7531               selected_address = fix->address + count;
7532             }
7533
7534           /* Continue after the dispatch table.  */
7535           from = NEXT_INSN (tmp);
7536           continue;
7537         }
7538
7539       new_cost = arm_barrier_cost (from);
7540
7541       if (count < max_count && new_cost <= selected_cost)
7542         {
7543           selected = from;
7544           selected_cost = new_cost;
7545           selected_address = fix->address + count;
7546         }
7547
7548       from = NEXT_INSN (from);
7549     }
7550
7551   /* Create a new JUMP_INSN that branches around a barrier.  */
7552   from = emit_jump_insn_after (gen_jump (label), selected);
7553   JUMP_LABEL (from) = label;
7554   barrier = emit_barrier_after (from);
7555   emit_label_after (label, barrier);
7556
7557   /* Create a minipool barrier entry for the new barrier.  */
7558   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7559   new_fix->insn = barrier;
7560   new_fix->address = selected_address;
7561   new_fix->next = fix->next;
7562   fix->next = new_fix;
7563
7564   return new_fix;
7565 }
7566
7567 /* Record that there is a natural barrier in the insn stream at
7568    ADDRESS.  */
7569 static void
7570 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7571 {
7572   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7573
7574   fix->insn = insn;
7575   fix->address = address;
7576
7577   fix->next = NULL;
7578   if (minipool_fix_head != NULL)
7579     minipool_fix_tail->next = fix;
7580   else
7581     minipool_fix_head = fix;
7582
7583   minipool_fix_tail = fix;
7584 }
7585
7586 /* Record INSN, which will need fixing up to load a value from the
7587    minipool.  ADDRESS is the offset of the insn since the start of the
7588    function; LOC is a pointer to the part of the insn which requires
7589    fixing; VALUE is the constant that must be loaded, which is of type
7590    MODE.  */
7591 static void
7592 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7593                    enum machine_mode mode, rtx value)
7594 {
7595   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7596
7597 #ifdef AOF_ASSEMBLER
7598   /* PIC symbol references need to be converted into offsets into the
7599      based area.  */
7600   /* XXX This shouldn't be done here.  */
7601   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7602     value = aof_pic_entry (value);
7603 #endif /* AOF_ASSEMBLER */
7604
7605   fix->insn = insn;
7606   fix->address = address;
7607   fix->loc = loc;
7608   fix->mode = mode;
7609   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7610   fix->value = value;
7611   fix->forwards = get_attr_pool_range (insn);
7612   fix->backwards = get_attr_neg_pool_range (insn);
7613   fix->minipool = NULL;
7614
7615   /* If an insn doesn't have a range defined for it, then it isn't
7616      expecting to be reworked by this code.  Better to stop now than
7617      to generate duff assembly code.  */
7618   gcc_assert (fix->forwards || fix->backwards);
7619
7620   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7621      So there might be an empty word before the start of the pool.
7622      Hence we reduce the forward range by 4 to allow for this
7623      possibility.  */
7624   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7625     fix->forwards -= 4;
7626
7627   if (dump_file)
7628     {
7629       fprintf (dump_file,
7630                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7631                GET_MODE_NAME (mode),
7632                INSN_UID (insn), (unsigned long) address,
7633                -1 * (long)fix->backwards, (long)fix->forwards);
7634       arm_print_value (dump_file, fix->value);
7635       fprintf (dump_file, "\n");
7636     }
7637
7638   /* Add it to the chain of fixes.  */
7639   fix->next = NULL;
7640
7641   if (minipool_fix_head != NULL)
7642     minipool_fix_tail->next = fix;
7643   else
7644     minipool_fix_head = fix;
7645
7646   minipool_fix_tail = fix;
7647 }
7648
7649 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7650    Returns the number of insns needed, or 99 if we don't know how to
7651    do it.  */
7652 int
7653 arm_const_double_inline_cost (rtx val)
7654 {
7655   rtx lowpart, highpart;
7656   enum machine_mode mode;
7657   
7658   mode = GET_MODE (val);
7659
7660   if (mode == VOIDmode)
7661     mode = DImode;
7662
7663   gcc_assert (GET_MODE_SIZE (mode) == 8);
7664   
7665   lowpart = gen_lowpart (SImode, val);
7666   highpart = gen_highpart_mode (SImode, mode, val);
7667   
7668   gcc_assert (GET_CODE (lowpart) == CONST_INT);
7669   gcc_assert (GET_CODE (highpart) == CONST_INT);
7670
7671   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7672                             NULL_RTX, NULL_RTX, 0, 0)
7673           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7674                               NULL_RTX, NULL_RTX, 0, 0));
7675 }
7676
7677 /* Return true if it is worthwhile to split a 64-bit constant into two
7678    32-bit operations.  This is the case if optimizing for size, or
7679    if we have load delay slots, or if one 32-bit part can be done with
7680    a single data operation.  */
7681 bool
7682 arm_const_double_by_parts (rtx val)
7683 {
7684   enum machine_mode mode = GET_MODE (val);
7685   rtx part;
7686
7687   if (optimize_size || arm_ld_sched)
7688     return true;
7689
7690   if (mode == VOIDmode)
7691     mode = DImode;
7692   
7693   part = gen_highpart_mode (SImode, mode, val);
7694   
7695   gcc_assert (GET_CODE (part) == CONST_INT);
7696   
7697   if (const_ok_for_arm (INTVAL (part))
7698       || const_ok_for_arm (~INTVAL (part)))
7699     return true;
7700   
7701   part = gen_lowpart (SImode, val);
7702   
7703   gcc_assert (GET_CODE (part) == CONST_INT);
7704   
7705   if (const_ok_for_arm (INTVAL (part))
7706       || const_ok_for_arm (~INTVAL (part)))
7707     return true;
7708   
7709   return false;
7710 }
7711
7712 /* Scan INSN and note any of its operands that need fixing.
7713    If DO_PUSHES is false we do not actually push any of the fixups
7714    needed.  The function returns TRUE if any fixups were needed/pushed.
7715    This is used by arm_memory_load_p() which needs to know about loads
7716    of constants that will be converted into minipool loads.  */
7717 static bool
7718 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7719 {
7720   bool result = false;
7721   int opno;
7722
7723   extract_insn (insn);
7724
7725   if (!constrain_operands (1))
7726     fatal_insn_not_found (insn);
7727
7728   if (recog_data.n_alternatives == 0)
7729     return false;
7730
7731   /* Fill in recog_op_alt with information about the constraints of
7732      this insn.  */
7733   preprocess_constraints ();
7734
7735   for (opno = 0; opno < recog_data.n_operands; opno++)
7736     {
7737       /* Things we need to fix can only occur in inputs.  */
7738       if (recog_data.operand_type[opno] != OP_IN)
7739         continue;
7740
7741       /* If this alternative is a memory reference, then any mention
7742          of constants in this alternative is really to fool reload
7743          into allowing us to accept one there.  We need to fix them up
7744          now so that we output the right code.  */
7745       if (recog_op_alt[opno][which_alternative].memory_ok)
7746         {
7747           rtx op = recog_data.operand[opno];
7748
7749           if (CONSTANT_P (op))
7750             {
7751               if (do_pushes)
7752                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7753                                    recog_data.operand_mode[opno], op);
7754               result = true;
7755             }
7756           else if (GET_CODE (op) == MEM
7757                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7758                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7759             {
7760               if (do_pushes)
7761                 {
7762                   rtx cop = avoid_constant_pool_reference (op);
7763
7764                   /* Casting the address of something to a mode narrower
7765                      than a word can cause avoid_constant_pool_reference()
7766                      to return the pool reference itself.  That's no good to
7767                      us here.  Lets just hope that we can use the
7768                      constant pool value directly.  */
7769                   if (op == cop)
7770                     cop = get_pool_constant (XEXP (op, 0));
7771
7772                   push_minipool_fix (insn, address,
7773                                      recog_data.operand_loc[opno],
7774                                      recog_data.operand_mode[opno], cop);
7775                 }
7776
7777               result = true;
7778             }
7779         }
7780     }
7781
7782   return result;
7783 }
7784
7785 /* Gcc puts the pool in the wrong place for ARM, since we can only
7786    load addresses a limited distance around the pc.  We do some
7787    special munging to move the constant pool values to the correct
7788    point in the code.  */
7789 static void
7790 arm_reorg (void)
7791 {
7792   rtx insn;
7793   HOST_WIDE_INT address = 0;
7794   Mfix * fix;
7795
7796   minipool_fix_head = minipool_fix_tail = NULL;
7797
7798   /* The first insn must always be a note, or the code below won't
7799      scan it properly.  */
7800   insn = get_insns ();
7801   gcc_assert (GET_CODE (insn) == NOTE);
7802
7803   /* Scan all the insns and record the operands that will need fixing.  */
7804   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7805     {
7806       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7807           && (arm_cirrus_insn_p (insn)
7808               || GET_CODE (insn) == JUMP_INSN
7809               || arm_memory_load_p (insn)))
7810         cirrus_reorg (insn);
7811
7812       if (GET_CODE (insn) == BARRIER)
7813         push_minipool_barrier (insn, address);
7814       else if (INSN_P (insn))
7815         {
7816           rtx table;
7817
7818           note_invalid_constants (insn, address, true);
7819           address += get_attr_length (insn);
7820
7821           /* If the insn is a vector jump, add the size of the table
7822              and skip the table.  */
7823           if ((table = is_jump_table (insn)) != NULL)
7824             {
7825               address += get_jump_table_size (table);
7826               insn = table;
7827             }
7828         }
7829     }
7830
7831   fix = minipool_fix_head;
7832
7833   /* Now scan the fixups and perform the required changes.  */
7834   while (fix)
7835     {
7836       Mfix * ftmp;
7837       Mfix * fdel;
7838       Mfix *  last_added_fix;
7839       Mfix * last_barrier = NULL;
7840       Mfix * this_fix;
7841
7842       /* Skip any further barriers before the next fix.  */
7843       while (fix && GET_CODE (fix->insn) == BARRIER)
7844         fix = fix->next;
7845
7846       /* No more fixes.  */
7847       if (fix == NULL)
7848         break;
7849
7850       last_added_fix = NULL;
7851
7852       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7853         {
7854           if (GET_CODE (ftmp->insn) == BARRIER)
7855             {
7856               if (ftmp->address >= minipool_vector_head->max_address)
7857                 break;
7858
7859               last_barrier = ftmp;
7860             }
7861           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7862             break;
7863
7864           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7865         }
7866
7867       /* If we found a barrier, drop back to that; any fixes that we
7868          could have reached but come after the barrier will now go in
7869          the next mini-pool.  */
7870       if (last_barrier != NULL)
7871         {
7872           /* Reduce the refcount for those fixes that won't go into this
7873              pool after all.  */
7874           for (fdel = last_barrier->next;
7875                fdel && fdel != ftmp;
7876                fdel = fdel->next)
7877             {
7878               fdel->minipool->refcount--;
7879               fdel->minipool = NULL;
7880             }
7881
7882           ftmp = last_barrier;
7883         }
7884       else
7885         {
7886           /* ftmp is first fix that we can't fit into this pool and
7887              there no natural barriers that we could use.  Insert a
7888              new barrier in the code somewhere between the previous
7889              fix and this one, and arrange to jump around it.  */
7890           HOST_WIDE_INT max_address;
7891
7892           /* The last item on the list of fixes must be a barrier, so
7893              we can never run off the end of the list of fixes without
7894              last_barrier being set.  */
7895           gcc_assert (ftmp);
7896
7897           max_address = minipool_vector_head->max_address;
7898           /* Check that there isn't another fix that is in range that
7899              we couldn't fit into this pool because the pool was
7900              already too large: we need to put the pool before such an
7901              instruction.  */
7902           if (ftmp->address < max_address)
7903             max_address = ftmp->address;
7904
7905           last_barrier = create_fix_barrier (last_added_fix, max_address);
7906         }
7907
7908       assign_minipool_offsets (last_barrier);
7909
7910       while (ftmp)
7911         {
7912           if (GET_CODE (ftmp->insn) != BARRIER
7913               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7914                   == NULL))
7915             break;
7916
7917           ftmp = ftmp->next;
7918         }
7919
7920       /* Scan over the fixes we have identified for this pool, fixing them
7921          up and adding the constants to the pool itself.  */
7922       for (this_fix = fix; this_fix && ftmp != this_fix;
7923            this_fix = this_fix->next)
7924         if (GET_CODE (this_fix->insn) != BARRIER)
7925           {
7926             rtx addr
7927               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7928                                                   minipool_vector_label),
7929                                this_fix->minipool->offset);
7930             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7931           }
7932
7933       dump_minipool (last_barrier->insn);
7934       fix = ftmp;
7935     }
7936
7937   /* From now on we must synthesize any constants that we can't handle
7938      directly.  This can happen if the RTL gets split during final
7939      instruction generation.  */
7940   after_arm_reorg = 1;
7941
7942   /* Free the minipool memory.  */
7943   obstack_free (&minipool_obstack, minipool_startobj);
7944 }
7945 \f
7946 /* Routines to output assembly language.  */
7947
7948 /* If the rtx is the correct value then return the string of the number.
7949    In this way we can ensure that valid double constants are generated even
7950    when cross compiling.  */
7951 const char *
7952 fp_immediate_constant (rtx x)
7953 {
7954   REAL_VALUE_TYPE r;
7955   int i;
7956
7957   if (!fp_consts_inited)
7958     init_fp_table ();
7959
7960   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7961   for (i = 0; i < 8; i++)
7962     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7963       return strings_fp[i];
7964
7965   gcc_unreachable ();
7966 }
7967
7968 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7969 static const char *
7970 fp_const_from_val (REAL_VALUE_TYPE *r)
7971 {
7972   int i;
7973
7974   if (!fp_consts_inited)
7975     init_fp_table ();
7976
7977   for (i = 0; i < 8; i++)
7978     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7979       return strings_fp[i];
7980
7981   gcc_unreachable ();
7982 }
7983
7984 /* Output the operands of a LDM/STM instruction to STREAM.
7985    MASK is the ARM register set mask of which only bits 0-15 are important.
7986    REG is the base register, either the frame pointer or the stack pointer,
7987    INSTR is the possibly suffixed load or store instruction.  */
7988
7989 static void
7990 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7991                  unsigned long mask)
7992 {
7993   unsigned i;
7994   bool not_first = FALSE;
7995
7996   fputc ('\t', stream);
7997   asm_fprintf (stream, instr, reg);
7998   fputs (", {", stream);
7999
8000   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8001     if (mask & (1 << i))
8002       {
8003         if (not_first)
8004           fprintf (stream, ", ");
8005
8006         asm_fprintf (stream, "%r", i);
8007         not_first = TRUE;
8008       }
8009
8010   fprintf (stream, "}\n");
8011 }
8012
8013
8014 /* Output a FLDMX instruction to STREAM.
8015    BASE if the register containing the address.
8016    REG and COUNT specify the register range.
8017    Extra registers may be added to avoid hardware bugs.  */
8018
8019 static void
8020 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8021 {
8022   int i;
8023
8024   /* Workaround ARM10 VFPr1 bug.  */
8025   if (count == 2 && !arm_arch6)
8026     {
8027       if (reg == 15)
8028         reg--;
8029       count++;
8030     }
8031
8032   fputc ('\t', stream);
8033   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8034
8035   for (i = reg; i < reg + count; i++)
8036     {
8037       if (i > reg)
8038         fputs (", ", stream);
8039       asm_fprintf (stream, "d%d", i);
8040     }
8041   fputs ("}\n", stream);
8042
8043 }
8044
8045
8046 /* Output the assembly for a store multiple.  */
8047
8048 const char *
8049 vfp_output_fstmx (rtx * operands)
8050 {
8051   char pattern[100];
8052   int p;
8053   int base;
8054   int i;
8055
8056   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8057   p = strlen (pattern);
8058
8059   gcc_assert (GET_CODE (operands[1]) == REG);
8060
8061   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8062   for (i = 1; i < XVECLEN (operands[2], 0); i++)
8063     {
8064       p += sprintf (&pattern[p], ", d%d", base + i);
8065     }
8066   strcpy (&pattern[p], "}");
8067
8068   output_asm_insn (pattern, operands);
8069   return "";
8070 }
8071
8072
8073 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8074    number of bytes pushed.  */
8075
8076 static int
8077 vfp_emit_fstmx (int base_reg, int count)
8078 {
8079   rtx par;
8080   rtx dwarf;
8081   rtx tmp, reg;
8082   int i;
8083
8084   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8085      register pairs are stored by a store multiple insn.  We avoid this
8086      by pushing an extra pair.  */
8087   if (count == 2 && !arm_arch6)
8088     {
8089       if (base_reg == LAST_VFP_REGNUM - 3)
8090         base_reg -= 2;
8091       count++;
8092     }
8093
8094   /* ??? The frame layout is implementation defined.  We describe
8095      standard format 1 (equivalent to a FSTMD insn and unused pad word).
8096      We really need some way of representing the whole block so that the
8097      unwinder can figure it out at runtime.  */
8098   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8099   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8100
8101   reg = gen_rtx_REG (DFmode, base_reg);
8102   base_reg += 2;
8103
8104   XVECEXP (par, 0, 0)
8105     = gen_rtx_SET (VOIDmode,
8106                    gen_rtx_MEM (BLKmode,
8107                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
8108                    gen_rtx_UNSPEC (BLKmode,
8109                                    gen_rtvec (1, reg),
8110                                    UNSPEC_PUSH_MULT));
8111
8112   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8113                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
8114                                    GEN_INT (-(count * 8 + 4))));
8115   RTX_FRAME_RELATED_P (tmp) = 1;
8116   XVECEXP (dwarf, 0, 0) = tmp;
8117
8118   tmp = gen_rtx_SET (VOIDmode,
8119                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
8120                      reg);
8121   RTX_FRAME_RELATED_P (tmp) = 1;
8122   XVECEXP (dwarf, 0, 1) = tmp;
8123
8124   for (i = 1; i < count; i++)
8125     {
8126       reg = gen_rtx_REG (DFmode, base_reg);
8127       base_reg += 2;
8128       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8129
8130       tmp = gen_rtx_SET (VOIDmode,
8131                          gen_rtx_MEM (DFmode,
8132                                       gen_rtx_PLUS (SImode,
8133                                                     stack_pointer_rtx,
8134                                                     GEN_INT (i * 8))),
8135                          reg);
8136       RTX_FRAME_RELATED_P (tmp) = 1;
8137       XVECEXP (dwarf, 0, i + 1) = tmp;
8138     }
8139
8140   par = emit_insn (par);
8141   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8142                                        REG_NOTES (par));
8143   RTX_FRAME_RELATED_P (par) = 1;
8144
8145   return count * 8 + 4;
8146 }
8147
8148
8149 /* Output a 'call' insn.  */
8150 const char *
8151 output_call (rtx *operands)
8152 {
8153   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8154
8155   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8156   if (REGNO (operands[0]) == LR_REGNUM)
8157     {
8158       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8159       output_asm_insn ("mov%?\t%0, %|lr", operands);
8160     }
8161
8162   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8163
8164   if (TARGET_INTERWORK || arm_arch4t)
8165     output_asm_insn ("bx%?\t%0", operands);
8166   else
8167     output_asm_insn ("mov%?\t%|pc, %0", operands);
8168
8169   return "";
8170 }
8171
8172 /* Output a 'call' insn that is a reference in memory.  */
8173 const char *
8174 output_call_mem (rtx *operands)
8175 {
8176   if (TARGET_INTERWORK && !arm_arch5)
8177     {
8178       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8179       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8180       output_asm_insn ("bx%?\t%|ip", operands);
8181     }
8182   else if (regno_use_in (LR_REGNUM, operands[0]))
8183     {
8184       /* LR is used in the memory address.  We load the address in the
8185          first instruction.  It's safe to use IP as the target of the
8186          load since the call will kill it anyway.  */
8187       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8188       if (arm_arch5)
8189         output_asm_insn ("blx%?\t%|ip", operands);
8190       else
8191         {
8192           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8193           if (arm_arch4t)
8194             output_asm_insn ("bx%?\t%|ip", operands);
8195           else
8196             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8197         }
8198     }
8199   else
8200     {
8201       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8202       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8203     }
8204
8205   return "";
8206 }
8207
8208
8209 /* Output a move from arm registers to an fpa registers.
8210    OPERANDS[0] is an fpa register.
8211    OPERANDS[1] is the first registers of an arm register pair.  */
8212 const char *
8213 output_mov_long_double_fpa_from_arm (rtx *operands)
8214 {
8215   int arm_reg0 = REGNO (operands[1]);
8216   rtx ops[3];
8217
8218   gcc_assert (arm_reg0 != IP_REGNUM);
8219
8220   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8221   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8222   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8223
8224   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8225   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8226
8227   return "";
8228 }
8229
8230 /* Output a move from an fpa register to arm registers.
8231    OPERANDS[0] is the first registers of an arm register pair.
8232    OPERANDS[1] is an fpa register.  */
8233 const char *
8234 output_mov_long_double_arm_from_fpa (rtx *operands)
8235 {
8236   int arm_reg0 = REGNO (operands[0]);
8237   rtx ops[3];
8238
8239   gcc_assert (arm_reg0 != IP_REGNUM);
8240
8241   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8242   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8243   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8244
8245   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8246   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8247   return "";
8248 }
8249
8250 /* Output a move from arm registers to arm registers of a long double
8251    OPERANDS[0] is the destination.
8252    OPERANDS[1] is the source.  */
8253 const char *
8254 output_mov_long_double_arm_from_arm (rtx *operands)
8255 {
8256   /* We have to be careful here because the two might overlap.  */
8257   int dest_start = REGNO (operands[0]);
8258   int src_start = REGNO (operands[1]);
8259   rtx ops[2];
8260   int i;
8261
8262   if (dest_start < src_start)
8263     {
8264       for (i = 0; i < 3; i++)
8265         {
8266           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8267           ops[1] = gen_rtx_REG (SImode, src_start + i);
8268           output_asm_insn ("mov%?\t%0, %1", ops);
8269         }
8270     }
8271   else
8272     {
8273       for (i = 2; i >= 0; i--)
8274         {
8275           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8276           ops[1] = gen_rtx_REG (SImode, src_start + i);
8277           output_asm_insn ("mov%?\t%0, %1", ops);
8278         }
8279     }
8280
8281   return "";
8282 }
8283
8284
8285 /* Output a move from arm registers to an fpa registers.
8286    OPERANDS[0] is an fpa register.
8287    OPERANDS[1] is the first registers of an arm register pair.  */
8288 const char *
8289 output_mov_double_fpa_from_arm (rtx *operands)
8290 {
8291   int arm_reg0 = REGNO (operands[1]);
8292   rtx ops[2];
8293
8294   gcc_assert (arm_reg0 != IP_REGNUM);
8295
8296   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8297   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8298   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8299   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8300   return "";
8301 }
8302
8303 /* Output a move from an fpa register to arm registers.
8304    OPERANDS[0] is the first registers of an arm register pair.
8305    OPERANDS[1] is an fpa register.  */
8306 const char *
8307 output_mov_double_arm_from_fpa (rtx *operands)
8308 {
8309   int arm_reg0 = REGNO (operands[0]);
8310   rtx ops[2];
8311
8312   gcc_assert (arm_reg0 != IP_REGNUM);
8313
8314   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8315   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8316   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8317   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8318   return "";
8319 }
8320
8321 /* Output a move between double words.
8322    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8323    or MEM<-REG and all MEMs must be offsettable addresses.  */
8324 const char *
8325 output_move_double (rtx *operands)
8326 {
8327   enum rtx_code code0 = GET_CODE (operands[0]);
8328   enum rtx_code code1 = GET_CODE (operands[1]);
8329   rtx otherops[3];
8330
8331   if (code0 == REG)
8332     {
8333       int reg0 = REGNO (operands[0]);
8334
8335       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8336
8337       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
8338
8339       switch (GET_CODE (XEXP (operands[1], 0)))
8340         {
8341         case REG:
8342           output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8343           break;
8344           
8345         case PRE_INC:
8346           gcc_assert (TARGET_LDRD);
8347           output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8348           break;
8349           
8350         case PRE_DEC:
8351           output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8352           break;
8353           
8354         case POST_INC:
8355           output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8356           break;
8357           
8358         case POST_DEC:
8359           gcc_assert (TARGET_LDRD);
8360           output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8361           break;
8362           
8363         case PRE_MODIFY:
8364         case POST_MODIFY:
8365           otherops[0] = operands[0];
8366           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8367           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8368           
8369           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8370             {
8371               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8372                 {
8373                   /* Registers overlap so split out the increment.  */
8374                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
8375                   output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8376                 }
8377               else
8378                 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8379             }
8380           else
8381             {
8382               /* We only allow constant increments, so this is safe.  */
8383               output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8384             }
8385           break;
8386           
8387         case LABEL_REF:
8388         case CONST:
8389           output_asm_insn ("adr%?\t%0, %1", operands);
8390           output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8391           break;
8392           
8393         default:
8394           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8395                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8396             {
8397               otherops[0] = operands[0];
8398               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8399               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8400               
8401               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8402                 {
8403                   if (GET_CODE (otherops[2]) == CONST_INT)
8404                     {
8405                       switch ((int) INTVAL (otherops[2]))
8406                         {
8407                         case -8:
8408                           output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8409                           return "";
8410                         case -4:
8411                           output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8412                           return "";
8413                         case 4:
8414                           output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8415                           return "";
8416                         }
8417                     }
8418                   if (TARGET_LDRD
8419                       && (GET_CODE (otherops[2]) == REG
8420                           || (GET_CODE (otherops[2]) == CONST_INT
8421                               && INTVAL (otherops[2]) > -256
8422                               && INTVAL (otherops[2]) < 256)))
8423                     {
8424                       if (reg_overlap_mentioned_p (otherops[0],
8425                                                    otherops[2]))
8426                         {
8427                           /* Swap base and index registers over to
8428                              avoid a conflict.  */
8429                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8430                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8431                           
8432                         }
8433                       /* If both registers conflict, it will usually
8434                          have been fixed by a splitter.  */
8435                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8436                         {
8437                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
8438                           output_asm_insn ("ldr%?d\t%0, [%1]",
8439                                            otherops);
8440                         }
8441                       else
8442                         output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8443                       return "";
8444                     }
8445                   
8446                   if (GET_CODE (otherops[2]) == CONST_INT)
8447                     {
8448                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8449                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8450                       else
8451                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8452                     }
8453                   else
8454                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
8455                 }
8456               else
8457                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8458
8459               return "ldm%?ia\t%0, %M0";
8460             }
8461           else
8462             {
8463               otherops[1] = adjust_address (operands[1], SImode, 4);
8464               /* Take care of overlapping base/data reg.  */
8465               if (reg_mentioned_p (operands[0], operands[1]))
8466                 {
8467                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8468                   output_asm_insn ("ldr%?\t%0, %1", operands);
8469                 }
8470               else
8471                 {
8472                   output_asm_insn ("ldr%?\t%0, %1", operands);
8473                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8474                 }
8475             }
8476         }
8477     }
8478   else
8479     {
8480       /* Constraints should ensure this.  */
8481       gcc_assert (code0 == MEM && code1 == REG);
8482       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8483
8484       switch (GET_CODE (XEXP (operands[0], 0)))
8485         {
8486         case REG:
8487           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8488           break;
8489
8490         case PRE_INC:
8491           gcc_assert (TARGET_LDRD);
8492           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8493           break;
8494
8495         case PRE_DEC:
8496           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8497           break;
8498
8499         case POST_INC:
8500           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8501           break;
8502
8503         case POST_DEC:
8504           gcc_assert (TARGET_LDRD);
8505           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8506           break;
8507
8508         case PRE_MODIFY:
8509         case POST_MODIFY:
8510           otherops[0] = operands[1];
8511           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8512           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8513
8514           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8515             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8516           else
8517             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8518           break;
8519
8520         case PLUS:
8521           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8522           if (GET_CODE (otherops[2]) == CONST_INT)
8523             {
8524               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8525                 {
8526                 case -8:
8527                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8528                   return "";
8529
8530                 case -4:
8531                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8532                   return "";
8533
8534                 case 4:
8535                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8536                   return "";
8537                 }
8538             }
8539           if (TARGET_LDRD
8540               && (GET_CODE (otherops[2]) == REG
8541                   || (GET_CODE (otherops[2]) == CONST_INT
8542                       && INTVAL (otherops[2]) > -256
8543                       && INTVAL (otherops[2]) < 256)))
8544             {
8545               otherops[0] = operands[1];
8546               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8547               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8548               return "";
8549             }
8550           /* Fall through */
8551
8552         default:
8553           otherops[0] = adjust_address (operands[0], SImode, 4);
8554           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8555           output_asm_insn ("str%?\t%1, %0", operands);
8556           output_asm_insn ("str%?\t%1, %0", otherops);
8557         }
8558     }
8559
8560   return "";
8561 }
8562
8563 /* Output an ADD r, s, #n where n may be too big for one instruction.
8564    If adding zero to one register, output nothing.  */
8565 const char *
8566 output_add_immediate (rtx *operands)
8567 {
8568   HOST_WIDE_INT n = INTVAL (operands[2]);
8569
8570   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8571     {
8572       if (n < 0)
8573         output_multi_immediate (operands,
8574                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8575                                 -n);
8576       else
8577         output_multi_immediate (operands,
8578                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8579                                 n);
8580     }
8581
8582   return "";
8583 }
8584
8585 /* Output a multiple immediate operation.
8586    OPERANDS is the vector of operands referred to in the output patterns.
8587    INSTR1 is the output pattern to use for the first constant.
8588    INSTR2 is the output pattern to use for subsequent constants.
8589    IMMED_OP is the index of the constant slot in OPERANDS.
8590    N is the constant value.  */
8591 static const char *
8592 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8593                         int immed_op, HOST_WIDE_INT n)
8594 {
8595 #if HOST_BITS_PER_WIDE_INT > 32
8596   n &= 0xffffffff;
8597 #endif
8598
8599   if (n == 0)
8600     {
8601       /* Quick and easy output.  */
8602       operands[immed_op] = const0_rtx;
8603       output_asm_insn (instr1, operands);
8604     }
8605   else
8606     {
8607       int i;
8608       const char * instr = instr1;
8609
8610       /* Note that n is never zero here (which would give no output).  */
8611       for (i = 0; i < 32; i += 2)
8612         {
8613           if (n & (3 << i))
8614             {
8615               operands[immed_op] = GEN_INT (n & (255 << i));
8616               output_asm_insn (instr, operands);
8617               instr = instr2;
8618               i += 6;
8619             }
8620         }
8621     }
8622
8623   return "";
8624 }
8625
8626 /* Return the appropriate ARM instruction for the operation code.
8627    The returned result should not be overwritten.  OP is the rtx of the
8628    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8629    was shifted.  */
8630 const char *
8631 arithmetic_instr (rtx op, int shift_first_arg)
8632 {
8633   switch (GET_CODE (op))
8634     {
8635     case PLUS:
8636       return "add";
8637
8638     case MINUS:
8639       return shift_first_arg ? "rsb" : "sub";
8640
8641     case IOR:
8642       return "orr";
8643
8644     case XOR:
8645       return "eor";
8646
8647     case AND:
8648       return "and";
8649
8650     default:
8651       gcc_unreachable ();
8652     }
8653 }
8654
8655 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8656    for the operation code.  The returned result should not be overwritten.
8657    OP is the rtx code of the shift.
8658    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8659    shift.  */
8660 static const char *
8661 shift_op (rtx op, HOST_WIDE_INT *amountp)
8662 {
8663   const char * mnem;
8664   enum rtx_code code = GET_CODE (op);
8665
8666   switch (GET_CODE (XEXP (op, 1)))
8667     {
8668     case REG:
8669     case SUBREG:
8670       *amountp = -1;
8671       break;
8672
8673     case CONST_INT:
8674       *amountp = INTVAL (XEXP (op, 1));
8675       break;
8676
8677     default:
8678       gcc_unreachable ();
8679     }
8680
8681   switch (code)
8682     {
8683     case ASHIFT:
8684       mnem = "asl";
8685       break;
8686
8687     case ASHIFTRT:
8688       mnem = "asr";
8689       break;
8690
8691     case LSHIFTRT:
8692       mnem = "lsr";
8693       break;
8694
8695     case ROTATE:
8696       gcc_assert (*amountp != -1);
8697       *amountp = 32 - *amountp;
8698
8699       /* Fall through.  */
8700
8701     case ROTATERT:
8702       mnem = "ror";
8703       break;
8704
8705     case MULT:
8706       /* We never have to worry about the amount being other than a
8707          power of 2, since this case can never be reloaded from a reg.  */
8708       gcc_assert (*amountp != -1);
8709       *amountp = int_log2 (*amountp);
8710       return "asl";
8711
8712     default:
8713       gcc_unreachable ();
8714     }
8715
8716   if (*amountp != -1)
8717     {
8718       /* This is not 100% correct, but follows from the desire to merge
8719          multiplication by a power of 2 with the recognizer for a
8720          shift.  >=32 is not a valid shift for "asl", so we must try and
8721          output a shift that produces the correct arithmetical result.
8722          Using lsr #32 is identical except for the fact that the carry bit
8723          is not set correctly if we set the flags; but we never use the
8724          carry bit from such an operation, so we can ignore that.  */
8725       if (code == ROTATERT)
8726         /* Rotate is just modulo 32.  */
8727         *amountp &= 31;
8728       else if (*amountp != (*amountp & 31))
8729         {
8730           if (code == ASHIFT)
8731             mnem = "lsr";
8732           *amountp = 32;
8733         }
8734
8735       /* Shifts of 0 are no-ops.  */
8736       if (*amountp == 0)
8737         return NULL;
8738     }
8739
8740   return mnem;
8741 }
8742
8743 /* Obtain the shift from the POWER of two.  */
8744
8745 static HOST_WIDE_INT
8746 int_log2 (HOST_WIDE_INT power)
8747 {
8748   HOST_WIDE_INT shift = 0;
8749
8750   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8751     {
8752       gcc_assert (shift <= 31);
8753       shift++;
8754     }
8755
8756   return shift;
8757 }
8758
8759 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
8760    because /bin/as is horribly restrictive.  The judgement about
8761    whether or not each character is 'printable' (and can be output as
8762    is) or not (and must be printed with an octal escape) must be made
8763    with reference to the *host* character set -- the situation is
8764    similar to that discussed in the comments above pp_c_char in
8765    c-pretty-print.c.  */
8766
8767 #define MAX_ASCII_LEN 51
8768
8769 void
8770 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8771 {
8772   int i;
8773   int len_so_far = 0;
8774
8775   fputs ("\t.ascii\t\"", stream);
8776
8777   for (i = 0; i < len; i++)
8778     {
8779       int c = p[i];
8780
8781       if (len_so_far >= MAX_ASCII_LEN)
8782         {
8783           fputs ("\"\n\t.ascii\t\"", stream);
8784           len_so_far = 0;
8785         }
8786
8787       if (ISPRINT (c))
8788         {
8789           if (c == '\\' || c == '\"')
8790             {
8791               putc ('\\', stream);
8792               len_so_far++;
8793             }
8794           putc (c, stream);
8795           len_so_far++;
8796         }
8797       else
8798         {
8799           fprintf (stream, "\\%03o", c);
8800           len_so_far += 4;
8801         }
8802     }
8803
8804   fputs ("\"\n", stream);
8805 }
8806 \f
8807 /* Compute the register save mask for registers 0 through 12
8808    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8809
8810 static unsigned long
8811 arm_compute_save_reg0_reg12_mask (void)
8812 {
8813   unsigned long func_type = arm_current_func_type ();
8814   unsigned long save_reg_mask = 0;
8815   unsigned int reg;
8816
8817   if (IS_INTERRUPT (func_type))
8818     {
8819       unsigned int max_reg;
8820       /* Interrupt functions must not corrupt any registers,
8821          even call clobbered ones.  If this is a leaf function
8822          we can just examine the registers used by the RTL, but
8823          otherwise we have to assume that whatever function is
8824          called might clobber anything, and so we have to save
8825          all the call-clobbered registers as well.  */
8826       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8827         /* FIQ handlers have registers r8 - r12 banked, so
8828            we only need to check r0 - r7, Normal ISRs only
8829            bank r14 and r15, so we must check up to r12.
8830            r13 is the stack pointer which is always preserved,
8831            so we do not need to consider it here.  */
8832         max_reg = 7;
8833       else
8834         max_reg = 12;
8835
8836       for (reg = 0; reg <= max_reg; reg++)
8837         if (regs_ever_live[reg]
8838             || (! current_function_is_leaf && call_used_regs [reg]))
8839           save_reg_mask |= (1 << reg);
8840
8841       /* Also save the pic base register if necessary.  */
8842       if (flag_pic
8843           && !TARGET_SINGLE_PIC_BASE
8844           && current_function_uses_pic_offset_table)
8845         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8846     }
8847   else
8848     {
8849       /* In the normal case we only need to save those registers
8850          which are call saved and which are used by this function.  */
8851       for (reg = 0; reg <= 10; reg++)
8852         if (regs_ever_live[reg] && ! call_used_regs [reg])
8853           save_reg_mask |= (1 << reg);
8854
8855       /* Handle the frame pointer as a special case.  */
8856       if (! TARGET_APCS_FRAME
8857           && ! frame_pointer_needed
8858           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8859           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8860         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8861
8862       /* If we aren't loading the PIC register,
8863          don't stack it even though it may be live.  */
8864       if (flag_pic
8865           && !TARGET_SINGLE_PIC_BASE 
8866           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8867               || current_function_uses_pic_offset_table))
8868         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8869     }
8870
8871   /* Save registers so the exception handler can modify them.  */
8872   if (current_function_calls_eh_return)
8873     {
8874       unsigned int i;
8875
8876       for (i = 0; ; i++)
8877         {
8878           reg = EH_RETURN_DATA_REGNO (i);
8879           if (reg == INVALID_REGNUM)
8880             break;
8881           save_reg_mask |= 1 << reg;
8882         }
8883     }
8884
8885   return save_reg_mask;
8886 }
8887
8888 /* Compute a bit mask of which registers need to be
8889    saved on the stack for the current function.  */
8890
8891 static unsigned long
8892 arm_compute_save_reg_mask (void)
8893 {
8894   unsigned int save_reg_mask = 0;
8895   unsigned long func_type = arm_current_func_type ();
8896
8897   if (IS_NAKED (func_type))
8898     /* This should never really happen.  */
8899     return 0;
8900
8901   /* If we are creating a stack frame, then we must save the frame pointer,
8902      IP (which will hold the old stack pointer), LR and the PC.  */
8903   if (frame_pointer_needed)
8904     save_reg_mask |=
8905       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8906       | (1 << IP_REGNUM)
8907       | (1 << LR_REGNUM)
8908       | (1 << PC_REGNUM);
8909
8910   /* Volatile functions do not return, so there
8911      is no need to save any other registers.  */
8912   if (IS_VOLATILE (func_type))
8913     return save_reg_mask;
8914
8915   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8916
8917   /* Decide if we need to save the link register.
8918      Interrupt routines have their own banked link register,
8919      so they never need to save it.
8920      Otherwise if we do not use the link register we do not need to save
8921      it.  If we are pushing other registers onto the stack however, we
8922      can save an instruction in the epilogue by pushing the link register
8923      now and then popping it back into the PC.  This incurs extra memory
8924      accesses though, so we only do it when optimizing for size, and only
8925      if we know that we will not need a fancy return sequence.  */
8926   if (regs_ever_live [LR_REGNUM]
8927           || (save_reg_mask
8928               && optimize_size
8929               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8930               && !current_function_calls_eh_return))
8931     save_reg_mask |= 1 << LR_REGNUM;
8932
8933   if (cfun->machine->lr_save_eliminated)
8934     save_reg_mask &= ~ (1 << LR_REGNUM);
8935
8936   if (TARGET_REALLY_IWMMXT
8937       && ((bit_count (save_reg_mask)
8938            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8939     {
8940       unsigned int reg;
8941
8942       /* The total number of registers that are going to be pushed
8943          onto the stack is odd.  We need to ensure that the stack
8944          is 64-bit aligned before we start to save iWMMXt registers,
8945          and also before we start to create locals.  (A local variable
8946          might be a double or long long which we will load/store using
8947          an iWMMXt instruction).  Therefore we need to push another
8948          ARM register, so that the stack will be 64-bit aligned.  We
8949          try to avoid using the arg registers (r0 -r3) as they might be
8950          used to pass values in a tail call.  */
8951       for (reg = 4; reg <= 12; reg++)
8952         if ((save_reg_mask & (1 << reg)) == 0)
8953           break;
8954
8955       if (reg <= 12)
8956         save_reg_mask |= (1 << reg);
8957       else
8958         {
8959           cfun->machine->sibcall_blocked = 1;
8960           save_reg_mask |= (1 << 3);
8961         }
8962     }
8963
8964   return save_reg_mask;
8965 }
8966
8967
8968 /* Compute a bit mask of which registers need to be
8969    saved on the stack for the current function.  */
8970 static unsigned long
8971 thumb_compute_save_reg_mask (void)
8972 {
8973   unsigned long mask;
8974   unsigned reg;
8975
8976   mask = 0;
8977   for (reg = 0; reg < 12; reg ++)
8978     if (regs_ever_live[reg] && !call_used_regs[reg])
8979       mask |= 1 << reg;
8980
8981   if (flag_pic
8982       && !TARGET_SINGLE_PIC_BASE
8983       && current_function_uses_pic_offset_table)
8984     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8985
8986   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
8987   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8988     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8989
8990   /* LR will also be pushed if any lo regs are pushed.  */
8991   if (mask & 0xff || thumb_force_lr_save ())
8992     mask |= (1 << LR_REGNUM);
8993
8994   /* Make sure we have a low work register if we need one.
8995      We will need one if we are going to push a high register,
8996      but we are not currently intending to push a low register.  */
8997   if ((mask & 0xff) == 0
8998       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8999     {
9000       /* Use thumb_find_work_register to choose which register
9001          we will use.  If the register is live then we will
9002          have to push it.  Use LAST_LO_REGNUM as our fallback
9003          choice for the register to select.  */
9004       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9005
9006       if (! call_used_regs[reg])
9007         mask |= 1 << reg;
9008     }
9009
9010   return mask;
9011 }
9012
9013
9014 /* Return the number of bytes required to save VFP registers.  */
9015 static int
9016 arm_get_vfp_saved_size (void)
9017 {
9018   unsigned int regno;
9019   int count;
9020   int saved;
9021
9022   saved = 0;
9023   /* Space for saved VFP registers.  */
9024   if (TARGET_HARD_FLOAT && TARGET_VFP)
9025     {
9026       count = 0;
9027       for (regno = FIRST_VFP_REGNUM;
9028            regno < LAST_VFP_REGNUM;
9029            regno += 2)
9030         {
9031           if ((!regs_ever_live[regno] || call_used_regs[regno])
9032               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9033             {
9034               if (count > 0)
9035                 {
9036                   /* Workaround ARM10 VFPr1 bug.  */
9037                   if (count == 2 && !arm_arch6)
9038                     count++;
9039                   saved += count * 8 + 4;
9040                 }
9041               count = 0;
9042             }
9043           else
9044             count++;
9045         }
9046       if (count > 0)
9047         {
9048           if (count == 2 && !arm_arch6)
9049             count++;
9050           saved += count * 8 + 4;
9051         }
9052     }
9053   return saved;
9054 }
9055
9056
9057 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9058    everything bar the final return instruction.  */
9059 const char *
9060 output_return_instruction (rtx operand, int really_return, int reverse)
9061 {
9062   char conditional[10];
9063   char instr[100];
9064   unsigned reg;
9065   unsigned long live_regs_mask;
9066   unsigned long func_type;
9067   arm_stack_offsets *offsets;
9068
9069   func_type = arm_current_func_type ();
9070
9071   if (IS_NAKED (func_type))
9072     return "";
9073
9074   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9075     {
9076       /* If this function was declared non-returning, and we have
9077          found a tail call, then we have to trust that the called
9078          function won't return.  */
9079       if (really_return)
9080         {
9081           rtx ops[2];
9082
9083           /* Otherwise, trap an attempted return by aborting.  */
9084           ops[0] = operand;
9085           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9086                                        : "abort");
9087           assemble_external_libcall (ops[1]);
9088           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9089         }
9090
9091       return "";
9092     }
9093
9094   gcc_assert (!current_function_calls_alloca || really_return);
9095
9096   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9097
9098   return_used_this_function = 1;
9099
9100   live_regs_mask = arm_compute_save_reg_mask ();
9101
9102   if (live_regs_mask)
9103     {
9104       const char * return_reg;
9105
9106       /* If we do not have any special requirements for function exit
9107          (e.g. interworking, or ISR) then we can load the return address
9108          directly into the PC.  Otherwise we must load it into LR.  */
9109       if (really_return
9110           && ! TARGET_INTERWORK)
9111         return_reg = reg_names[PC_REGNUM];
9112       else
9113         return_reg = reg_names[LR_REGNUM];
9114
9115       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9116         {
9117           /* There are three possible reasons for the IP register
9118              being saved.  1) a stack frame was created, in which case
9119              IP contains the old stack pointer, or 2) an ISR routine
9120              corrupted it, or 3) it was saved to align the stack on
9121              iWMMXt.  In case 1, restore IP into SP, otherwise just
9122              restore IP.  */
9123           if (frame_pointer_needed)
9124             {
9125               live_regs_mask &= ~ (1 << IP_REGNUM);
9126               live_regs_mask |=   (1 << SP_REGNUM);
9127             }
9128           else
9129             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9130         }
9131
9132       /* On some ARM architectures it is faster to use LDR rather than
9133          LDM to load a single register.  On other architectures, the
9134          cost is the same.  In 26 bit mode, or for exception handlers,
9135          we have to use LDM to load the PC so that the CPSR is also
9136          restored.  */
9137       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9138         if (live_regs_mask == (1U << reg))
9139           break;
9140
9141       if (reg <= LAST_ARM_REGNUM
9142           && (reg != LR_REGNUM
9143               || ! really_return
9144               || ! IS_INTERRUPT (func_type)))
9145         {
9146           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9147                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9148         }
9149       else
9150         {
9151           char *p;
9152           int first = 1;
9153
9154           /* Generate the load multiple instruction to restore the
9155              registers.  Note we can get here, even if
9156              frame_pointer_needed is true, but only if sp already
9157              points to the base of the saved core registers.  */
9158           if (live_regs_mask & (1 << SP_REGNUM))
9159             {
9160               unsigned HOST_WIDE_INT stack_adjust;
9161
9162               offsets = arm_get_frame_offsets ();
9163               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9164               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9165
9166               if (stack_adjust && arm_arch5)
9167                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9168               else
9169                 {
9170                   /* If we can't use ldmib (SA110 bug),
9171                      then try to pop r3 instead.  */
9172                   if (stack_adjust)
9173                     live_regs_mask |= 1 << 3;
9174                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9175                 }
9176             }
9177           else
9178             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9179
9180           p = instr + strlen (instr);
9181
9182           for (reg = 0; reg <= SP_REGNUM; reg++)
9183             if (live_regs_mask & (1 << reg))
9184               {
9185                 int l = strlen (reg_names[reg]);
9186
9187                 if (first)
9188                   first = 0;
9189                 else
9190                   {
9191                     memcpy (p, ", ", 2);
9192                     p += 2;
9193                   }
9194
9195                 memcpy (p, "%|", 2);
9196                 memcpy (p + 2, reg_names[reg], l);
9197                 p += l + 2;
9198               }
9199
9200           if (live_regs_mask & (1 << LR_REGNUM))
9201             {
9202               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9203               /* If returning from an interrupt, restore the CPSR.  */
9204               if (IS_INTERRUPT (func_type))
9205                 strcat (p, "^");
9206             }
9207           else
9208             strcpy (p, "}");
9209         }
9210
9211       output_asm_insn (instr, & operand);
9212
9213       /* See if we need to generate an extra instruction to
9214          perform the actual function return.  */
9215       if (really_return
9216           && func_type != ARM_FT_INTERWORKED
9217           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9218         {
9219           /* The return has already been handled
9220              by loading the LR into the PC.  */
9221           really_return = 0;
9222         }
9223     }
9224
9225   if (really_return)
9226     {
9227       switch ((int) ARM_FUNC_TYPE (func_type))
9228         {
9229         case ARM_FT_ISR:
9230         case ARM_FT_FIQ:
9231           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9232           break;
9233
9234         case ARM_FT_INTERWORKED:
9235           sprintf (instr, "bx%s\t%%|lr", conditional);
9236           break;
9237
9238         case ARM_FT_EXCEPTION:
9239           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9240           break;
9241
9242         default:
9243           /* Use bx if it's available.  */
9244           if (arm_arch5 || arm_arch4t)
9245             sprintf (instr, "bx%s\t%%|lr", conditional);
9246           else
9247             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9248           break;
9249         }
9250
9251       output_asm_insn (instr, & operand);
9252     }
9253
9254   return "";
9255 }
9256
9257 /* Write the function name into the code section, directly preceding
9258    the function prologue.
9259
9260    Code will be output similar to this:
9261      t0
9262          .ascii "arm_poke_function_name", 0
9263          .align
9264      t1
9265          .word 0xff000000 + (t1 - t0)
9266      arm_poke_function_name
9267          mov     ip, sp
9268          stmfd   sp!, {fp, ip, lr, pc}
9269          sub     fp, ip, #4
9270
9271    When performing a stack backtrace, code can inspect the value
9272    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9273    at location pc - 12 and the top 8 bits are set, then we know
9274    that there is a function name embedded immediately preceding this
9275    location and has length ((pc[-3]) & 0xff000000).
9276
9277    We assume that pc is declared as a pointer to an unsigned long.
9278
9279    It is of no benefit to output the function name if we are assembling
9280    a leaf function.  These function types will not contain a stack
9281    backtrace structure, therefore it is not possible to determine the
9282    function name.  */
9283 void
9284 arm_poke_function_name (FILE *stream, const char *name)
9285 {
9286   unsigned long alignlength;
9287   unsigned long length;
9288   rtx           x;
9289
9290   length      = strlen (name) + 1;
9291   alignlength = ROUND_UP_WORD (length);
9292
9293   ASM_OUTPUT_ASCII (stream, name, length);
9294   ASM_OUTPUT_ALIGN (stream, 2);
9295   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9296   assemble_aligned_integer (UNITS_PER_WORD, x);
9297 }
9298
9299 /* Place some comments into the assembler stream
9300    describing the current function.  */
9301 static void
9302 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9303 {
9304   unsigned long func_type;
9305
9306   if (!TARGET_ARM)
9307     {
9308       thumb_output_function_prologue (f, frame_size);
9309       return;
9310     }
9311
9312   /* Sanity check.  */
9313   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9314
9315   func_type = arm_current_func_type ();
9316
9317   switch ((int) ARM_FUNC_TYPE (func_type))
9318     {
9319     default:
9320     case ARM_FT_NORMAL:
9321       break;
9322     case ARM_FT_INTERWORKED:
9323       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9324       break;
9325     case ARM_FT_ISR:
9326       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9327       break;
9328     case ARM_FT_FIQ:
9329       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9330       break;
9331     case ARM_FT_EXCEPTION:
9332       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9333       break;
9334     }
9335
9336   if (IS_NAKED (func_type))
9337     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9338
9339   if (IS_VOLATILE (func_type))
9340     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9341
9342   if (IS_NESTED (func_type))
9343     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9344
9345   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9346                current_function_args_size,
9347                current_function_pretend_args_size, frame_size);
9348
9349   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9350                frame_pointer_needed,
9351                cfun->machine->uses_anonymous_args);
9352
9353   if (cfun->machine->lr_save_eliminated)
9354     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9355
9356   if (current_function_calls_eh_return)
9357     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9358
9359 #ifdef AOF_ASSEMBLER
9360   if (flag_pic)
9361     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9362 #endif
9363
9364   return_used_this_function = 0;
9365 }
9366
9367 const char *
9368 arm_output_epilogue (rtx sibling)
9369 {
9370   int reg;
9371   unsigned long saved_regs_mask;
9372   unsigned long func_type;
9373   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9374      frame that is $fp + 4 for a non-variadic function.  */
9375   int floats_offset = 0;
9376   rtx operands[3];
9377   FILE * f = asm_out_file;
9378   unsigned int lrm_count = 0;
9379   int really_return = (sibling == NULL);
9380   int start_reg;
9381   arm_stack_offsets *offsets;
9382
9383   /* If we have already generated the return instruction
9384      then it is futile to generate anything else.  */
9385   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9386     return "";
9387
9388   func_type = arm_current_func_type ();
9389
9390   if (IS_NAKED (func_type))
9391     /* Naked functions don't have epilogues.  */
9392     return "";
9393
9394   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9395     {
9396       rtx op;
9397
9398       /* A volatile function should never return.  Call abort.  */
9399       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9400       assemble_external_libcall (op);
9401       output_asm_insn ("bl\t%a0", &op);
9402
9403       return "";
9404     }
9405
9406   /* If we are throwing an exception, then we really must be doing a
9407      return, so we can't tail-call.  */
9408   gcc_assert (!current_function_calls_eh_return || really_return);
9409
9410   offsets = arm_get_frame_offsets ();
9411   saved_regs_mask = arm_compute_save_reg_mask ();
9412
9413   if (TARGET_IWMMXT)
9414     lrm_count = bit_count (saved_regs_mask);
9415
9416   floats_offset = offsets->saved_args;
9417   /* Compute how far away the floats will be.  */
9418   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9419     if (saved_regs_mask & (1 << reg))
9420       floats_offset += 4;
9421
9422   if (frame_pointer_needed)
9423     {
9424       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9425       int vfp_offset = offsets->frame;
9426
9427       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9428         {
9429           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9430             if (regs_ever_live[reg] && !call_used_regs[reg])
9431               {
9432                 floats_offset += 12;
9433                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9434                              reg, FP_REGNUM, floats_offset - vfp_offset);
9435               }
9436         }
9437       else
9438         {
9439           start_reg = LAST_FPA_REGNUM;
9440
9441           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9442             {
9443               if (regs_ever_live[reg] && !call_used_regs[reg])
9444                 {
9445                   floats_offset += 12;
9446
9447                   /* We can't unstack more than four registers at once.  */
9448                   if (start_reg - reg == 3)
9449                     {
9450                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9451                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9452                       start_reg = reg - 1;
9453                     }
9454                 }
9455               else
9456                 {
9457                   if (reg != start_reg)
9458                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9459                                  reg + 1, start_reg - reg,
9460                                  FP_REGNUM, floats_offset - vfp_offset);
9461                   start_reg = reg - 1;
9462                 }
9463             }
9464
9465           /* Just in case the last register checked also needs unstacking.  */
9466           if (reg != start_reg)
9467             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9468                          reg + 1, start_reg - reg,
9469                          FP_REGNUM, floats_offset - vfp_offset);
9470         }
9471
9472       if (TARGET_HARD_FLOAT && TARGET_VFP)
9473         {
9474           int saved_size;
9475
9476           /* The fldmx insn does not have base+offset addressing modes,
9477              so we use IP to hold the address.  */
9478           saved_size = arm_get_vfp_saved_size ();
9479
9480           if (saved_size > 0)
9481             {
9482               floats_offset += saved_size;
9483               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9484                            FP_REGNUM, floats_offset - vfp_offset);
9485             }
9486           start_reg = FIRST_VFP_REGNUM;
9487           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9488             {
9489               if ((!regs_ever_live[reg] || call_used_regs[reg])
9490                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9491                 {
9492                   if (start_reg != reg)
9493                     arm_output_fldmx (f, IP_REGNUM,
9494                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9495                                       (reg - start_reg) / 2);
9496                   start_reg = reg + 2;
9497                 }
9498             }
9499           if (start_reg != reg)
9500             arm_output_fldmx (f, IP_REGNUM,
9501                               (start_reg - FIRST_VFP_REGNUM) / 2,
9502                               (reg - start_reg) / 2);
9503         }
9504
9505       if (TARGET_IWMMXT)
9506         {
9507           /* The frame pointer is guaranteed to be non-double-word aligned.
9508              This is because it is set to (old_stack_pointer - 4) and the
9509              old_stack_pointer was double word aligned.  Thus the offset to
9510              the iWMMXt registers to be loaded must also be non-double-word
9511              sized, so that the resultant address *is* double-word aligned.
9512              We can ignore floats_offset since that was already included in
9513              the live_regs_mask.  */
9514           lrm_count += (lrm_count % 2 ? 2 : 1);
9515
9516           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9517             if (regs_ever_live[reg] && !call_used_regs[reg])
9518               {
9519                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9520                              reg, FP_REGNUM, lrm_count * 4);
9521                 lrm_count += 2;
9522               }
9523         }
9524
9525       /* saved_regs_mask should contain the IP, which at the time of stack
9526          frame generation actually contains the old stack pointer.  So a
9527          quick way to unwind the stack is just pop the IP register directly
9528          into the stack pointer.  */
9529       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9530       saved_regs_mask &= ~ (1 << IP_REGNUM);
9531       saved_regs_mask |=   (1 << SP_REGNUM);
9532
9533       /* There are two registers left in saved_regs_mask - LR and PC.  We
9534          only need to restore the LR register (the return address), but to
9535          save time we can load it directly into the PC, unless we need a
9536          special function exit sequence, or we are not really returning.  */
9537       if (really_return
9538           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9539           && !current_function_calls_eh_return)
9540         /* Delete the LR from the register mask, so that the LR on
9541            the stack is loaded into the PC in the register mask.  */
9542         saved_regs_mask &= ~ (1 << LR_REGNUM);
9543       else
9544         saved_regs_mask &= ~ (1 << PC_REGNUM);
9545
9546       /* We must use SP as the base register, because SP is one of the
9547          registers being restored.  If an interrupt or page fault
9548          happens in the ldm instruction, the SP might or might not
9549          have been restored.  That would be bad, as then SP will no
9550          longer indicate the safe area of stack, and we can get stack
9551          corruption.  Using SP as the base register means that it will
9552          be reset correctly to the original value, should an interrupt
9553          occur.  If the stack pointer already points at the right
9554          place, then omit the subtraction.  */
9555       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9556           || current_function_calls_alloca)
9557         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9558                      4 * bit_count (saved_regs_mask));
9559       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9560
9561       if (IS_INTERRUPT (func_type))
9562         /* Interrupt handlers will have pushed the
9563            IP onto the stack, so restore it now.  */
9564         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9565     }
9566   else
9567     {
9568       /* Restore stack pointer if necessary.  */
9569       if (offsets->outgoing_args != offsets->saved_regs)
9570         {
9571           operands[0] = operands[1] = stack_pointer_rtx;
9572           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9573           output_add_immediate (operands);
9574         }
9575
9576       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9577         {
9578           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9579             if (regs_ever_live[reg] && !call_used_regs[reg])
9580               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9581                            reg, SP_REGNUM);
9582         }
9583       else
9584         {
9585           start_reg = FIRST_FPA_REGNUM;
9586
9587           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9588             {
9589               if (regs_ever_live[reg] && !call_used_regs[reg])
9590                 {
9591                   if (reg - start_reg == 3)
9592                     {
9593                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9594                                    start_reg, SP_REGNUM);
9595                       start_reg = reg + 1;
9596                     }
9597                 }
9598               else
9599                 {
9600                   if (reg != start_reg)
9601                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9602                                  start_reg, reg - start_reg,
9603                                  SP_REGNUM);
9604
9605                   start_reg = reg + 1;
9606                 }
9607             }
9608
9609           /* Just in case the last register checked also needs unstacking.  */
9610           if (reg != start_reg)
9611             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9612                          start_reg, reg - start_reg, SP_REGNUM);
9613         }
9614
9615       if (TARGET_HARD_FLOAT && TARGET_VFP)
9616         {
9617           start_reg = FIRST_VFP_REGNUM;
9618           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9619             {
9620               if ((!regs_ever_live[reg] || call_used_regs[reg])
9621                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9622                 {
9623                   if (start_reg != reg)
9624                     arm_output_fldmx (f, SP_REGNUM,
9625                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9626                                       (reg - start_reg) / 2);
9627                   start_reg = reg + 2;
9628                 }
9629             }
9630           if (start_reg != reg)
9631             arm_output_fldmx (f, SP_REGNUM,
9632                               (start_reg - FIRST_VFP_REGNUM) / 2,
9633                               (reg - start_reg) / 2);
9634         }
9635       if (TARGET_IWMMXT)
9636         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9637           if (regs_ever_live[reg] && !call_used_regs[reg])
9638             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9639
9640       /* If we can, restore the LR into the PC.  */
9641       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9642           && really_return
9643           && current_function_pretend_args_size == 0
9644           && saved_regs_mask & (1 << LR_REGNUM)
9645           && !current_function_calls_eh_return)
9646         {
9647           saved_regs_mask &= ~ (1 << LR_REGNUM);
9648           saved_regs_mask |=   (1 << PC_REGNUM);
9649         }
9650
9651       /* Load the registers off the stack.  If we only have one register
9652          to load use the LDR instruction - it is faster.  */
9653       if (saved_regs_mask == (1 << LR_REGNUM))
9654         {
9655           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9656         }
9657       else if (saved_regs_mask)
9658         {
9659           if (saved_regs_mask & (1 << SP_REGNUM))
9660             /* Note - write back to the stack register is not enabled
9661                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9662                in the list of registers and if we add writeback the
9663                instruction becomes UNPREDICTABLE.  */
9664             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9665           else
9666             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9667         }
9668
9669       if (current_function_pretend_args_size)
9670         {
9671           /* Unwind the pre-pushed regs.  */
9672           operands[0] = operands[1] = stack_pointer_rtx;
9673           operands[2] = GEN_INT (current_function_pretend_args_size);
9674           output_add_immediate (operands);
9675         }
9676     }
9677
9678   /* We may have already restored PC directly from the stack.  */
9679   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9680     return "";
9681
9682   /* Stack adjustment for exception handler.  */
9683   if (current_function_calls_eh_return)
9684     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9685                  ARM_EH_STACKADJ_REGNUM);
9686
9687   /* Generate the return instruction.  */
9688   switch ((int) ARM_FUNC_TYPE (func_type))
9689     {
9690     case ARM_FT_ISR:
9691     case ARM_FT_FIQ:
9692       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9693       break;
9694
9695     case ARM_FT_EXCEPTION:
9696       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9697       break;
9698
9699     case ARM_FT_INTERWORKED:
9700       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9701       break;
9702
9703     default:
9704       if (arm_arch5 || arm_arch4t)
9705         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9706       else
9707         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9708       break;
9709     }
9710
9711   return "";
9712 }
9713
9714 static void
9715 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9716                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9717 {
9718   arm_stack_offsets *offsets;
9719
9720   if (TARGET_THUMB)
9721     {
9722       int regno;
9723
9724       /* Emit any call-via-reg trampolines that are needed for v4t support
9725          of call_reg and call_value_reg type insns.  */
9726       for (regno = 0; regno < LR_REGNUM; regno++)
9727         {
9728           rtx label = cfun->machine->call_via[regno];
9729
9730           if (label != NULL)
9731             {
9732               function_section (current_function_decl);
9733               targetm.asm_out.internal_label (asm_out_file, "L",
9734                                               CODE_LABEL_NUMBER (label));
9735               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9736             }
9737         }
9738
9739       /* ??? Probably not safe to set this here, since it assumes that a
9740          function will be emitted as assembly immediately after we generate
9741          RTL for it.  This does not happen for inline functions.  */
9742       return_used_this_function = 0;
9743     }
9744   else
9745     {
9746       /* We need to take into account any stack-frame rounding.  */
9747       offsets = arm_get_frame_offsets ();
9748
9749       gcc_assert (!use_return_insn (FALSE, NULL)
9750                   || !return_used_this_function
9751                   || offsets->saved_regs == offsets->outgoing_args
9752                   || frame_pointer_needed);
9753
9754       /* Reset the ARM-specific per-function variables.  */
9755       after_arm_reorg = 0;
9756     }
9757 }
9758
9759 /* Generate and emit an insn that we will recognize as a push_multi.
9760    Unfortunately, since this insn does not reflect very well the actual
9761    semantics of the operation, we need to annotate the insn for the benefit
9762    of DWARF2 frame unwind information.  */
9763 static rtx
9764 emit_multi_reg_push (unsigned long mask)
9765 {
9766   int num_regs = 0;
9767   int num_dwarf_regs;
9768   int i, j;
9769   rtx par;
9770   rtx dwarf;
9771   int dwarf_par_index;
9772   rtx tmp, reg;
9773
9774   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9775     if (mask & (1 << i))
9776       num_regs++;
9777
9778   gcc_assert (num_regs && num_regs <= 16);
9779
9780   /* We don't record the PC in the dwarf frame information.  */
9781   num_dwarf_regs = num_regs;
9782   if (mask & (1 << PC_REGNUM))
9783     num_dwarf_regs--;
9784
9785   /* For the body of the insn we are going to generate an UNSPEC in
9786      parallel with several USEs.  This allows the insn to be recognized
9787      by the push_multi pattern in the arm.md file.  The insn looks
9788      something like this:
9789
9790        (parallel [
9791            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9792                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9793            (use (reg:SI 11 fp))
9794            (use (reg:SI 12 ip))
9795            (use (reg:SI 14 lr))
9796            (use (reg:SI 15 pc))
9797         ])
9798
9799      For the frame note however, we try to be more explicit and actually
9800      show each register being stored into the stack frame, plus a (single)
9801      decrement of the stack pointer.  We do it this way in order to be
9802      friendly to the stack unwinding code, which only wants to see a single
9803      stack decrement per instruction.  The RTL we generate for the note looks
9804      something like this:
9805
9806       (sequence [
9807            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9808            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9809            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9810            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9811            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9812         ])
9813
9814       This sequence is used both by the code to support stack unwinding for
9815       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9816
9817   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9818   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9819   dwarf_par_index = 1;
9820
9821   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9822     {
9823       if (mask & (1 << i))
9824         {
9825           reg = gen_rtx_REG (SImode, i);
9826
9827           XVECEXP (par, 0, 0)
9828             = gen_rtx_SET (VOIDmode,
9829                            gen_rtx_MEM (BLKmode,
9830                                         gen_rtx_PRE_DEC (BLKmode,
9831                                                          stack_pointer_rtx)),
9832                            gen_rtx_UNSPEC (BLKmode,
9833                                            gen_rtvec (1, reg),
9834                                            UNSPEC_PUSH_MULT));
9835
9836           if (i != PC_REGNUM)
9837             {
9838               tmp = gen_rtx_SET (VOIDmode,
9839                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9840                                  reg);
9841               RTX_FRAME_RELATED_P (tmp) = 1;
9842               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9843               dwarf_par_index++;
9844             }
9845
9846           break;
9847         }
9848     }
9849
9850   for (j = 1, i++; j < num_regs; i++)
9851     {
9852       if (mask & (1 << i))
9853         {
9854           reg = gen_rtx_REG (SImode, i);
9855
9856           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9857
9858           if (i != PC_REGNUM)
9859             {
9860               tmp = gen_rtx_SET (VOIDmode,
9861                                  gen_rtx_MEM (SImode,
9862                                               plus_constant (stack_pointer_rtx,
9863                                                              4 * j)),
9864                                  reg);
9865               RTX_FRAME_RELATED_P (tmp) = 1;
9866               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9867             }
9868
9869           j++;
9870         }
9871     }
9872
9873   par = emit_insn (par);
9874
9875   tmp = gen_rtx_SET (SImode,
9876                      stack_pointer_rtx,
9877                      gen_rtx_PLUS (SImode,
9878                                    stack_pointer_rtx,
9879                                    GEN_INT (-4 * num_regs)));
9880   RTX_FRAME_RELATED_P (tmp) = 1;
9881   XVECEXP (dwarf, 0, 0) = tmp;
9882
9883   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9884                                        REG_NOTES (par));
9885   return par;
9886 }
9887
9888 /* Calculate the size of the return value that is passed in registers.  */
9889 static int
9890 arm_size_return_regs (void)
9891 {
9892   enum machine_mode mode;
9893
9894   if (current_function_return_rtx != 0)
9895     mode = GET_MODE (current_function_return_rtx);
9896   else
9897     mode = DECL_MODE (DECL_RESULT (current_function_decl));
9898
9899   return GET_MODE_SIZE (mode);
9900 }
9901
9902 static rtx
9903 emit_sfm (int base_reg, int count)
9904 {
9905   rtx par;
9906   rtx dwarf;
9907   rtx tmp, reg;
9908   int i;
9909
9910   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9911   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9912
9913   reg = gen_rtx_REG (XFmode, base_reg++);
9914
9915   XVECEXP (par, 0, 0)
9916     = gen_rtx_SET (VOIDmode,
9917                    gen_rtx_MEM (BLKmode,
9918                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9919                    gen_rtx_UNSPEC (BLKmode,
9920                                    gen_rtvec (1, reg),
9921                                    UNSPEC_PUSH_MULT));
9922   tmp = gen_rtx_SET (VOIDmode,
9923                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9924   RTX_FRAME_RELATED_P (tmp) = 1;
9925   XVECEXP (dwarf, 0, 1) = tmp;
9926
9927   for (i = 1; i < count; i++)
9928     {
9929       reg = gen_rtx_REG (XFmode, base_reg++);
9930       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9931
9932       tmp = gen_rtx_SET (VOIDmode,
9933                          gen_rtx_MEM (XFmode,
9934                                       plus_constant (stack_pointer_rtx,
9935                                                      i * 12)),
9936                          reg);
9937       RTX_FRAME_RELATED_P (tmp) = 1;
9938       XVECEXP (dwarf, 0, i + 1) = tmp;
9939     }
9940
9941   tmp = gen_rtx_SET (VOIDmode,
9942                      stack_pointer_rtx,
9943                      gen_rtx_PLUS (SImode,
9944                                    stack_pointer_rtx,
9945                                    GEN_INT (-12 * count)));
9946   RTX_FRAME_RELATED_P (tmp) = 1;
9947   XVECEXP (dwarf, 0, 0) = tmp;
9948
9949   par = emit_insn (par);
9950   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9951                                        REG_NOTES (par));
9952   return par;
9953 }
9954
9955
9956 /* Return true if the current function needs to save/restore LR.  */
9957
9958 static bool
9959 thumb_force_lr_save (void)
9960 {
9961   return !cfun->machine->lr_save_eliminated
9962          && (!leaf_function_p ()
9963              || thumb_far_jump_used_p ()
9964              || regs_ever_live [LR_REGNUM]);
9965 }
9966
9967
9968 /* Compute the distance from register FROM to register TO.
9969    These can be the arg pointer (26), the soft frame pointer (25),
9970    the stack pointer (13) or the hard frame pointer (11).
9971    In thumb mode r7 is used as the soft frame pointer, if needed.
9972    Typical stack layout looks like this:
9973
9974        old stack pointer -> |    |
9975                              ----
9976                             |    | \
9977                             |    |   saved arguments for
9978                             |    |   vararg functions
9979                             |    | /
9980                               --
9981    hard FP & arg pointer -> |    | \
9982                             |    |   stack
9983                             |    |   frame
9984                             |    | /
9985                               --
9986                             |    | \
9987                             |    |   call saved
9988                             |    |   registers
9989       soft frame pointer -> |    | /
9990                               --
9991                             |    | \
9992                             |    |   local
9993                             |    |   variables
9994                             |    | /
9995                               --
9996                             |    | \
9997                             |    |   outgoing
9998                             |    |   arguments
9999    current stack pointer -> |    | /
10000                               --
10001
10002   For a given function some or all of these stack components
10003   may not be needed, giving rise to the possibility of
10004   eliminating some of the registers.
10005
10006   The values returned by this function must reflect the behavior
10007   of arm_expand_prologue() and arm_compute_save_reg_mask().
10008
10009   The sign of the number returned reflects the direction of stack
10010   growth, so the values are positive for all eliminations except
10011   from the soft frame pointer to the hard frame pointer.
10012
10013   SFP may point just inside the local variables block to ensure correct
10014   alignment.  */
10015
10016
10017 /* Calculate stack offsets.  These are used to calculate register elimination
10018    offsets and in prologue/epilogue code.  */
10019
10020 static arm_stack_offsets *
10021 arm_get_frame_offsets (void)
10022 {
10023   struct arm_stack_offsets *offsets;
10024   unsigned long func_type;
10025   int leaf;
10026   int saved;
10027   HOST_WIDE_INT frame_size;
10028
10029   offsets = &cfun->machine->stack_offsets;
10030
10031   /* We need to know if we are a leaf function.  Unfortunately, it
10032      is possible to be called after start_sequence has been called,
10033      which causes get_insns to return the insns for the sequence,
10034      not the function, which will cause leaf_function_p to return
10035      the incorrect result.
10036
10037      to know about leaf functions once reload has completed, and the
10038      frame size cannot be changed after that time, so we can safely
10039      use the cached value.  */
10040
10041   if (reload_completed)
10042     return offsets;
10043
10044   /* Initially this is the size of the local variables.  It will translated
10045      into an offset once we have determined the size of preceding data.  */
10046   frame_size = ROUND_UP_WORD (get_frame_size ());
10047
10048   leaf = leaf_function_p ();
10049
10050   /* Space for variadic functions.  */
10051   offsets->saved_args = current_function_pretend_args_size;
10052
10053   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10054
10055   if (TARGET_ARM)
10056     {
10057       unsigned int regno;
10058
10059       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10060
10061       /* We know that SP will be doubleword aligned on entry, and we must
10062          preserve that condition at any subroutine call.  We also require the
10063          soft frame pointer to be doubleword aligned.  */
10064
10065       if (TARGET_REALLY_IWMMXT)
10066         {
10067           /* Check for the call-saved iWMMXt registers.  */
10068           for (regno = FIRST_IWMMXT_REGNUM;
10069                regno <= LAST_IWMMXT_REGNUM;
10070                regno++)
10071             if (regs_ever_live [regno] && ! call_used_regs [regno])
10072               saved += 8;
10073         }
10074
10075       func_type = arm_current_func_type ();
10076       if (! IS_VOLATILE (func_type))
10077         {
10078           /* Space for saved FPA registers.  */
10079           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10080           if (regs_ever_live[regno] && ! call_used_regs[regno])
10081             saved += 12;
10082
10083           /* Space for saved VFP registers.  */
10084           if (TARGET_HARD_FLOAT && TARGET_VFP)
10085             saved += arm_get_vfp_saved_size ();
10086         }
10087     }
10088   else /* TARGET_THUMB */
10089     {
10090       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10091       if (TARGET_BACKTRACE)
10092         saved += 16;
10093     }
10094
10095   /* Saved registers include the stack frame.  */
10096   offsets->saved_regs = offsets->saved_args + saved;
10097   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10098   /* A leaf function does not need any stack alignment if it has nothing
10099      on the stack.  */
10100   if (leaf && frame_size == 0)
10101     {
10102       offsets->outgoing_args = offsets->soft_frame;
10103       return offsets;
10104     }
10105
10106   /* Ensure SFP has the correct alignment.  */
10107   if (ARM_DOUBLEWORD_ALIGN
10108       && (offsets->soft_frame & 7))
10109     offsets->soft_frame += 4;
10110
10111   offsets->outgoing_args = offsets->soft_frame + frame_size
10112                            + current_function_outgoing_args_size;
10113
10114   if (ARM_DOUBLEWORD_ALIGN)
10115     {
10116       /* Ensure SP remains doubleword aligned.  */
10117       if (offsets->outgoing_args & 7)
10118         offsets->outgoing_args += 4;
10119       gcc_assert (!(offsets->outgoing_args & 7));
10120     }
10121
10122   return offsets;
10123 }
10124
10125
10126 /* Calculate the relative offsets for the different stack pointers.  Positive
10127    offsets are in the direction of stack growth.  */
10128
10129 HOST_WIDE_INT
10130 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10131 {
10132   arm_stack_offsets *offsets;
10133
10134   offsets = arm_get_frame_offsets ();
10135
10136   /* OK, now we have enough information to compute the distances.
10137      There must be an entry in these switch tables for each pair
10138      of registers in ELIMINABLE_REGS, even if some of the entries
10139      seem to be redundant or useless.  */
10140   switch (from)
10141     {
10142     case ARG_POINTER_REGNUM:
10143       switch (to)
10144         {
10145         case THUMB_HARD_FRAME_POINTER_REGNUM:
10146           return 0;
10147
10148         case FRAME_POINTER_REGNUM:
10149           /* This is the reverse of the soft frame pointer
10150              to hard frame pointer elimination below.  */
10151           return offsets->soft_frame - offsets->saved_args;
10152
10153         case ARM_HARD_FRAME_POINTER_REGNUM:
10154           /* If there is no stack frame then the hard
10155              frame pointer and the arg pointer coincide.  */
10156           if (offsets->frame == offsets->saved_regs)
10157             return 0;
10158           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10159           return (frame_pointer_needed
10160                   && cfun->static_chain_decl != NULL
10161                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10162
10163         case STACK_POINTER_REGNUM:
10164           /* If nothing has been pushed on the stack at all
10165              then this will return -4.  This *is* correct!  */
10166           return offsets->outgoing_args - (offsets->saved_args + 4);
10167
10168         default:
10169           gcc_unreachable ();
10170         }
10171       gcc_unreachable ();
10172
10173     case FRAME_POINTER_REGNUM:
10174       switch (to)
10175         {
10176         case THUMB_HARD_FRAME_POINTER_REGNUM:
10177           return 0;
10178
10179         case ARM_HARD_FRAME_POINTER_REGNUM:
10180           /* The hard frame pointer points to the top entry in the
10181              stack frame.  The soft frame pointer to the bottom entry
10182              in the stack frame.  If there is no stack frame at all,
10183              then they are identical.  */
10184
10185           return offsets->frame - offsets->soft_frame;
10186
10187         case STACK_POINTER_REGNUM:
10188           return offsets->outgoing_args - offsets->soft_frame;
10189
10190         default:
10191           gcc_unreachable ();
10192         }
10193       gcc_unreachable ();
10194
10195     default:
10196       /* You cannot eliminate from the stack pointer.
10197          In theory you could eliminate from the hard frame
10198          pointer to the stack pointer, but this will never
10199          happen, since if a stack frame is not needed the
10200          hard frame pointer will never be used.  */
10201       gcc_unreachable ();
10202     }
10203 }
10204
10205
10206 /* Generate the prologue instructions for entry into an ARM function.  */
10207 void
10208 arm_expand_prologue (void)
10209 {
10210   int reg;
10211   rtx amount;
10212   rtx insn;
10213   rtx ip_rtx;
10214   unsigned long live_regs_mask;
10215   unsigned long func_type;
10216   int fp_offset = 0;
10217   int saved_pretend_args = 0;
10218   int saved_regs = 0;
10219   unsigned HOST_WIDE_INT args_to_push;
10220   arm_stack_offsets *offsets;
10221
10222   func_type = arm_current_func_type ();
10223
10224   /* Naked functions don't have prologues.  */
10225   if (IS_NAKED (func_type))
10226     return;
10227
10228   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10229   args_to_push = current_function_pretend_args_size;
10230
10231   /* Compute which register we will have to save onto the stack.  */
10232   live_regs_mask = arm_compute_save_reg_mask ();
10233
10234   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10235
10236   if (frame_pointer_needed)
10237     {
10238       if (IS_INTERRUPT (func_type))
10239         {
10240           /* Interrupt functions must not corrupt any registers.
10241              Creating a frame pointer however, corrupts the IP
10242              register, so we must push it first.  */
10243           insn = emit_multi_reg_push (1 << IP_REGNUM);
10244
10245           /* Do not set RTX_FRAME_RELATED_P on this insn.
10246              The dwarf stack unwinding code only wants to see one
10247              stack decrement per function, and this is not it.  If
10248              this instruction is labeled as being part of the frame
10249              creation sequence then dwarf2out_frame_debug_expr will
10250              die when it encounters the assignment of IP to FP
10251              later on, since the use of SP here establishes SP as
10252              the CFA register and not IP.
10253
10254              Anyway this instruction is not really part of the stack
10255              frame creation although it is part of the prologue.  */
10256         }
10257       else if (IS_NESTED (func_type))
10258         {
10259           /* The Static chain register is the same as the IP register
10260              used as a scratch register during stack frame creation.
10261              To get around this need to find somewhere to store IP
10262              whilst the frame is being created.  We try the following
10263              places in order:
10264
10265                1. The last argument register.
10266                2. A slot on the stack above the frame.  (This only
10267                   works if the function is not a varargs function).
10268                3. Register r3, after pushing the argument registers
10269                   onto the stack.
10270
10271              Note - we only need to tell the dwarf2 backend about the SP
10272              adjustment in the second variant; the static chain register
10273              doesn't need to be unwound, as it doesn't contain a value
10274              inherited from the caller.  */
10275
10276           if (regs_ever_live[3] == 0)
10277             {
10278               insn = gen_rtx_REG (SImode, 3);
10279               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10280               insn = emit_insn (insn);
10281             }
10282           else if (args_to_push == 0)
10283             {
10284               rtx dwarf;
10285               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10286               insn = gen_rtx_MEM (SImode, insn);
10287               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10288               insn = emit_insn (insn);
10289
10290               fp_offset = 4;
10291
10292               /* Just tell the dwarf backend that we adjusted SP.  */
10293               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10294                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10295                                                  GEN_INT (-fp_offset)));
10296               RTX_FRAME_RELATED_P (insn) = 1;
10297               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10298                                                     dwarf, REG_NOTES (insn));
10299             }
10300           else
10301             {
10302               /* Store the args on the stack.  */
10303               if (cfun->machine->uses_anonymous_args)
10304                 insn = emit_multi_reg_push
10305                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10306               else
10307                 insn = emit_insn
10308                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10309                                GEN_INT (- args_to_push)));
10310
10311               RTX_FRAME_RELATED_P (insn) = 1;
10312
10313               saved_pretend_args = 1;
10314               fp_offset = args_to_push;
10315               args_to_push = 0;
10316
10317               /* Now reuse r3 to preserve IP.  */
10318               insn = gen_rtx_REG (SImode, 3);
10319               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10320               (void) emit_insn (insn);
10321             }
10322         }
10323
10324       if (fp_offset)
10325         {
10326           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10327           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10328         }
10329       else
10330         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10331
10332       insn = emit_insn (insn);
10333       RTX_FRAME_RELATED_P (insn) = 1;
10334     }
10335
10336   if (args_to_push)
10337     {
10338       /* Push the argument registers, or reserve space for them.  */
10339       if (cfun->machine->uses_anonymous_args)
10340         insn = emit_multi_reg_push
10341           ((0xf0 >> (args_to_push / 4)) & 0xf);
10342       else
10343         insn = emit_insn
10344           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10345                        GEN_INT (- args_to_push)));
10346       RTX_FRAME_RELATED_P (insn) = 1;
10347     }
10348
10349   /* If this is an interrupt service routine, and the link register
10350      is going to be pushed, and we are not creating a stack frame,
10351      (which would involve an extra push of IP and a pop in the epilogue)
10352      subtracting four from LR now will mean that the function return
10353      can be done with a single instruction.  */
10354   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10355       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10356       && ! frame_pointer_needed)
10357     emit_insn (gen_rtx_SET (SImode,
10358                             gen_rtx_REG (SImode, LR_REGNUM),
10359                             gen_rtx_PLUS (SImode,
10360                                           gen_rtx_REG (SImode, LR_REGNUM),
10361                                           GEN_INT (-4))));
10362
10363   if (live_regs_mask)
10364     {
10365       insn = emit_multi_reg_push (live_regs_mask);
10366       saved_regs += bit_count (live_regs_mask) * 4;
10367       RTX_FRAME_RELATED_P (insn) = 1;
10368     }
10369
10370   if (TARGET_IWMMXT)
10371     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10372       if (regs_ever_live[reg] && ! call_used_regs [reg])
10373         {
10374           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10375           insn = gen_rtx_MEM (V2SImode, insn);
10376           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10377                                          gen_rtx_REG (V2SImode, reg)));
10378           RTX_FRAME_RELATED_P (insn) = 1;
10379           saved_regs += 8;
10380         }
10381
10382   if (! IS_VOLATILE (func_type))
10383     {
10384       int start_reg;
10385
10386       /* Save any floating point call-saved registers used by this
10387          function.  */
10388       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10389         {
10390           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10391             if (regs_ever_live[reg] && !call_used_regs[reg])
10392               {
10393                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10394                 insn = gen_rtx_MEM (XFmode, insn);
10395                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10396                                                gen_rtx_REG (XFmode, reg)));
10397                 RTX_FRAME_RELATED_P (insn) = 1;
10398                 saved_regs += 12;
10399               }
10400         }
10401       else
10402         {
10403           start_reg = LAST_FPA_REGNUM;
10404
10405           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10406             {
10407               if (regs_ever_live[reg] && !call_used_regs[reg])
10408                 {
10409                   if (start_reg - reg == 3)
10410                     {
10411                       insn = emit_sfm (reg, 4);
10412                       RTX_FRAME_RELATED_P (insn) = 1;
10413                       saved_regs += 48;
10414                       start_reg = reg - 1;
10415                     }
10416                 }
10417               else
10418                 {
10419                   if (start_reg != reg)
10420                     {
10421                       insn = emit_sfm (reg + 1, start_reg - reg);
10422                       RTX_FRAME_RELATED_P (insn) = 1;
10423                       saved_regs += (start_reg - reg) * 12;
10424                     }
10425                   start_reg = reg - 1;
10426                 }
10427             }
10428
10429           if (start_reg != reg)
10430             {
10431               insn = emit_sfm (reg + 1, start_reg - reg);
10432               saved_regs += (start_reg - reg) * 12;
10433               RTX_FRAME_RELATED_P (insn) = 1;
10434             }
10435         }
10436       if (TARGET_HARD_FLOAT && TARGET_VFP)
10437         {
10438           start_reg = FIRST_VFP_REGNUM;
10439
10440           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10441             {
10442               if ((!regs_ever_live[reg] || call_used_regs[reg])
10443                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10444                 {
10445                   if (start_reg != reg)
10446                     saved_regs += vfp_emit_fstmx (start_reg,
10447                                                   (reg - start_reg) / 2);
10448                   start_reg = reg + 2;
10449                 }
10450             }
10451           if (start_reg != reg)
10452             saved_regs += vfp_emit_fstmx (start_reg,
10453                                           (reg - start_reg) / 2);
10454         }
10455     }
10456
10457   if (frame_pointer_needed)
10458     {
10459       /* Create the new frame pointer.  */
10460       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10461       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10462       RTX_FRAME_RELATED_P (insn) = 1;
10463
10464       if (IS_NESTED (func_type))
10465         {
10466           /* Recover the static chain register.  */
10467           if (regs_ever_live [3] == 0
10468               || saved_pretend_args)
10469             insn = gen_rtx_REG (SImode, 3);
10470           else /* if (current_function_pretend_args_size == 0) */
10471             {
10472               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10473                                    GEN_INT (4));
10474               insn = gen_rtx_MEM (SImode, insn);
10475             }
10476
10477           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10478           /* Add a USE to stop propagate_one_insn() from barfing.  */
10479           emit_insn (gen_prologue_use (ip_rtx));
10480         }
10481     }
10482
10483   offsets = arm_get_frame_offsets ();
10484   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10485     {
10486       /* This add can produce multiple insns for a large constant, so we
10487          need to get tricky.  */
10488       rtx last = get_last_insn ();
10489
10490       amount = GEN_INT (offsets->saved_args + saved_regs
10491                         - offsets->outgoing_args);
10492
10493       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10494                                     amount));
10495       do
10496         {
10497           last = last ? NEXT_INSN (last) : get_insns ();
10498           RTX_FRAME_RELATED_P (last) = 1;
10499         }
10500       while (last != insn);
10501
10502       /* If the frame pointer is needed, emit a special barrier that
10503          will prevent the scheduler from moving stores to the frame
10504          before the stack adjustment.  */
10505       if (frame_pointer_needed)
10506         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10507                                          hard_frame_pointer_rtx));
10508     }
10509
10510
10511   if (flag_pic)
10512     arm_load_pic_register (INVALID_REGNUM);
10513
10514   /* If we are profiling, make sure no instructions are scheduled before
10515      the call to mcount.  Similarly if the user has requested no
10516      scheduling in the prolog.  */
10517   if (current_function_profile || !TARGET_SCHED_PROLOG)
10518     emit_insn (gen_blockage ());
10519
10520   /* If the link register is being kept alive, with the return address in it,
10521      then make sure that it does not get reused by the ce2 pass.  */
10522   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10523     {
10524       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10525       cfun->machine->lr_save_eliminated = 1;
10526     }
10527 }
10528 \f
10529 /* If CODE is 'd', then the X is a condition operand and the instruction
10530    should only be executed if the condition is true.
10531    if CODE is 'D', then the X is a condition operand and the instruction
10532    should only be executed if the condition is false: however, if the mode
10533    of the comparison is CCFPEmode, then always execute the instruction -- we
10534    do this because in these circumstances !GE does not necessarily imply LT;
10535    in these cases the instruction pattern will take care to make sure that
10536    an instruction containing %d will follow, thereby undoing the effects of
10537    doing this instruction unconditionally.
10538    If CODE is 'N' then X is a floating point operand that must be negated
10539    before output.
10540    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10541    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10542 void
10543 arm_print_operand (FILE *stream, rtx x, int code)
10544 {
10545   switch (code)
10546     {
10547     case '@':
10548       fputs (ASM_COMMENT_START, stream);
10549       return;
10550
10551     case '_':
10552       fputs (user_label_prefix, stream);
10553       return;
10554
10555     case '|':
10556       fputs (REGISTER_PREFIX, stream);
10557       return;
10558
10559     case '?':
10560       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10561         {
10562           if (TARGET_THUMB)
10563             {
10564               output_operand_lossage ("predicated Thumb instruction");
10565               break;
10566             }
10567           if (current_insn_predicate != NULL)
10568             {
10569               output_operand_lossage
10570                 ("predicated instruction in conditional sequence");
10571               break;
10572             }
10573
10574           fputs (arm_condition_codes[arm_current_cc], stream);
10575         }
10576       else if (current_insn_predicate)
10577         {
10578           enum arm_cond_code code;
10579
10580           if (TARGET_THUMB)
10581             {
10582               output_operand_lossage ("predicated Thumb instruction");
10583               break;
10584             }
10585
10586           code = get_arm_condition_code (current_insn_predicate);
10587           fputs (arm_condition_codes[code], stream);
10588         }
10589       return;
10590
10591     case 'N':
10592       {
10593         REAL_VALUE_TYPE r;
10594         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10595         r = REAL_VALUE_NEGATE (r);
10596         fprintf (stream, "%s", fp_const_from_val (&r));
10597       }
10598       return;
10599
10600     case 'B':
10601       if (GET_CODE (x) == CONST_INT)
10602         {
10603           HOST_WIDE_INT val;
10604           val = ARM_SIGN_EXTEND (~INTVAL (x));
10605           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10606         }
10607       else
10608         {
10609           putc ('~', stream);
10610           output_addr_const (stream, x);
10611         }
10612       return;
10613
10614     case 'i':
10615       fprintf (stream, "%s", arithmetic_instr (x, 1));
10616       return;
10617
10618     /* Truncate Cirrus shift counts.  */
10619     case 's':
10620       if (GET_CODE (x) == CONST_INT)
10621         {
10622           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10623           return;
10624         }
10625       arm_print_operand (stream, x, 0);
10626       return;
10627
10628     case 'I':
10629       fprintf (stream, "%s", arithmetic_instr (x, 0));
10630       return;
10631
10632     case 'S':
10633       {
10634         HOST_WIDE_INT val;
10635         const char * shift = shift_op (x, &val);
10636
10637         if (shift)
10638           {
10639             fprintf (stream, ", %s ", shift_op (x, &val));
10640             if (val == -1)
10641               arm_print_operand (stream, XEXP (x, 1), 0);
10642             else
10643               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10644           }
10645       }
10646       return;
10647
10648       /* An explanation of the 'Q', 'R' and 'H' register operands:
10649
10650          In a pair of registers containing a DI or DF value the 'Q'
10651          operand returns the register number of the register containing
10652          the least significant part of the value.  The 'R' operand returns
10653          the register number of the register containing the most
10654          significant part of the value.
10655
10656          The 'H' operand returns the higher of the two register numbers.
10657          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10658          same as the 'Q' operand, since the most significant part of the
10659          value is held in the lower number register.  The reverse is true
10660          on systems where WORDS_BIG_ENDIAN is false.
10661
10662          The purpose of these operands is to distinguish between cases
10663          where the endian-ness of the values is important (for example
10664          when they are added together), and cases where the endian-ness
10665          is irrelevant, but the order of register operations is important.
10666          For example when loading a value from memory into a register
10667          pair, the endian-ness does not matter.  Provided that the value
10668          from the lower memory address is put into the lower numbered
10669          register, and the value from the higher address is put into the
10670          higher numbered register, the load will work regardless of whether
10671          the value being loaded is big-wordian or little-wordian.  The
10672          order of the two register loads can matter however, if the address
10673          of the memory location is actually held in one of the registers
10674          being overwritten by the load.  */
10675     case 'Q':
10676       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10677         {
10678           output_operand_lossage ("invalid operand for code '%c'", code);
10679           return;
10680         }
10681
10682       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10683       return;
10684
10685     case 'R':
10686       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10687         {
10688           output_operand_lossage ("invalid operand for code '%c'", code);
10689           return;
10690         }
10691
10692       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10693       return;
10694
10695     case 'H':
10696       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10697         {
10698           output_operand_lossage ("invalid operand for code '%c'", code);
10699           return;
10700         }
10701
10702       asm_fprintf (stream, "%r", REGNO (x) + 1);
10703       return;
10704
10705     case 'm':
10706       asm_fprintf (stream, "%r",
10707                    GET_CODE (XEXP (x, 0)) == REG
10708                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10709       return;
10710
10711     case 'M':
10712       asm_fprintf (stream, "{%r-%r}",
10713                    REGNO (x),
10714                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10715       return;
10716
10717     case 'd':
10718       /* CONST_TRUE_RTX means always -- that's the default.  */
10719       if (x == const_true_rtx)
10720         return;
10721
10722       if (!COMPARISON_P (x))
10723         {
10724           output_operand_lossage ("invalid operand for code '%c'", code);
10725           return;
10726         }
10727
10728       fputs (arm_condition_codes[get_arm_condition_code (x)],
10729              stream);
10730       return;
10731
10732     case 'D':
10733       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
10734          want to do that.  */
10735       if (x == const_true_rtx)
10736         {
10737           output_operand_lossage ("instruction never exectued");
10738           return;
10739         }
10740       if (!COMPARISON_P (x))
10741         {
10742           output_operand_lossage ("invalid operand for code '%c'", code);
10743           return;
10744         }
10745
10746       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10747                                  (get_arm_condition_code (x))],
10748              stream);
10749       return;
10750
10751     /* Cirrus registers can be accessed in a variety of ways:
10752          single floating point (f)
10753          double floating point (d)
10754          32bit integer         (fx)
10755          64bit integer         (dx).  */
10756     case 'W':                   /* Cirrus register in F mode.  */
10757     case 'X':                   /* Cirrus register in D mode.  */
10758     case 'Y':                   /* Cirrus register in FX mode.  */
10759     case 'Z':                   /* Cirrus register in DX mode.  */
10760       gcc_assert (GET_CODE (x) == REG
10761                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
10762
10763       fprintf (stream, "mv%s%s",
10764                code == 'W' ? "f"
10765                : code == 'X' ? "d"
10766                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10767
10768       return;
10769
10770     /* Print cirrus register in the mode specified by the register's mode.  */
10771     case 'V':
10772       {
10773         int mode = GET_MODE (x);
10774
10775         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10776           {
10777             output_operand_lossage ("invalid operand for code '%c'", code);
10778             return;
10779           }
10780
10781         fprintf (stream, "mv%s%s",
10782                  mode == DFmode ? "d"
10783                  : mode == SImode ? "fx"
10784                  : mode == DImode ? "dx"
10785                  : "f", reg_names[REGNO (x)] + 2);
10786
10787         return;
10788       }
10789
10790     case 'U':
10791       if (GET_CODE (x) != REG
10792           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10793           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10794         /* Bad value for wCG register number.  */
10795         {
10796           output_operand_lossage ("invalid operand for code '%c'", code);
10797           return;
10798         }
10799
10800       else
10801         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10802       return;
10803
10804       /* Print an iWMMXt control register name.  */
10805     case 'w':
10806       if (GET_CODE (x) != CONST_INT
10807           || INTVAL (x) < 0
10808           || INTVAL (x) >= 16)
10809         /* Bad value for wC register number.  */
10810         {
10811           output_operand_lossage ("invalid operand for code '%c'", code);
10812           return;
10813         }
10814
10815       else
10816         {
10817           static const char * wc_reg_names [16] =
10818             {
10819               "wCID",  "wCon",  "wCSSF", "wCASF",
10820               "wC4",   "wC5",   "wC6",   "wC7",
10821               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10822               "wC12",  "wC13",  "wC14",  "wC15"
10823             };
10824
10825           fprintf (stream, wc_reg_names [INTVAL (x)]);
10826         }
10827       return;
10828
10829       /* Print a VFP double precision register name.  */
10830     case 'P':
10831       {
10832         int mode = GET_MODE (x);
10833         int num;
10834
10835         if (mode != DImode && mode != DFmode)
10836           {
10837             output_operand_lossage ("invalid operand for code '%c'", code);
10838             return;
10839           }
10840
10841         if (GET_CODE (x) != REG
10842             || !IS_VFP_REGNUM (REGNO (x)))
10843           {
10844             output_operand_lossage ("invalid operand for code '%c'", code);
10845             return;
10846           }
10847
10848         num = REGNO(x) - FIRST_VFP_REGNUM;
10849         if (num & 1)
10850           {
10851             output_operand_lossage ("invalid operand for code '%c'", code);
10852             return;
10853           }
10854
10855         fprintf (stream, "d%d", num >> 1);
10856       }
10857       return;
10858
10859     default:
10860       if (x == 0)
10861         {
10862           output_operand_lossage ("missing operand");
10863           return;
10864         }
10865
10866       switch (GET_CODE (x))
10867         {
10868         case REG:
10869           asm_fprintf (stream, "%r", REGNO (x));
10870           break;
10871
10872         case MEM:
10873           output_memory_reference_mode = GET_MODE (x);
10874           output_address (XEXP (x, 0));
10875           break;
10876
10877         case CONST_DOUBLE:
10878           fprintf (stream, "#%s", fp_immediate_constant (x));
10879           break;
10880
10881         default:
10882           gcc_assert (GET_CODE (x) != NEG);
10883           fputc ('#', stream);
10884           output_addr_const (stream, x);
10885           break;
10886         }
10887     }
10888 }
10889 \f
10890 #ifndef AOF_ASSEMBLER
10891 /* Target hook for assembling integer objects.  The ARM version needs to
10892    handle word-sized values specially.  */
10893 static bool
10894 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10895 {
10896   if (size == UNITS_PER_WORD && aligned_p)
10897     {
10898       fputs ("\t.word\t", asm_out_file);
10899       output_addr_const (asm_out_file, x);
10900
10901       /* Mark symbols as position independent.  We only do this in the
10902          .text segment, not in the .data segment.  */
10903       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10904           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10905         {
10906           if (GET_CODE (x) == SYMBOL_REF
10907               && (CONSTANT_POOL_ADDRESS_P (x)
10908                   || SYMBOL_REF_LOCAL_P (x)))
10909             fputs ("(GOTOFF)", asm_out_file);
10910           else if (GET_CODE (x) == LABEL_REF)
10911             fputs ("(GOTOFF)", asm_out_file);
10912           else
10913             fputs ("(GOT)", asm_out_file);
10914         }
10915       fputc ('\n', asm_out_file);
10916       return true;
10917     }
10918
10919   if (arm_vector_mode_supported_p (GET_MODE (x)))
10920     {
10921       int i, units;
10922
10923       gcc_assert (GET_CODE (x) == CONST_VECTOR);
10924
10925       units = CONST_VECTOR_NUNITS (x);
10926
10927       switch (GET_MODE (x))
10928         {
10929         case V2SImode: size = 4; break;
10930         case V4HImode: size = 2; break;
10931         case V8QImode: size = 1; break;
10932         default:
10933           gcc_unreachable ();
10934         }
10935
10936       for (i = 0; i < units; i++)
10937         {
10938           rtx elt;
10939
10940           elt = CONST_VECTOR_ELT (x, i);
10941           assemble_integer
10942             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10943         }
10944
10945       return true;
10946     }
10947
10948   return default_assemble_integer (x, size, aligned_p);
10949 }
10950
10951
10952 /* Add a function to the list of static constructors.  */
10953
10954 static void
10955 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
10956 {
10957   if (!TARGET_AAPCS_BASED)
10958     {
10959       default_named_section_asm_out_constructor (symbol, priority);
10960       return;
10961     }
10962
10963   /* Put these in the .init_array section, using a special relocation.  */
10964   ctors_section ();
10965   assemble_align (POINTER_SIZE);
10966   fputs ("\t.word\t", asm_out_file);
10967   output_addr_const (asm_out_file, symbol);
10968   fputs ("(target1)\n", asm_out_file);
10969 }
10970 #endif
10971 \f
10972 /* A finite state machine takes care of noticing whether or not instructions
10973    can be conditionally executed, and thus decrease execution time and code
10974    size by deleting branch instructions.  The fsm is controlled by
10975    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10976
10977 /* The state of the fsm controlling condition codes are:
10978    0: normal, do nothing special
10979    1: make ASM_OUTPUT_OPCODE not output this instruction
10980    2: make ASM_OUTPUT_OPCODE not output this instruction
10981    3: make instructions conditional
10982    4: make instructions conditional
10983
10984    State transitions (state->state by whom under condition):
10985    0 -> 1 final_prescan_insn if the `target' is a label
10986    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10987    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10988    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10989    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10990           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10991    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10992           (the target insn is arm_target_insn).
10993
10994    If the jump clobbers the conditions then we use states 2 and 4.
10995
10996    A similar thing can be done with conditional return insns.
10997
10998    XXX In case the `target' is an unconditional branch, this conditionalising
10999    of the instructions always reduces code size, but not always execution
11000    time.  But then, I want to reduce the code size to somewhere near what
11001    /bin/cc produces.  */
11002
11003 /* Returns the index of the ARM condition code string in
11004    `arm_condition_codes'.  COMPARISON should be an rtx like
11005    `(eq (...) (...))'.  */
11006 static enum arm_cond_code
11007 get_arm_condition_code (rtx comparison)
11008 {
11009   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11010   int code;
11011   enum rtx_code comp_code = GET_CODE (comparison);
11012
11013   if (GET_MODE_CLASS (mode) != MODE_CC)
11014     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11015                            XEXP (comparison, 1));
11016
11017   switch (mode)
11018     {
11019     case CC_DNEmode: code = ARM_NE; goto dominance;
11020     case CC_DEQmode: code = ARM_EQ; goto dominance;
11021     case CC_DGEmode: code = ARM_GE; goto dominance;
11022     case CC_DGTmode: code = ARM_GT; goto dominance;
11023     case CC_DLEmode: code = ARM_LE; goto dominance;
11024     case CC_DLTmode: code = ARM_LT; goto dominance;
11025     case CC_DGEUmode: code = ARM_CS; goto dominance;
11026     case CC_DGTUmode: code = ARM_HI; goto dominance;
11027     case CC_DLEUmode: code = ARM_LS; goto dominance;
11028     case CC_DLTUmode: code = ARM_CC;
11029
11030     dominance:
11031       gcc_assert (comp_code == EQ || comp_code == NE);
11032
11033       if (comp_code == EQ)
11034         return ARM_INVERSE_CONDITION_CODE (code);
11035       return code;
11036
11037     case CC_NOOVmode:
11038       switch (comp_code)
11039         {
11040         case NE: return ARM_NE;
11041         case EQ: return ARM_EQ;
11042         case GE: return ARM_PL;
11043         case LT: return ARM_MI;
11044         default: gcc_unreachable ();
11045         }
11046
11047     case CC_Zmode:
11048       switch (comp_code)
11049         {
11050         case NE: return ARM_NE;
11051         case EQ: return ARM_EQ;
11052         default: gcc_unreachable ();
11053         }
11054
11055     case CC_Nmode:
11056       switch (comp_code)
11057         {
11058         case NE: return ARM_MI;
11059         case EQ: return ARM_PL;
11060         default: gcc_unreachable ();
11061         }
11062
11063     case CCFPEmode:
11064     case CCFPmode:
11065       /* These encodings assume that AC=1 in the FPA system control
11066          byte.  This allows us to handle all cases except UNEQ and
11067          LTGT.  */
11068       switch (comp_code)
11069         {
11070         case GE: return ARM_GE;
11071         case GT: return ARM_GT;
11072         case LE: return ARM_LS;
11073         case LT: return ARM_MI;
11074         case NE: return ARM_NE;
11075         case EQ: return ARM_EQ;
11076         case ORDERED: return ARM_VC;
11077         case UNORDERED: return ARM_VS;
11078         case UNLT: return ARM_LT;
11079         case UNLE: return ARM_LE;
11080         case UNGT: return ARM_HI;
11081         case UNGE: return ARM_PL;
11082           /* UNEQ and LTGT do not have a representation.  */
11083         case UNEQ: /* Fall through.  */
11084         case LTGT: /* Fall through.  */
11085         default: gcc_unreachable ();
11086         }
11087
11088     case CC_SWPmode:
11089       switch (comp_code)
11090         {
11091         case NE: return ARM_NE;
11092         case EQ: return ARM_EQ;
11093         case GE: return ARM_LE;
11094         case GT: return ARM_LT;
11095         case LE: return ARM_GE;
11096         case LT: return ARM_GT;
11097         case GEU: return ARM_LS;
11098         case GTU: return ARM_CC;
11099         case LEU: return ARM_CS;
11100         case LTU: return ARM_HI;
11101         default: gcc_unreachable ();
11102         }
11103
11104     case CC_Cmode:
11105       switch (comp_code)
11106       {
11107       case LTU: return ARM_CS;
11108       case GEU: return ARM_CC;
11109       default: gcc_unreachable ();
11110       }
11111
11112     case CCmode:
11113       switch (comp_code)
11114         {
11115         case NE: return ARM_NE;
11116         case EQ: return ARM_EQ;
11117         case GE: return ARM_GE;
11118         case GT: return ARM_GT;
11119         case LE: return ARM_LE;
11120         case LT: return ARM_LT;
11121         case GEU: return ARM_CS;
11122         case GTU: return ARM_HI;
11123         case LEU: return ARM_LS;
11124         case LTU: return ARM_CC;
11125         default: gcc_unreachable ();
11126         }
11127
11128     default: gcc_unreachable ();
11129     }
11130 }
11131
11132 void
11133 arm_final_prescan_insn (rtx insn)
11134 {
11135   /* BODY will hold the body of INSN.  */
11136   rtx body = PATTERN (insn);
11137
11138   /* This will be 1 if trying to repeat the trick, and things need to be
11139      reversed if it appears to fail.  */
11140   int reverse = 0;
11141
11142   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11143      taken are clobbered, even if the rtl suggests otherwise.  It also
11144      means that we have to grub around within the jump expression to find
11145      out what the conditions are when the jump isn't taken.  */
11146   int jump_clobbers = 0;
11147
11148   /* If we start with a return insn, we only succeed if we find another one.  */
11149   int seeking_return = 0;
11150
11151   /* START_INSN will hold the insn from where we start looking.  This is the
11152      first insn after the following code_label if REVERSE is true.  */
11153   rtx start_insn = insn;
11154
11155   /* If in state 4, check if the target branch is reached, in order to
11156      change back to state 0.  */
11157   if (arm_ccfsm_state == 4)
11158     {
11159       if (insn == arm_target_insn)
11160         {
11161           arm_target_insn = NULL;
11162           arm_ccfsm_state = 0;
11163         }
11164       return;
11165     }
11166
11167   /* If in state 3, it is possible to repeat the trick, if this insn is an
11168      unconditional branch to a label, and immediately following this branch
11169      is the previous target label which is only used once, and the label this
11170      branch jumps to is not too far off.  */
11171   if (arm_ccfsm_state == 3)
11172     {
11173       if (simplejump_p (insn))
11174         {
11175           start_insn = next_nonnote_insn (start_insn);
11176           if (GET_CODE (start_insn) == BARRIER)
11177             {
11178               /* XXX Isn't this always a barrier?  */
11179               start_insn = next_nonnote_insn (start_insn);
11180             }
11181           if (GET_CODE (start_insn) == CODE_LABEL
11182               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11183               && LABEL_NUSES (start_insn) == 1)
11184             reverse = TRUE;
11185           else
11186             return;
11187         }
11188       else if (GET_CODE (body) == RETURN)
11189         {
11190           start_insn = next_nonnote_insn (start_insn);
11191           if (GET_CODE (start_insn) == BARRIER)
11192             start_insn = next_nonnote_insn (start_insn);
11193           if (GET_CODE (start_insn) == CODE_LABEL
11194               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11195               && LABEL_NUSES (start_insn) == 1)
11196             {
11197               reverse = TRUE;
11198               seeking_return = 1;
11199             }
11200           else
11201             return;
11202         }
11203       else
11204         return;
11205     }
11206
11207   gcc_assert (!arm_ccfsm_state || reverse);
11208   if (GET_CODE (insn) != JUMP_INSN)
11209     return;
11210
11211   /* This jump might be paralleled with a clobber of the condition codes
11212      the jump should always come first */
11213   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11214     body = XVECEXP (body, 0, 0);
11215
11216   if (reverse
11217       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11218           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11219     {
11220       int insns_skipped;
11221       int fail = FALSE, succeed = FALSE;
11222       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11223       int then_not_else = TRUE;
11224       rtx this_insn = start_insn, label = 0;
11225
11226       /* If the jump cannot be done with one instruction, we cannot
11227          conditionally execute the instruction in the inverse case.  */
11228       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11229         {
11230           jump_clobbers = 1;
11231           return;
11232         }
11233
11234       /* Register the insn jumped to.  */
11235       if (reverse)
11236         {
11237           if (!seeking_return)
11238             label = XEXP (SET_SRC (body), 0);
11239         }
11240       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11241         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11242       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11243         {
11244           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11245           then_not_else = FALSE;
11246         }
11247       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11248         seeking_return = 1;
11249       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11250         {
11251           seeking_return = 1;
11252           then_not_else = FALSE;
11253         }
11254       else
11255         gcc_unreachable ();
11256
11257       /* See how many insns this branch skips, and what kind of insns.  If all
11258          insns are okay, and the label or unconditional branch to the same
11259          label is not too far away, succeed.  */
11260       for (insns_skipped = 0;
11261            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11262         {
11263           rtx scanbody;
11264
11265           this_insn = next_nonnote_insn (this_insn);
11266           if (!this_insn)
11267             break;
11268
11269           switch (GET_CODE (this_insn))
11270             {
11271             case CODE_LABEL:
11272               /* Succeed if it is the target label, otherwise fail since
11273                  control falls in from somewhere else.  */
11274               if (this_insn == label)
11275                 {
11276                   if (jump_clobbers)
11277                     {
11278                       arm_ccfsm_state = 2;
11279                       this_insn = next_nonnote_insn (this_insn);
11280                     }
11281                   else
11282                     arm_ccfsm_state = 1;
11283                   succeed = TRUE;
11284                 }
11285               else
11286                 fail = TRUE;
11287               break;
11288
11289             case BARRIER:
11290               /* Succeed if the following insn is the target label.
11291                  Otherwise fail.
11292                  If return insns are used then the last insn in a function
11293                  will be a barrier.  */
11294               this_insn = next_nonnote_insn (this_insn);
11295               if (this_insn && this_insn == label)
11296                 {
11297                   if (jump_clobbers)
11298                     {
11299                       arm_ccfsm_state = 2;
11300                       this_insn = next_nonnote_insn (this_insn);
11301                     }
11302                   else
11303                     arm_ccfsm_state = 1;
11304                   succeed = TRUE;
11305                 }
11306               else
11307                 fail = TRUE;
11308               break;
11309
11310             case CALL_INSN:
11311               /* The AAPCS says that conditional calls should not be
11312                  used since they make interworking inefficient (the
11313                  linker can't transform BL<cond> into BLX).  That's
11314                  only a problem if the machine has BLX.  */
11315               if (arm_arch5)
11316                 {
11317                   fail = TRUE;
11318                   break;
11319                 }
11320
11321               /* Succeed if the following insn is the target label, or
11322                  if the following two insns are a barrier and the
11323                  target label.  */
11324               this_insn = next_nonnote_insn (this_insn);
11325               if (this_insn && GET_CODE (this_insn) == BARRIER)
11326                 this_insn = next_nonnote_insn (this_insn);
11327
11328               if (this_insn && this_insn == label
11329                   && insns_skipped < max_insns_skipped)
11330                 {
11331                   if (jump_clobbers)
11332                     {
11333                       arm_ccfsm_state = 2;
11334                       this_insn = next_nonnote_insn (this_insn);
11335                     }
11336                   else
11337                     arm_ccfsm_state = 1;
11338                   succeed = TRUE;
11339                 }
11340               else
11341                 fail = TRUE;
11342               break;
11343
11344             case JUMP_INSN:
11345               /* If this is an unconditional branch to the same label, succeed.
11346                  If it is to another label, do nothing.  If it is conditional,
11347                  fail.  */
11348               /* XXX Probably, the tests for SET and the PC are
11349                  unnecessary.  */
11350
11351               scanbody = PATTERN (this_insn);
11352               if (GET_CODE (scanbody) == SET
11353                   && GET_CODE (SET_DEST (scanbody)) == PC)
11354                 {
11355                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11356                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11357                     {
11358                       arm_ccfsm_state = 2;
11359                       succeed = TRUE;
11360                     }
11361                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11362                     fail = TRUE;
11363                 }
11364               /* Fail if a conditional return is undesirable (e.g. on a
11365                  StrongARM), but still allow this if optimizing for size.  */
11366               else if (GET_CODE (scanbody) == RETURN
11367                        && !use_return_insn (TRUE, NULL)
11368                        && !optimize_size)
11369                 fail = TRUE;
11370               else if (GET_CODE (scanbody) == RETURN
11371                        && seeking_return)
11372                 {
11373                   arm_ccfsm_state = 2;
11374                   succeed = TRUE;
11375                 }
11376               else if (GET_CODE (scanbody) == PARALLEL)
11377                 {
11378                   switch (get_attr_conds (this_insn))
11379                     {
11380                     case CONDS_NOCOND:
11381                       break;
11382                     default:
11383                       fail = TRUE;
11384                       break;
11385                     }
11386                 }
11387               else
11388                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11389
11390               break;
11391
11392             case INSN:
11393               /* Instructions using or affecting the condition codes make it
11394                  fail.  */
11395               scanbody = PATTERN (this_insn);
11396               if (!(GET_CODE (scanbody) == SET
11397                     || GET_CODE (scanbody) == PARALLEL)
11398                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11399                 fail = TRUE;
11400
11401               /* A conditional cirrus instruction must be followed by
11402                  a non Cirrus instruction.  However, since we
11403                  conditionalize instructions in this function and by
11404                  the time we get here we can't add instructions
11405                  (nops), because shorten_branches() has already been
11406                  called, we will disable conditionalizing Cirrus
11407                  instructions to be safe.  */
11408               if (GET_CODE (scanbody) != USE
11409                   && GET_CODE (scanbody) != CLOBBER
11410                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11411                 fail = TRUE;
11412               break;
11413
11414             default:
11415               break;
11416             }
11417         }
11418       if (succeed)
11419         {
11420           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11421             arm_target_label = CODE_LABEL_NUMBER (label);
11422           else
11423             {
11424               gcc_assert (seeking_return || arm_ccfsm_state == 2);
11425               
11426               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11427                 {
11428                   this_insn = next_nonnote_insn (this_insn);
11429                   gcc_assert (!this_insn
11430                               || (GET_CODE (this_insn) != BARRIER
11431                                   && GET_CODE (this_insn) != CODE_LABEL));
11432                 }
11433               if (!this_insn)
11434                 {
11435                   /* Oh, dear! we ran off the end.. give up.  */
11436                   recog (PATTERN (insn), insn, NULL);
11437                   arm_ccfsm_state = 0;
11438                   arm_target_insn = NULL;
11439                   return;
11440                 }
11441               arm_target_insn = this_insn;
11442             }
11443           if (jump_clobbers)
11444             {
11445               gcc_assert (!reverse);
11446               arm_current_cc =
11447                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11448                                                             0), 0), 1));
11449               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11450                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11451               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11452                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11453             }
11454           else
11455             {
11456               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11457                  what it was.  */
11458               if (!reverse)
11459                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11460                                                                0));
11461             }
11462
11463           if (reverse || then_not_else)
11464             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11465         }
11466
11467       /* Restore recog_data (getting the attributes of other insns can
11468          destroy this array, but final.c assumes that it remains intact
11469          across this call; since the insn has been recognized already we
11470          call recog direct).  */
11471       recog (PATTERN (insn), insn, NULL);
11472     }
11473 }
11474
11475 /* Returns true if REGNO is a valid register
11476    for holding a quantity of type MODE.  */
11477 int
11478 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11479 {
11480   if (GET_MODE_CLASS (mode) == MODE_CC)
11481     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11482
11483   if (TARGET_THUMB)
11484     /* For the Thumb we only allow values bigger than SImode in
11485        registers 0 - 6, so that there is always a second low
11486        register available to hold the upper part of the value.
11487        We probably we ought to ensure that the register is the
11488        start of an even numbered register pair.  */
11489     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11490
11491   if (IS_CIRRUS_REGNUM (regno))
11492     /* We have outlawed SI values in Cirrus registers because they
11493        reside in the lower 32 bits, but SF values reside in the
11494        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11495        even split the registers into pairs because Cirrus SI values
11496        get sign extended to 64bits-- aldyh.  */
11497     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11498
11499   if (IS_VFP_REGNUM (regno))
11500     {
11501       if (mode == SFmode || mode == SImode)
11502         return TRUE;
11503
11504       /* DFmode values are only valid in even register pairs.  */
11505       if (mode == DFmode)
11506         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11507       return FALSE;
11508     }
11509
11510   if (IS_IWMMXT_GR_REGNUM (regno))
11511     return mode == SImode;
11512
11513   if (IS_IWMMXT_REGNUM (regno))
11514     return VALID_IWMMXT_REG_MODE (mode);
11515
11516   /* We allow any value to be stored in the general registers.
11517      Restrict doubleword quantities to even register pairs so that we can
11518      use ldrd.  */
11519   if (regno <= LAST_ARM_REGNUM)
11520     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11521
11522   if (   regno == FRAME_POINTER_REGNUM
11523       || regno == ARG_POINTER_REGNUM)
11524     /* We only allow integers in the fake hard registers.  */
11525     return GET_MODE_CLASS (mode) == MODE_INT;
11526
11527   /* The only registers left are the FPA registers
11528      which we only allow to hold FP values.  */
11529   return GET_MODE_CLASS (mode) == MODE_FLOAT
11530     && regno >= FIRST_FPA_REGNUM
11531     && regno <= LAST_FPA_REGNUM;
11532 }
11533
11534 int
11535 arm_regno_class (int regno)
11536 {
11537   if (TARGET_THUMB)
11538     {
11539       if (regno == STACK_POINTER_REGNUM)
11540         return STACK_REG;
11541       if (regno == CC_REGNUM)
11542         return CC_REG;
11543       if (regno < 8)
11544         return LO_REGS;
11545       return HI_REGS;
11546     }
11547
11548   if (   regno <= LAST_ARM_REGNUM
11549       || regno == FRAME_POINTER_REGNUM
11550       || regno == ARG_POINTER_REGNUM)
11551     return GENERAL_REGS;
11552
11553   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11554     return NO_REGS;
11555
11556   if (IS_CIRRUS_REGNUM (regno))
11557     return CIRRUS_REGS;
11558
11559   if (IS_VFP_REGNUM (regno))
11560     return VFP_REGS;
11561
11562   if (IS_IWMMXT_REGNUM (regno))
11563     return IWMMXT_REGS;
11564
11565   if (IS_IWMMXT_GR_REGNUM (regno))
11566     return IWMMXT_GR_REGS;
11567
11568   return FPA_REGS;
11569 }
11570
11571 /* Handle a special case when computing the offset
11572    of an argument from the frame pointer.  */
11573 int
11574 arm_debugger_arg_offset (int value, rtx addr)
11575 {
11576   rtx insn;
11577
11578   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11579   if (value != 0)
11580     return 0;
11581
11582   /* We can only cope with the case where the address is held in a register.  */
11583   if (GET_CODE (addr) != REG)
11584     return 0;
11585
11586   /* If we are using the frame pointer to point at the argument, then
11587      an offset of 0 is correct.  */
11588   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11589     return 0;
11590
11591   /* If we are using the stack pointer to point at the
11592      argument, then an offset of 0 is correct.  */
11593   if ((TARGET_THUMB || !frame_pointer_needed)
11594       && REGNO (addr) == SP_REGNUM)
11595     return 0;
11596
11597   /* Oh dear.  The argument is pointed to by a register rather
11598      than being held in a register, or being stored at a known
11599      offset from the frame pointer.  Since GDB only understands
11600      those two kinds of argument we must translate the address
11601      held in the register into an offset from the frame pointer.
11602      We do this by searching through the insns for the function
11603      looking to see where this register gets its value.  If the
11604      register is initialized from the frame pointer plus an offset
11605      then we are in luck and we can continue, otherwise we give up.
11606
11607      This code is exercised by producing debugging information
11608      for a function with arguments like this:
11609
11610            double func (double a, double b, int c, double d) {return d;}
11611
11612      Without this code the stab for parameter 'd' will be set to
11613      an offset of 0 from the frame pointer, rather than 8.  */
11614
11615   /* The if() statement says:
11616
11617      If the insn is a normal instruction
11618      and if the insn is setting the value in a register
11619      and if the register being set is the register holding the address of the argument
11620      and if the address is computing by an addition
11621      that involves adding to a register
11622      which is the frame pointer
11623      a constant integer
11624
11625      then...  */
11626
11627   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11628     {
11629       if (   GET_CODE (insn) == INSN
11630           && GET_CODE (PATTERN (insn)) == SET
11631           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11632           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11633           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11634           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11635           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11636              )
11637         {
11638           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11639
11640           break;
11641         }
11642     }
11643
11644   if (value == 0)
11645     {
11646       debug_rtx (addr);
11647       warning (0, "unable to compute real location of stacked parameter");
11648       value = 8; /* XXX magic hack */
11649     }
11650
11651   return value;
11652 }
11653 \f
11654 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11655   do                                                                    \
11656     {                                                                   \
11657       if ((MASK) & insn_flags)                                          \
11658         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11659                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11660     }                                                                   \
11661   while (0)
11662
11663 struct builtin_description
11664 {
11665   const unsigned int       mask;
11666   const enum insn_code     icode;
11667   const char * const       name;
11668   const enum arm_builtins  code;
11669   const enum rtx_code      comparison;
11670   const unsigned int       flag;
11671 };
11672
11673 static const struct builtin_description bdesc_2arg[] =
11674 {
11675 #define IWMMXT_BUILTIN(code, string, builtin) \
11676   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11677     ARM_BUILTIN_##builtin, 0, 0 },
11678
11679   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11680   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11681   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11682   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11683   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11684   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11685   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11686   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11687   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11688   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11689   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11690   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11691   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11692   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11693   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11694   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11695   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11696   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11697   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11698   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11699   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11700   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11701   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11702   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11703   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11704   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11705   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11706   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11707   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11708   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11709   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11710   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11711   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11712   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11713   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11714   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11715   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11716   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11717   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11718   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11719   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11720   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11721   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11722   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11723   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11724   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11725   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11726   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11727   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11728   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11729   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11730   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11731   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11732   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11733   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11734   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11735   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11736   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11737
11738 #define IWMMXT_BUILTIN2(code, builtin) \
11739   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11740
11741   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11742   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11743   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11744   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11745   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11746   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11747   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11748   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11749   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11750   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11751   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11752   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11753   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11754   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11755   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11756   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11757   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11758   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11759   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11760   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11761   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11762   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11763   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11764   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11765   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11766   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11767   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11768   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11769   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11770   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11771   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11772   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11773 };
11774
11775 static const struct builtin_description bdesc_1arg[] =
11776 {
11777   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11778   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11779   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11780   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11781   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11782   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11783   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11784   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11785   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11786   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11787   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11788   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11789   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11790   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11791   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11792   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11793   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11794   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11795 };
11796
11797 /* Set up all the iWMMXt builtins.  This is
11798    not called if TARGET_IWMMXT is zero.  */
11799
11800 static void
11801 arm_init_iwmmxt_builtins (void)
11802 {
11803   const struct builtin_description * d;
11804   size_t i;
11805   tree endlink = void_list_node;
11806
11807   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11808   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11809   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11810
11811   tree int_ftype_int
11812     = build_function_type (integer_type_node,
11813                            tree_cons (NULL_TREE, integer_type_node, endlink));
11814   tree v8qi_ftype_v8qi_v8qi_int
11815     = build_function_type (V8QI_type_node,
11816                            tree_cons (NULL_TREE, V8QI_type_node,
11817                                       tree_cons (NULL_TREE, V8QI_type_node,
11818                                                  tree_cons (NULL_TREE,
11819                                                             integer_type_node,
11820                                                             endlink))));
11821   tree v4hi_ftype_v4hi_int
11822     = build_function_type (V4HI_type_node,
11823                            tree_cons (NULL_TREE, V4HI_type_node,
11824                                       tree_cons (NULL_TREE, integer_type_node,
11825                                                  endlink)));
11826   tree v2si_ftype_v2si_int
11827     = build_function_type (V2SI_type_node,
11828                            tree_cons (NULL_TREE, V2SI_type_node,
11829                                       tree_cons (NULL_TREE, integer_type_node,
11830                                                  endlink)));
11831   tree v2si_ftype_di_di
11832     = build_function_type (V2SI_type_node,
11833                            tree_cons (NULL_TREE, long_long_integer_type_node,
11834                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11835                                                  endlink)));
11836   tree di_ftype_di_int
11837     = build_function_type (long_long_integer_type_node,
11838                            tree_cons (NULL_TREE, long_long_integer_type_node,
11839                                       tree_cons (NULL_TREE, integer_type_node,
11840                                                  endlink)));
11841   tree di_ftype_di_int_int
11842     = build_function_type (long_long_integer_type_node,
11843                            tree_cons (NULL_TREE, long_long_integer_type_node,
11844                                       tree_cons (NULL_TREE, integer_type_node,
11845                                                  tree_cons (NULL_TREE,
11846                                                             integer_type_node,
11847                                                             endlink))));
11848   tree int_ftype_v8qi
11849     = build_function_type (integer_type_node,
11850                            tree_cons (NULL_TREE, V8QI_type_node,
11851                                       endlink));
11852   tree int_ftype_v4hi
11853     = build_function_type (integer_type_node,
11854                            tree_cons (NULL_TREE, V4HI_type_node,
11855                                       endlink));
11856   tree int_ftype_v2si
11857     = build_function_type (integer_type_node,
11858                            tree_cons (NULL_TREE, V2SI_type_node,
11859                                       endlink));
11860   tree int_ftype_v8qi_int
11861     = build_function_type (integer_type_node,
11862                            tree_cons (NULL_TREE, V8QI_type_node,
11863                                       tree_cons (NULL_TREE, integer_type_node,
11864                                                  endlink)));
11865   tree int_ftype_v4hi_int
11866     = build_function_type (integer_type_node,
11867                            tree_cons (NULL_TREE, V4HI_type_node,
11868                                       tree_cons (NULL_TREE, integer_type_node,
11869                                                  endlink)));
11870   tree int_ftype_v2si_int
11871     = build_function_type (integer_type_node,
11872                            tree_cons (NULL_TREE, V2SI_type_node,
11873                                       tree_cons (NULL_TREE, integer_type_node,
11874                                                  endlink)));
11875   tree v8qi_ftype_v8qi_int_int
11876     = build_function_type (V8QI_type_node,
11877                            tree_cons (NULL_TREE, V8QI_type_node,
11878                                       tree_cons (NULL_TREE, integer_type_node,
11879                                                  tree_cons (NULL_TREE,
11880                                                             integer_type_node,
11881                                                             endlink))));
11882   tree v4hi_ftype_v4hi_int_int
11883     = build_function_type (V4HI_type_node,
11884                            tree_cons (NULL_TREE, V4HI_type_node,
11885                                       tree_cons (NULL_TREE, integer_type_node,
11886                                                  tree_cons (NULL_TREE,
11887                                                             integer_type_node,
11888                                                             endlink))));
11889   tree v2si_ftype_v2si_int_int
11890     = build_function_type (V2SI_type_node,
11891                            tree_cons (NULL_TREE, V2SI_type_node,
11892                                       tree_cons (NULL_TREE, integer_type_node,
11893                                                  tree_cons (NULL_TREE,
11894                                                             integer_type_node,
11895                                                             endlink))));
11896   /* Miscellaneous.  */
11897   tree v8qi_ftype_v4hi_v4hi
11898     = build_function_type (V8QI_type_node,
11899                            tree_cons (NULL_TREE, V4HI_type_node,
11900                                       tree_cons (NULL_TREE, V4HI_type_node,
11901                                                  endlink)));
11902   tree v4hi_ftype_v2si_v2si
11903     = build_function_type (V4HI_type_node,
11904                            tree_cons (NULL_TREE, V2SI_type_node,
11905                                       tree_cons (NULL_TREE, V2SI_type_node,
11906                                                  endlink)));
11907   tree v2si_ftype_v4hi_v4hi
11908     = build_function_type (V2SI_type_node,
11909                            tree_cons (NULL_TREE, V4HI_type_node,
11910                                       tree_cons (NULL_TREE, V4HI_type_node,
11911                                                  endlink)));
11912   tree v2si_ftype_v8qi_v8qi
11913     = build_function_type (V2SI_type_node,
11914                            tree_cons (NULL_TREE, V8QI_type_node,
11915                                       tree_cons (NULL_TREE, V8QI_type_node,
11916                                                  endlink)));
11917   tree v4hi_ftype_v4hi_di
11918     = build_function_type (V4HI_type_node,
11919                            tree_cons (NULL_TREE, V4HI_type_node,
11920                                       tree_cons (NULL_TREE,
11921                                                  long_long_integer_type_node,
11922                                                  endlink)));
11923   tree v2si_ftype_v2si_di
11924     = build_function_type (V2SI_type_node,
11925                            tree_cons (NULL_TREE, V2SI_type_node,
11926                                       tree_cons (NULL_TREE,
11927                                                  long_long_integer_type_node,
11928                                                  endlink)));
11929   tree void_ftype_int_int
11930     = build_function_type (void_type_node,
11931                            tree_cons (NULL_TREE, integer_type_node,
11932                                       tree_cons (NULL_TREE, integer_type_node,
11933                                                  endlink)));
11934   tree di_ftype_void
11935     = build_function_type (long_long_unsigned_type_node, endlink);
11936   tree di_ftype_v8qi
11937     = build_function_type (long_long_integer_type_node,
11938                            tree_cons (NULL_TREE, V8QI_type_node,
11939                                       endlink));
11940   tree di_ftype_v4hi
11941     = build_function_type (long_long_integer_type_node,
11942                            tree_cons (NULL_TREE, V4HI_type_node,
11943                                       endlink));
11944   tree di_ftype_v2si
11945     = build_function_type (long_long_integer_type_node,
11946                            tree_cons (NULL_TREE, V2SI_type_node,
11947                                       endlink));
11948   tree v2si_ftype_v4hi
11949     = build_function_type (V2SI_type_node,
11950                            tree_cons (NULL_TREE, V4HI_type_node,
11951                                       endlink));
11952   tree v4hi_ftype_v8qi
11953     = build_function_type (V4HI_type_node,
11954                            tree_cons (NULL_TREE, V8QI_type_node,
11955                                       endlink));
11956
11957   tree di_ftype_di_v4hi_v4hi
11958     = build_function_type (long_long_unsigned_type_node,
11959                            tree_cons (NULL_TREE,
11960                                       long_long_unsigned_type_node,
11961                                       tree_cons (NULL_TREE, V4HI_type_node,
11962                                                  tree_cons (NULL_TREE,
11963                                                             V4HI_type_node,
11964                                                             endlink))));
11965
11966   tree di_ftype_v4hi_v4hi
11967     = build_function_type (long_long_unsigned_type_node,
11968                            tree_cons (NULL_TREE, V4HI_type_node,
11969                                       tree_cons (NULL_TREE, V4HI_type_node,
11970                                                  endlink)));
11971
11972   /* Normal vector binops.  */
11973   tree v8qi_ftype_v8qi_v8qi
11974     = build_function_type (V8QI_type_node,
11975                            tree_cons (NULL_TREE, V8QI_type_node,
11976                                       tree_cons (NULL_TREE, V8QI_type_node,
11977                                                  endlink)));
11978   tree v4hi_ftype_v4hi_v4hi
11979     = build_function_type (V4HI_type_node,
11980                            tree_cons (NULL_TREE, V4HI_type_node,
11981                                       tree_cons (NULL_TREE, V4HI_type_node,
11982                                                  endlink)));
11983   tree v2si_ftype_v2si_v2si
11984     = build_function_type (V2SI_type_node,
11985                            tree_cons (NULL_TREE, V2SI_type_node,
11986                                       tree_cons (NULL_TREE, V2SI_type_node,
11987                                                  endlink)));
11988   tree di_ftype_di_di
11989     = build_function_type (long_long_unsigned_type_node,
11990                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11991                                       tree_cons (NULL_TREE,
11992                                                  long_long_unsigned_type_node,
11993                                                  endlink)));
11994
11995   /* Add all builtins that are more or less simple operations on two
11996      operands.  */
11997   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11998     {
11999       /* Use one of the operands; the target can have a different mode for
12000          mask-generating compares.  */
12001       enum machine_mode mode;
12002       tree type;
12003
12004       if (d->name == 0)
12005         continue;
12006
12007       mode = insn_data[d->icode].operand[1].mode;
12008
12009       switch (mode)
12010         {
12011         case V8QImode:
12012           type = v8qi_ftype_v8qi_v8qi;
12013           break;
12014         case V4HImode:
12015           type = v4hi_ftype_v4hi_v4hi;
12016           break;
12017         case V2SImode:
12018           type = v2si_ftype_v2si_v2si;
12019           break;
12020         case DImode:
12021           type = di_ftype_di_di;
12022           break;
12023
12024         default:
12025           gcc_unreachable ();
12026         }
12027
12028       def_mbuiltin (d->mask, d->name, type, d->code);
12029     }
12030
12031   /* Add the remaining MMX insns with somewhat more complicated types.  */
12032   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12033   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12034   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12035
12036   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12037   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12038   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12039   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12040   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12041   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12042
12043   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12044   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12045   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12046   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12047   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12048   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12049
12050   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12051   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12052   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12053   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12054   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12055   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12056
12057   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12058   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12059   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12060   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12061   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12062   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12063
12064   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12065
12066   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12067   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12068   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12069   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12070
12071   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12072   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12073   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12074   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12075   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12076   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12077   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12078   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12079   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12080
12081   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12082   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12083   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12084
12085   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12086   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12087   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12088
12089   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12090   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12091   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12092   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12093   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12094   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12095
12096   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12097   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12098   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12099   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12100   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12101   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12102   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12103   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12104   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12105   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12106   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12107   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12108
12109   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12110   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12111   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12112   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12113
12114   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12115   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12116   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12117   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12118   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12119   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12120   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12121 }
12122
12123 static void
12124 arm_init_builtins (void)
12125 {
12126   if (TARGET_REALLY_IWMMXT)
12127     arm_init_iwmmxt_builtins ();
12128 }
12129
12130 /* Errors in the source file can cause expand_expr to return const0_rtx
12131    where we expect a vector.  To avoid crashing, use one of the vector
12132    clear instructions.  */
12133
12134 static rtx
12135 safe_vector_operand (rtx x, enum machine_mode mode)
12136 {
12137   if (x != const0_rtx)
12138     return x;
12139   x = gen_reg_rtx (mode);
12140
12141   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12142                                : gen_rtx_SUBREG (DImode, x, 0)));
12143   return x;
12144 }
12145
12146 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12147
12148 static rtx
12149 arm_expand_binop_builtin (enum insn_code icode,
12150                           tree arglist, rtx target)
12151 {
12152   rtx pat;
12153   tree arg0 = TREE_VALUE (arglist);
12154   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12155   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12156   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12157   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12158   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12159   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12160
12161   if (VECTOR_MODE_P (mode0))
12162     op0 = safe_vector_operand (op0, mode0);
12163   if (VECTOR_MODE_P (mode1))
12164     op1 = safe_vector_operand (op1, mode1);
12165
12166   if (! target
12167       || GET_MODE (target) != tmode
12168       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12169     target = gen_reg_rtx (tmode);
12170
12171   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12172
12173   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12174     op0 = copy_to_mode_reg (mode0, op0);
12175   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12176     op1 = copy_to_mode_reg (mode1, op1);
12177
12178   pat = GEN_FCN (icode) (target, op0, op1);
12179   if (! pat)
12180     return 0;
12181   emit_insn (pat);
12182   return target;
12183 }
12184
12185 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12186
12187 static rtx
12188 arm_expand_unop_builtin (enum insn_code icode,
12189                          tree arglist, rtx target, int do_load)
12190 {
12191   rtx pat;
12192   tree arg0 = TREE_VALUE (arglist);
12193   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12194   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12195   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12196
12197   if (! target
12198       || GET_MODE (target) != tmode
12199       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12200     target = gen_reg_rtx (tmode);
12201   if (do_load)
12202     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12203   else
12204     {
12205       if (VECTOR_MODE_P (mode0))
12206         op0 = safe_vector_operand (op0, mode0);
12207
12208       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12209         op0 = copy_to_mode_reg (mode0, op0);
12210     }
12211
12212   pat = GEN_FCN (icode) (target, op0);
12213   if (! pat)
12214     return 0;
12215   emit_insn (pat);
12216   return target;
12217 }
12218
12219 /* Expand an expression EXP that calls a built-in function,
12220    with result going to TARGET if that's convenient
12221    (and in mode MODE if that's convenient).
12222    SUBTARGET may be used as the target for computing one of EXP's operands.
12223    IGNORE is nonzero if the value is to be ignored.  */
12224
12225 static rtx
12226 arm_expand_builtin (tree exp,
12227                     rtx target,
12228                     rtx subtarget ATTRIBUTE_UNUSED,
12229                     enum machine_mode mode ATTRIBUTE_UNUSED,
12230                     int ignore ATTRIBUTE_UNUSED)
12231 {
12232   const struct builtin_description * d;
12233   enum insn_code    icode;
12234   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12235   tree              arglist = TREE_OPERAND (exp, 1);
12236   tree              arg0;
12237   tree              arg1;
12238   tree              arg2;
12239   rtx               op0;
12240   rtx               op1;
12241   rtx               op2;
12242   rtx               pat;
12243   int               fcode = DECL_FUNCTION_CODE (fndecl);
12244   size_t            i;
12245   enum machine_mode tmode;
12246   enum machine_mode mode0;
12247   enum machine_mode mode1;
12248   enum machine_mode mode2;
12249
12250   switch (fcode)
12251     {
12252     case ARM_BUILTIN_TEXTRMSB:
12253     case ARM_BUILTIN_TEXTRMUB:
12254     case ARM_BUILTIN_TEXTRMSH:
12255     case ARM_BUILTIN_TEXTRMUH:
12256     case ARM_BUILTIN_TEXTRMSW:
12257     case ARM_BUILTIN_TEXTRMUW:
12258       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12259                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12260                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12261                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12262                : CODE_FOR_iwmmxt_textrmw);
12263
12264       arg0 = TREE_VALUE (arglist);
12265       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12266       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12267       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12268       tmode = insn_data[icode].operand[0].mode;
12269       mode0 = insn_data[icode].operand[1].mode;
12270       mode1 = insn_data[icode].operand[2].mode;
12271
12272       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12273         op0 = copy_to_mode_reg (mode0, op0);
12274       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12275         {
12276           /* @@@ better error message */
12277           error ("selector must be an immediate");
12278           return gen_reg_rtx (tmode);
12279         }
12280       if (target == 0
12281           || GET_MODE (target) != tmode
12282           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12283         target = gen_reg_rtx (tmode);
12284       pat = GEN_FCN (icode) (target, op0, op1);
12285       if (! pat)
12286         return 0;
12287       emit_insn (pat);
12288       return target;
12289
12290     case ARM_BUILTIN_TINSRB:
12291     case ARM_BUILTIN_TINSRH:
12292     case ARM_BUILTIN_TINSRW:
12293       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12294                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12295                : CODE_FOR_iwmmxt_tinsrw);
12296       arg0 = TREE_VALUE (arglist);
12297       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12298       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12299       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12300       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12301       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12302       tmode = insn_data[icode].operand[0].mode;
12303       mode0 = insn_data[icode].operand[1].mode;
12304       mode1 = insn_data[icode].operand[2].mode;
12305       mode2 = insn_data[icode].operand[3].mode;
12306
12307       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12308         op0 = copy_to_mode_reg (mode0, op0);
12309       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12310         op1 = copy_to_mode_reg (mode1, op1);
12311       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12312         {
12313           /* @@@ better error message */
12314           error ("selector must be an immediate");
12315           return const0_rtx;
12316         }
12317       if (target == 0
12318           || GET_MODE (target) != tmode
12319           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12320         target = gen_reg_rtx (tmode);
12321       pat = GEN_FCN (icode) (target, op0, op1, op2);
12322       if (! pat)
12323         return 0;
12324       emit_insn (pat);
12325       return target;
12326
12327     case ARM_BUILTIN_SETWCX:
12328       arg0 = TREE_VALUE (arglist);
12329       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12330       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12331       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12332       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12333       return 0;
12334
12335     case ARM_BUILTIN_GETWCX:
12336       arg0 = TREE_VALUE (arglist);
12337       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12338       target = gen_reg_rtx (SImode);
12339       emit_insn (gen_iwmmxt_tmrc (target, op0));
12340       return target;
12341
12342     case ARM_BUILTIN_WSHUFH:
12343       icode = CODE_FOR_iwmmxt_wshufh;
12344       arg0 = TREE_VALUE (arglist);
12345       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12346       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12347       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12348       tmode = insn_data[icode].operand[0].mode;
12349       mode1 = insn_data[icode].operand[1].mode;
12350       mode2 = insn_data[icode].operand[2].mode;
12351
12352       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12353         op0 = copy_to_mode_reg (mode1, op0);
12354       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12355         {
12356           /* @@@ better error message */
12357           error ("mask must be an immediate");
12358           return const0_rtx;
12359         }
12360       if (target == 0
12361           || GET_MODE (target) != tmode
12362           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12363         target = gen_reg_rtx (tmode);
12364       pat = GEN_FCN (icode) (target, op0, op1);
12365       if (! pat)
12366         return 0;
12367       emit_insn (pat);
12368       return target;
12369
12370     case ARM_BUILTIN_WSADB:
12371       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12372     case ARM_BUILTIN_WSADH:
12373       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12374     case ARM_BUILTIN_WSADBZ:
12375       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12376     case ARM_BUILTIN_WSADHZ:
12377       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12378
12379       /* Several three-argument builtins.  */
12380     case ARM_BUILTIN_WMACS:
12381     case ARM_BUILTIN_WMACU:
12382     case ARM_BUILTIN_WALIGN:
12383     case ARM_BUILTIN_TMIA:
12384     case ARM_BUILTIN_TMIAPH:
12385     case ARM_BUILTIN_TMIATT:
12386     case ARM_BUILTIN_TMIATB:
12387     case ARM_BUILTIN_TMIABT:
12388     case ARM_BUILTIN_TMIABB:
12389       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12390                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12391                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12392                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12393                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12394                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12395                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12396                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12397                : CODE_FOR_iwmmxt_walign);
12398       arg0 = TREE_VALUE (arglist);
12399       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12400       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12401       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12402       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12403       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12404       tmode = insn_data[icode].operand[0].mode;
12405       mode0 = insn_data[icode].operand[1].mode;
12406       mode1 = insn_data[icode].operand[2].mode;
12407       mode2 = insn_data[icode].operand[3].mode;
12408
12409       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12410         op0 = copy_to_mode_reg (mode0, op0);
12411       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12412         op1 = copy_to_mode_reg (mode1, op1);
12413       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12414         op2 = copy_to_mode_reg (mode2, op2);
12415       if (target == 0
12416           || GET_MODE (target) != tmode
12417           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12418         target = gen_reg_rtx (tmode);
12419       pat = GEN_FCN (icode) (target, op0, op1, op2);
12420       if (! pat)
12421         return 0;
12422       emit_insn (pat);
12423       return target;
12424
12425     case ARM_BUILTIN_WZERO:
12426       target = gen_reg_rtx (DImode);
12427       emit_insn (gen_iwmmxt_clrdi (target));
12428       return target;
12429
12430     default:
12431       break;
12432     }
12433
12434   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12435     if (d->code == (const enum arm_builtins) fcode)
12436       return arm_expand_binop_builtin (d->icode, arglist, target);
12437
12438   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12439     if (d->code == (const enum arm_builtins) fcode)
12440       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12441
12442   /* @@@ Should really do something sensible here.  */
12443   return NULL_RTX;
12444 }
12445 \f
12446 /* Return the number (counting from 0) of
12447    the least significant set bit in MASK.  */
12448
12449 inline static int
12450 number_of_first_bit_set (unsigned mask)
12451 {
12452   int bit;
12453
12454   for (bit = 0;
12455        (mask & (1 << bit)) == 0;
12456        ++bit)
12457     continue;
12458
12459   return bit;
12460 }
12461
12462 /* Emit code to push or pop registers to or from the stack.  F is the
12463    assembly file.  MASK is the registers to push or pop.  PUSH is
12464    nonzero if we should push, and zero if we should pop.  For debugging
12465    output, if pushing, adjust CFA_OFFSET by the amount of space added
12466    to the stack.  REAL_REGS should have the same number of bits set as
12467    MASK, and will be used instead (in the same order) to describe which
12468    registers were saved - this is used to mark the save slots when we
12469    push high registers after moving them to low registers.  */
12470 static void
12471 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12472                unsigned long real_regs)
12473 {
12474   int regno;
12475   int lo_mask = mask & 0xFF;
12476   int pushed_words = 0;
12477
12478   gcc_assert (mask);
12479
12480   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12481     {
12482       /* Special case.  Do not generate a POP PC statement here, do it in
12483          thumb_exit() */
12484       thumb_exit (f, -1);
12485       return;
12486     }
12487
12488   if (ARM_EABI_UNWIND_TABLES && push)
12489     {
12490       fprintf (f, "\t.save\t{");
12491       for (regno = 0; regno < 15; regno++)
12492         {
12493           if (real_regs & (1 << regno))
12494             {
12495               if (real_regs & ((1 << regno) -1))
12496                 fprintf (f, ", ");
12497               asm_fprintf (f, "%r", regno);
12498             }
12499         }
12500       fprintf (f, "}\n");
12501     }
12502
12503   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12504
12505   /* Look at the low registers first.  */
12506   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12507     {
12508       if (lo_mask & 1)
12509         {
12510           asm_fprintf (f, "%r", regno);
12511
12512           if ((lo_mask & ~1) != 0)
12513             fprintf (f, ", ");
12514
12515           pushed_words++;
12516         }
12517     }
12518
12519   if (push && (mask & (1 << LR_REGNUM)))
12520     {
12521       /* Catch pushing the LR.  */
12522       if (mask & 0xFF)
12523         fprintf (f, ", ");
12524
12525       asm_fprintf (f, "%r", LR_REGNUM);
12526
12527       pushed_words++;
12528     }
12529   else if (!push && (mask & (1 << PC_REGNUM)))
12530     {
12531       /* Catch popping the PC.  */
12532       if (TARGET_INTERWORK || TARGET_BACKTRACE
12533           || current_function_calls_eh_return)
12534         {
12535           /* The PC is never poped directly, instead
12536              it is popped into r3 and then BX is used.  */
12537           fprintf (f, "}\n");
12538
12539           thumb_exit (f, -1);
12540
12541           return;
12542         }
12543       else
12544         {
12545           if (mask & 0xFF)
12546             fprintf (f, ", ");
12547
12548           asm_fprintf (f, "%r", PC_REGNUM);
12549         }
12550     }
12551
12552   fprintf (f, "}\n");
12553
12554   if (push && pushed_words && dwarf2out_do_frame ())
12555     {
12556       char *l = dwarf2out_cfi_label ();
12557       int pushed_mask = real_regs;
12558
12559       *cfa_offset += pushed_words * 4;
12560       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12561
12562       pushed_words = 0;
12563       pushed_mask = real_regs;
12564       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12565         {
12566           if (pushed_mask & 1)
12567             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12568         }
12569     }
12570 }
12571
12572 /* Generate code to return from a thumb function.
12573    If 'reg_containing_return_addr' is -1, then the return address is
12574    actually on the stack, at the stack pointer.  */
12575 static void
12576 thumb_exit (FILE *f, int reg_containing_return_addr)
12577 {
12578   unsigned regs_available_for_popping;
12579   unsigned regs_to_pop;
12580   int pops_needed;
12581   unsigned available;
12582   unsigned required;
12583   int mode;
12584   int size;
12585   int restore_a4 = FALSE;
12586
12587   /* Compute the registers we need to pop.  */
12588   regs_to_pop = 0;
12589   pops_needed = 0;
12590
12591   if (reg_containing_return_addr == -1)
12592     {
12593       regs_to_pop |= 1 << LR_REGNUM;
12594       ++pops_needed;
12595     }
12596
12597   if (TARGET_BACKTRACE)
12598     {
12599       /* Restore the (ARM) frame pointer and stack pointer.  */
12600       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12601       pops_needed += 2;
12602     }
12603
12604   /* If there is nothing to pop then just emit the BX instruction and
12605      return.  */
12606   if (pops_needed == 0)
12607     {
12608       if (current_function_calls_eh_return)
12609         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12610
12611       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12612       return;
12613     }
12614   /* Otherwise if we are not supporting interworking and we have not created
12615      a backtrace structure and the function was not entered in ARM mode then
12616      just pop the return address straight into the PC.  */
12617   else if (!TARGET_INTERWORK
12618            && !TARGET_BACKTRACE
12619            && !is_called_in_ARM_mode (current_function_decl)
12620            && !current_function_calls_eh_return)
12621     {
12622       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12623       return;
12624     }
12625
12626   /* Find out how many of the (return) argument registers we can corrupt.  */
12627   regs_available_for_popping = 0;
12628
12629   /* If returning via __builtin_eh_return, the bottom three registers
12630      all contain information needed for the return.  */
12631   if (current_function_calls_eh_return)
12632     size = 12;
12633   else
12634     {
12635       /* If we can deduce the registers used from the function's
12636          return value.  This is more reliable that examining
12637          regs_ever_live[] because that will be set if the register is
12638          ever used in the function, not just if the register is used
12639          to hold a return value.  */
12640
12641       if (current_function_return_rtx != 0)
12642         mode = GET_MODE (current_function_return_rtx);
12643       else
12644         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12645
12646       size = GET_MODE_SIZE (mode);
12647
12648       if (size == 0)
12649         {
12650           /* In a void function we can use any argument register.
12651              In a function that returns a structure on the stack
12652              we can use the second and third argument registers.  */
12653           if (mode == VOIDmode)
12654             regs_available_for_popping =
12655               (1 << ARG_REGISTER (1))
12656               | (1 << ARG_REGISTER (2))
12657               | (1 << ARG_REGISTER (3));
12658           else
12659             regs_available_for_popping =
12660               (1 << ARG_REGISTER (2))
12661               | (1 << ARG_REGISTER (3));
12662         }
12663       else if (size <= 4)
12664         regs_available_for_popping =
12665           (1 << ARG_REGISTER (2))
12666           | (1 << ARG_REGISTER (3));
12667       else if (size <= 8)
12668         regs_available_for_popping =
12669           (1 << ARG_REGISTER (3));
12670     }
12671
12672   /* Match registers to be popped with registers into which we pop them.  */
12673   for (available = regs_available_for_popping,
12674        required  = regs_to_pop;
12675        required != 0 && available != 0;
12676        available &= ~(available & - available),
12677        required  &= ~(required  & - required))
12678     -- pops_needed;
12679
12680   /* If we have any popping registers left over, remove them.  */
12681   if (available > 0)
12682     regs_available_for_popping &= ~available;
12683
12684   /* Otherwise if we need another popping register we can use
12685      the fourth argument register.  */
12686   else if (pops_needed)
12687     {
12688       /* If we have not found any free argument registers and
12689          reg a4 contains the return address, we must move it.  */
12690       if (regs_available_for_popping == 0
12691           && reg_containing_return_addr == LAST_ARG_REGNUM)
12692         {
12693           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12694           reg_containing_return_addr = LR_REGNUM;
12695         }
12696       else if (size > 12)
12697         {
12698           /* Register a4 is being used to hold part of the return value,
12699              but we have dire need of a free, low register.  */
12700           restore_a4 = TRUE;
12701
12702           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12703         }
12704
12705       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12706         {
12707           /* The fourth argument register is available.  */
12708           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12709
12710           --pops_needed;
12711         }
12712     }
12713
12714   /* Pop as many registers as we can.  */
12715   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12716                  regs_available_for_popping);
12717
12718   /* Process the registers we popped.  */
12719   if (reg_containing_return_addr == -1)
12720     {
12721       /* The return address was popped into the lowest numbered register.  */
12722       regs_to_pop &= ~(1 << LR_REGNUM);
12723
12724       reg_containing_return_addr =
12725         number_of_first_bit_set (regs_available_for_popping);
12726
12727       /* Remove this register for the mask of available registers, so that
12728          the return address will not be corrupted by further pops.  */
12729       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12730     }
12731
12732   /* If we popped other registers then handle them here.  */
12733   if (regs_available_for_popping)
12734     {
12735       int frame_pointer;
12736
12737       /* Work out which register currently contains the frame pointer.  */
12738       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12739
12740       /* Move it into the correct place.  */
12741       asm_fprintf (f, "\tmov\t%r, %r\n",
12742                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12743
12744       /* (Temporarily) remove it from the mask of popped registers.  */
12745       regs_available_for_popping &= ~(1 << frame_pointer);
12746       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12747
12748       if (regs_available_for_popping)
12749         {
12750           int stack_pointer;
12751
12752           /* We popped the stack pointer as well,
12753              find the register that contains it.  */
12754           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12755
12756           /* Move it into the stack register.  */
12757           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12758
12759           /* At this point we have popped all necessary registers, so
12760              do not worry about restoring regs_available_for_popping
12761              to its correct value:
12762
12763              assert (pops_needed == 0)
12764              assert (regs_available_for_popping == (1 << frame_pointer))
12765              assert (regs_to_pop == (1 << STACK_POINTER))  */
12766         }
12767       else
12768         {
12769           /* Since we have just move the popped value into the frame
12770              pointer, the popping register is available for reuse, and
12771              we know that we still have the stack pointer left to pop.  */
12772           regs_available_for_popping |= (1 << frame_pointer);
12773         }
12774     }
12775
12776   /* If we still have registers left on the stack, but we no longer have
12777      any registers into which we can pop them, then we must move the return
12778      address into the link register and make available the register that
12779      contained it.  */
12780   if (regs_available_for_popping == 0 && pops_needed > 0)
12781     {
12782       regs_available_for_popping |= 1 << reg_containing_return_addr;
12783
12784       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12785                    reg_containing_return_addr);
12786
12787       reg_containing_return_addr = LR_REGNUM;
12788     }
12789
12790   /* If we have registers left on the stack then pop some more.
12791      We know that at most we will want to pop FP and SP.  */
12792   if (pops_needed > 0)
12793     {
12794       int  popped_into;
12795       int  move_to;
12796
12797       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12798                      regs_available_for_popping);
12799
12800       /* We have popped either FP or SP.
12801          Move whichever one it is into the correct register.  */
12802       popped_into = number_of_first_bit_set (regs_available_for_popping);
12803       move_to     = number_of_first_bit_set (regs_to_pop);
12804
12805       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12806
12807       regs_to_pop &= ~(1 << move_to);
12808
12809       --pops_needed;
12810     }
12811
12812   /* If we still have not popped everything then we must have only
12813      had one register available to us and we are now popping the SP.  */
12814   if (pops_needed > 0)
12815     {
12816       int  popped_into;
12817
12818       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12819                      regs_available_for_popping);
12820
12821       popped_into = number_of_first_bit_set (regs_available_for_popping);
12822
12823       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12824       /*
12825         assert (regs_to_pop == (1 << STACK_POINTER))
12826         assert (pops_needed == 1)
12827       */
12828     }
12829
12830   /* If necessary restore the a4 register.  */
12831   if (restore_a4)
12832     {
12833       if (reg_containing_return_addr != LR_REGNUM)
12834         {
12835           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12836           reg_containing_return_addr = LR_REGNUM;
12837         }
12838
12839       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12840     }
12841
12842   if (current_function_calls_eh_return)
12843     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12844
12845   /* Return to caller.  */
12846   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12847 }
12848
12849 \f
12850 void
12851 thumb_final_prescan_insn (rtx insn)
12852 {
12853   if (flag_print_asm_name)
12854     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12855                  INSN_ADDRESSES (INSN_UID (insn)));
12856 }
12857
12858 int
12859 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12860 {
12861   unsigned HOST_WIDE_INT mask = 0xff;
12862   int i;
12863
12864   if (val == 0) /* XXX */
12865     return 0;
12866
12867   for (i = 0; i < 25; i++)
12868     if ((val & (mask << i)) == val)
12869       return 1;
12870
12871   return 0;
12872 }
12873
12874 /* Returns nonzero if the current function contains,
12875    or might contain a far jump.  */
12876 static int
12877 thumb_far_jump_used_p (void)
12878 {
12879   rtx insn;
12880
12881   /* This test is only important for leaf functions.  */
12882   /* assert (!leaf_function_p ()); */
12883
12884   /* If we have already decided that far jumps may be used,
12885      do not bother checking again, and always return true even if
12886      it turns out that they are not being used.  Once we have made
12887      the decision that far jumps are present (and that hence the link
12888      register will be pushed onto the stack) we cannot go back on it.  */
12889   if (cfun->machine->far_jump_used)
12890     return 1;
12891
12892   /* If this function is not being called from the prologue/epilogue
12893      generation code then it must be being called from the
12894      INITIAL_ELIMINATION_OFFSET macro.  */
12895   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12896     {
12897       /* In this case we know that we are being asked about the elimination
12898          of the arg pointer register.  If that register is not being used,
12899          then there are no arguments on the stack, and we do not have to
12900          worry that a far jump might force the prologue to push the link
12901          register, changing the stack offsets.  In this case we can just
12902          return false, since the presence of far jumps in the function will
12903          not affect stack offsets.
12904
12905          If the arg pointer is live (or if it was live, but has now been
12906          eliminated and so set to dead) then we do have to test to see if
12907          the function might contain a far jump.  This test can lead to some
12908          false negatives, since before reload is completed, then length of
12909          branch instructions is not known, so gcc defaults to returning their
12910          longest length, which in turn sets the far jump attribute to true.
12911
12912          A false negative will not result in bad code being generated, but it
12913          will result in a needless push and pop of the link register.  We
12914          hope that this does not occur too often.
12915
12916          If we need doubleword stack alignment this could affect the other
12917          elimination offsets so we can't risk getting it wrong.  */
12918       if (regs_ever_live [ARG_POINTER_REGNUM])
12919         cfun->machine->arg_pointer_live = 1;
12920       else if (!cfun->machine->arg_pointer_live)
12921         return 0;
12922     }
12923
12924   /* Check to see if the function contains a branch
12925      insn with the far jump attribute set.  */
12926   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12927     {
12928       if (GET_CODE (insn) == JUMP_INSN
12929           /* Ignore tablejump patterns.  */
12930           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12931           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12932           && get_attr_far_jump (insn) == FAR_JUMP_YES
12933           )
12934         {
12935           /* Record the fact that we have decided that
12936              the function does use far jumps.  */
12937           cfun->machine->far_jump_used = 1;
12938           return 1;
12939         }
12940     }
12941
12942   return 0;
12943 }
12944
12945 /* Return nonzero if FUNC must be entered in ARM mode.  */
12946 int
12947 is_called_in_ARM_mode (tree func)
12948 {
12949   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
12950
12951   /* Ignore the problem about functions whose address is taken.  */
12952   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12953     return TRUE;
12954
12955 #ifdef ARM_PE
12956   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12957 #else
12958   return FALSE;
12959 #endif
12960 }
12961
12962 /* The bits which aren't usefully expanded as rtl.  */
12963 const char *
12964 thumb_unexpanded_epilogue (void)
12965 {
12966   int regno;
12967   unsigned long live_regs_mask = 0;
12968   int high_regs_pushed = 0;
12969   int had_to_push_lr;
12970   int size;
12971
12972   if (return_used_this_function)
12973     return "";
12974
12975   if (IS_NAKED (arm_current_func_type ()))
12976     return "";
12977
12978   live_regs_mask = thumb_compute_save_reg_mask ();
12979   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12980
12981   /* If we can deduce the registers used from the function's return value.
12982      This is more reliable that examining regs_ever_live[] because that
12983      will be set if the register is ever used in the function, not just if
12984      the register is used to hold a return value.  */
12985   size = arm_size_return_regs ();
12986
12987   /* The prolog may have pushed some high registers to use as
12988      work registers.  e.g. the testsuite file:
12989      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12990      compiles to produce:
12991         push    {r4, r5, r6, r7, lr}
12992         mov     r7, r9
12993         mov     r6, r8
12994         push    {r6, r7}
12995      as part of the prolog.  We have to undo that pushing here.  */
12996
12997   if (high_regs_pushed)
12998     {
12999       unsigned long mask = live_regs_mask & 0xff;
13000       int next_hi_reg;
13001
13002       /* The available low registers depend on the size of the value we are
13003          returning.  */
13004       if (size <= 12)
13005         mask |=  1 << 3;
13006       if (size <= 8)
13007         mask |= 1 << 2;
13008
13009       if (mask == 0)
13010         /* Oh dear!  We have no low registers into which we can pop
13011            high registers!  */
13012         internal_error
13013           ("no low registers available for popping high registers");
13014
13015       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13016         if (live_regs_mask & (1 << next_hi_reg))
13017           break;
13018
13019       while (high_regs_pushed)
13020         {
13021           /* Find lo register(s) into which the high register(s) can
13022              be popped.  */
13023           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13024             {
13025               if (mask & (1 << regno))
13026                 high_regs_pushed--;
13027               if (high_regs_pushed == 0)
13028                 break;
13029             }
13030
13031           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
13032
13033           /* Pop the values into the low register(s).  */
13034           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13035
13036           /* Move the value(s) into the high registers.  */
13037           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13038             {
13039               if (mask & (1 << regno))
13040                 {
13041                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13042                                regno);
13043
13044                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13045                     if (live_regs_mask & (1 << next_hi_reg))
13046                       break;
13047                 }
13048             }
13049         }
13050       live_regs_mask &= ~0x0f00;
13051     }
13052
13053   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13054   live_regs_mask &= 0xff;
13055
13056   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13057     {
13058       /* Pop the return address into the PC.  */
13059       if (had_to_push_lr)
13060         live_regs_mask |= 1 << PC_REGNUM;
13061
13062       /* Either no argument registers were pushed or a backtrace
13063          structure was created which includes an adjusted stack
13064          pointer, so just pop everything.  */
13065       if (live_regs_mask)
13066         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13067                        live_regs_mask);
13068
13069       /* We have either just popped the return address into the
13070          PC or it is was kept in LR for the entire function.  */
13071       if (!had_to_push_lr)
13072         thumb_exit (asm_out_file, LR_REGNUM);
13073     }
13074   else
13075     {
13076       /* Pop everything but the return address.  */
13077       if (live_regs_mask)
13078         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13079                        live_regs_mask);
13080
13081       if (had_to_push_lr)
13082         {
13083           if (size > 12)
13084             {
13085               /* We have no free low regs, so save one.  */
13086               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13087                            LAST_ARG_REGNUM);
13088             }
13089
13090           /* Get the return address into a temporary register.  */
13091           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13092                          1 << LAST_ARG_REGNUM);
13093
13094           if (size > 12)
13095             {
13096               /* Move the return address to lr.  */
13097               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13098                            LAST_ARG_REGNUM);
13099               /* Restore the low register.  */
13100               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13101                            IP_REGNUM);
13102               regno = LR_REGNUM;
13103             }
13104           else
13105             regno = LAST_ARG_REGNUM;
13106         }
13107       else
13108         regno = LR_REGNUM;
13109
13110       /* Remove the argument registers that were pushed onto the stack.  */
13111       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13112                    SP_REGNUM, SP_REGNUM,
13113                    current_function_pretend_args_size);
13114
13115       thumb_exit (asm_out_file, regno);
13116     }
13117
13118   return "";
13119 }
13120
13121 /* Functions to save and restore machine-specific function data.  */
13122 static struct machine_function *
13123 arm_init_machine_status (void)
13124 {
13125   struct machine_function *machine;
13126   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13127
13128 #if ARM_FT_UNKNOWN != 0
13129   machine->func_type = ARM_FT_UNKNOWN;
13130 #endif
13131   return machine;
13132 }
13133
13134 /* Return an RTX indicating where the return address to the
13135    calling function can be found.  */
13136 rtx
13137 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13138 {
13139   if (count != 0)
13140     return NULL_RTX;
13141
13142   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13143 }
13144
13145 /* Do anything needed before RTL is emitted for each function.  */
13146 void
13147 arm_init_expanders (void)
13148 {
13149   /* Arrange to initialize and mark the machine per-function status.  */
13150   init_machine_status = arm_init_machine_status;
13151
13152   /* This is to stop the combine pass optimizing away the alignment
13153      adjustment of va_arg.  */
13154   /* ??? It is claimed that this should not be necessary.  */
13155   if (cfun)
13156     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13157 }
13158
13159
13160 /* Like arm_compute_initial_elimination offset.  Simpler because
13161    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13162
13163 HOST_WIDE_INT
13164 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13165 {
13166   arm_stack_offsets *offsets;
13167
13168   offsets = arm_get_frame_offsets ();
13169
13170   switch (from)
13171     {
13172     case ARG_POINTER_REGNUM:
13173       switch (to)
13174         {
13175         case STACK_POINTER_REGNUM:
13176           return offsets->outgoing_args - offsets->saved_args;
13177
13178         case FRAME_POINTER_REGNUM:
13179           return offsets->soft_frame - offsets->saved_args;
13180
13181         case THUMB_HARD_FRAME_POINTER_REGNUM:
13182         case ARM_HARD_FRAME_POINTER_REGNUM:
13183           return offsets->saved_regs - offsets->saved_args;
13184
13185         default:
13186           gcc_unreachable ();
13187         }
13188       break;
13189
13190     case FRAME_POINTER_REGNUM:
13191       switch (to)
13192         {
13193         case STACK_POINTER_REGNUM:
13194           return offsets->outgoing_args - offsets->soft_frame;
13195
13196         case THUMB_HARD_FRAME_POINTER_REGNUM:
13197         case ARM_HARD_FRAME_POINTER_REGNUM:
13198           return offsets->saved_regs - offsets->soft_frame;
13199
13200         default:
13201           gcc_unreachable ();
13202         }
13203       break;
13204
13205     default:
13206       gcc_unreachable ();
13207     }
13208 }
13209
13210
13211 /* Generate the rest of a function's prologue.  */
13212 void
13213 thumb_expand_prologue (void)
13214 {
13215   rtx insn, dwarf;
13216
13217   HOST_WIDE_INT amount;
13218   arm_stack_offsets *offsets;
13219   unsigned long func_type;
13220   int regno;
13221   unsigned long live_regs_mask;
13222
13223   func_type = arm_current_func_type ();
13224
13225   /* Naked functions don't have prologues.  */
13226   if (IS_NAKED (func_type))
13227     return;
13228
13229   if (IS_INTERRUPT (func_type))
13230     {
13231       error ("interrupt Service Routines cannot be coded in Thumb mode");
13232       return;
13233     }
13234
13235   live_regs_mask = thumb_compute_save_reg_mask ();
13236   /* Load the pic register before setting the frame pointer,
13237      so we can use r7 as a temporary work register.  */
13238   if (flag_pic)
13239     arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13240
13241   offsets = arm_get_frame_offsets ();
13242
13243   if (frame_pointer_needed)
13244     {
13245       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13246                                    stack_pointer_rtx));
13247       RTX_FRAME_RELATED_P (insn) = 1;
13248     }
13249   else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13250     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13251                     stack_pointer_rtx);
13252
13253   amount = offsets->outgoing_args - offsets->saved_regs;
13254   if (amount)
13255     {
13256       if (amount < 512)
13257         {
13258           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13259                                         GEN_INT (- amount)));
13260           RTX_FRAME_RELATED_P (insn) = 1;
13261         }
13262       else
13263         {
13264           rtx reg;
13265
13266           /* The stack decrement is too big for an immediate value in a single
13267              insn.  In theory we could issue multiple subtracts, but after
13268              three of them it becomes more space efficient to place the full
13269              value in the constant pool and load into a register.  (Also the
13270              ARM debugger really likes to see only one stack decrement per
13271              function).  So instead we look for a scratch register into which
13272              we can load the decrement, and then we subtract this from the
13273              stack pointer.  Unfortunately on the thumb the only available
13274              scratch registers are the argument registers, and we cannot use
13275              these as they may hold arguments to the function.  Instead we
13276              attempt to locate a call preserved register which is used by this
13277              function.  If we can find one, then we know that it will have
13278              been pushed at the start of the prologue and so we can corrupt
13279              it now.  */
13280           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13281             if (live_regs_mask & (1 << regno)
13282                 && !(frame_pointer_needed
13283                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13284               break;
13285
13286           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13287             {
13288               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13289
13290               /* Choose an arbitrary, non-argument low register.  */
13291               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13292
13293               /* Save it by copying it into a high, scratch register.  */
13294               emit_insn (gen_movsi (spare, reg));
13295               /* Add a USE to stop propagate_one_insn() from barfing.  */
13296               emit_insn (gen_prologue_use (spare));
13297
13298               /* Decrement the stack.  */
13299               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13300               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13301                                             stack_pointer_rtx, reg));
13302               RTX_FRAME_RELATED_P (insn) = 1;
13303               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13304                                    plus_constant (stack_pointer_rtx,
13305                                                   -amount));
13306               RTX_FRAME_RELATED_P (dwarf) = 1;
13307               REG_NOTES (insn)
13308                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13309                                      REG_NOTES (insn));
13310
13311               /* Restore the low register's original value.  */
13312               emit_insn (gen_movsi (reg, spare));
13313
13314               /* Emit a USE of the restored scratch register, so that flow
13315                  analysis will not consider the restore redundant.  The
13316                  register won't be used again in this function and isn't
13317                  restored by the epilogue.  */
13318               emit_insn (gen_prologue_use (reg));
13319             }
13320           else
13321             {
13322               reg = gen_rtx_REG (SImode, regno);
13323
13324               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13325
13326               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13327                                             stack_pointer_rtx, reg));
13328               RTX_FRAME_RELATED_P (insn) = 1;
13329               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13330                                    plus_constant (stack_pointer_rtx,
13331                                                   -amount));
13332               RTX_FRAME_RELATED_P (dwarf) = 1;
13333               REG_NOTES (insn)
13334                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13335                                      REG_NOTES (insn));
13336             }
13337         }
13338       /* If the frame pointer is needed, emit a special barrier that
13339          will prevent the scheduler from moving stores to the frame
13340          before the stack adjustment.  */
13341       if (frame_pointer_needed)
13342         emit_insn (gen_stack_tie (stack_pointer_rtx,
13343                                   hard_frame_pointer_rtx));
13344     }
13345
13346   if (current_function_profile || !TARGET_SCHED_PROLOG)
13347     emit_insn (gen_blockage ());
13348
13349   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13350   if (live_regs_mask & 0xff)
13351     cfun->machine->lr_save_eliminated = 0;
13352
13353   /* If the link register is being kept alive, with the return address in it,
13354      then make sure that it does not get reused by the ce2 pass.  */
13355   if (cfun->machine->lr_save_eliminated)
13356     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13357 }
13358
13359
13360 void
13361 thumb_expand_epilogue (void)
13362 {
13363   HOST_WIDE_INT amount;
13364   arm_stack_offsets *offsets;
13365   int regno;
13366
13367   /* Naked functions don't have prologues.  */
13368   if (IS_NAKED (arm_current_func_type ()))
13369     return;
13370
13371   offsets = arm_get_frame_offsets ();
13372   amount = offsets->outgoing_args - offsets->saved_regs;
13373
13374   if (frame_pointer_needed)
13375     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13376   else if (amount)
13377     {
13378       if (amount < 512)
13379         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13380                                GEN_INT (amount)));
13381       else
13382         {
13383           /* r3 is always free in the epilogue.  */
13384           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13385
13386           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13387           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13388         }
13389     }
13390
13391   /* Emit a USE (stack_pointer_rtx), so that
13392      the stack adjustment will not be deleted.  */
13393   emit_insn (gen_prologue_use (stack_pointer_rtx));
13394
13395   if (current_function_profile || !TARGET_SCHED_PROLOG)
13396     emit_insn (gen_blockage ());
13397
13398   /* Emit a clobber for each insn that will be restored in the epilogue,
13399      so that flow2 will get register lifetimes correct.  */
13400   for (regno = 0; regno < 13; regno++)
13401     if (regs_ever_live[regno] && !call_used_regs[regno])
13402       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13403
13404   if (! regs_ever_live[LR_REGNUM])
13405     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13406 }
13407
13408 static void
13409 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13410 {
13411   unsigned long live_regs_mask = 0;
13412   unsigned long l_mask;
13413   unsigned high_regs_pushed = 0;
13414   int cfa_offset = 0;
13415   int regno;
13416
13417   if (IS_NAKED (arm_current_func_type ()))
13418     return;
13419
13420   if (is_called_in_ARM_mode (current_function_decl))
13421     {
13422       const char * name;
13423
13424       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13425       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13426                   == SYMBOL_REF);
13427       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13428
13429       /* Generate code sequence to switch us into Thumb mode.  */
13430       /* The .code 32 directive has already been emitted by
13431          ASM_DECLARE_FUNCTION_NAME.  */
13432       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13433       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13434
13435       /* Generate a label, so that the debugger will notice the
13436          change in instruction sets.  This label is also used by
13437          the assembler to bypass the ARM code when this function
13438          is called from a Thumb encoded function elsewhere in the
13439          same file.  Hence the definition of STUB_NAME here must
13440          agree with the definition in gas/config/tc-arm.c.  */
13441
13442 #define STUB_NAME ".real_start_of"
13443
13444       fprintf (f, "\t.code\t16\n");
13445 #ifdef ARM_PE
13446       if (arm_dllexport_name_p (name))
13447         name = arm_strip_name_encoding (name);
13448 #endif
13449       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13450       fprintf (f, "\t.thumb_func\n");
13451       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13452     }
13453
13454   if (current_function_pretend_args_size)
13455     {
13456       /* Output unwind directive for the stack adjustment.  */
13457       if (ARM_EABI_UNWIND_TABLES)
13458         fprintf (f, "\t.pad #%d\n",
13459                  current_function_pretend_args_size);
13460
13461       if (cfun->machine->uses_anonymous_args)
13462         {
13463           int num_pushes;
13464
13465           fprintf (f, "\tpush\t{");
13466
13467           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13468
13469           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13470                regno <= LAST_ARG_REGNUM;
13471                regno++)
13472             asm_fprintf (f, "%r%s", regno,
13473                          regno == LAST_ARG_REGNUM ? "" : ", ");
13474
13475           fprintf (f, "}\n");
13476         }
13477       else
13478         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13479                      SP_REGNUM, SP_REGNUM,
13480                      current_function_pretend_args_size);
13481
13482       /* We don't need to record the stores for unwinding (would it
13483          help the debugger any if we did?), but record the change in
13484          the stack pointer.  */
13485       if (dwarf2out_do_frame ())
13486         {
13487           char *l = dwarf2out_cfi_label ();
13488
13489           cfa_offset = cfa_offset + current_function_pretend_args_size;
13490           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13491         }
13492     }
13493
13494   /* Get the registers we are going to push.  */
13495   live_regs_mask = thumb_compute_save_reg_mask ();
13496   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13497   l_mask = live_regs_mask & 0x40ff;
13498   /* Then count how many other high registers will need to be pushed.  */
13499   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13500
13501   if (TARGET_BACKTRACE)
13502     {
13503       unsigned offset;
13504       unsigned work_register;
13505
13506       /* We have been asked to create a stack backtrace structure.
13507          The code looks like this:
13508
13509          0   .align 2
13510          0   func:
13511          0     sub   SP, #16         Reserve space for 4 registers.
13512          2     push  {R7}            Push low registers.
13513          4     add   R7, SP, #20     Get the stack pointer before the push.
13514          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13515          8     mov   R7, PC          Get hold of the start of this code plus 12.
13516         10     str   R7, [SP, #16]   Store it.
13517         12     mov   R7, FP          Get hold of the current frame pointer.
13518         14     str   R7, [SP, #4]    Store it.
13519         16     mov   R7, LR          Get hold of the current return address.
13520         18     str   R7, [SP, #12]   Store it.
13521         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13522         22     mov   FP, R7          Put this value into the frame pointer.  */
13523
13524       work_register = thumb_find_work_register (live_regs_mask);
13525
13526       if (ARM_EABI_UNWIND_TABLES)
13527         asm_fprintf (f, "\t.pad #16\n");
13528
13529       asm_fprintf
13530         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13531          SP_REGNUM, SP_REGNUM);
13532
13533       if (dwarf2out_do_frame ())
13534         {
13535           char *l = dwarf2out_cfi_label ();
13536
13537           cfa_offset = cfa_offset + 16;
13538           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13539         }
13540
13541       if (l_mask)
13542         {
13543           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13544           offset = bit_count (l_mask) * UNITS_PER_WORD;
13545         }
13546       else
13547         offset = 0;
13548
13549       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13550                    offset + 16 + current_function_pretend_args_size);
13551
13552       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13553                    offset + 4);
13554
13555       /* Make sure that the instruction fetching the PC is in the right place
13556          to calculate "start of backtrace creation code + 12".  */
13557       if (l_mask)
13558         {
13559           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13560           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13561                        offset + 12);
13562           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13563                        ARM_HARD_FRAME_POINTER_REGNUM);
13564           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13565                        offset);
13566         }
13567       else
13568         {
13569           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13570                        ARM_HARD_FRAME_POINTER_REGNUM);
13571           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13572                        offset);
13573           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13574           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13575                        offset + 12);
13576         }
13577
13578       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13579       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13580                    offset + 8);
13581       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13582                    offset + 12);
13583       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13584                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13585     }
13586   /* Optimization:  If we are not pushing any low registers but we are going
13587      to push some high registers then delay our first push.  This will just
13588      be a push of LR and we can combine it with the push of the first high
13589      register.  */
13590   else if ((l_mask & 0xff) != 0
13591            || (high_regs_pushed == 0 && l_mask))
13592     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13593
13594   if (high_regs_pushed)
13595     {
13596       unsigned pushable_regs;
13597       unsigned next_hi_reg;
13598
13599       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13600         if (live_regs_mask & (1 << next_hi_reg))
13601           break;
13602
13603       pushable_regs = l_mask & 0xff;
13604
13605       if (pushable_regs == 0)
13606         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13607
13608       while (high_regs_pushed > 0)
13609         {
13610           unsigned long real_regs_mask = 0;
13611
13612           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13613             {
13614               if (pushable_regs & (1 << regno))
13615                 {
13616                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13617
13618                   high_regs_pushed --;
13619                   real_regs_mask |= (1 << next_hi_reg);
13620
13621                   if (high_regs_pushed)
13622                     {
13623                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13624                            next_hi_reg --)
13625                         if (live_regs_mask & (1 << next_hi_reg))
13626                           break;
13627                     }
13628                   else
13629                     {
13630                       pushable_regs &= ~((1 << regno) - 1);
13631                       break;
13632                     }
13633                 }
13634             }
13635
13636           /* If we had to find a work register and we have not yet
13637              saved the LR then add it to the list of regs to push.  */
13638           if (l_mask == (1 << LR_REGNUM))
13639             {
13640               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13641                              1, &cfa_offset,
13642                              real_regs_mask | (1 << LR_REGNUM));
13643               l_mask = 0;
13644             }
13645           else
13646             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13647         }
13648     }
13649 }
13650
13651 /* Handle the case of a double word load into a low register from
13652    a computed memory address.  The computed address may involve a
13653    register which is overwritten by the load.  */
13654 const char *
13655 thumb_load_double_from_address (rtx *operands)
13656 {
13657   rtx addr;
13658   rtx base;
13659   rtx offset;
13660   rtx arg1;
13661   rtx arg2;
13662
13663   gcc_assert (GET_CODE (operands[0]) == REG);
13664   gcc_assert (GET_CODE (operands[1]) == MEM);
13665
13666   /* Get the memory address.  */
13667   addr = XEXP (operands[1], 0);
13668
13669   /* Work out how the memory address is computed.  */
13670   switch (GET_CODE (addr))
13671     {
13672     case REG:
13673       operands[2] = gen_rtx_MEM (SImode,
13674                                  plus_constant (XEXP (operands[1], 0), 4));
13675
13676       if (REGNO (operands[0]) == REGNO (addr))
13677         {
13678           output_asm_insn ("ldr\t%H0, %2", operands);
13679           output_asm_insn ("ldr\t%0, %1", operands);
13680         }
13681       else
13682         {
13683           output_asm_insn ("ldr\t%0, %1", operands);
13684           output_asm_insn ("ldr\t%H0, %2", operands);
13685         }
13686       break;
13687
13688     case CONST:
13689       /* Compute <address> + 4 for the high order load.  */
13690       operands[2] = gen_rtx_MEM (SImode,
13691                                  plus_constant (XEXP (operands[1], 0), 4));
13692
13693       output_asm_insn ("ldr\t%0, %1", operands);
13694       output_asm_insn ("ldr\t%H0, %2", operands);
13695       break;
13696
13697     case PLUS:
13698       arg1   = XEXP (addr, 0);
13699       arg2   = XEXP (addr, 1);
13700
13701       if (CONSTANT_P (arg1))
13702         base = arg2, offset = arg1;
13703       else
13704         base = arg1, offset = arg2;
13705
13706       gcc_assert (GET_CODE (base) == REG);
13707
13708       /* Catch the case of <address> = <reg> + <reg> */
13709       if (GET_CODE (offset) == REG)
13710         {
13711           int reg_offset = REGNO (offset);
13712           int reg_base   = REGNO (base);
13713           int reg_dest   = REGNO (operands[0]);
13714
13715           /* Add the base and offset registers together into the
13716              higher destination register.  */
13717           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13718                        reg_dest + 1, reg_base, reg_offset);
13719
13720           /* Load the lower destination register from the address in
13721              the higher destination register.  */
13722           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13723                        reg_dest, reg_dest + 1);
13724
13725           /* Load the higher destination register from its own address
13726              plus 4.  */
13727           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13728                        reg_dest + 1, reg_dest + 1);
13729         }
13730       else
13731         {
13732           /* Compute <address> + 4 for the high order load.  */
13733           operands[2] = gen_rtx_MEM (SImode,
13734                                      plus_constant (XEXP (operands[1], 0), 4));
13735
13736           /* If the computed address is held in the low order register
13737              then load the high order register first, otherwise always
13738              load the low order register first.  */
13739           if (REGNO (operands[0]) == REGNO (base))
13740             {
13741               output_asm_insn ("ldr\t%H0, %2", operands);
13742               output_asm_insn ("ldr\t%0, %1", operands);
13743             }
13744           else
13745             {
13746               output_asm_insn ("ldr\t%0, %1", operands);
13747               output_asm_insn ("ldr\t%H0, %2", operands);
13748             }
13749         }
13750       break;
13751
13752     case LABEL_REF:
13753       /* With no registers to worry about we can just load the value
13754          directly.  */
13755       operands[2] = gen_rtx_MEM (SImode,
13756                                  plus_constant (XEXP (operands[1], 0), 4));
13757
13758       output_asm_insn ("ldr\t%H0, %2", operands);
13759       output_asm_insn ("ldr\t%0, %1", operands);
13760       break;
13761
13762     default:
13763       gcc_unreachable ();
13764     }
13765
13766   return "";
13767 }
13768
13769 const char *
13770 thumb_output_move_mem_multiple (int n, rtx *operands)
13771 {
13772   rtx tmp;
13773
13774   switch (n)
13775     {
13776     case 2:
13777       if (REGNO (operands[4]) > REGNO (operands[5]))
13778         {
13779           tmp = operands[4];
13780           operands[4] = operands[5];
13781           operands[5] = tmp;
13782         }
13783       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13784       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13785       break;
13786
13787     case 3:
13788       if (REGNO (operands[4]) > REGNO (operands[5]))
13789         {
13790           tmp = operands[4];
13791           operands[4] = operands[5];
13792           operands[5] = tmp;
13793         }
13794       if (REGNO (operands[5]) > REGNO (operands[6]))
13795         {
13796           tmp = operands[5];
13797           operands[5] = operands[6];
13798           operands[6] = tmp;
13799         }
13800       if (REGNO (operands[4]) > REGNO (operands[5]))
13801         {
13802           tmp = operands[4];
13803           operands[4] = operands[5];
13804           operands[5] = tmp;
13805         }
13806
13807       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13808       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13809       break;
13810
13811     default:
13812       gcc_unreachable ();
13813     }
13814
13815   return "";
13816 }
13817
13818 /* Output a call-via instruction for thumb state.  */
13819 const char *
13820 thumb_call_via_reg (rtx reg)
13821 {
13822   int regno = REGNO (reg);
13823   rtx *labelp;
13824
13825   gcc_assert (regno < LR_REGNUM);
13826
13827   /* If we are in the normal text section we can use a single instance
13828      per compilation unit.  If we are doing function sections, then we need
13829      an entry per section, since we can't rely on reachability.  */
13830   if (in_text_section ())
13831     {
13832       thumb_call_reg_needed = 1;
13833
13834       if (thumb_call_via_label[regno] == NULL)
13835         thumb_call_via_label[regno] = gen_label_rtx ();
13836       labelp = thumb_call_via_label + regno;
13837     }
13838   else
13839     {
13840       if (cfun->machine->call_via[regno] == NULL)
13841         cfun->machine->call_via[regno] = gen_label_rtx ();
13842       labelp = cfun->machine->call_via + regno;
13843     }
13844
13845   output_asm_insn ("bl\t%a0", labelp);
13846   return "";
13847 }
13848
13849 /* Routines for generating rtl.  */
13850 void
13851 thumb_expand_movmemqi (rtx *operands)
13852 {
13853   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13854   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13855   HOST_WIDE_INT len = INTVAL (operands[2]);
13856   HOST_WIDE_INT offset = 0;
13857
13858   while (len >= 12)
13859     {
13860       emit_insn (gen_movmem12b (out, in, out, in));
13861       len -= 12;
13862     }
13863
13864   if (len >= 8)
13865     {
13866       emit_insn (gen_movmem8b (out, in, out, in));
13867       len -= 8;
13868     }
13869
13870   if (len >= 4)
13871     {
13872       rtx reg = gen_reg_rtx (SImode);
13873       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13874       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13875       len -= 4;
13876       offset += 4;
13877     }
13878
13879   if (len >= 2)
13880     {
13881       rtx reg = gen_reg_rtx (HImode);
13882       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13883                                               plus_constant (in, offset))));
13884       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13885                             reg));
13886       len -= 2;
13887       offset += 2;
13888     }
13889
13890   if (len)
13891     {
13892       rtx reg = gen_reg_rtx (QImode);
13893       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13894                                               plus_constant (in, offset))));
13895       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13896                             reg));
13897     }
13898 }
13899
13900 void
13901 thumb_reload_out_hi (rtx *operands)
13902 {
13903   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13904 }
13905
13906 /* Handle reading a half-word from memory during reload.  */
13907 void
13908 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13909 {
13910   gcc_unreachable ();
13911 }
13912
13913 /* Return the length of a function name prefix
13914     that starts with the character 'c'.  */
13915 static int
13916 arm_get_strip_length (int c)
13917 {
13918   switch (c)
13919     {
13920     ARM_NAME_ENCODING_LENGTHS
13921       default: return 0;
13922     }
13923 }
13924
13925 /* Return a pointer to a function's name with any
13926    and all prefix encodings stripped from it.  */
13927 const char *
13928 arm_strip_name_encoding (const char *name)
13929 {
13930   int skip;
13931
13932   while ((skip = arm_get_strip_length (* name)))
13933     name += skip;
13934
13935   return name;
13936 }
13937
13938 /* If there is a '*' anywhere in the name's prefix, then
13939    emit the stripped name verbatim, otherwise prepend an
13940    underscore if leading underscores are being used.  */
13941 void
13942 arm_asm_output_labelref (FILE *stream, const char *name)
13943 {
13944   int skip;
13945   int verbatim = 0;
13946
13947   while ((skip = arm_get_strip_length (* name)))
13948     {
13949       verbatim |= (*name == '*');
13950       name += skip;
13951     }
13952
13953   if (verbatim)
13954     fputs (name, stream);
13955   else
13956     asm_fprintf (stream, "%U%s", name);
13957 }
13958
13959 static void
13960 arm_file_end (void)
13961 {
13962   int regno;
13963
13964   if (! thumb_call_reg_needed)
13965     return;
13966
13967   text_section ();
13968   asm_fprintf (asm_out_file, "\t.code 16\n");
13969   ASM_OUTPUT_ALIGN (asm_out_file, 1);
13970
13971   for (regno = 0; regno < LR_REGNUM; regno++)
13972     {
13973       rtx label = thumb_call_via_label[regno];
13974
13975       if (label != 0)
13976         {
13977           targetm.asm_out.internal_label (asm_out_file, "L",
13978                                           CODE_LABEL_NUMBER (label));
13979           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13980         }
13981     }
13982 }
13983
13984 rtx aof_pic_label;
13985
13986 #ifdef AOF_ASSEMBLER
13987 /* Special functions only needed when producing AOF syntax assembler.  */
13988
13989 struct pic_chain
13990 {
13991   struct pic_chain * next;
13992   const char * symname;
13993 };
13994
13995 static struct pic_chain * aof_pic_chain = NULL;
13996
13997 rtx
13998 aof_pic_entry (rtx x)
13999 {
14000   struct pic_chain ** chainp;
14001   int offset;
14002
14003   if (aof_pic_label == NULL_RTX)
14004     {
14005       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14006     }
14007
14008   for (offset = 0, chainp = &aof_pic_chain; *chainp;
14009        offset += 4, chainp = &(*chainp)->next)
14010     if ((*chainp)->symname == XSTR (x, 0))
14011       return plus_constant (aof_pic_label, offset);
14012
14013   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14014   (*chainp)->next = NULL;
14015   (*chainp)->symname = XSTR (x, 0);
14016   return plus_constant (aof_pic_label, offset);
14017 }
14018
14019 void
14020 aof_dump_pic_table (FILE *f)
14021 {
14022   struct pic_chain * chain;
14023
14024   if (aof_pic_chain == NULL)
14025     return;
14026
14027   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14028                PIC_OFFSET_TABLE_REGNUM,
14029                PIC_OFFSET_TABLE_REGNUM);
14030   fputs ("|x$adcons|\n", f);
14031
14032   for (chain = aof_pic_chain; chain; chain = chain->next)
14033     {
14034       fputs ("\tDCD\t", f);
14035       assemble_name (f, chain->symname);
14036       fputs ("\n", f);
14037     }
14038 }
14039
14040 int arm_text_section_count = 1;
14041
14042 char *
14043 aof_text_section (void )
14044 {
14045   static char buf[100];
14046   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
14047            arm_text_section_count++);
14048   if (flag_pic)
14049     strcat (buf, ", PIC, REENTRANT");
14050   return buf;
14051 }
14052
14053 static int arm_data_section_count = 1;
14054
14055 char *
14056 aof_data_section (void)
14057 {
14058   static char buf[100];
14059   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
14060   return buf;
14061 }
14062
14063 /* The AOF assembler is religiously strict about declarations of
14064    imported and exported symbols, so that it is impossible to declare
14065    a function as imported near the beginning of the file, and then to
14066    export it later on.  It is, however, possible to delay the decision
14067    until all the functions in the file have been compiled.  To get
14068    around this, we maintain a list of the imports and exports, and
14069    delete from it any that are subsequently defined.  At the end of
14070    compilation we spit the remainder of the list out before the END
14071    directive.  */
14072
14073 struct import
14074 {
14075   struct import * next;
14076   const char * name;
14077 };
14078
14079 static struct import * imports_list = NULL;
14080
14081 void
14082 aof_add_import (const char *name)
14083 {
14084   struct import * new;
14085
14086   for (new = imports_list; new; new = new->next)
14087     if (new->name == name)
14088       return;
14089
14090   new = (struct import *) xmalloc (sizeof (struct import));
14091   new->next = imports_list;
14092   imports_list = new;
14093   new->name = name;
14094 }
14095
14096 void
14097 aof_delete_import (const char *name)
14098 {
14099   struct import ** old;
14100
14101   for (old = &imports_list; *old; old = & (*old)->next)
14102     {
14103       if ((*old)->name == name)
14104         {
14105           *old = (*old)->next;
14106           return;
14107         }
14108     }
14109 }
14110
14111 int arm_main_function = 0;
14112
14113 static void
14114 aof_dump_imports (FILE *f)
14115 {
14116   /* The AOF assembler needs this to cause the startup code to be extracted
14117      from the library.  Brining in __main causes the whole thing to work
14118      automagically.  */
14119   if (arm_main_function)
14120     {
14121       text_section ();
14122       fputs ("\tIMPORT __main\n", f);
14123       fputs ("\tDCD __main\n", f);
14124     }
14125
14126   /* Now dump the remaining imports.  */
14127   while (imports_list)
14128     {
14129       fprintf (f, "\tIMPORT\t");
14130       assemble_name (f, imports_list->name);
14131       fputc ('\n', f);
14132       imports_list = imports_list->next;
14133     }
14134 }
14135
14136 static void
14137 aof_globalize_label (FILE *stream, const char *name)
14138 {
14139   default_globalize_label (stream, name);
14140   if (! strcmp (name, "main"))
14141     arm_main_function = 1;
14142 }
14143
14144 static void
14145 aof_file_start (void)
14146 {
14147   fputs ("__r0\tRN\t0\n", asm_out_file);
14148   fputs ("__a1\tRN\t0\n", asm_out_file);
14149   fputs ("__a2\tRN\t1\n", asm_out_file);
14150   fputs ("__a3\tRN\t2\n", asm_out_file);
14151   fputs ("__a4\tRN\t3\n", asm_out_file);
14152   fputs ("__v1\tRN\t4\n", asm_out_file);
14153   fputs ("__v2\tRN\t5\n", asm_out_file);
14154   fputs ("__v3\tRN\t6\n", asm_out_file);
14155   fputs ("__v4\tRN\t7\n", asm_out_file);
14156   fputs ("__v5\tRN\t8\n", asm_out_file);
14157   fputs ("__v6\tRN\t9\n", asm_out_file);
14158   fputs ("__sl\tRN\t10\n", asm_out_file);
14159   fputs ("__fp\tRN\t11\n", asm_out_file);
14160   fputs ("__ip\tRN\t12\n", asm_out_file);
14161   fputs ("__sp\tRN\t13\n", asm_out_file);
14162   fputs ("__lr\tRN\t14\n", asm_out_file);
14163   fputs ("__pc\tRN\t15\n", asm_out_file);
14164   fputs ("__f0\tFN\t0\n", asm_out_file);
14165   fputs ("__f1\tFN\t1\n", asm_out_file);
14166   fputs ("__f2\tFN\t2\n", asm_out_file);
14167   fputs ("__f3\tFN\t3\n", asm_out_file);
14168   fputs ("__f4\tFN\t4\n", asm_out_file);
14169   fputs ("__f5\tFN\t5\n", asm_out_file);
14170   fputs ("__f6\tFN\t6\n", asm_out_file);
14171   fputs ("__f7\tFN\t7\n", asm_out_file);
14172   text_section ();
14173 }
14174
14175 static void
14176 aof_file_end (void)
14177 {
14178   if (flag_pic)
14179     aof_dump_pic_table (asm_out_file);
14180   arm_file_end ();
14181   aof_dump_imports (asm_out_file);
14182   fputs ("\tEND\n", asm_out_file);
14183 }
14184 #endif /* AOF_ASSEMBLER */
14185
14186 #ifndef ARM_PE
14187 /* Symbols in the text segment can be accessed without indirecting via the
14188    constant pool; it may take an extra binary operation, but this is still
14189    faster than indirecting via memory.  Don't do this when not optimizing,
14190    since we won't be calculating al of the offsets necessary to do this
14191    simplification.  */
14192
14193 static void
14194 arm_encode_section_info (tree decl, rtx rtl, int first)
14195 {
14196   /* This doesn't work with AOF syntax, since the string table may be in
14197      a different AREA.  */
14198 #ifndef AOF_ASSEMBLER
14199   if (optimize > 0 && TREE_CONSTANT (decl))
14200     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14201 #endif
14202
14203   /* If we are referencing a function that is weak then encode a long call
14204      flag in the function name, otherwise if the function is static or
14205      or known to be defined in this file then encode a short call flag.  */
14206   if (first && DECL_P (decl))
14207     {
14208       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14209         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14210       else if (! TREE_PUBLIC (decl))
14211         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14212     }
14213 }
14214 #endif /* !ARM_PE */
14215
14216 static void
14217 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14218 {
14219   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14220       && !strcmp (prefix, "L"))
14221     {
14222       arm_ccfsm_state = 0;
14223       arm_target_insn = NULL;
14224     }
14225   default_internal_label (stream, prefix, labelno);
14226 }
14227
14228 /* Output code to add DELTA to the first argument, and then jump
14229    to FUNCTION.  Used for C++ multiple inheritance.  */
14230 static void
14231 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14232                      HOST_WIDE_INT delta,
14233                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14234                      tree function)
14235 {
14236   static int thunk_label = 0;
14237   char label[256];
14238   int mi_delta = delta;
14239   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14240   int shift = 0;
14241   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14242                     ? 1 : 0);
14243   if (mi_delta < 0)
14244     mi_delta = - mi_delta;
14245   if (TARGET_THUMB)
14246     {
14247       int labelno = thunk_label++;
14248       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14249       fputs ("\tldr\tr12, ", file);
14250       assemble_name (file, label);
14251       fputc ('\n', file);
14252     }
14253   while (mi_delta != 0)
14254     {
14255       if ((mi_delta & (3 << shift)) == 0)
14256         shift += 2;
14257       else
14258         {
14259           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14260                        mi_op, this_regno, this_regno,
14261                        mi_delta & (0xff << shift));
14262           mi_delta &= ~(0xff << shift);
14263           shift += 8;
14264         }
14265     }
14266   if (TARGET_THUMB)
14267     {
14268       fprintf (file, "\tbx\tr12\n");
14269       ASM_OUTPUT_ALIGN (file, 2);
14270       assemble_name (file, label);
14271       fputs (":\n", file);
14272       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14273     }
14274   else
14275     {
14276       fputs ("\tb\t", file);
14277       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14278       if (NEED_PLT_RELOC)
14279         fputs ("(PLT)", file);
14280       fputc ('\n', file);
14281     }
14282 }
14283
14284 int
14285 arm_emit_vector_const (FILE *file, rtx x)
14286 {
14287   int i;
14288   const char * pattern;
14289
14290   gcc_assert (GET_CODE (x) == CONST_VECTOR);
14291
14292   switch (GET_MODE (x))
14293     {
14294     case V2SImode: pattern = "%08x"; break;
14295     case V4HImode: pattern = "%04x"; break;
14296     case V8QImode: pattern = "%02x"; break;
14297     default:       gcc_unreachable ();
14298     }
14299
14300   fprintf (file, "0x");
14301   for (i = CONST_VECTOR_NUNITS (x); i--;)
14302     {
14303       rtx element;
14304
14305       element = CONST_VECTOR_ELT (x, i);
14306       fprintf (file, pattern, INTVAL (element));
14307     }
14308
14309   return 1;
14310 }
14311
14312 const char *
14313 arm_output_load_gr (rtx *operands)
14314 {
14315   rtx reg;
14316   rtx offset;
14317   rtx wcgr;
14318   rtx sum;
14319
14320   if (GET_CODE (operands [1]) != MEM
14321       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14322       || GET_CODE (reg = XEXP (sum, 0)) != REG
14323       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14324       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14325     return "wldrw%?\t%0, %1";
14326
14327   /* Fix up an out-of-range load of a GR register.  */
14328   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14329   wcgr = operands[0];
14330   operands[0] = reg;
14331   output_asm_insn ("ldr%?\t%0, %1", operands);
14332
14333   operands[0] = wcgr;
14334   operands[1] = reg;
14335   output_asm_insn ("tmcr%?\t%0, %1", operands);
14336   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14337
14338   return "";
14339 }
14340
14341 static rtx
14342 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14343                       int incoming ATTRIBUTE_UNUSED)
14344 {
14345 #if 0
14346   /* FIXME: The ARM backend has special code to handle structure
14347          returns, and will reserve its own hidden first argument.  So
14348          if this macro is enabled a *second* hidden argument will be
14349          reserved, which will break binary compatibility with old
14350          toolchains and also thunk handling.  One day this should be
14351          fixed.  */
14352   return 0;
14353 #else
14354   /* Register in which address to store a structure value
14355      is passed to a function.  */
14356   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14357 #endif
14358 }
14359
14360 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14361
14362    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14363    named arg and all anonymous args onto the stack.
14364    XXX I know the prologue shouldn't be pushing registers, but it is faster
14365    that way.  */
14366
14367 static void
14368 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14369                             enum machine_mode mode ATTRIBUTE_UNUSED,
14370                             tree type ATTRIBUTE_UNUSED,
14371                             int *pretend_size,
14372                             int second_time ATTRIBUTE_UNUSED)
14373 {
14374   cfun->machine->uses_anonymous_args = 1;
14375   if (cum->nregs < NUM_ARG_REGS)
14376     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14377 }
14378
14379 /* Return nonzero if the CONSUMER instruction (a store) does not need
14380    PRODUCER's value to calculate the address.  */
14381
14382 int
14383 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14384 {
14385   rtx value = PATTERN (producer);
14386   rtx addr = PATTERN (consumer);
14387
14388   if (GET_CODE (value) == COND_EXEC)
14389     value = COND_EXEC_CODE (value);
14390   if (GET_CODE (value) == PARALLEL)
14391     value = XVECEXP (value, 0, 0);
14392   value = XEXP (value, 0);
14393   if (GET_CODE (addr) == COND_EXEC)
14394     addr = COND_EXEC_CODE (addr);
14395   if (GET_CODE (addr) == PARALLEL)
14396     addr = XVECEXP (addr, 0, 0);
14397   addr = XEXP (addr, 0);
14398
14399   return !reg_overlap_mentioned_p (value, addr);
14400 }
14401
14402 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14403    have an early register shift value or amount dependency on the
14404    result of PRODUCER.  */
14405
14406 int
14407 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14408 {
14409   rtx value = PATTERN (producer);
14410   rtx op = PATTERN (consumer);
14411   rtx early_op;
14412
14413   if (GET_CODE (value) == COND_EXEC)
14414     value = COND_EXEC_CODE (value);
14415   if (GET_CODE (value) == PARALLEL)
14416     value = XVECEXP (value, 0, 0);
14417   value = XEXP (value, 0);
14418   if (GET_CODE (op) == COND_EXEC)
14419     op = COND_EXEC_CODE (op);
14420   if (GET_CODE (op) == PARALLEL)
14421     op = XVECEXP (op, 0, 0);
14422   op = XEXP (op, 1);
14423
14424   early_op = XEXP (op, 0);
14425   /* This is either an actual independent shift, or a shift applied to
14426      the first operand of another operation.  We want the whole shift
14427      operation.  */
14428   if (GET_CODE (early_op) == REG)
14429     early_op = op;
14430
14431   return !reg_overlap_mentioned_p (value, early_op);
14432 }
14433
14434 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14435    have an early register shift value dependency on the result of
14436    PRODUCER.  */
14437
14438 int
14439 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14440 {
14441   rtx value = PATTERN (producer);
14442   rtx op = PATTERN (consumer);
14443   rtx early_op;
14444
14445   if (GET_CODE (value) == COND_EXEC)
14446     value = COND_EXEC_CODE (value);
14447   if (GET_CODE (value) == PARALLEL)
14448     value = XVECEXP (value, 0, 0);
14449   value = XEXP (value, 0);
14450   if (GET_CODE (op) == COND_EXEC)
14451     op = COND_EXEC_CODE (op);
14452   if (GET_CODE (op) == PARALLEL)
14453     op = XVECEXP (op, 0, 0);
14454   op = XEXP (op, 1);
14455
14456   early_op = XEXP (op, 0);
14457
14458   /* This is either an actual independent shift, or a shift applied to
14459      the first operand of another operation.  We want the value being
14460      shifted, in either case.  */
14461   if (GET_CODE (early_op) != REG)
14462     early_op = XEXP (early_op, 0);
14463
14464   return !reg_overlap_mentioned_p (value, early_op);
14465 }
14466
14467 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14468    have an early register mult dependency on the result of
14469    PRODUCER.  */
14470
14471 int
14472 arm_no_early_mul_dep (rtx producer, rtx consumer)
14473 {
14474   rtx value = PATTERN (producer);
14475   rtx op = PATTERN (consumer);
14476
14477   if (GET_CODE (value) == COND_EXEC)
14478     value = COND_EXEC_CODE (value);
14479   if (GET_CODE (value) == PARALLEL)
14480     value = XVECEXP (value, 0, 0);
14481   value = XEXP (value, 0);
14482   if (GET_CODE (op) == COND_EXEC)
14483     op = COND_EXEC_CODE (op);
14484   if (GET_CODE (op) == PARALLEL)
14485     op = XVECEXP (op, 0, 0);
14486   op = XEXP (op, 1);
14487
14488   return (GET_CODE (op) == PLUS
14489           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14490 }
14491
14492
14493 /* We can't rely on the caller doing the proper promotion when
14494    using APCS or ATPCS.  */
14495
14496 static bool
14497 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14498 {
14499     return !TARGET_AAPCS_BASED;
14500 }
14501
14502
14503 /* AAPCS based ABIs use short enums by default.  */
14504
14505 static bool
14506 arm_default_short_enums (void)
14507 {
14508   return TARGET_AAPCS_BASED;
14509 }
14510
14511
14512 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14513
14514 static bool
14515 arm_align_anon_bitfield (void)
14516 {
14517   return TARGET_AAPCS_BASED;
14518 }
14519
14520
14521 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14522
14523 static tree
14524 arm_cxx_guard_type (void)
14525 {
14526   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14527 }
14528
14529
14530 /* The EABI says test the least significant bit of a guard variable.  */
14531
14532 static bool
14533 arm_cxx_guard_mask_bit (void)
14534 {
14535   return TARGET_AAPCS_BASED;
14536 }
14537
14538
14539 /* The EABI specifies that all array cookies are 8 bytes long.  */
14540
14541 static tree
14542 arm_get_cookie_size (tree type)
14543 {
14544   tree size;
14545
14546   if (!TARGET_AAPCS_BASED)
14547     return default_cxx_get_cookie_size (type);
14548
14549   size = build_int_cst (sizetype, 8);
14550   return size;
14551 }
14552
14553
14554 /* The EABI says that array cookies should also contain the element size.  */
14555
14556 static bool
14557 arm_cookie_has_size (void)
14558 {
14559   return TARGET_AAPCS_BASED;
14560 }
14561
14562
14563 /* The EABI says constructors and destructors should return a pointer to
14564    the object constructed/destroyed.  */
14565
14566 static bool
14567 arm_cxx_cdtor_returns_this (void)
14568 {
14569   return TARGET_AAPCS_BASED;
14570 }
14571
14572 /* The EABI says that an inline function may never be the key
14573    method.  */
14574
14575 static bool
14576 arm_cxx_key_method_may_be_inline (void)
14577 {
14578   return !TARGET_AAPCS_BASED;
14579 }
14580
14581 static void
14582 arm_cxx_determine_class_data_visibility (tree decl)
14583 {
14584   if (!TARGET_AAPCS_BASED)
14585     return;
14586
14587   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14588      is exported.  However, on systems without dynamic vague linkage,
14589      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
14590   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14591     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14592   else
14593     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14594   DECL_VISIBILITY_SPECIFIED (decl) = 1;
14595 }
14596   
14597 static bool
14598 arm_cxx_class_data_always_comdat (void)
14599 {
14600   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14601      vague linkage if the class has no key function.  */
14602   return !TARGET_AAPCS_BASED;
14603 }
14604
14605
14606 /* The EABI says __aeabi_atexit should be used to register static
14607    destructors.  */
14608
14609 static bool
14610 arm_cxx_use_aeabi_atexit (void)
14611 {
14612   return TARGET_AAPCS_BASED;
14613 }
14614
14615
14616 void
14617 arm_set_return_address (rtx source, rtx scratch)
14618 {
14619   arm_stack_offsets *offsets;
14620   HOST_WIDE_INT delta;
14621   rtx addr;
14622   unsigned long saved_regs;
14623
14624   saved_regs = arm_compute_save_reg_mask ();
14625
14626   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14627     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14628   else
14629     {
14630       if (frame_pointer_needed)
14631         addr = plus_constant(hard_frame_pointer_rtx, -4);
14632       else
14633         {
14634           /* LR will be the first saved register.  */
14635           offsets = arm_get_frame_offsets ();
14636           delta = offsets->outgoing_args - (offsets->frame + 4);
14637
14638
14639           if (delta >= 4096)
14640             {
14641               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14642                                      GEN_INT (delta & ~4095)));
14643               addr = scratch;
14644               delta &= 4095;
14645             }
14646           else
14647             addr = stack_pointer_rtx;
14648
14649           addr = plus_constant (addr, delta);
14650         }
14651       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14652     }
14653 }
14654
14655
14656 void
14657 thumb_set_return_address (rtx source, rtx scratch)
14658 {
14659   arm_stack_offsets *offsets;
14660   HOST_WIDE_INT delta;
14661   int reg;
14662   rtx addr;
14663   unsigned long mask;
14664
14665   emit_insn (gen_rtx_USE (VOIDmode, source));
14666
14667   mask = thumb_compute_save_reg_mask ();
14668   if (mask & (1 << LR_REGNUM))
14669     {
14670       offsets = arm_get_frame_offsets ();
14671
14672       /* Find the saved regs.  */
14673       if (frame_pointer_needed)
14674         {
14675           delta = offsets->soft_frame - offsets->saved_args;
14676           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14677         }
14678       else
14679         {
14680           delta = offsets->outgoing_args - offsets->saved_args;
14681           reg = SP_REGNUM;
14682         }
14683       /* Allow for the stack frame.  */
14684       if (TARGET_BACKTRACE)
14685         delta -= 16;
14686       /* The link register is always the first saved register.  */
14687       delta -= 4;
14688
14689       /* Construct the address.  */
14690       addr = gen_rtx_REG (SImode, reg);
14691       if ((reg != SP_REGNUM && delta >= 128)
14692           || delta >= 1024)
14693         {
14694           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14695           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14696           addr = scratch;
14697         }
14698       else
14699         addr = plus_constant (addr, delta);
14700
14701       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14702     }
14703   else
14704     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14705 }
14706
14707 /* Implements target hook vector_mode_supported_p.  */
14708 bool
14709 arm_vector_mode_supported_p (enum machine_mode mode)
14710 {
14711   if ((mode == V2SImode)
14712       || (mode == V4HImode)
14713       || (mode == V8QImode))
14714     return true;
14715
14716   return false;
14717 }
14718
14719 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14720    ARM insns and therefore guarantee that the shift count is modulo 256.
14721    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14722    guarantee no particular behavior for out-of-range counts.  */
14723
14724 static unsigned HOST_WIDE_INT
14725 arm_shift_truncation_mask (enum machine_mode mode)
14726 {
14727   return mode == SImode ? 255 : 0;
14728 }
14729
14730
14731 /* Map internal gcc register numbers to DWARF2 register numbers.  */
14732
14733 unsigned int
14734 arm_dbx_register_number (unsigned int regno)
14735 {
14736   if (regno < 16)
14737     return regno;
14738
14739   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14740      compatibility.  The EABI defines them as registers 96-103.  */
14741   if (IS_FPA_REGNUM (regno))
14742     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14743
14744   if (IS_VFP_REGNUM (regno))
14745     return 64 + regno - FIRST_VFP_REGNUM;
14746
14747   if (IS_IWMMXT_GR_REGNUM (regno))
14748     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14749
14750   if (IS_IWMMXT_REGNUM (regno))
14751     return 112 + regno - FIRST_IWMMXT_REGNUM;
14752
14753   gcc_unreachable ();
14754 }
14755
14756
14757 #ifdef TARGET_UNWIND_INFO
14758 /* Emit unwind directives for a store-multiple instruction.  This should
14759    only ever be generated by the function prologue code, so we expect it
14760    to have a particular form.  */
14761
14762 static void
14763 arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
14764 {
14765   int i;
14766   HOST_WIDE_INT offset;
14767   HOST_WIDE_INT nregs;
14768   int reg_size;
14769   unsigned reg;
14770   unsigned lastreg;
14771   rtx e;
14772
14773   /* First insn will adjust the stack pointer.  */
14774   e = XVECEXP (p, 0, 0);
14775   if (GET_CODE (e) != SET
14776       || GET_CODE (XEXP (e, 0)) != REG
14777       || REGNO (XEXP (e, 0)) != SP_REGNUM
14778       || GET_CODE (XEXP (e, 1)) != PLUS)
14779     abort ();
14780
14781   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
14782   nregs = XVECLEN (p, 0) - 1;
14783
14784   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
14785   if (reg < 16)
14786     {
14787       /* The function prologue may also push pc, but not annotate it as it is
14788          never restored.  We turn this into a stack pointer adjustment.  */
14789       if (nregs * 4 == offset - 4)
14790         {
14791           fprintf (asm_out_file, "\t.pad #4\n");
14792           offset -= 4;
14793         }
14794       reg_size = 4;
14795     }
14796   else if (IS_VFP_REGNUM (reg))
14797     {
14798       /* FPA register saves use an additional word.  */
14799       offset -= 4;
14800       reg_size = 8;
14801     }
14802   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
14803     {
14804       /* FPA registers are done differently.  */
14805       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
14806       return;
14807     }
14808   else
14809     /* Unknown register type.  */
14810     abort ();
14811
14812   /* If the stack increment doesn't match the size of the saved registers,
14813      something has gone horribly wrong.  */
14814   if (offset != nregs * reg_size)
14815     abort ();
14816
14817   fprintf (asm_out_file, "\t.save {");
14818
14819   offset = 0;
14820   lastreg = 0;
14821   /* The remaining insns will describe the stores.  */
14822   for (i = 1; i <= nregs; i++)
14823     {
14824       /* Expect (set (mem <addr>) (reg)).
14825          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
14826       e = XVECEXP (p, 0, i);
14827       if (GET_CODE (e) != SET
14828           || GET_CODE (XEXP (e, 0)) != MEM
14829           || GET_CODE (XEXP (e, 1)) != REG)
14830         abort ();
14831       
14832       reg = REGNO (XEXP (e, 1));
14833       if (reg < lastreg)
14834         abort ();
14835           
14836       if (i != 1)
14837         fprintf (asm_out_file, ", ");
14838       /* We can't use %r for vfp because we need to use the
14839          double precision register names.  */
14840       if (IS_VFP_REGNUM (reg))
14841         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
14842       else
14843         asm_fprintf (asm_out_file, "%r", reg);
14844
14845 #ifdef ENABLE_CHECKING
14846       /* Check that the addresses are consecutive.  */
14847       e = XEXP (XEXP (e, 0), 0);
14848       if (GET_CODE (e) == PLUS)
14849         {
14850           offset += reg_size;
14851           if (GET_CODE (XEXP (e, 0)) != REG
14852               || REGNO (XEXP (e, 0)) != SP_REGNUM
14853               || GET_CODE (XEXP (e, 1)) != CONST_INT
14854               || offset != INTVAL (XEXP (e, 1)))
14855             abort ();
14856         }
14857       else if (i != 1
14858                || GET_CODE (e) != REG
14859                || REGNO (e) != SP_REGNUM)
14860         abort ();
14861 #endif
14862     }
14863   fprintf (asm_out_file, "}\n");
14864 }
14865
14866 /*  Emit unwind directives for a SET.  */
14867
14868 static void
14869 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
14870 {
14871   rtx e0;
14872   rtx e1;
14873
14874   e0 = XEXP (p, 0);
14875   e1 = XEXP (p, 1);
14876   switch (GET_CODE (e0))
14877     {
14878     case MEM:
14879       /* Pushing a single register.  */
14880       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
14881           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
14882           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
14883         abort ();
14884
14885       asm_fprintf (asm_out_file, "\t.save ");
14886       if (IS_VFP_REGNUM (REGNO (e1)))
14887         asm_fprintf(asm_out_file, "{d%d}\n",
14888                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
14889       else
14890         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
14891       break;
14892
14893     case REG:
14894       if (REGNO (e0) == SP_REGNUM)
14895         {
14896           /* A stack increment.  */
14897           if (GET_CODE (e1) != PLUS
14898               || GET_CODE (XEXP (e1, 0)) != REG
14899               || REGNO (XEXP (e1, 0)) != SP_REGNUM
14900               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
14901             abort ();
14902
14903           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
14904                        -INTVAL (XEXP (e1, 1)));
14905         }
14906       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
14907         {
14908           HOST_WIDE_INT offset;
14909           unsigned reg;
14910           
14911           if (GET_CODE (e1) == PLUS)
14912             {
14913               if (GET_CODE (XEXP (e1, 0)) != REG
14914                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
14915                 abort ();
14916               reg = REGNO (XEXP (e1, 0));
14917               offset = INTVAL (XEXP (e1, 1));
14918               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
14919                            HARD_FRAME_POINTER_REGNUM, reg,
14920                            INTVAL (XEXP (e1, 1)));
14921             }
14922           else if (GET_CODE (e1) == REG)
14923             {
14924               reg = REGNO (e1);
14925               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
14926                            HARD_FRAME_POINTER_REGNUM, reg);
14927             }
14928           else
14929             abort ();
14930         }
14931       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
14932         {
14933           /* Move from sp to reg.  */
14934           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
14935         }
14936       else
14937         abort ();
14938       break;
14939
14940     default:
14941       abort ();
14942     }
14943 }
14944
14945
14946 /* Emit unwind directives for the given insn.  */
14947
14948 static void
14949 arm_unwind_emit (FILE * asm_out_file, rtx insn)
14950 {
14951   rtx pat;
14952
14953   if (!ARM_EABI_UNWIND_TABLES)
14954     return;
14955
14956   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
14957     return;
14958
14959   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
14960   if (pat)
14961     pat = XEXP (pat, 0);
14962   else
14963     pat = PATTERN (insn);
14964
14965   switch (GET_CODE (pat))
14966     {
14967     case SET:
14968       arm_unwind_emit_set (asm_out_file, pat);
14969       break;
14970
14971     case SEQUENCE:
14972       /* Store multiple.  */
14973       arm_unwind_emit_stm (asm_out_file, pat);
14974       break;
14975
14976     default:
14977       abort();
14978     }
14979 }
14980
14981
14982 /* Output a reference from a function exception table to the type_info
14983    object X.  The EABI specifies that the symbol should be relocated by
14984    an R_ARM_TARGET2 relocation.  */
14985
14986 static bool
14987 arm_output_ttype (rtx x)
14988 {
14989   fputs ("\t.word\t", asm_out_file);
14990   output_addr_const (asm_out_file, x);
14991   /* Use special relocations for symbol references.  */
14992   if (GET_CODE (x) != CONST_INT)
14993     fputs ("(TARGET2)", asm_out_file);
14994   fputc ('\n', asm_out_file);
14995
14996   return TRUE;
14997 }
14998 #endif /* TARGET_UNWIND_INFO */
14999
15000
15001 /* Output unwind directives for the start/end of a function.  */
15002
15003 void
15004 arm_output_fn_unwind (FILE * f, bool prologue)
15005 {
15006   if (!ARM_EABI_UNWIND_TABLES)
15007     return;
15008
15009   if (prologue)
15010     fputs ("\t.fnstart\n", f);
15011   else
15012     fputs ("\t.fnend\n", f);
15013 }