OSDN Git Service

remove extraneous code checked in with previous delta
[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 "tm_p.h"
44
45 #ifndef Mmode
46 #define Mmode enum machine_mode
47 #endif
48
49 /* Some function declarations.  */
50 static HOST_WIDE_INT int_log2           PARAMS ((HOST_WIDE_INT));
51 static char * output_multi_immediate    PARAMS ((rtx *, char *, char *, int, HOST_WIDE_INT));
52 static int arm_gen_constant             PARAMS ((enum rtx_code, Mmode, HOST_WIDE_INT, rtx, rtx, int, int));
53 static int arm_naked_function_p         PARAMS ((tree));
54 static void init_fpa_table              PARAMS ((void));
55 static enum machine_mode select_dominance_cc_mode PARAMS ((rtx, rtx, HOST_WIDE_INT));
56 static HOST_WIDE_INT add_minipool_constant PARAMS ((rtx, Mmode));
57 static void dump_minipool               PARAMS ((rtx));
58 static rtx find_barrier                 PARAMS ((rtx, int));
59 static void push_minipool_fix           PARAMS ((rtx, int, rtx *, Mmode, rtx));
60 static void push_minipool_barrier       PARAMS ((rtx, int));
61 static void note_invalid_constants      PARAMS ((rtx, int));
62 static char * fp_const_from_val         PARAMS ((REAL_VALUE_TYPE *));
63 static int eliminate_lr2ip              PARAMS ((rtx *));
64 static char * shift_op                  PARAMS ((rtx, HOST_WIDE_INT *));
65 static int pattern_really_clobbers_lr   PARAMS ((rtx));
66 static int function_really_clobbers_lr  PARAMS ((rtx));
67 static void emit_multi_reg_push         PARAMS ((int));
68 static void emit_sfm                    PARAMS ((int, int));
69 static enum arm_cond_code get_arm_condition_code PARAMS ((rtx));
70 static int const_ok_for_op              PARAMS ((HOST_WIDE_INT, enum rtx_code));
71 static void arm_add_gc_roots            PARAMS ((void));
72
73 /* The maximum number of insns skipped which will be conditionalised if
74    possible.  */
75 static int max_insns_skipped = 5;
76
77 extern FILE * asm_out_file;
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     /* All simple types are returned in registers.  */
1397     return 0;
1398
1399   if (int_size_in_bytes (type) > 4)
1400     /* All structures/unions bigger than one word are returned in memory.  */
1401     return 1;
1402
1403   if (TREE_CODE (type) == RECORD_TYPE)
1404     {
1405       tree field;
1406
1407       /* For a struct the APCS says that we only return in a register
1408          if the type is 'integer like' and every addressable element
1409          has an offset of zero.  For practical purposes this means
1410          that the structure can have at most one non bit-field element
1411          and that this element must be the first one in the structure.  */
1412       
1413       /* Find the first field, ignoring non FIELD_DECL things which will
1414          have been created by C++.  */
1415       for (field = TYPE_FIELDS (type);
1416            field && TREE_CODE (field) != FIELD_DECL;
1417            field = TREE_CHAIN (field))
1418         continue;
1419       
1420       if (field == NULL)
1421         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
1422
1423       /* Check that the first field is valid for returning in a register...  */
1424       if (FLOAT_TYPE_P (TREE_TYPE (field)))
1425         return 1;
1426
1427       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1428         return 1;
1429
1430       /* Now check the remaining fields, if any.  Only bitfields are allowed,
1431          since they are not addressable.  */
1432       for (field = TREE_CHAIN (field);
1433            field;
1434            field = TREE_CHAIN (field))
1435         {
1436           if (TREE_CODE (field) != FIELD_DECL)
1437             continue;
1438           
1439           if (! DECL_BIT_FIELD_TYPE (field))
1440             return 1;
1441         }
1442
1443       return 0;
1444     }
1445   
1446   if (TREE_CODE (type) == UNION_TYPE)
1447     {
1448       tree field;
1449
1450       /* Unions can be returned in registers if every element is
1451          integral, or can be returned in an integer register.  */
1452       for (field = TYPE_FIELDS (type);
1453            field;
1454            field = TREE_CHAIN (field))
1455         {
1456           if (TREE_CODE (field) != FIELD_DECL)
1457             continue;
1458
1459           if (FLOAT_TYPE_P (TREE_TYPE (field)))
1460             return 1;
1461           
1462           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1463             return 1;
1464         }
1465       
1466       return 0;
1467     }
1468   
1469   /* XXX Not sure what should be done for
1470      other aggregates so put them in memory.  */
1471   return 1;
1472 }
1473
1474 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1475    for a call to a function whose data type is FNTYPE.
1476    For a library call, FNTYPE is NULL.  */
1477 void
1478 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1479      CUMULATIVE_ARGS * pcum;
1480      tree fntype;
1481      rtx libname  ATTRIBUTE_UNUSED;
1482      int indirect ATTRIBUTE_UNUSED;
1483 {
1484   /* On the ARM, the offset starts at 0.  */
1485   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype))) ? 1 : 0);
1486   
1487   pcum->call_cookie = CALL_NORMAL;
1488
1489   if (TARGET_LONG_CALLS)
1490     pcum->call_cookie = CALL_LONG;
1491     
1492   /* Check for long call/short call attributes.  The attributes
1493      override any command line option.  */
1494   if (fntype)
1495     {
1496       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1497         pcum->call_cookie = CALL_SHORT;
1498       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1499         pcum->call_cookie = CALL_LONG;
1500     }
1501 }
1502
1503 /* Determine where to put an argument to a function.
1504    Value is zero to push the argument on the stack,
1505    or a hard register in which to store the argument.
1506
1507    MODE is the argument's machine mode.
1508    TYPE is the data type of the argument (as a tree).
1509     This is null for libcalls where that information may
1510     not be available.
1511    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1512     the preceding args and about the function being called.
1513    NAMED is nonzero if this argument is a named parameter
1514     (otherwise it is an extra parameter matching an ellipsis).  */
1515 rtx
1516 arm_function_arg (pcum, mode, type, named)
1517      CUMULATIVE_ARGS * pcum;
1518      enum machine_mode mode;
1519      tree type ATTRIBUTE_UNUSED;
1520      int named;
1521 {
1522   if (mode == VOIDmode)
1523     /* Compute operand 2 of the call insn.  */
1524     return GEN_INT (pcum->call_cookie);
1525   
1526   if (! named || pcum->nregs >= NUM_ARG_REGS)
1527     return NULL_RTX;
1528   
1529   return gen_rtx_REG (mode, pcum->nregs);
1530 }
1531 \f
1532 /* Encode the current state of the #pragma [no_]long_calls.  */
1533 typedef enum
1534 {
1535   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
1536   LONG,         /* #pragma long_calls is in effect.  */
1537   SHORT         /* #pragma no_long_calls is in effect.  */
1538 } arm_pragma_enum;
1539
1540 static arm_pragma_enum arm_pragma_long_calls = OFF;
1541
1542 /* Handle pragmas for compatibility with Intel's compilers.
1543    FIXME: This is incomplete, since it does not handle all
1544    the pragmas that the Intel compilers understand.  */
1545 int
1546 arm_process_pragma (p_getc, p_ungetc, pname)
1547      int (*  p_getc)   PARAMS ((void)) ATTRIBUTE_UNUSED;
1548      void (* p_ungetc) PARAMS ((int))  ATTRIBUTE_UNUSED;
1549      char *  pname;
1550 {
1551   /* Should be pragma 'far' or equivalent for callx/balx here.  */
1552   if (strcmp (pname, "long_calls") == 0)
1553     arm_pragma_long_calls = LONG;
1554   else if (strcmp (pname, "no_long_calls") == 0)
1555     arm_pragma_long_calls = SHORT;
1556   else if (strcmp (pname, "long_calls_off") == 0)
1557     arm_pragma_long_calls = OFF;
1558   else
1559     return 0;
1560   
1561   return 1;
1562 }
1563 \f
1564 /* Return nonzero if IDENTIFIER with arguments ARGS is a valid machine specific
1565    attribute for TYPE.  The attributes in ATTRIBUTES have previously been
1566    assigned to TYPE.  */
1567 int
1568 arm_valid_type_attribute_p (type, attributes, identifier, args)
1569      tree type;
1570      tree attributes ATTRIBUTE_UNUSED;
1571      tree identifier;
1572      tree args;
1573 {
1574   if (   TREE_CODE (type) != FUNCTION_TYPE
1575       && TREE_CODE (type) != METHOD_TYPE
1576       && TREE_CODE (type) != FIELD_DECL
1577       && TREE_CODE (type) != TYPE_DECL)
1578     return 0;
1579
1580   /* Function calls made to this symbol must be done indirectly, because
1581      it may lie outside of the 26 bit addressing range of a normal function
1582      call.  */
1583   if (is_attribute_p ("long_call", identifier))
1584     return (args == NULL_TREE);
1585   
1586   /* Whereas these functions are always known to reside within the 26 bit
1587      addressing range.  */
1588   if (is_attribute_p ("short_call", identifier))
1589     return (args == NULL_TREE);
1590   
1591   return 0;
1592 }
1593
1594 /* Return 0 if the attributes for two types are incompatible, 1 if they
1595    are compatible, and 2 if they are nearly compatible (which causes a
1596    warning to be generated).  */
1597 int
1598 arm_comp_type_attributes (type1, type2)
1599      tree type1;
1600      tree type2;
1601 {
1602   int l1, l2, s1, s2;
1603   
1604   /* Check for mismatch of non-default calling convention.  */
1605   if (TREE_CODE (type1) != FUNCTION_TYPE)
1606     return 1;
1607
1608   /* Check for mismatched call attributes.  */
1609   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
1610   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
1611   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
1612   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
1613
1614   /* Only bother to check if an attribute is defined.  */
1615   if (l1 | l2 | s1 | s2)
1616     {
1617       /* If one type has an attribute, the other must have the same attribute.  */
1618       if ((l1 != l2) || (s1 != s2))
1619         return 0;
1620
1621       /* Disallow mixed attributes.  */
1622       if ((l1 & s2) || (l2 & s1))
1623         return 0;
1624     }
1625   
1626   return 1;
1627 }
1628
1629 /* Check the ARM specific attributes on the given function decl.
1630    If any of them would prevent the function from being inlined,
1631    return a tesxtual description of why not.  Otherwise return NULL.  */
1632 const char *
1633 arm_function_attribute_inlineable_p (fndecl)
1634      tree fndecl;
1635 {
1636   if (lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (fndecl)))
1637     return "naked functions cannot be inlined";
1638
1639   /* Allow functions with short_call and long_call attributes to be inlined.  */
1640   return NULL;
1641 }
1642      
1643 /*  Encode long_call or short_call attribute by prefixing
1644     symbol name in DECL with a special character FLAG.  */
1645 void
1646 arm_encode_call_attribute (decl, flag)
1647   tree decl;
1648   char flag;
1649 {
1650   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1651   int    len = strlen (str);
1652   char * newstr;
1653
1654   if (TREE_CODE (decl) != FUNCTION_DECL)
1655     return;
1656
1657   /* Do not allow weak functions to be treated as short call.  */
1658   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
1659     return;
1660   
1661   if (ggc_p)
1662     newstr = ggc_alloc_string (NULL, len + 2);
1663   else
1664     newstr = permalloc (len + 2);
1665
1666   sprintf (newstr, "%c%s", flag, str);
1667
1668   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
1669 }
1670
1671 /*  Assigns default attributes to newly defined type.  This is used to
1672     set short_call/long_call attributes for function types of
1673     functions defined inside corresponding #pragma scopes.  */
1674 void
1675 arm_set_default_type_attributes (type)
1676   tree type;
1677 {
1678   /* Add __attribute__ ((long_call)) to all functions, when
1679      inside #pragma long_calls or __attribute__ ((short_call)),
1680      when inside #pragma no_long_calls.  */
1681   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
1682     {
1683       tree type_attr_list, attr_name;
1684       type_attr_list = TYPE_ATTRIBUTES (type);
1685
1686       if (arm_pragma_long_calls == LONG)
1687         attr_name = get_identifier ("long_call");
1688       else if (arm_pragma_long_calls == SHORT)
1689         attr_name = get_identifier ("short_call");
1690       else
1691         return;
1692
1693       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
1694       TYPE_ATTRIBUTES (type) = type_attr_list;
1695     }
1696 }
1697 \f
1698 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
1699    defined within the current compilation unit.  If this caanot be
1700    determined, then 0 is returned.  */
1701 static int
1702 current_file_function_operand (sym_ref)
1703   rtx sym_ref;
1704 {
1705   /* This is a bit of a fib.  A function will have a short call flag
1706      applied to its name if it has the short call attribute, or it has
1707      already been defined within the current compilation unit.  */
1708   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
1709     return 1;
1710
1711   /* The current funciton is always defined within the current compilation
1712      unit.  if it s a weak defintion however, then this may not be the real
1713      defintion of the function, and so we have to say no.  */
1714   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
1715       && ! DECL_WEAK (current_function_decl))
1716     return 1;
1717
1718   /* We cannot make the determination - default to returning 0.  */
1719   return 0;
1720 }
1721
1722 /* Return non-zero if a 32 bit "long_call" should be generated for
1723    this call.  We generate a long_call if the function:
1724
1725         a.  has an __attribute__((long call))
1726      or b.  is within the scope of a #pragma long_calls
1727      or c.  the -mlong-calls command line switch has been specified
1728
1729    However we do not generate a long call if the function:
1730    
1731         d.  has an __attribute__ ((short_call))
1732      or e.  is inside the scope of a #pragma no_long_calls
1733      or f.  has an __attribute__ ((section))
1734      or g.  is defined within the current compilation unit.
1735    
1736    This function will be called by C fragments contained in the machine
1737    description file.  CALL_REF and CALL_COOKIE correspond to the matched
1738    rtl operands.  CALL_SYMBOL is used to distinguish between
1739    two different callers of the function.  It is set to 1 in the
1740    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
1741    and "call_value" patterns.  This is because of the difference in the
1742    SYM_REFs passed by these patterns.  */
1743 int
1744 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
1745   rtx sym_ref;
1746   int call_cookie;
1747   int call_symbol;
1748 {
1749   if (! call_symbol)
1750     {
1751       if (GET_CODE (sym_ref) != MEM)
1752         return 0;
1753
1754       sym_ref = XEXP (sym_ref, 0);
1755     }
1756
1757   if (GET_CODE (sym_ref) != SYMBOL_REF)
1758     return 0;
1759
1760   if (call_cookie & CALL_SHORT)
1761     return 0;
1762
1763   if (TARGET_LONG_CALLS && flag_function_sections)
1764     return 1;
1765   
1766   if (current_file_function_operand (sym_ref, VOIDmode))
1767     return 0;
1768   
1769   return (call_cookie & CALL_LONG)
1770     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
1771     || TARGET_LONG_CALLS;
1772 }
1773 \f
1774 int
1775 legitimate_pic_operand_p (x)
1776      rtx x;
1777 {
1778   if (CONSTANT_P (x) && flag_pic
1779       && (GET_CODE (x) == SYMBOL_REF
1780           || (GET_CODE (x) == CONST
1781               && GET_CODE (XEXP (x, 0)) == PLUS
1782               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
1783     return 0;
1784
1785   return 1;
1786 }
1787
1788 rtx
1789 legitimize_pic_address (orig, mode, reg)
1790      rtx orig;
1791      enum machine_mode mode;
1792      rtx reg;
1793 {
1794   if (GET_CODE (orig) == SYMBOL_REF)
1795     {
1796       rtx pic_ref, address;
1797       rtx insn;
1798       int subregs = 0;
1799
1800       if (reg == 0)
1801         {
1802           if (reload_in_progress || reload_completed)
1803             abort ();
1804           else
1805             reg = gen_reg_rtx (Pmode);
1806
1807           subregs = 1;
1808         }
1809
1810 #ifdef AOF_ASSEMBLER
1811       /* The AOF assembler can generate relocations for these directly, and
1812          understands that the PIC register has to be added into the offset.
1813          */
1814       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
1815 #else
1816       if (subregs)
1817         address = gen_reg_rtx (Pmode);
1818       else
1819         address = reg;
1820
1821       emit_insn (gen_pic_load_addr (address, orig));
1822
1823       pic_ref = gen_rtx_MEM (Pmode,
1824                              gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
1825                                            address));
1826       RTX_UNCHANGING_P (pic_ref) = 1;
1827       insn = emit_move_insn (reg, pic_ref);
1828 #endif
1829       current_function_uses_pic_offset_table = 1;
1830       /* Put a REG_EQUAL note on this insn, so that it can be optimized
1831          by loop.  */
1832       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
1833                                             REG_NOTES (insn));
1834       return reg;
1835     }
1836   else if (GET_CODE (orig) == CONST)
1837     {
1838       rtx base, offset;
1839
1840       if (GET_CODE (XEXP (orig, 0)) == PLUS
1841           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1842         return orig;
1843
1844       if (reg == 0)
1845         {
1846           if (reload_in_progress || reload_completed)
1847             abort ();
1848           else
1849             reg = gen_reg_rtx (Pmode);
1850         }
1851
1852       if (GET_CODE (XEXP (orig, 0)) == PLUS)
1853         {
1854           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1855           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1856                                            base == reg ? 0 : reg);
1857         }
1858       else
1859         abort ();
1860
1861       if (GET_CODE (offset) == CONST_INT)
1862         {
1863           /* The base register doesn't really matter, we only want to
1864              test the index for the appropriate mode.  */
1865           GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
1866
1867           if (! reload_in_progress && ! reload_completed)
1868             offset = force_reg (Pmode, offset);
1869           else
1870             abort ();
1871
1872         win:
1873           if (GET_CODE (offset) == CONST_INT)
1874             return plus_constant_for_output (base, INTVAL (offset));
1875         }
1876
1877       if (GET_MODE_SIZE (mode) > 4
1878           && (GET_MODE_CLASS (mode) == MODE_INT
1879               || TARGET_SOFT_FLOAT))
1880         {
1881           emit_insn (gen_addsi3 (reg, base, offset));
1882           return reg;
1883         }
1884
1885       return gen_rtx_PLUS (Pmode, base, offset);
1886     }
1887   else if (GET_CODE (orig) == LABEL_REF)
1888     {
1889       current_function_uses_pic_offset_table = 1;
1890       
1891       if (NEED_GOT_RELOC)
1892         {
1893           rtx pic_ref, address = gen_reg_rtx (Pmode);
1894           
1895           emit_insn (gen_pic_load_addr (address, orig));
1896           pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
1897           
1898           emit_move_insn (address, pic_ref);
1899           return address;
1900         }
1901     }
1902
1903   return orig;
1904 }
1905
1906 static rtx pic_rtx;
1907
1908 int
1909 is_pic (x)
1910      rtx x;
1911 {
1912   if (x == pic_rtx)
1913     return 1;
1914   return 0;
1915 }
1916
1917 void
1918 arm_finalize_pic ()
1919 {
1920 #ifndef AOF_ASSEMBLER
1921   rtx l1, pic_tmp, pic_tmp2, seq;
1922   rtx global_offset_table;
1923
1924   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
1925     return;
1926
1927   if (! flag_pic)
1928     abort ();
1929
1930   start_sequence ();
1931   l1 = gen_label_rtx ();
1932
1933   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1934   /* On the ARM the PC register contains 'dot + 8' at the time of the
1935      addition.  */
1936   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), 8);
1937   if (GOT_PCREL)
1938     pic_tmp2 = gen_rtx_CONST (VOIDmode,
1939                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
1940   else
1941     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
1942
1943   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
1944   
1945   emit_insn (gen_pic_load_addr (pic_offset_table_rtx, pic_rtx));
1946   emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
1947
1948   seq = gen_sequence ();
1949   end_sequence ();
1950   emit_insn_after (seq, get_insns ());
1951
1952   /* Need to emit this whether or not we obey regdecls,
1953      since setjmp/longjmp can cause life info to screw up.  */
1954   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
1955 #endif /* AOF_ASSEMBLER */
1956 }
1957
1958 #define REG_OR_SUBREG_REG(X)                                            \
1959   (GET_CODE (X) == REG                                                  \
1960    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
1961
1962 #define REG_OR_SUBREG_RTX(X)                    \
1963    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
1964
1965 #define ARM_FRAME_RTX(X)                                \
1966   ((X) == frame_pointer_rtx || (X) == stack_pointer_rtx \
1967    || (X) == arg_pointer_rtx)
1968
1969 int
1970 arm_rtx_costs (x, code)
1971      rtx x;
1972      enum rtx_code code;
1973 {
1974   enum machine_mode mode = GET_MODE (x);
1975   enum rtx_code subcode;
1976   int extra_cost;
1977
1978   switch (code)
1979     {
1980     case MEM:
1981       /* Memory costs quite a lot for the first word, but subsequent words
1982          load at the equivalent of a single insn each.  */
1983       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
1984               + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
1985
1986     case DIV:
1987     case MOD:
1988       return 100;
1989
1990     case ROTATE:
1991       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
1992         return 4;
1993       /* Fall through */
1994     case ROTATERT:
1995       if (mode != SImode)
1996         return 8;
1997       /* Fall through */
1998     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
1999       if (mode == DImode)
2000         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
2001                 + ((GET_CODE (XEXP (x, 0)) == REG 
2002                     || (GET_CODE (XEXP (x, 0)) == SUBREG
2003                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2004                    ? 0 : 8));
2005       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
2006                     || (GET_CODE (XEXP (x, 0)) == SUBREG
2007                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2008                    ? 0 : 4)
2009               + ((GET_CODE (XEXP (x, 1)) == REG
2010                   || (GET_CODE (XEXP (x, 1)) == SUBREG
2011                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
2012                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
2013                  ? 0 : 4));
2014
2015     case MINUS:
2016       if (mode == DImode)
2017         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
2018                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2019                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
2020                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
2021                    ? 0 : 8));
2022
2023       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2024         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2025                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2026                           && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2027                      ? 0 : 8)
2028                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2029                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
2030                         && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
2031                    ? 0 : 8));
2032
2033       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
2034             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
2035             && REG_OR_SUBREG_REG (XEXP (x, 1))))
2036           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
2037                || subcode == ASHIFTRT || subcode == LSHIFTRT
2038                || subcode == ROTATE || subcode == ROTATERT
2039                || (subcode == MULT
2040                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
2041                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
2042                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
2043               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
2044               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
2045                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
2046               && REG_OR_SUBREG_REG (XEXP (x, 0))))
2047         return 1;
2048       /* Fall through */
2049
2050     case PLUS: 
2051       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2052         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2053                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2054                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2055                         && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2056                    ? 0 : 8));
2057
2058       /* Fall through */
2059     case AND: case XOR: case IOR: 
2060       extra_cost = 0;
2061
2062       /* Normally the frame registers will be spilt into reg+const during
2063          reload, so it is a bad idea to combine them with other instructions,
2064          since then they might not be moved outside of loops.  As a compromise
2065          we allow integration with ops that have a constant as their second
2066          operand.  */
2067       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
2068            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
2069            && GET_CODE (XEXP (x, 1)) != CONST_INT)
2070           || (REG_OR_SUBREG_REG (XEXP (x, 0))
2071               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
2072         extra_cost = 4;
2073
2074       if (mode == DImode)
2075         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2076                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2077                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
2078                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2079                    ? 0 : 8));
2080
2081       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
2082         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
2083                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2084                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
2085                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2086                    ? 0 : 4));
2087
2088       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
2089         return (1 + extra_cost
2090                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
2091                      || subcode == LSHIFTRT || subcode == ASHIFTRT
2092                      || subcode == ROTATE || subcode == ROTATERT
2093                      || (subcode == MULT
2094                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2095                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
2096                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
2097                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
2098                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
2099                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
2100                    ? 0 : 4));
2101
2102       return 8;
2103
2104     case MULT:
2105       /* There is no point basing this on the tuning, since it is always the
2106          fast variant if it exists at all */
2107       if (arm_fast_multiply && mode == DImode
2108           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
2109           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2110               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
2111         return 8;
2112
2113       if (GET_MODE_CLASS (mode) == MODE_FLOAT
2114           || mode == DImode)
2115         return 30;
2116
2117       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2118         {
2119           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
2120                                       & (unsigned HOST_WIDE_INT) 0xffffffffUL);
2121           int add_cost = const_ok_for_arm (i) ? 4 : 8;
2122           int j;
2123           /* Tune as appropriate */ 
2124           int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
2125           
2126           for (j = 0; i && j < 32; j += booth_unit_size)
2127             {
2128               i >>= booth_unit_size;
2129               add_cost += 2;
2130             }
2131
2132           return add_cost;
2133         }
2134
2135       return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
2136               + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
2137               + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
2138
2139     case TRUNCATE:
2140       if (arm_fast_multiply && mode == SImode
2141           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
2142           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2143           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
2144               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
2145           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
2146               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
2147         return 8;
2148       return 99;
2149
2150     case NEG:
2151       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2152         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
2153       /* Fall through */
2154     case NOT:
2155       if (mode == DImode)
2156         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2157
2158       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2159
2160     case IF_THEN_ELSE:
2161       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2162         return 14;
2163       return 2;
2164
2165     case COMPARE:
2166       return 1;
2167
2168     case ABS:
2169       return 4 + (mode == DImode ? 4 : 0);
2170
2171     case SIGN_EXTEND:
2172       if (GET_MODE (XEXP (x, 0)) == QImode)
2173         return (4 + (mode == DImode ? 4 : 0)
2174                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2175       /* Fall through */
2176     case ZERO_EXTEND:
2177       switch (GET_MODE (XEXP (x, 0)))
2178         {
2179         case QImode:
2180           return (1 + (mode == DImode ? 4 : 0)
2181                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2182
2183         case HImode:
2184           return (4 + (mode == DImode ? 4 : 0)
2185                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2186
2187         case SImode:
2188           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2189
2190         default:
2191           break;
2192         }
2193       abort ();
2194
2195     default:
2196       return 99;
2197     }
2198 }
2199
2200 int
2201 arm_adjust_cost (insn, link, dep, cost)
2202      rtx insn;
2203      rtx link;
2204      rtx dep;
2205      int cost;
2206 {
2207   rtx i_pat, d_pat;
2208
2209   /* XXX This is not strictly true for the FPA. */
2210   if (REG_NOTE_KIND(link) == REG_DEP_ANTI
2211       || REG_NOTE_KIND(link) == REG_DEP_OUTPUT)
2212     return 0;
2213
2214   if ((i_pat = single_set (insn)) != NULL
2215       && GET_CODE (SET_SRC (i_pat)) == MEM
2216       && (d_pat = single_set (dep)) != NULL
2217       && GET_CODE (SET_DEST (d_pat)) == MEM)
2218     {
2219       /* This is a load after a store, there is no conflict if the load reads
2220          from a cached area.  Assume that loads from the stack, and from the
2221          constant pool are cached, and that others will miss.  This is a 
2222          hack. */
2223       
2224       if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
2225           || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2226           || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2227           || reg_mentioned_p (hard_frame_pointer_rtx, 
2228                               XEXP (SET_SRC (i_pat), 0)))
2229         return 1;
2230     }
2231
2232   return cost;
2233 }
2234
2235 /* This code has been fixed for cross compilation. */
2236
2237 static int fpa_consts_inited = 0;
2238
2239 char * strings_fpa[8] =
2240 {
2241   "0",   "1",   "2",   "3",
2242   "4",   "5",   "0.5", "10"
2243 };
2244
2245 static REAL_VALUE_TYPE values_fpa[8];
2246
2247 static void
2248 init_fpa_table ()
2249 {
2250   int i;
2251   REAL_VALUE_TYPE r;
2252
2253   for (i = 0; i < 8; i++)
2254     {
2255       r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
2256       values_fpa[i] = r;
2257     }
2258
2259   fpa_consts_inited = 1;
2260 }
2261
2262 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2263
2264 int
2265 const_double_rtx_ok_for_fpu (x)
2266      rtx x;
2267 {
2268   REAL_VALUE_TYPE r;
2269   int i;
2270   
2271   if (!fpa_consts_inited)
2272     init_fpa_table ();
2273   
2274   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2275   if (REAL_VALUE_MINUS_ZERO (r))
2276     return 0;
2277
2278   for (i = 0; i < 8; i++)
2279     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2280       return 1;
2281
2282   return 0;
2283 }
2284
2285 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2286
2287 int
2288 neg_const_double_rtx_ok_for_fpu (x)
2289      rtx x;
2290 {
2291   REAL_VALUE_TYPE r;
2292   int i;
2293   
2294   if (!fpa_consts_inited)
2295     init_fpa_table ();
2296   
2297   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2298   r = REAL_VALUE_NEGATE (r);
2299   if (REAL_VALUE_MINUS_ZERO (r))
2300     return 0;
2301
2302   for (i = 0; i < 8; i++)
2303     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2304       return 1;
2305
2306   return 0;
2307 }
2308 \f
2309 /* Predicates for `match_operand' and `match_operator'.  */
2310
2311 /* s_register_operand is the same as register_operand, but it doesn't accept
2312    (SUBREG (MEM)...).
2313
2314    This function exists because at the time it was put in it led to better
2315    code.  SUBREG(MEM) always needs a reload in the places where
2316    s_register_operand is used, and this seemed to lead to excessive
2317    reloading.  */
2318
2319 int
2320 s_register_operand (op, mode)
2321      register rtx op;
2322      enum machine_mode mode;
2323 {
2324   if (GET_MODE (op) != mode && mode != VOIDmode)
2325     return 0;
2326
2327   if (GET_CODE (op) == SUBREG)
2328     op = SUBREG_REG (op);
2329
2330   /* We don't consider registers whose class is NO_REGS
2331      to be a register operand.  */
2332   return (GET_CODE (op) == REG
2333           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2334               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2335 }
2336
2337 /* Only accept reg, subreg(reg), const_int.  */
2338
2339 int
2340 reg_or_int_operand (op, mode)
2341      register rtx op;
2342      enum machine_mode mode;
2343 {
2344   if (GET_CODE (op) == CONST_INT)
2345     return 1;
2346
2347   if (GET_MODE (op) != mode && mode != VOIDmode)
2348     return 0;
2349
2350   if (GET_CODE (op) == SUBREG)
2351     op = SUBREG_REG (op);
2352
2353   /* We don't consider registers whose class is NO_REGS
2354      to be a register operand.  */
2355   return (GET_CODE (op) == REG
2356           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2357               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2358 }
2359
2360 /* Return 1 if OP is an item in memory, given that we are in reload.  */
2361
2362 int
2363 reload_memory_operand (op, mode)
2364      rtx op;
2365      enum machine_mode mode ATTRIBUTE_UNUSED;
2366 {
2367   int regno = true_regnum (op);
2368
2369   return (! CONSTANT_P (op)
2370           && (regno == -1
2371               || (GET_CODE (op) == REG
2372                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2373 }
2374
2375 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
2376    memory access (architecture V4) */
2377 int
2378 bad_signed_byte_operand (op, mode)
2379      rtx op;
2380      enum machine_mode mode;
2381 {
2382   if (! memory_operand (op, mode) || GET_CODE (op) != MEM)
2383     return 0;
2384
2385   op = XEXP (op, 0);
2386
2387   /* A sum of anything more complex than reg + reg or reg + const is bad */
2388   if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
2389       && (! s_register_operand (XEXP (op, 0), VOIDmode)
2390           || (! s_register_operand (XEXP (op, 1), VOIDmode)
2391               && GET_CODE (XEXP (op, 1)) != CONST_INT)))
2392     return 1;
2393
2394   /* Big constants are also bad */
2395   if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
2396       && (INTVAL (XEXP (op, 1)) > 0xff
2397           || -INTVAL (XEXP (op, 1)) > 0xff))
2398     return 1;
2399
2400   /* Everything else is good, or can will automatically be made so. */
2401   return 0;
2402 }
2403
2404 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
2405
2406 int
2407 arm_rhs_operand (op, mode)
2408      rtx op;
2409      enum machine_mode mode;
2410 {
2411   return (s_register_operand (op, mode)
2412           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
2413 }
2414
2415 /* Return TRUE for valid operands for the rhs of an ARM instruction, or a load.
2416  */
2417
2418 int
2419 arm_rhsm_operand (op, mode)
2420      rtx op;
2421      enum machine_mode mode;
2422 {
2423   return (s_register_operand (op, mode)
2424           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
2425           || memory_operand (op, mode));
2426 }
2427
2428 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
2429    constant that is valid when negated.  */
2430
2431 int
2432 arm_add_operand (op, mode)
2433      rtx op;
2434      enum machine_mode mode;
2435 {
2436   return (s_register_operand (op, mode)
2437           || (GET_CODE (op) == CONST_INT
2438               && (const_ok_for_arm (INTVAL (op))
2439                   || const_ok_for_arm (-INTVAL (op)))));
2440 }
2441
2442 int
2443 arm_not_operand (op, mode)
2444      rtx op;
2445      enum machine_mode mode;
2446 {
2447   return (s_register_operand (op, mode)
2448           || (GET_CODE (op) == CONST_INT
2449               && (const_ok_for_arm (INTVAL (op))
2450                   || const_ok_for_arm (~INTVAL (op)))));
2451 }
2452
2453 /* Return TRUE if the operand is a memory reference which contains an
2454    offsettable address.  */
2455 int
2456 offsettable_memory_operand (op, mode)
2457      register rtx op;
2458      enum machine_mode mode;
2459 {
2460   if (mode == VOIDmode)
2461     mode = GET_MODE (op);
2462
2463   return (mode == GET_MODE (op)
2464           && GET_CODE (op) == MEM
2465           && offsettable_address_p (reload_completed | reload_in_progress,
2466                                     mode, XEXP (op, 0)));
2467 }
2468
2469 /* Return TRUE if the operand is a memory reference which is, or can be
2470    made word aligned by adjusting the offset.  */
2471 int
2472 alignable_memory_operand (op, mode)
2473      register rtx op;
2474      enum machine_mode mode;
2475 {
2476   rtx reg;
2477
2478   if (mode == VOIDmode)
2479     mode = GET_MODE (op);
2480
2481   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
2482     return 0;
2483
2484   op = XEXP (op, 0);
2485
2486   return ((GET_CODE (reg = op) == REG
2487            || (GET_CODE (op) == SUBREG
2488                && GET_CODE (reg = SUBREG_REG (op)) == REG)
2489            || (GET_CODE (op) == PLUS
2490                && GET_CODE (XEXP (op, 1)) == CONST_INT
2491                && (GET_CODE (reg = XEXP (op, 0)) == REG
2492                    || (GET_CODE (XEXP (op, 0)) == SUBREG
2493                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
2494           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 4);
2495 }
2496
2497 /* Similar to s_register_operand, but does not allow hard integer 
2498    registers.  */
2499 int
2500 f_register_operand (op, mode)
2501      register rtx op;
2502      enum machine_mode mode;
2503 {
2504   if (GET_MODE (op) != mode && mode != VOIDmode)
2505     return 0;
2506
2507   if (GET_CODE (op) == SUBREG)
2508     op = SUBREG_REG (op);
2509
2510   /* We don't consider registers whose class is NO_REGS
2511      to be a register operand.  */
2512   return (GET_CODE (op) == REG
2513           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2514               || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
2515 }
2516
2517 /* Return TRUE for valid operands for the rhs of an FPU instruction.  */
2518
2519 int
2520 fpu_rhs_operand (op, mode)
2521      rtx op;
2522      enum machine_mode mode;
2523 {
2524   if (s_register_operand (op, mode))
2525     return TRUE;
2526
2527   if (GET_MODE (op) != mode && mode != VOIDmode)
2528     return FALSE;
2529
2530   if (GET_CODE (op) == CONST_DOUBLE)
2531     return const_double_rtx_ok_for_fpu (op);
2532
2533   return FALSE;
2534 }
2535
2536 int
2537 fpu_add_operand (op, mode)
2538      rtx op;
2539      enum machine_mode mode;
2540 {
2541   if (s_register_operand (op, mode))
2542     return TRUE;
2543
2544   if (GET_MODE (op) != mode && mode != VOIDmode)
2545     return FALSE;
2546
2547   if (GET_CODE (op) == CONST_DOUBLE)
2548     return (const_double_rtx_ok_for_fpu (op) 
2549             || neg_const_double_rtx_ok_for_fpu (op));
2550
2551   return FALSE;
2552 }
2553
2554 /* Return nonzero if OP is a constant power of two.  */
2555
2556 int
2557 power_of_two_operand (op, mode)
2558      rtx op;
2559      enum machine_mode mode ATTRIBUTE_UNUSED;
2560 {
2561   if (GET_CODE (op) == CONST_INT)
2562     {
2563       HOST_WIDE_INT value = INTVAL(op);
2564       return value != 0  &&  (value & (value - 1)) == 0;
2565     }
2566   return FALSE;
2567 }
2568
2569 /* Return TRUE for a valid operand of a DImode operation.
2570    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2571    Note that this disallows MEM(REG+REG), but allows
2572    MEM(PRE/POST_INC/DEC(REG)).  */
2573
2574 int
2575 di_operand (op, mode)
2576      rtx op;
2577      enum machine_mode mode;
2578 {
2579   if (s_register_operand (op, mode))
2580     return TRUE;
2581
2582   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
2583     return FALSE;
2584
2585   if (GET_CODE (op) == SUBREG)
2586     op = SUBREG_REG (op);
2587
2588   switch (GET_CODE (op))
2589     {
2590     case CONST_DOUBLE:
2591     case CONST_INT:
2592       return TRUE;
2593
2594     case MEM:
2595       return memory_address_p (DImode, XEXP (op, 0));
2596
2597     default:
2598       return FALSE;
2599     }
2600 }
2601
2602 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
2603    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2604    Note that this disallows MEM(REG+REG), but allows
2605    MEM(PRE/POST_INC/DEC(REG)).  */
2606
2607 int
2608 soft_df_operand (op, mode)
2609      rtx op;
2610      enum machine_mode mode;
2611 {
2612   if (s_register_operand (op, mode))
2613     return TRUE;
2614
2615   if (mode != VOIDmode && GET_MODE (op) != mode)
2616     return FALSE;
2617
2618   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
2619     return FALSE;
2620   
2621   if (GET_CODE (op) == SUBREG)
2622     op = SUBREG_REG (op);
2623   
2624   switch (GET_CODE (op))
2625     {
2626     case CONST_DOUBLE:
2627       return TRUE;
2628
2629     case MEM:
2630       return memory_address_p (DFmode, XEXP (op, 0));
2631
2632     default:
2633       return FALSE;
2634     }
2635 }
2636
2637 /* Return TRUE for valid index operands. */
2638
2639 int
2640 index_operand (op, mode)
2641      rtx op;
2642      enum machine_mode mode;
2643 {
2644   return (s_register_operand(op, mode)
2645           || (immediate_operand (op, mode)
2646               && INTVAL (op) < 4096 && INTVAL (op) > -4096));
2647 }
2648
2649 /* Return TRUE for valid shifts by a constant. This also accepts any
2650    power of two on the (somewhat overly relaxed) assumption that the
2651    shift operator in this case was a mult. */
2652
2653 int
2654 const_shift_operand (op, mode)
2655      rtx op;
2656      enum machine_mode mode;
2657 {
2658   return (power_of_two_operand (op, mode)
2659           || (immediate_operand (op, mode)
2660               && (INTVAL (op) < 32 && INTVAL (op) > 0)));
2661 }
2662
2663 /* Return TRUE for arithmetic operators which can be combined with a multiply
2664    (shift).  */
2665
2666 int
2667 shiftable_operator (x, mode)
2668      rtx x;
2669      enum machine_mode mode;
2670 {
2671   if (GET_MODE (x) != mode)
2672     return FALSE;
2673   else
2674     {
2675       enum rtx_code code = GET_CODE (x);
2676
2677       return (code == PLUS || code == MINUS
2678               || code == IOR || code == XOR || code == AND);
2679     }
2680 }
2681
2682 /* Return TRUE for binary logical operators.  */
2683
2684 int
2685 logical_binary_operator (x, mode)
2686      rtx x;
2687      enum machine_mode mode;
2688 {
2689   if (GET_MODE (x) != mode)
2690     return FALSE;
2691   else
2692     {
2693       enum rtx_code code = GET_CODE (x);
2694
2695       return (code == IOR || code == XOR || code == AND);
2696     }
2697 }
2698
2699 /* Return TRUE for shift operators. */
2700
2701 int
2702 shift_operator (x, mode)
2703      rtx x;
2704      enum machine_mode mode;
2705 {
2706   if (GET_MODE (x) != mode)
2707     return FALSE;
2708   else
2709     {
2710       enum rtx_code code = GET_CODE (x);
2711
2712       if (code == MULT)
2713         return power_of_two_operand (XEXP (x, 1), mode);
2714
2715       return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
2716               || code == ROTATERT);
2717     }
2718 }
2719
2720 int equality_operator (x, mode)
2721      rtx x;
2722      enum machine_mode mode ATTRIBUTE_UNUSED;
2723 {
2724   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
2725 }
2726
2727 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
2728
2729 int
2730 minmax_operator (x, mode)
2731      rtx x;
2732      enum machine_mode mode;
2733 {
2734   enum rtx_code code = GET_CODE (x);
2735
2736   if (GET_MODE (x) != mode)
2737     return FALSE;
2738
2739   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
2740 }
2741
2742 /* return TRUE if x is EQ or NE */
2743
2744 /* Return TRUE if this is the condition code register, if we aren't given
2745    a mode, accept any class CCmode register */
2746
2747 int
2748 cc_register (x, mode)
2749      rtx x;
2750      enum machine_mode mode;
2751 {
2752   if (mode == VOIDmode)
2753     {
2754       mode = GET_MODE (x);
2755       if (GET_MODE_CLASS (mode) != MODE_CC)
2756         return FALSE;
2757     }
2758
2759   if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2760     return TRUE;
2761
2762   return FALSE;
2763 }
2764
2765 /* Return TRUE if this is the condition code register, if we aren't given
2766    a mode, accept any class CCmode register which indicates a dominance
2767    expression.  */
2768
2769 int
2770 dominant_cc_register (x, mode)
2771      rtx x;
2772      enum machine_mode mode;
2773 {
2774   if (mode == VOIDmode)
2775     {
2776       mode = GET_MODE (x);
2777       if (GET_MODE_CLASS (mode) != MODE_CC)
2778         return FALSE;
2779     }
2780
2781   if (mode != CC_DNEmode && mode != CC_DEQmode
2782       && mode != CC_DLEmode && mode != CC_DLTmode
2783       && mode != CC_DGEmode && mode != CC_DGTmode
2784       && mode != CC_DLEUmode && mode != CC_DLTUmode
2785       && mode != CC_DGEUmode && mode != CC_DGTUmode)
2786     return FALSE;
2787
2788   if (mode == GET_MODE (x) && GET_CODE (x) == REG && REGNO (x) == 24)
2789     return TRUE;
2790
2791   return FALSE;
2792 }
2793
2794 /* Return TRUE if X references a SYMBOL_REF.  */
2795 int
2796 symbol_mentioned_p (x)
2797      rtx x;
2798 {
2799   register const char * fmt;
2800   register int i;
2801
2802   if (GET_CODE (x) == SYMBOL_REF)
2803     return 1;
2804
2805   fmt = GET_RTX_FORMAT (GET_CODE (x));
2806   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2807     {
2808       if (fmt[i] == 'E')
2809         {
2810           register int j;
2811
2812           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2813             if (symbol_mentioned_p (XVECEXP (x, i, j)))
2814               return 1;
2815         }
2816       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
2817         return 1;
2818     }
2819
2820   return 0;
2821 }
2822
2823 /* Return TRUE if X references a LABEL_REF.  */
2824 int
2825 label_mentioned_p (x)
2826      rtx x;
2827 {
2828   register const char * fmt;
2829   register int i;
2830
2831   if (GET_CODE (x) == LABEL_REF)
2832     return 1;
2833
2834   fmt = GET_RTX_FORMAT (GET_CODE (x));
2835   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2836     {
2837       if (fmt[i] == 'E')
2838         {
2839           register int j;
2840
2841           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2842             if (label_mentioned_p (XVECEXP (x, i, j)))
2843               return 1;
2844         }
2845       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
2846         return 1;
2847     }
2848
2849   return 0;
2850 }
2851
2852 enum rtx_code
2853 minmax_code (x)
2854      rtx x;
2855 {
2856   enum rtx_code code = GET_CODE (x);
2857
2858   if (code == SMAX)
2859     return GE;
2860   else if (code == SMIN)
2861     return LE;
2862   else if (code == UMIN)
2863     return LEU;
2864   else if (code == UMAX)
2865     return GEU;
2866
2867   abort ();
2868 }
2869
2870 /* Return 1 if memory locations are adjacent */
2871
2872 int
2873 adjacent_mem_locations (a, b)
2874      rtx a, b;
2875 {
2876   int val0 = 0, val1 = 0;
2877   int reg0, reg1;
2878   
2879   if ((GET_CODE (XEXP (a, 0)) == REG
2880        || (GET_CODE (XEXP (a, 0)) == PLUS
2881            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
2882       && (GET_CODE (XEXP (b, 0)) == REG
2883           || (GET_CODE (XEXP (b, 0)) == PLUS
2884               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
2885     {
2886       if (GET_CODE (XEXP (a, 0)) == PLUS)
2887         {
2888           reg0 = REGNO (XEXP (XEXP (a, 0), 0));
2889           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
2890         }
2891       else
2892         reg0 = REGNO (XEXP (a, 0));
2893       if (GET_CODE (XEXP (b, 0)) == PLUS)
2894         {
2895           reg1 = REGNO (XEXP (XEXP (b, 0), 0));
2896           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
2897         }
2898       else
2899         reg1 = REGNO (XEXP (b, 0));
2900       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
2901     }
2902   return 0;
2903 }
2904
2905 /* Return 1 if OP is a load multiple operation.  It is known to be
2906    parallel and the first section will be tested. */
2907
2908 int
2909 load_multiple_operation (op, mode)
2910      rtx op;
2911      enum machine_mode mode ATTRIBUTE_UNUSED;
2912 {
2913   HOST_WIDE_INT count = XVECLEN (op, 0);
2914   int dest_regno;
2915   rtx src_addr;
2916   HOST_WIDE_INT i = 1, base = 0;
2917   rtx elt;
2918
2919   if (count <= 1
2920       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2921     return 0;
2922
2923   /* Check to see if this might be a write-back */
2924   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2925     {
2926       i++;
2927       base = 1;
2928
2929       /* Now check it more carefully */
2930       if (GET_CODE (SET_DEST (elt)) != REG
2931           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
2932           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
2933           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
2934           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
2935           || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
2936           || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
2937           || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
2938               != REGNO (SET_DEST (elt)))
2939         return 0;
2940
2941       count--;
2942     }
2943
2944   /* Perform a quick check so we don't blow up below.  */
2945   if (count <= i
2946       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
2947       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
2948       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
2949     return 0;
2950
2951   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
2952   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
2953
2954   for (; i < count; i++)
2955     {
2956       elt = XVECEXP (op, 0, i);
2957
2958       if (GET_CODE (elt) != SET
2959           || GET_CODE (SET_DEST (elt)) != REG
2960           || GET_MODE (SET_DEST (elt)) != SImode
2961           || REGNO (SET_DEST (elt)) != dest_regno + i - base
2962           || GET_CODE (SET_SRC (elt)) != MEM
2963           || GET_MODE (SET_SRC (elt)) != SImode
2964           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
2965           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
2966           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
2967           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
2968         return 0;
2969     }
2970
2971   return 1;
2972 }
2973
2974 /* Return 1 if OP is a store multiple operation.  It is known to be
2975    parallel and the first section will be tested. */
2976
2977 int
2978 store_multiple_operation (op, mode)
2979      rtx op;
2980      enum machine_mode mode ATTRIBUTE_UNUSED;
2981 {
2982   HOST_WIDE_INT count = XVECLEN (op, 0);
2983   int src_regno;
2984   rtx dest_addr;
2985   HOST_WIDE_INT i = 1, base = 0;
2986   rtx elt;
2987
2988   if (count <= 1
2989       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
2990     return 0;
2991
2992   /* Check to see if this might be a write-back */
2993   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
2994     {
2995       i++;
2996       base = 1;
2997
2998       /* Now check it more carefully */
2999       if (GET_CODE (SET_DEST (elt)) != REG
3000           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3001           || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3002           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3003           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
3004           || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
3005           || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
3006           || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
3007               != REGNO (SET_DEST (elt)))
3008         return 0;
3009
3010       count--;
3011     }
3012
3013   /* Perform a quick check so we don't blow up below.  */
3014   if (count <= i
3015       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3016       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
3017       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
3018     return 0;
3019
3020   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
3021   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
3022
3023   for (; i < count; i++)
3024     {
3025       elt = XVECEXP (op, 0, i);
3026
3027       if (GET_CODE (elt) != SET
3028           || GET_CODE (SET_SRC (elt)) != REG
3029           || GET_MODE (SET_SRC (elt)) != SImode
3030           || REGNO (SET_SRC (elt)) != src_regno + i - base
3031           || GET_CODE (SET_DEST (elt)) != MEM
3032           || GET_MODE (SET_DEST (elt)) != SImode
3033           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
3034           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
3035           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
3036           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
3037         return 0;
3038     }
3039
3040   return 1;
3041 }
3042
3043 int
3044 load_multiple_sequence (operands, nops, regs, base, load_offset)
3045      rtx * operands;
3046      int nops;
3047      int * regs;
3048      int * base;
3049      HOST_WIDE_INT * load_offset;
3050 {
3051   int unsorted_regs[4];
3052   HOST_WIDE_INT unsorted_offsets[4];
3053   int order[4];
3054   int base_reg = -1;
3055   int i;
3056
3057   /* Can only handle 2, 3, or 4 insns at present, though could be easily
3058      extended if required.  */
3059   if (nops < 2 || nops > 4)
3060     abort ();
3061
3062   /* Loop over the operands and check that the memory references are
3063      suitable (ie immediate offsets from the same base register).  At
3064      the same time, extract the target register, and the memory
3065      offsets.  */
3066   for (i = 0; i < nops; i++)
3067     {
3068       rtx reg;
3069       rtx offset;
3070
3071       /* Convert a subreg of a mem into the mem itself.  */
3072       if (GET_CODE (operands[nops + i]) == SUBREG)
3073         operands[nops + i] = alter_subreg(operands[nops + i]);
3074
3075       if (GET_CODE (operands[nops + i]) != MEM)
3076         abort ();
3077
3078       /* Don't reorder volatile memory references; it doesn't seem worth
3079          looking for the case where the order is ok anyway.  */
3080       if (MEM_VOLATILE_P (operands[nops + i]))
3081         return 0;
3082
3083       offset = const0_rtx;
3084
3085       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3086            || (GET_CODE (reg) == SUBREG
3087                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3088           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3089               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3090                    == REG)
3091                   || (GET_CODE (reg) == SUBREG
3092                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3093               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3094                   == CONST_INT)))
3095         {
3096           if (i == 0)
3097             {
3098               base_reg = REGNO(reg);
3099               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3100                                   ? REGNO (operands[i])
3101                                   : REGNO (SUBREG_REG (operands[i])));
3102               order[0] = 0;
3103             }
3104           else 
3105             {
3106               if (base_reg != REGNO (reg))
3107                 /* Not addressed from the same base register.  */
3108                 return 0;
3109
3110               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3111                                   ? REGNO (operands[i])
3112                                   : REGNO (SUBREG_REG (operands[i])));
3113               if (unsorted_regs[i] < unsorted_regs[order[0]])
3114                 order[0] = i;
3115             }
3116
3117           /* If it isn't an integer register, or if it overwrites the
3118              base register but isn't the last insn in the list, then
3119              we can't do this.  */
3120           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
3121               || (i != nops - 1 && unsorted_regs[i] == base_reg))
3122             return 0;
3123
3124           unsorted_offsets[i] = INTVAL (offset);
3125         }
3126       else
3127         /* Not a suitable memory address.  */
3128         return 0;
3129     }
3130
3131   /* All the useful information has now been extracted from the
3132      operands into unsorted_regs and unsorted_offsets; additionally,
3133      order[0] has been set to the lowest numbered register in the
3134      list.  Sort the registers into order, and check that the memory
3135      offsets are ascending and adjacent.  */
3136
3137   for (i = 1; i < nops; i++)
3138     {
3139       int j;
3140
3141       order[i] = order[i - 1];
3142       for (j = 0; j < nops; j++)
3143         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3144             && (order[i] == order[i - 1]
3145                 || unsorted_regs[j] < unsorted_regs[order[i]]))
3146           order[i] = j;
3147
3148       /* Have we found a suitable register? if not, one must be used more
3149          than once.  */
3150       if (order[i] == order[i - 1])
3151         return 0;
3152
3153       /* Is the memory address adjacent and ascending? */
3154       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3155         return 0;
3156     }
3157
3158   if (base)
3159     {
3160       *base = base_reg;
3161
3162       for (i = 0; i < nops; i++)
3163         regs[i] = unsorted_regs[order[i]];
3164
3165       *load_offset = unsorted_offsets[order[0]];
3166     }
3167
3168   if (unsorted_offsets[order[0]] == 0)
3169     return 1; /* ldmia */
3170
3171   if (unsorted_offsets[order[0]] == 4)
3172     return 2; /* ldmib */
3173
3174   if (unsorted_offsets[order[nops - 1]] == 0)
3175     return 3; /* ldmda */
3176
3177   if (unsorted_offsets[order[nops - 1]] == -4)
3178     return 4; /* ldmdb */
3179
3180   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
3181      if the offset isn't small enough.  The reason 2 ldrs are faster
3182      is because these ARMs are able to do more than one cache access
3183      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
3184      whilst the ARM8 has a double bandwidth cache.  This means that
3185      these cores can do both an instruction fetch and a data fetch in
3186      a single cycle, so the trick of calculating the address into a
3187      scratch register (one of the result regs) and then doing a load
3188      multiple actually becomes slower (and no smaller in code size).
3189      That is the transformation
3190  
3191         ldr     rd1, [rbase + offset]
3192         ldr     rd2, [rbase + offset + 4]
3193  
3194      to
3195  
3196         add     rd1, rbase, offset
3197         ldmia   rd1, {rd1, rd2}
3198  
3199      produces worse code -- '3 cycles + any stalls on rd2' instead of
3200      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
3201      access per cycle, the first sequence could never complete in less
3202      than 6 cycles, whereas the ldm sequence would only take 5 and
3203      would make better use of sequential accesses if not hitting the
3204      cache.
3205
3206      We cheat here and test 'arm_ld_sched' which we currently know to
3207      only be true for the ARM8, ARM9 and StrongARM.  If this ever
3208      changes, then the test below needs to be reworked.  */
3209   if (nops == 2 && arm_ld_sched)
3210     return 0;
3211
3212   /* Can't do it without setting up the offset, only do this if it takes
3213      no more than one insn.  */
3214   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
3215           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
3216 }
3217
3218 char *
3219 emit_ldm_seq (operands, nops)
3220      rtx * operands;
3221      int nops;
3222 {
3223   int regs[4];
3224   int base_reg;
3225   HOST_WIDE_INT offset;
3226   char buf[100];
3227   int i;
3228
3229   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3230     {
3231     case 1:
3232       strcpy (buf, "ldm%?ia\t");
3233       break;
3234
3235     case 2:
3236       strcpy (buf, "ldm%?ib\t");
3237       break;
3238
3239     case 3:
3240       strcpy (buf, "ldm%?da\t");
3241       break;
3242
3243     case 4:
3244       strcpy (buf, "ldm%?db\t");
3245       break;
3246
3247     case 5:
3248       if (offset >= 0)
3249         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3250                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3251                  (long) offset);
3252       else
3253         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3254                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3255                  (long) -offset);
3256       output_asm_insn (buf, operands);
3257       base_reg = regs[0];
3258       strcpy (buf, "ldm%?ia\t");
3259       break;
3260
3261     default:
3262       abort ();
3263     }
3264
3265   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
3266            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3267
3268   for (i = 1; i < nops; i++)
3269     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3270              reg_names[regs[i]]);
3271
3272   strcat (buf, "}\t%@ phole ldm");
3273
3274   output_asm_insn (buf, operands);
3275   return "";
3276 }
3277
3278 int
3279 store_multiple_sequence (operands, nops, regs, base, load_offset)
3280      rtx * operands;
3281      int nops;
3282      int * regs;
3283      int * base;
3284      HOST_WIDE_INT * load_offset;
3285 {
3286   int unsorted_regs[4];
3287   HOST_WIDE_INT unsorted_offsets[4];
3288   int order[4];
3289   int base_reg = -1;
3290   int i;
3291
3292   /* Can only handle 2, 3, or 4 insns at present, though could be easily
3293      extended if required.  */
3294   if (nops < 2 || nops > 4)
3295     abort ();
3296
3297   /* Loop over the operands and check that the memory references are
3298      suitable (ie immediate offsets from the same base register).  At
3299      the same time, extract the target register, and the memory
3300      offsets.  */
3301   for (i = 0; i < nops; i++)
3302     {
3303       rtx reg;
3304       rtx offset;
3305
3306       /* Convert a subreg of a mem into the mem itself.  */
3307       if (GET_CODE (operands[nops + i]) == SUBREG)
3308         operands[nops + i] = alter_subreg(operands[nops + i]);
3309
3310       if (GET_CODE (operands[nops + i]) != MEM)
3311         abort ();
3312
3313       /* Don't reorder volatile memory references; it doesn't seem worth
3314          looking for the case where the order is ok anyway.  */
3315       if (MEM_VOLATILE_P (operands[nops + i]))
3316         return 0;
3317
3318       offset = const0_rtx;
3319
3320       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3321            || (GET_CODE (reg) == SUBREG
3322                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3323           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3324               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3325                    == REG)
3326                   || (GET_CODE (reg) == SUBREG
3327                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3328               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3329                   == CONST_INT)))
3330         {
3331           if (i == 0)
3332             {
3333               base_reg = REGNO (reg);
3334               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3335                                   ? REGNO (operands[i])
3336                                   : REGNO (SUBREG_REG (operands[i])));
3337               order[0] = 0;
3338             }
3339           else 
3340             {
3341               if (base_reg != REGNO (reg))
3342                 /* Not addressed from the same base register.  */
3343                 return 0;
3344
3345               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3346                                   ? REGNO (operands[i])
3347                                   : REGNO (SUBREG_REG (operands[i])));
3348               if (unsorted_regs[i] < unsorted_regs[order[0]])
3349                 order[0] = i;
3350             }
3351
3352           /* If it isn't an integer register, then we can't do this.  */
3353           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
3354             return 0;
3355
3356           unsorted_offsets[i] = INTVAL (offset);
3357         }
3358       else
3359         /* Not a suitable memory address.  */
3360         return 0;
3361     }
3362
3363   /* All the useful information has now been extracted from the
3364      operands into unsorted_regs and unsorted_offsets; additionally,
3365      order[0] has been set to the lowest numbered register in the
3366      list.  Sort the registers into order, and check that the memory
3367      offsets are ascending and adjacent.  */
3368
3369   for (i = 1; i < nops; i++)
3370     {
3371       int j;
3372
3373       order[i] = order[i - 1];
3374       for (j = 0; j < nops; j++)
3375         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3376             && (order[i] == order[i - 1]
3377                 || unsorted_regs[j] < unsorted_regs[order[i]]))
3378           order[i] = j;
3379
3380       /* Have we found a suitable register? if not, one must be used more
3381          than once.  */
3382       if (order[i] == order[i - 1])
3383         return 0;
3384
3385       /* Is the memory address adjacent and ascending? */
3386       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3387         return 0;
3388     }
3389
3390   if (base)
3391     {
3392       *base = base_reg;
3393
3394       for (i = 0; i < nops; i++)
3395         regs[i] = unsorted_regs[order[i]];
3396
3397       *load_offset = unsorted_offsets[order[0]];
3398     }
3399
3400   if (unsorted_offsets[order[0]] == 0)
3401     return 1; /* stmia */
3402
3403   if (unsorted_offsets[order[0]] == 4)
3404     return 2; /* stmib */
3405
3406   if (unsorted_offsets[order[nops - 1]] == 0)
3407     return 3; /* stmda */
3408
3409   if (unsorted_offsets[order[nops - 1]] == -4)
3410     return 4; /* stmdb */
3411
3412   return 0;
3413 }
3414
3415 char *
3416 emit_stm_seq (operands, nops)
3417      rtx * operands;
3418      int nops;
3419 {
3420   int regs[4];
3421   int base_reg;
3422   HOST_WIDE_INT offset;
3423   char buf[100];
3424   int i;
3425
3426   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3427     {
3428     case 1:
3429       strcpy (buf, "stm%?ia\t");
3430       break;
3431
3432     case 2:
3433       strcpy (buf, "stm%?ib\t");
3434       break;
3435
3436     case 3:
3437       strcpy (buf, "stm%?da\t");
3438       break;
3439
3440     case 4:
3441       strcpy (buf, "stm%?db\t");
3442       break;
3443
3444     default:
3445       abort ();
3446     }
3447
3448   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
3449            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3450
3451   for (i = 1; i < nops; i++)
3452     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3453              reg_names[regs[i]]);
3454
3455   strcat (buf, "}\t%@ phole stm");
3456
3457   output_asm_insn (buf, operands);
3458   return "";
3459 }
3460
3461 int
3462 multi_register_push (op, mode)
3463      rtx op;
3464      enum machine_mode mode ATTRIBUTE_UNUSED;
3465 {
3466   if (GET_CODE (op) != PARALLEL
3467       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
3468       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
3469       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != 2))
3470     return 0;
3471
3472   return 1;
3473 }
3474
3475 \f
3476 /* Routines for use with attributes.  */
3477
3478 /* Return nonzero if ATTR is a valid attribute for DECL.
3479    ATTRIBUTES are any existing attributes and ARGS are
3480    the arguments supplied with ATTR.
3481
3482    Supported attributes:
3483
3484      naked: don't output any prologue or epilogue code,
3485             the user is assumed to do the right thing.
3486 */
3487 int
3488 arm_valid_machine_decl_attribute (decl, attr, args)
3489      tree decl;
3490      tree attr;
3491      tree args;
3492 {
3493   if (args != NULL_TREE)
3494     return 0;
3495
3496   if (is_attribute_p ("naked", attr))
3497     return TREE_CODE (decl) == FUNCTION_DECL;
3498   return 0;
3499 }
3500
3501 /* Return non-zero if FUNC is a naked function.  */
3502
3503 static int
3504 arm_naked_function_p (func)
3505      tree func;
3506 {
3507   tree a;
3508
3509   if (TREE_CODE (func) != FUNCTION_DECL)
3510     abort ();
3511   
3512   a = lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (func));
3513   return a != NULL_TREE;
3514 }
3515 \f
3516 /* Routines for use in generating RTL */
3517
3518 rtx
3519 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
3520                        in_struct_p, scalar_p)
3521      int base_regno;
3522      int count;
3523      rtx from;
3524      int up;
3525      int write_back;
3526      int unchanging_p;
3527      int in_struct_p;
3528      int scalar_p;
3529 {
3530   int i = 0, j;
3531   rtx result;
3532   int sign = up ? 1 : -1;
3533   rtx mem;
3534
3535   result = gen_rtx_PARALLEL (VOIDmode,
3536                              rtvec_alloc (count + (write_back ? 2 : 0)));
3537   if (write_back)
3538     {
3539       XVECEXP (result, 0, 0)
3540         = gen_rtx_SET (GET_MODE (from), from,
3541                        plus_constant (from, count * 4 * sign));
3542       i = 1;
3543       count++;
3544     }
3545
3546   for (j = 0; i < count; i++, j++)
3547     {
3548       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
3549       RTX_UNCHANGING_P (mem) = unchanging_p;
3550       MEM_IN_STRUCT_P (mem) = in_struct_p;
3551       MEM_SCALAR_P (mem) = scalar_p;
3552       XVECEXP (result, 0, i)
3553         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
3554     }
3555
3556   if (write_back)
3557     XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, from);
3558
3559   return result;
3560 }
3561
3562 rtx
3563 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
3564                         in_struct_p, scalar_p)
3565      int base_regno;
3566      int count;
3567      rtx to;
3568      int up;
3569      int write_back;
3570      int unchanging_p;
3571      int in_struct_p;
3572      int scalar_p;
3573 {
3574   int i = 0, j;
3575   rtx result;
3576   int sign = up ? 1 : -1;
3577   rtx mem;
3578
3579   result = gen_rtx_PARALLEL (VOIDmode,
3580                              rtvec_alloc (count + (write_back ? 2 : 0)));
3581   if (write_back)
3582     {
3583       XVECEXP (result, 0, 0)
3584         = gen_rtx_SET (GET_MODE (to), to,
3585                        plus_constant (to, count * 4 * sign));
3586       i = 1;
3587       count++;
3588     }
3589
3590   for (j = 0; i < count; i++, j++)
3591     {
3592       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
3593       RTX_UNCHANGING_P (mem) = unchanging_p;
3594       MEM_IN_STRUCT_P (mem) = in_struct_p;
3595       MEM_SCALAR_P (mem) = scalar_p;
3596
3597       XVECEXP (result, 0, i)
3598         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
3599     }
3600
3601   if (write_back)
3602     XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, to);
3603
3604   return result;
3605 }
3606
3607 int
3608 arm_gen_movstrqi (operands)
3609      rtx * operands;
3610 {
3611   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
3612   int i;
3613   rtx src, dst;
3614   rtx st_src, st_dst, fin_src, fin_dst;
3615   rtx part_bytes_reg = NULL;
3616   rtx mem;
3617   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
3618   int dst_scalar_p, src_scalar_p;
3619
3620   if (GET_CODE (operands[2]) != CONST_INT
3621       || GET_CODE (operands[3]) != CONST_INT
3622       || INTVAL (operands[2]) > 64
3623       || INTVAL (operands[3]) & 3)
3624     return 0;
3625
3626   st_dst = XEXP (operands[0], 0);
3627   st_src = XEXP (operands[1], 0);
3628
3629   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
3630   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
3631   dst_scalar_p = MEM_SCALAR_P (operands[0]);
3632   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
3633   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
3634   src_scalar_p = MEM_SCALAR_P (operands[1]);
3635
3636   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
3637   fin_src = src = copy_to_mode_reg (SImode, st_src);
3638
3639   in_words_to_go = (INTVAL (operands[2]) + 3) / 4;
3640   out_words_to_go = INTVAL (operands[2]) / 4;
3641   last_bytes = INTVAL (operands[2]) & 3;
3642
3643   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
3644     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
3645
3646   for (i = 0; in_words_to_go >= 2; i+=4)
3647     {
3648       if (in_words_to_go > 4)
3649         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
3650                                           src_unchanging_p,
3651                                           src_in_struct_p,
3652                                           src_scalar_p));
3653       else
3654         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
3655                                           FALSE, src_unchanging_p,
3656                                           src_in_struct_p, src_scalar_p));
3657
3658       if (out_words_to_go)
3659         {
3660           if (out_words_to_go > 4)
3661             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
3662                                                dst_unchanging_p,
3663                                                dst_in_struct_p,
3664                                                dst_scalar_p));
3665           else if (out_words_to_go != 1)
3666             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
3667                                                dst, TRUE, 
3668                                                (last_bytes == 0
3669                                                 ? FALSE : TRUE),
3670                                                dst_unchanging_p,
3671                                                dst_in_struct_p,
3672                                                dst_scalar_p));
3673           else
3674             {
3675               mem = gen_rtx_MEM (SImode, dst);
3676               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3677               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3678               MEM_SCALAR_P (mem) = dst_scalar_p;
3679               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
3680               if (last_bytes != 0)
3681                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
3682             }
3683         }
3684
3685       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
3686       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
3687     }
3688
3689   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
3690   if (out_words_to_go)
3691     {
3692       rtx sreg;
3693       
3694       mem = gen_rtx_MEM (SImode, src);
3695       RTX_UNCHANGING_P (mem) = src_unchanging_p;
3696       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3697       MEM_SCALAR_P (mem) = src_scalar_p;
3698       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
3699       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
3700       
3701       mem = gen_rtx_MEM (SImode, dst);
3702       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3703       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3704       MEM_SCALAR_P (mem) = dst_scalar_p;
3705       emit_move_insn (mem, sreg);
3706       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
3707       in_words_to_go--;
3708       
3709       if (in_words_to_go)       /* Sanity check */
3710         abort ();
3711     }
3712
3713   if (in_words_to_go)
3714     {
3715       if (in_words_to_go < 0)
3716         abort ();
3717
3718       mem = gen_rtx_MEM (SImode, src);
3719       RTX_UNCHANGING_P (mem) = src_unchanging_p;
3720       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3721       MEM_SCALAR_P (mem) = src_scalar_p;
3722       part_bytes_reg = copy_to_mode_reg (SImode, mem);
3723     }
3724
3725   if (BYTES_BIG_ENDIAN && last_bytes)
3726     {
3727       rtx tmp = gen_reg_rtx (SImode);
3728
3729       if (part_bytes_reg == NULL)
3730         abort ();
3731
3732       /* The bytes we want are in the top end of the word */
3733       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
3734                               GEN_INT (8 * (4 - last_bytes))));
3735       part_bytes_reg = tmp;
3736       
3737       while (last_bytes)
3738         {
3739           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
3740           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3741           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3742           MEM_SCALAR_P (mem) = dst_scalar_p;
3743           emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3744           
3745           if (--last_bytes)
3746             {
3747               tmp = gen_reg_rtx (SImode);
3748               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3749               part_bytes_reg = tmp;
3750             }
3751         }
3752           
3753     }
3754   else
3755     {
3756       while (last_bytes)
3757         {
3758           if (part_bytes_reg == NULL)
3759             abort ();
3760
3761           mem = gen_rtx_MEM (QImode, dst);
3762           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3763           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3764           MEM_SCALAR_P (mem) = dst_scalar_p;
3765           emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
3766           
3767           if (--last_bytes)
3768             {
3769               rtx tmp = gen_reg_rtx (SImode);
3770
3771               emit_insn (gen_addsi3 (dst, dst, const1_rtx));
3772               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
3773               part_bytes_reg = tmp;
3774             }
3775         }
3776     }
3777
3778   return 1;
3779 }
3780
3781 /* Generate a memory reference for a half word, such that it will be loaded
3782    into the top 16 bits of the word.  We can assume that the address is
3783    known to be alignable and of the form reg, or plus (reg, const).  */
3784 rtx
3785 gen_rotated_half_load (memref)
3786      rtx memref;
3787 {
3788   HOST_WIDE_INT offset = 0;
3789   rtx base = XEXP (memref, 0);
3790
3791   if (GET_CODE (base) == PLUS)
3792     {
3793       offset = INTVAL (XEXP (base, 1));
3794       base = XEXP (base, 0);
3795     }
3796
3797   /* If we aren't allowed to generate unaligned addresses, then fail.  */
3798   if (TARGET_MMU_TRAPS
3799       && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
3800     return NULL;
3801
3802   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
3803
3804   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
3805     return base;
3806
3807   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
3808 }
3809
3810 static enum machine_mode
3811 select_dominance_cc_mode (x, y, cond_or)
3812      rtx x;
3813      rtx y;
3814      HOST_WIDE_INT cond_or;
3815 {
3816   enum rtx_code cond1, cond2;
3817   int swapped = 0;
3818
3819   /* Currently we will probably get the wrong result if the individual
3820      comparisons are not simple.  This also ensures that it is safe to
3821      reverse a comparison if necessary.  */
3822   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
3823        != CCmode)
3824       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
3825           != CCmode))
3826     return CCmode;
3827
3828   if (cond_or)
3829     cond1 = reverse_condition (cond1);
3830
3831   /* If the comparisons are not equal, and one doesn't dominate the other,
3832      then we can't do this.  */
3833   if (cond1 != cond2 
3834       && ! comparison_dominates_p (cond1, cond2)
3835       && (swapped = 1, ! comparison_dominates_p (cond2, cond1)))
3836     return CCmode;
3837
3838   if (swapped)
3839     {
3840       enum rtx_code temp = cond1;
3841       cond1 = cond2;
3842       cond2 = temp;
3843     }
3844
3845   switch (cond1)
3846     {
3847     case EQ:
3848       if (cond2 == EQ || ! cond_or)
3849         return CC_DEQmode;
3850
3851       switch (cond2)
3852         {
3853         case LE: return CC_DLEmode;
3854         case LEU: return CC_DLEUmode;
3855         case GE: return CC_DGEmode;
3856         case GEU: return CC_DGEUmode;
3857         default: break;
3858         }
3859
3860       break;
3861
3862     case LT:
3863       if (cond2 == LT || ! cond_or)
3864         return CC_DLTmode;
3865       if (cond2 == LE)
3866         return CC_DLEmode;
3867       if (cond2 == NE)
3868         return CC_DNEmode;
3869       break;
3870
3871     case GT:
3872       if (cond2 == GT || ! cond_or)
3873         return CC_DGTmode;
3874       if (cond2 == GE)
3875         return CC_DGEmode;
3876       if (cond2 == NE)
3877         return CC_DNEmode;
3878       break;
3879       
3880     case LTU:
3881       if (cond2 == LTU || ! cond_or)
3882         return CC_DLTUmode;
3883       if (cond2 == LEU)
3884         return CC_DLEUmode;
3885       if (cond2 == NE)
3886         return CC_DNEmode;
3887       break;
3888
3889     case GTU:
3890       if (cond2 == GTU || ! cond_or)
3891         return CC_DGTUmode;
3892       if (cond2 == GEU)
3893         return CC_DGEUmode;
3894       if (cond2 == NE)
3895         return CC_DNEmode;
3896       break;
3897
3898     /* The remaining cases only occur when both comparisons are the
3899        same.  */
3900     case NE:
3901       return CC_DNEmode;
3902
3903     case LE:
3904       return CC_DLEmode;
3905
3906     case GE:
3907       return CC_DGEmode;
3908
3909     case LEU:
3910       return CC_DLEUmode;
3911
3912     case GEU:
3913       return CC_DGEUmode;
3914
3915     default:
3916       break;
3917     }
3918
3919   abort ();
3920 }
3921
3922 enum machine_mode
3923 arm_select_cc_mode (op, x, y)
3924      enum rtx_code op;
3925      rtx x;
3926      rtx y;
3927 {
3928   /* All floating point compares return CCFP if it is an equality
3929      comparison, and CCFPE otherwise.  */
3930   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3931     return (op == EQ || op == NE) ? CCFPmode : CCFPEmode;
3932   
3933   /* A compare with a shifted operand.  Because of canonicalization, the
3934      comparison will have to be swapped when we emit the assembler.  */
3935   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
3936       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3937           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
3938           || GET_CODE (x) == ROTATERT))
3939     return CC_SWPmode;
3940
3941   /* This is a special case that is used by combine to allow a 
3942      comparison of a shifted byte load to be split into a zero-extend
3943      followed by a comparison of the shifted integer (only valid for
3944      equalities and unsigned inequalities).  */
3945   if (GET_MODE (x) == SImode
3946       && GET_CODE (x) == ASHIFT
3947       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
3948       && GET_CODE (XEXP (x, 0)) == SUBREG
3949       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
3950       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
3951       && (op == EQ || op == NE
3952           || op == GEU || op == GTU || op == LTU || op == LEU)
3953       && GET_CODE (y) == CONST_INT)
3954     return CC_Zmode;
3955
3956   /* An operation that sets the condition codes as a side-effect, the
3957      V flag is not set correctly, so we can only use comparisons where
3958      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
3959      instead.  */
3960   if (GET_MODE (x) == SImode
3961       && y == const0_rtx
3962       && (op == EQ || op == NE || op == LT || op == GE)
3963       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
3964           || GET_CODE (x) == AND || GET_CODE (x) == IOR
3965           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
3966           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
3967           || GET_CODE (x) == LSHIFTRT
3968           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
3969           || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
3970     return CC_NOOVmode;
3971
3972   /* A construct for a conditional compare, if the false arm contains
3973      0, then both conditions must be true, otherwise either condition
3974      must be true.  Not all conditions are possible, so CCmode is
3975      returned if it can't be done.  */
3976   if (GET_CODE (x) == IF_THEN_ELSE
3977       && (XEXP (x, 2) == const0_rtx
3978           || XEXP (x, 2) == const1_rtx)
3979       && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3980       && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
3981     return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
3982                                      INTVAL (XEXP (x, 2)));
3983
3984   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
3985     return CC_Zmode;
3986
3987   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
3988       && GET_CODE (x) == PLUS
3989       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
3990     return CC_Cmode;
3991
3992   return CCmode;
3993 }
3994
3995 /* X and Y are two things to compare using CODE.  Emit the compare insn and
3996    return the rtx for register 0 in the proper mode.  FP means this is a
3997    floating point compare: I don't think that it is needed on the arm.  */
3998
3999 rtx
4000 gen_compare_reg (code, x, y)
4001      enum rtx_code code;
4002      rtx x, y;
4003 {
4004   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
4005   rtx cc_reg = gen_rtx_REG (mode, 24);
4006
4007   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
4008                           gen_rtx_COMPARE (mode, x, y)));
4009
4010   return cc_reg;
4011 }
4012
4013 void
4014 arm_reload_in_hi (operands)
4015      rtx * operands;
4016 {
4017   rtx ref = operands[1];
4018   rtx base, scratch;
4019   HOST_WIDE_INT offset = 0;
4020
4021   if (GET_CODE (ref) == SUBREG)
4022     {
4023       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
4024       if (BYTES_BIG_ENDIAN)
4025         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
4026                    - MIN (UNITS_PER_WORD,
4027                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
4028       ref = SUBREG_REG (ref);
4029     }
4030
4031   if (GET_CODE (ref) == REG)
4032     {
4033       /* We have a pseudo which has been spilt onto the stack; there
4034          are two cases here: the first where there is a simple
4035          stack-slot replacement and a second where the stack-slot is
4036          out of range, or is used as a subreg.  */
4037       if (reg_equiv_mem[REGNO (ref)])
4038         {
4039           ref = reg_equiv_mem[REGNO (ref)];
4040           base = find_replacement (&XEXP (ref, 0));
4041         }
4042       else
4043         /* The slot is out of range, or was dressed up in a SUBREG */
4044         base = reg_equiv_address[REGNO (ref)];
4045     }
4046   else
4047     base = find_replacement (&XEXP (ref, 0));
4048
4049   /* Handle the case where the address is too complex to be offset by 1.  */
4050   if (GET_CODE (base) == MINUS
4051       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4052     {
4053       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4054
4055       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4056       base = base_plus;
4057     }
4058   else if (GET_CODE (base) == PLUS)
4059     {
4060       /* The addend must be CONST_INT, or we would have dealt with it above */
4061       HOST_WIDE_INT hi, lo;
4062
4063       offset += INTVAL (XEXP (base, 1));
4064       base = XEXP (base, 0);
4065
4066       /* Rework the address into a legal sequence of insns */
4067       /* Valid range for lo is -4095 -> 4095 */
4068       lo = (offset >= 0
4069             ? (offset & 0xfff)
4070             : -((-offset) & 0xfff));
4071
4072       /* Corner case, if lo is the max offset then we would be out of range
4073          once we have added the additional 1 below, so bump the msb into the
4074          pre-loading insn(s).  */
4075       if (lo == 4095)
4076         lo &= 0x7ff;
4077
4078       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xFFFFFFFFUL)
4079              ^ (HOST_WIDE_INT) 0x80000000UL)
4080             - (HOST_WIDE_INT) 0x80000000UL);
4081
4082       if (hi + lo != offset)
4083         abort ();
4084
4085       if (hi != 0)
4086         {
4087           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4088
4089           /* Get the base address; addsi3 knows how to handle constants
4090              that require more than one insn */
4091           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4092           base = base_plus;
4093           offset = lo;
4094         }
4095     }
4096
4097   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4098   emit_insn (gen_zero_extendqisi2 (scratch,
4099                                    gen_rtx_MEM (QImode,
4100                                                 plus_constant (base,
4101                                                                offset))));
4102   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
4103                                    gen_rtx_MEM (QImode, 
4104                                                 plus_constant (base,
4105                                                                offset + 1))));
4106   if (! BYTES_BIG_ENDIAN)
4107     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4108                         gen_rtx_IOR (SImode, 
4109                                      gen_rtx_ASHIFT
4110                                      (SImode,
4111                                       gen_rtx_SUBREG (SImode, operands[0], 0),
4112                                       GEN_INT (8)),
4113                                      scratch)));
4114   else
4115     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4116                             gen_rtx_IOR (SImode, 
4117                                          gen_rtx_ASHIFT (SImode, scratch,
4118                                                          GEN_INT (8)),
4119                                          gen_rtx_SUBREG (SImode, operands[0],
4120                                                          0))));
4121 }
4122
4123 /* Handle storing a half-word to memory during reload by synthesising as two
4124    byte stores.  Take care not to clobber the input values until after we
4125    have moved them somewhere safe.  This code assumes that if the DImode
4126    scratch in operands[2] overlaps either the input value or output address
4127    in some way, then that value must die in this insn (we absolutely need
4128    two scratch registers for some corner cases).  */
4129 void
4130 arm_reload_out_hi (operands)
4131      rtx * operands;
4132 {
4133   rtx ref = operands[0];
4134   rtx outval = operands[1];
4135   rtx base, scratch;
4136   HOST_WIDE_INT offset = 0;
4137
4138   if (GET_CODE (ref) == SUBREG)
4139     {
4140       offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
4141       if (BYTES_BIG_ENDIAN)
4142         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
4143                    - MIN (UNITS_PER_WORD,
4144                           GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
4145       ref = SUBREG_REG (ref);
4146     }
4147
4148
4149   if (GET_CODE (ref) == REG)
4150     {
4151       /* We have a pseudo which has been spilt onto the stack; there
4152          are two cases here: the first where there is a simple
4153          stack-slot replacement and a second where the stack-slot is
4154          out of range, or is used as a subreg.  */
4155       if (reg_equiv_mem[REGNO (ref)])
4156         {
4157           ref = reg_equiv_mem[REGNO (ref)];
4158           base = find_replacement (&XEXP (ref, 0));
4159         }
4160       else
4161         /* The slot is out of range, or was dressed up in a SUBREG */
4162         base = reg_equiv_address[REGNO (ref)];
4163     }
4164   else
4165     base = find_replacement (&XEXP (ref, 0));
4166
4167   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4168
4169   /* Handle the case where the address is too complex to be offset by 1.  */
4170   if (GET_CODE (base) == MINUS
4171       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4172     {
4173       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4174
4175       /* Be careful not to destroy OUTVAL.  */
4176       if (reg_overlap_mentioned_p (base_plus, outval))
4177         {
4178           /* Updating base_plus might destroy outval, see if we can
4179              swap the scratch and base_plus.  */
4180           if (! reg_overlap_mentioned_p (scratch, outval))
4181             {
4182               rtx tmp = scratch;
4183               scratch = base_plus;
4184               base_plus = tmp;
4185             }
4186           else
4187             {
4188               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4189
4190               /* Be conservative and copy OUTVAL into the scratch now,
4191                  this should only be necessary if outval is a subreg
4192                  of something larger than a word.  */
4193               /* XXX Might this clobber base?  I can't see how it can,
4194                  since scratch is known to overlap with OUTVAL, and
4195                  must be wider than a word.  */
4196               emit_insn (gen_movhi (scratch_hi, outval));
4197               outval = scratch_hi;
4198             }
4199         }
4200
4201       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4202       base = base_plus;
4203     }
4204   else if (GET_CODE (base) == PLUS)
4205     {
4206       /* The addend must be CONST_INT, or we would have dealt with it above */
4207       HOST_WIDE_INT hi, lo;
4208
4209       offset += INTVAL (XEXP (base, 1));
4210       base = XEXP (base, 0);
4211
4212       /* Rework the address into a legal sequence of insns */
4213       /* Valid range for lo is -4095 -> 4095 */
4214       lo = (offset >= 0
4215             ? (offset & 0xfff)
4216             : -((-offset) & 0xfff));
4217
4218       /* Corner case, if lo is the max offset then we would be out of range
4219          once we have added the additional 1 below, so bump the msb into the
4220          pre-loading insn(s).  */
4221       if (lo == 4095)
4222         lo &= 0x7ff;
4223
4224       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xFFFFFFFFUL)
4225              ^ (HOST_WIDE_INT) 0x80000000UL)
4226             - (HOST_WIDE_INT) 0x80000000UL);
4227
4228       if (hi + lo != offset)
4229         abort ();
4230
4231       if (hi != 0)
4232         {
4233           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4234
4235           /* Be careful not to destroy OUTVAL.  */
4236           if (reg_overlap_mentioned_p (base_plus, outval))
4237             {
4238               /* Updating base_plus might destroy outval, see if we
4239                  can swap the scratch and base_plus.  */
4240               if (! reg_overlap_mentioned_p (scratch, outval))
4241                 {
4242                   rtx tmp = scratch;
4243                   scratch = base_plus;
4244                   base_plus = tmp;
4245                 }
4246               else
4247                 {
4248                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4249
4250                   /* Be conservative and copy outval into scratch now,
4251                      this should only be necessary if outval is a
4252                      subreg of something larger than a word.  */
4253                   /* XXX Might this clobber base?  I can't see how it
4254                      can, since scratch is known to overlap with
4255                      outval.  */
4256                   emit_insn (gen_movhi (scratch_hi, outval));
4257                   outval = scratch_hi;
4258                 }
4259             }
4260
4261           /* Get the base address; addsi3 knows how to handle constants
4262              that require more than one insn */
4263           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4264           base = base_plus;
4265           offset = lo;
4266         }
4267     }
4268
4269   if (BYTES_BIG_ENDIAN)
4270     {
4271       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
4272                                          plus_constant (base, offset + 1)),
4273                             gen_rtx_SUBREG (QImode, outval, 0)));
4274       emit_insn (gen_lshrsi3 (scratch,
4275                               gen_rtx_SUBREG (SImode, outval, 0),
4276                               GEN_INT (8)));
4277       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4278                             gen_rtx_SUBREG (QImode, scratch, 0)));
4279     }
4280   else
4281     {
4282       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4283                             gen_rtx_SUBREG (QImode, outval, 0)));
4284       emit_insn (gen_lshrsi3 (scratch,
4285                               gen_rtx_SUBREG (SImode, outval, 0),
4286                               GEN_INT (8)));
4287       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
4288                                          plus_constant (base, offset + 1)),
4289                             gen_rtx_SUBREG (QImode, scratch, 0)));
4290     }
4291 }
4292 \f
4293 /* Routines for manipulation of the constant pool.  */
4294
4295 /* Arm instructions cannot load a large constant directly into a
4296    register; they have to come from a pc relative load.  The constant
4297    must therefore be placed in the addressable range of the pc
4298    relative load.  Depending on the precise pc relative load
4299    instruction the range is somewhere between 256 bytes and 4k.  This
4300    means that we often have to dump a constant inside a function, and
4301    generate code to branch around it.
4302
4303    It is important to minimize this, since the branches will slow
4304    things down and make the code larger.
4305
4306    Normally we can hide the table after an existing unconditional
4307    branch so that there is no interruption of the flow, but in the
4308    worst case the code looks like this:
4309
4310         ldr     rn, L1
4311         ...
4312         b       L2
4313         align
4314         L1:     .long value
4315         L2:
4316         ...
4317
4318         ldr     rn, L3
4319         ...
4320         b       L4
4321         align
4322         L3:     .long value
4323         L4:
4324         ...
4325
4326    We fix this by performing a scan after scheduling, which notices
4327    which instructions need to have their operands fetched from the
4328    constant table and builds the table.
4329
4330    The algorithm starts by building a table of all the constants that
4331    need fixing up and all the natural barriers in the function (places
4332    where a constant table can be dropped without breaking the flow).
4333    For each fixup we note how far the pc-relative replacement will be
4334    able to reach and the offset of the instruction into the function.
4335
4336    Having built the table we then group the fixes together to form
4337    tables that are as large as possible (subject to addressing
4338    constraints) and emit each table of constants after the last
4339    barrier that is within range of all the instructions in the group.
4340    If a group does not contain a barrier, then we forcibly create one
4341    by inserting a jump instruction into the flow.  Once the table has
4342    been inserted, the insns are then modified to reference the
4343    relevant entry in the pool.
4344
4345    Possible enhancements to the alogorithm (not implemented) are:
4346
4347    1) ARM instructions (but not thumb) can use negative offsets, so we
4348    could reference back to a previous pool rather than forwards to a
4349    new one.  For large functions this may reduce the number of pools
4350    required.
4351
4352    2) For some processors and object formats, there may be benefit in
4353    aligning the pools to the start of cache lines; this alignment
4354    would need to be taken into account when calculating addressability
4355    of a pool.
4356
4357  */
4358
4359 typedef struct
4360 {
4361   rtx value;                    /* Value in table */
4362   HOST_WIDE_INT next_offset;
4363   enum machine_mode mode;       /* Mode of value */
4364 } minipool_node;
4365
4366 /* The maximum number of constants that can fit into one pool, since
4367    the pc relative range is 0...4092 bytes and constants are at least 4
4368    bytes long.  */
4369
4370 #define MAX_MINIPOOL_SIZE (4092/4)
4371 static minipool_node minipool_vector[MAX_MINIPOOL_SIZE];
4372 static int minipool_size;
4373 static rtx minipool_vector_label;
4374
4375 /* Add a constant to the pool and return its offset within the current
4376    pool.
4377
4378    X is the rtx we want to replace. MODE is its mode.  On return,
4379    ADDRESS_ONLY will be non-zero if we really want the address of such
4380    a constant, not the constant itself.  */
4381 static HOST_WIDE_INT
4382 add_minipool_constant (x, mode)
4383      rtx x;
4384      enum machine_mode mode;
4385 {
4386   int i;
4387   HOST_WIDE_INT offset;
4388   
4389   /* First, see if we've already got it.  */
4390   for (i = 0; i < minipool_size; i++)
4391     {
4392       if (GET_CODE (x) == minipool_vector[i].value->code
4393           && mode == minipool_vector[i].mode)
4394         {
4395           if (GET_CODE (x) == CODE_LABEL)
4396             {
4397               if (XINT (x, 3) != XINT (minipool_vector[i].value, 3))
4398                 continue;
4399             }
4400           if (rtx_equal_p (x, minipool_vector[i].value))
4401             return minipool_vector[i].next_offset - GET_MODE_SIZE (mode);
4402         }
4403     }
4404
4405   /* Need a new one */
4406   minipool_vector[minipool_size].next_offset = GET_MODE_SIZE (mode);
4407   offset = 0;
4408   if (minipool_size == 0)
4409     minipool_vector_label = gen_label_rtx ();
4410   else
4411     minipool_vector[minipool_size].next_offset
4412       += (offset = minipool_vector[minipool_size - 1].next_offset);
4413
4414   minipool_vector[minipool_size].value = x;
4415   minipool_vector[minipool_size].mode = mode;
4416   minipool_size++;
4417   return offset;
4418 }
4419
4420 /* Output the literal table */
4421 static void
4422 dump_minipool (scan)
4423      rtx scan;
4424 {
4425   int i;
4426
4427   scan = emit_label_after (gen_label_rtx (), scan);
4428   scan = emit_insn_after (gen_align_4 (), scan);
4429   scan = emit_label_after (minipool_vector_label, scan);
4430
4431   for (i = 0; i < minipool_size; i++)
4432     {
4433       minipool_node *p = minipool_vector + i;
4434
4435       switch (GET_MODE_SIZE (p->mode))
4436         {
4437         case 4:
4438           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
4439           break;
4440
4441         case 8:
4442           scan = emit_insn_after (gen_consttable_8 (p->value), scan);
4443           break;
4444
4445         default:
4446           abort ();
4447           break;
4448         }
4449     }
4450
4451   scan = emit_insn_after (gen_consttable_end (), scan);
4452   scan = emit_barrier_after (scan);
4453   minipool_size = 0;
4454 }
4455
4456 /* Find the last barrier less than MAX_COUNT bytes from FROM, or
4457    create one.  */
4458 static rtx
4459 find_barrier (from, max_count)
4460      rtx from;
4461      int max_count;
4462 {
4463   int count = 0;
4464   rtx found_barrier = 0;
4465   rtx last = from;
4466
4467   while (from && count < max_count)
4468     {
4469       rtx tmp;
4470       
4471       if (GET_CODE (from) == BARRIER)
4472         found_barrier = from;
4473
4474       /* Count the length of this insn */
4475       if (GET_CODE (from) == JUMP_INSN
4476           && JUMP_LABEL (from) != 0
4477           && ((tmp = next_real_insn (JUMP_LABEL (from)))
4478               == next_real_insn (from))
4479           && tmp != NULL
4480           && GET_CODE (tmp) == JUMP_INSN
4481           && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
4482               || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
4483         {
4484           int elt = GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC ? 1 : 0;
4485           count += (get_attr_length (from)
4486                     + GET_MODE_SIZE (SImode) * XVECLEN (PATTERN (tmp), elt));
4487           /* Continue after the dispatch table.  */
4488           last = from;
4489           from = NEXT_INSN (tmp);
4490           continue;
4491         }
4492       else
4493         count += get_attr_length (from);
4494
4495       last = from;
4496       from = NEXT_INSN (from);
4497     }
4498
4499   if (! found_barrier)
4500     {
4501       /* We didn't find a barrier in time to
4502          dump our stuff, so we'll make one.  */
4503       rtx label = gen_label_rtx ();
4504       
4505       if (from)
4506         from = PREV_INSN (last);
4507       else
4508         from = get_last_insn ();
4509       
4510       /* Walk back to be just before any jump.  */
4511       while (GET_CODE (from) == JUMP_INSN
4512              || GET_CODE (from) == NOTE
4513              || GET_CODE (from) == CODE_LABEL)
4514         from = PREV_INSN (from);
4515       
4516       from = emit_jump_insn_after (gen_jump (label), from);
4517       JUMP_LABEL (from) = label;
4518       found_barrier = emit_barrier_after (from);
4519       emit_label_after (label, found_barrier);
4520     }
4521
4522   return found_barrier;
4523 }
4524
4525 struct minipool_fixup
4526 {
4527   struct minipool_fixup *next;
4528   rtx insn;
4529   int address;
4530   rtx *loc;
4531   enum machine_mode mode;
4532   rtx value;
4533   int range;
4534 };
4535   
4536 struct minipool_fixup *minipool_fix_head;
4537 struct minipool_fixup *minipool_fix_tail;
4538
4539 static void
4540 push_minipool_barrier (insn, address)
4541      rtx insn;
4542      int address;
4543 {
4544   struct minipool_fixup *fix
4545     = (struct minipool_fixup *) oballoc (sizeof (struct minipool_fixup));
4546
4547   fix->insn = insn;
4548   fix->address = address;
4549
4550   fix->next = NULL;
4551   if (minipool_fix_head != NULL)
4552     minipool_fix_tail->next = fix;
4553   else
4554     minipool_fix_head = fix;
4555
4556   minipool_fix_tail = fix;
4557 }
4558
4559 static void
4560 push_minipool_fix (insn, address, loc, mode, value)
4561      rtx insn;
4562      int address;
4563      rtx *loc;
4564      enum machine_mode mode;
4565      rtx value;
4566 {
4567   struct minipool_fixup *fix
4568     = (struct minipool_fixup *) oballoc (sizeof (struct minipool_fixup));
4569
4570 #ifdef AOF_ASSEMBLER
4571   /* PIC symbol refereneces need to be converted into offsets into the
4572      based area.  */
4573   if (flag_pic && GET_MODE == SYMBOL_REF)
4574     value = aof_pic_entry (value);
4575 #endif /* AOF_ASSEMBLER */
4576
4577   fix->insn = insn;
4578   fix->address = address;
4579   fix->loc = loc;
4580   fix->mode = mode;
4581   fix->value = value;
4582   fix->range = get_attr_pool_range (insn);
4583
4584   /* If an insn doesn't have a range defined for it, then it isn't
4585      expecting to be reworked by this code.  Better to abort now than
4586      to generate duff assembly code.  */
4587   if (fix->range == 0)
4588     abort ();
4589
4590   /* Add it to the chain of fixes */
4591   fix->next = NULL;
4592   if (minipool_fix_head != NULL)
4593     minipool_fix_tail->next = fix;
4594   else
4595     minipool_fix_head = fix;
4596
4597   minipool_fix_tail = fix;
4598 }
4599
4600 static void
4601 note_invalid_constants (insn, address)
4602      rtx insn;
4603      int address;
4604 {
4605   int opno;
4606
4607   /* Extract the operands of the insn */
4608   extract_insn(insn);
4609
4610   /* Find the alternative selected */
4611   if (! constrain_operands (1))
4612     fatal_insn_not_found (insn);
4613
4614   /* Preprocess the constraints, to extract some useful information.  */
4615   preprocess_constraints ();
4616
4617   for (opno = 0; opno < recog_data.n_operands; opno++)
4618     {
4619       /* Things we need to fix can only occur in inputs */
4620       if (recog_data.operand_type[opno] != OP_IN)
4621         continue;
4622
4623       /* If this alternative is a memory reference, then any mention
4624          of constants in this alternative is really to fool reload
4625          into allowing us to accept one there.  We need to fix them up
4626          now so that we output the right code.  */
4627       if (recog_op_alt[opno][which_alternative].memory_ok)
4628         {
4629           rtx op = recog_data.operand[opno];
4630
4631           if (CONSTANT_P (op))
4632             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4633                                recog_data.operand_mode[opno], op);
4634 #ifndef AOF_ASSEMBLER
4635           else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == 3)
4636             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4637                                recog_data.operand_mode[opno],
4638                                XVECEXP (op, 0, 0));
4639 #endif
4640           else if (recog_data.operand_mode[opno] == SImode
4641                    && GET_CODE (op) == MEM
4642                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
4643                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
4644             push_minipool_fix (insn, address, recog_data.operand_loc[opno],
4645                                recog_data.operand_mode[opno],
4646                                get_pool_constant (XEXP (op, 0)));
4647         }
4648     }
4649 }
4650
4651 void
4652 arm_reorg (first)
4653      rtx first;
4654 {
4655   rtx insn;
4656   int address = 0;
4657   struct minipool_fixup *fix;
4658
4659   minipool_fix_head = minipool_fix_tail = NULL;
4660
4661   /* The first insn must always be a note, or the code below won't
4662      scan it properly.  */
4663   if (GET_CODE (first) != NOTE)
4664     abort ();
4665
4666   /* Scan all the insns and record the operands that will need fixing.  */
4667   for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
4668     {
4669
4670       if (GET_CODE (insn) == BARRIER)
4671         push_minipool_barrier(insn, address);
4672       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
4673                || GET_CODE (insn) == JUMP_INSN)
4674         {
4675           rtx table;
4676
4677           note_invalid_constants (insn, address);
4678           address += get_attr_length (insn);
4679           /* If the insn is a vector jump, add the size of the table
4680              and skip the table.  */
4681           if (GET_CODE (insn) == JUMP_INSN
4682               && JUMP_LABEL (insn) != NULL
4683               && ((table = next_real_insn (JUMP_LABEL (insn)))
4684                   == next_real_insn (insn))
4685               && table != NULL
4686               && GET_CODE (table) == JUMP_INSN
4687               && (GET_CODE (PATTERN (table)) == ADDR_VEC
4688                   || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
4689             {
4690               int elt = GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC ? 1 : 0;
4691
4692               address += GET_MODE_SIZE (SImode) * XVECLEN (PATTERN (table), 
4693                                                            elt);
4694               insn = table;
4695             }
4696         }
4697     }
4698
4699   /* Now scan the fixups and perform the required changes.  */
4700   for (fix = minipool_fix_head; fix; fix = fix->next)
4701     {
4702       struct minipool_fixup *ftmp;
4703       struct minipool_fixup *last_barrier = NULL;
4704       int max_range;
4705       rtx barrier;
4706       struct minipool_fixup *this_fix;
4707       int new_minipool_size = 0;
4708
4709       /* Skip any further barriers before the next fix.  */
4710       while (fix && GET_CODE (fix->insn) == BARRIER)
4711         fix = fix->next;
4712
4713       if (fix == NULL)
4714         break;
4715
4716       ftmp = fix;
4717       max_range = fix->address + fix->range;
4718
4719       /* Find all the other fixes that can live in the same pool.  */
4720       while (ftmp->next && ftmp->next->address < max_range
4721              && (GET_CODE (ftmp->next->insn) == BARRIER
4722                  /* Ensure we can reach the constant inside the pool.  */
4723                  || ftmp->next->range > new_minipool_size))
4724         {
4725           ftmp = ftmp->next;
4726           if (GET_CODE (ftmp->insn) == BARRIER)
4727             last_barrier = ftmp;
4728           else
4729             {
4730               /* Does this fix constrain the range we can search?  */
4731               if (ftmp->address + ftmp->range - new_minipool_size < max_range)
4732                 max_range = ftmp->address + ftmp->range - new_minipool_size;
4733
4734               new_minipool_size += GET_MODE_SIZE (ftmp->mode);
4735             }
4736         }
4737
4738       /* If we found a barrier, drop back to that; any fixes that we could
4739          have reached but come after the barrier will now go in the next
4740          mini-pool.  */
4741       if (last_barrier != NULL)
4742         {
4743           barrier = last_barrier->insn;
4744           ftmp = last_barrier;
4745         }
4746       /* ftmp is last fix that we can fit into this pool and we
4747          failed to find a barrier that we could use.  Insert a new
4748          barrier in the code and arrange to jump around it.  */
4749       else
4750         {
4751           /* Check that there isn't another fix that is in range that
4752              we couldn't fit into this pool because the pool was
4753              already too large: we need to put the pool before such an
4754              instruction.  */
4755           if (ftmp->next && ftmp->next->address < max_range)
4756             max_range = ftmp->address;
4757
4758           barrier = find_barrier (ftmp->insn, max_range - ftmp->address);
4759         }
4760
4761       /* Scan over the fixes we have identified for this pool, fixing them
4762          up and adding the constants to the pool itself.  */
4763       for (this_fix = fix; this_fix && ftmp->next != this_fix;
4764            this_fix = this_fix->next)
4765         if (GET_CODE (this_fix->insn) != BARRIER)
4766           {
4767             int offset = add_minipool_constant (this_fix->value,
4768                                                 this_fix->mode);
4769             rtx addr
4770               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
4771                                                   minipool_vector_label),
4772                                offset);
4773             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
4774           }
4775
4776       dump_minipool (barrier);
4777       fix = ftmp;
4778     }
4779
4780   /* From now on we must synthesize any constants that we can't handle
4781      directly.  This can happen if the RTL gets split during final
4782      instruction generation.  */
4783   after_arm_reorg = 1;
4784 }
4785
4786 \f
4787 /* Routines to output assembly language.  */
4788
4789 /* If the rtx is the correct value then return the string of the number.
4790    In this way we can ensure that valid double constants are generated even
4791    when cross compiling. */
4792 char *
4793 fp_immediate_constant (x)
4794      rtx x;
4795 {
4796   REAL_VALUE_TYPE r;
4797   int i;
4798   
4799   if (!fpa_consts_inited)
4800     init_fpa_table ();
4801   
4802   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4803   for (i = 0; i < 8; i++)
4804     if (REAL_VALUES_EQUAL (r, values_fpa[i]))
4805       return strings_fpa[i];
4806
4807   abort ();
4808 }
4809
4810 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
4811 static char *
4812 fp_const_from_val (r)
4813      REAL_VALUE_TYPE * r;
4814 {
4815   int i;
4816
4817   if (! fpa_consts_inited)
4818     init_fpa_table ();
4819
4820   for (i = 0; i < 8; i++)
4821     if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
4822       return strings_fpa[i];
4823
4824   abort ();
4825 }
4826
4827 /* Output the operands of a LDM/STM instruction to STREAM.
4828    MASK is the ARM register set mask of which only bits 0-15 are important.
4829    INSTR is the possibly suffixed base register.  HAT unequals zero if a hat
4830    must follow the register list.  */
4831
4832 void
4833 print_multi_reg (stream, instr, reg, mask, hat)
4834      FILE * stream;
4835      char * instr;
4836      int reg;
4837      int mask;
4838      int hat;
4839 {
4840   int i;
4841   int not_first = FALSE;
4842
4843   fputc ('\t', stream);
4844   asm_fprintf (stream, instr, reg);
4845   fputs (", {", stream);
4846   
4847   for (i = 0; i < 16; i++)
4848     if (mask & (1 << i))
4849       {
4850         if (not_first)
4851           fprintf (stream, ", ");
4852         
4853         asm_fprintf (stream, "%r", i);
4854         not_first = TRUE;
4855       }
4856
4857   fprintf (stream, "}%s\n", hat ? "^" : "");
4858 }
4859
4860 /* Output a 'call' insn. */
4861
4862 char *
4863 output_call (operands)
4864      rtx * operands;
4865 {
4866   /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
4867
4868   if (REGNO (operands[0]) == LR_REGNUM)
4869     {
4870       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
4871       output_asm_insn ("mov%?\t%0, %|lr", operands);
4872     }
4873   
4874   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4875   
4876   if (TARGET_INTERWORK)
4877     output_asm_insn ("bx%?\t%0", operands);
4878   else
4879     output_asm_insn ("mov%?\t%|pc, %0", operands);
4880   
4881   return "";
4882 }
4883
4884 static int
4885 eliminate_lr2ip (x)
4886      rtx * x;
4887 {
4888   int something_changed = 0;
4889   rtx x0 = * x;
4890   int code = GET_CODE (x0);
4891   register int i, j;
4892   register const char * fmt;
4893   
4894   switch (code)
4895     {
4896     case REG:
4897       if (REGNO (x0) == LR_REGNUM)
4898         {
4899           *x = gen_rtx_REG (SImode, IP_REGNUM);
4900           return 1;
4901         }
4902       return 0;
4903     default:
4904       /* Scan through the sub-elements and change any references there */
4905       fmt = GET_RTX_FORMAT (code);
4906       
4907       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4908         if (fmt[i] == 'e')
4909           something_changed |= eliminate_lr2ip (&XEXP (x0, i));
4910         else if (fmt[i] == 'E')
4911           for (j = 0; j < XVECLEN (x0, i); j++)
4912             something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
4913       
4914       return something_changed;
4915     }
4916 }
4917   
4918 /* Output a 'call' insn that is a reference in memory. */
4919
4920 char *
4921 output_call_mem (operands)
4922      rtx * operands;
4923 {
4924   operands[0] = copy_rtx (operands[0]); /* Be ultra careful */
4925   /* Handle calls using lr by using ip (which may be clobbered in subr anyway).
4926    */
4927   if (eliminate_lr2ip (&operands[0]))
4928     output_asm_insn ("mov%?\t%|ip, %|lr", operands);
4929
4930   if (TARGET_INTERWORK)
4931     {
4932       output_asm_insn ("ldr%?\t%|ip, %0", operands);
4933       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4934       output_asm_insn ("bx%?\t%|ip", operands);
4935     }
4936   else
4937     {
4938       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
4939       output_asm_insn ("ldr%?\t%|pc, %0", operands);
4940     }
4941
4942   return "";
4943 }
4944
4945
4946 /* Output a move from arm registers to an fpu registers.
4947    OPERANDS[0] is an fpu register.
4948    OPERANDS[1] is the first registers of an arm register pair.  */
4949
4950 char *
4951 output_mov_long_double_fpu_from_arm (operands)
4952      rtx * operands;
4953 {
4954   int arm_reg0 = REGNO (operands[1]);
4955   rtx ops[3];
4956
4957   if (arm_reg0 == IP_REGNUM)
4958     abort ();
4959
4960   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4961   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4962   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4963   
4964   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
4965   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
4966   
4967   return "";
4968 }
4969
4970 /* Output a move from an fpu register to arm registers.
4971    OPERANDS[0] is the first registers of an arm register pair.
4972    OPERANDS[1] is an fpu register.  */
4973
4974 char *
4975 output_mov_long_double_arm_from_fpu (operands)
4976      rtx * operands;
4977 {
4978   int arm_reg0 = REGNO (operands[0]);
4979   rtx ops[3];
4980
4981   if (arm_reg0 == IP_REGNUM)
4982     abort ();
4983
4984   ops[0] = gen_rtx_REG (SImode, arm_reg0);
4985   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
4986   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
4987
4988   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
4989   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
4990   return "";
4991 }
4992
4993 /* Output a move from arm registers to arm registers of a long double
4994    OPERANDS[0] is the destination.
4995    OPERANDS[1] is the source.  */
4996 char *
4997 output_mov_long_double_arm_from_arm (operands)
4998      rtx * operands;
4999 {
5000   /* We have to be careful here because the two might overlap */
5001   int dest_start = REGNO (operands[0]);
5002   int src_start = REGNO (operands[1]);
5003   rtx ops[2];
5004   int i;
5005
5006   if (dest_start < src_start)
5007     {
5008       for (i = 0; i < 3; i++)
5009         {
5010           ops[0] = gen_rtx_REG (SImode, dest_start + i);
5011           ops[1] = gen_rtx_REG (SImode, src_start + i);
5012           output_asm_insn ("mov%?\t%0, %1", ops);
5013         }
5014     }
5015   else
5016     {
5017       for (i = 2; i >= 0; i--)
5018         {
5019           ops[0] = gen_rtx_REG (SImode, dest_start + i);
5020           ops[1] = gen_rtx_REG (SImode, src_start + i);
5021           output_asm_insn ("mov%?\t%0, %1", ops);
5022         }
5023     }
5024
5025   return "";
5026 }
5027
5028
5029 /* Output a move from arm registers to an fpu registers.
5030    OPERANDS[0] is an fpu register.
5031    OPERANDS[1] is the first registers of an arm register pair.  */
5032
5033 char *
5034 output_mov_double_fpu_from_arm (operands)
5035      rtx * operands;
5036 {
5037   int arm_reg0 = REGNO (operands[1]);
5038   rtx ops[2];
5039
5040   if (arm_reg0 == IP_REGNUM)
5041     abort ();
5042   
5043   ops[0] = gen_rtx_REG (SImode, arm_reg0);
5044   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5045   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
5046   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
5047   return "";
5048 }
5049
5050 /* Output a move from an fpu register to arm registers.
5051    OPERANDS[0] is the first registers of an arm register pair.
5052    OPERANDS[1] is an fpu register.  */
5053
5054 char *
5055 output_mov_double_arm_from_fpu (operands)
5056      rtx * operands;
5057 {
5058   int arm_reg0 = REGNO (operands[0]);
5059   rtx ops[2];
5060
5061   if (arm_reg0 == IP_REGNUM)
5062     abort ();
5063
5064   ops[0] = gen_rtx_REG (SImode, arm_reg0);
5065   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5066   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
5067   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
5068   return "";
5069 }
5070
5071 /* Output a move between double words.
5072    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
5073    or MEM<-REG and all MEMs must be offsettable addresses.  */
5074
5075 char *
5076 output_move_double (operands)
5077      rtx * operands;
5078 {
5079   enum rtx_code code0 = GET_CODE (operands[0]);
5080   enum rtx_code code1 = GET_CODE (operands[1]);
5081   rtx otherops[3];
5082
5083   if (code0 == REG)
5084     {
5085       int reg0 = REGNO (operands[0]);
5086
5087       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
5088       
5089       if (code1 == REG)
5090         {
5091           int reg1 = REGNO (operands[1]);
5092           if (reg1 == IP_REGNUM)
5093             abort ();
5094
5095           /* Ensure the second source is not overwritten */
5096           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
5097             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
5098           else
5099             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
5100         }
5101       else if (code1 == CONST_DOUBLE)
5102         {
5103           if (GET_MODE (operands[1]) == DFmode)
5104             {
5105               long l[2];
5106               union real_extract u;
5107
5108               bcopy ((char *) &CONST_DOUBLE_LOW (operands[1]), (char *) &u,
5109                      sizeof (u));
5110               REAL_VALUE_TO_TARGET_DOUBLE (u.d, l);
5111               otherops[1] = GEN_INT(l[1]);
5112               operands[1] = GEN_INT(l[0]);
5113             }
5114           else if (GET_MODE (operands[1]) != VOIDmode)
5115             abort ();
5116           else if (WORDS_BIG_ENDIAN)
5117             {
5118               
5119               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5120               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5121             }
5122           else
5123             {
5124               
5125               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5126               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5127             }
5128           
5129           output_mov_immediate (operands);
5130           output_mov_immediate (otherops);
5131         }
5132       else if (code1 == CONST_INT)
5133         {
5134 #if HOST_BITS_PER_WIDE_INT > 32
5135           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
5136              what the upper word is.  */
5137           if (WORDS_BIG_ENDIAN)
5138             {
5139               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5140               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5141             }
5142           else
5143             {
5144               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5145               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5146             }
5147 #else
5148           /* Sign extend the intval into the high-order word */
5149           if (WORDS_BIG_ENDIAN)
5150             {
5151               otherops[1] = operands[1];
5152               operands[1] = (INTVAL (operands[1]) < 0
5153                              ? constm1_rtx : const0_rtx);
5154             }
5155           else
5156             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
5157 #endif
5158           output_mov_immediate (otherops);
5159           output_mov_immediate (operands);
5160         }
5161       else if (code1 == MEM)
5162         {
5163           switch (GET_CODE (XEXP (operands[1], 0)))
5164             {
5165             case REG:
5166               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
5167               break;
5168
5169             case PRE_INC:
5170               abort (); /* Should never happen now */
5171               break;
5172
5173             case PRE_DEC:
5174               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
5175               break;
5176
5177             case POST_INC:
5178               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
5179               break;
5180
5181             case POST_DEC:
5182               abort (); /* Should never happen now */
5183               break;
5184
5185             case LABEL_REF:
5186             case CONST:
5187               output_asm_insn ("adr%?\t%0, %1", operands);
5188               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
5189               break;
5190
5191             default:
5192               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
5193                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
5194                 {
5195                   otherops[0] = operands[0];
5196                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
5197                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
5198                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
5199                     {
5200                       if (GET_CODE (otherops[2]) == CONST_INT)
5201                         {
5202                           switch (INTVAL (otherops[2]))
5203                             {
5204                             case -8:
5205                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
5206                               return "";
5207                             case -4:
5208                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
5209                               return "";
5210                             case 4:
5211                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
5212                               return "";
5213                             }
5214                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
5215                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
5216                           else
5217                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
5218                         }
5219                       else
5220                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
5221                     }
5222                   else
5223                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
5224                   
5225                   return "ldm%?ia\t%0, %M0";
5226                 }
5227               else
5228                 {
5229                   otherops[1] = adj_offsettable_operand (operands[1], 4);
5230                   /* Take care of overlapping base/data reg.  */
5231                   if (reg_mentioned_p (operands[0], operands[1]))
5232                     {
5233                       output_asm_insn ("ldr%?\t%0, %1", otherops);
5234                       output_asm_insn ("ldr%?\t%0, %1", operands);
5235                     }
5236                   else
5237                     {
5238                       output_asm_insn ("ldr%?\t%0, %1", operands);
5239                       output_asm_insn ("ldr%?\t%0, %1", otherops);
5240                     }
5241                 }
5242             }
5243         }
5244       else
5245         abort ();  /* Constraints should prevent this */
5246     }
5247   else if (code0 == MEM && code1 == REG)
5248     {
5249       if (REGNO (operands[1]) == IP_REGNUM)
5250         abort ();
5251
5252       switch (GET_CODE (XEXP (operands[0], 0)))
5253         {
5254         case REG:
5255           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
5256           break;
5257
5258         case PRE_INC:
5259           abort (); /* Should never happen now */
5260           break;
5261
5262         case PRE_DEC:
5263           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
5264           break;
5265
5266         case POST_INC:
5267           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
5268           break;
5269
5270         case POST_DEC:
5271           abort (); /* Should never happen now */
5272           break;
5273
5274         case PLUS:
5275           if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
5276             {
5277               switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
5278                 {
5279                 case -8:
5280                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
5281                   return "";
5282
5283                 case -4:
5284                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
5285                   return "";
5286
5287                 case 4:
5288                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
5289                   return "";
5290                 }
5291             }
5292           /* Fall through */
5293
5294         default:
5295           otherops[0] = adj_offsettable_operand (operands[0], 4);
5296           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
5297           output_asm_insn ("str%?\t%1, %0", operands);
5298           output_asm_insn ("str%?\t%1, %0", otherops);
5299         }
5300     }
5301   else
5302     abort ();  /* Constraints should prevent this */
5303
5304   return "";
5305 }
5306
5307
5308 /* Output an arbitrary MOV reg, #n.
5309    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
5310
5311 char *
5312 output_mov_immediate (operands)
5313      rtx * operands;
5314 {
5315   HOST_WIDE_INT n = INTVAL (operands[1]);
5316   int n_ones = 0;
5317   int i;
5318
5319   /* Try to use one MOV */
5320   if (const_ok_for_arm (n))
5321     {
5322       output_asm_insn ("mov%?\t%0, %1", operands);
5323       return "";
5324     }
5325
5326   /* Try to use one MVN */
5327   if (const_ok_for_arm (~n))
5328     {
5329       operands[1] = GEN_INT (~n);
5330       output_asm_insn ("mvn%?\t%0, %1", operands);
5331       return "";
5332     }
5333
5334   /* If all else fails, make it out of ORRs or BICs as appropriate. */
5335
5336   for (i=0; i < 32; i++)
5337     if (n & 1 << i)
5338       n_ones++;
5339
5340   if (n_ones > 16)  /* Shorter to use MVN with BIC in this case. */
5341     output_multi_immediate(operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1,
5342                            ~n);
5343   else
5344     output_multi_immediate(operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1,
5345                            n);
5346
5347   return "";
5348 }
5349
5350
5351 /* Output an ADD r, s, #n where n may be too big for one instruction.  If
5352    adding zero to one register, output nothing.  */
5353
5354 char *
5355 output_add_immediate (operands)
5356      rtx * operands;
5357 {
5358   HOST_WIDE_INT n = INTVAL (operands[2]);
5359
5360   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
5361     {
5362       if (n < 0)
5363         output_multi_immediate (operands,
5364                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
5365                                 -n);
5366       else
5367         output_multi_immediate (operands,
5368                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
5369                                 n);
5370     }
5371
5372   return "";
5373 }
5374
5375 /* Output a multiple immediate operation.
5376    OPERANDS is the vector of operands referred to in the output patterns.
5377    INSTR1 is the output pattern to use for the first constant.
5378    INSTR2 is the output pattern to use for subsequent constants.
5379    IMMED_OP is the index of the constant slot in OPERANDS.
5380    N is the constant value.  */
5381
5382 static char *
5383 output_multi_immediate (operands, instr1, instr2, immed_op, n)
5384      rtx * operands;
5385      char * instr1, * instr2;
5386      int immed_op;
5387      HOST_WIDE_INT n;
5388 {
5389 #if HOST_BITS_PER_WIDE_INT > 32
5390   n &= 0xffffffff;
5391 #endif
5392
5393   if (n == 0)
5394     {
5395       operands[immed_op] = const0_rtx;
5396       output_asm_insn (instr1, operands); /* Quick and easy output */
5397     }
5398   else
5399     {
5400       int i;
5401       char *instr = instr1;
5402
5403       /* Note that n is never zero here (which would give no output) */
5404       for (i = 0; i < 32; i += 2)
5405         {
5406           if (n & (3 << i))
5407             {
5408               operands[immed_op] = GEN_INT (n & (255 << i));
5409               output_asm_insn (instr, operands);
5410               instr = instr2;
5411               i += 6;
5412             }
5413         }
5414     }
5415   return "";
5416 }
5417
5418
5419 /* Return the appropriate ARM instruction for the operation code.
5420    The returned result should not be overwritten.  OP is the rtx of the
5421    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
5422    was shifted.  */
5423
5424 char *
5425 arithmetic_instr (op, shift_first_arg)
5426      rtx op;
5427      int shift_first_arg;
5428 {
5429   switch (GET_CODE (op))
5430     {
5431     case PLUS:
5432       return "add";
5433
5434     case MINUS:
5435       return shift_first_arg ? "rsb" : "sub";
5436
5437     case IOR:
5438       return "orr";
5439
5440     case XOR:
5441       return "eor";
5442
5443     case AND:
5444       return "and";
5445
5446     default:
5447       abort ();
5448     }
5449 }
5450
5451
5452 /* Ensure valid constant shifts and return the appropriate shift mnemonic
5453    for the operation code.  The returned result should not be overwritten.
5454    OP is the rtx code of the shift.
5455    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
5456    shift. */
5457
5458 static char *
5459 shift_op (op, amountp)
5460      rtx op;
5461      HOST_WIDE_INT *amountp;
5462 {
5463   char * mnem;
5464   enum rtx_code code = GET_CODE (op);
5465
5466   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
5467     *amountp = -1;
5468   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
5469     *amountp = INTVAL (XEXP (op, 1));
5470   else
5471     abort ();
5472
5473   switch (code)
5474     {
5475     case ASHIFT:
5476       mnem = "asl";
5477       break;
5478
5479     case ASHIFTRT:
5480       mnem = "asr";
5481       break;
5482
5483     case LSHIFTRT:
5484       mnem = "lsr";
5485       break;
5486
5487     case ROTATERT:
5488       mnem = "ror";
5489       break;
5490
5491     case MULT:
5492       /* We never have to worry about the amount being other than a
5493          power of 2, since this case can never be reloaded from a reg.  */
5494       if (*amountp != -1)
5495         *amountp = int_log2 (*amountp);
5496       else
5497         abort ();
5498       return "asl";
5499
5500     default:
5501       abort ();
5502     }
5503
5504   if (*amountp != -1)
5505     {
5506       /* This is not 100% correct, but follows from the desire to merge
5507          multiplication by a power of 2 with the recognizer for a
5508          shift.  >=32 is not a valid shift for "asl", so we must try and
5509          output a shift that produces the correct arithmetical result.
5510          Using lsr #32 is identical except for the fact that the carry bit
5511          is not set correctly if we set the flags; but we never use the 
5512          carry bit from such an operation, so we can ignore that.  */
5513       if (code == ROTATERT)
5514         *amountp &= 31;         /* Rotate is just modulo 32 */
5515       else if (*amountp != (*amountp & 31))
5516         {
5517           if (code == ASHIFT)
5518             mnem = "lsr";
5519           *amountp = 32;
5520         }
5521
5522       /* Shifts of 0 are no-ops.  */
5523       if (*amountp == 0)
5524         return NULL;
5525     }     
5526
5527   return mnem;
5528 }
5529
5530
5531 /* Obtain the shift from the POWER of two. */
5532
5533 static HOST_WIDE_INT
5534 int_log2 (power)
5535      HOST_WIDE_INT power;
5536 {
5537   HOST_WIDE_INT shift = 0;
5538
5539   while (((((HOST_WIDE_INT) 1) << shift) & power) == 0)
5540     {
5541       if (shift > 31)
5542         abort ();
5543       shift++;
5544     }
5545
5546   return shift;
5547 }
5548
5549 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
5550    /bin/as is horribly restrictive.  */
5551 #define MAX_ASCII_LEN 51
5552
5553 void
5554 output_ascii_pseudo_op (stream, p, len)
5555      FILE * stream;
5556      const unsigned char * p;
5557      int len;
5558 {
5559   int i;
5560   int len_so_far = 0;
5561
5562   fputs ("\t.ascii\t\"", stream);
5563   
5564   for (i = 0; i < len; i++)
5565     {
5566       register int c = p[i];
5567
5568       if (len_so_far >= MAX_ASCII_LEN)
5569         {
5570           fputs ("\"\n\t.ascii\t\"", stream);
5571           len_so_far = 0;
5572         }
5573
5574       switch (c)
5575         {
5576         case TARGET_TAB:                
5577           fputs ("\\t", stream);
5578           len_so_far += 2;                      
5579           break;
5580           
5581         case TARGET_FF:
5582           fputs ("\\f", stream);
5583           len_so_far += 2;
5584           break;
5585           
5586         case TARGET_BS:
5587           fputs ("\\b", stream);
5588           len_so_far += 2;
5589           break;
5590           
5591         case TARGET_CR:
5592           fputs ("\\r", stream);
5593           len_so_far += 2;
5594           break;
5595           
5596         case TARGET_NEWLINE:
5597           fputs ("\\n", stream);
5598           c = p [i + 1];
5599           if ((c >= ' ' && c <= '~')
5600               || c == TARGET_TAB)
5601             /* This is a good place for a line break.  */
5602             len_so_far = MAX_ASCII_LEN;
5603           else
5604             len_so_far += 2;
5605           break;
5606           
5607         case '\"':
5608         case '\\':
5609           putc ('\\', stream);
5610           len_so_far ++;
5611           /* drop through.  */
5612
5613         default:
5614           if (c >= ' ' && c <= '~')
5615             {
5616               putc (c, stream);
5617               len_so_far ++;
5618             }
5619           else
5620             {
5621               fprintf (stream, "\\%03o", c);
5622               len_so_far += 4;
5623             }
5624           break;
5625         }
5626     }
5627
5628   fputs ("\"\n", stream);
5629 }
5630 \f
5631
5632 /* Try to determine whether a pattern really clobbers the link register.
5633    This information is useful when peepholing, so that lr need not be pushed
5634    if we combine a call followed by a return.
5635    NOTE: This code does not check for side-effect expressions in a SET_SRC:
5636    such a check should not be needed because these only update an existing
5637    value within a register; the register must still be set elsewhere within
5638    the function. */
5639
5640 static int
5641 pattern_really_clobbers_lr (x)
5642      rtx x;
5643 {
5644   int i;
5645   
5646   switch (GET_CODE (x))
5647     {
5648     case SET:
5649       switch (GET_CODE (SET_DEST (x)))
5650         {
5651         case REG:
5652           return REGNO (SET_DEST (x)) == LR_REGNUM;
5653
5654         case SUBREG:
5655           if (GET_CODE (XEXP (SET_DEST (x), 0)) == REG)
5656             return REGNO (XEXP (SET_DEST (x), 0)) == LR_REGNUM;
5657
5658           if (GET_CODE (XEXP (SET_DEST (x), 0)) == MEM)
5659             return 0;
5660           abort ();
5661
5662         default:
5663           return 0;
5664         }
5665
5666     case PARALLEL:
5667       for (i = 0; i < XVECLEN (x, 0); i++)
5668         if (pattern_really_clobbers_lr (XVECEXP (x, 0, i)))
5669           return 1;
5670       return 0;
5671
5672     case CLOBBER:
5673       switch (GET_CODE (XEXP (x, 0)))
5674         {
5675         case REG:
5676           return REGNO (XEXP (x, 0)) == LR_REGNUM;
5677
5678         case SUBREG:
5679           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG)
5680             return REGNO (XEXP (XEXP (x, 0), 0)) == LR_REGNUM;
5681           abort ();
5682
5683         default:
5684           return 0;
5685         }
5686
5687     case UNSPEC:
5688       return 1;
5689
5690     default:
5691       return 0;
5692     }
5693 }
5694
5695 static int
5696 function_really_clobbers_lr (first)
5697      rtx first;
5698 {
5699   rtx insn, next;
5700   
5701   for (insn = first; insn; insn = next_nonnote_insn (insn))
5702     {
5703       switch (GET_CODE (insn))
5704         {
5705         case BARRIER:
5706         case NOTE:
5707         case CODE_LABEL:
5708         case JUMP_INSN:         /* Jump insns only change the PC (and conds) */
5709           break;
5710
5711         case INSN:
5712           if (pattern_really_clobbers_lr (PATTERN (insn)))
5713             return 1;
5714           break;
5715
5716         case CALL_INSN:
5717           /* Don't yet know how to handle those calls that are not to a 
5718              SYMBOL_REF */
5719           if (GET_CODE (PATTERN (insn)) != PARALLEL)
5720             abort ();
5721
5722           switch (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)))
5723             {
5724             case CALL:
5725               if (GET_CODE (XEXP (XEXP (XVECEXP (PATTERN (insn), 0, 0), 0), 0))
5726                   != SYMBOL_REF)
5727                 return 1;
5728               break;
5729
5730             case SET:
5731               if (GET_CODE (XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn),
5732                                                           0, 0)), 0), 0))
5733                   != SYMBOL_REF)
5734                 return 1;
5735               break;
5736
5737             default:    /* Don't recognize it, be safe */
5738               return 1;
5739             }
5740
5741           /* A call can be made (by peepholing) not to clobber lr iff it is
5742              followed by a return.  There may, however, be a use insn iff
5743              we are returning the result of the call. 
5744              If we run off the end of the insn chain, then that means the
5745              call was at the end of the function.  Unfortunately we don't
5746              have a return insn for the peephole to recognize, so we
5747              must reject this.  (Can this be fixed by adding our own insn?) */
5748           if ((next = next_nonnote_insn (insn)) == NULL)
5749             return 1;
5750
5751           /* No need to worry about lr if the call never returns */
5752           if (GET_CODE (next) == BARRIER)
5753             break;
5754
5755           if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == USE
5756               && (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
5757               && (REGNO (SET_DEST (XVECEXP (PATTERN (insn), 0, 0)))
5758                   == REGNO (XEXP (PATTERN (next), 0))))
5759             if ((next = next_nonnote_insn (next)) == NULL)
5760               return 1;
5761
5762           if (GET_CODE (next) == JUMP_INSN
5763               && GET_CODE (PATTERN (next)) == RETURN)
5764             break;
5765           return 1;
5766
5767         default:
5768           abort ();
5769         }
5770     }
5771
5772   /* We have reached the end of the chain so lr was _not_ clobbered */
5773   return 0;
5774 }
5775
5776 char *
5777 output_return_instruction (operand, really_return, reverse)
5778      rtx operand;
5779      int really_return;
5780      int reverse;
5781 {
5782   char instr[100];
5783   int reg, live_regs = 0;
5784   int volatile_func = (optimize > 0 
5785                        && TREE_THIS_VOLATILE (current_function_decl));
5786
5787   return_used_this_function = 1;
5788
5789   if (TARGET_ABORT_NORETURN && volatile_func)
5790     {
5791       /* If this function was declared non-returning, and we have found a tail 
5792          call, then we have to trust that the called function won't return.  */
5793       if (really_return)
5794         {
5795           rtx ops[2];
5796       
5797           /* Otherwise, trap an attempted return by aborting.  */
5798           ops[0] = operand;
5799           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
5800                                        : "abort");
5801           assemble_external_libcall (ops[1]);
5802           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
5803         }
5804       
5805       return "";
5806     }
5807       
5808   if (current_function_calls_alloca && ! really_return)
5809     abort ();
5810     
5811   for (reg = 0; reg <= 10; reg++)
5812     if (regs_ever_live[reg] && ! call_used_regs[reg])
5813       live_regs++;
5814
5815   if (flag_pic && ! TARGET_SINGLE_PIC_BASE
5816       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5817     live_regs++;
5818
5819   if (live_regs || (regs_ever_live[LR_REGNUM] && ! lr_save_eliminated))
5820     live_regs++;
5821
5822   if (frame_pointer_needed)
5823     live_regs += 4;
5824
5825   /* On some ARM architectures it is faster to use LDR rather than LDM to
5826      load a single register.  On other architectures, the cost is the same.  */
5827   if (live_regs == 1
5828       && regs_ever_live[LR_REGNUM]
5829       && ! lr_save_eliminated
5830       /* FIXME: We ought to handle the case TARGET_APCS_32 is true,
5831          really_return is true, and only the PC needs restoring.  */
5832       && ! really_return)
5833     {
5834       output_asm_insn (reverse ? "ldr%?%D0\t%|lr, [%|sp], #4" 
5835                        : "ldr%?%d0\t%|lr, [%|sp], #4", &operand);
5836     }
5837   else if (live_regs)
5838     {
5839       if (lr_save_eliminated || ! regs_ever_live[LR_REGNUM])
5840         live_regs++;
5841
5842       if (frame_pointer_needed)
5843         strcpy (instr,
5844                 reverse ? "ldm%?%D0ea\t%|fp, {" : "ldm%?%d0ea\t%|fp, {");
5845       else
5846         strcpy (instr, 
5847                 reverse ? "ldm%?%D0fd\t%|sp!, {" : "ldm%?%d0fd\t%|sp!, {");
5848
5849       for (reg = 0; reg <= 10; reg++)
5850         if (regs_ever_live[reg]
5851             && (! call_used_regs[reg]
5852                 || (flag_pic && ! TARGET_SINGLE_PIC_BASE
5853                     && reg == PIC_OFFSET_TABLE_REGNUM)))
5854           {
5855             strcat (instr, "%|");
5856             strcat (instr, reg_names[reg]);
5857             if (--live_regs)
5858               strcat (instr, ", ");
5859           }
5860
5861       if (frame_pointer_needed)
5862         {
5863           strcat (instr, "%|");
5864           strcat (instr, reg_names[11]);
5865           strcat (instr, ", ");
5866           strcat (instr, "%|");
5867           strcat (instr, reg_names[13]);
5868           strcat (instr, ", ");
5869           strcat (instr, "%|");
5870           strcat (instr, TARGET_INTERWORK || (! really_return)
5871                   ? reg_names[LR_REGNUM] : reg_names[PC_REGNUM] );
5872         }
5873       else
5874         {
5875           strcat (instr, "%|");
5876           if (TARGET_INTERWORK && really_return)
5877             strcat (instr, reg_names[IP_REGNUM]);
5878           else
5879             strcat (instr, really_return ? reg_names[PC_REGNUM] : reg_names[LR_REGNUM]);
5880         }
5881       strcat (instr, (TARGET_APCS_32 || !really_return) ? "}" : "}^");
5882       output_asm_insn (instr, &operand);
5883
5884       if (TARGET_INTERWORK && really_return)
5885         {
5886           strcpy (instr, "bx%?");
5887           strcat (instr, reverse ? "%D0" : "%d0");
5888           strcat (instr, "\t%|");
5889           strcat (instr, frame_pointer_needed ? "lr" : "ip");
5890
5891           output_asm_insn (instr, & operand);
5892         }
5893     }
5894   else if (really_return)
5895     {
5896       if (TARGET_INTERWORK)
5897         sprintf (instr, "bx%%?%%%s0\t%%|lr", reverse ? "D" : "d");
5898       else
5899         sprintf (instr, "mov%%?%%%s0%s\t%%|pc, %%|lr",
5900                  reverse ? "D" : "d", TARGET_APCS_32 ? "" : "s");
5901       
5902       output_asm_insn (instr, & operand);
5903     }
5904
5905   return "";
5906 }
5907
5908 /* Return nonzero if optimizing and the current function is volatile.
5909    Such functions never return, and many memory cycles can be saved
5910    by not storing register values that will never be needed again.
5911    This optimization was added to speed up context switching in a
5912    kernel application. */
5913
5914 int
5915 arm_volatile_func ()
5916 {
5917   return (optimize > 0 && TREE_THIS_VOLATILE (current_function_decl));
5918 }
5919
5920 /* Write the function name into the code section, directly preceding
5921    the function prologue.
5922
5923    Code will be output similar to this:
5924      t0
5925          .ascii "arm_poke_function_name", 0
5926          .align
5927      t1
5928          .word 0xff000000 + (t1 - t0)
5929      arm_poke_function_name
5930          mov     ip, sp
5931          stmfd   sp!, {fp, ip, lr, pc}
5932          sub     fp, ip, #4
5933
5934    When performing a stack backtrace, code can inspect the value
5935    of 'pc' stored at 'fp' + 0.  If the trace function then looks
5936    at location pc - 12 and the top 8 bits are set, then we know
5937    that there is a function name embedded immediately preceding this
5938    location and has length ((pc[-3]) & 0xff000000).
5939
5940    We assume that pc is declared as a pointer to an unsigned long.
5941
5942    It is of no benefit to output the function name if we are assembling
5943    a leaf function.  These function types will not contain a stack
5944    backtrace structure, therefore it is not possible to determine the
5945    function name.  */
5946
5947 void
5948 arm_poke_function_name (stream, name)
5949    FILE * stream;
5950    char * name;
5951 {
5952   unsigned long alignlength;
5953   unsigned long length;
5954   rtx           x;
5955
5956   length = strlen (name) + 1;
5957   alignlength = (length + 3) & ~3;
5958   
5959   ASM_OUTPUT_ASCII (stream, name, length);
5960   ASM_OUTPUT_ALIGN (stream, 2);
5961   x = GEN_INT (0xff000000UL + alignlength);
5962   ASM_OUTPUT_INT (stream, x);
5963 }
5964
5965 /* The amount of stack adjustment that happens here, in output_return and in
5966    output_epilogue must be exactly the same as was calculated during reload,
5967    or things will point to the wrong place.  The only time we can safely
5968    ignore this constraint is when a function has no arguments on the stack,
5969    no stack frame requirement and no live registers execpt for `lr'.  If we
5970    can guarantee that by making all function calls into tail calls and that
5971    lr is not clobbered in any other way, then there is no need to push lr
5972    onto the stack. */
5973    
5974 void
5975 output_func_prologue (f, frame_size)
5976      FILE * f;
5977      int frame_size;
5978 {
5979   int reg, live_regs_mask = 0;
5980   int volatile_func = (optimize > 0
5981                        && TREE_THIS_VOLATILE (current_function_decl));
5982
5983   /* Nonzero if we must stuff some register arguments onto the stack as if
5984      they were passed there.  */
5985   int store_arg_regs = 0;
5986
5987   if (arm_ccfsm_state || arm_target_insn)
5988     abort ();                                   /* Sanity check */
5989
5990   if (arm_naked_function_p (current_function_decl))
5991     return;
5992
5993   return_used_this_function = 0;
5994   lr_save_eliminated = 0;
5995   
5996   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
5997                current_function_args_size,
5998                current_function_pretend_args_size, frame_size);
5999   asm_fprintf (f, "\t%@ frame_needed = %d, current_function_anonymous_args = %d\n",
6000                frame_pointer_needed,
6001                current_function_anonymous_args);
6002
6003   if (volatile_func)
6004     asm_fprintf (f, "\t%@ Volatile function.\n");
6005
6006   if (current_function_anonymous_args && current_function_pretend_args_size)
6007     store_arg_regs = 1;
6008
6009   for (reg = 0; reg <= 10; reg++)
6010     if (regs_ever_live[reg] && ! call_used_regs[reg])
6011       live_regs_mask |= (1 << reg);
6012
6013   if (flag_pic && ! TARGET_SINGLE_PIC_BASE
6014       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6015     live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
6016
6017   if (frame_pointer_needed)
6018     live_regs_mask |= 0xD800;
6019   else if (regs_ever_live[LR_REGNUM])
6020     {
6021       if (! current_function_args_size
6022           && ! function_really_clobbers_lr (get_insns ()))
6023         lr_save_eliminated = 1;
6024       else
6025         live_regs_mask |= 1 << LR_REGNUM;
6026     }
6027
6028   if (live_regs_mask)
6029     {
6030       /* if a di mode load/store multiple is used, and the base register
6031          is r3, then r4 can become an ever live register without lr
6032          doing so,  in this case we need to push lr as well, or we
6033          will fail to get a proper return. */
6034
6035       live_regs_mask |= 1 << LR_REGNUM;
6036       lr_save_eliminated = 0;
6037
6038     }
6039
6040   if (lr_save_eliminated)
6041     asm_fprintf (f,"\t%@ I don't think this function clobbers lr\n");
6042
6043 #ifdef AOF_ASSEMBLER
6044   if (flag_pic)
6045     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
6046 #endif
6047 }
6048
6049 char *
6050 arm_output_epilogue ()
6051 {
6052   int reg;
6053   int live_regs_mask = 0;
6054   /* If we need this, then it will always be at least this much */
6055   int floats_offset = 12;
6056   rtx operands[3];
6057   int frame_size = get_frame_size ();
6058   FILE *f = asm_out_file;
6059   int volatile_func = (optimize > 0
6060                        && TREE_THIS_VOLATILE (current_function_decl));
6061
6062   if (use_return_insn (FALSE) && return_used_this_function)
6063     return "";
6064
6065   /* Naked functions don't have epilogues.  */
6066   if (arm_naked_function_p (current_function_decl))
6067     return "";
6068
6069   /* A volatile function should never return.  Call abort.  */
6070   if (TARGET_ABORT_NORETURN && volatile_func)
6071     {
6072       rtx op;
6073       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
6074       assemble_external_libcall (op);
6075       output_asm_insn ("bl\t%a0", &op);
6076       return "";
6077     }
6078
6079   for (reg = 0; reg <= 10; reg++)
6080     if (regs_ever_live[reg] && ! call_used_regs[reg])
6081       {
6082         live_regs_mask |= (1 << reg);
6083         floats_offset += 4;
6084       }
6085
6086   /* If we aren't loading the PIC register, don't stack it even though it may
6087      be live.  */
6088   if (flag_pic && ! TARGET_SINGLE_PIC_BASE 
6089       && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6090     {
6091       live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
6092       floats_offset += 4;
6093     }
6094
6095   if (frame_pointer_needed)
6096     {
6097       if (arm_fpu_arch == FP_SOFT2)
6098         {
6099           for (reg = 23; reg > 15; reg--)
6100             if (regs_ever_live[reg] && ! call_used_regs[reg])
6101               {
6102                 floats_offset += 12;
6103                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
6104                              reg, FP_REGNUM, floats_offset);
6105               }
6106         }
6107       else
6108         {
6109           int start_reg = 23;
6110
6111           for (reg = 23; reg > 15; reg--)
6112             {
6113               if (regs_ever_live[reg] && ! call_used_regs[reg])
6114                 {
6115                   floats_offset += 12;
6116                   
6117                   /* We can't unstack more than four registers at once */
6118                   if (start_reg - reg == 3)
6119                     {
6120                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
6121                                    reg, FP_REGNUM, floats_offset);
6122                       start_reg = reg - 1;
6123                     }
6124                 }
6125               else
6126                 {
6127                   if (reg != start_reg)
6128                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
6129                                  reg + 1, start_reg - reg,
6130                                  FP_REGNUM, floats_offset);
6131                   start_reg = reg - 1;
6132                 }
6133             }
6134
6135           /* Just in case the last register checked also needs unstacking.  */
6136           if (reg != start_reg)
6137             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
6138                          reg + 1, start_reg - reg,
6139                          FP_REGNUM, floats_offset);
6140         }
6141       
6142       if (TARGET_INTERWORK)
6143         {
6144           live_regs_mask |= 0x6800;
6145           print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask, FALSE);
6146           asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6147         }
6148       else
6149         {
6150           live_regs_mask |= 0xA800;
6151           print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask,
6152                            TARGET_APCS_32 ? FALSE : TRUE);
6153         }
6154     }
6155   else
6156     {
6157       /* Restore stack pointer if necessary.  */
6158       if (frame_size + current_function_outgoing_args_size != 0)
6159         {
6160           operands[0] = operands[1] = stack_pointer_rtx;
6161           operands[2] = GEN_INT (frame_size
6162                                  + current_function_outgoing_args_size);
6163           output_add_immediate (operands);
6164         }
6165
6166       if (arm_fpu_arch == FP_SOFT2)
6167         {
6168           for (reg = 16; reg < 24; reg++)
6169             if (regs_ever_live[reg] && ! call_used_regs[reg])
6170               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
6171                            reg, SP_REGNUM);
6172         }
6173       else
6174         {
6175           int start_reg = 16;
6176
6177           for (reg = 16; reg < 24; reg++)
6178             {
6179               if (regs_ever_live[reg] && ! call_used_regs[reg])
6180                 {
6181                   if (reg - start_reg == 3)
6182                     {
6183                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
6184                                    start_reg, SP_REGNUM);
6185                       start_reg = reg + 1;
6186                     }
6187                 }
6188               else
6189                 {
6190                   if (reg != start_reg)
6191                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
6192                                  start_reg, reg - start_reg,
6193                                  SP_REGNUM);
6194                   
6195                   start_reg = reg + 1;
6196                 }
6197             }
6198
6199           /* Just in case the last register checked also needs unstacking.  */
6200           if (reg != start_reg)
6201             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
6202                          start_reg, reg - start_reg, SP_REGNUM);
6203         }
6204
6205       if (current_function_pretend_args_size == 0 && regs_ever_live[LR_REGNUM])
6206         {
6207           if (TARGET_INTERWORK)
6208             {
6209               if (! lr_save_eliminated)
6210                 live_regs_mask |= 1 << LR_REGNUM;
6211
6212               if (live_regs_mask != 0)
6213                 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask, FALSE);
6214               
6215               asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6216             }
6217           else if (lr_save_eliminated)
6218             asm_fprintf (f, 
6219                          TARGET_APCS_32 ? "\tmov\t%r, %r\n" : "\tmovs\t%r, %r\n",
6220                          PC_REGNUM, LR_REGNUM);
6221           else
6222             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask | 0x8000,
6223                              TARGET_APCS_32 ? FALSE : TRUE);
6224         }
6225       else
6226         {
6227           if (live_regs_mask || regs_ever_live[LR_REGNUM])
6228             {
6229               /* Restore the integer regs, and the return address into lr */
6230               if (! lr_save_eliminated)
6231                 live_regs_mask |= 1 << LR_REGNUM;
6232
6233               if (live_regs_mask != 0)
6234                 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask, FALSE);
6235             }
6236
6237           if (current_function_pretend_args_size)
6238             {
6239               /* Unwind the pre-pushed regs */
6240               operands[0] = operands[1] = stack_pointer_rtx;
6241               operands[2] = GEN_INT (current_function_pretend_args_size);
6242               output_add_immediate (operands);
6243             }
6244           /* And finally, go home */
6245           if (TARGET_INTERWORK)
6246             asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
6247           else if (TARGET_APCS_32)
6248             asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6249           else
6250             asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
6251         }
6252     }
6253
6254   return "";
6255 }
6256
6257 void
6258 output_func_epilogue (frame_size)
6259      int frame_size;
6260 {
6261   if (use_return_insn (FALSE) && return_used_this_function
6262       && (frame_size + current_function_outgoing_args_size) != 0
6263       && ! (frame_pointer_needed && TARGET_APCS))
6264     abort ();
6265
6266   /* Reset the ARM-specific per-function variables.  */
6267   current_function_anonymous_args = 0;
6268   after_arm_reorg = 0;
6269 }
6270
6271 static void
6272 emit_multi_reg_push (mask)
6273      int mask;
6274 {
6275   int num_regs = 0;
6276   int i, j;
6277   rtx par;
6278
6279   for (i = 0; i < 16; i++)
6280     if (mask & (1 << i))
6281       num_regs++;
6282
6283   if (num_regs == 0 || num_regs > 16)
6284     abort ();
6285
6286   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
6287
6288   for (i = 0; i < 16; i++)
6289     {
6290       if (mask & (1 << i))
6291         {
6292           XVECEXP (par, 0, 0)
6293             = gen_rtx_SET (VOIDmode,
6294                            gen_rtx_MEM (BLKmode,
6295                                         gen_rtx_PRE_DEC (BLKmode,
6296                                                          stack_pointer_rtx)),
6297                            gen_rtx_UNSPEC (BLKmode,
6298                                            gen_rtvec (1,
6299                                                       gen_rtx_REG (SImode, i)),
6300                                            2));
6301           break;
6302         }
6303     }
6304
6305   for (j = 1, i++; j < num_regs; i++)
6306     {
6307       if (mask & (1 << i))
6308         {
6309           XVECEXP (par, 0, j)
6310             = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, i));
6311           j++;
6312         }
6313     }
6314
6315   emit_insn (par);
6316 }
6317
6318 static void
6319 emit_sfm (base_reg, count)
6320      int base_reg;
6321      int count;
6322 {
6323   rtx par;
6324   int i;
6325
6326   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
6327
6328   XVECEXP (par, 0, 0)
6329     = gen_rtx_SET (VOIDmode, 
6330                    gen_rtx_MEM (BLKmode,
6331                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
6332                    gen_rtx_UNSPEC (BLKmode,
6333                                    gen_rtvec (1, gen_rtx_REG (XFmode, 
6334                                                               base_reg++)),
6335                                    2));
6336   for (i = 1; i < count; i++)
6337     XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, 
6338                                        gen_rtx_REG (XFmode, base_reg++));
6339
6340   emit_insn (par);
6341 }
6342
6343 void
6344 arm_expand_prologue ()
6345 {
6346   int reg;
6347   rtx amount = GEN_INT (-(get_frame_size ()
6348                           + current_function_outgoing_args_size));
6349   int live_regs_mask = 0;
6350   int store_arg_regs = 0;
6351   /* If this function doesn't return, then there is no need to push
6352      the call-saved regs.  */
6353   int volatile_func = (optimize > 0
6354                        && TREE_THIS_VOLATILE (current_function_decl));
6355
6356   /* Naked functions don't have prologues.  */
6357   if (arm_naked_function_p (current_function_decl))
6358     return;
6359
6360   if (current_function_anonymous_args && current_function_pretend_args_size)
6361     store_arg_regs = 1;
6362
6363   if (! volatile_func)
6364     {
6365       for (reg = 0; reg <= 10; reg++)
6366         if (regs_ever_live[reg] && ! call_used_regs[reg])
6367           live_regs_mask |= 1 << reg;
6368
6369       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6370         live_regs_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
6371
6372       if (regs_ever_live[LR_REGNUM])
6373         live_regs_mask |= 1 << LR_REGNUM;
6374     }
6375
6376   if (frame_pointer_needed)
6377     {
6378       live_regs_mask |= 0xD800;
6379       emit_insn (gen_movsi (gen_rtx_REG (SImode, IP_REGNUM),
6380                             stack_pointer_rtx));
6381     }
6382
6383   if (current_function_pretend_args_size)
6384     {
6385       if (store_arg_regs)
6386         emit_multi_reg_push ((0xf0 >> (current_function_pretend_args_size / 4))
6387                              & 0xf);
6388       else
6389         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
6390                                GEN_INT (-current_function_pretend_args_size)));
6391     }
6392
6393   if (live_regs_mask)
6394     {
6395       /* If we have to push any regs, then we must push lr as well, or
6396          we won't get a proper return.  */
6397       live_regs_mask |= 1 << LR_REGNUM;
6398       emit_multi_reg_push (live_regs_mask);
6399     }
6400       
6401   /* For now the integer regs are still pushed in output_func_epilogue ().  */
6402
6403   if (! volatile_func)
6404     {
6405       if (arm_fpu_arch == FP_SOFT2)
6406         {
6407           for (reg = 23; reg > 15; reg--)
6408             if (regs_ever_live[reg] && ! call_used_regs[reg])
6409               emit_insn (gen_rtx_SET
6410                          (VOIDmode, 
6411                           gen_rtx_MEM (XFmode, 
6412                                        gen_rtx_PRE_DEC (XFmode,
6413                                                         stack_pointer_rtx)),
6414                           gen_rtx_REG (XFmode, reg)));
6415         }
6416       else
6417         {
6418           int start_reg = 23;
6419
6420           for (reg = 23; reg > 15; reg--)
6421             {
6422               if (regs_ever_live[reg] && ! call_used_regs[reg])
6423                 {
6424                   if (start_reg - reg == 3)
6425                     {
6426                       emit_sfm (reg, 4);
6427                       start_reg = reg - 1;
6428                     }
6429                 }
6430               else
6431                 {
6432                   if (start_reg != reg)
6433                     emit_sfm (reg + 1, start_reg - reg);
6434                   start_reg = reg - 1;
6435                 }
6436             }
6437
6438           if (start_reg != reg)
6439             emit_sfm (reg + 1, start_reg - reg);
6440         }
6441     }
6442
6443   if (frame_pointer_needed)
6444     emit_insn (gen_addsi3 (hard_frame_pointer_rtx, gen_rtx_REG (SImode, IP_REGNUM),
6445                            (GEN_INT
6446                             (-(4 + current_function_pretend_args_size)))));
6447
6448   if (amount != const0_rtx)
6449     {
6450       emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, amount));
6451       emit_insn (gen_rtx_CLOBBER (VOIDmode, 
6452                                   gen_rtx_MEM (BLKmode, stack_pointer_rtx)));
6453     }
6454
6455   /* If we are profiling, make sure no instructions are scheduled before
6456      the call to mcount.  Similarly if the user has requested no
6457      scheduling in the prolog.  */
6458   if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
6459     emit_insn (gen_blockage ());
6460 }
6461   
6462 \f
6463 /* If CODE is 'd', then the X is a condition operand and the instruction
6464    should only be executed if the condition is true.
6465    if CODE is 'D', then the X is a condition operand and the instruction
6466    should only be executed if the condition is false: however, if the mode
6467    of the comparison is CCFPEmode, then always execute the instruction -- we
6468    do this because in these circumstances !GE does not necessarily imply LT;
6469    in these cases the instruction pattern will take care to make sure that
6470    an instruction containing %d will follow, thereby undoing the effects of
6471    doing this instruction unconditionally.
6472    If CODE is 'N' then X is a floating point operand that must be negated
6473    before output.
6474    If CODE is 'B' then output a bitwise inverted value of X (a const int).
6475    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
6476
6477 void
6478 arm_print_operand (stream, x, code)
6479      FILE * stream;
6480      rtx x;
6481      int code;
6482 {
6483   switch (code)
6484     {
6485     case '@':
6486       fputs (ASM_COMMENT_START, stream);
6487       return;
6488
6489     case '|':
6490       fputs (REGISTER_PREFIX, stream);
6491       return;
6492
6493     case '?':
6494       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
6495         fputs (arm_condition_codes[arm_current_cc], stream);
6496       return;
6497
6498     case 'N':
6499       {
6500         REAL_VALUE_TYPE r;
6501         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6502         r = REAL_VALUE_NEGATE (r);
6503         fprintf (stream, "%s", fp_const_from_val (&r));
6504       }
6505       return;
6506
6507     case 'B':
6508       if (GET_CODE (x) == CONST_INT)
6509         {
6510           HOST_WIDE_INT val;
6511           val = ARM_SIGN_EXTEND (~ INTVAL (x));
6512           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
6513         }
6514       else
6515         {
6516           putc ('~', stream);
6517           output_addr_const (stream, x);
6518         }
6519       return;
6520
6521     case 'i':
6522       fprintf (stream, "%s", arithmetic_instr (x, 1));
6523       return;
6524
6525     case 'I':
6526       fprintf (stream, "%s", arithmetic_instr (x, 0));
6527       return;
6528
6529     case 'S':
6530       {
6531         HOST_WIDE_INT val;
6532         char * shift = shift_op (x, & val);
6533
6534         if (shift)
6535           {
6536             fprintf (stream, ", %s ", shift_op (x, & val));
6537             if (val == -1)
6538               arm_print_operand (stream, XEXP (x, 1), 0);
6539             else
6540               {
6541                 fputc ('#', stream);
6542                 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
6543               }
6544           }
6545       }
6546       return;
6547
6548     case 'Q':
6549       if (REGNO (x) > 15)
6550         abort ();
6551       fputs (REGISTER_PREFIX, stream);
6552       fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0)], stream);
6553       return;
6554
6555     case 'R':
6556       if (REGNO (x) > 15)
6557         abort ();
6558       fputs (REGISTER_PREFIX, stream);
6559       fputs (reg_names[REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1)], stream);
6560       return;
6561
6562     case 'm':
6563       fputs (REGISTER_PREFIX, stream);
6564       if (GET_CODE (XEXP (x, 0)) == REG)
6565         fputs (reg_names[REGNO (XEXP (x, 0))], stream);
6566       else
6567         fputs (reg_names[REGNO (XEXP (XEXP (x, 0), 0))], stream);
6568       return;
6569
6570     case 'M':
6571       asm_fprintf (stream, "{%r-%r}",
6572                    REGNO (x), REGNO (x) + NUM_REGS (GET_MODE (x)) - 1);
6573       return;
6574
6575     case 'd':
6576       if (x)
6577         fputs (arm_condition_codes[get_arm_condition_code (x)],
6578                stream);
6579       return;
6580
6581     case 'D':
6582       if (x)
6583         fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
6584                                    (get_arm_condition_code (x))],
6585                stream);
6586       return;
6587
6588     default:
6589       if (x == 0)
6590         abort ();
6591
6592       if (GET_CODE (x) == REG)
6593         {
6594           fputs (REGISTER_PREFIX, stream);
6595           fputs (reg_names[REGNO (x)], stream);
6596         }
6597       else if (GET_CODE (x) == MEM)
6598         {
6599           output_memory_reference_mode = GET_MODE (x);
6600           output_address (XEXP (x, 0));
6601         }
6602       else if (GET_CODE (x) == CONST_DOUBLE)
6603         fprintf (stream, "#%s", fp_immediate_constant (x));
6604       else if (GET_CODE (x) == NEG)
6605         abort (); /* This should never happen now. */
6606       else
6607         {
6608           fputc ('#', stream);
6609           output_addr_const (stream, x);
6610         }
6611     }
6612 }
6613 \f
6614 /* A finite state machine takes care of noticing whether or not instructions
6615    can be conditionally executed, and thus decrease execution time and code
6616    size by deleting branch instructions.  The fsm is controlled by
6617    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
6618
6619 /* The state of the fsm controlling condition codes are:
6620    0: normal, do nothing special
6621    1: make ASM_OUTPUT_OPCODE not output this instruction
6622    2: make ASM_OUTPUT_OPCODE not output this instruction
6623    3: make instructions conditional
6624    4: make instructions conditional
6625
6626    State transitions (state->state by whom under condition):
6627    0 -> 1 final_prescan_insn if the `target' is a label
6628    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
6629    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
6630    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
6631    3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
6632           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
6633    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
6634           (the target insn is arm_target_insn).
6635
6636    If the jump clobbers the conditions then we use states 2 and 4.
6637
6638    A similar thing can be done with conditional return insns.
6639
6640    XXX In case the `target' is an unconditional branch, this conditionalising
6641    of the instructions always reduces code size, but not always execution
6642    time.  But then, I want to reduce the code size to somewhere near what
6643    /bin/cc produces.  */
6644
6645 /* Returns the index of the ARM condition code string in
6646    `arm_condition_codes'.  COMPARISON should be an rtx like
6647    `(eq (...) (...))'.  */
6648
6649 static enum arm_cond_code
6650 get_arm_condition_code (comparison)
6651      rtx comparison;
6652 {
6653   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
6654   register int code;
6655   register enum rtx_code comp_code = GET_CODE (comparison);
6656
6657   if (GET_MODE_CLASS (mode) != MODE_CC)
6658     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
6659                            XEXP (comparison, 1));
6660
6661   switch (mode)
6662     {
6663     case CC_DNEmode: code = ARM_NE; goto dominance;
6664     case CC_DEQmode: code = ARM_EQ; goto dominance;
6665     case CC_DGEmode: code = ARM_GE; goto dominance;
6666     case CC_DGTmode: code = ARM_GT; goto dominance;
6667     case CC_DLEmode: code = ARM_LE; goto dominance;
6668     case CC_DLTmode: code = ARM_LT; goto dominance;
6669     case CC_DGEUmode: code = ARM_CS; goto dominance;
6670     case CC_DGTUmode: code = ARM_HI; goto dominance;
6671     case CC_DLEUmode: code = ARM_LS; goto dominance;
6672     case CC_DLTUmode: code = ARM_CC;
6673
6674     dominance:
6675       if (comp_code != EQ && comp_code != NE)
6676         abort ();
6677
6678       if (comp_code == EQ)
6679         return ARM_INVERSE_CONDITION_CODE (code);
6680       return code;
6681
6682     case CC_NOOVmode:
6683       switch (comp_code)
6684         {
6685         case NE: return ARM_NE;
6686         case EQ: return ARM_EQ;
6687         case GE: return ARM_PL;
6688         case LT: return ARM_MI;
6689         default: abort ();
6690         }
6691
6692     case CC_Zmode:
6693     case CCFPmode:
6694       switch (comp_code)
6695         {
6696         case NE: return ARM_NE;
6697         case EQ: return ARM_EQ;
6698         default: abort ();
6699         }
6700
6701     case CCFPEmode:
6702       switch (comp_code)
6703         {
6704         case GE: return ARM_GE;
6705         case GT: return ARM_GT;
6706         case LE: return ARM_LS;
6707         case LT: return ARM_MI;
6708         default: abort ();
6709         }
6710
6711     case CC_SWPmode:
6712       switch (comp_code)
6713         {
6714         case NE: return ARM_NE;
6715         case EQ: return ARM_EQ;
6716         case GE: return ARM_LE;
6717         case GT: return ARM_LT;
6718         case LE: return ARM_GE;
6719         case LT: return ARM_GT;
6720         case GEU: return ARM_LS;
6721         case GTU: return ARM_CC;
6722         case LEU: return ARM_CS;
6723         case LTU: return ARM_HI;
6724         default: abort ();
6725         }
6726
6727     case CC_Cmode:
6728       switch (comp_code)
6729       {
6730       case LTU: return ARM_CS;
6731       case GEU: return ARM_CC;
6732       default: abort ();
6733       }
6734       
6735     case CCmode:
6736       switch (comp_code)
6737         {
6738         case NE: return ARM_NE;
6739         case EQ: return ARM_EQ;
6740         case GE: return ARM_GE;
6741         case GT: return ARM_GT;
6742         case LE: return ARM_LE;
6743         case LT: return ARM_LT;
6744         case GEU: return ARM_CS;
6745         case GTU: return ARM_HI;
6746         case LEU: return ARM_LS;
6747         case LTU: return ARM_CC;
6748         default: abort ();
6749         }
6750
6751     default: abort ();
6752     }
6753
6754   abort ();
6755 }
6756
6757
6758 void
6759 arm_final_prescan_insn (insn)
6760      rtx insn;
6761 {
6762   /* BODY will hold the body of INSN.  */
6763   register rtx body = PATTERN (insn);
6764
6765   /* This will be 1 if trying to repeat the trick, and things need to be
6766      reversed if it appears to fail.  */
6767   int reverse = 0;
6768
6769   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
6770      taken are clobbered, even if the rtl suggests otherwise.  It also
6771      means that we have to grub around within the jump expression to find
6772      out what the conditions are when the jump isn't taken.  */
6773   int jump_clobbers = 0;
6774   
6775   /* If we start with a return insn, we only succeed if we find another one. */
6776   int seeking_return = 0;
6777   
6778   /* START_INSN will hold the insn from where we start looking.  This is the
6779      first insn after the following code_label if REVERSE is true.  */
6780   rtx start_insn = insn;
6781
6782   /* If in state 4, check if the target branch is reached, in order to
6783      change back to state 0.  */
6784   if (arm_ccfsm_state == 4)
6785     {
6786       if (insn == arm_target_insn)
6787         {
6788           arm_target_insn = NULL;
6789           arm_ccfsm_state = 0;
6790         }
6791       return;
6792     }
6793
6794   /* If in state 3, it is possible to repeat the trick, if this insn is an
6795      unconditional branch to a label, and immediately following this branch
6796      is the previous target label which is only used once, and the label this
6797      branch jumps to is not too far off.  */
6798   if (arm_ccfsm_state == 3)
6799     {
6800       if (simplejump_p (insn))
6801         {
6802           start_insn = next_nonnote_insn (start_insn);
6803           if (GET_CODE (start_insn) == BARRIER)
6804             {
6805               /* XXX Isn't this always a barrier?  */
6806               start_insn = next_nonnote_insn (start_insn);
6807             }
6808           if (GET_CODE (start_insn) == CODE_LABEL
6809               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
6810               && LABEL_NUSES (start_insn) == 1)
6811             reverse = TRUE;
6812           else
6813             return;
6814         }
6815       else if (GET_CODE (body) == RETURN)
6816         {
6817           start_insn = next_nonnote_insn (start_insn);
6818           if (GET_CODE (start_insn) == BARRIER)
6819             start_insn = next_nonnote_insn (start_insn);
6820           if (GET_CODE (start_insn) == CODE_LABEL
6821               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
6822               && LABEL_NUSES (start_insn) == 1)
6823             {
6824               reverse = TRUE;
6825               seeking_return = 1;
6826             }
6827           else
6828             return;
6829         }
6830       else
6831         return;
6832     }
6833
6834   if (arm_ccfsm_state != 0 && !reverse)
6835     abort ();
6836   if (GET_CODE (insn) != JUMP_INSN)
6837     return;
6838
6839   /* This jump might be paralleled with a clobber of the condition codes 
6840      the jump should always come first */
6841   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6842     body = XVECEXP (body, 0, 0);
6843
6844 #if 0  
6845   /* If this is a conditional return then we don't want to know */
6846   if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
6847       && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
6848       && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
6849           || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
6850     return;
6851 #endif
6852
6853   if (reverse
6854       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
6855           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
6856     {
6857       int insns_skipped;
6858       int fail = FALSE, succeed = FALSE;
6859       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
6860       int then_not_else = TRUE;
6861       rtx this_insn = start_insn, label = 0;
6862
6863       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
6864         {
6865           /* The code below is wrong for these, and I haven't time to
6866              fix it now.  So we just do the safe thing and return.  This
6867              whole function needs re-writing anyway.  */
6868           jump_clobbers = 1;
6869           return;
6870         }
6871       
6872       /* Register the insn jumped to.  */
6873       if (reverse)
6874         {
6875           if (!seeking_return)
6876             label = XEXP (SET_SRC (body), 0);
6877         }
6878       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
6879         label = XEXP (XEXP (SET_SRC (body), 1), 0);
6880       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
6881         {
6882           label = XEXP (XEXP (SET_SRC (body), 2), 0);
6883           then_not_else = FALSE;
6884         }
6885       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
6886         seeking_return = 1;
6887       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
6888         {
6889           seeking_return = 1;
6890           then_not_else = FALSE;
6891         }
6892       else
6893         abort ();
6894
6895       /* See how many insns this branch skips, and what kind of insns.  If all
6896          insns are okay, and the label or unconditional branch to the same
6897          label is not too far away, succeed.  */
6898       for (insns_skipped = 0;
6899            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
6900         {
6901           rtx scanbody;
6902
6903           this_insn = next_nonnote_insn (this_insn);
6904           if (!this_insn)
6905             break;
6906
6907           switch (GET_CODE (this_insn))
6908             {
6909             case CODE_LABEL:
6910               /* Succeed if it is the target label, otherwise fail since
6911                  control falls in from somewhere else.  */
6912               if (this_insn == label)
6913                 {
6914                   if (jump_clobbers)
6915                     {
6916                       arm_ccfsm_state = 2;
6917                       this_insn = next_nonnote_insn (this_insn);
6918                     }
6919                   else
6920                     arm_ccfsm_state = 1;
6921                   succeed = TRUE;
6922                 }
6923               else
6924                 fail = TRUE;
6925               break;
6926
6927             case BARRIER:
6928               /* Succeed if the following insn is the target label.
6929                  Otherwise fail.  
6930                  If return insns are used then the last insn in a function 
6931                  will be a barrier. */
6932               this_insn = next_nonnote_insn (this_insn);
6933               if (this_insn && this_insn == label)
6934                 {
6935                   if (jump_clobbers)
6936                     {
6937                       arm_ccfsm_state = 2;
6938                       this_insn = next_nonnote_insn (this_insn);
6939                     }
6940                   else
6941                     arm_ccfsm_state = 1;
6942                   succeed = TRUE;
6943                 }
6944               else
6945                 fail = TRUE;
6946               break;
6947
6948             case CALL_INSN:
6949               /* If using 32-bit addresses the cc is not preserved over
6950                  calls.  */
6951               if (TARGET_APCS_32)
6952                 {
6953                   /* Succeed if the following insn is the target label,
6954                      or if the following two insns are a barrier and
6955                      the target label.  */
6956                   this_insn = next_nonnote_insn (this_insn);
6957                   if (this_insn && GET_CODE (this_insn) == BARRIER)
6958                     this_insn = next_nonnote_insn (this_insn);
6959
6960                   if (this_insn && this_insn == label
6961                       && insns_skipped < max_insns_skipped)
6962                     {
6963                       if (jump_clobbers)
6964                         {
6965                           arm_ccfsm_state = 2;
6966                           this_insn = next_nonnote_insn (this_insn);
6967                         }
6968                       else
6969                         arm_ccfsm_state = 1;
6970                       succeed = TRUE;
6971                     }
6972                   else
6973                     fail = TRUE;
6974                 }
6975               break;
6976
6977             case JUMP_INSN:
6978               /* If this is an unconditional branch to the same label, succeed.
6979                  If it is to another label, do nothing.  If it is conditional,
6980                  fail.  */
6981               /* XXX Probably, the tests for SET and the PC are unnecessary.  */
6982
6983               scanbody = PATTERN (this_insn);
6984               if (GET_CODE (scanbody) == SET
6985                   && GET_CODE (SET_DEST (scanbody)) == PC)
6986                 {
6987                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
6988                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
6989                     {
6990                       arm_ccfsm_state = 2;
6991                       succeed = TRUE;
6992                     }
6993                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
6994                     fail = TRUE;
6995                 }
6996               /* Fail if a conditional return is undesirable (eg on a
6997                  StrongARM), but still allow this if optimizing for size.  */
6998               else if (GET_CODE (scanbody) == RETURN
6999                        && ! use_return_insn (TRUE)
7000                        && ! optimize_size)
7001                 fail = TRUE;
7002               else if (GET_CODE (scanbody) == RETURN
7003                        && seeking_return)
7004                 {
7005                   arm_ccfsm_state = 2;
7006                   succeed = TRUE;
7007                 }
7008               else if (GET_CODE (scanbody) == PARALLEL)
7009                 {
7010                   switch (get_attr_conds (this_insn))
7011                     {
7012                     case CONDS_NOCOND:
7013                       break;
7014                     default:
7015                       fail = TRUE;
7016                       break;
7017                     }
7018                 }
7019               break;
7020
7021             case INSN:
7022               /* Instructions using or affecting the condition codes make it
7023                  fail.  */
7024               scanbody = PATTERN (this_insn);
7025               if (! (GET_CODE (scanbody) == SET
7026                      || GET_CODE (scanbody) == PARALLEL)
7027                   || get_attr_conds (this_insn) != CONDS_NOCOND)
7028                 fail = TRUE;
7029               break;
7030
7031             default:
7032               break;
7033             }
7034         }
7035       if (succeed)
7036         {
7037           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
7038             arm_target_label = CODE_LABEL_NUMBER (label);
7039           else if (seeking_return || arm_ccfsm_state == 2)
7040             {
7041               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
7042                 {
7043                   this_insn = next_nonnote_insn (this_insn);
7044                   if (this_insn && (GET_CODE (this_insn) == BARRIER
7045                                     || GET_CODE (this_insn) == CODE_LABEL))
7046                     abort ();
7047                 }
7048               if (!this_insn)
7049                 {
7050                   /* Oh, dear! we ran off the end.. give up */
7051                   recog (PATTERN (insn), insn, NULL_PTR);
7052                   arm_ccfsm_state = 0;
7053                   arm_target_insn = NULL;
7054                   return;
7055                 }
7056               arm_target_insn = this_insn;
7057             }
7058           else
7059             abort ();
7060           if (jump_clobbers)
7061             {
7062               if (reverse)
7063                 abort ();
7064               arm_current_cc = 
7065                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
7066                                                             0), 0), 1));
7067               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
7068                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
7069               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
7070                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
7071             }
7072           else
7073             {
7074               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
7075                  what it was.  */
7076               if (!reverse)
7077                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
7078                                                                0));
7079             }
7080
7081           if (reverse || then_not_else)
7082             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
7083         }
7084
7085       /* Restore recog_data (getting the attributes of other insns can
7086          destroy this array, but final.c assumes that it remains intact
7087          across this call; since the insn has been recognized already we
7088          call recog direct).  */
7089       recog (PATTERN (insn), insn, NULL_PTR);
7090     }
7091 }
7092
7093 /* Return the length of a function name prefix
7094     that starts with the character 'c'.  */
7095 static int
7096 arm_get_strip_length (char c)
7097 {
7098   switch (c)
7099     {
7100     ARM_NAME_ENCODING_LENGTHS
7101       default: return 0; 
7102     }
7103 }
7104
7105 /* Return a pointer to a function's name with any
7106    and all prefix encodings stripped from it.  */
7107 const char *
7108 arm_strip_name_encoding (const char * name)
7109 {
7110   int skip;
7111   
7112   while ((skip = arm_get_strip_length (* name)))
7113     name += skip;
7114
7115   return name;
7116 }
7117
7118 #ifdef AOF_ASSEMBLER
7119 /* Special functions only needed when producing AOF syntax assembler. */
7120
7121 rtx aof_pic_label = NULL_RTX;
7122 struct pic_chain
7123 {
7124   struct pic_chain * next;
7125   char * symname;
7126 };
7127
7128 static struct pic_chain * aof_pic_chain = NULL;
7129
7130 rtx
7131 aof_pic_entry (x)
7132      rtx x;
7133 {
7134   struct pic_chain ** chainp;
7135   int offset;
7136
7137   if (aof_pic_label == NULL_RTX)
7138     {
7139       /* We mark this here and not in arm_add_gc_roots() to avoid
7140          polluting even more code with ifdefs, and because it never
7141          contains anything useful until we assign to it here.  */
7142       ggc_add_rtx_root (&aof_pic_label, 1);
7143       /* This needs to persist throughout the compilation.  */
7144       end_temporary_allocation ();
7145       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
7146       resume_temporary_allocation ();
7147     }
7148
7149   for (offset = 0, chainp = &aof_pic_chain; *chainp;
7150        offset += 4, chainp = &(*chainp)->next)
7151     if ((*chainp)->symname == XSTR (x, 0))
7152       return plus_constant (aof_pic_label, offset);
7153
7154   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
7155   (*chainp)->next = NULL;
7156   (*chainp)->symname = XSTR (x, 0);
7157   return plus_constant (aof_pic_label, offset);
7158 }
7159
7160 void
7161 aof_dump_pic_table (f)
7162      FILE * f;
7163 {
7164   struct pic_chain * chain;
7165
7166   if (aof_pic_chain == NULL)
7167     return;
7168
7169   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
7170                PIC_OFFSET_TABLE_REGNUM,
7171                PIC_OFFSET_TABLE_REGNUM);
7172   fputs ("|x$adcons|\n", f);
7173   
7174   for (chain = aof_pic_chain; chain; chain = chain->next)
7175     {
7176       fputs ("\tDCD\t", f);
7177       assemble_name (f, chain->symname);
7178       fputs ("\n", f);
7179     }
7180 }
7181
7182 int arm_text_section_count = 1;
7183
7184 char *
7185 aof_text_section ()
7186 {
7187   static char buf[100];
7188   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
7189            arm_text_section_count++);
7190   if (flag_pic)
7191     strcat (buf, ", PIC, REENTRANT");
7192   return buf;
7193 }
7194
7195 static int arm_data_section_count = 1;
7196
7197 char *
7198 aof_data_section ()
7199 {
7200   static char buf[100];
7201   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
7202   return buf;
7203 }
7204
7205 /* The AOF assembler is religiously strict about declarations of
7206    imported and exported symbols, so that it is impossible to declare
7207    a function as imported near the beginning of the file, and then to
7208    export it later on.  It is, however, possible to delay the decision
7209    until all the functions in the file have been compiled.  To get
7210    around this, we maintain a list of the imports and exports, and
7211    delete from it any that are subsequently defined.  At the end of
7212    compilation we spit the remainder of the list out before the END
7213    directive.  */
7214
7215 struct import
7216 {
7217   struct import * next;
7218   char * name;
7219 };
7220
7221 static struct import * imports_list = NULL;
7222
7223 void
7224 aof_add_import (name)
7225      char * name;
7226 {
7227   struct import * new;
7228
7229   for (new = imports_list; new; new = new->next)
7230     if (new->name == name)
7231       return;
7232
7233   new = (struct import *) xmalloc (sizeof (struct import));
7234   new->next = imports_list;
7235   imports_list = new;
7236   new->name = name;
7237 }
7238
7239 void
7240 aof_delete_import (name)
7241      char * name;
7242 {
7243   struct import ** old;
7244
7245   for (old = &imports_list; *old; old = & (*old)->next)
7246     {
7247       if ((*old)->name == name)
7248         {
7249           *old = (*old)->next;
7250           return;
7251         }
7252     }
7253 }
7254
7255 int arm_main_function = 0;
7256
7257 void
7258 aof_dump_imports (f)
7259      FILE * f;
7260 {
7261   /* The AOF assembler needs this to cause the startup code to be extracted
7262      from the library.  Brining in __main causes the whole thing to work
7263      automagically.  */
7264   if (arm_main_function)
7265     {
7266       text_section ();
7267       fputs ("\tIMPORT __main\n", f);
7268       fputs ("\tDCD __main\n", f);
7269     }
7270
7271   /* Now dump the remaining imports.  */
7272   while (imports_list)
7273     {
7274       fprintf (f, "\tIMPORT\t");
7275       assemble_name (f, imports_list->name);
7276       fputc ('\n', f);
7277       imports_list = imports_list->next;
7278     }
7279 }
7280 #endif /* AOF_ASSEMBLER */