200x-xx-xx Julian Brown <julian@codesourcery.com>
[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
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 2, 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 COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "function.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "recog.h"
38 #include "toplev.h"
39 #include "expr.h"
40 #include "reload.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "debug.h"
45 #include "flags.h"
46
47 enum reg_class regno_reg_class[] =
48 {
49 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
50 DATA_REGS, DATA_REGS, DATA_REGS, DATA_REGS,
51 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
52 ADDR_REGS, ADDR_REGS, ADDR_REGS, ADDR_REGS,
53 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
54 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
55 ADDR_REGS
56 };
57
58
59 /* The ASM_DOT macro allows easy string pasting to handle the differences
60 between MOTOROLA and MIT syntaxes in asm_fprintf(), which doesn't
61 support the %. option. */
62 #if MOTOROLA
63 # define ASM_DOT "."
64 # define ASM_DOTW ".w"
65 # define ASM_DOTL ".l"
66 #else
67 # define ASM_DOT ""
68 # define ASM_DOTW ""
69 # define ASM_DOTL ""
70 #endif
71
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 unsigned int reg_rev_mask;
89
90 /* FPU registers. */
91 int fpu_no;
92 unsigned int fpu_mask;
93 unsigned int fpu_rev_mask;
94
95 /* Offsets relative to ARG_POINTER. */
96 HOST_WIDE_INT frame_pointer_offset;
97 HOST_WIDE_INT stack_pointer_offset;
98
99 /* Function which the above information refers to. */
100 int funcdef_no;
101 };
102
103 /* Current frame information calculated by m68k_compute_frame_layout(). */
104 static struct m68k_frame current_frame;
105
106 static bool m68k_handle_option (size_t, const char *, int);
107 static rtx find_addr_reg (rtx);
108 static const char *singlemove_string (rtx *);
109 static void m68k_output_function_prologue (FILE *, HOST_WIDE_INT);
110 static void m68k_output_function_epilogue (FILE *, HOST_WIDE_INT);
111 #ifdef M68K_TARGET_COFF
112 static void m68k_coff_asm_named_section (const char *, unsigned int, tree);
113 #endif /* M68K_TARGET_COFF */
114 static void m68k_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
115 HOST_WIDE_INT, tree);
116 static rtx m68k_struct_value_rtx (tree, int);
117 static bool m68k_interrupt_function_p (tree func);
118 static tree m68k_handle_fndecl_attribute (tree *node, tree name,
119 tree args, int flags,
120 bool *no_add_attrs);
121 static void m68k_compute_frame_layout (void);
122 static bool m68k_save_reg (unsigned int regno, bool interrupt_handler);
123 static int const_int_cost (rtx);
124 static bool m68k_rtx_costs (rtx, int, int, int *);
125 \f
126
127 /* Specify the identification number of the library being built */
128 const char *m68k_library_id_string = "_current_shared_library_a5_offset_";
129
130 /* Nonzero if the last compare/test insn had FP operands. The
131 sCC expanders peek at this to determine what to do for the
132 68060, which has no fsCC instructions. */
133 int m68k_last_compare_had_fp_operands;
134 \f
135 /* Initialize the GCC target structure. */
136
137 #if INT_OP_GROUP == INT_OP_DOT_WORD
138 #undef TARGET_ASM_ALIGNED_HI_OP
139 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
140 #endif
141
142 #if INT_OP_GROUP == INT_OP_NO_DOT
143 #undef TARGET_ASM_BYTE_OP
144 #define TARGET_ASM_BYTE_OP "\tbyte\t"
145 #undef TARGET_ASM_ALIGNED_HI_OP
146 #define TARGET_ASM_ALIGNED_HI_OP "\tshort\t"
147 #undef TARGET_ASM_ALIGNED_SI_OP
148 #define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
149 #endif
150
151 #if INT_OP_GROUP == INT_OP_DC
152 #undef TARGET_ASM_BYTE_OP
153 #define TARGET_ASM_BYTE_OP "\tdc.b\t"
154 #undef TARGET_ASM_ALIGNED_HI_OP
155 #define TARGET_ASM_ALIGNED_HI_OP "\tdc.w\t"
156 #undef TARGET_ASM_ALIGNED_SI_OP
157 #define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
158 #endif
159
160 #undef TARGET_ASM_UNALIGNED_HI_OP
161 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
162 #undef TARGET_ASM_UNALIGNED_SI_OP
163 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
164
165 #undef TARGET_ASM_FUNCTION_PROLOGUE
166 #define TARGET_ASM_FUNCTION_PROLOGUE m68k_output_function_prologue
167 #undef TARGET_ASM_FUNCTION_EPILOGUE
168 #define TARGET_ASM_FUNCTION_EPILOGUE m68k_output_function_epilogue
169
170 #undef TARGET_ASM_OUTPUT_MI_THUNK
171 #define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
172 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
173 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
174
175 #undef TARGET_ASM_FILE_START_APP_OFF
176 #define TARGET_ASM_FILE_START_APP_OFF true
177
178 #undef TARGET_DEFAULT_TARGET_FLAGS
179 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_STRICT_ALIGNMENT)
180 #undef TARGET_HANDLE_OPTION
181 #define TARGET_HANDLE_OPTION m68k_handle_option
182
183 #undef TARGET_RTX_COSTS
184 #define TARGET_RTX_COSTS m68k_rtx_costs
185
186 #undef TARGET_ATTRIBUTE_TABLE
187 #define TARGET_ATTRIBUTE_TABLE m68k_attribute_table
188
189 #undef TARGET_PROMOTE_PROTOTYPES
190 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
191
192 #undef TARGET_STRUCT_VALUE_RTX
193 #define TARGET_STRUCT_VALUE_RTX m68k_struct_value_rtx
194
195 static const struct attribute_spec m68k_attribute_table[] =
196 {
197 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
198 { "interrupt_handler", 0, 0, true, false, false, m68k_handle_fndecl_attribute },
199 { NULL, 0, 0, false, false, false, NULL }
200 };
201
202 struct gcc_target targetm = TARGET_INITIALIZER;
203 \f
204 /* These bits are controlled by all CPU selection options. Many options
205 also control MASK_68881, but some (notably -m68020) leave it alone. */
206
207 #define MASK_ALL_CPU_BITS \
208 (MASK_COLDFIRE | MASK_CF_HWDIV | MASK_68060 | MASK_68040 \
209 | MASK_68040_ONLY | MASK_68030 | MASK_68020 | MASK_BITFIELD)
210
211 /* Implement TARGET_HANDLE_OPTION. */
212
213 static bool
214 m68k_handle_option (size_t code, const char *arg, int value)
215 {
216 switch (code)
217 {
218 case OPT_m5200:
219 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
220 target_flags |= MASK_5200;
221 return true;
222
223 case OPT_m5206e:
224 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
225 target_flags |= MASK_5200 | MASK_CF_HWDIV;
226 return true;
227
228 case OPT_m528x:
229 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
230 target_flags |= MASK_528x | MASK_CF_HWDIV;
231 return true;
232
233 case OPT_m5307:
234 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
235 target_flags |= MASK_CFV3 | MASK_CF_HWDIV;
236 return true;
237
238 case OPT_m5407:
239 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
240 target_flags |= MASK_CFV4 | MASK_CF_HWDIV;
241 return true;
242
243 case OPT_mcfv4e:
244 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
245 target_flags |= MASK_CFV4 | MASK_CF_HWDIV | MASK_CFV4E;
246 return true;
247
248 case OPT_m68000:
249 case OPT_mc68000:
250 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
251 return true;
252
253 case OPT_m68020:
254 case OPT_mc68020:
255 target_flags &= ~MASK_ALL_CPU_BITS;
256 target_flags |= MASK_68020 | MASK_BITFIELD;
257 return true;
258
259 case OPT_m68020_40:
260 target_flags &= ~MASK_ALL_CPU_BITS;
261 target_flags |= MASK_BITFIELD | MASK_68881 | MASK_68020 | MASK_68040;
262 return true;
263
264 case OPT_m68020_60:
265 target_flags &= ~MASK_ALL_CPU_BITS;
266 target_flags |= (MASK_BITFIELD | MASK_68881 | MASK_68020
267 | MASK_68040 | MASK_68060);
268 return true;
269
270 case OPT_m68030:
271 target_flags &= ~MASK_ALL_CPU_BITS;
272 target_flags |= MASK_68020 | MASK_68030 | MASK_BITFIELD;
273 return true;
274
275 case OPT_m68040:
276 target_flags &= ~MASK_ALL_CPU_BITS;
277 target_flags |= (MASK_68020 | MASK_68881 | MASK_BITFIELD
278 | MASK_68040_ONLY | MASK_68040);
279 return true;
280
281 case OPT_m68060:
282 target_flags &= ~MASK_ALL_CPU_BITS;
283 target_flags |= (MASK_68020 | MASK_68881 | MASK_BITFIELD
284 | MASK_68040_ONLY | MASK_68060);
285 return true;
286
287 case OPT_m68302:
288 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
289 return true;
290
291 case OPT_m68332:
292 case OPT_mcpu32:
293 target_flags &= ~(MASK_ALL_CPU_BITS | MASK_68881);
294 target_flags |= MASK_68020;
295 return true;
296
297 case OPT_mshared_library_id_:
298 if (value > MAX_LIBRARY_ID)
299 error ("-mshared-library-id=%s is not between 0 and %d",
300 arg, MAX_LIBRARY_ID);
301 else
302 asprintf ((char **) &m68k_library_id_string, "%d", (value * -4) - 4);
303 return true;
304
305 default:
306 return true;
307 }
308 }
309
310 /* Sometimes certain combinations of command options do not make
311 sense on a particular target machine. You can define a macro
312 `OVERRIDE_OPTIONS' to take account of this. This macro, if
313 defined, is executed once just after all the command options have
314 been parsed.
315
316 Don't use this macro to turn on various extra optimizations for
317 `-O'. That is what `OPTIMIZATION_OPTIONS' is for. */
318
319 void
320 override_options (void)
321 {
322 /* Sanity check to ensure that msep-data and mid-sahred-library are not
323 * both specified together. Doing so simply doesn't make sense.
324 */
325 if (TARGET_SEP_DATA && TARGET_ID_SHARED_LIBRARY)
326 error ("cannot specify both -msep-data and -mid-shared-library");
327
328 /* If we're generating code for a separate A5 relative data segment,
329 * we've got to enable -fPIC as well. This might be relaxable to
330 * -fpic but it hasn't been tested properly.
331 */
332 if (TARGET_SEP_DATA || TARGET_ID_SHARED_LIBRARY)
333 flag_pic = 2;
334
335 /* -fPIC uses 32-bit pc-relative displacements, which don't exist
336 until the 68020. */
337 if (!TARGET_68020 && !TARGET_COLDFIRE && (flag_pic == 2))
338 error ("-fPIC is not currently supported on the 68000 or 68010");
339
340 /* ??? A historic way of turning on pic, or is this intended to
341 be an embedded thing that doesn't have the same name binding
342 significance that it does on hosted ELF systems? */
343 if (TARGET_PCREL && flag_pic == 0)
344 flag_pic = 1;
345
346 /* Turn off function cse if we are doing PIC. We always want function call
347 to be done as `bsr foo@PLTPC', so it will force the assembler to create
348 the PLT entry for `foo'. Doing function cse will cause the address of
349 `foo' to be loaded into a register, which is exactly what we want to
350 avoid when we are doing PIC on svr4 m68k. */
351 if (flag_pic)
352 flag_no_function_cse = 1;
353
354 SUBTARGET_OVERRIDE_OPTIONS;
355 }
356 \f
357 /* Return nonzero if FUNC is an interrupt function as specified by the
358 "interrupt_handler" attribute. */
359 static bool
360 m68k_interrupt_function_p(tree func)
361 {
362 tree a;
363
364 if (TREE_CODE (func) != FUNCTION_DECL)
365 return false;
366
367 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
368 return (a != NULL_TREE);
369 }
370
371 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
372 struct attribute_spec.handler. */
373 static tree
374 m68k_handle_fndecl_attribute (tree *node, tree name,
375 tree args ATTRIBUTE_UNUSED,
376 int flags ATTRIBUTE_UNUSED,
377 bool *no_add_attrs)
378 {
379 if (TREE_CODE (*node) != FUNCTION_DECL)
380 {
381 warning (OPT_Wattributes, "%qs attribute only applies to functions",
382 IDENTIFIER_POINTER (name));
383 *no_add_attrs = true;
384 }
385
386 return NULL_TREE;
387 }
388
389 static void
390 m68k_compute_frame_layout (void)
391 {
392 int regno, saved;
393 unsigned int mask, rmask;
394 bool interrupt_handler = m68k_interrupt_function_p (current_function_decl);
395
396 /* Only compute the frame once per function.
397 Don't cache information until reload has been completed. */
398 if (current_frame.funcdef_no == current_function_funcdef_no
399 && reload_completed)
400 return;
401
402 current_frame.size = (get_frame_size () + 3) & -4;
403
404 mask = rmask = saved = 0;
405 for (regno = 0; regno < 16; regno++)
406 if (m68k_save_reg (regno, interrupt_handler))
407 {
408 mask |= 1 << regno;
409 rmask |= 1 << (15 - regno);
410 saved++;
411 }
412 current_frame.offset = saved * 4;
413 current_frame.reg_no = saved;
414 current_frame.reg_mask = mask;
415 current_frame.reg_rev_mask = rmask;
416
417 current_frame.foffset = 0;
418 mask = rmask = saved = 0;
419 if (TARGET_HARD_FLOAT)
420 {
421 for (regno = 16; regno < 24; regno++)
422 if (m68k_save_reg (regno, interrupt_handler))
423 {
424 mask |= 1 << (regno - 16);
425 rmask |= 1 << (23 - regno);
426 saved++;
427 }
428 current_frame.foffset = saved * TARGET_FP_REG_SIZE;
429 current_frame.offset += current_frame.foffset;
430 }
431 current_frame.fpu_no = saved;
432 current_frame.fpu_mask = mask;
433 current_frame.fpu_rev_mask = rmask;
434
435 /* Remember what function this frame refers to. */
436 current_frame.funcdef_no = current_function_funcdef_no;
437 }
438
439 HOST_WIDE_INT
440 m68k_initial_elimination_offset (int from, int to)
441 {
442 int argptr_offset;
443 /* The arg pointer points 8 bytes before the start of the arguments,
444 as defined by FIRST_PARM_OFFSET. This makes it coincident with the
445 frame pointer in most frames. */
446 argptr_offset = frame_pointer_needed ? 0 : UNITS_PER_WORD;
447 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
448 return argptr_offset;
449
450 m68k_compute_frame_layout ();
451
452 gcc_assert (to == STACK_POINTER_REGNUM);
453 switch (from)
454 {
455 case ARG_POINTER_REGNUM:
456 return current_frame.offset + current_frame.size - argptr_offset;
457 case FRAME_POINTER_REGNUM:
458 return current_frame.offset + current_frame.size;
459 default:
460 gcc_unreachable ();
461 }
462 }
463
464 /* Refer to the array `regs_ever_live' to determine which registers
465 to save; `regs_ever_live[I]' is nonzero if register number I
466 is ever used in the function. This function is responsible for
467 knowing which registers should not be saved even if used.
468 Return true if we need to save REGNO. */
469
470 static bool
471 m68k_save_reg (unsigned int regno, bool interrupt_handler)
472 {
473 if (flag_pic && regno == PIC_OFFSET_TABLE_REGNUM)
474 {
475 if (current_function_uses_pic_offset_table)
476 return true;
477 if (!current_function_is_leaf && TARGET_ID_SHARED_LIBRARY)
478 return true;
479 }
480
481 if (current_function_calls_eh_return)
482 {
483 unsigned int i;
484 for (i = 0; ; i++)
485 {
486 unsigned int test = EH_RETURN_DATA_REGNO (i);
487 if (test == INVALID_REGNUM)
488 break;
489 if (test == regno)
490 return true;
491 }
492 }
493
494 /* Fixed regs we never touch. */
495 if (fixed_regs[regno])
496 return false;
497
498 /* The frame pointer (if it is such) is handled specially. */
499 if (regno == FRAME_POINTER_REGNUM && frame_pointer_needed)
500 return false;
501
502 /* Interrupt handlers must also save call_used_regs
503 if they are live or when calling nested functions. */
504 if (interrupt_handler)
505 {
506 if (regs_ever_live[regno])
507 return true;
508
509 if (!current_function_is_leaf && call_used_regs[regno])
510 return true;
511 }
512
513 /* Never need to save registers that aren't touched. */
514 if (!regs_ever_live[regno])
515 return false;
516
517 /* Otherwise save everything that isn't call-clobbered. */
518 return !call_used_regs[regno];
519 }
520
521 /* This function generates the assembly code for function entry.
522 STREAM is a stdio stream to output the code to.
523 SIZE is an int: how many units of temporary storage to allocate. */
524
525 static void
526 m68k_output_function_prologue (FILE *stream,
527 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
528 {
529 HOST_WIDE_INT fsize_with_regs;
530 HOST_WIDE_INT cfa_offset = INCOMING_FRAME_SP_OFFSET;
531
532 m68k_compute_frame_layout();
533
534 /* If the stack limit is a symbol, we can check it here,
535 before actually allocating the space. */
536 if (current_function_limit_stack
537 && GET_CODE (stack_limit_rtx) == SYMBOL_REF)
538 asm_fprintf (stream, "\tcmp" ASM_DOT "l %I%s+%wd,%Rsp\n\ttrapcs\n",
539 XSTR (stack_limit_rtx, 0), current_frame.size + 4);
540
541 /* On ColdFire add register save into initial stack frame setup, if possible. */
542 fsize_with_regs = current_frame.size;
543 if (TARGET_COLDFIRE)
544 {
545 if (current_frame.reg_no > 2)
546 fsize_with_regs += current_frame.reg_no * 4;
547 if (current_frame.fpu_no)
548 fsize_with_regs += current_frame.fpu_no * 8;
549 }
550
551 if (frame_pointer_needed)
552 {
553 if (current_frame.size == 0 && TUNE_68040)
554 /* on the 68040, pea + move is faster than link.w 0 */
555 fprintf (stream, (MOTOROLA
556 ? "\tpea (%s)\n\tmove.l %s,%s\n"
557 : "\tpea %s@\n\tmovel %s,%s\n"),
558 M68K_REGNAME (FRAME_POINTER_REGNUM),
559 M68K_REGNAME (STACK_POINTER_REGNUM),
560 M68K_REGNAME (FRAME_POINTER_REGNUM));
561 else if (fsize_with_regs < 0x8000)
562 asm_fprintf (stream, "\tlink" ASM_DOTW " %s,%I%wd\n",
563 M68K_REGNAME (FRAME_POINTER_REGNUM), -fsize_with_regs);
564 else if (TARGET_68020)
565 asm_fprintf (stream, "\tlink" ASM_DOTL " %s,%I%wd\n",
566 M68K_REGNAME (FRAME_POINTER_REGNUM), -fsize_with_regs);
567 else
568 /* Adding negative number is faster on the 68040. */
569 asm_fprintf (stream,
570 "\tlink" ASM_DOTW " %s,%I0\n"
571 "\tadd" ASM_DOT "l %I%wd,%Rsp\n",
572 M68K_REGNAME (FRAME_POINTER_REGNUM), -fsize_with_regs);
573 }
574 else if (fsize_with_regs) /* !frame_pointer_needed */
575 {
576 if (fsize_with_regs < 0x8000)
577 {
578 if (fsize_with_regs <= 8)
579 {
580 if (!TARGET_COLDFIRE)
581 asm_fprintf (stream, "\tsubq" ASM_DOT "w %I%wd,%Rsp\n",
582 fsize_with_regs);
583 else
584 asm_fprintf (stream, "\tsubq" ASM_DOT "l %I%wd,%Rsp\n",
585 fsize_with_regs);
586 }
587 else if (fsize_with_regs <= 16 && TUNE_CPU32)
588 /* On the CPU32 it is faster to use two subqw instructions to
589 subtract a small integer (8 < N <= 16) to a register. */
590 asm_fprintf (stream,
591 "\tsubq" ASM_DOT "w %I8,%Rsp\n"
592 "\tsubq" ASM_DOT "w %I%wd,%Rsp\n",
593 fsize_with_regs - 8);
594 else if (TUNE_68040)
595 /* Adding negative number is faster on the 68040. */
596 asm_fprintf (stream, "\tadd" ASM_DOT "w %I%wd,%Rsp\n",
597 -fsize_with_regs);
598 else
599 asm_fprintf (stream, (MOTOROLA
600 ? "\tlea (%wd,%Rsp),%Rsp\n"
601 : "\tlea %Rsp@(%wd),%Rsp\n"),
602 -fsize_with_regs);
603 }
604 else /* fsize_with_regs >= 0x8000 */
605 asm_fprintf (stream, "\tadd" ASM_DOT "l %I%wd,%Rsp\n",
606 -fsize_with_regs);
607 } /* !frame_pointer_needed */
608
609 if (dwarf2out_do_frame ())
610 {
611 if (frame_pointer_needed)
612 {
613 char *l;
614 l = (char *) dwarf2out_cfi_label ();
615 cfa_offset += 4;
616 dwarf2out_reg_save (l, FRAME_POINTER_REGNUM, -cfa_offset);
617 dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, cfa_offset);
618 cfa_offset += current_frame.size;
619 }
620 else
621 {
622 cfa_offset += current_frame.size;
623 dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, cfa_offset);
624 }
625 }
626
627 if (current_frame.fpu_mask)
628 {
629 if (TARGET_68881)
630 {
631 asm_fprintf (stream, (MOTOROLA
632 ? "\tfmovm %I0x%x,-(%Rsp)\n"
633 : "\tfmovem %I0x%x,%Rsp@-\n"),
634 current_frame.fpu_mask);
635 }
636 else
637 {
638 int offset;
639
640 /* stack already has registers in it. Find the offset from
641 the bottom of stack to where the FP registers go */
642 if (current_frame.reg_no <= 2)
643 offset = 0;
644 else
645 offset = current_frame.reg_no * 4;
646 if (offset)
647 asm_fprintf (stream,
648 "\tfmovem %I0x%x,%d(%Rsp)\n",
649 current_frame.fpu_rev_mask,
650 offset);
651 else
652 asm_fprintf (stream,
653 "\tfmovem %I0x%x,(%Rsp)\n",
654 current_frame.fpu_rev_mask);
655 }
656
657 if (dwarf2out_do_frame ())
658 {
659 char *l = (char *) dwarf2out_cfi_label ();
660 int n_regs, regno;
661
662 cfa_offset += current_frame.fpu_no * TARGET_FP_REG_SIZE;
663 if (! frame_pointer_needed)
664 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
665 for (regno = 16, n_regs = 0; regno < 24; regno++)
666 if (current_frame.fpu_mask & (1 << (regno - 16)))
667 dwarf2out_reg_save (l, regno, -cfa_offset
668 + n_regs++ * TARGET_FP_REG_SIZE);
669 }
670 }
671
672 /* If the stack limit is not a symbol, check it here.
673 This has the disadvantage that it may be too late... */
674 if (current_function_limit_stack)
675 {
676 if (REG_P (stack_limit_rtx))
677 asm_fprintf (stream, "\tcmp" ASM_DOT "l %s,%Rsp\n\ttrapcs\n",
678 M68K_REGNAME (REGNO (stack_limit_rtx)));
679 else if (GET_CODE (stack_limit_rtx) != SYMBOL_REF)
680 warning (0, "stack limit expression is not supported");
681 }
682
683 if (current_frame.reg_no <= 2)
684 {
685 /* Store each separately in the same order moveml uses.
686 Using two movel instructions instead of a single moveml
687 is about 15% faster for the 68020 and 68030 at no expense
688 in code size. */
689
690 int i;
691
692 for (i = 0; i < 16; i++)
693 if (current_frame.reg_rev_mask & (1 << i))
694 {
695 asm_fprintf (stream, (MOTOROLA
696 ? "\t%Omove.l %s,-(%Rsp)\n"
697 : "\tmovel %s,%Rsp@-\n"),
698 M68K_REGNAME (15 - i));
699 if (dwarf2out_do_frame ())
700 {
701 char *l = (char *) dwarf2out_cfi_label ();
702
703 cfa_offset += 4;
704 if (! frame_pointer_needed)
705 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
706 dwarf2out_reg_save (l, 15 - i, -cfa_offset);
707 }
708 }
709 }
710 else if (current_frame.reg_rev_mask)
711 {
712 if (TARGET_COLDFIRE)
713 /* The ColdFire does not support the predecrement form of the
714 MOVEM instruction, so we must adjust the stack pointer and
715 then use the plain address register indirect mode.
716 The required register save space was combined earlier with
717 the fsize_with_regs amount. */
718
719 asm_fprintf (stream, (MOTOROLA
720 ? "\tmovm.l %I0x%x,(%Rsp)\n"
721 : "\tmoveml %I0x%x,%Rsp@\n"),
722 current_frame.reg_mask);
723 else
724 asm_fprintf (stream, (MOTOROLA
725 ? "\tmovm.l %I0x%x,-(%Rsp)\n"
726 : "\tmoveml %I0x%x,%Rsp@-\n"),
727 current_frame.reg_rev_mask);
728 if (dwarf2out_do_frame ())
729 {
730 char *l = (char *) dwarf2out_cfi_label ();
731 int n_regs, regno;
732
733 cfa_offset += current_frame.reg_no * 4;
734 if (! frame_pointer_needed)
735 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, cfa_offset);
736 for (regno = 0, n_regs = 0; regno < 16; regno++)
737 if (current_frame.reg_mask & (1 << regno))
738 dwarf2out_reg_save (l, regno, -cfa_offset + n_regs++ * 4);
739 }
740 }
741 if (!TARGET_SEP_DATA && flag_pic
742 && (current_function_uses_pic_offset_table
743 || (!current_function_is_leaf && TARGET_ID_SHARED_LIBRARY)))
744 {
745 if (TARGET_ID_SHARED_LIBRARY)
746 {
747 asm_fprintf (stream, "\tmovel %s@(%s), %s\n",
748 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM),
749 m68k_library_id_string,
750 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM));
751 }
752 else
753 {
754 if (MOTOROLA)
755 asm_fprintf (stream,
756 "\t%Olea (%Rpc, %U_GLOBAL_OFFSET_TABLE_@GOTPC), %s\n",
757 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM));
758 else
759 {
760 asm_fprintf (stream, "\tmovel %I%U_GLOBAL_OFFSET_TABLE_, %s\n",
761 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM));
762 asm_fprintf (stream, "\tlea %Rpc@(0,%s:l),%s\n",
763 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM),
764 M68K_REGNAME (PIC_OFFSET_TABLE_REGNUM));
765 }
766 }
767 }
768 }
769 \f
770 /* Return true if this function's epilogue can be output as RTL. */
771
772 bool
773 use_return_insn (void)
774 {
775 if (!reload_completed || frame_pointer_needed || get_frame_size () != 0)
776 return false;
777
778 /* We can output the epilogue as RTL only if no registers need to be
779 restored. */
780 m68k_compute_frame_layout ();
781 return current_frame.reg_no ? false : true;
782 }
783
784 /* This function generates the assembly code for function exit,
785 on machines that need it.
786
787 The function epilogue should not depend on the current stack pointer!
788 It should use the frame pointer only, if there is a frame pointer.
789 This is mandatory because of alloca; we also take advantage of it to
790 omit stack adjustments before returning. */
791
792 static void
793 m68k_output_function_epilogue (FILE *stream,
794 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
795 {
796 HOST_WIDE_INT fsize, fsize_with_regs;
797 bool big = false;
798 bool restore_from_sp = false;
799 rtx insn = get_last_insn ();
800
801 m68k_compute_frame_layout ();
802
803 /* If the last insn was a BARRIER, we don't have to write any code. */
804 if (GET_CODE (insn) == NOTE)
805 insn = prev_nonnote_insn (insn);
806 if (insn && GET_CODE (insn) == BARRIER)
807 {
808 /* Output just a no-op so that debuggers don't get confused
809 about which function the pc is in at this address. */
810 fprintf (stream, "\tnop\n");
811 return;
812 }
813
814 #ifdef FUNCTION_EXTRA_EPILOGUE
815 FUNCTION_EXTRA_EPILOGUE (stream, size);
816 #endif
817
818 fsize = current_frame.size;
819
820 /* FIXME: leaf_function_p below is too strong.
821 What we really need to know there is if there could be pending
822 stack adjustment needed at that point. */
823 restore_from_sp
824 = (! frame_pointer_needed
825 || (! current_function_calls_alloca && leaf_function_p ()));
826
827 /* fsize_with_regs is the size we need to adjust the sp when
828 popping the frame. */
829 fsize_with_regs = fsize;
830
831 /* Because the ColdFire doesn't support moveml with
832 complex address modes, we must adjust the stack manually
833 after restoring registers. When the frame pointer isn't used,
834 we can merge movem adjustment into frame unlinking
835 made immediately after it. */
836 if (TARGET_COLDFIRE && restore_from_sp)
837 {
838 if (current_frame.reg_no > 2)
839 fsize_with_regs += current_frame.reg_no * 4;
840 if (current_frame.fpu_no)
841 fsize_with_regs += current_frame.fpu_no * 8;
842 }
843
844 if (current_frame.offset + fsize >= 0x8000
845 && ! restore_from_sp
846 && (current_frame.reg_mask || current_frame.fpu_mask))
847 {
848 /* Because the ColdFire doesn't support moveml with
849 complex address modes we make an extra correction here. */
850 if (TARGET_COLDFIRE)
851 fsize += current_frame.offset;
852
853 asm_fprintf (stream, "\t%Omove" ASM_DOT "l %I%wd,%Ra1\n", -fsize);
854 fsize = 0, big = true;
855 }
856 if (current_frame.reg_no <= 2)
857 {
858 /* Restore each separately in the same order moveml does.
859 Using two movel instructions instead of a single moveml
860 is about 15% faster for the 68020 and 68030 at no expense
861 in code size. */
862
863 int i;
864 HOST_WIDE_INT offset = current_frame.offset + fsize;
865
866 for (i = 0; i < 16; i++)
867 if (current_frame.reg_mask & (1 << i))
868 {
869 if (big)
870 {
871 if (MOTOROLA)
872 asm_fprintf (stream, "\t%Omove.l -%wd(%s,%Ra1.l),%s\n",
873 offset,
874 M68K_REGNAME (FRAME_POINTER_REGNUM),
875 M68K_REGNAME (i));
876 else
877 asm_fprintf (stream, "\tmovel %s@(-%wd,%Ra1:l),%s\n",
878 M68K_REGNAME (FRAME_POINTER_REGNUM),
879 offset,
880 M68K_REGNAME (i));
881 }
882 else if (restore_from_sp)
883 asm_fprintf (stream, (MOTOROLA
884 ? "\t%Omove.l (%Rsp)+,%s\n"
885 : "\tmovel %Rsp@+,%s\n"),
886 M68K_REGNAME (i));
887 else
888 {
889 if (MOTOROLA)
890 asm_fprintf (stream, "\t%Omove.l -%wd(%s),%s\n",
891 offset,
892 M68K_REGNAME (FRAME_POINTER_REGNUM),
893 M68K_REGNAME (i));
894 else
895 asm_fprintf (stream, "\tmovel %s@(-%wd),%s\n",
896 M68K_REGNAME (FRAME_POINTER_REGNUM),
897 offset,
898 M68K_REGNAME (i));
899 }
900 offset -= 4;
901 }
902 }
903 else if (current_frame.reg_mask)
904 {
905 /* The ColdFire requires special handling due to its limited moveml
906 insn. */
907 if (TARGET_COLDFIRE)
908 {
909 if (big)
910 {
911 asm_fprintf (stream, "\tadd" ASM_DOT "l %s,%Ra1\n",
912 M68K_REGNAME (FRAME_POINTER_REGNUM));
913 asm_fprintf (stream, (MOTOROLA
914 ? "\tmovm.l (%Ra1),%I0x%x\n"
915 : "\tmoveml %Ra1@,%I0x%x\n"),
916 current_frame.reg_mask);
917 }
918 else if (restore_from_sp)
919 asm_fprintf (stream, (MOTOROLA
920 ? "\tmovm.l (%Rsp),%I0x%x\n"
921 : "\tmoveml %Rsp@,%I0x%x\n"),
922 current_frame.reg_mask);
923 else
924 {
925 if (MOTOROLA)
926 asm_fprintf (stream, "\tmovm.l -%wd(%s),%I0x%x\n",
927 current_frame.offset + fsize,
928 M68K_REGNAME (FRAME_POINTER_REGNUM),
929 current_frame.reg_mask);
930 else
931 asm_fprintf (stream, "\tmoveml %s@(-%wd),%I0x%x\n",
932 M68K_REGNAME (FRAME_POINTER_REGNUM),
933 current_frame.offset + fsize,
934 current_frame.reg_mask);
935 }
936 }
937 else /* !TARGET_COLDFIRE */
938 {
939 if (big)
940 {
941 if (MOTOROLA)
942 asm_fprintf (stream, "\tmovm.l -%wd(%s,%Ra1.l),%I0x%x\n",
943 current_frame.offset + fsize,
944 M68K_REGNAME (FRAME_POINTER_REGNUM),
945 current_frame.reg_mask);
946 else
947 asm_fprintf (stream, "\tmoveml %s@(-%wd,%Ra1:l),%I0x%x\n",
948 M68K_REGNAME (FRAME_POINTER_REGNUM),
949 current_frame.offset + fsize,
950 current_frame.reg_mask);
951 }
952 else if (restore_from_sp)
953 {
954 asm_fprintf (stream, (MOTOROLA
955 ? "\tmovm.l (%Rsp)+,%I0x%x\n"
956 : "\tmoveml %Rsp@+,%I0x%x\n"),
957 current_frame.reg_mask);
958 }
959 else
960 {
961 if (MOTOROLA)
962 asm_fprintf (stream, "\tmovm.l -%wd(%s),%I0x%x\n",
963 current_frame.offset + fsize,
964 M68K_REGNAME (FRAME_POINTER_REGNUM),
965 current_frame.reg_mask);
966 else
967 asm_fprintf (stream, "\tmoveml %s@(-%wd),%I0x%x\n",
968 M68K_REGNAME (FRAME_POINTER_REGNUM),
969 current_frame.offset + fsize,
970 current_frame.reg_mask);
971 }
972 }
973 }
974 if (current_frame.fpu_rev_mask)
975 {
976 if (big)
977 {
978 if (TARGET_COLDFIRE)
979 {
980 if (current_frame.reg_no)
981 asm_fprintf (stream, MOTOROLA ?
982 "\tfmovem.d %d(%Ra1),%I0x%x\n" :
983 "\tfmovmd (%d,%Ra1),%I0x%x\n",
984 current_frame.reg_no * 4,
985 current_frame.fpu_rev_mask);
986 else
987 asm_fprintf (stream, MOTOROLA ?
988 "\tfmovem.d (%Ra1),%I0x%x\n" :
989 "\tfmovmd (%Ra1),%I0x%x\n",
990 current_frame.fpu_rev_mask);
991 }
992 else if (MOTOROLA)
993 asm_fprintf (stream, "\tfmovm -%wd(%s,%Ra1.l),%I0x%x\n",
994 current_frame.foffset + fsize,
995 M68K_REGNAME (FRAME_POINTER_REGNUM),
996 current_frame.fpu_rev_mask);
997 else
998 asm_fprintf (stream, "\tfmovem %s@(-%wd,%Ra1:l),%I0x%x\n",
999 M68K_REGNAME (FRAME_POINTER_REGNUM),
1000 current_frame.foffset + fsize,
1001 current_frame.fpu_rev_mask);
1002 }
1003 else if (restore_from_sp)
1004 {
1005 if (TARGET_COLDFIRE)
1006 {
1007 int offset;
1008
1009 /* Stack already has registers in it. Find the offset from
1010 the bottom of stack to where the FP registers go. */
1011 if (current_frame.reg_no <= 2)
1012 offset = 0;
1013 else
1014 offset = current_frame.reg_no * 4;
1015 if (offset)
1016 asm_fprintf (stream,
1017 "\tfmovem %Rsp@(%d), %I0x%x\n",
1018 offset, current_frame.fpu_rev_mask);
1019 else
1020 asm_fprintf (stream,
1021 "\tfmovem %Rsp@, %I0x%x\n",
1022 current_frame.fpu_rev_mask);
1023 }
1024 else
1025 asm_fprintf (stream, MOTOROLA ?
1026 "\tfmovm (%Rsp)+,%I0x%x\n" :
1027 "\tfmovem %Rsp@+,%I0x%x\n",
1028 current_frame.fpu_rev_mask);
1029 }
1030 else
1031 {
1032 if (MOTOROLA && !TARGET_COLDFIRE)
1033 asm_fprintf (stream, "\tfmovm -%wd(%s),%I0x%x\n",
1034 current_frame.foffset + fsize,
1035 M68K_REGNAME (FRAME_POINTER_REGNUM),
1036 current_frame.fpu_rev_mask);
1037 else
1038 asm_fprintf (stream, "\tfmovem %s@(-%wd),%I0x%x\n",
1039 M68K_REGNAME (FRAME_POINTER_REGNUM),
1040 current_frame.foffset + fsize,
1041 current_frame.fpu_rev_mask);
1042 }
1043 }
1044 if (frame_pointer_needed)
1045 fprintf (stream, "\tunlk %s\n", M68K_REGNAME (FRAME_POINTER_REGNUM));
1046 else if (fsize_with_regs)
1047 {
1048 if (fsize_with_regs <= 8)
1049 {
1050 if (!TARGET_COLDFIRE)
1051 asm_fprintf (stream, "\taddq" ASM_DOT "w %I%wd,%Rsp\n",
1052 fsize_with_regs);
1053 else
1054 asm_fprintf (stream, "\taddq" ASM_DOT "l %I%wd,%Rsp\n",
1055 fsize_with_regs);
1056 }
1057 else if (fsize_with_regs <= 16 && TUNE_CPU32)
1058 {
1059 /* On the CPU32 it is faster to use two addqw instructions to
1060 add a small integer (8 < N <= 16) to a register. */
1061 asm_fprintf (stream,
1062 "\taddq" ASM_DOT "w %I8,%Rsp\n"
1063 "\taddq" ASM_DOT "w %I%wd,%Rsp\n",
1064 fsize_with_regs - 8);
1065 }
1066 else if (fsize_with_regs < 0x8000)
1067 {
1068 if (TUNE_68040)
1069 asm_fprintf (stream, "\tadd" ASM_DOT "w %I%wd,%Rsp\n",
1070 fsize_with_regs);
1071 else
1072 asm_fprintf (stream, (MOTOROLA
1073 ? "\tlea (%wd,%Rsp),%Rsp\n"
1074 : "\tlea %Rsp@(%wd),%Rsp\n"),
1075 fsize_with_regs);
1076 }
1077 else
1078 asm_fprintf (stream, "\tadd" ASM_DOT "l %I%wd,%Rsp\n", fsize_with_regs);
1079 }
1080 if (current_function_calls_eh_return)
1081 asm_fprintf (stream, "\tadd" ASM_DOT "l %Ra0,%Rsp\n");
1082 if (m68k_interrupt_function_p (current_function_decl))
1083 fprintf (stream, "\trte\n");
1084 else if (current_function_pops_args)
1085 asm_fprintf (stream, "\trtd %I%d\n", current_function_pops_args);
1086 else
1087 fprintf (stream, "\trts\n");
1088 }
1089 \f
1090 /* Return true if X is a valid comparison operator for the dbcc
1091 instruction.
1092
1093 Note it rejects floating point comparison operators.
1094 (In the future we could use Fdbcc).
1095
1096 It also rejects some comparisons when CC_NO_OVERFLOW is set. */
1097
1098 int
1099 valid_dbcc_comparison_p_2 (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1100 {
1101 switch (GET_CODE (x))
1102 {
1103 case EQ: case NE: case GTU: case LTU:
1104 case GEU: case LEU:
1105 return 1;
1106
1107 /* Reject some when CC_NO_OVERFLOW is set. This may be over
1108 conservative */
1109 case GT: case LT: case GE: case LE:
1110 return ! (cc_prev_status.flags & CC_NO_OVERFLOW);
1111 default:
1112 return 0;
1113 }
1114 }
1115
1116 /* Return nonzero if flags are currently in the 68881 flag register. */
1117 int
1118 flags_in_68881 (void)
1119 {
1120 /* We could add support for these in the future */
1121 return cc_status.flags & CC_IN_68881;
1122 }
1123
1124 /* Output a BSR instruction suitable for PIC code. */
1125 void
1126 m68k_output_pic_call (rtx dest)
1127 {
1128 const char *out;
1129
1130 if (!(GET_CODE (dest) == MEM && GET_CODE (XEXP (dest, 0)) == SYMBOL_REF))
1131 out = "jsr %0";
1132 /* We output a BSR instruction if we're building for a target that
1133 supports long branches. Otherwise we generate one of two sequences:
1134 a shorter one that uses a GOT entry or a longer one that doesn't.
1135 We'll use the -Os command-line flag to decide which to generate.
1136 Both sequences take the same time to execute on the ColdFire. */
1137 else if (TARGET_PCREL)
1138 out = "bsr.l %o0";
1139 else if (TARGET_68020)
1140 #if defined(USE_GAS)
1141 out = "bsr.l %0@PLTPC";
1142 #else
1143 out = "bsr %0@PLTPC";
1144 #endif
1145 else if (optimize_size || TARGET_ID_SHARED_LIBRARY)
1146 out = "move.l %0@GOT(%%a5), %%a1\n\tjsr (%%a1)";
1147 else
1148 out = "lea %0-.-8,%%a1\n\tjsr 0(%%pc,%%a1)";
1149
1150 output_asm_insn (out, &dest);
1151 }
1152
1153 /* Output a dbCC; jCC sequence. Note we do not handle the
1154 floating point version of this sequence (Fdbcc). We also
1155 do not handle alternative conditions when CC_NO_OVERFLOW is
1156 set. It is assumed that valid_dbcc_comparison_p and flags_in_68881 will
1157 kick those out before we get here. */
1158
1159 void
1160 output_dbcc_and_branch (rtx *operands)
1161 {
1162 switch (GET_CODE (operands[3]))
1163 {
1164 case EQ:
1165 output_asm_insn (MOTOROLA
1166 ? "dbeq %0,%l1\n\tjbeq %l2"
1167 : "dbeq %0,%l1\n\tjeq %l2",
1168 operands);
1169 break;
1170
1171 case NE:
1172 output_asm_insn (MOTOROLA
1173 ? "dbne %0,%l1\n\tjbne %l2"
1174 : "dbne %0,%l1\n\tjne %l2",
1175 operands);
1176 break;
1177
1178 case GT:
1179 output_asm_insn (MOTOROLA
1180 ? "dbgt %0,%l1\n\tjbgt %l2"
1181 : "dbgt %0,%l1\n\tjgt %l2",
1182 operands);
1183 break;
1184
1185 case GTU:
1186 output_asm_insn (MOTOROLA
1187 ? "dbhi %0,%l1\n\tjbhi %l2"
1188 : "dbhi %0,%l1\n\tjhi %l2",
1189 operands);
1190 break;
1191
1192 case LT:
1193 output_asm_insn (MOTOROLA
1194 ? "dblt %0,%l1\n\tjblt %l2"
1195 : "dblt %0,%l1\n\tjlt %l2",
1196 operands);
1197 break;
1198
1199 case LTU:
1200 output_asm_insn (MOTOROLA
1201 ? "dbcs %0,%l1\n\tjbcs %l2"
1202 : "dbcs %0,%l1\n\tjcs %l2",
1203 operands);
1204 break;
1205
1206 case GE:
1207 output_asm_insn (MOTOROLA
1208 ? "dbge %0,%l1\n\tjbge %l2"
1209 : "dbge %0,%l1\n\tjge %l2",
1210 operands);
1211 break;
1212
1213 case GEU:
1214 output_asm_insn (MOTOROLA
1215 ? "dbcc %0,%l1\n\tjbcc %l2"
1216 : "dbcc %0,%l1\n\tjcc %l2",
1217 operands);
1218 break;
1219
1220 case LE:
1221 output_asm_insn (MOTOROLA
1222 ? "dble %0,%l1\n\tjble %l2"
1223 : "dble %0,%l1\n\tjle %l2",
1224 operands);
1225 break;
1226
1227 case LEU:
1228 output_asm_insn (MOTOROLA
1229 ? "dbls %0,%l1\n\tjbls %l2"
1230 : "dbls %0,%l1\n\tjls %l2",
1231 operands);
1232 break;
1233
1234 default:
1235 gcc_unreachable ();
1236 }
1237
1238 /* If the decrement is to be done in SImode, then we have
1239 to compensate for the fact that dbcc decrements in HImode. */
1240 switch (GET_MODE (operands[0]))
1241 {
1242 case SImode:
1243 output_asm_insn (MOTOROLA
1244 ? "clr%.w %0\n\tsubq%.l #1,%0\n\tjbpl %l1"
1245 : "clr%.w %0\n\tsubq%.l #1,%0\n\tjpl %l1",
1246 operands);
1247 break;
1248
1249 case HImode:
1250 break;
1251
1252 default:
1253 gcc_unreachable ();
1254 }
1255 }
1256
1257 const char *
1258 output_scc_di (rtx op, rtx operand1, rtx operand2, rtx dest)
1259 {
1260 rtx loperands[7];
1261 enum rtx_code op_code = GET_CODE (op);
1262
1263 /* This does not produce a useful cc. */
1264 CC_STATUS_INIT;
1265
1266 /* The m68k cmp.l instruction requires operand1 to be a reg as used
1267 below. Swap the operands and change the op if these requirements
1268 are not fulfilled. */
1269 if (GET_CODE (operand2) == REG && GET_CODE (operand1) != REG)
1270 {
1271 rtx tmp = operand1;
1272
1273 operand1 = operand2;
1274 operand2 = tmp;
1275 op_code = swap_condition (op_code);
1276 }
1277 loperands[0] = operand1;
1278 if (GET_CODE (operand1) == REG)
1279 loperands[1] = gen_rtx_REG (SImode, REGNO (operand1) + 1);
1280 else
1281 loperands[1] = adjust_address (operand1, SImode, 4);
1282 if (operand2 != const0_rtx)
1283 {
1284 loperands[2] = operand2;
1285 if (GET_CODE (operand2) == REG)
1286 loperands[3] = gen_rtx_REG (SImode, REGNO (operand2) + 1);
1287 else
1288 loperands[3] = adjust_address (operand2, SImode, 4);
1289 }
1290 loperands[4] = gen_label_rtx ();
1291 if (operand2 != const0_rtx)
1292 {
1293 output_asm_insn (MOTOROLA
1294 ? "cmp%.l %2,%0\n\tjbne %l4\n\tcmp%.l %3,%1"
1295 : "cmp%.l %2,%0\n\tjne %l4\n\tcmp%.l %3,%1",
1296 loperands);
1297 }
1298 else
1299 {
1300 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[0]))
1301 output_asm_insn ("tst%.l %0", loperands);
1302 else
1303 output_asm_insn ("cmp%.w #0,%0", loperands);
1304
1305 output_asm_insn (MOTOROLA ? "jbne %l4" : "jne %l4", loperands);
1306
1307 if (TARGET_68020 || TARGET_COLDFIRE || ! ADDRESS_REG_P (loperands[1]))
1308 output_asm_insn ("tst%.l %1", loperands);
1309 else
1310 output_asm_insn ("cmp%.w #0,%1", loperands);
1311 }
1312
1313 loperands[5] = dest;
1314
1315 switch (op_code)
1316 {
1317 case EQ:
1318 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1319 CODE_LABEL_NUMBER (loperands[4]));
1320 output_asm_insn ("seq %5", loperands);
1321 break;
1322
1323 case NE:
1324 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1325 CODE_LABEL_NUMBER (loperands[4]));
1326 output_asm_insn ("sne %5", loperands);
1327 break;
1328
1329 case GT:
1330 loperands[6] = gen_label_rtx ();
1331 output_asm_insn (MOTOROLA ? "shi %5\n\tjbra %l6" : "shi %5\n\tjra %l6",
1332 loperands);
1333 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1334 CODE_LABEL_NUMBER (loperands[4]));
1335 output_asm_insn ("sgt %5", loperands);
1336 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1337 CODE_LABEL_NUMBER (loperands[6]));
1338 break;
1339
1340 case GTU:
1341 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1342 CODE_LABEL_NUMBER (loperands[4]));
1343 output_asm_insn ("shi %5", loperands);
1344 break;
1345
1346 case LT:
1347 loperands[6] = gen_label_rtx ();
1348 output_asm_insn (MOTOROLA ? "scs %5\n\tjbra %l6" : "scs %5\n\tjra %l6",
1349 loperands);
1350 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1351 CODE_LABEL_NUMBER (loperands[4]));
1352 output_asm_insn ("slt %5", loperands);
1353 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1354 CODE_LABEL_NUMBER (loperands[6]));
1355 break;
1356
1357 case LTU:
1358 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1359 CODE_LABEL_NUMBER (loperands[4]));
1360 output_asm_insn ("scs %5", loperands);
1361 break;
1362
1363 case GE:
1364 loperands[6] = gen_label_rtx ();
1365 output_asm_insn (MOTOROLA ? "scc %5\n\tjbra %l6" : "scc %5\n\tjra %l6",
1366 loperands);
1367 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1368 CODE_LABEL_NUMBER (loperands[4]));
1369 output_asm_insn ("sge %5", loperands);
1370 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1371 CODE_LABEL_NUMBER (loperands[6]));
1372 break;
1373
1374 case GEU:
1375 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1376 CODE_LABEL_NUMBER (loperands[4]));
1377 output_asm_insn ("scc %5", loperands);
1378 break;
1379
1380 case LE:
1381 loperands[6] = gen_label_rtx ();
1382 output_asm_insn (MOTOROLA ? "sls %5\n\tjbra %l6" : "sls %5\n\tjra %l6",
1383 loperands);
1384 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1385 CODE_LABEL_NUMBER (loperands[4]));
1386 output_asm_insn ("sle %5", loperands);
1387 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1388 CODE_LABEL_NUMBER (loperands[6]));
1389 break;
1390
1391 case LEU:
1392 (*targetm.asm_out.internal_label) (asm_out_file, "L",
1393 CODE_LABEL_NUMBER (loperands[4]));
1394 output_asm_insn ("sls %5", loperands);
1395 break;
1396
1397 default:
1398 gcc_unreachable ();
1399 }
1400 return "";
1401 }
1402
1403 const char *
1404 output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
1405 {
1406 operands[0] = countop;
1407 operands[1] = dataop;
1408
1409 if (GET_CODE (countop) == CONST_INT)
1410 {
1411 register int count = INTVAL (countop);
1412 /* If COUNT is bigger than size of storage unit in use,
1413 advance to the containing unit of same size. */
1414 if (count > signpos)
1415 {
1416 int offset = (count & ~signpos) / 8;
1417 count = count & signpos;
1418 operands[1] = dataop = adjust_address (dataop, QImode, offset);
1419 }
1420 if (count == signpos)
1421 cc_status.flags = CC_NOT_POSITIVE | CC_Z_IN_NOT_N;
1422 else
1423 cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N;
1424
1425 /* These three statements used to use next_insns_test_no...
1426 but it appears that this should do the same job. */
1427 if (count == 31
1428 && next_insn_tests_no_inequality (insn))
1429 return "tst%.l %1";
1430 if (count == 15
1431 && next_insn_tests_no_inequality (insn))
1432 return "tst%.w %1";
1433 if (count == 7
1434 && next_insn_tests_no_inequality (insn))
1435 return "tst%.b %1";
1436
1437 cc_status.flags = CC_NOT_NEGATIVE;
1438 }
1439 return "btst %0,%1";
1440 }
1441 \f
1442 /* Legitimize PIC addresses. If the address is already
1443 position-independent, we return ORIG. Newly generated
1444 position-independent addresses go to REG. If we need more
1445 than one register, we lose.
1446
1447 An address is legitimized by making an indirect reference
1448 through the Global Offset Table with the name of the symbol
1449 used as an offset.
1450
1451 The assembler and linker are responsible for placing the
1452 address of the symbol in the GOT. The function prologue
1453 is responsible for initializing a5 to the starting address
1454 of the GOT.
1455
1456 The assembler is also responsible for translating a symbol name
1457 into a constant displacement from the start of the GOT.
1458
1459 A quick example may make things a little clearer:
1460
1461 When not generating PIC code to store the value 12345 into _foo
1462 we would generate the following code:
1463
1464 movel #12345, _foo
1465
1466 When generating PIC two transformations are made. First, the compiler
1467 loads the address of foo into a register. So the first transformation makes:
1468
1469 lea _foo, a0
1470 movel #12345, a0@
1471
1472 The code in movsi will intercept the lea instruction and call this
1473 routine which will transform the instructions into:
1474
1475 movel a5@(_foo:w), a0
1476 movel #12345, a0@
1477
1478
1479 That (in a nutshell) is how *all* symbol and label references are
1480 handled. */
1481
1482 rtx
1483 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
1484 rtx reg)
1485 {
1486 rtx pic_ref = orig;
1487
1488 /* First handle a simple SYMBOL_REF or LABEL_REF */
1489 if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
1490 {
1491 gcc_assert (reg);
1492
1493 pic_ref = gen_rtx_MEM (Pmode,
1494 gen_rtx_PLUS (Pmode,
1495 pic_offset_table_rtx, orig));
1496 current_function_uses_pic_offset_table = 1;
1497 MEM_READONLY_P (pic_ref) = 1;
1498 emit_move_insn (reg, pic_ref);
1499 return reg;
1500 }
1501 else if (GET_CODE (orig) == CONST)
1502 {
1503 rtx base;
1504
1505 /* Make sure this has not already been legitimized. */
1506 if (GET_CODE (XEXP (orig, 0)) == PLUS
1507 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1508 return orig;
1509
1510 gcc_assert (reg);
1511
1512 /* legitimize both operands of the PLUS */
1513 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
1514
1515 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1516 orig = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1517 base == reg ? 0 : reg);
1518
1519 if (GET_CODE (orig) == CONST_INT)
1520 return plus_constant (base, INTVAL (orig));
1521 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1522 /* Likewise, should we set special REG_NOTEs here? */
1523 }
1524 return pic_ref;
1525 }
1526
1527 \f
1528 typedef enum { MOVL, SWAP, NEGW, NOTW, NOTB, MOVQ, MVS, MVZ } CONST_METHOD;
1529
1530 static CONST_METHOD const_method (rtx);
1531
1532 #define USE_MOVQ(i) ((unsigned) ((i) + 128) <= 255)
1533
1534 static CONST_METHOD
1535 const_method (rtx constant)
1536 {
1537 int i;
1538 unsigned u;
1539
1540 i = INTVAL (constant);
1541 if (USE_MOVQ (i))
1542 return MOVQ;
1543
1544 /* The ColdFire doesn't have byte or word operations. */
1545 /* FIXME: This may not be useful for the m68060 either. */
1546 if (!TARGET_COLDFIRE)
1547 {
1548 /* if -256 < N < 256 but N is not in range for a moveq
1549 N^ff will be, so use moveq #N^ff, dreg; not.b dreg. */
1550 if (USE_MOVQ (i ^ 0xff))
1551 return NOTB;
1552 /* Likewise, try with not.w */
1553 if (USE_MOVQ (i ^ 0xffff))
1554 return NOTW;
1555 /* This is the only value where neg.w is useful */
1556 if (i == -65408)
1557 return NEGW;
1558 }
1559
1560 /* Try also with swap. */
1561 u = i;
1562 if (USE_MOVQ ((u >> 16) | (u << 16)))
1563 return SWAP;
1564
1565 if (TARGET_ISAB)
1566 {
1567 /* Try using MVZ/MVS with an immediate value to load constants. */
1568 if (i >= 0 && i <= 65535)
1569 return MVZ;
1570 if (i >= -32768 && i <= 32767)
1571 return MVS;
1572 }
1573
1574 /* Otherwise, use move.l */
1575 return MOVL;
1576 }
1577
1578 static int
1579 const_int_cost (rtx constant)
1580 {
1581 switch (const_method (constant))
1582 {
1583 case MOVQ:
1584 /* Constants between -128 and 127 are cheap due to moveq. */
1585 return 0;
1586 case MVZ:
1587 case MVS:
1588 case NOTB:
1589 case NOTW:
1590 case NEGW:
1591 case SWAP:
1592 /* Constants easily generated by moveq + not.b/not.w/neg.w/swap. */
1593 return 1;
1594 case MOVL:
1595 return 2;
1596 default:
1597 gcc_unreachable ();
1598 }
1599 }
1600
1601 static bool
1602 m68k_rtx_costs (rtx x, int code, int outer_code, int *total)
1603 {
1604 switch (code)
1605 {
1606 case CONST_INT:
1607 /* Constant zero is super cheap due to clr instruction. */
1608 if (x == const0_rtx)
1609 *total = 0;
1610 else
1611 *total = const_int_cost (x);
1612 return true;
1613
1614 case CONST:
1615 case LABEL_REF:
1616 case SYMBOL_REF:
1617 *total = 3;
1618 return true;
1619
1620 case CONST_DOUBLE:
1621 /* Make 0.0 cheaper than other floating constants to
1622 encourage creating tstsf and tstdf insns. */
1623 if (outer_code == COMPARE
1624 && (x == CONST0_RTX (SFmode) || x == CONST0_RTX (DFmode)))
1625 *total = 4;
1626 else
1627 *total = 5;
1628 return true;
1629
1630 /* These are vaguely right for a 68020. */
1631 /* The costs for long multiply have been adjusted to work properly
1632 in synth_mult on the 68020, relative to an average of the time
1633 for add and the time for shift, taking away a little more because
1634 sometimes move insns are needed. */
1635 /* div?.w is relatively cheaper on 68000 counted in COSTS_N_INSNS
1636 terms. */
1637 #define MULL_COST \
1638 (TUNE_68060 ? 2 \
1639 : TUNE_68040 ? 5 \
1640 : TUNE_CFV2 ? 10 \
1641 : TARGET_COLDFIRE ? 3 : 13)
1642
1643 #define MULW_COST \
1644 (TUNE_68060 ? 2 \
1645 : TUNE_68040 ? 3 \
1646 : TUNE_68000_10 || TUNE_CFV2 ? 5 \
1647 : TARGET_COLDFIRE ? 2 : 8)
1648
1649 #define DIVW_COST \
1650 (TARGET_CF_HWDIV ? 11 \
1651 : TUNE_68000_10 || TARGET_COLDFIRE ? 12 : 27)
1652
1653 case PLUS:
1654 /* An lea costs about three times as much as a simple add. */
1655 if (GET_MODE (x) == SImode
1656 && GET_CODE (XEXP (x, 1)) == REG
1657 && GET_CODE (XEXP (x, 0)) == MULT
1658 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
1659 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1660 && (INTVAL (XEXP (XEXP (x, 0), 1)) == 2
1661 || INTVAL (XEXP (XEXP (x, 0), 1)) == 4
1662 || INTVAL (XEXP (XEXP (x, 0), 1)) == 8))
1663 {
1664 /* lea an@(dx:l:i),am */
1665 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 2 : 3);
1666 return true;
1667 }
1668 return false;
1669
1670 case ASHIFT:
1671 case ASHIFTRT:
1672 case LSHIFTRT:
1673 if (TUNE_68060)
1674 {
1675 *total = COSTS_N_INSNS(1);
1676 return true;
1677 }
1678 if (TUNE_68000_10)
1679 {
1680 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
1681 {
1682 if (INTVAL (XEXP (x, 1)) < 16)
1683 *total = COSTS_N_INSNS (2) + INTVAL (XEXP (x, 1)) / 2;
1684 else
1685 /* We're using clrw + swap for these cases. */
1686 *total = COSTS_N_INSNS (4) + (INTVAL (XEXP (x, 1)) - 16) / 2;
1687 }
1688 else
1689 *total = COSTS_N_INSNS (10); /* Worst case. */
1690 return true;
1691 }
1692 /* A shift by a big integer takes an extra instruction. */
1693 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1694 && (INTVAL (XEXP (x, 1)) == 16))
1695 {
1696 *total = COSTS_N_INSNS (2); /* clrw;swap */
1697 return true;
1698 }
1699 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1700 && !(INTVAL (XEXP (x, 1)) > 0
1701 && INTVAL (XEXP (x, 1)) <= 8))
1702 {
1703 *total = COSTS_N_INSNS (TARGET_COLDFIRE ? 1 : 3); /* lsr #i,dn */
1704 return true;
1705 }
1706 return false;
1707
1708 case MULT:
1709 if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
1710 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
1711 && GET_MODE (x) == SImode)
1712 *total = COSTS_N_INSNS (MULW_COST);
1713 else if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
1714 *total = COSTS_N_INSNS (MULW_COST);
1715 else
1716 *total = COSTS_N_INSNS (MULL_COST);
1717 return true;
1718
1719 case DIV:
1720 case UDIV:
1721 case MOD:
1722 case UMOD:
1723 if (GET_MODE (x) == QImode || GET_MODE (x) == HImode)
1724 *total = COSTS_N_INSNS (DIVW_COST); /* div.w */
1725 else if (TARGET_CF_HWDIV)
1726 *total = COSTS_N_INSNS (18);
1727 else
1728 *total = COSTS_N_INSNS (43); /* div.l */
1729 return true;
1730
1731 default:
1732 return false;
1733 }
1734 }
1735
1736 const char *
1737 output_move_const_into_data_reg (rtx *operands)
1738 {
1739 int i;
1740
1741 i = INTVAL (operands[1]);
1742 switch (const_method (operands[1]))
1743 {
1744 case MVZ:
1745 return "mvzw %1,%0";
1746 case MVS:
1747 return "mvsw %1,%0";
1748 case MOVQ:
1749 return "moveq %1,%0";
1750 case NOTB:
1751 CC_STATUS_INIT;
1752 operands[1] = GEN_INT (i ^ 0xff);
1753 return "moveq %1,%0\n\tnot%.b %0";
1754 case NOTW:
1755 CC_STATUS_INIT;
1756 operands[1] = GEN_INT (i ^ 0xffff);
1757 return "moveq %1,%0\n\tnot%.w %0";
1758 case NEGW:
1759 CC_STATUS_INIT;
1760 return "moveq #-128,%0\n\tneg%.w %0";
1761 case SWAP:
1762 {
1763 unsigned u = i;
1764
1765 operands[1] = GEN_INT ((u << 16) | (u >> 16));
1766 return "moveq %1,%0\n\tswap %0";
1767 }
1768 case MOVL:
1769 return "move%.l %1,%0";
1770 default:
1771 gcc_unreachable ();
1772 }
1773 }
1774
1775 /* Return 1 if 'constant' can be represented by
1776 mov3q on a ColdFire V4 core. */
1777 int
1778 valid_mov3q_const (rtx constant)
1779 {
1780 int i;
1781
1782 if (TARGET_ISAB && GET_CODE (constant) == CONST_INT)
1783 {
1784 i = INTVAL (constant);
1785 if (i == -1 || (i >= 1 && i <= 7))
1786 return 1;
1787 }
1788 return 0;
1789 }
1790
1791
1792 const char *
1793 output_move_simode_const (rtx *operands)
1794 {
1795 if (operands[1] == const0_rtx
1796 && (DATA_REG_P (operands[0])
1797 || GET_CODE (operands[0]) == MEM)
1798 /* clr insns on 68000 read before writing.
1799 This isn't so on the 68010, but we have no TARGET_68010. */
1800 && ((TARGET_68020 || TARGET_COLDFIRE)
1801 || !(GET_CODE (operands[0]) == MEM
1802 && MEM_VOLATILE_P (operands[0]))))
1803 return "clr%.l %0";
1804 else if ((GET_MODE (operands[0]) == SImode)
1805 && valid_mov3q_const (operands[1]))
1806 return "mov3q%.l %1,%0";
1807 else if (operands[1] == const0_rtx
1808 && ADDRESS_REG_P (operands[0]))
1809 return "sub%.l %0,%0";
1810 else if (DATA_REG_P (operands[0]))
1811 return output_move_const_into_data_reg (operands);
1812 else if (ADDRESS_REG_P (operands[0])
1813 && INTVAL (operands[1]) < 0x8000
1814 && INTVAL (operands[1]) >= -0x8000)
1815 {
1816 if (valid_mov3q_const (operands[1]))
1817 return "mov3q%.l %1,%0";
1818 return "move%.w %1,%0";
1819 }
1820 else if (GET_CODE (operands[0]) == MEM
1821 && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
1822 && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
1823 && INTVAL (operands[1]) < 0x8000
1824 && INTVAL (operands[1]) >= -0x8000)
1825 {
1826 if (valid_mov3q_const (operands[1]))
1827 return "mov3q%.l %1,%-";
1828 return "pea %a1";
1829 }
1830 return "move%.l %1,%0";
1831 }
1832
1833 const char *
1834 output_move_simode (rtx *operands)
1835 {
1836 if (GET_CODE (operands[1]) == CONST_INT)
1837 return output_move_simode_const (operands);
1838 else if ((GET_CODE (operands[1]) == SYMBOL_REF
1839 || GET_CODE (operands[1]) == CONST)
1840 && push_operand (operands[0], SImode))
1841 return "pea %a1";
1842 else if ((GET_CODE (operands[1]) == SYMBOL_REF
1843 || GET_CODE (operands[1]) == CONST)
1844 && ADDRESS_REG_P (operands[0]))
1845 return "lea %a1,%0";
1846 return "move%.l %1,%0";
1847 }
1848
1849 const char *
1850 output_move_himode (rtx *operands)
1851 {
1852 if (GET_CODE (operands[1]) == CONST_INT)
1853 {
1854 if (operands[1] == const0_rtx
1855 && (DATA_REG_P (operands[0])
1856 || GET_CODE (operands[0]) == MEM)
1857 /* clr insns on 68000 read before writing.
1858 This isn't so on the 68010, but we have no TARGET_68010. */
1859 && ((TARGET_68020 || TARGET_COLDFIRE)
1860 || !(GET_CODE (operands[0]) == MEM
1861 && MEM_VOLATILE_P (operands[0]))))
1862 return "clr%.w %0";
1863 else if (operands[1] == const0_rtx
1864 && ADDRESS_REG_P (operands[0]))
1865 return "sub%.l %0,%0";
1866 else if (DATA_REG_P (operands[0])
1867 && INTVAL (operands[1]) < 128
1868 && INTVAL (operands[1]) >= -128)
1869 return "moveq %1,%0";
1870 else if (INTVAL (operands[1]) < 0x8000
1871 && INTVAL (operands[1]) >= -0x8000)
1872 return "move%.w %1,%0";
1873 }
1874 else if (CONSTANT_P (operands[1]))
1875 return "move%.l %1,%0";
1876 /* Recognize the insn before a tablejump, one that refers
1877 to a table of offsets. Such an insn will need to refer
1878 to a label on the insn. So output one. Use the label-number
1879 of the table of offsets to generate this label. This code,
1880 and similar code below, assumes that there will be at most one
1881 reference to each table. */
1882 if (GET_CODE (operands[1]) == MEM
1883 && GET_CODE (XEXP (operands[1], 0)) == PLUS
1884 && GET_CODE (XEXP (XEXP (operands[1], 0), 1)) == LABEL_REF
1885 && GET_CODE (XEXP (XEXP (operands[1], 0), 0)) != PLUS)
1886 {
1887 rtx labelref = XEXP (XEXP (operands[1], 0), 1);
1888 if (MOTOROLA)
1889 asm_fprintf (asm_out_file, "\t.set %LLI%d,.+2\n",
1890 CODE_LABEL_NUMBER (XEXP (labelref, 0)));
1891 else
1892 (*targetm.asm_out.internal_label) (asm_out_file, "LI",
1893 CODE_LABEL_NUMBER (XEXP (labelref, 0)));
1894 }
1895 return "move%.w %1,%0";
1896 }
1897
1898 const char *
1899 output_move_qimode (rtx *operands)
1900 {
1901 /* 68k family always modifies the stack pointer by at least 2, even for
1902 byte pushes. The 5200 (ColdFire) does not do this. */
1903
1904 /* This case is generated by pushqi1 pattern now. */
1905 gcc_assert (!(GET_CODE (operands[0]) == MEM
1906 && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
1907 && XEXP (XEXP (operands[0], 0), 0) == stack_pointer_rtx
1908 && ! ADDRESS_REG_P (operands[1])
1909 && ! TARGET_COLDFIRE));
1910
1911 /* clr and st insns on 68000 read before writing.
1912 This isn't so on the 68010, but we have no TARGET_68010. */
1913 if (!ADDRESS_REG_P (operands[0])
1914 && ((TARGET_68020 || TARGET_COLDFIRE)
1915 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1916 {
1917 if (operands[1] == const0_rtx)
1918 return "clr%.b %0";
1919 if ((!TARGET_COLDFIRE || DATA_REG_P (operands[0]))
1920 && GET_CODE (operands[1]) == CONST_INT
1921 && (INTVAL (operands[1]) & 255) == 255)
1922 {
1923 CC_STATUS_INIT;
1924 return "st %0";
1925 }
1926 }
1927 if (GET_CODE (operands[1]) == CONST_INT
1928 && DATA_REG_P (operands[0])
1929 && INTVAL (operands[1]) < 128
1930 && INTVAL (operands[1]) >= -128)
1931 return "moveq %1,%0";
1932 if (operands[1] == const0_rtx && ADDRESS_REG_P (operands[0]))
1933 return "sub%.l %0,%0";
1934 if (GET_CODE (operands[1]) != CONST_INT && CONSTANT_P (operands[1]))
1935 return "move%.l %1,%0";
1936 /* 68k family (including the 5200 ColdFire) does not support byte moves to
1937 from address registers. */
1938 if (ADDRESS_REG_P (operands[0]) || ADDRESS_REG_P (operands[1]))
1939 return "move%.w %1,%0";
1940 return "move%.b %1,%0";
1941 }
1942
1943 const char *
1944 output_move_stricthi (rtx *operands)
1945 {
1946 if (operands[1] == const0_rtx
1947 /* clr insns on 68000 read before writing.
1948 This isn't so on the 68010, but we have no TARGET_68010. */
1949 && ((TARGET_68020 || TARGET_COLDFIRE)
1950 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1951 return "clr%.w %0";
1952 return "move%.w %1,%0";
1953 }
1954
1955 const char *
1956 output_move_strictqi (rtx *operands)
1957 {
1958 if (operands[1] == const0_rtx
1959 /* clr insns on 68000 read before writing.
1960 This isn't so on the 68010, but we have no TARGET_68010. */
1961 && ((TARGET_68020 || TARGET_COLDFIRE)
1962 || !(GET_CODE (operands[0]) == MEM && MEM_VOLATILE_P (operands[0]))))
1963 return "clr%.b %0";
1964 return "move%.b %1,%0";
1965 }
1966
1967 /* Return the best assembler insn template
1968 for moving operands[1] into operands[0] as a fullword. */
1969
1970 static const char *
1971 singlemove_string (rtx *operands)
1972 {
1973 if (GET_CODE (operands[1]) == CONST_INT)
1974 return output_move_simode_const (operands);
1975 return "move%.l %1,%0";
1976 }
1977
1978
1979 /* Output assembler code to perform a doubleword move insn
1980 with operands OPERANDS. */
1981
1982 const char *
1983 output_move_double (rtx *operands)
1984 {
1985 enum
1986 {
1987 REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP
1988 } optype0, optype1;
1989 rtx latehalf[2];
1990 rtx middlehalf[2];
1991 rtx xops[2];
1992 rtx addreg0 = 0, addreg1 = 0;
1993 int dest_overlapped_low = 0;
1994 int size = GET_MODE_SIZE (GET_MODE (operands[0]));
1995
1996 middlehalf[0] = 0;
1997 middlehalf[1] = 0;
1998
1999 /* First classify both operands. */
2000
2001 if (REG_P (operands[0]))
2002 optype0 = REGOP;
2003 else if (offsettable_memref_p (operands[0]))
2004 optype0 = OFFSOP;
2005 else if (GET_CODE (XEXP (operands[0], 0)) == POST_INC)
2006 optype0 = POPOP;
2007 else if (GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
2008 optype0 = PUSHOP;
2009 else if (GET_CODE (operands[0]) == MEM)
2010 optype0 = MEMOP;
2011 else
2012 optype0 = RNDOP;
2013
2014 if (REG_P (operands[1]))
2015 optype1 = REGOP;
2016 else if (CONSTANT_P (operands[1]))
2017 optype1 = CNSTOP;
2018 else if (offsettable_memref_p (operands[1]))
2019 optype1 = OFFSOP;
2020 else if (GET_CODE (XEXP (operands[1], 0)) == POST_INC)
2021 optype1 = POPOP;
2022 else if (GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
2023 optype1 = PUSHOP;
2024 else if (GET_CODE (operands[1]) == MEM)
2025 optype1 = MEMOP;
2026 else
2027 optype1 = RNDOP;
2028
2029 /* Check for the cases that the operand constraints are not supposed
2030 to allow to happen. Generating code for these cases is
2031 painful. */
2032 gcc_assert (optype0 != RNDOP && optype1 != RNDOP);
2033
2034 /* If one operand is decrementing and one is incrementing
2035 decrement the former register explicitly
2036 and change that operand into ordinary indexing. */
2037
2038 if (optype0 == PUSHOP && optype1 == POPOP)
2039 {
2040 operands[0] = XEXP (XEXP (operands[0], 0), 0);
2041 if (size == 12)
2042 output_asm_insn ("sub%.l #12,%0", operands);
2043 else
2044 output_asm_insn ("subq%.l #8,%0", operands);
2045 if (GET_MODE (operands[1]) == XFmode)
2046 operands[0] = gen_rtx_MEM (XFmode, operands[0]);
2047 else if (GET_MODE (operands[0]) == DFmode)
2048 operands[0] = gen_rtx_MEM (DFmode, operands[0]);
2049 else
2050 operands[0] = gen_rtx_MEM (DImode, operands[0]);
2051 optype0 = OFFSOP;
2052 }
2053 if (optype0 == POPOP && optype1 == PUSHOP)
2054 {
2055 operands[1] = XEXP (XEXP (operands[1], 0), 0);
2056 if (size == 12)
2057 output_asm_insn ("sub%.l #12,%1", operands);
2058 else
2059 output_asm_insn ("subq%.l #8,%1", operands);
2060 if (GET_MODE (operands[1]) == XFmode)
2061 operands[1] = gen_rtx_MEM (XFmode, operands[1]);
2062 else if (GET_MODE (operands[1]) == DFmode)
2063 operands[1] = gen_rtx_MEM (DFmode, operands[1]);
2064 else
2065 operands[1] = gen_rtx_MEM (DImode, operands[1]);
2066 optype1 = OFFSOP;
2067 }
2068
2069 /* If an operand is an unoffsettable memory ref, find a register
2070 we can increment temporarily to make it refer to the second word. */
2071
2072 if (optype0 == MEMOP)
2073 addreg0 = find_addr_reg (XEXP (operands[0], 0));
2074
2075 if (optype1 == MEMOP)
2076 addreg1 = find_addr_reg (XEXP (operands[1], 0));
2077
2078 /* Ok, we can do one word at a time.
2079 Normally we do the low-numbered word first,
2080 but if either operand is autodecrementing then we
2081 do the high-numbered word first.
2082
2083 In either case, set up in LATEHALF the operands to use
2084 for the high-numbered word and in some cases alter the
2085 operands in OPERANDS to be suitable for the low-numbered word. */
2086
2087 if (size == 12)
2088 {
2089 if (optype0 == REGOP)
2090 {
2091 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 2);
2092 middlehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2093 }
2094 else if (optype0 == OFFSOP)
2095 {
2096 middlehalf[0] = adjust_address (operands[0], SImode, 4);
2097 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2098 }
2099 else
2100 {
2101 middlehalf[0] = operands[0];
2102 latehalf[0] = operands[0];
2103 }
2104
2105 if (optype1 == REGOP)
2106 {
2107 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 2);
2108 middlehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2109 }
2110 else if (optype1 == OFFSOP)
2111 {
2112 middlehalf[1] = adjust_address (operands[1], SImode, 4);
2113 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2114 }
2115 else if (optype1 == CNSTOP)
2116 {
2117 if (GET_CODE (operands[1]) == CONST_DOUBLE)
2118 {
2119 REAL_VALUE_TYPE r;
2120 long l[3];
2121
2122 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
2123 REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
2124 operands[1] = GEN_INT (l[0]);
2125 middlehalf[1] = GEN_INT (l[1]);
2126 latehalf[1] = GEN_INT (l[2]);
2127 }
2128 else
2129 {
2130 /* No non-CONST_DOUBLE constant should ever appear
2131 here. */
2132 gcc_assert (!CONSTANT_P (operands[1]));
2133 }
2134 }
2135 else
2136 {
2137 middlehalf[1] = operands[1];
2138 latehalf[1] = operands[1];
2139 }
2140 }
2141 else
2142 /* size is not 12: */
2143 {
2144 if (optype0 == REGOP)
2145 latehalf[0] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
2146 else if (optype0 == OFFSOP)
2147 latehalf[0] = adjust_address (operands[0], SImode, size - 4);
2148 else
2149 latehalf[0] = operands[0];
2150
2151 if (optype1 == REGOP)
2152 latehalf[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
2153 else if (optype1 == OFFSOP)
2154 latehalf[1] = adjust_address (operands[1], SImode, size - 4);
2155 else if (optype1 == CNSTOP)
2156 split_double (operands[1], &operands[1], &latehalf[1]);
2157 else
2158 latehalf[1] = operands[1];
2159 }
2160
2161 /* If insn is effectively movd N(sp),-(sp) then we will do the
2162 high word first. We should use the adjusted operand 1 (which is N+4(sp))
2163 for the low word as well, to compensate for the first decrement of sp. */
2164 if (optype0 == PUSHOP
2165 && REGNO (XEXP (XEXP (operands[0], 0), 0)) == STACK_POINTER_REGNUM
2166 && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
2167 operands[1] = middlehalf[1] = latehalf[1];
2168
2169 /* For (set (reg:DI N) (mem:DI ... (reg:SI N) ...)),
2170 if the upper part of reg N does not appear in the MEM, arrange to
2171 emit the move late-half first. Otherwise, compute the MEM address
2172 into the upper part of N and use that as a pointer to the memory
2173 operand. */
2174 if (optype0 == REGOP
2175 && (optype1 == OFFSOP || optype1 == MEMOP))
2176 {
2177 rtx testlow = gen_rtx_REG (SImode, REGNO (operands[0]));
2178
2179 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2180 && reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2181 {
2182 /* If both halves of dest are used in the src memory address,
2183 compute the address into latehalf of dest.
2184 Note that this can't happen if the dest is two data regs. */
2185 compadr:
2186 xops[0] = latehalf[0];
2187 xops[1] = XEXP (operands[1], 0);
2188 output_asm_insn ("lea %a1,%0", xops);
2189 if (GET_MODE (operands[1]) == XFmode )
2190 {
2191 operands[1] = gen_rtx_MEM (XFmode, latehalf[0]);
2192 middlehalf[1] = adjust_address (operands[1], DImode, size - 8);
2193 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2194 }
2195 else
2196 {
2197 operands[1] = gen_rtx_MEM (DImode, latehalf[0]);
2198 latehalf[1] = adjust_address (operands[1], DImode, size - 4);
2199 }
2200 }
2201 else if (size == 12
2202 && reg_overlap_mentioned_p (middlehalf[0],
2203 XEXP (operands[1], 0)))
2204 {
2205 /* Check for two regs used by both source and dest.
2206 Note that this can't happen if the dest is all data regs.
2207 It can happen if the dest is d6, d7, a0.
2208 But in that case, latehalf is an addr reg, so
2209 the code at compadr does ok. */
2210
2211 if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0))
2212 || reg_overlap_mentioned_p (latehalf[0], XEXP (operands[1], 0)))
2213 goto compadr;
2214
2215 /* JRV says this can't happen: */
2216 gcc_assert (!addreg0 && !addreg1);
2217
2218 /* Only the middle reg conflicts; simply put it last. */
2219 output_asm_insn (singlemove_string (operands), operands);
2220 output_asm_insn (singlemove_string (latehalf), latehalf);
2221 output_asm_insn (singlemove_string (middlehalf), middlehalf);
2222 return "";
2223 }
2224 else if (reg_overlap_mentioned_p (testlow, XEXP (operands[1], 0)))
2225 /* If the low half of dest is mentioned in the source memory
2226 address, the arrange to emit the move late half first. */
2227 dest_overlapped_low = 1;
2228 }
2229
2230 /* If one or both operands autodecrementing,
2231 do the two words, high-numbered first. */
2232
2233 /* Likewise, the first move would clobber the source of the second one,
2234 do them in the other order. This happens only for registers;
2235 such overlap can't happen in memory unless the user explicitly
2236 sets it up, and that is an undefined circumstance. */
2237
2238 if (optype0 == PUSHOP || optype1 == PUSHOP
2239 || (optype0 == REGOP && optype1 == REGOP
2240 && ((middlehalf[1] && REGNO (operands[0]) == REGNO (middlehalf[1]))
2241 || REGNO (operands[0]) == REGNO (latehalf[1])))
2242 || dest_overlapped_low)
2243 {
2244 /* Make any unoffsettable addresses point at high-numbered word. */
2245 if (addreg0)
2246 {
2247 if (size == 12)
2248 output_asm_insn ("addq%.l #8,%0", &addreg0);
2249 else
2250 output_asm_insn ("addq%.l #4,%0", &addreg0);
2251 }
2252 if (addreg1)
2253 {
2254 if (size == 12)
2255 output_asm_insn ("addq%.l #8,%0", &addreg1);
2256 else
2257 output_asm_insn ("addq%.l #4,%0", &addreg1);
2258 }
2259
2260 /* Do that word. */
2261 output_asm_insn (singlemove_string (latehalf), latehalf);
2262
2263 /* Undo the adds we just did. */
2264 if (addreg0)
2265 output_asm_insn ("subq%.l #4,%0", &addreg0);
2266 if (addreg1)
2267 output_asm_insn ("subq%.l #4,%0", &addreg1);
2268
2269 if (size == 12)
2270 {
2271 output_asm_insn (singlemove_string (middlehalf), middlehalf);
2272 if (addreg0)
2273 output_asm_insn ("subq%.l #4,%0", &addreg0);
2274 if (addreg1)
2275 output_asm_insn ("subq%.l #4,%0", &addreg1);
2276 }
2277
2278 /* Do low-numbered word. */
2279 return singlemove_string (operands);
2280 }
2281
2282 /* Normal case: do the two words, low-numbered first. */
2283
2284 output_asm_insn (singlemove_string (operands), operands);
2285
2286 /* Do the middle one of the three words for long double */
2287 if (size == 12)
2288 {
2289 if (addreg0)
2290 output_asm_insn ("addq%.l #4,%0", &addreg0);
2291 if (addreg1)
2292 output_asm_insn ("addq%.l #4,%0", &addreg1);
2293
2294 output_asm_insn (singlemove_string (middlehalf), middlehalf);
2295 }
2296
2297 /* Make any unoffsettable addresses point at high-numbered word. */
2298 if (addreg0)
2299 output_asm_insn ("addq%.l #4,%0", &addreg0);
2300 if (addreg1)
2301 output_asm_insn ("addq%.l #4,%0", &addreg1);
2302
2303 /* Do that word. */
2304 output_asm_insn (singlemove_string (latehalf), latehalf);
2305
2306 /* Undo the adds we just did. */
2307 if (addreg0)
2308 {
2309 if (size == 12)
2310 output_asm_insn ("subq%.l #8,%0", &addreg0);
2311 else
2312 output_asm_insn ("subq%.l #4,%0", &addreg0);
2313 }
2314 if (addreg1)
2315 {
2316 if (size == 12)
2317 output_asm_insn ("subq%.l #8,%0", &addreg1);
2318 else
2319 output_asm_insn ("subq%.l #4,%0", &addreg1);
2320 }
2321
2322 return "";
2323 }
2324
2325
2326 /* Ensure mode of ORIG, a REG rtx, is MODE. Returns either ORIG or a
2327 new rtx with the correct mode. */
2328
2329 static rtx
2330 force_mode (enum machine_mode mode, rtx orig)
2331 {
2332 if (mode == GET_MODE (orig))
2333 return orig;
2334
2335 if (REGNO (orig) >= FIRST_PSEUDO_REGISTER)
2336 abort ();
2337
2338 return gen_rtx_REG (mode, REGNO (orig));
2339 }
2340
2341 static int
2342 fp_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2343 {
2344 return reg_renumber && FP_REG_P (op);
2345 }
2346
2347 /* Emit insns to move operands[1] into operands[0].
2348
2349 Return 1 if we have written out everything that needs to be done to
2350 do the move. Otherwise, return 0 and the caller will emit the move
2351 normally.
2352
2353 Note SCRATCH_REG may not be in the proper mode depending on how it
2354 will be used. This routine is responsible for creating a new copy
2355 of SCRATCH_REG in the proper mode. */
2356
2357 int
2358 emit_move_sequence (rtx *operands, enum machine_mode mode, rtx scratch_reg)
2359 {
2360 register rtx operand0 = operands[0];
2361 register rtx operand1 = operands[1];
2362 register rtx tem;
2363
2364 if (scratch_reg
2365 && reload_in_progress && GET_CODE (operand0) == REG
2366 && REGNO (operand0) >= FIRST_PSEUDO_REGISTER)
2367 operand0 = reg_equiv_mem[REGNO (operand0)];
2368 else if (scratch_reg
2369 && reload_in_progress && GET_CODE (operand0) == SUBREG
2370 && GET_CODE (SUBREG_REG (operand0)) == REG
2371 && REGNO (SUBREG_REG (operand0)) >= FIRST_PSEUDO_REGISTER)
2372 {
2373 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
2374 the code which tracks sets/uses for delete_output_reload. */
2375 rtx temp = gen_rtx_SUBREG (GET_MODE (operand0),
2376 reg_equiv_mem [REGNO (SUBREG_REG (operand0))],
2377 SUBREG_BYTE (operand0));
2378 operand0 = alter_subreg (&temp);
2379 }
2380
2381 if (scratch_reg
2382 && reload_in_progress && GET_CODE (operand1) == REG
2383 && REGNO (operand1) >= FIRST_PSEUDO_REGISTER)
2384 operand1 = reg_equiv_mem[REGNO (operand1)];
2385 else if (scratch_reg
2386 && reload_in_progress && GET_CODE (operand1) == SUBREG
2387 && GET_CODE (SUBREG_REG (operand1)) == REG
2388 && REGNO (SUBREG_REG (operand1)) >= FIRST_PSEUDO_REGISTER)
2389 {
2390 /* We must not alter SUBREG_BYTE (operand0) since that would confuse
2391 the code which tracks sets/uses for delete_output_reload. */
2392 rtx temp = gen_rtx_SUBREG (GET_MODE (operand1),
2393 reg_equiv_mem [REGNO (SUBREG_REG (operand1))],
2394 SUBREG_BYTE (operand1));
2395 operand1 = alter_subreg (&temp);
2396 }
2397
2398 if (scratch_reg && reload_in_progress && GET_CODE (operand0) == MEM
2399 && ((tem = find_replacement (&XEXP (operand0, 0)))
2400 != XEXP (operand0, 0)))
2401 operand0 = gen_rtx_MEM (GET_MODE (operand0), tem);
2402 if (scratch_reg && reload_in_progress && GET_CODE (operand1) == MEM
2403 && ((tem = find_replacement (&XEXP (operand1, 0)))
2404 != XEXP (operand1, 0)))
2405 operand1 = gen_rtx_MEM (GET_MODE (operand1), tem);
2406
2407 /* Handle secondary reloads for loads/stores of FP registers where
2408 the address is symbolic by using the scratch register */
2409 if (fp_reg_operand (operand0, mode)
2410 && ((GET_CODE (operand1) == MEM
2411 && ! memory_address_p (DFmode, XEXP (operand1, 0)))
2412 || ((GET_CODE (operand1) == SUBREG
2413 && GET_CODE (XEXP (operand1, 0)) == MEM
2414 && !memory_address_p (DFmode, XEXP (XEXP (operand1, 0), 0)))))
2415 && scratch_reg)
2416 {
2417 if (GET_CODE (operand1) == SUBREG)
2418 operand1 = XEXP (operand1, 0);
2419
2420 /* SCRATCH_REG will hold an address. We want
2421 it in SImode regardless of what mode it was originally given
2422 to us. */
2423 scratch_reg = force_mode (SImode, scratch_reg);
2424
2425 /* D might not fit in 14 bits either; for such cases load D into
2426 scratch reg. */
2427 if (!memory_address_p (Pmode, XEXP (operand1, 0)))
2428 {
2429 emit_move_insn (scratch_reg, XEXP (XEXP (operand1, 0), 1));
2430 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand1, 0)),
2431 Pmode,
2432 XEXP (XEXP (operand1, 0), 0),
2433 scratch_reg));
2434 }
2435 else
2436 emit_move_insn (scratch_reg, XEXP (operand1, 0));
2437 emit_insn (gen_rtx_SET (VOIDmode, operand0,
2438 gen_rtx_MEM (mode, scratch_reg)));
2439 return 1;
2440 }
2441 else if (fp_reg_operand (operand1, mode)
2442 && ((GET_CODE (operand0) == MEM
2443 && ! memory_address_p (DFmode, XEXP (operand0, 0)))
2444 || ((GET_CODE (operand0) == SUBREG)
2445 && GET_CODE (XEXP (operand0, 0)) == MEM
2446 && !memory_address_p (DFmode, XEXP (XEXP (operand0, 0), 0))))
2447 && scratch_reg)
2448 {
2449 if (GET_CODE (operand0) == SUBREG)
2450 operand0 = XEXP (operand0, 0);
2451
2452 /* SCRATCH_REG will hold an address and maybe the actual data. We want
2453 it in SIMODE regardless of what mode it was originally given
2454 to us. */
2455 scratch_reg = force_mode (SImode, scratch_reg);
2456
2457 /* D might not fit in 14 bits either; for such cases load D into
2458 scratch reg. */
2459 if (!memory_address_p (Pmode, XEXP (operand0, 0)))
2460 {
2461 emit_move_insn (scratch_reg, XEXP (XEXP (operand0, 0), 1));
2462 emit_move_insn (scratch_reg, gen_rtx_fmt_ee (GET_CODE (XEXP (operand0,
2463 0)),
2464 Pmode,
2465 XEXP (XEXP (operand0, 0),
2466 0),
2467 scratch_reg));
2468 }
2469 else
2470 emit_move_insn (scratch_reg, XEXP (operand0, 0));
2471 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_MEM (mode, scratch_reg),
2472 operand1));
2473 return 1;
2474 }
2475 /* Handle secondary reloads for loads of FP registers from constant
2476 expressions by forcing the constant into memory.
2477
2478 use scratch_reg to hold the address of the memory location.
2479
2480 The proper fix is to change PREFERRED_RELOAD_CLASS to return
2481 NO_REGS when presented with a const_int and an register class
2482 containing only FP registers. Doing so unfortunately creates
2483 more problems than it solves. Fix this for 2.5. */
2484 else if (fp_reg_operand (operand0, mode)
2485 && CONSTANT_P (operand1)
2486 && scratch_reg)
2487 {
2488 rtx xoperands[2];
2489
2490 /* SCRATCH_REG will hold an address and maybe the actual data. We want
2491 it in SIMODE regardless of what mode it was originally given
2492 to us. */
2493 scratch_reg = force_mode (SImode, scratch_reg);
2494
2495 /* Force the constant into memory and put the address of the
2496 memory location into scratch_reg. */
2497 xoperands[0] = scratch_reg;
2498 xoperands[1] = XEXP (force_const_mem (mode, operand1), 0);
2499 emit_insn (gen_rtx_SET (mode, scratch_reg, xoperands[1]));
2500
2501 /* Now load the destination register. */
2502 emit_insn (gen_rtx_SET (mode, operand0,
2503 gen_rtx_MEM (mode, scratch_reg)));
2504 return 1;
2505 }
2506
2507 /* Now have insn-emit do whatever it normally does. */
2508 return 0;
2509 }
2510
2511 /* Return a REG that occurs in ADDR with coefficient 1.
2512 ADDR can be effectively incremented by incrementing REG. */
2513
2514 static rtx
2515 find_addr_reg (rtx addr)
2516 {
2517 while (GET_CODE (addr) == PLUS)
2518 {
2519 if (GET_CODE (XEXP (addr, 0)) == REG)
2520 addr = XEXP (addr, 0);
2521 else if (GET_CODE (XEXP (addr, 1)) == REG)
2522 addr = XEXP (addr, 1);
2523 else if (CONSTANT_P (XEXP (addr, 0)))
2524 addr = XEXP (addr, 1);
2525 else if (CONSTANT_P (XEXP (addr, 1)))
2526 addr = XEXP (addr, 0);
2527 else
2528 gcc_unreachable ();
2529 }
2530 gcc_assert (GET_CODE (addr) == REG);
2531 return addr;
2532 }
2533
2534 /* Output assembler code to perform a 32-bit 3-operand add. */
2535
2536 const char *
2537 output_addsi3 (rtx *operands)
2538 {
2539 if (! operands_match_p (operands[0], operands[1]))
2540 {
2541 if (!ADDRESS_REG_P (operands[1]))
2542 {
2543 rtx tmp = operands[1];
2544
2545 operands[1] = operands[2];
2546 operands[2] = tmp;
2547 }
2548
2549 /* These insns can result from reloads to access
2550 stack slots over 64k from the frame pointer. */
2551 if (GET_CODE (operands[2]) == CONST_INT
2552 && (INTVAL (operands[2]) < -32768 || INTVAL (operands[2]) > 32767))
2553 return "move%.l %2,%0\n\tadd%.l %1,%0";
2554 if (GET_CODE (operands[2]) == REG)
2555 return MOTOROLA ? "lea (%1,%2.l),%0" : "lea %1@(0,%2:l),%0";
2556 return MOTOROLA ? "lea (%c2,%1),%0" : "lea %1@(%c2),%0";
2557 }
2558 if (GET_CODE (operands[2]) == CONST_INT)
2559 {
2560 if (INTVAL (operands[2]) > 0
2561 && INTVAL (operands[2]) <= 8)
2562 return "addq%.l %2,%0";
2563 if (INTVAL (operands[2]) < 0
2564 && INTVAL (operands[2]) >= -8)
2565 {
2566 operands[2] = GEN_INT (- INTVAL (operands[2]));
2567 return "subq%.l %2,%0";
2568 }
2569 /* On the CPU32 it is faster to use two addql instructions to
2570 add a small integer (8 < N <= 16) to a register.
2571 Likewise for subql. */
2572 if (TUNE_CPU32 && REG_P (operands[0]))
2573 {
2574 if (INTVAL (operands[2]) > 8
2575 && INTVAL (operands[2]) <= 16)
2576 {
2577 operands[2] = GEN_INT (INTVAL (operands[2]) - 8);
2578 return "addq%.l #8,%0\n\taddq%.l %2,%0";
2579 }
2580 if (INTVAL (operands[2]) < -8
2581 && INTVAL (operands[2]) >= -16)
2582 {
2583 operands[2] = GEN_INT (- INTVAL (operands[2]) - 8);
2584 return "subq%.l #8,%0\n\tsubq%.l %2,%0";
2585 }
2586 }
2587 if (ADDRESS_REG_P (operands[0])
2588 && INTVAL (operands[2]) >= -0x8000
2589 && INTVAL (operands[2]) < 0x8000)
2590 {
2591 if (TUNE_68040)
2592 return "add%.w %2,%0";
2593 else
2594 return MOTOROLA ? "lea (%c2,%0),%0" : "lea %0@(%c2),%0";
2595 }
2596 }
2597 return "add%.l %2,%0";
2598 }
2599 \f
2600 /* Store in cc_status the expressions that the condition codes will
2601 describe after execution of an instruction whose pattern is EXP.
2602 Do not alter them if the instruction would not alter the cc's. */
2603
2604 /* On the 68000, all the insns to store in an address register fail to
2605 set the cc's. However, in some cases these instructions can make it
2606 possibly invalid to use the saved cc's. In those cases we clear out
2607 some or all of the saved cc's so they won't be used. */
2608
2609 void
2610 notice_update_cc (rtx exp, rtx insn)
2611 {
2612 if (GET_CODE (exp) == SET)
2613 {
2614 if (GET_CODE (SET_SRC (exp)) == CALL)
2615 CC_STATUS_INIT;
2616 else if (ADDRESS_REG_P (SET_DEST (exp)))
2617 {
2618 if (cc_status.value1 && modified_in_p (cc_status.value1, insn))
2619 cc_status.value1 = 0;
2620 if (cc_status.value2 && modified_in_p (cc_status.value2, insn))
2621 cc_status.value2 = 0;
2622 }
2623 else if (!FP_REG_P (SET_DEST (exp))
2624 && SET_DEST (exp) != cc0_rtx
2625 && (FP_REG_P (SET_SRC (exp))
2626 || GET_CODE (SET_SRC (exp)) == FIX
2627 || GET_CODE (SET_SRC (exp)) == FLOAT_TRUNCATE
2628 || GET_CODE (SET_SRC (exp)) == FLOAT_EXTEND))
2629 CC_STATUS_INIT;
2630 /* A pair of move insns doesn't produce a useful overall cc. */
2631 else if (!FP_REG_P (SET_DEST (exp))
2632 && !FP_REG_P (SET_SRC (exp))
2633 && GET_MODE_SIZE (GET_MODE (SET_SRC (exp))) > 4
2634 && (GET_CODE (SET_SRC (exp)) == REG
2635 || GET_CODE (SET_SRC (exp)) == MEM
2636 || GET_CODE (SET_SRC (exp)) == CONST_DOUBLE))
2637 CC_STATUS_INIT;
2638 else if (SET_DEST (exp) != pc_rtx)
2639 {
2640 cc_status.flags = 0;
2641 cc_status.value1 = SET_DEST (exp);
2642 cc_status.value2 = SET_SRC (exp);
2643 }
2644 }
2645 else if (GET_CODE (exp) == PARALLEL
2646 && GET_CODE (XVECEXP (exp, 0, 0)) == SET)
2647 {
2648 rtx dest = SET_DEST (XVECEXP (exp, 0, 0));
2649 rtx src = SET_SRC (XVECEXP (exp, 0, 0));
2650
2651 if (ADDRESS_REG_P (dest))
2652 CC_STATUS_INIT;
2653 else if (dest != pc_rtx)
2654 {
2655 cc_status.flags = 0;
2656 cc_status.value1 = dest;
2657 cc_status.value2 = src;
2658 }
2659 }
2660 else
2661 CC_STATUS_INIT;
2662 if (cc_status.value2 != 0
2663 && ADDRESS_REG_P (cc_status.value2)
2664 && GET_MODE (cc_status.value2) == QImode)
2665 CC_STATUS_INIT;
2666 if (cc_status.value2 != 0)
2667 switch (GET_CODE (cc_status.value2))
2668 {
2669 case ASHIFT: case ASHIFTRT: case LSHIFTRT:
2670 case ROTATE: case ROTATERT:
2671 /* These instructions always clear the overflow bit, and set
2672 the carry to the bit shifted out. */
2673 /* ??? We don't currently have a way to signal carry not valid,
2674 nor do we check for it in the branch insns. */
2675 CC_STATUS_INIT;
2676 break;
2677
2678 case PLUS: case MINUS: case MULT:
2679 case DIV: case UDIV: case MOD: case UMOD: case NEG:
2680 if (GET_MODE (cc_status.value2) != VOIDmode)
2681 cc_status.flags |= CC_NO_OVERFLOW;
2682 break;
2683 case ZERO_EXTEND:
2684 /* (SET r1 (ZERO_EXTEND r2)) on this machine
2685 ends with a move insn moving r2 in r2's mode.
2686 Thus, the cc's are set for r2.
2687 This can set N bit spuriously. */
2688 cc_status.flags |= CC_NOT_NEGATIVE;
2689
2690 default:
2691 break;
2692 }
2693 if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
2694 && cc_status.value2
2695 && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
2696 cc_status.value2 = 0;
2697 if (((cc_status.value1 && FP_REG_P (cc_status.value1))
2698 || (cc_status.value2 && FP_REG_P (cc_status.value2))))
2699 cc_status.flags = CC_IN_68881;
2700 }
2701 \f
2702 const char *
2703 output_move_const_double (rtx *operands)
2704 {
2705 int code = standard_68881_constant_p (operands[1]);
2706
2707 if (code != 0)
2708 {
2709 static char buf[40];
2710
2711 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
2712 return buf;
2713 }
2714 return "fmove%.d %1,%0";
2715 }
2716
2717 const char *
2718 output_move_const_single (rtx *operands)
2719 {
2720 int code = standard_68881_constant_p (operands[1]);
2721
2722 if (code != 0)
2723 {
2724 static char buf[40];
2725
2726 sprintf (buf, "fmovecr #0x%x,%%0", code & 0xff);
2727 return buf;
2728 }
2729 return "fmove%.s %f1,%0";
2730 }
2731
2732 /* Return nonzero if X, a CONST_DOUBLE, has a value that we can get
2733 from the "fmovecr" instruction.
2734 The value, anded with 0xff, gives the code to use in fmovecr
2735 to get the desired constant. */
2736
2737 /* This code has been fixed for cross-compilation. */
2738
2739 static int inited_68881_table = 0;
2740
2741 static const char *const strings_68881[7] = {
2742 "0.0",
2743 "1.0",
2744 "10.0",
2745 "100.0",
2746 "10000.0",
2747 "1e8",
2748 "1e16"
2749 };
2750
2751 static const int codes_68881[7] = {
2752 0x0f,
2753 0x32,
2754 0x33,
2755 0x34,
2756 0x35,
2757 0x36,
2758 0x37
2759 };
2760
2761 REAL_VALUE_TYPE values_68881[7];
2762
2763 /* Set up values_68881 array by converting the decimal values
2764 strings_68881 to binary. */
2765
2766 void
2767 init_68881_table (void)
2768 {
2769 int i;
2770 REAL_VALUE_TYPE r;
2771 enum machine_mode mode;
2772
2773 mode = SFmode;
2774 for (i = 0; i < 7; i++)
2775 {
2776 if (i == 6)
2777 mode = DFmode;
2778 r = REAL_VALUE_ATOF (strings_68881[i], mode);
2779 values_68881[i] = r;
2780 }
2781 inited_68881_table = 1;
2782 }
2783
2784 int
2785 standard_68881_constant_p (rtx x)
2786 {
2787 REAL_VALUE_TYPE r;
2788 int i;
2789
2790 /* fmovecr must be emulated on the 68040 and 68060, so it shouldn't be
2791 used at all on those chips. */
2792 if (TUNE_68040_60)
2793 return 0;
2794
2795 if (! inited_68881_table)
2796 init_68881_table ();
2797
2798 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2799
2800 /* Use REAL_VALUES_IDENTICAL instead of REAL_VALUES_EQUAL so that -0.0
2801 is rejected. */
2802 for (i = 0; i < 6; i++)
2803 {
2804 if (REAL_VALUES_IDENTICAL (r, values_68881[i]))
2805 return (codes_68881[i]);
2806 }
2807
2808 if (GET_MODE (x) == SFmode)
2809 return 0;
2810
2811 if (REAL_VALUES_EQUAL (r, values_68881[6]))
2812 return (codes_68881[6]);
2813
2814 /* larger powers of ten in the constants ram are not used
2815 because they are not equal to a `double' C constant. */
2816 return 0;
2817 }
2818
2819 /* If X is a floating-point constant, return the logarithm of X base 2,
2820 or 0 if X is not a power of 2. */
2821
2822 int
2823 floating_exact_log2 (rtx x)
2824 {
2825 REAL_VALUE_TYPE r, r1;
2826 int exp;
2827
2828 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2829
2830 if (REAL_VALUES_LESS (r, dconst1))
2831 return 0;
2832
2833 exp = real_exponent (&r);
2834 real_2expN (&r1, exp);
2835 if (REAL_VALUES_EQUAL (r1, r))
2836 return exp;
2837
2838 return 0;
2839 }
2840 \f
2841 /* A C compound statement to output to stdio stream STREAM the
2842 assembler syntax for an instruction operand X. X is an RTL
2843 expression.
2844
2845 CODE is a value that can be used to specify one of several ways
2846 of printing the operand. It is used when identical operands
2847 must be printed differently depending on the context. CODE
2848 comes from the `%' specification that was used to request
2849 printing of the operand. If the specification was just `%DIGIT'
2850 then CODE is 0; if the specification was `%LTR DIGIT' then CODE
2851 is the ASCII code for LTR.
2852
2853 If X is a register, this macro should print the register's name.
2854 The names can be found in an array `reg_names' whose type is
2855 `char *[]'. `reg_names' is initialized from `REGISTER_NAMES'.
2856
2857 When the machine description has a specification `%PUNCT' (a `%'
2858 followed by a punctuation character), this macro is called with
2859 a null pointer for X and the punctuation character for CODE.
2860
2861 The m68k specific codes are:
2862
2863 '.' for dot needed in Motorola-style opcode names.
2864 '-' for an operand pushing on the stack:
2865 sp@-, -(sp) or -(%sp) depending on the style of syntax.
2866 '+' for an operand pushing on the stack:
2867 sp@+, (sp)+ or (%sp)+ depending on the style of syntax.
2868 '@' for a reference to the top word on the stack:
2869 sp@, (sp) or (%sp) depending on the style of syntax.
2870 '#' for an immediate operand prefix (# in MIT and Motorola syntax
2871 but & in SGS syntax).
2872 '!' for the cc register (used in an `and to cc' insn).
2873 '$' for the letter `s' in an op code, but only on the 68040.
2874 '&' for the letter `d' in an op code, but only on the 68040.
2875 '/' for register prefix needed by longlong.h.
2876
2877 'b' for byte insn (no effect, on the Sun; this is for the ISI).
2878 'd' to force memory addressing to be absolute, not relative.
2879 'f' for float insn (print a CONST_DOUBLE as a float rather than in hex)
2880 'o' for operands to go directly to output_operand_address (bypassing
2881 print_operand_address--used only for SYMBOL_REFs under TARGET_PCREL)
2882 'x' for float insn (print a CONST_DOUBLE as a float rather than in hex),
2883 or print pair of registers as rx:ry.
2884
2885 */
2886
2887 void
2888 print_operand (FILE *file, rtx op, int letter)
2889 {
2890 if (letter == '.')
2891 {
2892 if (MOTOROLA)
2893 fprintf (file, ".");
2894 }
2895 else if (letter == '#')
2896 asm_fprintf (file, "%I");
2897 else if (letter == '-')
2898 asm_fprintf (file, MOTOROLA ? "-(%Rsp)" : "%Rsp@-");
2899 else if (letter == '+')
2900 asm_fprintf (file, MOTOROLA ? "(%Rsp)+" : "%Rsp@+");
2901 else if (letter == '@')
2902 asm_fprintf (file, MOTOROLA ? "(%Rsp)" : "%Rsp@");
2903 else if (letter == '!')
2904 asm_fprintf (file, "%Rfpcr");
2905 else if (letter == '$')
2906 {
2907 if (TARGET_68040_ONLY)
2908 fprintf (file, "s");
2909 }
2910 else if (letter == '&')
2911 {
2912 if (TARGET_68040_ONLY)
2913 fprintf (file, "d");
2914 }
2915 else if (letter == '/')
2916 asm_fprintf (file, "%R");
2917 else if (letter == 'o')
2918 {
2919 /* This is only for direct addresses with TARGET_PCREL */
2920 gcc_assert (GET_CODE (op) == MEM
2921 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
2922 && TARGET_PCREL);
2923 output_addr_const (file, XEXP (op, 0));
2924 }
2925 else if (GET_CODE (op) == REG)
2926 {
2927 if (letter == 'R')
2928 /* Print out the second register name of a register pair.
2929 I.e., R (6) => 7. */
2930 fputs (M68K_REGNAME(REGNO (op) + 1), file);
2931 else
2932 fputs (M68K_REGNAME(REGNO (op)), file);
2933 }
2934 else if (GET_CODE (op) == MEM)
2935 {
2936 output_address (XEXP (op, 0));
2937 if (letter == 'd' && ! TARGET_68020
2938 && CONSTANT_ADDRESS_P (XEXP (op, 0))
2939 && !(GET_CODE (XEXP (op, 0)) == CONST_INT
2940 && INTVAL (XEXP (op, 0)) < 0x8000
2941 && INTVAL (XEXP (op, 0)) >= -0x8000))
2942 fprintf (file, MOTOROLA ? ".l" : ":l");
2943 }
2944 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
2945 {
2946 REAL_VALUE_TYPE r;
2947 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2948 ASM_OUTPUT_FLOAT_OPERAND (letter, file, r);
2949 }
2950 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == XFmode)
2951 {
2952 REAL_VALUE_TYPE r;
2953 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2954 ASM_OUTPUT_LONG_DOUBLE_OPERAND (file, r);
2955 }
2956 else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
2957 {
2958 REAL_VALUE_TYPE r;
2959 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2960 ASM_OUTPUT_DOUBLE_OPERAND (file, r);
2961 }
2962 else
2963 {
2964 /* Use `print_operand_address' instead of `output_addr_const'
2965 to ensure that we print relevant PIC stuff. */
2966 asm_fprintf (file, "%I");
2967 if (TARGET_PCREL
2968 && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST))
2969 print_operand_address (file, op);
2970 else
2971 output_addr_const (file, op);
2972 }
2973 }
2974
2975 \f
2976 /* A C compound statement to output to stdio stream STREAM the
2977 assembler syntax for an instruction operand that is a memory
2978 reference whose address is ADDR. ADDR is an RTL expression.
2979
2980 Note that this contains a kludge that knows that the only reason
2981 we have an address (plus (label_ref...) (reg...)) when not generating
2982 PIC code is in the insn before a tablejump, and we know that m68k.md
2983 generates a label LInnn: on such an insn.
2984
2985 It is possible for PIC to generate a (plus (label_ref...) (reg...))
2986 and we handle that just like we would a (plus (symbol_ref...) (reg...)).
2987
2988 Some SGS assemblers have a bug such that "Lnnn-LInnn-2.b(pc,d0.l*2)"
2989 fails to assemble. Luckily "Lnnn(pc,d0.l*2)" produces the results
2990 we want. This difference can be accommodated by using an assembler
2991 define such "LDnnn" to be either "Lnnn-LInnn-2.b", "Lnnn", or any other
2992 string, as necessary. This is accomplished via the ASM_OUTPUT_CASE_END
2993 macro. See m68k/sgs.h for an example; for versions without the bug.
2994 Some assemblers refuse all the above solutions. The workaround is to
2995 emit "K(pc,d0.l*2)" with K being a small constant known to give the
2996 right behavior.
2997
2998 They also do not like things like "pea 1.w", so we simple leave off
2999 the .w on small constants.
3000
3001 This routine is responsible for distinguishing between -fpic and -fPIC
3002 style relocations in an address. When generating -fpic code the
3003 offset is output in word mode (e.g. movel a5@(_foo:w), a0). When generating
3004 -fPIC code the offset is output in long mode (e.g. movel a5@(_foo:l), a0) */
3005
3006 #if MOTOROLA
3007 # define ASM_OUTPUT_CASE_FETCH(file, labelno, regname) \
3008 asm_fprintf (file, "%LL%d-%LLI%d.b(%Rpc,%s.", labelno, labelno, regname)
3009 #else /* !MOTOROLA */
3010 # define ASM_OUTPUT_CASE_FETCH(file, labelno, regname) \
3011 asm_fprintf (file, "%Rpc@(%LL%d-%LLI%d-2:b,%s:", labelno, labelno, regname)
3012 #endif /* !MOTOROLA */
3013
3014 void
3015 print_operand_address (FILE *file, rtx addr)
3016 {
3017 register rtx reg1, reg2, breg, ireg;
3018 rtx offset;
3019
3020 switch (GET_CODE (addr))
3021 {
3022 case REG:
3023 fprintf (file, MOTOROLA ? "(%s)" : "%s@", M68K_REGNAME (REGNO (addr)));
3024 break;
3025 case PRE_DEC:
3026 fprintf (file, MOTOROLA ? "-(%s)" : "%s@-",
3027 M68K_REGNAME (REGNO (XEXP (addr, 0))));
3028 break;
3029 case POST_INC:
3030 fprintf (file, MOTOROLA ? "(%s)+" : "%s@+",
3031 M68K_REGNAME (REGNO (XEXP (addr, 0))));
3032 break;
3033 case PLUS:
3034 reg1 = reg2 = ireg = breg = offset = 0;
3035 if (CONSTANT_ADDRESS_P (XEXP (addr, 0)))
3036 {
3037 offset = XEXP (addr, 0);
3038 addr = XEXP (addr, 1);
3039 }
3040 else if (CONSTANT_ADDRESS_P (XEXP (addr, 1)))
3041 {
3042 offset = XEXP (addr, 1);
3043 addr = XEXP (addr, 0);
3044 }
3045 if (GET_CODE (addr) != PLUS)
3046 {
3047 ;
3048 }
3049 else if (GET_CODE (XEXP (addr, 0)) == SIGN_EXTEND)
3050 {
3051 reg1 = XEXP (addr, 0);
3052 addr = XEXP (addr, 1);
3053 }
3054 else if (GET_CODE (XEXP (addr, 1)) == SIGN_EXTEND)
3055 {
3056 reg1 = XEXP (addr, 1);
3057 addr = XEXP (addr, 0);
3058 }
3059 else if (GET_CODE (XEXP (addr, 0)) == MULT)
3060 {
3061 reg1 = XEXP (addr, 0);
3062 addr = XEXP (addr, 1);
3063 }
3064 else if (GET_CODE (XEXP (addr, 1)) == MULT)
3065 {
3066 reg1 = XEXP (addr, 1);
3067 addr = XEXP (addr, 0);
3068 }
3069 else if (GET_CODE (XEXP (addr, 0)) == REG)
3070 {
3071 reg1 = XEXP (addr, 0);
3072 addr = XEXP (addr, 1);
3073 }
3074 else if (GET_CODE (XEXP (addr, 1)) == REG)
3075 {
3076 reg1 = XEXP (addr, 1);
3077 addr = XEXP (addr, 0);
3078 }
3079 if (GET_CODE (addr) == REG || GET_CODE (addr) == MULT
3080 || GET_CODE (addr) == SIGN_EXTEND)
3081 {
3082 if (reg1 == 0)
3083 reg1 = addr;
3084 else
3085 reg2 = addr;
3086 addr = 0;
3087 }
3088 #if 0 /* for OLD_INDEXING */
3089 else if (GET_CODE (addr) == PLUS)
3090 {
3091 if (GET_CODE (XEXP (addr, 0)) == REG)
3092 {
3093 reg2 = XEXP (addr, 0);
3094 addr = XEXP (addr, 1);
3095 }
3096 else if (GET_CODE (XEXP (addr, 1)) == REG)
3097 {
3098 reg2 = XEXP (addr, 1);
3099 addr = XEXP (addr, 0);
3100 }
3101 }
3102 #endif
3103 if (offset != 0)
3104 {
3105 gcc_assert (!addr);
3106 addr = offset;
3107 }
3108 if ((reg1 && (GET_CODE (reg1) == SIGN_EXTEND
3109 || GET_CODE (reg1) == MULT))
3110 || (reg2 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg2))))
3111 {
3112 breg = reg2;
3113 ireg = reg1;
3114 }
3115 else if (reg1 != 0 && REGNO_OK_FOR_BASE_P (REGNO (reg1)))
3116 {
3117 breg = reg1;
3118 ireg = reg2;
3119 }
3120 if (ireg != 0 && breg == 0 && GET_CODE (addr) == LABEL_REF
3121 && ! (flag_pic && ireg == pic_offset_table_rtx))
3122 {
3123 int scale = 1;
3124 if (GET_CODE (ireg) == MULT)
3125 {
3126 scale = INTVAL (XEXP (ireg, 1));
3127 ireg = XEXP (ireg, 0);
3128 }
3129 if (GET_CODE (ireg) == SIGN_EXTEND)
3130 {
3131 ASM_OUTPUT_CASE_FETCH (file,
3132 CODE_LABEL_NUMBER (XEXP (addr, 0)),
3133 M68K_REGNAME (REGNO (XEXP (ireg, 0))));
3134 fprintf (file, "w");
3135 }
3136 else
3137 {
3138 ASM_OUTPUT_CASE_FETCH (file,
3139 CODE_LABEL_NUMBER (XEXP (addr, 0)),
3140 M68K_REGNAME (REGNO (ireg)));
3141 fprintf (file, "l");
3142 }
3143 if (scale != 1)
3144 fprintf (file, MOTOROLA ? "*%d" : ":%d", scale);
3145 putc (')', file);
3146 break;
3147 }
3148 if (breg != 0 && ireg == 0 && GET_CODE (addr) == LABEL_REF
3149 && ! (flag_pic && breg == pic_offset_table_rtx))
3150 {
3151 ASM_OUTPUT_CASE_FETCH (file,
3152 CODE_LABEL_NUMBER (XEXP (addr, 0)),
3153 M68K_REGNAME (REGNO (breg)));
3154 fprintf (file, "l)");
3155 break;
3156 }
3157 if (ireg != 0 || breg != 0)
3158 {
3159 int scale = 1;
3160
3161 gcc_assert (breg);
3162 gcc_assert (flag_pic || !addr || GET_CODE (addr) != LABEL_REF);
3163
3164 if (MOTOROLA)
3165 {
3166 if (addr != 0)
3167 {
3168 output_addr_const (file, addr);
3169 if (flag_pic && (breg == pic_offset_table_rtx))
3170 {
3171 fprintf (file, "@GOT");
3172 if (flag_pic == 1)
3173 fprintf (file, ".w");
3174 }
3175 }
3176 fprintf (file, "(%s", M68K_REGNAME (REGNO (breg)));
3177 if (ireg != 0)
3178 putc (',', file);
3179 }
3180 else /* !MOTOROLA */
3181 {
3182 fprintf (file, "%s@(", M68K_REGNAME (REGNO (breg)));
3183 if (addr != 0)
3184 {
3185 output_addr_const (file, addr);
3186 if (breg == pic_offset_table_rtx)
3187 switch (flag_pic)
3188 {
3189 case 1:
3190 fprintf (file, ":w");
3191 break;
3192 case 2:
3193 fprintf (file, ":l");
3194 break;
3195 default:
3196 break;
3197 }
3198 if (ireg != 0)
3199 putc (',', file);
3200 }
3201 } /* !MOTOROLA */
3202 if (ireg != 0 && GET_CODE (ireg) == MULT)
3203 {
3204 scale = INTVAL (XEXP (ireg, 1));
3205 ireg = XEXP (ireg, 0);
3206 }
3207 if (ireg != 0 && GET_CODE (ireg) == SIGN_EXTEND)
3208 fprintf (file, MOTOROLA ? "%s.w" : "%s:w",
3209 M68K_REGNAME (REGNO (XEXP (ireg, 0))));
3210 else if (ireg != 0)
3211 fprintf (file, MOTOROLA ? "%s.l" : "%s:l",
3212 M68K_REGNAME (REGNO (ireg)));
3213 if (scale != 1)
3214 fprintf (file, MOTOROLA ? "*%d" : ":%d", scale);
3215 putc (')', file);
3216 break;
3217 }
3218 else if (reg1 != 0 && GET_CODE (addr) == LABEL_REF
3219 && ! (flag_pic && reg1 == pic_offset_table_rtx))
3220 {
3221 ASM_OUTPUT_CASE_FETCH (file,
3222 CODE_LABEL_NUMBER (XEXP (addr, 0)),
3223 M68K_REGNAME (REGNO (reg1)));
3224 fprintf (file, "l)");
3225 break;
3226 }
3227 /* FALL-THROUGH (is this really what we want?) */
3228 default:
3229 if (GET_CODE (addr) == CONST_INT
3230 && INTVAL (addr) < 0x8000
3231 && INTVAL (addr) >= -0x8000)
3232 {
3233 fprintf (file, MOTOROLA ? "%d.w" : "%d:w", (int) INTVAL (addr));
3234 }
3235 else if (GET_CODE (addr) == CONST_INT)
3236 {
3237 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
3238 }
3239 else if (TARGET_PCREL)
3240 {
3241 fputc ('(', file);
3242 output_addr_const (file, addr);
3243 if (flag_pic == 1)
3244 asm_fprintf (file, ":w,%Rpc)");
3245 else
3246 asm_fprintf (file, ":l,%Rpc)");
3247 }
3248 else
3249 {
3250 /* Special case for SYMBOL_REF if the symbol name ends in
3251 `.<letter>', this can be mistaken as a size suffix. Put
3252 the name in parentheses. */
3253 if (GET_CODE (addr) == SYMBOL_REF
3254 && strlen (XSTR (addr, 0)) > 2
3255 && XSTR (addr, 0)[strlen (XSTR (addr, 0)) - 2] == '.')
3256 {
3257 putc ('(', file);
3258 output_addr_const (file, addr);
3259 putc (')', file);
3260 }
3261 else
3262 output_addr_const (file, addr);
3263 }
3264 break;
3265 }
3266 }
3267 \f
3268 /* Check for cases where a clr insns can be omitted from code using
3269 strict_low_part sets. For example, the second clrl here is not needed:
3270 clrl d0; movw a0@+,d0; use d0; clrl d0; movw a0@+; use d0; ...
3271
3272 MODE is the mode of this STRICT_LOW_PART set. FIRST_INSN is the clear
3273 insn we are checking for redundancy. TARGET is the register set by the
3274 clear insn. */
3275
3276 bool
3277 strict_low_part_peephole_ok (enum machine_mode mode, rtx first_insn,
3278 rtx target)
3279 {
3280 rtx p;
3281
3282 p = prev_nonnote_insn (first_insn);
3283
3284 while (p)
3285 {
3286 /* If it isn't an insn, then give up. */
3287 if (GET_CODE (p) != INSN)
3288 return false;
3289
3290 if (reg_set_p (target, p))
3291 {
3292 rtx set = single_set (p);
3293 rtx dest;
3294
3295 /* If it isn't an easy to recognize insn, then give up. */
3296 if (! set)
3297 return false;
3298
3299 dest = SET_DEST (set);
3300
3301 /* If this sets the entire target register to zero, then our
3302 first_insn is redundant. */
3303 if (rtx_equal_p (dest, target)
3304 && SET_SRC (set) == const0_rtx)
3305 return true;
3306 else if (GET_CODE (dest) == STRICT_LOW_PART
3307 && GET_CODE (XEXP (dest, 0)) == REG
3308 && REGNO (XEXP (dest, 0)) == REGNO (target)
3309 && (GET_MODE_SIZE (GET_MODE (XEXP (dest, 0)))
3310 <= GET_MODE_SIZE (mode)))
3311 /* This is a strict low part set which modifies less than
3312 we are using, so it is safe. */
3313 ;
3314 else
3315 return false;
3316 }
3317
3318 p = prev_nonnote_insn (p);
3319 }
3320
3321 return false;
3322 }
3323
3324 /* Operand predicates for implementing asymmetric pc-relative addressing
3325 on m68k. The m68k supports pc-relative addressing (mode 7, register 2)
3326 when used as a source operand, but not as a destination operand.
3327
3328 We model this by restricting the meaning of the basic predicates
3329 (general_operand, memory_operand, etc) to forbid the use of this
3330 addressing mode, and then define the following predicates that permit
3331 this addressing mode. These predicates can then be used for the
3332 source operands of the appropriate instructions.
3333
3334 n.b. While it is theoretically possible to change all machine patterns
3335 to use this addressing more where permitted by the architecture,
3336 it has only been implemented for "common" cases: SImode, HImode, and
3337 QImode operands, and only for the principle operations that would
3338 require this addressing mode: data movement and simple integer operations.
3339
3340 In parallel with these new predicates, two new constraint letters
3341 were defined: 'S' and 'T'. 'S' is the -mpcrel analog of 'm'.
3342 'T' replaces 's' in the non-pcrel case. It is a no-op in the pcrel case.
3343 In the pcrel case 's' is only valid in combination with 'a' registers.
3344 See addsi3, subsi3, cmpsi, and movsi patterns for a better understanding
3345 of how these constraints are used.
3346
3347 The use of these predicates is strictly optional, though patterns that
3348 don't will cause an extra reload register to be allocated where one
3349 was not necessary:
3350
3351 lea (abc:w,%pc),%a0 ; need to reload address
3352 moveq &1,%d1 ; since write to pc-relative space
3353 movel %d1,%a0@ ; is not allowed
3354 ...
3355 lea (abc:w,%pc),%a1 ; no need to reload address here
3356 movel %a1@,%d0 ; since "movel (abc:w,%pc),%d0" is ok
3357
3358 For more info, consult tiemann@cygnus.com.
3359
3360
3361 All of the ugliness with predicates and constraints is due to the
3362 simple fact that the m68k does not allow a pc-relative addressing
3363 mode as a destination. gcc does not distinguish between source and
3364 destination addresses. Hence, if we claim that pc-relative address
3365 modes are valid, e.g. GO_IF_LEGITIMATE_ADDRESS accepts them, then we
3366 end up with invalid code. To get around this problem, we left
3367 pc-relative modes as invalid addresses, and then added special
3368 predicates and constraints to accept them.
3369
3370 A cleaner way to handle this is to modify gcc to distinguish
3371 between source and destination addresses. We can then say that
3372 pc-relative is a valid source address but not a valid destination
3373 address, and hopefully avoid a lot of the predicate and constraint
3374 hackery. Unfortunately, this would be a pretty big change. It would
3375 be a useful change for a number of ports, but there aren't any current
3376 plans to undertake this.
3377
3378 ***************************************************************************/
3379
3380
3381 const char *
3382 output_andsi3 (rtx *operands)
3383 {
3384 int logval;
3385 if (GET_CODE (operands[2]) == CONST_INT
3386 && (INTVAL (operands[2]) | 0xffff) == -1
3387 && (DATA_REG_P (operands[0])
3388 || offsettable_memref_p (operands[0]))
3389 && !TARGET_COLDFIRE)
3390 {
3391 if (GET_CODE (operands[0]) != REG)
3392 operands[0] = adjust_address (operands[0], HImode, 2);
3393 operands[2] = GEN_INT (INTVAL (operands[2]) & 0xffff);
3394 /* Do not delete a following tstl %0 insn; that would be incorrect. */
3395 CC_STATUS_INIT;
3396 if (operands[2] == const0_rtx)
3397 return "clr%.w %0";
3398 return "and%.w %2,%0";
3399 }
3400 if (GET_CODE (operands[2]) == CONST_INT
3401 && (logval = exact_log2 (~ INTVAL (operands[2]))) >= 0
3402 && (DATA_REG_P (operands[0])
3403 || offsettable_memref_p (operands[0])))
3404 {
3405 if (DATA_REG_P (operands[0]))
3406 operands[1] = GEN_INT (logval);
3407 else
3408 {
3409 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3410 operands[1] = GEN_INT (logval % 8);
3411 }
3412 /* This does not set condition codes in a standard way. */
3413 CC_STATUS_INIT;
3414 return "bclr %1,%0";
3415 }
3416 return "and%.l %2,%0";
3417 }
3418
3419 const char *
3420 output_iorsi3 (rtx *operands)
3421 {
3422 register int logval;
3423 if (GET_CODE (operands[2]) == CONST_INT
3424 && INTVAL (operands[2]) >> 16 == 0
3425 && (DATA_REG_P (operands[0])
3426 || offsettable_memref_p (operands[0]))
3427 && !TARGET_COLDFIRE)
3428 {
3429 if (GET_CODE (operands[0]) != REG)
3430 operands[0] = adjust_address (operands[0], HImode, 2);
3431 /* Do not delete a following tstl %0 insn; that would be incorrect. */
3432 CC_STATUS_INIT;
3433 if (INTVAL (operands[2]) == 0xffff)
3434 return "mov%.w %2,%0";
3435 return "or%.w %2,%0";
3436 }
3437 if (GET_CODE (operands[2]) == CONST_INT
3438 && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
3439 && (DATA_REG_P (operands[0])
3440 || offsettable_memref_p (operands[0])))
3441 {
3442 if (DATA_REG_P (operands[0]))
3443 operands[1] = GEN_INT (logval);
3444 else
3445 {
3446 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3447 operands[1] = GEN_INT (logval % 8);
3448 }
3449 CC_STATUS_INIT;
3450 return "bset %1,%0";
3451 }
3452 return "or%.l %2,%0";
3453 }
3454
3455 const char *
3456 output_xorsi3 (rtx *operands)
3457 {
3458 register int logval;
3459 if (GET_CODE (operands[2]) == CONST_INT
3460 && INTVAL (operands[2]) >> 16 == 0
3461 && (offsettable_memref_p (operands[0]) || DATA_REG_P (operands[0]))
3462 && !TARGET_COLDFIRE)
3463 {
3464 if (! DATA_REG_P (operands[0]))
3465 operands[0] = adjust_address (operands[0], HImode, 2);
3466 /* Do not delete a following tstl %0 insn; that would be incorrect. */
3467 CC_STATUS_INIT;
3468 if (INTVAL (operands[2]) == 0xffff)
3469 return "not%.w %0";
3470 return "eor%.w %2,%0";
3471 }
3472 if (GET_CODE (operands[2]) == CONST_INT
3473 && (logval = exact_log2 (INTVAL (operands[2]))) >= 0
3474 && (DATA_REG_P (operands[0])
3475 || offsettable_memref_p (operands[0])))
3476 {
3477 if (DATA_REG_P (operands[0]))
3478 operands[1] = GEN_INT (logval);
3479 else
3480 {
3481 operands[0] = adjust_address (operands[0], SImode, 3 - (logval / 8));
3482 operands[1] = GEN_INT (logval % 8);
3483 }
3484 CC_STATUS_INIT;
3485 return "bchg %1,%0";
3486 }
3487 return "eor%.l %2,%0";
3488 }
3489
3490 #ifdef M68K_TARGET_COFF
3491
3492 /* Output assembly to switch to section NAME with attribute FLAGS. */
3493
3494 static void
3495 m68k_coff_asm_named_section (const char *name, unsigned int flags,
3496 tree decl ATTRIBUTE_UNUSED)
3497 {
3498 char flagchar;
3499
3500 if (flags & SECTION_WRITE)
3501 flagchar = 'd';
3502 else
3503 flagchar = 'x';
3504
3505 fprintf (asm_out_file, "\t.section\t%s,\"%c\"\n", name, flagchar);
3506 }
3507
3508 #endif /* M68K_TARGET_COFF */
3509
3510 static void
3511 m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
3512 HOST_WIDE_INT delta,
3513 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
3514 tree function)
3515 {
3516 rtx xops[1];
3517 const char *fmt;
3518
3519 if (delta > 0 && delta <= 8)
3520 asm_fprintf (file, (MOTOROLA
3521 ? "\taddq.l %I%d,4(%Rsp)\n"
3522 : "\taddql %I%d,%Rsp@(4)\n"),
3523 (int) delta);
3524 else if (delta < 0 && delta >= -8)
3525 asm_fprintf (file, (MOTOROLA
3526 ? "\tsubq.l %I%d,4(%Rsp)\n"
3527 : "\tsubql %I%d,%Rsp@(4)\n"),
3528 (int) -delta);
3529 else if (TARGET_COLDFIRE)
3530 {
3531 /* ColdFire can't add/sub a constant to memory unless it is in
3532 the range of addq/subq. So load the value into %d0 and
3533 then add it to 4(%sp). */
3534 if (delta >= -128 && delta <= 127)
3535 asm_fprintf (file, (MOTOROLA
3536 ? "\tmoveq.l %I%wd,%Rd0\n"
3537 : "\tmoveql %I%wd,%Rd0\n"),
3538 delta);
3539 else
3540 asm_fprintf (file, (MOTOROLA
3541 ? "\tmove.l %I%wd,%Rd0\n"
3542 : "\tmovel %I%wd,%Rd0\n"),
3543 delta);
3544 asm_fprintf (file, (MOTOROLA
3545 ? "\tadd.l %Rd0,4(%Rsp)\n"
3546 : "\taddl %Rd0,%Rsp@(4)\n"));
3547 }
3548 else
3549 asm_fprintf (file, (MOTOROLA
3550 ? "\tadd.l %I%wd,4(%Rsp)\n"
3551 : "\taddl %I%wd,%Rsp@(4)\n"),
3552 delta);
3553
3554 xops[0] = DECL_RTL (function);
3555
3556 /* Logic taken from call patterns in m68k.md. */
3557 if (flag_pic)
3558 {
3559 if (TARGET_PCREL)
3560 fmt = "bra.l %o0";
3561 else if (flag_pic == 1 || TARGET_68020)
3562 {
3563 if (MOTOROLA)
3564 {
3565 #if defined (USE_GAS)
3566 fmt = "bra.l %0@PLTPC";
3567 #else
3568 fmt = "bra %0@PLTPC";
3569 #endif
3570 }
3571 else /* !MOTOROLA */
3572 {
3573 #ifdef USE_GAS
3574 fmt = "bra.l %0";
3575 #else
3576 fmt = "jra %0,a1";
3577 #endif
3578 }
3579 }
3580 else if (optimize_size || TARGET_ID_SHARED_LIBRARY)
3581 fmt = "move.l %0@GOT(%%a5), %%a1\n\tjmp (%%a1)";
3582 else
3583 fmt = "lea %0-.-8,%%a1\n\tjmp 0(%%pc,%%a1)";
3584 }
3585 else
3586 {
3587 #if MOTOROLA && !defined (USE_GAS)
3588 fmt = "jmp %0";
3589 #else
3590 fmt = "jra %0";
3591 #endif
3592 }
3593
3594 output_asm_insn (fmt, xops);
3595 }
3596
3597 /* Worker function for TARGET_STRUCT_VALUE_RTX. */
3598
3599 static rtx
3600 m68k_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
3601 int incoming ATTRIBUTE_UNUSED)
3602 {
3603 return gen_rtx_REG (Pmode, M68K_STRUCT_VALUE_REGNUM);
3604 }
3605
3606 /* Return nonzero if register old_reg can be renamed to register new_reg. */
3607 int
3608 m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
3609 unsigned int new_reg)
3610 {
3611
3612 /* Interrupt functions can only use registers that have already been
3613 saved by the prologue, even if they would normally be
3614 call-clobbered. */
3615
3616 if (m68k_interrupt_function_p (current_function_decl)
3617 && !regs_ever_live[new_reg])
3618 return 0;
3619
3620 return 1;
3621 }
3622
3623 /* Value is true if hard register REGNO can hold a value of machine-mode MODE.
3624 On the 68000, the cpu registers can hold any mode except bytes in address
3625 registers, but the 68881 registers can hold only SFmode or DFmode. */
3626 bool
3627 m68k_regno_mode_ok (int regno, enum machine_mode mode)
3628 {
3629 if (regno < 8)
3630 {
3631 /* Data Registers, can hold aggregate if fits in. */
3632 if (regno + GET_MODE_SIZE (mode) / 4 <= 8)
3633 return true;
3634 }
3635 else if (regno < 16)
3636 {
3637 /* Address Registers, can't hold bytes, can hold aggregate if
3638 fits in. */
3639 if (GET_MODE_SIZE (mode) == 1)
3640 return false;
3641 if (regno + GET_MODE_SIZE (mode) / 4 <= 16)
3642 return true;
3643 }
3644 else if (regno < 24)
3645 {
3646 /* FPU registers, hold float or complex float of long double or
3647 smaller. */
3648 if ((GET_MODE_CLASS (mode) == MODE_FLOAT
3649 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3650 && GET_MODE_UNIT_SIZE (mode) <= TARGET_FP_REG_SIZE)
3651 return true;
3652 }
3653 return false;
3654 }
3655
3656 /* Return floating point values in a 68881 register. This makes 68881 code
3657 a little bit faster. It also makes -msoft-float code incompatible with
3658 hard-float code, so people have to be careful not to mix the two.
3659 For ColdFire it was decided the ABI incompatibility is undesirable.
3660 If there is need for a hard-float ABI it is probably worth doing it
3661 properly and also passing function arguments in FP registers. */
3662 rtx
3663 m68k_libcall_value (enum machine_mode mode)
3664 {
3665 switch (mode) {
3666 case SFmode:
3667 case DFmode:
3668 case XFmode:
3669 if (TARGET_68881)
3670 return gen_rtx_REG (mode, 16);
3671 break;
3672 default:
3673 break;
3674 }
3675 return gen_rtx_REG (mode, 0);
3676 }
3677
3678 rtx
3679 m68k_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
3680 {
3681 enum machine_mode mode;
3682
3683 mode = TYPE_MODE (valtype);
3684 switch (mode) {
3685 case SFmode:
3686 case DFmode:
3687 case XFmode:
3688 if (TARGET_68881)
3689 return gen_rtx_REG (mode, 16);
3690 break;
3691 default:
3692 break;
3693 }
3694
3695 /* If the function returns a pointer, push that into %a0 */
3696 if (POINTER_TYPE_P (valtype))
3697 return gen_rtx_REG (mode, 8);
3698 else
3699 return gen_rtx_REG (mode, 0);
3700 }