OSDN Git Service

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