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