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