OSDN Git Service

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