OSDN Git Service

2007-02-21 Kyle Galloway <kgallowa@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007  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 thumb2_legitimate_index_p (enum machine_mode, rtx, int);
71 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
72 inline static int thumb1_index_register_rtx_p (rtx, int);
73 static int thumb_far_jump_used_p (void);
74 static bool thumb_force_lr_save (void);
75 static unsigned long thumb1_compute_save_reg_mask (void);
76 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
77 static rtx emit_sfm (int, int);
78 static int arm_size_return_regs (void);
79 #ifndef AOF_ASSEMBLER
80 static bool arm_assemble_integer (rtx, unsigned int, int);
81 #endif
82 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
83 static arm_cc get_arm_condition_code (rtx);
84 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
85 static rtx is_jump_table (rtx);
86 static const char *output_multi_immediate (rtx *, const char *, const char *,
87                                            int, HOST_WIDE_INT);
88 static const char *shift_op (rtx, HOST_WIDE_INT *);
89 static struct machine_function *arm_init_machine_status (void);
90 static void thumb_exit (FILE *, int);
91 static rtx is_jump_table (rtx);
92 static HOST_WIDE_INT get_jump_table_size (rtx);
93 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_forward_ref (Mfix *);
95 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96 static Mnode *add_minipool_backward_ref (Mfix *);
97 static void assign_minipool_offsets (Mfix *);
98 static void arm_print_value (FILE *, rtx);
99 static void dump_minipool (rtx);
100 static int arm_barrier_cost (rtx);
101 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104                                rtx);
105 static void arm_reorg (void);
106 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
107 static int current_file_function_operand (rtx);
108 static unsigned long arm_compute_save_reg0_reg12_mask (void);
109 static unsigned long arm_compute_save_reg_mask (void);
110 static unsigned long arm_isr_value (tree);
111 static unsigned long arm_compute_func_type (void);
112 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
113 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
114 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
115 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
116 #endif
117 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
118 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
119 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
120 static int arm_comp_type_attributes (tree, tree);
121 static void arm_set_default_type_attributes (tree);
122 static int arm_adjust_cost (rtx, rtx, rtx, int);
123 static int count_insns_for_constant (HOST_WIDE_INT, int);
124 static int arm_get_strip_length (int);
125 static bool arm_function_ok_for_sibcall (tree, tree);
126 static void arm_internal_label (FILE *, const char *, unsigned long);
127 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
128                                  tree);
129 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
130 static bool arm_size_rtx_costs (rtx, int, int, int *);
131 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
132 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
133 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
134 static bool arm_9e_rtx_costs (rtx, int, int, int *);
135 static int arm_address_cost (rtx);
136 static bool arm_memory_load_p (rtx);
137 static bool arm_cirrus_insn_p (rtx);
138 static void cirrus_reorg (rtx);
139 static void arm_init_builtins (void);
140 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
141 static void arm_init_iwmmxt_builtins (void);
142 static rtx safe_vector_operand (rtx, enum machine_mode);
143 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
144 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
145 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
146 static void emit_constant_insn (rtx cond, rtx pattern);
147 static rtx emit_set_insn (rtx, rtx);
148 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
149                                   tree, bool);
150
151 #ifdef OBJECT_FORMAT_ELF
152 static void arm_elf_asm_constructor (rtx, int);
153 #endif
154 #ifndef ARM_PE
155 static void arm_encode_section_info (tree, rtx, int);
156 #endif
157
158 static void arm_file_end (void);
159 static void arm_file_start (void);
160
161 #ifdef AOF_ASSEMBLER
162 static void aof_globalize_label (FILE *, const char *);
163 static void aof_dump_imports (FILE *);
164 static void aof_dump_pic_table (FILE *);
165 static void aof_file_start (void);
166 static void aof_file_end (void);
167 static void aof_asm_init_sections (void);
168 #endif
169 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
170                                         tree, int *, int);
171 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
172                                    enum machine_mode, tree, bool);
173 static bool arm_promote_prototypes (tree);
174 static bool arm_default_short_enums (void);
175 static bool arm_align_anon_bitfield (void);
176 static bool arm_return_in_msb (tree);
177 static bool arm_must_pass_in_stack (enum machine_mode, tree);
178 #ifdef TARGET_UNWIND_INFO
179 static void arm_unwind_emit (FILE *, rtx);
180 static bool arm_output_ttype (rtx);
181 #endif
182 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
183
184 static tree arm_cxx_guard_type (void);
185 static bool arm_cxx_guard_mask_bit (void);
186 static tree arm_get_cookie_size (tree);
187 static bool arm_cookie_has_size (void);
188 static bool arm_cxx_cdtor_returns_this (void);
189 static bool arm_cxx_key_method_may_be_inline (void);
190 static void arm_cxx_determine_class_data_visibility (tree);
191 static bool arm_cxx_class_data_always_comdat (void);
192 static bool arm_cxx_use_aeabi_atexit (void);
193 static void arm_init_libfuncs (void);
194 static bool arm_handle_option (size_t, const char *, int);
195 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
196 static bool arm_cannot_copy_insn_p (rtx);
197 static bool arm_tls_symbol_p (rtx x);
198
199 \f
200 /* Initialize the GCC target structure.  */
201 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
202 #undef  TARGET_MERGE_DECL_ATTRIBUTES
203 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
204 #endif
205
206 #undef  TARGET_ATTRIBUTE_TABLE
207 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
208
209 #undef TARGET_ASM_FILE_START
210 #define TARGET_ASM_FILE_START arm_file_start
211 #undef TARGET_ASM_FILE_END
212 #define TARGET_ASM_FILE_END arm_file_end
213
214 #ifdef AOF_ASSEMBLER
215 #undef  TARGET_ASM_BYTE_OP
216 #define TARGET_ASM_BYTE_OP "\tDCB\t"
217 #undef  TARGET_ASM_ALIGNED_HI_OP
218 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
219 #undef  TARGET_ASM_ALIGNED_SI_OP
220 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
221 #undef TARGET_ASM_GLOBALIZE_LABEL
222 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
223 #undef TARGET_ASM_FILE_START
224 #define TARGET_ASM_FILE_START aof_file_start
225 #undef TARGET_ASM_FILE_END
226 #define TARGET_ASM_FILE_END aof_file_end
227 #else
228 #undef  TARGET_ASM_ALIGNED_SI_OP
229 #define TARGET_ASM_ALIGNED_SI_OP NULL
230 #undef  TARGET_ASM_INTEGER
231 #define TARGET_ASM_INTEGER arm_assemble_integer
232 #endif
233
234 #undef  TARGET_ASM_FUNCTION_PROLOGUE
235 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
236
237 #undef  TARGET_ASM_FUNCTION_EPILOGUE
238 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
239
240 #undef  TARGET_DEFAULT_TARGET_FLAGS
241 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
242 #undef  TARGET_HANDLE_OPTION
243 #define TARGET_HANDLE_OPTION arm_handle_option
244
245 #undef  TARGET_COMP_TYPE_ATTRIBUTES
246 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
247
248 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
249 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
250
251 #undef  TARGET_SCHED_ADJUST_COST
252 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
253
254 #undef TARGET_ENCODE_SECTION_INFO
255 #ifdef ARM_PE
256 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
257 #else
258 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
259 #endif
260
261 #undef  TARGET_STRIP_NAME_ENCODING
262 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
263
264 #undef  TARGET_ASM_INTERNAL_LABEL
265 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
266
267 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
268 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
269
270 #undef  TARGET_ASM_OUTPUT_MI_THUNK
271 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
272 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
273 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
274
275 /* This will be overridden in arm_override_options.  */
276 #undef  TARGET_RTX_COSTS
277 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
278 #undef  TARGET_ADDRESS_COST
279 #define TARGET_ADDRESS_COST arm_address_cost
280
281 #undef TARGET_SHIFT_TRUNCATION_MASK
282 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
283 #undef TARGET_VECTOR_MODE_SUPPORTED_P
284 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
285
286 #undef  TARGET_MACHINE_DEPENDENT_REORG
287 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
288
289 #undef  TARGET_INIT_BUILTINS
290 #define TARGET_INIT_BUILTINS  arm_init_builtins
291 #undef  TARGET_EXPAND_BUILTIN
292 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
293
294 #undef TARGET_INIT_LIBFUNCS
295 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
296
297 #undef TARGET_PROMOTE_FUNCTION_ARGS
298 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
299 #undef TARGET_PROMOTE_FUNCTION_RETURN
300 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
301 #undef TARGET_PROMOTE_PROTOTYPES
302 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
303 #undef TARGET_PASS_BY_REFERENCE
304 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
305 #undef TARGET_ARG_PARTIAL_BYTES
306 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
307
308 #undef  TARGET_SETUP_INCOMING_VARARGS
309 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
310
311 #undef TARGET_DEFAULT_SHORT_ENUMS
312 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
313
314 #undef TARGET_ALIGN_ANON_BITFIELD
315 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
316
317 #undef TARGET_NARROW_VOLATILE_BITFIELD
318 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
319
320 #undef TARGET_CXX_GUARD_TYPE
321 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
322
323 #undef TARGET_CXX_GUARD_MASK_BIT
324 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
325
326 #undef TARGET_CXX_GET_COOKIE_SIZE
327 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
328
329 #undef TARGET_CXX_COOKIE_HAS_SIZE
330 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
331
332 #undef TARGET_CXX_CDTOR_RETURNS_THIS
333 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
334
335 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
336 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
337
338 #undef TARGET_CXX_USE_AEABI_ATEXIT
339 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
340
341 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
342 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
343   arm_cxx_determine_class_data_visibility
344
345 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
346 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
347
348 #undef TARGET_RETURN_IN_MSB
349 #define TARGET_RETURN_IN_MSB arm_return_in_msb
350
351 #undef TARGET_MUST_PASS_IN_STACK
352 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
353
354 #ifdef TARGET_UNWIND_INFO
355 #undef TARGET_UNWIND_EMIT
356 #define TARGET_UNWIND_EMIT arm_unwind_emit
357
358 /* EABI unwinding tables use a different format for the typeinfo tables.  */
359 #undef TARGET_ASM_TTYPE
360 #define TARGET_ASM_TTYPE arm_output_ttype
361
362 #undef TARGET_ARM_EABI_UNWINDER
363 #define TARGET_ARM_EABI_UNWINDER true
364 #endif /* TARGET_UNWIND_INFO */
365
366 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
367 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
368
369 #undef  TARGET_CANNOT_COPY_INSN_P
370 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
371
372 #ifdef HAVE_AS_TLS
373 #undef TARGET_HAVE_TLS
374 #define TARGET_HAVE_TLS true
375 #endif
376
377 #undef TARGET_CANNOT_FORCE_CONST_MEM
378 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
379
380 struct gcc_target targetm = TARGET_INITIALIZER;
381 \f
382 /* Obstack for minipool constant handling.  */
383 static struct obstack minipool_obstack;
384 static char *         minipool_startobj;
385
386 /* The maximum number of insns skipped which
387    will be conditionalised if possible.  */
388 static int max_insns_skipped = 5;
389
390 extern FILE * asm_out_file;
391
392 /* True if we are currently building a constant table.  */
393 int making_const_table;
394
395 /* Define the information needed to generate branch insns.  This is
396    stored from the compare operation.  */
397 rtx arm_compare_op0, arm_compare_op1;
398
399 /* The processor for which instructions should be scheduled.  */
400 enum processor_type arm_tune = arm_none;
401
402 /* The default processor used if not overridden by commandline.  */
403 static enum processor_type arm_default_cpu = arm_none;
404
405 /* Which floating point model to use.  */
406 enum arm_fp_model arm_fp_model;
407
408 /* Which floating point hardware is available.  */
409 enum fputype arm_fpu_arch;
410
411 /* Which floating point hardware to schedule for.  */
412 enum fputype arm_fpu_tune;
413
414 /* Whether to use floating point hardware.  */
415 enum float_abi_type arm_float_abi;
416
417 /* Which ABI to use.  */
418 enum arm_abi_type arm_abi;
419
420 /* Which thread pointer model to use.  */
421 enum arm_tp_type target_thread_pointer = TP_AUTO;
422
423 /* Used to parse -mstructure_size_boundary command line option.  */
424 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
425
426 /* Used for Thumb call_via trampolines.  */
427 rtx thumb_call_via_label[14];
428 static int thumb_call_reg_needed;
429
430 /* Bit values used to identify processor capabilities.  */
431 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
432 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
433 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
434 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
435 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
436 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
437 #define FL_THUMB      (1 << 6)        /* Thumb aware */
438 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
439 #define FL_STRONG     (1 << 8)        /* StrongARM */
440 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
441 #define FL_XSCALE     (1 << 10)       /* XScale */
442 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
443 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
444                                          media instructions.  */
445 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
446 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
447                                          Note: ARM6 & 7 derivatives only.  */
448 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
449 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
450 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
451                                          profile.  */
452 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
453
454 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
455
456 #define FL_FOR_ARCH2    FL_NOTM
457 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
458 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
459 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
460 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
461 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
462 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
463 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
464 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
465 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
466 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
467 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
468 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
469 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
470 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
471 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
472 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
473 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
474 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
475 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
476
477 /* The bits in this mask specify which
478    instructions we are allowed to generate.  */
479 static unsigned long insn_flags = 0;
480
481 /* The bits in this mask specify which instruction scheduling options should
482    be used.  */
483 static unsigned long tune_flags = 0;
484
485 /* The following are used in the arm.md file as equivalents to bits
486    in the above two flag variables.  */
487
488 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
489 int arm_arch3m = 0;
490
491 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
492 int arm_arch4 = 0;
493
494 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
495 int arm_arch4t = 0;
496
497 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
498 int arm_arch5 = 0;
499
500 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
501 int arm_arch5e = 0;
502
503 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
504 int arm_arch6 = 0;
505
506 /* Nonzero if this chip supports the ARM 6K extensions.  */
507 int arm_arch6k = 0;
508
509 /* Nonzero if instructions not present in the 'M' profile can be used.  */
510 int arm_arch_notm = 0;
511
512 /* Nonzero if this chip can benefit from load scheduling.  */
513 int arm_ld_sched = 0;
514
515 /* Nonzero if this chip is a StrongARM.  */
516 int arm_tune_strongarm = 0;
517
518 /* Nonzero if this chip is a Cirrus variant.  */
519 int arm_arch_cirrus = 0;
520
521 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
522 int arm_arch_iwmmxt = 0;
523
524 /* Nonzero if this chip is an XScale.  */
525 int arm_arch_xscale = 0;
526
527 /* Nonzero if tuning for XScale  */
528 int arm_tune_xscale = 0;
529
530 /* Nonzero if we want to tune for stores that access the write-buffer.
531    This typically means an ARM6 or ARM7 with MMU or MPU.  */
532 int arm_tune_wbuf = 0;
533
534 /* Nonzero if generating Thumb instructions.  */
535 int thumb_code = 0;
536
537 /* Nonzero if we should define __THUMB_INTERWORK__ in the
538    preprocessor.
539    XXX This is a bit of a hack, it's intended to help work around
540    problems in GLD which doesn't understand that armv5t code is
541    interworking clean.  */
542 int arm_cpp_interwork = 0;
543
544 /* Nonzero if chip supports Thumb 2.  */
545 int arm_arch_thumb2;
546
547 /* Nonzero if chip supports integer division instruction.  */
548 int arm_arch_hwdiv;
549
550 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
551    must report the mode of the memory reference from PRINT_OPERAND to
552    PRINT_OPERAND_ADDRESS.  */
553 enum machine_mode output_memory_reference_mode;
554
555 /* The register number to be used for the PIC offset register.  */
556 unsigned arm_pic_register = INVALID_REGNUM;
557
558 /* Set to 1 when a return insn is output, this means that the epilogue
559    is not needed.  */
560 int return_used_this_function;
561
562 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
563    the next function.  */
564 static int after_arm_reorg = 0;
565
566 /* The maximum number of insns to be used when loading a constant.  */
567 static int arm_constant_limit = 3;
568
569 /* For an explanation of these variables, see final_prescan_insn below.  */
570 int arm_ccfsm_state;
571 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
572 enum arm_cond_code arm_current_cc;
573 rtx arm_target_insn;
574 int arm_target_label;
575 /* The number of conditionally executed insns, including the current insn.  */
576 int arm_condexec_count = 0;
577 /* A bitmask specifying the patterns for the IT block.
578    Zero means do not output an IT block before this insn. */
579 int arm_condexec_mask = 0;
580 /* The number of bits used in arm_condexec_mask.  */
581 int arm_condexec_masklen = 0;
582
583 /* The condition codes of the ARM, and the inverse function.  */
584 static const char * const arm_condition_codes[] =
585 {
586   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
587   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
588 };
589
590 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
591 #define streq(string1, string2) (strcmp (string1, string2) == 0)
592
593 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
594                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
595                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
596 \f
597 /* Initialization code.  */
598
599 struct processors
600 {
601   const char *const name;
602   enum processor_type core;
603   const char *arch;
604   const unsigned long flags;
605   bool (* rtx_costs) (rtx, int, int, int *);
606 };
607
608 /* Not all of these give usefully different compilation alternatives,
609    but there is no simple way of generalizing them.  */
610 static const struct processors all_cores[] =
611 {
612   /* ARM Cores */
613 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
614   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
615 #include "arm-cores.def"
616 #undef ARM_CORE
617   {NULL, arm_none, NULL, 0, NULL}
618 };
619
620 static const struct processors all_architectures[] =
621 {
622   /* ARM Architectures */
623   /* We don't specify rtx_costs here as it will be figured out
624      from the core.  */
625
626   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
627   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
628   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
629   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
630   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
631   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
632      implementations that support it, so we will leave it out for now.  */
633   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
634   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
635   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
636   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
637   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
638   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
639   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
640   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
641   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
642   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
643   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
644   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
645   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
646   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
647   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
648   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
649   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
650   {NULL, arm_none, NULL, 0 , NULL}
651 };
652
653 struct arm_cpu_select
654 {
655   const char *              string;
656   const char *              name;
657   const struct processors * processors;
658 };
659
660 /* This is a magic structure.  The 'string' field is magically filled in
661    with a pointer to the value specified by the user on the command line
662    assuming that the user has specified such a value.  */
663
664 static struct arm_cpu_select arm_select[] =
665 {
666   /* string       name            processors  */
667   { NULL,       "-mcpu=",       all_cores  },
668   { NULL,       "-march=",      all_architectures },
669   { NULL,       "-mtune=",      all_cores }
670 };
671
672 /* Defines representing the indexes into the above table.  */
673 #define ARM_OPT_SET_CPU 0
674 #define ARM_OPT_SET_ARCH 1
675 #define ARM_OPT_SET_TUNE 2
676
677 /* The name of the preprocessor macro to define for this architecture.  */
678
679 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
680
681 struct fpu_desc
682 {
683   const char * name;
684   enum fputype fpu;
685 };
686
687
688 /* Available values for -mfpu=.  */
689
690 static const struct fpu_desc all_fpus[] =
691 {
692   {"fpa",       FPUTYPE_FPA},
693   {"fpe2",      FPUTYPE_FPA_EMU2},
694   {"fpe3",      FPUTYPE_FPA_EMU2},
695   {"maverick",  FPUTYPE_MAVERICK},
696   {"vfp",       FPUTYPE_VFP}
697 };
698
699
700 /* Floating point models used by the different hardware.
701    See fputype in arm.h.  */
702
703 static const enum fputype fp_model_for_fpu[] =
704 {
705   /* No FP hardware.  */
706   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
707   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
708   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
709   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
710   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
711   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
712 };
713
714
715 struct float_abi
716 {
717   const char * name;
718   enum float_abi_type abi_type;
719 };
720
721
722 /* Available values for -mfloat-abi=.  */
723
724 static const struct float_abi all_float_abis[] =
725 {
726   {"soft",      ARM_FLOAT_ABI_SOFT},
727   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
728   {"hard",      ARM_FLOAT_ABI_HARD}
729 };
730
731
732 struct abi_name
733 {
734   const char *name;
735   enum arm_abi_type abi_type;
736 };
737
738
739 /* Available values for -mabi=.  */
740
741 static const struct abi_name arm_all_abis[] =
742 {
743   {"apcs-gnu",    ARM_ABI_APCS},
744   {"atpcs",   ARM_ABI_ATPCS},
745   {"aapcs",   ARM_ABI_AAPCS},
746   {"iwmmxt",  ARM_ABI_IWMMXT},
747   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
748 };
749
750 /* Supported TLS relocations.  */
751
752 enum tls_reloc {
753   TLS_GD32,
754   TLS_LDM32,
755   TLS_LDO32,
756   TLS_IE32,
757   TLS_LE32
758 };
759
760 /* Emit an insn that's a simple single-set.  Both the operands must be known
761    to be valid.  */
762 inline static rtx
763 emit_set_insn (rtx x, rtx y)
764 {
765   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
766 }
767
768 /* Return the number of bits set in VALUE.  */
769 static unsigned
770 bit_count (unsigned long value)
771 {
772   unsigned long count = 0;
773
774   while (value)
775     {
776       count++;
777       value &= value - 1;  /* Clear the least-significant set bit.  */
778     }
779
780   return count;
781 }
782
783 /* Set up library functions unique to ARM.  */
784
785 static void
786 arm_init_libfuncs (void)
787 {
788   /* There are no special library functions unless we are using the
789      ARM BPABI.  */
790   if (!TARGET_BPABI)
791     return;
792
793   /* The functions below are described in Section 4 of the "Run-Time
794      ABI for the ARM architecture", Version 1.0.  */
795
796   /* Double-precision floating-point arithmetic.  Table 2.  */
797   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
798   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
799   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
800   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
801   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
802
803   /* Double-precision comparisons.  Table 3.  */
804   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
805   set_optab_libfunc (ne_optab, DFmode, NULL);
806   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
807   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
808   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
809   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
810   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
811
812   /* Single-precision floating-point arithmetic.  Table 4.  */
813   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
814   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
815   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
816   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
817   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
818
819   /* Single-precision comparisons.  Table 5.  */
820   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
821   set_optab_libfunc (ne_optab, SFmode, NULL);
822   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
823   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
824   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
825   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
826   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
827
828   /* Floating-point to integer conversions.  Table 6.  */
829   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
830   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
831   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
832   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
833   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
834   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
835   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
836   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
837
838   /* Conversions between floating types.  Table 7.  */
839   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
840   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
841
842   /* Integer to floating-point conversions.  Table 8.  */
843   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
844   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
845   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
846   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
847   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
848   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
849   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
850   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
851
852   /* Long long.  Table 9.  */
853   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
854   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
855   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
856   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
857   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
858   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
859   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
860   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
861
862   /* Integer (32/32->32) division.  \S 4.3.1.  */
863   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
864   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
865
866   /* The divmod functions are designed so that they can be used for
867      plain division, even though they return both the quotient and the
868      remainder.  The quotient is returned in the usual location (i.e.,
869      r0 for SImode, {r0, r1} for DImode), just as would be expected
870      for an ordinary division routine.  Because the AAPCS calling
871      conventions specify that all of { r0, r1, r2, r3 } are
872      callee-saved registers, there is no need to tell the compiler
873      explicitly that those registers are clobbered by these
874      routines.  */
875   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
876   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
877
878   /* For SImode division the ABI provides div-without-mod routines,
879      which are faster.  */
880   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
881   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
882
883   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
884      divmod libcalls instead.  */
885   set_optab_libfunc (smod_optab, DImode, NULL);
886   set_optab_libfunc (umod_optab, DImode, NULL);
887   set_optab_libfunc (smod_optab, SImode, NULL);
888   set_optab_libfunc (umod_optab, SImode, NULL);
889 }
890
891 /* Implement TARGET_HANDLE_OPTION.  */
892
893 static bool
894 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
895 {
896   switch (code)
897     {
898     case OPT_march_:
899       arm_select[1].string = arg;
900       return true;
901
902     case OPT_mcpu_:
903       arm_select[0].string = arg;
904       return true;
905
906     case OPT_mhard_float:
907       target_float_abi_name = "hard";
908       return true;
909
910     case OPT_msoft_float:
911       target_float_abi_name = "soft";
912       return true;
913
914     case OPT_mtune_:
915       arm_select[2].string = arg;
916       return true;
917
918     default:
919       return true;
920     }
921 }
922
923 /* Fix up any incompatible options that the user has specified.
924    This has now turned into a maze.  */
925 void
926 arm_override_options (void)
927 {
928   unsigned i;
929   enum processor_type target_arch_cpu = arm_none;
930
931   /* Set up the flags based on the cpu/architecture selected by the user.  */
932   for (i = ARRAY_SIZE (arm_select); i--;)
933     {
934       struct arm_cpu_select * ptr = arm_select + i;
935
936       if (ptr->string != NULL && ptr->string[0] != '\0')
937         {
938           const struct processors * sel;
939
940           for (sel = ptr->processors; sel->name != NULL; sel++)
941             if (streq (ptr->string, sel->name))
942               {
943                 /* Set the architecture define.  */
944                 if (i != ARM_OPT_SET_TUNE)
945                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
946
947                 /* Determine the processor core for which we should
948                    tune code-generation.  */
949                 if (/* -mcpu= is a sensible default.  */
950                     i == ARM_OPT_SET_CPU
951                     /* -mtune= overrides -mcpu= and -march=.  */
952                     || i == ARM_OPT_SET_TUNE)
953                   arm_tune = (enum processor_type) (sel - ptr->processors);
954
955                 /* Remember the CPU associated with this architecture.
956                    If no other option is used to set the CPU type,
957                    we'll use this to guess the most suitable tuning
958                    options.  */
959                 if (i == ARM_OPT_SET_ARCH)
960                   target_arch_cpu = sel->core;
961
962                 if (i != ARM_OPT_SET_TUNE)
963                   {
964                     /* If we have been given an architecture and a processor
965                        make sure that they are compatible.  We only generate
966                        a warning though, and we prefer the CPU over the
967                        architecture.  */
968                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
969                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
970                                ptr->string);
971
972                     insn_flags = sel->flags;
973                   }
974
975                 break;
976               }
977
978           if (sel->name == NULL)
979             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
980         }
981     }
982
983   /* Guess the tuning options from the architecture if necessary.  */
984   if (arm_tune == arm_none)
985     arm_tune = target_arch_cpu;
986
987   /* If the user did not specify a processor, choose one for them.  */
988   if (insn_flags == 0)
989     {
990       const struct processors * sel;
991       unsigned int        sought;
992       enum processor_type cpu;
993
994       cpu = TARGET_CPU_DEFAULT;
995       if (cpu == arm_none)
996         {
997 #ifdef SUBTARGET_CPU_DEFAULT
998           /* Use the subtarget default CPU if none was specified by
999              configure.  */
1000           cpu = SUBTARGET_CPU_DEFAULT;
1001 #endif
1002           /* Default to ARM6.  */
1003           if (cpu == arm_none)
1004             cpu = arm6;
1005         }
1006       sel = &all_cores[cpu];
1007
1008       insn_flags = sel->flags;
1009
1010       /* Now check to see if the user has specified some command line
1011          switch that require certain abilities from the cpu.  */
1012       sought = 0;
1013
1014       if (TARGET_INTERWORK || TARGET_THUMB)
1015         {
1016           sought |= (FL_THUMB | FL_MODE32);
1017
1018           /* There are no ARM processors that support both APCS-26 and
1019              interworking.  Therefore we force FL_MODE26 to be removed
1020              from insn_flags here (if it was set), so that the search
1021              below will always be able to find a compatible processor.  */
1022           insn_flags &= ~FL_MODE26;
1023         }
1024
1025       if (sought != 0 && ((sought & insn_flags) != sought))
1026         {
1027           /* Try to locate a CPU type that supports all of the abilities
1028              of the default CPU, plus the extra abilities requested by
1029              the user.  */
1030           for (sel = all_cores; sel->name != NULL; sel++)
1031             if ((sel->flags & sought) == (sought | insn_flags))
1032               break;
1033
1034           if (sel->name == NULL)
1035             {
1036               unsigned current_bit_count = 0;
1037               const struct processors * best_fit = NULL;
1038
1039               /* Ideally we would like to issue an error message here
1040                  saying that it was not possible to find a CPU compatible
1041                  with the default CPU, but which also supports the command
1042                  line options specified by the programmer, and so they
1043                  ought to use the -mcpu=<name> command line option to
1044                  override the default CPU type.
1045
1046                  If we cannot find a cpu that has both the
1047                  characteristics of the default cpu and the given
1048                  command line options we scan the array again looking
1049                  for a best match.  */
1050               for (sel = all_cores; sel->name != NULL; sel++)
1051                 if ((sel->flags & sought) == sought)
1052                   {
1053                     unsigned count;
1054
1055                     count = bit_count (sel->flags & insn_flags);
1056
1057                     if (count >= current_bit_count)
1058                       {
1059                         best_fit = sel;
1060                         current_bit_count = count;
1061                       }
1062                   }
1063
1064               gcc_assert (best_fit);
1065               sel = best_fit;
1066             }
1067
1068           insn_flags = sel->flags;
1069         }
1070       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1071       arm_default_cpu = (enum processor_type) (sel - all_cores);
1072       if (arm_tune == arm_none)
1073         arm_tune = arm_default_cpu;
1074     }
1075
1076   /* The processor for which we should tune should now have been
1077      chosen.  */
1078   gcc_assert (arm_tune != arm_none);
1079
1080   tune_flags = all_cores[(int)arm_tune].flags;
1081   if (optimize_size)
1082     targetm.rtx_costs = arm_size_rtx_costs;
1083   else
1084     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1085
1086   /* Make sure that the processor choice does not conflict with any of the
1087      other command line choices.  */
1088   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1089     error ("target CPU does not support ARM mode");
1090
1091   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1092     {
1093       warning (0, "target CPU does not support interworking" );
1094       target_flags &= ~MASK_INTERWORK;
1095     }
1096
1097   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1098     {
1099       warning (0, "target CPU does not support THUMB instructions");
1100       target_flags &= ~MASK_THUMB;
1101     }
1102
1103   if (TARGET_APCS_FRAME && TARGET_THUMB)
1104     {
1105       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1106       target_flags &= ~MASK_APCS_FRAME;
1107     }
1108
1109   /* Callee super interworking implies thumb interworking.  Adding
1110      this to the flags here simplifies the logic elsewhere.  */
1111   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1112       target_flags |= MASK_INTERWORK;
1113
1114   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1115      from here where no function is being compiled currently.  */
1116   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1117     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1118
1119   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1120     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1121
1122   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1123     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1124
1125   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1126     {
1127       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1128       target_flags |= MASK_APCS_FRAME;
1129     }
1130
1131   if (TARGET_POKE_FUNCTION_NAME)
1132     target_flags |= MASK_APCS_FRAME;
1133
1134   if (TARGET_APCS_REENT && flag_pic)
1135     error ("-fpic and -mapcs-reent are incompatible");
1136
1137   if (TARGET_APCS_REENT)
1138     warning (0, "APCS reentrant code not supported.  Ignored");
1139
1140   /* If this target is normally configured to use APCS frames, warn if they
1141      are turned off and debugging is turned on.  */
1142   if (TARGET_ARM
1143       && write_symbols != NO_DEBUG
1144       && !TARGET_APCS_FRAME
1145       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1146     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1147
1148   if (TARGET_APCS_FLOAT)
1149     warning (0, "passing floating point arguments in fp regs not yet supported");
1150
1151   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1152   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1153   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1154   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1155   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1156   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1157   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1158   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1159   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1160   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1161   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1162   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1163
1164   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1165   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1166   thumb_code = (TARGET_ARM == 0);
1167   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1168   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1169   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1170   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1171
1172   /* V5 code we generate is completely interworking capable, so we turn off
1173      TARGET_INTERWORK here to avoid many tests later on.  */
1174
1175   /* XXX However, we must pass the right pre-processor defines to CPP
1176      or GLD can get confused.  This is a hack.  */
1177   if (TARGET_INTERWORK)
1178     arm_cpp_interwork = 1;
1179
1180   if (arm_arch5)
1181     target_flags &= ~MASK_INTERWORK;
1182
1183   if (target_abi_name)
1184     {
1185       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1186         {
1187           if (streq (arm_all_abis[i].name, target_abi_name))
1188             {
1189               arm_abi = arm_all_abis[i].abi_type;
1190               break;
1191             }
1192         }
1193       if (i == ARRAY_SIZE (arm_all_abis))
1194         error ("invalid ABI option: -mabi=%s", target_abi_name);
1195     }
1196   else
1197     arm_abi = ARM_DEFAULT_ABI;
1198
1199   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1200     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1201
1202   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1203     error ("iwmmxt abi requires an iwmmxt capable cpu");
1204
1205   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1206   if (target_fpu_name == NULL && target_fpe_name != NULL)
1207     {
1208       if (streq (target_fpe_name, "2"))
1209         target_fpu_name = "fpe2";
1210       else if (streq (target_fpe_name, "3"))
1211         target_fpu_name = "fpe3";
1212       else
1213         error ("invalid floating point emulation option: -mfpe=%s",
1214                target_fpe_name);
1215     }
1216   if (target_fpu_name != NULL)
1217     {
1218       /* The user specified a FPU.  */
1219       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1220         {
1221           if (streq (all_fpus[i].name, target_fpu_name))
1222             {
1223               arm_fpu_arch = all_fpus[i].fpu;
1224               arm_fpu_tune = arm_fpu_arch;
1225               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1226               break;
1227             }
1228         }
1229       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1230         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1231     }
1232   else
1233     {
1234 #ifdef FPUTYPE_DEFAULT
1235       /* Use the default if it is specified for this platform.  */
1236       arm_fpu_arch = FPUTYPE_DEFAULT;
1237       arm_fpu_tune = FPUTYPE_DEFAULT;
1238 #else
1239       /* Pick one based on CPU type.  */
1240       /* ??? Some targets assume FPA is the default.
1241       if ((insn_flags & FL_VFP) != 0)
1242         arm_fpu_arch = FPUTYPE_VFP;
1243       else
1244       */
1245       if (arm_arch_cirrus)
1246         arm_fpu_arch = FPUTYPE_MAVERICK;
1247       else
1248         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1249 #endif
1250       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1251         arm_fpu_tune = FPUTYPE_FPA;
1252       else
1253         arm_fpu_tune = arm_fpu_arch;
1254       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1255       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1256     }
1257
1258   if (target_float_abi_name != NULL)
1259     {
1260       /* The user specified a FP ABI.  */
1261       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1262         {
1263           if (streq (all_float_abis[i].name, target_float_abi_name))
1264             {
1265               arm_float_abi = all_float_abis[i].abi_type;
1266               break;
1267             }
1268         }
1269       if (i == ARRAY_SIZE (all_float_abis))
1270         error ("invalid floating point abi: -mfloat-abi=%s",
1271                target_float_abi_name);
1272     }
1273   else
1274     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1275
1276   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1277     sorry ("-mfloat-abi=hard and VFP");
1278
1279   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1280      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1281      will ever exist.  GCC makes no attempt to support this combination.  */
1282   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1283     sorry ("iWMMXt and hardware floating point");
1284
1285   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1286   if (TARGET_THUMB2 && TARGET_IWMMXT)
1287     sorry ("Thumb-2 iWMMXt");
1288
1289   /* If soft-float is specified then don't use FPU.  */
1290   if (TARGET_SOFT_FLOAT)
1291     arm_fpu_arch = FPUTYPE_NONE;
1292
1293   /* For arm2/3 there is no need to do any scheduling if there is only
1294      a floating point emulator, or we are doing software floating-point.  */
1295   if ((TARGET_SOFT_FLOAT
1296        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1297        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1298       && (tune_flags & FL_MODE32) == 0)
1299     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1300
1301   if (target_thread_switch)
1302     {
1303       if (strcmp (target_thread_switch, "soft") == 0)
1304         target_thread_pointer = TP_SOFT;
1305       else if (strcmp (target_thread_switch, "auto") == 0)
1306         target_thread_pointer = TP_AUTO;
1307       else if (strcmp (target_thread_switch, "cp15") == 0)
1308         target_thread_pointer = TP_CP15;
1309       else
1310         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1311     }
1312
1313   /* Use the cp15 method if it is available.  */
1314   if (target_thread_pointer == TP_AUTO)
1315     {
1316       if (arm_arch6k && !TARGET_THUMB)
1317         target_thread_pointer = TP_CP15;
1318       else
1319         target_thread_pointer = TP_SOFT;
1320     }
1321
1322   if (TARGET_HARD_TP && TARGET_THUMB1)
1323     error ("can not use -mtp=cp15 with 16-bit Thumb");
1324
1325   /* Override the default structure alignment for AAPCS ABI.  */
1326   if (TARGET_AAPCS_BASED)
1327     arm_structure_size_boundary = 8;
1328
1329   if (structure_size_string != NULL)
1330     {
1331       int size = strtol (structure_size_string, NULL, 0);
1332
1333       if (size == 8 || size == 32
1334           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1335         arm_structure_size_boundary = size;
1336       else
1337         warning (0, "structure size boundary can only be set to %s",
1338                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1339     }
1340
1341   /* If stack checking is disabled, we can use r10 as the PIC register,
1342      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1343   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1344     arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1345
1346   if (arm_pic_register_string != NULL)
1347     {
1348       int pic_register = decode_reg_name (arm_pic_register_string);
1349
1350       if (!flag_pic)
1351         warning (0, "-mpic-register= is useless without -fpic");
1352
1353       /* Prevent the user from choosing an obviously stupid PIC register.  */
1354       else if (pic_register < 0 || call_used_regs[pic_register]
1355                || pic_register == HARD_FRAME_POINTER_REGNUM
1356                || pic_register == STACK_POINTER_REGNUM
1357                || pic_register >= PC_REGNUM)
1358         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1359       else
1360         arm_pic_register = pic_register;
1361     }
1362
1363   /* ??? We might want scheduling for thumb2.  */
1364   if (TARGET_THUMB && flag_schedule_insns)
1365     {
1366       /* Don't warn since it's on by default in -O2.  */
1367       flag_schedule_insns = 0;
1368     }
1369
1370   if (optimize_size)
1371     {
1372       arm_constant_limit = 1;
1373
1374       /* If optimizing for size, bump the number of instructions that we
1375          are prepared to conditionally execute (even on a StrongARM).  */
1376       max_insns_skipped = 6;
1377     }
1378   else
1379     {
1380       /* For processors with load scheduling, it never costs more than
1381          2 cycles to load a constant, and the load scheduler may well
1382          reduce that to 1.  */
1383       if (arm_ld_sched)
1384         arm_constant_limit = 1;
1385
1386       /* On XScale the longer latency of a load makes it more difficult
1387          to achieve a good schedule, so it's faster to synthesize
1388          constants that can be done in two insns.  */
1389       if (arm_tune_xscale)
1390         arm_constant_limit = 2;
1391
1392       /* StrongARM has early execution of branches, so a sequence
1393          that is worth skipping is shorter.  */
1394       if (arm_tune_strongarm)
1395         max_insns_skipped = 3;
1396     }
1397
1398   /* Register global variables with the garbage collector.  */
1399   arm_add_gc_roots ();
1400 }
1401
1402 static void
1403 arm_add_gc_roots (void)
1404 {
1405   gcc_obstack_init(&minipool_obstack);
1406   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1407 }
1408 \f
1409 /* A table of known ARM exception types.
1410    For use with the interrupt function attribute.  */
1411
1412 typedef struct
1413 {
1414   const char *const arg;
1415   const unsigned long return_value;
1416 }
1417 isr_attribute_arg;
1418
1419 static const isr_attribute_arg isr_attribute_args [] =
1420 {
1421   { "IRQ",   ARM_FT_ISR },
1422   { "irq",   ARM_FT_ISR },
1423   { "FIQ",   ARM_FT_FIQ },
1424   { "fiq",   ARM_FT_FIQ },
1425   { "ABORT", ARM_FT_ISR },
1426   { "abort", ARM_FT_ISR },
1427   { "ABORT", ARM_FT_ISR },
1428   { "abort", ARM_FT_ISR },
1429   { "UNDEF", ARM_FT_EXCEPTION },
1430   { "undef", ARM_FT_EXCEPTION },
1431   { "SWI",   ARM_FT_EXCEPTION },
1432   { "swi",   ARM_FT_EXCEPTION },
1433   { NULL,    ARM_FT_NORMAL }
1434 };
1435
1436 /* Returns the (interrupt) function type of the current
1437    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1438
1439 static unsigned long
1440 arm_isr_value (tree argument)
1441 {
1442   const isr_attribute_arg * ptr;
1443   const char *              arg;
1444
1445   if (!arm_arch_notm)
1446     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1447
1448   /* No argument - default to IRQ.  */
1449   if (argument == NULL_TREE)
1450     return ARM_FT_ISR;
1451
1452   /* Get the value of the argument.  */
1453   if (TREE_VALUE (argument) == NULL_TREE
1454       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1455     return ARM_FT_UNKNOWN;
1456
1457   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1458
1459   /* Check it against the list of known arguments.  */
1460   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1461     if (streq (arg, ptr->arg))
1462       return ptr->return_value;
1463
1464   /* An unrecognized interrupt type.  */
1465   return ARM_FT_UNKNOWN;
1466 }
1467
1468 /* Computes the type of the current function.  */
1469
1470 static unsigned long
1471 arm_compute_func_type (void)
1472 {
1473   unsigned long type = ARM_FT_UNKNOWN;
1474   tree a;
1475   tree attr;
1476
1477   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1478
1479   /* Decide if the current function is volatile.  Such functions
1480      never return, and many memory cycles can be saved by not storing
1481      register values that will never be needed again.  This optimization
1482      was added to speed up context switching in a kernel application.  */
1483   if (optimize > 0
1484       && (TREE_NOTHROW (current_function_decl)
1485           || !(flag_unwind_tables
1486                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1487       && TREE_THIS_VOLATILE (current_function_decl))
1488     type |= ARM_FT_VOLATILE;
1489
1490   if (cfun->static_chain_decl != NULL)
1491     type |= ARM_FT_NESTED;
1492
1493   attr = DECL_ATTRIBUTES (current_function_decl);
1494
1495   a = lookup_attribute ("naked", attr);
1496   if (a != NULL_TREE)
1497     type |= ARM_FT_NAKED;
1498
1499   a = lookup_attribute ("isr", attr);
1500   if (a == NULL_TREE)
1501     a = lookup_attribute ("interrupt", attr);
1502
1503   if (a == NULL_TREE)
1504     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1505   else
1506     type |= arm_isr_value (TREE_VALUE (a));
1507
1508   return type;
1509 }
1510
1511 /* Returns the type of the current function.  */
1512
1513 unsigned long
1514 arm_current_func_type (void)
1515 {
1516   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1517     cfun->machine->func_type = arm_compute_func_type ();
1518
1519   return cfun->machine->func_type;
1520 }
1521 \f
1522 /* Return 1 if it is possible to return using a single instruction.
1523    If SIBLING is non-null, this is a test for a return before a sibling
1524    call.  SIBLING is the call insn, so we can examine its register usage.  */
1525
1526 int
1527 use_return_insn (int iscond, rtx sibling)
1528 {
1529   int regno;
1530   unsigned int func_type;
1531   unsigned long saved_int_regs;
1532   unsigned HOST_WIDE_INT stack_adjust;
1533   arm_stack_offsets *offsets;
1534
1535   /* Never use a return instruction before reload has run.  */
1536   if (!reload_completed)
1537     return 0;
1538
1539   func_type = arm_current_func_type ();
1540
1541   /* Naked, volatile and stack alignment functions need special
1542      consideration.  */
1543   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1544     return 0;
1545
1546   /* So do interrupt functions that use the frame pointer and Thumb
1547      interrupt functions.  */
1548   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1549     return 0;
1550
1551   offsets = arm_get_frame_offsets ();
1552   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1553
1554   /* As do variadic functions.  */
1555   if (current_function_pretend_args_size
1556       || cfun->machine->uses_anonymous_args
1557       /* Or if the function calls __builtin_eh_return () */
1558       || current_function_calls_eh_return
1559       /* Or if the function calls alloca */
1560       || current_function_calls_alloca
1561       /* Or if there is a stack adjustment.  However, if the stack pointer
1562          is saved on the stack, we can use a pre-incrementing stack load.  */
1563       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1564     return 0;
1565
1566   saved_int_regs = arm_compute_save_reg_mask ();
1567
1568   /* Unfortunately, the insn
1569
1570        ldmib sp, {..., sp, ...}
1571
1572      triggers a bug on most SA-110 based devices, such that the stack
1573      pointer won't be correctly restored if the instruction takes a
1574      page fault.  We work around this problem by popping r3 along with
1575      the other registers, since that is never slower than executing
1576      another instruction.
1577
1578      We test for !arm_arch5 here, because code for any architecture
1579      less than this could potentially be run on one of the buggy
1580      chips.  */
1581   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1582     {
1583       /* Validate that r3 is a call-clobbered register (always true in
1584          the default abi) ...  */
1585       if (!call_used_regs[3])
1586         return 0;
1587
1588       /* ... that it isn't being used for a return value ... */
1589       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1590         return 0;
1591
1592       /* ... or for a tail-call argument ...  */
1593       if (sibling)
1594         {
1595           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1596
1597           if (find_regno_fusage (sibling, USE, 3))
1598             return 0;
1599         }
1600
1601       /* ... and that there are no call-saved registers in r0-r2
1602          (always true in the default ABI).  */
1603       if (saved_int_regs & 0x7)
1604         return 0;
1605     }
1606
1607   /* Can't be done if interworking with Thumb, and any registers have been
1608      stacked.  */
1609   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1610     return 0;
1611
1612   /* On StrongARM, conditional returns are expensive if they aren't
1613      taken and multiple registers have been stacked.  */
1614   if (iscond && arm_tune_strongarm)
1615     {
1616       /* Conditional return when just the LR is stored is a simple
1617          conditional-load instruction, that's not expensive.  */
1618       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1619         return 0;
1620
1621       if (flag_pic 
1622           && arm_pic_register != INVALID_REGNUM
1623           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1624         return 0;
1625     }
1626
1627   /* If there are saved registers but the LR isn't saved, then we need
1628      two instructions for the return.  */
1629   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1630     return 0;
1631
1632   /* Can't be done if any of the FPA regs are pushed,
1633      since this also requires an insn.  */
1634   if (TARGET_HARD_FLOAT && TARGET_FPA)
1635     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1636       if (regs_ever_live[regno] && !call_used_regs[regno])
1637         return 0;
1638
1639   /* Likewise VFP regs.  */
1640   if (TARGET_HARD_FLOAT && TARGET_VFP)
1641     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1642       if (regs_ever_live[regno] && !call_used_regs[regno])
1643         return 0;
1644
1645   if (TARGET_REALLY_IWMMXT)
1646     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1647       if (regs_ever_live[regno] && ! call_used_regs [regno])
1648         return 0;
1649
1650   return 1;
1651 }
1652
1653 /* Return TRUE if int I is a valid immediate ARM constant.  */
1654
1655 int
1656 const_ok_for_arm (HOST_WIDE_INT i)
1657 {
1658   int lowbit;
1659
1660   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1661      be all zero, or all one.  */
1662   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1663       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1664           != ((~(unsigned HOST_WIDE_INT) 0)
1665               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1666     return FALSE;
1667
1668   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1669
1670   /* Fast return for 0 and small values.  We must do this for zero, since
1671      the code below can't handle that one case.  */
1672   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1673     return TRUE;
1674
1675   /* Get the number of trailing zeros.  */
1676   lowbit = ffs((int) i) - 1;
1677   
1678   /* Only even shifts are allowed in ARM mode so round down to the
1679      nearest even number.  */
1680   if (TARGET_ARM)
1681     lowbit &= ~1;
1682
1683   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1684     return TRUE;
1685
1686   if (TARGET_ARM)
1687     {
1688       /* Allow rotated constants in ARM mode.  */
1689       if (lowbit <= 4
1690            && ((i & ~0xc000003f) == 0
1691                || (i & ~0xf000000f) == 0
1692                || (i & ~0xfc000003) == 0))
1693         return TRUE;
1694     }
1695   else
1696     {
1697       HOST_WIDE_INT v;
1698
1699       /* Allow repeated pattern.  */
1700       v = i & 0xff;
1701       v |= v << 16;
1702       if (i == v || i == (v | (v << 8)))
1703         return TRUE;
1704     }
1705
1706   return FALSE;
1707 }
1708
1709 /* Return true if I is a valid constant for the operation CODE.  */
1710 static int
1711 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1712 {
1713   if (const_ok_for_arm (i))
1714     return 1;
1715
1716   switch (code)
1717     {
1718     case PLUS:
1719       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1720
1721     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1722     case XOR:
1723     case IOR:
1724       return 0;
1725
1726     case AND:
1727       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1728
1729     default:
1730       gcc_unreachable ();
1731     }
1732 }
1733
1734 /* Emit a sequence of insns to handle a large constant.
1735    CODE is the code of the operation required, it can be any of SET, PLUS,
1736    IOR, AND, XOR, MINUS;
1737    MODE is the mode in which the operation is being performed;
1738    VAL is the integer to operate on;
1739    SOURCE is the other operand (a register, or a null-pointer for SET);
1740    SUBTARGETS means it is safe to create scratch registers if that will
1741    either produce a simpler sequence, or we will want to cse the values.
1742    Return value is the number of insns emitted.  */
1743
1744 /* ??? Tweak this for thumb2.  */
1745 int
1746 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1747                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1748 {
1749   rtx cond;
1750
1751   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1752     cond = COND_EXEC_TEST (PATTERN (insn));
1753   else
1754     cond = NULL_RTX;
1755
1756   if (subtargets || code == SET
1757       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1758           && REGNO (target) != REGNO (source)))
1759     {
1760       /* After arm_reorg has been called, we can't fix up expensive
1761          constants by pushing them into memory so we must synthesize
1762          them in-line, regardless of the cost.  This is only likely to
1763          be more costly on chips that have load delay slots and we are
1764          compiling without running the scheduler (so no splitting
1765          occurred before the final instruction emission).
1766
1767          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1768       */
1769       if (!after_arm_reorg
1770           && !cond
1771           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1772                                 1, 0)
1773               > arm_constant_limit + (code != SET)))
1774         {
1775           if (code == SET)
1776             {
1777               /* Currently SET is the only monadic value for CODE, all
1778                  the rest are diadic.  */
1779               emit_set_insn (target, GEN_INT (val));
1780               return 1;
1781             }
1782           else
1783             {
1784               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1785
1786               emit_set_insn (temp, GEN_INT (val));
1787               /* For MINUS, the value is subtracted from, since we never
1788                  have subtraction of a constant.  */
1789               if (code == MINUS)
1790                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1791               else
1792                 emit_set_insn (target,
1793                                gen_rtx_fmt_ee (code, mode, source, temp));
1794               return 2;
1795             }
1796         }
1797     }
1798
1799   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1800                            1);
1801 }
1802
1803 /* Return the number of ARM instructions required to synthesize the given
1804    constant.  */
1805 static int
1806 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1807 {
1808   HOST_WIDE_INT temp1;
1809   int num_insns = 0;
1810   do
1811     {
1812       int end;
1813
1814       if (i <= 0)
1815         i += 32;
1816       if (remainder & (3 << (i - 2)))
1817         {
1818           end = i - 8;
1819           if (end < 0)
1820             end += 32;
1821           temp1 = remainder & ((0x0ff << end)
1822                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1823           remainder &= ~temp1;
1824           num_insns++;
1825           i -= 6;
1826         }
1827       i -= 2;
1828     } while (remainder);
1829   return num_insns;
1830 }
1831
1832 /* Emit an instruction with the indicated PATTERN.  If COND is
1833    non-NULL, conditionalize the execution of the instruction on COND
1834    being true.  */
1835
1836 static void
1837 emit_constant_insn (rtx cond, rtx pattern)
1838 {
1839   if (cond)
1840     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1841   emit_insn (pattern);
1842 }
1843
1844 /* As above, but extra parameter GENERATE which, if clear, suppresses
1845    RTL generation.  */
1846 /* ??? This needs more work for thumb2.  */
1847
1848 static int
1849 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1850                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1851                   int generate)
1852 {
1853   int can_invert = 0;
1854   int can_negate = 0;
1855   int can_negate_initial = 0;
1856   int can_shift = 0;
1857   int i;
1858   int num_bits_set = 0;
1859   int set_sign_bit_copies = 0;
1860   int clear_sign_bit_copies = 0;
1861   int clear_zero_bit_copies = 0;
1862   int set_zero_bit_copies = 0;
1863   int insns = 0;
1864   unsigned HOST_WIDE_INT temp1, temp2;
1865   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1866
1867   /* Find out which operations are safe for a given CODE.  Also do a quick
1868      check for degenerate cases; these can occur when DImode operations
1869      are split.  */
1870   switch (code)
1871     {
1872     case SET:
1873       can_invert = 1;
1874       can_shift = 1;
1875       can_negate = 1;
1876       break;
1877
1878     case PLUS:
1879       can_negate = 1;
1880       can_negate_initial = 1;
1881       break;
1882
1883     case IOR:
1884       if (remainder == 0xffffffff)
1885         {
1886           if (generate)
1887             emit_constant_insn (cond,
1888                                 gen_rtx_SET (VOIDmode, target,
1889                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1890           return 1;
1891         }
1892       if (remainder == 0)
1893         {
1894           if (reload_completed && rtx_equal_p (target, source))
1895             return 0;
1896           if (generate)
1897             emit_constant_insn (cond,
1898                                 gen_rtx_SET (VOIDmode, target, source));
1899           return 1;
1900         }
1901       break;
1902
1903     case AND:
1904       if (remainder == 0)
1905         {
1906           if (generate)
1907             emit_constant_insn (cond,
1908                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1909           return 1;
1910         }
1911       if (remainder == 0xffffffff)
1912         {
1913           if (reload_completed && rtx_equal_p (target, source))
1914             return 0;
1915           if (generate)
1916             emit_constant_insn (cond,
1917                                 gen_rtx_SET (VOIDmode, target, source));
1918           return 1;
1919         }
1920       can_invert = 1;
1921       break;
1922
1923     case XOR:
1924       if (remainder == 0)
1925         {
1926           if (reload_completed && rtx_equal_p (target, source))
1927             return 0;
1928           if (generate)
1929             emit_constant_insn (cond,
1930                                 gen_rtx_SET (VOIDmode, target, source));
1931           return 1;
1932         }
1933
1934       /* We don't know how to handle other cases yet.  */
1935       gcc_assert (remainder == 0xffffffff);
1936
1937       if (generate)
1938         emit_constant_insn (cond,
1939                             gen_rtx_SET (VOIDmode, target,
1940                                          gen_rtx_NOT (mode, source)));
1941       return 1;
1942
1943     case MINUS:
1944       /* We treat MINUS as (val - source), since (source - val) is always
1945          passed as (source + (-val)).  */
1946       if (remainder == 0)
1947         {
1948           if (generate)
1949             emit_constant_insn (cond,
1950                                 gen_rtx_SET (VOIDmode, target,
1951                                              gen_rtx_NEG (mode, source)));
1952           return 1;
1953         }
1954       if (const_ok_for_arm (val))
1955         {
1956           if (generate)
1957             emit_constant_insn (cond,
1958                                 gen_rtx_SET (VOIDmode, target,
1959                                              gen_rtx_MINUS (mode, GEN_INT (val),
1960                                                             source)));
1961           return 1;
1962         }
1963       can_negate = 1;
1964
1965       break;
1966
1967     default:
1968       gcc_unreachable ();
1969     }
1970
1971   /* If we can do it in one insn get out quickly.  */
1972   if (const_ok_for_arm (val)
1973       || (can_negate_initial && const_ok_for_arm (-val))
1974       || (can_invert && const_ok_for_arm (~val)))
1975     {
1976       if (generate)
1977         emit_constant_insn (cond,
1978                             gen_rtx_SET (VOIDmode, target,
1979                                          (source
1980                                           ? gen_rtx_fmt_ee (code, mode, source,
1981                                                             GEN_INT (val))
1982                                           : GEN_INT (val))));
1983       return 1;
1984     }
1985
1986   /* Calculate a few attributes that may be useful for specific
1987      optimizations.  */
1988   for (i = 31; i >= 0; i--)
1989     {
1990       if ((remainder & (1 << i)) == 0)
1991         clear_sign_bit_copies++;
1992       else
1993         break;
1994     }
1995
1996   for (i = 31; i >= 0; i--)
1997     {
1998       if ((remainder & (1 << i)) != 0)
1999         set_sign_bit_copies++;
2000       else
2001         break;
2002     }
2003
2004   for (i = 0; i <= 31; i++)
2005     {
2006       if ((remainder & (1 << i)) == 0)
2007         clear_zero_bit_copies++;
2008       else
2009         break;
2010     }
2011
2012   for (i = 0; i <= 31; i++)
2013     {
2014       if ((remainder & (1 << i)) != 0)
2015         set_zero_bit_copies++;
2016       else
2017         break;
2018     }
2019
2020   switch (code)
2021     {
2022     case SET:
2023       /* See if we can use movw.  */
2024       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2025         {
2026           if (generate)
2027             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2028                                                    GEN_INT (val)));
2029           return 1;
2030         }
2031
2032       /* See if we can do this by sign_extending a constant that is known
2033          to be negative.  This is a good, way of doing it, since the shift
2034          may well merge into a subsequent insn.  */
2035       if (set_sign_bit_copies > 1)
2036         {
2037           if (const_ok_for_arm
2038               (temp1 = ARM_SIGN_EXTEND (remainder
2039                                         << (set_sign_bit_copies - 1))))
2040             {
2041               if (generate)
2042                 {
2043                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2044                   emit_constant_insn (cond,
2045                                       gen_rtx_SET (VOIDmode, new_src,
2046                                                    GEN_INT (temp1)));
2047                   emit_constant_insn (cond,
2048                                       gen_ashrsi3 (target, new_src,
2049                                                    GEN_INT (set_sign_bit_copies - 1)));
2050                 }
2051               return 2;
2052             }
2053           /* For an inverted constant, we will need to set the low bits,
2054              these will be shifted out of harm's way.  */
2055           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2056           if (const_ok_for_arm (~temp1))
2057             {
2058               if (generate)
2059                 {
2060                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2061                   emit_constant_insn (cond,
2062                                       gen_rtx_SET (VOIDmode, new_src,
2063                                                    GEN_INT (temp1)));
2064                   emit_constant_insn (cond,
2065                                       gen_ashrsi3 (target, new_src,
2066                                                    GEN_INT (set_sign_bit_copies - 1)));
2067                 }
2068               return 2;
2069             }
2070         }
2071
2072       /* See if we can calculate the value as the difference between two
2073          valid immediates.  */
2074       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2075         {
2076           int topshift = clear_sign_bit_copies & ~1;
2077
2078           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2079                                    & (0xff000000 >> topshift));
2080
2081           /* If temp1 is zero, then that means the 9 most significant
2082              bits of remainder were 1 and we've caused it to overflow.
2083              When topshift is 0 we don't need to do anything since we
2084              can borrow from 'bit 32'.  */
2085           if (temp1 == 0 && topshift != 0)
2086             temp1 = 0x80000000 >> (topshift - 1);
2087
2088           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2089
2090           if (const_ok_for_arm (temp2))
2091             {
2092               if (generate)
2093                 {
2094                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2095                   emit_constant_insn (cond,
2096                                       gen_rtx_SET (VOIDmode, new_src,
2097                                                    GEN_INT (temp1)));
2098                   emit_constant_insn (cond,
2099                                       gen_addsi3 (target, new_src,
2100                                                   GEN_INT (-temp2)));
2101                 }
2102
2103               return 2;
2104             }
2105         }
2106
2107       /* See if we can generate this by setting the bottom (or the top)
2108          16 bits, and then shifting these into the other half of the
2109          word.  We only look for the simplest cases, to do more would cost
2110          too much.  Be careful, however, not to generate this when the
2111          alternative would take fewer insns.  */
2112       if (val & 0xffff0000)
2113         {
2114           temp1 = remainder & 0xffff0000;
2115           temp2 = remainder & 0x0000ffff;
2116
2117           /* Overlaps outside this range are best done using other methods.  */
2118           for (i = 9; i < 24; i++)
2119             {
2120               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2121                   && !const_ok_for_arm (temp2))
2122                 {
2123                   rtx new_src = (subtargets
2124                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2125                                  : target);
2126                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2127                                             source, subtargets, generate);
2128                   source = new_src;
2129                   if (generate)
2130                     emit_constant_insn
2131                       (cond,
2132                        gen_rtx_SET
2133                        (VOIDmode, target,
2134                         gen_rtx_IOR (mode,
2135                                      gen_rtx_ASHIFT (mode, source,
2136                                                      GEN_INT (i)),
2137                                      source)));
2138                   return insns + 1;
2139                 }
2140             }
2141
2142           /* Don't duplicate cases already considered.  */
2143           for (i = 17; i < 24; i++)
2144             {
2145               if (((temp1 | (temp1 >> i)) == remainder)
2146                   && !const_ok_for_arm (temp1))
2147                 {
2148                   rtx new_src = (subtargets
2149                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2150                                  : target);
2151                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2152                                             source, subtargets, generate);
2153                   source = new_src;
2154                   if (generate)
2155                     emit_constant_insn
2156                       (cond,
2157                        gen_rtx_SET (VOIDmode, target,
2158                                     gen_rtx_IOR
2159                                     (mode,
2160                                      gen_rtx_LSHIFTRT (mode, source,
2161                                                        GEN_INT (i)),
2162                                      source)));
2163                   return insns + 1;
2164                 }
2165             }
2166         }
2167       break;
2168
2169     case IOR:
2170     case XOR:
2171       /* If we have IOR or XOR, and the constant can be loaded in a
2172          single instruction, and we can find a temporary to put it in,
2173          then this can be done in two instructions instead of 3-4.  */
2174       if (subtargets
2175           /* TARGET can't be NULL if SUBTARGETS is 0 */
2176           || (reload_completed && !reg_mentioned_p (target, source)))
2177         {
2178           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2179             {
2180               if (generate)
2181                 {
2182                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2183
2184                   emit_constant_insn (cond,
2185                                       gen_rtx_SET (VOIDmode, sub,
2186                                                    GEN_INT (val)));
2187                   emit_constant_insn (cond,
2188                                       gen_rtx_SET (VOIDmode, target,
2189                                                    gen_rtx_fmt_ee (code, mode,
2190                                                                    source, sub)));
2191                 }
2192               return 2;
2193             }
2194         }
2195
2196       if (code == XOR)
2197         break;
2198
2199       if (set_sign_bit_copies > 8
2200           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2201         {
2202           if (generate)
2203             {
2204               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2205               rtx shift = GEN_INT (set_sign_bit_copies);
2206
2207               emit_constant_insn
2208                 (cond,
2209                  gen_rtx_SET (VOIDmode, sub,
2210                               gen_rtx_NOT (mode,
2211                                            gen_rtx_ASHIFT (mode,
2212                                                            source,
2213                                                            shift))));
2214               emit_constant_insn
2215                 (cond,
2216                  gen_rtx_SET (VOIDmode, target,
2217                               gen_rtx_NOT (mode,
2218                                            gen_rtx_LSHIFTRT (mode, sub,
2219                                                              shift))));
2220             }
2221           return 2;
2222         }
2223
2224       if (set_zero_bit_copies > 8
2225           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2226         {
2227           if (generate)
2228             {
2229               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2230               rtx shift = GEN_INT (set_zero_bit_copies);
2231
2232               emit_constant_insn
2233                 (cond,
2234                  gen_rtx_SET (VOIDmode, sub,
2235                               gen_rtx_NOT (mode,
2236                                            gen_rtx_LSHIFTRT (mode,
2237                                                              source,
2238                                                              shift))));
2239               emit_constant_insn
2240                 (cond,
2241                  gen_rtx_SET (VOIDmode, target,
2242                               gen_rtx_NOT (mode,
2243                                            gen_rtx_ASHIFT (mode, sub,
2244                                                            shift))));
2245             }
2246           return 2;
2247         }
2248
2249       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2250         {
2251           if (generate)
2252             {
2253               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2254               emit_constant_insn (cond,
2255                                   gen_rtx_SET (VOIDmode, sub,
2256                                                gen_rtx_NOT (mode, source)));
2257               source = sub;
2258               if (subtargets)
2259                 sub = gen_reg_rtx (mode);
2260               emit_constant_insn (cond,
2261                                   gen_rtx_SET (VOIDmode, sub,
2262                                                gen_rtx_AND (mode, source,
2263                                                             GEN_INT (temp1))));
2264               emit_constant_insn (cond,
2265                                   gen_rtx_SET (VOIDmode, target,
2266                                                gen_rtx_NOT (mode, sub)));
2267             }
2268           return 3;
2269         }
2270       break;
2271
2272     case AND:
2273       /* See if two shifts will do 2 or more insn's worth of work.  */
2274       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2275         {
2276           HOST_WIDE_INT shift_mask = ((0xffffffff
2277                                        << (32 - clear_sign_bit_copies))
2278                                       & 0xffffffff);
2279
2280           if ((remainder | shift_mask) != 0xffffffff)
2281             {
2282               if (generate)
2283                 {
2284                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2285                   insns = arm_gen_constant (AND, mode, cond,
2286                                             remainder | shift_mask,
2287                                             new_src, source, subtargets, 1);
2288                   source = new_src;
2289                 }
2290               else
2291                 {
2292                   rtx targ = subtargets ? NULL_RTX : target;
2293                   insns = arm_gen_constant (AND, mode, cond,
2294                                             remainder | shift_mask,
2295                                             targ, source, subtargets, 0);
2296                 }
2297             }
2298
2299           if (generate)
2300             {
2301               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2302               rtx shift = GEN_INT (clear_sign_bit_copies);
2303
2304               emit_insn (gen_ashlsi3 (new_src, source, shift));
2305               emit_insn (gen_lshrsi3 (target, new_src, shift));
2306             }
2307
2308           return insns + 2;
2309         }
2310
2311       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2312         {
2313           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2314
2315           if ((remainder | shift_mask) != 0xffffffff)
2316             {
2317               if (generate)
2318                 {
2319                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2320
2321                   insns = arm_gen_constant (AND, mode, cond,
2322                                             remainder | shift_mask,
2323                                             new_src, source, subtargets, 1);
2324                   source = new_src;
2325                 }
2326               else
2327                 {
2328                   rtx targ = subtargets ? NULL_RTX : target;
2329
2330                   insns = arm_gen_constant (AND, mode, cond,
2331                                             remainder | shift_mask,
2332                                             targ, source, subtargets, 0);
2333                 }
2334             }
2335
2336           if (generate)
2337             {
2338               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2339               rtx shift = GEN_INT (clear_zero_bit_copies);
2340
2341               emit_insn (gen_lshrsi3 (new_src, source, shift));
2342               emit_insn (gen_ashlsi3 (target, new_src, shift));
2343             }
2344
2345           return insns + 2;
2346         }
2347
2348       break;
2349
2350     default:
2351       break;
2352     }
2353
2354   for (i = 0; i < 32; i++)
2355     if (remainder & (1 << i))
2356       num_bits_set++;
2357
2358   if (code == AND || (can_invert && num_bits_set > 16))
2359     remainder = (~remainder) & 0xffffffff;
2360   else if (code == PLUS && num_bits_set > 16)
2361     remainder = (-remainder) & 0xffffffff;
2362   else
2363     {
2364       can_invert = 0;
2365       can_negate = 0;
2366     }
2367
2368   /* Now try and find a way of doing the job in either two or three
2369      instructions.
2370      We start by looking for the largest block of zeros that are aligned on
2371      a 2-bit boundary, we then fill up the temps, wrapping around to the
2372      top of the word when we drop off the bottom.
2373      In the worst case this code should produce no more than four insns.
2374      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2375      best place to start.  */
2376
2377   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2378      the same.  */
2379   {
2380     int best_start = 0;
2381     if (!TARGET_THUMB2)
2382       {
2383         int best_consecutive_zeros = 0;
2384
2385         for (i = 0; i < 32; i += 2)
2386           {
2387             int consecutive_zeros = 0;
2388
2389             if (!(remainder & (3 << i)))
2390               {
2391                 while ((i < 32) && !(remainder & (3 << i)))
2392                   {
2393                     consecutive_zeros += 2;
2394                     i += 2;
2395                   }
2396                 if (consecutive_zeros > best_consecutive_zeros)
2397                   {
2398                     best_consecutive_zeros = consecutive_zeros;
2399                     best_start = i - consecutive_zeros;
2400                   }
2401                 i -= 2;
2402               }
2403           }
2404
2405         /* So long as it won't require any more insns to do so, it's
2406            desirable to emit a small constant (in bits 0...9) in the last
2407            insn.  This way there is more chance that it can be combined with
2408            a later addressing insn to form a pre-indexed load or store
2409            operation.  Consider:
2410
2411                    *((volatile int *)0xe0000100) = 1;
2412                    *((volatile int *)0xe0000110) = 2;
2413
2414            We want this to wind up as:
2415
2416                     mov rA, #0xe0000000
2417                     mov rB, #1
2418                     str rB, [rA, #0x100]
2419                     mov rB, #2
2420                     str rB, [rA, #0x110]
2421
2422            rather than having to synthesize both large constants from scratch.
2423
2424            Therefore, we calculate how many insns would be required to emit
2425            the constant starting from `best_start', and also starting from
2426            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2427            yield a shorter sequence, we may as well use zero.  */
2428         if (best_start != 0
2429             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2430             && (count_insns_for_constant (remainder, 0) <=
2431                 count_insns_for_constant (remainder, best_start)))
2432           best_start = 0;
2433       }
2434
2435     /* Now start emitting the insns.  */
2436     i = best_start;
2437     do
2438       {
2439         int end;
2440
2441         if (i <= 0)
2442           i += 32;
2443         if (remainder & (3 << (i - 2)))
2444           {
2445             end = i - 8;
2446             if (end < 0)
2447               end += 32;
2448             temp1 = remainder & ((0x0ff << end)
2449                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2450             remainder &= ~temp1;
2451
2452             if (generate)
2453               {
2454                 rtx new_src, temp1_rtx;
2455
2456                 if (code == SET || code == MINUS)
2457                   {
2458                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2459                     if (can_invert && code != MINUS)
2460                       temp1 = ~temp1;
2461                   }
2462                 else
2463                   {
2464                     if (remainder && subtargets)
2465                       new_src = gen_reg_rtx (mode);
2466                     else
2467                       new_src = target;
2468                     if (can_invert)
2469                       temp1 = ~temp1;
2470                     else if (can_negate)
2471                       temp1 = -temp1;
2472                   }
2473
2474                 temp1 = trunc_int_for_mode (temp1, mode);
2475                 temp1_rtx = GEN_INT (temp1);
2476
2477                 if (code == SET)
2478                   ;
2479                 else if (code == MINUS)
2480                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2481                 else
2482                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2483
2484                 emit_constant_insn (cond,
2485                                     gen_rtx_SET (VOIDmode, new_src,
2486                                                  temp1_rtx));
2487                 source = new_src;
2488               }
2489
2490             if (code == SET)
2491               {
2492                 can_invert = 0;
2493                 code = PLUS;
2494               }
2495             else if (code == MINUS)
2496               code = PLUS;
2497
2498             insns++;
2499             if (TARGET_ARM)
2500               i -= 6;
2501             else
2502               i -= 7;
2503           }
2504         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2505            shifts.  */
2506         if (TARGET_ARM)
2507           i -= 2;
2508         else
2509           i--;
2510       }
2511     while (remainder);
2512   }
2513
2514   return insns;
2515 }
2516
2517 /* Canonicalize a comparison so that we are more likely to recognize it.
2518    This can be done for a few constant compares, where we can make the
2519    immediate value easier to load.  */
2520
2521 enum rtx_code
2522 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2523                              rtx * op1)
2524 {
2525   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2526   unsigned HOST_WIDE_INT maxval;
2527   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2528
2529   switch (code)
2530     {
2531     case EQ:
2532     case NE:
2533       return code;
2534
2535     case GT:
2536     case LE:
2537       if (i != maxval
2538           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2539         {
2540           *op1 = GEN_INT (i + 1);
2541           return code == GT ? GE : LT;
2542         }
2543       break;
2544
2545     case GE:
2546     case LT:
2547       if (i != ~maxval
2548           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2549         {
2550           *op1 = GEN_INT (i - 1);
2551           return code == GE ? GT : LE;
2552         }
2553       break;
2554
2555     case GTU:
2556     case LEU:
2557       if (i != ~((unsigned HOST_WIDE_INT) 0)
2558           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2559         {
2560           *op1 = GEN_INT (i + 1);
2561           return code == GTU ? GEU : LTU;
2562         }
2563       break;
2564
2565     case GEU:
2566     case LTU:
2567       if (i != 0
2568           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2569         {
2570           *op1 = GEN_INT (i - 1);
2571           return code == GEU ? GTU : LEU;
2572         }
2573       break;
2574
2575     default:
2576       gcc_unreachable ();
2577     }
2578
2579   return code;
2580 }
2581
2582
2583 /* Define how to find the value returned by a function.  */
2584
2585 rtx
2586 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2587 {
2588   enum machine_mode mode;
2589   int unsignedp ATTRIBUTE_UNUSED;
2590   rtx r ATTRIBUTE_UNUSED;
2591
2592   mode = TYPE_MODE (type);
2593   /* Promote integer types.  */
2594   if (INTEGRAL_TYPE_P (type))
2595     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2596
2597   /* Promotes small structs returned in a register to full-word size
2598      for big-endian AAPCS.  */
2599   if (arm_return_in_msb (type))
2600     {
2601       HOST_WIDE_INT size = int_size_in_bytes (type);
2602       if (size % UNITS_PER_WORD != 0)
2603         {
2604           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2605           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2606         }
2607     }
2608
2609   return LIBCALL_VALUE(mode);
2610 }
2611
2612 /* Determine the amount of memory needed to store the possible return
2613    registers of an untyped call.  */
2614 int
2615 arm_apply_result_size (void)
2616 {
2617   int size = 16;
2618
2619   if (TARGET_ARM)
2620     {
2621       if (TARGET_HARD_FLOAT_ABI)
2622         {
2623           if (TARGET_FPA)
2624             size += 12;
2625           if (TARGET_MAVERICK)
2626             size += 8;
2627         }
2628       if (TARGET_IWMMXT_ABI)
2629         size += 8;
2630     }
2631
2632   return size;
2633 }
2634
2635 /* Decide whether a type should be returned in memory (true)
2636    or in a register (false).  This is called by the macro
2637    RETURN_IN_MEMORY.  */
2638 int
2639 arm_return_in_memory (tree type)
2640 {
2641   HOST_WIDE_INT size;
2642
2643   if (!AGGREGATE_TYPE_P (type) &&
2644       (TREE_CODE (type) != VECTOR_TYPE) &&
2645       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2646     /* All simple types are returned in registers.
2647        For AAPCS, complex types are treated the same as aggregates.  */
2648     return 0;
2649
2650   size = int_size_in_bytes (type);
2651
2652   if (arm_abi != ARM_ABI_APCS)
2653     {
2654       /* ATPCS and later return aggregate types in memory only if they are
2655          larger than a word (or are variable size).  */
2656       return (size < 0 || size > UNITS_PER_WORD);
2657     }
2658
2659   /* To maximize backwards compatibility with previous versions of gcc,
2660      return vectors up to 4 words in registers.  */
2661   if (TREE_CODE (type) == VECTOR_TYPE)
2662     return (size < 0 || size > (4 * UNITS_PER_WORD));
2663
2664   /* For the arm-wince targets we choose to be compatible with Microsoft's
2665      ARM and Thumb compilers, which always return aggregates in memory.  */
2666 #ifndef ARM_WINCE
2667   /* All structures/unions bigger than one word are returned in memory.
2668      Also catch the case where int_size_in_bytes returns -1.  In this case
2669      the aggregate is either huge or of variable size, and in either case
2670      we will want to return it via memory and not in a register.  */
2671   if (size < 0 || size > UNITS_PER_WORD)
2672     return 1;
2673
2674   if (TREE_CODE (type) == RECORD_TYPE)
2675     {
2676       tree field;
2677
2678       /* For a struct the APCS says that we only return in a register
2679          if the type is 'integer like' and every addressable element
2680          has an offset of zero.  For practical purposes this means
2681          that the structure can have at most one non bit-field element
2682          and that this element must be the first one in the structure.  */
2683
2684       /* Find the first field, ignoring non FIELD_DECL things which will
2685          have been created by C++.  */
2686       for (field = TYPE_FIELDS (type);
2687            field && TREE_CODE (field) != FIELD_DECL;
2688            field = TREE_CHAIN (field))
2689         continue;
2690
2691       if (field == NULL)
2692         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2693
2694       /* Check that the first field is valid for returning in a register.  */
2695
2696       /* ... Floats are not allowed */
2697       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2698         return 1;
2699
2700       /* ... Aggregates that are not themselves valid for returning in
2701          a register are not allowed.  */
2702       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2703         return 1;
2704
2705       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2706          since they are not addressable.  */
2707       for (field = TREE_CHAIN (field);
2708            field;
2709            field = TREE_CHAIN (field))
2710         {
2711           if (TREE_CODE (field) != FIELD_DECL)
2712             continue;
2713
2714           if (!DECL_BIT_FIELD_TYPE (field))
2715             return 1;
2716         }
2717
2718       return 0;
2719     }
2720
2721   if (TREE_CODE (type) == UNION_TYPE)
2722     {
2723       tree field;
2724
2725       /* Unions can be returned in registers if every element is
2726          integral, or can be returned in an integer register.  */
2727       for (field = TYPE_FIELDS (type);
2728            field;
2729            field = TREE_CHAIN (field))
2730         {
2731           if (TREE_CODE (field) != FIELD_DECL)
2732             continue;
2733
2734           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2735             return 1;
2736
2737           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2738             return 1;
2739         }
2740
2741       return 0;
2742     }
2743 #endif /* not ARM_WINCE */
2744
2745   /* Return all other types in memory.  */
2746   return 1;
2747 }
2748
2749 /* Indicate whether or not words of a double are in big-endian order.  */
2750
2751 int
2752 arm_float_words_big_endian (void)
2753 {
2754   if (TARGET_MAVERICK)
2755     return 0;
2756
2757   /* For FPA, float words are always big-endian.  For VFP, floats words
2758      follow the memory system mode.  */
2759
2760   if (TARGET_FPA)
2761     {
2762       return 1;
2763     }
2764
2765   if (TARGET_VFP)
2766     return (TARGET_BIG_END ? 1 : 0);
2767
2768   return 1;
2769 }
2770
2771 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2772    for a call to a function whose data type is FNTYPE.
2773    For a library call, FNTYPE is NULL.  */
2774 void
2775 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2776                           rtx libname  ATTRIBUTE_UNUSED,
2777                           tree fndecl ATTRIBUTE_UNUSED)
2778 {
2779   /* On the ARM, the offset starts at 0.  */
2780   pcum->nregs = 0;
2781   pcum->iwmmxt_nregs = 0;
2782   pcum->can_split = true;
2783
2784   pcum->call_cookie = CALL_NORMAL;
2785
2786   if (TARGET_LONG_CALLS)
2787     pcum->call_cookie = CALL_LONG;
2788
2789   /* Check for long call/short call attributes.  The attributes
2790      override any command line option.  */
2791   if (fntype)
2792     {
2793       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2794         pcum->call_cookie = CALL_SHORT;
2795       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2796         pcum->call_cookie = CALL_LONG;
2797     }
2798
2799   /* Varargs vectors are treated the same as long long.
2800      named_count avoids having to change the way arm handles 'named' */
2801   pcum->named_count = 0;
2802   pcum->nargs = 0;
2803
2804   if (TARGET_REALLY_IWMMXT && fntype)
2805     {
2806       tree fn_arg;
2807
2808       for (fn_arg = TYPE_ARG_TYPES (fntype);
2809            fn_arg;
2810            fn_arg = TREE_CHAIN (fn_arg))
2811         pcum->named_count += 1;
2812
2813       if (! pcum->named_count)
2814         pcum->named_count = INT_MAX;
2815     }
2816 }
2817
2818
2819 /* Return true if mode/type need doubleword alignment.  */
2820 bool
2821 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2822 {
2823   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2824           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2825 }
2826
2827
2828 /* Determine where to put an argument to a function.
2829    Value is zero to push the argument on the stack,
2830    or a hard register in which to store the argument.
2831
2832    MODE is the argument's machine mode.
2833    TYPE is the data type of the argument (as a tree).
2834     This is null for libcalls where that information may
2835     not be available.
2836    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2837     the preceding args and about the function being called.
2838    NAMED is nonzero if this argument is a named parameter
2839     (otherwise it is an extra parameter matching an ellipsis).  */
2840
2841 rtx
2842 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2843                   tree type, int named)
2844 {
2845   int nregs;
2846
2847   /* Varargs vectors are treated the same as long long.
2848      named_count avoids having to change the way arm handles 'named' */
2849   if (TARGET_IWMMXT_ABI
2850       && arm_vector_mode_supported_p (mode)
2851       && pcum->named_count > pcum->nargs + 1)
2852     {
2853       if (pcum->iwmmxt_nregs <= 9)
2854         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2855       else
2856         {
2857           pcum->can_split = false;
2858           return NULL_RTX;
2859         }
2860     }
2861
2862   /* Put doubleword aligned quantities in even register pairs.  */
2863   if (pcum->nregs & 1
2864       && ARM_DOUBLEWORD_ALIGN
2865       && arm_needs_doubleword_align (mode, type))
2866     pcum->nregs++;
2867
2868   if (mode == VOIDmode)
2869     /* Compute operand 2 of the call insn.  */
2870     return GEN_INT (pcum->call_cookie);
2871
2872   /* Only allow splitting an arg between regs and memory if all preceding
2873      args were allocated to regs.  For args passed by reference we only count
2874      the reference pointer.  */
2875   if (pcum->can_split)
2876     nregs = 1;
2877   else
2878     nregs = ARM_NUM_REGS2 (mode, type);
2879
2880   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2881     return NULL_RTX;
2882
2883   return gen_rtx_REG (mode, pcum->nregs);
2884 }
2885
2886 static int
2887 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2888                        tree type, bool named ATTRIBUTE_UNUSED)
2889 {
2890   int nregs = pcum->nregs;
2891
2892   if (arm_vector_mode_supported_p (mode))
2893     return 0;
2894
2895   if (NUM_ARG_REGS > nregs
2896       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2897       && pcum->can_split)
2898     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2899
2900   return 0;
2901 }
2902
2903 /* Variable sized types are passed by reference.  This is a GCC
2904    extension to the ARM ABI.  */
2905
2906 static bool
2907 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2908                        enum machine_mode mode ATTRIBUTE_UNUSED,
2909                        tree type, bool named ATTRIBUTE_UNUSED)
2910 {
2911   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2912 }
2913 \f
2914 /* Encode the current state of the #pragma [no_]long_calls.  */
2915 typedef enum
2916 {
2917   OFF,          /* No #pragma [no_]long_calls is in effect.  */
2918   LONG,         /* #pragma long_calls is in effect.  */
2919   SHORT         /* #pragma no_long_calls is in effect.  */
2920 } arm_pragma_enum;
2921
2922 static arm_pragma_enum arm_pragma_long_calls = OFF;
2923
2924 void
2925 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2926 {
2927   arm_pragma_long_calls = LONG;
2928 }
2929
2930 void
2931 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2932 {
2933   arm_pragma_long_calls = SHORT;
2934 }
2935
2936 void
2937 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2938 {
2939   arm_pragma_long_calls = OFF;
2940 }
2941 \f
2942 /* Table of machine attributes.  */
2943 const struct attribute_spec arm_attribute_table[] =
2944 {
2945   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2946   /* Function calls made to this symbol must be done indirectly, because
2947      it may lie outside of the 26 bit addressing range of a normal function
2948      call.  */
2949   { "long_call",    0, 0, false, true,  true,  NULL },
2950   /* Whereas these functions are always known to reside within the 26 bit
2951      addressing range.  */
2952   { "short_call",   0, 0, false, true,  true,  NULL },
2953   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2954   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2955   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2956   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2957 #ifdef ARM_PE
2958   /* ARM/PE has three new attributes:
2959      interfacearm - ?
2960      dllexport - for exporting a function/variable that will live in a dll
2961      dllimport - for importing a function/variable from a dll
2962
2963      Microsoft allows multiple declspecs in one __declspec, separating
2964      them with spaces.  We do NOT support this.  Instead, use __declspec
2965      multiple times.
2966   */
2967   { "dllimport",    0, 0, true,  false, false, NULL },
2968   { "dllexport",    0, 0, true,  false, false, NULL },
2969   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2970 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2971   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2972   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2973   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2974 #endif
2975   { NULL,           0, 0, false, false, false, NULL }
2976 };
2977
2978 /* Handle an attribute requiring a FUNCTION_DECL;
2979    arguments as in struct attribute_spec.handler.  */
2980 static tree
2981 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2982                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2983 {
2984   if (TREE_CODE (*node) != FUNCTION_DECL)
2985     {
2986       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2987                IDENTIFIER_POINTER (name));
2988       *no_add_attrs = true;
2989     }
2990
2991   return NULL_TREE;
2992 }
2993
2994 /* Handle an "interrupt" or "isr" attribute;
2995    arguments as in struct attribute_spec.handler.  */
2996 static tree
2997 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2998                           bool *no_add_attrs)
2999 {
3000   if (DECL_P (*node))
3001     {
3002       if (TREE_CODE (*node) != FUNCTION_DECL)
3003         {
3004           warning (OPT_Wattributes, "%qs attribute only applies to functions",
3005                    IDENTIFIER_POINTER (name));
3006           *no_add_attrs = true;
3007         }
3008       /* FIXME: the argument if any is checked for type attributes;
3009          should it be checked for decl ones?  */
3010     }
3011   else
3012     {
3013       if (TREE_CODE (*node) == FUNCTION_TYPE
3014           || TREE_CODE (*node) == METHOD_TYPE)
3015         {
3016           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3017             {
3018               warning (OPT_Wattributes, "%qs attribute ignored",
3019                        IDENTIFIER_POINTER (name));
3020               *no_add_attrs = true;
3021             }
3022         }
3023       else if (TREE_CODE (*node) == POINTER_TYPE
3024                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3025                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3026                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3027         {
3028           *node = build_variant_type_copy (*node);
3029           TREE_TYPE (*node) = build_type_attribute_variant
3030             (TREE_TYPE (*node),
3031              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3032           *no_add_attrs = true;
3033         }
3034       else
3035         {
3036           /* Possibly pass this attribute on from the type to a decl.  */
3037           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3038                        | (int) ATTR_FLAG_FUNCTION_NEXT
3039                        | (int) ATTR_FLAG_ARRAY_NEXT))
3040             {
3041               *no_add_attrs = true;
3042               return tree_cons (name, args, NULL_TREE);
3043             }
3044           else
3045             {
3046               warning (OPT_Wattributes, "%qs attribute ignored",
3047                        IDENTIFIER_POINTER (name));
3048             }
3049         }
3050     }
3051
3052   return NULL_TREE;
3053 }
3054
3055 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3056 /* Handle the "notshared" attribute.  This attribute is another way of
3057    requesting hidden visibility.  ARM's compiler supports
3058    "__declspec(notshared)"; we support the same thing via an
3059    attribute.  */
3060
3061 static tree
3062 arm_handle_notshared_attribute (tree *node,
3063                                 tree name ATTRIBUTE_UNUSED,
3064                                 tree args ATTRIBUTE_UNUSED,
3065                                 int flags ATTRIBUTE_UNUSED,
3066                                 bool *no_add_attrs)
3067 {
3068   tree decl = TYPE_NAME (*node);
3069
3070   if (decl)
3071     {
3072       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3073       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3074       *no_add_attrs = false;
3075     }
3076   return NULL_TREE;
3077 }
3078 #endif
3079
3080 /* Return 0 if the attributes for two types are incompatible, 1 if they
3081    are compatible, and 2 if they are nearly compatible (which causes a
3082    warning to be generated).  */
3083 static int
3084 arm_comp_type_attributes (tree type1, tree type2)
3085 {
3086   int l1, l2, s1, s2;
3087
3088   /* Check for mismatch of non-default calling convention.  */
3089   if (TREE_CODE (type1) != FUNCTION_TYPE)
3090     return 1;
3091
3092   /* Check for mismatched call attributes.  */
3093   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3094   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3095   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3096   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3097
3098   /* Only bother to check if an attribute is defined.  */
3099   if (l1 | l2 | s1 | s2)
3100     {
3101       /* If one type has an attribute, the other must have the same attribute.  */
3102       if ((l1 != l2) || (s1 != s2))
3103         return 0;
3104
3105       /* Disallow mixed attributes.  */
3106       if ((l1 & s2) || (l2 & s1))
3107         return 0;
3108     }
3109
3110   /* Check for mismatched ISR attribute.  */
3111   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3112   if (! l1)
3113     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3114   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3115   if (! l2)
3116     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3117   if (l1 != l2)
3118     return 0;
3119
3120   return 1;
3121 }
3122
3123 /*  Encode long_call or short_call attribute by prefixing
3124     symbol name in DECL with a special character FLAG.  */
3125 void
3126 arm_encode_call_attribute (tree decl, int flag)
3127 {
3128   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
3129   int          len = strlen (str);
3130   char *       newstr;
3131
3132   /* Do not allow weak functions to be treated as short call.  */
3133   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3134     return;
3135
3136   newstr = alloca (len + 2);
3137   newstr[0] = flag;
3138   strcpy (newstr + 1, str);
3139
3140   newstr = (char *) ggc_alloc_string (newstr, len + 1);
3141   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3142 }
3143
3144 /*  Assigns default attributes to newly defined type.  This is used to
3145     set short_call/long_call attributes for function types of
3146     functions defined inside corresponding #pragma scopes.  */
3147 static void
3148 arm_set_default_type_attributes (tree type)
3149 {
3150   /* Add __attribute__ ((long_call)) to all functions, when
3151      inside #pragma long_calls or __attribute__ ((short_call)),
3152      when inside #pragma no_long_calls.  */
3153   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3154     {
3155       tree type_attr_list, attr_name;
3156       type_attr_list = TYPE_ATTRIBUTES (type);
3157
3158       if (arm_pragma_long_calls == LONG)
3159         attr_name = get_identifier ("long_call");
3160       else if (arm_pragma_long_calls == SHORT)
3161         attr_name = get_identifier ("short_call");
3162       else
3163         return;
3164
3165       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3166       TYPE_ATTRIBUTES (type) = type_attr_list;
3167     }
3168 }
3169 \f
3170 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
3171    defined within the current compilation unit.  If this cannot be
3172    determined, then 0 is returned.  */
3173 static int
3174 current_file_function_operand (rtx sym_ref)
3175 {
3176   /* This is a bit of a fib.  A function will have a short call flag
3177      applied to its name if it has the short call attribute, or it has
3178      already been defined within the current compilation unit.  */
3179   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3180     return 1;
3181
3182   /* The current function is always defined within the current compilation
3183      unit.  If it s a weak definition however, then this may not be the real
3184      definition of the function, and so we have to say no.  */
3185   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3186       && !DECL_WEAK (current_function_decl))
3187     return 1;
3188
3189   /* We cannot make the determination - default to returning 0.  */
3190   return 0;
3191 }
3192
3193 /* Return nonzero if a 32-bit "long_call" should be generated for
3194    this call.  We generate a long_call if the function:
3195
3196         a.  has an __attribute__((long call))
3197      or b.  is within the scope of a #pragma long_calls
3198      or c.  the -mlong-calls command line switch has been specified
3199          .  and either:
3200                 1. -ffunction-sections is in effect
3201              or 2. the current function has __attribute__ ((section))
3202              or 3. the target function has __attribute__ ((section))
3203
3204    However we do not generate a long call if the function:
3205
3206         d.  has an __attribute__ ((short_call))
3207      or e.  is inside the scope of a #pragma no_long_calls
3208      or f.  is defined within the current compilation unit.
3209
3210    This function will be called by C fragments contained in the machine
3211    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3212    rtl operands.  CALL_SYMBOL is used to distinguish between
3213    two different callers of the function.  It is set to 1 in the
3214    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3215    and "call_value" patterns.  This is because of the difference in the
3216    SYM_REFs passed by these patterns.  */
3217 int
3218 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3219 {
3220   if (!call_symbol)
3221     {
3222       if (GET_CODE (sym_ref) != MEM)
3223         return 0;
3224
3225       sym_ref = XEXP (sym_ref, 0);
3226     }
3227
3228   if (GET_CODE (sym_ref) != SYMBOL_REF)
3229     return 0;
3230
3231   if (call_cookie & CALL_SHORT)
3232     return 0;
3233
3234   if (TARGET_LONG_CALLS)
3235     {
3236       if (flag_function_sections
3237           || DECL_SECTION_NAME (current_function_decl))
3238         /* c.3 is handled by the definition of the
3239            ARM_DECLARE_FUNCTION_SIZE macro.  */
3240         return 1;
3241     }
3242
3243   if (current_file_function_operand (sym_ref))
3244     return 0;
3245
3246   return (call_cookie & CALL_LONG)
3247     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3248     || TARGET_LONG_CALLS;
3249 }
3250
3251 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3252 static bool
3253 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3254 {
3255   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3256   unsigned long func_type;
3257
3258   if (cfun->machine->sibcall_blocked)
3259     return false;
3260
3261   /* Never tailcall something for which we have no decl, or if we
3262      are in Thumb mode.  */
3263   if (decl == NULL || TARGET_THUMB)
3264     return false;
3265
3266   /* Get the calling method.  */
3267   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3268     call_type = CALL_SHORT;
3269   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3270     call_type = CALL_LONG;
3271
3272   /* Cannot tail-call to long calls, since these are out of range of
3273      a branch instruction.  However, if not compiling PIC, we know
3274      we can reach the symbol if it is in this compilation unit.  */
3275   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3276     return false;
3277
3278   /* If we are interworking and the function is not declared static
3279      then we can't tail-call it unless we know that it exists in this
3280      compilation unit (since it might be a Thumb routine).  */
3281   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3282     return false;
3283
3284   func_type = arm_current_func_type ();
3285   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3286   if (IS_INTERRUPT (func_type))
3287     return false;
3288
3289   /* Never tailcall if function may be called with a misaligned SP.  */
3290   if (IS_STACKALIGN (func_type))
3291     return false;
3292
3293   /* Everything else is ok.  */
3294   return true;
3295 }
3296
3297 \f
3298 /* Addressing mode support functions.  */
3299
3300 /* Return nonzero if X is a legitimate immediate operand when compiling
3301    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3302 int
3303 legitimate_pic_operand_p (rtx x)
3304 {
3305   if (GET_CODE (x) == SYMBOL_REF
3306       || (GET_CODE (x) == CONST
3307           && GET_CODE (XEXP (x, 0)) == PLUS
3308           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3309     return 0;
3310
3311   return 1;
3312 }
3313
3314 rtx
3315 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3316 {
3317   if (GET_CODE (orig) == SYMBOL_REF
3318       || GET_CODE (orig) == LABEL_REF)
3319     {
3320 #ifndef AOF_ASSEMBLER
3321       rtx pic_ref, address;
3322 #endif
3323       rtx insn;
3324       int subregs = 0;
3325
3326       /* If this function doesn't have a pic register, create one now.
3327          A lot of the logic here is made obscure by the fact that this
3328          routine gets called as part of the rtx cost estimation
3329          process.  We don't want those calls to affect any assumptions
3330          about the real function; and further, we can't call
3331          entry_of_function() until we start the real expansion
3332          process.  */
3333       if (!current_function_uses_pic_offset_table)
3334         {
3335           gcc_assert (!no_new_pseudos);
3336           if (arm_pic_register != INVALID_REGNUM)
3337             {
3338               cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3339
3340               /* Play games to avoid marking the function as needing pic
3341                  if we are being called as part of the cost-estimation
3342                  process.  */
3343               if (current_ir_type () != IR_GIMPLE)
3344                 current_function_uses_pic_offset_table = 1;
3345             }
3346           else
3347             {
3348               rtx seq;
3349
3350               cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3351
3352               /* Play games to avoid marking the function as needing pic
3353                  if we are being called as part of the cost-estimation
3354                  process.  */
3355               if (current_ir_type () != IR_GIMPLE)
3356                 {
3357                   current_function_uses_pic_offset_table = 1;
3358                   start_sequence ();
3359
3360                   arm_load_pic_register (0UL);
3361
3362                   seq = get_insns ();
3363                   end_sequence ();
3364                   emit_insn_after (seq, entry_of_function ());
3365                 }
3366             }
3367         }
3368
3369       if (reg == 0)
3370         {
3371           gcc_assert (!no_new_pseudos);
3372           reg = gen_reg_rtx (Pmode);
3373
3374           subregs = 1;
3375         }
3376
3377 #ifdef AOF_ASSEMBLER
3378       /* The AOF assembler can generate relocations for these directly, and
3379          understands that the PIC register has to be added into the offset.  */
3380       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3381 #else
3382       if (subregs)
3383         address = gen_reg_rtx (Pmode);
3384       else
3385         address = reg;
3386
3387       if (TARGET_ARM)
3388         emit_insn (gen_pic_load_addr_arm (address, orig));
3389       else if (TARGET_THUMB2)
3390         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3391       else /* TARGET_THUMB1 */
3392         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3393
3394       if ((GET_CODE (orig) == LABEL_REF
3395            || (GET_CODE (orig) == SYMBOL_REF &&
3396                SYMBOL_REF_LOCAL_P (orig)))
3397           && NEED_GOT_RELOC)
3398         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3399       else
3400         {
3401           pic_ref = gen_const_mem (Pmode,
3402                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3403                                                  address));
3404         }
3405
3406       insn = emit_move_insn (reg, pic_ref);
3407 #endif
3408       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3409          by loop.  */
3410       set_unique_reg_note (insn, REG_EQUAL, orig);
3411
3412       return reg;
3413     }
3414   else if (GET_CODE (orig) == CONST)
3415     {
3416       rtx base, offset;
3417
3418       if (GET_CODE (XEXP (orig, 0)) == PLUS
3419           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3420         return orig;
3421
3422       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3423           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3424         return orig;
3425
3426       if (reg == 0)
3427         {
3428           gcc_assert (!no_new_pseudos);
3429           reg = gen_reg_rtx (Pmode);
3430         }
3431
3432       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3433
3434       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3435       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3436                                        base == reg ? 0 : reg);
3437
3438       if (GET_CODE (offset) == CONST_INT)
3439         {
3440           /* The base register doesn't really matter, we only want to
3441              test the index for the appropriate mode.  */
3442           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3443             {
3444               gcc_assert (!no_new_pseudos);
3445               offset = force_reg (Pmode, offset);
3446             }
3447
3448           if (GET_CODE (offset) == CONST_INT)
3449             return plus_constant (base, INTVAL (offset));
3450         }
3451
3452       if (GET_MODE_SIZE (mode) > 4
3453           && (GET_MODE_CLASS (mode) == MODE_INT
3454               || TARGET_SOFT_FLOAT))
3455         {
3456           emit_insn (gen_addsi3 (reg, base, offset));
3457           return reg;
3458         }
3459
3460       return gen_rtx_PLUS (Pmode, base, offset);
3461     }
3462
3463   return orig;
3464 }
3465
3466
3467 /* Find a spare register to use during the prolog of a function.  */
3468
3469 static int
3470 thumb_find_work_register (unsigned long pushed_regs_mask)
3471 {
3472   int reg;
3473
3474   /* Check the argument registers first as these are call-used.  The
3475      register allocation order means that sometimes r3 might be used
3476      but earlier argument registers might not, so check them all.  */
3477   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3478     if (!regs_ever_live[reg])
3479       return reg;
3480
3481   /* Before going on to check the call-saved registers we can try a couple
3482      more ways of deducing that r3 is available.  The first is when we are
3483      pushing anonymous arguments onto the stack and we have less than 4
3484      registers worth of fixed arguments(*).  In this case r3 will be part of
3485      the variable argument list and so we can be sure that it will be
3486      pushed right at the start of the function.  Hence it will be available
3487      for the rest of the prologue.
3488      (*): ie current_function_pretend_args_size is greater than 0.  */
3489   if (cfun->machine->uses_anonymous_args
3490       && current_function_pretend_args_size > 0)
3491     return LAST_ARG_REGNUM;
3492
3493   /* The other case is when we have fixed arguments but less than 4 registers
3494      worth.  In this case r3 might be used in the body of the function, but
3495      it is not being used to convey an argument into the function.  In theory
3496      we could just check current_function_args_size to see how many bytes are
3497      being passed in argument registers, but it seems that it is unreliable.
3498      Sometimes it will have the value 0 when in fact arguments are being
3499      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3500      check the args_info.nregs field as well.  The problem with this field is
3501      that it makes no allowances for arguments that are passed to the
3502      function but which are not used.  Hence we could miss an opportunity
3503      when a function has an unused argument in r3.  But it is better to be
3504      safe than to be sorry.  */
3505   if (! cfun->machine->uses_anonymous_args
3506       && current_function_args_size >= 0
3507       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3508       && cfun->args_info.nregs < 4)
3509     return LAST_ARG_REGNUM;
3510
3511   /* Otherwise look for a call-saved register that is going to be pushed.  */
3512   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3513     if (pushed_regs_mask & (1 << reg))
3514       return reg;
3515
3516   if (TARGET_THUMB2)
3517     {
3518       /* Thumb-2 can use high regs.  */
3519       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3520         if (pushed_regs_mask & (1 << reg))
3521           return reg;
3522     }
3523   /* Something went wrong - thumb_compute_save_reg_mask()
3524      should have arranged for a suitable register to be pushed.  */
3525   gcc_unreachable ();
3526 }
3527
3528 static GTY(()) int pic_labelno;
3529
3530 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3531    low register.  */
3532
3533 void
3534 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3535 {
3536 #ifndef AOF_ASSEMBLER
3537   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3538   rtx global_offset_table;
3539
3540   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3541     return;
3542
3543   gcc_assert (flag_pic);
3544
3545   /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3546      in the code stream.  */
3547
3548   labelno = GEN_INT (pic_labelno++);
3549   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3550   l1 = gen_rtx_CONST (VOIDmode, l1);
3551
3552   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3553   /* On the ARM the PC register contains 'dot + 8' at the time of the
3554      addition, on the Thumb it is 'dot + 4'.  */
3555   pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3556   if (GOT_PCREL)
3557     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3558                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3559   else
3560     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3561
3562   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3563
3564   if (TARGET_ARM)
3565     {
3566       emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3567       emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3568                                              cfun->machine->pic_reg, labelno));
3569     }
3570   else if (TARGET_THUMB2)
3571     {
3572       /* Thumb-2 only allows very limited access to the PC.  Calculate the
3573          address in a temporary register.  */
3574       if (arm_pic_register != INVALID_REGNUM)
3575         {
3576           pic_tmp = gen_rtx_REG (SImode,
3577                                  thumb_find_work_register (saved_regs));
3578         }
3579       else
3580         {
3581           gcc_assert (!no_new_pseudos);
3582           pic_tmp = gen_reg_rtx (Pmode);
3583         }
3584
3585       emit_insn (gen_pic_load_addr_thumb2 (cfun->machine->pic_reg, pic_rtx));
3586       emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3587       emit_insn (gen_addsi3(cfun->machine->pic_reg, cfun->machine->pic_reg,
3588                             pic_tmp));
3589     }
3590   else /* TARGET_THUMB1 */
3591     {
3592       if (arm_pic_register != INVALID_REGNUM
3593           && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3594         {
3595           /* We will have pushed the pic register, so we should always be
3596              able to find a work register.  */
3597           pic_tmp = gen_rtx_REG (SImode,
3598                                  thumb_find_work_register (saved_regs));
3599           emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3600           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3601         }
3602       else
3603         emit_insn (gen_pic_load_addr_thumb1 (cfun->machine->pic_reg, pic_rtx));
3604       emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3605                                             cfun->machine->pic_reg, labelno));
3606     }
3607
3608   /* Need to emit this whether or not we obey regdecls,
3609      since setjmp/longjmp can cause life info to screw up.  */
3610   emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3611 #endif /* AOF_ASSEMBLER */
3612 }
3613
3614
3615 /* Return nonzero if X is valid as an ARM state addressing register.  */
3616 static int
3617 arm_address_register_rtx_p (rtx x, int strict_p)
3618 {
3619   int regno;
3620
3621   if (GET_CODE (x) != REG)
3622     return 0;
3623
3624   regno = REGNO (x);
3625
3626   if (strict_p)
3627     return ARM_REGNO_OK_FOR_BASE_P (regno);
3628
3629   return (regno <= LAST_ARM_REGNUM
3630           || regno >= FIRST_PSEUDO_REGISTER
3631           || regno == FRAME_POINTER_REGNUM
3632           || regno == ARG_POINTER_REGNUM);
3633 }
3634
3635 /* Return TRUE if this rtx is the difference of a symbol and a label,
3636    and will reduce to a PC-relative relocation in the object file.
3637    Expressions like this can be left alone when generating PIC, rather
3638    than forced through the GOT.  */
3639 static int
3640 pcrel_constant_p (rtx x)
3641 {
3642   if (GET_CODE (x) == MINUS)
3643     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3644
3645   return FALSE;
3646 }
3647
3648 /* Return nonzero if X is a valid ARM state address operand.  */
3649 int
3650 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3651                           int strict_p)
3652 {
3653   bool use_ldrd;
3654   enum rtx_code code = GET_CODE (x);
3655
3656   if (arm_address_register_rtx_p (x, strict_p))
3657     return 1;
3658
3659   use_ldrd = (TARGET_LDRD
3660               && (mode == DImode
3661                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3662
3663   if (code == POST_INC || code == PRE_DEC
3664       || ((code == PRE_INC || code == POST_DEC)
3665           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3666     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3667
3668   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3669            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3670            && GET_CODE (XEXP (x, 1)) == PLUS
3671            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3672     {
3673       rtx addend = XEXP (XEXP (x, 1), 1);
3674
3675       /* Don't allow ldrd post increment by register because it's hard
3676          to fixup invalid register choices.  */
3677       if (use_ldrd
3678           && GET_CODE (x) == POST_MODIFY
3679           && GET_CODE (addend) == REG)
3680         return 0;
3681
3682       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3683               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3684     }
3685
3686   /* After reload constants split into minipools will have addresses
3687      from a LABEL_REF.  */
3688   else if (reload_completed
3689            && (code == LABEL_REF
3690                || (code == CONST
3691                    && GET_CODE (XEXP (x, 0)) == PLUS
3692                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3693                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3694     return 1;
3695
3696   else if (mode == TImode)
3697     return 0;
3698
3699   else if (code == PLUS)
3700     {
3701       rtx xop0 = XEXP (x, 0);
3702       rtx xop1 = XEXP (x, 1);
3703
3704       return ((arm_address_register_rtx_p (xop0, strict_p)
3705                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3706               || (arm_address_register_rtx_p (xop1, strict_p)
3707                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3708     }
3709
3710 #if 0
3711   /* Reload currently can't handle MINUS, so disable this for now */
3712   else if (GET_CODE (x) == MINUS)
3713     {
3714       rtx xop0 = XEXP (x, 0);
3715       rtx xop1 = XEXP (x, 1);
3716
3717       return (arm_address_register_rtx_p (xop0, strict_p)
3718               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3719     }
3720 #endif
3721
3722   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3723            && code == SYMBOL_REF
3724            && CONSTANT_POOL_ADDRESS_P (x)
3725            && ! (flag_pic
3726                  && symbol_mentioned_p (get_pool_constant (x))
3727                  && ! pcrel_constant_p (get_pool_constant (x))))
3728     return 1;
3729
3730   return 0;
3731 }
3732
3733 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3734 int
3735 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3736 {
3737   bool use_ldrd;
3738   enum rtx_code code = GET_CODE (x);
3739   
3740   if (arm_address_register_rtx_p (x, strict_p))
3741     return 1;
3742
3743   use_ldrd = (TARGET_LDRD
3744               && (mode == DImode
3745                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3746
3747   if (code == POST_INC || code == PRE_DEC
3748       || ((code == PRE_INC || code == POST_DEC)
3749           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3750     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3751
3752   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3753            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3754            && GET_CODE (XEXP (x, 1)) == PLUS
3755            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3756     {
3757       /* Thumb-2 only has autoincrement by constant.  */
3758       rtx addend = XEXP (XEXP (x, 1), 1);
3759       HOST_WIDE_INT offset;
3760
3761       if (GET_CODE (addend) != CONST_INT)
3762         return 0;
3763
3764       offset = INTVAL(addend);
3765       if (GET_MODE_SIZE (mode) <= 4)
3766         return (offset > -256 && offset < 256);
3767       
3768       return (use_ldrd && offset > -1024 && offset < 1024
3769               && (offset & 3) == 0);
3770     }
3771
3772   /* After reload constants split into minipools will have addresses
3773      from a LABEL_REF.  */
3774   else if (reload_completed
3775            && (code == LABEL_REF
3776                || (code == CONST
3777                    && GET_CODE (XEXP (x, 0)) == PLUS
3778                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3779                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3780     return 1;
3781
3782   else if (mode == TImode)
3783     return 0;
3784
3785   else if (code == PLUS)
3786     {
3787       rtx xop0 = XEXP (x, 0);
3788       rtx xop1 = XEXP (x, 1);
3789
3790       return ((arm_address_register_rtx_p (xop0, strict_p)
3791                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3792               || (arm_address_register_rtx_p (xop1, strict_p)
3793                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3794     }
3795
3796   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3797            && code == SYMBOL_REF
3798            && CONSTANT_POOL_ADDRESS_P (x)
3799            && ! (flag_pic
3800                  && symbol_mentioned_p (get_pool_constant (x))
3801                  && ! pcrel_constant_p (get_pool_constant (x))))
3802     return 1;
3803
3804   return 0;
3805 }
3806
3807 /* Return nonzero if INDEX is valid for an address index operand in
3808    ARM state.  */
3809 static int
3810 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3811                         int strict_p)
3812 {
3813   HOST_WIDE_INT range;
3814   enum rtx_code code = GET_CODE (index);
3815
3816   /* Standard coprocessor addressing modes.  */
3817   if (TARGET_HARD_FLOAT
3818       && (TARGET_FPA || TARGET_MAVERICK)
3819       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3820           || (TARGET_MAVERICK && mode == DImode)))
3821     return (code == CONST_INT && INTVAL (index) < 1024
3822             && INTVAL (index) > -1024
3823             && (INTVAL (index) & 3) == 0);
3824
3825   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3826     return (code == CONST_INT
3827             && INTVAL (index) < 1024
3828             && INTVAL (index) > -1024
3829             && (INTVAL (index) & 3) == 0);
3830
3831   if (arm_address_register_rtx_p (index, strict_p)
3832       && (GET_MODE_SIZE (mode) <= 4))
3833     return 1;
3834
3835   if (mode == DImode || mode == DFmode)
3836     {
3837       if (code == CONST_INT)
3838         {
3839           HOST_WIDE_INT val = INTVAL (index);
3840
3841           if (TARGET_LDRD)
3842             return val > -256 && val < 256;
3843           else
3844             return val > -4096 && val < 4092;
3845         }
3846
3847       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3848     }
3849
3850   if (GET_MODE_SIZE (mode) <= 4
3851       && ! (arm_arch4
3852             && (mode == HImode
3853                 || (mode == QImode && outer == SIGN_EXTEND))))
3854     {
3855       if (code == MULT)
3856         {
3857           rtx xiop0 = XEXP (index, 0);
3858           rtx xiop1 = XEXP (index, 1);
3859
3860           return ((arm_address_register_rtx_p (xiop0, strict_p)
3861                    && power_of_two_operand (xiop1, SImode))
3862                   || (arm_address_register_rtx_p (xiop1, strict_p)
3863                       && power_of_two_operand (xiop0, SImode)));
3864         }
3865       else if (code == LSHIFTRT || code == ASHIFTRT
3866                || code == ASHIFT || code == ROTATERT)
3867         {
3868           rtx op = XEXP (index, 1);
3869
3870           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3871                   && GET_CODE (op) == CONST_INT
3872                   && INTVAL (op) > 0
3873                   && INTVAL (op) <= 31);
3874         }
3875     }
3876
3877   /* For ARM v4 we may be doing a sign-extend operation during the
3878      load.  */
3879   if (arm_arch4)
3880     {
3881       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3882         range = 256;
3883       else
3884         range = 4096;
3885     }
3886   else
3887     range = (mode == HImode) ? 4095 : 4096;
3888
3889   return (code == CONST_INT
3890           && INTVAL (index) < range
3891           && INTVAL (index) > -range);
3892 }
3893
3894 /* Return true if OP is a valid index scaling factor for Thumb-2 address
3895    index operand.  i.e. 1, 2, 4 or 8.  */
3896 static bool
3897 thumb2_index_mul_operand (rtx op)
3898 {
3899   HOST_WIDE_INT val;
3900   
3901   if (GET_CODE(op) != CONST_INT)
3902     return false;
3903
3904   val = INTVAL(op);
3905   return (val == 1 || val == 2 || val == 4 || val == 8);
3906 }
3907   
3908 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
3909 static int
3910 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
3911 {
3912   enum rtx_code code = GET_CODE (index);
3913
3914   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
3915   /* Standard coprocessor addressing modes.  */
3916   if (TARGET_HARD_FLOAT
3917       && (TARGET_FPA || TARGET_MAVERICK)
3918       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3919           || (TARGET_MAVERICK && mode == DImode)))
3920     return (code == CONST_INT && INTVAL (index) < 1024
3921             && INTVAL (index) > -1024
3922             && (INTVAL (index) & 3) == 0);
3923
3924   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3925     return (code == CONST_INT
3926             && INTVAL (index) < 1024
3927             && INTVAL (index) > -1024
3928             && (INTVAL (index) & 3) == 0);
3929
3930   if (arm_address_register_rtx_p (index, strict_p)
3931       && (GET_MODE_SIZE (mode) <= 4))
3932     return 1;
3933
3934   if (mode == DImode || mode == DFmode)
3935     {
3936       HOST_WIDE_INT val = INTVAL (index);
3937       /* ??? Can we assume ldrd for thumb2?  */
3938       /* Thumb-2 ldrd only has reg+const addressing modes.  */
3939       if (code != CONST_INT)
3940         return 0;
3941
3942       /* ldrd supports offsets of +-1020.
3943          However the ldr fallback does not.  */
3944       return val > -256 && val < 256 && (val & 3) == 0;
3945     }
3946
3947   if (code == MULT)
3948     {
3949       rtx xiop0 = XEXP (index, 0);
3950       rtx xiop1 = XEXP (index, 1);
3951
3952       return ((arm_address_register_rtx_p (xiop0, strict_p)
3953                && thumb2_index_mul_operand (xiop1))
3954               || (arm_address_register_rtx_p (xiop1, strict_p)
3955                   && thumb2_index_mul_operand (xiop0)));
3956     }
3957   else if (code == ASHIFT)
3958     {
3959       rtx op = XEXP (index, 1);
3960
3961       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3962               && GET_CODE (op) == CONST_INT
3963               && INTVAL (op) > 0
3964               && INTVAL (op) <= 3);
3965     }
3966
3967   return (code == CONST_INT
3968           && INTVAL (index) < 4096
3969           && INTVAL (index) > -256);
3970 }
3971
3972 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
3973 static int
3974 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3975 {
3976   int regno;
3977
3978   if (GET_CODE (x) != REG)
3979     return 0;
3980
3981   regno = REGNO (x);
3982
3983   if (strict_p)
3984     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3985
3986   return (regno <= LAST_LO_REGNUM
3987           || regno > LAST_VIRTUAL_REGISTER
3988           || regno == FRAME_POINTER_REGNUM
3989           || (GET_MODE_SIZE (mode) >= 4
3990               && (regno == STACK_POINTER_REGNUM
3991                   || regno >= FIRST_PSEUDO_REGISTER
3992                   || x == hard_frame_pointer_rtx
3993                   || x == arg_pointer_rtx)));
3994 }
3995
3996 /* Return nonzero if x is a legitimate index register.  This is the case
3997    for any base register that can access a QImode object.  */
3998 inline static int
3999 thumb1_index_register_rtx_p (rtx x, int strict_p)
4000 {
4001   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4002 }
4003
4004 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4005
4006    The AP may be eliminated to either the SP or the FP, so we use the
4007    least common denominator, e.g. SImode, and offsets from 0 to 64.
4008
4009    ??? Verify whether the above is the right approach.
4010
4011    ??? Also, the FP may be eliminated to the SP, so perhaps that
4012    needs special handling also.
4013
4014    ??? Look at how the mips16 port solves this problem.  It probably uses
4015    better ways to solve some of these problems.
4016
4017    Although it is not incorrect, we don't accept QImode and HImode
4018    addresses based on the frame pointer or arg pointer until the
4019    reload pass starts.  This is so that eliminating such addresses
4020    into stack based ones won't produce impossible code.  */
4021 int
4022 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4023 {
4024   /* ??? Not clear if this is right.  Experiment.  */
4025   if (GET_MODE_SIZE (mode) < 4
4026       && !(reload_in_progress || reload_completed)
4027       && (reg_mentioned_p (frame_pointer_rtx, x)
4028           || reg_mentioned_p (arg_pointer_rtx, x)
4029           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4030           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4031           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4032           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4033     return 0;
4034
4035   /* Accept any base register.  SP only in SImode or larger.  */
4036   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4037     return 1;
4038
4039   /* This is PC relative data before arm_reorg runs.  */
4040   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4041            && GET_CODE (x) == SYMBOL_REF
4042            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4043     return 1;
4044
4045   /* This is PC relative data after arm_reorg runs.  */
4046   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4047            && (GET_CODE (x) == LABEL_REF
4048                || (GET_CODE (x) == CONST
4049                    && GET_CODE (XEXP (x, 0)) == PLUS
4050                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4051                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4052     return 1;
4053
4054   /* Post-inc indexing only supported for SImode and larger.  */
4055   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4056            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4057     return 1;
4058
4059   else if (GET_CODE (x) == PLUS)
4060     {
4061       /* REG+REG address can be any two index registers.  */
4062       /* We disallow FRAME+REG addressing since we know that FRAME
4063          will be replaced with STACK, and SP relative addressing only
4064          permits SP+OFFSET.  */
4065       if (GET_MODE_SIZE (mode) <= 4
4066           && XEXP (x, 0) != frame_pointer_rtx
4067           && XEXP (x, 1) != frame_pointer_rtx
4068           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4069           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4070         return 1;
4071
4072       /* REG+const has 5-7 bit offset for non-SP registers.  */
4073       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4074                 || XEXP (x, 0) == arg_pointer_rtx)
4075                && GET_CODE (XEXP (x, 1)) == CONST_INT
4076                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4077         return 1;
4078
4079       /* REG+const has 10-bit offset for SP, but only SImode and
4080          larger is supported.  */
4081       /* ??? Should probably check for DI/DFmode overflow here
4082          just like GO_IF_LEGITIMATE_OFFSET does.  */
4083       else if (GET_CODE (XEXP (x, 0)) == REG
4084                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4085                && GET_MODE_SIZE (mode) >= 4
4086                && GET_CODE (XEXP (x, 1)) == CONST_INT
4087                && INTVAL (XEXP (x, 1)) >= 0
4088                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4089                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4090         return 1;
4091
4092       else if (GET_CODE (XEXP (x, 0)) == REG
4093                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4094                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4095                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4096                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4097                && GET_MODE_SIZE (mode) >= 4
4098                && GET_CODE (XEXP (x, 1)) == CONST_INT
4099                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4100         return 1;
4101     }
4102
4103   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4104            && GET_MODE_SIZE (mode) == 4
4105            && GET_CODE (x) == SYMBOL_REF
4106            && CONSTANT_POOL_ADDRESS_P (x)
4107            && ! (flag_pic
4108                  && symbol_mentioned_p (get_pool_constant (x))
4109                  && ! pcrel_constant_p (get_pool_constant (x))))
4110     return 1;
4111
4112   return 0;
4113 }
4114
4115 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4116    instruction of mode MODE.  */
4117 int
4118 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4119 {
4120   switch (GET_MODE_SIZE (mode))
4121     {
4122     case 1:
4123       return val >= 0 && val < 32;
4124
4125     case 2:
4126       return val >= 0 && val < 64 && (val & 1) == 0;
4127
4128     default:
4129       return (val >= 0
4130               && (val + GET_MODE_SIZE (mode)) <= 128
4131               && (val & 3) == 0);
4132     }
4133 }
4134
4135 /* Build the SYMBOL_REF for __tls_get_addr.  */
4136
4137 static GTY(()) rtx tls_get_addr_libfunc;
4138
4139 static rtx
4140 get_tls_get_addr (void)
4141 {
4142   if (!tls_get_addr_libfunc)
4143     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4144   return tls_get_addr_libfunc;
4145 }
4146
4147 static rtx
4148 arm_load_tp (rtx target)
4149 {
4150   if (!target)
4151     target = gen_reg_rtx (SImode);
4152
4153   if (TARGET_HARD_TP)
4154     {
4155       /* Can return in any reg.  */
4156       emit_insn (gen_load_tp_hard (target));
4157     }
4158   else
4159     {
4160       /* Always returned in r0.  Immediately copy the result into a pseudo,
4161          otherwise other uses of r0 (e.g. setting up function arguments) may
4162          clobber the value.  */
4163
4164       rtx tmp;
4165
4166       emit_insn (gen_load_tp_soft ());
4167
4168       tmp = gen_rtx_REG (SImode, 0);
4169       emit_move_insn (target, tmp);
4170     }
4171   return target;
4172 }
4173
4174 static rtx
4175 load_tls_operand (rtx x, rtx reg)
4176 {
4177   rtx tmp;
4178
4179   if (reg == NULL_RTX)
4180     reg = gen_reg_rtx (SImode);
4181
4182   tmp = gen_rtx_CONST (SImode, x);
4183
4184   emit_move_insn (reg, tmp);
4185
4186   return reg;
4187 }
4188
4189 static rtx
4190 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4191 {
4192   rtx insns, label, labelno, sum;
4193
4194   start_sequence ();
4195
4196   labelno = GEN_INT (pic_labelno++);
4197   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4198   label = gen_rtx_CONST (VOIDmode, label);
4199
4200   sum = gen_rtx_UNSPEC (Pmode,
4201                         gen_rtvec (4, x, GEN_INT (reloc), label,
4202                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4203                         UNSPEC_TLS);
4204   reg = load_tls_operand (sum, reg);
4205
4206   if (TARGET_ARM)
4207     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4208   else if (TARGET_THUMB2)
4209     {
4210       rtx tmp;
4211       /* Thumb-2 only allows very limited access to the PC.  Calculate
4212          the address in a temporary register.  */
4213       tmp = gen_reg_rtx (SImode);
4214       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4215       emit_insn (gen_addsi3(reg, reg, tmp));
4216     }
4217   else /* TARGET_THUMB1 */
4218     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4219
4220   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4221                                      Pmode, 1, reg, Pmode);
4222
4223   insns = get_insns ();
4224   end_sequence ();
4225
4226   return insns;
4227 }
4228
4229 rtx
4230 legitimize_tls_address (rtx x, rtx reg)
4231 {
4232   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4233   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4234
4235   switch (model)
4236     {
4237     case TLS_MODEL_GLOBAL_DYNAMIC:
4238       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4239       dest = gen_reg_rtx (Pmode);
4240       emit_libcall_block (insns, dest, ret, x);
4241       return dest;
4242
4243     case TLS_MODEL_LOCAL_DYNAMIC:
4244       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4245
4246       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4247          share the LDM result with other LD model accesses.  */
4248       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4249                             UNSPEC_TLS);
4250       dest = gen_reg_rtx (Pmode);
4251       emit_libcall_block (insns, dest, ret, eqv);
4252
4253       /* Load the addend.  */
4254       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4255                                UNSPEC_TLS);
4256       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4257       return gen_rtx_PLUS (Pmode, dest, addend);
4258
4259     case TLS_MODEL_INITIAL_EXEC:
4260       labelno = GEN_INT (pic_labelno++);
4261       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4262       label = gen_rtx_CONST (VOIDmode, label);
4263       sum = gen_rtx_UNSPEC (Pmode,
4264                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4265                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4266                             UNSPEC_TLS);
4267       reg = load_tls_operand (sum, reg);
4268
4269       if (TARGET_ARM)
4270         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4271       else if (TARGET_THUMB2)
4272         {
4273           rtx tmp;
4274           /* Thumb-2 only allows very limited access to the PC.  Calculate
4275              the address in a temporary register.  */
4276           tmp = gen_reg_rtx (SImode);
4277           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4278           emit_insn (gen_addsi3(reg, reg, tmp));
4279           emit_move_insn (reg, gen_const_mem (SImode, reg));
4280         }
4281       else
4282         {
4283           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4284           emit_move_insn (reg, gen_const_mem (SImode, reg));
4285         }
4286
4287       tp = arm_load_tp (NULL_RTX);
4288
4289       return gen_rtx_PLUS (Pmode, tp, reg);
4290
4291     case TLS_MODEL_LOCAL_EXEC:
4292       tp = arm_load_tp (NULL_RTX);
4293
4294       reg = gen_rtx_UNSPEC (Pmode,
4295                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4296                             UNSPEC_TLS);
4297       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4298
4299       return gen_rtx_PLUS (Pmode, tp, reg);
4300
4301     default:
4302       abort ();
4303     }
4304 }
4305
4306 /* Try machine-dependent ways of modifying an illegitimate address
4307    to be legitimate.  If we find one, return the new, valid address.  */
4308 rtx
4309 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4310 {
4311   if (arm_tls_symbol_p (x))
4312     return legitimize_tls_address (x, NULL_RTX);
4313
4314   if (GET_CODE (x) == PLUS)
4315     {
4316       rtx xop0 = XEXP (x, 0);
4317       rtx xop1 = XEXP (x, 1);
4318
4319       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4320         xop0 = force_reg (SImode, xop0);
4321
4322       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4323         xop1 = force_reg (SImode, xop1);
4324
4325       if (ARM_BASE_REGISTER_RTX_P (xop0)
4326           && GET_CODE (xop1) == CONST_INT)
4327         {
4328           HOST_WIDE_INT n, low_n;
4329           rtx base_reg, val;
4330           n = INTVAL (xop1);
4331
4332           /* VFP addressing modes actually allow greater offsets, but for
4333              now we just stick with the lowest common denominator.  */
4334           if (mode == DImode
4335               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4336             {
4337               low_n = n & 0x0f;
4338               n &= ~0x0f;
4339               if (low_n > 4)
4340                 {
4341                   n += 16;
4342                   low_n -= 16;
4343                 }
4344             }
4345           else
4346             {
4347               low_n = ((mode) == TImode ? 0
4348                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4349               n -= low_n;
4350             }
4351
4352           base_reg = gen_reg_rtx (SImode);
4353           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4354           emit_move_insn (base_reg, val);
4355           x = plus_constant (base_reg, low_n);
4356         }
4357       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4358         x = gen_rtx_PLUS (SImode, xop0, xop1);
4359     }
4360
4361   /* XXX We don't allow MINUS any more -- see comment in
4362      arm_legitimate_address_p ().  */
4363   else if (GET_CODE (x) == MINUS)
4364     {
4365       rtx xop0 = XEXP (x, 0);
4366       rtx xop1 = XEXP (x, 1);
4367
4368       if (CONSTANT_P (xop0))
4369         xop0 = force_reg (SImode, xop0);
4370
4371       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4372         xop1 = force_reg (SImode, xop1);
4373
4374       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4375         x = gen_rtx_MINUS (SImode, xop0, xop1);
4376     }
4377
4378   /* Make sure to take full advantage of the pre-indexed addressing mode
4379      with absolute addresses which often allows for the base register to
4380      be factorized for multiple adjacent memory references, and it might
4381      even allows for the mini pool to be avoided entirely. */
4382   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4383     {
4384       unsigned int bits;
4385       HOST_WIDE_INT mask, base, index;
4386       rtx base_reg;
4387
4388       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4389          use a 8-bit index. So let's use a 12-bit index for SImode only and
4390          hope that arm_gen_constant will enable ldrb to use more bits. */
4391       bits = (mode == SImode) ? 12 : 8;
4392       mask = (1 << bits) - 1;
4393       base = INTVAL (x) & ~mask;
4394       index = INTVAL (x) & mask;
4395       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4396         {
4397           /* It'll most probably be more efficient to generate the base
4398              with more bits set and use a negative index instead. */
4399           base |= mask;
4400           index -= mask;
4401         }
4402       base_reg = force_reg (SImode, GEN_INT (base));
4403       x = plus_constant (base_reg, index);
4404     }
4405
4406   if (flag_pic)
4407     {
4408       /* We need to find and carefully transform any SYMBOL and LABEL
4409          references; so go back to the original address expression.  */
4410       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4411
4412       if (new_x != orig_x)
4413         x = new_x;
4414     }
4415
4416   return x;
4417 }
4418
4419
4420 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4421    to be legitimate.  If we find one, return the new, valid address.  */
4422 rtx
4423 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4424 {
4425   if (arm_tls_symbol_p (x))
4426     return legitimize_tls_address (x, NULL_RTX);
4427
4428   if (GET_CODE (x) == PLUS
4429       && GET_CODE (XEXP (x, 1)) == CONST_INT
4430       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4431           || INTVAL (XEXP (x, 1)) < 0))
4432     {
4433       rtx xop0 = XEXP (x, 0);
4434       rtx xop1 = XEXP (x, 1);
4435       HOST_WIDE_INT offset = INTVAL (xop1);
4436
4437       /* Try and fold the offset into a biasing of the base register and
4438          then offsetting that.  Don't do this when optimizing for space
4439          since it can cause too many CSEs.  */
4440       if (optimize_size && offset >= 0
4441           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4442         {
4443           HOST_WIDE_INT delta;
4444
4445           if (offset >= 256)
4446             delta = offset - (256 - GET_MODE_SIZE (mode));
4447           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4448             delta = 31 * GET_MODE_SIZE (mode);
4449           else
4450             delta = offset & (~31 * GET_MODE_SIZE (mode));
4451
4452           xop0 = force_operand (plus_constant (xop0, offset - delta),
4453                                 NULL_RTX);
4454           x = plus_constant (xop0, delta);
4455         }
4456       else if (offset < 0 && offset > -256)
4457         /* Small negative offsets are best done with a subtract before the
4458            dereference, forcing these into a register normally takes two
4459            instructions.  */
4460         x = force_operand (x, NULL_RTX);
4461       else
4462         {
4463           /* For the remaining cases, force the constant into a register.  */
4464           xop1 = force_reg (SImode, xop1);
4465           x = gen_rtx_PLUS (SImode, xop0, xop1);
4466         }
4467     }
4468   else if (GET_CODE (x) == PLUS
4469            && s_register_operand (XEXP (x, 1), SImode)
4470            && !s_register_operand (XEXP (x, 0), SImode))
4471     {
4472       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4473
4474       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4475     }
4476
4477   if (flag_pic)
4478     {
4479       /* We need to find and carefully transform any SYMBOL and LABEL
4480          references; so go back to the original address expression.  */
4481       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4482
4483       if (new_x != orig_x)
4484         x = new_x;
4485     }
4486
4487   return x;
4488 }
4489
4490 rtx
4491 thumb_legitimize_reload_address (rtx *x_p,
4492                                  enum machine_mode mode,
4493                                  int opnum, int type,
4494                                  int ind_levels ATTRIBUTE_UNUSED)
4495 {
4496   rtx x = *x_p;
4497
4498   if (GET_CODE (x) == PLUS
4499       && GET_MODE_SIZE (mode) < 4
4500       && REG_P (XEXP (x, 0))
4501       && XEXP (x, 0) == stack_pointer_rtx
4502       && GET_CODE (XEXP (x, 1)) == CONST_INT
4503       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4504     {
4505       rtx orig_x = x;
4506
4507       x = copy_rtx (x);
4508       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4509                    Pmode, VOIDmode, 0, 0, opnum, type);
4510       return x;
4511     }
4512
4513   /* If both registers are hi-regs, then it's better to reload the
4514      entire expression rather than each register individually.  That
4515      only requires one reload register rather than two.  */
4516   if (GET_CODE (x) == PLUS
4517       && REG_P (XEXP (x, 0))
4518       && REG_P (XEXP (x, 1))
4519       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4520       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4521     {
4522       rtx orig_x = x;
4523
4524       x = copy_rtx (x);
4525       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4526                    Pmode, VOIDmode, 0, 0, opnum, type);
4527       return x;
4528     }
4529
4530   return NULL;
4531 }
4532
4533 /* Test for various thread-local symbols.  */
4534
4535 /* Return TRUE if X is a thread-local symbol.  */
4536
4537 static bool
4538 arm_tls_symbol_p (rtx x)
4539 {
4540   if (! TARGET_HAVE_TLS)
4541     return false;
4542
4543   if (GET_CODE (x) != SYMBOL_REF)
4544     return false;
4545
4546   return SYMBOL_REF_TLS_MODEL (x) != 0;
4547 }
4548
4549 /* Helper for arm_tls_referenced_p.  */
4550
4551 static int
4552 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4553 {
4554   if (GET_CODE (*x) == SYMBOL_REF)
4555     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4556
4557   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4558      TLS offsets, not real symbol references.  */
4559   if (GET_CODE (*x) == UNSPEC
4560       && XINT (*x, 1) == UNSPEC_TLS)
4561     return -1;
4562
4563   return 0;
4564 }
4565
4566 /* Return TRUE if X contains any TLS symbol references.  */
4567
4568 bool
4569 arm_tls_referenced_p (rtx x)
4570 {
4571   if (! TARGET_HAVE_TLS)
4572     return false;
4573
4574   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4575 }
4576 \f
4577 #define REG_OR_SUBREG_REG(X)                                            \
4578   (GET_CODE (X) == REG                                                  \
4579    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4580
4581 #define REG_OR_SUBREG_RTX(X)                    \
4582    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4583
4584 #ifndef COSTS_N_INSNS
4585 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4586 #endif
4587 static inline int
4588 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4589 {
4590   enum machine_mode mode = GET_MODE (x);
4591
4592   switch (code)
4593     {
4594     case ASHIFT:
4595     case ASHIFTRT:
4596     case LSHIFTRT:
4597     case ROTATERT:
4598     case PLUS:
4599     case MINUS:
4600     case COMPARE:
4601     case NEG:
4602     case NOT:
4603       return COSTS_N_INSNS (1);
4604
4605     case MULT:
4606       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4607         {
4608           int cycles = 0;
4609           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4610
4611           while (i)
4612             {
4613               i >>= 2;
4614               cycles++;
4615             }
4616           return COSTS_N_INSNS (2) + cycles;
4617         }
4618       return COSTS_N_INSNS (1) + 16;
4619
4620     case SET:
4621       return (COSTS_N_INSNS (1)
4622               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4623                      + GET_CODE (SET_DEST (x)) == MEM));
4624
4625     case CONST_INT:
4626       if (outer == SET)
4627         {
4628           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4629             return 0;
4630           if (thumb_shiftable_const (INTVAL (x)))
4631             return COSTS_N_INSNS (2);
4632           return COSTS_N_INSNS (3);
4633         }
4634       else if ((outer == PLUS || outer == COMPARE)
4635                && INTVAL (x) < 256 && INTVAL (x) > -256)
4636         return 0;
4637       else if (outer == AND
4638                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4639         return COSTS_N_INSNS (1);
4640       else if (outer == ASHIFT || outer == ASHIFTRT
4641                || outer == LSHIFTRT)
4642         return 0;
4643       return COSTS_N_INSNS (2);
4644
4645     case CONST:
4646     case CONST_DOUBLE:
4647     case LABEL_REF:
4648     case SYMBOL_REF:
4649       return COSTS_N_INSNS (3);
4650
4651     case UDIV:
4652     case UMOD:
4653     case DIV:
4654     case MOD:
4655       return 100;
4656
4657     case TRUNCATE:
4658       return 99;
4659
4660     case AND:
4661     case XOR:
4662     case IOR:
4663       /* XXX guess.  */
4664       return 8;
4665
4666     case MEM:
4667       /* XXX another guess.  */
4668       /* Memory costs quite a lot for the first word, but subsequent words
4669          load at the equivalent of a single insn each.  */
4670       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4671               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4672                  ? 4 : 0));
4673
4674     case IF_THEN_ELSE:
4675       /* XXX a guess.  */
4676       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4677         return 14;
4678       return 2;
4679
4680     case ZERO_EXTEND:
4681       /* XXX still guessing.  */
4682       switch (GET_MODE (XEXP (x, 0)))
4683         {
4684         case QImode:
4685           return (1 + (mode == DImode ? 4 : 0)
4686                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4687
4688         case HImode:
4689           return (4 + (mode == DImode ? 4 : 0)
4690                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4691
4692         case SImode:
4693           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4694
4695         default:
4696           return 99;
4697         }
4698
4699     default:
4700       return 99;
4701     }
4702 }
4703
4704
4705 /* Worker routine for arm_rtx_costs.  */
4706 /* ??? This needs updating for thumb2.  */
4707 static inline int
4708 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4709 {
4710   enum machine_mode mode = GET_MODE (x);
4711   enum rtx_code subcode;
4712   int extra_cost;
4713
4714   switch (code)
4715     {
4716     case MEM:
4717       /* Memory costs quite a lot for the first word, but subsequent words
4718          load at the equivalent of a single insn each.  */
4719       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4720               + (GET_CODE (x) == SYMBOL_REF
4721                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4722
4723     case DIV:
4724     case MOD:
4725     case UDIV:
4726     case UMOD:
4727       return optimize_size ? COSTS_N_INSNS (2) : 100;
4728
4729     case ROTATE:
4730       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4731         return 4;
4732       /* Fall through */
4733     case ROTATERT:
4734       if (mode != SImode)
4735         return 8;
4736       /* Fall through */
4737     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4738       if (mode == DImode)
4739         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4740                 + ((GET_CODE (XEXP (x, 0)) == REG
4741                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4742                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4743                    ? 0 : 8));
4744       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4745                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4746                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4747                    ? 0 : 4)
4748               + ((GET_CODE (XEXP (x, 1)) == REG
4749                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4750                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4751                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4752                  ? 0 : 4));
4753
4754     case MINUS:
4755       if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4756         {
4757           extra_cost = rtx_cost (XEXP (x, 1), code);
4758           if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4759             extra_cost += 4 * ARM_NUM_REGS (mode);
4760           return extra_cost;
4761         }
4762
4763       if (mode == DImode)
4764         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4765                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4766                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4767                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4768                    ? 0 : 8));
4769
4770       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4771         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4772                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4773                           && arm_const_double_rtx (XEXP (x, 1))))
4774                      ? 0 : 8)
4775                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4776                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4777                         && arm_const_double_rtx (XEXP (x, 0))))
4778                    ? 0 : 8));
4779
4780       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4781             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4782             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4783           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4784                || subcode == ASHIFTRT || subcode == LSHIFTRT
4785                || subcode == ROTATE || subcode == ROTATERT
4786                || (subcode == MULT
4787                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4788                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4789                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4790               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4791               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4792                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4793               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4794         return 1;
4795       /* Fall through */
4796
4797     case PLUS:
4798       if (GET_CODE (XEXP (x, 0)) == MULT)
4799         {
4800           extra_cost = rtx_cost (XEXP (x, 0), code);
4801           if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4802             extra_cost += 4 * ARM_NUM_REGS (mode);
4803           return extra_cost;
4804         }
4805
4806       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4807         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4808                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4809                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4810                         && arm_const_double_rtx (XEXP (x, 1))))
4811                    ? 0 : 8));
4812
4813       /* Fall through */
4814     case AND: case XOR: case IOR:
4815       extra_cost = 0;
4816
4817       /* Normally the frame registers will be spilt into reg+const during
4818          reload, so it is a bad idea to combine them with other instructions,
4819          since then they might not be moved outside of loops.  As a compromise
4820          we allow integration with ops that have a constant as their second
4821          operand.  */
4822       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4823            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4824            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4825           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4826               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4827         extra_cost = 4;
4828
4829       if (mode == DImode)
4830         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4831                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4832                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4833                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4834                    ? 0 : 8));
4835
4836       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4837         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4838                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4839                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4840                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4841                    ? 0 : 4));
4842
4843       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4844         return (1 + extra_cost
4845                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4846                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4847                      || subcode == ROTATE || subcode == ROTATERT
4848                      || (subcode == MULT
4849                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4850                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4851                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4852                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4853                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4854                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4855                    ? 0 : 4));
4856
4857       return 8;
4858
4859     case MULT:
4860       /* This should have been handled by the CPU specific routines.  */
4861       gcc_unreachable ();
4862
4863     case TRUNCATE:
4864       if (arm_arch3m && mode == SImode
4865           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4866           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4867           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4868               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4869           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4870               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4871         return 8;
4872       return 99;
4873
4874     case NEG:
4875       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4876         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4877       /* Fall through */
4878     case NOT:
4879       if (mode == DImode)
4880         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4881
4882       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4883
4884     case IF_THEN_ELSE:
4885       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4886         return 14;
4887       return 2;
4888
4889     case COMPARE:
4890       return 1;
4891
4892     case ABS:
4893       return 4 + (mode == DImode ? 4 : 0);
4894
4895     case SIGN_EXTEND:
4896       /* ??? value extensions are cheaper on armv6. */
4897       if (GET_MODE (XEXP (x, 0)) == QImode)
4898         return (4 + (mode == DImode ? 4 : 0)
4899                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4900       /* Fall through */
4901     case ZERO_EXTEND:
4902       switch (GET_MODE (XEXP (x, 0)))
4903         {
4904         case QImode:
4905           return (1 + (mode == DImode ? 4 : 0)
4906                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4907
4908         case HImode:
4909           return (4 + (mode == DImode ? 4 : 0)
4910                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4911
4912         case SImode:
4913           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4914
4915         case V8QImode:
4916         case V4HImode:
4917         case V2SImode:
4918         case V4QImode:
4919         case V2HImode:
4920             return 1;
4921
4922         default:
4923           gcc_unreachable ();
4924         }
4925       gcc_unreachable ();
4926
4927     case CONST_INT:
4928       if (const_ok_for_arm (INTVAL (x)))
4929         return outer == SET ? 2 : -1;
4930       else if (outer == AND
4931                && const_ok_for_arm (~INTVAL (x)))
4932         return -1;
4933       else if ((outer == COMPARE
4934                 || outer == PLUS || outer == MINUS)
4935                && const_ok_for_arm (-INTVAL (x)))
4936         return -1;
4937       else
4938         return 5;
4939
4940     case CONST:
4941     case LABEL_REF:
4942     case SYMBOL_REF:
4943       return 6;
4944
4945     case CONST_DOUBLE:
4946       if (arm_const_double_rtx (x))
4947         return outer == SET ? 2 : -1;
4948       else if ((outer == COMPARE || outer == PLUS)
4949                && neg_const_double_rtx_ok_for_fpa (x))
4950         return -1;
4951       return 7;
4952
4953     default:
4954       return 99;
4955     }
4956 }
4957
4958 /* RTX costs when optimizing for size.  */
4959 static bool
4960 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4961 {
4962   enum machine_mode mode = GET_MODE (x);
4963
4964   if (TARGET_THUMB)
4965     {
4966       /* XXX TBD.  For now, use the standard costs.  */
4967       *total = thumb1_rtx_costs (x, code, outer_code);
4968       return true;
4969     }
4970
4971   switch (code)
4972     {
4973     case MEM:
4974       /* A memory access costs 1 insn if the mode is small, or the address is
4975          a single register, otherwise it costs one insn per word.  */
4976       if (REG_P (XEXP (x, 0)))
4977         *total = COSTS_N_INSNS (1);
4978       else
4979         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4980       return true;
4981
4982     case DIV:
4983     case MOD:
4984     case UDIV:
4985     case UMOD:
4986       /* Needs a libcall, so it costs about this.  */
4987       *total = COSTS_N_INSNS (2);
4988       return false;
4989
4990     case ROTATE:
4991       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4992         {
4993           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4994           return true;
4995         }
4996       /* Fall through */
4997     case ROTATERT:
4998     case ASHIFT:
4999     case LSHIFTRT:
5000     case ASHIFTRT:
5001       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5002         {
5003           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
5004           return true;
5005         }
5006       else if (mode == SImode)
5007         {
5008           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
5009           /* Slightly disparage register shifts, but not by much.  */
5010           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5011             *total += 1 + rtx_cost (XEXP (x, 1), code);
5012           return true;
5013         }
5014
5015       /* Needs a libcall.  */
5016       *total = COSTS_N_INSNS (2);
5017       return false;
5018
5019     case MINUS:
5020       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5021         {
5022           *total = COSTS_N_INSNS (1);
5023           return false;
5024         }
5025
5026       if (mode == SImode)
5027         {
5028           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5029           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5030
5031           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5032               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5033               || subcode1 == ROTATE || subcode1 == ROTATERT
5034               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5035               || subcode1 == ASHIFTRT)
5036             {
5037               /* It's just the cost of the two operands.  */
5038               *total = 0;
5039               return false;
5040             }
5041
5042           *total = COSTS_N_INSNS (1);
5043           return false;
5044         }
5045
5046       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5047       return false;
5048
5049     case PLUS:
5050       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5051         {
5052           *total = COSTS_N_INSNS (1);
5053           return false;
5054         }
5055
5056       /* Fall through */
5057     case AND: case XOR: case IOR:
5058       if (mode == SImode)
5059         {
5060           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5061
5062           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5063               || subcode == LSHIFTRT || subcode == ASHIFTRT
5064               || (code == AND && subcode == NOT))
5065             {
5066               /* It's just the cost of the two operands.  */
5067               *total = 0;
5068               return false;
5069             }
5070         }
5071
5072       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5073       return false;
5074
5075     case MULT:
5076       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5077       return false;
5078
5079     case NEG:
5080       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5081         *total = COSTS_N_INSNS (1);
5082       /* Fall through */
5083     case NOT:
5084       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5085
5086       return false;
5087
5088     case IF_THEN_ELSE:
5089       *total = 0;
5090       return false;
5091
5092     case COMPARE:
5093       if (cc_register (XEXP (x, 0), VOIDmode))
5094         * total = 0;
5095       else
5096         *total = COSTS_N_INSNS (1);
5097       return false;
5098
5099     case ABS:
5100       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5101         *total = COSTS_N_INSNS (1);
5102       else
5103         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5104       return false;
5105
5106     case SIGN_EXTEND:
5107       *total = 0;
5108       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5109         {
5110           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5111             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5112         }
5113       if (mode == DImode)
5114         *total += COSTS_N_INSNS (1);
5115       return false;
5116
5117     case ZERO_EXTEND:
5118       *total = 0;
5119       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5120         {
5121           switch (GET_MODE (XEXP (x, 0)))
5122             {
5123             case QImode:
5124               *total += COSTS_N_INSNS (1);
5125               break;
5126
5127             case HImode:
5128               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5129
5130             case SImode:
5131               break;
5132
5133             default:
5134               *total += COSTS_N_INSNS (2);
5135             }
5136         }
5137
5138       if (mode == DImode)
5139         *total += COSTS_N_INSNS (1);
5140
5141       return false;
5142
5143     case CONST_INT:
5144       if (const_ok_for_arm (INTVAL (x)))
5145         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5146       else if (const_ok_for_arm (~INTVAL (x)))
5147         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5148       else if (const_ok_for_arm (-INTVAL (x)))
5149         {
5150           if (outer_code == COMPARE || outer_code == PLUS
5151               || outer_code == MINUS)
5152             *total = 0;
5153           else
5154             *total = COSTS_N_INSNS (1);
5155         }
5156       else
5157         *total = COSTS_N_INSNS (2);
5158       return true;
5159
5160     case CONST:
5161     case LABEL_REF:
5162     case SYMBOL_REF:
5163       *total = COSTS_N_INSNS (2);
5164       return true;
5165
5166     case CONST_DOUBLE:
5167       *total = COSTS_N_INSNS (4);
5168       return true;
5169
5170     default:
5171       if (mode != VOIDmode)
5172         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5173       else
5174         *total = COSTS_N_INSNS (4); /* How knows?  */
5175       return false;
5176     }
5177 }
5178
5179 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5180    supported on any "slowmul" cores, so it can be ignored.  */
5181
5182 static bool
5183 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5184 {
5185   enum machine_mode mode = GET_MODE (x);
5186
5187   if (TARGET_THUMB)
5188     {
5189       *total = thumb1_rtx_costs (x, code, outer_code);
5190       return true;
5191     }
5192
5193   switch (code)
5194     {
5195     case MULT:
5196       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5197           || mode == DImode)
5198         {
5199           *total = 30;
5200           return true;
5201         }
5202
5203       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5204         {
5205           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5206                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5207           int cost, const_ok = const_ok_for_arm (i);
5208           int j, booth_unit_size;
5209
5210           /* Tune as appropriate.  */
5211           cost = const_ok ? 4 : 8;
5212           booth_unit_size = 2;
5213           for (j = 0; i && j < 32; j += booth_unit_size)
5214             {
5215               i >>= booth_unit_size;
5216               cost += 2;
5217             }
5218
5219           *total = cost;
5220           return true;
5221         }
5222
5223       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5224                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5225       return true;
5226
5227     default:
5228       *total = arm_rtx_costs_1 (x, code, outer_code);
5229       return true;
5230     }
5231 }
5232
5233
5234 /* RTX cost for cores with a fast multiply unit (M variants).  */
5235
5236 static bool
5237 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5238 {
5239   enum machine_mode mode = GET_MODE (x);
5240
5241   if (TARGET_THUMB1)
5242     {
5243       *total = thumb1_rtx_costs (x, code, outer_code);
5244       return true;
5245     }
5246
5247   /* ??? should thumb2 use different costs?  */
5248   switch (code)
5249     {
5250     case MULT:
5251       /* There is no point basing this on the tuning, since it is always the
5252          fast variant if it exists at all.  */
5253       if (mode == DImode
5254           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5255           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5256               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5257         {
5258           *total = 8;
5259           return true;
5260         }
5261
5262
5263       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5264           || mode == DImode)
5265         {
5266           *total = 30;
5267           return true;
5268         }
5269
5270       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5271         {
5272           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5273                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5274           int cost, const_ok = const_ok_for_arm (i);
5275           int j, booth_unit_size;
5276
5277           /* Tune as appropriate.  */
5278           cost = const_ok ? 4 : 8;
5279           booth_unit_size = 8;
5280           for (j = 0; i && j < 32; j += booth_unit_size)
5281             {
5282               i >>= booth_unit_size;
5283               cost += 2;
5284             }
5285
5286           *total = cost;
5287           return true;
5288         }
5289
5290       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5291                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5292       return true;
5293
5294     default:
5295       *total = arm_rtx_costs_1 (x, code, outer_code);
5296       return true;
5297     }
5298 }
5299
5300
5301 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5302    so it can be ignored.  */
5303
5304 static bool
5305 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5306 {
5307   enum machine_mode mode = GET_MODE (x);
5308
5309   if (TARGET_THUMB)
5310     {
5311       *total = thumb1_rtx_costs (x, code, outer_code);
5312       return true;
5313     }
5314
5315   switch (code)
5316     {
5317     case MULT:
5318       /* There is no point basing this on the tuning, since it is always the
5319          fast variant if it exists at all.  */
5320       if (mode == DImode
5321           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5322           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5323               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5324         {
5325           *total = 8;
5326           return true;
5327         }
5328
5329
5330       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5331           || mode == DImode)
5332         {
5333           *total = 30;
5334           return true;
5335         }
5336
5337       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5338         {
5339           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5340                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5341           int cost, const_ok = const_ok_for_arm (i);
5342           unsigned HOST_WIDE_INT masked_const;
5343
5344           /* The cost will be related to two insns.
5345              First a load of the constant (MOV or LDR), then a multiply.  */
5346           cost = 2;
5347           if (! const_ok)
5348             cost += 1;      /* LDR is probably more expensive because
5349                                of longer result latency.  */
5350           masked_const = i & 0xffff8000;
5351           if (masked_const != 0 && masked_const != 0xffff8000)
5352             {
5353               masked_const = i & 0xf8000000;
5354               if (masked_const == 0 || masked_const == 0xf8000000)
5355                 cost += 1;
5356               else
5357                 cost += 2;
5358             }
5359           *total = cost;
5360           return true;
5361         }
5362
5363       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5364                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5365       return true;
5366
5367     case COMPARE:
5368       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5369          will stall until the multiplication is complete.  */
5370       if (GET_CODE (XEXP (x, 0)) == MULT)
5371         *total = 4 + rtx_cost (XEXP (x, 0), code);
5372       else
5373         *total = arm_rtx_costs_1 (x, code, outer_code);
5374       return true;
5375
5376     default:
5377       *total = arm_rtx_costs_1 (x, code, outer_code);
5378       return true;
5379     }
5380 }
5381
5382
5383 /* RTX costs for 9e (and later) cores.  */
5384
5385 static bool
5386 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5387 {
5388   enum machine_mode mode = GET_MODE (x);
5389   int nonreg_cost;
5390   int cost;
5391
5392   if (TARGET_THUMB1)
5393     {
5394       switch (code)
5395         {
5396         case MULT:
5397           *total = COSTS_N_INSNS (3);
5398           return true;
5399
5400         default:
5401           *total = thumb1_rtx_costs (x, code, outer_code);
5402           return true;
5403         }
5404     }
5405
5406   switch (code)
5407     {
5408     case MULT:
5409       /* There is no point basing this on the tuning, since it is always the
5410          fast variant if it exists at all.  */
5411       if (mode == DImode
5412           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5413           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5414               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5415         {
5416           *total = 3;
5417           return true;
5418         }
5419
5420
5421       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5422         {
5423           *total = 30;
5424           return true;
5425         }
5426       if (mode == DImode)
5427         {
5428           cost = 7;
5429           nonreg_cost = 8;
5430         }
5431       else
5432         {
5433           cost = 2;
5434           nonreg_cost = 4;
5435         }
5436
5437
5438       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5439                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5440       return true;
5441
5442     default:
5443       *total = arm_rtx_costs_1 (x, code, outer_code);
5444       return true;
5445     }
5446 }
5447 /* All address computations that can be done are free, but rtx cost returns
5448    the same for practically all of them.  So we weight the different types
5449    of address here in the order (most pref first):
5450    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5451 static inline int
5452 arm_arm_address_cost (rtx x)
5453 {
5454   enum rtx_code c  = GET_CODE (x);
5455
5456   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5457     return 0;
5458   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5459     return 10;
5460
5461   if (c == PLUS || c == MINUS)
5462     {
5463       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5464         return 2;
5465
5466       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5467         return 3;
5468
5469       return 4;
5470     }
5471
5472   return 6;
5473 }
5474
5475 static inline int
5476 arm_thumb_address_cost (rtx x)
5477 {
5478   enum rtx_code c  = GET_CODE (x);
5479
5480   if (c == REG)
5481     return 1;
5482   if (c == PLUS
5483       && GET_CODE (XEXP (x, 0)) == REG
5484       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5485     return 1;
5486
5487   return 2;
5488 }
5489
5490 static int
5491 arm_address_cost (rtx x)
5492 {
5493   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5494 }
5495
5496 static int
5497 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5498 {
5499   rtx i_pat, d_pat;
5500
5501   /* Some true dependencies can have a higher cost depending
5502      on precisely how certain input operands are used.  */
5503   if (arm_tune_xscale
5504       && REG_NOTE_KIND (link) == 0
5505       && recog_memoized (insn) >= 0
5506       && recog_memoized (dep) >= 0)
5507     {
5508       int shift_opnum = get_attr_shift (insn);
5509       enum attr_type attr_type = get_attr_type (dep);
5510
5511       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5512          operand for INSN.  If we have a shifted input operand and the
5513          instruction we depend on is another ALU instruction, then we may
5514          have to account for an additional stall.  */
5515       if (shift_opnum != 0
5516           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5517         {
5518           rtx shifted_operand;
5519           int opno;
5520
5521           /* Get the shifted operand.  */
5522           extract_insn (insn);
5523           shifted_operand = recog_data.operand[shift_opnum];
5524
5525           /* Iterate over all the operands in DEP.  If we write an operand
5526              that overlaps with SHIFTED_OPERAND, then we have increase the
5527              cost of this dependency.  */
5528           extract_insn (dep);
5529           preprocess_constraints ();
5530           for (opno = 0; opno < recog_data.n_operands; opno++)
5531             {
5532               /* We can ignore strict inputs.  */
5533               if (recog_data.operand_type[opno] == OP_IN)
5534                 continue;
5535
5536               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5537                                            shifted_operand))
5538                 return 2;
5539             }
5540         }
5541     }
5542
5543   /* XXX This is not strictly true for the FPA.  */
5544   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5545       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5546     return 0;
5547
5548   /* Call insns don't incur a stall, even if they follow a load.  */
5549   if (REG_NOTE_KIND (link) == 0
5550       && GET_CODE (insn) == CALL_INSN)
5551     return 1;
5552
5553   if ((i_pat = single_set (insn)) != NULL
5554       && GET_CODE (SET_SRC (i_pat)) == MEM
5555       && (d_pat = single_set (dep)) != NULL
5556       && GET_CODE (SET_DEST (d_pat)) == MEM)
5557     {
5558       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5559       /* This is a load after a store, there is no conflict if the load reads
5560          from a cached area.  Assume that loads from the stack, and from the
5561          constant pool are cached, and that others will miss.  This is a
5562          hack.  */
5563
5564       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5565           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5566           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5567           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5568         return 1;
5569     }
5570
5571   return cost;
5572 }
5573
5574 static int fp_consts_inited = 0;
5575
5576 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5577 static const char * const strings_fp[8] =
5578 {
5579   "0",   "1",   "2",   "3",
5580   "4",   "5",   "0.5", "10"
5581 };
5582
5583 static REAL_VALUE_TYPE values_fp[8];
5584
5585 static void
5586 init_fp_table (void)
5587 {
5588   int i;
5589   REAL_VALUE_TYPE r;
5590
5591   if (TARGET_VFP)
5592     fp_consts_inited = 1;
5593   else
5594     fp_consts_inited = 8;
5595
5596   for (i = 0; i < fp_consts_inited; i++)
5597     {
5598       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5599       values_fp[i] = r;
5600     }
5601 }
5602
5603 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5604 int
5605 arm_const_double_rtx (rtx x)
5606 {
5607   REAL_VALUE_TYPE r;
5608   int i;
5609
5610   if (!fp_consts_inited)
5611     init_fp_table ();
5612
5613   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5614   if (REAL_VALUE_MINUS_ZERO (r))
5615     return 0;
5616
5617   for (i = 0; i < fp_consts_inited; i++)
5618     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5619       return 1;
5620
5621   return 0;
5622 }
5623
5624 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5625 int
5626 neg_const_double_rtx_ok_for_fpa (rtx x)
5627 {
5628   REAL_VALUE_TYPE r;
5629   int i;
5630
5631   if (!fp_consts_inited)
5632     init_fp_table ();
5633
5634   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5635   r = REAL_VALUE_NEGATE (r);
5636   if (REAL_VALUE_MINUS_ZERO (r))
5637     return 0;
5638
5639   for (i = 0; i < 8; i++)
5640     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5641       return 1;
5642
5643   return 0;
5644 }
5645 \f
5646 /* Predicates for `match_operand' and `match_operator'.  */
5647
5648 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5649 int
5650 cirrus_memory_offset (rtx op)
5651 {
5652   /* Reject eliminable registers.  */
5653   if (! (reload_in_progress || reload_completed)
5654       && (   reg_mentioned_p (frame_pointer_rtx, op)
5655           || reg_mentioned_p (arg_pointer_rtx, op)
5656           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5657           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5658           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5659           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5660     return 0;
5661
5662   if (GET_CODE (op) == MEM)
5663     {
5664       rtx ind;
5665
5666       ind = XEXP (op, 0);
5667
5668       /* Match: (mem (reg)).  */
5669       if (GET_CODE (ind) == REG)
5670         return 1;
5671
5672       /* Match:
5673          (mem (plus (reg)
5674                     (const))).  */
5675       if (GET_CODE (ind) == PLUS
5676           && GET_CODE (XEXP (ind, 0)) == REG
5677           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5678           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5679         return 1;
5680     }
5681
5682   return 0;
5683 }
5684
5685 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5686    WB is true if full writeback address modes are allowed and is false
5687    if limited writeback address modes (POST_INC and PRE_DEC) are
5688    allowed.  */
5689
5690 int
5691 arm_coproc_mem_operand (rtx op, bool wb)
5692 {
5693   rtx ind;
5694
5695   /* Reject eliminable registers.  */
5696   if (! (reload_in_progress || reload_completed)
5697       && (   reg_mentioned_p (frame_pointer_rtx, op)
5698           || reg_mentioned_p (arg_pointer_rtx, op)
5699           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5700           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5701           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5702           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5703     return FALSE;
5704
5705   /* Constants are converted into offsets from labels.  */
5706   if (GET_CODE (op) != MEM)
5707     return FALSE;
5708
5709   ind = XEXP (op, 0);
5710
5711   if (reload_completed
5712       && (GET_CODE (ind) == LABEL_REF
5713           || (GET_CODE (ind) == CONST
5714               && GET_CODE (XEXP (ind, 0)) == PLUS
5715               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5716               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5717     return TRUE;
5718
5719   /* Match: (mem (reg)).  */
5720   if (GET_CODE (ind) == REG)
5721     return arm_address_register_rtx_p (ind, 0);
5722
5723   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
5724      acceptable in any case (subject to verification by
5725      arm_address_register_rtx_p).  We need WB to be true to accept
5726      PRE_INC and POST_DEC.  */
5727   if (GET_CODE (ind) == POST_INC
5728       || GET_CODE (ind) == PRE_DEC
5729       || (wb
5730           && (GET_CODE (ind) == PRE_INC
5731               || GET_CODE (ind) == POST_DEC)))
5732     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5733
5734   if (wb
5735       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5736       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5737       && GET_CODE (XEXP (ind, 1)) == PLUS
5738       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5739     ind = XEXP (ind, 1);
5740
5741   /* Match:
5742      (plus (reg)
5743            (const)).  */
5744   if (GET_CODE (ind) == PLUS
5745       && GET_CODE (XEXP (ind, 0)) == REG
5746       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5747       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5748       && INTVAL (XEXP (ind, 1)) > -1024
5749       && INTVAL (XEXP (ind, 1)) <  1024
5750       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5751     return TRUE;
5752
5753   return FALSE;
5754 }
5755
5756 /* Return true if X is a register that will be eliminated later on.  */
5757 int
5758 arm_eliminable_register (rtx x)
5759 {
5760   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5761                        || REGNO (x) == ARG_POINTER_REGNUM
5762                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5763                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5764 }
5765
5766 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5767    VFP registers.  Otherwise return NO_REGS.  */
5768
5769 enum reg_class
5770 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5771 {
5772   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5773     return NO_REGS;
5774
5775   return GENERAL_REGS;
5776 }
5777
5778 /* Values which must be returned in the most-significant end of the return
5779    register.  */
5780
5781 static bool
5782 arm_return_in_msb (tree valtype)
5783 {
5784   return (TARGET_AAPCS_BASED
5785           && BYTES_BIG_ENDIAN
5786           && (AGGREGATE_TYPE_P (valtype)
5787               || TREE_CODE (valtype) == COMPLEX_TYPE));
5788 }
5789
5790 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5791    Use by the Cirrus Maverick code which has to workaround
5792    a hardware bug triggered by such instructions.  */
5793 static bool
5794 arm_memory_load_p (rtx insn)
5795 {
5796   rtx body, lhs, rhs;;
5797
5798   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5799     return false;
5800
5801   body = PATTERN (insn);
5802
5803   if (GET_CODE (body) != SET)
5804     return false;
5805
5806   lhs = XEXP (body, 0);
5807   rhs = XEXP (body, 1);
5808
5809   lhs = REG_OR_SUBREG_RTX (lhs);
5810
5811   /* If the destination is not a general purpose
5812      register we do not have to worry.  */
5813   if (GET_CODE (lhs) != REG
5814       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5815     return false;
5816
5817   /* As well as loads from memory we also have to react
5818      to loads of invalid constants which will be turned
5819      into loads from the minipool.  */
5820   return (GET_CODE (rhs) == MEM
5821           || GET_CODE (rhs) == SYMBOL_REF
5822           || note_invalid_constants (insn, -1, false));
5823 }
5824
5825 /* Return TRUE if INSN is a Cirrus instruction.  */
5826 static bool
5827 arm_cirrus_insn_p (rtx insn)
5828 {
5829   enum attr_cirrus attr;
5830
5831   /* get_attr cannot accept USE or CLOBBER.  */
5832   if (!insn
5833       || GET_CODE (insn) != INSN
5834       || GET_CODE (PATTERN (insn)) == USE
5835       || GET_CODE (PATTERN (insn)) == CLOBBER)
5836     return 0;
5837
5838   attr = get_attr_cirrus (insn);
5839
5840   return attr != CIRRUS_NOT;
5841 }
5842
5843 /* Cirrus reorg for invalid instruction combinations.  */
5844 static void
5845 cirrus_reorg (rtx first)
5846 {
5847   enum attr_cirrus attr;
5848   rtx body = PATTERN (first);
5849   rtx t;
5850   int nops;
5851
5852   /* Any branch must be followed by 2 non Cirrus instructions.  */
5853   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5854     {
5855       nops = 0;
5856       t = next_nonnote_insn (first);
5857
5858       if (arm_cirrus_insn_p (t))
5859         ++ nops;
5860
5861       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5862         ++ nops;
5863
5864       while (nops --)
5865         emit_insn_after (gen_nop (), first);
5866
5867       return;
5868     }
5869
5870   /* (float (blah)) is in parallel with a clobber.  */
5871   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5872     body = XVECEXP (body, 0, 0);
5873
5874   if (GET_CODE (body) == SET)
5875     {
5876       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5877
5878       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5879          be followed by a non Cirrus insn.  */
5880       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5881         {
5882           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5883             emit_insn_after (gen_nop (), first);
5884
5885           return;
5886         }
5887       else if (arm_memory_load_p (first))
5888         {
5889           unsigned int arm_regno;
5890
5891           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5892              ldr/cfmv64hr combination where the Rd field is the same
5893              in both instructions must be split with a non Cirrus
5894              insn.  Example:
5895
5896              ldr r0, blah
5897              nop
5898              cfmvsr mvf0, r0.  */
5899
5900           /* Get Arm register number for ldr insn.  */
5901           if (GET_CODE (lhs) == REG)
5902             arm_regno = REGNO (lhs);
5903           else
5904             {
5905               gcc_assert (GET_CODE (rhs) == REG);
5906               arm_regno = REGNO (rhs);
5907             }
5908
5909           /* Next insn.  */
5910           first = next_nonnote_insn (first);
5911
5912           if (! arm_cirrus_insn_p (first))
5913             return;
5914
5915           body = PATTERN (first);
5916
5917           /* (float (blah)) is in parallel with a clobber.  */
5918           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5919             body = XVECEXP (body, 0, 0);
5920
5921           if (GET_CODE (body) == FLOAT)
5922             body = XEXP (body, 0);
5923
5924           if (get_attr_cirrus (first) == CIRRUS_MOVE
5925               && GET_CODE (XEXP (body, 1)) == REG
5926               && arm_regno == REGNO (XEXP (body, 1)))
5927             emit_insn_after (gen_nop (), first);
5928
5929           return;
5930         }
5931     }
5932
5933   /* get_attr cannot accept USE or CLOBBER.  */
5934   if (!first
5935       || GET_CODE (first) != INSN
5936       || GET_CODE (PATTERN (first)) == USE
5937       || GET_CODE (PATTERN (first)) == CLOBBER)
5938     return;
5939
5940   attr = get_attr_cirrus (first);
5941
5942   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5943      must be followed by a non-coprocessor instruction.  */
5944   if (attr == CIRRUS_COMPARE)
5945     {
5946       nops = 0;
5947
5948       t = next_nonnote_insn (first);
5949
5950       if (arm_cirrus_insn_p (t))
5951         ++ nops;
5952
5953       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5954         ++ nops;
5955
5956       while (nops --)
5957         emit_insn_after (gen_nop (), first);
5958
5959       return;
5960     }
5961 }
5962
5963 /* Return TRUE if X references a SYMBOL_REF.  */
5964 int
5965 symbol_mentioned_p (rtx x)
5966 {
5967   const char * fmt;
5968   int i;
5969
5970   if (GET_CODE (x) == SYMBOL_REF)
5971     return 1;
5972
5973   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5974      are constant offsets, not symbols.  */
5975   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5976     return 0;
5977
5978   fmt = GET_RTX_FORMAT (GET_CODE (x));
5979
5980   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5981     {
5982       if (fmt[i] == 'E')
5983         {
5984           int j;
5985
5986           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5987             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5988               return 1;
5989         }
5990       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5991         return 1;
5992     }
5993
5994   return 0;
5995 }
5996
5997 /* Return TRUE if X references a LABEL_REF.  */
5998 int
5999 label_mentioned_p (rtx x)
6000 {
6001   const char * fmt;
6002   int i;
6003
6004   if (GET_CODE (x) == LABEL_REF)
6005     return 1;
6006
6007   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
6008      instruction, but they are constant offsets, not symbols.  */
6009   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6010     return 0;
6011
6012   fmt = GET_RTX_FORMAT (GET_CODE (x));
6013   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6014     {
6015       if (fmt[i] == 'E')
6016         {
6017           int j;
6018
6019           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6020             if (label_mentioned_p (XVECEXP (x, i, j)))
6021               return 1;
6022         }
6023       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
6024         return 1;
6025     }
6026
6027   return 0;
6028 }
6029
6030 int
6031 tls_mentioned_p (rtx x)
6032 {
6033   switch (GET_CODE (x))
6034     {
6035     case CONST:
6036       return tls_mentioned_p (XEXP (x, 0));
6037
6038     case UNSPEC:
6039       if (XINT (x, 1) == UNSPEC_TLS)
6040         return 1;
6041
6042     default:
6043       return 0;
6044     }
6045 }
6046
6047 /* Must not copy a SET whose source operand is PC-relative.  */
6048
6049 static bool
6050 arm_cannot_copy_insn_p (rtx insn)
6051 {
6052   rtx pat = PATTERN (insn);
6053
6054   if (GET_CODE (pat) == PARALLEL
6055       && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
6056     {
6057       rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
6058
6059       if (GET_CODE (rhs) == UNSPEC
6060           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6061         return TRUE;
6062
6063       if (GET_CODE (rhs) == MEM
6064           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6065           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6066         return TRUE;
6067     }
6068
6069   return FALSE;
6070 }
6071
6072 enum rtx_code
6073 minmax_code (rtx x)
6074 {
6075   enum rtx_code code = GET_CODE (x);
6076
6077   switch (code)
6078     {
6079     case SMAX:
6080       return GE;
6081     case SMIN:
6082       return LE;
6083     case UMIN:
6084       return LEU;
6085     case UMAX:
6086       return GEU;
6087     default:
6088       gcc_unreachable ();
6089     }
6090 }
6091
6092 /* Return 1 if memory locations are adjacent.  */
6093 int
6094 adjacent_mem_locations (rtx a, rtx b)
6095 {
6096   /* We don't guarantee to preserve the order of these memory refs.  */
6097   if (volatile_refs_p (a) || volatile_refs_p (b))
6098     return 0;
6099
6100   if ((GET_CODE (XEXP (a, 0)) == REG
6101        || (GET_CODE (XEXP (a, 0)) == PLUS
6102            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6103       && (GET_CODE (XEXP (b, 0)) == REG
6104           || (GET_CODE (XEXP (b, 0)) == PLUS
6105               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6106     {
6107       HOST_WIDE_INT val0 = 0, val1 = 0;
6108       rtx reg0, reg1;
6109       int val_diff;
6110
6111       if (GET_CODE (XEXP (a, 0)) == PLUS)
6112         {
6113           reg0 = XEXP (XEXP (a, 0), 0);
6114           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6115         }
6116       else
6117         reg0 = XEXP (a, 0);
6118
6119       if (GET_CODE (XEXP (b, 0)) == PLUS)
6120         {
6121           reg1 = XEXP (XEXP (b, 0), 0);
6122           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6123         }
6124       else
6125         reg1 = XEXP (b, 0);
6126
6127       /* Don't accept any offset that will require multiple
6128          instructions to handle, since this would cause the
6129          arith_adjacentmem pattern to output an overlong sequence.  */
6130       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6131         return 0;
6132
6133       /* Don't allow an eliminable register: register elimination can make
6134          the offset too large.  */
6135       if (arm_eliminable_register (reg0))
6136         return 0;
6137
6138       val_diff = val1 - val0;
6139
6140       if (arm_ld_sched)
6141         {
6142           /* If the target has load delay slots, then there's no benefit
6143              to using an ldm instruction unless the offset is zero and
6144              we are optimizing for size.  */
6145           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6146                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6147                   && (val_diff == 4 || val_diff == -4));
6148         }
6149
6150       return ((REGNO (reg0) == REGNO (reg1))
6151               && (val_diff == 4 || val_diff == -4));
6152     }
6153
6154   return 0;
6155 }
6156
6157 int
6158 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6159                         HOST_WIDE_INT *load_offset)
6160 {
6161   int unsorted_regs[4];
6162   HOST_WIDE_INT unsorted_offsets[4];
6163   int order[4];
6164   int base_reg = -1;
6165   int i;
6166
6167   /* Can only handle 2, 3, or 4 insns at present,
6168      though could be easily extended if required.  */
6169   gcc_assert (nops >= 2 && nops <= 4);
6170
6171   /* Loop over the operands and check that the memory references are
6172      suitable (i.e. immediate offsets from the same base register).  At
6173      the same time, extract the target register, and the memory
6174      offsets.  */
6175   for (i = 0; i < nops; i++)
6176     {
6177       rtx reg;
6178       rtx offset;
6179
6180       /* Convert a subreg of a mem into the mem itself.  */
6181       if (GET_CODE (operands[nops + i]) == SUBREG)
6182         operands[nops + i] = alter_subreg (operands + (nops + i));
6183
6184       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6185
6186       /* Don't reorder volatile memory references; it doesn't seem worth
6187          looking for the case where the order is ok anyway.  */
6188       if (MEM_VOLATILE_P (operands[nops + i]))
6189         return 0;
6190
6191       offset = const0_rtx;
6192
6193       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6194            || (GET_CODE (reg) == SUBREG
6195                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6196           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6197               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6198                    == REG)
6199                   || (GET_CODE (reg) == SUBREG
6200                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6201               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6202                   == CONST_INT)))
6203         {
6204           if (i == 0)
6205             {
6206               base_reg = REGNO (reg);
6207               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6208                                   ? REGNO (operands[i])
6209                                   : REGNO (SUBREG_REG (operands[i])));
6210               order[0] = 0;
6211             }
6212           else
6213             {
6214               if (base_reg != (int) REGNO (reg))
6215                 /* Not addressed from the same base register.  */
6216                 return 0;
6217
6218               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6219                                   ? REGNO (operands[i])
6220                                   : REGNO (SUBREG_REG (operands[i])));
6221               if (unsorted_regs[i] < unsorted_regs[order[0]])
6222                 order[0] = i;
6223             }
6224
6225           /* If it isn't an integer register, or if it overwrites the
6226              base register but isn't the last insn in the list, then
6227              we can't do this.  */
6228           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
6229               || (i != nops - 1 && unsorted_regs[i] == base_reg))
6230             return 0;
6231
6232           unsorted_offsets[i] = INTVAL (offset);
6233         }
6234       else
6235         /* Not a suitable memory address.  */
6236         return 0;
6237     }
6238
6239   /* All the useful information has now been extracted from the
6240      operands into unsorted_regs and unsorted_offsets; additionally,
6241      order[0] has been set to the lowest numbered register in the
6242      list.  Sort the registers into order, and check that the memory
6243      offsets are ascending and adjacent.  */
6244
6245   for (i = 1; i < nops; i++)
6246     {
6247       int j;
6248
6249       order[i] = order[i - 1];
6250       for (j = 0; j < nops; j++)
6251         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6252             && (order[i] == order[i - 1]
6253                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6254           order[i] = j;
6255
6256       /* Have we found a suitable register? if not, one must be used more
6257          than once.  */
6258       if (order[i] == order[i - 1])
6259         return 0;
6260
6261       /* Is the memory address adjacent and ascending? */
6262       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6263         return 0;
6264     }
6265
6266   if (base)
6267     {
6268       *base = base_reg;
6269
6270       for (i = 0; i < nops; i++)
6271         regs[i] = unsorted_regs[order[i]];
6272
6273       *load_offset = unsorted_offsets[order[0]];
6274     }
6275
6276   if (unsorted_offsets[order[0]] == 0)
6277     return 1; /* ldmia */
6278
6279   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
6280     return 2; /* ldmib */
6281
6282   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
6283     return 3; /* ldmda */
6284
6285   if (unsorted_offsets[order[nops - 1]] == -4)
6286     return 4; /* ldmdb */
6287
6288   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
6289      if the offset isn't small enough.  The reason 2 ldrs are faster
6290      is because these ARMs are able to do more than one cache access
6291      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
6292      whilst the ARM8 has a double bandwidth cache.  This means that
6293      these cores can do both an instruction fetch and a data fetch in
6294      a single cycle, so the trick of calculating the address into a
6295      scratch register (one of the result regs) and then doing a load
6296      multiple actually becomes slower (and no smaller in code size).
6297      That is the transformation
6298
6299         ldr     rd1, [rbase + offset]
6300         ldr     rd2, [rbase + offset + 4]
6301
6302      to
6303
6304         add     rd1, rbase, offset
6305         ldmia   rd1, {rd1, rd2}
6306
6307      produces worse code -- '3 cycles + any stalls on rd2' instead of
6308      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
6309      access per cycle, the first sequence could never complete in less
6310      than 6 cycles, whereas the ldm sequence would only take 5 and
6311      would make better use of sequential accesses if not hitting the
6312      cache.
6313
6314      We cheat here and test 'arm_ld_sched' which we currently know to
6315      only be true for the ARM8, ARM9 and StrongARM.  If this ever
6316      changes, then the test below needs to be reworked.  */
6317   if (nops == 2 && arm_ld_sched)
6318     return 0;
6319
6320   /* Can't do it without setting up the offset, only do this if it takes
6321      no more than one insn.  */
6322   return (const_ok_for_arm (unsorted_offsets[order[0]])
6323           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
6324 }
6325
6326 const char *
6327 emit_ldm_seq (rtx *operands, int nops)
6328 {
6329   int regs[4];
6330   int base_reg;
6331   HOST_WIDE_INT offset;
6332   char buf[100];
6333   int i;
6334
6335   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6336     {
6337     case 1:
6338       strcpy (buf, "ldm%(ia%)\t");
6339       break;
6340
6341     case 2:
6342       strcpy (buf, "ldm%(ib%)\t");
6343       break;
6344
6345     case 3:
6346       strcpy (buf, "ldm%(da%)\t");
6347       break;
6348
6349     case 4:
6350       strcpy (buf, "ldm%(db%)\t");
6351       break;
6352
6353     case 5:
6354       if (offset >= 0)
6355         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6356                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6357                  (long) offset);
6358       else
6359         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6360                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6361                  (long) -offset);
6362       output_asm_insn (buf, operands);
6363       base_reg = regs[0];
6364       strcpy (buf, "ldm%(ia%)\t");
6365       break;
6366
6367     default:
6368       gcc_unreachable ();
6369     }
6370
6371   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6372            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6373
6374   for (i = 1; i < nops; i++)
6375     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6376              reg_names[regs[i]]);
6377
6378   strcat (buf, "}\t%@ phole ldm");
6379
6380   output_asm_insn (buf, operands);
6381   return "";
6382 }
6383
6384 int
6385 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6386                          HOST_WIDE_INT * load_offset)
6387 {
6388   int unsorted_regs[4];
6389   HOST_WIDE_INT unsorted_offsets[4];
6390   int order[4];
6391   int base_reg = -1;
6392   int i;
6393
6394   /* Can only handle 2, 3, or 4 insns at present, though could be easily
6395      extended if required.  */
6396   gcc_assert (nops >= 2 && nops <= 4);
6397
6398   /* Loop over the operands and check that the memory references are
6399      suitable (i.e. immediate offsets from the same base register).  At
6400      the same time, extract the target register, and the memory
6401      offsets.  */
6402   for (i = 0; i < nops; i++)
6403     {
6404       rtx reg;
6405       rtx offset;
6406
6407       /* Convert a subreg of a mem into the mem itself.  */
6408       if (GET_CODE (operands[nops + i]) == SUBREG)
6409         operands[nops + i] = alter_subreg (operands + (nops + i));
6410
6411       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6412
6413       /* Don't reorder volatile memory references; it doesn't seem worth
6414          looking for the case where the order is ok anyway.  */
6415       if (MEM_VOLATILE_P (operands[nops + i]))
6416         return 0;
6417
6418       offset = const0_rtx;
6419
6420       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6421            || (GET_CODE (reg) == SUBREG
6422                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6423           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6424               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6425                    == REG)
6426                   || (GET_CODE (reg) == SUBREG
6427                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6428               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6429                   == CONST_INT)))
6430         {
6431           if (i == 0)
6432             {
6433               base_reg = REGNO (reg);
6434               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6435                                   ? REGNO (operands[i])
6436                                   : REGNO (SUBREG_REG (operands[i])));
6437               order[0] = 0;
6438             }
6439           else
6440             {
6441               if (base_reg != (int) REGNO (reg))
6442                 /* Not addressed from the same base register.  */
6443                 return 0;
6444
6445               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6446                                   ? REGNO (operands[i])
6447                                   : REGNO (SUBREG_REG (operands[i])));
6448               if (unsorted_regs[i] < unsorted_regs[order[0]])
6449                 order[0] = i;
6450             }
6451
6452           /* If it isn't an integer register, then we can't do this.  */
6453           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6454             return 0;
6455
6456           unsorted_offsets[i] = INTVAL (offset);
6457         }
6458       else
6459         /* Not a suitable memory address.  */
6460         return 0;
6461     }
6462
6463   /* All the useful information has now been extracted from the
6464      operands into unsorted_regs and unsorted_offsets; additionally,
6465      order[0] has been set to the lowest numbered register in the
6466      list.  Sort the registers into order, and check that the memory
6467      offsets are ascending and adjacent.  */
6468
6469   for (i = 1; i < nops; i++)
6470     {
6471       int j;
6472
6473       order[i] = order[i - 1];
6474       for (j = 0; j < nops; j++)
6475         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6476             && (order[i] == order[i - 1]
6477                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6478           order[i] = j;
6479
6480       /* Have we found a suitable register? if not, one must be used more
6481          than once.  */
6482       if (order[i] == order[i - 1])
6483         return 0;
6484
6485       /* Is the memory address adjacent and ascending? */
6486       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6487         return 0;
6488     }
6489
6490   if (base)
6491     {
6492       *base = base_reg;
6493
6494       for (i = 0; i < nops; i++)
6495         regs[i] = unsorted_regs[order[i]];
6496
6497       *load_offset = unsorted_offsets[order[0]];
6498     }
6499
6500   if (unsorted_offsets[order[0]] == 0)
6501     return 1; /* stmia */
6502
6503   if (unsorted_offsets[order[0]] == 4)
6504     return 2; /* stmib */
6505
6506   if (unsorted_offsets[order[nops - 1]] == 0)
6507     return 3; /* stmda */
6508
6509   if (unsorted_offsets[order[nops - 1]] == -4)
6510     return 4; /* stmdb */
6511
6512   return 0;
6513 }
6514
6515 const char *
6516 emit_stm_seq (rtx *operands, int nops)
6517 {
6518   int regs[4];
6519   int base_reg;
6520   HOST_WIDE_INT offset;
6521   char buf[100];
6522   int i;
6523
6524   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6525     {
6526     case 1:
6527       strcpy (buf, "stm%(ia%)\t");
6528       break;
6529
6530     case 2:
6531       strcpy (buf, "stm%(ib%)\t");
6532       break;
6533
6534     case 3:
6535       strcpy (buf, "stm%(da%)\t");
6536       break;
6537
6538     case 4:
6539       strcpy (buf, "stm%(db%)\t");
6540       break;
6541
6542     default:
6543       gcc_unreachable ();
6544     }
6545
6546   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6547            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6548
6549   for (i = 1; i < nops; i++)
6550     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6551              reg_names[regs[i]]);
6552
6553   strcat (buf, "}\t%@ phole stm");
6554
6555   output_asm_insn (buf, operands);
6556   return "";
6557 }
6558 \f
6559 /* Routines for use in generating RTL.  */
6560
6561 rtx
6562 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6563                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6564 {
6565   HOST_WIDE_INT offset = *offsetp;
6566   int i = 0, j;
6567   rtx result;
6568   int sign = up ? 1 : -1;
6569   rtx mem, addr;
6570
6571   /* XScale has load-store double instructions, but they have stricter
6572      alignment requirements than load-store multiple, so we cannot
6573      use them.
6574
6575      For XScale ldm requires 2 + NREGS cycles to complete and blocks
6576      the pipeline until completion.
6577
6578         NREGS           CYCLES
6579           1               3
6580           2               4
6581           3               5
6582           4               6
6583
6584      An ldr instruction takes 1-3 cycles, but does not block the
6585      pipeline.
6586
6587         NREGS           CYCLES
6588           1              1-3
6589           2              2-6
6590           3              3-9
6591           4              4-12
6592
6593      Best case ldr will always win.  However, the more ldr instructions
6594      we issue, the less likely we are to be able to schedule them well.
6595      Using ldr instructions also increases code size.
6596
6597      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6598      for counts of 3 or 4 regs.  */
6599   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6600     {
6601       rtx seq;
6602
6603       start_sequence ();
6604
6605       for (i = 0; i < count; i++)
6606         {
6607           addr = plus_constant (from, i * 4 * sign);
6608           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6609           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6610           offset += 4 * sign;
6611         }
6612
6613       if (write_back)
6614         {
6615           emit_move_insn (from, plus_constant (from, count * 4 * sign));
6616           *offsetp = offset;
6617         }
6618
6619       seq = get_insns ();
6620       end_sequence ();
6621
6622       return seq;
6623     }
6624
6625   result = gen_rtx_PARALLEL (VOIDmode,
6626                              rtvec_alloc (count + (write_back ? 1 : 0)));
6627   if (write_back)
6628     {
6629       XVECEXP (result, 0, 0)
6630         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6631       i = 1;
6632       count++;
6633     }
6634
6635   for (j = 0; i < count; i++, j++)
6636     {
6637       addr = plus_constant (from, j * 4 * sign);
6638       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6639       XVECEXP (result, 0, i)
6640         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6641       offset += 4 * sign;
6642     }
6643
6644   if (write_back)
6645     *offsetp = offset;
6646
6647   return result;
6648 }
6649
6650 rtx
6651 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6652                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6653 {
6654   HOST_WIDE_INT offset = *offsetp;
6655   int i = 0, j;
6656   rtx result;
6657   int sign = up ? 1 : -1;
6658   rtx mem, addr;
6659
6660   /* See arm_gen_load_multiple for discussion of
6661      the pros/cons of ldm/stm usage for XScale.  */
6662   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6663     {
6664       rtx seq;
6665
6666       start_sequence ();
6667
6668       for (i = 0; i < count; i++)
6669         {
6670           addr = plus_constant (to, i * 4 * sign);
6671           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6672           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6673           offset += 4 * sign;
6674         }
6675
6676       if (write_back)
6677         {
6678           emit_move_insn (to, plus_constant (to, count * 4 * sign));
6679           *offsetp = offset;
6680         }
6681
6682       seq = get_insns ();
6683       end_sequence ();
6684
6685       return seq;
6686     }
6687
6688   result = gen_rtx_PARALLEL (VOIDmode,
6689                              rtvec_alloc (count + (write_back ? 1 : 0)));
6690   if (write_back)
6691     {
6692       XVECEXP (result, 0, 0)
6693         = gen_rtx_SET (VOIDmode, to,
6694                        plus_constant (to, count * 4 * sign));
6695       i = 1;
6696       count++;
6697     }
6698
6699   for (j = 0; i < count; i++, j++)
6700     {
6701       addr = plus_constant (to, j * 4 * sign);
6702       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6703       XVECEXP (result, 0, i)
6704         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6705       offset += 4 * sign;
6706     }
6707
6708   if (write_back)
6709     *offsetp = offset;
6710
6711   return result;
6712 }
6713
6714 int
6715 arm_gen_movmemqi (rtx *operands)
6716 {
6717   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6718   HOST_WIDE_INT srcoffset, dstoffset;
6719   int i;
6720   rtx src, dst, srcbase, dstbase;
6721   rtx part_bytes_reg = NULL;
6722   rtx mem;
6723
6724   if (GET_CODE (operands[2]) != CONST_INT
6725       || GET_CODE (operands[3]) != CONST_INT
6726       || INTVAL (operands[2]) > 64
6727       || INTVAL (operands[3]) & 3)
6728     return 0;
6729
6730   dstbase = operands[0];
6731   srcbase = operands[1];
6732
6733   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6734   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6735
6736   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6737   out_words_to_go = INTVAL (operands[2]) / 4;
6738   last_bytes = INTVAL (operands[2]) & 3;
6739   dstoffset = srcoffset = 0;
6740
6741   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6742     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6743
6744   for (i = 0; in_words_to_go >= 2; i+=4)
6745     {
6746       if (in_words_to_go > 4)
6747         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6748                                           srcbase, &srcoffset));
6749       else
6750         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6751                                           FALSE, srcbase, &srcoffset));
6752
6753       if (out_words_to_go)
6754         {
6755           if (out_words_to_go > 4)
6756             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6757                                                dstbase, &dstoffset));
6758           else if (out_words_to_go != 1)
6759             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6760                                                dst, TRUE,
6761                                                (last_bytes == 0
6762                                                 ? FALSE : TRUE),
6763                                                dstbase, &dstoffset));
6764           else
6765             {
6766               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6767               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6768               if (last_bytes != 0)
6769                 {
6770                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6771                   dstoffset += 4;
6772                 }
6773             }
6774         }
6775
6776       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6777       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6778     }
6779
6780   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6781   if (out_words_to_go)
6782     {
6783       rtx sreg;
6784
6785       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6786       sreg = copy_to_reg (mem);
6787
6788       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6789       emit_move_insn (mem, sreg);
6790       in_words_to_go--;
6791
6792       gcc_assert (!in_words_to_go);     /* Sanity check */
6793     }
6794
6795   if (in_words_to_go)
6796     {
6797       gcc_assert (in_words_to_go > 0);
6798
6799       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6800       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6801     }
6802
6803   gcc_assert (!last_bytes || part_bytes_reg);
6804
6805   if (BYTES_BIG_ENDIAN && last_bytes)
6806     {
6807       rtx tmp = gen_reg_rtx (SImode);
6808
6809       /* The bytes we want are in the top end of the word.  */
6810       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6811                               GEN_INT (8 * (4 - last_bytes))));
6812       part_bytes_reg = tmp;
6813
6814       while (last_bytes)
6815         {
6816           mem = adjust_automodify_address (dstbase, QImode,
6817                                            plus_constant (dst, last_bytes - 1),
6818                                            dstoffset + last_bytes - 1);
6819           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6820
6821           if (--last_bytes)
6822             {
6823               tmp = gen_reg_rtx (SImode);
6824               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6825               part_bytes_reg = tmp;
6826             }
6827         }
6828
6829     }
6830   else
6831     {
6832       if (last_bytes > 1)
6833         {
6834           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6835           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6836           last_bytes -= 2;
6837           if (last_bytes)
6838             {
6839               rtx tmp = gen_reg_rtx (SImode);
6840               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6841               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6842               part_bytes_reg = tmp;
6843               dstoffset += 2;
6844             }
6845         }
6846
6847       if (last_bytes)
6848         {
6849           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6850           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6851         }
6852     }
6853
6854   return 1;
6855 }
6856
6857 /* Select a dominance comparison mode if possible for a test of the general
6858    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6859    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6860    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6861    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6862    In all cases OP will be either EQ or NE, but we don't need to know which
6863    here.  If we are unable to support a dominance comparison we return
6864    CC mode.  This will then fail to match for the RTL expressions that
6865    generate this call.  */
6866 enum machine_mode
6867 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6868 {
6869   enum rtx_code cond1, cond2;
6870   int swapped = 0;
6871
6872   /* Currently we will probably get the wrong result if the individual
6873      comparisons are not simple.  This also ensures that it is safe to
6874      reverse a comparison if necessary.  */
6875   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6876        != CCmode)
6877       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6878           != CCmode))
6879     return CCmode;
6880
6881   /* The if_then_else variant of this tests the second condition if the
6882      first passes, but is true if the first fails.  Reverse the first
6883      condition to get a true "inclusive-or" expression.  */
6884   if (cond_or == DOM_CC_NX_OR_Y)
6885     cond1 = reverse_condition (cond1);
6886
6887   /* If the comparisons are not equal, and one doesn't dominate the other,
6888      then we can't do this.  */
6889   if (cond1 != cond2
6890       && !comparison_dominates_p (cond1, cond2)
6891       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6892     return CCmode;
6893
6894   if (swapped)
6895     {
6896       enum rtx_code temp = cond1;
6897       cond1 = cond2;
6898       cond2 = temp;
6899     }
6900
6901   switch (cond1)
6902     {
6903     case EQ:
6904       if (cond_or == DOM_CC_X_AND_Y)
6905         return CC_DEQmode;
6906
6907       switch (cond2)
6908         {
6909         case EQ: return CC_DEQmode;
6910         case LE: return CC_DLEmode;
6911         case LEU: return CC_DLEUmode;
6912         case GE: return CC_DGEmode;
6913         case GEU: return CC_DGEUmode;
6914         default: gcc_unreachable ();
6915         }
6916
6917     case LT:
6918       if (cond_or == DOM_CC_X_AND_Y)
6919         return CC_DLTmode;
6920
6921       switch (cond2)
6922         {
6923         case  LT:
6924             return CC_DLTmode;
6925         case LE:
6926           return CC_DLEmode;
6927         case NE:
6928           return CC_DNEmode;
6929         default:
6930           gcc_unreachable ();
6931         }
6932
6933     case GT:
6934       if (cond_or == DOM_CC_X_AND_Y)
6935         return CC_DGTmode;
6936
6937       switch (cond2)
6938         {
6939         case GT:
6940           return CC_DGTmode;
6941         case GE:
6942           return CC_DGEmode;
6943         case NE:
6944           return CC_DNEmode;
6945         default:
6946           gcc_unreachable ();
6947         }
6948
6949     case LTU:
6950       if (cond_or == DOM_CC_X_AND_Y)
6951         return CC_DLTUmode;
6952
6953       switch (cond2)
6954         {
6955         case LTU:
6956           return CC_DLTUmode;
6957         case LEU:
6958           return CC_DLEUmode;
6959         case NE:
6960           return CC_DNEmode;
6961         default:
6962           gcc_unreachable ();
6963         }
6964
6965     case GTU:
6966       if (cond_or == DOM_CC_X_AND_Y)
6967         return CC_DGTUmode;
6968
6969       switch (cond2)
6970         {
6971         case GTU:
6972           return CC_DGTUmode;
6973         case GEU:
6974           return CC_DGEUmode;
6975         case NE:
6976           return CC_DNEmode;
6977         default:
6978           gcc_unreachable ();
6979         }
6980
6981     /* The remaining cases only occur when both comparisons are the
6982        same.  */
6983     case NE:
6984       gcc_assert (cond1 == cond2);
6985       return CC_DNEmode;
6986
6987     case LE:
6988       gcc_assert (cond1 == cond2);
6989       return CC_DLEmode;
6990
6991     case GE:
6992       gcc_assert (cond1 == cond2);
6993       return CC_DGEmode;
6994
6995     case LEU:
6996       gcc_assert (cond1 == cond2);
6997       return CC_DLEUmode;
6998
6999     case GEU:
7000       gcc_assert (cond1 == cond2);
7001       return CC_DGEUmode;
7002
7003     default:
7004       gcc_unreachable ();
7005     }
7006 }
7007
7008 enum machine_mode
7009 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
7010 {
7011   /* All floating point compares return CCFP if it is an equality
7012      comparison, and CCFPE otherwise.  */
7013   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
7014     {
7015       switch (op)
7016         {
7017         case EQ:
7018         case NE:
7019         case UNORDERED:
7020         case ORDERED:
7021         case UNLT:
7022         case UNLE:
7023         case UNGT:
7024         case UNGE:
7025         case UNEQ:
7026         case LTGT:
7027           return CCFPmode;
7028
7029         case LT:
7030         case LE:
7031         case GT:
7032         case GE:
7033           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
7034             return CCFPmode;
7035           return CCFPEmode;
7036
7037         default:
7038           gcc_unreachable ();
7039         }
7040     }
7041
7042   /* A compare with a shifted operand.  Because of canonicalization, the
7043      comparison will have to be swapped when we emit the assembler.  */
7044   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
7045       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7046           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
7047           || GET_CODE (x) == ROTATERT))
7048     return CC_SWPmode;
7049
7050   /* This operation is performed swapped, but since we only rely on the Z
7051      flag we don't need an additional mode.  */
7052   if (GET_MODE (y) == SImode && REG_P (y)
7053       && GET_CODE (x) == NEG
7054       && (op == EQ || op == NE))
7055     return CC_Zmode;
7056
7057   /* This is a special case that is used by combine to allow a
7058      comparison of a shifted byte load to be split into a zero-extend
7059      followed by a comparison of the shifted integer (only valid for
7060      equalities and unsigned inequalities).  */
7061   if (GET_MODE (x) == SImode
7062       && GET_CODE (x) == ASHIFT
7063       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7064       && GET_CODE (XEXP (x, 0)) == SUBREG
7065       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7066       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7067       && (op == EQ || op == NE
7068           || op == GEU || op == GTU || op == LTU || op == LEU)
7069       && GET_CODE (y) == CONST_INT)
7070     return CC_Zmode;
7071
7072   /* A construct for a conditional compare, if the false arm contains
7073      0, then both conditions must be true, otherwise either condition
7074      must be true.  Not all conditions are possible, so CCmode is
7075      returned if it can't be done.  */
7076   if (GET_CODE (x) == IF_THEN_ELSE
7077       && (XEXP (x, 2) == const0_rtx
7078           || XEXP (x, 2) == const1_rtx)
7079       && COMPARISON_P (XEXP (x, 0))
7080       && COMPARISON_P (XEXP (x, 1)))
7081     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7082                                          INTVAL (XEXP (x, 2)));
7083
7084   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
7085   if (GET_CODE (x) == AND
7086       && COMPARISON_P (XEXP (x, 0))
7087       && COMPARISON_P (XEXP (x, 1)))
7088     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7089                                          DOM_CC_X_AND_Y);
7090
7091   if (GET_CODE (x) == IOR
7092       && COMPARISON_P (XEXP (x, 0))
7093       && COMPARISON_P (XEXP (x, 1)))
7094     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7095                                          DOM_CC_X_OR_Y);
7096
7097   /* An operation (on Thumb) where we want to test for a single bit.
7098      This is done by shifting that bit up into the top bit of a
7099      scratch register; we can then branch on the sign bit.  */
7100   if (TARGET_THUMB1
7101       && GET_MODE (x) == SImode
7102       && (op == EQ || op == NE)
7103       && GET_CODE (x) == ZERO_EXTRACT
7104       && XEXP (x, 1) == const1_rtx)
7105     return CC_Nmode;
7106
7107   /* An operation that sets the condition codes as a side-effect, the
7108      V flag is not set correctly, so we can only use comparisons where
7109      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
7110      instead.)  */
7111   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
7112   if (GET_MODE (x) == SImode
7113       && y == const0_rtx
7114       && (op == EQ || op == NE || op == LT || op == GE)
7115       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7116           || GET_CODE (x) == AND || GET_CODE (x) == IOR
7117           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7118           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7119           || GET_CODE (x) == LSHIFTRT
7120           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7121           || GET_CODE (x) == ROTATERT
7122           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
7123     return CC_NOOVmode;
7124
7125   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7126     return CC_Zmode;
7127
7128   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7129       && GET_CODE (x) == PLUS
7130       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7131     return CC_Cmode;
7132
7133   return CCmode;
7134 }
7135
7136 /* X and Y are two things to compare using CODE.  Emit the compare insn and
7137    return the rtx for register 0 in the proper mode.  FP means this is a
7138    floating point compare: I don't think that it is needed on the arm.  */
7139 rtx
7140 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
7141 {
7142   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
7143   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
7144
7145   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
7146
7147   return cc_reg;
7148 }
7149
7150 /* Generate a sequence of insns that will generate the correct return
7151    address mask depending on the physical architecture that the program
7152    is running on.  */
7153 rtx
7154 arm_gen_return_addr_mask (void)
7155 {
7156   rtx reg = gen_reg_rtx (Pmode);
7157
7158   emit_insn (gen_return_addr_mask (reg));
7159   return reg;
7160 }
7161
7162 void
7163 arm_reload_in_hi (rtx *operands)
7164 {
7165   rtx ref = operands[1];
7166   rtx base, scratch;
7167   HOST_WIDE_INT offset = 0;
7168
7169   if (GET_CODE (ref) == SUBREG)
7170     {
7171       offset = SUBREG_BYTE (ref);
7172       ref = SUBREG_REG (ref);
7173     }
7174
7175   if (GET_CODE (ref) == REG)
7176     {
7177       /* We have a pseudo which has been spilt onto the stack; there
7178          are two cases here: the first where there is a simple
7179          stack-slot replacement and a second where the stack-slot is
7180          out of range, or is used as a subreg.  */
7181       if (reg_equiv_mem[REGNO (ref)])
7182         {
7183           ref = reg_equiv_mem[REGNO (ref)];
7184           base = find_replacement (&XEXP (ref, 0));
7185         }
7186       else
7187         /* The slot is out of range, or was dressed up in a SUBREG.  */
7188         base = reg_equiv_address[REGNO (ref)];
7189     }
7190   else
7191     base = find_replacement (&XEXP (ref, 0));
7192
7193   /* Handle the case where the address is too complex to be offset by 1.  */
7194   if (GET_CODE (base) == MINUS
7195       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7196     {
7197       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7198
7199       emit_set_insn (base_plus, base);
7200       base = base_plus;
7201     }
7202   else if (GET_CODE (base) == PLUS)
7203     {
7204       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7205       HOST_WIDE_INT hi, lo;
7206
7207       offset += INTVAL (XEXP (base, 1));
7208       base = XEXP (base, 0);
7209
7210       /* Rework the address into a legal sequence of insns.  */
7211       /* Valid range for lo is -4095 -> 4095 */
7212       lo = (offset >= 0
7213             ? (offset & 0xfff)
7214             : -((-offset) & 0xfff));
7215
7216       /* Corner case, if lo is the max offset then we would be out of range
7217          once we have added the additional 1 below, so bump the msb into the
7218          pre-loading insn(s).  */
7219       if (lo == 4095)
7220         lo &= 0x7ff;
7221
7222       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7223              ^ (HOST_WIDE_INT) 0x80000000)
7224             - (HOST_WIDE_INT) 0x80000000);
7225
7226       gcc_assert (hi + lo == offset);
7227
7228       if (hi != 0)
7229         {
7230           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7231
7232           /* Get the base address; addsi3 knows how to handle constants
7233              that require more than one insn.  */
7234           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7235           base = base_plus;
7236           offset = lo;
7237         }
7238     }
7239
7240   /* Operands[2] may overlap operands[0] (though it won't overlap
7241      operands[1]), that's why we asked for a DImode reg -- so we can
7242      use the bit that does not overlap.  */
7243   if (REGNO (operands[2]) == REGNO (operands[0]))
7244     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7245   else
7246     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7247
7248   emit_insn (gen_zero_extendqisi2 (scratch,
7249                                    gen_rtx_MEM (QImode,
7250                                                 plus_constant (base,
7251                                                                offset))));
7252   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
7253                                    gen_rtx_MEM (QImode,
7254                                                 plus_constant (base,
7255                                                                offset + 1))));
7256   if (!BYTES_BIG_ENDIAN)
7257     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7258                    gen_rtx_IOR (SImode,
7259                                 gen_rtx_ASHIFT
7260                                 (SImode,
7261                                  gen_rtx_SUBREG (SImode, operands[0], 0),
7262                                  GEN_INT (8)),
7263                                 scratch));
7264   else
7265     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7266                    gen_rtx_IOR (SImode,
7267                                 gen_rtx_ASHIFT (SImode, scratch,
7268                                                 GEN_INT (8)),
7269                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
7270 }
7271
7272 /* Handle storing a half-word to memory during reload by synthesizing as two
7273    byte stores.  Take care not to clobber the input values until after we
7274    have moved them somewhere safe.  This code assumes that if the DImode
7275    scratch in operands[2] overlaps either the input value or output address
7276    in some way, then that value must die in this insn (we absolutely need
7277    two scratch registers for some corner cases).  */
7278 void
7279 arm_reload_out_hi (rtx *operands)
7280 {
7281   rtx ref = operands[0];
7282   rtx outval = operands[1];
7283   rtx base, scratch;
7284   HOST_WIDE_INT offset = 0;
7285
7286   if (GET_CODE (ref) == SUBREG)
7287     {
7288       offset = SUBREG_BYTE (ref);
7289       ref = SUBREG_REG (ref);
7290     }
7291
7292   if (GET_CODE (ref) == REG)
7293     {
7294       /* We have a pseudo which has been spilt onto the stack; there
7295          are two cases here: the first where there is a simple
7296          stack-slot replacement and a second where the stack-slot is
7297          out of range, or is used as a subreg.  */
7298       if (reg_equiv_mem[REGNO (ref)])
7299         {
7300           ref = reg_equiv_mem[REGNO (ref)];
7301           base = find_replacement (&XEXP (ref, 0));
7302         }
7303       else
7304         /* The slot is out of range, or was dressed up in a SUBREG.  */
7305         base = reg_equiv_address[REGNO (ref)];
7306     }
7307   else
7308     base = find_replacement (&XEXP (ref, 0));
7309
7310   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7311
7312   /* Handle the case where the address is too complex to be offset by 1.  */
7313   if (GET_CODE (base) == MINUS
7314       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7315     {
7316       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7317
7318       /* Be careful not to destroy OUTVAL.  */
7319       if (reg_overlap_mentioned_p (base_plus, outval))
7320         {
7321           /* Updating base_plus might destroy outval, see if we can
7322              swap the scratch and base_plus.  */
7323           if (!reg_overlap_mentioned_p (scratch, outval))
7324             {
7325               rtx tmp = scratch;
7326               scratch = base_plus;
7327               base_plus = tmp;
7328             }
7329           else
7330             {
7331               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7332
7333               /* Be conservative and copy OUTVAL into the scratch now,
7334                  this should only be necessary if outval is a subreg
7335                  of something larger than a word.  */
7336               /* XXX Might this clobber base?  I can't see how it can,
7337                  since scratch is known to overlap with OUTVAL, and
7338                  must be wider than a word.  */
7339               emit_insn (gen_movhi (scratch_hi, outval));
7340               outval = scratch_hi;
7341             }
7342         }
7343
7344       emit_set_insn (base_plus, base);
7345       base = base_plus;
7346     }
7347   else if (GET_CODE (base) == PLUS)
7348     {
7349       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7350       HOST_WIDE_INT hi, lo;
7351
7352       offset += INTVAL (XEXP (base, 1));
7353       base = XEXP (base, 0);
7354
7355       /* Rework the address into a legal sequence of insns.  */
7356       /* Valid range for lo is -4095 -> 4095 */
7357       lo = (offset >= 0
7358             ? (offset & 0xfff)
7359             : -((-offset) & 0xfff));
7360
7361       /* Corner case, if lo is the max offset then we would be out of range
7362          once we have added the additional 1 below, so bump the msb into the
7363          pre-loading insn(s).  */
7364       if (lo == 4095)
7365         lo &= 0x7ff;
7366
7367       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7368              ^ (HOST_WIDE_INT) 0x80000000)
7369             - (HOST_WIDE_INT) 0x80000000);
7370
7371       gcc_assert (hi + lo == offset);
7372
7373       if (hi != 0)
7374         {
7375           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7376
7377           /* Be careful not to destroy OUTVAL.  */
7378           if (reg_overlap_mentioned_p (base_plus, outval))
7379             {
7380               /* Updating base_plus might destroy outval, see if we
7381                  can swap the scratch and base_plus.  */
7382               if (!reg_overlap_mentioned_p (scratch, outval))
7383                 {
7384                   rtx tmp = scratch;
7385                   scratch = base_plus;
7386                   base_plus = tmp;
7387                 }
7388               else
7389                 {
7390                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7391
7392                   /* Be conservative and copy outval into scratch now,
7393                      this should only be necessary if outval is a
7394                      subreg of something larger than a word.  */
7395                   /* XXX Might this clobber base?  I can't see how it
7396                      can, since scratch is known to overlap with
7397                      outval.  */
7398                   emit_insn (gen_movhi (scratch_hi, outval));
7399                   outval = scratch_hi;
7400                 }
7401             }
7402
7403           /* Get the base address; addsi3 knows how to handle constants
7404              that require more than one insn.  */
7405           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7406           base = base_plus;
7407           offset = lo;
7408         }
7409     }
7410
7411   if (BYTES_BIG_ENDIAN)
7412     {
7413       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7414                                          plus_constant (base, offset + 1)),
7415                             gen_lowpart (QImode, outval)));
7416       emit_insn (gen_lshrsi3 (scratch,
7417                               gen_rtx_SUBREG (SImode, outval, 0),
7418                               GEN_INT (8)));
7419       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7420                             gen_lowpart (QImode, scratch)));
7421     }
7422   else
7423     {
7424       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7425                             gen_lowpart (QImode, outval)));
7426       emit_insn (gen_lshrsi3 (scratch,
7427                               gen_rtx_SUBREG (SImode, outval, 0),
7428                               GEN_INT (8)));
7429       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7430                                          plus_constant (base, offset + 1)),
7431                             gen_lowpart (QImode, scratch)));
7432     }
7433 }
7434
7435 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7436    (padded to the size of a word) should be passed in a register.  */
7437
7438 static bool
7439 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7440 {
7441   if (TARGET_AAPCS_BASED)
7442     return must_pass_in_stack_var_size (mode, type);
7443   else
7444     return must_pass_in_stack_var_size_or_pad (mode, type);
7445 }
7446
7447
7448 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7449    Return true if an argument passed on the stack should be padded upwards,
7450    i.e. if the least-significant byte has useful data.
7451    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7452    aggregate types are placed in the lowest memory address.  */
7453
7454 bool
7455 arm_pad_arg_upward (enum machine_mode mode, tree type)
7456 {
7457   if (!TARGET_AAPCS_BASED)
7458     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7459
7460   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7461     return false;
7462
7463   return true;
7464 }
7465
7466
7467 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7468    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7469    byte of the register has useful data, and return the opposite if the
7470    most significant byte does.
7471    For AAPCS, small aggregates and small complex types are always padded
7472    upwards.  */
7473
7474 bool
7475 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7476                     tree type, int first ATTRIBUTE_UNUSED)
7477 {
7478   if (TARGET_AAPCS_BASED
7479       && BYTES_BIG_ENDIAN
7480       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7481       && int_size_in_bytes (type) <= 4)
7482     return true;
7483
7484   /* Otherwise, use default padding.  */
7485   return !BYTES_BIG_ENDIAN;
7486 }
7487
7488 \f
7489 /* Print a symbolic form of X to the debug file, F.  */
7490 static void
7491 arm_print_value (FILE *f, rtx x)
7492 {
7493   switch (GET_CODE (x))
7494     {
7495     case CONST_INT:
7496       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7497       return;
7498
7499     case CONST_DOUBLE:
7500       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7501       return;
7502
7503     case CONST_VECTOR:
7504       {
7505         int i;
7506
7507         fprintf (f, "<");
7508         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7509           {
7510             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7511             if (i < (CONST_VECTOR_NUNITS (x) - 1))
7512               fputc (',', f);
7513           }
7514         fprintf (f, ">");
7515       }
7516       return;
7517
7518     case CONST_STRING:
7519       fprintf (f, "\"%s\"", XSTR (x, 0));
7520       return;
7521
7522     case SYMBOL_REF:
7523       fprintf (f, "`%s'", XSTR (x, 0));
7524       return;
7525
7526     case LABEL_REF:
7527       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7528       return;
7529
7530     case CONST:
7531       arm_print_value (f, XEXP (x, 0));
7532       return;
7533
7534     case PLUS:
7535       arm_print_value (f, XEXP (x, 0));
7536       fprintf (f, "+");
7537       arm_print_value (f, XEXP (x, 1));
7538       return;
7539
7540     case PC:
7541       fprintf (f, "pc");
7542       return;
7543
7544     default:
7545       fprintf (f, "????");
7546       return;
7547     }
7548 }
7549 \f
7550 /* Routines for manipulation of the constant pool.  */
7551
7552 /* Arm instructions cannot load a large constant directly into a
7553    register; they have to come from a pc relative load.  The constant
7554    must therefore be placed in the addressable range of the pc
7555    relative load.  Depending on the precise pc relative load
7556    instruction the range is somewhere between 256 bytes and 4k.  This
7557    means that we often have to dump a constant inside a function, and
7558    generate code to branch around it.
7559
7560    It is important to minimize this, since the branches will slow
7561    things down and make the code larger.
7562
7563    Normally we can hide the table after an existing unconditional
7564    branch so that there is no interruption of the flow, but in the
7565    worst case the code looks like this:
7566
7567         ldr     rn, L1
7568         ...
7569         b       L2
7570         align
7571         L1:     .long value
7572         L2:
7573         ...
7574
7575         ldr     rn, L3
7576         ...
7577         b       L4
7578         align
7579         L3:     .long value
7580         L4:
7581         ...
7582
7583    We fix this by performing a scan after scheduling, which notices
7584    which instructions need to have their operands fetched from the
7585    constant table and builds the table.
7586
7587    The algorithm starts by building a table of all the constants that
7588    need fixing up and all the natural barriers in the function (places
7589    where a constant table can be dropped without breaking the flow).
7590    For each fixup we note how far the pc-relative replacement will be
7591    able to reach and the offset of the instruction into the function.
7592
7593    Having built the table we then group the fixes together to form
7594    tables that are as large as possible (subject to addressing
7595    constraints) and emit each table of constants after the last
7596    barrier that is within range of all the instructions in the group.
7597    If a group does not contain a barrier, then we forcibly create one
7598    by inserting a jump instruction into the flow.  Once the table has
7599    been inserted, the insns are then modified to reference the
7600    relevant entry in the pool.
7601
7602    Possible enhancements to the algorithm (not implemented) are:
7603
7604    1) For some processors and object formats, there may be benefit in
7605    aligning the pools to the start of cache lines; this alignment
7606    would need to be taken into account when calculating addressability
7607    of a pool.  */
7608
7609 /* These typedefs are located at the start of this file, so that
7610    they can be used in the prototypes there.  This comment is to
7611    remind readers of that fact so that the following structures
7612    can be understood more easily.
7613
7614      typedef struct minipool_node    Mnode;
7615      typedef struct minipool_fixup   Mfix;  */
7616
7617 struct minipool_node
7618 {
7619   /* Doubly linked chain of entries.  */
7620   Mnode * next;
7621   Mnode * prev;
7622   /* The maximum offset into the code that this entry can be placed.  While
7623      pushing fixes for forward references, all entries are sorted in order
7624      of increasing max_address.  */
7625   HOST_WIDE_INT max_address;
7626   /* Similarly for an entry inserted for a backwards ref.  */
7627   HOST_WIDE_INT min_address;
7628   /* The number of fixes referencing this entry.  This can become zero
7629      if we "unpush" an entry.  In this case we ignore the entry when we
7630      come to emit the code.  */
7631   int refcount;
7632   /* The offset from the start of the minipool.  */
7633   HOST_WIDE_INT offset;
7634   /* The value in table.  */
7635   rtx value;
7636   /* The mode of value.  */
7637   enum machine_mode mode;
7638   /* The size of the value.  With iWMMXt enabled
7639      sizes > 4 also imply an alignment of 8-bytes.  */
7640   int fix_size;
7641 };
7642
7643 struct minipool_fixup
7644 {
7645   Mfix *            next;
7646   rtx               insn;
7647   HOST_WIDE_INT     address;
7648   rtx *             loc;
7649   enum machine_mode mode;
7650   int               fix_size;
7651   rtx               value;
7652   Mnode *           minipool;
7653   HOST_WIDE_INT     forwards;
7654   HOST_WIDE_INT     backwards;
7655 };
7656
7657 /* Fixes less than a word need padding out to a word boundary.  */
7658 #define MINIPOOL_FIX_SIZE(mode) \
7659   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7660
7661 static Mnode *  minipool_vector_head;
7662 static Mnode *  minipool_vector_tail;
7663 static rtx      minipool_vector_label;
7664 static int      minipool_pad;
7665
7666 /* The linked list of all minipool fixes required for this function.  */
7667 Mfix *          minipool_fix_head;
7668 Mfix *          minipool_fix_tail;
7669 /* The fix entry for the current minipool, once it has been placed.  */
7670 Mfix *          minipool_barrier;
7671
7672 /* Determines if INSN is the start of a jump table.  Returns the end
7673    of the TABLE or NULL_RTX.  */
7674 static rtx
7675 is_jump_table (rtx insn)
7676 {
7677   rtx table;
7678
7679   if (GET_CODE (insn) == JUMP_INSN
7680       && JUMP_LABEL (insn) != NULL
7681       && ((table = next_real_insn (JUMP_LABEL (insn)))
7682           == next_real_insn (insn))
7683       && table != NULL
7684       && GET_CODE (table) == JUMP_INSN
7685       && (GET_CODE (PATTERN (table)) == ADDR_VEC
7686           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7687     return table;
7688
7689   return NULL_RTX;
7690 }
7691
7692 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7693 #define JUMP_TABLES_IN_TEXT_SECTION 0
7694 #endif
7695
7696 static HOST_WIDE_INT
7697 get_jump_table_size (rtx insn)
7698 {
7699   /* ADDR_VECs only take room if read-only data does into the text
7700      section.  */
7701   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7702     {
7703       rtx body = PATTERN (insn);
7704       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7705       HOST_WIDE_INT size;
7706       HOST_WIDE_INT modesize;
7707
7708       modesize = GET_MODE_SIZE (GET_MODE (body));
7709       size = modesize * XVECLEN (body, elt);
7710       switch (modesize)
7711         {
7712         case 1:
7713           /* Round up size  of TBB table to a halfword boundary.  */
7714           size = (size + 1) & ~(HOST_WIDE_INT)1;
7715           break;
7716         case 2:
7717           /* No padding necessary for TBH.  */
7718           break;
7719         case 4:
7720           /* Add two bytes for alignment on Thumb.  */
7721           if (TARGET_THUMB)
7722             size += 2;
7723           break;
7724         default:
7725           gcc_unreachable ();
7726         }
7727       return size;
7728     }
7729
7730   return 0;
7731 }
7732
7733 /* Move a minipool fix MP from its current location to before MAX_MP.
7734    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7735    constraints may need updating.  */
7736 static Mnode *
7737 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7738                                HOST_WIDE_INT max_address)
7739 {
7740   /* The code below assumes these are different.  */
7741   gcc_assert (mp != max_mp);
7742
7743   if (max_mp == NULL)
7744     {
7745       if (max_address < mp->max_address)
7746         mp->max_address = max_address;
7747     }
7748   else
7749     {
7750       if (max_address > max_mp->max_address - mp->fix_size)
7751         mp->max_address = max_mp->max_address - mp->fix_size;
7752       else
7753         mp->max_address = max_address;
7754
7755       /* Unlink MP from its current position.  Since max_mp is non-null,
7756        mp->prev must be non-null.  */
7757       mp->prev->next = mp->next;
7758       if (mp->next != NULL)
7759         mp->next->prev = mp->prev;
7760       else
7761         minipool_vector_tail = mp->prev;
7762
7763       /* Re-insert it before MAX_MP.  */
7764       mp->next = max_mp;
7765       mp->prev = max_mp->prev;
7766       max_mp->prev = mp;
7767
7768       if (mp->prev != NULL)
7769         mp->prev->next = mp;
7770       else
7771         minipool_vector_head = mp;
7772     }
7773
7774   /* Save the new entry.  */
7775   max_mp = mp;
7776
7777   /* Scan over the preceding entries and adjust their addresses as
7778      required.  */
7779   while (mp->prev != NULL
7780          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7781     {
7782       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7783       mp = mp->prev;
7784     }
7785
7786   return max_mp;
7787 }
7788
7789 /* Add a constant to the minipool for a forward reference.  Returns the
7790    node added or NULL if the constant will not fit in this pool.  */
7791 static Mnode *
7792 add_minipool_forward_ref (Mfix *fix)
7793 {
7794   /* If set, max_mp is the first pool_entry that has a lower
7795      constraint than the one we are trying to add.  */
7796   Mnode *       max_mp = NULL;
7797   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
7798   Mnode *       mp;
7799
7800   /* If the minipool starts before the end of FIX->INSN then this FIX
7801      can not be placed into the current pool.  Furthermore, adding the
7802      new constant pool entry may cause the pool to start FIX_SIZE bytes
7803      earlier.  */
7804   if (minipool_vector_head &&
7805       (fix->address + get_attr_length (fix->insn)
7806        >= minipool_vector_head->max_address - fix->fix_size))
7807     return NULL;
7808
7809   /* Scan the pool to see if a constant with the same value has
7810      already been added.  While we are doing this, also note the
7811      location where we must insert the constant if it doesn't already
7812      exist.  */
7813   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7814     {
7815       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7816           && fix->mode == mp->mode
7817           && (GET_CODE (fix->value) != CODE_LABEL
7818               || (CODE_LABEL_NUMBER (fix->value)
7819                   == CODE_LABEL_NUMBER (mp->value)))
7820           && rtx_equal_p (fix->value, mp->value))
7821         {
7822           /* More than one fix references this entry.  */
7823           mp->refcount++;
7824           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7825         }
7826
7827       /* Note the insertion point if necessary.  */
7828       if (max_mp == NULL
7829           && mp->max_address > max_address)
7830         max_mp = mp;
7831
7832       /* If we are inserting an 8-bytes aligned quantity and
7833          we have not already found an insertion point, then
7834          make sure that all such 8-byte aligned quantities are
7835          placed at the start of the pool.  */
7836       if (ARM_DOUBLEWORD_ALIGN
7837           && max_mp == NULL
7838           && fix->fix_size == 8
7839           && mp->fix_size != 8)
7840         {
7841           max_mp = mp;
7842           max_address = mp->max_address;
7843         }
7844     }
7845
7846   /* The value is not currently in the minipool, so we need to create
7847      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7848      the end of the list since the placement is less constrained than
7849      any existing entry.  Otherwise, we insert the new fix before
7850      MAX_MP and, if necessary, adjust the constraints on the other
7851      entries.  */
7852   mp = XNEW (Mnode);
7853   mp->fix_size = fix->fix_size;
7854   mp->mode = fix->mode;
7855   mp->value = fix->value;
7856   mp->refcount = 1;
7857   /* Not yet required for a backwards ref.  */
7858   mp->min_address = -65536;
7859
7860   if (max_mp == NULL)
7861     {
7862       mp->max_address = max_address;
7863       mp->next = NULL;
7864       mp->prev = minipool_vector_tail;
7865
7866       if (mp->prev == NULL)
7867         {
7868           minipool_vector_head = mp;
7869           minipool_vector_label = gen_label_rtx ();
7870         }
7871       else
7872         mp->prev->next = mp;
7873
7874       minipool_vector_tail = mp;
7875     }
7876   else
7877     {
7878       if (max_address > max_mp->max_address - mp->fix_size)
7879         mp->max_address = max_mp->max_address - mp->fix_size;
7880       else
7881         mp->max_address = max_address;
7882
7883       mp->next = max_mp;
7884       mp->prev = max_mp->prev;
7885       max_mp->prev = mp;
7886       if (mp->prev != NULL)
7887         mp->prev->next = mp;
7888       else
7889         minipool_vector_head = mp;
7890     }
7891
7892   /* Save the new entry.  */
7893   max_mp = mp;
7894
7895   /* Scan over the preceding entries and adjust their addresses as
7896      required.  */
7897   while (mp->prev != NULL
7898          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7899     {
7900       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7901       mp = mp->prev;
7902     }
7903
7904   return max_mp;
7905 }
7906
7907 static Mnode *
7908 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7909                                 HOST_WIDE_INT  min_address)
7910 {
7911   HOST_WIDE_INT offset;
7912
7913   /* The code below assumes these are different.  */
7914   gcc_assert (mp != min_mp);
7915
7916   if (min_mp == NULL)
7917     {
7918       if (min_address > mp->min_address)
7919         mp->min_address = min_address;
7920     }
7921   else
7922     {
7923       /* We will adjust this below if it is too loose.  */
7924       mp->min_address = min_address;
7925
7926       /* Unlink MP from its current position.  Since min_mp is non-null,
7927          mp->next must be non-null.  */
7928       mp->next->prev = mp->prev;
7929       if (mp->prev != NULL)
7930         mp->prev->next = mp->next;
7931       else
7932         minipool_vector_head = mp->next;
7933
7934       /* Reinsert it after MIN_MP.  */
7935       mp->prev = min_mp;
7936       mp->next = min_mp->next;
7937       min_mp->next = mp;
7938       if (mp->next != NULL)
7939         mp->next->prev = mp;
7940       else
7941         minipool_vector_tail = mp;
7942     }
7943
7944   min_mp = mp;
7945
7946   offset = 0;
7947   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7948     {
7949       mp->offset = offset;
7950       if (mp->refcount > 0)
7951         offset += mp->fix_size;
7952
7953       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7954         mp->next->min_address = mp->min_address + mp->fix_size;
7955     }
7956
7957   return min_mp;
7958 }
7959
7960 /* Add a constant to the minipool for a backward reference.  Returns the
7961    node added or NULL if the constant will not fit in this pool.
7962
7963    Note that the code for insertion for a backwards reference can be
7964    somewhat confusing because the calculated offsets for each fix do
7965    not take into account the size of the pool (which is still under
7966    construction.  */
7967 static Mnode *
7968 add_minipool_backward_ref (Mfix *fix)
7969 {
7970   /* If set, min_mp is the last pool_entry that has a lower constraint
7971      than the one we are trying to add.  */
7972   Mnode *min_mp = NULL;
7973   /* This can be negative, since it is only a constraint.  */
7974   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7975   Mnode *mp;
7976
7977   /* If we can't reach the current pool from this insn, or if we can't
7978      insert this entry at the end of the pool without pushing other
7979      fixes out of range, then we don't try.  This ensures that we
7980      can't fail later on.  */
7981   if (min_address >= minipool_barrier->address
7982       || (minipool_vector_tail->min_address + fix->fix_size
7983           >= minipool_barrier->address))
7984     return NULL;
7985
7986   /* Scan the pool to see if a constant with the same value has
7987      already been added.  While we are doing this, also note the
7988      location where we must insert the constant if it doesn't already
7989      exist.  */
7990   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7991     {
7992       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7993           && fix->mode == mp->mode
7994           && (GET_CODE (fix->value) != CODE_LABEL
7995               || (CODE_LABEL_NUMBER (fix->value)
7996                   == CODE_LABEL_NUMBER (mp->value)))
7997           && rtx_equal_p (fix->value, mp->value)
7998           /* Check that there is enough slack to move this entry to the
7999              end of the table (this is conservative).  */
8000           && (mp->max_address
8001               > (minipool_barrier->address
8002                  + minipool_vector_tail->offset
8003                  + minipool_vector_tail->fix_size)))
8004         {
8005           mp->refcount++;
8006           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
8007         }
8008
8009       if (min_mp != NULL)
8010         mp->min_address += fix->fix_size;
8011       else
8012         {
8013           /* Note the insertion point if necessary.  */
8014           if (mp->min_address < min_address)
8015             {
8016               /* For now, we do not allow the insertion of 8-byte alignment
8017                  requiring nodes anywhere but at the start of the pool.  */
8018               if (ARM_DOUBLEWORD_ALIGN
8019                   && fix->fix_size == 8 && mp->fix_size != 8)
8020                 return NULL;
8021               else
8022                 min_mp = mp;
8023             }
8024           else if (mp->max_address
8025                    < minipool_barrier->address + mp->offset + fix->fix_size)
8026             {
8027               /* Inserting before this entry would push the fix beyond
8028                  its maximum address (which can happen if we have
8029                  re-located a forwards fix); force the new fix to come
8030                  after it.  */
8031               min_mp = mp;
8032               min_address = mp->min_address + fix->fix_size;
8033             }
8034           /* If we are inserting an 8-bytes aligned quantity and
8035              we have not already found an insertion point, then
8036              make sure that all such 8-byte aligned quantities are
8037              placed at the start of the pool.  */
8038           else if (ARM_DOUBLEWORD_ALIGN
8039                    && min_mp == NULL
8040                    && fix->fix_size == 8
8041                    && mp->fix_size < 8)
8042             {
8043               min_mp = mp;
8044               min_address = mp->min_address + fix->fix_size;
8045             }
8046         }
8047     }
8048
8049   /* We need to create a new entry.  */
8050   mp = XNEW (Mnode);
8051   mp->fix_size = fix->fix_size;
8052   mp->mode = fix->mode;
8053   mp->value = fix->value;
8054   mp->refcount = 1;
8055   mp->max_address = minipool_barrier->address + 65536;
8056
8057   mp->min_address = min_address;
8058
8059   if (min_mp == NULL)
8060     {
8061       mp->prev = NULL;
8062       mp->next = minipool_vector_head;
8063
8064       if (mp->next == NULL)
8065         {
8066           minipool_vector_tail = mp;
8067           minipool_vector_label = gen_label_rtx ();
8068         }
8069       else
8070         mp->next->prev = mp;
8071
8072       minipool_vector_head = mp;
8073     }
8074   else
8075     {
8076       mp->next = min_mp->next;
8077       mp->prev = min_mp;
8078       min_mp->next = mp;
8079
8080       if (mp->next != NULL)
8081         mp->next->prev = mp;
8082       else
8083         minipool_vector_tail = mp;
8084     }
8085
8086   /* Save the new entry.  */
8087   min_mp = mp;
8088
8089   if (mp->prev)
8090     mp = mp->prev;
8091   else
8092     mp->offset = 0;
8093
8094   /* Scan over the following entries and adjust their offsets.  */
8095   while (mp->next != NULL)
8096     {
8097       if (mp->next->min_address < mp->min_address + mp->fix_size)
8098         mp->next->min_address = mp->min_address + mp->fix_size;
8099
8100       if (mp->refcount)
8101         mp->next->offset = mp->offset + mp->fix_size;
8102       else
8103         mp->next->offset = mp->offset;
8104
8105       mp = mp->next;
8106     }
8107
8108   return min_mp;
8109 }
8110
8111 static void
8112 assign_minipool_offsets (Mfix *barrier)
8113 {
8114   HOST_WIDE_INT offset = 0;
8115   Mnode *mp;
8116
8117   minipool_barrier = barrier;
8118
8119   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8120     {
8121       mp->offset = offset;
8122
8123       if (mp->refcount > 0)
8124         offset += mp->fix_size;
8125     }
8126 }
8127
8128 /* Output the literal table */
8129 static void
8130 dump_minipool (rtx scan)
8131 {
8132   Mnode * mp;
8133   Mnode * nmp;
8134   int align64 = 0;
8135
8136   if (ARM_DOUBLEWORD_ALIGN)
8137     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8138       if (mp->refcount > 0 && mp->fix_size == 8)
8139         {
8140           align64 = 1;
8141           break;
8142         }
8143
8144   if (dump_file)
8145     fprintf (dump_file,
8146              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8147              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
8148
8149   scan = emit_label_after (gen_label_rtx (), scan);
8150   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
8151   scan = emit_label_after (minipool_vector_label, scan);
8152
8153   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8154     {
8155       if (mp->refcount > 0)
8156         {
8157           if (dump_file)
8158             {
8159               fprintf (dump_file,
8160                        ";;  Offset %u, min %ld, max %ld ",
8161                        (unsigned) mp->offset, (unsigned long) mp->min_address,
8162                        (unsigned long) mp->max_address);
8163               arm_print_value (dump_file, mp->value);
8164               fputc ('\n', dump_file);
8165             }
8166
8167           switch (mp->fix_size)
8168             {
8169 #ifdef HAVE_consttable_1
8170             case 1:
8171               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
8172               break;
8173
8174 #endif
8175 #ifdef HAVE_consttable_2
8176             case 2:
8177               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
8178               break;
8179
8180 #endif
8181 #ifdef HAVE_consttable_4
8182             case 4:
8183               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
8184               break;
8185
8186 #endif
8187 #ifdef HAVE_consttable_8
8188             case 8:
8189               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
8190               break;
8191
8192 #endif
8193             default:
8194               gcc_unreachable ();
8195             }
8196         }
8197
8198       nmp = mp->next;
8199       free (mp);
8200     }
8201
8202   minipool_vector_head = minipool_vector_tail = NULL;
8203   scan = emit_insn_after (gen_consttable_end (), scan);
8204   scan = emit_barrier_after (scan);
8205 }
8206
8207 /* Return the cost of forcibly inserting a barrier after INSN.  */
8208 static int
8209 arm_barrier_cost (rtx insn)
8210 {
8211   /* Basing the location of the pool on the loop depth is preferable,
8212      but at the moment, the basic block information seems to be
8213      corrupt by this stage of the compilation.  */
8214   int base_cost = 50;
8215   rtx next = next_nonnote_insn (insn);
8216
8217   if (next != NULL && GET_CODE (next) == CODE_LABEL)
8218     base_cost -= 20;
8219
8220   switch (GET_CODE (insn))
8221     {
8222     case CODE_LABEL:
8223       /* It will always be better to place the table before the label, rather
8224          than after it.  */
8225       return 50;
8226
8227     case INSN:
8228     case CALL_INSN:
8229       return base_cost;
8230
8231     case JUMP_INSN:
8232       return base_cost - 10;
8233
8234     default:
8235       return base_cost + 10;
8236     }
8237 }
8238
8239 /* Find the best place in the insn stream in the range
8240    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
8241    Create the barrier by inserting a jump and add a new fix entry for
8242    it.  */
8243 static Mfix *
8244 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
8245 {
8246   HOST_WIDE_INT count = 0;
8247   rtx barrier;
8248   rtx from = fix->insn;
8249   /* The instruction after which we will insert the jump.  */
8250   rtx selected = NULL;
8251   int selected_cost;
8252   /* The address at which the jump instruction will be placed.  */
8253   HOST_WIDE_INT selected_address;
8254   Mfix * new_fix;
8255   HOST_WIDE_INT max_count = max_address - fix->address;
8256   rtx label = gen_label_rtx ();
8257
8258   selected_cost = arm_barrier_cost (from);
8259   selected_address = fix->address;
8260
8261   while (from && count < max_count)
8262     {
8263       rtx tmp;
8264       int new_cost;
8265
8266       /* This code shouldn't have been called if there was a natural barrier
8267          within range.  */
8268       gcc_assert (GET_CODE (from) != BARRIER);
8269
8270       /* Count the length of this insn.  */
8271       count += get_attr_length (from);
8272
8273       /* If there is a jump table, add its length.  */
8274       tmp = is_jump_table (from);
8275       if (tmp != NULL)
8276         {
8277           count += get_jump_table_size (tmp);
8278
8279           /* Jump tables aren't in a basic block, so base the cost on
8280              the dispatch insn.  If we select this location, we will
8281              still put the pool after the table.  */
8282           new_cost = arm_barrier_cost (from);
8283
8284           if (count < max_count 
8285               && (!selected || new_cost <= selected_cost))
8286             {
8287               selected = tmp;
8288               selected_cost = new_cost;
8289               selected_address = fix->address + count;
8290             }
8291
8292           /* Continue after the dispatch table.  */
8293           from = NEXT_INSN (tmp);
8294           continue;
8295         }
8296
8297       new_cost = arm_barrier_cost (from);
8298
8299       if (count < max_count
8300           && (!selected || new_cost <= selected_cost))
8301         {
8302           selected = from;
8303           selected_cost = new_cost;
8304           selected_address = fix->address + count;
8305         }
8306
8307       from = NEXT_INSN (from);
8308     }
8309
8310   /* Make sure that we found a place to insert the jump.  */
8311   gcc_assert (selected);
8312
8313   /* Create a new JUMP_INSN that branches around a barrier.  */
8314   from = emit_jump_insn_after (gen_jump (label), selected);
8315   JUMP_LABEL (from) = label;
8316   barrier = emit_barrier_after (from);
8317   emit_label_after (label, barrier);
8318
8319   /* Create a minipool barrier entry for the new barrier.  */
8320   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
8321   new_fix->insn = barrier;
8322   new_fix->address = selected_address;
8323   new_fix->next = fix->next;
8324   fix->next = new_fix;
8325
8326   return new_fix;
8327 }
8328
8329 /* Record that there is a natural barrier in the insn stream at
8330    ADDRESS.  */
8331 static void
8332 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
8333 {
8334   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8335
8336   fix->insn = insn;
8337   fix->address = address;
8338
8339   fix->next = NULL;
8340   if (minipool_fix_head != NULL)
8341     minipool_fix_tail->next = fix;
8342   else
8343     minipool_fix_head = fix;
8344
8345   minipool_fix_tail = fix;
8346 }
8347
8348 /* Record INSN, which will need fixing up to load a value from the
8349    minipool.  ADDRESS is the offset of the insn since the start of the
8350    function; LOC is a pointer to the part of the insn which requires
8351    fixing; VALUE is the constant that must be loaded, which is of type
8352    MODE.  */
8353 static void
8354 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
8355                    enum machine_mode mode, rtx value)
8356 {
8357   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8358
8359 #ifdef AOF_ASSEMBLER
8360   /* PIC symbol references need to be converted into offsets into the
8361      based area.  */
8362   /* XXX This shouldn't be done here.  */
8363   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
8364     value = aof_pic_entry (value);
8365 #endif /* AOF_ASSEMBLER */
8366
8367   fix->insn = insn;
8368   fix->address = address;
8369   fix->loc = loc;
8370   fix->mode = mode;
8371   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
8372   fix->value = value;
8373   fix->forwards = get_attr_pool_range (insn);
8374   fix->backwards = get_attr_neg_pool_range (insn);
8375   fix->minipool = NULL;
8376
8377   /* If an insn doesn't have a range defined for it, then it isn't
8378      expecting to be reworked by this code.  Better to stop now than
8379      to generate duff assembly code.  */
8380   gcc_assert (fix->forwards || fix->backwards);
8381
8382   /* If an entry requires 8-byte alignment then assume all constant pools
8383      require 4 bytes of padding.  Trying to do this later on a per-pool
8384      basis is awkward because existing pool entries have to be modified.  */
8385   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8386     minipool_pad = 4;
8387
8388   if (dump_file)
8389     {
8390       fprintf (dump_file,
8391                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8392                GET_MODE_NAME (mode),
8393                INSN_UID (insn), (unsigned long) address,
8394                -1 * (long)fix->backwards, (long)fix->forwards);
8395       arm_print_value (dump_file, fix->value);
8396       fprintf (dump_file, "\n");
8397     }
8398
8399   /* Add it to the chain of fixes.  */
8400   fix->next = NULL;
8401
8402   if (minipool_fix_head != NULL)
8403     minipool_fix_tail->next = fix;
8404   else
8405     minipool_fix_head = fix;
8406
8407   minipool_fix_tail = fix;
8408 }
8409
8410 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8411    Returns the number of insns needed, or 99 if we don't know how to
8412    do it.  */
8413 int
8414 arm_const_double_inline_cost (rtx val)
8415 {
8416   rtx lowpart, highpart;
8417   enum machine_mode mode;
8418
8419   mode = GET_MODE (val);
8420
8421   if (mode == VOIDmode)
8422     mode = DImode;
8423
8424   gcc_assert (GET_MODE_SIZE (mode) == 8);
8425
8426   lowpart = gen_lowpart (SImode, val);
8427   highpart = gen_highpart_mode (SImode, mode, val);
8428
8429   gcc_assert (GET_CODE (lowpart) == CONST_INT);
8430   gcc_assert (GET_CODE (highpart) == CONST_INT);
8431
8432   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8433                             NULL_RTX, NULL_RTX, 0, 0)
8434           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8435                               NULL_RTX, NULL_RTX, 0, 0));
8436 }
8437
8438 /* Return true if it is worthwhile to split a 64-bit constant into two
8439    32-bit operations.  This is the case if optimizing for size, or
8440    if we have load delay slots, or if one 32-bit part can be done with
8441    a single data operation.  */
8442 bool
8443 arm_const_double_by_parts (rtx val)
8444 {
8445   enum machine_mode mode = GET_MODE (val);
8446   rtx part;
8447
8448   if (optimize_size || arm_ld_sched)
8449     return true;
8450
8451   if (mode == VOIDmode)
8452     mode = DImode;
8453
8454   part = gen_highpart_mode (SImode, mode, val);
8455
8456   gcc_assert (GET_CODE (part) == CONST_INT);
8457
8458   if (const_ok_for_arm (INTVAL (part))
8459       || const_ok_for_arm (~INTVAL (part)))
8460     return true;
8461
8462   part = gen_lowpart (SImode, val);
8463
8464   gcc_assert (GET_CODE (part) == CONST_INT);
8465
8466   if (const_ok_for_arm (INTVAL (part))
8467       || const_ok_for_arm (~INTVAL (part)))
8468     return true;
8469
8470   return false;
8471 }
8472
8473 /* Scan INSN and note any of its operands that need fixing.
8474    If DO_PUSHES is false we do not actually push any of the fixups
8475    needed.  The function returns TRUE if any fixups were needed/pushed.
8476    This is used by arm_memory_load_p() which needs to know about loads
8477    of constants that will be converted into minipool loads.  */
8478 static bool
8479 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8480 {
8481   bool result = false;
8482   int opno;
8483
8484   extract_insn (insn);
8485
8486   if (!constrain_operands (1))
8487     fatal_insn_not_found (insn);
8488
8489   if (recog_data.n_alternatives == 0)
8490     return false;
8491
8492   /* Fill in recog_op_alt with information about the constraints of
8493      this insn.  */
8494   preprocess_constraints ();
8495
8496   for (opno = 0; opno < recog_data.n_operands; opno++)
8497     {
8498       /* Things we need to fix can only occur in inputs.  */
8499       if (recog_data.operand_type[opno] != OP_IN)
8500         continue;
8501
8502       /* If this alternative is a memory reference, then any mention
8503          of constants in this alternative is really to fool reload
8504          into allowing us to accept one there.  We need to fix them up
8505          now so that we output the right code.  */
8506       if (recog_op_alt[opno][which_alternative].memory_ok)
8507         {
8508           rtx op = recog_data.operand[opno];
8509
8510           if (CONSTANT_P (op))
8511             {
8512               if (do_pushes)
8513                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8514                                    recog_data.operand_mode[opno], op);
8515               result = true;
8516             }
8517           else if (GET_CODE (op) == MEM
8518                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8519                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8520             {
8521               if (do_pushes)
8522                 {
8523                   rtx cop = avoid_constant_pool_reference (op);
8524
8525                   /* Casting the address of something to a mode narrower
8526                      than a word can cause avoid_constant_pool_reference()
8527                      to return the pool reference itself.  That's no good to
8528                      us here.  Lets just hope that we can use the
8529                      constant pool value directly.  */
8530                   if (op == cop)
8531                     cop = get_pool_constant (XEXP (op, 0));
8532
8533                   push_minipool_fix (insn, address,
8534                                      recog_data.operand_loc[opno],
8535                                      recog_data.operand_mode[opno], cop);
8536                 }
8537
8538               result = true;
8539             }
8540         }
8541     }
8542
8543   return result;
8544 }
8545
8546 /* Gcc puts the pool in the wrong place for ARM, since we can only
8547    load addresses a limited distance around the pc.  We do some
8548    special munging to move the constant pool values to the correct
8549    point in the code.  */
8550 static void
8551 arm_reorg (void)
8552 {
8553   rtx insn;
8554   HOST_WIDE_INT address = 0;
8555   Mfix * fix;
8556
8557   minipool_fix_head = minipool_fix_tail = NULL;
8558
8559   /* The first insn must always be a note, or the code below won't
8560      scan it properly.  */
8561   insn = get_insns ();
8562   gcc_assert (GET_CODE (insn) == NOTE);
8563   minipool_pad = 0;
8564
8565   /* Scan all the insns and record the operands that will need fixing.  */
8566   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8567     {
8568       if (TARGET_CIRRUS_FIX_INVALID_INSNS
8569           && (arm_cirrus_insn_p (insn)
8570               || GET_CODE (insn) == JUMP_INSN
8571               || arm_memory_load_p (insn)))
8572         cirrus_reorg (insn);
8573
8574       if (GET_CODE (insn) == BARRIER)
8575         push_minipool_barrier (insn, address);
8576       else if (INSN_P (insn))
8577         {
8578           rtx table;
8579
8580           note_invalid_constants (insn, address, true);
8581           address += get_attr_length (insn);
8582
8583           /* If the insn is a vector jump, add the size of the table
8584              and skip the table.  */
8585           if ((table = is_jump_table (insn)) != NULL)
8586             {
8587               address += get_jump_table_size (table);
8588               insn = table;
8589             }
8590         }
8591     }
8592
8593   fix = minipool_fix_head;
8594
8595   /* Now scan the fixups and perform the required changes.  */
8596   while (fix)
8597     {
8598       Mfix * ftmp;
8599       Mfix * fdel;
8600       Mfix *  last_added_fix;
8601       Mfix * last_barrier = NULL;
8602       Mfix * this_fix;
8603
8604       /* Skip any further barriers before the next fix.  */
8605       while (fix && GET_CODE (fix->insn) == BARRIER)
8606         fix = fix->next;
8607
8608       /* No more fixes.  */
8609       if (fix == NULL)
8610         break;
8611
8612       last_added_fix = NULL;
8613
8614       for (ftmp = fix; ftmp; ftmp = ftmp->next)
8615         {
8616           if (GET_CODE (ftmp->insn) == BARRIER)
8617             {
8618               if (ftmp->address >= minipool_vector_head->max_address)
8619                 break;
8620
8621               last_barrier = ftmp;
8622             }
8623           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8624             break;
8625
8626           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8627         }
8628
8629       /* If we found a barrier, drop back to that; any fixes that we
8630          could have reached but come after the barrier will now go in
8631          the next mini-pool.  */
8632       if (last_barrier != NULL)
8633         {
8634           /* Reduce the refcount for those fixes that won't go into this
8635              pool after all.  */
8636           for (fdel = last_barrier->next;
8637                fdel && fdel != ftmp;
8638                fdel = fdel->next)
8639             {
8640               fdel->minipool->refcount--;
8641               fdel->minipool = NULL;
8642             }
8643
8644           ftmp = last_barrier;
8645         }
8646       else
8647         {
8648           /* ftmp is first fix that we can't fit into this pool and
8649              there no natural barriers that we could use.  Insert a
8650              new barrier in the code somewhere between the previous
8651              fix and this one, and arrange to jump around it.  */
8652           HOST_WIDE_INT max_address;
8653
8654           /* The last item on the list of fixes must be a barrier, so
8655              we can never run off the end of the list of fixes without
8656              last_barrier being set.  */
8657           gcc_assert (ftmp);
8658
8659           max_address = minipool_vector_head->max_address;
8660           /* Check that there isn't another fix that is in range that
8661              we couldn't fit into this pool because the pool was
8662              already too large: we need to put the pool before such an
8663              instruction.  The pool itself may come just after the
8664              fix because create_fix_barrier also allows space for a
8665              jump instruction.  */
8666           if (ftmp->address < max_address)
8667             max_address = ftmp->address + 1;
8668
8669           last_barrier = create_fix_barrier (last_added_fix, max_address);
8670         }
8671
8672       assign_minipool_offsets (last_barrier);
8673
8674       while (ftmp)
8675         {
8676           if (GET_CODE (ftmp->insn) != BARRIER
8677               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8678                   == NULL))
8679             break;
8680
8681           ftmp = ftmp->next;
8682         }
8683
8684       /* Scan over the fixes we have identified for this pool, fixing them
8685          up and adding the constants to the pool itself.  */
8686       for (this_fix = fix; this_fix && ftmp != this_fix;
8687            this_fix = this_fix->next)
8688         if (GET_CODE (this_fix->insn) != BARRIER)
8689           {
8690             rtx addr
8691               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8692                                                   minipool_vector_label),
8693                                this_fix->minipool->offset);
8694             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8695           }
8696
8697       dump_minipool (last_barrier->insn);
8698       fix = ftmp;
8699     }
8700
8701   /* From now on we must synthesize any constants that we can't handle
8702      directly.  This can happen if the RTL gets split during final
8703      instruction generation.  */
8704   after_arm_reorg = 1;
8705
8706   /* Free the minipool memory.  */
8707   obstack_free (&minipool_obstack, minipool_startobj);
8708 }
8709 \f
8710 /* Routines to output assembly language.  */
8711
8712 /* If the rtx is the correct value then return the string of the number.
8713    In this way we can ensure that valid double constants are generated even
8714    when cross compiling.  */
8715 const char *
8716 fp_immediate_constant (rtx x)
8717 {
8718   REAL_VALUE_TYPE r;
8719   int i;
8720
8721   if (!fp_consts_inited)
8722     init_fp_table ();
8723
8724   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8725   for (i = 0; i < 8; i++)
8726     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8727       return strings_fp[i];
8728
8729   gcc_unreachable ();
8730 }
8731
8732 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8733 static const char *
8734 fp_const_from_val (REAL_VALUE_TYPE *r)
8735 {
8736   int i;
8737
8738   if (!fp_consts_inited)
8739     init_fp_table ();
8740
8741   for (i = 0; i < 8; i++)
8742     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8743       return strings_fp[i];
8744
8745   gcc_unreachable ();
8746 }
8747
8748 /* Output the operands of a LDM/STM instruction to STREAM.
8749    MASK is the ARM register set mask of which only bits 0-15 are important.
8750    REG is the base register, either the frame pointer or the stack pointer,
8751    INSTR is the possibly suffixed load or store instruction.
8752    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
8753
8754 static void
8755 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8756                  unsigned long mask, int rfe)
8757 {
8758   unsigned i;
8759   bool not_first = FALSE;
8760
8761   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
8762   fputc ('\t', stream);
8763   asm_fprintf (stream, instr, reg);
8764   fputc ('{', stream);
8765
8766   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8767     if (mask & (1 << i))
8768       {
8769         if (not_first)
8770           fprintf (stream, ", ");
8771
8772         asm_fprintf (stream, "%r", i);
8773         not_first = TRUE;
8774       }
8775
8776   if (rfe)
8777     fprintf (stream, "}^\n");
8778   else
8779     fprintf (stream, "}\n");
8780 }
8781
8782
8783 /* Output a FLDMD instruction to STREAM.
8784    BASE if the register containing the address.
8785    REG and COUNT specify the register range.
8786    Extra registers may be added to avoid hardware bugs.
8787
8788    We output FLDMD even for ARMv5 VFP implementations.  Although
8789    FLDMD is technically not supported until ARMv6, it is believed
8790    that all VFP implementations support its use in this context.  */
8791
8792 static void
8793 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
8794 {
8795   int i;
8796
8797   /* Workaround ARM10 VFPr1 bug.  */
8798   if (count == 2 && !arm_arch6)
8799     {
8800       if (reg == 15)
8801         reg--;
8802       count++;
8803     }
8804
8805   fputc ('\t', stream);
8806   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
8807
8808   for (i = reg; i < reg + count; i++)
8809     {
8810       if (i > reg)
8811         fputs (", ", stream);
8812       asm_fprintf (stream, "d%d", i);
8813     }
8814   fputs ("}\n", stream);
8815
8816 }
8817
8818
8819 /* Output the assembly for a store multiple.  */
8820
8821 const char *
8822 vfp_output_fstmd (rtx * operands)
8823 {
8824   char pattern[100];
8825   int p;
8826   int base;
8827   int i;
8828
8829   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
8830   p = strlen (pattern);
8831
8832   gcc_assert (GET_CODE (operands[1]) == REG);
8833
8834   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8835   for (i = 1; i < XVECLEN (operands[2], 0); i++)
8836     {
8837       p += sprintf (&pattern[p], ", d%d", base + i);
8838     }
8839   strcpy (&pattern[p], "}");
8840
8841   output_asm_insn (pattern, operands);
8842   return "";
8843 }
8844
8845
8846 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8847    number of bytes pushed.  */
8848
8849 static int
8850 vfp_emit_fstmd (int base_reg, int count)
8851 {
8852   rtx par;
8853   rtx dwarf;
8854   rtx tmp, reg;
8855   int i;
8856
8857   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8858      register pairs are stored by a store multiple insn.  We avoid this
8859      by pushing an extra pair.  */
8860   if (count == 2 && !arm_arch6)
8861     {
8862       if (base_reg == LAST_VFP_REGNUM - 3)
8863         base_reg -= 2;
8864       count++;
8865     }
8866
8867   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8868   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8869
8870   reg = gen_rtx_REG (DFmode, base_reg);
8871   base_reg += 2;
8872
8873   XVECEXP (par, 0, 0)
8874     = gen_rtx_SET (VOIDmode,
8875                    gen_frame_mem (BLKmode,
8876                                   gen_rtx_PRE_DEC (BLKmode,
8877                                                    stack_pointer_rtx)),
8878                    gen_rtx_UNSPEC (BLKmode,
8879                                    gen_rtvec (1, reg),
8880                                    UNSPEC_PUSH_MULT));
8881
8882   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8883                      plus_constant (stack_pointer_rtx, -(count * 8)));
8884   RTX_FRAME_RELATED_P (tmp) = 1;
8885   XVECEXP (dwarf, 0, 0) = tmp;
8886
8887   tmp = gen_rtx_SET (VOIDmode,
8888                      gen_frame_mem (DFmode, stack_pointer_rtx),
8889                      reg);
8890   RTX_FRAME_RELATED_P (tmp) = 1;
8891   XVECEXP (dwarf, 0, 1) = tmp;
8892
8893   for (i = 1; i < count; i++)
8894     {
8895       reg = gen_rtx_REG (DFmode, base_reg);
8896       base_reg += 2;
8897       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8898
8899       tmp = gen_rtx_SET (VOIDmode,
8900                          gen_frame_mem (DFmode,
8901                                         plus_constant (stack_pointer_rtx,
8902                                                        i * 8)),
8903                          reg);
8904       RTX_FRAME_RELATED_P (tmp) = 1;
8905       XVECEXP (dwarf, 0, i + 1) = tmp;
8906     }
8907
8908   par = emit_insn (par);
8909   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8910                                        REG_NOTES (par));
8911   RTX_FRAME_RELATED_P (par) = 1;
8912
8913   return count * 8;
8914 }
8915
8916
8917 /* Output a 'call' insn.  */
8918 const char *
8919 output_call (rtx *operands)
8920 {
8921   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8922
8923   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8924   if (REGNO (operands[0]) == LR_REGNUM)
8925     {
8926       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8927       output_asm_insn ("mov%?\t%0, %|lr", operands);
8928     }
8929
8930   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8931
8932   if (TARGET_INTERWORK || arm_arch4t)
8933     output_asm_insn ("bx%?\t%0", operands);
8934   else
8935     output_asm_insn ("mov%?\t%|pc, %0", operands);
8936
8937   return "";
8938 }
8939
8940 /* Output a 'call' insn that is a reference in memory.  */
8941 const char *
8942 output_call_mem (rtx *operands)
8943 {
8944   if (TARGET_INTERWORK && !arm_arch5)
8945     {
8946       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8947       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8948       output_asm_insn ("bx%?\t%|ip", operands);
8949     }
8950   else if (regno_use_in (LR_REGNUM, operands[0]))
8951     {
8952       /* LR is used in the memory address.  We load the address in the
8953          first instruction.  It's safe to use IP as the target of the
8954          load since the call will kill it anyway.  */
8955       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8956       if (arm_arch5)
8957         output_asm_insn ("blx%?\t%|ip", operands);
8958       else
8959         {
8960           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8961           if (arm_arch4t)
8962             output_asm_insn ("bx%?\t%|ip", operands);
8963           else
8964             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8965         }
8966     }
8967   else
8968     {
8969       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8970       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8971     }
8972
8973   return "";
8974 }
8975
8976
8977 /* Output a move from arm registers to an fpa registers.
8978    OPERANDS[0] is an fpa register.
8979    OPERANDS[1] is the first registers of an arm register pair.  */
8980 const char *
8981 output_mov_long_double_fpa_from_arm (rtx *operands)
8982 {
8983   int arm_reg0 = REGNO (operands[1]);
8984   rtx ops[3];
8985
8986   gcc_assert (arm_reg0 != IP_REGNUM);
8987
8988   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8989   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8990   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8991
8992   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
8993   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8994
8995   return "";
8996 }
8997
8998 /* Output a move from an fpa register to arm registers.
8999    OPERANDS[0] is the first registers of an arm register pair.
9000    OPERANDS[1] is an fpa register.  */
9001 const char *
9002 output_mov_long_double_arm_from_fpa (rtx *operands)
9003 {
9004   int arm_reg0 = REGNO (operands[0]);
9005   rtx ops[3];
9006
9007   gcc_assert (arm_reg0 != IP_REGNUM);
9008
9009   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9010   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9011   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9012
9013   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
9014   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9015   return "";
9016 }
9017
9018 /* Output a move from arm registers to arm registers of a long double
9019    OPERANDS[0] is the destination.
9020    OPERANDS[1] is the source.  */
9021 const char *
9022 output_mov_long_double_arm_from_arm (rtx *operands)
9023 {
9024   /* We have to be careful here because the two might overlap.  */
9025   int dest_start = REGNO (operands[0]);
9026   int src_start = REGNO (operands[1]);
9027   rtx ops[2];
9028   int i;
9029
9030   if (dest_start < src_start)
9031     {
9032       for (i = 0; i < 3; i++)
9033         {
9034           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9035           ops[1] = gen_rtx_REG (SImode, src_start + i);
9036           output_asm_insn ("mov%?\t%0, %1", ops);
9037         }
9038     }
9039   else
9040     {
9041       for (i = 2; i >= 0; i--)
9042         {
9043           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9044           ops[1] = gen_rtx_REG (SImode, src_start + i);
9045           output_asm_insn ("mov%?\t%0, %1", ops);
9046         }
9047     }
9048
9049   return "";
9050 }
9051
9052
9053 /* Output a move from arm registers to an fpa registers.
9054    OPERANDS[0] is an fpa register.
9055    OPERANDS[1] is the first registers of an arm register pair.  */
9056 const char *
9057 output_mov_double_fpa_from_arm (rtx *operands)
9058 {
9059   int arm_reg0 = REGNO (operands[1]);
9060   rtx ops[2];
9061
9062   gcc_assert (arm_reg0 != IP_REGNUM);
9063
9064   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9065   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9066   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
9067   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
9068   return "";
9069 }
9070
9071 /* Output a move from an fpa register to arm registers.
9072    OPERANDS[0] is the first registers of an arm register pair.
9073    OPERANDS[1] is an fpa register.  */
9074 const char *
9075 output_mov_double_arm_from_fpa (rtx *operands)
9076 {
9077   int arm_reg0 = REGNO (operands[0]);
9078   rtx ops[2];
9079
9080   gcc_assert (arm_reg0 != IP_REGNUM);
9081
9082   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9083   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9084   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
9085   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
9086   return "";
9087 }
9088
9089 /* Output a move between double words.
9090    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9091    or MEM<-REG and all MEMs must be offsettable addresses.  */
9092 const char *
9093 output_move_double (rtx *operands)
9094 {
9095   enum rtx_code code0 = GET_CODE (operands[0]);
9096   enum rtx_code code1 = GET_CODE (operands[1]);
9097   rtx otherops[3];
9098
9099   if (code0 == REG)
9100     {
9101       int reg0 = REGNO (operands[0]);
9102
9103       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9104
9105       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
9106
9107       switch (GET_CODE (XEXP (operands[1], 0)))
9108         {
9109         case REG:
9110           output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
9111           break;
9112
9113         case PRE_INC:
9114           gcc_assert (TARGET_LDRD);
9115           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
9116           break;
9117
9118         case PRE_DEC:
9119           if (TARGET_LDRD)
9120             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
9121           else
9122             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
9123           break;
9124
9125         case POST_INC:
9126           output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
9127           break;
9128
9129         case POST_DEC:
9130           gcc_assert (TARGET_LDRD);
9131           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
9132           break;
9133
9134         case PRE_MODIFY:
9135         case POST_MODIFY:
9136           otherops[0] = operands[0];
9137           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
9138           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
9139
9140           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
9141             {
9142               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9143                 {
9144                   /* Registers overlap so split out the increment.  */
9145                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
9146                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
9147                 }
9148               else
9149                 output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
9150             }
9151           else
9152             {
9153               /* We only allow constant increments, so this is safe.  */
9154               output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
9155             }
9156           break;
9157
9158         case LABEL_REF:
9159         case CONST:
9160           output_asm_insn ("adr%?\t%0, %1", operands);
9161           output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
9162           break;
9163
9164           /* ??? This needs checking for thumb2.  */
9165         default:
9166           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
9167                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
9168             {
9169               otherops[0] = operands[0];
9170               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
9171               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
9172
9173               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
9174                 {
9175                   if (GET_CODE (otherops[2]) == CONST_INT)
9176                     {
9177                       switch ((int) INTVAL (otherops[2]))
9178                         {
9179                         case -8:
9180                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
9181                           return "";
9182                         case -4:
9183                           if (TARGET_THUMB2)
9184                             break;
9185                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
9186                           return "";
9187                         case 4:
9188                           if (TARGET_THUMB2)
9189                             break;
9190                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
9191                           return "";
9192                         }
9193                     }
9194                   if (TARGET_LDRD
9195                       && (GET_CODE (otherops[2]) == REG
9196                           || (GET_CODE (otherops[2]) == CONST_INT
9197                               && INTVAL (otherops[2]) > -256
9198                               && INTVAL (otherops[2]) < 256)))
9199                     {
9200                       if (reg_overlap_mentioned_p (otherops[0],
9201                                                    otherops[2]))
9202                         {
9203                           /* Swap base and index registers over to
9204                              avoid a conflict.  */
9205                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
9206                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
9207                         }
9208                       /* If both registers conflict, it will usually
9209                          have been fixed by a splitter.  */
9210                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9211                         {
9212                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
9213                           output_asm_insn ("ldr%(d%)\t%0, [%1]",
9214                                            otherops);
9215                         }
9216                       else
9217                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
9218                       return "";
9219                     }
9220
9221                   if (GET_CODE (otherops[2]) == CONST_INT)
9222                     {
9223                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
9224                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
9225                       else
9226                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
9227                     }
9228                   else
9229                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
9230                 }
9231               else
9232                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
9233
9234               return "ldm%(ia%)\t%0, %M0";
9235             }
9236           else
9237             {
9238               otherops[1] = adjust_address (operands[1], SImode, 4);
9239               /* Take care of overlapping base/data reg.  */
9240               if (reg_mentioned_p (operands[0], operands[1]))
9241                 {
9242                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9243                   output_asm_insn ("ldr%?\t%0, %1", operands);
9244                 }
9245               else
9246                 {
9247                   output_asm_insn ("ldr%?\t%0, %1", operands);
9248                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9249                 }
9250             }
9251         }
9252     }
9253   else
9254     {
9255       /* Constraints should ensure this.  */
9256       gcc_assert (code0 == MEM && code1 == REG);
9257       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
9258
9259       switch (GET_CODE (XEXP (operands[0], 0)))
9260         {
9261         case REG:
9262           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
9263           break;
9264
9265         case PRE_INC:
9266           gcc_assert (TARGET_LDRD);
9267           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
9268           break;
9269
9270         case PRE_DEC:
9271           if (TARGET_LDRD)
9272             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
9273           else
9274             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
9275           break;
9276
9277         case POST_INC:
9278           output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
9279           break;
9280
9281         case POST_DEC:
9282           gcc_assert (TARGET_LDRD);
9283           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
9284           break;
9285
9286         case PRE_MODIFY:
9287         case POST_MODIFY:
9288           otherops[0] = operands[1];
9289           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
9290           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
9291
9292           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
9293             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
9294           else
9295             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
9296           break;
9297
9298         case PLUS:
9299           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
9300           if (GET_CODE (otherops[2]) == CONST_INT)
9301             {
9302               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
9303                 {
9304                 case -8:
9305                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
9306                   return "";
9307
9308                 case -4:
9309                   if (TARGET_THUMB2)
9310                     break;
9311                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
9312                   return "";
9313
9314                 case 4:
9315                   if (TARGET_THUMB2)
9316                     break;
9317                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
9318                   return "";
9319                 }
9320             }
9321           if (TARGET_LDRD
9322               && (GET_CODE (otherops[2]) == REG
9323                   || (GET_CODE (otherops[2]) == CONST_INT
9324                       && INTVAL (otherops[2]) > -256
9325                       && INTVAL (otherops[2]) < 256)))
9326             {
9327               otherops[0] = operands[1];
9328               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
9329               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
9330               return "";
9331             }
9332           /* Fall through */
9333
9334         default:
9335           otherops[0] = adjust_address (operands[0], SImode, 4);
9336           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9337           output_asm_insn ("str%?\t%1, %0", operands);
9338           output_asm_insn ("str%?\t%1, %0", otherops);
9339         }
9340     }
9341
9342   return "";
9343 }
9344
9345 /* Output a VFP load or store instruction.  */
9346
9347 const char *
9348 output_move_vfp (rtx *operands)
9349 {
9350   rtx reg, mem, addr, ops[2];
9351   int load = REG_P (operands[0]);
9352   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
9353   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
9354   const char *template;
9355   char buff[50];
9356
9357   reg = operands[!load];
9358   mem = operands[load];
9359
9360   gcc_assert (REG_P (reg));
9361   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
9362   gcc_assert (GET_MODE (reg) == SFmode
9363               || GET_MODE (reg) == DFmode
9364               || GET_MODE (reg) == SImode
9365               || GET_MODE (reg) == DImode);
9366   gcc_assert (MEM_P (mem));
9367
9368   addr = XEXP (mem, 0);
9369
9370   switch (GET_CODE (addr))
9371     {
9372     case PRE_DEC:
9373       template = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
9374       ops[0] = XEXP (addr, 0);
9375       ops[1] = reg;
9376       break;
9377
9378     case POST_INC:
9379       template = "f%smia%c%%?\t%%0!, {%%%s1}%s";
9380       ops[0] = XEXP (addr, 0);
9381       ops[1] = reg;
9382       break;
9383
9384     default:
9385       template = "f%s%c%%?\t%%%s0, %%1%s";
9386       ops[0] = reg;
9387       ops[1] = mem;
9388       break;
9389     }
9390
9391   sprintf (buff, template,
9392            load ? "ld" : "st",
9393            dp ? 'd' : 's',
9394            dp ? "P" : "",
9395            integer_p ? "\t%@ int" : "");
9396   output_asm_insn (buff, ops);
9397
9398   return "";
9399 }
9400
9401 /* Output an ADD r, s, #n where n may be too big for one instruction.
9402    If adding zero to one register, output nothing.  */
9403 const char *
9404 output_add_immediate (rtx *operands)
9405 {
9406   HOST_WIDE_INT n = INTVAL (operands[2]);
9407
9408   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
9409     {
9410       if (n < 0)
9411         output_multi_immediate (operands,
9412                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
9413                                 -n);
9414       else
9415         output_multi_immediate (operands,
9416                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
9417                                 n);
9418     }
9419
9420   return "";
9421 }
9422
9423 /* Output a multiple immediate operation.
9424    OPERANDS is the vector of operands referred to in the output patterns.
9425    INSTR1 is the output pattern to use for the first constant.
9426    INSTR2 is the output pattern to use for subsequent constants.
9427    IMMED_OP is the index of the constant slot in OPERANDS.
9428    N is the constant value.  */
9429 static const char *
9430 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
9431                         int immed_op, HOST_WIDE_INT n)
9432 {
9433 #if HOST_BITS_PER_WIDE_INT > 32
9434   n &= 0xffffffff;
9435 #endif
9436
9437   if (n == 0)
9438     {
9439       /* Quick and easy output.  */
9440       operands[immed_op] = const0_rtx;
9441       output_asm_insn (instr1, operands);
9442     }
9443   else
9444     {
9445       int i;
9446       const char * instr = instr1;
9447
9448       /* Note that n is never zero here (which would give no output).  */
9449       for (i = 0; i < 32; i += 2)
9450         {
9451           if (n & (3 << i))
9452             {
9453               operands[immed_op] = GEN_INT (n & (255 << i));
9454               output_asm_insn (instr, operands);
9455               instr = instr2;
9456               i += 6;
9457             }
9458         }
9459     }
9460
9461   return "";
9462 }
9463
9464 /* Return the name of a shifter operation.  */
9465 static const char *
9466 arm_shift_nmem(enum rtx_code code)
9467 {
9468   switch (code)
9469     {
9470     case ASHIFT:
9471       return ARM_LSL_NAME;
9472
9473     case ASHIFTRT:
9474       return "asr";
9475
9476     case LSHIFTRT:
9477       return "lsr";
9478
9479     case ROTATERT:
9480       return "ror";
9481
9482     default:
9483       abort();
9484     }
9485 }
9486
9487 /* Return the appropriate ARM instruction for the operation code.
9488    The returned result should not be overwritten.  OP is the rtx of the
9489    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9490    was shifted.  */
9491 const char *
9492 arithmetic_instr (rtx op, int shift_first_arg)
9493 {
9494   switch (GET_CODE (op))
9495     {
9496     case PLUS:
9497       return "add";
9498
9499     case MINUS:
9500       return shift_first_arg ? "rsb" : "sub";
9501
9502     case IOR:
9503       return "orr";
9504
9505     case XOR:
9506       return "eor";
9507
9508     case AND:
9509       return "and";
9510
9511     case ASHIFT:
9512     case ASHIFTRT:
9513     case LSHIFTRT:
9514     case ROTATERT:
9515       return arm_shift_nmem(GET_CODE(op));
9516
9517     default:
9518       gcc_unreachable ();
9519     }
9520 }
9521
9522 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9523    for the operation code.  The returned result should not be overwritten.
9524    OP is the rtx code of the shift.
9525    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9526    shift.  */
9527 static const char *
9528 shift_op (rtx op, HOST_WIDE_INT *amountp)
9529 {
9530   const char * mnem;
9531   enum rtx_code code = GET_CODE (op);
9532
9533   switch (GET_CODE (XEXP (op, 1)))
9534     {
9535     case REG:
9536     case SUBREG:
9537       *amountp = -1;
9538       break;
9539
9540     case CONST_INT:
9541       *amountp = INTVAL (XEXP (op, 1));
9542       break;
9543
9544     default:
9545       gcc_unreachable ();
9546     }
9547
9548   switch (code)
9549     {
9550     case ROTATE:
9551       gcc_assert (*amountp != -1);
9552       *amountp = 32 - *amountp;
9553       code = ROTATERT;
9554
9555       /* Fall through.  */
9556
9557     case ASHIFT:
9558     case ASHIFTRT:
9559     case LSHIFTRT:
9560     case ROTATERT:
9561       mnem = arm_shift_nmem(code);
9562       break;
9563
9564     case MULT:
9565       /* We never have to worry about the amount being other than a
9566          power of 2, since this case can never be reloaded from a reg.  */
9567       gcc_assert (*amountp != -1);
9568       *amountp = int_log2 (*amountp);
9569       return ARM_LSL_NAME;
9570
9571     default:
9572       gcc_unreachable ();
9573     }
9574
9575   if (*amountp != -1)
9576     {
9577       /* This is not 100% correct, but follows from the desire to merge
9578          multiplication by a power of 2 with the recognizer for a
9579          shift.  >=32 is not a valid shift for "lsl", so we must try and
9580          output a shift that produces the correct arithmetical result.
9581          Using lsr #32 is identical except for the fact that the carry bit
9582          is not set correctly if we set the flags; but we never use the
9583          carry bit from such an operation, so we can ignore that.  */
9584       if (code == ROTATERT)
9585         /* Rotate is just modulo 32.  */
9586         *amountp &= 31;
9587       else if (*amountp != (*amountp & 31))
9588         {
9589           if (code == ASHIFT)
9590             mnem = "lsr";
9591           *amountp = 32;
9592         }
9593
9594       /* Shifts of 0 are no-ops.  */
9595       if (*amountp == 0)
9596         return NULL;
9597     }
9598
9599   return mnem;
9600 }
9601
9602 /* Obtain the shift from the POWER of two.  */
9603
9604 static HOST_WIDE_INT
9605 int_log2 (HOST_WIDE_INT power)
9606 {
9607   HOST_WIDE_INT shift = 0;
9608
9609   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9610     {
9611       gcc_assert (shift <= 31);
9612       shift++;
9613     }
9614
9615   return shift;
9616 }
9617
9618 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
9619    because /bin/as is horribly restrictive.  The judgement about
9620    whether or not each character is 'printable' (and can be output as
9621    is) or not (and must be printed with an octal escape) must be made
9622    with reference to the *host* character set -- the situation is
9623    similar to that discussed in the comments above pp_c_char in
9624    c-pretty-print.c.  */
9625
9626 #define MAX_ASCII_LEN 51
9627
9628 void
9629 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9630 {
9631   int i;
9632   int len_so_far = 0;
9633
9634   fputs ("\t.ascii\t\"", stream);
9635
9636   for (i = 0; i < len; i++)
9637     {
9638       int c = p[i];
9639
9640       if (len_so_far >= MAX_ASCII_LEN)
9641         {
9642           fputs ("\"\n\t.ascii\t\"", stream);
9643           len_so_far = 0;
9644         }
9645
9646       if (ISPRINT (c))
9647         {
9648           if (c == '\\' || c == '\"')
9649             {
9650               putc ('\\', stream);
9651               len_so_far++;
9652             }
9653           putc (c, stream);
9654           len_so_far++;
9655         }
9656       else
9657         {
9658           fprintf (stream, "\\%03o", c);
9659           len_so_far += 4;
9660         }
9661     }
9662
9663   fputs ("\"\n", stream);
9664 }
9665 \f
9666 /* Compute the register save mask for registers 0 through 12
9667    inclusive.  This code is used by arm_compute_save_reg_mask.  */
9668
9669 static unsigned long
9670 arm_compute_save_reg0_reg12_mask (void)
9671 {
9672   unsigned long func_type = arm_current_func_type ();
9673   unsigned long save_reg_mask = 0;
9674   unsigned int reg;
9675
9676   if (IS_INTERRUPT (func_type))
9677     {
9678       unsigned int max_reg;
9679       /* Interrupt functions must not corrupt any registers,
9680          even call clobbered ones.  If this is a leaf function
9681          we can just examine the registers used by the RTL, but
9682          otherwise we have to assume that whatever function is
9683          called might clobber anything, and so we have to save
9684          all the call-clobbered registers as well.  */
9685       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9686         /* FIQ handlers have registers r8 - r12 banked, so
9687            we only need to check r0 - r7, Normal ISRs only
9688            bank r14 and r15, so we must check up to r12.
9689            r13 is the stack pointer which is always preserved,
9690            so we do not need to consider it here.  */
9691         max_reg = 7;
9692       else
9693         max_reg = 12;
9694
9695       for (reg = 0; reg <= max_reg; reg++)
9696         if (regs_ever_live[reg]
9697             || (! current_function_is_leaf && call_used_regs [reg]))
9698           save_reg_mask |= (1 << reg);
9699
9700       /* Also save the pic base register if necessary.  */
9701       if (flag_pic
9702           && !TARGET_SINGLE_PIC_BASE
9703           && arm_pic_register != INVALID_REGNUM
9704           && current_function_uses_pic_offset_table)
9705         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9706     }
9707   else
9708     {
9709       /* In arm mode we handle r11 (FP) as a special case.  */
9710       unsigned last_reg = TARGET_ARM ? 10 : 11;
9711       
9712       /* In the normal case we only need to save those registers
9713          which are call saved and which are used by this function.  */
9714       for (reg = 0; reg <= last_reg; reg++)
9715         if (regs_ever_live[reg] && ! call_used_regs [reg])
9716           save_reg_mask |= (1 << reg);
9717
9718       /* Handle the frame pointer as a special case.  */
9719       if (TARGET_THUMB2 && frame_pointer_needed)
9720         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9721       else if (! TARGET_APCS_FRAME
9722                && ! frame_pointer_needed
9723                && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9724                && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9725         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9726
9727       /* If we aren't loading the PIC register,
9728          don't stack it even though it may be live.  */
9729       if (flag_pic
9730           && !TARGET_SINGLE_PIC_BASE
9731           && arm_pic_register != INVALID_REGNUM
9732           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9733               || current_function_uses_pic_offset_table))
9734         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9735
9736       /* The prologue will copy SP into R0, so save it.  */
9737       if (IS_STACKALIGN (func_type))
9738         save_reg_mask |= 1;
9739     }
9740
9741   /* Save registers so the exception handler can modify them.  */
9742   if (current_function_calls_eh_return)
9743     {
9744       unsigned int i;
9745
9746       for (i = 0; ; i++)
9747         {
9748           reg = EH_RETURN_DATA_REGNO (i);
9749           if (reg == INVALID_REGNUM)
9750             break;
9751           save_reg_mask |= 1 << reg;
9752         }
9753     }
9754
9755   return save_reg_mask;
9756 }
9757
9758
9759 /* Compute a bit mask of which registers need to be
9760    saved on the stack for the current function.  */
9761
9762 static unsigned long
9763 arm_compute_save_reg_mask (void)
9764 {
9765   unsigned int save_reg_mask = 0;
9766   unsigned long func_type = arm_current_func_type ();
9767   unsigned int reg;
9768
9769   if (IS_NAKED (func_type))
9770     /* This should never really happen.  */
9771     return 0;
9772
9773   /* If we are creating a stack frame, then we must save the frame pointer,
9774      IP (which will hold the old stack pointer), LR and the PC.  */
9775   if (frame_pointer_needed && TARGET_ARM)
9776     save_reg_mask |=
9777       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9778       | (1 << IP_REGNUM)
9779       | (1 << LR_REGNUM)
9780       | (1 << PC_REGNUM);
9781
9782   /* Volatile functions do not return, so there
9783      is no need to save any other registers.  */
9784   if (IS_VOLATILE (func_type))
9785     return save_reg_mask;
9786
9787   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9788
9789   /* Decide if we need to save the link register.
9790      Interrupt routines have their own banked link register,
9791      so they never need to save it.
9792      Otherwise if we do not use the link register we do not need to save
9793      it.  If we are pushing other registers onto the stack however, we
9794      can save an instruction in the epilogue by pushing the link register
9795      now and then popping it back into the PC.  This incurs extra memory
9796      accesses though, so we only do it when optimizing for size, and only
9797      if we know that we will not need a fancy return sequence.  */
9798   if (regs_ever_live [LR_REGNUM]
9799           || (save_reg_mask
9800               && optimize_size
9801               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9802               && !current_function_calls_eh_return))
9803     save_reg_mask |= 1 << LR_REGNUM;
9804
9805   if (cfun->machine->lr_save_eliminated)
9806     save_reg_mask &= ~ (1 << LR_REGNUM);
9807
9808   if (TARGET_REALLY_IWMMXT
9809       && ((bit_count (save_reg_mask)
9810            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9811     {
9812       /* The total number of registers that are going to be pushed
9813          onto the stack is odd.  We need to ensure that the stack
9814          is 64-bit aligned before we start to save iWMMXt registers,
9815          and also before we start to create locals.  (A local variable
9816          might be a double or long long which we will load/store using
9817          an iWMMXt instruction).  Therefore we need to push another
9818          ARM register, so that the stack will be 64-bit aligned.  We
9819          try to avoid using the arg registers (r0 -r3) as they might be
9820          used to pass values in a tail call.  */
9821       for (reg = 4; reg <= 12; reg++)
9822         if ((save_reg_mask & (1 << reg)) == 0)
9823           break;
9824
9825       if (reg <= 12)
9826         save_reg_mask |= (1 << reg);
9827       else
9828         {
9829           cfun->machine->sibcall_blocked = 1;
9830           save_reg_mask |= (1 << 3);
9831         }
9832     }
9833
9834   /* We may need to push an additional register for use initializing the
9835      PIC base register.  */
9836   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
9837       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
9838     {
9839       reg = thumb_find_work_register (1 << 4);
9840       if (!call_used_regs[reg])
9841         save_reg_mask |= (1 << reg);
9842     }
9843
9844   return save_reg_mask;
9845 }
9846
9847
9848 /* Compute a bit mask of which registers need to be
9849    saved on the stack for the current function.  */
9850 static unsigned long
9851 thumb1_compute_save_reg_mask (void)
9852 {
9853   unsigned long mask;
9854   unsigned reg;
9855
9856   mask = 0;
9857   for (reg = 0; reg < 12; reg ++)
9858     if (regs_ever_live[reg] && !call_used_regs[reg])
9859       mask |= 1 << reg;
9860
9861   if (flag_pic
9862       && !TARGET_SINGLE_PIC_BASE
9863       && arm_pic_register != INVALID_REGNUM
9864       && current_function_uses_pic_offset_table)
9865     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9866
9867   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
9868   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9869     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9870
9871   /* LR will also be pushed if any lo regs are pushed.  */
9872   if (mask & 0xff || thumb_force_lr_save ())
9873     mask |= (1 << LR_REGNUM);
9874
9875   /* Make sure we have a low work register if we need one.
9876      We will need one if we are going to push a high register,
9877      but we are not currently intending to push a low register.  */
9878   if ((mask & 0xff) == 0
9879       && ((mask & 0x0f00) || TARGET_BACKTRACE))
9880     {
9881       /* Use thumb_find_work_register to choose which register
9882          we will use.  If the register is live then we will
9883          have to push it.  Use LAST_LO_REGNUM as our fallback
9884          choice for the register to select.  */
9885       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9886
9887       if (! call_used_regs[reg])
9888         mask |= 1 << reg;
9889     }
9890
9891   return mask;
9892 }
9893
9894
9895 /* Return the number of bytes required to save VFP registers.  */
9896 static int
9897 arm_get_vfp_saved_size (void)
9898 {
9899   unsigned int regno;
9900   int count;
9901   int saved;
9902
9903   saved = 0;
9904   /* Space for saved VFP registers.  */
9905   if (TARGET_HARD_FLOAT && TARGET_VFP)
9906     {
9907       count = 0;
9908       for (regno = FIRST_VFP_REGNUM;
9909            regno < LAST_VFP_REGNUM;
9910            regno += 2)
9911         {
9912           if ((!regs_ever_live[regno] || call_used_regs[regno])
9913               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9914             {
9915               if (count > 0)
9916                 {
9917                   /* Workaround ARM10 VFPr1 bug.  */
9918                   if (count == 2 && !arm_arch6)
9919                     count++;
9920                   saved += count * 8;
9921                 }
9922               count = 0;
9923             }
9924           else
9925             count++;
9926         }
9927       if (count > 0)
9928         {
9929           if (count == 2 && !arm_arch6)
9930             count++;
9931           saved += count * 8;
9932         }
9933     }
9934   return saved;
9935 }
9936
9937
9938 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9939    everything bar the final return instruction.  */
9940 const char *
9941 output_return_instruction (rtx operand, int really_return, int reverse)
9942 {
9943   char conditional[10];
9944   char instr[100];
9945   unsigned reg;
9946   unsigned long live_regs_mask;
9947   unsigned long func_type;
9948   arm_stack_offsets *offsets;
9949
9950   func_type = arm_current_func_type ();
9951
9952   if (IS_NAKED (func_type))
9953     return "";
9954
9955   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9956     {
9957       /* If this function was declared non-returning, and we have
9958          found a tail call, then we have to trust that the called
9959          function won't return.  */
9960       if (really_return)
9961         {
9962           rtx ops[2];
9963
9964           /* Otherwise, trap an attempted return by aborting.  */
9965           ops[0] = operand;
9966           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9967                                        : "abort");
9968           assemble_external_libcall (ops[1]);
9969           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9970         }
9971
9972       return "";
9973     }
9974
9975   gcc_assert (!current_function_calls_alloca || really_return);
9976
9977   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9978
9979   return_used_this_function = 1;
9980
9981   live_regs_mask = arm_compute_save_reg_mask ();
9982
9983   if (live_regs_mask)
9984     {
9985       const char * return_reg;
9986
9987       /* If we do not have any special requirements for function exit
9988          (e.g. interworking) then we can load the return address
9989          directly into the PC.  Otherwise we must load it into LR.  */
9990       if (really_return
9991           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
9992         return_reg = reg_names[PC_REGNUM];
9993       else
9994         return_reg = reg_names[LR_REGNUM];
9995
9996       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9997         {
9998           /* There are three possible reasons for the IP register
9999              being saved.  1) a stack frame was created, in which case
10000              IP contains the old stack pointer, or 2) an ISR routine
10001              corrupted it, or 3) it was saved to align the stack on
10002              iWMMXt.  In case 1, restore IP into SP, otherwise just
10003              restore IP.  */
10004           if (frame_pointer_needed)
10005             {
10006               live_regs_mask &= ~ (1 << IP_REGNUM);
10007               live_regs_mask |=   (1 << SP_REGNUM);
10008             }
10009           else
10010             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
10011         }
10012
10013       /* On some ARM architectures it is faster to use LDR rather than
10014          LDM to load a single register.  On other architectures, the
10015          cost is the same.  In 26 bit mode, or for exception handlers,
10016          we have to use LDM to load the PC so that the CPSR is also
10017          restored.  */
10018       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10019         if (live_regs_mask == (1U << reg))
10020           break;
10021
10022       if (reg <= LAST_ARM_REGNUM
10023           && (reg != LR_REGNUM
10024               || ! really_return
10025               || ! IS_INTERRUPT (func_type)))
10026         {
10027           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
10028                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
10029         }
10030       else
10031         {
10032           char *p;
10033           int first = 1;
10034
10035           /* Generate the load multiple instruction to restore the
10036              registers.  Note we can get here, even if
10037              frame_pointer_needed is true, but only if sp already
10038              points to the base of the saved core registers.  */
10039           if (live_regs_mask & (1 << SP_REGNUM))
10040             {
10041               unsigned HOST_WIDE_INT stack_adjust;
10042
10043               offsets = arm_get_frame_offsets ();
10044               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
10045               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
10046
10047               if (stack_adjust && arm_arch5 && TARGET_ARM)
10048                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
10049               else
10050                 {
10051                   /* If we can't use ldmib (SA110 bug),
10052                      then try to pop r3 instead.  */
10053                   if (stack_adjust)
10054                     live_regs_mask |= 1 << 3;
10055                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
10056                 }
10057             }
10058           else
10059             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
10060
10061           p = instr + strlen (instr);
10062
10063           for (reg = 0; reg <= SP_REGNUM; reg++)
10064             if (live_regs_mask & (1 << reg))
10065               {
10066                 int l = strlen (reg_names[reg]);
10067
10068                 if (first)
10069                   first = 0;
10070                 else
10071                   {
10072                     memcpy (p, ", ", 2);
10073                     p += 2;
10074                   }
10075
10076                 memcpy (p, "%|", 2);
10077                 memcpy (p + 2, reg_names[reg], l);
10078                 p += l + 2;
10079               }
10080
10081           if (live_regs_mask & (1 << LR_REGNUM))
10082             {
10083               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
10084               /* If returning from an interrupt, restore the CPSR.  */
10085               if (IS_INTERRUPT (func_type))
10086                 strcat (p, "^");
10087             }
10088           else
10089             strcpy (p, "}");
10090         }
10091
10092       output_asm_insn (instr, & operand);
10093
10094       /* See if we need to generate an extra instruction to
10095          perform the actual function return.  */
10096       if (really_return
10097           && func_type != ARM_FT_INTERWORKED
10098           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
10099         {
10100           /* The return has already been handled
10101              by loading the LR into the PC.  */
10102           really_return = 0;
10103         }
10104     }
10105
10106   if (really_return)
10107     {
10108       switch ((int) ARM_FUNC_TYPE (func_type))
10109         {
10110         case ARM_FT_ISR:
10111         case ARM_FT_FIQ:
10112           /* ??? This is wrong for unified assembly syntax.  */
10113           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
10114           break;
10115
10116         case ARM_FT_INTERWORKED:
10117           sprintf (instr, "bx%s\t%%|lr", conditional);
10118           break;
10119
10120         case ARM_FT_EXCEPTION:
10121           /* ??? This is wrong for unified assembly syntax.  */
10122           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
10123           break;
10124
10125         default:
10126           /* Use bx if it's available.  */
10127           if (arm_arch5 || arm_arch4t)
10128             sprintf (instr, "bx%s\t%%|lr", conditional);
10129           else
10130             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
10131           break;
10132         }
10133
10134       output_asm_insn (instr, & operand);
10135     }
10136
10137   return "";
10138 }
10139
10140 /* Write the function name into the code section, directly preceding
10141    the function prologue.
10142
10143    Code will be output similar to this:
10144      t0
10145          .ascii "arm_poke_function_name", 0
10146          .align
10147      t1
10148          .word 0xff000000 + (t1 - t0)
10149      arm_poke_function_name
10150          mov     ip, sp
10151          stmfd   sp!, {fp, ip, lr, pc}
10152          sub     fp, ip, #4
10153
10154    When performing a stack backtrace, code can inspect the value
10155    of 'pc' stored at 'fp' + 0.  If the trace function then looks
10156    at location pc - 12 and the top 8 bits are set, then we know
10157    that there is a function name embedded immediately preceding this
10158    location and has length ((pc[-3]) & 0xff000000).
10159
10160    We assume that pc is declared as a pointer to an unsigned long.
10161
10162    It is of no benefit to output the function name if we are assembling
10163    a leaf function.  These function types will not contain a stack
10164    backtrace structure, therefore it is not possible to determine the
10165    function name.  */
10166 void
10167 arm_poke_function_name (FILE *stream, const char *name)
10168 {
10169   unsigned long alignlength;
10170   unsigned long length;
10171   rtx           x;
10172
10173   length      = strlen (name) + 1;
10174   alignlength = ROUND_UP_WORD (length);
10175
10176   ASM_OUTPUT_ASCII (stream, name, length);
10177   ASM_OUTPUT_ALIGN (stream, 2);
10178   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
10179   assemble_aligned_integer (UNITS_PER_WORD, x);
10180 }
10181
10182 /* Place some comments into the assembler stream
10183    describing the current function.  */
10184 static void
10185 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
10186 {
10187   unsigned long func_type;
10188
10189   if (TARGET_THUMB1)
10190     {
10191       thumb1_output_function_prologue (f, frame_size);
10192       return;
10193     }
10194
10195   /* Sanity check.  */
10196   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
10197
10198   func_type = arm_current_func_type ();
10199
10200   switch ((int) ARM_FUNC_TYPE (func_type))
10201     {
10202     default:
10203     case ARM_FT_NORMAL:
10204       break;
10205     case ARM_FT_INTERWORKED:
10206       asm_fprintf (f, "\t%@ Function supports interworking.\n");
10207       break;
10208     case ARM_FT_ISR:
10209       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
10210       break;
10211     case ARM_FT_FIQ:
10212       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
10213       break;
10214     case ARM_FT_EXCEPTION:
10215       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
10216       break;
10217     }
10218
10219   if (IS_NAKED (func_type))
10220     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
10221
10222   if (IS_VOLATILE (func_type))
10223     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
10224
10225   if (IS_NESTED (func_type))
10226     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
10227   if (IS_STACKALIGN (func_type))
10228     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
10229
10230   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
10231                current_function_args_size,
10232                current_function_pretend_args_size, frame_size);
10233
10234   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
10235                frame_pointer_needed,
10236                cfun->machine->uses_anonymous_args);
10237
10238   if (cfun->machine->lr_save_eliminated)
10239     asm_fprintf (f, "\t%@ link register save eliminated.\n");
10240
10241   if (current_function_calls_eh_return)
10242     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
10243
10244 #ifdef AOF_ASSEMBLER
10245   if (flag_pic)
10246     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
10247 #endif
10248
10249   return_used_this_function = 0;
10250 }
10251
10252 const char *
10253 arm_output_epilogue (rtx sibling)
10254 {
10255   int reg;
10256   unsigned long saved_regs_mask;
10257   unsigned long func_type;
10258   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
10259      frame that is $fp + 4 for a non-variadic function.  */
10260   int floats_offset = 0;
10261   rtx operands[3];
10262   FILE * f = asm_out_file;
10263   unsigned int lrm_count = 0;
10264   int really_return = (sibling == NULL);
10265   int start_reg;
10266   arm_stack_offsets *offsets;
10267
10268   /* If we have already generated the return instruction
10269      then it is futile to generate anything else.  */
10270   if (use_return_insn (FALSE, sibling) && return_used_this_function)
10271     return "";
10272
10273   func_type = arm_current_func_type ();
10274
10275   if (IS_NAKED (func_type))
10276     /* Naked functions don't have epilogues.  */
10277     return "";
10278
10279   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
10280     {
10281       rtx op;
10282
10283       /* A volatile function should never return.  Call abort.  */
10284       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
10285       assemble_external_libcall (op);
10286       output_asm_insn ("bl\t%a0", &op);
10287
10288       return "";
10289     }
10290
10291   /* If we are throwing an exception, then we really must be doing a
10292      return, so we can't tail-call.  */
10293   gcc_assert (!current_function_calls_eh_return || really_return);
10294
10295   offsets = arm_get_frame_offsets ();
10296   saved_regs_mask = arm_compute_save_reg_mask ();
10297
10298   if (TARGET_IWMMXT)
10299     lrm_count = bit_count (saved_regs_mask);
10300
10301   floats_offset = offsets->saved_args;
10302   /* Compute how far away the floats will be.  */
10303   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10304     if (saved_regs_mask & (1 << reg))
10305       floats_offset += 4;
10306
10307   if (frame_pointer_needed && TARGET_ARM)
10308     {
10309       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
10310       int vfp_offset = offsets->frame;
10311
10312       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10313         {
10314           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10315             if (regs_ever_live[reg] && !call_used_regs[reg])
10316               {
10317                 floats_offset += 12;
10318                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
10319                              reg, FP_REGNUM, floats_offset - vfp_offset);
10320               }
10321         }
10322       else
10323         {
10324           start_reg = LAST_FPA_REGNUM;
10325
10326           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10327             {
10328               if (regs_ever_live[reg] && !call_used_regs[reg])
10329                 {
10330                   floats_offset += 12;
10331
10332                   /* We can't unstack more than four registers at once.  */
10333                   if (start_reg - reg == 3)
10334                     {
10335                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
10336                                    reg, FP_REGNUM, floats_offset - vfp_offset);
10337                       start_reg = reg - 1;
10338                     }
10339                 }
10340               else
10341                 {
10342                   if (reg != start_reg)
10343                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10344                                  reg + 1, start_reg - reg,
10345                                  FP_REGNUM, floats_offset - vfp_offset);
10346                   start_reg = reg - 1;
10347                 }
10348             }
10349
10350           /* Just in case the last register checked also needs unstacking.  */
10351           if (reg != start_reg)
10352             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10353                          reg + 1, start_reg - reg,
10354                          FP_REGNUM, floats_offset - vfp_offset);
10355         }
10356
10357       if (TARGET_HARD_FLOAT && TARGET_VFP)
10358         {
10359           int saved_size;
10360
10361           /* The fldmd insns do not have base+offset addressing
10362              modes, so we use IP to hold the address.  */
10363           saved_size = arm_get_vfp_saved_size ();
10364
10365           if (saved_size > 0)
10366             {
10367               floats_offset += saved_size;
10368               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
10369                            FP_REGNUM, floats_offset - vfp_offset);
10370             }
10371           start_reg = FIRST_VFP_REGNUM;
10372           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10373             {
10374               if ((!regs_ever_live[reg] || call_used_regs[reg])
10375                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10376                 {
10377                   if (start_reg != reg)
10378                     vfp_output_fldmd (f, IP_REGNUM,
10379                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10380                                       (reg - start_reg) / 2);
10381                   start_reg = reg + 2;
10382                 }
10383             }
10384           if (start_reg != reg)
10385             vfp_output_fldmd (f, IP_REGNUM,
10386                               (start_reg - FIRST_VFP_REGNUM) / 2,
10387                               (reg - start_reg) / 2);
10388         }
10389
10390       if (TARGET_IWMMXT)
10391         {
10392           /* The frame pointer is guaranteed to be non-double-word aligned.
10393              This is because it is set to (old_stack_pointer - 4) and the
10394              old_stack_pointer was double word aligned.  Thus the offset to
10395              the iWMMXt registers to be loaded must also be non-double-word
10396              sized, so that the resultant address *is* double-word aligned.
10397              We can ignore floats_offset since that was already included in
10398              the live_regs_mask.  */
10399           lrm_count += (lrm_count % 2 ? 2 : 1);
10400
10401           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10402             if (regs_ever_live[reg] && !call_used_regs[reg])
10403               {
10404                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
10405                              reg, FP_REGNUM, lrm_count * 4);
10406                 lrm_count += 2;
10407               }
10408         }
10409
10410       /* saved_regs_mask should contain the IP, which at the time of stack
10411          frame generation actually contains the old stack pointer.  So a
10412          quick way to unwind the stack is just pop the IP register directly
10413          into the stack pointer.  */
10414       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
10415       saved_regs_mask &= ~ (1 << IP_REGNUM);
10416       saved_regs_mask |=   (1 << SP_REGNUM);
10417
10418       /* There are two registers left in saved_regs_mask - LR and PC.  We
10419          only need to restore the LR register (the return address), but to
10420          save time we can load it directly into the PC, unless we need a
10421          special function exit sequence, or we are not really returning.  */
10422       if (really_return
10423           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10424           && !current_function_calls_eh_return)
10425         /* Delete the LR from the register mask, so that the LR on
10426            the stack is loaded into the PC in the register mask.  */
10427         saved_regs_mask &= ~ (1 << LR_REGNUM);
10428       else
10429         saved_regs_mask &= ~ (1 << PC_REGNUM);
10430
10431       /* We must use SP as the base register, because SP is one of the
10432          registers being restored.  If an interrupt or page fault
10433          happens in the ldm instruction, the SP might or might not
10434          have been restored.  That would be bad, as then SP will no
10435          longer indicate the safe area of stack, and we can get stack
10436          corruption.  Using SP as the base register means that it will
10437          be reset correctly to the original value, should an interrupt
10438          occur.  If the stack pointer already points at the right
10439          place, then omit the subtraction.  */
10440       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
10441           || current_function_calls_alloca)
10442         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
10443                      4 * bit_count (saved_regs_mask));
10444       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
10445
10446       if (IS_INTERRUPT (func_type))
10447         /* Interrupt handlers will have pushed the
10448            IP onto the stack, so restore it now.  */
10449         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
10450     }
10451   else
10452     {
10453       HOST_WIDE_INT amount;
10454       int rfe;
10455       /* Restore stack pointer if necessary.  */
10456       if (frame_pointer_needed)
10457         {
10458           /* For Thumb-2 restore sp from the frame pointer.
10459              Operand restrictions mean we have to increment FP, then copy
10460              to SP.  */
10461           amount = offsets->locals_base - offsets->saved_regs;
10462           operands[0] = hard_frame_pointer_rtx;
10463         }
10464       else
10465         {
10466           operands[0] = stack_pointer_rtx;
10467           amount = offsets->outgoing_args - offsets->saved_regs;
10468         }
10469
10470       if (amount)
10471         {
10472           operands[1] = operands[0];
10473           operands[2] = GEN_INT (amount);
10474           output_add_immediate (operands);
10475         }
10476       if (frame_pointer_needed)
10477         asm_fprintf (f, "\tmov\t%r, %r\n",
10478                      SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
10479
10480       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10481         {
10482           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10483             if (regs_ever_live[reg] && !call_used_regs[reg])
10484               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
10485                            reg, SP_REGNUM);
10486         }
10487       else
10488         {
10489           start_reg = FIRST_FPA_REGNUM;
10490
10491           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10492             {
10493               if (regs_ever_live[reg] && !call_used_regs[reg])
10494                 {
10495                   if (reg - start_reg == 3)
10496                     {
10497                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
10498                                    start_reg, SP_REGNUM);
10499                       start_reg = reg + 1;
10500                     }
10501                 }
10502               else
10503                 {
10504                   if (reg != start_reg)
10505                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10506                                  start_reg, reg - start_reg,
10507                                  SP_REGNUM);
10508
10509                   start_reg = reg + 1;
10510                 }
10511             }
10512
10513           /* Just in case the last register checked also needs unstacking.  */
10514           if (reg != start_reg)
10515             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10516                          start_reg, reg - start_reg, SP_REGNUM);
10517         }
10518
10519       if (TARGET_HARD_FLOAT && TARGET_VFP)
10520         {
10521           start_reg = FIRST_VFP_REGNUM;
10522           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10523             {
10524               if ((!regs_ever_live[reg] || call_used_regs[reg])
10525                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10526                 {
10527                   if (start_reg != reg)
10528                     vfp_output_fldmd (f, SP_REGNUM,
10529                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10530                                       (reg - start_reg) / 2);
10531                   start_reg = reg + 2;
10532                 }
10533             }
10534           if (start_reg != reg)
10535             vfp_output_fldmd (f, SP_REGNUM,
10536                               (start_reg - FIRST_VFP_REGNUM) / 2,
10537                               (reg - start_reg) / 2);
10538         }
10539       if (TARGET_IWMMXT)
10540         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10541           if (regs_ever_live[reg] && !call_used_regs[reg])
10542             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10543
10544       /* If we can, restore the LR into the PC.  */
10545       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
10546           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
10547           && !IS_STACKALIGN (func_type)
10548           && really_return
10549           && current_function_pretend_args_size == 0
10550           && saved_regs_mask & (1 << LR_REGNUM)
10551           && !current_function_calls_eh_return)
10552         {
10553           saved_regs_mask &= ~ (1 << LR_REGNUM);
10554           saved_regs_mask |=   (1 << PC_REGNUM);
10555           rfe = IS_INTERRUPT (func_type);
10556         }
10557       else
10558         rfe = 0;
10559
10560       /* Load the registers off the stack.  If we only have one register
10561          to load use the LDR instruction - it is faster.  For Thumb-2
10562          always use pop and the assembler will pick the best instruction.*/
10563       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
10564           && !IS_INTERRUPT(func_type))
10565         {
10566           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10567         }
10568       else if (saved_regs_mask)
10569         {
10570           if (saved_regs_mask & (1 << SP_REGNUM))
10571             /* Note - write back to the stack register is not enabled
10572                (i.e. "ldmfd sp!...").  We know that the stack pointer is
10573                in the list of registers and if we add writeback the
10574                instruction becomes UNPREDICTABLE.  */
10575             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
10576                              rfe);
10577           else if (TARGET_ARM)
10578             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
10579                              rfe);
10580           else
10581             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
10582         }
10583
10584       if (current_function_pretend_args_size)
10585         {
10586           /* Unwind the pre-pushed regs.  */
10587           operands[0] = operands[1] = stack_pointer_rtx;
10588           operands[2] = GEN_INT (current_function_pretend_args_size);
10589           output_add_immediate (operands);
10590         }
10591     }
10592
10593   /* We may have already restored PC directly from the stack.  */
10594   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10595     return "";
10596
10597   /* Stack adjustment for exception handler.  */
10598   if (current_function_calls_eh_return)
10599     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10600                  ARM_EH_STACKADJ_REGNUM);
10601
10602   /* Generate the return instruction.  */
10603   switch ((int) ARM_FUNC_TYPE (func_type))
10604     {
10605     case ARM_FT_ISR:
10606     case ARM_FT_FIQ:
10607       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10608       break;
10609
10610     case ARM_FT_EXCEPTION:
10611       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10612       break;
10613
10614     case ARM_FT_INTERWORKED:
10615       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10616       break;
10617
10618     default:
10619       if (IS_STACKALIGN (func_type))
10620         {
10621           /* See comment in arm_expand_prologue.  */
10622           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
10623         }
10624       if (arm_arch5 || arm_arch4t)
10625         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10626       else
10627         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10628       break;
10629     }
10630
10631   return "";
10632 }
10633
10634 static void
10635 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10636                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10637 {
10638   arm_stack_offsets *offsets;
10639
10640   if (TARGET_THUMB1)
10641     {
10642       int regno;
10643
10644       /* Emit any call-via-reg trampolines that are needed for v4t support
10645          of call_reg and call_value_reg type insns.  */
10646       for (regno = 0; regno < LR_REGNUM; regno++)
10647         {
10648           rtx label = cfun->machine->call_via[regno];
10649
10650           if (label != NULL)
10651             {
10652               switch_to_section (function_section (current_function_decl));
10653               targetm.asm_out.internal_label (asm_out_file, "L",
10654                                               CODE_LABEL_NUMBER (label));
10655               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10656             }
10657         }
10658
10659       /* ??? Probably not safe to set this here, since it assumes that a
10660          function will be emitted as assembly immediately after we generate
10661          RTL for it.  This does not happen for inline functions.  */
10662       return_used_this_function = 0;
10663     }
10664   else /* TARGET_32BIT */
10665     {
10666       /* We need to take into account any stack-frame rounding.  */
10667       offsets = arm_get_frame_offsets ();
10668
10669       gcc_assert (!use_return_insn (FALSE, NULL)
10670                   || !return_used_this_function
10671                   || offsets->saved_regs == offsets->outgoing_args
10672                   || frame_pointer_needed);
10673
10674       /* Reset the ARM-specific per-function variables.  */
10675       after_arm_reorg = 0;
10676     }
10677 }
10678
10679 /* Generate and emit an insn that we will recognize as a push_multi.
10680    Unfortunately, since this insn does not reflect very well the actual
10681    semantics of the operation, we need to annotate the insn for the benefit
10682    of DWARF2 frame unwind information.  */
10683 static rtx
10684 emit_multi_reg_push (unsigned long mask)
10685 {
10686   int num_regs = 0;
10687   int num_dwarf_regs;
10688   int i, j;
10689   rtx par;
10690   rtx dwarf;
10691   int dwarf_par_index;
10692   rtx tmp, reg;
10693
10694   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10695     if (mask & (1 << i))
10696       num_regs++;
10697
10698   gcc_assert (num_regs && num_regs <= 16);
10699
10700   /* We don't record the PC in the dwarf frame information.  */
10701   num_dwarf_regs = num_regs;
10702   if (mask & (1 << PC_REGNUM))
10703     num_dwarf_regs--;
10704
10705   /* For the body of the insn we are going to generate an UNSPEC in
10706      parallel with several USEs.  This allows the insn to be recognized
10707      by the push_multi pattern in the arm.md file.  The insn looks
10708      something like this:
10709
10710        (parallel [
10711            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10712                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10713            (use (reg:SI 11 fp))
10714            (use (reg:SI 12 ip))
10715            (use (reg:SI 14 lr))
10716            (use (reg:SI 15 pc))
10717         ])
10718
10719      For the frame note however, we try to be more explicit and actually
10720      show each register being stored into the stack frame, plus a (single)
10721      decrement of the stack pointer.  We do it this way in order to be
10722      friendly to the stack unwinding code, which only wants to see a single
10723      stack decrement per instruction.  The RTL we generate for the note looks
10724      something like this:
10725
10726       (sequence [
10727            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10728            (set (mem:SI (reg:SI sp)) (reg:SI r4))
10729            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10730            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10731            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10732         ])
10733
10734       This sequence is used both by the code to support stack unwinding for
10735       exceptions handlers and the code to generate dwarf2 frame debugging.  */
10736
10737   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10738   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10739   dwarf_par_index = 1;
10740
10741   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10742     {
10743       if (mask & (1 << i))
10744         {
10745           reg = gen_rtx_REG (SImode, i);
10746
10747           XVECEXP (par, 0, 0)
10748             = gen_rtx_SET (VOIDmode,
10749                            gen_frame_mem (BLKmode,
10750                                           gen_rtx_PRE_DEC (BLKmode,
10751                                                            stack_pointer_rtx)),
10752                            gen_rtx_UNSPEC (BLKmode,
10753                                            gen_rtvec (1, reg),
10754                                            UNSPEC_PUSH_MULT));
10755
10756           if (i != PC_REGNUM)
10757             {
10758               tmp = gen_rtx_SET (VOIDmode,
10759                                  gen_frame_mem (SImode, stack_pointer_rtx),
10760                                  reg);
10761               RTX_FRAME_RELATED_P (tmp) = 1;
10762               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10763               dwarf_par_index++;
10764             }
10765
10766           break;
10767         }
10768     }
10769
10770   for (j = 1, i++; j < num_regs; i++)
10771     {
10772       if (mask & (1 << i))
10773         {
10774           reg = gen_rtx_REG (SImode, i);
10775
10776           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10777
10778           if (i != PC_REGNUM)
10779             {
10780               tmp
10781                 = gen_rtx_SET (VOIDmode,
10782                                gen_frame_mem (SImode,
10783                                               plus_constant (stack_pointer_rtx,
10784                                                              4 * j)),
10785                                reg);
10786               RTX_FRAME_RELATED_P (tmp) = 1;
10787               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10788             }
10789
10790           j++;
10791         }
10792     }
10793
10794   par = emit_insn (par);
10795
10796   tmp = gen_rtx_SET (VOIDmode,
10797                      stack_pointer_rtx,
10798                      plus_constant (stack_pointer_rtx, -4 * num_regs));
10799   RTX_FRAME_RELATED_P (tmp) = 1;
10800   XVECEXP (dwarf, 0, 0) = tmp;
10801
10802   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10803                                        REG_NOTES (par));
10804   return par;
10805 }
10806
10807 /* Calculate the size of the return value that is passed in registers.  */
10808 static int
10809 arm_size_return_regs (void)
10810 {
10811   enum machine_mode mode;
10812
10813   if (current_function_return_rtx != 0)
10814     mode = GET_MODE (current_function_return_rtx);
10815   else
10816     mode = DECL_MODE (DECL_RESULT (current_function_decl));
10817
10818   return GET_MODE_SIZE (mode);
10819 }
10820
10821 static rtx
10822 emit_sfm (int base_reg, int count)
10823 {
10824   rtx par;
10825   rtx dwarf;
10826   rtx tmp, reg;
10827   int i;
10828
10829   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10830   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10831
10832   reg = gen_rtx_REG (XFmode, base_reg++);
10833
10834   XVECEXP (par, 0, 0)
10835     = gen_rtx_SET (VOIDmode,
10836                    gen_frame_mem (BLKmode,
10837                                   gen_rtx_PRE_DEC (BLKmode,
10838                                                    stack_pointer_rtx)),
10839                    gen_rtx_UNSPEC (BLKmode,
10840                                    gen_rtvec (1, reg),
10841                                    UNSPEC_PUSH_MULT));
10842   tmp = gen_rtx_SET (VOIDmode,
10843                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10844   RTX_FRAME_RELATED_P (tmp) = 1;
10845   XVECEXP (dwarf, 0, 1) = tmp;
10846
10847   for (i = 1; i < count; i++)
10848     {
10849       reg = gen_rtx_REG (XFmode, base_reg++);
10850       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10851
10852       tmp = gen_rtx_SET (VOIDmode,
10853                          gen_frame_mem (XFmode,
10854                                         plus_constant (stack_pointer_rtx,
10855                                                        i * 12)),
10856                          reg);
10857       RTX_FRAME_RELATED_P (tmp) = 1;
10858       XVECEXP (dwarf, 0, i + 1) = tmp;
10859     }
10860
10861   tmp = gen_rtx_SET (VOIDmode,
10862                      stack_pointer_rtx,
10863                      plus_constant (stack_pointer_rtx, -12 * count));
10864
10865   RTX_FRAME_RELATED_P (tmp) = 1;
10866   XVECEXP (dwarf, 0, 0) = tmp;
10867
10868   par = emit_insn (par);
10869   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10870                                        REG_NOTES (par));
10871   return par;
10872 }
10873
10874
10875 /* Return true if the current function needs to save/restore LR.  */
10876
10877 static bool
10878 thumb_force_lr_save (void)
10879 {
10880   return !cfun->machine->lr_save_eliminated
10881          && (!leaf_function_p ()
10882              || thumb_far_jump_used_p ()
10883              || regs_ever_live [LR_REGNUM]);
10884 }
10885
10886
10887 /* Compute the distance from register FROM to register TO.
10888    These can be the arg pointer (26), the soft frame pointer (25),
10889    the stack pointer (13) or the hard frame pointer (11).
10890    In thumb mode r7 is used as the soft frame pointer, if needed.
10891    Typical stack layout looks like this:
10892
10893        old stack pointer -> |    |
10894                              ----
10895                             |    | \
10896                             |    |   saved arguments for
10897                             |    |   vararg functions
10898                             |    | /
10899                               --
10900    hard FP & arg pointer -> |    | \
10901                             |    |   stack
10902                             |    |   frame
10903                             |    | /
10904                               --
10905                             |    | \
10906                             |    |   call saved
10907                             |    |   registers
10908       soft frame pointer -> |    | /
10909                               --
10910                             |    | \
10911                             |    |   local
10912                             |    |   variables
10913      locals base pointer -> |    | /
10914                               --
10915                             |    | \
10916                             |    |   outgoing
10917                             |    |   arguments
10918    current stack pointer -> |    | /
10919                               --
10920
10921   For a given function some or all of these stack components
10922   may not be needed, giving rise to the possibility of
10923   eliminating some of the registers.
10924
10925   The values returned by this function must reflect the behavior
10926   of arm_expand_prologue() and arm_compute_save_reg_mask().
10927
10928   The sign of the number returned reflects the direction of stack
10929   growth, so the values are positive for all eliminations except
10930   from the soft frame pointer to the hard frame pointer.
10931
10932   SFP may point just inside the local variables block to ensure correct
10933   alignment.  */
10934
10935
10936 /* Calculate stack offsets.  These are used to calculate register elimination
10937    offsets and in prologue/epilogue code.  */
10938
10939 static arm_stack_offsets *
10940 arm_get_frame_offsets (void)
10941 {
10942   struct arm_stack_offsets *offsets;
10943   unsigned long func_type;
10944   int leaf;
10945   int saved;
10946   HOST_WIDE_INT frame_size;
10947
10948   offsets = &cfun->machine->stack_offsets;
10949
10950   /* We need to know if we are a leaf function.  Unfortunately, it
10951      is possible to be called after start_sequence has been called,
10952      which causes get_insns to return the insns for the sequence,
10953      not the function, which will cause leaf_function_p to return
10954      the incorrect result.
10955
10956      to know about leaf functions once reload has completed, and the
10957      frame size cannot be changed after that time, so we can safely
10958      use the cached value.  */
10959
10960   if (reload_completed)
10961     return offsets;
10962
10963   /* Initially this is the size of the local variables.  It will translated
10964      into an offset once we have determined the size of preceding data.  */
10965   frame_size = ROUND_UP_WORD (get_frame_size ());
10966
10967   leaf = leaf_function_p ();
10968
10969   /* Space for variadic functions.  */
10970   offsets->saved_args = current_function_pretend_args_size;
10971
10972   /* In Thumb mode this is incorrect, but never used.  */
10973   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10974
10975   if (TARGET_32BIT)
10976     {
10977       unsigned int regno;
10978
10979       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10980
10981       /* We know that SP will be doubleword aligned on entry, and we must
10982          preserve that condition at any subroutine call.  We also require the
10983          soft frame pointer to be doubleword aligned.  */
10984
10985       if (TARGET_REALLY_IWMMXT)
10986         {
10987           /* Check for the call-saved iWMMXt registers.  */
10988           for (regno = FIRST_IWMMXT_REGNUM;
10989                regno <= LAST_IWMMXT_REGNUM;
10990                regno++)
10991             if (regs_ever_live [regno] && ! call_used_regs [regno])
10992               saved += 8;
10993         }
10994
10995       func_type = arm_current_func_type ();
10996       if (! IS_VOLATILE (func_type))
10997         {
10998           /* Space for saved FPA registers.  */
10999           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
11000           if (regs_ever_live[regno] && ! call_used_regs[regno])
11001             saved += 12;
11002
11003           /* Space for saved VFP registers.  */
11004           if (TARGET_HARD_FLOAT && TARGET_VFP)
11005             saved += arm_get_vfp_saved_size ();
11006         }
11007     }
11008   else /* TARGET_THUMB1 */
11009     {
11010       saved = bit_count (thumb1_compute_save_reg_mask ()) * 4;
11011       if (TARGET_BACKTRACE)
11012         saved += 16;
11013     }
11014
11015   /* Saved registers include the stack frame.  */
11016   offsets->saved_regs = offsets->saved_args + saved;
11017   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
11018   /* A leaf function does not need any stack alignment if it has nothing
11019      on the stack.  */
11020   if (leaf && frame_size == 0)
11021     {
11022       offsets->outgoing_args = offsets->soft_frame;
11023       return offsets;
11024     }
11025
11026   /* Ensure SFP has the correct alignment.  */
11027   if (ARM_DOUBLEWORD_ALIGN
11028       && (offsets->soft_frame & 7))
11029     offsets->soft_frame += 4;
11030
11031   offsets->locals_base = offsets->soft_frame + frame_size;
11032   offsets->outgoing_args = (offsets->locals_base
11033                             + current_function_outgoing_args_size);
11034
11035   if (ARM_DOUBLEWORD_ALIGN)
11036     {
11037       /* Ensure SP remains doubleword aligned.  */
11038       if (offsets->outgoing_args & 7)
11039         offsets->outgoing_args += 4;
11040       gcc_assert (!(offsets->outgoing_args & 7));
11041     }
11042
11043   return offsets;
11044 }
11045
11046
11047 /* Calculate the relative offsets for the different stack pointers.  Positive
11048    offsets are in the direction of stack growth.  */
11049
11050 HOST_WIDE_INT
11051 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
11052 {
11053   arm_stack_offsets *offsets;
11054
11055   offsets = arm_get_frame_offsets ();
11056
11057   /* OK, now we have enough information to compute the distances.
11058      There must be an entry in these switch tables for each pair
11059      of registers in ELIMINABLE_REGS, even if some of the entries
11060      seem to be redundant or useless.  */
11061   switch (from)
11062     {
11063     case ARG_POINTER_REGNUM:
11064       switch (to)
11065         {
11066         case THUMB_HARD_FRAME_POINTER_REGNUM:
11067           return 0;
11068
11069         case FRAME_POINTER_REGNUM:
11070           /* This is the reverse of the soft frame pointer
11071              to hard frame pointer elimination below.  */
11072           return offsets->soft_frame - offsets->saved_args;
11073
11074         case ARM_HARD_FRAME_POINTER_REGNUM:
11075           /* If there is no stack frame then the hard
11076              frame pointer and the arg pointer coincide.  */
11077           if (offsets->frame == offsets->saved_regs)
11078             return 0;
11079           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
11080           return (frame_pointer_needed
11081                   && cfun->static_chain_decl != NULL
11082                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
11083
11084         case STACK_POINTER_REGNUM:
11085           /* If nothing has been pushed on the stack at all
11086              then this will return -4.  This *is* correct!  */
11087           return offsets->outgoing_args - (offsets->saved_args + 4);
11088
11089         default:
11090           gcc_unreachable ();
11091         }
11092       gcc_unreachable ();
11093
11094     case FRAME_POINTER_REGNUM:
11095       switch (to)
11096         {
11097         case THUMB_HARD_FRAME_POINTER_REGNUM:
11098           return 0;
11099
11100         case ARM_HARD_FRAME_POINTER_REGNUM:
11101           /* The hard frame pointer points to the top entry in the
11102              stack frame.  The soft frame pointer to the bottom entry
11103              in the stack frame.  If there is no stack frame at all,
11104              then they are identical.  */
11105
11106           return offsets->frame - offsets->soft_frame;
11107
11108         case STACK_POINTER_REGNUM:
11109           return offsets->outgoing_args - offsets->soft_frame;
11110
11111         default:
11112           gcc_unreachable ();
11113         }
11114       gcc_unreachable ();
11115
11116     default:
11117       /* You cannot eliminate from the stack pointer.
11118          In theory you could eliminate from the hard frame
11119          pointer to the stack pointer, but this will never
11120          happen, since if a stack frame is not needed the
11121          hard frame pointer will never be used.  */
11122       gcc_unreachable ();
11123     }
11124 }
11125
11126
11127 /* Emit RTL to save coprocessor registers on function entry.  Returns the
11128    number of bytes pushed.  */
11129
11130 static int
11131 arm_save_coproc_regs(void)
11132 {
11133   int saved_size = 0;
11134   unsigned reg;
11135   unsigned start_reg;
11136   rtx insn;
11137
11138   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11139     if (regs_ever_live[reg] && ! call_used_regs [reg])
11140       {
11141         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
11142         insn = gen_rtx_MEM (V2SImode, insn);
11143         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
11144         RTX_FRAME_RELATED_P (insn) = 1;
11145         saved_size += 8;
11146       }
11147
11148   /* Save any floating point call-saved registers used by this
11149      function.  */
11150   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11151     {
11152       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11153         if (regs_ever_live[reg] && !call_used_regs[reg])
11154           {
11155             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
11156             insn = gen_rtx_MEM (XFmode, insn);
11157             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
11158             RTX_FRAME_RELATED_P (insn) = 1;
11159             saved_size += 12;
11160           }
11161     }
11162   else
11163     {
11164       start_reg = LAST_FPA_REGNUM;
11165
11166       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11167         {
11168           if (regs_ever_live[reg] && !call_used_regs[reg])
11169             {
11170               if (start_reg - reg == 3)
11171                 {
11172                   insn = emit_sfm (reg, 4);
11173                   RTX_FRAME_RELATED_P (insn) = 1;
11174                   saved_size += 48;
11175                   start_reg = reg - 1;
11176                 }
11177             }
11178           else
11179             {
11180               if (start_reg != reg)
11181                 {
11182                   insn = emit_sfm (reg + 1, start_reg - reg);
11183                   RTX_FRAME_RELATED_P (insn) = 1;
11184                   saved_size += (start_reg - reg) * 12;
11185                 }
11186               start_reg = reg - 1;
11187             }
11188         }
11189
11190       if (start_reg != reg)
11191         {
11192           insn = emit_sfm (reg + 1, start_reg - reg);
11193           saved_size += (start_reg - reg) * 12;
11194           RTX_FRAME_RELATED_P (insn) = 1;
11195         }
11196     }
11197   if (TARGET_HARD_FLOAT && TARGET_VFP)
11198     {
11199       start_reg = FIRST_VFP_REGNUM;
11200
11201       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11202         {
11203           if ((!regs_ever_live[reg] || call_used_regs[reg])
11204               && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
11205             {
11206               if (start_reg != reg)
11207                 saved_size += vfp_emit_fstmd (start_reg,
11208                                               (reg - start_reg) / 2);
11209               start_reg = reg + 2;
11210             }
11211         }
11212       if (start_reg != reg)
11213         saved_size += vfp_emit_fstmd (start_reg,
11214                                       (reg - start_reg) / 2);
11215     }
11216   return saved_size;
11217 }
11218
11219
11220 /* Set the Thumb frame pointer from the stack pointer.  */
11221
11222 static void
11223 thumb_set_frame_pointer (arm_stack_offsets *offsets)
11224 {
11225   HOST_WIDE_INT amount;
11226   rtx insn, dwarf;
11227
11228   amount = offsets->outgoing_args - offsets->locals_base;
11229   if (amount < 1024)
11230     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11231                                   stack_pointer_rtx, GEN_INT (amount)));
11232   else
11233     {
11234       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
11235       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11236                                     hard_frame_pointer_rtx,
11237                                     stack_pointer_rtx));
11238       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11239                            plus_constant (stack_pointer_rtx, amount));
11240       RTX_FRAME_RELATED_P (dwarf) = 1;
11241       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11242                                             REG_NOTES (insn));
11243     }
11244
11245   RTX_FRAME_RELATED_P (insn) = 1;
11246 }
11247
11248 /* Generate the prologue instructions for entry into an ARM or Thumb-2
11249    function.  */
11250 void
11251 arm_expand_prologue (void)
11252 {
11253   rtx amount;
11254   rtx insn;
11255   rtx ip_rtx;
11256   unsigned long live_regs_mask;
11257   unsigned long func_type;
11258   int fp_offset = 0;
11259   int saved_pretend_args = 0;
11260   int saved_regs = 0;
11261   unsigned HOST_WIDE_INT args_to_push;
11262   arm_stack_offsets *offsets;
11263
11264   func_type = arm_current_func_type ();
11265
11266   /* Naked functions don't have prologues.  */
11267   if (IS_NAKED (func_type))
11268     return;
11269
11270   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
11271   args_to_push = current_function_pretend_args_size;
11272
11273   /* Compute which register we will have to save onto the stack.  */
11274   live_regs_mask = arm_compute_save_reg_mask ();
11275
11276   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
11277
11278   if (IS_STACKALIGN (func_type))
11279     {
11280       rtx dwarf;
11281       rtx r0;
11282       rtx r1;
11283       /* Handle a word-aligned stack pointer.  We generate the following:
11284
11285           mov r0, sp
11286           bic r1, r0, #7
11287           mov sp, r1
11288           <save and restore r0 in normal prologue/epilogue>
11289           mov sp, r0
11290           bx lr
11291
11292          The unwinder doesn't need to know about the stack realignment.
11293          Just tell it we saved SP in r0.  */
11294       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
11295
11296       r0 = gen_rtx_REG (SImode, 0);
11297       r1 = gen_rtx_REG (SImode, 1);
11298       dwarf = gen_rtx_UNSPEC (SImode, NULL_RTVEC, UNSPEC_STACK_ALIGN);
11299       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
11300       insn = gen_movsi (r0, stack_pointer_rtx);
11301       RTX_FRAME_RELATED_P (insn) = 1;
11302       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11303                                             dwarf, REG_NOTES (insn));
11304       emit_insn (insn);
11305       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
11306       emit_insn (gen_movsi (stack_pointer_rtx, r1));
11307     }
11308
11309   if (frame_pointer_needed && TARGET_ARM)
11310     {
11311       if (IS_INTERRUPT (func_type))
11312         {
11313           /* Interrupt functions must not corrupt any registers.
11314              Creating a frame pointer however, corrupts the IP
11315              register, so we must push it first.  */
11316           insn = emit_multi_reg_push (1 << IP_REGNUM);
11317
11318           /* Do not set RTX_FRAME_RELATED_P on this insn.
11319              The dwarf stack unwinding code only wants to see one
11320              stack decrement per function, and this is not it.  If
11321              this instruction is labeled as being part of the frame
11322              creation sequence then dwarf2out_frame_debug_expr will
11323              die when it encounters the assignment of IP to FP
11324              later on, since the use of SP here establishes SP as
11325              the CFA register and not IP.
11326
11327              Anyway this instruction is not really part of the stack
11328              frame creation although it is part of the prologue.  */
11329         }
11330       else if (IS_NESTED (func_type))
11331         {
11332           /* The Static chain register is the same as the IP register
11333              used as a scratch register during stack frame creation.
11334              To get around this need to find somewhere to store IP
11335              whilst the frame is being created.  We try the following
11336              places in order:
11337
11338                1. The last argument register.
11339                2. A slot on the stack above the frame.  (This only
11340                   works if the function is not a varargs function).
11341                3. Register r3, after pushing the argument registers
11342                   onto the stack.
11343
11344              Note - we only need to tell the dwarf2 backend about the SP
11345              adjustment in the second variant; the static chain register
11346              doesn't need to be unwound, as it doesn't contain a value
11347              inherited from the caller.  */
11348
11349           if (regs_ever_live[3] == 0)
11350             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11351           else if (args_to_push == 0)
11352             {
11353               rtx dwarf;
11354
11355               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
11356               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
11357               fp_offset = 4;
11358
11359               /* Just tell the dwarf backend that we adjusted SP.  */
11360               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11361                                    plus_constant (stack_pointer_rtx,
11362                                                   -fp_offset));
11363               RTX_FRAME_RELATED_P (insn) = 1;
11364               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11365                                                     dwarf, REG_NOTES (insn));
11366             }
11367           else
11368             {
11369               /* Store the args on the stack.  */
11370               if (cfun->machine->uses_anonymous_args)
11371                 insn = emit_multi_reg_push
11372                   ((0xf0 >> (args_to_push / 4)) & 0xf);
11373               else
11374                 insn = emit_insn
11375                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11376                                GEN_INT (- args_to_push)));
11377
11378               RTX_FRAME_RELATED_P (insn) = 1;
11379
11380               saved_pretend_args = 1;
11381               fp_offset = args_to_push;
11382               args_to_push = 0;
11383
11384               /* Now reuse r3 to preserve IP.  */
11385               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11386             }
11387         }
11388
11389       insn = emit_set_insn (ip_rtx,
11390                             plus_constant (stack_pointer_rtx, fp_offset));
11391       RTX_FRAME_RELATED_P (insn) = 1;
11392     }
11393
11394   if (args_to_push)
11395     {
11396       /* Push the argument registers, or reserve space for them.  */
11397       if (cfun->machine->uses_anonymous_args)
11398         insn = emit_multi_reg_push
11399           ((0xf0 >> (args_to_push / 4)) & 0xf);
11400       else
11401         insn = emit_insn
11402           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11403                        GEN_INT (- args_to_push)));
11404       RTX_FRAME_RELATED_P (insn) = 1;
11405     }
11406
11407   /* If this is an interrupt service routine, and the link register
11408      is going to be pushed, and we are not creating a stack frame,
11409      (which would involve an extra push of IP and a pop in the epilogue)
11410      subtracting four from LR now will mean that the function return
11411      can be done with a single instruction.  */
11412   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
11413       && (live_regs_mask & (1 << LR_REGNUM)) != 0
11414       && ! frame_pointer_needed
11415       && TARGET_ARM)
11416     {
11417       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
11418       
11419       emit_set_insn (lr, plus_constant (lr, -4));
11420     }
11421
11422   if (live_regs_mask)
11423     {
11424       insn = emit_multi_reg_push (live_regs_mask);
11425       saved_regs += bit_count (live_regs_mask) * 4;
11426       RTX_FRAME_RELATED_P (insn) = 1;
11427     }
11428
11429   if (! IS_VOLATILE (func_type))
11430     saved_regs += arm_save_coproc_regs ();
11431
11432   if (frame_pointer_needed && TARGET_ARM)
11433     {
11434       /* Create the new frame pointer.  */
11435         {
11436           insn = GEN_INT (-(4 + args_to_push + fp_offset));
11437           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
11438           RTX_FRAME_RELATED_P (insn) = 1;
11439
11440           if (IS_NESTED (func_type))
11441             {
11442               /* Recover the static chain register.  */
11443               if (regs_ever_live [3] == 0
11444                   || saved_pretend_args)
11445                 insn = gen_rtx_REG (SImode, 3);
11446               else /* if (current_function_pretend_args_size == 0) */
11447                 {
11448                   insn = plus_constant (hard_frame_pointer_rtx, 4);
11449                   insn = gen_frame_mem (SImode, insn);
11450                 }
11451               emit_set_insn (ip_rtx, insn);
11452               /* Add a USE to stop propagate_one_insn() from barfing.  */
11453               emit_insn (gen_prologue_use (ip_rtx));
11454             }
11455         }
11456     }
11457
11458   offsets = arm_get_frame_offsets ();
11459   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
11460     {
11461       /* This add can produce multiple insns for a large constant, so we
11462          need to get tricky.  */
11463       rtx last = get_last_insn ();
11464
11465       amount = GEN_INT (offsets->saved_args + saved_regs
11466                         - offsets->outgoing_args);
11467
11468       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11469                                     amount));
11470       do
11471         {
11472           last = last ? NEXT_INSN (last) : get_insns ();
11473           RTX_FRAME_RELATED_P (last) = 1;
11474         }
11475       while (last != insn);
11476
11477       /* If the frame pointer is needed, emit a special barrier that
11478          will prevent the scheduler from moving stores to the frame
11479          before the stack adjustment.  */
11480       if (frame_pointer_needed)
11481         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
11482                                          hard_frame_pointer_rtx));
11483     }
11484
11485
11486   if (frame_pointer_needed && TARGET_THUMB2)
11487     thumb_set_frame_pointer (offsets);
11488
11489   if (flag_pic && arm_pic_register != INVALID_REGNUM)
11490     {
11491       unsigned long mask;
11492
11493       mask = live_regs_mask;
11494       mask &= THUMB2_WORK_REGS;
11495       if (!IS_NESTED (func_type))
11496         mask |= (1 << IP_REGNUM);
11497       arm_load_pic_register (mask);
11498     }
11499
11500   /* If we are profiling, make sure no instructions are scheduled before
11501      the call to mcount.  Similarly if the user has requested no
11502      scheduling in the prolog.  Similarly if we want non-call exceptions
11503      using the EABI unwinder, to prevent faulting instructions from being
11504      swapped with a stack adjustment.  */
11505   if (current_function_profile || !TARGET_SCHED_PROLOG
11506       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
11507     emit_insn (gen_blockage ());
11508
11509   /* If the link register is being kept alive, with the return address in it,
11510      then make sure that it does not get reused by the ce2 pass.  */
11511   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
11512     {
11513       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
11514       cfun->machine->lr_save_eliminated = 1;
11515     }
11516 }
11517 \f
11518 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
11519 static void
11520 arm_print_condition (FILE *stream)
11521 {
11522   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
11523     {
11524       /* Branch conversion is not implemented for Thumb-2.  */
11525       if (TARGET_THUMB)
11526         {
11527           output_operand_lossage ("predicated Thumb instruction");
11528           return;
11529         }
11530       if (current_insn_predicate != NULL)
11531         {
11532           output_operand_lossage
11533             ("predicated instruction in conditional sequence");
11534           return;
11535         }
11536
11537       fputs (arm_condition_codes[arm_current_cc], stream);
11538     }
11539   else if (current_insn_predicate)
11540     {
11541       enum arm_cond_code code;
11542
11543       if (TARGET_THUMB1)
11544         {
11545           output_operand_lossage ("predicated Thumb instruction");
11546           return;
11547         }
11548
11549       code = get_arm_condition_code (current_insn_predicate);
11550       fputs (arm_condition_codes[code], stream);
11551     }
11552 }
11553
11554
11555 /* If CODE is 'd', then the X is a condition operand and the instruction
11556    should only be executed if the condition is true.
11557    if CODE is 'D', then the X is a condition operand and the instruction
11558    should only be executed if the condition is false: however, if the mode
11559    of the comparison is CCFPEmode, then always execute the instruction -- we
11560    do this because in these circumstances !GE does not necessarily imply LT;
11561    in these cases the instruction pattern will take care to make sure that
11562    an instruction containing %d will follow, thereby undoing the effects of
11563    doing this instruction unconditionally.
11564    If CODE is 'N' then X is a floating point operand that must be negated
11565    before output.
11566    If CODE is 'B' then output a bitwise inverted value of X (a const int).
11567    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
11568 void
11569 arm_print_operand (FILE *stream, rtx x, int code)
11570 {
11571   switch (code)
11572     {
11573     case '@':
11574       fputs (ASM_COMMENT_START, stream);
11575       return;
11576
11577     case '_':
11578       fputs (user_label_prefix, stream);
11579       return;
11580
11581     case '|':
11582       fputs (REGISTER_PREFIX, stream);
11583       return;
11584
11585     case '?':
11586       arm_print_condition (stream);
11587       return;
11588
11589     case '(':
11590       /* Nothing in unified syntax, otherwise the current condition code.  */
11591       if (!TARGET_UNIFIED_ASM)
11592         arm_print_condition (stream);
11593       break;
11594
11595     case ')':
11596       /* The current condition code in unified syntax, otherwise nothing.  */
11597       if (TARGET_UNIFIED_ASM)
11598         arm_print_condition (stream);
11599       break;
11600   
11601     case '.':
11602       /* The current condition code for a condition code setting instruction.
11603          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
11604       if (TARGET_UNIFIED_ASM)
11605         {
11606           fputc('s', stream);
11607           arm_print_condition (stream);
11608         }
11609       else
11610         {
11611           arm_print_condition (stream);
11612           fputc('s', stream);
11613         }
11614       return;
11615
11616     case '!':
11617       /* If the instruction is conditionally executed then print
11618          the current condition code, otherwise print 's'.  */
11619       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
11620       if (current_insn_predicate)
11621         arm_print_condition (stream);
11622       else
11623         fputc('s', stream);
11624       break;
11625
11626     case 'N':
11627       {
11628         REAL_VALUE_TYPE r;
11629         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11630         r = REAL_VALUE_NEGATE (r);
11631         fprintf (stream, "%s", fp_const_from_val (&r));
11632       }
11633       return;
11634
11635     case 'B':
11636       if (GET_CODE (x) == CONST_INT)
11637         {
11638           HOST_WIDE_INT val;
11639           val = ARM_SIGN_EXTEND (~INTVAL (x));
11640           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
11641         }
11642       else
11643         {
11644           putc ('~', stream);
11645           output_addr_const (stream, x);
11646         }
11647       return;
11648
11649     case 'L':
11650       /* The low 16 bits of an immediate constant.  */
11651       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
11652       return;
11653
11654     case 'i':
11655       fprintf (stream, "%s", arithmetic_instr (x, 1));
11656       return;
11657
11658     /* Truncate Cirrus shift counts.  */
11659     case 's':
11660       if (GET_CODE (x) == CONST_INT)
11661         {
11662           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
11663           return;
11664         }
11665       arm_print_operand (stream, x, 0);
11666       return;
11667
11668     case 'I':
11669       fprintf (stream, "%s", arithmetic_instr (x, 0));
11670       return;
11671
11672     case 'S':
11673       {
11674         HOST_WIDE_INT val;
11675         const char *shift;
11676
11677         if (!shift_operator (x, SImode))
11678           {
11679             output_operand_lossage ("invalid shift operand");
11680             break;
11681           }
11682
11683         shift = shift_op (x, &val);
11684
11685         if (shift)
11686           {
11687             fprintf (stream, ", %s ", shift);
11688             if (val == -1)
11689               arm_print_operand (stream, XEXP (x, 1), 0);
11690             else
11691               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11692           }
11693       }
11694       return;
11695
11696       /* An explanation of the 'Q', 'R' and 'H' register operands:
11697
11698          In a pair of registers containing a DI or DF value the 'Q'
11699          operand returns the register number of the register containing
11700          the least significant part of the value.  The 'R' operand returns
11701          the register number of the register containing the most
11702          significant part of the value.
11703
11704          The 'H' operand returns the higher of the two register numbers.
11705          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11706          same as the 'Q' operand, since the most significant part of the
11707          value is held in the lower number register.  The reverse is true
11708          on systems where WORDS_BIG_ENDIAN is false.
11709
11710          The purpose of these operands is to distinguish between cases
11711          where the endian-ness of the values is important (for example
11712          when they are added together), and cases where the endian-ness
11713          is irrelevant, but the order of register operations is important.
11714          For example when loading a value from memory into a register
11715          pair, the endian-ness does not matter.  Provided that the value
11716          from the lower memory address is put into the lower numbered
11717          register, and the value from the higher address is put into the
11718          higher numbered register, the load will work regardless of whether
11719          the value being loaded is big-wordian or little-wordian.  The
11720          order of the two register loads can matter however, if the address
11721          of the memory location is actually held in one of the registers
11722          being overwritten by the load.  */
11723     case 'Q':
11724       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11725         {
11726           output_operand_lossage ("invalid operand for code '%c'", code);
11727           return;
11728         }
11729
11730       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11731       return;
11732
11733     case 'R':
11734       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11735         {
11736           output_operand_lossage ("invalid operand for code '%c'", code);
11737           return;
11738         }
11739
11740       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11741       return;
11742
11743     case 'H':
11744       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11745         {
11746           output_operand_lossage ("invalid operand for code '%c'", code);
11747           return;
11748         }
11749
11750       asm_fprintf (stream, "%r", REGNO (x) + 1);
11751       return;
11752
11753     case 'm':
11754       asm_fprintf (stream, "%r",
11755                    GET_CODE (XEXP (x, 0)) == REG
11756                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11757       return;
11758
11759     case 'M':
11760       asm_fprintf (stream, "{%r-%r}",
11761                    REGNO (x),
11762                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11763       return;
11764
11765     case 'd':
11766       /* CONST_TRUE_RTX means always -- that's the default.  */
11767       if (x == const_true_rtx)
11768         return;
11769
11770       if (!COMPARISON_P (x))
11771         {
11772           output_operand_lossage ("invalid operand for code '%c'", code);
11773           return;
11774         }
11775
11776       fputs (arm_condition_codes[get_arm_condition_code (x)],
11777              stream);
11778       return;
11779
11780     case 'D':
11781       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
11782          want to do that.  */
11783       if (x == const_true_rtx)
11784         {
11785           output_operand_lossage ("instruction never exectued");
11786           return;
11787         }
11788       if (!COMPARISON_P (x))
11789         {
11790           output_operand_lossage ("invalid operand for code '%c'", code);
11791           return;
11792         }
11793
11794       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11795                                  (get_arm_condition_code (x))],
11796              stream);
11797       return;
11798
11799     /* Cirrus registers can be accessed in a variety of ways:
11800          single floating point (f)
11801          double floating point (d)
11802          32bit integer         (fx)
11803          64bit integer         (dx).  */
11804     case 'W':                   /* Cirrus register in F mode.  */
11805     case 'X':                   /* Cirrus register in D mode.  */
11806     case 'Y':                   /* Cirrus register in FX mode.  */
11807     case 'Z':                   /* Cirrus register in DX mode.  */
11808       gcc_assert (GET_CODE (x) == REG
11809                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11810
11811       fprintf (stream, "mv%s%s",
11812                code == 'W' ? "f"
11813                : code == 'X' ? "d"
11814                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11815
11816       return;
11817
11818     /* Print cirrus register in the mode specified by the register's mode.  */
11819     case 'V':
11820       {
11821         int mode = GET_MODE (x);
11822
11823         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11824           {
11825             output_operand_lossage ("invalid operand for code '%c'", code);
11826             return;
11827           }
11828
11829         fprintf (stream, "mv%s%s",
11830                  mode == DFmode ? "d"
11831                  : mode == SImode ? "fx"
11832                  : mode == DImode ? "dx"
11833                  : "f", reg_names[REGNO (x)] + 2);
11834
11835         return;
11836       }
11837
11838     case 'U':
11839       if (GET_CODE (x) != REG
11840           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11841           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11842         /* Bad value for wCG register number.  */
11843         {
11844           output_operand_lossage ("invalid operand for code '%c'", code);
11845           return;
11846         }
11847
11848       else
11849         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11850       return;
11851
11852       /* Print an iWMMXt control register name.  */
11853     case 'w':
11854       if (GET_CODE (x) != CONST_INT
11855           || INTVAL (x) < 0
11856           || INTVAL (x) >= 16)
11857         /* Bad value for wC register number.  */
11858         {
11859           output_operand_lossage ("invalid operand for code '%c'", code);
11860           return;
11861         }
11862
11863       else
11864         {
11865           static const char * wc_reg_names [16] =
11866             {
11867               "wCID",  "wCon",  "wCSSF", "wCASF",
11868               "wC4",   "wC5",   "wC6",   "wC7",
11869               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11870               "wC12",  "wC13",  "wC14",  "wC15"
11871             };
11872
11873           fprintf (stream, wc_reg_names [INTVAL (x)]);
11874         }
11875       return;
11876
11877       /* Print a VFP double precision register name.  */
11878     case 'P':
11879       {
11880         int mode = GET_MODE (x);
11881         int num;
11882
11883         if (mode != DImode && mode != DFmode)
11884           {
11885             output_operand_lossage ("invalid operand for code '%c'", code);
11886             return;
11887           }
11888
11889         if (GET_CODE (x) != REG
11890             || !IS_VFP_REGNUM (REGNO (x)))
11891           {
11892             output_operand_lossage ("invalid operand for code '%c'", code);
11893             return;
11894           }
11895
11896         num = REGNO(x) - FIRST_VFP_REGNUM;
11897         if (num & 1)
11898           {
11899             output_operand_lossage ("invalid operand for code '%c'", code);
11900             return;
11901           }
11902
11903         fprintf (stream, "d%d", num >> 1);
11904       }
11905       return;
11906
11907     default:
11908       if (x == 0)
11909         {
11910           output_operand_lossage ("missing operand");
11911           return;
11912         }
11913
11914       switch (GET_CODE (x))
11915         {
11916         case REG:
11917           asm_fprintf (stream, "%r", REGNO (x));
11918           break;
11919
11920         case MEM:
11921           output_memory_reference_mode = GET_MODE (x);
11922           output_address (XEXP (x, 0));
11923           break;
11924
11925         case CONST_DOUBLE:
11926           fprintf (stream, "#%s", fp_immediate_constant (x));
11927           break;
11928
11929         default:
11930           gcc_assert (GET_CODE (x) != NEG);
11931           fputc ('#', stream);
11932           output_addr_const (stream, x);
11933           break;
11934         }
11935     }
11936 }
11937 \f
11938 #ifndef AOF_ASSEMBLER
11939 /* Target hook for assembling integer objects.  The ARM version needs to
11940    handle word-sized values specially.  */
11941 static bool
11942 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11943 {
11944   if (size == UNITS_PER_WORD && aligned_p)
11945     {
11946       fputs ("\t.word\t", asm_out_file);
11947       output_addr_const (asm_out_file, x);
11948
11949       /* Mark symbols as position independent.  We only do this in the
11950          .text segment, not in the .data segment.  */
11951       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11952           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11953         {
11954           if (GET_CODE (x) == SYMBOL_REF
11955               && (CONSTANT_POOL_ADDRESS_P (x)
11956                   || SYMBOL_REF_LOCAL_P (x)))
11957             fputs ("(GOTOFF)", asm_out_file);
11958           else if (GET_CODE (x) == LABEL_REF)
11959             fputs ("(GOTOFF)", asm_out_file);
11960           else
11961             fputs ("(GOT)", asm_out_file);
11962         }
11963       fputc ('\n', asm_out_file);
11964       return true;
11965     }
11966
11967   if (arm_vector_mode_supported_p (GET_MODE (x)))
11968     {
11969       int i, units;
11970
11971       gcc_assert (GET_CODE (x) == CONST_VECTOR);
11972
11973       units = CONST_VECTOR_NUNITS (x);
11974
11975       switch (GET_MODE (x))
11976         {
11977         case V2SImode: size = 4; break;
11978         case V4HImode: size = 2; break;
11979         case V8QImode: size = 1; break;
11980         default:
11981           gcc_unreachable ();
11982         }
11983
11984       for (i = 0; i < units; i++)
11985         {
11986           rtx elt;
11987
11988           elt = CONST_VECTOR_ELT (x, i);
11989           assemble_integer
11990             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11991         }
11992
11993       return true;
11994     }
11995
11996   return default_assemble_integer (x, size, aligned_p);
11997 }
11998
11999
12000 /* Add a function to the list of static constructors.  */
12001
12002 static void
12003 arm_elf_asm_constructor (rtx symbol, int priority)
12004 {
12005   section *s;
12006
12007   if (!TARGET_AAPCS_BASED)
12008     {
12009       default_named_section_asm_out_constructor (symbol, priority);
12010       return;
12011     }
12012
12013   /* Put these in the .init_array section, using a special relocation.  */
12014   if (priority != DEFAULT_INIT_PRIORITY)
12015     {
12016       char buf[18];
12017       sprintf (buf, ".init_array.%.5u", priority);
12018       s = get_section (buf, SECTION_WRITE, NULL_TREE);
12019     }
12020   else
12021     s = ctors_section;
12022
12023   switch_to_section (s);
12024   assemble_align (POINTER_SIZE);
12025   fputs ("\t.word\t", asm_out_file);
12026   output_addr_const (asm_out_file, symbol);
12027   fputs ("(target1)\n", asm_out_file);
12028 }
12029 #endif
12030 \f
12031 /* A finite state machine takes care of noticing whether or not instructions
12032    can be conditionally executed, and thus decrease execution time and code
12033    size by deleting branch instructions.  The fsm is controlled by
12034    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
12035
12036 /* The state of the fsm controlling condition codes are:
12037    0: normal, do nothing special
12038    1: make ASM_OUTPUT_OPCODE not output this instruction
12039    2: make ASM_OUTPUT_OPCODE not output this instruction
12040    3: make instructions conditional
12041    4: make instructions conditional
12042
12043    State transitions (state->state by whom under condition):
12044    0 -> 1 final_prescan_insn if the `target' is a label
12045    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
12046    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
12047    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
12048    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
12049           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
12050    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
12051           (the target insn is arm_target_insn).
12052
12053    If the jump clobbers the conditions then we use states 2 and 4.
12054
12055    A similar thing can be done with conditional return insns.
12056
12057    XXX In case the `target' is an unconditional branch, this conditionalising
12058    of the instructions always reduces code size, but not always execution
12059    time.  But then, I want to reduce the code size to somewhere near what
12060    /bin/cc produces.  */
12061
12062 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
12063    instructions.  When a COND_EXEC instruction is seen the subsequent
12064    instructions are scanned so that multiple conditional instructions can be
12065    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
12066    specify the length and true/false mask for the IT block.  These will be
12067    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
12068
12069 /* Returns the index of the ARM condition code string in
12070    `arm_condition_codes'.  COMPARISON should be an rtx like
12071    `(eq (...) (...))'.  */
12072 static enum arm_cond_code
12073 get_arm_condition_code (rtx comparison)
12074 {
12075   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
12076   int code;
12077   enum rtx_code comp_code = GET_CODE (comparison);
12078
12079   if (GET_MODE_CLASS (mode) != MODE_CC)
12080     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
12081                            XEXP (comparison, 1));
12082
12083   switch (mode)
12084     {
12085     case CC_DNEmode: code = ARM_NE; goto dominance;
12086     case CC_DEQmode: code = ARM_EQ; goto dominance;
12087     case CC_DGEmode: code = ARM_GE; goto dominance;
12088     case CC_DGTmode: code = ARM_GT; goto dominance;
12089     case CC_DLEmode: code = ARM_LE; goto dominance;
12090     case CC_DLTmode: code = ARM_LT; goto dominance;
12091     case CC_DGEUmode: code = ARM_CS; goto dominance;
12092     case CC_DGTUmode: code = ARM_HI; goto dominance;
12093     case CC_DLEUmode: code = ARM_LS; goto dominance;
12094     case CC_DLTUmode: code = ARM_CC;
12095
12096     dominance:
12097       gcc_assert (comp_code == EQ || comp_code == NE);
12098
12099       if (comp_code == EQ)
12100         return ARM_INVERSE_CONDITION_CODE (code);
12101       return code;
12102
12103     case CC_NOOVmode:
12104       switch (comp_code)
12105         {
12106         case NE: return ARM_NE;
12107         case EQ: return ARM_EQ;
12108         case GE: return ARM_PL;
12109         case LT: return ARM_MI;
12110         default: gcc_unreachable ();
12111         }
12112
12113     case CC_Zmode:
12114       switch (comp_code)
12115         {
12116         case NE: return ARM_NE;
12117         case EQ: return ARM_EQ;
12118         default: gcc_unreachable ();
12119         }
12120
12121     case CC_Nmode:
12122       switch (comp_code)
12123         {
12124         case NE: return ARM_MI;
12125         case EQ: return ARM_PL;
12126         default: gcc_unreachable ();
12127         }
12128
12129     case CCFPEmode:
12130     case CCFPmode:
12131       /* These encodings assume that AC=1 in the FPA system control
12132          byte.  This allows us to handle all cases except UNEQ and
12133          LTGT.  */
12134       switch (comp_code)
12135         {
12136         case GE: return ARM_GE;
12137         case GT: return ARM_GT;
12138         case LE: return ARM_LS;
12139         case LT: return ARM_MI;
12140         case NE: return ARM_NE;
12141         case EQ: return ARM_EQ;
12142         case ORDERED: return ARM_VC;
12143         case UNORDERED: return ARM_VS;
12144         case UNLT: return ARM_LT;
12145         case UNLE: return ARM_LE;
12146         case UNGT: return ARM_HI;
12147         case UNGE: return ARM_PL;
12148           /* UNEQ and LTGT do not have a representation.  */
12149         case UNEQ: /* Fall through.  */
12150         case LTGT: /* Fall through.  */
12151         default: gcc_unreachable ();
12152         }
12153
12154     case CC_SWPmode:
12155       switch (comp_code)
12156         {
12157         case NE: return ARM_NE;
12158         case EQ: return ARM_EQ;
12159         case GE: return ARM_LE;
12160         case GT: return ARM_LT;
12161         case LE: return ARM_GE;
12162         case LT: return ARM_GT;
12163         case GEU: return ARM_LS;
12164         case GTU: return ARM_CC;
12165         case LEU: return ARM_CS;
12166         case LTU: return ARM_HI;
12167         default: gcc_unreachable ();
12168         }
12169
12170     case CC_Cmode:
12171       switch (comp_code)
12172       {
12173       case LTU: return ARM_CS;
12174       case GEU: return ARM_CC;
12175       default: gcc_unreachable ();
12176       }
12177
12178     case CCmode:
12179       switch (comp_code)
12180         {
12181         case NE: return ARM_NE;
12182         case EQ: return ARM_EQ;
12183         case GE: return ARM_GE;
12184         case GT: return ARM_GT;
12185         case LE: return ARM_LE;
12186         case LT: return ARM_LT;
12187         case GEU: return ARM_CS;
12188         case GTU: return ARM_HI;
12189         case LEU: return ARM_LS;
12190         case LTU: return ARM_CC;
12191         default: gcc_unreachable ();
12192         }
12193
12194     default: gcc_unreachable ();
12195     }
12196 }
12197
12198 /* Tell arm_asm_ouput_opcode to output IT blocks for conditionally executed
12199    instructions.  */
12200 void
12201 thumb2_final_prescan_insn (rtx insn)
12202 {
12203   rtx first_insn = insn;
12204   rtx body = PATTERN (insn);
12205   rtx predicate;
12206   enum arm_cond_code code;
12207   int n;
12208   int mask;
12209
12210   /* Remove the previous insn from the count of insns to be output.  */
12211   if (arm_condexec_count)
12212       arm_condexec_count--;
12213
12214   /* Nothing to do if we are already inside a conditional block.  */
12215   if (arm_condexec_count)
12216     return;
12217
12218   if (GET_CODE (body) != COND_EXEC)
12219     return;
12220
12221   /* Conditional jumps are implemented directly.  */
12222   if (GET_CODE (insn) == JUMP_INSN)
12223     return;
12224
12225   predicate = COND_EXEC_TEST (body);
12226   arm_current_cc = get_arm_condition_code (predicate);
12227
12228   n = get_attr_ce_count (insn);
12229   arm_condexec_count = 1;
12230   arm_condexec_mask = (1 << n) - 1;
12231   arm_condexec_masklen = n;
12232   /* See if subsequent instructions can be combined into the same block.  */
12233   for (;;)
12234     {
12235       insn = next_nonnote_insn (insn);
12236
12237       /* Jumping into the middle of an IT block is illegal, so a label or
12238          barrier terminates the block.  */
12239       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
12240         break;
12241
12242       body = PATTERN (insn);
12243       /* USE and CLOBBER aren't really insns, so just skip them.  */
12244       if (GET_CODE (body) == USE
12245           || GET_CODE (body) == CLOBBER)
12246         {
12247           arm_condexec_count++;
12248           continue;
12249         }
12250
12251       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
12252       if (GET_CODE (body) != COND_EXEC)
12253         break;
12254       /* Allow up to 4 conditionally executed instructions in a block.  */
12255       n = get_attr_ce_count (insn);
12256       if (arm_condexec_masklen + n > 4)
12257         break;
12258
12259       predicate = COND_EXEC_TEST (body);
12260       code = get_arm_condition_code (predicate);
12261       mask = (1 << n) - 1;
12262       if (arm_current_cc == code)
12263         arm_condexec_mask |= (mask << arm_condexec_masklen);
12264       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
12265         break;
12266
12267       arm_condexec_count++;
12268       arm_condexec_masklen += n;
12269
12270       /* A jump must be the last instruction in a conditional block.  */
12271       if (GET_CODE(insn) == JUMP_INSN)
12272         break;
12273     }
12274   /* Restore recog_data (getting the attributes of other insns can
12275      destroy this array, but final.c assumes that it remains intact
12276      across this call).  */
12277   extract_constrain_insn_cached (first_insn);
12278 }
12279
12280 void
12281 arm_final_prescan_insn (rtx insn)
12282 {
12283   /* BODY will hold the body of INSN.  */
12284   rtx body = PATTERN (insn);
12285
12286   /* This will be 1 if trying to repeat the trick, and things need to be
12287      reversed if it appears to fail.  */
12288   int reverse = 0;
12289
12290   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
12291      taken are clobbered, even if the rtl suggests otherwise.  It also
12292      means that we have to grub around within the jump expression to find
12293      out what the conditions are when the jump isn't taken.  */
12294   int jump_clobbers = 0;
12295
12296   /* If we start with a return insn, we only succeed if we find another one.  */
12297   int seeking_return = 0;
12298
12299   /* START_INSN will hold the insn from where we start looking.  This is the
12300      first insn after the following code_label if REVERSE is true.  */
12301   rtx start_insn = insn;
12302
12303   /* If in state 4, check if the target branch is reached, in order to
12304      change back to state 0.  */
12305   if (arm_ccfsm_state == 4)
12306     {
12307       if (insn == arm_target_insn)
12308         {
12309           arm_target_insn = NULL;
12310           arm_ccfsm_state = 0;
12311         }
12312       return;
12313     }
12314
12315   /* If in state 3, it is possible to repeat the trick, if this insn is an
12316      unconditional branch to a label, and immediately following this branch
12317      is the previous target label which is only used once, and the label this
12318      branch jumps to is not too far off.  */
12319   if (arm_ccfsm_state == 3)
12320     {
12321       if (simplejump_p (insn))
12322         {
12323           start_insn = next_nonnote_insn (start_insn);
12324           if (GET_CODE (start_insn) == BARRIER)
12325             {
12326               /* XXX Isn't this always a barrier?  */
12327               start_insn = next_nonnote_insn (start_insn);
12328             }
12329           if (GET_CODE (start_insn) == CODE_LABEL
12330               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12331               && LABEL_NUSES (start_insn) == 1)
12332             reverse = TRUE;
12333           else
12334             return;
12335         }
12336       else if (GET_CODE (body) == RETURN)
12337         {
12338           start_insn = next_nonnote_insn (start_insn);
12339           if (GET_CODE (start_insn) == BARRIER)
12340             start_insn = next_nonnote_insn (start_insn);
12341           if (GET_CODE (start_insn) == CODE_LABEL
12342               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12343               && LABEL_NUSES (start_insn) == 1)
12344             {
12345               reverse = TRUE;
12346               seeking_return = 1;
12347             }
12348           else
12349             return;
12350         }
12351       else
12352         return;
12353     }
12354
12355   gcc_assert (!arm_ccfsm_state || reverse);
12356   if (GET_CODE (insn) != JUMP_INSN)
12357     return;
12358
12359   /* This jump might be paralleled with a clobber of the condition codes
12360      the jump should always come first */
12361   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
12362     body = XVECEXP (body, 0, 0);
12363
12364   if (reverse
12365       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
12366           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
12367     {
12368       int insns_skipped;
12369       int fail = FALSE, succeed = FALSE;
12370       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
12371       int then_not_else = TRUE;
12372       rtx this_insn = start_insn, label = 0;
12373
12374       /* If the jump cannot be done with one instruction, we cannot
12375          conditionally execute the instruction in the inverse case.  */
12376       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
12377         {
12378           jump_clobbers = 1;
12379           return;
12380         }
12381
12382       /* Register the insn jumped to.  */
12383       if (reverse)
12384         {
12385           if (!seeking_return)
12386             label = XEXP (SET_SRC (body), 0);
12387         }
12388       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
12389         label = XEXP (XEXP (SET_SRC (body), 1), 0);
12390       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
12391         {
12392           label = XEXP (XEXP (SET_SRC (body), 2), 0);
12393           then_not_else = FALSE;
12394         }
12395       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
12396         seeking_return = 1;
12397       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
12398         {
12399           seeking_return = 1;
12400           then_not_else = FALSE;
12401         }
12402       else
12403         gcc_unreachable ();
12404
12405       /* See how many insns this branch skips, and what kind of insns.  If all
12406          insns are okay, and the label or unconditional branch to the same
12407          label is not too far away, succeed.  */
12408       for (insns_skipped = 0;
12409            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
12410         {
12411           rtx scanbody;
12412
12413           this_insn = next_nonnote_insn (this_insn);
12414           if (!this_insn)
12415             break;
12416
12417           switch (GET_CODE (this_insn))
12418             {
12419             case CODE_LABEL:
12420               /* Succeed if it is the target label, otherwise fail since
12421                  control falls in from somewhere else.  */
12422               if (this_insn == label)
12423                 {
12424                   if (jump_clobbers)
12425                     {
12426                       arm_ccfsm_state = 2;
12427                       this_insn = next_nonnote_insn (this_insn);
12428                     }
12429                   else
12430                     arm_ccfsm_state = 1;
12431                   succeed = TRUE;
12432                 }
12433               else
12434                 fail = TRUE;
12435               break;
12436
12437             case BARRIER:
12438               /* Succeed if the following insn is the target label.
12439                  Otherwise fail.
12440                  If return insns are used then the last insn in a function
12441                  will be a barrier.  */
12442               this_insn = next_nonnote_insn (this_insn);
12443               if (this_insn && this_insn == label)
12444                 {
12445                   if (jump_clobbers)
12446                     {
12447                       arm_ccfsm_state = 2;
12448                       this_insn = next_nonnote_insn (this_insn);
12449                     }
12450                   else
12451                     arm_ccfsm_state = 1;
12452                   succeed = TRUE;
12453                 }
12454               else
12455                 fail = TRUE;
12456               break;
12457
12458             case CALL_INSN:
12459               /* The AAPCS says that conditional calls should not be
12460                  used since they make interworking inefficient (the
12461                  linker can't transform BL<cond> into BLX).  That's
12462                  only a problem if the machine has BLX.  */
12463               if (arm_arch5)
12464                 {
12465                   fail = TRUE;
12466                   break;
12467                 }
12468
12469               /* Succeed if the following insn is the target label, or
12470                  if the following two insns are a barrier and the
12471                  target label.  */
12472               this_insn = next_nonnote_insn (this_insn);
12473               if (this_insn && GET_CODE (this_insn) == BARRIER)
12474                 this_insn = next_nonnote_insn (this_insn);
12475
12476               if (this_insn && this_insn == label
12477                   && insns_skipped < max_insns_skipped)
12478                 {
12479                   if (jump_clobbers)
12480                     {
12481                       arm_ccfsm_state = 2;
12482                       this_insn = next_nonnote_insn (this_insn);
12483                     }
12484                   else
12485                     arm_ccfsm_state = 1;
12486                   succeed = TRUE;
12487                 }
12488               else
12489                 fail = TRUE;
12490               break;
12491
12492             case JUMP_INSN:
12493               /* If this is an unconditional branch to the same label, succeed.
12494                  If it is to another label, do nothing.  If it is conditional,
12495                  fail.  */
12496               /* XXX Probably, the tests for SET and the PC are
12497                  unnecessary.  */
12498
12499               scanbody = PATTERN (this_insn);
12500               if (GET_CODE (scanbody) == SET
12501                   && GET_CODE (SET_DEST (scanbody)) == PC)
12502                 {
12503                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
12504                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
12505                     {
12506                       arm_ccfsm_state = 2;
12507                       succeed = TRUE;
12508                     }
12509                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
12510                     fail = TRUE;
12511                 }
12512               /* Fail if a conditional return is undesirable (e.g. on a
12513                  StrongARM), but still allow this if optimizing for size.  */
12514               else if (GET_CODE (scanbody) == RETURN
12515                        && !use_return_insn (TRUE, NULL)
12516                        && !optimize_size)
12517                 fail = TRUE;
12518               else if (GET_CODE (scanbody) == RETURN
12519                        && seeking_return)
12520                 {
12521                   arm_ccfsm_state = 2;
12522                   succeed = TRUE;
12523                 }
12524               else if (GET_CODE (scanbody) == PARALLEL)
12525                 {
12526                   switch (get_attr_conds (this_insn))
12527                     {
12528                     case CONDS_NOCOND:
12529                       break;
12530                     default:
12531                       fail = TRUE;
12532                       break;
12533                     }
12534                 }
12535               else
12536                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
12537
12538               break;
12539
12540             case INSN:
12541               /* Instructions using or affecting the condition codes make it
12542                  fail.  */
12543               scanbody = PATTERN (this_insn);
12544               if (!(GET_CODE (scanbody) == SET
12545                     || GET_CODE (scanbody) == PARALLEL)
12546                   || get_attr_conds (this_insn) != CONDS_NOCOND)
12547                 fail = TRUE;
12548
12549               /* A conditional cirrus instruction must be followed by
12550                  a non Cirrus instruction.  However, since we
12551                  conditionalize instructions in this function and by
12552                  the time we get here we can't add instructions
12553                  (nops), because shorten_branches() has already been
12554                  called, we will disable conditionalizing Cirrus
12555                  instructions to be safe.  */
12556               if (GET_CODE (scanbody) != USE
12557                   && GET_CODE (scanbody) != CLOBBER
12558                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
12559                 fail = TRUE;
12560               break;
12561
12562             default:
12563               break;
12564             }
12565         }
12566       if (succeed)
12567         {
12568           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
12569             arm_target_label = CODE_LABEL_NUMBER (label);
12570           else
12571             {
12572               gcc_assert (seeking_return || arm_ccfsm_state == 2);
12573
12574               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
12575                 {
12576                   this_insn = next_nonnote_insn (this_insn);
12577                   gcc_assert (!this_insn
12578                               || (GET_CODE (this_insn) != BARRIER
12579                                   && GET_CODE (this_insn) != CODE_LABEL));
12580                 }
12581               if (!this_insn)
12582                 {
12583                   /* Oh, dear! we ran off the end.. give up.  */
12584                   extract_constrain_insn_cached (insn);
12585                   arm_ccfsm_state = 0;
12586                   arm_target_insn = NULL;
12587                   return;
12588                 }
12589               arm_target_insn = this_insn;
12590             }
12591           if (jump_clobbers)
12592             {
12593               gcc_assert (!reverse);
12594               arm_current_cc =
12595                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
12596                                                             0), 0), 1));
12597               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
12598                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12599               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
12600                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12601             }
12602           else
12603             {
12604               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
12605                  what it was.  */
12606               if (!reverse)
12607                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
12608                                                                0));
12609             }
12610
12611           if (reverse || then_not_else)
12612             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12613         }
12614
12615       /* Restore recog_data (getting the attributes of other insns can
12616          destroy this array, but final.c assumes that it remains intact
12617          across this call.  */
12618       extract_constrain_insn_cached (insn);
12619     }
12620 }
12621
12622 /* Output IT instructions.  */
12623 void
12624 thumb2_asm_output_opcode (FILE * stream)
12625 {
12626   char buff[5];
12627   int n;
12628
12629   if (arm_condexec_mask)
12630     {
12631       for (n = 0; n < arm_condexec_masklen; n++)
12632         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
12633       buff[n] = 0;
12634       asm_fprintf(stream, "i%s\t%s\n\t", buff,
12635                   arm_condition_codes[arm_current_cc]);
12636       arm_condexec_mask = 0;
12637     }
12638 }
12639
12640 /* Returns true if REGNO is a valid register
12641    for holding a quantity of type MODE.  */
12642 int
12643 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
12644 {
12645   if (GET_MODE_CLASS (mode) == MODE_CC)
12646     return (regno == CC_REGNUM
12647             || (TARGET_HARD_FLOAT && TARGET_VFP
12648                 && regno == VFPCC_REGNUM));
12649
12650   if (TARGET_THUMB1)
12651     /* For the Thumb we only allow values bigger than SImode in
12652        registers 0 - 6, so that there is always a second low
12653        register available to hold the upper part of the value.
12654        We probably we ought to ensure that the register is the
12655        start of an even numbered register pair.  */
12656     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
12657
12658   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
12659       && IS_CIRRUS_REGNUM (regno))
12660     /* We have outlawed SI values in Cirrus registers because they
12661        reside in the lower 32 bits, but SF values reside in the
12662        upper 32 bits.  This causes gcc all sorts of grief.  We can't
12663        even split the registers into pairs because Cirrus SI values
12664        get sign extended to 64bits-- aldyh.  */
12665     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
12666
12667   if (TARGET_HARD_FLOAT && TARGET_VFP
12668       && IS_VFP_REGNUM (regno))
12669     {
12670       if (mode == SFmode || mode == SImode)
12671         return TRUE;
12672
12673       /* DFmode values are only valid in even register pairs.  */
12674       if (mode == DFmode)
12675         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
12676       return FALSE;
12677     }
12678
12679   if (TARGET_REALLY_IWMMXT)
12680     {
12681       if (IS_IWMMXT_GR_REGNUM (regno))
12682         return mode == SImode;
12683
12684       if (IS_IWMMXT_REGNUM (regno))
12685         return VALID_IWMMXT_REG_MODE (mode);
12686     }
12687   
12688   /* We allow any value to be stored in the general registers.
12689      Restrict doubleword quantities to even register pairs so that we can
12690      use ldrd.  */
12691   if (regno <= LAST_ARM_REGNUM)
12692     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
12693
12694   if (regno == FRAME_POINTER_REGNUM
12695       || regno == ARG_POINTER_REGNUM)
12696     /* We only allow integers in the fake hard registers.  */
12697     return GET_MODE_CLASS (mode) == MODE_INT;
12698
12699   /* The only registers left are the FPA registers
12700      which we only allow to hold FP values.  */
12701   return (TARGET_HARD_FLOAT && TARGET_FPA
12702           && GET_MODE_CLASS (mode) == MODE_FLOAT
12703           && regno >= FIRST_FPA_REGNUM
12704           && regno <= LAST_FPA_REGNUM);
12705 }
12706
12707 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
12708    not used in arm mode.  */
12709 int
12710 arm_regno_class (int regno)
12711 {
12712   if (TARGET_THUMB1)
12713     {
12714       if (regno == STACK_POINTER_REGNUM)
12715         return STACK_REG;
12716       if (regno == CC_REGNUM)
12717         return CC_REG;
12718       if (regno < 8)
12719         return LO_REGS;
12720       return HI_REGS;
12721     }
12722
12723   if (TARGET_THUMB2 && regno < 8)
12724     return LO_REGS;
12725
12726   if (   regno <= LAST_ARM_REGNUM
12727       || regno == FRAME_POINTER_REGNUM
12728       || regno == ARG_POINTER_REGNUM)
12729     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
12730
12731   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
12732     return TARGET_THUMB2 ? CC_REG : NO_REGS;
12733
12734   if (IS_CIRRUS_REGNUM (regno))
12735     return CIRRUS_REGS;
12736
12737   if (IS_VFP_REGNUM (regno))
12738     return VFP_REGS;
12739
12740   if (IS_IWMMXT_REGNUM (regno))
12741     return IWMMXT_REGS;
12742
12743   if (IS_IWMMXT_GR_REGNUM (regno))
12744     return IWMMXT_GR_REGS;
12745
12746   return FPA_REGS;
12747 }
12748
12749 /* Handle a special case when computing the offset
12750    of an argument from the frame pointer.  */
12751 int
12752 arm_debugger_arg_offset (int value, rtx addr)
12753 {
12754   rtx insn;
12755
12756   /* We are only interested if dbxout_parms() failed to compute the offset.  */
12757   if (value != 0)
12758     return 0;
12759
12760   /* We can only cope with the case where the address is held in a register.  */
12761   if (GET_CODE (addr) != REG)
12762     return 0;
12763
12764   /* If we are using the frame pointer to point at the argument, then
12765      an offset of 0 is correct.  */
12766   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
12767     return 0;
12768
12769   /* If we are using the stack pointer to point at the
12770      argument, then an offset of 0 is correct.  */
12771   /* ??? Check this is consistent with thumb2 frame layout.  */
12772   if ((TARGET_THUMB || !frame_pointer_needed)
12773       && REGNO (addr) == SP_REGNUM)
12774     return 0;
12775
12776   /* Oh dear.  The argument is pointed to by a register rather
12777      than being held in a register, or being stored at a known
12778      offset from the frame pointer.  Since GDB only understands
12779      those two kinds of argument we must translate the address
12780      held in the register into an offset from the frame pointer.
12781      We do this by searching through the insns for the function
12782      looking to see where this register gets its value.  If the
12783      register is initialized from the frame pointer plus an offset
12784      then we are in luck and we can continue, otherwise we give up.
12785
12786      This code is exercised by producing debugging information
12787      for a function with arguments like this:
12788
12789            double func (double a, double b, int c, double d) {return d;}
12790
12791      Without this code the stab for parameter 'd' will be set to
12792      an offset of 0 from the frame pointer, rather than 8.  */
12793
12794   /* The if() statement says:
12795
12796      If the insn is a normal instruction
12797      and if the insn is setting the value in a register
12798      and if the register being set is the register holding the address of the argument
12799      and if the address is computing by an addition
12800      that involves adding to a register
12801      which is the frame pointer
12802      a constant integer
12803
12804      then...  */
12805
12806   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12807     {
12808       if (   GET_CODE (insn) == INSN
12809           && GET_CODE (PATTERN (insn)) == SET
12810           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12811           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12812           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12813           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12814           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12815              )
12816         {
12817           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12818
12819           break;
12820         }
12821     }
12822
12823   if (value == 0)
12824     {
12825       debug_rtx (addr);
12826       warning (0, "unable to compute real location of stacked parameter");
12827       value = 8; /* XXX magic hack */
12828     }
12829
12830   return value;
12831 }
12832 \f
12833 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
12834   do                                                                    \
12835     {                                                                   \
12836       if ((MASK) & insn_flags)                                          \
12837         add_builtin_function ((NAME), (TYPE), (CODE),                   \
12838                              BUILT_IN_MD, NULL, NULL_TREE);             \
12839     }                                                                   \
12840   while (0)
12841
12842 struct builtin_description
12843 {
12844   const unsigned int       mask;
12845   const enum insn_code     icode;
12846   const char * const       name;
12847   const enum arm_builtins  code;
12848   const enum rtx_code      comparison;
12849   const unsigned int       flag;
12850 };
12851
12852 static const struct builtin_description bdesc_2arg[] =
12853 {
12854 #define IWMMXT_BUILTIN(code, string, builtin) \
12855   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12856     ARM_BUILTIN_##builtin, 0, 0 },
12857
12858   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12859   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12860   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12861   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12862   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12863   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12864   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12865   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12866   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12867   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12868   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12869   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12870   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12871   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12872   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12873   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12874   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12875   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12876   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12877   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12878   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12879   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12880   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12881   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12882   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12883   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12884   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12885   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12886   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12887   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12888   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12889   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12890   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12891   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12892   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12893   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12894   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12895   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12896   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12897   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12898   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12899   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12900   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12901   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12902   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12903   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12904   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12905   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12906   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12907   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12908   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12909   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12910   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12911   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12912   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12913   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12914   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12915   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12916
12917 #define IWMMXT_BUILTIN2(code, builtin) \
12918   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12919
12920   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12921   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12922   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12923   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12924   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12925   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12926   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
12927   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
12928   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
12929   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
12930   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
12931   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
12932   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
12933   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
12934   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
12935   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
12936   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
12937   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
12938   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
12939   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
12940   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
12941   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
12942   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
12943   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
12944   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
12945   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
12946   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
12947   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
12948   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
12949   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
12950   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
12951   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
12952 };
12953
12954 static const struct builtin_description bdesc_1arg[] =
12955 {
12956   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12957   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12958   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12959   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12960   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12961   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12962   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12963   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12964   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12965   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12966   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12967   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12968   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12969   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12970   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12971   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12972   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12973   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12974 };
12975
12976 /* Set up all the iWMMXt builtins.  This is
12977    not called if TARGET_IWMMXT is zero.  */
12978
12979 static void
12980 arm_init_iwmmxt_builtins (void)
12981 {
12982   const struct builtin_description * d;
12983   size_t i;
12984   tree endlink = void_list_node;
12985
12986   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12987   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12988   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12989
12990   tree int_ftype_int
12991     = build_function_type (integer_type_node,
12992                            tree_cons (NULL_TREE, integer_type_node, endlink));
12993   tree v8qi_ftype_v8qi_v8qi_int
12994     = build_function_type (V8QI_type_node,
12995                            tree_cons (NULL_TREE, V8QI_type_node,
12996                                       tree_cons (NULL_TREE, V8QI_type_node,
12997                                                  tree_cons (NULL_TREE,
12998                                                             integer_type_node,
12999                                                             endlink))));
13000   tree v4hi_ftype_v4hi_int
13001     = build_function_type (V4HI_type_node,
13002                            tree_cons (NULL_TREE, V4HI_type_node,
13003                                       tree_cons (NULL_TREE, integer_type_node,
13004                                                  endlink)));
13005   tree v2si_ftype_v2si_int
13006     = build_function_type (V2SI_type_node,
13007                            tree_cons (NULL_TREE, V2SI_type_node,
13008                                       tree_cons (NULL_TREE, integer_type_node,
13009                                                  endlink)));
13010   tree v2si_ftype_di_di
13011     = build_function_type (V2SI_type_node,
13012                            tree_cons (NULL_TREE, long_long_integer_type_node,
13013                                       tree_cons (NULL_TREE, long_long_integer_type_node,
13014                                                  endlink)));
13015   tree di_ftype_di_int
13016     = build_function_type (long_long_integer_type_node,
13017                            tree_cons (NULL_TREE, long_long_integer_type_node,
13018                                       tree_cons (NULL_TREE, integer_type_node,
13019                                                  endlink)));
13020   tree di_ftype_di_int_int
13021     = build_function_type (long_long_integer_type_node,
13022                            tree_cons (NULL_TREE, long_long_integer_type_node,
13023                                       tree_cons (NULL_TREE, integer_type_node,
13024                                                  tree_cons (NULL_TREE,
13025                                                             integer_type_node,
13026                                                             endlink))));
13027   tree int_ftype_v8qi
13028     = build_function_type (integer_type_node,
13029                            tree_cons (NULL_TREE, V8QI_type_node,
13030                                       endlink));
13031   tree int_ftype_v4hi
13032     = build_function_type (integer_type_node,
13033                            tree_cons (NULL_TREE, V4HI_type_node,
13034                                       endlink));
13035   tree int_ftype_v2si
13036     = build_function_type (integer_type_node,
13037                            tree_cons (NULL_TREE, V2SI_type_node,
13038                                       endlink));
13039   tree int_ftype_v8qi_int
13040     = build_function_type (integer_type_node,
13041                            tree_cons (NULL_TREE, V8QI_type_node,
13042                                       tree_cons (NULL_TREE, integer_type_node,
13043                                                  endlink)));
13044   tree int_ftype_v4hi_int
13045     = build_function_type (integer_type_node,
13046                            tree_cons (NULL_TREE, V4HI_type_node,
13047                                       tree_cons (NULL_TREE, integer_type_node,
13048                                                  endlink)));
13049   tree int_ftype_v2si_int
13050     = build_function_type (integer_type_node,
13051                            tree_cons (NULL_TREE, V2SI_type_node,
13052                                       tree_cons (NULL_TREE, integer_type_node,
13053                                                  endlink)));
13054   tree v8qi_ftype_v8qi_int_int
13055     = build_function_type (V8QI_type_node,
13056                            tree_cons (NULL_TREE, V8QI_type_node,
13057                                       tree_cons (NULL_TREE, integer_type_node,
13058                                                  tree_cons (NULL_TREE,
13059                                                             integer_type_node,
13060                                                             endlink))));
13061   tree v4hi_ftype_v4hi_int_int
13062     = build_function_type (V4HI_type_node,
13063                            tree_cons (NULL_TREE, V4HI_type_node,
13064                                       tree_cons (NULL_TREE, integer_type_node,
13065                                                  tree_cons (NULL_TREE,
13066                                                             integer_type_node,
13067                                                             endlink))));
13068   tree v2si_ftype_v2si_int_int
13069     = build_function_type (V2SI_type_node,
13070                            tree_cons (NULL_TREE, V2SI_type_node,
13071                                       tree_cons (NULL_TREE, integer_type_node,
13072                                                  tree_cons (NULL_TREE,
13073                                                             integer_type_node,
13074                                                             endlink))));
13075   /* Miscellaneous.  */
13076   tree v8qi_ftype_v4hi_v4hi
13077     = build_function_type (V8QI_type_node,
13078                            tree_cons (NULL_TREE, V4HI_type_node,
13079                                       tree_cons (NULL_TREE, V4HI_type_node,
13080                                                  endlink)));
13081   tree v4hi_ftype_v2si_v2si
13082     = build_function_type (V4HI_type_node,
13083                            tree_cons (NULL_TREE, V2SI_type_node,
13084                                       tree_cons (NULL_TREE, V2SI_type_node,
13085                                                  endlink)));
13086   tree v2si_ftype_v4hi_v4hi
13087     = build_function_type (V2SI_type_node,
13088                            tree_cons (NULL_TREE, V4HI_type_node,
13089                                       tree_cons (NULL_TREE, V4HI_type_node,
13090                                                  endlink)));
13091   tree v2si_ftype_v8qi_v8qi
13092     = build_function_type (V2SI_type_node,
13093                            tree_cons (NULL_TREE, V8QI_type_node,
13094                                       tree_cons (NULL_TREE, V8QI_type_node,
13095                                                  endlink)));
13096   tree v4hi_ftype_v4hi_di
13097     = build_function_type (V4HI_type_node,
13098                            tree_cons (NULL_TREE, V4HI_type_node,
13099                                       tree_cons (NULL_TREE,
13100                                                  long_long_integer_type_node,
13101                                                  endlink)));
13102   tree v2si_ftype_v2si_di
13103     = build_function_type (V2SI_type_node,
13104                            tree_cons (NULL_TREE, V2SI_type_node,
13105                                       tree_cons (NULL_TREE,
13106                                                  long_long_integer_type_node,
13107                                                  endlink)));
13108   tree void_ftype_int_int
13109     = build_function_type (void_type_node,
13110                            tree_cons (NULL_TREE, integer_type_node,
13111                                       tree_cons (NULL_TREE, integer_type_node,
13112                                                  endlink)));
13113   tree di_ftype_void
13114     = build_function_type (long_long_unsigned_type_node, endlink);
13115   tree di_ftype_v8qi
13116     = build_function_type (long_long_integer_type_node,
13117                            tree_cons (NULL_TREE, V8QI_type_node,
13118                                       endlink));
13119   tree di_ftype_v4hi
13120     = build_function_type (long_long_integer_type_node,
13121                            tree_cons (NULL_TREE, V4HI_type_node,
13122                                       endlink));
13123   tree di_ftype_v2si
13124     = build_function_type (long_long_integer_type_node,
13125                            tree_cons (NULL_TREE, V2SI_type_node,
13126                                       endlink));
13127   tree v2si_ftype_v4hi
13128     = build_function_type (V2SI_type_node,
13129                            tree_cons (NULL_TREE, V4HI_type_node,
13130                                       endlink));
13131   tree v4hi_ftype_v8qi
13132     = build_function_type (V4HI_type_node,
13133                            tree_cons (NULL_TREE, V8QI_type_node,
13134                                       endlink));
13135
13136   tree di_ftype_di_v4hi_v4hi
13137     = build_function_type (long_long_unsigned_type_node,
13138                            tree_cons (NULL_TREE,
13139                                       long_long_unsigned_type_node,
13140                                       tree_cons (NULL_TREE, V4HI_type_node,
13141                                                  tree_cons (NULL_TREE,
13142                                                             V4HI_type_node,
13143                                                             endlink))));
13144
13145   tree di_ftype_v4hi_v4hi
13146     = build_function_type (long_long_unsigned_type_node,
13147                            tree_cons (NULL_TREE, V4HI_type_node,
13148                                       tree_cons (NULL_TREE, V4HI_type_node,
13149                                                  endlink)));
13150
13151   /* Normal vector binops.  */
13152   tree v8qi_ftype_v8qi_v8qi
13153     = build_function_type (V8QI_type_node,
13154                            tree_cons (NULL_TREE, V8QI_type_node,
13155                                       tree_cons (NULL_TREE, V8QI_type_node,
13156                                                  endlink)));
13157   tree v4hi_ftype_v4hi_v4hi
13158     = build_function_type (V4HI_type_node,
13159                            tree_cons (NULL_TREE, V4HI_type_node,
13160                                       tree_cons (NULL_TREE, V4HI_type_node,
13161                                                  endlink)));
13162   tree v2si_ftype_v2si_v2si
13163     = build_function_type (V2SI_type_node,
13164                            tree_cons (NULL_TREE, V2SI_type_node,
13165                                       tree_cons (NULL_TREE, V2SI_type_node,
13166                                                  endlink)));
13167   tree di_ftype_di_di
13168     = build_function_type (long_long_unsigned_type_node,
13169                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
13170                                       tree_cons (NULL_TREE,
13171                                                  long_long_unsigned_type_node,
13172                                                  endlink)));
13173
13174   /* Add all builtins that are more or less simple operations on two
13175      operands.  */
13176   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13177     {
13178       /* Use one of the operands; the target can have a different mode for
13179          mask-generating compares.  */
13180       enum machine_mode mode;
13181       tree type;
13182
13183       if (d->name == 0)
13184         continue;
13185
13186       mode = insn_data[d->icode].operand[1].mode;
13187
13188       switch (mode)
13189         {
13190         case V8QImode:
13191           type = v8qi_ftype_v8qi_v8qi;
13192           break;
13193         case V4HImode:
13194           type = v4hi_ftype_v4hi_v4hi;
13195           break;
13196         case V2SImode:
13197           type = v2si_ftype_v2si_v2si;
13198           break;
13199         case DImode:
13200           type = di_ftype_di_di;
13201           break;
13202
13203         default:
13204           gcc_unreachable ();
13205         }
13206
13207       def_mbuiltin (d->mask, d->name, type, d->code);
13208     }
13209
13210   /* Add the remaining MMX insns with somewhat more complicated types.  */
13211   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
13212   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
13213   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
13214
13215   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
13216   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
13217   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
13218   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
13219   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
13220   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
13221
13222   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
13223   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
13224   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
13225   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
13226   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
13227   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
13228
13229   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
13230   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
13231   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
13232   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
13233   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
13234   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
13235
13236   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
13237   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
13238   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
13239   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
13240   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
13241   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
13242
13243   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
13244
13245   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
13246   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
13247   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
13248   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
13249
13250   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
13251   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
13252   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
13253   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
13254   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
13255   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
13256   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
13257   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
13258   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
13259
13260   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
13261   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
13262   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
13263
13264   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
13265   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
13266   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
13267
13268   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
13269   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
13270   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
13271   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
13272   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
13273   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
13274
13275   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
13276   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
13277   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
13278   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
13279   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
13280   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
13281   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
13282   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
13283   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
13284   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
13285   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
13286   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
13287
13288   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
13289   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
13290   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
13291   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
13292
13293   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
13294   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
13295   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
13296   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
13297   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
13298   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
13299   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
13300 }
13301
13302 static void
13303 arm_init_tls_builtins (void)
13304 {
13305   tree ftype;
13306   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
13307   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
13308
13309   ftype = build_function_type (ptr_type_node, void_list_node);
13310   add_builtin_function ("__builtin_thread_pointer", ftype,
13311                         ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
13312                         NULL, const_nothrow);
13313 }
13314
13315 static void
13316 arm_init_builtins (void)
13317 {
13318   arm_init_tls_builtins ();
13319
13320   if (TARGET_REALLY_IWMMXT)
13321     arm_init_iwmmxt_builtins ();
13322 }
13323
13324 /* Errors in the source file can cause expand_expr to return const0_rtx
13325    where we expect a vector.  To avoid crashing, use one of the vector
13326    clear instructions.  */
13327
13328 static rtx
13329 safe_vector_operand (rtx x, enum machine_mode mode)
13330 {
13331   if (x != const0_rtx)
13332     return x;
13333   x = gen_reg_rtx (mode);
13334
13335   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
13336                                : gen_rtx_SUBREG (DImode, x, 0)));
13337   return x;
13338 }
13339
13340 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
13341
13342 static rtx
13343 arm_expand_binop_builtin (enum insn_code icode,
13344                           tree exp, rtx target)
13345 {
13346   rtx pat;
13347   tree arg0 = CALL_EXPR_ARG (exp, 0);
13348   tree arg1 = CALL_EXPR_ARG (exp, 1);
13349   rtx op0 = expand_normal (arg0);
13350   rtx op1 = expand_normal (arg1);
13351   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13352   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13353   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13354
13355   if (VECTOR_MODE_P (mode0))
13356     op0 = safe_vector_operand (op0, mode0);
13357   if (VECTOR_MODE_P (mode1))
13358     op1 = safe_vector_operand (op1, mode1);
13359
13360   if (! target
13361       || GET_MODE (target) != tmode
13362       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13363     target = gen_reg_rtx (tmode);
13364
13365   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
13366
13367   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13368     op0 = copy_to_mode_reg (mode0, op0);
13369   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13370     op1 = copy_to_mode_reg (mode1, op1);
13371
13372   pat = GEN_FCN (icode) (target, op0, op1);
13373   if (! pat)
13374     return 0;
13375   emit_insn (pat);
13376   return target;
13377 }
13378
13379 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
13380
13381 static rtx
13382 arm_expand_unop_builtin (enum insn_code icode,
13383                          tree exp, rtx target, int do_load)
13384 {
13385   rtx pat;
13386   tree arg0 = CALL_EXPR_ARG (exp, 0);
13387   rtx op0 = expand_normal (arg0);
13388   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13389   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13390
13391   if (! target
13392       || GET_MODE (target) != tmode
13393       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13394     target = gen_reg_rtx (tmode);
13395   if (do_load)
13396     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13397   else
13398     {
13399       if (VECTOR_MODE_P (mode0))
13400         op0 = safe_vector_operand (op0, mode0);
13401
13402       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13403         op0 = copy_to_mode_reg (mode0, op0);
13404     }
13405
13406   pat = GEN_FCN (icode) (target, op0);
13407   if (! pat)
13408     return 0;
13409   emit_insn (pat);
13410   return target;
13411 }
13412
13413 /* Expand an expression EXP that calls a built-in function,
13414    with result going to TARGET if that's convenient
13415    (and in mode MODE if that's convenient).
13416    SUBTARGET may be used as the target for computing one of EXP's operands.
13417    IGNORE is nonzero if the value is to be ignored.  */
13418
13419 static rtx
13420 arm_expand_builtin (tree exp,
13421                     rtx target,
13422                     rtx subtarget ATTRIBUTE_UNUSED,
13423                     enum machine_mode mode ATTRIBUTE_UNUSED,
13424                     int ignore ATTRIBUTE_UNUSED)
13425 {
13426   const struct builtin_description * d;
13427   enum insn_code    icode;
13428   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13429   tree              arg0;
13430   tree              arg1;
13431   tree              arg2;
13432   rtx               op0;
13433   rtx               op1;
13434   rtx               op2;
13435   rtx               pat;
13436   int               fcode = DECL_FUNCTION_CODE (fndecl);
13437   size_t            i;
13438   enum machine_mode tmode;
13439   enum machine_mode mode0;
13440   enum machine_mode mode1;
13441   enum machine_mode mode2;
13442
13443   switch (fcode)
13444     {
13445     case ARM_BUILTIN_TEXTRMSB:
13446     case ARM_BUILTIN_TEXTRMUB:
13447     case ARM_BUILTIN_TEXTRMSH:
13448     case ARM_BUILTIN_TEXTRMUH:
13449     case ARM_BUILTIN_TEXTRMSW:
13450     case ARM_BUILTIN_TEXTRMUW:
13451       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
13452                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
13453                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
13454                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
13455                : CODE_FOR_iwmmxt_textrmw);
13456
13457       arg0 = CALL_EXPR_ARG (exp, 0);
13458       arg1 = CALL_EXPR_ARG (exp, 1);
13459       op0 = expand_normal (arg0);
13460       op1 = expand_normal (arg1);
13461       tmode = insn_data[icode].operand[0].mode;
13462       mode0 = insn_data[icode].operand[1].mode;
13463       mode1 = insn_data[icode].operand[2].mode;
13464
13465       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13466         op0 = copy_to_mode_reg (mode0, op0);
13467       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13468         {
13469           /* @@@ better error message */
13470           error ("selector must be an immediate");
13471           return gen_reg_rtx (tmode);
13472         }
13473       if (target == 0
13474           || GET_MODE (target) != tmode
13475           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13476         target = gen_reg_rtx (tmode);
13477       pat = GEN_FCN (icode) (target, op0, op1);
13478       if (! pat)
13479         return 0;
13480       emit_insn (pat);
13481       return target;
13482
13483     case ARM_BUILTIN_TINSRB:
13484     case ARM_BUILTIN_TINSRH:
13485     case ARM_BUILTIN_TINSRW:
13486       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
13487                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
13488                : CODE_FOR_iwmmxt_tinsrw);
13489       arg0 = CALL_EXPR_ARG (exp, 0);
13490       arg1 = CALL_EXPR_ARG (exp, 1);
13491       arg2 = CALL_EXPR_ARG (exp, 2);
13492       op0 = expand_normal (arg0);
13493       op1 = expand_normal (arg1);
13494       op2 = expand_normal (arg2);
13495       tmode = insn_data[icode].operand[0].mode;
13496       mode0 = insn_data[icode].operand[1].mode;
13497       mode1 = insn_data[icode].operand[2].mode;
13498       mode2 = insn_data[icode].operand[3].mode;
13499
13500       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13501         op0 = copy_to_mode_reg (mode0, op0);
13502       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13503         op1 = copy_to_mode_reg (mode1, op1);
13504       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13505         {
13506           /* @@@ better error message */
13507           error ("selector must be an immediate");
13508           return const0_rtx;
13509         }
13510       if (target == 0
13511           || GET_MODE (target) != tmode
13512           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13513         target = gen_reg_rtx (tmode);
13514       pat = GEN_FCN (icode) (target, op0, op1, op2);
13515       if (! pat)
13516         return 0;
13517       emit_insn (pat);
13518       return target;
13519
13520     case ARM_BUILTIN_SETWCX:
13521       arg0 = CALL_EXPR_ARG (exp, 0);
13522       arg1 = CALL_EXPR_ARG (exp, 1);
13523       op0 = force_reg (SImode, expand_normal (arg0));
13524       op1 = expand_normal (arg1);
13525       emit_insn (gen_iwmmxt_tmcr (op1, op0));
13526       return 0;
13527
13528     case ARM_BUILTIN_GETWCX:
13529       arg0 = CALL_EXPR_ARG (exp, 0);
13530       op0 = expand_normal (arg0);
13531       target = gen_reg_rtx (SImode);
13532       emit_insn (gen_iwmmxt_tmrc (target, op0));
13533       return target;
13534
13535     case ARM_BUILTIN_WSHUFH:
13536       icode = CODE_FOR_iwmmxt_wshufh;
13537       arg0 = CALL_EXPR_ARG (exp, 0);
13538       arg1 = CALL_EXPR_ARG (exp, 1);
13539       op0 = expand_normal (arg0);
13540       op1 = expand_normal (arg1);
13541       tmode = insn_data[icode].operand[0].mode;
13542       mode1 = insn_data[icode].operand[1].mode;
13543       mode2 = insn_data[icode].operand[2].mode;
13544
13545       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13546         op0 = copy_to_mode_reg (mode1, op0);
13547       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13548         {
13549           /* @@@ better error message */
13550           error ("mask must be an immediate");
13551           return const0_rtx;
13552         }
13553       if (target == 0
13554           || GET_MODE (target) != tmode
13555           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13556         target = gen_reg_rtx (tmode);
13557       pat = GEN_FCN (icode) (target, op0, op1);
13558       if (! pat)
13559         return 0;
13560       emit_insn (pat);
13561       return target;
13562
13563     case ARM_BUILTIN_WSADB:
13564       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
13565     case ARM_BUILTIN_WSADH:
13566       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
13567     case ARM_BUILTIN_WSADBZ:
13568       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
13569     case ARM_BUILTIN_WSADHZ:
13570       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
13571
13572       /* Several three-argument builtins.  */
13573     case ARM_BUILTIN_WMACS:
13574     case ARM_BUILTIN_WMACU:
13575     case ARM_BUILTIN_WALIGN:
13576     case ARM_BUILTIN_TMIA:
13577     case ARM_BUILTIN_TMIAPH:
13578     case ARM_BUILTIN_TMIATT:
13579     case ARM_BUILTIN_TMIATB:
13580     case ARM_BUILTIN_TMIABT:
13581     case ARM_BUILTIN_TMIABB:
13582       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
13583                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
13584                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
13585                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
13586                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
13587                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
13588                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
13589                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
13590                : CODE_FOR_iwmmxt_walign);
13591       arg0 = CALL_EXPR_ARG (exp, 0);
13592       arg1 = CALL_EXPR_ARG (exp, 1);
13593       arg2 = CALL_EXPR_ARG (exp, 2);
13594       op0 = expand_normal (arg0);
13595       op1 = expand_normal (arg1);
13596       op2 = expand_normal (arg2);
13597       tmode = insn_data[icode].operand[0].mode;
13598       mode0 = insn_data[icode].operand[1].mode;
13599       mode1 = insn_data[icode].operand[2].mode;
13600       mode2 = insn_data[icode].operand[3].mode;
13601
13602       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13603         op0 = copy_to_mode_reg (mode0, op0);
13604       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13605         op1 = copy_to_mode_reg (mode1, op1);
13606       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13607         op2 = copy_to_mode_reg (mode2, op2);
13608       if (target == 0
13609           || GET_MODE (target) != tmode
13610           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13611         target = gen_reg_rtx (tmode);
13612       pat = GEN_FCN (icode) (target, op0, op1, op2);
13613       if (! pat)
13614         return 0;
13615       emit_insn (pat);
13616       return target;
13617
13618     case ARM_BUILTIN_WZERO:
13619       target = gen_reg_rtx (DImode);
13620       emit_insn (gen_iwmmxt_clrdi (target));
13621       return target;
13622
13623     case ARM_BUILTIN_THREAD_POINTER:
13624       return arm_load_tp (target);
13625
13626     default:
13627       break;
13628     }
13629
13630   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13631     if (d->code == (const enum arm_builtins) fcode)
13632       return arm_expand_binop_builtin (d->icode, exp, target);
13633
13634   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13635     if (d->code == (const enum arm_builtins) fcode)
13636       return arm_expand_unop_builtin (d->icode, exp, target, 0);
13637
13638   /* @@@ Should really do something sensible here.  */
13639   return NULL_RTX;
13640 }
13641 \f
13642 /* Return the number (counting from 0) of
13643    the least significant set bit in MASK.  */
13644
13645 inline static int
13646 number_of_first_bit_set (unsigned mask)
13647 {
13648   int bit;
13649
13650   for (bit = 0;
13651        (mask & (1 << bit)) == 0;
13652        ++bit)
13653     continue;
13654
13655   return bit;
13656 }
13657
13658 /* Emit code to push or pop registers to or from the stack.  F is the
13659    assembly file.  MASK is the registers to push or pop.  PUSH is
13660    nonzero if we should push, and zero if we should pop.  For debugging
13661    output, if pushing, adjust CFA_OFFSET by the amount of space added
13662    to the stack.  REAL_REGS should have the same number of bits set as
13663    MASK, and will be used instead (in the same order) to describe which
13664    registers were saved - this is used to mark the save slots when we
13665    push high registers after moving them to low registers.  */
13666 static void
13667 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
13668                unsigned long real_regs)
13669 {
13670   int regno;
13671   int lo_mask = mask & 0xFF;
13672   int pushed_words = 0;
13673
13674   gcc_assert (mask);
13675
13676   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
13677     {
13678       /* Special case.  Do not generate a POP PC statement here, do it in
13679          thumb_exit() */
13680       thumb_exit (f, -1);
13681       return;
13682     }
13683
13684   if (ARM_EABI_UNWIND_TABLES && push)
13685     {
13686       fprintf (f, "\t.save\t{");
13687       for (regno = 0; regno < 15; regno++)
13688         {
13689           if (real_regs & (1 << regno))
13690             {
13691               if (real_regs & ((1 << regno) -1))
13692                 fprintf (f, ", ");
13693               asm_fprintf (f, "%r", regno);
13694             }
13695         }
13696       fprintf (f, "}\n");
13697     }
13698
13699   fprintf (f, "\t%s\t{", push ? "push" : "pop");
13700
13701   /* Look at the low registers first.  */
13702   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
13703     {
13704       if (lo_mask & 1)
13705         {
13706           asm_fprintf (f, "%r", regno);
13707
13708           if ((lo_mask & ~1) != 0)
13709             fprintf (f, ", ");
13710
13711           pushed_words++;
13712         }
13713     }
13714
13715   if (push && (mask & (1 << LR_REGNUM)))
13716     {
13717       /* Catch pushing the LR.  */
13718       if (mask & 0xFF)
13719         fprintf (f, ", ");
13720
13721       asm_fprintf (f, "%r", LR_REGNUM);
13722
13723       pushed_words++;
13724     }
13725   else if (!push && (mask & (1 << PC_REGNUM)))
13726     {
13727       /* Catch popping the PC.  */
13728       if (TARGET_INTERWORK || TARGET_BACKTRACE
13729           || current_function_calls_eh_return)
13730         {
13731           /* The PC is never poped directly, instead
13732              it is popped into r3 and then BX is used.  */
13733           fprintf (f, "}\n");
13734
13735           thumb_exit (f, -1);
13736
13737           return;
13738         }
13739       else
13740         {
13741           if (mask & 0xFF)
13742             fprintf (f, ", ");
13743
13744           asm_fprintf (f, "%r", PC_REGNUM);
13745         }
13746     }
13747
13748   fprintf (f, "}\n");
13749
13750   if (push && pushed_words && dwarf2out_do_frame ())
13751     {
13752       char *l = dwarf2out_cfi_label ();
13753       int pushed_mask = real_regs;
13754
13755       *cfa_offset += pushed_words * 4;
13756       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
13757
13758       pushed_words = 0;
13759       pushed_mask = real_regs;
13760       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
13761         {
13762           if (pushed_mask & 1)
13763             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
13764         }
13765     }
13766 }
13767
13768 /* Generate code to return from a thumb function.
13769    If 'reg_containing_return_addr' is -1, then the return address is
13770    actually on the stack, at the stack pointer.  */
13771 static void
13772 thumb_exit (FILE *f, int reg_containing_return_addr)
13773 {
13774   unsigned regs_available_for_popping;
13775   unsigned regs_to_pop;
13776   int pops_needed;
13777   unsigned available;
13778   unsigned required;
13779   int mode;
13780   int size;
13781   int restore_a4 = FALSE;
13782
13783   /* Compute the registers we need to pop.  */
13784   regs_to_pop = 0;
13785   pops_needed = 0;
13786
13787   if (reg_containing_return_addr == -1)
13788     {
13789       regs_to_pop |= 1 << LR_REGNUM;
13790       ++pops_needed;
13791     }
13792
13793   if (TARGET_BACKTRACE)
13794     {
13795       /* Restore the (ARM) frame pointer and stack pointer.  */
13796       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
13797       pops_needed += 2;
13798     }
13799
13800   /* If there is nothing to pop then just emit the BX instruction and
13801      return.  */
13802   if (pops_needed == 0)
13803     {
13804       if (current_function_calls_eh_return)
13805         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13806
13807       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13808       return;
13809     }
13810   /* Otherwise if we are not supporting interworking and we have not created
13811      a backtrace structure and the function was not entered in ARM mode then
13812      just pop the return address straight into the PC.  */
13813   else if (!TARGET_INTERWORK
13814            && !TARGET_BACKTRACE
13815            && !is_called_in_ARM_mode (current_function_decl)
13816            && !current_function_calls_eh_return)
13817     {
13818       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13819       return;
13820     }
13821
13822   /* Find out how many of the (return) argument registers we can corrupt.  */
13823   regs_available_for_popping = 0;
13824
13825   /* If returning via __builtin_eh_return, the bottom three registers
13826      all contain information needed for the return.  */
13827   if (current_function_calls_eh_return)
13828     size = 12;
13829   else
13830     {
13831       /* If we can deduce the registers used from the function's
13832          return value.  This is more reliable that examining
13833          regs_ever_live[] because that will be set if the register is
13834          ever used in the function, not just if the register is used
13835          to hold a return value.  */
13836
13837       if (current_function_return_rtx != 0)
13838         mode = GET_MODE (current_function_return_rtx);
13839       else
13840         mode = DECL_MODE (DECL_RESULT (current_function_decl));
13841
13842       size = GET_MODE_SIZE (mode);
13843
13844       if (size == 0)
13845         {
13846           /* In a void function we can use any argument register.
13847              In a function that returns a structure on the stack
13848              we can use the second and third argument registers.  */
13849           if (mode == VOIDmode)
13850             regs_available_for_popping =
13851               (1 << ARG_REGISTER (1))
13852               | (1 << ARG_REGISTER (2))
13853               | (1 << ARG_REGISTER (3));
13854           else
13855             regs_available_for_popping =
13856               (1 << ARG_REGISTER (2))
13857               | (1 << ARG_REGISTER (3));
13858         }
13859       else if (size <= 4)
13860         regs_available_for_popping =
13861           (1 << ARG_REGISTER (2))
13862           | (1 << ARG_REGISTER (3));
13863       else if (size <= 8)
13864         regs_available_for_popping =
13865           (1 << ARG_REGISTER (3));
13866     }
13867
13868   /* Match registers to be popped with registers into which we pop them.  */
13869   for (available = regs_available_for_popping,
13870        required  = regs_to_pop;
13871        required != 0 && available != 0;
13872        available &= ~(available & - available),
13873        required  &= ~(required  & - required))
13874     -- pops_needed;
13875
13876   /* If we have any popping registers left over, remove them.  */
13877   if (available > 0)
13878     regs_available_for_popping &= ~available;
13879
13880   /* Otherwise if we need another popping register we can use
13881      the fourth argument register.  */
13882   else if (pops_needed)
13883     {
13884       /* If we have not found any free argument registers and
13885          reg a4 contains the return address, we must move it.  */
13886       if (regs_available_for_popping == 0
13887           && reg_containing_return_addr == LAST_ARG_REGNUM)
13888         {
13889           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13890           reg_containing_return_addr = LR_REGNUM;
13891         }
13892       else if (size > 12)
13893         {
13894           /* Register a4 is being used to hold part of the return value,
13895              but we have dire need of a free, low register.  */
13896           restore_a4 = TRUE;
13897
13898           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13899         }
13900
13901       if (reg_containing_return_addr != LAST_ARG_REGNUM)
13902         {
13903           /* The fourth argument register is available.  */
13904           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13905
13906           --pops_needed;
13907         }
13908     }
13909
13910   /* Pop as many registers as we can.  */
13911   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13912                  regs_available_for_popping);
13913
13914   /* Process the registers we popped.  */
13915   if (reg_containing_return_addr == -1)
13916     {
13917       /* The return address was popped into the lowest numbered register.  */
13918       regs_to_pop &= ~(1 << LR_REGNUM);
13919
13920       reg_containing_return_addr =
13921         number_of_first_bit_set (regs_available_for_popping);
13922
13923       /* Remove this register for the mask of available registers, so that
13924          the return address will not be corrupted by further pops.  */
13925       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13926     }
13927
13928   /* If we popped other registers then handle them here.  */
13929   if (regs_available_for_popping)
13930     {
13931       int frame_pointer;
13932
13933       /* Work out which register currently contains the frame pointer.  */
13934       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13935
13936       /* Move it into the correct place.  */
13937       asm_fprintf (f, "\tmov\t%r, %r\n",
13938                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13939
13940       /* (Temporarily) remove it from the mask of popped registers.  */
13941       regs_available_for_popping &= ~(1 << frame_pointer);
13942       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13943
13944       if (regs_available_for_popping)
13945         {
13946           int stack_pointer;
13947
13948           /* We popped the stack pointer as well,
13949              find the register that contains it.  */
13950           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13951
13952           /* Move it into the stack register.  */
13953           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13954
13955           /* At this point we have popped all necessary registers, so
13956              do not worry about restoring regs_available_for_popping
13957              to its correct value:
13958
13959              assert (pops_needed == 0)
13960              assert (regs_available_for_popping == (1 << frame_pointer))
13961              assert (regs_to_pop == (1 << STACK_POINTER))  */
13962         }
13963       else
13964         {
13965           /* Since we have just move the popped value into the frame
13966              pointer, the popping register is available for reuse, and
13967              we know that we still have the stack pointer left to pop.  */
13968           regs_available_for_popping |= (1 << frame_pointer);
13969         }
13970     }
13971
13972   /* If we still have registers left on the stack, but we no longer have
13973      any registers into which we can pop them, then we must move the return
13974      address into the link register and make available the register that
13975      contained it.  */
13976   if (regs_available_for_popping == 0 && pops_needed > 0)
13977     {
13978       regs_available_for_popping |= 1 << reg_containing_return_addr;
13979
13980       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13981                    reg_containing_return_addr);
13982
13983       reg_containing_return_addr = LR_REGNUM;
13984     }
13985
13986   /* If we have registers left on the stack then pop some more.
13987      We know that at most we will want to pop FP and SP.  */
13988   if (pops_needed > 0)
13989     {
13990       int  popped_into;
13991       int  move_to;
13992
13993       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13994                      regs_available_for_popping);
13995
13996       /* We have popped either FP or SP.
13997          Move whichever one it is into the correct register.  */
13998       popped_into = number_of_first_bit_set (regs_available_for_popping);
13999       move_to     = number_of_first_bit_set (regs_to_pop);
14000
14001       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
14002
14003       regs_to_pop &= ~(1 << move_to);
14004
14005       --pops_needed;
14006     }
14007
14008   /* If we still have not popped everything then we must have only
14009      had one register available to us and we are now popping the SP.  */
14010   if (pops_needed > 0)
14011     {
14012       int  popped_into;
14013
14014       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14015                      regs_available_for_popping);
14016
14017       popped_into = number_of_first_bit_set (regs_available_for_popping);
14018
14019       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
14020       /*
14021         assert (regs_to_pop == (1 << STACK_POINTER))
14022         assert (pops_needed == 1)
14023       */
14024     }
14025
14026   /* If necessary restore the a4 register.  */
14027   if (restore_a4)
14028     {
14029       if (reg_containing_return_addr != LR_REGNUM)
14030         {
14031           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
14032           reg_containing_return_addr = LR_REGNUM;
14033         }
14034
14035       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
14036     }
14037
14038   if (current_function_calls_eh_return)
14039     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
14040
14041   /* Return to caller.  */
14042   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
14043 }
14044
14045 \f
14046 void
14047 thumb1_final_prescan_insn (rtx insn)
14048 {
14049   if (flag_print_asm_name)
14050     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
14051                  INSN_ADDRESSES (INSN_UID (insn)));
14052 }
14053
14054 int
14055 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
14056 {
14057   unsigned HOST_WIDE_INT mask = 0xff;
14058   int i;
14059
14060   if (val == 0) /* XXX */
14061     return 0;
14062
14063   for (i = 0; i < 25; i++)
14064     if ((val & (mask << i)) == val)
14065       return 1;
14066
14067   return 0;
14068 }
14069
14070 /* Returns nonzero if the current function contains,
14071    or might contain a far jump.  */
14072 static int
14073 thumb_far_jump_used_p (void)
14074 {
14075   rtx insn;
14076
14077   /* This test is only important for leaf functions.  */
14078   /* assert (!leaf_function_p ()); */
14079
14080   /* If we have already decided that far jumps may be used,
14081      do not bother checking again, and always return true even if
14082      it turns out that they are not being used.  Once we have made
14083      the decision that far jumps are present (and that hence the link
14084      register will be pushed onto the stack) we cannot go back on it.  */
14085   if (cfun->machine->far_jump_used)
14086     return 1;
14087
14088   /* If this function is not being called from the prologue/epilogue
14089      generation code then it must be being called from the
14090      INITIAL_ELIMINATION_OFFSET macro.  */
14091   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
14092     {
14093       /* In this case we know that we are being asked about the elimination
14094          of the arg pointer register.  If that register is not being used,
14095          then there are no arguments on the stack, and we do not have to
14096          worry that a far jump might force the prologue to push the link
14097          register, changing the stack offsets.  In this case we can just
14098          return false, since the presence of far jumps in the function will
14099          not affect stack offsets.
14100
14101          If the arg pointer is live (or if it was live, but has now been
14102          eliminated and so set to dead) then we do have to test to see if
14103          the function might contain a far jump.  This test can lead to some
14104          false negatives, since before reload is completed, then length of
14105          branch instructions is not known, so gcc defaults to returning their
14106          longest length, which in turn sets the far jump attribute to true.
14107
14108          A false negative will not result in bad code being generated, but it
14109          will result in a needless push and pop of the link register.  We
14110          hope that this does not occur too often.
14111
14112          If we need doubleword stack alignment this could affect the other
14113          elimination offsets so we can't risk getting it wrong.  */
14114       if (regs_ever_live [ARG_POINTER_REGNUM])
14115         cfun->machine->arg_pointer_live = 1;
14116       else if (!cfun->machine->arg_pointer_live)
14117         return 0;
14118     }
14119
14120   /* Check to see if the function contains a branch
14121      insn with the far jump attribute set.  */
14122   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14123     {
14124       if (GET_CODE (insn) == JUMP_INSN
14125           /* Ignore tablejump patterns.  */
14126           && GET_CODE (PATTERN (insn)) != ADDR_VEC
14127           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
14128           && get_attr_far_jump (insn) == FAR_JUMP_YES
14129           )
14130         {
14131           /* Record the fact that we have decided that
14132              the function does use far jumps.  */
14133           cfun->machine->far_jump_used = 1;
14134           return 1;
14135         }
14136     }
14137
14138   return 0;
14139 }
14140
14141 /* Return nonzero if FUNC must be entered in ARM mode.  */
14142 int
14143 is_called_in_ARM_mode (tree func)
14144 {
14145   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
14146
14147   /* Ignore the problem about functions whose address is taken.  */
14148   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
14149     return TRUE;
14150
14151 #ifdef ARM_PE
14152   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
14153 #else
14154   return FALSE;
14155 #endif
14156 }
14157
14158 /* The bits which aren't usefully expanded as rtl.  */
14159 const char *
14160 thumb_unexpanded_epilogue (void)
14161 {
14162   int regno;
14163   unsigned long live_regs_mask = 0;
14164   int high_regs_pushed = 0;
14165   int had_to_push_lr;
14166   int size;
14167
14168   if (return_used_this_function)
14169     return "";
14170
14171   if (IS_NAKED (arm_current_func_type ()))
14172     return "";
14173
14174   live_regs_mask = thumb1_compute_save_reg_mask ();
14175   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14176
14177   /* If we can deduce the registers used from the function's return value.
14178      This is more reliable that examining regs_ever_live[] because that
14179      will be set if the register is ever used in the function, not just if
14180      the register is used to hold a return value.  */
14181   size = arm_size_return_regs ();
14182
14183   /* The prolog may have pushed some high registers to use as
14184      work registers.  e.g. the testsuite file:
14185      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
14186      compiles to produce:
14187         push    {r4, r5, r6, r7, lr}
14188         mov     r7, r9
14189         mov     r6, r8
14190         push    {r6, r7}
14191      as part of the prolog.  We have to undo that pushing here.  */
14192
14193   if (high_regs_pushed)
14194     {
14195       unsigned long mask = live_regs_mask & 0xff;
14196       int next_hi_reg;
14197
14198       /* The available low registers depend on the size of the value we are
14199          returning.  */
14200       if (size <= 12)
14201         mask |=  1 << 3;
14202       if (size <= 8)
14203         mask |= 1 << 2;
14204
14205       if (mask == 0)
14206         /* Oh dear!  We have no low registers into which we can pop
14207            high registers!  */
14208         internal_error
14209           ("no low registers available for popping high registers");
14210
14211       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
14212         if (live_regs_mask & (1 << next_hi_reg))
14213           break;
14214
14215       while (high_regs_pushed)
14216         {
14217           /* Find lo register(s) into which the high register(s) can
14218              be popped.  */
14219           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14220             {
14221               if (mask & (1 << regno))
14222                 high_regs_pushed--;
14223               if (high_regs_pushed == 0)
14224                 break;
14225             }
14226
14227           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
14228
14229           /* Pop the values into the low register(s).  */
14230           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
14231
14232           /* Move the value(s) into the high registers.  */
14233           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14234             {
14235               if (mask & (1 << regno))
14236                 {
14237                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
14238                                regno);
14239
14240                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
14241                     if (live_regs_mask & (1 << next_hi_reg))
14242                       break;
14243                 }
14244             }
14245         }
14246       live_regs_mask &= ~0x0f00;
14247     }
14248
14249   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
14250   live_regs_mask &= 0xff;
14251
14252   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
14253     {
14254       /* Pop the return address into the PC.  */
14255       if (had_to_push_lr)
14256         live_regs_mask |= 1 << PC_REGNUM;
14257
14258       /* Either no argument registers were pushed or a backtrace
14259          structure was created which includes an adjusted stack
14260          pointer, so just pop everything.  */
14261       if (live_regs_mask)
14262         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14263                        live_regs_mask);
14264
14265       /* We have either just popped the return address into the
14266          PC or it is was kept in LR for the entire function.  */
14267       if (!had_to_push_lr)
14268         thumb_exit (asm_out_file, LR_REGNUM);
14269     }
14270   else
14271     {
14272       /* Pop everything but the return address.  */
14273       if (live_regs_mask)
14274         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14275                        live_regs_mask);
14276
14277       if (had_to_push_lr)
14278         {
14279           if (size > 12)
14280             {
14281               /* We have no free low regs, so save one.  */
14282               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
14283                            LAST_ARG_REGNUM);
14284             }
14285
14286           /* Get the return address into a temporary register.  */
14287           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
14288                          1 << LAST_ARG_REGNUM);
14289
14290           if (size > 12)
14291             {
14292               /* Move the return address to lr.  */
14293               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
14294                            LAST_ARG_REGNUM);
14295               /* Restore the low register.  */
14296               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
14297                            IP_REGNUM);
14298               regno = LR_REGNUM;
14299             }
14300           else
14301             regno = LAST_ARG_REGNUM;
14302         }
14303       else
14304         regno = LR_REGNUM;
14305
14306       /* Remove the argument registers that were pushed onto the stack.  */
14307       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
14308                    SP_REGNUM, SP_REGNUM,
14309                    current_function_pretend_args_size);
14310
14311       thumb_exit (asm_out_file, regno);
14312     }
14313
14314   return "";
14315 }
14316
14317 /* Functions to save and restore machine-specific function data.  */
14318 static struct machine_function *
14319 arm_init_machine_status (void)
14320 {
14321   struct machine_function *machine;
14322   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
14323
14324 #if ARM_FT_UNKNOWN != 0
14325   machine->func_type = ARM_FT_UNKNOWN;
14326 #endif
14327   return machine;
14328 }
14329
14330 /* Return an RTX indicating where the return address to the
14331    calling function can be found.  */
14332 rtx
14333 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
14334 {
14335   if (count != 0)
14336     return NULL_RTX;
14337
14338   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
14339 }
14340
14341 /* Do anything needed before RTL is emitted for each function.  */
14342 void
14343 arm_init_expanders (void)
14344 {
14345   /* Arrange to initialize and mark the machine per-function status.  */
14346   init_machine_status = arm_init_machine_status;
14347
14348   /* This is to stop the combine pass optimizing away the alignment
14349      adjustment of va_arg.  */
14350   /* ??? It is claimed that this should not be necessary.  */
14351   if (cfun)
14352     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
14353 }
14354
14355
14356 /* Like arm_compute_initial_elimination offset.  Simpler because there
14357    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
14358    to point at the base of the local variables after static stack
14359    space for a function has been allocated.  */
14360
14361 HOST_WIDE_INT
14362 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
14363 {
14364   arm_stack_offsets *offsets;
14365
14366   offsets = arm_get_frame_offsets ();
14367
14368   switch (from)
14369     {
14370     case ARG_POINTER_REGNUM:
14371       switch (to)
14372         {
14373         case STACK_POINTER_REGNUM:
14374           return offsets->outgoing_args - offsets->saved_args;
14375
14376         case FRAME_POINTER_REGNUM:
14377           return offsets->soft_frame - offsets->saved_args;
14378
14379         case ARM_HARD_FRAME_POINTER_REGNUM:
14380           return offsets->saved_regs - offsets->saved_args;
14381
14382         case THUMB_HARD_FRAME_POINTER_REGNUM:
14383           return offsets->locals_base - offsets->saved_args;
14384
14385         default:
14386           gcc_unreachable ();
14387         }
14388       break;
14389
14390     case FRAME_POINTER_REGNUM:
14391       switch (to)
14392         {
14393         case STACK_POINTER_REGNUM:
14394           return offsets->outgoing_args - offsets->soft_frame;
14395
14396         case ARM_HARD_FRAME_POINTER_REGNUM:
14397           return offsets->saved_regs - offsets->soft_frame;
14398
14399         case THUMB_HARD_FRAME_POINTER_REGNUM:
14400           return offsets->locals_base - offsets->soft_frame;
14401
14402         default:
14403           gcc_unreachable ();
14404         }
14405       break;
14406
14407     default:
14408       gcc_unreachable ();
14409     }
14410 }
14411
14412 /* Generate the rest of a function's prologue.  */
14413 void
14414 thumb1_expand_prologue (void)
14415 {
14416   rtx insn, dwarf;
14417
14418   HOST_WIDE_INT amount;
14419   arm_stack_offsets *offsets;
14420   unsigned long func_type;
14421   int regno;
14422   unsigned long live_regs_mask;
14423
14424   func_type = arm_current_func_type ();
14425
14426   /* Naked functions don't have prologues.  */
14427   if (IS_NAKED (func_type))
14428     return;
14429
14430   if (IS_INTERRUPT (func_type))
14431     {
14432       error ("interrupt Service Routines cannot be coded in Thumb mode");
14433       return;
14434     }
14435
14436   live_regs_mask = thumb1_compute_save_reg_mask ();
14437   /* Load the pic register before setting the frame pointer,
14438      so we can use r7 as a temporary work register.  */
14439   if (flag_pic && arm_pic_register != INVALID_REGNUM)
14440     arm_load_pic_register (live_regs_mask);
14441
14442   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14443     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
14444                     stack_pointer_rtx);
14445
14446   offsets = arm_get_frame_offsets ();
14447   amount = offsets->outgoing_args - offsets->saved_regs;
14448   if (amount)
14449     {
14450       if (amount < 512)
14451         {
14452           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14453                                         GEN_INT (- amount)));
14454           RTX_FRAME_RELATED_P (insn) = 1;
14455         }
14456       else
14457         {
14458           rtx reg;
14459
14460           /* The stack decrement is too big for an immediate value in a single
14461              insn.  In theory we could issue multiple subtracts, but after
14462              three of them it becomes more space efficient to place the full
14463              value in the constant pool and load into a register.  (Also the
14464              ARM debugger really likes to see only one stack decrement per
14465              function).  So instead we look for a scratch register into which
14466              we can load the decrement, and then we subtract this from the
14467              stack pointer.  Unfortunately on the thumb the only available
14468              scratch registers are the argument registers, and we cannot use
14469              these as they may hold arguments to the function.  Instead we
14470              attempt to locate a call preserved register which is used by this
14471              function.  If we can find one, then we know that it will have
14472              been pushed at the start of the prologue and so we can corrupt
14473              it now.  */
14474           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
14475             if (live_regs_mask & (1 << regno)
14476                 && !(frame_pointer_needed
14477                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
14478               break;
14479
14480           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
14481             {
14482               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
14483
14484               /* Choose an arbitrary, non-argument low register.  */
14485               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
14486
14487               /* Save it by copying it into a high, scratch register.  */
14488               emit_insn (gen_movsi (spare, reg));
14489               /* Add a USE to stop propagate_one_insn() from barfing.  */
14490               emit_insn (gen_prologue_use (spare));
14491
14492               /* Decrement the stack.  */
14493               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14494               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14495                                             stack_pointer_rtx, reg));
14496               RTX_FRAME_RELATED_P (insn) = 1;
14497               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14498                                    plus_constant (stack_pointer_rtx,
14499                                                   -amount));
14500               RTX_FRAME_RELATED_P (dwarf) = 1;
14501               REG_NOTES (insn)
14502                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14503                                      REG_NOTES (insn));
14504
14505               /* Restore the low register's original value.  */
14506               emit_insn (gen_movsi (reg, spare));
14507
14508               /* Emit a USE of the restored scratch register, so that flow
14509                  analysis will not consider the restore redundant.  The
14510                  register won't be used again in this function and isn't
14511                  restored by the epilogue.  */
14512               emit_insn (gen_prologue_use (reg));
14513             }
14514           else
14515             {
14516               reg = gen_rtx_REG (SImode, regno);
14517
14518               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14519
14520               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14521                                             stack_pointer_rtx, reg));
14522               RTX_FRAME_RELATED_P (insn) = 1;
14523               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14524                                    plus_constant (stack_pointer_rtx,
14525                                                   -amount));
14526               RTX_FRAME_RELATED_P (dwarf) = 1;
14527               REG_NOTES (insn)
14528                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14529                                      REG_NOTES (insn));
14530             }
14531         }
14532     }
14533
14534   if (frame_pointer_needed)
14535     thumb_set_frame_pointer (offsets);
14536
14537   /* If we are profiling, make sure no instructions are scheduled before
14538      the call to mcount.  Similarly if the user has requested no
14539      scheduling in the prolog.  Similarly if we want non-call exceptions
14540      using the EABI unwinder, to prevent faulting instructions from being
14541      swapped with a stack adjustment.  */
14542   if (current_function_profile || !TARGET_SCHED_PROLOG
14543       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
14544     emit_insn (gen_blockage ());
14545
14546   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
14547   if (live_regs_mask & 0xff)
14548     cfun->machine->lr_save_eliminated = 0;
14549
14550   /* If the link register is being kept alive, with the return address in it,
14551      then make sure that it does not get reused by the ce2 pass.  */
14552   if (cfun->machine->lr_save_eliminated)
14553     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
14554 }
14555
14556
14557 void
14558 thumb1_expand_epilogue (void)
14559 {
14560   HOST_WIDE_INT amount;
14561   arm_stack_offsets *offsets;
14562   int regno;
14563
14564   /* Naked functions don't have prologues.  */
14565   if (IS_NAKED (arm_current_func_type ()))
14566     return;
14567
14568   offsets = arm_get_frame_offsets ();
14569   amount = offsets->outgoing_args - offsets->saved_regs;
14570
14571   if (frame_pointer_needed)
14572     {
14573       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
14574       amount = offsets->locals_base - offsets->saved_regs;
14575     }
14576
14577   if (amount)
14578     {
14579       if (amount < 512)
14580         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14581                                GEN_INT (amount)));
14582       else
14583         {
14584           /* r3 is always free in the epilogue.  */
14585           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
14586
14587           emit_insn (gen_movsi (reg, GEN_INT (amount)));
14588           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
14589         }
14590     }
14591
14592   /* Emit a USE (stack_pointer_rtx), so that
14593      the stack adjustment will not be deleted.  */
14594   emit_insn (gen_prologue_use (stack_pointer_rtx));
14595
14596   if (current_function_profile || !TARGET_SCHED_PROLOG)
14597     emit_insn (gen_blockage ());
14598
14599   /* Emit a clobber for each insn that will be restored in the epilogue,
14600      so that flow2 will get register lifetimes correct.  */
14601   for (regno = 0; regno < 13; regno++)
14602     if (regs_ever_live[regno] && !call_used_regs[regno])
14603       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
14604
14605   if (! regs_ever_live[LR_REGNUM])
14606     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
14607 }
14608
14609 static void
14610 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
14611 {
14612   unsigned long live_regs_mask = 0;
14613   unsigned long l_mask;
14614   unsigned high_regs_pushed = 0;
14615   int cfa_offset = 0;
14616   int regno;
14617
14618   if (IS_NAKED (arm_current_func_type ()))
14619     return;
14620
14621   if (is_called_in_ARM_mode (current_function_decl))
14622     {
14623       const char * name;
14624
14625       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
14626       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
14627                   == SYMBOL_REF);
14628       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
14629
14630       /* Generate code sequence to switch us into Thumb mode.  */
14631       /* The .code 32 directive has already been emitted by
14632          ASM_DECLARE_FUNCTION_NAME.  */
14633       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
14634       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
14635
14636       /* Generate a label, so that the debugger will notice the
14637          change in instruction sets.  This label is also used by
14638          the assembler to bypass the ARM code when this function
14639          is called from a Thumb encoded function elsewhere in the
14640          same file.  Hence the definition of STUB_NAME here must
14641          agree with the definition in gas/config/tc-arm.c.  */
14642
14643 #define STUB_NAME ".real_start_of"
14644
14645       fprintf (f, "\t.code\t16\n");
14646 #ifdef ARM_PE
14647       if (arm_dllexport_name_p (name))
14648         name = arm_strip_name_encoding (name);
14649 #endif
14650       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
14651       fprintf (f, "\t.thumb_func\n");
14652       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
14653     }
14654
14655   if (current_function_pretend_args_size)
14656     {
14657       /* Output unwind directive for the stack adjustment.  */
14658       if (ARM_EABI_UNWIND_TABLES)
14659         fprintf (f, "\t.pad #%d\n",
14660                  current_function_pretend_args_size);
14661
14662       if (cfun->machine->uses_anonymous_args)
14663         {
14664           int num_pushes;
14665
14666           fprintf (f, "\tpush\t{");
14667
14668           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
14669
14670           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
14671                regno <= LAST_ARG_REGNUM;
14672                regno++)
14673             asm_fprintf (f, "%r%s", regno,
14674                          regno == LAST_ARG_REGNUM ? "" : ", ");
14675
14676           fprintf (f, "}\n");
14677         }
14678       else
14679         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
14680                      SP_REGNUM, SP_REGNUM,
14681                      current_function_pretend_args_size);
14682
14683       /* We don't need to record the stores for unwinding (would it
14684          help the debugger any if we did?), but record the change in
14685          the stack pointer.  */
14686       if (dwarf2out_do_frame ())
14687         {
14688           char *l = dwarf2out_cfi_label ();
14689
14690           cfa_offset = cfa_offset + current_function_pretend_args_size;
14691           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
14692         }
14693     }
14694
14695   /* Get the registers we are going to push.  */
14696   live_regs_mask = thumb1_compute_save_reg_mask ();
14697   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
14698   l_mask = live_regs_mask & 0x40ff;
14699   /* Then count how many other high registers will need to be pushed.  */
14700   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14701
14702   if (TARGET_BACKTRACE)
14703     {
14704       unsigned offset;
14705       unsigned work_register;
14706
14707       /* We have been asked to create a stack backtrace structure.
14708          The code looks like this:
14709
14710          0   .align 2
14711          0   func:
14712          0     sub   SP, #16         Reserve space for 4 registers.
14713          2     push  {R7}            Push low registers.
14714          4     add   R7, SP, #20     Get the stack pointer before the push.
14715          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
14716          8     mov   R7, PC          Get hold of the start of this code plus 12.
14717         10     str   R7, [SP, #16]   Store it.
14718         12     mov   R7, FP          Get hold of the current frame pointer.
14719         14     str   R7, [SP, #4]    Store it.
14720         16     mov   R7, LR          Get hold of the current return address.
14721         18     str   R7, [SP, #12]   Store it.
14722         20     add   R7, SP, #16     Point at the start of the backtrace structure.
14723         22     mov   FP, R7          Put this value into the frame pointer.  */
14724
14725       work_register = thumb_find_work_register (live_regs_mask);
14726
14727       if (ARM_EABI_UNWIND_TABLES)
14728         asm_fprintf (f, "\t.pad #16\n");
14729
14730       asm_fprintf
14731         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
14732          SP_REGNUM, SP_REGNUM);
14733
14734       if (dwarf2out_do_frame ())
14735         {
14736           char *l = dwarf2out_cfi_label ();
14737
14738           cfa_offset = cfa_offset + 16;
14739           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
14740         }
14741
14742       if (l_mask)
14743         {
14744           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14745           offset = bit_count (l_mask) * UNITS_PER_WORD;
14746         }
14747       else
14748         offset = 0;
14749
14750       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14751                    offset + 16 + current_function_pretend_args_size);
14752
14753       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14754                    offset + 4);
14755
14756       /* Make sure that the instruction fetching the PC is in the right place
14757          to calculate "start of backtrace creation code + 12".  */
14758       if (l_mask)
14759         {
14760           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14761           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14762                        offset + 12);
14763           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14764                        ARM_HARD_FRAME_POINTER_REGNUM);
14765           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14766                        offset);
14767         }
14768       else
14769         {
14770           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14771                        ARM_HARD_FRAME_POINTER_REGNUM);
14772           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14773                        offset);
14774           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14775           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14776                        offset + 12);
14777         }
14778
14779       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
14780       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14781                    offset + 8);
14782       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14783                    offset + 12);
14784       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14785                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14786     }
14787   /* Optimization:  If we are not pushing any low registers but we are going
14788      to push some high registers then delay our first push.  This will just
14789      be a push of LR and we can combine it with the push of the first high
14790      register.  */
14791   else if ((l_mask & 0xff) != 0
14792            || (high_regs_pushed == 0 && l_mask))
14793     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14794
14795   if (high_regs_pushed)
14796     {
14797       unsigned pushable_regs;
14798       unsigned next_hi_reg;
14799
14800       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14801         if (live_regs_mask & (1 << next_hi_reg))
14802           break;
14803
14804       pushable_regs = l_mask & 0xff;
14805
14806       if (pushable_regs == 0)
14807         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14808
14809       while (high_regs_pushed > 0)
14810         {
14811           unsigned long real_regs_mask = 0;
14812
14813           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14814             {
14815               if (pushable_regs & (1 << regno))
14816                 {
14817                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14818
14819                   high_regs_pushed --;
14820                   real_regs_mask |= (1 << next_hi_reg);
14821
14822                   if (high_regs_pushed)
14823                     {
14824                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14825                            next_hi_reg --)
14826                         if (live_regs_mask & (1 << next_hi_reg))
14827                           break;
14828                     }
14829                   else
14830                     {
14831                       pushable_regs &= ~((1 << regno) - 1);
14832                       break;
14833                     }
14834                 }
14835             }
14836
14837           /* If we had to find a work register and we have not yet
14838              saved the LR then add it to the list of regs to push.  */
14839           if (l_mask == (1 << LR_REGNUM))
14840             {
14841               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14842                              1, &cfa_offset,
14843                              real_regs_mask | (1 << LR_REGNUM));
14844               l_mask = 0;
14845             }
14846           else
14847             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14848         }
14849     }
14850 }
14851
14852 /* Handle the case of a double word load into a low register from
14853    a computed memory address.  The computed address may involve a
14854    register which is overwritten by the load.  */
14855 const char *
14856 thumb_load_double_from_address (rtx *operands)
14857 {
14858   rtx addr;
14859   rtx base;
14860   rtx offset;
14861   rtx arg1;
14862   rtx arg2;
14863
14864   gcc_assert (GET_CODE (operands[0]) == REG);
14865   gcc_assert (GET_CODE (operands[1]) == MEM);
14866
14867   /* Get the memory address.  */
14868   addr = XEXP (operands[1], 0);
14869
14870   /* Work out how the memory address is computed.  */
14871   switch (GET_CODE (addr))
14872     {
14873     case REG:
14874       operands[2] = adjust_address (operands[1], SImode, 4);
14875
14876       if (REGNO (operands[0]) == REGNO (addr))
14877         {
14878           output_asm_insn ("ldr\t%H0, %2", operands);
14879           output_asm_insn ("ldr\t%0, %1", operands);
14880         }
14881       else
14882         {
14883           output_asm_insn ("ldr\t%0, %1", operands);
14884           output_asm_insn ("ldr\t%H0, %2", operands);
14885         }
14886       break;
14887
14888     case CONST:
14889       /* Compute <address> + 4 for the high order load.  */
14890       operands[2] = adjust_address (operands[1], SImode, 4);
14891
14892       output_asm_insn ("ldr\t%0, %1", operands);
14893       output_asm_insn ("ldr\t%H0, %2", operands);
14894       break;
14895
14896     case PLUS:
14897       arg1   = XEXP (addr, 0);
14898       arg2   = XEXP (addr, 1);
14899
14900       if (CONSTANT_P (arg1))
14901         base = arg2, offset = arg1;
14902       else
14903         base = arg1, offset = arg2;
14904
14905       gcc_assert (GET_CODE (base) == REG);
14906
14907       /* Catch the case of <address> = <reg> + <reg> */
14908       if (GET_CODE (offset) == REG)
14909         {
14910           int reg_offset = REGNO (offset);
14911           int reg_base   = REGNO (base);
14912           int reg_dest   = REGNO (operands[0]);
14913
14914           /* Add the base and offset registers together into the
14915              higher destination register.  */
14916           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14917                        reg_dest + 1, reg_base, reg_offset);
14918
14919           /* Load the lower destination register from the address in
14920              the higher destination register.  */
14921           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14922                        reg_dest, reg_dest + 1);
14923
14924           /* Load the higher destination register from its own address
14925              plus 4.  */
14926           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14927                        reg_dest + 1, reg_dest + 1);
14928         }
14929       else
14930         {
14931           /* Compute <address> + 4 for the high order load.  */
14932           operands[2] = adjust_address (operands[1], SImode, 4);
14933
14934           /* If the computed address is held in the low order register
14935              then load the high order register first, otherwise always
14936              load the low order register first.  */
14937           if (REGNO (operands[0]) == REGNO (base))
14938             {
14939               output_asm_insn ("ldr\t%H0, %2", operands);
14940               output_asm_insn ("ldr\t%0, %1", operands);
14941             }
14942           else
14943             {
14944               output_asm_insn ("ldr\t%0, %1", operands);
14945               output_asm_insn ("ldr\t%H0, %2", operands);
14946             }
14947         }
14948       break;
14949
14950     case LABEL_REF:
14951       /* With no registers to worry about we can just load the value
14952          directly.  */
14953       operands[2] = adjust_address (operands[1], SImode, 4);
14954
14955       output_asm_insn ("ldr\t%H0, %2", operands);
14956       output_asm_insn ("ldr\t%0, %1", operands);
14957       break;
14958
14959     default:
14960       gcc_unreachable ();
14961     }
14962
14963   return "";
14964 }
14965
14966 const char *
14967 thumb_output_move_mem_multiple (int n, rtx *operands)
14968 {
14969   rtx tmp;
14970
14971   switch (n)
14972     {
14973     case 2:
14974       if (REGNO (operands[4]) > REGNO (operands[5]))
14975         {
14976           tmp = operands[4];
14977           operands[4] = operands[5];
14978           operands[5] = tmp;
14979         }
14980       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14981       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14982       break;
14983
14984     case 3:
14985       if (REGNO (operands[4]) > REGNO (operands[5]))
14986         {
14987           tmp = operands[4];
14988           operands[4] = operands[5];
14989           operands[5] = tmp;
14990         }
14991       if (REGNO (operands[5]) > REGNO (operands[6]))
14992         {
14993           tmp = operands[5];
14994           operands[5] = operands[6];
14995           operands[6] = tmp;
14996         }
14997       if (REGNO (operands[4]) > REGNO (operands[5]))
14998         {
14999           tmp = operands[4];
15000           operands[4] = operands[5];
15001           operands[5] = tmp;
15002         }
15003
15004       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
15005       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
15006       break;
15007
15008     default:
15009       gcc_unreachable ();
15010     }
15011
15012   return "";
15013 }
15014
15015 /* Output a call-via instruction for thumb state.  */
15016 const char *
15017 thumb_call_via_reg (rtx reg)
15018 {
15019   int regno = REGNO (reg);
15020   rtx *labelp;
15021
15022   gcc_assert (regno < LR_REGNUM);
15023
15024   /* If we are in the normal text section we can use a single instance
15025      per compilation unit.  If we are doing function sections, then we need
15026      an entry per section, since we can't rely on reachability.  */
15027   if (in_section == text_section)
15028     {
15029       thumb_call_reg_needed = 1;
15030
15031       if (thumb_call_via_label[regno] == NULL)
15032         thumb_call_via_label[regno] = gen_label_rtx ();
15033       labelp = thumb_call_via_label + regno;
15034     }
15035   else
15036     {
15037       if (cfun->machine->call_via[regno] == NULL)
15038         cfun->machine->call_via[regno] = gen_label_rtx ();
15039       labelp = cfun->machine->call_via + regno;
15040     }
15041
15042   output_asm_insn ("bl\t%a0", labelp);
15043   return "";
15044 }
15045
15046 /* Routines for generating rtl.  */
15047 void
15048 thumb_expand_movmemqi (rtx *operands)
15049 {
15050   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
15051   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
15052   HOST_WIDE_INT len = INTVAL (operands[2]);
15053   HOST_WIDE_INT offset = 0;
15054
15055   while (len >= 12)
15056     {
15057       emit_insn (gen_movmem12b (out, in, out, in));
15058       len -= 12;
15059     }
15060
15061   if (len >= 8)
15062     {
15063       emit_insn (gen_movmem8b (out, in, out, in));
15064       len -= 8;
15065     }
15066
15067   if (len >= 4)
15068     {
15069       rtx reg = gen_reg_rtx (SImode);
15070       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
15071       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
15072       len -= 4;
15073       offset += 4;
15074     }
15075
15076   if (len >= 2)
15077     {
15078       rtx reg = gen_reg_rtx (HImode);
15079       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
15080                                               plus_constant (in, offset))));
15081       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
15082                             reg));
15083       len -= 2;
15084       offset += 2;
15085     }
15086
15087   if (len)
15088     {
15089       rtx reg = gen_reg_rtx (QImode);
15090       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
15091                                               plus_constant (in, offset))));
15092       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
15093                             reg));
15094     }
15095 }
15096
15097 void
15098 thumb_reload_out_hi (rtx *operands)
15099 {
15100   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
15101 }
15102
15103 /* Handle reading a half-word from memory during reload.  */
15104 void
15105 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
15106 {
15107   gcc_unreachable ();
15108 }
15109
15110 /* Return the length of a function name prefix
15111     that starts with the character 'c'.  */
15112 static int
15113 arm_get_strip_length (int c)
15114 {
15115   switch (c)
15116     {
15117     ARM_NAME_ENCODING_LENGTHS
15118       default: return 0;
15119     }
15120 }
15121
15122 /* Return a pointer to a function's name with any
15123    and all prefix encodings stripped from it.  */
15124 const char *
15125 arm_strip_name_encoding (const char *name)
15126 {
15127   int skip;
15128
15129   while ((skip = arm_get_strip_length (* name)))
15130     name += skip;
15131
15132   return name;
15133 }
15134
15135 /* If there is a '*' anywhere in the name's prefix, then
15136    emit the stripped name verbatim, otherwise prepend an
15137    underscore if leading underscores are being used.  */
15138 void
15139 arm_asm_output_labelref (FILE *stream, const char *name)
15140 {
15141   int skip;
15142   int verbatim = 0;
15143
15144   while ((skip = arm_get_strip_length (* name)))
15145     {
15146       verbatim |= (*name == '*');
15147       name += skip;
15148     }
15149
15150   if (verbatim)
15151     fputs (name, stream);
15152   else
15153     asm_fprintf (stream, "%U%s", name);
15154 }
15155
15156 static void
15157 arm_file_start (void)
15158 {
15159   int val;
15160
15161   if (TARGET_UNIFIED_ASM)
15162     asm_fprintf (asm_out_file, "\t.syntax unified\n");
15163
15164   if (TARGET_BPABI)
15165     {
15166       const char *fpu_name;
15167       if (arm_select[0].string)
15168         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
15169       else if (arm_select[1].string)
15170         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
15171       else
15172         asm_fprintf (asm_out_file, "\t.cpu %s\n",
15173                      all_cores[arm_default_cpu].name);
15174
15175       if (TARGET_SOFT_FLOAT)
15176         {
15177           if (TARGET_VFP)
15178             fpu_name = "softvfp";
15179           else
15180             fpu_name = "softfpa";
15181         }
15182       else
15183         {
15184           switch (arm_fpu_arch)
15185             {
15186             case FPUTYPE_FPA:
15187               fpu_name = "fpa";
15188               break;
15189             case FPUTYPE_FPA_EMU2:
15190               fpu_name = "fpe2";
15191               break;
15192             case FPUTYPE_FPA_EMU3:
15193               fpu_name = "fpe3";
15194               break;
15195             case FPUTYPE_MAVERICK:
15196               fpu_name = "maverick";
15197               break;
15198             case FPUTYPE_VFP:
15199               if (TARGET_HARD_FLOAT)
15200                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
15201               if (TARGET_HARD_FLOAT_ABI)
15202                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
15203               fpu_name = "vfp";
15204               break;
15205             default:
15206               abort();
15207             }
15208         }
15209       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
15210
15211       /* Some of these attributes only apply when the corresponding features
15212          are used.  However we don't have any easy way of figuring this out.
15213          Conservatively record the setting that would have been used.  */
15214
15215       /* Tag_ABI_PCS_wchar_t.  */
15216       asm_fprintf (asm_out_file, "\t.eabi_attribute 18, %d\n",
15217                    (int)WCHAR_TYPE_SIZE / BITS_PER_UNIT);
15218
15219       /* Tag_ABI_FP_rounding.  */
15220       if (flag_rounding_math)
15221         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
15222       if (!flag_unsafe_math_optimizations)
15223         {
15224           /* Tag_ABI_FP_denomal.  */
15225           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
15226           /* Tag_ABI_FP_exceptions.  */
15227           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
15228         }
15229       /* Tag_ABI_FP_user_exceptions.  */
15230       if (flag_signaling_nans)
15231         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
15232       /* Tag_ABI_FP_number_model.  */
15233       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
15234                    flag_finite_math_only ? 1 : 3);
15235
15236       /* Tag_ABI_align8_needed.  */
15237       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
15238       /* Tag_ABI_align8_preserved.  */
15239       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
15240       /* Tag_ABI_enum_size.  */
15241       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
15242                    flag_short_enums ? 1 : 2);
15243
15244       /* Tag_ABI_optimization_goals.  */
15245       if (optimize_size)
15246         val = 4;
15247       else if (optimize >= 2)
15248         val = 2;
15249       else if (optimize)
15250         val = 1;
15251       else
15252         val = 6;
15253       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
15254     }
15255   default_file_start();
15256 }
15257
15258 static void
15259 arm_file_end (void)
15260 {
15261   int regno;
15262
15263   if (NEED_INDICATE_EXEC_STACK)
15264     /* Add .note.GNU-stack.  */
15265     file_end_indicate_exec_stack ();
15266
15267   if (! thumb_call_reg_needed)
15268     return;
15269
15270   switch_to_section (text_section);
15271   asm_fprintf (asm_out_file, "\t.code 16\n");
15272   ASM_OUTPUT_ALIGN (asm_out_file, 1);
15273
15274   for (regno = 0; regno < LR_REGNUM; regno++)
15275     {
15276       rtx label = thumb_call_via_label[regno];
15277
15278       if (label != 0)
15279         {
15280           targetm.asm_out.internal_label (asm_out_file, "L",
15281                                           CODE_LABEL_NUMBER (label));
15282           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
15283         }
15284     }
15285 }
15286
15287 rtx aof_pic_label;
15288
15289 #ifdef AOF_ASSEMBLER
15290 /* Special functions only needed when producing AOF syntax assembler.  */
15291
15292 struct pic_chain
15293 {
15294   struct pic_chain * next;
15295   const char * symname;
15296 };
15297
15298 static struct pic_chain * aof_pic_chain = NULL;
15299
15300 rtx
15301 aof_pic_entry (rtx x)
15302 {
15303   struct pic_chain ** chainp;
15304   int offset;
15305
15306   if (aof_pic_label == NULL_RTX)
15307     {
15308       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
15309     }
15310
15311   for (offset = 0, chainp = &aof_pic_chain; *chainp;
15312        offset += 4, chainp = &(*chainp)->next)
15313     if ((*chainp)->symname == XSTR (x, 0))
15314       return plus_constant (aof_pic_label, offset);
15315
15316   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
15317   (*chainp)->next = NULL;
15318   (*chainp)->symname = XSTR (x, 0);
15319   return plus_constant (aof_pic_label, offset);
15320 }
15321
15322 void
15323 aof_dump_pic_table (FILE *f)
15324 {
15325   struct pic_chain * chain;
15326
15327   if (aof_pic_chain == NULL)
15328     return;
15329
15330   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
15331                PIC_OFFSET_TABLE_REGNUM,
15332                PIC_OFFSET_TABLE_REGNUM);
15333   fputs ("|x$adcons|\n", f);
15334
15335   for (chain = aof_pic_chain; chain; chain = chain->next)
15336     {
15337       fputs ("\tDCD\t", f);
15338       assemble_name (f, chain->symname);
15339       fputs ("\n", f);
15340     }
15341 }
15342
15343 int arm_text_section_count = 1;
15344
15345 /* A get_unnamed_section callback for switching to the text section.  */
15346
15347 static void
15348 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15349 {
15350   fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
15351            arm_text_section_count++);
15352   if (flag_pic)
15353     fprintf (asm_out_file, ", PIC, REENTRANT");
15354   fprintf (asm_out_file, "\n");
15355 }
15356
15357 static int arm_data_section_count = 1;
15358
15359 /* A get_unnamed_section callback for switching to the data section.  */
15360
15361 static void
15362 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15363 {
15364   fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
15365            arm_data_section_count++);
15366 }
15367
15368 /* Implement TARGET_ASM_INIT_SECTIONS.
15369
15370    AOF Assembler syntax is a nightmare when it comes to areas, since once
15371    we change from one area to another, we can't go back again.  Instead,
15372    we must create a new area with the same attributes and add the new output
15373    to that.  Unfortunately, there is nothing we can do here to guarantee that
15374    two areas with the same attributes will be linked adjacently in the
15375    resulting executable, so we have to be careful not to do pc-relative
15376    addressing across such boundaries.  */
15377
15378 static void
15379 aof_asm_init_sections (void)
15380 {
15381   text_section = get_unnamed_section (SECTION_CODE,
15382                                       aof_output_text_section_asm_op, NULL);
15383   data_section = get_unnamed_section (SECTION_WRITE,
15384                                       aof_output_data_section_asm_op, NULL);
15385   readonly_data_section = text_section;
15386 }
15387
15388 void
15389 zero_init_section (void)
15390 {
15391   static int zero_init_count = 1;
15392
15393   fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
15394   in_section = NULL;
15395 }
15396
15397 /* The AOF assembler is religiously strict about declarations of
15398    imported and exported symbols, so that it is impossible to declare
15399    a function as imported near the beginning of the file, and then to
15400    export it later on.  It is, however, possible to delay the decision
15401    until all the functions in the file have been compiled.  To get
15402    around this, we maintain a list of the imports and exports, and
15403    delete from it any that are subsequently defined.  At the end of
15404    compilation we spit the remainder of the list out before the END
15405    directive.  */
15406
15407 struct import
15408 {
15409   struct import * next;
15410   const char * name;
15411 };
15412
15413 static struct import * imports_list = NULL;
15414
15415 void
15416 aof_add_import (const char *name)
15417 {
15418   struct import * new;
15419
15420   for (new = imports_list; new; new = new->next)
15421     if (new->name == name)
15422       return;
15423
15424   new = (struct import *) xmalloc (sizeof (struct import));
15425   new->next = imports_list;
15426   imports_list = new;
15427   new->name = name;
15428 }
15429
15430 void
15431 aof_delete_import (const char *name)
15432 {
15433   struct import ** old;
15434
15435   for (old = &imports_list; *old; old = & (*old)->next)
15436     {
15437       if ((*old)->name == name)
15438         {
15439           *old = (*old)->next;
15440           return;
15441         }
15442     }
15443 }
15444
15445 int arm_main_function = 0;
15446
15447 static void
15448 aof_dump_imports (FILE *f)
15449 {
15450   /* The AOF assembler needs this to cause the startup code to be extracted
15451      from the library.  Brining in __main causes the whole thing to work
15452      automagically.  */
15453   if (arm_main_function)
15454     {
15455       switch_to_section (text_section);
15456       fputs ("\tIMPORT __main\n", f);
15457       fputs ("\tDCD __main\n", f);
15458     }
15459
15460   /* Now dump the remaining imports.  */
15461   while (imports_list)
15462     {
15463       fprintf (f, "\tIMPORT\t");
15464       assemble_name (f, imports_list->name);
15465       fputc ('\n', f);
15466       imports_list = imports_list->next;
15467     }
15468 }
15469
15470 static void
15471 aof_globalize_label (FILE *stream, const char *name)
15472 {
15473   default_globalize_label (stream, name);
15474   if (! strcmp (name, "main"))
15475     arm_main_function = 1;
15476 }
15477
15478 static void
15479 aof_file_start (void)
15480 {
15481   fputs ("__r0\tRN\t0\n", asm_out_file);
15482   fputs ("__a1\tRN\t0\n", asm_out_file);
15483   fputs ("__a2\tRN\t1\n", asm_out_file);
15484   fputs ("__a3\tRN\t2\n", asm_out_file);
15485   fputs ("__a4\tRN\t3\n", asm_out_file);
15486   fputs ("__v1\tRN\t4\n", asm_out_file);
15487   fputs ("__v2\tRN\t5\n", asm_out_file);
15488   fputs ("__v3\tRN\t6\n", asm_out_file);
15489   fputs ("__v4\tRN\t7\n", asm_out_file);
15490   fputs ("__v5\tRN\t8\n", asm_out_file);
15491   fputs ("__v6\tRN\t9\n", asm_out_file);
15492   fputs ("__sl\tRN\t10\n", asm_out_file);
15493   fputs ("__fp\tRN\t11\n", asm_out_file);
15494   fputs ("__ip\tRN\t12\n", asm_out_file);
15495   fputs ("__sp\tRN\t13\n", asm_out_file);
15496   fputs ("__lr\tRN\t14\n", asm_out_file);
15497   fputs ("__pc\tRN\t15\n", asm_out_file);
15498   fputs ("__f0\tFN\t0\n", asm_out_file);
15499   fputs ("__f1\tFN\t1\n", asm_out_file);
15500   fputs ("__f2\tFN\t2\n", asm_out_file);
15501   fputs ("__f3\tFN\t3\n", asm_out_file);
15502   fputs ("__f4\tFN\t4\n", asm_out_file);
15503   fputs ("__f5\tFN\t5\n", asm_out_file);
15504   fputs ("__f6\tFN\t6\n", asm_out_file);
15505   fputs ("__f7\tFN\t7\n", asm_out_file);
15506   switch_to_section (text_section);
15507 }
15508
15509 static void
15510 aof_file_end (void)
15511 {
15512   if (flag_pic)
15513     aof_dump_pic_table (asm_out_file);
15514   arm_file_end ();
15515   aof_dump_imports (asm_out_file);
15516   fputs ("\tEND\n", asm_out_file);
15517 }
15518 #endif /* AOF_ASSEMBLER */
15519
15520 #ifndef ARM_PE
15521 /* Symbols in the text segment can be accessed without indirecting via the
15522    constant pool; it may take an extra binary operation, but this is still
15523    faster than indirecting via memory.  Don't do this when not optimizing,
15524    since we won't be calculating al of the offsets necessary to do this
15525    simplification.  */
15526
15527 static void
15528 arm_encode_section_info (tree decl, rtx rtl, int first)
15529 {
15530   /* This doesn't work with AOF syntax, since the string table may be in
15531      a different AREA.  */
15532 #ifndef AOF_ASSEMBLER
15533   if (optimize > 0 && TREE_CONSTANT (decl))
15534     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
15535 #endif
15536
15537   /* If we are referencing a function that is weak then encode a long call
15538      flag in the function name, otherwise if the function is static or
15539      or known to be defined in this file then encode a short call flag.  */
15540   if (first && DECL_P (decl))
15541     {
15542       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
15543         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
15544       else if (! TREE_PUBLIC (decl))
15545         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
15546     }
15547
15548   default_encode_section_info (decl, rtl, first);
15549 }
15550 #endif /* !ARM_PE */
15551
15552 static void
15553 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
15554 {
15555   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
15556       && !strcmp (prefix, "L"))
15557     {
15558       arm_ccfsm_state = 0;
15559       arm_target_insn = NULL;
15560     }
15561   default_internal_label (stream, prefix, labelno);
15562 }
15563
15564 /* Output code to add DELTA to the first argument, and then jump
15565    to FUNCTION.  Used for C++ multiple inheritance.  */
15566 static void
15567 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
15568                      HOST_WIDE_INT delta,
15569                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
15570                      tree function)
15571 {
15572   static int thunk_label = 0;
15573   char label[256];
15574   char labelpc[256];
15575   int mi_delta = delta;
15576   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
15577   int shift = 0;
15578   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
15579                     ? 1 : 0);
15580   if (mi_delta < 0)
15581     mi_delta = - mi_delta;
15582   /* When generating 16-bit thumb code, thunks are entered in arm mode.  */
15583   if (TARGET_THUMB1)
15584     {
15585       int labelno = thunk_label++;
15586       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
15587       fputs ("\tldr\tr12, ", file);
15588       assemble_name (file, label);
15589       fputc ('\n', file);
15590       if (flag_pic)
15591         {
15592           /* If we are generating PIC, the ldr instruction below loads
15593              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
15594              the address of the add + 8, so we have:
15595
15596              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
15597                  = target + 1.
15598
15599              Note that we have "+ 1" because some versions of GNU ld
15600              don't set the low bit of the result for R_ARM_REL32
15601              relocations against thumb function symbols.  */
15602           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
15603           assemble_name (file, labelpc);
15604           fputs (":\n", file);
15605           fputs ("\tadd\tr12, pc, r12\n", file);
15606         }
15607     }
15608   /* TODO: Use movw/movt for large constants when available.  */
15609   while (mi_delta != 0)
15610     {
15611       if ((mi_delta & (3 << shift)) == 0)
15612         shift += 2;
15613       else
15614         {
15615           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
15616                        mi_op, this_regno, this_regno,
15617                        mi_delta & (0xff << shift));
15618           mi_delta &= ~(0xff << shift);
15619           shift += 8;
15620         }
15621     }
15622   if (TARGET_THUMB1)
15623     {
15624       fprintf (file, "\tbx\tr12\n");
15625       ASM_OUTPUT_ALIGN (file, 2);
15626       assemble_name (file, label);
15627       fputs (":\n", file);
15628       if (flag_pic)
15629         {
15630           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
15631           rtx tem = XEXP (DECL_RTL (function), 0);
15632           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
15633           tem = gen_rtx_MINUS (GET_MODE (tem),
15634                                tem,
15635                                gen_rtx_SYMBOL_REF (Pmode,
15636                                                    ggc_strdup (labelpc)));
15637           assemble_integer (tem, 4, BITS_PER_WORD, 1);
15638         }
15639       else
15640         /* Output ".word .LTHUNKn".  */
15641         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
15642     }
15643   else
15644     {
15645       fputs ("\tb\t", file);
15646       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
15647       if (NEED_PLT_RELOC)
15648         fputs ("(PLT)", file);
15649       fputc ('\n', file);
15650     }
15651 }
15652
15653 int
15654 arm_emit_vector_const (FILE *file, rtx x)
15655 {
15656   int i;
15657   const char * pattern;
15658
15659   gcc_assert (GET_CODE (x) == CONST_VECTOR);
15660
15661   switch (GET_MODE (x))
15662     {
15663     case V2SImode: pattern = "%08x"; break;
15664     case V4HImode: pattern = "%04x"; break;
15665     case V8QImode: pattern = "%02x"; break;
15666     default:       gcc_unreachable ();
15667     }
15668
15669   fprintf (file, "0x");
15670   for (i = CONST_VECTOR_NUNITS (x); i--;)
15671     {
15672       rtx element;
15673
15674       element = CONST_VECTOR_ELT (x, i);
15675       fprintf (file, pattern, INTVAL (element));
15676     }
15677
15678   return 1;
15679 }
15680
15681 const char *
15682 arm_output_load_gr (rtx *operands)
15683 {
15684   rtx reg;
15685   rtx offset;
15686   rtx wcgr;
15687   rtx sum;
15688
15689   if (GET_CODE (operands [1]) != MEM
15690       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
15691       || GET_CODE (reg = XEXP (sum, 0)) != REG
15692       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
15693       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
15694     return "wldrw%?\t%0, %1";
15695
15696   /* Fix up an out-of-range load of a GR register.  */
15697   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
15698   wcgr = operands[0];
15699   operands[0] = reg;
15700   output_asm_insn ("ldr%?\t%0, %1", operands);
15701
15702   operands[0] = wcgr;
15703   operands[1] = reg;
15704   output_asm_insn ("tmcr%?\t%0, %1", operands);
15705   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
15706
15707   return "";
15708 }
15709
15710 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
15711
15712    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
15713    named arg and all anonymous args onto the stack.
15714    XXX I know the prologue shouldn't be pushing registers, but it is faster
15715    that way.  */
15716
15717 static void
15718 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
15719                             enum machine_mode mode ATTRIBUTE_UNUSED,
15720                             tree type ATTRIBUTE_UNUSED,
15721                             int *pretend_size,
15722                             int second_time ATTRIBUTE_UNUSED)
15723 {
15724   cfun->machine->uses_anonymous_args = 1;
15725   if (cum->nregs < NUM_ARG_REGS)
15726     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
15727 }
15728
15729 /* Return nonzero if the CONSUMER instruction (a store) does not need
15730    PRODUCER's value to calculate the address.  */
15731
15732 int
15733 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
15734 {
15735   rtx value = PATTERN (producer);
15736   rtx addr = PATTERN (consumer);
15737
15738   if (GET_CODE (value) == COND_EXEC)
15739     value = COND_EXEC_CODE (value);
15740   if (GET_CODE (value) == PARALLEL)
15741     value = XVECEXP (value, 0, 0);
15742   value = XEXP (value, 0);
15743   if (GET_CODE (addr) == COND_EXEC)
15744     addr = COND_EXEC_CODE (addr);
15745   if (GET_CODE (addr) == PARALLEL)
15746     addr = XVECEXP (addr, 0, 0);
15747   addr = XEXP (addr, 0);
15748
15749   return !reg_overlap_mentioned_p (value, addr);
15750 }
15751
15752 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
15753    have an early register shift value or amount dependency on the
15754    result of PRODUCER.  */
15755
15756 int
15757 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
15758 {
15759   rtx value = PATTERN (producer);
15760   rtx op = PATTERN (consumer);
15761   rtx early_op;
15762
15763   if (GET_CODE (value) == COND_EXEC)
15764     value = COND_EXEC_CODE (value);
15765   if (GET_CODE (value) == PARALLEL)
15766     value = XVECEXP (value, 0, 0);
15767   value = XEXP (value, 0);
15768   if (GET_CODE (op) == COND_EXEC)
15769     op = COND_EXEC_CODE (op);
15770   if (GET_CODE (op) == PARALLEL)
15771     op = XVECEXP (op, 0, 0);
15772   op = XEXP (op, 1);
15773
15774   early_op = XEXP (op, 0);
15775   /* This is either an actual independent shift, or a shift applied to
15776      the first operand of another operation.  We want the whole shift
15777      operation.  */
15778   if (GET_CODE (early_op) == REG)
15779     early_op = op;
15780
15781   return !reg_overlap_mentioned_p (value, early_op);
15782 }
15783
15784 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
15785    have an early register shift value dependency on the result of
15786    PRODUCER.  */
15787
15788 int
15789 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
15790 {
15791   rtx value = PATTERN (producer);
15792   rtx op = PATTERN (consumer);
15793   rtx early_op;
15794
15795   if (GET_CODE (value) == COND_EXEC)
15796     value = COND_EXEC_CODE (value);
15797   if (GET_CODE (value) == PARALLEL)
15798     value = XVECEXP (value, 0, 0);
15799   value = XEXP (value, 0);
15800   if (GET_CODE (op) == COND_EXEC)
15801     op = COND_EXEC_CODE (op);
15802   if (GET_CODE (op) == PARALLEL)
15803     op = XVECEXP (op, 0, 0);
15804   op = XEXP (op, 1);
15805
15806   early_op = XEXP (op, 0);
15807
15808   /* This is either an actual independent shift, or a shift applied to
15809      the first operand of another operation.  We want the value being
15810      shifted, in either case.  */
15811   if (GET_CODE (early_op) != REG)
15812     early_op = XEXP (early_op, 0);
15813
15814   return !reg_overlap_mentioned_p (value, early_op);
15815 }
15816
15817 /* Return nonzero if the CONSUMER (a mul or mac op) does not
15818    have an early register mult dependency on the result of
15819    PRODUCER.  */
15820
15821 int
15822 arm_no_early_mul_dep (rtx producer, rtx consumer)
15823 {
15824   rtx value = PATTERN (producer);
15825   rtx op = PATTERN (consumer);
15826
15827   if (GET_CODE (value) == COND_EXEC)
15828     value = COND_EXEC_CODE (value);
15829   if (GET_CODE (value) == PARALLEL)
15830     value = XVECEXP (value, 0, 0);
15831   value = XEXP (value, 0);
15832   if (GET_CODE (op) == COND_EXEC)
15833     op = COND_EXEC_CODE (op);
15834   if (GET_CODE (op) == PARALLEL)
15835     op = XVECEXP (op, 0, 0);
15836   op = XEXP (op, 1);
15837
15838   return (GET_CODE (op) == PLUS
15839           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
15840 }
15841
15842
15843 /* We can't rely on the caller doing the proper promotion when
15844    using APCS or ATPCS.  */
15845
15846 static bool
15847 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
15848 {
15849     return !TARGET_AAPCS_BASED;
15850 }
15851
15852
15853 /* AAPCS based ABIs use short enums by default.  */
15854
15855 static bool
15856 arm_default_short_enums (void)
15857 {
15858   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
15859 }
15860
15861
15862 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
15863
15864 static bool
15865 arm_align_anon_bitfield (void)
15866 {
15867   return TARGET_AAPCS_BASED;
15868 }
15869
15870
15871 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
15872
15873 static tree
15874 arm_cxx_guard_type (void)
15875 {
15876   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
15877 }
15878
15879
15880 /* The EABI says test the least significant bit of a guard variable.  */
15881
15882 static bool
15883 arm_cxx_guard_mask_bit (void)
15884 {
15885   return TARGET_AAPCS_BASED;
15886 }
15887
15888
15889 /* The EABI specifies that all array cookies are 8 bytes long.  */
15890
15891 static tree
15892 arm_get_cookie_size (tree type)
15893 {
15894   tree size;
15895
15896   if (!TARGET_AAPCS_BASED)
15897     return default_cxx_get_cookie_size (type);
15898
15899   size = build_int_cst (sizetype, 8);
15900   return size;
15901 }
15902
15903
15904 /* The EABI says that array cookies should also contain the element size.  */
15905
15906 static bool
15907 arm_cookie_has_size (void)
15908 {
15909   return TARGET_AAPCS_BASED;
15910 }
15911
15912
15913 /* The EABI says constructors and destructors should return a pointer to
15914    the object constructed/destroyed.  */
15915
15916 static bool
15917 arm_cxx_cdtor_returns_this (void)
15918 {
15919   return TARGET_AAPCS_BASED;
15920 }
15921
15922 /* The EABI says that an inline function may never be the key
15923    method.  */
15924
15925 static bool
15926 arm_cxx_key_method_may_be_inline (void)
15927 {
15928   return !TARGET_AAPCS_BASED;
15929 }
15930
15931 static void
15932 arm_cxx_determine_class_data_visibility (tree decl)
15933 {
15934   if (!TARGET_AAPCS_BASED)
15935     return;
15936
15937   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15938      is exported.  However, on systems without dynamic vague linkage,
15939      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
15940   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15941     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15942   else
15943     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15944   DECL_VISIBILITY_SPECIFIED (decl) = 1;
15945 }
15946
15947 static bool
15948 arm_cxx_class_data_always_comdat (void)
15949 {
15950   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15951      vague linkage if the class has no key function.  */
15952   return !TARGET_AAPCS_BASED;
15953 }
15954
15955
15956 /* The EABI says __aeabi_atexit should be used to register static
15957    destructors.  */
15958
15959 static bool
15960 arm_cxx_use_aeabi_atexit (void)
15961 {
15962   return TARGET_AAPCS_BASED;
15963 }
15964
15965
15966 void
15967 arm_set_return_address (rtx source, rtx scratch)
15968 {
15969   arm_stack_offsets *offsets;
15970   HOST_WIDE_INT delta;
15971   rtx addr;
15972   unsigned long saved_regs;
15973
15974   saved_regs = arm_compute_save_reg_mask ();
15975
15976   if ((saved_regs & (1 << LR_REGNUM)) == 0)
15977     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15978   else
15979     {
15980       if (frame_pointer_needed)
15981         addr = plus_constant(hard_frame_pointer_rtx, -4);
15982       else
15983         {
15984           /* LR will be the first saved register.  */
15985           offsets = arm_get_frame_offsets ();
15986           delta = offsets->outgoing_args - (offsets->frame + 4);
15987
15988
15989           if (delta >= 4096)
15990             {
15991               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15992                                      GEN_INT (delta & ~4095)));
15993               addr = scratch;
15994               delta &= 4095;
15995             }
15996           else
15997             addr = stack_pointer_rtx;
15998
15999           addr = plus_constant (addr, delta);
16000         }
16001       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16002     }
16003 }
16004
16005
16006 void
16007 thumb_set_return_address (rtx source, rtx scratch)
16008 {
16009   arm_stack_offsets *offsets;
16010   HOST_WIDE_INT delta;
16011   HOST_WIDE_INT limit;
16012   int reg;
16013   rtx addr;
16014   unsigned long mask;
16015
16016   emit_insn (gen_rtx_USE (VOIDmode, source));
16017
16018   mask = thumb1_compute_save_reg_mask ();
16019   if (mask & (1 << LR_REGNUM))
16020     {
16021       offsets = arm_get_frame_offsets ();
16022
16023       limit = 1024;
16024       /* Find the saved regs.  */
16025       if (frame_pointer_needed)
16026         {
16027           delta = offsets->soft_frame - offsets->saved_args;
16028           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
16029           if (TARGET_THUMB1)
16030             limit = 128;
16031         }
16032       else
16033         {
16034           delta = offsets->outgoing_args - offsets->saved_args;
16035           reg = SP_REGNUM;
16036         }
16037       /* Allow for the stack frame.  */
16038       if (TARGET_THUMB1 && TARGET_BACKTRACE)
16039         delta -= 16;
16040       /* The link register is always the first saved register.  */
16041       delta -= 4;
16042
16043       /* Construct the address.  */
16044       addr = gen_rtx_REG (SImode, reg);
16045       if (delta > limit)
16046         {
16047           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
16048           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
16049           addr = scratch;
16050         }
16051       else
16052         addr = plus_constant (addr, delta);
16053
16054       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16055     }
16056   else
16057     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
16058 }
16059
16060 /* Implements target hook vector_mode_supported_p.  */
16061 bool
16062 arm_vector_mode_supported_p (enum machine_mode mode)
16063 {
16064   if ((mode == V2SImode)
16065       || (mode == V4HImode)
16066       || (mode == V8QImode))
16067     return true;
16068
16069   return false;
16070 }
16071
16072 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
16073    ARM insns and therefore guarantee that the shift count is modulo 256.
16074    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
16075    guarantee no particular behavior for out-of-range counts.  */
16076
16077 static unsigned HOST_WIDE_INT
16078 arm_shift_truncation_mask (enum machine_mode mode)
16079 {
16080   return mode == SImode ? 255 : 0;
16081 }
16082
16083
16084 /* Map internal gcc register numbers to DWARF2 register numbers.  */
16085
16086 unsigned int
16087 arm_dbx_register_number (unsigned int regno)
16088 {
16089   if (regno < 16)
16090     return regno;
16091
16092   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
16093      compatibility.  The EABI defines them as registers 96-103.  */
16094   if (IS_FPA_REGNUM (regno))
16095     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
16096
16097   if (IS_VFP_REGNUM (regno))
16098     return 64 + regno - FIRST_VFP_REGNUM;
16099
16100   if (IS_IWMMXT_GR_REGNUM (regno))
16101     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
16102
16103   if (IS_IWMMXT_REGNUM (regno))
16104     return 112 + regno - FIRST_IWMMXT_REGNUM;
16105
16106   gcc_unreachable ();
16107 }
16108
16109
16110 #ifdef TARGET_UNWIND_INFO
16111 /* Emit unwind directives for a store-multiple instruction or stack pointer
16112    push during alignment.
16113    These should only ever be generated by the function prologue code, so
16114    expect them to have a particular form.  */
16115
16116 static void
16117 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
16118 {
16119   int i;
16120   HOST_WIDE_INT offset;
16121   HOST_WIDE_INT nregs;
16122   int reg_size;
16123   unsigned reg;
16124   unsigned lastreg;
16125   rtx e;
16126
16127   e = XVECEXP (p, 0, 0);
16128   if (GET_CODE (e) != SET)
16129     abort ();
16130
16131   /* First insn will adjust the stack pointer.  */
16132   if (GET_CODE (e) != SET
16133       || GET_CODE (XEXP (e, 0)) != REG
16134       || REGNO (XEXP (e, 0)) != SP_REGNUM
16135       || GET_CODE (XEXP (e, 1)) != PLUS)
16136     abort ();
16137
16138   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
16139   nregs = XVECLEN (p, 0) - 1;
16140
16141   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
16142   if (reg < 16)
16143     {
16144       /* The function prologue may also push pc, but not annotate it as it is
16145          never restored.  We turn this into a stack pointer adjustment.  */
16146       if (nregs * 4 == offset - 4)
16147         {
16148           fprintf (asm_out_file, "\t.pad #4\n");
16149           offset -= 4;
16150         }
16151       reg_size = 4;
16152       fprintf (asm_out_file, "\t.save {");
16153     }
16154   else if (IS_VFP_REGNUM (reg))
16155     {
16156       reg_size = 8;
16157       fprintf (asm_out_file, "\t.vsave {");
16158     }
16159   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
16160     {
16161       /* FPA registers are done differently.  */
16162       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
16163       return;
16164     }
16165   else
16166     /* Unknown register type.  */
16167     abort ();
16168
16169   /* If the stack increment doesn't match the size of the saved registers,
16170      something has gone horribly wrong.  */
16171   if (offset != nregs * reg_size)
16172     abort ();
16173
16174   offset = 0;
16175   lastreg = 0;
16176   /* The remaining insns will describe the stores.  */
16177   for (i = 1; i <= nregs; i++)
16178     {
16179       /* Expect (set (mem <addr>) (reg)).
16180          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
16181       e = XVECEXP (p, 0, i);
16182       if (GET_CODE (e) != SET
16183           || GET_CODE (XEXP (e, 0)) != MEM
16184           || GET_CODE (XEXP (e, 1)) != REG)
16185         abort ();
16186
16187       reg = REGNO (XEXP (e, 1));
16188       if (reg < lastreg)
16189         abort ();
16190
16191       if (i != 1)
16192         fprintf (asm_out_file, ", ");
16193       /* We can't use %r for vfp because we need to use the
16194          double precision register names.  */
16195       if (IS_VFP_REGNUM (reg))
16196         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
16197       else
16198         asm_fprintf (asm_out_file, "%r", reg);
16199
16200 #ifdef ENABLE_CHECKING
16201       /* Check that the addresses are consecutive.  */
16202       e = XEXP (XEXP (e, 0), 0);
16203       if (GET_CODE (e) == PLUS)
16204         {
16205           offset += reg_size;
16206           if (GET_CODE (XEXP (e, 0)) != REG
16207               || REGNO (XEXP (e, 0)) != SP_REGNUM
16208               || GET_CODE (XEXP (e, 1)) != CONST_INT
16209               || offset != INTVAL (XEXP (e, 1)))
16210             abort ();
16211         }
16212       else if (i != 1
16213                || GET_CODE (e) != REG
16214                || REGNO (e) != SP_REGNUM)
16215         abort ();
16216 #endif
16217     }
16218   fprintf (asm_out_file, "}\n");
16219 }
16220
16221 /*  Emit unwind directives for a SET.  */
16222
16223 static void
16224 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
16225 {
16226   rtx e0;
16227   rtx e1;
16228   unsigned reg;
16229
16230   e0 = XEXP (p, 0);
16231   e1 = XEXP (p, 1);
16232   switch (GET_CODE (e0))
16233     {
16234     case MEM:
16235       /* Pushing a single register.  */
16236       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
16237           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
16238           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
16239         abort ();
16240
16241       asm_fprintf (asm_out_file, "\t.save ");
16242       if (IS_VFP_REGNUM (REGNO (e1)))
16243         asm_fprintf(asm_out_file, "{d%d}\n",
16244                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
16245       else
16246         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
16247       break;
16248
16249     case REG:
16250       if (REGNO (e0) == SP_REGNUM)
16251         {
16252           /* A stack increment.  */
16253           if (GET_CODE (e1) != PLUS
16254               || GET_CODE (XEXP (e1, 0)) != REG
16255               || REGNO (XEXP (e1, 0)) != SP_REGNUM
16256               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16257             abort ();
16258
16259           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
16260                        -INTVAL (XEXP (e1, 1)));
16261         }
16262       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
16263         {
16264           HOST_WIDE_INT offset;
16265
16266           if (GET_CODE (e1) == PLUS)
16267             {
16268               if (GET_CODE (XEXP (e1, 0)) != REG
16269                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16270                 abort ();
16271               reg = REGNO (XEXP (e1, 0));
16272               offset = INTVAL (XEXP (e1, 1));
16273               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
16274                            HARD_FRAME_POINTER_REGNUM, reg,
16275                            INTVAL (XEXP (e1, 1)));
16276             }
16277           else if (GET_CODE (e1) == REG)
16278             {
16279               reg = REGNO (e1);
16280               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
16281                            HARD_FRAME_POINTER_REGNUM, reg);
16282             }
16283           else
16284             abort ();
16285         }
16286       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
16287         {
16288           /* Move from sp to reg.  */
16289           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
16290         }
16291      else if (GET_CODE (e1) == PLUS
16292               && GET_CODE (XEXP (e1, 0)) == REG
16293               && REGNO (XEXP (e1, 0)) == SP_REGNUM
16294               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
16295         {
16296           /* Set reg to offset from sp.  */
16297           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
16298                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
16299         }
16300       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
16301         {
16302           /* Stack pointer save before alignment.  */
16303           reg = REGNO (e0);
16304           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
16305                        reg + 0x90, reg);
16306         }
16307       else
16308         abort ();
16309       break;
16310
16311     default:
16312       abort ();
16313     }
16314 }
16315
16316
16317 /* Emit unwind directives for the given insn.  */
16318
16319 static void
16320 arm_unwind_emit (FILE * asm_out_file, rtx insn)
16321 {
16322   rtx pat;
16323
16324   if (!ARM_EABI_UNWIND_TABLES)
16325     return;
16326
16327   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
16328     return;
16329
16330   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
16331   if (pat)
16332     pat = XEXP (pat, 0);
16333   else
16334     pat = PATTERN (insn);
16335
16336   switch (GET_CODE (pat))
16337     {
16338     case SET:
16339       arm_unwind_emit_set (asm_out_file, pat);
16340       break;
16341
16342     case SEQUENCE:
16343       /* Store multiple.  */
16344       arm_unwind_emit_sequence (asm_out_file, pat);
16345       break;
16346
16347     default:
16348       abort();
16349     }
16350 }
16351
16352
16353 /* Output a reference from a function exception table to the type_info
16354    object X.  The EABI specifies that the symbol should be relocated by
16355    an R_ARM_TARGET2 relocation.  */
16356
16357 static bool
16358 arm_output_ttype (rtx x)
16359 {
16360   fputs ("\t.word\t", asm_out_file);
16361   output_addr_const (asm_out_file, x);
16362   /* Use special relocations for symbol references.  */
16363   if (GET_CODE (x) != CONST_INT)
16364     fputs ("(TARGET2)", asm_out_file);
16365   fputc ('\n', asm_out_file);
16366
16367   return TRUE;
16368 }
16369 #endif /* TARGET_UNWIND_INFO */
16370
16371
16372 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
16373    stack alignment.  */
16374
16375 static void
16376 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
16377 {
16378   rtx unspec = SET_SRC (pattern);
16379   gcc_assert (GET_CODE (unspec) == UNSPEC);
16380
16381   switch (index)
16382     {
16383     case UNSPEC_STACK_ALIGN:
16384       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
16385          put anything on the stack, so hopefully it won't matter.
16386          CFA = SP will be correct after alignment.  */
16387       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
16388                               SET_DEST (pattern));
16389       break;
16390     default:
16391       gcc_unreachable ();
16392     }
16393 }
16394
16395
16396 /* Output unwind directives for the start/end of a function.  */
16397
16398 void
16399 arm_output_fn_unwind (FILE * f, bool prologue)
16400 {
16401   if (!ARM_EABI_UNWIND_TABLES)
16402     return;
16403
16404   if (prologue)
16405     fputs ("\t.fnstart\n", f);
16406   else
16407     fputs ("\t.fnend\n", f);
16408 }
16409
16410 static bool
16411 arm_emit_tls_decoration (FILE *fp, rtx x)
16412 {
16413   enum tls_reloc reloc;
16414   rtx val;
16415
16416   val = XVECEXP (x, 0, 0);
16417   reloc = INTVAL (XVECEXP (x, 0, 1));
16418
16419   output_addr_const (fp, val);
16420
16421   switch (reloc)
16422     {
16423     case TLS_GD32:
16424       fputs ("(tlsgd)", fp);
16425       break;
16426     case TLS_LDM32:
16427       fputs ("(tlsldm)", fp);
16428       break;
16429     case TLS_LDO32:
16430       fputs ("(tlsldo)", fp);
16431       break;
16432     case TLS_IE32:
16433       fputs ("(gottpoff)", fp);
16434       break;
16435     case TLS_LE32:
16436       fputs ("(tpoff)", fp);
16437       break;
16438     default:
16439       gcc_unreachable ();
16440     }
16441
16442   switch (reloc)
16443     {
16444     case TLS_GD32:
16445     case TLS_LDM32:
16446     case TLS_IE32:
16447       fputs (" + (. - ", fp);
16448       output_addr_const (fp, XVECEXP (x, 0, 2));
16449       fputs (" - ", fp);
16450       output_addr_const (fp, XVECEXP (x, 0, 3));
16451       fputc (')', fp);
16452       break;
16453     default:
16454       break;
16455     }
16456
16457   return TRUE;
16458 }
16459
16460 bool
16461 arm_output_addr_const_extra (FILE *fp, rtx x)
16462 {
16463   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
16464     return arm_emit_tls_decoration (fp, x);
16465   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
16466     {
16467       char label[256];
16468       int labelno = INTVAL (XVECEXP (x, 0, 0));
16469
16470       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
16471       assemble_name_raw (fp, label);
16472
16473       return TRUE;
16474     }
16475   else if (GET_CODE (x) == CONST_VECTOR)
16476     return arm_emit_vector_const (fp, x);
16477
16478   return FALSE;
16479 }
16480
16481 /* Output assembly for a shift instruction.
16482    SET_FLAGS determines how the instruction modifies the condition codes.
16483    0 - Do not set condition codes.
16484    1 - Set condition codes.
16485    2 - Use smallest instruction.  */
16486 const char *
16487 arm_output_shift(rtx * operands, int set_flags)
16488 {
16489   char pattern[100];
16490   static const char flag_chars[3] = {'?', '.', '!'};
16491   const char *shift;
16492   HOST_WIDE_INT val;
16493   char c;
16494   
16495   c = flag_chars[set_flags];
16496   if (TARGET_UNIFIED_ASM)
16497     {
16498       shift = shift_op(operands[3], &val);
16499       if (shift)
16500         {
16501           if (val != -1)
16502             operands[2] = GEN_INT(val);
16503           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
16504         }
16505       else
16506         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
16507     }
16508   else
16509     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
16510   output_asm_insn (pattern, operands);
16511   return "";
16512 }
16513
16514 /* Output a Thumb-2 casesi instruction.  */
16515 const char *
16516 thumb2_output_casesi (rtx *operands)
16517 {
16518   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
16519
16520   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
16521
16522   output_asm_insn ("cmp\t%0, %1", operands);
16523   output_asm_insn ("bhi\t%l3", operands);
16524   switch (GET_MODE(diff_vec))
16525     {
16526     case QImode:
16527       return "tbb\t[%|pc, %0]";
16528     case HImode:
16529       return "tbh\t[%|pc, %0, lsl #1]";
16530     case SImode:
16531       if (flag_pic)
16532         {
16533           output_asm_insn ("adr\t%4, %l2", operands);
16534           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
16535           output_asm_insn ("add\t%4, %4, %5", operands);
16536           return "bx\t%4";
16537         }
16538       else
16539         {
16540           output_asm_insn ("adr\t%4, %l2", operands);
16541           return "ldr\t%|pc, [%4, %0, lsl #2]";
16542         }
16543     default:
16544       gcc_unreachable ();
16545     }
16546 }
16547
16548 #include "gt-arm.h"