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