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