OSDN Git Service

1de284e1d621c598d91f7da9ba866a826836da62
[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  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24     
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54
55 /* Forward definitions of types.  */
56 typedef struct minipool_node    Mnode;
57 typedef struct minipool_fixup   Mfix;
58
59 const struct attribute_spec arm_attribute_table[];
60
61 /* Forward function declarations.  */
62 static arm_stack_offsets *arm_get_frame_offsets (void);
63 static void arm_add_gc_roots (void);
64 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
65                              HOST_WIDE_INT, rtx, rtx, int, int);
66 static unsigned bit_count (unsigned long);
67 static int arm_address_register_rtx_p (rtx, int);
68 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
69 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
70 inline static int thumb_index_register_rtx_p (rtx, int);
71 static int thumb_far_jump_used_p (void);
72 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
73 static rtx emit_multi_reg_push (int);
74 static rtx emit_sfm (int, int);
75 #ifndef AOF_ASSEMBLER
76 static bool arm_assemble_integer (rtx, unsigned int, int);
77 #endif
78 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
79 static arm_cc get_arm_condition_code (rtx);
80 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
81 static rtx is_jump_table (rtx);
82 static const char *output_multi_immediate (rtx *, const char *, const char *,
83                                            int, HOST_WIDE_INT);
84 static void print_multi_reg (FILE *, const char *, int, int);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static int number_of_first_bit_set (int);
88 static void replace_symbols_in_block (tree, rtx, rtx);
89 static void thumb_exit (FILE *, int, rtx);
90 static void thumb_pushpop (FILE *, int, int, int *, 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 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125                                  tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
128 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
130 static bool arm_9e_rtx_costs (rtx, int, int, int *);
131 static int arm_address_cost (rtx);
132 static bool arm_memory_load_p (rtx);
133 static bool arm_cirrus_insn_p (rtx);
134 static void cirrus_reorg (rtx);
135 static void arm_init_builtins (void);
136 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
137 static void arm_init_iwmmxt_builtins (void);
138 static rtx safe_vector_operand (rtx, enum machine_mode);
139 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
140 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
141 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
142 static void emit_constant_insn (rtx cond, rtx pattern);
143
144 #ifdef OBJECT_FORMAT_ELF
145 static void arm_elf_asm_named_section (const char *, unsigned int);
146 #endif
147 #ifndef ARM_PE
148 static void arm_encode_section_info (tree, rtx, int);
149 #endif
150 #ifdef AOF_ASSEMBLER
151 static void aof_globalize_label (FILE *, const char *);
152 static void aof_dump_imports (FILE *);
153 static void aof_dump_pic_table (FILE *);
154 static void aof_file_start (void);
155 static void aof_file_end (void);
156 #endif
157 static rtx arm_struct_value_rtx (tree, int);
158 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
159                                         tree, int *, int);
160 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
161                                    enum machine_mode, tree, bool);
162 static bool arm_promote_prototypes (tree);
163 static bool arm_default_short_enums (void);
164 static bool arm_align_anon_bitfield (void);
165
166 static tree arm_cxx_guard_type (void);
167 static bool arm_cxx_guard_mask_bit (void);
168 static tree arm_get_cookie_size (tree);
169 static bool arm_cookie_has_size (void);
170
171 \f
172 /* Initialize the GCC target structure.  */
173 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
174 #undef  TARGET_MERGE_DECL_ATTRIBUTES
175 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
176 #endif
177
178 #undef  TARGET_ATTRIBUTE_TABLE
179 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
180
181 #ifdef AOF_ASSEMBLER
182 #undef  TARGET_ASM_BYTE_OP
183 #define TARGET_ASM_BYTE_OP "\tDCB\t"
184 #undef  TARGET_ASM_ALIGNED_HI_OP
185 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
186 #undef  TARGET_ASM_ALIGNED_SI_OP
187 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
188 #undef TARGET_ASM_GLOBALIZE_LABEL
189 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
190 #undef TARGET_ASM_FILE_START
191 #define TARGET_ASM_FILE_START aof_file_start
192 #undef TARGET_ASM_FILE_END
193 #define TARGET_ASM_FILE_END aof_file_end
194 #else
195 #undef  TARGET_ASM_ALIGNED_SI_OP
196 #define TARGET_ASM_ALIGNED_SI_OP NULL
197 #undef  TARGET_ASM_INTEGER
198 #define TARGET_ASM_INTEGER arm_assemble_integer
199 #endif
200
201 #undef  TARGET_ASM_FUNCTION_PROLOGUE
202 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
203
204 #undef  TARGET_ASM_FUNCTION_EPILOGUE
205 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
206
207 #undef  TARGET_COMP_TYPE_ATTRIBUTES
208 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
209
210 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
211 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
212
213 #undef  TARGET_SCHED_ADJUST_COST
214 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
215
216 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
217 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE hook_int_void_1
218
219 #undef TARGET_ENCODE_SECTION_INFO
220 #ifdef ARM_PE
221 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
222 #else
223 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
224 #endif
225
226 #undef  TARGET_STRIP_NAME_ENCODING
227 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
228
229 #undef  TARGET_ASM_INTERNAL_LABEL
230 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
231
232 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
233 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
234
235 #undef  TARGET_ASM_OUTPUT_MI_THUNK
236 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
237 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
238 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
239
240 /* This will be overridden in arm_override_options.  */
241 #undef  TARGET_RTX_COSTS
242 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
243 #undef  TARGET_ADDRESS_COST
244 #define TARGET_ADDRESS_COST arm_address_cost
245
246 #undef  TARGET_MACHINE_DEPENDENT_REORG
247 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
248
249 #undef  TARGET_INIT_BUILTINS
250 #define TARGET_INIT_BUILTINS  arm_init_builtins
251 #undef  TARGET_EXPAND_BUILTIN
252 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
253
254 #undef TARGET_PROMOTE_FUNCTION_ARGS
255 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
256 #undef TARGET_PROMOTE_FUNCTION_RETURN
257 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
258 #undef TARGET_PROMOTE_PROTOTYPES
259 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
260 #undef TARGET_PASS_BY_REFERENCE
261 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
262
263 #undef TARGET_STRUCT_VALUE_RTX
264 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
265
266 #undef  TARGET_SETUP_INCOMING_VARARGS
267 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
268
269 #undef TARGET_DEFAULT_SHORT_ENUMS
270 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
271
272 #undef TARGET_ALIGN_ANON_BITFIELD
273 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
274
275 #undef TARGET_CXX_GUARD_TYPE
276 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
277
278 #undef TARGET_CXX_GUARD_MASK_BIT
279 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
280
281 #undef TARGET_CXX_GET_COOKIE_SIZE
282 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
283
284 #undef TARGET_CXX_COOKIE_HAS_SIZE
285 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
286
287 struct gcc_target targetm = TARGET_INITIALIZER;
288 \f
289 /* Obstack for minipool constant handling.  */
290 static struct obstack minipool_obstack;
291 static char *         minipool_startobj;
292
293 /* The maximum number of insns skipped which
294    will be conditionalised if possible.  */
295 static int max_insns_skipped = 5;
296
297 extern FILE * asm_out_file;
298
299 /* True if we are currently building a constant table.  */
300 int making_const_table;
301
302 /* Define the information needed to generate branch insns.  This is
303    stored from the compare operation.  */
304 rtx arm_compare_op0, arm_compare_op1;
305
306 /* The processor for which instructions should be scheduled.  */
307 enum processor_type arm_tune = arm_none;
308
309 /* Which floating point model to use.  */
310 enum arm_fp_model arm_fp_model;
311
312 /* Which floating point hardware is available.  */
313 enum fputype arm_fpu_arch;
314
315 /* Which floating point hardware to schedule for.  */
316 enum fputype arm_fpu_tune;
317
318 /* Whether to use floating point hardware.  */
319 enum float_abi_type arm_float_abi;
320
321 /* Which ABI to use.  */
322 enum arm_abi_type arm_abi;
323
324 /* Set by the -mfpu=... option.  */
325 const char * target_fpu_name = NULL;
326
327 /* Set by the -mfpe=... option.  */
328 const char * target_fpe_name = NULL;
329
330 /* Set by the -mfloat-abi=... option.  */
331 const char * target_float_abi_name = NULL;
332
333 /* Set by the -mabi=... option.  */
334 const char * target_abi_name = NULL;
335
336 /* Used to parse -mstructure_size_boundary command line option.  */
337 const char * structure_size_string = NULL;
338 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
339
340 /* Bit values used to identify processor capabilities.  */
341 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
342 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
343 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
344 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
345 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
346 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
347 #define FL_THUMB      (1 << 6)        /* Thumb aware */
348 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
349 #define FL_STRONG     (1 << 8)        /* StrongARM */
350 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
351 #define FL_XSCALE     (1 << 10)       /* XScale */
352 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
353 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
354                                          media instructions.  */
355 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
356
357 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
358
359 #define FL_FOR_ARCH2    0
360 #define FL_FOR_ARCH3    FL_MODE32
361 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
362 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
363 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
364 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
365 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
366 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
367 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
368 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
369 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
370 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
371
372 /* The bits in this mask specify which
373    instructions we are allowed to generate.  */
374 static unsigned long insn_flags = 0;
375
376 /* The bits in this mask specify which instruction scheduling options should
377    be used.  */
378 static unsigned long tune_flags = 0;
379
380 /* The following are used in the arm.md file as equivalents to bits
381    in the above two flag variables.  */
382
383 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
384 int arm_arch3m = 0;
385
386 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
387 int arm_arch4 = 0;
388
389 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
390 int arm_arch4t = 0;
391
392 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
393 int arm_arch5 = 0;
394
395 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
396 int arm_arch5e = 0;
397
398 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
399 int arm_arch6 = 0;
400
401 /* Nonzero if this chip can benefit from load scheduling.  */
402 int arm_ld_sched = 0;
403
404 /* Nonzero if this chip is a StrongARM.  */
405 int arm_is_strong = 0;
406
407 /* Nonzero if this chip is a Cirrus variant.  */
408 int arm_arch_cirrus = 0;
409
410 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
411 int arm_arch_iwmmxt = 0;
412
413 /* Nonzero if this chip is an XScale.  */
414 int arm_arch_xscale = 0;
415
416 /* Nonzero if tuning for XScale  */
417 int arm_tune_xscale = 0;
418
419 /* Nonzero if this chip is an ARM6 or an ARM7.  */
420 int arm_is_6_or_7 = 0;
421
422 /* Nonzero if generating Thumb instructions.  */
423 int thumb_code = 0;
424
425 /* Nonzero if we should define __THUMB_INTERWORK__ in the
426    preprocessor.  
427    XXX This is a bit of a hack, it's intended to help work around
428    problems in GLD which doesn't understand that armv5t code is
429    interworking clean.  */
430 int arm_cpp_interwork = 0;
431
432 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
433    must report the mode of the memory reference from PRINT_OPERAND to
434    PRINT_OPERAND_ADDRESS.  */
435 enum machine_mode output_memory_reference_mode;
436
437 /* The register number to be used for the PIC offset register.  */
438 const char * arm_pic_register_string = NULL;
439 int arm_pic_register = INVALID_REGNUM;
440
441 /* Set to 1 when a return insn is output, this means that the epilogue
442    is not needed.  */
443 int return_used_this_function;
444
445 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
446    the next function.  */
447 static int after_arm_reorg = 0;
448
449 /* The maximum number of insns to be used when loading a constant.  */
450 static int arm_constant_limit = 3;
451
452 /* For an explanation of these variables, see final_prescan_insn below.  */
453 int arm_ccfsm_state;
454 enum arm_cond_code arm_current_cc;
455 rtx arm_target_insn;
456 int arm_target_label;
457
458 /* The condition codes of the ARM, and the inverse function.  */
459 static const char * const arm_condition_codes[] =
460 {
461   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
462   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
463 };
464
465 #define streq(string1, string2) (strcmp (string1, string2) == 0)
466 \f
467 /* Initialization code.  */
468
469 struct processors
470 {
471   const char *const name;
472   enum processor_type core;
473   const char *arch;
474   const unsigned long flags;
475   bool (* rtx_costs) (rtx, int, int, int *);
476 };
477
478 /* Not all of these give usefully different compilation alternatives,
479    but there is no simple way of generalizing them.  */
480 static const struct processors all_cores[] =
481 {
482   /* ARM Cores */
483 #define ARM_CORE(NAME, ARCH, FLAGS, COSTS) \
484   {#NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
485 #include "arm-cores.def"
486 #undef ARM_CORE
487   {NULL, arm_none, NULL, 0, NULL}
488 };
489
490 static const struct processors all_architectures[] =
491 {
492   /* ARM Architectures */
493   /* We don't specify rtx_costs here as it will be figured out
494      from the core.  */
495   
496   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
497   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
498   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
499   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
500   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
501   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
502      implementations that support it, so we will leave it out for now.  */
503   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
504   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
505   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
506   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
507   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
508   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
509   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
510   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
511   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
512   {NULL, arm_none, NULL, 0 , NULL}
513 };
514
515 /* This is a magic structure.  The 'string' field is magically filled in
516    with a pointer to the value specified by the user on the command line
517    assuming that the user has specified such a value.  */
518
519 struct arm_cpu_select arm_select[] =
520 {
521   /* string       name            processors  */        
522   { NULL,       "-mcpu=",       all_cores  },
523   { NULL,       "-march=",      all_architectures },
524   { NULL,       "-mtune=",      all_cores }
525 };
526
527
528 /* The name of the proprocessor macro to define for this architecture.  */
529
530 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
531
532 struct fpu_desc
533 {
534   const char * name;
535   enum fputype fpu;
536 };
537
538
539 /* Available values for for -mfpu=.  */
540
541 static const struct fpu_desc all_fpus[] =
542 {
543   {"fpa",       FPUTYPE_FPA},
544   {"fpe2",      FPUTYPE_FPA_EMU2},
545   {"fpe3",      FPUTYPE_FPA_EMU2},
546   {"maverick",  FPUTYPE_MAVERICK},
547   {"vfp",       FPUTYPE_VFP}
548 };
549
550
551 /* Floating point models used by the different hardware.
552    See fputype in arm.h.  */
553
554 static const enum fputype fp_model_for_fpu[] =
555 {
556   /* No FP hardware.  */
557   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
558   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
559   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
560   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
561   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
562   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
563 };
564
565
566 struct float_abi
567 {
568   const char * name;
569   enum float_abi_type abi_type;
570 };
571
572
573 /* Available values for -mfloat-abi=.  */
574
575 static const struct float_abi all_float_abis[] =
576 {
577   {"soft",      ARM_FLOAT_ABI_SOFT},
578   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
579   {"hard",      ARM_FLOAT_ABI_HARD}
580 };
581
582
583 struct abi_name
584 {
585   const char *name;
586   enum arm_abi_type abi_type;
587 };
588
589
590 /* Available values for -mabi=.  */
591
592 static const struct abi_name arm_all_abis[] =
593 {
594   {"apcs-gnu",    ARM_ABI_APCS},
595   {"atpcs",   ARM_ABI_ATPCS},
596   {"aapcs",   ARM_ABI_AAPCS},
597   {"iwmmxt",  ARM_ABI_IWMMXT}
598 };
599
600 /* Return the number of bits set in VALUE.  */
601 static unsigned
602 bit_count (unsigned long value)
603 {
604   unsigned long count = 0;
605   
606   while (value)
607     {
608       count++;
609       value &= value - 1;  /* Clear the least-significant set bit.  */
610     }
611
612   return count;
613 }
614
615 /* Fix up any incompatible options that the user has specified.
616    This has now turned into a maze.  */
617 void
618 arm_override_options (void)
619 {
620   unsigned i;
621
622   /* Set up the flags based on the cpu/architecture selected by the user.  */
623   for (i = ARRAY_SIZE (arm_select); i--;)
624     {
625       struct arm_cpu_select * ptr = arm_select + i;
626       
627       if (ptr->string != NULL && ptr->string[0] != '\0')
628         {
629           const struct processors * sel;
630
631           for (sel = ptr->processors; sel->name != NULL; sel++)
632             if (streq (ptr->string, sel->name))
633               {
634                 /* Set the architecture define.  */
635                 if (i != 2)
636                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
637
638                 /* Determine the processor core for which we should
639                    tune code-generation.  */
640                 if (/* -mcpu= is a sensible default.  */
641                     i == 0
642                     /* If -march= is used, and -mcpu= has not been used,
643                        assume that we should tune for a representative
644                        CPU from that architecture.  */
645                     || i == 1
646                     /* -mtune= overrides -mcpu= and -march=.  */
647                     || i == 2)
648                   arm_tune = (enum processor_type) (sel - ptr->processors);
649
650                 if (i != 2)
651                   {
652                     /* If we have been given an architecture and a processor
653                        make sure that they are compatible.  We only generate
654                        a warning though, and we prefer the CPU over the
655                        architecture.  */
656                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
657                       warning ("switch -mcpu=%s conflicts with -march= switch",
658                                ptr->string);
659                     
660                     insn_flags = sel->flags;
661                   }
662                 
663                 break;
664               }
665
666           if (sel->name == NULL)
667             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
668         }
669     }
670   
671   /* If the user did not specify a processor, choose one for them.  */
672   if (insn_flags == 0)
673     {
674       const struct processors * sel;
675       unsigned int        sought;
676       enum processor_type cpu;
677
678       cpu = TARGET_CPU_DEFAULT;
679       if (cpu == arm_none)
680         {
681 #ifdef SUBTARGET_CPU_DEFAULT
682           /* Use the subtarget default CPU if none was specified by
683              configure.  */
684           cpu = SUBTARGET_CPU_DEFAULT;
685 #endif
686           /* Default to ARM6.  */
687           if (cpu == arm_none)
688             cpu = arm6;
689         }
690       sel = &all_cores[cpu];
691
692       insn_flags = sel->flags;
693
694       /* Now check to see if the user has specified some command line
695          switch that require certain abilities from the cpu.  */
696       sought = 0;
697       
698       if (TARGET_INTERWORK || TARGET_THUMB)
699         {
700           sought |= (FL_THUMB | FL_MODE32);
701           
702           /* There are no ARM processors that support both APCS-26 and
703              interworking.  Therefore we force FL_MODE26 to be removed
704              from insn_flags here (if it was set), so that the search
705              below will always be able to find a compatible processor.  */
706           insn_flags &= ~FL_MODE26;
707         }
708       
709       if (sought != 0 && ((sought & insn_flags) != sought))
710         {
711           /* Try to locate a CPU type that supports all of the abilities
712              of the default CPU, plus the extra abilities requested by
713              the user.  */
714           for (sel = all_cores; sel->name != NULL; sel++)
715             if ((sel->flags & sought) == (sought | insn_flags))
716               break;
717
718           if (sel->name == NULL)
719             {
720               unsigned current_bit_count = 0;
721               const struct processors * best_fit = NULL;
722               
723               /* Ideally we would like to issue an error message here
724                  saying that it was not possible to find a CPU compatible
725                  with the default CPU, but which also supports the command
726                  line options specified by the programmer, and so they
727                  ought to use the -mcpu=<name> command line option to
728                  override the default CPU type.
729
730                  If we cannot find a cpu that has both the
731                  characteristics of the default cpu and the given
732                  command line options we scan the array again looking
733                  for a best match.  */
734               for (sel = all_cores; sel->name != NULL; sel++)
735                 if ((sel->flags & sought) == sought)
736                   {
737                     unsigned count;
738
739                     count = bit_count (sel->flags & insn_flags);
740
741                     if (count >= current_bit_count)
742                       {
743                         best_fit = sel;
744                         current_bit_count = count;
745                       }
746                   }
747
748               if (best_fit == NULL)
749                 abort ();
750               else
751                 sel = best_fit;
752             }
753
754           insn_flags = sel->flags;
755         }
756       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
757       if (arm_tune == arm_none)
758         arm_tune = (enum processor_type) (sel - all_cores);
759     }
760   
761   /* The processor for which we should tune should now have been
762      chosen.  */
763   if (arm_tune == arm_none)
764     abort ();
765   
766   tune_flags = all_cores[(int)arm_tune].flags;
767   targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
768
769   /* Make sure that the processor choice does not conflict with any of the
770      other command line choices.  */
771   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
772     {
773       warning ("target CPU does not support interworking" );
774       target_flags &= ~ARM_FLAG_INTERWORK;
775     }
776   
777   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
778     {
779       warning ("target CPU does not support THUMB instructions");
780       target_flags &= ~ARM_FLAG_THUMB;
781     }
782
783   if (TARGET_APCS_FRAME && TARGET_THUMB)
784     {
785       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
786       target_flags &= ~ARM_FLAG_APCS_FRAME;
787     }
788
789   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
790      from here where no function is being compiled currently.  */
791   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
792       && TARGET_ARM)
793     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
794
795   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
796     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
797
798   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
799     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
800
801   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
802     {
803       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
804       target_flags |= ARM_FLAG_APCS_FRAME;
805     }
806   
807   if (TARGET_POKE_FUNCTION_NAME)
808     target_flags |= ARM_FLAG_APCS_FRAME;
809   
810   if (TARGET_APCS_REENT && flag_pic)
811     error ("-fpic and -mapcs-reent are incompatible");
812   
813   if (TARGET_APCS_REENT)
814     warning ("APCS reentrant code not supported.  Ignored");
815   
816   /* If this target is normally configured to use APCS frames, warn if they
817      are turned off and debugging is turned on.  */
818   if (TARGET_ARM
819       && write_symbols != NO_DEBUG
820       && !TARGET_APCS_FRAME
821       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
822     warning ("-g with -mno-apcs-frame may not give sensible debugging");
823   
824   /* If stack checking is disabled, we can use r10 as the PIC register,
825      which keeps r9 available.  */
826   if (flag_pic)
827     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
828   
829   if (TARGET_APCS_FLOAT)
830     warning ("passing floating point arguments in fp regs not yet supported");
831   
832   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
833   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
834   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
835   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
836   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
837   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
838   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
839   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
840   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
841
842   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
843   arm_is_strong = (tune_flags & FL_STRONG) != 0;
844   thumb_code = (TARGET_ARM == 0);
845   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
846                     && !(tune_flags & FL_ARCH4))) != 0;
847   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
848   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
849
850   /* V5 code we generate is completely interworking capable, so we turn off
851      TARGET_INTERWORK here to avoid many tests later on.  */
852
853   /* XXX However, we must pass the right pre-processor defines to CPP
854      or GLD can get confused.  This is a hack.  */
855   if (TARGET_INTERWORK)
856     arm_cpp_interwork = 1;
857
858   if (arm_arch5)
859     target_flags &= ~ARM_FLAG_INTERWORK;
860
861   if (target_abi_name)
862     {
863       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
864         {
865           if (streq (arm_all_abis[i].name, target_abi_name))
866             {
867               arm_abi = arm_all_abis[i].abi_type;
868               break;
869             }
870         }
871       if (i == ARRAY_SIZE (arm_all_abis))
872         error ("invalid ABI option: -mabi=%s", target_abi_name);
873     }
874   else
875     arm_abi = ARM_DEFAULT_ABI;
876
877   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
878     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
879
880   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
881     error ("iwmmxt abi requires an iwmmxt capable cpu");
882
883   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
884   if (target_fpu_name == NULL && target_fpe_name != NULL)
885     {
886       if (streq (target_fpe_name, "2"))
887         target_fpu_name = "fpe2";
888       else if (streq (target_fpe_name, "3"))
889         target_fpu_name = "fpe3";
890       else
891         error ("invalid floating point emulation option: -mfpe=%s",
892                target_fpe_name);
893     }
894   if (target_fpu_name != NULL)
895     {
896       /* The user specified a FPU.  */
897       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
898         {
899           if (streq (all_fpus[i].name, target_fpu_name))
900             {
901               arm_fpu_arch = all_fpus[i].fpu;
902               arm_fpu_tune = arm_fpu_arch;
903               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
904               break;
905             }
906         }
907       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
908         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
909     }
910   else
911     {
912 #ifdef FPUTYPE_DEFAULT
913       /* Use the default if it is specified for this platform.  */
914       arm_fpu_arch = FPUTYPE_DEFAULT;
915       arm_fpu_tune = FPUTYPE_DEFAULT;
916 #else
917       /* Pick one based on CPU type.  */
918       /* ??? Some targets assume FPA is the default.
919       if ((insn_flags & FL_VFP) != 0)
920         arm_fpu_arch = FPUTYPE_VFP;
921       else
922       */
923       if (arm_arch_cirrus)
924         arm_fpu_arch = FPUTYPE_MAVERICK;
925       else
926         arm_fpu_arch = FPUTYPE_FPA_EMU2;
927 #endif
928       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
929         arm_fpu_tune = FPUTYPE_FPA;
930       else
931         arm_fpu_tune = arm_fpu_arch;
932       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
933       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
934         abort ();
935     }
936
937   if (target_float_abi_name != NULL)
938     {
939       /* The user specified a FP ABI.  */
940       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
941         {
942           if (streq (all_float_abis[i].name, target_float_abi_name))
943             {
944               arm_float_abi = all_float_abis[i].abi_type;
945               break;
946             }
947         }
948       if (i == ARRAY_SIZE (all_float_abis))
949         error ("invalid floating point abi: -mfloat-abi=%s",
950                target_float_abi_name);
951     }
952   else
953     {
954       /* Use soft-float target flag.  */
955       if (target_flags & ARM_FLAG_SOFT_FLOAT)
956         arm_float_abi = ARM_FLOAT_ABI_SOFT;
957       else
958         arm_float_abi = ARM_FLOAT_ABI_HARD;
959     }
960
961   if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
962     sorry ("-mfloat-abi=softfp");
963   /* If soft-float is specified then don't use FPU.  */
964   if (TARGET_SOFT_FLOAT)
965     arm_fpu_arch = FPUTYPE_NONE;
966   
967   /* For arm2/3 there is no need to do any scheduling if there is only
968      a floating point emulator, or we are doing software floating-point.  */
969   if ((TARGET_SOFT_FLOAT
970        || arm_fpu_tune == FPUTYPE_FPA_EMU2
971        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
972       && (tune_flags & FL_MODE32) == 0)
973     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
974   
975   /* Override the default structure alignment for AAPCS ABI.  */
976   if (arm_abi == ARM_ABI_AAPCS)
977     arm_structure_size_boundary = 8;
978
979   if (structure_size_string != NULL)
980     {
981       int size = strtol (structure_size_string, NULL, 0);
982
983       if (size == 8 || size == 32
984           || (ARM_DOUBLEWORD_ALIGN && size == 64))
985         arm_structure_size_boundary = size;
986       else
987         warning ("structure size boundary can only be set to %s",
988                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
989     }
990
991   if (arm_pic_register_string != NULL)
992     {
993       int pic_register = decode_reg_name (arm_pic_register_string);
994
995       if (!flag_pic)
996         warning ("-mpic-register= is useless without -fpic");
997
998       /* Prevent the user from choosing an obviously stupid PIC register.  */
999       else if (pic_register < 0 || call_used_regs[pic_register]
1000                || pic_register == HARD_FRAME_POINTER_REGNUM
1001                || pic_register == STACK_POINTER_REGNUM
1002                || pic_register >= PC_REGNUM)
1003         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1004       else
1005         arm_pic_register = pic_register;
1006     }
1007
1008   if (TARGET_THUMB && flag_schedule_insns)
1009     {
1010       /* Don't warn since it's on by default in -O2.  */
1011       flag_schedule_insns = 0;
1012     }
1013
1014   if (optimize_size)
1015     {
1016       /* There's some dispute as to whether this should be 1 or 2.  However,
1017          experiments seem to show that in pathological cases a setting of
1018          1 degrades less severely than a setting of 2.  This could change if
1019          other parts of the compiler change their behavior.  */
1020       arm_constant_limit = 1;
1021
1022       /* If optimizing for size, bump the number of instructions that we
1023          are prepared to conditionally execute (even on a StrongARM).  */
1024       max_insns_skipped = 6;
1025     }
1026   else
1027     {
1028       /* For processors with load scheduling, it never costs more than
1029          2 cycles to load a constant, and the load scheduler may well
1030          reduce that to 1.  */
1031       if (tune_flags & FL_LDSCHED)
1032         arm_constant_limit = 1;
1033
1034       /* On XScale the longer latency of a load makes it more difficult
1035          to achieve a good schedule, so it's faster to synthesize
1036          constants that can be done in two insns.  */
1037       if (arm_tune_xscale)
1038         arm_constant_limit = 2;
1039
1040       /* StrongARM has early execution of branches, so a sequence
1041          that is worth skipping is shorter.  */
1042       if (arm_is_strong)
1043         max_insns_skipped = 3;
1044     }
1045
1046   /* Register global variables with the garbage collector.  */
1047   arm_add_gc_roots ();
1048 }
1049
1050 static void
1051 arm_add_gc_roots (void)
1052 {
1053   gcc_obstack_init(&minipool_obstack);
1054   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1055 }
1056 \f
1057 /* A table of known ARM exception types.
1058    For use with the interrupt function attribute.  */
1059
1060 typedef struct
1061 {
1062   const char *const arg;
1063   const unsigned long return_value;
1064 }
1065 isr_attribute_arg;
1066
1067 static const isr_attribute_arg isr_attribute_args [] =
1068 {
1069   { "IRQ",   ARM_FT_ISR },
1070   { "irq",   ARM_FT_ISR },
1071   { "FIQ",   ARM_FT_FIQ },
1072   { "fiq",   ARM_FT_FIQ },
1073   { "ABORT", ARM_FT_ISR },
1074   { "abort", ARM_FT_ISR },
1075   { "ABORT", ARM_FT_ISR },
1076   { "abort", ARM_FT_ISR },
1077   { "UNDEF", ARM_FT_EXCEPTION },
1078   { "undef", ARM_FT_EXCEPTION },
1079   { "SWI",   ARM_FT_EXCEPTION },
1080   { "swi",   ARM_FT_EXCEPTION },
1081   { NULL,    ARM_FT_NORMAL }
1082 };
1083
1084 /* Returns the (interrupt) function type of the current
1085    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1086
1087 static unsigned long
1088 arm_isr_value (tree argument)
1089 {
1090   const isr_attribute_arg * ptr;
1091   const char *              arg;
1092
1093   /* No argument - default to IRQ.  */
1094   if (argument == NULL_TREE)
1095     return ARM_FT_ISR;
1096
1097   /* Get the value of the argument.  */
1098   if (TREE_VALUE (argument) == NULL_TREE
1099       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1100     return ARM_FT_UNKNOWN;
1101
1102   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1103
1104   /* Check it against the list of known arguments.  */
1105   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1106     if (streq (arg, ptr->arg))
1107       return ptr->return_value;
1108
1109   /* An unrecognized interrupt type.  */
1110   return ARM_FT_UNKNOWN;
1111 }
1112
1113 /* Computes the type of the current function.  */
1114
1115 static unsigned long
1116 arm_compute_func_type (void)
1117 {
1118   unsigned long type = ARM_FT_UNKNOWN;
1119   tree a;
1120   tree attr;
1121   
1122   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1123     abort ();
1124
1125   /* Decide if the current function is volatile.  Such functions
1126      never return, and many memory cycles can be saved by not storing
1127      register values that will never be needed again.  This optimization
1128      was added to speed up context switching in a kernel application.  */
1129   if (optimize > 0
1130       && TREE_NOTHROW (current_function_decl)
1131       && TREE_THIS_VOLATILE (current_function_decl))
1132     type |= ARM_FT_VOLATILE;
1133   
1134   if (cfun->static_chain_decl != NULL)
1135     type |= ARM_FT_NESTED;
1136
1137   attr = DECL_ATTRIBUTES (current_function_decl);
1138   
1139   a = lookup_attribute ("naked", attr);
1140   if (a != NULL_TREE)
1141     type |= ARM_FT_NAKED;
1142
1143   if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1144     type |= ARM_FT_EXCEPTION_HANDLER;
1145   else
1146     {
1147       a = lookup_attribute ("isr", attr);
1148       if (a == NULL_TREE)
1149         a = lookup_attribute ("interrupt", attr);
1150       
1151       if (a == NULL_TREE)
1152         type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1153       else
1154         type |= arm_isr_value (TREE_VALUE (a));
1155     }
1156   
1157   return type;
1158 }
1159
1160 /* Returns the type of the current function.  */
1161
1162 unsigned long
1163 arm_current_func_type (void)
1164 {
1165   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1166     cfun->machine->func_type = arm_compute_func_type ();
1167
1168   return cfun->machine->func_type;
1169 }
1170 \f
1171 /* Return 1 if it is possible to return using a single instruction.  
1172    If SIBLING is non-null, this is a test for a return before a sibling
1173    call.  SIBLING is the call insn, so we can examine its register usage.  */
1174
1175 int
1176 use_return_insn (int iscond, rtx sibling)
1177 {
1178   int regno;
1179   unsigned int func_type;
1180   unsigned long saved_int_regs;
1181   unsigned HOST_WIDE_INT stack_adjust;
1182   arm_stack_offsets *offsets;
1183
1184   /* Never use a return instruction before reload has run.  */
1185   if (!reload_completed)
1186     return 0;
1187
1188   func_type = arm_current_func_type ();
1189
1190   /* Naked functions and volatile functions need special
1191      consideration.  */
1192   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1193     return 0;
1194
1195   /* So do interrupt functions that use the frame pointer.  */
1196   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1197     return 0;
1198
1199   offsets = arm_get_frame_offsets ();
1200   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1201
1202   /* As do variadic functions.  */
1203   if (current_function_pretend_args_size
1204       || cfun->machine->uses_anonymous_args
1205       /* Or if the function calls __builtin_eh_return () */
1206       || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
1207       /* Or if the function calls alloca */
1208       || current_function_calls_alloca
1209       /* Or if there is a stack adjustment.  However, if the stack pointer
1210          is saved on the stack, we can use a pre-incrementing stack load.  */
1211       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1212     return 0;
1213
1214   saved_int_regs = arm_compute_save_reg_mask ();
1215
1216   /* Unfortunately, the insn
1217
1218        ldmib sp, {..., sp, ...}
1219
1220      triggers a bug on most SA-110 based devices, such that the stack
1221      pointer won't be correctly restored if the instruction takes a
1222      page fault.  We work around this problem by popping r3 along with
1223      the other registers, since that is never slower than executing
1224      another instruction.  
1225
1226      We test for !arm_arch5 here, because code for any architecture
1227      less than this could potentially be run on one of the buggy
1228      chips.  */
1229   if (stack_adjust == 4 && !arm_arch5)
1230     {
1231       /* Validate that r3 is a call-clobbered register (always true in
1232          the default abi) ...  */
1233       if (!call_used_regs[3])
1234         return 0;
1235
1236       /* ... that it isn't being used for a return value (always true
1237          until we implement return-in-regs), or for a tail-call
1238          argument ...  */
1239       if (sibling)
1240         {
1241           if (GET_CODE (sibling) != CALL_INSN)
1242             abort ();
1243
1244           if (find_regno_fusage (sibling, USE, 3))
1245             return 0;
1246         }
1247
1248       /* ... and that there are no call-saved registers in r0-r2
1249          (always true in the default ABI).  */
1250       if (saved_int_regs & 0x7)
1251         return 0;
1252     }
1253
1254   /* Can't be done if interworking with Thumb, and any registers have been
1255      stacked.  */
1256   if (TARGET_INTERWORK && saved_int_regs != 0)
1257     return 0;
1258
1259   /* On StrongARM, conditional returns are expensive if they aren't
1260      taken and multiple registers have been stacked.  */
1261   if (iscond && arm_is_strong)
1262     {
1263       /* Conditional return when just the LR is stored is a simple 
1264          conditional-load instruction, that's not expensive.  */
1265       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1266         return 0;
1267
1268       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1269         return 0;
1270     }
1271
1272   /* If there are saved registers but the LR isn't saved, then we need
1273      two instructions for the return.  */
1274   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1275     return 0;
1276
1277   /* Can't be done if any of the FPA regs are pushed,
1278      since this also requires an insn.  */
1279   if (TARGET_HARD_FLOAT && TARGET_FPA)
1280     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1281       if (regs_ever_live[regno] && !call_used_regs[regno])
1282         return 0;
1283
1284   /* Likewise VFP regs.  */
1285   if (TARGET_HARD_FLOAT && TARGET_VFP)
1286     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1287       if (regs_ever_live[regno] && !call_used_regs[regno])
1288         return 0;
1289
1290   if (TARGET_REALLY_IWMMXT)
1291     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1292       if (regs_ever_live[regno] && ! call_used_regs [regno])
1293         return 0;
1294
1295   return 1;
1296 }
1297
1298 /* Return TRUE if int I is a valid immediate ARM constant.  */
1299
1300 int
1301 const_ok_for_arm (HOST_WIDE_INT i)
1302 {
1303   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1304
1305   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must 
1306      be all zero, or all one.  */
1307   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1308       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1309           != ((~(unsigned HOST_WIDE_INT) 0)
1310               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1311     return FALSE;
1312   
1313   /* Fast return for 0 and powers of 2 */
1314   if ((i & (i - 1)) == 0)
1315     return TRUE;
1316
1317   do
1318     {
1319       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1320         return TRUE;
1321       mask =
1322           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1323                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1324     }
1325   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1326
1327   return FALSE;
1328 }
1329
1330 /* Return true if I is a valid constant for the operation CODE.  */
1331 static int
1332 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1333 {
1334   if (const_ok_for_arm (i))
1335     return 1;
1336
1337   switch (code)
1338     {
1339     case PLUS:
1340       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1341
1342     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1343     case XOR:
1344     case IOR:
1345       return 0;
1346
1347     case AND:
1348       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1349
1350     default:
1351       abort ();
1352     }
1353 }
1354
1355 /* Emit a sequence of insns to handle a large constant.
1356    CODE is the code of the operation required, it can be any of SET, PLUS,
1357    IOR, AND, XOR, MINUS;
1358    MODE is the mode in which the operation is being performed;
1359    VAL is the integer to operate on;
1360    SOURCE is the other operand (a register, or a null-pointer for SET);
1361    SUBTARGETS means it is safe to create scratch registers if that will
1362    either produce a simpler sequence, or we will want to cse the values.
1363    Return value is the number of insns emitted.  */
1364
1365 int
1366 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1367                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1368 {
1369   rtx cond;
1370
1371   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1372     cond = COND_EXEC_TEST (PATTERN (insn));
1373   else
1374     cond = NULL_RTX;
1375
1376   if (subtargets || code == SET
1377       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1378           && REGNO (target) != REGNO (source)))
1379     {
1380       /* After arm_reorg has been called, we can't fix up expensive
1381          constants by pushing them into memory so we must synthesize
1382          them in-line, regardless of the cost.  This is only likely to
1383          be more costly on chips that have load delay slots and we are
1384          compiling without running the scheduler (so no splitting
1385          occurred before the final instruction emission).
1386
1387          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1388       */
1389       if (!after_arm_reorg
1390           && !cond
1391           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source, 
1392                                 1, 0)
1393               > arm_constant_limit + (code != SET)))
1394         {
1395           if (code == SET)
1396             {
1397               /* Currently SET is the only monadic value for CODE, all
1398                  the rest are diadic.  */
1399               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1400               return 1;
1401             }
1402           else
1403             {
1404               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1405
1406               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1407               /* For MINUS, the value is subtracted from, since we never
1408                  have subtraction of a constant.  */
1409               if (code == MINUS)
1410                 emit_insn (gen_rtx_SET (VOIDmode, target,
1411                                         gen_rtx_MINUS (mode, temp, source)));
1412               else
1413                 emit_insn (gen_rtx_SET (VOIDmode, target,
1414                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1415               return 2;
1416             }
1417         }
1418     }
1419
1420   return arm_gen_constant (code, mode, cond, val, target, source, subtargets, 
1421                            1);
1422 }
1423
1424 static int
1425 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1426 {
1427   HOST_WIDE_INT temp1;
1428   int num_insns = 0;
1429   do
1430     {
1431       int end;
1432           
1433       if (i <= 0)
1434         i += 32;
1435       if (remainder & (3 << (i - 2)))
1436         {
1437           end = i - 8;
1438           if (end < 0)
1439             end += 32;
1440           temp1 = remainder & ((0x0ff << end)
1441                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1442           remainder &= ~temp1;
1443           num_insns++;
1444           i -= 6;
1445         }
1446       i -= 2;
1447     } while (remainder);
1448   return num_insns;
1449 }
1450
1451 /* Emit an instruction with the indicated PATTERN.  If COND is
1452    non-NULL, conditionalize the execution of the instruction on COND
1453    being true.  */
1454
1455 static void
1456 emit_constant_insn (rtx cond, rtx pattern)
1457 {
1458   if (cond)
1459     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1460   emit_insn (pattern);
1461 }
1462
1463 /* As above, but extra parameter GENERATE which, if clear, suppresses
1464    RTL generation.  */
1465
1466 static int
1467 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1468                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1469                   int generate)
1470 {
1471   int can_invert = 0;
1472   int can_negate = 0;
1473   int can_negate_initial = 0;
1474   int can_shift = 0;
1475   int i;
1476   int num_bits_set = 0;
1477   int set_sign_bit_copies = 0;
1478   int clear_sign_bit_copies = 0;
1479   int clear_zero_bit_copies = 0;
1480   int set_zero_bit_copies = 0;
1481   int insns = 0;
1482   unsigned HOST_WIDE_INT temp1, temp2;
1483   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1484
1485   /* Find out which operations are safe for a given CODE.  Also do a quick
1486      check for degenerate cases; these can occur when DImode operations
1487      are split.  */
1488   switch (code)
1489     {
1490     case SET:
1491       can_invert = 1;
1492       can_shift = 1;
1493       can_negate = 1;
1494       break;
1495
1496     case PLUS:
1497       can_negate = 1;
1498       can_negate_initial = 1;
1499       break;
1500
1501     case IOR:
1502       if (remainder == 0xffffffff)
1503         {
1504           if (generate)
1505             emit_constant_insn (cond,
1506                                 gen_rtx_SET (VOIDmode, target,
1507                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1508           return 1;
1509         }
1510       if (remainder == 0)
1511         {
1512           if (reload_completed && rtx_equal_p (target, source))
1513             return 0;
1514           if (generate)
1515             emit_constant_insn (cond,
1516                                 gen_rtx_SET (VOIDmode, target, source));
1517           return 1;
1518         }
1519       break;
1520
1521     case AND:
1522       if (remainder == 0)
1523         {
1524           if (generate)
1525             emit_constant_insn (cond,
1526                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1527           return 1;
1528         }
1529       if (remainder == 0xffffffff)
1530         {
1531           if (reload_completed && rtx_equal_p (target, source))
1532             return 0;
1533           if (generate)
1534             emit_constant_insn (cond,
1535                                 gen_rtx_SET (VOIDmode, target, source));
1536           return 1;
1537         }
1538       can_invert = 1;
1539       break;
1540
1541     case XOR:
1542       if (remainder == 0)
1543         {
1544           if (reload_completed && rtx_equal_p (target, source))
1545             return 0;
1546           if (generate)
1547             emit_constant_insn (cond,
1548                                 gen_rtx_SET (VOIDmode, target, source));
1549           return 1;
1550         }
1551       if (remainder == 0xffffffff)
1552         {
1553           if (generate)
1554             emit_constant_insn (cond,
1555                                 gen_rtx_SET (VOIDmode, target,
1556                                              gen_rtx_NOT (mode, source)));
1557           return 1;
1558         }
1559
1560       /* We don't know how to handle this yet below.  */
1561       abort ();
1562
1563     case MINUS:
1564       /* We treat MINUS as (val - source), since (source - val) is always
1565          passed as (source + (-val)).  */
1566       if (remainder == 0)
1567         {
1568           if (generate)
1569             emit_constant_insn (cond,
1570                                 gen_rtx_SET (VOIDmode, target,
1571                                              gen_rtx_NEG (mode, source)));
1572           return 1;
1573         }
1574       if (const_ok_for_arm (val))
1575         {
1576           if (generate)
1577             emit_constant_insn (cond,
1578                                 gen_rtx_SET (VOIDmode, target, 
1579                                              gen_rtx_MINUS (mode, GEN_INT (val),
1580                                                             source)));
1581           return 1;
1582         }
1583       can_negate = 1;
1584
1585       break;
1586
1587     default:
1588       abort ();
1589     }
1590
1591   /* If we can do it in one insn get out quickly.  */
1592   if (const_ok_for_arm (val)
1593       || (can_negate_initial && const_ok_for_arm (-val))
1594       || (can_invert && const_ok_for_arm (~val)))
1595     {
1596       if (generate)
1597         emit_constant_insn (cond,
1598                             gen_rtx_SET (VOIDmode, target,
1599                                          (source 
1600                                           ? gen_rtx_fmt_ee (code, mode, source,
1601                                                             GEN_INT (val))
1602                                           : GEN_INT (val))));
1603       return 1;
1604     }
1605
1606   /* Calculate a few attributes that may be useful for specific
1607      optimizations.  */
1608   for (i = 31; i >= 0; i--)
1609     {
1610       if ((remainder & (1 << i)) == 0)
1611         clear_sign_bit_copies++;
1612       else
1613         break;
1614     }
1615
1616   for (i = 31; i >= 0; i--)
1617     {
1618       if ((remainder & (1 << i)) != 0)
1619         set_sign_bit_copies++;
1620       else
1621         break;
1622     }
1623
1624   for (i = 0; i <= 31; i++)
1625     {
1626       if ((remainder & (1 << i)) == 0)
1627         clear_zero_bit_copies++;
1628       else
1629         break;
1630     }
1631
1632   for (i = 0; i <= 31; i++)
1633     {
1634       if ((remainder & (1 << i)) != 0)
1635         set_zero_bit_copies++;
1636       else
1637         break;
1638     }
1639
1640   switch (code)
1641     {
1642     case SET:
1643       /* See if we can do this by sign_extending a constant that is known
1644          to be negative.  This is a good, way of doing it, since the shift
1645          may well merge into a subsequent insn.  */
1646       if (set_sign_bit_copies > 1)
1647         {
1648           if (const_ok_for_arm
1649               (temp1 = ARM_SIGN_EXTEND (remainder 
1650                                         << (set_sign_bit_copies - 1))))
1651             {
1652               if (generate)
1653                 {
1654                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1655                   emit_constant_insn (cond,
1656                                       gen_rtx_SET (VOIDmode, new_src, 
1657                                                    GEN_INT (temp1)));
1658                   emit_constant_insn (cond,
1659                                       gen_ashrsi3 (target, new_src, 
1660                                                    GEN_INT (set_sign_bit_copies - 1)));
1661                 }
1662               return 2;
1663             }
1664           /* For an inverted constant, we will need to set the low bits,
1665              these will be shifted out of harm's way.  */
1666           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1667           if (const_ok_for_arm (~temp1))
1668             {
1669               if (generate)
1670                 {
1671                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1672                   emit_constant_insn (cond,
1673                                       gen_rtx_SET (VOIDmode, new_src,
1674                                                    GEN_INT (temp1)));
1675                   emit_constant_insn (cond,
1676                                       gen_ashrsi3 (target, new_src, 
1677                                                    GEN_INT (set_sign_bit_copies - 1)));
1678                 }
1679               return 2;
1680             }
1681         }
1682
1683       /* See if we can generate this by setting the bottom (or the top)
1684          16 bits, and then shifting these into the other half of the
1685          word.  We only look for the simplest cases, to do more would cost
1686          too much.  Be careful, however, not to generate this when the
1687          alternative would take fewer insns.  */
1688       if (val & 0xffff0000)
1689         {
1690           temp1 = remainder & 0xffff0000;
1691           temp2 = remainder & 0x0000ffff;
1692
1693           /* Overlaps outside this range are best done using other methods.  */
1694           for (i = 9; i < 24; i++)
1695             {
1696               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1697                   && !const_ok_for_arm (temp2))
1698                 {
1699                   rtx new_src = (subtargets
1700                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1701                                  : target);
1702                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1703                                             source, subtargets, generate);
1704                   source = new_src;
1705                   if (generate)
1706                     emit_constant_insn 
1707                       (cond,
1708                        gen_rtx_SET
1709                        (VOIDmode, target,
1710                         gen_rtx_IOR (mode,
1711                                      gen_rtx_ASHIFT (mode, source,
1712                                                      GEN_INT (i)),
1713                                      source)));
1714                   return insns + 1;
1715                 }
1716             }
1717
1718           /* Don't duplicate cases already considered.  */
1719           for (i = 17; i < 24; i++)
1720             {
1721               if (((temp1 | (temp1 >> i)) == remainder)
1722                   && !const_ok_for_arm (temp1))
1723                 {
1724                   rtx new_src = (subtargets
1725                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1726                                  : target);
1727                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1728                                             source, subtargets, generate);
1729                   source = new_src;
1730                   if (generate)
1731                     emit_constant_insn
1732                       (cond,
1733                        gen_rtx_SET (VOIDmode, target,
1734                                     gen_rtx_IOR
1735                                     (mode,
1736                                      gen_rtx_LSHIFTRT (mode, source,
1737                                                        GEN_INT (i)),
1738                                      source)));
1739                   return insns + 1;
1740                 }
1741             }
1742         }
1743       break;
1744
1745     case IOR:
1746     case XOR:
1747       /* If we have IOR or XOR, and the constant can be loaded in a
1748          single instruction, and we can find a temporary to put it in,
1749          then this can be done in two instructions instead of 3-4.  */
1750       if (subtargets
1751           /* TARGET can't be NULL if SUBTARGETS is 0 */
1752           || (reload_completed && !reg_mentioned_p (target, source)))
1753         {
1754           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1755             {
1756               if (generate)
1757                 {
1758                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1759
1760                   emit_constant_insn (cond,
1761                                       gen_rtx_SET (VOIDmode, sub, 
1762                                                    GEN_INT (val)));
1763                   emit_constant_insn (cond,
1764                                       gen_rtx_SET (VOIDmode, target, 
1765                                                    gen_rtx_fmt_ee (code, mode,
1766                                                                    source, sub)));
1767                 }
1768               return 2;
1769             }
1770         }
1771
1772       if (code == XOR)
1773         break;
1774
1775       if (set_sign_bit_copies > 8
1776           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1777         {
1778           if (generate)
1779             {
1780               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1781               rtx shift = GEN_INT (set_sign_bit_copies);
1782
1783               emit_constant_insn 
1784                 (cond,
1785                  gen_rtx_SET (VOIDmode, sub,
1786                               gen_rtx_NOT (mode, 
1787                                            gen_rtx_ASHIFT (mode,
1788                                                            source, 
1789                                                            shift))));
1790               emit_constant_insn 
1791                 (cond,
1792                  gen_rtx_SET (VOIDmode, target,
1793                               gen_rtx_NOT (mode,
1794                                            gen_rtx_LSHIFTRT (mode, sub,
1795                                                              shift))));
1796             }
1797           return 2;
1798         }
1799
1800       if (set_zero_bit_copies > 8
1801           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1802         {
1803           if (generate)
1804             {
1805               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1806               rtx shift = GEN_INT (set_zero_bit_copies);
1807
1808               emit_constant_insn
1809                 (cond,
1810                  gen_rtx_SET (VOIDmode, sub,
1811                               gen_rtx_NOT (mode,
1812                                            gen_rtx_LSHIFTRT (mode,
1813                                                              source,
1814                                                              shift))));
1815               emit_constant_insn 
1816                 (cond,
1817                  gen_rtx_SET (VOIDmode, target,
1818                               gen_rtx_NOT (mode,
1819                                            gen_rtx_ASHIFT (mode, sub,
1820                                                            shift))));
1821             }
1822           return 2;
1823         }
1824
1825       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1826         {
1827           if (generate)
1828             {
1829               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1830               emit_constant_insn (cond,
1831                                   gen_rtx_SET (VOIDmode, sub,
1832                                                gen_rtx_NOT (mode, source)));
1833               source = sub;
1834               if (subtargets)
1835                 sub = gen_reg_rtx (mode);
1836               emit_constant_insn (cond,
1837                                   gen_rtx_SET (VOIDmode, sub,
1838                                                gen_rtx_AND (mode, source, 
1839                                                             GEN_INT (temp1))));
1840               emit_constant_insn (cond,
1841                                   gen_rtx_SET (VOIDmode, target,
1842                                                gen_rtx_NOT (mode, sub)));
1843             }
1844           return 3;
1845         }
1846       break;
1847
1848     case AND:
1849       /* See if two shifts will do 2 or more insn's worth of work.  */
1850       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1851         {
1852           HOST_WIDE_INT shift_mask = ((0xffffffff
1853                                        << (32 - clear_sign_bit_copies))
1854                                       & 0xffffffff);
1855
1856           if ((remainder | shift_mask) != 0xffffffff)
1857             {
1858               if (generate)
1859                 {
1860                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1861                   insns = arm_gen_constant (AND, mode, cond, 
1862                                             remainder | shift_mask,
1863                                             new_src, source, subtargets, 1);
1864                   source = new_src;
1865                 }
1866               else
1867                 {
1868                   rtx targ = subtargets ? NULL_RTX : target;
1869                   insns = arm_gen_constant (AND, mode, cond,
1870                                             remainder | shift_mask,
1871                                             targ, source, subtargets, 0);
1872                 }
1873             }
1874
1875           if (generate)
1876             {
1877               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1878               rtx shift = GEN_INT (clear_sign_bit_copies);
1879
1880               emit_insn (gen_ashlsi3 (new_src, source, shift));
1881               emit_insn (gen_lshrsi3 (target, new_src, shift));
1882             }
1883
1884           return insns + 2;
1885         }
1886
1887       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1888         {
1889           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1890           
1891           if ((remainder | shift_mask) != 0xffffffff)
1892             {
1893               if (generate)
1894                 {
1895                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1896
1897                   insns = arm_gen_constant (AND, mode, cond,
1898                                             remainder | shift_mask,
1899                                             new_src, source, subtargets, 1);
1900                   source = new_src;
1901                 }
1902               else
1903                 {
1904                   rtx targ = subtargets ? NULL_RTX : target;
1905
1906                   insns = arm_gen_constant (AND, mode, cond,
1907                                             remainder | shift_mask,
1908                                             targ, source, subtargets, 0);
1909                 }
1910             }
1911
1912           if (generate)
1913             {
1914               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1915               rtx shift = GEN_INT (clear_zero_bit_copies);
1916
1917               emit_insn (gen_lshrsi3 (new_src, source, shift));
1918               emit_insn (gen_ashlsi3 (target, new_src, shift));
1919             }
1920
1921           return insns + 2;
1922         }
1923
1924       break;
1925
1926     default:
1927       break;
1928     }
1929
1930   for (i = 0; i < 32; i++)
1931     if (remainder & (1 << i))
1932       num_bits_set++;
1933
1934   if (code == AND || (can_invert && num_bits_set > 16))
1935     remainder = (~remainder) & 0xffffffff;
1936   else if (code == PLUS && num_bits_set > 16)
1937     remainder = (-remainder) & 0xffffffff;
1938   else
1939     {
1940       can_invert = 0;
1941       can_negate = 0;
1942     }
1943
1944   /* Now try and find a way of doing the job in either two or three
1945      instructions.
1946      We start by looking for the largest block of zeros that are aligned on
1947      a 2-bit boundary, we then fill up the temps, wrapping around to the
1948      top of the word when we drop off the bottom.
1949      In the worst case this code should produce no more than four insns.  */
1950   {
1951     int best_start = 0;
1952     int best_consecutive_zeros = 0;
1953
1954     for (i = 0; i < 32; i += 2)
1955       {
1956         int consecutive_zeros = 0;
1957
1958         if (!(remainder & (3 << i)))
1959           {
1960             while ((i < 32) && !(remainder & (3 << i)))
1961               {
1962                 consecutive_zeros += 2;
1963                 i += 2;
1964               }
1965             if (consecutive_zeros > best_consecutive_zeros)
1966               {
1967                 best_consecutive_zeros = consecutive_zeros;
1968                 best_start = i - consecutive_zeros;
1969               }
1970             i -= 2;
1971           }
1972       }
1973
1974     /* So long as it won't require any more insns to do so, it's
1975        desirable to emit a small constant (in bits 0...9) in the last
1976        insn.  This way there is more chance that it can be combined with
1977        a later addressing insn to form a pre-indexed load or store
1978        operation.  Consider:
1979
1980                *((volatile int *)0xe0000100) = 1;
1981                *((volatile int *)0xe0000110) = 2;
1982
1983        We want this to wind up as:
1984
1985                 mov rA, #0xe0000000
1986                 mov rB, #1
1987                 str rB, [rA, #0x100]
1988                 mov rB, #2
1989                 str rB, [rA, #0x110]
1990
1991        rather than having to synthesize both large constants from scratch.
1992
1993        Therefore, we calculate how many insns would be required to emit
1994        the constant starting from `best_start', and also starting from 
1995        zero (ie with bit 31 first to be output).  If `best_start' doesn't 
1996        yield a shorter sequence, we may as well use zero.  */
1997     if (best_start != 0
1998         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1999         && (count_insns_for_constant (remainder, 0) <= 
2000             count_insns_for_constant (remainder, best_start)))
2001       best_start = 0;
2002
2003     /* Now start emitting the insns.  */
2004     i = best_start;
2005     do
2006       {
2007         int end;
2008
2009         if (i <= 0)
2010           i += 32;
2011         if (remainder & (3 << (i - 2)))
2012           {
2013             end = i - 8;
2014             if (end < 0)
2015               end += 32;
2016             temp1 = remainder & ((0x0ff << end)
2017                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2018             remainder &= ~temp1;
2019
2020             if (generate)
2021               {
2022                 rtx new_src, temp1_rtx;
2023
2024                 if (code == SET || code == MINUS)
2025                   {
2026                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2027                     if (can_invert && code != MINUS)
2028                       temp1 = ~temp1;
2029                   }
2030                 else
2031                   {
2032                     if (remainder && subtargets)
2033                       new_src = gen_reg_rtx (mode);
2034                     else
2035                       new_src = target;
2036                     if (can_invert)
2037                       temp1 = ~temp1;
2038                     else if (can_negate)
2039                       temp1 = -temp1;
2040                   }
2041
2042                 temp1 = trunc_int_for_mode (temp1, mode);
2043                 temp1_rtx = GEN_INT (temp1);
2044
2045                 if (code == SET)
2046                   ;
2047                 else if (code == MINUS)
2048                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2049                 else
2050                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2051
2052                 emit_constant_insn (cond,
2053                                     gen_rtx_SET (VOIDmode, new_src, 
2054                                                  temp1_rtx));
2055                 source = new_src;
2056               }
2057
2058             if (code == SET)
2059               {
2060                 can_invert = 0;
2061                 code = PLUS;
2062               }
2063             else if (code == MINUS)
2064               code = PLUS;
2065
2066             insns++;
2067             i -= 6;
2068           }
2069         i -= 2;
2070       }
2071     while (remainder);
2072   }
2073
2074   return insns;
2075 }
2076
2077 /* Canonicalize a comparison so that we are more likely to recognize it.
2078    This can be done for a few constant compares, where we can make the
2079    immediate value easier to load.  */
2080
2081 enum rtx_code
2082 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2083 {
2084   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2085
2086   switch (code)
2087     {
2088     case EQ:
2089     case NE:
2090       return code;
2091
2092     case GT:
2093     case LE:
2094       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2095           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2096         {
2097           *op1 = GEN_INT (i + 1);
2098           return code == GT ? GE : LT;
2099         }
2100       break;
2101
2102     case GE:
2103     case LT:
2104       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2105           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2106         {
2107           *op1 = GEN_INT (i - 1);
2108           return code == GE ? GT : LE;
2109         }
2110       break;
2111
2112     case GTU:
2113     case LEU:
2114       if (i != ~((unsigned HOST_WIDE_INT) 0)
2115           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2116         {
2117           *op1 = GEN_INT (i + 1);
2118           return code == GTU ? GEU : LTU;
2119         }
2120       break;
2121
2122     case GEU:
2123     case LTU:
2124       if (i != 0
2125           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2126         {
2127           *op1 = GEN_INT (i - 1);
2128           return code == GEU ? GTU : LEU;
2129         }
2130       break;
2131
2132     default:
2133       abort ();
2134     }
2135
2136   return code;
2137 }
2138
2139
2140 /* Define how to find the value returned by a function.  */
2141
2142 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2143 {
2144   enum machine_mode mode;
2145   int unsignedp ATTRIBUTE_UNUSED;
2146   rtx r ATTRIBUTE_UNUSED;
2147
2148   
2149   mode = TYPE_MODE (type);
2150   /* Promote integer types.  */
2151   if (INTEGRAL_TYPE_P (type))
2152     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2153   return LIBCALL_VALUE(mode);
2154 }
2155
2156
2157 /* Decide whether a type should be returned in memory (true)
2158    or in a register (false).  This is called by the macro
2159    RETURN_IN_MEMORY.  */
2160 int
2161 arm_return_in_memory (tree type)
2162 {
2163   HOST_WIDE_INT size;
2164
2165   if (!AGGREGATE_TYPE_P (type))
2166     /* All simple types are returned in registers.  */
2167     return 0;
2168
2169   size = int_size_in_bytes (type);
2170
2171   if (arm_abi != ARM_ABI_APCS)
2172     {
2173       /* ATPCS and later return aggregate types in memory only if they are
2174          larger than a word (or are variable size).  */
2175       return (size < 0 || size > UNITS_PER_WORD);
2176     }
2177   
2178   /* For the arm-wince targets we choose to be compatible with Microsoft's
2179      ARM and Thumb compilers, which always return aggregates in memory.  */
2180 #ifndef ARM_WINCE
2181   /* All structures/unions bigger than one word are returned in memory.
2182      Also catch the case where int_size_in_bytes returns -1.  In this case
2183      the aggregate is either huge or of variable size, and in either case
2184      we will want to return it via memory and not in a register.  */
2185   if (size < 0 || size > UNITS_PER_WORD)
2186     return 1;
2187   
2188   if (TREE_CODE (type) == RECORD_TYPE)
2189     {
2190       tree field;
2191
2192       /* For a struct the APCS says that we only return in a register
2193          if the type is 'integer like' and every addressable element
2194          has an offset of zero.  For practical purposes this means
2195          that the structure can have at most one non bit-field element
2196          and that this element must be the first one in the structure.  */
2197       
2198       /* Find the first field, ignoring non FIELD_DECL things which will
2199          have been created by C++.  */
2200       for (field = TYPE_FIELDS (type);
2201            field && TREE_CODE (field) != FIELD_DECL;
2202            field = TREE_CHAIN (field))
2203         continue;
2204       
2205       if (field == NULL)
2206         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2207
2208       /* Check that the first field is valid for returning in a register.  */
2209
2210       /* ... Floats are not allowed */
2211       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2212         return 1;
2213
2214       /* ... Aggregates that are not themselves valid for returning in
2215          a register are not allowed.  */
2216       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2217         return 1;
2218
2219       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2220          since they are not addressable.  */
2221       for (field = TREE_CHAIN (field);
2222            field;
2223            field = TREE_CHAIN (field))
2224         {
2225           if (TREE_CODE (field) != FIELD_DECL)
2226             continue;
2227           
2228           if (!DECL_BIT_FIELD_TYPE (field))
2229             return 1;
2230         }
2231
2232       return 0;
2233     }
2234   
2235   if (TREE_CODE (type) == UNION_TYPE)
2236     {
2237       tree field;
2238
2239       /* Unions can be returned in registers if every element is
2240          integral, or can be returned in an integer register.  */
2241       for (field = TYPE_FIELDS (type);
2242            field;
2243            field = TREE_CHAIN (field))
2244         {
2245           if (TREE_CODE (field) != FIELD_DECL)
2246             continue;
2247
2248           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2249             return 1;
2250           
2251           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2252             return 1;
2253         }
2254       
2255       return 0;
2256     }
2257 #endif /* not ARM_WINCE */  
2258   
2259   /* Return all other types in memory.  */
2260   return 1;
2261 }
2262
2263 /* Indicate whether or not words of a double are in big-endian order.  */
2264
2265 int
2266 arm_float_words_big_endian (void)
2267 {
2268   if (TARGET_MAVERICK)
2269     return 0;
2270
2271   /* For FPA, float words are always big-endian.  For VFP, floats words
2272      follow the memory system mode.  */
2273
2274   if (TARGET_FPA)
2275     {
2276       return 1;
2277     }
2278
2279   if (TARGET_VFP)
2280     return (TARGET_BIG_END ? 1 : 0);
2281
2282   return 1;
2283 }
2284
2285 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2286    for a call to a function whose data type is FNTYPE.
2287    For a library call, FNTYPE is NULL.  */
2288 void
2289 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype, 
2290                           rtx libname  ATTRIBUTE_UNUSED,
2291                           tree fndecl ATTRIBUTE_UNUSED)
2292 {
2293   /* On the ARM, the offset starts at 0.  */
2294   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2295   pcum->iwmmxt_nregs = 0;
2296   pcum->can_split = true;
2297   
2298   pcum->call_cookie = CALL_NORMAL;
2299
2300   if (TARGET_LONG_CALLS)
2301     pcum->call_cookie = CALL_LONG;
2302     
2303   /* Check for long call/short call attributes.  The attributes
2304      override any command line option.  */
2305   if (fntype)
2306     {
2307       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2308         pcum->call_cookie = CALL_SHORT;
2309       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2310         pcum->call_cookie = CALL_LONG;
2311     }
2312
2313   /* Varargs vectors are treated the same as long long.
2314      named_count avoids having to change the way arm handles 'named' */
2315   pcum->named_count = 0;
2316   pcum->nargs = 0;
2317
2318   if (TARGET_REALLY_IWMMXT && fntype)
2319     {
2320       tree fn_arg;
2321
2322       for (fn_arg = TYPE_ARG_TYPES (fntype);
2323            fn_arg;
2324            fn_arg = TREE_CHAIN (fn_arg))
2325         pcum->named_count += 1;
2326
2327       if (! pcum->named_count)
2328         pcum->named_count = INT_MAX;
2329     }
2330 }
2331
2332
2333 /* Return true if mode/type need doubleword alignment.  */
2334 bool
2335 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2336 {
2337   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2338           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2339 }
2340
2341
2342 /* Determine where to put an argument to a function.
2343    Value is zero to push the argument on the stack,
2344    or a hard register in which to store the argument.
2345
2346    MODE is the argument's machine mode.
2347    TYPE is the data type of the argument (as a tree).
2348     This is null for libcalls where that information may
2349     not be available.
2350    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2351     the preceding args and about the function being called.
2352    NAMED is nonzero if this argument is a named parameter
2353     (otherwise it is an extra parameter matching an ellipsis).  */
2354
2355 rtx
2356 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2357                   tree type, int named)
2358 {
2359   int nregs;
2360
2361   /* Varargs vectors are treated the same as long long.
2362      named_count avoids having to change the way arm handles 'named' */
2363   if (TARGET_IWMMXT_ABI
2364       && VECTOR_MODE_SUPPORTED_P (mode)
2365       && pcum->named_count > pcum->nargs + 1)
2366     {
2367       if (pcum->iwmmxt_nregs <= 9)
2368         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2369       else
2370         {
2371           pcum->can_split = false;
2372           return NULL_RTX;
2373         }
2374     }
2375
2376   /* Put doubleword aligned quantities in even register pairs.  */
2377   if (pcum->nregs & 1
2378       && ARM_DOUBLEWORD_ALIGN
2379       && arm_needs_doubleword_align (mode, type))
2380     pcum->nregs++;
2381
2382   if (mode == VOIDmode)
2383     /* Compute operand 2 of the call insn.  */
2384     return GEN_INT (pcum->call_cookie);
2385
2386   /* Only allow splitting an arg between regs and memory if all preceding
2387      args were allocated to regs.  For args passed by reference we only count
2388      the reference pointer.  */
2389   if (pcum->can_split)
2390     nregs = 1;
2391   else
2392     nregs = ARM_NUM_REGS2 (mode, type);
2393
2394   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2395     return NULL_RTX;
2396   
2397   return gen_rtx_REG (mode, pcum->nregs);
2398 }
2399
2400 /* Variable sized types are passed by reference.  This is a GCC
2401    extension to the ARM ABI.  */
2402
2403 static bool
2404 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2405                        enum machine_mode mode ATTRIBUTE_UNUSED,
2406                        tree type, bool named ATTRIBUTE_UNUSED)
2407 {
2408   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2409 }
2410 \f
2411 /* Encode the current state of the #pragma [no_]long_calls.  */
2412 typedef enum
2413 {
2414   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2415   LONG,         /* #pragma long_calls is in effect.  */
2416   SHORT         /* #pragma no_long_calls is in effect.  */
2417 } arm_pragma_enum;
2418
2419 static arm_pragma_enum arm_pragma_long_calls = OFF;
2420
2421 void
2422 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2423 {
2424   arm_pragma_long_calls = LONG;
2425 }
2426
2427 void
2428 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2429 {
2430   arm_pragma_long_calls = SHORT;
2431 }
2432
2433 void
2434 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2435 {
2436   arm_pragma_long_calls = OFF;
2437 }
2438 \f
2439 /* Table of machine attributes.  */
2440 const struct attribute_spec arm_attribute_table[] =
2441 {
2442   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2443   /* Function calls made to this symbol must be done indirectly, because
2444      it may lie outside of the 26 bit addressing range of a normal function
2445      call.  */
2446   { "long_call",    0, 0, false, true,  true,  NULL },
2447   /* Whereas these functions are always known to reside within the 26 bit
2448      addressing range.  */
2449   { "short_call",   0, 0, false, true,  true,  NULL },
2450   /* Interrupt Service Routines have special prologue and epilogue requirements.  */ 
2451   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2452   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2453   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2454 #ifdef ARM_PE
2455   /* ARM/PE has three new attributes:
2456      interfacearm - ?
2457      dllexport - for exporting a function/variable that will live in a dll
2458      dllimport - for importing a function/variable from a dll
2459
2460      Microsoft allows multiple declspecs in one __declspec, separating
2461      them with spaces.  We do NOT support this.  Instead, use __declspec
2462      multiple times.
2463   */
2464   { "dllimport",    0, 0, true,  false, false, NULL },
2465   { "dllexport",    0, 0, true,  false, false, NULL },
2466   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2467 #endif
2468   { NULL,           0, 0, false, false, false, NULL }
2469 };
2470
2471 /* Handle an attribute requiring a FUNCTION_DECL;
2472    arguments as in struct attribute_spec.handler.  */
2473 static tree
2474 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2475                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2476 {
2477   if (TREE_CODE (*node) != FUNCTION_DECL)
2478     {
2479       warning ("`%s' attribute only applies to functions",
2480                IDENTIFIER_POINTER (name));
2481       *no_add_attrs = true;
2482     }
2483
2484   return NULL_TREE;
2485 }
2486
2487 /* Handle an "interrupt" or "isr" attribute;
2488    arguments as in struct attribute_spec.handler.  */
2489 static tree
2490 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2491                           bool *no_add_attrs)
2492 {
2493   if (DECL_P (*node))
2494     {
2495       if (TREE_CODE (*node) != FUNCTION_DECL)
2496         {
2497           warning ("`%s' attribute only applies to functions",
2498                    IDENTIFIER_POINTER (name));
2499           *no_add_attrs = true;
2500         }
2501       /* FIXME: the argument if any is checked for type attributes;
2502          should it be checked for decl ones?  */
2503     }
2504   else
2505     {
2506       if (TREE_CODE (*node) == FUNCTION_TYPE
2507           || TREE_CODE (*node) == METHOD_TYPE)
2508         {
2509           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2510             {
2511               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2512               *no_add_attrs = true;
2513             }
2514         }
2515       else if (TREE_CODE (*node) == POINTER_TYPE
2516                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2517                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2518                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2519         {
2520           *node = build_type_copy (*node);
2521           TREE_TYPE (*node) = build_type_attribute_variant
2522             (TREE_TYPE (*node),
2523              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2524           *no_add_attrs = true;
2525         }
2526       else
2527         {
2528           /* Possibly pass this attribute on from the type to a decl.  */
2529           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2530                        | (int) ATTR_FLAG_FUNCTION_NEXT
2531                        | (int) ATTR_FLAG_ARRAY_NEXT))
2532             {
2533               *no_add_attrs = true;
2534               return tree_cons (name, args, NULL_TREE);
2535             }
2536           else
2537             {
2538               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2539             }
2540         }
2541     }
2542
2543   return NULL_TREE;
2544 }
2545
2546 /* Return 0 if the attributes for two types are incompatible, 1 if they
2547    are compatible, and 2 if they are nearly compatible (which causes a
2548    warning to be generated).  */
2549 static int
2550 arm_comp_type_attributes (tree type1, tree type2)
2551 {
2552   int l1, l2, s1, s2;
2553   
2554   /* Check for mismatch of non-default calling convention.  */
2555   if (TREE_CODE (type1) != FUNCTION_TYPE)
2556     return 1;
2557
2558   /* Check for mismatched call attributes.  */
2559   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2560   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2561   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2562   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2563
2564   /* Only bother to check if an attribute is defined.  */
2565   if (l1 | l2 | s1 | s2)
2566     {
2567       /* If one type has an attribute, the other must have the same attribute.  */
2568       if ((l1 != l2) || (s1 != s2))
2569         return 0;
2570
2571       /* Disallow mixed attributes.  */
2572       if ((l1 & s2) || (l2 & s1))
2573         return 0;
2574     }
2575   
2576   /* Check for mismatched ISR attribute.  */
2577   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2578   if (! l1)
2579     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2580   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2581   if (! l2)
2582     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2583   if (l1 != l2)
2584     return 0;
2585
2586   return 1;
2587 }
2588
2589 /*  Encode long_call or short_call attribute by prefixing
2590     symbol name in DECL with a special character FLAG.  */
2591 void
2592 arm_encode_call_attribute (tree decl, int flag)
2593 {
2594   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2595   int          len = strlen (str);
2596   char *       newstr;
2597
2598   /* Do not allow weak functions to be treated as short call.  */
2599   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2600     return;
2601
2602   newstr = alloca (len + 2);
2603   newstr[0] = flag;
2604   strcpy (newstr + 1, str);
2605
2606   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2607   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2608 }
2609
2610 /*  Assigns default attributes to newly defined type.  This is used to
2611     set short_call/long_call attributes for function types of
2612     functions defined inside corresponding #pragma scopes.  */
2613 static void
2614 arm_set_default_type_attributes (tree type)
2615 {
2616   /* Add __attribute__ ((long_call)) to all functions, when
2617      inside #pragma long_calls or __attribute__ ((short_call)),
2618      when inside #pragma no_long_calls.  */
2619   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2620     {
2621       tree type_attr_list, attr_name;
2622       type_attr_list = TYPE_ATTRIBUTES (type);
2623
2624       if (arm_pragma_long_calls == LONG)
2625         attr_name = get_identifier ("long_call");
2626       else if (arm_pragma_long_calls == SHORT)
2627         attr_name = get_identifier ("short_call");
2628       else
2629         return;
2630
2631       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2632       TYPE_ATTRIBUTES (type) = type_attr_list;
2633     }
2634 }
2635 \f
2636 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2637    defined within the current compilation unit.  If this cannot be
2638    determined, then 0 is returned.  */
2639 static int
2640 current_file_function_operand (rtx sym_ref)
2641 {
2642   /* This is a bit of a fib.  A function will have a short call flag
2643      applied to its name if it has the short call attribute, or it has
2644      already been defined within the current compilation unit.  */
2645   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2646     return 1;
2647
2648   /* The current function is always defined within the current compilation
2649      unit.  if it s a weak definition however, then this may not be the real
2650      definition of the function, and so we have to say no.  */
2651   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2652       && !DECL_WEAK (current_function_decl))
2653     return 1;
2654
2655   /* We cannot make the determination - default to returning 0.  */
2656   return 0;
2657 }
2658
2659 /* Return nonzero if a 32 bit "long_call" should be generated for
2660    this call.  We generate a long_call if the function:
2661
2662         a.  has an __attribute__((long call))
2663      or b.  is within the scope of a #pragma long_calls
2664      or c.  the -mlong-calls command line switch has been specified
2665
2666    However we do not generate a long call if the function:
2667    
2668         d.  has an __attribute__ ((short_call))
2669      or e.  is inside the scope of a #pragma no_long_calls
2670      or f.  has an __attribute__ ((section))
2671      or g.  is defined within the current compilation unit.
2672    
2673    This function will be called by C fragments contained in the machine
2674    description file.  CALL_REF and CALL_COOKIE correspond to the matched
2675    rtl operands.  CALL_SYMBOL is used to distinguish between
2676    two different callers of the function.  It is set to 1 in the
2677    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2678    and "call_value" patterns.  This is because of the difference in the
2679    SYM_REFs passed by these patterns.  */
2680 int
2681 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2682 {
2683   if (!call_symbol)
2684     {
2685       if (GET_CODE (sym_ref) != MEM)
2686         return 0;
2687
2688       sym_ref = XEXP (sym_ref, 0);
2689     }
2690
2691   if (GET_CODE (sym_ref) != SYMBOL_REF)
2692     return 0;
2693
2694   if (call_cookie & CALL_SHORT)
2695     return 0;
2696
2697   if (TARGET_LONG_CALLS && flag_function_sections)
2698     return 1;
2699   
2700   if (current_file_function_operand (sym_ref))
2701     return 0;
2702   
2703   return (call_cookie & CALL_LONG)
2704     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2705     || TARGET_LONG_CALLS;
2706 }
2707
2708 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2709 static bool
2710 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2711 {
2712   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2713
2714   if (cfun->machine->sibcall_blocked)
2715     return false;
2716
2717   /* Never tailcall something for which we have no decl, or if we
2718      are in Thumb mode.  */
2719   if (decl == NULL || TARGET_THUMB)
2720     return false;
2721
2722   /* Get the calling method.  */
2723   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2724     call_type = CALL_SHORT;
2725   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2726     call_type = CALL_LONG;
2727
2728   /* Cannot tail-call to long calls, since these are out of range of
2729      a branch instruction.  However, if not compiling PIC, we know
2730      we can reach the symbol if it is in this compilation unit.  */
2731   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2732     return false;
2733
2734   /* If we are interworking and the function is not declared static
2735      then we can't tail-call it unless we know that it exists in this 
2736      compilation unit (since it might be a Thumb routine).  */
2737   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2738     return false;
2739
2740   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2741   if (IS_INTERRUPT (arm_current_func_type ()))
2742     return false;
2743
2744   /* Everything else is ok.  */
2745   return true;
2746 }
2747
2748 \f
2749 /* Addressing mode support functions.  */
2750
2751 /* Return nonzero if X is a legitimate immediate operand when compiling
2752    for PIC.  */
2753 int
2754 legitimate_pic_operand_p (rtx x)
2755 {
2756   if (CONSTANT_P (x)
2757       && flag_pic
2758       && (GET_CODE (x) == SYMBOL_REF
2759           || (GET_CODE (x) == CONST
2760               && GET_CODE (XEXP (x, 0)) == PLUS
2761               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2762     return 0;
2763
2764   return 1;
2765 }
2766
2767 rtx
2768 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2769 {
2770   if (GET_CODE (orig) == SYMBOL_REF
2771       || GET_CODE (orig) == LABEL_REF)
2772     {
2773 #ifndef AOF_ASSEMBLER
2774       rtx pic_ref, address;
2775 #endif
2776       rtx insn;
2777       int subregs = 0;
2778
2779       if (reg == 0)
2780         {
2781           if (no_new_pseudos)
2782             abort ();
2783           else
2784             reg = gen_reg_rtx (Pmode);
2785
2786           subregs = 1;
2787         }
2788
2789 #ifdef AOF_ASSEMBLER
2790       /* The AOF assembler can generate relocations for these directly, and
2791          understands that the PIC register has to be added into the offset.  */
2792       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2793 #else
2794       if (subregs)
2795         address = gen_reg_rtx (Pmode);
2796       else
2797         address = reg;
2798
2799       if (TARGET_ARM)
2800         emit_insn (gen_pic_load_addr_arm (address, orig));
2801       else
2802         emit_insn (gen_pic_load_addr_thumb (address, orig));
2803
2804       if ((GET_CODE (orig) == LABEL_REF
2805            || (GET_CODE (orig) == SYMBOL_REF && 
2806                SYMBOL_REF_LOCAL_P (orig)))
2807           && NEED_GOT_RELOC)
2808         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2809       else
2810         {
2811           pic_ref = gen_rtx_MEM (Pmode,
2812                                  gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2813                                                address));
2814           RTX_UNCHANGING_P (pic_ref) = 1;
2815         }
2816
2817       insn = emit_move_insn (reg, pic_ref);
2818 #endif
2819       current_function_uses_pic_offset_table = 1;
2820       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2821          by loop.  */
2822       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2823                                             REG_NOTES (insn));
2824       return reg;
2825     }
2826   else if (GET_CODE (orig) == CONST)
2827     {
2828       rtx base, offset;
2829
2830       if (GET_CODE (XEXP (orig, 0)) == PLUS
2831           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2832         return orig;
2833
2834       if (reg == 0)
2835         {
2836           if (no_new_pseudos)
2837             abort ();
2838           else
2839             reg = gen_reg_rtx (Pmode);
2840         }
2841
2842       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2843         {
2844           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2845           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2846                                            base == reg ? 0 : reg);
2847         }
2848       else
2849         abort ();
2850
2851       if (GET_CODE (offset) == CONST_INT)
2852         {
2853           /* The base register doesn't really matter, we only want to
2854              test the index for the appropriate mode.  */
2855           if (!arm_legitimate_index_p (mode, offset, SET, 0))
2856             {
2857               if (!no_new_pseudos)
2858                 offset = force_reg (Pmode, offset);
2859               else
2860                 abort ();
2861             }
2862
2863           if (GET_CODE (offset) == CONST_INT)
2864             return plus_constant (base, INTVAL (offset));
2865         }
2866
2867       if (GET_MODE_SIZE (mode) > 4
2868           && (GET_MODE_CLASS (mode) == MODE_INT
2869               || TARGET_SOFT_FLOAT))
2870         {
2871           emit_insn (gen_addsi3 (reg, base, offset));
2872           return reg;
2873         }
2874
2875       return gen_rtx_PLUS (Pmode, base, offset);
2876     }
2877
2878   return orig;
2879 }
2880
2881 /* Generate code to load the PIC register.  PROLOGUE is true if
2882    called from arm_expand_prologue (in which case we want the 
2883    generated insns at the start of the function);  false if called
2884    by an exception receiver that needs the PIC register reloaded
2885    (in which case the insns are just dumped at the current location).  */
2886 void
2887 arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
2888 {
2889 #ifndef AOF_ASSEMBLER
2890   rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2891   rtx global_offset_table;
2892
2893   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2894     return;
2895
2896   if (!flag_pic)
2897     abort ();
2898
2899   start_sequence ();
2900   l1 = gen_label_rtx ();
2901
2902   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2903   /* On the ARM the PC register contains 'dot + 8' at the time of the
2904      addition, on the Thumb it is 'dot + 4'.  */
2905   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2906   if (GOT_PCREL)
2907     pic_tmp2 = gen_rtx_CONST (VOIDmode,
2908                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2909   else
2910     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2911
2912   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2913   
2914   if (TARGET_ARM)
2915     {
2916       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2917       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2918     }
2919   else
2920     {
2921       emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2922       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2923     }
2924
2925   seq = get_insns ();
2926   end_sequence ();
2927   if (prologue)
2928     emit_insn_after (seq, get_insns ());
2929   else
2930     emit_insn (seq);
2931
2932   /* Need to emit this whether or not we obey regdecls,
2933      since setjmp/longjmp can cause life info to screw up.  */
2934   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2935 #endif /* AOF_ASSEMBLER */
2936 }
2937
2938 /* Return nonzero if X is valid as an ARM state addressing register.  */
2939 static int
2940 arm_address_register_rtx_p (rtx x, int strict_p)
2941 {
2942   int regno;
2943
2944   if (GET_CODE (x) != REG)
2945     return 0;
2946
2947   regno = REGNO (x);
2948
2949   if (strict_p)
2950     return ARM_REGNO_OK_FOR_BASE_P (regno);
2951
2952   return (regno <= LAST_ARM_REGNUM
2953           || regno >= FIRST_PSEUDO_REGISTER
2954           || regno == FRAME_POINTER_REGNUM
2955           || regno == ARG_POINTER_REGNUM);
2956 }
2957
2958 /* Return nonzero if X is a valid ARM state address operand.  */
2959 int
2960 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2961                           int strict_p)
2962 {
2963   bool use_ldrd;
2964   enum rtx_code code = GET_CODE (x);
2965   
2966   if (arm_address_register_rtx_p (x, strict_p))
2967     return 1;
2968
2969   use_ldrd = (TARGET_LDRD
2970               && (mode == DImode
2971                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
2972
2973   if (code == POST_INC || code == PRE_DEC
2974       || ((code == PRE_INC || code == POST_DEC)
2975           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
2976     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2977
2978   else if ((code == POST_MODIFY || code == PRE_MODIFY)
2979            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2980            && GET_CODE (XEXP (x, 1)) == PLUS
2981            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
2982     {
2983       rtx addend = XEXP (XEXP (x, 1), 1);
2984
2985       /* Don't allow ldrd post increment by register becuase it's hard
2986          to fixup invalid register choices.  */
2987       if (use_ldrd
2988           && GET_CODE (x) == POST_MODIFY
2989           && GET_CODE (addend) == REG)
2990         return 0;
2991
2992       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
2993               && arm_legitimate_index_p (mode, addend, outer, strict_p));
2994     }
2995
2996   /* After reload constants split into minipools will have addresses
2997      from a LABEL_REF.  */
2998   else if (reload_completed
2999            && (code == LABEL_REF
3000                || (code == CONST
3001                    && GET_CODE (XEXP (x, 0)) == PLUS
3002                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3003                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3004     return 1;
3005
3006   else if (mode == TImode)
3007     return 0;
3008
3009   else if (code == PLUS)
3010     {
3011       rtx xop0 = XEXP (x, 0);
3012       rtx xop1 = XEXP (x, 1);
3013
3014       return ((arm_address_register_rtx_p (xop0, strict_p)
3015                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3016               || (arm_address_register_rtx_p (xop1, strict_p)
3017                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3018     }
3019
3020 #if 0
3021   /* Reload currently can't handle MINUS, so disable this for now */
3022   else if (GET_CODE (x) == MINUS)
3023     {
3024       rtx xop0 = XEXP (x, 0);
3025       rtx xop1 = XEXP (x, 1);
3026
3027       return (arm_address_register_rtx_p (xop0, strict_p)
3028               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3029     }
3030 #endif
3031
3032   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3033            && code == SYMBOL_REF
3034            && CONSTANT_POOL_ADDRESS_P (x)
3035            && ! (flag_pic
3036                  && symbol_mentioned_p (get_pool_constant (x))))
3037     return 1;
3038
3039   return 0;
3040 }
3041
3042 /* Return nonzero if INDEX is valid for an address index operand in
3043    ARM state.  */
3044 static int
3045 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3046                         int strict_p)
3047 {
3048   HOST_WIDE_INT range;
3049   enum rtx_code code = GET_CODE (index);
3050
3051   /* Standard coprocessor addressing modes.  */
3052   if (TARGET_HARD_FLOAT
3053       && (TARGET_FPA || TARGET_MAVERICK)
3054       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3055           || (TARGET_MAVERICK && mode == DImode)))
3056     return (code == CONST_INT && INTVAL (index) < 1024
3057             && INTVAL (index) > -1024
3058             && (INTVAL (index) & 3) == 0);
3059
3060   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3061     return (code == CONST_INT
3062             && INTVAL (index) < 1024
3063             && INTVAL (index) > -1024
3064             && (INTVAL (index) & 3) == 0);
3065
3066   if (arm_address_register_rtx_p (index, strict_p)
3067       && (GET_MODE_SIZE (mode) <= 4))
3068     return 1;
3069
3070   if (mode == DImode || mode == DFmode)
3071     {
3072       if (code == CONST_INT)
3073         {
3074           HOST_WIDE_INT val = INTVAL (index);
3075
3076           if (TARGET_LDRD)
3077             return val > -256 && val < 256;
3078           else
3079             return val > -4096 && val < 4092;
3080         }
3081
3082       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3083     }
3084
3085   if (GET_MODE_SIZE (mode) <= 4
3086       && ! (arm_arch4
3087             && (mode == HImode
3088                 || (mode == QImode && outer == SIGN_EXTEND))))
3089     {
3090       if (code == MULT)
3091         {
3092           rtx xiop0 = XEXP (index, 0);
3093           rtx xiop1 = XEXP (index, 1);
3094
3095           return ((arm_address_register_rtx_p (xiop0, strict_p)
3096                    && power_of_two_operand (xiop1, SImode))
3097                   || (arm_address_register_rtx_p (xiop1, strict_p)
3098                       && power_of_two_operand (xiop0, SImode)));
3099         }
3100       else if (code == LSHIFTRT || code == ASHIFTRT
3101                || code == ASHIFT || code == ROTATERT)
3102         {
3103           rtx op = XEXP (index, 1);
3104
3105           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3106                   && GET_CODE (op) == CONST_INT
3107                   && INTVAL (op) > 0
3108                   && INTVAL (op) <= 31);
3109         }
3110     }
3111
3112   /* For ARM v4 we may be doing a sign-extend operation during the
3113      load.  */
3114   if (arm_arch4)
3115     {
3116       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3117         range = 256;
3118       else
3119         range = 4096;
3120     }
3121   else
3122     range = (mode == HImode) ? 4095 : 4096;
3123
3124   return (code == CONST_INT
3125           && INTVAL (index) < range
3126           && INTVAL (index) > -range);
3127 }
3128
3129 /* Return nonzero if X is valid as a Thumb state base register.  */
3130 static int
3131 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3132 {
3133   int regno;
3134
3135   if (GET_CODE (x) != REG)
3136     return 0;
3137
3138   regno = REGNO (x);
3139
3140   if (strict_p)
3141     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3142
3143   return (regno <= LAST_LO_REGNUM
3144           || regno > LAST_VIRTUAL_REGISTER
3145           || regno == FRAME_POINTER_REGNUM
3146           || (GET_MODE_SIZE (mode) >= 4
3147               && (regno == STACK_POINTER_REGNUM
3148                   || regno >= FIRST_PSEUDO_REGISTER
3149                   || x == hard_frame_pointer_rtx
3150                   || x == arg_pointer_rtx)));
3151 }
3152
3153 /* Return nonzero if x is a legitimate index register.  This is the case
3154    for any base register that can access a QImode object.  */
3155 inline static int
3156 thumb_index_register_rtx_p (rtx x, int strict_p)
3157 {
3158   return thumb_base_register_rtx_p (x, QImode, strict_p);
3159 }
3160
3161 /* Return nonzero if x is a legitimate Thumb-state address.
3162  
3163    The AP may be eliminated to either the SP or the FP, so we use the
3164    least common denominator, e.g. SImode, and offsets from 0 to 64.
3165
3166    ??? Verify whether the above is the right approach.
3167
3168    ??? Also, the FP may be eliminated to the SP, so perhaps that
3169    needs special handling also.
3170
3171    ??? Look at how the mips16 port solves this problem.  It probably uses
3172    better ways to solve some of these problems.
3173
3174    Although it is not incorrect, we don't accept QImode and HImode
3175    addresses based on the frame pointer or arg pointer until the
3176    reload pass starts.  This is so that eliminating such addresses
3177    into stack based ones won't produce impossible code.  */
3178 int
3179 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3180 {
3181   /* ??? Not clear if this is right.  Experiment.  */
3182   if (GET_MODE_SIZE (mode) < 4
3183       && !(reload_in_progress || reload_completed)
3184       && (reg_mentioned_p (frame_pointer_rtx, x)
3185           || reg_mentioned_p (arg_pointer_rtx, x)
3186           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3187           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3188           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3189           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3190     return 0;
3191
3192   /* Accept any base register.  SP only in SImode or larger.  */
3193   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3194     return 1;
3195
3196   /* This is PC relative data before arm_reorg runs.  */
3197   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3198            && GET_CODE (x) == SYMBOL_REF
3199            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3200     return 1;
3201
3202   /* This is PC relative data after arm_reorg runs.  */
3203   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3204            && (GET_CODE (x) == LABEL_REF
3205                || (GET_CODE (x) == CONST
3206                    && GET_CODE (XEXP (x, 0)) == PLUS
3207                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3208                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3209     return 1;
3210
3211   /* Post-inc indexing only supported for SImode and larger.  */
3212   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3213            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3214     return 1;
3215
3216   else if (GET_CODE (x) == PLUS)
3217     {
3218       /* REG+REG address can be any two index registers.  */
3219       /* We disallow FRAME+REG addressing since we know that FRAME
3220          will be replaced with STACK, and SP relative addressing only
3221          permits SP+OFFSET.  */
3222       if (GET_MODE_SIZE (mode) <= 4
3223           && XEXP (x, 0) != frame_pointer_rtx
3224           && XEXP (x, 1) != frame_pointer_rtx
3225           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3226           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3227         return 1;
3228
3229       /* REG+const has 5-7 bit offset for non-SP registers.  */
3230       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3231                 || XEXP (x, 0) == arg_pointer_rtx)
3232                && GET_CODE (XEXP (x, 1)) == CONST_INT
3233                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3234         return 1;
3235
3236       /* REG+const has 10 bit offset for SP, but only SImode and
3237          larger is supported.  */
3238       /* ??? Should probably check for DI/DFmode overflow here
3239          just like GO_IF_LEGITIMATE_OFFSET does.  */
3240       else if (GET_CODE (XEXP (x, 0)) == REG
3241                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3242                && GET_MODE_SIZE (mode) >= 4
3243                && GET_CODE (XEXP (x, 1)) == CONST_INT
3244                && INTVAL (XEXP (x, 1)) >= 0
3245                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3246                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3247         return 1;
3248
3249       else if (GET_CODE (XEXP (x, 0)) == REG
3250                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3251                && GET_MODE_SIZE (mode) >= 4
3252                && GET_CODE (XEXP (x, 1)) == CONST_INT
3253                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3254         return 1;
3255     }
3256
3257   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3258            && GET_MODE_SIZE (mode) == 4
3259            && GET_CODE (x) == SYMBOL_REF
3260            && CONSTANT_POOL_ADDRESS_P (x)
3261            && !(flag_pic
3262                 && symbol_mentioned_p (get_pool_constant (x))))
3263     return 1;
3264
3265   return 0;
3266 }
3267
3268 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3269    instruction of mode MODE.  */
3270 int
3271 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3272 {
3273   switch (GET_MODE_SIZE (mode))
3274     {
3275     case 1:
3276       return val >= 0 && val < 32;
3277
3278     case 2:
3279       return val >= 0 && val < 64 && (val & 1) == 0;
3280
3281     default:
3282       return (val >= 0
3283               && (val + GET_MODE_SIZE (mode)) <= 128
3284               && (val & 3) == 0);
3285     }
3286 }
3287
3288 /* Try machine-dependent ways of modifying an illegitimate address
3289    to be legitimate.  If we find one, return the new, valid address.  */
3290 rtx
3291 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3292 {
3293   if (GET_CODE (x) == PLUS)
3294     {
3295       rtx xop0 = XEXP (x, 0);
3296       rtx xop1 = XEXP (x, 1);
3297
3298       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3299         xop0 = force_reg (SImode, xop0);
3300
3301       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3302         xop1 = force_reg (SImode, xop1);
3303
3304       if (ARM_BASE_REGISTER_RTX_P (xop0)
3305           && GET_CODE (xop1) == CONST_INT)
3306         {
3307           HOST_WIDE_INT n, low_n;
3308           rtx base_reg, val;
3309           n = INTVAL (xop1);
3310
3311           /* VFP addressing modes actually allow greater offsets, but for
3312              now we just stick with the lowest common denominator.  */
3313           if (mode == DImode
3314               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3315             {
3316               low_n = n & 0x0f;
3317               n &= ~0x0f;
3318               if (low_n > 4)
3319                 {
3320                   n += 16;
3321                   low_n -= 16;
3322                 }
3323             }
3324           else
3325             {
3326               low_n = ((mode) == TImode ? 0
3327                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3328               n -= low_n;
3329             }
3330
3331           base_reg = gen_reg_rtx (SImode);
3332           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3333                                              GEN_INT (n)), NULL_RTX);
3334           emit_move_insn (base_reg, val);
3335           x = (low_n == 0 ? base_reg
3336                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3337         }
3338       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3339         x = gen_rtx_PLUS (SImode, xop0, xop1);
3340     }
3341
3342   /* XXX We don't allow MINUS any more -- see comment in
3343      arm_legitimate_address_p ().  */
3344   else if (GET_CODE (x) == MINUS)
3345     {
3346       rtx xop0 = XEXP (x, 0);
3347       rtx xop1 = XEXP (x, 1);
3348
3349       if (CONSTANT_P (xop0))
3350         xop0 = force_reg (SImode, xop0);
3351
3352       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3353         xop1 = force_reg (SImode, xop1);
3354
3355       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3356         x = gen_rtx_MINUS (SImode, xop0, xop1);
3357     }
3358
3359   if (flag_pic)
3360     {
3361       /* We need to find and carefully transform any SYMBOL and LABEL
3362          references; so go back to the original address expression.  */
3363       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3364
3365       if (new_x != orig_x)
3366         x = new_x;
3367     }
3368
3369   return x;
3370 }
3371
3372
3373 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3374    to be legitimate.  If we find one, return the new, valid address.  */
3375 rtx
3376 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3377 {
3378   if (GET_CODE (x) == PLUS
3379       && GET_CODE (XEXP (x, 1)) == CONST_INT
3380       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3381           || INTVAL (XEXP (x, 1)) < 0))
3382     {
3383       rtx xop0 = XEXP (x, 0);
3384       rtx xop1 = XEXP (x, 1);
3385       HOST_WIDE_INT offset = INTVAL (xop1);
3386
3387       /* Try and fold the offset into a biasing of the base register and
3388          then offsetting that.  Don't do this when optimizing for space
3389          since it can cause too many CSEs.  */
3390       if (optimize_size && offset >= 0
3391           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3392         {
3393           HOST_WIDE_INT delta;
3394
3395           if (offset >= 256)
3396             delta = offset - (256 - GET_MODE_SIZE (mode));
3397           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3398             delta = 31 * GET_MODE_SIZE (mode);
3399           else
3400             delta = offset & (~31 * GET_MODE_SIZE (mode));
3401
3402           xop0 = force_operand (plus_constant (xop0, offset - delta),
3403                                 NULL_RTX);
3404           x = plus_constant (xop0, delta);
3405         }
3406       else if (offset < 0 && offset > -256)
3407         /* Small negative offsets are best done with a subtract before the
3408            dereference, forcing these into a register normally takes two
3409            instructions.  */
3410         x = force_operand (x, NULL_RTX);
3411       else
3412         {
3413           /* For the remaining cases, force the constant into a register.  */
3414           xop1 = force_reg (SImode, xop1);
3415           x = gen_rtx_PLUS (SImode, xop0, xop1);
3416         }
3417     }
3418   else if (GET_CODE (x) == PLUS
3419            && s_register_operand (XEXP (x, 1), SImode)
3420            && !s_register_operand (XEXP (x, 0), SImode))
3421     {
3422       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3423
3424       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3425     }
3426
3427   if (flag_pic)
3428     {
3429       /* We need to find and carefully transform any SYMBOL and LABEL
3430          references; so go back to the original address expression.  */
3431       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3432
3433       if (new_x != orig_x)
3434         x = new_x;
3435     }
3436
3437   return x;
3438 }
3439
3440 \f
3441
3442 #define REG_OR_SUBREG_REG(X)                                            \
3443   (GET_CODE (X) == REG                                                  \
3444    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3445
3446 #define REG_OR_SUBREG_RTX(X)                    \
3447    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3448
3449 #ifndef COSTS_N_INSNS
3450 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3451 #endif
3452 static inline int
3453 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3454 {
3455   enum machine_mode mode = GET_MODE (x);
3456
3457   switch (code)
3458     {
3459     case ASHIFT:
3460     case ASHIFTRT:
3461     case LSHIFTRT:
3462     case ROTATERT:      
3463     case PLUS:
3464     case MINUS:
3465     case COMPARE:
3466     case NEG:
3467     case NOT:   
3468       return COSTS_N_INSNS (1);
3469       
3470     case MULT:                                                  
3471       if (GET_CODE (XEXP (x, 1)) == CONST_INT)                  
3472         {                                                               
3473           int cycles = 0;                                               
3474           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3475           
3476           while (i)                                             
3477             {                                                   
3478               i >>= 2;                                          
3479               cycles++;                                         
3480             }                                                   
3481           return COSTS_N_INSNS (2) + cycles;                    
3482         }
3483       return COSTS_N_INSNS (1) + 16;
3484       
3485     case SET:                                                   
3486       return (COSTS_N_INSNS (1)                                 
3487               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)            
3488                      + GET_CODE (SET_DEST (x)) == MEM));
3489       
3490     case CONST_INT:                                             
3491       if (outer == SET)                                         
3492         {                                                       
3493           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)                
3494             return 0;                                           
3495           if (thumb_shiftable_const (INTVAL (x)))                       
3496             return COSTS_N_INSNS (2);                           
3497           return COSTS_N_INSNS (3);                             
3498         }                                                               
3499       else if ((outer == PLUS || outer == COMPARE)
3500                && INTVAL (x) < 256 && INTVAL (x) > -256)                
3501         return 0;
3502       else if (outer == AND
3503                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3504         return COSTS_N_INSNS (1);
3505       else if (outer == ASHIFT || outer == ASHIFTRT             
3506                || outer == LSHIFTRT)                            
3507         return 0;                                                       
3508       return COSTS_N_INSNS (2);
3509       
3510     case CONST:                                                 
3511     case CONST_DOUBLE:                                          
3512     case LABEL_REF:                                             
3513     case SYMBOL_REF:                                            
3514       return COSTS_N_INSNS (3);
3515       
3516     case UDIV:
3517     case UMOD:
3518     case DIV:
3519     case MOD:
3520       return 100;
3521
3522     case TRUNCATE:
3523       return 99;
3524
3525     case AND:
3526     case XOR:
3527     case IOR: 
3528       /* XXX guess.  */
3529       return 8;
3530
3531     case MEM:
3532       /* XXX another guess.  */
3533       /* Memory costs quite a lot for the first word, but subsequent words
3534          load at the equivalent of a single insn each.  */
3535       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3536               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3537                  ? 4 : 0));
3538
3539     case IF_THEN_ELSE:
3540       /* XXX a guess.  */
3541       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3542         return 14;
3543       return 2;
3544
3545     case ZERO_EXTEND:
3546       /* XXX still guessing.  */
3547       switch (GET_MODE (XEXP (x, 0)))
3548         {
3549         case QImode:
3550           return (1 + (mode == DImode ? 4 : 0)
3551                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3552           
3553         case HImode:
3554           return (4 + (mode == DImode ? 4 : 0)
3555                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3556           
3557         case SImode:
3558           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3559       
3560         default:
3561           return 99;
3562         }
3563       
3564     default:
3565       return 99;
3566     }
3567 }
3568
3569
3570 /* Worker routine for arm_rtx_costs.  */
3571 static inline int
3572 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3573 {
3574   enum machine_mode mode = GET_MODE (x);
3575   enum rtx_code subcode;
3576   int extra_cost;
3577
3578   switch (code)
3579     {
3580     case MEM:
3581       /* Memory costs quite a lot for the first word, but subsequent words
3582          load at the equivalent of a single insn each.  */
3583       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3584               + (GET_CODE (x) == SYMBOL_REF
3585                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3586
3587     case DIV:
3588     case MOD:
3589     case UDIV:
3590     case UMOD:
3591       return optimize_size ? COSTS_N_INSNS (2) : 100;
3592
3593     case ROTATE:
3594       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3595         return 4;
3596       /* Fall through */
3597     case ROTATERT:
3598       if (mode != SImode)
3599         return 8;
3600       /* Fall through */
3601     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3602       if (mode == DImode)
3603         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3604                 + ((GET_CODE (XEXP (x, 0)) == REG 
3605                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3606                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3607                    ? 0 : 8));
3608       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3609                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3610                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3611                    ? 0 : 4)
3612               + ((GET_CODE (XEXP (x, 1)) == REG
3613                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3614                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3615                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3616                  ? 0 : 4));
3617
3618     case MINUS:
3619       if (mode == DImode)
3620         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3621                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3622                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3623                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3624                    ? 0 : 8));
3625
3626       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3627         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3628                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3629                           && arm_const_double_rtx (XEXP (x, 1))))
3630                      ? 0 : 8)
3631                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3632                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3633                         && arm_const_double_rtx (XEXP (x, 0))))
3634                    ? 0 : 8));
3635
3636       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3637             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3638             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3639           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3640                || subcode == ASHIFTRT || subcode == LSHIFTRT
3641                || subcode == ROTATE || subcode == ROTATERT
3642                || (subcode == MULT
3643                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3644                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3645                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3646               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3647               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3648                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3649               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3650         return 1;
3651       /* Fall through */
3652
3653     case PLUS: 
3654       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3655         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3656                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3657                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3658                         && arm_const_double_rtx (XEXP (x, 1))))
3659                    ? 0 : 8));
3660
3661       /* Fall through */
3662     case AND: case XOR: case IOR: 
3663       extra_cost = 0;
3664
3665       /* Normally the frame registers will be spilt into reg+const during
3666          reload, so it is a bad idea to combine them with other instructions,
3667          since then they might not be moved outside of loops.  As a compromise
3668          we allow integration with ops that have a constant as their second
3669          operand.  */
3670       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3671            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3672            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3673           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3674               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3675         extra_cost = 4;
3676
3677       if (mode == DImode)
3678         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3679                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3680                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3681                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3682                    ? 0 : 8));
3683
3684       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3685         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3686                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3687                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3688                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3689                    ? 0 : 4));
3690
3691       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3692         return (1 + extra_cost
3693                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3694                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3695                      || subcode == ROTATE || subcode == ROTATERT
3696                      || (subcode == MULT
3697                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3698                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3699                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3700                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3701                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3702                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3703                    ? 0 : 4));
3704
3705       return 8;
3706
3707     case MULT:
3708       /* This should have been handled by the CPU specific routines.  */
3709       abort ();
3710
3711     case TRUNCATE:
3712       if (arm_arch3m && mode == SImode
3713           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3714           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3715           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3716               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3717           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3718               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3719         return 8;
3720       return 99;
3721
3722     case NEG:
3723       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3724         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3725       /* Fall through */
3726     case NOT:
3727       if (mode == DImode)
3728         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3729
3730       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3731
3732     case IF_THEN_ELSE:
3733       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3734         return 14;
3735       return 2;
3736
3737     case COMPARE:
3738       return 1;
3739
3740     case ABS:
3741       return 4 + (mode == DImode ? 4 : 0);
3742
3743     case SIGN_EXTEND:
3744       if (GET_MODE (XEXP (x, 0)) == QImode)
3745         return (4 + (mode == DImode ? 4 : 0)
3746                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3747       /* Fall through */
3748     case ZERO_EXTEND:
3749       switch (GET_MODE (XEXP (x, 0)))
3750         {
3751         case QImode:
3752           return (1 + (mode == DImode ? 4 : 0)
3753                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3754
3755         case HImode:
3756           return (4 + (mode == DImode ? 4 : 0)
3757                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3758
3759         case SImode:
3760           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3761
3762         case V8QImode:
3763         case V4HImode:
3764         case V2SImode:
3765         case V4QImode:
3766         case V2HImode:
3767             return 1;
3768
3769         default:
3770           break;
3771         }
3772       abort ();
3773
3774     case CONST_INT:                                             
3775       if (const_ok_for_arm (INTVAL (x)))                        
3776         return outer == SET ? 2 : -1;                   
3777       else if (outer == AND                             
3778                && const_ok_for_arm (~INTVAL (x)))               
3779         return -1;                                              
3780       else if ((outer == COMPARE                        
3781                 || outer == PLUS || outer == MINUS)     
3782                && const_ok_for_arm (-INTVAL (x)))               
3783         return -1;                                              
3784       else                                                      
3785         return 5;
3786       
3787     case CONST:                                                         
3788     case LABEL_REF:                                             
3789     case SYMBOL_REF:                                            
3790       return 6;
3791       
3792     case CONST_DOUBLE:                                          
3793       if (arm_const_double_rtx (x))
3794         return outer == SET ? 2 : -1;                   
3795       else if ((outer == COMPARE || outer == PLUS)      
3796                && neg_const_double_rtx_ok_for_fpa (x))          
3797         return -1;                                              
3798       return 7;
3799       
3800     default:
3801       return 99;
3802     }
3803 }
3804
3805 /* RTX costs for cores with a slow MUL implementation.  */
3806
3807 static bool
3808 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3809 {
3810   enum machine_mode mode = GET_MODE (x);
3811
3812   if (TARGET_THUMB)
3813     {
3814       *total = thumb_rtx_costs (x, code, outer_code);
3815       return true;
3816     }
3817   
3818   switch (code)
3819     {
3820     case MULT:
3821       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3822           || mode == DImode)
3823         {
3824           *total = 30;
3825           return true;
3826         }
3827
3828       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3829         {
3830           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3831                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3832           int cost, const_ok = const_ok_for_arm (i);
3833           int j, booth_unit_size;
3834
3835           /* Tune as appropriate.  */ 
3836           cost = const_ok ? 4 : 8;
3837           booth_unit_size = 2;
3838           for (j = 0; i && j < 32; j += booth_unit_size)
3839             {
3840               i >>= booth_unit_size;
3841               cost += 2;
3842             }
3843
3844           *total = cost;
3845           return true;
3846         }
3847
3848       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3849                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3850       return true;
3851   
3852     default:
3853       *total = arm_rtx_costs_1 (x, code, outer_code);
3854       return true;
3855     }
3856 }
3857
3858
3859 /* RTX cost for cores with a fast multiply unit (M variants).  */
3860
3861 static bool
3862 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3863 {
3864   enum machine_mode mode = GET_MODE (x);
3865
3866   if (TARGET_THUMB)
3867     {
3868       *total = thumb_rtx_costs (x, code, outer_code);
3869       return true;
3870     }
3871   
3872   switch (code)
3873     {
3874     case MULT:
3875       /* There is no point basing this on the tuning, since it is always the
3876          fast variant if it exists at all.  */
3877       if (mode == DImode
3878           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3879           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3880               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3881         {
3882           *total = 8;
3883           return true;
3884         }
3885       
3886
3887       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3888           || mode == DImode)
3889         {
3890           *total = 30;
3891           return true;
3892         }
3893
3894       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3895         {
3896           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3897                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3898           int cost, const_ok = const_ok_for_arm (i);
3899           int j, booth_unit_size;
3900
3901           /* Tune as appropriate.  */ 
3902           cost = const_ok ? 4 : 8;
3903           booth_unit_size = 8;
3904           for (j = 0; i && j < 32; j += booth_unit_size)
3905             {
3906               i >>= booth_unit_size;
3907               cost += 2;
3908             }
3909
3910           *total = cost;
3911           return true;
3912         }
3913
3914       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3915                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3916       return true;
3917   
3918     default:
3919       *total = arm_rtx_costs_1 (x, code, outer_code);
3920       return true;
3921     }
3922 }
3923
3924
3925 /* RTX cost for XScale CPUs.  */
3926
3927 static bool
3928 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3929 {
3930   enum machine_mode mode = GET_MODE (x);
3931
3932   if (TARGET_THUMB)
3933     {
3934       *total = thumb_rtx_costs (x, code, outer_code);
3935       return true;
3936     }
3937   
3938   switch (code)
3939     {
3940     case MULT:
3941       /* There is no point basing this on the tuning, since it is always the
3942          fast variant if it exists at all.  */
3943       if (mode == DImode
3944           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3945           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3946               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3947         {
3948           *total = 8;
3949           return true;
3950         }
3951       
3952
3953       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3954           || mode == DImode)
3955         {
3956           *total = 30;
3957           return true;
3958         }
3959
3960       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3961         {
3962           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3963                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3964           int cost, const_ok = const_ok_for_arm (i);
3965           unsigned HOST_WIDE_INT masked_const;
3966
3967           /* The cost will be related to two insns.
3968              First a load of the constant (MOV or LDR), then a multiply.  */
3969           cost = 2;
3970           if (! const_ok)
3971             cost += 1;      /* LDR is probably more expensive because
3972                                of longer result latency.  */
3973           masked_const = i & 0xffff8000;
3974           if (masked_const != 0 && masked_const != 0xffff8000)
3975             {
3976               masked_const = i & 0xf8000000;
3977               if (masked_const == 0 || masked_const == 0xf8000000)
3978                 cost += 1;
3979               else
3980                 cost += 2;
3981             }
3982           *total = cost;
3983           return true;
3984         }
3985
3986       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3987                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3988       return true;
3989   
3990     default:
3991       *total = arm_rtx_costs_1 (x, code, outer_code);
3992       return true;
3993     }
3994 }
3995
3996
3997 /* RTX costs for 9e (and later) cores.  */
3998
3999 static bool
4000 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4001 {
4002   enum machine_mode mode = GET_MODE (x);
4003   int nonreg_cost;
4004   int cost;
4005   
4006   if (TARGET_THUMB)
4007     {
4008       switch (code)
4009         {
4010         case MULT:
4011           *total = COSTS_N_INSNS (3);
4012           return true;
4013           
4014         default:
4015           *total = thumb_rtx_costs (x, code, outer_code);
4016           return true;
4017         }
4018     }
4019   
4020   switch (code)
4021     {
4022     case MULT:
4023       /* There is no point basing this on the tuning, since it is always the
4024          fast variant if it exists at all.  */
4025       if (mode == DImode
4026           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4027           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4028               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4029         {
4030           *total = 3;
4031           return true;
4032         }
4033       
4034
4035       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4036         {
4037           *total = 30;
4038           return true;
4039         }
4040       if (mode == DImode)
4041         {
4042           cost = 7;
4043           nonreg_cost = 8;
4044         }
4045       else
4046         {
4047           cost = 2;
4048           nonreg_cost = 4;
4049         }
4050
4051
4052       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4053                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4054       return true;
4055   
4056     default:
4057       *total = arm_rtx_costs_1 (x, code, outer_code);
4058       return true;
4059     }
4060 }
4061 /* All address computations that can be done are free, but rtx cost returns
4062    the same for practically all of them.  So we weight the different types
4063    of address here in the order (most pref first):
4064    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4065 static inline int
4066 arm_arm_address_cost (rtx x)
4067 {
4068   enum rtx_code c  = GET_CODE (x);
4069
4070   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4071     return 0;
4072   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4073     return 10;
4074
4075   if (c == PLUS || c == MINUS)
4076     {
4077       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4078         return 2;
4079
4080       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4081         return 3;
4082
4083       return 4;
4084     }
4085
4086   return 6;
4087 }
4088
4089 static inline int
4090 arm_thumb_address_cost (rtx x)
4091 {
4092   enum rtx_code c  = GET_CODE (x);
4093
4094   if (c == REG)
4095     return 1;
4096   if (c == PLUS
4097       && GET_CODE (XEXP (x, 0)) == REG
4098       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4099     return 1;
4100
4101   return 2;
4102 }
4103
4104 static int
4105 arm_address_cost (rtx x)
4106 {
4107   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4108 }
4109
4110 static int
4111 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4112 {
4113   rtx i_pat, d_pat;
4114
4115   /* Some true dependencies can have a higher cost depending
4116      on precisely how certain input operands are used.  */
4117   if (arm_tune_xscale
4118       && REG_NOTE_KIND (link) == 0
4119       && recog_memoized (insn) >= 0
4120       && recog_memoized (dep) >= 0)
4121     {
4122       int shift_opnum = get_attr_shift (insn);
4123       enum attr_type attr_type = get_attr_type (dep);
4124
4125       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4126          operand for INSN.  If we have a shifted input operand and the
4127          instruction we depend on is another ALU instruction, then we may
4128          have to account for an additional stall.  */
4129       if (shift_opnum != 0
4130           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4131         {
4132           rtx shifted_operand;
4133           int opno;
4134           
4135           /* Get the shifted operand.  */
4136           extract_insn (insn);
4137           shifted_operand = recog_data.operand[shift_opnum];
4138
4139           /* Iterate over all the operands in DEP.  If we write an operand
4140              that overlaps with SHIFTED_OPERAND, then we have increase the
4141              cost of this dependency.  */
4142           extract_insn (dep);
4143           preprocess_constraints ();
4144           for (opno = 0; opno < recog_data.n_operands; opno++)
4145             {
4146               /* We can ignore strict inputs.  */
4147               if (recog_data.operand_type[opno] == OP_IN)
4148                 continue;
4149
4150               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4151                                            shifted_operand))
4152                 return 2;
4153             }
4154         }
4155     }
4156
4157   /* XXX This is not strictly true for the FPA.  */
4158   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4159       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4160     return 0;
4161
4162   /* Call insns don't incur a stall, even if they follow a load.  */
4163   if (REG_NOTE_KIND (link) == 0
4164       && GET_CODE (insn) == CALL_INSN)
4165     return 1;
4166
4167   if ((i_pat = single_set (insn)) != NULL
4168       && GET_CODE (SET_SRC (i_pat)) == MEM
4169       && (d_pat = single_set (dep)) != NULL
4170       && GET_CODE (SET_DEST (d_pat)) == MEM)
4171     {
4172       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4173       /* This is a load after a store, there is no conflict if the load reads
4174          from a cached area.  Assume that loads from the stack, and from the
4175          constant pool are cached, and that others will miss.  This is a 
4176          hack.  */
4177       
4178       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4179           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4180           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4181           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4182         return 1;
4183     }
4184
4185   return cost;
4186 }
4187
4188 static int fp_consts_inited = 0;
4189
4190 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4191 static const char * const strings_fp[8] =
4192 {
4193   "0",   "1",   "2",   "3",
4194   "4",   "5",   "0.5", "10"
4195 };
4196
4197 static REAL_VALUE_TYPE values_fp[8];
4198
4199 static void
4200 init_fp_table (void)
4201 {
4202   int i;
4203   REAL_VALUE_TYPE r;
4204
4205   if (TARGET_VFP)
4206     fp_consts_inited = 1;
4207   else
4208     fp_consts_inited = 8;
4209
4210   for (i = 0; i < fp_consts_inited; i++)
4211     {
4212       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4213       values_fp[i] = r;
4214     }
4215 }
4216
4217 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4218 int
4219 arm_const_double_rtx (rtx x)
4220 {
4221   REAL_VALUE_TYPE r;
4222   int i;
4223   
4224   if (!fp_consts_inited)
4225     init_fp_table ();
4226   
4227   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4228   if (REAL_VALUE_MINUS_ZERO (r))
4229     return 0;
4230
4231   for (i = 0; i < fp_consts_inited; i++)
4232     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4233       return 1;
4234
4235   return 0;
4236 }
4237
4238 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4239 int
4240 neg_const_double_rtx_ok_for_fpa (rtx x)
4241 {
4242   REAL_VALUE_TYPE r;
4243   int i;
4244   
4245   if (!fp_consts_inited)
4246     init_fp_table ();
4247   
4248   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4249   r = REAL_VALUE_NEGATE (r);
4250   if (REAL_VALUE_MINUS_ZERO (r))
4251     return 0;
4252
4253   for (i = 0; i < 8; i++)
4254     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4255       return 1;
4256
4257   return 0;
4258 }
4259 \f
4260 /* Predicates for `match_operand' and `match_operator'.  */
4261
4262 /* s_register_operand is the same as register_operand, but it doesn't accept
4263    (SUBREG (MEM)...).
4264
4265    This function exists because at the time it was put in it led to better
4266    code.  SUBREG(MEM) always needs a reload in the places where
4267    s_register_operand is used, and this seemed to lead to excessive
4268    reloading.  */
4269 int
4270 s_register_operand (rtx op, enum machine_mode mode)
4271 {
4272   if (GET_MODE (op) != mode && mode != VOIDmode)
4273     return 0;
4274
4275   if (GET_CODE (op) == SUBREG)
4276     op = SUBREG_REG (op);
4277
4278   /* We don't consider registers whose class is NO_REGS
4279      to be a register operand.  */
4280   /* XXX might have to check for lo regs only for thumb ??? */
4281   return (GET_CODE (op) == REG
4282           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4283               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4284 }
4285
4286 /* A hard register operand (even before reload.  */
4287 int
4288 arm_hard_register_operand (rtx op, enum machine_mode mode)
4289 {
4290   if (GET_MODE (op) != mode && mode != VOIDmode)
4291     return 0;
4292
4293   return (GET_CODE (op) == REG
4294           && REGNO (op) < FIRST_PSEUDO_REGISTER);
4295 }
4296     
4297 /* An arm register operand.  */
4298 int
4299 arm_general_register_operand (rtx op, enum machine_mode mode)
4300 {
4301   if (GET_MODE (op) != mode && mode != VOIDmode)
4302     return 0;
4303
4304   if (GET_CODE (op) == SUBREG)
4305     op = SUBREG_REG (op);
4306
4307   return (GET_CODE (op) == REG
4308           && (REGNO (op) <= LAST_ARM_REGNUM
4309               || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4310 }
4311
4312 /* Only accept reg, subreg(reg), const_int.  */
4313 int
4314 reg_or_int_operand (rtx op, enum machine_mode mode)
4315 {
4316   if (GET_CODE (op) == CONST_INT)
4317     return 1;
4318
4319   if (GET_MODE (op) != mode && mode != VOIDmode)
4320     return 0;
4321
4322   if (GET_CODE (op) == SUBREG)
4323     op = SUBREG_REG (op);
4324
4325   /* We don't consider registers whose class is NO_REGS
4326      to be a register operand.  */
4327   return (GET_CODE (op) == REG
4328           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4329               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4330 }
4331
4332 /* Return 1 if OP is an item in memory, given that we are in reload.  */
4333 int
4334 arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4335 {
4336   int regno = true_regnum (op);
4337
4338   return (!CONSTANT_P (op)
4339           && (regno == -1
4340               || (GET_CODE (op) == REG
4341                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4342 }
4343
4344 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
4345 int
4346 arm_rhs_operand (rtx op, enum machine_mode mode)
4347 {
4348   return (s_register_operand (op, mode)
4349           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
4350 }
4351
4352 /* Return TRUE for valid operands for the
4353    rhs of an ARM instruction, or a load.  */
4354 int
4355 arm_rhsm_operand (rtx op, enum machine_mode mode)
4356 {
4357   return (s_register_operand (op, mode)
4358           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4359           || memory_operand (op, mode));
4360 }
4361
4362 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4363    constant that is valid when negated.  */
4364 int
4365 arm_add_operand (rtx op, enum machine_mode mode)
4366 {
4367   if (TARGET_THUMB)
4368     return thumb_cmp_operand (op, mode);
4369   
4370   return (s_register_operand (op, mode)
4371           || (GET_CODE (op) == CONST_INT
4372               && (const_ok_for_arm (INTVAL (op))
4373                   || const_ok_for_arm (-INTVAL (op)))));
4374 }
4375
4376 /* Return TRUE for valid ARM constants (or when valid if negated).  */
4377 int
4378 arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4379 {
4380   return (GET_CODE (op) == CONST_INT
4381           && (const_ok_for_arm (INTVAL (op))
4382               || const_ok_for_arm (-INTVAL (op))));
4383 }
4384
4385 int
4386 arm_not_operand (rtx op, enum machine_mode mode)
4387 {
4388   return (s_register_operand (op, mode)
4389           || (GET_CODE (op) == CONST_INT
4390               && (const_ok_for_arm (INTVAL (op))
4391                   || const_ok_for_arm (~INTVAL (op)))));
4392 }
4393
4394 /* Return TRUE if the operand is a memory reference which contains an
4395    offsettable address.  */
4396 int
4397 offsettable_memory_operand (rtx op, enum machine_mode mode)
4398 {
4399   if (mode == VOIDmode)
4400     mode = GET_MODE (op);
4401
4402   return (mode == GET_MODE (op)
4403           && GET_CODE (op) == MEM
4404           && offsettable_address_p (reload_completed | reload_in_progress,
4405                                     mode, XEXP (op, 0)));
4406 }
4407
4408 /* Return TRUE if the operand is a memory reference which is, or can be
4409    made word aligned by adjusting the offset.  */
4410 int
4411 alignable_memory_operand (rtx op, enum machine_mode mode)
4412 {
4413   rtx reg;
4414
4415   if (mode == VOIDmode)
4416     mode = GET_MODE (op);
4417
4418   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4419     return 0;
4420
4421   op = XEXP (op, 0);
4422
4423   return ((GET_CODE (reg = op) == REG
4424            || (GET_CODE (op) == SUBREG
4425                && GET_CODE (reg = SUBREG_REG (op)) == REG)
4426            || (GET_CODE (op) == PLUS
4427                && GET_CODE (XEXP (op, 1)) == CONST_INT
4428                && (GET_CODE (reg = XEXP (op, 0)) == REG
4429                    || (GET_CODE (XEXP (op, 0)) == SUBREG
4430                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
4431           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
4432 }
4433
4434 /* Similar to s_register_operand, but does not allow hard integer 
4435    registers.  */
4436 int
4437 f_register_operand (rtx op, enum machine_mode mode)
4438 {
4439   if (GET_MODE (op) != mode && mode != VOIDmode)
4440     return 0;
4441
4442   if (GET_CODE (op) == SUBREG)
4443     op = SUBREG_REG (op);
4444
4445   /* We don't consider registers whose class is NO_REGS
4446      to be a register operand.  */
4447   return (GET_CODE (op) == REG
4448           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4449               || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
4450 }
4451
4452 /* Return TRUE for valid operands for the rhs of an floating point insns.
4453    Allows regs or certain consts on FPA, just regs for everything else.  */
4454 int
4455 arm_float_rhs_operand (rtx op, enum machine_mode mode)
4456 {
4457   if (s_register_operand (op, mode))
4458     return TRUE;
4459
4460   if (GET_MODE (op) != mode && mode != VOIDmode)
4461     return FALSE;
4462
4463   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4464     return arm_const_double_rtx (op);
4465
4466   return FALSE;
4467 }
4468
4469 int
4470 arm_float_add_operand (rtx op, enum machine_mode mode)
4471 {
4472   if (s_register_operand (op, mode))
4473     return TRUE;
4474
4475   if (GET_MODE (op) != mode && mode != VOIDmode)
4476     return FALSE;
4477
4478   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4479     return (arm_const_double_rtx (op)
4480             || neg_const_double_rtx_ok_for_fpa (op));
4481
4482   return FALSE;
4483 }
4484
4485
4486 /* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4487    Depends which fpu we are targeting.  */
4488
4489 int
4490 arm_float_compare_operand (rtx op, enum machine_mode mode)
4491 {
4492   if (TARGET_VFP)
4493     return vfp_compare_operand (op, mode);
4494   else
4495     return arm_float_rhs_operand (op, mode);
4496 }
4497
4498
4499 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4500 int
4501 cirrus_memory_offset (rtx op)
4502 {
4503   /* Reject eliminable registers.  */
4504   if (! (reload_in_progress || reload_completed)
4505       && (   reg_mentioned_p (frame_pointer_rtx, op)
4506           || reg_mentioned_p (arg_pointer_rtx, op)
4507           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4508           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4509           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4510           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4511     return 0;
4512
4513   if (GET_CODE (op) == MEM)
4514     {
4515       rtx ind;
4516
4517       ind = XEXP (op, 0);
4518
4519       /* Match: (mem (reg)).  */
4520       if (GET_CODE (ind) == REG)
4521         return 1;
4522
4523       /* Match:
4524          (mem (plus (reg)
4525                     (const))).  */
4526       if (GET_CODE (ind) == PLUS
4527           && GET_CODE (XEXP (ind, 0)) == REG
4528           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4529           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4530         return 1;
4531     }
4532
4533   return 0;
4534 }
4535
4536 int
4537 arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4538 {
4539   if (!memory_operand (op, mode))
4540     return 0;
4541
4542   return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4543 }
4544
4545 /* Return nonzero if OP is a Cirrus or general register.  */
4546 int
4547 cirrus_register_operand (rtx op, enum machine_mode mode)
4548 {
4549   if (GET_MODE (op) != mode && mode != VOIDmode)
4550     return FALSE;
4551
4552   if (GET_CODE (op) == SUBREG)
4553     op = SUBREG_REG (op);
4554
4555   return (GET_CODE (op) == REG
4556           && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4557               || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4558 }
4559
4560 /* Return nonzero if OP is a cirrus FP register.  */
4561 int
4562 cirrus_fp_register (rtx op, enum machine_mode mode)
4563 {
4564   if (GET_MODE (op) != mode && mode != VOIDmode)
4565     return FALSE;
4566
4567   if (GET_CODE (op) == SUBREG)
4568     op = SUBREG_REG (op);
4569
4570   return (GET_CODE (op) == REG
4571           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4572               || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4573 }
4574
4575 /* Return nonzero if OP is a 6bit constant (0..63).  */
4576 int
4577 cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4578 {
4579   return (GET_CODE (op) == CONST_INT
4580           && INTVAL (op) >= 0
4581           && INTVAL (op) < 64);
4582 }
4583
4584
4585 /* Return TRUE if OP is a valid VFP memory address pattern.
4586    WB if true if writeback address modes are allowed.  */
4587
4588 int
4589 arm_coproc_mem_operand (rtx op, bool wb)
4590 {
4591   rtx ind;
4592
4593   /* Reject eliminable registers.  */
4594   if (! (reload_in_progress || reload_completed)
4595       && (   reg_mentioned_p (frame_pointer_rtx, op)
4596           || reg_mentioned_p (arg_pointer_rtx, op)
4597           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4598           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4599           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4600           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4601     return FALSE;
4602
4603   /* Constants are converted into offsets from labels.  */
4604   if (GET_CODE (op) != MEM)
4605     return FALSE;
4606
4607   ind = XEXP (op, 0);
4608
4609   if (reload_completed
4610       && (GET_CODE (ind) == LABEL_REF
4611           || (GET_CODE (ind) == CONST
4612               && GET_CODE (XEXP (ind, 0)) == PLUS
4613               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4614               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4615     return TRUE;
4616
4617   /* Match: (mem (reg)).  */
4618   if (GET_CODE (ind) == REG)
4619     return arm_address_register_rtx_p (ind, 0);
4620
4621   /* Autoincremment addressing modes.  */
4622   if (wb
4623       && (GET_CODE (ind) == PRE_INC
4624           || GET_CODE (ind) == POST_INC
4625           || GET_CODE (ind) == PRE_DEC
4626           || GET_CODE (ind) == POST_DEC))
4627     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4628
4629   if (wb
4630       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4631       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4632       && GET_CODE (XEXP (ind, 1)) == PLUS
4633       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4634     ind = XEXP (ind, 1);
4635
4636   /* Match:
4637      (plus (reg)
4638            (const)).  */
4639   if (GET_CODE (ind) == PLUS
4640       && GET_CODE (XEXP (ind, 0)) == REG
4641       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4642       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4643       && INTVAL (XEXP (ind, 1)) > -1024
4644       && INTVAL (XEXP (ind, 1)) <  1024
4645       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4646     return TRUE;
4647
4648   return FALSE;
4649 }
4650
4651
4652 /* Return TRUE if OP is a REG or constant zero.  */
4653 int
4654 vfp_compare_operand (rtx op, enum machine_mode mode)
4655 {
4656   if (s_register_operand (op, mode))
4657     return TRUE;
4658
4659   return (GET_CODE (op) == CONST_DOUBLE
4660           && arm_const_double_rtx (op));
4661 }
4662
4663
4664 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4665    VFP registers.  Otherwise return NO_REGS.  */
4666
4667 enum reg_class
4668 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4669 {
4670   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4671     return NO_REGS;
4672
4673   return GENERAL_REGS;
4674 }
4675
4676
4677 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4678    Use by the Cirrus Maverick code which has to workaround
4679    a hardware bug triggered by such instructions.  */
4680 static bool
4681 arm_memory_load_p (rtx insn)
4682 {
4683   rtx body, lhs, rhs;;
4684
4685   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4686     return false;
4687
4688   body = PATTERN (insn);
4689
4690   if (GET_CODE (body) != SET)
4691     return false;
4692
4693   lhs = XEXP (body, 0);
4694   rhs = XEXP (body, 1);
4695
4696   lhs = REG_OR_SUBREG_RTX (lhs);
4697
4698   /* If the destination is not a general purpose
4699      register we do not have to worry.  */
4700   if (GET_CODE (lhs) != REG
4701       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4702     return false;
4703
4704   /* As well as loads from memory we also have to react
4705      to loads of invalid constants which will be turned
4706      into loads from the minipool.  */
4707   return (GET_CODE (rhs) == MEM
4708           || GET_CODE (rhs) == SYMBOL_REF
4709           || note_invalid_constants (insn, -1, false));
4710 }
4711
4712 /* Return TRUE if INSN is a Cirrus instruction.  */
4713 static bool
4714 arm_cirrus_insn_p (rtx insn)
4715 {
4716   enum attr_cirrus attr;
4717
4718   /* get_attr aborts on USE and CLOBBER.  */
4719   if (!insn
4720       || GET_CODE (insn) != INSN
4721       || GET_CODE (PATTERN (insn)) == USE
4722       || GET_CODE (PATTERN (insn)) == CLOBBER)
4723     return 0;
4724
4725   attr = get_attr_cirrus (insn);
4726
4727   return attr != CIRRUS_NOT;
4728 }
4729
4730 /* Cirrus reorg for invalid instruction combinations.  */
4731 static void
4732 cirrus_reorg (rtx first)
4733 {
4734   enum attr_cirrus attr;
4735   rtx body = PATTERN (first);
4736   rtx t;
4737   int nops;
4738
4739   /* Any branch must be followed by 2 non Cirrus instructions.  */
4740   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4741     {
4742       nops = 0;
4743       t = next_nonnote_insn (first);
4744
4745       if (arm_cirrus_insn_p (t))
4746         ++ nops;
4747
4748       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4749         ++ nops;
4750
4751       while (nops --)
4752         emit_insn_after (gen_nop (), first);
4753
4754       return;
4755     }
4756
4757   /* (float (blah)) is in parallel with a clobber.  */
4758   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4759     body = XVECEXP (body, 0, 0);
4760
4761   if (GET_CODE (body) == SET)
4762     {
4763       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4764
4765       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4766          be followed by a non Cirrus insn.  */
4767       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4768         {
4769           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4770             emit_insn_after (gen_nop (), first);
4771
4772           return;
4773         }
4774       else if (arm_memory_load_p (first))
4775         {
4776           unsigned int arm_regno;
4777
4778           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4779              ldr/cfmv64hr combination where the Rd field is the same
4780              in both instructions must be split with a non Cirrus
4781              insn.  Example:
4782
4783              ldr r0, blah
4784              nop
4785              cfmvsr mvf0, r0.  */
4786
4787           /* Get Arm register number for ldr insn.  */
4788           if (GET_CODE (lhs) == REG)
4789             arm_regno = REGNO (lhs);
4790           else if (GET_CODE (rhs) == REG)
4791             arm_regno = REGNO (rhs);
4792           else
4793             abort ();
4794
4795           /* Next insn.  */
4796           first = next_nonnote_insn (first);
4797
4798           if (! arm_cirrus_insn_p (first))
4799             return;
4800
4801           body = PATTERN (first);
4802
4803           /* (float (blah)) is in parallel with a clobber.  */
4804           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4805             body = XVECEXP (body, 0, 0);
4806
4807           if (GET_CODE (body) == FLOAT)
4808             body = XEXP (body, 0);
4809
4810           if (get_attr_cirrus (first) == CIRRUS_MOVE
4811               && GET_CODE (XEXP (body, 1)) == REG
4812               && arm_regno == REGNO (XEXP (body, 1)))
4813             emit_insn_after (gen_nop (), first);
4814
4815           return;
4816         }
4817     }
4818
4819   /* get_attr aborts on USE and CLOBBER.  */
4820   if (!first
4821       || GET_CODE (first) != INSN
4822       || GET_CODE (PATTERN (first)) == USE
4823       || GET_CODE (PATTERN (first)) == CLOBBER)
4824     return;
4825
4826   attr = get_attr_cirrus (first);
4827
4828   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4829      must be followed by a non-coprocessor instruction.  */
4830   if (attr == CIRRUS_COMPARE)
4831     {
4832       nops = 0;
4833
4834       t = next_nonnote_insn (first);
4835
4836       if (arm_cirrus_insn_p (t))
4837         ++ nops;
4838
4839       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4840         ++ nops;
4841
4842       while (nops --)
4843         emit_insn_after (gen_nop (), first);
4844
4845       return;
4846     }
4847 }
4848
4849 /* Return nonzero if OP is a constant power of two.  */
4850 int
4851 power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4852 {
4853   if (GET_CODE (op) == CONST_INT)
4854     {
4855       HOST_WIDE_INT value = INTVAL (op);
4856
4857       return value != 0  &&  (value & (value - 1)) == 0;
4858     }
4859
4860   return FALSE;
4861 }
4862
4863 /* Return TRUE for a valid operand of a DImode operation.
4864    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4865    Note that this disallows MEM(REG+REG), but allows
4866    MEM(PRE/POST_INC/DEC(REG)).  */
4867 int
4868 di_operand (rtx op, enum machine_mode mode)
4869 {
4870   if (s_register_operand (op, mode))
4871     return TRUE;
4872
4873   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4874     return FALSE;
4875
4876   if (GET_CODE (op) == SUBREG)
4877     op = SUBREG_REG (op);
4878
4879   switch (GET_CODE (op))
4880     {
4881     case CONST_DOUBLE:
4882     case CONST_INT:
4883       return TRUE;
4884
4885     case MEM:
4886       return memory_address_p (DImode, XEXP (op, 0));
4887
4888     default:
4889       return FALSE;
4890     }
4891 }
4892
4893 /* Like di_operand, but don't accept constants.  */
4894 int
4895 nonimmediate_di_operand (rtx op, enum machine_mode mode)
4896 {
4897   if (s_register_operand (op, mode))
4898     return TRUE;
4899
4900   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4901     return FALSE;
4902
4903   if (GET_CODE (op) == SUBREG)
4904     op = SUBREG_REG (op);
4905
4906   if (GET_CODE (op) == MEM)
4907     return memory_address_p (DImode, XEXP (op, 0));
4908
4909   return FALSE;
4910 }
4911
4912 /* Return TRUE for a valid operand of a DFmode operation when soft-float.
4913    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4914    Note that this disallows MEM(REG+REG), but allows
4915    MEM(PRE/POST_INC/DEC(REG)).  */
4916 int
4917 soft_df_operand (rtx op, enum machine_mode mode)
4918 {
4919   if (s_register_operand (op, mode))
4920     return TRUE;
4921
4922   if (mode != VOIDmode && GET_MODE (op) != mode)
4923     return FALSE;
4924
4925   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4926     return FALSE;
4927   
4928   if (GET_CODE (op) == SUBREG)
4929     op = SUBREG_REG (op);
4930   
4931   switch (GET_CODE (op))
4932     {
4933     case CONST_DOUBLE:
4934       return TRUE;
4935
4936     case MEM:
4937       return memory_address_p (DFmode, XEXP (op, 0));
4938
4939     default:
4940       return FALSE;
4941     }
4942 }
4943
4944 /* Like soft_df_operand, but don't accept constants.  */
4945 int
4946 nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
4947 {
4948   if (s_register_operand (op, mode))
4949     return TRUE;
4950
4951   if (mode != VOIDmode && GET_MODE (op) != mode)
4952     return FALSE;
4953
4954   if (GET_CODE (op) == SUBREG)
4955     op = SUBREG_REG (op);
4956
4957   if (GET_CODE (op) == MEM)
4958     return memory_address_p (DFmode, XEXP (op, 0));
4959   return FALSE;
4960 }
4961
4962 /* Return TRUE for valid index operands.  */
4963 int
4964 index_operand (rtx op, enum machine_mode mode)
4965 {
4966   return (s_register_operand (op, mode)
4967           || (immediate_operand (op, mode)
4968               && (GET_CODE (op) != CONST_INT
4969                   || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4970 }
4971
4972 /* Return TRUE for valid shifts by a constant. This also accepts any
4973    power of two on the (somewhat overly relaxed) assumption that the
4974    shift operator in this case was a mult.  */
4975 int
4976 const_shift_operand (rtx op, enum machine_mode mode)
4977 {
4978   return (power_of_two_operand (op, mode)
4979           || (immediate_operand (op, mode)
4980               && (GET_CODE (op) != CONST_INT
4981                   || (INTVAL (op) < 32 && INTVAL (op) > 0))));
4982 }
4983
4984 /* Return TRUE for arithmetic operators which can be combined with a multiply
4985    (shift).  */
4986 int
4987 shiftable_operator (rtx x, enum machine_mode mode)
4988 {
4989   enum rtx_code code;
4990
4991   if (GET_MODE (x) != mode)
4992     return FALSE;
4993
4994   code = GET_CODE (x);
4995
4996   return (code == PLUS || code == MINUS
4997           || code == IOR || code == XOR || code == AND);
4998 }
4999
5000 /* Return TRUE for binary logical operators.  */
5001 int
5002 logical_binary_operator (rtx x, enum machine_mode mode)
5003 {
5004   enum rtx_code code;
5005
5006   if (GET_MODE (x) != mode)
5007     return FALSE;
5008
5009   code = GET_CODE (x);
5010
5011   return (code == IOR || code == XOR || code == AND);
5012 }
5013
5014 /* Return TRUE for shift operators.  */
5015 int
5016 shift_operator (rtx x,enum machine_mode mode)
5017 {
5018   enum rtx_code code;
5019
5020   if (GET_MODE (x) != mode)
5021     return FALSE;
5022
5023   code = GET_CODE (x);
5024
5025   if (code == MULT)
5026     return power_of_two_operand (XEXP (x, 1), mode);
5027
5028   return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5029           || code == ROTATERT);
5030 }
5031
5032 /* Return TRUE if x is EQ or NE.  */
5033 int
5034 equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
5035 {
5036   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
5037 }
5038
5039 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ.  */
5040 int
5041 arm_comparison_operator (rtx x, enum machine_mode mode)
5042 {
5043   return (comparison_operator (x, mode)
5044           && GET_CODE (x) != LTGT
5045           && GET_CODE (x) != UNEQ);
5046 }
5047
5048 /* Return TRUE for SMIN SMAX UMIN UMAX operators.  */
5049 int
5050 minmax_operator (rtx x, enum machine_mode mode)
5051 {
5052   enum rtx_code code = GET_CODE (x);
5053
5054   if (GET_MODE (x) != mode)
5055     return FALSE;
5056
5057   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
5058 }
5059
5060 /* Return TRUE if this is the condition code register, if we aren't given
5061    a mode, accept any class CCmode register.  */
5062 int
5063 cc_register (rtx x, enum machine_mode mode)
5064 {
5065   if (mode == VOIDmode)
5066     {
5067       mode = GET_MODE (x);
5068       
5069       if (GET_MODE_CLASS (mode) != MODE_CC)
5070         return FALSE;
5071     }
5072
5073   if (   GET_MODE (x) == mode
5074       && GET_CODE (x) == REG
5075       && REGNO    (x) == CC_REGNUM)
5076     return TRUE;
5077
5078   return FALSE;
5079 }
5080
5081 /* Return TRUE if this is the condition code register, if we aren't given
5082    a mode, accept any class CCmode register which indicates a dominance
5083    expression.  */
5084 int
5085 dominant_cc_register (rtx x, enum machine_mode mode)
5086 {
5087   if (mode == VOIDmode)
5088     {
5089       mode = GET_MODE (x);
5090       
5091       if (GET_MODE_CLASS (mode) != MODE_CC)
5092         return FALSE;
5093     }
5094
5095   if (mode != CC_DNEmode && mode != CC_DEQmode
5096       && mode != CC_DLEmode && mode != CC_DLTmode
5097       && mode != CC_DGEmode && mode != CC_DGTmode
5098       && mode != CC_DLEUmode && mode != CC_DLTUmode
5099       && mode != CC_DGEUmode && mode != CC_DGTUmode)
5100     return FALSE;
5101
5102   return cc_register (x, mode);
5103 }
5104
5105 /* Return TRUE if X references a SYMBOL_REF.  */
5106 int
5107 symbol_mentioned_p (rtx x)
5108 {
5109   const char * fmt;
5110   int i;
5111
5112   if (GET_CODE (x) == SYMBOL_REF)
5113     return 1;
5114
5115   fmt = GET_RTX_FORMAT (GET_CODE (x));
5116   
5117   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5118     {
5119       if (fmt[i] == 'E')
5120         {
5121           int j;
5122
5123           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5124             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5125               return 1;
5126         }
5127       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5128         return 1;
5129     }
5130
5131   return 0;
5132 }
5133
5134 /* Return TRUE if X references a LABEL_REF.  */
5135 int
5136 label_mentioned_p (rtx x)
5137 {
5138   const char * fmt;
5139   int i;
5140
5141   if (GET_CODE (x) == LABEL_REF)
5142     return 1;
5143
5144   fmt = GET_RTX_FORMAT (GET_CODE (x));
5145   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5146     {
5147       if (fmt[i] == 'E')
5148         {
5149           int j;
5150
5151           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5152             if (label_mentioned_p (XVECEXP (x, i, j)))
5153               return 1;
5154         }
5155       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5156         return 1;
5157     }
5158
5159   return 0;
5160 }
5161
5162 enum rtx_code
5163 minmax_code (rtx x)
5164 {
5165   enum rtx_code code = GET_CODE (x);
5166
5167   if (code == SMAX)
5168     return GE;
5169   else if (code == SMIN)
5170     return LE;
5171   else if (code == UMIN)
5172     return LEU;
5173   else if (code == UMAX)
5174     return GEU;
5175
5176   abort ();
5177 }
5178
5179 /* Return 1 if memory locations are adjacent.  */
5180 int
5181 adjacent_mem_locations (rtx a, rtx b)
5182 {
5183   if ((GET_CODE (XEXP (a, 0)) == REG
5184        || (GET_CODE (XEXP (a, 0)) == PLUS
5185            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5186       && (GET_CODE (XEXP (b, 0)) == REG
5187           || (GET_CODE (XEXP (b, 0)) == PLUS
5188               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5189     {
5190       int val0 = 0, val1 = 0;
5191       int reg0, reg1;
5192   
5193       if (GET_CODE (XEXP (a, 0)) == PLUS)
5194         {
5195           reg0 = REGNO  (XEXP (XEXP (a, 0), 0));
5196           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5197         }
5198       else
5199         reg0 = REGNO (XEXP (a, 0));
5200
5201       if (GET_CODE (XEXP (b, 0)) == PLUS)
5202         {
5203           reg1 = REGNO  (XEXP (XEXP (b, 0), 0));
5204           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5205         }
5206       else
5207         reg1 = REGNO (XEXP (b, 0));
5208
5209       /* Don't accept any offset that will require multiple
5210          instructions to handle, since this would cause the
5211          arith_adjacentmem pattern to output an overlong sequence.  */
5212       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5213         return 0;
5214       
5215       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5216     }
5217   return 0;
5218 }
5219
5220 /* Return 1 if OP is a load multiple operation.  It is known to be
5221    parallel and the first section will be tested.  */
5222 int
5223 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5224 {
5225   HOST_WIDE_INT count = XVECLEN (op, 0);
5226   int dest_regno;
5227   rtx src_addr;
5228   HOST_WIDE_INT i = 1, base = 0;
5229   rtx elt;
5230
5231   if (count <= 1
5232       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5233     return 0;
5234
5235   /* Check to see if this might be a write-back.  */
5236   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5237     {
5238       i++;
5239       base = 1;
5240
5241       /* Now check it more carefully.  */
5242       if (GET_CODE (SET_DEST (elt)) != REG
5243           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5244           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5245           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5246         return 0;
5247     }
5248
5249   /* Perform a quick check so we don't blow up below.  */
5250   if (count <= i
5251       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5252       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5253       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5254     return 0;
5255
5256   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5257   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5258
5259   for (; i < count; i++)
5260     {
5261       elt = XVECEXP (op, 0, i);
5262
5263       if (GET_CODE (elt) != SET
5264           || GET_CODE (SET_DEST (elt)) != REG
5265           || GET_MODE (SET_DEST (elt)) != SImode
5266           || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
5267           || GET_CODE (SET_SRC (elt)) != MEM
5268           || GET_MODE (SET_SRC (elt)) != SImode
5269           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5270           || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5271           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5272           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5273         return 0;
5274     }
5275
5276   return 1;
5277 }
5278
5279 /* Return 1 if OP is a store multiple operation.  It is known to be
5280    parallel and the first section will be tested.  */
5281 int
5282 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5283 {
5284   HOST_WIDE_INT count = XVECLEN (op, 0);
5285   int src_regno;
5286   rtx dest_addr;
5287   HOST_WIDE_INT i = 1, base = 0;
5288   rtx elt;
5289
5290   if (count <= 1
5291       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5292     return 0;
5293
5294   /* Check to see if this might be a write-back.  */
5295   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5296     {
5297       i++;
5298       base = 1;
5299
5300       /* Now check it more carefully.  */
5301       if (GET_CODE (SET_DEST (elt)) != REG
5302           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5303           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5304           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5305         return 0;
5306     }
5307
5308   /* Perform a quick check so we don't blow up below.  */
5309   if (count <= i
5310       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5311       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5312       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5313     return 0;
5314
5315   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5316   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5317
5318   for (; i < count; i++)
5319     {
5320       elt = XVECEXP (op, 0, i);
5321
5322       if (GET_CODE (elt) != SET
5323           || GET_CODE (SET_SRC (elt)) != REG
5324           || GET_MODE (SET_SRC (elt)) != SImode
5325           || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
5326           || GET_CODE (SET_DEST (elt)) != MEM
5327           || GET_MODE (SET_DEST (elt)) != SImode
5328           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5329           || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5330           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5331           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5332         return 0;
5333     }
5334
5335   return 1;
5336 }
5337
5338 int
5339 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5340                         HOST_WIDE_INT *load_offset)
5341 {
5342   int unsorted_regs[4];
5343   HOST_WIDE_INT unsorted_offsets[4];
5344   int order[4];
5345   int base_reg = -1;
5346   int i;
5347
5348   /* Can only handle 2, 3, or 4 insns at present,
5349      though could be easily extended if required.  */
5350   if (nops < 2 || nops > 4)
5351     abort ();
5352
5353   /* Loop over the operands and check that the memory references are
5354      suitable (ie immediate offsets from the same base register).  At
5355      the same time, extract the target register, and the memory
5356      offsets.  */
5357   for (i = 0; i < nops; i++)
5358     {
5359       rtx reg;
5360       rtx offset;
5361
5362       /* Convert a subreg of a mem into the mem itself.  */
5363       if (GET_CODE (operands[nops + i]) == SUBREG)
5364         operands[nops + i] = alter_subreg (operands + (nops + i));
5365
5366       if (GET_CODE (operands[nops + i]) != MEM)
5367         abort ();
5368
5369       /* Don't reorder volatile memory references; it doesn't seem worth
5370          looking for the case where the order is ok anyway.  */
5371       if (MEM_VOLATILE_P (operands[nops + i]))
5372         return 0;
5373
5374       offset = const0_rtx;
5375
5376       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5377            || (GET_CODE (reg) == SUBREG
5378                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5379           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5380               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5381                    == REG)
5382                   || (GET_CODE (reg) == SUBREG
5383                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5384               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5385                   == CONST_INT)))
5386         {
5387           if (i == 0)
5388             {
5389               base_reg = REGNO (reg);
5390               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5391                                   ? REGNO (operands[i])
5392                                   : REGNO (SUBREG_REG (operands[i])));
5393               order[0] = 0;
5394             }
5395           else 
5396             {
5397               if (base_reg != (int) REGNO (reg))
5398                 /* Not addressed from the same base register.  */
5399                 return 0;
5400
5401               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5402                                   ? REGNO (operands[i])
5403                                   : REGNO (SUBREG_REG (operands[i])));
5404               if (unsorted_regs[i] < unsorted_regs[order[0]])
5405                 order[0] = i;
5406             }
5407
5408           /* If it isn't an integer register, or if it overwrites the
5409              base register but isn't the last insn in the list, then
5410              we can't do this.  */
5411           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5412               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5413             return 0;
5414
5415           unsorted_offsets[i] = INTVAL (offset);
5416         }
5417       else
5418         /* Not a suitable memory address.  */
5419         return 0;
5420     }
5421
5422   /* All the useful information has now been extracted from the
5423      operands into unsorted_regs and unsorted_offsets; additionally,
5424      order[0] has been set to the lowest numbered register in the
5425      list.  Sort the registers into order, and check that the memory
5426      offsets are ascending and adjacent.  */
5427
5428   for (i = 1; i < nops; i++)
5429     {
5430       int j;
5431
5432       order[i] = order[i - 1];
5433       for (j = 0; j < nops; j++)
5434         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5435             && (order[i] == order[i - 1]
5436                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5437           order[i] = j;
5438
5439       /* Have we found a suitable register? if not, one must be used more
5440          than once.  */
5441       if (order[i] == order[i - 1])
5442         return 0;
5443
5444       /* Is the memory address adjacent and ascending? */
5445       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5446         return 0;
5447     }
5448
5449   if (base)
5450     {
5451       *base = base_reg;
5452
5453       for (i = 0; i < nops; i++)
5454         regs[i] = unsorted_regs[order[i]];
5455
5456       *load_offset = unsorted_offsets[order[0]];
5457     }
5458
5459   if (unsorted_offsets[order[0]] == 0)
5460     return 1; /* ldmia */
5461
5462   if (unsorted_offsets[order[0]] == 4)
5463     return 2; /* ldmib */
5464
5465   if (unsorted_offsets[order[nops - 1]] == 0)
5466     return 3; /* ldmda */
5467
5468   if (unsorted_offsets[order[nops - 1]] == -4)
5469     return 4; /* ldmdb */
5470
5471   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5472      if the offset isn't small enough.  The reason 2 ldrs are faster
5473      is because these ARMs are able to do more than one cache access
5474      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5475      whilst the ARM8 has a double bandwidth cache.  This means that
5476      these cores can do both an instruction fetch and a data fetch in
5477      a single cycle, so the trick of calculating the address into a
5478      scratch register (one of the result regs) and then doing a load
5479      multiple actually becomes slower (and no smaller in code size).
5480      That is the transformation
5481  
5482         ldr     rd1, [rbase + offset]
5483         ldr     rd2, [rbase + offset + 4]
5484  
5485      to
5486  
5487         add     rd1, rbase, offset
5488         ldmia   rd1, {rd1, rd2}
5489  
5490      produces worse code -- '3 cycles + any stalls on rd2' instead of
5491      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5492      access per cycle, the first sequence could never complete in less
5493      than 6 cycles, whereas the ldm sequence would only take 5 and
5494      would make better use of sequential accesses if not hitting the
5495      cache.
5496
5497      We cheat here and test 'arm_ld_sched' which we currently know to
5498      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5499      changes, then the test below needs to be reworked.  */
5500   if (nops == 2 && arm_ld_sched)
5501     return 0;
5502
5503   /* Can't do it without setting up the offset, only do this if it takes
5504      no more than one insn.  */
5505   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
5506           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5507 }
5508
5509 const char *
5510 emit_ldm_seq (rtx *operands, int nops)
5511 {
5512   int regs[4];
5513   int base_reg;
5514   HOST_WIDE_INT offset;
5515   char buf[100];
5516   int i;
5517
5518   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5519     {
5520     case 1:
5521       strcpy (buf, "ldm%?ia\t");
5522       break;
5523
5524     case 2:
5525       strcpy (buf, "ldm%?ib\t");
5526       break;
5527
5528     case 3:
5529       strcpy (buf, "ldm%?da\t");
5530       break;
5531
5532     case 4:
5533       strcpy (buf, "ldm%?db\t");
5534       break;
5535
5536     case 5:
5537       if (offset >= 0)
5538         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5539                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5540                  (long) offset);
5541       else
5542         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5543                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5544                  (long) -offset);
5545       output_asm_insn (buf, operands);
5546       base_reg = regs[0];
5547       strcpy (buf, "ldm%?ia\t");
5548       break;
5549
5550     default:
5551       abort ();
5552     }
5553
5554   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5555            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5556
5557   for (i = 1; i < nops; i++)
5558     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5559              reg_names[regs[i]]);
5560
5561   strcat (buf, "}\t%@ phole ldm");
5562
5563   output_asm_insn (buf, operands);
5564   return "";
5565 }
5566
5567 int
5568 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5569                          HOST_WIDE_INT * load_offset)
5570 {
5571   int unsorted_regs[4];
5572   HOST_WIDE_INT unsorted_offsets[4];
5573   int order[4];
5574   int base_reg = -1;
5575   int i;
5576
5577   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5578      extended if required.  */
5579   if (nops < 2 || nops > 4)
5580     abort ();
5581
5582   /* Loop over the operands and check that the memory references are
5583      suitable (ie immediate offsets from the same base register).  At
5584      the same time, extract the target register, and the memory
5585      offsets.  */
5586   for (i = 0; i < nops; i++)
5587     {
5588       rtx reg;
5589       rtx offset;
5590
5591       /* Convert a subreg of a mem into the mem itself.  */
5592       if (GET_CODE (operands[nops + i]) == SUBREG)
5593         operands[nops + i] = alter_subreg (operands + (nops + i));
5594
5595       if (GET_CODE (operands[nops + i]) != MEM)
5596         abort ();
5597
5598       /* Don't reorder volatile memory references; it doesn't seem worth
5599          looking for the case where the order is ok anyway.  */
5600       if (MEM_VOLATILE_P (operands[nops + i]))
5601         return 0;
5602
5603       offset = const0_rtx;
5604
5605       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5606            || (GET_CODE (reg) == SUBREG
5607                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5608           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5609               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5610                    == REG)
5611                   || (GET_CODE (reg) == SUBREG
5612                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5613               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5614                   == CONST_INT)))
5615         {
5616           if (i == 0)
5617             {
5618               base_reg = REGNO (reg);
5619               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5620                                   ? REGNO (operands[i])
5621                                   : REGNO (SUBREG_REG (operands[i])));
5622               order[0] = 0;
5623             }
5624           else 
5625             {
5626               if (base_reg != (int) REGNO (reg))
5627                 /* Not addressed from the same base register.  */
5628                 return 0;
5629
5630               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5631                                   ? REGNO (operands[i])
5632                                   : REGNO (SUBREG_REG (operands[i])));
5633               if (unsorted_regs[i] < unsorted_regs[order[0]])
5634                 order[0] = i;
5635             }
5636
5637           /* If it isn't an integer register, then we can't do this.  */
5638           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5639             return 0;
5640
5641           unsorted_offsets[i] = INTVAL (offset);
5642         }
5643       else
5644         /* Not a suitable memory address.  */
5645         return 0;
5646     }
5647
5648   /* All the useful information has now been extracted from the
5649      operands into unsorted_regs and unsorted_offsets; additionally,
5650      order[0] has been set to the lowest numbered register in the
5651      list.  Sort the registers into order, and check that the memory
5652      offsets are ascending and adjacent.  */
5653
5654   for (i = 1; i < nops; i++)
5655     {
5656       int j;
5657
5658       order[i] = order[i - 1];
5659       for (j = 0; j < nops; j++)
5660         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5661             && (order[i] == order[i - 1]
5662                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5663           order[i] = j;
5664
5665       /* Have we found a suitable register? if not, one must be used more
5666          than once.  */
5667       if (order[i] == order[i - 1])
5668         return 0;
5669
5670       /* Is the memory address adjacent and ascending? */
5671       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5672         return 0;
5673     }
5674
5675   if (base)
5676     {
5677       *base = base_reg;
5678
5679       for (i = 0; i < nops; i++)
5680         regs[i] = unsorted_regs[order[i]];
5681
5682       *load_offset = unsorted_offsets[order[0]];
5683     }
5684
5685   if (unsorted_offsets[order[0]] == 0)
5686     return 1; /* stmia */
5687
5688   if (unsorted_offsets[order[0]] == 4)
5689     return 2; /* stmib */
5690
5691   if (unsorted_offsets[order[nops - 1]] == 0)
5692     return 3; /* stmda */
5693
5694   if (unsorted_offsets[order[nops - 1]] == -4)
5695     return 4; /* stmdb */
5696
5697   return 0;
5698 }
5699
5700 const char *
5701 emit_stm_seq (rtx *operands, int nops)
5702 {
5703   int regs[4];
5704   int base_reg;
5705   HOST_WIDE_INT offset;
5706   char buf[100];
5707   int i;
5708
5709   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5710     {
5711     case 1:
5712       strcpy (buf, "stm%?ia\t");
5713       break;
5714
5715     case 2:
5716       strcpy (buf, "stm%?ib\t");
5717       break;
5718
5719     case 3:
5720       strcpy (buf, "stm%?da\t");
5721       break;
5722
5723     case 4:
5724       strcpy (buf, "stm%?db\t");
5725       break;
5726
5727     default:
5728       abort ();
5729     }
5730
5731   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5732            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5733
5734   for (i = 1; i < nops; i++)
5735     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5736              reg_names[regs[i]]);
5737
5738   strcat (buf, "}\t%@ phole stm");
5739
5740   output_asm_insn (buf, operands);
5741   return "";
5742 }
5743
5744 int
5745 multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5746 {
5747   if (GET_CODE (op) != PARALLEL
5748       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5749       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5750       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5751     return 0;
5752
5753   return 1;
5754 }
5755 \f
5756 /* Routines for use in generating RTL.  */
5757
5758 rtx
5759 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5760                        int write_back, int unchanging_p, int in_struct_p,
5761                        int scalar_p)
5762 {
5763   int i = 0, j;
5764   rtx result;
5765   int sign = up ? 1 : -1;
5766   rtx mem;
5767
5768   /* XScale has load-store double instructions, but they have stricter
5769      alignment requirements than load-store multiple, so we can not
5770      use them.
5771
5772      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5773      the pipeline until completion.
5774
5775         NREGS           CYCLES
5776           1               3
5777           2               4
5778           3               5
5779           4               6
5780
5781      An ldr instruction takes 1-3 cycles, but does not block the
5782      pipeline.
5783
5784         NREGS           CYCLES
5785           1              1-3
5786           2              2-6
5787           3              3-9
5788           4              4-12
5789
5790      Best case ldr will always win.  However, the more ldr instructions
5791      we issue, the less likely we are to be able to schedule them well.
5792      Using ldr instructions also increases code size.
5793
5794      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5795      for counts of 3 or 4 regs.  */
5796   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5797     {
5798       rtx seq;
5799       
5800       start_sequence ();
5801       
5802       for (i = 0; i < count; i++)
5803         {
5804           mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5805           RTX_UNCHANGING_P (mem) = unchanging_p;
5806           MEM_IN_STRUCT_P (mem) = in_struct_p;
5807           MEM_SCALAR_P (mem) = scalar_p;
5808           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5809         }
5810
5811       if (write_back)
5812         emit_move_insn (from, plus_constant (from, count * 4 * sign));
5813
5814       seq = get_insns ();
5815       end_sequence ();
5816       
5817       return seq;
5818     }
5819
5820   result = gen_rtx_PARALLEL (VOIDmode,
5821                              rtvec_alloc (count + (write_back ? 1 : 0)));
5822   if (write_back)
5823     {
5824       XVECEXP (result, 0, 0)
5825         = gen_rtx_SET (GET_MODE (from), from,
5826                        plus_constant (from, count * 4 * sign));
5827       i = 1;
5828       count++;
5829     }
5830
5831   for (j = 0; i < count; i++, j++)
5832     {
5833       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5834       RTX_UNCHANGING_P (mem) = unchanging_p;
5835       MEM_IN_STRUCT_P (mem) = in_struct_p;
5836       MEM_SCALAR_P (mem) = scalar_p;
5837       XVECEXP (result, 0, i)
5838         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5839     }
5840
5841   return result;
5842 }
5843
5844 rtx
5845 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5846                         int write_back, int unchanging_p, int in_struct_p,
5847                         int scalar_p)
5848 {
5849   int i = 0, j;
5850   rtx result;
5851   int sign = up ? 1 : -1;
5852   rtx mem;
5853
5854   /* See arm_gen_load_multiple for discussion of
5855      the pros/cons of ldm/stm usage for XScale.  */
5856   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5857     {
5858       rtx seq;
5859       
5860       start_sequence ();
5861       
5862       for (i = 0; i < count; i++)
5863         {
5864           mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5865           RTX_UNCHANGING_P (mem) = unchanging_p;
5866           MEM_IN_STRUCT_P (mem) = in_struct_p;
5867           MEM_SCALAR_P (mem) = scalar_p;
5868           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5869         }
5870
5871       if (write_back)
5872         emit_move_insn (to, plus_constant (to, count * 4 * sign));
5873
5874       seq = get_insns ();
5875       end_sequence ();
5876       
5877       return seq;
5878     }
5879
5880   result = gen_rtx_PARALLEL (VOIDmode,
5881                              rtvec_alloc (count + (write_back ? 1 : 0)));
5882   if (write_back)
5883     {
5884       XVECEXP (result, 0, 0)
5885         = gen_rtx_SET (GET_MODE (to), to,
5886                        plus_constant (to, count * 4 * sign));
5887       i = 1;
5888       count++;
5889     }
5890
5891   for (j = 0; i < count; i++, j++)
5892     {
5893       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5894       RTX_UNCHANGING_P (mem) = unchanging_p;
5895       MEM_IN_STRUCT_P (mem) = in_struct_p;
5896       MEM_SCALAR_P (mem) = scalar_p;
5897
5898       XVECEXP (result, 0, i)
5899         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5900     }
5901
5902   return result;
5903 }
5904
5905 int
5906 arm_gen_movmemqi (rtx *operands)
5907 {
5908   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5909   int i;
5910   rtx src, dst;
5911   rtx st_src, st_dst, fin_src, fin_dst;
5912   rtx part_bytes_reg = NULL;
5913   rtx mem;
5914   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5915   int dst_scalar_p, src_scalar_p;
5916
5917   if (GET_CODE (operands[2]) != CONST_INT
5918       || GET_CODE (operands[3]) != CONST_INT
5919       || INTVAL (operands[2]) > 64
5920       || INTVAL (operands[3]) & 3)
5921     return 0;
5922
5923   st_dst = XEXP (operands[0], 0);
5924   st_src = XEXP (operands[1], 0);
5925
5926   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5927   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5928   dst_scalar_p = MEM_SCALAR_P (operands[0]);
5929   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5930   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5931   src_scalar_p = MEM_SCALAR_P (operands[1]);
5932
5933   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5934   fin_src = src = copy_to_mode_reg (SImode, st_src);
5935
5936   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5937   out_words_to_go = INTVAL (operands[2]) / 4;
5938   last_bytes = INTVAL (operands[2]) & 3;
5939
5940   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5941     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5942
5943   for (i = 0; in_words_to_go >= 2; i+=4)
5944     {
5945       if (in_words_to_go > 4)
5946         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5947                                           src_unchanging_p,
5948                                           src_in_struct_p,
5949                                           src_scalar_p));
5950       else
5951         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
5952                                           FALSE, src_unchanging_p,
5953                                           src_in_struct_p, src_scalar_p));
5954
5955       if (out_words_to_go)
5956         {
5957           if (out_words_to_go > 4)
5958             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5959                                                dst_unchanging_p,
5960                                                dst_in_struct_p,
5961                                                dst_scalar_p));
5962           else if (out_words_to_go != 1)
5963             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5964                                                dst, TRUE, 
5965                                                (last_bytes == 0
5966                                                 ? FALSE : TRUE),
5967                                                dst_unchanging_p,
5968                                                dst_in_struct_p,
5969                                                dst_scalar_p));
5970           else
5971             {
5972               mem = gen_rtx_MEM (SImode, dst);
5973               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5974               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5975               MEM_SCALAR_P (mem) = dst_scalar_p;
5976               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5977               if (last_bytes != 0)
5978                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5979             }
5980         }
5981
5982       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5983       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5984     }
5985
5986   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5987   if (out_words_to_go)
5988     {
5989       rtx sreg;
5990       
5991       mem = gen_rtx_MEM (SImode, src);
5992       RTX_UNCHANGING_P (mem) = src_unchanging_p;
5993       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
5994       MEM_SCALAR_P (mem) = src_scalar_p;
5995       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
5996       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
5997       
5998       mem = gen_rtx_MEM (SImode, dst);
5999       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6000       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6001       MEM_SCALAR_P (mem) = dst_scalar_p;
6002       emit_move_insn (mem, sreg);
6003       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6004       in_words_to_go--;
6005       
6006       if (in_words_to_go)       /* Sanity check */
6007         abort ();
6008     }
6009
6010   if (in_words_to_go)
6011     {
6012       if (in_words_to_go < 0)
6013         abort ();
6014
6015       mem = gen_rtx_MEM (SImode, src);
6016       RTX_UNCHANGING_P (mem) = src_unchanging_p;
6017       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6018       MEM_SCALAR_P (mem) = src_scalar_p;
6019       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6020     }
6021
6022   if (last_bytes && part_bytes_reg == NULL)
6023     abort ();
6024
6025   if (BYTES_BIG_ENDIAN && last_bytes)
6026     {
6027       rtx tmp = gen_reg_rtx (SImode);
6028
6029       /* The bytes we want are in the top end of the word.  */
6030       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6031                               GEN_INT (8 * (4 - last_bytes))));
6032       part_bytes_reg = tmp;
6033       
6034       while (last_bytes)
6035         {
6036           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
6037           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6038           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6039           MEM_SCALAR_P (mem) = dst_scalar_p;
6040           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6041
6042           if (--last_bytes)
6043             {
6044               tmp = gen_reg_rtx (SImode);
6045               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6046               part_bytes_reg = tmp;
6047             }
6048         }
6049           
6050     }
6051   else
6052     {
6053       if (last_bytes > 1)
6054         {
6055           mem = gen_rtx_MEM (HImode, dst);
6056           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6057           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6058           MEM_SCALAR_P (mem) = dst_scalar_p;
6059           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6060           last_bytes -= 2;
6061           if (last_bytes)
6062             {
6063               rtx tmp = gen_reg_rtx (SImode);
6064
6065               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6066               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6067               part_bytes_reg = tmp;
6068             }
6069         }
6070       
6071       if (last_bytes)
6072         {
6073           mem = gen_rtx_MEM (QImode, dst);
6074           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6075           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6076           MEM_SCALAR_P (mem) = dst_scalar_p;
6077           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6078         }
6079     }
6080
6081   return 1;
6082 }
6083
6084 /* Generate a memory reference for a half word, such that it will be loaded
6085    into the top 16 bits of the word.  We can assume that the address is
6086    known to be alignable and of the form reg, or plus (reg, const).  */
6087
6088 rtx
6089 arm_gen_rotated_half_load (rtx memref)
6090 {
6091   HOST_WIDE_INT offset = 0;
6092   rtx base = XEXP (memref, 0);
6093
6094   if (GET_CODE (base) == PLUS)
6095     {
6096       offset = INTVAL (XEXP (base, 1));
6097       base = XEXP (base, 0);
6098     }
6099
6100   /* If we aren't allowed to generate unaligned addresses, then fail.  */
6101   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
6102     return NULL;
6103
6104   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6105
6106   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6107     return base;
6108
6109   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6110 }
6111
6112 /* Select a dominance comparison mode if possible for a test of the general
6113    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6114    COND_OR == DOM_CC_X_AND_Y => (X && Y) 
6115    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6116    COND_OR == DOM_CC_X_OR_Y => (X || Y) 
6117    In all cases OP will be either EQ or NE, but we don't need to know which
6118    here.  If we are unable to support a dominance comparison we return 
6119    CC mode.  This will then fail to match for the RTL expressions that
6120    generate this call.  */
6121 enum machine_mode
6122 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6123 {
6124   enum rtx_code cond1, cond2;
6125   int swapped = 0;
6126
6127   /* Currently we will probably get the wrong result if the individual
6128      comparisons are not simple.  This also ensures that it is safe to
6129      reverse a comparison if necessary.  */
6130   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6131        != CCmode)
6132       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6133           != CCmode))
6134     return CCmode;
6135
6136   /* The if_then_else variant of this tests the second condition if the
6137      first passes, but is true if the first fails.  Reverse the first
6138      condition to get a true "inclusive-or" expression.  */
6139   if (cond_or == DOM_CC_NX_OR_Y)
6140     cond1 = reverse_condition (cond1);
6141
6142   /* If the comparisons are not equal, and one doesn't dominate the other,
6143      then we can't do this.  */
6144   if (cond1 != cond2 
6145       && !comparison_dominates_p (cond1, cond2)
6146       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6147     return CCmode;
6148
6149   if (swapped)
6150     {
6151       enum rtx_code temp = cond1;
6152       cond1 = cond2;
6153       cond2 = temp;
6154     }
6155
6156   switch (cond1)
6157     {
6158     case EQ:
6159       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6160         return CC_DEQmode;
6161
6162       switch (cond2)
6163         {
6164         case LE: return CC_DLEmode;
6165         case LEU: return CC_DLEUmode;
6166         case GE: return CC_DGEmode;
6167         case GEU: return CC_DGEUmode;
6168         default: break;
6169         }
6170
6171       break;
6172
6173     case LT:
6174       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6175         return CC_DLTmode;
6176       if (cond2 == LE)
6177         return CC_DLEmode;
6178       if (cond2 == NE)
6179         return CC_DNEmode;
6180       break;
6181
6182     case GT:
6183       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6184         return CC_DGTmode;
6185       if (cond2 == GE)
6186         return CC_DGEmode;
6187       if (cond2 == NE)
6188         return CC_DNEmode;
6189       break;
6190       
6191     case LTU:
6192       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6193         return CC_DLTUmode;
6194       if (cond2 == LEU)
6195         return CC_DLEUmode;
6196       if (cond2 == NE)
6197         return CC_DNEmode;
6198       break;
6199
6200     case GTU:
6201       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6202         return CC_DGTUmode;
6203       if (cond2 == GEU)
6204         return CC_DGEUmode;
6205       if (cond2 == NE)
6206         return CC_DNEmode;
6207       break;
6208
6209     /* The remaining cases only occur when both comparisons are the
6210        same.  */
6211     case NE:
6212       return CC_DNEmode;
6213
6214     case LE:
6215       return CC_DLEmode;
6216
6217     case GE:
6218       return CC_DGEmode;
6219
6220     case LEU:
6221       return CC_DLEUmode;
6222
6223     case GEU:
6224       return CC_DGEUmode;
6225
6226     default:
6227       break;
6228     }
6229
6230   abort ();
6231 }
6232
6233 enum machine_mode
6234 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6235 {
6236   /* All floating point compares return CCFP if it is an equality
6237      comparison, and CCFPE otherwise.  */
6238   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6239     {
6240       switch (op)
6241         {
6242         case EQ:
6243         case NE:
6244         case UNORDERED:
6245         case ORDERED:
6246         case UNLT:
6247         case UNLE:
6248         case UNGT:
6249         case UNGE:
6250         case UNEQ:
6251         case LTGT:
6252           return CCFPmode;
6253
6254         case LT:
6255         case LE:
6256         case GT:
6257         case GE:
6258           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6259             return CCFPmode;
6260           return CCFPEmode;
6261
6262         default:
6263           abort ();
6264         }
6265     }
6266   
6267   /* A compare with a shifted operand.  Because of canonicalization, the
6268      comparison will have to be swapped when we emit the assembler.  */
6269   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6270       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6271           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6272           || GET_CODE (x) == ROTATERT))
6273     return CC_SWPmode;
6274
6275   /* This is a special case that is used by combine to allow a 
6276      comparison of a shifted byte load to be split into a zero-extend
6277      followed by a comparison of the shifted integer (only valid for
6278      equalities and unsigned inequalities).  */
6279   if (GET_MODE (x) == SImode
6280       && GET_CODE (x) == ASHIFT
6281       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6282       && GET_CODE (XEXP (x, 0)) == SUBREG
6283       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6284       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6285       && (op == EQ || op == NE
6286           || op == GEU || op == GTU || op == LTU || op == LEU)
6287       && GET_CODE (y) == CONST_INT)
6288     return CC_Zmode;
6289
6290   /* A construct for a conditional compare, if the false arm contains
6291      0, then both conditions must be true, otherwise either condition
6292      must be true.  Not all conditions are possible, so CCmode is
6293      returned if it can't be done.  */
6294   if (GET_CODE (x) == IF_THEN_ELSE
6295       && (XEXP (x, 2) == const0_rtx
6296           || XEXP (x, 2) == const1_rtx)
6297       && COMPARISON_P (XEXP (x, 0))
6298       && COMPARISON_P (XEXP (x, 1)))
6299     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
6300                                          INTVAL (XEXP (x, 2)));
6301
6302   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6303   if (GET_CODE (x) == AND
6304       && COMPARISON_P (XEXP (x, 0))
6305       && COMPARISON_P (XEXP (x, 1)))
6306     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6307                                          DOM_CC_X_AND_Y);
6308
6309   if (GET_CODE (x) == IOR
6310       && COMPARISON_P (XEXP (x, 0))
6311       && COMPARISON_P (XEXP (x, 1)))
6312     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6313                                          DOM_CC_X_OR_Y);
6314
6315   /* An operation (on Thumb) where we want to test for a single bit.
6316      This is done by shifting that bit up into the top bit of a
6317      scratch register; we can then branch on the sign bit.  */
6318   if (TARGET_THUMB
6319       && GET_MODE (x) == SImode
6320       && (op == EQ || op == NE)
6321       && (GET_CODE (x) == ZERO_EXTRACT))
6322     return CC_Nmode;
6323
6324   /* An operation that sets the condition codes as a side-effect, the
6325      V flag is not set correctly, so we can only use comparisons where
6326      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6327      instead.)  */
6328   if (GET_MODE (x) == SImode
6329       && y == const0_rtx
6330       && (op == EQ || op == NE || op == LT || op == GE)
6331       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6332           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6333           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6334           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6335           || GET_CODE (x) == LSHIFTRT
6336           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6337           || GET_CODE (x) == ROTATERT
6338           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6339     return CC_NOOVmode;
6340
6341   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6342     return CC_Zmode;
6343
6344   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6345       && GET_CODE (x) == PLUS
6346       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6347     return CC_Cmode;
6348
6349   return CCmode;
6350 }
6351
6352 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6353    return the rtx for register 0 in the proper mode.  FP means this is a
6354    floating point compare: I don't think that it is needed on the arm.  */
6355 rtx
6356 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6357 {
6358   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6359   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6360
6361   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6362                           gen_rtx_COMPARE (mode, x, y)));
6363
6364   return cc_reg;
6365 }
6366
6367 /* Generate a sequence of insns that will generate the correct return
6368    address mask depending on the physical architecture that the program
6369    is running on.  */
6370 rtx
6371 arm_gen_return_addr_mask (void)
6372 {
6373   rtx reg = gen_reg_rtx (Pmode);
6374
6375   emit_insn (gen_return_addr_mask (reg));
6376   return reg;
6377 }
6378
6379 void
6380 arm_reload_in_hi (rtx *operands)
6381 {
6382   rtx ref = operands[1];
6383   rtx base, scratch;
6384   HOST_WIDE_INT offset = 0;
6385
6386   if (GET_CODE (ref) == SUBREG)
6387     {
6388       offset = SUBREG_BYTE (ref);
6389       ref = SUBREG_REG (ref);
6390     }
6391
6392   if (GET_CODE (ref) == REG)
6393     {
6394       /* We have a pseudo which has been spilt onto the stack; there
6395          are two cases here: the first where there is a simple
6396          stack-slot replacement and a second where the stack-slot is
6397          out of range, or is used as a subreg.  */
6398       if (reg_equiv_mem[REGNO (ref)])
6399         {
6400           ref = reg_equiv_mem[REGNO (ref)];
6401           base = find_replacement (&XEXP (ref, 0));
6402         }
6403       else
6404         /* The slot is out of range, or was dressed up in a SUBREG.  */
6405         base = reg_equiv_address[REGNO (ref)];
6406     }
6407   else
6408     base = find_replacement (&XEXP (ref, 0));
6409
6410   /* Handle the case where the address is too complex to be offset by 1.  */
6411   if (GET_CODE (base) == MINUS
6412       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6413     {
6414       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6415
6416       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6417       base = base_plus;
6418     }
6419   else if (GET_CODE (base) == PLUS)
6420     {
6421       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6422       HOST_WIDE_INT hi, lo;
6423
6424       offset += INTVAL (XEXP (base, 1));
6425       base = XEXP (base, 0);
6426
6427       /* Rework the address into a legal sequence of insns.  */
6428       /* Valid range for lo is -4095 -> 4095 */
6429       lo = (offset >= 0
6430             ? (offset & 0xfff)
6431             : -((-offset) & 0xfff));
6432
6433       /* Corner case, if lo is the max offset then we would be out of range
6434          once we have added the additional 1 below, so bump the msb into the
6435          pre-loading insn(s).  */
6436       if (lo == 4095)
6437         lo &= 0x7ff;
6438
6439       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6440              ^ (HOST_WIDE_INT) 0x80000000)
6441             - (HOST_WIDE_INT) 0x80000000);
6442
6443       if (hi + lo != offset)
6444         abort ();
6445
6446       if (hi != 0)
6447         {
6448           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6449
6450           /* Get the base address; addsi3 knows how to handle constants
6451              that require more than one insn.  */
6452           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6453           base = base_plus;
6454           offset = lo;
6455         }
6456     }
6457
6458   /* Operands[2] may overlap operands[0] (though it won't overlap
6459      operands[1]), that's why we asked for a DImode reg -- so we can
6460      use the bit that does not overlap.  */
6461   if (REGNO (operands[2]) == REGNO (operands[0]))
6462     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6463   else
6464     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6465
6466   emit_insn (gen_zero_extendqisi2 (scratch,
6467                                    gen_rtx_MEM (QImode,
6468                                                 plus_constant (base,
6469                                                                offset))));
6470   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6471                                    gen_rtx_MEM (QImode, 
6472                                                 plus_constant (base,
6473                                                                offset + 1))));
6474   if (!BYTES_BIG_ENDIAN)
6475     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6476                         gen_rtx_IOR (SImode, 
6477                                      gen_rtx_ASHIFT
6478                                      (SImode,
6479                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6480                                       GEN_INT (8)),
6481                                      scratch)));
6482   else
6483     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6484                             gen_rtx_IOR (SImode, 
6485                                          gen_rtx_ASHIFT (SImode, scratch,
6486                                                          GEN_INT (8)),
6487                                          gen_rtx_SUBREG (SImode, operands[0],
6488                                                          0))));
6489 }
6490
6491 /* Handle storing a half-word to memory during reload by synthesizing as two
6492    byte stores.  Take care not to clobber the input values until after we
6493    have moved them somewhere safe.  This code assumes that if the DImode
6494    scratch in operands[2] overlaps either the input value or output address
6495    in some way, then that value must die in this insn (we absolutely need
6496    two scratch registers for some corner cases).  */
6497 void
6498 arm_reload_out_hi (rtx *operands)
6499 {
6500   rtx ref = operands[0];
6501   rtx outval = operands[1];
6502   rtx base, scratch;
6503   HOST_WIDE_INT offset = 0;
6504
6505   if (GET_CODE (ref) == SUBREG)
6506     {
6507       offset = SUBREG_BYTE (ref);
6508       ref = SUBREG_REG (ref);
6509     }
6510
6511   if (GET_CODE (ref) == REG)
6512     {
6513       /* We have a pseudo which has been spilt onto the stack; there
6514          are two cases here: the first where there is a simple
6515          stack-slot replacement and a second where the stack-slot is
6516          out of range, or is used as a subreg.  */
6517       if (reg_equiv_mem[REGNO (ref)])
6518         {
6519           ref = reg_equiv_mem[REGNO (ref)];
6520           base = find_replacement (&XEXP (ref, 0));
6521         }
6522       else
6523         /* The slot is out of range, or was dressed up in a SUBREG.  */
6524         base = reg_equiv_address[REGNO (ref)];
6525     }
6526   else
6527     base = find_replacement (&XEXP (ref, 0));
6528
6529   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6530
6531   /* Handle the case where the address is too complex to be offset by 1.  */
6532   if (GET_CODE (base) == MINUS
6533       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6534     {
6535       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6536
6537       /* Be careful not to destroy OUTVAL.  */
6538       if (reg_overlap_mentioned_p (base_plus, outval))
6539         {
6540           /* Updating base_plus might destroy outval, see if we can
6541              swap the scratch and base_plus.  */
6542           if (!reg_overlap_mentioned_p (scratch, outval))
6543             {
6544               rtx tmp = scratch;
6545               scratch = base_plus;
6546               base_plus = tmp;
6547             }
6548           else
6549             {
6550               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6551
6552               /* Be conservative and copy OUTVAL into the scratch now,
6553                  this should only be necessary if outval is a subreg
6554                  of something larger than a word.  */
6555               /* XXX Might this clobber base?  I can't see how it can,
6556                  since scratch is known to overlap with OUTVAL, and
6557                  must be wider than a word.  */
6558               emit_insn (gen_movhi (scratch_hi, outval));
6559               outval = scratch_hi;
6560             }
6561         }
6562
6563       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6564       base = base_plus;
6565     }
6566   else if (GET_CODE (base) == PLUS)
6567     {
6568       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6569       HOST_WIDE_INT hi, lo;
6570
6571       offset += INTVAL (XEXP (base, 1));
6572       base = XEXP (base, 0);
6573
6574       /* Rework the address into a legal sequence of insns.  */
6575       /* Valid range for lo is -4095 -> 4095 */
6576       lo = (offset >= 0
6577             ? (offset & 0xfff)
6578             : -((-offset) & 0xfff));
6579
6580       /* Corner case, if lo is the max offset then we would be out of range
6581          once we have added the additional 1 below, so bump the msb into the
6582          pre-loading insn(s).  */
6583       if (lo == 4095)
6584         lo &= 0x7ff;
6585
6586       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6587              ^ (HOST_WIDE_INT) 0x80000000)
6588             - (HOST_WIDE_INT) 0x80000000);
6589
6590       if (hi + lo != offset)
6591         abort ();
6592
6593       if (hi != 0)
6594         {
6595           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6596
6597           /* Be careful not to destroy OUTVAL.  */
6598           if (reg_overlap_mentioned_p (base_plus, outval))
6599             {
6600               /* Updating base_plus might destroy outval, see if we
6601                  can swap the scratch and base_plus.  */
6602               if (!reg_overlap_mentioned_p (scratch, outval))
6603                 {
6604                   rtx tmp = scratch;
6605                   scratch = base_plus;
6606                   base_plus = tmp;
6607                 }
6608               else
6609                 {
6610                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6611
6612                   /* Be conservative and copy outval into scratch now,
6613                      this should only be necessary if outval is a
6614                      subreg of something larger than a word.  */
6615                   /* XXX Might this clobber base?  I can't see how it
6616                      can, since scratch is known to overlap with
6617                      outval.  */
6618                   emit_insn (gen_movhi (scratch_hi, outval));
6619                   outval = scratch_hi;
6620                 }
6621             }
6622
6623           /* Get the base address; addsi3 knows how to handle constants
6624              that require more than one insn.  */
6625           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6626           base = base_plus;
6627           offset = lo;
6628         }
6629     }
6630
6631   if (BYTES_BIG_ENDIAN)
6632     {
6633       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
6634                                          plus_constant (base, offset + 1)),
6635                             gen_lowpart (QImode, outval)));
6636       emit_insn (gen_lshrsi3 (scratch,
6637                               gen_rtx_SUBREG (SImode, outval, 0),
6638                               GEN_INT (8)));
6639       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6640                             gen_lowpart (QImode, scratch)));
6641     }
6642   else
6643     {
6644       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6645                             gen_lowpart (QImode, outval)));
6646       emit_insn (gen_lshrsi3 (scratch,
6647                               gen_rtx_SUBREG (SImode, outval, 0),
6648                               GEN_INT (8)));
6649       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6650                                          plus_constant (base, offset + 1)),
6651                             gen_lowpart (QImode, scratch)));
6652     }
6653 }
6654 \f
6655 /* Print a symbolic form of X to the debug file, F.  */
6656 static void
6657 arm_print_value (FILE *f, rtx x)
6658 {
6659   switch (GET_CODE (x))
6660     {
6661     case CONST_INT:
6662       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6663       return;
6664
6665     case CONST_DOUBLE:
6666       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6667       return;
6668
6669     case CONST_VECTOR:
6670       {
6671         int i;
6672
6673         fprintf (f, "<");
6674         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6675           {
6676             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6677             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6678               fputc (',', f);
6679           }
6680         fprintf (f, ">");
6681       }
6682       return;
6683
6684     case CONST_STRING:
6685       fprintf (f, "\"%s\"", XSTR (x, 0));
6686       return;
6687
6688     case SYMBOL_REF:
6689       fprintf (f, "`%s'", XSTR (x, 0));
6690       return;
6691
6692     case LABEL_REF:
6693       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6694       return;
6695
6696     case CONST:
6697       arm_print_value (f, XEXP (x, 0));
6698       return;
6699
6700     case PLUS:
6701       arm_print_value (f, XEXP (x, 0));
6702       fprintf (f, "+");
6703       arm_print_value (f, XEXP (x, 1));
6704       return;
6705
6706     case PC:
6707       fprintf (f, "pc");
6708       return;
6709
6710     default:
6711       fprintf (f, "????");
6712       return;
6713     }
6714 }
6715 \f
6716 /* Routines for manipulation of the constant pool.  */
6717
6718 /* Arm instructions cannot load a large constant directly into a
6719    register; they have to come from a pc relative load.  The constant
6720    must therefore be placed in the addressable range of the pc
6721    relative load.  Depending on the precise pc relative load
6722    instruction the range is somewhere between 256 bytes and 4k.  This
6723    means that we often have to dump a constant inside a function, and
6724    generate code to branch around it.
6725
6726    It is important to minimize this, since the branches will slow
6727    things down and make the code larger.
6728
6729    Normally we can hide the table after an existing unconditional
6730    branch so that there is no interruption of the flow, but in the
6731    worst case the code looks like this:
6732
6733         ldr     rn, L1
6734         ...
6735         b       L2
6736         align
6737         L1:     .long value
6738         L2:
6739         ...
6740
6741         ldr     rn, L3
6742         ...
6743         b       L4
6744         align
6745         L3:     .long value
6746         L4:
6747         ...
6748
6749    We fix this by performing a scan after scheduling, which notices
6750    which instructions need to have their operands fetched from the
6751    constant table and builds the table.
6752
6753    The algorithm starts by building a table of all the constants that
6754    need fixing up and all the natural barriers in the function (places
6755    where a constant table can be dropped without breaking the flow).
6756    For each fixup we note how far the pc-relative replacement will be
6757    able to reach and the offset of the instruction into the function.
6758
6759    Having built the table we then group the fixes together to form
6760    tables that are as large as possible (subject to addressing
6761    constraints) and emit each table of constants after the last
6762    barrier that is within range of all the instructions in the group.
6763    If a group does not contain a barrier, then we forcibly create one
6764    by inserting a jump instruction into the flow.  Once the table has
6765    been inserted, the insns are then modified to reference the
6766    relevant entry in the pool.
6767
6768    Possible enhancements to the algorithm (not implemented) are:
6769
6770    1) For some processors and object formats, there may be benefit in
6771    aligning the pools to the start of cache lines; this alignment
6772    would need to be taken into account when calculating addressability
6773    of a pool.  */
6774
6775 /* These typedefs are located at the start of this file, so that
6776    they can be used in the prototypes there.  This comment is to
6777    remind readers of that fact so that the following structures
6778    can be understood more easily.
6779
6780      typedef struct minipool_node    Mnode;
6781      typedef struct minipool_fixup   Mfix;  */
6782
6783 struct minipool_node
6784 {
6785   /* Doubly linked chain of entries.  */
6786   Mnode * next;
6787   Mnode * prev;
6788   /* The maximum offset into the code that this entry can be placed.  While
6789      pushing fixes for forward references, all entries are sorted in order
6790      of increasing max_address.  */
6791   HOST_WIDE_INT max_address;
6792   /* Similarly for an entry inserted for a backwards ref.  */
6793   HOST_WIDE_INT min_address;
6794   /* The number of fixes referencing this entry.  This can become zero
6795      if we "unpush" an entry.  In this case we ignore the entry when we
6796      come to emit the code.  */
6797   int refcount;
6798   /* The offset from the start of the minipool.  */
6799   HOST_WIDE_INT offset;
6800   /* The value in table.  */
6801   rtx value;
6802   /* The mode of value.  */
6803   enum machine_mode mode;
6804   /* The size of the value.  With iWMMXt enabled
6805      sizes > 4 also imply an alignment of 8-bytes.  */
6806   int fix_size;
6807 };
6808
6809 struct minipool_fixup
6810 {
6811   Mfix *            next;
6812   rtx               insn;
6813   HOST_WIDE_INT     address;
6814   rtx *             loc;
6815   enum machine_mode mode;
6816   int               fix_size;
6817   rtx               value;
6818   Mnode *           minipool;
6819   HOST_WIDE_INT     forwards;
6820   HOST_WIDE_INT     backwards;
6821 };
6822
6823 /* Fixes less than a word need padding out to a word boundary.  */
6824 #define MINIPOOL_FIX_SIZE(mode) \
6825   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6826
6827 static Mnode *  minipool_vector_head;
6828 static Mnode *  minipool_vector_tail;
6829 static rtx      minipool_vector_label;
6830
6831 /* The linked list of all minipool fixes required for this function.  */
6832 Mfix *          minipool_fix_head;
6833 Mfix *          minipool_fix_tail;
6834 /* The fix entry for the current minipool, once it has been placed.  */
6835 Mfix *          minipool_barrier;
6836
6837 /* Determines if INSN is the start of a jump table.  Returns the end
6838    of the TABLE or NULL_RTX.  */
6839 static rtx
6840 is_jump_table (rtx insn)
6841 {
6842   rtx table;
6843   
6844   if (GET_CODE (insn) == JUMP_INSN
6845       && JUMP_LABEL (insn) != NULL
6846       && ((table = next_real_insn (JUMP_LABEL (insn)))
6847           == next_real_insn (insn))
6848       && table != NULL
6849       && GET_CODE (table) == JUMP_INSN
6850       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6851           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6852     return table;
6853
6854   return NULL_RTX;
6855 }
6856
6857 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6858 #define JUMP_TABLES_IN_TEXT_SECTION 0
6859 #endif
6860
6861 static HOST_WIDE_INT
6862 get_jump_table_size (rtx insn)
6863 {
6864   /* ADDR_VECs only take room if read-only data does into the text
6865      section.  */
6866   if (JUMP_TABLES_IN_TEXT_SECTION
6867 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6868       || 1
6869 #endif
6870       )
6871     {
6872       rtx body = PATTERN (insn);
6873       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6874
6875       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6876     }
6877
6878   return 0;
6879 }
6880
6881 /* Move a minipool fix MP from its current location to before MAX_MP.
6882    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6883    constraints may need updating.  */
6884 static Mnode *
6885 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6886                                HOST_WIDE_INT max_address)
6887 {
6888   /* This should never be true and the code below assumes these are
6889      different.  */
6890   if (mp == max_mp)
6891     abort ();
6892
6893   if (max_mp == NULL)
6894     {
6895       if (max_address < mp->max_address)
6896         mp->max_address = max_address;
6897     }
6898   else
6899     {
6900       if (max_address > max_mp->max_address - mp->fix_size)
6901         mp->max_address = max_mp->max_address - mp->fix_size;
6902       else
6903         mp->max_address = max_address;
6904
6905       /* Unlink MP from its current position.  Since max_mp is non-null,
6906        mp->prev must be non-null.  */
6907       mp->prev->next = mp->next;
6908       if (mp->next != NULL)
6909         mp->next->prev = mp->prev;
6910       else
6911         minipool_vector_tail = mp->prev;
6912
6913       /* Re-insert it before MAX_MP.  */
6914       mp->next = max_mp;
6915       mp->prev = max_mp->prev;
6916       max_mp->prev = mp;
6917       
6918       if (mp->prev != NULL)
6919         mp->prev->next = mp;
6920       else
6921         minipool_vector_head = mp;
6922     }
6923
6924   /* Save the new entry.  */
6925   max_mp = mp;
6926
6927   /* Scan over the preceding entries and adjust their addresses as
6928      required.  */
6929   while (mp->prev != NULL
6930          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6931     {
6932       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6933       mp = mp->prev;
6934     }
6935
6936   return max_mp;
6937 }
6938
6939 /* Add a constant to the minipool for a forward reference.  Returns the
6940    node added or NULL if the constant will not fit in this pool.  */
6941 static Mnode *
6942 add_minipool_forward_ref (Mfix *fix)
6943 {
6944   /* If set, max_mp is the first pool_entry that has a lower
6945      constraint than the one we are trying to add.  */
6946   Mnode *       max_mp = NULL;
6947   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6948   Mnode *       mp;
6949   
6950   /* If this fix's address is greater than the address of the first
6951      entry, then we can't put the fix in this pool.  We subtract the
6952      size of the current fix to ensure that if the table is fully
6953      packed we still have enough room to insert this value by suffling
6954      the other fixes forwards.  */
6955   if (minipool_vector_head &&
6956       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6957     return NULL;
6958
6959   /* Scan the pool to see if a constant with the same value has
6960      already been added.  While we are doing this, also note the
6961      location where we must insert the constant if it doesn't already
6962      exist.  */
6963   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6964     {
6965       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6966           && fix->mode == mp->mode
6967           && (GET_CODE (fix->value) != CODE_LABEL
6968               || (CODE_LABEL_NUMBER (fix->value)
6969                   == CODE_LABEL_NUMBER (mp->value)))
6970           && rtx_equal_p (fix->value, mp->value))
6971         {
6972           /* More than one fix references this entry.  */
6973           mp->refcount++;
6974           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6975         }
6976
6977       /* Note the insertion point if necessary.  */
6978       if (max_mp == NULL
6979           && mp->max_address > max_address)
6980         max_mp = mp;
6981
6982       /* If we are inserting an 8-bytes aligned quantity and
6983          we have not already found an insertion point, then
6984          make sure that all such 8-byte aligned quantities are
6985          placed at the start of the pool.  */
6986       if (ARM_DOUBLEWORD_ALIGN
6987           && max_mp == NULL
6988           && fix->fix_size == 8
6989           && mp->fix_size != 8)
6990         {
6991           max_mp = mp;
6992           max_address = mp->max_address;
6993         }
6994     }
6995
6996   /* The value is not currently in the minipool, so we need to create
6997      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6998      the end of the list since the placement is less constrained than
6999      any existing entry.  Otherwise, we insert the new fix before
7000      MAX_MP and, if necessary, adjust the constraints on the other
7001      entries.  */
7002   mp = xmalloc (sizeof (* mp));
7003   mp->fix_size = fix->fix_size;
7004   mp->mode = fix->mode;
7005   mp->value = fix->value;
7006   mp->refcount = 1;
7007   /* Not yet required for a backwards ref.  */
7008   mp->min_address = -65536;
7009
7010   if (max_mp == NULL)
7011     {
7012       mp->max_address = max_address;
7013       mp->next = NULL;
7014       mp->prev = minipool_vector_tail;
7015
7016       if (mp->prev == NULL)
7017         {
7018           minipool_vector_head = mp;
7019           minipool_vector_label = gen_label_rtx ();
7020         }
7021       else
7022         mp->prev->next = mp;
7023
7024       minipool_vector_tail = mp;
7025     }
7026   else
7027     {
7028       if (max_address > max_mp->max_address - mp->fix_size)
7029         mp->max_address = max_mp->max_address - mp->fix_size;
7030       else
7031         mp->max_address = max_address;
7032
7033       mp->next = max_mp;
7034       mp->prev = max_mp->prev;
7035       max_mp->prev = mp;
7036       if (mp->prev != NULL)
7037         mp->prev->next = mp;
7038       else
7039         minipool_vector_head = mp;
7040     }
7041
7042   /* Save the new entry.  */
7043   max_mp = mp;
7044
7045   /* Scan over the preceding entries and adjust their addresses as
7046      required.  */
7047   while (mp->prev != NULL
7048          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7049     {
7050       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7051       mp = mp->prev;
7052     }
7053
7054   return max_mp;
7055 }
7056
7057 static Mnode *
7058 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7059                                 HOST_WIDE_INT  min_address)
7060 {
7061   HOST_WIDE_INT offset;
7062
7063   /* This should never be true, and the code below assumes these are
7064      different.  */
7065   if (mp == min_mp)
7066     abort ();
7067
7068   if (min_mp == NULL)
7069     {
7070       if (min_address > mp->min_address)
7071         mp->min_address = min_address;
7072     }
7073   else
7074     {
7075       /* We will adjust this below if it is too loose.  */
7076       mp->min_address = min_address;
7077
7078       /* Unlink MP from its current position.  Since min_mp is non-null,
7079          mp->next must be non-null.  */
7080       mp->next->prev = mp->prev;
7081       if (mp->prev != NULL)
7082         mp->prev->next = mp->next;
7083       else
7084         minipool_vector_head = mp->next;
7085
7086       /* Reinsert it after MIN_MP.  */
7087       mp->prev = min_mp;
7088       mp->next = min_mp->next;
7089       min_mp->next = mp;
7090       if (mp->next != NULL)
7091         mp->next->prev = mp;
7092       else
7093         minipool_vector_tail = mp;
7094     }
7095
7096   min_mp = mp;
7097
7098   offset = 0;
7099   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7100     {
7101       mp->offset = offset;
7102       if (mp->refcount > 0)
7103         offset += mp->fix_size;
7104
7105       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7106         mp->next->min_address = mp->min_address + mp->fix_size;
7107     }
7108
7109   return min_mp;
7110 }      
7111
7112 /* Add a constant to the minipool for a backward reference.  Returns the
7113    node added or NULL if the constant will not fit in this pool.  
7114
7115    Note that the code for insertion for a backwards reference can be
7116    somewhat confusing because the calculated offsets for each fix do
7117    not take into account the size of the pool (which is still under
7118    construction.  */
7119 static Mnode *
7120 add_minipool_backward_ref (Mfix *fix)
7121 {
7122   /* If set, min_mp is the last pool_entry that has a lower constraint
7123      than the one we are trying to add.  */
7124   Mnode *min_mp = NULL;
7125   /* This can be negative, since it is only a constraint.  */
7126   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7127   Mnode *mp;
7128
7129   /* If we can't reach the current pool from this insn, or if we can't
7130      insert this entry at the end of the pool without pushing other
7131      fixes out of range, then we don't try.  This ensures that we
7132      can't fail later on.  */
7133   if (min_address >= minipool_barrier->address
7134       || (minipool_vector_tail->min_address + fix->fix_size
7135           >= minipool_barrier->address))
7136     return NULL;
7137
7138   /* Scan the pool to see if a constant with the same value has
7139      already been added.  While we are doing this, also note the
7140      location where we must insert the constant if it doesn't already
7141      exist.  */
7142   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7143     {
7144       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7145           && fix->mode == mp->mode
7146           && (GET_CODE (fix->value) != CODE_LABEL
7147               || (CODE_LABEL_NUMBER (fix->value)
7148                   == CODE_LABEL_NUMBER (mp->value)))
7149           && rtx_equal_p (fix->value, mp->value)
7150           /* Check that there is enough slack to move this entry to the
7151              end of the table (this is conservative).  */
7152           && (mp->max_address 
7153               > (minipool_barrier->address 
7154                  + minipool_vector_tail->offset
7155                  + minipool_vector_tail->fix_size)))
7156         {
7157           mp->refcount++;
7158           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7159         }
7160
7161       if (min_mp != NULL)
7162         mp->min_address += fix->fix_size;
7163       else
7164         {
7165           /* Note the insertion point if necessary.  */
7166           if (mp->min_address < min_address)
7167             {
7168               /* For now, we do not allow the insertion of 8-byte alignment
7169                  requiring nodes anywhere but at the start of the pool.  */
7170               if (ARM_DOUBLEWORD_ALIGN
7171                   && fix->fix_size == 8 && mp->fix_size != 8)
7172                 return NULL;
7173               else
7174                 min_mp = mp;
7175             }
7176           else if (mp->max_address
7177                    < minipool_barrier->address + mp->offset + fix->fix_size)
7178             {
7179               /* Inserting before this entry would push the fix beyond
7180                  its maximum address (which can happen if we have
7181                  re-located a forwards fix); force the new fix to come
7182                  after it.  */
7183               min_mp = mp;
7184               min_address = mp->min_address + fix->fix_size;
7185             }
7186           /* If we are inserting an 8-bytes aligned quantity and
7187              we have not already found an insertion point, then
7188              make sure that all such 8-byte aligned quantities are
7189              placed at the start of the pool.  */
7190           else if (ARM_DOUBLEWORD_ALIGN
7191                    && min_mp == NULL
7192                    && fix->fix_size == 8
7193                    && mp->fix_size < 8)
7194             {
7195               min_mp = mp;
7196               min_address = mp->min_address + fix->fix_size;
7197             }
7198         }
7199     }
7200
7201   /* We need to create a new entry.  */
7202   mp = xmalloc (sizeof (* mp));
7203   mp->fix_size = fix->fix_size;
7204   mp->mode = fix->mode;
7205   mp->value = fix->value;
7206   mp->refcount = 1;
7207   mp->max_address = minipool_barrier->address + 65536;
7208
7209   mp->min_address = min_address;
7210
7211   if (min_mp == NULL)
7212     {
7213       mp->prev = NULL;
7214       mp->next = minipool_vector_head;
7215
7216       if (mp->next == NULL)
7217         {
7218           minipool_vector_tail = mp;
7219           minipool_vector_label = gen_label_rtx ();
7220         }
7221       else
7222         mp->next->prev = mp;
7223
7224       minipool_vector_head = mp;
7225     }
7226   else
7227     {
7228       mp->next = min_mp->next;
7229       mp->prev = min_mp;
7230       min_mp->next = mp;
7231       
7232       if (mp->next != NULL)
7233         mp->next->prev = mp;
7234       else
7235         minipool_vector_tail = mp;
7236     }
7237
7238   /* Save the new entry.  */
7239   min_mp = mp;
7240
7241   if (mp->prev)
7242     mp = mp->prev;
7243   else
7244     mp->offset = 0;
7245
7246   /* Scan over the following entries and adjust their offsets.  */
7247   while (mp->next != NULL)
7248     {
7249       if (mp->next->min_address < mp->min_address + mp->fix_size)
7250         mp->next->min_address = mp->min_address + mp->fix_size;
7251
7252       if (mp->refcount)
7253         mp->next->offset = mp->offset + mp->fix_size;
7254       else
7255         mp->next->offset = mp->offset;
7256
7257       mp = mp->next;
7258     }
7259
7260   return min_mp;
7261 }
7262
7263 static void
7264 assign_minipool_offsets (Mfix *barrier)
7265 {
7266   HOST_WIDE_INT offset = 0;
7267   Mnode *mp;
7268
7269   minipool_barrier = barrier;
7270
7271   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7272     {
7273       mp->offset = offset;
7274       
7275       if (mp->refcount > 0)
7276         offset += mp->fix_size;
7277     }
7278 }
7279
7280 /* Output the literal table */
7281 static void
7282 dump_minipool (rtx scan)
7283 {
7284   Mnode * mp;
7285   Mnode * nmp;
7286   int align64 = 0;
7287
7288   if (ARM_DOUBLEWORD_ALIGN)
7289     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7290       if (mp->refcount > 0 && mp->fix_size == 8)
7291         {
7292           align64 = 1;
7293           break;
7294         }
7295
7296   if (dump_file)
7297     fprintf (dump_file,
7298              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7299              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7300
7301   scan = emit_label_after (gen_label_rtx (), scan);
7302   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7303   scan = emit_label_after (minipool_vector_label, scan);
7304
7305   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7306     {
7307       if (mp->refcount > 0)
7308         {
7309           if (dump_file)
7310             {
7311               fprintf (dump_file, 
7312                        ";;  Offset %u, min %ld, max %ld ",
7313                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7314                        (unsigned long) mp->max_address);
7315               arm_print_value (dump_file, mp->value);
7316               fputc ('\n', dump_file);
7317             }
7318
7319           switch (mp->fix_size)
7320             {
7321 #ifdef HAVE_consttable_1
7322             case 1:
7323               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7324               break;
7325
7326 #endif
7327 #ifdef HAVE_consttable_2
7328             case 2:
7329               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7330               break;
7331
7332 #endif
7333 #ifdef HAVE_consttable_4
7334             case 4:
7335               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7336               break;
7337
7338 #endif
7339 #ifdef HAVE_consttable_8
7340             case 8:
7341               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7342               break;
7343
7344 #endif
7345             default:
7346               abort ();
7347               break;
7348             }
7349         }
7350
7351       nmp = mp->next;
7352       free (mp);
7353     }
7354
7355   minipool_vector_head = minipool_vector_tail = NULL;
7356   scan = emit_insn_after (gen_consttable_end (), scan);
7357   scan = emit_barrier_after (scan);
7358 }
7359
7360 /* Return the cost of forcibly inserting a barrier after INSN.  */
7361 static int
7362 arm_barrier_cost (rtx insn)
7363 {
7364   /* Basing the location of the pool on the loop depth is preferable,
7365      but at the moment, the basic block information seems to be
7366      corrupt by this stage of the compilation.  */
7367   int base_cost = 50;
7368   rtx next = next_nonnote_insn (insn);
7369
7370   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7371     base_cost -= 20;
7372
7373   switch (GET_CODE (insn))
7374     {
7375     case CODE_LABEL:
7376       /* It will always be better to place the table before the label, rather
7377          than after it.  */
7378       return 50;  
7379
7380     case INSN:
7381     case CALL_INSN:
7382       return base_cost;
7383
7384     case JUMP_INSN:
7385       return base_cost - 10;
7386
7387     default:
7388       return base_cost + 10;
7389     }
7390 }
7391
7392 /* Find the best place in the insn stream in the range
7393    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7394    Create the barrier by inserting a jump and add a new fix entry for
7395    it.  */
7396 static Mfix *
7397 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7398 {
7399   HOST_WIDE_INT count = 0;
7400   rtx barrier;
7401   rtx from = fix->insn;
7402   rtx selected = from;
7403   int selected_cost;
7404   HOST_WIDE_INT selected_address;
7405   Mfix * new_fix;
7406   HOST_WIDE_INT max_count = max_address - fix->address;
7407   rtx label = gen_label_rtx ();
7408
7409   selected_cost = arm_barrier_cost (from);
7410   selected_address = fix->address;
7411
7412   while (from && count < max_count)
7413     {
7414       rtx tmp;
7415       int new_cost;
7416
7417       /* This code shouldn't have been called if there was a natural barrier
7418          within range.  */
7419       if (GET_CODE (from) == BARRIER)
7420         abort ();
7421
7422       /* Count the length of this insn.  */
7423       count += get_attr_length (from);
7424
7425       /* If there is a jump table, add its length.  */
7426       tmp = is_jump_table (from);
7427       if (tmp != NULL)
7428         {
7429           count += get_jump_table_size (tmp);
7430
7431           /* Jump tables aren't in a basic block, so base the cost on
7432              the dispatch insn.  If we select this location, we will
7433              still put the pool after the table.  */
7434           new_cost = arm_barrier_cost (from);
7435
7436           if (count < max_count && new_cost <= selected_cost)
7437             {
7438               selected = tmp;
7439               selected_cost = new_cost;
7440               selected_address = fix->address + count;
7441             }
7442
7443           /* Continue after the dispatch table.  */
7444           from = NEXT_INSN (tmp);
7445           continue;
7446         }
7447
7448       new_cost = arm_barrier_cost (from);
7449       
7450       if (count < max_count && new_cost <= selected_cost)
7451         {
7452           selected = from;
7453           selected_cost = new_cost;
7454           selected_address = fix->address + count;
7455         }
7456
7457       from = NEXT_INSN (from);
7458     }
7459
7460   /* Create a new JUMP_INSN that branches around a barrier.  */
7461   from = emit_jump_insn_after (gen_jump (label), selected);
7462   JUMP_LABEL (from) = label;
7463   barrier = emit_barrier_after (from);
7464   emit_label_after (label, barrier);
7465
7466   /* Create a minipool barrier entry for the new barrier.  */
7467   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7468   new_fix->insn = barrier;
7469   new_fix->address = selected_address;
7470   new_fix->next = fix->next;
7471   fix->next = new_fix;
7472
7473   return new_fix;
7474 }
7475
7476 /* Record that there is a natural barrier in the insn stream at
7477    ADDRESS.  */
7478 static void
7479 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7480 {
7481   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7482
7483   fix->insn = insn;
7484   fix->address = address;
7485
7486   fix->next = NULL;
7487   if (minipool_fix_head != NULL)
7488     minipool_fix_tail->next = fix;
7489   else
7490     minipool_fix_head = fix;
7491
7492   minipool_fix_tail = fix;
7493 }
7494
7495 /* Record INSN, which will need fixing up to load a value from the
7496    minipool.  ADDRESS is the offset of the insn since the start of the
7497    function; LOC is a pointer to the part of the insn which requires
7498    fixing; VALUE is the constant that must be loaded, which is of type
7499    MODE.  */
7500 static void
7501 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7502                    enum machine_mode mode, rtx value)
7503 {
7504   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7505
7506 #ifdef AOF_ASSEMBLER
7507   /* PIC symbol references need to be converted into offsets into the
7508      based area.  */
7509   /* XXX This shouldn't be done here.  */
7510   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7511     value = aof_pic_entry (value);
7512 #endif /* AOF_ASSEMBLER */
7513
7514   fix->insn = insn;
7515   fix->address = address;
7516   fix->loc = loc;
7517   fix->mode = mode;
7518   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7519   fix->value = value;
7520   fix->forwards = get_attr_pool_range (insn);
7521   fix->backwards = get_attr_neg_pool_range (insn);
7522   fix->minipool = NULL;
7523
7524   /* If an insn doesn't have a range defined for it, then it isn't
7525      expecting to be reworked by this code.  Better to abort now than
7526      to generate duff assembly code.  */
7527   if (fix->forwards == 0 && fix->backwards == 0)
7528     abort ();
7529
7530   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7531      So there might be an empty word before the start of the pool.
7532      Hence we reduce the forward range by 4 to allow for this
7533      possibility.  */
7534   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7535     fix->forwards -= 4;
7536
7537   if (dump_file)
7538     {
7539       fprintf (dump_file,
7540                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7541                GET_MODE_NAME (mode),
7542                INSN_UID (insn), (unsigned long) address, 
7543                -1 * (long)fix->backwards, (long)fix->forwards);
7544       arm_print_value (dump_file, fix->value);
7545       fprintf (dump_file, "\n");
7546     }
7547
7548   /* Add it to the chain of fixes.  */
7549   fix->next = NULL;
7550   
7551   if (minipool_fix_head != NULL)
7552     minipool_fix_tail->next = fix;
7553   else
7554     minipool_fix_head = fix;
7555
7556   minipool_fix_tail = fix;
7557 }
7558
7559 /* Scan INSN and note any of its operands that need fixing.
7560    If DO_PUSHES is false we do not actually push any of the fixups
7561    needed.  The function returns TRUE is any fixups were needed/pushed.
7562    This is used by arm_memory_load_p() which needs to know about loads
7563    of constants that will be converted into minipool loads.  */
7564 static bool
7565 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7566 {
7567   bool result = false;
7568   int opno;
7569
7570   extract_insn (insn);
7571
7572   if (!constrain_operands (1))
7573     fatal_insn_not_found (insn);
7574
7575   if (recog_data.n_alternatives == 0)
7576     return false;
7577
7578   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7579   preprocess_constraints ();
7580
7581   for (opno = 0; opno < recog_data.n_operands; opno++)
7582     {
7583       /* Things we need to fix can only occur in inputs.  */
7584       if (recog_data.operand_type[opno] != OP_IN)
7585         continue;
7586
7587       /* If this alternative is a memory reference, then any mention
7588          of constants in this alternative is really to fool reload
7589          into allowing us to accept one there.  We need to fix them up
7590          now so that we output the right code.  */
7591       if (recog_op_alt[opno][which_alternative].memory_ok)
7592         {
7593           rtx op = recog_data.operand[opno];
7594
7595           if (CONSTANT_P (op))
7596             {
7597               if (do_pushes)
7598                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7599                                    recog_data.operand_mode[opno], op);
7600               result = true;
7601             }
7602           else if (GET_CODE (op) == MEM
7603                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7604                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7605             {
7606               if (do_pushes)
7607                 {
7608                   rtx cop = avoid_constant_pool_reference (op);
7609
7610                   /* Casting the address of something to a mode narrower
7611                      than a word can cause avoid_constant_pool_reference()
7612                      to return the pool reference itself.  That's no good to
7613                      us here.  Lets just hope that we can use the 
7614                      constant pool value directly.  */
7615                   if (op == cop)
7616                     cop = get_pool_constant (XEXP (op, 0));
7617
7618                   push_minipool_fix (insn, address,
7619                                      recog_data.operand_loc[opno],
7620                                      recog_data.operand_mode[opno], cop);
7621                 }
7622
7623               result = true;
7624             }
7625         }
7626     }
7627
7628   return result;
7629 }
7630
7631 /* Gcc puts the pool in the wrong place for ARM, since we can only
7632    load addresses a limited distance around the pc.  We do some
7633    special munging to move the constant pool values to the correct
7634    point in the code.  */
7635 static void
7636 arm_reorg (void)
7637 {
7638   rtx insn;
7639   HOST_WIDE_INT address = 0;
7640   Mfix * fix;
7641
7642   minipool_fix_head = minipool_fix_tail = NULL;
7643
7644   /* The first insn must always be a note, or the code below won't
7645      scan it properly.  */
7646   insn = get_insns ();
7647   if (GET_CODE (insn) != NOTE)
7648     abort ();
7649
7650   /* Scan all the insns and record the operands that will need fixing.  */
7651   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7652     {
7653       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7654           && (arm_cirrus_insn_p (insn)
7655               || GET_CODE (insn) == JUMP_INSN
7656               || arm_memory_load_p (insn)))
7657         cirrus_reorg (insn);
7658
7659       if (GET_CODE (insn) == BARRIER)
7660         push_minipool_barrier (insn, address);
7661       else if (INSN_P (insn))
7662         {
7663           rtx table;
7664
7665           note_invalid_constants (insn, address, true);
7666           address += get_attr_length (insn);
7667
7668           /* If the insn is a vector jump, add the size of the table
7669              and skip the table.  */
7670           if ((table = is_jump_table (insn)) != NULL)
7671             {
7672               address += get_jump_table_size (table);
7673               insn = table;
7674             }
7675         }
7676     }
7677
7678   fix = minipool_fix_head;
7679   
7680   /* Now scan the fixups and perform the required changes.  */
7681   while (fix)
7682     {
7683       Mfix * ftmp;
7684       Mfix * fdel;
7685       Mfix *  last_added_fix;
7686       Mfix * last_barrier = NULL;
7687       Mfix * this_fix;
7688
7689       /* Skip any further barriers before the next fix.  */
7690       while (fix && GET_CODE (fix->insn) == BARRIER)
7691         fix = fix->next;
7692
7693       /* No more fixes.  */
7694       if (fix == NULL)
7695         break;
7696
7697       last_added_fix = NULL;
7698
7699       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7700         {
7701           if (GET_CODE (ftmp->insn) == BARRIER)
7702             {
7703               if (ftmp->address >= minipool_vector_head->max_address)
7704                 break;
7705
7706               last_barrier = ftmp;
7707             }
7708           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7709             break;
7710
7711           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7712         }
7713
7714       /* If we found a barrier, drop back to that; any fixes that we
7715          could have reached but come after the barrier will now go in
7716          the next mini-pool.  */
7717       if (last_barrier != NULL)
7718         {
7719           /* Reduce the refcount for those fixes that won't go into this 
7720              pool after all.  */
7721           for (fdel = last_barrier->next;
7722                fdel && fdel != ftmp;
7723                fdel = fdel->next)
7724             {
7725               fdel->minipool->refcount--;
7726               fdel->minipool = NULL;
7727             }
7728
7729           ftmp = last_barrier;
7730         }
7731       else
7732         {
7733           /* ftmp is first fix that we can't fit into this pool and
7734              there no natural barriers that we could use.  Insert a
7735              new barrier in the code somewhere between the previous
7736              fix and this one, and arrange to jump around it.  */
7737           HOST_WIDE_INT max_address;
7738
7739           /* The last item on the list of fixes must be a barrier, so
7740              we can never run off the end of the list of fixes without
7741              last_barrier being set.  */
7742           if (ftmp == NULL)
7743             abort ();
7744
7745           max_address = minipool_vector_head->max_address;
7746           /* Check that there isn't another fix that is in range that
7747              we couldn't fit into this pool because the pool was
7748              already too large: we need to put the pool before such an
7749              instruction.  */
7750           if (ftmp->address < max_address)
7751             max_address = ftmp->address;
7752
7753           last_barrier = create_fix_barrier (last_added_fix, max_address);
7754         }
7755
7756       assign_minipool_offsets (last_barrier);
7757
7758       while (ftmp)
7759         {
7760           if (GET_CODE (ftmp->insn) != BARRIER
7761               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7762                   == NULL))
7763             break;
7764
7765           ftmp = ftmp->next;
7766         }
7767
7768       /* Scan over the fixes we have identified for this pool, fixing them
7769          up and adding the constants to the pool itself.  */
7770       for (this_fix = fix; this_fix && ftmp != this_fix;
7771            this_fix = this_fix->next)
7772         if (GET_CODE (this_fix->insn) != BARRIER)
7773           {
7774             rtx addr
7775               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
7776                                                   minipool_vector_label),
7777                                this_fix->minipool->offset);
7778             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7779           }
7780
7781       dump_minipool (last_barrier->insn);
7782       fix = ftmp;
7783     }
7784
7785   /* From now on we must synthesize any constants that we can't handle
7786      directly.  This can happen if the RTL gets split during final
7787      instruction generation.  */
7788   after_arm_reorg = 1;
7789
7790   /* Free the minipool memory.  */
7791   obstack_free (&minipool_obstack, minipool_startobj);
7792 }
7793 \f
7794 /* Routines to output assembly language.  */
7795
7796 /* If the rtx is the correct value then return the string of the number.
7797    In this way we can ensure that valid double constants are generated even
7798    when cross compiling.  */
7799 const char *
7800 fp_immediate_constant (rtx x)
7801 {
7802   REAL_VALUE_TYPE r;
7803   int i;
7804   
7805   if (!fp_consts_inited)
7806     init_fp_table ();
7807   
7808   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7809   for (i = 0; i < 8; i++)
7810     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7811       return strings_fp[i];
7812
7813   abort ();
7814 }
7815
7816 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7817 static const char *
7818 fp_const_from_val (REAL_VALUE_TYPE *r)
7819 {
7820   int i;
7821
7822   if (!fp_consts_inited)
7823     init_fp_table ();
7824
7825   for (i = 0; i < 8; i++)
7826     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7827       return strings_fp[i];
7828
7829   abort ();
7830 }
7831
7832 /* Output the operands of a LDM/STM instruction to STREAM.
7833    MASK is the ARM register set mask of which only bits 0-15 are important.
7834    REG is the base register, either the frame pointer or the stack pointer,
7835    INSTR is the possibly suffixed load or store instruction.  */
7836 static void
7837 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7838 {
7839   int i;
7840   int not_first = FALSE;
7841
7842   fputc ('\t', stream);
7843   asm_fprintf (stream, instr, reg);
7844   fputs (", {", stream);
7845   
7846   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7847     if (mask & (1 << i))
7848       {
7849         if (not_first)
7850           fprintf (stream, ", ");
7851         
7852         asm_fprintf (stream, "%r", i);
7853         not_first = TRUE;
7854       }
7855
7856   fprintf (stream, "}\n");
7857 }
7858
7859
7860 /* Output a FLDMX instruction to STREAM.
7861    BASE if the register containing the address.
7862    REG and COUNT specify the register range.
7863    Extra registers may be added to avoid hardware bugs.  */
7864
7865 static void
7866 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7867 {
7868   int i;
7869
7870   /* Workaround ARM10 VFPr1 bug.  */
7871   if (count == 2 && !arm_arch6)
7872     {
7873       if (reg == 15)
7874         reg--;
7875       count++;
7876     }
7877
7878   fputc ('\t', stream);
7879   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7880
7881   for (i = reg; i < reg + count; i++)
7882     {
7883       if (i > reg)
7884         fputs (", ", stream);
7885       asm_fprintf (stream, "d%d", i);
7886     }
7887   fputs ("}\n", stream);
7888
7889 }
7890
7891
7892 /* Output the assembly for a store multiple.  */
7893
7894 const char *
7895 vfp_output_fstmx (rtx * operands)
7896 {
7897   char pattern[100];
7898   int p;
7899   int base;
7900   int i;
7901
7902   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7903   p = strlen (pattern);
7904
7905   if (GET_CODE (operands[1]) != REG)
7906     abort ();
7907
7908   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7909   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7910     {
7911       p += sprintf (&pattern[p], ", d%d", base + i);
7912     }
7913   strcpy (&pattern[p], "}");
7914
7915   output_asm_insn (pattern, operands);
7916   return "";
7917 }
7918
7919
7920 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7921    number of bytes pushed.  */
7922
7923 static int
7924 vfp_emit_fstmx (int base_reg, int count)
7925 {
7926   rtx par;
7927   rtx dwarf;
7928   rtx tmp, reg;
7929   int i;
7930
7931   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7932      register pairs are stored by a store multiple insn.  We avoid this
7933      by pushing an extra pair.  */
7934   if (count == 2 && !arm_arch6)
7935     {
7936       if (base_reg == LAST_VFP_REGNUM - 3)
7937         base_reg -= 2;
7938       count++;
7939     }
7940
7941   /* ??? The frame layout is implementation defined.  We describe
7942      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7943      We really need some way of representing the whole block so that the
7944      unwinder can figure it out at runtime.  */
7945   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7946   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7947
7948   reg = gen_rtx_REG (DFmode, base_reg);
7949   base_reg += 2;
7950
7951   XVECEXP (par, 0, 0)
7952     = gen_rtx_SET (VOIDmode,
7953                    gen_rtx_MEM (BLKmode,
7954                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7955                    gen_rtx_UNSPEC (BLKmode,
7956                                    gen_rtvec (1, reg),
7957                                    UNSPEC_PUSH_MULT));
7958
7959   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7960                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7961                                    GEN_INT (-(count * 8 + 4))));
7962   RTX_FRAME_RELATED_P (tmp) = 1;
7963   XVECEXP (dwarf, 0, 0) = tmp;
7964
7965   tmp = gen_rtx_SET (VOIDmode,
7966                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7967                      reg);
7968   RTX_FRAME_RELATED_P (tmp) = 1;
7969   XVECEXP (dwarf, 0, 1) = tmp;
7970
7971   for (i = 1; i < count; i++)
7972     {
7973       reg = gen_rtx_REG (DFmode, base_reg);
7974       base_reg += 2;
7975       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7976
7977       tmp = gen_rtx_SET (VOIDmode,
7978                          gen_rtx_MEM (DFmode,
7979                                       gen_rtx_PLUS (SImode,
7980                                                     stack_pointer_rtx,
7981                                                     GEN_INT (i * 8))),
7982                          reg);
7983       RTX_FRAME_RELATED_P (tmp) = 1;
7984       XVECEXP (dwarf, 0, i + 1) = tmp;
7985     }
7986
7987   par = emit_insn (par);
7988   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7989                                        REG_NOTES (par));
7990   RTX_FRAME_RELATED_P (par) = 1;
7991
7992   return count * 8 + 4;
7993 }
7994
7995
7996 /* Output a 'call' insn.  */
7997 const char *
7998 output_call (rtx *operands)
7999 {
8000   if (arm_arch5)
8001     abort ();           /* Patterns should call blx <reg> directly.  */
8002
8003   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8004   if (REGNO (operands[0]) == LR_REGNUM)
8005     {
8006       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8007       output_asm_insn ("mov%?\t%0, %|lr", operands);
8008     }
8009   
8010   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8011   
8012   if (TARGET_INTERWORK || arm_arch4t)
8013     output_asm_insn ("bx%?\t%0", operands);
8014   else
8015     output_asm_insn ("mov%?\t%|pc, %0", operands);
8016   
8017   return "";
8018 }
8019
8020 /* Output a 'call' insn that is a reference in memory.  */
8021 const char *
8022 output_call_mem (rtx *operands)
8023 {
8024   if (TARGET_INTERWORK && !arm_arch5)
8025     {
8026       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8027       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8028       output_asm_insn ("bx%?\t%|ip", operands);
8029     }
8030   else if (regno_use_in (LR_REGNUM, operands[0]))
8031     {
8032       /* LR is used in the memory address.  We load the address in the
8033          first instruction.  It's safe to use IP as the target of the
8034          load since the call will kill it anyway.  */
8035       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8036       if (arm_arch5)
8037         output_asm_insn ("blx%?%|ip", operands);
8038       else
8039         {
8040           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8041           if (arm_arch4t)
8042             output_asm_insn ("bx%?\t%|ip", operands);
8043           else
8044             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8045         }
8046     }
8047   else
8048     {
8049       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8050       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8051     }
8052
8053   return "";
8054 }
8055
8056
8057 /* Output a move from arm registers to an fpa registers.
8058    OPERANDS[0] is an fpa register.
8059    OPERANDS[1] is the first registers of an arm register pair.  */
8060 const char *
8061 output_mov_long_double_fpa_from_arm (rtx *operands)
8062 {
8063   int arm_reg0 = REGNO (operands[1]);
8064   rtx ops[3];
8065
8066   if (arm_reg0 == IP_REGNUM)
8067     abort ();
8068
8069   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8070   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8071   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8072   
8073   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8074   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8075   
8076   return "";
8077 }
8078
8079 /* Output a move from an fpa register to arm registers.
8080    OPERANDS[0] is the first registers of an arm register pair.
8081    OPERANDS[1] is an fpa register.  */
8082 const char *
8083 output_mov_long_double_arm_from_fpa (rtx *operands)
8084 {
8085   int arm_reg0 = REGNO (operands[0]);
8086   rtx ops[3];
8087
8088   if (arm_reg0 == IP_REGNUM)
8089     abort ();
8090
8091   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8092   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8093   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8094
8095   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8096   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8097   return "";
8098 }
8099
8100 /* Output a move from arm registers to arm registers of a long double
8101    OPERANDS[0] is the destination.
8102    OPERANDS[1] is the source.  */
8103 const char *
8104 output_mov_long_double_arm_from_arm (rtx *operands)
8105 {
8106   /* We have to be careful here because the two might overlap.  */
8107   int dest_start = REGNO (operands[0]);
8108   int src_start = REGNO (operands[1]);
8109   rtx ops[2];
8110   int i;
8111
8112   if (dest_start < src_start)
8113     {
8114       for (i = 0; i < 3; i++)
8115         {
8116           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8117           ops[1] = gen_rtx_REG (SImode, src_start + i);
8118           output_asm_insn ("mov%?\t%0, %1", ops);
8119         }
8120     }
8121   else
8122     {
8123       for (i = 2; i >= 0; i--)
8124         {
8125           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8126           ops[1] = gen_rtx_REG (SImode, src_start + i);
8127           output_asm_insn ("mov%?\t%0, %1", ops);
8128         }
8129     }
8130
8131   return "";
8132 }
8133
8134
8135 /* Output a move from arm registers to an fpa registers.
8136    OPERANDS[0] is an fpa register.
8137    OPERANDS[1] is the first registers of an arm register pair.  */
8138 const char *
8139 output_mov_double_fpa_from_arm (rtx *operands)
8140 {
8141   int arm_reg0 = REGNO (operands[1]);
8142   rtx ops[2];
8143
8144   if (arm_reg0 == IP_REGNUM)
8145     abort ();
8146   
8147   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8148   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8149   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8150   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8151   return "";
8152 }
8153
8154 /* Output a move from an fpa register to arm registers.
8155    OPERANDS[0] is the first registers of an arm register pair.
8156    OPERANDS[1] is an fpa register.  */
8157 const char *
8158 output_mov_double_arm_from_fpa (rtx *operands)
8159 {
8160   int arm_reg0 = REGNO (operands[0]);
8161   rtx ops[2];
8162
8163   if (arm_reg0 == IP_REGNUM)
8164     abort ();
8165
8166   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8167   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8168   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8169   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8170   return "";
8171 }
8172
8173 /* Output a move between double words.
8174    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8175    or MEM<-REG and all MEMs must be offsettable addresses.  */
8176 const char *
8177 output_move_double (rtx *operands)
8178 {
8179   enum rtx_code code0 = GET_CODE (operands[0]);
8180   enum rtx_code code1 = GET_CODE (operands[1]);
8181   rtx otherops[3];
8182
8183   if (code0 == REG)
8184     {
8185       int reg0 = REGNO (operands[0]);
8186
8187       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8188       
8189       if (code1 == REG)
8190         {
8191           int reg1 = REGNO (operands[1]);
8192           if (reg1 == IP_REGNUM)
8193             abort ();
8194
8195           /* Ensure the second source is not overwritten.  */
8196           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8197             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8198           else
8199             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8200         }
8201       else if (code1 == CONST_VECTOR)
8202         {
8203           HOST_WIDE_INT hint = 0;
8204
8205           switch (GET_MODE (operands[1]))
8206             {
8207             case V2SImode:
8208               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8209               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8210               break;
8211
8212             case V4HImode:
8213               if (BYTES_BIG_ENDIAN)
8214                 {
8215                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8216                   hint <<= 16;
8217                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8218                 }
8219               else
8220                 {
8221                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8222                   hint <<= 16;
8223                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8224                 }
8225
8226               otherops[1] = GEN_INT (hint);
8227               hint = 0;
8228
8229               if (BYTES_BIG_ENDIAN)
8230                 {
8231                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8232                   hint <<= 16;
8233                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8234                 }
8235               else
8236                 {
8237                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8238                   hint <<= 16;
8239                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8240                 }
8241
8242               operands[1] = GEN_INT (hint);
8243               break;
8244
8245             case V8QImode:
8246               if (BYTES_BIG_ENDIAN)
8247                 {
8248                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8249                   hint <<= 8;
8250                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8251                   hint <<= 8;
8252                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8253                   hint <<= 8;
8254                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8255                 }
8256               else
8257                 {
8258                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8259                   hint <<= 8;
8260                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8261                   hint <<= 8;
8262                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8263                   hint <<= 8;
8264                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8265                 }
8266
8267               otherops[1] = GEN_INT (hint);
8268               hint = 0;
8269
8270               if (BYTES_BIG_ENDIAN)
8271                 {
8272                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8273                   hint <<= 8;
8274                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8275                   hint <<= 8;
8276                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8277                   hint <<= 8;
8278                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8279                 }
8280               else
8281                 {
8282                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8283                   hint <<= 8;
8284                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8285                   hint <<= 8;
8286                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8287                   hint <<= 8;
8288                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8289                 }
8290
8291               operands[1] = GEN_INT (hint);
8292               break;
8293               
8294             default:
8295               abort ();
8296             }
8297           output_mov_immediate (operands);
8298           output_mov_immediate (otherops);
8299         }
8300       else if (code1 == CONST_DOUBLE)
8301         {
8302           if (GET_MODE (operands[1]) == DFmode)
8303             {
8304               REAL_VALUE_TYPE r;
8305               long l[2];
8306
8307               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8308               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8309               otherops[1] = GEN_INT (l[1]);
8310               operands[1] = GEN_INT (l[0]);
8311             }
8312           else if (GET_MODE (operands[1]) != VOIDmode)
8313             abort ();
8314           else if (WORDS_BIG_ENDIAN)
8315             {
8316               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8317               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8318             }
8319           else
8320             {
8321               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8322               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8323             }
8324           
8325           output_mov_immediate (operands);
8326           output_mov_immediate (otherops);
8327         }
8328       else if (code1 == CONST_INT)
8329         {
8330 #if HOST_BITS_PER_WIDE_INT > 32
8331           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8332              what the upper word is.  */
8333           if (WORDS_BIG_ENDIAN)
8334             {
8335               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8336               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8337             }
8338           else
8339             {
8340               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8341               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8342             }
8343 #else
8344           /* Sign extend the intval into the high-order word.  */
8345           if (WORDS_BIG_ENDIAN)
8346             {
8347               otherops[1] = operands[1];
8348               operands[1] = (INTVAL (operands[1]) < 0
8349                              ? constm1_rtx : const0_rtx);
8350             }
8351           else
8352             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8353 #endif
8354           output_mov_immediate (otherops);
8355           output_mov_immediate (operands);
8356         }
8357       else if (code1 == MEM)
8358         {
8359           switch (GET_CODE (XEXP (operands[1], 0)))
8360             {
8361             case REG:
8362               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8363               break;
8364
8365             case PRE_INC:
8366               if (!TARGET_LDRD)
8367                 abort (); /* Should never happen now.  */
8368               output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8369               break;
8370
8371             case PRE_DEC:
8372               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8373               break;
8374
8375             case POST_INC:
8376               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8377               break;
8378
8379             case POST_DEC:
8380               if (!TARGET_LDRD)
8381                 abort (); /* Should never happen now.  */
8382               output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8383               break;
8384
8385             case PRE_MODIFY:
8386             case POST_MODIFY:
8387               otherops[0] = operands[0];
8388               otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8389               otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8390
8391               if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8392                 {
8393                   if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8394                     {
8395                       /* Registers overlap so split out the increment.  */
8396                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
8397                       output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8398                     }
8399                   else
8400                     output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8401                 }
8402               else
8403                 {
8404                   /* We only allow constant increments, so this is safe.  */
8405                   output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8406                 }
8407               break;
8408
8409             case LABEL_REF:
8410             case CONST:
8411               output_asm_insn ("adr%?\t%0, %1", operands);
8412               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8413               break;
8414
8415             default:
8416               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8417                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8418                 {
8419                   otherops[0] = operands[0];
8420                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8421                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8422
8423                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8424                     {
8425                       if (GET_CODE (otherops[2]) == CONST_INT)
8426                         {
8427                           switch ((int) INTVAL (otherops[2]))
8428                             {
8429                             case -8:
8430                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8431                               return "";
8432                             case -4:
8433                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8434                               return "";
8435                             case 4:
8436                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8437                               return "";
8438                             }
8439                         }
8440                       if (TARGET_LDRD
8441                           && (GET_CODE (otherops[2]) == REG
8442                               || (GET_CODE (otherops[2]) == CONST_INT
8443                                   && INTVAL (otherops[2]) > -256
8444                                   && INTVAL (otherops[2]) < 256)))
8445                         {
8446                           if (reg_overlap_mentioned_p (otherops[0],
8447                                                        otherops[2]))
8448                             {
8449                               /* Swap base and index registers over to
8450                                  avoid a conflict.  */
8451                               otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8452                               otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8453                               
8454                             }
8455                           /* If both registers conflict, it will usually
8456                              have been fixed by a splitter.  */
8457                           if (reg_overlap_mentioned_p (otherops[0],
8458                                                         otherops[2]))
8459                             {
8460                               output_asm_insn ("add%?\t%1, %1, %2", otherops);
8461                               output_asm_insn ("ldr%?d\t%0, [%1]",
8462                                                otherops);
8463                               return "";
8464                             }
8465                           else
8466                             {
8467                               output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8468                                                otherops);
8469                               return "";
8470                             }
8471                         }
8472                       if (GET_CODE (otherops[2]) == CONST_INT)
8473                         {
8474                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8475                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8476                           else
8477                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8478                         }
8479                       else
8480                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8481                     }
8482                   else
8483                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8484                   
8485                   return "ldm%?ia\t%0, %M0";
8486                 }
8487               else
8488                 {
8489                   otherops[1] = adjust_address (operands[1], SImode, 4);
8490                   /* Take care of overlapping base/data reg.  */
8491                   if (reg_mentioned_p (operands[0], operands[1]))
8492                     {
8493                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8494                       output_asm_insn ("ldr%?\t%0, %1", operands);
8495                     }
8496                   else
8497                     {
8498                       output_asm_insn ("ldr%?\t%0, %1", operands);
8499                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8500                     }
8501                 }
8502             }
8503         }
8504       else
8505         abort ();  /* Constraints should prevent this.  */
8506     }
8507   else if (code0 == MEM && code1 == REG)
8508     {
8509       if (REGNO (operands[1]) == IP_REGNUM)
8510         abort ();
8511
8512       switch (GET_CODE (XEXP (operands[0], 0)))
8513         {
8514         case REG:
8515           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8516           break;
8517
8518         case PRE_INC:
8519           if (!TARGET_LDRD)
8520             abort (); /* Should never happen now.  */
8521           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8522           break;
8523
8524         case PRE_DEC:
8525           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8526           break;
8527
8528         case POST_INC:
8529           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8530           break;
8531
8532         case POST_DEC:
8533           if (!TARGET_LDRD)
8534             abort (); /* Should never happen now.  */
8535           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8536           break;
8537
8538         case PRE_MODIFY:
8539         case POST_MODIFY:
8540           otherops[0] = operands[1];
8541           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8542           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8543
8544           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8545             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8546           else
8547             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8548           break;
8549
8550         case PLUS:
8551           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8552           if (GET_CODE (otherops[2]) == CONST_INT)
8553             {
8554               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8555                 {
8556                 case -8:
8557                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8558                   return "";
8559
8560                 case -4:
8561                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8562                   return "";
8563
8564                 case 4:
8565                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8566                   return "";
8567                 }
8568             }
8569           if (TARGET_LDRD
8570               && (GET_CODE (otherops[2]) == REG
8571                   || (GET_CODE (otherops[2]) == CONST_INT
8572                       && INTVAL (otherops[2]) > -256
8573                       && INTVAL (otherops[2]) < 256)))
8574             {
8575               otherops[0] = operands[1];
8576               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8577               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8578               return "";
8579             }
8580           /* Fall through */
8581
8582         default:
8583           otherops[0] = adjust_address (operands[0], SImode, 4);
8584           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8585           output_asm_insn ("str%?\t%1, %0", operands);
8586           output_asm_insn ("str%?\t%1, %0", otherops);
8587         }
8588     }
8589   else
8590     /* Constraints should prevent this.  */
8591     abort ();
8592
8593   return "";
8594 }
8595
8596
8597 /* Output an arbitrary MOV reg, #n.
8598    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8599 const char *
8600 output_mov_immediate (rtx *operands)
8601 {
8602   HOST_WIDE_INT n = INTVAL (operands[1]);
8603
8604   /* Try to use one MOV.  */
8605   if (const_ok_for_arm (n))
8606     output_asm_insn ("mov%?\t%0, %1", operands);
8607
8608   /* Try to use one MVN.  */
8609   else if (const_ok_for_arm (~n))
8610     {
8611       operands[1] = GEN_INT (~n);
8612       output_asm_insn ("mvn%?\t%0, %1", operands);
8613     }
8614   else
8615     {
8616       int n_ones = 0;
8617       int i;
8618
8619       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8620       for (i = 0; i < 32; i++)
8621         if (n & 1 << i)
8622           n_ones++;
8623
8624       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8625         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8626       else
8627         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8628     }
8629
8630   return "";
8631 }
8632
8633 /* Output an ADD r, s, #n where n may be too big for one instruction.
8634    If adding zero to one register, output nothing.  */
8635 const char *
8636 output_add_immediate (rtx *operands)
8637 {
8638   HOST_WIDE_INT n = INTVAL (operands[2]);
8639
8640   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8641     {
8642       if (n < 0)
8643         output_multi_immediate (operands,
8644                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8645                                 -n);
8646       else
8647         output_multi_immediate (operands,
8648                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8649                                 n);
8650     }
8651
8652   return "";
8653 }
8654
8655 /* Output a multiple immediate operation.
8656    OPERANDS is the vector of operands referred to in the output patterns.
8657    INSTR1 is the output pattern to use for the first constant.
8658    INSTR2 is the output pattern to use for subsequent constants.
8659    IMMED_OP is the index of the constant slot in OPERANDS.
8660    N is the constant value.  */
8661 static const char *
8662 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8663                         int immed_op, HOST_WIDE_INT n)
8664 {
8665 #if HOST_BITS_PER_WIDE_INT > 32
8666   n &= 0xffffffff;
8667 #endif
8668
8669   if (n == 0)
8670     {
8671       /* Quick and easy output.  */
8672       operands[immed_op] = const0_rtx;
8673       output_asm_insn (instr1, operands);
8674     }
8675   else
8676     {
8677       int i;
8678       const char * instr = instr1;
8679
8680       /* Note that n is never zero here (which would give no output).  */
8681       for (i = 0; i < 32; i += 2)
8682         {
8683           if (n & (3 << i))
8684             {
8685               operands[immed_op] = GEN_INT (n & (255 << i));
8686               output_asm_insn (instr, operands);
8687               instr = instr2;
8688               i += 6;
8689             }
8690         }
8691     }
8692   
8693   return "";
8694 }
8695
8696 /* Return the appropriate ARM instruction for the operation code.
8697    The returned result should not be overwritten.  OP is the rtx of the
8698    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8699    was shifted.  */
8700 const char *
8701 arithmetic_instr (rtx op, int shift_first_arg)
8702 {
8703   switch (GET_CODE (op))
8704     {
8705     case PLUS:
8706       return "add";
8707
8708     case MINUS:
8709       return shift_first_arg ? "rsb" : "sub";
8710
8711     case IOR:
8712       return "orr";
8713
8714     case XOR:
8715       return "eor";
8716
8717     case AND:
8718       return "and";
8719
8720     default:
8721       abort ();
8722     }
8723 }
8724
8725 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8726    for the operation code.  The returned result should not be overwritten.
8727    OP is the rtx code of the shift.
8728    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8729    shift.  */
8730 static const char *
8731 shift_op (rtx op, HOST_WIDE_INT *amountp)
8732 {
8733   const char * mnem;
8734   enum rtx_code code = GET_CODE (op);
8735
8736   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8737     *amountp = -1;
8738   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8739     *amountp = INTVAL (XEXP (op, 1));
8740   else
8741     abort ();
8742
8743   switch (code)
8744     {
8745     case ASHIFT:
8746       mnem = "asl";
8747       break;
8748
8749     case ASHIFTRT:
8750       mnem = "asr";
8751       break;
8752
8753     case LSHIFTRT:
8754       mnem = "lsr";
8755       break;
8756
8757     case ROTATERT:
8758       mnem = "ror";
8759       break;
8760
8761     case MULT:
8762       /* We never have to worry about the amount being other than a
8763          power of 2, since this case can never be reloaded from a reg.  */
8764       if (*amountp != -1)
8765         *amountp = int_log2 (*amountp);
8766       else
8767         abort ();
8768       return "asl";
8769
8770     default:
8771       abort ();
8772     }
8773
8774   if (*amountp != -1)
8775     {
8776       /* This is not 100% correct, but follows from the desire to merge
8777          multiplication by a power of 2 with the recognizer for a
8778          shift.  >=32 is not a valid shift for "asl", so we must try and
8779          output a shift that produces the correct arithmetical result.
8780          Using lsr #32 is identical except for the fact that the carry bit
8781          is not set correctly if we set the flags; but we never use the 
8782          carry bit from such an operation, so we can ignore that.  */
8783       if (code == ROTATERT)
8784         /* Rotate is just modulo 32.  */
8785         *amountp &= 31;
8786       else if (*amountp != (*amountp & 31))
8787         {
8788           if (code == ASHIFT)
8789             mnem = "lsr";
8790           *amountp = 32;
8791         }
8792
8793       /* Shifts of 0 are no-ops.  */
8794       if (*amountp == 0)
8795         return NULL;
8796     }     
8797
8798   return mnem;
8799 }
8800
8801 /* Obtain the shift from the POWER of two.  */
8802
8803 static HOST_WIDE_INT
8804 int_log2 (HOST_WIDE_INT power)
8805 {
8806   HOST_WIDE_INT shift = 0;
8807
8808   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8809     {
8810       if (shift > 31)
8811         abort ();
8812       shift++;
8813     }
8814
8815   return shift;
8816 }
8817
8818 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
8819    /bin/as is horribly restrictive.  */
8820 #define MAX_ASCII_LEN 51
8821
8822 void
8823 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8824 {
8825   int i;
8826   int len_so_far = 0;
8827
8828   fputs ("\t.ascii\t\"", stream);
8829   
8830   for (i = 0; i < len; i++)
8831     {
8832       int c = p[i];
8833
8834       if (len_so_far >= MAX_ASCII_LEN)
8835         {
8836           fputs ("\"\n\t.ascii\t\"", stream);
8837           len_so_far = 0;
8838         }
8839
8840       switch (c)
8841         {
8842         case TARGET_TAB:                
8843           fputs ("\\t", stream);
8844           len_so_far += 2;                      
8845           break;
8846           
8847         case TARGET_FF:
8848           fputs ("\\f", stream);
8849           len_so_far += 2;
8850           break;
8851           
8852         case TARGET_BS:
8853           fputs ("\\b", stream);
8854           len_so_far += 2;
8855           break;
8856           
8857         case TARGET_CR:
8858           fputs ("\\r", stream);
8859           len_so_far += 2;
8860           break;
8861           
8862         case TARGET_NEWLINE:
8863           fputs ("\\n", stream);
8864           c = p [i + 1];
8865           if ((c >= ' ' && c <= '~')
8866               || c == TARGET_TAB)
8867             /* This is a good place for a line break.  */
8868             len_so_far = MAX_ASCII_LEN;
8869           else
8870             len_so_far += 2;
8871           break;
8872           
8873         case '\"':
8874         case '\\':
8875           putc ('\\', stream);
8876           len_so_far++;
8877           /* Drop through.  */
8878
8879         default:
8880           if (c >= ' ' && c <= '~')
8881             {
8882               putc (c, stream);
8883               len_so_far++;
8884             }
8885           else
8886             {
8887               fprintf (stream, "\\%03o", c);
8888               len_so_far += 4;
8889             }
8890           break;
8891         }
8892     }
8893
8894   fputs ("\"\n", stream);
8895 }
8896 \f
8897 /* Compute the register sabe mask for registers 0 through 12
8898    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8899 static unsigned long
8900 arm_compute_save_reg0_reg12_mask (void)
8901 {
8902   unsigned long func_type = arm_current_func_type ();
8903   unsigned int save_reg_mask = 0;
8904   unsigned int reg;
8905
8906   if (IS_INTERRUPT (func_type))
8907     {
8908       unsigned int max_reg;
8909       /* Interrupt functions must not corrupt any registers,
8910          even call clobbered ones.  If this is a leaf function
8911          we can just examine the registers used by the RTL, but
8912          otherwise we have to assume that whatever function is
8913          called might clobber anything, and so we have to save
8914          all the call-clobbered registers as well.  */
8915       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8916         /* FIQ handlers have registers r8 - r12 banked, so
8917            we only need to check r0 - r7, Normal ISRs only
8918            bank r14 and r15, so we must check up to r12.
8919            r13 is the stack pointer which is always preserved,
8920            so we do not need to consider it here.  */
8921         max_reg = 7;
8922       else
8923         max_reg = 12;
8924         
8925       for (reg = 0; reg <= max_reg; reg++)
8926         if (regs_ever_live[reg]
8927             || (! current_function_is_leaf && call_used_regs [reg]))
8928           save_reg_mask |= (1 << reg);
8929     }
8930   else
8931     {
8932       /* In the normal case we only need to save those registers
8933          which are call saved and which are used by this function.  */
8934       for (reg = 0; reg <= 10; reg++)
8935         if (regs_ever_live[reg] && ! call_used_regs [reg])
8936           save_reg_mask |= (1 << reg);
8937
8938       /* Handle the frame pointer as a special case.  */
8939       if (! TARGET_APCS_FRAME
8940           && ! frame_pointer_needed
8941           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8942           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8943         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8944
8945       /* If we aren't loading the PIC register,
8946          don't stack it even though it may be live.  */
8947       if (flag_pic
8948           && ! TARGET_SINGLE_PIC_BASE 
8949           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8950         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8951     }
8952
8953   return save_reg_mask;
8954 }
8955
8956 /* Compute a bit mask of which registers need to be
8957    saved on the stack for the current function.  */
8958
8959 static unsigned long
8960 arm_compute_save_reg_mask (void)
8961 {
8962   unsigned int save_reg_mask = 0;
8963   unsigned long func_type = arm_current_func_type ();
8964
8965   if (IS_NAKED (func_type))
8966     /* This should never really happen.  */
8967     return 0;
8968
8969   /* If we are creating a stack frame, then we must save the frame pointer,
8970      IP (which will hold the old stack pointer), LR and the PC.  */
8971   if (frame_pointer_needed)
8972     save_reg_mask |=
8973       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8974       | (1 << IP_REGNUM)
8975       | (1 << LR_REGNUM)
8976       | (1 << PC_REGNUM);
8977
8978   /* Volatile functions do not return, so there
8979      is no need to save any other registers.  */
8980   if (IS_VOLATILE (func_type))
8981     return save_reg_mask;
8982
8983   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8984
8985   /* Decide if we need to save the link register.
8986      Interrupt routines have their own banked link register,
8987      so they never need to save it.
8988      Otherwise if we do not use the link register we do not need to save
8989      it.  If we are pushing other registers onto the stack however, we
8990      can save an instruction in the epilogue by pushing the link register
8991      now and then popping it back into the PC.  This incurs extra memory
8992      accesses though, so we only do it when optimizing for size, and only
8993      if we know that we will not need a fancy return sequence.  */
8994   if (regs_ever_live [LR_REGNUM]
8995           || (save_reg_mask
8996               && optimize_size
8997               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8998     save_reg_mask |= 1 << LR_REGNUM;
8999
9000   if (cfun->machine->lr_save_eliminated)
9001     save_reg_mask &= ~ (1 << LR_REGNUM);
9002
9003   if (TARGET_REALLY_IWMMXT
9004       && ((bit_count (save_reg_mask)
9005            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9006     {
9007       unsigned int reg;
9008
9009       /* The total number of registers that are going to be pushed
9010          onto the stack is odd.  We need to ensure that the stack
9011          is 64-bit aligned before we start to save iWMMXt registers,
9012          and also before we start to create locals.  (A local variable
9013          might be a double or long long which we will load/store using
9014          an iWMMXt instruction).  Therefore we need to push another
9015          ARM register, so that the stack will be 64-bit aligned.  We
9016          try to avoid using the arg registers (r0 -r3) as they might be
9017          used to pass values in a tail call.  */
9018       for (reg = 4; reg <= 12; reg++)
9019         if ((save_reg_mask & (1 << reg)) == 0)
9020           break;
9021
9022       if (reg <= 12)
9023         save_reg_mask |= (1 << reg);
9024       else
9025         {
9026           cfun->machine->sibcall_blocked = 1;
9027           save_reg_mask |= (1 << 3);
9028         }
9029     }
9030
9031   return save_reg_mask;
9032 }
9033
9034
9035 /* Return the number of bytes required to save VFP registers.  */
9036 static int
9037 arm_get_vfp_saved_size (void)
9038 {
9039   unsigned int regno;
9040   int count;
9041   int saved;
9042
9043   saved = 0;
9044   /* Space for saved VFP registers.  */
9045   if (TARGET_HARD_FLOAT && TARGET_VFP)
9046     {
9047       count = 0;
9048       for (regno = FIRST_VFP_REGNUM;
9049            regno < LAST_VFP_REGNUM;
9050            regno += 2)
9051         {
9052           if ((!regs_ever_live[regno] || call_used_regs[regno])
9053               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9054             {
9055               if (count > 0)
9056                 {
9057                   /* Workaround ARM10 VFPr1 bug.  */
9058                   if (count == 2 && !arm_arch6)
9059                     count++;
9060                   saved += count * 8 + 4;
9061                 }
9062               count = 0;
9063             }
9064           else
9065             count++;
9066         }
9067       if (count > 0)
9068         {
9069           if (count == 2 && !arm_arch6)
9070             count++;
9071           saved += count * 8 + 4;
9072         }
9073     }
9074   return saved;
9075 }
9076
9077
9078 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9079    everything bar the final return instruction.  */
9080 const char *
9081 output_return_instruction (rtx operand, int really_return, int reverse)
9082 {
9083   char conditional[10];
9084   char instr[100];
9085   int reg;
9086   unsigned long live_regs_mask;
9087   unsigned long func_type;
9088   arm_stack_offsets *offsets;
9089
9090   func_type = arm_current_func_type ();
9091
9092   if (IS_NAKED (func_type))
9093     return "";
9094
9095   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9096     {
9097       /* If this function was declared non-returning, and we have
9098          found a tail call, then we have to trust that the called
9099          function won't return.  */
9100       if (really_return)
9101         {
9102           rtx ops[2];
9103       
9104           /* Otherwise, trap an attempted return by aborting.  */
9105           ops[0] = operand;
9106           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
9107                                        : "abort");
9108           assemble_external_libcall (ops[1]);
9109           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9110         }
9111       
9112       return "";
9113     }
9114
9115   if (current_function_calls_alloca && !really_return)
9116     abort ();
9117
9118   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9119
9120   return_used_this_function = 1;
9121
9122   live_regs_mask = arm_compute_save_reg_mask ();
9123
9124   if (live_regs_mask)
9125     {
9126       const char * return_reg;
9127
9128       /* If we do not have any special requirements for function exit 
9129          (eg interworking, or ISR) then we can load the return address 
9130          directly into the PC.  Otherwise we must load it into LR.  */
9131       if (really_return
9132           && ! TARGET_INTERWORK)
9133         return_reg = reg_names[PC_REGNUM];
9134       else
9135         return_reg = reg_names[LR_REGNUM];
9136
9137       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9138         {
9139           /* There are three possible reasons for the IP register
9140              being saved.  1) a stack frame was created, in which case
9141              IP contains the old stack pointer, or 2) an ISR routine
9142              corrupted it, or 3) it was saved to align the stack on
9143              iWMMXt.  In case 1, restore IP into SP, otherwise just
9144              restore IP.  */
9145           if (frame_pointer_needed)
9146             {
9147               live_regs_mask &= ~ (1 << IP_REGNUM);
9148               live_regs_mask |=   (1 << SP_REGNUM);
9149             }
9150           else
9151             {
9152               if (! IS_INTERRUPT (func_type)
9153                   && ! TARGET_REALLY_IWMMXT)
9154                 abort ();
9155             }
9156         }
9157
9158       /* On some ARM architectures it is faster to use LDR rather than
9159          LDM to load a single register.  On other architectures, the
9160          cost is the same.  In 26 bit mode, or for exception handlers,
9161          we have to use LDM to load the PC so that the CPSR is also
9162          restored.  */
9163       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9164         {
9165           if (live_regs_mask == (unsigned int)(1 << reg))
9166             break;
9167         }
9168       if (reg <= LAST_ARM_REGNUM
9169           && (reg != LR_REGNUM
9170               || ! really_return 
9171               || ! IS_INTERRUPT (func_type)))
9172         {
9173           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional, 
9174                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9175         }
9176       else
9177         {
9178           char *p;
9179           int first = 1;
9180
9181           /* Generate the load multiple instruction to restore the
9182              registers.  Note we can get here, even if
9183              frame_pointer_needed is true, but only if sp already
9184              points to the base of the saved core registers.  */
9185           if (live_regs_mask & (1 << SP_REGNUM))
9186             {
9187               unsigned HOST_WIDE_INT stack_adjust;
9188
9189               offsets = arm_get_frame_offsets ();
9190               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9191               if (stack_adjust != 0 && stack_adjust != 4)
9192                 abort ();
9193
9194               if (stack_adjust && arm_arch5)
9195                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9196               else
9197                 {
9198                   /* If we can't use ldmib (SA110 bug), then try to pop r3
9199                      instead.  */
9200                   if (stack_adjust)
9201                     live_regs_mask |= 1 << 3;
9202                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9203                 }
9204             }
9205           else
9206             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9207
9208           p = instr + strlen (instr);
9209
9210           for (reg = 0; reg <= SP_REGNUM; reg++)
9211             if (live_regs_mask & (1 << reg))
9212               {
9213                 int l = strlen (reg_names[reg]);
9214
9215                 if (first)
9216                   first = 0;
9217                 else
9218                   {
9219                     memcpy (p, ", ", 2);
9220                     p += 2;
9221                   }
9222
9223                 memcpy (p, "%|", 2);
9224                 memcpy (p + 2, reg_names[reg], l);
9225                 p += l + 2;
9226               }
9227           
9228           if (live_regs_mask & (1 << LR_REGNUM))
9229             {
9230               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9231               /* If returning from an interrupt, restore the CPSR.  */
9232               if (IS_INTERRUPT (func_type))
9233                 strcat (p, "^");
9234             }
9235           else
9236             strcpy (p, "}");
9237         }
9238
9239       output_asm_insn (instr, & operand);
9240
9241       /* See if we need to generate an extra instruction to
9242          perform the actual function return.  */
9243       if (really_return
9244           && func_type != ARM_FT_INTERWORKED
9245           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9246         {
9247           /* The return has already been handled
9248              by loading the LR into the PC.  */
9249           really_return = 0;
9250         }
9251     }
9252
9253   if (really_return)
9254     {
9255       switch ((int) ARM_FUNC_TYPE (func_type))
9256         {
9257         case ARM_FT_ISR:
9258         case ARM_FT_FIQ:
9259           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9260           break;
9261
9262         case ARM_FT_INTERWORKED:
9263           sprintf (instr, "bx%s\t%%|lr", conditional);
9264           break;
9265
9266         case ARM_FT_EXCEPTION:
9267           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9268           break;
9269
9270         default:
9271           /* Use bx if it's available.  */
9272           if (arm_arch5 || arm_arch4t)
9273             sprintf (instr, "bx%s\t%%|lr", conditional);            
9274           else
9275             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9276           break;
9277         }
9278
9279       output_asm_insn (instr, & operand);
9280     }
9281
9282   return "";
9283 }
9284
9285 /* Write the function name into the code section, directly preceding
9286    the function prologue.
9287
9288    Code will be output similar to this:
9289      t0
9290          .ascii "arm_poke_function_name", 0
9291          .align
9292      t1
9293          .word 0xff000000 + (t1 - t0)
9294      arm_poke_function_name
9295          mov     ip, sp
9296          stmfd   sp!, {fp, ip, lr, pc}
9297          sub     fp, ip, #4
9298
9299    When performing a stack backtrace, code can inspect the value
9300    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9301    at location pc - 12 and the top 8 bits are set, then we know
9302    that there is a function name embedded immediately preceding this
9303    location and has length ((pc[-3]) & 0xff000000).
9304
9305    We assume that pc is declared as a pointer to an unsigned long.
9306
9307    It is of no benefit to output the function name if we are assembling
9308    a leaf function.  These function types will not contain a stack
9309    backtrace structure, therefore it is not possible to determine the
9310    function name.  */
9311 void
9312 arm_poke_function_name (FILE *stream, const char *name)
9313 {
9314   unsigned long alignlength;
9315   unsigned long length;
9316   rtx           x;
9317
9318   length      = strlen (name) + 1;
9319   alignlength = ROUND_UP_WORD (length);
9320   
9321   ASM_OUTPUT_ASCII (stream, name, length);
9322   ASM_OUTPUT_ALIGN (stream, 2);
9323   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9324   assemble_aligned_integer (UNITS_PER_WORD, x);
9325 }
9326
9327 /* Place some comments into the assembler stream
9328    describing the current function.  */
9329 static void
9330 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9331 {
9332   unsigned long func_type;
9333
9334   if (!TARGET_ARM)
9335     {
9336       thumb_output_function_prologue (f, frame_size);
9337       return;
9338     }
9339   
9340   /* Sanity check.  */
9341   if (arm_ccfsm_state || arm_target_insn)
9342     abort ();
9343
9344   func_type = arm_current_func_type ();
9345   
9346   switch ((int) ARM_FUNC_TYPE (func_type))
9347     {
9348     default:
9349     case ARM_FT_NORMAL:
9350       break;
9351     case ARM_FT_INTERWORKED:
9352       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9353       break;
9354     case ARM_FT_EXCEPTION_HANDLER:
9355       asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9356       break;
9357     case ARM_FT_ISR:
9358       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9359       break;
9360     case ARM_FT_FIQ:
9361       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9362       break;
9363     case ARM_FT_EXCEPTION:
9364       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9365       break;
9366     }
9367   
9368   if (IS_NAKED (func_type))
9369     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9370
9371   if (IS_VOLATILE (func_type))
9372     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9373
9374   if (IS_NESTED (func_type))
9375     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9376     
9377   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9378                current_function_args_size,
9379                current_function_pretend_args_size, frame_size);
9380
9381   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9382                frame_pointer_needed,
9383                cfun->machine->uses_anonymous_args);
9384
9385   if (cfun->machine->lr_save_eliminated)
9386     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9387
9388 #ifdef AOF_ASSEMBLER
9389   if (flag_pic)
9390     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9391 #endif
9392
9393   return_used_this_function = 0;  
9394 }
9395
9396 const char *
9397 arm_output_epilogue (rtx sibling)
9398 {
9399   int reg;
9400   unsigned long saved_regs_mask;
9401   unsigned long func_type;
9402   /* Floats_offset is the offset from the "virtual" frame.  In an APCS 
9403      frame that is $fp + 4 for a non-variadic function.  */
9404   int floats_offset = 0;
9405   rtx operands[3];
9406   FILE * f = asm_out_file;
9407   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9408   unsigned int lrm_count = 0;
9409   int really_return = (sibling == NULL);
9410   int start_reg;
9411   arm_stack_offsets *offsets;
9412
9413   /* If we have already generated the return instruction
9414      then it is futile to generate anything else.  */
9415   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9416     return "";
9417
9418   func_type = arm_current_func_type ();
9419
9420   if (IS_NAKED (func_type))
9421     /* Naked functions don't have epilogues.  */
9422     return "";
9423
9424   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9425     {
9426       rtx op;
9427           
9428       /* A volatile function should never return.  Call abort.  */
9429       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9430       assemble_external_libcall (op);
9431       output_asm_insn ("bl\t%a0", &op);
9432       
9433       return "";
9434     }
9435
9436   if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9437       && ! really_return)
9438     /* If we are throwing an exception, then we really must
9439        be doing a return,  so we can't tail-call.  */
9440     abort ();
9441   
9442   offsets = arm_get_frame_offsets ();
9443   saved_regs_mask = arm_compute_save_reg_mask ();
9444
9445   if (TARGET_IWMMXT)
9446     lrm_count = bit_count (saved_regs_mask);
9447
9448   floats_offset = offsets->saved_args;
9449   /* Compute how far away the floats will be.  */
9450   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9451     if (saved_regs_mask & (1 << reg))
9452       floats_offset += 4;
9453   
9454   if (frame_pointer_needed)
9455     {
9456       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9457       int vfp_offset = offsets->frame;
9458
9459       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9460         {
9461           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9462             if (regs_ever_live[reg] && !call_used_regs[reg])
9463               {
9464                 floats_offset += 12;
9465                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
9466                              reg, FP_REGNUM, floats_offset - vfp_offset);
9467               }
9468         }
9469       else
9470         {
9471           start_reg = LAST_FPA_REGNUM;
9472
9473           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9474             {
9475               if (regs_ever_live[reg] && !call_used_regs[reg])
9476                 {
9477                   floats_offset += 12;
9478                   
9479                   /* We can't unstack more than four registers at once.  */
9480                   if (start_reg - reg == 3)
9481                     {
9482                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9483                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9484                       start_reg = reg - 1;
9485                     }
9486                 }
9487               else
9488                 {
9489                   if (reg != start_reg)
9490                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9491                                  reg + 1, start_reg - reg,
9492                                  FP_REGNUM, floats_offset - vfp_offset);
9493                   start_reg = reg - 1;
9494                 }
9495             }
9496
9497           /* Just in case the last register checked also needs unstacking.  */
9498           if (reg != start_reg)
9499             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9500                          reg + 1, start_reg - reg,
9501                          FP_REGNUM, floats_offset - vfp_offset);
9502         }
9503
9504       if (TARGET_HARD_FLOAT && TARGET_VFP)
9505         {
9506           int saved_size;
9507
9508           /* The fldmx insn does not have base+offset addressing modes,
9509              so we use IP to hold the address.  */
9510           saved_size = arm_get_vfp_saved_size ();
9511
9512           if (saved_size > 0)
9513             {
9514               floats_offset += saved_size;
9515               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9516                            FP_REGNUM, floats_offset - vfp_offset);
9517             }
9518           start_reg = FIRST_VFP_REGNUM;
9519           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9520             {
9521               if ((!regs_ever_live[reg] || call_used_regs[reg])
9522                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9523                 {
9524                   if (start_reg != reg)
9525                     arm_output_fldmx (f, IP_REGNUM,
9526                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9527                                       (reg - start_reg) / 2);
9528                   start_reg = reg + 2;
9529                 }
9530             }
9531           if (start_reg != reg)
9532             arm_output_fldmx (f, IP_REGNUM,
9533                               (start_reg - FIRST_VFP_REGNUM) / 2,
9534                               (reg - start_reg) / 2);
9535         }
9536
9537       if (TARGET_IWMMXT)
9538         {
9539           /* The frame pointer is guaranteed to be non-double-word aligned.
9540              This is because it is set to (old_stack_pointer - 4) and the
9541              old_stack_pointer was double word aligned.  Thus the offset to
9542              the iWMMXt registers to be loaded must also be non-double-word
9543              sized, so that the resultant address *is* double-word aligned.
9544              We can ignore floats_offset since that was already included in
9545              the live_regs_mask.  */
9546           lrm_count += (lrm_count % 2 ? 2 : 1);
9547               
9548           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9549             if (regs_ever_live[reg] && !call_used_regs[reg])
9550               {
9551                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n", 
9552                              reg, FP_REGNUM, lrm_count * 4);
9553                 lrm_count += 2; 
9554               }
9555         }
9556
9557       /* saved_regs_mask should contain the IP, which at the time of stack
9558          frame generation actually contains the old stack pointer.  So a
9559          quick way to unwind the stack is just pop the IP register directly
9560          into the stack pointer.  */
9561       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9562         abort ();
9563       saved_regs_mask &= ~ (1 << IP_REGNUM);
9564       saved_regs_mask |=   (1 << SP_REGNUM);
9565
9566       /* There are two registers left in saved_regs_mask - LR and PC.  We
9567          only need to restore the LR register (the return address), but to
9568          save time we can load it directly into the PC, unless we need a
9569          special function exit sequence, or we are not really returning.  */
9570       if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9571         /* Delete the LR from the register mask, so that the LR on
9572            the stack is loaded into the PC in the register mask.  */
9573         saved_regs_mask &= ~ (1 << LR_REGNUM);
9574       else
9575         saved_regs_mask &= ~ (1 << PC_REGNUM);
9576
9577       /* We must use SP as the base register, because SP is one of the
9578          registers being restored.  If an interrupt or page fault
9579          happens in the ldm instruction, the SP might or might not
9580          have been restored.  That would be bad, as then SP will no
9581          longer indicate the safe area of stack, and we can get stack
9582          corruption.  Using SP as the base register means that it will
9583          be reset correctly to the original value, should an interrupt
9584          occur.  If the stack pointer already points at the right
9585          place, then omit the subtraction.  */
9586       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9587           || current_function_calls_alloca)
9588         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9589                      4 * bit_count (saved_regs_mask));
9590       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9591
9592       if (IS_INTERRUPT (func_type))
9593         /* Interrupt handlers will have pushed the
9594            IP onto the stack, so restore it now.  */
9595         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9596     }
9597   else
9598     {
9599       /* Restore stack pointer if necessary.  */
9600       if (offsets->outgoing_args != offsets->saved_regs)
9601         {
9602           operands[0] = operands[1] = stack_pointer_rtx;
9603           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9604           output_add_immediate (operands);
9605         }
9606
9607       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9608         {
9609           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9610             if (regs_ever_live[reg] && !call_used_regs[reg])
9611               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9612                            reg, SP_REGNUM);
9613         }
9614       else
9615         {
9616           start_reg = FIRST_FPA_REGNUM;
9617
9618           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9619             {
9620               if (regs_ever_live[reg] && !call_used_regs[reg])
9621                 {
9622                   if (reg - start_reg == 3)
9623                     {
9624                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9625                                    start_reg, SP_REGNUM);
9626                       start_reg = reg + 1;
9627                     }
9628                 }
9629               else
9630                 {
9631                   if (reg != start_reg)
9632                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9633                                  start_reg, reg - start_reg,
9634                                  SP_REGNUM);
9635                   
9636                   start_reg = reg + 1;
9637                 }
9638             }
9639
9640           /* Just in case the last register checked also needs unstacking.  */
9641           if (reg != start_reg)
9642             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9643                          start_reg, reg - start_reg, SP_REGNUM);
9644         }
9645
9646       if (TARGET_HARD_FLOAT && TARGET_VFP)
9647         {
9648           start_reg = FIRST_VFP_REGNUM;
9649           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9650             {
9651               if ((!regs_ever_live[reg] || call_used_regs[reg])
9652                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9653                 {
9654                   if (start_reg != reg)
9655                     arm_output_fldmx (f, SP_REGNUM,
9656                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9657                                       (reg - start_reg) / 2);
9658                   start_reg = reg + 2;
9659                 }
9660             }
9661           if (start_reg != reg)
9662             arm_output_fldmx (f, SP_REGNUM,
9663                               (start_reg - FIRST_VFP_REGNUM) / 2,
9664                               (reg - start_reg) / 2);
9665         }
9666       if (TARGET_IWMMXT)
9667         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9668           if (regs_ever_live[reg] && !call_used_regs[reg])
9669             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9670
9671       /* If we can, restore the LR into the PC.  */
9672       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9673           && really_return
9674           && current_function_pretend_args_size == 0
9675           && saved_regs_mask & (1 << LR_REGNUM))
9676         {
9677           saved_regs_mask &= ~ (1 << LR_REGNUM);
9678           saved_regs_mask |=   (1 << PC_REGNUM);
9679         }
9680
9681       /* Load the registers off the stack.  If we only have one register
9682          to load use the LDR instruction - it is faster.  */
9683       if (saved_regs_mask == (1 << LR_REGNUM))
9684         {
9685           /* The exception handler ignores the LR, so we do
9686              not really need to load it off the stack.  */
9687           if (eh_ofs)
9688             asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
9689           else
9690             asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9691         }
9692       else if (saved_regs_mask)
9693         {
9694           if (saved_regs_mask & (1 << SP_REGNUM))
9695             /* Note - write back to the stack register is not enabled
9696                (ie "ldmfd sp!...").  We know that the stack pointer is
9697                in the list of registers and if we add writeback the
9698                instruction becomes UNPREDICTABLE.  */
9699             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9700           else
9701             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9702         }
9703
9704       if (current_function_pretend_args_size)
9705         {
9706           /* Unwind the pre-pushed regs.  */
9707           operands[0] = operands[1] = stack_pointer_rtx;
9708           operands[2] = GEN_INT (current_function_pretend_args_size);
9709           output_add_immediate (operands);
9710         }
9711     }
9712
9713   /* We may have already restored PC directly from the stack.  */
9714   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9715     return "";
9716
9717   /* Generate the return instruction.  */
9718   switch ((int) ARM_FUNC_TYPE (func_type))
9719     {
9720     case ARM_FT_EXCEPTION_HANDLER:
9721       asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9722       break;
9723
9724     case ARM_FT_ISR:
9725     case ARM_FT_FIQ:
9726       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9727       break;
9728
9729     case ARM_FT_EXCEPTION:
9730       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9731       break;
9732
9733     case ARM_FT_INTERWORKED:
9734       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9735       break;
9736
9737     default:
9738       if (arm_arch5 || arm_arch4t)
9739         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9740       else
9741         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9742       break;
9743     }
9744
9745   return "";
9746 }
9747
9748 static void
9749 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9750                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9751 {
9752   arm_stack_offsets *offsets;
9753
9754   if (TARGET_THUMB)
9755     {
9756       /* ??? Probably not safe to set this here, since it assumes that a
9757          function will be emitted as assembly immediately after we generate
9758          RTL for it.  This does not happen for inline functions.  */
9759       return_used_this_function = 0;
9760     }
9761   else
9762     {
9763       /* We need to take into account any stack-frame rounding.  */
9764       offsets = arm_get_frame_offsets ();
9765
9766       if (use_return_insn (FALSE, NULL)
9767           && return_used_this_function
9768           && offsets->saved_regs != offsets->outgoing_args
9769           && !frame_pointer_needed)
9770         abort ();
9771
9772       /* Reset the ARM-specific per-function variables.  */
9773       after_arm_reorg = 0;
9774     }
9775 }
9776
9777 /* Generate and emit an insn that we will recognize as a push_multi.
9778    Unfortunately, since this insn does not reflect very well the actual
9779    semantics of the operation, we need to annotate the insn for the benefit
9780    of DWARF2 frame unwind information.  */
9781 static rtx
9782 emit_multi_reg_push (int mask)
9783 {
9784   int num_regs = 0;
9785   int num_dwarf_regs;
9786   int i, j;
9787   rtx par;
9788   rtx dwarf;
9789   int dwarf_par_index;
9790   rtx tmp, reg;
9791
9792   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9793     if (mask & (1 << i))
9794       num_regs++;
9795
9796   if (num_regs == 0 || num_regs > 16)
9797     abort ();
9798
9799   /* We don't record the PC in the dwarf frame information.  */
9800   num_dwarf_regs = num_regs;
9801   if (mask & (1 << PC_REGNUM))
9802     num_dwarf_regs--;
9803
9804   /* For the body of the insn we are going to generate an UNSPEC in
9805      parallel with several USEs.  This allows the insn to be recognized
9806      by the push_multi pattern in the arm.md file.  The insn looks
9807      something like this:
9808
9809        (parallel [ 
9810            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9811                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9812            (use (reg:SI 11 fp))
9813            (use (reg:SI 12 ip))
9814            (use (reg:SI 14 lr))
9815            (use (reg:SI 15 pc))
9816         ])
9817
9818      For the frame note however, we try to be more explicit and actually
9819      show each register being stored into the stack frame, plus a (single)
9820      decrement of the stack pointer.  We do it this way in order to be
9821      friendly to the stack unwinding code, which only wants to see a single
9822      stack decrement per instruction.  The RTL we generate for the note looks
9823      something like this:
9824
9825       (sequence [ 
9826            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9827            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9828            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9829            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9830            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9831         ])
9832
9833       This sequence is used both by the code to support stack unwinding for
9834       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9835   
9836   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9837   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9838   dwarf_par_index = 1;
9839
9840   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9841     {
9842       if (mask & (1 << i))
9843         {
9844           reg = gen_rtx_REG (SImode, i);
9845
9846           XVECEXP (par, 0, 0)
9847             = gen_rtx_SET (VOIDmode,
9848                            gen_rtx_MEM (BLKmode,
9849                                         gen_rtx_PRE_DEC (BLKmode,
9850                                                          stack_pointer_rtx)),
9851                            gen_rtx_UNSPEC (BLKmode,
9852                                            gen_rtvec (1, reg),
9853                                            UNSPEC_PUSH_MULT));
9854
9855           if (i != PC_REGNUM)
9856             {
9857               tmp = gen_rtx_SET (VOIDmode,
9858                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9859                                  reg);
9860               RTX_FRAME_RELATED_P (tmp) = 1;
9861               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9862               dwarf_par_index++;
9863             }
9864
9865           break;
9866         }
9867     }
9868
9869   for (j = 1, i++; j < num_regs; i++)
9870     {
9871       if (mask & (1 << i))
9872         {
9873           reg = gen_rtx_REG (SImode, i);
9874
9875           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9876
9877           if (i != PC_REGNUM)
9878             {
9879               tmp = gen_rtx_SET (VOIDmode,
9880                                  gen_rtx_MEM (SImode,
9881                                               plus_constant (stack_pointer_rtx,
9882                                                              4 * j)),
9883                                  reg);
9884               RTX_FRAME_RELATED_P (tmp) = 1;
9885               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9886             }
9887
9888           j++;
9889         }
9890     }
9891
9892   par = emit_insn (par);
9893   
9894   tmp = gen_rtx_SET (SImode,
9895                      stack_pointer_rtx,
9896                      gen_rtx_PLUS (SImode,
9897                                    stack_pointer_rtx,
9898                                    GEN_INT (-4 * num_regs)));
9899   RTX_FRAME_RELATED_P (tmp) = 1;
9900   XVECEXP (dwarf, 0, 0) = tmp;
9901   
9902   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9903                                        REG_NOTES (par));
9904   return par;
9905 }
9906
9907 static rtx
9908 emit_sfm (int base_reg, int count)
9909 {
9910   rtx par;
9911   rtx dwarf;
9912   rtx tmp, reg;
9913   int i;
9914
9915   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9916   dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9917
9918   reg = gen_rtx_REG (XFmode, base_reg++);
9919
9920   XVECEXP (par, 0, 0)
9921     = gen_rtx_SET (VOIDmode, 
9922                    gen_rtx_MEM (BLKmode,
9923                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9924                    gen_rtx_UNSPEC (BLKmode,
9925                                    gen_rtvec (1, reg),
9926                                    UNSPEC_PUSH_MULT));
9927   tmp
9928     = gen_rtx_SET (VOIDmode, 
9929                    gen_rtx_MEM (XFmode,
9930                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9931                    reg);
9932   RTX_FRAME_RELATED_P (tmp) = 1;
9933   XVECEXP (dwarf, 0, count - 1) = tmp;    
9934   
9935   for (i = 1; i < count; i++)
9936     {
9937       reg = gen_rtx_REG (XFmode, base_reg++);
9938       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9939
9940       tmp = gen_rtx_SET (VOIDmode, 
9941                          gen_rtx_MEM (XFmode,
9942                                       gen_rtx_PRE_DEC (BLKmode,
9943                                                        stack_pointer_rtx)),
9944                          reg);
9945       RTX_FRAME_RELATED_P (tmp) = 1;
9946       XVECEXP (dwarf, 0, count - i - 1) = tmp;    
9947     }
9948
9949   par = emit_insn (par);
9950   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9951                                        REG_NOTES (par));
9952   return par;
9953 }
9954
9955
9956 /* Return true if the current function needs to save/restore LR.  */
9957
9958 static bool
9959 thumb_force_lr_save (void)
9960 {
9961   return !cfun->machine->lr_save_eliminated
9962          && (!leaf_function_p ()
9963              || thumb_far_jump_used_p ()
9964              || regs_ever_live [LR_REGNUM]);
9965 }
9966
9967
9968 /* Compute the distance from register FROM to register TO.
9969    These can be the arg pointer (26), the soft frame pointer (25),
9970    the stack pointer (13) or the hard frame pointer (11).
9971    Typical stack layout looks like this:
9972
9973        old stack pointer -> |    |
9974                              ----
9975                             |    | \
9976                             |    |   saved arguments for
9977                             |    |   vararg functions
9978                             |    | /
9979                               --
9980    hard FP & arg pointer -> |    | \
9981                             |    |   stack
9982                             |    |   frame
9983                             |    | /
9984                               --
9985                             |    | \
9986                             |    |   call saved
9987                             |    |   registers
9988       soft frame pointer -> |    | /
9989                               --
9990                             |    | \
9991                             |    |   local
9992                             |    |   variables
9993                             |    | /
9994                               --
9995                             |    | \
9996                             |    |   outgoing
9997                             |    |   arguments
9998    current stack pointer -> |    | /
9999                               --
10000
10001   For a given function some or all of these stack components
10002   may not be needed, giving rise to the possibility of
10003   eliminating some of the registers.
10004
10005   The values returned by this function must reflect the behavior
10006   of arm_expand_prologue() and arm_compute_save_reg_mask().
10007
10008   The sign of the number returned reflects the direction of stack
10009   growth, so the values are positive for all eliminations except
10010   from the soft frame pointer to the hard frame pointer.
10011
10012   SFP may point just inside the local variables block to ensure correct
10013   alignment.  */
10014
10015
10016 /* Calculate stack offsets.  These are used to calculate register elimination
10017    offsets and in prologue/epilogue code.  */
10018
10019 static arm_stack_offsets *
10020 arm_get_frame_offsets (void)
10021 {
10022   struct arm_stack_offsets *offsets;
10023   unsigned long func_type;
10024   int leaf;
10025   int saved;
10026   HOST_WIDE_INT frame_size;
10027
10028   offsets = &cfun->machine->stack_offsets;
10029   
10030   /* We need to know if we are a leaf function.  Unfortunately, it
10031      is possible to be called after start_sequence has been called,
10032      which causes get_insns to return the insns for the sequence,
10033      not the function, which will cause leaf_function_p to return
10034      the incorrect result.
10035
10036      to know about leaf functions once reload has completed, and the
10037      frame size cannot be changed after that time, so we can safely
10038      use the cached value.  */
10039
10040   if (reload_completed)
10041     return offsets;
10042
10043   /* Initially this is the size of the local variables.  It will translated
10044      into an offset once we have determined the size of preceding data.  */
10045   frame_size = ROUND_UP_WORD (get_frame_size ());
10046
10047   leaf = leaf_function_p ();
10048
10049   /* Space for variadic functions.  */
10050   offsets->saved_args = current_function_pretend_args_size;
10051
10052   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10053
10054   if (TARGET_ARM)
10055     {
10056       unsigned int regno;
10057
10058       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10059
10060       /* We know that SP will be doubleword aligned on entry, and we must
10061          preserve that condition at any subroutine call.  We also require the
10062          soft frame pointer to be doubleword aligned.  */
10063
10064       if (TARGET_REALLY_IWMMXT)
10065         {
10066           /* Check for the call-saved iWMMXt registers.  */
10067           for (regno = FIRST_IWMMXT_REGNUM;
10068                regno <= LAST_IWMMXT_REGNUM;
10069                regno++)
10070             if (regs_ever_live [regno] && ! call_used_regs [regno])
10071               saved += 8;
10072         }
10073
10074       func_type = arm_current_func_type ();
10075       if (! IS_VOLATILE (func_type))
10076         {
10077           /* Space for saved FPA registers.  */
10078           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10079           if (regs_ever_live[regno] && ! call_used_regs[regno])
10080             saved += 12;
10081
10082           /* Space for saved VFP registers.  */
10083           if (TARGET_HARD_FLOAT && TARGET_VFP)
10084             saved += arm_get_vfp_saved_size ();
10085         }
10086     }
10087   else /* TARGET_THUMB */
10088     {
10089       int reg;
10090       int count_regs;
10091
10092       saved = 0;
10093       count_regs = 0;
10094       for (reg = 8; reg < 13; reg ++)
10095         if (THUMB_REG_PUSHED_P (reg))
10096           count_regs ++;
10097       if (count_regs)
10098         saved += 4 * count_regs;
10099       count_regs = 0;
10100       for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10101         if (THUMB_REG_PUSHED_P (reg))
10102           count_regs ++;
10103       if (count_regs || thumb_force_lr_save ())
10104         saved += 4 * (count_regs + 1);
10105       if (TARGET_BACKTRACE)
10106         {
10107           if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10108             saved += 20;
10109           else
10110             saved += 16;
10111         }
10112     }
10113
10114   /* Saved registers include the stack frame.  */
10115   offsets->saved_regs = offsets->saved_args + saved;
10116   offsets->soft_frame = offsets->saved_regs;
10117   /* A leaf function does not need any stack alignment if it has nothing
10118      on the stack.  */
10119   if (leaf && frame_size == 0)
10120     {
10121       offsets->outgoing_args = offsets->soft_frame;
10122       return offsets;
10123     }
10124
10125   /* Ensure SFP has the correct alignment.  */
10126   if (ARM_DOUBLEWORD_ALIGN
10127       && (offsets->soft_frame & 7))
10128     offsets->soft_frame += 4;
10129
10130   offsets->outgoing_args = offsets->soft_frame + frame_size
10131                            + current_function_outgoing_args_size;
10132
10133   if (ARM_DOUBLEWORD_ALIGN)
10134     {
10135       /* Ensure SP remains doubleword aligned.  */
10136       if (offsets->outgoing_args & 7)
10137         offsets->outgoing_args += 4;
10138       if (offsets->outgoing_args & 7)
10139         abort ();
10140     }
10141
10142   return offsets;
10143 }
10144
10145
10146 /* Calculate the relative offsets for the different stack pointers.  Positive
10147    offsets are in the direction of stack growth.  */
10148
10149 unsigned int
10150 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10151 {
10152   arm_stack_offsets *offsets;
10153
10154   offsets = arm_get_frame_offsets ();
10155
10156   /* OK, now we have enough information to compute the distances.
10157      There must be an entry in these switch tables for each pair
10158      of registers in ELIMINABLE_REGS, even if some of the entries
10159      seem to be redundant or useless.  */
10160   switch (from)
10161     {
10162     case ARG_POINTER_REGNUM:
10163       switch (to)
10164         {
10165         case THUMB_HARD_FRAME_POINTER_REGNUM:
10166           return 0;
10167
10168         case FRAME_POINTER_REGNUM:
10169           /* This is the reverse of the soft frame pointer
10170              to hard frame pointer elimination below.  */
10171           return offsets->soft_frame - offsets->saved_args;
10172
10173         case ARM_HARD_FRAME_POINTER_REGNUM:
10174           /* If there is no stack frame then the hard
10175              frame pointer and the arg pointer coincide.  */
10176           if (offsets->frame == offsets->saved_regs)
10177             return 0;
10178           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10179           return (frame_pointer_needed
10180                   && cfun->static_chain_decl != NULL
10181                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10182
10183         case STACK_POINTER_REGNUM:
10184           /* If nothing has been pushed on the stack at all
10185              then this will return -4.  This *is* correct!  */
10186           return offsets->outgoing_args - (offsets->saved_args + 4);
10187
10188         default:
10189           abort ();
10190         }
10191       break;
10192
10193     case FRAME_POINTER_REGNUM:
10194       switch (to)
10195         {
10196         case THUMB_HARD_FRAME_POINTER_REGNUM:
10197           return 0;
10198
10199         case ARM_HARD_FRAME_POINTER_REGNUM:
10200           /* The hard frame pointer points to the top entry in the
10201              stack frame.  The soft frame pointer to the bottom entry
10202              in the stack frame.  If there is no stack frame at all,
10203              then they are identical.  */
10204
10205           return offsets->frame - offsets->soft_frame;
10206
10207         case STACK_POINTER_REGNUM:
10208           return offsets->outgoing_args - offsets->soft_frame;
10209
10210         default:
10211           abort ();
10212         }
10213       break;
10214
10215     default:
10216       /* You cannot eliminate from the stack pointer.
10217          In theory you could eliminate from the hard frame
10218          pointer to the stack pointer, but this will never
10219          happen, since if a stack frame is not needed the
10220          hard frame pointer will never be used.  */
10221       abort ();
10222     }
10223 }
10224
10225
10226 /* Generate the prologue instructions for entry into an ARM function.  */
10227 void
10228 arm_expand_prologue (void)
10229 {
10230   int reg;
10231   rtx amount;
10232   rtx insn;
10233   rtx ip_rtx;
10234   unsigned long live_regs_mask;
10235   unsigned long func_type;
10236   int fp_offset = 0;
10237   int saved_pretend_args = 0;
10238   int saved_regs = 0;
10239   unsigned int args_to_push;
10240   arm_stack_offsets *offsets;
10241
10242   func_type = arm_current_func_type ();
10243
10244   /* Naked functions don't have prologues.  */
10245   if (IS_NAKED (func_type))
10246     return;
10247
10248   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10249   args_to_push = current_function_pretend_args_size;
10250   
10251   /* Compute which register we will have to save onto the stack.  */
10252   live_regs_mask = arm_compute_save_reg_mask ();
10253
10254   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10255
10256   if (frame_pointer_needed)
10257     {
10258       if (IS_INTERRUPT (func_type))
10259         {
10260           /* Interrupt functions must not corrupt any registers.
10261              Creating a frame pointer however, corrupts the IP
10262              register, so we must push it first.  */
10263           insn = emit_multi_reg_push (1 << IP_REGNUM);
10264
10265           /* Do not set RTX_FRAME_RELATED_P on this insn.
10266              The dwarf stack unwinding code only wants to see one
10267              stack decrement per function, and this is not it.  If
10268              this instruction is labeled as being part of the frame
10269              creation sequence then dwarf2out_frame_debug_expr will
10270              abort when it encounters the assignment of IP to FP
10271              later on, since the use of SP here establishes SP as
10272              the CFA register and not IP.
10273
10274              Anyway this instruction is not really part of the stack
10275              frame creation although it is part of the prologue.  */
10276         }
10277       else if (IS_NESTED (func_type))
10278         {
10279           /* The Static chain register is the same as the IP register
10280              used as a scratch register during stack frame creation.
10281              To get around this need to find somewhere to store IP
10282              whilst the frame is being created.  We try the following
10283              places in order:
10284              
10285                1. The last argument register.
10286                2. A slot on the stack above the frame.  (This only
10287                   works if the function is not a varargs function).
10288                3. Register r3, after pushing the argument registers
10289                   onto the stack.
10290
10291              Note - we only need to tell the dwarf2 backend about the SP
10292              adjustment in the second variant; the static chain register
10293              doesn't need to be unwound, as it doesn't contain a value
10294              inherited from the caller.  */
10295
10296           if (regs_ever_live[3] == 0)
10297             {
10298               insn = gen_rtx_REG (SImode, 3);
10299               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10300               insn = emit_insn (insn);
10301             }
10302           else if (args_to_push == 0)
10303             {
10304               rtx dwarf;
10305               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10306               insn = gen_rtx_MEM (SImode, insn);
10307               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10308               insn = emit_insn (insn);
10309
10310               fp_offset = 4;
10311
10312               /* Just tell the dwarf backend that we adjusted SP.  */
10313               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10314                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10315                                                  GEN_INT (-fp_offset)));
10316               RTX_FRAME_RELATED_P (insn) = 1;
10317               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10318                                                     dwarf, REG_NOTES (insn));
10319             }
10320           else
10321             {
10322               /* Store the args on the stack.  */
10323               if (cfun->machine->uses_anonymous_args)
10324                 insn = emit_multi_reg_push
10325                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10326               else
10327                 insn = emit_insn
10328                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10329                                GEN_INT (- args_to_push)));
10330
10331               RTX_FRAME_RELATED_P (insn) = 1;
10332
10333               saved_pretend_args = 1;
10334               fp_offset = args_to_push;
10335               args_to_push = 0;
10336
10337               /* Now reuse r3 to preserve IP.  */
10338               insn = gen_rtx_REG (SImode, 3);
10339               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10340               (void) emit_insn (insn);
10341             }
10342         }
10343
10344       if (fp_offset)
10345         {
10346           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10347           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10348         }
10349       else
10350         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10351       
10352       insn = emit_insn (insn);
10353       RTX_FRAME_RELATED_P (insn) = 1;
10354     }
10355
10356   if (args_to_push)
10357     {
10358       /* Push the argument registers, or reserve space for them.  */
10359       if (cfun->machine->uses_anonymous_args)
10360         insn = emit_multi_reg_push
10361           ((0xf0 >> (args_to_push / 4)) & 0xf);
10362       else
10363         insn = emit_insn
10364           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10365                        GEN_INT (- args_to_push)));
10366       RTX_FRAME_RELATED_P (insn) = 1;
10367     }
10368
10369   /* If this is an interrupt service routine, and the link register
10370      is going to be pushed, and we are not creating a stack frame,
10371      (which would involve an extra push of IP and a pop in the epilogue)
10372      subtracting four from LR now will mean that the function return
10373      can be done with a single instruction.  */
10374   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10375       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10376       && ! frame_pointer_needed)
10377     emit_insn (gen_rtx_SET (SImode, 
10378                             gen_rtx_REG (SImode, LR_REGNUM),
10379                             gen_rtx_PLUS (SImode,
10380                                           gen_rtx_REG (SImode, LR_REGNUM),
10381                                           GEN_INT (-4))));
10382
10383   if (live_regs_mask)
10384     {
10385       insn = emit_multi_reg_push (live_regs_mask);
10386       saved_regs += bit_count (live_regs_mask) * 4;
10387       RTX_FRAME_RELATED_P (insn) = 1;
10388     }
10389
10390   if (TARGET_IWMMXT)
10391     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10392       if (regs_ever_live[reg] && ! call_used_regs [reg])
10393         {
10394           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10395           insn = gen_rtx_MEM (V2SImode, insn);
10396           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10397                                          gen_rtx_REG (V2SImode, reg)));
10398           RTX_FRAME_RELATED_P (insn) = 1;
10399           saved_regs += 8;
10400         }
10401
10402   if (! IS_VOLATILE (func_type))
10403     {
10404       int start_reg;
10405
10406       /* Save any floating point call-saved registers used by this
10407          function.  */
10408       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10409         {
10410           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10411             if (regs_ever_live[reg] && !call_used_regs[reg])
10412               {
10413                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10414                 insn = gen_rtx_MEM (XFmode, insn);
10415                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10416                                                gen_rtx_REG (XFmode, reg)));
10417                 RTX_FRAME_RELATED_P (insn) = 1;
10418                 saved_regs += 12;
10419               }
10420         }
10421       else
10422         {
10423           start_reg = LAST_FPA_REGNUM;
10424
10425           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10426             {
10427               if (regs_ever_live[reg] && !call_used_regs[reg])
10428                 {
10429                   if (start_reg - reg == 3)
10430                     {
10431                       insn = emit_sfm (reg, 4);
10432                       RTX_FRAME_RELATED_P (insn) = 1;
10433                       saved_regs += 48;
10434                       start_reg = reg - 1;
10435                     }
10436                 }
10437               else
10438                 {
10439                   if (start_reg != reg)
10440                     {
10441                       insn = emit_sfm (reg + 1, start_reg - reg);
10442                       RTX_FRAME_RELATED_P (insn) = 1;
10443                       saved_regs += (start_reg - reg) * 12;
10444                     }
10445                   start_reg = reg - 1;
10446                 }
10447             }
10448
10449           if (start_reg != reg)
10450             {
10451               insn = emit_sfm (reg + 1, start_reg - reg);
10452               saved_regs += (start_reg - reg) * 12;
10453               RTX_FRAME_RELATED_P (insn) = 1;
10454             }
10455         }
10456       if (TARGET_HARD_FLOAT && TARGET_VFP)
10457         {
10458           start_reg = FIRST_VFP_REGNUM;
10459
10460           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10461             {
10462               if ((!regs_ever_live[reg] || call_used_regs[reg])
10463                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10464                 {
10465                   if (start_reg != reg)
10466                     saved_regs += vfp_emit_fstmx (start_reg,
10467                                                   (reg - start_reg) / 2);
10468                   start_reg = reg + 2;
10469                 }
10470             }
10471           if (start_reg != reg)
10472             saved_regs += vfp_emit_fstmx (start_reg,
10473                                           (reg - start_reg) / 2);
10474         }
10475     }
10476
10477   if (frame_pointer_needed)
10478     {
10479       /* Create the new frame pointer.  */
10480       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10481       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10482       RTX_FRAME_RELATED_P (insn) = 1;
10483       
10484       if (IS_NESTED (func_type))
10485         {
10486           /* Recover the static chain register.  */
10487           if (regs_ever_live [3] == 0
10488               || saved_pretend_args)
10489             insn = gen_rtx_REG (SImode, 3);
10490           else /* if (current_function_pretend_args_size == 0) */
10491             {
10492               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10493                                    GEN_INT (4));
10494               insn = gen_rtx_MEM (SImode, insn);
10495             }
10496
10497           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10498           /* Add a USE to stop propagate_one_insn() from barfing.  */
10499           emit_insn (gen_prologue_use (ip_rtx));
10500         }
10501     }
10502
10503   offsets = arm_get_frame_offsets ();
10504   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10505     {
10506       /* This add can produce multiple insns for a large constant, so we
10507          need to get tricky.  */
10508       rtx last = get_last_insn ();
10509
10510       amount = GEN_INT (offsets->saved_args + saved_regs
10511                         - offsets->outgoing_args);
10512
10513       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10514                                     amount));
10515       do
10516         {
10517           last = last ? NEXT_INSN (last) : get_insns ();
10518           RTX_FRAME_RELATED_P (last) = 1;
10519         }
10520       while (last != insn);
10521
10522       /* If the frame pointer is needed, emit a special barrier that
10523          will prevent the scheduler from moving stores to the frame
10524          before the stack adjustment.  */
10525       if (frame_pointer_needed)
10526         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10527                                          hard_frame_pointer_rtx));
10528     }
10529
10530   /* If we are profiling, make sure no instructions are scheduled before
10531      the call to mcount.  Similarly if the user has requested no
10532      scheduling in the prolog.  */
10533   if (current_function_profile || TARGET_NO_SCHED_PRO)
10534     emit_insn (gen_blockage ());
10535
10536   /* If the link register is being kept alive, with the return address in it,
10537      then make sure that it does not get reused by the ce2 pass.  */
10538   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10539     {
10540       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10541       cfun->machine->lr_save_eliminated = 1;
10542     }
10543 }
10544 \f
10545 /* If CODE is 'd', then the X is a condition operand and the instruction
10546    should only be executed if the condition is true.
10547    if CODE is 'D', then the X is a condition operand and the instruction
10548    should only be executed if the condition is false: however, if the mode
10549    of the comparison is CCFPEmode, then always execute the instruction -- we
10550    do this because in these circumstances !GE does not necessarily imply LT;
10551    in these cases the instruction pattern will take care to make sure that
10552    an instruction containing %d will follow, thereby undoing the effects of
10553    doing this instruction unconditionally.
10554    If CODE is 'N' then X is a floating point operand that must be negated
10555    before output.
10556    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10557    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10558 void
10559 arm_print_operand (FILE *stream, rtx x, int code)
10560 {
10561   switch (code)
10562     {
10563     case '@':
10564       fputs (ASM_COMMENT_START, stream);
10565       return;
10566
10567     case '_':
10568       fputs (user_label_prefix, stream);
10569       return;
10570           
10571     case '|':
10572       fputs (REGISTER_PREFIX, stream);
10573       return;
10574
10575     case '?':
10576       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10577         {
10578           if (TARGET_THUMB || current_insn_predicate != NULL)
10579             abort ();
10580
10581           fputs (arm_condition_codes[arm_current_cc], stream);
10582         }
10583       else if (current_insn_predicate)
10584         {
10585           enum arm_cond_code code;
10586
10587           if (TARGET_THUMB)
10588             abort ();
10589
10590           code = get_arm_condition_code (current_insn_predicate);
10591           fputs (arm_condition_codes[code], stream);
10592         }
10593       return;
10594
10595     case 'N':
10596       {
10597         REAL_VALUE_TYPE r;
10598         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10599         r = REAL_VALUE_NEGATE (r);
10600         fprintf (stream, "%s", fp_const_from_val (&r));
10601       }
10602       return;
10603
10604     case 'B':
10605       if (GET_CODE (x) == CONST_INT)
10606         {
10607           HOST_WIDE_INT val;
10608           val = ARM_SIGN_EXTEND (~INTVAL (x));
10609           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10610         }
10611       else
10612         {
10613           putc ('~', stream);
10614           output_addr_const (stream, x);
10615         }
10616       return;
10617
10618     case 'i':
10619       fprintf (stream, "%s", arithmetic_instr (x, 1));
10620       return;
10621
10622     /* Truncate Cirrus shift counts.  */
10623     case 's':
10624       if (GET_CODE (x) == CONST_INT)
10625         {
10626           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10627           return;
10628         }
10629       arm_print_operand (stream, x, 0);
10630       return;
10631
10632     case 'I':
10633       fprintf (stream, "%s", arithmetic_instr (x, 0));
10634       return;
10635
10636     case 'S':
10637       {
10638         HOST_WIDE_INT val;
10639         const char * shift = shift_op (x, &val);
10640
10641         if (shift)
10642           {
10643             fprintf (stream, ", %s ", shift_op (x, &val));
10644             if (val == -1)
10645               arm_print_operand (stream, XEXP (x, 1), 0);
10646             else
10647               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10648           }
10649       }
10650       return;
10651
10652       /* An explanation of the 'Q', 'R' and 'H' register operands:
10653          
10654          In a pair of registers containing a DI or DF value the 'Q'
10655          operand returns the register number of the register containing
10656          the least significant part of the value.  The 'R' operand returns
10657          the register number of the register containing the most
10658          significant part of the value.
10659          
10660          The 'H' operand returns the higher of the two register numbers.
10661          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10662          same as the 'Q' operand, since the most significant part of the
10663          value is held in the lower number register.  The reverse is true
10664          on systems where WORDS_BIG_ENDIAN is false.
10665          
10666          The purpose of these operands is to distinguish between cases
10667          where the endian-ness of the values is important (for example
10668          when they are added together), and cases where the endian-ness
10669          is irrelevant, but the order of register operations is important.
10670          For example when loading a value from memory into a register
10671          pair, the endian-ness does not matter.  Provided that the value
10672          from the lower memory address is put into the lower numbered
10673          register, and the value from the higher address is put into the
10674          higher numbered register, the load will work regardless of whether
10675          the value being loaded is big-wordian or little-wordian.  The
10676          order of the two register loads can matter however, if the address
10677          of the memory location is actually held in one of the registers
10678          being overwritten by the load.  */
10679     case 'Q':
10680       if (REGNO (x) > LAST_ARM_REGNUM)
10681         abort ();
10682       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10683       return;
10684
10685     case 'R':
10686       if (REGNO (x) > LAST_ARM_REGNUM)
10687         abort ();
10688       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10689       return;
10690
10691     case 'H':
10692       if (REGNO (x) > LAST_ARM_REGNUM)
10693         abort ();
10694       asm_fprintf (stream, "%r", REGNO (x) + 1);
10695       return;
10696
10697     case 'm':
10698       asm_fprintf (stream, "%r", 
10699                    GET_CODE (XEXP (x, 0)) == REG
10700                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10701       return;
10702
10703     case 'M':
10704       asm_fprintf (stream, "{%r-%r}",
10705                    REGNO (x),
10706                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10707       return;
10708
10709     case 'd':
10710       /* CONST_TRUE_RTX means always -- that's the default.  */
10711       if (x == const_true_rtx)
10712         return;
10713       
10714       fputs (arm_condition_codes[get_arm_condition_code (x)],
10715              stream);
10716       return;
10717
10718     case 'D':
10719       /* CONST_TRUE_RTX means not always -- ie never.  We shouldn't ever
10720          want to do that.  */
10721       if (x == const_true_rtx)
10722         abort ();
10723
10724       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10725                                  (get_arm_condition_code (x))],
10726              stream);
10727       return;
10728
10729     /* Cirrus registers can be accessed in a variety of ways:
10730          single floating point (f)
10731          double floating point (d)
10732          32bit integer         (fx)
10733          64bit integer         (dx).  */
10734     case 'W':                   /* Cirrus register in F mode.  */
10735     case 'X':                   /* Cirrus register in D mode.  */
10736     case 'Y':                   /* Cirrus register in FX mode.  */
10737     case 'Z':                   /* Cirrus register in DX mode.  */
10738       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10739         abort ();
10740
10741       fprintf (stream, "mv%s%s",
10742                code == 'W' ? "f"
10743                : code == 'X' ? "d"
10744                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10745
10746       return;
10747
10748     /* Print cirrus register in the mode specified by the register's mode.  */
10749     case 'V':
10750       {
10751         int mode = GET_MODE (x);
10752
10753         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10754           abort ();
10755
10756         fprintf (stream, "mv%s%s",
10757                  mode == DFmode ? "d"
10758                  : mode == SImode ? "fx"
10759                  : mode == DImode ? "dx"
10760                  : "f", reg_names[REGNO (x)] + 2);
10761
10762         return;
10763       }
10764
10765     case 'U':
10766       if (GET_CODE (x) != REG
10767           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10768           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10769         /* Bad value for wCG register number.  */
10770         abort ();
10771       else
10772         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10773       return;
10774
10775       /* Print an iWMMXt control register name.  */
10776     case 'w':
10777       if (GET_CODE (x) != CONST_INT
10778           || INTVAL (x) < 0
10779           || INTVAL (x) >= 16)
10780         /* Bad value for wC register number.  */
10781         abort ();
10782       else
10783         {
10784           static const char * wc_reg_names [16] =
10785             {
10786               "wCID",  "wCon",  "wCSSF", "wCASF",
10787               "wC4",   "wC5",   "wC6",   "wC7",
10788               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10789               "wC12",  "wC13",  "wC14",  "wC15"
10790             };
10791           
10792           fprintf (stream, wc_reg_names [INTVAL (x)]);
10793         }
10794       return;
10795
10796       /* Print a VFP double precision register name.  */
10797     case 'P':
10798       {
10799         int mode = GET_MODE (x);
10800         int num;
10801
10802         if (mode != DImode && mode != DFmode)
10803           abort ();
10804
10805         if (GET_CODE (x) != REG
10806             || !IS_VFP_REGNUM (REGNO (x)))
10807           abort ();
10808
10809         num = REGNO(x) - FIRST_VFP_REGNUM;
10810         if (num & 1)
10811           abort ();
10812
10813         fprintf (stream, "d%d", num >> 1);
10814       }
10815       return;
10816
10817     default:
10818       if (x == 0)
10819         abort ();
10820
10821       if (GET_CODE (x) == REG)
10822         asm_fprintf (stream, "%r", REGNO (x));
10823       else if (GET_CODE (x) == MEM)
10824         {
10825           output_memory_reference_mode = GET_MODE (x);
10826           output_address (XEXP (x, 0));
10827         }
10828       else if (GET_CODE (x) == CONST_DOUBLE)
10829         fprintf (stream, "#%s", fp_immediate_constant (x));
10830       else if (GET_CODE (x) == NEG)
10831         abort (); /* This should never happen now.  */
10832       else
10833         {
10834           fputc ('#', stream);
10835           output_addr_const (stream, x);
10836         }
10837     }
10838 }
10839 \f
10840 #ifndef AOF_ASSEMBLER
10841 /* Target hook for assembling integer objects.  The ARM version needs to
10842    handle word-sized values specially.  */
10843 static bool
10844 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10845 {
10846   if (size == UNITS_PER_WORD && aligned_p)
10847     {
10848       fputs ("\t.word\t", asm_out_file);
10849       output_addr_const (asm_out_file, x);
10850
10851       /* Mark symbols as position independent.  We only do this in the
10852          .text segment, not in the .data segment.  */
10853       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10854           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10855         {
10856           if (GET_CODE (x) == SYMBOL_REF
10857               && (CONSTANT_POOL_ADDRESS_P (x)
10858                   || SYMBOL_REF_LOCAL_P (x)))
10859             fputs ("(GOTOFF)", asm_out_file);
10860           else if (GET_CODE (x) == LABEL_REF)
10861             fputs ("(GOTOFF)", asm_out_file);
10862           else
10863             fputs ("(GOT)", asm_out_file);
10864         }
10865       fputc ('\n', asm_out_file);
10866       return true;
10867     }
10868
10869   if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10870     {
10871       int i, units;
10872
10873       if (GET_CODE (x) != CONST_VECTOR)
10874         abort ();
10875
10876       units = CONST_VECTOR_NUNITS (x);
10877
10878       switch (GET_MODE (x))
10879         {
10880         case V2SImode: size = 4; break;
10881         case V4HImode: size = 2; break;
10882         case V8QImode: size = 1; break;
10883         default:
10884           abort ();
10885         }
10886
10887       for (i = 0; i < units; i++)
10888         {
10889           rtx elt;
10890
10891           elt = CONST_VECTOR_ELT (x, i);
10892           assemble_integer
10893             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10894         }
10895
10896       return true;
10897     }
10898
10899   return default_assemble_integer (x, size, aligned_p);
10900 }
10901 #endif
10902 \f
10903 /* A finite state machine takes care of noticing whether or not instructions
10904    can be conditionally executed, and thus decrease execution time and code
10905    size by deleting branch instructions.  The fsm is controlled by
10906    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10907
10908 /* The state of the fsm controlling condition codes are:
10909    0: normal, do nothing special
10910    1: make ASM_OUTPUT_OPCODE not output this instruction
10911    2: make ASM_OUTPUT_OPCODE not output this instruction
10912    3: make instructions conditional
10913    4: make instructions conditional
10914
10915    State transitions (state->state by whom under condition):
10916    0 -> 1 final_prescan_insn if the `target' is a label
10917    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10918    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10919    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10920    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10921           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10922    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10923           (the target insn is arm_target_insn).
10924
10925    If the jump clobbers the conditions then we use states 2 and 4.
10926
10927    A similar thing can be done with conditional return insns.
10928
10929    XXX In case the `target' is an unconditional branch, this conditionalising
10930    of the instructions always reduces code size, but not always execution
10931    time.  But then, I want to reduce the code size to somewhere near what
10932    /bin/cc produces.  */
10933
10934 /* Returns the index of the ARM condition code string in
10935    `arm_condition_codes'.  COMPARISON should be an rtx like
10936    `(eq (...) (...))'.  */
10937 static enum arm_cond_code
10938 get_arm_condition_code (rtx comparison)
10939 {
10940   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10941   int code;
10942   enum rtx_code comp_code = GET_CODE (comparison);
10943
10944   if (GET_MODE_CLASS (mode) != MODE_CC)
10945     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10946                            XEXP (comparison, 1));
10947
10948   switch (mode)
10949     {
10950     case CC_DNEmode: code = ARM_NE; goto dominance;
10951     case CC_DEQmode: code = ARM_EQ; goto dominance;
10952     case CC_DGEmode: code = ARM_GE; goto dominance;
10953     case CC_DGTmode: code = ARM_GT; goto dominance;
10954     case CC_DLEmode: code = ARM_LE; goto dominance;
10955     case CC_DLTmode: code = ARM_LT; goto dominance;
10956     case CC_DGEUmode: code = ARM_CS; goto dominance;
10957     case CC_DGTUmode: code = ARM_HI; goto dominance;
10958     case CC_DLEUmode: code = ARM_LS; goto dominance;
10959     case CC_DLTUmode: code = ARM_CC;
10960
10961     dominance:
10962       if (comp_code != EQ && comp_code != NE)
10963         abort ();
10964
10965       if (comp_code == EQ)
10966         return ARM_INVERSE_CONDITION_CODE (code);
10967       return code;
10968
10969     case CC_NOOVmode:
10970       switch (comp_code)
10971         {
10972         case NE: return ARM_NE;
10973         case EQ: return ARM_EQ;
10974         case GE: return ARM_PL;
10975         case LT: return ARM_MI;
10976         default: abort ();
10977         }
10978
10979     case CC_Zmode:
10980       switch (comp_code)
10981         {
10982         case NE: return ARM_NE;
10983         case EQ: return ARM_EQ;
10984         default: abort ();
10985         }
10986
10987     case CC_Nmode:
10988       switch (comp_code)
10989         {
10990         case NE: return ARM_MI;
10991         case EQ: return ARM_PL;
10992         default: abort ();
10993         }
10994
10995     case CCFPEmode:
10996     case CCFPmode:
10997       /* These encodings assume that AC=1 in the FPA system control
10998          byte.  This allows us to handle all cases except UNEQ and
10999          LTGT.  */
11000       switch (comp_code)
11001         {
11002         case GE: return ARM_GE;
11003         case GT: return ARM_GT;
11004         case LE: return ARM_LS;
11005         case LT: return ARM_MI;
11006         case NE: return ARM_NE;
11007         case EQ: return ARM_EQ;
11008         case ORDERED: return ARM_VC;
11009         case UNORDERED: return ARM_VS;
11010         case UNLT: return ARM_LT;
11011         case UNLE: return ARM_LE;
11012         case UNGT: return ARM_HI;
11013         case UNGE: return ARM_PL;
11014           /* UNEQ and LTGT do not have a representation.  */
11015         case UNEQ: /* Fall through.  */
11016         case LTGT: /* Fall through.  */
11017         default: abort ();
11018         }
11019
11020     case CC_SWPmode:
11021       switch (comp_code)
11022         {
11023         case NE: return ARM_NE;
11024         case EQ: return ARM_EQ;
11025         case GE: return ARM_LE;
11026         case GT: return ARM_LT;
11027         case LE: return ARM_GE;
11028         case LT: return ARM_GT;
11029         case GEU: return ARM_LS;
11030         case GTU: return ARM_CC;
11031         case LEU: return ARM_CS;
11032         case LTU: return ARM_HI;
11033         default: abort ();
11034         }
11035
11036     case CC_Cmode:
11037       switch (comp_code)
11038       {
11039       case LTU: return ARM_CS;
11040       case GEU: return ARM_CC;
11041       default: abort ();
11042       }
11043       
11044     case CCmode:
11045       switch (comp_code)
11046         {
11047         case NE: return ARM_NE;
11048         case EQ: return ARM_EQ;
11049         case GE: return ARM_GE;
11050         case GT: return ARM_GT;
11051         case LE: return ARM_LE;
11052         case LT: return ARM_LT;
11053         case GEU: return ARM_CS;
11054         case GTU: return ARM_HI;
11055         case LEU: return ARM_LS;
11056         case LTU: return ARM_CC;
11057         default: abort ();
11058         }
11059
11060     default: abort ();
11061     }
11062
11063   abort ();
11064 }
11065
11066 void
11067 arm_final_prescan_insn (rtx insn)
11068 {
11069   /* BODY will hold the body of INSN.  */
11070   rtx body = PATTERN (insn);
11071
11072   /* This will be 1 if trying to repeat the trick, and things need to be
11073      reversed if it appears to fail.  */
11074   int reverse = 0;
11075
11076   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11077      taken are clobbered, even if the rtl suggests otherwise.  It also
11078      means that we have to grub around within the jump expression to find
11079      out what the conditions are when the jump isn't taken.  */
11080   int jump_clobbers = 0;
11081   
11082   /* If we start with a return insn, we only succeed if we find another one.  */
11083   int seeking_return = 0;
11084   
11085   /* START_INSN will hold the insn from where we start looking.  This is the
11086      first insn after the following code_label if REVERSE is true.  */
11087   rtx start_insn = insn;
11088
11089   /* If in state 4, check if the target branch is reached, in order to
11090      change back to state 0.  */
11091   if (arm_ccfsm_state == 4)
11092     {
11093       if (insn == arm_target_insn)
11094         {
11095           arm_target_insn = NULL;
11096           arm_ccfsm_state = 0;
11097         }
11098       return;
11099     }
11100
11101   /* If in state 3, it is possible to repeat the trick, if this insn is an
11102      unconditional branch to a label, and immediately following this branch
11103      is the previous target label which is only used once, and the label this
11104      branch jumps to is not too far off.  */
11105   if (arm_ccfsm_state == 3)
11106     {
11107       if (simplejump_p (insn))
11108         {
11109           start_insn = next_nonnote_insn (start_insn);
11110           if (GET_CODE (start_insn) == BARRIER)
11111             {
11112               /* XXX Isn't this always a barrier?  */
11113               start_insn = next_nonnote_insn (start_insn);
11114             }
11115           if (GET_CODE (start_insn) == CODE_LABEL
11116               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11117               && LABEL_NUSES (start_insn) == 1)
11118             reverse = TRUE;
11119           else
11120             return;
11121         }
11122       else if (GET_CODE (body) == RETURN)
11123         {
11124           start_insn = next_nonnote_insn (start_insn);
11125           if (GET_CODE (start_insn) == BARRIER)
11126             start_insn = next_nonnote_insn (start_insn);
11127           if (GET_CODE (start_insn) == CODE_LABEL
11128               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11129               && LABEL_NUSES (start_insn) == 1)
11130             {
11131               reverse = TRUE;
11132               seeking_return = 1;
11133             }
11134           else
11135             return;
11136         }
11137       else
11138         return;
11139     }
11140
11141   if (arm_ccfsm_state != 0 && !reverse)
11142     abort ();
11143   if (GET_CODE (insn) != JUMP_INSN)
11144     return;
11145
11146   /* This jump might be paralleled with a clobber of the condition codes 
11147      the jump should always come first */
11148   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11149     body = XVECEXP (body, 0, 0);
11150
11151   if (reverse
11152       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11153           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11154     {
11155       int insns_skipped;
11156       int fail = FALSE, succeed = FALSE;
11157       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11158       int then_not_else = TRUE;
11159       rtx this_insn = start_insn, label = 0;
11160
11161       /* If the jump cannot be done with one instruction, we cannot 
11162          conditionally execute the instruction in the inverse case.  */
11163       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11164         {
11165           jump_clobbers = 1;
11166           return;
11167         }
11168       
11169       /* Register the insn jumped to.  */
11170       if (reverse)
11171         {
11172           if (!seeking_return)
11173             label = XEXP (SET_SRC (body), 0);
11174         }
11175       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11176         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11177       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11178         {
11179           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11180           then_not_else = FALSE;
11181         }
11182       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11183         seeking_return = 1;
11184       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11185         {
11186           seeking_return = 1;
11187           then_not_else = FALSE;
11188         }
11189       else
11190         abort ();
11191
11192       /* See how many insns this branch skips, and what kind of insns.  If all
11193          insns are okay, and the label or unconditional branch to the same
11194          label is not too far away, succeed.  */
11195       for (insns_skipped = 0;
11196            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11197         {
11198           rtx scanbody;
11199
11200           this_insn = next_nonnote_insn (this_insn);
11201           if (!this_insn)
11202             break;
11203
11204           switch (GET_CODE (this_insn))
11205             {
11206             case CODE_LABEL:
11207               /* Succeed if it is the target label, otherwise fail since
11208                  control falls in from somewhere else.  */
11209               if (this_insn == label)
11210                 {
11211                   if (jump_clobbers)
11212                     {
11213                       arm_ccfsm_state = 2;
11214                       this_insn = next_nonnote_insn (this_insn);
11215                     }
11216                   else
11217                     arm_ccfsm_state = 1;
11218                   succeed = TRUE;
11219                 }
11220               else
11221                 fail = TRUE;
11222               break;
11223
11224             case BARRIER:
11225               /* Succeed if the following insn is the target label.
11226                  Otherwise fail.  
11227                  If return insns are used then the last insn in a function 
11228                  will be a barrier.  */
11229               this_insn = next_nonnote_insn (this_insn);
11230               if (this_insn && this_insn == label)
11231                 {
11232                   if (jump_clobbers)
11233                     {
11234                       arm_ccfsm_state = 2;
11235                       this_insn = next_nonnote_insn (this_insn);
11236                     }
11237                   else
11238                     arm_ccfsm_state = 1;
11239                   succeed = TRUE;
11240                 }
11241               else
11242                 fail = TRUE;
11243               break;
11244
11245             case CALL_INSN:
11246               /* The AAPCS says that conditional calls should not be
11247                  used since they make interworking inefficient (the
11248                  linker can't transform BL<cond> into BLX).  That's
11249                  only a problem if the machine has BLX.  */
11250               if (arm_arch5)
11251                 {
11252                   fail = TRUE;
11253                   break;
11254                 }
11255
11256               /* Succeed if the following insn is the target label, or
11257                  if the following two insns are a barrier and the
11258                  target label.  */
11259               this_insn = next_nonnote_insn (this_insn);
11260               if (this_insn && GET_CODE (this_insn) == BARRIER)
11261                 this_insn = next_nonnote_insn (this_insn);
11262
11263               if (this_insn && this_insn == label
11264                   && insns_skipped < max_insns_skipped)
11265                 {
11266                   if (jump_clobbers)
11267                     {
11268                       arm_ccfsm_state = 2;
11269                       this_insn = next_nonnote_insn (this_insn);
11270                     }
11271                   else
11272                     arm_ccfsm_state = 1;
11273                   succeed = TRUE;
11274                 }
11275               else
11276                 fail = TRUE;
11277               break;
11278
11279             case JUMP_INSN:
11280               /* If this is an unconditional branch to the same label, succeed.
11281                  If it is to another label, do nothing.  If it is conditional,
11282                  fail.  */
11283               /* XXX Probably, the tests for SET and the PC are
11284                  unnecessary.  */
11285
11286               scanbody = PATTERN (this_insn);
11287               if (GET_CODE (scanbody) == SET
11288                   && GET_CODE (SET_DEST (scanbody)) == PC)
11289                 {
11290                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11291                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11292                     {
11293                       arm_ccfsm_state = 2;
11294                       succeed = TRUE;
11295                     }
11296                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11297                     fail = TRUE;
11298                 }
11299               /* Fail if a conditional return is undesirable (eg on a
11300                  StrongARM), but still allow this if optimizing for size.  */
11301               else if (GET_CODE (scanbody) == RETURN
11302                        && !use_return_insn (TRUE, NULL)
11303                        && !optimize_size)
11304                 fail = TRUE;
11305               else if (GET_CODE (scanbody) == RETURN
11306                        && seeking_return)
11307                 {
11308                   arm_ccfsm_state = 2;
11309                   succeed = TRUE;
11310                 }
11311               else if (GET_CODE (scanbody) == PARALLEL)
11312                 {
11313                   switch (get_attr_conds (this_insn))
11314                     {
11315                     case CONDS_NOCOND:
11316                       break;
11317                     default:
11318                       fail = TRUE;
11319                       break;
11320                     }
11321                 }
11322               else
11323                 fail = TRUE;    /* Unrecognized jump (eg epilogue).  */
11324
11325               break;
11326
11327             case INSN:
11328               /* Instructions using or affecting the condition codes make it
11329                  fail.  */
11330               scanbody = PATTERN (this_insn);
11331               if (!(GET_CODE (scanbody) == SET
11332                     || GET_CODE (scanbody) == PARALLEL)
11333                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11334                 fail = TRUE;
11335
11336               /* A conditional cirrus instruction must be followed by
11337                  a non Cirrus instruction.  However, since we
11338                  conditionalize instructions in this function and by
11339                  the time we get here we can't add instructions
11340                  (nops), because shorten_branches() has already been
11341                  called, we will disable conditionalizing Cirrus
11342                  instructions to be safe.  */
11343               if (GET_CODE (scanbody) != USE
11344                   && GET_CODE (scanbody) != CLOBBER
11345                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11346                 fail = TRUE;
11347               break;
11348
11349             default:
11350               break;
11351             }
11352         }
11353       if (succeed)
11354         {
11355           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11356             arm_target_label = CODE_LABEL_NUMBER (label);
11357           else if (seeking_return || arm_ccfsm_state == 2)
11358             {
11359               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11360                 {
11361                   this_insn = next_nonnote_insn (this_insn);
11362                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11363                                     || GET_CODE (this_insn) == CODE_LABEL))
11364                     abort ();
11365                 }
11366               if (!this_insn)
11367                 {
11368                   /* Oh, dear! we ran off the end.. give up.  */
11369                   recog (PATTERN (insn), insn, NULL);
11370                   arm_ccfsm_state = 0;
11371                   arm_target_insn = NULL;
11372                   return;
11373                 }
11374               arm_target_insn = this_insn;
11375             }
11376           else
11377             abort ();
11378           if (jump_clobbers)
11379             {
11380               if (reverse)
11381                 abort ();
11382               arm_current_cc = 
11383                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11384                                                             0), 0), 1));
11385               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11386                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11387               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11388                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11389             }
11390           else
11391             {
11392               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11393                  what it was.  */
11394               if (!reverse)
11395                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11396                                                                0));
11397             }
11398
11399           if (reverse || then_not_else)
11400             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11401         }
11402       
11403       /* Restore recog_data (getting the attributes of other insns can
11404          destroy this array, but final.c assumes that it remains intact
11405          across this call; since the insn has been recognized already we
11406          call recog direct).  */
11407       recog (PATTERN (insn), insn, NULL);
11408     }
11409 }
11410
11411 /* Returns true if REGNO is a valid register
11412    for holding a quantity of tyoe MODE.  */
11413 int
11414 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11415 {
11416   if (GET_MODE_CLASS (mode) == MODE_CC)
11417     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11418   
11419   if (TARGET_THUMB)
11420     /* For the Thumb we only allow values bigger than SImode in
11421        registers 0 - 6, so that there is always a second low
11422        register available to hold the upper part of the value.
11423        We probably we ought to ensure that the register is the
11424        start of an even numbered register pair.  */
11425     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11426
11427   if (IS_CIRRUS_REGNUM (regno))
11428     /* We have outlawed SI values in Cirrus registers because they
11429        reside in the lower 32 bits, but SF values reside in the
11430        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11431        even split the registers into pairs because Cirrus SI values
11432        get sign extended to 64bits-- aldyh.  */
11433     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11434
11435   if (IS_VFP_REGNUM (regno))
11436     {
11437       if (mode == SFmode || mode == SImode)
11438         return TRUE;
11439
11440       /* DFmode values are only valid in even register pairs.  */
11441       if (mode == DFmode)
11442         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11443       return FALSE;
11444     }
11445
11446   if (IS_IWMMXT_GR_REGNUM (regno))
11447     return mode == SImode;
11448
11449   if (IS_IWMMXT_REGNUM (regno))
11450     return VALID_IWMMXT_REG_MODE (mode);
11451
11452   /* We allow any value to be stored in the general registers.
11453      Restrict doubleword quantities to even register pairs so that we can
11454      use ldrd.  */
11455   if (regno <= LAST_ARM_REGNUM)
11456     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11457
11458   if (   regno == FRAME_POINTER_REGNUM
11459       || regno == ARG_POINTER_REGNUM)
11460     /* We only allow integers in the fake hard registers.  */
11461     return GET_MODE_CLASS (mode) == MODE_INT;
11462
11463   /* The only registers left are the FPA registers
11464      which we only allow to hold FP values.  */
11465   return GET_MODE_CLASS (mode) == MODE_FLOAT
11466     && regno >= FIRST_FPA_REGNUM
11467     && regno <= LAST_FPA_REGNUM;
11468 }
11469
11470 int
11471 arm_regno_class (int regno)
11472 {
11473   if (TARGET_THUMB)
11474     {
11475       if (regno == STACK_POINTER_REGNUM)
11476         return STACK_REG;
11477       if (regno == CC_REGNUM)
11478         return CC_REG;
11479       if (regno < 8)
11480         return LO_REGS;
11481       return HI_REGS;
11482     }
11483
11484   if (   regno <= LAST_ARM_REGNUM
11485       || regno == FRAME_POINTER_REGNUM
11486       || regno == ARG_POINTER_REGNUM)
11487     return GENERAL_REGS;
11488   
11489   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11490     return NO_REGS;
11491
11492   if (IS_CIRRUS_REGNUM (regno))
11493     return CIRRUS_REGS;
11494
11495   if (IS_VFP_REGNUM (regno))
11496     return VFP_REGS;
11497
11498   if (IS_IWMMXT_REGNUM (regno))
11499     return IWMMXT_REGS;
11500
11501   if (IS_IWMMXT_GR_REGNUM (regno))
11502     return IWMMXT_GR_REGS;
11503
11504   return FPA_REGS;
11505 }
11506
11507 /* Handle a special case when computing the offset
11508    of an argument from the frame pointer.  */
11509 int
11510 arm_debugger_arg_offset (int value, rtx addr)
11511 {
11512   rtx insn;
11513
11514   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11515   if (value != 0)
11516     return 0;
11517
11518   /* We can only cope with the case where the address is held in a register.  */
11519   if (GET_CODE (addr) != REG)
11520     return 0;
11521
11522   /* If we are using the frame pointer to point at the argument, then
11523      an offset of 0 is correct.  */
11524   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11525     return 0;
11526   
11527   /* If we are using the stack pointer to point at the
11528      argument, then an offset of 0 is correct.  */
11529   if ((TARGET_THUMB || !frame_pointer_needed)
11530       && REGNO (addr) == SP_REGNUM)
11531     return 0;
11532   
11533   /* Oh dear.  The argument is pointed to by a register rather
11534      than being held in a register, or being stored at a known
11535      offset from the frame pointer.  Since GDB only understands
11536      those two kinds of argument we must translate the address
11537      held in the register into an offset from the frame pointer.
11538      We do this by searching through the insns for the function
11539      looking to see where this register gets its value.  If the
11540      register is initialized from the frame pointer plus an offset
11541      then we are in luck and we can continue, otherwise we give up.
11542      
11543      This code is exercised by producing debugging information
11544      for a function with arguments like this:
11545      
11546            double func (double a, double b, int c, double d) {return d;}
11547      
11548      Without this code the stab for parameter 'd' will be set to
11549      an offset of 0 from the frame pointer, rather than 8.  */
11550
11551   /* The if() statement says:
11552
11553      If the insn is a normal instruction
11554      and if the insn is setting the value in a register
11555      and if the register being set is the register holding the address of the argument
11556      and if the address is computing by an addition
11557      that involves adding to a register
11558      which is the frame pointer
11559      a constant integer
11560
11561      then...  */
11562   
11563   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11564     {
11565       if (   GET_CODE (insn) == INSN 
11566           && GET_CODE (PATTERN (insn)) == SET
11567           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11568           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11569           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11570           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11571           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11572              )
11573         {
11574           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11575           
11576           break;
11577         }
11578     }
11579   
11580   if (value == 0)
11581     {
11582       debug_rtx (addr);
11583       warning ("unable to compute real location of stacked parameter");
11584       value = 8; /* XXX magic hack */
11585     }
11586
11587   return value;
11588 }
11589 \f
11590 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11591   do                                                                    \
11592     {                                                                   \
11593       if ((MASK) & insn_flags)                                          \
11594         builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, NULL_TREE);        \
11595     }                                                                   \
11596   while (0)
11597
11598 struct builtin_description
11599 {
11600   const unsigned int       mask;
11601   const enum insn_code     icode;
11602   const char * const       name;
11603   const enum arm_builtins  code;
11604   const enum rtx_code      comparison;
11605   const unsigned int       flag;
11606 };
11607
11608 static const struct builtin_description bdesc_2arg[] =
11609 {
11610 #define IWMMXT_BUILTIN(code, string, builtin) \
11611   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11612     ARM_BUILTIN_##builtin, 0, 0 },
11613
11614   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11615   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11616   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11617   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11618   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11619   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11620   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11621   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11622   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11623   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11624   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11625   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11626   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11627   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11628   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11629   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11630   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11631   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11632   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11633   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11634   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11635   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11636   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11637   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11638   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11639   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11640   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11641   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11642   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11643   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11644   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11645   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11646   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11647   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11648   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11649   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11650   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11651   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11652   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11653   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11654   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11655   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11656   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11657   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11658   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11659   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11660   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11661   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11662   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11663   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11664   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11665   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11666   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11667   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11668   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11669   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11670   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11671   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11672
11673 #define IWMMXT_BUILTIN2(code, builtin) \
11674   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11675   
11676   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11677   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11678   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11679   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11680   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11681   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11682   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11683   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11684   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11685   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11686   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11687   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11688   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11689   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11690   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11691   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11692   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11693   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11694   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11695   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11696   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11697   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11698   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11699   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11700   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11701   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11702   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11703   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11704   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11705   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11706   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11707   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11708 };
11709
11710 static const struct builtin_description bdesc_1arg[] =
11711 {
11712   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11713   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11714   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11715   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11716   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11717   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11718   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11719   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11720   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11721   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11722   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11723   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11724   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11725   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11726   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11727   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11728   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11729   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11730 };
11731
11732 /* Set up all the iWMMXt builtins.  This is
11733    not called if TARGET_IWMMXT is zero.  */
11734
11735 static void
11736 arm_init_iwmmxt_builtins (void)
11737 {
11738   const struct builtin_description * d;
11739   size_t i;
11740   tree endlink = void_list_node;
11741
11742   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11743   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11744   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11745
11746   tree int_ftype_int
11747     = build_function_type (integer_type_node,
11748                            tree_cons (NULL_TREE, integer_type_node, endlink));
11749   tree v8qi_ftype_v8qi_v8qi_int
11750     = build_function_type (V8QI_type_node,
11751                            tree_cons (NULL_TREE, V8QI_type_node,
11752                                       tree_cons (NULL_TREE, V8QI_type_node,
11753                                                  tree_cons (NULL_TREE,
11754                                                             integer_type_node,
11755                                                             endlink))));
11756   tree v4hi_ftype_v4hi_int
11757     = build_function_type (V4HI_type_node,
11758                            tree_cons (NULL_TREE, V4HI_type_node,
11759                                       tree_cons (NULL_TREE, integer_type_node,
11760                                                  endlink)));
11761   tree v2si_ftype_v2si_int
11762     = build_function_type (V2SI_type_node,
11763                            tree_cons (NULL_TREE, V2SI_type_node,
11764                                       tree_cons (NULL_TREE, integer_type_node,
11765                                                  endlink)));
11766   tree v2si_ftype_di_di
11767     = build_function_type (V2SI_type_node,
11768                            tree_cons (NULL_TREE, long_long_integer_type_node,
11769                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11770                                                  endlink)));
11771   tree di_ftype_di_int
11772     = build_function_type (long_long_integer_type_node,
11773                            tree_cons (NULL_TREE, long_long_integer_type_node,
11774                                       tree_cons (NULL_TREE, integer_type_node,
11775                                                  endlink)));
11776   tree di_ftype_di_int_int
11777     = build_function_type (long_long_integer_type_node,
11778                            tree_cons (NULL_TREE, long_long_integer_type_node,
11779                                       tree_cons (NULL_TREE, integer_type_node,
11780                                                  tree_cons (NULL_TREE,
11781                                                             integer_type_node,
11782                                                             endlink))));
11783   tree int_ftype_v8qi
11784     = build_function_type (integer_type_node,
11785                            tree_cons (NULL_TREE, V8QI_type_node,
11786                                       endlink));
11787   tree int_ftype_v4hi
11788     = build_function_type (integer_type_node,
11789                            tree_cons (NULL_TREE, V4HI_type_node,
11790                                       endlink));
11791   tree int_ftype_v2si
11792     = build_function_type (integer_type_node,
11793                            tree_cons (NULL_TREE, V2SI_type_node,
11794                                       endlink));
11795   tree int_ftype_v8qi_int
11796     = build_function_type (integer_type_node,
11797                            tree_cons (NULL_TREE, V8QI_type_node,
11798                                       tree_cons (NULL_TREE, integer_type_node,
11799                                                  endlink)));
11800   tree int_ftype_v4hi_int
11801     = build_function_type (integer_type_node,
11802                            tree_cons (NULL_TREE, V4HI_type_node,
11803                                       tree_cons (NULL_TREE, integer_type_node,
11804                                                  endlink)));
11805   tree int_ftype_v2si_int
11806     = build_function_type (integer_type_node,
11807                            tree_cons (NULL_TREE, V2SI_type_node,
11808                                       tree_cons (NULL_TREE, integer_type_node,
11809                                                  endlink)));
11810   tree v8qi_ftype_v8qi_int_int
11811     = build_function_type (V8QI_type_node,
11812                            tree_cons (NULL_TREE, V8QI_type_node,
11813                                       tree_cons (NULL_TREE, integer_type_node,
11814                                                  tree_cons (NULL_TREE,
11815                                                             integer_type_node,
11816                                                             endlink))));
11817   tree v4hi_ftype_v4hi_int_int
11818     = build_function_type (V4HI_type_node,
11819                            tree_cons (NULL_TREE, V4HI_type_node,
11820                                       tree_cons (NULL_TREE, integer_type_node,
11821                                                  tree_cons (NULL_TREE,
11822                                                             integer_type_node,
11823                                                             endlink))));
11824   tree v2si_ftype_v2si_int_int
11825     = build_function_type (V2SI_type_node,
11826                            tree_cons (NULL_TREE, V2SI_type_node,
11827                                       tree_cons (NULL_TREE, integer_type_node,
11828                                                  tree_cons (NULL_TREE,
11829                                                             integer_type_node,
11830                                                             endlink))));
11831   /* Miscellaneous.  */
11832   tree v8qi_ftype_v4hi_v4hi
11833     = build_function_type (V8QI_type_node,
11834                            tree_cons (NULL_TREE, V4HI_type_node,
11835                                       tree_cons (NULL_TREE, V4HI_type_node,
11836                                                  endlink)));
11837   tree v4hi_ftype_v2si_v2si
11838     = build_function_type (V4HI_type_node,
11839                            tree_cons (NULL_TREE, V2SI_type_node,
11840                                       tree_cons (NULL_TREE, V2SI_type_node,
11841                                                  endlink)));
11842   tree v2si_ftype_v4hi_v4hi
11843     = build_function_type (V2SI_type_node,
11844                            tree_cons (NULL_TREE, V4HI_type_node,
11845                                       tree_cons (NULL_TREE, V4HI_type_node,
11846                                                  endlink)));
11847   tree v2si_ftype_v8qi_v8qi
11848     = build_function_type (V2SI_type_node,
11849                            tree_cons (NULL_TREE, V8QI_type_node,
11850                                       tree_cons (NULL_TREE, V8QI_type_node,
11851                                                  endlink)));
11852   tree v4hi_ftype_v4hi_di
11853     = build_function_type (V4HI_type_node,
11854                            tree_cons (NULL_TREE, V4HI_type_node,
11855                                       tree_cons (NULL_TREE,
11856                                                  long_long_integer_type_node,
11857                                                  endlink)));
11858   tree v2si_ftype_v2si_di
11859     = build_function_type (V2SI_type_node,
11860                            tree_cons (NULL_TREE, V2SI_type_node,
11861                                       tree_cons (NULL_TREE,
11862                                                  long_long_integer_type_node,
11863                                                  endlink)));
11864   tree void_ftype_int_int
11865     = build_function_type (void_type_node,
11866                            tree_cons (NULL_TREE, integer_type_node,
11867                                       tree_cons (NULL_TREE, integer_type_node,
11868                                                  endlink)));
11869   tree di_ftype_void
11870     = build_function_type (long_long_unsigned_type_node, endlink);
11871   tree di_ftype_v8qi
11872     = build_function_type (long_long_integer_type_node,
11873                            tree_cons (NULL_TREE, V8QI_type_node,
11874                                       endlink));
11875   tree di_ftype_v4hi
11876     = build_function_type (long_long_integer_type_node,
11877                            tree_cons (NULL_TREE, V4HI_type_node,
11878                                       endlink));
11879   tree di_ftype_v2si
11880     = build_function_type (long_long_integer_type_node,
11881                            tree_cons (NULL_TREE, V2SI_type_node,
11882                                       endlink));
11883   tree v2si_ftype_v4hi
11884     = build_function_type (V2SI_type_node,
11885                            tree_cons (NULL_TREE, V4HI_type_node,
11886                                       endlink));
11887   tree v4hi_ftype_v8qi
11888     = build_function_type (V4HI_type_node,
11889                            tree_cons (NULL_TREE, V8QI_type_node,
11890                                       endlink));
11891
11892   tree di_ftype_di_v4hi_v4hi
11893     = build_function_type (long_long_unsigned_type_node,
11894                            tree_cons (NULL_TREE,
11895                                       long_long_unsigned_type_node,
11896                                       tree_cons (NULL_TREE, V4HI_type_node,
11897                                                  tree_cons (NULL_TREE,
11898                                                             V4HI_type_node,
11899                                                             endlink))));
11900
11901   tree di_ftype_v4hi_v4hi
11902     = build_function_type (long_long_unsigned_type_node,
11903                            tree_cons (NULL_TREE, V4HI_type_node,
11904                                       tree_cons (NULL_TREE, V4HI_type_node,
11905                                                  endlink)));
11906
11907   /* Normal vector binops.  */
11908   tree v8qi_ftype_v8qi_v8qi
11909     = build_function_type (V8QI_type_node,
11910                            tree_cons (NULL_TREE, V8QI_type_node,
11911                                       tree_cons (NULL_TREE, V8QI_type_node,
11912                                                  endlink)));
11913   tree v4hi_ftype_v4hi_v4hi
11914     = build_function_type (V4HI_type_node,
11915                            tree_cons (NULL_TREE, V4HI_type_node,
11916                                       tree_cons (NULL_TREE, V4HI_type_node,
11917                                                  endlink)));
11918   tree v2si_ftype_v2si_v2si
11919     = build_function_type (V2SI_type_node,
11920                            tree_cons (NULL_TREE, V2SI_type_node,
11921                                       tree_cons (NULL_TREE, V2SI_type_node,
11922                                                  endlink)));
11923   tree di_ftype_di_di
11924     = build_function_type (long_long_unsigned_type_node,
11925                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11926                                       tree_cons (NULL_TREE,
11927                                                  long_long_unsigned_type_node,
11928                                                  endlink)));
11929
11930   /* Add all builtins that are more or less simple operations on two
11931      operands.  */
11932   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11933     {
11934       /* Use one of the operands; the target can have a different mode for
11935          mask-generating compares.  */
11936       enum machine_mode mode;
11937       tree type;
11938
11939       if (d->name == 0)
11940         continue;
11941
11942       mode = insn_data[d->icode].operand[1].mode;
11943
11944       switch (mode)
11945         {
11946         case V8QImode:
11947           type = v8qi_ftype_v8qi_v8qi;
11948           break;
11949         case V4HImode:
11950           type = v4hi_ftype_v4hi_v4hi;
11951           break;
11952         case V2SImode:
11953           type = v2si_ftype_v2si_v2si;
11954           break;
11955         case DImode:
11956           type = di_ftype_di_di;
11957           break;
11958
11959         default:
11960           abort ();
11961         }
11962
11963       def_mbuiltin (d->mask, d->name, type, d->code);
11964     }
11965
11966   /* Add the remaining MMX insns with somewhat more complicated types.  */
11967   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11968   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11969   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11970
11971   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11972   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11974   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11975   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11976   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11977
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11982   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11984
11985   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11986   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11987   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11989   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11990   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11991
11992   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11994   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11996   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11997   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11998
11999   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12000
12001   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12002   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12003   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12004   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12005
12006   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12007   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12008   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12009   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12010   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12011   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12012   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12013   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12014   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12015
12016   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12017   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12018   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12019
12020   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12021   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12022   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12023
12024   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12025   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12026   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12027   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12028   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12029   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12030
12031   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12032   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12033   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12034   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12035   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12036   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12037   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12038   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12039   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12040   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12041   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12042   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12043
12044   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12045   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12046   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12047   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12048
12049   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12050   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12051   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12052   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12053   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12054   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12055   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12056 }
12057
12058 static void
12059 arm_init_builtins (void)
12060 {
12061   if (TARGET_REALLY_IWMMXT)
12062     arm_init_iwmmxt_builtins ();
12063 }
12064
12065 /* Errors in the source file can cause expand_expr to return const0_rtx
12066    where we expect a vector.  To avoid crashing, use one of the vector
12067    clear instructions.  */
12068
12069 static rtx
12070 safe_vector_operand (rtx x, enum machine_mode mode)
12071 {
12072   if (x != const0_rtx)
12073     return x;
12074   x = gen_reg_rtx (mode);
12075
12076   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12077                                : gen_rtx_SUBREG (DImode, x, 0)));
12078   return x;
12079 }
12080
12081 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12082
12083 static rtx
12084 arm_expand_binop_builtin (enum insn_code icode,
12085                           tree arglist, rtx target)
12086 {
12087   rtx pat;
12088   tree arg0 = TREE_VALUE (arglist);
12089   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12090   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12091   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12092   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12093   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12094   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12095
12096   if (VECTOR_MODE_P (mode0))
12097     op0 = safe_vector_operand (op0, mode0);
12098   if (VECTOR_MODE_P (mode1))
12099     op1 = safe_vector_operand (op1, mode1);
12100
12101   if (! target
12102       || GET_MODE (target) != tmode
12103       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12104     target = gen_reg_rtx (tmode);
12105
12106   /* In case the insn wants input operands in modes different from
12107      the result, abort.  */
12108   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12109     abort ();
12110
12111   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12112     op0 = copy_to_mode_reg (mode0, op0);
12113   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12114     op1 = copy_to_mode_reg (mode1, op1);
12115
12116   pat = GEN_FCN (icode) (target, op0, op1);
12117   if (! pat)
12118     return 0;
12119   emit_insn (pat);
12120   return target;
12121 }
12122
12123 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12124
12125 static rtx
12126 arm_expand_unop_builtin (enum insn_code icode,
12127                          tree arglist, rtx target, int do_load)
12128 {
12129   rtx pat;
12130   tree arg0 = TREE_VALUE (arglist);
12131   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12132   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12133   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12134
12135   if (! target
12136       || GET_MODE (target) != tmode
12137       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12138     target = gen_reg_rtx (tmode);
12139   if (do_load)
12140     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12141   else
12142     {
12143       if (VECTOR_MODE_P (mode0))
12144         op0 = safe_vector_operand (op0, mode0);
12145
12146       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12147         op0 = copy_to_mode_reg (mode0, op0);
12148     }
12149
12150   pat = GEN_FCN (icode) (target, op0);
12151   if (! pat)
12152     return 0;
12153   emit_insn (pat);
12154   return target;
12155 }
12156
12157 /* Expand an expression EXP that calls a built-in function,
12158    with result going to TARGET if that's convenient
12159    (and in mode MODE if that's convenient).
12160    SUBTARGET may be used as the target for computing one of EXP's operands.
12161    IGNORE is nonzero if the value is to be ignored.  */
12162
12163 static rtx
12164 arm_expand_builtin (tree exp,
12165                     rtx target,
12166                     rtx subtarget ATTRIBUTE_UNUSED,
12167                     enum machine_mode mode ATTRIBUTE_UNUSED,
12168                     int ignore ATTRIBUTE_UNUSED)
12169 {
12170   const struct builtin_description * d;
12171   enum insn_code    icode;
12172   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12173   tree              arglist = TREE_OPERAND (exp, 1);
12174   tree              arg0;
12175   tree              arg1;
12176   tree              arg2;
12177   rtx               op0;
12178   rtx               op1;
12179   rtx               op2;
12180   rtx               pat;
12181   int               fcode = DECL_FUNCTION_CODE (fndecl);
12182   size_t            i;
12183   enum machine_mode tmode;
12184   enum machine_mode mode0;
12185   enum machine_mode mode1;
12186   enum machine_mode mode2;
12187
12188   switch (fcode)
12189     {
12190     case ARM_BUILTIN_TEXTRMSB:
12191     case ARM_BUILTIN_TEXTRMUB:
12192     case ARM_BUILTIN_TEXTRMSH:
12193     case ARM_BUILTIN_TEXTRMUH:
12194     case ARM_BUILTIN_TEXTRMSW:
12195     case ARM_BUILTIN_TEXTRMUW:
12196       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12197                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12198                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12199                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12200                : CODE_FOR_iwmmxt_textrmw);
12201
12202       arg0 = TREE_VALUE (arglist);
12203       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12204       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12205       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12206       tmode = insn_data[icode].operand[0].mode;
12207       mode0 = insn_data[icode].operand[1].mode;
12208       mode1 = insn_data[icode].operand[2].mode;
12209
12210       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12211         op0 = copy_to_mode_reg (mode0, op0);
12212       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12213         {
12214           /* @@@ better error message */
12215           error ("selector must be an immediate");
12216           return gen_reg_rtx (tmode);
12217         }
12218       if (target == 0
12219           || GET_MODE (target) != tmode
12220           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12221         target = gen_reg_rtx (tmode);
12222       pat = GEN_FCN (icode) (target, op0, op1);
12223       if (! pat)
12224         return 0;
12225       emit_insn (pat);
12226       return target;
12227
12228     case ARM_BUILTIN_TINSRB:
12229     case ARM_BUILTIN_TINSRH:
12230     case ARM_BUILTIN_TINSRW:
12231       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12232                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12233                : CODE_FOR_iwmmxt_tinsrw);
12234       arg0 = TREE_VALUE (arglist);
12235       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12236       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12237       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12238       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12239       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12240       tmode = insn_data[icode].operand[0].mode;
12241       mode0 = insn_data[icode].operand[1].mode;
12242       mode1 = insn_data[icode].operand[2].mode;
12243       mode2 = insn_data[icode].operand[3].mode;
12244
12245       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12246         op0 = copy_to_mode_reg (mode0, op0);
12247       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12248         op1 = copy_to_mode_reg (mode1, op1);
12249       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12250         {
12251           /* @@@ better error message */
12252           error ("selector must be an immediate");
12253           return const0_rtx;
12254         }
12255       if (target == 0
12256           || GET_MODE (target) != tmode
12257           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12258         target = gen_reg_rtx (tmode);
12259       pat = GEN_FCN (icode) (target, op0, op1, op2);
12260       if (! pat)
12261         return 0;
12262       emit_insn (pat);
12263       return target;
12264
12265     case ARM_BUILTIN_SETWCX:
12266       arg0 = TREE_VALUE (arglist);
12267       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12268       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12269       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12270       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12271       return 0;
12272
12273     case ARM_BUILTIN_GETWCX:
12274       arg0 = TREE_VALUE (arglist);
12275       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12276       target = gen_reg_rtx (SImode);
12277       emit_insn (gen_iwmmxt_tmrc (target, op0));
12278       return target;
12279
12280     case ARM_BUILTIN_WSHUFH:
12281       icode = CODE_FOR_iwmmxt_wshufh;
12282       arg0 = TREE_VALUE (arglist);
12283       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12284       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12285       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12286       tmode = insn_data[icode].operand[0].mode;
12287       mode1 = insn_data[icode].operand[1].mode;
12288       mode2 = insn_data[icode].operand[2].mode;
12289
12290       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12291         op0 = copy_to_mode_reg (mode1, op0);
12292       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12293         {
12294           /* @@@ better error message */
12295           error ("mask must be an immediate");
12296           return const0_rtx;
12297         }
12298       if (target == 0
12299           || GET_MODE (target) != tmode
12300           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12301         target = gen_reg_rtx (tmode);
12302       pat = GEN_FCN (icode) (target, op0, op1);
12303       if (! pat)
12304         return 0;
12305       emit_insn (pat);
12306       return target;
12307
12308     case ARM_BUILTIN_WSADB:
12309       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12310     case ARM_BUILTIN_WSADH:
12311       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12312     case ARM_BUILTIN_WSADBZ:
12313       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12314     case ARM_BUILTIN_WSADHZ:
12315       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12316
12317       /* Several three-argument builtins.  */
12318     case ARM_BUILTIN_WMACS:
12319     case ARM_BUILTIN_WMACU:
12320     case ARM_BUILTIN_WALIGN:
12321     case ARM_BUILTIN_TMIA:
12322     case ARM_BUILTIN_TMIAPH:
12323     case ARM_BUILTIN_TMIATT:
12324     case ARM_BUILTIN_TMIATB:
12325     case ARM_BUILTIN_TMIABT:
12326     case ARM_BUILTIN_TMIABB:
12327       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12328                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12329                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12330                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12331                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12332                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12333                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12334                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12335                : CODE_FOR_iwmmxt_walign);
12336       arg0 = TREE_VALUE (arglist);
12337       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12338       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12339       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12340       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12341       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12342       tmode = insn_data[icode].operand[0].mode;
12343       mode0 = insn_data[icode].operand[1].mode;
12344       mode1 = insn_data[icode].operand[2].mode;
12345       mode2 = insn_data[icode].operand[3].mode;
12346
12347       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12348         op0 = copy_to_mode_reg (mode0, op0);
12349       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12350         op1 = copy_to_mode_reg (mode1, op1);
12351       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12352         op2 = copy_to_mode_reg (mode2, op2);
12353       if (target == 0
12354           || GET_MODE (target) != tmode
12355           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12356         target = gen_reg_rtx (tmode);
12357       pat = GEN_FCN (icode) (target, op0, op1, op2);
12358       if (! pat)
12359         return 0;
12360       emit_insn (pat);
12361       return target;
12362       
12363     case ARM_BUILTIN_WZERO:
12364       target = gen_reg_rtx (DImode);
12365       emit_insn (gen_iwmmxt_clrdi (target));
12366       return target;
12367
12368     default:
12369       break;
12370     }
12371
12372   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12373     if (d->code == (const enum arm_builtins) fcode)
12374       return arm_expand_binop_builtin (d->icode, arglist, target);
12375
12376   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12377     if (d->code == (const enum arm_builtins) fcode)
12378       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12379
12380   /* @@@ Should really do something sensible here.  */
12381   return NULL_RTX;
12382 }
12383 \f
12384 /* Recursively search through all of the blocks in a function
12385    checking to see if any of the variables created in that
12386    function match the RTX called 'orig'.  If they do then
12387    replace them with the RTX called 'new'.  */
12388 static void
12389 replace_symbols_in_block (tree block, rtx orig, rtx new)
12390 {
12391   for (; block; block = BLOCK_CHAIN (block))
12392     {
12393       tree sym;
12394       
12395       if (!TREE_USED (block))
12396         continue;
12397
12398       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12399         {
12400           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12401               || DECL_IGNORED_P (sym)
12402               || TREE_CODE (sym) != VAR_DECL
12403               || DECL_EXTERNAL (sym)
12404               || !rtx_equal_p (DECL_RTL (sym), orig)
12405               )
12406             continue;
12407
12408           SET_DECL_RTL (sym, new);
12409         }
12410       
12411       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12412     }
12413 }
12414
12415 /* Return the number (counting from 0) of
12416    the least significant set bit in MASK.  */
12417
12418 inline static int
12419 number_of_first_bit_set (int mask)
12420 {
12421   int bit;
12422
12423   for (bit = 0;
12424        (mask & (1 << bit)) == 0;
12425        ++bit)
12426     continue;
12427
12428   return bit;
12429 }
12430
12431 /* Generate code to return from a thumb function.
12432    If 'reg_containing_return_addr' is -1, then the return address is
12433    actually on the stack, at the stack pointer.  */
12434 static void
12435 thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
12436 {
12437   unsigned regs_available_for_popping;
12438   unsigned regs_to_pop;
12439   int pops_needed;
12440   unsigned available;
12441   unsigned required;
12442   int mode;
12443   int size;
12444   int restore_a4 = FALSE;
12445
12446   /* Compute the registers we need to pop.  */
12447   regs_to_pop = 0;
12448   pops_needed = 0;
12449
12450   /* There is an assumption here, that if eh_ofs is not NULL, the
12451      normal return address will have been pushed.  */
12452   if (reg_containing_return_addr == -1 || eh_ofs)
12453     {
12454       /* When we are generating a return for __builtin_eh_return, 
12455          reg_containing_return_addr must specify the return regno.  */
12456       if (eh_ofs && reg_containing_return_addr == -1)
12457         abort ();
12458
12459       regs_to_pop |= 1 << LR_REGNUM;
12460       ++pops_needed;
12461     }
12462
12463   if (TARGET_BACKTRACE)
12464     {
12465       /* Restore the (ARM) frame pointer and stack pointer.  */
12466       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12467       pops_needed += 2;
12468     }
12469
12470   /* If there is nothing to pop then just emit the BX instruction and
12471      return.  */
12472   if (pops_needed == 0)
12473     {
12474       if (eh_ofs)
12475         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12476
12477       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12478       return;
12479     }
12480   /* Otherwise if we are not supporting interworking and we have not created
12481      a backtrace structure and the function was not entered in ARM mode then
12482      just pop the return address straight into the PC.  */
12483   else if (!TARGET_INTERWORK
12484            && !TARGET_BACKTRACE
12485            && !is_called_in_ARM_mode (current_function_decl))
12486     {
12487       if (eh_ofs)
12488         {
12489           asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12490           asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12491           asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12492         }
12493       else
12494         asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12495
12496       return;
12497     }
12498
12499   /* Find out how many of the (return) argument registers we can corrupt.  */
12500   regs_available_for_popping = 0;
12501
12502   /* If returning via __builtin_eh_return, the bottom three registers
12503      all contain information needed for the return.  */
12504   if (eh_ofs)
12505     size = 12;
12506   else
12507     {
12508       /* If we can deduce the registers used from the function's
12509          return value.  This is more reliable that examining
12510          regs_ever_live[] because that will be set if the register is
12511          ever used in the function, not just if the register is used
12512          to hold a return value.  */
12513
12514       if (current_function_return_rtx != 0)
12515         mode = GET_MODE (current_function_return_rtx);
12516       else
12517         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12518
12519       size = GET_MODE_SIZE (mode);
12520
12521       if (size == 0)
12522         {
12523           /* In a void function we can use any argument register.
12524              In a function that returns a structure on the stack
12525              we can use the second and third argument registers.  */
12526           if (mode == VOIDmode)
12527             regs_available_for_popping =
12528               (1 << ARG_REGISTER (1))
12529               | (1 << ARG_REGISTER (2))
12530               | (1 << ARG_REGISTER (3));
12531           else
12532             regs_available_for_popping =
12533               (1 << ARG_REGISTER (2))
12534               | (1 << ARG_REGISTER (3));
12535         }
12536       else if (size <= 4)
12537         regs_available_for_popping =
12538           (1 << ARG_REGISTER (2))
12539           | (1 << ARG_REGISTER (3));
12540       else if (size <= 8)
12541         regs_available_for_popping =
12542           (1 << ARG_REGISTER (3));
12543     }
12544
12545   /* Match registers to be popped with registers into which we pop them.  */
12546   for (available = regs_available_for_popping,
12547        required  = regs_to_pop;
12548        required != 0 && available != 0;
12549        available &= ~(available & - available),
12550        required  &= ~(required  & - required))
12551     -- pops_needed;
12552
12553   /* If we have any popping registers left over, remove them.  */
12554   if (available > 0)
12555     regs_available_for_popping &= ~available;
12556   
12557   /* Otherwise if we need another popping register we can use
12558      the fourth argument register.  */
12559   else if (pops_needed)
12560     {
12561       /* If we have not found any free argument registers and
12562          reg a4 contains the return address, we must move it.  */
12563       if (regs_available_for_popping == 0
12564           && reg_containing_return_addr == LAST_ARG_REGNUM)
12565         {
12566           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12567           reg_containing_return_addr = LR_REGNUM;
12568         }
12569       else if (size > 12)
12570         {
12571           /* Register a4 is being used to hold part of the return value,
12572              but we have dire need of a free, low register.  */
12573           restore_a4 = TRUE;
12574           
12575           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12576         }
12577       
12578       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12579         {
12580           /* The fourth argument register is available.  */
12581           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12582           
12583           --pops_needed;
12584         }
12585     }
12586
12587   /* Pop as many registers as we can.  */
12588   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12589                  regs_available_for_popping);
12590
12591   /* Process the registers we popped.  */
12592   if (reg_containing_return_addr == -1)
12593     {
12594       /* The return address was popped into the lowest numbered register.  */
12595       regs_to_pop &= ~(1 << LR_REGNUM);
12596       
12597       reg_containing_return_addr =
12598         number_of_first_bit_set (regs_available_for_popping);
12599
12600       /* Remove this register for the mask of available registers, so that
12601          the return address will not be corrupted by further pops.  */
12602       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12603     }
12604
12605   /* If we popped other registers then handle them here.  */
12606   if (regs_available_for_popping)
12607     {
12608       int frame_pointer;
12609       
12610       /* Work out which register currently contains the frame pointer.  */
12611       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12612
12613       /* Move it into the correct place.  */
12614       asm_fprintf (f, "\tmov\t%r, %r\n",
12615                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12616
12617       /* (Temporarily) remove it from the mask of popped registers.  */
12618       regs_available_for_popping &= ~(1 << frame_pointer);
12619       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12620       
12621       if (regs_available_for_popping)
12622         {
12623           int stack_pointer;
12624           
12625           /* We popped the stack pointer as well,
12626              find the register that contains it.  */
12627           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12628
12629           /* Move it into the stack register.  */
12630           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12631           
12632           /* At this point we have popped all necessary registers, so
12633              do not worry about restoring regs_available_for_popping
12634              to its correct value:
12635
12636              assert (pops_needed == 0)
12637              assert (regs_available_for_popping == (1 << frame_pointer))
12638              assert (regs_to_pop == (1 << STACK_POINTER))  */
12639         }
12640       else
12641         {
12642           /* Since we have just move the popped value into the frame
12643              pointer, the popping register is available for reuse, and
12644              we know that we still have the stack pointer left to pop.  */
12645           regs_available_for_popping |= (1 << frame_pointer);
12646         }
12647     }
12648   
12649   /* If we still have registers left on the stack, but we no longer have
12650      any registers into which we can pop them, then we must move the return
12651      address into the link register and make available the register that
12652      contained it.  */
12653   if (regs_available_for_popping == 0 && pops_needed > 0)
12654     {
12655       regs_available_for_popping |= 1 << reg_containing_return_addr;
12656       
12657       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12658                    reg_containing_return_addr);
12659       
12660       reg_containing_return_addr = LR_REGNUM;
12661     }
12662
12663   /* If we have registers left on the stack then pop some more.
12664      We know that at most we will want to pop FP and SP.  */
12665   if (pops_needed > 0)
12666     {
12667       int  popped_into;
12668       int  move_to;
12669       
12670       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12671                      regs_available_for_popping);
12672
12673       /* We have popped either FP or SP.
12674          Move whichever one it is into the correct register.  */
12675       popped_into = number_of_first_bit_set (regs_available_for_popping);
12676       move_to     = number_of_first_bit_set (regs_to_pop);
12677
12678       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12679
12680       regs_to_pop &= ~(1 << move_to);
12681
12682       --pops_needed;
12683     }
12684   
12685   /* If we still have not popped everything then we must have only
12686      had one register available to us and we are now popping the SP.  */
12687   if (pops_needed > 0)
12688     {
12689       int  popped_into;
12690       
12691       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12692                      regs_available_for_popping);
12693
12694       popped_into = number_of_first_bit_set (regs_available_for_popping);
12695
12696       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12697       /*
12698         assert (regs_to_pop == (1 << STACK_POINTER))
12699         assert (pops_needed == 1)
12700       */
12701     }
12702
12703   /* If necessary restore the a4 register.  */
12704   if (restore_a4)
12705     {
12706       if (reg_containing_return_addr != LR_REGNUM)
12707         {
12708           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12709           reg_containing_return_addr = LR_REGNUM;
12710         }
12711     
12712       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12713     }
12714
12715   if (eh_ofs)
12716     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12717
12718   /* Return to caller.  */
12719   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12720 }
12721
12722 /* Emit code to push or pop registers to or from the stack.  F is the
12723    assembly file.  MASK is the registers to push or pop.  PUSH is
12724    nonzero if we should push, and zero if we should pop.  For debugging
12725    output, if pushing, adjust CFA_OFFSET by the amount of space added
12726    to the stack.  REAL_REGS should have the same number of bits set as
12727    MASK, and will be used instead (in the same order) to describe which
12728    registers were saved - this is used to mark the save slots when we
12729    push high registers after moving them to low registers.  */
12730 static void
12731 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12732 {
12733   int regno;
12734   int lo_mask = mask & 0xFF;
12735   int pushed_words = 0;
12736
12737   if (lo_mask == 0 && !push && (mask & (1 << 15)))
12738     {
12739       /* Special case.  Do not generate a POP PC statement here, do it in
12740          thumb_exit() */
12741       thumb_exit (f, -1, NULL_RTX);
12742       return;
12743     }
12744       
12745   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12746
12747   /* Look at the low registers first.  */
12748   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12749     {
12750       if (lo_mask & 1)
12751         {
12752           asm_fprintf (f, "%r", regno);
12753           
12754           if ((lo_mask & ~1) != 0)
12755             fprintf (f, ", ");
12756
12757           pushed_words++;
12758         }
12759     }
12760   
12761   if (push && (mask & (1 << LR_REGNUM)))
12762     {
12763       /* Catch pushing the LR.  */
12764       if (mask & 0xFF)
12765         fprintf (f, ", ");
12766       
12767       asm_fprintf (f, "%r", LR_REGNUM);
12768
12769       pushed_words++;
12770     }
12771   else if (!push && (mask & (1 << PC_REGNUM)))
12772     {
12773       /* Catch popping the PC.  */
12774       if (TARGET_INTERWORK || TARGET_BACKTRACE)
12775         {
12776           /* The PC is never poped directly, instead
12777              it is popped into r3 and then BX is used.  */
12778           fprintf (f, "}\n");
12779
12780           thumb_exit (f, -1, NULL_RTX);
12781
12782           return;
12783         }
12784       else
12785         {
12786           if (mask & 0xFF)
12787             fprintf (f, ", ");
12788           
12789           asm_fprintf (f, "%r", PC_REGNUM);
12790         }
12791     }
12792        
12793   fprintf (f, "}\n");
12794
12795   if (push && pushed_words && dwarf2out_do_frame ())
12796     {
12797       char *l = dwarf2out_cfi_label ();
12798       int pushed_mask = real_regs;
12799
12800       *cfa_offset += pushed_words * 4;
12801       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12802
12803       pushed_words = 0;
12804       pushed_mask = real_regs;
12805       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12806         {
12807           if (pushed_mask & 1)
12808             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12809         }
12810     }
12811 }
12812 \f
12813 void
12814 thumb_final_prescan_insn (rtx insn)
12815 {
12816   if (flag_print_asm_name)
12817     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12818                  INSN_ADDRESSES (INSN_UID (insn)));
12819 }
12820
12821 int
12822 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12823 {
12824   unsigned HOST_WIDE_INT mask = 0xff;
12825   int i;
12826
12827   if (val == 0) /* XXX */
12828     return 0;
12829   
12830   for (i = 0; i < 25; i++)
12831     if ((val & (mask << i)) == val)
12832       return 1;
12833
12834   return 0;
12835 }
12836
12837 /* Returns nonzero if the current function contains,
12838    or might contain a far jump.  */
12839 static int
12840 thumb_far_jump_used_p (void)
12841 {
12842   rtx insn;
12843
12844   /* This test is only important for leaf functions.  */
12845   /* assert (!leaf_function_p ()); */
12846   
12847   /* If we have already decided that far jumps may be used,
12848      do not bother checking again, and always return true even if
12849      it turns out that they are not being used.  Once we have made
12850      the decision that far jumps are present (and that hence the link
12851      register will be pushed onto the stack) we cannot go back on it.  */
12852   if (cfun->machine->far_jump_used)
12853     return 1;
12854
12855   /* If this function is not being called from the prologue/epilogue
12856      generation code then it must be being called from the
12857      INITIAL_ELIMINATION_OFFSET macro.  */
12858   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12859     {
12860       /* In this case we know that we are being asked about the elimination
12861          of the arg pointer register.  If that register is not being used,
12862          then there are no arguments on the stack, and we do not have to
12863          worry that a far jump might force the prologue to push the link
12864          register, changing the stack offsets.  In this case we can just
12865          return false, since the presence of far jumps in the function will
12866          not affect stack offsets.
12867
12868          If the arg pointer is live (or if it was live, but has now been
12869          eliminated and so set to dead) then we do have to test to see if
12870          the function might contain a far jump.  This test can lead to some
12871          false negatives, since before reload is completed, then length of
12872          branch instructions is not known, so gcc defaults to returning their
12873          longest length, which in turn sets the far jump attribute to true.
12874
12875          A false negative will not result in bad code being generated, but it
12876          will result in a needless push and pop of the link register.  We
12877          hope that this does not occur too often.
12878
12879          If we need doubleword stack alignment this could affect the other
12880          elimination offsets so we can't risk getting it wrong.  */
12881       if (regs_ever_live [ARG_POINTER_REGNUM])
12882         cfun->machine->arg_pointer_live = 1;
12883       else if (!cfun->machine->arg_pointer_live)
12884         return 0;
12885     }
12886
12887   /* Check to see if the function contains a branch
12888      insn with the far jump attribute set.  */
12889   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12890     {
12891       if (GET_CODE (insn) == JUMP_INSN
12892           /* Ignore tablejump patterns.  */
12893           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12894           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12895           && get_attr_far_jump (insn) == FAR_JUMP_YES
12896           )
12897         {
12898           /* Record the fact that we have decided that
12899              the function does use far jumps.  */
12900           cfun->machine->far_jump_used = 1;
12901           return 1;
12902         }
12903     }
12904   
12905   return 0;
12906 }
12907
12908 /* Return nonzero if FUNC must be entered in ARM mode.  */
12909 int
12910 is_called_in_ARM_mode (tree func)
12911 {
12912   if (TREE_CODE (func) != FUNCTION_DECL)
12913     abort ();
12914
12915   /* Ignore the problem about functions whoes address is taken.  */
12916   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12917     return TRUE;
12918
12919 #ifdef ARM_PE 
12920   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12921 #else
12922   return FALSE;
12923 #endif
12924 }
12925
12926 /* The bits which aren't usefully expanded as rtl.  */
12927 const char *
12928 thumb_unexpanded_epilogue (void)
12929 {
12930   int regno;
12931   int live_regs_mask = 0;
12932   int high_regs_pushed = 0;
12933   int had_to_push_lr;
12934   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12935
12936   if (return_used_this_function)
12937     return "";
12938
12939   if (IS_NAKED (arm_current_func_type ()))
12940     return "";
12941
12942   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12943     if (THUMB_REG_PUSHED_P (regno))
12944       live_regs_mask |= 1 << regno;
12945
12946   for (regno = 8; regno < 13; regno++)
12947     if (THUMB_REG_PUSHED_P (regno))
12948       high_regs_pushed++;
12949
12950   /* The prolog may have pushed some high registers to use as
12951      work registers.  eg the testsuite file:
12952      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12953      compiles to produce:
12954         push    {r4, r5, r6, r7, lr}
12955         mov     r7, r9
12956         mov     r6, r8
12957         push    {r6, r7}
12958      as part of the prolog.  We have to undo that pushing here.  */
12959   
12960   if (high_regs_pushed)
12961     {
12962       int mask = live_regs_mask;
12963       int next_hi_reg;
12964       int size;
12965       int mode;
12966        
12967       /* If we can deduce the registers used from the function's return value.
12968          This is more reliable that examining regs_ever_live[] because that
12969          will be set if the register is ever used in the function, not just if
12970          the register is used to hold a return value.  */
12971
12972       if (current_function_return_rtx != 0)
12973         mode = GET_MODE (current_function_return_rtx);
12974       else
12975         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12976
12977       size = GET_MODE_SIZE (mode);
12978
12979       /* Unless we are returning a type of size > 12 register r3 is
12980          available.  */
12981       if (size < 13)
12982         mask |=  1 << 3;
12983
12984       if (mask == 0)
12985         /* Oh dear!  We have no low registers into which we can pop
12986            high registers!  */
12987         internal_error
12988           ("no low registers available for popping high registers");
12989       
12990       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12991         if (THUMB_REG_PUSHED_P (next_hi_reg))
12992           break;
12993
12994       while (high_regs_pushed)
12995         {
12996           /* Find lo register(s) into which the high register(s) can
12997              be popped.  */
12998           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12999             {
13000               if (mask & (1 << regno))
13001                 high_regs_pushed--;
13002               if (high_regs_pushed == 0)
13003                 break;
13004             }
13005
13006           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
13007
13008           /* Pop the values into the low register(s).  */
13009           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13010
13011           /* Move the value(s) into the high registers.  */
13012           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13013             {
13014               if (mask & (1 << regno))
13015                 {
13016                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13017                                regno);
13018                   
13019                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13020                     if (THUMB_REG_PUSHED_P (next_hi_reg))
13021                       break;
13022                 }
13023             }
13024         }
13025     }
13026
13027   had_to_push_lr = (live_regs_mask || thumb_force_lr_save ());
13028   
13029   if (TARGET_BACKTRACE
13030       && ((live_regs_mask & 0xFF) == 0)
13031       && regs_ever_live [LAST_ARG_REGNUM] != 0)
13032     {
13033       /* The stack backtrace structure creation code had to
13034          push R7 in order to get a work register, so we pop
13035          it now.  */
13036       live_regs_mask |= (1 << LAST_LO_REGNUM);
13037     }
13038   
13039   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13040     {
13041       if (had_to_push_lr
13042           && !is_called_in_ARM_mode (current_function_decl)
13043           && !eh_ofs)
13044         live_regs_mask |= 1 << PC_REGNUM;
13045
13046       /* Either no argument registers were pushed or a backtrace
13047          structure was created which includes an adjusted stack
13048          pointer, so just pop everything.  */
13049       if (live_regs_mask)
13050         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13051                        live_regs_mask);
13052       
13053       if (eh_ofs)
13054         thumb_exit (asm_out_file, 2, eh_ofs);
13055       /* We have either just popped the return address into the
13056          PC or it is was kept in LR for the entire function or
13057          it is still on the stack because we do not want to
13058          return by doing a pop {pc}.  */
13059       else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13060         thumb_exit (asm_out_file,
13061                     (had_to_push_lr
13062                      && is_called_in_ARM_mode (current_function_decl)) ?
13063                     -1 : LR_REGNUM, NULL_RTX);
13064     }
13065   else
13066     {
13067       /* Pop everything but the return address.  */
13068       live_regs_mask &= ~(1 << PC_REGNUM);
13069       
13070       if (live_regs_mask)
13071         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13072                        live_regs_mask);
13073
13074       if (had_to_push_lr)
13075         /* Get the return address into a temporary register.  */
13076         thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13077                        1 << LAST_ARG_REGNUM);
13078       
13079       /* Remove the argument registers that were pushed onto the stack.  */
13080       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13081                    SP_REGNUM, SP_REGNUM,
13082                    current_function_pretend_args_size);
13083       
13084       if (eh_ofs)
13085         thumb_exit (asm_out_file, 2, eh_ofs);
13086       else
13087         thumb_exit (asm_out_file,
13088                     had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13089     }
13090
13091   return "";
13092 }
13093
13094 /* Functions to save and restore machine-specific function data.  */
13095 static struct machine_function *
13096 arm_init_machine_status (void)
13097 {
13098   struct machine_function *machine;
13099   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13100
13101 #if ARM_FT_UNKNOWN != 0  
13102   machine->func_type = ARM_FT_UNKNOWN;
13103 #endif
13104   return machine;
13105 }
13106
13107 /* Return an RTX indicating where the return address to the
13108    calling function can be found.  */
13109 rtx
13110 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13111 {
13112   if (count != 0)
13113     return NULL_RTX;
13114
13115   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13116 }
13117
13118 /* Do anything needed before RTL is emitted for each function.  */
13119 void
13120 arm_init_expanders (void)
13121 {
13122   /* Arrange to initialize and mark the machine per-function status.  */
13123   init_machine_status = arm_init_machine_status;
13124
13125   /* This is to stop the combine pass optimizing away the alignment
13126      adjustment of va_arg.  */
13127   /* ??? It is claimed that this should not be necessary.  */
13128   if (cfun)
13129     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13130 }
13131
13132
13133 /* Like arm_compute_initial_elimination offset.  Simpler because
13134    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13135
13136 HOST_WIDE_INT
13137 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13138 {
13139   arm_stack_offsets *offsets;
13140
13141   offsets = arm_get_frame_offsets ();
13142
13143   switch (from)
13144     {
13145     case ARG_POINTER_REGNUM:
13146       switch (to)
13147         {
13148         case STACK_POINTER_REGNUM:
13149           return offsets->outgoing_args - offsets->saved_args;
13150
13151         case FRAME_POINTER_REGNUM:
13152           return offsets->soft_frame - offsets->saved_args;
13153
13154         case THUMB_HARD_FRAME_POINTER_REGNUM:
13155         case ARM_HARD_FRAME_POINTER_REGNUM:
13156           return offsets->saved_regs - offsets->saved_args;
13157
13158         default:
13159           abort();
13160         }
13161       break;
13162
13163     case FRAME_POINTER_REGNUM:
13164       switch (to)
13165         {
13166         case STACK_POINTER_REGNUM:
13167           return offsets->outgoing_args - offsets->soft_frame;
13168
13169         case THUMB_HARD_FRAME_POINTER_REGNUM:
13170         case ARM_HARD_FRAME_POINTER_REGNUM:
13171           return offsets->saved_regs - offsets->soft_frame;
13172
13173         default:
13174           abort();
13175         }
13176       break;
13177
13178     default:
13179       abort ();
13180     }
13181 }
13182
13183
13184 /* Generate the rest of a function's prologue.  */
13185 void
13186 thumb_expand_prologue (void)
13187 {
13188   rtx insn, dwarf;
13189
13190   HOST_WIDE_INT amount;
13191   arm_stack_offsets *offsets;
13192   unsigned long func_type;
13193   int regno;
13194
13195   func_type = arm_current_func_type ();
13196   
13197   /* Naked functions don't have prologues.  */
13198   if (IS_NAKED (func_type))
13199     return;
13200
13201   if (IS_INTERRUPT (func_type))
13202     {
13203       error ("interrupt Service Routines cannot be coded in Thumb mode");
13204       return;
13205     }
13206
13207   offsets = arm_get_frame_offsets ();
13208
13209   if (frame_pointer_needed)
13210     {
13211       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13212                                    stack_pointer_rtx));
13213       RTX_FRAME_RELATED_P (insn) = 1;
13214     }
13215
13216   amount = offsets->outgoing_args - offsets->saved_regs;
13217   if (amount)
13218     {
13219       if (amount < 512)
13220         {
13221           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13222                                         GEN_INT (- amount)));
13223           RTX_FRAME_RELATED_P (insn) = 1;
13224         }
13225       else
13226         {
13227           rtx reg;
13228
13229           /* The stack decrement is too big for an immediate value in a single
13230              insn.  In theory we could issue multiple subtracts, but after
13231              three of them it becomes more space efficient to place the full
13232              value in the constant pool and load into a register.  (Also the
13233              ARM debugger really likes to see only one stack decrement per
13234              function).  So instead we look for a scratch register into which
13235              we can load the decrement, and then we subtract this from the
13236              stack pointer.  Unfortunately on the thumb the only available
13237              scratch registers are the argument registers, and we cannot use
13238              these as they may hold arguments to the function.  Instead we
13239              attempt to locate a call preserved register which is used by this
13240              function.  If we can find one, then we know that it will have
13241              been pushed at the start of the prologue and so we can corrupt
13242              it now.  */
13243           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13244             if (THUMB_REG_PUSHED_P (regno)
13245                 && !(frame_pointer_needed
13246                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13247               break;
13248
13249           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13250             {
13251               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13252
13253               /* Choose an arbitrary, non-argument low register.  */
13254               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13255
13256               /* Save it by copying it into a high, scratch register.  */
13257               emit_insn (gen_movsi (spare, reg));
13258               /* Add a USE to stop propagate_one_insn() from barfing.  */
13259               emit_insn (gen_prologue_use (spare));
13260
13261               /* Decrement the stack.  */
13262               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13263               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13264                                             stack_pointer_rtx, reg));
13265               RTX_FRAME_RELATED_P (insn) = 1;
13266               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13267                                    plus_constant (stack_pointer_rtx,
13268                                                   GEN_INT (- amount)));
13269               RTX_FRAME_RELATED_P (dwarf) = 1;
13270               REG_NOTES (insn)
13271                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13272                                      REG_NOTES (insn));
13273
13274               /* Restore the low register's original value.  */
13275               emit_insn (gen_movsi (reg, spare));
13276               
13277               /* Emit a USE of the restored scratch register, so that flow
13278                  analysis will not consider the restore redundant.  The
13279                  register won't be used again in this function and isn't
13280                  restored by the epilogue.  */
13281               emit_insn (gen_prologue_use (reg));
13282             }
13283           else
13284             {
13285               reg = gen_rtx_REG (SImode, regno);
13286
13287               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13288
13289               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13290                                             stack_pointer_rtx, reg));
13291               RTX_FRAME_RELATED_P (insn) = 1;
13292               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13293                                    plus_constant (stack_pointer_rtx,
13294                                                   GEN_INT (- amount)));
13295               RTX_FRAME_RELATED_P (dwarf) = 1;
13296               REG_NOTES (insn)
13297                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13298                                      REG_NOTES (insn));
13299             }
13300         }
13301       /* If the frame pointer is needed, emit a special barrier that
13302          will prevent the scheduler from moving stores to the frame
13303          before the stack adjustment.  */
13304       if (frame_pointer_needed)
13305         emit_insn (gen_stack_tie (stack_pointer_rtx,
13306                                   hard_frame_pointer_rtx));
13307     }
13308   
13309   if (current_function_profile || TARGET_NO_SCHED_PRO)
13310     emit_insn (gen_blockage ());
13311
13312   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13313   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13314     {
13315       if (THUMB_REG_PUSHED_P (regno))
13316         {
13317           cfun->machine->lr_save_eliminated = 0;
13318           break;
13319         }
13320     }
13321
13322   /* If the link register is being kept alive, with the return address in it,
13323      then make sure that it does not get reused by the ce2 pass.  */
13324   if (cfun->machine->lr_save_eliminated)
13325     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13326 }
13327
13328 void
13329 thumb_expand_epilogue (void)
13330 {
13331   HOST_WIDE_INT amount;
13332   arm_stack_offsets *offsets;
13333   int regno;
13334
13335   /* Naked functions don't have prologues.  */
13336   if (IS_NAKED (arm_current_func_type ()))
13337     return;
13338
13339   offsets = arm_get_frame_offsets ();
13340   amount = offsets->outgoing_args - offsets->saved_regs;
13341
13342   if (frame_pointer_needed)
13343     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13344   else if (amount)
13345     {
13346       if (amount < 512)
13347         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13348                                GEN_INT (amount)));
13349       else
13350         {
13351           /* r3 is always free in the epilogue.  */
13352           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13353
13354           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13355           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13356         }
13357     }
13358       
13359   /* Emit a USE (stack_pointer_rtx), so that
13360      the stack adjustment will not be deleted.  */
13361   emit_insn (gen_prologue_use (stack_pointer_rtx));
13362
13363   if (current_function_profile || TARGET_NO_SCHED_PRO)
13364     emit_insn (gen_blockage ());
13365
13366   /* Emit a clobber for each insn that will be restored in the epilogue,
13367      so that flow2 will get register lifetimes correct.  */
13368   for (regno = 0; regno < 13; regno++)
13369     if (regs_ever_live[regno] && !call_used_regs[regno])
13370       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13371
13372   if (! regs_ever_live[LR_REGNUM])
13373     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13374 }
13375
13376 static void
13377 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13378 {
13379   int live_regs_mask = 0;
13380   int high_regs_pushed = 0;
13381   int cfa_offset = 0;
13382   int regno;
13383
13384   if (IS_NAKED (arm_current_func_type ()))
13385     return;
13386
13387   if (is_called_in_ARM_mode (current_function_decl))
13388     {
13389       const char * name;
13390
13391       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13392         abort ();
13393       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13394         abort ();
13395       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13396       
13397       /* Generate code sequence to switch us into Thumb mode.  */
13398       /* The .code 32 directive has already been emitted by
13399          ASM_DECLARE_FUNCTION_NAME.  */
13400       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13401       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13402
13403       /* Generate a label, so that the debugger will notice the
13404          change in instruction sets.  This label is also used by
13405          the assembler to bypass the ARM code when this function
13406          is called from a Thumb encoded function elsewhere in the
13407          same file.  Hence the definition of STUB_NAME here must
13408          agree with the definition in gas/config/tc-arm.c.  */
13409       
13410 #define STUB_NAME ".real_start_of"
13411       
13412       fprintf (f, "\t.code\t16\n");
13413 #ifdef ARM_PE
13414       if (arm_dllexport_name_p (name))
13415         name = arm_strip_name_encoding (name);
13416 #endif        
13417       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13418       fprintf (f, "\t.thumb_func\n");
13419       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13420     }
13421     
13422   if (current_function_pretend_args_size)
13423     {
13424       if (cfun->machine->uses_anonymous_args)
13425         {
13426           int num_pushes;
13427           
13428           fprintf (f, "\tpush\t{");
13429
13430           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13431           
13432           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13433                regno <= LAST_ARG_REGNUM;
13434                regno++)
13435             asm_fprintf (f, "%r%s", regno,
13436                          regno == LAST_ARG_REGNUM ? "" : ", ");
13437
13438           fprintf (f, "}\n");
13439         }
13440       else
13441         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", 
13442                      SP_REGNUM, SP_REGNUM,
13443                      current_function_pretend_args_size);
13444
13445       /* We don't need to record the stores for unwinding (would it
13446          help the debugger any if we did?), but record the change in
13447          the stack pointer.  */
13448       if (dwarf2out_do_frame ())
13449         {
13450           char *l = dwarf2out_cfi_label ();
13451           cfa_offset = cfa_offset + current_function_pretend_args_size;
13452           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13453         }
13454     }
13455
13456   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13457     if (THUMB_REG_PUSHED_P (regno))
13458       live_regs_mask |= 1 << regno;
13459
13460   if (live_regs_mask || thumb_force_lr_save ())
13461     live_regs_mask |= 1 << LR_REGNUM;
13462
13463   if (TARGET_BACKTRACE)
13464     {
13465       int    offset;
13466       int    work_register = 0;
13467       int    wr;
13468       
13469       /* We have been asked to create a stack backtrace structure.
13470          The code looks like this:
13471          
13472          0   .align 2
13473          0   func:
13474          0     sub   SP, #16         Reserve space for 4 registers.
13475          2     push  {R7}            Get a work register.
13476          4     add   R7, SP, #20     Get the stack pointer before the push.
13477          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13478          8     mov   R7, PC          Get hold of the start of this code plus 12.
13479         10     str   R7, [SP, #16]   Store it.
13480         12     mov   R7, FP          Get hold of the current frame pointer.
13481         14     str   R7, [SP, #4]    Store it.
13482         16     mov   R7, LR          Get hold of the current return address.
13483         18     str   R7, [SP, #12]   Store it.
13484         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13485         22     mov   FP, R7          Put this value into the frame pointer.  */
13486
13487       if ((live_regs_mask & 0xFF) == 0)
13488         {
13489           /* See if the a4 register is free.  */
13490
13491           if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13492             work_register = LAST_ARG_REGNUM;
13493           else    /* We must push a register of our own.  */
13494             live_regs_mask |= (1 << LAST_LO_REGNUM);
13495         }
13496
13497       if (work_register == 0)
13498         {
13499           /* Select a register from the list that will be pushed to
13500              use as our work register.  */
13501           for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13502             if ((1 << work_register) & live_regs_mask)
13503               break;
13504         }
13505       
13506       asm_fprintf
13507         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13508          SP_REGNUM, SP_REGNUM);
13509
13510       if (dwarf2out_do_frame ())
13511         {
13512           char *l = dwarf2out_cfi_label ();
13513           cfa_offset = cfa_offset + 16;
13514           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13515         }
13516
13517       if (live_regs_mask)
13518         thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13519       
13520       for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13521         if (wr & live_regs_mask)
13522           offset += 4;
13523       
13524       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13525                    offset + 16 + current_function_pretend_args_size);
13526       
13527       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13528                    offset + 4);
13529
13530       /* Make sure that the instruction fetching the PC is in the right place
13531          to calculate "start of backtrace creation code + 12".  */
13532       if (live_regs_mask)
13533         {
13534           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13535           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13536                        offset + 12);
13537           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13538                        ARM_HARD_FRAME_POINTER_REGNUM);
13539           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13540                        offset);
13541         }
13542       else
13543         {
13544           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13545                        ARM_HARD_FRAME_POINTER_REGNUM);
13546           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13547                        offset);
13548           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13549           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13550                        offset + 12);
13551         }
13552       
13553       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13554       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13555                    offset + 8);
13556       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13557                    offset + 12);
13558       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13559                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13560     }
13561   else if (live_regs_mask)
13562     thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13563
13564   for (regno = 8; regno < 13; regno++)
13565     if (THUMB_REG_PUSHED_P (regno))
13566       high_regs_pushed++;
13567
13568   if (high_regs_pushed)
13569     {
13570       int pushable_regs = 0;
13571       int mask = live_regs_mask & 0xff;
13572       int next_hi_reg;
13573
13574       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13575         if (THUMB_REG_PUSHED_P (next_hi_reg))
13576           break;
13577
13578       pushable_regs = mask;
13579
13580       if (pushable_regs == 0)
13581         {
13582           /* Desperation time -- this probably will never happen.  */
13583           if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
13584             asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13585           mask = 1 << LAST_ARG_REGNUM;
13586         }
13587
13588       while (high_regs_pushed > 0)
13589         {
13590           int real_regs_mask = 0;
13591
13592           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13593             {
13594               if (mask & (1 << regno))
13595                 {
13596                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13597                   
13598                   high_regs_pushed--;
13599                   real_regs_mask |= (1 << next_hi_reg);
13600                   
13601                   if (high_regs_pushed)
13602                     {
13603                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13604                            next_hi_reg--)
13605                         if (THUMB_REG_PUSHED_P (next_hi_reg))
13606                           break;
13607                     }
13608                   else
13609                     {
13610                       mask &= ~((1 << regno) - 1);
13611                       break;
13612                     }
13613                 }
13614             }
13615
13616           thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
13617         }
13618
13619       if (pushable_regs == 0
13620           && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
13621         asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13622     }
13623 }
13624
13625 /* Handle the case of a double word load into a low register from
13626    a computed memory address.  The computed address may involve a
13627    register which is overwritten by the load.  */
13628 const char *
13629 thumb_load_double_from_address (rtx *operands)
13630 {
13631   rtx addr;
13632   rtx base;
13633   rtx offset;
13634   rtx arg1;
13635   rtx arg2;
13636   
13637   if (GET_CODE (operands[0]) != REG)
13638     abort ();
13639   
13640   if (GET_CODE (operands[1]) != MEM)
13641     abort ();
13642
13643   /* Get the memory address.  */
13644   addr = XEXP (operands[1], 0);
13645       
13646   /* Work out how the memory address is computed.  */
13647   switch (GET_CODE (addr))
13648     {
13649     case REG:
13650       operands[2] = gen_rtx_MEM (SImode,
13651                                  plus_constant (XEXP (operands[1], 0), 4));
13652
13653       if (REGNO (operands[0]) == REGNO (addr))
13654         {
13655           output_asm_insn ("ldr\t%H0, %2", operands);
13656           output_asm_insn ("ldr\t%0, %1", operands);
13657         }
13658       else
13659         {
13660           output_asm_insn ("ldr\t%0, %1", operands);
13661           output_asm_insn ("ldr\t%H0, %2", operands);
13662         }
13663       break;
13664       
13665     case CONST:
13666       /* Compute <address> + 4 for the high order load.  */
13667       operands[2] = gen_rtx_MEM (SImode,
13668                                  plus_constant (XEXP (operands[1], 0), 4));
13669       
13670       output_asm_insn ("ldr\t%0, %1", operands);
13671       output_asm_insn ("ldr\t%H0, %2", operands);
13672       break;
13673           
13674     case PLUS:
13675       arg1   = XEXP (addr, 0);
13676       arg2   = XEXP (addr, 1);
13677             
13678       if (CONSTANT_P (arg1))
13679         base = arg2, offset = arg1;
13680       else
13681         base = arg1, offset = arg2;
13682   
13683       if (GET_CODE (base) != REG)
13684         abort ();
13685
13686       /* Catch the case of <address> = <reg> + <reg> */
13687       if (GET_CODE (offset) == REG)
13688         {
13689           int reg_offset = REGNO (offset);
13690           int reg_base   = REGNO (base);
13691           int reg_dest   = REGNO (operands[0]);
13692           
13693           /* Add the base and offset registers together into the
13694              higher destination register.  */
13695           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13696                        reg_dest + 1, reg_base, reg_offset);
13697           
13698           /* Load the lower destination register from the address in
13699              the higher destination register.  */
13700           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13701                        reg_dest, reg_dest + 1);
13702           
13703           /* Load the higher destination register from its own address
13704              plus 4.  */
13705           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13706                        reg_dest + 1, reg_dest + 1);
13707         }
13708       else
13709         {
13710           /* Compute <address> + 4 for the high order load.  */
13711           operands[2] = gen_rtx_MEM (SImode,
13712                                      plus_constant (XEXP (operands[1], 0), 4));
13713           
13714           /* If the computed address is held in the low order register
13715              then load the high order register first, otherwise always
13716              load the low order register first.  */
13717           if (REGNO (operands[0]) == REGNO (base))
13718             {
13719               output_asm_insn ("ldr\t%H0, %2", operands);
13720               output_asm_insn ("ldr\t%0, %1", operands);
13721             }
13722           else
13723             {
13724               output_asm_insn ("ldr\t%0, %1", operands);
13725               output_asm_insn ("ldr\t%H0, %2", operands);
13726             }
13727         }
13728       break;
13729
13730     case LABEL_REF:
13731       /* With no registers to worry about we can just load the value
13732          directly.  */
13733       operands[2] = gen_rtx_MEM (SImode,
13734                                  plus_constant (XEXP (operands[1], 0), 4));
13735           
13736       output_asm_insn ("ldr\t%H0, %2", operands);
13737       output_asm_insn ("ldr\t%0, %1", operands);
13738       break;
13739       
13740     default:
13741       abort ();
13742       break;
13743     }
13744   
13745   return "";
13746 }
13747
13748 const char *
13749 thumb_output_move_mem_multiple (int n, rtx *operands)
13750 {
13751   rtx tmp;
13752
13753   switch (n)
13754     {
13755     case 2:
13756       if (REGNO (operands[4]) > REGNO (operands[5]))
13757         {
13758           tmp = operands[4];
13759           operands[4] = operands[5];
13760           operands[5] = tmp;
13761         }
13762       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13763       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13764       break;
13765
13766     case 3:
13767       if (REGNO (operands[4]) > REGNO (operands[5]))
13768         {
13769           tmp = operands[4];
13770           operands[4] = operands[5];
13771           operands[5] = tmp;
13772         }
13773       if (REGNO (operands[5]) > REGNO (operands[6]))
13774         {
13775           tmp = operands[5];
13776           operands[5] = operands[6];
13777           operands[6] = tmp;
13778         }
13779       if (REGNO (operands[4]) > REGNO (operands[5]))
13780         {
13781           tmp = operands[4];
13782           operands[4] = operands[5];
13783           operands[5] = tmp;
13784         }
13785       
13786       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13787       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13788       break;
13789
13790     default:
13791       abort ();
13792     }
13793
13794   return "";
13795 }
13796
13797 /* Routines for generating rtl.  */
13798 void
13799 thumb_expand_movmemqi (rtx *operands)
13800 {
13801   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13802   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13803   HOST_WIDE_INT len = INTVAL (operands[2]);
13804   HOST_WIDE_INT offset = 0;
13805
13806   while (len >= 12)
13807     {
13808       emit_insn (gen_movmem12b (out, in, out, in));
13809       len -= 12;
13810     }
13811   
13812   if (len >= 8)
13813     {
13814       emit_insn (gen_movmem8b (out, in, out, in));
13815       len -= 8;
13816     }
13817   
13818   if (len >= 4)
13819     {
13820       rtx reg = gen_reg_rtx (SImode);
13821       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13822       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13823       len -= 4;
13824       offset += 4;
13825     }
13826   
13827   if (len >= 2)
13828     {
13829       rtx reg = gen_reg_rtx (HImode);
13830       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode, 
13831                                               plus_constant (in, offset))));
13832       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13833                             reg));
13834       len -= 2;
13835       offset += 2;
13836     }
13837   
13838   if (len)
13839     {
13840       rtx reg = gen_reg_rtx (QImode);
13841       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13842                                               plus_constant (in, offset))));
13843       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13844                             reg));
13845     }
13846 }
13847
13848 int
13849 thumb_cmp_operand (rtx op, enum machine_mode mode)
13850 {
13851   return ((GET_CODE (op) == CONST_INT
13852            && INTVAL (op) < 256
13853            && INTVAL (op) >= 0)
13854           || s_register_operand (op, mode));
13855 }
13856
13857 int
13858 thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13859 {
13860   return (GET_CODE (op) == CONST_INT
13861           && INTVAL (op) < 0
13862           && INTVAL (op) > -256);
13863 }
13864
13865 /* Return TRUE if a result can be stored in OP without clobbering the
13866    condition code register.  Prior to reload we only accept a
13867    register.  After reload we have to be able to handle memory as
13868    well, since a pseudo may not get a hard reg and reload cannot
13869    handle output-reloads on jump insns.
13870
13871    We could possibly handle mem before reload as well, but that might
13872    complicate things with the need to handle increment
13873    side-effects.  */
13874
13875 int
13876 thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13877 {
13878   return (s_register_operand (op, mode)
13879           || ((reload_in_progress || reload_completed)
13880               && memory_operand (op, mode)));
13881 }
13882
13883 /* Handle storing a half-word to memory during reload.  */ 
13884 void
13885 thumb_reload_out_hi (rtx *operands)
13886 {
13887   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13888 }
13889
13890 /* Handle reading a half-word from memory during reload.  */ 
13891 void
13892 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13893 {
13894   abort ();
13895 }
13896
13897 /* Return the length of a function name prefix
13898     that starts with the character 'c'.  */
13899 static int
13900 arm_get_strip_length (int c)
13901 {
13902   switch (c)
13903     {
13904     ARM_NAME_ENCODING_LENGTHS
13905       default: return 0; 
13906     }
13907 }
13908
13909 /* Return a pointer to a function's name with any
13910    and all prefix encodings stripped from it.  */
13911 const char *
13912 arm_strip_name_encoding (const char *name)
13913 {
13914   int skip;
13915   
13916   while ((skip = arm_get_strip_length (* name)))
13917     name += skip;
13918
13919   return name;
13920 }
13921
13922 /* If there is a '*' anywhere in the name's prefix, then
13923    emit the stripped name verbatim, otherwise prepend an
13924    underscore if leading underscores are being used.  */
13925 void
13926 arm_asm_output_labelref (FILE *stream, const char *name)
13927 {
13928   int skip;
13929   int verbatim = 0;
13930
13931   while ((skip = arm_get_strip_length (* name)))
13932     {
13933       verbatim |= (*name == '*');
13934       name += skip;
13935     }
13936
13937   if (verbatim)
13938     fputs (name, stream);
13939   else
13940     asm_fprintf (stream, "%U%s", name);
13941 }
13942
13943 rtx aof_pic_label;
13944
13945 #ifdef AOF_ASSEMBLER
13946 /* Special functions only needed when producing AOF syntax assembler.  */
13947
13948 struct pic_chain
13949 {
13950   struct pic_chain * next;
13951   const char * symname;
13952 };
13953
13954 static struct pic_chain * aof_pic_chain = NULL;
13955
13956 rtx
13957 aof_pic_entry (rtx x)
13958 {
13959   struct pic_chain ** chainp;
13960   int offset;
13961
13962   if (aof_pic_label == NULL_RTX)
13963     {
13964       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13965     }
13966
13967   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13968        offset += 4, chainp = &(*chainp)->next)
13969     if ((*chainp)->symname == XSTR (x, 0))
13970       return plus_constant (aof_pic_label, offset);
13971
13972   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13973   (*chainp)->next = NULL;
13974   (*chainp)->symname = XSTR (x, 0);
13975   return plus_constant (aof_pic_label, offset);
13976 }
13977
13978 void
13979 aof_dump_pic_table (FILE *f)
13980 {
13981   struct pic_chain * chain;
13982
13983   if (aof_pic_chain == NULL)
13984     return;
13985
13986   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13987                PIC_OFFSET_TABLE_REGNUM,
13988                PIC_OFFSET_TABLE_REGNUM);
13989   fputs ("|x$adcons|\n", f);
13990   
13991   for (chain = aof_pic_chain; chain; chain = chain->next)
13992     {
13993       fputs ("\tDCD\t", f);
13994       assemble_name (f, chain->symname);
13995       fputs ("\n", f);
13996     }
13997 }
13998
13999 int arm_text_section_count = 1;
14000
14001 char *
14002 aof_text_section (void )
14003 {
14004   static char buf[100];
14005   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
14006            arm_text_section_count++);
14007   if (flag_pic)
14008     strcat (buf, ", PIC, REENTRANT");
14009   return buf;
14010 }
14011
14012 static int arm_data_section_count = 1;
14013
14014 char *
14015 aof_data_section (void)
14016 {
14017   static char buf[100];
14018   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
14019   return buf;
14020 }
14021
14022 /* The AOF assembler is religiously strict about declarations of
14023    imported and exported symbols, so that it is impossible to declare
14024    a function as imported near the beginning of the file, and then to
14025    export it later on.  It is, however, possible to delay the decision
14026    until all the functions in the file have been compiled.  To get
14027    around this, we maintain a list of the imports and exports, and
14028    delete from it any that are subsequently defined.  At the end of
14029    compilation we spit the remainder of the list out before the END
14030    directive.  */
14031
14032 struct import
14033 {
14034   struct import * next;
14035   const char * name;
14036 };
14037
14038 static struct import * imports_list = NULL;
14039
14040 void
14041 aof_add_import (const char *name)
14042 {
14043   struct import * new;
14044
14045   for (new = imports_list; new; new = new->next)
14046     if (new->name == name)
14047       return;
14048
14049   new = (struct import *) xmalloc (sizeof (struct import));
14050   new->next = imports_list;
14051   imports_list = new;
14052   new->name = name;
14053 }
14054
14055 void
14056 aof_delete_import (const char *name)
14057 {
14058   struct import ** old;
14059
14060   for (old = &imports_list; *old; old = & (*old)->next)
14061     {
14062       if ((*old)->name == name)
14063         {
14064           *old = (*old)->next;
14065           return;
14066         }
14067     }
14068 }
14069
14070 int arm_main_function = 0;
14071
14072 static void
14073 aof_dump_imports (FILE *f)
14074 {
14075   /* The AOF assembler needs this to cause the startup code to be extracted
14076      from the library.  Brining in __main causes the whole thing to work
14077      automagically.  */
14078   if (arm_main_function)
14079     {
14080       text_section ();
14081       fputs ("\tIMPORT __main\n", f);
14082       fputs ("\tDCD __main\n", f);
14083     }
14084
14085   /* Now dump the remaining imports.  */
14086   while (imports_list)
14087     {
14088       fprintf (f, "\tIMPORT\t");
14089       assemble_name (f, imports_list->name);
14090       fputc ('\n', f);
14091       imports_list = imports_list->next;
14092     }
14093 }
14094
14095 static void
14096 aof_globalize_label (FILE *stream, const char *name)
14097 {
14098   default_globalize_label (stream, name);
14099   if (! strcmp (name, "main"))
14100     arm_main_function = 1;
14101 }
14102
14103 static void
14104 aof_file_start (void)
14105 {
14106   fputs ("__r0\tRN\t0\n", asm_out_file);
14107   fputs ("__a1\tRN\t0\n", asm_out_file);
14108   fputs ("__a2\tRN\t1\n", asm_out_file);
14109   fputs ("__a3\tRN\t2\n", asm_out_file);
14110   fputs ("__a4\tRN\t3\n", asm_out_file);
14111   fputs ("__v1\tRN\t4\n", asm_out_file);
14112   fputs ("__v2\tRN\t5\n", asm_out_file);
14113   fputs ("__v3\tRN\t6\n", asm_out_file);
14114   fputs ("__v4\tRN\t7\n", asm_out_file);
14115   fputs ("__v5\tRN\t8\n", asm_out_file);
14116   fputs ("__v6\tRN\t9\n", asm_out_file);
14117   fputs ("__sl\tRN\t10\n", asm_out_file);
14118   fputs ("__fp\tRN\t11\n", asm_out_file);
14119   fputs ("__ip\tRN\t12\n", asm_out_file);
14120   fputs ("__sp\tRN\t13\n", asm_out_file);
14121   fputs ("__lr\tRN\t14\n", asm_out_file);
14122   fputs ("__pc\tRN\t15\n", asm_out_file);
14123   fputs ("__f0\tFN\t0\n", asm_out_file);
14124   fputs ("__f1\tFN\t1\n", asm_out_file);
14125   fputs ("__f2\tFN\t2\n", asm_out_file);
14126   fputs ("__f3\tFN\t3\n", asm_out_file);
14127   fputs ("__f4\tFN\t4\n", asm_out_file);
14128   fputs ("__f5\tFN\t5\n", asm_out_file);
14129   fputs ("__f6\tFN\t6\n", asm_out_file);
14130   fputs ("__f7\tFN\t7\n", asm_out_file);
14131   text_section ();
14132 }
14133
14134 static void
14135 aof_file_end (void)
14136 {
14137   if (flag_pic)
14138     aof_dump_pic_table (asm_out_file);
14139   aof_dump_imports (asm_out_file);
14140   fputs ("\tEND\n", asm_out_file);
14141 }
14142 #endif /* AOF_ASSEMBLER */
14143
14144 #ifdef OBJECT_FORMAT_ELF
14145 /* Switch to an arbitrary section NAME with attributes as specified
14146    by FLAGS.  ALIGN specifies any known alignment requirements for
14147    the section; 0 if the default should be used.
14148
14149    Differs from the default elf version only in the prefix character
14150    used before the section type.  */
14151
14152 static void
14153 arm_elf_asm_named_section (const char *name, unsigned int flags)
14154 {
14155   char flagchars[10], *f = flagchars;
14156
14157   if (! named_section_first_declaration (name))
14158     {
14159       fprintf (asm_out_file, "\t.section\t%s\n", name);
14160       return;
14161     }
14162
14163   if (!(flags & SECTION_DEBUG))
14164     *f++ = 'a';
14165   if (flags & SECTION_WRITE)
14166     *f++ = 'w';
14167   if (flags & SECTION_CODE)
14168     *f++ = 'x';
14169   if (flags & SECTION_SMALL)
14170     *f++ = 's';
14171   if (flags & SECTION_MERGE)
14172     *f++ = 'M';
14173   if (flags & SECTION_STRINGS)
14174     *f++ = 'S';
14175   if (flags & SECTION_TLS)
14176     *f++ = 'T';
14177   *f = '\0';
14178
14179   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
14180
14181   if (!(flags & SECTION_NOTYPE))
14182     {
14183       const char *type;
14184
14185       if (flags & SECTION_BSS)
14186         type = "nobits";
14187       else
14188         type = "progbits";
14189
14190       fprintf (asm_out_file, ",%%%s", type);
14191
14192       if (flags & SECTION_ENTSIZE)
14193         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14194     }
14195
14196   putc ('\n', asm_out_file);
14197 }
14198 #endif
14199
14200 #ifndef ARM_PE
14201 /* Symbols in the text segment can be accessed without indirecting via the
14202    constant pool; it may take an extra binary operation, but this is still
14203    faster than indirecting via memory.  Don't do this when not optimizing,
14204    since we won't be calculating al of the offsets necessary to do this
14205    simplification.  */
14206
14207 static void
14208 arm_encode_section_info (tree decl, rtx rtl, int first)
14209 {
14210   /* This doesn't work with AOF syntax, since the string table may be in
14211      a different AREA.  */
14212 #ifndef AOF_ASSEMBLER
14213   if (optimize > 0 && TREE_CONSTANT (decl))
14214     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14215 #endif
14216
14217   /* If we are referencing a function that is weak then encode a long call
14218      flag in the function name, otherwise if the function is static or
14219      or known to be defined in this file then encode a short call flag.  */
14220   if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14221     {
14222       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14223         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14224       else if (! TREE_PUBLIC (decl))
14225         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14226     }
14227 }
14228 #endif /* !ARM_PE */
14229
14230 static void
14231 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14232 {
14233   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14234       && !strcmp (prefix, "L"))
14235     {
14236       arm_ccfsm_state = 0;
14237       arm_target_insn = NULL;
14238     }
14239   default_internal_label (stream, prefix, labelno);
14240 }
14241
14242 /* Output code to add DELTA to the first argument, and then jump
14243    to FUNCTION.  Used for C++ multiple inheritance.  */
14244 static void
14245 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14246                      HOST_WIDE_INT delta,
14247                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14248                      tree function)
14249 {
14250   static int thunk_label = 0;
14251   char label[256];
14252   int mi_delta = delta;
14253   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14254   int shift = 0;
14255   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14256                     ? 1 : 0);
14257   if (mi_delta < 0)
14258     mi_delta = - mi_delta;
14259   if (TARGET_THUMB)
14260     {
14261       int labelno = thunk_label++;
14262       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14263       fputs ("\tldr\tr12, ", file);
14264       assemble_name (file, label);
14265       fputc ('\n', file);
14266     }
14267   while (mi_delta != 0)
14268     {
14269       if ((mi_delta & (3 << shift)) == 0)
14270         shift += 2;
14271       else
14272         {
14273           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14274                        mi_op, this_regno, this_regno,
14275                        mi_delta & (0xff << shift));
14276           mi_delta &= ~(0xff << shift);
14277           shift += 8;
14278         }
14279     }
14280   if (TARGET_THUMB)
14281     {
14282       fprintf (file, "\tbx\tr12\n");
14283       ASM_OUTPUT_ALIGN (file, 2);
14284       assemble_name (file, label);
14285       fputs (":\n", file);
14286       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14287     }
14288   else
14289     {
14290       fputs ("\tb\t", file);
14291       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14292       if (NEED_PLT_RELOC)
14293         fputs ("(PLT)", file);
14294       fputc ('\n', file);
14295     }
14296 }
14297
14298 int
14299 arm_emit_vector_const (FILE *file, rtx x)
14300 {
14301   int i;
14302   const char * pattern;
14303
14304   if (GET_CODE (x) != CONST_VECTOR)
14305     abort ();
14306
14307   switch (GET_MODE (x))
14308     {
14309     case V2SImode: pattern = "%08x"; break;
14310     case V4HImode: pattern = "%04x"; break;
14311     case V8QImode: pattern = "%02x"; break;
14312     default:       abort ();
14313     }
14314
14315   fprintf (file, "0x");
14316   for (i = CONST_VECTOR_NUNITS (x); i--;)
14317     {
14318       rtx element;
14319
14320       element = CONST_VECTOR_ELT (x, i);
14321       fprintf (file, pattern, INTVAL (element));
14322     }
14323
14324   return 1;
14325 }
14326
14327 const char *
14328 arm_output_load_gr (rtx *operands)
14329 {
14330   rtx reg;
14331   rtx offset;
14332   rtx wcgr;
14333   rtx sum;
14334   
14335   if (GET_CODE (operands [1]) != MEM
14336       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14337       || GET_CODE (reg = XEXP (sum, 0)) != REG
14338       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14339       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14340     return "wldrw%?\t%0, %1";
14341   
14342   /* Fix up an out-of-range load of a GR register.  */  
14343   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14344   wcgr = operands[0];
14345   operands[0] = reg;
14346   output_asm_insn ("ldr%?\t%0, %1", operands);
14347
14348   operands[0] = wcgr;
14349   operands[1] = reg;
14350   output_asm_insn ("tmcr%?\t%0, %1", operands);
14351   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14352
14353   return "";
14354 }
14355
14356 static rtx
14357 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14358                       int incoming ATTRIBUTE_UNUSED)
14359 {
14360 #if 0
14361   /* FIXME: The ARM backend has special code to handle structure
14362          returns, and will reserve its own hidden first argument.  So
14363          if this macro is enabled a *second* hidden argument will be
14364          reserved, which will break binary compatibility with old
14365          toolchains and also thunk handling.  One day this should be
14366          fixed.  */
14367   return 0;
14368 #else
14369   /* Register in which address to store a structure value
14370      is passed to a function.  */
14371   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14372 #endif
14373 }
14374
14375 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14376
14377    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14378    named arg and all anonymous args onto the stack.
14379    XXX I know the prologue shouldn't be pushing registers, but it is faster
14380    that way.  */
14381
14382 static void
14383 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14384                             enum machine_mode mode ATTRIBUTE_UNUSED,
14385                             tree type ATTRIBUTE_UNUSED,
14386                             int *pretend_size,
14387                             int second_time ATTRIBUTE_UNUSED)
14388 {
14389   cfun->machine->uses_anonymous_args = 1;
14390   if (cum->nregs < NUM_ARG_REGS)
14391     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14392 }
14393
14394 /* Return nonzero if the CONSUMER instruction (a store) does not need
14395    PRODUCER's value to calculate the address.  */
14396
14397 int
14398 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14399 {
14400   rtx value = PATTERN (producer);
14401   rtx addr = PATTERN (consumer);
14402
14403   if (GET_CODE (value) == COND_EXEC)
14404     value = COND_EXEC_CODE (value);
14405   if (GET_CODE (value) == PARALLEL)
14406     value = XVECEXP (value, 0, 0);
14407   value = XEXP (value, 0);
14408   if (GET_CODE (addr) == COND_EXEC)
14409     addr = COND_EXEC_CODE (addr);
14410   if (GET_CODE (addr) == PARALLEL)
14411     addr = XVECEXP (addr, 0, 0);
14412   addr = XEXP (addr, 0);
14413   
14414   return !reg_overlap_mentioned_p (value, addr);
14415 }
14416
14417 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14418    have an early register shift value or amount dependency on the
14419    result of PRODUCER.  */
14420
14421 int
14422 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14423 {
14424   rtx value = PATTERN (producer);
14425   rtx op = PATTERN (consumer);
14426   rtx early_op;
14427
14428   if (GET_CODE (value) == COND_EXEC)
14429     value = COND_EXEC_CODE (value);
14430   if (GET_CODE (value) == PARALLEL)
14431     value = XVECEXP (value, 0, 0);
14432   value = XEXP (value, 0);
14433   if (GET_CODE (op) == COND_EXEC)
14434     op = COND_EXEC_CODE (op);
14435   if (GET_CODE (op) == PARALLEL)
14436     op = XVECEXP (op, 0, 0);
14437   op = XEXP (op, 1);
14438   
14439   early_op = XEXP (op, 0);
14440   /* This is either an actual independent shift, or a shift applied to
14441      the first operand of another operation.  We want the whole shift
14442      operation.  */
14443   if (GET_CODE (early_op) == REG)
14444     early_op = op;
14445
14446   return !reg_overlap_mentioned_p (value, early_op);
14447 }
14448
14449 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14450    have an early register shift value dependency on the result of
14451    PRODUCER.  */
14452
14453 int
14454 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14455 {
14456   rtx value = PATTERN (producer);
14457   rtx op = PATTERN (consumer);
14458   rtx early_op;
14459
14460   if (GET_CODE (value) == COND_EXEC)
14461     value = COND_EXEC_CODE (value);
14462   if (GET_CODE (value) == PARALLEL)
14463     value = XVECEXP (value, 0, 0);
14464   value = XEXP (value, 0);
14465   if (GET_CODE (op) == COND_EXEC)
14466     op = COND_EXEC_CODE (op);
14467   if (GET_CODE (op) == PARALLEL)
14468     op = XVECEXP (op, 0, 0);
14469   op = XEXP (op, 1);
14470   
14471   early_op = XEXP (op, 0);
14472
14473   /* This is either an actual independent shift, or a shift applied to
14474      the first operand of another operation.  We want the value being
14475      shifted, in either case.  */
14476   if (GET_CODE (early_op) != REG)
14477     early_op = XEXP (early_op, 0);
14478   
14479   return !reg_overlap_mentioned_p (value, early_op);
14480 }
14481
14482 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14483    have an early register mult dependency on the result of
14484    PRODUCER.  */
14485
14486 int
14487 arm_no_early_mul_dep (rtx producer, rtx consumer)
14488 {
14489   rtx value = PATTERN (producer);
14490   rtx op = PATTERN (consumer);
14491
14492   if (GET_CODE (value) == COND_EXEC)
14493     value = COND_EXEC_CODE (value);
14494   if (GET_CODE (value) == PARALLEL)
14495     value = XVECEXP (value, 0, 0);
14496   value = XEXP (value, 0);
14497   if (GET_CODE (op) == COND_EXEC)
14498     op = COND_EXEC_CODE (op);
14499   if (GET_CODE (op) == PARALLEL)
14500     op = XVECEXP (op, 0, 0);
14501   op = XEXP (op, 1);
14502   
14503   return (GET_CODE (op) == PLUS
14504           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14505 }
14506
14507
14508 /* We can't rely on the caller doing the proper promotion when
14509    using APCS or ATPCS.  */
14510
14511 static bool
14512 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14513 {
14514     return !TARGET_AAPCS_BASED;
14515 }
14516
14517
14518 /* AAPCS based ABIs use short enums by default.  */
14519
14520 static bool
14521 arm_default_short_enums (void)
14522 {
14523   return TARGET_AAPCS_BASED;
14524 }
14525
14526
14527 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14528
14529 static bool
14530 arm_align_anon_bitfield (void)
14531 {
14532   return TARGET_AAPCS_BASED;
14533 }
14534
14535
14536 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14537
14538 static tree
14539 arm_cxx_guard_type (void)
14540 {
14541   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14542 }
14543
14544
14545 /* The EABI says test the least significan bit of a guard variable.  */
14546
14547 static bool
14548 arm_cxx_guard_mask_bit (void)
14549 {
14550   return TARGET_AAPCS_BASED;
14551 }
14552
14553
14554 /* The EABI specifies that all array cookies are 8 bytes long.  */
14555
14556 static tree
14557 arm_get_cookie_size (tree type)
14558 {
14559   tree size;
14560
14561   if (!TARGET_AAPCS_BASED)
14562     return default_cxx_get_cookie_size (type);
14563
14564   size = build_int_2 (8, 0);
14565   TREE_TYPE (size) = sizetype;
14566   return size;
14567 }
14568
14569
14570 /* The EABI says that array cookies should also contain the element size.  */
14571
14572 static bool
14573 arm_cookie_has_size (void)
14574 {
14575   return TARGET_AAPCS_BASED;
14576 }