OSDN Git Service

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