OSDN Git Service

Add support for Cirrus EP9312, an ARM variant.
[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, 2002, 2003
3    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 GNU CC.
9
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; 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
54 /* Forward definitions of types.  */
55 typedef struct minipool_node    Mnode;
56 typedef struct minipool_fixup   Mfix;
57
58 /* In order to improve the layout of the prototypes below
59    some short type abbreviations are defined here.  */
60 #define Hint     HOST_WIDE_INT
61 #define Mmode    enum machine_mode
62 #define Ulong    unsigned long
63 #define Ccstar   const char *
64
65 const char extra_reg_names1[][16] =
66 { "mv0", "mv1", "mv2",  "mv3",  "mv4",  "mv5",  "mv6",  "mv7",
67   "mv8", "mv9", "mv10", "mv11", "mv12", "mv13", "mv14", "mv15"
68 };
69 #define extra_reg_names1 bogus1_regnames
70
71 const struct attribute_spec arm_attribute_table[];
72
73 /* Forward function declarations.  */
74 static void      arm_add_gc_roots               PARAMS ((void));
75 static int       arm_gen_constant               PARAMS ((enum rtx_code, Mmode, Hint, rtx, rtx, int, int));
76 static unsigned  bit_count                      PARAMS ((Ulong));
77 static int       arm_address_register_rtx_p     PARAMS ((rtx, int));
78 static int       arm_legitimate_index_p         PARAMS ((enum machine_mode,
79                                                          rtx, int));
80 static int       thumb_base_register_rtx_p      PARAMS ((rtx, 
81                                                          enum machine_mode,
82                                                          int));
83 inline static int thumb_index_register_rtx_p    PARAMS ((rtx, int));
84 static int       const_ok_for_op                PARAMS ((Hint, enum rtx_code));
85 static int       eliminate_lr2ip                PARAMS ((rtx *));
86 static rtx       emit_multi_reg_push            PARAMS ((int));
87 static rtx       emit_sfm                       PARAMS ((int, int));
88 #ifndef AOF_ASSEMBLER
89 static bool      arm_assemble_integer           PARAMS ((rtx, unsigned int, int));
90 #endif
91 static Ccstar    fp_const_from_val              PARAMS ((REAL_VALUE_TYPE *));
92 static arm_cc    get_arm_condition_code         PARAMS ((rtx));
93 static void      init_fpa_table                 PARAMS ((void));
94 static Hint      int_log2                       PARAMS ((Hint));
95 static rtx       is_jump_table                  PARAMS ((rtx));
96 static Ccstar    output_multi_immediate         PARAMS ((rtx *, Ccstar, Ccstar, int, Hint));
97 static void      print_multi_reg                PARAMS ((FILE *, Ccstar, int, int));
98 static Mmode     select_dominance_cc_mode       PARAMS ((rtx, rtx, Hint));
99 static Ccstar    shift_op                       PARAMS ((rtx, Hint *));
100 static struct machine_function * arm_init_machine_status PARAMS ((void));
101 static int       number_of_first_bit_set        PARAMS ((int));
102 static void      replace_symbols_in_block       PARAMS ((tree, rtx, rtx));
103 static void      thumb_exit                     PARAMS ((FILE *, int, rtx));
104 static void      thumb_pushpop                  PARAMS ((FILE *, int, int));
105 static Ccstar    thumb_condition_code           PARAMS ((rtx, int));
106 static rtx       is_jump_table                  PARAMS ((rtx));
107 static Hint      get_jump_table_size            PARAMS ((rtx));
108 static Mnode *   move_minipool_fix_forward_ref  PARAMS ((Mnode *, Mnode *, Hint));
109 static Mnode *   add_minipool_forward_ref       PARAMS ((Mfix *));
110 static Mnode *   move_minipool_fix_backward_ref PARAMS ((Mnode *, Mnode *, Hint));
111 static Mnode *   add_minipool_backward_ref      PARAMS ((Mfix *));
112 static void      assign_minipool_offsets        PARAMS ((Mfix *));
113 static void      arm_print_value                PARAMS ((FILE *, rtx));
114 static void      dump_minipool                  PARAMS ((rtx));
115 static int       arm_barrier_cost               PARAMS ((rtx));
116 static Mfix *    create_fix_barrier             PARAMS ((Mfix *, Hint));
117 static void      push_minipool_barrier          PARAMS ((rtx, Hint));
118 static void      push_minipool_fix              PARAMS ((rtx, Hint, rtx *, Mmode, rtx));
119 static void      note_invalid_constants         PARAMS ((rtx, Hint));
120 static int       current_file_function_operand  PARAMS ((rtx));
121 static Ulong     arm_compute_save_reg0_reg12_mask  PARAMS ((void));
122 static Ulong     arm_compute_save_reg_mask      PARAMS ((void));
123 static Ulong     arm_isr_value                  PARAMS ((tree));
124 static Ulong     arm_compute_func_type          PARAMS ((void));
125 static tree      arm_handle_fndecl_attribute    PARAMS ((tree *, tree, tree, int, bool *));
126 static tree      arm_handle_isr_attribute       PARAMS ((tree *, tree, tree, int, bool *));
127 static void      arm_output_function_epilogue   PARAMS ((FILE *, Hint));
128 static void      arm_output_function_prologue   PARAMS ((FILE *, Hint));
129 static void      thumb_output_function_prologue PARAMS ((FILE *, Hint));
130 static int       arm_comp_type_attributes       PARAMS ((tree, tree));
131 static void      arm_set_default_type_attributes  PARAMS ((tree));
132 static int       arm_adjust_cost                PARAMS ((rtx, rtx, rtx, int));
133 static int       count_insns_for_constant       PARAMS ((HOST_WIDE_INT, int));
134 static int       arm_get_strip_length           PARAMS ((int));
135 static bool      arm_function_ok_for_sibcall    PARAMS ((tree, tree));
136 #ifdef OBJECT_FORMAT_ELF
137 static void      arm_elf_asm_named_section      PARAMS ((const char *, unsigned int));
138 #endif
139 #ifndef ARM_PE
140 static void      arm_encode_section_info        PARAMS ((tree, int));
141 #endif
142 #ifdef AOF_ASSEMBLER
143 static void      aof_globalize_label            PARAMS ((FILE *, const char *));
144 #endif
145 static void      arm_internal_label             PARAMS ((FILE *, const char *, unsigned long));
146 static void arm_output_mi_thunk                 PARAMS ((FILE *, tree,
147                                                          HOST_WIDE_INT,
148                                                          HOST_WIDE_INT, tree));
149 static int arm_rtx_costs_1                      PARAMS ((rtx, enum rtx_code,
150                                                          enum rtx_code));
151 static bool arm_rtx_costs                       PARAMS ((rtx, int, int, int*));
152 static int arm_address_cost                     PARAMS ((rtx));
153 static int       is_load_address                PARAMS ((rtx));
154 static int       is_cirrus_insn                 PARAMS ((rtx));
155 static void      cirrus_reorg                   PARAMS ((rtx));
156
157 #undef Hint
158 #undef Mmode
159 #undef Ulong
160 #undef Ccstar
161 \f
162 /* Initialize the GCC target structure.  */
163 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
164 #undef  TARGET_MERGE_DECL_ATTRIBUTES
165 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
166 #endif
167
168 #undef  TARGET_ATTRIBUTE_TABLE
169 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
170
171 #ifdef AOF_ASSEMBLER
172 #undef  TARGET_ASM_BYTE_OP
173 #define TARGET_ASM_BYTE_OP "\tDCB\t"
174 #undef  TARGET_ASM_ALIGNED_HI_OP
175 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
176 #undef  TARGET_ASM_ALIGNED_SI_OP
177 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
178 #undef TARGET_ASM_GLOBALIZE_LABEL
179 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
180 #else
181 #undef  TARGET_ASM_ALIGNED_SI_OP
182 #define TARGET_ASM_ALIGNED_SI_OP NULL
183 #undef  TARGET_ASM_INTEGER
184 #define TARGET_ASM_INTEGER arm_assemble_integer
185 #endif
186
187 #undef  TARGET_ASM_FUNCTION_PROLOGUE
188 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
189
190 #undef  TARGET_ASM_FUNCTION_EPILOGUE
191 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
192
193 #undef  TARGET_COMP_TYPE_ATTRIBUTES
194 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
195
196 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
197 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
198
199 #undef  TARGET_SCHED_ADJUST_COST
200 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
201
202 #undef TARGET_ENCODE_SECTION_INFO
203 #ifdef ARM_PE
204 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
205 #else
206 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
207 #endif
208
209 #undef TARGET_STRIP_NAME_ENCODING
210 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
211
212 #undef TARGET_ASM_INTERNAL_LABEL
213 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
214
215 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
216 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
217
218 #undef TARGET_ASM_OUTPUT_MI_THUNK
219 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
220 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
221 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
222
223 #undef TARGET_RTX_COSTS
224 #define TARGET_RTX_COSTS arm_rtx_costs
225 #undef TARGET_ADDRESS_COST
226 #define TARGET_ADDRESS_COST arm_address_cost
227
228 struct gcc_target targetm = TARGET_INITIALIZER;
229 \f
230 /* Obstack for minipool constant handling.  */
231 static struct obstack minipool_obstack;
232 static char *         minipool_startobj;
233
234 /* The maximum number of insns skipped which
235    will be conditionalised if possible.  */
236 static int max_insns_skipped = 5;
237
238 extern FILE * asm_out_file;
239
240 /* True if we are currently building a constant table.  */
241 int making_const_table;
242
243 /* Define the information needed to generate branch insns.  This is
244    stored from the compare operation.  */
245 rtx arm_compare_op0, arm_compare_op1;
246
247 /* What type of floating point are we tuning for?  */
248 enum floating_point_type arm_fpu;
249
250 /* What type of floating point instructions are available?  */
251 enum floating_point_type arm_fpu_arch;
252
253 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode.  */
254 enum prog_mode_type arm_prgmode;
255
256 /* Set by the -mfp=... option.  */
257 const char * target_fp_name = NULL;
258
259 /* Used to parse -mstructure_size_boundary command line option.  */
260 const char * structure_size_string = NULL;
261 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
262
263 /* Bit values used to identify processor capabilities.  */
264 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
265 #define FL_FAST_MULT  (1 << 1)        /* Fast multiply */
266 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
267 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
268 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
269 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
270 #define FL_THUMB      (1 << 6)        /* Thumb aware */
271 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
272 #define FL_STRONG     (1 << 8)        /* StrongARM */
273 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
274 #define FL_XSCALE     (1 << 10)       /* XScale */
275 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
276
277 /* The bits in this mask specify which
278    instructions we are allowed to generate.  */
279 static unsigned long insn_flags = 0;
280
281 /* The bits in this mask specify which instruction scheduling options should
282    be used.  Note - there is an overlap with the FL_FAST_MULT.  For some
283    hardware we want to be able to generate the multiply instructions, but to
284    tune as if they were not present in the architecture.  */
285 static unsigned long tune_flags = 0;
286
287 /* The following are used in the arm.md file as equivalents to bits
288    in the above two flag variables.  */
289
290 /* Nonzero if this is an "M" variant of the processor.  */
291 int arm_fast_multiply = 0;
292
293 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
294 int arm_arch4 = 0;
295
296 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
297 int arm_arch5 = 0;
298
299 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
300 int arm_arch5e = 0;
301
302 /* Nonzero if this chip can benefit from load scheduling.  */
303 int arm_ld_sched = 0;
304
305 /* Nonzero if this chip is a StrongARM.  */
306 int arm_is_strong = 0;
307
308 /* Nonzero if this chip is an XScale.  */
309 int arm_is_xscale = 0;
310
311 /* Nonzero if this chip is an ARM6 or an ARM7.  */
312 int arm_is_6_or_7 = 0;
313
314 /* Nonzero if this chip is a Cirrus/DSP.  */
315 int arm_is_cirrus = 0;
316
317 /* Nonzero if generating Thumb instructions.  */
318 int thumb_code = 0;
319
320 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
321    must report the mode of the memory reference from PRINT_OPERAND to
322    PRINT_OPERAND_ADDRESS.  */
323 enum machine_mode output_memory_reference_mode;
324
325 /* The register number to be used for the PIC offset register.  */
326 const char * arm_pic_register_string = NULL;
327 int arm_pic_register = INVALID_REGNUM;
328
329 /* Set to 1 when a return insn is output, this means that the epilogue
330    is not needed.  */
331 int return_used_this_function;
332
333 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
334    the next function.  */
335 static int after_arm_reorg = 0;
336
337 /* The maximum number of insns to be used when loading a constant.  */
338 static int arm_constant_limit = 3;
339
340 /* For an explanation of these variables, see final_prescan_insn below.  */
341 int arm_ccfsm_state;
342 enum arm_cond_code arm_current_cc;
343 rtx arm_target_insn;
344 int arm_target_label;
345
346 /* The condition codes of the ARM, and the inverse function.  */
347 static const char * const arm_condition_codes[] =
348 {
349   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
350   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
351 };
352
353 #define streq(string1, string2) (strcmp (string1, string2) == 0)
354 \f
355 /* Initialization code.  */
356
357 struct processors
358 {
359   const char *const name;
360   const unsigned long flags;
361 };
362
363 /* Not all of these give usefully different compilation alternatives,
364    but there is no simple way of generalizing them.  */
365 static const struct processors all_cores[] =
366 {
367   /* ARM Cores */
368   
369   {"arm2",      FL_CO_PROC | FL_MODE26 },
370   {"arm250",    FL_CO_PROC | FL_MODE26 },
371   {"arm3",      FL_CO_PROC | FL_MODE26 },
372   {"arm6",      FL_CO_PROC | FL_MODE26 | FL_MODE32 },
373   {"arm60",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
374   {"arm600",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
375   {"arm610",                 FL_MODE26 | FL_MODE32 },
376   {"arm620",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
377   {"arm7",      FL_CO_PROC | FL_MODE26 | FL_MODE32 },
378   /* arm7m doesn't exist on its own, but only with D, (and I), but
379      those don't alter the code, so arm7m is sometimes used.  */
380   {"arm7m",     FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
381   {"arm7d",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
382   {"arm7dm",    FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
383   {"arm7di",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
384   {"arm7dmi",   FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
385   {"arm70",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
386   {"arm700",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
387   {"arm700i",   FL_CO_PROC | FL_MODE26 | FL_MODE32 },
388   {"arm710",                 FL_MODE26 | FL_MODE32 },
389   {"arm710t",                FL_MODE26 | FL_MODE32                           | FL_THUMB },
390   {"arm720",                 FL_MODE26 | FL_MODE32 },
391   {"arm720t",                FL_MODE26 | FL_MODE32                           | FL_THUMB },
392   {"arm740t",                FL_MODE26 | FL_MODE32                           | FL_THUMB },
393   {"arm710c",                FL_MODE26 | FL_MODE32 },
394   {"arm7100",                FL_MODE26 | FL_MODE32 },
395   {"arm7500",                FL_MODE26 | FL_MODE32 },
396   /* Doesn't have an external co-proc, but does have embedded fpu.  */
397   {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
398   {"arm7tdmi",  FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
399   {"arm8",                   FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
400   {"arm810",                 FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
401   {"arm9",                               FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
402   {"arm920",                             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
403   {"arm920t",                            FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
404   {"arm940t",                            FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
405   {"arm9tdmi",                           FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
406   {"arm9e",                              FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
407   {"ep9312",                             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED |             FL_CIRRUS },
408   {"strongarm",              FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
409   {"strongarm110",           FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
410   {"strongarm1100",          FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
411   {"strongarm1110",          FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
412   {"arm10tdmi",                          FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED             | FL_ARCH5 },
413   {"arm1020t",                           FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED             | FL_ARCH5 },
414   {"xscale",                             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE },
415
416   {NULL, 0}
417 };
418
419 static const struct processors all_architectures[] =
420 {
421   /* ARM Architectures */
422   
423   { "armv2",     FL_CO_PROC | FL_MODE26 },
424   { "armv2a",    FL_CO_PROC | FL_MODE26 },
425   { "armv3",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
426   { "armv3m",    FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
427   { "armv4",     FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
428   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
429      implementations that support it, so we will leave it out for now.  */
430   { "armv4t",    FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
431   { "armv5",     FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
432   { "armv5t",    FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
433   { "armv5te",   FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E },
434   { "ep9312",                             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_CIRRUS },
435   { NULL, 0 }
436 };
437
438 /* This is a magic stucture.  The 'string' field is magically filled in
439    with a pointer to the value specified by the user on the command line
440    assuming that the user has specified such a value.  */
441
442 struct arm_cpu_select arm_select[] =
443 {
444   /* string       name            processors  */        
445   { NULL,       "-mcpu=",       all_cores  },
446   { NULL,       "-march=",      all_architectures },
447   { NULL,       "-mtune=",      all_cores }
448 };
449
450 /* Return the number of bits set in VALUE.  */
451 static unsigned
452 bit_count (value)
453      unsigned long value;
454 {
455   unsigned long count = 0;
456   
457   while (value)
458     {
459       count++;
460       value &= value - 1;  /* Clear the least-significant set bit.  */
461     }
462
463   return count;
464 }
465
466 /* Fix up any incompatible options that the user has specified.
467    This has now turned into a maze.  */
468 void
469 arm_override_options ()
470 {
471   unsigned i;
472   
473   /* Set up the flags based on the cpu/architecture selected by the user.  */
474   for (i = ARRAY_SIZE (arm_select); i--;)
475     {
476       struct arm_cpu_select * ptr = arm_select + i;
477       
478       if (ptr->string != NULL && ptr->string[0] != '\0')
479         {
480           const struct processors * sel;
481
482           for (sel = ptr->processors; sel->name != NULL; sel++)
483             if (streq (ptr->string, sel->name))
484               {
485                 if (i == 2)
486                   tune_flags = sel->flags;
487                 else
488                   {
489                     /* If we have been given an architecture and a processor
490                        make sure that they are compatible.  We only generate
491                        a warning though, and we prefer the CPU over the
492                        architecture.  */
493                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
494                       warning ("switch -mcpu=%s conflicts with -march= switch",
495                                ptr->string);
496                     
497                     insn_flags = sel->flags;
498                   }
499                 
500                 break;
501               }
502
503           if (sel->name == NULL)
504             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
505         }
506     }
507   
508   /* If the user did not specify a processor, choose one for them.  */
509   if (insn_flags == 0)
510     {
511       const struct processors * sel;
512       unsigned int        sought;
513       static const struct cpu_default
514       {
515         const int cpu;
516         const char *const name;
517       }
518       cpu_defaults[] =
519       {
520         { TARGET_CPU_arm2,      "arm2" },
521         { TARGET_CPU_arm6,      "arm6" },
522         { TARGET_CPU_arm610,    "arm610" },
523         { TARGET_CPU_arm710,    "arm710" },
524         { TARGET_CPU_arm7m,     "arm7m" },
525         { TARGET_CPU_arm7500fe, "arm7500fe" },
526         { TARGET_CPU_arm7tdmi,  "arm7tdmi" },
527         { TARGET_CPU_arm8,      "arm8" },
528         { TARGET_CPU_arm810,    "arm810" },
529         { TARGET_CPU_arm9,      "arm9" },
530         { TARGET_CPU_strongarm, "strongarm" },
531         { TARGET_CPU_xscale,    "xscale" },
532         { TARGET_CPU_ep9312,    "ep9312" },
533         { TARGET_CPU_generic,   "arm" },
534         { 0, 0 }
535       };
536       const struct cpu_default * def;
537           
538       /* Find the default.  */
539       for (def = cpu_defaults; def->name; def++)
540         if (def->cpu == TARGET_CPU_DEFAULT)
541           break;
542
543       /* Make sure we found the default CPU.  */
544       if (def->name == NULL)
545         abort ();
546       
547       /* Find the default CPU's flags.  */
548       for (sel = all_cores; sel->name != NULL; sel++)
549         if (streq (def->name, sel->name))
550           break;
551       
552       if (sel->name == NULL)
553         abort ();
554
555       insn_flags = sel->flags;
556       
557       /* Now check to see if the user has specified some command line
558          switch that require certain abilities from the cpu.  */
559       sought = 0;
560       
561       if (TARGET_INTERWORK || TARGET_THUMB)
562         {
563           sought |= (FL_THUMB | FL_MODE32);
564           
565           /* Force apcs-32 to be used for interworking.  */
566           target_flags |= ARM_FLAG_APCS_32;
567
568           /* There are no ARM processors that support both APCS-26 and
569              interworking.  Therefore we force FL_MODE26 to be removed
570              from insn_flags here (if it was set), so that the search
571              below will always be able to find a compatible processor.  */
572           insn_flags &= ~FL_MODE26;
573         }
574       else if (!TARGET_APCS_32)
575         sought |= FL_MODE26;
576       
577       if (sought != 0 && ((sought & insn_flags) != sought))
578         {
579           /* Try to locate a CPU type that supports all of the abilities
580              of the default CPU, plus the extra abilities requested by
581              the user.  */
582           for (sel = all_cores; sel->name != NULL; sel++)
583             if ((sel->flags & sought) == (sought | insn_flags))
584               break;
585
586           if (sel->name == NULL)
587             {
588               unsigned current_bit_count = 0;
589               const struct processors * best_fit = NULL;
590               
591               /* Ideally we would like to issue an error message here
592                  saying that it was not possible to find a CPU compatible
593                  with the default CPU, but which also supports the command
594                  line options specified by the programmer, and so they
595                  ought to use the -mcpu=<name> command line option to
596                  override the default CPU type.
597
598                  Unfortunately this does not work with multilibing.  We
599                  need to be able to support multilibs for -mapcs-26 and for
600                  -mthumb-interwork and there is no CPU that can support both
601                  options.  Instead if we cannot find a cpu that has both the
602                  characteristics of the default cpu and the given command line
603                  options we scan the array again looking for a best match.  */
604               for (sel = all_cores; sel->name != NULL; sel++)
605                 if ((sel->flags & sought) == sought)
606                   {
607                     unsigned count;
608
609                     count = bit_count (sel->flags & insn_flags);
610
611                     if (count >= current_bit_count)
612                       {
613                         best_fit = sel;
614                         current_bit_count = count;
615                       }
616                   }
617
618               if (best_fit == NULL)
619                 abort ();
620               else
621                 sel = best_fit;
622             }
623
624           insn_flags = sel->flags;
625         }
626     }
627   
628   /* If tuning has not been specified, tune for whichever processor or
629      architecture has been selected.  */
630   if (tune_flags == 0)
631     tune_flags = insn_flags;
632
633   /* Make sure that the processor choice does not conflict with any of the
634      other command line choices.  */
635   if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
636     {
637       /* If APCS-32 was not the default then it must have been set by the
638          user, so issue a warning message.  If the user has specified
639          "-mapcs-32 -mcpu=arm2" then we loose here.  */
640       if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
641         warning ("target CPU does not support APCS-32" );
642       target_flags &= ~ARM_FLAG_APCS_32;
643     }
644   else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
645     {
646       warning ("target CPU does not support APCS-26" );
647       target_flags |= ARM_FLAG_APCS_32;
648     }
649   
650   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
651     {
652       warning ("target CPU does not support interworking" );
653       target_flags &= ~ARM_FLAG_INTERWORK;
654     }
655   
656   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
657     {
658       warning ("target CPU does not support THUMB instructions");
659       target_flags &= ~ARM_FLAG_THUMB;
660     }
661
662   if (TARGET_APCS_FRAME && TARGET_THUMB)
663     {
664       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
665       target_flags &= ~ARM_FLAG_APCS_FRAME;
666     }
667
668   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
669      from here where no function is being compiled currently.  */
670   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
671       && TARGET_ARM)
672     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
673
674   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
675     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
676
677   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
678     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
679
680   /* If interworking is enabled then APCS-32 must be selected as well.  */
681   if (TARGET_INTERWORK)
682     {
683       if (!TARGET_APCS_32)
684         warning ("interworking forces APCS-32 to be used" );
685       target_flags |= ARM_FLAG_APCS_32;
686     }
687   
688   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
689     {
690       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
691       target_flags |= ARM_FLAG_APCS_FRAME;
692     }
693   
694   if (TARGET_POKE_FUNCTION_NAME)
695     target_flags |= ARM_FLAG_APCS_FRAME;
696   
697   if (TARGET_APCS_REENT && flag_pic)
698     error ("-fpic and -mapcs-reent are incompatible");
699   
700   if (TARGET_APCS_REENT)
701     warning ("APCS reentrant code not supported.  Ignored");
702   
703   /* If this target is normally configured to use APCS frames, warn if they
704      are turned off and debugging is turned on.  */
705   if (TARGET_ARM
706       && write_symbols != NO_DEBUG
707       && !TARGET_APCS_FRAME
708       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
709     warning ("-g with -mno-apcs-frame may not give sensible debugging");
710   
711   /* If stack checking is disabled, we can use r10 as the PIC register,
712      which keeps r9 available.  */
713   if (flag_pic)
714     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
715   
716   if (TARGET_APCS_FLOAT)
717     warning ("passing floating point arguments in fp regs not yet supported");
718   
719   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
720   arm_fast_multiply = (insn_flags & FL_FAST_MULT) != 0;
721   arm_arch4         = (insn_flags & FL_ARCH4) != 0;
722   arm_arch5         = (insn_flags & FL_ARCH5) != 0;
723   arm_arch5e        = (insn_flags & FL_ARCH5E) != 0;
724   arm_is_xscale     = (insn_flags & FL_XSCALE) != 0;
725
726   arm_ld_sched      = (tune_flags & FL_LDSCHED) != 0;
727   arm_is_strong     = (tune_flags & FL_STRONG) != 0;
728   thumb_code        = (TARGET_ARM == 0);
729   arm_is_6_or_7     = (((tune_flags & (FL_MODE26 | FL_MODE32))
730                        && !(tune_flags & FL_ARCH4))) != 0;
731   arm_is_cirrus     = (tune_flags & FL_CIRRUS) != 0;
732
733   if (arm_is_cirrus)
734     {
735       arm_fpu = FP_CIRRUS;
736
737       /* Ignore -mhard-float if -mcpu=ep9312.  */
738       if (TARGET_HARD_FLOAT)
739         target_flags ^= ARM_FLAG_SOFT_FLOAT;
740     }
741   else
742     /* Default value for floating point code... if no co-processor
743        bus, then schedule for emulated floating point.  Otherwise,
744        assume the user has an FPA.
745        Note: this does not prevent use of floating point instructions,
746        -msoft-float does that.  */
747     arm_fpu = (tune_flags & FL_CO_PROC) ? FP_HARD : FP_SOFT3;
748   
749   if (target_fp_name)
750     {
751       if (streq (target_fp_name, "2"))
752         arm_fpu_arch = FP_SOFT2;
753       else if (streq (target_fp_name, "3"))
754         arm_fpu_arch = FP_SOFT3;
755       else
756         error ("invalid floating point emulation option: -mfpe-%s",
757                target_fp_name);
758     }
759   else
760     arm_fpu_arch = FP_DEFAULT;
761   
762   if (TARGET_FPE)
763     {
764       if (arm_fpu == FP_SOFT3)
765         arm_fpu = FP_SOFT2;
766       else if (arm_fpu == FP_CIRRUS)
767         warning ("-mpfpe switch not supported by ep9312 target cpu - ignored.");
768       else if (arm_fpu != FP_HARD)
769     arm_fpu = FP_SOFT2;
770     }
771   
772   /* For arm2/3 there is no need to do any scheduling if there is only
773      a floating point emulator, or we are doing software floating-point.  */
774   if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD)
775       && (tune_flags & FL_MODE32) == 0)
776     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
777   
778   arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
779   
780   if (structure_size_string != NULL)
781     {
782       int size = strtol (structure_size_string, NULL, 0);
783       
784       if (size == 8 || size == 32)
785         arm_structure_size_boundary = size;
786       else
787         warning ("structure size boundary can only be set to 8 or 32");
788     }
789
790   if (arm_pic_register_string != NULL)
791     {
792       int pic_register = decode_reg_name (arm_pic_register_string);
793
794       if (!flag_pic)
795         warning ("-mpic-register= is useless without -fpic");
796
797       /* Prevent the user from choosing an obviously stupid PIC register.  */
798       else if (pic_register < 0 || call_used_regs[pic_register]
799                || pic_register == HARD_FRAME_POINTER_REGNUM
800                || pic_register == STACK_POINTER_REGNUM
801                || pic_register >= PC_REGNUM)
802         error ("unable to use '%s' for PIC register", arm_pic_register_string);
803       else
804         arm_pic_register = pic_register;
805     }
806
807   if (TARGET_THUMB && flag_schedule_insns)
808     {
809       /* Don't warn since it's on by default in -O2.  */
810       flag_schedule_insns = 0;
811     }
812
813   /* If optimizing for space, don't synthesize constants.
814      For processors with load scheduling, it never costs more than 2 cycles
815      to load a constant, and the load scheduler may well reduce that to 1.  */
816   if (optimize_size || (tune_flags & FL_LDSCHED))
817     arm_constant_limit = 1;
818   
819   if (arm_is_xscale)
820     arm_constant_limit = 2;
821
822   /* If optimizing for size, bump the number of instructions that we
823      are prepared to conditionally execute (even on a StrongARM). 
824      Otherwise for the StrongARM, which has early execution of branches,
825      a sequence that is worth skipping is shorter.  */
826   if (optimize_size)
827     max_insns_skipped = 6;
828   else if (arm_is_strong)
829     max_insns_skipped = 3;
830
831   /* Register global variables with the garbage collector.  */
832   arm_add_gc_roots ();
833 }
834
835 static void
836 arm_add_gc_roots ()
837 {
838   gcc_obstack_init(&minipool_obstack);
839   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
840 }
841 \f
842 /* A table of known ARM exception types.
843    For use with the interrupt function attribute.  */
844
845 typedef struct
846 {
847   const char *const arg;
848   const unsigned long return_value;
849 }
850 isr_attribute_arg;
851
852 static const isr_attribute_arg isr_attribute_args [] =
853 {
854   { "IRQ",   ARM_FT_ISR },
855   { "irq",   ARM_FT_ISR },
856   { "FIQ",   ARM_FT_FIQ },
857   { "fiq",   ARM_FT_FIQ },
858   { "ABORT", ARM_FT_ISR },
859   { "abort", ARM_FT_ISR },
860   { "ABORT", ARM_FT_ISR },
861   { "abort", ARM_FT_ISR },
862   { "UNDEF", ARM_FT_EXCEPTION },
863   { "undef", ARM_FT_EXCEPTION },
864   { "SWI",   ARM_FT_EXCEPTION },
865   { "swi",   ARM_FT_EXCEPTION },
866   { NULL,    ARM_FT_NORMAL }
867 };
868
869 /* Returns the (interrupt) function type of the current
870    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
871
872 static unsigned long
873 arm_isr_value (argument)
874      tree argument;
875 {
876   const isr_attribute_arg * ptr;
877   const char *              arg;
878
879   /* No argument - default to IRQ.  */
880   if (argument == NULL_TREE)
881     return ARM_FT_ISR;
882
883   /* Get the value of the argument.  */
884   if (TREE_VALUE (argument) == NULL_TREE
885       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
886     return ARM_FT_UNKNOWN;
887
888   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
889
890   /* Check it against the list of known arguments.  */
891   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr ++)
892     if (streq (arg, ptr->arg))
893       return ptr->return_value;
894
895   /* An unrecognized interrupt type.  */
896   return ARM_FT_UNKNOWN;
897 }
898
899 /* Computes the type of the current function.  */
900
901 static unsigned long
902 arm_compute_func_type ()
903 {
904   unsigned long type = ARM_FT_UNKNOWN;
905   tree a;
906   tree attr;
907   
908   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
909     abort ();
910
911   /* Decide if the current function is volatile.  Such functions
912      never return, and many memory cycles can be saved by not storing
913      register values that will never be needed again.  This optimization
914      was added to speed up context switching in a kernel application.  */
915   if (optimize > 0
916       && current_function_nothrow
917       && TREE_THIS_VOLATILE (current_function_decl))
918     type |= ARM_FT_VOLATILE;
919   
920   if (current_function_needs_context)
921     type |= ARM_FT_NESTED;
922
923   attr = DECL_ATTRIBUTES (current_function_decl);
924   
925   a = lookup_attribute ("naked", attr);
926   if (a != NULL_TREE)
927     type |= ARM_FT_NAKED;
928
929   if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
930     type |= ARM_FT_EXCEPTION_HANDLER;
931   else
932     {
933       a = lookup_attribute ("isr", attr);
934       if (a == NULL_TREE)
935         a = lookup_attribute ("interrupt", attr);
936       
937       if (a == NULL_TREE)
938         type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
939       else
940         type |= arm_isr_value (TREE_VALUE (a));
941     }
942   
943   return type;
944 }
945
946 /* Returns the type of the current function.  */
947
948 unsigned long
949 arm_current_func_type ()
950 {
951   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
952     cfun->machine->func_type = arm_compute_func_type ();
953
954   return cfun->machine->func_type;
955 }
956 \f
957 /* Return 1 if it is possible to return using a single instruction.  */
958
959 int
960 use_return_insn (iscond)
961      int iscond;
962 {
963   int regno;
964   unsigned int func_type;
965   unsigned long saved_int_regs;
966
967   /* Never use a return instruction before reload has run.  */
968   if (!reload_completed)
969     return 0;
970       
971   func_type = arm_current_func_type ();
972
973   /* Naked functions and volatile functions need special
974      consideration.  */
975   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
976     return 0;
977
978   /* So do interrupt functions that use the frame pointer.  */
979   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
980     return 0;
981   
982   /* As do variadic functions.  */
983   if (current_function_pretend_args_size
984       || cfun->machine->uses_anonymous_args
985       /* Of if the function calls __builtin_eh_return () */
986       || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
987       /* Or if there is no frame pointer and there is a stack adjustment.  */
988       || ((arm_get_frame_size () + current_function_outgoing_args_size != 0)
989           && !frame_pointer_needed))
990     return 0;
991
992   saved_int_regs = arm_compute_save_reg_mask ();
993
994   /* Can't be done if interworking with Thumb, and any registers have been
995      stacked.  */
996   if (TARGET_INTERWORK && saved_int_regs != 0)
997     return 0;
998
999   /* On StrongARM, conditional returns are expensive if they aren't
1000      taken and multiple registers have been stacked.  */
1001   if (iscond && arm_is_strong)
1002     {
1003       /* Conditional return when just the LR is stored is a simple 
1004          conditional-load instruction, that's not expensive.  */
1005       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1006         return 0;
1007
1008       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1009         return 0;
1010     }
1011
1012   /* If there are saved registers but the LR isn't saved, then we need
1013      two instructions for the return.  */
1014   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1015     return 0;
1016
1017   /* Can't be done if any of the FPU regs are pushed,
1018      since this also requires an insn.  */
1019   if (TARGET_HARD_FLOAT)
1020     for (regno = FIRST_ARM_FP_REGNUM; regno <= LAST_ARM_FP_REGNUM; regno++)
1021       if (regs_ever_live[regno] && !call_used_regs[regno])
1022         return 0;
1023
1024   return 1;
1025 }
1026
1027 /* Return TRUE if int I is a valid immediate ARM constant.  */
1028
1029 int
1030 const_ok_for_arm (i)
1031      HOST_WIDE_INT i;
1032 {
1033   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1034
1035   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must 
1036      be all zero, or all one.  */
1037   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1038       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1039           != ((~(unsigned HOST_WIDE_INT) 0)
1040               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1041     return FALSE;
1042   
1043   /* Fast return for 0 and powers of 2 */
1044   if ((i & (i - 1)) == 0)
1045     return TRUE;
1046
1047   do
1048     {
1049       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1050         return TRUE;
1051       mask =
1052           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1053                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1054     }
1055   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1056
1057   return FALSE;
1058 }
1059
1060 /* Return true if I is a valid constant for the operation CODE.  */
1061 static int
1062 const_ok_for_op (i, code)
1063      HOST_WIDE_INT i;
1064      enum rtx_code code;
1065 {
1066   if (const_ok_for_arm (i))
1067     return 1;
1068
1069   switch (code)
1070     {
1071     case PLUS:
1072       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1073
1074     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1075     case XOR:
1076     case IOR:
1077       return 0;
1078
1079     case AND:
1080       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1081
1082     default:
1083       abort ();
1084     }
1085 }
1086
1087 /* Emit a sequence of insns to handle a large constant.
1088    CODE is the code of the operation required, it can be any of SET, PLUS,
1089    IOR, AND, XOR, MINUS;
1090    MODE is the mode in which the operation is being performed;
1091    VAL is the integer to operate on;
1092    SOURCE is the other operand (a register, or a null-pointer for SET);
1093    SUBTARGETS means it is safe to create scratch registers if that will
1094    either produce a simpler sequence, or we will want to cse the values.
1095    Return value is the number of insns emitted.  */
1096
1097 int
1098 arm_split_constant (code, mode, val, target, source, subtargets)
1099      enum rtx_code code;
1100      enum machine_mode mode;
1101      HOST_WIDE_INT val;
1102      rtx target;
1103      rtx source;
1104      int subtargets;
1105 {
1106   if (subtargets || code == SET
1107       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1108           && REGNO (target) != REGNO (source)))
1109     {
1110       /* After arm_reorg has been called, we can't fix up expensive
1111          constants by pushing them into memory so we must synthesize
1112          them in-line, regardless of the cost.  This is only likely to
1113          be more costly on chips that have load delay slots and we are
1114          compiling without running the scheduler (so no splitting
1115          occurred before the final instruction emission).
1116
1117          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1118       */
1119       if (!after_arm_reorg
1120           && (arm_gen_constant (code, mode, val, target, source, 1, 0)
1121               > arm_constant_limit + (code != SET)))
1122         {
1123           if (code == SET)
1124             {
1125               /* Currently SET is the only monadic value for CODE, all
1126                  the rest are diadic.  */
1127               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1128               return 1;
1129             }
1130           else
1131             {
1132               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1133
1134               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1135               /* For MINUS, the value is subtracted from, since we never
1136                  have subtraction of a constant.  */
1137               if (code == MINUS)
1138                 emit_insn (gen_rtx_SET (VOIDmode, target,
1139                                         gen_rtx_MINUS (mode, temp, source)));
1140               else
1141                 emit_insn (gen_rtx_SET (VOIDmode, target,
1142                                         gen_rtx (code, mode, source, temp)));
1143               return 2;
1144             }
1145         }
1146     }
1147
1148   return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
1149 }
1150
1151 static int
1152 count_insns_for_constant (remainder, i)
1153      HOST_WIDE_INT remainder;
1154      int i;
1155 {
1156   HOST_WIDE_INT temp1;
1157   int num_insns = 0;
1158   do
1159     {
1160       int end;
1161           
1162       if (i <= 0)
1163         i += 32;
1164       if (remainder & (3 << (i - 2)))
1165         {
1166           end = i - 8;
1167           if (end < 0)
1168             end += 32;
1169           temp1 = remainder & ((0x0ff << end)
1170                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1171           remainder &= ~temp1;
1172           num_insns++;
1173           i -= 6;
1174         }
1175       i -= 2;
1176     } while (remainder);
1177   return num_insns;
1178 }
1179
1180 /* As above, but extra parameter GENERATE which, if clear, suppresses
1181    RTL generation.  */
1182
1183 static int
1184 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
1185      enum rtx_code code;
1186      enum machine_mode mode;
1187      HOST_WIDE_INT val;
1188      rtx target;
1189      rtx source;
1190      int subtargets;
1191      int generate;
1192 {
1193   int can_invert = 0;
1194   int can_negate = 0;
1195   int can_negate_initial = 0;
1196   int can_shift = 0;
1197   int i;
1198   int num_bits_set = 0;
1199   int set_sign_bit_copies = 0;
1200   int clear_sign_bit_copies = 0;
1201   int clear_zero_bit_copies = 0;
1202   int set_zero_bit_copies = 0;
1203   int insns = 0;
1204   unsigned HOST_WIDE_INT temp1, temp2;
1205   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1206
1207   /* Find out which operations are safe for a given CODE.  Also do a quick
1208      check for degenerate cases; these can occur when DImode operations
1209      are split.  */
1210   switch (code)
1211     {
1212     case SET:
1213       can_invert = 1;
1214       can_shift = 1;
1215       can_negate = 1;
1216       break;
1217
1218     case PLUS:
1219       can_negate = 1;
1220       can_negate_initial = 1;
1221       break;
1222
1223     case IOR:
1224       if (remainder == 0xffffffff)
1225         {
1226           if (generate)
1227             emit_insn (gen_rtx_SET (VOIDmode, target,
1228                                     GEN_INT (ARM_SIGN_EXTEND (val))));
1229           return 1;
1230         }
1231       if (remainder == 0)
1232         {
1233           if (reload_completed && rtx_equal_p (target, source))
1234             return 0;
1235           if (generate)
1236             emit_insn (gen_rtx_SET (VOIDmode, target, source));
1237           return 1;
1238         }
1239       break;
1240
1241     case AND:
1242       if (remainder == 0)
1243         {
1244           if (generate)
1245             emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
1246           return 1;
1247         }
1248       if (remainder == 0xffffffff)
1249         {
1250           if (reload_completed && rtx_equal_p (target, source))
1251             return 0;
1252           if (generate)
1253             emit_insn (gen_rtx_SET (VOIDmode, target, source));
1254           return 1;
1255         }
1256       can_invert = 1;
1257       break;
1258
1259     case XOR:
1260       if (remainder == 0)
1261         {
1262           if (reload_completed && rtx_equal_p (target, source))
1263             return 0;
1264           if (generate)
1265             emit_insn (gen_rtx_SET (VOIDmode, target, source));
1266           return 1;
1267         }
1268       if (remainder == 0xffffffff)
1269         {
1270           if (generate)
1271             emit_insn (gen_rtx_SET (VOIDmode, target,
1272                                     gen_rtx_NOT (mode, source)));
1273           return 1;
1274         }
1275
1276       /* We don't know how to handle this yet below.  */
1277       abort ();
1278
1279     case MINUS:
1280       /* We treat MINUS as (val - source), since (source - val) is always
1281          passed as (source + (-val)).  */
1282       if (remainder == 0)
1283         {
1284           if (generate)
1285             emit_insn (gen_rtx_SET (VOIDmode, target,
1286                                     gen_rtx_NEG (mode, source)));
1287           return 1;
1288         }
1289       if (const_ok_for_arm (val))
1290         {
1291           if (generate)
1292             emit_insn (gen_rtx_SET (VOIDmode, target, 
1293                                     gen_rtx_MINUS (mode, GEN_INT (val),
1294                                                    source)));
1295           return 1;
1296         }
1297       can_negate = 1;
1298
1299       break;
1300
1301     default:
1302       abort ();
1303     }
1304
1305   /* If we can do it in one insn get out quickly.  */
1306   if (const_ok_for_arm (val)
1307       || (can_negate_initial && const_ok_for_arm (-val))
1308       || (can_invert && const_ok_for_arm (~val)))
1309     {
1310       if (generate)
1311         emit_insn (gen_rtx_SET (VOIDmode, target,
1312                                 (source ? gen_rtx (code, mode, source,
1313                                                    GEN_INT (val))
1314                                  : GEN_INT (val))));
1315       return 1;
1316     }
1317
1318   /* Calculate a few attributes that may be useful for specific
1319      optimizations.  */
1320   for (i = 31; i >= 0; i--)
1321     {
1322       if ((remainder & (1 << i)) == 0)
1323         clear_sign_bit_copies++;
1324       else
1325         break;
1326     }
1327
1328   for (i = 31; i >= 0; i--)
1329     {
1330       if ((remainder & (1 << i)) != 0)
1331         set_sign_bit_copies++;
1332       else
1333         break;
1334     }
1335
1336   for (i = 0; i <= 31; i++)
1337     {
1338       if ((remainder & (1 << i)) == 0)
1339         clear_zero_bit_copies++;
1340       else
1341         break;
1342     }
1343
1344   for (i = 0; i <= 31; i++)
1345     {
1346       if ((remainder & (1 << i)) != 0)
1347         set_zero_bit_copies++;
1348       else
1349         break;
1350     }
1351
1352   switch (code)
1353     {
1354     case SET:
1355       /* See if we can do this by sign_extending a constant that is known
1356          to be negative.  This is a good, way of doing it, since the shift
1357          may well merge into a subsequent insn.  */
1358       if (set_sign_bit_copies > 1)
1359         {
1360           if (const_ok_for_arm
1361               (temp1 = ARM_SIGN_EXTEND (remainder 
1362                                         << (set_sign_bit_copies - 1))))
1363             {
1364               if (generate)
1365                 {
1366                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1367                   emit_insn (gen_rtx_SET (VOIDmode, new_src, 
1368                                           GEN_INT (temp1)));
1369                   emit_insn (gen_ashrsi3 (target, new_src, 
1370                                           GEN_INT (set_sign_bit_copies - 1)));
1371                 }
1372               return 2;
1373             }
1374           /* For an inverted constant, we will need to set the low bits,
1375              these will be shifted out of harm's way.  */
1376           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1377           if (const_ok_for_arm (~temp1))
1378             {
1379               if (generate)
1380                 {
1381                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1382                   emit_insn (gen_rtx_SET (VOIDmode, new_src,
1383                                           GEN_INT (temp1)));
1384                   emit_insn (gen_ashrsi3 (target, new_src, 
1385                                           GEN_INT (set_sign_bit_copies - 1)));
1386                 }
1387               return 2;
1388             }
1389         }
1390
1391       /* See if we can generate this by setting the bottom (or the top)
1392          16 bits, and then shifting these into the other half of the
1393          word.  We only look for the simplest cases, to do more would cost
1394          too much.  Be careful, however, not to generate this when the
1395          alternative would take fewer insns.  */
1396       if (val & 0xffff0000)
1397         {
1398           temp1 = remainder & 0xffff0000;
1399           temp2 = remainder & 0x0000ffff;
1400
1401           /* Overlaps outside this range are best done using other methods.  */
1402           for (i = 9; i < 24; i++)
1403             {
1404               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1405                   && !const_ok_for_arm (temp2))
1406                 {
1407                   rtx new_src = (subtargets
1408                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1409                                  : target);
1410                   insns = arm_gen_constant (code, mode, temp2, new_src,
1411                                             source, subtargets, generate);
1412                   source = new_src;
1413                   if (generate)
1414                     emit_insn (gen_rtx_SET
1415                                (VOIDmode, target,
1416                                 gen_rtx_IOR (mode,
1417                                              gen_rtx_ASHIFT (mode, source,
1418                                                              GEN_INT (i)),
1419                                              source)));
1420                   return insns + 1;
1421                 }
1422             }
1423
1424           /* Don't duplicate cases already considered.  */
1425           for (i = 17; i < 24; i++)
1426             {
1427               if (((temp1 | (temp1 >> i)) == remainder)
1428                   && !const_ok_for_arm (temp1))
1429                 {
1430                   rtx new_src = (subtargets
1431                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1432                                  : target);
1433                   insns = arm_gen_constant (code, mode, temp1, new_src,
1434                                             source, subtargets, generate);
1435                   source = new_src;
1436                   if (generate)
1437                     emit_insn
1438                       (gen_rtx_SET (VOIDmode, target,
1439                                     gen_rtx_IOR
1440                                     (mode,
1441                                      gen_rtx_LSHIFTRT (mode, source,
1442                                                        GEN_INT (i)),
1443                                      source)));
1444                   return insns + 1;
1445                 }
1446             }
1447         }
1448       break;
1449
1450     case IOR:
1451     case XOR:
1452       /* If we have IOR or XOR, and the constant can be loaded in a
1453          single instruction, and we can find a temporary to put it in,
1454          then this can be done in two instructions instead of 3-4.  */
1455       if (subtargets
1456           /* TARGET can't be NULL if SUBTARGETS is 0 */
1457           || (reload_completed && !reg_mentioned_p (target, source)))
1458         {
1459           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1460             {
1461               if (generate)
1462                 {
1463                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1464
1465                   emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
1466                   emit_insn (gen_rtx_SET (VOIDmode, target, 
1467                                           gen_rtx (code, mode, source, sub)));
1468                 }
1469               return 2;
1470             }
1471         }
1472
1473       if (code == XOR)
1474         break;
1475
1476       if (set_sign_bit_copies > 8
1477           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1478         {
1479           if (generate)
1480             {
1481               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1482               rtx shift = GEN_INT (set_sign_bit_copies);
1483
1484               emit_insn (gen_rtx_SET (VOIDmode, sub,
1485                                       gen_rtx_NOT (mode, 
1486                                                    gen_rtx_ASHIFT (mode,
1487                                                                    source, 
1488                                                                    shift))));
1489               emit_insn (gen_rtx_SET (VOIDmode, target,
1490                                       gen_rtx_NOT (mode,
1491                                                    gen_rtx_LSHIFTRT (mode, sub,
1492                                                                      shift))));
1493             }
1494           return 2;
1495         }
1496
1497       if (set_zero_bit_copies > 8
1498           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1499         {
1500           if (generate)
1501             {
1502               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1503               rtx shift = GEN_INT (set_zero_bit_copies);
1504
1505               emit_insn (gen_rtx_SET (VOIDmode, sub,
1506                                       gen_rtx_NOT (mode,
1507                                                    gen_rtx_LSHIFTRT (mode,
1508                                                                      source,
1509                                                                      shift))));
1510               emit_insn (gen_rtx_SET (VOIDmode, target,
1511                                       gen_rtx_NOT (mode,
1512                                                    gen_rtx_ASHIFT (mode, sub,
1513                                                                    shift))));
1514             }
1515           return 2;
1516         }
1517
1518       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1519         {
1520           if (generate)
1521             {
1522               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1523               emit_insn (gen_rtx_SET (VOIDmode, sub,
1524                                       gen_rtx_NOT (mode, source)));
1525               source = sub;
1526               if (subtargets)
1527                 sub = gen_reg_rtx (mode);
1528               emit_insn (gen_rtx_SET (VOIDmode, sub,
1529                                       gen_rtx_AND (mode, source, 
1530                                                    GEN_INT (temp1))));
1531               emit_insn (gen_rtx_SET (VOIDmode, target,
1532                                       gen_rtx_NOT (mode, sub)));
1533             }
1534           return 3;
1535         }
1536       break;
1537
1538     case AND:
1539       /* See if two shifts will do 2 or more insn's worth of work.  */
1540       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1541         {
1542           HOST_WIDE_INT shift_mask = ((0xffffffff
1543                                        << (32 - clear_sign_bit_copies))
1544                                       & 0xffffffff);
1545
1546           if ((remainder | shift_mask) != 0xffffffff)
1547             {
1548               if (generate)
1549                 {
1550                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1551                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1552                                             new_src, source, subtargets, 1);
1553                   source = new_src;
1554                 }
1555               else
1556                 {
1557                   rtx targ = subtargets ? NULL_RTX : target;
1558                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1559                                             targ, source, subtargets, 0);
1560                 }
1561             }
1562
1563           if (generate)
1564             {
1565               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1566               rtx shift = GEN_INT (clear_sign_bit_copies);
1567
1568               emit_insn (gen_ashlsi3 (new_src, source, shift));
1569               emit_insn (gen_lshrsi3 (target, new_src, shift));
1570             }
1571
1572           return insns + 2;
1573         }
1574
1575       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1576         {
1577           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1578           
1579           if ((remainder | shift_mask) != 0xffffffff)
1580             {
1581               if (generate)
1582                 {
1583                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1584
1585                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1586                                             new_src, source, subtargets, 1);
1587                   source = new_src;
1588                 }
1589               else
1590                 {
1591                   rtx targ = subtargets ? NULL_RTX : target;
1592
1593                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1594                                             targ, source, subtargets, 0);
1595                 }
1596             }
1597
1598           if (generate)
1599             {
1600               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1601               rtx shift = GEN_INT (clear_zero_bit_copies);
1602
1603               emit_insn (gen_lshrsi3 (new_src, source, shift));
1604               emit_insn (gen_ashlsi3 (target, new_src, shift));
1605             }
1606
1607           return insns + 2;
1608         }
1609
1610       break;
1611
1612     default:
1613       break;
1614     }
1615
1616   for (i = 0; i < 32; i++)
1617     if (remainder & (1 << i))
1618       num_bits_set++;
1619
1620   if (code == AND || (can_invert && num_bits_set > 16))
1621     remainder = (~remainder) & 0xffffffff;
1622   else if (code == PLUS && num_bits_set > 16)
1623     remainder = (-remainder) & 0xffffffff;
1624   else
1625     {
1626       can_invert = 0;
1627       can_negate = 0;
1628     }
1629
1630   /* Now try and find a way of doing the job in either two or three
1631      instructions.
1632      We start by looking for the largest block of zeros that are aligned on
1633      a 2-bit boundary, we then fill up the temps, wrapping around to the
1634      top of the word when we drop off the bottom.
1635      In the worst case this code should produce no more than four insns.  */
1636   {
1637     int best_start = 0;
1638     int best_consecutive_zeros = 0;
1639
1640     for (i = 0; i < 32; i += 2)
1641       {
1642         int consecutive_zeros = 0;
1643
1644         if (!(remainder & (3 << i)))
1645           {
1646             while ((i < 32) && !(remainder & (3 << i)))
1647               {
1648                 consecutive_zeros += 2;
1649                 i += 2;
1650               }
1651             if (consecutive_zeros > best_consecutive_zeros)
1652               {
1653                 best_consecutive_zeros = consecutive_zeros;
1654                 best_start = i - consecutive_zeros;
1655               }
1656             i -= 2;
1657           }
1658       }
1659
1660     /* So long as it won't require any more insns to do so, it's
1661        desirable to emit a small constant (in bits 0...9) in the last
1662        insn.  This way there is more chance that it can be combined with
1663        a later addressing insn to form a pre-indexed load or store
1664        operation.  Consider:
1665
1666                *((volatile int *)0xe0000100) = 1;
1667                *((volatile int *)0xe0000110) = 2;
1668
1669        We want this to wind up as:
1670
1671                 mov rA, #0xe0000000
1672                 mov rB, #1
1673                 str rB, [rA, #0x100]
1674                 mov rB, #2
1675                 str rB, [rA, #0x110]
1676
1677        rather than having to synthesize both large constants from scratch.
1678
1679        Therefore, we calculate how many insns would be required to emit
1680        the constant starting from `best_start', and also starting from 
1681        zero (ie with bit 31 first to be output).  If `best_start' doesn't 
1682        yield a shorter sequence, we may as well use zero.  */
1683     if (best_start != 0
1684         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1685         && (count_insns_for_constant (remainder, 0) <= 
1686             count_insns_for_constant (remainder, best_start)))
1687       best_start = 0;
1688
1689     /* Now start emitting the insns.  */
1690     i = best_start;
1691     do
1692       {
1693         int end;
1694
1695         if (i <= 0)
1696           i += 32;
1697         if (remainder & (3 << (i - 2)))
1698           {
1699             end = i - 8;
1700             if (end < 0)
1701               end += 32;
1702             temp1 = remainder & ((0x0ff << end)
1703                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
1704             remainder &= ~temp1;
1705
1706             if (generate)
1707               {
1708                 rtx new_src, temp1_rtx;
1709
1710                 if (code == SET || code == MINUS)
1711                   {
1712                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
1713                     if (can_invert && code != MINUS)
1714                       temp1 = ~temp1;
1715                   }
1716                 else
1717                   {
1718                     if (remainder && subtargets)
1719                       new_src = gen_reg_rtx (mode);
1720                     else
1721                       new_src = target;
1722                     if (can_invert)
1723                       temp1 = ~temp1;
1724                     else if (can_negate)
1725                       temp1 = -temp1;
1726                   }
1727
1728                 temp1 = trunc_int_for_mode (temp1, mode);
1729                 temp1_rtx = GEN_INT (temp1);
1730
1731                 if (code == SET)
1732                   ;
1733                 else if (code == MINUS)
1734                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
1735                 else
1736                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
1737
1738                 emit_insn (gen_rtx_SET (VOIDmode, new_src, temp1_rtx));
1739                 source = new_src;
1740               }
1741
1742             if (code == SET)
1743               {
1744                 can_invert = 0;
1745                 code = PLUS;
1746               }
1747             else if (code == MINUS)
1748               code = PLUS;
1749
1750             insns++;
1751             i -= 6;
1752           }
1753         i -= 2;
1754       }
1755     while (remainder);
1756   }
1757
1758   return insns;
1759 }
1760
1761 /* Canonicalize a comparison so that we are more likely to recognize it.
1762    This can be done for a few constant compares, where we can make the
1763    immediate value easier to load.  */
1764
1765 enum rtx_code
1766 arm_canonicalize_comparison (code, op1)
1767      enum rtx_code code;
1768      rtx * op1;
1769 {
1770   unsigned HOST_WIDE_INT i = INTVAL (*op1);
1771
1772   switch (code)
1773     {
1774     case EQ:
1775     case NE:
1776       return code;
1777
1778     case GT:
1779     case LE:
1780       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1781           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1782         {
1783           *op1 = GEN_INT (i + 1);
1784           return code == GT ? GE : LT;
1785         }
1786       break;
1787
1788     case GE:
1789     case LT:
1790       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1791           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1792         {
1793           *op1 = GEN_INT (i - 1);
1794           return code == GE ? GT : LE;
1795         }
1796       break;
1797
1798     case GTU:
1799     case LEU:
1800       if (i != ~((unsigned HOST_WIDE_INT) 0)
1801           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
1802         {
1803           *op1 = GEN_INT (i + 1);
1804           return code == GTU ? GEU : LTU;
1805         }
1806       break;
1807
1808     case GEU:
1809     case LTU:
1810       if (i != 0
1811           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
1812         {
1813           *op1 = GEN_INT (i - 1);
1814           return code == GEU ? GTU : LEU;
1815         }
1816       break;
1817
1818     default:
1819       abort ();
1820     }
1821
1822   return code;
1823 }
1824
1825 /* Decide whether a type should be returned in memory (true)
1826    or in a register (false).  This is called by the macro
1827    RETURN_IN_MEMORY.  */
1828
1829 int
1830 arm_return_in_memory (type)
1831      tree type;
1832 {
1833   HOST_WIDE_INT size;
1834
1835   if (!AGGREGATE_TYPE_P (type))
1836     /* All simple types are returned in registers.  */
1837     return 0;
1838
1839   size = int_size_in_bytes (type);
1840
1841   if (TARGET_ATPCS)
1842     {
1843       /* ATPCS returns aggregate types in memory only if they are
1844          larger than a word (or are variable size).  */
1845       return (size < 0 || size > UNITS_PER_WORD);
1846     }
1847   
1848   /* For the arm-wince targets we choose to be compatible with Microsoft's
1849      ARM and Thumb compilers, which always return aggregates in memory.  */
1850 #ifndef ARM_WINCE
1851   /* All structures/unions bigger than one word are returned in memory.
1852      Also catch the case where int_size_in_bytes returns -1.  In this case
1853      the aggregate is either huge or of variable size, and in either case
1854      we will want to return it via memory and not in a register.  */
1855   if (size < 0 || size > UNITS_PER_WORD)
1856     return 1;
1857   
1858   if (TREE_CODE (type) == RECORD_TYPE)
1859     {
1860       tree field;
1861
1862       /* For a struct the APCS says that we only return in a register
1863          if the type is 'integer like' and every addressable element
1864          has an offset of zero.  For practical purposes this means
1865          that the structure can have at most one non bit-field element
1866          and that this element must be the first one in the structure.  */
1867       
1868       /* Find the first field, ignoring non FIELD_DECL things which will
1869          have been created by C++.  */
1870       for (field = TYPE_FIELDS (type);
1871            field && TREE_CODE (field) != FIELD_DECL;
1872            field = TREE_CHAIN (field))
1873         continue;
1874       
1875       if (field == NULL)
1876         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
1877
1878       /* Check that the first field is valid for returning in a register.  */
1879
1880       /* ... Floats are not allowed */
1881       if (FLOAT_TYPE_P (TREE_TYPE (field)))
1882         return 1;
1883
1884       /* ... Aggregates that are not themselves valid for returning in
1885          a register are not allowed.  */
1886       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1887         return 1;
1888
1889       /* Now check the remaining fields, if any.  Only bitfields are allowed,
1890          since they are not addressable.  */
1891       for (field = TREE_CHAIN (field);
1892            field;
1893            field = TREE_CHAIN (field))
1894         {
1895           if (TREE_CODE (field) != FIELD_DECL)
1896             continue;
1897           
1898           if (!DECL_BIT_FIELD_TYPE (field))
1899             return 1;
1900         }
1901
1902       return 0;
1903     }
1904   
1905   if (TREE_CODE (type) == UNION_TYPE)
1906     {
1907       tree field;
1908
1909       /* Unions can be returned in registers if every element is
1910          integral, or can be returned in an integer register.  */
1911       for (field = TYPE_FIELDS (type);
1912            field;
1913            field = TREE_CHAIN (field))
1914         {
1915           if (TREE_CODE (field) != FIELD_DECL)
1916             continue;
1917
1918           if (FLOAT_TYPE_P (TREE_TYPE (field)))
1919             return 1;
1920           
1921           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1922             return 1;
1923         }
1924       
1925       return 0;
1926     }
1927 #endif /* not ARM_WINCE */  
1928   
1929   /* Return all other types in memory.  */
1930   return 1;
1931 }
1932
1933 /* Indicate whether or not words of a double are in big-endian order. */
1934
1935 int
1936 arm_float_words_big_endian ()
1937 {
1938   if (TARGET_CIRRUS)
1939     return 0;
1940
1941   /* For FPA, float words are always big-endian.  For VFP, floats words
1942      follow the memory system mode.  */
1943
1944   if (TARGET_HARD_FLOAT)
1945     {
1946       /* FIXME: TARGET_HARD_FLOAT currently implies FPA.  */
1947       return 1;
1948     }
1949
1950   if (TARGET_VFP)
1951     return (TARGET_BIG_END ? 1 : 0);
1952
1953   return 1;
1954 }
1955
1956 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1957    for a call to a function whose data type is FNTYPE.
1958    For a library call, FNTYPE is NULL.  */
1959 void
1960 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1961      CUMULATIVE_ARGS * pcum;
1962      tree fntype;
1963      rtx libname  ATTRIBUTE_UNUSED;
1964      int indirect ATTRIBUTE_UNUSED;
1965 {
1966   /* On the ARM, the offset starts at 0.  */
1967   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype))) ? 1 : 0);
1968   
1969   pcum->call_cookie = CALL_NORMAL;
1970
1971   if (TARGET_LONG_CALLS)
1972     pcum->call_cookie = CALL_LONG;
1973     
1974   /* Check for long call/short call attributes.  The attributes
1975      override any command line option.  */
1976   if (fntype)
1977     {
1978       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1979         pcum->call_cookie = CALL_SHORT;
1980       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1981         pcum->call_cookie = CALL_LONG;
1982     }
1983 }
1984
1985 /* Determine where to put an argument to a function.
1986    Value is zero to push the argument on the stack,
1987    or a hard register in which to store the argument.
1988
1989    MODE is the argument's machine mode.
1990    TYPE is the data type of the argument (as a tree).
1991     This is null for libcalls where that information may
1992     not be available.
1993    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1994     the preceding args and about the function being called.
1995    NAMED is nonzero if this argument is a named parameter
1996     (otherwise it is an extra parameter matching an ellipsis).  */
1997
1998 rtx
1999 arm_function_arg (pcum, mode, type, named)
2000      CUMULATIVE_ARGS * pcum;
2001      enum machine_mode mode;
2002      tree type ATTRIBUTE_UNUSED;
2003      int named;
2004 {
2005   if (mode == VOIDmode)
2006     /* Compute operand 2 of the call insn.  */
2007     return GEN_INT (pcum->call_cookie);
2008   
2009   if (!named || pcum->nregs >= NUM_ARG_REGS)
2010     return NULL_RTX;
2011   
2012   return gen_rtx_REG (mode, pcum->nregs);
2013 }
2014
2015 /* Variable sized types are passed by reference.  This is a GCC
2016    extension to the ARM ABI.  */
2017
2018 int
2019 arm_function_arg_pass_by_reference (cum, mode, type, named)
2020      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
2021      enum machine_mode mode ATTRIBUTE_UNUSED;
2022      tree type;
2023      int named ATTRIBUTE_UNUSED;
2024 {
2025   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2026 }
2027
2028 /* Implement va_arg.  */
2029
2030 rtx
2031 arm_va_arg (valist, type)
2032      tree valist, type;
2033 {
2034   /* Variable sized types are passed by reference.  */
2035   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2036     {
2037       rtx addr = std_expand_builtin_va_arg (valist, build_pointer_type (type));
2038       return gen_rtx_MEM (ptr_mode, force_reg (Pmode, addr));
2039     }
2040
2041   return std_expand_builtin_va_arg (valist, type);
2042 }
2043 \f
2044 /* Encode the current state of the #pragma [no_]long_calls.  */
2045 typedef enum
2046 {
2047   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2048   LONG,         /* #pragma long_calls is in effect.  */
2049   SHORT         /* #pragma no_long_calls is in effect.  */
2050 } arm_pragma_enum;
2051
2052 static arm_pragma_enum arm_pragma_long_calls = OFF;
2053
2054 void
2055 arm_pr_long_calls (pfile)
2056      struct cpp_reader * pfile ATTRIBUTE_UNUSED;
2057 {
2058   arm_pragma_long_calls = LONG;
2059 }
2060
2061 void
2062 arm_pr_no_long_calls (pfile)
2063      struct cpp_reader * pfile ATTRIBUTE_UNUSED;
2064 {
2065   arm_pragma_long_calls = SHORT;
2066 }
2067
2068 void
2069 arm_pr_long_calls_off (pfile)
2070      struct cpp_reader * pfile ATTRIBUTE_UNUSED;
2071 {
2072   arm_pragma_long_calls = OFF;
2073 }
2074 \f
2075 /* Table of machine attributes.  */
2076 const struct attribute_spec arm_attribute_table[] =
2077 {
2078   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2079   /* Function calls made to this symbol must be done indirectly, because
2080      it may lie outside of the 26 bit addressing range of a normal function
2081      call.  */
2082   { "long_call",    0, 0, false, true,  true,  NULL },
2083   /* Whereas these functions are always known to reside within the 26 bit
2084      addressing range.  */
2085   { "short_call",   0, 0, false, true,  true,  NULL },
2086   /* Interrupt Service Routines have special prologue and epilogue requirements.  */ 
2087   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2088   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2089   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2090 #ifdef ARM_PE
2091   /* ARM/PE has three new attributes:
2092      interfacearm - ?
2093      dllexport - for exporting a function/variable that will live in a dll
2094      dllimport - for importing a function/variable from a dll
2095
2096      Microsoft allows multiple declspecs in one __declspec, separating
2097      them with spaces.  We do NOT support this.  Instead, use __declspec
2098      multiple times.
2099   */
2100   { "dllimport",    0, 0, true,  false, false, NULL },
2101   { "dllexport",    0, 0, true,  false, false, NULL },
2102   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2103 #endif
2104   { NULL,           0, 0, false, false, false, NULL }
2105 };
2106
2107 /* Handle an attribute requiring a FUNCTION_DECL;
2108    arguments as in struct attribute_spec.handler.  */
2109
2110 static tree
2111 arm_handle_fndecl_attribute (node, name, args, flags, no_add_attrs)
2112      tree * node;
2113      tree   name;
2114      tree   args ATTRIBUTE_UNUSED;
2115      int    flags ATTRIBUTE_UNUSED;
2116      bool * no_add_attrs;
2117 {
2118   if (TREE_CODE (*node) != FUNCTION_DECL)
2119     {
2120       warning ("`%s' attribute only applies to functions",
2121                IDENTIFIER_POINTER (name));
2122       *no_add_attrs = true;
2123     }
2124
2125   return NULL_TREE;
2126 }
2127
2128 /* Handle an "interrupt" or "isr" attribute;
2129    arguments as in struct attribute_spec.handler.  */
2130
2131 static tree
2132 arm_handle_isr_attribute (node, name, args, flags, no_add_attrs)
2133      tree * node;
2134      tree   name;
2135      tree   args;
2136      int    flags;
2137      bool * no_add_attrs;
2138 {
2139   if (DECL_P (*node))
2140     {
2141       if (TREE_CODE (*node) != FUNCTION_DECL)
2142         {
2143           warning ("`%s' attribute only applies to functions",
2144                    IDENTIFIER_POINTER (name));
2145           *no_add_attrs = true;
2146         }
2147       /* FIXME: the argument if any is checked for type attributes;
2148          should it be checked for decl ones?  */
2149     }
2150   else
2151     {
2152       if (TREE_CODE (*node) == FUNCTION_TYPE
2153           || TREE_CODE (*node) == METHOD_TYPE)
2154         {
2155           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2156             {
2157               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2158               *no_add_attrs = true;
2159             }
2160         }
2161       else if (TREE_CODE (*node) == POINTER_TYPE
2162                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2163                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2164                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2165         {
2166           *node = build_type_copy (*node);
2167           TREE_TYPE (*node) = build_type_attribute_variant
2168             (TREE_TYPE (*node),
2169              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2170           *no_add_attrs = true;
2171         }
2172       else
2173         {
2174           /* Possibly pass this attribute on from the type to a decl.  */
2175           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2176                        | (int) ATTR_FLAG_FUNCTION_NEXT
2177                        | (int) ATTR_FLAG_ARRAY_NEXT))
2178             {
2179               *no_add_attrs = true;
2180               return tree_cons (name, args, NULL_TREE);
2181             }
2182           else
2183             {
2184               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2185             }
2186         }
2187     }
2188
2189   return NULL_TREE;
2190 }
2191
2192 /* Return 0 if the attributes for two types are incompatible, 1 if they
2193    are compatible, and 2 if they are nearly compatible (which causes a
2194    warning to be generated).  */
2195
2196 static int
2197 arm_comp_type_attributes (type1, type2)
2198      tree type1;
2199      tree type2;
2200 {
2201   int l1, l2, s1, s2;
2202   
2203   /* Check for mismatch of non-default calling convention.  */
2204   if (TREE_CODE (type1) != FUNCTION_TYPE)
2205     return 1;
2206
2207   /* Check for mismatched call attributes.  */
2208   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2209   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2210   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2211   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2212
2213   /* Only bother to check if an attribute is defined.  */
2214   if (l1 | l2 | s1 | s2)
2215     {
2216       /* If one type has an attribute, the other must have the same attribute.  */
2217       if ((l1 != l2) || (s1 != s2))
2218         return 0;
2219
2220       /* Disallow mixed attributes.  */
2221       if ((l1 & s2) || (l2 & s1))
2222         return 0;
2223     }
2224   
2225   /* Check for mismatched ISR attribute.  */
2226   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2227   if (! l1)
2228     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2229   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2230   if (! l2)
2231     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2232   if (l1 != l2)
2233     return 0;
2234
2235   return 1;
2236 }
2237
2238 /*  Encode long_call or short_call attribute by prefixing
2239     symbol name in DECL with a special character FLAG.  */
2240
2241 void
2242 arm_encode_call_attribute (decl, flag)
2243   tree decl;
2244   int flag;
2245 {
2246   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2247   int          len = strlen (str);
2248   char *       newstr;
2249
2250   /* Do not allow weak functions to be treated as short call.  */
2251   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2252     return;
2253
2254   newstr = alloca (len + 2);
2255   newstr[0] = flag;
2256   strcpy (newstr + 1, str);
2257
2258   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2259   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2260 }
2261
2262 /*  Assigns default attributes to newly defined type.  This is used to
2263     set short_call/long_call attributes for function types of
2264     functions defined inside corresponding #pragma scopes.  */
2265
2266 static void
2267 arm_set_default_type_attributes (type)
2268   tree type;
2269 {
2270   /* Add __attribute__ ((long_call)) to all functions, when
2271      inside #pragma long_calls or __attribute__ ((short_call)),
2272      when inside #pragma no_long_calls.  */
2273   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2274     {
2275       tree type_attr_list, attr_name;
2276       type_attr_list = TYPE_ATTRIBUTES (type);
2277
2278       if (arm_pragma_long_calls == LONG)
2279         attr_name = get_identifier ("long_call");
2280       else if (arm_pragma_long_calls == SHORT)
2281         attr_name = get_identifier ("short_call");
2282       else
2283         return;
2284
2285       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2286       TYPE_ATTRIBUTES (type) = type_attr_list;
2287     }
2288 }
2289 \f
2290 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2291    defined within the current compilation unit.  If this cannot be
2292    determined, then 0 is returned.  */
2293
2294 static int
2295 current_file_function_operand (sym_ref)
2296   rtx sym_ref;
2297 {
2298   /* This is a bit of a fib.  A function will have a short call flag
2299      applied to its name if it has the short call attribute, or it has
2300      already been defined within the current compilation unit.  */
2301   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2302     return 1;
2303
2304   /* The current function is always defined within the current compilation
2305      unit.  if it s a weak definition however, then this may not be the real
2306      definition of the function, and so we have to say no.  */
2307   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2308       && !DECL_WEAK (current_function_decl))
2309     return 1;
2310
2311   /* We cannot make the determination - default to returning 0.  */
2312   return 0;
2313 }
2314
2315 /* Return nonzero if a 32 bit "long_call" should be generated for
2316    this call.  We generate a long_call if the function:
2317
2318         a.  has an __attribute__((long call))
2319      or b.  is within the scope of a #pragma long_calls
2320      or c.  the -mlong-calls command line switch has been specified
2321
2322    However we do not generate a long call if the function:
2323    
2324         d.  has an __attribute__ ((short_call))
2325      or e.  is inside the scope of a #pragma no_long_calls
2326      or f.  has an __attribute__ ((section))
2327      or g.  is defined within the current compilation unit.
2328    
2329    This function will be called by C fragments contained in the machine
2330    description file.  CALL_REF and CALL_COOKIE correspond to the matched
2331    rtl operands.  CALL_SYMBOL is used to distinguish between
2332    two different callers of the function.  It is set to 1 in the
2333    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2334    and "call_value" patterns.  This is because of the difference in the
2335    SYM_REFs passed by these patterns.  */
2336
2337 int
2338 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
2339   rtx sym_ref;
2340   int call_cookie;
2341   int call_symbol;
2342 {
2343   if (!call_symbol)
2344     {
2345       if (GET_CODE (sym_ref) != MEM)
2346         return 0;
2347
2348       sym_ref = XEXP (sym_ref, 0);
2349     }
2350
2351   if (GET_CODE (sym_ref) != SYMBOL_REF)
2352     return 0;
2353
2354   if (call_cookie & CALL_SHORT)
2355     return 0;
2356
2357   if (TARGET_LONG_CALLS && flag_function_sections)
2358     return 1;
2359   
2360   if (current_file_function_operand (sym_ref))
2361     return 0;
2362   
2363   return (call_cookie & CALL_LONG)
2364     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2365     || TARGET_LONG_CALLS;
2366 }
2367
2368 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2369
2370 static bool
2371 arm_function_ok_for_sibcall (decl, exp)
2372      tree decl;
2373      tree exp ATTRIBUTE_UNUSED;
2374 {
2375   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2376
2377   /* Never tailcall something for which we have no decl, or if we
2378      are in Thumb mode.  */
2379   if (decl == NULL || TARGET_THUMB)
2380     return false;
2381
2382   /* Get the calling method.  */
2383   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2384     call_type = CALL_SHORT;
2385   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2386     call_type = CALL_LONG;
2387
2388   /* Cannot tail-call to long calls, since these are out of range of
2389      a branch instruction.  However, if not compiling PIC, we know
2390      we can reach the symbol if it is in this compilation unit.  */
2391   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2392     return false;
2393
2394   /* If we are interworking and the function is not declared static
2395      then we can't tail-call it unless we know that it exists in this 
2396      compilation unit (since it might be a Thumb routine).  */
2397   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2398     return false;
2399
2400   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2401   if (IS_INTERRUPT (arm_current_func_type ()))
2402     return false;
2403
2404   /* Everything else is ok.  */
2405   return true;
2406 }
2407
2408 \f
2409 /* Addressing mode support functions.  */
2410
2411 /* Return non-zero if X is a legitimate immediate operand when compiling
2412    for PIC.  */
2413 int
2414 legitimate_pic_operand_p (x)
2415      rtx x;
2416 {
2417   if (CONSTANT_P (x)
2418       && flag_pic
2419       && (GET_CODE (x) == SYMBOL_REF
2420           || (GET_CODE (x) == CONST
2421               && GET_CODE (XEXP (x, 0)) == PLUS
2422               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2423     return 0;
2424
2425   return 1;
2426 }
2427
2428 rtx
2429 legitimize_pic_address (orig, mode, reg)
2430      rtx orig;
2431      enum machine_mode mode;
2432      rtx reg;
2433 {
2434   if (GET_CODE (orig) == SYMBOL_REF
2435       || GET_CODE (orig) == LABEL_REF)
2436     {
2437 #ifndef AOF_ASSEMBLER
2438       rtx pic_ref, address;
2439 #endif
2440       rtx insn;
2441       int subregs = 0;
2442
2443       if (reg == 0)
2444         {
2445           if (no_new_pseudos)
2446             abort ();
2447           else
2448             reg = gen_reg_rtx (Pmode);
2449
2450           subregs = 1;
2451         }
2452
2453 #ifdef AOF_ASSEMBLER
2454       /* The AOF assembler can generate relocations for these directly, and
2455          understands that the PIC register has to be added into the offset.  */
2456       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2457 #else
2458       if (subregs)
2459         address = gen_reg_rtx (Pmode);
2460       else
2461         address = reg;
2462
2463       if (TARGET_ARM)
2464         emit_insn (gen_pic_load_addr_arm (address, orig));
2465       else
2466         emit_insn (gen_pic_load_addr_thumb (address, orig));
2467
2468       if ((GET_CODE (orig) == LABEL_REF
2469            || (GET_CODE (orig) == SYMBOL_REF && 
2470                ENCODED_SHORT_CALL_ATTR_P (XSTR (orig, 0))))
2471           && NEED_GOT_RELOC)
2472         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2473       else
2474         {
2475           pic_ref = gen_rtx_MEM (Pmode,
2476                                  gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2477                                                address));
2478           RTX_UNCHANGING_P (pic_ref) = 1;
2479         }
2480
2481       insn = emit_move_insn (reg, pic_ref);
2482 #endif
2483       current_function_uses_pic_offset_table = 1;
2484       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2485          by loop.  */
2486       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2487                                             REG_NOTES (insn));
2488       return reg;
2489     }
2490   else if (GET_CODE (orig) == CONST)
2491     {
2492       rtx base, offset;
2493
2494       if (GET_CODE (XEXP (orig, 0)) == PLUS
2495           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2496         return orig;
2497
2498       if (reg == 0)
2499         {
2500           if (no_new_pseudos)
2501             abort ();
2502           else
2503             reg = gen_reg_rtx (Pmode);
2504         }
2505
2506       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2507         {
2508           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2509           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2510                                            base == reg ? 0 : reg);
2511         }
2512       else
2513         abort ();
2514
2515       if (GET_CODE (offset) == CONST_INT)
2516         {
2517           /* The base register doesn't really matter, we only want to
2518              test the index for the appropriate mode.  */
2519           if (!arm_legitimate_index_p (mode, offset, 0))
2520             {
2521               if (!no_new_pseudos)
2522                 offset = force_reg (Pmode, offset);
2523               else
2524                 abort ();
2525             }
2526
2527           if (GET_CODE (offset) == CONST_INT)
2528             return plus_constant (base, INTVAL (offset));
2529         }
2530
2531       if (GET_MODE_SIZE (mode) > 4
2532           && (GET_MODE_CLASS (mode) == MODE_INT
2533               || TARGET_SOFT_FLOAT))
2534         {
2535           emit_insn (gen_addsi3 (reg, base, offset));
2536           return reg;
2537         }
2538
2539       return gen_rtx_PLUS (Pmode, base, offset);
2540     }
2541
2542   return orig;
2543 }
2544
2545 /* Generate code to load the PIC register.  PROLOGUE is true if
2546    called from arm_expand_prologue (in which case we want the 
2547    generated insns at the start of the function);  false if called
2548    by an exception receiver that needs the PIC register reloaded
2549    (in which case the insns are just dumped at the current location).  */
2550
2551 void
2552 arm_finalize_pic (prologue)
2553      int prologue ATTRIBUTE_UNUSED;
2554 {
2555 #ifndef AOF_ASSEMBLER
2556   rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2557   rtx global_offset_table;
2558
2559   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2560     return;
2561
2562   if (!flag_pic)
2563     abort ();
2564
2565   start_sequence ();
2566   l1 = gen_label_rtx ();
2567
2568   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2569   /* On the ARM the PC register contains 'dot + 8' at the time of the
2570      addition, on the Thumb it is 'dot + 4'.  */
2571   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2572   if (GOT_PCREL)
2573     pic_tmp2 = gen_rtx_CONST (VOIDmode,
2574                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2575   else
2576     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2577
2578   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2579   
2580   if (TARGET_ARM)
2581     {
2582       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2583       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2584     }
2585   else
2586     {
2587       emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2588       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2589     }
2590
2591   seq = get_insns ();
2592   end_sequence ();
2593   if (prologue)
2594     emit_insn_after (seq, get_insns ());
2595   else
2596     emit_insn (seq);
2597
2598   /* Need to emit this whether or not we obey regdecls,
2599      since setjmp/longjmp can cause life info to screw up.  */
2600   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2601 #endif /* AOF_ASSEMBLER */
2602 }
2603
2604 /* Return nonzero if X is valid as an ARM state addressing register.  */
2605 static int
2606 arm_address_register_rtx_p (x, strict_p)
2607      rtx x;
2608      int strict_p;
2609 {
2610   int regno;
2611
2612   if (GET_CODE (x) != REG)
2613     return 0;
2614
2615   regno = REGNO (x);
2616
2617   if (strict_p)
2618     return ARM_REGNO_OK_FOR_BASE_P (regno);
2619
2620   return (regno <= LAST_ARM_REGNUM
2621           || regno >= FIRST_PSEUDO_REGISTER
2622           || regno == FRAME_POINTER_REGNUM
2623           || regno == ARG_POINTER_REGNUM);
2624 }
2625
2626 /* Return nonzero if X is a valid ARM state address operand.  */
2627 int
2628 arm_legitimate_address_p (mode, x, strict_p)
2629      enum machine_mode mode;
2630      rtx x;
2631      int strict_p;
2632 {
2633   if (arm_address_register_rtx_p (x, strict_p))
2634     return 1;
2635
2636   else if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
2637     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2638
2639   else if ((GET_CODE (x) == POST_MODIFY || GET_CODE (x) == PRE_MODIFY)
2640            && GET_MODE_SIZE (mode) <= 4
2641            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2642            && GET_CODE (XEXP (x, 1)) == PLUS
2643            && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2644     return arm_legitimate_index_p (mode, XEXP (XEXP (x, 1), 1), strict_p);
2645
2646   /* After reload constants split into minipools will have addresses
2647      from a LABEL_REF.  */
2648   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
2649            && (GET_CODE (x) == LABEL_REF
2650                || (GET_CODE (x) == CONST
2651                    && GET_CODE (XEXP (x, 0)) == PLUS
2652                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
2653                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
2654     return 1;
2655
2656   else if (mode == TImode)
2657     return 0;
2658
2659   else if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
2660     {
2661       if (GET_CODE (x) == PLUS
2662           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2663           && GET_CODE (XEXP (x, 1)) == CONST_INT)
2664         {
2665           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
2666
2667           if (val == 4 || val == -4 || val == -8)
2668             return 1;
2669         }
2670     }
2671
2672   else if (GET_CODE (x) == PLUS)
2673     {
2674       rtx xop0 = XEXP (x, 0);
2675       rtx xop1 = XEXP (x, 1);
2676
2677       return ((arm_address_register_rtx_p (xop0, strict_p)
2678                && arm_legitimate_index_p (mode, xop1, strict_p))
2679               || (arm_address_register_rtx_p (xop1, strict_p)
2680                   && arm_legitimate_index_p (mode, xop0, strict_p)));
2681     }
2682
2683 #if 0
2684   /* Reload currently can't handle MINUS, so disable this for now */
2685   else if (GET_CODE (x) == MINUS)
2686     {
2687       rtx xop0 = XEXP (x, 0);
2688       rtx xop1 = XEXP (x, 1);
2689
2690       return (arm_address_register_rtx_p (xop0, strict_p)
2691               && arm_legitimate_index_p (mode, xop1, strict_p));
2692     }
2693 #endif
2694
2695   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
2696            && GET_CODE (x) == SYMBOL_REF
2697            && CONSTANT_POOL_ADDRESS_P (x)
2698            && ! (flag_pic
2699                  && symbol_mentioned_p (get_pool_constant (x))))
2700     return 1;
2701
2702   else if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_DEC)
2703            && (GET_MODE_SIZE (mode) <= 4)
2704            && arm_address_register_rtx_p (XEXP (x, 0), strict_p))
2705     return 1;
2706
2707   return 0;
2708 }
2709
2710 /* Return nonzero if INDEX is valid for an address index operand in
2711    ARM state.  */
2712 static int
2713 arm_legitimate_index_p (mode, index, strict_p)
2714      enum machine_mode mode;
2715      rtx index;
2716      int strict_p;
2717 {
2718   HOST_WIDE_INT range;
2719   enum rtx_code code = GET_CODE (index);
2720
2721   if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
2722     return (code == CONST_INT && INTVAL (index) < 1024
2723             && INTVAL (index) > -1024
2724             && (INTVAL (index) & 3) == 0);
2725
2726   if (TARGET_CIRRUS
2727       && (GET_MODE_CLASS (mode) == MODE_FLOAT || mode == DImode))
2728     return (code == CONST_INT
2729             && INTVAL (index) < 255
2730             && INTVAL (index) > -255);
2731
2732   if (arm_address_register_rtx_p (index, strict_p)
2733       && GET_MODE_SIZE (mode) <= 4)
2734     return 1;
2735
2736   /* XXX What about ldrsb?  */
2737   if (GET_MODE_SIZE (mode) <= 4  && code == MULT
2738       && (!arm_arch4 || (mode) != HImode))
2739     {
2740       rtx xiop0 = XEXP (index, 0);
2741       rtx xiop1 = XEXP (index, 1);
2742
2743       return ((arm_address_register_rtx_p (xiop0, strict_p)
2744                && power_of_two_operand (xiop1, SImode))
2745               || (arm_address_register_rtx_p (xiop1, strict_p)
2746                   && power_of_two_operand (xiop0, SImode)));
2747     }
2748
2749   if (GET_MODE_SIZE (mode) <= 4
2750       && (code == LSHIFTRT || code == ASHIFTRT
2751           || code == ASHIFT || code == ROTATERT)
2752       && (!arm_arch4 || (mode) != HImode))
2753     {
2754       rtx op = XEXP (index, 1);
2755
2756       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
2757               && GET_CODE (op) == CONST_INT
2758               && INTVAL (op) > 0
2759               && INTVAL (op) <= 31);
2760     }
2761
2762   /* XXX For ARM v4 we may be doing a sign-extend operation during the
2763      load, but that has a restricted addressing range and we are unable
2764      to tell here whether that is the case.  To be safe we restrict all
2765      loads to that range.  */
2766   range = ((mode) == HImode || (mode) == QImode)
2767     ? (arm_arch4 ? 256 : 4095) : 4096;
2768
2769   return (code == CONST_INT
2770           && INTVAL (index) < range
2771           && INTVAL (index) > -range);
2772 }
2773
2774 /* Return nonzero if X is valid as an ARM state addressing register.  */
2775 static int
2776 thumb_base_register_rtx_p (x, mode, strict_p)
2777      rtx x;
2778      enum machine_mode mode;
2779      int strict_p;
2780 {
2781   int regno;
2782
2783   if (GET_CODE (x) != REG)
2784     return 0;
2785
2786   regno = REGNO (x);
2787
2788   if (strict_p)
2789     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
2790
2791   return (regno <= LAST_LO_REGNUM
2792           || regno >= FIRST_PSEUDO_REGISTER
2793           || regno == FRAME_POINTER_REGNUM
2794           || (GET_MODE_SIZE (mode) >= 4
2795               && (regno == STACK_POINTER_REGNUM
2796                   || x == hard_frame_pointer_rtx
2797                   || x == arg_pointer_rtx)));
2798 }
2799
2800 /* Return nonzero if x is a legitimate index register.  This is the case
2801    for any base register that can access a QImode object.  */
2802 inline static int
2803 thumb_index_register_rtx_p (x, strict_p)
2804      rtx x;
2805      int strict_p;
2806 {
2807   return thumb_base_register_rtx_p (x, QImode, strict_p);
2808 }
2809
2810 /* Return nonzero if x is a legitimate Thumb-state address.
2811  
2812    The AP may be eliminated to either the SP or the FP, so we use the
2813    least common denominator, e.g. SImode, and offsets from 0 to 64.
2814
2815    ??? Verify whether the above is the right approach.
2816
2817    ??? Also, the FP may be eliminated to the SP, so perhaps that
2818    needs special handling also.
2819
2820    ??? Look at how the mips16 port solves this problem.  It probably uses
2821    better ways to solve some of these problems.
2822
2823    Although it is not incorrect, we don't accept QImode and HImode
2824    addresses based on the frame pointer or arg pointer until the
2825    reload pass starts.  This is so that eliminating such addresses
2826    into stack based ones won't produce impossible code.  */
2827 int
2828 thumb_legitimate_address_p (mode, x, strict_p)
2829      enum machine_mode mode;
2830      rtx x;
2831      int strict_p;
2832 {
2833   /* ??? Not clear if this is right.  Experiment.  */
2834   if (GET_MODE_SIZE (mode) < 4
2835       && !(reload_in_progress || reload_completed)
2836       && (reg_mentioned_p (frame_pointer_rtx, x)
2837           || reg_mentioned_p (arg_pointer_rtx, x)
2838           || reg_mentioned_p (virtual_incoming_args_rtx, x)
2839           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
2840           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
2841           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
2842     return 0;
2843
2844   /* Accept any base register.  SP only in SImode or larger.  */
2845   else if (thumb_base_register_rtx_p (x, mode, strict_p))
2846     return 1;
2847
2848   /* This is PC relative data before MACHINE_DEPENDENT_REORG runs.  */
2849   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
2850            && GET_CODE (x) == SYMBOL_REF
2851            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
2852     return 1;
2853
2854   /* This is PC relative data after MACHINE_DEPENDENT_REORG runs.  */
2855   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
2856            && (GET_CODE (x) == LABEL_REF
2857                || (GET_CODE (x) == CONST
2858                    && GET_CODE (XEXP (x, 0)) == PLUS
2859                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
2860                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
2861     return 1;
2862
2863   /* Post-inc indexing only supported for SImode and larger.  */
2864   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
2865            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
2866     return 1;
2867
2868   else if (GET_CODE (x) == PLUS)
2869     {
2870       /* REG+REG address can be any two index registers.  */
2871       /* We disallow FRAME+REG addressing since we know that FRAME
2872          will be replaced with STACK, and SP relative addressing only
2873          permits SP+OFFSET.  */
2874       if (GET_MODE_SIZE (mode) <= 4
2875           && XEXP (x, 0) != frame_pointer_rtx
2876           && XEXP (x, 1) != frame_pointer_rtx
2877           && XEXP (x, 0) != virtual_stack_vars_rtx
2878           && XEXP (x, 1) != virtual_stack_vars_rtx
2879           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
2880           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
2881         return 1;
2882
2883       /* REG+const has 5-7 bit offset for non-SP registers.  */
2884       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
2885                 || XEXP (x, 0) == arg_pointer_rtx)
2886                && GET_CODE (XEXP (x, 1)) == CONST_INT
2887                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
2888         return 1;
2889
2890       /* REG+const has 10 bit offset for SP, but only SImode and
2891          larger is supported.  */
2892       /* ??? Should probably check for DI/DFmode overflow here
2893          just like GO_IF_LEGITIMATE_OFFSET does.  */
2894       else if (GET_CODE (XEXP (x, 0)) == REG
2895                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
2896                && GET_MODE_SIZE (mode) >= 4
2897                && GET_CODE (XEXP (x, 1)) == CONST_INT
2898                && INTVAL (XEXP (x, 1)) >= 0
2899                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
2900                && (INTVAL (XEXP (x, 1)) & 3) == 0)
2901         return 1;
2902
2903       else if (GET_CODE (XEXP (x, 0)) == REG
2904                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
2905                && GET_MODE_SIZE (mode) >= 4
2906                && GET_CODE (XEXP (x, 1)) == CONST_INT
2907                && (INTVAL (XEXP (x, 1)) & 3) == 0)
2908         return 1;
2909     }
2910
2911   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
2912            && GET_CODE (x) == SYMBOL_REF
2913            && CONSTANT_POOL_ADDRESS_P (x)
2914            && !(flag_pic
2915                 && symbol_mentioned_p (get_pool_constant (x))))
2916     return 1;
2917
2918   return 0;
2919 }
2920
2921 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
2922    instruction of mode MODE.  */
2923 int
2924 thumb_legitimate_offset_p (mode, val)
2925      enum machine_mode mode;
2926      HOST_WIDE_INT val;
2927 {
2928   switch (GET_MODE_SIZE (mode))
2929     {
2930     case 1:
2931       return val >= 0 && val < 32;
2932
2933     case 2:
2934       return val >= 0 && val < 64 && (val & 1) == 0;
2935
2936     default:
2937       return (val >= 0
2938               && (val + GET_MODE_SIZE (mode)) <= 128
2939               && (val & 3) == 0);
2940     }
2941 }
2942
2943 /* Try machine-dependent ways of modifying an illegitimate address
2944    to be legitimate.  If we find one, return the new, valid address.  */
2945
2946 rtx
2947 arm_legitimize_address (x, orig_x, mode)
2948      rtx x;
2949      rtx orig_x;
2950      enum machine_mode mode;
2951 {
2952   if (GET_CODE (x) == PLUS)
2953     {
2954       rtx xop0 = XEXP (x, 0);
2955       rtx xop1 = XEXP (x, 1);
2956
2957       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
2958         xop0 = force_reg (SImode, xop0);
2959
2960       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
2961         xop1 = force_reg (SImode, xop1);
2962
2963       if (ARM_BASE_REGISTER_RTX_P (xop0)
2964           && GET_CODE (xop1) == CONST_INT)
2965         {
2966           HOST_WIDE_INT n, low_n;
2967           rtx base_reg, val;
2968           n = INTVAL (xop1);
2969
2970           if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
2971             {
2972               low_n = n & 0x0f;
2973               n &= ~0x0f;
2974               if (low_n > 4)
2975                 {
2976                   n += 16;
2977                   low_n -= 16;
2978                 }
2979             }
2980           else
2981             {
2982               low_n = ((mode) == TImode ? 0
2983                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
2984               n -= low_n;
2985             }
2986
2987           base_reg = gen_reg_rtx (SImode);
2988           val = force_operand (gen_rtx_PLUS (SImode, xop0,
2989                                              GEN_INT (n)), NULL_RTX);
2990           emit_move_insn (base_reg, val);
2991           x = (low_n == 0 ? base_reg
2992                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
2993         }
2994       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
2995         x = gen_rtx_PLUS (SImode, xop0, xop1);
2996     }
2997
2998   /* XXX We don't allow MINUS any more -- see comment in
2999      arm_legitimate_address_p ().  */
3000   else if (GET_CODE (x) == MINUS)
3001     {
3002       rtx xop0 = XEXP (x, 0);
3003       rtx xop1 = XEXP (x, 1);
3004
3005       if (CONSTANT_P (xop0))
3006         xop0 = force_reg (SImode, xop0);
3007
3008       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3009         xop1 = force_reg (SImode, xop1);
3010
3011       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3012         x = gen_rtx_MINUS (SImode, xop0, xop1);
3013     }
3014
3015   if (flag_pic)
3016     {
3017       /* We need to find and carefully transform any SYMBOL and LABEL
3018          references; so go back to the original address expression.  */
3019       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3020
3021       if (new_x != orig_x)
3022         x = new_x;
3023     }
3024
3025   return x;
3026 }
3027
3028 \f
3029
3030 #define REG_OR_SUBREG_REG(X)                                            \
3031   (GET_CODE (X) == REG                                                  \
3032    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3033
3034 #define REG_OR_SUBREG_RTX(X)                    \
3035    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3036
3037 #ifndef COSTS_N_INSNS
3038 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3039 #endif
3040
3041 static inline int
3042 arm_rtx_costs_1 (x, code, outer)
3043      rtx x;
3044      enum rtx_code code;
3045      enum rtx_code outer;
3046 {
3047   enum machine_mode mode = GET_MODE (x);
3048   enum rtx_code subcode;
3049   int extra_cost;
3050
3051   if (TARGET_THUMB)
3052     {
3053       switch (code)
3054         {
3055         case ASHIFT:
3056         case ASHIFTRT:
3057         case LSHIFTRT:
3058         case ROTATERT:  
3059         case PLUS:
3060         case MINUS:
3061         case COMPARE:
3062         case NEG:
3063         case NOT:       
3064           return COSTS_N_INSNS (1);
3065           
3066         case MULT:                                                      
3067           if (GET_CODE (XEXP (x, 1)) == CONST_INT)                      
3068             {                                                           
3069               int cycles = 0;                                           
3070               unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3071               
3072               while (i)                                         
3073                 {                                                       
3074                   i >>= 2;                                              
3075                   cycles++;                                             
3076                 }                                                       
3077               return COSTS_N_INSNS (2) + cycles;                        
3078             }
3079           return COSTS_N_INSNS (1) + 16;
3080           
3081         case SET:                                                       
3082           return (COSTS_N_INSNS (1)                                     
3083                   + 4 * ((GET_CODE (SET_SRC (x)) == MEM)                
3084                          + GET_CODE (SET_DEST (x)) == MEM));
3085           
3086         case CONST_INT:                                         
3087           if (outer == SET)                                             
3088             {                                                   
3089               if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)            
3090                 return 0;                                               
3091               if (thumb_shiftable_const (INTVAL (x)))                   
3092                 return COSTS_N_INSNS (2);                               
3093               return COSTS_N_INSNS (3);                         
3094             }                                                           
3095           else if (outer == PLUS                                        
3096                    && INTVAL (x) < 256 && INTVAL (x) > -256)            
3097             return 0;                                                   
3098           else if (outer == COMPARE                                     
3099                    && (unsigned HOST_WIDE_INT) INTVAL (x) < 256)        
3100             return 0;                                                   
3101           else if (outer == ASHIFT || outer == ASHIFTRT         
3102                    || outer == LSHIFTRT)                                
3103             return 0;                                                   
3104           return COSTS_N_INSNS (2);
3105           
3106         case CONST:                                                     
3107         case CONST_DOUBLE:                                              
3108         case LABEL_REF:                                         
3109         case SYMBOL_REF:                                                
3110           return COSTS_N_INSNS (3);
3111           
3112         case UDIV:
3113         case UMOD:
3114         case DIV:
3115         case MOD:
3116           return 100;
3117
3118         case TRUNCATE:
3119           return 99;
3120
3121         case AND:
3122         case XOR:
3123         case IOR: 
3124           /* XXX guess. */
3125           return 8;
3126
3127         case ADDRESSOF:
3128         case MEM:
3129           /* XXX another guess.  */
3130           /* Memory costs quite a lot for the first word, but subsequent words
3131              load at the equivalent of a single insn each.  */
3132           return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3133                   + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3134                      ? 4 : 0));
3135
3136         case IF_THEN_ELSE:
3137           /* XXX a guess. */
3138           if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3139             return 14;
3140           return 2;
3141
3142         case ZERO_EXTEND:
3143           /* XXX still guessing.  */
3144           switch (GET_MODE (XEXP (x, 0)))
3145             {
3146             case QImode:
3147               return (1 + (mode == DImode ? 4 : 0)
3148                       + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3149               
3150             case HImode:
3151               return (4 + (mode == DImode ? 4 : 0)
3152                       + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3153               
3154             case SImode:
3155               return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3156           
3157             default:
3158               return 99;
3159             }
3160           
3161         default:
3162           return 99;
3163 #if 0     
3164         case FFS:
3165         case FLOAT:
3166         case FIX:
3167         case UNSIGNED_FIX:
3168           /* XXX guess */
3169           fprintf (stderr, "unexpected code for thumb in rtx_costs: %s\n",
3170                    rtx_name[code]);
3171           abort ();
3172 #endif
3173         }
3174     }
3175   
3176   switch (code)
3177     {
3178     case MEM:
3179       /* Memory costs quite a lot for the first word, but subsequent words
3180          load at the equivalent of a single insn each.  */
3181       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3182               + (GET_CODE (x) == SYMBOL_REF
3183                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3184
3185     case DIV:
3186     case MOD:
3187       return 100;
3188
3189     case ROTATE:
3190       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3191         return 4;
3192       /* Fall through */
3193     case ROTATERT:
3194       if (mode != SImode)
3195         return 8;
3196       /* Fall through */
3197     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3198       if (mode == DImode)
3199         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3200                 + ((GET_CODE (XEXP (x, 0)) == REG 
3201                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3202                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3203                    ? 0 : 8));
3204       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3205                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3206                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3207                    ? 0 : 4)
3208               + ((GET_CODE (XEXP (x, 1)) == REG
3209                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3210                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3211                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3212                  ? 0 : 4));
3213
3214     case MINUS:
3215       if (mode == DImode)
3216         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3217                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3218                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3219                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3220                    ? 0 : 8));
3221
3222       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3223         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3224                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3225                           && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
3226                      ? 0 : 8)
3227                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3228                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3229                         && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
3230                    ? 0 : 8));
3231
3232       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3233             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3234             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3235           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3236                || subcode == ASHIFTRT || subcode == LSHIFTRT
3237                || subcode == ROTATE || subcode == ROTATERT
3238                || (subcode == MULT
3239                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3240                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3241                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3242               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3243               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3244                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3245               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3246         return 1;
3247       /* Fall through */
3248
3249     case PLUS: 
3250       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3251         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3252                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3253                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3254                         && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
3255                    ? 0 : 8));
3256
3257       /* Fall through */
3258     case AND: case XOR: case IOR: 
3259       extra_cost = 0;
3260
3261       /* Normally the frame registers will be spilt into reg+const during
3262          reload, so it is a bad idea to combine them with other instructions,
3263          since then they might not be moved outside of loops.  As a compromise
3264          we allow integration with ops that have a constant as their second
3265          operand.  */
3266       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3267            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3268            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3269           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3270               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3271         extra_cost = 4;
3272
3273       if (mode == DImode)
3274         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3275                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3276                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3277                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3278                    ? 0 : 8));
3279
3280       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3281         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3282                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3283                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3284                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3285                    ? 0 : 4));
3286
3287       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3288         return (1 + extra_cost
3289                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3290                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3291                      || subcode == ROTATE || subcode == ROTATERT
3292                      || (subcode == MULT
3293                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3294                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3295                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3296                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3297                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3298                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3299                    ? 0 : 4));
3300
3301       return 8;
3302
3303     case MULT:
3304       /* There is no point basing this on the tuning, since it is always the
3305          fast variant if it exists at all.  */
3306       if (arm_fast_multiply && mode == DImode
3307           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3308           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3309               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3310         return 8;
3311
3312       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3313           || mode == DImode)
3314         return 30;
3315
3316       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3317         {
3318           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3319                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3320           int add_cost = const_ok_for_arm (i) ? 4 : 8;
3321           int j;
3322           
3323           /* Tune as appropriate.  */ 
3324           int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
3325           
3326           for (j = 0; i && j < 32; j += booth_unit_size)
3327             {
3328               i >>= booth_unit_size;
3329               add_cost += 2;
3330             }
3331
3332           return add_cost;
3333         }
3334
3335       return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
3336               + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3337               + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
3338
3339     case TRUNCATE:
3340       if (arm_fast_multiply && mode == SImode
3341           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3342           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3343           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3344               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3345           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3346               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3347         return 8;
3348       return 99;
3349
3350     case NEG:
3351       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3352         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3353       /* Fall through */
3354     case NOT:
3355       if (mode == DImode)
3356         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3357
3358       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3359
3360     case IF_THEN_ELSE:
3361       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3362         return 14;
3363       return 2;
3364
3365     case COMPARE:
3366       return 1;
3367
3368     case ABS:
3369       return 4 + (mode == DImode ? 4 : 0);
3370
3371     case SIGN_EXTEND:
3372       if (GET_MODE (XEXP (x, 0)) == QImode)
3373         return (4 + (mode == DImode ? 4 : 0)
3374                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3375       /* Fall through */
3376     case ZERO_EXTEND:
3377       switch (GET_MODE (XEXP (x, 0)))
3378         {
3379         case QImode:
3380           return (1 + (mode == DImode ? 4 : 0)
3381                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3382
3383         case HImode:
3384           return (4 + (mode == DImode ? 4 : 0)
3385                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3386
3387         case SImode:
3388           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3389
3390         default:
3391           break;
3392         }
3393       abort ();
3394
3395     case CONST_INT:                                             
3396       if (const_ok_for_arm (INTVAL (x)))                        
3397         return outer == SET ? 2 : -1;                   
3398       else if (outer == AND                             
3399                && const_ok_for_arm (~INTVAL (x)))               
3400         return -1;                                              
3401       else if ((outer == COMPARE                        
3402                 || outer == PLUS || outer == MINUS)     
3403                && const_ok_for_arm (-INTVAL (x)))               
3404         return -1;                                              
3405       else                                                      
3406         return 5;
3407       
3408     case CONST:                                                         
3409     case LABEL_REF:                                             
3410     case SYMBOL_REF:                                            
3411       return 6;
3412       
3413     case CONST_DOUBLE:                                          
3414       if (const_double_rtx_ok_for_fpu (x))                      
3415         return outer == SET ? 2 : -1;                   
3416       else if ((outer == COMPARE || outer == PLUS)      
3417                && neg_const_double_rtx_ok_for_fpu (x))          
3418         return -1;                                              
3419       return 7;
3420       
3421     default:
3422       return 99;
3423     }
3424 }
3425
3426 static bool
3427 arm_rtx_costs (x, code, outer_code, total)
3428      rtx x;
3429      int code, outer_code;
3430      int *total;
3431 {
3432   *total = arm_rtx_costs_1 (x, code, outer_code);
3433   return true;
3434 }
3435
3436 /* All address computations that can be done are free, but rtx cost returns
3437    the same for practically all of them.  So we weight the different types
3438    of address here in the order (most pref first):
3439    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
3440
3441 static int
3442 arm_address_cost (X)
3443     rtx X;
3444 {
3445 #define ARM_ADDRESS_COST(X)                                                  \
3446   (10 - ((GET_CODE (X) == MEM || GET_CODE (X) == LABEL_REF                   \
3447           || GET_CODE (X) == SYMBOL_REF)                                     \
3448          ? 0                                                                 \
3449          : ((GET_CODE (X) == PRE_INC || GET_CODE (X) == PRE_DEC              \
3450              || GET_CODE (X) == POST_INC || GET_CODE (X) == POST_DEC)        \
3451             ? 10                                                             \
3452             : (((GET_CODE (X) == PLUS || GET_CODE (X) == MINUS)              \
3453                 ? 6 + (GET_CODE (XEXP (X, 1)) == CONST_INT ? 2               \
3454                        : ((GET_RTX_CLASS (GET_CODE (XEXP (X, 0))) == '2'     \
3455                            || GET_RTX_CLASS (GET_CODE (XEXP (X, 0))) == 'c'  \
3456                            || GET_RTX_CLASS (GET_CODE (XEXP (X, 1))) == '2'  \
3457                            || GET_RTX_CLASS (GET_CODE (XEXP (X, 1))) == 'c') \
3458                           ? 1 : 0))                                          \
3459                 : 4)))))
3460          
3461 #define THUMB_ADDRESS_COST(X)                                   \
3462   ((GET_CODE (X) == REG                                         \
3463     || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 0)) == REG   \
3464         && GET_CODE (XEXP (X, 1)) == CONST_INT))                \
3465    ? 1 : 2)
3466      
3467   return (TARGET_ARM ? ARM_ADDRESS_COST (X) : THUMB_ADDRESS_COST (X));
3468 }
3469
3470 static int
3471 arm_adjust_cost (insn, link, dep, cost)
3472      rtx insn;
3473      rtx link;
3474      rtx dep;
3475      int cost;
3476 {
3477   rtx i_pat, d_pat;
3478
3479   /* Some true dependencies can have a higher cost depending
3480      on precisely how certain input operands are used.  */
3481   if (arm_is_xscale
3482       && REG_NOTE_KIND (link) == 0
3483       && recog_memoized (insn) < 0
3484       && recog_memoized (dep) < 0)
3485     {
3486       int shift_opnum = get_attr_shift (insn);
3487       enum attr_type attr_type = get_attr_type (dep);
3488
3489       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
3490          operand for INSN.  If we have a shifted input operand and the
3491          instruction we depend on is another ALU instruction, then we may
3492          have to account for an additional stall.  */
3493       if (shift_opnum != 0 && attr_type == TYPE_NORMAL)
3494         {
3495           rtx shifted_operand;
3496           int opno;
3497           
3498           /* Get the shifted operand.  */
3499           extract_insn (insn);
3500           shifted_operand = recog_data.operand[shift_opnum];
3501
3502           /* Iterate over all the operands in DEP.  If we write an operand
3503              that overlaps with SHIFTED_OPERAND, then we have increase the
3504              cost of this dependency.  */
3505           extract_insn (dep);
3506           preprocess_constraints ();
3507           for (opno = 0; opno < recog_data.n_operands; opno++)
3508             {
3509               /* We can ignore strict inputs.  */
3510               if (recog_data.operand_type[opno] == OP_IN)
3511                 continue;
3512
3513               if (reg_overlap_mentioned_p (recog_data.operand[opno],
3514                                            shifted_operand))
3515                 return 2;
3516             }
3517         }
3518     }
3519
3520   /* XXX This is not strictly true for the FPA.  */
3521   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
3522       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
3523     return 0;
3524
3525   /* Call insns don't incur a stall, even if they follow a load.  */
3526   if (REG_NOTE_KIND (link) == 0
3527       && GET_CODE (insn) == CALL_INSN)
3528     return 1;
3529
3530   if ((i_pat = single_set (insn)) != NULL
3531       && GET_CODE (SET_SRC (i_pat)) == MEM
3532       && (d_pat = single_set (dep)) != NULL
3533       && GET_CODE (SET_DEST (d_pat)) == MEM)
3534     {
3535       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
3536       /* This is a load after a store, there is no conflict if the load reads
3537          from a cached area.  Assume that loads from the stack, and from the
3538          constant pool are cached, and that others will miss.  This is a 
3539          hack.  */
3540       
3541       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
3542           || reg_mentioned_p (stack_pointer_rtx, src_mem)
3543           || reg_mentioned_p (frame_pointer_rtx, src_mem)
3544           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
3545         return 1;
3546     }
3547
3548   return cost;
3549 }
3550
3551 /* This code has been fixed for cross compilation.  */
3552
3553 static int fpa_consts_inited = 0;
3554
3555 static const char * const strings_fpa[8] =
3556 {
3557   "0",   "1",   "2",   "3",
3558   "4",   "5",   "0.5", "10"
3559 };
3560
3561 static REAL_VALUE_TYPE values_fpa[8];
3562
3563 static void
3564 init_fpa_table ()
3565 {
3566   int i;
3567   REAL_VALUE_TYPE r;
3568
3569   for (i = 0; i < 8; i++)
3570     {
3571       r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
3572       values_fpa[i] = r;
3573     }
3574
3575   fpa_consts_inited = 1;
3576 }
3577
3578 /* Return TRUE if rtx X is a valid immediate FPU constant.  */
3579
3580 int
3581 const_double_rtx_ok_for_fpu (x)
3582      rtx x;
3583 {
3584   REAL_VALUE_TYPE r;
3585   int i;
3586   
3587   if (!fpa_consts_inited)
3588     init_fpa_table ();
3589   
3590   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3591   if (REAL_VALUE_MINUS_ZERO (r))
3592     return 0;
3593
3594   for (i = 0; i < 8; i++)
3595     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
3596       return 1;
3597
3598   return 0;
3599 }
3600
3601 /* Return TRUE if rtx X is a valid immediate FPU constant.  */
3602
3603 int
3604 neg_const_double_rtx_ok_for_fpu (x)
3605      rtx x;
3606 {
3607   REAL_VALUE_TYPE r;
3608   int i;
3609   
3610   if (!fpa_consts_inited)
3611     init_fpa_table ();
3612   
3613   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3614   r = REAL_VALUE_NEGATE (r);
3615   if (REAL_VALUE_MINUS_ZERO (r))
3616     return 0;
3617
3618   for (i = 0; i < 8; i++)
3619     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
3620       return 1;
3621
3622   return 0;
3623 }
3624 \f
3625 /* Predicates for `match_operand' and `match_operator'.  */
3626
3627 /* s_register_operand is the same as register_operand, but it doesn't accept
3628    (SUBREG (MEM)...).
3629
3630    This function exists because at the time it was put in it led to better
3631    code.  SUBREG(MEM) always needs a reload in the places where
3632    s_register_operand is used, and this seemed to lead to excessive
3633    reloading.  */
3634
3635 int
3636 s_register_operand (op, mode)
3637      rtx op;
3638      enum machine_mode mode;
3639 {
3640   if (GET_MODE (op) != mode && mode != VOIDmode)
3641     return 0;
3642
3643   if (GET_CODE (op) == SUBREG)
3644     op = SUBREG_REG (op);
3645
3646   /* We don't consider registers whose class is NO_REGS
3647      to be a register operand.  */
3648   /* XXX might have to check for lo regs only for thumb ??? */
3649   return (GET_CODE (op) == REG
3650           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3651               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3652 }
3653
3654 /* A hard register operand (even before reload.  */
3655
3656 int
3657 arm_hard_register_operand (op, mode)
3658      rtx op;
3659      enum machine_mode mode;
3660 {
3661   if (GET_MODE (op) != mode && mode != VOIDmode)
3662     return 0;
3663
3664   return (GET_CODE (op) == REG
3665           && REGNO (op) < FIRST_PSEUDO_REGISTER);
3666 }
3667     
3668 /* Only accept reg, subreg(reg), const_int.  */
3669
3670 int
3671 reg_or_int_operand (op, mode)
3672      rtx op;
3673      enum machine_mode mode;
3674 {
3675   if (GET_CODE (op) == CONST_INT)
3676     return 1;
3677
3678   if (GET_MODE (op) != mode && mode != VOIDmode)
3679     return 0;
3680
3681   if (GET_CODE (op) == SUBREG)
3682     op = SUBREG_REG (op);
3683
3684   /* We don't consider registers whose class is NO_REGS
3685      to be a register operand.  */
3686   return (GET_CODE (op) == REG
3687           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3688               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
3689 }
3690
3691 /* Return 1 if OP is an item in memory, given that we are in reload.  */
3692
3693 int
3694 arm_reload_memory_operand (op, mode)
3695      rtx op;
3696      enum machine_mode mode ATTRIBUTE_UNUSED;
3697 {
3698   int regno = true_regnum (op);
3699
3700   return (!CONSTANT_P (op)
3701           && (regno == -1
3702               || (GET_CODE (op) == REG
3703                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
3704 }
3705
3706 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
3707    memory access (architecture V4).
3708    MODE is QImode if called when computing constraints, or VOIDmode when
3709    emitting patterns.  In this latter case we cannot use memory_operand()
3710    because it will fail on badly formed MEMs, which is precisely what we are
3711    trying to catch.  */
3712
3713 int
3714 bad_signed_byte_operand (op, mode)
3715      rtx op;
3716      enum machine_mode mode ATTRIBUTE_UNUSED;
3717 {
3718 #if 0
3719   if ((mode == QImode && !memory_operand (op, mode)) || GET_CODE (op) != MEM)
3720     return 0;
3721 #endif
3722   if (GET_CODE (op) != MEM)
3723     return 0;
3724
3725   op = XEXP (op, 0);
3726
3727   /* A sum of anything more complex than reg + reg or reg + const is bad.  */
3728   if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3729       && (!s_register_operand (XEXP (op, 0), VOIDmode)
3730           || (!s_register_operand (XEXP (op, 1), VOIDmode)
3731               && GET_CODE (XEXP (op, 1)) != CONST_INT)))
3732     return 1;
3733
3734   /* Big constants are also bad.  */
3735   if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
3736       && (INTVAL (XEXP (op, 1)) > 0xff
3737           || -INTVAL (XEXP (op, 1)) > 0xff))
3738     return 1;
3739
3740   /* Everything else is good, or can will automatically be made so.  */
3741   return 0;
3742 }
3743
3744 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
3745
3746 int
3747 arm_rhs_operand (op, mode)
3748      rtx op;
3749      enum machine_mode mode;
3750 {
3751   return (s_register_operand (op, mode)
3752           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
3753 }
3754
3755 /* Return TRUE for valid operands for the
3756    rhs of an ARM instruction, or a load.  */
3757
3758 int
3759 arm_rhsm_operand (op, mode)
3760      rtx op;
3761      enum machine_mode mode;
3762 {
3763   return (s_register_operand (op, mode)
3764           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
3765           || memory_operand (op, mode));
3766 }
3767
3768 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
3769    constant that is valid when negated.  */
3770
3771 int
3772 arm_add_operand (op, mode)
3773      rtx op;
3774      enum machine_mode mode;
3775 {
3776   if (TARGET_THUMB)
3777     return thumb_cmp_operand (op, mode);
3778   
3779   return (s_register_operand (op, mode)
3780           || (GET_CODE (op) == CONST_INT
3781               && (const_ok_for_arm (INTVAL (op))
3782                   || const_ok_for_arm (-INTVAL (op)))));
3783 }
3784
3785 int
3786 arm_not_operand (op, mode)
3787      rtx op;
3788      enum machine_mode mode;
3789 {
3790   return (s_register_operand (op, mode)
3791           || (GET_CODE (op) == CONST_INT
3792               && (const_ok_for_arm (INTVAL (op))
3793                   || const_ok_for_arm (~INTVAL (op)))));
3794 }
3795
3796 /* Return TRUE if the operand is a memory reference which contains an
3797    offsettable address.  */
3798
3799 int
3800 offsettable_memory_operand (op, mode)
3801      rtx op;
3802      enum machine_mode mode;
3803 {
3804   if (mode == VOIDmode)
3805     mode = GET_MODE (op);
3806
3807   return (mode == GET_MODE (op)
3808           && GET_CODE (op) == MEM
3809           && offsettable_address_p (reload_completed | reload_in_progress,
3810                                     mode, XEXP (op, 0)));
3811 }
3812
3813 /* Return TRUE if the operand is a memory reference which is, or can be
3814    made word aligned by adjusting the offset.  */
3815
3816 int
3817 alignable_memory_operand (op, mode)
3818      rtx op;
3819      enum machine_mode mode;
3820 {
3821   rtx reg;
3822
3823   if (mode == VOIDmode)
3824     mode = GET_MODE (op);
3825
3826   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
3827     return 0;
3828
3829   op = XEXP (op, 0);
3830
3831   return ((GET_CODE (reg = op) == REG
3832            || (GET_CODE (op) == SUBREG
3833                && GET_CODE (reg = SUBREG_REG (op)) == REG)
3834            || (GET_CODE (op) == PLUS
3835                && GET_CODE (XEXP (op, 1)) == CONST_INT
3836                && (GET_CODE (reg = XEXP (op, 0)) == REG
3837                    || (GET_CODE (XEXP (op, 0)) == SUBREG
3838                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
3839           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
3840 }
3841
3842 /* Similar to s_register_operand, but does not allow hard integer 
3843    registers.  */
3844
3845 int
3846 f_register_operand (op, mode)
3847      rtx op;
3848      enum machine_mode mode;
3849 {
3850   if (GET_MODE (op) != mode && mode != VOIDmode)
3851     return 0;
3852
3853   if (GET_CODE (op) == SUBREG)
3854     op = SUBREG_REG (op);
3855
3856   /* We don't consider registers whose class is NO_REGS
3857      to be a register operand.  */
3858   return (GET_CODE (op) == REG
3859           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3860               || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
3861 }
3862
3863 /* Return TRUE for valid operands for the rhs of an FPU instruction.  */
3864
3865 int
3866 fpu_rhs_operand (op, mode)
3867      rtx op;
3868      enum machine_mode mode;
3869 {
3870   if (s_register_operand (op, mode))
3871     return TRUE;
3872
3873   if (GET_MODE (op) != mode && mode != VOIDmode)
3874     return FALSE;
3875
3876   if (GET_CODE (op) == CONST_DOUBLE)
3877     return const_double_rtx_ok_for_fpu (op);
3878
3879   return FALSE;
3880 }
3881
3882 int
3883 fpu_add_operand (op, mode)
3884      rtx op;
3885      enum machine_mode mode;
3886 {
3887   if (s_register_operand (op, mode))
3888     return TRUE;
3889
3890   if (GET_MODE (op) != mode && mode != VOIDmode)
3891     return FALSE;
3892
3893   if (GET_CODE (op) == CONST_DOUBLE)
3894     return (const_double_rtx_ok_for_fpu (op) 
3895             || neg_const_double_rtx_ok_for_fpu (op));
3896
3897   return FALSE;
3898 }
3899
3900 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
3901
3902 int
3903 cirrus_memory_offset (op)
3904      rtx op;
3905 {
3906   /* Reject eliminable registers.  */
3907   if (! (reload_in_progress || reload_completed)
3908       && (   reg_mentioned_p (frame_pointer_rtx, op)
3909           || reg_mentioned_p (arg_pointer_rtx, op)
3910           || reg_mentioned_p (virtual_incoming_args_rtx, op)
3911           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
3912           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
3913           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
3914     return 0;
3915
3916   if (GET_CODE (op) == MEM)
3917     {
3918       rtx ind;
3919
3920       ind = XEXP (op, 0);
3921
3922       /* Match: (mem (reg)).  */
3923       if (GET_CODE (ind) == REG)
3924         return 1;
3925
3926       /* Match:
3927          (mem (plus (reg)
3928                     (const))).  */
3929       if (GET_CODE (ind) == PLUS
3930           && GET_CODE (XEXP (ind, 0)) == REG
3931           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
3932           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
3933         return 1;
3934     }
3935
3936   return 0;
3937 }
3938
3939 /* Return nonzero if OP is a Cirrus or general register.  */
3940
3941 int
3942 cirrus_register_operand (op, mode)
3943      rtx op;
3944      enum machine_mode mode;
3945 {
3946   if (GET_MODE (op) != mode && mode != VOIDmode)
3947     return FALSE;
3948
3949   if (GET_CODE (op) == SUBREG)
3950     op = SUBREG_REG (op);
3951
3952   return (GET_CODE (op) == REG
3953           && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
3954               || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
3955 }
3956
3957 /* Return nonzero if OP is a cirrus FP register.  */
3958
3959 int
3960 cirrus_fp_register (op, mode)
3961      rtx op;
3962      enum machine_mode mode;
3963 {
3964   if (GET_MODE (op) != mode && mode != VOIDmode)
3965     return FALSE;
3966
3967   if (GET_CODE (op) == SUBREG)
3968     op = SUBREG_REG (op);
3969
3970   return (GET_CODE (op) == REG
3971           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
3972               || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
3973 }
3974
3975 /* Return nonzero if OP is a 6bit constant (0..63).  */
3976
3977 int
3978 cirrus_shift_const (op, mode)
3979      rtx op;
3980      enum machine_mode mode ATTRIBUTE_UNUSED;
3981 {
3982   return (GET_CODE (op) == CONST_INT
3983           && INTVAL (op) >= 0
3984           && INTVAL (op) < 64);
3985 }
3986
3987 /* Return nonzero if INSN is an LDR R0,ADDR instruction.  */
3988
3989 static int
3990 is_load_address (insn)
3991      rtx insn;
3992 {
3993   rtx body, lhs, rhs;;
3994
3995   if (!insn)
3996     return 0;
3997
3998   if (GET_CODE (insn) != INSN)
3999     return 0;
4000
4001   body = PATTERN (insn);
4002
4003   if (GET_CODE (body) != SET)
4004     return 0;
4005
4006   lhs = XEXP (body, 0);
4007   rhs = XEXP (body, 1);
4008
4009   return (GET_CODE (lhs) == REG
4010           && REGNO_REG_CLASS (REGNO (lhs)) == GENERAL_REGS
4011           && (GET_CODE (rhs) == MEM
4012               || GET_CODE (rhs) == SYMBOL_REF));
4013 }
4014
4015 /* Return nonzero if INSN is a Cirrus instruction.  */
4016
4017 static int
4018 is_cirrus_insn (insn)
4019      rtx insn;
4020 {
4021   enum attr_cirrus attr;
4022
4023   /* get_attr aborts on USE and CLOBBER.  */
4024   if (!insn
4025       || GET_CODE (insn) != INSN
4026       || GET_CODE (PATTERN (insn)) == USE
4027       || GET_CODE (PATTERN (insn)) == CLOBBER)
4028     return 0;
4029
4030   attr = get_attr_cirrus (insn);
4031
4032   return attr != CIRRUS_NO;
4033 }
4034
4035 /* Cirrus reorg for invalid instruction combinations.  */
4036
4037 static void
4038 cirrus_reorg (first)
4039      rtx first;
4040 {
4041   enum attr_cirrus attr;
4042   rtx body = PATTERN (first);
4043   rtx t;
4044   int nops;
4045
4046   /* Any branch must be followed by 2 non Cirrus instructions.  */
4047   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4048     {
4049       nops = 0;
4050       t = next_nonnote_insn (first);
4051
4052       if (is_cirrus_insn (t))
4053         ++ nops;
4054
4055       if (is_cirrus_insn (next_nonnote_insn (t)))
4056         ++ nops;
4057
4058       while (nops --)
4059         emit_insn_after (gen_nop (), first);
4060
4061       return;
4062     }
4063
4064   /* (float (blah)) is in parallel with a clobber.  */
4065   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4066     body = XVECEXP (body, 0, 0);
4067
4068   if (GET_CODE (body) == SET)
4069     {
4070       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4071
4072       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4073          be followed by a non Cirrus insn.  */
4074       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4075         {
4076           if (is_cirrus_insn (next_nonnote_insn (first)))
4077             emit_insn_after (gen_nop (), first);
4078
4079           return;
4080         }
4081       else if (is_load_address (first))
4082         {
4083           unsigned int arm_regno;
4084
4085           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4086              ldr/cfmv64hr combination where the Rd field is the same
4087              in both instructions must be split with a non Cirrus
4088              insn.  Example:
4089
4090              ldr r0, blah
4091              nop
4092              cfmvsr mvf0, r0.  */
4093
4094           /* Get Arm register number for ldr insn.  */
4095           if (GET_CODE (lhs) == REG)
4096             arm_regno = REGNO (lhs);
4097           else if (GET_CODE (rhs) == REG)
4098             arm_regno = REGNO (rhs);
4099           else
4100             abort ();
4101
4102           /* Next insn.  */
4103           first = next_nonnote_insn (first);
4104
4105           if (!is_cirrus_insn (first))
4106             return;
4107
4108           body = PATTERN (first);
4109
4110           /* (float (blah)) is in parallel with a clobber.  */
4111           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4112             body = XVECEXP (body, 0, 0);
4113
4114           if (GET_CODE (body) == FLOAT)
4115             body = XEXP (body, 0);
4116
4117           if (get_attr_cirrus (first) == CIRRUS_MOVE
4118               && GET_CODE (XEXP (body, 1)) == REG
4119               && arm_regno == REGNO (XEXP (body, 1)))
4120             emit_insn_after (gen_nop (), first);
4121
4122           return;
4123         }
4124     }
4125
4126   /* get_attr aborts on USE and CLOBBER.  */
4127   if (!first
4128       || GET_CODE (first) != INSN
4129       || GET_CODE (PATTERN (first)) == USE
4130       || GET_CODE (PATTERN (first)) == CLOBBER)
4131     return;
4132
4133   attr = get_attr_cirrus (first);
4134
4135   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4136      must be followed by a non-coprocessor instruction.  */
4137   if (attr == CIRRUS_COMPARE)
4138     {
4139       nops = 0;
4140
4141       t = next_nonnote_insn (first);
4142
4143       if (is_cirrus_insn (t))
4144         ++ nops;
4145
4146       if (is_cirrus_insn (next_nonnote_insn (t)))
4147         ++ nops;
4148
4149       while (nops --)
4150         emit_insn_after (gen_nop (), first);
4151
4152       return;
4153     }
4154 }
4155
4156 /* Return nonzero if OP is a constant power of two.  */
4157
4158 int
4159 power_of_two_operand (op, mode)
4160      rtx op;
4161      enum machine_mode mode ATTRIBUTE_UNUSED;
4162 {
4163   if (GET_CODE (op) == CONST_INT)
4164     {
4165       HOST_WIDE_INT value = INTVAL (op);
4166
4167       return value != 0  &&  (value & (value - 1)) == 0;
4168     }
4169
4170   return FALSE;
4171 }
4172
4173 /* Return TRUE for a valid operand of a DImode operation.
4174    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4175    Note that this disallows MEM(REG+REG), but allows
4176    MEM(PRE/POST_INC/DEC(REG)).  */
4177
4178 int
4179 di_operand (op, mode)
4180      rtx op;
4181      enum machine_mode mode;
4182 {
4183   if (s_register_operand (op, mode))
4184     return TRUE;
4185
4186   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4187     return FALSE;
4188
4189   if (GET_CODE (op) == SUBREG)
4190     op = SUBREG_REG (op);
4191
4192   switch (GET_CODE (op))
4193     {
4194     case CONST_DOUBLE:
4195     case CONST_INT:
4196       return TRUE;
4197
4198     case MEM:
4199       return memory_address_p (DImode, XEXP (op, 0));
4200
4201     default:
4202       return FALSE;
4203     }
4204 }
4205
4206 /* Like di_operand, but don't accept constants.  */
4207
4208 int
4209 nonimmediate_di_operand (op, mode)
4210      rtx op;
4211      enum machine_mode mode;
4212 {
4213   if (s_register_operand (op, mode))
4214     return TRUE;
4215
4216   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4217     return FALSE;
4218
4219   if (GET_CODE (op) == SUBREG)
4220     op = SUBREG_REG (op);
4221
4222   if (GET_CODE (op) == MEM)
4223     return memory_address_p (DImode, XEXP (op, 0));
4224
4225   return FALSE;
4226 }
4227
4228 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
4229    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4230    Note that this disallows MEM(REG+REG), but allows
4231    MEM(PRE/POST_INC/DEC(REG)).  */
4232
4233 int
4234 soft_df_operand (op, mode)
4235      rtx op;
4236      enum machine_mode mode;
4237 {
4238   if (s_register_operand (op, mode))
4239     return TRUE;
4240
4241   if (mode != VOIDmode && GET_MODE (op) != mode)
4242     return FALSE;
4243
4244   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4245     return FALSE;
4246   
4247   if (GET_CODE (op) == SUBREG)
4248     op = SUBREG_REG (op);
4249   
4250   switch (GET_CODE (op))
4251     {
4252     case CONST_DOUBLE:
4253       return TRUE;
4254
4255     case MEM:
4256       return memory_address_p (DFmode, XEXP (op, 0));
4257
4258     default:
4259       return FALSE;
4260     }
4261 }
4262
4263 /* Like soft_df_operand, but don't accept constants.  */
4264
4265 int
4266 nonimmediate_soft_df_operand (op, mode)
4267      rtx op;
4268      enum machine_mode mode;
4269 {
4270   if (s_register_operand (op, mode))
4271     return TRUE;
4272
4273   if (mode != VOIDmode && GET_MODE (op) != mode)
4274     return FALSE;
4275
4276   if (GET_CODE (op) == SUBREG)
4277     op = SUBREG_REG (op);
4278
4279   if (GET_CODE (op) == MEM)
4280     return memory_address_p (DFmode, XEXP (op, 0));
4281   return FALSE;
4282 }
4283
4284 /* Return TRUE for valid index operands.  */
4285
4286 int
4287 index_operand (op, mode)
4288      rtx op;
4289      enum machine_mode mode;
4290 {
4291   return (s_register_operand (op, mode)
4292           || (immediate_operand (op, mode)
4293               && (GET_CODE (op) != CONST_INT
4294                   || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4295 }
4296
4297 /* Return TRUE for valid shifts by a constant. This also accepts any
4298    power of two on the (somewhat overly relaxed) assumption that the
4299    shift operator in this case was a mult.  */
4300
4301 int
4302 const_shift_operand (op, mode)
4303      rtx op;
4304      enum machine_mode mode;
4305 {
4306   return (power_of_two_operand (op, mode)
4307           || (immediate_operand (op, mode)
4308               && (GET_CODE (op) != CONST_INT
4309                   || (INTVAL (op) < 32 && INTVAL (op) > 0))));
4310 }
4311
4312 /* Return TRUE for arithmetic operators which can be combined with a multiply
4313    (shift).  */
4314
4315 int
4316 shiftable_operator (x, mode)
4317      rtx x;
4318      enum machine_mode mode;
4319 {
4320   enum rtx_code code;
4321
4322   if (GET_MODE (x) != mode)
4323     return FALSE;
4324
4325   code = GET_CODE (x);
4326
4327   return (code == PLUS || code == MINUS
4328           || code == IOR || code == XOR || code == AND);
4329 }
4330
4331 /* Return TRUE for binary logical operators.  */
4332
4333 int
4334 logical_binary_operator (x, mode)
4335      rtx x;
4336      enum machine_mode mode;
4337 {
4338   enum rtx_code code;
4339
4340   if (GET_MODE (x) != mode)
4341     return FALSE;
4342
4343   code = GET_CODE (x);
4344
4345   return (code == IOR || code == XOR || code == AND);
4346 }
4347
4348 /* Return TRUE for shift operators.  */
4349
4350 int
4351 shift_operator (x, mode)
4352      rtx x;
4353      enum machine_mode mode;
4354 {
4355   enum rtx_code code;
4356
4357   if (GET_MODE (x) != mode)
4358     return FALSE;
4359
4360   code = GET_CODE (x);
4361
4362   if (code == MULT)
4363     return power_of_two_operand (XEXP (x, 1), mode);
4364
4365   return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
4366           || code == ROTATERT);
4367 }
4368
4369 /* Return TRUE if x is EQ or NE.  */
4370
4371 int
4372 equality_operator (x, mode)
4373      rtx x;
4374      enum machine_mode mode ATTRIBUTE_UNUSED;
4375 {
4376   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
4377 }
4378
4379 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ.  */
4380
4381 int
4382 arm_comparison_operator (x, mode)
4383      rtx x;
4384      enum machine_mode mode;
4385 {
4386   return (comparison_operator (x, mode)
4387           && GET_CODE (x) != LTGT
4388           && GET_CODE (x) != UNEQ);
4389 }
4390
4391 /* Return TRUE for SMIN SMAX UMIN UMAX operators.  */
4392
4393 int
4394 minmax_operator (x, mode)
4395      rtx x;
4396      enum machine_mode mode;
4397 {
4398   enum rtx_code code = GET_CODE (x);
4399
4400   if (GET_MODE (x) != mode)
4401     return FALSE;
4402
4403   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
4404 }
4405
4406 /* Return TRUE if this is the condition code register, if we aren't given
4407    a mode, accept any class CCmode register.  */
4408
4409 int
4410 cc_register (x, mode)
4411      rtx x;
4412      enum machine_mode mode;
4413 {
4414   if (mode == VOIDmode)
4415     {
4416       mode = GET_MODE (x);
4417       
4418       if (GET_MODE_CLASS (mode) != MODE_CC)
4419         return FALSE;
4420     }
4421
4422   if (   GET_MODE (x) == mode
4423       && GET_CODE (x) == REG
4424       && REGNO    (x) == CC_REGNUM)
4425     return TRUE;
4426
4427   return FALSE;
4428 }
4429
4430 /* Return TRUE if this is the condition code register, if we aren't given
4431    a mode, accept any class CCmode register which indicates a dominance
4432    expression.  */
4433
4434 int
4435 dominant_cc_register (x, mode)
4436      rtx x;
4437      enum machine_mode mode;
4438 {
4439   if (mode == VOIDmode)
4440     {
4441       mode = GET_MODE (x);
4442       
4443       if (GET_MODE_CLASS (mode) != MODE_CC)
4444         return FALSE;
4445     }
4446
4447   if (   mode != CC_DNEmode && mode != CC_DEQmode
4448       && mode != CC_DLEmode && mode != CC_DLTmode
4449       && mode != CC_DGEmode && mode != CC_DGTmode
4450       && mode != CC_DLEUmode && mode != CC_DLTUmode
4451       && mode != CC_DGEUmode && mode != CC_DGTUmode)
4452     return FALSE;
4453
4454   return cc_register (x, mode);
4455 }
4456
4457 /* Return TRUE if X references a SYMBOL_REF.  */
4458
4459 int
4460 symbol_mentioned_p (x)
4461      rtx x;
4462 {
4463   const char * fmt;
4464   int i;
4465
4466   if (GET_CODE (x) == SYMBOL_REF)
4467     return 1;
4468
4469   fmt = GET_RTX_FORMAT (GET_CODE (x));
4470   
4471   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4472     {
4473       if (fmt[i] == 'E')
4474         {
4475           int j;
4476
4477           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4478             if (symbol_mentioned_p (XVECEXP (x, i, j)))
4479               return 1;
4480         }
4481       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
4482         return 1;
4483     }
4484
4485   return 0;
4486 }
4487
4488 /* Return TRUE if X references a LABEL_REF.  */
4489
4490 int
4491 label_mentioned_p (x)
4492      rtx x;
4493 {
4494   const char * fmt;
4495   int i;
4496
4497   if (GET_CODE (x) == LABEL_REF)
4498     return 1;
4499
4500   fmt = GET_RTX_FORMAT (GET_CODE (x));
4501   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4502     {
4503       if (fmt[i] == 'E')
4504         {
4505           int j;
4506
4507           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4508             if (label_mentioned_p (XVECEXP (x, i, j)))
4509               return 1;
4510         }
4511       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
4512         return 1;
4513     }
4514
4515   return 0;
4516 }
4517
4518 enum rtx_code
4519 minmax_code (x)
4520      rtx x;
4521 {
4522   enum rtx_code code = GET_CODE (x);
4523
4524   if (code == SMAX)
4525     return GE;
4526   else if (code == SMIN)
4527     return LE;
4528   else if (code == UMIN)
4529     return LEU;
4530   else if (code == UMAX)
4531     return GEU;
4532
4533   abort ();
4534 }
4535
4536 /* Return 1 if memory locations are adjacent.  */
4537
4538 int
4539 adjacent_mem_locations (a, b)
4540      rtx a, b;
4541 {
4542   if ((GET_CODE (XEXP (a, 0)) == REG
4543        || (GET_CODE (XEXP (a, 0)) == PLUS
4544            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
4545       && (GET_CODE (XEXP (b, 0)) == REG
4546           || (GET_CODE (XEXP (b, 0)) == PLUS
4547               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
4548     {
4549       int val0 = 0, val1 = 0;
4550       int reg0, reg1;
4551   
4552       if (GET_CODE (XEXP (a, 0)) == PLUS)
4553         {
4554           reg0 = REGNO  (XEXP (XEXP (a, 0), 0));
4555           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
4556         }
4557       else
4558         reg0 = REGNO (XEXP (a, 0));
4559
4560       if (GET_CODE (XEXP (b, 0)) == PLUS)
4561         {
4562           reg1 = REGNO  (XEXP (XEXP (b, 0), 0));
4563           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
4564         }
4565       else
4566         reg1 = REGNO (XEXP (b, 0));
4567
4568       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
4569     }
4570   return 0;
4571 }
4572
4573 /* Return 1 if OP is a load multiple operation.  It is known to be
4574    parallel and the first section will be tested.  */
4575
4576 int
4577 load_multiple_operation (op, mode)
4578      rtx op;
4579      enum machine_mode mode ATTRIBUTE_UNUSED;
4580 {
4581   HOST_WIDE_INT count = XVECLEN (op, 0);
4582   int dest_regno;
4583   rtx src_addr;
4584   HOST_WIDE_INT i = 1, base = 0;
4585   rtx elt;
4586
4587   if (count <= 1
4588       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
4589     return 0;
4590
4591   /* Check to see if this might be a write-back.  */
4592   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
4593     {
4594       i++;
4595       base = 1;
4596
4597       /* Now check it more carefully.  */
4598       if (GET_CODE (SET_DEST (elt)) != REG
4599           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
4600           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
4601           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
4602           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
4603         return 0;
4604     }
4605
4606   /* Perform a quick check so we don't blow up below.  */
4607   if (count <= i
4608       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
4609       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
4610       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
4611     return 0;
4612
4613   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
4614   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
4615
4616   for (; i < count; i++)
4617     {
4618       elt = XVECEXP (op, 0, i);
4619
4620       if (GET_CODE (elt) != SET
4621           || GET_CODE (SET_DEST (elt)) != REG
4622           || GET_MODE (SET_DEST (elt)) != SImode
4623           || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
4624           || GET_CODE (SET_SRC (elt)) != MEM
4625           || GET_MODE (SET_SRC (elt)) != SImode
4626           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
4627           || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
4628           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
4629           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
4630         return 0;
4631     }
4632
4633   return 1;
4634 }
4635
4636 /* Return 1 if OP is a store multiple operation.  It is known to be
4637    parallel and the first section will be tested.  */
4638
4639 int
4640 store_multiple_operation (op, mode)
4641      rtx op;
4642      enum machine_mode mode ATTRIBUTE_UNUSED;
4643 {
4644   HOST_WIDE_INT count = XVECLEN (op, 0);
4645   int src_regno;
4646   rtx dest_addr;
4647   HOST_WIDE_INT i = 1, base = 0;
4648   rtx elt;
4649
4650   if (count <= 1
4651       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
4652     return 0;
4653
4654   /* Check to see if this might be a write-back.  */
4655   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
4656     {
4657       i++;
4658       base = 1;
4659
4660       /* Now check it more carefully.  */
4661       if (GET_CODE (SET_DEST (elt)) != REG
4662           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
4663           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
4664           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
4665           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
4666         return 0;
4667     }
4668
4669   /* Perform a quick check so we don't blow up below.  */
4670   if (count <= i
4671       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
4672       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
4673       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
4674     return 0;
4675
4676   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
4677   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
4678
4679   for (; i < count; i++)
4680     {
4681       elt = XVECEXP (op, 0, i);
4682
4683       if (GET_CODE (elt) != SET
4684           || GET_CODE (SET_SRC (elt)) != REG
4685           || GET_MODE (SET_SRC (elt)) != SImode
4686           || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
4687           || GET_CODE (SET_DEST (elt)) != MEM
4688           || GET_MODE (SET_DEST (elt)) != SImode
4689           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
4690           || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
4691           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
4692           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
4693         return 0;
4694     }
4695
4696   return 1;
4697 }
4698
4699 int
4700 load_multiple_sequence (operands, nops, regs, base, load_offset)
4701      rtx * operands;
4702      int nops;
4703      int * regs;
4704      int * base;
4705      HOST_WIDE_INT * load_offset;
4706 {
4707   int unsorted_regs[4];
4708   HOST_WIDE_INT unsorted_offsets[4];
4709   int order[4];
4710   int base_reg = -1;
4711   int i;
4712
4713   /* Can only handle 2, 3, or 4 insns at present,
4714      though could be easily extended if required.  */
4715   if (nops < 2 || nops > 4)
4716     abort ();
4717
4718   /* Loop over the operands and check that the memory references are
4719      suitable (ie immediate offsets from the same base register).  At
4720      the same time, extract the target register, and the memory
4721      offsets.  */
4722   for (i = 0; i < nops; i++)
4723     {
4724       rtx reg;
4725       rtx offset;
4726
4727       /* Convert a subreg of a mem into the mem itself.  */
4728       if (GET_CODE (operands[nops + i]) == SUBREG)
4729         operands[nops + i] = alter_subreg (operands + (nops + i));
4730
4731       if (GET_CODE (operands[nops + i]) != MEM)
4732         abort ();
4733
4734       /* Don't reorder volatile memory references; it doesn't seem worth
4735          looking for the case where the order is ok anyway.  */
4736       if (MEM_VOLATILE_P (operands[nops + i]))
4737         return 0;
4738
4739       offset = const0_rtx;
4740
4741       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
4742            || (GET_CODE (reg) == SUBREG
4743                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4744           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
4745               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
4746                    == REG)
4747                   || (GET_CODE (reg) == SUBREG
4748                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4749               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
4750                   == CONST_INT)))
4751         {
4752           if (i == 0)
4753             {
4754               base_reg = REGNO (reg);
4755               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
4756                                   ? REGNO (operands[i])
4757                                   : REGNO (SUBREG_REG (operands[i])));
4758               order[0] = 0;
4759             }
4760           else 
4761             {
4762               if (base_reg != (int) REGNO (reg))
4763                 /* Not addressed from the same base register.  */
4764                 return 0;
4765
4766               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
4767                                   ? REGNO (operands[i])
4768                                   : REGNO (SUBREG_REG (operands[i])));
4769               if (unsorted_regs[i] < unsorted_regs[order[0]])
4770                 order[0] = i;
4771             }
4772
4773           /* If it isn't an integer register, or if it overwrites the
4774              base register but isn't the last insn in the list, then
4775              we can't do this.  */
4776           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
4777               || (i != nops - 1 && unsorted_regs[i] == base_reg))
4778             return 0;
4779
4780           unsorted_offsets[i] = INTVAL (offset);
4781         }
4782       else
4783         /* Not a suitable memory address.  */
4784         return 0;
4785     }
4786
4787   /* All the useful information has now been extracted from the
4788      operands into unsorted_regs and unsorted_offsets; additionally,
4789      order[0] has been set to the lowest numbered register in the
4790      list.  Sort the registers into order, and check that the memory
4791      offsets are ascending and adjacent.  */
4792
4793   for (i = 1; i < nops; i++)
4794     {
4795       int j;
4796
4797       order[i] = order[i - 1];
4798       for (j = 0; j < nops; j++)
4799         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
4800             && (order[i] == order[i - 1]
4801                 || unsorted_regs[j] < unsorted_regs[order[i]]))
4802           order[i] = j;
4803
4804       /* Have we found a suitable register? if not, one must be used more
4805          than once.  */
4806       if (order[i] == order[i - 1])
4807         return 0;
4808
4809       /* Is the memory address adjacent and ascending? */
4810       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
4811         return 0;
4812     }
4813
4814   if (base)
4815     {
4816       *base = base_reg;
4817
4818       for (i = 0; i < nops; i++)
4819         regs[i] = unsorted_regs[order[i]];
4820
4821       *load_offset = unsorted_offsets[order[0]];
4822     }
4823
4824   if (unsorted_offsets[order[0]] == 0)
4825     return 1; /* ldmia */
4826
4827   if (unsorted_offsets[order[0]] == 4)
4828     return 2; /* ldmib */
4829
4830   if (unsorted_offsets[order[nops - 1]] == 0)
4831     return 3; /* ldmda */
4832
4833   if (unsorted_offsets[order[nops - 1]] == -4)
4834     return 4; /* ldmdb */
4835
4836   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
4837      if the offset isn't small enough.  The reason 2 ldrs are faster
4838      is because these ARMs are able to do more than one cache access
4839      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
4840      whilst the ARM8 has a double bandwidth cache.  This means that
4841      these cores can do both an instruction fetch and a data fetch in
4842      a single cycle, so the trick of calculating the address into a
4843      scratch register (one of the result regs) and then doing a load
4844      multiple actually becomes slower (and no smaller in code size).
4845      That is the transformation
4846  
4847         ldr     rd1, [rbase + offset]
4848         ldr     rd2, [rbase + offset + 4]
4849  
4850      to
4851  
4852         add     rd1, rbase, offset
4853         ldmia   rd1, {rd1, rd2}
4854  
4855      produces worse code -- '3 cycles + any stalls on rd2' instead of
4856      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
4857      access per cycle, the first sequence could never complete in less
4858      than 6 cycles, whereas the ldm sequence would only take 5 and
4859      would make better use of sequential accesses if not hitting the
4860      cache.
4861
4862      We cheat here and test 'arm_ld_sched' which we currently know to
4863      only be true for the ARM8, ARM9 and StrongARM.  If this ever
4864      changes, then the test below needs to be reworked.  */
4865   if (nops == 2 && arm_ld_sched)
4866     return 0;
4867
4868   /* Can't do it without setting up the offset, only do this if it takes
4869      no more than one insn.  */
4870   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
4871           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
4872 }
4873
4874 const char *
4875 emit_ldm_seq (operands, nops)
4876      rtx * operands;
4877      int nops;
4878 {
4879   int regs[4];
4880   int base_reg;
4881   HOST_WIDE_INT offset;
4882   char buf[100];
4883   int i;
4884
4885   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
4886     {
4887     case 1:
4888       strcpy (buf, "ldm%?ia\t");
4889       break;
4890
4891     case 2:
4892       strcpy (buf, "ldm%?ib\t");
4893       break;
4894
4895     case 3:
4896       strcpy (buf, "ldm%?da\t");
4897       break;
4898
4899     case 4:
4900       strcpy (buf, "ldm%?db\t");
4901       break;
4902
4903     case 5:
4904       if (offset >= 0)
4905         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
4906                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
4907                  (long) offset);
4908       else
4909         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
4910                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
4911                  (long) -offset);
4912       output_asm_insn (buf, operands);
4913       base_reg = regs[0];
4914       strcpy (buf, "ldm%?ia\t");
4915       break;
4916
4917     default:
4918       abort ();
4919     }
4920
4921   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
4922            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
4923
4924   for (i = 1; i < nops; i++)
4925     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
4926              reg_names[regs[i]]);
4927
4928   strcat (buf, "}\t%@ phole ldm");
4929
4930   output_asm_insn (buf, operands);
4931   return "";
4932 }
4933
4934 int
4935 store_multiple_sequence (operands, nops, regs, base, load_offset)
4936      rtx * operands;
4937      int nops;
4938      int * regs;
4939      int * base;
4940      HOST_WIDE_INT * load_offset;
4941 {
4942   int unsorted_regs[4];
4943   HOST_WIDE_INT unsorted_offsets[4];
4944   int order[4];
4945   int base_reg = -1;
4946   int i;
4947
4948   /* Can only handle 2, 3, or 4 insns at present, though could be easily
4949      extended if required.  */
4950   if (nops < 2 || nops > 4)
4951     abort ();
4952
4953   /* Loop over the operands and check that the memory references are
4954      suitable (ie immediate offsets from the same base register).  At
4955      the same time, extract the target register, and the memory
4956      offsets.  */
4957   for (i = 0; i < nops; i++)
4958     {
4959       rtx reg;
4960       rtx offset;
4961
4962       /* Convert a subreg of a mem into the mem itself.  */
4963       if (GET_CODE (operands[nops + i]) == SUBREG)
4964         operands[nops + i] = alter_subreg (operands + (nops + i));
4965
4966       if (GET_CODE (operands[nops + i]) != MEM)
4967         abort ();
4968
4969       /* Don't reorder volatile memory references; it doesn't seem worth
4970          looking for the case where the order is ok anyway.  */
4971       if (MEM_VOLATILE_P (operands[nops + i]))
4972         return 0;
4973
4974       offset = const0_rtx;
4975
4976       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
4977            || (GET_CODE (reg) == SUBREG
4978                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4979           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
4980               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
4981                    == REG)
4982                   || (GET_CODE (reg) == SUBREG
4983                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
4984               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
4985                   == CONST_INT)))
4986         {
4987           if (i == 0)
4988             {
4989               base_reg = REGNO (reg);
4990               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
4991                                   ? REGNO (operands[i])
4992                                   : REGNO (SUBREG_REG (operands[i])));
4993               order[0] = 0;
4994             }
4995           else 
4996             {
4997               if (base_reg != (int) REGNO (reg))
4998                 /* Not addressed from the same base register.  */
4999                 return 0;
5000
5001               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5002                                   ? REGNO (operands[i])
5003                                   : REGNO (SUBREG_REG (operands[i])));
5004               if (unsorted_regs[i] < unsorted_regs[order[0]])
5005                 order[0] = i;
5006             }
5007
5008           /* If it isn't an integer register, then we can't do this.  */
5009           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5010             return 0;
5011
5012           unsorted_offsets[i] = INTVAL (offset);
5013         }
5014       else
5015         /* Not a suitable memory address.  */
5016         return 0;
5017     }
5018
5019   /* All the useful information has now been extracted from the
5020      operands into unsorted_regs and unsorted_offsets; additionally,
5021      order[0] has been set to the lowest numbered register in the
5022      list.  Sort the registers into order, and check that the memory
5023      offsets are ascending and adjacent.  */
5024
5025   for (i = 1; i < nops; i++)
5026     {
5027       int j;
5028
5029       order[i] = order[i - 1];
5030       for (j = 0; j < nops; j++)
5031         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5032             && (order[i] == order[i - 1]
5033                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5034           order[i] = j;
5035
5036       /* Have we found a suitable register? if not, one must be used more
5037          than once.  */
5038       if (order[i] == order[i - 1])
5039         return 0;
5040
5041       /* Is the memory address adjacent and ascending? */
5042       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5043         return 0;
5044     }
5045
5046   if (base)
5047     {
5048       *base = base_reg;
5049
5050       for (i = 0; i < nops; i++)
5051         regs[i] = unsorted_regs[order[i]];
5052
5053       *load_offset = unsorted_offsets[order[0]];
5054     }
5055
5056   if (unsorted_offsets[order[0]] == 0)
5057     return 1; /* stmia */
5058
5059   if (unsorted_offsets[order[0]] == 4)
5060     return 2; /* stmib */
5061
5062   if (unsorted_offsets[order[nops - 1]] == 0)
5063     return 3; /* stmda */
5064
5065   if (unsorted_offsets[order[nops - 1]] == -4)
5066     return 4; /* stmdb */
5067
5068   return 0;
5069 }
5070
5071 const char *
5072 emit_stm_seq (operands, nops)
5073      rtx * operands;
5074      int nops;
5075 {
5076   int regs[4];
5077   int base_reg;
5078   HOST_WIDE_INT offset;
5079   char buf[100];
5080   int i;
5081
5082   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5083     {
5084     case 1:
5085       strcpy (buf, "stm%?ia\t");
5086       break;
5087
5088     case 2:
5089       strcpy (buf, "stm%?ib\t");
5090       break;
5091
5092     case 3:
5093       strcpy (buf, "stm%?da\t");
5094       break;
5095
5096     case 4:
5097       strcpy (buf, "stm%?db\t");
5098       break;
5099
5100     default:
5101       abort ();
5102     }
5103
5104   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5105            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5106
5107   for (i = 1; i < nops; i++)
5108     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5109              reg_names[regs[i]]);
5110
5111   strcat (buf, "}\t%@ phole stm");
5112
5113   output_asm_insn (buf, operands);
5114   return "";
5115 }
5116
5117 int
5118 multi_register_push (op, mode)
5119      rtx op;
5120      enum machine_mode mode ATTRIBUTE_UNUSED;
5121 {
5122   if (GET_CODE (op) != PARALLEL
5123       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5124       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5125       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5126     return 0;
5127
5128   return 1;
5129 }
5130 \f
5131 /* Routines for use in generating RTL.  */
5132
5133 rtx
5134 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
5135                        in_struct_p, scalar_p)
5136      int base_regno;
5137      int count;
5138      rtx from;
5139      int up;
5140      int write_back;
5141      int unchanging_p;
5142      int in_struct_p;
5143      int scalar_p;
5144 {
5145   int i = 0, j;
5146   rtx result;
5147   int sign = up ? 1 : -1;
5148   rtx mem;
5149
5150   /* XScale has load-store double instructions, but they have stricter
5151      alignment requirements than load-store multiple, so we can not
5152      use them.
5153
5154      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5155      the pipeline until completion.
5156
5157         NREGS           CYCLES
5158           1               3
5159           2               4
5160           3               5
5161           4               6
5162
5163      An ldr instruction takes 1-3 cycles, but does not block the
5164      pipeline.
5165
5166         NREGS           CYCLES
5167           1              1-3
5168           2              2-6
5169           3              3-9
5170           4              4-12
5171
5172      Best case ldr will always win.  However, the more ldr instructions
5173      we issue, the less likely we are to be able to schedule them well.
5174      Using ldr instructions also increases code size.
5175
5176      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5177      for counts of 3 or 4 regs.  */
5178   if (arm_is_xscale && count <= 2 && ! optimize_size)
5179     {
5180       rtx seq;
5181       
5182       start_sequence ();
5183       
5184       for (i = 0; i < count; i++)
5185         {
5186           mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5187           RTX_UNCHANGING_P (mem) = unchanging_p;
5188           MEM_IN_STRUCT_P (mem) = in_struct_p;
5189           MEM_SCALAR_P (mem) = scalar_p;
5190           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5191         }
5192
5193       if (write_back)
5194         emit_move_insn (from, plus_constant (from, count * 4 * sign));
5195
5196       seq = get_insns ();
5197       end_sequence ();
5198       
5199       return seq;
5200     }
5201
5202   result = gen_rtx_PARALLEL (VOIDmode,
5203                              rtvec_alloc (count + (write_back ? 1 : 0)));
5204   if (write_back)
5205     {
5206       XVECEXP (result, 0, 0)
5207         = gen_rtx_SET (GET_MODE (from), from,
5208                        plus_constant (from, count * 4 * sign));
5209       i = 1;
5210       count++;
5211     }
5212
5213   for (j = 0; i < count; i++, j++)
5214     {
5215       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5216       RTX_UNCHANGING_P (mem) = unchanging_p;
5217       MEM_IN_STRUCT_P (mem) = in_struct_p;
5218       MEM_SCALAR_P (mem) = scalar_p;
5219       XVECEXP (result, 0, i)
5220         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5221     }
5222
5223   return result;
5224 }
5225
5226 rtx
5227 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
5228                         in_struct_p, scalar_p)
5229      int base_regno;
5230      int count;
5231      rtx to;
5232      int up;
5233      int write_back;
5234      int unchanging_p;
5235      int in_struct_p;
5236      int scalar_p;
5237 {
5238   int i = 0, j;
5239   rtx result;
5240   int sign = up ? 1 : -1;
5241   rtx mem;
5242
5243   /* See arm_gen_load_multiple for discussion of
5244      the pros/cons of ldm/stm usage for XScale.  */
5245   if (arm_is_xscale && count <= 2 && ! optimize_size)
5246     {
5247       rtx seq;
5248       
5249       start_sequence ();
5250       
5251       for (i = 0; i < count; i++)
5252         {
5253           mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5254           RTX_UNCHANGING_P (mem) = unchanging_p;
5255           MEM_IN_STRUCT_P (mem) = in_struct_p;
5256           MEM_SCALAR_P (mem) = scalar_p;
5257           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5258         }
5259
5260       if (write_back)
5261         emit_move_insn (to, plus_constant (to, count * 4 * sign));
5262
5263       seq = get_insns ();
5264       end_sequence ();
5265       
5266       return seq;
5267     }
5268
5269   result = gen_rtx_PARALLEL (VOIDmode,
5270                              rtvec_alloc (count + (write_back ? 1 : 0)));
5271   if (write_back)
5272     {
5273       XVECEXP (result, 0, 0)
5274         = gen_rtx_SET (GET_MODE (to), to,
5275                        plus_constant (to, count * 4 * sign));
5276       i = 1;
5277       count++;
5278     }
5279
5280   for (j = 0; i < count; i++, j++)
5281     {
5282       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5283       RTX_UNCHANGING_P (mem) = unchanging_p;
5284       MEM_IN_STRUCT_P (mem) = in_struct_p;
5285       MEM_SCALAR_P (mem) = scalar_p;
5286
5287       XVECEXP (result, 0, i)
5288         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5289     }
5290
5291   return result;
5292 }
5293
5294 int
5295 arm_gen_movstrqi (operands)
5296      rtx * operands;
5297 {
5298   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5299   int i;
5300   rtx src, dst;
5301   rtx st_src, st_dst, fin_src, fin_dst;
5302   rtx part_bytes_reg = NULL;
5303   rtx mem;
5304   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5305   int dst_scalar_p, src_scalar_p;
5306
5307   if (GET_CODE (operands[2]) != CONST_INT
5308       || GET_CODE (operands[3]) != CONST_INT
5309       || INTVAL (operands[2]) > 64
5310       || INTVAL (operands[3]) & 3)
5311     return 0;
5312
5313   st_dst = XEXP (operands[0], 0);
5314   st_src = XEXP (operands[1], 0);
5315
5316   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5317   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5318   dst_scalar_p = MEM_SCALAR_P (operands[0]);
5319   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5320   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5321   src_scalar_p = MEM_SCALAR_P (operands[1]);
5322
5323   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5324   fin_src = src = copy_to_mode_reg (SImode, st_src);
5325
5326   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5327   out_words_to_go = INTVAL (operands[2]) / 4;
5328   last_bytes = INTVAL (operands[2]) & 3;
5329
5330   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5331     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5332
5333   for (i = 0; in_words_to_go >= 2; i+=4)
5334     {
5335       if (in_words_to_go > 4)
5336         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5337                                           src_unchanging_p,
5338                                           src_in_struct_p,
5339                                           src_scalar_p));
5340       else
5341         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
5342                                           FALSE, src_unchanging_p,
5343                                           src_in_struct_p, src_scalar_p));
5344
5345       if (out_words_to_go)
5346         {
5347           if (out_words_to_go > 4)
5348             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5349                                                dst_unchanging_p,
5350                                                dst_in_struct_p,
5351                                                dst_scalar_p));
5352           else if (out_words_to_go != 1)
5353             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5354                                                dst, TRUE, 
5355                                                (last_bytes == 0
5356                                                 ? FALSE : TRUE),
5357                                                dst_unchanging_p,
5358                                                dst_in_struct_p,
5359                                                dst_scalar_p));
5360           else
5361             {
5362               mem = gen_rtx_MEM (SImode, dst);
5363               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5364               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5365               MEM_SCALAR_P (mem) = dst_scalar_p;
5366               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5367               if (last_bytes != 0)
5368                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5369             }
5370         }
5371
5372       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5373       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5374     }
5375
5376   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5377   if (out_words_to_go)
5378     {
5379       rtx sreg;
5380       
5381       mem = gen_rtx_MEM (SImode, src);
5382       RTX_UNCHANGING_P (mem) = src_unchanging_p;
5383       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
5384       MEM_SCALAR_P (mem) = src_scalar_p;
5385       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
5386       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
5387       
5388       mem = gen_rtx_MEM (SImode, dst);
5389       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5390       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5391       MEM_SCALAR_P (mem) = dst_scalar_p;
5392       emit_move_insn (mem, sreg);
5393       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
5394       in_words_to_go--;
5395       
5396       if (in_words_to_go)       /* Sanity check */
5397         abort ();
5398     }
5399
5400   if (in_words_to_go)
5401     {
5402       if (in_words_to_go < 0)
5403         abort ();
5404
5405       mem = gen_rtx_MEM (SImode, src);
5406       RTX_UNCHANGING_P (mem) = src_unchanging_p;
5407       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
5408       MEM_SCALAR_P (mem) = src_scalar_p;
5409       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5410     }
5411
5412   if (last_bytes && part_bytes_reg == NULL)
5413     abort ();
5414
5415   if (BYTES_BIG_ENDIAN && last_bytes)
5416     {
5417       rtx tmp = gen_reg_rtx (SImode);
5418
5419       /* The bytes we want are in the top end of the word.  */
5420       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5421                               GEN_INT (8 * (4 - last_bytes))));
5422       part_bytes_reg = tmp;
5423       
5424       while (last_bytes)
5425         {
5426           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
5427           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5428           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5429           MEM_SCALAR_P (mem) = dst_scalar_p;
5430           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5431
5432           if (--last_bytes)
5433             {
5434               tmp = gen_reg_rtx (SImode);
5435               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5436               part_bytes_reg = tmp;
5437             }
5438         }
5439           
5440     }
5441   else
5442     {
5443       if (last_bytes > 1)
5444         {
5445           mem = gen_rtx_MEM (HImode, dst);
5446           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5447           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5448           MEM_SCALAR_P (mem) = dst_scalar_p;
5449           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5450           last_bytes -= 2;
5451           if (last_bytes)
5452             {
5453               rtx tmp = gen_reg_rtx (SImode);
5454
5455               emit_insn (gen_addsi3 (dst, dst, GEN_INT (2)));
5456               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5457               part_bytes_reg = tmp;
5458             }
5459         }
5460       
5461       if (last_bytes)
5462         {
5463           mem = gen_rtx_MEM (QImode, dst);
5464           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5465           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5466           MEM_SCALAR_P (mem) = dst_scalar_p;
5467           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5468         }
5469     }
5470
5471   return 1;
5472 }
5473
5474 /* Generate a memory reference for a half word, such that it will be loaded
5475    into the top 16 bits of the word.  We can assume that the address is
5476    known to be alignable and of the form reg, or plus (reg, const).  */
5477
5478 rtx
5479 arm_gen_rotated_half_load (memref)
5480      rtx memref;
5481 {
5482   HOST_WIDE_INT offset = 0;
5483   rtx base = XEXP (memref, 0);
5484
5485   if (GET_CODE (base) == PLUS)
5486     {
5487       offset = INTVAL (XEXP (base, 1));
5488       base = XEXP (base, 0);
5489     }
5490
5491   /* If we aren't allowed to generate unaligned addresses, then fail.  */
5492   if (TARGET_MMU_TRAPS
5493       && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
5494     return NULL;
5495
5496   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5497
5498   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5499     return base;
5500
5501   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5502 }
5503
5504 /* Select a dominance comparison mode if possible.  We support three forms.
5505    COND_OR == 0 => (X && Y) 
5506    COND_OR == 1 => ((! X( || Y)
5507    COND_OR == 2 => (X || Y) 
5508    If we are unable to support a dominance comparison we return CC mode.  
5509    This will then fail to match for the RTL expressions that generate this
5510    call.  */
5511
5512 static enum machine_mode
5513 select_dominance_cc_mode (x, y, cond_or)
5514      rtx x;
5515      rtx y;
5516      HOST_WIDE_INT cond_or;
5517 {
5518   enum rtx_code cond1, cond2;
5519   int swapped = 0;
5520
5521   /* Currently we will probably get the wrong result if the individual
5522      comparisons are not simple.  This also ensures that it is safe to
5523      reverse a comparison if necessary.  */
5524   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5525        != CCmode)
5526       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5527           != CCmode))
5528     return CCmode;
5529
5530   /* The if_then_else variant of this tests the second condition if the
5531      first passes, but is true if the first fails.  Reverse the first
5532      condition to get a true "inclusive-or" expression.  */
5533   if (cond_or == 1)
5534     cond1 = reverse_condition (cond1);
5535
5536   /* If the comparisons are not equal, and one doesn't dominate the other,
5537      then we can't do this.  */
5538   if (cond1 != cond2 
5539       && !comparison_dominates_p (cond1, cond2)
5540       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5541     return CCmode;
5542
5543   if (swapped)
5544     {
5545       enum rtx_code temp = cond1;
5546       cond1 = cond2;
5547       cond2 = temp;
5548     }
5549
5550   switch (cond1)
5551     {
5552     case EQ:
5553       if (cond2 == EQ || !cond_or)
5554         return CC_DEQmode;
5555
5556       switch (cond2)
5557         {
5558         case LE: return CC_DLEmode;
5559         case LEU: return CC_DLEUmode;
5560         case GE: return CC_DGEmode;
5561         case GEU: return CC_DGEUmode;
5562         default: break;
5563         }
5564
5565       break;
5566
5567     case LT:
5568       if (cond2 == LT || !cond_or)
5569         return CC_DLTmode;
5570       if (cond2 == LE)
5571         return CC_DLEmode;
5572       if (cond2 == NE)
5573         return CC_DNEmode;
5574       break;
5575
5576     case GT:
5577       if (cond2 == GT || !cond_or)
5578         return CC_DGTmode;
5579       if (cond2 == GE)
5580         return CC_DGEmode;
5581       if (cond2 == NE)
5582         return CC_DNEmode;
5583       break;
5584       
5585     case LTU:
5586       if (cond2 == LTU || !cond_or)
5587         return CC_DLTUmode;
5588       if (cond2 == LEU)
5589         return CC_DLEUmode;
5590       if (cond2 == NE)
5591         return CC_DNEmode;
5592       break;
5593
5594     case GTU:
5595       if (cond2 == GTU || !cond_or)
5596         return CC_DGTUmode;
5597       if (cond2 == GEU)
5598         return CC_DGEUmode;
5599       if (cond2 == NE)
5600         return CC_DNEmode;
5601       break;
5602
5603     /* The remaining cases only occur when both comparisons are the
5604        same.  */
5605     case NE:
5606       return CC_DNEmode;
5607
5608     case LE:
5609       return CC_DLEmode;
5610
5611     case GE:
5612       return CC_DGEmode;
5613
5614     case LEU:
5615       return CC_DLEUmode;
5616
5617     case GEU:
5618       return CC_DGEUmode;
5619
5620     default:
5621       break;
5622     }
5623
5624   abort ();
5625 }
5626
5627 enum machine_mode
5628 arm_select_cc_mode (op, x, y)
5629      enum rtx_code op;
5630      rtx x;
5631      rtx y;
5632 {
5633   /* All floating point compares return CCFP if it is an equality
5634      comparison, and CCFPE otherwise.  */
5635   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
5636     {
5637       switch (op)
5638         {
5639         case EQ:
5640         case NE:
5641         case UNORDERED:
5642         case ORDERED:
5643         case UNLT:
5644         case UNLE:
5645         case UNGT:
5646         case UNGE:
5647         case UNEQ:
5648         case LTGT:
5649           return CCFPmode;
5650
5651         case LT:
5652         case LE:
5653         case GT:
5654         case GE:
5655           if (TARGET_CIRRUS)
5656             return CCFPmode;
5657           return CCFPEmode;
5658
5659         default:
5660           abort ();
5661         }
5662     }
5663   
5664   /* A compare with a shifted operand.  Because of canonicalization, the
5665      comparison will have to be swapped when we emit the assembler.  */
5666   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
5667       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
5668           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
5669           || GET_CODE (x) == ROTATERT))
5670     return CC_SWPmode;
5671
5672   /* This is a special case that is used by combine to allow a 
5673      comparison of a shifted byte load to be split into a zero-extend
5674      followed by a comparison of the shifted integer (only valid for
5675      equalities and unsigned inequalities).  */
5676   if (GET_MODE (x) == SImode
5677       && GET_CODE (x) == ASHIFT
5678       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
5679       && GET_CODE (XEXP (x, 0)) == SUBREG
5680       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
5681       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
5682       && (op == EQ || op == NE
5683           || op == GEU || op == GTU || op == LTU || op == LEU)
5684       && GET_CODE (y) == CONST_INT)
5685     return CC_Zmode;
5686
5687   /* A construct for a conditional compare, if the false arm contains
5688      0, then both conditions must be true, otherwise either condition
5689      must be true.  Not all conditions are possible, so CCmode is
5690      returned if it can't be done.  */
5691   if (GET_CODE (x) == IF_THEN_ELSE
5692       && (XEXP (x, 2) == const0_rtx
5693           || XEXP (x, 2) == const1_rtx)
5694       && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5695       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
5696     return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
5697                                      INTVAL (XEXP (x, 2)));
5698
5699   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
5700   if (GET_CODE (x) == AND
5701       && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5702       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
5703     return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 0);
5704
5705   if (GET_CODE (x) == IOR
5706       && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5707       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
5708     return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 2);
5709
5710   /* An operation that sets the condition codes as a side-effect, the
5711      V flag is not set correctly, so we can only use comparisons where
5712      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
5713      instead.  */
5714   if (GET_MODE (x) == SImode
5715       && y == const0_rtx
5716       && (op == EQ || op == NE || op == LT || op == GE)
5717       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
5718           || GET_CODE (x) == AND || GET_CODE (x) == IOR
5719           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
5720           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
5721           || GET_CODE (x) == LSHIFTRT
5722           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
5723           || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
5724     return CC_NOOVmode;
5725
5726   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
5727     return CC_Zmode;
5728
5729   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
5730       && GET_CODE (x) == PLUS
5731       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
5732     return CC_Cmode;
5733
5734   return CCmode;
5735 }
5736
5737 /* X and Y are two things to compare using CODE.  Emit the compare insn and
5738    return the rtx for register 0 in the proper mode.  FP means this is a
5739    floating point compare: I don't think that it is needed on the arm.  */
5740
5741 rtx
5742 arm_gen_compare_reg (code, x, y)
5743      enum rtx_code code;
5744      rtx x, y;
5745 {
5746   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
5747   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
5748
5749   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
5750                           gen_rtx_COMPARE (mode, x, y)));
5751
5752   return cc_reg;
5753 }
5754
5755 /* Generate a sequence of insns that will generate the correct return
5756    address mask depending on the physical architecture that the program
5757    is running on.  */
5758
5759 rtx
5760 arm_gen_return_addr_mask ()
5761 {
5762   rtx reg = gen_reg_rtx (Pmode);
5763
5764   emit_insn (gen_return_addr_mask (reg));
5765   return reg;
5766 }
5767
5768 void
5769 arm_reload_in_hi (operands)
5770      rtx * operands;
5771 {
5772   rtx ref = operands[1];
5773   rtx base, scratch;
5774   HOST_WIDE_INT offset = 0;
5775
5776   if (GET_CODE (ref) == SUBREG)
5777     {
5778       offset = SUBREG_BYTE (ref);
5779       ref = SUBREG_REG (ref);
5780     }
5781
5782   if (GET_CODE (ref) == REG)
5783     {
5784       /* We have a pseudo which has been spilt onto the stack; there
5785          are two cases here: the first where there is a simple
5786          stack-slot replacement and a second where the stack-slot is
5787          out of range, or is used as a subreg.  */
5788       if (reg_equiv_mem[REGNO (ref)])
5789         {
5790           ref = reg_equiv_mem[REGNO (ref)];
5791           base = find_replacement (&XEXP (ref, 0));
5792         }
5793       else
5794         /* The slot is out of range, or was dressed up in a SUBREG.  */
5795         base = reg_equiv_address[REGNO (ref)];
5796     }
5797   else
5798     base = find_replacement (&XEXP (ref, 0));
5799
5800   /* Handle the case where the address is too complex to be offset by 1.  */
5801   if (GET_CODE (base) == MINUS
5802       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
5803     {
5804       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5805
5806       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
5807       base = base_plus;
5808     }
5809   else if (GET_CODE (base) == PLUS)
5810     {
5811       /* The addend must be CONST_INT, or we would have dealt with it above.  */
5812       HOST_WIDE_INT hi, lo;
5813
5814       offset += INTVAL (XEXP (base, 1));
5815       base = XEXP (base, 0);
5816
5817       /* Rework the address into a legal sequence of insns.  */
5818       /* Valid range for lo is -4095 -> 4095 */
5819       lo = (offset >= 0
5820             ? (offset & 0xfff)
5821             : -((-offset) & 0xfff));
5822
5823       /* Corner case, if lo is the max offset then we would be out of range
5824          once we have added the additional 1 below, so bump the msb into the
5825          pre-loading insn(s).  */
5826       if (lo == 4095)
5827         lo &= 0x7ff;
5828
5829       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
5830              ^ (HOST_WIDE_INT) 0x80000000)
5831             - (HOST_WIDE_INT) 0x80000000);
5832
5833       if (hi + lo != offset)
5834         abort ();
5835
5836       if (hi != 0)
5837         {
5838           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5839
5840           /* Get the base address; addsi3 knows how to handle constants
5841              that require more than one insn.  */
5842           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
5843           base = base_plus;
5844           offset = lo;
5845         }
5846     }
5847
5848   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
5849   emit_insn (gen_zero_extendqisi2 (scratch,
5850                                    gen_rtx_MEM (QImode,
5851                                                 plus_constant (base,
5852                                                                offset))));
5853   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
5854                                    gen_rtx_MEM (QImode, 
5855                                                 plus_constant (base,
5856                                                                offset + 1))));
5857   if (!BYTES_BIG_ENDIAN)
5858     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
5859                         gen_rtx_IOR (SImode, 
5860                                      gen_rtx_ASHIFT
5861                                      (SImode,
5862                                       gen_rtx_SUBREG (SImode, operands[0], 0),
5863                                       GEN_INT (8)),
5864                                      scratch)));
5865   else
5866     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
5867                             gen_rtx_IOR (SImode, 
5868                                          gen_rtx_ASHIFT (SImode, scratch,
5869                                                          GEN_INT (8)),
5870                                          gen_rtx_SUBREG (SImode, operands[0],
5871                                                          0))));
5872 }
5873
5874 /* Handle storing a half-word to memory during reload by synthesising as two
5875    byte stores.  Take care not to clobber the input values until after we
5876    have moved them somewhere safe.  This code assumes that if the DImode
5877    scratch in operands[2] overlaps either the input value or output address
5878    in some way, then that value must die in this insn (we absolutely need
5879    two scratch registers for some corner cases).  */
5880
5881 void
5882 arm_reload_out_hi (operands)
5883      rtx * operands;
5884 {
5885   rtx ref = operands[0];
5886   rtx outval = operands[1];
5887   rtx base, scratch;
5888   HOST_WIDE_INT offset = 0;
5889
5890   if (GET_CODE (ref) == SUBREG)
5891     {
5892       offset = SUBREG_BYTE (ref);
5893       ref = SUBREG_REG (ref);
5894     }
5895
5896   if (GET_CODE (ref) == REG)
5897     {
5898       /* We have a pseudo which has been spilt onto the stack; there
5899          are two cases here: the first where there is a simple
5900          stack-slot replacement and a second where the stack-slot is
5901          out of range, or is used as a subreg.  */
5902       if (reg_equiv_mem[REGNO (ref)])
5903         {
5904           ref = reg_equiv_mem[REGNO (ref)];
5905           base = find_replacement (&XEXP (ref, 0));
5906         }
5907       else
5908         /* The slot is out of range, or was dressed up in a SUBREG.  */
5909         base = reg_equiv_address[REGNO (ref)];
5910     }
5911   else
5912     base = find_replacement (&XEXP (ref, 0));
5913
5914   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
5915
5916   /* Handle the case where the address is too complex to be offset by 1.  */
5917   if (GET_CODE (base) == MINUS
5918       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
5919     {
5920       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5921
5922       /* Be careful not to destroy OUTVAL.  */
5923       if (reg_overlap_mentioned_p (base_plus, outval))
5924         {
5925           /* Updating base_plus might destroy outval, see if we can
5926              swap the scratch and base_plus.  */
5927           if (!reg_overlap_mentioned_p (scratch, outval))
5928             {
5929               rtx tmp = scratch;
5930               scratch = base_plus;
5931               base_plus = tmp;
5932             }
5933           else
5934             {
5935               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5936
5937               /* Be conservative and copy OUTVAL into the scratch now,
5938                  this should only be necessary if outval is a subreg
5939                  of something larger than a word.  */
5940               /* XXX Might this clobber base?  I can't see how it can,
5941                  since scratch is known to overlap with OUTVAL, and
5942                  must be wider than a word.  */
5943               emit_insn (gen_movhi (scratch_hi, outval));
5944               outval = scratch_hi;
5945             }
5946         }
5947
5948       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
5949       base = base_plus;
5950     }
5951   else if (GET_CODE (base) == PLUS)
5952     {
5953       /* The addend must be CONST_INT, or we would have dealt with it above.  */
5954       HOST_WIDE_INT hi, lo;
5955
5956       offset += INTVAL (XEXP (base, 1));
5957       base = XEXP (base, 0);
5958
5959       /* Rework the address into a legal sequence of insns.  */
5960       /* Valid range for lo is -4095 -> 4095 */
5961       lo = (offset >= 0
5962             ? (offset & 0xfff)
5963             : -((-offset) & 0xfff));
5964
5965       /* Corner case, if lo is the max offset then we would be out of range
5966          once we have added the additional 1 below, so bump the msb into the
5967          pre-loading insn(s).  */
5968       if (lo == 4095)
5969         lo &= 0x7ff;
5970
5971       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
5972              ^ (HOST_WIDE_INT) 0x80000000)
5973             - (HOST_WIDE_INT) 0x80000000);
5974
5975       if (hi + lo != offset)
5976         abort ();
5977
5978       if (hi != 0)
5979         {
5980           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
5981
5982           /* Be careful not to destroy OUTVAL.  */
5983           if (reg_overlap_mentioned_p (base_plus, outval))
5984             {
5985               /* Updating base_plus might destroy outval, see if we
5986                  can swap the scratch and base_plus.  */
5987               if (!reg_overlap_mentioned_p (scratch, outval))
5988                 {
5989                   rtx tmp = scratch;
5990                   scratch = base_plus;
5991                   base_plus = tmp;
5992                 }
5993               else
5994                 {
5995                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
5996
5997                   /* Be conservative and copy outval into scratch now,
5998                      this should only be necessary if outval is a
5999                      subreg of something larger than a word.  */
6000                   /* XXX Might this clobber base?  I can't see how it
6001                      can, since scratch is known to overlap with
6002                      outval.  */
6003                   emit_insn (gen_movhi (scratch_hi, outval));
6004                   outval = scratch_hi;
6005                 }
6006             }
6007
6008           /* Get the base address; addsi3 knows how to handle constants
6009              that require more than one insn.  */
6010           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6011           base = base_plus;
6012           offset = lo;
6013         }
6014     }
6015
6016   if (BYTES_BIG_ENDIAN)
6017     {
6018       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
6019                                          plus_constant (base, offset + 1)),
6020                             gen_lowpart (QImode, outval)));
6021       emit_insn (gen_lshrsi3 (scratch,
6022                               gen_rtx_SUBREG (SImode, outval, 0),
6023                               GEN_INT (8)));
6024       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6025                             gen_lowpart (QImode, scratch)));
6026     }
6027   else
6028     {
6029       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6030                             gen_lowpart (QImode, outval)));
6031       emit_insn (gen_lshrsi3 (scratch,
6032                               gen_rtx_SUBREG (SImode, outval, 0),
6033                               GEN_INT (8)));
6034       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6035                                          plus_constant (base, offset + 1)),
6036                             gen_lowpart (QImode, scratch)));
6037     }
6038 }
6039 \f
6040 /* Print a symbolic form of X to the debug file, F.  */
6041
6042 static void
6043 arm_print_value (f, x)
6044      FILE * f;
6045      rtx x;
6046 {
6047   switch (GET_CODE (x))
6048     {
6049     case CONST_INT:
6050       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6051       return;
6052
6053     case CONST_DOUBLE:
6054       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6055       return;
6056
6057     case CONST_STRING:
6058       fprintf (f, "\"%s\"", XSTR (x, 0));
6059       return;
6060
6061     case SYMBOL_REF:
6062       fprintf (f, "`%s'", XSTR (x, 0));
6063       return;
6064
6065     case LABEL_REF:
6066       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6067       return;
6068
6069     case CONST:
6070       arm_print_value (f, XEXP (x, 0));
6071       return;
6072
6073     case PLUS:
6074       arm_print_value (f, XEXP (x, 0));
6075       fprintf (f, "+");
6076       arm_print_value (f, XEXP (x, 1));
6077       return;
6078
6079     case PC:
6080       fprintf (f, "pc");
6081       return;
6082
6083     default:
6084       fprintf (f, "????");
6085       return;
6086     }
6087 }
6088 \f
6089 /* Routines for manipulation of the constant pool.  */
6090
6091 /* Arm instructions cannot load a large constant directly into a
6092    register; they have to come from a pc relative load.  The constant
6093    must therefore be placed in the addressable range of the pc
6094    relative load.  Depending on the precise pc relative load
6095    instruction the range is somewhere between 256 bytes and 4k.  This
6096    means that we often have to dump a constant inside a function, and
6097    generate code to branch around it.
6098
6099    It is important to minimize this, since the branches will slow
6100    things down and make the code larger.
6101
6102    Normally we can hide the table after an existing unconditional
6103    branch so that there is no interruption of the flow, but in the
6104    worst case the code looks like this:
6105
6106         ldr     rn, L1
6107         ...
6108         b       L2
6109         align
6110         L1:     .long value
6111         L2:
6112         ...
6113
6114         ldr     rn, L3
6115         ...
6116         b       L4
6117         align
6118         L3:     .long value
6119         L4:
6120         ...
6121
6122    We fix this by performing a scan after scheduling, which notices
6123    which instructions need to have their operands fetched from the
6124    constant table and builds the table.
6125
6126    The algorithm starts by building a table of all the constants that
6127    need fixing up and all the natural barriers in the function (places
6128    where a constant table can be dropped without breaking the flow).
6129    For each fixup we note how far the pc-relative replacement will be
6130    able to reach and the offset of the instruction into the function.
6131
6132    Having built the table we then group the fixes together to form
6133    tables that are as large as possible (subject to addressing
6134    constraints) and emit each table of constants after the last
6135    barrier that is within range of all the instructions in the group.
6136    If a group does not contain a barrier, then we forcibly create one
6137    by inserting a jump instruction into the flow.  Once the table has
6138    been inserted, the insns are then modified to reference the
6139    relevant entry in the pool.
6140
6141    Possible enhancements to the algorithm (not implemented) are:
6142
6143    1) For some processors and object formats, there may be benefit in
6144    aligning the pools to the start of cache lines; this alignment
6145    would need to be taken into account when calculating addressability
6146    of a pool.  */
6147
6148 /* These typedefs are located at the start of this file, so that
6149    they can be used in the prototypes there.  This comment is to
6150    remind readers of that fact so that the following structures
6151    can be understood more easily.
6152
6153      typedef struct minipool_node    Mnode;
6154      typedef struct minipool_fixup   Mfix;  */
6155
6156 struct minipool_node
6157 {
6158   /* Doubly linked chain of entries.  */
6159   Mnode * next;
6160   Mnode * prev;
6161   /* The maximum offset into the code that this entry can be placed.  While
6162      pushing fixes for forward references, all entries are sorted in order
6163      of increasing max_address.  */
6164   HOST_WIDE_INT max_address;
6165   /* Similarly for an entry inserted for a backwards ref.  */
6166   HOST_WIDE_INT min_address;
6167   /* The number of fixes referencing this entry.  This can become zero
6168      if we "unpush" an entry.  In this case we ignore the entry when we
6169      come to emit the code.  */
6170   int refcount;
6171   /* The offset from the start of the minipool.  */
6172   HOST_WIDE_INT offset;
6173   /* The value in table.  */
6174   rtx value;
6175   /* The mode of value.  */
6176   enum machine_mode mode;
6177   int fix_size;
6178 };
6179
6180 struct minipool_fixup
6181 {
6182   Mfix *            next;
6183   rtx               insn;
6184   HOST_WIDE_INT     address;
6185   rtx *             loc;
6186   enum machine_mode mode;
6187   int               fix_size;
6188   rtx               value;
6189   Mnode *           minipool;
6190   HOST_WIDE_INT     forwards;
6191   HOST_WIDE_INT     backwards;
6192 };
6193
6194 /* Fixes less than a word need padding out to a word boundary.  */
6195 #define MINIPOOL_FIX_SIZE(mode) \
6196   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6197
6198 static Mnode *  minipool_vector_head;
6199 static Mnode *  minipool_vector_tail;
6200 static rtx      minipool_vector_label;
6201
6202 /* The linked list of all minipool fixes required for this function.  */
6203 Mfix *          minipool_fix_head;
6204 Mfix *          minipool_fix_tail;
6205 /* The fix entry for the current minipool, once it has been placed.  */
6206 Mfix *          minipool_barrier;
6207
6208 /* Determines if INSN is the start of a jump table.  Returns the end
6209    of the TABLE or NULL_RTX.  */
6210
6211 static rtx
6212 is_jump_table (insn)
6213      rtx insn;
6214 {
6215   rtx table;
6216   
6217   if (GET_CODE (insn) == JUMP_INSN
6218       && JUMP_LABEL (insn) != NULL
6219       && ((table = next_real_insn (JUMP_LABEL (insn)))
6220           == next_real_insn (insn))
6221       && table != NULL
6222       && GET_CODE (table) == JUMP_INSN
6223       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6224           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6225     return table;
6226
6227   return NULL_RTX;
6228 }
6229
6230 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6231 #define JUMP_TABLES_IN_TEXT_SECTION 0
6232 #endif
6233
6234 static HOST_WIDE_INT
6235 get_jump_table_size (insn)
6236      rtx insn;
6237 {
6238   /* ADDR_VECs only take room if read-only data does into the text
6239      section.  */
6240   if (JUMP_TABLES_IN_TEXT_SECTION
6241 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6242       || 1
6243 #endif
6244       )
6245     {
6246       rtx body = PATTERN (insn);
6247       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6248
6249       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6250     }
6251
6252   return 0;
6253 }
6254
6255 /* Move a minipool fix MP from its current location to before MAX_MP.
6256    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6257    contrains may need updating.  */
6258
6259 static Mnode *
6260 move_minipool_fix_forward_ref (mp, max_mp, max_address)
6261      Mnode *       mp;
6262      Mnode *       max_mp;
6263      HOST_WIDE_INT max_address;
6264 {
6265   /* This should never be true and the code below assumes these are
6266      different.  */
6267   if (mp == max_mp)
6268     abort ();
6269
6270   if (max_mp == NULL)
6271     {
6272       if (max_address < mp->max_address)
6273         mp->max_address = max_address;
6274     }
6275   else
6276     {
6277       if (max_address > max_mp->max_address - mp->fix_size)
6278         mp->max_address = max_mp->max_address - mp->fix_size;
6279       else
6280         mp->max_address = max_address;
6281
6282       /* Unlink MP from its current position.  Since max_mp is non-null,
6283        mp->prev must be non-null.  */
6284       mp->prev->next = mp->next;
6285       if (mp->next != NULL)
6286         mp->next->prev = mp->prev;
6287       else
6288         minipool_vector_tail = mp->prev;
6289
6290       /* Re-insert it before MAX_MP.  */
6291       mp->next = max_mp;
6292       mp->prev = max_mp->prev;
6293       max_mp->prev = mp;
6294       
6295       if (mp->prev != NULL)
6296         mp->prev->next = mp;
6297       else
6298         minipool_vector_head = mp;
6299     }
6300
6301   /* Save the new entry.  */
6302   max_mp = mp;
6303
6304   /* Scan over the preceding entries and adjust their addresses as
6305      required.  */
6306   while (mp->prev != NULL
6307          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6308     {
6309       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6310       mp = mp->prev;
6311     }
6312
6313   return max_mp;
6314 }
6315
6316 /* Add a constant to the minipool for a forward reference.  Returns the
6317    node added or NULL if the constant will not fit in this pool.  */
6318
6319 static Mnode *
6320 add_minipool_forward_ref (fix)
6321      Mfix * fix;
6322 {
6323   /* If set, max_mp is the first pool_entry that has a lower
6324      constraint than the one we are trying to add.  */
6325   Mnode *       max_mp = NULL;
6326   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6327   Mnode *       mp;
6328   
6329   /* If this fix's address is greater than the address of the first
6330      entry, then we can't put the fix in this pool.  We subtract the
6331      size of the current fix to ensure that if the table is fully
6332      packed we still have enough room to insert this value by suffling
6333      the other fixes forwards.  */
6334   if (minipool_vector_head &&
6335       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6336     return NULL;
6337
6338   /* Scan the pool to see if a constant with the same value has
6339      already been added.  While we are doing this, also note the
6340      location where we must insert the constant if it doesn't already
6341      exist.  */
6342   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6343     {
6344       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6345           && fix->mode == mp->mode
6346           && (GET_CODE (fix->value) != CODE_LABEL
6347               || (CODE_LABEL_NUMBER (fix->value)
6348                   == CODE_LABEL_NUMBER (mp->value)))
6349           && rtx_equal_p (fix->value, mp->value))
6350         {
6351           /* More than one fix references this entry.  */
6352           mp->refcount++;
6353           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6354         }
6355
6356       /* Note the insertion point if necessary.  */
6357       if (max_mp == NULL
6358           && mp->max_address > max_address)
6359         max_mp = mp;
6360     }
6361
6362   /* The value is not currently in the minipool, so we need to create
6363      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6364      the end of the list since the placement is less constrained than
6365      any existing entry.  Otherwise, we insert the new fix before
6366      MAX_MP and, if necessary, adjust the constraints on the other
6367      entries.  */
6368   mp = xmalloc (sizeof (* mp));
6369   mp->fix_size = fix->fix_size;
6370   mp->mode = fix->mode;
6371   mp->value = fix->value;
6372   mp->refcount = 1;
6373   /* Not yet required for a backwards ref.  */
6374   mp->min_address = -65536;
6375
6376   if (max_mp == NULL)
6377     {
6378       mp->max_address = max_address;
6379       mp->next = NULL;
6380       mp->prev = minipool_vector_tail;
6381
6382       if (mp->prev == NULL)
6383         {
6384           minipool_vector_head = mp;
6385           minipool_vector_label = gen_label_rtx ();
6386         }
6387       else
6388         mp->prev->next = mp;
6389
6390       minipool_vector_tail = mp;
6391     }
6392   else
6393     {
6394       if (max_address > max_mp->max_address - mp->fix_size)
6395         mp->max_address = max_mp->max_address - mp->fix_size;
6396       else
6397         mp->max_address = max_address;
6398
6399       mp->next = max_mp;
6400       mp->prev = max_mp->prev;
6401       max_mp->prev = mp;
6402       if (mp->prev != NULL)
6403         mp->prev->next = mp;
6404       else
6405         minipool_vector_head = mp;
6406     }
6407
6408   /* Save the new entry.  */
6409   max_mp = mp;
6410
6411   /* Scan over the preceding entries and adjust their addresses as
6412      required.  */
6413   while (mp->prev != NULL
6414          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6415     {
6416       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6417       mp = mp->prev;
6418     }
6419
6420   return max_mp;
6421 }
6422
6423 static Mnode *
6424 move_minipool_fix_backward_ref (mp, min_mp, min_address)
6425      Mnode *        mp;
6426      Mnode *        min_mp;
6427      HOST_WIDE_INT  min_address;
6428 {
6429   HOST_WIDE_INT offset;
6430
6431   /* This should never be true, and the code below assumes these are
6432      different.  */
6433   if (mp == min_mp)
6434     abort ();
6435
6436   if (min_mp == NULL)
6437     {
6438       if (min_address > mp->min_address)
6439         mp->min_address = min_address;
6440     }
6441   else
6442     {
6443       /* We will adjust this below if it is too loose.  */
6444       mp->min_address = min_address;
6445
6446       /* Unlink MP from its current position.  Since min_mp is non-null,
6447          mp->next must be non-null.  */
6448       mp->next->prev = mp->prev;
6449       if (mp->prev != NULL)
6450         mp->prev->next = mp->next;
6451       else
6452         minipool_vector_head = mp->next;
6453
6454       /* Reinsert it after MIN_MP.  */
6455       mp->prev = min_mp;
6456       mp->next = min_mp->next;
6457       min_mp->next = mp;
6458       if (mp->next != NULL)
6459         mp->next->prev = mp;
6460       else
6461         minipool_vector_tail = mp;
6462     }
6463
6464   min_mp = mp;
6465
6466   offset = 0;
6467   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6468     {
6469       mp->offset = offset;
6470       if (mp->refcount > 0)
6471         offset += mp->fix_size;
6472
6473       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6474         mp->next->min_address = mp->min_address + mp->fix_size;
6475     }
6476
6477   return min_mp;
6478 }      
6479
6480 /* Add a constant to the minipool for a backward reference.  Returns the
6481    node added or NULL if the constant will not fit in this pool.  
6482
6483    Note that the code for insertion for a backwards reference can be
6484    somewhat confusing because the calculated offsets for each fix do
6485    not take into account the size of the pool (which is still under
6486    construction.  */
6487
6488 static Mnode *
6489 add_minipool_backward_ref (fix)
6490      Mfix * fix;
6491 {
6492   /* If set, min_mp is the last pool_entry that has a lower constraint
6493      than the one we are trying to add.  */
6494   Mnode *        min_mp = NULL;
6495   /* This can be negative, since it is only a constraint.  */
6496   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
6497   Mnode *        mp;
6498
6499   /* If we can't reach the current pool from this insn, or if we can't
6500      insert this entry at the end of the pool without pushing other
6501      fixes out of range, then we don't try.  This ensures that we
6502      can't fail later on.  */
6503   if (min_address >= minipool_barrier->address
6504       || (minipool_vector_tail->min_address + fix->fix_size
6505           >= minipool_barrier->address))
6506     return NULL;
6507
6508   /* Scan the pool to see if a constant with the same value has
6509      already been added.  While we are doing this, also note the
6510      location where we must insert the constant if it doesn't already
6511      exist.  */
6512   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6513     {
6514       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6515           && fix->mode == mp->mode
6516           && (GET_CODE (fix->value) != CODE_LABEL
6517               || (CODE_LABEL_NUMBER (fix->value)
6518                   == CODE_LABEL_NUMBER (mp->value)))
6519           && rtx_equal_p (fix->value, mp->value)
6520           /* Check that there is enough slack to move this entry to the
6521              end of the table (this is conservative).  */
6522           && (mp->max_address 
6523               > (minipool_barrier->address 
6524                  + minipool_vector_tail->offset
6525                  + minipool_vector_tail->fix_size)))
6526         {
6527           mp->refcount++;
6528           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6529         }
6530
6531       if (min_mp != NULL)
6532         mp->min_address += fix->fix_size;
6533       else
6534         {
6535           /* Note the insertion point if necessary.  */
6536           if (mp->min_address < min_address)
6537             min_mp = mp;
6538           else if (mp->max_address
6539                    < minipool_barrier->address + mp->offset + fix->fix_size)
6540             {
6541               /* Inserting before this entry would push the fix beyond
6542                  its maximum address (which can happen if we have
6543                  re-located a forwards fix); force the new fix to come
6544                  after it.  */
6545               min_mp = mp;
6546               min_address = mp->min_address + fix->fix_size;
6547             }
6548         }
6549     }
6550
6551   /* We need to create a new entry.  */
6552   mp = xmalloc (sizeof (* mp));
6553   mp->fix_size = fix->fix_size;
6554   mp->mode = fix->mode;
6555   mp->value = fix->value;
6556   mp->refcount = 1;
6557   mp->max_address = minipool_barrier->address + 65536;
6558
6559   mp->min_address = min_address;
6560
6561   if (min_mp == NULL)
6562     {
6563       mp->prev = NULL;
6564       mp->next = minipool_vector_head;
6565
6566       if (mp->next == NULL)
6567         {
6568           minipool_vector_tail = mp;
6569           minipool_vector_label = gen_label_rtx ();
6570         }
6571       else
6572         mp->next->prev = mp;
6573
6574       minipool_vector_head = mp;
6575     }
6576   else
6577     {
6578       mp->next = min_mp->next;
6579       mp->prev = min_mp;
6580       min_mp->next = mp;
6581       
6582       if (mp->next != NULL)
6583         mp->next->prev = mp;
6584       else
6585         minipool_vector_tail = mp;
6586     }
6587
6588   /* Save the new entry.  */
6589   min_mp = mp;
6590
6591   if (mp->prev)
6592     mp = mp->prev;
6593   else
6594     mp->offset = 0;
6595
6596   /* Scan over the following entries and adjust their offsets.  */
6597   while (mp->next != NULL)
6598     {
6599       if (mp->next->min_address < mp->min_address + mp->fix_size)
6600         mp->next->min_address = mp->min_address + mp->fix_size;
6601
6602       if (mp->refcount)
6603         mp->next->offset = mp->offset + mp->fix_size;
6604       else
6605         mp->next->offset = mp->offset;
6606
6607       mp = mp->next;
6608     }
6609
6610   return min_mp;
6611 }
6612
6613 static void
6614 assign_minipool_offsets (barrier)
6615      Mfix * barrier;
6616 {
6617   HOST_WIDE_INT offset = 0;
6618   Mnode * mp;
6619
6620   minipool_barrier = barrier;
6621
6622   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6623     {
6624       mp->offset = offset;
6625       
6626       if (mp->refcount > 0)
6627         offset += mp->fix_size;
6628     }
6629 }
6630
6631 /* Output the literal table */
6632 static void
6633 dump_minipool (scan)
6634      rtx scan;
6635 {
6636   Mnode * mp;
6637   Mnode * nmp;
6638
6639   if (rtl_dump_file)
6640     fprintf (rtl_dump_file,
6641              ";; Emitting minipool after insn %u; address %ld\n",
6642              INSN_UID (scan), (unsigned long) minipool_barrier->address);
6643
6644   scan = emit_label_after (gen_label_rtx (), scan);
6645   scan = emit_insn_after (gen_align_4 (), scan);
6646   scan = emit_label_after (minipool_vector_label, scan);
6647
6648   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
6649     {
6650       if (mp->refcount > 0)
6651         {
6652           if (rtl_dump_file)
6653             {
6654               fprintf (rtl_dump_file, 
6655                        ";;  Offset %u, min %ld, max %ld ",
6656                        (unsigned) mp->offset, (unsigned long) mp->min_address,
6657                        (unsigned long) mp->max_address);
6658               arm_print_value (rtl_dump_file, mp->value);
6659               fputc ('\n', rtl_dump_file);
6660             }
6661
6662           switch (mp->fix_size)
6663             {
6664 #ifdef HAVE_consttable_1
6665             case 1:
6666               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
6667               break;
6668
6669 #endif
6670 #ifdef HAVE_consttable_2
6671             case 2:
6672               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
6673               break;
6674
6675 #endif
6676 #ifdef HAVE_consttable_4
6677             case 4:
6678               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
6679               break;
6680
6681 #endif
6682 #ifdef HAVE_consttable_8
6683             case 8:
6684               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
6685               break;
6686
6687 #endif
6688             default:
6689               abort ();
6690               break;
6691             }
6692         }
6693
6694       nmp = mp->next;
6695       free (mp);
6696     }
6697
6698   minipool_vector_head = minipool_vector_tail = NULL;
6699   scan = emit_insn_after (gen_consttable_end (), scan);
6700   scan = emit_barrier_after (scan);
6701 }
6702
6703 /* Return the cost of forcibly inserting a barrier after INSN.  */
6704
6705 static int
6706 arm_barrier_cost (insn)
6707      rtx insn;
6708 {
6709   /* Basing the location of the pool on the loop depth is preferable,
6710      but at the moment, the basic block information seems to be
6711      corrupt by this stage of the compilation.  */
6712   int base_cost = 50;
6713   rtx next = next_nonnote_insn (insn);
6714
6715   if (next != NULL && GET_CODE (next) == CODE_LABEL)
6716     base_cost -= 20;
6717
6718   switch (GET_CODE (insn))
6719     {
6720     case CODE_LABEL:
6721       /* It will always be better to place the table before the label, rather
6722          than after it.  */
6723       return 50;  
6724
6725     case INSN:
6726     case CALL_INSN:
6727       return base_cost;
6728
6729     case JUMP_INSN:
6730       return base_cost - 10;
6731
6732     default:
6733       return base_cost + 10;
6734     }
6735 }
6736
6737 /* Find the best place in the insn stream in the range
6738    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
6739    Create the barrier by inserting a jump and add a new fix entry for
6740    it.  */
6741
6742 static Mfix *
6743 create_fix_barrier (fix, max_address)
6744      Mfix * fix;
6745      HOST_WIDE_INT max_address;
6746 {
6747   HOST_WIDE_INT count = 0;
6748   rtx barrier;
6749   rtx from = fix->insn;
6750   rtx selected = from;
6751   int selected_cost;
6752   HOST_WIDE_INT selected_address;
6753   Mfix * new_fix;
6754   HOST_WIDE_INT max_count = max_address - fix->address;
6755   rtx label = gen_label_rtx ();
6756
6757   selected_cost = arm_barrier_cost (from);
6758   selected_address = fix->address;
6759
6760   while (from && count < max_count)
6761     {
6762       rtx tmp;
6763       int new_cost;
6764
6765       /* This code shouldn't have been called if there was a natural barrier
6766          within range.  */
6767       if (GET_CODE (from) == BARRIER)
6768         abort ();
6769
6770       /* Count the length of this insn.  */
6771       count += get_attr_length (from);
6772
6773       /* If there is a jump table, add its length.  */
6774       tmp = is_jump_table (from);
6775       if (tmp != NULL)
6776         {
6777           count += get_jump_table_size (tmp);
6778
6779           /* Jump tables aren't in a basic block, so base the cost on
6780              the dispatch insn.  If we select this location, we will
6781              still put the pool after the table.  */
6782           new_cost = arm_barrier_cost (from);
6783
6784           if (count < max_count && new_cost <= selected_cost)
6785             {
6786               selected = tmp;
6787               selected_cost = new_cost;
6788               selected_address = fix->address + count;
6789             }
6790
6791           /* Continue after the dispatch table.  */
6792           from = NEXT_INSN (tmp);
6793           continue;
6794         }
6795
6796       new_cost = arm_barrier_cost (from);
6797       
6798       if (count < max_count && new_cost <= selected_cost)
6799         {
6800           selected = from;
6801           selected_cost = new_cost;
6802           selected_address = fix->address + count;
6803         }
6804
6805       from = NEXT_INSN (from);
6806     }
6807
6808   /* Create a new JUMP_INSN that branches around a barrier.  */
6809   from = emit_jump_insn_after (gen_jump (label), selected);
6810   JUMP_LABEL (from) = label;
6811   barrier = emit_barrier_after (from);
6812   emit_label_after (label, barrier);
6813
6814   /* Create a minipool barrier entry for the new barrier.  */
6815   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
6816   new_fix->insn = barrier;
6817   new_fix->address = selected_address;
6818   new_fix->next = fix->next;
6819   fix->next = new_fix;
6820
6821   return new_fix;
6822 }
6823
6824 /* Record that there is a natural barrier in the insn stream at
6825    ADDRESS.  */
6826 static void
6827 push_minipool_barrier (insn, address)
6828      rtx insn;
6829      HOST_WIDE_INT address;
6830 {
6831   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
6832
6833   fix->insn = insn;
6834   fix->address = address;
6835
6836   fix->next = NULL;
6837   if (minipool_fix_head != NULL)
6838     minipool_fix_tail->next = fix;
6839   else
6840     minipool_fix_head = fix;
6841
6842   minipool_fix_tail = fix;
6843 }
6844
6845 /* Record INSN, which will need fixing up to load a value from the
6846    minipool.  ADDRESS is the offset of the insn since the start of the
6847    function; LOC is a pointer to the part of the insn which requires
6848    fixing; VALUE is the constant that must be loaded, which is of type
6849    MODE.  */
6850 static void
6851 push_minipool_fix (insn, address, loc, mode, value)
6852      rtx insn;
6853      HOST_WIDE_INT address;
6854      rtx * loc;
6855      enum machine_mode mode;
6856      rtx value;
6857 {
6858   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
6859
6860 #ifdef AOF_ASSEMBLER
6861   /* PIC symbol refereneces need to be converted into offsets into the
6862      based area.  */
6863   /* XXX This shouldn't be done here.  */
6864   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
6865     value = aof_pic_entry (value);
6866 #endif /* AOF_ASSEMBLER */
6867
6868   fix->insn = insn;
6869   fix->address = address;
6870   fix->loc = loc;
6871   fix->mode = mode;
6872   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
6873   fix->value = value;
6874   fix->forwards = get_attr_pool_range (insn);
6875   fix->backwards = get_attr_neg_pool_range (insn);
6876   fix->minipool = NULL;
6877
6878   /* If an insn doesn't have a range defined for it, then it isn't
6879      expecting to be reworked by this code.  Better to abort now than
6880      to generate duff assembly code.  */
6881   if (fix->forwards == 0 && fix->backwards == 0)
6882     abort ();
6883
6884   if (rtl_dump_file)
6885     {
6886       fprintf (rtl_dump_file,
6887                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
6888                GET_MODE_NAME (mode),
6889                INSN_UID (insn), (unsigned long) address, 
6890                -1 * (long)fix->backwards, (long)fix->forwards);
6891       arm_print_value (rtl_dump_file, fix->value);
6892       fprintf (rtl_dump_file, "\n");
6893     }
6894
6895   /* Add it to the chain of fixes.  */
6896   fix->next = NULL;
6897   
6898   if (minipool_fix_head != NULL)
6899     minipool_fix_tail->next = fix;
6900   else
6901     minipool_fix_head = fix;
6902
6903   minipool_fix_tail = fix;
6904 }
6905
6906 /* Scan INSN and note any of its operands that need fixing.  */
6907
6908 static void
6909 note_invalid_constants (insn, address)
6910      rtx insn;
6911      HOST_WIDE_INT address;
6912 {
6913   int opno;
6914
6915   extract_insn (insn);
6916
6917   if (!constrain_operands (1))
6918     fatal_insn_not_found (insn);
6919
6920   /* Fill in recog_op_alt with information about the constraints of this
6921      insn.  */
6922   preprocess_constraints ();
6923
6924   for (opno = 0; opno < recog_data.n_operands; opno++)
6925     {
6926       /* Things we need to fix can only occur in inputs.  */
6927       if (recog_data.operand_type[opno] != OP_IN)
6928         continue;
6929
6930       /* If this alternative is a memory reference, then any mention
6931          of constants in this alternative is really to fool reload
6932          into allowing us to accept one there.  We need to fix them up
6933          now so that we output the right code.  */
6934       if (recog_op_alt[opno][which_alternative].memory_ok)
6935         {
6936           rtx op = recog_data.operand[opno];
6937
6938           if (CONSTANT_P (op))
6939             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6940                                recog_data.operand_mode[opno], op);
6941 #if 0
6942           /* RWE: Now we look correctly at the operands for the insn,
6943              this shouldn't be needed any more.  */
6944 #ifndef AOF_ASSEMBLER
6945           /* XXX Is this still needed?  */
6946           else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == UNSPEC_PIC_SYM)
6947             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6948                                recog_data.operand_mode[opno],
6949                                XVECEXP (op, 0, 0));
6950 #endif
6951 #endif
6952           else if (GET_CODE (op) == MEM
6953                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
6954                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
6955             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
6956                                recog_data.operand_mode[opno],
6957                                get_pool_constant (XEXP (op, 0)));
6958         }
6959     }
6960 }
6961
6962 void
6963 arm_reorg (first)
6964      rtx first;
6965 {
6966   rtx insn;
6967   HOST_WIDE_INT address = 0;
6968   Mfix * fix;
6969
6970   minipool_fix_head = minipool_fix_tail = NULL;
6971
6972   /* The first insn must always be a note, or the code below won't
6973      scan it properly.  */
6974   if (GET_CODE (first) != NOTE)
6975     abort ();
6976
6977   /* Scan all the insns and record the operands that will need fixing.  */
6978   for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
6979     {
6980       if (TARGET_CIRRUS_FIX_INVALID_INSNS
6981           && (is_cirrus_insn (insn)
6982               || GET_CODE (insn) == JUMP_INSN
6983               || is_load_address (insn)))
6984         cirrus_reorg (insn);
6985
6986       if (GET_CODE (insn) == BARRIER)
6987         push_minipool_barrier (insn, address);
6988       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
6989                || GET_CODE (insn) == JUMP_INSN)
6990         {
6991           rtx table;
6992
6993           note_invalid_constants (insn, address);
6994           address += get_attr_length (insn);
6995
6996           /* If the insn is a vector jump, add the size of the table
6997              and skip the table.  */
6998           if ((table = is_jump_table (insn)) != NULL)
6999             {
7000               address += get_jump_table_size (table);
7001               insn = table;
7002             }
7003         }
7004     }
7005
7006   fix = minipool_fix_head;
7007   
7008   /* Now scan the fixups and perform the required changes.  */
7009   while (fix)
7010     {
7011       Mfix * ftmp;
7012       Mfix * fdel;
7013       Mfix *  last_added_fix;
7014       Mfix * last_barrier = NULL;
7015       Mfix * this_fix;
7016
7017       /* Skip any further barriers before the next fix.  */
7018       while (fix && GET_CODE (fix->insn) == BARRIER)
7019         fix = fix->next;
7020
7021       /* No more fixes.  */
7022       if (fix == NULL)
7023         break;
7024
7025       last_added_fix = NULL;
7026
7027       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7028         {
7029           if (GET_CODE (ftmp->insn) == BARRIER)
7030             {
7031               if (ftmp->address >= minipool_vector_head->max_address)
7032                 break;
7033
7034               last_barrier = ftmp;
7035             }
7036           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7037             break;
7038
7039           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7040         }
7041
7042       /* If we found a barrier, drop back to that; any fixes that we
7043          could have reached but come after the barrier will now go in
7044          the next mini-pool.  */
7045       if (last_barrier != NULL)
7046         {
7047           /* Reduce the refcount for those fixes that won't go into this 
7048              pool after all.  */
7049           for (fdel = last_barrier->next;
7050                fdel && fdel != ftmp;
7051                fdel = fdel->next)
7052             {
7053               fdel->minipool->refcount--;
7054               fdel->minipool = NULL;
7055             }
7056
7057           ftmp = last_barrier;
7058         }
7059       else
7060         {
7061           /* ftmp is first fix that we can't fit into this pool and
7062              there no natural barriers that we could use.  Insert a
7063              new barrier in the code somewhere between the previous
7064              fix and this one, and arrange to jump around it.  */
7065           HOST_WIDE_INT max_address;
7066
7067           /* The last item on the list of fixes must be a barrier, so
7068              we can never run off the end of the list of fixes without
7069              last_barrier being set.  */
7070           if (ftmp == NULL)
7071             abort ();
7072
7073           max_address = minipool_vector_head->max_address;
7074           /* Check that there isn't another fix that is in range that
7075              we couldn't fit into this pool because the pool was
7076              already too large: we need to put the pool before such an
7077              instruction.  */
7078           if (ftmp->address < max_address)
7079             max_address = ftmp->address;
7080
7081           last_barrier = create_fix_barrier (last_added_fix, max_address);
7082         }
7083
7084       assign_minipool_offsets (last_barrier);
7085
7086       while (ftmp)
7087         {
7088           if (GET_CODE (ftmp->insn) != BARRIER
7089               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7090                   == NULL))
7091             break;
7092
7093           ftmp = ftmp->next;
7094         }
7095
7096       /* Scan over the fixes we have identified for this pool, fixing them
7097          up and adding the constants to the pool itself.  */
7098       for (this_fix = fix; this_fix && ftmp != this_fix;
7099            this_fix = this_fix->next)
7100         if (GET_CODE (this_fix->insn) != BARRIER)
7101           {
7102             rtx addr
7103               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
7104                                                   minipool_vector_label),
7105                                this_fix->minipool->offset);
7106             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7107           }
7108
7109       dump_minipool (last_barrier->insn);
7110       fix = ftmp;
7111     }
7112
7113   /* From now on we must synthesize any constants that we can't handle
7114      directly.  This can happen if the RTL gets split during final
7115      instruction generation.  */
7116   after_arm_reorg = 1;
7117
7118   /* Free the minipool memory.  */
7119   obstack_free (&minipool_obstack, minipool_startobj);
7120 }
7121 \f
7122 /* Routines to output assembly language.  */
7123
7124 /* If the rtx is the correct value then return the string of the number.
7125    In this way we can ensure that valid double constants are generated even
7126    when cross compiling.  */
7127
7128 const char *
7129 fp_immediate_constant (x)
7130      rtx x;
7131 {
7132   REAL_VALUE_TYPE r;
7133   int i;
7134   
7135   if (!fpa_consts_inited)
7136     init_fpa_table ();
7137   
7138   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7139   for (i = 0; i < 8; i++)
7140     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
7141       return strings_fpa[i];
7142
7143   abort ();
7144 }
7145
7146 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7147
7148 static const char *
7149 fp_const_from_val (r)
7150      REAL_VALUE_TYPE * r;
7151 {
7152   int i;
7153
7154   if (!fpa_consts_inited)
7155     init_fpa_table ();
7156
7157   for (i = 0; i < 8; i++)
7158     if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
7159       return strings_fpa[i];
7160
7161   abort ();
7162 }
7163
7164 /* Output the operands of a LDM/STM instruction to STREAM.
7165    MASK is the ARM register set mask of which only bits 0-15 are important.
7166    REG is the base register, either the frame pointer or the stack pointer,
7167    INSTR is the possibly suffixed load or store instruction.  */
7168
7169 static void
7170 print_multi_reg (stream, instr, reg, mask)
7171      FILE * stream;
7172      const char * instr;
7173      int reg;
7174      int mask;
7175 {
7176   int i;
7177   int not_first = FALSE;
7178
7179   fputc ('\t', stream);
7180   asm_fprintf (stream, instr, reg);
7181   fputs (", {", stream);
7182   
7183   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7184     if (mask & (1 << i))
7185       {
7186         if (not_first)
7187           fprintf (stream, ", ");
7188         
7189         asm_fprintf (stream, "%r", i);
7190         not_first = TRUE;
7191       }
7192
7193   fprintf (stream, "}%s\n", TARGET_APCS_32 ? "" : "^");
7194 }
7195
7196 /* Output a 'call' insn.  */
7197
7198 const char *
7199 output_call (operands)
7200      rtx * operands;
7201 {
7202   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
7203
7204   if (REGNO (operands[0]) == LR_REGNUM)
7205     {
7206       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7207       output_asm_insn ("mov%?\t%0, %|lr", operands);
7208     }
7209   
7210   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7211   
7212   if (TARGET_INTERWORK)
7213     output_asm_insn ("bx%?\t%0", operands);
7214   else
7215     output_asm_insn ("mov%?\t%|pc, %0", operands);
7216   
7217   return "";
7218 }
7219
7220 static int
7221 eliminate_lr2ip (x)
7222      rtx * x;
7223 {
7224   int something_changed = 0;
7225   rtx x0 = * x;
7226   int code = GET_CODE (x0);
7227   int i, j;
7228   const char * fmt;
7229   
7230   switch (code)
7231     {
7232     case REG:
7233       if (REGNO (x0) == LR_REGNUM)
7234         {
7235           *x = gen_rtx_REG (SImode, IP_REGNUM);
7236           return 1;
7237         }
7238       return 0;
7239     default:
7240       /* Scan through the sub-elements and change any references there.  */
7241       fmt = GET_RTX_FORMAT (code);
7242       
7243       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7244         if (fmt[i] == 'e')
7245           something_changed |= eliminate_lr2ip (&XEXP (x0, i));
7246         else if (fmt[i] == 'E')
7247           for (j = 0; j < XVECLEN (x0, i); j++)
7248             something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
7249       
7250       return something_changed;
7251     }
7252 }
7253   
7254 /* Output a 'call' insn that is a reference in memory.  */
7255
7256 const char *
7257 output_call_mem (operands)
7258      rtx * operands;
7259 {
7260   operands[0] = copy_rtx (operands[0]); /* Be ultra careful.  */
7261   /* Handle calls using lr by using ip (which may be clobbered in subr anyway).  */
7262   if (eliminate_lr2ip (&operands[0]))
7263     output_asm_insn ("mov%?\t%|ip, %|lr", operands);
7264
7265   if (TARGET_INTERWORK)
7266     {
7267       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7268       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7269       output_asm_insn ("bx%?\t%|ip", operands);
7270     }
7271   else
7272     {
7273       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7274       output_asm_insn ("ldr%?\t%|pc, %0", operands);
7275     }
7276
7277   return "";
7278 }
7279
7280
7281 /* Output a move from arm registers to an fpu registers.
7282    OPERANDS[0] is an fpu register.
7283    OPERANDS[1] is the first registers of an arm register pair.  */
7284
7285 const char *
7286 output_mov_long_double_fpu_from_arm (operands)
7287      rtx * operands;
7288 {
7289   int arm_reg0 = REGNO (operands[1]);
7290   rtx ops[3];
7291
7292   if (arm_reg0 == IP_REGNUM)
7293     abort ();
7294
7295   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7296   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7297   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7298   
7299   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7300   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7301   
7302   return "";
7303 }
7304
7305 /* Output a move from an fpu register to arm registers.
7306    OPERANDS[0] is the first registers of an arm register pair.
7307    OPERANDS[1] is an fpu register.  */
7308
7309 const char *
7310 output_mov_long_double_arm_from_fpu (operands)
7311      rtx * operands;
7312 {
7313   int arm_reg0 = REGNO (operands[0]);
7314   rtx ops[3];
7315
7316   if (arm_reg0 == IP_REGNUM)
7317     abort ();
7318
7319   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7320   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7321   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7322
7323   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
7324   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
7325   return "";
7326 }
7327
7328 /* Output a move from arm registers to arm registers of a long double
7329    OPERANDS[0] is the destination.
7330    OPERANDS[1] is the source.  */
7331
7332 const char *
7333 output_mov_long_double_arm_from_arm (operands)
7334      rtx * operands;
7335 {
7336   /* We have to be careful here because the two might overlap.  */
7337   int dest_start = REGNO (operands[0]);
7338   int src_start = REGNO (operands[1]);
7339   rtx ops[2];
7340   int i;
7341
7342   if (dest_start < src_start)
7343     {
7344       for (i = 0; i < 3; i++)
7345         {
7346           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7347           ops[1] = gen_rtx_REG (SImode, src_start + i);
7348           output_asm_insn ("mov%?\t%0, %1", ops);
7349         }
7350     }
7351   else
7352     {
7353       for (i = 2; i >= 0; i--)
7354         {
7355           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7356           ops[1] = gen_rtx_REG (SImode, src_start + i);
7357           output_asm_insn ("mov%?\t%0, %1", ops);
7358         }
7359     }
7360
7361   return "";
7362 }
7363
7364
7365 /* Output a move from arm registers to an fpu registers.
7366    OPERANDS[0] is an fpu register.
7367    OPERANDS[1] is the first registers of an arm register pair.  */
7368
7369 const char *
7370 output_mov_double_fpu_from_arm (operands)
7371      rtx * operands;
7372 {
7373   int arm_reg0 = REGNO (operands[1]);
7374   rtx ops[2];
7375
7376   if (arm_reg0 == IP_REGNUM)
7377     abort ();
7378   
7379   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7380   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7381   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
7382   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
7383   return "";
7384 }
7385
7386 /* Output a move from an fpu register to arm registers.
7387    OPERANDS[0] is the first registers of an arm register pair.
7388    OPERANDS[1] is an fpu register.  */
7389
7390 const char *
7391 output_mov_double_arm_from_fpu (operands)
7392      rtx * operands;
7393 {
7394   int arm_reg0 = REGNO (operands[0]);
7395   rtx ops[2];
7396
7397   if (arm_reg0 == IP_REGNUM)
7398     abort ();
7399
7400   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7401   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7402   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
7403   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
7404   return "";
7405 }
7406
7407 /* Output a move between double words.
7408    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
7409    or MEM<-REG and all MEMs must be offsettable addresses.  */
7410
7411 const char *
7412 output_move_double (operands)
7413      rtx * operands;
7414 {
7415   enum rtx_code code0 = GET_CODE (operands[0]);
7416   enum rtx_code code1 = GET_CODE (operands[1]);
7417   rtx otherops[3];
7418
7419   if (code0 == REG)
7420     {
7421       int reg0 = REGNO (operands[0]);
7422
7423       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
7424       
7425       if (code1 == REG)
7426         {
7427           int reg1 = REGNO (operands[1]);
7428           if (reg1 == IP_REGNUM)
7429             abort ();
7430
7431           /* Ensure the second source is not overwritten.  */
7432           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
7433             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
7434           else
7435             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
7436         }
7437       else if (code1 == CONST_DOUBLE)
7438         {
7439           if (GET_MODE (operands[1]) == DFmode)
7440             {
7441               REAL_VALUE_TYPE r;
7442               long l[2];
7443
7444               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
7445               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
7446               otherops[1] = GEN_INT (l[1]);
7447               operands[1] = GEN_INT (l[0]);
7448             }
7449           else if (GET_MODE (operands[1]) != VOIDmode)
7450             abort ();
7451           else if (WORDS_BIG_ENDIAN)
7452             {
7453               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7454               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7455             }
7456           else
7457             {
7458               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7459               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7460             }
7461           
7462           output_mov_immediate (operands);
7463           output_mov_immediate (otherops);
7464         }
7465       else if (code1 == CONST_INT)
7466         {
7467 #if HOST_BITS_PER_WIDE_INT > 32
7468           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
7469              what the upper word is.  */
7470           if (WORDS_BIG_ENDIAN)
7471             {
7472               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
7473               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
7474             }
7475           else
7476             {
7477               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
7478               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
7479             }
7480 #else
7481           /* Sign extend the intval into the high-order word.  */
7482           if (WORDS_BIG_ENDIAN)
7483             {
7484               otherops[1] = operands[1];
7485               operands[1] = (INTVAL (operands[1]) < 0
7486                              ? constm1_rtx : const0_rtx);
7487             }
7488           else
7489             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
7490 #endif
7491           output_mov_immediate (otherops);
7492           output_mov_immediate (operands);
7493         }
7494       else if (code1 == MEM)
7495         {
7496           switch (GET_CODE (XEXP (operands[1], 0)))
7497             {
7498             case REG:
7499               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
7500               break;
7501
7502             case PRE_INC:
7503               abort (); /* Should never happen now.  */
7504               break;
7505
7506             case PRE_DEC:
7507               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
7508               break;
7509
7510             case POST_INC:
7511               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
7512               break;
7513
7514             case POST_DEC:
7515               abort (); /* Should never happen now.  */
7516               break;
7517
7518             case LABEL_REF:
7519             case CONST:
7520               output_asm_insn ("adr%?\t%0, %1", operands);
7521               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
7522               break;
7523
7524             default:
7525               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
7526                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
7527                 {
7528                   otherops[0] = operands[0];
7529                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
7530                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
7531
7532                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
7533                     {
7534                       if (GET_CODE (otherops[2]) == CONST_INT)
7535                         {
7536                           switch ((int) INTVAL (otherops[2]))
7537                             {
7538                             case -8:
7539                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
7540                               return "";
7541                             case -4:
7542                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
7543                               return "";
7544                             case 4:
7545                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
7546                               return "";
7547                             }
7548
7549                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
7550                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
7551                           else
7552                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
7553                         }
7554                       else
7555                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
7556                     }
7557                   else
7558                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
7559                   
7560                   return "ldm%?ia\t%0, %M0";
7561                 }
7562               else
7563                 {
7564                   otherops[1] = adjust_address (operands[1], VOIDmode, 4);
7565                   /* Take care of overlapping base/data reg.  */
7566                   if (reg_mentioned_p (operands[0], operands[1]))
7567                     {
7568                       output_asm_insn ("ldr%?\t%0, %1", otherops);
7569                       output_asm_insn ("ldr%?\t%0, %1", operands);
7570                     }
7571                   else
7572                     {
7573                       output_asm_insn ("ldr%?\t%0, %1", operands);
7574                       output_asm_insn ("ldr%?\t%0, %1", otherops);
7575                     }
7576                 }
7577             }
7578         }
7579       else
7580         abort ();  /* Constraints should prevent this.  */
7581     }
7582   else if (code0 == MEM && code1 == REG)
7583     {
7584       if (REGNO (operands[1]) == IP_REGNUM)
7585         abort ();
7586
7587       switch (GET_CODE (XEXP (operands[0], 0)))
7588         {
7589         case REG:
7590           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
7591           break;
7592
7593         case PRE_INC:
7594           abort (); /* Should never happen now.  */
7595           break;
7596
7597         case PRE_DEC:
7598           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
7599           break;
7600
7601         case POST_INC:
7602           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
7603           break;
7604
7605         case POST_DEC:
7606           abort (); /* Should never happen now.  */
7607           break;
7608
7609         case PLUS:
7610           if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
7611             {
7612               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
7613                 {
7614                 case -8:
7615                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
7616                   return "";
7617
7618                 case -4:
7619                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
7620                   return "";
7621
7622                 case 4:
7623                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
7624                   return "";
7625                 }
7626             }
7627           /* Fall through */
7628
7629         default:
7630           otherops[0] = adjust_address (operands[0], VOIDmode, 4);
7631           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
7632           output_asm_insn ("str%?\t%1, %0", operands);
7633           output_asm_insn ("str%?\t%1, %0", otherops);
7634         }
7635     }
7636   else
7637     /* Constraints should prevent this.  */
7638     abort ();
7639
7640   return "";
7641 }
7642
7643
7644 /* Output an arbitrary MOV reg, #n.
7645    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
7646
7647 const char *
7648 output_mov_immediate (operands)
7649      rtx * operands;
7650 {
7651   HOST_WIDE_INT n = INTVAL (operands[1]);
7652
7653   /* Try to use one MOV.  */
7654   if (const_ok_for_arm (n))
7655     output_asm_insn ("mov%?\t%0, %1", operands);
7656
7657   /* Try to use one MVN.  */
7658   else if (const_ok_for_arm (~n))
7659     {
7660       operands[1] = GEN_INT (~n);
7661       output_asm_insn ("mvn%?\t%0, %1", operands);
7662     }
7663   else
7664     {
7665       int n_ones = 0;
7666       int i;
7667
7668       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
7669       for (i = 0; i < 32; i ++)
7670         if (n & 1 << i)
7671           n_ones ++;
7672
7673       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
7674         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
7675       else
7676         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
7677     }
7678
7679   return "";
7680 }
7681
7682 /* Output an ADD r, s, #n where n may be too big for one instruction.
7683    If adding zero to one register, output nothing.  */
7684
7685 const char *
7686 output_add_immediate (operands)
7687      rtx * operands;
7688 {
7689   HOST_WIDE_INT n = INTVAL (operands[2]);
7690
7691   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
7692     {
7693       if (n < 0)
7694         output_multi_immediate (operands,
7695                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
7696                                 -n);
7697       else
7698         output_multi_immediate (operands,
7699                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
7700                                 n);
7701     }
7702
7703   return "";
7704 }
7705
7706 /* Output a multiple immediate operation.
7707    OPERANDS is the vector of operands referred to in the output patterns.
7708    INSTR1 is the output pattern to use for the first constant.
7709    INSTR2 is the output pattern to use for subsequent constants.
7710    IMMED_OP is the index of the constant slot in OPERANDS.
7711    N is the constant value.  */
7712
7713 static const char *
7714 output_multi_immediate (operands, instr1, instr2, immed_op, n)
7715      rtx * operands;
7716      const char * instr1;
7717      const char * instr2;
7718      int immed_op;
7719      HOST_WIDE_INT n;
7720 {
7721 #if HOST_BITS_PER_WIDE_INT > 32
7722   n &= 0xffffffff;
7723 #endif
7724
7725   if (n == 0)
7726     {
7727       /* Quick and easy output.  */
7728       operands[immed_op] = const0_rtx;
7729       output_asm_insn (instr1, operands);
7730     }
7731   else
7732     {
7733       int i;
7734       const char * instr = instr1;
7735
7736       /* Note that n is never zero here (which would give no output).  */
7737       for (i = 0; i < 32; i += 2)
7738         {
7739           if (n & (3 << i))
7740             {
7741               operands[immed_op] = GEN_INT (n & (255 << i));
7742               output_asm_insn (instr, operands);
7743               instr = instr2;
7744               i += 6;
7745             }
7746         }
7747     }
7748   
7749   return "";
7750 }
7751
7752 /* Return the appropriate ARM instruction for the operation code.
7753    The returned result should not be overwritten.  OP is the rtx of the
7754    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
7755    was shifted.  */
7756
7757 const char *
7758 arithmetic_instr (op, shift_first_arg)
7759      rtx op;
7760      int shift_first_arg;
7761 {
7762   switch (GET_CODE (op))
7763     {
7764     case PLUS:
7765       return "add";
7766
7767     case MINUS:
7768       return shift_first_arg ? "rsb" : "sub";
7769
7770     case IOR:
7771       return "orr";
7772
7773     case XOR:
7774       return "eor";
7775
7776     case AND:
7777       return "and";
7778
7779     default:
7780       abort ();
7781     }
7782 }
7783
7784 /* Ensure valid constant shifts and return the appropriate shift mnemonic
7785    for the operation code.  The returned result should not be overwritten.
7786    OP is the rtx code of the shift.
7787    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
7788    shift.  */
7789
7790 static const char *
7791 shift_op (op, amountp)
7792      rtx op;
7793      HOST_WIDE_INT *amountp;
7794 {
7795   const char * mnem;
7796   enum rtx_code code = GET_CODE (op);
7797
7798   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
7799     *amountp = -1;
7800   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
7801     *amountp = INTVAL (XEXP (op, 1));
7802   else
7803     abort ();
7804
7805   switch (code)
7806     {
7807     case ASHIFT:
7808       mnem = "asl";
7809       break;
7810
7811     case ASHIFTRT:
7812       mnem = "asr";
7813       break;
7814
7815     case LSHIFTRT:
7816       mnem = "lsr";
7817       break;
7818
7819     case ROTATERT:
7820       mnem = "ror";
7821       break;
7822
7823     case MULT:
7824       /* We never have to worry about the amount being other than a
7825          power of 2, since this case can never be reloaded from a reg.  */
7826       if (*amountp != -1)
7827         *amountp = int_log2 (*amountp);
7828       else
7829         abort ();
7830       return "asl";
7831
7832     default:
7833       abort ();
7834     }
7835
7836   if (*amountp != -1)
7837     {
7838       /* This is not 100% correct, but follows from the desire to merge
7839          multiplication by a power of 2 with the recognizer for a
7840          shift.  >=32 is not a valid shift for "asl", so we must try and
7841          output a shift that produces the correct arithmetical result.
7842          Using lsr #32 is identical except for the fact that the carry bit
7843          is not set correctly if we set the flags; but we never use the 
7844          carry bit from such an operation, so we can ignore that.  */
7845       if (code == ROTATERT)
7846         /* Rotate is just modulo 32.  */
7847         *amountp &= 31;
7848       else if (*amountp != (*amountp & 31))
7849         {
7850           if (code == ASHIFT)
7851             mnem = "lsr";
7852           *amountp = 32;
7853         }
7854
7855       /* Shifts of 0 are no-ops.  */
7856       if (*amountp == 0)
7857         return NULL;
7858     }     
7859
7860   return mnem;
7861 }
7862
7863 /* Obtain the shift from the POWER of two.  */
7864
7865 static HOST_WIDE_INT
7866 int_log2 (power)
7867      HOST_WIDE_INT power;
7868 {
7869   HOST_WIDE_INT shift = 0;
7870
7871   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
7872     {
7873       if (shift > 31)
7874         abort ();
7875       shift ++;
7876     }
7877
7878   return shift;
7879 }
7880
7881 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
7882    /bin/as is horribly restrictive.  */
7883 #define MAX_ASCII_LEN 51
7884
7885 void
7886 output_ascii_pseudo_op (stream, p, len)
7887      FILE * stream;
7888      const unsigned char * p;
7889      int len;
7890 {
7891   int i;
7892   int len_so_far = 0;
7893
7894   fputs ("\t.ascii\t\"", stream);
7895   
7896   for (i = 0; i < len; i++)
7897     {
7898       int c = p[i];
7899
7900       if (len_so_far >= MAX_ASCII_LEN)
7901         {
7902           fputs ("\"\n\t.ascii\t\"", stream);
7903           len_so_far = 0;
7904         }
7905
7906       switch (c)
7907         {
7908         case TARGET_TAB:                
7909           fputs ("\\t", stream);
7910           len_so_far += 2;                      
7911           break;
7912           
7913         case TARGET_FF:
7914           fputs ("\\f", stream);
7915           len_so_far += 2;
7916           break;
7917           
7918         case TARGET_BS:
7919           fputs ("\\b", stream);
7920           len_so_far += 2;
7921           break;
7922           
7923         case TARGET_CR:
7924           fputs ("\\r", stream);
7925           len_so_far += 2;
7926           break;
7927           
7928         case TARGET_NEWLINE:
7929           fputs ("\\n", stream);
7930           c = p [i + 1];
7931           if ((c >= ' ' && c <= '~')
7932               || c == TARGET_TAB)
7933             /* This is a good place for a line break.  */
7934             len_so_far = MAX_ASCII_LEN;
7935           else
7936             len_so_far += 2;
7937           break;
7938           
7939         case '\"':
7940         case '\\':
7941           putc ('\\', stream);
7942           len_so_far++;
7943           /* drop through.  */
7944
7945         default:
7946           if (c >= ' ' && c <= '~')
7947             {
7948               putc (c, stream);
7949               len_so_far++;
7950             }
7951           else
7952             {
7953               fprintf (stream, "\\%03o", c);
7954               len_so_far += 4;
7955             }
7956           break;
7957         }
7958     }
7959
7960   fputs ("\"\n", stream);
7961 }
7962 \f
7963 /* Compute the register sabe mask for registers 0 through 12
7964    inclusive.  This code is used by both arm_compute_save_reg_mask
7965    and arm_compute_initial_elimination_offset.  */
7966
7967 static unsigned long
7968 arm_compute_save_reg0_reg12_mask ()
7969 {
7970   unsigned long func_type = arm_current_func_type ();
7971   unsigned int save_reg_mask = 0;
7972   unsigned int reg;
7973
7974   if (IS_INTERRUPT (func_type))
7975     {
7976       unsigned int max_reg;
7977       /* Interrupt functions must not corrupt any registers,
7978          even call clobbered ones.  If this is a leaf function
7979          we can just examine the registers used by the RTL, but
7980          otherwise we have to assume that whatever function is
7981          called might clobber anything, and so we have to save
7982          all the call-clobbered registers as well.  */
7983       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
7984         /* FIQ handlers have registers r8 - r12 banked, so
7985            we only need to check r0 - r7, Normal ISRs only
7986            bank r14 and r15, so we must check up to r12.
7987            r13 is the stack pointer which is always preserved,
7988            so we do not need to consider it here.  */
7989         max_reg = 7;
7990       else
7991         max_reg = 12;
7992         
7993       for (reg = 0; reg <= max_reg; reg++)
7994         if (regs_ever_live[reg]
7995             || (! current_function_is_leaf && call_used_regs [reg]))
7996           save_reg_mask |= (1 << reg);
7997     }
7998   else
7999     {
8000       /* In the normal case we only need to save those registers
8001          which are call saved and which are used by this function.  */
8002       for (reg = 0; reg <= 10; reg++)
8003         if (regs_ever_live[reg] && ! call_used_regs [reg])
8004           save_reg_mask |= (1 << reg);
8005
8006       /* Handle the frame pointer as a special case.  */
8007       if (! TARGET_APCS_FRAME
8008           && ! frame_pointer_needed
8009           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8010           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8011         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8012
8013       /* If we aren't loading the PIC register,
8014          don't stack it even though it may be live.  */
8015       if (flag_pic
8016           && ! TARGET_SINGLE_PIC_BASE 
8017           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8018         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8019     }
8020
8021   return save_reg_mask;
8022 }
8023
8024 /* Compute a bit mask of which registers need to be
8025    saved on the stack for the current function.  */
8026
8027 static unsigned long
8028 arm_compute_save_reg_mask ()
8029 {
8030   unsigned int save_reg_mask = 0;
8031   unsigned long func_type = arm_current_func_type ();
8032
8033   if (IS_NAKED (func_type))
8034     /* This should never really happen.  */
8035     return 0;
8036
8037   /* If we are creating a stack frame, then we must save the frame pointer,
8038      IP (which will hold the old stack pointer), LR and the PC.  */
8039   if (frame_pointer_needed)
8040     save_reg_mask |=
8041       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8042       | (1 << IP_REGNUM)
8043       | (1 << LR_REGNUM)
8044       | (1 << PC_REGNUM);
8045
8046   /* Volatile functions do not return, so there
8047      is no need to save any other registers.  */
8048   if (IS_VOLATILE (func_type))
8049     return save_reg_mask;
8050
8051   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8052
8053   /* Decide if we need to save the link register.
8054      Interrupt routines have their own banked link register,
8055      so they never need to save it.
8056      Otherwise if we do not use the link register we do not need to save
8057      it.  If we are pushing other registers onto the stack however, we
8058      can save an instruction in the epilogue by pushing the link register
8059      now and then popping it back into the PC.  This incurs extra memory
8060      accesses though, so we only do it when optimising for size, and only
8061      if we know that we will not need a fancy return sequence.  */
8062   if (regs_ever_live [LR_REGNUM]
8063           || (save_reg_mask
8064               && optimize_size
8065               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8066     save_reg_mask |= 1 << LR_REGNUM;
8067
8068   if (cfun->machine->lr_save_eliminated)
8069     save_reg_mask &= ~ (1 << LR_REGNUM);
8070
8071   return save_reg_mask;
8072 }
8073
8074 /* Generate a function exit sequence.  If REALLY_RETURN is true, then do
8075    everything bar the final return instruction.  */
8076
8077 const char *
8078 output_return_instruction (operand, really_return, reverse)
8079      rtx operand;
8080      int really_return;
8081      int reverse;
8082 {
8083   char conditional[10];
8084   char instr[100];
8085   int reg;
8086   unsigned long live_regs_mask;
8087   unsigned long func_type;
8088
8089   func_type = arm_current_func_type ();
8090
8091   if (IS_NAKED (func_type))
8092     return "";
8093
8094   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8095     {
8096       /* If this function was declared non-returning, and we have found a tail 
8097          call, then we have to trust that the called function won't return.  */
8098       if (really_return)
8099         {
8100           rtx ops[2];
8101       
8102           /* Otherwise, trap an attempted return by aborting.  */
8103           ops[0] = operand;
8104           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
8105                                        : "abort");
8106           assemble_external_libcall (ops[1]);
8107           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8108         }
8109       
8110       return "";
8111     }
8112
8113   if (current_function_calls_alloca && !really_return)
8114     abort ();
8115
8116   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8117
8118   return_used_this_function = 1;
8119
8120   live_regs_mask = arm_compute_save_reg_mask ();
8121
8122   if (live_regs_mask)
8123     {
8124       const char * return_reg;
8125
8126       /* If we do not have any special requirements for function exit 
8127          (eg interworking, or ISR) then we can load the return address 
8128          directly into the PC.  Otherwise we must load it into LR.  */
8129       if (really_return
8130           && ! TARGET_INTERWORK)
8131         return_reg = reg_names[PC_REGNUM];
8132       else
8133         return_reg = reg_names[LR_REGNUM];
8134
8135       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8136         /* There are two possible reasons for the IP register being saved.
8137            Either a stack frame was created, in which case IP contains the
8138            old stack pointer, or an ISR routine corrupted it.  If this in an
8139            ISR routine then just restore IP, otherwise restore IP into SP.  */
8140         if (! IS_INTERRUPT (func_type))
8141           {
8142             live_regs_mask &= ~ (1 << IP_REGNUM);
8143             live_regs_mask |=   (1 << SP_REGNUM);
8144           }
8145
8146       /* On some ARM architectures it is faster to use LDR rather than
8147          LDM to load a single register.  On other architectures, the
8148          cost is the same.  In 26 bit mode, or for exception handlers,
8149          we have to use LDM to load the PC so that the CPSR is also
8150          restored.  */
8151       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8152         {
8153           if (live_regs_mask == (unsigned int)(1 << reg))
8154             break;
8155         }
8156       if (reg <= LAST_ARM_REGNUM
8157           && (reg != LR_REGNUM
8158               || ! really_return 
8159               || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
8160         {
8161           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional, 
8162                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8163         }
8164       else
8165         {
8166           char *p;
8167           int first = 1;
8168
8169           /* Generate the load multiple instruction to restore the registers.  */
8170           if (frame_pointer_needed)
8171             sprintf (instr, "ldm%sea\t%%|fp, {", conditional);
8172           else if (live_regs_mask & (1 << SP_REGNUM))
8173             sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8174           else
8175             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8176
8177           p = instr + strlen (instr);
8178
8179           for (reg = 0; reg <= SP_REGNUM; reg++)
8180             if (live_regs_mask & (1 << reg))
8181               {
8182                 int l = strlen (reg_names[reg]);
8183
8184                 if (first)
8185                   first = 0;
8186                 else
8187                   {
8188                     memcpy (p, ", ", 2);
8189                     p += 2;
8190                   }
8191
8192                 memcpy (p, "%|", 2);
8193                 memcpy (p + 2, reg_names[reg], l);
8194                 p += l + 2;
8195               }
8196           
8197           if (live_regs_mask & (1 << LR_REGNUM))
8198             {
8199               int l = strlen (return_reg);
8200
8201               if (! first)
8202                 {
8203                   memcpy (p, ", ", 2);
8204                   p += 2;
8205                 }
8206
8207               memcpy (p, "%|", 2);
8208               memcpy (p + 2, return_reg, l);
8209               strcpy (p + 2 + l, ((TARGET_APCS_32 
8210                                    && !IS_INTERRUPT (func_type)) 
8211                                   || !really_return) 
8212                       ? "}" : "}^");
8213             }
8214           else
8215             strcpy (p, "}");
8216         }
8217
8218       output_asm_insn (instr, & operand);
8219
8220       /* See if we need to generate an extra instruction to
8221          perform the actual function return.  */
8222       if (really_return
8223           && func_type != ARM_FT_INTERWORKED
8224           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8225         {
8226           /* The return has already been handled
8227              by loading the LR into the PC.  */
8228           really_return = 0;
8229         }
8230     }
8231
8232   if (really_return)
8233     {
8234       switch ((int) ARM_FUNC_TYPE (func_type))
8235         {
8236         case ARM_FT_ISR:
8237         case ARM_FT_FIQ:
8238           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
8239           break;
8240
8241         case ARM_FT_INTERWORKED:
8242           sprintf (instr, "bx%s\t%%|lr", conditional);
8243           break;
8244
8245         case ARM_FT_EXCEPTION:
8246           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
8247           break;
8248
8249         default:
8250           /* ARMv5 implementations always provide BX, so interworking
8251              is the default unless APCS-26 is in use.  */
8252           if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
8253             sprintf (instr, "bx%s\t%%|lr", conditional);            
8254           else
8255             sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
8256                      conditional, TARGET_APCS_32 ? "" : "s");
8257           break;
8258         }
8259
8260       output_asm_insn (instr, & operand);
8261     }
8262
8263   return "";
8264 }
8265
8266 /* Write the function name into the code section, directly preceding
8267    the function prologue.
8268
8269    Code will be output similar to this:
8270      t0
8271          .ascii "arm_poke_function_name", 0
8272          .align
8273      t1
8274          .word 0xff000000 + (t1 - t0)
8275      arm_poke_function_name
8276          mov     ip, sp
8277          stmfd   sp!, {fp, ip, lr, pc}
8278          sub     fp, ip, #4
8279
8280    When performing a stack backtrace, code can inspect the value
8281    of 'pc' stored at 'fp' + 0.  If the trace function then looks
8282    at location pc - 12 and the top 8 bits are set, then we know
8283    that there is a function name embedded immediately preceding this
8284    location and has length ((pc[-3]) & 0xff000000).
8285
8286    We assume that pc is declared as a pointer to an unsigned long.
8287
8288    It is of no benefit to output the function name if we are assembling
8289    a leaf function.  These function types will not contain a stack
8290    backtrace structure, therefore it is not possible to determine the
8291    function name.  */
8292
8293 void
8294 arm_poke_function_name (stream, name)
8295    FILE * stream;
8296    const char * name;
8297 {
8298   unsigned long alignlength;
8299   unsigned long length;
8300   rtx           x;
8301
8302   length      = strlen (name) + 1;
8303   alignlength = ROUND_UP_WORD (length);
8304   
8305   ASM_OUTPUT_ASCII (stream, name, length);
8306   ASM_OUTPUT_ALIGN (stream, 2);
8307   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
8308   assemble_aligned_integer (UNITS_PER_WORD, x);
8309 }
8310
8311 /* Place some comments into the assembler stream
8312    describing the current function.  */
8313
8314 static void
8315 arm_output_function_prologue (f, frame_size)
8316      FILE * f;
8317      HOST_WIDE_INT frame_size;
8318 {
8319   unsigned long func_type;
8320
8321   if (!TARGET_ARM)
8322     {
8323       thumb_output_function_prologue (f, frame_size);
8324       return;
8325     }
8326   
8327   /* Sanity check.  */
8328   if (arm_ccfsm_state || arm_target_insn)
8329     abort ();
8330
8331   func_type = arm_current_func_type ();
8332   
8333   switch ((int) ARM_FUNC_TYPE (func_type))
8334     {
8335     default:
8336     case ARM_FT_NORMAL:
8337       break;
8338     case ARM_FT_INTERWORKED:
8339       asm_fprintf (f, "\t%@ Function supports interworking.\n");
8340       break;
8341     case ARM_FT_EXCEPTION_HANDLER:
8342       asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
8343       break;
8344     case ARM_FT_ISR:
8345       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
8346       break;
8347     case ARM_FT_FIQ:
8348       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
8349       break;
8350     case ARM_FT_EXCEPTION:
8351       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
8352       break;
8353     }
8354   
8355   if (IS_NAKED (func_type))
8356     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
8357
8358   if (IS_VOLATILE (func_type))
8359     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
8360
8361   if (IS_NESTED (func_type))
8362     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
8363     
8364   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
8365                current_function_args_size,
8366                current_function_pretend_args_size, frame_size);
8367
8368   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
8369                frame_pointer_needed,
8370                cfun->machine->uses_anonymous_args);
8371
8372   if (cfun->machine->lr_save_eliminated)
8373     asm_fprintf (f, "\t%@ link register save eliminated.\n");
8374
8375 #ifdef AOF_ASSEMBLER
8376   if (flag_pic)
8377     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
8378 #endif
8379
8380   return_used_this_function = 0;  
8381 }
8382
8383 const char *
8384 arm_output_epilogue (really_return)
8385      int really_return;
8386 {
8387   int reg;
8388   unsigned long saved_regs_mask;
8389   unsigned long func_type;
8390   /* Floats_offset is the offset from the "virtual" frame.  In an APCS 
8391      frame that is $fp + 4 for a non-variadic function.  */
8392   int floats_offset = 0;
8393   rtx operands[3];
8394   int frame_size = arm_get_frame_size ();
8395   FILE * f = asm_out_file;
8396   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
8397
8398   /* If we have already generated the return instruction
8399      then it is futile to generate anything else.  */
8400   if (use_return_insn (FALSE) && return_used_this_function)
8401     return "";
8402
8403   func_type = arm_current_func_type ();
8404
8405   if (IS_NAKED (func_type))
8406     /* Naked functions don't have epilogues.  */
8407     return "";
8408
8409   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8410     {
8411       rtx op;
8412           
8413       /* A volatile function should never return.  Call abort.  */
8414       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
8415       assemble_external_libcall (op);
8416       output_asm_insn ("bl\t%a0", &op);
8417       
8418       return "";
8419     }
8420
8421   if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
8422       && ! really_return)
8423     /* If we are throwing an exception, then we really must
8424        be doing a return,  so we can't tail-call.  */
8425     abort ();
8426   
8427   saved_regs_mask = arm_compute_save_reg_mask ();
8428   
8429   /* XXX We should adjust floats_offset for any anonymous args, and then
8430      re-adjust vfp_offset below to compensate.  */
8431
8432   /* Compute how far away the floats will be.  */
8433   for (reg = 0; reg <= LAST_ARM_REGNUM; reg ++)
8434     if (saved_regs_mask & (1 << reg))
8435       floats_offset += 4;
8436   
8437   if (frame_pointer_needed)
8438     {
8439       int vfp_offset = 4;
8440
8441       if (arm_fpu_arch == FP_SOFT2)
8442         {
8443           for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
8444             if (regs_ever_live[reg] && !call_used_regs[reg])
8445               {
8446                 floats_offset += 12;
8447                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
8448                              reg, FP_REGNUM, floats_offset - vfp_offset);
8449               }
8450         }
8451       else
8452         {
8453           int start_reg = LAST_ARM_FP_REGNUM;
8454
8455           for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
8456             {
8457               if (regs_ever_live[reg] && !call_used_regs[reg])
8458                 {
8459                   floats_offset += 12;
8460                   
8461                   /* We can't unstack more than four registers at once.  */
8462                   if (start_reg - reg == 3)
8463                     {
8464                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
8465                                    reg, FP_REGNUM, floats_offset - vfp_offset);
8466                       start_reg = reg - 1;
8467                     }
8468                 }
8469               else
8470                 {
8471                   if (reg != start_reg)
8472                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
8473                                  reg + 1, start_reg - reg,
8474                                  FP_REGNUM, floats_offset - vfp_offset);
8475                   start_reg = reg - 1;
8476                 }
8477             }
8478
8479           /* Just in case the last register checked also needs unstacking.  */
8480           if (reg != start_reg)
8481             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
8482                          reg + 1, start_reg - reg,
8483                          FP_REGNUM, floats_offset - vfp_offset);
8484         }
8485
8486       /* saved_regs_mask should contain the IP, which at the time of stack
8487          frame generation actually contains the old stack pointer.  So a
8488          quick way to unwind the stack is just pop the IP register directly
8489          into the stack pointer.  */
8490       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
8491         abort ();
8492       saved_regs_mask &= ~ (1 << IP_REGNUM);
8493       saved_regs_mask |=   (1 << SP_REGNUM);
8494
8495       /* There are two registers left in saved_regs_mask - LR and PC.  We
8496          only need to restore the LR register (the return address), but to
8497          save time we can load it directly into the PC, unless we need a
8498          special function exit sequence, or we are not really returning.  */
8499       if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
8500         /* Delete the LR from the register mask, so that the LR on
8501            the stack is loaded into the PC in the register mask.  */
8502         saved_regs_mask &= ~ (1 << LR_REGNUM);
8503       else
8504         saved_regs_mask &= ~ (1 << PC_REGNUM);
8505       
8506       print_multi_reg (f, "ldmea\t%r", FP_REGNUM, saved_regs_mask);
8507
8508       if (IS_INTERRUPT (func_type))
8509         /* Interrupt handlers will have pushed the
8510            IP onto the stack, so restore it now.  */
8511         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
8512     }
8513   else
8514     {
8515       /* Restore stack pointer if necessary.  */
8516       if (frame_size + current_function_outgoing_args_size != 0)
8517         {
8518           operands[0] = operands[1] = stack_pointer_rtx;
8519           operands[2] = GEN_INT (frame_size
8520                                  + current_function_outgoing_args_size);
8521           output_add_immediate (operands);
8522         }
8523
8524       if (arm_fpu_arch == FP_SOFT2)
8525         {
8526           for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
8527             if (regs_ever_live[reg] && !call_used_regs[reg])
8528               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
8529                            reg, SP_REGNUM);
8530         }
8531       else
8532         {
8533           int start_reg = FIRST_ARM_FP_REGNUM;
8534
8535           for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
8536             {
8537               if (regs_ever_live[reg] && !call_used_regs[reg])
8538                 {
8539                   if (reg - start_reg == 3)
8540                     {
8541                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
8542                                    start_reg, SP_REGNUM);
8543                       start_reg = reg + 1;
8544                     }
8545                 }
8546               else
8547                 {
8548                   if (reg != start_reg)
8549                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
8550                                  start_reg, reg - start_reg,
8551                                  SP_REGNUM);
8552                   
8553                   start_reg = reg + 1;
8554                 }
8555             }
8556
8557           /* Just in case the last register checked also needs unstacking.  */
8558           if (reg != start_reg)
8559             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
8560                          start_reg, reg - start_reg, SP_REGNUM);
8561         }
8562
8563       /* If we can, restore the LR into the PC.  */
8564       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8565           && really_return
8566           && current_function_pretend_args_size == 0
8567           && saved_regs_mask & (1 << LR_REGNUM))
8568         {
8569           saved_regs_mask &= ~ (1 << LR_REGNUM);
8570           saved_regs_mask |=   (1 << PC_REGNUM);
8571         }
8572
8573       /* Load the registers off the stack.  If we only have one register
8574          to load use the LDR instruction - it is faster.  */
8575       if (saved_regs_mask == (1 << LR_REGNUM))
8576         {
8577           /* The exception handler ignores the LR, so we do
8578              not really need to load it off the stack.  */
8579           if (eh_ofs)
8580             asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
8581           else
8582             asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
8583         }
8584       else if (saved_regs_mask)
8585         {
8586           if (saved_regs_mask & (1 << SP_REGNUM))
8587             /* Note - write back to the stack register is not enabled
8588                (ie "ldmfd sp!...").  We know that the stack pointer is
8589                in the list of registers and if we add writeback the
8590                instruction becomes UNPREDICTABLE.  */
8591             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
8592           else
8593             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
8594         }
8595
8596       if (current_function_pretend_args_size)
8597         {
8598           /* Unwind the pre-pushed regs.  */
8599           operands[0] = operands[1] = stack_pointer_rtx;
8600           operands[2] = GEN_INT (current_function_pretend_args_size);
8601           output_add_immediate (operands);
8602         }
8603     }
8604
8605 #if 0
8606   if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER)
8607     /* Adjust the stack to remove the exception handler stuff.  */
8608     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
8609                  REGNO (eh_ofs));
8610 #endif
8611
8612   if (! really_return
8613     || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8614         && current_function_pretend_args_size == 0
8615         && saved_regs_mask & (1 << PC_REGNUM)))
8616     return "";
8617
8618   /* Generate the return instruction.  */
8619   switch ((int) ARM_FUNC_TYPE (func_type))
8620     {
8621     case ARM_FT_EXCEPTION_HANDLER:
8622       /* Even in 26-bit mode we do a mov (rather than a movs)
8623          because we don't have the PSR bits set in the address.  */
8624       asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
8625       break;
8626
8627     case ARM_FT_ISR:
8628     case ARM_FT_FIQ:
8629       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
8630       break;
8631
8632     case ARM_FT_EXCEPTION:
8633       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
8634       break;
8635
8636     case ARM_FT_INTERWORKED:
8637       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
8638       break;
8639
8640     default:
8641       if (frame_pointer_needed)
8642         /* If we used the frame pointer then the return address
8643            will have been loaded off the stack directly into the
8644            PC, so there is no need to issue a MOV instruction
8645            here.  */
8646         ;
8647       else if (current_function_pretend_args_size == 0
8648                && (saved_regs_mask & (1 << LR_REGNUM)))
8649         /* Similarly we may have been able to load LR into the PC
8650            even if we did not create a stack frame.  */
8651         ;
8652       else if (TARGET_APCS_32)
8653         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
8654       else
8655         asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
8656       break;
8657     }
8658
8659   return "";
8660 }
8661
8662 static void
8663 arm_output_function_epilogue (file, frame_size)
8664      FILE *file ATTRIBUTE_UNUSED;
8665      HOST_WIDE_INT frame_size;
8666 {
8667   if (TARGET_THUMB)
8668     {
8669       /* ??? Probably not safe to set this here, since it assumes that a
8670          function will be emitted as assembly immediately after we generate
8671          RTL for it.  This does not happen for inline functions.  */
8672       return_used_this_function = 0;
8673     }
8674   else
8675     {
8676       /* We need to take into account any stack-frame rounding.  */
8677       frame_size = arm_get_frame_size ();
8678
8679       if (use_return_insn (FALSE)
8680           && return_used_this_function
8681           && (frame_size + current_function_outgoing_args_size) != 0
8682           && !frame_pointer_needed)
8683         abort ();
8684
8685       /* Reset the ARM-specific per-function variables.  */
8686       after_arm_reorg = 0;
8687     }
8688 }
8689
8690 /* Generate and emit an insn that we will recognize as a push_multi.
8691    Unfortunately, since this insn does not reflect very well the actual
8692    semantics of the operation, we need to annotate the insn for the benefit
8693    of DWARF2 frame unwind information.  */
8694
8695 static rtx
8696 emit_multi_reg_push (mask)
8697      int mask;
8698 {
8699   int num_regs = 0;
8700   int num_dwarf_regs;
8701   int i, j;
8702   rtx par;
8703   rtx dwarf;
8704   int dwarf_par_index;
8705   rtx tmp, reg;
8706
8707   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8708     if (mask & (1 << i))
8709       num_regs++;
8710
8711   if (num_regs == 0 || num_regs > 16)
8712     abort ();
8713
8714   /* We don't record the PC in the dwarf frame information.  */
8715   num_dwarf_regs = num_regs;
8716   if (mask & (1 << PC_REGNUM))
8717     num_dwarf_regs--;
8718
8719   /* For the body of the insn we are going to generate an UNSPEC in
8720      parallel with several USEs.  This allows the insn to be recognized
8721      by the push_multi pattern in the arm.md file.  The insn looks
8722      something like this:
8723
8724        (parallel [ 
8725            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
8726                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
8727            (use (reg:SI 11 fp))
8728            (use (reg:SI 12 ip))
8729            (use (reg:SI 14 lr))
8730            (use (reg:SI 15 pc))
8731         ])
8732
8733      For the frame note however, we try to be more explicit and actually
8734      show each register being stored into the stack frame, plus a (single)
8735      decrement of the stack pointer.  We do it this way in order to be
8736      friendly to the stack unwinding code, which only wants to see a single
8737      stack decrement per instruction.  The RTL we generate for the note looks
8738      something like this:
8739
8740       (sequence [ 
8741            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
8742            (set (mem:SI (reg:SI sp)) (reg:SI r4))
8743            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
8744            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
8745            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
8746         ])
8747
8748       This sequence is used both by the code to support stack unwinding for
8749       exceptions handlers and the code to generate dwarf2 frame debugging.  */
8750   
8751   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
8752   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
8753   dwarf_par_index = 1;
8754
8755   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8756     {
8757       if (mask & (1 << i))
8758         {
8759           reg = gen_rtx_REG (SImode, i);
8760
8761           XVECEXP (par, 0, 0)
8762             = gen_rtx_SET (VOIDmode,
8763                            gen_rtx_MEM (BLKmode,
8764                                         gen_rtx_PRE_DEC (BLKmode,
8765                                                          stack_pointer_rtx)),
8766                            gen_rtx_UNSPEC (BLKmode,
8767                                            gen_rtvec (1, reg),
8768                                            UNSPEC_PUSH_MULT));
8769
8770           if (i != PC_REGNUM)
8771             {
8772               tmp = gen_rtx_SET (VOIDmode,
8773                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
8774                                  reg);
8775               RTX_FRAME_RELATED_P (tmp) = 1;
8776               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
8777               dwarf_par_index++;
8778             }
8779
8780           break;
8781         }
8782     }
8783
8784   for (j = 1, i++; j < num_regs; i++)
8785     {
8786       if (mask & (1 << i))
8787         {
8788           reg = gen_rtx_REG (SImode, i);
8789
8790           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
8791
8792           if (i != PC_REGNUM)
8793             {
8794               tmp = gen_rtx_SET (VOIDmode,
8795                                  gen_rtx_MEM (SImode,
8796                                               plus_constant (stack_pointer_rtx,
8797                                                              4 * j)),
8798                                  reg);
8799               RTX_FRAME_RELATED_P (tmp) = 1;
8800               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
8801             }
8802
8803           j++;
8804         }
8805     }
8806
8807   par = emit_insn (par);
8808   
8809   tmp = gen_rtx_SET (SImode,
8810                      stack_pointer_rtx,
8811                      gen_rtx_PLUS (SImode,
8812                                    stack_pointer_rtx,
8813                                    GEN_INT (-4 * num_regs)));
8814   RTX_FRAME_RELATED_P (tmp) = 1;
8815   XVECEXP (dwarf, 0, 0) = tmp;
8816   
8817   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8818                                        REG_NOTES (par));
8819   return par;
8820 }
8821
8822 static rtx
8823 emit_sfm (base_reg, count)
8824      int base_reg;
8825      int count;
8826 {
8827   rtx par;
8828   rtx dwarf;
8829   rtx tmp, reg;
8830   int i;
8831
8832   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8833   dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8834
8835   reg = gen_rtx_REG (XFmode, base_reg++);
8836
8837   XVECEXP (par, 0, 0)
8838     = gen_rtx_SET (VOIDmode, 
8839                    gen_rtx_MEM (BLKmode,
8840                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
8841                    gen_rtx_UNSPEC (BLKmode,
8842                                    gen_rtvec (1, reg),
8843                                    UNSPEC_PUSH_MULT));
8844   tmp
8845     = gen_rtx_SET (VOIDmode, 
8846                    gen_rtx_MEM (XFmode,
8847                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
8848                    reg);
8849   RTX_FRAME_RELATED_P (tmp) = 1;
8850   XVECEXP (dwarf, 0, count - 1) = tmp;    
8851   
8852   for (i = 1; i < count; i++)
8853     {
8854       reg = gen_rtx_REG (XFmode, base_reg++);
8855       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8856
8857       tmp = gen_rtx_SET (VOIDmode, 
8858                          gen_rtx_MEM (XFmode,
8859                                       gen_rtx_PRE_DEC (BLKmode,
8860                                                        stack_pointer_rtx)),
8861                          reg);
8862       RTX_FRAME_RELATED_P (tmp) = 1;
8863       XVECEXP (dwarf, 0, count - i - 1) = tmp;    
8864     }
8865
8866   par = emit_insn (par);
8867   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8868                                        REG_NOTES (par));
8869   return par;
8870 }
8871
8872 /* Compute the distance from register FROM to register TO.
8873    These can be the arg pointer (26), the soft frame pointer (25),
8874    the stack pointer (13) or the hard frame pointer (11).
8875    Typical stack layout looks like this:
8876
8877        old stack pointer -> |    |
8878                              ----
8879                             |    | \
8880                             |    |   saved arguments for
8881                             |    |   vararg functions
8882                             |    | /
8883                               --
8884    hard FP & arg pointer -> |    | \
8885                             |    |   stack
8886                             |    |   frame
8887                             |    | /
8888                               --
8889                             |    | \
8890                             |    |   call saved
8891                             |    |   registers
8892       soft frame pointer -> |    | /
8893                               --
8894                             |    | \
8895                             |    |   local
8896                             |    |   variables
8897                             |    | /
8898                               --
8899                             |    | \
8900                             |    |   outgoing
8901                             |    |   arguments
8902    current stack pointer -> |    | /
8903                               --
8904
8905   For a given function some or all of these stack components
8906   may not be needed, giving rise to the possibility of
8907   eliminating some of the registers.
8908
8909   The values returned by this function must reflect the behavior
8910   of arm_expand_prologue() and arm_compute_save_reg_mask().
8911
8912   The sign of the number returned reflects the direction of stack
8913   growth, so the values are positive for all eliminations except
8914   from the soft frame pointer to the hard frame pointer.  */
8915                             
8916 unsigned int
8917 arm_compute_initial_elimination_offset (from, to)
8918      unsigned int from;
8919      unsigned int to;
8920 {
8921   unsigned int local_vars    = arm_get_frame_size ();
8922   unsigned int outgoing_args = current_function_outgoing_args_size;
8923   unsigned int stack_frame;
8924   unsigned int call_saved_registers;
8925   unsigned long func_type;
8926   
8927   func_type = arm_current_func_type ();
8928
8929   /* Volatile functions never return, so there is
8930      no need to save call saved registers.  */
8931   call_saved_registers = 0;
8932   if (! IS_VOLATILE (func_type))
8933     {
8934       unsigned int reg_mask;
8935       unsigned int reg;
8936
8937       /* Make sure that we compute which registers will be saved
8938          on the stack using the same algorithm that is used by
8939          arm_compute_save_reg_mask().  */
8940       reg_mask = arm_compute_save_reg0_reg12_mask ();
8941
8942       /* Now count the number of bits set in save_reg_mask.
8943          For each set bit we need 4 bytes of stack space.  */
8944       while (reg_mask)
8945         {
8946           call_saved_registers += 4;
8947           reg_mask = reg_mask & ~ (reg_mask & - reg_mask);
8948         }
8949
8950       if ((regs_ever_live[LR_REGNUM]
8951            /* If optimizing for size, then we save the link register if
8952               any other integer register is saved.  This gives a smaller
8953               return sequence.  */
8954            || (optimize_size && call_saved_registers > 0))
8955           /* But if a stack frame is going to be created, the LR will
8956              be saved as part of that, so we do not need to allow for
8957              it here.  */
8958           && ! frame_pointer_needed)
8959         call_saved_registers += 4;
8960
8961       /* If the hard floating point registers are going to be
8962          used then they must be saved on the stack as well.
8963          Each register occupies 12 bytes of stack space.  */
8964       for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg ++)
8965         if (regs_ever_live[reg] && ! call_used_regs[reg])
8966           call_saved_registers += 12;
8967     }
8968
8969   /* The stack frame contains 4 registers - the old frame pointer,
8970      the old stack pointer, the return address and PC of the start
8971      of the function.  */
8972   stack_frame = frame_pointer_needed ? 16 : 0;
8973
8974   /* OK, now we have enough information to compute the distances.
8975      There must be an entry in these switch tables for each pair
8976      of registers in ELIMINABLE_REGS, even if some of the entries
8977      seem to be redundant or useless.  */
8978   switch (from)
8979     {
8980     case ARG_POINTER_REGNUM:
8981       switch (to)
8982         {
8983         case THUMB_HARD_FRAME_POINTER_REGNUM:
8984           return 0;
8985
8986         case FRAME_POINTER_REGNUM:
8987           /* This is the reverse of the soft frame pointer
8988              to hard frame pointer elimination below.  */
8989           if (call_saved_registers == 0 && stack_frame == 0)
8990             return 0;
8991           return (call_saved_registers + stack_frame - 4);
8992
8993         case ARM_HARD_FRAME_POINTER_REGNUM:
8994           /* If there is no stack frame then the hard
8995              frame pointer and the arg pointer coincide.  */
8996           if (stack_frame == 0 && call_saved_registers != 0)
8997             return 0;
8998           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
8999           return (frame_pointer_needed
9000                   && current_function_needs_context
9001                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9002
9003         case STACK_POINTER_REGNUM:
9004           /* If nothing has been pushed on the stack at all
9005              then this will return -4.  This *is* correct!  */
9006           return call_saved_registers + stack_frame + local_vars + outgoing_args - 4;
9007
9008         default:
9009           abort ();
9010         }
9011       break;
9012
9013     case FRAME_POINTER_REGNUM:
9014       switch (to)
9015         {
9016         case THUMB_HARD_FRAME_POINTER_REGNUM:
9017           return 0;
9018
9019         case ARM_HARD_FRAME_POINTER_REGNUM:
9020           /* The hard frame pointer points to the top entry in the
9021              stack frame.  The soft frame pointer to the bottom entry
9022              in the stack frame.  If there is no stack frame at all,
9023              then they are identical.  */
9024           if (call_saved_registers == 0 && stack_frame == 0)
9025             return 0;
9026           return - (call_saved_registers + stack_frame - 4);
9027
9028         case STACK_POINTER_REGNUM:
9029           return local_vars + outgoing_args;
9030
9031         default:
9032           abort ();
9033         }
9034       break;
9035
9036     default:
9037       /* You cannot eliminate from the stack pointer.
9038          In theory you could eliminate from the hard frame
9039          pointer to the stack pointer, but this will never
9040          happen, since if a stack frame is not needed the
9041          hard frame pointer will never be used.  */
9042       abort ();
9043     }
9044 }
9045
9046 /* Calculate the size of the stack frame, taking into account any
9047    padding that is required to ensure stack-alignment.  */
9048
9049 HOST_WIDE_INT
9050 arm_get_frame_size ()
9051 {
9052   int regno;
9053
9054   int base_size = ROUND_UP_WORD (get_frame_size ());
9055   int entry_size = 0;
9056   unsigned long func_type = arm_current_func_type ();
9057   int leaf;
9058
9059   if (! TARGET_ARM)
9060     abort();
9061
9062   if (! TARGET_ATPCS)
9063     return base_size;
9064
9065   /* We need to know if we are a leaf function.  Unfortunately, it
9066      is possible to be called after start_sequence has been called,
9067      which causes get_insns to return the insns for the sequence,
9068      not the function, which will cause leaf_function_p to return
9069      the incorrect result.
9070
9071      To work around this, we cache the computed frame size.  This
9072      works because we will only be calling RTL expanders that need
9073      to know about leaf functions once reload has completed, and the
9074      frame size cannot be changed after that time, so we can safely
9075      use the cached value.  */
9076
9077   if (reload_completed)
9078     return cfun->machine->frame_size;
9079
9080   leaf = leaf_function_p ();
9081
9082   /* A leaf function does not need any stack alignment if it has nothing
9083      on the stack.  */
9084   if (leaf && base_size == 0)
9085     {
9086       cfun->machine->frame_size = 0;
9087       return 0;
9088     }
9089
9090   /* We know that SP will be word aligned on entry, and we must
9091      preserve that condition at any subroutine call.  But those are
9092      the only constraints.  */
9093
9094   /* Space for variadic functions.  */
9095   if (current_function_pretend_args_size)
9096     entry_size += current_function_pretend_args_size;
9097
9098   /* Space for saved registers.  */
9099   entry_size += bit_count (arm_compute_save_reg_mask ()) * 4;
9100
9101   /* Space for saved FPA registers.  */
9102   if (! IS_VOLATILE (func_type))
9103     {
9104       for (regno = FIRST_ARM_FP_REGNUM; regno <= LAST_ARM_FP_REGNUM; regno++)
9105       if (regs_ever_live[regno] && ! call_used_regs[regno])
9106         entry_size += 12;
9107     }
9108
9109   if ((entry_size + base_size + current_function_outgoing_args_size) & 7)
9110     base_size += 4;
9111   if ((entry_size + base_size + current_function_outgoing_args_size) & 7)
9112     abort ();
9113
9114   cfun->machine->frame_size = base_size;
9115
9116   return base_size;
9117 }
9118
9119 /* Generate the prologue instructions for entry into an ARM function.  */
9120
9121 void
9122 arm_expand_prologue ()
9123 {
9124   int reg;
9125   rtx amount;
9126   rtx insn;
9127   rtx ip_rtx;
9128   unsigned long live_regs_mask;
9129   unsigned long func_type;
9130   int fp_offset = 0;
9131   int saved_pretend_args = 0;
9132   unsigned int args_to_push;
9133
9134   func_type = arm_current_func_type ();
9135
9136   /* Naked functions don't have prologues.  */
9137   if (IS_NAKED (func_type))
9138     return;
9139
9140   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
9141   args_to_push = current_function_pretend_args_size;
9142   
9143   /* Compute which register we will have to save onto the stack.  */
9144   live_regs_mask = arm_compute_save_reg_mask ();
9145
9146   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
9147
9148   if (frame_pointer_needed)
9149     {
9150       if (IS_INTERRUPT (func_type))
9151         {
9152           /* Interrupt functions must not corrupt any registers.
9153              Creating a frame pointer however, corrupts the IP
9154              register, so we must push it first.  */
9155           insn = emit_multi_reg_push (1 << IP_REGNUM);
9156
9157           /* Do not set RTX_FRAME_RELATED_P on this insn.
9158              The dwarf stack unwinding code only wants to see one
9159              stack decrement per function, and this is not it.  If
9160              this instruction is labeled as being part of the frame
9161              creation sequence then dwarf2out_frame_debug_expr will
9162              abort when it encounters the assignment of IP to FP
9163              later on, since the use of SP here establishes SP as
9164              the CFA register and not IP.
9165
9166              Anyway this instruction is not really part of the stack
9167              frame creation although it is part of the prologue.  */
9168         }
9169       else if (IS_NESTED (func_type))
9170         {
9171           /* The Static chain register is the same as the IP register
9172              used as a scratch register during stack frame creation.
9173              To get around this need to find somewhere to store IP
9174              whilst the frame is being created.  We try the following
9175              places in order:
9176              
9177                1. The last argument register.
9178                2. A slot on the stack above the frame.  (This only
9179                   works if the function is not a varargs function).
9180                3. Register r3, after pushing the argument registers
9181                   onto the stack.
9182
9183              Note - we only need to tell the dwarf2 backend about the SP
9184              adjustment in the second variant; the static chain register
9185              doesn't need to be unwound, as it doesn't contain a value
9186              inherited from the caller.  */
9187
9188           if (regs_ever_live[3] == 0)
9189             {
9190               insn = gen_rtx_REG (SImode, 3);
9191               insn = gen_rtx_SET (SImode, insn, ip_rtx);
9192               insn = emit_insn (insn);
9193             }
9194           else if (args_to_push == 0)
9195             {
9196               rtx dwarf;
9197               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
9198               insn = gen_rtx_MEM (SImode, insn);
9199               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
9200               insn = emit_insn (insn);
9201
9202               fp_offset = 4;
9203
9204               /* Just tell the dwarf backend that we adjusted SP.  */
9205               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9206                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
9207                                                  GEN_INT (-fp_offset)));
9208               RTX_FRAME_RELATED_P (insn) = 1;
9209               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9210                                                     dwarf, REG_NOTES (insn));
9211             }
9212           else
9213             {
9214               /* Store the args on the stack.  */
9215               if (cfun->machine->uses_anonymous_args)
9216                 insn = emit_multi_reg_push
9217                   ((0xf0 >> (args_to_push / 4)) & 0xf);
9218               else
9219                 insn = emit_insn
9220                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
9221                                GEN_INT (- args_to_push)));
9222
9223               RTX_FRAME_RELATED_P (insn) = 1;
9224
9225               saved_pretend_args = 1;
9226               fp_offset = args_to_push;
9227               args_to_push = 0;
9228
9229               /* Now reuse r3 to preserve IP.  */
9230               insn = gen_rtx_REG (SImode, 3);
9231               insn = gen_rtx_SET (SImode, insn, ip_rtx);
9232               (void) emit_insn (insn);
9233             }
9234         }
9235
9236       if (fp_offset)
9237         {
9238           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
9239           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
9240         }
9241       else
9242         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
9243       
9244       insn = emit_insn (insn);
9245       RTX_FRAME_RELATED_P (insn) = 1;
9246     }
9247
9248   if (args_to_push)
9249     {
9250       /* Push the argument registers, or reserve space for them.  */
9251       if (cfun->machine->uses_anonymous_args)
9252         insn = emit_multi_reg_push
9253           ((0xf0 >> (args_to_push / 4)) & 0xf);
9254       else
9255         insn = emit_insn
9256           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
9257                        GEN_INT (- args_to_push)));
9258       RTX_FRAME_RELATED_P (insn) = 1;
9259     }
9260
9261   /* If this is an interrupt service routine, and the link register
9262      is going to be pushed, and we are not creating a stack frame,
9263      (which would involve an extra push of IP and a pop in the epilogue)
9264      subtracting four from LR now will mean that the function return
9265      can be done with a single instruction.  */
9266   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
9267       && (live_regs_mask & (1 << LR_REGNUM)) != 0
9268       && ! frame_pointer_needed)
9269     emit_insn (gen_rtx_SET (SImode, 
9270                             gen_rtx_REG (SImode, LR_REGNUM),
9271                             gen_rtx_PLUS (SImode,
9272                                           gen_rtx_REG (SImode, LR_REGNUM),
9273                                           GEN_INT (-4))));
9274
9275   if (live_regs_mask)
9276     {
9277       insn = emit_multi_reg_push (live_regs_mask);
9278       RTX_FRAME_RELATED_P (insn) = 1;
9279     }
9280
9281   if (! IS_VOLATILE (func_type))
9282     {
9283       /* Save any floating point call-saved registers used by this function.  */
9284       if (arm_fpu_arch == FP_SOFT2)
9285         {
9286           for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
9287             if (regs_ever_live[reg] && !call_used_regs[reg])
9288               {
9289                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
9290                 insn = gen_rtx_MEM (XFmode, insn);
9291                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
9292                                                gen_rtx_REG (XFmode, reg)));
9293                 RTX_FRAME_RELATED_P (insn) = 1;
9294               }
9295         }
9296       else
9297         {
9298           int start_reg = LAST_ARM_FP_REGNUM;
9299
9300           for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
9301             {
9302               if (regs_ever_live[reg] && !call_used_regs[reg])
9303                 {
9304                   if (start_reg - reg == 3)
9305                     {
9306                       insn = emit_sfm (reg, 4);
9307                       RTX_FRAME_RELATED_P (insn) = 1;
9308                       start_reg = reg - 1;
9309                     }
9310                 }
9311               else
9312                 {
9313                   if (start_reg != reg)
9314                     {
9315                       insn = emit_sfm (reg + 1, start_reg - reg);
9316                       RTX_FRAME_RELATED_P (insn) = 1;
9317                     }
9318                   start_reg = reg - 1;
9319                 }
9320             }
9321
9322           if (start_reg != reg)
9323             {
9324               insn = emit_sfm (reg + 1, start_reg - reg);
9325               RTX_FRAME_RELATED_P (insn) = 1;
9326             }
9327         }
9328     }
9329
9330   if (frame_pointer_needed)
9331     {
9332       /* Create the new frame pointer.  */
9333       insn = GEN_INT (-(4 + args_to_push + fp_offset));
9334       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
9335       RTX_FRAME_RELATED_P (insn) = 1;
9336       
9337       if (IS_NESTED (func_type))
9338         {
9339           /* Recover the static chain register.  */
9340           if (regs_ever_live [3] == 0
9341               || saved_pretend_args)
9342             insn = gen_rtx_REG (SImode, 3);
9343           else /* if (current_function_pretend_args_size == 0) */
9344             {
9345               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx, GEN_INT (4));
9346               insn = gen_rtx_MEM (SImode, insn);
9347             }
9348
9349           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
9350           /* Add a USE to stop propagate_one_insn() from barfing.  */
9351           emit_insn (gen_prologue_use (ip_rtx));
9352         }
9353     }
9354
9355   amount = GEN_INT (-(arm_get_frame_size ()
9356                       + current_function_outgoing_args_size));
9357
9358   if (amount != const0_rtx)
9359     {
9360       /* This add can produce multiple insns for a large constant, so we
9361          need to get tricky.  */
9362       rtx last = get_last_insn ();
9363       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
9364                                     amount));
9365       do
9366         {
9367           last = last ? NEXT_INSN (last) : get_insns ();
9368           RTX_FRAME_RELATED_P (last) = 1;
9369         }
9370       while (last != insn);
9371
9372       /* If the frame pointer is needed, emit a special barrier that
9373          will prevent the scheduler from moving stores to the frame
9374          before the stack adjustment.  */
9375       if (frame_pointer_needed)
9376         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
9377                                          hard_frame_pointer_rtx));
9378     }
9379
9380   /* If we are profiling, make sure no instructions are scheduled before
9381      the call to mcount.  Similarly if the user has requested no
9382      scheduling in the prolog.  */
9383   if (current_function_profile || TARGET_NO_SCHED_PRO)
9384     emit_insn (gen_blockage ());
9385
9386   /* If the link register is being kept alive, with the return address in it,
9387      then make sure that it does not get reused by the ce2 pass.  */
9388   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
9389     {
9390       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
9391       cfun->machine->lr_save_eliminated = 1;
9392     }
9393 }
9394 \f
9395 /* If CODE is 'd', then the X is a condition operand and the instruction
9396    should only be executed if the condition is true.
9397    if CODE is 'D', then the X is a condition operand and the instruction
9398    should only be executed if the condition is false: however, if the mode
9399    of the comparison is CCFPEmode, then always execute the instruction -- we
9400    do this because in these circumstances !GE does not necessarily imply LT;
9401    in these cases the instruction pattern will take care to make sure that
9402    an instruction containing %d will follow, thereby undoing the effects of
9403    doing this instruction unconditionally.
9404    If CODE is 'N' then X is a floating point operand that must be negated
9405    before output.
9406    If CODE is 'B' then output a bitwise inverted value of X (a const int).
9407    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
9408
9409 void
9410 arm_print_operand (stream, x, code)
9411      FILE * stream;
9412      rtx x;
9413      int code;
9414 {
9415   switch (code)
9416     {
9417     case '@':
9418       fputs (ASM_COMMENT_START, stream);
9419       return;
9420
9421     case '_':
9422       fputs (user_label_prefix, stream);
9423       return;
9424           
9425     case '|':
9426       fputs (REGISTER_PREFIX, stream);
9427       return;
9428
9429     case '?':
9430       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
9431         {
9432           if (TARGET_THUMB || current_insn_predicate != NULL)
9433             abort ();
9434
9435           fputs (arm_condition_codes[arm_current_cc], stream);
9436         }
9437       else if (current_insn_predicate)
9438         {
9439           enum arm_cond_code code;
9440
9441           if (TARGET_THUMB)
9442             abort ();
9443
9444           code = get_arm_condition_code (current_insn_predicate);
9445           fputs (arm_condition_codes[code], stream);
9446         }
9447       return;
9448
9449     case 'N':
9450       {
9451         REAL_VALUE_TYPE r;
9452         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9453         r = REAL_VALUE_NEGATE (r);
9454         fprintf (stream, "%s", fp_const_from_val (&r));
9455       }
9456       return;
9457
9458     case 'B':
9459       if (GET_CODE (x) == CONST_INT)
9460         {
9461           HOST_WIDE_INT val;
9462           val = ARM_SIGN_EXTEND (~INTVAL (x));
9463           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
9464         }
9465       else
9466         {
9467           putc ('~', stream);
9468           output_addr_const (stream, x);
9469         }
9470       return;
9471
9472     case 'i':
9473       fprintf (stream, "%s", arithmetic_instr (x, 1));
9474       return;
9475
9476     /* Truncate Cirrus shift counts.  */
9477     case 's':
9478       if (GET_CODE (x) == CONST_INT)
9479         {
9480           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
9481           return;
9482         }
9483       arm_print_operand (stream, x, 0);
9484       return;
9485
9486     case 'I':
9487       fprintf (stream, "%s", arithmetic_instr (x, 0));
9488       return;
9489
9490     case 'S':
9491       {
9492         HOST_WIDE_INT val;
9493         const char * shift = shift_op (x, &val);
9494
9495         if (shift)
9496           {
9497             fprintf (stream, ", %s ", shift_op (x, &val));
9498             if (val == -1)
9499               arm_print_operand (stream, XEXP (x, 1), 0);
9500             else
9501               {
9502                 fputc ('#', stream);
9503                 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
9504               }
9505           }
9506       }
9507       return;
9508
9509       /* An explanation of the 'Q', 'R' and 'H' register operands:
9510          
9511          In a pair of registers containing a DI or DF value the 'Q'
9512          operand returns the register number of the register containing
9513          the least signficant part of the value.  The 'R' operand returns
9514          the register number of the register containing the most
9515          significant part of the value.
9516          
9517          The 'H' operand returns the higher of the two register numbers.
9518          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
9519          same as the 'Q' operand, since the most signficant part of the
9520          value is held in the lower number register.  The reverse is true
9521          on systems where WORDS_BIG_ENDIAN is false.
9522          
9523          The purpose of these operands is to distinguish between cases
9524          where the endian-ness of the values is important (for example
9525          when they are added together), and cases where the endian-ness
9526          is irrelevant, but the order of register operations is important.
9527          For example when loading a value from memory into a register
9528          pair, the endian-ness does not matter.  Provided that the value
9529          from the lower memory address is put into the lower numbered
9530          register, and the value from the higher address is put into the
9531          higher numbered register, the load will work regardless of whether
9532          the value being loaded is big-wordian or little-wordian.  The
9533          order of the two register loads can matter however, if the address
9534          of the memory location is actually held in one of the registers
9535          being overwritten by the load.  */
9536     case 'Q':
9537       if (REGNO (x) > LAST_ARM_REGNUM)
9538         abort ();
9539       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
9540       return;
9541
9542     case 'R':
9543       if (REGNO (x) > LAST_ARM_REGNUM)
9544         abort ();
9545       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
9546       return;
9547
9548     case 'H':
9549       if (REGNO (x) > LAST_ARM_REGNUM)
9550         abort ();
9551       asm_fprintf (stream, "%r", REGNO (x) + 1);
9552       return;
9553
9554     case 'm':
9555       asm_fprintf (stream, "%r", 
9556                    GET_CODE (XEXP (x, 0)) == REG
9557                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
9558       return;
9559
9560     case 'M':
9561       asm_fprintf (stream, "{%r-%r}",
9562                    REGNO (x),
9563                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
9564       return;
9565
9566     case 'd':
9567       /* CONST_TRUE_RTX means always -- that's the default.  */
9568       if (x == const_true_rtx)
9569         return;
9570       
9571       if (TARGET_ARM)
9572         fputs (arm_condition_codes[get_arm_condition_code (x)],
9573                stream);
9574       else
9575         fputs (thumb_condition_code (x, 0), stream);
9576       return;
9577
9578     case 'D':
9579       /* CONST_TRUE_RTX means not always -- ie never.  We shouldn't ever
9580          want to do that.  */
9581       if (x == const_true_rtx)
9582         abort ();
9583
9584       if (TARGET_ARM)
9585         fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
9586                                   (get_arm_condition_code (x))],
9587                stream);
9588       else
9589         fputs (thumb_condition_code (x, 1), stream);
9590       return;
9591
9592
9593     /* Cirrus registers can be accessed in a variety of ways:
9594          single floating point (f)
9595          double floating point (d)
9596          32bit integer         (fx)
9597          64bit integer         (dx).  */
9598     case 'W':                   /* Cirrus register in F mode.  */
9599     case 'X':                   /* Cirrus register in D mode.  */
9600     case 'Y':                   /* Cirrus register in FX mode.  */
9601     case 'Z':                   /* Cirrus register in DX mode.  */
9602       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
9603         abort ();
9604
9605       fprintf (stream, "mv%s%s",
9606                code == 'W' ? "f"
9607                : code == 'X' ? "d"
9608                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
9609
9610       return;
9611
9612     /* Print cirrus register in the mode specified by the register's mode.  */
9613     case 'V':
9614       {
9615         int mode = GET_MODE (x);
9616
9617         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
9618           abort ();
9619
9620         fprintf (stream, "mv%s%s",
9621                  mode == DFmode ? "d"
9622                  : mode == SImode ? "fx"
9623                  : mode == DImode ? "dx"
9624                  : "f", reg_names[REGNO (x)] + 2);
9625
9626         return;
9627       }
9628
9629     default:
9630       if (x == 0)
9631         abort ();
9632
9633       if (GET_CODE (x) == REG)
9634         asm_fprintf (stream, "%r", REGNO (x));
9635       else if (GET_CODE (x) == MEM)
9636         {
9637           output_memory_reference_mode = GET_MODE (x);
9638           output_address (XEXP (x, 0));
9639         }
9640       else if (GET_CODE (x) == CONST_DOUBLE)
9641         fprintf (stream, "#%s", fp_immediate_constant (x));
9642       else if (GET_CODE (x) == NEG)
9643         abort (); /* This should never happen now.  */
9644       else
9645         {
9646           fputc ('#', stream);
9647           output_addr_const (stream, x);
9648         }
9649     }
9650 }
9651 \f
9652 #ifndef AOF_ASSEMBLER
9653 /* Target hook for assembling integer objects.  The ARM version needs to
9654    handle word-sized values specially.  */
9655
9656 static bool
9657 arm_assemble_integer (x, size, aligned_p)
9658      rtx x;
9659      unsigned int size;
9660      int aligned_p;
9661 {
9662   if (size == UNITS_PER_WORD && aligned_p)
9663     {
9664       fputs ("\t.word\t", asm_out_file);
9665       output_addr_const (asm_out_file, x);
9666
9667       /* Mark symbols as position independent.  We only do this in the
9668          .text segment, not in the .data segment. */
9669       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
9670           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
9671         {
9672           if (GET_CODE (x) == SYMBOL_REF
9673               && (CONSTANT_POOL_ADDRESS_P (x)
9674                   || ENCODED_SHORT_CALL_ATTR_P (XSTR (x, 0))))
9675             fputs ("(GOTOFF)", asm_out_file);
9676           else if (GET_CODE (x) == LABEL_REF)
9677             fputs ("(GOTOFF)", asm_out_file);
9678           else
9679             fputs ("(GOT)", asm_out_file);
9680         }
9681       fputc ('\n', asm_out_file);
9682       return true;
9683     }
9684
9685   return default_assemble_integer (x, size, aligned_p);
9686 }
9687 #endif
9688 \f
9689 /* A finite state machine takes care of noticing whether or not instructions
9690    can be conditionally executed, and thus decrease execution time and code
9691    size by deleting branch instructions.  The fsm is controlled by
9692    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
9693
9694 /* The state of the fsm controlling condition codes are:
9695    0: normal, do nothing special
9696    1: make ASM_OUTPUT_OPCODE not output this instruction
9697    2: make ASM_OUTPUT_OPCODE not output this instruction
9698    3: make instructions conditional
9699    4: make instructions conditional
9700
9701    State transitions (state->state by whom under condition):
9702    0 -> 1 final_prescan_insn if the `target' is a label
9703    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
9704    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
9705    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
9706    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
9707           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
9708    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
9709           (the target insn is arm_target_insn).
9710
9711    If the jump clobbers the conditions then we use states 2 and 4.
9712
9713    A similar thing can be done with conditional return insns.
9714
9715    XXX In case the `target' is an unconditional branch, this conditionalising
9716    of the instructions always reduces code size, but not always execution
9717    time.  But then, I want to reduce the code size to somewhere near what
9718    /bin/cc produces.  */
9719
9720 /* Returns the index of the ARM condition code string in
9721    `arm_condition_codes'.  COMPARISON should be an rtx like
9722    `(eq (...) (...))'.  */
9723
9724 static enum arm_cond_code
9725 get_arm_condition_code (comparison)
9726      rtx comparison;
9727 {
9728   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
9729   int code;
9730   enum rtx_code comp_code = GET_CODE (comparison);
9731
9732   if (GET_MODE_CLASS (mode) != MODE_CC)
9733     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
9734                            XEXP (comparison, 1));
9735
9736   switch (mode)
9737     {
9738     case CC_DNEmode: code = ARM_NE; goto dominance;
9739     case CC_DEQmode: code = ARM_EQ; goto dominance;
9740     case CC_DGEmode: code = ARM_GE; goto dominance;
9741     case CC_DGTmode: code = ARM_GT; goto dominance;
9742     case CC_DLEmode: code = ARM_LE; goto dominance;
9743     case CC_DLTmode: code = ARM_LT; goto dominance;
9744     case CC_DGEUmode: code = ARM_CS; goto dominance;
9745     case CC_DGTUmode: code = ARM_HI; goto dominance;
9746     case CC_DLEUmode: code = ARM_LS; goto dominance;
9747     case CC_DLTUmode: code = ARM_CC;
9748
9749     dominance:
9750       if (comp_code != EQ && comp_code != NE)
9751         abort ();
9752
9753       if (comp_code == EQ)
9754         return ARM_INVERSE_CONDITION_CODE (code);
9755       return code;
9756
9757     case CC_NOOVmode:
9758       switch (comp_code)
9759         {
9760         case NE: return ARM_NE;
9761         case EQ: return ARM_EQ;
9762         case GE: return ARM_PL;
9763         case LT: return ARM_MI;
9764         default: abort ();
9765         }
9766
9767     case CC_Zmode:
9768       switch (comp_code)
9769         {
9770         case NE: return ARM_NE;
9771         case EQ: return ARM_EQ;
9772         default: abort ();
9773         }
9774
9775     case CCFPEmode:
9776     case CCFPmode:
9777       /* These encodings assume that AC=1 in the FPA system control
9778          byte.  This allows us to handle all cases except UNEQ and
9779          LTGT.  */
9780       switch (comp_code)
9781         {
9782         case GE: return ARM_GE;
9783         case GT: return ARM_GT;
9784         case LE: return ARM_LS;
9785         case LT: return ARM_MI;
9786         case NE: return ARM_NE;
9787         case EQ: return ARM_EQ;
9788         case ORDERED: return ARM_VC;
9789         case UNORDERED: return ARM_VS;
9790         case UNLT: return ARM_LT;
9791         case UNLE: return ARM_LE;
9792         case UNGT: return ARM_HI;
9793         case UNGE: return ARM_PL;
9794           /* UNEQ and LTGT do not have a representation.  */
9795         case UNEQ: /* Fall through.  */
9796         case LTGT: /* Fall through.  */
9797         default: abort ();
9798         }
9799
9800     case CC_SWPmode:
9801       switch (comp_code)
9802         {
9803         case NE: return ARM_NE;
9804         case EQ: return ARM_EQ;
9805         case GE: return ARM_LE;
9806         case GT: return ARM_LT;
9807         case LE: return ARM_GE;
9808         case LT: return ARM_GT;
9809         case GEU: return ARM_LS;
9810         case GTU: return ARM_CC;
9811         case LEU: return ARM_CS;
9812         case LTU: return ARM_HI;
9813         default: abort ();
9814         }
9815
9816     case CC_Cmode:
9817       switch (comp_code)
9818       {
9819       case LTU: return ARM_CS;
9820       case GEU: return ARM_CC;
9821       default: abort ();
9822       }
9823       
9824     case CCmode:
9825       switch (comp_code)
9826         {
9827         case NE: return ARM_NE;
9828         case EQ: return ARM_EQ;
9829         case GE: return ARM_GE;
9830         case GT: return ARM_GT;
9831         case LE: return ARM_LE;
9832         case LT: return ARM_LT;
9833         case GEU: return ARM_CS;
9834         case GTU: return ARM_HI;
9835         case LEU: return ARM_LS;
9836         case LTU: return ARM_CC;
9837         default: abort ();
9838         }
9839
9840     default: abort ();
9841     }
9842
9843   abort ();
9844 }
9845
9846
9847 void
9848 arm_final_prescan_insn (insn)
9849      rtx insn;
9850 {
9851   /* BODY will hold the body of INSN.  */
9852   rtx body = PATTERN (insn);
9853
9854   /* This will be 1 if trying to repeat the trick, and things need to be
9855      reversed if it appears to fail.  */
9856   int reverse = 0;
9857
9858   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
9859      taken are clobbered, even if the rtl suggests otherwise.  It also
9860      means that we have to grub around within the jump expression to find
9861      out what the conditions are when the jump isn't taken.  */
9862   int jump_clobbers = 0;
9863   
9864   /* If we start with a return insn, we only succeed if we find another one.  */
9865   int seeking_return = 0;
9866   
9867   /* START_INSN will hold the insn from where we start looking.  This is the
9868      first insn after the following code_label if REVERSE is true.  */
9869   rtx start_insn = insn;
9870
9871   /* If in state 4, check if the target branch is reached, in order to
9872      change back to state 0.  */
9873   if (arm_ccfsm_state == 4)
9874     {
9875       if (insn == arm_target_insn)
9876         {
9877           arm_target_insn = NULL;
9878           arm_ccfsm_state = 0;
9879         }
9880       return;
9881     }
9882
9883   /* If in state 3, it is possible to repeat the trick, if this insn is an
9884      unconditional branch to a label, and immediately following this branch
9885      is the previous target label which is only used once, and the label this
9886      branch jumps to is not too far off.  */
9887   if (arm_ccfsm_state == 3)
9888     {
9889       if (simplejump_p (insn))
9890         {
9891           start_insn = next_nonnote_insn (start_insn);
9892           if (GET_CODE (start_insn) == BARRIER)
9893             {
9894               /* XXX Isn't this always a barrier?  */
9895               start_insn = next_nonnote_insn (start_insn);
9896             }
9897           if (GET_CODE (start_insn) == CODE_LABEL
9898               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
9899               && LABEL_NUSES (start_insn) == 1)
9900             reverse = TRUE;
9901           else
9902             return;
9903         }
9904       else if (GET_CODE (body) == RETURN)
9905         {
9906           start_insn = next_nonnote_insn (start_insn);
9907           if (GET_CODE (start_insn) == BARRIER)
9908             start_insn = next_nonnote_insn (start_insn);
9909           if (GET_CODE (start_insn) == CODE_LABEL
9910               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
9911               && LABEL_NUSES (start_insn) == 1)
9912             {
9913               reverse = TRUE;
9914               seeking_return = 1;
9915             }
9916           else
9917             return;
9918         }
9919       else
9920         return;
9921     }
9922
9923   if (arm_ccfsm_state != 0 && !reverse)
9924     abort ();
9925   if (GET_CODE (insn) != JUMP_INSN)
9926     return;
9927
9928   /* This jump might be paralleled with a clobber of the condition codes 
9929      the jump should always come first */
9930   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
9931     body = XVECEXP (body, 0, 0);
9932
9933 #if 0  
9934   /* If this is a conditional return then we don't want to know */
9935   if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
9936       && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
9937       && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
9938           || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
9939     return;
9940 #endif
9941
9942   if (reverse
9943       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
9944           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
9945     {
9946       int insns_skipped;
9947       int fail = FALSE, succeed = FALSE;
9948       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
9949       int then_not_else = TRUE;
9950       rtx this_insn = start_insn, label = 0;
9951
9952       /* If the jump cannot be done with one instruction, we cannot 
9953          conditionally execute the instruction in the inverse case.  */
9954       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
9955         {
9956           jump_clobbers = 1;
9957           return;
9958         }
9959       
9960       /* Register the insn jumped to.  */
9961       if (reverse)
9962         {
9963           if (!seeking_return)
9964             label = XEXP (SET_SRC (body), 0);
9965         }
9966       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
9967         label = XEXP (XEXP (SET_SRC (body), 1), 0);
9968       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
9969         {
9970           label = XEXP (XEXP (SET_SRC (body), 2), 0);
9971           then_not_else = FALSE;
9972         }
9973       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
9974         seeking_return = 1;
9975       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
9976         {
9977           seeking_return = 1;
9978           then_not_else = FALSE;
9979         }
9980       else
9981         abort ();
9982
9983       /* See how many insns this branch skips, and what kind of insns.  If all
9984          insns are okay, and the label or unconditional branch to the same
9985          label is not too far away, succeed.  */
9986       for (insns_skipped = 0;
9987            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
9988         {
9989           rtx scanbody;
9990
9991           this_insn = next_nonnote_insn (this_insn);
9992           if (!this_insn)
9993             break;
9994
9995           switch (GET_CODE (this_insn))
9996             {
9997             case CODE_LABEL:
9998               /* Succeed if it is the target label, otherwise fail since
9999                  control falls in from somewhere else.  */
10000               if (this_insn == label)
10001                 {
10002                   if (jump_clobbers)
10003                     {
10004                       arm_ccfsm_state = 2;
10005                       this_insn = next_nonnote_insn (this_insn);
10006                     }
10007                   else
10008                     arm_ccfsm_state = 1;
10009                   succeed = TRUE;
10010                 }
10011               else
10012                 fail = TRUE;
10013               break;
10014
10015             case BARRIER:
10016               /* Succeed if the following insn is the target label.
10017                  Otherwise fail.  
10018                  If return insns are used then the last insn in a function 
10019                  will be a barrier.  */
10020               this_insn = next_nonnote_insn (this_insn);
10021               if (this_insn && this_insn == label)
10022                 {
10023                   if (jump_clobbers)
10024                     {
10025                       arm_ccfsm_state = 2;
10026                       this_insn = next_nonnote_insn (this_insn);
10027                     }
10028                   else
10029                     arm_ccfsm_state = 1;
10030                   succeed = TRUE;
10031                 }
10032               else
10033                 fail = TRUE;
10034               break;
10035
10036             case CALL_INSN:
10037               /* If using 32-bit addresses the cc is not preserved over
10038                  calls.  */
10039               if (TARGET_APCS_32)
10040                 {
10041                   /* Succeed if the following insn is the target label,
10042                      or if the following two insns are a barrier and
10043                      the target label.  */
10044                   this_insn = next_nonnote_insn (this_insn);
10045                   if (this_insn && GET_CODE (this_insn) == BARRIER)
10046                     this_insn = next_nonnote_insn (this_insn);
10047
10048                   if (this_insn && this_insn == label
10049                       && insns_skipped < max_insns_skipped)
10050                     {
10051                       if (jump_clobbers)
10052                         {
10053                           arm_ccfsm_state = 2;
10054                           this_insn = next_nonnote_insn (this_insn);
10055                         }
10056                       else
10057                         arm_ccfsm_state = 1;
10058                       succeed = TRUE;
10059                     }
10060                   else
10061                     fail = TRUE;
10062                 }
10063               break;
10064
10065             case JUMP_INSN:
10066               /* If this is an unconditional branch to the same label, succeed.
10067                  If it is to another label, do nothing.  If it is conditional,
10068                  fail.  */
10069               /* XXX Probably, the tests for SET and the PC are unnecessary.  */
10070
10071               scanbody = PATTERN (this_insn);
10072               if (GET_CODE (scanbody) == SET
10073                   && GET_CODE (SET_DEST (scanbody)) == PC)
10074                 {
10075                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
10076                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
10077                     {
10078                       arm_ccfsm_state = 2;
10079                       succeed = TRUE;
10080                     }
10081                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
10082                     fail = TRUE;
10083                 }
10084               /* Fail if a conditional return is undesirable (eg on a
10085                  StrongARM), but still allow this if optimizing for size.  */
10086               else if (GET_CODE (scanbody) == RETURN
10087                        && !use_return_insn (TRUE)
10088                        && !optimize_size)
10089                 fail = TRUE;
10090               else if (GET_CODE (scanbody) == RETURN
10091                        && seeking_return)
10092                 {
10093                   arm_ccfsm_state = 2;
10094                   succeed = TRUE;
10095                 }
10096               else if (GET_CODE (scanbody) == PARALLEL)
10097                 {
10098                   switch (get_attr_conds (this_insn))
10099                     {
10100                     case CONDS_NOCOND:
10101                       break;
10102                     default:
10103                       fail = TRUE;
10104                       break;
10105                     }
10106                 }
10107               else
10108                 fail = TRUE;    /* Unrecognized jump (eg epilogue).  */
10109
10110               break;
10111
10112             case INSN:
10113               /* Instructions using or affecting the condition codes make it
10114                  fail.  */
10115               scanbody = PATTERN (this_insn);
10116               if (!(GET_CODE (scanbody) == SET
10117                     || GET_CODE (scanbody) == PARALLEL)
10118                   || get_attr_conds (this_insn) != CONDS_NOCOND)
10119                 fail = TRUE;
10120
10121               /* A conditional cirrus instruction must be followed by
10122                  a non Cirrus instruction.  However, since we
10123                  conditionalize instructions in this function and by
10124                  the time we get here we can't add instructions
10125                  (nops), because shorten_branches() has already been
10126                  called, we will disable conditionalizing Cirrus
10127                  instructions to be safe.  */
10128               if (GET_CODE (scanbody) != USE
10129                   && GET_CODE (scanbody) != CLOBBER
10130                   && get_attr_cirrus (this_insn) != CIRRUS_NO)
10131                 fail = TRUE;
10132               break;
10133
10134             default:
10135               break;
10136             }
10137         }
10138       if (succeed)
10139         {
10140           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
10141             arm_target_label = CODE_LABEL_NUMBER (label);
10142           else if (seeking_return || arm_ccfsm_state == 2)
10143             {
10144               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
10145                 {
10146                   this_insn = next_nonnote_insn (this_insn);
10147                   if (this_insn && (GET_CODE (this_insn) == BARRIER
10148                                     || GET_CODE (this_insn) == CODE_LABEL))
10149                     abort ();
10150                 }
10151               if (!this_insn)
10152                 {
10153                   /* Oh, dear! we ran off the end.. give up */
10154                   recog (PATTERN (insn), insn, NULL);
10155                   arm_ccfsm_state = 0;
10156                   arm_target_insn = NULL;
10157                   return;
10158                 }
10159               arm_target_insn = this_insn;
10160             }
10161           else
10162             abort ();
10163           if (jump_clobbers)
10164             {
10165               if (reverse)
10166                 abort ();
10167               arm_current_cc = 
10168                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
10169                                                             0), 0), 1));
10170               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
10171                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
10172               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
10173                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
10174             }
10175           else
10176             {
10177               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
10178                  what it was.  */
10179               if (!reverse)
10180                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
10181                                                                0));
10182             }
10183
10184           if (reverse || then_not_else)
10185             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
10186         }
10187       
10188       /* Restore recog_data (getting the attributes of other insns can
10189          destroy this array, but final.c assumes that it remains intact
10190          across this call; since the insn has been recognized already we
10191          call recog direct).  */
10192       recog (PATTERN (insn), insn, NULL);
10193     }
10194 }
10195
10196 /* Returns true if REGNO is a valid register
10197    for holding a quantity of tyoe MODE.  */
10198
10199 int
10200 arm_hard_regno_mode_ok (regno, mode)
10201      unsigned int regno;
10202      enum machine_mode mode;
10203 {
10204   if (GET_MODE_CLASS (mode) == MODE_CC)
10205     return regno == CC_REGNUM;
10206   
10207   if (TARGET_THUMB)
10208     /* For the Thumb we only allow values bigger than SImode in
10209        registers 0 - 6, so that there is always a second low
10210        register available to hold the upper part of the value.
10211        We probably we ought to ensure that the register is the
10212        start of an even numbered register pair.  */
10213     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
10214
10215   if (IS_CIRRUS_REGNUM (regno))
10216     /* We have outlawed SI values in Cirrus registers because they
10217        reside in the lower 32 bits, but SF values reside in the
10218        upper 32 bits.  This causes gcc all sorts of grief.  We can't
10219        even split the registers into pairs because Cirrus SI values
10220        get sign extended to 64bits-- aldyh.  */
10221     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
10222
10223   if (regno <= LAST_ARM_REGNUM)
10224     /* We allow any value to be stored in the general regisetrs.  */
10225     return 1;
10226
10227   if (   regno == FRAME_POINTER_REGNUM
10228       || regno == ARG_POINTER_REGNUM)
10229     /* We only allow integers in the fake hard registers.  */
10230     return GET_MODE_CLASS (mode) == MODE_INT;
10231
10232   /* The only registers left are the FPU registers
10233      which we only allow to hold FP values.  */
10234   return GET_MODE_CLASS (mode) == MODE_FLOAT
10235     && regno >= FIRST_ARM_FP_REGNUM
10236     && regno <= LAST_ARM_FP_REGNUM;
10237 }
10238
10239 int
10240 arm_regno_class (regno)
10241      int regno;
10242 {
10243   if (TARGET_THUMB)
10244     {
10245       if (regno == STACK_POINTER_REGNUM)
10246         return STACK_REG;
10247       if (regno == CC_REGNUM)
10248         return CC_REG;
10249       if (regno < 8)
10250         return LO_REGS;
10251       return HI_REGS;
10252     }
10253
10254   if (   regno <= LAST_ARM_REGNUM
10255       || regno == FRAME_POINTER_REGNUM
10256       || regno == ARG_POINTER_REGNUM)
10257     return GENERAL_REGS;
10258   
10259   if (regno == CC_REGNUM)
10260     return NO_REGS;
10261
10262   if (IS_CIRRUS_REGNUM (regno))
10263     return CIRRUS_REGS;
10264
10265   return FPU_REGS;
10266 }
10267
10268 /* Handle a special case when computing the offset
10269    of an argument from the frame pointer.  */
10270
10271 int
10272 arm_debugger_arg_offset (value, addr)
10273      int value;
10274      rtx addr;
10275 {
10276   rtx insn;
10277
10278   /* We are only interested if dbxout_parms() failed to compute the offset.  */
10279   if (value != 0)
10280     return 0;
10281
10282   /* We can only cope with the case where the address is held in a register.  */
10283   if (GET_CODE (addr) != REG)
10284     return 0;
10285
10286   /* If we are using the frame pointer to point at the argument, then
10287      an offset of 0 is correct.  */
10288   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
10289     return 0;
10290   
10291   /* If we are using the stack pointer to point at the
10292      argument, then an offset of 0 is correct.  */
10293   if ((TARGET_THUMB || !frame_pointer_needed)
10294       && REGNO (addr) == SP_REGNUM)
10295     return 0;
10296   
10297   /* Oh dear.  The argument is pointed to by a register rather
10298      than being held in a register, or being stored at a known
10299      offset from the frame pointer.  Since GDB only understands
10300      those two kinds of argument we must translate the address
10301      held in the register into an offset from the frame pointer.
10302      We do this by searching through the insns for the function
10303      looking to see where this register gets its value.  If the
10304      register is initialized from the frame pointer plus an offset
10305      then we are in luck and we can continue, otherwise we give up.
10306      
10307      This code is exercised by producing debugging information
10308      for a function with arguments like this:
10309      
10310            double func (double a, double b, int c, double d) {return d;}
10311      
10312      Without this code the stab for parameter 'd' will be set to
10313      an offset of 0 from the frame pointer, rather than 8.  */
10314
10315   /* The if() statement says:
10316
10317      If the insn is a normal instruction
10318      and if the insn is setting the value in a register
10319      and if the register being set is the register holding the address of the argument
10320      and if the address is computing by an addition
10321      that involves adding to a register
10322      which is the frame pointer
10323      a constant integer
10324
10325      then... */
10326   
10327   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10328     {
10329       if (   GET_CODE (insn) == INSN 
10330           && GET_CODE (PATTERN (insn)) == SET
10331           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
10332           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
10333           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
10334           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
10335           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
10336              )
10337         {
10338           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
10339           
10340           break;
10341         }
10342     }
10343   
10344   if (value == 0)
10345     {
10346       debug_rtx (addr);
10347       warning ("unable to compute real location of stacked parameter");
10348       value = 8; /* XXX magic hack */
10349     }
10350
10351   return value;
10352 }
10353 \f
10354 /* Recursively search through all of the blocks in a function
10355    checking to see if any of the variables created in that
10356    function match the RTX called 'orig'.  If they do then
10357    replace them with the RTX called 'new'.  */
10358
10359 static void
10360 replace_symbols_in_block (block, orig, new)
10361      tree block;
10362      rtx orig;
10363      rtx new;
10364 {
10365   for (; block; block = BLOCK_CHAIN (block))
10366     {
10367       tree sym;
10368       
10369       if (!TREE_USED (block))
10370         continue;
10371
10372       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
10373         {
10374           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
10375               || DECL_IGNORED_P (sym)
10376               || TREE_CODE (sym) != VAR_DECL
10377               || DECL_EXTERNAL (sym)
10378               || !rtx_equal_p (DECL_RTL (sym), orig)
10379               )
10380             continue;
10381
10382           SET_DECL_RTL (sym, new);
10383         }
10384       
10385       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
10386     }
10387 }
10388
10389 /* Return the number (counting from 0) of
10390    the least significant set bit in MASK.  */
10391
10392 #ifdef __GNUC__
10393 inline
10394 #endif
10395 static int
10396 number_of_first_bit_set (mask)
10397      int mask;
10398 {
10399   int bit;
10400
10401   for (bit = 0;
10402        (mask & (1 << bit)) == 0;
10403        ++bit)
10404     continue;
10405
10406   return bit;
10407 }
10408
10409 /* Generate code to return from a thumb function.
10410    If 'reg_containing_return_addr' is -1, then the return address is
10411    actually on the stack, at the stack pointer.  */
10412 static void
10413 thumb_exit (f, reg_containing_return_addr, eh_ofs)
10414      FILE * f;
10415      int    reg_containing_return_addr;
10416      rtx    eh_ofs;
10417 {
10418   unsigned regs_available_for_popping;
10419   unsigned regs_to_pop;
10420   int pops_needed;
10421   unsigned available;
10422   unsigned required;
10423   int mode;
10424   int size;
10425   int restore_a4 = FALSE;
10426
10427   /* Compute the registers we need to pop.  */
10428   regs_to_pop = 0;
10429   pops_needed = 0;
10430
10431   /* There is an assumption here, that if eh_ofs is not NULL, the
10432      normal return address will have been pushed.  */
10433   if (reg_containing_return_addr == -1 || eh_ofs)
10434     {
10435       /* When we are generating a return for __builtin_eh_return, 
10436          reg_containing_return_addr must specify the return regno.  */
10437       if (eh_ofs && reg_containing_return_addr == -1)
10438         abort ();
10439
10440       regs_to_pop |= 1 << LR_REGNUM;
10441       ++pops_needed;
10442     }
10443
10444   if (TARGET_BACKTRACE)
10445     {
10446       /* Restore the (ARM) frame pointer and stack pointer.  */
10447       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
10448       pops_needed += 2;
10449     }
10450
10451   /* If there is nothing to pop then just emit the BX instruction and
10452      return.  */
10453   if (pops_needed == 0)
10454     {
10455       if (eh_ofs)
10456         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
10457
10458       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
10459       return;
10460     }
10461   /* Otherwise if we are not supporting interworking and we have not created
10462      a backtrace structure and the function was not entered in ARM mode then
10463      just pop the return address straight into the PC.  */
10464   else if (!TARGET_INTERWORK
10465            && !TARGET_BACKTRACE
10466            && !is_called_in_ARM_mode (current_function_decl))
10467     {
10468       if (eh_ofs)
10469         {
10470           asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
10471           asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
10472           asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
10473         }
10474       else
10475         asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
10476
10477       return;
10478     }
10479
10480   /* Find out how many of the (return) argument registers we can corrupt.  */
10481   regs_available_for_popping = 0;
10482
10483   /* If returning via __builtin_eh_return, the bottom three registers
10484      all contain information needed for the return.  */
10485   if (eh_ofs)
10486     size = 12;
10487   else
10488     {
10489 #ifdef RTX_CODE
10490       /* If we can deduce the registers used from the function's
10491          return value.  This is more reliable that examining
10492          regs_ever_live[] because that will be set if the register is
10493          ever used in the function, not just if the register is used
10494          to hold a return value.  */
10495
10496       if (current_function_return_rtx != 0)
10497         mode = GET_MODE (current_function_return_rtx);
10498       else
10499 #endif
10500         mode = DECL_MODE (DECL_RESULT (current_function_decl));
10501
10502       size = GET_MODE_SIZE (mode);
10503
10504       if (size == 0)
10505         {
10506           /* In a void function we can use any argument register.
10507              In a function that returns a structure on the stack
10508              we can use the second and third argument registers.  */
10509           if (mode == VOIDmode)
10510             regs_available_for_popping =
10511               (1 << ARG_REGISTER (1))
10512               | (1 << ARG_REGISTER (2))
10513               | (1 << ARG_REGISTER (3));
10514           else
10515             regs_available_for_popping =
10516               (1 << ARG_REGISTER (2))
10517               | (1 << ARG_REGISTER (3));
10518         }
10519       else if (size <= 4)
10520         regs_available_for_popping =
10521           (1 << ARG_REGISTER (2))
10522           | (1 << ARG_REGISTER (3));
10523       else if (size <= 8)
10524         regs_available_for_popping =
10525           (1 << ARG_REGISTER (3));
10526     }
10527
10528   /* Match registers to be popped with registers into which we pop them.  */
10529   for (available = regs_available_for_popping,
10530        required  = regs_to_pop;
10531        required != 0 && available != 0;
10532        available &= ~(available & - available),
10533        required  &= ~(required  & - required))
10534     -- pops_needed;
10535
10536   /* If we have any popping registers left over, remove them.  */
10537   if (available > 0)
10538     regs_available_for_popping &= ~available;
10539   
10540   /* Otherwise if we need another popping register we can use
10541      the fourth argument register.  */
10542   else if (pops_needed)
10543     {
10544       /* If we have not found any free argument registers and
10545          reg a4 contains the return address, we must move it.  */
10546       if (regs_available_for_popping == 0
10547           && reg_containing_return_addr == LAST_ARG_REGNUM)
10548         {
10549           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
10550           reg_containing_return_addr = LR_REGNUM;
10551         }
10552       else if (size > 12)
10553         {
10554           /* Register a4 is being used to hold part of the return value,
10555              but we have dire need of a free, low register.  */
10556           restore_a4 = TRUE;
10557           
10558           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
10559         }
10560       
10561       if (reg_containing_return_addr != LAST_ARG_REGNUM)
10562         {
10563           /* The fourth argument register is available.  */
10564           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
10565           
10566           --pops_needed;
10567         }
10568     }
10569
10570   /* Pop as many registers as we can.  */
10571   thumb_pushpop (f, regs_available_for_popping, FALSE);
10572
10573   /* Process the registers we popped.  */
10574   if (reg_containing_return_addr == -1)
10575     {
10576       /* The return address was popped into the lowest numbered register.  */
10577       regs_to_pop &= ~(1 << LR_REGNUM);
10578       
10579       reg_containing_return_addr =
10580         number_of_first_bit_set (regs_available_for_popping);
10581
10582       /* Remove this register for the mask of available registers, so that
10583          the return address will not be corrupted by further pops.  */
10584       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
10585     }
10586
10587   /* If we popped other registers then handle them here.  */
10588   if (regs_available_for_popping)
10589     {
10590       int frame_pointer;
10591       
10592       /* Work out which register currently contains the frame pointer.  */
10593       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
10594
10595       /* Move it into the correct place.  */
10596       asm_fprintf (f, "\tmov\t%r, %r\n",
10597                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
10598
10599       /* (Temporarily) remove it from the mask of popped registers.  */
10600       regs_available_for_popping &= ~(1 << frame_pointer);
10601       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
10602       
10603       if (regs_available_for_popping)
10604         {
10605           int stack_pointer;
10606           
10607           /* We popped the stack pointer as well,
10608              find the register that contains it.  */
10609           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
10610
10611           /* Move it into the stack register.  */
10612           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
10613           
10614           /* At this point we have popped all necessary registers, so
10615              do not worry about restoring regs_available_for_popping
10616              to its correct value:
10617
10618              assert (pops_needed == 0)
10619              assert (regs_available_for_popping == (1 << frame_pointer))
10620              assert (regs_to_pop == (1 << STACK_POINTER))  */
10621         }
10622       else
10623         {
10624           /* Since we have just move the popped value into the frame
10625              pointer, the popping register is available for reuse, and
10626              we know that we still have the stack pointer left to pop.  */
10627           regs_available_for_popping |= (1 << frame_pointer);
10628         }
10629     }
10630   
10631   /* If we still have registers left on the stack, but we no longer have
10632      any registers into which we can pop them, then we must move the return
10633      address into the link register and make available the register that
10634      contained it.  */
10635   if (regs_available_for_popping == 0 && pops_needed > 0)
10636     {
10637       regs_available_for_popping |= 1 << reg_containing_return_addr;
10638       
10639       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
10640                    reg_containing_return_addr);
10641       
10642       reg_containing_return_addr = LR_REGNUM;
10643     }
10644
10645   /* If we have registers left on the stack then pop some more.
10646      We know that at most we will want to pop FP and SP.  */
10647   if (pops_needed > 0)
10648     {
10649       int  popped_into;
10650       int  move_to;
10651       
10652       thumb_pushpop (f, regs_available_for_popping, FALSE);
10653
10654       /* We have popped either FP or SP.
10655          Move whichever one it is into the correct register.  */
10656       popped_into = number_of_first_bit_set (regs_available_for_popping);
10657       move_to     = number_of_first_bit_set (regs_to_pop);
10658
10659       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
10660
10661       regs_to_pop &= ~(1 << move_to);
10662
10663       --pops_needed;
10664     }
10665   
10666   /* If we still have not popped everything then we must have only
10667      had one register available to us and we are now popping the SP.  */
10668   if (pops_needed > 0)
10669     {
10670       int  popped_into;
10671       
10672       thumb_pushpop (f, regs_available_for_popping, FALSE);
10673
10674       popped_into = number_of_first_bit_set (regs_available_for_popping);
10675
10676       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
10677       /*
10678         assert (regs_to_pop == (1 << STACK_POINTER))
10679         assert (pops_needed == 1)
10680       */
10681     }
10682
10683   /* If necessary restore the a4 register.  */
10684   if (restore_a4)
10685     {
10686       if (reg_containing_return_addr != LR_REGNUM)
10687         {
10688           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
10689           reg_containing_return_addr = LR_REGNUM;
10690         }
10691     
10692       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
10693     }
10694
10695   if (eh_ofs)
10696     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
10697
10698   /* Return to caller.  */
10699   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
10700 }
10701
10702 /* Emit code to push or pop registers to or from the stack.  */
10703
10704 static void
10705 thumb_pushpop (f, mask, push)
10706      FILE * f;
10707      int mask;
10708      int push;
10709 {
10710   int regno;
10711   int lo_mask = mask & 0xFF;
10712
10713   if (lo_mask == 0 && !push && (mask & (1 << 15)))
10714     {
10715       /* Special case.  Do not generate a POP PC statement here, do it in
10716          thumb_exit() */
10717       thumb_exit (f, -1, NULL_RTX);
10718       return;
10719     }
10720       
10721   fprintf (f, "\t%s\t{", push ? "push" : "pop");
10722
10723   /* Look at the low registers first.  */
10724   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
10725     {
10726       if (lo_mask & 1)
10727         {
10728           asm_fprintf (f, "%r", regno);
10729           
10730           if ((lo_mask & ~1) != 0)
10731             fprintf (f, ", ");
10732         }
10733     }
10734   
10735   if (push && (mask & (1 << LR_REGNUM)))
10736     {
10737       /* Catch pushing the LR.  */
10738       if (mask & 0xFF)
10739         fprintf (f, ", ");
10740       
10741       asm_fprintf (f, "%r", LR_REGNUM);
10742     }
10743   else if (!push && (mask & (1 << PC_REGNUM)))
10744     {
10745       /* Catch popping the PC.  */
10746       if (TARGET_INTERWORK || TARGET_BACKTRACE)
10747         {
10748           /* The PC is never poped directly, instead
10749              it is popped into r3 and then BX is used.  */
10750           fprintf (f, "}\n");
10751
10752           thumb_exit (f, -1, NULL_RTX);
10753
10754           return;
10755         }
10756       else
10757         {
10758           if (mask & 0xFF)
10759             fprintf (f, ", ");
10760           
10761           asm_fprintf (f, "%r", PC_REGNUM);
10762         }
10763     }
10764        
10765   fprintf (f, "}\n");
10766 }
10767 \f
10768 void
10769 thumb_final_prescan_insn (insn)
10770      rtx insn;
10771 {
10772   if (flag_print_asm_name)
10773     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
10774                  INSN_ADDRESSES (INSN_UID (insn)));
10775 }
10776
10777 int
10778 thumb_shiftable_const (val)
10779      unsigned HOST_WIDE_INT val;
10780 {
10781   unsigned HOST_WIDE_INT mask = 0xff;
10782   int i;
10783
10784   if (val == 0) /* XXX */
10785     return 0;
10786   
10787   for (i = 0; i < 25; i++)
10788     if ((val & (mask << i)) == val)
10789       return 1;
10790
10791   return 0;
10792 }
10793
10794 /* Returns nonzero if the current function contains,
10795    or might contain a far jump.  */
10796
10797 int
10798 thumb_far_jump_used_p (in_prologue)
10799      int in_prologue;
10800 {
10801   rtx insn;
10802
10803   /* This test is only important for leaf functions.  */
10804   /* assert (!leaf_function_p ()); */
10805   
10806   /* If we have already decided that far jumps may be used,
10807      do not bother checking again, and always return true even if
10808      it turns out that they are not being used.  Once we have made
10809      the decision that far jumps are present (and that hence the link
10810      register will be pushed onto the stack) we cannot go back on it.  */
10811   if (cfun->machine->far_jump_used)
10812     return 1;
10813
10814   /* If this function is not being called from the prologue/epilogue
10815      generation code then it must be being called from the
10816      INITIAL_ELIMINATION_OFFSET macro.  */
10817   if (!in_prologue)
10818     {
10819       /* In this case we know that we are being asked about the elimination
10820          of the arg pointer register.  If that register is not being used,
10821          then there are no arguments on the stack, and we do not have to
10822          worry that a far jump might force the prologue to push the link
10823          register, changing the stack offsets.  In this case we can just
10824          return false, since the presence of far jumps in the function will
10825          not affect stack offsets.
10826
10827          If the arg pointer is live (or if it was live, but has now been
10828          eliminated and so set to dead) then we do have to test to see if
10829          the function might contain a far jump.  This test can lead to some
10830          false negatives, since before reload is completed, then length of
10831          branch instructions is not known, so gcc defaults to returning their
10832          longest length, which in turn sets the far jump attribute to true.
10833
10834          A false negative will not result in bad code being generated, but it
10835          will result in a needless push and pop of the link register.  We
10836          hope that this does not occur too often.  */
10837       if (regs_ever_live [ARG_POINTER_REGNUM])
10838         cfun->machine->arg_pointer_live = 1;
10839       else if (!cfun->machine->arg_pointer_live)
10840         return 0;
10841     }
10842
10843   /* Check to see if the function contains a branch
10844      insn with the far jump attribute set.  */
10845   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10846     {
10847       if (GET_CODE (insn) == JUMP_INSN
10848           /* Ignore tablejump patterns.  */
10849           && GET_CODE (PATTERN (insn)) != ADDR_VEC
10850           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
10851           && get_attr_far_jump (insn) == FAR_JUMP_YES
10852           )
10853         {
10854           /* Record the fact that we have decied that
10855              the function does use far jumps.  */
10856           cfun->machine->far_jump_used = 1;
10857           return 1;
10858         }
10859     }
10860   
10861   return 0;
10862 }
10863
10864 /* Return nonzero if FUNC must be entered in ARM mode.  */
10865
10866 int
10867 is_called_in_ARM_mode (func)
10868      tree func;
10869 {
10870   if (TREE_CODE (func) != FUNCTION_DECL)
10871     abort ();
10872
10873   /* Ignore the problem about functions whoes address is taken.  */
10874   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
10875     return TRUE;
10876
10877 #ifdef ARM_PE 
10878   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
10879 #else
10880   return FALSE;
10881 #endif
10882 }
10883
10884 /* The bits which aren't usefully expanded as rtl. */
10885
10886 const char *
10887 thumb_unexpanded_epilogue ()
10888 {
10889   int regno;
10890   int live_regs_mask = 0;
10891   int high_regs_pushed = 0;
10892   int leaf_function = leaf_function_p ();
10893   int had_to_push_lr;
10894   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
10895
10896   if (return_used_this_function)
10897     return "";
10898
10899   if (IS_NAKED (arm_current_func_type ()))
10900     return "";
10901
10902   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10903     if (THUMB_REG_PUSHED_P (regno))
10904       live_regs_mask |= 1 << regno;
10905
10906   for (regno = 8; regno < 13; regno++)
10907     if (THUMB_REG_PUSHED_P (regno))
10908       high_regs_pushed++;
10909
10910   /* The prolog may have pushed some high registers to use as
10911      work registers.  eg the testuite file:
10912      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
10913      compiles to produce:
10914         push    {r4, r5, r6, r7, lr}
10915         mov     r7, r9
10916         mov     r6, r8
10917         push    {r6, r7}
10918      as part of the prolog.  We have to undo that pushing here.  */
10919   
10920   if (high_regs_pushed)
10921     {
10922       int mask = live_regs_mask;
10923       int next_hi_reg;
10924       int size;
10925       int mode;
10926        
10927 #ifdef RTX_CODE
10928       /* If we can deduce the registers used from the function's return value.
10929          This is more reliable that examining regs_ever_live[] because that
10930          will be set if the register is ever used in the function, not just if
10931          the register is used to hold a return value.  */
10932
10933       if (current_function_return_rtx != 0)
10934         mode = GET_MODE (current_function_return_rtx);
10935       else
10936 #endif
10937         mode = DECL_MODE (DECL_RESULT (current_function_decl));
10938
10939       size = GET_MODE_SIZE (mode);
10940
10941       /* Unless we are returning a type of size > 12 register r3 is
10942          available.  */
10943       if (size < 13)
10944         mask |=  1 << 3;
10945
10946       if (mask == 0)
10947         /* Oh dear!  We have no low registers into which we can pop
10948            high registers!  */
10949         internal_error
10950           ("no low registers available for popping high registers");
10951       
10952       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
10953         if (THUMB_REG_PUSHED_P (next_hi_reg))
10954           break;
10955
10956       while (high_regs_pushed)
10957         {
10958           /* Find lo register(s) into which the high register(s) can
10959              be popped.  */
10960           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10961             {
10962               if (mask & (1 << regno))
10963                 high_regs_pushed--;
10964               if (high_regs_pushed == 0)
10965                 break;
10966             }
10967
10968           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
10969
10970           /* Pop the values into the low register(s). */
10971           thumb_pushpop (asm_out_file, mask, 0);
10972
10973           /* Move the value(s) into the high registers.  */
10974           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
10975             {
10976               if (mask & (1 << regno))
10977                 {
10978                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
10979                                regno);
10980                   
10981                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
10982                     if (THUMB_REG_PUSHED_P (next_hi_reg))
10983                       break;
10984                 }
10985             }
10986         }
10987     }
10988
10989   had_to_push_lr = (live_regs_mask || !leaf_function
10990                     || thumb_far_jump_used_p (1));
10991   
10992   if (TARGET_BACKTRACE
10993       && ((live_regs_mask & 0xFF) == 0)
10994       && regs_ever_live [LAST_ARG_REGNUM] != 0)
10995     {
10996       /* The stack backtrace structure creation code had to
10997          push R7 in order to get a work register, so we pop
10998          it now.   */
10999       live_regs_mask |= (1 << LAST_LO_REGNUM);
11000     }
11001   
11002   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
11003     {
11004       if (had_to_push_lr
11005           && !is_called_in_ARM_mode (current_function_decl)
11006           && !eh_ofs)
11007         live_regs_mask |= 1 << PC_REGNUM;
11008
11009       /* Either no argument registers were pushed or a backtrace
11010          structure was created which includes an adjusted stack
11011          pointer, so just pop everything.  */
11012       if (live_regs_mask)
11013         thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
11014       
11015       if (eh_ofs)
11016         thumb_exit (asm_out_file, 2, eh_ofs);
11017       /* We have either just popped the return address into the
11018          PC or it is was kept in LR for the entire function or
11019          it is still on the stack because we do not want to
11020          return by doing a pop {pc}.  */
11021       else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
11022         thumb_exit (asm_out_file,
11023                     (had_to_push_lr
11024                      && is_called_in_ARM_mode (current_function_decl)) ?
11025                     -1 : LR_REGNUM, NULL_RTX);
11026     }
11027   else
11028     {
11029       /* Pop everything but the return address.  */
11030       live_regs_mask &= ~(1 << PC_REGNUM);
11031       
11032       if (live_regs_mask)
11033         thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
11034
11035       if (had_to_push_lr)
11036         /* Get the return address into a temporary register.  */
11037         thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0);
11038       
11039       /* Remove the argument registers that were pushed onto the stack.  */
11040       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
11041                    SP_REGNUM, SP_REGNUM,
11042                    current_function_pretend_args_size);
11043       
11044       if (eh_ofs)
11045         thumb_exit (asm_out_file, 2, eh_ofs);
11046       else
11047         thumb_exit (asm_out_file,
11048                     had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
11049     }
11050
11051   return "";
11052 }
11053
11054 /* Functions to save and restore machine-specific function data.  */
11055
11056 static struct machine_function *
11057 arm_init_machine_status ()
11058 {
11059   struct machine_function *machine;
11060   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
11061
11062 #if ARM_FT_UNKNOWN != 0  
11063   machine->func_type = ARM_FT_UNKNOWN;
11064 #endif
11065   return machine;
11066 }
11067
11068 /* Return an RTX indicating where the return address to the
11069    calling function can be found.  */
11070
11071 rtx
11072 arm_return_addr (count, frame)
11073      int count;
11074      rtx frame ATTRIBUTE_UNUSED;
11075 {
11076   if (count != 0)
11077     return NULL_RTX;
11078
11079   if (TARGET_APCS_32)
11080     return get_hard_reg_initial_val (Pmode, LR_REGNUM);
11081   else
11082     {
11083       rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
11084                             GEN_INT (RETURN_ADDR_MASK26));
11085       return get_func_hard_reg_initial_val (cfun, lr);
11086     }
11087 }
11088
11089 /* Do anything needed before RTL is emitted for each function.  */
11090
11091 void
11092 arm_init_expanders ()
11093 {
11094   /* Arrange to initialize and mark the machine per-function status.  */
11095   init_machine_status = arm_init_machine_status;
11096 }
11097
11098 HOST_WIDE_INT
11099 thumb_get_frame_size ()
11100 {
11101   int regno;
11102
11103   int base_size = ROUND_UP_WORD (get_frame_size ());
11104   int count_regs = 0;
11105   int entry_size = 0;
11106   int leaf;
11107
11108   if (! TARGET_THUMB)
11109     abort ();
11110
11111   if (! TARGET_ATPCS)
11112     return base_size;
11113
11114   /* We need to know if we are a leaf function.  Unfortunately, it
11115      is possible to be called after start_sequence has been called,
11116      which causes get_insns to return the insns for the sequence,
11117      not the function, which will cause leaf_function_p to return
11118      the incorrect result.
11119
11120      To work around this, we cache the computed frame size.  This
11121      works because we will only be calling RTL expanders that need
11122      to know about leaf functions once reload has completed, and the
11123      frame size cannot be changed after that time, so we can safely
11124      use the cached value.  */
11125
11126   if (reload_completed)
11127     return cfun->machine->frame_size;
11128
11129   leaf = leaf_function_p ();
11130
11131   /* A leaf function does not need any stack alignment if it has nothing
11132      on the stack.  */
11133   if (leaf && base_size == 0)
11134     {
11135       cfun->machine->frame_size = 0;
11136       return 0;
11137     }
11138
11139   /* We know that SP will be word aligned on entry, and we must
11140      preserve that condition at any subroutine call.  But those are
11141      the only constraints.  */
11142
11143   /* Space for variadic functions.  */
11144   if (current_function_pretend_args_size)
11145     entry_size += current_function_pretend_args_size;
11146
11147   /* Space for pushed lo registers.  */
11148   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
11149     if (THUMB_REG_PUSHED_P (regno))
11150       count_regs++;
11151
11152   /* Space for backtrace structure.  */
11153   if (TARGET_BACKTRACE)
11154     {
11155       if (count_regs == 0 && regs_ever_live[LAST_ARG_REGNUM] != 0)
11156         entry_size += 20;
11157       else
11158         entry_size += 16;
11159     }
11160
11161   if (count_regs || !leaf || thumb_far_jump_used_p (1))
11162     count_regs++;       /* LR */
11163
11164   entry_size += count_regs * 4;
11165   count_regs = 0;
11166
11167   /* Space for pushed hi regs.  */
11168   for (regno = 8; regno < 13; regno++)
11169     if (THUMB_REG_PUSHED_P (regno))
11170       count_regs++;
11171
11172   entry_size += count_regs * 4;
11173
11174   if ((entry_size + base_size + current_function_outgoing_args_size) & 7)
11175     base_size += 4;
11176   if ((entry_size + base_size + current_function_outgoing_args_size) & 7)
11177     abort ();
11178
11179   cfun->machine->frame_size = base_size;
11180
11181   return base_size;
11182 }
11183
11184 /* Generate the rest of a function's prologue.  */
11185
11186 void
11187 thumb_expand_prologue ()
11188 {
11189   HOST_WIDE_INT amount = (thumb_get_frame_size ()
11190                           + current_function_outgoing_args_size);
11191   unsigned long func_type;
11192
11193   func_type = arm_current_func_type ();
11194   
11195   /* Naked functions don't have prologues.  */
11196   if (IS_NAKED (func_type))
11197     return;
11198
11199   if (IS_INTERRUPT (func_type))
11200     {
11201       error ("interrupt Service Routines cannot be coded in Thumb mode");
11202       return;
11203     }
11204
11205   if (frame_pointer_needed)
11206     emit_insn (gen_movsi (hard_frame_pointer_rtx, stack_pointer_rtx));
11207
11208   if (amount)
11209     {
11210       amount = ROUND_UP_WORD (amount);
11211       
11212       if (amount < 512)
11213         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11214                                GEN_INT (- amount)));
11215       else
11216         {
11217           int regno;
11218           rtx reg;
11219
11220           /* The stack decrement is too big for an immediate value in a single
11221              insn.  In theory we could issue multiple subtracts, but after
11222              three of them it becomes more space efficient to place the full
11223              value in the constant pool and load into a register.  (Also the
11224              ARM debugger really likes to see only one stack decrement per
11225              function).  So instead we look for a scratch register into which
11226              we can load the decrement, and then we subtract this from the
11227              stack pointer.  Unfortunately on the thumb the only available
11228              scratch registers are the argument registers, and we cannot use
11229              these as they may hold arguments to the function.  Instead we
11230              attempt to locate a call preserved register which is used by this
11231              function.  If we can find one, then we know that it will have
11232              been pushed at the start of the prologue and so we can corrupt
11233              it now.  */
11234           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
11235             if (THUMB_REG_PUSHED_P (regno)
11236                 && !(frame_pointer_needed
11237                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
11238               break;
11239
11240           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
11241             {
11242               rtx spare = gen_rtx (REG, SImode, IP_REGNUM);
11243
11244               /* Choose an arbitrary, non-argument low register.  */
11245               reg = gen_rtx (REG, SImode, LAST_LO_REGNUM);
11246
11247               /* Save it by copying it into a high, scratch register.  */
11248               emit_insn (gen_movsi (spare, reg));
11249               /* Add a USE to stop propagate_one_insn() from barfing.  */
11250               emit_insn (gen_prologue_use (spare));
11251
11252               /* Decrement the stack.  */
11253               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
11254               emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11255                                      reg));
11256
11257               /* Restore the low register's original value.  */
11258               emit_insn (gen_movsi (reg, spare));
11259               
11260               /* Emit a USE of the restored scratch register, so that flow
11261                  analysis will not consider the restore redundant.  The
11262                  register won't be used again in this function and isn't
11263                  restored by the epilogue.  */
11264               emit_insn (gen_prologue_use (reg));
11265             }
11266           else
11267             {
11268               reg = gen_rtx (REG, SImode, regno);
11269
11270               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
11271               emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11272                                      reg));
11273             }
11274         }
11275     }
11276   
11277   if (current_function_profile || TARGET_NO_SCHED_PRO)
11278     emit_insn (gen_blockage ());
11279 }
11280
11281 void
11282 thumb_expand_epilogue ()
11283 {
11284   HOST_WIDE_INT amount = (thumb_get_frame_size ()
11285                           + current_function_outgoing_args_size);
11286   
11287   /* Naked functions don't have prologues.  */
11288   if (IS_NAKED (arm_current_func_type ()))
11289     return;
11290
11291   if (frame_pointer_needed)
11292     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
11293   else if (amount)
11294     {
11295       amount = ROUND_UP_WORD (amount);
11296       
11297       if (amount < 512)
11298         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11299                                GEN_INT (amount)));
11300       else
11301         {
11302           /* r3 is always free in the epilogue.  */
11303           rtx reg = gen_rtx (REG, SImode, LAST_ARG_REGNUM);
11304
11305           emit_insn (gen_movsi (reg, GEN_INT (amount)));
11306           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
11307         }
11308     }
11309       
11310   /* Emit a USE (stack_pointer_rtx), so that
11311      the stack adjustment will not be deleted.  */
11312   emit_insn (gen_prologue_use (stack_pointer_rtx));
11313
11314   if (current_function_profile || TARGET_NO_SCHED_PRO)
11315     emit_insn (gen_blockage ());
11316 }
11317
11318 static void
11319 thumb_output_function_prologue (f, size)
11320      FILE * f;
11321      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
11322 {
11323   int live_regs_mask = 0;
11324   int high_regs_pushed = 0;
11325   int regno;
11326
11327   if (IS_NAKED (arm_current_func_type ()))
11328     return;
11329
11330   if (is_called_in_ARM_mode (current_function_decl))
11331     {
11332       const char * name;
11333
11334       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
11335         abort ();
11336       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
11337         abort ();
11338       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
11339       
11340       /* Generate code sequence to switch us into Thumb mode.  */
11341       /* The .code 32 directive has already been emitted by
11342          ASM_DECLARE_FUNCTION_NAME.  */
11343       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
11344       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
11345
11346       /* Generate a label, so that the debugger will notice the
11347          change in instruction sets.  This label is also used by
11348          the assembler to bypass the ARM code when this function
11349          is called from a Thumb encoded function elsewhere in the
11350          same file.  Hence the definition of STUB_NAME here must
11351          agree with the definition in gas/config/tc-arm.c  */
11352       
11353 #define STUB_NAME ".real_start_of"
11354       
11355       fprintf (f, "\t.code\t16\n");
11356 #ifdef ARM_PE
11357       if (arm_dllexport_name_p (name))
11358         name = arm_strip_name_encoding (name);
11359 #endif        
11360       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
11361       fprintf (f, "\t.thumb_func\n");
11362       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
11363     }
11364     
11365   if (current_function_pretend_args_size)
11366     {
11367       if (cfun->machine->uses_anonymous_args)
11368         {
11369           int num_pushes;
11370           
11371           fprintf (f, "\tpush\t{");
11372
11373           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
11374           
11375           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
11376                regno <= LAST_ARG_REGNUM;
11377                regno++)
11378             asm_fprintf (f, "%r%s", regno,
11379                          regno == LAST_ARG_REGNUM ? "" : ", ");
11380
11381           fprintf (f, "}\n");
11382         }
11383       else
11384         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", 
11385                      SP_REGNUM, SP_REGNUM,
11386                      current_function_pretend_args_size);
11387     }
11388
11389   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
11390     if (THUMB_REG_PUSHED_P (regno))
11391       live_regs_mask |= 1 << regno;
11392
11393   if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p (1))
11394     live_regs_mask |= 1 << LR_REGNUM;
11395
11396   if (TARGET_BACKTRACE)
11397     {
11398       int    offset;
11399       int    work_register = 0;
11400       int    wr;
11401       
11402       /* We have been asked to create a stack backtrace structure.
11403          The code looks like this:
11404          
11405          0   .align 2
11406          0   func:
11407          0     sub   SP, #16         Reserve space for 4 registers.
11408          2     push  {R7}            Get a work register.
11409          4     add   R7, SP, #20     Get the stack pointer before the push.
11410          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
11411          8     mov   R7, PC          Get hold of the start of this code plus 12.
11412         10     str   R7, [SP, #16]   Store it.
11413         12     mov   R7, FP          Get hold of the current frame pointer.
11414         14     str   R7, [SP, #4]    Store it.
11415         16     mov   R7, LR          Get hold of the current return address.
11416         18     str   R7, [SP, #12]   Store it.
11417         20     add   R7, SP, #16     Point at the start of the backtrace structure.
11418         22     mov   FP, R7          Put this value into the frame pointer.  */
11419
11420       if ((live_regs_mask & 0xFF) == 0)
11421         {
11422           /* See if the a4 register is free.  */
11423
11424           if (regs_ever_live [LAST_ARG_REGNUM] == 0)
11425             work_register = LAST_ARG_REGNUM;
11426           else    /* We must push a register of our own */
11427             live_regs_mask |= (1 << LAST_LO_REGNUM);
11428         }
11429
11430       if (work_register == 0)
11431         {
11432           /* Select a register from the list that will be pushed to
11433              use as our work register.  */
11434           for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
11435             if ((1 << work_register) & live_regs_mask)
11436               break;
11437         }
11438       
11439       asm_fprintf
11440         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
11441          SP_REGNUM, SP_REGNUM);
11442       
11443       if (live_regs_mask)
11444         thumb_pushpop (f, live_regs_mask, 1);
11445       
11446       for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
11447         if (wr & live_regs_mask)
11448           offset += 4;
11449       
11450       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
11451                    offset + 16 + current_function_pretend_args_size);
11452       
11453       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11454                    offset + 4);
11455
11456       /* Make sure that the instruction fetching the PC is in the right place
11457          to calculate "start of backtrace creation code + 12".  */
11458       if (live_regs_mask)
11459         {
11460           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
11461           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11462                        offset + 12);
11463           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
11464                        ARM_HARD_FRAME_POINTER_REGNUM);
11465           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11466                        offset);
11467         }
11468       else
11469         {
11470           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
11471                        ARM_HARD_FRAME_POINTER_REGNUM);
11472           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11473                        offset);
11474           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
11475           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11476                        offset + 12);
11477         }
11478       
11479       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
11480       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
11481                    offset + 8);
11482       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
11483                    offset + 12);
11484       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
11485                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
11486     }
11487   else if (live_regs_mask)
11488     thumb_pushpop (f, live_regs_mask, 1);
11489
11490   for (regno = 8; regno < 13; regno++)
11491     if (THUMB_REG_PUSHED_P (regno))
11492       high_regs_pushed++;
11493
11494   if (high_regs_pushed)
11495     {
11496       int pushable_regs = 0;
11497       int mask = live_regs_mask & 0xff;
11498       int next_hi_reg;
11499
11500       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
11501         if (THUMB_REG_PUSHED_P (next_hi_reg))
11502           break;
11503
11504       pushable_regs = mask;
11505
11506       if (pushable_regs == 0)
11507         {
11508           /* Desperation time -- this probably will never happen.  */
11509           if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
11510             asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
11511           mask = 1 << LAST_ARG_REGNUM;
11512         }
11513
11514       while (high_regs_pushed > 0)
11515         {
11516           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
11517             {
11518               if (mask & (1 << regno))
11519                 {
11520                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
11521                   
11522                   high_regs_pushed--;
11523                   
11524                   if (high_regs_pushed)
11525                     {
11526                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
11527                            next_hi_reg--)
11528                         if (THUMB_REG_PUSHED_P (next_hi_reg))
11529                           break;
11530                     }
11531                   else
11532                     {
11533                       mask &= ~((1 << regno) - 1);
11534                       break;
11535                     }
11536                 }
11537             }
11538           
11539           thumb_pushpop (f, mask, 1);
11540         }
11541
11542       if (pushable_regs == 0
11543           && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
11544         asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
11545     }
11546 }
11547
11548 /* Handle the case of a double word load into a low register from
11549    a computed memory address.  The computed address may involve a
11550    register which is overwritten by the load.  */
11551
11552 const char *
11553 thumb_load_double_from_address (operands)
11554      rtx *operands;
11555 {
11556   rtx addr;
11557   rtx base;
11558   rtx offset;
11559   rtx arg1;
11560   rtx arg2;
11561   
11562   if (GET_CODE (operands[0]) != REG)
11563     abort ();
11564   
11565   if (GET_CODE (operands[1]) != MEM)
11566     abort ();
11567
11568   /* Get the memory address.  */
11569   addr = XEXP (operands[1], 0);
11570       
11571   /* Work out how the memory address is computed.  */
11572   switch (GET_CODE (addr))
11573     {
11574     case REG:
11575       operands[2] = gen_rtx (MEM, SImode,
11576                              plus_constant (XEXP (operands[1], 0), 4));
11577
11578       if (REGNO (operands[0]) == REGNO (addr))
11579         {
11580           output_asm_insn ("ldr\t%H0, %2", operands);
11581           output_asm_insn ("ldr\t%0, %1", operands);
11582         }
11583       else
11584         {
11585           output_asm_insn ("ldr\t%0, %1", operands);
11586           output_asm_insn ("ldr\t%H0, %2", operands);
11587         }
11588       break;
11589       
11590     case CONST:
11591       /* Compute <address> + 4 for the high order load.  */
11592       operands[2] = gen_rtx (MEM, SImode,
11593                              plus_constant (XEXP (operands[1], 0), 4));
11594       
11595       output_asm_insn ("ldr\t%0, %1", operands);
11596       output_asm_insn ("ldr\t%H0, %2", operands);
11597       break;
11598           
11599     case PLUS:
11600       arg1   = XEXP (addr, 0);
11601       arg2   = XEXP (addr, 1);
11602             
11603       if (CONSTANT_P (arg1))
11604         base = arg2, offset = arg1;
11605       else
11606         base = arg1, offset = arg2;
11607   
11608       if (GET_CODE (base) != REG)
11609         abort ();
11610
11611       /* Catch the case of <address> = <reg> + <reg> */
11612       if (GET_CODE (offset) == REG)
11613         {
11614           int reg_offset = REGNO (offset);
11615           int reg_base   = REGNO (base);
11616           int reg_dest   = REGNO (operands[0]);
11617           
11618           /* Add the base and offset registers together into the
11619              higher destination register.  */
11620           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
11621                        reg_dest + 1, reg_base, reg_offset);
11622           
11623           /* Load the lower destination register from the address in
11624              the higher destination register.  */
11625           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
11626                        reg_dest, reg_dest + 1);
11627           
11628           /* Load the higher destination register from its own address
11629              plus 4.  */
11630           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
11631                        reg_dest + 1, reg_dest + 1);
11632         }
11633       else
11634         {
11635           /* Compute <address> + 4 for the high order load.  */
11636           operands[2] = gen_rtx (MEM, SImode,
11637                                  plus_constant (XEXP (operands[1], 0), 4));
11638           
11639           /* If the computed address is held in the low order register
11640              then load the high order register first, otherwise always
11641              load the low order register first.  */
11642           if (REGNO (operands[0]) == REGNO (base))
11643             {
11644               output_asm_insn ("ldr\t%H0, %2", operands);
11645               output_asm_insn ("ldr\t%0, %1", operands);
11646             }
11647           else
11648             {
11649               output_asm_insn ("ldr\t%0, %1", operands);
11650               output_asm_insn ("ldr\t%H0, %2", operands);
11651             }
11652         }
11653       break;
11654
11655     case LABEL_REF:
11656       /* With no registers to worry about we can just load the value
11657          directly.  */
11658       operands[2] = gen_rtx (MEM, SImode,
11659                              plus_constant (XEXP (operands[1], 0), 4));
11660           
11661       output_asm_insn ("ldr\t%H0, %2", operands);
11662       output_asm_insn ("ldr\t%0, %1", operands);
11663       break;
11664       
11665     default:
11666       abort ();
11667       break;
11668     }
11669   
11670   return "";
11671 }
11672
11673
11674 const char *
11675 thumb_output_move_mem_multiple (n, operands)
11676      int n;
11677      rtx * operands;
11678 {
11679   rtx tmp;
11680
11681   switch (n)
11682     {
11683     case 2:
11684       if (REGNO (operands[4]) > REGNO (operands[5]))
11685         {
11686           tmp = operands[4];
11687           operands[4] = operands[5];
11688           operands[5] = tmp;
11689         }
11690       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
11691       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
11692       break;
11693
11694     case 3:
11695       if (REGNO (operands[4]) > REGNO (operands[5]))
11696         {
11697           tmp = operands[4];
11698           operands[4] = operands[5];
11699           operands[5] = tmp;
11700         }
11701       if (REGNO (operands[5]) > REGNO (operands[6]))
11702         {
11703           tmp = operands[5];
11704           operands[5] = operands[6];
11705           operands[6] = tmp;
11706         }
11707       if (REGNO (operands[4]) > REGNO (operands[5]))
11708         {
11709           tmp = operands[4];
11710           operands[4] = operands[5];
11711           operands[5] = tmp;
11712         }
11713       
11714       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
11715       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
11716       break;
11717
11718     default:
11719       abort ();
11720     }
11721
11722   return "";
11723 }
11724
11725 /* Routines for generating rtl.  */
11726
11727 void
11728 thumb_expand_movstrqi (operands)
11729      rtx * operands;
11730 {
11731   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
11732   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
11733   HOST_WIDE_INT len = INTVAL (operands[2]);
11734   HOST_WIDE_INT offset = 0;
11735
11736   while (len >= 12)
11737     {
11738       emit_insn (gen_movmem12b (out, in, out, in));
11739       len -= 12;
11740     }
11741   
11742   if (len >= 8)
11743     {
11744       emit_insn (gen_movmem8b (out, in, out, in));
11745       len -= 8;
11746     }
11747   
11748   if (len >= 4)
11749     {
11750       rtx reg = gen_reg_rtx (SImode);
11751       emit_insn (gen_movsi (reg, gen_rtx (MEM, SImode, in)));
11752       emit_insn (gen_movsi (gen_rtx (MEM, SImode, out), reg));
11753       len -= 4;
11754       offset += 4;
11755     }
11756   
11757   if (len >= 2)
11758     {
11759       rtx reg = gen_reg_rtx (HImode);
11760       emit_insn (gen_movhi (reg, gen_rtx (MEM, HImode, 
11761                                           plus_constant (in, offset))));
11762       emit_insn (gen_movhi (gen_rtx (MEM, HImode, plus_constant (out, offset)),
11763                             reg));
11764       len -= 2;
11765       offset += 2;
11766     }
11767   
11768   if (len)
11769     {
11770       rtx reg = gen_reg_rtx (QImode);
11771       emit_insn (gen_movqi (reg, gen_rtx (MEM, QImode,
11772                                           plus_constant (in, offset))));
11773       emit_insn (gen_movqi (gen_rtx (MEM, QImode, plus_constant (out, offset)),
11774                             reg));
11775     }
11776 }
11777
11778 int
11779 thumb_cmp_operand (op, mode)
11780      rtx op;
11781      enum machine_mode mode;
11782 {
11783   return ((GET_CODE (op) == CONST_INT
11784            && (unsigned HOST_WIDE_INT) (INTVAL (op)) < 256)
11785           || register_operand (op, mode));
11786 }
11787
11788 static const char *
11789 thumb_condition_code (x, invert)
11790      rtx x;
11791      int invert;
11792 {
11793   static const char * const conds[] =
11794   {
11795     "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", 
11796     "hi", "ls", "ge", "lt", "gt", "le"
11797   };
11798   int val;
11799
11800   switch (GET_CODE (x))
11801     {
11802     case EQ: val = 0; break;
11803     case NE: val = 1; break;
11804     case GEU: val = 2; break;
11805     case LTU: val = 3; break;
11806     case GTU: val = 8; break;
11807     case LEU: val = 9; break;
11808     case GE: val = 10; break;
11809     case LT: val = 11; break;
11810     case GT: val = 12; break;
11811     case LE: val = 13; break;
11812     default:
11813       abort ();
11814     }
11815
11816   return conds[val ^ invert];
11817 }
11818
11819 /* Handle storing a half-word to memory during reload.  */ 
11820
11821 void
11822 thumb_reload_out_hi (operands)
11823      rtx * operands;
11824 {
11825   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
11826 }
11827
11828 /* Handle storing a half-word to memory during reload.  */ 
11829
11830 void
11831 thumb_reload_in_hi (operands)
11832      rtx * operands ATTRIBUTE_UNUSED;
11833 {
11834   abort ();
11835 }
11836
11837 /* Return the length of a function name prefix
11838     that starts with the character 'c'.  */
11839
11840 static int
11841 arm_get_strip_length (c)
11842      int c;
11843 {
11844   switch (c)
11845     {
11846     ARM_NAME_ENCODING_LENGTHS
11847       default: return 0; 
11848     }
11849 }
11850
11851 /* Return a pointer to a function's name with any
11852    and all prefix encodings stripped from it.  */
11853
11854 const char *
11855 arm_strip_name_encoding (name)
11856      const char * name;
11857 {
11858   int skip;
11859   
11860   while ((skip = arm_get_strip_length (* name)))
11861     name += skip;
11862
11863   return name;
11864 }
11865
11866 /* If there is a '*' anywhere in the name's prefix, then
11867    emit the stripped name verbatim, otherwise prepend an
11868    underscore if leading underscores are being used.  */
11869
11870 void
11871 arm_asm_output_labelref (stream, name)
11872      FILE * stream;
11873      const char * name;
11874 {
11875   int skip;
11876   int verbatim = 0;
11877
11878   while ((skip = arm_get_strip_length (* name)))
11879     {
11880       verbatim |= (*name == '*');
11881       name += skip;
11882     }
11883
11884   if (verbatim)
11885     fputs (name, stream);
11886   else
11887     asm_fprintf (stream, "%U%s", name);
11888 }
11889
11890 rtx aof_pic_label;
11891
11892 #ifdef AOF_ASSEMBLER
11893 /* Special functions only needed when producing AOF syntax assembler.  */
11894
11895 struct pic_chain
11896 {
11897   struct pic_chain * next;
11898   const char * symname;
11899 };
11900
11901 static struct pic_chain * aof_pic_chain = NULL;
11902
11903 rtx
11904 aof_pic_entry (x)
11905      rtx x;
11906 {
11907   struct pic_chain ** chainp;
11908   int offset;
11909
11910   if (aof_pic_label == NULL_RTX)
11911     {
11912       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
11913     }
11914
11915   for (offset = 0, chainp = &aof_pic_chain; *chainp;
11916        offset += 4, chainp = &(*chainp)->next)
11917     if ((*chainp)->symname == XSTR (x, 0))
11918       return plus_constant (aof_pic_label, offset);
11919
11920   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
11921   (*chainp)->next = NULL;
11922   (*chainp)->symname = XSTR (x, 0);
11923   return plus_constant (aof_pic_label, offset);
11924 }
11925
11926 void
11927 aof_dump_pic_table (f)
11928      FILE * f;
11929 {
11930   struct pic_chain * chain;
11931
11932   if (aof_pic_chain == NULL)
11933     return;
11934
11935   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
11936                PIC_OFFSET_TABLE_REGNUM,
11937                PIC_OFFSET_TABLE_REGNUM);
11938   fputs ("|x$adcons|\n", f);
11939   
11940   for (chain = aof_pic_chain; chain; chain = chain->next)
11941     {
11942       fputs ("\tDCD\t", f);
11943       assemble_name (f, chain->symname);
11944       fputs ("\n", f);
11945     }
11946 }
11947
11948 int arm_text_section_count = 1;
11949
11950 char *
11951 aof_text_section ()
11952 {
11953   static char buf[100];
11954   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
11955            arm_text_section_count++);
11956   if (flag_pic)
11957     strcat (buf, ", PIC, REENTRANT");
11958   return buf;
11959 }
11960
11961 static int arm_data_section_count = 1;
11962
11963 char *
11964 aof_data_section ()
11965 {
11966   static char buf[100];
11967   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
11968   return buf;
11969 }
11970
11971 /* The AOF assembler is religiously strict about declarations of
11972    imported and exported symbols, so that it is impossible to declare
11973    a function as imported near the beginning of the file, and then to
11974    export it later on.  It is, however, possible to delay the decision
11975    until all the functions in the file have been compiled.  To get
11976    around this, we maintain a list of the imports and exports, and
11977    delete from it any that are subsequently defined.  At the end of
11978    compilation we spit the remainder of the list out before the END
11979    directive.  */
11980
11981 struct import
11982 {
11983   struct import * next;
11984   const char * name;
11985 };
11986
11987 static struct import * imports_list = NULL;
11988
11989 void
11990 aof_add_import (name)
11991      const char * name;
11992 {
11993   struct import * new;
11994
11995   for (new = imports_list; new; new = new->next)
11996     if (new->name == name)
11997       return;
11998
11999   new = (struct import *) xmalloc (sizeof (struct import));
12000   new->next = imports_list;
12001   imports_list = new;
12002   new->name = name;
12003 }
12004
12005 void
12006 aof_delete_import (name)
12007      const char * name;
12008 {
12009   struct import ** old;
12010
12011   for (old = &imports_list; *old; old = & (*old)->next)
12012     {
12013       if ((*old)->name == name)
12014         {
12015           *old = (*old)->next;
12016           return;
12017         }
12018     }
12019 }
12020
12021 int arm_main_function = 0;
12022
12023 void
12024 aof_dump_imports (f)
12025      FILE * f;
12026 {
12027   /* The AOF assembler needs this to cause the startup code to be extracted
12028      from the library.  Brining in __main causes the whole thing to work
12029      automagically.  */
12030   if (arm_main_function)
12031     {
12032       text_section ();
12033       fputs ("\tIMPORT __main\n", f);
12034       fputs ("\tDCD __main\n", f);
12035     }
12036
12037   /* Now dump the remaining imports.  */
12038   while (imports_list)
12039     {
12040       fprintf (f, "\tIMPORT\t");
12041       assemble_name (f, imports_list->name);
12042       fputc ('\n', f);
12043       imports_list = imports_list->next;
12044     }
12045 }
12046
12047 static void
12048 aof_globalize_label (stream, name)
12049      FILE *stream;
12050      const char *name;
12051 {
12052   default_globalize_label (stream, name);
12053   if (! strcmp (name, "main"))
12054     arm_main_function = 1;
12055 }
12056 #endif /* AOF_ASSEMBLER */
12057
12058 #ifdef OBJECT_FORMAT_ELF
12059 /* Switch to an arbitrary section NAME with attributes as specified
12060    by FLAGS.  ALIGN specifies any known alignment requirements for
12061    the section; 0 if the default should be used.
12062
12063    Differs from the default elf version only in the prefix character
12064    used before the section type.  */
12065
12066 static void
12067 arm_elf_asm_named_section (name, flags)
12068      const char *name;
12069      unsigned int flags;
12070 {
12071   char flagchars[10], *f = flagchars;
12072
12073   if (! named_section_first_declaration (name))
12074     {
12075       fprintf (asm_out_file, "\t.section\t%s\n", name);
12076       return;
12077     }
12078
12079   if (!(flags & SECTION_DEBUG))
12080     *f++ = 'a';
12081   if (flags & SECTION_WRITE)
12082     *f++ = 'w';
12083   if (flags & SECTION_CODE)
12084     *f++ = 'x';
12085   if (flags & SECTION_SMALL)
12086     *f++ = 's';
12087   if (flags & SECTION_MERGE)
12088     *f++ = 'M';
12089   if (flags & SECTION_STRINGS)
12090     *f++ = 'S';
12091   if (flags & SECTION_TLS)
12092     *f++ = 'T';
12093   *f = '\0';
12094
12095   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
12096
12097   if (!(flags & SECTION_NOTYPE))
12098     {
12099       const char *type;
12100
12101       if (flags & SECTION_BSS)
12102         type = "nobits";
12103       else
12104         type = "progbits";
12105
12106       fprintf (asm_out_file, ",%%%s", type);
12107
12108       if (flags & SECTION_ENTSIZE)
12109         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
12110     }
12111
12112   putc ('\n', asm_out_file);
12113 }
12114 #endif
12115
12116 #ifndef ARM_PE
12117 /* Symbols in the text segment can be accessed without indirecting via the
12118    constant pool; it may take an extra binary operation, but this is still
12119    faster than indirecting via memory.  Don't do this when not optimizing,
12120    since we won't be calculating al of the offsets necessary to do this
12121    simplification.  */
12122
12123 static void
12124 arm_encode_section_info (decl, first)
12125      tree decl;
12126      int first;
12127 {
12128   /* This doesn't work with AOF syntax, since the string table may be in
12129      a different AREA.  */
12130 #ifndef AOF_ASSEMBLER
12131   if (optimize > 0 && TREE_CONSTANT (decl)
12132       && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
12133     {
12134       rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
12135                  ? TREE_CST_RTL (decl) : DECL_RTL (decl));
12136       SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
12137     }
12138 #endif
12139
12140   /* If we are referencing a function that is weak then encode a long call
12141      flag in the function name, otherwise if the function is static or
12142      or known to be defined in this file then encode a short call flag.  */
12143   if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
12144     {
12145       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
12146         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
12147       else if (! TREE_PUBLIC (decl))
12148         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
12149     }
12150 }
12151 #endif /* !ARM_PE */
12152
12153 static void
12154 arm_internal_label (stream, prefix, labelno)
12155      FILE *stream;
12156      const char *prefix;
12157      unsigned long labelno;
12158 {
12159   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
12160       && !strcmp (prefix, "L"))
12161     {
12162       arm_ccfsm_state = 0;
12163       arm_target_insn = NULL;
12164     }
12165   default_internal_label (stream, prefix, labelno);
12166 }
12167
12168 /* Output code to add DELTA to the first argument, and then jump
12169    to FUNCTION.  Used for C++ multiple inheritance.  */
12170
12171 static void
12172 arm_output_mi_thunk (file, thunk, delta, vcall_offset, function)
12173      FILE *file;
12174      tree thunk ATTRIBUTE_UNUSED;
12175      HOST_WIDE_INT delta;
12176      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED;
12177      tree function;
12178 {
12179   int mi_delta = delta;
12180   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
12181   int shift = 0;
12182   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)))
12183                     ? 1 : 0);
12184   if (mi_delta < 0)
12185     mi_delta = - mi_delta;
12186   while (mi_delta != 0)
12187     {
12188       if ((mi_delta & (3 << shift)) == 0)
12189         shift += 2;
12190       else
12191         {
12192           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
12193                        mi_op, this_regno, this_regno,
12194                        mi_delta & (0xff << shift));
12195           mi_delta &= ~(0xff << shift);
12196           shift += 8;
12197         }
12198     }
12199   fputs ("\tb\t", file);
12200   assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
12201   if (NEED_PLT_RELOC)
12202     fputs ("(PLT)", file);
12203   fputc ('\n', file);
12204 }