OSDN Git Service

* configure.gcc: Default ep9312 to hard-float.
[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 arm_use_dfa_pipeline_interface (void);
121 static int count_insns_for_constant (HOST_WIDE_INT, int);
122 static int arm_get_strip_length (int);
123 static bool arm_function_ok_for_sibcall (tree, tree);
124 static void arm_internal_label (FILE *, const char *, unsigned long);
125 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126                                  tree);
127 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144
145 #ifdef OBJECT_FORMAT_ELF
146 static void arm_elf_asm_named_section (const char *, unsigned int);
147 #endif
148 #ifndef ARM_PE
149 static void arm_encode_section_info (tree, rtx, int);
150 #endif
151 #ifdef AOF_ASSEMBLER
152 static void aof_globalize_label (FILE *, const char *);
153 static void aof_dump_imports (FILE *);
154 static void aof_dump_pic_table (FILE *);
155 static void aof_file_start (void);
156 static void aof_file_end (void);
157 #endif
158 static rtx arm_struct_value_rtx (tree, int);
159 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
160                                         tree, int *, int);
161 static bool arm_promote_prototypes (tree);
162
163 \f
164 /* Initialize the GCC target structure.  */
165 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
166 #undef  TARGET_MERGE_DECL_ATTRIBUTES
167 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
168 #endif
169
170 #undef  TARGET_ATTRIBUTE_TABLE
171 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
172
173 #ifdef AOF_ASSEMBLER
174 #undef  TARGET_ASM_BYTE_OP
175 #define TARGET_ASM_BYTE_OP "\tDCB\t"
176 #undef  TARGET_ASM_ALIGNED_HI_OP
177 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
178 #undef  TARGET_ASM_ALIGNED_SI_OP
179 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
180 #undef TARGET_ASM_GLOBALIZE_LABEL
181 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
182 #undef TARGET_ASM_FILE_START
183 #define TARGET_ASM_FILE_START aof_file_start
184 #undef TARGET_ASM_FILE_END
185 #define TARGET_ASM_FILE_END aof_file_end
186 #else
187 #undef  TARGET_ASM_ALIGNED_SI_OP
188 #define TARGET_ASM_ALIGNED_SI_OP NULL
189 #undef  TARGET_ASM_INTEGER
190 #define TARGET_ASM_INTEGER arm_assemble_integer
191 #endif
192
193 #undef  TARGET_ASM_FUNCTION_PROLOGUE
194 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
195
196 #undef  TARGET_ASM_FUNCTION_EPILOGUE
197 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
198
199 #undef  TARGET_COMP_TYPE_ATTRIBUTES
200 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
201
202 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
203 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
204
205 #undef  TARGET_SCHED_ADJUST_COST
206 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
207
208 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
209 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE arm_use_dfa_pipeline_interface
210
211 #undef TARGET_ENCODE_SECTION_INFO
212 #ifdef ARM_PE
213 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
214 #else
215 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
216 #endif
217
218 #undef  TARGET_STRIP_NAME_ENCODING
219 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
220
221 #undef  TARGET_ASM_INTERNAL_LABEL
222 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
223
224 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
225 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
226
227 #undef  TARGET_ASM_OUTPUT_MI_THUNK
228 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
229 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
230 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
231
232 /* This will be overridden in arm_override_options.  */
233 #undef  TARGET_RTX_COSTS
234 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
235 #undef  TARGET_ADDRESS_COST
236 #define TARGET_ADDRESS_COST arm_address_cost
237
238 #undef  TARGET_MACHINE_DEPENDENT_REORG
239 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
240
241 #undef  TARGET_INIT_BUILTINS
242 #define TARGET_INIT_BUILTINS  arm_init_builtins
243 #undef  TARGET_EXPAND_BUILTIN
244 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
245
246 #undef TARGET_PROMOTE_FUNCTION_ARGS
247 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
248 #undef TARGET_PROMOTE_FUNCTION_RETURN
249 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
250 #undef TARGET_PROMOTE_PROTOTYPES
251 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
252
253 #undef TARGET_STRUCT_VALUE_RTX
254 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
255
256 #undef  TARGET_SETUP_INCOMING_VARARGS
257 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
258
259 struct gcc_target targetm = TARGET_INITIALIZER;
260 \f
261 /* Obstack for minipool constant handling.  */
262 static struct obstack minipool_obstack;
263 static char *         minipool_startobj;
264
265 /* The maximum number of insns skipped which
266    will be conditionalised if possible.  */
267 static int max_insns_skipped = 5;
268
269 extern FILE * asm_out_file;
270
271 /* True if we are currently building a constant table.  */
272 int making_const_table;
273
274 /* Define the information needed to generate branch insns.  This is
275    stored from the compare operation.  */
276 rtx arm_compare_op0, arm_compare_op1;
277
278 /* The processor for which instructions should be scheduled.  */
279 enum processor_type arm_tune = arm_none;
280
281 /* Which floating point model to use.  */
282 enum arm_fp_model arm_fp_model;
283
284 /* Which floating point hardware is available.  */
285 enum fputype arm_fpu_arch;
286
287 /* Which floating point hardware to schedule for.  */
288 enum fputype arm_fpu_tune;
289
290 /* Whether to use floating point hardware.  */
291 enum float_abi_type arm_float_abi;
292
293 /* Which ABI to use.  */
294 enum arm_abi_type arm_abi;
295
296 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode.  */
297 enum prog_mode_type arm_prgmode;
298
299 /* Set by the -mfpu=... option.  */
300 const char * target_fpu_name = NULL;
301
302 /* Set by the -mfpe=... option.  */
303 const char * target_fpe_name = NULL;
304
305 /* Set by the -mfloat-abi=... option.  */
306 const char * target_float_abi_name = NULL;
307
308 /* Set by the -mabi=... option.  */
309 const char * target_abi_name = NULL;
310
311 /* Used to parse -mstructure_size_boundary command line option.  */
312 const char * structure_size_string = NULL;
313 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
314
315 /* Bit values used to identify processor capabilities.  */
316 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
317 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
318 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
319 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
320 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
321 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
322 #define FL_THUMB      (1 << 6)        /* Thumb aware */
323 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
324 #define FL_STRONG     (1 << 8)        /* StrongARM */
325 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
326 #define FL_XSCALE     (1 << 10)       /* XScale */
327 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
328 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
329                                          media instructions.  */
330 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
331
332 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
333
334 #define FL_FOR_ARCH2    0
335 #define FL_FOR_ARCH3    FL_MODE32
336 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
337 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
338 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
339 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
340 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
341 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
342 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
343 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
344 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
345 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
346
347 /* The bits in this mask specify which
348    instructions we are allowed to generate.  */
349 static unsigned long insn_flags = 0;
350
351 /* The bits in this mask specify which instruction scheduling options should
352    be used.  */
353 static unsigned long tune_flags = 0;
354
355 /* The following are used in the arm.md file as equivalents to bits
356    in the above two flag variables.  */
357
358 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
359 int arm_arch3m = 0;
360
361 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
362 int arm_arch4 = 0;
363
364 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
365 int arm_arch5 = 0;
366
367 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
368 int arm_arch5e = 0;
369
370 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
371 int arm_arch6 = 0;
372
373 /* Nonzero if this chip can benefit from load scheduling.  */
374 int arm_ld_sched = 0;
375
376 /* Nonzero if this chip is a StrongARM.  */
377 int arm_is_strong = 0;
378
379 /* Nonzero if this chip is a Cirrus variant.  */
380 int arm_arch_cirrus = 0;
381
382 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
383 int arm_arch_iwmmxt = 0;
384
385 /* Nonzero if this chip is an XScale.  */
386 int arm_arch_xscale = 0;
387
388 /* Nonzero if tuning for XScale  */
389 int arm_tune_xscale = 0;
390
391 /* Nonzero if this chip is an ARM6 or an ARM7.  */
392 int arm_is_6_or_7 = 0;
393
394 /* Nonzero if generating Thumb instructions.  */
395 int thumb_code = 0;
396
397 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
398    must report the mode of the memory reference from PRINT_OPERAND to
399    PRINT_OPERAND_ADDRESS.  */
400 enum machine_mode output_memory_reference_mode;
401
402 /* The register number to be used for the PIC offset register.  */
403 const char * arm_pic_register_string = NULL;
404 int arm_pic_register = INVALID_REGNUM;
405
406 /* Set to 1 when a return insn is output, this means that the epilogue
407    is not needed.  */
408 int return_used_this_function;
409
410 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
411    the next function.  */
412 static int after_arm_reorg = 0;
413
414 /* The maximum number of insns to be used when loading a constant.  */
415 static int arm_constant_limit = 3;
416
417 /* For an explanation of these variables, see final_prescan_insn below.  */
418 int arm_ccfsm_state;
419 enum arm_cond_code arm_current_cc;
420 rtx arm_target_insn;
421 int arm_target_label;
422
423 /* The condition codes of the ARM, and the inverse function.  */
424 static const char * const arm_condition_codes[] =
425 {
426   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
427   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
428 };
429
430 #define streq(string1, string2) (strcmp (string1, string2) == 0)
431 \f
432 /* Initialization code.  */
433
434 struct processors
435 {
436   const char *const name;
437   enum processor_type core;
438   const char *arch;
439   const unsigned long flags;
440   bool (* rtx_costs) (rtx, int, int, int *);
441 };
442
443 /* Not all of these give usefully different compilation alternatives,
444    but there is no simple way of generalizing them.  */
445 static const struct processors all_cores[] =
446 {
447   /* ARM Cores */
448 #define ARM_CORE(NAME, ARCH, FLAGS, COSTS) \
449   {#NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
450 #include "arm-cores.def"
451 #undef ARM_CORE
452   {NULL, arm_none, NULL, 0, NULL}
453 };
454
455 static const struct processors all_architectures[] =
456 {
457   /* ARM Architectures */
458   /* We don't specify rtx_costs here as it will be figured out
459      from the core.  */
460   
461   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
462   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
463   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
464   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
465   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
466   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
467      implementations that support it, so we will leave it out for now.  */
468   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
469   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
470   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
471   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
472   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
473   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
474   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
475   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
476   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
477   {NULL, arm_none, NULL, 0 , NULL}
478 };
479
480 /* This is a magic structure.  The 'string' field is magically filled in
481    with a pointer to the value specified by the user on the command line
482    assuming that the user has specified such a value.  */
483
484 struct arm_cpu_select arm_select[] =
485 {
486   /* string       name            processors  */        
487   { NULL,       "-mcpu=",       all_cores  },
488   { NULL,       "-march=",      all_architectures },
489   { NULL,       "-mtune=",      all_cores }
490 };
491
492
493 /* The name of the proprocessor macro to define for this architecture.  */
494
495 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
496
497 struct fpu_desc
498 {
499   const char * name;
500   enum fputype fpu;
501 };
502
503
504 /* Available values for for -mfpu=.  */
505
506 static const struct fpu_desc all_fpus[] =
507 {
508   {"fpa",       FPUTYPE_FPA},
509   {"fpe2",      FPUTYPE_FPA_EMU2},
510   {"fpe3",      FPUTYPE_FPA_EMU2},
511   {"maverick",  FPUTYPE_MAVERICK},
512   {"vfp",       FPUTYPE_VFP}
513 };
514
515
516 /* Floating point models used by the different hardware.
517    See fputype in arm.h.  */
518
519 static const enum fputype fp_model_for_fpu[] =
520 {
521   /* No FP hardware.  */
522   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
523   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
524   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
525   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
526   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
527   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
528 };
529
530
531 struct float_abi
532 {
533   const char * name;
534   enum float_abi_type abi_type;
535 };
536
537
538 /* Available values for -mfloat-abi=.  */
539
540 static const struct float_abi all_float_abis[] =
541 {
542   {"soft",      ARM_FLOAT_ABI_SOFT},
543   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
544   {"hard",      ARM_FLOAT_ABI_HARD}
545 };
546
547
548 struct abi_name
549 {
550   const char *name;
551   enum arm_abi_type abi_type;
552 };
553
554
555 /* Available values for -mabi=.  */
556
557 static const struct abi_name arm_all_abis[] =
558 {
559   {"apcs-gnu",    ARM_ABI_APCS},
560   {"atpcs",   ARM_ABI_ATPCS},
561   {"aapcs",   ARM_ABI_AAPCS},
562   {"iwmmxt",  ARM_ABI_IWMMXT}
563 };
564
565 /* Return the number of bits set in VALUE.  */
566 static unsigned
567 bit_count (unsigned long value)
568 {
569   unsigned long count = 0;
570   
571   while (value)
572     {
573       count++;
574       value &= value - 1;  /* Clear the least-significant set bit.  */
575     }
576
577   return count;
578 }
579
580 /* Fix up any incompatible options that the user has specified.
581    This has now turned into a maze.  */
582 void
583 arm_override_options (void)
584 {
585   unsigned i;
586
587   /* Set up the flags based on the cpu/architecture selected by the user.  */
588   for (i = ARRAY_SIZE (arm_select); i--;)
589     {
590       struct arm_cpu_select * ptr = arm_select + i;
591       
592       if (ptr->string != NULL && ptr->string[0] != '\0')
593         {
594           const struct processors * sel;
595
596           for (sel = ptr->processors; sel->name != NULL; sel++)
597             if (streq (ptr->string, sel->name))
598               {
599                 /* Set the architecture define.  */
600                 if (i != 2)
601                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
602
603                 /* Determine the processor core for which we should
604                    tune code-generation.  */
605                 if (/* -mcpu= is a sensible default.  */
606                     i == 0
607                     /* If -march= is used, and -mcpu= has not been used,
608                        assume that we should tune for a representative
609                        CPU from that architecture.  */
610                     || i == 1
611                     /* -mtune= overrides -mcpu= and -march=.  */
612                     || i == 2)
613                   arm_tune = (enum processor_type) (sel - ptr->processors);
614
615                 if (i != 2)
616                   {
617                     /* If we have been given an architecture and a processor
618                        make sure that they are compatible.  We only generate
619                        a warning though, and we prefer the CPU over the
620                        architecture.  */
621                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
622                       warning ("switch -mcpu=%s conflicts with -march= switch",
623                                ptr->string);
624                     
625                     insn_flags = sel->flags;
626                   }
627                 
628                 break;
629               }
630
631           if (sel->name == NULL)
632             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
633         }
634     }
635   
636   /* If the user did not specify a processor, choose one for them.  */
637   if (insn_flags == 0)
638     {
639       const struct processors * sel;
640       unsigned int        sought;
641       enum processor_type cpu;
642
643       cpu = TARGET_CPU_DEFAULT;
644       if (cpu == arm_none)
645         {
646 #ifdef SUBTARGET_CPU_DEFAULT
647           /* Use the subtarget default CPU if none was specified by
648              configure.  */
649           cpu = SUBTARGET_CPU_DEFAULT;
650 #endif
651           /* Default to ARM6.  */
652           if (cpu == arm_none)
653             cpu = arm6;
654         }
655       sel = &all_cores[cpu];
656
657       insn_flags = sel->flags;
658
659       /* Now check to see if the user has specified some command line
660          switch that require certain abilities from the cpu.  */
661       sought = 0;
662       
663       if (TARGET_INTERWORK || TARGET_THUMB)
664         {
665           sought |= (FL_THUMB | FL_MODE32);
666           
667           /* Force apcs-32 to be used for interworking.  */
668           target_flags |= ARM_FLAG_APCS_32;
669
670           /* There are no ARM processors that support both APCS-26 and
671              interworking.  Therefore we force FL_MODE26 to be removed
672              from insn_flags here (if it was set), so that the search
673              below will always be able to find a compatible processor.  */
674           insn_flags &= ~FL_MODE26;
675         }
676       else if (!TARGET_APCS_32)
677         sought |= FL_MODE26;
678       
679       if (sought != 0 && ((sought & insn_flags) != sought))
680         {
681           /* Try to locate a CPU type that supports all of the abilities
682              of the default CPU, plus the extra abilities requested by
683              the user.  */
684           for (sel = all_cores; sel->name != NULL; sel++)
685             if ((sel->flags & sought) == (sought | insn_flags))
686               break;
687
688           if (sel->name == NULL)
689             {
690               unsigned current_bit_count = 0;
691               const struct processors * best_fit = NULL;
692               
693               /* Ideally we would like to issue an error message here
694                  saying that it was not possible to find a CPU compatible
695                  with the default CPU, but which also supports the command
696                  line options specified by the programmer, and so they
697                  ought to use the -mcpu=<name> command line option to
698                  override the default CPU type.
699
700                  Unfortunately this does not work with multilibing.  We
701                  need to be able to support multilibs for -mapcs-26 and for
702                  -mthumb-interwork and there is no CPU that can support both
703                  options.  Instead if we cannot find a cpu that has both the
704                  characteristics of the default cpu and the given command line
705                  options we scan the array again looking for a best match.  */
706               for (sel = all_cores; sel->name != NULL; sel++)
707                 if ((sel->flags & sought) == sought)
708                   {
709                     unsigned count;
710
711                     count = bit_count (sel->flags & insn_flags);
712
713                     if (count >= current_bit_count)
714                       {
715                         best_fit = sel;
716                         current_bit_count = count;
717                       }
718                   }
719
720               if (best_fit == NULL)
721                 abort ();
722               else
723                 sel = best_fit;
724             }
725
726           insn_flags = sel->flags;
727         }
728       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
729       if (arm_tune == arm_none)
730         arm_tune = (enum processor_type) (sel - all_cores);
731     }
732   
733   /* The processor for which we should tune should now have been
734      chosen.  */
735   if (arm_tune == arm_none)
736     abort ();
737   
738   tune_flags = all_cores[(int)arm_tune].flags;
739   targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
740
741   /* Make sure that the processor choice does not conflict with any of the
742      other command line choices.  */
743   if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
744     {
745       /* If APCS-32 was not the default then it must have been set by the
746          user, so issue a warning message.  If the user has specified
747          "-mapcs-32 -mcpu=arm2" then we loose here.  */
748       if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
749         warning ("target CPU does not support APCS-32" );
750       target_flags &= ~ARM_FLAG_APCS_32;
751     }
752   else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
753     {
754       warning ("target CPU does not support APCS-26" );
755       target_flags |= ARM_FLAG_APCS_32;
756     }
757   
758   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
759     {
760       warning ("target CPU does not support interworking" );
761       target_flags &= ~ARM_FLAG_INTERWORK;
762     }
763   
764   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
765     {
766       warning ("target CPU does not support THUMB instructions");
767       target_flags &= ~ARM_FLAG_THUMB;
768     }
769
770   if (TARGET_APCS_FRAME && TARGET_THUMB)
771     {
772       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
773       target_flags &= ~ARM_FLAG_APCS_FRAME;
774     }
775
776   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
777      from here where no function is being compiled currently.  */
778   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
779       && TARGET_ARM)
780     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
781
782   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
783     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
784
785   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
786     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
787
788   /* If interworking is enabled then APCS-32 must be selected as well.  */
789   if (TARGET_INTERWORK)
790     {
791       if (!TARGET_APCS_32)
792         warning ("interworking forces APCS-32 to be used" );
793       target_flags |= ARM_FLAG_APCS_32;
794     }
795   
796   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
797     {
798       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
799       target_flags |= ARM_FLAG_APCS_FRAME;
800     }
801   
802   if (TARGET_POKE_FUNCTION_NAME)
803     target_flags |= ARM_FLAG_APCS_FRAME;
804   
805   if (TARGET_APCS_REENT && flag_pic)
806     error ("-fpic and -mapcs-reent are incompatible");
807   
808   if (TARGET_APCS_REENT)
809     warning ("APCS reentrant code not supported.  Ignored");
810   
811   /* If this target is normally configured to use APCS frames, warn if they
812      are turned off and debugging is turned on.  */
813   if (TARGET_ARM
814       && write_symbols != NO_DEBUG
815       && !TARGET_APCS_FRAME
816       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
817     warning ("-g with -mno-apcs-frame may not give sensible debugging");
818   
819   /* If stack checking is disabled, we can use r10 as the PIC register,
820      which keeps r9 available.  */
821   if (flag_pic)
822     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
823   
824   if (TARGET_APCS_FLOAT)
825     warning ("passing floating point arguments in fp regs not yet supported");
826   
827   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
828   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
829   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
830   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
831   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
832   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
833   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
834   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
835
836   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
837   arm_is_strong = (tune_flags & FL_STRONG) != 0;
838   thumb_code = (TARGET_ARM == 0);
839   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
840                     && !(tune_flags & FL_ARCH4))) != 0;
841   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
842   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
843
844   if (target_abi_name)
845     {
846       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
847         {
848           if (streq (arm_all_abis[i].name, target_abi_name))
849             {
850               arm_abi = arm_all_abis[i].abi_type;
851               break;
852             }
853         }
854       if (i == ARRAY_SIZE (arm_all_abis))
855         error ("invalid ABI option: -mabi=%s", target_abi_name);
856     }
857   else
858     arm_abi = ARM_DEFAULT_ABI;
859
860   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
861     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
862
863   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
864     error ("iwmmxt abi requires an iwmmxt capable cpu");
865
866   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
867   if (target_fpu_name == NULL && target_fpe_name != NULL)
868     {
869       if (streq (target_fpe_name, "2"))
870         target_fpu_name = "fpe2";
871       else if (streq (target_fpe_name, "3"))
872         target_fpu_name = "fpe3";
873       else
874         error ("invalid floating point emulation option: -mfpe=%s",
875                target_fpe_name);
876     }
877   if (target_fpu_name != NULL)
878     {
879       /* The user specified a FPU.  */
880       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
881         {
882           if (streq (all_fpus[i].name, target_fpu_name))
883             {
884               arm_fpu_arch = all_fpus[i].fpu;
885               arm_fpu_tune = arm_fpu_arch;
886               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
887               break;
888             }
889         }
890       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
891         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
892     }
893   else
894     {
895 #ifdef FPUTYPE_DEFAULT
896       /* Use the default if it is specified for this platform.  */
897       arm_fpu_arch = FPUTYPE_DEFAULT;
898       arm_fpu_tune = FPUTYPE_DEFAULT;
899 #else
900       /* Pick one based on CPU type.  */
901       /* ??? Some targets assume FPA is the default.
902       if ((insn_flags & FL_VFP) != 0)
903         arm_fpu_arch = FPUTYPE_VFP;
904       else
905       */
906       if (arm_arch_cirrus)
907         arm_fpu_arch = FPUTYPE_MAVERICK;
908       else
909         arm_fpu_arch = FPUTYPE_FPA_EMU2;
910 #endif
911       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
912         arm_fpu_tune = FPUTYPE_FPA;
913       else
914         arm_fpu_tune = arm_fpu_arch;
915       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
916       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
917         abort ();
918     }
919
920   if (target_float_abi_name != NULL)
921     {
922       /* The user specified a FP ABI.  */
923       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
924         {
925           if (streq (all_float_abis[i].name, target_float_abi_name))
926             {
927               arm_float_abi = all_float_abis[i].abi_type;
928               break;
929             }
930         }
931       if (i == ARRAY_SIZE (all_float_abis))
932         error ("invalid floating point abi: -mfloat-abi=%s",
933                target_float_abi_name);
934     }
935   else
936     {
937       /* Use soft-float target flag.  */
938       if (target_flags & ARM_FLAG_SOFT_FLOAT)
939         arm_float_abi = ARM_FLOAT_ABI_SOFT;
940       else
941         arm_float_abi = ARM_FLOAT_ABI_HARD;
942     }
943
944   if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
945     sorry ("-mfloat-abi=softfp");
946   /* If soft-float is specified then don't use FPU.  */
947   if (TARGET_SOFT_FLOAT)
948     arm_fpu_arch = FPUTYPE_NONE;
949   
950   /* For arm2/3 there is no need to do any scheduling if there is only
951      a floating point emulator, or we are doing software floating-point.  */
952   if ((TARGET_SOFT_FLOAT
953        || arm_fpu_tune == FPUTYPE_FPA_EMU2
954        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
955       && (tune_flags & FL_MODE32) == 0)
956     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
957   
958   arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
959   
960   /* Override the default structure alignment for AAPCS ABI.  */
961   if (arm_abi == ARM_ABI_AAPCS)
962     arm_structure_size_boundary = 8;
963
964   if (structure_size_string != NULL)
965     {
966       int size = strtol (structure_size_string, NULL, 0);
967
968       if (size == 8 || size == 32
969           || (ARM_DOUBLEWORD_ALIGN && size == 64))
970         arm_structure_size_boundary = size;
971       else
972         warning ("structure size boundary can only be set to %s",
973                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
974     }
975
976   if (arm_pic_register_string != NULL)
977     {
978       int pic_register = decode_reg_name (arm_pic_register_string);
979
980       if (!flag_pic)
981         warning ("-mpic-register= is useless without -fpic");
982
983       /* Prevent the user from choosing an obviously stupid PIC register.  */
984       else if (pic_register < 0 || call_used_regs[pic_register]
985                || pic_register == HARD_FRAME_POINTER_REGNUM
986                || pic_register == STACK_POINTER_REGNUM
987                || pic_register >= PC_REGNUM)
988         error ("unable to use '%s' for PIC register", arm_pic_register_string);
989       else
990         arm_pic_register = pic_register;
991     }
992
993   if (TARGET_THUMB && flag_schedule_insns)
994     {
995       /* Don't warn since it's on by default in -O2.  */
996       flag_schedule_insns = 0;
997     }
998
999   if (optimize_size)
1000     {
1001       /* There's some dispute as to whether this should be 1 or 2.  However,
1002          experiments seem to show that in pathological cases a setting of
1003          1 degrades less severely than a setting of 2.  This could change if
1004          other parts of the compiler change their behavior.  */
1005       arm_constant_limit = 1;
1006
1007       /* If optimizing for size, bump the number of instructions that we
1008          are prepared to conditionally execute (even on a StrongARM).  */
1009       max_insns_skipped = 6;
1010     }
1011   else
1012     {
1013       /* For processors with load scheduling, it never costs more than
1014          2 cycles to load a constant, and the load scheduler may well
1015          reduce that to 1.  */
1016       if (tune_flags & FL_LDSCHED)
1017         arm_constant_limit = 1;
1018
1019       /* On XScale the longer latency of a load makes it more difficult
1020          to achieve a good schedule, so it's faster to synthesize
1021          constants that can be done in two insns.  */
1022       if (arm_tune_xscale)
1023         arm_constant_limit = 2;
1024
1025       /* StrongARM has early execution of branches, so a sequence
1026          that is worth skipping is shorter.  */
1027       if (arm_is_strong)
1028         max_insns_skipped = 3;
1029     }
1030
1031   /* Register global variables with the garbage collector.  */
1032   arm_add_gc_roots ();
1033 }
1034
1035 static void
1036 arm_add_gc_roots (void)
1037 {
1038   gcc_obstack_init(&minipool_obstack);
1039   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1040 }
1041 \f
1042 /* A table of known ARM exception types.
1043    For use with the interrupt function attribute.  */
1044
1045 typedef struct
1046 {
1047   const char *const arg;
1048   const unsigned long return_value;
1049 }
1050 isr_attribute_arg;
1051
1052 static const isr_attribute_arg isr_attribute_args [] =
1053 {
1054   { "IRQ",   ARM_FT_ISR },
1055   { "irq",   ARM_FT_ISR },
1056   { "FIQ",   ARM_FT_FIQ },
1057   { "fiq",   ARM_FT_FIQ },
1058   { "ABORT", ARM_FT_ISR },
1059   { "abort", ARM_FT_ISR },
1060   { "ABORT", ARM_FT_ISR },
1061   { "abort", ARM_FT_ISR },
1062   { "UNDEF", ARM_FT_EXCEPTION },
1063   { "undef", ARM_FT_EXCEPTION },
1064   { "SWI",   ARM_FT_EXCEPTION },
1065   { "swi",   ARM_FT_EXCEPTION },
1066   { NULL,    ARM_FT_NORMAL }
1067 };
1068
1069 /* Returns the (interrupt) function type of the current
1070    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1071
1072 static unsigned long
1073 arm_isr_value (tree argument)
1074 {
1075   const isr_attribute_arg * ptr;
1076   const char *              arg;
1077
1078   /* No argument - default to IRQ.  */
1079   if (argument == NULL_TREE)
1080     return ARM_FT_ISR;
1081
1082   /* Get the value of the argument.  */
1083   if (TREE_VALUE (argument) == NULL_TREE
1084       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1085     return ARM_FT_UNKNOWN;
1086
1087   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1088
1089   /* Check it against the list of known arguments.  */
1090   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1091     if (streq (arg, ptr->arg))
1092       return ptr->return_value;
1093
1094   /* An unrecognized interrupt type.  */
1095   return ARM_FT_UNKNOWN;
1096 }
1097
1098 /* Computes the type of the current function.  */
1099
1100 static unsigned long
1101 arm_compute_func_type (void)
1102 {
1103   unsigned long type = ARM_FT_UNKNOWN;
1104   tree a;
1105   tree attr;
1106   
1107   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1108     abort ();
1109
1110   /* Decide if the current function is volatile.  Such functions
1111      never return, and many memory cycles can be saved by not storing
1112      register values that will never be needed again.  This optimization
1113      was added to speed up context switching in a kernel application.  */
1114   if (optimize > 0
1115       && current_function_nothrow
1116       && TREE_THIS_VOLATILE (current_function_decl))
1117     type |= ARM_FT_VOLATILE;
1118   
1119   if (current_function_needs_context)
1120     type |= ARM_FT_NESTED;
1121
1122   attr = DECL_ATTRIBUTES (current_function_decl);
1123   
1124   a = lookup_attribute ("naked", attr);
1125   if (a != NULL_TREE)
1126     type |= ARM_FT_NAKED;
1127
1128   if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1129     type |= ARM_FT_EXCEPTION_HANDLER;
1130   else
1131     {
1132       a = lookup_attribute ("isr", attr);
1133       if (a == NULL_TREE)
1134         a = lookup_attribute ("interrupt", attr);
1135       
1136       if (a == NULL_TREE)
1137         type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1138       else
1139         type |= arm_isr_value (TREE_VALUE (a));
1140     }
1141   
1142   return type;
1143 }
1144
1145 /* Returns the type of the current function.  */
1146
1147 unsigned long
1148 arm_current_func_type (void)
1149 {
1150   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1151     cfun->machine->func_type = arm_compute_func_type ();
1152
1153   return cfun->machine->func_type;
1154 }
1155 \f
1156 /* Return 1 if it is possible to return using a single instruction.  
1157    If SIBLING is non-null, this is a test for a return before a sibling
1158    call.  SIBLING is the call insn, so we can examine its register usage.  */
1159
1160 int
1161 use_return_insn (int iscond, rtx sibling)
1162 {
1163   int regno;
1164   unsigned int func_type;
1165   unsigned long saved_int_regs;
1166   unsigned HOST_WIDE_INT stack_adjust;
1167   arm_stack_offsets *offsets;
1168
1169   /* Never use a return instruction before reload has run.  */
1170   if (!reload_completed)
1171     return 0;
1172
1173   func_type = arm_current_func_type ();
1174
1175   /* Naked functions and volatile functions need special
1176      consideration.  */
1177   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1178     return 0;
1179
1180   /* So do interrupt functions that use the frame pointer.  */
1181   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1182     return 0;
1183
1184   offsets = arm_get_frame_offsets ();
1185   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1186
1187   /* As do variadic functions.  */
1188   if (current_function_pretend_args_size
1189       || cfun->machine->uses_anonymous_args
1190       /* Or if the function calls __builtin_eh_return () */
1191       || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
1192       /* Or if the function calls alloca */
1193       || current_function_calls_alloca
1194       /* Or if there is a stack adjustment.  However, if the stack pointer
1195          is saved on the stack, we can use a pre-incrementing stack load.  */
1196       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1197     return 0;
1198
1199   saved_int_regs = arm_compute_save_reg_mask ();
1200
1201   /* Unfortunately, the insn
1202
1203        ldmib sp, {..., sp, ...}
1204
1205      triggers a bug on most SA-110 based devices, such that the stack
1206      pointer won't be correctly restored if the instruction takes a
1207      page fault.  We work around this problem by popping r3 along with
1208      the other registers, since that is never slower than executing
1209      another instruction.  
1210
1211      We test for !arm_arch5 here, because code for any architecture
1212      less than this could potentially be run on one of the buggy
1213      chips.  */
1214   if (stack_adjust == 4 && !arm_arch5)
1215     {
1216       /* Validate that r3 is a call-clobbered register (always true in
1217          the default abi) ...  */
1218       if (!call_used_regs[3])
1219         return 0;
1220
1221       /* ... that it isn't being used for a return value (always true
1222          until we implement return-in-regs), or for a tail-call
1223          argument ...  */
1224       if (sibling)
1225         {
1226           if (GET_CODE (sibling) != CALL_INSN)
1227             abort ();
1228
1229           if (find_regno_fusage (sibling, USE, 3))
1230             return 0;
1231         }
1232
1233       /* ... and that there are no call-saved registers in r0-r2
1234          (always true in the default ABI).  */
1235       if (saved_int_regs & 0x7)
1236         return 0;
1237     }
1238
1239   /* Can't be done if interworking with Thumb, and any registers have been
1240      stacked.  */
1241   if (TARGET_INTERWORK && saved_int_regs != 0)
1242     return 0;
1243
1244   /* On StrongARM, conditional returns are expensive if they aren't
1245      taken and multiple registers have been stacked.  */
1246   if (iscond && arm_is_strong)
1247     {
1248       /* Conditional return when just the LR is stored is a simple 
1249          conditional-load instruction, that's not expensive.  */
1250       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1251         return 0;
1252
1253       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1254         return 0;
1255     }
1256
1257   /* If there are saved registers but the LR isn't saved, then we need
1258      two instructions for the return.  */
1259   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1260     return 0;
1261
1262   /* Can't be done if any of the FPA regs are pushed,
1263      since this also requires an insn.  */
1264   if (TARGET_HARD_FLOAT && TARGET_FPA)
1265     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1266       if (regs_ever_live[regno] && !call_used_regs[regno])
1267         return 0;
1268
1269   /* Likewise VFP regs.  */
1270   if (TARGET_HARD_FLOAT && TARGET_VFP)
1271     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1272       if (regs_ever_live[regno] && !call_used_regs[regno])
1273         return 0;
1274
1275   if (TARGET_REALLY_IWMMXT)
1276     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1277       if (regs_ever_live[regno] && ! call_used_regs [regno])
1278         return 0;
1279
1280   return 1;
1281 }
1282
1283 /* Return TRUE if int I is a valid immediate ARM constant.  */
1284
1285 int
1286 const_ok_for_arm (HOST_WIDE_INT i)
1287 {
1288   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1289
1290   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must 
1291      be all zero, or all one.  */
1292   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1293       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1294           != ((~(unsigned HOST_WIDE_INT) 0)
1295               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1296     return FALSE;
1297   
1298   /* Fast return for 0 and powers of 2 */
1299   if ((i & (i - 1)) == 0)
1300     return TRUE;
1301
1302   do
1303     {
1304       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1305         return TRUE;
1306       mask =
1307           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1308                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1309     }
1310   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1311
1312   return FALSE;
1313 }
1314
1315 /* Return true if I is a valid constant for the operation CODE.  */
1316 static int
1317 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1318 {
1319   if (const_ok_for_arm (i))
1320     return 1;
1321
1322   switch (code)
1323     {
1324     case PLUS:
1325       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1326
1327     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1328     case XOR:
1329     case IOR:
1330       return 0;
1331
1332     case AND:
1333       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1334
1335     default:
1336       abort ();
1337     }
1338 }
1339
1340 /* Emit a sequence of insns to handle a large constant.
1341    CODE is the code of the operation required, it can be any of SET, PLUS,
1342    IOR, AND, XOR, MINUS;
1343    MODE is the mode in which the operation is being performed;
1344    VAL is the integer to operate on;
1345    SOURCE is the other operand (a register, or a null-pointer for SET);
1346    SUBTARGETS means it is safe to create scratch registers if that will
1347    either produce a simpler sequence, or we will want to cse the values.
1348    Return value is the number of insns emitted.  */
1349
1350 int
1351 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1352                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1353 {
1354   rtx cond;
1355
1356   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1357     cond = COND_EXEC_TEST (PATTERN (insn));
1358   else
1359     cond = NULL_RTX;
1360
1361   if (subtargets || code == SET
1362       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1363           && REGNO (target) != REGNO (source)))
1364     {
1365       /* After arm_reorg has been called, we can't fix up expensive
1366          constants by pushing them into memory so we must synthesize
1367          them in-line, regardless of the cost.  This is only likely to
1368          be more costly on chips that have load delay slots and we are
1369          compiling without running the scheduler (so no splitting
1370          occurred before the final instruction emission).
1371
1372          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1373       */
1374       if (!after_arm_reorg
1375           && !cond
1376           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source, 
1377                                 1, 0)
1378               > arm_constant_limit + (code != SET)))
1379         {
1380           if (code == SET)
1381             {
1382               /* Currently SET is the only monadic value for CODE, all
1383                  the rest are diadic.  */
1384               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1385               return 1;
1386             }
1387           else
1388             {
1389               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1390
1391               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1392               /* For MINUS, the value is subtracted from, since we never
1393                  have subtraction of a constant.  */
1394               if (code == MINUS)
1395                 emit_insn (gen_rtx_SET (VOIDmode, target,
1396                                         gen_rtx_MINUS (mode, temp, source)));
1397               else
1398                 emit_insn (gen_rtx_SET (VOIDmode, target,
1399                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1400               return 2;
1401             }
1402         }
1403     }
1404
1405   return arm_gen_constant (code, mode, cond, val, target, source, subtargets, 
1406                            1);
1407 }
1408
1409 static int
1410 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1411 {
1412   HOST_WIDE_INT temp1;
1413   int num_insns = 0;
1414   do
1415     {
1416       int end;
1417           
1418       if (i <= 0)
1419         i += 32;
1420       if (remainder & (3 << (i - 2)))
1421         {
1422           end = i - 8;
1423           if (end < 0)
1424             end += 32;
1425           temp1 = remainder & ((0x0ff << end)
1426                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1427           remainder &= ~temp1;
1428           num_insns++;
1429           i -= 6;
1430         }
1431       i -= 2;
1432     } while (remainder);
1433   return num_insns;
1434 }
1435
1436 /* Emit an instruction with the indicated PATTERN.  If COND is
1437    non-NULL, conditionalize the execution of the instruction on COND
1438    being true.  */
1439
1440 static void
1441 emit_constant_insn (rtx cond, rtx pattern)
1442 {
1443   if (cond)
1444     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1445   emit_insn (pattern);
1446 }
1447
1448 /* As above, but extra parameter GENERATE which, if clear, suppresses
1449    RTL generation.  */
1450
1451 static int
1452 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1453                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1454                   int generate)
1455 {
1456   int can_invert = 0;
1457   int can_negate = 0;
1458   int can_negate_initial = 0;
1459   int can_shift = 0;
1460   int i;
1461   int num_bits_set = 0;
1462   int set_sign_bit_copies = 0;
1463   int clear_sign_bit_copies = 0;
1464   int clear_zero_bit_copies = 0;
1465   int set_zero_bit_copies = 0;
1466   int insns = 0;
1467   unsigned HOST_WIDE_INT temp1, temp2;
1468   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1469
1470   /* Find out which operations are safe for a given CODE.  Also do a quick
1471      check for degenerate cases; these can occur when DImode operations
1472      are split.  */
1473   switch (code)
1474     {
1475     case SET:
1476       can_invert = 1;
1477       can_shift = 1;
1478       can_negate = 1;
1479       break;
1480
1481     case PLUS:
1482       can_negate = 1;
1483       can_negate_initial = 1;
1484       break;
1485
1486     case IOR:
1487       if (remainder == 0xffffffff)
1488         {
1489           if (generate)
1490             emit_constant_insn (cond,
1491                                 gen_rtx_SET (VOIDmode, target,
1492                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1493           return 1;
1494         }
1495       if (remainder == 0)
1496         {
1497           if (reload_completed && rtx_equal_p (target, source))
1498             return 0;
1499           if (generate)
1500             emit_constant_insn (cond,
1501                                 gen_rtx_SET (VOIDmode, target, source));
1502           return 1;
1503         }
1504       break;
1505
1506     case AND:
1507       if (remainder == 0)
1508         {
1509           if (generate)
1510             emit_constant_insn (cond,
1511                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1512           return 1;
1513         }
1514       if (remainder == 0xffffffff)
1515         {
1516           if (reload_completed && rtx_equal_p (target, source))
1517             return 0;
1518           if (generate)
1519             emit_constant_insn (cond,
1520                                 gen_rtx_SET (VOIDmode, target, source));
1521           return 1;
1522         }
1523       can_invert = 1;
1524       break;
1525
1526     case XOR:
1527       if (remainder == 0)
1528         {
1529           if (reload_completed && rtx_equal_p (target, source))
1530             return 0;
1531           if (generate)
1532             emit_constant_insn (cond,
1533                                 gen_rtx_SET (VOIDmode, target, source));
1534           return 1;
1535         }
1536       if (remainder == 0xffffffff)
1537         {
1538           if (generate)
1539             emit_constant_insn (cond,
1540                                 gen_rtx_SET (VOIDmode, target,
1541                                              gen_rtx_NOT (mode, source)));
1542           return 1;
1543         }
1544
1545       /* We don't know how to handle this yet below.  */
1546       abort ();
1547
1548     case MINUS:
1549       /* We treat MINUS as (val - source), since (source - val) is always
1550          passed as (source + (-val)).  */
1551       if (remainder == 0)
1552         {
1553           if (generate)
1554             emit_constant_insn (cond,
1555                                 gen_rtx_SET (VOIDmode, target,
1556                                              gen_rtx_NEG (mode, source)));
1557           return 1;
1558         }
1559       if (const_ok_for_arm (val))
1560         {
1561           if (generate)
1562             emit_constant_insn (cond,
1563                                 gen_rtx_SET (VOIDmode, target, 
1564                                              gen_rtx_MINUS (mode, GEN_INT (val),
1565                                                             source)));
1566           return 1;
1567         }
1568       can_negate = 1;
1569
1570       break;
1571
1572     default:
1573       abort ();
1574     }
1575
1576   /* If we can do it in one insn get out quickly.  */
1577   if (const_ok_for_arm (val)
1578       || (can_negate_initial && const_ok_for_arm (-val))
1579       || (can_invert && const_ok_for_arm (~val)))
1580     {
1581       if (generate)
1582         emit_constant_insn (cond,
1583                             gen_rtx_SET (VOIDmode, target,
1584                                          (source 
1585                                           ? gen_rtx_fmt_ee (code, mode, source,
1586                                                             GEN_INT (val))
1587                                           : GEN_INT (val))));
1588       return 1;
1589     }
1590
1591   /* Calculate a few attributes that may be useful for specific
1592      optimizations.  */
1593   for (i = 31; i >= 0; i--)
1594     {
1595       if ((remainder & (1 << i)) == 0)
1596         clear_sign_bit_copies++;
1597       else
1598         break;
1599     }
1600
1601   for (i = 31; i >= 0; i--)
1602     {
1603       if ((remainder & (1 << i)) != 0)
1604         set_sign_bit_copies++;
1605       else
1606         break;
1607     }
1608
1609   for (i = 0; i <= 31; i++)
1610     {
1611       if ((remainder & (1 << i)) == 0)
1612         clear_zero_bit_copies++;
1613       else
1614         break;
1615     }
1616
1617   for (i = 0; i <= 31; i++)
1618     {
1619       if ((remainder & (1 << i)) != 0)
1620         set_zero_bit_copies++;
1621       else
1622         break;
1623     }
1624
1625   switch (code)
1626     {
1627     case SET:
1628       /* See if we can do this by sign_extending a constant that is known
1629          to be negative.  This is a good, way of doing it, since the shift
1630          may well merge into a subsequent insn.  */
1631       if (set_sign_bit_copies > 1)
1632         {
1633           if (const_ok_for_arm
1634               (temp1 = ARM_SIGN_EXTEND (remainder 
1635                                         << (set_sign_bit_copies - 1))))
1636             {
1637               if (generate)
1638                 {
1639                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1640                   emit_constant_insn (cond,
1641                                       gen_rtx_SET (VOIDmode, new_src, 
1642                                                    GEN_INT (temp1)));
1643                   emit_constant_insn (cond,
1644                                       gen_ashrsi3 (target, new_src, 
1645                                                    GEN_INT (set_sign_bit_copies - 1)));
1646                 }
1647               return 2;
1648             }
1649           /* For an inverted constant, we will need to set the low bits,
1650              these will be shifted out of harm's way.  */
1651           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1652           if (const_ok_for_arm (~temp1))
1653             {
1654               if (generate)
1655                 {
1656                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1657                   emit_constant_insn (cond,
1658                                       gen_rtx_SET (VOIDmode, new_src,
1659                                                    GEN_INT (temp1)));
1660                   emit_constant_insn (cond,
1661                                       gen_ashrsi3 (target, new_src, 
1662                                                    GEN_INT (set_sign_bit_copies - 1)));
1663                 }
1664               return 2;
1665             }
1666         }
1667
1668       /* See if we can generate this by setting the bottom (or the top)
1669          16 bits, and then shifting these into the other half of the
1670          word.  We only look for the simplest cases, to do more would cost
1671          too much.  Be careful, however, not to generate this when the
1672          alternative would take fewer insns.  */
1673       if (val & 0xffff0000)
1674         {
1675           temp1 = remainder & 0xffff0000;
1676           temp2 = remainder & 0x0000ffff;
1677
1678           /* Overlaps outside this range are best done using other methods.  */
1679           for (i = 9; i < 24; i++)
1680             {
1681               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1682                   && !const_ok_for_arm (temp2))
1683                 {
1684                   rtx new_src = (subtargets
1685                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1686                                  : target);
1687                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1688                                             source, subtargets, generate);
1689                   source = new_src;
1690                   if (generate)
1691                     emit_constant_insn 
1692                       (cond,
1693                        gen_rtx_SET
1694                        (VOIDmode, target,
1695                         gen_rtx_IOR (mode,
1696                                      gen_rtx_ASHIFT (mode, source,
1697                                                      GEN_INT (i)),
1698                                      source)));
1699                   return insns + 1;
1700                 }
1701             }
1702
1703           /* Don't duplicate cases already considered.  */
1704           for (i = 17; i < 24; i++)
1705             {
1706               if (((temp1 | (temp1 >> i)) == remainder)
1707                   && !const_ok_for_arm (temp1))
1708                 {
1709                   rtx new_src = (subtargets
1710                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1711                                  : target);
1712                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1713                                             source, subtargets, generate);
1714                   source = new_src;
1715                   if (generate)
1716                     emit_constant_insn
1717                       (cond,
1718                        gen_rtx_SET (VOIDmode, target,
1719                                     gen_rtx_IOR
1720                                     (mode,
1721                                      gen_rtx_LSHIFTRT (mode, source,
1722                                                        GEN_INT (i)),
1723                                      source)));
1724                   return insns + 1;
1725                 }
1726             }
1727         }
1728       break;
1729
1730     case IOR:
1731     case XOR:
1732       /* If we have IOR or XOR, and the constant can be loaded in a
1733          single instruction, and we can find a temporary to put it in,
1734          then this can be done in two instructions instead of 3-4.  */
1735       if (subtargets
1736           /* TARGET can't be NULL if SUBTARGETS is 0 */
1737           || (reload_completed && !reg_mentioned_p (target, source)))
1738         {
1739           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1740             {
1741               if (generate)
1742                 {
1743                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1744
1745                   emit_constant_insn (cond,
1746                                       gen_rtx_SET (VOIDmode, sub, 
1747                                                    GEN_INT (val)));
1748                   emit_constant_insn (cond,
1749                                       gen_rtx_SET (VOIDmode, target, 
1750                                                    gen_rtx_fmt_ee (code, mode,
1751                                                                    source, sub)));
1752                 }
1753               return 2;
1754             }
1755         }
1756
1757       if (code == XOR)
1758         break;
1759
1760       if (set_sign_bit_copies > 8
1761           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1762         {
1763           if (generate)
1764             {
1765               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1766               rtx shift = GEN_INT (set_sign_bit_copies);
1767
1768               emit_constant_insn 
1769                 (cond,
1770                  gen_rtx_SET (VOIDmode, sub,
1771                               gen_rtx_NOT (mode, 
1772                                            gen_rtx_ASHIFT (mode,
1773                                                            source, 
1774                                                            shift))));
1775               emit_constant_insn 
1776                 (cond,
1777                  gen_rtx_SET (VOIDmode, target,
1778                               gen_rtx_NOT (mode,
1779                                            gen_rtx_LSHIFTRT (mode, sub,
1780                                                              shift))));
1781             }
1782           return 2;
1783         }
1784
1785       if (set_zero_bit_copies > 8
1786           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1787         {
1788           if (generate)
1789             {
1790               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1791               rtx shift = GEN_INT (set_zero_bit_copies);
1792
1793               emit_constant_insn
1794                 (cond,
1795                  gen_rtx_SET (VOIDmode, sub,
1796                               gen_rtx_NOT (mode,
1797                                            gen_rtx_LSHIFTRT (mode,
1798                                                              source,
1799                                                              shift))));
1800               emit_constant_insn 
1801                 (cond,
1802                  gen_rtx_SET (VOIDmode, target,
1803                               gen_rtx_NOT (mode,
1804                                            gen_rtx_ASHIFT (mode, sub,
1805                                                            shift))));
1806             }
1807           return 2;
1808         }
1809
1810       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1811         {
1812           if (generate)
1813             {
1814               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1815               emit_constant_insn (cond,
1816                                   gen_rtx_SET (VOIDmode, sub,
1817                                                gen_rtx_NOT (mode, source)));
1818               source = sub;
1819               if (subtargets)
1820                 sub = gen_reg_rtx (mode);
1821               emit_constant_insn (cond,
1822                                   gen_rtx_SET (VOIDmode, sub,
1823                                                gen_rtx_AND (mode, source, 
1824                                                             GEN_INT (temp1))));
1825               emit_constant_insn (cond,
1826                                   gen_rtx_SET (VOIDmode, target,
1827                                                gen_rtx_NOT (mode, sub)));
1828             }
1829           return 3;
1830         }
1831       break;
1832
1833     case AND:
1834       /* See if two shifts will do 2 or more insn's worth of work.  */
1835       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1836         {
1837           HOST_WIDE_INT shift_mask = ((0xffffffff
1838                                        << (32 - clear_sign_bit_copies))
1839                                       & 0xffffffff);
1840
1841           if ((remainder | shift_mask) != 0xffffffff)
1842             {
1843               if (generate)
1844                 {
1845                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1846                   insns = arm_gen_constant (AND, mode, cond, 
1847                                             remainder | shift_mask,
1848                                             new_src, source, subtargets, 1);
1849                   source = new_src;
1850                 }
1851               else
1852                 {
1853                   rtx targ = subtargets ? NULL_RTX : target;
1854                   insns = arm_gen_constant (AND, mode, cond,
1855                                             remainder | shift_mask,
1856                                             targ, source, subtargets, 0);
1857                 }
1858             }
1859
1860           if (generate)
1861             {
1862               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1863               rtx shift = GEN_INT (clear_sign_bit_copies);
1864
1865               emit_insn (gen_ashlsi3 (new_src, source, shift));
1866               emit_insn (gen_lshrsi3 (target, new_src, shift));
1867             }
1868
1869           return insns + 2;
1870         }
1871
1872       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1873         {
1874           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1875           
1876           if ((remainder | shift_mask) != 0xffffffff)
1877             {
1878               if (generate)
1879                 {
1880                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1881
1882                   insns = arm_gen_constant (AND, mode, cond,
1883                                             remainder | shift_mask,
1884                                             new_src, source, subtargets, 1);
1885                   source = new_src;
1886                 }
1887               else
1888                 {
1889                   rtx targ = subtargets ? NULL_RTX : target;
1890
1891                   insns = arm_gen_constant (AND, mode, cond,
1892                                             remainder | shift_mask,
1893                                             targ, source, subtargets, 0);
1894                 }
1895             }
1896
1897           if (generate)
1898             {
1899               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1900               rtx shift = GEN_INT (clear_zero_bit_copies);
1901
1902               emit_insn (gen_lshrsi3 (new_src, source, shift));
1903               emit_insn (gen_ashlsi3 (target, new_src, shift));
1904             }
1905
1906           return insns + 2;
1907         }
1908
1909       break;
1910
1911     default:
1912       break;
1913     }
1914
1915   for (i = 0; i < 32; i++)
1916     if (remainder & (1 << i))
1917       num_bits_set++;
1918
1919   if (code == AND || (can_invert && num_bits_set > 16))
1920     remainder = (~remainder) & 0xffffffff;
1921   else if (code == PLUS && num_bits_set > 16)
1922     remainder = (-remainder) & 0xffffffff;
1923   else
1924     {
1925       can_invert = 0;
1926       can_negate = 0;
1927     }
1928
1929   /* Now try and find a way of doing the job in either two or three
1930      instructions.
1931      We start by looking for the largest block of zeros that are aligned on
1932      a 2-bit boundary, we then fill up the temps, wrapping around to the
1933      top of the word when we drop off the bottom.
1934      In the worst case this code should produce no more than four insns.  */
1935   {
1936     int best_start = 0;
1937     int best_consecutive_zeros = 0;
1938
1939     for (i = 0; i < 32; i += 2)
1940       {
1941         int consecutive_zeros = 0;
1942
1943         if (!(remainder & (3 << i)))
1944           {
1945             while ((i < 32) && !(remainder & (3 << i)))
1946               {
1947                 consecutive_zeros += 2;
1948                 i += 2;
1949               }
1950             if (consecutive_zeros > best_consecutive_zeros)
1951               {
1952                 best_consecutive_zeros = consecutive_zeros;
1953                 best_start = i - consecutive_zeros;
1954               }
1955             i -= 2;
1956           }
1957       }
1958
1959     /* So long as it won't require any more insns to do so, it's
1960        desirable to emit a small constant (in bits 0...9) in the last
1961        insn.  This way there is more chance that it can be combined with
1962        a later addressing insn to form a pre-indexed load or store
1963        operation.  Consider:
1964
1965                *((volatile int *)0xe0000100) = 1;
1966                *((volatile int *)0xe0000110) = 2;
1967
1968        We want this to wind up as:
1969
1970                 mov rA, #0xe0000000
1971                 mov rB, #1
1972                 str rB, [rA, #0x100]
1973                 mov rB, #2
1974                 str rB, [rA, #0x110]
1975
1976        rather than having to synthesize both large constants from scratch.
1977
1978        Therefore, we calculate how many insns would be required to emit
1979        the constant starting from `best_start', and also starting from 
1980        zero (ie with bit 31 first to be output).  If `best_start' doesn't 
1981        yield a shorter sequence, we may as well use zero.  */
1982     if (best_start != 0
1983         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1984         && (count_insns_for_constant (remainder, 0) <= 
1985             count_insns_for_constant (remainder, best_start)))
1986       best_start = 0;
1987
1988     /* Now start emitting the insns.  */
1989     i = best_start;
1990     do
1991       {
1992         int end;
1993
1994         if (i <= 0)
1995           i += 32;
1996         if (remainder & (3 << (i - 2)))
1997           {
1998             end = i - 8;
1999             if (end < 0)
2000               end += 32;
2001             temp1 = remainder & ((0x0ff << end)
2002                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2003             remainder &= ~temp1;
2004
2005             if (generate)
2006               {
2007                 rtx new_src, temp1_rtx;
2008
2009                 if (code == SET || code == MINUS)
2010                   {
2011                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2012                     if (can_invert && code != MINUS)
2013                       temp1 = ~temp1;
2014                   }
2015                 else
2016                   {
2017                     if (remainder && subtargets)
2018                       new_src = gen_reg_rtx (mode);
2019                     else
2020                       new_src = target;
2021                     if (can_invert)
2022                       temp1 = ~temp1;
2023                     else if (can_negate)
2024                       temp1 = -temp1;
2025                   }
2026
2027                 temp1 = trunc_int_for_mode (temp1, mode);
2028                 temp1_rtx = GEN_INT (temp1);
2029
2030                 if (code == SET)
2031                   ;
2032                 else if (code == MINUS)
2033                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2034                 else
2035                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2036
2037                 emit_constant_insn (cond,
2038                                     gen_rtx_SET (VOIDmode, new_src, 
2039                                                  temp1_rtx));
2040                 source = new_src;
2041               }
2042
2043             if (code == SET)
2044               {
2045                 can_invert = 0;
2046                 code = PLUS;
2047               }
2048             else if (code == MINUS)
2049               code = PLUS;
2050
2051             insns++;
2052             i -= 6;
2053           }
2054         i -= 2;
2055       }
2056     while (remainder);
2057   }
2058
2059   return insns;
2060 }
2061
2062 /* Canonicalize a comparison so that we are more likely to recognize it.
2063    This can be done for a few constant compares, where we can make the
2064    immediate value easier to load.  */
2065
2066 enum rtx_code
2067 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2068 {
2069   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2070
2071   switch (code)
2072     {
2073     case EQ:
2074     case NE:
2075       return code;
2076
2077     case GT:
2078     case LE:
2079       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2080           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2081         {
2082           *op1 = GEN_INT (i + 1);
2083           return code == GT ? GE : LT;
2084         }
2085       break;
2086
2087     case GE:
2088     case LT:
2089       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2090           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2091         {
2092           *op1 = GEN_INT (i - 1);
2093           return code == GE ? GT : LE;
2094         }
2095       break;
2096
2097     case GTU:
2098     case LEU:
2099       if (i != ~((unsigned HOST_WIDE_INT) 0)
2100           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2101         {
2102           *op1 = GEN_INT (i + 1);
2103           return code == GTU ? GEU : LTU;
2104         }
2105       break;
2106
2107     case GEU:
2108     case LTU:
2109       if (i != 0
2110           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2111         {
2112           *op1 = GEN_INT (i - 1);
2113           return code == GEU ? GTU : LEU;
2114         }
2115       break;
2116
2117     default:
2118       abort ();
2119     }
2120
2121   return code;
2122 }
2123
2124
2125 /* Define how to find the value returned by a function.  */
2126
2127 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2128 {
2129   enum machine_mode mode;
2130   int unsignedp ATTRIBUTE_UNUSED;
2131   rtx r ATTRIBUTE_UNUSED;
2132
2133   
2134   mode = TYPE_MODE (type);
2135   /* Promote integer types.  */
2136   if (INTEGRAL_TYPE_P (type))
2137     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2138   return LIBCALL_VALUE(mode);
2139 }
2140
2141
2142 /* Decide whether a type should be returned in memory (true)
2143    or in a register (false).  This is called by the macro
2144    RETURN_IN_MEMORY.  */
2145 int
2146 arm_return_in_memory (tree type)
2147 {
2148   HOST_WIDE_INT size;
2149
2150   if (!AGGREGATE_TYPE_P (type))
2151     /* All simple types are returned in registers.  */
2152     return 0;
2153
2154   size = int_size_in_bytes (type);
2155
2156   if (arm_abi != ARM_ABI_APCS)
2157     {
2158       /* ATPCS and later return aggregate types in memory only if they are
2159          larger than a word (or are variable size).  */
2160       return (size < 0 || size > UNITS_PER_WORD);
2161     }
2162   
2163   /* For the arm-wince targets we choose to be compatible with Microsoft's
2164      ARM and Thumb compilers, which always return aggregates in memory.  */
2165 #ifndef ARM_WINCE
2166   /* All structures/unions bigger than one word are returned in memory.
2167      Also catch the case where int_size_in_bytes returns -1.  In this case
2168      the aggregate is either huge or of variable size, and in either case
2169      we will want to return it via memory and not in a register.  */
2170   if (size < 0 || size > UNITS_PER_WORD)
2171     return 1;
2172   
2173   if (TREE_CODE (type) == RECORD_TYPE)
2174     {
2175       tree field;
2176
2177       /* For a struct the APCS says that we only return in a register
2178          if the type is 'integer like' and every addressable element
2179          has an offset of zero.  For practical purposes this means
2180          that the structure can have at most one non bit-field element
2181          and that this element must be the first one in the structure.  */
2182       
2183       /* Find the first field, ignoring non FIELD_DECL things which will
2184          have been created by C++.  */
2185       for (field = TYPE_FIELDS (type);
2186            field && TREE_CODE (field) != FIELD_DECL;
2187            field = TREE_CHAIN (field))
2188         continue;
2189       
2190       if (field == NULL)
2191         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2192
2193       /* Check that the first field is valid for returning in a register.  */
2194
2195       /* ... Floats are not allowed */
2196       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2197         return 1;
2198
2199       /* ... Aggregates that are not themselves valid for returning in
2200          a register are not allowed.  */
2201       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2202         return 1;
2203
2204       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2205          since they are not addressable.  */
2206       for (field = TREE_CHAIN (field);
2207            field;
2208            field = TREE_CHAIN (field))
2209         {
2210           if (TREE_CODE (field) != FIELD_DECL)
2211             continue;
2212           
2213           if (!DECL_BIT_FIELD_TYPE (field))
2214             return 1;
2215         }
2216
2217       return 0;
2218     }
2219   
2220   if (TREE_CODE (type) == UNION_TYPE)
2221     {
2222       tree field;
2223
2224       /* Unions can be returned in registers if every element is
2225          integral, or can be returned in an integer register.  */
2226       for (field = TYPE_FIELDS (type);
2227            field;
2228            field = TREE_CHAIN (field))
2229         {
2230           if (TREE_CODE (field) != FIELD_DECL)
2231             continue;
2232
2233           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2234             return 1;
2235           
2236           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2237             return 1;
2238         }
2239       
2240       return 0;
2241     }
2242 #endif /* not ARM_WINCE */  
2243   
2244   /* Return all other types in memory.  */
2245   return 1;
2246 }
2247
2248 /* Indicate whether or not words of a double are in big-endian order.  */
2249
2250 int
2251 arm_float_words_big_endian (void)
2252 {
2253   if (TARGET_MAVERICK)
2254     return 0;
2255
2256   /* For FPA, float words are always big-endian.  For VFP, floats words
2257      follow the memory system mode.  */
2258
2259   if (TARGET_FPA)
2260     {
2261       return 1;
2262     }
2263
2264   if (TARGET_VFP)
2265     return (TARGET_BIG_END ? 1 : 0);
2266
2267   return 1;
2268 }
2269
2270 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2271    for a call to a function whose data type is FNTYPE.
2272    For a library call, FNTYPE is NULL.  */
2273 void
2274 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype, 
2275                           rtx libname  ATTRIBUTE_UNUSED,
2276                           tree fndecl ATTRIBUTE_UNUSED)
2277 {
2278   /* On the ARM, the offset starts at 0.  */
2279   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2280   pcum->iwmmxt_nregs = 0;
2281   pcum->can_split = true;
2282   
2283   pcum->call_cookie = CALL_NORMAL;
2284
2285   if (TARGET_LONG_CALLS)
2286     pcum->call_cookie = CALL_LONG;
2287     
2288   /* Check for long call/short call attributes.  The attributes
2289      override any command line option.  */
2290   if (fntype)
2291     {
2292       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2293         pcum->call_cookie = CALL_SHORT;
2294       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2295         pcum->call_cookie = CALL_LONG;
2296     }
2297
2298   /* Varargs vectors are treated the same as long long.
2299      named_count avoids having to change the way arm handles 'named' */
2300   pcum->named_count = 0;
2301   pcum->nargs = 0;
2302
2303   if (TARGET_REALLY_IWMMXT && fntype)
2304     {
2305       tree fn_arg;
2306
2307       for (fn_arg = TYPE_ARG_TYPES (fntype);
2308            fn_arg;
2309            fn_arg = TREE_CHAIN (fn_arg))
2310         pcum->named_count += 1;
2311
2312       if (! pcum->named_count)
2313         pcum->named_count = INT_MAX;
2314     }
2315 }
2316
2317
2318 /* Return true if mode/type need doubleword alignment.  */
2319 bool
2320 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2321 {
2322   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2323           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2324 }
2325
2326
2327 /* Determine where to put an argument to a function.
2328    Value is zero to push the argument on the stack,
2329    or a hard register in which to store the argument.
2330
2331    MODE is the argument's machine mode.
2332    TYPE is the data type of the argument (as a tree).
2333     This is null for libcalls where that information may
2334     not be available.
2335    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2336     the preceding args and about the function being called.
2337    NAMED is nonzero if this argument is a named parameter
2338     (otherwise it is an extra parameter matching an ellipsis).  */
2339
2340 rtx
2341 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2342                   tree type, int named)
2343 {
2344   int nregs;
2345
2346   /* Varargs vectors are treated the same as long long.
2347      named_count avoids having to change the way arm handles 'named' */
2348   if (TARGET_IWMMXT_ABI
2349       && VECTOR_MODE_SUPPORTED_P (mode)
2350       && pcum->named_count > pcum->nargs + 1)
2351     {
2352       if (pcum->iwmmxt_nregs <= 9)
2353         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2354       else
2355         {
2356           pcum->can_split = false;
2357           return NULL_RTX;
2358         }
2359     }
2360
2361   /* Put doubleword aligned quantities in even register pairs.  */
2362   if (pcum->nregs & 1
2363       && ARM_DOUBLEWORD_ALIGN
2364       && arm_needs_doubleword_align (mode, type))
2365     pcum->nregs++;
2366
2367   if (mode == VOIDmode)
2368     /* Compute operand 2 of the call insn.  */
2369     return GEN_INT (pcum->call_cookie);
2370
2371   /* Only allow splitting an arg between regs and memory if all preceding
2372      args were allocated to regs.  For args passed by reference we only count
2373      the reference pointer.  */
2374   if (pcum->can_split)
2375     nregs = 1;
2376   else
2377     nregs = ARM_NUM_REGS2 (mode, type);
2378
2379   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2380     return NULL_RTX;
2381   
2382   return gen_rtx_REG (mode, pcum->nregs);
2383 }
2384
2385 /* Variable sized types are passed by reference.  This is a GCC
2386    extension to the ARM ABI.  */
2387
2388 int
2389 arm_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2390                                     enum machine_mode mode ATTRIBUTE_UNUSED,
2391                                     tree type, int named ATTRIBUTE_UNUSED)
2392 {
2393   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2394 }
2395
2396 /* Implement va_arg.  */
2397
2398 rtx
2399 arm_va_arg (tree valist, tree type)
2400 {
2401   int align;
2402
2403   /* Variable sized types are passed by reference.  */
2404   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2405     {
2406       rtx addr = std_expand_builtin_va_arg (valist, build_pointer_type (type));
2407       return gen_rtx_MEM (ptr_mode, force_reg (Pmode, addr));
2408     }
2409
2410   align = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
2411   if (align > PARM_BOUNDARY)
2412     {
2413       tree mask;
2414       tree t;
2415
2416       /* Maintain 64-bit alignment of the valist pointer by
2417          constructing:   valist = ((valist + (8 - 1)) & -8).  */
2418       mask = build_int_2 (- (align / BITS_PER_UNIT), -1);
2419       t = build_int_2 ((align / BITS_PER_UNIT) - 1, 0);
2420       t = build (PLUS_EXPR,    TREE_TYPE (valist), valist, t);
2421       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, mask);
2422       t = build (MODIFY_EXPR,  TREE_TYPE (valist), valist, t);
2423       TREE_SIDE_EFFECTS (t) = 1;
2424       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2425
2426       /* This is to stop the combine pass optimizing
2427          away the alignment adjustment.  */
2428       mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
2429     }
2430
2431   return std_expand_builtin_va_arg (valist, type);
2432 }
2433 \f
2434 /* Encode the current state of the #pragma [no_]long_calls.  */
2435 typedef enum
2436 {
2437   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2438   LONG,         /* #pragma long_calls is in effect.  */
2439   SHORT         /* #pragma no_long_calls is in effect.  */
2440 } arm_pragma_enum;
2441
2442 static arm_pragma_enum arm_pragma_long_calls = OFF;
2443
2444 void
2445 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2446 {
2447   arm_pragma_long_calls = LONG;
2448 }
2449
2450 void
2451 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2452 {
2453   arm_pragma_long_calls = SHORT;
2454 }
2455
2456 void
2457 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2458 {
2459   arm_pragma_long_calls = OFF;
2460 }
2461 \f
2462 /* Table of machine attributes.  */
2463 const struct attribute_spec arm_attribute_table[] =
2464 {
2465   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2466   /* Function calls made to this symbol must be done indirectly, because
2467      it may lie outside of the 26 bit addressing range of a normal function
2468      call.  */
2469   { "long_call",    0, 0, false, true,  true,  NULL },
2470   /* Whereas these functions are always known to reside within the 26 bit
2471      addressing range.  */
2472   { "short_call",   0, 0, false, true,  true,  NULL },
2473   /* Interrupt Service Routines have special prologue and epilogue requirements.  */ 
2474   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2475   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2476   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2477 #ifdef ARM_PE
2478   /* ARM/PE has three new attributes:
2479      interfacearm - ?
2480      dllexport - for exporting a function/variable that will live in a dll
2481      dllimport - for importing a function/variable from a dll
2482
2483      Microsoft allows multiple declspecs in one __declspec, separating
2484      them with spaces.  We do NOT support this.  Instead, use __declspec
2485      multiple times.
2486   */
2487   { "dllimport",    0, 0, true,  false, false, NULL },
2488   { "dllexport",    0, 0, true,  false, false, NULL },
2489   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2490 #endif
2491   { NULL,           0, 0, false, false, false, NULL }
2492 };
2493
2494 /* Handle an attribute requiring a FUNCTION_DECL;
2495    arguments as in struct attribute_spec.handler.  */
2496 static tree
2497 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2498                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2499 {
2500   if (TREE_CODE (*node) != FUNCTION_DECL)
2501     {
2502       warning ("`%s' attribute only applies to functions",
2503                IDENTIFIER_POINTER (name));
2504       *no_add_attrs = true;
2505     }
2506
2507   return NULL_TREE;
2508 }
2509
2510 /* Handle an "interrupt" or "isr" attribute;
2511    arguments as in struct attribute_spec.handler.  */
2512 static tree
2513 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2514                           bool *no_add_attrs)
2515 {
2516   if (DECL_P (*node))
2517     {
2518       if (TREE_CODE (*node) != FUNCTION_DECL)
2519         {
2520           warning ("`%s' attribute only applies to functions",
2521                    IDENTIFIER_POINTER (name));
2522           *no_add_attrs = true;
2523         }
2524       /* FIXME: the argument if any is checked for type attributes;
2525          should it be checked for decl ones?  */
2526     }
2527   else
2528     {
2529       if (TREE_CODE (*node) == FUNCTION_TYPE
2530           || TREE_CODE (*node) == METHOD_TYPE)
2531         {
2532           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2533             {
2534               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2535               *no_add_attrs = true;
2536             }
2537         }
2538       else if (TREE_CODE (*node) == POINTER_TYPE
2539                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2540                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2541                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2542         {
2543           *node = build_type_copy (*node);
2544           TREE_TYPE (*node) = build_type_attribute_variant
2545             (TREE_TYPE (*node),
2546              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2547           *no_add_attrs = true;
2548         }
2549       else
2550         {
2551           /* Possibly pass this attribute on from the type to a decl.  */
2552           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2553                        | (int) ATTR_FLAG_FUNCTION_NEXT
2554                        | (int) ATTR_FLAG_ARRAY_NEXT))
2555             {
2556               *no_add_attrs = true;
2557               return tree_cons (name, args, NULL_TREE);
2558             }
2559           else
2560             {
2561               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2562             }
2563         }
2564     }
2565
2566   return NULL_TREE;
2567 }
2568
2569 /* Return 0 if the attributes for two types are incompatible, 1 if they
2570    are compatible, and 2 if they are nearly compatible (which causes a
2571    warning to be generated).  */
2572 static int
2573 arm_comp_type_attributes (tree type1, tree type2)
2574 {
2575   int l1, l2, s1, s2;
2576   
2577   /* Check for mismatch of non-default calling convention.  */
2578   if (TREE_CODE (type1) != FUNCTION_TYPE)
2579     return 1;
2580
2581   /* Check for mismatched call attributes.  */
2582   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2583   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2584   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2585   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2586
2587   /* Only bother to check if an attribute is defined.  */
2588   if (l1 | l2 | s1 | s2)
2589     {
2590       /* If one type has an attribute, the other must have the same attribute.  */
2591       if ((l1 != l2) || (s1 != s2))
2592         return 0;
2593
2594       /* Disallow mixed attributes.  */
2595       if ((l1 & s2) || (l2 & s1))
2596         return 0;
2597     }
2598   
2599   /* Check for mismatched ISR attribute.  */
2600   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2601   if (! l1)
2602     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2603   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2604   if (! l2)
2605     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2606   if (l1 != l2)
2607     return 0;
2608
2609   return 1;
2610 }
2611
2612 /*  Encode long_call or short_call attribute by prefixing
2613     symbol name in DECL with a special character FLAG.  */
2614 void
2615 arm_encode_call_attribute (tree decl, int flag)
2616 {
2617   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2618   int          len = strlen (str);
2619   char *       newstr;
2620
2621   /* Do not allow weak functions to be treated as short call.  */
2622   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2623     return;
2624
2625   newstr = alloca (len + 2);
2626   newstr[0] = flag;
2627   strcpy (newstr + 1, str);
2628
2629   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2630   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2631 }
2632
2633 /*  Assigns default attributes to newly defined type.  This is used to
2634     set short_call/long_call attributes for function types of
2635     functions defined inside corresponding #pragma scopes.  */
2636 static void
2637 arm_set_default_type_attributes (tree type)
2638 {
2639   /* Add __attribute__ ((long_call)) to all functions, when
2640      inside #pragma long_calls or __attribute__ ((short_call)),
2641      when inside #pragma no_long_calls.  */
2642   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2643     {
2644       tree type_attr_list, attr_name;
2645       type_attr_list = TYPE_ATTRIBUTES (type);
2646
2647       if (arm_pragma_long_calls == LONG)
2648         attr_name = get_identifier ("long_call");
2649       else if (arm_pragma_long_calls == SHORT)
2650         attr_name = get_identifier ("short_call");
2651       else
2652         return;
2653
2654       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2655       TYPE_ATTRIBUTES (type) = type_attr_list;
2656     }
2657 }
2658 \f
2659 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2660    defined within the current compilation unit.  If this cannot be
2661    determined, then 0 is returned.  */
2662 static int
2663 current_file_function_operand (rtx sym_ref)
2664 {
2665   /* This is a bit of a fib.  A function will have a short call flag
2666      applied to its name if it has the short call attribute, or it has
2667      already been defined within the current compilation unit.  */
2668   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2669     return 1;
2670
2671   /* The current function is always defined within the current compilation
2672      unit.  if it s a weak definition however, then this may not be the real
2673      definition of the function, and so we have to say no.  */
2674   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2675       && !DECL_WEAK (current_function_decl))
2676     return 1;
2677
2678   /* We cannot make the determination - default to returning 0.  */
2679   return 0;
2680 }
2681
2682 /* Return nonzero if a 32 bit "long_call" should be generated for
2683    this call.  We generate a long_call if the function:
2684
2685         a.  has an __attribute__((long call))
2686      or b.  is within the scope of a #pragma long_calls
2687      or c.  the -mlong-calls command line switch has been specified
2688
2689    However we do not generate a long call if the function:
2690    
2691         d.  has an __attribute__ ((short_call))
2692      or e.  is inside the scope of a #pragma no_long_calls
2693      or f.  has an __attribute__ ((section))
2694      or g.  is defined within the current compilation unit.
2695    
2696    This function will be called by C fragments contained in the machine
2697    description file.  CALL_REF and CALL_COOKIE correspond to the matched
2698    rtl operands.  CALL_SYMBOL is used to distinguish between
2699    two different callers of the function.  It is set to 1 in the
2700    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2701    and "call_value" patterns.  This is because of the difference in the
2702    SYM_REFs passed by these patterns.  */
2703 int
2704 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2705 {
2706   if (!call_symbol)
2707     {
2708       if (GET_CODE (sym_ref) != MEM)
2709         return 0;
2710
2711       sym_ref = XEXP (sym_ref, 0);
2712     }
2713
2714   if (GET_CODE (sym_ref) != SYMBOL_REF)
2715     return 0;
2716
2717   if (call_cookie & CALL_SHORT)
2718     return 0;
2719
2720   if (TARGET_LONG_CALLS && flag_function_sections)
2721     return 1;
2722   
2723   if (current_file_function_operand (sym_ref))
2724     return 0;
2725   
2726   return (call_cookie & CALL_LONG)
2727     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2728     || TARGET_LONG_CALLS;
2729 }
2730
2731 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2732 static bool
2733 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2734 {
2735   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2736
2737   if (cfun->machine->sibcall_blocked)
2738     return false;
2739
2740   /* Never tailcall something for which we have no decl, or if we
2741      are in Thumb mode.  */
2742   if (decl == NULL || TARGET_THUMB)
2743     return false;
2744
2745   /* Get the calling method.  */
2746   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2747     call_type = CALL_SHORT;
2748   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2749     call_type = CALL_LONG;
2750
2751   /* Cannot tail-call to long calls, since these are out of range of
2752      a branch instruction.  However, if not compiling PIC, we know
2753      we can reach the symbol if it is in this compilation unit.  */
2754   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2755     return false;
2756
2757   /* If we are interworking and the function is not declared static
2758      then we can't tail-call it unless we know that it exists in this 
2759      compilation unit (since it might be a Thumb routine).  */
2760   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2761     return false;
2762
2763   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2764   if (IS_INTERRUPT (arm_current_func_type ()))
2765     return false;
2766
2767   /* Everything else is ok.  */
2768   return true;
2769 }
2770
2771 \f
2772 /* Addressing mode support functions.  */
2773
2774 /* Return nonzero if X is a legitimate immediate operand when compiling
2775    for PIC.  */
2776 int
2777 legitimate_pic_operand_p (rtx x)
2778 {
2779   if (CONSTANT_P (x)
2780       && flag_pic
2781       && (GET_CODE (x) == SYMBOL_REF
2782           || (GET_CODE (x) == CONST
2783               && GET_CODE (XEXP (x, 0)) == PLUS
2784               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2785     return 0;
2786
2787   return 1;
2788 }
2789
2790 rtx
2791 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2792 {
2793   if (GET_CODE (orig) == SYMBOL_REF
2794       || GET_CODE (orig) == LABEL_REF)
2795     {
2796 #ifndef AOF_ASSEMBLER
2797       rtx pic_ref, address;
2798 #endif
2799       rtx insn;
2800       int subregs = 0;
2801
2802       if (reg == 0)
2803         {
2804           if (no_new_pseudos)
2805             abort ();
2806           else
2807             reg = gen_reg_rtx (Pmode);
2808
2809           subregs = 1;
2810         }
2811
2812 #ifdef AOF_ASSEMBLER
2813       /* The AOF assembler can generate relocations for these directly, and
2814          understands that the PIC register has to be added into the offset.  */
2815       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2816 #else
2817       if (subregs)
2818         address = gen_reg_rtx (Pmode);
2819       else
2820         address = reg;
2821
2822       if (TARGET_ARM)
2823         emit_insn (gen_pic_load_addr_arm (address, orig));
2824       else
2825         emit_insn (gen_pic_load_addr_thumb (address, orig));
2826
2827       if ((GET_CODE (orig) == LABEL_REF
2828            || (GET_CODE (orig) == SYMBOL_REF && 
2829                SYMBOL_REF_LOCAL_P (orig)))
2830           && NEED_GOT_RELOC)
2831         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2832       else
2833         {
2834           pic_ref = gen_rtx_MEM (Pmode,
2835                                  gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2836                                                address));
2837           RTX_UNCHANGING_P (pic_ref) = 1;
2838         }
2839
2840       insn = emit_move_insn (reg, pic_ref);
2841 #endif
2842       current_function_uses_pic_offset_table = 1;
2843       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2844          by loop.  */
2845       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2846                                             REG_NOTES (insn));
2847       return reg;
2848     }
2849   else if (GET_CODE (orig) == CONST)
2850     {
2851       rtx base, offset;
2852
2853       if (GET_CODE (XEXP (orig, 0)) == PLUS
2854           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2855         return orig;
2856
2857       if (reg == 0)
2858         {
2859           if (no_new_pseudos)
2860             abort ();
2861           else
2862             reg = gen_reg_rtx (Pmode);
2863         }
2864
2865       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2866         {
2867           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2868           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2869                                            base == reg ? 0 : reg);
2870         }
2871       else
2872         abort ();
2873
2874       if (GET_CODE (offset) == CONST_INT)
2875         {
2876           /* The base register doesn't really matter, we only want to
2877              test the index for the appropriate mode.  */
2878           if (!arm_legitimate_index_p (mode, offset, SET, 0))
2879             {
2880               if (!no_new_pseudos)
2881                 offset = force_reg (Pmode, offset);
2882               else
2883                 abort ();
2884             }
2885
2886           if (GET_CODE (offset) == CONST_INT)
2887             return plus_constant (base, INTVAL (offset));
2888         }
2889
2890       if (GET_MODE_SIZE (mode) > 4
2891           && (GET_MODE_CLASS (mode) == MODE_INT
2892               || TARGET_SOFT_FLOAT))
2893         {
2894           emit_insn (gen_addsi3 (reg, base, offset));
2895           return reg;
2896         }
2897
2898       return gen_rtx_PLUS (Pmode, base, offset);
2899     }
2900
2901   return orig;
2902 }
2903
2904 /* Generate code to load the PIC register.  PROLOGUE is true if
2905    called from arm_expand_prologue (in which case we want the 
2906    generated insns at the start of the function);  false if called
2907    by an exception receiver that needs the PIC register reloaded
2908    (in which case the insns are just dumped at the current location).  */
2909 void
2910 arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
2911 {
2912 #ifndef AOF_ASSEMBLER
2913   rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2914   rtx global_offset_table;
2915
2916   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2917     return;
2918
2919   if (!flag_pic)
2920     abort ();
2921
2922   start_sequence ();
2923   l1 = gen_label_rtx ();
2924
2925   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2926   /* On the ARM the PC register contains 'dot + 8' at the time of the
2927      addition, on the Thumb it is 'dot + 4'.  */
2928   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2929   if (GOT_PCREL)
2930     pic_tmp2 = gen_rtx_CONST (VOIDmode,
2931                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2932   else
2933     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2934
2935   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2936   
2937   if (TARGET_ARM)
2938     {
2939       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2940       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2941     }
2942   else
2943     {
2944       emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2945       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2946     }
2947
2948   seq = get_insns ();
2949   end_sequence ();
2950   if (prologue)
2951     emit_insn_after (seq, get_insns ());
2952   else
2953     emit_insn (seq);
2954
2955   /* Need to emit this whether or not we obey regdecls,
2956      since setjmp/longjmp can cause life info to screw up.  */
2957   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2958 #endif /* AOF_ASSEMBLER */
2959 }
2960
2961 /* Return nonzero if X is valid as an ARM state addressing register.  */
2962 static int
2963 arm_address_register_rtx_p (rtx x, int strict_p)
2964 {
2965   int regno;
2966
2967   if (GET_CODE (x) != REG)
2968     return 0;
2969
2970   regno = REGNO (x);
2971
2972   if (strict_p)
2973     return ARM_REGNO_OK_FOR_BASE_P (regno);
2974
2975   return (regno <= LAST_ARM_REGNUM
2976           || regno >= FIRST_PSEUDO_REGISTER
2977           || regno == FRAME_POINTER_REGNUM
2978           || regno == ARG_POINTER_REGNUM);
2979 }
2980
2981 /* Return nonzero if X is a valid ARM state address operand.  */
2982 int
2983 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2984                           int strict_p)
2985 {
2986   if (arm_address_register_rtx_p (x, strict_p))
2987     return 1;
2988
2989   else if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
2990     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2991
2992   else if ((GET_CODE (x) == POST_MODIFY || GET_CODE (x) == PRE_MODIFY)
2993            && GET_MODE_SIZE (mode) <= 4
2994            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2995            && GET_CODE (XEXP (x, 1)) == PLUS
2996            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
2997     return arm_legitimate_index_p (mode, XEXP (XEXP (x, 1), 1), outer,
2998                                    strict_p);
2999
3000   /* After reload constants split into minipools will have addresses
3001      from a LABEL_REF.  */
3002   else if (reload_completed
3003            && (GET_CODE (x) == LABEL_REF
3004                || (GET_CODE (x) == CONST
3005                    && GET_CODE (XEXP (x, 0)) == PLUS
3006                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3007                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3008     return 1;
3009
3010   else if (mode == TImode)
3011     return 0;
3012
3013   else if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
3014     {
3015       if (GET_CODE (x) == PLUS
3016           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3017           && GET_CODE (XEXP (x, 1)) == CONST_INT)
3018         {
3019           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3020
3021           if (val == 4 || val == -4 || val == -8)
3022             return 1;
3023         }
3024     }
3025
3026   else if (TARGET_HARD_FLOAT && TARGET_VFP && mode == DFmode)
3027     {
3028       if (GET_CODE (x) == PLUS
3029           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3030           && GET_CODE (XEXP (x, 1)) == CONST_INT)
3031         {
3032           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3033
3034           /* ??? valid arm offsets are a subset of VFP offsets.
3035              For now only allow this subset.  Proper fix is to add an
3036              additional memory constraint for arm address modes.
3037              Alternatively allow full vfp addressing and let
3038              output_move_double fix it up with a sub-optimal sequence.  */
3039           if (val == 4 || val == -4 || val == -8)
3040             return 1;
3041         }
3042     }
3043
3044   else if (GET_CODE (x) == PLUS)
3045     {
3046       rtx xop0 = XEXP (x, 0);
3047       rtx xop1 = XEXP (x, 1);
3048
3049       return ((arm_address_register_rtx_p (xop0, strict_p)
3050                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3051               || (arm_address_register_rtx_p (xop1, strict_p)
3052                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3053     }
3054
3055 #if 0
3056   /* Reload currently can't handle MINUS, so disable this for now */
3057   else if (GET_CODE (x) == MINUS)
3058     {
3059       rtx xop0 = XEXP (x, 0);
3060       rtx xop1 = XEXP (x, 1);
3061
3062       return (arm_address_register_rtx_p (xop0, strict_p)
3063               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3064     }
3065 #endif
3066
3067   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3068            && GET_CODE (x) == SYMBOL_REF
3069            && CONSTANT_POOL_ADDRESS_P (x)
3070            && ! (flag_pic
3071                  && symbol_mentioned_p (get_pool_constant (x))))
3072     return 1;
3073
3074   else if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_DEC)
3075            && (GET_MODE_SIZE (mode) <= 4)
3076            && arm_address_register_rtx_p (XEXP (x, 0), strict_p))
3077     return 1;
3078
3079   return 0;
3080 }
3081
3082 /* Return nonzero if INDEX is valid for an address index operand in
3083    ARM state.  */
3084 static int
3085 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3086                         int strict_p)
3087 {
3088   HOST_WIDE_INT range;
3089   enum rtx_code code = GET_CODE (index);
3090
3091   /* Standard coprocessor addressing modes.  */
3092   if (TARGET_HARD_FLOAT
3093       && (TARGET_FPA || TARGET_MAVERICK)
3094       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3095           || (TARGET_MAVERICK && mode == DImode)))
3096     return (code == CONST_INT && INTVAL (index) < 1024
3097             && INTVAL (index) > -1024
3098             && (INTVAL (index) & 3) == 0);
3099
3100   if (arm_address_register_rtx_p (index, strict_p)
3101       && GET_MODE_SIZE (mode) <= 4)
3102     return 1;
3103
3104   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3105     return (code == CONST_INT
3106             && INTVAL (index) < 1024
3107             && INTVAL (index) > -1024
3108             && (INTVAL (index) & 3) == 0);
3109
3110   if (GET_MODE_SIZE (mode) <= 4
3111       && ! (arm_arch4
3112             && (mode == HImode
3113                 || (mode == QImode && outer == SIGN_EXTEND))))
3114     {
3115       if (code == MULT)
3116         {
3117           rtx xiop0 = XEXP (index, 0);
3118           rtx xiop1 = XEXP (index, 1);
3119
3120           return ((arm_address_register_rtx_p (xiop0, strict_p)
3121                    && power_of_two_operand (xiop1, SImode))
3122                   || (arm_address_register_rtx_p (xiop1, strict_p)
3123                       && power_of_two_operand (xiop0, SImode)));
3124         }
3125       else if (code == LSHIFTRT || code == ASHIFTRT
3126                || code == ASHIFT || code == ROTATERT)
3127         {
3128           rtx op = XEXP (index, 1);
3129
3130           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3131                   && GET_CODE (op) == CONST_INT
3132                   && INTVAL (op) > 0
3133                   && INTVAL (op) <= 31);
3134         }
3135     }
3136
3137   /* For ARM v4 we may be doing a sign-extend operation during the
3138      load.  */
3139   if (arm_arch4)
3140     {
3141       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3142         range = 256;
3143       else
3144         range = 4096;
3145     }
3146   else
3147     range = (mode == HImode) ? 4095 : 4096;
3148
3149   return (code == CONST_INT
3150           && INTVAL (index) < range
3151           && INTVAL (index) > -range);
3152 }
3153
3154 /* Return nonzero if X is valid as a Thumb state base register.  */
3155 static int
3156 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3157 {
3158   int regno;
3159
3160   if (GET_CODE (x) != REG)
3161     return 0;
3162
3163   regno = REGNO (x);
3164
3165   if (strict_p)
3166     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3167
3168   return (regno <= LAST_LO_REGNUM
3169           || regno > LAST_VIRTUAL_REGISTER
3170           || regno == FRAME_POINTER_REGNUM
3171           || (GET_MODE_SIZE (mode) >= 4
3172               && (regno == STACK_POINTER_REGNUM
3173                   || regno >= FIRST_PSEUDO_REGISTER
3174                   || x == hard_frame_pointer_rtx
3175                   || x == arg_pointer_rtx)));
3176 }
3177
3178 /* Return nonzero if x is a legitimate index register.  This is the case
3179    for any base register that can access a QImode object.  */
3180 inline static int
3181 thumb_index_register_rtx_p (rtx x, int strict_p)
3182 {
3183   return thumb_base_register_rtx_p (x, QImode, strict_p);
3184 }
3185
3186 /* Return nonzero if x is a legitimate Thumb-state address.
3187  
3188    The AP may be eliminated to either the SP or the FP, so we use the
3189    least common denominator, e.g. SImode, and offsets from 0 to 64.
3190
3191    ??? Verify whether the above is the right approach.
3192
3193    ??? Also, the FP may be eliminated to the SP, so perhaps that
3194    needs special handling also.
3195
3196    ??? Look at how the mips16 port solves this problem.  It probably uses
3197    better ways to solve some of these problems.
3198
3199    Although it is not incorrect, we don't accept QImode and HImode
3200    addresses based on the frame pointer or arg pointer until the
3201    reload pass starts.  This is so that eliminating such addresses
3202    into stack based ones won't produce impossible code.  */
3203 int
3204 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3205 {
3206   /* ??? Not clear if this is right.  Experiment.  */
3207   if (GET_MODE_SIZE (mode) < 4
3208       && !(reload_in_progress || reload_completed)
3209       && (reg_mentioned_p (frame_pointer_rtx, x)
3210           || reg_mentioned_p (arg_pointer_rtx, x)
3211           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3212           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3213           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3214           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3215     return 0;
3216
3217   /* Accept any base register.  SP only in SImode or larger.  */
3218   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3219     return 1;
3220
3221   /* This is PC relative data before arm_reorg runs.  */
3222   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3223            && GET_CODE (x) == SYMBOL_REF
3224            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3225     return 1;
3226
3227   /* This is PC relative data after arm_reorg runs.  */
3228   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3229            && (GET_CODE (x) == LABEL_REF
3230                || (GET_CODE (x) == CONST
3231                    && GET_CODE (XEXP (x, 0)) == PLUS
3232                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3233                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3234     return 1;
3235
3236   /* Post-inc indexing only supported for SImode and larger.  */
3237   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3238            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3239     return 1;
3240
3241   else if (GET_CODE (x) == PLUS)
3242     {
3243       /* REG+REG address can be any two index registers.  */
3244       /* We disallow FRAME+REG addressing since we know that FRAME
3245          will be replaced with STACK, and SP relative addressing only
3246          permits SP+OFFSET.  */
3247       if (GET_MODE_SIZE (mode) <= 4
3248           && XEXP (x, 0) != frame_pointer_rtx
3249           && XEXP (x, 1) != frame_pointer_rtx
3250           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3251           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3252         return 1;
3253
3254       /* REG+const has 5-7 bit offset for non-SP registers.  */
3255       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3256                 || XEXP (x, 0) == arg_pointer_rtx)
3257                && GET_CODE (XEXP (x, 1)) == CONST_INT
3258                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3259         return 1;
3260
3261       /* REG+const has 10 bit offset for SP, but only SImode and
3262          larger is supported.  */
3263       /* ??? Should probably check for DI/DFmode overflow here
3264          just like GO_IF_LEGITIMATE_OFFSET does.  */
3265       else if (GET_CODE (XEXP (x, 0)) == REG
3266                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3267                && GET_MODE_SIZE (mode) >= 4
3268                && GET_CODE (XEXP (x, 1)) == CONST_INT
3269                && INTVAL (XEXP (x, 1)) >= 0
3270                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3271                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3272         return 1;
3273
3274       else if (GET_CODE (XEXP (x, 0)) == REG
3275                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3276                && GET_MODE_SIZE (mode) >= 4
3277                && GET_CODE (XEXP (x, 1)) == CONST_INT
3278                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3279         return 1;
3280     }
3281
3282   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3283            && GET_MODE_SIZE (mode) == 4
3284            && GET_CODE (x) == SYMBOL_REF
3285            && CONSTANT_POOL_ADDRESS_P (x)
3286            && !(flag_pic
3287                 && symbol_mentioned_p (get_pool_constant (x))))
3288     return 1;
3289
3290   return 0;
3291 }
3292
3293 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3294    instruction of mode MODE.  */
3295 int
3296 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3297 {
3298   switch (GET_MODE_SIZE (mode))
3299     {
3300     case 1:
3301       return val >= 0 && val < 32;
3302
3303     case 2:
3304       return val >= 0 && val < 64 && (val & 1) == 0;
3305
3306     default:
3307       return (val >= 0
3308               && (val + GET_MODE_SIZE (mode)) <= 128
3309               && (val & 3) == 0);
3310     }
3311 }
3312
3313 /* Try machine-dependent ways of modifying an illegitimate address
3314    to be legitimate.  If we find one, return the new, valid address.  */
3315 rtx
3316 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3317 {
3318   if (GET_CODE (x) == PLUS)
3319     {
3320       rtx xop0 = XEXP (x, 0);
3321       rtx xop1 = XEXP (x, 1);
3322
3323       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3324         xop0 = force_reg (SImode, xop0);
3325
3326       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3327         xop1 = force_reg (SImode, xop1);
3328
3329       if (ARM_BASE_REGISTER_RTX_P (xop0)
3330           && GET_CODE (xop1) == CONST_INT)
3331         {
3332           HOST_WIDE_INT n, low_n;
3333           rtx base_reg, val;
3334           n = INTVAL (xop1);
3335
3336           /* VFP addressing modes actually allow greater offsets, but for
3337              now we just stick with the lowest common denominator.  */
3338           if (mode == DImode
3339               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3340             {
3341               low_n = n & 0x0f;
3342               n &= ~0x0f;
3343               if (low_n > 4)
3344                 {
3345                   n += 16;
3346                   low_n -= 16;
3347                 }
3348             }
3349           else
3350             {
3351               low_n = ((mode) == TImode ? 0
3352                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3353               n -= low_n;
3354             }
3355
3356           base_reg = gen_reg_rtx (SImode);
3357           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3358                                              GEN_INT (n)), NULL_RTX);
3359           emit_move_insn (base_reg, val);
3360           x = (low_n == 0 ? base_reg
3361                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3362         }
3363       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3364         x = gen_rtx_PLUS (SImode, xop0, xop1);
3365     }
3366
3367   /* XXX We don't allow MINUS any more -- see comment in
3368      arm_legitimate_address_p ().  */
3369   else if (GET_CODE (x) == MINUS)
3370     {
3371       rtx xop0 = XEXP (x, 0);
3372       rtx xop1 = XEXP (x, 1);
3373
3374       if (CONSTANT_P (xop0))
3375         xop0 = force_reg (SImode, xop0);
3376
3377       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3378         xop1 = force_reg (SImode, xop1);
3379
3380       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3381         x = gen_rtx_MINUS (SImode, xop0, xop1);
3382     }
3383
3384   if (flag_pic)
3385     {
3386       /* We need to find and carefully transform any SYMBOL and LABEL
3387          references; so go back to the original address expression.  */
3388       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3389
3390       if (new_x != orig_x)
3391         x = new_x;
3392     }
3393
3394   return x;
3395 }
3396
3397
3398 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3399    to be legitimate.  If we find one, return the new, valid address.  */
3400 rtx
3401 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3402 {
3403   if (GET_CODE (x) == PLUS
3404       && GET_CODE (XEXP (x, 1)) == CONST_INT
3405       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3406           || INTVAL (XEXP (x, 1)) < 0))
3407     {
3408       rtx xop0 = XEXP (x, 0);
3409       rtx xop1 = XEXP (x, 1);
3410       HOST_WIDE_INT offset = INTVAL (xop1);
3411
3412       /* Try and fold the offset into a biasing of the base register and
3413          then offsetting that.  Don't do this when optimizing for space
3414          since it can cause too many CSEs.  */
3415       if (optimize_size && offset >= 0
3416           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3417         {
3418           HOST_WIDE_INT delta;
3419
3420           if (offset >= 256)
3421             delta = offset - (256 - GET_MODE_SIZE (mode));
3422           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3423             delta = 31 * GET_MODE_SIZE (mode);
3424           else
3425             delta = offset & (~31 * GET_MODE_SIZE (mode));
3426
3427           xop0 = force_operand (plus_constant (xop0, offset - delta),
3428                                 NULL_RTX);
3429           x = plus_constant (xop0, delta);
3430         }
3431       else if (offset < 0 && offset > -256)
3432         /* Small negative offsets are best done with a subtract before the
3433            dereference, forcing these into a register normally takes two
3434            instructions.  */
3435         x = force_operand (x, NULL_RTX);
3436       else
3437         {
3438           /* For the remaining cases, force the constant into a register.  */
3439           xop1 = force_reg (SImode, xop1);
3440           x = gen_rtx_PLUS (SImode, xop0, xop1);
3441         }
3442     }
3443   else if (GET_CODE (x) == PLUS
3444            && s_register_operand (XEXP (x, 1), SImode)
3445            && !s_register_operand (XEXP (x, 0), SImode))
3446     {
3447       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3448
3449       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3450     }
3451
3452   if (flag_pic)
3453     {
3454       /* We need to find and carefully transform any SYMBOL and LABEL
3455          references; so go back to the original address expression.  */
3456       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3457
3458       if (new_x != orig_x)
3459         x = new_x;
3460     }
3461
3462   return x;
3463 }
3464
3465 \f
3466
3467 #define REG_OR_SUBREG_REG(X)                                            \
3468   (GET_CODE (X) == REG                                                  \
3469    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3470
3471 #define REG_OR_SUBREG_RTX(X)                    \
3472    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3473
3474 #ifndef COSTS_N_INSNS
3475 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3476 #endif
3477 static inline int
3478 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3479 {
3480   enum machine_mode mode = GET_MODE (x);
3481
3482   switch (code)
3483     {
3484     case ASHIFT:
3485     case ASHIFTRT:
3486     case LSHIFTRT:
3487     case ROTATERT:      
3488     case PLUS:
3489     case MINUS:
3490     case COMPARE:
3491     case NEG:
3492     case NOT:   
3493       return COSTS_N_INSNS (1);
3494       
3495     case MULT:                                                  
3496       if (GET_CODE (XEXP (x, 1)) == CONST_INT)                  
3497         {                                                               
3498           int cycles = 0;                                               
3499           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3500           
3501           while (i)                                             
3502             {                                                   
3503               i >>= 2;                                          
3504               cycles++;                                         
3505             }                                                   
3506           return COSTS_N_INSNS (2) + cycles;                    
3507         }
3508       return COSTS_N_INSNS (1) + 16;
3509       
3510     case SET:                                                   
3511       return (COSTS_N_INSNS (1)                                 
3512               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)            
3513                      + GET_CODE (SET_DEST (x)) == MEM));
3514       
3515     case CONST_INT:                                             
3516       if (outer == SET)                                         
3517         {                                                       
3518           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)                
3519             return 0;                                           
3520           if (thumb_shiftable_const (INTVAL (x)))                       
3521             return COSTS_N_INSNS (2);                           
3522           return COSTS_N_INSNS (3);                             
3523         }                                                               
3524       else if ((outer == PLUS || outer == COMPARE)
3525                && INTVAL (x) < 256 && INTVAL (x) > -256)                
3526         return 0;
3527       else if (outer == AND
3528                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3529         return COSTS_N_INSNS (1);
3530       else if (outer == ASHIFT || outer == ASHIFTRT             
3531                || outer == LSHIFTRT)                            
3532         return 0;                                                       
3533       return COSTS_N_INSNS (2);
3534       
3535     case CONST:                                                 
3536     case CONST_DOUBLE:                                          
3537     case LABEL_REF:                                             
3538     case SYMBOL_REF:                                            
3539       return COSTS_N_INSNS (3);
3540       
3541     case UDIV:
3542     case UMOD:
3543     case DIV:
3544     case MOD:
3545       return 100;
3546
3547     case TRUNCATE:
3548       return 99;
3549
3550     case AND:
3551     case XOR:
3552     case IOR: 
3553       /* XXX guess.  */
3554       return 8;
3555
3556     case ADDRESSOF:
3557     case MEM:
3558       /* XXX another guess.  */
3559       /* Memory costs quite a lot for the first word, but subsequent words
3560          load at the equivalent of a single insn each.  */
3561       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3562               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3563                  ? 4 : 0));
3564
3565     case IF_THEN_ELSE:
3566       /* XXX a guess.  */
3567       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3568         return 14;
3569       return 2;
3570
3571     case ZERO_EXTEND:
3572       /* XXX still guessing.  */
3573       switch (GET_MODE (XEXP (x, 0)))
3574         {
3575         case QImode:
3576           return (1 + (mode == DImode ? 4 : 0)
3577                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3578           
3579         case HImode:
3580           return (4 + (mode == DImode ? 4 : 0)
3581                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3582           
3583         case SImode:
3584           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3585       
3586         default:
3587           return 99;
3588         }
3589       
3590     default:
3591       return 99;
3592     }
3593 }
3594
3595
3596 /* Worker routine for arm_rtx_costs.  */
3597 static inline int
3598 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3599 {
3600   enum machine_mode mode = GET_MODE (x);
3601   enum rtx_code subcode;
3602   int extra_cost;
3603
3604   switch (code)
3605     {
3606     case MEM:
3607       /* Memory costs quite a lot for the first word, but subsequent words
3608          load at the equivalent of a single insn each.  */
3609       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3610               + (GET_CODE (x) == SYMBOL_REF
3611                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3612
3613     case DIV:
3614     case MOD:
3615     case UDIV:
3616     case UMOD:
3617       return optimize_size ? COSTS_N_INSNS (2) : 100;
3618
3619     case ROTATE:
3620       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3621         return 4;
3622       /* Fall through */
3623     case ROTATERT:
3624       if (mode != SImode)
3625         return 8;
3626       /* Fall through */
3627     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3628       if (mode == DImode)
3629         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3630                 + ((GET_CODE (XEXP (x, 0)) == REG 
3631                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3632                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3633                    ? 0 : 8));
3634       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3635                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3636                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3637                    ? 0 : 4)
3638               + ((GET_CODE (XEXP (x, 1)) == REG
3639                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3640                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3641                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3642                  ? 0 : 4));
3643
3644     case MINUS:
3645       if (mode == DImode)
3646         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3647                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3648                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3649                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3650                    ? 0 : 8));
3651
3652       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3653         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3654                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3655                           && arm_const_double_rtx (XEXP (x, 1))))
3656                      ? 0 : 8)
3657                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3658                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3659                         && arm_const_double_rtx (XEXP (x, 0))))
3660                    ? 0 : 8));
3661
3662       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3663             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3664             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3665           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3666                || subcode == ASHIFTRT || subcode == LSHIFTRT
3667                || subcode == ROTATE || subcode == ROTATERT
3668                || (subcode == MULT
3669                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3670                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3671                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3672               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3673               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3674                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3675               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3676         return 1;
3677       /* Fall through */
3678
3679     case PLUS: 
3680       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3681         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3682                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3683                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3684                         && arm_const_double_rtx (XEXP (x, 1))))
3685                    ? 0 : 8));
3686
3687       /* Fall through */
3688     case AND: case XOR: case IOR: 
3689       extra_cost = 0;
3690
3691       /* Normally the frame registers will be spilt into reg+const during
3692          reload, so it is a bad idea to combine them with other instructions,
3693          since then they might not be moved outside of loops.  As a compromise
3694          we allow integration with ops that have a constant as their second
3695          operand.  */
3696       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3697            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3698            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3699           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3700               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3701         extra_cost = 4;
3702
3703       if (mode == DImode)
3704         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3705                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3706                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3707                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3708                    ? 0 : 8));
3709
3710       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3711         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3712                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3713                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3714                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3715                    ? 0 : 4));
3716
3717       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3718         return (1 + extra_cost
3719                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3720                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3721                      || subcode == ROTATE || subcode == ROTATERT
3722                      || (subcode == MULT
3723                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3724                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3725                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3726                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3727                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3728                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3729                    ? 0 : 4));
3730
3731       return 8;
3732
3733     case MULT:
3734       /* This should have been handled by the CPU specific routines.  */
3735       abort ();
3736
3737     case TRUNCATE:
3738       if (arm_arch3m && mode == SImode
3739           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3740           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3741           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3742               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3743           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3744               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3745         return 8;
3746       return 99;
3747
3748     case NEG:
3749       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3750         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3751       /* Fall through */
3752     case NOT:
3753       if (mode == DImode)
3754         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3755
3756       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3757
3758     case IF_THEN_ELSE:
3759       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3760         return 14;
3761       return 2;
3762
3763     case COMPARE:
3764       return 1;
3765
3766     case ABS:
3767       return 4 + (mode == DImode ? 4 : 0);
3768
3769     case SIGN_EXTEND:
3770       if (GET_MODE (XEXP (x, 0)) == QImode)
3771         return (4 + (mode == DImode ? 4 : 0)
3772                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3773       /* Fall through */
3774     case ZERO_EXTEND:
3775       switch (GET_MODE (XEXP (x, 0)))
3776         {
3777         case QImode:
3778           return (1 + (mode == DImode ? 4 : 0)
3779                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3780
3781         case HImode:
3782           return (4 + (mode == DImode ? 4 : 0)
3783                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3784
3785         case SImode:
3786           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3787
3788         case V8QImode:
3789         case V4HImode:
3790         case V2SImode:
3791         case V4QImode:
3792         case V2HImode:
3793             return 1;
3794
3795         default:
3796           break;
3797         }
3798       abort ();
3799
3800     case CONST_INT:                                             
3801       if (const_ok_for_arm (INTVAL (x)))                        
3802         return outer == SET ? 2 : -1;                   
3803       else if (outer == AND                             
3804                && const_ok_for_arm (~INTVAL (x)))               
3805         return -1;                                              
3806       else if ((outer == COMPARE                        
3807                 || outer == PLUS || outer == MINUS)     
3808                && const_ok_for_arm (-INTVAL (x)))               
3809         return -1;                                              
3810       else                                                      
3811         return 5;
3812       
3813     case CONST:                                                         
3814     case LABEL_REF:                                             
3815     case SYMBOL_REF:                                            
3816       return 6;
3817       
3818     case CONST_DOUBLE:                                          
3819       if (arm_const_double_rtx (x))
3820         return outer == SET ? 2 : -1;                   
3821       else if ((outer == COMPARE || outer == PLUS)      
3822                && neg_const_double_rtx_ok_for_fpa (x))          
3823         return -1;                                              
3824       return 7;
3825       
3826     default:
3827       return 99;
3828     }
3829 }
3830
3831 /* RTX costs for cores with a slow MUL implementation.  */
3832
3833 static bool
3834 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3835 {
3836   enum machine_mode mode = GET_MODE (x);
3837
3838   if (TARGET_THUMB)
3839     {
3840       *total = thumb_rtx_costs (x, code, outer_code);
3841       return true;
3842     }
3843   
3844   switch (code)
3845     {
3846     case MULT:
3847       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3848           || mode == DImode)
3849         {
3850           *total = 30;
3851           return true;
3852         }
3853
3854       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3855         {
3856           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3857                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3858           int cost, const_ok = const_ok_for_arm (i);
3859           int j, booth_unit_size;
3860
3861           /* Tune as appropriate.  */ 
3862           cost = const_ok ? 4 : 8;
3863           booth_unit_size = 2;
3864           for (j = 0; i && j < 32; j += booth_unit_size)
3865             {
3866               i >>= booth_unit_size;
3867               cost += 2;
3868             }
3869
3870           *total = cost;
3871           return true;
3872         }
3873
3874       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3875                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3876       return true;
3877   
3878     default:
3879       *total = arm_rtx_costs_1 (x, code, outer_code);
3880       return true;
3881     }
3882 }
3883
3884
3885 /* RTX cost for cores with a fast multiply unit (M variants).  */
3886
3887 static bool
3888 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3889 {
3890   enum machine_mode mode = GET_MODE (x);
3891
3892   if (TARGET_THUMB)
3893     {
3894       *total = thumb_rtx_costs (x, code, outer_code);
3895       return true;
3896     }
3897   
3898   switch (code)
3899     {
3900     case MULT:
3901       /* There is no point basing this on the tuning, since it is always the
3902          fast variant if it exists at all.  */
3903       if (mode == DImode
3904           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3905           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3906               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3907         {
3908           *total = 8;
3909           return true;
3910         }
3911       
3912
3913       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3914           || mode == DImode)
3915         {
3916           *total = 30;
3917           return true;
3918         }
3919
3920       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3921         {
3922           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3923                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3924           int cost, const_ok = const_ok_for_arm (i);
3925           int j, booth_unit_size;
3926
3927           /* Tune as appropriate.  */ 
3928           cost = const_ok ? 4 : 8;
3929           booth_unit_size = 8;
3930           for (j = 0; i && j < 32; j += booth_unit_size)
3931             {
3932               i >>= booth_unit_size;
3933               cost += 2;
3934             }
3935
3936           *total = cost;
3937           return true;
3938         }
3939
3940       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3941                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3942       return true;
3943   
3944     default:
3945       *total = arm_rtx_costs_1 (x, code, outer_code);
3946       return true;
3947     }
3948 }
3949
3950
3951 /* RTX cost for XScale CPUs.  */
3952
3953 static bool
3954 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3955 {
3956   enum machine_mode mode = GET_MODE (x);
3957
3958   if (TARGET_THUMB)
3959     {
3960       *total = thumb_rtx_costs (x, code, outer_code);
3961       return true;
3962     }
3963   
3964   switch (code)
3965     {
3966     case MULT:
3967       /* There is no point basing this on the tuning, since it is always the
3968          fast variant if it exists at all.  */
3969       if (mode == DImode
3970           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3971           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3972               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3973         {
3974           *total = 8;
3975           return true;
3976         }
3977       
3978
3979       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3980           || mode == DImode)
3981         {
3982           *total = 30;
3983           return true;
3984         }
3985
3986       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3987         {
3988           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3989                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3990           int cost, const_ok = const_ok_for_arm (i);
3991           unsigned HOST_WIDE_INT masked_const;
3992
3993           /* The cost will be related to two insns.
3994              First a load of the constant (MOV or LDR), then a multiply.  */
3995           cost = 2;
3996           if (! const_ok)
3997             cost += 1;      /* LDR is probably more expensive because
3998                                of longer result latency.  */
3999           masked_const = i & 0xffff8000;
4000           if (masked_const != 0 && masked_const != 0xffff8000)
4001             {
4002               masked_const = i & 0xf8000000;
4003               if (masked_const == 0 || masked_const == 0xf8000000)
4004                 cost += 1;
4005               else
4006                 cost += 2;
4007             }
4008           *total = cost;
4009           return true;
4010         }
4011
4012       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4013                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4014       return true;
4015   
4016     default:
4017       *total = arm_rtx_costs_1 (x, code, outer_code);
4018       return true;
4019     }
4020 }
4021
4022
4023 /* RTX costs for 9e (and later) cores.  */
4024
4025 static bool
4026 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4027 {
4028   enum machine_mode mode = GET_MODE (x);
4029   int nonreg_cost;
4030   int cost;
4031   
4032   if (TARGET_THUMB)
4033     {
4034       switch (code)
4035         {
4036         case MULT:
4037           *total = COSTS_N_INSNS (3);
4038           return true;
4039           
4040         default:
4041           *total = thumb_rtx_costs (x, code, outer_code);
4042           return true;
4043         }
4044     }
4045   
4046   switch (code)
4047     {
4048     case MULT:
4049       /* There is no point basing this on the tuning, since it is always the
4050          fast variant if it exists at all.  */
4051       if (mode == DImode
4052           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4053           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4054               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4055         {
4056           *total = 3;
4057           return true;
4058         }
4059       
4060
4061       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4062         {
4063           *total = 30;
4064           return true;
4065         }
4066       if (mode == DImode)
4067         {
4068           cost = 7;
4069           nonreg_cost = 8;
4070         }
4071       else
4072         {
4073           cost = 2;
4074           nonreg_cost = 4;
4075         }
4076
4077
4078       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4079                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4080       return true;
4081   
4082     default:
4083       *total = arm_rtx_costs_1 (x, code, outer_code);
4084       return true;
4085     }
4086 }
4087 /* All address computations that can be done are free, but rtx cost returns
4088    the same for practically all of them.  So we weight the different types
4089    of address here in the order (most pref first):
4090    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4091 static inline int
4092 arm_arm_address_cost (rtx x)
4093 {
4094   enum rtx_code c  = GET_CODE (x);
4095
4096   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4097     return 0;
4098   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4099     return 10;
4100
4101   if (c == PLUS || c == MINUS)
4102     {
4103       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4104         return 2;
4105
4106       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4107         return 3;
4108
4109       return 4;
4110     }
4111
4112   return 6;
4113 }
4114
4115 static inline int
4116 arm_thumb_address_cost (rtx x)
4117 {
4118   enum rtx_code c  = GET_CODE (x);
4119
4120   if (c == REG)
4121     return 1;
4122   if (c == PLUS
4123       && GET_CODE (XEXP (x, 0)) == REG
4124       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4125     return 1;
4126
4127   return 2;
4128 }
4129
4130 static int
4131 arm_address_cost (rtx x)
4132 {
4133   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4134 }
4135
4136 static int
4137 arm_use_dfa_pipeline_interface (void)
4138 {
4139   return true;
4140 }
4141
4142 static int
4143 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4144 {
4145   rtx i_pat, d_pat;
4146
4147   /* Some true dependencies can have a higher cost depending
4148      on precisely how certain input operands are used.  */
4149   if (arm_tune_xscale
4150       && REG_NOTE_KIND (link) == 0
4151       && recog_memoized (insn) >= 0
4152       && recog_memoized (dep) >= 0)
4153     {
4154       int shift_opnum = get_attr_shift (insn);
4155       enum attr_type attr_type = get_attr_type (dep);
4156
4157       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4158          operand for INSN.  If we have a shifted input operand and the
4159          instruction we depend on is another ALU instruction, then we may
4160          have to account for an additional stall.  */
4161       if (shift_opnum != 0
4162           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4163         {
4164           rtx shifted_operand;
4165           int opno;
4166           
4167           /* Get the shifted operand.  */
4168           extract_insn (insn);
4169           shifted_operand = recog_data.operand[shift_opnum];
4170
4171           /* Iterate over all the operands in DEP.  If we write an operand
4172              that overlaps with SHIFTED_OPERAND, then we have increase the
4173              cost of this dependency.  */
4174           extract_insn (dep);
4175           preprocess_constraints ();
4176           for (opno = 0; opno < recog_data.n_operands; opno++)
4177             {
4178               /* We can ignore strict inputs.  */
4179               if (recog_data.operand_type[opno] == OP_IN)
4180                 continue;
4181
4182               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4183                                            shifted_operand))
4184                 return 2;
4185             }
4186         }
4187     }
4188
4189   /* XXX This is not strictly true for the FPA.  */
4190   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4191       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4192     return 0;
4193
4194   /* Call insns don't incur a stall, even if they follow a load.  */
4195   if (REG_NOTE_KIND (link) == 0
4196       && GET_CODE (insn) == CALL_INSN)
4197     return 1;
4198
4199   if ((i_pat = single_set (insn)) != NULL
4200       && GET_CODE (SET_SRC (i_pat)) == MEM
4201       && (d_pat = single_set (dep)) != NULL
4202       && GET_CODE (SET_DEST (d_pat)) == MEM)
4203     {
4204       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4205       /* This is a load after a store, there is no conflict if the load reads
4206          from a cached area.  Assume that loads from the stack, and from the
4207          constant pool are cached, and that others will miss.  This is a 
4208          hack.  */
4209       
4210       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4211           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4212           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4213           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4214         return 1;
4215     }
4216
4217   return cost;
4218 }
4219
4220 static int fp_consts_inited = 0;
4221
4222 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4223 static const char * const strings_fp[8] =
4224 {
4225   "0",   "1",   "2",   "3",
4226   "4",   "5",   "0.5", "10"
4227 };
4228
4229 static REAL_VALUE_TYPE values_fp[8];
4230
4231 static void
4232 init_fp_table (void)
4233 {
4234   int i;
4235   REAL_VALUE_TYPE r;
4236
4237   if (TARGET_VFP)
4238     fp_consts_inited = 1;
4239   else
4240     fp_consts_inited = 8;
4241
4242   for (i = 0; i < fp_consts_inited; i++)
4243     {
4244       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4245       values_fp[i] = r;
4246     }
4247 }
4248
4249 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4250 int
4251 arm_const_double_rtx (rtx x)
4252 {
4253   REAL_VALUE_TYPE r;
4254   int i;
4255   
4256   if (!fp_consts_inited)
4257     init_fp_table ();
4258   
4259   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4260   if (REAL_VALUE_MINUS_ZERO (r))
4261     return 0;
4262
4263   for (i = 0; i < fp_consts_inited; i++)
4264     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4265       return 1;
4266
4267   return 0;
4268 }
4269
4270 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4271 int
4272 neg_const_double_rtx_ok_for_fpa (rtx x)
4273 {
4274   REAL_VALUE_TYPE r;
4275   int i;
4276   
4277   if (!fp_consts_inited)
4278     init_fp_table ();
4279   
4280   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4281   r = REAL_VALUE_NEGATE (r);
4282   if (REAL_VALUE_MINUS_ZERO (r))
4283     return 0;
4284
4285   for (i = 0; i < 8; i++)
4286     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4287       return 1;
4288
4289   return 0;
4290 }
4291 \f
4292 /* Predicates for `match_operand' and `match_operator'.  */
4293
4294 /* s_register_operand is the same as register_operand, but it doesn't accept
4295    (SUBREG (MEM)...).
4296
4297    This function exists because at the time it was put in it led to better
4298    code.  SUBREG(MEM) always needs a reload in the places where
4299    s_register_operand is used, and this seemed to lead to excessive
4300    reloading.  */
4301 int
4302 s_register_operand (rtx op, enum machine_mode mode)
4303 {
4304   if (GET_MODE (op) != mode && mode != VOIDmode)
4305     return 0;
4306
4307   if (GET_CODE (op) == SUBREG)
4308     op = SUBREG_REG (op);
4309
4310   /* We don't consider registers whose class is NO_REGS
4311      to be a register operand.  */
4312   /* XXX might have to check for lo regs only for thumb ??? */
4313   return (GET_CODE (op) == REG
4314           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4315               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4316 }
4317
4318 /* A hard register operand (even before reload.  */
4319 int
4320 arm_hard_register_operand (rtx op, enum machine_mode mode)
4321 {
4322   if (GET_MODE (op) != mode && mode != VOIDmode)
4323     return 0;
4324
4325   return (GET_CODE (op) == REG
4326           && REGNO (op) < FIRST_PSEUDO_REGISTER);
4327 }
4328     
4329 /* An arm register operand.  */
4330 int
4331 arm_general_register_operand (rtx op, enum machine_mode mode)
4332 {
4333   if (GET_MODE (op) != mode && mode != VOIDmode)
4334     return 0;
4335
4336   if (GET_CODE (op) == SUBREG)
4337     op = SUBREG_REG (op);
4338
4339   return (GET_CODE (op) == REG
4340           && (REGNO (op) <= LAST_ARM_REGNUM
4341               || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4342 }
4343
4344 /* Only accept reg, subreg(reg), const_int.  */
4345 int
4346 reg_or_int_operand (rtx op, enum machine_mode mode)
4347 {
4348   if (GET_CODE (op) == CONST_INT)
4349     return 1;
4350
4351   if (GET_MODE (op) != mode && mode != VOIDmode)
4352     return 0;
4353
4354   if (GET_CODE (op) == SUBREG)
4355     op = SUBREG_REG (op);
4356
4357   /* We don't consider registers whose class is NO_REGS
4358      to be a register operand.  */
4359   return (GET_CODE (op) == REG
4360           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4361               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4362 }
4363
4364 /* Return 1 if OP is an item in memory, given that we are in reload.  */
4365 int
4366 arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4367 {
4368   int regno = true_regnum (op);
4369
4370   return (!CONSTANT_P (op)
4371           && (regno == -1
4372               || (GET_CODE (op) == REG
4373                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4374 }
4375
4376 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
4377 int
4378 arm_rhs_operand (rtx op, enum machine_mode mode)
4379 {
4380   return (s_register_operand (op, mode)
4381           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
4382 }
4383
4384 /* Return TRUE for valid operands for the
4385    rhs of an ARM instruction, or a load.  */
4386 int
4387 arm_rhsm_operand (rtx op, enum machine_mode mode)
4388 {
4389   return (s_register_operand (op, mode)
4390           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4391           || memory_operand (op, mode));
4392 }
4393
4394 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4395    constant that is valid when negated.  */
4396 int
4397 arm_add_operand (rtx op, enum machine_mode mode)
4398 {
4399   if (TARGET_THUMB)
4400     return thumb_cmp_operand (op, mode);
4401   
4402   return (s_register_operand (op, mode)
4403           || (GET_CODE (op) == CONST_INT
4404               && (const_ok_for_arm (INTVAL (op))
4405                   || const_ok_for_arm (-INTVAL (op)))));
4406 }
4407
4408 /* Return TRUE for valid ARM constants (or when valid if negated).  */
4409 int
4410 arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4411 {
4412   return (GET_CODE (op) == CONST_INT
4413           && (const_ok_for_arm (INTVAL (op))
4414               || const_ok_for_arm (-INTVAL (op))));
4415 }
4416
4417 int
4418 arm_not_operand (rtx op, enum machine_mode mode)
4419 {
4420   return (s_register_operand (op, mode)
4421           || (GET_CODE (op) == CONST_INT
4422               && (const_ok_for_arm (INTVAL (op))
4423                   || const_ok_for_arm (~INTVAL (op)))));
4424 }
4425
4426 /* Return TRUE if the operand is a memory reference which contains an
4427    offsettable address.  */
4428 int
4429 offsettable_memory_operand (rtx op, enum machine_mode mode)
4430 {
4431   if (mode == VOIDmode)
4432     mode = GET_MODE (op);
4433
4434   return (mode == GET_MODE (op)
4435           && GET_CODE (op) == MEM
4436           && offsettable_address_p (reload_completed | reload_in_progress,
4437                                     mode, XEXP (op, 0)));
4438 }
4439
4440 /* Return TRUE if the operand is a memory reference which is, or can be
4441    made word aligned by adjusting the offset.  */
4442 int
4443 alignable_memory_operand (rtx op, enum machine_mode mode)
4444 {
4445   rtx reg;
4446
4447   if (mode == VOIDmode)
4448     mode = GET_MODE (op);
4449
4450   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4451     return 0;
4452
4453   op = XEXP (op, 0);
4454
4455   return ((GET_CODE (reg = op) == REG
4456            || (GET_CODE (op) == SUBREG
4457                && GET_CODE (reg = SUBREG_REG (op)) == REG)
4458            || (GET_CODE (op) == PLUS
4459                && GET_CODE (XEXP (op, 1)) == CONST_INT
4460                && (GET_CODE (reg = XEXP (op, 0)) == REG
4461                    || (GET_CODE (XEXP (op, 0)) == SUBREG
4462                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
4463           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
4464 }
4465
4466 /* Similar to s_register_operand, but does not allow hard integer 
4467    registers.  */
4468 int
4469 f_register_operand (rtx op, enum machine_mode mode)
4470 {
4471   if (GET_MODE (op) != mode && mode != VOIDmode)
4472     return 0;
4473
4474   if (GET_CODE (op) == SUBREG)
4475     op = SUBREG_REG (op);
4476
4477   /* We don't consider registers whose class is NO_REGS
4478      to be a register operand.  */
4479   return (GET_CODE (op) == REG
4480           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4481               || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
4482 }
4483
4484 /* Return TRUE for valid operands for the rhs of an floating point insns.
4485    Allows regs or certain consts on FPA, just regs for everything else.  */
4486 int
4487 arm_float_rhs_operand (rtx op, enum machine_mode mode)
4488 {
4489   if (s_register_operand (op, mode))
4490     return TRUE;
4491
4492   if (GET_MODE (op) != mode && mode != VOIDmode)
4493     return FALSE;
4494
4495   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4496     return arm_const_double_rtx (op);
4497
4498   return FALSE;
4499 }
4500
4501 int
4502 arm_float_add_operand (rtx op, enum machine_mode mode)
4503 {
4504   if (s_register_operand (op, mode))
4505     return TRUE;
4506
4507   if (GET_MODE (op) != mode && mode != VOIDmode)
4508     return FALSE;
4509
4510   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4511     return (arm_const_double_rtx (op)
4512             || neg_const_double_rtx_ok_for_fpa (op));
4513
4514   return FALSE;
4515 }
4516
4517
4518 /* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4519    Depends which fpu we are targeting.  */
4520
4521 int
4522 arm_float_compare_operand (rtx op, enum machine_mode mode)
4523 {
4524   if (TARGET_VFP)
4525     return vfp_compare_operand (op, mode);
4526   else
4527     return arm_float_rhs_operand (op, mode);
4528 }
4529
4530
4531 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4532 int
4533 cirrus_memory_offset (rtx op)
4534 {
4535   /* Reject eliminable registers.  */
4536   if (! (reload_in_progress || reload_completed)
4537       && (   reg_mentioned_p (frame_pointer_rtx, op)
4538           || reg_mentioned_p (arg_pointer_rtx, op)
4539           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4540           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4541           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4542           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4543     return 0;
4544
4545   if (GET_CODE (op) == MEM)
4546     {
4547       rtx ind;
4548
4549       ind = XEXP (op, 0);
4550
4551       /* Match: (mem (reg)).  */
4552       if (GET_CODE (ind) == REG)
4553         return 1;
4554
4555       /* Match:
4556          (mem (plus (reg)
4557                     (const))).  */
4558       if (GET_CODE (ind) == PLUS
4559           && GET_CODE (XEXP (ind, 0)) == REG
4560           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4561           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4562         return 1;
4563     }
4564
4565   return 0;
4566 }
4567
4568 int
4569 arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4570 {
4571   if (!memory_operand (op, mode))
4572     return 0;
4573
4574   return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4575 }
4576
4577 /* Return nonzero if OP is a Cirrus or general register.  */
4578 int
4579 cirrus_register_operand (rtx op, enum machine_mode mode)
4580 {
4581   if (GET_MODE (op) != mode && mode != VOIDmode)
4582     return FALSE;
4583
4584   if (GET_CODE (op) == SUBREG)
4585     op = SUBREG_REG (op);
4586
4587   return (GET_CODE (op) == REG
4588           && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4589               || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4590 }
4591
4592 /* Return nonzero if OP is a cirrus FP register.  */
4593 int
4594 cirrus_fp_register (rtx op, enum machine_mode mode)
4595 {
4596   if (GET_MODE (op) != mode && mode != VOIDmode)
4597     return FALSE;
4598
4599   if (GET_CODE (op) == SUBREG)
4600     op = SUBREG_REG (op);
4601
4602   return (GET_CODE (op) == REG
4603           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4604               || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4605 }
4606
4607 /* Return nonzero if OP is a 6bit constant (0..63).  */
4608 int
4609 cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4610 {
4611   return (GET_CODE (op) == CONST_INT
4612           && INTVAL (op) >= 0
4613           && INTVAL (op) < 64);
4614 }
4615
4616
4617 /* Return TRUE if OP is a valid VFP memory address pattern.  */
4618 /* Copied from cirrus_memory_offset but with restricted offset range.  */
4619
4620 int
4621 vfp_mem_operand (rtx op)
4622 {
4623   /* Reject eliminable registers.  */
4624
4625   if (! (reload_in_progress || reload_completed)
4626       && (   reg_mentioned_p (frame_pointer_rtx, op)
4627           || reg_mentioned_p (arg_pointer_rtx, op)
4628           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4629           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4630           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4631           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4632     return FALSE;
4633
4634   /* Constants are converted into offsets from labels.  */
4635   if (GET_CODE (op) == MEM)
4636     {
4637       rtx ind;
4638
4639       ind = XEXP (op, 0);
4640
4641       if (reload_completed
4642           && (GET_CODE (ind) == LABEL_REF
4643               || (GET_CODE (ind) == CONST
4644                   && GET_CODE (XEXP (ind, 0)) == PLUS
4645                   && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4646                   && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4647         return TRUE;
4648
4649       /* Match: (mem (reg)).  */
4650       if (GET_CODE (ind) == REG)
4651         return arm_address_register_rtx_p (ind, 0);
4652
4653       /* Match:
4654          (mem (plus (reg)
4655                     (const))).  */
4656       if (GET_CODE (ind) == PLUS
4657           && GET_CODE (XEXP (ind, 0)) == REG
4658           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4659           && GET_CODE (XEXP (ind, 1)) == CONST_INT
4660           && INTVAL (XEXP (ind, 1)) > -1024
4661           && INTVAL (XEXP (ind, 1)) <  1024)
4662         return TRUE;
4663     }
4664
4665   return FALSE;
4666 }
4667
4668
4669 /* Return TRUE if OP is a REG or constant zero.  */
4670 int
4671 vfp_compare_operand (rtx op, enum machine_mode mode)
4672 {
4673   if (s_register_operand (op, mode))
4674     return TRUE;
4675
4676   return (GET_CODE (op) == CONST_DOUBLE
4677           && arm_const_double_rtx (op));
4678 }
4679
4680
4681 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4682    VFP registers.  Otherwise return NO_REGS.  */
4683
4684 enum reg_class
4685 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4686 {
4687   if (vfp_mem_operand (x) || s_register_operand (x, mode))
4688     return NO_REGS;
4689
4690   return GENERAL_REGS;
4691 }
4692
4693
4694 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4695    Use by the Cirrus Maverick code which has to workaround
4696    a hardware bug triggered by such instructions.  */
4697 static bool
4698 arm_memory_load_p (rtx insn)
4699 {
4700   rtx body, lhs, rhs;;
4701
4702   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4703     return false;
4704
4705   body = PATTERN (insn);
4706
4707   if (GET_CODE (body) != SET)
4708     return false;
4709
4710   lhs = XEXP (body, 0);
4711   rhs = XEXP (body, 1);
4712
4713   lhs = REG_OR_SUBREG_RTX (lhs);
4714
4715   /* If the destination is not a general purpose
4716      register we do not have to worry.  */
4717   if (GET_CODE (lhs) != REG
4718       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4719     return false;
4720
4721   /* As well as loads from memory we also have to react
4722      to loads of invalid constants which will be turned
4723      into loads from the minipool.  */
4724   return (GET_CODE (rhs) == MEM
4725           || GET_CODE (rhs) == SYMBOL_REF
4726           || note_invalid_constants (insn, -1, false));
4727 }
4728
4729 /* Return TRUE if INSN is a Cirrus instruction.  */
4730 static bool
4731 arm_cirrus_insn_p (rtx insn)
4732 {
4733   enum attr_cirrus attr;
4734
4735   /* get_attr aborts on USE and CLOBBER.  */
4736   if (!insn
4737       || GET_CODE (insn) != INSN
4738       || GET_CODE (PATTERN (insn)) == USE
4739       || GET_CODE (PATTERN (insn)) == CLOBBER)
4740     return 0;
4741
4742   attr = get_attr_cirrus (insn);
4743
4744   return attr != CIRRUS_NOT;
4745 }
4746
4747 /* Cirrus reorg for invalid instruction combinations.  */
4748 static void
4749 cirrus_reorg (rtx first)
4750 {
4751   enum attr_cirrus attr;
4752   rtx body = PATTERN (first);
4753   rtx t;
4754   int nops;
4755
4756   /* Any branch must be followed by 2 non Cirrus instructions.  */
4757   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4758     {
4759       nops = 0;
4760       t = next_nonnote_insn (first);
4761
4762       if (arm_cirrus_insn_p (t))
4763         ++ nops;
4764
4765       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4766         ++ nops;
4767
4768       while (nops --)
4769         emit_insn_after (gen_nop (), first);
4770
4771       return;
4772     }
4773
4774   /* (float (blah)) is in parallel with a clobber.  */
4775   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4776     body = XVECEXP (body, 0, 0);
4777
4778   if (GET_CODE (body) == SET)
4779     {
4780       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4781
4782       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4783          be followed by a non Cirrus insn.  */
4784       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4785         {
4786           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4787             emit_insn_after (gen_nop (), first);
4788
4789           return;
4790         }
4791       else if (arm_memory_load_p (first))
4792         {
4793           unsigned int arm_regno;
4794
4795           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4796              ldr/cfmv64hr combination where the Rd field is the same
4797              in both instructions must be split with a non Cirrus
4798              insn.  Example:
4799
4800              ldr r0, blah
4801              nop
4802              cfmvsr mvf0, r0.  */
4803
4804           /* Get Arm register number for ldr insn.  */
4805           if (GET_CODE (lhs) == REG)
4806             arm_regno = REGNO (lhs);
4807           else if (GET_CODE (rhs) == REG)
4808             arm_regno = REGNO (rhs);
4809           else
4810             abort ();
4811
4812           /* Next insn.  */
4813           first = next_nonnote_insn (first);
4814
4815           if (! arm_cirrus_insn_p (first))
4816             return;
4817
4818           body = PATTERN (first);
4819
4820           /* (float (blah)) is in parallel with a clobber.  */
4821           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4822             body = XVECEXP (body, 0, 0);
4823
4824           if (GET_CODE (body) == FLOAT)
4825             body = XEXP (body, 0);
4826
4827           if (get_attr_cirrus (first) == CIRRUS_MOVE
4828               && GET_CODE (XEXP (body, 1)) == REG
4829               && arm_regno == REGNO (XEXP (body, 1)))
4830             emit_insn_after (gen_nop (), first);
4831
4832           return;
4833         }
4834     }
4835
4836   /* get_attr aborts on USE and CLOBBER.  */
4837   if (!first
4838       || GET_CODE (first) != INSN
4839       || GET_CODE (PATTERN (first)) == USE
4840       || GET_CODE (PATTERN (first)) == CLOBBER)
4841     return;
4842
4843   attr = get_attr_cirrus (first);
4844
4845   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4846      must be followed by a non-coprocessor instruction.  */
4847   if (attr == CIRRUS_COMPARE)
4848     {
4849       nops = 0;
4850
4851       t = next_nonnote_insn (first);
4852
4853       if (arm_cirrus_insn_p (t))
4854         ++ nops;
4855
4856       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4857         ++ nops;
4858
4859       while (nops --)
4860         emit_insn_after (gen_nop (), first);
4861
4862       return;
4863     }
4864 }
4865
4866 /* Return nonzero if OP is a constant power of two.  */
4867 int
4868 power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4869 {
4870   if (GET_CODE (op) == CONST_INT)
4871     {
4872       HOST_WIDE_INT value = INTVAL (op);
4873
4874       return value != 0  &&  (value & (value - 1)) == 0;
4875     }
4876
4877   return FALSE;
4878 }
4879
4880 /* Return TRUE for a valid operand of a DImode operation.
4881    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4882    Note that this disallows MEM(REG+REG), but allows
4883    MEM(PRE/POST_INC/DEC(REG)).  */
4884 int
4885 di_operand (rtx op, enum machine_mode mode)
4886 {
4887   if (s_register_operand (op, mode))
4888     return TRUE;
4889
4890   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4891     return FALSE;
4892
4893   if (GET_CODE (op) == SUBREG)
4894     op = SUBREG_REG (op);
4895
4896   switch (GET_CODE (op))
4897     {
4898     case CONST_DOUBLE:
4899     case CONST_INT:
4900       return TRUE;
4901
4902     case MEM:
4903       return memory_address_p (DImode, XEXP (op, 0));
4904
4905     default:
4906       return FALSE;
4907     }
4908 }
4909
4910 /* Like di_operand, but don't accept constants.  */
4911 int
4912 nonimmediate_di_operand (rtx op, enum machine_mode mode)
4913 {
4914   if (s_register_operand (op, mode))
4915     return TRUE;
4916
4917   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4918     return FALSE;
4919
4920   if (GET_CODE (op) == SUBREG)
4921     op = SUBREG_REG (op);
4922
4923   if (GET_CODE (op) == MEM)
4924     return memory_address_p (DImode, XEXP (op, 0));
4925
4926   return FALSE;
4927 }
4928
4929 /* Return TRUE for a valid operand of a DFmode operation when soft-float.
4930    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4931    Note that this disallows MEM(REG+REG), but allows
4932    MEM(PRE/POST_INC/DEC(REG)).  */
4933 int
4934 soft_df_operand (rtx op, enum machine_mode mode)
4935 {
4936   if (s_register_operand (op, mode))
4937     return TRUE;
4938
4939   if (mode != VOIDmode && GET_MODE (op) != mode)
4940     return FALSE;
4941
4942   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4943     return FALSE;
4944   
4945   if (GET_CODE (op) == SUBREG)
4946     op = SUBREG_REG (op);
4947   
4948   switch (GET_CODE (op))
4949     {
4950     case CONST_DOUBLE:
4951       return TRUE;
4952
4953     case MEM:
4954       return memory_address_p (DFmode, XEXP (op, 0));
4955
4956     default:
4957       return FALSE;
4958     }
4959 }
4960
4961 /* Like soft_df_operand, but don't accept constants.  */
4962 int
4963 nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
4964 {
4965   if (s_register_operand (op, mode))
4966     return TRUE;
4967
4968   if (mode != VOIDmode && GET_MODE (op) != mode)
4969     return FALSE;
4970
4971   if (GET_CODE (op) == SUBREG)
4972     op = SUBREG_REG (op);
4973
4974   if (GET_CODE (op) == MEM)
4975     return memory_address_p (DFmode, XEXP (op, 0));
4976   return FALSE;
4977 }
4978
4979 /* Return TRUE for valid index operands.  */
4980 int
4981 index_operand (rtx op, enum machine_mode mode)
4982 {
4983   return (s_register_operand (op, mode)
4984           || (immediate_operand (op, mode)
4985               && (GET_CODE (op) != CONST_INT
4986                   || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4987 }
4988
4989 /* Return TRUE for valid shifts by a constant. This also accepts any
4990    power of two on the (somewhat overly relaxed) assumption that the
4991    shift operator in this case was a mult.  */
4992 int
4993 const_shift_operand (rtx op, enum machine_mode mode)
4994 {
4995   return (power_of_two_operand (op, mode)
4996           || (immediate_operand (op, mode)
4997               && (GET_CODE (op) != CONST_INT
4998                   || (INTVAL (op) < 32 && INTVAL (op) > 0))));
4999 }
5000
5001 /* Return TRUE for arithmetic operators which can be combined with a multiply
5002    (shift).  */
5003 int
5004 shiftable_operator (rtx x, enum machine_mode mode)
5005 {
5006   enum rtx_code code;
5007
5008   if (GET_MODE (x) != mode)
5009     return FALSE;
5010
5011   code = GET_CODE (x);
5012
5013   return (code == PLUS || code == MINUS
5014           || code == IOR || code == XOR || code == AND);
5015 }
5016
5017 /* Return TRUE for binary logical operators.  */
5018 int
5019 logical_binary_operator (rtx x, enum machine_mode mode)
5020 {
5021   enum rtx_code code;
5022
5023   if (GET_MODE (x) != mode)
5024     return FALSE;
5025
5026   code = GET_CODE (x);
5027
5028   return (code == IOR || code == XOR || code == AND);
5029 }
5030
5031 /* Return TRUE for shift operators.  */
5032 int
5033 shift_operator (rtx x,enum machine_mode mode)
5034 {
5035   enum rtx_code code;
5036
5037   if (GET_MODE (x) != mode)
5038     return FALSE;
5039
5040   code = GET_CODE (x);
5041
5042   if (code == MULT)
5043     return power_of_two_operand (XEXP (x, 1), mode);
5044
5045   return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5046           || code == ROTATERT);
5047 }
5048
5049 /* Return TRUE if x is EQ or NE.  */
5050 int
5051 equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
5052 {
5053   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
5054 }
5055
5056 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ.  */
5057 int
5058 arm_comparison_operator (rtx x, enum machine_mode mode)
5059 {
5060   return (comparison_operator (x, mode)
5061           && GET_CODE (x) != LTGT
5062           && GET_CODE (x) != UNEQ);
5063 }
5064
5065 /* Return TRUE for SMIN SMAX UMIN UMAX operators.  */
5066 int
5067 minmax_operator (rtx x, enum machine_mode mode)
5068 {
5069   enum rtx_code code = GET_CODE (x);
5070
5071   if (GET_MODE (x) != mode)
5072     return FALSE;
5073
5074   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
5075 }
5076
5077 /* Return TRUE if this is the condition code register, if we aren't given
5078    a mode, accept any class CCmode register.  */
5079 int
5080 cc_register (rtx x, enum machine_mode mode)
5081 {
5082   if (mode == VOIDmode)
5083     {
5084       mode = GET_MODE (x);
5085       
5086       if (GET_MODE_CLASS (mode) != MODE_CC)
5087         return FALSE;
5088     }
5089
5090   if (   GET_MODE (x) == mode
5091       && GET_CODE (x) == REG
5092       && REGNO    (x) == CC_REGNUM)
5093     return TRUE;
5094
5095   return FALSE;
5096 }
5097
5098 /* Return TRUE if this is the condition code register, if we aren't given
5099    a mode, accept any class CCmode register which indicates a dominance
5100    expression.  */
5101 int
5102 dominant_cc_register (rtx x, enum machine_mode mode)
5103 {
5104   if (mode == VOIDmode)
5105     {
5106       mode = GET_MODE (x);
5107       
5108       if (GET_MODE_CLASS (mode) != MODE_CC)
5109         return FALSE;
5110     }
5111
5112   if (mode != CC_DNEmode && mode != CC_DEQmode
5113       && mode != CC_DLEmode && mode != CC_DLTmode
5114       && mode != CC_DGEmode && mode != CC_DGTmode
5115       && mode != CC_DLEUmode && mode != CC_DLTUmode
5116       && mode != CC_DGEUmode && mode != CC_DGTUmode)
5117     return FALSE;
5118
5119   return cc_register (x, mode);
5120 }
5121
5122 /* Return TRUE if X references a SYMBOL_REF.  */
5123 int
5124 symbol_mentioned_p (rtx x)
5125 {
5126   const char * fmt;
5127   int i;
5128
5129   if (GET_CODE (x) == SYMBOL_REF)
5130     return 1;
5131
5132   fmt = GET_RTX_FORMAT (GET_CODE (x));
5133   
5134   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5135     {
5136       if (fmt[i] == 'E')
5137         {
5138           int j;
5139
5140           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5141             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5142               return 1;
5143         }
5144       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5145         return 1;
5146     }
5147
5148   return 0;
5149 }
5150
5151 /* Return TRUE if X references a LABEL_REF.  */
5152 int
5153 label_mentioned_p (rtx x)
5154 {
5155   const char * fmt;
5156   int i;
5157
5158   if (GET_CODE (x) == LABEL_REF)
5159     return 1;
5160
5161   fmt = GET_RTX_FORMAT (GET_CODE (x));
5162   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5163     {
5164       if (fmt[i] == 'E')
5165         {
5166           int j;
5167
5168           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5169             if (label_mentioned_p (XVECEXP (x, i, j)))
5170               return 1;
5171         }
5172       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5173         return 1;
5174     }
5175
5176   return 0;
5177 }
5178
5179 enum rtx_code
5180 minmax_code (rtx x)
5181 {
5182   enum rtx_code code = GET_CODE (x);
5183
5184   if (code == SMAX)
5185     return GE;
5186   else if (code == SMIN)
5187     return LE;
5188   else if (code == UMIN)
5189     return LEU;
5190   else if (code == UMAX)
5191     return GEU;
5192
5193   abort ();
5194 }
5195
5196 /* Return 1 if memory locations are adjacent.  */
5197 int
5198 adjacent_mem_locations (rtx a, rtx b)
5199 {
5200   if ((GET_CODE (XEXP (a, 0)) == REG
5201        || (GET_CODE (XEXP (a, 0)) == PLUS
5202            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5203       && (GET_CODE (XEXP (b, 0)) == REG
5204           || (GET_CODE (XEXP (b, 0)) == PLUS
5205               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5206     {
5207       int val0 = 0, val1 = 0;
5208       int reg0, reg1;
5209   
5210       if (GET_CODE (XEXP (a, 0)) == PLUS)
5211         {
5212           reg0 = REGNO  (XEXP (XEXP (a, 0), 0));
5213           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5214         }
5215       else
5216         reg0 = REGNO (XEXP (a, 0));
5217
5218       if (GET_CODE (XEXP (b, 0)) == PLUS)
5219         {
5220           reg1 = REGNO  (XEXP (XEXP (b, 0), 0));
5221           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5222         }
5223       else
5224         reg1 = REGNO (XEXP (b, 0));
5225
5226       /* Don't accept any offset that will require multiple
5227          instructions to handle, since this would cause the
5228          arith_adjacentmem pattern to output an overlong sequence.  */
5229       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5230         return 0;
5231       
5232       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5233     }
5234   return 0;
5235 }
5236
5237 /* Return 1 if OP is a load multiple operation.  It is known to be
5238    parallel and the first section will be tested.  */
5239 int
5240 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5241 {
5242   HOST_WIDE_INT count = XVECLEN (op, 0);
5243   int dest_regno;
5244   rtx src_addr;
5245   HOST_WIDE_INT i = 1, base = 0;
5246   rtx elt;
5247
5248   if (count <= 1
5249       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5250     return 0;
5251
5252   /* Check to see if this might be a write-back.  */
5253   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5254     {
5255       i++;
5256       base = 1;
5257
5258       /* Now check it more carefully.  */
5259       if (GET_CODE (SET_DEST (elt)) != REG
5260           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5261           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5262           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5263         return 0;
5264     }
5265
5266   /* Perform a quick check so we don't blow up below.  */
5267   if (count <= i
5268       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5269       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5270       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5271     return 0;
5272
5273   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5274   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5275
5276   for (; i < count; i++)
5277     {
5278       elt = XVECEXP (op, 0, i);
5279
5280       if (GET_CODE (elt) != SET
5281           || GET_CODE (SET_DEST (elt)) != REG
5282           || GET_MODE (SET_DEST (elt)) != SImode
5283           || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
5284           || GET_CODE (SET_SRC (elt)) != MEM
5285           || GET_MODE (SET_SRC (elt)) != SImode
5286           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5287           || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5288           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5289           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5290         return 0;
5291     }
5292
5293   return 1;
5294 }
5295
5296 /* Return 1 if OP is a store multiple operation.  It is known to be
5297    parallel and the first section will be tested.  */
5298 int
5299 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5300 {
5301   HOST_WIDE_INT count = XVECLEN (op, 0);
5302   int src_regno;
5303   rtx dest_addr;
5304   HOST_WIDE_INT i = 1, base = 0;
5305   rtx elt;
5306
5307   if (count <= 1
5308       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5309     return 0;
5310
5311   /* Check to see if this might be a write-back.  */
5312   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5313     {
5314       i++;
5315       base = 1;
5316
5317       /* Now check it more carefully.  */
5318       if (GET_CODE (SET_DEST (elt)) != REG
5319           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5320           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5321           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5322         return 0;
5323     }
5324
5325   /* Perform a quick check so we don't blow up below.  */
5326   if (count <= i
5327       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5328       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5329       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5330     return 0;
5331
5332   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5333   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5334
5335   for (; i < count; i++)
5336     {
5337       elt = XVECEXP (op, 0, i);
5338
5339       if (GET_CODE (elt) != SET
5340           || GET_CODE (SET_SRC (elt)) != REG
5341           || GET_MODE (SET_SRC (elt)) != SImode
5342           || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
5343           || GET_CODE (SET_DEST (elt)) != MEM
5344           || GET_MODE (SET_DEST (elt)) != SImode
5345           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5346           || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5347           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5348           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5349         return 0;
5350     }
5351
5352   return 1;
5353 }
5354
5355 int
5356 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5357                         HOST_WIDE_INT *load_offset)
5358 {
5359   int unsorted_regs[4];
5360   HOST_WIDE_INT unsorted_offsets[4];
5361   int order[4];
5362   int base_reg = -1;
5363   int i;
5364
5365   /* Can only handle 2, 3, or 4 insns at present,
5366      though could be easily extended if required.  */
5367   if (nops < 2 || nops > 4)
5368     abort ();
5369
5370   /* Loop over the operands and check that the memory references are
5371      suitable (ie immediate offsets from the same base register).  At
5372      the same time, extract the target register, and the memory
5373      offsets.  */
5374   for (i = 0; i < nops; i++)
5375     {
5376       rtx reg;
5377       rtx offset;
5378
5379       /* Convert a subreg of a mem into the mem itself.  */
5380       if (GET_CODE (operands[nops + i]) == SUBREG)
5381         operands[nops + i] = alter_subreg (operands + (nops + i));
5382
5383       if (GET_CODE (operands[nops + i]) != MEM)
5384         abort ();
5385
5386       /* Don't reorder volatile memory references; it doesn't seem worth
5387          looking for the case where the order is ok anyway.  */
5388       if (MEM_VOLATILE_P (operands[nops + i]))
5389         return 0;
5390
5391       offset = const0_rtx;
5392
5393       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5394            || (GET_CODE (reg) == SUBREG
5395                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5396           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5397               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5398                    == REG)
5399                   || (GET_CODE (reg) == SUBREG
5400                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5401               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5402                   == CONST_INT)))
5403         {
5404           if (i == 0)
5405             {
5406               base_reg = REGNO (reg);
5407               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5408                                   ? REGNO (operands[i])
5409                                   : REGNO (SUBREG_REG (operands[i])));
5410               order[0] = 0;
5411             }
5412           else 
5413             {
5414               if (base_reg != (int) REGNO (reg))
5415                 /* Not addressed from the same base register.  */
5416                 return 0;
5417
5418               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5419                                   ? REGNO (operands[i])
5420                                   : REGNO (SUBREG_REG (operands[i])));
5421               if (unsorted_regs[i] < unsorted_regs[order[0]])
5422                 order[0] = i;
5423             }
5424
5425           /* If it isn't an integer register, or if it overwrites the
5426              base register but isn't the last insn in the list, then
5427              we can't do this.  */
5428           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5429               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5430             return 0;
5431
5432           unsorted_offsets[i] = INTVAL (offset);
5433         }
5434       else
5435         /* Not a suitable memory address.  */
5436         return 0;
5437     }
5438
5439   /* All the useful information has now been extracted from the
5440      operands into unsorted_regs and unsorted_offsets; additionally,
5441      order[0] has been set to the lowest numbered register in the
5442      list.  Sort the registers into order, and check that the memory
5443      offsets are ascending and adjacent.  */
5444
5445   for (i = 1; i < nops; i++)
5446     {
5447       int j;
5448
5449       order[i] = order[i - 1];
5450       for (j = 0; j < nops; j++)
5451         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5452             && (order[i] == order[i - 1]
5453                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5454           order[i] = j;
5455
5456       /* Have we found a suitable register? if not, one must be used more
5457          than once.  */
5458       if (order[i] == order[i - 1])
5459         return 0;
5460
5461       /* Is the memory address adjacent and ascending? */
5462       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5463         return 0;
5464     }
5465
5466   if (base)
5467     {
5468       *base = base_reg;
5469
5470       for (i = 0; i < nops; i++)
5471         regs[i] = unsorted_regs[order[i]];
5472
5473       *load_offset = unsorted_offsets[order[0]];
5474     }
5475
5476   if (unsorted_offsets[order[0]] == 0)
5477     return 1; /* ldmia */
5478
5479   if (unsorted_offsets[order[0]] == 4)
5480     return 2; /* ldmib */
5481
5482   if (unsorted_offsets[order[nops - 1]] == 0)
5483     return 3; /* ldmda */
5484
5485   if (unsorted_offsets[order[nops - 1]] == -4)
5486     return 4; /* ldmdb */
5487
5488   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5489      if the offset isn't small enough.  The reason 2 ldrs are faster
5490      is because these ARMs are able to do more than one cache access
5491      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5492      whilst the ARM8 has a double bandwidth cache.  This means that
5493      these cores can do both an instruction fetch and a data fetch in
5494      a single cycle, so the trick of calculating the address into a
5495      scratch register (one of the result regs) and then doing a load
5496      multiple actually becomes slower (and no smaller in code size).
5497      That is the transformation
5498  
5499         ldr     rd1, [rbase + offset]
5500         ldr     rd2, [rbase + offset + 4]
5501  
5502      to
5503  
5504         add     rd1, rbase, offset
5505         ldmia   rd1, {rd1, rd2}
5506  
5507      produces worse code -- '3 cycles + any stalls on rd2' instead of
5508      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5509      access per cycle, the first sequence could never complete in less
5510      than 6 cycles, whereas the ldm sequence would only take 5 and
5511      would make better use of sequential accesses if not hitting the
5512      cache.
5513
5514      We cheat here and test 'arm_ld_sched' which we currently know to
5515      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5516      changes, then the test below needs to be reworked.  */
5517   if (nops == 2 && arm_ld_sched)
5518     return 0;
5519
5520   /* Can't do it without setting up the offset, only do this if it takes
5521      no more than one insn.  */
5522   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
5523           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5524 }
5525
5526 const char *
5527 emit_ldm_seq (rtx *operands, int nops)
5528 {
5529   int regs[4];
5530   int base_reg;
5531   HOST_WIDE_INT offset;
5532   char buf[100];
5533   int i;
5534
5535   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5536     {
5537     case 1:
5538       strcpy (buf, "ldm%?ia\t");
5539       break;
5540
5541     case 2:
5542       strcpy (buf, "ldm%?ib\t");
5543       break;
5544
5545     case 3:
5546       strcpy (buf, "ldm%?da\t");
5547       break;
5548
5549     case 4:
5550       strcpy (buf, "ldm%?db\t");
5551       break;
5552
5553     case 5:
5554       if (offset >= 0)
5555         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5556                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5557                  (long) offset);
5558       else
5559         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5560                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5561                  (long) -offset);
5562       output_asm_insn (buf, operands);
5563       base_reg = regs[0];
5564       strcpy (buf, "ldm%?ia\t");
5565       break;
5566
5567     default:
5568       abort ();
5569     }
5570
5571   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5572            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5573
5574   for (i = 1; i < nops; i++)
5575     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5576              reg_names[regs[i]]);
5577
5578   strcat (buf, "}\t%@ phole ldm");
5579
5580   output_asm_insn (buf, operands);
5581   return "";
5582 }
5583
5584 int
5585 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5586                          HOST_WIDE_INT * load_offset)
5587 {
5588   int unsorted_regs[4];
5589   HOST_WIDE_INT unsorted_offsets[4];
5590   int order[4];
5591   int base_reg = -1;
5592   int i;
5593
5594   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5595      extended if required.  */
5596   if (nops < 2 || nops > 4)
5597     abort ();
5598
5599   /* Loop over the operands and check that the memory references are
5600      suitable (ie immediate offsets from the same base register).  At
5601      the same time, extract the target register, and the memory
5602      offsets.  */
5603   for (i = 0; i < nops; i++)
5604     {
5605       rtx reg;
5606       rtx offset;
5607
5608       /* Convert a subreg of a mem into the mem itself.  */
5609       if (GET_CODE (operands[nops + i]) == SUBREG)
5610         operands[nops + i] = alter_subreg (operands + (nops + i));
5611
5612       if (GET_CODE (operands[nops + i]) != MEM)
5613         abort ();
5614
5615       /* Don't reorder volatile memory references; it doesn't seem worth
5616          looking for the case where the order is ok anyway.  */
5617       if (MEM_VOLATILE_P (operands[nops + i]))
5618         return 0;
5619
5620       offset = const0_rtx;
5621
5622       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5623            || (GET_CODE (reg) == SUBREG
5624                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5625           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5626               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5627                    == REG)
5628                   || (GET_CODE (reg) == SUBREG
5629                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5630               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5631                   == CONST_INT)))
5632         {
5633           if (i == 0)
5634             {
5635               base_reg = REGNO (reg);
5636               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5637                                   ? REGNO (operands[i])
5638                                   : REGNO (SUBREG_REG (operands[i])));
5639               order[0] = 0;
5640             }
5641           else 
5642             {
5643               if (base_reg != (int) REGNO (reg))
5644                 /* Not addressed from the same base register.  */
5645                 return 0;
5646
5647               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5648                                   ? REGNO (operands[i])
5649                                   : REGNO (SUBREG_REG (operands[i])));
5650               if (unsorted_regs[i] < unsorted_regs[order[0]])
5651                 order[0] = i;
5652             }
5653
5654           /* If it isn't an integer register, then we can't do this.  */
5655           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5656             return 0;
5657
5658           unsorted_offsets[i] = INTVAL (offset);
5659         }
5660       else
5661         /* Not a suitable memory address.  */
5662         return 0;
5663     }
5664
5665   /* All the useful information has now been extracted from the
5666      operands into unsorted_regs and unsorted_offsets; additionally,
5667      order[0] has been set to the lowest numbered register in the
5668      list.  Sort the registers into order, and check that the memory
5669      offsets are ascending and adjacent.  */
5670
5671   for (i = 1; i < nops; i++)
5672     {
5673       int j;
5674
5675       order[i] = order[i - 1];
5676       for (j = 0; j < nops; j++)
5677         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5678             && (order[i] == order[i - 1]
5679                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5680           order[i] = j;
5681
5682       /* Have we found a suitable register? if not, one must be used more
5683          than once.  */
5684       if (order[i] == order[i - 1])
5685         return 0;
5686
5687       /* Is the memory address adjacent and ascending? */
5688       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5689         return 0;
5690     }
5691
5692   if (base)
5693     {
5694       *base = base_reg;
5695
5696       for (i = 0; i < nops; i++)
5697         regs[i] = unsorted_regs[order[i]];
5698
5699       *load_offset = unsorted_offsets[order[0]];
5700     }
5701
5702   if (unsorted_offsets[order[0]] == 0)
5703     return 1; /* stmia */
5704
5705   if (unsorted_offsets[order[0]] == 4)
5706     return 2; /* stmib */
5707
5708   if (unsorted_offsets[order[nops - 1]] == 0)
5709     return 3; /* stmda */
5710
5711   if (unsorted_offsets[order[nops - 1]] == -4)
5712     return 4; /* stmdb */
5713
5714   return 0;
5715 }
5716
5717 const char *
5718 emit_stm_seq (rtx *operands, int nops)
5719 {
5720   int regs[4];
5721   int base_reg;
5722   HOST_WIDE_INT offset;
5723   char buf[100];
5724   int i;
5725
5726   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5727     {
5728     case 1:
5729       strcpy (buf, "stm%?ia\t");
5730       break;
5731
5732     case 2:
5733       strcpy (buf, "stm%?ib\t");
5734       break;
5735
5736     case 3:
5737       strcpy (buf, "stm%?da\t");
5738       break;
5739
5740     case 4:
5741       strcpy (buf, "stm%?db\t");
5742       break;
5743
5744     default:
5745       abort ();
5746     }
5747
5748   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5749            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5750
5751   for (i = 1; i < nops; i++)
5752     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5753              reg_names[regs[i]]);
5754
5755   strcat (buf, "}\t%@ phole stm");
5756
5757   output_asm_insn (buf, operands);
5758   return "";
5759 }
5760
5761 int
5762 multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5763 {
5764   if (GET_CODE (op) != PARALLEL
5765       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5766       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5767       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5768     return 0;
5769
5770   return 1;
5771 }
5772 \f
5773 /* Routines for use in generating RTL.  */
5774
5775 rtx
5776 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5777                        int write_back, int unchanging_p, int in_struct_p,
5778                        int scalar_p)
5779 {
5780   int i = 0, j;
5781   rtx result;
5782   int sign = up ? 1 : -1;
5783   rtx mem;
5784
5785   /* XScale has load-store double instructions, but they have stricter
5786      alignment requirements than load-store multiple, so we can not
5787      use them.
5788
5789      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5790      the pipeline until completion.
5791
5792         NREGS           CYCLES
5793           1               3
5794           2               4
5795           3               5
5796           4               6
5797
5798      An ldr instruction takes 1-3 cycles, but does not block the
5799      pipeline.
5800
5801         NREGS           CYCLES
5802           1              1-3
5803           2              2-6
5804           3              3-9
5805           4              4-12
5806
5807      Best case ldr will always win.  However, the more ldr instructions
5808      we issue, the less likely we are to be able to schedule them well.
5809      Using ldr instructions also increases code size.
5810
5811      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5812      for counts of 3 or 4 regs.  */
5813   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5814     {
5815       rtx seq;
5816       
5817       start_sequence ();
5818       
5819       for (i = 0; i < count; i++)
5820         {
5821           mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5822           RTX_UNCHANGING_P (mem) = unchanging_p;
5823           MEM_IN_STRUCT_P (mem) = in_struct_p;
5824           MEM_SCALAR_P (mem) = scalar_p;
5825           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5826         }
5827
5828       if (write_back)
5829         emit_move_insn (from, plus_constant (from, count * 4 * sign));
5830
5831       seq = get_insns ();
5832       end_sequence ();
5833       
5834       return seq;
5835     }
5836
5837   result = gen_rtx_PARALLEL (VOIDmode,
5838                              rtvec_alloc (count + (write_back ? 1 : 0)));
5839   if (write_back)
5840     {
5841       XVECEXP (result, 0, 0)
5842         = gen_rtx_SET (GET_MODE (from), from,
5843                        plus_constant (from, count * 4 * sign));
5844       i = 1;
5845       count++;
5846     }
5847
5848   for (j = 0; i < count; i++, j++)
5849     {
5850       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5851       RTX_UNCHANGING_P (mem) = unchanging_p;
5852       MEM_IN_STRUCT_P (mem) = in_struct_p;
5853       MEM_SCALAR_P (mem) = scalar_p;
5854       XVECEXP (result, 0, i)
5855         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5856     }
5857
5858   return result;
5859 }
5860
5861 rtx
5862 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5863                         int write_back, int unchanging_p, int in_struct_p,
5864                         int scalar_p)
5865 {
5866   int i = 0, j;
5867   rtx result;
5868   int sign = up ? 1 : -1;
5869   rtx mem;
5870
5871   /* See arm_gen_load_multiple for discussion of
5872      the pros/cons of ldm/stm usage for XScale.  */
5873   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5874     {
5875       rtx seq;
5876       
5877       start_sequence ();
5878       
5879       for (i = 0; i < count; i++)
5880         {
5881           mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5882           RTX_UNCHANGING_P (mem) = unchanging_p;
5883           MEM_IN_STRUCT_P (mem) = in_struct_p;
5884           MEM_SCALAR_P (mem) = scalar_p;
5885           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5886         }
5887
5888       if (write_back)
5889         emit_move_insn (to, plus_constant (to, count * 4 * sign));
5890
5891       seq = get_insns ();
5892       end_sequence ();
5893       
5894       return seq;
5895     }
5896
5897   result = gen_rtx_PARALLEL (VOIDmode,
5898                              rtvec_alloc (count + (write_back ? 1 : 0)));
5899   if (write_back)
5900     {
5901       XVECEXP (result, 0, 0)
5902         = gen_rtx_SET (GET_MODE (to), to,
5903                        plus_constant (to, count * 4 * sign));
5904       i = 1;
5905       count++;
5906     }
5907
5908   for (j = 0; i < count; i++, j++)
5909     {
5910       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5911       RTX_UNCHANGING_P (mem) = unchanging_p;
5912       MEM_IN_STRUCT_P (mem) = in_struct_p;
5913       MEM_SCALAR_P (mem) = scalar_p;
5914
5915       XVECEXP (result, 0, i)
5916         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5917     }
5918
5919   return result;
5920 }
5921
5922 int
5923 arm_gen_movstrqi (rtx *operands)
5924 {
5925   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5926   int i;
5927   rtx src, dst;
5928   rtx st_src, st_dst, fin_src, fin_dst;
5929   rtx part_bytes_reg = NULL;
5930   rtx mem;
5931   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5932   int dst_scalar_p, src_scalar_p;
5933
5934   if (GET_CODE (operands[2]) != CONST_INT
5935       || GET_CODE (operands[3]) != CONST_INT
5936       || INTVAL (operands[2]) > 64
5937       || INTVAL (operands[3]) & 3)
5938     return 0;
5939
5940   st_dst = XEXP (operands[0], 0);
5941   st_src = XEXP (operands[1], 0);
5942
5943   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5944   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5945   dst_scalar_p = MEM_SCALAR_P (operands[0]);
5946   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5947   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5948   src_scalar_p = MEM_SCALAR_P (operands[1]);
5949
5950   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5951   fin_src = src = copy_to_mode_reg (SImode, st_src);
5952
5953   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5954   out_words_to_go = INTVAL (operands[2]) / 4;
5955   last_bytes = INTVAL (operands[2]) & 3;
5956
5957   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5958     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5959
5960   for (i = 0; in_words_to_go >= 2; i+=4)
5961     {
5962       if (in_words_to_go > 4)
5963         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5964                                           src_unchanging_p,
5965                                           src_in_struct_p,
5966                                           src_scalar_p));
5967       else
5968         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
5969                                           FALSE, src_unchanging_p,
5970                                           src_in_struct_p, src_scalar_p));
5971
5972       if (out_words_to_go)
5973         {
5974           if (out_words_to_go > 4)
5975             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5976                                                dst_unchanging_p,
5977                                                dst_in_struct_p,
5978                                                dst_scalar_p));
5979           else if (out_words_to_go != 1)
5980             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5981                                                dst, TRUE, 
5982                                                (last_bytes == 0
5983                                                 ? FALSE : TRUE),
5984                                                dst_unchanging_p,
5985                                                dst_in_struct_p,
5986                                                dst_scalar_p));
5987           else
5988             {
5989               mem = gen_rtx_MEM (SImode, dst);
5990               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5991               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5992               MEM_SCALAR_P (mem) = dst_scalar_p;
5993               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5994               if (last_bytes != 0)
5995                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5996             }
5997         }
5998
5999       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6000       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6001     }
6002
6003   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6004   if (out_words_to_go)
6005     {
6006       rtx sreg;
6007       
6008       mem = gen_rtx_MEM (SImode, src);
6009       RTX_UNCHANGING_P (mem) = src_unchanging_p;
6010       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6011       MEM_SCALAR_P (mem) = src_scalar_p;
6012       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
6013       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
6014       
6015       mem = gen_rtx_MEM (SImode, dst);
6016       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6017       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6018       MEM_SCALAR_P (mem) = dst_scalar_p;
6019       emit_move_insn (mem, sreg);
6020       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6021       in_words_to_go--;
6022       
6023       if (in_words_to_go)       /* Sanity check */
6024         abort ();
6025     }
6026
6027   if (in_words_to_go)
6028     {
6029       if (in_words_to_go < 0)
6030         abort ();
6031
6032       mem = gen_rtx_MEM (SImode, src);
6033       RTX_UNCHANGING_P (mem) = src_unchanging_p;
6034       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6035       MEM_SCALAR_P (mem) = src_scalar_p;
6036       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6037     }
6038
6039   if (last_bytes && part_bytes_reg == NULL)
6040     abort ();
6041
6042   if (BYTES_BIG_ENDIAN && last_bytes)
6043     {
6044       rtx tmp = gen_reg_rtx (SImode);
6045
6046       /* The bytes we want are in the top end of the word.  */
6047       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6048                               GEN_INT (8 * (4 - last_bytes))));
6049       part_bytes_reg = tmp;
6050       
6051       while (last_bytes)
6052         {
6053           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
6054           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6055           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6056           MEM_SCALAR_P (mem) = dst_scalar_p;
6057           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6058
6059           if (--last_bytes)
6060             {
6061               tmp = gen_reg_rtx (SImode);
6062               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6063               part_bytes_reg = tmp;
6064             }
6065         }
6066           
6067     }
6068   else
6069     {
6070       if (last_bytes > 1)
6071         {
6072           mem = gen_rtx_MEM (HImode, dst);
6073           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6074           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6075           MEM_SCALAR_P (mem) = dst_scalar_p;
6076           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6077           last_bytes -= 2;
6078           if (last_bytes)
6079             {
6080               rtx tmp = gen_reg_rtx (SImode);
6081
6082               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6083               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6084               part_bytes_reg = tmp;
6085             }
6086         }
6087       
6088       if (last_bytes)
6089         {
6090           mem = gen_rtx_MEM (QImode, dst);
6091           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6092           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6093           MEM_SCALAR_P (mem) = dst_scalar_p;
6094           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6095         }
6096     }
6097
6098   return 1;
6099 }
6100
6101 /* Generate a memory reference for a half word, such that it will be loaded
6102    into the top 16 bits of the word.  We can assume that the address is
6103    known to be alignable and of the form reg, or plus (reg, const).  */
6104
6105 rtx
6106 arm_gen_rotated_half_load (rtx memref)
6107 {
6108   HOST_WIDE_INT offset = 0;
6109   rtx base = XEXP (memref, 0);
6110
6111   if (GET_CODE (base) == PLUS)
6112     {
6113       offset = INTVAL (XEXP (base, 1));
6114       base = XEXP (base, 0);
6115     }
6116
6117   /* If we aren't allowed to generate unaligned addresses, then fail.  */
6118   if (TARGET_MMU_TRAPS
6119       && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
6120     return NULL;
6121
6122   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6123
6124   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6125     return base;
6126
6127   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6128 }
6129
6130 /* Select a dominance comparison mode if possible for a test of the general
6131    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6132    COND_OR == DOM_CC_X_AND_Y => (X && Y) 
6133    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6134    COND_OR == DOM_CC_X_OR_Y => (X || Y) 
6135    In all cases OP will be either EQ or NE, but we don't need to know which
6136    here.  If we are unable to support a dominance comparison we return 
6137    CC mode.  This will then fail to match for the RTL expressions that
6138    generate this call.  */
6139 enum machine_mode
6140 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6141 {
6142   enum rtx_code cond1, cond2;
6143   int swapped = 0;
6144
6145   /* Currently we will probably get the wrong result if the individual
6146      comparisons are not simple.  This also ensures that it is safe to
6147      reverse a comparison if necessary.  */
6148   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6149        != CCmode)
6150       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6151           != CCmode))
6152     return CCmode;
6153
6154   /* The if_then_else variant of this tests the second condition if the
6155      first passes, but is true if the first fails.  Reverse the first
6156      condition to get a true "inclusive-or" expression.  */
6157   if (cond_or == DOM_CC_NX_OR_Y)
6158     cond1 = reverse_condition (cond1);
6159
6160   /* If the comparisons are not equal, and one doesn't dominate the other,
6161      then we can't do this.  */
6162   if (cond1 != cond2 
6163       && !comparison_dominates_p (cond1, cond2)
6164       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6165     return CCmode;
6166
6167   if (swapped)
6168     {
6169       enum rtx_code temp = cond1;
6170       cond1 = cond2;
6171       cond2 = temp;
6172     }
6173
6174   switch (cond1)
6175     {
6176     case EQ:
6177       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6178         return CC_DEQmode;
6179
6180       switch (cond2)
6181         {
6182         case LE: return CC_DLEmode;
6183         case LEU: return CC_DLEUmode;
6184         case GE: return CC_DGEmode;
6185         case GEU: return CC_DGEUmode;
6186         default: break;
6187         }
6188
6189       break;
6190
6191     case LT:
6192       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6193         return CC_DLTmode;
6194       if (cond2 == LE)
6195         return CC_DLEmode;
6196       if (cond2 == NE)
6197         return CC_DNEmode;
6198       break;
6199
6200     case GT:
6201       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6202         return CC_DGTmode;
6203       if (cond2 == GE)
6204         return CC_DGEmode;
6205       if (cond2 == NE)
6206         return CC_DNEmode;
6207       break;
6208       
6209     case LTU:
6210       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6211         return CC_DLTUmode;
6212       if (cond2 == LEU)
6213         return CC_DLEUmode;
6214       if (cond2 == NE)
6215         return CC_DNEmode;
6216       break;
6217
6218     case GTU:
6219       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6220         return CC_DGTUmode;
6221       if (cond2 == GEU)
6222         return CC_DGEUmode;
6223       if (cond2 == NE)
6224         return CC_DNEmode;
6225       break;
6226
6227     /* The remaining cases only occur when both comparisons are the
6228        same.  */
6229     case NE:
6230       return CC_DNEmode;
6231
6232     case LE:
6233       return CC_DLEmode;
6234
6235     case GE:
6236       return CC_DGEmode;
6237
6238     case LEU:
6239       return CC_DLEUmode;
6240
6241     case GEU:
6242       return CC_DGEUmode;
6243
6244     default:
6245       break;
6246     }
6247
6248   abort ();
6249 }
6250
6251 enum machine_mode
6252 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6253 {
6254   /* All floating point compares return CCFP if it is an equality
6255      comparison, and CCFPE otherwise.  */
6256   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6257     {
6258       switch (op)
6259         {
6260         case EQ:
6261         case NE:
6262         case UNORDERED:
6263         case ORDERED:
6264         case UNLT:
6265         case UNLE:
6266         case UNGT:
6267         case UNGE:
6268         case UNEQ:
6269         case LTGT:
6270           return CCFPmode;
6271
6272         case LT:
6273         case LE:
6274         case GT:
6275         case GE:
6276           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6277             return CCFPmode;
6278           return CCFPEmode;
6279
6280         default:
6281           abort ();
6282         }
6283     }
6284   
6285   /* A compare with a shifted operand.  Because of canonicalization, the
6286      comparison will have to be swapped when we emit the assembler.  */
6287   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6288       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6289           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6290           || GET_CODE (x) == ROTATERT))
6291     return CC_SWPmode;
6292
6293   /* This is a special case that is used by combine to allow a 
6294      comparison of a shifted byte load to be split into a zero-extend
6295      followed by a comparison of the shifted integer (only valid for
6296      equalities and unsigned inequalities).  */
6297   if (GET_MODE (x) == SImode
6298       && GET_CODE (x) == ASHIFT
6299       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6300       && GET_CODE (XEXP (x, 0)) == SUBREG
6301       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6302       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6303       && (op == EQ || op == NE
6304           || op == GEU || op == GTU || op == LTU || op == LEU)
6305       && GET_CODE (y) == CONST_INT)
6306     return CC_Zmode;
6307
6308   /* A construct for a conditional compare, if the false arm contains
6309      0, then both conditions must be true, otherwise either condition
6310      must be true.  Not all conditions are possible, so CCmode is
6311      returned if it can't be done.  */
6312   if (GET_CODE (x) == IF_THEN_ELSE
6313       && (XEXP (x, 2) == const0_rtx
6314           || XEXP (x, 2) == const1_rtx)
6315       && COMPARISON_P (XEXP (x, 0))
6316       && COMPARISON_P (XEXP (x, 1)))
6317     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
6318                                          INTVAL (XEXP (x, 2)));
6319
6320   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6321   if (GET_CODE (x) == AND
6322       && COMPARISON_P (XEXP (x, 0))
6323       && COMPARISON_P (XEXP (x, 1)))
6324     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6325                                          DOM_CC_X_AND_Y);
6326
6327   if (GET_CODE (x) == IOR
6328       && COMPARISON_P (XEXP (x, 0))
6329       && COMPARISON_P (XEXP (x, 1)))
6330     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6331                                          DOM_CC_X_OR_Y);
6332
6333   /* An operation (on Thumb) where we want to test for a single bit.
6334      This is done by shifting that bit up into the top bit of a
6335      scratch register; we can then branch on the sign bit.  */
6336   if (TARGET_THUMB
6337       && GET_MODE (x) == SImode
6338       && (op == EQ || op == NE)
6339       && (GET_CODE (x) == ZERO_EXTRACT))
6340     return CC_Nmode;
6341
6342   /* An operation that sets the condition codes as a side-effect, the
6343      V flag is not set correctly, so we can only use comparisons where
6344      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6345      instead.)  */
6346   if (GET_MODE (x) == SImode
6347       && y == const0_rtx
6348       && (op == EQ || op == NE || op == LT || op == GE)
6349       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6350           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6351           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6352           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6353           || GET_CODE (x) == LSHIFTRT
6354           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6355           || GET_CODE (x) == ROTATERT
6356           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6357     return CC_NOOVmode;
6358
6359   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6360     return CC_Zmode;
6361
6362   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6363       && GET_CODE (x) == PLUS
6364       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6365     return CC_Cmode;
6366
6367   return CCmode;
6368 }
6369
6370 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6371    return the rtx for register 0 in the proper mode.  FP means this is a
6372    floating point compare: I don't think that it is needed on the arm.  */
6373 rtx
6374 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6375 {
6376   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6377   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6378
6379   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6380                           gen_rtx_COMPARE (mode, x, y)));
6381
6382   return cc_reg;
6383 }
6384
6385 /* Generate a sequence of insns that will generate the correct return
6386    address mask depending on the physical architecture that the program
6387    is running on.  */
6388 rtx
6389 arm_gen_return_addr_mask (void)
6390 {
6391   rtx reg = gen_reg_rtx (Pmode);
6392
6393   emit_insn (gen_return_addr_mask (reg));
6394   return reg;
6395 }
6396
6397 void
6398 arm_reload_in_hi (rtx *operands)
6399 {
6400   rtx ref = operands[1];
6401   rtx base, scratch;
6402   HOST_WIDE_INT offset = 0;
6403
6404   if (GET_CODE (ref) == SUBREG)
6405     {
6406       offset = SUBREG_BYTE (ref);
6407       ref = SUBREG_REG (ref);
6408     }
6409
6410   if (GET_CODE (ref) == REG)
6411     {
6412       /* We have a pseudo which has been spilt onto the stack; there
6413          are two cases here: the first where there is a simple
6414          stack-slot replacement and a second where the stack-slot is
6415          out of range, or is used as a subreg.  */
6416       if (reg_equiv_mem[REGNO (ref)])
6417         {
6418           ref = reg_equiv_mem[REGNO (ref)];
6419           base = find_replacement (&XEXP (ref, 0));
6420         }
6421       else
6422         /* The slot is out of range, or was dressed up in a SUBREG.  */
6423         base = reg_equiv_address[REGNO (ref)];
6424     }
6425   else
6426     base = find_replacement (&XEXP (ref, 0));
6427
6428   /* Handle the case where the address is too complex to be offset by 1.  */
6429   if (GET_CODE (base) == MINUS
6430       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6431     {
6432       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6433
6434       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6435       base = base_plus;
6436     }
6437   else if (GET_CODE (base) == PLUS)
6438     {
6439       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6440       HOST_WIDE_INT hi, lo;
6441
6442       offset += INTVAL (XEXP (base, 1));
6443       base = XEXP (base, 0);
6444
6445       /* Rework the address into a legal sequence of insns.  */
6446       /* Valid range for lo is -4095 -> 4095 */
6447       lo = (offset >= 0
6448             ? (offset & 0xfff)
6449             : -((-offset) & 0xfff));
6450
6451       /* Corner case, if lo is the max offset then we would be out of range
6452          once we have added the additional 1 below, so bump the msb into the
6453          pre-loading insn(s).  */
6454       if (lo == 4095)
6455         lo &= 0x7ff;
6456
6457       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6458              ^ (HOST_WIDE_INT) 0x80000000)
6459             - (HOST_WIDE_INT) 0x80000000);
6460
6461       if (hi + lo != offset)
6462         abort ();
6463
6464       if (hi != 0)
6465         {
6466           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6467
6468           /* Get the base address; addsi3 knows how to handle constants
6469              that require more than one insn.  */
6470           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6471           base = base_plus;
6472           offset = lo;
6473         }
6474     }
6475
6476   /* Operands[2] may overlap operands[0] (though it won't overlap
6477      operands[1]), that's why we asked for a DImode reg -- so we can
6478      use the bit that does not overlap.  */
6479   if (REGNO (operands[2]) == REGNO (operands[0]))
6480     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6481   else
6482     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6483
6484   emit_insn (gen_zero_extendqisi2 (scratch,
6485                                    gen_rtx_MEM (QImode,
6486                                                 plus_constant (base,
6487                                                                offset))));
6488   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6489                                    gen_rtx_MEM (QImode, 
6490                                                 plus_constant (base,
6491                                                                offset + 1))));
6492   if (!BYTES_BIG_ENDIAN)
6493     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6494                         gen_rtx_IOR (SImode, 
6495                                      gen_rtx_ASHIFT
6496                                      (SImode,
6497                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6498                                       GEN_INT (8)),
6499                                      scratch)));
6500   else
6501     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6502                             gen_rtx_IOR (SImode, 
6503                                          gen_rtx_ASHIFT (SImode, scratch,
6504                                                          GEN_INT (8)),
6505                                          gen_rtx_SUBREG (SImode, operands[0],
6506                                                          0))));
6507 }
6508
6509 /* Handle storing a half-word to memory during reload by synthesizing as two
6510    byte stores.  Take care not to clobber the input values until after we
6511    have moved them somewhere safe.  This code assumes that if the DImode
6512    scratch in operands[2] overlaps either the input value or output address
6513    in some way, then that value must die in this insn (we absolutely need
6514    two scratch registers for some corner cases).  */
6515 void
6516 arm_reload_out_hi (rtx *operands)
6517 {
6518   rtx ref = operands[0];
6519   rtx outval = operands[1];
6520   rtx base, scratch;
6521   HOST_WIDE_INT offset = 0;
6522
6523   if (GET_CODE (ref) == SUBREG)
6524     {
6525       offset = SUBREG_BYTE (ref);
6526       ref = SUBREG_REG (ref);
6527     }
6528
6529   if (GET_CODE (ref) == REG)
6530     {
6531       /* We have a pseudo which has been spilt onto the stack; there
6532          are two cases here: the first where there is a simple
6533          stack-slot replacement and a second where the stack-slot is
6534          out of range, or is used as a subreg.  */
6535       if (reg_equiv_mem[REGNO (ref)])
6536         {
6537           ref = reg_equiv_mem[REGNO (ref)];
6538           base = find_replacement (&XEXP (ref, 0));
6539         }
6540       else
6541         /* The slot is out of range, or was dressed up in a SUBREG.  */
6542         base = reg_equiv_address[REGNO (ref)];
6543     }
6544   else
6545     base = find_replacement (&XEXP (ref, 0));
6546
6547   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6548
6549   /* Handle the case where the address is too complex to be offset by 1.  */
6550   if (GET_CODE (base) == MINUS
6551       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6552     {
6553       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6554
6555       /* Be careful not to destroy OUTVAL.  */
6556       if (reg_overlap_mentioned_p (base_plus, outval))
6557         {
6558           /* Updating base_plus might destroy outval, see if we can
6559              swap the scratch and base_plus.  */
6560           if (!reg_overlap_mentioned_p (scratch, outval))
6561             {
6562               rtx tmp = scratch;
6563               scratch = base_plus;
6564               base_plus = tmp;
6565             }
6566           else
6567             {
6568               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6569
6570               /* Be conservative and copy OUTVAL into the scratch now,
6571                  this should only be necessary if outval is a subreg
6572                  of something larger than a word.  */
6573               /* XXX Might this clobber base?  I can't see how it can,
6574                  since scratch is known to overlap with OUTVAL, and
6575                  must be wider than a word.  */
6576               emit_insn (gen_movhi (scratch_hi, outval));
6577               outval = scratch_hi;
6578             }
6579         }
6580
6581       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6582       base = base_plus;
6583     }
6584   else if (GET_CODE (base) == PLUS)
6585     {
6586       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6587       HOST_WIDE_INT hi, lo;
6588
6589       offset += INTVAL (XEXP (base, 1));
6590       base = XEXP (base, 0);
6591
6592       /* Rework the address into a legal sequence of insns.  */
6593       /* Valid range for lo is -4095 -> 4095 */
6594       lo = (offset >= 0
6595             ? (offset & 0xfff)
6596             : -((-offset) & 0xfff));
6597
6598       /* Corner case, if lo is the max offset then we would be out of range
6599          once we have added the additional 1 below, so bump the msb into the
6600          pre-loading insn(s).  */
6601       if (lo == 4095)
6602         lo &= 0x7ff;
6603
6604       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6605              ^ (HOST_WIDE_INT) 0x80000000)
6606             - (HOST_WIDE_INT) 0x80000000);
6607
6608       if (hi + lo != offset)
6609         abort ();
6610
6611       if (hi != 0)
6612         {
6613           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6614
6615           /* Be careful not to destroy OUTVAL.  */
6616           if (reg_overlap_mentioned_p (base_plus, outval))
6617             {
6618               /* Updating base_plus might destroy outval, see if we
6619                  can swap the scratch and base_plus.  */
6620               if (!reg_overlap_mentioned_p (scratch, outval))
6621                 {
6622                   rtx tmp = scratch;
6623                   scratch = base_plus;
6624                   base_plus = tmp;
6625                 }
6626               else
6627                 {
6628                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6629
6630                   /* Be conservative and copy outval into scratch now,
6631                      this should only be necessary if outval is a
6632                      subreg of something larger than a word.  */
6633                   /* XXX Might this clobber base?  I can't see how it
6634                      can, since scratch is known to overlap with
6635                      outval.  */
6636                   emit_insn (gen_movhi (scratch_hi, outval));
6637                   outval = scratch_hi;
6638                 }
6639             }
6640
6641           /* Get the base address; addsi3 knows how to handle constants
6642              that require more than one insn.  */
6643           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6644           base = base_plus;
6645           offset = lo;
6646         }
6647     }
6648
6649   if (BYTES_BIG_ENDIAN)
6650     {
6651       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
6652                                          plus_constant (base, offset + 1)),
6653                             gen_lowpart (QImode, outval)));
6654       emit_insn (gen_lshrsi3 (scratch,
6655                               gen_rtx_SUBREG (SImode, outval, 0),
6656                               GEN_INT (8)));
6657       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6658                             gen_lowpart (QImode, scratch)));
6659     }
6660   else
6661     {
6662       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6663                             gen_lowpart (QImode, outval)));
6664       emit_insn (gen_lshrsi3 (scratch,
6665                               gen_rtx_SUBREG (SImode, outval, 0),
6666                               GEN_INT (8)));
6667       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6668                                          plus_constant (base, offset + 1)),
6669                             gen_lowpart (QImode, scratch)));
6670     }
6671 }
6672 \f
6673 /* Print a symbolic form of X to the debug file, F.  */
6674 static void
6675 arm_print_value (FILE *f, rtx x)
6676 {
6677   switch (GET_CODE (x))
6678     {
6679     case CONST_INT:
6680       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6681       return;
6682
6683     case CONST_DOUBLE:
6684       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6685       return;
6686
6687     case CONST_VECTOR:
6688       {
6689         int i;
6690
6691         fprintf (f, "<");
6692         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6693           {
6694             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6695             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6696               fputc (',', f);
6697           }
6698         fprintf (f, ">");
6699       }
6700       return;
6701
6702     case CONST_STRING:
6703       fprintf (f, "\"%s\"", XSTR (x, 0));
6704       return;
6705
6706     case SYMBOL_REF:
6707       fprintf (f, "`%s'", XSTR (x, 0));
6708       return;
6709
6710     case LABEL_REF:
6711       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6712       return;
6713
6714     case CONST:
6715       arm_print_value (f, XEXP (x, 0));
6716       return;
6717
6718     case PLUS:
6719       arm_print_value (f, XEXP (x, 0));
6720       fprintf (f, "+");
6721       arm_print_value (f, XEXP (x, 1));
6722       return;
6723
6724     case PC:
6725       fprintf (f, "pc");
6726       return;
6727
6728     default:
6729       fprintf (f, "????");
6730       return;
6731     }
6732 }
6733 \f
6734 /* Routines for manipulation of the constant pool.  */
6735
6736 /* Arm instructions cannot load a large constant directly into a
6737    register; they have to come from a pc relative load.  The constant
6738    must therefore be placed in the addressable range of the pc
6739    relative load.  Depending on the precise pc relative load
6740    instruction the range is somewhere between 256 bytes and 4k.  This
6741    means that we often have to dump a constant inside a function, and
6742    generate code to branch around it.
6743
6744    It is important to minimize this, since the branches will slow
6745    things down and make the code larger.
6746
6747    Normally we can hide the table after an existing unconditional
6748    branch so that there is no interruption of the flow, but in the
6749    worst case the code looks like this:
6750
6751         ldr     rn, L1
6752         ...
6753         b       L2
6754         align
6755         L1:     .long value
6756         L2:
6757         ...
6758
6759         ldr     rn, L3
6760         ...
6761         b       L4
6762         align
6763         L3:     .long value
6764         L4:
6765         ...
6766
6767    We fix this by performing a scan after scheduling, which notices
6768    which instructions need to have their operands fetched from the
6769    constant table and builds the table.
6770
6771    The algorithm starts by building a table of all the constants that
6772    need fixing up and all the natural barriers in the function (places
6773    where a constant table can be dropped without breaking the flow).
6774    For each fixup we note how far the pc-relative replacement will be
6775    able to reach and the offset of the instruction into the function.
6776
6777    Having built the table we then group the fixes together to form
6778    tables that are as large as possible (subject to addressing
6779    constraints) and emit each table of constants after the last
6780    barrier that is within range of all the instructions in the group.
6781    If a group does not contain a barrier, then we forcibly create one
6782    by inserting a jump instruction into the flow.  Once the table has
6783    been inserted, the insns are then modified to reference the
6784    relevant entry in the pool.
6785
6786    Possible enhancements to the algorithm (not implemented) are:
6787
6788    1) For some processors and object formats, there may be benefit in
6789    aligning the pools to the start of cache lines; this alignment
6790    would need to be taken into account when calculating addressability
6791    of a pool.  */
6792
6793 /* These typedefs are located at the start of this file, so that
6794    they can be used in the prototypes there.  This comment is to
6795    remind readers of that fact so that the following structures
6796    can be understood more easily.
6797
6798      typedef struct minipool_node    Mnode;
6799      typedef struct minipool_fixup   Mfix;  */
6800
6801 struct minipool_node
6802 {
6803   /* Doubly linked chain of entries.  */
6804   Mnode * next;
6805   Mnode * prev;
6806   /* The maximum offset into the code that this entry can be placed.  While
6807      pushing fixes for forward references, all entries are sorted in order
6808      of increasing max_address.  */
6809   HOST_WIDE_INT max_address;
6810   /* Similarly for an entry inserted for a backwards ref.  */
6811   HOST_WIDE_INT min_address;
6812   /* The number of fixes referencing this entry.  This can become zero
6813      if we "unpush" an entry.  In this case we ignore the entry when we
6814      come to emit the code.  */
6815   int refcount;
6816   /* The offset from the start of the minipool.  */
6817   HOST_WIDE_INT offset;
6818   /* The value in table.  */
6819   rtx value;
6820   /* The mode of value.  */
6821   enum machine_mode mode;
6822   /* The size of the value.  With iWMMXt enabled
6823      sizes > 4 also imply an alignment of 8-bytes.  */
6824   int fix_size;
6825 };
6826
6827 struct minipool_fixup
6828 {
6829   Mfix *            next;
6830   rtx               insn;
6831   HOST_WIDE_INT     address;
6832   rtx *             loc;
6833   enum machine_mode mode;
6834   int               fix_size;
6835   rtx               value;
6836   Mnode *           minipool;
6837   HOST_WIDE_INT     forwards;
6838   HOST_WIDE_INT     backwards;
6839 };
6840
6841 /* Fixes less than a word need padding out to a word boundary.  */
6842 #define MINIPOOL_FIX_SIZE(mode) \
6843   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6844
6845 static Mnode *  minipool_vector_head;
6846 static Mnode *  minipool_vector_tail;
6847 static rtx      minipool_vector_label;
6848
6849 /* The linked list of all minipool fixes required for this function.  */
6850 Mfix *          minipool_fix_head;
6851 Mfix *          minipool_fix_tail;
6852 /* The fix entry for the current minipool, once it has been placed.  */
6853 Mfix *          minipool_barrier;
6854
6855 /* Determines if INSN is the start of a jump table.  Returns the end
6856    of the TABLE or NULL_RTX.  */
6857 static rtx
6858 is_jump_table (rtx insn)
6859 {
6860   rtx table;
6861   
6862   if (GET_CODE (insn) == JUMP_INSN
6863       && JUMP_LABEL (insn) != NULL
6864       && ((table = next_real_insn (JUMP_LABEL (insn)))
6865           == next_real_insn (insn))
6866       && table != NULL
6867       && GET_CODE (table) == JUMP_INSN
6868       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6869           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6870     return table;
6871
6872   return NULL_RTX;
6873 }
6874
6875 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6876 #define JUMP_TABLES_IN_TEXT_SECTION 0
6877 #endif
6878
6879 static HOST_WIDE_INT
6880 get_jump_table_size (rtx insn)
6881 {
6882   /* ADDR_VECs only take room if read-only data does into the text
6883      section.  */
6884   if (JUMP_TABLES_IN_TEXT_SECTION
6885 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6886       || 1
6887 #endif
6888       )
6889     {
6890       rtx body = PATTERN (insn);
6891       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6892
6893       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6894     }
6895
6896   return 0;
6897 }
6898
6899 /* Move a minipool fix MP from its current location to before MAX_MP.
6900    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6901    constraints may need updating.  */
6902 static Mnode *
6903 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6904                                HOST_WIDE_INT max_address)
6905 {
6906   /* This should never be true and the code below assumes these are
6907      different.  */
6908   if (mp == max_mp)
6909     abort ();
6910
6911   if (max_mp == NULL)
6912     {
6913       if (max_address < mp->max_address)
6914         mp->max_address = max_address;
6915     }
6916   else
6917     {
6918       if (max_address > max_mp->max_address - mp->fix_size)
6919         mp->max_address = max_mp->max_address - mp->fix_size;
6920       else
6921         mp->max_address = max_address;
6922
6923       /* Unlink MP from its current position.  Since max_mp is non-null,
6924        mp->prev must be non-null.  */
6925       mp->prev->next = mp->next;
6926       if (mp->next != NULL)
6927         mp->next->prev = mp->prev;
6928       else
6929         minipool_vector_tail = mp->prev;
6930
6931       /* Re-insert it before MAX_MP.  */
6932       mp->next = max_mp;
6933       mp->prev = max_mp->prev;
6934       max_mp->prev = mp;
6935       
6936       if (mp->prev != NULL)
6937         mp->prev->next = mp;
6938       else
6939         minipool_vector_head = mp;
6940     }
6941
6942   /* Save the new entry.  */
6943   max_mp = mp;
6944
6945   /* Scan over the preceding entries and adjust their addresses as
6946      required.  */
6947   while (mp->prev != NULL
6948          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6949     {
6950       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6951       mp = mp->prev;
6952     }
6953
6954   return max_mp;
6955 }
6956
6957 /* Add a constant to the minipool for a forward reference.  Returns the
6958    node added or NULL if the constant will not fit in this pool.  */
6959 static Mnode *
6960 add_minipool_forward_ref (Mfix *fix)
6961 {
6962   /* If set, max_mp is the first pool_entry that has a lower
6963      constraint than the one we are trying to add.  */
6964   Mnode *       max_mp = NULL;
6965   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6966   Mnode *       mp;
6967   
6968   /* If this fix's address is greater than the address of the first
6969      entry, then we can't put the fix in this pool.  We subtract the
6970      size of the current fix to ensure that if the table is fully
6971      packed we still have enough room to insert this value by suffling
6972      the other fixes forwards.  */
6973   if (minipool_vector_head &&
6974       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6975     return NULL;
6976
6977   /* Scan the pool to see if a constant with the same value has
6978      already been added.  While we are doing this, also note the
6979      location where we must insert the constant if it doesn't already
6980      exist.  */
6981   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6982     {
6983       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6984           && fix->mode == mp->mode
6985           && (GET_CODE (fix->value) != CODE_LABEL
6986               || (CODE_LABEL_NUMBER (fix->value)
6987                   == CODE_LABEL_NUMBER (mp->value)))
6988           && rtx_equal_p (fix->value, mp->value))
6989         {
6990           /* More than one fix references this entry.  */
6991           mp->refcount++;
6992           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6993         }
6994
6995       /* Note the insertion point if necessary.  */
6996       if (max_mp == NULL
6997           && mp->max_address > max_address)
6998         max_mp = mp;
6999
7000       /* If we are inserting an 8-bytes aligned quantity and
7001          we have not already found an insertion point, then
7002          make sure that all such 8-byte aligned quantities are
7003          placed at the start of the pool.  */
7004       if (ARM_DOUBLEWORD_ALIGN
7005           && max_mp == NULL
7006           && fix->fix_size == 8
7007           && mp->fix_size != 8)
7008         {
7009           max_mp = mp;
7010           max_address = mp->max_address;
7011         }
7012     }
7013
7014   /* The value is not currently in the minipool, so we need to create
7015      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7016      the end of the list since the placement is less constrained than
7017      any existing entry.  Otherwise, we insert the new fix before
7018      MAX_MP and, if necessary, adjust the constraints on the other
7019      entries.  */
7020   mp = xmalloc (sizeof (* mp));
7021   mp->fix_size = fix->fix_size;
7022   mp->mode = fix->mode;
7023   mp->value = fix->value;
7024   mp->refcount = 1;
7025   /* Not yet required for a backwards ref.  */
7026   mp->min_address = -65536;
7027
7028   if (max_mp == NULL)
7029     {
7030       mp->max_address = max_address;
7031       mp->next = NULL;
7032       mp->prev = minipool_vector_tail;
7033
7034       if (mp->prev == NULL)
7035         {
7036           minipool_vector_head = mp;
7037           minipool_vector_label = gen_label_rtx ();
7038         }
7039       else
7040         mp->prev->next = mp;
7041
7042       minipool_vector_tail = mp;
7043     }
7044   else
7045     {
7046       if (max_address > max_mp->max_address - mp->fix_size)
7047         mp->max_address = max_mp->max_address - mp->fix_size;
7048       else
7049         mp->max_address = max_address;
7050
7051       mp->next = max_mp;
7052       mp->prev = max_mp->prev;
7053       max_mp->prev = mp;
7054       if (mp->prev != NULL)
7055         mp->prev->next = mp;
7056       else
7057         minipool_vector_head = mp;
7058     }
7059
7060   /* Save the new entry.  */
7061   max_mp = mp;
7062
7063   /* Scan over the preceding entries and adjust their addresses as
7064      required.  */
7065   while (mp->prev != NULL
7066          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7067     {
7068       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7069       mp = mp->prev;
7070     }
7071
7072   return max_mp;
7073 }
7074
7075 static Mnode *
7076 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7077                                 HOST_WIDE_INT  min_address)
7078 {
7079   HOST_WIDE_INT offset;
7080
7081   /* This should never be true, and the code below assumes these are
7082      different.  */
7083   if (mp == min_mp)
7084     abort ();
7085
7086   if (min_mp == NULL)
7087     {
7088       if (min_address > mp->min_address)
7089         mp->min_address = min_address;
7090     }
7091   else
7092     {
7093       /* We will adjust this below if it is too loose.  */
7094       mp->min_address = min_address;
7095
7096       /* Unlink MP from its current position.  Since min_mp is non-null,
7097          mp->next must be non-null.  */
7098       mp->next->prev = mp->prev;
7099       if (mp->prev != NULL)
7100         mp->prev->next = mp->next;
7101       else
7102         minipool_vector_head = mp->next;
7103
7104       /* Reinsert it after MIN_MP.  */
7105       mp->prev = min_mp;
7106       mp->next = min_mp->next;
7107       min_mp->next = mp;
7108       if (mp->next != NULL)
7109         mp->next->prev = mp;
7110       else
7111         minipool_vector_tail = mp;
7112     }
7113
7114   min_mp = mp;
7115
7116   offset = 0;
7117   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7118     {
7119       mp->offset = offset;
7120       if (mp->refcount > 0)
7121         offset += mp->fix_size;
7122
7123       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7124         mp->next->min_address = mp->min_address + mp->fix_size;
7125     }
7126
7127   return min_mp;
7128 }      
7129
7130 /* Add a constant to the minipool for a backward reference.  Returns the
7131    node added or NULL if the constant will not fit in this pool.  
7132
7133    Note that the code for insertion for a backwards reference can be
7134    somewhat confusing because the calculated offsets for each fix do
7135    not take into account the size of the pool (which is still under
7136    construction.  */
7137 static Mnode *
7138 add_minipool_backward_ref (Mfix *fix)
7139 {
7140   /* If set, min_mp is the last pool_entry that has a lower constraint
7141      than the one we are trying to add.  */
7142   Mnode *min_mp = NULL;
7143   /* This can be negative, since it is only a constraint.  */
7144   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7145   Mnode *mp;
7146
7147   /* If we can't reach the current pool from this insn, or if we can't
7148      insert this entry at the end of the pool without pushing other
7149      fixes out of range, then we don't try.  This ensures that we
7150      can't fail later on.  */
7151   if (min_address >= minipool_barrier->address
7152       || (minipool_vector_tail->min_address + fix->fix_size
7153           >= minipool_barrier->address))
7154     return NULL;
7155
7156   /* Scan the pool to see if a constant with the same value has
7157      already been added.  While we are doing this, also note the
7158      location where we must insert the constant if it doesn't already
7159      exist.  */
7160   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7161     {
7162       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7163           && fix->mode == mp->mode
7164           && (GET_CODE (fix->value) != CODE_LABEL
7165               || (CODE_LABEL_NUMBER (fix->value)
7166                   == CODE_LABEL_NUMBER (mp->value)))
7167           && rtx_equal_p (fix->value, mp->value)
7168           /* Check that there is enough slack to move this entry to the
7169              end of the table (this is conservative).  */
7170           && (mp->max_address 
7171               > (minipool_barrier->address 
7172                  + minipool_vector_tail->offset
7173                  + minipool_vector_tail->fix_size)))
7174         {
7175           mp->refcount++;
7176           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7177         }
7178
7179       if (min_mp != NULL)
7180         mp->min_address += fix->fix_size;
7181       else
7182         {
7183           /* Note the insertion point if necessary.  */
7184           if (mp->min_address < min_address)
7185             {
7186               /* For now, we do not allow the insertion of 8-byte alignment
7187                  requiring nodes anywhere but at the start of the pool.  */
7188               if (ARM_DOUBLEWORD_ALIGN
7189                   && fix->fix_size == 8 && mp->fix_size != 8)
7190                 return NULL;
7191               else
7192                 min_mp = mp;
7193             }
7194           else if (mp->max_address
7195                    < minipool_barrier->address + mp->offset + fix->fix_size)
7196             {
7197               /* Inserting before this entry would push the fix beyond
7198                  its maximum address (which can happen if we have
7199                  re-located a forwards fix); force the new fix to come
7200                  after it.  */
7201               min_mp = mp;
7202               min_address = mp->min_address + fix->fix_size;
7203             }
7204           /* If we are inserting an 8-bytes aligned quantity and
7205              we have not already found an insertion point, then
7206              make sure that all such 8-byte aligned quantities are
7207              placed at the start of the pool.  */
7208           else if (ARM_DOUBLEWORD_ALIGN
7209                    && min_mp == NULL
7210                    && fix->fix_size == 8
7211                    && mp->fix_size < 8)
7212             {
7213               min_mp = mp;
7214               min_address = mp->min_address + fix->fix_size;
7215             }
7216         }
7217     }
7218
7219   /* We need to create a new entry.  */
7220   mp = xmalloc (sizeof (* mp));
7221   mp->fix_size = fix->fix_size;
7222   mp->mode = fix->mode;
7223   mp->value = fix->value;
7224   mp->refcount = 1;
7225   mp->max_address = minipool_barrier->address + 65536;
7226
7227   mp->min_address = min_address;
7228
7229   if (min_mp == NULL)
7230     {
7231       mp->prev = NULL;
7232       mp->next = minipool_vector_head;
7233
7234       if (mp->next == NULL)
7235         {
7236           minipool_vector_tail = mp;
7237           minipool_vector_label = gen_label_rtx ();
7238         }
7239       else
7240         mp->next->prev = mp;
7241
7242       minipool_vector_head = mp;
7243     }
7244   else
7245     {
7246       mp->next = min_mp->next;
7247       mp->prev = min_mp;
7248       min_mp->next = mp;
7249       
7250       if (mp->next != NULL)
7251         mp->next->prev = mp;
7252       else
7253         minipool_vector_tail = mp;
7254     }
7255
7256   /* Save the new entry.  */
7257   min_mp = mp;
7258
7259   if (mp->prev)
7260     mp = mp->prev;
7261   else
7262     mp->offset = 0;
7263
7264   /* Scan over the following entries and adjust their offsets.  */
7265   while (mp->next != NULL)
7266     {
7267       if (mp->next->min_address < mp->min_address + mp->fix_size)
7268         mp->next->min_address = mp->min_address + mp->fix_size;
7269
7270       if (mp->refcount)
7271         mp->next->offset = mp->offset + mp->fix_size;
7272       else
7273         mp->next->offset = mp->offset;
7274
7275       mp = mp->next;
7276     }
7277
7278   return min_mp;
7279 }
7280
7281 static void
7282 assign_minipool_offsets (Mfix *barrier)
7283 {
7284   HOST_WIDE_INT offset = 0;
7285   Mnode *mp;
7286
7287   minipool_barrier = barrier;
7288
7289   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7290     {
7291       mp->offset = offset;
7292       
7293       if (mp->refcount > 0)
7294         offset += mp->fix_size;
7295     }
7296 }
7297
7298 /* Output the literal table */
7299 static void
7300 dump_minipool (rtx scan)
7301 {
7302   Mnode * mp;
7303   Mnode * nmp;
7304   int align64 = 0;
7305
7306   if (ARM_DOUBLEWORD_ALIGN)
7307     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7308       if (mp->refcount > 0 && mp->fix_size == 8)
7309         {
7310           align64 = 1;
7311           break;
7312         }
7313
7314   if (dump_file)
7315     fprintf (dump_file,
7316              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7317              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7318
7319   scan = emit_label_after (gen_label_rtx (), scan);
7320   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7321   scan = emit_label_after (minipool_vector_label, scan);
7322
7323   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7324     {
7325       if (mp->refcount > 0)
7326         {
7327           if (dump_file)
7328             {
7329               fprintf (dump_file, 
7330                        ";;  Offset %u, min %ld, max %ld ",
7331                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7332                        (unsigned long) mp->max_address);
7333               arm_print_value (dump_file, mp->value);
7334               fputc ('\n', dump_file);
7335             }
7336
7337           switch (mp->fix_size)
7338             {
7339 #ifdef HAVE_consttable_1
7340             case 1:
7341               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7342               break;
7343
7344 #endif
7345 #ifdef HAVE_consttable_2
7346             case 2:
7347               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7348               break;
7349
7350 #endif
7351 #ifdef HAVE_consttable_4
7352             case 4:
7353               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7354               break;
7355
7356 #endif
7357 #ifdef HAVE_consttable_8
7358             case 8:
7359               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7360               break;
7361
7362 #endif
7363             default:
7364               abort ();
7365               break;
7366             }
7367         }
7368
7369       nmp = mp->next;
7370       free (mp);
7371     }
7372
7373   minipool_vector_head = minipool_vector_tail = NULL;
7374   scan = emit_insn_after (gen_consttable_end (), scan);
7375   scan = emit_barrier_after (scan);
7376 }
7377
7378 /* Return the cost of forcibly inserting a barrier after INSN.  */
7379 static int
7380 arm_barrier_cost (rtx insn)
7381 {
7382   /* Basing the location of the pool on the loop depth is preferable,
7383      but at the moment, the basic block information seems to be
7384      corrupt by this stage of the compilation.  */
7385   int base_cost = 50;
7386   rtx next = next_nonnote_insn (insn);
7387
7388   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7389     base_cost -= 20;
7390
7391   switch (GET_CODE (insn))
7392     {
7393     case CODE_LABEL:
7394       /* It will always be better to place the table before the label, rather
7395          than after it.  */
7396       return 50;  
7397
7398     case INSN:
7399     case CALL_INSN:
7400       return base_cost;
7401
7402     case JUMP_INSN:
7403       return base_cost - 10;
7404
7405     default:
7406       return base_cost + 10;
7407     }
7408 }
7409
7410 /* Find the best place in the insn stream in the range
7411    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7412    Create the barrier by inserting a jump and add a new fix entry for
7413    it.  */
7414 static Mfix *
7415 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7416 {
7417   HOST_WIDE_INT count = 0;
7418   rtx barrier;
7419   rtx from = fix->insn;
7420   rtx selected = from;
7421   int selected_cost;
7422   HOST_WIDE_INT selected_address;
7423   Mfix * new_fix;
7424   HOST_WIDE_INT max_count = max_address - fix->address;
7425   rtx label = gen_label_rtx ();
7426
7427   selected_cost = arm_barrier_cost (from);
7428   selected_address = fix->address;
7429
7430   while (from && count < max_count)
7431     {
7432       rtx tmp;
7433       int new_cost;
7434
7435       /* This code shouldn't have been called if there was a natural barrier
7436          within range.  */
7437       if (GET_CODE (from) == BARRIER)
7438         abort ();
7439
7440       /* Count the length of this insn.  */
7441       count += get_attr_length (from);
7442
7443       /* If there is a jump table, add its length.  */
7444       tmp = is_jump_table (from);
7445       if (tmp != NULL)
7446         {
7447           count += get_jump_table_size (tmp);
7448
7449           /* Jump tables aren't in a basic block, so base the cost on
7450              the dispatch insn.  If we select this location, we will
7451              still put the pool after the table.  */
7452           new_cost = arm_barrier_cost (from);
7453
7454           if (count < max_count && new_cost <= selected_cost)
7455             {
7456               selected = tmp;
7457               selected_cost = new_cost;
7458               selected_address = fix->address + count;
7459             }
7460
7461           /* Continue after the dispatch table.  */
7462           from = NEXT_INSN (tmp);
7463           continue;
7464         }
7465
7466       new_cost = arm_barrier_cost (from);
7467       
7468       if (count < max_count && new_cost <= selected_cost)
7469         {
7470           selected = from;
7471           selected_cost = new_cost;
7472           selected_address = fix->address + count;
7473         }
7474
7475       from = NEXT_INSN (from);
7476     }
7477
7478   /* Create a new JUMP_INSN that branches around a barrier.  */
7479   from = emit_jump_insn_after (gen_jump (label), selected);
7480   JUMP_LABEL (from) = label;
7481   barrier = emit_barrier_after (from);
7482   emit_label_after (label, barrier);
7483
7484   /* Create a minipool barrier entry for the new barrier.  */
7485   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7486   new_fix->insn = barrier;
7487   new_fix->address = selected_address;
7488   new_fix->next = fix->next;
7489   fix->next = new_fix;
7490
7491   return new_fix;
7492 }
7493
7494 /* Record that there is a natural barrier in the insn stream at
7495    ADDRESS.  */
7496 static void
7497 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7498 {
7499   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7500
7501   fix->insn = insn;
7502   fix->address = address;
7503
7504   fix->next = NULL;
7505   if (minipool_fix_head != NULL)
7506     minipool_fix_tail->next = fix;
7507   else
7508     minipool_fix_head = fix;
7509
7510   minipool_fix_tail = fix;
7511 }
7512
7513 /* Record INSN, which will need fixing up to load a value from the
7514    minipool.  ADDRESS is the offset of the insn since the start of the
7515    function; LOC is a pointer to the part of the insn which requires
7516    fixing; VALUE is the constant that must be loaded, which is of type
7517    MODE.  */
7518 static void
7519 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7520                    enum machine_mode mode, rtx value)
7521 {
7522   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7523
7524 #ifdef AOF_ASSEMBLER
7525   /* PIC symbol references need to be converted into offsets into the
7526      based area.  */
7527   /* XXX This shouldn't be done here.  */
7528   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7529     value = aof_pic_entry (value);
7530 #endif /* AOF_ASSEMBLER */
7531
7532   fix->insn = insn;
7533   fix->address = address;
7534   fix->loc = loc;
7535   fix->mode = mode;
7536   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7537   fix->value = value;
7538   fix->forwards = get_attr_pool_range (insn);
7539   fix->backwards = get_attr_neg_pool_range (insn);
7540   fix->minipool = NULL;
7541
7542   /* If an insn doesn't have a range defined for it, then it isn't
7543      expecting to be reworked by this code.  Better to abort now than
7544      to generate duff assembly code.  */
7545   if (fix->forwards == 0 && fix->backwards == 0)
7546     abort ();
7547
7548   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7549      So there might be an empty word before the start of the pool.
7550      Hence we reduce the forward range by 4 to allow for this
7551      possibility.  */
7552   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7553     fix->forwards -= 4;
7554
7555   if (dump_file)
7556     {
7557       fprintf (dump_file,
7558                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7559                GET_MODE_NAME (mode),
7560                INSN_UID (insn), (unsigned long) address, 
7561                -1 * (long)fix->backwards, (long)fix->forwards);
7562       arm_print_value (dump_file, fix->value);
7563       fprintf (dump_file, "\n");
7564     }
7565
7566   /* Add it to the chain of fixes.  */
7567   fix->next = NULL;
7568   
7569   if (minipool_fix_head != NULL)
7570     minipool_fix_tail->next = fix;
7571   else
7572     minipool_fix_head = fix;
7573
7574   minipool_fix_tail = fix;
7575 }
7576
7577 /* Scan INSN and note any of its operands that need fixing.
7578    If DO_PUSHES is false we do not actually push any of the fixups
7579    needed.  The function returns TRUE is any fixups were needed/pushed.
7580    This is used by arm_memory_load_p() which needs to know about loads
7581    of constants that will be converted into minipool loads.  */
7582 static bool
7583 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7584 {
7585   bool result = false;
7586   int opno;
7587
7588   extract_insn (insn);
7589
7590   if (!constrain_operands (1))
7591     fatal_insn_not_found (insn);
7592
7593   if (recog_data.n_alternatives == 0)
7594     return false;
7595
7596   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7597   preprocess_constraints ();
7598
7599   for (opno = 0; opno < recog_data.n_operands; opno++)
7600     {
7601       /* Things we need to fix can only occur in inputs.  */
7602       if (recog_data.operand_type[opno] != OP_IN)
7603         continue;
7604
7605       /* If this alternative is a memory reference, then any mention
7606          of constants in this alternative is really to fool reload
7607          into allowing us to accept one there.  We need to fix them up
7608          now so that we output the right code.  */
7609       if (recog_op_alt[opno][which_alternative].memory_ok)
7610         {
7611           rtx op = recog_data.operand[opno];
7612
7613           if (CONSTANT_P (op))
7614             {
7615               if (do_pushes)
7616                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7617                                    recog_data.operand_mode[opno], op);
7618               result = true;
7619             }
7620           else if (GET_CODE (op) == MEM
7621                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7622                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7623             {
7624               if (do_pushes)
7625                 {
7626                   rtx cop = avoid_constant_pool_reference (op);
7627
7628                   /* Casting the address of something to a mode narrower
7629                      than a word can cause avoid_constant_pool_reference()
7630                      to return the pool reference itself.  That's no good to
7631                      us here.  Lets just hope that we can use the 
7632                      constant pool value directly.  */
7633                   if (op == cop)
7634                     cop = get_pool_constant (XEXP (op, 0));
7635
7636                   push_minipool_fix (insn, address,
7637                                      recog_data.operand_loc[opno],
7638                                      recog_data.operand_mode[opno], cop);
7639                 }
7640
7641               result = true;
7642             }
7643         }
7644     }
7645
7646   return result;
7647 }
7648
7649 /* Gcc puts the pool in the wrong place for ARM, since we can only
7650    load addresses a limited distance around the pc.  We do some
7651    special munging to move the constant pool values to the correct
7652    point in the code.  */
7653 static void
7654 arm_reorg (void)
7655 {
7656   rtx insn;
7657   HOST_WIDE_INT address = 0;
7658   Mfix * fix;
7659
7660   minipool_fix_head = minipool_fix_tail = NULL;
7661
7662   /* The first insn must always be a note, or the code below won't
7663      scan it properly.  */
7664   insn = get_insns ();
7665   if (GET_CODE (insn) != NOTE)
7666     abort ();
7667
7668   /* Scan all the insns and record the operands that will need fixing.  */
7669   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7670     {
7671       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7672           && (arm_cirrus_insn_p (insn)
7673               || GET_CODE (insn) == JUMP_INSN
7674               || arm_memory_load_p (insn)))
7675         cirrus_reorg (insn);
7676
7677       if (GET_CODE (insn) == BARRIER)
7678         push_minipool_barrier (insn, address);
7679       else if (INSN_P (insn))
7680         {
7681           rtx table;
7682
7683           note_invalid_constants (insn, address, true);
7684           address += get_attr_length (insn);
7685
7686           /* If the insn is a vector jump, add the size of the table
7687              and skip the table.  */
7688           if ((table = is_jump_table (insn)) != NULL)
7689             {
7690               address += get_jump_table_size (table);
7691               insn = table;
7692             }
7693         }
7694     }
7695
7696   fix = minipool_fix_head;
7697   
7698   /* Now scan the fixups and perform the required changes.  */
7699   while (fix)
7700     {
7701       Mfix * ftmp;
7702       Mfix * fdel;
7703       Mfix *  last_added_fix;
7704       Mfix * last_barrier = NULL;
7705       Mfix * this_fix;
7706
7707       /* Skip any further barriers before the next fix.  */
7708       while (fix && GET_CODE (fix->insn) == BARRIER)
7709         fix = fix->next;
7710
7711       /* No more fixes.  */
7712       if (fix == NULL)
7713         break;
7714
7715       last_added_fix = NULL;
7716
7717       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7718         {
7719           if (GET_CODE (ftmp->insn) == BARRIER)
7720             {
7721               if (ftmp->address >= minipool_vector_head->max_address)
7722                 break;
7723
7724               last_barrier = ftmp;
7725             }
7726           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7727             break;
7728
7729           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7730         }
7731
7732       /* If we found a barrier, drop back to that; any fixes that we
7733          could have reached but come after the barrier will now go in
7734          the next mini-pool.  */
7735       if (last_barrier != NULL)
7736         {
7737           /* Reduce the refcount for those fixes that won't go into this 
7738              pool after all.  */
7739           for (fdel = last_barrier->next;
7740                fdel && fdel != ftmp;
7741                fdel = fdel->next)
7742             {
7743               fdel->minipool->refcount--;
7744               fdel->minipool = NULL;
7745             }
7746
7747           ftmp = last_barrier;
7748         }
7749       else
7750         {
7751           /* ftmp is first fix that we can't fit into this pool and
7752              there no natural barriers that we could use.  Insert a
7753              new barrier in the code somewhere between the previous
7754              fix and this one, and arrange to jump around it.  */
7755           HOST_WIDE_INT max_address;
7756
7757           /* The last item on the list of fixes must be a barrier, so
7758              we can never run off the end of the list of fixes without
7759              last_barrier being set.  */
7760           if (ftmp == NULL)
7761             abort ();
7762
7763           max_address = minipool_vector_head->max_address;
7764           /* Check that there isn't another fix that is in range that
7765              we couldn't fit into this pool because the pool was
7766              already too large: we need to put the pool before such an
7767              instruction.  */
7768           if (ftmp->address < max_address)
7769             max_address = ftmp->address;
7770
7771           last_barrier = create_fix_barrier (last_added_fix, max_address);
7772         }
7773
7774       assign_minipool_offsets (last_barrier);
7775
7776       while (ftmp)
7777         {
7778           if (GET_CODE (ftmp->insn) != BARRIER
7779               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7780                   == NULL))
7781             break;
7782
7783           ftmp = ftmp->next;
7784         }
7785
7786       /* Scan over the fixes we have identified for this pool, fixing them
7787          up and adding the constants to the pool itself.  */
7788       for (this_fix = fix; this_fix && ftmp != this_fix;
7789            this_fix = this_fix->next)
7790         if (GET_CODE (this_fix->insn) != BARRIER)
7791           {
7792             rtx addr
7793               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
7794                                                   minipool_vector_label),
7795                                this_fix->minipool->offset);
7796             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7797           }
7798
7799       dump_minipool (last_barrier->insn);
7800       fix = ftmp;
7801     }
7802
7803   /* From now on we must synthesize any constants that we can't handle
7804      directly.  This can happen if the RTL gets split during final
7805      instruction generation.  */
7806   after_arm_reorg = 1;
7807
7808   /* Free the minipool memory.  */
7809   obstack_free (&minipool_obstack, minipool_startobj);
7810 }
7811 \f
7812 /* Routines to output assembly language.  */
7813
7814 /* If the rtx is the correct value then return the string of the number.
7815    In this way we can ensure that valid double constants are generated even
7816    when cross compiling.  */
7817 const char *
7818 fp_immediate_constant (rtx x)
7819 {
7820   REAL_VALUE_TYPE r;
7821   int i;
7822   
7823   if (!fp_consts_inited)
7824     init_fp_table ();
7825   
7826   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7827   for (i = 0; i < 8; i++)
7828     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7829       return strings_fp[i];
7830
7831   abort ();
7832 }
7833
7834 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7835 static const char *
7836 fp_const_from_val (REAL_VALUE_TYPE *r)
7837 {
7838   int i;
7839
7840   if (!fp_consts_inited)
7841     init_fp_table ();
7842
7843   for (i = 0; i < 8; i++)
7844     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7845       return strings_fp[i];
7846
7847   abort ();
7848 }
7849
7850 /* Output the operands of a LDM/STM instruction to STREAM.
7851    MASK is the ARM register set mask of which only bits 0-15 are important.
7852    REG is the base register, either the frame pointer or the stack pointer,
7853    INSTR is the possibly suffixed load or store instruction.  */
7854 static void
7855 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7856 {
7857   int i;
7858   int not_first = FALSE;
7859
7860   fputc ('\t', stream);
7861   asm_fprintf (stream, instr, reg);
7862   fputs (", {", stream);
7863   
7864   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7865     if (mask & (1 << i))
7866       {
7867         if (not_first)
7868           fprintf (stream, ", ");
7869         
7870         asm_fprintf (stream, "%r", i);
7871         not_first = TRUE;
7872       }
7873
7874   fprintf (stream, "}");
7875
7876   /* Add a ^ character for the 26-bit ABI, but only if we were loading
7877      the PC.  Otherwise we would generate an UNPREDICTABLE instruction.
7878      Strictly speaking the instruction would be unpredicatble only if
7879      we were writing back the base register as well, but since we never
7880      want to generate an LDM type 2 instruction (register bank switching)
7881      which is what you get if the PC is not being loaded, we do not need
7882      to check for writeback.  */
7883   if (! TARGET_APCS_32
7884       && ((mask & (1 << PC_REGNUM)) != 0))
7885     fprintf (stream, "^");
7886   
7887   fprintf (stream, "\n");
7888 }
7889
7890
7891 /* Output a FLDMX instruction to STREAM.
7892    BASE if the register containing the address.
7893    REG and COUNT specify the register range.
7894    Extra registers may be added to avoid hardware bugs.  */
7895
7896 static void
7897 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7898 {
7899   int i;
7900
7901   /* Workaround ARM10 VFPr1 bug.  */
7902   if (count == 2 && !arm_arch6)
7903     {
7904       if (reg == 15)
7905         reg--;
7906       count++;
7907     }
7908
7909   fputc ('\t', stream);
7910   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7911
7912   for (i = reg; i < reg + count; i++)
7913     {
7914       if (i > reg)
7915         fputs (", ", stream);
7916       asm_fprintf (stream, "d%d", i);
7917     }
7918   fputs ("}\n", stream);
7919
7920 }
7921
7922
7923 /* Output the assembly for a store multiple.  */
7924
7925 const char *
7926 vfp_output_fstmx (rtx * operands)
7927 {
7928   char pattern[100];
7929   int p;
7930   int base;
7931   int i;
7932
7933   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7934   p = strlen (pattern);
7935
7936   if (GET_CODE (operands[1]) != REG)
7937     abort ();
7938
7939   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7940   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7941     {
7942       p += sprintf (&pattern[p], ", d%d", base + i);
7943     }
7944   strcpy (&pattern[p], "}");
7945
7946   output_asm_insn (pattern, operands);
7947   return "";
7948 }
7949
7950
7951 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7952    number of bytes pushed.  */
7953
7954 static int
7955 vfp_emit_fstmx (int base_reg, int count)
7956 {
7957   rtx par;
7958   rtx dwarf;
7959   rtx tmp, reg;
7960   int i;
7961
7962   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7963      register pairs are stored by a store multiple insn.  We avoid this
7964      by pushing an extra pair.  */
7965   if (count == 2 && !arm_arch6)
7966     {
7967       if (base_reg == LAST_VFP_REGNUM - 3)
7968         base_reg -= 2;
7969       count++;
7970     }
7971
7972   /* ??? The frame layout is implementation defined.  We describe
7973      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7974      We really need some way of representing the whole block so that the
7975      unwinder can figure it out at runtime.  */
7976   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7977   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7978
7979   reg = gen_rtx_REG (DFmode, base_reg);
7980   base_reg += 2;
7981
7982   XVECEXP (par, 0, 0)
7983     = gen_rtx_SET (VOIDmode,
7984                    gen_rtx_MEM (BLKmode,
7985                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7986                    gen_rtx_UNSPEC (BLKmode,
7987                                    gen_rtvec (1, reg),
7988                                    UNSPEC_PUSH_MULT));
7989
7990   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7991                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7992                                    GEN_INT (-(count * 8 + 4))));
7993   RTX_FRAME_RELATED_P (tmp) = 1;
7994   XVECEXP (dwarf, 0, 0) = tmp;
7995
7996   tmp = gen_rtx_SET (VOIDmode,
7997                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7998                      reg);
7999   RTX_FRAME_RELATED_P (tmp) = 1;
8000   XVECEXP (dwarf, 0, 1) = tmp;
8001
8002   for (i = 1; i < count; i++)
8003     {
8004       reg = gen_rtx_REG (DFmode, base_reg);
8005       base_reg += 2;
8006       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8007
8008       tmp = gen_rtx_SET (VOIDmode,
8009                          gen_rtx_MEM (DFmode,
8010                                       gen_rtx_PLUS (SImode,
8011                                                     stack_pointer_rtx,
8012                                                     GEN_INT (i * 8))),
8013                          reg);
8014       RTX_FRAME_RELATED_P (tmp) = 1;
8015       XVECEXP (dwarf, 0, i + 1) = tmp;
8016     }
8017
8018   par = emit_insn (par);
8019   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8020                                        REG_NOTES (par));
8021   RTX_FRAME_RELATED_P (par) = 1;
8022
8023   return count * 8 + 4;
8024 }
8025
8026
8027 /* Output a 'call' insn.  */
8028 const char *
8029 output_call (rtx *operands)
8030 {
8031   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8032
8033   if (REGNO (operands[0]) == LR_REGNUM)
8034     {
8035       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8036       output_asm_insn ("mov%?\t%0, %|lr", operands);
8037     }
8038   
8039   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8040   
8041   if (TARGET_INTERWORK)
8042     output_asm_insn ("bx%?\t%0", operands);
8043   else
8044     output_asm_insn ("mov%?\t%|pc, %0", operands);
8045   
8046   return "";
8047 }
8048
8049 /* Output a 'call' insn that is a reference in memory.  */
8050 const char *
8051 output_call_mem (rtx *operands)
8052 {
8053   if (TARGET_INTERWORK)
8054     {
8055       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8056       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8057       output_asm_insn ("bx%?\t%|ip", operands);
8058     }
8059   else if (regno_use_in (LR_REGNUM, operands[0]))
8060     {
8061       /* LR is used in the memory address.  We load the address in the
8062          first instruction.  It's safe to use IP as the target of the
8063          load since the call will kill it anyway.  */
8064       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8065       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8066       output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8067     }
8068   else
8069     {
8070       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8071       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8072     }
8073
8074   return "";
8075 }
8076
8077
8078 /* Output a move from arm registers to an fpa registers.
8079    OPERANDS[0] is an fpa register.
8080    OPERANDS[1] is the first registers of an arm register pair.  */
8081 const char *
8082 output_mov_long_double_fpa_from_arm (rtx *operands)
8083 {
8084   int arm_reg0 = REGNO (operands[1]);
8085   rtx ops[3];
8086
8087   if (arm_reg0 == IP_REGNUM)
8088     abort ();
8089
8090   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8091   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8092   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8093   
8094   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8095   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8096   
8097   return "";
8098 }
8099
8100 /* Output a move from an fpa register to arm registers.
8101    OPERANDS[0] is the first registers of an arm register pair.
8102    OPERANDS[1] is an fpa register.  */
8103 const char *
8104 output_mov_long_double_arm_from_fpa (rtx *operands)
8105 {
8106   int arm_reg0 = REGNO (operands[0]);
8107   rtx ops[3];
8108
8109   if (arm_reg0 == IP_REGNUM)
8110     abort ();
8111
8112   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8113   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8114   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8115
8116   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8117   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8118   return "";
8119 }
8120
8121 /* Output a move from arm registers to arm registers of a long double
8122    OPERANDS[0] is the destination.
8123    OPERANDS[1] is the source.  */
8124 const char *
8125 output_mov_long_double_arm_from_arm (rtx *operands)
8126 {
8127   /* We have to be careful here because the two might overlap.  */
8128   int dest_start = REGNO (operands[0]);
8129   int src_start = REGNO (operands[1]);
8130   rtx ops[2];
8131   int i;
8132
8133   if (dest_start < src_start)
8134     {
8135       for (i = 0; i < 3; i++)
8136         {
8137           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8138           ops[1] = gen_rtx_REG (SImode, src_start + i);
8139           output_asm_insn ("mov%?\t%0, %1", ops);
8140         }
8141     }
8142   else
8143     {
8144       for (i = 2; i >= 0; i--)
8145         {
8146           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8147           ops[1] = gen_rtx_REG (SImode, src_start + i);
8148           output_asm_insn ("mov%?\t%0, %1", ops);
8149         }
8150     }
8151
8152   return "";
8153 }
8154
8155
8156 /* Output a move from arm registers to an fpa registers.
8157    OPERANDS[0] is an fpa register.
8158    OPERANDS[1] is the first registers of an arm register pair.  */
8159 const char *
8160 output_mov_double_fpa_from_arm (rtx *operands)
8161 {
8162   int arm_reg0 = REGNO (operands[1]);
8163   rtx ops[2];
8164
8165   if (arm_reg0 == IP_REGNUM)
8166     abort ();
8167   
8168   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8169   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8170   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8171   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8172   return "";
8173 }
8174
8175 /* Output a move from an fpa register to arm registers.
8176    OPERANDS[0] is the first registers of an arm register pair.
8177    OPERANDS[1] is an fpa register.  */
8178 const char *
8179 output_mov_double_arm_from_fpa (rtx *operands)
8180 {
8181   int arm_reg0 = REGNO (operands[0]);
8182   rtx ops[2];
8183
8184   if (arm_reg0 == IP_REGNUM)
8185     abort ();
8186
8187   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8188   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8189   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8190   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8191   return "";
8192 }
8193
8194 /* Output a move between double words.
8195    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8196    or MEM<-REG and all MEMs must be offsettable addresses.  */
8197 const char *
8198 output_move_double (rtx *operands)
8199 {
8200   enum rtx_code code0 = GET_CODE (operands[0]);
8201   enum rtx_code code1 = GET_CODE (operands[1]);
8202   rtx otherops[3];
8203
8204   if (code0 == REG)
8205     {
8206       int reg0 = REGNO (operands[0]);
8207
8208       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8209       
8210       if (code1 == REG)
8211         {
8212           int reg1 = REGNO (operands[1]);
8213           if (reg1 == IP_REGNUM)
8214             abort ();
8215
8216           /* Ensure the second source is not overwritten.  */
8217           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8218             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8219           else
8220             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8221         }
8222       else if (code1 == CONST_VECTOR)
8223         {
8224           HOST_WIDE_INT hint = 0;
8225
8226           switch (GET_MODE (operands[1]))
8227             {
8228             case V2SImode:
8229               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8230               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8231               break;
8232
8233             case V4HImode:
8234               if (BYTES_BIG_ENDIAN)
8235                 {
8236                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8237                   hint <<= 16;
8238                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8239                 }
8240               else
8241                 {
8242                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8243                   hint <<= 16;
8244                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8245                 }
8246
8247               otherops[1] = GEN_INT (hint);
8248               hint = 0;
8249
8250               if (BYTES_BIG_ENDIAN)
8251                 {
8252                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8253                   hint <<= 16;
8254                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8255                 }
8256               else
8257                 {
8258                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8259                   hint <<= 16;
8260                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8261                 }
8262
8263               operands[1] = GEN_INT (hint);
8264               break;
8265
8266             case V8QImode:
8267               if (BYTES_BIG_ENDIAN)
8268                 {
8269                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8270                   hint <<= 8;
8271                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8272                   hint <<= 8;
8273                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8274                   hint <<= 8;
8275                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8276                 }
8277               else
8278                 {
8279                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8280                   hint <<= 8;
8281                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8282                   hint <<= 8;
8283                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8284                   hint <<= 8;
8285                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8286                 }
8287
8288               otherops[1] = GEN_INT (hint);
8289               hint = 0;
8290
8291               if (BYTES_BIG_ENDIAN)
8292                 {
8293                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8294                   hint <<= 8;
8295                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8296                   hint <<= 8;
8297                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8298                   hint <<= 8;
8299                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8300                 }
8301               else
8302                 {
8303                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8304                   hint <<= 8;
8305                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8306                   hint <<= 8;
8307                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8308                   hint <<= 8;
8309                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8310                 }
8311
8312               operands[1] = GEN_INT (hint);
8313               break;
8314               
8315             default:
8316               abort ();
8317             }
8318           output_mov_immediate (operands);
8319           output_mov_immediate (otherops);
8320         }
8321       else if (code1 == CONST_DOUBLE)
8322         {
8323           if (GET_MODE (operands[1]) == DFmode)
8324             {
8325               REAL_VALUE_TYPE r;
8326               long l[2];
8327
8328               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8329               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8330               otherops[1] = GEN_INT (l[1]);
8331               operands[1] = GEN_INT (l[0]);
8332             }
8333           else if (GET_MODE (operands[1]) != VOIDmode)
8334             abort ();
8335           else if (WORDS_BIG_ENDIAN)
8336             {
8337               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8338               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8339             }
8340           else
8341             {
8342               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8343               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8344             }
8345           
8346           output_mov_immediate (operands);
8347           output_mov_immediate (otherops);
8348         }
8349       else if (code1 == CONST_INT)
8350         {
8351 #if HOST_BITS_PER_WIDE_INT > 32
8352           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8353              what the upper word is.  */
8354           if (WORDS_BIG_ENDIAN)
8355             {
8356               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8357               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8358             }
8359           else
8360             {
8361               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8362               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8363             }
8364 #else
8365           /* Sign extend the intval into the high-order word.  */
8366           if (WORDS_BIG_ENDIAN)
8367             {
8368               otherops[1] = operands[1];
8369               operands[1] = (INTVAL (operands[1]) < 0
8370                              ? constm1_rtx : const0_rtx);
8371             }
8372           else
8373             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8374 #endif
8375           output_mov_immediate (otherops);
8376           output_mov_immediate (operands);
8377         }
8378       else if (code1 == MEM)
8379         {
8380           switch (GET_CODE (XEXP (operands[1], 0)))
8381             {
8382             case REG:
8383               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8384               break;
8385
8386             case PRE_INC:
8387               abort (); /* Should never happen now.  */
8388               break;
8389
8390             case PRE_DEC:
8391               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8392               break;
8393
8394             case POST_INC:
8395               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8396               break;
8397
8398             case POST_DEC:
8399               abort (); /* Should never happen now.  */
8400               break;
8401
8402             case LABEL_REF:
8403             case CONST:
8404               output_asm_insn ("adr%?\t%0, %1", operands);
8405               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8406               break;
8407
8408             default:
8409               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8410                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8411                 {
8412                   otherops[0] = operands[0];
8413                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8414                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8415
8416                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8417                     {
8418                       if (GET_CODE (otherops[2]) == CONST_INT)
8419                         {
8420                           switch ((int) INTVAL (otherops[2]))
8421                             {
8422                             case -8:
8423                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8424                               return "";
8425                             case -4:
8426                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8427                               return "";
8428                             case 4:
8429                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8430                               return "";
8431                             }
8432
8433                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8434                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8435                           else
8436                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8437                         }
8438                       else
8439                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8440                     }
8441                   else
8442                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8443                   
8444                   return "ldm%?ia\t%0, %M0";
8445                 }
8446               else
8447                 {
8448                   otherops[1] = adjust_address (operands[1], SImode, 4);
8449                   /* Take care of overlapping base/data reg.  */
8450                   if (reg_mentioned_p (operands[0], operands[1]))
8451                     {
8452                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8453                       output_asm_insn ("ldr%?\t%0, %1", operands);
8454                     }
8455                   else
8456                     {
8457                       output_asm_insn ("ldr%?\t%0, %1", operands);
8458                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8459                     }
8460                 }
8461             }
8462         }
8463       else
8464         abort ();  /* Constraints should prevent this.  */
8465     }
8466   else if (code0 == MEM && code1 == REG)
8467     {
8468       if (REGNO (operands[1]) == IP_REGNUM)
8469         abort ();
8470
8471       switch (GET_CODE (XEXP (operands[0], 0)))
8472         {
8473         case REG:
8474           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8475           break;
8476
8477         case PRE_INC:
8478           abort (); /* Should never happen now.  */
8479           break;
8480
8481         case PRE_DEC:
8482           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8483           break;
8484
8485         case POST_INC:
8486           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8487           break;
8488
8489         case POST_DEC:
8490           abort (); /* Should never happen now.  */
8491           break;
8492
8493         case PLUS:
8494           if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
8495             {
8496               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8497                 {
8498                 case -8:
8499                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8500                   return "";
8501
8502                 case -4:
8503                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8504                   return "";
8505
8506                 case 4:
8507                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8508                   return "";
8509                 }
8510             }
8511           /* Fall through */
8512
8513         default:
8514           otherops[0] = adjust_address (operands[0], SImode, 4);
8515           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8516           output_asm_insn ("str%?\t%1, %0", operands);
8517           output_asm_insn ("str%?\t%1, %0", otherops);
8518         }
8519     }
8520   else
8521     /* Constraints should prevent this.  */
8522     abort ();
8523
8524   return "";
8525 }
8526
8527
8528 /* Output an arbitrary MOV reg, #n.
8529    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8530 const char *
8531 output_mov_immediate (rtx *operands)
8532 {
8533   HOST_WIDE_INT n = INTVAL (operands[1]);
8534
8535   /* Try to use one MOV.  */
8536   if (const_ok_for_arm (n))
8537     output_asm_insn ("mov%?\t%0, %1", operands);
8538
8539   /* Try to use one MVN.  */
8540   else if (const_ok_for_arm (~n))
8541     {
8542       operands[1] = GEN_INT (~n);
8543       output_asm_insn ("mvn%?\t%0, %1", operands);
8544     }
8545   else
8546     {
8547       int n_ones = 0;
8548       int i;
8549
8550       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8551       for (i = 0; i < 32; i++)
8552         if (n & 1 << i)
8553           n_ones++;
8554
8555       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8556         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8557       else
8558         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8559     }
8560
8561   return "";
8562 }
8563
8564 /* Output an ADD r, s, #n where n may be too big for one instruction.
8565    If adding zero to one register, output nothing.  */
8566 const char *
8567 output_add_immediate (rtx *operands)
8568 {
8569   HOST_WIDE_INT n = INTVAL (operands[2]);
8570
8571   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8572     {
8573       if (n < 0)
8574         output_multi_immediate (operands,
8575                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8576                                 -n);
8577       else
8578         output_multi_immediate (operands,
8579                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8580                                 n);
8581     }
8582
8583   return "";
8584 }
8585
8586 /* Output a multiple immediate operation.
8587    OPERANDS is the vector of operands referred to in the output patterns.
8588    INSTR1 is the output pattern to use for the first constant.
8589    INSTR2 is the output pattern to use for subsequent constants.
8590    IMMED_OP is the index of the constant slot in OPERANDS.
8591    N is the constant value.  */
8592 static const char *
8593 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8594                         int immed_op, HOST_WIDE_INT n)
8595 {
8596 #if HOST_BITS_PER_WIDE_INT > 32
8597   n &= 0xffffffff;
8598 #endif
8599
8600   if (n == 0)
8601     {
8602       /* Quick and easy output.  */
8603       operands[immed_op] = const0_rtx;
8604       output_asm_insn (instr1, operands);
8605     }
8606   else
8607     {
8608       int i;
8609       const char * instr = instr1;
8610
8611       /* Note that n is never zero here (which would give no output).  */
8612       for (i = 0; i < 32; i += 2)
8613         {
8614           if (n & (3 << i))
8615             {
8616               operands[immed_op] = GEN_INT (n & (255 << i));
8617               output_asm_insn (instr, operands);
8618               instr = instr2;
8619               i += 6;
8620             }
8621         }
8622     }
8623   
8624   return "";
8625 }
8626
8627 /* Return the appropriate ARM instruction for the operation code.
8628    The returned result should not be overwritten.  OP is the rtx of the
8629    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8630    was shifted.  */
8631 const char *
8632 arithmetic_instr (rtx op, int shift_first_arg)
8633 {
8634   switch (GET_CODE (op))
8635     {
8636     case PLUS:
8637       return "add";
8638
8639     case MINUS:
8640       return shift_first_arg ? "rsb" : "sub";
8641
8642     case IOR:
8643       return "orr";
8644
8645     case XOR:
8646       return "eor";
8647
8648     case AND:
8649       return "and";
8650
8651     default:
8652       abort ();
8653     }
8654 }
8655
8656 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8657    for the operation code.  The returned result should not be overwritten.
8658    OP is the rtx code of the shift.
8659    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8660    shift.  */
8661 static const char *
8662 shift_op (rtx op, HOST_WIDE_INT *amountp)
8663 {
8664   const char * mnem;
8665   enum rtx_code code = GET_CODE (op);
8666
8667   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8668     *amountp = -1;
8669   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8670     *amountp = INTVAL (XEXP (op, 1));
8671   else
8672     abort ();
8673
8674   switch (code)
8675     {
8676     case ASHIFT:
8677       mnem = "asl";
8678       break;
8679
8680     case ASHIFTRT:
8681       mnem = "asr";
8682       break;
8683
8684     case LSHIFTRT:
8685       mnem = "lsr";
8686       break;
8687
8688     case ROTATERT:
8689       mnem = "ror";
8690       break;
8691
8692     case MULT:
8693       /* We never have to worry about the amount being other than a
8694          power of 2, since this case can never be reloaded from a reg.  */
8695       if (*amountp != -1)
8696         *amountp = int_log2 (*amountp);
8697       else
8698         abort ();
8699       return "asl";
8700
8701     default:
8702       abort ();
8703     }
8704
8705   if (*amountp != -1)
8706     {
8707       /* This is not 100% correct, but follows from the desire to merge
8708          multiplication by a power of 2 with the recognizer for a
8709          shift.  >=32 is not a valid shift for "asl", so we must try and
8710          output a shift that produces the correct arithmetical result.
8711          Using lsr #32 is identical except for the fact that the carry bit
8712          is not set correctly if we set the flags; but we never use the 
8713          carry bit from such an operation, so we can ignore that.  */
8714       if (code == ROTATERT)
8715         /* Rotate is just modulo 32.  */
8716         *amountp &= 31;
8717       else if (*amountp != (*amountp & 31))
8718         {
8719           if (code == ASHIFT)
8720             mnem = "lsr";
8721           *amountp = 32;
8722         }
8723
8724       /* Shifts of 0 are no-ops.  */
8725       if (*amountp == 0)
8726         return NULL;
8727     }     
8728
8729   return mnem;
8730 }
8731
8732 /* Obtain the shift from the POWER of two.  */
8733
8734 static HOST_WIDE_INT
8735 int_log2 (HOST_WIDE_INT power)
8736 {
8737   HOST_WIDE_INT shift = 0;
8738
8739   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8740     {
8741       if (shift > 31)
8742         abort ();
8743       shift++;
8744     }
8745
8746   return shift;
8747 }
8748
8749 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
8750    /bin/as is horribly restrictive.  */
8751 #define MAX_ASCII_LEN 51
8752
8753 void
8754 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8755 {
8756   int i;
8757   int len_so_far = 0;
8758
8759   fputs ("\t.ascii\t\"", stream);
8760   
8761   for (i = 0; i < len; i++)
8762     {
8763       int c = p[i];
8764
8765       if (len_so_far >= MAX_ASCII_LEN)
8766         {
8767           fputs ("\"\n\t.ascii\t\"", stream);
8768           len_so_far = 0;
8769         }
8770
8771       switch (c)
8772         {
8773         case TARGET_TAB:                
8774           fputs ("\\t", stream);
8775           len_so_far += 2;                      
8776           break;
8777           
8778         case TARGET_FF:
8779           fputs ("\\f", stream);
8780           len_so_far += 2;
8781           break;
8782           
8783         case TARGET_BS:
8784           fputs ("\\b", stream);
8785           len_so_far += 2;
8786           break;
8787           
8788         case TARGET_CR:
8789           fputs ("\\r", stream);
8790           len_so_far += 2;
8791           break;
8792           
8793         case TARGET_NEWLINE:
8794           fputs ("\\n", stream);
8795           c = p [i + 1];
8796           if ((c >= ' ' && c <= '~')
8797               || c == TARGET_TAB)
8798             /* This is a good place for a line break.  */
8799             len_so_far = MAX_ASCII_LEN;
8800           else
8801             len_so_far += 2;
8802           break;
8803           
8804         case '\"':
8805         case '\\':
8806           putc ('\\', stream);
8807           len_so_far++;
8808           /* Drop through.  */
8809
8810         default:
8811           if (c >= ' ' && c <= '~')
8812             {
8813               putc (c, stream);
8814               len_so_far++;
8815             }
8816           else
8817             {
8818               fprintf (stream, "\\%03o", c);
8819               len_so_far += 4;
8820             }
8821           break;
8822         }
8823     }
8824
8825   fputs ("\"\n", stream);
8826 }
8827 \f
8828 /* Compute the register sabe mask for registers 0 through 12
8829    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8830 static unsigned long
8831 arm_compute_save_reg0_reg12_mask (void)
8832 {
8833   unsigned long func_type = arm_current_func_type ();
8834   unsigned int save_reg_mask = 0;
8835   unsigned int reg;
8836
8837   if (IS_INTERRUPT (func_type))
8838     {
8839       unsigned int max_reg;
8840       /* Interrupt functions must not corrupt any registers,
8841          even call clobbered ones.  If this is a leaf function
8842          we can just examine the registers used by the RTL, but
8843          otherwise we have to assume that whatever function is
8844          called might clobber anything, and so we have to save
8845          all the call-clobbered registers as well.  */
8846       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8847         /* FIQ handlers have registers r8 - r12 banked, so
8848            we only need to check r0 - r7, Normal ISRs only
8849            bank r14 and r15, so we must check up to r12.
8850            r13 is the stack pointer which is always preserved,
8851            so we do not need to consider it here.  */
8852         max_reg = 7;
8853       else
8854         max_reg = 12;
8855         
8856       for (reg = 0; reg <= max_reg; reg++)
8857         if (regs_ever_live[reg]
8858             || (! current_function_is_leaf && call_used_regs [reg]))
8859           save_reg_mask |= (1 << reg);
8860     }
8861   else
8862     {
8863       /* In the normal case we only need to save those registers
8864          which are call saved and which are used by this function.  */
8865       for (reg = 0; reg <= 10; reg++)
8866         if (regs_ever_live[reg] && ! call_used_regs [reg])
8867           save_reg_mask |= (1 << reg);
8868
8869       /* Handle the frame pointer as a special case.  */
8870       if (! TARGET_APCS_FRAME
8871           && ! frame_pointer_needed
8872           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8873           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8874         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8875
8876       /* If we aren't loading the PIC register,
8877          don't stack it even though it may be live.  */
8878       if (flag_pic
8879           && ! TARGET_SINGLE_PIC_BASE 
8880           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8881         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8882     }
8883
8884   return save_reg_mask;
8885 }
8886
8887 /* Compute a bit mask of which registers need to be
8888    saved on the stack for the current function.  */
8889
8890 static unsigned long
8891 arm_compute_save_reg_mask (void)
8892 {
8893   unsigned int save_reg_mask = 0;
8894   unsigned long func_type = arm_current_func_type ();
8895
8896   if (IS_NAKED (func_type))
8897     /* This should never really happen.  */
8898     return 0;
8899
8900   /* If we are creating a stack frame, then we must save the frame pointer,
8901      IP (which will hold the old stack pointer), LR and the PC.  */
8902   if (frame_pointer_needed)
8903     save_reg_mask |=
8904       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8905       | (1 << IP_REGNUM)
8906       | (1 << LR_REGNUM)
8907       | (1 << PC_REGNUM);
8908
8909   /* Volatile functions do not return, so there
8910      is no need to save any other registers.  */
8911   if (IS_VOLATILE (func_type))
8912     return save_reg_mask;
8913
8914   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8915
8916   /* Decide if we need to save the link register.
8917      Interrupt routines have their own banked link register,
8918      so they never need to save it.
8919      Otherwise if we do not use the link register we do not need to save
8920      it.  If we are pushing other registers onto the stack however, we
8921      can save an instruction in the epilogue by pushing the link register
8922      now and then popping it back into the PC.  This incurs extra memory
8923      accesses though, so we only do it when optimizing for size, and only
8924      if we know that we will not need a fancy return sequence.  */
8925   if (regs_ever_live [LR_REGNUM]
8926           || (save_reg_mask
8927               && optimize_size
8928               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8929     save_reg_mask |= 1 << LR_REGNUM;
8930
8931   if (cfun->machine->lr_save_eliminated)
8932     save_reg_mask &= ~ (1 << LR_REGNUM);
8933
8934   if (TARGET_REALLY_IWMMXT
8935       && ((bit_count (save_reg_mask)
8936            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8937     {
8938       unsigned int reg;
8939
8940       /* The total number of registers that are going to be pushed
8941          onto the stack is odd.  We need to ensure that the stack
8942          is 64-bit aligned before we start to save iWMMXt registers,
8943          and also before we start to create locals.  (A local variable
8944          might be a double or long long which we will load/store using
8945          an iWMMXt instruction).  Therefore we need to push another
8946          ARM register, so that the stack will be 64-bit aligned.  We
8947          try to avoid using the arg registers (r0 -r3) as they might be
8948          used to pass values in a tail call.  */
8949       for (reg = 4; reg <= 12; reg++)
8950         if ((save_reg_mask & (1 << reg)) == 0)
8951           break;
8952
8953       if (reg <= 12)
8954         save_reg_mask |= (1 << reg);
8955       else
8956         {
8957           cfun->machine->sibcall_blocked = 1;
8958           save_reg_mask |= (1 << 3);
8959         }
8960     }
8961
8962   return save_reg_mask;
8963 }
8964
8965
8966 /* Return the number of bytes required to save VFP registers.  */
8967 static int
8968 arm_get_vfp_saved_size (void)
8969 {
8970   unsigned int regno;
8971   int count;
8972   int saved;
8973
8974   saved = 0;
8975   /* Space for saved VFP registers.  */
8976   if (TARGET_HARD_FLOAT && TARGET_VFP)
8977     {
8978       count = 0;
8979       for (regno = FIRST_VFP_REGNUM;
8980            regno < LAST_VFP_REGNUM;
8981            regno += 2)
8982         {
8983           if ((!regs_ever_live[regno] || call_used_regs[regno])
8984               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8985             {
8986               if (count > 0)
8987                 {
8988                   /* Workaround ARM10 VFPr1 bug.  */
8989                   if (count == 2 && !arm_arch6)
8990                     count++;
8991                   saved += count * 8 + 4;
8992                 }
8993               count = 0;
8994             }
8995           else
8996             count++;
8997         }
8998       if (count > 0)
8999         {
9000           if (count == 2 && !arm_arch6)
9001             count++;
9002           saved += count * 8 + 4;
9003         }
9004     }
9005   return saved;
9006 }
9007
9008
9009 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9010    everything bar the final return instruction.  */
9011 const char *
9012 output_return_instruction (rtx operand, int really_return, int reverse)
9013 {
9014   char conditional[10];
9015   char instr[100];
9016   int reg;
9017   unsigned long live_regs_mask;
9018   unsigned long func_type;
9019   arm_stack_offsets *offsets;
9020
9021   func_type = arm_current_func_type ();
9022
9023   if (IS_NAKED (func_type))
9024     return "";
9025
9026   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9027     {
9028       /* If this function was declared non-returning, and we have
9029          found a tail call, then we have to trust that the called
9030          function won't return.  */
9031       if (really_return)
9032         {
9033           rtx ops[2];
9034       
9035           /* Otherwise, trap an attempted return by aborting.  */
9036           ops[0] = operand;
9037           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
9038                                        : "abort");
9039           assemble_external_libcall (ops[1]);
9040           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9041         }
9042       
9043       return "";
9044     }
9045
9046   if (current_function_calls_alloca && !really_return)
9047     abort ();
9048
9049   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9050
9051   return_used_this_function = 1;
9052
9053   live_regs_mask = arm_compute_save_reg_mask ();
9054
9055   if (live_regs_mask)
9056     {
9057       const char * return_reg;
9058
9059       /* If we do not have any special requirements for function exit 
9060          (eg interworking, or ISR) then we can load the return address 
9061          directly into the PC.  Otherwise we must load it into LR.  */
9062       if (really_return
9063           && ! TARGET_INTERWORK)
9064         return_reg = reg_names[PC_REGNUM];
9065       else
9066         return_reg = reg_names[LR_REGNUM];
9067
9068       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9069         {
9070           /* There are three possible reasons for the IP register
9071              being saved.  1) a stack frame was created, in which case
9072              IP contains the old stack pointer, or 2) an ISR routine
9073              corrupted it, or 3) it was saved to align the stack on
9074              iWMMXt.  In case 1, restore IP into SP, otherwise just
9075              restore IP.  */
9076           if (frame_pointer_needed)
9077             {
9078               live_regs_mask &= ~ (1 << IP_REGNUM);
9079               live_regs_mask |=   (1 << SP_REGNUM);
9080             }
9081           else
9082             {
9083               if (! IS_INTERRUPT (func_type)
9084                   && ! TARGET_REALLY_IWMMXT)
9085                 abort ();
9086             }
9087         }
9088
9089       /* On some ARM architectures it is faster to use LDR rather than
9090          LDM to load a single register.  On other architectures, the
9091          cost is the same.  In 26 bit mode, or for exception handlers,
9092          we have to use LDM to load the PC so that the CPSR is also
9093          restored.  */
9094       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9095         {
9096           if (live_regs_mask == (unsigned int)(1 << reg))
9097             break;
9098         }
9099       if (reg <= LAST_ARM_REGNUM
9100           && (reg != LR_REGNUM
9101               || ! really_return 
9102               || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
9103         {
9104           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional, 
9105                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9106         }
9107       else
9108         {
9109           char *p;
9110           int first = 1;
9111
9112           /* Generate the load multiple instruction to restore the
9113              registers.  Note we can get here, even if
9114              frame_pointer_needed is true, but only if sp already
9115              points to the base of the saved core registers.  */
9116           if (live_regs_mask & (1 << SP_REGNUM))
9117             {
9118               unsigned HOST_WIDE_INT stack_adjust;
9119
9120               offsets = arm_get_frame_offsets ();
9121               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9122               if (stack_adjust != 0 && stack_adjust != 4)
9123                 abort ();
9124
9125               if (stack_adjust && arm_arch5)
9126                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9127               else
9128                 {
9129                   /* If we can't use ldmib (SA110 bug), then try to pop r3
9130                      instead.  */
9131                   if (stack_adjust)
9132                     live_regs_mask |= 1 << 3;
9133                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9134                 }
9135             }
9136           else
9137             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9138
9139           p = instr + strlen (instr);
9140
9141           for (reg = 0; reg <= SP_REGNUM; reg++)
9142             if (live_regs_mask & (1 << reg))
9143               {
9144                 int l = strlen (reg_names[reg]);
9145
9146                 if (first)
9147                   first = 0;
9148                 else
9149                   {
9150                     memcpy (p, ", ", 2);
9151                     p += 2;
9152                   }
9153
9154                 memcpy (p, "%|", 2);
9155                 memcpy (p + 2, reg_names[reg], l);
9156                 p += l + 2;
9157               }
9158           
9159           if (live_regs_mask & (1 << LR_REGNUM))
9160             {
9161               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9162               /* Decide if we need to add the ^ symbol to the end of the
9163                  register list.  This causes the saved condition codes
9164                  register to be copied into the current condition codes
9165                  register.  We do the copy if we are conforming to the 32-bit
9166                  ABI and this is an interrupt function, or if we are
9167                  conforming to the 26-bit ABI.  There is a special case for
9168                  the 26-bit ABI however, which is if we are writing back the
9169                  stack pointer but not loading the PC.  In this case adding
9170                  the ^ symbol would create a type 2 LDM instruction, where
9171                  writeback is UNPREDICTABLE.  We are safe in leaving the ^
9172                  character off in this case however, since the actual return
9173                  instruction will be a MOVS which will restore the CPSR.  */
9174               if ((TARGET_APCS_32 && IS_INTERRUPT (func_type))
9175                   || (! TARGET_APCS_32 && really_return))
9176                 strcat (p, "^");
9177             }
9178           else
9179             strcpy (p, "}");
9180         }
9181
9182       output_asm_insn (instr, & operand);
9183
9184       /* See if we need to generate an extra instruction to
9185          perform the actual function return.  */
9186       if (really_return
9187           && func_type != ARM_FT_INTERWORKED
9188           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9189         {
9190           /* The return has already been handled
9191              by loading the LR into the PC.  */
9192           really_return = 0;
9193         }
9194     }
9195
9196   if (really_return)
9197     {
9198       switch ((int) ARM_FUNC_TYPE (func_type))
9199         {
9200         case ARM_FT_ISR:
9201         case ARM_FT_FIQ:
9202           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9203           break;
9204
9205         case ARM_FT_INTERWORKED:
9206           sprintf (instr, "bx%s\t%%|lr", conditional);
9207           break;
9208
9209         case ARM_FT_EXCEPTION:
9210           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9211           break;
9212
9213         default:
9214           /* ARMv5 implementations always provide BX, so interworking
9215              is the default unless APCS-26 is in use.  */
9216           if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
9217             sprintf (instr, "bx%s\t%%|lr", conditional);            
9218           else
9219             sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
9220                      conditional, TARGET_APCS_32 ? "" : "s");
9221           break;
9222         }
9223
9224       output_asm_insn (instr, & operand);
9225     }
9226
9227   return "";
9228 }
9229
9230 /* Write the function name into the code section, directly preceding
9231    the function prologue.
9232
9233    Code will be output similar to this:
9234      t0
9235          .ascii "arm_poke_function_name", 0
9236          .align
9237      t1
9238          .word 0xff000000 + (t1 - t0)
9239      arm_poke_function_name
9240          mov     ip, sp
9241          stmfd   sp!, {fp, ip, lr, pc}
9242          sub     fp, ip, #4
9243
9244    When performing a stack backtrace, code can inspect the value
9245    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9246    at location pc - 12 and the top 8 bits are set, then we know
9247    that there is a function name embedded immediately preceding this
9248    location and has length ((pc[-3]) & 0xff000000).
9249
9250    We assume that pc is declared as a pointer to an unsigned long.
9251
9252    It is of no benefit to output the function name if we are assembling
9253    a leaf function.  These function types will not contain a stack
9254    backtrace structure, therefore it is not possible to determine the
9255    function name.  */
9256 void
9257 arm_poke_function_name (FILE *stream, const char *name)
9258 {
9259   unsigned long alignlength;
9260   unsigned long length;
9261   rtx           x;
9262
9263   length      = strlen (name) + 1;
9264   alignlength = ROUND_UP_WORD (length);
9265   
9266   ASM_OUTPUT_ASCII (stream, name, length);
9267   ASM_OUTPUT_ALIGN (stream, 2);
9268   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9269   assemble_aligned_integer (UNITS_PER_WORD, x);
9270 }
9271
9272 /* Place some comments into the assembler stream
9273    describing the current function.  */
9274 static void
9275 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9276 {
9277   unsigned long func_type;
9278
9279   if (!TARGET_ARM)
9280     {
9281       thumb_output_function_prologue (f, frame_size);
9282       return;
9283     }
9284   
9285   /* Sanity check.  */
9286   if (arm_ccfsm_state || arm_target_insn)
9287     abort ();
9288
9289   func_type = arm_current_func_type ();
9290   
9291   switch ((int) ARM_FUNC_TYPE (func_type))
9292     {
9293     default:
9294     case ARM_FT_NORMAL:
9295       break;
9296     case ARM_FT_INTERWORKED:
9297       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9298       break;
9299     case ARM_FT_EXCEPTION_HANDLER:
9300       asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9301       break;
9302     case ARM_FT_ISR:
9303       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9304       break;
9305     case ARM_FT_FIQ:
9306       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9307       break;
9308     case ARM_FT_EXCEPTION:
9309       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9310       break;
9311     }
9312   
9313   if (IS_NAKED (func_type))
9314     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9315
9316   if (IS_VOLATILE (func_type))
9317     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9318
9319   if (IS_NESTED (func_type))
9320     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9321     
9322   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9323                current_function_args_size,
9324                current_function_pretend_args_size, frame_size);
9325
9326   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9327                frame_pointer_needed,
9328                cfun->machine->uses_anonymous_args);
9329
9330   if (cfun->machine->lr_save_eliminated)
9331     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9332
9333 #ifdef AOF_ASSEMBLER
9334   if (flag_pic)
9335     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9336 #endif
9337
9338   return_used_this_function = 0;  
9339 }
9340
9341 const char *
9342 arm_output_epilogue (rtx sibling)
9343 {
9344   int reg;
9345   unsigned long saved_regs_mask;
9346   unsigned long func_type;
9347   /* Floats_offset is the offset from the "virtual" frame.  In an APCS 
9348      frame that is $fp + 4 for a non-variadic function.  */
9349   int floats_offset = 0;
9350   rtx operands[3];
9351   FILE * f = asm_out_file;
9352   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9353   unsigned int lrm_count = 0;
9354   int really_return = (sibling == NULL);
9355   int start_reg;
9356   arm_stack_offsets *offsets;
9357
9358   /* If we have already generated the return instruction
9359      then it is futile to generate anything else.  */
9360   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9361     return "";
9362
9363   func_type = arm_current_func_type ();
9364
9365   if (IS_NAKED (func_type))
9366     /* Naked functions don't have epilogues.  */
9367     return "";
9368
9369   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9370     {
9371       rtx op;
9372           
9373       /* A volatile function should never return.  Call abort.  */
9374       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9375       assemble_external_libcall (op);
9376       output_asm_insn ("bl\t%a0", &op);
9377       
9378       return "";
9379     }
9380
9381   if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9382       && ! really_return)
9383     /* If we are throwing an exception, then we really must
9384        be doing a return,  so we can't tail-call.  */
9385     abort ();
9386   
9387   offsets = arm_get_frame_offsets ();
9388   saved_regs_mask = arm_compute_save_reg_mask ();
9389
9390   if (TARGET_IWMMXT)
9391     lrm_count = bit_count (saved_regs_mask);
9392
9393   floats_offset = offsets->saved_args;
9394   /* Compute how far away the floats will be.  */
9395   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9396     if (saved_regs_mask & (1 << reg))
9397       floats_offset += 4;
9398   
9399   if (frame_pointer_needed)
9400     {
9401       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9402       int vfp_offset = offsets->frame;
9403
9404       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9405         {
9406           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9407             if (regs_ever_live[reg] && !call_used_regs[reg])
9408               {
9409                 floats_offset += 12;
9410                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
9411                              reg, FP_REGNUM, floats_offset - vfp_offset);
9412               }
9413         }
9414       else
9415         {
9416           start_reg = LAST_FPA_REGNUM;
9417
9418           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9419             {
9420               if (regs_ever_live[reg] && !call_used_regs[reg])
9421                 {
9422                   floats_offset += 12;
9423                   
9424                   /* We can't unstack more than four registers at once.  */
9425                   if (start_reg - reg == 3)
9426                     {
9427                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9428                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9429                       start_reg = reg - 1;
9430                     }
9431                 }
9432               else
9433                 {
9434                   if (reg != start_reg)
9435                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9436                                  reg + 1, start_reg - reg,
9437                                  FP_REGNUM, floats_offset - vfp_offset);
9438                   start_reg = reg - 1;
9439                 }
9440             }
9441
9442           /* Just in case the last register checked also needs unstacking.  */
9443           if (reg != start_reg)
9444             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9445                          reg + 1, start_reg - reg,
9446                          FP_REGNUM, floats_offset - vfp_offset);
9447         }
9448
9449       if (TARGET_HARD_FLOAT && TARGET_VFP)
9450         {
9451           int saved_size;
9452
9453           /* The fldmx insn does not have base+offset addressing modes,
9454              so we use IP to hold the address.  */
9455           saved_size = arm_get_vfp_saved_size ();
9456
9457           if (saved_size > 0)
9458             {
9459               floats_offset += saved_size;
9460               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9461                            FP_REGNUM, floats_offset - vfp_offset);
9462             }
9463           start_reg = FIRST_VFP_REGNUM;
9464           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9465             {
9466               if ((!regs_ever_live[reg] || call_used_regs[reg])
9467                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9468                 {
9469                   if (start_reg != reg)
9470                     arm_output_fldmx (f, IP_REGNUM,
9471                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9472                                       (reg - start_reg) / 2);
9473                   start_reg = reg + 2;
9474                 }
9475             }
9476           if (start_reg != reg)
9477             arm_output_fldmx (f, IP_REGNUM,
9478                               (start_reg - FIRST_VFP_REGNUM) / 2,
9479                               (reg - start_reg) / 2);
9480         }
9481
9482       if (TARGET_IWMMXT)
9483         {
9484           /* The frame pointer is guaranteed to be non-double-word aligned.
9485              This is because it is set to (old_stack_pointer - 4) and the
9486              old_stack_pointer was double word aligned.  Thus the offset to
9487              the iWMMXt registers to be loaded must also be non-double-word
9488              sized, so that the resultant address *is* double-word aligned.
9489              We can ignore floats_offset since that was already included in
9490              the live_regs_mask.  */
9491           lrm_count += (lrm_count % 2 ? 2 : 1);
9492               
9493           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9494             if (regs_ever_live[reg] && !call_used_regs[reg])
9495               {
9496                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n", 
9497                              reg, FP_REGNUM, lrm_count * 4);
9498                 lrm_count += 2; 
9499               }
9500         }
9501
9502       /* saved_regs_mask should contain the IP, which at the time of stack
9503          frame generation actually contains the old stack pointer.  So a
9504          quick way to unwind the stack is just pop the IP register directly
9505          into the stack pointer.  */
9506       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9507         abort ();
9508       saved_regs_mask &= ~ (1 << IP_REGNUM);
9509       saved_regs_mask |=   (1 << SP_REGNUM);
9510
9511       /* There are two registers left in saved_regs_mask - LR and PC.  We
9512          only need to restore the LR register (the return address), but to
9513          save time we can load it directly into the PC, unless we need a
9514          special function exit sequence, or we are not really returning.  */
9515       if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9516         /* Delete the LR from the register mask, so that the LR on
9517            the stack is loaded into the PC in the register mask.  */
9518         saved_regs_mask &= ~ (1 << LR_REGNUM);
9519       else
9520         saved_regs_mask &= ~ (1 << PC_REGNUM);
9521
9522       /* We must use SP as the base register, because SP is one of the
9523          registers being restored.  If an interrupt or page fault
9524          happens in the ldm instruction, the SP might or might not
9525          have been restored.  That would be bad, as then SP will no
9526          longer indicate the safe area of stack, and we can get stack
9527          corruption.  Using SP as the base register means that it will
9528          be reset correctly to the original value, should an interrupt
9529          occur.  If the stack pointer already points at the right
9530          place, then omit the subtraction.  */
9531       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9532           || current_function_calls_alloca)
9533         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9534                      4 * bit_count (saved_regs_mask));
9535       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9536
9537       if (IS_INTERRUPT (func_type))
9538         /* Interrupt handlers will have pushed the
9539            IP onto the stack, so restore it now.  */
9540         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9541     }
9542   else
9543     {
9544       /* Restore stack pointer if necessary.  */
9545       if (offsets->outgoing_args != offsets->saved_regs)
9546         {
9547           operands[0] = operands[1] = stack_pointer_rtx;
9548           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9549           output_add_immediate (operands);
9550         }
9551
9552       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9553         {
9554           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9555             if (regs_ever_live[reg] && !call_used_regs[reg])
9556               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9557                            reg, SP_REGNUM);
9558         }
9559       else
9560         {
9561           start_reg = FIRST_FPA_REGNUM;
9562
9563           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9564             {
9565               if (regs_ever_live[reg] && !call_used_regs[reg])
9566                 {
9567                   if (reg - start_reg == 3)
9568                     {
9569                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9570                                    start_reg, SP_REGNUM);
9571                       start_reg = reg + 1;
9572                     }
9573                 }
9574               else
9575                 {
9576                   if (reg != start_reg)
9577                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9578                                  start_reg, reg - start_reg,
9579                                  SP_REGNUM);
9580                   
9581                   start_reg = reg + 1;
9582                 }
9583             }
9584
9585           /* Just in case the last register checked also needs unstacking.  */
9586           if (reg != start_reg)
9587             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9588                          start_reg, reg - start_reg, SP_REGNUM);
9589         }
9590
9591       if (TARGET_HARD_FLOAT && TARGET_VFP)
9592         {
9593           start_reg = FIRST_VFP_REGNUM;
9594           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9595             {
9596               if ((!regs_ever_live[reg] || call_used_regs[reg])
9597                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9598                 {
9599                   if (start_reg != reg)
9600                     arm_output_fldmx (f, SP_REGNUM,
9601                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9602                                       (reg - start_reg) / 2);
9603                   start_reg = reg + 2;
9604                 }
9605             }
9606           if (start_reg != reg)
9607             arm_output_fldmx (f, SP_REGNUM,
9608                               (start_reg - FIRST_VFP_REGNUM) / 2,
9609                               (reg - start_reg) / 2);
9610         }
9611       if (TARGET_IWMMXT)
9612         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9613           if (regs_ever_live[reg] && !call_used_regs[reg])
9614             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9615
9616       /* If we can, restore the LR into the PC.  */
9617       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9618           && really_return
9619           && current_function_pretend_args_size == 0
9620           && saved_regs_mask & (1 << LR_REGNUM))
9621         {
9622           saved_regs_mask &= ~ (1 << LR_REGNUM);
9623           saved_regs_mask |=   (1 << PC_REGNUM);
9624         }
9625
9626       /* Load the registers off the stack.  If we only have one register
9627          to load use the LDR instruction - it is faster.  */
9628       if (saved_regs_mask == (1 << LR_REGNUM))
9629         {
9630           /* The exception handler ignores the LR, so we do
9631              not really need to load it off the stack.  */
9632           if (eh_ofs)
9633             asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
9634           else
9635             asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9636         }
9637       else if (saved_regs_mask)
9638         {
9639           if (saved_regs_mask & (1 << SP_REGNUM))
9640             /* Note - write back to the stack register is not enabled
9641                (ie "ldmfd sp!...").  We know that the stack pointer is
9642                in the list of registers and if we add writeback the
9643                instruction becomes UNPREDICTABLE.  */
9644             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9645           else
9646             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9647         }
9648
9649       if (current_function_pretend_args_size)
9650         {
9651           /* Unwind the pre-pushed regs.  */
9652           operands[0] = operands[1] = stack_pointer_rtx;
9653           operands[2] = GEN_INT (current_function_pretend_args_size);
9654           output_add_immediate (operands);
9655         }
9656     }
9657
9658   if (! really_return
9659     || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9660         && current_function_pretend_args_size == 0
9661         && saved_regs_mask & (1 << PC_REGNUM)))
9662     return "";
9663
9664   /* Generate the return instruction.  */
9665   switch ((int) ARM_FUNC_TYPE (func_type))
9666     {
9667     case ARM_FT_EXCEPTION_HANDLER:
9668       /* Even in 26-bit mode we do a mov (rather than a movs)
9669          because we don't have the PSR bits set in the address.  */
9670       asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9671       break;
9672
9673     case ARM_FT_ISR:
9674     case ARM_FT_FIQ:
9675       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9676       break;
9677
9678     case ARM_FT_EXCEPTION:
9679       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9680       break;
9681
9682     case ARM_FT_INTERWORKED:
9683       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9684       break;
9685
9686     default:
9687       if (frame_pointer_needed)
9688         /* If we used the frame pointer then the return address
9689            will have been loaded off the stack directly into the
9690            PC, so there is no need to issue a MOV instruction
9691            here.  */
9692         ;
9693       else if (current_function_pretend_args_size == 0
9694                && (saved_regs_mask & (1 << LR_REGNUM)))
9695         /* Similarly we may have been able to load LR into the PC
9696            even if we did not create a stack frame.  */
9697         ;
9698       else if (TARGET_APCS_32)
9699         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9700       else
9701         asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9702       break;
9703     }
9704
9705   return "";
9706 }
9707
9708 static void
9709 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9710                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9711 {
9712   arm_stack_offsets *offsets;
9713
9714   if (TARGET_THUMB)
9715     {
9716       /* ??? Probably not safe to set this here, since it assumes that a
9717          function will be emitted as assembly immediately after we generate
9718          RTL for it.  This does not happen for inline functions.  */
9719       return_used_this_function = 0;
9720     }
9721   else
9722     {
9723       /* We need to take into account any stack-frame rounding.  */
9724       offsets = arm_get_frame_offsets ();
9725
9726       if (use_return_insn (FALSE, NULL)
9727           && return_used_this_function
9728           && offsets->saved_regs != offsets->outgoing_args
9729           && !frame_pointer_needed)
9730         abort ();
9731
9732       /* Reset the ARM-specific per-function variables.  */
9733       after_arm_reorg = 0;
9734     }
9735 }
9736
9737 /* Generate and emit an insn that we will recognize as a push_multi.
9738    Unfortunately, since this insn does not reflect very well the actual
9739    semantics of the operation, we need to annotate the insn for the benefit
9740    of DWARF2 frame unwind information.  */
9741 static rtx
9742 emit_multi_reg_push (int mask)
9743 {
9744   int num_regs = 0;
9745   int num_dwarf_regs;
9746   int i, j;
9747   rtx par;
9748   rtx dwarf;
9749   int dwarf_par_index;
9750   rtx tmp, reg;
9751
9752   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9753     if (mask & (1 << i))
9754       num_regs++;
9755
9756   if (num_regs == 0 || num_regs > 16)
9757     abort ();
9758
9759   /* We don't record the PC in the dwarf frame information.  */
9760   num_dwarf_regs = num_regs;
9761   if (mask & (1 << PC_REGNUM))
9762     num_dwarf_regs--;
9763
9764   /* For the body of the insn we are going to generate an UNSPEC in
9765      parallel with several USEs.  This allows the insn to be recognized
9766      by the push_multi pattern in the arm.md file.  The insn looks
9767      something like this:
9768
9769        (parallel [ 
9770            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9771                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9772            (use (reg:SI 11 fp))
9773            (use (reg:SI 12 ip))
9774            (use (reg:SI 14 lr))
9775            (use (reg:SI 15 pc))
9776         ])
9777
9778      For the frame note however, we try to be more explicit and actually
9779      show each register being stored into the stack frame, plus a (single)
9780      decrement of the stack pointer.  We do it this way in order to be
9781      friendly to the stack unwinding code, which only wants to see a single
9782      stack decrement per instruction.  The RTL we generate for the note looks
9783      something like this:
9784
9785       (sequence [ 
9786            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9787            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9788            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9789            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9790            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9791         ])
9792
9793       This sequence is used both by the code to support stack unwinding for
9794       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9795   
9796   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9797   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9798   dwarf_par_index = 1;
9799
9800   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9801     {
9802       if (mask & (1 << i))
9803         {
9804           reg = gen_rtx_REG (SImode, i);
9805
9806           XVECEXP (par, 0, 0)
9807             = gen_rtx_SET (VOIDmode,
9808                            gen_rtx_MEM (BLKmode,
9809                                         gen_rtx_PRE_DEC (BLKmode,
9810                                                          stack_pointer_rtx)),
9811                            gen_rtx_UNSPEC (BLKmode,
9812                                            gen_rtvec (1, reg),
9813                                            UNSPEC_PUSH_MULT));
9814
9815           if (i != PC_REGNUM)
9816             {
9817               tmp = gen_rtx_SET (VOIDmode,
9818                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9819                                  reg);
9820               RTX_FRAME_RELATED_P (tmp) = 1;
9821               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9822               dwarf_par_index++;
9823             }
9824
9825           break;
9826         }
9827     }
9828
9829   for (j = 1, i++; j < num_regs; i++)
9830     {
9831       if (mask & (1 << i))
9832         {
9833           reg = gen_rtx_REG (SImode, i);
9834
9835           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9836
9837           if (i != PC_REGNUM)
9838             {
9839               tmp = gen_rtx_SET (VOIDmode,
9840                                  gen_rtx_MEM (SImode,
9841                                               plus_constant (stack_pointer_rtx,
9842                                                              4 * j)),
9843                                  reg);
9844               RTX_FRAME_RELATED_P (tmp) = 1;
9845               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9846             }
9847
9848           j++;
9849         }
9850     }
9851
9852   par = emit_insn (par);
9853   
9854   tmp = gen_rtx_SET (SImode,
9855                      stack_pointer_rtx,
9856                      gen_rtx_PLUS (SImode,
9857                                    stack_pointer_rtx,
9858                                    GEN_INT (-4 * num_regs)));
9859   RTX_FRAME_RELATED_P (tmp) = 1;
9860   XVECEXP (dwarf, 0, 0) = tmp;
9861   
9862   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9863                                        REG_NOTES (par));
9864   return par;
9865 }
9866
9867 static rtx
9868 emit_sfm (int base_reg, int count)
9869 {
9870   rtx par;
9871   rtx dwarf;
9872   rtx tmp, reg;
9873   int i;
9874
9875   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9876   dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9877
9878   reg = gen_rtx_REG (XFmode, base_reg++);
9879
9880   XVECEXP (par, 0, 0)
9881     = gen_rtx_SET (VOIDmode, 
9882                    gen_rtx_MEM (BLKmode,
9883                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9884                    gen_rtx_UNSPEC (BLKmode,
9885                                    gen_rtvec (1, reg),
9886                                    UNSPEC_PUSH_MULT));
9887   tmp
9888     = gen_rtx_SET (VOIDmode, 
9889                    gen_rtx_MEM (XFmode,
9890                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9891                    reg);
9892   RTX_FRAME_RELATED_P (tmp) = 1;
9893   XVECEXP (dwarf, 0, count - 1) = tmp;    
9894   
9895   for (i = 1; i < count; i++)
9896     {
9897       reg = gen_rtx_REG (XFmode, base_reg++);
9898       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9899
9900       tmp = gen_rtx_SET (VOIDmode, 
9901                          gen_rtx_MEM (XFmode,
9902                                       gen_rtx_PRE_DEC (BLKmode,
9903                                                        stack_pointer_rtx)),
9904                          reg);
9905       RTX_FRAME_RELATED_P (tmp) = 1;
9906       XVECEXP (dwarf, 0, count - i - 1) = tmp;    
9907     }
9908
9909   par = emit_insn (par);
9910   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9911                                        REG_NOTES (par));
9912   return par;
9913 }
9914
9915
9916 /* Compute the distance from register FROM to register TO.
9917    These can be the arg pointer (26), the soft frame pointer (25),
9918    the stack pointer (13) or the hard frame pointer (11).
9919    Typical stack layout looks like this:
9920
9921        old stack pointer -> |    |
9922                              ----
9923                             |    | \
9924                             |    |   saved arguments for
9925                             |    |   vararg functions
9926                             |    | /
9927                               --
9928    hard FP & arg pointer -> |    | \
9929                             |    |   stack
9930                             |    |   frame
9931                             |    | /
9932                               --
9933                             |    | \
9934                             |    |   call saved
9935                             |    |   registers
9936       soft frame pointer -> |    | /
9937                               --
9938                             |    | \
9939                             |    |   local
9940                             |    |   variables
9941                             |    | /
9942                               --
9943                             |    | \
9944                             |    |   outgoing
9945                             |    |   arguments
9946    current stack pointer -> |    | /
9947                               --
9948
9949   For a given function some or all of these stack components
9950   may not be needed, giving rise to the possibility of
9951   eliminating some of the registers.
9952
9953   The values returned by this function must reflect the behavior
9954   of arm_expand_prologue() and arm_compute_save_reg_mask().
9955
9956   The sign of the number returned reflects the direction of stack
9957   growth, so the values are positive for all eliminations except
9958   from the soft frame pointer to the hard frame pointer.
9959
9960   SFP may point just inside the local variables block to ensure correct
9961   alignment.  */
9962
9963
9964 /* Calculate stack offsets.  These are used to calculate register elimination
9965    offsets and in prologue/epilogue code.  */
9966
9967 static arm_stack_offsets *
9968 arm_get_frame_offsets (void)
9969 {
9970   struct arm_stack_offsets *offsets;
9971   unsigned long func_type;
9972   int leaf;
9973   int saved;
9974   HOST_WIDE_INT frame_size;
9975
9976   offsets = &cfun->machine->stack_offsets;
9977   
9978   /* We need to know if we are a leaf function.  Unfortunately, it
9979      is possible to be called after start_sequence has been called,
9980      which causes get_insns to return the insns for the sequence,
9981      not the function, which will cause leaf_function_p to return
9982      the incorrect result.
9983
9984      to know about leaf functions once reload has completed, and the
9985      frame size cannot be changed after that time, so we can safely
9986      use the cached value.  */
9987
9988   if (reload_completed)
9989     return offsets;
9990
9991   /* Initially this is the size of the local variables.  It will translated
9992      into an offset once we have determined the size of preceding data.  */
9993   frame_size = ROUND_UP_WORD (get_frame_size ());
9994
9995   leaf = leaf_function_p ();
9996
9997   /* Space for variadic functions.  */
9998   offsets->saved_args = current_function_pretend_args_size;
9999
10000   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10001
10002   if (TARGET_ARM)
10003     {
10004       unsigned int regno;
10005
10006       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10007
10008       /* We know that SP will be doubleword aligned on entry, and we must
10009          preserve that condition at any subroutine call.  We also require the
10010          soft frame pointer to be doubleword aligned.  */
10011
10012       if (TARGET_REALLY_IWMMXT)
10013         {
10014           /* Check for the call-saved iWMMXt registers.  */
10015           for (regno = FIRST_IWMMXT_REGNUM;
10016                regno <= LAST_IWMMXT_REGNUM;
10017                regno++)
10018             if (regs_ever_live [regno] && ! call_used_regs [regno])
10019               saved += 8;
10020         }
10021
10022       func_type = arm_current_func_type ();
10023       if (! IS_VOLATILE (func_type))
10024         {
10025           /* Space for saved FPA registers.  */
10026           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10027           if (regs_ever_live[regno] && ! call_used_regs[regno])
10028             saved += 12;
10029
10030           /* Space for saved VFP registers.  */
10031           if (TARGET_HARD_FLOAT && TARGET_VFP)
10032             saved += arm_get_vfp_saved_size ();
10033         }
10034     }
10035   else /* TARGET_THUMB */
10036     {
10037       int reg;
10038       int count_regs;
10039
10040       saved = 0;
10041       count_regs = 0;
10042       for (reg = 8; reg < 13; reg ++)
10043         if (THUMB_REG_PUSHED_P (reg))
10044           count_regs ++;
10045       if (count_regs)
10046         saved += 4 * count_regs;
10047       count_regs = 0;
10048       for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10049         if (THUMB_REG_PUSHED_P (reg))
10050           count_regs ++;
10051       if (count_regs || ! leaf_function_p ()
10052           || thumb_far_jump_used_p ())
10053         saved += 4 * (count_regs + 1);
10054       if (TARGET_BACKTRACE)
10055         {
10056           if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10057             saved += 20;
10058           else
10059             saved += 16;
10060         }
10061     }
10062
10063   /* Saved registers include the stack frame.  */
10064   offsets->saved_regs = offsets->saved_args + saved;
10065   offsets->soft_frame = offsets->saved_regs;
10066   /* A leaf function does not need any stack alignment if it has nothing
10067      on the stack.  */
10068   if (leaf && frame_size == 0)
10069     {
10070       offsets->outgoing_args = offsets->soft_frame;
10071       return offsets;
10072     }
10073
10074   /* Ensure SFP has the correct alignment.  */
10075   if (ARM_DOUBLEWORD_ALIGN
10076       && (offsets->soft_frame & 7))
10077     offsets->soft_frame += 4;
10078
10079   offsets->outgoing_args = offsets->soft_frame + frame_size
10080                            + current_function_outgoing_args_size;
10081
10082   if (ARM_DOUBLEWORD_ALIGN)
10083     {
10084       /* Ensure SP remains doubleword aligned.  */
10085       if (offsets->outgoing_args & 7)
10086         offsets->outgoing_args += 4;
10087       if (offsets->outgoing_args & 7)
10088         abort ();
10089     }
10090
10091   return offsets;
10092 }
10093
10094
10095 /* Calculate the relative offsets for the different stack pointers.  Positive
10096    offsets are in the direction of stack growth.  */
10097
10098 unsigned int
10099 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10100 {
10101   arm_stack_offsets *offsets;
10102
10103   offsets = arm_get_frame_offsets ();
10104
10105   /* OK, now we have enough information to compute the distances.
10106      There must be an entry in these switch tables for each pair
10107      of registers in ELIMINABLE_REGS, even if some of the entries
10108      seem to be redundant or useless.  */
10109   switch (from)
10110     {
10111     case ARG_POINTER_REGNUM:
10112       switch (to)
10113         {
10114         case THUMB_HARD_FRAME_POINTER_REGNUM:
10115           return 0;
10116
10117         case FRAME_POINTER_REGNUM:
10118           /* This is the reverse of the soft frame pointer
10119              to hard frame pointer elimination below.  */
10120           return offsets->soft_frame - offsets->saved_args;
10121
10122         case ARM_HARD_FRAME_POINTER_REGNUM:
10123           /* If there is no stack frame then the hard
10124              frame pointer and the arg pointer coincide.  */
10125           if (offsets->frame == offsets->saved_regs)
10126             return 0;
10127           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10128           return (frame_pointer_needed
10129                   && current_function_needs_context
10130                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10131
10132         case STACK_POINTER_REGNUM:
10133           /* If nothing has been pushed on the stack at all
10134              then this will return -4.  This *is* correct!  */
10135           return offsets->outgoing_args - (offsets->saved_args + 4);
10136
10137         default:
10138           abort ();
10139         }
10140       break;
10141
10142     case FRAME_POINTER_REGNUM:
10143       switch (to)
10144         {
10145         case THUMB_HARD_FRAME_POINTER_REGNUM:
10146           return 0;
10147
10148         case ARM_HARD_FRAME_POINTER_REGNUM:
10149           /* The hard frame pointer points to the top entry in the
10150              stack frame.  The soft frame pointer to the bottom entry
10151              in the stack frame.  If there is no stack frame at all,
10152              then they are identical.  */
10153
10154           return offsets->frame - offsets->soft_frame;
10155
10156         case STACK_POINTER_REGNUM:
10157           return offsets->outgoing_args - offsets->soft_frame;
10158
10159         default:
10160           abort ();
10161         }
10162       break;
10163
10164     default:
10165       /* You cannot eliminate from the stack pointer.
10166          In theory you could eliminate from the hard frame
10167          pointer to the stack pointer, but this will never
10168          happen, since if a stack frame is not needed the
10169          hard frame pointer will never be used.  */
10170       abort ();
10171     }
10172 }
10173
10174
10175 /* Generate the prologue instructions for entry into an ARM function.  */
10176 void
10177 arm_expand_prologue (void)
10178 {
10179   int reg;
10180   rtx amount;
10181   rtx insn;
10182   rtx ip_rtx;
10183   unsigned long live_regs_mask;
10184   unsigned long func_type;
10185   int fp_offset = 0;
10186   int saved_pretend_args = 0;
10187   int saved_regs = 0;
10188   unsigned int args_to_push;
10189   arm_stack_offsets *offsets;
10190
10191   func_type = arm_current_func_type ();
10192
10193   /* Naked functions don't have prologues.  */
10194   if (IS_NAKED (func_type))
10195     return;
10196
10197   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10198   args_to_push = current_function_pretend_args_size;
10199   
10200   /* Compute which register we will have to save onto the stack.  */
10201   live_regs_mask = arm_compute_save_reg_mask ();
10202
10203   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10204
10205   if (frame_pointer_needed)
10206     {
10207       if (IS_INTERRUPT (func_type))
10208         {
10209           /* Interrupt functions must not corrupt any registers.
10210              Creating a frame pointer however, corrupts the IP
10211              register, so we must push it first.  */
10212           insn = emit_multi_reg_push (1 << IP_REGNUM);
10213
10214           /* Do not set RTX_FRAME_RELATED_P on this insn.
10215              The dwarf stack unwinding code only wants to see one
10216              stack decrement per function, and this is not it.  If
10217              this instruction is labeled as being part of the frame
10218              creation sequence then dwarf2out_frame_debug_expr will
10219              abort when it encounters the assignment of IP to FP
10220              later on, since the use of SP here establishes SP as
10221              the CFA register and not IP.
10222
10223              Anyway this instruction is not really part of the stack
10224              frame creation although it is part of the prologue.  */
10225         }
10226       else if (IS_NESTED (func_type))
10227         {
10228           /* The Static chain register is the same as the IP register
10229              used as a scratch register during stack frame creation.
10230              To get around this need to find somewhere to store IP
10231              whilst the frame is being created.  We try the following
10232              places in order:
10233              
10234                1. The last argument register.
10235                2. A slot on the stack above the frame.  (This only
10236                   works if the function is not a varargs function).
10237                3. Register r3, after pushing the argument registers
10238                   onto the stack.
10239
10240              Note - we only need to tell the dwarf2 backend about the SP
10241              adjustment in the second variant; the static chain register
10242              doesn't need to be unwound, as it doesn't contain a value
10243              inherited from the caller.  */
10244
10245           if (regs_ever_live[3] == 0)
10246             {
10247               insn = gen_rtx_REG (SImode, 3);
10248               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10249               insn = emit_insn (insn);
10250             }
10251           else if (args_to_push == 0)
10252             {
10253               rtx dwarf;
10254               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10255               insn = gen_rtx_MEM (SImode, insn);
10256               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10257               insn = emit_insn (insn);
10258
10259               fp_offset = 4;
10260
10261               /* Just tell the dwarf backend that we adjusted SP.  */
10262               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10263                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10264                                                  GEN_INT (-fp_offset)));
10265               RTX_FRAME_RELATED_P (insn) = 1;
10266               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10267                                                     dwarf, REG_NOTES (insn));
10268             }
10269           else
10270             {
10271               /* Store the args on the stack.  */
10272               if (cfun->machine->uses_anonymous_args)
10273                 insn = emit_multi_reg_push
10274                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10275               else
10276                 insn = emit_insn
10277                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10278                                GEN_INT (- args_to_push)));
10279
10280               RTX_FRAME_RELATED_P (insn) = 1;
10281
10282               saved_pretend_args = 1;
10283               fp_offset = args_to_push;
10284               args_to_push = 0;
10285
10286               /* Now reuse r3 to preserve IP.  */
10287               insn = gen_rtx_REG (SImode, 3);
10288               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10289               (void) emit_insn (insn);
10290             }
10291         }
10292
10293       if (fp_offset)
10294         {
10295           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10296           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10297         }
10298       else
10299         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10300       
10301       insn = emit_insn (insn);
10302       RTX_FRAME_RELATED_P (insn) = 1;
10303     }
10304
10305   if (args_to_push)
10306     {
10307       /* Push the argument registers, or reserve space for them.  */
10308       if (cfun->machine->uses_anonymous_args)
10309         insn = emit_multi_reg_push
10310           ((0xf0 >> (args_to_push / 4)) & 0xf);
10311       else
10312         insn = emit_insn
10313           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10314                        GEN_INT (- args_to_push)));
10315       RTX_FRAME_RELATED_P (insn) = 1;
10316     }
10317
10318   /* If this is an interrupt service routine, and the link register
10319      is going to be pushed, and we are not creating a stack frame,
10320      (which would involve an extra push of IP and a pop in the epilogue)
10321      subtracting four from LR now will mean that the function return
10322      can be done with a single instruction.  */
10323   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10324       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10325       && ! frame_pointer_needed)
10326     emit_insn (gen_rtx_SET (SImode, 
10327                             gen_rtx_REG (SImode, LR_REGNUM),
10328                             gen_rtx_PLUS (SImode,
10329                                           gen_rtx_REG (SImode, LR_REGNUM),
10330                                           GEN_INT (-4))));
10331
10332   if (live_regs_mask)
10333     {
10334       insn = emit_multi_reg_push (live_regs_mask);
10335       saved_regs += bit_count (live_regs_mask) * 4;
10336       RTX_FRAME_RELATED_P (insn) = 1;
10337     }
10338
10339   if (TARGET_IWMMXT)
10340     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10341       if (regs_ever_live[reg] && ! call_used_regs [reg])
10342         {
10343           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10344           insn = gen_rtx_MEM (V2SImode, insn);
10345           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10346                                          gen_rtx_REG (V2SImode, reg)));
10347           RTX_FRAME_RELATED_P (insn) = 1;
10348           saved_regs += 8;
10349         }
10350
10351   if (! IS_VOLATILE (func_type))
10352     {
10353       int start_reg;
10354
10355       /* Save any floating point call-saved registers used by this
10356          function.  */
10357       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10358         {
10359           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10360             if (regs_ever_live[reg] && !call_used_regs[reg])
10361               {
10362                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10363                 insn = gen_rtx_MEM (XFmode, insn);
10364                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10365                                                gen_rtx_REG (XFmode, reg)));
10366                 RTX_FRAME_RELATED_P (insn) = 1;
10367                 saved_regs += 12;
10368               }
10369         }
10370       else
10371         {
10372           start_reg = LAST_FPA_REGNUM;
10373
10374           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10375             {
10376               if (regs_ever_live[reg] && !call_used_regs[reg])
10377                 {
10378                   if (start_reg - reg == 3)
10379                     {
10380                       insn = emit_sfm (reg, 4);
10381                       RTX_FRAME_RELATED_P (insn) = 1;
10382                       saved_regs += 48;
10383                       start_reg = reg - 1;
10384                     }
10385                 }
10386               else
10387                 {
10388                   if (start_reg != reg)
10389                     {
10390                       insn = emit_sfm (reg + 1, start_reg - reg);
10391                       RTX_FRAME_RELATED_P (insn) = 1;
10392                       saved_regs += (start_reg - reg) * 12;
10393                     }
10394                   start_reg = reg - 1;
10395                 }
10396             }
10397
10398           if (start_reg != reg)
10399             {
10400               insn = emit_sfm (reg + 1, start_reg - reg);
10401               saved_regs += (start_reg - reg) * 12;
10402               RTX_FRAME_RELATED_P (insn) = 1;
10403             }
10404         }
10405       if (TARGET_HARD_FLOAT && TARGET_VFP)
10406         {
10407           start_reg = FIRST_VFP_REGNUM;
10408
10409           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10410             {
10411               if ((!regs_ever_live[reg] || call_used_regs[reg])
10412                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10413                 {
10414                   if (start_reg != reg)
10415                     saved_regs += vfp_emit_fstmx (start_reg,
10416                                                   (reg - start_reg) / 2);
10417                   start_reg = reg + 2;
10418                 }
10419             }
10420           if (start_reg != reg)
10421             saved_regs += vfp_emit_fstmx (start_reg,
10422                                           (reg - start_reg) / 2);
10423         }
10424     }
10425
10426   if (frame_pointer_needed)
10427     {
10428       /* Create the new frame pointer.  */
10429       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10430       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10431       RTX_FRAME_RELATED_P (insn) = 1;
10432       
10433       if (IS_NESTED (func_type))
10434         {
10435           /* Recover the static chain register.  */
10436           if (regs_ever_live [3] == 0
10437               || saved_pretend_args)
10438             insn = gen_rtx_REG (SImode, 3);
10439           else /* if (current_function_pretend_args_size == 0) */
10440             {
10441               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10442                                    GEN_INT (4));
10443               insn = gen_rtx_MEM (SImode, insn);
10444             }
10445
10446           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10447           /* Add a USE to stop propagate_one_insn() from barfing.  */
10448           emit_insn (gen_prologue_use (ip_rtx));
10449         }
10450     }
10451
10452   offsets = arm_get_frame_offsets ();
10453   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10454     {
10455       /* This add can produce multiple insns for a large constant, so we
10456          need to get tricky.  */
10457       rtx last = get_last_insn ();
10458
10459       amount = GEN_INT (offsets->saved_args + saved_regs
10460                         - offsets->outgoing_args);
10461
10462       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10463                                     amount));
10464       do
10465         {
10466           last = last ? NEXT_INSN (last) : get_insns ();
10467           RTX_FRAME_RELATED_P (last) = 1;
10468         }
10469       while (last != insn);
10470
10471       /* If the frame pointer is needed, emit a special barrier that
10472          will prevent the scheduler from moving stores to the frame
10473          before the stack adjustment.  */
10474       if (frame_pointer_needed)
10475         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10476                                          hard_frame_pointer_rtx));
10477     }
10478
10479   /* If we are profiling, make sure no instructions are scheduled before
10480      the call to mcount.  Similarly if the user has requested no
10481      scheduling in the prolog.  */
10482   if (current_function_profile || TARGET_NO_SCHED_PRO)
10483     emit_insn (gen_blockage ());
10484
10485   /* If the link register is being kept alive, with the return address in it,
10486      then make sure that it does not get reused by the ce2 pass.  */
10487   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10488     {
10489       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10490       cfun->machine->lr_save_eliminated = 1;
10491     }
10492 }
10493 \f
10494 /* If CODE is 'd', then the X is a condition operand and the instruction
10495    should only be executed if the condition is true.
10496    if CODE is 'D', then the X is a condition operand and the instruction
10497    should only be executed if the condition is false: however, if the mode
10498    of the comparison is CCFPEmode, then always execute the instruction -- we
10499    do this because in these circumstances !GE does not necessarily imply LT;
10500    in these cases the instruction pattern will take care to make sure that
10501    an instruction containing %d will follow, thereby undoing the effects of
10502    doing this instruction unconditionally.
10503    If CODE is 'N' then X is a floating point operand that must be negated
10504    before output.
10505    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10506    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10507 void
10508 arm_print_operand (FILE *stream, rtx x, int code)
10509 {
10510   switch (code)
10511     {
10512     case '@':
10513       fputs (ASM_COMMENT_START, stream);
10514       return;
10515
10516     case '_':
10517       fputs (user_label_prefix, stream);
10518       return;
10519           
10520     case '|':
10521       fputs (REGISTER_PREFIX, stream);
10522       return;
10523
10524     case '?':
10525       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10526         {
10527           if (TARGET_THUMB || current_insn_predicate != NULL)
10528             abort ();
10529
10530           fputs (arm_condition_codes[arm_current_cc], stream);
10531         }
10532       else if (current_insn_predicate)
10533         {
10534           enum arm_cond_code code;
10535
10536           if (TARGET_THUMB)
10537             abort ();
10538
10539           code = get_arm_condition_code (current_insn_predicate);
10540           fputs (arm_condition_codes[code], stream);
10541         }
10542       return;
10543
10544     case 'N':
10545       {
10546         REAL_VALUE_TYPE r;
10547         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10548         r = REAL_VALUE_NEGATE (r);
10549         fprintf (stream, "%s", fp_const_from_val (&r));
10550       }
10551       return;
10552
10553     case 'B':
10554       if (GET_CODE (x) == CONST_INT)
10555         {
10556           HOST_WIDE_INT val;
10557           val = ARM_SIGN_EXTEND (~INTVAL (x));
10558           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10559         }
10560       else
10561         {
10562           putc ('~', stream);
10563           output_addr_const (stream, x);
10564         }
10565       return;
10566
10567     case 'i':
10568       fprintf (stream, "%s", arithmetic_instr (x, 1));
10569       return;
10570
10571     /* Truncate Cirrus shift counts.  */
10572     case 's':
10573       if (GET_CODE (x) == CONST_INT)
10574         {
10575           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10576           return;
10577         }
10578       arm_print_operand (stream, x, 0);
10579       return;
10580
10581     case 'I':
10582       fprintf (stream, "%s", arithmetic_instr (x, 0));
10583       return;
10584
10585     case 'S':
10586       {
10587         HOST_WIDE_INT val;
10588         const char * shift = shift_op (x, &val);
10589
10590         if (shift)
10591           {
10592             fprintf (stream, ", %s ", shift_op (x, &val));
10593             if (val == -1)
10594               arm_print_operand (stream, XEXP (x, 1), 0);
10595             else
10596               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10597           }
10598       }
10599       return;
10600
10601       /* An explanation of the 'Q', 'R' and 'H' register operands:
10602          
10603          In a pair of registers containing a DI or DF value the 'Q'
10604          operand returns the register number of the register containing
10605          the least significant part of the value.  The 'R' operand returns
10606          the register number of the register containing the most
10607          significant part of the value.
10608          
10609          The 'H' operand returns the higher of the two register numbers.
10610          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10611          same as the 'Q' operand, since the most significant part of the
10612          value is held in the lower number register.  The reverse is true
10613          on systems where WORDS_BIG_ENDIAN is false.
10614          
10615          The purpose of these operands is to distinguish between cases
10616          where the endian-ness of the values is important (for example
10617          when they are added together), and cases where the endian-ness
10618          is irrelevant, but the order of register operations is important.
10619          For example when loading a value from memory into a register
10620          pair, the endian-ness does not matter.  Provided that the value
10621          from the lower memory address is put into the lower numbered
10622          register, and the value from the higher address is put into the
10623          higher numbered register, the load will work regardless of whether
10624          the value being loaded is big-wordian or little-wordian.  The
10625          order of the two register loads can matter however, if the address
10626          of the memory location is actually held in one of the registers
10627          being overwritten by the load.  */
10628     case 'Q':
10629       if (REGNO (x) > LAST_ARM_REGNUM)
10630         abort ();
10631       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10632       return;
10633
10634     case 'R':
10635       if (REGNO (x) > LAST_ARM_REGNUM)
10636         abort ();
10637       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10638       return;
10639
10640     case 'H':
10641       if (REGNO (x) > LAST_ARM_REGNUM)
10642         abort ();
10643       asm_fprintf (stream, "%r", REGNO (x) + 1);
10644       return;
10645
10646     case 'm':
10647       asm_fprintf (stream, "%r", 
10648                    GET_CODE (XEXP (x, 0)) == REG
10649                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10650       return;
10651
10652     case 'M':
10653       asm_fprintf (stream, "{%r-%r}",
10654                    REGNO (x),
10655                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10656       return;
10657
10658     case 'd':
10659       /* CONST_TRUE_RTX means always -- that's the default.  */
10660       if (x == const_true_rtx)
10661         return;
10662       
10663       fputs (arm_condition_codes[get_arm_condition_code (x)],
10664              stream);
10665       return;
10666
10667     case 'D':
10668       /* CONST_TRUE_RTX means not always -- ie never.  We shouldn't ever
10669          want to do that.  */
10670       if (x == const_true_rtx)
10671         abort ();
10672
10673       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10674                                  (get_arm_condition_code (x))],
10675              stream);
10676       return;
10677
10678     /* Cirrus registers can be accessed in a variety of ways:
10679          single floating point (f)
10680          double floating point (d)
10681          32bit integer         (fx)
10682          64bit integer         (dx).  */
10683     case 'W':                   /* Cirrus register in F mode.  */
10684     case 'X':                   /* Cirrus register in D mode.  */
10685     case 'Y':                   /* Cirrus register in FX mode.  */
10686     case 'Z':                   /* Cirrus register in DX mode.  */
10687       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10688         abort ();
10689
10690       fprintf (stream, "mv%s%s",
10691                code == 'W' ? "f"
10692                : code == 'X' ? "d"
10693                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10694
10695       return;
10696
10697     /* Print cirrus register in the mode specified by the register's mode.  */
10698     case 'V':
10699       {
10700         int mode = GET_MODE (x);
10701
10702         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10703           abort ();
10704
10705         fprintf (stream, "mv%s%s",
10706                  mode == DFmode ? "d"
10707                  : mode == SImode ? "fx"
10708                  : mode == DImode ? "dx"
10709                  : "f", reg_names[REGNO (x)] + 2);
10710
10711         return;
10712       }
10713
10714     case 'U':
10715       if (GET_CODE (x) != REG
10716           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10717           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10718         /* Bad value for wCG register number.  */
10719         abort ();
10720       else
10721         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10722       return;
10723
10724       /* Print an iWMMXt control register name.  */
10725     case 'w':
10726       if (GET_CODE (x) != CONST_INT
10727           || INTVAL (x) < 0
10728           || INTVAL (x) >= 16)
10729         /* Bad value for wC register number.  */
10730         abort ();
10731       else
10732         {
10733           static const char * wc_reg_names [16] =
10734             {
10735               "wCID",  "wCon",  "wCSSF", "wCASF",
10736               "wC4",   "wC5",   "wC6",   "wC7",
10737               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10738               "wC12",  "wC13",  "wC14",  "wC15"
10739             };
10740           
10741           fprintf (stream, wc_reg_names [INTVAL (x)]);
10742         }
10743       return;
10744
10745       /* Print a VFP double precision register name.  */
10746     case 'P':
10747       {
10748         int mode = GET_MODE (x);
10749         int num;
10750
10751         if (mode != DImode && mode != DFmode)
10752           abort ();
10753
10754         if (GET_CODE (x) != REG
10755             || !IS_VFP_REGNUM (REGNO (x)))
10756           abort ();
10757
10758         num = REGNO(x) - FIRST_VFP_REGNUM;
10759         if (num & 1)
10760           abort ();
10761
10762         fprintf (stream, "d%d", num >> 1);
10763       }
10764       return;
10765
10766     default:
10767       if (x == 0)
10768         abort ();
10769
10770       if (GET_CODE (x) == REG)
10771         asm_fprintf (stream, "%r", REGNO (x));
10772       else if (GET_CODE (x) == MEM)
10773         {
10774           output_memory_reference_mode = GET_MODE (x);
10775           output_address (XEXP (x, 0));
10776         }
10777       else if (GET_CODE (x) == CONST_DOUBLE)
10778         fprintf (stream, "#%s", fp_immediate_constant (x));
10779       else if (GET_CODE (x) == NEG)
10780         abort (); /* This should never happen now.  */
10781       else
10782         {
10783           fputc ('#', stream);
10784           output_addr_const (stream, x);
10785         }
10786     }
10787 }
10788 \f
10789 #ifndef AOF_ASSEMBLER
10790 /* Target hook for assembling integer objects.  The ARM version needs to
10791    handle word-sized values specially.  */
10792 static bool
10793 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10794 {
10795   if (size == UNITS_PER_WORD && aligned_p)
10796     {
10797       fputs ("\t.word\t", asm_out_file);
10798       output_addr_const (asm_out_file, x);
10799
10800       /* Mark symbols as position independent.  We only do this in the
10801          .text segment, not in the .data segment.  */
10802       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10803           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10804         {
10805           if (GET_CODE (x) == SYMBOL_REF
10806               && (CONSTANT_POOL_ADDRESS_P (x)
10807                   || SYMBOL_REF_LOCAL_P (x)))
10808             fputs ("(GOTOFF)", asm_out_file);
10809           else if (GET_CODE (x) == LABEL_REF)
10810             fputs ("(GOTOFF)", asm_out_file);
10811           else
10812             fputs ("(GOT)", asm_out_file);
10813         }
10814       fputc ('\n', asm_out_file);
10815       return true;
10816     }
10817
10818   if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10819     {
10820       int i, units;
10821
10822       if (GET_CODE (x) != CONST_VECTOR)
10823         abort ();
10824
10825       units = CONST_VECTOR_NUNITS (x);
10826
10827       switch (GET_MODE (x))
10828         {
10829         case V2SImode: size = 4; break;
10830         case V4HImode: size = 2; break;
10831         case V8QImode: size = 1; break;
10832         default:
10833           abort ();
10834         }
10835
10836       for (i = 0; i < units; i++)
10837         {
10838           rtx elt;
10839
10840           elt = CONST_VECTOR_ELT (x, i);
10841           assemble_integer
10842             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10843         }
10844
10845       return true;
10846     }
10847
10848   return default_assemble_integer (x, size, aligned_p);
10849 }
10850 #endif
10851 \f
10852 /* A finite state machine takes care of noticing whether or not instructions
10853    can be conditionally executed, and thus decrease execution time and code
10854    size by deleting branch instructions.  The fsm is controlled by
10855    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10856
10857 /* The state of the fsm controlling condition codes are:
10858    0: normal, do nothing special
10859    1: make ASM_OUTPUT_OPCODE not output this instruction
10860    2: make ASM_OUTPUT_OPCODE not output this instruction
10861    3: make instructions conditional
10862    4: make instructions conditional
10863
10864    State transitions (state->state by whom under condition):
10865    0 -> 1 final_prescan_insn if the `target' is a label
10866    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10867    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10868    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10869    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10870           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10871    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10872           (the target insn is arm_target_insn).
10873
10874    If the jump clobbers the conditions then we use states 2 and 4.
10875
10876    A similar thing can be done with conditional return insns.
10877
10878    XXX In case the `target' is an unconditional branch, this conditionalising
10879    of the instructions always reduces code size, but not always execution
10880    time.  But then, I want to reduce the code size to somewhere near what
10881    /bin/cc produces.  */
10882
10883 /* Returns the index of the ARM condition code string in
10884    `arm_condition_codes'.  COMPARISON should be an rtx like
10885    `(eq (...) (...))'.  */
10886 static enum arm_cond_code
10887 get_arm_condition_code (rtx comparison)
10888 {
10889   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10890   int code;
10891   enum rtx_code comp_code = GET_CODE (comparison);
10892
10893   if (GET_MODE_CLASS (mode) != MODE_CC)
10894     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10895                            XEXP (comparison, 1));
10896
10897   switch (mode)
10898     {
10899     case CC_DNEmode: code = ARM_NE; goto dominance;
10900     case CC_DEQmode: code = ARM_EQ; goto dominance;
10901     case CC_DGEmode: code = ARM_GE; goto dominance;
10902     case CC_DGTmode: code = ARM_GT; goto dominance;
10903     case CC_DLEmode: code = ARM_LE; goto dominance;
10904     case CC_DLTmode: code = ARM_LT; goto dominance;
10905     case CC_DGEUmode: code = ARM_CS; goto dominance;
10906     case CC_DGTUmode: code = ARM_HI; goto dominance;
10907     case CC_DLEUmode: code = ARM_LS; goto dominance;
10908     case CC_DLTUmode: code = ARM_CC;
10909
10910     dominance:
10911       if (comp_code != EQ && comp_code != NE)
10912         abort ();
10913
10914       if (comp_code == EQ)
10915         return ARM_INVERSE_CONDITION_CODE (code);
10916       return code;
10917
10918     case CC_NOOVmode:
10919       switch (comp_code)
10920         {
10921         case NE: return ARM_NE;
10922         case EQ: return ARM_EQ;
10923         case GE: return ARM_PL;
10924         case LT: return ARM_MI;
10925         default: abort ();
10926         }
10927
10928     case CC_Zmode:
10929       switch (comp_code)
10930         {
10931         case NE: return ARM_NE;
10932         case EQ: return ARM_EQ;
10933         default: abort ();
10934         }
10935
10936     case CC_Nmode:
10937       switch (comp_code)
10938         {
10939         case NE: return ARM_MI;
10940         case EQ: return ARM_PL;
10941         default: abort ();
10942         }
10943
10944     case CCFPEmode:
10945     case CCFPmode:
10946       /* These encodings assume that AC=1 in the FPA system control
10947          byte.  This allows us to handle all cases except UNEQ and
10948          LTGT.  */
10949       switch (comp_code)
10950         {
10951         case GE: return ARM_GE;
10952         case GT: return ARM_GT;
10953         case LE: return ARM_LS;
10954         case LT: return ARM_MI;
10955         case NE: return ARM_NE;
10956         case EQ: return ARM_EQ;
10957         case ORDERED: return ARM_VC;
10958         case UNORDERED: return ARM_VS;
10959         case UNLT: return ARM_LT;
10960         case UNLE: return ARM_LE;
10961         case UNGT: return ARM_HI;
10962         case UNGE: return ARM_PL;
10963           /* UNEQ and LTGT do not have a representation.  */
10964         case UNEQ: /* Fall through.  */
10965         case LTGT: /* Fall through.  */
10966         default: abort ();
10967         }
10968
10969     case CC_SWPmode:
10970       switch (comp_code)
10971         {
10972         case NE: return ARM_NE;
10973         case EQ: return ARM_EQ;
10974         case GE: return ARM_LE;
10975         case GT: return ARM_LT;
10976         case LE: return ARM_GE;
10977         case LT: return ARM_GT;
10978         case GEU: return ARM_LS;
10979         case GTU: return ARM_CC;
10980         case LEU: return ARM_CS;
10981         case LTU: return ARM_HI;
10982         default: abort ();
10983         }
10984
10985     case CC_Cmode:
10986       switch (comp_code)
10987       {
10988       case LTU: return ARM_CS;
10989       case GEU: return ARM_CC;
10990       default: abort ();
10991       }
10992       
10993     case CCmode:
10994       switch (comp_code)
10995         {
10996         case NE: return ARM_NE;
10997         case EQ: return ARM_EQ;
10998         case GE: return ARM_GE;
10999         case GT: return ARM_GT;
11000         case LE: return ARM_LE;
11001         case LT: return ARM_LT;
11002         case GEU: return ARM_CS;
11003         case GTU: return ARM_HI;
11004         case LEU: return ARM_LS;
11005         case LTU: return ARM_CC;
11006         default: abort ();
11007         }
11008
11009     default: abort ();
11010     }
11011
11012   abort ();
11013 }
11014
11015 void
11016 arm_final_prescan_insn (rtx insn)
11017 {
11018   /* BODY will hold the body of INSN.  */
11019   rtx body = PATTERN (insn);
11020
11021   /* This will be 1 if trying to repeat the trick, and things need to be
11022      reversed if it appears to fail.  */
11023   int reverse = 0;
11024
11025   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11026      taken are clobbered, even if the rtl suggests otherwise.  It also
11027      means that we have to grub around within the jump expression to find
11028      out what the conditions are when the jump isn't taken.  */
11029   int jump_clobbers = 0;
11030   
11031   /* If we start with a return insn, we only succeed if we find another one.  */
11032   int seeking_return = 0;
11033   
11034   /* START_INSN will hold the insn from where we start looking.  This is the
11035      first insn after the following code_label if REVERSE is true.  */
11036   rtx start_insn = insn;
11037
11038   /* If in state 4, check if the target branch is reached, in order to
11039      change back to state 0.  */
11040   if (arm_ccfsm_state == 4)
11041     {
11042       if (insn == arm_target_insn)
11043         {
11044           arm_target_insn = NULL;
11045           arm_ccfsm_state = 0;
11046         }
11047       return;
11048     }
11049
11050   /* If in state 3, it is possible to repeat the trick, if this insn is an
11051      unconditional branch to a label, and immediately following this branch
11052      is the previous target label which is only used once, and the label this
11053      branch jumps to is not too far off.  */
11054   if (arm_ccfsm_state == 3)
11055     {
11056       if (simplejump_p (insn))
11057         {
11058           start_insn = next_nonnote_insn (start_insn);
11059           if (GET_CODE (start_insn) == BARRIER)
11060             {
11061               /* XXX Isn't this always a barrier?  */
11062               start_insn = next_nonnote_insn (start_insn);
11063             }
11064           if (GET_CODE (start_insn) == CODE_LABEL
11065               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11066               && LABEL_NUSES (start_insn) == 1)
11067             reverse = TRUE;
11068           else
11069             return;
11070         }
11071       else if (GET_CODE (body) == RETURN)
11072         {
11073           start_insn = next_nonnote_insn (start_insn);
11074           if (GET_CODE (start_insn) == BARRIER)
11075             start_insn = next_nonnote_insn (start_insn);
11076           if (GET_CODE (start_insn) == CODE_LABEL
11077               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11078               && LABEL_NUSES (start_insn) == 1)
11079             {
11080               reverse = TRUE;
11081               seeking_return = 1;
11082             }
11083           else
11084             return;
11085         }
11086       else
11087         return;
11088     }
11089
11090   if (arm_ccfsm_state != 0 && !reverse)
11091     abort ();
11092   if (GET_CODE (insn) != JUMP_INSN)
11093     return;
11094
11095   /* This jump might be paralleled with a clobber of the condition codes 
11096      the jump should always come first */
11097   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11098     body = XVECEXP (body, 0, 0);
11099
11100   if (reverse
11101       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11102           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11103     {
11104       int insns_skipped;
11105       int fail = FALSE, succeed = FALSE;
11106       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11107       int then_not_else = TRUE;
11108       rtx this_insn = start_insn, label = 0;
11109
11110       /* If the jump cannot be done with one instruction, we cannot 
11111          conditionally execute the instruction in the inverse case.  */
11112       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11113         {
11114           jump_clobbers = 1;
11115           return;
11116         }
11117       
11118       /* Register the insn jumped to.  */
11119       if (reverse)
11120         {
11121           if (!seeking_return)
11122             label = XEXP (SET_SRC (body), 0);
11123         }
11124       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11125         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11126       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11127         {
11128           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11129           then_not_else = FALSE;
11130         }
11131       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11132         seeking_return = 1;
11133       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11134         {
11135           seeking_return = 1;
11136           then_not_else = FALSE;
11137         }
11138       else
11139         abort ();
11140
11141       /* See how many insns this branch skips, and what kind of insns.  If all
11142          insns are okay, and the label or unconditional branch to the same
11143          label is not too far away, succeed.  */
11144       for (insns_skipped = 0;
11145            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11146         {
11147           rtx scanbody;
11148
11149           this_insn = next_nonnote_insn (this_insn);
11150           if (!this_insn)
11151             break;
11152
11153           switch (GET_CODE (this_insn))
11154             {
11155             case CODE_LABEL:
11156               /* Succeed if it is the target label, otherwise fail since
11157                  control falls in from somewhere else.  */
11158               if (this_insn == label)
11159                 {
11160                   if (jump_clobbers)
11161                     {
11162                       arm_ccfsm_state = 2;
11163                       this_insn = next_nonnote_insn (this_insn);
11164                     }
11165                   else
11166                     arm_ccfsm_state = 1;
11167                   succeed = TRUE;
11168                 }
11169               else
11170                 fail = TRUE;
11171               break;
11172
11173             case BARRIER:
11174               /* Succeed if the following insn is the target label.
11175                  Otherwise fail.  
11176                  If return insns are used then the last insn in a function 
11177                  will be a barrier.  */
11178               this_insn = next_nonnote_insn (this_insn);
11179               if (this_insn && this_insn == label)
11180                 {
11181                   if (jump_clobbers)
11182                     {
11183                       arm_ccfsm_state = 2;
11184                       this_insn = next_nonnote_insn (this_insn);
11185                     }
11186                   else
11187                     arm_ccfsm_state = 1;
11188                   succeed = TRUE;
11189                 }
11190               else
11191                 fail = TRUE;
11192               break;
11193
11194             case CALL_INSN:
11195               /* If using 32-bit addresses the cc is not preserved over
11196                  calls.  */
11197               if (TARGET_APCS_32)
11198                 {
11199                   /* Succeed if the following insn is the target label,
11200                      or if the following two insns are a barrier and
11201                      the target label.  */
11202                   this_insn = next_nonnote_insn (this_insn);
11203                   if (this_insn && GET_CODE (this_insn) == BARRIER)
11204                     this_insn = next_nonnote_insn (this_insn);
11205
11206                   if (this_insn && this_insn == label
11207                       && insns_skipped < max_insns_skipped)
11208                     {
11209                       if (jump_clobbers)
11210                         {
11211                           arm_ccfsm_state = 2;
11212                           this_insn = next_nonnote_insn (this_insn);
11213                         }
11214                       else
11215                         arm_ccfsm_state = 1;
11216                       succeed = TRUE;
11217                     }
11218                   else
11219                     fail = TRUE;
11220                 }
11221               break;
11222
11223             case JUMP_INSN:
11224               /* If this is an unconditional branch to the same label, succeed.
11225                  If it is to another label, do nothing.  If it is conditional,
11226                  fail.  */
11227               /* XXX Probably, the tests for SET and the PC are
11228                  unnecessary.  */
11229
11230               scanbody = PATTERN (this_insn);
11231               if (GET_CODE (scanbody) == SET
11232                   && GET_CODE (SET_DEST (scanbody)) == PC)
11233                 {
11234                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11235                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11236                     {
11237                       arm_ccfsm_state = 2;
11238                       succeed = TRUE;
11239                     }
11240                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11241                     fail = TRUE;
11242                 }
11243               /* Fail if a conditional return is undesirable (eg on a
11244                  StrongARM), but still allow this if optimizing for size.  */
11245               else if (GET_CODE (scanbody) == RETURN
11246                        && !use_return_insn (TRUE, NULL)
11247                        && !optimize_size)
11248                 fail = TRUE;
11249               else if (GET_CODE (scanbody) == RETURN
11250                        && seeking_return)
11251                 {
11252                   arm_ccfsm_state = 2;
11253                   succeed = TRUE;
11254                 }
11255               else if (GET_CODE (scanbody) == PARALLEL)
11256                 {
11257                   switch (get_attr_conds (this_insn))
11258                     {
11259                     case CONDS_NOCOND:
11260                       break;
11261                     default:
11262                       fail = TRUE;
11263                       break;
11264                     }
11265                 }
11266               else
11267                 fail = TRUE;    /* Unrecognized jump (eg epilogue).  */
11268
11269               break;
11270
11271             case INSN:
11272               /* Instructions using or affecting the condition codes make it
11273                  fail.  */
11274               scanbody = PATTERN (this_insn);
11275               if (!(GET_CODE (scanbody) == SET
11276                     || GET_CODE (scanbody) == PARALLEL)
11277                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11278                 fail = TRUE;
11279
11280               /* A conditional cirrus instruction must be followed by
11281                  a non Cirrus instruction.  However, since we
11282                  conditionalize instructions in this function and by
11283                  the time we get here we can't add instructions
11284                  (nops), because shorten_branches() has already been
11285                  called, we will disable conditionalizing Cirrus
11286                  instructions to be safe.  */
11287               if (GET_CODE (scanbody) != USE
11288                   && GET_CODE (scanbody) != CLOBBER
11289                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11290                 fail = TRUE;
11291               break;
11292
11293             default:
11294               break;
11295             }
11296         }
11297       if (succeed)
11298         {
11299           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11300             arm_target_label = CODE_LABEL_NUMBER (label);
11301           else if (seeking_return || arm_ccfsm_state == 2)
11302             {
11303               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11304                 {
11305                   this_insn = next_nonnote_insn (this_insn);
11306                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11307                                     || GET_CODE (this_insn) == CODE_LABEL))
11308                     abort ();
11309                 }
11310               if (!this_insn)
11311                 {
11312                   /* Oh, dear! we ran off the end.. give up.  */
11313                   recog (PATTERN (insn), insn, NULL);
11314                   arm_ccfsm_state = 0;
11315                   arm_target_insn = NULL;
11316                   return;
11317                 }
11318               arm_target_insn = this_insn;
11319             }
11320           else
11321             abort ();
11322           if (jump_clobbers)
11323             {
11324               if (reverse)
11325                 abort ();
11326               arm_current_cc = 
11327                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11328                                                             0), 0), 1));
11329               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11330                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11331               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11332                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11333             }
11334           else
11335             {
11336               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11337                  what it was.  */
11338               if (!reverse)
11339                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11340                                                                0));
11341             }
11342
11343           if (reverse || then_not_else)
11344             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11345         }
11346       
11347       /* Restore recog_data (getting the attributes of other insns can
11348          destroy this array, but final.c assumes that it remains intact
11349          across this call; since the insn has been recognized already we
11350          call recog direct).  */
11351       recog (PATTERN (insn), insn, NULL);
11352     }
11353 }
11354
11355 /* Returns true if REGNO is a valid register
11356    for holding a quantity of tyoe MODE.  */
11357 int
11358 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11359 {
11360   if (GET_MODE_CLASS (mode) == MODE_CC)
11361     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11362   
11363   if (TARGET_THUMB)
11364     /* For the Thumb we only allow values bigger than SImode in
11365        registers 0 - 6, so that there is always a second low
11366        register available to hold the upper part of the value.
11367        We probably we ought to ensure that the register is the
11368        start of an even numbered register pair.  */
11369     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11370
11371   if (IS_CIRRUS_REGNUM (regno))
11372     /* We have outlawed SI values in Cirrus registers because they
11373        reside in the lower 32 bits, but SF values reside in the
11374        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11375        even split the registers into pairs because Cirrus SI values
11376        get sign extended to 64bits-- aldyh.  */
11377     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11378
11379   if (IS_VFP_REGNUM (regno))
11380     {
11381       if (mode == SFmode || mode == SImode)
11382         return TRUE;
11383
11384       /* DFmode values are only valid in even register pairs.  */
11385       if (mode == DFmode)
11386         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11387       return FALSE;
11388     }
11389
11390   if (IS_IWMMXT_GR_REGNUM (regno))
11391     return mode == SImode;
11392
11393   if (IS_IWMMXT_REGNUM (regno))
11394     return VALID_IWMMXT_REG_MODE (mode);
11395
11396   if (regno <= LAST_ARM_REGNUM)
11397     /* We allow any value to be stored in the general registers.  */
11398     return 1;
11399
11400   if (   regno == FRAME_POINTER_REGNUM
11401       || regno == ARG_POINTER_REGNUM)
11402     /* We only allow integers in the fake hard registers.  */
11403     return GET_MODE_CLASS (mode) == MODE_INT;
11404
11405   /* The only registers left are the FPA registers
11406      which we only allow to hold FP values.  */
11407   return GET_MODE_CLASS (mode) == MODE_FLOAT
11408     && regno >= FIRST_FPA_REGNUM
11409     && regno <= LAST_FPA_REGNUM;
11410 }
11411
11412 int
11413 arm_regno_class (int regno)
11414 {
11415   if (TARGET_THUMB)
11416     {
11417       if (regno == STACK_POINTER_REGNUM)
11418         return STACK_REG;
11419       if (regno == CC_REGNUM)
11420         return CC_REG;
11421       if (regno < 8)
11422         return LO_REGS;
11423       return HI_REGS;
11424     }
11425
11426   if (   regno <= LAST_ARM_REGNUM
11427       || regno == FRAME_POINTER_REGNUM
11428       || regno == ARG_POINTER_REGNUM)
11429     return GENERAL_REGS;
11430   
11431   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11432     return NO_REGS;
11433
11434   if (IS_CIRRUS_REGNUM (regno))
11435     return CIRRUS_REGS;
11436
11437   if (IS_VFP_REGNUM (regno))
11438     return VFP_REGS;
11439
11440   if (IS_IWMMXT_REGNUM (regno))
11441     return IWMMXT_REGS;
11442
11443   if (IS_IWMMXT_GR_REGNUM (regno))
11444     return IWMMXT_GR_REGS;
11445
11446   return FPA_REGS;
11447 }
11448
11449 /* Handle a special case when computing the offset
11450    of an argument from the frame pointer.  */
11451 int
11452 arm_debugger_arg_offset (int value, rtx addr)
11453 {
11454   rtx insn;
11455
11456   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11457   if (value != 0)
11458     return 0;
11459
11460   /* We can only cope with the case where the address is held in a register.  */
11461   if (GET_CODE (addr) != REG)
11462     return 0;
11463
11464   /* If we are using the frame pointer to point at the argument, then
11465      an offset of 0 is correct.  */
11466   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11467     return 0;
11468   
11469   /* If we are using the stack pointer to point at the
11470      argument, then an offset of 0 is correct.  */
11471   if ((TARGET_THUMB || !frame_pointer_needed)
11472       && REGNO (addr) == SP_REGNUM)
11473     return 0;
11474   
11475   /* Oh dear.  The argument is pointed to by a register rather
11476      than being held in a register, or being stored at a known
11477      offset from the frame pointer.  Since GDB only understands
11478      those two kinds of argument we must translate the address
11479      held in the register into an offset from the frame pointer.
11480      We do this by searching through the insns for the function
11481      looking to see where this register gets its value.  If the
11482      register is initialized from the frame pointer plus an offset
11483      then we are in luck and we can continue, otherwise we give up.
11484      
11485      This code is exercised by producing debugging information
11486      for a function with arguments like this:
11487      
11488            double func (double a, double b, int c, double d) {return d;}
11489      
11490      Without this code the stab for parameter 'd' will be set to
11491      an offset of 0 from the frame pointer, rather than 8.  */
11492
11493   /* The if() statement says:
11494
11495      If the insn is a normal instruction
11496      and if the insn is setting the value in a register
11497      and if the register being set is the register holding the address of the argument
11498      and if the address is computing by an addition
11499      that involves adding to a register
11500      which is the frame pointer
11501      a constant integer
11502
11503      then...  */
11504   
11505   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11506     {
11507       if (   GET_CODE (insn) == INSN 
11508           && GET_CODE (PATTERN (insn)) == SET
11509           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11510           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11511           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11512           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11513           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11514              )
11515         {
11516           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11517           
11518           break;
11519         }
11520     }
11521   
11522   if (value == 0)
11523     {
11524       debug_rtx (addr);
11525       warning ("unable to compute real location of stacked parameter");
11526       value = 8; /* XXX magic hack */
11527     }
11528
11529   return value;
11530 }
11531 \f
11532 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11533   do                                                                    \
11534     {                                                                   \
11535       if ((MASK) & insn_flags)                                          \
11536         builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, NULL_TREE);        \
11537     }                                                                   \
11538   while (0)
11539
11540 struct builtin_description
11541 {
11542   const unsigned int       mask;
11543   const enum insn_code     icode;
11544   const char * const       name;
11545   const enum arm_builtins  code;
11546   const enum rtx_code      comparison;
11547   const unsigned int       flag;
11548 };
11549
11550 static const struct builtin_description bdesc_2arg[] =
11551 {
11552 #define IWMMXT_BUILTIN(code, string, builtin) \
11553   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11554     ARM_BUILTIN_##builtin, 0, 0 },
11555
11556   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11557   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11558   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11559   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11560   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11561   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11562   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11563   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11564   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11565   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11566   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11567   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11568   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11569   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11570   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11571   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11572   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11573   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11574   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11575   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11576   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11577   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11578   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11579   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11580   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11581   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11582   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11583   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11584   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11585   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11586   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11587   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11588   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11589   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11590   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11591   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11592   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11593   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11594   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11595   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11596   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11597   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11598   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11599   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11600   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11601   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11602   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11603   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11604   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11605   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11606   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11607   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11608   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11609   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11610   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11611   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11612   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11613   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11614
11615 #define IWMMXT_BUILTIN2(code, builtin) \
11616   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11617   
11618   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11619   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11620   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11621   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11622   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11623   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11624   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11625   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11626   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11627   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11628   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11629   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11630   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11631   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11632   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11633   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11634   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11635   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11636   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11637   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11638   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11639   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11640   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11641   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11642   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11643   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11644   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11645   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11646   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11647   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11648   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11649   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11650 };
11651
11652 static const struct builtin_description bdesc_1arg[] =
11653 {
11654   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11655   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11656   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11657   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11658   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11659   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11660   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11661   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11662   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11663   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11664   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11665   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11666   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11667   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11668   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11669   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11670   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11671   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11672 };
11673
11674 /* Set up all the iWMMXt builtins.  This is
11675    not called if TARGET_IWMMXT is zero.  */
11676
11677 static void
11678 arm_init_iwmmxt_builtins (void)
11679 {
11680   const struct builtin_description * d;
11681   size_t i;
11682   tree endlink = void_list_node;
11683
11684   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11685   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11686   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11687
11688   tree int_ftype_int
11689     = build_function_type (integer_type_node,
11690                            tree_cons (NULL_TREE, integer_type_node, endlink));
11691   tree v8qi_ftype_v8qi_v8qi_int
11692     = build_function_type (V8QI_type_node,
11693                            tree_cons (NULL_TREE, V8QI_type_node,
11694                                       tree_cons (NULL_TREE, V8QI_type_node,
11695                                                  tree_cons (NULL_TREE,
11696                                                             integer_type_node,
11697                                                             endlink))));
11698   tree v4hi_ftype_v4hi_int
11699     = build_function_type (V4HI_type_node,
11700                            tree_cons (NULL_TREE, V4HI_type_node,
11701                                       tree_cons (NULL_TREE, integer_type_node,
11702                                                  endlink)));
11703   tree v2si_ftype_v2si_int
11704     = build_function_type (V2SI_type_node,
11705                            tree_cons (NULL_TREE, V2SI_type_node,
11706                                       tree_cons (NULL_TREE, integer_type_node,
11707                                                  endlink)));
11708   tree v2si_ftype_di_di
11709     = build_function_type (V2SI_type_node,
11710                            tree_cons (NULL_TREE, long_long_integer_type_node,
11711                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11712                                                  endlink)));
11713   tree di_ftype_di_int
11714     = build_function_type (long_long_integer_type_node,
11715                            tree_cons (NULL_TREE, long_long_integer_type_node,
11716                                       tree_cons (NULL_TREE, integer_type_node,
11717                                                  endlink)));
11718   tree di_ftype_di_int_int
11719     = build_function_type (long_long_integer_type_node,
11720                            tree_cons (NULL_TREE, long_long_integer_type_node,
11721                                       tree_cons (NULL_TREE, integer_type_node,
11722                                                  tree_cons (NULL_TREE,
11723                                                             integer_type_node,
11724                                                             endlink))));
11725   tree int_ftype_v8qi
11726     = build_function_type (integer_type_node,
11727                            tree_cons (NULL_TREE, V8QI_type_node,
11728                                       endlink));
11729   tree int_ftype_v4hi
11730     = build_function_type (integer_type_node,
11731                            tree_cons (NULL_TREE, V4HI_type_node,
11732                                       endlink));
11733   tree int_ftype_v2si
11734     = build_function_type (integer_type_node,
11735                            tree_cons (NULL_TREE, V2SI_type_node,
11736                                       endlink));
11737   tree int_ftype_v8qi_int
11738     = build_function_type (integer_type_node,
11739                            tree_cons (NULL_TREE, V8QI_type_node,
11740                                       tree_cons (NULL_TREE, integer_type_node,
11741                                                  endlink)));
11742   tree int_ftype_v4hi_int
11743     = build_function_type (integer_type_node,
11744                            tree_cons (NULL_TREE, V4HI_type_node,
11745                                       tree_cons (NULL_TREE, integer_type_node,
11746                                                  endlink)));
11747   tree int_ftype_v2si_int
11748     = build_function_type (integer_type_node,
11749                            tree_cons (NULL_TREE, V2SI_type_node,
11750                                       tree_cons (NULL_TREE, integer_type_node,
11751                                                  endlink)));
11752   tree v8qi_ftype_v8qi_int_int
11753     = build_function_type (V8QI_type_node,
11754                            tree_cons (NULL_TREE, V8QI_type_node,
11755                                       tree_cons (NULL_TREE, integer_type_node,
11756                                                  tree_cons (NULL_TREE,
11757                                                             integer_type_node,
11758                                                             endlink))));
11759   tree v4hi_ftype_v4hi_int_int
11760     = build_function_type (V4HI_type_node,
11761                            tree_cons (NULL_TREE, V4HI_type_node,
11762                                       tree_cons (NULL_TREE, integer_type_node,
11763                                                  tree_cons (NULL_TREE,
11764                                                             integer_type_node,
11765                                                             endlink))));
11766   tree v2si_ftype_v2si_int_int
11767     = build_function_type (V2SI_type_node,
11768                            tree_cons (NULL_TREE, V2SI_type_node,
11769                                       tree_cons (NULL_TREE, integer_type_node,
11770                                                  tree_cons (NULL_TREE,
11771                                                             integer_type_node,
11772                                                             endlink))));
11773   /* Miscellaneous.  */
11774   tree v8qi_ftype_v4hi_v4hi
11775     = build_function_type (V8QI_type_node,
11776                            tree_cons (NULL_TREE, V4HI_type_node,
11777                                       tree_cons (NULL_TREE, V4HI_type_node,
11778                                                  endlink)));
11779   tree v4hi_ftype_v2si_v2si
11780     = build_function_type (V4HI_type_node,
11781                            tree_cons (NULL_TREE, V2SI_type_node,
11782                                       tree_cons (NULL_TREE, V2SI_type_node,
11783                                                  endlink)));
11784   tree v2si_ftype_v4hi_v4hi
11785     = build_function_type (V2SI_type_node,
11786                            tree_cons (NULL_TREE, V4HI_type_node,
11787                                       tree_cons (NULL_TREE, V4HI_type_node,
11788                                                  endlink)));
11789   tree v2si_ftype_v8qi_v8qi
11790     = build_function_type (V2SI_type_node,
11791                            tree_cons (NULL_TREE, V8QI_type_node,
11792                                       tree_cons (NULL_TREE, V8QI_type_node,
11793                                                  endlink)));
11794   tree v4hi_ftype_v4hi_di
11795     = build_function_type (V4HI_type_node,
11796                            tree_cons (NULL_TREE, V4HI_type_node,
11797                                       tree_cons (NULL_TREE,
11798                                                  long_long_integer_type_node,
11799                                                  endlink)));
11800   tree v2si_ftype_v2si_di
11801     = build_function_type (V2SI_type_node,
11802                            tree_cons (NULL_TREE, V2SI_type_node,
11803                                       tree_cons (NULL_TREE,
11804                                                  long_long_integer_type_node,
11805                                                  endlink)));
11806   tree void_ftype_int_int
11807     = build_function_type (void_type_node,
11808                            tree_cons (NULL_TREE, integer_type_node,
11809                                       tree_cons (NULL_TREE, integer_type_node,
11810                                                  endlink)));
11811   tree di_ftype_void
11812     = build_function_type (long_long_unsigned_type_node, endlink);
11813   tree di_ftype_v8qi
11814     = build_function_type (long_long_integer_type_node,
11815                            tree_cons (NULL_TREE, V8QI_type_node,
11816                                       endlink));
11817   tree di_ftype_v4hi
11818     = build_function_type (long_long_integer_type_node,
11819                            tree_cons (NULL_TREE, V4HI_type_node,
11820                                       endlink));
11821   tree di_ftype_v2si
11822     = build_function_type (long_long_integer_type_node,
11823                            tree_cons (NULL_TREE, V2SI_type_node,
11824                                       endlink));
11825   tree v2si_ftype_v4hi
11826     = build_function_type (V2SI_type_node,
11827                            tree_cons (NULL_TREE, V4HI_type_node,
11828                                       endlink));
11829   tree v4hi_ftype_v8qi
11830     = build_function_type (V4HI_type_node,
11831                            tree_cons (NULL_TREE, V8QI_type_node,
11832                                       endlink));
11833
11834   tree di_ftype_di_v4hi_v4hi
11835     = build_function_type (long_long_unsigned_type_node,
11836                            tree_cons (NULL_TREE,
11837                                       long_long_unsigned_type_node,
11838                                       tree_cons (NULL_TREE, V4HI_type_node,
11839                                                  tree_cons (NULL_TREE,
11840                                                             V4HI_type_node,
11841                                                             endlink))));
11842
11843   tree di_ftype_v4hi_v4hi
11844     = build_function_type (long_long_unsigned_type_node,
11845                            tree_cons (NULL_TREE, V4HI_type_node,
11846                                       tree_cons (NULL_TREE, V4HI_type_node,
11847                                                  endlink)));
11848
11849   /* Normal vector binops.  */
11850   tree v8qi_ftype_v8qi_v8qi
11851     = build_function_type (V8QI_type_node,
11852                            tree_cons (NULL_TREE, V8QI_type_node,
11853                                       tree_cons (NULL_TREE, V8QI_type_node,
11854                                                  endlink)));
11855   tree v4hi_ftype_v4hi_v4hi
11856     = build_function_type (V4HI_type_node,
11857                            tree_cons (NULL_TREE, V4HI_type_node,
11858                                       tree_cons (NULL_TREE, V4HI_type_node,
11859                                                  endlink)));
11860   tree v2si_ftype_v2si_v2si
11861     = build_function_type (V2SI_type_node,
11862                            tree_cons (NULL_TREE, V2SI_type_node,
11863                                       tree_cons (NULL_TREE, V2SI_type_node,
11864                                                  endlink)));
11865   tree di_ftype_di_di
11866     = build_function_type (long_long_unsigned_type_node,
11867                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11868                                       tree_cons (NULL_TREE,
11869                                                  long_long_unsigned_type_node,
11870                                                  endlink)));
11871
11872   /* Add all builtins that are more or less simple operations on two
11873      operands.  */
11874   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11875     {
11876       /* Use one of the operands; the target can have a different mode for
11877          mask-generating compares.  */
11878       enum machine_mode mode;
11879       tree type;
11880
11881       if (d->name == 0)
11882         continue;
11883
11884       mode = insn_data[d->icode].operand[1].mode;
11885
11886       switch (mode)
11887         {
11888         case V8QImode:
11889           type = v8qi_ftype_v8qi_v8qi;
11890           break;
11891         case V4HImode:
11892           type = v4hi_ftype_v4hi_v4hi;
11893           break;
11894         case V2SImode:
11895           type = v2si_ftype_v2si_v2si;
11896           break;
11897         case DImode:
11898           type = di_ftype_di_di;
11899           break;
11900
11901         default:
11902           abort ();
11903         }
11904
11905       def_mbuiltin (d->mask, d->name, type, d->code);
11906     }
11907
11908   /* Add the remaining MMX insns with somewhat more complicated types.  */
11909   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11910   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11911   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11912
11913   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11914   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11915   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11916   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11917   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11918   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11919
11920   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11921   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11922   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11923   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11924   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11925   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11926
11927   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11928   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11929   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11930   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11931   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11932   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11933
11934   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11935   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11936   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11937   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11938   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11939   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11940
11941   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11942
11943   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11944   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11945   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11946   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11947
11948   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11949   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11950   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11951   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11952   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11953   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11954   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11955   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11956   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11957
11958   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11959   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11960   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11961
11962   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11963   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11964   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11965
11966   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11967   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11968   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11969   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11970   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11971   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11972
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11974   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11975   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11976   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11977   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11982   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11984   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11985
11986   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11987   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11989   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11990
11991   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11992   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11994   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11996   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11997   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11998 }
11999
12000 static void
12001 arm_init_builtins (void)
12002 {
12003   if (TARGET_REALLY_IWMMXT)
12004     arm_init_iwmmxt_builtins ();
12005 }
12006
12007 /* Errors in the source file can cause expand_expr to return const0_rtx
12008    where we expect a vector.  To avoid crashing, use one of the vector
12009    clear instructions.  */
12010
12011 static rtx
12012 safe_vector_operand (rtx x, enum machine_mode mode)
12013 {
12014   if (x != const0_rtx)
12015     return x;
12016   x = gen_reg_rtx (mode);
12017
12018   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12019                                : gen_rtx_SUBREG (DImode, x, 0)));
12020   return x;
12021 }
12022
12023 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12024
12025 static rtx
12026 arm_expand_binop_builtin (enum insn_code icode,
12027                           tree arglist, rtx target)
12028 {
12029   rtx pat;
12030   tree arg0 = TREE_VALUE (arglist);
12031   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12032   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12033   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12034   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12035   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12036   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12037
12038   if (VECTOR_MODE_P (mode0))
12039     op0 = safe_vector_operand (op0, mode0);
12040   if (VECTOR_MODE_P (mode1))
12041     op1 = safe_vector_operand (op1, mode1);
12042
12043   if (! target
12044       || GET_MODE (target) != tmode
12045       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12046     target = gen_reg_rtx (tmode);
12047
12048   /* In case the insn wants input operands in modes different from
12049      the result, abort.  */
12050   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12051     abort ();
12052
12053   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12054     op0 = copy_to_mode_reg (mode0, op0);
12055   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12056     op1 = copy_to_mode_reg (mode1, op1);
12057
12058   pat = GEN_FCN (icode) (target, op0, op1);
12059   if (! pat)
12060     return 0;
12061   emit_insn (pat);
12062   return target;
12063 }
12064
12065 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12066
12067 static rtx
12068 arm_expand_unop_builtin (enum insn_code icode,
12069                          tree arglist, rtx target, int do_load)
12070 {
12071   rtx pat;
12072   tree arg0 = TREE_VALUE (arglist);
12073   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12074   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12075   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12076
12077   if (! target
12078       || GET_MODE (target) != tmode
12079       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12080     target = gen_reg_rtx (tmode);
12081   if (do_load)
12082     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12083   else
12084     {
12085       if (VECTOR_MODE_P (mode0))
12086         op0 = safe_vector_operand (op0, mode0);
12087
12088       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12089         op0 = copy_to_mode_reg (mode0, op0);
12090     }
12091
12092   pat = GEN_FCN (icode) (target, op0);
12093   if (! pat)
12094     return 0;
12095   emit_insn (pat);
12096   return target;
12097 }
12098
12099 /* Expand an expression EXP that calls a built-in function,
12100    with result going to TARGET if that's convenient
12101    (and in mode MODE if that's convenient).
12102    SUBTARGET may be used as the target for computing one of EXP's operands.
12103    IGNORE is nonzero if the value is to be ignored.  */
12104
12105 static rtx
12106 arm_expand_builtin (tree exp,
12107                     rtx target,
12108                     rtx subtarget ATTRIBUTE_UNUSED,
12109                     enum machine_mode mode ATTRIBUTE_UNUSED,
12110                     int ignore ATTRIBUTE_UNUSED)
12111 {
12112   const struct builtin_description * d;
12113   enum insn_code    icode;
12114   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12115   tree              arglist = TREE_OPERAND (exp, 1);
12116   tree              arg0;
12117   tree              arg1;
12118   tree              arg2;
12119   rtx               op0;
12120   rtx               op1;
12121   rtx               op2;
12122   rtx               pat;
12123   int               fcode = DECL_FUNCTION_CODE (fndecl);
12124   size_t            i;
12125   enum machine_mode tmode;
12126   enum machine_mode mode0;
12127   enum machine_mode mode1;
12128   enum machine_mode mode2;
12129
12130   switch (fcode)
12131     {
12132     case ARM_BUILTIN_TEXTRMSB:
12133     case ARM_BUILTIN_TEXTRMUB:
12134     case ARM_BUILTIN_TEXTRMSH:
12135     case ARM_BUILTIN_TEXTRMUH:
12136     case ARM_BUILTIN_TEXTRMSW:
12137     case ARM_BUILTIN_TEXTRMUW:
12138       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12139                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12140                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12141                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12142                : CODE_FOR_iwmmxt_textrmw);
12143
12144       arg0 = TREE_VALUE (arglist);
12145       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12146       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12147       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12148       tmode = insn_data[icode].operand[0].mode;
12149       mode0 = insn_data[icode].operand[1].mode;
12150       mode1 = insn_data[icode].operand[2].mode;
12151
12152       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12153         op0 = copy_to_mode_reg (mode0, op0);
12154       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12155         {
12156           /* @@@ better error message */
12157           error ("selector must be an immediate");
12158           return gen_reg_rtx (tmode);
12159         }
12160       if (target == 0
12161           || GET_MODE (target) != tmode
12162           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12163         target = gen_reg_rtx (tmode);
12164       pat = GEN_FCN (icode) (target, op0, op1);
12165       if (! pat)
12166         return 0;
12167       emit_insn (pat);
12168       return target;
12169
12170     case ARM_BUILTIN_TINSRB:
12171     case ARM_BUILTIN_TINSRH:
12172     case ARM_BUILTIN_TINSRW:
12173       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12174                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12175                : CODE_FOR_iwmmxt_tinsrw);
12176       arg0 = TREE_VALUE (arglist);
12177       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12178       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12179       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12180       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12181       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12182       tmode = insn_data[icode].operand[0].mode;
12183       mode0 = insn_data[icode].operand[1].mode;
12184       mode1 = insn_data[icode].operand[2].mode;
12185       mode2 = insn_data[icode].operand[3].mode;
12186
12187       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12188         op0 = copy_to_mode_reg (mode0, op0);
12189       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12190         op1 = copy_to_mode_reg (mode1, op1);
12191       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12192         {
12193           /* @@@ better error message */
12194           error ("selector must be an immediate");
12195           return const0_rtx;
12196         }
12197       if (target == 0
12198           || GET_MODE (target) != tmode
12199           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12200         target = gen_reg_rtx (tmode);
12201       pat = GEN_FCN (icode) (target, op0, op1, op2);
12202       if (! pat)
12203         return 0;
12204       emit_insn (pat);
12205       return target;
12206
12207     case ARM_BUILTIN_SETWCX:
12208       arg0 = TREE_VALUE (arglist);
12209       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12210       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12211       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12212       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12213       return 0;
12214
12215     case ARM_BUILTIN_GETWCX:
12216       arg0 = TREE_VALUE (arglist);
12217       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12218       target = gen_reg_rtx (SImode);
12219       emit_insn (gen_iwmmxt_tmrc (target, op0));
12220       return target;
12221
12222     case ARM_BUILTIN_WSHUFH:
12223       icode = CODE_FOR_iwmmxt_wshufh;
12224       arg0 = TREE_VALUE (arglist);
12225       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12226       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12227       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12228       tmode = insn_data[icode].operand[0].mode;
12229       mode1 = insn_data[icode].operand[1].mode;
12230       mode2 = insn_data[icode].operand[2].mode;
12231
12232       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12233         op0 = copy_to_mode_reg (mode1, op0);
12234       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12235         {
12236           /* @@@ better error message */
12237           error ("mask must be an immediate");
12238           return const0_rtx;
12239         }
12240       if (target == 0
12241           || GET_MODE (target) != tmode
12242           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12243         target = gen_reg_rtx (tmode);
12244       pat = GEN_FCN (icode) (target, op0, op1);
12245       if (! pat)
12246         return 0;
12247       emit_insn (pat);
12248       return target;
12249
12250     case ARM_BUILTIN_WSADB:
12251       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12252     case ARM_BUILTIN_WSADH:
12253       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12254     case ARM_BUILTIN_WSADBZ:
12255       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12256     case ARM_BUILTIN_WSADHZ:
12257       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12258
12259       /* Several three-argument builtins.  */
12260     case ARM_BUILTIN_WMACS:
12261     case ARM_BUILTIN_WMACU:
12262     case ARM_BUILTIN_WALIGN:
12263     case ARM_BUILTIN_TMIA:
12264     case ARM_BUILTIN_TMIAPH:
12265     case ARM_BUILTIN_TMIATT:
12266     case ARM_BUILTIN_TMIATB:
12267     case ARM_BUILTIN_TMIABT:
12268     case ARM_BUILTIN_TMIABB:
12269       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12270                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12271                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12272                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12273                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12274                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12275                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12276                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12277                : CODE_FOR_iwmmxt_walign);
12278       arg0 = TREE_VALUE (arglist);
12279       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12280       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12281       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12282       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12283       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12284       tmode = insn_data[icode].operand[0].mode;
12285       mode0 = insn_data[icode].operand[1].mode;
12286       mode1 = insn_data[icode].operand[2].mode;
12287       mode2 = insn_data[icode].operand[3].mode;
12288
12289       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12290         op0 = copy_to_mode_reg (mode0, op0);
12291       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12292         op1 = copy_to_mode_reg (mode1, op1);
12293       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12294         op2 = copy_to_mode_reg (mode2, op2);
12295       if (target == 0
12296           || GET_MODE (target) != tmode
12297           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12298         target = gen_reg_rtx (tmode);
12299       pat = GEN_FCN (icode) (target, op0, op1, op2);
12300       if (! pat)
12301         return 0;
12302       emit_insn (pat);
12303       return target;
12304       
12305     case ARM_BUILTIN_WZERO:
12306       target = gen_reg_rtx (DImode);
12307       emit_insn (gen_iwmmxt_clrdi (target));
12308       return target;
12309
12310     default:
12311       break;
12312     }
12313
12314   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12315     if (d->code == (const enum arm_builtins) fcode)
12316       return arm_expand_binop_builtin (d->icode, arglist, target);
12317
12318   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12319     if (d->code == (const enum arm_builtins) fcode)
12320       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12321
12322   /* @@@ Should really do something sensible here.  */
12323   return NULL_RTX;
12324 }
12325 \f
12326 /* Recursively search through all of the blocks in a function
12327    checking to see if any of the variables created in that
12328    function match the RTX called 'orig'.  If they do then
12329    replace them with the RTX called 'new'.  */
12330 static void
12331 replace_symbols_in_block (tree block, rtx orig, rtx new)
12332 {
12333   for (; block; block = BLOCK_CHAIN (block))
12334     {
12335       tree sym;
12336       
12337       if (!TREE_USED (block))
12338         continue;
12339
12340       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12341         {
12342           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12343               || DECL_IGNORED_P (sym)
12344               || TREE_CODE (sym) != VAR_DECL
12345               || DECL_EXTERNAL (sym)
12346               || !rtx_equal_p (DECL_RTL (sym), orig)
12347               )
12348             continue;
12349
12350           SET_DECL_RTL (sym, new);
12351         }
12352       
12353       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12354     }
12355 }
12356
12357 /* Return the number (counting from 0) of
12358    the least significant set bit in MASK.  */
12359
12360 inline static int
12361 number_of_first_bit_set (int mask)
12362 {
12363   int bit;
12364
12365   for (bit = 0;
12366        (mask & (1 << bit)) == 0;
12367        ++bit)
12368     continue;
12369
12370   return bit;
12371 }
12372
12373 /* Generate code to return from a thumb function.
12374    If 'reg_containing_return_addr' is -1, then the return address is
12375    actually on the stack, at the stack pointer.  */
12376 static void
12377 thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
12378 {
12379   unsigned regs_available_for_popping;
12380   unsigned regs_to_pop;
12381   int pops_needed;
12382   unsigned available;
12383   unsigned required;
12384   int mode;
12385   int size;
12386   int restore_a4 = FALSE;
12387
12388   /* Compute the registers we need to pop.  */
12389   regs_to_pop = 0;
12390   pops_needed = 0;
12391
12392   /* There is an assumption here, that if eh_ofs is not NULL, the
12393      normal return address will have been pushed.  */
12394   if (reg_containing_return_addr == -1 || eh_ofs)
12395     {
12396       /* When we are generating a return for __builtin_eh_return, 
12397          reg_containing_return_addr must specify the return regno.  */
12398       if (eh_ofs && reg_containing_return_addr == -1)
12399         abort ();
12400
12401       regs_to_pop |= 1 << LR_REGNUM;
12402       ++pops_needed;
12403     }
12404
12405   if (TARGET_BACKTRACE)
12406     {
12407       /* Restore the (ARM) frame pointer and stack pointer.  */
12408       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12409       pops_needed += 2;
12410     }
12411
12412   /* If there is nothing to pop then just emit the BX instruction and
12413      return.  */
12414   if (pops_needed == 0)
12415     {
12416       if (eh_ofs)
12417         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12418
12419       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12420       return;
12421     }
12422   /* Otherwise if we are not supporting interworking and we have not created
12423      a backtrace structure and the function was not entered in ARM mode then
12424      just pop the return address straight into the PC.  */
12425   else if (!TARGET_INTERWORK
12426            && !TARGET_BACKTRACE
12427            && !is_called_in_ARM_mode (current_function_decl))
12428     {
12429       if (eh_ofs)
12430         {
12431           asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12432           asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12433           asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12434         }
12435       else
12436         asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12437
12438       return;
12439     }
12440
12441   /* Find out how many of the (return) argument registers we can corrupt.  */
12442   regs_available_for_popping = 0;
12443
12444   /* If returning via __builtin_eh_return, the bottom three registers
12445      all contain information needed for the return.  */
12446   if (eh_ofs)
12447     size = 12;
12448   else
12449     {
12450 #ifdef RTX_CODE
12451       /* If we can deduce the registers used from the function's
12452          return value.  This is more reliable that examining
12453          regs_ever_live[] because that will be set if the register is
12454          ever used in the function, not just if the register is used
12455          to hold a return value.  */
12456
12457       if (current_function_return_rtx != 0)
12458         mode = GET_MODE (current_function_return_rtx);
12459       else
12460 #endif
12461         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12462
12463       size = GET_MODE_SIZE (mode);
12464
12465       if (size == 0)
12466         {
12467           /* In a void function we can use any argument register.
12468              In a function that returns a structure on the stack
12469              we can use the second and third argument registers.  */
12470           if (mode == VOIDmode)
12471             regs_available_for_popping =
12472               (1 << ARG_REGISTER (1))
12473               | (1 << ARG_REGISTER (2))
12474               | (1 << ARG_REGISTER (3));
12475           else
12476             regs_available_for_popping =
12477               (1 << ARG_REGISTER (2))
12478               | (1 << ARG_REGISTER (3));
12479         }
12480       else if (size <= 4)
12481         regs_available_for_popping =
12482           (1 << ARG_REGISTER (2))
12483           | (1 << ARG_REGISTER (3));
12484       else if (size <= 8)
12485         regs_available_for_popping =
12486           (1 << ARG_REGISTER (3));
12487     }
12488
12489   /* Match registers to be popped with registers into which we pop them.  */
12490   for (available = regs_available_for_popping,
12491        required  = regs_to_pop;
12492        required != 0 && available != 0;
12493        available &= ~(available & - available),
12494        required  &= ~(required  & - required))
12495     -- pops_needed;
12496
12497   /* If we have any popping registers left over, remove them.  */
12498   if (available > 0)
12499     regs_available_for_popping &= ~available;
12500   
12501   /* Otherwise if we need another popping register we can use
12502      the fourth argument register.  */
12503   else if (pops_needed)
12504     {
12505       /* If we have not found any free argument registers and
12506          reg a4 contains the return address, we must move it.  */
12507       if (regs_available_for_popping == 0
12508           && reg_containing_return_addr == LAST_ARG_REGNUM)
12509         {
12510           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12511           reg_containing_return_addr = LR_REGNUM;
12512         }
12513       else if (size > 12)
12514         {
12515           /* Register a4 is being used to hold part of the return value,
12516              but we have dire need of a free, low register.  */
12517           restore_a4 = TRUE;
12518           
12519           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12520         }
12521       
12522       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12523         {
12524           /* The fourth argument register is available.  */
12525           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12526           
12527           --pops_needed;
12528         }
12529     }
12530
12531   /* Pop as many registers as we can.  */
12532   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12533                  regs_available_for_popping);
12534
12535   /* Process the registers we popped.  */
12536   if (reg_containing_return_addr == -1)
12537     {
12538       /* The return address was popped into the lowest numbered register.  */
12539       regs_to_pop &= ~(1 << LR_REGNUM);
12540       
12541       reg_containing_return_addr =
12542         number_of_first_bit_set (regs_available_for_popping);
12543
12544       /* Remove this register for the mask of available registers, so that
12545          the return address will not be corrupted by further pops.  */
12546       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12547     }
12548
12549   /* If we popped other registers then handle them here.  */
12550   if (regs_available_for_popping)
12551     {
12552       int frame_pointer;
12553       
12554       /* Work out which register currently contains the frame pointer.  */
12555       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12556
12557       /* Move it into the correct place.  */
12558       asm_fprintf (f, "\tmov\t%r, %r\n",
12559                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12560
12561       /* (Temporarily) remove it from the mask of popped registers.  */
12562       regs_available_for_popping &= ~(1 << frame_pointer);
12563       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12564       
12565       if (regs_available_for_popping)
12566         {
12567           int stack_pointer;
12568           
12569           /* We popped the stack pointer as well,
12570              find the register that contains it.  */
12571           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12572
12573           /* Move it into the stack register.  */
12574           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12575           
12576           /* At this point we have popped all necessary registers, so
12577              do not worry about restoring regs_available_for_popping
12578              to its correct value:
12579
12580              assert (pops_needed == 0)
12581              assert (regs_available_for_popping == (1 << frame_pointer))
12582              assert (regs_to_pop == (1 << STACK_POINTER))  */
12583         }
12584       else
12585         {
12586           /* Since we have just move the popped value into the frame
12587              pointer, the popping register is available for reuse, and
12588              we know that we still have the stack pointer left to pop.  */
12589           regs_available_for_popping |= (1 << frame_pointer);
12590         }
12591     }
12592   
12593   /* If we still have registers left on the stack, but we no longer have
12594      any registers into which we can pop them, then we must move the return
12595      address into the link register and make available the register that
12596      contained it.  */
12597   if (regs_available_for_popping == 0 && pops_needed > 0)
12598     {
12599       regs_available_for_popping |= 1 << reg_containing_return_addr;
12600       
12601       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12602                    reg_containing_return_addr);
12603       
12604       reg_containing_return_addr = LR_REGNUM;
12605     }
12606
12607   /* If we have registers left on the stack then pop some more.
12608      We know that at most we will want to pop FP and SP.  */
12609   if (pops_needed > 0)
12610     {
12611       int  popped_into;
12612       int  move_to;
12613       
12614       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12615                      regs_available_for_popping);
12616
12617       /* We have popped either FP or SP.
12618          Move whichever one it is into the correct register.  */
12619       popped_into = number_of_first_bit_set (regs_available_for_popping);
12620       move_to     = number_of_first_bit_set (regs_to_pop);
12621
12622       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12623
12624       regs_to_pop &= ~(1 << move_to);
12625
12626       --pops_needed;
12627     }
12628   
12629   /* If we still have not popped everything then we must have only
12630      had one register available to us and we are now popping the SP.  */
12631   if (pops_needed > 0)
12632     {
12633       int  popped_into;
12634       
12635       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12636                      regs_available_for_popping);
12637
12638       popped_into = number_of_first_bit_set (regs_available_for_popping);
12639
12640       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12641       /*
12642         assert (regs_to_pop == (1 << STACK_POINTER))
12643         assert (pops_needed == 1)
12644       */
12645     }
12646
12647   /* If necessary restore the a4 register.  */
12648   if (restore_a4)
12649     {
12650       if (reg_containing_return_addr != LR_REGNUM)
12651         {
12652           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12653           reg_containing_return_addr = LR_REGNUM;
12654         }
12655     
12656       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12657     }
12658
12659   if (eh_ofs)
12660     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12661
12662   /* Return to caller.  */
12663   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12664 }
12665
12666 /* Emit code to push or pop registers to or from the stack.  F is the
12667    assembly file.  MASK is the registers to push or pop.  PUSH is
12668    nonzero if we should push, and zero if we should pop.  For debugging
12669    output, if pushing, adjust CFA_OFFSET by the amount of space added
12670    to the stack.  REAL_REGS should have the same number of bits set as
12671    MASK, and will be used instead (in the same order) to describe which
12672    registers were saved - this is used to mark the save slots when we
12673    push high registers after moving them to low registers.  */
12674 static void
12675 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12676 {
12677   int regno;
12678   int lo_mask = mask & 0xFF;
12679   int pushed_words = 0;
12680
12681   if (lo_mask == 0 && !push && (mask & (1 << 15)))
12682     {
12683       /* Special case.  Do not generate a POP PC statement here, do it in
12684          thumb_exit() */
12685       thumb_exit (f, -1, NULL_RTX);
12686       return;
12687     }
12688       
12689   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12690
12691   /* Look at the low registers first.  */
12692   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12693     {
12694       if (lo_mask & 1)
12695         {
12696           asm_fprintf (f, "%r", regno);
12697           
12698           if ((lo_mask & ~1) != 0)
12699             fprintf (f, ", ");
12700
12701           pushed_words++;
12702         }
12703     }
12704   
12705   if (push && (mask & (1 << LR_REGNUM)))
12706     {
12707       /* Catch pushing the LR.  */
12708       if (mask & 0xFF)
12709         fprintf (f, ", ");
12710       
12711       asm_fprintf (f, "%r", LR_REGNUM);
12712
12713       pushed_words++;
12714     }
12715   else if (!push && (mask & (1 << PC_REGNUM)))
12716     {
12717       /* Catch popping the PC.  */
12718       if (TARGET_INTERWORK || TARGET_BACKTRACE)
12719         {
12720           /* The PC is never poped directly, instead
12721              it is popped into r3 and then BX is used.  */
12722           fprintf (f, "}\n");
12723
12724           thumb_exit (f, -1, NULL_RTX);
12725
12726           return;
12727         }
12728       else
12729         {
12730           if (mask & 0xFF)
12731             fprintf (f, ", ");
12732           
12733           asm_fprintf (f, "%r", PC_REGNUM);
12734         }
12735     }
12736        
12737   fprintf (f, "}\n");
12738
12739   if (push && pushed_words && dwarf2out_do_frame ())
12740     {
12741       char *l = dwarf2out_cfi_label ();
12742       int pushed_mask = real_regs;
12743
12744       *cfa_offset += pushed_words * 4;
12745       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12746
12747       pushed_words = 0;
12748       pushed_mask = real_regs;
12749       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12750         {
12751           if (pushed_mask & 1)
12752             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12753         }
12754     }
12755 }
12756 \f
12757 void
12758 thumb_final_prescan_insn (rtx insn)
12759 {
12760   if (flag_print_asm_name)
12761     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12762                  INSN_ADDRESSES (INSN_UID (insn)));
12763 }
12764
12765 int
12766 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12767 {
12768   unsigned HOST_WIDE_INT mask = 0xff;
12769   int i;
12770
12771   if (val == 0) /* XXX */
12772     return 0;
12773   
12774   for (i = 0; i < 25; i++)
12775     if ((val & (mask << i)) == val)
12776       return 1;
12777
12778   return 0;
12779 }
12780
12781 /* Returns nonzero if the current function contains,
12782    or might contain a far jump.  */
12783 static int
12784 thumb_far_jump_used_p (void)
12785 {
12786   rtx insn;
12787
12788   /* This test is only important for leaf functions.  */
12789   /* assert (!leaf_function_p ()); */
12790   
12791   /* If we have already decided that far jumps may be used,
12792      do not bother checking again, and always return true even if
12793      it turns out that they are not being used.  Once we have made
12794      the decision that far jumps are present (and that hence the link
12795      register will be pushed onto the stack) we cannot go back on it.  */
12796   if (cfun->machine->far_jump_used)
12797     return 1;
12798
12799   /* If this function is not being called from the prologue/epilogue
12800      generation code then it must be being called from the
12801      INITIAL_ELIMINATION_OFFSET macro.  */
12802   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12803     {
12804       /* In this case we know that we are being asked about the elimination
12805          of the arg pointer register.  If that register is not being used,
12806          then there are no arguments on the stack, and we do not have to
12807          worry that a far jump might force the prologue to push the link
12808          register, changing the stack offsets.  In this case we can just
12809          return false, since the presence of far jumps in the function will
12810          not affect stack offsets.
12811
12812          If the arg pointer is live (or if it was live, but has now been
12813          eliminated and so set to dead) then we do have to test to see if
12814          the function might contain a far jump.  This test can lead to some
12815          false negatives, since before reload is completed, then length of
12816          branch instructions is not known, so gcc defaults to returning their
12817          longest length, which in turn sets the far jump attribute to true.
12818
12819          A false negative will not result in bad code being generated, but it
12820          will result in a needless push and pop of the link register.  We
12821          hope that this does not occur too often.
12822
12823          If we need doubleword stack alignment this could affect the other
12824          elimination offsets so we can't risk getting it wrong.  */
12825       if (regs_ever_live [ARG_POINTER_REGNUM])
12826         cfun->machine->arg_pointer_live = 1;
12827       else if (!cfun->machine->arg_pointer_live)
12828         return 0;
12829     }
12830
12831   /* Check to see if the function contains a branch
12832      insn with the far jump attribute set.  */
12833   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12834     {
12835       if (GET_CODE (insn) == JUMP_INSN
12836           /* Ignore tablejump patterns.  */
12837           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12838           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12839           && get_attr_far_jump (insn) == FAR_JUMP_YES
12840           )
12841         {
12842           /* Record the fact that we have decided that
12843              the function does use far jumps.  */
12844           cfun->machine->far_jump_used = 1;
12845           return 1;
12846         }
12847     }
12848   
12849   return 0;
12850 }
12851
12852 /* Return nonzero if FUNC must be entered in ARM mode.  */
12853 int
12854 is_called_in_ARM_mode (tree func)
12855 {
12856   if (TREE_CODE (func) != FUNCTION_DECL)
12857     abort ();
12858
12859   /* Ignore the problem about functions whoes address is taken.  */
12860   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12861     return TRUE;
12862
12863 #ifdef ARM_PE 
12864   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12865 #else
12866   return FALSE;
12867 #endif
12868 }
12869
12870 /* The bits which aren't usefully expanded as rtl.  */
12871 const char *
12872 thumb_unexpanded_epilogue (void)
12873 {
12874   int regno;
12875   int live_regs_mask = 0;
12876   int high_regs_pushed = 0;
12877   int leaf_function = leaf_function_p ();
12878   int had_to_push_lr;
12879   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12880
12881   if (return_used_this_function)
12882     return "";
12883
12884   if (IS_NAKED (arm_current_func_type ()))
12885     return "";
12886
12887   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12888     if (THUMB_REG_PUSHED_P (regno))
12889       live_regs_mask |= 1 << regno;
12890
12891   for (regno = 8; regno < 13; regno++)
12892     if (THUMB_REG_PUSHED_P (regno))
12893       high_regs_pushed++;
12894
12895   /* The prolog may have pushed some high registers to use as
12896      work registers.  eg the testsuite file:
12897      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12898      compiles to produce:
12899         push    {r4, r5, r6, r7, lr}
12900         mov     r7, r9
12901         mov     r6, r8
12902         push    {r6, r7}
12903      as part of the prolog.  We have to undo that pushing here.  */
12904   
12905   if (high_regs_pushed)
12906     {
12907       int mask = live_regs_mask;
12908       int next_hi_reg;
12909       int size;
12910       int mode;
12911        
12912 #ifdef RTX_CODE
12913       /* If we can deduce the registers used from the function's return value.
12914          This is more reliable that examining regs_ever_live[] because that
12915          will be set if the register is ever used in the function, not just if
12916          the register is used to hold a return value.  */
12917
12918       if (current_function_return_rtx != 0)
12919         mode = GET_MODE (current_function_return_rtx);
12920       else
12921 #endif
12922         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12923
12924       size = GET_MODE_SIZE (mode);
12925
12926       /* Unless we are returning a type of size > 12 register r3 is
12927          available.  */
12928       if (size < 13)
12929         mask |=  1 << 3;
12930
12931       if (mask == 0)
12932         /* Oh dear!  We have no low registers into which we can pop
12933            high registers!  */
12934         internal_error
12935           ("no low registers available for popping high registers");
12936       
12937       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12938         if (THUMB_REG_PUSHED_P (next_hi_reg))
12939           break;
12940
12941       while (high_regs_pushed)
12942         {
12943           /* Find lo register(s) into which the high register(s) can
12944              be popped.  */
12945           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12946             {
12947               if (mask & (1 << regno))
12948                 high_regs_pushed--;
12949               if (high_regs_pushed == 0)
12950                 break;
12951             }
12952
12953           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12954
12955           /* Pop the values into the low register(s).  */
12956           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12957
12958           /* Move the value(s) into the high registers.  */
12959           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12960             {
12961               if (mask & (1 << regno))
12962                 {
12963                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12964                                regno);
12965                   
12966                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12967                     if (THUMB_REG_PUSHED_P (next_hi_reg))
12968                       break;
12969                 }
12970             }
12971         }
12972     }
12973
12974   had_to_push_lr = (live_regs_mask || !leaf_function
12975                     || thumb_far_jump_used_p ());
12976   
12977   if (TARGET_BACKTRACE
12978       && ((live_regs_mask & 0xFF) == 0)
12979       && regs_ever_live [LAST_ARG_REGNUM] != 0)
12980     {
12981       /* The stack backtrace structure creation code had to
12982          push R7 in order to get a work register, so we pop
12983          it now.  */
12984       live_regs_mask |= (1 << LAST_LO_REGNUM);
12985     }
12986   
12987   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12988     {
12989       if (had_to_push_lr
12990           && !is_called_in_ARM_mode (current_function_decl)
12991           && !eh_ofs)
12992         live_regs_mask |= 1 << PC_REGNUM;
12993
12994       /* Either no argument registers were pushed or a backtrace
12995          structure was created which includes an adjusted stack
12996          pointer, so just pop everything.  */
12997       if (live_regs_mask)
12998         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12999                        live_regs_mask);
13000       
13001       if (eh_ofs)
13002         thumb_exit (asm_out_file, 2, eh_ofs);
13003       /* We have either just popped the return address into the
13004          PC or it is was kept in LR for the entire function or
13005          it is still on the stack because we do not want to
13006          return by doing a pop {pc}.  */
13007       else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13008         thumb_exit (asm_out_file,
13009                     (had_to_push_lr
13010                      && is_called_in_ARM_mode (current_function_decl)) ?
13011                     -1 : LR_REGNUM, NULL_RTX);
13012     }
13013   else
13014     {
13015       /* Pop everything but the return address.  */
13016       live_regs_mask &= ~(1 << PC_REGNUM);
13017       
13018       if (live_regs_mask)
13019         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13020                        live_regs_mask);
13021
13022       if (had_to_push_lr)
13023         /* Get the return address into a temporary register.  */
13024         thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13025                        1 << LAST_ARG_REGNUM);
13026       
13027       /* Remove the argument registers that were pushed onto the stack.  */
13028       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13029                    SP_REGNUM, SP_REGNUM,
13030                    current_function_pretend_args_size);
13031       
13032       if (eh_ofs)
13033         thumb_exit (asm_out_file, 2, eh_ofs);
13034       else
13035         thumb_exit (asm_out_file,
13036                     had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13037     }
13038
13039   return "";
13040 }
13041
13042 /* Functions to save and restore machine-specific function data.  */
13043 static struct machine_function *
13044 arm_init_machine_status (void)
13045 {
13046   struct machine_function *machine;
13047   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13048
13049 #if ARM_FT_UNKNOWN != 0  
13050   machine->func_type = ARM_FT_UNKNOWN;
13051 #endif
13052   return machine;
13053 }
13054
13055 /* Return an RTX indicating where the return address to the
13056    calling function can be found.  */
13057 rtx
13058 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13059 {
13060   if (count != 0)
13061     return NULL_RTX;
13062
13063   if (TARGET_APCS_32)
13064     return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13065   else
13066     {
13067       rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
13068                             GEN_INT (RETURN_ADDR_MASK26));
13069       return get_func_hard_reg_initial_val (cfun, lr);
13070     }
13071 }
13072
13073 /* Do anything needed before RTL is emitted for each function.  */
13074 void
13075 arm_init_expanders (void)
13076 {
13077   /* Arrange to initialize and mark the machine per-function status.  */
13078   init_machine_status = arm_init_machine_status;
13079 }
13080
13081
13082 /* Like arm_compute_initial_elimination offset.  Simpler because
13083    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13084
13085 HOST_WIDE_INT
13086 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13087 {
13088   arm_stack_offsets *offsets;
13089
13090   offsets = arm_get_frame_offsets ();
13091
13092   switch (from)
13093     {
13094     case ARG_POINTER_REGNUM:
13095       switch (to)
13096         {
13097         case STACK_POINTER_REGNUM:
13098           return offsets->outgoing_args - offsets->saved_args;
13099
13100         case FRAME_POINTER_REGNUM:
13101           return offsets->soft_frame - offsets->saved_args;
13102
13103         case THUMB_HARD_FRAME_POINTER_REGNUM:
13104         case ARM_HARD_FRAME_POINTER_REGNUM:
13105           return offsets->saved_regs - offsets->saved_args;
13106
13107         default:
13108           abort();
13109         }
13110       break;
13111
13112     case FRAME_POINTER_REGNUM:
13113       switch (to)
13114         {
13115         case STACK_POINTER_REGNUM:
13116           return offsets->outgoing_args - offsets->soft_frame;
13117
13118         case THUMB_HARD_FRAME_POINTER_REGNUM:
13119         case ARM_HARD_FRAME_POINTER_REGNUM:
13120           return offsets->saved_regs - offsets->soft_frame;
13121
13122         default:
13123           abort();
13124         }
13125       break;
13126
13127     default:
13128       abort ();
13129     }
13130 }
13131
13132
13133 /* Generate the rest of a function's prologue.  */
13134 void
13135 thumb_expand_prologue (void)
13136 {
13137   rtx insn, dwarf;
13138
13139   HOST_WIDE_INT amount;
13140   arm_stack_offsets *offsets;
13141   unsigned long func_type;
13142
13143   func_type = arm_current_func_type ();
13144   
13145   /* Naked functions don't have prologues.  */
13146   if (IS_NAKED (func_type))
13147     return;
13148
13149   if (IS_INTERRUPT (func_type))
13150     {
13151       error ("interrupt Service Routines cannot be coded in Thumb mode");
13152       return;
13153     }
13154
13155   offsets = arm_get_frame_offsets ();
13156
13157   if (frame_pointer_needed)
13158     {
13159       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13160                                    stack_pointer_rtx));
13161       RTX_FRAME_RELATED_P (insn) = 1;
13162     }
13163
13164   amount = offsets->outgoing_args - offsets->saved_regs;
13165   if (amount)
13166     {
13167       if (amount < 512)
13168         {
13169           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13170                                         GEN_INT (- amount)));
13171           RTX_FRAME_RELATED_P (insn) = 1;
13172         }
13173       else
13174         {
13175           int regno;
13176           rtx reg;
13177
13178           /* The stack decrement is too big for an immediate value in a single
13179              insn.  In theory we could issue multiple subtracts, but after
13180              three of them it becomes more space efficient to place the full
13181              value in the constant pool and load into a register.  (Also the
13182              ARM debugger really likes to see only one stack decrement per
13183              function).  So instead we look for a scratch register into which
13184              we can load the decrement, and then we subtract this from the
13185              stack pointer.  Unfortunately on the thumb the only available
13186              scratch registers are the argument registers, and we cannot use
13187              these as they may hold arguments to the function.  Instead we
13188              attempt to locate a call preserved register which is used by this
13189              function.  If we can find one, then we know that it will have
13190              been pushed at the start of the prologue and so we can corrupt
13191              it now.  */
13192           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13193             if (THUMB_REG_PUSHED_P (regno)
13194                 && !(frame_pointer_needed
13195                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13196               break;
13197
13198           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13199             {
13200               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13201
13202               /* Choose an arbitrary, non-argument low register.  */
13203               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13204
13205               /* Save it by copying it into a high, scratch register.  */
13206               emit_insn (gen_movsi (spare, reg));
13207               /* Add a USE to stop propagate_one_insn() from barfing.  */
13208               emit_insn (gen_prologue_use (spare));
13209
13210               /* Decrement the stack.  */
13211               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13212               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13213                                             stack_pointer_rtx, reg));
13214               RTX_FRAME_RELATED_P (insn) = 1;
13215               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13216                                    plus_constant (stack_pointer_rtx,
13217                                                   GEN_INT (- amount)));
13218               RTX_FRAME_RELATED_P (dwarf) = 1;
13219               REG_NOTES (insn)
13220                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13221                                      REG_NOTES (insn));
13222
13223               /* Restore the low register's original value.  */
13224               emit_insn (gen_movsi (reg, spare));
13225               
13226               /* Emit a USE of the restored scratch register, so that flow
13227                  analysis will not consider the restore redundant.  The
13228                  register won't be used again in this function and isn't
13229                  restored by the epilogue.  */
13230               emit_insn (gen_prologue_use (reg));
13231             }
13232           else
13233             {
13234               reg = gen_rtx_REG (SImode, regno);
13235
13236               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13237
13238               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13239                                             stack_pointer_rtx, reg));
13240               RTX_FRAME_RELATED_P (insn) = 1;
13241               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13242                                    plus_constant (stack_pointer_rtx,
13243                                                   GEN_INT (- amount)));
13244               RTX_FRAME_RELATED_P (dwarf) = 1;
13245               REG_NOTES (insn)
13246                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13247                                      REG_NOTES (insn));
13248             }
13249         }
13250       /* If the frame pointer is needed, emit a special barrier that
13251          will prevent the scheduler from moving stores to the frame
13252          before the stack adjustment.  */
13253       if (frame_pointer_needed)
13254         emit_insn (gen_stack_tie (stack_pointer_rtx,
13255                                   hard_frame_pointer_rtx));
13256     }
13257   
13258   if (current_function_profile || TARGET_NO_SCHED_PRO)
13259     emit_insn (gen_blockage ());
13260 }
13261
13262 void
13263 thumb_expand_epilogue (void)
13264 {
13265   HOST_WIDE_INT amount;
13266   arm_stack_offsets *offsets;
13267   int regno;
13268
13269   /* Naked functions don't have prologues.  */
13270   if (IS_NAKED (arm_current_func_type ()))
13271     return;
13272
13273   offsets = arm_get_frame_offsets ();
13274   amount = offsets->outgoing_args - offsets->saved_regs;
13275
13276   if (frame_pointer_needed)
13277     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13278   else if (amount)
13279     {
13280       if (amount < 512)
13281         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13282                                GEN_INT (amount)));
13283       else
13284         {
13285           /* r3 is always free in the epilogue.  */
13286           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13287
13288           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13289           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13290         }
13291     }
13292       
13293   /* Emit a USE (stack_pointer_rtx), so that
13294      the stack adjustment will not be deleted.  */
13295   emit_insn (gen_prologue_use (stack_pointer_rtx));
13296
13297   if (current_function_profile || TARGET_NO_SCHED_PRO)
13298     emit_insn (gen_blockage ());
13299
13300   /* Emit a clobber for each insn that will be restored in the epilogue,
13301      so that flow2 will get register lifetimes correct.  */
13302   for (regno = 0; regno < 13; regno++)
13303     if (regs_ever_live[regno] && !call_used_regs[regno])
13304       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13305
13306   if (! regs_ever_live[LR_REGNUM])
13307     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13308 }
13309
13310 static void
13311 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13312 {
13313   int live_regs_mask = 0;
13314   int high_regs_pushed = 0;
13315   int cfa_offset = 0;
13316   int regno;
13317
13318   if (IS_NAKED (arm_current_func_type ()))
13319     return;
13320
13321   if (is_called_in_ARM_mode (current_function_decl))
13322     {
13323       const char * name;
13324
13325       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13326         abort ();
13327       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13328         abort ();
13329       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13330       
13331       /* Generate code sequence to switch us into Thumb mode.  */
13332       /* The .code 32 directive has already been emitted by
13333          ASM_DECLARE_FUNCTION_NAME.  */
13334       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13335       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13336
13337       /* Generate a label, so that the debugger will notice the
13338          change in instruction sets.  This label is also used by
13339          the assembler to bypass the ARM code when this function
13340          is called from a Thumb encoded function elsewhere in the
13341          same file.  Hence the definition of STUB_NAME here must
13342          agree with the definition in gas/config/tc-arm.c.  */
13343       
13344 #define STUB_NAME ".real_start_of"
13345       
13346       fprintf (f, "\t.code\t16\n");
13347 #ifdef ARM_PE
13348       if (arm_dllexport_name_p (name))
13349         name = arm_strip_name_encoding (name);
13350 #endif        
13351       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13352       fprintf (f, "\t.thumb_func\n");
13353       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13354     }
13355     
13356   if (current_function_pretend_args_size)
13357     {
13358       if (cfun->machine->uses_anonymous_args)
13359         {
13360           int num_pushes;
13361           
13362           fprintf (f, "\tpush\t{");
13363
13364           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13365           
13366           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13367                regno <= LAST_ARG_REGNUM;
13368                regno++)
13369             asm_fprintf (f, "%r%s", regno,
13370                          regno == LAST_ARG_REGNUM ? "" : ", ");
13371
13372           fprintf (f, "}\n");
13373         }
13374       else
13375         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", 
13376                      SP_REGNUM, SP_REGNUM,
13377                      current_function_pretend_args_size);
13378
13379       /* We don't need to record the stores for unwinding (would it
13380          help the debugger any if we did?), but record the change in
13381          the stack pointer.  */
13382       if (dwarf2out_do_frame ())
13383         {
13384           char *l = dwarf2out_cfi_label ();
13385           cfa_offset = cfa_offset + current_function_pretend_args_size;
13386           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13387         }
13388     }
13389
13390   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13391     if (THUMB_REG_PUSHED_P (regno))
13392       live_regs_mask |= 1 << regno;
13393
13394   if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p ())
13395     live_regs_mask |= 1 << LR_REGNUM;
13396
13397   if (TARGET_BACKTRACE)
13398     {
13399       int    offset;
13400       int    work_register = 0;
13401       int    wr;
13402       
13403       /* We have been asked to create a stack backtrace structure.
13404          The code looks like this:
13405          
13406          0   .align 2
13407          0   func:
13408          0     sub   SP, #16         Reserve space for 4 registers.
13409          2     push  {R7}            Get a work register.
13410          4     add   R7, SP, #20     Get the stack pointer before the push.
13411          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13412          8     mov   R7, PC          Get hold of the start of this code plus 12.
13413         10     str   R7, [SP, #16]   Store it.
13414         12     mov   R7, FP          Get hold of the current frame pointer.
13415         14     str   R7, [SP, #4]    Store it.
13416         16     mov   R7, LR          Get hold of the current return address.
13417         18     str   R7, [SP, #12]   Store it.
13418         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13419         22     mov   FP, R7          Put this value into the frame pointer.  */
13420
13421       if ((live_regs_mask & 0xFF) == 0)
13422         {
13423           /* See if the a4 register is free.  */
13424
13425           if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13426             work_register = LAST_ARG_REGNUM;
13427           else    /* We must push a register of our own.  */
13428             live_regs_mask |= (1 << LAST_LO_REGNUM);
13429         }
13430
13431       if (work_register == 0)
13432         {
13433           /* Select a register from the list that will be pushed to
13434              use as our work register.  */
13435           for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13436             if ((1 << work_register) & live_regs_mask)
13437               break;
13438         }
13439       
13440       asm_fprintf
13441         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13442          SP_REGNUM, SP_REGNUM);
13443
13444       if (dwarf2out_do_frame ())
13445         {
13446           char *l = dwarf2out_cfi_label ();
13447           cfa_offset = cfa_offset + 16;
13448           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13449         }
13450
13451       if (live_regs_mask)
13452         thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13453       
13454       for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13455         if (wr & live_regs_mask)
13456           offset += 4;
13457       
13458       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13459                    offset + 16 + current_function_pretend_args_size);
13460       
13461       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13462                    offset + 4);
13463
13464       /* Make sure that the instruction fetching the PC is in the right place
13465          to calculate "start of backtrace creation code + 12".  */
13466       if (live_regs_mask)
13467         {
13468           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13469           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13470                        offset + 12);
13471           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13472                        ARM_HARD_FRAME_POINTER_REGNUM);
13473           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13474                        offset);
13475         }
13476       else
13477         {
13478           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13479                        ARM_HARD_FRAME_POINTER_REGNUM);
13480           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13481                        offset);
13482           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13483           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13484                        offset + 12);
13485         }
13486       
13487       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13488       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13489                    offset + 8);
13490       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13491                    offset + 12);
13492       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13493                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13494     }
13495   else if (live_regs_mask)
13496     thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13497
13498   for (regno = 8; regno < 13; regno++)
13499     if (THUMB_REG_PUSHED_P (regno))
13500       high_regs_pushed++;
13501
13502   if (high_regs_pushed)
13503     {
13504       int pushable_regs = 0;
13505       int mask = live_regs_mask & 0xff;
13506       int next_hi_reg;
13507
13508       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13509         if (THUMB_REG_PUSHED_P (next_hi_reg))
13510           break;
13511
13512       pushable_regs = mask;
13513
13514       if (pushable_regs == 0)
13515         {
13516           /* Desperation time -- this probably will never happen.  */
13517           if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
13518             asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13519           mask = 1 << LAST_ARG_REGNUM;
13520         }
13521
13522       while (high_regs_pushed > 0)
13523         {
13524           int real_regs_mask = 0;
13525
13526           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13527             {
13528               if (mask & (1 << regno))
13529                 {
13530                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13531                   
13532                   high_regs_pushed--;
13533                   real_regs_mask |= (1 << next_hi_reg);
13534                   
13535                   if (high_regs_pushed)
13536                     {
13537                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13538                            next_hi_reg--)
13539                         if (THUMB_REG_PUSHED_P (next_hi_reg))
13540                           break;
13541                     }
13542                   else
13543                     {
13544                       mask &= ~((1 << regno) - 1);
13545                       break;
13546                     }
13547                 }
13548             }
13549
13550           thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
13551         }
13552
13553       if (pushable_regs == 0
13554           && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
13555         asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13556     }
13557 }
13558
13559 /* Handle the case of a double word load into a low register from
13560    a computed memory address.  The computed address may involve a
13561    register which is overwritten by the load.  */
13562 const char *
13563 thumb_load_double_from_address (rtx *operands)
13564 {
13565   rtx addr;
13566   rtx base;
13567   rtx offset;
13568   rtx arg1;
13569   rtx arg2;
13570   
13571   if (GET_CODE (operands[0]) != REG)
13572     abort ();
13573   
13574   if (GET_CODE (operands[1]) != MEM)
13575     abort ();
13576
13577   /* Get the memory address.  */
13578   addr = XEXP (operands[1], 0);
13579       
13580   /* Work out how the memory address is computed.  */
13581   switch (GET_CODE (addr))
13582     {
13583     case REG:
13584       operands[2] = gen_rtx_MEM (SImode,
13585                                  plus_constant (XEXP (operands[1], 0), 4));
13586
13587       if (REGNO (operands[0]) == REGNO (addr))
13588         {
13589           output_asm_insn ("ldr\t%H0, %2", operands);
13590           output_asm_insn ("ldr\t%0, %1", operands);
13591         }
13592       else
13593         {
13594           output_asm_insn ("ldr\t%0, %1", operands);
13595           output_asm_insn ("ldr\t%H0, %2", operands);
13596         }
13597       break;
13598       
13599     case CONST:
13600       /* Compute <address> + 4 for the high order load.  */
13601       operands[2] = gen_rtx_MEM (SImode,
13602                                  plus_constant (XEXP (operands[1], 0), 4));
13603       
13604       output_asm_insn ("ldr\t%0, %1", operands);
13605       output_asm_insn ("ldr\t%H0, %2", operands);
13606       break;
13607           
13608     case PLUS:
13609       arg1   = XEXP (addr, 0);
13610       arg2   = XEXP (addr, 1);
13611             
13612       if (CONSTANT_P (arg1))
13613         base = arg2, offset = arg1;
13614       else
13615         base = arg1, offset = arg2;
13616   
13617       if (GET_CODE (base) != REG)
13618         abort ();
13619
13620       /* Catch the case of <address> = <reg> + <reg> */
13621       if (GET_CODE (offset) == REG)
13622         {
13623           int reg_offset = REGNO (offset);
13624           int reg_base   = REGNO (base);
13625           int reg_dest   = REGNO (operands[0]);
13626           
13627           /* Add the base and offset registers together into the
13628              higher destination register.  */
13629           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13630                        reg_dest + 1, reg_base, reg_offset);
13631           
13632           /* Load the lower destination register from the address in
13633              the higher destination register.  */
13634           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13635                        reg_dest, reg_dest + 1);
13636           
13637           /* Load the higher destination register from its own address
13638              plus 4.  */
13639           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13640                        reg_dest + 1, reg_dest + 1);
13641         }
13642       else
13643         {
13644           /* Compute <address> + 4 for the high order load.  */
13645           operands[2] = gen_rtx_MEM (SImode,
13646                                      plus_constant (XEXP (operands[1], 0), 4));
13647           
13648           /* If the computed address is held in the low order register
13649              then load the high order register first, otherwise always
13650              load the low order register first.  */
13651           if (REGNO (operands[0]) == REGNO (base))
13652             {
13653               output_asm_insn ("ldr\t%H0, %2", operands);
13654               output_asm_insn ("ldr\t%0, %1", operands);
13655             }
13656           else
13657             {
13658               output_asm_insn ("ldr\t%0, %1", operands);
13659               output_asm_insn ("ldr\t%H0, %2", operands);
13660             }
13661         }
13662       break;
13663
13664     case LABEL_REF:
13665       /* With no registers to worry about we can just load the value
13666          directly.  */
13667       operands[2] = gen_rtx_MEM (SImode,
13668                                  plus_constant (XEXP (operands[1], 0), 4));
13669           
13670       output_asm_insn ("ldr\t%H0, %2", operands);
13671       output_asm_insn ("ldr\t%0, %1", operands);
13672       break;
13673       
13674     default:
13675       abort ();
13676       break;
13677     }
13678   
13679   return "";
13680 }
13681
13682 const char *
13683 thumb_output_move_mem_multiple (int n, rtx *operands)
13684 {
13685   rtx tmp;
13686
13687   switch (n)
13688     {
13689     case 2:
13690       if (REGNO (operands[4]) > REGNO (operands[5]))
13691         {
13692           tmp = operands[4];
13693           operands[4] = operands[5];
13694           operands[5] = tmp;
13695         }
13696       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13697       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13698       break;
13699
13700     case 3:
13701       if (REGNO (operands[4]) > REGNO (operands[5]))
13702         {
13703           tmp = operands[4];
13704           operands[4] = operands[5];
13705           operands[5] = tmp;
13706         }
13707       if (REGNO (operands[5]) > REGNO (operands[6]))
13708         {
13709           tmp = operands[5];
13710           operands[5] = operands[6];
13711           operands[6] = tmp;
13712         }
13713       if (REGNO (operands[4]) > REGNO (operands[5]))
13714         {
13715           tmp = operands[4];
13716           operands[4] = operands[5];
13717           operands[5] = tmp;
13718         }
13719       
13720       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13721       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13722       break;
13723
13724     default:
13725       abort ();
13726     }
13727
13728   return "";
13729 }
13730
13731 /* Routines for generating rtl.  */
13732 void
13733 thumb_expand_movstrqi (rtx *operands)
13734 {
13735   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13736   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13737   HOST_WIDE_INT len = INTVAL (operands[2]);
13738   HOST_WIDE_INT offset = 0;
13739
13740   while (len >= 12)
13741     {
13742       emit_insn (gen_movmem12b (out, in, out, in));
13743       len -= 12;
13744     }
13745   
13746   if (len >= 8)
13747     {
13748       emit_insn (gen_movmem8b (out, in, out, in));
13749       len -= 8;
13750     }
13751   
13752   if (len >= 4)
13753     {
13754       rtx reg = gen_reg_rtx (SImode);
13755       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13756       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13757       len -= 4;
13758       offset += 4;
13759     }
13760   
13761   if (len >= 2)
13762     {
13763       rtx reg = gen_reg_rtx (HImode);
13764       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode, 
13765                                               plus_constant (in, offset))));
13766       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13767                             reg));
13768       len -= 2;
13769       offset += 2;
13770     }
13771   
13772   if (len)
13773     {
13774       rtx reg = gen_reg_rtx (QImode);
13775       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13776                                               plus_constant (in, offset))));
13777       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13778                             reg));
13779     }
13780 }
13781
13782 int
13783 thumb_cmp_operand (rtx op, enum machine_mode mode)
13784 {
13785   return ((GET_CODE (op) == CONST_INT
13786            && INTVAL (op) < 256
13787            && INTVAL (op) >= 0)
13788           || s_register_operand (op, mode));
13789 }
13790
13791 int
13792 thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13793 {
13794   return (GET_CODE (op) == CONST_INT
13795           && INTVAL (op) < 0
13796           && INTVAL (op) > -256);
13797 }
13798
13799 /* Return TRUE if a result can be stored in OP without clobbering the
13800    condition code register.  Prior to reload we only accept a
13801    register.  After reload we have to be able to handle memory as
13802    well, since a pseudo may not get a hard reg and reload cannot
13803    handle output-reloads on jump insns.
13804
13805    We could possibly handle mem before reload as well, but that might
13806    complicate things with the need to handle increment
13807    side-effects.  */
13808
13809 int
13810 thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13811 {
13812   return (s_register_operand (op, mode)
13813           || ((reload_in_progress || reload_completed)
13814               && memory_operand (op, mode)));
13815 }
13816
13817 /* Handle storing a half-word to memory during reload.  */ 
13818 void
13819 thumb_reload_out_hi (rtx *operands)
13820 {
13821   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13822 }
13823
13824 /* Handle reading a half-word from memory during reload.  */ 
13825 void
13826 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13827 {
13828   abort ();
13829 }
13830
13831 /* Return the length of a function name prefix
13832     that starts with the character 'c'.  */
13833 static int
13834 arm_get_strip_length (int c)
13835 {
13836   switch (c)
13837     {
13838     ARM_NAME_ENCODING_LENGTHS
13839       default: return 0; 
13840     }
13841 }
13842
13843 /* Return a pointer to a function's name with any
13844    and all prefix encodings stripped from it.  */
13845 const char *
13846 arm_strip_name_encoding (const char *name)
13847 {
13848   int skip;
13849   
13850   while ((skip = arm_get_strip_length (* name)))
13851     name += skip;
13852
13853   return name;
13854 }
13855
13856 /* If there is a '*' anywhere in the name's prefix, then
13857    emit the stripped name verbatim, otherwise prepend an
13858    underscore if leading underscores are being used.  */
13859 void
13860 arm_asm_output_labelref (FILE *stream, const char *name)
13861 {
13862   int skip;
13863   int verbatim = 0;
13864
13865   while ((skip = arm_get_strip_length (* name)))
13866     {
13867       verbatim |= (*name == '*');
13868       name += skip;
13869     }
13870
13871   if (verbatim)
13872     fputs (name, stream);
13873   else
13874     asm_fprintf (stream, "%U%s", name);
13875 }
13876
13877 rtx aof_pic_label;
13878
13879 #ifdef AOF_ASSEMBLER
13880 /* Special functions only needed when producing AOF syntax assembler.  */
13881
13882 struct pic_chain
13883 {
13884   struct pic_chain * next;
13885   const char * symname;
13886 };
13887
13888 static struct pic_chain * aof_pic_chain = NULL;
13889
13890 rtx
13891 aof_pic_entry (rtx x)
13892 {
13893   struct pic_chain ** chainp;
13894   int offset;
13895
13896   if (aof_pic_label == NULL_RTX)
13897     {
13898       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13899     }
13900
13901   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13902        offset += 4, chainp = &(*chainp)->next)
13903     if ((*chainp)->symname == XSTR (x, 0))
13904       return plus_constant (aof_pic_label, offset);
13905
13906   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13907   (*chainp)->next = NULL;
13908   (*chainp)->symname = XSTR (x, 0);
13909   return plus_constant (aof_pic_label, offset);
13910 }
13911
13912 void
13913 aof_dump_pic_table (FILE *f)
13914 {
13915   struct pic_chain * chain;
13916
13917   if (aof_pic_chain == NULL)
13918     return;
13919
13920   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13921                PIC_OFFSET_TABLE_REGNUM,
13922                PIC_OFFSET_TABLE_REGNUM);
13923   fputs ("|x$adcons|\n", f);
13924   
13925   for (chain = aof_pic_chain; chain; chain = chain->next)
13926     {
13927       fputs ("\tDCD\t", f);
13928       assemble_name (f, chain->symname);
13929       fputs ("\n", f);
13930     }
13931 }
13932
13933 int arm_text_section_count = 1;
13934
13935 char *
13936 aof_text_section (void )
13937 {
13938   static char buf[100];
13939   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13940            arm_text_section_count++);
13941   if (flag_pic)
13942     strcat (buf, ", PIC, REENTRANT");
13943   return buf;
13944 }
13945
13946 static int arm_data_section_count = 1;
13947
13948 char *
13949 aof_data_section (void)
13950 {
13951   static char buf[100];
13952   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13953   return buf;
13954 }
13955
13956 /* The AOF assembler is religiously strict about declarations of
13957    imported and exported symbols, so that it is impossible to declare
13958    a function as imported near the beginning of the file, and then to
13959    export it later on.  It is, however, possible to delay the decision
13960    until all the functions in the file have been compiled.  To get
13961    around this, we maintain a list of the imports and exports, and
13962    delete from it any that are subsequently defined.  At the end of
13963    compilation we spit the remainder of the list out before the END
13964    directive.  */
13965
13966 struct import
13967 {
13968   struct import * next;
13969   const char * name;
13970 };
13971
13972 static struct import * imports_list = NULL;
13973
13974 void
13975 aof_add_import (const char *name)
13976 {
13977   struct import * new;
13978
13979   for (new = imports_list; new; new = new->next)
13980     if (new->name == name)
13981       return;
13982
13983   new = (struct import *) xmalloc (sizeof (struct import));
13984   new->next = imports_list;
13985   imports_list = new;
13986   new->name = name;
13987 }
13988
13989 void
13990 aof_delete_import (const char *name)
13991 {
13992   struct import ** old;
13993
13994   for (old = &imports_list; *old; old = & (*old)->next)
13995     {
13996       if ((*old)->name == name)
13997         {
13998           *old = (*old)->next;
13999           return;
14000         }
14001     }
14002 }
14003
14004 int arm_main_function = 0;
14005
14006 static void
14007 aof_dump_imports (FILE *f)
14008 {
14009   /* The AOF assembler needs this to cause the startup code to be extracted
14010      from the library.  Brining in __main causes the whole thing to work
14011      automagically.  */
14012   if (arm_main_function)
14013     {
14014       text_section ();
14015       fputs ("\tIMPORT __main\n", f);
14016       fputs ("\tDCD __main\n", f);
14017     }
14018
14019   /* Now dump the remaining imports.  */
14020   while (imports_list)
14021     {
14022       fprintf (f, "\tIMPORT\t");
14023       assemble_name (f, imports_list->name);
14024       fputc ('\n', f);
14025       imports_list = imports_list->next;
14026     }
14027 }
14028
14029 static void
14030 aof_globalize_label (FILE *stream, const char *name)
14031 {
14032   default_globalize_label (stream, name);
14033   if (! strcmp (name, "main"))
14034     arm_main_function = 1;
14035 }
14036
14037 static void
14038 aof_file_start (void)
14039 {
14040   fputs ("__r0\tRN\t0\n", asm_out_file);
14041   fputs ("__a1\tRN\t0\n", asm_out_file);
14042   fputs ("__a2\tRN\t1\n", asm_out_file);
14043   fputs ("__a3\tRN\t2\n", asm_out_file);
14044   fputs ("__a4\tRN\t3\n", asm_out_file);
14045   fputs ("__v1\tRN\t4\n", asm_out_file);
14046   fputs ("__v2\tRN\t5\n", asm_out_file);
14047   fputs ("__v3\tRN\t6\n", asm_out_file);
14048   fputs ("__v4\tRN\t7\n", asm_out_file);
14049   fputs ("__v5\tRN\t8\n", asm_out_file);
14050   fputs ("__v6\tRN\t9\n", asm_out_file);
14051   fputs ("__sl\tRN\t10\n", asm_out_file);
14052   fputs ("__fp\tRN\t11\n", asm_out_file);
14053   fputs ("__ip\tRN\t12\n", asm_out_file);
14054   fputs ("__sp\tRN\t13\n", asm_out_file);
14055   fputs ("__lr\tRN\t14\n", asm_out_file);
14056   fputs ("__pc\tRN\t15\n", asm_out_file);
14057   fputs ("__f0\tFN\t0\n", asm_out_file);
14058   fputs ("__f1\tFN\t1\n", asm_out_file);
14059   fputs ("__f2\tFN\t2\n", asm_out_file);
14060   fputs ("__f3\tFN\t3\n", asm_out_file);
14061   fputs ("__f4\tFN\t4\n", asm_out_file);
14062   fputs ("__f5\tFN\t5\n", asm_out_file);
14063   fputs ("__f6\tFN\t6\n", asm_out_file);
14064   fputs ("__f7\tFN\t7\n", asm_out_file);
14065   text_section ();
14066 }
14067
14068 static void
14069 aof_file_end (void)
14070 {
14071   if (flag_pic)
14072     aof_dump_pic_table (asm_out_file);
14073   aof_dump_imports (asm_out_file);
14074   fputs ("\tEND\n", asm_out_file);
14075 }
14076 #endif /* AOF_ASSEMBLER */
14077
14078 #ifdef OBJECT_FORMAT_ELF
14079 /* Switch to an arbitrary section NAME with attributes as specified
14080    by FLAGS.  ALIGN specifies any known alignment requirements for
14081    the section; 0 if the default should be used.
14082
14083    Differs from the default elf version only in the prefix character
14084    used before the section type.  */
14085
14086 static void
14087 arm_elf_asm_named_section (const char *name, unsigned int flags)
14088 {
14089   char flagchars[10], *f = flagchars;
14090
14091   if (! named_section_first_declaration (name))
14092     {
14093       fprintf (asm_out_file, "\t.section\t%s\n", name);
14094       return;
14095     }
14096
14097   if (!(flags & SECTION_DEBUG))
14098     *f++ = 'a';
14099   if (flags & SECTION_WRITE)
14100     *f++ = 'w';
14101   if (flags & SECTION_CODE)
14102     *f++ = 'x';
14103   if (flags & SECTION_SMALL)
14104     *f++ = 's';
14105   if (flags & SECTION_MERGE)
14106     *f++ = 'M';
14107   if (flags & SECTION_STRINGS)
14108     *f++ = 'S';
14109   if (flags & SECTION_TLS)
14110     *f++ = 'T';
14111   *f = '\0';
14112
14113   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
14114
14115   if (!(flags & SECTION_NOTYPE))
14116     {
14117       const char *type;
14118
14119       if (flags & SECTION_BSS)
14120         type = "nobits";
14121       else
14122         type = "progbits";
14123
14124       fprintf (asm_out_file, ",%%%s", type);
14125
14126       if (flags & SECTION_ENTSIZE)
14127         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14128     }
14129
14130   putc ('\n', asm_out_file);
14131 }
14132 #endif
14133
14134 #ifndef ARM_PE
14135 /* Symbols in the text segment can be accessed without indirecting via the
14136    constant pool; it may take an extra binary operation, but this is still
14137    faster than indirecting via memory.  Don't do this when not optimizing,
14138    since we won't be calculating al of the offsets necessary to do this
14139    simplification.  */
14140
14141 static void
14142 arm_encode_section_info (tree decl, rtx rtl, int first)
14143 {
14144   /* This doesn't work with AOF syntax, since the string table may be in
14145      a different AREA.  */
14146 #ifndef AOF_ASSEMBLER
14147   if (optimize > 0 && TREE_CONSTANT (decl))
14148     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14149 #endif
14150
14151   /* If we are referencing a function that is weak then encode a long call
14152      flag in the function name, otherwise if the function is static or
14153      or known to be defined in this file then encode a short call flag.  */
14154   if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14155     {
14156       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14157         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14158       else if (! TREE_PUBLIC (decl))
14159         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14160     }
14161 }
14162 #endif /* !ARM_PE */
14163
14164 static void
14165 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14166 {
14167   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14168       && !strcmp (prefix, "L"))
14169     {
14170       arm_ccfsm_state = 0;
14171       arm_target_insn = NULL;
14172     }
14173   default_internal_label (stream, prefix, labelno);
14174 }
14175
14176 /* Output code to add DELTA to the first argument, and then jump
14177    to FUNCTION.  Used for C++ multiple inheritance.  */
14178 static void
14179 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14180                      HOST_WIDE_INT delta,
14181                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14182                      tree function)
14183 {
14184   static int thunk_label = 0;
14185   char label[256];
14186   int mi_delta = delta;
14187   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14188   int shift = 0;
14189   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14190                     ? 1 : 0);
14191   if (mi_delta < 0)
14192     mi_delta = - mi_delta;
14193   if (TARGET_THUMB)
14194     {
14195       int labelno = thunk_label++;
14196       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14197       fputs ("\tldr\tr12, ", file);
14198       assemble_name (file, label);
14199       fputc ('\n', file);
14200     }
14201   while (mi_delta != 0)
14202     {
14203       if ((mi_delta & (3 << shift)) == 0)
14204         shift += 2;
14205       else
14206         {
14207           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14208                        mi_op, this_regno, this_regno,
14209                        mi_delta & (0xff << shift));
14210           mi_delta &= ~(0xff << shift);
14211           shift += 8;
14212         }
14213     }
14214   if (TARGET_THUMB)
14215     {
14216       fprintf (file, "\tbx\tr12\n");
14217       ASM_OUTPUT_ALIGN (file, 2);
14218       assemble_name (file, label);
14219       fputs (":\n", file);
14220       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14221     }
14222   else
14223     {
14224       fputs ("\tb\t", file);
14225       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14226       if (NEED_PLT_RELOC)
14227         fputs ("(PLT)", file);
14228       fputc ('\n', file);
14229     }
14230 }
14231
14232 int
14233 arm_emit_vector_const (FILE *file, rtx x)
14234 {
14235   int i;
14236   const char * pattern;
14237
14238   if (GET_CODE (x) != CONST_VECTOR)
14239     abort ();
14240
14241   switch (GET_MODE (x))
14242     {
14243     case V2SImode: pattern = "%08x"; break;
14244     case V4HImode: pattern = "%04x"; break;
14245     case V8QImode: pattern = "%02x"; break;
14246     default:       abort ();
14247     }
14248
14249   fprintf (file, "0x");
14250   for (i = CONST_VECTOR_NUNITS (x); i--;)
14251     {
14252       rtx element;
14253
14254       element = CONST_VECTOR_ELT (x, i);
14255       fprintf (file, pattern, INTVAL (element));
14256     }
14257
14258   return 1;
14259 }
14260
14261 const char *
14262 arm_output_load_gr (rtx *operands)
14263 {
14264   rtx reg;
14265   rtx offset;
14266   rtx wcgr;
14267   rtx sum;
14268   
14269   if (GET_CODE (operands [1]) != MEM
14270       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14271       || GET_CODE (reg = XEXP (sum, 0)) != REG
14272       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14273       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14274     return "wldrw%?\t%0, %1";
14275   
14276   /* Fix up an out-of-range load of a GR register.  */  
14277   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14278   wcgr = operands[0];
14279   operands[0] = reg;
14280   output_asm_insn ("ldr%?\t%0, %1", operands);
14281
14282   operands[0] = wcgr;
14283   operands[1] = reg;
14284   output_asm_insn ("tmcr%?\t%0, %1", operands);
14285   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14286
14287   return "";
14288 }
14289
14290 static rtx
14291 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14292                       int incoming ATTRIBUTE_UNUSED)
14293 {
14294 #if 0
14295   /* FIXME: The ARM backend has special code to handle structure
14296          returns, and will reserve its own hidden first argument.  So
14297          if this macro is enabled a *second* hidden argument will be
14298          reserved, which will break binary compatibility with old
14299          toolchains and also thunk handling.  One day this should be
14300          fixed.  */
14301   return 0;
14302 #else
14303   /* Register in which address to store a structure value
14304      is passed to a function.  */
14305   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14306 #endif
14307 }
14308
14309 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14310
14311    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14312    named arg and all anonymous args onto the stack.
14313    XXX I know the prologue shouldn't be pushing registers, but it is faster
14314    that way.  */
14315
14316 static void
14317 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14318                             enum machine_mode mode ATTRIBUTE_UNUSED,
14319                             tree type ATTRIBUTE_UNUSED,
14320                             int *pretend_size,
14321                             int second_time ATTRIBUTE_UNUSED)
14322 {
14323   cfun->machine->uses_anonymous_args = 1;
14324   if (cum->nregs < NUM_ARG_REGS)
14325     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14326 }
14327
14328 /* Return nonzero if the CONSUMER instruction (a store) does not need
14329    PRODUCER's value to calculate the address.  */
14330
14331 int
14332 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14333 {
14334   rtx value = PATTERN (producer);
14335   rtx addr = PATTERN (consumer);
14336
14337   if (GET_CODE (value) == COND_EXEC)
14338     value = COND_EXEC_CODE (value);
14339   if (GET_CODE (value) == PARALLEL)
14340     value = XVECEXP (value, 0, 0);
14341   value = XEXP (value, 0);
14342   if (GET_CODE (addr) == COND_EXEC)
14343     addr = COND_EXEC_CODE (addr);
14344   if (GET_CODE (addr) == PARALLEL)
14345     addr = XVECEXP (addr, 0, 0);
14346   addr = XEXP (addr, 0);
14347   
14348   return !reg_overlap_mentioned_p (value, addr);
14349 }
14350
14351 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14352    have an early register shift value or amount dependency on the
14353    result of PRODUCER.  */
14354
14355 int
14356 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14357 {
14358   rtx value = PATTERN (producer);
14359   rtx op = PATTERN (consumer);
14360   rtx early_op;
14361
14362   if (GET_CODE (value) == COND_EXEC)
14363     value = COND_EXEC_CODE (value);
14364   if (GET_CODE (value) == PARALLEL)
14365     value = XVECEXP (value, 0, 0);
14366   value = XEXP (value, 0);
14367   if (GET_CODE (op) == COND_EXEC)
14368     op = COND_EXEC_CODE (op);
14369   if (GET_CODE (op) == PARALLEL)
14370     op = XVECEXP (op, 0, 0);
14371   op = XEXP (op, 1);
14372   
14373   early_op = XEXP (op, 0);
14374   /* This is either an actual independent shift, or a shift applied to
14375      the first operand of another operation.  We want the whole shift
14376      operation.  */
14377   if (GET_CODE (early_op) == REG)
14378     early_op = op;
14379
14380   return !reg_overlap_mentioned_p (value, early_op);
14381 }
14382
14383 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14384    have an early register shift value dependency on the result of
14385    PRODUCER.  */
14386
14387 int
14388 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14389 {
14390   rtx value = PATTERN (producer);
14391   rtx op = PATTERN (consumer);
14392   rtx early_op;
14393
14394   if (GET_CODE (value) == COND_EXEC)
14395     value = COND_EXEC_CODE (value);
14396   if (GET_CODE (value) == PARALLEL)
14397     value = XVECEXP (value, 0, 0);
14398   value = XEXP (value, 0);
14399   if (GET_CODE (op) == COND_EXEC)
14400     op = COND_EXEC_CODE (op);
14401   if (GET_CODE (op) == PARALLEL)
14402     op = XVECEXP (op, 0, 0);
14403   op = XEXP (op, 1);
14404   
14405   early_op = XEXP (op, 0);
14406
14407   /* This is either an actual independent shift, or a shift applied to
14408      the first operand of another operation.  We want the value being
14409      shifted, in either case.  */
14410   if (GET_CODE (early_op) != REG)
14411     early_op = XEXP (early_op, 0);
14412   
14413   return !reg_overlap_mentioned_p (value, early_op);
14414 }
14415
14416 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14417    have an early register mult dependency on the result of
14418    PRODUCER.  */
14419
14420 int
14421 arm_no_early_mul_dep (rtx producer, rtx consumer)
14422 {
14423   rtx value = PATTERN (producer);
14424   rtx op = PATTERN (consumer);
14425
14426   if (GET_CODE (value) == COND_EXEC)
14427     value = COND_EXEC_CODE (value);
14428   if (GET_CODE (value) == PARALLEL)
14429     value = XVECEXP (value, 0, 0);
14430   value = XEXP (value, 0);
14431   if (GET_CODE (op) == COND_EXEC)
14432     op = COND_EXEC_CODE (op);
14433   if (GET_CODE (op) == PARALLEL)
14434     op = XVECEXP (op, 0, 0);
14435   op = XEXP (op, 1);
14436   
14437   return (GET_CODE (op) == PLUS
14438           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14439 }
14440
14441
14442 /* We can't rely on the caller doing the proper promotion when
14443    using APCS or ATPCS.  */
14444
14445 static bool
14446 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14447 {
14448     return arm_abi == ARM_ABI_APCS || arm_abi == ARM_ABI_ATPCS;
14449 }
14450