OSDN Git Service

0c38fbdfd19659ae9e5fe357d94896feb9b88878
[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, 2005, 2006, 2007
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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "recog.h"
37 #include "toplev.h"
38 #include "expr.h"
39 #include "reload.h"
40 #include "tm_p.h"
41 #include "target.h"
42 #include "target-def.h"
43 #include "debug.h"
44 #include "flags.h"
45 #include "df.h"
46 /* ??? Need to add a dependency between m68k.o and sched-int.h.  */
47 #include "sched-int.h"
48 #include "insn-codes.h"
49
50 enum reg_class regno_reg_class[] =
51 {
52   DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
53   DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
54   ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
55   ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
56   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
57   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
58   ADDR_REGS
59 };
60
61
62 /* The minimum number of integer registers that we want to save with the
63    movem instruction.  Using two movel instructions instead of a single
64    moveml is about 15% faster for the 68020 and 68030 at no expense in
65    code size.  */
66 #define MIN_MOVEM_REGS 3
67
68 /* The minimum number of floating point registers that we want to save
69    with the fmovem instruction.  */
70 #define MIN_FMOVEM_REGS 1
71
72 /* Structure describing stack frame layout.  */
73 struct m68k_frame
74 {
75   /* Stack pointer to frame pointer offset.  */
76   HOST_WIDE_INT offset;
77
78   /* Offset of FPU registers.  */
79   HOST_WIDE_INT foffset;
80
81   /* Frame size in bytes (rounded up).  */
82   HOST_WIDE_INT size;
83
84   /* Data and address register.  */
85   int reg_no;
86   unsigned int reg_mask;
87
88   /* FPU registers.  */
89   int fpu_no;
90   unsigned int fpu_mask;
91
92   /* Offsets relative to ARG_POINTER.  */
93   HOST_WIDE_INT frame_pointer_offset;
94   HOST_WIDE_INT stack_pointer_offset;
95
96   /* Function which the above information refers to.  */
97   int funcdef_no;
98 };
99
100 /* Current frame information calculated by m68k_compute_frame_layout().  */
101 static struct m68k_frame current_frame;
102
103 /* Structure describing an m68k address.
104
105    If CODE is UNKNOWN, the address is BASE + INDEX * SCALE + OFFSET,
106    with null fields evaluating to 0.  Here:
107
108    - BASE satisfies m68k_legitimate_base_reg_p
109    - INDEX satisfies m68k_legitimate_index_reg_p
110    - OFFSET satisfies m68k_legitimate_constant_address_p
111
112    INDEX is either HImode or SImode.  The other fields are SImode.
113
114    If CODE is PRE_DEC, the address is -(BASE).  If CODE is POST_INC,
115    the address is (BASE)+.  */
116 struct m68k_address {
117   enum rtx_code code;
118   rtx base;
119   rtx index;
120   rtx offset;
121   int scale;
122 };
123
124 static int m68k_sched_adjust_cost (rtx, rtx, rtx, int);
125 static int m68k_sched_variable_issue (FILE *, int, rtx, int);
126 static void m68k_sched_md_init_global (FILE *, int, int);
127 static void m68k_sched_md_finish_global (FILE *, int);
128 static void m68k_sched_md_init (FILE *, int, int);
129 static void m68k_sched_dfa_pre_advance_cycle (void);
130 static void m68k_sched_dfa_post_advance_cycle (void);
131
132 static bool m68k_handle_option (size_t, const char *, int);
133 static rtx find_addr_reg (rtx);
134 static const char *singlemove_string (rtx *);
135 #ifdef M68K_TARGET_COFF
136 static void m68k_coff_asm_named_section (const char *, unsigned int, tree);
137 #endif /* M68K_TARGET_COFF */
138 static void m68k_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
139                                           HOST_WIDE_INT, tree);
140 static rtx m68k_struct_value_rtx (tree, int);
141 static tree m68k_handle_fndecl_attribute (tree *node, tree name,
142                                           tree args, int flags,
143                                           bool *no_add_attrs);
144 static void m68k_compute_frame_layout (void);
145 static bool m68k_save_reg (unsigned int regno, bool interrupt_handler);
146 static bool m68k_ok_for_sibcall_p (tree, tree);
147 static bool m68k_rtx_costs (rtx, int, int, int *);
148 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
149 static bool m68k_return_in_memory (tree, tree);
150 #endif
151 \f
152
153 /* Specify the identification number of the library being built */
154 const char *m68k_library_id_string = "_current_shared_library_a5_offset_";
155
156 /* Nonzero if the last compare/test insn had FP operands.  The
157    sCC expanders peek at this to determine what to do for the
158    68060, which has no fsCC instructions.  */
159 int m68k_last_compare_had_fp_operands;
160 \f
161 /* Initialize the GCC target structure.  */
162
163 #if INT_OP_GROUP == INT_OP_DOT_WORD
164 #undef TARGET_ASM_ALIGNED_HI_OP
165 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
166 #endif
167
168 #if INT_OP_GROUP == INT_OP_NO_DOT
169 #undef TARGET_ASM_BYTE_OP
170 #define TARGET_ASM_BYTE_OP "\tbyte\t"
171 #undef TARGET_ASM_ALIGNED_HI_OP
172 #define TARGET_ASM_ALIGNED_HI_OP "\tshort\t"
173 #undef TARGET_ASM_ALIGNED_SI_OP
174 #define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
175 #endif
176
177 #if INT_OP_GROUP == INT_OP_DC
178 #undef TARGET_ASM_BYTE_OP
179 #define TARGET_ASM_BYTE_OP "\tdc.b\t"
180 #undef TARGET_ASM_ALIGNED_HI_OP
181 #define TARGET_ASM_ALIGNED_HI_OP "\tdc.w\t"
182 #undef TARGET_ASM_ALIGNED_SI_OP
183 #define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
184 #endif
185
186 #undef TARGET_ASM_UNALIGNED_HI_OP
187 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
188 #undef TARGET_ASM_UNALIGNED_SI_OP
189 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
190
191 #undef TARGET_ASM_OUTPUT_MI_THUNK
192 #define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
193 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
194 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
195
196 #undef TARGET_ASM_FILE_START_APP_OFF
197 #define TARGET_ASM_FILE_START_APP_OFF true
198
199 #undef TARGET_SCHED_ADJUST_COST
200 #define TARGET_SCHED_ADJUST_COST m68k_sched_adjust_cost
201
202 #undef TARGET_SCHED_VARIABLE_ISSUE
203 #define TARGET_SCHED_VARIABLE_ISSUE m68k_sched_variable_issue
204
205 #undef TARGET_SCHED_INIT_GLOBAL
206 #define TARGET_SCHED_INIT_GLOBAL m68k_sched_md_init_global
207
208 #undef TARGET_SCHED_FINISH_GLOBAL
209 #define TARGET_SCHED_FINISH_GLOBAL m68k_sched_md_finish_global
210
211 #undef TARGET_SCHED_INIT
212 #define TARGET_SCHED_INIT m68k_sched_md_init
213
214 #undef TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE
215 #define TARGET_SCHED_DFA_PRE_ADVANCE_CYCLE m68k_sched_dfa_pre_advance_cycle
216
217 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
218 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE m68k_sched_dfa_post_advance_cycle
219
220 #undef TARGET_HANDLE_OPTION
221 #define TARGET_HANDLE_OPTION m68k_handle_option
222
223 #undef TARGET_RTX_COSTS
224 #define TARGET_RTX_COSTS m68k_rtx_costs
225
226 #undef TARGET_ATTRIBUTE_TABLE
227 #define TARGET_ATTRIBUTE_TABLE m68k_attribute_table
228
229 #undef TARGET_PROMOTE_PROTOTYPES
230 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
231
232 #undef TARGET_STRUCT_VALUE_RTX
233 #define TARGET_STRUCT_VALUE_RTX m68k_struct_value_rtx
234
235 #undef TARGET_CANNOT_FORCE_CONST_MEM
236 #define TARGET_CANNOT_FORCE_CONST_MEM m68k_illegitimate_symbolic_constant_p
237
238 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
239 #define TARGET_FUNCTION_OK_FOR_SIBCALL m68k_ok_for_sibcall_p
240
241 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
242 #undef TARGET_RETURN_IN_MEMORY
243 #define TARGET_RETURN_IN_MEMORY m68k_return_in_memory
244 #endif
245
246 static const struct attribute_spec m68k_attribute_table[] =
247 {
248   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
249   { "interrupt", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
250   { "interrupt_handler", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
251   { "interrupt_thread", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
252   { NULL,                0, 0, false, false, false, NULL }
253 };
254
255 struct gcc_target targetm = TARGET_INITIALIZER;
256 \f
257 /* Base flags for 68k ISAs.  */
258 #define FL_FOR_isa_00    FL_ISA_68000
259 #define FL_FOR_isa_10    (FL_FOR_isa_00 | FL_ISA_68010)
260 /* FL_68881 controls the default setting of -m68881.  gcc has traditionally
261    generated 68881 code for 68020 and 68030 targets unless explicitly told
262    not to.  */
263 #define FL_FOR_isa_20    (FL_FOR_isa_10 | FL_ISA_68020 \
264                           | FL_BITFIELD | FL_68881)
265 #define FL_FOR_isa_40    (FL_FOR_isa_20 | FL_ISA_68040)
266 #define FL_FOR_isa_cpu32 (FL_FOR_isa_10 | FL_ISA_68020)
267
268 /* Base flags for ColdFire ISAs.  */
269 #define FL_FOR_isa_a     (FL_COLDFIRE | FL_ISA_A)
270 #define FL_FOR_isa_aplus (FL_FOR_isa_a | FL_ISA_APLUS | FL_CF_USP)
271 /* Note ISA_B doesn't necessarily include USP (user stack pointer) support.  */
272 #define FL_FOR_isa_b     (FL_FOR_isa_a | FL_ISA_B | FL_CF_HWDIV)
273 /* ISA_C is not upwardly compatible with ISA_B.  */
274 #define FL_FOR_isa_c     (FL_FOR_isa_a | FL_ISA_C | FL_CF_USP)
275
276 enum m68k_isa
277 {
278   /* Traditional 68000 instruction sets.  */
279   isa_00,
280   isa_10,
281   isa_20,
282   isa_40,
283   isa_cpu32,
284   /* ColdFire instruction set variants.  */
285   isa_a,
286   isa_aplus,
287   isa_b,
288   isa_c,
289   isa_max
290 };
291
292 /* Information about one of the -march, -mcpu or -mtune arguments.  */
293 struct m68k_target_selection
294 {
295   /* The argument being described.  */
296   const char *name;
297
298   /* For -mcpu, this is the device selected by the option.
299      For -mtune and -march, it is a representative device
300      for the microarchitecture or ISA respectively.  */
301   enum target_device device;
302
303   /* The M68K_DEVICE fields associated with DEVICE.  See the comment
304      in m68k-devices.def for details.  FAMILY is only valid for -mcpu.  */
305   const char *family;
306   enum uarch_type microarch;
307   enum m68k_isa isa;
308   unsigned long flags;
309 };
310
311 /* A list of all devices in m68k-devices.def.  Used for -mcpu selection.  */
312 static const struct m68k_target_selection all_devices[] =
313 {
314 #define M68K_DEVICE(NAME,ENUM_VALUE,FAMILY,MULTILIB,MICROARCH,ISA,FLAGS) \
315   { NAME, ENUM_VALUE, FAMILY, u##MICROARCH, ISA, FLAGS | FL_FOR_##ISA },
316 #include "m68k-devices.def"
317 #undef M68K_DEVICE
318   { NULL, unk_device, NULL, unk_arch, isa_max, 0 }
319 };
320
321 /* A list of all ISAs, mapping each one to a representative device.
322    Used for -march selection.  */
323 static const struct m68k_target_selection all_isas[] =
324 {
325   { "68000",    m68000,     NULL,  u68000,   isa_00,    FL_FOR_isa_00 },
326   { "68010",    m68010,     NULL,  u68010,   isa_10,    FL_FOR_isa_10 },
327   { "68020",    m68020,     NULL,  u68020,   isa_20,    FL_FOR_isa_20 },
328   { "68030",    m68030,     NULL,  u68030,   isa_20,    FL_FOR_isa_20 },
329   { "68040",    m68040,     NULL,  u68040,   isa_40,    FL_FOR_isa_40 },
330   { "68060",    m68060,     NULL,  u68060,   isa_40,    FL_FOR_isa_40 },
331   { "cpu32",    cpu32,      NULL,  ucpu32,   isa_20,    FL_FOR_isa_cpu32 },
332   { "isaa",     mcf5206e,   NULL,  ucfv2,    isa_a,     (FL_FOR_isa_a
333                                                          | FL_CF_HWDIV) },
334   { "isaaplus", mcf5271,    NULL,  ucfv2,    isa_aplus, (FL_FOR_isa_aplus
335                                                          | FL_CF_HWDIV) },
336   { "isab",     mcf5407,    NULL,  ucfv4,    isa_b,     FL_FOR_isa_b },
337   { "isac",     unk_device, NULL,  ucfv4,    isa_c,     (FL_FOR_isa_c
338                                                          | FL_CF_HWDIV) },
339   { NULL,       unk_device, NULL,  unk_arch, isa_max,   0 }
340 };
341
342 /* A list of all microarchitectures, mapping each one to a representative
343    device.  Used for -mtune selection.  */
344 static const struct m68k_target_selection all_microarchs[] =
345 {
346   { "68000",    m68000,     NULL,  u68000,    isa_00,  FL_FOR_isa_00 },
347   { "68010",    m68010,     NULL,  u68010,    isa_10,  FL_FOR_isa_10 },
348   { "68020",    m68020,     NULL,  u68020,    isa_20,  FL_FOR_isa_20 },
349   { "68020-40", m68020,     NULL,  u68020_40, isa_20,  FL_FOR_isa_20 },
350   { "68020-60", m68020,     NULL,  u68020_60, isa_20,  FL_FOR_isa_20 },
351   { "68030",    m68030,     NULL,  u68030,    isa_20,  FL_FOR_isa_20 },
352   { "68040",    m68040,     NULL,  u68040,    isa_40,  FL_FOR_isa_40 },
353   { "68060",    m68060,     NULL,  u68060,    isa_40,  FL_FOR_isa_40 },
354   { "cpu32",    cpu32,      NULL,  ucpu32,    isa_20,  FL_FOR_isa_cpu32 },
355   { "cfv1",     mcf51qe,    NULL,  ucfv1,     isa_c,   FL_FOR_isa_c },
356   { "cfv2",     mcf5206,    NULL,  ucfv2,     isa_a,   FL_FOR_isa_a },
357   { "cfv3",     mcf5307,    NULL,  ucfv3,     isa_a,   (FL_FOR_isa_a
358                                                         | FL_CF_HWDIV) },
359   { "cfv4",     mcf5407,    NULL,  ucfv4,     isa_b,   FL_FOR_isa_b },
360   { "cfv4e",    mcf547x,    NULL,  ucfv4e,    isa_b,   (FL_FOR_isa_b
361                                                         | FL_CF_USP
362                                                         | FL_CF_EMAC
363                                                         | FL_CF_FPU) },
364   { NULL,       unk_device, NULL,  unk_arch,  isa_max, 0 }
365 };
366 \f
367 /* The entries associated with the -mcpu, -march and -mtune settings,
368    or null for options that have not been used.  */
369 const struct m68k_target_selection *m68k_cpu_entry;
370 const struct m68k_target_selection *m68k_arch_entry;
371 const struct m68k_target_selection *m68k_tune_entry;
372
373 /* Which CPU we are generating code for.  */
374 enum target_device m68k_cpu;
375
376 /* Which microarchitecture to tune for.  */
377 enum uarch_type m68k_tune;
378
379 /* Which FPU to use.  */
380 enum fpu_type m68k_fpu;
381
382 /* The set of FL_* flags that apply to the target processor.  */
383 unsigned int m68k_cpu_flags;
384
385 /* Asm templates for calling or jumping to an arbitrary symbolic address,
386    or NULL if such calls or jumps are not supported.  The address is held
387    in operand 0.  */
388 const char *m68k_symbolic_call;
389 const char *m68k_symbolic_jump;
390
391 /* Enum variable that corresponds to m68k_symbolic_call values.  */
392 enum M68K_SYMBOLIC_CALL m68k_symbolic_call_var;
393
394 \f
395 /* See whether TABLE has an entry with name NAME.  Return true and
396    store the entry in *ENTRY if so, otherwise return false and
397    leave *ENTRY alone.  */
398
399 static bool
400 m68k_find_selection (const struct m68k_target_selection **entry,
401                      const struct m68k_target_selection *table,
402                      const char *name)
403 {
404   size_t i;
405
406   for (i = 0; table[i].name; i++)
407     if (strcmp (table[i].name, name) == 0)
408       {
409         *entry = table + i;
410         return true;
411       }
412   return false;
413 }
414
415 /* Implement TARGET_HANDLE_OPTION.  */
416
417 static bool
418 m68k_handle_option (size_t code, const char *arg, int value)
419 {
420   switch (code)
421     {
422     case OPT_march_:
423       return m68k_find_selection (&m68k_arch_entry, all_isas, arg);
424
425     case OPT_mcpu_:
426       return m68k_find_selection (&m68k_cpu_entry, all_devices, arg);
427
428     case OPT_mtune_:
429       return m68k_find_selection (&m68k_tune_entry, all_microarchs, arg);
430
431     case OPT_m5200:
432       return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206");
433
434     case OPT_m5206e:
435       return m68k_find_selection (&m68k_cpu_entry, all_devices, "5206e");
436
437     case OPT_m528x:
438       return m68k_find_selection (&m68k_cpu_entry, all_devices, "528x");
439
440     case OPT_m5307:
441       return m68k_find_selection (&m68k_cpu_entry, all_devices, "5307");
442
443     case OPT_m5407:
444       return m68k_find_selection (&m68k_cpu_entry, all_devices, "5407");
445
446     case OPT_mcfv4e:
447       return m68k_find_selection (&m68k_cpu_entry, all_devices, "547x");
448
449     case OPT_m68000:
450     case OPT_mc68000:
451       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68000");
452
453     case OPT_m68010:
454       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68010");
455
456     case OPT_m68020:
457     case OPT_mc68020:
458       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68020");
459
460     case OPT_m68020_40:
461       return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
462                                    "68020-40")
463               && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
464
465     case OPT_m68020_60:
466       return (m68k_find_selection (&m68k_tune_entry, all_microarchs,
467                                    "68020-60")
468               && m68k_find_selection (&m68k_cpu_entry, all_devices, "68020"));
469
470     case OPT_m68030:
471       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68030");
472
473     case OPT_m68040:
474       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68040");
475
476     case OPT_m68060:
477       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68060");
478
479     case OPT_m68302:
480       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68302");
481
482     case OPT_m68332:
483     case OPT_mcpu32:
484       return m68k_find_selection (&m68k_cpu_entry, all_devices, "68332");
485
486     case OPT_mshared_library_id_:
487       if (value > MAX_LIBRARY_ID)
488         error ("-mshared-library-id=%s is not between 0 and %d",
489                arg, MAX_LIBRARY_ID);
490       else
491         asprintf ((char **) &m68k_library_id_string, "%d", (value * -4) - 4);
492       return true;
493
494     default:
495       return true;
496     }
497 }
498
499 /* Sometimes certain combinations of command options do not make
500    sense on a particular target machine.  You can define a macro
501    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
502    defined, is executed once just after all the command options have
503    been parsed.
504
505    Don't use this macro to turn on various extra optimizations for
506    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
507
508 void
509 override_options (void)
510 {
511   const struct m68k_target_selection *entry;
512   unsigned long target_mask;
513
514   /* User can choose:
515
516      -mcpu=
517      -march=
518      -mtune=
519
520      -march=ARCH should generate code that runs any processor
521      implementing architecture ARCH.  -mcpu=CPU should override -march
522      and should generate code that runs on processor CPU, making free
523      use of any instructions that CPU understands.  -mtune=UARCH applies
524      on top of -mcpu or -march and optimizes the code for UARCH.  It does
525      not change the target architecture.  */
526   if (m68k_cpu_entry)
527     {
528       /* Complain if the -march setting is for a different microarchitecture,
529          or includes flags that the -mcpu setting doesn't.  */
530       if (m68k_arch_entry
531           && (m68k_arch_entry->microarch != m68k_cpu_entry->microarch
532               || (m68k_arch_entry->flags & ~m68k_cpu_entry->flags) != 0))
533         warning (0, "-mcpu=%s conflicts with -march=%s",
534                  m68k_cpu_entry->name, m68k_arch_entry->name);
535
536       entry = m68k_cpu_entry;
537     }
538   else
539     entry = m68k_arch_entry;
540
541   if (!entry)
542     entry = all_devices + TARGET_CPU_DEFAULT;
543
544   m68k_cpu_flags = entry->flags;
545
546   /* Use the architecture setting to derive default values for
547      certain flags.  */
548   target_mask = 0;
549
550   /* ColdFire is lenient about alignment.  */
551   if (!TARGET_COLDFIRE)
552     target_mask |= MASK_STRICT_ALIGNMENT;
553
554   if ((m68k_cpu_flags & FL_BITFIELD) != 0)
555     target_mask |= MASK_BITFIELD;
556   if ((m68k_cpu_flags & FL_CF_HWDIV) != 0)
557     target_mask |= MASK_CF_HWDIV;
558   if ((m68k_cpu_flags & (FL_68881 | FL_CF_FPU)) != 0)
559     target_mask |= MASK_HARD_FLOAT;
560   target_flags |= target_mask & ~target_flags_explicit;
561
562   /* Set the directly-usable versions of the -mcpu and -mtune settings.  */
563   m68k_cpu = entry->device;
564   if (m68k_tune_entry)
565     m68k_tune = m68k_tune_entry->microarch;
566 #ifdef M68K_DEFAULT_TUNE
567   else if (!m68k_cpu_entry && !m68k_arch_entry)
568     m68k_tune = M68K_DEFAULT_TUNE;
569 #endif
570   else
571     m68k_tune = entry->microarch;
572
573   /* Set the type of FPU.  */
574   m68k_fpu = (!TARGET_HARD_FLOAT ? FPUTYPE_NONE
575               : (m68k_cpu_flags & FL_COLDFIRE) != 0 ? FPUTYPE_COLDFIRE
576               : FPUTYPE_68881);
577
578   /* Sanity check to ensure that msep-data and mid-sahred-library are not
579    * both specified together.  Doing so simply doesn't make sense.
580    */
581   if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
582     error ("cannot specify both -msep-data and -mid-shared-library");
583
584   /* If we're generating code for a separate A5 relative data segment,
585    * we've got to enable -fPIC as well.  This might be relaxable to
586    * -fpic but it hasn't been tested properly.
587    */
588   if (TARGET_SEP_DATA || TARGET_ID_SHARED_LIBRARY)
589     flag_pic = 2;
590
591   /* -mpcrel -fPIC uses 32-bit pc-relative displacements.  Raise an
592      error if the target does not support them.  */
593   if (TARGET_PCREL && !TARGET_68020 && flag_pic == 2)
594     error ("-mpcrel -fPIC is not currently supported on selected cpu");
595
596   /* ??? A historic way of turning on pic, or is this intended to
597      be an embedded thing that doesn't have the same name binding
598      significance that it does on hosted ELF systems?  */
599   if (TARGET_PCREL && flag_pic == 0)
600     flag_pic = 1;
601
602   if (!flag_pic)
603     {
604       m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_JSR;
605
606       m68k_symbolic_jump = "jra %a0";
607     }
608   else if (TARGET_ID_SHARED_LIBRARY)
609     /* All addresses must be loaded from the GOT.  */
610     ;
611   else if (TARGET_68020 || TARGET_ISAB || TARGET_ISAC)
612     {
613       if (TARGET_PCREL)
614         m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_C;
615       else
616         m68k_symbolic_call_var = M68K_SYMBOLIC_CALL_BSR_P;
617
618       if (TARGET_ISAC)
619         /* No unconditional long branch */;
620       else if (TARGET_PCREL)
621         m68k_symbolic_jump = "bra%.l %c0";
622       else
623         m68k_symbolic_jump = "bra%.l %p0";
624       /* Turn off function cse if we are doing PIC.  We always want
625          function call to be done as `bsr foo@PLTPC'.  */
626       /* ??? It's traditional to do this for -mpcrel too, but it isn't
627          clear how intentional that is.  */
628       flag_no_function_cse = 1;
629     }
630
631   switch (m68k_symbolic_call_var)
632     {
633     case M68K_SYMBOLIC_CALL_JSR:
634       m68k_symbolic_call = "jsr %a0";
635       break;
636
637     case M68K_SYMBOLIC_CALL_BSR_C:
638       m68k_symbolic_call = "bsr%.l %c0";
639       break;
640
641     case M68K_SYMBOLIC_CALL_BSR_P:
642       m68k_symbolic_call = "bsr%.l %p0";
643       break;
644
645     case M68K_SYMBOLIC_CALL_NONE:
646       gcc_assert (m68k_symbolic_call == NULL);
647       break;
648
649     default:
650       gcc_unreachable ();
651     }
652
653 #ifndef ASM_OUTPUT_ALIGN_WITH_NOP
654   if (align_labels > 2)
655     {
656       warning (0, "-falign-labels=%d is not supported", align_labels);
657       align_labels = 0;
658     }
659   if (align_loops > 2)
660     {
661       warning (0, "-falign-loops=%d is not supported", align_loops);
662       align_loops = 0;
663     }
664 #endif
665
666   SUBTARGET_OVERRIDE_OPTIONS;
667
668   /* Setup scheduling options.  */
669   if (TUNE_CFV1)
670     m68k_sched_cpu = CPU_CFV1;
671   else if (TUNE_CFV2)
672     m68k_sched_cpu = CPU_CFV2;
673   else if (TUNE_CFV3)
674     m68k_sched_cpu = CPU_CFV3;
675   else
676     {
677       m68k_sched_cpu = CPU_UNKNOWN;
678       flag_schedule_insns = 0;
679       flag_schedule_insns_after_reload = 0;
680       flag_modulo_sched = 0;
681     }
682
683   if (m68k_sched_cpu != CPU_UNKNOWN)
684     {
685       if ((m68k_cpu_flags & (FL_CF_EMAC | FL_CF_EMAC_B)) != 0)
686         m68k_sched_mac = MAC_CF_EMAC;
687       else if ((m68k_cpu_flags & FL_CF_MAC) != 0)
688         m68k_sched_mac = MAC_CF_MAC;
689       else
690         m68k_sched_mac = MAC_NO;
691     }
692 }
693
694 /* Generate a macro of the form __mPREFIX_cpu_NAME, where PREFIX is the
695    given argument and NAME is the argument passed to -mcpu.  Return NULL
696    if -mcpu was not passed.  */
697
698 const char *
699 m68k_cpp_cpu_ident (const char *prefix)
700 {
701   if (!m68k_cpu_entry)
702     return NULL;
703   return concat ("__m", prefix, "_cpu_", m68k_cpu_entry->name, NULL);
704 }
705
706 /* Generate a macro of the form __mPREFIX_family_NAME, where PREFIX is the
707    given argument and NAME is the name of the representative device for
708    the -mcpu argument's family.  Return NULL if -mcpu was not passed.  */
709
710 const char *
711 m68k_cpp_cpu_family (const char *prefix)
712 {
713   if (!m68k_cpu_entry)
714     return NULL;
715   return concat ("__m", prefix, "_family_", m68k_cpu_entry->family, NULL);
716 }
717 \f
718 /* Return m68k_fk_interrupt_handler if FUNC has an "interrupt" or
719    "interrupt_handler" attribute and interrupt_thread if FUNC has an
720    "interrupt_thread" attribute.  Otherwise, return
721    m68k_fk_normal_function.  */
722
723 enum m68k_function_kind
724 m68k_get_function_kind (tree func)
725 {
726   tree a;
727
728   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
729   
730   a = lookup_attribute ("interrupt", DECL_ATTRIBUTES (func));
731   if (a != NULL_TREE)
732     return m68k_fk_interrupt_handler;
733
734   a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
735   if (a != NULL_TREE)
736     return m68k_fk_interrupt_handler;
737
738   a = lookup_attribute ("interrupt_thread", DECL_ATTRIBUTES (func));
739   if (a != NULL_TREE)
740     return m68k_fk_interrupt_thread;
741
742   return m68k_fk_normal_function;
743 }
744
745 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
746    struct attribute_spec.handler.  */
747 static tree
748 m68k_handle_fndecl_attribute (tree *node, tree name,
749                               tree args ATTRIBUTE_UNUSED,
750                               int flags ATTRIBUTE_UNUSED,
751                               bool *no_add_attrs)
752 {
753   if (TREE_CODE (*node) != FUNCTION_DECL)
754     {
755       warning (OPT_Wattributes, "%qs attribute only applies to functions",
756                IDENTIFIER_POINTER (name));
757       *no_add_attrs = true;
758     }
759
760   if (m68k_get_function_kind (*node) != m68k_fk_normal_function)
761     {
762       error ("multiple interrupt attributes not allowed");
763       *no_add_attrs = true;
764     }
765
766   if (!TARGET_FIDOA
767       && !strcmp (IDENTIFIER_POINTER (name), "interrupt_thread"))
768     {
769       error ("interrupt_thread is available only on fido");
770       *no_add_attrs = true;
771     }
772
773   return NULL_TREE;
774 }
775
776 static void
777 m68k_compute_frame_layout (void)
778 {
779   int regno, saved;
780   unsigned int mask;
781   enum m68k_function_kind func_kind =
782     m68k_get_function_kind (current_function_decl);
783   bool interrupt_handler = func_kind == m68k_fk_interrupt_handler;
784   bool interrupt_thread = func_kind == m68k_fk_interrupt_thread;
785
786   /* Only compute the frame once per function.
787      Don't cache information until reload has been completed.  */
788   if (current_frame.funcdef_no == current_function_funcdef_no
789       && reload_completed)
790     return;
791
792   current_frame.size = (get_frame_size () + 3) & -4;
793
794   mask = saved = 0;
795
796   /* Interrupt thread does not need to save any register.  */
797   if (!interrupt_thread)
798     for (regno = 0; regno < 16; regno++)
799       if (m68k_save_reg (regno, interrupt_handler))
800         {
801           mask |= 1 << (regno - D0_REG);
802           saved++;
803         }
804   current_frame.offset = saved * 4;
805   current_frame.reg_no = saved;
806   current_frame.reg_mask = mask;
807
808   current_frame.foffset = 0;
809   mask = saved = 0;
810   if (TARGET_HARD_FLOAT)
811     {
812       /* Interrupt thread does not need to save any register.  */
813       if (!interrupt_thread)
814         for (regno = 16; regno < 24; regno++)
815           if (m68k_save_reg (regno, interrupt_handler))
816             {
817               mask |= 1 << (regno - FP0_REG);
818               saved++;
819             }
820       current_frame.foffset = saved * TARGET_FP_REG_SIZE;
821       current_frame.offset += current_frame.foffset;
822     }
823   current_frame.fpu_no = saved;
824   current_frame.fpu_mask = mask;
825
826   /* Remember what function this frame refers to.  */
827   current_frame.funcdef_no = current_function_funcdef_no;
828 }
829
830 HOST_WIDE_INT
831 m68k_initial_elimination_offset (int from, int to)
832 {
833   int argptr_offset;
834   /* The arg pointer points 8 bytes before the start of the arguments,
835      as defined by FIRST_PARM_OFFSET.  This makes it coincident with the
836      frame pointer in most frames.  */
837   argptr_offset = frame_pointer_needed ? 0 : UNITS_PER_WORD;
838   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
839     return argptr_offset;
840
841   m68k_compute_frame_layout ();
842
843   gcc_assert (to == STACK_POINTER_REGNUM);
844   switch (from)
845     {
846     case ARG_POINTER_REGNUM:
847       return current_frame.offset + current_frame.size - argptr_offset;
848     case FRAME_POINTER_REGNUM:
849       return current_frame.offset + current_frame.size;
850     default:
851       gcc_unreachable ();
852     }
853 }
854
855 /* Refer to the array `regs_ever_live' to determine which registers
856    to save; `regs_ever_live[I]' is nonzero if register number I
857    is ever used in the function.  This function is responsible for
858    knowing which registers should not be saved even if used.
859    Return true if we need to save REGNO.  */
860
861 static bool
862 m68k_save_reg (unsigned int regno, bool interrupt_handler)
863 {
864   if (flag_pic && regno == PIC_REG)
865     {
866       if (current_function_saves_all_registers)
867         return true;
868       if (current_function_uses_pic_offset_table)
869         return true;
870       /* Reload may introduce constant pool references into a function
871          that thitherto didn't need a PIC register.  Note that the test
872          above will not catch that case because we will only set
873          current_function_uses_pic_offset_table when emitting
874          the address reloads.  */
875       if (current_function_uses_const_pool)
876         return true;
877     }
878
879   if (current_function_calls_eh_return)
880     {
881       unsigned int i;
882       for (i = 0; ; i++)
883         {
884           unsigned int test = EH_RETURN_DATA_REGNO (i);
885           if (test == INVALID_REGNUM)
886             break;
887           if (test == regno)
888             return true;
889         }
890     }
891
892   /* Fixed regs we never touch.  */
893   if (fixed_regs[regno])
894     return false;
895
896   /* The frame pointer (if it is such) is handled specially.  */
897   if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
898     return false;
899
900   /* Interrupt handlers must also save call_used_regs
901      if they are live or when calling nested functions.  */
902   if (interrupt_handler)
903     {
904       if (df_regs_ever_live_p (regno))
905         return true;
906
907       if (!current_function_is_leaf && call_used_regs[regno])
908         return true;
909     }
910
911   /* Never need to save registers that aren't touched.  */
912   if (!df_regs_ever_live_p (regno))
913     return false;
914
915   /* Otherwise save everything that isn't call-clobbered.  */
916   return !call_used_regs[regno];
917 }
918
919 /* Emit RTL for a MOVEM or FMOVEM instruction.  BASE + OFFSET represents
920    the lowest memory address.  COUNT is the number of registers to be
921    moved, with register REGNO + I being moved if bit I of MASK is set.
922    STORE_P specifies the direction of the move and ADJUST_STACK_P says
923    whether or not this is pre-decrement (if STORE_P) or post-increment
924    (if !STORE_P) operation.  */
925
926 static rtx
927 m68k_emit_movem (rtx base, HOST_WIDE_INT offset,
928                  unsigned int count, unsigned int regno,
929                  unsigned int mask, bool store_p, bool adjust_stack_p)
930 {
931   int i;
932   rtx body, addr, src, operands[2];
933   enum machine_mode mode;
934
935   body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (adjust_stack_p + count));
936   mode = reg_raw_mode[regno];
937   i = 0;
938
939   if (adjust_stack_p)
940     {
941       src = plus_constant (base, (count
942                                   * GET_MODE_SIZE (mode)
943                                   * (HOST_WIDE_INT) (store_p ? -1 : 1)));
944       XVECEXP (body, 0, i++) = gen_rtx_SET (VOIDmode, base, src);
945     }
946
947   for (; mask != 0; mask >>= 1, regno++)
948     if (mask & 1)
949       {
950         addr = plus_constant (base, offset);
951         operands[!store_p] = gen_frame_mem (mode, addr);
952         operands[store_p] = gen_rtx_REG (mode, regno);
953         XVECEXP (body, 0, i++)
954           = gen_rtx_SET (VOIDmode, operands[0], operands[1]);
955         offset += GET_MODE_SIZE (mode);
956       }
957   gcc_assert (i == XVECLEN (body, 0));
958
959   return emit_insn (body);
960 }
961
962 /* Make INSN a frame-related instruction.  */
963
964 static void
965 m68k_set_frame_related (rtx insn)
966 {
967   rtx body;
968   int i;
969
970   RTX_FRAME_RELATED_P (insn) = 1;
971   body = PATTERN (insn);
972   if (GET_CODE (body) == PARALLEL)
973     for (i = 0; i < XVECLEN (body, 0); i++)
974       RTX_FRAME_RELATED_P (XVECEXP (body, 0, i)) = 1;
975 }
976
977 /* Emit RTL for the "prologue" define_expand.  */
978
979 void
980 m68k_expand_prologue (void)
981 {
982   HOST_WIDE_INT fsize_with_regs;
983   rtx limit, src, dest, insn;
984
985   m68k_compute_frame_layout ();
986
987   /* If the stack limit is a symbol, we can check it here,
988      before actually allocating the space.  */
989   if (current_function_limit_stack
990       && GET_CODE (stack_limit_rtx) == SYMBOL_REF)
991     {
992       limit = plus_constant (stack_limit_rtx, current_frame.size + 4);
993       if (!LEGITIMATE_CONSTANT_P (limit))
994         {
995           emit_move_insn (gen_rtx_REG (Pmode, D0_REG), limit);
996           limit = gen_rtx_REG (Pmode, D0_REG);
997         }
998       emit_insn (gen_cmpsi (stack_pointer_rtx, limit));
999       emit_insn (gen_conditional_trap (gen_rtx_LTU (VOIDmode,
1000                                                     cc0_rtx, const0_rtx),
1001                                        const1_rtx));
1002     }
1003
1004   fsize_with_regs = current_frame.size;
1005   if (TARGET_COLDFIRE)
1006     {
1007       /* ColdFire's move multiple instructions do not allow pre-decrement
1008          addressing.  Add the size of movem saves to the initial stack
1009          allocation instead.  */
1010       if (current_frame.reg_no >= MIN_MOVEM_REGS)
1011         fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1012       if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1013         fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
1014     }
1015
1016   if (frame_pointer_needed)
1017     {
1018       if (fsize_with_regs == 0 && TUNE_68040)
1019         {
1020           /* On the 68040, two separate moves are faster than link.w 0.  */
1021           dest = gen_frame_mem (Pmode,
1022                                 gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1023           m68k_set_frame_related (emit_move_insn (dest, frame_pointer_rtx));
1024           m68k_set_frame_related (emit_move_insn (frame_pointer_rtx,
1025                                                   stack_pointer_rtx));
1026         }
1027       else if (fsize_with_regs < 0x8000 || TARGET_68020)
1028         m68k_set_frame_related
1029           (emit_insn (gen_link (frame_pointer_rtx,
1030                                 GEN_INT (-4 - fsize_with_regs))));
1031       else
1032         {
1033           m68k_set_frame_related
1034             (emit_insn (gen_link (frame_pointer_rtx, GEN_INT (-4))));
1035           m68k_set_frame_related
1036             (emit_insn (gen_addsi3 (stack_pointer_rtx,
1037                                     stack_pointer_rtx,
1038                                     GEN_INT (-fsize_with_regs))));
1039         }
1040     }
1041   else if (fsize_with_regs != 0)
1042     m68k_set_frame_related
1043       (emit_insn (gen_addsi3 (stack_pointer_rtx,
1044                               stack_pointer_rtx,
1045                               GEN_INT (-fsize_with_regs))));
1046
1047   if (current_frame.fpu_mask)
1048     {
1049       gcc_assert (current_frame.fpu_no >= MIN_FMOVEM_REGS);
1050       if (TARGET_68881)
1051         m68k_set_frame_related
1052           (m68k_emit_movem (stack_pointer_rtx,
1053                             current_frame.fpu_no * -GET_MODE_SIZE (XFmode),
1054                             current_frame.fpu_no, FP0_REG,
1055                             current_frame.fpu_mask, true, true));
1056       else
1057         {
1058           int offset;
1059
1060           /* If we're using moveml to save the integer registers,
1061              the stack pointer will point to the bottom of the moveml
1062              save area.  Find the stack offset of the first FP register.  */
1063           if (current_frame.reg_no < MIN_MOVEM_REGS)
1064             offset = 0;
1065           else
1066             offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1067           m68k_set_frame_related
1068             (m68k_emit_movem (stack_pointer_rtx, offset,
1069                               current_frame.fpu_no, FP0_REG,
1070                               current_frame.fpu_mask, true, false));
1071         }
1072     }
1073
1074   /* If the stack limit is not a symbol, check it here.
1075      This has the disadvantage that it may be too late...  */
1076   if (current_function_limit_stack)
1077     {
1078       if (REG_P (stack_limit_rtx))
1079         {
1080           emit_insn (gen_cmpsi (stack_pointer_rtx, stack_limit_rtx));
1081           emit_insn (gen_conditional_trap (gen_rtx_LTU (VOIDmode,
1082                                                         cc0_rtx, const0_rtx),
1083                                            const1_rtx));
1084         }
1085       else if (GET_CODE (stack_limit_rtx) != SYMBOL_REF)
1086         warning (0, "stack limit expression is not supported");
1087     }
1088
1089   if (current_frame.reg_no < MIN_MOVEM_REGS)
1090     {
1091       /* Store each register separately in the same order moveml does.  */
1092       int i;
1093
1094       for (i = 16; i-- > 0; )
1095         if (current_frame.reg_mask & (1 << i))
1096           {
1097             src = gen_rtx_REG (SImode, D0_REG + i);
1098             dest = gen_frame_mem (SImode,
1099                                   gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx));
1100             m68k_set_frame_related (emit_insn (gen_movsi (dest, src)));
1101           }
1102     }
1103   else
1104     {
1105       if (TARGET_COLDFIRE)
1106         /* The required register save space has already been allocated.
1107            The first register should be stored at (%sp).  */
1108         m68k_set_frame_related
1109           (m68k_emit_movem (stack_pointer_rtx, 0,
1110                             current_frame.reg_no, D0_REG,
1111                             current_frame.reg_mask, true, false));
1112       else
1113         m68k_set_frame_related
1114           (m68k_emit_movem (stack_pointer_rtx,
1115                             current_frame.reg_no * -GET_MODE_SIZE (SImode),
1116                             current_frame.reg_no, D0_REG,
1117                             current_frame.reg_mask, true, true));
1118     }
1119
1120   if (flag_pic
1121       && !TARGET_SEP_DATA
1122       && current_function_uses_pic_offset_table)
1123     insn = emit_insn (gen_load_got (pic_offset_table_rtx));
1124 }
1125 \f
1126 /* Return true if a simple (return) instruction is sufficient for this
1127    instruction (i.e. if no epilogue is needed).  */
1128
1129 bool
1130 m68k_use_return_insn (void)
1131 {
1132   if (!reload_completed || frame_pointer_needed || get_frame_size () != 0)
1133     return false;
1134
1135   m68k_compute_frame_layout ();
1136   return current_frame.offset == 0;
1137 }
1138
1139 /* Emit RTL for the "epilogue" or "sibcall_epilogue" define_expand;
1140    SIBCALL_P says which.
1141
1142    The function epilogue should not depend on the current stack pointer!
1143    It should use the frame pointer only, if there is a frame pointer.
1144    This is mandatory because of alloca; we also take advantage of it to
1145    omit stack adjustments before returning.  */
1146
1147 void
1148 m68k_expand_epilogue (bool sibcall_p)
1149 {
1150   HOST_WIDE_INT fsize, fsize_with_regs;
1151   bool big, restore_from_sp;
1152
1153   m68k_compute_frame_layout ();
1154
1155   fsize = current_frame.size;
1156   big = false;
1157   restore_from_sp = false;
1158
1159   /* FIXME : current_function_is_leaf below is too strong.
1160      What we really need to know there is if there could be pending
1161      stack adjustment needed at that point.  */
1162   restore_from_sp = (!frame_pointer_needed
1163                      || (!current_function_calls_alloca
1164                          && current_function_is_leaf));
1165
1166   /* fsize_with_regs is the size we need to adjust the sp when
1167      popping the frame.  */
1168   fsize_with_regs = fsize;
1169   if (TARGET_COLDFIRE && restore_from_sp)
1170     {
1171       /* ColdFire's move multiple instructions do not allow post-increment
1172          addressing.  Add the size of movem loads to the final deallocation
1173          instead.  */
1174       if (current_frame.reg_no >= MIN_MOVEM_REGS)
1175         fsize_with_regs += current_frame.reg_no * GET_MODE_SIZE (SImode);
1176       if (current_frame.fpu_no >= MIN_FMOVEM_REGS)
1177         fsize_with_regs += current_frame.fpu_no * GET_MODE_SIZE (DFmode);
1178     }
1179
1180   if (current_frame.offset + fsize >= 0x8000
1181       && !restore_from_sp
1182       && (current_frame.reg_mask || current_frame.fpu_mask))
1183     {
1184       if (TARGET_COLDFIRE
1185           && (current_frame.reg_no >= MIN_MOVEM_REGS
1186               || current_frame.fpu_no >= MIN_FMOVEM_REGS))
1187         {
1188           /* ColdFire's move multiple instructions do not support the
1189              (d8,Ax,Xi) addressing mode, so we're as well using a normal
1190              stack-based restore.  */
1191           emit_move_insn (gen_rtx_REG (Pmode, A1_REG),
1192                           GEN_INT (-(current_frame.offset + fsize)));
1193           emit_insn (gen_addsi3 (stack_pointer_rtx,
1194                                  gen_rtx_REG (Pmode, A1_REG),
1195                                  frame_pointer_rtx));
1196           restore_from_sp = true;
1197         }
1198       else
1199         {
1200           emit_move_insn (gen_rtx_REG (Pmode, A1_REG), GEN_INT (-fsize));
1201           fsize = 0;
1202           big = true;
1203         }
1204     }
1205
1206   if (current_frame.reg_no < MIN_MOVEM_REGS)
1207     {
1208       /* Restore each register separately in the same order moveml does.  */
1209       int i;
1210       HOST_WIDE_INT offset;
1211
1212       offset = current_frame.offset + fsize;
1213       for (i = 0; i < 16; i++)
1214         if (current_frame.reg_mask & (1 << i))
1215           {
1216             rtx addr;
1217
1218             if (big)
1219               {
1220                 /* Generate the address -OFFSET(%fp,%a1.l).  */
1221                 addr = gen_rtx_REG (Pmode, A1_REG);
1222                 addr = gen_rtx_PLUS (Pmode, addr, frame_pointer_rtx);
1223                 addr = plus_constant (addr, -offset);
1224               }
1225             else if (restore_from_sp)
1226               addr = gen_rtx_POST_INC (Pmode, stack_pointer_rtx);
1227             else
1228               addr = plus_constant (frame_pointer_rtx, -offset);
1229             emit_move_insn (gen_rtx_REG (SImode, D0_REG + i),
1230                             gen_frame_mem (SImode, addr));
1231             offset -= GET_MODE_SIZE (SImode);
1232           }
1233     }
1234   else if (current_frame.reg_mask)
1235     {
1236       if (big)
1237         m68k_emit_movem (gen_rtx_PLUS (Pmode,
1238                                        gen_rtx_REG (Pmode, A1_REG),
1239                                        frame_pointer_rtx),
1240                          -(current_frame.offset + fsize),
1241                          current_frame.reg_no, D0_REG,
1242                          current_frame.reg_mask, false, false);
1243       else if (restore_from_sp)
1244         m68k_emit_movem (stack_pointer_rtx, 0,
1245                          current_frame.reg_no, D0_REG,
1246                          current_frame.reg_mask, false,
1247                          !TARGET_COLDFIRE);
1248       else
1249         m68k_emit_movem (frame_pointer_rtx,
1250                          -(current_frame.offset + fsize),
1251                          current_frame.reg_no, D0_REG,
1252                          current_frame.reg_mask, false, false);
1253     }
1254
1255   if (current_frame.fpu_no > 0)
1256     {
1257       if (big)
1258         m68k_emit_movem (gen_rtx_PLUS (Pmode,
1259                                        gen_rtx_REG (Pmode, A1_REG),
1260                                        frame_pointer_rtx),
1261                          -(current_frame.foffset + fsize),
1262                          current_frame.fpu_no, FP0_REG,
1263                          current_frame.fpu_mask, false, false);
1264       else if (restore_from_sp)
1265         {
1266           if (TARGET_COLDFIRE)
1267             {
1268               int offset;
1269
1270               /* If we used moveml to restore the integer registers, the
1271                  stack pointer will still point to the bottom of the moveml
1272                  save area.  Find the stack offset of the first FP
1273                  register.  */
1274               if (current_frame.reg_no < MIN_MOVEM_REGS)
1275                 offset = 0;
1276               else
1277                 offset = current_frame.reg_no * GET_MODE_SIZE (SImode);
1278               m68k_emit_movem (stack_pointer_rtx, offset,
1279                                current_frame.fpu_no, FP0_REG,
1280                                current_frame.fpu_mask, false, false);
1281             }
1282           else
1283             m68k_emit_movem (stack_pointer_rtx, 0,
1284                              current_frame.fpu_no, FP0_REG,
1285                              current_frame.fpu_mask, false, true);
1286         }
1287       else
1288         m68k_emit_movem (frame_pointer_rtx,
1289                          -(current_frame.foffset + fsize),
1290                          current_frame.fpu_no, FP0_REG,
1291                          current_frame.fpu_mask, false, false);
1292     }
1293
1294   if (frame_pointer_needed)
1295     emit_insn (gen_unlink (frame_pointer_rtx));
1296   else if (fsize_with_regs)
1297     emit_insn (gen_addsi3 (stack_pointer_rtx,
1298                            stack_pointer_rtx,
1299                            GEN_INT (fsize_with_regs)));
1300
1301   if (current_function_calls_eh_return)
1302     emit_insn (gen_addsi3 (stack_pointer_rtx,
1303                            stack_pointer_rtx,
1304                            EH_RETURN_STACKADJ_RTX));
1305
1306   if (!sibcall_p)
1307     emit_jump_insn (gen_rtx_RETURN (VOIDmode));
1308 }
1309 \f
1310 /* Return true if X is a valid comparison operator for the dbcc 
1311    instruction.  
1312
1313    Note it rejects floating point comparison operators.
1314    (In the future we could use Fdbcc).
1315
1316    It also rejects some comparisons when CC_NO_OVERFLOW is set.  */
1317    
1318 int
1319 valid_dbcc_comparison_p_2 (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1320 {
1321   switch (GET_CODE (x))
1322     {
1323       case EQ: case NE: case GTU: case LTU:
1324       case GEU: case LEU:
1325         return 1;
1326
1327       /* Reject some when CC_NO_OVERFLOW is set.  This may be over
1328          conservative */
1329       case GT: case LT: case GE: case LE:
1330         return ! (cc_prev_status.flags & CC_NO_OVERFLOW);
1331       default:
1332         return 0;
1333     }
1334 }
1335
1336 /* Return nonzero if flags are currently in the 68881 flag register.  */
1337 int
1338 flags_in_68881 (void)
1339 {
1340   /* We could add support for these in the future */
1341   return cc_status.flags & CC_IN_68881;
1342 }
1343
1344 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL_P.  */
1345
1346 static bool
1347 m68k_ok_for_sibcall_p (tree decl, tree exp)
1348 {
1349   enum m68k_function_kind kind;
1350   
1351   /* We cannot use sibcalls for nested functions because we use the
1352      static chain register for indirect calls.  */
1353   if (CALL_EXPR_STATIC_CHAIN (exp))
1354     return false;
1355
1356   kind = m68k_get_function_kind (current_function_decl);
1357   if (kind == m68k_fk_normal_function)
1358     /* We can always sibcall from a normal function, because it's
1359        undefined if it is calling an interrupt function.  */
1360     return true;
1361
1362   /* Otherwise we can only sibcall if the function kind is known to be
1363      the same.  */
1364   if (decl && m68k_get_function_kind (decl) == kind)
1365     return true;
1366   
1367   return false;
1368 }
1369
1370 /* Convert X to a legitimate function call memory reference and return the
1371    result.  */
1372
1373 rtx
1374 m68k_legitimize_call_address (rtx x)
1375 {
1376   gcc_assert (MEM_P (x));
1377   if (call_operand (XEXP (x, 0), VOIDmode))
1378     return x;
1379   return replace_equiv_address (x, force_reg (Pmode, XEXP (x, 0)));
1380 }
1381
1382 /* Likewise for sibling calls.  */
1383
1384 rtx
1385 m68k_legitimize_sibcall_address (rtx x)
1386 {
1387   gcc_assert (MEM_P (x));
1388   if (sibcall_operand (XEXP (x, 0), VOIDmode))
1389     return x;
1390
1391   emit_move_insn (gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM), XEXP (x, 0));
1392   return replace_equiv_address (x, gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM));
1393 }
1394
1395 /* Output a dbCC; jCC sequence.  Note we do not handle the 
1396    floating point version of this sequence (Fdbcc).  We also
1397    do not handle alternative conditions when CC_NO_OVERFLOW is
1398    set.  It is assumed that valid_dbcc_comparison_p and flags_in_68881 will
1399    kick those out before we get here.  */
1400
1401 void
1402 output_dbcc_and_branch (rtx *operands)
1403 {
1404   switch (GET_CODE (operands[3]))
1405     {
1406       case EQ:
1407         output_asm_insn ("dbeq %0,%l1\n\tjeq %l2", operands);
1408         break;
1409
1410       case NE:
1411         output_asm_insn ("dbne %0,%l1\n\tjne %l2", operands);
1412         break;
1413
1414       case GT:
1415         output_asm_insn ("dbgt %0,%l1\n\tjgt %l2", operands);
1416         break;
1417
1418       case GTU:
1419         output_asm_insn ("dbhi %0,%l1\n\tjhi %l2", operands);
1420         break;
1421
1422       case LT:
1423         output_asm_insn ("dblt %0,%l1\n\tjlt %l2", operands);
1424         break;
1425
1426       case LTU:
1427         output_asm_insn ("dbcs %0,%l1\n\tjcs %l2", operands);
1428         break;
1429
1430       case GE:
1431         output_asm_insn ("dbge %0,%l1\n\tjge %l2", operands);
1432         break;
1433
1434       case GEU:
1435         output_asm_insn ("dbcc %0,%l1\n\tjcc %l2", operands);
1436         break;
1437
1438       case LE:
1439         output_asm_insn ("dble %0,%l1\n\tjle %l2", operands);
1440         break;
1441
1442       case LEU:
1443         output_asm_insn ("dbls %0,%l1\n\tjls %l2", operands);
1444         break;
1445
1446       default:
1447         gcc_unreachable ();
1448     }
1449
1450   /* If the decrement is to be done in SImode, then we have
1451      to compensate for the fact that dbcc decrements in HImode.  */
1452   switch (GET_MODE (operands[0]))
1453     {
1454       case SImode:
1455         output_asm_insn ("clr%.w %0\n\tsubq%.l #1,%0\n\tjpl %l1", operands);
1456         break;
1457
1458       case HImode:
1459         break;
1460
1461       default:
1462         gcc_unreachable ();
1463     }
1464 }
1465
1466 const char *
1467 output_scc_di (rtx op, rtx operand1, rtx operand2, rtx dest)
1468 {
1469   rtx loperands[7];
1470   enum rtx_code op_code = GET_CODE (op);
1471
1472   /* This does not produce a useful cc.  */
1473   CC_STATUS_INIT;
1474
1475   /* The m68k cmp.l instruction requires operand1 to be a reg as used
1476      below.  Swap the operands and change the op if these requirements
1477      are not fulfilled.  */
1478   if (GET_CODE (operand2) == REG && GET_CODE (operand1) != REG)
1479     {
1480       rtx tmp = operand1;
1481
1482       operand1 = operand2;
1483       operand2 = tmp;
1484       op_code = swap_condition (op_code);
1485     }
1486   loperands[0] = operand1;
1487   if (GET_CODE (operand1) == REG)
1488     loperands[1] = gen_rtx_REG (SImode, REGNO (operand1) + 1);
1489   else
1490     loperands[1] = adjust_address (operand1, SImode, 4);
1491   if (operand2 != const0_rtx)
1492     {
1493       loperands[2] = operand2;
1494       if (GET_CODE (operand2) == REG)
1495         loperands[3] = gen_rtx_REG (SImode, REGNO (operand2) + 1);
1496       else
1497         loperands[3] = adjust_address (operand2, SImode, 4);
1498     }
1499   loperands[4] = gen_label_rtx ();
1500   if (operand2 != const0_rtx)
1501     output_asm_insn ("cmp%.l %2,%0\n\tjne %l4\n\tcmp%.l %3,%1", loperands);
1502   else
1503     {
1504       if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[0]))
1505         output_asm_insn ("tst%.l %0", loperands);
1506       else
1507         output_asm_insn ("cmp%.w #0,%0", loperands);
1508
1509       output_asm_insn ("jne %l4", loperands);
1510
1511       if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[1]))
1512         output_asm_insn ("tst%.l %1", loperands);
1513       else
1514         output_asm_insn ("cmp%.w #0,%1", loperands);
1515     }
1516
1517   loperands[5] = dest;
1518
1519   switch (op_code)
1520     {
1521       case EQ:
1522         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1523                                            CODE_LABEL_NUMBER (loperands[4]));
1524         output_asm_insn ("seq %5", loperands);
1525         break;
1526
1527       case NE:
1528         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1529                                            CODE_LABEL_NUMBER (loperands[4]));
1530         output_asm_insn ("sne %5", loperands);
1531         break;
1532
1533       case GT:
1534         loperands[6] = gen_label_rtx ();
1535         output_asm_insn ("shi %5\n\tjra %l6", loperands);
1536         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1537                                            CODE_LABEL_NUMBER (loperands[4]));
1538         output_asm_insn ("sgt %5", loperands);
1539         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1540                                            CODE_LABEL_NUMBER (loperands[6]));
1541         break;
1542
1543       case GTU:
1544         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1545                                            CODE_LABEL_NUMBER (loperands[4]));
1546         output_asm_insn ("shi %5", loperands);
1547         break;
1548
1549       case LT:
1550         loperands[6] = gen_label_rtx ();
1551         output_asm_insn ("scs %5\n\tjra %l6", loperands);
1552         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1553                                            CODE_LABEL_NUMBER (loperands[4]));
1554         output_asm_insn ("slt %5", loperands);
1555         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1556                                            CODE_LABEL_NUMBER (loperands[6]));
1557         break;
1558
1559       case LTU:
1560         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1561                                            CODE_LABEL_NUMBER (loperands[4]));
1562         output_asm_insn ("scs %5", loperands);
1563         break;
1564
1565       case GE:
1566         loperands[6] = gen_label_rtx ();
1567         output_asm_insn ("scc %5\n\tjra %l6", loperands);
1568         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1569                                            CODE_LABEL_NUMBER (loperands[4]));
1570         output_asm_insn ("sge %5", loperands);
1571         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1572                                            CODE_LABEL_NUMBER (loperands[6]));
1573         break;
1574
1575       case GEU:
1576         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1577                                            CODE_LABEL_NUMBER (loperands[4]));
1578         output_asm_insn ("scc %5", loperands);
1579         break;
1580
1581       case LE:
1582         loperands[6] = gen_label_rtx ();
1583         output_asm_insn ("sls %5\n\tjra %l6", loperands);
1584         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1585                                            CODE_LABEL_NUMBER (loperands[4]));
1586         output_asm_insn ("sle %5", loperands);
1587         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1588                                            CODE_LABEL_NUMBER (loperands[6]));
1589         break;
1590
1591       case LEU:
1592         (*targetm.asm_out.internal_label) (asm_out_file, "L",
1593                                            CODE_LABEL_NUMBER (loperands[4]));
1594         output_asm_insn ("sls %5", loperands);
1595         break;
1596
1597       default:
1598         gcc_unreachable ();
1599     }
1600   return "";
1601 }
1602
1603 const char *
1604 output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
1605 {
1606   operands[0] = countop;
1607   operands[1] = dataop;
1608
1609   if (GET_CODE (countop) == CONST_INT)
1610     {
1611       register int count = INTVAL (countop);
1612       /* If COUNT is bigger than size of storage unit in use,
1613          advance to the containing unit of same size.  */
1614       if (count > signpos)
1615         {
1616           int offset = (count & ~signpos) / 8;
1617           count = count & signpos;
1618           operands[1] = dataop = adjust_address (dataop, QImode, offset);
1619         }
1620       if (count == signpos)
1621         cc_status.flags = CC_NOT_POSITIVE | CC_Z_IN_NOT_N;
1622       else
1623         cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N;
1624
1625       /* These three statements used to use next_insns_test_no...
1626          but it appears that this should do the same job.  */
1627       if (count == 31
1628           && next_insn_tests_no_inequality (insn))
1629         return "tst%.l %1";
1630       if (count == 15
1631           && next_insn_tests_no_inequality (insn))
1632         return "tst%.w %1";
1633       if (count == 7
1634           && next_insn_tests_no_inequality (insn))
1635         return "tst%.b %1";
1636       /* Try to use `movew to ccr' followed by the appropriate branch insn.
1637          On some m68k variants unfortunately that's slower than btst.
1638          On 68000 and higher, that should also work for all HImode operands. */
1639       if (TUNE_CPU32 || TARGET_COLDFIRE || optimize_size)
1640         {
1641           if (count == 3 && DATA_REG_P (operands[1])
1642               && next_insn_tests_no_inequality (insn))
1643             {
1644             cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N | CC_NO_OVERFLOW;
1645             return "move%.w %1,%%ccr";
1646             }
1647           if (count == 2 && DATA_REG_P (operands[1])
1648               && next_insn_tests_no_inequality (insn))
1649             {
1650             cc_status.flags = CC_NOT_NEGATIVE | CC_INVERTED | CC_NO_OVERFLOW;
1651             return "move%.w %1,%%ccr";
1652             }
1653           /* count == 1 followed by bvc/bvs and
1654              count == 0 followed by bcc/bcs are also possible, but need
1655              m68k-specific CC_Z_IN_NOT_V and CC_Z_IN_NOT_C flags. */
1656         }
1657
1658       cc_status.flags = CC_NOT_NEGATIVE;
1659     }
1660   return "btst %0,%1";
1661 }
1662 \f
1663 /* Return true if X is a legitimate base register.  STRICT_P says
1664    whether we need strict checking.  */
1665
1666 bool
1667 m68k_legitimate_base_reg_p (rtx x, bool strict_p)
1668 {
1669   /* Allow SUBREG everywhere we allow REG.  This results in better code.  */
1670   if (!strict_p && GET_CODE (x) == SUBREG)
1671     x = SUBREG_REG (x);
1672
1673   return (REG_P (x)
1674           && (strict_p
1675               ? REGNO_OK_FOR_BASE_P (REGNO (x))
1676               : REGNO_OK_FOR_BASE_NONSTRICT_P (REGNO (x))));
1677 }
1678
1679 /* Return true if X is a legitimate index register.  STRICT_P says
1680    whether we need strict checking.  */
1681
1682 bool
1683 m68k_legitimate_index_reg_p (rtx x, bool strict_p)
1684 {
1685   if (!strict_p && GET_CODE (x) == SUBREG)
1686     x = SUBREG_REG (x);
1687
1688   return (REG_P (x)
1689           && (strict_p
1690               ? REGNO_OK_FOR_INDEX_P (REGNO (x))
1691               : REGNO_OK_FOR_INDEX_NONSTRICT_P (REGNO (x))));
1692 }
1693
1694 /* Return true if X is a legitimate index expression for a (d8,An,Xn) or
1695    (bd,An,Xn) addressing mode.  Fill in the INDEX and SCALE fields of
1696    ADDRESS if so.  STRICT_P says whether we need strict checking.  */
1697
1698 static bool
1699 m68k_decompose_index (rtx x, bool strict_p, struct m68k_address *address)
1700 {
1701   int scale;
1702
1703   /* Check for a scale factor.  */
1704   scale = 1;
1705   if ((TARGET_68020 || TARGET_COLDFIRE)
1706       && GET_CODE (x) == MULT
1707       && GET_CODE (XEXP (x, 1)) == CONST_INT
1708       && (INTVAL (XEXP (x, 1)) == 2
1709           || INTVAL (XEXP (x, 1)) == 4
1710           || (INTVAL (XEXP (x, 1)) == 8
1711               && (TARGET_COLDFIRE_FPU || !TARGET_COLDFIRE))))
1712     {
1713       scale = INTVAL (XEXP (x, 1));
1714       x = XEXP (x, 0);
1715     }
1716
1717   /* Check for a word extension.  */
1718   if (!TARGET_COLDFIRE
1719       && GET_CODE (x) == SIGN_EXTEND
1720       && GET_MODE (XEXP (x, 0)) == HImode)
1721     x = XEXP (x, 0);
1722
1723   if (m68k_legitimate_index_reg_p (x, strict_p))
1724     {
1725       address->scale = scale;
1726       address->index = x;
1727       return true;
1728     }
1729
1730   return false;
1731 }
1732
1733 /* Return true if X is an illegitimate symbolic constant.  */
1734
1735 bool
1736 m68k_illegitimate_symbolic_constant_p (rtx x)
1737 {
1738   rtx base, offset;
1739
1740   if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
1741     {
1742       split_const (x, &base, &offset);
1743       if (GET_CODE (base) == SYMBOL_REF
1744           && !offset_within_block_p (base, INTVAL (offset)))
1745         return true;
1746     }
1747   return false;
1748 }
1749
1750 /* Return true if X is a legitimate constant address that can reach
1751    bytes in the range [X, X + REACH).  STRICT_P says whether we need
1752    strict checking.  */
1753
1754 static bool
1755 m68k_legitimate_constant_address_p (rtx x, unsigned int reach, bool strict_p)
1756 {
1757   rtx base, offset;
1758
1759   if (!CONSTANT_ADDRESS_P (x))
1760     return false;
1761
1762   if (flag_pic
1763       && !(strict_p && TARGET_PCREL)
1764       && symbolic_operand (x, VOIDmode))
1765     return false;
1766
1767   if (M68K_OFFSETS_MUST_BE_WITHIN_SECTIONS_P && reach > 1)
1768     {
1769       split_const (x, &base, &offset);
1770       if (GET_CODE (base) == SYMBOL_REF
1771           && !offset_within_block_p (base, INTVAL (offset) + reach - 1))
1772         return false;
1773     }
1774
1775   return true;
1776 }
1777
1778 /* Return true if X is a LABEL_REF for a jump table.  Assume that unplaced
1779    labels will become jump tables.  */
1780
1781 static bool
1782 m68k_jump_table_ref_p (rtx x)
1783 {
1784   if (GET_CODE (x) != LABEL_REF)
1785     return false;
1786
1787   x = XEXP (x, 0);
1788   if (!NEXT_INSN (x) && !PREV_INSN (x))
1789     return true;
1790
1791   x = next_nonnote_insn (x);
1792   return x && JUMP_TABLE_DATA_P (x);
1793 }
1794
1795 /* Return true if X is a legitimate address for values of mode MODE.
1796    STRICT_P says whether strict checking is needed.  If the address
1797    is valid, describe its components in *ADDRESS.  */
1798
1799 static bool
1800 m68k_decompose_address (enum machine_mode mode, rtx x,
1801                         bool strict_p, struct m68k_address *address)
1802 {
1803   unsigned int reach;
1804
1805   memset (address, 0, sizeof (*address));
1806
1807   if (mode == BLKmode)
1808     reach = 1;
1809   else
1810     reach = GET_MODE_SIZE (mode);
1811
1812   /* Check for (An) (mode 2).  */
1813   if (m68k_legitimate_base_reg_p (x, strict_p))
1814     {
1815       address->base = x;
1816       return true;
1817     }
1818
1819   /* Check for -(An) and (An)+ (modes 3 and 4).  */
1820   if ((GET_CODE (x) == PRE_DEC || GET_CODE (x) == POST_INC)
1821       && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
1822     {
1823       address->code = GET_CODE (x);
1824       address->base = XEXP (x, 0);
1825       return true;
1826     }
1827
1828   /* Check for (d16,An) (mode 5).  */
1829   if (GET_CODE (x) == PLUS
1830       && GET_CODE (XEXP (x, 1)) == CONST_INT
1831       && IN_RANGE (INTVAL (XEXP (x, 1)), -0x8000, 0x8000 - reach)
1832       && m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p))
1833     {
1834       address->base = XEXP (x, 0);
1835       address->offset = XEXP (x, 1);
1836       return true;
1837     }
1838
1839   /* Check for GOT loads.  These are (bd,An,Xn) addresses if
1840      TARGET_68020 && flag_pic == 2, otherwise they are (d16,An)
1841      addresses.  */
1842   if (flag_pic
1843       && GET_CODE (x) == PLUS
1844       && XEXP (x, 0) == pic_offset_table_rtx
1845       && (GET_CODE (XEXP (x, 1)) == SYMBOL_REF
1846           || GET_CODE (XEXP (x, 1)) == LABEL_REF))
1847     {
1848       address->base = XEXP (x, 0);
1849       address->offset = XEXP (x, 1);
1850       return true;
1851     }
1852
1853   /* The ColdFire FPU only accepts addressing modes 2-5.  */
1854   if (TARGET_COLDFIRE_FPU && GET_MODE_CLASS (mode) == MODE_FLOAT)
1855     return false;
1856
1857   /* Check for (xxx).w and (xxx).l.  Also, in the TARGET_PCREL case,
1858      check for (d16,PC) or (bd,PC,Xn) with a suppressed index register.
1859      All these modes are variations of mode 7.  */
1860   if (m68k_legitimate_constant_address_p (x, reach, strict_p))
1861     {
1862       address->offset = x;
1863       return true;
1864     }
1865
1866   /* Check for (d8,PC,Xn), a mode 7 form.  This case is needed for
1867      tablejumps.
1868
1869      ??? do_tablejump creates these addresses before placing the target
1870      label, so we have to assume that unplaced labels are jump table
1871      references.  It seems unlikely that we would ever generate indexed
1872      accesses to unplaced labels in other cases.  */
1873   if (GET_CODE (x) == PLUS
1874       && m68k_jump_table_ref_p (XEXP (x, 1))
1875       && m68k_decompose_index (XEXP (x, 0), strict_p, address))
1876     {
1877       address->offset = XEXP (x, 1);
1878       return true;
1879     }
1880
1881   /* Everything hereafter deals with (d8,An,Xn.SIZE*SCALE) or
1882      (bd,An,Xn.SIZE*SCALE) addresses.  */
1883
1884   if (TARGET_68020)
1885     {
1886       /* Check for a nonzero base displacement.  */
1887       if (GET_CODE (x) == PLUS
1888           && m68k_legitimate_constant_address_p (XEXP (x, 1), reach, strict_p))
1889         {
1890           address->offset = XEXP (x, 1);
1891           x = XEXP (x, 0);
1892         }
1893
1894       /* Check for a suppressed index register.  */
1895       if (m68k_legitimate_base_reg_p (x, strict_p))
1896         {
1897           address->base = x;
1898           return true;
1899         }
1900
1901       /* Check for a suppressed base register.  Do not allow this case
1902          for non-symbolic offsets as it effectively gives gcc freedom
1903          to treat data registers as base registers, which can generate
1904          worse code.  */
1905       if (address->offset
1906           && symbolic_operand (address->offset, VOIDmode)
1907           && m68k_decompose_index (x, strict_p, address))
1908         return true;
1909     }
1910   else
1911     {
1912       /* Check for a nonzero base displacement.  */
1913       if (GET_CODE (x) == PLUS
1914           && GET_CODE (XEXP (x, 1)) == CONST_INT
1915           && IN_RANGE (INTVAL (XEXP (x, 1)), -0x80, 0x80 - reach))
1916         {
1917           address->offset = XEXP (x, 1);
1918           x = XEXP (x, 0);
1919         }
1920     }
1921
1922   /* We now expect the sum of a base and an index.  */
1923   if (GET_CODE (x) == PLUS)
1924     {
1925       if (m68k_legitimate_base_reg_p (XEXP (x, 0), strict_p)
1926           && m68k_decompose_index (XEXP (x, 1), strict_p, address))
1927         {
1928           address->base = XEXP (x, 0);
1929           return true;
1930         }
1931
1932       if (m68k_legitimate_base_reg_p (XEXP (x, 1), strict_p)
1933           && m68k_decompose_index (XEXP (x, 0), strict_p, address))
1934         {
1935           address->base = XEXP (x, 1);
1936           return true;
1937         }
1938     }
1939   return false;
1940 }
1941
1942 /* Return true if X is a legitimate address for values of mode MODE.
1943    STRICT_P says whether strict checking is needed.  */
1944
1945 bool
1946 m68k_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
1947 {
1948   struct m68k_address address;
1949
1950   return m68k_decompose_address (mode, x, strict_p, &address);
1951 }
1952
1953 /* Return true if X is a memory, describing its address in ADDRESS if so.
1954    Apply strict checking if called during or after reload.  */
1955
1956 static bool
1957 m68k_legitimate_mem_p (rtx x, struct m68k_address *address)
1958 {
1959   return (MEM_P (x)
1960           && m68k_decompose_address (GET_MODE (x), XEXP (x, 0),
1961                                      reload_in_progress || reload_completed,
1962                                      address));
1963 }
1964
1965 /* Return true if X matches the 'Q' constraint.  It must be a memory
1966    with a base address and no constant offset or index.  */
1967
1968 bool
1969 m68k_matches_q_p (rtx x)
1970 {
1971   struct m68k_address address;
1972
1973   return (m68k_legitimate_mem_p (x, &address)
1974           && address.code == UNKNOWN
1975           && address.base
1976           && !address.offset
1977           && !address.index);
1978 }
1979
1980 /* Return true if X matches the 'U' constraint.  It must be a base address
1981    with a constant offset and no index.  */
1982
1983 bool
1984 m68k_matches_u_p (rtx x)
1985 {
1986   struct m68k_address address;
1987
1988   return (m68k_legitimate_mem_p (x, &address)
1989           && address.code == UNKNOWN
1990           && address.base
1991           && address.offset
1992           && !address.index);
1993 }
1994
1995 /* Legitimize PIC addresses.  If the address is already
1996    position-independent, we return ORIG.  Newly generated
1997    position-independent addresses go to REG.  If we need more
1998    than one register, we lose.  
1999
2000    An address is legitimized by making an indirect reference
2001    through the Global Offset Table with the name of the symbol
2002    used as an offset.  
2003
2004    The assembler and linker are responsible for placing the 
2005    address of the symbol in the GOT.  The function prologue
2006    is responsible for initializing a5 to the starting address
2007    of the GOT.
2008
2009    The assembler is also responsible for translating a symbol name
2010    into a constant displacement from the start of the GOT.  
2011
2012    A quick example may make things a little clearer:
2013
2014    When not generating PIC code to store the value 12345 into _foo
2015    we would generate the following code:
2016
2017         movel #12345, _foo
2018
2019    When generating PIC two transformations are made.  First, the compiler
2020    loads the address of foo into a register.  So the first transformation makes:
2021
2022         lea     _foo, a0
2023         movel   #12345, a0@
2024
2025    The code in movsi will intercept the lea instruction and call this
2026    routine which will transform the instructions into:
2027
2028         movel   a5@(_foo:w), a0
2029         movel   #12345, a0@
2030    
2031
2032    That (in a nutshell) is how *all* symbol and label references are 
2033    handled.  */
2034
2035 rtx
2036 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
2037                         rtx reg)
2038 {
2039   rtx pic_ref = orig;
2040
2041   /* First handle a simple SYMBOL_REF or LABEL_REF */
2042   if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
2043     {
2044       gcc_assert (reg);
2045
2046       pic_ref = gen_rtx_MEM (Pmode,
2047                              gen_rtx_PLUS (Pmode,
2048                                            pic_offset_table_rtx, orig));
2049       current_function_uses_pic_offset_table = 1;
2050       MEM_READONLY_P (pic_ref) = 1;
2051       emit_move_insn (reg, pic_ref);
2052       return reg;
2053     }
2054   else if (GET_CODE (orig) == CONST)
2055     {
2056       rtx base;
2057
2058       /* Make sure this has not already been legitimized.  */
2059       if (GET_CODE (XEXP (orig, 0)) == PLUS
2060           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2061         return orig;
2062
2063       gcc_assert (reg);
2064
2065       /* legitimize both operands of the PLUS */
2066       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
2067       
2068       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2069       orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2070                                      base == reg ? 0 : reg);
2071
2072       if (GET_CODE (orig) == CONST_INT)
2073         return plus_constant (base, INTVAL (orig));
2074       pic_ref = gen_rtx_PLUS (Pmode, base, orig);
2075       /* Likewise, should we set special REG_NOTEs here?  */
2076     }
2077   return pic_ref;
2078 }
2079
2080 \f
2081
2082 #define USE_MOVQ(i)     ((unsigned) ((i) + 128) <= 255)
2083
2084 /* Return the type of move that should be used for integer I.  */
2085
2086 M68K_CONST_METHOD
2087 m68k_const_method (HOST_WIDE_INT i)
2088 {
2089   unsigned u;
2090
2091   if (USE_MOVQ (i))
2092     return MOVQ;
2093
2094   /* The ColdFire doesn't have byte or word operations.  */
2095   /* FIXME: This may not be useful for the m68060 either.  */
2096   if (!TARGET_COLDFIRE)
2097     {
2098       /* if -256 < N < 256 but N is not in range for a moveq
2099          N^ff will be, so use moveq #N^ff, dreg; not.b dreg.  */
2100       if (USE_MOVQ (i ^ 0xff))
2101         return NOTB;
2102       /* Likewise, try with not.w */
2103       if (USE_MOVQ (i ^ 0xffff))
2104         return NOTW;
2105       /* This is the only value where neg.w is useful */
2106       if (i == -65408)
2107         return NEGW;
2108     }
2109
2110   /* Try also with swap.  */
2111   u = i;
2112   if (USE_MOVQ ((u >> 16) | (u << 16)))
2113     return SWAP;
2114
2115   if (TARGET_ISAB)
2116     {
2117       /* Try using MVZ/MVS with an immediate value to load constants.  */
2118       if (i >= 0 && i <= 65535)
2119         return MVZ;
2120       if (i >= -32768 && i <= 32767)
2121         return MVS;
2122     }
2123
2124   /* Otherwise, use move.l */
2125   return MOVL;
2126 }
2127
2128 /* Return the cost of moving constant I into a data register.  */
2129
2130 static int
2131 const_int_cost (HOST_WIDE_INT i)
2132 {
2133   switch (m68k_const_method (i))
2134     {
2135     case MOVQ:
2136       /* Constants between -128 and 127 are cheap due to moveq.  */
2137       return 0;
2138     case MVZ:
2139     case MVS:
2140     case NOTB:
2141     case NOTW:
2142     case NEGW:
2143     case SWAP:
2144       /* Constants easily generated by moveq + not.b/not.w/neg.w/swap.  */
2145       return 1;
2146     case MOVL:
2147       return 2;
2148     default:
2149       gcc_unreachable ();
2150     }
2151 }
2152
2153 static bool
2154 m68k_rtx_costs (rtx x, int code, int outer_code, int *total)
2155 {
2156   switch (code)
2157     {
2158     case CONST_INT:
2159       /* Constant zero is super cheap due to clr instruction.  */
2160       if (x == const0_rtx)
2161         *total = 0;
2162       else
2163         *total = const_int_cost (INTVAL (x));
2164       return true;
2165
2166     case CONST:
2167     case LABEL_REF:
2168     case SYMBOL_REF:
2169       *total = 3;
2170       return true;
2171
2172     case CONST_DOUBLE:
2173       /* Make 0.0 cheaper than other floating constants to
2174          encourage creating tstsf and tstdf insns.  */
2175       if (outer_code == COMPARE
2176           && (x == CONST0_RTX (SFmode) || x == CONST0_RTX (DFmode)))
2177         *total = 4;
2178       else
2179         *total = 5;
2180       return true;
2181
2182     /* These are vaguely right for a 68020.  */
2183     /* The costs for long multiply have been adjusted to work properly
2184        in synth_mult on the 68020, relative to an average of the time
2185        for add and the time for shift, taking away a little more because
2186        sometimes move insns are needed.  */
2187     /* div?.w is relatively cheaper on 68000 counted in COSTS_N_INSNS
2188        terms.  */
2189 #define MULL_COST                               \
2190   (TUNE_68060 ? 2                               \
2191    : TUNE_68040 ? 5                             \
2192    : TUNE_CFV2 ? 10                             \
2193    : TARGET_COLDFIRE ? 3 : 13)
2194
2195 #define MULW_COST                               \
2196   (TUNE_68060 ? 2                               \
2197    : TUNE_68040 ? 3                             \
2198    : TUNE_68000_10 || TUNE_CFV2 ? 5             \
2199    : TARGET_COLDFIRE ? 2 : 8)
2200
2201 #define DIVW_COST                               \
2202   (TARGET_CF_HWDIV ? 11                         \
2203    : TUNE_68000_10 || TARGET_COLDFIRE ? 12 : 27)
2204
2205     case PLUS:
2206       /* An lea costs about three times as much as a simple add.  */
2207       if (GET_MODE (x) == SImode
2208           && GET_CODE (XEXP (x, 1)) == REG
2209           && GET_CODE (XEXP (x, 0)) == MULT
2210           && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2211           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2212           && (INTVAL (XEXP (XEXP (x, 0), 1)) == 2
2213               || INTVAL (XEXP (XEXP (x, 0), 1)) == 4
2214               || INTVAL (XEXP (XEXP (x, 0), 1)) == 8))
2215         {
2216             /* lea an@(dx:l:i),am */
2217             *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 2 : 3);
2218             return true;
2219         }
2220       return false;
2221
2222     case ASHIFT:
2223     case ASHIFTRT:
2224     case LSHIFTRT:
2225       if (TUNE_68060)
2226         {
2227           *total = COSTS_N_INSNS(1);
2228           return true;
2229         }
2230       if (TUNE_68000_10)
2231         {
2232           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2233             {
2234               if (INTVAL (XEXP (x, 1)) < 16)
2235                 *total = COSTS_N_INSNS (2) + INTVAL (XEXP (x, 1)) / 2;
2236               else
2237                 /* We're using clrw + swap for these cases.  */
2238                 *total = COSTS_N_INSNS (4) + (INTVAL (XEXP (x, 1)) - 16) / 2;
2239             }
2240           else
2241             *total = COSTS_N_INSNS (10); /* Worst case.  */
2242           return true;
2243         }
2244       /* A shift by a big integer takes an extra instruction.  */
2245       if (GET_CODE (XEXP (x, 1)) == CONST_INT
2246           && (INTVAL (XEXP (x, 1)) == 16))
2247         {
2248           *total = COSTS_N_INSNS (2);    /* clrw;swap */
2249           return true;
2250         }
2251       if (GET_CODE (XEXP (x, 1)) == CONST_INT
2252           && !(INTVAL (XEXP (x, 1)) > 0
2253                && INTVAL (XEXP (x, 1)) <= 8))
2254         {
2255           *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 1 : 3);      /* lsr #i,dn */
2256           return true;
2257         }
2258       return false;
2259
2260     case MULT:
2261       if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2262            || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
2263           && GET_MODE (x) == SImode)
2264         *total = COSTS_N_INSNS (MULW_COST);
2265       else if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2266         *total = COSTS_N_INSNS (MULW_COST);
2267       else
2268         *total = COSTS_N_INSNS (MULL_COST);
2269       return true;
2270
2271     case DIV:
2272     case UDIV:
2273     case MOD:
2274     case UMOD:
2275       if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
2276         *total = COSTS_N_INSNS (DIVW_COST);     /* div.w */
2277       else if (TARGET_CF_HWDIV)
2278         *total = COSTS_N_INSNS (18);
2279       else
2280         *total = COSTS_N_INSNS (43);            /* div.l */
2281       return true;
2282
2283     default:
2284       return false;
2285     }
2286 }
2287
2288 /* Return an instruction to move CONST_INT OPERANDS[1] into data register
2289    OPERANDS[0].  */
2290
2291 static const char *
2292 output_move_const_into_data_reg (rtx *operands)
2293 {
2294   HOST_WIDE_INT i;
2295
2296   i = INTVAL (operands[1]);
2297   switch (m68k_const_method (i))
2298     {
2299     case MVZ:
2300       return "mvzw %1,%0";
2301     case MVS:
2302       return "mvsw %1,%0";
2303     case MOVQ:
2304       return "moveq %1,%0";
2305     case NOTB:
2306       CC_STATUS_INIT;
2307       operands[1] = GEN_INT (i ^ 0xff);
2308       return "moveq %1,%0\n\tnot%.b %0";
2309     case NOTW:
2310       CC_STATUS_INIT;
2311       operands[1] = GEN_INT (i ^ 0xffff);
2312       return "moveq %1,%0\n\tnot%.w %0";
2313     case NEGW:
2314       CC_STATUS_INIT;
2315       return "moveq #-128,%0\n\tneg%.w %0";
2316     case SWAP:
2317       {
2318         unsigned u = i;
2319
2320         operands[1] = GEN_INT ((u << 16) | (u >> 16));
2321         return "moveq %1,%0\n\tswap %0";
2322       }
2323     case MOVL:
2324       return "move%.l %1,%0";
2325     default:
2326       gcc_unreachable ();
2327     }
2328 }
2329
2330 /* Return true if I can be handled by ISA B's mov3q instruction.  */
2331
2332 bool
2333 valid_mov3q_const (HOST_WIDE_INT i)
2334 {
2335   return TARGET_ISAB && (i == -1 || IN_RANGE (i, 1, 7));
2336 }
2337
2338 /* Return an instruction to move CONST_INT OPERANDS[1] into OPERANDS[0].
2339    I is the value of OPERANDS[1].  */
2340
2341 static const char *
2342 output_move_simode_const (rtx *operands)
2343 {
2344   rtx dest;
2345   HOST_WIDE_INT src;
2346
2347   dest = operands[0];
2348   src = INTVAL (operands[1]);
2349   if (src == 0
2350       && (DATA_REG_P (dest) || MEM_P (dest))
2351       /* clr insns on 68000 read before writing.  */
2352       && ((TARGET_68010 || TARGET_COLDFIRE)
2353           || !(MEM_P (dest) && MEM_VOLATILE_P (dest))))
2354     return "clr%.l %0";
2355   else if (GET_MODE (dest) == SImode && valid_mov3q_const (src))
2356     return "mov3q%.l %1,%0";
2357   else if (src == 0 && ADDRESS_REG_P (dest))
2358     return "sub%.l %0,%0";
2359   else if (DATA_REG_P (dest))
2360     return output_move_const_into_data_reg (operands);
2361   else if (ADDRESS_REG_P (dest) && IN_RANGE (src, -0x8000, 0x7fff))
2362     {
2363       if (valid_mov3q_const (src))
2364         return "mov3q%.l %1,%0";
2365       return "move%.w %1,%0";
2366     }
2367   else if (MEM_P (dest)
2368            && GET_CODE (XEXP (dest, 0)) == PRE_DEC
2369            && REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2370            && IN_RANGE (src, -0x8000, 0x7fff))
2371     {
2372       if (valid_mov3q_const (src))
2373         return "mov3q%.l %1,%-";
2374       return "pea %a1";
2375     }
2376   return "move%.l %1,%0";
2377 }
2378
2379 const char *
2380 output_move_simode (rtx *operands)
2381 {
2382   if (GET_CODE (operands[1]) == CONST_INT)
2383     return output_move_simode_const (operands);
2384   else if ((GET_CODE (operands[1]) == SYMBOL_REF
2385             || GET_CODE (operands[1]) == CONST)
2386            && push_operand (operands[0], SImode))
2387     return "pea %a1";
2388   else if ((GET_CODE (operands[1]) == SYMBOL_REF
2389             || GET_CODE (operands[1]) == CONST)
2390            && ADDRESS_REG_P (operands[0]))
2391     return "lea %a1,%0";
2392   return "move%.l %1,%0";
2393 }
2394
2395 const char *
2396 output_move_himode (rtx *operands)
2397 {
2398  if (GET_CODE (operands[1]) == CONST_INT)
2399     {
2400       if (operands[1] == const0_rtx
2401           && (DATA_REG_P (operands[0])
2402               || GET_CODE (operands[0]) == MEM)
2403           /* clr insns on 68000 read before writing.  */
2404           && ((TARGET_68010 || TARGET_COLDFIRE)
2405               || !(GET_CODE (operands[0]) == MEM
2406                    && MEM_VOLATILE_P (operands[0]))))
2407         return "clr%.w %0";
2408       else if (operands[1] == const0_rtx
2409                && ADDRESS_REG_P (operands[0]))
2410         return "sub%.l %0,%0";
2411       else if (DATA_REG_P (operands[0])
2412                && INTVAL (operands[1]) < 128
2413                && INTVAL (operands[1]) >= -128)
2414         return "moveq %1,%0";
2415       else if (INTVAL (operands[1]) < 0x8000
2416                && INTVAL (operands[1]) >= -0x8000)
2417         return "move%.w %1,%0";
2418     }
2419   else if (CONSTANT_P (operands[1]))
2420     return "move%.l %1,%0";
2421   return "move%.w %1,%0";
2422 }
2423
2424 const char *
2425 output_move_qimode (rtx *operands)
2426 {
2427   /* 68k family always modifies the stack pointer by at least 2, even for
2428      byte pushes.  The 5200 (ColdFire) does not do this.  */
2429   
2430   /* This case is generated by pushqi1 pattern now.  */
2431   gcc_assert (!(GET_CODE (operands[0]) == MEM
2432                 && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
2433                 && XEXP (XEXP (operands[0], 0), 0) == stack_pointer_rtx
2434                 && ! ADDRESS_REG_P (operands[1])
2435                 && ! TARGET_COLDFIRE));
2436
2437   /* clr and st insns on 68000 read before writing.  */
2438   if (!ADDRESS_REG_P (operands[0])
2439       && ((TARGET_68010 || TARGET_COLDFIRE)
2440           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2441     {
2442       if (operands[1] == const0_rtx)
2443         return "clr%.b %0";
2444       if ((!TARGET_COLDFIRE || DATA_REG_P (operands[0]))
2445           && GET_CODE (operands[1]) == CONST_INT
2446           && (INTVAL (operands[1]) & 255) == 255)
2447         {
2448           CC_STATUS_INIT;
2449           return "st %0";
2450         }
2451     }
2452   if (GET_CODE (operands[1]) == CONST_INT
2453       && DATA_REG_P (operands[0])
2454       && INTVAL (operands[1]) < 128
2455       && INTVAL (operands[1]) >= -128)
2456     return "moveq %1,%0";
2457   if (operands[1] == const0_rtx && ADDRESS_REG_P (operands[0]))
2458     return "sub%.l %0,%0";
2459   if (GET_CODE (operands[1]) != CONST_INT && CONSTANT_P (operands[1]))
2460     return "move%.l %1,%0";
2461   /* 68k family (including the 5200 ColdFire) does not support byte moves to
2462      from address registers.  */
2463   if (ADDRESS_REG_P (operands[0]) || ADDRESS_REG_P (operands[1]))
2464     return "move%.w %1,%0";
2465   return "move%.b %1,%0";
2466 }
2467
2468 const char *
2469 output_move_stricthi (rtx *operands)
2470 {
2471   if (operands[1] == const0_rtx
2472       /* clr insns on 68000 read before writing.  */
2473       && ((TARGET_68010 || TARGET_COLDFIRE)
2474           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2475     return "clr%.w %0";
2476   return "move%.w %1,%0";
2477 }
2478
2479 const char *
2480 output_move_strictqi (rtx *operands)
2481 {
2482   if (operands[1] == const0_rtx
2483       /* clr insns on 68000 read before writing.  */
2484       && ((TARGET_68010 || TARGET_COLDFIRE)
2485           || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
2486     return "clr%.b %0";
2487   return "move%.b %1,%0";
2488 }
2489
2490 /* Return the best assembler insn template
2491    for moving operands[1] into operands[0] as a fullword.  */
2492
2493 static const char *
2494 singlemove_string (rtx *operands)
2495 {
2496   if (GET_CODE (operands[1]) == CONST_INT)
2497     return output_move_simode_const (operands);
2498   return "move%.l %1,%0";
2499 }
2500
2501
2502 /* Output assembler or rtl code to perform a doubleword move insn
2503    with operands OPERANDS.
2504    Pointers to 3 helper functions should be specified:
2505    HANDLE_REG_ADJUST to adjust a register by a small value,
2506    HANDLE_COMPADR to compute an address and
2507    HANDLE_MOVSI to move 4 bytes.  */
2508
2509 static void
2510 handle_move_double (rtx operands[2],
2511                     void (*handle_reg_adjust) (rtx, int),
2512                     void (*handle_compadr) (rtx [2]),
2513                     void (*handle_movsi) (rtx [2]))
2514 {
2515   enum
2516     {
2517       REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP
2518     } optype0, optype1;
2519   rtx latehalf[2];
2520   rtx middlehalf[2];
2521   rtx xops[2];
2522   rtx addreg0 = 0, addreg1 = 0;
2523   int dest_overlapped_low = 0;
2524   int size = GET_MODE_SIZE (GET_MODE (operands[0]));
2525
2526   middlehalf[0] = 0;
2527   middlehalf[1] = 0;
2528
2529   /* First classify both operands.  */
2530
2531   if (REG_P (operands[0]))
2532     optype0 = REGOP;
2533   else if (offsettable_memref_p (operands[0]))
2534     optype0 = OFFSOP;
2535   else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
2536     optype0 = POPOP;
2537   else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
2538     optype0 = PUSHOP;
2539   else if (GET_CODE (operands[0]) == MEM)
2540     optype0 = MEMOP;
2541   else
2542     optype0 = RNDOP;
2543
2544   if (REG_P (operands[1]))
2545     optype1 = REGOP;
2546   else if (CONSTANT_P (operands[1]))
2547     optype1 = CNSTOP;
2548   else if (offsettable_memref_p (operands[1]))
2549     optype1 = OFFSOP;
2550   else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
2551     optype1 = POPOP;
2552   else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
2553     optype1 = PUSHOP;
2554   else if (GET_CODE (operands[1]) == MEM)
2555     optype1 = MEMOP;
2556   else
2557     optype1 = RNDOP;
2558
2559   /* Check for the cases that the operand constraints are not supposed
2560      to allow to happen.  Generating code for these cases is
2561      painful.  */
2562   gcc_assert (optype0 != RNDOP && optype1 != RNDOP);
2563
2564   /* If one operand is decrementing and one is incrementing
2565      decrement the former register explicitly
2566      and change that operand into ordinary indexing.  */
2567
2568   if (optype0 == PUSHOP && optype1 == POPOP)
2569     {
2570       operands[0] = XEXP (XEXP (operands[0], 0), 0);
2571
2572       handle_reg_adjust (operands[0], -size);
2573
2574       if (GET_MODE (operands[1]) == XFmode)
2575         operands[0] = gen_rtx_MEM (XFmode, operands[0]);
2576       else if (GET_MODE (operands[0]) == DFmode)
2577         operands[0] = gen_rtx_MEM (DFmode, operands[0]);
2578       else
2579         operands[0] = gen_rtx_MEM (DImode, operands[0]);
2580       optype0 = OFFSOP;
2581     }
2582   if (optype0 == POPOP && optype1 == PUSHOP)
2583     {
2584       operands[1] = XEXP (XEXP (operands[1], 0), 0);
2585
2586       handle_reg_adjust (operands[1], -size);
2587
2588       if (GET_MODE (operands[1]) == XFmode)
2589         operands[1] = gen_rtx_MEM (XFmode, operands[1]);
2590       else if (GET_MODE (operands[1]) == DFmode)
2591         operands[1] = gen_rtx_MEM (DFmode, operands[1]);
2592       else
2593         operands[1] = gen_rtx_MEM (DImode, operands[1]);
2594       optype1 = OFFSOP;
2595     }
2596
2597   /* If an operand is an unoffsettable memory ref, find a register
2598      we can increment temporarily to make it refer to the second word.  */
2599
2600   if (optype0 == MEMOP)
2601     addreg0 = find_addr_reg (XEXP (operands[0], 0));
2602
2603   if (optype1 == MEMOP)
2604     addreg1 = find_addr_reg (XEXP (operands[1], 0));
2605
2606   /* Ok, we can do one word at a time.
2607      Normally we do the low-numbered word first,
2608      but if either operand is autodecrementing then we
2609      do the high-numbered word first.
2610
2611      In either case, set up in LATEHALF the operands to use
2612      for the high-numbered word and in some cases alter the
2613      operands in OPERANDS to be suitable for the low-numbered word.  */
2614
2615   if (size == 12)
2616     {
2617       if (optype0 == REGOP)
2618         {
2619           latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 2);
2620           middlehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2621         }
2622       else if (optype0 == OFFSOP)
2623         {
2624           middlehalf[0] = adjust_address (operands[0], SImode, 4);
2625           latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2626         }
2627       else
2628         {
2629           middlehalf[0] = adjust_address (operands[0], SImode, 0);
2630           latehalf[0] = adjust_address (operands[0], SImode, 0);
2631         }
2632
2633       if (optype1 == REGOP)
2634         {
2635           latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 2);
2636           middlehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2637         }
2638       else if (optype1 == OFFSOP)
2639         {
2640           middlehalf[1] = adjust_address (operands[1], SImode, 4);
2641           latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2642         }
2643       else if (optype1 == CNSTOP)
2644         {
2645           if (GET_CODE (operands[1]) == CONST_DOUBLE)
2646             {
2647               REAL_VALUE_TYPE r;
2648               long l[3];
2649
2650               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
2651               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
2652               operands[1] = GEN_INT (l[0]);
2653               middlehalf[1] = GEN_INT (l[1]);
2654               latehalf[1] = GEN_INT (l[2]);
2655             }
2656           else
2657             {
2658               /* No non-CONST_DOUBLE constant should ever appear
2659                  here.  */
2660               gcc_assert (!CONSTANT_P (operands[1]));
2661             }
2662         }
2663       else
2664         {
2665           middlehalf[1] = adjust_address (operands[1], SImode, 0);
2666           latehalf[1] = adjust_address (operands[1], SImode, 0);
2667         }
2668     }
2669   else
2670     /* size is not 12: */
2671     {
2672       if (optype0 == REGOP)
2673         latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2674       else if (optype0 == OFFSOP)
2675         latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2676       else
2677         latehalf[0] = adjust_address (operands[0], SImode, 0);
2678
2679       if (optype1 == REGOP)
2680         latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2681       else if (optype1 == OFFSOP)
2682         latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2683       else if (optype1 == CNSTOP)
2684         split_double (operands[1], &operands[1], &latehalf[1]);
2685       else
2686         latehalf[1] = adjust_address (operands[1], SImode, 0);
2687     }
2688
2689   /* If insn is effectively movd N(sp),-(sp) then we will do the
2690      high word first.  We should use the adjusted operand 1 (which is N+4(sp))
2691      for the low word as well, to compensate for the first decrement of sp.  */
2692   if (optype0 == PUSHOP
2693       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
2694       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
2695     operands[1] = middlehalf[1] = latehalf[1];
2696
2697   /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
2698      if the upper part of reg N does not appear in the MEM, arrange to
2699      emit the move late-half first.  Otherwise, compute the MEM address
2700      into the upper part of N and use that as a pointer to the memory
2701      operand.  */
2702   if (optype0 == REGOP
2703       && (optype1 == OFFSOP || optype1 == MEMOP))
2704     {
2705       rtx testlow = gen_rtx_REG (SImode, REGNO (operands[0]));
2706
2707       if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2708           && reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2709         {
2710           /* If both halves of dest are used in the src memory address,
2711              compute the address into latehalf of dest.
2712              Note that this can't happen if the dest is two data regs.  */
2713         compadr:
2714           xops[0] = latehalf[0];
2715           xops[1] = XEXP (operands[1], 0);
2716
2717           handle_compadr (xops);
2718           if (GET_MODE (operands[1]) == XFmode)
2719             {
2720               operands[1] = gen_rtx_MEM (XFmode, latehalf[0]);
2721               middlehalf[1] = adjust_address (operands[1], DImode, size - 8);
2722               latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2723             }
2724           else
2725             {
2726               operands[1] = gen_rtx_MEM (DImode, latehalf[0]);
2727               latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2728             }
2729         }
2730       else if (size == 12
2731                && reg_overlap_mentioned_p (middlehalf[0],
2732                                            XEXP (operands[1], 0)))
2733         {
2734           /* Check for two regs used by both source and dest.
2735              Note that this can't happen if the dest is all data regs.
2736              It can happen if the dest is d6, d7, a0.
2737              But in that case, latehalf is an addr reg, so
2738              the code at compadr does ok.  */
2739
2740           if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2741               || reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2742             goto compadr;
2743
2744           /* JRV says this can't happen: */
2745           gcc_assert (!addreg0 && !addreg1);
2746
2747           /* Only the middle reg conflicts; simply put it last.  */
2748           handle_movsi (operands);
2749           handle_movsi (latehalf);
2750           handle_movsi (middlehalf);
2751
2752           return;
2753         }
2754       else if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0)))
2755         /* If the low half of dest is mentioned in the source memory
2756            address, the arrange to emit the move late half first.  */
2757         dest_overlapped_low = 1;
2758     }
2759
2760   /* If one or both operands autodecrementing,
2761      do the two words, high-numbered first.  */
2762
2763   /* Likewise,  the first move would clobber the source of the second one,
2764      do them in the other order.  This happens only for registers;
2765      such overlap can't happen in memory unless the user explicitly
2766      sets it up, and that is an undefined circumstance.  */
2767
2768   if (optype0 == PUSHOP || optype1 == PUSHOP
2769       || (optype0 == REGOP && optype1 == REGOP
2770           && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
2771               || REGNO (operands[0]) == REGNO (latehalf[1])))
2772       || dest_overlapped_low)
2773     {
2774       /* Make any unoffsettable addresses point at high-numbered word.  */
2775       if (addreg0)
2776         handle_reg_adjust (addreg0, size - 4);
2777       if (addreg1)
2778         handle_reg_adjust (addreg1, size - 4);
2779
2780       /* Do that word.  */
2781       handle_movsi (latehalf);
2782
2783       /* Undo the adds we just did.  */
2784       if (addreg0)
2785         handle_reg_adjust (addreg0, -4);
2786       if (addreg1)
2787         handle_reg_adjust (addreg1, -4);
2788
2789       if (size == 12)
2790         {
2791           handle_movsi (middlehalf);
2792
2793           if (addreg0)
2794             handle_reg_adjust (addreg0, -4);
2795           if (addreg1)
2796             handle_reg_adjust (addreg1, -4);
2797         }
2798
2799       /* Do low-numbered word.  */
2800
2801       handle_movsi (operands);
2802       return;
2803     }
2804
2805   /* Normal case: do the two words, low-numbered first.  */
2806
2807   handle_movsi (operands);
2808
2809   /* Do the middle one of the three words for long double */
2810   if (size == 12)
2811     {
2812       if (addreg0)
2813         handle_reg_adjust (addreg0, 4);
2814       if (addreg1)
2815         handle_reg_adjust (addreg1, 4);
2816
2817       handle_movsi (middlehalf);
2818     }
2819
2820   /* Make any unoffsettable addresses point at high-numbered word.  */
2821   if (addreg0)
2822     handle_reg_adjust (addreg0, 4);
2823   if (addreg1)
2824     handle_reg_adjust (addreg1, 4);
2825
2826   /* Do that word.  */
2827   handle_movsi (latehalf);
2828
2829   /* Undo the adds we just did.  */
2830   if (addreg0)
2831     handle_reg_adjust (addreg0, -(size - 4));
2832   if (addreg1)
2833     handle_reg_adjust (addreg1, -(size - 4));
2834
2835   return;
2836 }
2837
2838 /* Output assembler code to adjust REG by N.  */
2839 static void
2840 output_reg_adjust (rtx reg, int n)
2841 {
2842   const char *s;
2843
2844   gcc_assert (GET_MODE (reg) == SImode
2845               && -12 <= n && n != 0 && n <= 12);
2846
2847   switch (n)
2848     {
2849     case 12:
2850       s = "add%.l #12,%0";
2851       break;
2852
2853     case 8:
2854       s = "addq%.l #8,%0";
2855       break;
2856
2857     case 4:
2858       s = "addq%.l #4,%0";
2859       break;
2860
2861     case -12:
2862       s = "sub%.l #12,%0";
2863       break;
2864
2865     case -8:
2866       s = "subq%.l #8,%0";
2867       break;
2868
2869     case -4:
2870       s = "subq%.l #4,%0";
2871       break;
2872
2873     default:
2874       gcc_unreachable ();
2875       s = NULL;
2876     }
2877
2878   output_asm_insn (s, &reg);
2879 }
2880
2881 /* Emit rtl code to adjust REG by N.  */
2882 static void
2883 emit_reg_adjust (rtx reg1, int n)
2884 {
2885   rtx reg2;
2886
2887   gcc_assert (GET_MODE (reg1) == SImode
2888               && -12 <= n && n != 0 && n <= 12);
2889
2890   reg1 = copy_rtx (reg1);
2891   reg2 = copy_rtx (reg1);
2892
2893   if (n < 0)
2894     emit_insn (gen_subsi3 (reg1, reg2, GEN_INT (-n)));
2895   else if (n > 0)
2896     emit_insn (gen_addsi3 (reg1, reg2, GEN_INT (n)));
2897   else
2898     gcc_unreachable ();
2899 }
2900
2901 /* Output assembler to load address OPERANDS[0] to register OPERANDS[1].  */
2902 static void
2903 output_compadr (rtx operands[2])
2904 {
2905   output_asm_insn ("lea %a1,%0", operands);
2906 }
2907
2908 /* Output the best assembler insn for moving operands[1] into operands[0]
2909    as a fullword.  */
2910 static void
2911 output_movsi (rtx operands[2])
2912 {
2913   output_asm_insn (singlemove_string (operands), operands);
2914 }
2915
2916 /* Copy OP and change its mode to MODE.  */
2917 static rtx
2918 copy_operand (rtx op, enum machine_mode mode)
2919 {
2920   /* ??? This looks really ugly.  There must be a better way
2921      to change a mode on the operand.  */
2922   if (GET_MODE (op) != VOIDmode)
2923     {
2924       if (REG_P (op))
2925         op = gen_rtx_REG (mode, REGNO (op));
2926       else
2927         {
2928           op = copy_rtx (op);
2929           PUT_MODE (op, mode);
2930         }
2931     }
2932
2933   return op;
2934 }
2935
2936 /* Emit rtl code for moving operands[1] into operands[0] as a fullword.  */
2937 static void
2938 emit_movsi (rtx operands[2])
2939 {
2940   operands[0] = copy_operand (operands[0], SImode);
2941   operands[1] = copy_operand (operands[1], SImode);
2942
2943   emit_insn (gen_movsi (operands[0], operands[1]));
2944 }
2945
2946 /* Output assembler code to perform a doubleword move insn
2947    with operands OPERANDS.  */
2948 const char *
2949 output_move_double (rtx *operands)
2950 {
2951   handle_move_double (operands,
2952                       output_reg_adjust, output_compadr, output_movsi);
2953
2954   return "";
2955 }
2956
2957 /* Output rtl code to perform a doubleword move insn
2958    with operands OPERANDS.  */
2959 void
2960 m68k_emit_move_double (rtx operands[2])
2961 {
2962   handle_move_double (operands, emit_reg_adjust, emit_movsi, emit_movsi);
2963 }
2964
2965 /* Ensure mode of ORIG, a REG rtx, is MODE.  Returns either ORIG or a
2966    new rtx with the correct mode.  */
2967
2968 static rtx
2969 force_mode (enum machine_mode mode, rtx orig)
2970 {
2971   if (mode == GET_MODE (orig))
2972     return orig;
2973
2974   if (REGNO (orig) >= FIRST_PSEUDO_REGISTER)
2975     abort ();
2976
2977   return gen_rtx_REG (mode, REGNO (orig));
2978 }
2979
2980 static int
2981 fp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2982 {
2983   return reg_renumber && FP_REG_P (op);
2984 }
2985
2986 /* Emit insns to move operands[1] into operands[0].
2987
2988    Return 1 if we have written out everything that needs to be done to
2989    do the move.  Otherwise, return 0 and the caller will emit the move
2990    normally.
2991
2992    Note SCRATCH_REG may not be in the proper mode depending on how it
2993    will be used.  This routine is responsible for creating a new copy
2994    of SCRATCH_REG in the proper mode.  */
2995
2996 int
2997 emit_move_sequence (rtx *operands, enum machine_mode mode, rtx scratch_reg)
2998 {
2999   register rtx operand0 = operands[0];
3000   register rtx operand1 = operands[1];
3001   register rtx tem;
3002
3003   if (scratch_reg
3004       && reload_in_progress && GET_CODE (operand0) == REG
3005       && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
3006     operand0 = reg_equiv_mem[REGNO (operand0)];
3007   else if (scratch_reg
3008            && reload_in_progress && GET_CODE (operand0) == SUBREG
3009            && GET_CODE (SUBREG_REG (operand0)) == REG
3010            && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
3011     {
3012      /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3013         the code which tracks sets/uses for delete_output_reload.  */
3014       rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
3015                                  reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
3016                                  SUBREG_BYTE (operand0));
3017       operand0 = alter_subreg (&temp);
3018     }
3019
3020   if (scratch_reg
3021       && reload_in_progress && GET_CODE (operand1) == REG
3022       && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
3023     operand1 = reg_equiv_mem[REGNO (operand1)];
3024   else if (scratch_reg
3025            && reload_in_progress && GET_CODE (operand1) == SUBREG
3026            && GET_CODE (SUBREG_REG (operand1)) == REG
3027            && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
3028     {
3029      /* We must not alter SUBREG_BYTE (operand0) since that would confuse
3030         the code which tracks sets/uses for delete_output_reload.  */
3031       rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
3032                                  reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
3033                                  SUBREG_BYTE (operand1));
3034       operand1 = alter_subreg (&temp);
3035     }
3036
3037   if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
3038       && ((tem = find_replacement (&XEXP (operand0, 0)))
3039           != XEXP (operand0, 0)))
3040     operand0 = gen_rtx_MEM (GET_MODE (operand0), tem);
3041   if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
3042       && ((tem = find_replacement (&XEXP (operand1, 0)))
3043           != XEXP (operand1, 0)))
3044     operand1 = gen_rtx_MEM (GET_MODE (operand1), tem);
3045
3046   /* Handle secondary reloads for loads/stores of FP registers where
3047      the address is symbolic by using the scratch register */
3048   if (fp_reg_operand (operand0, mode)
3049       && ((GET_CODE (operand1) == MEM
3050            && ! memory_address_p (DFmode, XEXP (operand1, 0)))
3051           || ((GET_CODE (operand1) == SUBREG
3052                && GET_CODE (XEXP (operand1, 0)) == MEM
3053                && !memory_address_p (DFmode, XEXP (XEXP (operand1, 0), 0)))))
3054       && scratch_reg)
3055     {
3056       if (GET_CODE (operand1) == SUBREG)
3057         operand1 = XEXP (operand1, 0);
3058
3059       /* SCRATCH_REG will hold an address.  We want
3060          it in SImode regardless of what mode it was originally given
3061          to us.  */
3062       scratch_reg = force_mode (SImode, scratch_reg);
3063
3064       /* D might not fit in 14 bits either; for such cases load D into
3065          scratch reg.  */
3066       if (!memory_address_p (Pmode, XEXP (operand1, 0)))
3067         {
3068           emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
3069           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
3070                                                        Pmode,
3071                                                        XEXP (XEXP (operand1, 0), 0),
3072                                                        scratch_reg));
3073         }
3074       else
3075         emit_move_insn (scratch_reg, XEXP (operand1, 0));
3076       emit_insn (gen_rtx_SET (VOIDmode, operand0,
3077                               gen_rtx_MEM (mode, scratch_reg)));
3078       return 1;
3079     }
3080   else if (fp_reg_operand (operand1, mode)
3081            && ((GET_CODE (operand0) == MEM
3082                 && ! memory_address_p (DFmode, XEXP (operand0, 0)))
3083                || ((GET_CODE (operand0) == SUBREG)
3084                    && GET_CODE (XEXP (operand0, 0)) == MEM
3085                    && !memory_address_p (DFmode, XEXP (XEXP (operand0, 0), 0))))
3086            && scratch_reg)
3087     {
3088       if (GET_CODE (operand0) == SUBREG)
3089         operand0 = XEXP (operand0, 0);
3090
3091       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
3092          it in SIMODE regardless of what mode it was originally given
3093          to us.  */
3094       scratch_reg = force_mode (SImode, scratch_reg);
3095
3096       /* D might not fit in 14 bits either; for such cases load D into
3097          scratch reg.  */
3098       if (!memory_address_p (Pmode, XEXP (operand0, 0)))
3099         {
3100           emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
3101           emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
3102                                                                         0)),
3103                                                        Pmode,
3104                                                        XEXP (XEXP (operand0, 0),
3105                                                                    0),
3106                                                        scratch_reg));
3107         }
3108       else
3109         emit_move_insn (scratch_reg, XEXP (operand0, 0));
3110       emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_MEM (mode, scratch_reg),
3111                               operand1));
3112       return 1;
3113     }
3114   /* Handle secondary reloads for loads of FP registers from constant
3115      expressions by forcing the constant into memory.
3116
3117      use scratch_reg to hold the address of the memory location.
3118
3119      The proper fix is to change PREFERRED_RELOAD_CLASS to return
3120      NO_REGS when presented with a const_int and an register class
3121      containing only FP registers.  Doing so unfortunately creates
3122      more problems than it solves.   Fix this for 2.5.  */
3123   else if (fp_reg_operand (operand0, mode)
3124            && CONSTANT_P (operand1)
3125            && scratch_reg)
3126     {
3127       rtx xoperands[2];
3128
3129       /* SCRATCH_REG will hold an address and maybe the actual data.  We want
3130          it in SIMODE regardless of what mode it was originally given
3131          to us.  */
3132       scratch_reg = force_mode (SImode, scratch_reg);
3133
3134       /* Force the constant into memory and put the address of the
3135          memory location into scratch_reg.  */
3136       xoperands[0] = scratch_reg;
3137       xoperands[1] = XEXP (force_const_mem (mode, operand1), 0);
3138       emit_insn (gen_rtx_SET (mode, scratch_reg, xoperands[1]));
3139
3140       /* Now load the destination register.  */
3141       emit_insn (gen_rtx_SET (mode, operand0,
3142                               gen_rtx_MEM (mode, scratch_reg)));
3143       return 1;
3144     }
3145
3146   /* Now have insn-emit do whatever it normally does.  */
3147   return 0;
3148 }
3149
3150 /* Split one or more DImode RTL references into pairs of SImode
3151    references.  The RTL can be REG, offsettable MEM, integer constant, or
3152    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
3153    split and "num" is its length.  lo_half and hi_half are output arrays
3154    that parallel "operands".  */
3155
3156 void
3157 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
3158 {
3159   while (num--)
3160     {
3161       rtx op = operands[num];
3162
3163       /* simplify_subreg refuses to split volatile memory addresses,
3164          but we still have to handle it.  */
3165       if (GET_CODE (op) == MEM)
3166         {
3167           lo_half[num] = adjust_address (op, SImode, 4);
3168           hi_half[num] = adjust_address (op, SImode, 0);
3169         }
3170       else
3171         {
3172           lo_half[num] = simplify_gen_subreg (SImode, op,
3173                                               GET_MODE (op) == VOIDmode
3174                                               ? DImode : GET_MODE (op), 4);
3175           hi_half[num] = simplify_gen_subreg (SImode, op,
3176                                               GET_MODE (op) == VOIDmode
3177                                               ? DImode : GET_MODE (op), 0);
3178         }
3179     }
3180 }
3181
3182 /* Split X into a base and a constant offset, storing them in *BASE
3183    and *OFFSET respectively.  */
3184
3185 static void
3186 m68k_split_offset (rtx x, rtx *base, HOST_WIDE_INT *offset)
3187 {
3188   *offset = 0;
3189   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
3190     {
3191       *offset += INTVAL (XEXP (x, 1));
3192       x = XEXP (x, 0);
3193     }
3194   *base = x;
3195 }
3196
3197 /* Return true if PATTERN is a PARALLEL suitable for a movem or fmovem
3198    instruction.  STORE_P says whether the move is a load or store.
3199
3200    If the instruction uses post-increment or pre-decrement addressing,
3201    AUTOMOD_BASE is the base register and AUTOMOD_OFFSET is the total
3202    adjustment.  This adjustment will be made by the first element of
3203    PARALLEL, with the loads or stores starting at element 1.  If the
3204    instruction does not use post-increment or pre-decrement addressing,
3205    AUTOMOD_BASE is null, AUTOMOD_OFFSET is 0, and the loads or stores
3206    start at element 0.  */
3207
3208 bool
3209 m68k_movem_pattern_p (rtx pattern, rtx automod_base,
3210                       HOST_WIDE_INT automod_offset, bool store_p)
3211 {
3212   rtx base, mem_base, set, mem, reg, last_reg;
3213   HOST_WIDE_INT offset, mem_offset;
3214   int i, first, len;
3215   enum reg_class rclass;
3216
3217   len = XVECLEN (pattern, 0);
3218   first = (automod_base != NULL);
3219
3220   if (automod_base)
3221     {
3222       /* Stores must be pre-decrement and loads must be post-increment.  */
3223       if (store_p != (automod_offset < 0))
3224         return false;
3225
3226       /* Work out the base and offset for lowest memory location.  */
3227       base = automod_base;
3228       offset = (automod_offset < 0 ? automod_offset : 0);
3229     }
3230   else
3231     {
3232       /* Allow any valid base and offset in the first access.  */
3233       base = NULL;
3234       offset = 0;
3235     }
3236
3237   last_reg = NULL;
3238   rclass = NO_REGS;
3239   for (i = first; i < len; i++)
3240     {
3241       /* We need a plain SET.  */
3242       set = XVECEXP (pattern, 0, i);
3243       if (GET_CODE (set) != SET)
3244         return false;
3245
3246       /* Check that we have a memory location...  */
3247       mem = XEXP (set, !store_p);
3248       if (!MEM_P (mem) || !memory_operand (mem, VOIDmode))
3249         return false;
3250
3251       /* ...with the right address.  */
3252       if (base == NULL)
3253         {
3254           m68k_split_offset (XEXP (mem, 0), &base, &offset);
3255           /* The ColdFire instruction only allows (An) and (d16,An) modes.
3256              There are no mode restrictions for 680x0 besides the
3257              automodification rules enforced above.  */
3258           if (TARGET_COLDFIRE
3259               && !m68k_legitimate_base_reg_p (base, reload_completed))
3260             return false;
3261         }
3262       else
3263         {
3264           m68k_split_offset (XEXP (mem, 0), &mem_base, &mem_offset);
3265           if (!rtx_equal_p (base, mem_base) || offset != mem_offset)
3266             return false;
3267         }
3268
3269       /* Check that we have a register of the required mode and class.  */
3270       reg = XEXP (set, store_p);
3271       if (!REG_P (reg)
3272           || !HARD_REGISTER_P (reg)
3273           || GET_MODE (reg) != reg_raw_mode[REGNO (reg)])
3274         return false;
3275
3276       if (last_reg)
3277         {
3278           /* The register must belong to RCLASS and have a higher number
3279              than the register in the previous SET.  */
3280           if (!TEST_HARD_REG_BIT (reg_class_contents[rclass], REGNO (reg))
3281               || REGNO (last_reg) >= REGNO (reg))
3282             return false;
3283         }
3284       else
3285         {
3286           /* Work out which register class we need.  */
3287           if (INT_REGNO_P (REGNO (reg)))
3288             rclass = GENERAL_REGS;
3289           else if (FP_REGNO_P (REGNO (reg)))
3290             rclass = FP_REGS;
3291           else
3292             return false;
3293         }
3294
3295       last_reg = reg;
3296       offset += GET_MODE_SIZE (GET_MODE (reg));
3297     }
3298
3299   /* If we have an automodification, check whether the final offset is OK.  */
3300   if (automod_base && offset != (automod_offset < 0 ? 0 : automod_offset))
3301     return false;
3302
3303   /* Reject unprofitable cases.  */
3304   if (len < first + (rclass == FP_REGS ? MIN_FMOVEM_REGS : MIN_MOVEM_REGS))
3305     return false;
3306
3307   return true;
3308 }
3309
3310 /* Return the assembly code template for a movem or fmovem instruction
3311    whose pattern is given by PATTERN.  Store the template's operands
3312    in OPERANDS.
3313
3314    If the instruction uses post-increment or pre-decrement addressing,
3315    AUTOMOD_OFFSET is the total adjustment, otherwise it is 0.  STORE_P
3316    is true if this is a store instruction.  */
3317
3318 const char *
3319 m68k_output_movem (rtx *operands, rtx pattern,
3320                    HOST_WIDE_INT automod_offset, bool store_p)
3321 {
3322   unsigned int mask;
3323   int i, first;
3324
3325   gcc_assert (GET_CODE (pattern) == PARALLEL);
3326   mask = 0;
3327   first = (automod_offset != 0);
3328   for (i = first; i < XVECLEN (pattern, 0); i++)
3329     {
3330       /* When using movem with pre-decrement addressing, register X + D0_REG
3331          is controlled by bit 15 - X.  For all other addressing modes,
3332          register X + D0_REG is controlled by bit X.  Confusingly, the
3333          register mask for fmovem is in the opposite order to that for
3334          movem.  */
3335       unsigned int regno;
3336
3337       gcc_assert (MEM_P (XEXP (XVECEXP (pattern, 0, i), !store_p)));
3338       gcc_assert (REG_P (XEXP (XVECEXP (pattern, 0, i), store_p)));
3339       regno = REGNO (XEXP (XVECEXP (pattern, 0, i), store_p));
3340       if (automod_offset < 0)
3341         {
3342           if (FP_REGNO_P (regno))
3343             mask |= 1 << (regno - FP0_REG);
3344           else
3345             mask |= 1 << (15 - (regno - D0_REG));
3346         }
3347       else
3348         {
3349           if (FP_REGNO_P (regno))
3350             mask |= 1 << (7 - (regno - FP0_REG));
3351           else
3352             mask |= 1 << (regno - D0_REG);
3353         }
3354     }
3355   CC_STATUS_INIT;
3356
3357   if (automod_offset == 0)
3358     operands[0] = XEXP (XEXP (XVECEXP (pattern, 0, first), !store_p), 0);
3359   else if (automod_offset < 0)
3360     operands[0] = gen_rtx_PRE_DEC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
3361   else
3362     operands[0] = gen_rtx_POST_INC (Pmode, SET_DEST (XVECEXP (pattern, 0, 0)));
3363   operands[1] = GEN_INT (mask);
3364   if (FP_REGNO_P (REGNO (XEXP (XVECEXP (pattern, 0, first), store_p))))
3365     {
3366       if (store_p)
3367         return "fmovem %1,%a0";
3368       else
3369         return "fmovem %a0,%1";
3370     }
3371   else
3372     {
3373       if (store_p)
3374         return "movem%.l %1,%a0";
3375       else
3376         return "movem%.l %a0,%1";
3377     }
3378 }
3379
3380 /* Return a REG that occurs in ADDR with coefficient 1.
3381    ADDR can be effectively incremented by incrementing REG.  */
3382
3383 static rtx
3384 find_addr_reg (rtx addr)
3385 {
3386   while (GET_CODE (addr) == PLUS)
3387     {
3388       if (GET_CODE (XEXP (addr, 0)) == REG)
3389         addr = XEXP (addr, 0);
3390       else if (GET_CODE (XEXP (addr, 1)) == REG)
3391         addr = XEXP (addr, 1);
3392       else if (CONSTANT_P (XEXP (addr, 0)))
3393         addr = XEXP (addr, 1);
3394       else if (CONSTANT_P (XEXP (addr, 1)))
3395         addr = XEXP (addr, 0);
3396       else
3397         gcc_unreachable ();
3398     }
3399   gcc_assert (GET_CODE (addr) == REG);
3400   return addr;
3401 }
3402
3403 /* Output assembler code to perform a 32-bit 3-operand add.  */
3404
3405 const char *
3406 output_addsi3 (rtx *operands)
3407 {
3408   if (! operands_match_p (operands[0], operands[1]))
3409     {
3410       if (!ADDRESS_REG_P (operands[1]))
3411         {
3412           rtx tmp = operands[1];
3413
3414           operands[1] = operands[2];
3415           operands[2] = tmp;
3416         }
3417
3418       /* These insns can result from reloads to access
3419          stack slots over 64k from the frame pointer.  */
3420       if (GET_CODE (operands[2]) == CONST_INT
3421           && (INTVAL (operands[2]) < -32768 || INTVAL (operands[2]) > 32767))
3422         return "move%.l %2,%0\n\tadd%.l %1,%0";
3423       if (GET_CODE (operands[2]) == REG)
3424         return MOTOROLA ? "lea (%1,%2.l),%0" : "lea %1@(0,%2:l),%0";
3425       return MOTOROLA ? "lea (%c2,%1),%0" : "lea %1@(%c2),%0";
3426     }
3427   if (GET_CODE (operands[2]) == CONST_INT)
3428     {
3429       if (INTVAL (operands[2]) > 0
3430           && INTVAL (operands[2]) <= 8)
3431         return "addq%.l %2,%0";
3432       if (INTVAL (operands[2]) < 0
3433           && INTVAL (operands[2]) >= -8)
3434         {
3435           operands[2] = GEN_INT (- INTVAL (operands[2]));
3436           return "subq%.l %2,%0";
3437         }
3438       /* On the CPU32 it is faster to use two addql instructions to
3439          add a small integer (8 < N <= 16) to a register.
3440          Likewise for subql.  */
3441       if (TUNE_CPU32 && REG_P (operands[0]))
3442         {
3443           if (INTVAL (operands[2]) > 8
3444               && INTVAL (operands[2]) <= 16)
3445             {
3446               operands[2] = GEN_INT (INTVAL (operands[2]) - 8);
3447               return "addq%.l #8,%0\n\taddq%.l %2,%0";
3448             }
3449           if (INTVAL (operands[2]) < -8
3450               && INTVAL (operands[2]) >= -16)
3451             {
3452               operands[2] = GEN_INT (- INTVAL (operands[2]) - 8);
3453               return "subq%.l #8,%0\n\tsubq%.l %2,%0";
3454             }
3455         }
3456       if (ADDRESS_REG_P (operands[0])
3457           && INTVAL (operands[2]) >= -0x8000
3458           && INTVAL (operands[2]) < 0x8000)
3459         {
3460           if (TUNE_68040)
3461             return "add%.w %2,%0";
3462           else
3463             return MOTOROLA ? "lea (%c2,%0),%0" : "lea %0@(%c2),%0";
3464         }
3465     }
3466   return "add%.l %2,%0";
3467 }
3468 \f
3469 /* Store in cc_status the expressions that the condition codes will
3470    describe after execution of an instruction whose pattern is EXP.
3471    Do not alter them if the instruction would not alter the cc's.  */
3472
3473 /* On the 68000, all the insns to store in an address register fail to
3474    set the cc's.  However, in some cases these instructions can make it
3475    possibly invalid to use the saved cc's.  In those cases we clear out
3476    some or all of the saved cc's so they won't be used.  */
3477
3478 void
3479 notice_update_cc (rtx exp, rtx insn)
3480 {
3481   if (GET_CODE (exp) == SET)
3482     {
3483       if (GET_CODE (SET_SRC (exp)) == CALL)
3484         CC_STATUS_INIT; 
3485       else if (ADDRESS_REG_P (SET_DEST (exp)))
3486         {
3487           if (cc_status.value1 && modified_in_p (cc_status.value1, insn))
3488             cc_status.value1 = 0;
3489           if (cc_status.value2 && modified_in_p (cc_status.value2, insn))
3490             cc_status.value2 = 0; 
3491         }
3492       /* fmoves to memory or data registers do not set the condition
3493          codes.  Normal moves _do_ set the condition codes, but not in
3494          a way that is appropriate for comparison with 0, because -0.0
3495          would be treated as a negative nonzero number.  Note that it
3496          isn't appropriate to conditionalize this restriction on
3497          HONOR_SIGNED_ZEROS because that macro merely indicates whether
3498          we care about the difference between -0.0 and +0.0.  */
3499       else if (!FP_REG_P (SET_DEST (exp))
3500                && SET_DEST (exp) != cc0_rtx
3501                && (FP_REG_P (SET_SRC (exp))
3502                    || GET_CODE (SET_SRC (exp)) == FIX
3503                    || FLOAT_MODE_P (GET_MODE (SET_DEST (exp)))))
3504         CC_STATUS_INIT; 
3505       /* A pair of move insns doesn't produce a useful overall cc.  */
3506       else if (!FP_REG_P (SET_DEST (exp))
3507                && !FP_REG_P (SET_SRC (exp))
3508                && GET_MODE_SIZE (GET_MODE (SET_SRC (exp))) > 4
3509                && (GET_CODE (SET_SRC (exp)) == REG
3510                    || GET_CODE (SET_SRC (exp)) == MEM
3511                    || GET_CODE (SET_SRC (exp)) == CONST_DOUBLE))
3512         CC_STATUS_INIT; 
3513       else if (SET_DEST (exp) != pc_rtx)
3514         {
3515           cc_status.flags = 0;
3516           cc_status.value1 = SET_DEST (exp);
3517           cc_status.value2 = SET_SRC (exp);
3518         }
3519     }
3520   else if (GET_CODE (exp) == PARALLEL
3521            && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
3522     {
3523       rtx dest = SET_DEST (XVECEXP (exp, 0, 0));
3524       rtx src  = SET_SRC  (XVECEXP (exp, 0, 0));
3525
3526       if (ADDRESS_REG_P (dest))
3527         CC_STATUS_INIT;
3528       else if (dest != pc_rtx)
3529         {
3530           cc_status.flags = 0;
3531           cc_status.value1 = dest;
3532           cc_status.value2 = src;
3533         }
3534     }
3535   else
3536     CC_STATUS_INIT;
3537   if (cc_status.value2 != 0
3538       && ADDRESS_REG_P (cc_status.value2)
3539       && GET_MODE (cc_status.value2) == QImode)
3540     CC_STATUS_INIT;
3541   if (cc_status.value2 != 0)
3542     switch (GET_CODE (cc_status.value2))
3543       {
3544       case ASHIFT: case ASHIFTRT: case LSHIFTRT:
3545       case ROTATE: case ROTATERT:
3546         /* These instructions always clear the overflow bit, and set
3547            the carry to the bit shifted out.  */
3548         /* ??? We don't currently have a way to signal carry not valid,
3549            nor do we check for it in the branch insns.  */
3550         CC_STATUS_INIT;
3551         break;
3552
3553       case PLUS: case MINUS: case MULT:
3554       case DIV: case UDIV: case MOD: case UMOD: case NEG:
3555         if (GET_MODE (cc_status.value2) != VOIDmode)
3556           cc_status.flags |= CC_NO_OVERFLOW;
3557         break;
3558       case ZERO_EXTEND:
3559         /* (SET r1 (ZERO_EXTEND r2)) on this machine
3560            ends with a move insn moving r2 in r2's mode.
3561            Thus, the cc's are set for r2.
3562            This can set N bit spuriously.  */
3563         cc_status.flags |= CC_NOT_NEGATIVE; 
3564
3565       default:
3566         break;
3567       }
3568   if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
3569       && cc_status.value2
3570       && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
3571     cc_status.value2 = 0;
3572   if (((cc_status.value1 && FP_REG_P (cc_status.value1))
3573        || (cc_status.value2 && FP_REG_P (cc_status.value2))))
3574     cc_status.flags = CC_IN_68881;
3575   if (cc_status.value2 && GET_CODE (cc_status.value2) == COMPARE
3576       && GET_MODE_CLASS (GET_MODE (XEXP (cc_status.value2, 0))) == MODE_FLOAT)
3577     {
3578       cc_status.flags = CC_IN_68881;
3579       if (!FP_REG_P (XEXP (cc_status.value2, 0)))
3580         cc_status.flags |= CC_REVERSED;
3581     }
3582 }
3583 \f
3584 const char *
3585 output_move_const_double (rtx *operands)
3586 {
3587   int code = standard_68881_constant_p (operands[1]);
3588
3589   if (code != 0)
3590     {
3591       static char buf[40];
3592
3593       sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
3594       return buf;
3595     }
3596   return "fmove%.d %1,%0";
3597 }
3598
3599 const char *
3600 output_move_const_single (rtx *operands)
3601 {
3602   int code = standard_68881_constant_p (operands[1]);
3603
3604   if (code != 0)
3605     {
3606       static char buf[40];
3607
3608       sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
3609       return buf;
3610     }
3611   return "fmove%.s %f1,%0";
3612 }
3613
3614 /* Return nonzero if X, a CONST_DOUBLE, has a value that we can get
3615    from the "fmovecr" instruction.
3616    The value, anded with 0xff, gives the code to use in fmovecr
3617    to get the desired constant.  */
3618
3619 /* This code has been fixed for cross-compilation.  */
3620   
3621 static int inited_68881_table = 0;
3622
3623 static const char *const strings_68881[7] = {
3624   "0.0",
3625   "1.0",
3626   "10.0",
3627   "100.0",
3628   "10000.0",
3629   "1e8",
3630   "1e16"
3631 };
3632
3633 static const int codes_68881[7] = {
3634   0x0f,
3635   0x32,
3636   0x33,
3637   0x34,
3638   0x35,
3639   0x36,
3640   0x37
3641 };
3642
3643 REAL_VALUE_TYPE values_68881[7];
3644
3645 /* Set up values_68881 array by converting the decimal values
3646    strings_68881 to binary.  */
3647
3648 void
3649 init_68881_table (void)
3650 {
3651   int i;
3652   REAL_VALUE_TYPE r;
3653   enum machine_mode mode;
3654
3655   mode = SFmode;
3656   for (i = 0; i < 7; i++)
3657     {
3658       if (i == 6)
3659         mode = DFmode;
3660       r = REAL_VALUE_ATOF (strings_68881[i], mode);
3661       values_68881[i] = r;
3662     }
3663   inited_68881_table = 1;
3664 }
3665
3666 int
3667 standard_68881_constant_p (rtx x)
3668 {
3669   REAL_VALUE_TYPE r;
3670   int i;
3671
3672   /* fmovecr must be emulated on the 68040 and 68060, so it shouldn't be
3673      used at all on those chips.  */
3674   if (TUNE_68040_60)
3675     return 0;
3676
3677   if (! inited_68881_table)
3678     init_68881_table ();
3679
3680   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3681
3682   /* Use REAL_VALUES_IDENTICAL instead of REAL_VALUES_EQUAL so that -0.0
3683      is rejected.  */
3684   for (i = 0; i < 6; i++)
3685     {
3686       if (REAL_VALUES_IDENTICAL (r, values_68881[i]))
3687         return (codes_68881[i]);
3688     }
3689   
3690   if (GET_MODE (x) == SFmode)
3691     return 0;
3692
3693   if (REAL_VALUES_EQUAL (r, values_68881[6]))
3694     return (codes_68881[6]);
3695
3696   /* larger powers of ten in the constants ram are not used
3697      because they are not equal to a `double' C constant.  */
3698   return 0;
3699 }
3700
3701 /* If X is a floating-point constant, return the logarithm of X base 2,
3702    or 0 if X is not a power of 2.  */
3703
3704 int
3705 floating_exact_log2 (rtx x)
3706 {
3707   REAL_VALUE_TYPE r, r1;
3708   int exp;
3709
3710   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3711
3712   if (REAL_VALUES_LESS (r, dconst1))
3713     return 0;
3714
3715   exp = real_exponent (&r);
3716   real_2expN (&r1, exp, DFmode);
3717   if (REAL_VALUES_EQUAL (r1, r))
3718     return exp;
3719
3720   return 0;
3721 }
3722 \f
3723 /* A C compound statement to output to stdio stream STREAM the
3724    assembler syntax for an instruction operand X.  X is an RTL
3725    expression.
3726
3727    CODE is a value that can be used to specify one of several ways
3728    of printing the operand.  It is used when identical operands
3729    must be printed differently depending on the context.  CODE
3730    comes from the `%' specification that was used to request
3731    printing of the operand.  If the specification was just `%DIGIT'
3732    then CODE is 0; if the specification was `%LTR DIGIT' then CODE
3733    is the ASCII code for LTR.
3734
3735    If X is a register, this macro should print the register's name.
3736    The names can be found in an array `reg_names' whose type is
3737    `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.
3738
3739    When the machine description has a specification `%PUNCT' (a `%'
3740    followed by a punctuation character), this macro is called with
3741    a null pointer for X and the punctuation character for CODE.
3742
3743    The m68k specific codes are:
3744
3745    '.' for dot needed in Motorola-style opcode names.
3746    '-' for an operand pushing on the stack:
3747        sp@-, -(sp) or -(%sp) depending on the style of syntax.
3748    '+' for an operand pushing on the stack:
3749        sp@+, (sp)+ or (%sp)+ depending on the style of syntax.
3750    '@' for a reference to the top word on the stack:
3751        sp@, (sp) or (%sp) depending on the style of syntax.
3752    '#' for an immediate operand prefix (# in MIT and Motorola syntax
3753        but & in SGS syntax).
3754    '!' for the cc register (used in an `and to cc' insn).
3755    '$' for the letter `s' in an op code, but only on the 68040.
3756    '&' for the letter `d' in an op code, but only on the 68040.
3757    '/' for register prefix needed by longlong.h.
3758    '?' for m68k_library_id_string
3759
3760    'b' for byte insn (no effect, on the Sun; this is for the ISI).
3761    'd' to force memory addressing to be absolute, not relative.
3762    'f' for float insn (print a CONST_DOUBLE as a float rather than in hex)
3763    'x' for float insn (print a CONST_DOUBLE as a float rather than in hex),
3764        or print pair of registers as rx:ry.
3765    'p' print an address with @PLTPC attached, but only if the operand
3766        is not locally-bound.  */
3767
3768 void
3769 print_operand (FILE *file, rtx op, int letter)
3770 {
3771   if (letter == '.')
3772     {
3773       if (MOTOROLA)
3774         fprintf (file, ".");
3775     }
3776   else if (letter == '#')
3777     asm_fprintf (file, "%I");
3778   else if (letter == '-')
3779     asm_fprintf (file, MOTOROLA ? "-(%Rsp)" : "%Rsp@-");
3780   else if (letter == '+')
3781     asm_fprintf (file, MOTOROLA ? "(%Rsp)+" : "%Rsp@+");
3782   else if (letter == '@')
3783     asm_fprintf (file, MOTOROLA ? "(%Rsp)" : "%Rsp@");
3784   else if (letter == '!')
3785     asm_fprintf (file, "%Rfpcr");
3786   else if (letter == '$')
3787     {
3788       if (TARGET_68040)
3789         fprintf (file, "s");
3790     }
3791   else if (letter == '&')
3792     {
3793       if (TARGET_68040)
3794         fprintf (file, "d");
3795     }
3796   else if (letter == '/')
3797     asm_fprintf (file, "%R");
3798   else if (letter == '?')
3799     asm_fprintf (file, m68k_library_id_string);
3800   else if (letter == 'p')
3801     {
3802       output_addr_const (file, op);
3803       if (!(GET_CODE (op) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (op)))
3804         fprintf (file, "@PLTPC");
3805     }
3806   else if (GET_CODE (op) == REG)
3807     {
3808       if (letter == 'R')
3809         /* Print out the second register name of a register pair.
3810            I.e., R (6) => 7.  */
3811         fputs (M68K_REGNAME(REGNO (op) + 1), file);
3812       else
3813         fputs (M68K_REGNAME(REGNO (op)), file);
3814     }
3815   else if (GET_CODE (op) == MEM)
3816     {
3817       output_address (XEXP (op, 0));
3818       if (letter == 'd' && ! TARGET_68020
3819           && CONSTANT_ADDRESS_P (XEXP (op, 0))
3820           && !(GET_CODE (XEXP (op, 0)) == CONST_INT
3821                && INTVAL (XEXP (op, 0)) < 0x8000
3822                && INTVAL (XEXP (op, 0)) >= -0x8000))
3823         fprintf (file, MOTOROLA ? ".l" : ":l");
3824     }
3825   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
3826     {
3827       REAL_VALUE_TYPE r;
3828       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3829       ASM_OUTPUT_FLOAT_OPERAND (letter, file, r);
3830     }
3831   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == XFmode)
3832     {
3833       REAL_VALUE_TYPE r;
3834       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3835       ASM_OUTPUT_LONG_DOUBLE_OPERAND (file, r);
3836     }
3837   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
3838     {
3839       REAL_VALUE_TYPE r;
3840       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3841       ASM_OUTPUT_DOUBLE_OPERAND (file, r);
3842     }
3843   else
3844     {
3845       /* Use `print_operand_address' instead of `output_addr_const'
3846          to ensure that we print relevant PIC stuff.  */
3847       asm_fprintf (file, "%I");
3848       if (TARGET_PCREL
3849           && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST))
3850         print_operand_address (file, op);
3851       else
3852         output_addr_const (file, op);
3853     }
3854 }
3855
3856 \f
3857 /* A C compound statement to output to stdio stream STREAM the
3858    assembler syntax for an instruction operand that is a memory
3859    reference whose address is ADDR.  ADDR is an RTL expression.
3860
3861    Note that this contains a kludge that knows that the only reason
3862    we have an address (plus (label_ref...) (reg...)) when not generating
3863    PIC code is in the insn before a tablejump, and we know that m68k.md
3864    generates a label LInnn: on such an insn.
3865
3866    It is possible for PIC to generate a (plus (label_ref...) (reg...))
3867    and we handle that just like we would a (plus (symbol_ref...) (reg...)).
3868
3869    This routine is responsible for distinguishing between -fpic and -fPIC 
3870    style relocations in an address.  When generating -fpic code the
3871    offset is output in word mode (e.g. movel a5@(_foo:w), a0).  When generating
3872    -fPIC code the offset is output in long mode (e.g. movel a5@(_foo:l), a0) */
3873
3874 void
3875 print_operand_address (FILE *file, rtx addr)
3876 {
3877   struct m68k_address address;
3878
3879   if (!m68k_decompose_address (QImode, addr, true, &address))
3880     gcc_unreachable ();
3881
3882   if (address.code == PRE_DEC)
3883     fprintf (file, MOTOROLA ? "-(%s)" : "%s@-",
3884              M68K_REGNAME (REGNO (address.base)));
3885   else if (address.code == POST_INC)
3886     fprintf (file, MOTOROLA ? "(%s)+" : "%s@+",
3887              M68K_REGNAME (REGNO (address.base)));
3888   else if (!address.base && !address.index)
3889     {
3890       /* A constant address.  */
3891       gcc_assert (address.offset == addr);
3892       if (GET_CODE (addr) == CONST_INT)
3893         {
3894           /* (xxx).w or (xxx).l.  */
3895           if (IN_RANGE (INTVAL (addr), -0x8000, 0x7fff))
3896             fprintf (file, MOTOROLA ? "%d.w" : "%d:w", (int) INTVAL (addr));
3897           else
3898             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
3899         }
3900       else if (TARGET_PCREL)
3901         {
3902           /* (d16,PC) or (bd,PC,Xn) (with suppressed index register).  */
3903           fputc ('(', file);
3904           output_addr_const (file, addr);
3905           asm_fprintf (file, flag_pic == 1 ? ":w,%Rpc)" : ":l,%Rpc)");
3906         }
3907       else
3908         {
3909           /* (xxx).l.  We need a special case for SYMBOL_REF if the symbol
3910              name ends in `.<letter>', as the last 2 characters can be
3911              mistaken as a size suffix.  Put the name in parentheses.  */
3912           if (GET_CODE (addr) == SYMBOL_REF
3913               && strlen (XSTR (addr, 0)) > 2
3914               && XSTR (addr, 0)[strlen (XSTR (addr, 0)) - 2] == '.')
3915             {
3916               putc ('(', file);
3917               output_addr_const (file, addr);
3918               putc (')', file);
3919             }
3920           else
3921             output_addr_const (file, addr);
3922         }
3923     }
3924   else
3925     {
3926       int labelno;
3927
3928       /* If ADDR is a (d8,pc,Xn) address, this is the number of the
3929          label being accessed, otherwise it is -1.  */
3930       labelno = (address.offset
3931                  && !address.base
3932                  && GET_CODE (address.offset) == LABEL_REF
3933                  ? CODE_LABEL_NUMBER (XEXP (address.offset, 0))
3934                  : -1);
3935       if (MOTOROLA)
3936         {
3937           /* Print the "offset(base" component.  */
3938           if (labelno >= 0)
3939             asm_fprintf (file, "%LL%d(%Rpc,", labelno);
3940           else
3941             {
3942               if (address.offset)
3943                 {
3944                   output_addr_const (file, address.offset);
3945                   if (flag_pic && address.base == pic_offset_table_rtx)
3946                     {
3947                       fprintf (file, "@GOT");
3948                       if (flag_pic == 1 && TARGET_68020)
3949                         fprintf (file, ".w");
3950                     }
3951                 }
3952               putc ('(', file);
3953               if (address.base)
3954                 fputs (M68K_REGNAME (REGNO (address.base)), file);
3955             }
3956           /* Print the ",index" component, if any.  */
3957           if (address.index)
3958             {
3959               if (address.base)
3960                 putc (',', file);
3961               fprintf (file, "%s.%c",
3962                        M68K_REGNAME (REGNO (address.index)),
3963                        GET_MODE (address.index) == HImode ? 'w' : 'l');
3964               if (address.scale != 1)
3965                 fprintf (file, "*%d", address.scale);
3966             }
3967           putc (')', file);
3968         }
3969       else /* !MOTOROLA */
3970         {
3971           if (!address.offset && !address.index)
3972             fprintf (file, "%s@", M68K_REGNAME (REGNO (address.base)));
3973           else
3974             {
3975               /* Print the "base@(offset" component.  */
3976               if (labelno >= 0)
3977                 asm_fprintf (file, "%Rpc@(%LL%d", labelno);
3978               else
3979                 {
3980                   if (address.base)
3981                     fputs (M68K_REGNAME (REGNO (address.base)), file);
3982                   fprintf (file, "@(");
3983                   if (address.offset)
3984                     {
3985                       output_addr_const (file, address.offset);
3986                       if (address.base == pic_offset_table_rtx && TARGET_68020)
3987                         switch (flag_pic)
3988                           {
3989                           case 1:
3990                             fprintf (file, ":w"); break;
3991                           case 2:
3992                             fprintf (file, ":l"); break;
3993                           default:
3994                             break;
3995                           }
3996                     }
3997                 }
3998               /* Print the ",index" component, if any.  */
3999               if (address.index)
4000                 {
4001                   fprintf (file, ",%s:%c",
4002                            M68K_REGNAME (REGNO (address.index)),
4003                            GET_MODE (address.index) == HImode ? 'w' : 'l');
4004                   if (address.scale != 1)
4005                     fprintf (file, ":%d", address.scale);
4006                 }
4007               putc (')', file);
4008             }
4009         }
4010     }
4011 }
4012 \f
4013 /* Check for cases where a clr insns can be omitted from code using
4014    strict_low_part sets.  For example, the second clrl here is not needed:
4015    clrl d0; movw a0@+,d0; use d0; clrl d0; movw a0@+; use d0; ...
4016
4017    MODE is the mode of this STRICT_LOW_PART set.  FIRST_INSN is the clear
4018    insn we are checking for redundancy.  TARGET is the register set by the
4019    clear insn.  */
4020
4021 bool
4022 strict_low_part_peephole_ok (enum machine_mode mode, rtx first_insn,
4023                              rtx target)
4024 {
4025   rtx p = first_insn;
4026
4027   while ((p = PREV_INSN (p)))
4028     {
4029       if (NOTE_INSN_BASIC_BLOCK_P (p))
4030         return false;
4031
4032       if (NOTE_P (p))
4033         continue;
4034
4035       /* If it isn't an insn, then give up.  */
4036       if (!INSN_P (p))
4037         return false;
4038
4039       if (reg_set_p (target, p))
4040         {
4041           rtx set = single_set (p);
4042           rtx dest;
4043
4044           /* If it isn't an easy to recognize insn, then give up.  */
4045           if (! set)
4046             return false;
4047
4048           dest = SET_DEST (set);
4049
4050           /* If this sets the entire target register to zero, then our
4051              first_insn is redundant.  */
4052           if (rtx_equal_p (dest, target)
4053               && SET_SRC (set) == const0_rtx)
4054             return true;
4055           else if (GET_CODE (dest) == STRICT_LOW_PART
4056                    && GET_CODE (XEXP (dest, 0)) == REG
4057                    && REGNO (XEXP (dest, 0)) == REGNO (target)
4058                    && (GET_MODE_SIZE (GET_MODE (XEXP (dest, 0)))
4059                        <= GET_MODE_SIZE (mode)))
4060             /* This is a strict low part set which modifies less than
4061                we are using, so it is safe.  */
4062             ;
4063           else
4064             return false;
4065         }
4066     }
4067
4068   return false;
4069 }
4070
4071 /* Operand predicates for implementing asymmetric pc-relative addressing
4072    on m68k.  The m68k supports pc-relative addressing (mode 7, register 2)
4073    when used as a source operand, but not as a destination operand.
4074
4075    We model this by restricting the meaning of the basic predicates
4076    (general_operand, memory_operand, etc) to forbid the use of this
4077    addressing mode, and then define the following predicates that permit
4078    this addressing mode.  These predicates can then be used for the
4079    source operands of the appropriate instructions.
4080
4081    n.b.  While it is theoretically possible to change all machine patterns
4082    to use this addressing more where permitted by the architecture,
4083    it has only been implemented for "common" cases: SImode, HImode, and
4084    QImode operands, and only for the principle operations that would
4085    require this addressing mode: data movement and simple integer operations.
4086
4087    In parallel with these new predicates, two new constraint letters
4088    were defined: 'S' and 'T'.  'S' is the -mpcrel analog of 'm'.
4089    'T' replaces 's' in the non-pcrel case.  It is a no-op in the pcrel case.
4090    In the pcrel case 's' is only valid in combination with 'a' registers.
4091    See addsi3, subsi3, cmpsi, and movsi patterns for a better understanding
4092    of how these constraints are used.
4093
4094    The use of these predicates is strictly optional, though patterns that
4095    don't will cause an extra reload register to be allocated where one
4096    was not necessary:
4097
4098         lea (abc:w,%pc),%a0     ; need to reload address
4099         moveq &1,%d1            ; since write to pc-relative space
4100         movel %d1,%a0@          ; is not allowed
4101         ...
4102         lea (abc:w,%pc),%a1     ; no need to reload address here
4103         movel %a1@,%d0          ; since "movel (abc:w,%pc),%d0" is ok
4104
4105    For more info, consult tiemann@cygnus.com.
4106
4107
4108    All of the ugliness with predicates and constraints is due to the
4109    simple fact that the m68k does not allow a pc-relative addressing
4110    mode as a destination.  gcc does not distinguish between source and
4111    destination addresses.  Hence, if we claim that pc-relative address
4112    modes are valid, e.g. GO_IF_LEGITIMATE_ADDRESS accepts them, then we
4113    end up with invalid code.  To get around this problem, we left
4114    pc-relative modes as invalid addresses, and then added special
4115    predicates and constraints to accept them.
4116
4117    A cleaner way to handle this is to modify gcc to distinguish
4118    between source and destination addresses.  We can then say that
4119    pc-relative is a valid source address but not a valid destination
4120    address, and hopefully avoid a lot of the predicate and constraint
4121    hackery.  Unfortunately, this would be a pretty big change.  It would
4122    be a useful change for a number of ports, but there aren't any current
4123    plans to undertake this.
4124
4125    ***************************************************************************/
4126
4127
4128 const char *
4129 output_andsi3 (rtx *operands)
4130 {
4131   int logval;
4132   if (GET_CODE (operands[2]) == CONST_INT
4133       && (INTVAL (operands[2]) | 0xffff) == -1
4134       && (DATA_REG_P (operands[0])
4135           || offsettable_memref_p (operands[0]))
4136       && !TARGET_COLDFIRE)
4137     {
4138       if (GET_CODE (operands[0]) != REG)
4139         operands[0] = adjust_address (operands[0], HImode, 2);
4140       operands[2] = GEN_INT (INTVAL (operands[2]) & 0xffff);
4141       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
4142       CC_STATUS_INIT;
4143       if (operands[2] == const0_rtx)
4144         return "clr%.w %0";
4145       return "and%.w %2,%0";
4146     }
4147   if (GET_CODE (operands[2]) == CONST_INT
4148       && (logval = exact_log2 (~ INTVAL (operands[2]))) >= 0
4149       && (DATA_REG_P (operands[0])
4150           || offsettable_memref_p (operands[0])))
4151     {
4152       if (DATA_REG_P (operands[0]))
4153         operands[1] = GEN_INT (logval);
4154       else
4155         {
4156           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4157           operands[1] = GEN_INT (logval % 8);
4158         }
4159       /* This does not set condition codes in a standard way.  */
4160       CC_STATUS_INIT;
4161       return "bclr %1,%0";
4162     }
4163   return "and%.l %2,%0";
4164 }
4165
4166 const char *
4167 output_iorsi3 (rtx *operands)
4168 {
4169   register int logval;
4170   if (GET_CODE (operands[2]) == CONST_INT
4171       && INTVAL (operands[2]) >> 16 == 0
4172       && (DATA_REG_P (operands[0])
4173           || offsettable_memref_p (operands[0]))
4174       && !TARGET_COLDFIRE)
4175     {
4176       if (GET_CODE (operands[0]) != REG)
4177         operands[0] = adjust_address (operands[0], HImode, 2);
4178       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
4179       CC_STATUS_INIT;
4180       if (INTVAL (operands[2]) == 0xffff)
4181         return "mov%.w %2,%0";
4182       return "or%.w %2,%0";
4183     }
4184   if (GET_CODE (operands[2]) == CONST_INT
4185       && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
4186       && (DATA_REG_P (operands[0])
4187           || offsettable_memref_p (operands[0])))
4188     {
4189       if (DATA_REG_P (operands[0]))
4190         operands[1] = GEN_INT (logval);
4191       else
4192         {
4193           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4194           operands[1] = GEN_INT (logval % 8);
4195         }
4196       CC_STATUS_INIT;
4197       return "bset %1,%0";
4198     }
4199   return "or%.l %2,%0";
4200 }
4201
4202 const char *
4203 output_xorsi3 (rtx *operands)
4204 {
4205   register int logval;
4206   if (GET_CODE (operands[2]) == CONST_INT
4207       && INTVAL (operands[2]) >> 16 == 0
4208       && (offsettable_memref_p (operands[0]) || DATA_REG_P (operands[0]))
4209       && !TARGET_COLDFIRE)
4210     {
4211       if (! DATA_REG_P (operands[0]))
4212         operands[0] = adjust_address (operands[0], HImode, 2);
4213       /* Do not delete a following tstl %0 insn; that would be incorrect.  */
4214       CC_STATUS_INIT;
4215       if (INTVAL (operands[2]) == 0xffff)
4216         return "not%.w %0";
4217       return "eor%.w %2,%0";
4218     }
4219   if (GET_CODE (operands[2]) == CONST_INT
4220       && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
4221       && (DATA_REG_P (operands[0])
4222           || offsettable_memref_p (operands[0])))
4223     {
4224       if (DATA_REG_P (operands[0]))
4225         operands[1] = GEN_INT (logval);
4226       else
4227         {
4228           operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
4229           operands[1] = GEN_INT (logval % 8);
4230         }
4231       CC_STATUS_INIT;
4232       return "bchg %1,%0";
4233     }
4234   return "eor%.l %2,%0";
4235 }
4236
4237 /* Return the instruction that should be used for a call to address X,
4238    which is known to be in operand 0.  */
4239
4240 const char *
4241 output_call (rtx x)
4242 {
4243   if (symbolic_operand (x, VOIDmode))
4244     return m68k_symbolic_call;
4245   else
4246     return "jsr %a0";
4247 }
4248
4249 /* Likewise sibling calls.  */
4250
4251 const char *
4252 output_sibcall (rtx x)
4253 {
4254   if (symbolic_operand (x, VOIDmode))
4255     return m68k_symbolic_jump;
4256   else
4257     return "jmp %a0";
4258 }
4259
4260 #ifdef M68K_TARGET_COFF
4261
4262 /* Output assembly to switch to section NAME with attribute FLAGS.  */
4263
4264 static void
4265 m68k_coff_asm_named_section (const char *name, unsigned int flags, 
4266                              tree decl ATTRIBUTE_UNUSED)
4267 {
4268   char flagchar;
4269
4270   if (flags & SECTION_WRITE)
4271     flagchar = 'd';
4272   else
4273     flagchar = 'x';
4274
4275   fprintf (asm_out_file, "\t.section\t%s,\"%c\"\n", name, flagchar);
4276 }
4277
4278 #endif /* M68K_TARGET_COFF */
4279
4280 static void
4281 m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
4282                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
4283                       tree function)
4284 {
4285   rtx this_slot, offset, addr, mem, insn;
4286
4287   /* Pretend to be a post-reload pass while generating rtl.  */
4288   reload_completed = 1;
4289
4290   /* The "this" pointer is stored at 4(%sp).  */
4291   this_slot = gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx, 4));
4292
4293   /* Add DELTA to THIS.  */
4294   if (delta != 0)
4295     {
4296       /* Make the offset a legitimate operand for memory addition.  */
4297       offset = GEN_INT (delta);
4298       if ((delta < -8 || delta > 8)
4299           && (TARGET_COLDFIRE || USE_MOVQ (delta)))
4300         {
4301           emit_move_insn (gen_rtx_REG (Pmode, D0_REG), offset);
4302           offset = gen_rtx_REG (Pmode, D0_REG);
4303         }
4304       emit_insn (gen_add3_insn (copy_rtx (this_slot),
4305                                 copy_rtx (this_slot), offset));
4306     }
4307
4308   /* If needed, add *(*THIS + VCALL_OFFSET) to THIS.  */
4309   if (vcall_offset != 0)
4310     {
4311       /* Set the static chain register to *THIS.  */
4312       emit_move_insn (static_chain_rtx, this_slot);
4313       emit_move_insn (static_chain_rtx, gen_rtx_MEM (Pmode, static_chain_rtx));
4314
4315       /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET.  */
4316       addr = plus_constant (static_chain_rtx, vcall_offset);
4317       if (!m68k_legitimate_address_p (Pmode, addr, true))
4318         {
4319           emit_insn (gen_rtx_SET (VOIDmode, static_chain_rtx, addr));
4320           addr = static_chain_rtx;
4321         }
4322
4323       /* Load the offset into %d0 and add it to THIS.  */
4324       emit_move_insn (gen_rtx_REG (Pmode, D0_REG),
4325                       gen_rtx_MEM (Pmode, addr));
4326       emit_insn (gen_add3_insn (copy_rtx (this_slot),
4327                                 copy_rtx (this_slot),
4328                                 gen_rtx_REG (Pmode, D0_REG)));
4329     }
4330
4331   /* Jump to the target function.  Use a sibcall if direct jumps are
4332      allowed, otherwise load the address into a register first.  */
4333   mem = DECL_RTL (function);
4334   if (!sibcall_operand (XEXP (mem, 0), VOIDmode))
4335     {
4336       gcc_assert (flag_pic);
4337
4338       if (!TARGET_SEP_DATA)
4339         {
4340           /* Use the static chain register as a temporary (call-clobbered)
4341              GOT pointer for this function.  We can use the static chain
4342              register because it isn't live on entry to the thunk.  */
4343           SET_REGNO (pic_offset_table_rtx, STATIC_CHAIN_REGNUM);
4344           emit_insn (gen_load_got (pic_offset_table_rtx));
4345         }
4346       legitimize_pic_address (XEXP (mem, 0), Pmode, static_chain_rtx);
4347       mem = replace_equiv_address (mem, static_chain_rtx);
4348     }
4349   insn = emit_call_insn (gen_sibcall (mem, const0_rtx));
4350   SIBLING_CALL_P (insn) = 1;
4351
4352   /* Run just enough of rest_of_compilation.  */
4353   insn = get_insns ();
4354   split_all_insns_noflow ();
4355   final_start_function (insn, file, 1);
4356   final (insn, file, 1);
4357   final_end_function ();
4358
4359   /* Clean up the vars set above.  */
4360   reload_completed = 0;
4361
4362   /* Restore the original PIC register.  */
4363   if (flag_pic)
4364     SET_REGNO (pic_offset_table_rtx, PIC_REG);
4365   free_after_compilation (cfun);
4366 }
4367
4368 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
4369
4370 static rtx
4371 m68k_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
4372                        int incoming ATTRIBUTE_UNUSED)
4373 {
4374   return gen_rtx_REG (Pmode, M68K_STRUCT_VALUE_REGNUM);
4375 }
4376
4377 /* Return nonzero if register old_reg can be renamed to register new_reg.  */
4378 int
4379 m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
4380                            unsigned int new_reg)
4381 {
4382
4383   /* Interrupt functions can only use registers that have already been
4384      saved by the prologue, even if they would normally be
4385      call-clobbered.  */
4386
4387   if ((m68k_get_function_kind (current_function_decl)
4388        == m68k_fk_interrupt_handler)
4389       && !df_regs_ever_live_p (new_reg))
4390     return 0;
4391
4392   return 1;
4393 }
4394
4395 /* Value is true if hard register REGNO can hold a value of machine-mode
4396    MODE.  On the 68000, we let the cpu registers can hold any mode, but
4397    restrict the 68881 registers to floating-point modes.  */
4398
4399 bool
4400 m68k_regno_mode_ok (int regno, enum machine_mode mode)
4401 {
4402   if (DATA_REGNO_P (regno))
4403     {
4404       /* Data Registers, can hold aggregate if fits in.  */
4405       if (regno + GET_MODE_SIZE (mode) / 4 <= 8)
4406         return true;
4407     }
4408   else if (ADDRESS_REGNO_P (regno))
4409     {
4410       if (regno + GET_MODE_SIZE (mode) / 4 <= 16)
4411         return true;
4412     }
4413   else if (FP_REGNO_P (regno))
4414     {
4415       /* FPU registers, hold float or complex float of long double or
4416          smaller.  */
4417       if ((GET_MODE_CLASS (mode) == MODE_FLOAT
4418            || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4419           && GET_MODE_UNIT_SIZE (mode) <= TARGET_FP_REG_SIZE)
4420         return true;
4421     }
4422   return false;
4423 }
4424
4425 /* Implement SECONDARY_RELOAD_CLASS.  */
4426
4427 enum reg_class
4428 m68k_secondary_reload_class (enum reg_class rclass,
4429                              enum machine_mode mode, rtx x)
4430 {
4431   int regno;
4432
4433   regno = true_regnum (x);
4434
4435   /* If one operand of a movqi is an address register, the other
4436      operand must be a general register or constant.  Other types
4437      of operand must be reloaded through a data register.  */
4438   if (GET_MODE_SIZE (mode) == 1
4439       && reg_classes_intersect_p (rclass, ADDR_REGS)
4440       && !(INT_REGNO_P (regno) || CONSTANT_P (x)))
4441     return DATA_REGS;
4442
4443   /* PC-relative addresses must be loaded into an address register first.  */
4444   if (TARGET_PCREL
4445       && !reg_class_subset_p (rclass, ADDR_REGS)
4446       && symbolic_operand (x, VOIDmode))
4447     return ADDR_REGS;
4448
4449   return NO_REGS;
4450 }
4451
4452 /* Implement PREFERRED_RELOAD_CLASS.  */
4453
4454 enum reg_class
4455 m68k_preferred_reload_class (rtx x, enum reg_class rclass)
4456 {
4457   enum reg_class secondary_class;
4458
4459   /* If RCLASS might need a secondary reload, try restricting it to
4460      a class that doesn't.  */
4461   secondary_class = m68k_secondary_reload_class (rclass, GET_MODE (x), x);
4462   if (secondary_class != NO_REGS
4463       && reg_class_subset_p (secondary_class, rclass))
4464     return secondary_class;
4465
4466   /* Prefer to use moveq for in-range constants.  */
4467   if (GET_CODE (x) == CONST_INT
4468       && reg_class_subset_p (DATA_REGS, rclass)
4469       && IN_RANGE (INTVAL (x), -0x80, 0x7f))
4470     return DATA_REGS;
4471
4472   /* ??? Do we really need this now?  */
4473   if (GET_CODE (x) == CONST_DOUBLE
4474       && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
4475     {
4476       if (TARGET_HARD_FLOAT && reg_class_subset_p (FP_REGS, rclass))
4477         return FP_REGS;
4478
4479       return NO_REGS;
4480     }
4481
4482   return rclass;
4483 }
4484
4485 /* Return floating point values in a 68881 register.  This makes 68881 code
4486    a little bit faster.  It also makes -msoft-float code incompatible with
4487    hard-float code, so people have to be careful not to mix the two.
4488    For ColdFire it was decided the ABI incompatibility is undesirable.
4489    If there is need for a hard-float ABI it is probably worth doing it
4490    properly and also passing function arguments in FP registers.  */
4491 rtx
4492 m68k_libcall_value (enum machine_mode mode)
4493 {
4494   switch (mode) {
4495   case SFmode:
4496   case DFmode:
4497   case XFmode:
4498     if (TARGET_68881)
4499       return gen_rtx_REG (mode, FP0_REG);
4500     break;
4501   default:
4502     break;
4503   }
4504   return gen_rtx_REG (mode, D0_REG);
4505 }
4506
4507 rtx
4508 m68k_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED)
4509 {
4510   enum machine_mode mode;
4511
4512   mode = TYPE_MODE (valtype);
4513   switch (mode) {
4514   case SFmode:
4515   case DFmode:
4516   case XFmode:
4517     if (TARGET_68881)
4518       return gen_rtx_REG (mode, FP0_REG);
4519     break;
4520   default:
4521     break;
4522   }
4523
4524   /* If the function returns a pointer, push that into %a0.  */
4525   if (func && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (func))))
4526     /* For compatibility with the large body of existing code which
4527        does not always properly declare external functions returning
4528        pointer types, the m68k/SVR4 convention is to copy the value
4529        returned for pointer functions from a0 to d0 in the function
4530        epilogue, so that callers that have neglected to properly
4531        declare the callee can still find the correct return value in
4532        d0.  */
4533     return gen_rtx_PARALLEL
4534       (mode,
4535        gen_rtvec (2,
4536                   gen_rtx_EXPR_LIST (VOIDmode,
4537                                      gen_rtx_REG (mode, A0_REG),
4538                                      const0_rtx),
4539                   gen_rtx_EXPR_LIST (VOIDmode,
4540                                      gen_rtx_REG (mode, D0_REG),
4541                                      const0_rtx)));
4542   else if (POINTER_TYPE_P (valtype))
4543     return gen_rtx_REG (mode, A0_REG);
4544   else
4545     return gen_rtx_REG (mode, D0_REG);
4546 }
4547
4548 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
4549 #if M68K_HONOR_TARGET_STRICT_ALIGNMENT
4550 static bool
4551 m68k_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
4552 {
4553   enum machine_mode mode = TYPE_MODE (type);
4554
4555   if (mode == BLKmode)
4556     return true;
4557
4558   /* If TYPE's known alignment is less than the alignment of MODE that
4559      would contain the structure, then return in memory.  We need to
4560      do so to maintain the compatibility between code compiled with
4561      -mstrict-align and that compiled with -mno-strict-align.  */
4562   if (AGGREGATE_TYPE_P (type)
4563       && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (mode))
4564     return true;
4565
4566   return false;
4567 }
4568 #endif
4569
4570 /* CPU to schedule the program for.  */
4571 enum attr_cpu m68k_sched_cpu;
4572
4573 /* MAC to schedule the program for.  */
4574 enum attr_mac m68k_sched_mac;
4575
4576 /* Operand type.  */
4577 enum attr_op_type
4578   {
4579     /* No operand.  */
4580     OP_TYPE_NONE,
4581
4582     /* Register.  */
4583     OP_TYPE_REG,
4584
4585     /* Implicit mem reference (e.g. stack).  */
4586     OP_TYPE_MEM1,
4587
4588     /* Memory without offset or indexing.  EA modes 2, 3 and 4.  */
4589     OP_TYPE_MEM234,
4590
4591     /* Memory with offset but without indexing.  EA mode 5.  */
4592     OP_TYPE_MEM5,
4593
4594     /* Memory with indexing.  EA mode 6.  */
4595     OP_TYPE_MEM6,
4596
4597     /* Memory referenced by absolute address.  EA mode 7.  */
4598     OP_TYPE_MEM7,
4599
4600     /* Immediate operand that doesn't require extension word.  */
4601     OP_TYPE_IMM_Q,
4602
4603     /* Immediate 16 bit operand.  */
4604     OP_TYPE_IMM_W,
4605
4606     /* Immediate 32 bit operand.  */
4607     OP_TYPE_IMM_L
4608   };
4609
4610 /* True if current insn doesn't have complete pipeline description.  */
4611 static bool sched_guess_p;
4612
4613 /* Return type of memory ADDR_RTX refers to.  */
4614 static enum attr_op_type
4615 sched_address_type (enum machine_mode mode, rtx addr_rtx)
4616 {
4617   struct m68k_address address;
4618
4619   if (!m68k_decompose_address (mode, addr_rtx,
4620                                reload_completed, &address))
4621     {
4622       gcc_assert (sched_guess_p);
4623       /* Reload will likely fix the address to be in the register.  */
4624       return OP_TYPE_MEM234;
4625     }
4626
4627   if (address.scale != 0)
4628     return OP_TYPE_MEM6;
4629
4630   if (address.base != NULL_RTX)
4631     {
4632       if (address.offset == NULL_RTX)
4633         return OP_TYPE_MEM234;
4634
4635       return OP_TYPE_MEM5;
4636     }
4637
4638   gcc_assert (address.offset != NULL_RTX);
4639
4640   return OP_TYPE_MEM7;
4641 }
4642
4643 /* Return type of the operand OP.
4644    If ADDRESS_P is true, return type of memory location OP refers to.  */
4645 static enum attr_op_type
4646 sched_operand_type (rtx op, bool address_p)
4647 {
4648   gcc_assert (op != NULL_RTX);
4649
4650   if (address_p)
4651     return sched_address_type (QImode, op);
4652
4653   if (memory_operand (op, VOIDmode))
4654     return sched_address_type (GET_MODE (op), XEXP (op, 0));
4655
4656   if (register_operand (op, VOIDmode))
4657     return OP_TYPE_REG;
4658
4659   if (GET_CODE (op) == CONST_INT)
4660     {
4661       /* ??? Below condition should probably check if the operation is
4662          signed or unsigned.  */
4663       if (IN_RANGE (INTVAL (op), -0x8000, 0x7fff))
4664         return OP_TYPE_IMM_W;
4665
4666       return OP_TYPE_IMM_L;
4667     }
4668
4669   if (GET_CODE (op) == CONST_DOUBLE)
4670     {
4671       switch (GET_MODE (op))
4672         {
4673         case SFmode:
4674           return OP_TYPE_IMM_W;
4675
4676         case VOIDmode:
4677         case DFmode:
4678           return OP_TYPE_IMM_L;
4679
4680         default:
4681           gcc_unreachable ();
4682         }
4683     }
4684
4685   if (symbolic_operand (op, VOIDmode)
4686       || LABEL_P (op))
4687     {
4688       switch (GET_MODE (op))
4689         {
4690         case QImode:
4691           return OP_TYPE_IMM_Q;
4692
4693         case HImode:
4694           return OP_TYPE_IMM_W;
4695
4696         case SImode:
4697           return OP_TYPE_IMM_L;
4698
4699         default:
4700           if (GET_CODE (op) == SYMBOL_REF)
4701             /* ??? Just a guess.  Probably we can guess better using length
4702                attribute of the instructions.  */
4703             return OP_TYPE_IMM_W;
4704
4705           return OP_TYPE_IMM_L;
4706         }
4707     }
4708
4709   gcc_assert (sched_guess_p);
4710
4711   return OP_TYPE_REG;
4712 }
4713
4714 /* Return type of INSN's operand X (if OPX_P) or operand Y (if !OPX_P).
4715    If ADDRESS_P is true, return type of memory location operand refers to.  */
4716 static enum attr_op_type
4717 sched_attr_op_type (rtx insn, bool opx_p, bool address_p)
4718 {
4719   int i;
4720
4721   extract_constrain_insn_cached (insn);
4722
4723   if (opx_p)
4724     i = get_attr_opx (insn);
4725   else
4726     i = get_attr_opy (insn);
4727
4728   if (i >= recog_data.n_operands)
4729     {
4730       gcc_assert (sched_guess_p);
4731       return OP_TYPE_REG;
4732     }
4733
4734   return sched_operand_type (recog_data.operand[i], address_p);
4735 }
4736
4737 /* Implement opx_type attribute.
4738    Return type of INSN's operand X.
4739    If ADDRESS_P is true, return type of memory location operand refers to.  */
4740 enum attr_opx_type
4741 m68k_sched_attr_opx_type (rtx insn, int address_p)
4742 {
4743   sched_guess_p = (get_attr_guess (insn) == GUESS_YES);
4744
4745   switch (sched_attr_op_type (insn, true, address_p != 0))
4746     {
4747     case OP_TYPE_REG:
4748       return OPX_TYPE_REG;
4749
4750     case OP_TYPE_MEM1:
4751       return OPX_TYPE_MEM1;
4752
4753     case OP_TYPE_MEM234:
4754       return OPX_TYPE_MEM234;
4755
4756     case OP_TYPE_MEM5:
4757       return OPX_TYPE_MEM5;
4758
4759     case OP_TYPE_MEM6:
4760       return OPX_TYPE_MEM6;
4761
4762     case OP_TYPE_MEM7:
4763       return OPX_TYPE_MEM7;
4764
4765     case OP_TYPE_IMM_Q:
4766       return OPX_TYPE_IMM_Q;
4767
4768     case OP_TYPE_IMM_W:
4769       return OPX_TYPE_IMM_W;
4770
4771     case OP_TYPE_IMM_L:
4772       return OPX_TYPE_IMM_L;
4773
4774     default:
4775       gcc_unreachable ();
4776       return 0;
4777     }
4778 }
4779
4780 /* Implement opy_type attribute.
4781    Return type of INSN's operand Y.
4782    If ADDRESS_P is true, return type of memory location operand refers to.  */
4783 enum attr_opy_type
4784 m68k_sched_attr_opy_type (rtx insn, int address_p)
4785 {
4786   sched_guess_p = (get_attr_guess (insn) == GUESS_YES);
4787
4788   switch (sched_attr_op_type (insn, false, address_p != 0))
4789     {
4790     case OP_TYPE_REG:
4791       return OPY_TYPE_REG;
4792
4793     case OP_TYPE_MEM1:
4794       return OPY_TYPE_MEM1;
4795
4796     case OP_TYPE_MEM234:
4797       return OPY_TYPE_MEM234;
4798
4799     case OP_TYPE_MEM5:
4800       return OPY_TYPE_MEM5;
4801
4802     case OP_TYPE_MEM6:
4803       return OPY_TYPE_MEM6;
4804
4805     case OP_TYPE_MEM7:
4806       return OPY_TYPE_MEM7;
4807
4808     case OP_TYPE_IMM_Q:
4809       return OPY_TYPE_IMM_Q;
4810
4811     case OP_TYPE_IMM_W:
4812       return OPY_TYPE_IMM_W;
4813
4814     case OP_TYPE_IMM_L:
4815       return OPY_TYPE_IMM_L;
4816
4817     default:
4818       gcc_unreachable ();
4819       return 0;
4820     }
4821 }
4822
4823 /* Return the size of INSN.  */
4824 int
4825 m68k_sched_attr_size (rtx insn)
4826 {
4827   int size;
4828
4829   sched_guess_p = (get_attr_guess (insn) == GUESS_YES);
4830
4831   switch (get_attr_type1 (insn))
4832     {
4833     case TYPE1_MUL_L:
4834       size = 2;
4835       break;
4836
4837     default:
4838       size = 1;
4839       break;
4840     }
4841
4842   switch (get_attr_opx_type (insn))
4843     {
4844     case OPX_TYPE_NONE:
4845     case OPX_TYPE_REG:
4846     case OPX_TYPE_MEM1:
4847     case OPX_TYPE_MEM234:
4848     case OPY_TYPE_IMM_Q:
4849       break;
4850
4851     case OPX_TYPE_MEM5:
4852     case OPX_TYPE_MEM6:
4853       /* Here we assume that most absolute references are short.  */
4854     case OPX_TYPE_MEM7:
4855     case OPY_TYPE_IMM_W:
4856       ++size;
4857       break;
4858
4859     case OPY_TYPE_IMM_L:
4860       size += 2;
4861       break;
4862
4863     default:
4864       gcc_unreachable ();
4865     }
4866
4867   switch (get_attr_opy_type (insn))
4868     {
4869     case OPY_TYPE_NONE:
4870     case OPY_TYPE_REG:
4871     case OPY_TYPE_MEM1:
4872     case OPY_TYPE_MEM234:
4873     case OPY_TYPE_IMM_Q:
4874       break;
4875
4876     case OPY_TYPE_MEM5:
4877     case OPY_TYPE_MEM6:
4878       /* Here we assume that most absolute references are short.  */
4879     case OPY_TYPE_MEM7:
4880     case OPY_TYPE_IMM_W:
4881       ++size;
4882       break;
4883
4884     case OPY_TYPE_IMM_L:
4885       size += 2;
4886       break;
4887
4888     default:
4889       gcc_unreachable ();
4890     }
4891
4892   if (size > 3)
4893     {
4894       gcc_assert (sched_guess_p);
4895
4896       size = 3;
4897     }
4898
4899   return size;
4900 }
4901
4902 /* Implement op_mem attribute.  */
4903 enum attr_op_mem
4904 m68k_sched_attr_op_mem (rtx insn)
4905 {
4906   enum attr_opy_mem opy;
4907   enum attr_opx_mem opx;
4908
4909   sched_guess_p = (get_attr_guess (insn) == GUESS_YES);
4910
4911   opy = get_attr_opy_mem (insn);
4912   opx = get_attr_opx_mem (insn);
4913
4914   if (opy == OPY_MEM_R && opx == OPX_MEM_R)
4915     return OP_MEM_00;
4916
4917   if (opy == OPY_MEM_R && opx == OPX_MEM_M)
4918     {
4919       switch (get_attr_opx_access (insn))
4920         {
4921         case OPX_ACCESS_R:
4922           return OP_MEM_10;
4923
4924         case OPX_ACCESS_W:
4925           return OP_MEM_01;
4926
4927         case OPX_ACCESS_RW:
4928           return OP_MEM_11;
4929
4930         default:
4931           gcc_assert (sched_guess_p);
4932           return OP_MEM_UNKNOWN;
4933         }
4934     }
4935
4936   if (opy == OPY_MEM_R && opx == OPX_MEM_I)
4937     {
4938       switch (get_attr_opx_access (insn))
4939         {
4940         case OPX_ACCESS_R:
4941           return OP_MEM_I0;
4942
4943         case OPX_ACCESS_W:
4944           return OP_MEM_0I;
4945
4946         case OPX_ACCESS_RW:
4947           return OP_MEM_I1;
4948
4949         default:
4950           gcc_assert (sched_guess_p);
4951           return OP_MEM_UNKNOWN;
4952         }
4953     }
4954
4955   if (opy == OPY_MEM_M && opx == OPX_MEM_R)
4956     return OP_MEM_10;
4957
4958   if (opy == OPY_MEM_M && opx == OPX_MEM_M)
4959     {
4960       switch (get_attr_opx_access (insn))
4961         {
4962         case OPX_ACCESS_W:
4963           return OP_MEM_11;
4964
4965         default:
4966           gcc_assert (sched_guess_p);
4967           return OP_MEM_UNKNOWN;
4968         }
4969     }
4970
4971   if (opy == OPY_MEM_M && opx == OPX_MEM_I)
4972     {
4973       switch (get_attr_opx_access (insn))
4974         {
4975         case OPX_ACCESS_W:
4976           return OP_MEM_1I;
4977
4978         default:
4979           gcc_assert (sched_guess_p);
4980           return OP_MEM_UNKNOWN;
4981         }
4982     }
4983
4984   if (opy == OPY_MEM_I && opx == OPX_MEM_R)
4985     return OP_MEM_I0;
4986
4987
4988   if (opy == OPY_MEM_I && opx == OPX_MEM_M)
4989     {
4990       switch (get_attr_opx_access (insn))
4991         {
4992         case OPX_ACCESS_W:
4993           return OP_MEM_I1;
4994
4995         default:
4996           gcc_assert (sched_guess_p);
4997           return OP_MEM_UNKNOWN;
4998         }
4999     }
5000
5001   gcc_assert (sched_guess_p);
5002   return OP_MEM_UNKNOWN;
5003 }
5004
5005 /* Jump instructions types.  Indexed by INSN_UID.
5006    The same rtl insn can be expanded into different asm instructions
5007    depending on the cc0_status.  To properly determine type of jump
5008    instructions we scan instruction stream and map jumps types to this
5009    array.  */
5010 static enum attr_type *sched_branch_type;
5011
5012 /* Return the type of the jump insn.  */
5013 enum attr_type
5014 m68k_sched_branch_type (rtx insn)
5015 {
5016   enum attr_type type;
5017
5018   type = sched_branch_type[INSN_UID (insn)];
5019
5020   gcc_assert (type != 0);
5021
5022   return type;
5023 }
5024
5025 /* Implement type2 attribute.  */
5026 enum attr_type2
5027 m68k_sched_attr_type2 (rtx insn)
5028 {
5029   switch (get_attr_type1 (insn))
5030     {
5031     case TYPE1_ALU_L:
5032     case TYPE1_ALUQ_L:
5033     case TYPE1_CMP_L:
5034       return TYPE2_ALU;
5035
5036     case TYPE1_ALU_REG1:
5037     case TYPE1_ALU_REGX:
5038       return TYPE2_ALU_REG;
5039
5040     case TYPE1_BCC:
5041       return TYPE2_BCC;
5042
5043     case TYPE1_BRA:
5044       return TYPE2_BRA;
5045
5046     case TYPE1_BSR:
5047     case TYPE1_JSR:
5048       return TYPE2_CALL;
5049
5050     case TYPE1_JMP:
5051       return TYPE2_JMP;
5052
5053     case TYPE1_LEA:
5054       return TYPE2_LEA;
5055
5056     case TYPE1_CLR:
5057     case TYPE1_MOV3Q_L:
5058     case TYPE1_MOVE:
5059     case TYPE1_MOVEQ_L:
5060     case TYPE1_TST:
5061       switch (m68k_sched_cpu)
5062         {
5063         case CPU_CFV1:
5064           return TYPE2_OMOVE;
5065
5066         case CPU_CFV2:
5067         case CPU_CFV3:
5068           return TYPE2_ALU;
5069
5070         default:
5071           gcc_assert (get_attr_guess (insn) == GUESS_YES);
5072           return TYPE2_UNKNOWN;
5073         }
5074
5075     case TYPE1_MUL_L:
5076       return TYPE2_MUL_L;
5077
5078     case TYPE1_MUL_W:
5079       return TYPE2_MUL_W;
5080
5081     case TYPE1_MOVE_L:
5082     case TYPE1_TST_L:
5083       return TYPE2_OMOVE;
5084
5085     case TYPE1_PEA:
5086       return TYPE2_PEA;
5087
5088     case TYPE1_RTS:
5089       return TYPE2_RTS;
5090
5091     case TYPE1_UNLK:
5092       return TYPE2_UNLK;
5093
5094     default:
5095       gcc_assert (get_attr_guess (insn) == GUESS_YES);
5096       return TYPE2_UNKNOWN;
5097     }
5098 }
5099
5100 /* An empty state that is used in m68k_sched_adjust_cost.  */
5101 static state_t sched_adjust_cost_state;
5102
5103 /* Implement adjust_cost scheduler hook.
5104    Return adjusted COST of dependency LINK between DEF_INSN and INSN.  */
5105 static int
5106 m68k_sched_adjust_cost (rtx insn, rtx link ATTRIBUTE_UNUSED, rtx def_insn,
5107                         int cost)
5108 {
5109   int delay;
5110
5111   if (recog_memoized (def_insn) < 0
5112       || recog_memoized (insn) < 0)
5113     return cost;
5114
5115   /* Don't try to issue INSN earlier than DFA permits.
5116      This is especially useful for instructions that write to memory,
5117      as their true dependence (default) latency is better to be set to 0
5118      to workaround alias analysis limitations.
5119      This is, in fact, a machine independent tweak, so, probably,
5120      it should be moved to haifa-sched.c: insn_cost ().  */
5121
5122   delay = min_insn_conflict_delay (sched_adjust_cost_state, def_insn, insn);
5123   if (delay > cost)
5124     cost = delay;
5125
5126   return cost;
5127 }
5128
5129 /* Maximal length of instruction for current CPU.
5130    E.g. it is 3 for any ColdFire core.  */
5131 static int max_insn_size;
5132
5133 /* Data to model instruction buffer of CPU.  */
5134 struct _sched_ib
5135 {
5136   /* Size of the instruction buffer in words.  */
5137   int size;
5138
5139   /* Number of filled words in the instruction buffer.  */
5140   int filled;
5141
5142   /* Additional information about instruction buffer for CPUs that have
5143      a buffer of instruction records, rather then a plain buffer
5144      of instruction words.  */
5145   struct _sched_ib_records
5146   {
5147     /* Size of buffer in records.  */
5148     int n_insns;
5149
5150     /* Array to hold data on adjustements made to the size of the buffer.  */
5151     int *adjust;
5152
5153     /* Index of the above array.  */
5154     int adjust_index;
5155   } records;
5156
5157   /* An insn that reserves (marks empty) one word in the instruction buffer.  */
5158   rtx insn;
5159 };
5160
5161 static struct _sched_ib sched_ib;
5162
5163 /* ID of memory unit.  */
5164 static int sched_mem_unit_code;
5165
5166 /* Implementation of the targetm.sched.variable_issue () hook.
5167    It is called after INSN was issued.  It returns the number of insns
5168    that can possibly get scheduled on the current cycle.
5169    It is used here to determine the effect of INSN on the instruction
5170    buffer.  */
5171 static int
5172 m68k_sched_variable_issue (FILE *sched_dump ATTRIBUTE_UNUSED,
5173                            int sched_verbose ATTRIBUTE_UNUSED,
5174                            rtx insn, int can_issue_more)
5175 {
5176   int insn_size;
5177
5178   if (recog_memoized (insn) >= 0)
5179     {
5180       switch (m68k_sched_cpu)
5181         {
5182         case CPU_CFV1:
5183         case CPU_CFV2:
5184           insn_size = get_attr_size (insn);
5185           break;
5186
5187         case CPU_CFV3:
5188           insn_size = get_attr_size (insn);
5189           
5190           /* ColdFire V3 and V4 cores have instruction buffers that can
5191              accumulate up to 8 instructions regardless of instructions'
5192              sizes.  So we should take care not to "prefetch" 24 one-word
5193              or 12 two-words instructions.
5194              To model this behavior we temporarily decrease size of the
5195              buffer by (max_insn_size - insn_size) for next 7 instructions.  */
5196           {
5197             int adjust;
5198
5199             adjust = max_insn_size - insn_size;
5200             sched_ib.size -= adjust;
5201
5202             if (sched_ib.filled > sched_ib.size)
5203               sched_ib.filled = sched_ib.size;
5204
5205             sched_ib.records.adjust[sched_ib.records.adjust_index] = adjust;
5206           }
5207
5208           ++sched_ib.records.adjust_index;
5209           if (sched_ib.records.adjust_index == sched_ib.records.n_insns)
5210             sched_ib.records.adjust_index = 0;
5211
5212           /* Undo adjustement we did 7 instructions ago.  */
5213           sched_ib.size
5214             += sched_ib.records.adjust[sched_ib.records.adjust_index];
5215
5216           break;
5217
5218         default:
5219           gcc_unreachable ();
5220         }
5221
5222       gcc_assert (insn_size <= sched_ib.filled);
5223       --can_issue_more;
5224     }
5225   else if (GET_CODE (PATTERN (insn)) == ASM_INPUT
5226            || asm_noperands (PATTERN (insn)) >= 0)
5227     insn_size = sched_ib.filled;
5228   else
5229     insn_size = 0;
5230
5231   sched_ib.filled -= insn_size;
5232
5233   return can_issue_more;
5234 }
5235
5236 /* Statistics gatherer.  */
5237
5238 typedef enum
5239   {
5240     /* Something needs to be done for this insn.  */
5241     SCHED_DUMP_TODO,
5242
5243     /* Support for this insn is complete.  */
5244     SCHED_DUMP_DONE,
5245
5246     /* This insn didn't require much effort to support it.  */
5247     SCHED_DUMP_NOTHING
5248   } sched_dump_class_def;
5249
5250 /* Pointer to functions that classifies insns into 3 above classes.  */
5251 typedef sched_dump_class_def (*sched_dump_class_func_t) (rtx);
5252
5253 /* Return statistical type of INSN regarding splits.  */
5254 static sched_dump_class_def
5255 sched_dump_split_class (rtx insn)
5256 {
5257   int i;
5258
5259   i = recog_memoized (insn);
5260   gcc_assert (i >= 0);
5261
5262   switch (get_attr_split (insn))
5263     {
5264     case SPLIT_TODO:
5265       return SCHED_DUMP_TODO;
5266
5267     case SPLIT_DONE:
5268       return SCHED_DUMP_DONE;
5269
5270     case SPLIT_NOTHING:
5271       return SCHED_DUMP_NOTHING;
5272
5273     default:
5274       gcc_unreachable ();
5275     }
5276 }
5277
5278 /* ID of the guess unit.  */
5279 static int sched_dump_dfa_guess_unit_code;
5280
5281 /* DFA state for use in sched_dump_dfa_class ().  */
5282 static state_t sched_dump_dfa_state;
5283
5284 /* Return statistical type of INSN regarding DFA reservations.  */
5285 static sched_dump_class_def
5286 sched_dump_dfa_class (rtx insn)
5287 {
5288   int i;
5289
5290   i = recog_memoized (insn);
5291   gcc_assert (i >= 0 && insn_has_dfa_reservation_p (insn));
5292
5293   if (sched_dump_split_class (insn) == SCHED_DUMP_TODO)
5294     /* Insn is not yet ready for reservations.  */
5295     return SCHED_DUMP_NOTHING;
5296
5297   state_reset (sched_dump_dfa_state);
5298
5299   if (state_transition (sched_dump_dfa_state, insn) >= 0)
5300     gcc_unreachable ();
5301
5302   if (cpu_unit_reservation_p (sched_dump_dfa_state,
5303                               sched_dump_dfa_guess_unit_code))
5304     return SCHED_DUMP_TODO;
5305
5306   return SCHED_DUMP_DONE;
5307 }
5308
5309 /* Dump statistics on current function into file DUMP_FILENAME and prefix
5310    each entry with PREFIX.
5311    Instructions are classified with DUMP_CLASS.  */
5312 static void
5313 m68k_sched_dump (sched_dump_class_func_t dump_class,
5314                  const char *prefix, FILE *dump)
5315 {
5316   sbitmap present;
5317   int *todos;
5318   int *dones;
5319   int *nothings;
5320   rtx insn;
5321
5322   gcc_assert (dump != NULL);
5323
5324   present = sbitmap_alloc (CODE_FOR_nothing);
5325   sbitmap_zero (present);
5326
5327   todos = xcalloc (CODE_FOR_nothing, sizeof (*todos));
5328   dones = xcalloc (CODE_FOR_nothing, sizeof (*dones));
5329   nothings = xcalloc (CODE_FOR_nothing, sizeof (*nothings));
5330
5331   /* Gather statistics.  */
5332   for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
5333     {
5334       if (INSN_P (insn) && recog_memoized (insn) >= 0)
5335         {
5336           enum insn_code code;
5337
5338           code = INSN_CODE (insn);
5339           gcc_assert (code < CODE_FOR_nothing);
5340
5341           SET_BIT (present, code);
5342
5343           switch (dump_class (insn))
5344             {
5345             case SCHED_DUMP_TODO:
5346               ++todos[code];
5347               break;
5348
5349             case SCHED_DUMP_DONE:
5350               ++dones[code];
5351               break;
5352
5353             case SCHED_DUMP_NOTHING:
5354               ++nothings[code];
5355               break;
5356             }
5357         }
5358     }
5359
5360   /* Print statisctics.  */
5361   {
5362     unsigned int i;
5363     sbitmap_iterator si;
5364     int total_todo;
5365     int total_done;
5366     int total_nothing;
5367
5368     total_todo = 0;
5369     total_done = 0;
5370     total_nothing = 0;
5371
5372     EXECUTE_IF_SET_IN_SBITMAP (present, 0, i, si)
5373       {
5374         int todo;
5375         int done;
5376         int nothing;
5377         enum insn_code code;
5378
5379         code = (enum insn_code) i;
5380
5381         todo = todos[code];
5382         done = dones[code];
5383         nothing = nothings[code];
5384
5385         total_todo += todo;
5386         total_done += done;
5387         total_nothing += nothing;
5388
5389         if (todo != 0)
5390           {
5391             fprintf (dump,
5392                      "%s: %3d: %d / %d / %d ;",
5393                      prefix, code, todo, done, nothing);
5394
5395             {
5396               const char *name;
5397
5398               name = get_insn_name (code);
5399
5400               if (name != NULL)
5401                 fprintf (dump, " {%s}\n", name);
5402               else
5403                 fprintf (dump, " {unknown}\n");
5404             }
5405           }
5406       }
5407
5408     gcc_assert (CODE_FOR_nothing < 999);
5409
5410     fprintf (dump,
5411              "%s: 999: %d / %d / %d ; {total}\n",
5412              prefix, total_todo, total_done, total_nothing);
5413   }
5414
5415   free (nothings);
5416   nothings = NULL;
5417   free (dones);
5418   dones = NULL;
5419   free (todos);
5420   todos = NULL;
5421
5422   sbitmap_free (present);
5423   present = NULL;
5424 }
5425
5426 /* Implementation of targetm.sched.md_init_global () hook.
5427    It is invoked once per scheduling pass and is used here
5428    to initialize scheduler constants.  */
5429 static void
5430 m68k_sched_md_init_global (FILE *sched_dump ATTRIBUTE_UNUSED,
5431                            int sched_verbose ATTRIBUTE_UNUSED,
5432                            int n_insns ATTRIBUTE_UNUSED)
5433 {
5434   /* Init branch types.  */
5435   {
5436     rtx insn;
5437
5438     sched_branch_type = xcalloc (get_max_uid () + 1,
5439                                  sizeof (*sched_branch_type));
5440
5441     for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
5442       {
5443         if (JUMP_P (insn))
5444           /* !!! FIXME: Implement real scan here.  */
5445           sched_branch_type[INSN_UID (insn)] = TYPE_BCC;
5446       }
5447   }
5448
5449   if (reload_completed && sched_verbose >= 8)
5450     /* Dump statistics.  */
5451     {
5452       m68k_sched_dump (sched_dump_split_class, "m68k_sched_split",
5453                        sched_dump);
5454
5455       sched_dump_dfa_guess_unit_code = get_cpu_unit_code ("cf_guess");
5456       sched_dump_dfa_state = alloca (state_size ());
5457
5458       m68k_sched_dump (sched_dump_dfa_class, "m68k_sched_dfa",
5459                        sched_dump);
5460
5461       sched_dump_dfa_state = NULL;
5462       sched_dump_dfa_guess_unit_code = 0;
5463     }
5464
5465   /* Setup target cpu.  */
5466   switch (m68k_sched_cpu)
5467     {
5468     case CPU_CFV1:
5469     case CPU_CFV2:
5470       max_insn_size = 3;
5471       sched_ib.records.n_insns = 0;
5472       sched_ib.records.adjust = NULL;
5473       break;
5474
5475     case CPU_CFV3:
5476       max_insn_size = 3;
5477       sched_ib.records.n_insns = 8;
5478       sched_ib.records.adjust = xmalloc (sched_ib.records.n_insns
5479                                          * sizeof (*sched_ib.records.adjust));
5480       break;
5481
5482     default:
5483       gcc_unreachable ();
5484     }
5485
5486   sched_mem_unit_code = get_cpu_unit_code ("cf_mem1");
5487
5488   sched_adjust_cost_state = xmalloc (state_size ());
5489   state_reset (sched_adjust_cost_state);
5490
5491   start_sequence ();
5492   emit_insn (gen_ib ());
5493   sched_ib.insn = get_insns ();
5494   end_sequence ();
5495 }
5496
5497 /* Scheduling pass is now finished.  Free/reset static variables.  */
5498 static void
5499 m68k_sched_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
5500                              int verbose ATTRIBUTE_UNUSED)
5501 {
5502   sched_ib.insn = NULL;
5503
5504   free (sched_adjust_cost_state);
5505   sched_adjust_cost_state = NULL;
5506
5507   sched_mem_unit_code = 0;
5508
5509   free (sched_ib.records.adjust);
5510   sched_ib.records.adjust = NULL;
5511   sched_ib.records.n_insns = 0;
5512   max_insn_size = 0;
5513
5514   free (sched_branch_type);
5515   sched_branch_type = NULL;
5516 }
5517
5518 /* Implementation of targetm.sched.md_init () hook.
5519    It is invoked each time scheduler starts on the new block (basic block or
5520    extended basic block).  */
5521 static void
5522 m68k_sched_md_init (FILE *sched_dump ATTRIBUTE_UNUSED,
5523                     int sched_verbose ATTRIBUTE_UNUSED,
5524                     int n_insns ATTRIBUTE_UNUSED)
5525 {
5526   switch (m68k_sched_cpu)
5527     {
5528     case CPU_CFV1:
5529     case CPU_CFV2:
5530       sched_ib.size = 6;
5531       break;
5532
5533     case CPU_CFV3:
5534       sched_ib.size = sched_ib.records.n_insns * max_insn_size;
5535
5536       memset (sched_ib.records.adjust, 0,
5537               sched_ib.records.n_insns * sizeof (*sched_ib.records.adjust));
5538       sched_ib.records.adjust_index = 0;
5539       break;
5540
5541     default:
5542       gcc_unreachable ();
5543     }
5544
5545   /* haifa-sched.c: schedule_block () calls advance_cycle () just before
5546      the first cycle.  Workaround that.  */
5547   sched_ib.filled = -2;
5548 }
5549
5550 /* Implementation of targetm.sched.dfa_pre_advance_cycle () hook.
5551    It is invoked just before current cycle finishes and is used here
5552    to track if instruction buffer got its two words this cycle.  */
5553 static void
5554 m68k_sched_dfa_pre_advance_cycle (void)
5555 {
5556   if (!cpu_unit_reservation_p (curr_state, sched_mem_unit_code))
5557     {
5558       sched_ib.filled += 2;
5559
5560       if (sched_ib.filled > sched_ib.size)
5561         sched_ib.filled = sched_ib.size;
5562     }
5563 }
5564
5565 /* Implementation of targetm.sched.dfa_post_advance_cycle () hook.
5566    It is invoked just after new cycle begins and is used here
5567    to setup number of filled words in the instruction buffer so that
5568    instructions which won't have all their words prefetched would be
5569    stalled for a cycle.  */
5570 static void
5571 m68k_sched_dfa_post_advance_cycle (void)
5572 {
5573   int i;
5574
5575   /* Setup number of prefetched instruction words in the instruction
5576      buffer.  */
5577   i = max_insn_size - sched_ib.filled;
5578
5579   while (--i >= 0)
5580     {
5581       if (state_transition (curr_state, sched_ib.insn) >= 0)
5582         gcc_unreachable ();
5583     }
5584 }