OSDN Git Service

Fix arm_comp_type_attributes
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
3    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
4    and Martin Simmons (@harleqn.co.uk).
5    More major hacks by Richard Earnshaw (rearnsha@arm.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23     
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-flags.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "reload.h"
37 #include "tree.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "recog.h"
42 #include "ggc.h"
43 #include "arm-protos.h"
44
45 /* The maximum number of insns skipped which will be conditionalised if
46    possible.  */
47 static int max_insns_skipped = 5;
48
49 extern FILE * asm_out_file;
50 /* Some function declarations.  */
51
52 #ifndef Mmode
53 #define Mmode enum machine_mode
54 #endif
55
56 static HOST_WIDE_INT int_log2           PARAMS ((HOST_WIDE_INT));
57 static char * output_multi_immediate    PARAMS ((rtx *, char *, char *, int, HOST_WIDE_INT));
58 static int arm_gen_constant             PARAMS ((enum rtx_code, Mmode, HOST_WIDE_INT, rtx, rtx, int, int));
59 static int arm_naked_function_p         PARAMS ((tree));
60 static void init_fpa_table              PARAMS ((void));
61 static enum machine_mode select_dominance_cc_mode PARAMS ((rtx, rtx, HOST_WIDE_INT));
62 static HOST_WIDE_INT add_minipool_constant PARAMS ((rtx, Mmode));
63 static void dump_minipool               PARAMS ((rtx));
64 static rtx find_barrier                 PARAMS ((rtx, int));
65 static void push_minipool_fix           PARAMS ((rtx, int, rtx *, Mmode, rtx));
66 static void push_minipool_barrier       PARAMS ((rtx, int));
67 static void note_invalid_constants      PARAMS ((rtx, int));
68 static char * fp_const_from_val         PARAMS ((REAL_VALUE_TYPE *));
69 static int eliminate_lr2ip              PARAMS ((rtx *));
70 static char * shift_op                  PARAMS ((rtx, HOST_WIDE_INT *));
71 static int pattern_really_clobbers_lr   PARAMS ((rtx));
72 static int function_really_clobbers_lr  PARAMS ((rtx));
73 static void emit_multi_reg_push         PARAMS ((int));
74 static void emit_sfm                    PARAMS ((int, int));
75 static enum arm_cond_code get_arm_condition_code PARAMS ((rtx));
76 static int const_ok_for_op              PARAMS ((HOST_WIDE_INT, enum rtx_code));
77 static void arm_add_gc_roots            PARAMS ((void));
78
79 /* True if we are currently building a constant table. */
80 int making_const_table;
81
82 /* Define the information needed to generate branch insns.  This is
83    stored from the compare operation. */
84 rtx arm_compare_op0, arm_compare_op1;
85
86 /* What type of floating point are we tuning for? */
87 enum floating_point_type arm_fpu;
88
89 /* What type of floating point instructions are available? */
90 enum floating_point_type arm_fpu_arch;
91
92 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode */
93 enum prog_mode_type arm_prgmode;
94
95 /* Set by the -mfp=... option */
96 const char * target_fp_name = NULL;
97
98 /* Used to parse -mstructure_size_boundary command line option.  */
99 const char * structure_size_string = NULL;
100 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
101
102 /* Bit values used to identify processor capabilities.  */
103 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
104 #define FL_FAST_MULT  (1 << 1)        /* Fast multiply */
105 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
106 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
107 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
108 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
109 #define FL_THUMB      (1 << 6)        /* Thumb aware */
110 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
111 #define FL_STRONG     (1 << 8)        /* StrongARM */
112
113 /* The bits in this mask specify which instructions we are allowed to
114    generate.  */
115 static int insn_flags = 0;
116 /* The bits in this mask specify which instruction scheduling options should
117    be used.  Note - there is an overlap with the FL_FAST_MULT.  For some
118    hardware we want to be able to generate the multiply instructions, but to
119    tune as if they were not present in the architecture.  */
120 static int tune_flags = 0;
121
122 /* The following are used in the arm.md file as equivalents to bits
123    in the above two flag variables.  */
124
125 /* Nonzero if this is an "M" variant of the processor.  */
126 int arm_fast_multiply = 0;
127
128 /* Nonzero if this chip supports the ARM Architecture 4 extensions */
129 int arm_arch4 = 0;
130
131 /* Nonzero if this chip supports the ARM Architecture 5 extensions */
132 int arm_arch5 = 0;
133
134 /* Nonzero if this chip can benefit from load scheduling.  */
135 int arm_ld_sched = 0;
136
137 /* Nonzero if this chip is a StrongARM.  */
138 int arm_is_strong = 0;
139
140 /* Nonzero if this chip is a an ARM6 or an ARM7.  */
141 int arm_is_6_or_7 = 0;
142
143 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
144    must report the mode of the memory reference from PRINT_OPERAND to
145    PRINT_OPERAND_ADDRESS.  */
146 enum machine_mode output_memory_reference_mode;
147
148 /* Nonzero if the prologue must setup `fp'.  */
149 int current_function_anonymous_args;
150
151 /* The register number to be used for the PIC offset register.  */
152 const char * arm_pic_register_string = NULL;
153 int arm_pic_register = 9;
154
155 /* Set to one if we think that lr is only saved because of subroutine calls,
156    but all of these can be `put after' return insns */
157 int lr_save_eliminated;
158
159 /* Set to 1 when a return insn is output, this means that the epilogue
160    is not needed. */
161 static int return_used_this_function;
162
163 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
164    the next function.  */
165 static int after_arm_reorg = 0;
166
167 /* The maximum number of insns to be used when loading a constant.  */
168 static int arm_constant_limit = 3;
169
170 /* For an explanation of these variables, see final_prescan_insn below.  */
171 int arm_ccfsm_state;
172 enum arm_cond_code arm_current_cc;
173 rtx arm_target_insn;
174 int arm_target_label;
175
176 /* The condition codes of the ARM, and the inverse function.  */
177 char * arm_condition_codes[] =
178 {
179   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
180   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
181 };
182
183 static enum arm_cond_code get_arm_condition_code ();
184
185 #define streq(string1, string2) (strcmp (string1, string2) == 0)
186 \f
187 /* Initialization code */
188
189 struct processors
190 {
191   char *       name;
192   unsigned int flags;
193 };
194
195 /* Not all of these give usefully different compilation alternatives,
196    but there is no simple way of generalizing them.  */
197 static struct processors all_cores[] =
198 {
199   /* ARM Cores */
200   
201   {"arm2",      FL_CO_PROC | FL_MODE26 },
202   {"arm250",    FL_CO_PROC | FL_MODE26 },
203   {"arm3",      FL_CO_PROC | FL_MODE26 },
204   {"arm6",      FL_CO_PROC | FL_MODE26 | FL_MODE32 },
205   {"arm60",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
206   {"arm600",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
207   {"arm610",                 FL_MODE26 | FL_MODE32 },
208   {"arm620",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
209   {"arm7",      FL_CO_PROC | FL_MODE26 | FL_MODE32 },
210   /* arm7m doesn't exist on its own, but only with D, (and I), but
211    those don't alter the code, so arm7m is sometimes used.  */
212   {"arm7m",     FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
213   {"arm7d",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
214   {"arm7dm",    FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
215   {"arm7di",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
216   {"arm7dmi",   FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
217   {"arm70",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
218   {"arm700",    FL_CO_PROC | FL_MODE26 | FL_MODE32 },
219   {"arm700i",   FL_CO_PROC | FL_MODE26 | FL_MODE32 },
220   {"arm710",                 FL_MODE26 | FL_MODE32 },
221   {"arm720",                 FL_MODE26 | FL_MODE32 },
222   {"arm710c",                FL_MODE26 | FL_MODE32 },
223   {"arm7100",                FL_MODE26 | FL_MODE32 },
224   {"arm7500",                FL_MODE26 | FL_MODE32 },
225   /* Doesn't have an external co-proc, but does have embedded fpu.  */
226   {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
227   {"arm7tdmi",  FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
228   {"arm8",                   FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
229   {"arm810",                 FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
230   {"arm9",                               FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
231   {"arm920",                             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED },
232   {"arm920t",                            FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
233   {"arm9tdmi",                           FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
234   {"strongarm",              FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
235   {"strongarm110",           FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
236   {"strongarm1100",          FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 |            FL_LDSCHED | FL_STRONG },
237   
238   {NULL, 0}
239 };
240
241 static struct processors all_architectures[] =
242 {
243   /* ARM Architectures */
244   
245   { "armv2",     FL_CO_PROC | FL_MODE26 },
246   { "armv2a",    FL_CO_PROC | FL_MODE26 },
247   { "armv3",     FL_CO_PROC | FL_MODE26 | FL_MODE32 },
248   { "armv3m",    FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
249   { "armv4",     FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
250   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
251      implementations that support it, so we will leave it out for now.  */
252   { "armv4t",    FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
253   { "armv5",     FL_CO_PROC |             FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
254   { NULL, 0 }
255 };
256
257 /* This is a magic stucture.  The 'string' field is magically filled in
258    with a pointer to the value specified by the user on the command line
259    assuming that the user has specified such a value.  */
260
261 struct arm_cpu_select arm_select[] =
262 {
263   /* string       name            processors  */        
264   { NULL,       "-mcpu=",       all_cores  },
265   { NULL,       "-march=",      all_architectures },
266   { NULL,       "-mtune=",      all_cores }
267 };
268
269 /* Return the number of bits set in value' */
270 static unsigned int
271 bit_count (value)
272      signed int value;
273 {
274   unsigned int count = 0;
275   
276   while (value)
277     {
278       value &= ~(value & - value);
279       ++ count;
280     }
281
282   return count;
283 }
284
285 /* Fix up any incompatible options that the user has specified.
286    This has now turned into a maze.  */
287 void
288 arm_override_options ()
289 {
290   unsigned i;
291   
292   /* Set up the flags based on the cpu/architecture selected by the user.  */
293   for (i = sizeof (arm_select) / sizeof (arm_select[0]); i--;)
294     {
295       struct arm_cpu_select * ptr = arm_select + i;
296       
297       if (ptr->string != NULL && ptr->string[0] != '\0')
298         {
299           const struct processors * sel;
300
301           for (sel = ptr->processors; sel->name != NULL; sel ++)
302             if (streq (ptr->string, sel->name))
303               {
304                 if (i == 2)
305                   tune_flags = sel->flags;
306                 else
307                   {
308                     /* If we have been given an architecture and a processor
309                        make sure that they are compatible.  We only generate
310                        a warning though, and we prefer the CPU over the
311                        architecture. */
312                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
313                       warning ("switch -mcpu=%s conflicts with -march= switch",
314                                ptr->string);
315                     
316                     insn_flags = sel->flags;
317                   }
318                 
319                 break;
320               }
321
322           if (sel->name == NULL)
323             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
324         }
325     }
326   
327   /* If the user did not specify a processor, choose one for them.  */
328   if (insn_flags == 0)
329     {
330       struct processors * sel;
331       unsigned int        sought;
332       static struct cpu_default
333       {
334         int    cpu;
335         char * name;
336       }
337       cpu_defaults[] =
338       {
339         { TARGET_CPU_arm2,      "arm2" },
340         { TARGET_CPU_arm6,      "arm6" },
341         { TARGET_CPU_arm610,    "arm610" },
342         { TARGET_CPU_arm710,    "arm710" },
343         { TARGET_CPU_arm7m,     "arm7m" },
344         { TARGET_CPU_arm7500fe, "arm7500fe" },
345         { TARGET_CPU_arm7tdmi,  "arm7tdmi" },
346         { TARGET_CPU_arm8,      "arm8" },
347         { TARGET_CPU_arm810,    "arm810" },
348         { TARGET_CPU_arm9,      "arm9" },
349         { TARGET_CPU_strongarm, "strongarm" },
350         { TARGET_CPU_generic,   "arm" },
351         { 0, 0 }
352       };
353       struct cpu_default * def;
354           
355       /* Find the default.  */
356       for (def = cpu_defaults; def->name; def ++)
357         if (def->cpu == TARGET_CPU_DEFAULT)
358           break;
359
360       /* Make sure we found the default CPU.  */
361       if (def->name == NULL)
362         abort ();
363       
364       /* Find the default CPU's flags.  */
365       for (sel = all_cores; sel->name != NULL; sel ++)
366         if (streq (def->name, sel->name))
367           break;
368       
369       if (sel->name == NULL)
370         abort ();
371
372       insn_flags = sel->flags;
373       
374       /* Now check to see if the user has specified some command line
375          switch that require certain abilities from the cpu.  */
376       sought = 0;
377       
378       if (TARGET_INTERWORK)
379         {
380           sought |= (FL_THUMB | FL_MODE32);
381           
382           /* Force apcs-32 to be used for interworking.  */
383           target_flags |= ARM_FLAG_APCS_32;
384
385           /* There are no ARM processor that supports both APCS-26 and
386              interworking.  Therefore we force FL_MODE26 to be removed
387              from insn_flags here (if it was set), so that the search
388              below will always be able to find a compatible processor.  */
389           insn_flags &= ~ FL_MODE26;
390         }
391       
392       if (! TARGET_APCS_32)
393         sought |= FL_MODE26;
394
395       if (sought != 0 && ((sought & insn_flags) != sought))
396         {
397           /* Try to locate a CPU type that supports all of the abilities
398              of the default CPU, plus the extra abilities requested by
399              the user.  */
400           for (sel = all_cores; sel->name != NULL; sel ++)
401             if ((sel->flags & sought) == (sought | insn_flags))
402               break;
403
404           if (sel->name == NULL)
405             {
406               unsigned int        current_bit_count = 0;
407               struct processors * best_fit = NULL;
408               
409               /* Ideally we would like to issue an error message here
410                  saying that it was not possible to find a CPU compatible
411                  with the default CPU, but which also supports the command
412                  line options specified by the programmer, and so they
413                  ought to use the -mcpu=<name> command line option to
414                  override the default CPU type.
415
416                  Unfortunately this does not work with multilibing.  We
417                  need to be able to support multilibs for -mapcs-26 and for
418                  -mthumb-interwork and there is no CPU that can support both
419                  options.  Instead if we cannot find a cpu that has both the
420                  characteristics of the default cpu and the given command line
421                  options we scan the array again looking for a best match.  */
422               for (sel = all_cores; sel->name != NULL; sel ++)
423                 if ((sel->flags & sought) == sought)
424                   {
425                     unsigned int count;
426
427                     count = bit_count (sel->flags & insn_flags);
428
429                     if (count >= current_bit_count)
430                       {
431                         best_fit = sel;
432                         current_bit_count = count;
433                       }
434                   }
435
436               if (best_fit == NULL)
437                 abort ();
438               else
439                 sel = best_fit;
440             }
441
442           insn_flags = sel->flags;
443         }
444     }
445   
446   /* If tuning has not been specified, tune for whichever processor or
447      architecture has been selected.  */
448   if (tune_flags == 0)
449     tune_flags = insn_flags;
450   
451   /* Make sure that the processor choice does not conflict with any of the
452      other command line choices.  */
453   if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
454     {
455       /* If APCS-32 was not the default then it must have been set by the
456          user, so issue a warning message.  If the user has specified
457          "-mapcs-32 -mcpu=arm2" then we loose here.  */
458       if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
459         warning ("target CPU does not support APCS-32" );
460       target_flags &= ~ ARM_FLAG_APCS_32;
461     }
462   else if (! TARGET_APCS_32 && !(insn_flags & FL_MODE26))
463     {
464       warning ("target CPU does not support APCS-26" );
465       target_flags |= ARM_FLAG_APCS_32;
466     }
467   
468   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
469     {
470       warning ("target CPU does not support interworking" );
471       target_flags &= ~ARM_FLAG_INTERWORK;
472     }
473   
474   /* If interworking is enabled then APCS-32 must be selected as well.  */
475   if (TARGET_INTERWORK)
476     {
477       if (! TARGET_APCS_32)
478         warning ("interworking forces APCS-32 to be used" );
479       target_flags |= ARM_FLAG_APCS_32;
480     }
481   
482   if (TARGET_APCS_STACK && ! TARGET_APCS)
483     {
484       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
485       target_flags |= ARM_FLAG_APCS_FRAME;
486     }
487   
488   if (TARGET_POKE_FUNCTION_NAME)
489     target_flags |= ARM_FLAG_APCS_FRAME;
490   
491   if (TARGET_APCS_REENT && flag_pic)
492     fatal ("-fpic and -mapcs-reent are incompatible");
493   
494   if (TARGET_APCS_REENT)
495     warning ("APCS reentrant code not supported.  Ignored");
496   
497   if (write_symbols != NO_DEBUG && flag_omit_frame_pointer)
498     warning ("-g with -fomit-frame-pointer may not give sensible debugging");
499   
500   /* If stack checking is disabled, we can use r10 as the PIC register,
501      which keeps r9 available.  */
502   if (flag_pic && ! TARGET_APCS_STACK)
503     arm_pic_register = 10;
504   
505   if (TARGET_APCS_FLOAT)
506     warning ("Passing floating point arguments in fp regs not yet supported");
507   
508   /* Initialise boolean versions of the flags, for use in the arm.md file.  */
509   arm_fast_multiply = (insn_flags & FL_FAST_MULT) != 0;
510   arm_arch4         = (insn_flags & FL_ARCH4) != 0;
511   arm_arch5         = (insn_flags & FL_ARCH5) != 0;
512   
513   arm_ld_sched      = (tune_flags & FL_LDSCHED) != 0;
514   arm_is_strong     = (tune_flags & FL_STRONG) != 0;
515   arm_is_6_or_7     = ((tune_flags & (FL_MODE26 | FL_MODE32))
516                        && !(tune_flags & FL_ARCH4)) != 0;
517   
518   /* Default value for floating point code... if no co-processor
519      bus, then schedule for emulated floating point.  Otherwise,
520      assume the user has an FPA.
521      Note: this does not prevent use of floating point instructions,
522      -msoft-float does that.  */
523   arm_fpu = (tune_flags & FL_CO_PROC) ? FP_HARD : FP_SOFT3;
524   
525   if (target_fp_name)
526     {
527       if (streq (target_fp_name, "2"))
528         arm_fpu_arch = FP_SOFT2;
529       else if (streq (target_fp_name, "3"))
530         arm_fpu_arch = FP_SOFT3;
531       else
532         fatal ("Invalid floating point emulation option: -mfpe-%s",
533                target_fp_name);
534     }
535   else
536     arm_fpu_arch = FP_DEFAULT;
537   
538   if (TARGET_FPE && arm_fpu != FP_HARD)
539     arm_fpu = FP_SOFT2;
540   
541   /* For arm2/3 there is no need to do any scheduling if there is only
542      a floating point emulator, or we are doing software floating-point.  */
543   if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD)
544       && (tune_flags & FL_MODE32) == 0)
545     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
546   
547   arm_prog_mode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
548   
549   if (structure_size_string != NULL)
550     {
551       int size = strtol (structure_size_string, NULL, 0);
552       
553       if (size == 8 || size == 32)
554         arm_structure_size_boundary = size;
555       else
556         warning ("Structure size boundary can only be set to 8 or 32");
557     }
558
559   if (arm_pic_register_string != NULL)
560     {
561       int pic_register;
562
563       if (! flag_pic)
564         warning ("-mpic-register= is useless without -fpic");
565
566       pic_register = decode_reg_name (arm_pic_register_string);
567       
568       /* Prevent the user from choosing an obviously stupid PIC register.  */
569       if (pic_register < 0 || call_used_regs[pic_register]
570           || pic_register == HARD_FRAME_POINTER_REGNUM
571           || pic_register == STACK_POINTER_REGNUM
572           || pic_register >= PC_REGNUM)
573         error ("Unable to use '%s' for PIC register", arm_pic_register_string);
574       else
575         arm_pic_register = pic_register;
576     }
577   
578   /* If optimizing for space, don't synthesize constants.
579      For processors with load scheduling, it never costs more than 2 cycles
580      to load a constant, and the load scheduler may well reduce that to 1.  */
581   if (optimize_size || (tune_flags & FL_LDSCHED))
582     arm_constant_limit = 1;
583   
584   /* If optimizing for size, bump the number of instructions that we
585      are prepared to conditionally execute (even on a StrongARM). 
586      Otherwise for the StrongARM, which has early execution of branches,
587      a sequence that is worth skipping is shorter.  */
588   if (optimize_size)
589     max_insns_skipped = 6;
590   else if (arm_is_strong)
591     max_insns_skipped = 3;
592
593   /* Register global variables with the garbage collector.  */
594   arm_add_gc_roots ();
595 }
596
597 static void
598 arm_add_gc_roots ()
599 {
600   ggc_add_rtx_root (&arm_compare_op0, 1);
601   ggc_add_rtx_root (&arm_compare_op1, 1);
602   ggc_add_rtx_root (&arm_target_insn, 1); /* Not sure this is really a root */
603   /* XXX: What about the minipool tables?  */
604 }
605
606 \f
607 /* Return 1 if it is possible to return using a single instruction */
608
609 int
610 use_return_insn (iscond)
611      int iscond;
612 {
613   int regno;
614
615   if (!reload_completed
616       || current_function_pretend_args_size
617       || current_function_anonymous_args
618       || ((get_frame_size () + current_function_outgoing_args_size != 0)
619           && !(TARGET_APCS && frame_pointer_needed)))
620     return 0;
621
622   /* Can't be done if interworking with Thumb, and any registers have been
623      stacked.  Similarly, on StrongARM, conditional returns are expensive
624      if they aren't taken and registers have been stacked.  */
625   if (iscond && arm_is_strong && frame_pointer_needed)
626     return 0;
627   if ((iscond && arm_is_strong)
628       || TARGET_INTERWORK)
629     {
630       for (regno = 0; regno < 16; regno++)
631         if (regs_ever_live[regno] && ! call_used_regs[regno])
632           return 0;
633
634       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
635         return 0;
636     }
637       
638   /* Can't be done if any of the FPU regs are pushed, since this also
639      requires an insn */
640   for (regno = 16; regno < 24; regno++)
641     if (regs_ever_live[regno] && ! call_used_regs[regno])
642       return 0;
643
644   /* If a function is naked, don't use the "return" insn.  */
645   if (arm_naked_function_p (current_function_decl))
646     return 0;
647
648   return 1;
649 }
650
651 /* Return TRUE if int I is a valid immediate ARM constant.  */
652
653 int
654 const_ok_for_arm (i)
655      HOST_WIDE_INT i;
656 {
657   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
658
659   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must 
660      be all zero, or all one.  */
661   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffffUL) != 0
662       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffffUL) 
663           != ((~(unsigned HOST_WIDE_INT) 0)
664               & ~(unsigned HOST_WIDE_INT) 0xffffffffUL)))
665     return FALSE;
666   
667   /* Fast return for 0 and powers of 2 */
668   if ((i & (i - 1)) == 0)
669     return TRUE;
670
671   do
672     {
673       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffffUL) == 0)
674         return TRUE;
675       mask =
676           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffffUL)
677                          >> (32 - 2)) | ~((unsigned HOST_WIDE_INT) 0xffffffffUL);
678     } while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
679
680   return FALSE;
681 }
682
683 /* Return true if I is a valid constant for the operation CODE. */
684 static int
685 const_ok_for_op (i, code)
686      HOST_WIDE_INT i;
687      enum rtx_code code;
688 {
689   if (const_ok_for_arm (i))
690     return 1;
691
692   switch (code)
693     {
694     case PLUS:
695       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
696
697     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
698     case XOR:
699     case IOR:
700       return 0;
701
702     case AND:
703       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
704
705     default:
706       abort ();
707     }
708 }
709
710 /* Emit a sequence of insns to handle a large constant.
711    CODE is the code of the operation required, it can be any of SET, PLUS,
712    IOR, AND, XOR, MINUS;
713    MODE is the mode in which the operation is being performed;
714    VAL is the integer to operate on;
715    SOURCE is the other operand (a register, or a null-pointer for SET);
716    SUBTARGETS means it is safe to create scratch registers if that will
717    either produce a simpler sequence, or we will want to cse the values.
718    Return value is the number of insns emitted.  */
719
720 int
721 arm_split_constant (code, mode, val, target, source, subtargets)
722      enum rtx_code code;
723      enum machine_mode mode;
724      HOST_WIDE_INT val;
725      rtx target;
726      rtx source;
727      int subtargets;
728 {
729   if (subtargets || code == SET
730       || (GET_CODE (target) == REG && GET_CODE (source) == REG
731           && REGNO (target) != REGNO (source)))
732     {
733       /* After arm_reorg has been called, we can't fix up expensive
734          constants by pushing them into memory so we must synthesise
735          them in-line, regardless of the cost.  This is only likely to
736          be more costly on chips that have load delay slots and we are
737          compiling without running the scheduler (so no splitting
738          occurred before the final instruction emission).
739
740          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
741       */
742       if (! after_arm_reorg
743           && (arm_gen_constant (code, mode, val, target, source, 1, 0)
744               > arm_constant_limit + (code != SET)))
745         {
746           if (code == SET)
747             {
748               /* Currently SET is the only monadic value for CODE, all
749                  the rest are diadic.  */
750               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
751               return 1;
752             }
753           else
754             {
755               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
756
757               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
758               /* For MINUS, the value is subtracted from, since we never
759                  have subtraction of a constant.  */
760               if (code == MINUS)
761                 emit_insn (gen_rtx_SET (VOIDmode, target,
762                                         gen_rtx (code, mode, temp, source)));
763               else
764                 emit_insn (gen_rtx_SET (VOIDmode, target,
765                                         gen_rtx (code, mode, source, temp)));
766               return 2;
767             }
768         }
769     }
770
771   return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
772 }
773
774 /* As above, but extra parameter GENERATE which, if clear, suppresses
775    RTL generation.  */
776 int
777 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
778      enum rtx_code code;
779      enum machine_mode mode;
780      HOST_WIDE_INT val;
781      rtx target;
782      rtx source;
783      int subtargets;
784      int generate;
785 {
786   int can_invert = 0;
787   int can_negate = 0;
788   int can_negate_initial = 0;
789   int can_shift = 0;
790   int i;
791   int num_bits_set = 0;
792   int set_sign_bit_copies = 0;
793   int clear_sign_bit_copies = 0;
794   int clear_zero_bit_copies = 0;
795   int set_zero_bit_copies = 0;
796   int insns = 0;
797   unsigned HOST_WIDE_INT temp1, temp2;
798   unsigned HOST_WIDE_INT remainder = val & 0xffffffffUL;
799
800   /* find out which operations are safe for a given CODE.  Also do a quick
801      check for degenerate cases; these can occur when DImode operations
802      are split.  */
803   switch (code)
804     {
805     case SET:
806       can_invert = 1;
807       can_shift = 1;
808       can_negate = 1;
809       break;
810
811     case PLUS:
812       can_negate = 1;
813       can_negate_initial = 1;
814       break;
815
816     case IOR:
817       if (remainder == 0xffffffffUL)
818         {
819           if (generate)
820             emit_insn (gen_rtx_SET (VOIDmode, target,
821                                     GEN_INT (ARM_SIGN_EXTEND (val))));
822           return 1;
823         }
824       if (remainder == 0)
825         {
826           if (reload_completed && rtx_equal_p (target, source))
827             return 0;
828           if (generate)
829             emit_insn (gen_rtx_SET (VOIDmode, target, source));
830           return 1;
831         }
832       break;
833
834     case AND:
835       if (remainder == 0)
836         {
837           if (generate)
838             emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
839           return 1;
840         }
841       if (remainder == 0xffffffffUL)
842         {
843           if (reload_completed && rtx_equal_p (target, source))
844             return 0;
845           if (generate)
846             emit_insn (gen_rtx_SET (VOIDmode, target, source));
847           return 1;
848         }
849       can_invert = 1;
850       break;
851
852     case XOR:
853       if (remainder == 0)
854         {
855           if (reload_completed && rtx_equal_p (target, source))
856             return 0;
857           if (generate)
858             emit_insn (gen_rtx_SET (VOIDmode, target, source));
859           return 1;
860         }
861       if (remainder == 0xffffffffUL)
862         {
863           if (generate)
864             emit_insn (gen_rtx_SET (VOIDmode, target,
865                                     gen_rtx_NOT (mode, source)));
866           return 1;
867         }
868
869       /* We don't know how to handle this yet below.  */
870       abort ();
871
872     case MINUS:
873       /* We treat MINUS as (val - source), since (source - val) is always
874          passed as (source + (-val)).  */
875       if (remainder == 0)
876         {
877           if (generate)
878             emit_insn (gen_rtx_SET (VOIDmode, target,
879                                     gen_rtx_NEG (mode, source)));
880           return 1;
881         }
882       if (const_ok_for_arm (val))
883         {
884           if (generate)
885             emit_insn (gen_rtx_SET (VOIDmode, target, 
886                                     gen_rtx_MINUS (mode, GEN_INT (val),
887                                                    source)));
888           return 1;
889         }
890       can_negate = 1;
891
892       break;
893
894     default:
895       abort ();
896     }
897
898   /* If we can do it in one insn get out quickly */
899   if (const_ok_for_arm (val)
900       || (can_negate_initial && const_ok_for_arm (-val))
901       || (can_invert && const_ok_for_arm (~val)))
902     {
903       if (generate)
904         emit_insn (gen_rtx_SET (VOIDmode, target,
905                                 (source ? gen_rtx (code, mode, source,
906                                                    GEN_INT (val))
907                                  : GEN_INT (val))));
908       return 1;
909     }
910
911
912   /* Calculate a few attributes that may be useful for specific
913      optimizations. */
914
915   for (i = 31; i >= 0; i--)
916     {
917       if ((remainder & (1 << i)) == 0)
918         clear_sign_bit_copies++;
919       else
920         break;
921     }
922
923   for (i = 31; i >= 0; i--)
924     {
925       if ((remainder & (1 << i)) != 0)
926         set_sign_bit_copies++;
927       else
928         break;
929     }
930
931   for (i = 0; i <= 31; i++)
932     {
933       if ((remainder & (1 << i)) == 0)
934         clear_zero_bit_copies++;
935       else
936         break;
937     }
938
939   for (i = 0; i <= 31; i++)
940     {
941       if ((remainder & (1 << i)) != 0)
942         set_zero_bit_copies++;
943       else
944         break;
945     }
946
947   switch (code)
948     {
949     case SET:
950       /* See if we can do this by sign_extending a constant that is known
951          to be negative.  This is a good, way of doing it, since the shift
952          may well merge into a subsequent insn.  */
953       if (set_sign_bit_copies > 1)
954         {
955           if (const_ok_for_arm
956               (temp1 = ARM_SIGN_EXTEND (remainder 
957                                         << (set_sign_bit_copies - 1))))
958             {
959               if (generate)
960                 {
961                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
962                   emit_insn (gen_rtx_SET (VOIDmode, new_src, 
963                                           GEN_INT (temp1)));
964                   emit_insn (gen_ashrsi3 (target, new_src, 
965                                           GEN_INT (set_sign_bit_copies - 1)));
966                 }
967               return 2;
968             }
969           /* For an inverted constant, we will need to set the low bits,
970              these will be shifted out of harm's way.  */
971           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
972           if (const_ok_for_arm (~temp1))
973             {
974               if (generate)
975                 {
976                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
977                   emit_insn (gen_rtx_SET (VOIDmode, new_src,
978                                           GEN_INT (temp1)));
979                   emit_insn (gen_ashrsi3 (target, new_src, 
980                                           GEN_INT (set_sign_bit_copies - 1)));
981                 }
982               return 2;
983             }
984         }
985
986       /* See if we can generate this by setting the bottom (or the top)
987          16 bits, and then shifting these into the other half of the
988          word.  We only look for the simplest cases, to do more would cost
989          too much.  Be careful, however, not to generate this when the
990          alternative would take fewer insns.  */
991       if (val & 0xffff0000UL)
992         {
993           temp1 = remainder & 0xffff0000UL;
994           temp2 = remainder & 0x0000ffff;
995
996           /* Overlaps outside this range are best done using other methods. */
997           for (i = 9; i < 24; i++)
998             {
999               if ((((temp2 | (temp2 << i)) & 0xffffffffUL) == remainder)
1000                   && ! const_ok_for_arm (temp2))
1001                 {
1002                   rtx new_src = (subtargets
1003                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1004                                  : target);
1005                   insns = arm_gen_constant (code, mode, temp2, new_src,
1006                                             source, subtargets, generate);
1007                   source = new_src;
1008                   if (generate)
1009                     emit_insn (gen_rtx_SET
1010                                (VOIDmode, target,
1011                                 gen_rtx_IOR (mode,
1012                                              gen_rtx_ASHIFT (mode, source,
1013                                                              GEN_INT (i)),
1014                                              source)));
1015                   return insns + 1;
1016                 }
1017             }
1018
1019           /* Don't duplicate cases already considered. */
1020           for (i = 17; i < 24; i++)
1021             {
1022               if (((temp1 | (temp1 >> i)) == remainder)
1023                   && ! const_ok_for_arm (temp1))
1024                 {
1025                   rtx new_src = (subtargets
1026                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1027                                  : target);
1028                   insns = arm_gen_constant (code, mode, temp1, new_src,
1029                                             source, subtargets, generate);
1030                   source = new_src;
1031                   if (generate)
1032                     emit_insn
1033                       (gen_rtx_SET (VOIDmode, target,
1034                                     gen_rtx_IOR
1035                                     (mode,
1036                                      gen_rtx_LSHIFTRT (mode, source,
1037                                                        GEN_INT (i)),
1038                                      source)));
1039                   return insns + 1;
1040                 }
1041             }
1042         }
1043       break;
1044
1045     case IOR:
1046     case XOR:
1047       /* If we have IOR or XOR, and the constant can be loaded in a
1048          single instruction, and we can find a temporary to put it in,
1049          then this can be done in two instructions instead of 3-4.  */
1050       if (subtargets
1051           /* TARGET can't be NULL if SUBTARGETS is 0 */
1052           || (reload_completed && ! reg_mentioned_p (target, source)))
1053         {
1054           if (const_ok_for_arm (ARM_SIGN_EXTEND (~ val)))
1055             {
1056               if (generate)
1057                 {
1058                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1059
1060                   emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
1061                   emit_insn (gen_rtx_SET (VOIDmode, target, 
1062                                           gen_rtx (code, mode, source, sub)));
1063                 }
1064               return 2;
1065             }
1066         }
1067
1068       if (code == XOR)
1069         break;
1070
1071       if (set_sign_bit_copies > 8
1072           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1073         {
1074           if (generate)
1075             {
1076               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1077               rtx shift = GEN_INT (set_sign_bit_copies);
1078
1079               emit_insn (gen_rtx_SET (VOIDmode, sub,
1080                                       gen_rtx_NOT (mode, 
1081                                                    gen_rtx_ASHIFT (mode,
1082                                                                    source, 
1083                                                                    shift))));
1084               emit_insn (gen_rtx_SET (VOIDmode, target,
1085                                       gen_rtx_NOT (mode,
1086                                                    gen_rtx_LSHIFTRT (mode, sub,
1087                                                                      shift))));
1088             }
1089           return 2;
1090         }
1091
1092       if (set_zero_bit_copies > 8
1093           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1094         {
1095           if (generate)
1096             {
1097               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1098               rtx shift = GEN_INT (set_zero_bit_copies);
1099
1100               emit_insn (gen_rtx_SET (VOIDmode, sub,
1101                                       gen_rtx_NOT (mode,
1102                                                    gen_rtx_LSHIFTRT (mode,
1103                                                                      source,
1104                                                                      shift))));
1105               emit_insn (gen_rtx_SET (VOIDmode, target,
1106                                       gen_rtx_NOT (mode,
1107                                                    gen_rtx_ASHIFT (mode, sub,
1108                                                                    shift))));
1109             }
1110           return 2;
1111         }
1112
1113       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~ val)))
1114         {
1115           if (generate)
1116             {
1117               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1118               emit_insn (gen_rtx_SET (VOIDmode, sub,
1119                                       gen_rtx_NOT (mode, source)));
1120               source = sub;
1121               if (subtargets)
1122                 sub = gen_reg_rtx (mode);
1123               emit_insn (gen_rtx_SET (VOIDmode, sub,
1124                                       gen_rtx_AND (mode, source, 
1125                                                    GEN_INT (temp1))));
1126               emit_insn (gen_rtx_SET (VOIDmode, target,
1127                                       gen_rtx_NOT (mode, sub)));
1128             }
1129           return 3;
1130         }
1131       break;
1132
1133     case AND:
1134       /* See if two shifts will do 2 or more insn's worth of work.  */
1135       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1136         {
1137           HOST_WIDE_INT shift_mask = ((0xffffffffUL
1138                                        << (32 - clear_sign_bit_copies))
1139                                       & 0xffffffffUL);
1140
1141           if ((remainder | shift_mask) != 0xffffffffUL)
1142             {
1143               if (generate)
1144                 {
1145                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1146                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1147                                             new_src, source, subtargets, 1);
1148                   source = new_src;
1149                 }
1150               else
1151                 {
1152                   rtx targ = subtargets ? NULL_RTX : target;
1153                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1154                                             targ, source, subtargets, 0);
1155                 }
1156             }
1157
1158           if (generate)
1159             {
1160               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1161               rtx shift = GEN_INT (clear_sign_bit_copies);
1162
1163               emit_insn (gen_ashlsi3 (new_src, source, shift));
1164               emit_insn (gen_lshrsi3 (target, new_src, shift));
1165             }
1166
1167           return insns + 2;
1168         }
1169
1170       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1171         {
1172           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1173           
1174           if ((remainder | shift_mask) != 0xffffffffUL)
1175             {
1176               if (generate)
1177                 {
1178                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1179
1180                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1181                                             new_src, source, subtargets, 1);
1182                   source = new_src;
1183                 }
1184               else
1185                 {
1186                   rtx targ = subtargets ? NULL_RTX : target;
1187
1188                   insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1189                                             targ, source, subtargets, 0);
1190                 }
1191             }
1192
1193           if (generate)
1194             {
1195               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1196               rtx shift = GEN_INT (clear_zero_bit_copies);
1197
1198               emit_insn (gen_lshrsi3 (new_src, source, shift));
1199               emit_insn (gen_ashlsi3 (target, new_src, shift));
1200             }
1201
1202           return insns + 2;
1203         }
1204
1205       break;
1206
1207     default:
1208       break;
1209     }
1210
1211   for (i = 0; i < 32; i++)
1212     if (remainder & (1 << i))
1213       num_bits_set++;
1214
1215   if (code == AND || (can_invert && num_bits_set > 16))
1216     remainder = (~remainder) & 0xffffffffUL;
1217   else if (code == PLUS && num_bits_set > 16)
1218     remainder = (-remainder) & 0xffffffffUL;
1219   else
1220     {
1221       can_invert = 0;
1222       can_negate = 0;
1223     }
1224
1225   /* Now try and find a way of doing the job in either two or three
1226      instructions.
1227      We start by looking for the largest block of zeros that are aligned on
1228      a 2-bit boundary, we then fill up the temps, wrapping around to the
1229      top of the word when we drop off the bottom.
1230      In the worst case this code should produce no more than four insns. */
1231   {
1232     int best_start = 0;
1233     int best_consecutive_zeros = 0;
1234
1235     for (i = 0; i < 32; i += 2)
1236       {
1237         int consecutive_zeros = 0;
1238
1239         if (! (remainder & (3 << i)))
1240           {
1241             while ((i < 32) && ! (remainder & (3 << i)))
1242               {
1243                 consecutive_zeros += 2;
1244                 i += 2;
1245               }
1246             if (consecutive_zeros > best_consecutive_zeros)
1247               {
1248                 best_consecutive_zeros = consecutive_zeros;
1249                 best_start = i - consecutive_zeros;
1250               }
1251             i -= 2;
1252           }
1253       }
1254
1255     /* Now start emitting the insns, starting with the one with the highest
1256        bit set: we do this so that the smallest number will be emitted last;
1257        this is more likely to be combinable with addressing insns. */
1258     i = best_start;
1259     do
1260       {
1261         int end;
1262
1263         if (i <= 0)
1264           i += 32;
1265         if (remainder & (3 << (i - 2)))
1266           {
1267             end = i - 8;
1268             if (end < 0)
1269               end += 32;
1270             temp1 = remainder & ((0x0ff << end)
1271                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
1272             remainder &= ~temp1;
1273
1274             if (generate)
1275               {
1276                 rtx new_src;
1277
1278                 if (code == SET)
1279                   emit_insn (gen_rtx_SET (VOIDmode,
1280                                           new_src = (subtargets
1281                                                      ? gen_reg_rtx (mode)
1282                                                      : target),
1283                                           GEN_INT (can_invert
1284                                                    ? ~temp1 : temp1)));
1285                 else if (code == MINUS)
1286                   emit_insn (gen_rtx_SET (VOIDmode,
1287                                           new_src = (subtargets
1288                                                      ? gen_reg_rtx (mode)
1289                                                      : target),
1290                                           gen_rtx (code, mode, GEN_INT (temp1),
1291                                                    source)));
1292                 else
1293                   emit_insn (gen_rtx_SET (VOIDmode,
1294                                           new_src = (remainder
1295                                                      ? (subtargets
1296                                                         ? gen_reg_rtx (mode)
1297                                                         : target)
1298                                                      : target),
1299                                           gen_rtx (code, mode, source,
1300                                                    GEN_INT (can_invert ? ~temp1
1301                                                             : (can_negate
1302                                                                ? -temp1
1303                                                                : temp1)))));
1304                 source = new_src;
1305               }
1306
1307             if (code == SET)
1308               {
1309                 can_invert = 0;
1310                 code = PLUS;
1311               }
1312             else if (code == MINUS)
1313               code = PLUS;
1314
1315             insns++;
1316             i -= 6;
1317           }
1318         i -= 2;
1319       } while (remainder);
1320   }
1321   return insns;
1322 }
1323
1324 /* Canonicalize a comparison so that we are more likely to recognize it.
1325    This can be done for a few constant compares, where we can make the
1326    immediate value easier to load.  */
1327 enum rtx_code
1328 arm_canonicalize_comparison (code, op1)
1329      enum rtx_code code;
1330      rtx * op1;
1331 {
1332   unsigned HOST_WIDE_INT i = INTVAL (*op1);
1333
1334   switch (code)
1335     {
1336     case EQ:
1337     case NE:
1338       return code;
1339
1340     case GT:
1341     case LE:
1342       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1343                 - 1)
1344           && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1345         {
1346           *op1 = GEN_INT (i+1);
1347           return code == GT ? GE : LT;
1348         }
1349       break;
1350
1351     case GE:
1352     case LT:
1353       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1354           && (const_ok_for_arm (i-1) || const_ok_for_arm (- (i-1))))
1355         {
1356           *op1 = GEN_INT (i-1);
1357           return code == GE ? GT : LE;
1358         }
1359       break;
1360
1361     case GTU:
1362     case LEU:
1363       if (i != ~((unsigned HOST_WIDE_INT) 0)
1364           && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1365         {
1366           *op1 = GEN_INT (i + 1);
1367           return code == GTU ? GEU : LTU;
1368         }
1369       break;
1370
1371     case GEU:
1372     case LTU:
1373       if (i != 0
1374           && (const_ok_for_arm (i - 1) || const_ok_for_arm (- (i - 1))))
1375         {
1376           *op1 = GEN_INT (i - 1);
1377           return code == GEU ? GTU : LEU;
1378         }
1379       break;
1380
1381     default:
1382       abort ();
1383     }
1384
1385   return code;
1386 }
1387
1388 /* Decide whether a type should be returned in memory (true)
1389    or in a register (false).  This is called by the macro
1390    RETURN_IN_MEMORY.  */
1391 int
1392 arm_return_in_memory (type)
1393      tree type;
1394 {
1395   if (! AGGREGATE_TYPE_P (type))
1396     {
1397       /* All simple types are returned in registers. */
1398       return 0;
1399     }
1400   else if (int_size_in_bytes (type) > 4)
1401     {
1402       /* All structures/unions bigger than one word are returned in memory. */
1403       return 1;
1404     }
1405   else if (TREE_CODE (type) == RECORD_TYPE)
1406     {
1407       tree field;
1408
1409       /* For a struct the APCS says that we only return in a register
1410          if the type is 'integer like' and every addressable element
1411          has an offset of zero.  For practical purposes this means
1412          that the structure can have at most one non bit-field element
1413          and that this element must be the first one in the structure.  */
1414       
1415       /* Find the first field, ignoring non FIELD_DECL things which will
1416          have been created by C++.  */
1417       for (field = TYPE_FIELDS (type);
1418            field && TREE_CODE (field) != FIELD_DECL;
1419            field = TREE_CHAIN (field))
1420         continue;
1421       
1422       if (field == NULL)
1423         return 0; /* An empty structure.  Allowed by an extension to ANSI C. */
1424
1425       /* Check that the first field is valid for returning in a register...  */
1426
1427       /* ... Floats are not allowed */
1428       if (FLOAT_TYPE_P (TREE_TYPE (field)))
1429         return 1;
1430
1431       /* ... Aggregates that are not themselves valid for returning in
1432          a register are not allowed.  */
1433       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1434         return 1;
1435
1436       /* Now check the remaining fields, if any.  Only bitfields are allowed,
1437          since they are not addressable.  */
1438       for (field = TREE_CHAIN (field);
1439            field;
1440            field = TREE_CHAIN (field))
1441         {
1442           if (TREE_CODE (field) != FIELD_DECL)
1443             continue;
1444           
1445           if (! DECL_BIT_FIELD_TYPE (field))
1446             return 1;
1447         }
1448
1449       return 0;
1450     }
1451   else if (TREE_CODE (type) == UNION_TYPE)
1452     {
1453       tree field;
1454
1455       /* Unions can be returned in registers if every element is
1456          integral, or can be returned in an integer register.  */
1457       for (field = TYPE_FIELDS (type);
1458            field;
1459            field = TREE_CHAIN (field))
1460         {
1461           if (TREE_CODE (field) != FIELD_DECL)
1462             continue;
1463
1464           if (FLOAT_TYPE_P (TREE_TYPE (field)))
1465             return 1;
1466           
1467           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1468             return 1;
1469         }
1470       
1471       return 0;
1472     }
1473   
1474   /* XXX Not sure what should be done for other aggregates, so put them in
1475      memory. */
1476   return 1;
1477 }
1478
1479 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1480    for a call to a function whose data type is FNTYPE.
1481    For a library call, FNTYPE is NULL.  */
1482 void
1483 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1484      CUMULATIVE_ARGS * pcum;
1485      tree fntype;
1486      rtx libname  ATTRIBUTE_UNUSED;
1487      int indirect ATTRIBUTE_UNUSED;
1488 {
1489   /* On the ARM, the offset starts at 0.  */
1490   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype)))
1491                  ? 1 : 0);
1492
1493   pcum->call_cookie = CALL_NORMAL;
1494
1495   if (TARGET_LONG_CALLS)
1496     pcum->call_cookie = CALL_LONG;
1497     
1498   /* Check for long call/short call attributes.  The attributes
1499      override any command line option.  */
1500   if (fntype)
1501     {
1502       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1503         pcum->call_cookie = CALL_SHORT;
1504       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1505         pcum->call_cookie = CALL_LONG;
1506     }
1507 }
1508
1509 /* Determine where to put an argument to a function.
1510    Value is zero to push the argument on the stack,
1511    or a hard register in which to store the argument.
1512
1513    MODE is the argument's machine mode.
1514    TYPE is the data type of the argument (as a tree).
1515     This is null for libcalls where that information may
1516     not be available.
1517    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1518     the preceding args and about the function being called.
1519    NAMED is nonzero if this argument is a named parameter
1520     (otherwise it is an extra parameter matching an ellipsis).  */
1521 rtx
1522 arm_function_arg (pcum, mode, type, named)
1523      CUMULATIVE_ARGS * pcum;
1524      enum machine_mode mode;
1525      tree type ATTRIBUTE_UNUSED;
1526      int named;
1527 {
1528   if (mode == VOIDmode)
1529     /* Compute operand 2 of the call insn.  */
1530     return GEN_INT (pcum->call_cookie);
1531   
1532   if (! named || pcum->nregs >= NUM_ARG_REGS)
1533     return NULL_RTX;
1534   
1535   return gen_rtx_REG (mode, pcum->nregs);
1536 }
1537
1538 \f
1539 /* Return 1 if the operand is a SYMBOL_REF for a function
1540    known to be defined in the current compilation unit.  */
1541 static int
1542 current_file_function_operand (sym_ref)
1543   rtx sym_ref;
1544 {
1545   /* XXX FIXME - we need some way to determine if SYMREF has already been
1546      compiled.  We wanted to used SYMBOL_REF_FLAG but this is already in use
1547      by the constant pool generation code.  */
1548   return
1549     GET_CODE (sym_ref) == SYMBOL_REF
1550     && sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
1551     && ! DECL_WEAK (current_function_decl);
1552 }
1553
1554 /* Return non-zero if a 32 bit "long call" should be generated for this
1555    call.
1556
1557    We generate a long call if the function is not declared
1558    __attribute__ ((short_call),
1559
1560    AND:
1561    
1562      (1)  the function is declared __attribute__ ((long_call))
1563
1564    OR
1565
1566      (2) -mlong-calls is enabled and we don't know whether the target
1567          function is declared in this file.
1568          
1569    This function will typically be called by C fragments in the machine
1570    description file.  CALL_REF is the matched rtl operand.  CALL_COOKIE
1571    describes the value of the long_call and short_call attributes for
1572    the called functiion.  CALL_SYMBOL is used to distinguish between
1573    two different callers of the function.  It is set to 1 in the "call_symbol"
1574    and "call_symbol_value" patterns in arm.md and to 0 in the "call" and
1575    "call_value" patterns.  This is because of the difference of SYM_REFs passed
1576    from "call_symbol" and "call"  patterns.  */
1577 int
1578 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
1579   rtx sym_ref;
1580   int call_cookie;
1581   int call_symbol;
1582 {
1583   if (! call_symbol)
1584     {
1585       if (GET_CODE (sym_ref) != MEM)
1586         return 0;
1587
1588       sym_ref = XEXP (sym_ref, 0);
1589     }
1590
1591   if (GET_CODE (sym_ref) != SYMBOL_REF)
1592     return 0;
1593
1594   if (call_cookie & CALL_SHORT)
1595     return 0;
1596
1597   if (TARGET_LONG_CALLS && flag_function_sections)
1598     return 1;
1599   
1600   if (current_file_function_operand (sym_ref, VOIDmode))
1601     return 0;
1602   
1603   return (call_cookie & CALL_LONG) || TARGET_LONG_CALLS;
1604 }
1605 \f
1606 /* Return nonzero if IDENTIFIER with arguments ARGS is a valid machine specific
1607    attribute for TYPE.  The attributes in ATTRIBUTES have previously been
1608    assigned to TYPE.  */
1609 int
1610 arm_valid_type_attribute_p (type, attributes, identifier, args)
1611      tree type;
1612      tree attributes ATTRIBUTE_UNUSED;
1613      tree identifier;
1614      tree args;
1615 {
1616   if (   TREE_CODE (type) != FUNCTION_TYPE
1617       && TREE_CODE (type) != METHOD_TYPE
1618       && TREE_CODE (type) != FIELD_DECL
1619       && TREE_CODE (type) != TYPE_DECL)
1620     return 0;
1621
1622   /* Function calls made to this symbol must be done indirectly, because
1623      it may lie outside of the 26 bit addressing range of a normal function
1624      call.  */
1625   if (is_attribute_p ("long_call", identifier))
1626     return (args == NULL_TREE);
1627
1628   /* Whereas these functions are always known to reside within the 26 bit
1629      addressing range.  */
1630   if (is_attribute_p ("short_call", identifier))
1631     return (args == NULL_TREE);
1632   
1633   return 0;
1634 }
1635
1636 /* Return 0 if the attributes for two types are incompatible, 1 if they
1637    are compatible, and 2 if they are nearly compatible (which causes a
1638    warning to be generated).  */
1639 int
1640 arm_comp_type_attributes (type1, type2)
1641      tree type1;
1642      tree type2;
1643 {
1644   int l1, l2, s1, s2;
1645   
1646   /* Check for mismatch of non-default calling convention.  */
1647   if (TREE_CODE (type1) != FUNCTION_TYPE)
1648     return 1;
1649
1650   /* Check for mismatched call attributes.  */
1651   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1));
1652   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2));
1653   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1));
1654   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2));
1655
1656   /* Only bother to check if an attribute is defined.  */
1657   if (l1 | l2 | s1 | s2)
1658     {
1659       /* If one type has an attribute, the other must have the same attribute.  */
1660       if ((!l1 != !l2) || (!s1 != !s2))
1661         return 0;
1662
1663       /* Disallow mixed attributes.  */
1664       if ((l1 & s2) || (l2 & s1))
1665         return 0;
1666     }
1667   
1668   return 1;
1669 }
1670
1671 \f
1672 int
1673 legitimate_pic_operand_p (x)
1674      rtx x;
1675 {
1676   if (CONSTANT_P (x) && flag_pic
1677       && (GET_CODE (x) == SYMBOL_REF
1678           || (GET_CODE (x) == CONST
1679               && GET_CODE (XEXP (x, 0)) == PLUS
1680               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
1681     return 0;
1682
1683   return 1;
1684 }
1685
1686 rtx
1687 legitimize_pic_address (orig, mode, reg)
1688      rtx orig;
1689      enum machine_mode mode;
1690      rtx reg;
1691 {
1692   if (GET_CODE (orig) == SYMBOL_REF)
1693     {
1694       rtx pic_ref, address;
1695       rtx insn;
1696       int subregs = 0;
1697
1698       if (reg == 0)
1699         {
1700           if (reload_in_progress || reload_completed)
1701             abort ();
1702           else
1703             reg = gen_reg_rtx (Pmode);
1704
1705           subregs = 1;
1706         }
1707
1708 #ifdef AOF_ASSEMBLER
1709       /* The AOF assembler can generate relocations for these directly, and
1710          understands that the PIC register has to be added into the offset.
1711          */
1712       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
1713 #else
1714       if (subregs)
1715         address = gen_reg_rtx (Pmode);
1716       else
1717         address = reg;
1718
1719       emit_insn (gen_pic_load_addr (address, orig));
1720
1721       pic_ref = gen_rtx_MEM (Pmode,
1722                              gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
1723                                            address));
1724       RTX_UNCHANGING_P (pic_ref) = 1;
1725       insn = emit_move_insn (reg, pic_ref);
1726 #endif
1727       current_function_uses_pic_offset_table = 1;
1728       /* Put a REG_EQUAL note on this insn, so that it can be optimized
1729          by loop.  */
1730       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
1731                                             REG_NOTES (insn));
1732       return reg;
1733     }
1734   else if (GET_CODE (orig) == CONST)
1735     {
1736       rtx base, offset;
1737
1738       if (GET_CODE (XEXP (orig, 0)) == PLUS
1739           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1740         return orig;
1741
1742       if (reg == 0)
1743         {
1744           if (reload_in_progress || reload_completed)
1745             abort ();
1746           else
1747             reg = gen_reg_rtx (Pmode);
1748         }
1749
1750       if (GET_CODE (XEXP (orig, 0)) == PLUS)
1751         {
1752           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1753           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1754                                            base == reg ? 0 : reg);
1755         }
1756       else
1757         abort ();
1758
1759       if (GET_CODE (offset) == CONST_INT)
1760         {
1761           /* The base register doesn't really matter, we only want to
1762              test the index for the appropriate mode.  */
1763           GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
1764
1765           if (! reload_in_progress && ! reload_completed)
1766             offset = force_reg (Pmode, offset);
1767           else
1768             abort ();
1769
1770         win:
1771           if (GET_CODE (offset) == CONST_INT)
1772             return plus_constant_for_output (base, INTVAL (offset));
1773         }
1774
1775       if (GET_MODE_SIZE (mode) > 4
1776           && (GET_MODE_CLASS (mode) == MODE_INT
1777               || TARGET_SOFT_FLOAT))
1778         {
1779           emit_insn (gen_addsi3 (reg, base, offset));
1780           return reg;
1781         }
1782
1783       return gen_rtx_PLUS (Pmode, base, offset);
1784     }
1785   else if (GET_CODE (orig) == LABEL_REF)
1786     {
1787       current_function_uses_pic_offset_table = 1;
1788       
1789       if (NEED_GOT_RELOC)
1790         {
1791           rtx pic_ref, address = gen_reg_rtx (Pmode);
1792           
1793           emit_insn (gen_pic_load_addr (address, orig));
1794           pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
1795           
1796           emit_move_insn (address, pic_ref);
1797           return address;
1798         }
1799     }
1800
1801   return orig;
1802 }
1803
1804 static rtx pic_rtx;
1805
1806 int
1807 is_pic (x)
1808      rtx x;
1809 {
1810   if (x == pic_rtx)
1811     return 1;
1812   return 0;
1813 }
1814
1815 void
1816 arm_finalize_pic ()
1817 {
1818 #ifndef AOF_ASSEMBLER
1819   rtx l1, pic_tmp, pic_tmp2, seq;
1820   rtx global_offset_table;
1821
1822   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
1823     return;
1824
1825   if (! flag_pic)
1826     abort ();
1827
1828   start_sequence ();
1829   l1 = gen_label_rtx ();
1830
1831   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1832   /* On the ARM the PC register contains 'dot + 8' at the time of the
1833      addition.  */
1834   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), 8);
1835   if (GOT_PCREL)
1836     pic_tmp2 = gen_rtx_CONST (VOIDmode,
1837                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
1838   else
1839     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
1840
1841   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
1842   
1843   emit_insn (gen_pic_load_addr (pic_offset_table_rtx, pic_rtx));
1844   emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
1845
1846   seq = gen_sequence ();
1847   end_sequence ();
1848   emit_insn_after (seq, get_insns ());
1849
1850   /* Need to emit this whether or not we obey regdecls,
1851      since setjmp/longjmp can cause life info to screw up.  */
1852   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
1853 #endif /* AOF_ASSEMBLER */
1854 }
1855
1856 #define REG_OR_SUBREG_REG(X)                                            \
1857   (GET_CODE (X) == REG                                                  \
1858    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
1859
1860 #define REG_OR_SUBREG_RTX(X)                    \
1861    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
1862
1863 #define ARM_FRAME_RTX(X)                                \
1864   ((X) == frame_pointer_rtx || (X) == stack_pointer_rtx \
1865    || (X) == arg_pointer_rtx)
1866
1867 int
1868 arm_rtx_costs (x, code)
1869      rtx x;
1870      enum rtx_code code;
1871 {
1872   enum machine_mode mode = GET_MODE (x);
1873   enum rtx_code subcode;
1874   int extra_cost;
1875
1876   switch (code)
1877     {
1878     case MEM:
1879       /* Memory costs quite a lot for the first word, but subsequent words
1880          load at the equivalent of a single insn each.  */
1881       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
1882               + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
1883
1884     case DIV:
1885     case MOD:
1886       return 100;
1887
1888     case ROTATE:
1889       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
1890         return 4;
1891       /* Fall through */
1892     case ROTATERT:
1893       if (mode != SImode)
1894         return 8;
1895       /* Fall through */
1896     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
1897       if (mode == DImode)
1898         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
1899                 + ((GET_CODE (XEXP (x, 0)) == REG 
1900                     || (GET_CODE (XEXP (x, 0)) == SUBREG
1901                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
1902                    ? 0 : 8));
1903       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
1904                     || (GET_CODE (XEXP (x, 0)) == SUBREG
1905                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
1906                    ? 0 : 4)
1907               + ((GET_CODE (XEXP (x, 1)) == REG
1908                   || (GET_CODE (XEXP (x, 1)) == SUBREG
1909                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
1910                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
1911                  ? 0 : 4));
1912
1913     case MINUS:
1914       if (mode == DImode)
1915         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
1916                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
1917                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
1918                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
1919                    ? 0 : 8));
1920
1921       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1922         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1923                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
1924                           && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
1925                      ? 0 : 8)
1926                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
1927                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
1928                         && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
1929                    ? 0 : 8));
1930
1931       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
1932             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
1933             && REG_OR_SUBREG_REG (XEXP (x, 1))))
1934           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
1935                || subcode == ASHIFTRT || subcode == LSHIFTRT
1936                || subcode == ROTATE || subcode == ROTATERT
1937                || (subcode == MULT
1938                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
1939                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
1940                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
1941               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
1942               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
1943                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
1944               && REG_OR_SUBREG_REG (XEXP (x, 0))))
1945         return 1;
1946       /* Fall through */
1947
1948     case PLUS: 
1949       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1950         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
1951                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1952                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
1953                         && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
1954                    ? 0 : 8));
1955
1956       /* Fall through */
1957     case AND: case XOR: case IOR: 
1958       extra_cost = 0;
1959
1960       /* Normally the frame registers will be spilt into reg+const during
1961          reload, so it is a bad idea to combine them with other instructions,
1962          since then they might not be moved outside of loops.  As a compromise
1963          we allow integration with ops that have a constant as their second
1964          operand.  */
1965       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
1966            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
1967            && GET_CODE (XEXP (x, 1)) != CONST_INT)
1968           || (REG_OR_SUBREG_REG (XEXP (x, 0))
1969               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
1970         extra_cost = 4;
1971
1972       if (mode == DImode)
1973         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
1974                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1975                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
1976                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
1977                    ? 0 : 8));
1978
1979       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
1980         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
1981                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
1982                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
1983                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
1984                    ? 0 : 4));
1985
1986       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
1987         return (1 + extra_cost
1988                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
1989                      || subcode == LSHIFTRT || subcode == ASHIFTRT
1990                      || subcode == ROTATE || subcode == ROTATERT
1991                      || (subcode == MULT
1992                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1993                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
1994                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
1995                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
1996                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
1997                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
1998                    ? 0 : 4));
1999
2000       return 8;
2001
2002     case MULT:
2003       /* There is no point basing this on the tuning, since it is always the
2004          fast variant if it exists at all */
2005       if (arm_fast_multiply && mode == DImode
2006           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
2007           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2008               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
2009         return 8;
2010
2011       if (GET_MODE_CLASS (mode) == MODE_FLOAT
2012           || mode == DImode)
2013         return 30;
2014
2015       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2016         {
2017           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
2018                                       & (unsigned HOST_WIDE_INT) 0xffffffffUL);
2019           int add_cost = const_ok_for_arm (i) ? 4 : 8;
2020           int j;
2021           /* Tune as appropriate */ 
2022           int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
2023           
2024           for (j = 0; i && j < 32; j += booth_unit_size)
2025             {
2026               i >>= booth_unit_size;
2027               add_cost += 2;
2028             }
2029
2030           return add_cost;
2031         }
2032
2033       return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
2034               + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
2035               + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
2036
2037     case TRUNCATE:
2038       if (arm_fast_multiply && mode == SImode
2039           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
2040           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2041           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
2042               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
2043           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
2044               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
2045         return 8;
2046       return 99;
2047
2048     case NEG:
2049       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2050         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
2051       /* Fall through */
2052     case NOT:
2053       if (mode == DImode)
2054         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2055
2056       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2057
2058     case IF_THEN_ELSE:
2059       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2060         return 14;
2061       return 2;
2062
2063     case COMPARE:
2064       return 1;
2065
2066     case ABS:
2067       return 4 + (mode == DImode ? 4 : 0);
2068
2069     case SIGN_EXTEND:
2070       if (GET_MODE (XEXP (x, 0)) == QImode)
2071         return (4 + (mode == DImode ? 4 : 0)
2072                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2073       /* Fall through */
2074     case ZERO_EXTEND:
2075       switch (GET_MODE (XEXP (x, 0)))
2076         {
2077         case QImode:
2078           return (1 + (mode == DImode ? 4 : 0)
2079                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2080
2081         case HImode:
2082           return (4 + (mode == DImode ? 4 : 0)
2083                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2084
2085         case SImode:
2086           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2087
2088         default:
2089           break;
2090         }
2091       abort ();
2092
2093     default:
2094       return 99;
2095     }
2096 }
2097
2098 int
2099 arm_adjust_cost (insn, link, dep, cost)
2100      rtx insn;
2101      rtx link;
2102      rtx dep;
2103      int cost;
2104 {
2105   rtx i_pat, d_pat;
2106
2107   /* XXX This is not strictly true for the FPA. */
2108   if (REG_NOTE_KIND(link) == REG_DEP_ANTI
2109       || REG_NOTE_KIND(link) == REG_DEP_OUTPUT)
2110     return 0;
2111
2112   if ((i_pat = single_set (insn)) != NULL
2113       && GET_CODE (SET_SRC (i_pat)) == MEM
2114       && (d_pat = single_set (dep)) != NULL
2115       && GET_CODE (SET_DEST (d_pat)) == MEM)
2116     {
2117       /* This is a load after a store, there is no conflict if the load reads
2118          from a cached area.  Assume that loads from the stack, and from the
2119          constant pool are cached, and that others will miss.  This is a 
2120          hack. */
2121       
2122       if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
2123           || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2124           || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2125           || reg_mentioned_p (hard_frame_pointer_rtx, 
2126                               XEXP (SET_SRC (i_pat), 0)))
2127         return 1;
2128     }
2129
2130   return cost;
2131 }
2132
2133 /* This code has been fixed for cross compilation. */
2134
2135 static int fpa_consts_inited = 0;
2136
2137 char * strings_fpa[8] =
2138 {
2139   "0",   "1",   "2",   "3",
2140   "4",   "5",   "0.5", "10"
2141 };
2142
2143 static REAL_VALUE_TYPE values_fpa[8];
2144
2145 static void
2146 init_fpa_table ()
2147 {
2148   int i;
2149   REAL_VALUE_TYPE r;
2150
2151   for (i = 0; i < 8; i++)
2152     {
2153       r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
2154       values_fpa[i] = r;
2155     }
2156
2157   fpa_consts_inited = 1;
2158 }
2159
2160 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2161
2162 int
2163 const_double_rtx_ok_for_fpu (x)
2164      rtx x;
2165 {
2166   REAL_VALUE_TYPE r;
2167   int i;
2168   
2169   if (!fpa_consts_inited)
2170     init_fpa_table ();
2171   
2172   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2173   if (REAL_VALUE_MINUS_ZERO (r))
2174     return 0;
2175
2176   for (i = 0; i < 8; i++)
2177     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2178       return 1;
2179
2180   return 0;
2181 }
2182
2183 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2184
2185 int
2186 neg_const_double_rtx_ok_for_fpu (x)
2187      rtx x;
2188 {
2189   REAL_VALUE_TYPE r;
2190   int i;
2191   
2192   if (!fpa_consts_inited)
2193     init_fpa_table ();
2194   
2195   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2196   r = REAL_VALUE_NEGATE (r);
2197   if (REAL_VALUE_MINUS_ZERO (r))
2198     return 0;
2199
2200   for (i = 0; i < 8; i++)
2201     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2202       return 1;
2203
2204   return 0;
2205 }
2206 \f
2207 /* Predicates for `match_operand' and `match_operator'.  */
2208
2209 /* s_register_operand is the same as register_operand, but it doesn't accept
2210    (SUBREG (MEM)...).
2211
2212    This function exists because at the time it was put in it led to better
2213    code.  SUBREG(MEM) always needs a reload in the places where
2214    s_register_operand is used, and this seemed to lead to excessive
2215    reloading.  */
2216
2217 int
2218 s_register_operand (op, mode)
2219      register rtx op;
2220      enum machine_mode mode;
2221 {
2222   if (GET_MODE (op) != mode && mode != VOIDmode)
2223     return 0;
2224
2225   if (GET_CODE (op) == SUBREG)
2226     op = SUBREG_REG (op);
2227
2228   /* We don't consider registers whose class is NO_REGS
2229      to be a register operand.  */
2230   return (GET_CODE (op) == REG
2231           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2232               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2233 }
2234
2235 /* Only accept reg, subreg(reg), const_int.  */
2236
2237 int
2238 reg_or_int_operand (op, mode)
2239      register rtx op;
2240      enum machine_mode mode;
2241 {
2242   if (GET_CODE (op) == CONST_INT)
2243     return 1;
2244
2245   if (GET_MODE (op) != mode && mode != VOIDmode)
2246     return 0;
2247
2248   if (GET_CODE (op) == SUBREG)
2249     op = SUBREG_REG (op);
2250
2251   /* We don't consider registers whose class is NO_REGS
2252      to be a register operand.  */
2253   return (GET_CODE (op) == REG
2254           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2255               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2256 }
2257
2258 /* Return 1 if OP is an item in memory, given that we are in reload.  */
2259
2260 int
2261 reload_memory_operand (op, mode)
2262      rtx op;
2263      enum machine_mode mode ATTRIBUTE_UNUSED;
2264 {
2265   int regno = true_regnum (op);
2266
2267   return (! CONSTANT_P (op)
2268           && (regno == -1
2269               || (GET_CODE (op) == REG
2270                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2271 }
2272
2273 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
2274    memory access (architecture V4) */
2275 int
2276 bad_signed_byte_operand (op, mode)
2277      rtx op;
2278      enum machine_mode mode;
2279 {
2280   if (! memory_operand (op, mode) || GET_CODE (op) != MEM)
2281     return 0;
2282
2283   op = XEXP (op, 0);
2284
2285   /* A sum of anything more complex than reg + reg or reg + const is bad */
2286   if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
2287       && (! s_register_operand (XEXP (op, 0), VOIDmode)
2288           || (! s_register_operand (XEXP (op, 1), VOIDmode)
2289               && GET_CODE (XEXP (op, 1)) != CONST_INT)))
2290     return 1;
2291
2292   /* Big constants are also bad */
2293   if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
2294       && (INTVAL (XEXP (op, 1)) > 0xff
2295           || -INTVAL (XEXP (op, 1)) > 0xff))
2296     return 1;
2297
2298   /* Everything else is good, or can will automatically be made so. */
2299   return 0;
2300 }
2301
2302 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
2303
2304 int
2305 arm_rhs_operand (op, mode)
2306      rtx op;
2307      enum machine_mode mode;
2308 {
2309   return (s_register_operand (op, mode)
2310           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
2311 }
2312
2313 /* Return TRUE for valid operands for the rhs of an ARM instruction, or a load.
2314  */
2315
2316 int
2317 arm_rhsm_operand (op, mode)
2318      rtx op;
2319      enum machine_mode mode;
2320 {
2321   return (s_register_operand (op, mode)
2322           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
2323           || memory_operand (op, mode));
2324 }
2325
2326 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
2327    constant that is valid when negated.  */
2328
2329 int
2330 arm_add_operand (op, mode)
2331      rtx op;
2332      enum machine_mode mode;
2333 {
2334   return (s_register_operand (op, mode)
2335           || (GET_CODE (op) == CONST_INT
2336               && (const_ok_for_arm (INTVAL (op))
2337                   || const_ok_for_arm (-INTVAL (op)))));
2338 }
2339
2340 int
2341 arm_not_operand (op, mode)
2342      rtx op;
2343      enum machine_mode mode;
2344 {
2345   return (s_register_operand (op, mode)
2346           || (GET_CODE (op) == CONST_INT
2347               && (const_ok_for_arm (INTVAL (op))
2348                   || const_ok_for_arm (~INTVAL (op)))));
2349 }
2350
2351 /* Return TRUE if the operand is a memory reference which contains an
2352    offsettable address.  */
2353 int
2354 offsettable_memory_operand (op, mode)
2355      register rtx op;
2356      enum machine_mode mode;
2357 {
2358   if (mode == VOIDmode)
2359     mode = GET_MODE (op);
2360
2361   return (mode == GET_MODE (op)
2362           && GET_CODE (op) == MEM
2363           && offsettable_address_p (reload_completed | reload_in_progress,
2364                                     mode, XEXP (op, 0)));
2365 }
2366
2367 /* Return TRUE if the operand is a memory reference which is, or can be
2368    made word aligned by adjusting the offset.  */
2369 int
2370 alignable_memory_operand (op, mode)
2371      register rtx op;
2372      enum machine_mode mode;
2373 {
2374   rtx reg;
2375
2376   if (mode == VOIDmode)
2377     mode = GET_MODE (op);
2378
2379   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
2380     return 0;
2381
2382   op = XEXP (op, 0);
2383
2384   return ((GET_CODE (reg = op) == REG
2385            || (GET_CODE (op) == SUBREG
2386                && GET_CODE (reg = SUBREG_REG (op)) == REG)
2387            || (GET_CODE (op) == PLUS
2388                && GET_CODE (XEXP (op, 1)) == CONST_INT
2389                && (GET_CODE (reg = XEXP (op, 0)) == REG
2390                    || (GET_CODE (XEXP (op, 0)) == SUBREG
2391                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
2392           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 4);
2393 }
2394
2395 /* Similar to s_register_operand, but does not allow hard integer 
2396    registers.  */
2397 int
2398 f_register_operand (op, mode)
2399      register rtx op;
2400      enum machine_mode mode;
2401 {
2402   if (GET_MODE (op) != mode && mode != VOIDmode)
2403     return 0;
2404
2405   if (GET_CODE (op) == SUBREG)
2406     op = SUBREG_REG (op);
2407
2408   /* We don't consider registers whose class is NO_REGS
2409      to be a register operand.  */
2410   return (GET_CODE (op) == REG
2411           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2412               || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
2413 }
2414
2415 /* Return TRUE for valid operands for the rhs of an FPU instruction.  */
2416
2417 int
2418 fpu_rhs_operand (op, mode)
2419      rtx op;
2420      enum machine_mode mode;
2421 {
2422   if (s_register_operand (op, mode))
2423     return TRUE;
2424
2425   if (GET_MODE (op) != mode && mode != VOIDmode)
2426     return FALSE;
2427
2428   if (GET_CODE (op) == CONST_DOUBLE)
2429     return const_double_rtx_ok_for_fpu (op);
2430
2431   return FALSE;
2432 }
2433
2434 int
2435 fpu_add_operand (op, mode)
2436      rtx op;
2437      enum machine_mode mode;
2438 {
2439   if (s_register_operand (op, mode))
2440     return TRUE;
2441
2442   if (GET_MODE (op) != mode && mode != VOIDmode)
2443     return FALSE;
2444
2445   if (GET_CODE (op) == CONST_DOUBLE)
2446     return (const_double_rtx_ok_for_fpu (op) 
2447             || neg_const_double_rtx_ok_for_fpu (op));
2448
2449   return FALSE;
2450 }
2451
2452 /* Return nonzero if OP is a constant power of two.  */
2453
2454 int
2455 power_of_two_operand (op, mode)
2456      rtx op;
2457      enum machine_mode mode ATTRIBUTE_UNUSED;
2458 {
2459   if (GET_CODE (op) == CONST_INT)
2460     {
2461       HOST_WIDE_INT value = INTVAL(op);
2462       return value != 0  &&  (value & (value - 1)) == 0;
2463     }
2464   return FALSE;
2465 }
2466
2467 /* Return TRUE for a valid operand of a DImode operation.
2468    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2469    Note that this disallows MEM(REG+REG), but allows
2470    MEM(PRE/POST_INC/DEC(REG)).  */
2471
2472 int
2473 di_operand (op, mode)
2474      rtx op;
2475      enum machine_mode mode;
2476 {
2477   if (s_register_operand (op, mode))
2478     return TRUE;
2479
2480   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
2481     return FALSE;
2482
2483   if (GET_CODE (op) == SUBREG)
2484     op = SUBREG_REG (op);
2485
2486   switch (GET_CODE (op))
2487     {
2488     case CONST_DOUBLE:
2489     case CONST_INT:
2490       return TRUE;
2491
2492     case MEM:
2493       return memory_address_p (DImode, XEXP (op, 0));
2494
2495     default:
2496       return FALSE;
2497     }
2498 }
2499
2500 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
2501    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2502    Note that this disallows MEM(REG+REG), but allows
2503    MEM(PRE/POST_INC/DEC(REG)).  */
2504
2505 int
2506 soft_df_operand (op, mode)
2507      rtx op;
2508      enum machine_mode mode;
2509 {
2510   if (s_register_operand (op, mode))
2511     return TRUE;
2512
2513   if (mode != VOIDmode && GET_MODE (op) != mode)
2514     return FALSE;
2515
2516   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
2517     return FALSE;
2518   
2519   if (GET_CODE (op) == SUBREG)
2520     op = SUBREG_REG (op);
2521   
2522   switch (GET_CODE (op))
2523     {
2524     case CONST_DOUBLE:
2525       return TRUE;
2526
2527     case MEM:
2528       return memory_address_p (DFmode, XEXP (op, 0));
2529
2530     default:
2531       return FALSE;
2532     }
2533 }
2534
2535 /* Return TRUE for valid index operands. */
2536
2537 int
2538 index_operand (op, mode)
2539      rtx op;
2540      enum machine_mode mode;
2541 {
2542   return (s_register_operand(op, mode)
2543           || (immediate_operand (op, mode)
2544               && INTVAL (op) < 4096 && INTVAL (op) > -4096));
2545 }
2546
2547 /* Return TRUE for valid shifts by a constant. This also accepts any
2548    power of two on the (somewhat overly relaxed) assumption that the
2549    shift operator in this case was a mult. */
2550
2551 int
2552 const_shift_operand (op, mode)
2553      rtx op;
2554      enum machine_mode mode;
2555 {
2556   return (power_of_two_operand (op, mode)
2557           || (immediate_operand (op, mode)
2558               && (INTVAL (op) < 32 && INTVAL (op) > 0)));
2559 }
2560
2561 /* Return TRUE for arithmetic operators which can be combined with a multiply
2562    (shift).  */
2563
2564 int
2565 shiftable_operator (x, mode)
2566      rtx x;
2567      enum machine_mode mode;
2568 {
2569   if (GET_MODE (x) != mode)
2570     return FALSE;
2571   else
2572     {
2573       enum rtx_code code = GET_CODE (x);
2574
2575       return (code == PLUS || code == MINUS
2576               || code == IOR || code == XOR || code == AND);
2577     }
2578 }
2579
2580 /* Return TRUE for binary logical operators.  */
2581
2582 int
2583 logical_binary_operator (x, mode)
2584      rtx x;
2585      enum machine_mode mode;
2586 {
2587   if (GET_MODE (x) != mode)
2588     return FALSE;
2589   else
2590     {
2591       enum rtx_code code = GET_CODE (x);
2592
2593       return (code == IOR || code == XOR || code == AND);
2594     }
2595 }
2596
2597 /* Return TRUE for shift operators. */
2598
2599 int
2600 shift_operator (x, mode)
2601      rtx x;
2602      enum machine_mode mode;
2603 {
2604   if (GET_MODE (x) != mode)
2605     return FALSE;
2606   else
2607     {
2608       enum rtx_code code = GET_CODE (x);
2609
2610       if (code == MULT)
2611         return power_of_two_operand (XEXP (x, 1), mode);
2612
2613       return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
2614               || code == ROTATERT);
2615     }
2616 }
2617
2618 int equality_operator (x, mode)
2619      rtx x;
2620      enum machine_mode mode ATTRIBUTE_UNUSED;
2621 {
2622   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
2623 }
2624
2625 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
2626
2627 int
2628 minmax_operator (x, mode)
2629      rtx x;
2630      enum machine_mode mode;
2631 {
2632   enum rtx_code code = GET_CODE (x);
2633
2634   if (GET_MODE (x) != mode)
2635     return FALSE;
2636
2637   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
2638 }
2639
2640 /* return TRUE if x is EQ or NE */
2641
2642 /* Return TRUE if this is the condition code register, if we aren't given
2643    a mode, accept any class CCmode register */
2644
2645 int
2646 cc_register (x, mode)
2647      rtx x;
2648      enum machine_mode mode;
2649 {
2650   if (mode == VOIDmode)
2651     {
2652       mode = GET_MODE (x);
2653       if (GET_MODE_CLASS (mode) != MODE_CC)
2654         return FALSE;
2655     }
2656
2657   if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2658     return TRUE;
2659
2660   return FALSE;
2661 }
2662
2663 /* Return TRUE if this is the condition code register, if we aren't given
2664    a mode, accept any class CCmode register which indicates a dominance
2665    expression.  */
2666
2667 int
2668 dominant_cc_register (x, mode)
2669      rtx x;
2670      enum machine_mode mode;
2671 {
2672   if (mode == VOIDmode)
2673     {
2674       mode = GET_MODE (x);
2675       if (GET_MODE_CLASS (mode) != MODE_CC)
2676         return FALSE;
2677     }
2678
2679   if (mode != CC_DNEmode && mode != CC_DEQmode
2680       && mode != CC_DLEmode && mode != CC_DLTmode
2681       && mode != CC_DGEmode && mode != CC_DGTmode
2682       && mode != CC_DLEUmode && mode != CC_DLTUmode
2683       && mode != CC_DGEUmode && mode != CC_DGTUmode)
2684     return FALSE;
2685
2686   if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2687     return TRUE;
2688
2689   return FALSE;
2690 }
2691
2692 /* Return TRUE if X references a SYMBOL_REF.  */
2693 int
2694 symbol_mentioned_p (x)
2695      rtx x;
2696 {
2697   register const char * fmt;
2698   register int i;
2699
2700   if (GET_CODE (x) == SYMBOL_REF)
2701     return 1;
2702
2703   fmt = GET_RTX_FORMAT (GET_CODE (x));
2704   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2705     {
2706       if (fmt[i] == 'E')
2707         {
2708           register int j;
2709
2710           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2711             if (symbol_mentioned_p (XVECEXP (x, i, j)))
2712               return 1;
2713         }
2714       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
2715         return 1;
2716     }
2717
2718   return 0;
2719 }
2720
2721 /* Return TRUE if X references a LABEL_REF.  */
2722 int
2723 label_mentioned_p (x)
2724      rtx x;
2725 {
2726   register const char * fmt;
2727   register int i;
2728
2729   if (GET_CODE (x) == LABEL_REF)
2730     return 1;
2731
2732   fmt = GET_RTX_FORMAT (GET_CODE (x));
2733   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2734     {
2735       if (fmt[i] == 'E')
2736         {
2737           register int j;
2738
2739           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2740             if (label_mentioned_p (XVECEXP (x, i, j)))
2741               return 1;
2742         }
2743       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
2744         return 1;
2745     }
2746
2747   return 0;
2748 }
2749
2750 enum rtx_code
2751 minmax_code (x)
2752      rtx x;
2753 {
2754   enum rtx_code code = GET_CODE (x);
2755
2756   if (code == SMAX)
2757     return GE;
2758   else if (code == SMIN)
2759     return LE;
2760   else if (code == UMIN)
2761     return LEU;
2762   else if (code == UMAX)
2763     return GEU;
2764
2765   abort ();
2766 }
2767
2768 /* Return 1 if memory locations are adjacent */
2769
2770 int
2771 adjacent_mem_locations (a, b)
2772      rtx a, b;
2773 {
2774   int val0 = 0, val1 = 0;
2775   int reg0, reg1;
2776   
2777   if ((GET_CODE (XEXP (a, 0)) == REG
2778        || (GET_CODE (XEXP (a, 0)) == PLUS
2779            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
2780       && (GET_CODE (XEXP (b, 0)) == REG
2781           || (GET_CODE (XEXP (b, 0)) == PLUS
2782               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
2783     {
2784       if (GET_CODE (XEXP (a, 0)) == PLUS)
2785         {
2786           reg0 = REGNO (XEXP (XEXP (a, 0), 0));
2787           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
2788         }
2789       else
2790         reg0 = REGNO (XEXP (a, 0));
2791       if (GET_CODE (XEXP (b, 0)) == PLUS)
2792         {
2793           reg1 = REGNO (XEXP (XEXP (b, 0), 0));
2794           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
2795         }
2796       else
2797         reg1 = REGNO (XEXP (b, 0));
2798       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
2799     }
2800   return 0;
2801 }
2802
2803 /* Return 1 if OP is a load multiple operation.  It is known to be
2804    parallel and the first section will be tested. */
2805
2806 int
2807 load_multiple_operation (op, mode)
2808      rtx op;
2809      enum machine_mode mode ATTRIBUTE_UNUSED;
2810 {
2811   HOST_WIDE_INT count = XVECLEN (op, 0);
2812   int dest_regno;
2813   rtx src_addr;
2814   HOST_WIDE_INT i = 1, base = 0;
2815   rtx elt;
2816
2817   if (count <= 1
2818       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2819     return 0;
2820
2821   /* Check to see if this might be a write-back */
2822   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2823     {
2824       i++;
2825       base = 1;
2826
2827       /* Now check it more carefully */
2828       if (GET_CODE (SET_DEST (elt)) != REG
2829           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
2830           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
2831           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
2832           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
2833           || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
2834           || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
2835           || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
2836               != REGNO (SET_DEST (elt)))
2837         return 0;
2838
2839       count--;
2840     }
2841
2842   /* Perform a quick check so we don't blow up below.  */
2843   if (count <= i
2844       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
2845       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
2846       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
2847     return 0;
2848
2849   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
2850   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
2851
2852   for (; i < count; i++)
2853     {
2854       elt = XVECEXP (op, 0, i);
2855
2856       if (GET_CODE (elt) != SET
2857           || GET_CODE (SET_DEST (elt)) != REG
2858           || GET_MODE (SET_DEST (elt)) != SImode
2859           || REGNO (SET_DEST (elt)) != dest_regno + i - base
2860           || GET_CODE (SET_SRC (elt)) != MEM
2861           || GET_MODE (SET_SRC (elt)) != SImode
2862           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
2863           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
2864           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
2865           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
2866         return 0;
2867     }
2868
2869   return 1;
2870 }
2871
2872 /* Return 1 if OP is a store multiple operation.  It is known to be
2873    parallel and the first section will be tested. */
2874
2875 int
2876 store_multiple_operation (op, mode)
2877      rtx op;
2878      enum machine_mode mode ATTRIBUTE_UNUSED;
2879 {
2880   HOST_WIDE_INT count = XVECLEN (op, 0);
2881   int src_regno;
2882   rtx dest_addr;
2883   HOST_WIDE_INT i = 1, base = 0;
2884   rtx elt;
2885
2886   if (count <= 1
2887       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2888     return 0;
2889
2890   /* Check to see if this might be a write-back */
2891   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2892     {
2893       i++;
2894       base = 1;
2895
2896       /* Now check it more carefully */
2897       if (GET_CODE (SET_DEST (elt)) != REG
2898           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
2899           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
2900           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
2901           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
2902           || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
2903           || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
2904           || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
2905               != REGNO (SET_DEST (elt)))
2906         return 0;
2907
2908       count--;
2909     }
2910
2911   /* Perform a quick check so we don't blow up below.  */
2912   if (count <= i
2913       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
2914       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
2915       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
2916     return 0;
2917
2918   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
2919   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
2920
2921   for (; i < count; i++)
2922     {
2923       elt = XVECEXP (op, 0, i);
2924
2925       if (GET_CODE (elt) != SET
2926           || GET_CODE (SET_SRC (elt)) != REG
2927           || GET_MODE (SET_SRC (elt)) != SImode
2928           || REGNO (SET_SRC (elt)) != src_regno + i - base
2929           || GET_CODE (SET_DEST (elt)) != MEM
2930           || GET_MODE (SET_DEST (elt)) != SImode
2931           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
2932           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
2933           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
2934           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
2935         return 0;
2936     }
2937
2938   return 1;
2939 }
2940
2941 int
2942 load_multiple_sequence (operands, nops, regs, base, load_offset)
2943      rtx * operands;
2944      int nops;
2945      int * regs;
2946      int * base;
2947      HOST_WIDE_INT * load_offset;
2948 {
2949   int unsorted_regs[4];
2950   HOST_WIDE_INT unsorted_offsets[4];
2951   int order[4];
2952   int base_reg = -1;
2953   int i;
2954
2955   /* Can only handle 2, 3, or 4 insns at present, though could be easily
2956      extended if required.  */
2957   if (nops < 2 || nops > 4)
2958     abort ();
2959
2960   /* Loop over the operands and check that the memory references are
2961      suitable (ie immediate offsets from the same base register).  At
2962      the same time, extract the target register, and the memory
2963      offsets.  */
2964   for (i = 0; i < nops; i++)
2965     {
2966       rtx reg;
2967       rtx offset;
2968
2969       /* Convert a subreg of a mem into the mem itself.  */
2970       if (GET_CODE (operands[nops + i]) == SUBREG)
2971         operands[nops + i] = alter_subreg(operands[nops + i]);
2972
2973       if (GET_CODE (operands[nops + i]) != MEM)
2974         abort ();
2975
2976       /* Don't reorder volatile memory references; it doesn't seem worth
2977          looking for the case where the order is ok anyway.  */
2978       if (MEM_VOLATILE_P (operands[nops + i]))
2979         return 0;
2980
2981       offset = const0_rtx;
2982
2983       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
2984            || (GET_CODE (reg) == SUBREG
2985                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2986           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
2987               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
2988                    == REG)
2989                   || (GET_CODE (reg) == SUBREG
2990                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
2991               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
2992                   == CONST_INT)))
2993         {
2994           if (i == 0)
2995             {
2996               base_reg = REGNO(reg);
2997               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
2998                                   ? REGNO (operands[i])
2999                                   : REGNO (SUBREG_REG (operands[i])));
3000               order[0] = 0;
3001             }
3002           else 
3003             {
3004               if (base_reg != REGNO (reg))
3005                 /* Not addressed from the same base register.  */
3006                 return 0;
3007
3008               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3009                                   ? REGNO (operands[i])
3010                                   : REGNO (SUBREG_REG (operands[i])));
3011               if (unsorted_regs[i] < unsorted_regs[order[0]])
3012                 order[0] = i;
3013             }
3014
3015           /* If it isn't an integer register, or if it overwrites the
3016              base register but isn't the last insn in the list, then
3017              we can't do this.  */
3018           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
3019               || (i != nops - 1 && unsorted_regs[i] == base_reg))
3020             return 0;
3021
3022           unsorted_offsets[i] = INTVAL (offset);
3023         }
3024       else
3025         /* Not a suitable memory address.  */
3026         return 0;
3027     }
3028
3029   /* All the useful information has now been extracted from the
3030      operands into unsorted_regs and unsorted_offsets; additionally,
3031      order[0] has been set to the lowest numbered register in the
3032      list.  Sort the registers into order, and check that the memory
3033      offsets are ascending and adjacent.  */
3034
3035   for (i = 1; i < nops; i++)
3036     {
3037       int j;
3038
3039       order[i] = order[i - 1];
3040       for (j = 0; j < nops; j++)
3041         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3042             && (order[i] == order[i - 1]
3043                 || unsorted_regs[j] < unsorted_regs[order[i]]))
3044           order[i] = j;
3045
3046       /* Have we found a suitable register? if not, one must be used more
3047          than once.  */
3048       if (order[i] == order[i - 1])
3049         return 0;
3050
3051       /* Is the memory address adjacent and ascending? */
3052       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3053         return 0;
3054     }
3055
3056   if (base)
3057     {
3058       *base = base_reg;
3059
3060       for (i = 0; i < nops; i++)
3061         regs[i] = unsorted_regs[order[i]];
3062
3063       *load_offset = unsorted_offsets[order[0]];
3064     }
3065
3066   if (unsorted_offsets[order[0]] == 0)
3067     return 1; /* ldmia */
3068
3069   if (unsorted_offsets[order[0]] == 4)
3070     return 2; /* ldmib */
3071
3072   if (unsorted_offsets[order[nops - 1]] == 0)
3073     return 3; /* ldmda */
3074
3075   if (unsorted_offsets[order[nops - 1]] == -4)
3076     return 4; /* ldmdb */
3077
3078   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
3079      if the offset isn't small enough.  The reason 2 ldrs are faster
3080      is because these ARMs are able to do more than one cache access
3081      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
3082      whilst the ARM8 has a double bandwidth cache.  This means that
3083      these cores can do both an instruction fetch and a data fetch in
3084      a single cycle, so the trick of calculating the address into a
3085      scratch register (one of the result regs) and then doing a load
3086      multiple actually becomes slower (and no smaller in code size).
3087      That is the transformation
3088  
3089         ldr     rd1, [rbase + offset]
3090         ldr     rd2, [rbase + offset + 4]
3091  
3092      to
3093  
3094         add     rd1, rbase, offset
3095         ldmia   rd1, {rd1, rd2}
3096  
3097      produces worse code -- '3 cycles + any stalls on rd2' instead of
3098      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
3099      access per cycle, the first sequence could never complete in less
3100      than 6 cycles, whereas the ldm sequence would only take 5 and
3101      would make better use of sequential accesses if not hitting the
3102      cache.
3103
3104      We cheat here and test 'arm_ld_sched' which we currently know to
3105      only be true for the ARM8, ARM9 and StrongARM.  If this ever
3106      changes, then the test below needs to be reworked.  */
3107   if (nops == 2 && arm_ld_sched)
3108     return 0;
3109
3110   /* Can't do it without setting up the offset, only do this if it takes
3111      no more than one insn.  */
3112   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
3113           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
3114 }
3115
3116 char *
3117 emit_ldm_seq (operands, nops)
3118      rtx * operands;
3119      int nops;
3120 {
3121   int regs[4];
3122   int base_reg;
3123   HOST_WIDE_INT offset;
3124   char buf[100];
3125   int i;
3126
3127   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3128     {
3129     case 1:
3130       strcpy (buf, "ldm%?ia\t");
3131       break;
3132
3133     case 2:
3134       strcpy (buf, "ldm%?ib\t");
3135       break;
3136
3137     case 3:
3138       strcpy (buf, "ldm%?da\t");
3139       break;
3140
3141     case 4:
3142       strcpy (buf, "ldm%?db\t");
3143       break;
3144
3145     case 5:
3146       if (offset >= 0)
3147         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3148                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3149                  (long) offset);
3150       else
3151         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3152                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3153                  (long) -offset);
3154       output_asm_insn (buf, operands);
3155       base_reg = regs[0];
3156       strcpy (buf, "ldm%?ia\t");
3157       break;
3158
3159     default:
3160       abort ();
3161     }
3162
3163   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
3164            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3165
3166   for (i = 1; i < nops; i++)
3167     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3168              reg_names[regs[i]]);
3169
3170   strcat (buf, "}\t%@ phole ldm");
3171
3172   output_asm_insn (buf, operands);
3173   return "";
3174 }
3175
3176 int
3177 store_multiple_sequence (operands, nops, regs, base, load_offset)
3178      rtx * operands;
3179      int nops;
3180      int * regs;
3181      int * base;
3182      HOST_WIDE_INT * load_offset;
3183 {
3184   int unsorted_regs[4];
3185   HOST_WIDE_INT unsorted_offsets[4];
3186   int order[4];
3187   int base_reg = -1;
3188   int i;
3189
3190   /* Can only handle 2, 3, or 4 insns at present, though could be easily
3191      extended if required.  */
3192   if (nops < 2 || nops > 4)
3193     abort ();
3194
3195   /* Loop over the operands and check that the memory references are
3196      suitable (ie immediate offsets from the same base register).  At
3197      the same time, extract the target register, and the memory
3198      offsets.  */
3199   for (i = 0; i < nops; i++)
3200     {
3201       rtx reg;
3202       rtx offset;
3203
3204       /* Convert a subreg of a mem into the mem itself.  */
3205       if (GET_CODE (operands[nops + i]) == SUBREG)
3206         operands[nops + i] = alter_subreg(operands[nops + i]);
3207
3208       if (GET_CODE (operands[nops + i]) != MEM)
3209         abort ();
3210
3211       /* Don't reorder volatile memory references; it doesn't seem worth
3212          looking for the case where the order is ok anyway.  */
3213       if (MEM_VOLATILE_P (operands[nops + i]))
3214         return 0;
3215
3216       offset = const0_rtx;
3217
3218       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3219            || (GET_CODE (reg) == SUBREG
3220                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3221           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3222               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3223                    == REG)
3224                   || (GET_CODE (reg) == SUBREG
3225                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3226               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3227                   == CONST_INT)))
3228         {
3229           if (i == 0)
3230             {
3231               base_reg = REGNO (reg);
3232               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3233                                   ? REGNO (operands[i])
3234                                   : REGNO (SUBREG_REG (operands[i])));
3235               order[0] = 0;
3236             }
3237           else 
3238             {
3239               if (base_reg != REGNO (reg))
3240                 /* Not addressed from the same base register.  */
3241                 return 0;
3242
3243               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3244                                   ? REGNO (operands[i])
3245                                   : REGNO (SUBREG_REG (operands[i])));
3246               if (unsorted_regs[i] < unsorted_regs[order[0]])
3247                 order[0] = i;
3248             }
3249
3250           /* If it isn't an integer register, then we can't do this.  */
3251           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
3252             return 0;
3253
3254           unsorted_offsets[i] = INTVAL (offset);
3255         }
3256       else
3257         /* Not a suitable memory address.  */
3258         return 0;
3259     }
3260
3261   /* All the useful information has now been extracted from the
3262      operands into unsorted_regs and unsorted_offsets; additionally,
3263      order[0] has been set to the lowest numbered register in the
3264      list.  Sort the registers into order, and check that the memory
3265      offsets are ascending and adjacent.  */
3266
3267   for (i = 1; i < nops; i++)
3268     {
3269       int j;
3270
3271       order[i] = order[i - 1];
3272       for (j = 0; j < nops; j++)
3273         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3274             && (order[i] == order[i - 1]
3275                 || unsorted_regs[j] < unsorted_regs[order[i]]))
3276           order[i] = j;
3277
3278       /* Have we found a suitable register? if not, one must be used more
3279          than once.  */
3280       if (order[i] == order[i - 1])
3281         return 0;
3282
3283       /* Is the memory address adjacent and ascending? */
3284       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3285         return 0;
3286     }
3287
3288   if (base)
3289     {
3290       *base = base_reg;
3291
3292       for (i = 0; i < nops; i++)
3293         regs[i] = unsorted_regs[order[i]];
3294
3295       *load_offset = unsorted_offsets[order[0]];
3296     }
3297
3298   if (unsorted_offsets[order[0]] == 0)
3299     return 1; /* stmia */
3300
3301   if (unsorted_offsets[order[0]] == 4)
3302     return 2; /* stmib */
3303
3304   if (unsorted_offsets[order[nops - 1]] == 0)
3305     return 3; /* stmda */
3306
3307   if (unsorted_offsets[order[nops - 1]] == -4)
3308     return 4; /* stmdb */
3309
3310   return 0;
3311 }
3312
3313 char *
3314 emit_stm_seq (operands, nops)
3315      rtx * operands;
3316      int nops;
3317 {
3318   int regs[4];
3319   int base_reg;
3320   HOST_WIDE_INT offset;
3321   char buf[100];
3322   int i;
3323
3324   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3325     {
3326     case 1:
3327       strcpy (buf, "stm%?ia\t");
3328       break;
3329
3330     case 2:
3331       strcpy (buf, "stm%?ib\t");
3332       break;
3333
3334     case 3:
3335       strcpy (buf, "stm%?da\t");
3336       break;
3337
3338     case 4:
3339       strcpy (buf, "stm%?db\t");
3340       break;
3341
3342     default:
3343       abort ();
3344     }
3345
3346   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
3347            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3348
3349   for (i = 1; i < nops; i++)
3350     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3351              reg_names[regs[i]]);
3352
3353   strcat (buf, "}\t%@ phole stm");
3354
3355   output_asm_insn (buf, operands);
3356   return "";
3357 }
3358
3359 int
3360 multi_register_push (op, mode)
3361      rtx op;
3362      enum machine_mode mode ATTRIBUTE_UNUSED;
3363 {
3364   if (GET_CODE (op) != PARALLEL
3365       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
3366       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
3367       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != 2))
3368     return 0;
3369
3370   return 1;
3371 }
3372
3373 \f
3374 /* Routines for use with attributes */
3375
3376 /* Return nonzero if ATTR is a valid attribute for DECL.
3377    ATTRIBUTES are any existing attributes and ARGS are the arguments
3378    supplied with ATTR.
3379
3380    Supported attributes:
3381
3382    naked: don't output any prologue or epilogue code, the user is assumed
3383    to do the right thing.  */
3384
3385 int
3386 arm_valid_machine_decl_attribute (decl, attr, args)
3387      tree decl;
3388      tree attr;
3389      tree args;
3390 {
3391   if (args != NULL_TREE)
3392     return 0;
3393
3394   if (is_attribute_p ("naked", attr))
3395     return TREE_CODE (decl) == FUNCTION_DECL;
3396   return 0;
3397 }
3398
3399 /* Return non-zero if FUNC is a naked function.  */
3400
3401 static int
3402 arm_naked_function_p (func)
3403      tree func;
3404 {
3405   tree a;
3406
3407   if (TREE_CODE (func) != FUNCTION_DECL)
3408     abort ();
3409   
3410   a = lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (func));
3411   return a != NULL_TREE;
3412 }
3413 \f
3414 /* Routines for use in generating RTL */
3415
3416 rtx
3417 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
3418                        in_struct_p, scalar_p)
3419      int base_regno;
3420      int count;
3421      rtx from;
3422      int up;
3423      int write_back;
3424      int unchanging_p;
3425      int in_struct_p;
3426      int scalar_p;
3427 {
3428   int i = 0, j;
3429   rtx result;
3430   int sign = up ? 1 : -1;
3431   rtx mem;
3432
3433   result = gen_rtx_PARALLEL (VOIDmode,
3434                              rtvec_alloc (count + (write_back ? 2 : 0)));
3435   if (write_back)
3436     {
3437       XVECEXP (result, 0, 0)
3438         = gen_rtx_SET (GET_MODE (from), from,
3439                        plus_constant (from, count * 4 * sign));
3440       i = 1;
3441       count++;
3442     }
3443
3444   for (j = 0; i < count; i++, j++)
3445     {
3446       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
3447       RTX_UNCHANGING_P (mem) = unchanging_p;
3448       MEM_IN_STRUCT_P (mem) = in_struct_p;
3449       MEM_SCALAR_P (mem) = scalar_p;
3450       XVECEXP (result, 0, i)
3451         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
3452     }
3453
3454   if (write_back)
3455     XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, from);
3456
3457   return result;
3458 }
3459
3460 rtx
3461 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
3462                         in_struct_p, scalar_p)
3463      int base_regno;
3464      int count;
3465      rtx to;
3466      int up;
3467      int write_back;
3468      int unchanging_p;
3469      int in_struct_p;
3470      int scalar_p;
3471 {
3472   int i = 0, j;
3473   rtx result;
3474   int sign = up ? 1 : -1;
3475   rtx mem;
3476
3477   result = gen_rtx_PARALLEL (VOIDmode,
3478                              rtvec_alloc (count + (write_back ? 2 : 0)));
3479   if (write_back)
3480     {
3481       XVECEXP (result, 0, 0)
3482         = gen_rtx_SET (GET_MODE (to), to,
3483                        plus_constant (to, count * 4 * sign));
3484       i = 1;
3485       count++;
3486     }
3487
3488   for (j = 0; i < count; i++, j++)
3489     {
3490       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
3491       RTX_UNCHANGING_P (mem) = unchanging_p;
3492       MEM_IN_STRUCT_P (mem) = in_struct_p;
3493       MEM_SCALAR_P (mem) = scalar_p;
3494
3495       XVECEXP (result, 0, i)
3496         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
3497     }
3498
3499   if (write_back)
3500     XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, to);
3501
3502   return result;
3503 }
3504
3505 int
3506 arm_gen_movstrqi (operands)
3507      rtx * operands;
3508 {
3509   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
3510   int i;
3511   rtx src, dst;
3512   rtx st_src, st_dst, fin_src, fin_dst;
3513   rtx part_bytes_reg = NULL;
3514   rtx mem;
3515   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
3516   int dst_scalar_p, src_scalar_p;
3517
3518   if (GET_CODE (operands[2]) != CONST_INT
3519       || GET_CODE (operands[3]) != CONST_INT
3520       || INTVAL (operands[2]) > 64
3521       || INTVAL (operands[3]) & 3)
3522     return 0;
3523
3524   st_dst = XEXP (operands[0], 0);
3525   st_src = XEXP (operands[1], 0);
3526
3527   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
3528   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
3529   dst_scalar_p = MEM_SCALAR_P (operands[0]);
3530   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
3531   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
3532   src_scalar_p = MEM_SCALAR_P (operands[1]);
3533
3534   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
3535   fin_src = src = copy_to_mode_reg (SImode, st_src);
3536
3537   in_words_to_go = (INTVAL (operands[2]) + 3) / 4;
3538   out_words_to_go = INTVAL (operands[2]) / 4;
3539   last_bytes = INTVAL (operands[2]) & 3;
3540
3541   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
3542     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
3543
3544   for (i = 0; in_words_to_go >= 2; i+=4)
3545     {
3546       if (in_words_to_go > 4)
3547         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
3548                                           src_unchanging_p,
3549                                           src_in_struct_p,
3550                                           src_scalar_p));
3551       else
3552         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
3553                                           FALSE, src_unchanging_p,
3554                                           src_in_struct_p, src_scalar_p));
3555
3556       if (out_words_to_go)
3557         {
3558           if (out_words_to_go > 4)
3559             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
3560                                                dst_unchanging_p,
3561                                                dst_in_struct_p,
3562                                                dst_scalar_p));
3563           else if (out_words_to_go != 1)
3564             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
3565                                                dst, TRUE, 
3566                                                (last_bytes == 0
3567                                                 ? FALSE : TRUE),
3568                                                dst_unchanging_p,
3569                                                dst_in_struct_p,
3570                                                dst_scalar_p));
3571           else
3572             {
3573               mem = gen_rtx_MEM (SImode, dst);
3574               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3575               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3576               MEM_SCALAR_P (mem) = dst_scalar_p;
3577               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
3578               if (last_bytes != 0)
3579                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
3580             }
3581         }
3582
3583       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
3584       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
3585     }
3586
3587   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
3588   if (out_words_to_go)
3589     {
3590       rtx sreg;
3591       
3592       mem = gen_rtx_MEM (SImode, src);
3593       RTX_UNCHANGING_P (mem) = src_unchanging_p;
3594       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3595       MEM_SCALAR_P (mem) = src_scalar_p;
3596       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
3597       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
3598       
3599       mem = gen_rtx_MEM (SImode, dst);
3600       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3601       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3602       MEM_SCALAR_P (mem) = dst_scalar_p;
3603       emit_move_insn (mem, sreg);
3604       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
3605       in_words_to_go--;
3606       
3607       if (in_words_to_go)       /* Sanity check */
3608         abort ();
3609     }
3610
3611   if (in_words_to_go)
3612     {
3613       if (in_words_to_go < 0)
3614         abort ();
3615
3616       mem = gen_rtx_MEM (SImode, src);
3617       RTX_UNCHANGING_P (mem) = src_unchanging_p;
3618       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3619       MEM_SCALAR_P (mem) = src_scalar_p;
3620       part_bytes_reg = copy_to_mode_reg (SImode, mem);
3621     }
3622
3623   if (BYTES_BIG_ENDIAN && last_bytes)
3624     {
3625       rtx tmp = gen_reg_rtx (SImode);
3626
3627       if (part_bytes_reg == NULL)
3628         abort ();
3629
3630       /* The bytes we want are in the top end of the word */
3631       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
3632                               GEN_INT (8 * (4 - last_bytes))));
3633       part_bytes_reg = tmp;
3634       
3635       while (last_bytes)
3636         {
3637           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
3638           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3639           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3640           MEM_SCALAR_P (mem) = dst_scalar_p;
3641           emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3642           
3643           if (--last_bytes)
3644             {
3645               tmp = gen_reg_rtx (SImode);
3646               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3647               part_bytes_reg = tmp;
3648             }
3649         }
3650           
3651     }
3652   else
3653     {
3654       while (last_bytes)
3655         {
3656           if (part_bytes_reg == NULL)
3657             abort ();
3658
3659           mem = gen_rtx_MEM (QImode, dst);
3660           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3661           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3662           MEM_SCALAR_P (mem) = dst_scalar_p;
3663           emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3664           
3665           if (--last_bytes)
3666             {
3667               rtx tmp = gen_reg_rtx (SImode);
3668
3669               emit_insn (gen_addsi3 (dst, dst, const1_rtx));
3670               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3671               part_bytes_reg = tmp;
3672             }
3673         }
3674     }
3675
3676   return 1;
3677 }
3678
3679 /* Generate a memory reference for a half word, such that it will be loaded
3680    into the top 16 bits of the word.  We can assume that the address is
3681    known to be alignable and of the form reg, or plus (reg, const).  */
3682 rtx
3683 gen_rotated_half_load (memref)
3684      rtx memref;
3685 {
3686   HOST_WIDE_INT offset = 0;
3687   rtx base = XEXP (memref, 0);
3688
3689   if (GET_CODE (base) == PLUS)
3690     {
3691       offset = INTVAL (XEXP (base, 1));
3692       base = XEXP (base, 0);
3693     }
3694
3695   /* If we aren't allowed to generate unaligned addresses, then fail.  */
3696   if (TARGET_MMU_TRAPS
3697       && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
3698     return NULL;
3699
3700   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
3701
3702   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
3703     return base;
3704
3705   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
3706 }
3707
3708 static enum machine_mode
3709 select_dominance_cc_mode (x, y, cond_or)
3710      rtx x;
3711      rtx y;
3712      HOST_WIDE_INT cond_or;
3713 {
3714   enum rtx_code cond1, cond2;
3715   int swapped = 0;
3716
3717   /* Currently we will probably get the wrong result if the individual
3718      comparisons are not simple.  This also ensures that it is safe to
3719      reverse a comparison if necessary.  */
3720   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
3721        != CCmode)
3722       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
3723           != CCmode))
3724     return CCmode;
3725
3726   if (cond_or)
3727     cond1 = reverse_condition (cond1);
3728
3729   /* If the comparisons are not equal, and one doesn't dominate the other,
3730      then we can't do this.  */
3731   if (cond1 != cond2 
3732       && ! comparison_dominates_p (cond1, cond2)
3733       && (swapped = 1, ! comparison_dominates_p (cond2, cond1)))
3734     return CCmode;
3735
3736   if (swapped)
3737     {
3738       enum rtx_code temp = cond1;
3739       cond1 = cond2;
3740       cond2 = temp;
3741     }
3742
3743   switch (cond1)
3744     {
3745     case EQ:
3746       if (cond2 == EQ || ! cond_or)
3747         return CC_DEQmode;
3748
3749       switch (cond2)
3750         {
3751         case LE: return CC_DLEmode;
3752         case LEU: return CC_DLEUmode;
3753         case GE: return CC_DGEmode;
3754         case GEU: return CC_DGEUmode;
3755         default: break;
3756         }
3757
3758       break;
3759
3760     case LT:
3761       if (cond2 == LT || ! cond_or)
3762         return CC_DLTmode;
3763       if (cond2 == LE)
3764         return CC_DLEmode;
3765       if (cond2 == NE)
3766         return CC_DNEmode;
3767       break;
3768
3769     case GT:
3770       if (cond2 == GT || ! cond_or)
3771         return CC_DGTmode;
3772       if (cond2 == GE)
3773         return CC_DGEmode;
3774       if (cond2 == NE)
3775         return CC_DNEmode;
3776       break;
3777       
3778     case LTU:
3779       if (cond2 == LTU || ! cond_or)
3780         return CC_DLTUmode;
3781       if (cond2 == LEU)
3782         return CC_DLEUmode;
3783       if (cond2 == NE)
3784         return CC_DNEmode;
3785       break;
3786
3787     case GTU:
3788       if (cond2 == GTU || ! cond_or)
3789         return CC_DGTUmode;
3790       if (cond2 == GEU)
3791         return CC_DGEUmode;
3792       if (cond2 == NE)
3793         return CC_DNEmode;
3794       break;
3795
3796     /* The remaining cases only occur when both comparisons are the
3797        same.  */
3798     case NE:
3799       return CC_DNEmode;
3800
3801     case LE:
3802       return CC_DLEmode;
3803
3804     case GE:
3805       return CC_DGEmode;
3806
3807     case LEU:
3808       return CC_DLEUmode;
3809
3810     case GEU:
3811       return CC_DGEUmode;
3812
3813     default:
3814       break;
3815     }
3816
3817   abort ();
3818 }
3819
3820 enum machine_mode
3821 arm_select_cc_mode (op, x, y)
3822      enum rtx_code op;
3823      rtx x;
3824      rtx y;
3825 {
3826   /* All floating point compares return CCFP if it is an equality
3827      comparison, and CCFPE otherwise.  */
3828   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3829     return (op == EQ || op == NE) ? CCFPmode : CCFPEmode;
3830   
3831   /* A compare with a shifted operand.  Because of canonicalization, the
3832      comparison will have to be swapped when we emit the assembler.  */
3833   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
3834       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3835           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
3836           || GET_CODE (x) == ROTATERT))
3837     return CC_SWPmode;
3838
3839   /* This is a special case that is used by combine to allow a 
3840      comparison of a shifted byte load to be split into a zero-extend
3841      followed by a comparison of the shifted integer (only valid for
3842      equalities and unsigned inequalities).  */
3843   if (GET_MODE (x) == SImode
3844       && GET_CODE (x) == ASHIFT
3845       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
3846       && GET_CODE (XEXP (x, 0)) == SUBREG
3847       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
3848       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
3849       && (op == EQ || op == NE
3850           || op == GEU || op == GTU || op == LTU || op == LEU)
3851       && GET_CODE (y) == CONST_INT)
3852     return CC_Zmode;
3853
3854   /* An operation that sets the condition codes as a side-effect, the
3855      V flag is not set correctly, so we can only use comparisons where
3856      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
3857      instead.  */
3858   if (GET_MODE (x) == SImode
3859       && y == const0_rtx
3860       && (op == EQ || op == NE || op == LT || op == GE)
3861       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
3862           || GET_CODE (x) == AND || GET_CODE (x) == IOR
3863           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
3864           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
3865           || GET_CODE (x) == LSHIFTRT
3866           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3867           || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
3868     return CC_NOOVmode;
3869
3870   /* A construct for a conditional compare, if the false arm contains
3871      0, then both conditions must be true, otherwise either condition
3872      must be true.  Not all conditions are possible, so CCmode is
3873      returned if it can't be done.  */
3874   if (GET_CODE (x) == IF_THEN_ELSE
3875       && (XEXP (x, 2) == const0_rtx
3876           || XEXP (x, 2) == const1_rtx)
3877       && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3878       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
3879     return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
3880                                      INTVAL (XEXP (x, 2)));
3881
3882   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
3883     return CC_Zmode;
3884
3885   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
3886       && GET_CODE (x) == PLUS
3887       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
3888     return CC_Cmode;
3889
3890   return CCmode;
3891 }
3892
3893 /* X and Y are two things to compare using CODE.  Emit the compare insn and
3894    return the rtx for register 0 in the proper mode.  FP means this is a
3895    floating point compare: I don't think that it is needed on the arm.  */
3896
3897 rtx
3898 gen_compare_reg (code, x, y)
3899      enum rtx_code code;
3900      rtx x, y;
3901 {
3902   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
3903   rtx cc_reg = gen_rtx_REG (mode, 24);
3904
3905   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
3906                           gen_rtx_COMPARE (mode, x, y)));
3907
3908   return cc_reg;
3909 }
3910
3911 void
3912 arm_reload_in_hi (operands)
3913      rtx * operands;
3914 {
3915   rtx ref = operands[1];
3916   rtx base, scratch;
3917   HOST_WIDE_INT offset = 0;
3918
3919   if (GET_CODE (ref) == SUBREG)
3920     {
3921       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
3922       if (BYTES_BIG_ENDIAN)
3923         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
3924                    - MIN (UNITS_PER_WORD,
3925                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
3926       ref = SUBREG_REG (ref);
3927     }
3928
3929   if (GET_CODE (ref) == REG)
3930     {
3931       /* We have a pseudo which has been spilt onto the stack; there
3932          are two cases here: the first where there is a simple
3933          stack-slot replacement and a second where the stack-slot is
3934          out of range, or is used as a subreg.  */
3935       if (reg_equiv_mem[REGNO (ref)])
3936         {
3937           ref = reg_equiv_mem[REGNO (ref)];
3938           base = find_replacement (&XEXP (ref, 0));
3939         }
3940       else
3941         /* The slot is out of range, or was dressed up in a SUBREG */
3942         base = reg_equiv_address[REGNO (ref)];
3943     }
3944   else
3945     base = find_replacement (&XEXP (ref, 0));
3946
3947   /* Handle the case where the address is too complex to be offset by 1.  */
3948   if (GET_CODE (base) == MINUS
3949       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
3950     {
3951       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
3952
3953       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
3954       base = base_plus;
3955     }
3956   else if (GET_CODE (base) == PLUS)
3957     {
3958       /* The addend must be CONST_INT, or we would have dealt with it above */
3959       HOST_WIDE_INT hi, lo;
3960
3961       offset += INTVAL (XEXP (base, 1));
3962       base = XEXP (base, 0);
3963
3964       /* Rework the address into a legal sequence of insns */
3965       /* Valid range for lo is -4095 -> 4095 */
3966       lo = (offset >= 0
3967             ? (offset & 0xfff)
3968             : -((-offset) & 0xfff));
3969
3970       /* Corner case, if lo is the max offset then we would be out of range
3971          once we have added the additional 1 below, so bump the msb into the
3972          pre-loading insn(s).  */
3973       if (lo == 4095)
3974         lo &= 0x7ff;
3975
3976       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xFFFFFFFFUL)
3977              ^ (HOST_WIDE_INT) 0x80000000UL)
3978             - (HOST_WIDE_INT) 0x80000000UL);
3979
3980       if (hi + lo != offset)
3981         abort ();
3982
3983       if (hi != 0)
3984         {
3985           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
3986
3987           /* Get the base address; addsi3 knows how to handle constants
3988              that require more than one insn */
3989           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
3990           base = base_plus;
3991           offset = lo;
3992         }
3993     }
3994
3995   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
3996   emit_insn (gen_zero_extendqisi2 (scratch,
3997                                    gen_rtx_MEM (QImode,
3998                                                 plus_constant (base,
3999                                                                offset))));
4000   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
4001                                    gen_rtx_MEM (QImode, 
4002                                                 plus_constant (base,
4003                                                                offset + 1))));
4004   if (! BYTES_BIG_ENDIAN)
4005     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4006                         gen_rtx_IOR (SImode, 
4007                                      gen_rtx_ASHIFT
4008                                      (SImode,
4009                                       gen_rtx_SUBREG (SImode, operands[0], 0),
4010                                       GEN_INT (8)),
4011                                      scratch)));
4012   else
4013     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4014                             gen_rtx_IOR (SImode, 
4015                                          gen_rtx_ASHIFT (SImode, scratch,
4016                                                          GEN_INT (8)),
4017                                          gen_rtx_SUBREG (SImode, operands[0],
4018                                                          0))));
4019 }
4020
4021 /* Handle storing a half-word to memory during reload by synthesising as two
4022    byte stores.  Take care not to clobber the input values until after we
4023    have moved them somewhere safe.  This code assumes that if the DImode
4024    scratch in operands[2] overlaps either the input value or output address
4025    in some way, then that value must die in this insn (we absolutely need
4026    two scratch registers for some corner cases).  */
4027 void
4028 arm_reload_out_hi (operands)
4029      rtx * operands;
4030 {
4031   rtx ref = operands[0];
4032   rtx outval = operands[1];
4033   rtx base, scratch;
4034   HOST_WIDE_INT offset = 0;
4035
4036   if (GET_CODE (ref) == SUBREG)
4037     {
4038       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
4039       if (BYTES_BIG_ENDIAN)
4040         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
4041                    - MIN (UNITS_PER_WORD,
4042                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
4043       ref = SUBREG_REG (ref);
4044     }
4045
4046
4047   if (GET_CODE (ref) == REG)
4048     {
4049       /* We have a pseudo which has been spilt onto the stack; there
4050          are two cases here: the first where there is a simple
4051          stack-slot replacement and a second where the stack-slot is
4052          out of range, or is used as a subreg.  */
4053       if (reg_equiv_mem[REGNO (ref)])
4054         {
4055           ref = reg_equiv_mem[REGNO (ref)];
4056           base = find_replacement (&XEXP (ref, 0));
4057         }
4058       else
4059         /* The slot is out of range, or was dressed up in a SUBREG */
4060         base = reg_equiv_address[REGNO (ref)];
4061     }
4062   else
4063     base = find_replacement (&XEXP (ref, 0));
4064
4065   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4066
4067   /* Handle the case where the address is too complex to be offset by 1.  */
4068   if (GET_CODE (base) == MINUS
4069       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4070     {
4071       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4072
4073       /* Be careful not to destroy OUTVAL.  */
4074       if (reg_overlap_mentioned_p (base_plus, outval))
4075         {
4076           /* Updating base_plus might destroy outval, see if we can
4077              swap the scratch and base_plus.  */
4078           if (! reg_overlap_mentioned_p (scratch, outval))
4079             {
4080               rtx tmp = scratch;
4081               scratch = base_plus;
4082               base_plus = tmp;
4083             }
4084           else
4085             {
4086               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4087
4088               /* Be conservative and copy OUTVAL into the scratch now,
4089                  this should only be necessary if outval is a subreg
4090                  of something larger than a word.  */
4091               /* XXX Might this clobber base?  I can't see how it can,
4092                  since scratch is known to overlap with OUTVAL, and
4093                  must be wider than a word.  */
4094               emit_insn (gen_movhi (scratch_hi, outval));
4095               outval = scratch_hi;
4096             }
4097         }
4098
4099       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4100       base = base_plus;
4101     }
4102   else if (GET_CODE (base) == PLUS)
4103     {
4104       /* The addend must be CONST_INT, or we would have dealt with it above */
4105       HOST_WIDE_INT hi, lo;
4106
4107       offset += INTVAL (XEXP (base, 1));
4108       base = XEXP (base, 0);
4109
4110       /* Rework the address into a legal sequence of insns */
4111       /* Valid range for lo is -4095 -> 4095 */
4112       lo = (offset >= 0
4113             ? (offset & 0xfff)
4114             : -((-offset) & 0xfff));
4115
4116       /* Corner case, if lo is the max offset then we would be out of range
4117          once we have added the additional 1 below, so bump the msb into the
4118          pre-loading insn(s).  */
4119       if (lo == 4095)
4120         lo &= 0x7ff;
4121
4122       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xFFFFFFFFUL)
4123              ^ (HOST_WIDE_INT) 0x80000000UL)
4124             - (HOST_WIDE_INT) 0x80000000UL);
4125
4126       if (hi + lo != offset)
4127         abort ();
4128
4129       if (hi != 0)
4130         {
4131           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4132
4133           /* Be careful not to destroy OUTVAL.  */
4134           if (reg_overlap_mentioned_p (base_plus, outval))
4135             {
4136               /* Updating base_plus might destroy outval, see if we
4137                  can swap the scratch and base_plus.  */
4138               if (! reg_overlap_mentioned_p (scratch, outval))
4139                 {
4140                   rtx tmp = scratch;
4141                   scratch = base_plus;
4142                   base_plus = tmp;
4143                 }
4144               else
4145                 {
4146                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4147
4148                   /* Be conservative and copy outval into scratch now,
4149                      this should only be necessary if outval is a
4150                      subreg of something larger than a word.  */
4151                   /* XXX Might this clobber base?  I can't see how it
4152                      can, since scratch is known to overlap with
4153                      outval.  */
4154                   emit_insn (gen_movhi (scratch_hi, outval));
4155                   outval = scratch_hi;
4156                 }
4157             }
4158
4159           /* Get the base address; addsi3 knows how to handle constants
4160              that require more than one insn */
4161           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4162           base = base_plus;
4163           offset = lo;
4164         }
4165     }
4166
4167   if (BYTES_BIG_ENDIAN)
4168     {
4169       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
4170                                          plus_constant (base, offset + 1)),
4171                             gen_rtx_SUBREG (QImode, outval, 0)));
4172       emit_insn (gen_lshrsi3 (scratch,
4173                               gen_rtx_SUBREG (SImode, outval, 0),
4174                               GEN_INT (8)));
4175       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4176                             gen_rtx_SUBREG (QImode, scratch, 0)));
4177     }
4178   else
4179     {
4180       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4181                             gen_rtx_SUBREG (QImode, outval, 0)));
4182       emit_insn (gen_lshrsi3 (scratch,
4183                               gen_rtx_SUBREG (SImode, outval, 0),
4184                               GEN_INT (8)));
4185       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
4186                                          plus_constant (base, offset + 1)),
4187                             gen_rtx_SUBREG (QImode, scratch, 0)));
4188     }
4189 }
4190 \f
4191 /* Routines for manipulation of the constant pool.  */
4192
4193 /* Arm instructions cannot load a large constant directly into a
4194    register; they have to come from a pc relative load.  The constant
4195    must therefore be placed in the addressable range of the pc
4196    relative load.  Depending on the precise pc relative load
4197    instruction the range is somewhere between 256 bytes and 4k.  This
4198    means that we often have to dump a constant inside a function, and
4199    generate code to branch around it.
4200
4201    It is important to minimize this, since the branches will slow
4202    things down and make the code larger.
4203
4204    Normally we can hide the table after an existing unconditional
4205    branch so that there is no interruption of the flow, but in the
4206    worst case the code looks like this:
4207
4208         ldr     rn, L1
4209         ...
4210         b       L2
4211         align
4212         L1:     .long value
4213         L2:
4214         ...
4215
4216         ldr     rn, L3
4217         ...
4218         b       L4
4219         align
4220         L3:     .long value
4221         L4:
4222         ...
4223
4224    We fix this by performing a scan after scheduling, which notices
4225    which instructions need to have their operands fetched from the
4226    constant table and builds the table.
4227
4228    The algorithm starts by building a table of all the constants that
4229    need fixing up and all the natural barriers in the function (places
4230    where a constant table can be dropped without breaking the flow).
4231    For each fixup we note how far the pc-relative replacement will be
4232    able to reach and the offset of the instruction into the function.
4233
4234    Having built the table we then group the fixes together to form
4235    tables that are as large as possible (subject to addressing
4236    constraints) and emit each table of constants after the last
4237    barrier that is within range of all the instructions in the group.
4238    If a group does not contain a barrier, then we forcibly create one
4239    by inserting a jump instruction into the flow.  Once the table has
4240    been inserted, the insns are then modified to reference the
4241    relevant entry in the pool.
4242
4243    Possible enhancements to the alogorithm (not implemented) are:
4244
4245    1) ARM instructions (but not thumb) can use negative offsets, so we
4246    could reference back to a previous pool rather than forwards to a
4247    new one.  For large functions this may reduce the number of pools
4248    required.
4249
4250    2) For some processors and object formats, there may be benefit in
4251    aligning the pools to the start of cache lines; this alignment
4252    would need to be taken into account when calculating addressability
4253    of a pool.
4254
4255  */
4256
4257 typedef struct
4258 {
4259   rtx value;                    /* Value in table */
4260   HOST_WIDE_INT next_offset;
4261   enum machine_mode mode;       /* Mode of value */
4262 } minipool_node;
4263
4264 /* The maximum number of constants that can fit into one pool, since
4265    the pc relative range is 0...4092 bytes and constants are at least 4
4266    bytes long.  */
4267
4268 #define MAX_MINIPOOL_SIZE (4092/4)
4269 static minipool_node minipool_vector[MAX_MINIPOOL_SIZE];
4270 static int minipool_size;
4271 static rtx minipool_vector_label;
4272
4273 /* Add a constant to the pool and return its offset within the current
4274    pool.
4275
4276    X is the rtx we want to replace. MODE is its mode.  On return,
4277    ADDRESS_ONLY will be non-zero if we really want the address of such
4278    a constant, not the constant itself.  */
4279 static HOST_WIDE_INT
4280 add_minipool_constant (x, mode)
4281      rtx x;
4282      enum machine_mode mode;
4283 {
4284   int i;
4285   HOST_WIDE_INT offset;
4286   
4287   /* First, see if we've already got it.  */
4288   for (i = 0; i < minipool_size; i++)
4289     {
4290       if (GET_CODE (x) == minipool_vector[i].value->code
4291           && mode == minipool_vector[i].mode)
4292         {
4293           if (GET_CODE (x) == CODE_LABEL)
4294             {
4295               if (XINT (x, 3) != XINT (minipool_vector[i].value, 3))
4296                 continue;
4297             }
4298           if (rtx_equal_p (x, minipool_vector[i].value))
4299             return minipool_vector[i].next_offset - GET_MODE_SIZE (mode);
4300         }
4301     }
4302
4303   /* Need a new one */
4304   minipool_vector[minipool_size].next_offset = GET_MODE_SIZE (mode);
4305   offset = 0;
4306   if (minipool_size == 0)
4307     minipool_vector_label = gen_label_rtx ();
4308   else
4309     minipool_vector[minipool_size].next_offset
4310       += (offset = minipool_vector[minipool_size - 1].next_offset);
4311
4312   minipool_vector[minipool_size].value = x;
4313   minipool_vector[minipool_size].mode = mode;
4314   minipool_size++;
4315   return offset;
4316 }
4317
4318 /* Output the literal table */
4319 static void
4320 dump_minipool (scan)
4321      rtx scan;
4322 {
4323   int i;
4324
4325   scan = emit_label_after (gen_label_rtx (), scan);
4326   scan = emit_insn_after (gen_align_4 (), scan);
4327   scan = emit_label_after (minipool_vector_label, scan);
4328
4329   for (i = 0; i < minipool_size; i++)
4330     {
4331       minipool_node *p = minipool_vector + i;
4332
4333       switch (GET_MODE_SIZE (p->mode))
4334         {
4335         case 4:
4336           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
4337           break;
4338
4339         case 8:
4340           scan = emit_insn_after (gen_consttable_8 (p->value), scan);
4341           break;
4342
4343         default:
4344           abort ();
4345           break;
4346         }
4347     }
4348
4349   scan = emit_insn_after (gen_consttable_end (), scan);
4350   scan = emit_barrier_after (scan);
4351   minipool_size = 0;
4352 }
4353
4354 /* Find the last barrier less than MAX_COUNT bytes from FROM, or
4355    create one.  */
4356 static rtx
4357 find_barrier (from, max_count)
4358      rtx from;
4359      int max_count;
4360 {
4361   int count = 0;
4362   rtx found_barrier = 0;
4363   rtx last = from;
4364
4365   while (from && count < max_count)
4366     {
4367       rtx tmp;
4368       
4369       if (GET_CODE (from) == BARRIER)
4370         found_barrier = from;
4371
4372       /* Count the length of this insn */
4373       if (GET_CODE (from) == JUMP_INSN
4374           && JUMP_LABEL (from) != 0
4375           && ((tmp = next_real_insn (JUMP_LABEL (from)))
4376               == next_real_insn (from))
4377           && tmp != NULL
4378           && GET_CODE (tmp) == JUMP_INSN
4379           && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
4380               || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
4381         {
4382           int elt = GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC ? 1 : 0;
4383           count += (get_attr_length (from)
4384                     + GET_MODE_SIZE (SImode) * XVECLEN (PATTERN (tmp), elt));
4385           /* Continue after the dispatch table.  */
4386           last = from;
4387           from = NEXT_INSN (tmp);
4388           continue;
4389         }
4390       else
4391         count += get_attr_length (from);
4392
4393       last = from;
4394       from = NEXT_INSN (from);
4395     }
4396
4397   if (! found_barrier)
4398     {
4399       /* We didn't find a barrier in time to
4400          dump our stuff, so we'll make one.  */
4401       rtx label = gen_label_rtx ();
4402       
4403       if (from)
4404         from = PREV_INSN (last);
4405       else
4406         from = get_last_insn ();
4407       
4408       /* Walk back to be just before any jump.  */
4409       while (GET_CODE (from) == JUMP_INSN
4410              || GET_CODE (from) == NOTE
4411              || GET_CODE (from) == CODE_LABEL)
4412         from = PREV_INSN (from);
4413       
4414       from = emit_jump_insn_after (gen_jump (label), from);
4415       JUMP_LABEL (from) = label;
4416       found_barrier = emit_barrier_after (from);
4417       emit_label_after (label, found_barrier);
4418     }
4419
4420   return found_barrier;
4421 }
4422
4423 struct minipool_fixup
4424 {
4425   struct minipool_fixup *next;
4426   rtx insn;
4427   int address;
4428   rtx *loc;
4429   enum machine_mode mode;
4430   rtx value;
4431   int range;
4432 };
4433   
4434 struct minipool_fixup *minipool_fix_head;
4435 struct minipool_fixup *minipool_fix_tail;
4436
4437 static void
4438 push_minipool_barrier (insn, address)
4439      rtx insn;
4440      int address;
4441 {
4442   struct minipool_fixup *fix
4443     = (struct minipool_fixup *) oballoc (sizeof (struct minipool_fixup));
4444
4445   fix->insn = insn;
4446   fix->address = address;
4447
4448   fix->next = NULL;
4449   if (minipool_fix_head != NULL)
4450     minipool_fix_tail->next = fix;
4451   else
4452     minipool_fix_head = fix;
4453
4454   minipool_fix_tail = fix;
4455 }
4456
4457 static void
4458 push_minipool_fix (insn, address, loc, mode, value)
4459      rtx insn;
4460      int address;
4461      rtx *loc;
4462      enum machine_mode mode;
4463      rtx value;
4464 {
4465   struct minipool_fixup *fix
4466     = (struct minipool_fixup *) oballoc (sizeof (struct minipool_fixup));
4467
4468 #ifdef AOF_ASSEMBLER
4469   /* PIC symbol refereneces need to be converted into offsets into the
4470      based area.  */
4471   if (flag_pic && GET_MODE == SYMBOL_REF)
4472     value = aof_pic_entry (value);
4473 #endif /* AOF_ASSEMBLER */
4474
4475   fix->insn = insn;
4476   fix->address = address;
4477   fix->loc = loc;
4478   fix->mode = mode;
4479   fix->value = value;
4480   fix->range = get_attr_pool_range (insn);
4481
4482   /* If an insn doesn't have a range defined for it, then it isn't
4483      expecting to be reworked by this code.  Better to abort now than
4484      to generate duff assembly code.  */
4485   if (fix->range == 0)
4486     abort ();
4487
4488   /* Add it to the chain of fixes */
4489   fix->next = NULL;
4490   if (minipool_fix_head != NULL)
4491     minipool_fix_tail->next = fix;
4492   else
4493     minipool_fix_head = fix;
4494
4495   minipool_fix_tail = fix;
4496 }
4497
4498 static void
4499 note_invalid_constants (insn, address)
4500      rtx insn;
4501      int address;
4502 {
4503   int opno;
4504
4505   /* Extract the operands of the insn */
4506   extract_insn(insn);
4507
4508   /* Find the alternative selected */
4509   if (! constrain_operands (1))
4510     fatal_insn_not_found (insn);
4511
4512   /* Preprocess the constraints, to extract some useful information.  */
4513   preprocess_constraints ();
4514
4515   for (opno = 0; opno < recog_data.n_operands; opno++)
4516     {
4517       /* Things we need to fix can only occur in inputs */
4518       if (recog_data.operand_type[opno] != OP_IN)
4519         continue;
4520
4521       /* If this alternative is a memory reference, then any mention
4522          of constants in this alternative is really to fool reload
4523          into allowing us to accept one there.  We need to fix them up
4524          now so that we output the right code.  */
4525       if (recog_op_alt[opno][which_alternative].memory_ok)
4526         {
4527           rtx op = recog_data.operand[opno];
4528
4529           if (CONSTANT_P (op))
4530             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4531                                recog_data.operand_mode[opno], op);
4532 #ifndef AOF_ASSEMBLER
4533           else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == 3)
4534             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4535                                recog_data.operand_mode[opno],
4536                                XVECEXP (op, 0, 0));
4537 #endif
4538           else if (recog_data.operand_mode[opno] == SImode
4539                    && GET_CODE (op) == MEM
4540                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
4541                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
4542             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4543                                recog_data.operand_mode[opno],
4544                                get_pool_constant (XEXP (op, 0)));
4545         }
4546     }
4547 }
4548
4549 void
4550 arm_reorg (first)
4551      rtx first;
4552 {
4553   rtx insn;
4554   int address = 0;
4555   struct minipool_fixup *fix;
4556
4557   minipool_fix_head = minipool_fix_tail = NULL;
4558
4559   /* The first insn must always be a note, or the code below won't
4560      scan it properly.  */
4561   if (GET_CODE (first) != NOTE)
4562     abort ();
4563
4564   /* Scan all the insns and record the operands that will need fixing.  */
4565   for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
4566     {
4567
4568       if (GET_CODE (insn) == BARRIER)
4569         push_minipool_barrier(insn, address);
4570       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
4571                || GET_CODE (insn) == JUMP_INSN)
4572         {
4573           rtx table;
4574
4575           note_invalid_constants (insn, address);
4576           address += get_attr_length (insn);
4577           /* If the insn is a vector jump, add the size of the table
4578              and skip the table.  */
4579           if (GET_CODE (insn) == JUMP_INSN
4580               && JUMP_LABEL (insn) != NULL
4581               && ((table = next_real_insn (JUMP_LABEL (insn)))
4582                   == next_real_insn (insn))
4583               && table != NULL
4584               && GET_CODE (table) == JUMP_INSN
4585               && (GET_CODE (PATTERN (table)) == ADDR_VEC
4586                   || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
4587             {
4588               int elt = GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC ? 1 : 0;
4589
4590               address += GET_MODE_SIZE (SImode) * XVECLEN (PATTERN (table), 
4591                                                            elt);
4592               insn = table;
4593             }
4594         }
4595     }
4596
4597   /* Now scan the fixups and perform the required changes.  */
4598   for (fix = minipool_fix_head; fix; fix = fix->next)
4599     {
4600       struct minipool_fixup *ftmp;
4601       struct minipool_fixup *last_barrier = NULL;
4602       int max_range;
4603       rtx barrier;
4604       struct minipool_fixup *this_fix;
4605       int new_minipool_size = 0;
4606
4607       /* Skip any further barriers before the next fix.  */
4608       while (fix && GET_CODE (fix->insn) == BARRIER)
4609         fix = fix->next;
4610
4611       if (fix == NULL)
4612         break;
4613
4614       ftmp = fix;
4615       max_range = fix->address + fix->range;
4616
4617       /* Find all the other fixes that can live in the same pool.  */
4618       while (ftmp->next && ftmp->next->address < max_range
4619              && (GET_CODE (ftmp->next->insn) == BARRIER
4620                  /* Ensure we can reach the constant inside the pool.  */
4621                  || ftmp->next->range > new_minipool_size))
4622         {
4623           ftmp = ftmp->next;
4624           if (GET_CODE (ftmp->insn) == BARRIER)
4625             last_barrier = ftmp;
4626           else
4627             {
4628               /* Does this fix constrain the range we can search?  */
4629               if (ftmp->address + ftmp->range - new_minipool_size < max_range)
4630                 max_range = ftmp->address + ftmp->range - new_minipool_size;
4631
4632               new_minipool_size += GET_MODE_SIZE (ftmp->mode);
4633             }
4634         }
4635
4636       /* If we found a barrier, drop back to that; any fixes that we could
4637          have reached but come after the barrier will now go in the next
4638          mini-pool.  */
4639       if (last_barrier != NULL)
4640         {
4641           barrier = last_barrier->insn;
4642           ftmp = last_barrier;
4643         }
4644       /* ftmp is last fix that we can fit into this pool and we
4645          failed to find a barrier that we could use.  Insert a new
4646          barrier in the code and arrange to jump around it.  */
4647       else
4648         {
4649           /* Check that there isn't another fix that is in range that
4650              we couldn't fit into this pool because the pool was
4651              already too large: we need to put the pool before such an
4652              instruction.  */
4653           if (ftmp->next && ftmp->next->address < max_range)
4654             max_range = ftmp->address;
4655
4656           barrier = find_barrier (ftmp->insn, max_range - ftmp->address);
4657         }
4658
4659       /* Scan over the fixes we have identified for this pool, fixing them
4660          up and adding the constants to the pool itself.  */
4661       for (this_fix = fix; this_fix && ftmp->next != this_fix;
4662            this_fix = this_fix->next)
4663         if (GET_CODE (this_fix->insn) != BARRIER)
4664           {
4665             int offset = add_minipool_constant (this_fix->value,
4666                                                 this_fix->mode);
4667             rtx addr
4668               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
4669                                                   minipool_vector_label),
4670                                offset);
4671             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
4672           }
4673
4674       dump_minipool (barrier);
4675       fix = ftmp;
4676     }
4677
4678   /* From now on we must synthesize any constants that we can't handle
4679      directly.  This can happen if the RTL gets split during final
4680      instruction generation.  */
4681   after_arm_reorg = 1;
4682 }
4683
4684 \f
4685 /* Routines to output assembly language.  */
4686
4687 /* If the rtx is the correct value then return the string of the number.
4688    In this way we can ensure that valid double constants are generated even
4689    when cross compiling. */
4690 char *
4691 fp_immediate_constant (x)
4692      rtx x;
4693 {
4694   REAL_VALUE_TYPE r;
4695   int i;
4696   
4697   if (!fpa_consts_inited)
4698     init_fpa_table ();
4699   
4700   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4701   for (i = 0; i < 8; i++)
4702     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
4703       return strings_fpa[i];
4704
4705   abort ();
4706 }
4707
4708 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
4709 static char *
4710 fp_const_from_val (r)
4711      REAL_VALUE_TYPE * r;
4712 {
4713   int i;
4714
4715   if (! fpa_consts_inited)
4716     init_fpa_table ();
4717
4718   for (i = 0; i < 8; i++)
4719     if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
4720       return strings_fpa[i];
4721
4722   abort ();
4723 }
4724
4725 /* Output the operands of a LDM/STM instruction to STREAM.
4726    MASK is the ARM register set mask of which only bits 0-15 are important.
4727    INSTR is the possibly suffixed base register.  HAT unequals zero if a hat
4728    must follow the register list.  */
4729
4730 void
4731 print_multi_reg (stream, instr, reg, mask, hat)
4732      FILE * stream;
4733      char * instr;
4734      int reg;
4735      int mask;
4736      int hat;
4737 {
4738   int i;
4739   int not_first = FALSE;
4740
4741   fputc ('\t', stream);
4742   asm_fprintf (stream, instr, reg);
4743   fputs (", {", stream);
4744   
4745   for (i = 0; i < 16; i++)
4746     if (mask & (1 << i))
4747       {
4748         if (not_first)
4749           fprintf (stream, ", ");
4750         
4751         asm_fprintf (stream, "%r", i);
4752         not_first = TRUE;
4753       }
4754
4755   fprintf (stream, "}%s\n", hat ? "^" : "");
4756 }
4757
4758 /* Output a 'call' insn. */
4759
4760 char *
4761 output_call (operands)
4762      rtx * operands;
4763 {
4764   /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
4765
4766   if (REGNO (operands[0]) == LR_REGNUM)
4767     {
4768       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
4769       output_asm_insn ("mov%?\t%0, %|lr", operands);
4770     }
4771   
4772   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4773   
4774   if (TARGET_INTERWORK)
4775     output_asm_insn ("bx%?\t%0", operands);
4776   else
4777     output_asm_insn ("mov%?\t%|pc, %0", operands);
4778   
4779   return "";
4780 }
4781
4782 static int
4783 eliminate_lr2ip (x)
4784      rtx * x;
4785 {
4786   int something_changed = 0;
4787   rtx x0 = * x;
4788   int code = GET_CODE (x0);
4789   register int i, j;
4790   register const char * fmt;
4791   
4792   switch (code)
4793     {
4794     case REG:
4795       if (REGNO (x0) == LR_REGNUM)
4796         {
4797           *x = gen_rtx_REG (SImode, IP_REGNUM);
4798           return 1;
4799         }
4800       return 0;
4801     default:
4802       /* Scan through the sub-elements and change any references there */
4803       fmt = GET_RTX_FORMAT (code);
4804       
4805       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4806         if (fmt[i] == 'e')
4807           something_changed |= eliminate_lr2ip (&XEXP (x0, i));
4808         else if (fmt[i] == 'E')
4809           for (j = 0; j < XVECLEN (x0, i); j++)
4810             something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
4811       
4812       return something_changed;
4813     }
4814 }
4815   
4816 /* Output a 'call' insn that is a reference in memory. */
4817
4818 char *
4819 output_call_mem (operands)
4820      rtx * operands;
4821 {
4822   operands[0] = copy_rtx (operands[0]); /* Be ultra careful */
4823   /* Handle calls using lr by using ip (which may be clobbered in subr anyway).
4824    */
4825   if (eliminate_lr2ip (&operands[0]))
4826     output_asm_insn ("mov%?\t%|ip, %|lr", operands);
4827
4828   if (TARGET_INTERWORK)
4829     {
4830       output_asm_insn ("ldr%?\t%|ip, %0", operands);
4831       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4832       output_asm_insn ("bx%?\t%|ip", operands);
4833     }
4834   else
4835     {
4836       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4837       output_asm_insn ("ldr%?\t%|pc, %0", operands);
4838     }
4839
4840   return "";
4841 }
4842
4843
4844 /* Output a move from arm registers to an fpu registers.
4845    OPERANDS[0] is an fpu register.
4846    OPERANDS[1] is the first registers of an arm register pair.  */
4847
4848 char *
4849 output_mov_long_double_fpu_from_arm (operands)
4850      rtx * operands;
4851 {
4852   int arm_reg0 = REGNO (operands[1]);
4853   rtx ops[3];
4854
4855   if (arm_reg0 == IP_REGNUM)
4856     abort ();
4857
4858   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4859   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4860   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4861   
4862   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
4863   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
4864   
4865   return "";
4866 }
4867
4868 /* Output a move from an fpu register to arm registers.
4869    OPERANDS[0] is the first registers of an arm register pair.
4870    OPERANDS[1] is an fpu register.  */
4871
4872 char *
4873 output_mov_long_double_arm_from_fpu (operands)
4874      rtx * operands;
4875 {
4876   int arm_reg0 = REGNO (operands[0]);
4877   rtx ops[3];
4878
4879   if (arm_reg0 == IP_REGNUM)
4880     abort ();
4881
4882   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4883   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4884   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4885
4886   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
4887   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
4888   return "";
4889 }
4890
4891 /* Output a move from arm registers to arm registers of a long double
4892    OPERANDS[0] is the destination.
4893    OPERANDS[1] is the source.  */
4894 char *
4895 output_mov_long_double_arm_from_arm (operands)
4896      rtx * operands;
4897 {
4898   /* We have to be careful here because the two might overlap */
4899   int dest_start = REGNO (operands[0]);
4900   int src_start = REGNO (operands[1]);
4901   rtx ops[2];
4902   int i;
4903
4904   if (dest_start < src_start)
4905     {
4906       for (i = 0; i < 3; i++)
4907         {
4908           ops[0] = gen_rtx_REG (SImode, dest_start + i);
4909           ops[1] = gen_rtx_REG (SImode, src_start + i);
4910           output_asm_insn ("mov%?\t%0, %1", ops);
4911         }
4912     }
4913   else
4914     {
4915       for (i = 2; i >= 0; i--)
4916         {
4917           ops[0] = gen_rtx_REG (SImode, dest_start + i);
4918           ops[1] = gen_rtx_REG (SImode, src_start + i);
4919           output_asm_insn ("mov%?\t%0, %1", ops);
4920         }
4921     }
4922
4923   return "";
4924 }
4925
4926
4927 /* Output a move from arm registers to an fpu registers.
4928    OPERANDS[0] is an fpu register.
4929    OPERANDS[1] is the first registers of an arm register pair.  */
4930
4931 char *
4932 output_mov_double_fpu_from_arm (operands)
4933      rtx * operands;
4934 {
4935   int arm_reg0 = REGNO (operands[1]);
4936   rtx ops[2];
4937
4938   if (arm_reg0 == IP_REGNUM)
4939     abort ();
4940   
4941   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4942   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4943   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
4944   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
4945   return "";
4946 }
4947
4948 /* Output a move from an fpu register to arm registers.
4949    OPERANDS[0] is the first registers of an arm register pair.
4950    OPERANDS[1] is an fpu register.  */
4951
4952 char *
4953 output_mov_double_arm_from_fpu (operands)
4954      rtx * operands;
4955 {
4956   int arm_reg0 = REGNO (operands[0]);
4957   rtx ops[2];
4958
4959   if (arm_reg0 == IP_REGNUM)
4960     abort ();
4961
4962   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4963   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4964   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
4965   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
4966   return "";
4967 }
4968
4969 /* Output a move between double words.
4970    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
4971    or MEM<-REG and all MEMs must be offsettable addresses.  */
4972
4973 char *
4974 output_move_double (operands)
4975      rtx * operands;
4976 {
4977   enum rtx_code code0 = GET_CODE (operands[0]);
4978   enum rtx_code code1 = GET_CODE (operands[1]);
4979   rtx otherops[3];
4980
4981   if (code0 == REG)
4982     {
4983       int reg0 = REGNO (operands[0]);
4984
4985       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
4986       
4987       if (code1 == REG)
4988         {
4989           int reg1 = REGNO (operands[1]);
4990           if (reg1 == IP_REGNUM)
4991             abort ();
4992
4993           /* Ensure the second source is not overwritten */
4994           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
4995             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
4996           else
4997             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
4998         }
4999       else if (code1 == CONST_DOUBLE)
5000         {
5001           if (GET_MODE (operands[1]) == DFmode)
5002             {
5003               long l[2];
5004               union real_extract u;
5005
5006               bcopy ((char *) &CONST_DOUBLE_LOW (operands[1]), (char *) &u,
5007                      sizeof (u));
5008               REAL_VALUE_TO_TARGET_DOUBLE (u.d, l);
5009               otherops[1] = GEN_INT(l[1]);
5010               operands[1] = GEN_INT(l[0]);
5011             }
5012           else if (GET_MODE (operands[1]) != VOIDmode)
5013             abort ();
5014           else if (WORDS_BIG_ENDIAN)
5015             {
5016               
5017               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5018               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5019             }
5020           else
5021             {
5022               
5023               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5024               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5025             }
5026           
5027           output_mov_immediate (operands);
5028           output_mov_immediate (otherops);
5029         }
5030       else if (code1 == CONST_INT)
5031         {
5032 #if HOST_BITS_PER_WIDE_INT > 32
5033           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
5034              what the upper word is.  */
5035           if (WORDS_BIG_ENDIAN)
5036             {
5037               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5038               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5039             }
5040           else
5041             {
5042               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5043               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5044             }
5045 #else
5046           /* Sign extend the intval into the high-order word */
5047           if (WORDS_BIG_ENDIAN)
5048             {
5049               otherops[1] = operands[1];
5050               operands[1] = (INTVAL (operands[1]) < 0
5051                              ? constm1_rtx : const0_rtx);
5052             }
5053           else
5054             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
5055 #endif
5056           output_mov_immediate (otherops);
5057           output_mov_immediate (operands);
5058         }
5059       else if (code1 == MEM)
5060         {
5061           switch (GET_CODE (XEXP (operands[1], 0)))
5062             {
5063             case REG:
5064               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
5065               break;
5066
5067             case PRE_INC:
5068               abort (); /* Should never happen now */
5069               break;
5070
5071             case PRE_DEC:
5072               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
5073               break;
5074
5075             case POST_INC:
5076               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
5077               break;
5078
5079             case POST_DEC:
5080               abort (); /* Should never happen now */
5081               break;
5082
5083             case LABEL_REF:
5084             case CONST:
5085               output_asm_insn ("adr%?\t%0, %1", operands);
5086               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
5087               break;
5088
5089             default:
5090               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
5091                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
5092                 {
5093                   otherops[0] = operands[0];
5094                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
5095                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
5096                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
5097                     {
5098                       if (GET_CODE (otherops[2]) == CONST_INT)
5099                         {
5100                           switch (INTVAL (otherops[2]))
5101                             {
5102                             case -8:
5103                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
5104                               return "";
5105                             case -4:
5106                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
5107                               return "";
5108                             case 4:
5109                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
5110                               return "";
5111                             }
5112                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
5113                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
5114                           else
5115                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
5116                         }
5117                       else
5118                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
5119                     }
5120                   else
5121                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
5122                   
5123                   return "ldm%?ia\t%0, %M0";
5124                 }
5125               else
5126                 {
5127                   otherops[1] = adj_offsettable_operand (operands[1], 4);
5128                   /* Take care of overlapping base/data reg.  */
5129                   if (reg_mentioned_p (operands[0], operands[1]))
5130                     {
5131                       output_asm_insn ("ldr%?\t%0, %1", otherops);
5132                       output_asm_insn ("ldr%?\t%0, %1", operands);
5133                     }
5134                   else
5135                     {
5136                       output_asm_insn ("ldr%?\t%0, %1", operands);
5137                       output_asm_insn ("ldr%?\t%0, %1", otherops);
5138                     }
5139                 }
5140             }
5141         }
5142       else
5143         abort ();  /* Constraints should prevent this */
5144     }
5145   else if (code0 == MEM && code1 == REG)
5146     {
5147       if (REGNO (operands[1]) == IP_REGNUM)
5148         abort ();
5149
5150       switch (GET_CODE (XEXP (operands[0], 0)))
5151         {
5152         case REG:
5153           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
5154           break;
5155
5156         case PRE_INC:
5157           abort (); /* Should never happen now */
5158           break;
5159
5160         case PRE_DEC:
5161           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
5162           break;
5163
5164         case POST_INC:
5165           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
5166           break;
5167
5168         case POST_DEC:
5169           abort (); /* Should never happen now */
5170           break;
5171
5172         case PLUS:
5173           if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
5174             {
5175               switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
5176                 {
5177                 case -8:
5178                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
5179                   return "";
5180
5181                 case -4:
5182                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
5183                   return "";
5184
5185                 case 4:
5186                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
5187                   return "";
5188                 }
5189             }
5190           /* Fall through */
5191
5192         default:
5193           otherops[0] = adj_offsettable_operand (operands[0], 4);
5194           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
5195           output_asm_insn ("str%?\t%1, %0", operands);
5196           output_asm_insn ("str%?\t%1, %0", otherops);
5197         }
5198     }
5199   else
5200     abort ();  /* Constraints should prevent this */
5201
5202   return "";
5203 }
5204
5205
5206 /* Output an arbitrary MOV reg, #n.
5207    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
5208
5209 char *
5210 output_mov_immediate (operands)
5211      rtx * operands;
5212 {
5213   HOST_WIDE_INT n = INTVAL (operands[1]);
5214   int n_ones = 0;
5215   int i;
5216
5217   /* Try to use one MOV */
5218   if (const_ok_for_arm (n))
5219     {
5220       output_asm_insn ("mov%?\t%0, %1", operands);
5221       return "";
5222     }
5223
5224   /* Try to use one MVN */
5225   if (const_ok_for_arm (~n))
5226     {
5227       operands[1] = GEN_INT (~n);
5228       output_asm_insn ("mvn%?\t%0, %1", operands);
5229       return "";
5230     }
5231
5232   /* If all else fails, make it out of ORRs or BICs as appropriate. */
5233
5234   for (i=0; i < 32; i++)
5235     if (n & 1 << i)
5236       n_ones++;
5237
5238   if (n_ones > 16)  /* Shorter to use MVN with BIC in this case. */
5239     output_multi_immediate(operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1,
5240                            ~n);
5241   else
5242     output_multi_immediate(operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1,
5243                            n);
5244
5245   return "";
5246 }
5247
5248
5249 /* Output an ADD r, s, #n where n may be too big for one instruction.  If
5250    adding zero to one register, output nothing.  */
5251
5252 char *
5253 output_add_immediate (operands)
5254      rtx * operands;
5255 {
5256   HOST_WIDE_INT n = INTVAL (operands[2]);
5257
5258   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
5259     {
5260       if (n < 0)
5261         output_multi_immediate (operands,
5262                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
5263                                 -n);
5264       else
5265         output_multi_immediate (operands,
5266                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
5267                                 n);
5268     }
5269
5270   return "";
5271 }
5272
5273 /* Output a multiple immediate operation.
5274    OPERANDS is the vector of operands referred to in the output patterns.
5275    INSTR1 is the output pattern to use for the first constant.
5276    INSTR2 is the output pattern to use for subsequent constants.
5277    IMMED_OP is the index of the constant slot in OPERANDS.
5278    N is the constant value.  */
5279
5280 static char *
5281 output_multi_immediate (operands, instr1, instr2, immed_op, n)
5282      rtx * operands;
5283      char * instr1, * instr2;
5284      int immed_op;
5285      HOST_WIDE_INT n;
5286 {
5287 #if HOST_BITS_PER_WIDE_INT > 32
5288   n &= 0xffffffff;
5289 #endif
5290
5291   if (n == 0)
5292     {
5293       operands[immed_op] = const0_rtx;
5294       output_asm_insn (instr1, operands); /* Quick and easy output */
5295     }
5296   else
5297     {
5298       int i;
5299       char *instr = instr1;
5300
5301       /* Note that n is never zero here (which would give no output) */
5302       for (i = 0; i < 32; i += 2)
5303         {
5304           if (n & (3 << i))
5305             {
5306               operands[immed_op] = GEN_INT (n & (255 << i));
5307               output_asm_insn (instr, operands);
5308               instr = instr2;
5309               i += 6;
5310             }
5311         }
5312     }
5313   return "";
5314 }
5315
5316
5317 /* Return the appropriate ARM instruction for the operation code.
5318    The returned result should not be overwritten.  OP is the rtx of the
5319    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
5320    was shifted.  */
5321
5322 char *
5323 arithmetic_instr (op, shift_first_arg)
5324      rtx op;
5325      int shift_first_arg;
5326 {
5327   switch (GET_CODE (op))
5328     {
5329     case PLUS:
5330       return "add";
5331
5332     case MINUS:
5333       return shift_first_arg ? "rsb" : "sub";
5334
5335     case IOR:
5336       return "orr";
5337
5338     case XOR:
5339       return "eor";
5340
5341     case AND:
5342       return "and";
5343
5344     default:
5345       abort ();
5346     }
5347 }
5348
5349
5350 /* Ensure valid constant shifts and return the appropriate shift mnemonic
5351    for the operation code.  The returned result should not be overwritten.
5352    OP is the rtx code of the shift.
5353    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
5354    shift. */
5355
5356 static char *
5357 shift_op (op, amountp)
5358      rtx op;
5359      HOST_WIDE_INT *amountp;
5360 {
5361   char * mnem;
5362   enum rtx_code code = GET_CODE (op);
5363
5364   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
5365     *amountp = -1;
5366   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
5367     *amountp = INTVAL (XEXP (op, 1));
5368   else
5369     abort ();
5370
5371   switch (code)
5372     {
5373     case ASHIFT:
5374       mnem = "asl";
5375       break;
5376
5377     case ASHIFTRT:
5378       mnem = "asr";
5379       break;
5380
5381     case LSHIFTRT:
5382       mnem = "lsr";
5383       break;
5384
5385     case ROTATERT:
5386       mnem = "ror";
5387       break;
5388
5389     case MULT:
5390       /* We never have to worry about the amount being other than a
5391          power of 2, since this case can never be reloaded from a reg.  */
5392       if (*amountp != -1)
5393         *amountp = int_log2 (*amountp);
5394       else
5395         abort ();
5396       return "asl";
5397
5398     default:
5399       abort ();
5400     }
5401
5402   if (*amountp != -1)
5403     {
5404       /* This is not 100% correct, but follows from the desire to merge
5405          multiplication by a power of 2 with the recognizer for a
5406          shift.  >=32 is not a valid shift for "asl", so we must try and
5407          output a shift that produces the correct arithmetical result.
5408          Using lsr #32 is identical except for the fact that the carry bit
5409          is not set correctly if we set the flags; but we never use the 
5410          carry bit from such an operation, so we can ignore that.  */
5411       if (code == ROTATERT)
5412         *amountp &= 31;         /* Rotate is just modulo 32 */
5413       else if (*amountp != (*amountp & 31))
5414         {
5415           if (code == ASHIFT)
5416             mnem = "lsr";
5417           *amountp = 32;
5418         }
5419
5420       /* Shifts of 0 are no-ops.  */
5421       if (*amountp == 0)
5422         return NULL;
5423     }     
5424
5425   return mnem;
5426 }
5427
5428
5429 /* Obtain the shift from the POWER of two. */
5430
5431 static HOST_WIDE_INT
5432 int_log2 (power)
5433      HOST_WIDE_INT power;
5434 {
5435   HOST_WIDE_INT shift = 0;
5436
5437   while (((((HOST_WIDE_INT) 1) << shift) & power) == 0)
5438     {
5439       if (shift > 31)
5440         abort ();
5441       shift++;
5442     }
5443
5444   return shift;
5445 }
5446
5447 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
5448    /bin/as is horribly restrictive.  */
5449 #define MAX_ASCII_LEN 51
5450
5451 void
5452 output_ascii_pseudo_op (stream, p, len)
5453      FILE * stream;
5454      unsigned char * p;
5455      int len;
5456 {
5457   int i;
5458   int len_so_far = 0;
5459
5460   fputs ("\t.ascii\t\"", stream);
5461   
5462   for (i = 0; i < len; i++)
5463     {
5464       register int c = p[i];
5465
5466       if (len_so_far >= MAX_ASCII_LEN)
5467         {
5468           fputs ("\"\n\t.ascii\t\"", stream);
5469           len_so_far = 0;
5470         }
5471
5472       switch (c)
5473         {
5474         case TARGET_TAB:                
5475           fputs ("\\t", stream);
5476           len_so_far += 2;                      
5477           break;
5478           
5479         case TARGET_FF:
5480           fputs ("\\f", stream);
5481           len_so_far += 2;
5482           break;
5483           
5484         case TARGET_BS:
5485           fputs ("\\b", stream);
5486           len_so_far += 2;
5487           break;
5488           
5489         case TARGET_CR:
5490           fputs ("\\r", stream);
5491           len_so_far += 2;
5492           break;
5493           
5494         case TARGET_NEWLINE:
5495           fputs ("\\n", stream);
5496           c = p [i + 1];
5497           if ((c >= ' ' && c <= '~')
5498               || c == TARGET_TAB)
5499             /* This is a good place for a line break.  */
5500             len_so_far = MAX_ASCII_LEN;
5501           else
5502             len_so_far += 2;
5503           break;
5504           
5505         case '\"':
5506         case '\\':
5507           putc ('\\', stream);
5508           len_so_far ++;
5509           /* drop through.  */
5510
5511         default:
5512           if (c >= ' ' && c <= '~')
5513             {
5514               putc (c, stream);
5515               len_so_far ++;
5516             }
5517           else
5518             {
5519               fprintf (stream, "\\%03o", c);
5520               len_so_far += 4;
5521             }
5522           break;
5523         }
5524     }
5525
5526   fputs ("\"\n", stream);
5527 }
5528 \f
5529
5530 /* Try to determine whether a pattern really clobbers the link register.
5531    This information is useful when peepholing, so that lr need not be pushed
5532    if we combine a call followed by a return.
5533    NOTE: This code does not check for side-effect expressions in a SET_SRC:
5534    such a check should not be needed because these only update an existing
5535    value within a register; the register must still be set elsewhere within
5536    the function. */
5537
5538 static int
5539 pattern_really_clobbers_lr (x)
5540      rtx x;
5541 {
5542   int i;
5543   
5544   switch (GET_CODE (x))
5545     {
5546     case SET:
5547       switch (GET_CODE (SET_DEST (x)))
5548         {
5549         case REG:
5550           return REGNO (SET_DEST (x)) == LR_REGNUM;
5551
5552         case SUBREG:
5553           if (GET_CODE (XEXP (SET_DEST (x), 0)) == REG)
5554             return REGNO (XEXP (SET_DEST (x), 0)) == LR_REGNUM;
5555
5556           if (GET_CODE (XEXP (SET_DEST (x), 0)) == MEM)
5557             return 0;
5558           abort ();
5559
5560         default:
5561           return 0;
5562         }
5563
5564     case PARALLEL:
5565       for (i = 0; i < XVECLEN (x, 0); i++)
5566         if (pattern_really_clobbers_lr (XVECEXP (x, 0, i)))
5567           return 1;
5568       return 0;
5569
5570     case CLOBBER:
5571       switch (GET_CODE (XEXP (x, 0)))
5572         {
5573         case REG:
5574           return REGNO (XEXP (x, 0)) == LR_REGNUM;
5575
5576         case SUBREG:
5577           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG)
5578             return REGNO (XEXP (XEXP (x, 0), 0)) == LR_REGNUM;
5579           abort ();
5580
5581         default:
5582           return 0;
5583         }
5584
5585     case UNSPEC:
5586       return 1;
5587
5588     default:
5589       return 0;
5590     }
5591 }
5592
5593 static int
5594 function_really_clobbers_lr (first)
5595      rtx first;
5596 {
5597   rtx insn, next;
5598   
5599   for (insn = first; insn; insn = next_nonnote_insn (insn))
5600     {
5601       switch (GET_CODE (insn))
5602         {
5603         case BARRIER:
5604         case NOTE:
5605         case CODE_LABEL:
5606         case JUMP_INSN:         /* Jump insns only change the PC (and conds) */
5607           break;
5608
5609         case INSN:
5610           if (pattern_really_clobbers_lr (PATTERN (insn)))
5611             return 1;
5612           break;
5613
5614         case CALL_INSN:
5615           /* Don't yet know how to handle those calls that are not to a 
5616              SYMBOL_REF */
5617           if (GET_CODE (PATTERN (insn)) != PARALLEL)
5618             abort ();
5619
5620           switch (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)))
5621             {
5622             case CALL:
5623               if (GET_CODE (XEXP (XEXP (XVECEXP (PATTERN (insn), 0, 0), 0), 0))
5624                   != SYMBOL_REF)
5625                 return 1;
5626               break;
5627
5628             case SET:
5629               if (GET_CODE (XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn),
5630                                                           0, 0)), 0), 0))
5631                   != SYMBOL_REF)
5632                 return 1;
5633               break;
5634
5635             default:    /* Don't recognize it, be safe */
5636               return 1;
5637             }
5638
5639           /* A call can be made (by peepholing) not to clobber lr iff it is
5640              followed by a return.  There may, however, be a use insn iff
5641              we are returning the result of the call. 
5642              If we run off the end of the insn chain, then that means the
5643              call was at the end of the function.  Unfortunately we don't
5644              have a return insn for the peephole to recognize, so we
5645              must reject this.  (Can this be fixed by adding our own insn?) */
5646           if ((next = next_nonnote_insn (insn)) == NULL)
5647             return 1;
5648
5649           /* No need to worry about lr if the call never returns */
5650           if (GET_CODE (next) == BARRIER)
5651             break;
5652
5653           if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == USE
5654               && (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
5655               && (REGNO (SET_DEST (XVECEXP (PATTERN (insn), 0, 0)))
5656                   == REGNO (XEXP (PATTERN (next), 0))))
5657             if ((next = next_nonnote_insn (next)) == NULL)
5658               return 1;
5659
5660           if (GET_CODE (next) == JUMP_INSN
5661               && GET_CODE (PATTERN (next)) == RETURN)
5662             break;
5663           return 1;
5664
5665         default:
5666           abort ();
5667         }
5668     }
5669
5670   /* We have reached the end of the chain so lr was _not_ clobbered */
5671   return 0;
5672 }
5673
5674 char *
5675 output_return_instruction (operand, really_return, reverse)
5676      rtx operand;
5677      int really_return;
5678      int reverse;
5679 {
5680   char instr[100];
5681   int reg, live_regs = 0;
5682   int volatile_func = (optimize > 0 
5683                        && TREE_THIS_VOLATILE (current_function_decl));
5684
5685   return_used_this_function = 1;
5686
5687   if (TARGET_ABORT_NORETURN && volatile_func)
5688     {
5689       /* If this function was declared non-returning, and we have found a tail 
5690          call, then we have to trust that the called function won't return.  */
5691       if (really_return)
5692         {
5693           rtx ops[2];
5694       
5695           /* Otherwise, trap an attempted return by aborting.  */
5696           ops[0] = operand;
5697           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
5698                                        : "abort");
5699           assemble_external_libcall (ops[1]);
5700           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
5701         }
5702       
5703       return "";
5704     }
5705       
5706   if (current_function_calls_alloca && ! really_return)
5707     abort ();
5708     
5709   for (reg = 0; reg <= 10; reg++)
5710     if (regs_ever_live[reg] && ! call_used_regs[reg])
5711       live_regs++;
5712
5713   if (flag_pic && ! TARGET_SINGLE_PIC_BASE
5714       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5715     live_regs++;
5716
5717   if (live_regs || (regs_ever_live[LR_REGNUM] && ! lr_save_eliminated))
5718     live_regs++;
5719
5720   if (frame_pointer_needed)
5721     live_regs += 4;
5722
5723   /* On some ARM architectures it is faster to use LDR rather than LDM to
5724      load a single register.  On other architectures, the cost is the same.  */
5725   if (live_regs == 1
5726       && regs_ever_live[LR_REGNUM]
5727       && ! lr_save_eliminated
5728       /* FIXME: We ought to handle the case TARGET_APCS_32 is true,
5729          really_return is true, and only the PC needs restoring.  */
5730       && ! really_return)
5731     {
5732       output_asm_insn (reverse ? "ldr%?%D0\t%|lr, [%|sp], #4" 
5733                        : "ldr%?%d0\t%|lr, [%|sp], #4", &operand);
5734     }
5735   else if (live_regs)
5736     {
5737       if (lr_save_eliminated || ! regs_ever_live[LR_REGNUM])
5738         live_regs++;
5739
5740       if (frame_pointer_needed)
5741         strcpy (instr,
5742                 reverse ? "ldm%?%D0ea\t%|fp, {" : "ldm%?%d0ea\t%|fp, {");
5743       else
5744         strcpy (instr, 
5745                 reverse ? "ldm%?%D0fd\t%|sp!, {" : "ldm%?%d0fd\t%|sp!, {");
5746
5747       for (reg = 0; reg <= 10; reg++)
5748         if (regs_ever_live[reg]
5749             && (! call_used_regs[reg]
5750                 || (flag_pic && ! TARGET_SINGLE_PIC_BASE
5751                     && reg == PIC_OFFSET_TABLE_REGNUM)))
5752           {
5753             strcat (instr, "%|");
5754             strcat (instr, reg_names[reg]);
5755             if (--live_regs)
5756               strcat (instr, ", ");
5757           }
5758
5759       if (frame_pointer_needed)
5760         {
5761           strcat (instr, "%|");
5762           strcat (instr, reg_names[11]);
5763           strcat (instr, ", ");
5764           strcat (instr, "%|");
5765           strcat (instr, reg_names[13]);
5766           strcat (instr, ", ");
5767           strcat (instr, "%|");
5768           strcat (instr, TARGET_INTERWORK || (! really_return)
5769                   ? reg_names[LR_REGNUM] : reg_names[PC_REGNUM] );
5770         }
5771       else
5772         {
5773           strcat (instr, "%|");
5774           if (TARGET_INTERWORK && really_return)
5775             strcat (instr, reg_names[IP_REGNUM]);
5776           else
5777             strcat (instr, really_return ? reg_names[PC_REGNUM] : reg_names[LR_REGNUM]);
5778         }
5779       strcat (instr, (TARGET_APCS_32 || !really_return) ? "}" : "}^");
5780       output_asm_insn (instr, &operand);
5781
5782       if (TARGET_INTERWORK && really_return)
5783         {
5784           strcpy (instr, "bx%?");
5785           strcat (instr, reverse ? "%D0" : "%d0");
5786           strcat (instr, "\t%|");
5787           strcat (instr, frame_pointer_needed ? "lr" : "ip");
5788
5789           output_asm_insn (instr, & operand);
5790         }
5791     }
5792   else if (really_return)
5793     {
5794       if (TARGET_INTERWORK)
5795         sprintf (instr, "bx%%?%%%s0\t%%|lr", reverse ? "D" : "d");
5796       else
5797         sprintf (instr, "mov%%?%%%s0%s\t%%|pc, %%|lr",
5798                  reverse ? "D" : "d", TARGET_APCS_32 ? "" : "s");
5799       
5800       output_asm_insn (instr, & operand);
5801     }
5802
5803   return "";
5804 }
5805
5806 /* Return nonzero if optimizing and the current function is volatile.
5807    Such functions never return, and many memory cycles can be saved
5808    by not storing register values that will never be needed again.
5809    This optimization was added to speed up context switching in a
5810    kernel application. */
5811
5812 int
5813 arm_volatile_func ()
5814 {
5815   return (optimize > 0 && TREE_THIS_VOLATILE (current_function_decl));
5816 }
5817
5818 /* Write the function name into the code section, directly preceding
5819    the function prologue.
5820
5821    Code will be output similar to this:
5822      t0
5823          .ascii "arm_poke_function_name", 0
5824          .align
5825      t1
5826          .word 0xff000000 + (t1 - t0)
5827      arm_poke_function_name
5828          mov     ip, sp
5829          stmfd   sp!, {fp, ip, lr, pc}
5830          sub     fp, ip, #4
5831
5832    When performing a stack backtrace, code can inspect the value
5833    of 'pc' stored at 'fp' + 0.  If the trace function then looks
5834    at location pc - 12 and the top 8 bits are set, then we know
5835    that there is a function name embedded immediately preceding this
5836    location and has length ((pc[-3]) & 0xff000000).
5837
5838    We assume that pc is declared as a pointer to an unsigned long.
5839
5840    It is of no benefit to output the function name if we are assembling
5841    a leaf function.  These function types will not contain a stack
5842    backtrace structure, therefore it is not possible to determine the
5843    function name.  */
5844
5845 void
5846 arm_poke_function_name (stream, name)
5847    FILE * stream;
5848    char * name;
5849 {
5850   unsigned long alignlength;
5851   unsigned long length;
5852   rtx           x;
5853
5854   length = strlen (name) + 1;
5855   alignlength = (length + 3) & ~3;
5856   
5857   ASM_OUTPUT_ASCII (stream, name, length);
5858   ASM_OUTPUT_ALIGN (stream, 2);
5859   x = GEN_INT (0xff000000UL + alignlength);
5860   ASM_OUTPUT_INT (stream, x);
5861 }
5862
5863 /* The amount of stack adjustment that happens here, in output_return and in
5864    output_epilogue must be exactly the same as was calculated during reload,
5865    or things will point to the wrong place.  The only time we can safely
5866    ignore this constraint is when a function has no arguments on the stack,
5867    no stack frame requirement and no live registers execpt for `lr'.  If we
5868    can guarantee that by making all function calls into tail calls and that
5869    lr is not clobbered in any other way, then there is no need to push lr
5870    onto the stack. */
5871    
5872 void
5873 output_func_prologue (f, frame_size)
5874      FILE * f;
5875      int frame_size;
5876 {
5877   int reg, live_regs_mask = 0;
5878   int volatile_func = (optimize > 0
5879                        && TREE_THIS_VOLATILE (current_function_decl));
5880
5881   /* Nonzero if we must stuff some register arguments onto the stack as if
5882      they were passed there.  */
5883   int store_arg_regs = 0;
5884
5885   if (arm_ccfsm_state || arm_target_insn)
5886     abort ();                                   /* Sanity check */
5887
5888   if (arm_naked_function_p (current_function_decl))
5889     return;
5890
5891   return_used_this_function = 0;
5892   lr_save_eliminated = 0;
5893   
5894   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
5895                current_function_args_size,
5896                current_function_pretend_args_size, frame_size);
5897   asm_fprintf (f, "\t%@ frame_needed = %d, current_function_anonymous_args = %d\n",
5898                frame_pointer_needed,
5899                current_function_anonymous_args);
5900
5901   if (volatile_func)
5902     asm_fprintf (f, "\t%@ Volatile function.\n");
5903
5904   if (current_function_anonymous_args && current_function_pretend_args_size)
5905     store_arg_regs = 1;
5906
5907   for (reg = 0; reg <= 10; reg++)
5908     if (regs_ever_live[reg] && ! call_used_regs[reg])
5909       live_regs_mask |= (1 << reg);
5910
5911   if (flag_pic && ! TARGET_SINGLE_PIC_BASE
5912       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5913     live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
5914
5915   if (frame_pointer_needed)
5916     live_regs_mask |= 0xD800;
5917   else if (regs_ever_live[LR_REGNUM])
5918     {
5919       if (! current_function_args_size
5920           && ! function_really_clobbers_lr (get_insns ()))
5921         lr_save_eliminated = 1;
5922       else
5923         live_regs_mask |= 1 << LR_REGNUM;
5924     }
5925
5926   if (live_regs_mask)
5927     {
5928       /* if a di mode load/store multiple is used, and the base register
5929          is r3, then r4 can become an ever live register without lr
5930          doing so,  in this case we need to push lr as well, or we
5931          will fail to get a proper return. */
5932
5933       live_regs_mask |= 1 << LR_REGNUM;
5934       lr_save_eliminated = 0;
5935
5936     }
5937
5938   if (lr_save_eliminated)
5939     asm_fprintf (f,"\t%@ I don't think this function clobbers lr\n");
5940
5941 #ifdef AOF_ASSEMBLER
5942   if (flag_pic)
5943     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
5944 #endif
5945 }
5946
5947 char *
5948 arm_output_epilogue ()
5949 {
5950   int reg;
5951   int live_regs_mask = 0;
5952   /* If we need this, then it will always be at least this much */
5953   int floats_offset = 12;
5954   rtx operands[3];
5955   int frame_size = get_frame_size ();
5956   FILE *f = asm_out_file;
5957   int volatile_func = (optimize > 0
5958                        && TREE_THIS_VOLATILE (current_function_decl));
5959
5960   if (use_return_insn (FALSE) && return_used_this_function)
5961     return "";
5962
5963   /* Naked functions don't have epilogues.  */
5964   if (arm_naked_function_p (current_function_decl))
5965     return "";
5966
5967   /* A volatile function should never return.  Call abort.  */
5968   if (TARGET_ABORT_NORETURN && volatile_func)
5969     {
5970       rtx op;
5971       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
5972       assemble_external_libcall (op);
5973       output_asm_insn ("bl\t%a0", &op);
5974       return "";
5975     }
5976
5977   for (reg = 0; reg <= 10; reg++)
5978     if (regs_ever_live[reg] && ! call_used_regs[reg])
5979       {
5980         live_regs_mask |= (1 << reg);
5981         floats_offset += 4;
5982       }
5983
5984   /* If we aren't loading the PIC register, don't stack it even though it may
5985      be live.  */
5986   if (flag_pic && ! TARGET_SINGLE_PIC_BASE 
5987       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5988     {
5989       live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
5990       floats_offset += 4;
5991     }
5992
5993   if (frame_pointer_needed)
5994     {
5995       if (arm_fpu_arch == FP_SOFT2)
5996         {
5997           for (reg = 23; reg > 15; reg--)
5998             if (regs_ever_live[reg] && ! call_used_regs[reg])
5999               {
6000                 floats_offset += 12;
6001                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
6002                              reg, FP_REGNUM, floats_offset);
6003               }
6004         }
6005       else
6006         {
6007           int start_reg = 23;
6008
6009           for (reg = 23; reg > 15; reg--)
6010             {
6011               if (regs_ever_live[reg] && ! call_used_regs[reg])
6012                 {
6013                   floats_offset += 12;
6014                   
6015                   /* We can't unstack more than four registers at once */
6016                   if (start_reg - reg == 3)
6017                     {
6018                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
6019                                    reg, FP_REGNUM, floats_offset);
6020                       start_reg = reg - 1;
6021                     }
6022                 }
6023               else
6024                 {
6025                   if (reg != start_reg)
6026                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
6027                                  reg + 1, start_reg - reg,
6028                                  FP_REGNUM, floats_offset);
6029                   start_reg = reg - 1;
6030                 }
6031             }
6032
6033           /* Just in case the last register checked also needs unstacking.  */
6034           if (reg != start_reg)
6035             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
6036                          reg + 1, start_reg - reg,
6037                          FP_REGNUM, floats_offset);
6038         }
6039       
6040       if (TARGET_INTERWORK)
6041         {
6042           live_regs_mask |= 0x6800;
6043           print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask, FALSE);
6044           asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6045         }
6046       else
6047         {
6048           live_regs_mask |= 0xA800;
6049           print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask,
6050                            TARGET_APCS_32 ? FALSE : TRUE);
6051         }
6052     }
6053   else
6054     {
6055       /* Restore stack pointer if necessary.  */
6056       if (frame_size + current_function_outgoing_args_size != 0)
6057         {
6058           operands[0] = operands[1] = stack_pointer_rtx;
6059           operands[2] = GEN_INT (frame_size
6060                                  + current_function_outgoing_args_size);
6061           output_add_immediate (operands);
6062         }
6063
6064       if (arm_fpu_arch == FP_SOFT2)
6065         {
6066           for (reg = 16; reg < 24; reg++)
6067             if (regs_ever_live[reg] && ! call_used_regs[reg])
6068               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
6069                            reg, SP_REGNUM);
6070         }
6071       else
6072         {
6073           int start_reg = 16;
6074
6075           for (reg = 16; reg < 24; reg++)
6076             {
6077               if (regs_ever_live[reg] && ! call_used_regs[reg])
6078                 {
6079                   if (reg - start_reg == 3)
6080                     {
6081                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
6082                                    start_reg, SP_REGNUM);
6083                       start_reg = reg + 1;
6084                     }
6085                 }
6086               else
6087                 {
6088                   if (reg != start_reg)
6089                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
6090                                  start_reg, reg - start_reg,
6091                                  SP_REGNUM);
6092                   
6093                   start_reg = reg + 1;
6094                 }
6095             }
6096
6097           /* Just in case the last register checked also needs unstacking.  */
6098           if (reg != start_reg)
6099             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
6100                          start_reg, reg - start_reg, SP_REGNUM);
6101         }
6102
6103       if (current_function_pretend_args_size == 0 && regs_ever_live[LR_REGNUM])
6104         {
6105           if (TARGET_INTERWORK)
6106             {
6107               if (! lr_save_eliminated)
6108                 live_regs_mask |= 1 << LR_REGNUM;
6109
6110               if (live_regs_mask != 0)
6111                 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask, FALSE);
6112               
6113               asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6114             }
6115           else if (lr_save_eliminated)
6116             asm_fprintf (f, "\tmov%c\t%r, %r\n",
6117                          TARGET_APCS_32 ? ' ' : 's',
6118                          PC_REGNUM, LR_REGNUM);
6119           else
6120             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask | 0x8000,
6121                              TARGET_APCS_32 ? FALSE : TRUE);
6122         }
6123       else
6124         {
6125           if (live_regs_mask || regs_ever_live[LR_REGNUM])
6126             {
6127               /* Restore the integer regs, and the return address into lr */
6128               if (! lr_save_eliminated)
6129                 live_regs_mask |= 1 << LR_REGNUM;
6130
6131               if (live_regs_mask != 0)
6132                 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask, FALSE);
6133             }
6134
6135           if (current_function_pretend_args_size)
6136             {
6137               /* Unwind the pre-pushed regs */
6138               operands[0] = operands[1] = stack_pointer_rtx;
6139               operands[2] = GEN_INT (current_function_pretend_args_size);
6140               output_add_immediate (operands);
6141             }
6142           /* And finally, go home */
6143           if (TARGET_INTERWORK)
6144             asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6145           else if (TARGET_APCS_32)
6146             asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6147           else
6148             asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6149         }
6150     }
6151
6152   return "";
6153 }
6154
6155 void
6156 output_func_epilogue (frame_size)
6157      int frame_size;
6158 {
6159   if (use_return_insn (FALSE) && return_used_this_function
6160       && (frame_size + current_function_outgoing_args_size) != 0
6161       && ! (frame_pointer_needed && TARGET_APCS))
6162     abort ();
6163
6164   /* Reset the ARM-specific per-function variables.  */
6165   current_function_anonymous_args = 0;
6166   after_arm_reorg = 0;
6167 }
6168
6169 static void
6170 emit_multi_reg_push (mask)
6171      int mask;
6172 {
6173   int num_regs = 0;
6174   int i, j;
6175   rtx par;
6176
6177   for (i = 0; i < 16; i++)
6178     if (mask & (1 << i))
6179       num_regs++;
6180
6181   if (num_regs == 0 || num_regs > 16)
6182     abort ();
6183
6184   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
6185
6186   for (i = 0; i < 16; i++)
6187     {
6188       if (mask & (1 << i))
6189         {
6190           XVECEXP (par, 0, 0)
6191             = gen_rtx_SET (VOIDmode,
6192                            gen_rtx_MEM (BLKmode,
6193                                         gen_rtx_PRE_DEC (BLKmode,
6194                                                          stack_pointer_rtx)),
6195                            gen_rtx_UNSPEC (BLKmode,
6196                                            gen_rtvec (1,
6197                                                       gen_rtx_REG (SImode, i)),
6198                                            2));
6199           break;
6200         }
6201     }
6202
6203   for (j = 1, i++; j < num_regs; i++)
6204     {
6205       if (mask & (1 << i))
6206         {
6207           XVECEXP (par, 0, j)
6208             = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, i));
6209           j++;
6210         }
6211     }
6212
6213   emit_insn (par);
6214 }
6215
6216 static void
6217 emit_sfm (base_reg, count)
6218      int base_reg;
6219      int count;
6220 {
6221   rtx par;
6222   int i;
6223
6224   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
6225
6226   XVECEXP (par, 0, 0)
6227     = gen_rtx_SET (VOIDmode, 
6228                    gen_rtx_MEM (BLKmode,
6229                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
6230                    gen_rtx_UNSPEC (BLKmode,
6231                                    gen_rtvec (1, gen_rtx_REG (XFmode, 
6232                                                               base_reg++)),
6233                                    2));
6234   for (i = 1; i < count; i++)
6235     XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, 
6236                                        gen_rtx_REG (XFmode, base_reg++));
6237
6238   emit_insn (par);
6239 }
6240
6241 void
6242 arm_expand_prologue ()
6243 {
6244   int reg;
6245   rtx amount = GEN_INT (-(get_frame_size ()
6246                           + current_function_outgoing_args_size));
6247   int live_regs_mask = 0;
6248   int store_arg_regs = 0;
6249   /* If this function doesn't return, then there is no need to push
6250      the call-saved regs.  */
6251   int volatile_func = (optimize > 0
6252                        && TREE_THIS_VOLATILE (current_function_decl));
6253
6254   /* Naked functions don't have prologues.  */
6255   if (arm_naked_function_p (current_function_decl))
6256     return;
6257
6258   if (current_function_anonymous_args && current_function_pretend_args_size)
6259     store_arg_regs = 1;
6260
6261   if (! volatile_func)
6262     {
6263       for (reg = 0; reg <= 10; reg++)
6264         if (regs_ever_live[reg] && ! call_used_regs[reg])
6265           live_regs_mask |= 1 << reg;
6266
6267       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6268         live_regs_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
6269
6270       if (regs_ever_live[LR_REGNUM])
6271         live_regs_mask |= 1 << LR_REGNUM;
6272     }
6273
6274   if (frame_pointer_needed)
6275     {
6276       live_regs_mask |= 0xD800;
6277       emit_insn (gen_movsi (gen_rtx_REG (SImode, IP_REGNUM),
6278                             stack_pointer_rtx));
6279     }
6280
6281   if (current_function_pretend_args_size)
6282     {
6283       if (store_arg_regs)
6284         emit_multi_reg_push ((0xf0 >> (current_function_pretend_args_size / 4))
6285                              & 0xf);
6286       else
6287         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
6288                                GEN_INT (-current_function_pretend_args_size)));
6289     }
6290
6291   if (live_regs_mask)
6292     {
6293       /* If we have to push any regs, then we must push lr as well, or
6294          we won't get a proper return.  */
6295       live_regs_mask |= 1 << LR_REGNUM;
6296       emit_multi_reg_push (live_regs_mask);
6297     }
6298       
6299   /* For now the integer regs are still pushed in output_func_epilogue ().  */
6300
6301   if (! volatile_func)
6302     {
6303       if (arm_fpu_arch == FP_SOFT2)
6304         {
6305           for (reg = 23; reg > 15; reg--)
6306             if (regs_ever_live[reg] && ! call_used_regs[reg])
6307               emit_insn (gen_rtx_SET
6308                          (VOIDmode, 
6309                           gen_rtx_MEM (XFmode, 
6310                                        gen_rtx_PRE_DEC (XFmode,
6311                                                         stack_pointer_rtx)),
6312                           gen_rtx_REG (XFmode, reg)));
6313         }
6314       else
6315         {
6316           int start_reg = 23;
6317
6318           for (reg = 23; reg > 15; reg--)
6319             {
6320               if (regs_ever_live[reg] && ! call_used_regs[reg])
6321                 {
6322                   if (start_reg - reg == 3)
6323                     {
6324                       emit_sfm (reg, 4);
6325                       start_reg = reg - 1;
6326                     }
6327                 }
6328               else
6329                 {
6330                   if (start_reg != reg)
6331                     emit_sfm (reg + 1, start_reg - reg);
6332                   start_reg = reg - 1;
6333                 }
6334             }
6335
6336           if (start_reg != reg)
6337             emit_sfm (reg + 1, start_reg - reg);
6338         }
6339     }
6340
6341   if (frame_pointer_needed)
6342     emit_insn (gen_addsi3 (hard_frame_pointer_rtx, gen_rtx_REG (SImode, IP_REGNUM),
6343                            (GEN_INT
6344                             (-(4 + current_function_pretend_args_size)))));
6345
6346   if (amount != const0_rtx)
6347     {
6348       emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, amount));
6349       emit_insn (gen_rtx_CLOBBER (VOIDmode, 
6350                                   gen_rtx_MEM (BLKmode, stack_pointer_rtx)));
6351     }
6352
6353   /* If we are profiling, make sure no instructions are scheduled before
6354      the call to mcount.  Similarly if the user has requested no
6355      scheduling in the prolog.  */
6356   if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
6357     emit_insn (gen_blockage ());
6358 }
6359   
6360 \f
6361 /* If CODE is 'd', then the X is a condition operand and the instruction
6362    should only be executed if the condition is true.
6363    if CODE is 'D', then the X is a condition operand and the instruction
6364    should only be executed if the condition is false: however, if the mode
6365    of the comparison is CCFPEmode, then always execute the instruction -- we
6366    do this because in these circumstances !GE does not necessarily imply LT;
6367    in these cases the instruction pattern will take care to make sure that
6368    an instruction containing %d will follow, thereby undoing the effects of
6369    doing this instruction unconditionally.
6370    If CODE is 'N' then X is a floating point operand that must be negated
6371    before output.
6372    If CODE is 'B' then output a bitwise inverted value of X (a const int).
6373    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
6374
6375 void
6376 arm_print_operand (stream, x, code)
6377      FILE * stream;
6378      rtx x;
6379      int code;
6380 {
6381   switch (code)
6382     {
6383     case '@':
6384       fputs (ASM_COMMENT_START, stream);
6385       return;
6386
6387     case '|':
6388       fputs (REGISTER_PREFIX, stream);
6389       return;
6390
6391     case '?':
6392       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
6393         fputs (arm_condition_codes[arm_current_cc], stream);
6394       return;
6395
6396     case 'N':
6397       {
6398         REAL_VALUE_TYPE r;
6399         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6400         r = REAL_VALUE_NEGATE (r);
6401         fprintf (stream, "%s", fp_const_from_val (&r));
6402       }
6403       return;
6404
6405     case 'B':
6406       if (GET_CODE (x) == CONST_INT)
6407         {
6408           HOST_WIDE_INT val;
6409           val = ARM_SIGN_EXTEND (~ INTVAL (x));
6410           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
6411         }
6412       else
6413         {
6414           putc ('~', stream);
6415           output_addr_const (stream, x);
6416         }
6417       return;
6418
6419     case 'i':
6420       fprintf (stream, "%s", arithmetic_instr (x, 1));
6421       return;
6422
6423     case 'I':
6424       fprintf (stream, "%s", arithmetic_instr (x, 0));
6425       return;
6426
6427     case 'S':
6428       {
6429         HOST_WIDE_INT val;
6430         char * shift = shift_op (x, & val);
6431
6432         if (shift)
6433           {
6434             fprintf (stream, ", %s ", shift_op (x, & val));
6435             if (val == -1)
6436               arm_print_operand (stream, XEXP (x, 1), 0);
6437             else
6438               {
6439                 fputc ('#', stream);
6440                 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
6441               }
6442           }
6443       }
6444       return;
6445
6446     case 'Q':
6447       if (REGNO (x) > 15)
6448         abort ();
6449       fputs (REGISTER_PREFIX, stream);
6450       fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0)], stream);
6451       return;
6452
6453     case 'R':
6454       if (REGNO (x) > 15)
6455         abort ();
6456       fputs (REGISTER_PREFIX, stream);
6457       fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1)], stream);
6458       return;
6459
6460     case 'm':
6461       fputs (REGISTER_PREFIX, stream);
6462       if (GET_CODE (XEXP (x, 0)) == REG)
6463         fputs (reg_names[REGNO (XEXP (x, 0))], stream);
6464       else
6465         fputs (reg_names[REGNO (XEXP (XEXP (x, 0), 0))], stream);
6466       return;
6467
6468     case 'M':
6469       asm_fprintf (stream, "{%r-%r}",
6470                    REGNO (x), REGNO (x) + NUM_REGS (GET_MODE (x)) - 1);
6471       return;
6472
6473     case 'd':
6474       if (x)
6475         fputs (arm_condition_codes[get_arm_condition_code (x)],
6476                stream);
6477       return;
6478
6479     case 'D':
6480       if (x)
6481         fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
6482                                    (get_arm_condition_code (x))],
6483                stream);
6484       return;
6485
6486     default:
6487       if (x == 0)
6488         abort ();
6489
6490       if (GET_CODE (x) == REG)
6491         {
6492           fputs (REGISTER_PREFIX, stream);
6493           fputs (reg_names[REGNO (x)], stream);
6494         }
6495       else if (GET_CODE (x) == MEM)
6496         {
6497           output_memory_reference_mode = GET_MODE (x);
6498           output_address (XEXP (x, 0));
6499         }
6500       else if (GET_CODE (x) == CONST_DOUBLE)
6501         fprintf (stream, "#%s", fp_immediate_constant (x));
6502       else if (GET_CODE (x) == NEG)
6503         abort (); /* This should never happen now. */
6504       else
6505         {
6506           fputc ('#', stream);
6507           output_addr_const (stream, x);
6508         }
6509     }
6510 }
6511 \f
6512 /* A finite state machine takes care of noticing whether or not instructions
6513    can be conditionally executed, and thus decrease execution time and code
6514    size by deleting branch instructions.  The fsm is controlled by
6515    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
6516
6517 /* The state of the fsm controlling condition codes are:
6518    0: normal, do nothing special
6519    1: make ASM_OUTPUT_OPCODE not output this instruction
6520    2: make ASM_OUTPUT_OPCODE not output this instruction
6521    3: make instructions conditional
6522    4: make instructions conditional
6523
6524    State transitions (state->state by whom under condition):
6525    0 -> 1 final_prescan_insn if the `target' is a label
6526    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
6527    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
6528    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
6529    3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
6530           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
6531    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
6532           (the target insn is arm_target_insn).
6533
6534    If the jump clobbers the conditions then we use states 2 and 4.
6535
6536    A similar thing can be done with conditional return insns.
6537
6538    XXX In case the `target' is an unconditional branch, this conditionalising
6539    of the instructions always reduces code size, but not always execution
6540    time.  But then, I want to reduce the code size to somewhere near what
6541    /bin/cc produces.  */
6542
6543 /* Returns the index of the ARM condition code string in
6544    `arm_condition_codes'.  COMPARISON should be an rtx like
6545    `(eq (...) (...))'.  */
6546
6547 static enum arm_cond_code
6548 get_arm_condition_code (comparison)
6549      rtx comparison;
6550 {
6551   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
6552   register int code;
6553   register enum rtx_code comp_code = GET_CODE (comparison);
6554
6555   if (GET_MODE_CLASS (mode) != MODE_CC)
6556     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
6557                            XEXP (comparison, 1));
6558
6559   switch (mode)
6560     {
6561     case CC_DNEmode: code = ARM_NE; goto dominance;
6562     case CC_DEQmode: code = ARM_EQ; goto dominance;
6563     case CC_DGEmode: code = ARM_GE; goto dominance;
6564     case CC_DGTmode: code = ARM_GT; goto dominance;
6565     case CC_DLEmode: code = ARM_LE; goto dominance;
6566     case CC_DLTmode: code = ARM_LT; goto dominance;
6567     case CC_DGEUmode: code = ARM_CS; goto dominance;
6568     case CC_DGTUmode: code = ARM_HI; goto dominance;
6569     case CC_DLEUmode: code = ARM_LS; goto dominance;
6570     case CC_DLTUmode: code = ARM_CC;
6571
6572     dominance:
6573       if (comp_code != EQ && comp_code != NE)
6574         abort ();
6575
6576       if (comp_code == EQ)
6577         return ARM_INVERSE_CONDITION_CODE (code);
6578       return code;
6579
6580     case CC_NOOVmode:
6581       switch (comp_code)
6582         {
6583         case NE: return ARM_NE;
6584         case EQ: return ARM_EQ;
6585         case GE: return ARM_PL;
6586         case LT: return ARM_MI;
6587         default: abort ();
6588         }
6589
6590     case CC_Zmode:
6591     case CCFPmode:
6592       switch (comp_code)
6593         {
6594         case NE: return ARM_NE;
6595         case EQ: return ARM_EQ;
6596         default: abort ();
6597         }
6598
6599     case CCFPEmode:
6600       switch (comp_code)
6601         {
6602         case GE: return ARM_GE;
6603         case GT: return ARM_GT;
6604         case LE: return ARM_LS;
6605         case LT: return ARM_MI;
6606         default: abort ();
6607         }
6608
6609     case CC_SWPmode:
6610       switch (comp_code)
6611         {
6612         case NE: return ARM_NE;
6613         case EQ: return ARM_EQ;
6614         case GE: return ARM_LE;
6615         case GT: return ARM_LT;
6616         case LE: return ARM_GE;
6617         case LT: return ARM_GT;
6618         case GEU: return ARM_LS;
6619         case GTU: return ARM_CC;
6620         case LEU: return ARM_CS;
6621         case LTU: return ARM_HI;
6622         default: abort ();
6623         }
6624
6625     case CC_Cmode:
6626       switch (comp_code)
6627       {
6628       case LTU: return ARM_CS;
6629       case GEU: return ARM_CC;
6630       default: abort ();
6631       }
6632       
6633     case CCmode:
6634       switch (comp_code)
6635         {
6636         case NE: return ARM_NE;
6637         case EQ: return ARM_EQ;
6638         case GE: return ARM_GE;
6639         case GT: return ARM_GT;
6640         case LE: return ARM_LE;
6641         case LT: return ARM_LT;
6642         case GEU: return ARM_CS;
6643         case GTU: return ARM_HI;
6644         case LEU: return ARM_LS;
6645         case LTU: return ARM_CC;
6646         default: abort ();
6647         }
6648
6649     default: abort ();
6650     }
6651
6652   abort ();
6653 }
6654
6655
6656 void
6657 arm_final_prescan_insn (insn)
6658      rtx insn;
6659 {
6660   /* BODY will hold the body of INSN.  */
6661   register rtx body = PATTERN (insn);
6662
6663   /* This will be 1 if trying to repeat the trick, and things need to be
6664      reversed if it appears to fail.  */
6665   int reverse = 0;
6666
6667   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
6668      taken are clobbered, even if the rtl suggests otherwise.  It also
6669      means that we have to grub around within the jump expression to find
6670      out what the conditions are when the jump isn't taken.  */
6671   int jump_clobbers = 0;
6672   
6673   /* If we start with a return insn, we only succeed if we find another one. */
6674   int seeking_return = 0;
6675   
6676   /* START_INSN will hold the insn from where we start looking.  This is the
6677      first insn after the following code_label if REVERSE is true.  */
6678   rtx start_insn = insn;
6679
6680   /* If in state 4, check if the target branch is reached, in order to
6681      change back to state 0.  */
6682   if (arm_ccfsm_state == 4)
6683     {
6684       if (insn == arm_target_insn)
6685         {
6686           arm_target_insn = NULL;
6687           arm_ccfsm_state = 0;
6688         }
6689       return;
6690     }
6691
6692   /* If in state 3, it is possible to repeat the trick, if this insn is an
6693      unconditional branch to a label, and immediately following this branch
6694      is the previous target label which is only used once, and the label this
6695      branch jumps to is not too far off.  */
6696   if (arm_ccfsm_state == 3)
6697     {
6698       if (simplejump_p (insn))
6699         {
6700           start_insn = next_nonnote_insn (start_insn);
6701           if (GET_CODE (start_insn) == BARRIER)
6702             {
6703               /* XXX Isn't this always a barrier?  */
6704               start_insn = next_nonnote_insn (start_insn);
6705             }
6706           if (GET_CODE (start_insn) == CODE_LABEL
6707               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
6708               && LABEL_NUSES (start_insn) == 1)
6709             reverse = TRUE;
6710           else
6711             return;
6712         }
6713       else if (GET_CODE (body) == RETURN)
6714         {
6715           start_insn = next_nonnote_insn (start_insn);
6716           if (GET_CODE (start_insn) == BARRIER)
6717             start_insn = next_nonnote_insn (start_insn);
6718           if (GET_CODE (start_insn) == CODE_LABEL
6719               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
6720               && LABEL_NUSES (start_insn) == 1)
6721             {
6722               reverse = TRUE;
6723               seeking_return = 1;
6724             }
6725           else
6726             return;
6727         }
6728       else
6729         return;
6730     }
6731
6732   if (arm_ccfsm_state != 0 && !reverse)
6733     abort ();
6734   if (GET_CODE (insn) != JUMP_INSN)
6735     return;
6736
6737   /* This jump might be paralleled with a clobber of the condition codes 
6738      the jump should always come first */
6739   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6740     body = XVECEXP (body, 0, 0);
6741
6742 #if 0  
6743   /* If this is a conditional return then we don't want to know */
6744   if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
6745       && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
6746       && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
6747           || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
6748     return;
6749 #endif
6750
6751   if (reverse
6752       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
6753           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
6754     {
6755       int insns_skipped;
6756       int fail = FALSE, succeed = FALSE;
6757       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
6758       int then_not_else = TRUE;
6759       rtx this_insn = start_insn, label = 0;
6760
6761       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
6762         {
6763           /* The code below is wrong for these, and I haven't time to
6764              fix it now.  So we just do the safe thing and return.  This
6765              whole function needs re-writing anyway.  */
6766           jump_clobbers = 1;
6767           return;
6768         }
6769       
6770       /* Register the insn jumped to.  */
6771       if (reverse)
6772         {
6773           if (!seeking_return)
6774             label = XEXP (SET_SRC (body), 0);
6775         }
6776       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
6777         label = XEXP (XEXP (SET_SRC (body), 1), 0);
6778       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
6779         {
6780           label = XEXP (XEXP (SET_SRC (body), 2), 0);
6781           then_not_else = FALSE;
6782         }
6783       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
6784         seeking_return = 1;
6785       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
6786         {
6787           seeking_return = 1;
6788           then_not_else = FALSE;
6789         }
6790       else
6791         abort ();
6792
6793       /* See how many insns this branch skips, and what kind of insns.  If all
6794          insns are okay, and the label or unconditional branch to the same
6795          label is not too far away, succeed.  */
6796       for (insns_skipped = 0;
6797            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
6798         {
6799           rtx scanbody;
6800
6801           this_insn = next_nonnote_insn (this_insn);
6802           if (!this_insn)
6803             break;
6804
6805           switch (GET_CODE (this_insn))
6806             {
6807             case CODE_LABEL:
6808               /* Succeed if it is the target label, otherwise fail since
6809                  control falls in from somewhere else.  */
6810               if (this_insn == label)
6811                 {
6812                   if (jump_clobbers)
6813                     {
6814                       arm_ccfsm_state = 2;
6815                       this_insn = next_nonnote_insn (this_insn);
6816                     }
6817                   else
6818                     arm_ccfsm_state = 1;
6819                   succeed = TRUE;
6820                 }
6821               else
6822                 fail = TRUE;
6823               break;
6824
6825             case BARRIER:
6826               /* Succeed if the following insn is the target label.
6827                  Otherwise fail.  
6828                  If return insns are used then the last insn in a function 
6829                  will be a barrier. */
6830               this_insn = next_nonnote_insn (this_insn);
6831               if (this_insn && this_insn == label)
6832                 {
6833                   if (jump_clobbers)
6834                     {
6835                       arm_ccfsm_state = 2;
6836                       this_insn = next_nonnote_insn (this_insn);
6837                     }
6838                   else
6839                     arm_ccfsm_state = 1;
6840                   succeed = TRUE;
6841                 }
6842               else
6843                 fail = TRUE;
6844               break;
6845
6846             case CALL_INSN:
6847               /* If using 32-bit addresses the cc is not preserved over
6848                  calls.  */
6849               if (TARGET_APCS_32)
6850                 {
6851                   /* Succeed if the following insn is the target label,
6852                      or if the following two insns are a barrier and
6853                      the target label.  */
6854                   this_insn = next_nonnote_insn (this_insn);
6855                   if (this_insn && GET_CODE (this_insn) == BARRIER)
6856                     this_insn = next_nonnote_insn (this_insn);
6857
6858                   if (this_insn && this_insn == label
6859                       && insns_skipped < max_insns_skipped)
6860                     {
6861                       if (jump_clobbers)
6862                         {
6863                           arm_ccfsm_state = 2;
6864                           this_insn = next_nonnote_insn (this_insn);
6865                         }
6866                       else
6867                         arm_ccfsm_state = 1;
6868                       succeed = TRUE;
6869                     }
6870                   else
6871                     fail = TRUE;
6872                 }
6873               break;
6874
6875             case JUMP_INSN:
6876               /* If this is an unconditional branch to the same label, succeed.
6877                  If it is to another label, do nothing.  If it is conditional,
6878                  fail.  */
6879               /* XXX Probably, the tests for SET and the PC are unnecessary.  */
6880
6881               scanbody = PATTERN (this_insn);
6882               if (GET_CODE (scanbody) == SET
6883                   && GET_CODE (SET_DEST (scanbody)) == PC)
6884                 {
6885                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
6886                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
6887                     {
6888                       arm_ccfsm_state = 2;
6889                       succeed = TRUE;
6890                     }
6891                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
6892                     fail = TRUE;
6893                 }
6894               /* Fail if a conditional return is undesirable (eg on a
6895                  StrongARM), but still allow this if optimizing for size.  */
6896               else if (GET_CODE (scanbody) == RETURN
6897                        && ! use_return_insn (TRUE)
6898                        && ! optimize_size)
6899                 fail = TRUE;
6900               else if (GET_CODE (scanbody) == RETURN
6901                        && seeking_return)
6902                 {
6903                   arm_ccfsm_state = 2;
6904                   succeed = TRUE;
6905                 }
6906               else if (GET_CODE (scanbody) == PARALLEL)
6907                 {
6908                   switch (get_attr_conds (this_insn))
6909                     {
6910                     case CONDS_NOCOND:
6911                       break;
6912                     default:
6913                       fail = TRUE;
6914                       break;
6915                     }
6916                 }
6917               break;
6918
6919             case INSN:
6920               /* Instructions using or affecting the condition codes make it
6921                  fail.  */
6922               scanbody = PATTERN (this_insn);
6923               if (! (GET_CODE (scanbody) == SET
6924                      || GET_CODE (scanbody) == PARALLEL)
6925                   || get_attr_conds (this_insn) != CONDS_NOCOND)
6926                 fail = TRUE;
6927               break;
6928
6929             default:
6930               break;
6931             }
6932         }
6933       if (succeed)
6934         {
6935           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
6936             arm_target_label = CODE_LABEL_NUMBER (label);
6937           else if (seeking_return || arm_ccfsm_state == 2)
6938             {
6939               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
6940                 {
6941                   this_insn = next_nonnote_insn (this_insn);
6942                   if (this_insn && (GET_CODE (this_insn) == BARRIER
6943                                     || GET_CODE (this_insn) == CODE_LABEL))
6944                     abort ();
6945                 }
6946               if (!this_insn)
6947                 {
6948                   /* Oh, dear! we ran off the end.. give up */
6949                   recog (PATTERN (insn), insn, NULL_PTR);
6950                   arm_ccfsm_state = 0;
6951                   arm_target_insn = NULL;
6952                   return;
6953                 }
6954               arm_target_insn = this_insn;
6955             }
6956           else
6957             abort ();
6958           if (jump_clobbers)
6959             {
6960               if (reverse)
6961                 abort ();
6962               arm_current_cc = 
6963                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
6964                                                             0), 0), 1));
6965               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
6966                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6967               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
6968                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6969             }
6970           else
6971             {
6972               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
6973                  what it was.  */
6974               if (!reverse)
6975                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
6976                                                                0));
6977             }
6978
6979           if (reverse || then_not_else)
6980             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
6981         }
6982
6983       /* Restore recog_data (getting the attributes of other insns can
6984          destroy this array, but final.c assumes that it remains intact
6985          across this call; since the insn has been recognized already we
6986          call recog direct).  */
6987       recog (PATTERN (insn), insn, NULL_PTR);
6988     }
6989 }
6990
6991 #ifdef AOF_ASSEMBLER
6992 /* Special functions only needed when producing AOF syntax assembler. */
6993
6994 rtx aof_pic_label = NULL_RTX;
6995 struct pic_chain
6996 {
6997   struct pic_chain * next;
6998   char * symname;
6999 };
7000
7001 static struct pic_chain * aof_pic_chain = NULL;
7002
7003 rtx
7004 aof_pic_entry (x)
7005      rtx x;
7006 {
7007   struct pic_chain ** chainp;
7008   int offset;
7009
7010   if (aof_pic_label == NULL_RTX)
7011     {
7012       /* We mark this here and not in arm_add_gc_roots() to avoid
7013          polluting even more code with ifdefs, and because it never
7014          contains anything useful until we assign to it here.  */
7015       ggc_add_rtx_root (&aof_pic_label, 1);
7016       /* This needs to persist throughout the compilation.  */
7017       end_temporary_allocation ();
7018       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
7019       resume_temporary_allocation ();
7020     }
7021
7022   for (offset = 0, chainp = &aof_pic_chain; *chainp;
7023        offset += 4, chainp = &(*chainp)->next)
7024     if ((*chainp)->symname == XSTR (x, 0))
7025       return plus_constant (aof_pic_label, offset);
7026
7027   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
7028   (*chainp)->next = NULL;
7029   (*chainp)->symname = XSTR (x, 0);
7030   return plus_constant (aof_pic_label, offset);
7031 }
7032
7033 void
7034 aof_dump_pic_table (f)
7035      FILE * f;
7036 {
7037   struct pic_chain * chain;
7038
7039   if (aof_pic_chain == NULL)
7040     return;
7041
7042   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
7043                PIC_OFFSET_TABLE_REGNUM,
7044                PIC_OFFSET_TABLE_REGNUM);
7045   fputs ("|x$adcons|\n", f);
7046   
7047   for (chain = aof_pic_chain; chain; chain = chain->next)
7048     {
7049       fputs ("\tDCD\t", f);
7050       assemble_name (f, chain->symname);
7051       fputs ("\n", f);
7052     }
7053 }
7054
7055 int arm_text_section_count = 1;
7056
7057 char *
7058 aof_text_section ()
7059 {
7060   static char buf[100];
7061   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
7062            arm_text_section_count++);
7063   if (flag_pic)
7064     strcat (buf, ", PIC, REENTRANT");
7065   return buf;
7066 }
7067
7068 static int arm_data_section_count = 1;
7069
7070 char *
7071 aof_data_section ()
7072 {
7073   static char buf[100];
7074   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
7075   return buf;
7076 }
7077
7078 /* The AOF assembler is religiously strict about declarations of
7079    imported and exported symbols, so that it is impossible to declare
7080    a function as imported near the beginning of the file, and then to
7081    export it later on.  It is, however, possible to delay the decision
7082    until all the functions in the file have been compiled.  To get
7083    around this, we maintain a list of the imports and exports, and
7084    delete from it any that are subsequently defined.  At the end of
7085    compilation we spit the remainder of the list out before the END
7086    directive.  */
7087
7088 struct import
7089 {
7090   struct import * next;
7091   char * name;
7092 };
7093
7094 static struct import * imports_list = NULL;
7095
7096 void
7097 aof_add_import (name)
7098      char * name;
7099 {
7100   struct import * new;
7101
7102   for (new = imports_list; new; new = new->next)
7103     if (new->name == name)
7104       return;
7105
7106   new = (struct import *) xmalloc (sizeof (struct import));
7107   new->next = imports_list;
7108   imports_list = new;
7109   new->name = name;
7110 }
7111
7112 void
7113 aof_delete_import (name)
7114      char * name;
7115 {
7116   struct import ** old;
7117
7118   for (old = &imports_list; *old; old = & (*old)->next)
7119     {
7120       if ((*old)->name == name)
7121         {
7122           *old = (*old)->next;
7123           return;
7124         }
7125     }
7126 }
7127
7128 int arm_main_function = 0;
7129
7130 void
7131 aof_dump_imports (f)
7132      FILE * f;
7133 {
7134   /* The AOF assembler needs this to cause the startup code to be extracted
7135      from the library.  Brining in __main causes the whole thing to work
7136      automagically.  */
7137   if (arm_main_function)
7138     {
7139       text_section ();
7140       fputs ("\tIMPORT __main\n", f);
7141       fputs ("\tDCD __main\n", f);
7142     }
7143
7144   /* Now dump the remaining imports.  */
7145   while (imports_list)
7146     {
7147       fprintf (f, "\tIMPORT\t");
7148       assemble_name (f, imports_list->name);
7149       fputc ('\n', f);
7150       imports_list = imports_list->next;
7151     }
7152 }
7153 #endif /* AOF_ASSEMBLER */