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