OSDN Git Service

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