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