OSDN Git Service

* config/m68k/m68k.c (m68k_save_reg): Also save A5 for non-leaf
[pf3gnuchains/gcc-fork.git] / gcc / config / m68k / m68k.c
1 /* Subroutines for insn-output.c for Motorola 68000 family.
2    Copyright (C) 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2003, 2004
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "function.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "recog.h"
38 #include "toplev.h"
39 #include "expr.h"
40 #include "reload.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "debug.h"
45 #include "flags.h"
46
47 enum reg_class regno_reg_class[] =
48 {
49   DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
50   DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
51   ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
52   ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
53   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
54   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
55   ADDR_REGS
56 };
57
58
59 /* The ASM_DOT macro allows easy string pasting to handle the differences
60    between MOTOROLA and MIT syntaxes in asm_fprintf(), which doesn't
61    support the %. option.  */
62 #if MOTOROLA
63 # define ASM_DOT "."
64 # define ASM_DOTW ".w"
65 # define ASM_DOTL ".l"
66 #else
67 # define ASM_DOT ""
68 # define ASM_DOTW ""
69 # define ASM_DOTL ""
70 #endif
71
72
73 /* Structure describing stack frame layout.  */
74 struct m68k_frame
75 {
76   /* Stack pointer to frame pointer offset.  */
77   HOST_WIDE_INT offset;
78
79   /* Offset of FPU registers.  */
80   HOST_WIDE_INT foffset;
81
82   /* Frame size in bytes (rounded up).  */
83   HOST_WIDE_INT size;
84
85   /* Data and address register.  */
86   int reg_no;
87   unsigned int reg_mask;
88   unsigned int reg_rev_mask;
89
90   /* FPU registers.  */
91   int fpu_no;
92   unsigned int fpu_mask;
93   unsigned int fpu_rev_mask;
94
95   /* Offsets relative to ARG_POINTER.  */
96   HOST_WIDE_INT frame_pointer_offset;
97   HOST_WIDE_INT stack_pointer_offset;
98
99   /* Function which the above information refers to.  */
100   int funcdef_no;
101 };
102
103 /* Current frame information calculated by m68k_compute_frame_layout().  */
104 static struct m68k_frame current_frame;
105
106 static rtx find_addr_reg (rtx);
107 static const char *singlemove_string (rtx *);
108 static void m68k_output_function_prologue (FILE *, HOST_WIDE_INT);
109 static void m68k_output_function_epilogue (FILE *, HOST_WIDE_INT);
110 #ifdef M68K_TARGET_COFF
111 static void m68k_coff_asm_named_section (const char *, unsigned int, tree);
112 #endif /* M68K_TARGET_COFF */
113 static void m68k_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
114                                           HOST_WIDE_INT, tree);
115 static rtx m68k_struct_value_rtx (tree, int);
116 static bool m68k_interrupt_function_p (tree func);
117 static tree m68k_handle_fndecl_attribute (tree *node, tree name,
118                                           tree args, int flags,
119                                           bool *no_add_attrs);
120 static void m68k_compute_frame_layout (void);
121 static bool m68k_save_reg (unsigned int regno, bool interrupt_handler);
122 static int const_int_cost (rtx);
123 static bool m68k_rtx_costs (rtx, int, int, int *);
124 \f
125
126 /* Specify the identification number of the library being built */
127 const char *m68k_library_id_string;
128
129 /* Nonzero if the last compare/test insn had FP operands.  The
130    sCC expanders peek at this to determine what to do for the
131    68060, which has no fsCC instructions.  */
132 int m68k_last_compare_had_fp_operands;
133 \f
134 /* Initialize the GCC target structure.  */
135
136 #if INT_OP_GROUP == INT_OP_DOT_WORD
137 #undef TARGET_ASM_ALIGNED_HI_OP
138 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
139 #endif
140
141 #if INT_OP_GROUP == INT_OP_NO_DOT
142 #undef TARGET_ASM_BYTE_OP
143 #define TARGET_ASM_BYTE_OP "\tbyte\t"
144 #undef TARGET_ASM_ALIGNED_HI_OP
145 #define TARGET_ASM_ALIGNED_HI_OP "\tshort\t"
146 #undef TARGET_ASM_ALIGNED_SI_OP
147 #define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
148 #endif
149
150 #if INT_OP_GROUP == INT_OP_DC
151 #undef TARGET_ASM_BYTE_OP
152 #define TARGET_ASM_BYTE_OP "\tdc.b\t"
153 #undef TARGET_ASM_ALIGNED_HI_OP
154 #define TARGET_ASM_ALIGNED_HI_OP "\tdc.w\t"
155 #undef TARGET_ASM_ALIGNED_SI_OP
156 #define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
157 #endif
158
159 #undef TARGET_ASM_UNALIGNED_HI_OP
160 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
161 #undef TARGET_ASM_UNALIGNED_SI_OP
162 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
163
164 #undef TARGET_ASM_FUNCTION_PROLOGUE
165 #define TARGET_ASM_FUNCTION_PROLOGUE m68k_output_function_prologue
166 #undef TARGET_ASM_FUNCTION_EPILOGUE
167 #define TARGET_ASM_FUNCTION_EPILOGUE m68k_output_function_epilogue
168
169 #undef TARGET_ASM_OUTPUT_MI_THUNK
170 #define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
171 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
172 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
173
174 #undef TARGET_ASM_FILE_START_APP_OFF
175 #define TARGET_ASM_FILE_START_APP_OFF true
176
177 #undef TARGET_RTX_COSTS
178 #define TARGET_RTX_COSTS m68k_rtx_costs
179
180 #undef TARGET_ATTRIBUTE_TABLE
181 #define TARGET_ATTRIBUTE_TABLE m68k_attribute_table
182
183 #undef TARGET_PROMOTE_PROTOTYPES
184 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
185
186 #undef TARGET_STRUCT_VALUE_RTX
187 #define TARGET_STRUCT_VALUE_RTX m68k_struct_value_rtx
188
189 static const struct attribute_spec m68k_attribute_table[] =
190 {
191   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
192   { "interrupt_handler", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
193   { NULL,                0, 0, false, false, false, NULL }
194 };
195
196 struct gcc_target targetm = TARGET_INITIALIZER;
197 \f
198 /* Sometimes certain combinations of command options do not make
199    sense on a particular target machine.  You can define a macro
200    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
201    defined, is executed once just after all the command options have
202    been parsed.
203
204    Don't use this macro to turn on various extra optimizations for
205    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
206
207 void
208 override_options (void)
209 {
210   /* Library identification */
211   if (m68k_library_id_string)
212     {
213       int id;
214
215       if (! TARGET_ID_SHARED_LIBRARY)
216         error ("-mshared-library-id= specified without -mid-shared-library");
217       id = atoi (m68k_library_id_string);
218       if (id < 0 || id > MAX_LIBRARY_ID)
219         error ("-mshared-library-id=%d is not between 0 and %d", id, MAX_LIBRARY_ID);
220
221       /* From now on, m68k_library_id_string will contain the library offset.  */
222       asprintf ((char **)&m68k_library_id_string, "%d", (id * -4) - 4);
223     }
224   else
225     /* If TARGET_ID_SHARED_LIBRARY is enabled, this will point to the
226        current library.  */
227     m68k_library_id_string = "_current_shared_library_a5_offset_";
228
229   /* Sanity check to ensure that msep-data and mid-sahred-library are not
230    * both specified together.  Doing so simply doesn't make sense.
231    */
232   if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
233     error ("cannot specify both -msep-data and -mid-shared-library");
234
235   /* If we're generating code for a separate A5 relative data segment,
236    * we've got to enable -fPIC as well.  This might be relaxable to
237    * -fpic but it hasn't been tested properly.
238    */
239   if (TARGET_SEP_DATA || TARGET_ID_SHARED_LIBRARY)
240     flag_pic = 2;
241
242   /* -fPIC uses 32-bit pc-relative displacements, which don't exist
243      until the 68020.  */
244   if (!TARGET_68020 && !TARGET_COLDFIRE && (flag_pic == 2))
245     error("-fPIC is not currently supported on the 68000 or 68010\n");
246
247   /* ??? A historic way of turning on pic, or is this intended to
248      be an embedded thing that doesn't have the same name binding
249      significance that it does on hosted ELF systems?  */
250   if (TARGET_PCREL && flag_pic == 0)
251     flag_pic = 1;
252
253   /* Turn off function cse if we are doing PIC.  We always want function call
254      to be done as `bsr foo@PLTPC', so it will force the assembler to create
255      the PLT entry for `foo'. Doing function cse will cause the address of
256      `foo' to be loaded into a register, which is exactly what we want to
257      avoid when we are doing PIC on svr4 m68k.  */
258   if (flag_pic)
259     flag_no_function_cse = 1;
260
261   SUBTARGET_OVERRIDE_OPTIONS;
262 }
263 \f
264 /* Return nonzero if FUNC is an interrupt function as specified by the
265    "interrupt_handler" attribute.  */
266 static bool
267 m68k_interrupt_function_p(tree func)
268 {
269   tree a;
270
271   if (TREE_CODE (func) != FUNCTION_DECL)
272     return false;
273
274   a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
275   return (a != NULL_TREE);
276 }
277
278 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
279    struct attribute_spec.handler.  */
280 static tree
281 m68k_handle_fndecl_attribute (tree *node, tree name,
282                               tree args ATTRIBUTE_UNUSED,
283                               int flags ATTRIBUTE_UNUSED,
284                               bool *no_add_attrs)
285 {
286   if (TREE_CODE (*node) != FUNCTION_DECL)
287     {
288       warning ("%qs attribute only applies to functions",
289                IDENTIFIER_POINTER (name));
290       *no_add_attrs = true;
291     }
292
293   return NULL_TREE;
294 }
295
296 static void
297 m68k_compute_frame_layout (void)
298 {
299   int regno, saved;
300   unsigned int mask, rmask;
301   bool interrupt_handler = m68k_interrupt_function_p (current_function_decl);
302
303   /* Only compute the frame once per function.
304      Don't cache information until reload has been completed.  */
305   if (current_frame.funcdef_no == current_function_funcdef_no
306       && reload_completed)
307     return;
308
309   current_frame.size = (get_frame_size () + 3) & -4;
310
311   mask = rmask = saved = 0;
312   for (regno = 0; regno < 16; regno++)
313     if (m68k_save_reg (regno, interrupt_handler))
314       {
315         mask |= 1 << regno;
316         rmask |= 1 << (15 - regno);
317         saved++;
318       }
319   current_frame.offset = saved * 4;
320   current_frame.reg_no = saved;
321   current_frame.reg_mask = mask;
322   current_frame.reg_rev_mask = rmask;
323
324   current_frame.foffset = 0;
325   mask = rmask = saved = 0;
326   if (TARGET_68881 /* || TARGET_CFV4E */)
327     {
328       for (regno = 16; regno < 24; regno++)
329         if (m68k_save_reg (regno, interrupt_handler))
330           {
331             mask |= 1 << (regno - 16);
332             rmask |= 1 << (23 - regno);
333             saved++;
334           }
335       current_frame.foffset = saved * 12 /* (TARGET_CFV4E ? 8 : 12) */;
336       current_frame.offset += current_frame.foffset;
337     }
338   current_frame.fpu_no = saved;
339   current_frame.fpu_mask = mask;
340   current_frame.fpu_rev_mask = rmask;
341
342   /* Remember what function this frame refers to.  */
343   current_frame.funcdef_no = current_function_funcdef_no;
344 }
345
346 HOST_WIDE_INT
347 m68k_initial_elimination_offset (int from, int to)
348 {
349   /* FIXME: The correct offset to compute here would appear to be
350        (frame_pointer_needed ? -UNITS_PER_WORD * 2 : -UNITS_PER_WORD);
351      but for some obscure reason, this must be 0 to get correct code.  */
352   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
353     return 0;
354
355   m68k_compute_frame_layout ();
356
357   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
358     return current_frame.offset + current_frame.size + (frame_pointer_needed ? -UNITS_PER_WORD * 2 : -UNITS_PER_WORD);
359   else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
360     return current_frame.offset + current_frame.size;
361
362   abort();
363 }
364
365 /* Refer to the array `regs_ever_live' to determine which registers
366    to save; `regs_ever_live[I]' is nonzero if register number I
367    is ever used in the function.  This function is responsible for
368    knowing which registers should not be saved even if used.
369    Return true if we need to save REGNO.  */
370
371 static bool
372 m68k_save_reg (unsigned int regno, bool interrupt_handler)
373 {
374   if (flag_pic && regno == PIC_OFFSET_TABLE_REGNUM)
375     {
376       if (current_function_uses_pic_offset_table)
377         return true;
378       if (!current_function_is_leaf && TARGET_ID_SHARED_LIBRARY)
379         return true;
380     }
381
382   if (current_function_calls_eh_return)
383     {
384       unsigned int i;
385       for (i = 0; ; i++)
386         {
387           unsigned int test = EH_RETURN_DATA_REGNO (i);
388           if (test == INVALID_REGNUM)
389             break;
390           if (test == regno)
391             return true;
392         }
393     }
394
395   /* Fixed regs we never touch.  */
396   if (fixed_regs[regno])
397     return false;
398
399   /* The frame pointer (if it is such) is handled specially.  */
400   if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
401     return false;
402
403   /* Interrupt handlers must also save call_used_regs
404      if they are live or when calling nested functions.  */
405   if (interrupt_handler)
406   {
407      if (regs_ever_live[regno])
408        return true;
409
410      if (!current_function_is_leaf && call_used_regs[regno])
411        return true;
412   }
413
414   /* Never need to save registers that aren't touched.  */
415   if (!regs_ever_live[regno])
416     return false;
417
418   /* Otherwise save everything that isn't call-clobbered.  */
419   return !call_used_regs[regno];
420 }
421
422 /* This function generates the assembly code for function entry.
423    STREAM is a stdio stream to output the code to.
424    SIZE is an int: how many units of temporary storage to allocate.  */
425
426 static void
427 m68k_output_function_prologue (FILE *stream, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
428 {
429   HOST_WIDE_INT fsize_with_regs;
430   HOST_WIDE_INT cfa_offset = INCOMING_FRAME_SP_OFFSET;
431
432   m68k_compute_frame_layout();
433
434   /* If the stack limit is a symbol, we can check it here,
435      before actually allocating the space.  */
436   if (current_function_limit_stack
437       && GET_CODE (stack_limit_rtx) == SYMBOL_REF)
438     asm_fprintf (stream, "\tcmp" ASM_DOT "l %I%s+%wd,%Rsp\n\ttrapcs\n",
439                  XSTR (stack_limit_rtx, 0), current_frame.size + 4);
440
441   /* On ColdFire add register save into initial stack frame setup, if possible.  */
442   fsize_with_regs = current_frame.size;
443   if (TARGET_COLDFIRE && current_frame.reg_no > 2)
444     fsize_with_regs += current_frame.reg_no * 4;
445
446   if (frame_pointer_needed)
447     {
448       if (current_frame.size == 0 && TARGET_68040)
449         /* on the 68040, pea + move is faster than link.w 0 */
450         fprintf (stream, MOTOROLA ?
451                            "\tpea (%s)\n\tmove.l %s,%s\n" :
452                            "\tpea %s@\n\tmovel %s,%s\n",
453                  M68K_REGNAME(FRAME_POINTER_REGNUM),
454                  M68K_REGNAME(STACK_POINTER_REGNUM),
455                  M68K_REGNAME(FRAME_POINTER_REGNUM));
456       else if (fsize_with_regs < 0x8000)
457         asm_fprintf (stream, "\tlink" ASM_DOTW " %s,%I%wd\n",
458                      M68K_REGNAME(FRAME_POINTER_REGNUM), -fsize_with_regs);
459       else if (TARGET_68020)
460         asm_fprintf (stream, "\tlink" ASM_DOTL " %s,%I%wd\n",
461                      M68K_REGNAME(FRAME_POINTER_REGNUM), -fsize_with_regs);
462       else
463         /* Adding negative number is faster on the 68040.  */
464         asm_fprintf (stream, "\tlink" ASM_DOTW " %s,%I0\n"
465                              "\tadd" ASM_DOT "l %I%wd,%Rsp\n",
466                      M68K_REGNAME(FRAME_POINTER_REGNUM), -fsize_with_regs);
467
468       if (dwarf2out_do_frame ())
469         {
470           char *l;
471           l = (char *) dwarf2out_cfi_label ();
472           cfa_offset += 4;
473           dwarf2out_reg_save (l, FRAME_POINTER_REGNUM, -cfa_offset);
474           dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, cfa_offset);
475           cfa_offset += current_frame.size;
476         }
477     }
478   else if (fsize_with_regs) /* !frame_pointer_needed */
479     {
480       if (fsize_with_regs < 0x8000)
481         {
482           if (fsize_with_regs <= 8)
483             {
484               if (!TARGET_COLDFIRE)
485                 asm_fprintf (stream, "\tsubq" ASM_DOT "w %I%wd,%Rsp\n",
486                              fsize_with_regs);
487               else
488                 asm_fprintf (stream, "\tsubq" ASM_DOT "l %I%wd,%Rsp\n",
489                              fsize_with_regs);
490             }
491           else if (fsize_with_regs <= 16 && TARGET_CPU32)
492             /* On the CPU32 it is faster to use two subqw instructions to
493                subtract a small integer (8 < N <= 16) to a register.  */
494             asm_fprintf (stream,
495                          "\tsubq" ASM_DOT "w %I8,%Rsp\n"
496                          "\tsubq" ASM_DOT "w %I%wd,%Rsp\n",
497                          fsize_with_regs - 8);
498           else if (TARGET_68040)
499             /* Adding negative number is faster on the 68040.  */
500             asm_fprintf (stream, "\tadd" ASM_DOT "w %I%wd,%Rsp\n",
501                          -fsize_with_regs);
502           else
503             asm_fprintf (stream, MOTOROLA ?
504                                    "\tlea (%wd,%Rsp),%Rsp\n" :
505                                    "\tlea %Rsp@(%wd),%Rsp\n",
506                          -fsize_with_regs);
507         }
508       else /* fsize_with_regs >= 0x8000 */
509         asm_fprintf (stream, "\tadd" ASM_DOT "l %I%wd,%Rsp\n", -fsize_with_regs);
510
511       if (dwarf2out_do_frame ())
512         {
513           cfa_offset += current_frame.size + 4;
514           dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, cfa_offset);
515         }
516     } /* !frame_pointer_needed */
517
518   if (current_frame.fpu_mask)
519     {
520       asm_fprintf (stream, MOTOROLA ?
521                              "\tfmovm %I0x%x,-(%Rsp)\n" :
522                              "\tfmovem %I0x%x,%Rsp@-\n",
523                    current_frame.fpu_mask);
524
525       if (dwarf2out_do_frame ())
526         {
527           char *l = (char *) dwarf2out_cfi_label ();
528           int n_regs, regno;
529
530           cfa_offset += current_frame.fpu_no * 12;
531           if (! frame_pointer_needed)
532             dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
533           for (regno = 16, n_regs = 0; regno < 24; regno++)
534             if (current_frame.fpu_mask & (1 << (regno - 16)))
535               dwarf2out_reg_save (l, regno, -cfa_offset + n_regs++ * 12);
536         }
537     }
538
539   /* If the stack limit is not a symbol, check it here.
540      This has the disadvantage that it may be too late...  */
541   if (current_function_limit_stack)
542     {
543       if (REG_P (stack_limit_rtx))
544         asm_fprintf (stream, "\tcmp" ASM_DOT "l %s,%Rsp\n\ttrapcs\n",
545                      M68K_REGNAME(REGNO (stack_limit_rtx)));
546       else if (GET_CODE (stack_limit_rtx) != SYMBOL_REF)
547         warning ("stack limit expression is not supported");
548     }
549
550   if (current_frame.reg_no <= 2)
551     {
552       /* Store each separately in the same order moveml uses.
553          Using two movel instructions instead of a single moveml
554          is about 15% faster for the 68020 and 68030 at no expense
555          in code size.  */
556
557       int i;
558
559       for (i = 0; i < 16; i++)
560         if (current_frame.reg_rev_mask & (1 << i))
561           {
562             asm_fprintf (stream, MOTOROLA ?
563                                    "\t%Omove.l %s,-(%Rsp)\n" :
564                                    "\tmovel %s,%Rsp@-\n",
565                          M68K_REGNAME(15 - i));
566             if (dwarf2out_do_frame ())
567               {
568                 char *l = (char *) dwarf2out_cfi_label ();
569
570                 cfa_offset += 4;
571                 if (! frame_pointer_needed)
572                   dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
573                 dwarf2out_reg_save (l, 15 - i, -cfa_offset);
574               }
575           }
576     }
577   else if (current_frame.reg_rev_mask)
578     {
579       if (TARGET_COLDFIRE)
580         /* The ColdFire does not support the predecrement form of the
581            MOVEM instruction, so we must adjust the stack pointer and
582            then use the plain address register indirect mode.
583            The required register save space was combined earlier with
584            the fsize_with_regs amount.  */
585
586         asm_fprintf (stream, MOTOROLA ?
587                                "\tmovm.l %I0x%x,(%Rsp)\n" :
588                                "\tmoveml %I0x%x,%Rsp@\n",
589                      current_frame.reg_mask);
590       else
591         asm_fprintf (stream, MOTOROLA ?
592                                "\tmovm.l %I0x%x,-(%Rsp)\n" :
593                                "\tmoveml %I0x%x,%Rsp@-\n",
594                      current_frame.reg_rev_mask);
595       if (dwarf2out_do_frame ())
596         {
597           char *l = (char *) dwarf2out_cfi_label ();
598           int n_regs, regno;
599
600           cfa_offset += current_frame.reg_no * 4;
601           if (! frame_pointer_needed)
602             dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
603           for (regno = 0, n_regs = 0; regno < 16; regno++)
604             if (current_frame.reg_mask & (1 << regno))
605               dwarf2out_reg_save (l, regno, -cfa_offset + n_regs++ * 4);
606         }
607     }
608   if (!TARGET_SEP_DATA && flag_pic &&
609       (current_function_uses_pic_offset_table ||
610         (!current_function_is_leaf && TARGET_ID_SHARED_LIBRARY)))
611     {
612       if (TARGET_ID_SHARED_LIBRARY)
613         {
614           asm_fprintf (stream, "\tmovel %s@(%s), %s\n",
615                        M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM),
616                        m68k_library_id_string,
617                        M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM));
618         }
619       else
620         {
621           if (MOTOROLA)
622             asm_fprintf (stream, "\t%Olea (%Rpc, %U_GLOBAL_OFFSET_TABLE_@GOTPC), %s\n",
623                          M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM));
624           else
625             {
626               asm_fprintf (stream, "\tmovel %I%U_GLOBAL_OFFSET_TABLE_, %s\n",
627                            M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM));
628               asm_fprintf (stream, "\tlea %Rpc@(0,%s:l),%s\n",
629                            M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM),
630                            M68K_REGNAME(PIC_OFFSET_TABLE_REGNUM));
631             }
632         }
633     }
634 }
635 \f
636 /* Return true if this function's epilogue can be output as RTL.  */
637
638 bool
639 use_return_insn (void)
640 {
641   if (!reload_completed || frame_pointer_needed || get_frame_size () != 0)
642     return false;
643
644   /* We can output the epilogue as RTL only if no registers need to be
645      restored.  */
646   m68k_compute_frame_layout();
647   return current_frame.reg_no ? false : true;
648 }
649
650 /* This function generates the assembly code for function exit,
651    on machines that need it.
652
653    The function epilogue should not depend on the current stack pointer!
654    It should use the frame pointer only, if there is a frame pointer.
655    This is mandatory because of alloca; we also take advantage of it to
656    omit stack adjustments before returning.  */
657
658 static void
659 m68k_output_function_epilogue (FILE *stream, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
660 {
661   HOST_WIDE_INT fsize, fsize_with_regs;
662   bool big = false;
663   bool restore_from_sp = false;
664   rtx insn = get_last_insn ();
665
666   m68k_compute_frame_layout();
667
668   /* If the last insn was a BARRIER, we don't have to write any code.  */
669   if (GET_CODE (insn) == NOTE)
670     insn = prev_nonnote_insn (insn);
671   if (insn && GET_CODE (insn) == BARRIER)
672     {
673       /* Output just a no-op so that debuggers don't get confused
674          about which function the pc is in at this address.  */
675       fprintf (stream, "\tnop\n");
676       return;
677     }
678
679 #ifdef FUNCTION_EXTRA_EPILOGUE
680   FUNCTION_EXTRA_EPILOGUE (stream, size);
681 #endif
682
683   fsize = current_frame.size;
684
685   /* FIXME : leaf_function_p below is too strong.
686      What we really need to know there is if there could be pending
687      stack adjustment needed at that point.  */
688   restore_from_sp = ! frame_pointer_needed
689              || (! current_function_calls_alloca && leaf_function_p ());
690
691   /* fsize_with_regs is the size we need to adjust the sp when
692      popping the frame.  */
693   fsize_with_regs = fsize;
694
695   /* Because the ColdFire doesn't support moveml with
696      complex address modes, we must adjust the stack manually
697      after restoring registers. When the frame pointer isn't used,
698      we can merge movem adjustment into frame unlinking
699      made immediately after it.  */
700   if (TARGET_COLDFIRE && restore_from_sp && (current_frame.reg_no > 2))
701     fsize_with_regs += current_frame.reg_no * 4;
702
703   if (current_frame.offset + fsize >= 0x8000
704       && ! restore_from_sp
705       && (current_frame.reg_mask || current_frame.fpu_mask))
706     {
707       /* Because the ColdFire doesn't support moveml with
708          complex address modes we make an extra correction here.  */
709       if (TARGET_COLDFIRE)
710         fsize += current_frame.offset;
711
712       asm_fprintf (stream, "\t%Omove" ASM_DOT "l %I%wd,%Ra1\n", -fsize);
713       fsize = 0, big = true;
714     }
715   if (current_frame.reg_no <= 2)
716     {
717       /* Restore each separately in the same order moveml does.
718          Using two movel instructions instead of a single moveml
719          is about 15% faster for the 68020 and 68030 at no expense
720          in code size.  */
721
722       int i;
723       HOST_WIDE_INT offset = current_frame.offset + fsize;
724
725       for (i = 0; i < 16; i++)
726         if (current_frame.reg_mask & (1 << i))
727           {
728             if (big)
729               {
730                 if (MOTOROLA)
731                   asm_fprintf (stream, "\t%Omove.l -%wd(%s,%Ra1.l),%s\n",
732                                offset,
733                                M68K_REGNAME(FRAME_POINTER_REGNUM),
734                                M68K_REGNAME(i));
735                 else
736                   asm_fprintf (stream, "\tmovel %s@(-%wd,%Ra1:l),%s\n",
737                                M68K_REGNAME(FRAME_POINTER_REGNUM),
738                                offset,
739                                M68K_REGNAME(i));
740               }
741             else if (restore_from_sp)
742               asm_fprintf (stream, MOTOROLA ?
743                                      "\t%Omove.l (%Rsp)+,%s\n" :
744                                      "\tmovel %Rsp@+,%s\n",
745                            M68K_REGNAME(i));
746             else
747               {
748                 if (MOTOROLA)
749                   asm_fprintf (stream, "\t%Omove.l -%wd(%s),%s\n",
750                                offset,
751                                M68K_REGNAME(FRAME_POINTER_REGNUM),
752                                M68K_REGNAME(i));
753                 else
754                   asm_fprintf (stream, "\tmovel %s@(-%wd),%s\n",
755                                M68K_REGNAME(FRAME_POINTER_REGNUM),
756                                offset,
757                                M68K_REGNAME(i));
758               }
759             offset -= 4;
760           }
761     }
762   else if (current_frame.reg_mask)
763     {
764       /* The ColdFire requires special handling due to its limited moveml insn.  */
765       if (TARGET_COLDFIRE)
766         {
767           if (big)
768             {
769               asm_fprintf (stream, "\tadd" ASM_DOT "l %s,%Ra1\n",
770                            M68K_REGNAME(FRAME_POINTER_REGNUM));
771               asm_fprintf (stream, MOTOROLA ?
772                                      "\tmovm.l (%Ra1),%I0x%x\n" :
773                                      "\tmoveml %Ra1@,%I0x%x\n",
774                            current_frame.reg_mask);
775              }
776            else if (restore_from_sp)
777              asm_fprintf (stream, MOTOROLA ?
778                                     "\tmovm.l (%Rsp),%I0x%x\n" :
779                                     "\tmoveml %Rsp@,%I0x%x\n",
780                           current_frame.reg_mask);
781           else
782             {
783               if (MOTOROLA)
784                 asm_fprintf (stream, "\tmovm.l -%wd(%s),%I0x%x\n",
785                              current_frame.offset + fsize,
786                              M68K_REGNAME(FRAME_POINTER_REGNUM),
787                              current_frame.reg_mask);
788               else
789                 asm_fprintf (stream, "\tmoveml %s@(-%wd),%I0x%x\n",
790                              M68K_REGNAME(FRAME_POINTER_REGNUM),
791                              current_frame.offset + fsize,
792                              current_frame.reg_mask);
793             }
794         }
795       else /* !TARGET_COLDFIRE */
796         {
797           if (big)
798             {
799               if (MOTOROLA)
800                 asm_fprintf (stream, "\tmovm.l -%wd(%s,%Ra1.l),%I0x%x\n",
801                              current_frame.offset + fsize,
802                              M68K_REGNAME(FRAME_POINTER_REGNUM),
803                              current_frame.reg_mask);
804               else
805                 asm_fprintf (stream, "\tmoveml %s@(-%wd,%Ra1:l),%I0x%x\n",
806                              M68K_REGNAME(FRAME_POINTER_REGNUM),
807                              current_frame.offset + fsize,
808                              current_frame.reg_mask);
809             }
810           else if (restore_from_sp)
811             {
812               asm_fprintf (stream, MOTOROLA ?
813                                      "\tmovm.l (%Rsp)+,%I0x%x\n" :
814                                      "\tmoveml %Rsp@+,%I0x%x\n",
815                            current_frame.reg_mask);
816             }
817           else
818             {
819               if (MOTOROLA)
820                 asm_fprintf (stream, "\tmovm.l -%wd(%s),%I0x%x\n",
821                              current_frame.offset + fsize,
822                              M68K_REGNAME(FRAME_POINTER_REGNUM),
823                              current_frame.reg_mask);
824               else
825                 asm_fprintf (stream, "\tmoveml %s@(-%wd),%I0x%x\n",
826                              M68K_REGNAME(FRAME_POINTER_REGNUM),
827                              current_frame.offset + fsize,
828                              current_frame.reg_mask);
829             }
830         }
831     }
832   if (current_frame.fpu_rev_mask)
833     {
834       if (big)
835         {
836           if (MOTOROLA)
837             asm_fprintf (stream, "\tfmovm -%wd(%s,%Ra1.l),%I0x%x\n",
838                          current_frame.foffset + fsize,
839                          M68K_REGNAME(FRAME_POINTER_REGNUM),
840                          current_frame.fpu_rev_mask);
841           else
842             asm_fprintf (stream, "\tfmovem %s@(-%wd,%Ra1:l),%I0x%x\n",
843                          M68K_REGNAME(FRAME_POINTER_REGNUM),
844                          current_frame.foffset + fsize,
845                          current_frame.fpu_rev_mask);
846         }
847       else if (restore_from_sp)
848         {
849           if (MOTOROLA)
850             asm_fprintf (stream, "\tfmovm (%Rsp)+,%I0x%x\n",
851                          current_frame.fpu_rev_mask);
852           else
853             asm_fprintf (stream, "\tfmovem %Rsp@+,%I0x%x\n",
854                          current_frame.fpu_rev_mask);
855         }
856       else
857         {
858           if (MOTOROLA)
859             asm_fprintf (stream, "\tfmovm -%wd(%s),%I0x%x\n",
860                          current_frame.foffset + fsize,
861                          M68K_REGNAME(FRAME_POINTER_REGNUM),
862                          current_frame.fpu_rev_mask);
863           else
864             asm_fprintf (stream, "\tfmovem %s@(-%wd),%I0x%x\n",
865                          M68K_REGNAME(FRAME_POINTER_REGNUM),
866                          current_frame.foffset + fsize,
867                          current_frame.fpu_rev_mask);
868         }
869     }
870   if (frame_pointer_needed)
871     fprintf (stream, "\tunlk %s\n", M68K_REGNAME(FRAME_POINTER_REGNUM));
872   else if (fsize_with_regs)
873     {
874       if (fsize_with_regs <= 8)
875         {
876           if (!TARGET_COLDFIRE)
877             asm_fprintf (stream, "\taddq" ASM_DOT "w %I%wd,%Rsp\n",
878                          fsize_with_regs);
879           else
880             asm_fprintf (stream, "\taddq" ASM_DOT "l %I%wd,%Rsp\n",
881                          fsize_with_regs);
882         }
883       else if (fsize_with_regs <= 16 && TARGET_CPU32)
884         {
885           /* On the CPU32 it is faster to use two addqw instructions to
886              add a small integer (8 < N <= 16) to a register.  */
887           asm_fprintf (stream, "\taddq" ASM_DOT "w %I8,%Rsp\n"
888                                "\taddq" ASM_DOT "w %I%wd,%Rsp\n",
889                        fsize_with_regs - 8);
890         }
891       else if (fsize_with_regs < 0x8000)
892         {
893           if (TARGET_68040)
894             asm_fprintf (stream, "\tadd" ASM_DOT "w %I%wd,%Rsp\n",
895                          fsize_with_regs);
896           else
897             asm_fprintf (stream, MOTOROLA ?
898                                    "\tlea (%wd,%Rsp),%Rsp\n" :
899                                    "\tlea %Rsp@(%wd),%Rsp\n",
900                          fsize_with_regs);
901         }
902       else
903         asm_fprintf (stream, "\tadd" ASM_DOT "l %I%wd,%Rsp\n", fsize_with_regs);
904     }
905   if (current_function_calls_eh_return)
906     asm_fprintf (stream, "\tadd" ASM_DOT"l %Ra0,%Rsp\n");
907   if (m68k_interrupt_function_p (current_function_decl))
908     fprintf (stream, "\trte\n");
909   else if (current_function_pops_args)
910     asm_fprintf (stream, "\trtd %I%d\n", current_function_pops_args);
911   else
912     fprintf (stream, "\trts\n");
913 }
914 \f
915 /* Similar to general_operand, but exclude stack_pointer_rtx.  */
916
917 int
918 not_sp_operand (rtx op, enum machine_mode mode)
919 {
920   return op != stack_pointer_rtx && nonimmediate_operand (op, mode);
921 }
922
923 /* Return true if X is a valid comparison operator for the dbcc 
924    instruction.  
925
926    Note it rejects floating point comparison operators.
927    (In the future we could use Fdbcc).
928
929    It also rejects some comparisons when CC_NO_OVERFLOW is set.  */
930    
931 int
932 valid_dbcc_comparison_p (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
933 {
934   switch (GET_CODE (x))
935     {
936       case EQ: case NE: case GTU: case LTU:
937       case GEU: case LEU:
938         return 1;
939
940       /* Reject some when CC_NO_OVERFLOW is set.  This may be over
941          conservative */
942       case GT: case LT: case GE: case LE:
943         return ! (cc_prev_status.flags & CC_NO_OVERFLOW);
944       default:
945         return 0;
946     }
947 }
948
949 /* Return nonzero if flags are currently in the 68881 flag register.  */
950 int
951 flags_in_68881 (void)
952 {
953   /* We could add support for these in the future */
954   return cc_status.flags & CC_IN_68881;
955 }
956
957 /* Output a BSR instruction suitable for PIC code.  */
958 void
959 m68k_output_pic_call(rtx dest)
960 {
961   const char *out;
962
963   if (!(GET_CODE (dest) == MEM && GET_CODE (XEXP (dest, 0)) == SYMBOL_REF))
964     out = "jsr %0";
965       /* We output a BSR instruction if we're using -fpic or we're building for
966        * a target that supports long branches.  If we're building -fPIC on the
967        * 68000, 68010 or ColdFire we generate one of two sequences:
968        * a shorter one that uses a GOT entry or a longer one that doesn't.
969        * We'll use the -Os command-line flag to decide which to generate.
970        * Both sequences take the same time to execute on the ColdFire.
971        */
972   else if (TARGET_PCREL)
973     out = "bsr.l %o0";
974   else if ((flag_pic == 1) || TARGET_68020)
975 #if defined(USE_GAS)
976     out = "bsr.l %0@PLTPC";
977 #else
978     out = "bsr %0@PLTPC";
979 #endif
980   else if (optimize_size || TARGET_ID_SHARED_LIBRARY)
981     out = "move.l %0@GOT(%%a5), %%a1\n\tjsr (%%a1)";
982   else
983     out = "lea %0-.-8,%%a1\n\tjsr 0(%%pc,%%a1)";
984
985   output_asm_insn(out, &dest);
986 }
987
988 /* Output a dbCC; jCC sequence.  Note we do not handle the 
989    floating point version of this sequence (Fdbcc).  We also
990    do not handle alternative conditions when CC_NO_OVERFLOW is
991    set.  It is assumed that valid_dbcc_comparison_p and flags_in_68881 will
992    kick those out before we get here.  */
993
994 void
995 output_dbcc_and_branch (rtx *operands)
996 {
997   switch (GET_CODE (operands[3]))
998     {
999       case EQ:
1000         output_asm_insn (MOTOROLA ?
1001                            "dbeq %0,%l1\n\tjbeq %l2" :
1002                            "dbeq %0,%l1\n\tjeq %l2",
1003                          operands);
1004         break;
1005
1006       case NE:
1007         output_asm_insn (MOTOROLA ?
1008                            "dbne %0,%l1\n\tjbne %l2" :
1009                            "dbne %0,%l1\n\tjne %l2",
1010                          operands);
1011         break;
1012
1013       case GT:
1014         output_asm_insn (MOTOROLA ?
1015                            "dbgt %0,%l1\n\tjbgt %l2" :
1016                            "dbgt %0,%l1\n\tjgt %l2",
1017                          operands);
1018         break;
1019
1020       case GTU:
1021         output_asm_insn (MOTOROLA ?
1022                            "dbhi %0,%l1\n\tjbhi %l2" :
1023                            "dbhi %0,%l1\n\tjhi %l2",
1024                          operands);
1025         break;
1026
1027       case LT:
1028         output_asm_insn (MOTOROLA ?
1029                            "dblt %0,%l1\n\tjblt %l2" :
1030                            "dblt %0,%l1\n\tjlt %l2",
1031                          operands);
1032         break;
1033
1034       case LTU:
1035         output_asm_insn (MOTOROLA ?
1036                            "dbcs %0,%l1\n\tjbcs %l2" :
1037                            "dbcs %0,%l1\n\tjcs %l2",
1038                          operands);
1039         break;
1040
1041       case GE:
1042         output_asm_insn (MOTOROLA ?
1043                            "dbge %0,%l1\n\tjbge %l2" :
1044                            "dbge %0,%l1\n\tjge %l2",
1045                          operands);
1046         break;
1047
1048       case GEU:
1049         output_asm_insn (MOTOROLA ?
1050                            "dbcc %0,%l1\n\tjbcc %l2" :
1051                            "dbcc %0,%l1\n\tjcc %l2",
1052                          operands);
1053         break;
1054
1055       case LE:
1056         output_asm_insn (MOTOROLA ?
1057                            "dble %0,%l1\n\tjble %l2" :
1058                            "dble %0,%l1\n\tjle %l2",
1059                          operands);
1060         break;
1061
1062       case LEU:
1063         output_asm_insn (MOTOROLA ?
1064                            "dbls %0,%l1\n\tjbls %l2" : 
1065                            "dbls %0,%l1\n\tjls %l2",
1066                          operands);
1067         break;
1068
1069       default:
1070         abort ();
1071     }
1072
1073   /* If the decrement is to be done in SImode, then we have
1074      to compensate for the fact that dbcc decrements in HImode.  */
1075   switch (GET_MODE (operands[0]))
1076     {
1077       case SImode:
1078         output_asm_insn (MOTOROLA ?
1079                            "clr%.w %0\n\tsubq%.l #1,%0\n\tjbpl %l1" :
1080                            "clr%.w %0\n\tsubq%.l #1,%0\n\tjpl %l1",
1081                          operands);
1082         break;
1083
1084       case HImode:
1085         break;
1086
1087       default:
1088         abort ();
1089     }
1090 }
1091
1092 const char *
1093 output_scc_di(rtx op, rtx operand1, rtx operand2, rtx dest)
1094 {
1095   rtx loperands[7];
1096   enum rtx_code op_code = GET_CODE (op);
1097
1098   /* This does not produce a useful cc.  */
1099   CC_STATUS_INIT;
1100
1101   /* The m68k cmp.l instruction requires operand1 to be a reg as used
1102      below.  Swap the operands and change the op if these requirements
1103      are not fulfilled.  */
1104   if (GET_CODE (operand2) == REG && GET_CODE (operand1) != REG)
1105     {
1106       rtx tmp = operand1;
1107
1108       operand1 = operand2;
1109       operand2 = tmp;
1110       op_code = swap_condition (op_code);
1111     }
1112   loperands[0] = operand1;
1113   if (GET_CODE (operand1) == REG)
1114     loperands[1] = gen_rtx_REG (SImode, REGNO (operand1) + 1);
1115   else
1116     loperands[1] = adjust_address (operand1, SImode, 4);
1117   if (operand2 != const0_rtx)
1118     {
1119       loperands[2] = operand2;
1120       if (GET_CODE (operand2) == REG)
1121         loperands[3] = gen_rtx_REG (SImode, REGNO (operand2) + 1);
1122       else
1123         loperands[3] = adjust_address (operand2, SImode, 4);
1124     }
1125   loperands[4] = gen_label_rtx ();
1126   if (operand2 != const0_rtx)
1127     {
1128       output_asm_insn (MOTOROLA ?
1129           "cmp%.l %2,%0\n\tjbne %l4\n\tcmp%.l %3,%1" :
1130           "cmp%.l %2,%0\n\tjne %l4\n\tcmp%.l %3,%1",
1131         loperands);
1132     }
1133   else
1134     {
1135       if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[0]))
1136         output_asm_insn ("tst%.l %0", loperands);
1137       else
1138         {
1139           output_asm_insn ("cmp%.w #0,%0", loperands);
1140         }
1141
1142       output_asm_insn (MOTOROLA ? "jbne %l4" : "jne %l4", loperands);
1143
1144       if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[1]))
1145         output_asm_insn ("tst%.l %1", loperands);
1146       else
1147         output_asm_insn ("cmp%.w #0,%1", loperands);
1148     }
1149
1150   loperands[5] = dest;
1151
1152   switch (op_code)
1153     {
1154       case EQ:
1155         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1156                                     CODE_LABEL_NUMBER (loperands[4]));
1157         output_asm_insn ("seq %5", loperands);
1158         break;
1159
1160       case NE:
1161         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1162                                     CODE_LABEL_NUMBER (loperands[4]));
1163         output_asm_insn ("sne %5", loperands);
1164         break;
1165
1166       case GT:
1167         loperands[6] = gen_label_rtx ();
1168         output_asm_insn (MOTOROLA ?
1169                            "shi %5\n\tjbra %l6" :
1170                            "shi %5\n\tjra %l6",
1171                          loperands);
1172         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1173                                     CODE_LABEL_NUMBER (loperands[4]));
1174         output_asm_insn ("sgt %5", loperands);
1175         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1176                                     CODE_LABEL_NUMBER (loperands[6]));
1177         break;
1178
1179       case GTU:
1180         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1181                                     CODE_LABEL_NUMBER (loperands[4]));
1182         output_asm_insn ("shi %5", loperands);
1183         break;
1184
1185       case LT:
1186         loperands[6] = gen_label_rtx ();
1187         output_asm_insn (MOTOROLA ?
1188                            "scs %5\n\tjbra %l6" :
1189                            "scs %5\n\tjra %l6",
1190                          loperands);
1191         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1192                                     CODE_LABEL_NUMBER (loperands[4]));
1193         output_asm_insn ("slt %5", loperands);
1194         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1195                                     CODE_LABEL_NUMBER (loperands[6]));
1196         break;
1197
1198       case LTU:
1199         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1200                                     CODE_LABEL_NUMBER (loperands[4]));
1201         output_asm_insn ("scs %5", loperands);
1202         break;
1203
1204       case GE:
1205         loperands[6] = gen_label_rtx ();
1206         output_asm_insn (MOTOROLA ?
1207                            "scc %5\n\tjbra %l6" :
1208                            "scc %5\n\tjra %l6",
1209                            loperands);
1210         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1211                                     CODE_LABEL_NUMBER (loperands[4]));
1212         output_asm_insn ("sge %5", loperands);
1213         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1214                                     CODE_LABEL_NUMBER (loperands[6]));
1215         break;
1216
1217       case GEU:
1218         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1219                                     CODE_LABEL_NUMBER (loperands[4]));
1220         output_asm_insn ("scc %5", loperands);
1221         break;
1222
1223       case LE:
1224         loperands[6] = gen_label_rtx ();
1225         output_asm_insn (MOTOROLA ?
1226                            "sls %5\n\tjbra %l6" :
1227                            "sls %5\n\tjra %l6",
1228                          loperands);
1229         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1230                                     CODE_LABEL_NUMBER (loperands[4]));
1231         output_asm_insn ("sle %5", loperands);
1232         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1233                                     CODE_LABEL_NUMBER (loperands[6]));
1234         break;
1235
1236       case LEU:
1237         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1238                                     CODE_LABEL_NUMBER (loperands[4]));
1239         output_asm_insn ("sls %5", loperands);
1240         break;
1241
1242       default:
1243         abort ();
1244     }
1245   return "";
1246 }
1247
1248 const char *
1249 output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
1250 {
1251   operands[0] = countop;
1252   operands[1] = dataop;
1253
1254   if (GET_CODE (countop) == CONST_INT)
1255     {
1256       register int count = INTVAL (countop);
1257       /* If COUNT is bigger than size of storage unit in use,
1258          advance to the containing unit of same size.  */
1259       if (count > signpos)
1260         {
1261           int offset = (count & ~signpos) / 8;
1262           count = count & signpos;
1263           operands[1] = dataop = adjust_address (dataop, QImode, offset);
1264         }
1265       if (count == signpos)
1266         cc_status.flags = CC_NOT_POSITIVE | CC_Z_IN_NOT_N;
1267       else
1268         cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N;
1269
1270       /* These three statements used to use next_insns_test_no...
1271          but it appears that this should do the same job.  */
1272       if (count == 31
1273           && next_insn_tests_no_inequality (insn))
1274         return "tst%.l %1";
1275       if (count == 15
1276           && next_insn_tests_no_inequality (insn))
1277         return "tst%.w %1";
1278       if (count == 7
1279           && next_insn_tests_no_inequality (insn))
1280         return "tst%.b %1";
1281
1282       cc_status.flags = CC_NOT_NEGATIVE;
1283     }
1284   return "btst %0,%1";
1285 }
1286 \f
1287 /* Returns true if OP is either a symbol reference or a sum of a symbol
1288    reference and a constant.  */
1289
1290 int
1291 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1292 {
1293   switch (GET_CODE (op))
1294     {
1295     case SYMBOL_REF:
1296     case LABEL_REF:
1297       return true;
1298
1299     case CONST:
1300       op = XEXP (op, 0);
1301       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1302                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1303               && GET_CODE (XEXP (op, 1)) == CONST_INT);
1304
1305 #if 0 /* Deleted, with corresponding change in m68k.h,
1306          so as to fit the specs.  No CONST_DOUBLE is ever symbolic.  */
1307     case CONST_DOUBLE:
1308       return GET_MODE (op) == mode;
1309 #endif
1310
1311     default:
1312       return false;
1313     }
1314 }
1315 \f
1316 /* Check for sign_extend or zero_extend.  Used for bit-count operands.  */
1317
1318 int
1319 extend_operator(rtx x, enum machine_mode mode)
1320 {
1321     if (mode != VOIDmode && GET_MODE(x) != mode)
1322         return 0;
1323     switch (GET_CODE(x))
1324         {
1325         case SIGN_EXTEND :
1326         case ZERO_EXTEND :
1327             return 1;
1328         default :
1329             return 0;
1330         }
1331 }
1332
1333 \f
1334 /* Legitimize PIC addresses.  If the address is already
1335    position-independent, we return ORIG.  Newly generated
1336    position-independent addresses go to REG.  If we need more
1337    than one register, we lose.  
1338
1339    An address is legitimized by making an indirect reference
1340    through the Global Offset Table with the name of the symbol
1341    used as an offset.  
1342
1343    The assembler and linker are responsible for placing the 
1344    address of the symbol in the GOT.  The function prologue
1345    is responsible for initializing a5 to the starting address
1346    of the GOT.
1347
1348    The assembler is also responsible for translating a symbol name
1349    into a constant displacement from the start of the GOT.  
1350
1351    A quick example may make things a little clearer:
1352
1353    When not generating PIC code to store the value 12345 into _foo
1354    we would generate the following code:
1355
1356         movel #12345, _foo
1357
1358    When generating PIC two transformations are made.  First, the compiler
1359    loads the address of foo into a register.  So the first transformation makes:
1360
1361         lea     _foo, a0
1362         movel   #12345, a0@
1363
1364    The code in movsi will intercept the lea instruction and call this
1365    routine which will transform the instructions into:
1366
1367         movel   a5@(_foo:w), a0
1368         movel   #12345, a0@
1369    
1370
1371    That (in a nutshell) is how *all* symbol and label references are 
1372    handled.  */
1373
1374 rtx
1375 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
1376                         rtx reg)
1377 {
1378   rtx pic_ref = orig;
1379
1380   /* First handle a simple SYMBOL_REF or LABEL_REF */
1381   if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
1382     {
1383       if (reg == 0)
1384         abort ();
1385
1386       pic_ref = gen_rtx_MEM (Pmode,
1387                              gen_rtx_PLUS (Pmode,
1388                                            pic_offset_table_rtx, orig));
1389       current_function_uses_pic_offset_table = 1;
1390       MEM_READONLY_P (pic_ref) = 1;
1391       emit_move_insn (reg, pic_ref);
1392       return reg;
1393     }
1394   else if (GET_CODE (orig) == CONST)
1395     {
1396       rtx base;
1397
1398       /* Make sure this has not already been legitimized.  */
1399       if (GET_CODE (XEXP (orig, 0)) == PLUS
1400           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1401         return orig;
1402
1403       if (reg == 0)
1404         abort ();
1405
1406       /* legitimize both operands of the PLUS */
1407       if (GET_CODE (XEXP (orig, 0)) == PLUS)
1408         {
1409           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1410           orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1411                                          base == reg ? 0 : reg);
1412         }
1413       else abort ();
1414
1415       if (GET_CODE (orig) == CONST_INT)
1416         return plus_constant (base, INTVAL (orig));
1417       pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1418       /* Likewise, should we set special REG_NOTEs here?  */
1419     }
1420   return pic_ref;
1421 }
1422
1423 \f
1424 typedef enum { MOVL, SWAP, NEGW, NOTW, NOTB, MOVQ, MVS, MVZ } CONST_METHOD;
1425
1426 static CONST_METHOD const_method (rtx);
1427
1428 #define USE_MOVQ(i)     ((unsigned)((i) + 128) <= 255)
1429
1430 static CONST_METHOD
1431 const_method (rtx constant)
1432 {
1433   int i;
1434   unsigned u;
1435
1436   i = INTVAL (constant);
1437   if (USE_MOVQ (i))
1438     return MOVQ;
1439
1440   /* The ColdFire doesn't have byte or word operations.  */
1441   /* FIXME: This may not be useful for the m68060 either.  */
1442   if (!TARGET_COLDFIRE) 
1443     {
1444       /* if -256 < N < 256 but N is not in range for a moveq
1445          N^ff will be, so use moveq #N^ff, dreg; not.b dreg.  */
1446       if (USE_MOVQ (i ^ 0xff))
1447         return NOTB;
1448       /* Likewise, try with not.w */
1449       if (USE_MOVQ (i ^ 0xffff))
1450         return NOTW;
1451       /* This is the only value where neg.w is useful */
1452       if (i == -65408)
1453         return NEGW;
1454     }
1455
1456   /* Try also with swap.  */
1457   u = i;
1458   if (USE_MOVQ ((u >> 16) | (u << 16)))
1459     return SWAP;
1460
1461   if (TARGET_CFV4)
1462     {
1463       /* Try using MVZ/MVS with an immediate value to load constants.  */
1464       if (i >= 0 && i <= 65535)
1465         return MVZ;
1466       if (i >= -32768 && i <= 32767)
1467         return MVS;
1468     }
1469
1470   /* Otherwise, use move.l */
1471   return MOVL;
1472 }
1473
1474 static int
1475 const_int_cost (rtx constant)
1476 {
1477   switch (const_method (constant))
1478     {
1479       case MOVQ :
1480       /* Constants between -128 and 127 are cheap due to moveq */
1481         return 0;
1482       case MVZ:
1483       case MVS:
1484       case NOTB :
1485       case NOTW :
1486       case NEGW :
1487       case SWAP :
1488       /* Constants easily generated by moveq + not.b/not.w/neg.w/swap  */
1489         return 1;
1490       case MOVL :
1491         return 2;
1492       default :
1493         abort ();
1494     }
1495 }
1496
1497 static bool
1498 m68k_rtx_costs (rtx x, int code, int outer_code, int *total)
1499 {
1500   switch (code)
1501     {
1502     case CONST_INT:
1503       /* Constant zero is super cheap due to clr instruction.  */
1504       if (x == const0_rtx)
1505         *total = 0;
1506       else
1507         *total = const_int_cost (x);
1508       return true;
1509
1510     case CONST:
1511     case LABEL_REF:
1512     case SYMBOL_REF:
1513       *total = 3;
1514       return true;
1515
1516     case CONST_DOUBLE:
1517       /* Make 0.0 cheaper than other floating constants to
1518          encourage creating tstsf and tstdf insns.  */
1519       if (outer_code == COMPARE
1520           && (x == CONST0_RTX (SFmode) || x == CONST0_RTX (DFmode)))
1521         *total = 4;
1522       else
1523         *total = 5;
1524       return true;
1525
1526     /* These are vaguely right for a 68020.  */
1527     /* The costs for long multiply have been adjusted to work properly
1528        in synth_mult on the 68020, relative to an average of the time
1529        for add and the time for shift, taking away a little more because
1530        sometimes move insns are needed.  */
1531     /* div?.w is relatively cheaper on 68000 counted in COSTS_N_INSNS terms.  */
1532 #define MULL_COST (TARGET_68060 ? 2 : TARGET_68040 ? 5 : (TARGET_COLDFIRE && !TARGET_5200) ? 3 : TARGET_COLDFIRE ? 10 : 13)
1533 #define MULW_COST (TARGET_68060 ? 2 : TARGET_68040 ? 3 : TARGET_68020 ? 8 : \
1534                         (TARGET_COLDFIRE && !TARGET_5200) ? 2 : 5)
1535 #define DIVW_COST (TARGET_68020 ? 27 : TARGET_CF_HWDIV ? 11 : 12)
1536
1537     case PLUS:
1538       /* An lea costs about three times as much as a simple add.  */
1539       if (GET_MODE (x) == SImode
1540           && GET_CODE (XEXP (x, 1)) == REG
1541           && GET_CODE (XEXP (x, 0)) == MULT
1542           && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
1543           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1544           && (INTVAL (XEXP (XEXP (x, 0), 1)) == 2
1545               || INTVAL (XEXP (XEXP (x, 0), 1)) == 4
1546               || INTVAL (XEXP (XEXP (x, 0), 1)) == 8))
1547         {
1548             /* lea an@(dx:l:i),am */
1549             *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 2 : 3);
1550             return true;
1551         }
1552       return false;
1553
1554     case ASHIFT:
1555     case ASHIFTRT:
1556     case LSHIFTRT:
1557       if (TARGET_68060)
1558         {
1559           *total = COSTS_N_INSNS(1);
1560           return true;
1561         }
1562       if (! TARGET_68020 && ! TARGET_COLDFIRE)
1563         {
1564           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
1565             {
1566               if (INTVAL (XEXP (x, 1)) < 16)
1567                 *total = COSTS_N_INSNS (2) + INTVAL (XEXP (x, 1)) / 2;
1568               else
1569                 /* We're using clrw + swap for these cases.  */
1570                 *total = COSTS_N_INSNS (4) + (INTVAL (XEXP (x, 1)) - 16) / 2;
1571             }
1572           else
1573             *total = COSTS_N_INSNS (10); /* worst case */
1574           return true;
1575         }
1576       /* A shift by a big integer takes an extra instruction.  */
1577       if (GET_CODE (XEXP (x, 1)) == CONST_INT
1578           && (INTVAL (XEXP (x, 1)) == 16))
1579         {
1580           *total = COSTS_N_INSNS (2);    /* clrw;swap */
1581           return true;
1582         }
1583       if (GET_CODE (XEXP (x, 1)) == CONST_INT
1584           && !(INTVAL (XEXP (x, 1)) > 0
1585                && INTVAL (XEXP (x, 1)) <= 8))
1586         {
1587           *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 1 : 3);      /* lsr #i,dn */
1588           return true;
1589         }
1590       return false;
1591
1592     case MULT:
1593       if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
1594            || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
1595           && GET_MODE (x) == SImode)
1596         *total = COSTS_N_INSNS (MULW_COST);
1597       else if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
1598         *total = COSTS_N_INSNS (MULW_COST);
1599       else
1600         *total = COSTS_N_INSNS (MULL_COST);
1601       return true;
1602
1603     case DIV:
1604     case UDIV:
1605     case MOD:
1606     case UMOD:
1607       if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
1608         *total = COSTS_N_INSNS (DIVW_COST);     /* div.w */
1609       else if (TARGET_CF_HWDIV)
1610         *total = COSTS_N_INSNS (18);
1611       else
1612         *total = COSTS_N_INSNS (43);            /* div.l */
1613       return true;
1614
1615     default:
1616       return false;
1617     }
1618 }
1619
1620 const char *
1621 output_move_const_into_data_reg (rtx *operands)
1622 {
1623   int i;
1624
1625   i = INTVAL (operands[1]);
1626   switch (const_method (operands[1]))
1627     {
1628     case MVZ:
1629       return "mvsw %1,%0";
1630     case MVS:
1631       return "mvzw %1,%0";
1632     case MOVQ :
1633       return "moveq %1,%0";
1634     case NOTB :
1635       CC_STATUS_INIT;
1636       operands[1] = GEN_INT (i ^ 0xff);
1637       return "moveq %1,%0\n\tnot%.b %0";
1638     case NOTW :
1639       CC_STATUS_INIT;
1640       operands[1] = GEN_INT (i ^ 0xffff);
1641       return "moveq %1,%0\n\tnot%.w %0";
1642     case NEGW :
1643       CC_STATUS_INIT;
1644       return "moveq #-128,%0\n\tneg%.w %0";
1645     case SWAP :
1646       {
1647         unsigned u = i;
1648
1649         operands[1] = GEN_INT ((u << 16) | (u >> 16));
1650         return "moveq %1,%0\n\tswap %0";
1651       }
1652     case MOVL :
1653         return "move%.l %1,%0";
1654     default :
1655         abort ();
1656     }
1657 }
1658
1659 /* Return 1 if 'constant' can be represented by
1660    mov3q on a ColdFire V4 core.  */
1661 int
1662 valid_mov3q_const (rtx constant)
1663 {
1664   int i;
1665
1666   if (TARGET_CFV4 && GET_CODE (constant) == CONST_INT)
1667     {
1668       i = INTVAL (constant);
1669       if ((i == -1) || (i >= 1 && i <= 7))
1670         return 1;
1671     }
1672   return 0;
1673 }
1674
1675
1676 const char *
1677 output_move_simode_const (rtx *operands)
1678 {
1679   if (operands[1] == const0_rtx
1680       && (DATA_REG_P (operands[0])
1681           || GET_CODE (operands[0]) == MEM)
1682       /* clr insns on 68000 read before writing.
1683          This isn't so on the 68010, but we have no TARGET_68010.  */
1684       && ((TARGET_68020 || TARGET_COLDFIRE)
1685           || !(GET_CODE (operands[0]) == MEM
1686                && MEM_VOLATILE_P (operands[0]))))
1687     return "clr%.l %0";
1688   else if ((GET_MODE (operands[0]) == SImode)
1689            && valid_mov3q_const (operands[1]))
1690       return "mov3q%.l %1,%0";
1691   else if (operands[1] == const0_rtx
1692            && ADDRESS_REG_P (operands[0]))
1693     return "sub%.l %0,%0";
1694   else if (DATA_REG_P (operands[0]))
1695     return output_move_const_into_data_reg (operands);
1696   else if (ADDRESS_REG_P (operands[0])
1697            && INTVAL (operands[1]) < 0x8000
1698            && INTVAL (operands[1]) >= -0x8000)
1699     {
1700       if (valid_mov3q_const (operands[1]))
1701         return "mov3q%.l %1,%0";
1702       return "move%.w %1,%0";
1703     }
1704   else if (GET_CODE (operands[0]) == MEM
1705       && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
1706       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
1707            && INTVAL (operands[1]) < 0x8000
1708            && INTVAL (operands[1]) >= -0x8000)
1709     {
1710       if (valid_mov3q_const (operands[1]))
1711         return "mov3q%.l %1,%-";
1712       return "pea %a1";
1713     }
1714   return "move%.l %1,%0";
1715 }
1716
1717 const char *
1718 output_move_simode (rtx *operands)
1719 {
1720   if (GET_CODE (operands[1]) == CONST_INT)
1721     return output_move_simode_const (operands);
1722   else if ((GET_CODE (operands[1]) == SYMBOL_REF
1723             || GET_CODE (operands[1]) == CONST)
1724            && push_operand (operands[0], SImode))
1725     return "pea %a1";
1726   else if ((GET_CODE (operands[1]) == SYMBOL_REF
1727             || GET_CODE (operands[1]) == CONST)
1728            && ADDRESS_REG_P (operands[0]))
1729     return "lea %a1,%0";
1730   return "move%.l %1,%0";
1731 }
1732
1733 const char *
1734 output_move_himode (rtx *operands)
1735 {
1736  if (GET_CODE (operands[1]) == CONST_INT)
1737     {
1738       if (operands[1] == const0_rtx
1739           && (DATA_REG_P (operands[0])
1740               || GET_CODE (operands[0]) == MEM)
1741           /* clr insns on 68000 read before writing.
1742              This isn't so on the 68010, but we have no TARGET_68010.  */
1743           && ((TARGET_68020 || TARGET_COLDFIRE)
1744               || !(GET_CODE (operands[0]) == MEM
1745                    && MEM_VOLATILE_P (operands[0]))))
1746         return "clr%.w %0";
1747       else if (operands[1] == const0_rtx
1748                && ADDRESS_REG_P (operands[0]))
1749         return "sub%.l %0,%0";
1750       else if (DATA_REG_P (operands[0])
1751                && INTVAL (operands[1]) < 128
1752                && INTVAL (operands[1]) >= -128)
1753         {
1754           return "moveq %1,%0";
1755         }
1756       else if (INTVAL (operands[1]) < 0x8000
1757                && INTVAL (operands[1]) >= -0x8000)
1758         return "move%.w %1,%0";
1759     }
1760   else if (CONSTANT_P (operands[1]))
1761     return "move%.l %1,%0";
1762   /* Recognize the insn before a tablejump, one that refers
1763      to a table of offsets.  Such an insn will need to refer
1764      to a label on the insn.  So output one.  Use the label-number
1765      of the table of offsets to generate this label.  This code,
1766      and similar code below, assumes that there will be at most one
1767      reference to each table.  */
1768   if (GET_CODE (operands[1]) == MEM
1769       && GET_CODE (XEXP (operands[1], 0)) == PLUS
1770       && GET_CODE (XEXP (XEXP (operands[1], 0), 1)) == LABEL_REF
1771       && GET_CODE (XEXP (XEXP (operands[1], 0), 0)) != PLUS)
1772     {
1773       rtx labelref = XEXP (XEXP (operands[1], 0), 1);
1774       if (MOTOROLA)
1775         asm_fprintf (asm_out_file, "\t.set %LLI%d,.+2\n",
1776                      CODE_LABEL_NUMBER (XEXP (labelref, 0)));
1777       else
1778         (*targetm.asm_out.internal_label) (asm_out_file, "LI",
1779                      CODE_LABEL_NUMBER (XEXP (labelref, 0)));
1780     }
1781   return "move%.w %1,%0";
1782 }
1783
1784 const char *
1785 output_move_qimode (rtx *operands)
1786 {
1787   /* 68k family always modifies the stack pointer by at least 2, even for
1788      byte pushes.  The 5200 (ColdFire) does not do this.  */
1789   if (GET_CODE (operands[0]) == MEM
1790       && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
1791       && XEXP (XEXP (operands[0], 0), 0) == stack_pointer_rtx
1792       && ! ADDRESS_REG_P (operands[1])
1793       && ! TARGET_COLDFIRE)
1794     /* generated by pushqi1 pattern now */
1795     abort ();
1796
1797   /* clr and st insns on 68000 read before writing.
1798      This isn't so on the 68010, but we have no TARGET_68010.  */
1799   if (!ADDRESS_REG_P (operands[0])
1800       && ((TARGET_68020 || TARGET_COLDFIRE)
1801           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1802     {
1803       if (operands[1] == const0_rtx)
1804         return "clr%.b %0";
1805       if ((!TARGET_COLDFIRE || DATA_REG_P (operands[0]))
1806           && GET_CODE (operands[1]) == CONST_INT
1807           && (INTVAL (operands[1]) & 255) == 255)
1808         {
1809           CC_STATUS_INIT;
1810           return "st %0";
1811         }
1812     }
1813   if (GET_CODE (operands[1]) == CONST_INT
1814       && DATA_REG_P (operands[0])
1815       && INTVAL (operands[1]) < 128
1816       && INTVAL (operands[1]) >= -128)
1817     {
1818       return "moveq %1,%0";
1819     }
1820   if (operands[1] == const0_rtx && ADDRESS_REG_P (operands[0]))
1821     return "sub%.l %0,%0";
1822   if (GET_CODE (operands[1]) != CONST_INT && CONSTANT_P (operands[1]))
1823     return "move%.l %1,%0";
1824   /* 68k family (including the 5200 ColdFire) does not support byte moves to
1825      from address registers.  */
1826   if (ADDRESS_REG_P (operands[0]) || ADDRESS_REG_P (operands[1]))
1827     return "move%.w %1,%0";
1828   return "move%.b %1,%0";
1829 }
1830
1831 const char *
1832 output_move_stricthi (rtx *operands)
1833 {
1834   if (operands[1] == const0_rtx
1835       /* clr insns on 68000 read before writing.
1836          This isn't so on the 68010, but we have no TARGET_68010.  */
1837       && ((TARGET_68020 || TARGET_COLDFIRE)
1838           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1839     return "clr%.w %0";
1840   return "move%.w %1,%0";
1841 }
1842
1843 const char *
1844 output_move_strictqi (rtx *operands)
1845 {
1846   if (operands[1] == const0_rtx
1847       /* clr insns on 68000 read before writing.
1848          This isn't so on the 68010, but we have no TARGET_68010.  */
1849       && ((TARGET_68020 || TARGET_COLDFIRE)
1850           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1851     return "clr%.b %0";
1852   return "move%.b %1,%0";
1853 }
1854
1855 /* Return the best assembler insn template
1856    for moving operands[1] into operands[0] as a fullword.  */
1857
1858 static const char *
1859 singlemove_string (rtx *operands)
1860 {
1861   if (GET_CODE (operands[1]) == CONST_INT)
1862     return output_move_simode_const (operands);
1863   return "move%.l %1,%0";
1864 }
1865
1866
1867 /* Output assembler code to perform a doubleword move insn
1868    with operands OPERANDS.  */
1869
1870 const char *
1871 output_move_double (rtx *operands)
1872 {
1873   enum
1874     {
1875       REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP
1876     } optype0, optype1;
1877   rtx latehalf[2];
1878   rtx middlehalf[2];
1879   rtx xops[2];
1880   rtx addreg0 = 0, addreg1 = 0;
1881   int dest_overlapped_low = 0;
1882   int size = GET_MODE_SIZE (GET_MODE (operands[0]));
1883
1884   middlehalf[0] = 0;
1885   middlehalf[1] = 0;
1886
1887   /* First classify both operands.  */
1888
1889   if (REG_P (operands[0]))
1890     optype0 = REGOP;
1891   else if (offsettable_memref_p (operands[0]))
1892     optype0 = OFFSOP;
1893   else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
1894     optype0 = POPOP;
1895   else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
1896     optype0 = PUSHOP;
1897   else if (GET_CODE (operands[0]) == MEM)
1898     optype0 = MEMOP;
1899   else
1900     optype0 = RNDOP;
1901
1902   if (REG_P (operands[1]))
1903     optype1 = REGOP;
1904   else if (CONSTANT_P (operands[1]))
1905     optype1 = CNSTOP;
1906   else if (offsettable_memref_p (operands[1]))
1907     optype1 = OFFSOP;
1908   else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
1909     optype1 = POPOP;
1910   else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
1911     optype1 = PUSHOP;
1912   else if (GET_CODE (operands[1]) == MEM)
1913     optype1 = MEMOP;
1914   else
1915     optype1 = RNDOP;
1916
1917   /* Check for the cases that the operand constraints are not
1918      supposed to allow to happen.  Abort if we get one,
1919      because generating code for these cases is painful.  */
1920
1921   if (optype0 == RNDOP || optype1 == RNDOP)
1922     abort ();
1923
1924   /* If one operand is decrementing and one is incrementing
1925      decrement the former register explicitly
1926      and change that operand into ordinary indexing.  */
1927
1928   if (optype0 == PUSHOP && optype1 == POPOP)
1929     {
1930       operands[0] = XEXP (XEXP (operands[0], 0), 0);
1931       if (size == 12)
1932         output_asm_insn ("sub%.l #12,%0", operands);
1933       else
1934         output_asm_insn ("subq%.l #8,%0", operands);
1935       if (GET_MODE (operands[1]) == XFmode)
1936         operands[0] = gen_rtx_MEM (XFmode, operands[0]);
1937       else if (GET_MODE (operands[0]) == DFmode)
1938         operands[0] = gen_rtx_MEM (DFmode, operands[0]);
1939       else
1940         operands[0] = gen_rtx_MEM (DImode, operands[0]);
1941       optype0 = OFFSOP;
1942     }
1943   if (optype0 == POPOP && optype1 == PUSHOP)
1944     {
1945       operands[1] = XEXP (XEXP (operands[1], 0), 0);
1946       if (size == 12)
1947         output_asm_insn ("sub%.l #12,%1", operands);
1948       else
1949         output_asm_insn ("subq%.l #8,%1", operands);
1950       if (GET_MODE (operands[1]) == XFmode)
1951         operands[1] = gen_rtx_MEM (XFmode, operands[1]);
1952       else if (GET_MODE (operands[1]) == DFmode)
1953         operands[1] = gen_rtx_MEM (DFmode, operands[1]);
1954       else
1955         operands[1] = gen_rtx_MEM (DImode, operands[1]);
1956       optype1 = OFFSOP;
1957     }
1958
1959   /* If an operand is an unoffsettable memory ref, find a register
1960      we can increment temporarily to make it refer to the second word.  */
1961
1962   if (optype0 == MEMOP)
1963     addreg0 = find_addr_reg (XEXP (operands[0], 0));
1964
1965   if (optype1 == MEMOP)
1966     addreg1 = find_addr_reg (XEXP (operands[1], 0));
1967
1968   /* Ok, we can do one word at a time.
1969      Normally we do the low-numbered word first,
1970      but if either operand is autodecrementing then we
1971      do the high-numbered word first.
1972
1973      In either case, set up in LATEHALF the operands to use
1974      for the high-numbered word and in some cases alter the
1975      operands in OPERANDS to be suitable for the low-numbered word.  */
1976
1977   if (size == 12)
1978     {
1979       if (optype0 == REGOP)
1980         {
1981           latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 2);
1982           middlehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
1983         }
1984       else if (optype0 == OFFSOP)
1985         {
1986           middlehalf[0] = adjust_address (operands[0], SImode, 4);
1987           latehalf[0] = adjust_address (operands[0], SImode, size - 4);
1988         }
1989       else
1990         {
1991           middlehalf[0] = operands[0];
1992           latehalf[0] = operands[0];
1993         }
1994
1995       if (optype1 == REGOP)
1996         {
1997           latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 2);
1998           middlehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
1999         }
2000       else if (optype1 == OFFSOP)
2001         {
2002           middlehalf[1] = adjust_address (operands[1], SImode, 4);
2003           latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2004         }
2005       else if (optype1 == CNSTOP)
2006         {
2007           if (GET_CODE (operands[1]) == CONST_DOUBLE)
2008             {
2009               REAL_VALUE_TYPE r;
2010               long l[3];
2011
2012               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
2013               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
2014               operands[1] = GEN_INT (l[0]);
2015               middlehalf[1] = GEN_INT (l[1]);
2016               latehalf[1] = GEN_INT (l[2]);
2017             }
2018           else if (CONSTANT_P (operands[1]))
2019             {
2020               /* actually, no non-CONST_DOUBLE constant should ever
2021                  appear here.  */
2022               abort ();
2023               if (GET_CODE (operands[1]) == CONST_INT && INTVAL (operands[1]) < 0)
2024                 latehalf[1] = constm1_rtx;
2025               else
2026                 latehalf[1] = const0_rtx;
2027             }
2028         }
2029       else
2030         {
2031           middlehalf[1] = operands[1];
2032           latehalf[1] = operands[1];
2033         }
2034     }
2035   else
2036     /* size is not 12: */
2037     {
2038       if (optype0 == REGOP)
2039         latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2040       else if (optype0 == OFFSOP)
2041         latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2042       else
2043         latehalf[0] = operands[0];
2044
2045       if (optype1 == REGOP)
2046         latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2047       else if (optype1 == OFFSOP)
2048         latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2049       else if (optype1 == CNSTOP)
2050         split_double (operands[1], &operands[1], &latehalf[1]);
2051       else
2052         latehalf[1] = operands[1];
2053     }
2054
2055   /* If insn is effectively movd N(sp),-(sp) then we will do the
2056      high word first.  We should use the adjusted operand 1 (which is N+4(sp))
2057      for the low word as well, to compensate for the first decrement of sp.  */
2058   if (optype0 == PUSHOP
2059       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
2060       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
2061     operands[1] = middlehalf[1] = latehalf[1];
2062
2063   /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
2064      if the upper part of reg N does not appear in the MEM, arrange to
2065      emit the move late-half first.  Otherwise, compute the MEM address
2066      into the upper part of N and use that as a pointer to the memory
2067      operand.  */
2068   if (optype0 == REGOP
2069       && (optype1 == OFFSOP || optype1 == MEMOP))
2070     {
2071       rtx testlow = gen_rtx_REG (SImode, REGNO (operands[0]));
2072
2073       if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2074           && reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2075         {
2076           /* If both halves of dest are used in the src memory address,
2077              compute the address into latehalf of dest.
2078              Note that this can't happen if the dest is two data regs.  */
2079 compadr:
2080           xops[0] = latehalf[0];
2081           xops[1] = XEXP (operands[1], 0);
2082           output_asm_insn ("lea %a1,%0", xops);
2083           if (GET_MODE (operands[1]) == XFmode )
2084             {
2085               operands[1] = gen_rtx_MEM (XFmode, latehalf[0]);
2086               middlehalf[1] = adjust_address (operands[1], DImode, size - 8);
2087               latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2088             }
2089           else
2090             {
2091               operands[1] = gen_rtx_MEM (DImode, latehalf[0]);
2092               latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2093             }
2094         }
2095       else if (size == 12
2096                && reg_overlap_mentioned_p (middlehalf[0],
2097                                            XEXP (operands[1], 0)))
2098         {
2099           /* Check for two regs used by both source and dest.
2100              Note that this can't happen if the dest is all data regs.
2101              It can happen if the dest is d6, d7, a0.
2102              But in that case, latehalf is an addr reg, so
2103              the code at compadr does ok.  */
2104
2105           if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2106               || reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2107             goto compadr;
2108
2109           /* JRV says this can't happen: */
2110           if (addreg0 || addreg1)
2111             abort ();
2112
2113           /* Only the middle reg conflicts; simply put it last.  */
2114           output_asm_insn (singlemove_string (operands), operands);
2115           output_asm_insn (singlemove_string (latehalf), latehalf);
2116           output_asm_insn (singlemove_string (middlehalf), middlehalf);
2117           return "";
2118         }
2119       else if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0)))
2120         /* If the low half of dest is mentioned in the source memory
2121            address, the arrange to emit the move late half first.  */
2122         dest_overlapped_low = 1;
2123     }
2124
2125   /* If one or both operands autodecrementing,
2126      do the two words, high-numbered first.  */
2127
2128   /* Likewise,  the first move would clobber the source of the second one,
2129      do them in the other order.  This happens only for registers;
2130      such overlap can't happen in memory unless the user explicitly
2131      sets it up, and that is an undefined circumstance.  */
2132
2133   if (optype0 == PUSHOP || optype1 == PUSHOP
2134       || (optype0 == REGOP && optype1 == REGOP
2135           && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
2136               || REGNO (operands[0]) == REGNO (latehalf[1])))
2137       || dest_overlapped_low)
2138     {
2139       /* Make any unoffsettable addresses point at high-numbered word.  */
2140       if (addreg0)
2141         {
2142           if (size == 12)
2143             output_asm_insn ("addq%.l #8,%0", &addreg0);
2144           else
2145             output_asm_insn ("addq%.l #4,%0", &addreg0);
2146         }
2147       if (addreg1)
2148         {
2149           if (size == 12)
2150             output_asm_insn ("addq%.l #8,%0", &addreg1);
2151           else
2152             output_asm_insn ("addq%.l #4,%0", &addreg1);
2153         }
2154
2155       /* Do that word.  */
2156       output_asm_insn (singlemove_string (latehalf), latehalf);
2157
2158       /* Undo the adds we just did.  */
2159       if (addreg0)
2160         output_asm_insn ("subq%.l #4,%0", &addreg0);
2161       if (addreg1)
2162         output_asm_insn ("subq%.l #4,%0", &addreg1);
2163
2164       if (size == 12)
2165         {
2166           output_asm_insn (singlemove_string (middlehalf), middlehalf);
2167           if (addreg0)
2168             output_asm_insn ("subq%.l #4,%0", &addreg0);
2169           if (addreg1)
2170             output_asm_insn ("subq%.l #4,%0", &addreg1);
2171         }
2172
2173       /* Do low-numbered word.  */
2174       return singlemove_string (operands);
2175     }
2176
2177   /* Normal case: do the two words, low-numbered first.  */
2178
2179   output_asm_insn (singlemove_string (operands), operands);
2180
2181   /* Do the middle one of the three words for long double */
2182   if (size == 12)
2183     {
2184       if (addreg0)
2185         output_asm_insn ("addq%.l #4,%0", &addreg0);
2186       if (addreg1)
2187         output_asm_insn ("addq%.l #4,%0", &addreg1);
2188
2189       output_asm_insn (singlemove_string (middlehalf), middlehalf);
2190     }
2191
2192   /* Make any unoffsettable addresses point at high-numbered word.  */
2193   if (addreg0)
2194     output_asm_insn ("addq%.l #4,%0", &addreg0);
2195   if (addreg1)
2196     output_asm_insn ("addq%.l #4,%0", &addreg1);
2197
2198   /* Do that word.  */
2199   output_asm_insn (singlemove_string (latehalf), latehalf);
2200
2201   /* Undo the adds we just did.  */
2202   if (addreg0)
2203     {
2204       if (size == 12)
2205         output_asm_insn ("subq%.l #8,%0", &addreg0);
2206       else
2207         output_asm_insn ("subq%.l #4,%0", &addreg0);
2208     }
2209   if (addreg1)
2210     {
2211       if (size == 12)
2212         output_asm_insn ("subq%.l #8,%0", &addreg1);
2213       else
2214         output_asm_insn ("subq%.l #4,%0", &addreg1);
2215     }
2216
2217   return "";
2218 }
2219
2220 /* Return a REG that occurs in ADDR with coefficient 1.
2221    ADDR can be effectively incremented by incrementing REG.  */
2222
2223 static rtx
2224 find_addr_reg (rtx addr)
2225 {
2226   while (GET_CODE (addr) == PLUS)
2227     {
2228       if (GET_CODE (XEXP (addr, 0)) == REG)
2229         addr = XEXP (addr, 0);
2230       else if (GET_CODE (XEXP (addr, 1)) == REG)
2231         addr = XEXP (addr, 1);
2232       else if (CONSTANT_P (XEXP (addr, 0)))
2233         addr = XEXP (addr, 1);
2234       else if (CONSTANT_P (XEXP (addr, 1)))
2235         addr = XEXP (addr, 0);
2236       else
2237         abort ();
2238     }
2239   if (GET_CODE (addr) == REG)
2240     return addr;
2241   abort ();
2242 }
2243
2244 /* Output assembler code to perform a 32-bit 3-operand add.  */
2245
2246 const char *
2247 output_addsi3 (rtx *operands)
2248 {
2249   if (! operands_match_p (operands[0], operands[1]))
2250     {
2251       if (!ADDRESS_REG_P (operands[1]))
2252         {
2253           rtx tmp = operands[1];
2254
2255           operands[1] = operands[2];
2256           operands[2] = tmp;
2257         }
2258
2259       /* These insns can result from reloads to access
2260          stack slots over 64k from the frame pointer.  */
2261       if (GET_CODE (operands[2]) == CONST_INT
2262           && (INTVAL (operands[2]) < -32768 || INTVAL (operands[2]) > 32767))
2263         return "move%.l %2,%0\n\tadd%.l %1,%0";
2264       if (GET_CODE (operands[2]) == REG)
2265         return MOTOROLA ? "lea (%1,%2.l),%0" : "lea %1@(0,%2:l),%0";
2266       return MOTOROLA ? "lea (%c2,%1),%0" : "lea %1@(%c2),%0";
2267     }
2268   if (GET_CODE (operands[2]) == CONST_INT)
2269     {
2270       if (INTVAL (operands[2]) > 0
2271           && INTVAL (operands[2]) <= 8)
2272         return "addq%.l %2,%0";
2273       if (INTVAL (operands[2]) < 0
2274           && INTVAL (operands[2]) >= -8)
2275         {
2276           operands[2] = GEN_INT (- INTVAL (operands[2]));
2277           return "subq%.l %2,%0";
2278         }
2279       /* On the CPU32 it is faster to use two addql instructions to
2280          add a small integer (8 < N <= 16) to a register.
2281          Likewise for subql.  */
2282       if (TARGET_CPU32 && REG_P (operands[0]))
2283         {
2284           if (INTVAL (operands[2]) > 8
2285               && INTVAL (operands[2]) <= 16)
2286             {
2287               operands[2] = GEN_INT (INTVAL (operands[2]) - 8);
2288               return "addq%.l #8,%0\n\taddq%.l %2,%0";
2289             }
2290           if (INTVAL (operands[2]) < -8
2291               && INTVAL (operands[2]) >= -16)
2292             {
2293               operands[2] = GEN_INT (- INTVAL (operands[2]) - 8);
2294               return "subq%.l #8,%0\n\tsubq%.l %2,%0";
2295             }
2296         }
2297       if (ADDRESS_REG_P (operands[0])
2298           && INTVAL (operands[2]) >= -0x8000
2299           && INTVAL (operands[2]) < 0x8000)
2300         {
2301           if (TARGET_68040)
2302             return "add%.w %2,%0";
2303           else
2304             return MOTOROLA ? "lea (%c2,%0),%0" : "lea %0@(%c2),%0";
2305         }
2306     }
2307   return "add%.l %2,%0";
2308 }
2309 \f
2310 /* Store in cc_status the expressions that the condition codes will
2311    describe after execution of an instruction whose pattern is EXP.
2312    Do not alter them if the instruction would not alter the cc's.  */
2313
2314 /* On the 68000, all the insns to store in an address register fail to
2315    set the cc's.  However, in some cases these instructions can make it
2316    possibly invalid to use the saved cc's.  In those cases we clear out
2317    some or all of the saved cc's so they won't be used.  */
2318
2319 void
2320 notice_update_cc (rtx exp, rtx insn)
2321 {
2322   if (GET_CODE (exp) == SET)
2323     {
2324       if (GET_CODE (SET_SRC (exp)) == CALL)
2325         {
2326           CC_STATUS_INIT; 
2327         }
2328       else if (ADDRESS_REG_P (SET_DEST (exp)))
2329         {
2330           if (cc_status.value1 && modified_in_p (cc_status.value1, insn))
2331             cc_status.value1 = 0;
2332           if (cc_status.value2 && modified_in_p (cc_status.value2, insn))
2333             cc_status.value2 = 0; 
2334         }
2335       else if (!FP_REG_P (SET_DEST (exp))
2336                && SET_DEST (exp) != cc0_rtx
2337                && (FP_REG_P (SET_SRC (exp))
2338                    || GET_CODE (SET_SRC (exp)) == FIX
2339                    || GET_CODE (SET_SRC (exp)) == FLOAT_TRUNCATE
2340                    || GET_CODE (SET_SRC (exp)) == FLOAT_EXTEND))
2341         {
2342           CC_STATUS_INIT; 
2343         }
2344       /* A pair of move insns doesn't produce a useful overall cc.  */
2345       else if (!FP_REG_P (SET_DEST (exp))
2346                && !FP_REG_P (SET_SRC (exp))
2347                && GET_MODE_SIZE (GET_MODE (SET_SRC (exp))) > 4
2348                && (GET_CODE (SET_SRC (exp)) == REG
2349                    || GET_CODE (SET_SRC (exp)) == MEM
2350                    || GET_CODE (SET_SRC (exp)) == CONST_DOUBLE))
2351         {
2352           CC_STATUS_INIT; 
2353         }
2354       else if (GET_CODE (SET_SRC (exp)) == CALL)
2355         {
2356           CC_STATUS_INIT; 
2357         }
2358       else if (XEXP (exp, 0) != pc_rtx)
2359         {
2360           cc_status.flags = 0;
2361           cc_status.value1 = XEXP (exp, 0);
2362           cc_status.value2 = XEXP (exp, 1);
2363         }
2364     }
2365   else if (GET_CODE (exp) == PARALLEL
2366            && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
2367     {
2368       if (ADDRESS_REG_P (XEXP (XVECEXP (exp, 0, 0), 0)))
2369         CC_STATUS_INIT;
2370       else if (XEXP (XVECEXP (exp, 0, 0), 0) != pc_rtx)
2371         {
2372           cc_status.flags = 0;
2373           cc_status.value1 = XEXP (XVECEXP (exp, 0, 0), 0);
2374           cc_status.value2 = XEXP (XVECEXP (exp, 0, 0), 1);
2375         }
2376     }
2377   else
2378     CC_STATUS_INIT;
2379   if (cc_status.value2 != 0
2380       && ADDRESS_REG_P (cc_status.value2)
2381       && GET_MODE (cc_status.value2) == QImode)
2382     CC_STATUS_INIT;
2383   if (cc_status.value2 != 0)
2384     switch (GET_CODE (cc_status.value2))
2385       {
2386       case ASHIFT: case ASHIFTRT: case LSHIFTRT:
2387       case ROTATE: case ROTATERT:
2388         /* These instructions always clear the overflow bit, and set
2389            the carry to the bit shifted out.  */
2390         /* ??? We don't currently have a way to signal carry not valid,
2391            nor do we check for it in the branch insns.  */
2392         CC_STATUS_INIT;
2393         break;
2394
2395       case PLUS: case MINUS: case MULT:
2396       case DIV: case UDIV: case MOD: case UMOD: case NEG:
2397         if (GET_MODE (cc_status.value2) != VOIDmode)
2398           cc_status.flags |= CC_NO_OVERFLOW;
2399         break;
2400       case ZERO_EXTEND:
2401         /* (SET r1 (ZERO_EXTEND r2)) on this machine
2402            ends with a move insn moving r2 in r2's mode.
2403            Thus, the cc's are set for r2.
2404            This can set N bit spuriously.  */
2405         cc_status.flags |= CC_NOT_NEGATIVE; 
2406
2407       default:
2408         break;
2409       }
2410   if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
2411       && cc_status.value2
2412       && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
2413     cc_status.value2 = 0;
2414   if (((cc_status.value1 && FP_REG_P (cc_status.value1))
2415        || (cc_status.value2 && FP_REG_P (cc_status.value2))))
2416     cc_status.flags = CC_IN_68881;
2417 }
2418 \f
2419 const char *
2420 output_move_const_double (rtx *operands)
2421 {
2422   int code = standard_68881_constant_p (operands[1]);
2423
2424   if (code != 0)
2425     {
2426       static char buf[40];
2427
2428       sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
2429       return buf;
2430     }
2431   return "fmove%.d %1,%0";
2432 }
2433
2434 const char *
2435 output_move_const_single (rtx *operands)
2436 {
2437   int code = standard_68881_constant_p (operands[1]);
2438
2439   if (code != 0)
2440     {
2441       static char buf[40];
2442
2443       sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
2444       return buf;
2445     }
2446   return "fmove%.s %f1,%0";
2447 }
2448
2449 /* Return nonzero if X, a CONST_DOUBLE, has a value that we can get
2450    from the "fmovecr" instruction.
2451    The value, anded with 0xff, gives the code to use in fmovecr
2452    to get the desired constant.  */
2453
2454 /* This code has been fixed for cross-compilation.  */
2455   
2456 static int inited_68881_table = 0;
2457
2458 static const char *const strings_68881[7] = {
2459   "0.0",
2460   "1.0",
2461   "10.0",
2462   "100.0",
2463   "10000.0",
2464   "1e8",
2465   "1e16"
2466   };
2467
2468 static const int codes_68881[7] = {
2469   0x0f,
2470   0x32,
2471   0x33,
2472   0x34,
2473   0x35,
2474   0x36,
2475   0x37
2476   };
2477
2478 REAL_VALUE_TYPE values_68881[7];
2479
2480 /* Set up values_68881 array by converting the decimal values
2481    strings_68881 to binary.  */
2482
2483 void
2484 init_68881_table (void)
2485 {
2486   int i;
2487   REAL_VALUE_TYPE r;
2488   enum machine_mode mode;
2489
2490   mode = SFmode;
2491   for (i = 0; i < 7; i++)
2492     {
2493       if (i == 6)
2494         mode = DFmode;
2495       r = REAL_VALUE_ATOF (strings_68881[i], mode);
2496       values_68881[i] = r;
2497     }
2498   inited_68881_table = 1;
2499 }
2500
2501 int
2502 standard_68881_constant_p (rtx x)
2503 {
2504   REAL_VALUE_TYPE r;
2505   int i;
2506
2507   /* fmovecr must be emulated on the 68040 and 68060, so it shouldn't be
2508      used at all on those chips.  */
2509   if (TARGET_68040 || TARGET_68060)
2510     return 0;
2511
2512   if (! inited_68881_table)
2513     init_68881_table ();
2514
2515   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2516
2517   /* Use REAL_VALUES_IDENTICAL instead of REAL_VALUES_EQUAL so that -0.0
2518      is rejected.  */
2519   for (i = 0; i < 6; i++)
2520     {
2521       if (REAL_VALUES_IDENTICAL (r, values_68881[i]))
2522         return (codes_68881[i]);
2523     }
2524   
2525   if (GET_MODE (x) == SFmode)
2526     return 0;
2527
2528   if (REAL_VALUES_EQUAL (r, values_68881[6]))
2529     return (codes_68881[6]);
2530
2531   /* larger powers of ten in the constants ram are not used
2532      because they are not equal to a `double' C constant.  */
2533   return 0;
2534 }
2535
2536 /* If X is a floating-point constant, return the logarithm of X base 2,
2537    or 0 if X is not a power of 2.  */
2538
2539 int
2540 floating_exact_log2 (rtx x)
2541 {
2542   REAL_VALUE_TYPE r, r1;
2543   int exp;
2544
2545   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2546
2547   if (REAL_VALUES_LESS (r, dconst1))
2548     return 0;
2549
2550   exp = real_exponent (&r);
2551   real_2expN (&r1, exp);
2552   if (REAL_VALUES_EQUAL (r1, r))
2553     return exp;
2554
2555   return 0;
2556 }
2557 \f
2558 /* A C compound statement to output to stdio stream STREAM the
2559    assembler syntax for an instruction operand X.  X is an RTL
2560    expression.
2561
2562    CODE is a value that can be used to specify one of several ways
2563    of printing the operand.  It is used when identical operands
2564    must be printed differently depending on the context.  CODE
2565    comes from the `%' specification that was used to request
2566    printing of the operand.  If the specification was just `%DIGIT'
2567    then CODE is 0; if the specification was `%LTR DIGIT' then CODE
2568    is the ASCII code for LTR.
2569
2570    If X is a register, this macro should print the register's name.
2571    The names can be found in an array `reg_names' whose type is
2572    `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.
2573
2574    When the machine description has a specification `%PUNCT' (a `%'
2575    followed by a punctuation character), this macro is called with
2576    a null pointer for X and the punctuation character for CODE.
2577
2578    The m68k specific codes are:
2579
2580    '.' for dot needed in Motorola-style opcode names.
2581    '-' for an operand pushing on the stack:
2582        sp@-, -(sp) or -(%sp) depending on the style of syntax.
2583    '+' for an operand pushing on the stack:
2584        sp@+, (sp)+ or (%sp)+ depending on the style of syntax.
2585    '@' for a reference to the top word on the stack:
2586        sp@, (sp) or (%sp) depending on the style of syntax.
2587    '#' for an immediate operand prefix (# in MIT and Motorola syntax
2588        but & in SGS syntax).
2589    '!' for the cc register (used in an `and to cc' insn).
2590    '$' for the letter `s' in an op code, but only on the 68040.
2591    '&' for the letter `d' in an op code, but only on the 68040.
2592    '/' for register prefix needed by longlong.h.
2593
2594    'b' for byte insn (no effect, on the Sun; this is for the ISI).
2595    'd' to force memory addressing to be absolute, not relative.
2596    'f' for float insn (print a CONST_DOUBLE as a float rather than in hex)
2597    'o' for operands to go directly to output_operand_address (bypassing
2598        print_operand_address--used only for SYMBOL_REFs under TARGET_PCREL)
2599    'x' for float insn (print a CONST_DOUBLE as a float rather than in hex),
2600        or print pair of registers as rx:ry.
2601
2602    */
2603
2604 void
2605 print_operand (FILE *file, rtx op, int letter)
2606 {
2607   if (letter == '.')
2608     {
2609       if (MOTOROLA)
2610         fprintf (file, ".");
2611     }
2612   else if (letter == '#')
2613     asm_fprintf (file, "%I");
2614   else if (letter == '-')
2615     asm_fprintf (file, MOTOROLA ? "-(%Rsp)" : "%Rsp@-");
2616   else if (letter == '+')
2617     asm_fprintf (file, MOTOROLA ? "(%Rsp)+" : "%Rsp@+");
2618   else if (letter == '@')
2619     asm_fprintf (file, MOTOROLA ? "(%Rsp)" : "%Rsp@");
2620   else if (letter == '!')
2621     asm_fprintf (file, "%Rfpcr");
2622   else if (letter == '$')
2623     {
2624       if (TARGET_68040_ONLY)
2625         fprintf (file, "s");
2626     }
2627   else if (letter == '&')
2628     {
2629       if (TARGET_68040_ONLY)
2630         fprintf (file, "d");
2631     }
2632   else if (letter == '/')
2633     asm_fprintf (file, "%R");
2634   else if (letter == 'o')
2635     {
2636       /* This is only for direct addresses with TARGET_PCREL */
2637       if (GET_CODE (op) != MEM || GET_CODE (XEXP (op, 0)) != SYMBOL_REF
2638           || !TARGET_PCREL)
2639         abort ();
2640       output_addr_const (file, XEXP (op, 0));
2641     }
2642   else if (GET_CODE (op) == REG)
2643     {
2644       if (letter == 'R')
2645         /* Print out the second register name of a register pair.
2646            I.e., R (6) => 7.  */
2647         fputs (M68K_REGNAME(REGNO (op) + 1), file);
2648       else
2649         fputs (M68K_REGNAME(REGNO (op)), file);
2650     }
2651   else if (GET_CODE (op) == MEM)
2652     {
2653       output_address (XEXP (op, 0));
2654       if (letter == 'd' && ! TARGET_68020
2655           && CONSTANT_ADDRESS_P (XEXP (op, 0))
2656           && !(GET_CODE (XEXP (op, 0)) == CONST_INT
2657                && INTVAL (XEXP (op, 0)) < 0x8000
2658                && INTVAL (XEXP (op, 0)) >= -0x8000))
2659         fprintf (file, MOTOROLA ? ".l" : ":l");
2660     }
2661   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
2662     {
2663       REAL_VALUE_TYPE r;
2664       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2665       ASM_OUTPUT_FLOAT_OPERAND (letter, file, r);
2666     }
2667   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == XFmode)
2668     {
2669       REAL_VALUE_TYPE r;
2670       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2671       ASM_OUTPUT_LONG_DOUBLE_OPERAND (file, r);
2672     }
2673   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
2674     {
2675       REAL_VALUE_TYPE r;
2676       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2677       ASM_OUTPUT_DOUBLE_OPERAND (file, r);
2678     }
2679   else
2680     {
2681       /* Use `print_operand_address' instead of `output_addr_const'
2682          to ensure that we print relevant PIC stuff.  */
2683       asm_fprintf (file, "%I");
2684       if (TARGET_PCREL
2685           && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST))
2686         print_operand_address (file, op);
2687       else
2688         output_addr_const (file, op);
2689     }
2690 }
2691
2692 \f
2693 /* A C compound statement to output to stdio stream STREAM the
2694    assembler syntax for an instruction operand that is a memory
2695    reference whose address is ADDR.  ADDR is an RTL expression.
2696
2697    Note that this contains a kludge that knows that the only reason
2698    we have an address (plus (label_ref...) (reg...)) when not generating
2699    PIC code is in the insn before a tablejump, and we know that m68k.md
2700    generates a label LInnn: on such an insn.
2701
2702    It is possible for PIC to generate a (plus (label_ref...) (reg...))
2703    and we handle that just like we would a (plus (symbol_ref...) (reg...)).
2704
2705    Some SGS assemblers have a bug such that "Lnnn-LInnn-2.b(pc,d0.l*2)"
2706    fails to assemble.  Luckily "Lnnn(pc,d0.l*2)" produces the results
2707    we want.  This difference can be accommodated by using an assembler
2708    define such "LDnnn" to be either "Lnnn-LInnn-2.b", "Lnnn", or any other
2709    string, as necessary.  This is accomplished via the ASM_OUTPUT_CASE_END
2710    macro.  See m68k/sgs.h for an example; for versions without the bug.
2711    Some assemblers refuse all the above solutions.  The workaround is to
2712    emit "K(pc,d0.l*2)" with K being a small constant known to give the
2713    right behavior.
2714
2715    They also do not like things like "pea 1.w", so we simple leave off
2716    the .w on small constants. 
2717
2718    This routine is responsible for distinguishing between -fpic and -fPIC 
2719    style relocations in an address.  When generating -fpic code the
2720    offset is output in word mode (e.g. movel a5@(_foo:w), a0).  When generating
2721    -fPIC code the offset is output in long mode (e.g. movel a5@(_foo:l), a0) */
2722
2723 #if MOTOROLA
2724 #  define ASM_OUTPUT_CASE_FETCH(file, labelno, regname)\
2725         asm_fprintf (file, "%LL%d-%LLI%d.b(%Rpc,%s.", labelno, labelno, regname)
2726 #else /* !MOTOROLA */
2727 # define ASM_OUTPUT_CASE_FETCH(file, labelno, regname)\
2728         asm_fprintf (file, "%Rpc@(%LL%d-%LLI%d-2:b,%s:", labelno, labelno, regname)
2729 #endif /* !MOTOROLA */
2730
2731 void
2732 print_operand_address (FILE *file, rtx addr)
2733 {
2734   register rtx reg1, reg2, breg, ireg;
2735   rtx offset;
2736
2737   switch (GET_CODE (addr))
2738     {
2739       case REG:
2740         fprintf (file, MOTOROLA ? "(%s)" : "%s@", M68K_REGNAME(REGNO (addr)));
2741         break;
2742       case PRE_DEC:
2743         fprintf (file, MOTOROLA ? "-(%s)" : "%s@-",
2744                  M68K_REGNAME(REGNO (XEXP (addr, 0))));
2745         break;
2746       case POST_INC:
2747         fprintf (file, MOTOROLA ? "(%s)+" : "%s@+",
2748                  M68K_REGNAME(REGNO (XEXP (addr, 0))));
2749         break;
2750       case PLUS:
2751         reg1 = reg2 = ireg = breg = offset = 0;
2752         if (CONSTANT_ADDRESS_P (XEXP (addr, 0)))
2753           {
2754             offset = XEXP (addr, 0);
2755             addr = XEXP (addr, 1);
2756           }
2757         else if (CONSTANT_ADDRESS_P (XEXP (addr, 1)))
2758           {
2759             offset = XEXP (addr, 1);
2760             addr = XEXP (addr, 0);
2761           }
2762         if (GET_CODE (addr) != PLUS)
2763           {
2764             ;
2765           }
2766         else if (GET_CODE (XEXP (addr, 0)) == SIGN_EXTEND)
2767           {
2768             reg1 = XEXP (addr, 0);
2769             addr = XEXP (addr, 1);
2770           }
2771         else if (GET_CODE (XEXP (addr, 1)) == SIGN_EXTEND)
2772           {
2773             reg1 = XEXP (addr, 1);
2774             addr = XEXP (addr, 0);
2775           }
2776         else if (GET_CODE (XEXP (addr, 0)) == MULT)
2777           {
2778             reg1 = XEXP (addr, 0);
2779             addr = XEXP (addr, 1);
2780           }
2781         else if (GET_CODE (XEXP (addr, 1)) == MULT)
2782           {
2783             reg1 = XEXP (addr, 1);
2784             addr = XEXP (addr, 0);
2785           }
2786         else if (GET_CODE (XEXP (addr, 0)) == REG)
2787           {
2788             reg1 = XEXP (addr, 0);
2789             addr = XEXP (addr, 1);
2790           }
2791         else if (GET_CODE (XEXP (addr, 1)) == REG)
2792           {
2793             reg1 = XEXP (addr, 1);
2794             addr = XEXP (addr, 0);
2795           }
2796         if (GET_CODE (addr) == REG || GET_CODE (addr) == MULT
2797             || GET_CODE (addr) == SIGN_EXTEND)
2798           {
2799             if (reg1 == 0)
2800               {
2801                 reg1 = addr;
2802               }
2803             else
2804               {
2805                 reg2 = addr;
2806               }
2807             addr = 0;
2808           }
2809 #if 0   /* for OLD_INDEXING */
2810         else if (GET_CODE (addr) == PLUS)
2811           {
2812             if (GET_CODE (XEXP (addr, 0)) == REG)
2813               {
2814                 reg2 = XEXP (addr, 0);
2815                 addr = XEXP (addr, 1);
2816               }
2817             else if (GET_CODE (XEXP (addr, 1)) == REG)
2818               {
2819                 reg2 = XEXP (addr, 1);
2820                 addr = XEXP (addr, 0);
2821               }
2822           }
2823 #endif
2824         if (offset != 0)
2825           {
2826             if (addr != 0)
2827               {
2828                 abort ();
2829               }
2830             addr = offset;
2831           }
2832         if ((reg1 && (GET_CODE (reg1) == SIGN_EXTEND
2833                       || GET_CODE (reg1) == MULT))
2834             || (reg2 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg2))))
2835           {
2836             breg = reg2;
2837             ireg = reg1;
2838           }
2839         else if (reg1 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg1)))
2840           {
2841             breg = reg1;
2842             ireg = reg2;
2843           }
2844         if (ireg != 0 && breg == 0 && GET_CODE (addr) == LABEL_REF
2845             && ! (flag_pic && ireg == pic_offset_table_rtx))
2846           {
2847             int scale = 1;
2848             if (GET_CODE (ireg) == MULT)
2849               {
2850                 scale = INTVAL (XEXP (ireg, 1));
2851                 ireg = XEXP (ireg, 0);
2852               }
2853             if (GET_CODE (ireg) == SIGN_EXTEND)
2854               {
2855                 ASM_OUTPUT_CASE_FETCH (file,
2856                              CODE_LABEL_NUMBER (XEXP (addr, 0)),
2857                              M68K_REGNAME(REGNO (XEXP (ireg, 0))));
2858                 fprintf (file, "w");
2859               }
2860             else
2861               {
2862                 ASM_OUTPUT_CASE_FETCH (file,
2863                              CODE_LABEL_NUMBER (XEXP (addr, 0)),
2864                              M68K_REGNAME(REGNO (ireg)));
2865                 fprintf (file, "l");
2866               }
2867             if (scale != 1)
2868               fprintf (file, MOTOROLA ? "*%d" : ":%d", scale);
2869             putc (')', file);
2870             break;
2871           }
2872         if (breg != 0 && ireg == 0 && GET_CODE (addr) == LABEL_REF
2873             && ! (flag_pic && breg == pic_offset_table_rtx))
2874           {
2875             ASM_OUTPUT_CASE_FETCH (file,
2876                          CODE_LABEL_NUMBER (XEXP (addr, 0)),
2877                          M68K_REGNAME(REGNO (breg)));
2878             fprintf (file, "l)");
2879             break;
2880           }
2881         if (ireg != 0 || breg != 0)
2882           {
2883             int scale = 1;
2884             if (breg == 0)
2885               {
2886                 abort ();
2887               }
2888             if (! flag_pic && addr && GET_CODE (addr) == LABEL_REF)
2889               {
2890                 abort ();
2891               }
2892             if (MOTOROLA)
2893               {
2894                 if (addr != 0)
2895                   {
2896                     output_addr_const (file, addr);
2897                     if (flag_pic && (breg == pic_offset_table_rtx))
2898                       {
2899                         fprintf (file, "@GOT");
2900                         if (flag_pic == 1)
2901                           fprintf (file, ".w");
2902                       }
2903                   }
2904                 fprintf (file, "(%s", M68K_REGNAME(REGNO (breg)));
2905                 if (ireg != 0)
2906                   putc (',', file);
2907               }
2908             else /* !MOTOROLA */
2909               {
2910                 fprintf (file, "%s@(", M68K_REGNAME(REGNO (breg)));
2911                 if (addr != 0)
2912                   {
2913                     output_addr_const (file, addr);
2914                     if (breg == pic_offset_table_rtx)
2915                       switch (flag_pic)
2916                         {
2917                         case 1:
2918                           fprintf (file, ":w"); break;
2919                         case 2:
2920                           fprintf (file, ":l"); break;
2921                         default:
2922                           break;
2923                         }
2924                     if (ireg != 0)
2925                       putc (',', file);
2926                   }
2927               } /* !MOTOROLA */
2928             if (ireg != 0 && GET_CODE (ireg) == MULT)
2929               {
2930                 scale = INTVAL (XEXP (ireg, 1));
2931                 ireg = XEXP (ireg, 0);
2932               }
2933             if (ireg != 0 && GET_CODE (ireg) == SIGN_EXTEND)
2934               fprintf (file, MOTOROLA ? "%s.w" : "%s:w",
2935                        M68K_REGNAME(REGNO (XEXP (ireg, 0))));
2936             else if (ireg != 0)
2937               fprintf (file, MOTOROLA ? "%s.l" : "%s:l",
2938                        M68K_REGNAME(REGNO (ireg)));
2939             if (scale != 1)
2940               fprintf (file, MOTOROLA ? "*%d" : ":%d", scale);
2941             putc (')', file);
2942             break;
2943           }
2944         else if (reg1 != 0 && GET_CODE (addr) == LABEL_REF
2945                  && ! (flag_pic && reg1 == pic_offset_table_rtx))
2946           {
2947             ASM_OUTPUT_CASE_FETCH (file,
2948                          CODE_LABEL_NUMBER (XEXP (addr, 0)),
2949                          M68K_REGNAME(REGNO (reg1)));
2950             fprintf (file, "l)");
2951             break;
2952           }
2953         /* FALL-THROUGH (is this really what we want?)  */
2954       default:
2955         if (GET_CODE (addr) == CONST_INT
2956             && INTVAL (addr) < 0x8000
2957             && INTVAL (addr) >= -0x8000)
2958           {
2959             fprintf (file, MOTOROLA ? "%d.w" : "%d:w", (int) INTVAL (addr));
2960           }
2961         else if (GET_CODE (addr) == CONST_INT)
2962           {
2963             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
2964           }
2965         else if (TARGET_PCREL)
2966           {
2967             fputc ('(', file);
2968             output_addr_const (file, addr);
2969             if (flag_pic == 1)
2970               asm_fprintf (file, ":w,%Rpc)");
2971             else
2972               asm_fprintf (file, ":l,%Rpc)");
2973           }
2974         else
2975           {
2976             /* Special case for SYMBOL_REF if the symbol name ends in
2977                `.<letter>', this can be mistaken as a size suffix.  Put
2978                the name in parentheses.  */
2979             if (GET_CODE (addr) == SYMBOL_REF
2980                 && strlen (XSTR (addr, 0)) > 2
2981                 && XSTR (addr, 0)[strlen (XSTR (addr, 0)) - 2] == '.')
2982               {
2983                 putc ('(', file);
2984                 output_addr_const (file, addr);
2985                 putc (')', file);
2986               }
2987             else
2988               output_addr_const (file, addr);
2989           }
2990         break;
2991     }
2992 }
2993 \f
2994 /* Check for cases where a clr insns can be omitted from code using
2995    strict_low_part sets.  For example, the second clrl here is not needed:
2996    clrl d0; movw a0@+,d0; use d0; clrl d0; movw a0@+; use d0; ...
2997
2998    MODE is the mode of this STRICT_LOW_PART set.  FIRST_INSN is the clear
2999    insn we are checking for redundancy.  TARGET is the register set by the
3000    clear insn.  */
3001
3002 bool
3003 strict_low_part_peephole_ok (enum machine_mode mode, rtx first_insn,
3004                              rtx target)
3005 {
3006   rtx p;
3007
3008   p = prev_nonnote_insn (first_insn);
3009
3010   while (p)
3011     {
3012       /* If it isn't an insn, then give up.  */
3013       if (GET_CODE (p) != INSN)
3014         return false;
3015
3016       if (reg_set_p (target, p))
3017         {
3018           rtx set = single_set (p);
3019           rtx dest;
3020
3021           /* If it isn't an easy to recognize insn, then give up.  */
3022           if (! set)
3023             return false;
3024
3025           dest = SET_DEST (set);
3026
3027           /* If this sets the entire target register to zero, then our
3028              first_insn is redundant.  */
3029           if (rtx_equal_p (dest, target)
3030               && SET_SRC (set) == const0_rtx)
3031             return true;
3032           else if (GET_CODE (dest) == STRICT_LOW_PART
3033                    && GET_CODE (XEXP (dest, 0)) == REG
3034                    && REGNO (XEXP (dest, 0)) == REGNO (target)
3035                    && (GET_MODE_SIZE (GET_MODE (XEXP (dest, 0)))
3036                        <= GET_MODE_SIZE (mode)))
3037             /* This is a strict low part set which modifies less than
3038                we are using, so it is safe.  */
3039             ;
3040           else
3041             return false;
3042         }
3043
3044       p = prev_nonnote_insn (p);
3045     }
3046
3047   return false;
3048 }
3049
3050 /* Accept integer operands in the range 0..0xffffffff.  We have to check the
3051    range carefully since this predicate is used in DImode contexts.  Also, we
3052    need some extra crud to make it work when hosted on 64-bit machines.  */
3053
3054 int
3055 const_uint32_operand (rtx op, enum machine_mode mode)
3056 {
3057   /* It doesn't make sense to ask this question with a mode that is
3058      not larger than 32 bits.  */
3059   if (GET_MODE_BITSIZE (mode) <= 32)
3060     abort ();
3061
3062 #if HOST_BITS_PER_WIDE_INT > 32
3063   /* All allowed constants will fit a CONST_INT.  */
3064   return (GET_CODE (op) == CONST_INT
3065           && (INTVAL (op) >= 0 && INTVAL (op) <= 0xffffffffL));
3066 #else
3067   return (GET_CODE (op) == CONST_INT
3068           || (GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_HIGH (op) == 0));
3069 #endif
3070 }
3071
3072 /* Accept integer operands in the range -0x80000000..0x7fffffff.  We have
3073    to check the range carefully since this predicate is used in DImode
3074    contexts.  */
3075
3076 int
3077 const_sint32_operand (rtx op, enum machine_mode mode)
3078 {
3079   /* It doesn't make sense to ask this question with a mode that is
3080      not larger than 32 bits.  */
3081   if (GET_MODE_BITSIZE (mode) <= 32)
3082     abort ();
3083
3084   /* All allowed constants will fit a CONST_INT.  */
3085   return (GET_CODE (op) == CONST_INT
3086           && (INTVAL (op) >= (-0x7fffffff - 1) && INTVAL (op) <= 0x7fffffff));
3087 }
3088
3089 /* Operand predicates for implementing asymmetric pc-relative addressing
3090    on m68k.  The m68k supports pc-relative addressing (mode 7, register 2)
3091    when used as a source operand, but not as a destination operand.
3092
3093    We model this by restricting the meaning of the basic predicates
3094    (general_operand, memory_operand, etc) to forbid the use of this
3095    addressing mode, and then define the following predicates that permit
3096    this addressing mode.  These predicates can then be used for the
3097    source operands of the appropriate instructions.
3098
3099    n.b.  While it is theoretically possible to change all machine patterns
3100    to use this addressing more where permitted by the architecture,
3101    it has only been implemented for "common" cases: SImode, HImode, and
3102    QImode operands, and only for the principle operations that would
3103    require this addressing mode: data movement and simple integer operations.
3104
3105    In parallel with these new predicates, two new constraint letters
3106    were defined: 'S' and 'T'.  'S' is the -mpcrel analog of 'm'.
3107    'T' replaces 's' in the non-pcrel case.  It is a no-op in the pcrel case.
3108    In the pcrel case 's' is only valid in combination with 'a' registers.
3109    See addsi3, subsi3, cmpsi, and movsi patterns for a better understanding
3110    of how these constraints are used.
3111
3112    The use of these predicates is strictly optional, though patterns that
3113    don't will cause an extra reload register to be allocated where one
3114    was not necessary:
3115
3116         lea (abc:w,%pc),%a0     ; need to reload address
3117         moveq &1,%d1            ; since write to pc-relative space
3118         movel %d1,%a0@          ; is not allowed
3119         ...
3120         lea (abc:w,%pc),%a1     ; no need to reload address here
3121         movel %a1@,%d0          ; since "movel (abc:w,%pc),%d0" is ok
3122
3123    For more info, consult tiemann@cygnus.com.
3124
3125
3126    All of the ugliness with predicates and constraints is due to the
3127    simple fact that the m68k does not allow a pc-relative addressing
3128    mode as a destination.  gcc does not distinguish between source and
3129    destination addresses.  Hence, if we claim that pc-relative address
3130    modes are valid, e.g. GO_IF_LEGITIMATE_ADDRESS accepts them, then we
3131    end up with invalid code.  To get around this problem, we left
3132    pc-relative modes as invalid addresses, and then added special
3133    predicates and constraints to accept them.
3134
3135    A cleaner way to handle this is to modify gcc to distinguish
3136    between source and destination addresses.  We can then say that
3137    pc-relative is a valid source address but not a valid destination
3138    address, and hopefully avoid a lot of the predicate and constraint
3139    hackery.  Unfortunately, this would be a pretty big change.  It would
3140    be a useful change for a number of ports, but there aren't any current
3141    plans to undertake this.
3142
3143    ***************************************************************************/
3144
3145
3146 /* Special case of a general operand that's used as a source operand.
3147    Use this to permit reads from PC-relative memory when -mpcrel
3148    is specified.  */
3149
3150 int
3151 general_src_operand (rtx op, enum machine_mode mode)
3152 {
3153   if (TARGET_PCREL
3154       && GET_CODE (op) == MEM
3155       && (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
3156           || GET_CODE (XEXP (op, 0)) == LABEL_REF
3157           || GET_CODE (XEXP (op, 0)) == CONST))
3158     return 1;
3159   return general_operand (op, mode);
3160 }
3161
3162 /* Special case of a nonimmediate operand that's used as a source.
3163    Use this to permit reads from PC-relative memory when -mpcrel
3164    is specified.  */
3165
3166 int
3167 nonimmediate_src_operand (rtx op, enum machine_mode mode)
3168 {
3169   if (TARGET_PCREL && GET_CODE (op) == MEM
3170       && (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
3171           || GET_CODE (XEXP (op, 0)) == LABEL_REF
3172           || GET_CODE (XEXP (op, 0)) == CONST))
3173     return 1;
3174   return nonimmediate_operand (op, mode);
3175 }
3176
3177 /* Special case of a memory operand that's used as a source.
3178    Use this to permit reads from PC-relative memory when -mpcrel
3179    is specified.  */
3180
3181 int
3182 memory_src_operand (rtx op, enum machine_mode mode)
3183 {
3184   if (TARGET_PCREL && GET_CODE (op) == MEM
3185       && (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
3186           || GET_CODE (XEXP (op, 0)) == LABEL_REF
3187           || GET_CODE (XEXP (op, 0)) == CONST))
3188     return 1;
3189   return memory_operand (op, mode);
3190 }
3191
3192 int
3193 post_inc_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3194 {
3195   return MEM_P (op) && GET_CODE (XEXP (op, 0)) == POST_INC;
3196 }
3197
3198 int
3199 pre_dec_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3200 {
3201   return MEM_P (op) && GET_CODE (XEXP (op, 0)) == PRE_DEC;
3202 }
3203
3204 /* Predicate that accepts only a pc-relative address.  This is needed
3205    because pc-relative addresses don't satisfy the predicate
3206    "general_src_operand".  */
3207
3208 int
3209 pcrel_address (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3210 {
3211   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF
3212           || GET_CODE (op) == CONST);
3213 }
3214
3215 const char *
3216 output_andsi3 (rtx *operands)
3217 {
3218   int logval;
3219   if (GET_CODE (operands[2]) == CONST_INT
3220       && (INTVAL (operands[2]) | 0xffff) == -1
3221       && (DATA_REG_P (operands[0])
3222           || offsettable_memref_p (operands[0]))
3223       && !TARGET_COLDFIRE)
3224     {
3225       if (GET_CODE (operands[0]) != REG)
3226         operands[0] = adjust_address (operands[0], HImode, 2);
3227       operands[2] = GEN_INT (INTVAL (operands[2]) & 0xffff);
3228       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
3229       CC_STATUS_INIT;
3230       if (operands[2] == const0_rtx)
3231         return "clr%.w %0";
3232       return "and%.w %2,%0";
3233     }
3234   if (GET_CODE (operands[2]) == CONST_INT
3235       && (logval = exact_log2 (~ INTVAL (operands[2]))) >= 0
3236       && (DATA_REG_P (operands[0])
3237           || offsettable_memref_p (operands[0])))
3238     {
3239       if (DATA_REG_P (operands[0]))
3240         {
3241           operands[1] = GEN_INT (logval);
3242         }
3243       else
3244         {
3245           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3246           operands[1] = GEN_INT (logval % 8);
3247         }
3248       /* This does not set condition codes in a standard way.  */
3249       CC_STATUS_INIT;
3250       return "bclr %1,%0";
3251     }
3252   return "and%.l %2,%0";
3253 }
3254
3255 const char *
3256 output_iorsi3 (rtx *operands)
3257 {
3258   register int logval;
3259   if (GET_CODE (operands[2]) == CONST_INT
3260       && INTVAL (operands[2]) >> 16 == 0
3261       && (DATA_REG_P (operands[0])
3262           || offsettable_memref_p (operands[0]))
3263       && !TARGET_COLDFIRE)
3264     {
3265       if (GET_CODE (operands[0]) != REG)
3266         operands[0] = adjust_address (operands[0], HImode, 2);
3267       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
3268       CC_STATUS_INIT;
3269       if (INTVAL (operands[2]) == 0xffff)
3270         return "mov%.w %2,%0";
3271       return "or%.w %2,%0";
3272     }
3273   if (GET_CODE (operands[2]) == CONST_INT
3274       && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
3275       && (DATA_REG_P (operands[0])
3276           || offsettable_memref_p (operands[0])))
3277     {
3278       if (DATA_REG_P (operands[0]))
3279         operands[1] = GEN_INT (logval);
3280       else
3281         {
3282           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3283           operands[1] = GEN_INT (logval % 8);
3284         }
3285       CC_STATUS_INIT;
3286       return "bset %1,%0";
3287     }
3288   return "or%.l %2,%0";
3289 }
3290
3291 const char *
3292 output_xorsi3 (rtx *operands)
3293 {
3294   register int logval;
3295   if (GET_CODE (operands[2]) == CONST_INT
3296       && INTVAL (operands[2]) >> 16 == 0
3297       && (offsettable_memref_p (operands[0]) || DATA_REG_P (operands[0]))
3298       && !TARGET_COLDFIRE)
3299     {
3300       if (! DATA_REG_P (operands[0]))
3301         operands[0] = adjust_address (operands[0], HImode, 2);
3302       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
3303       CC_STATUS_INIT;
3304       if (INTVAL (operands[2]) == 0xffff)
3305         return "not%.w %0";
3306       return "eor%.w %2,%0";
3307     }
3308   if (GET_CODE (operands[2]) == CONST_INT
3309       && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
3310       && (DATA_REG_P (operands[0])
3311           || offsettable_memref_p (operands[0])))
3312     {
3313       if (DATA_REG_P (operands[0]))
3314         operands[1] = GEN_INT (logval);
3315       else
3316         {
3317           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3318           operands[1] = GEN_INT (logval % 8);
3319         }
3320       CC_STATUS_INIT;
3321       return "bchg %1,%0";
3322     }
3323   return "eor%.l %2,%0";
3324 }
3325
3326 #ifdef M68K_TARGET_COFF
3327
3328 /* Output assembly to switch to section NAME with attribute FLAGS.  */
3329
3330 static void
3331 m68k_coff_asm_named_section (const char *name, unsigned int flags, 
3332                              tree decl ATTRIBUTE_UNUSED)
3333 {
3334   char flagchar;
3335
3336   if (flags & SECTION_WRITE)
3337     flagchar = 'd';
3338   else
3339     flagchar = 'x';
3340
3341   fprintf (asm_out_file, "\t.section\t%s,\"%c\"\n", name, flagchar);
3342 }
3343
3344 #endif /* M68K_TARGET_COFF */
3345
3346 static void
3347 m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
3348                       HOST_WIDE_INT delta,
3349                       HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
3350                       tree function)
3351 {
3352   rtx xops[1];
3353   const char *fmt;
3354
3355   if (delta > 0 && delta <= 8)
3356     asm_fprintf (file, MOTOROLA ?
3357                          "\taddq.l %I%d,4(%Rsp)\n" :
3358                          "\taddql %I%d,%Rsp@(4)\n",
3359                  (int) delta);
3360   else if (delta < 0 && delta >= -8)
3361     asm_fprintf (file, MOTOROLA ?
3362                          "\tsubq.l %I%d,4(%Rsp)\n" :
3363                          "\tsubql %I%d,%Rsp@(4)\n",
3364                  (int) -delta);
3365   else if (TARGET_COLDFIRE)
3366     {
3367       /* ColdFire can't add/sub a constant to memory unless it is in
3368          the range of addq/subq.  So load the value into %d0 and
3369          then add it to 4(%sp). */
3370       if (delta >= -128 && delta <= 127)
3371         asm_fprintf (file, MOTOROLA ?
3372                      "\tmoveq.l %I%wd,%Rd0\n" :
3373                      "\tmoveql %I%wd,%Rd0\n", delta);
3374       else
3375         asm_fprintf (file, MOTOROLA ?
3376                      "\tmove.l %I%wd,%Rd0\n" :
3377                      "\tmovel %I%wd,%Rd0\n", delta);
3378       asm_fprintf (file, MOTOROLA ?
3379                    "\tadd.l %Rd0,4(%Rsp)\n" :
3380                    "\taddl %Rd0,%Rsp@(4)\n");
3381     }
3382   else
3383     asm_fprintf (file, MOTOROLA ?
3384                          "\tadd.l %I%wd,4(%Rsp)\n" :
3385                          "\taddl %I%wd,%Rsp@(4)\n",
3386                  delta);
3387
3388   xops[0] = DECL_RTL (function);
3389
3390   /* Logic taken from call patterns in m68k.md.  */
3391   if (flag_pic)
3392     {
3393       if (TARGET_PCREL)
3394         fmt = "bra.l %o0";
3395       else if ((flag_pic == 1) || TARGET_68020)
3396         {
3397           if (MOTOROLA)
3398 #if defined(USE_GAS)
3399             fmt = "bra.l %0@PLTPC";
3400 #else
3401             fmt = "bra %0@PLTPC";
3402 #endif
3403           else /* !MOTOROLA */
3404 #ifdef USE_GAS
3405             fmt = "bra.l %0";
3406 #else
3407             fmt = "jra %0,a1";
3408 #endif
3409         }
3410       else if (optimize_size || TARGET_ID_SHARED_LIBRARY)
3411         fmt = "move.l %0@GOT(%%a5), %%a1\n\tjmp (%%a1)";
3412       else
3413         fmt = "lea %0-.-8,%%a1\n\tjsr 0(%%pc,%%a1)";
3414     }
3415   else
3416     {
3417 #if MOTOROLA && !defined (USE_GAS)
3418       fmt = "jmp %0";
3419 #else
3420       fmt = "jra %0";
3421 #endif
3422     }
3423
3424   output_asm_insn (fmt, xops);
3425 }
3426
3427 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
3428
3429 static rtx
3430 m68k_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
3431                        int incoming ATTRIBUTE_UNUSED)
3432 {
3433   return gen_rtx_REG (Pmode, M68K_STRUCT_VALUE_REGNUM);
3434 }
3435
3436 /* Return nonzero if register old_reg can be renamed to register new_reg.  */
3437 int
3438 m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
3439                            unsigned int new_reg)
3440 {
3441
3442   /* Interrupt functions can only use registers that have already been
3443      saved by the prologue, even if they would normally be
3444      call-clobbered.  */
3445
3446   if (m68k_interrupt_function_p (current_function_decl)
3447       && !regs_ever_live[new_reg])
3448     return 0;
3449
3450   return 1;
3451 }