* config/m32c/m32c.c (m32c_prepare_shift): Add code to deal with
[official-gcc.git] / gcc / config / m32c / m32c.c
blob73d75d63e12be4c2f92d9debdc3c804a313ee5a4
1 /* Target Code for R8C/M16C/M32C
2 Copyright (C) 2005
3 Free Software Foundation, Inc.
4 Contributed by Red Hat.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 2, or (at your
11 option) any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
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 the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "reload.h"
39 #include "toplev.h"
40 #include "obstack.h"
41 #include "tree.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "except.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "tm_p.h"
50 #include "langhooks.h"
51 #include "tree-gimple.h"
53 /* Prototypes */
55 /* Used by m32c_pushm_popm. */
56 typedef enum
58 PP_pushm,
59 PP_popm,
60 PP_justcount
61 } Push_Pop_Type;
63 static tree interrupt_handler (tree *, tree, tree, int, bool *);
64 static int interrupt_p (tree node);
65 static bool m32c_asm_integer (rtx, unsigned int, int);
66 static int m32c_comp_type_attributes (tree, tree);
67 static bool m32c_fixed_condition_code_regs (unsigned int *, unsigned int *);
68 static struct machine_function *m32c_init_machine_status (void);
69 static void m32c_insert_attributes (tree, tree *);
70 static bool m32c_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
71 tree, bool);
72 static bool m32c_promote_prototypes (tree);
73 static int m32c_pushm_popm (Push_Pop_Type);
74 static bool m32c_strict_argument_naming (CUMULATIVE_ARGS *);
75 static rtx m32c_struct_value_rtx (tree, int);
76 static rtx m32c_subreg (enum machine_mode, rtx, enum machine_mode, int);
77 static int need_to_save (int);
79 #define streq(a,b) (strcmp ((a), (b)) == 0)
81 /* Internal support routines */
83 /* Debugging statements are tagged with DEBUG0 only so that they can
84 be easily enabled individually, by replacing the '0' with '1' as
85 needed. */
86 #define DEBUG0 0
87 #define DEBUG1 1
89 #if DEBUG0
90 /* This is needed by some of the commented-out debug statements
91 below. */
92 static char const *class_names[LIM_REG_CLASSES] = REG_CLASS_NAMES;
93 #endif
94 static int class_contents[LIM_REG_CLASSES][1] = REG_CLASS_CONTENTS;
96 /* These are all to support encode_pattern(). */
97 static char pattern[30], *patternp;
98 static GTY(()) rtx patternr[30];
99 #define RTX_IS(x) (streq (pattern, x))
101 /* Some macros to simplify the logic throughout this file. */
102 #define IS_MEM_REGNO(regno) ((regno) >= MEM0_REGNO && (regno) <= MEM7_REGNO)
103 #define IS_MEM_REG(rtx) (GET_CODE (rtx) == REG && IS_MEM_REGNO (REGNO (rtx)))
105 #define IS_CR_REGNO(regno) ((regno) >= SB_REGNO && (regno) <= PC_REGNO)
106 #define IS_CR_REG(rtx) (GET_CODE (rtx) == REG && IS_CR_REGNO (REGNO (rtx)))
108 /* We do most RTX matching by converting the RTX into a string, and
109 using string compares. This vastly simplifies the logic in many of
110 the functions in this file.
112 On exit, pattern[] has the encoded string (use RTX_IS("...") to
113 compare it) and patternr[] has pointers to the nodes in the RTX
114 corresponding to each character in the encoded string. The latter
115 is mostly used by print_operand().
117 Unrecognized patterns have '?' in them; this shows up when the
118 assembler complains about syntax errors.
121 static void
122 encode_pattern_1 (rtx x)
124 int i;
126 if (patternp == pattern + sizeof (pattern) - 2)
128 patternp[-1] = '?';
129 return;
132 patternr[patternp - pattern] = x;
134 switch (GET_CODE (x))
136 case REG:
137 *patternp++ = 'r';
138 break;
139 case SUBREG:
140 if (GET_MODE_SIZE (GET_MODE (x)) !=
141 GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
142 *patternp++ = 'S';
143 encode_pattern_1 (XEXP (x, 0));
144 break;
145 case MEM:
146 *patternp++ = 'm';
147 case CONST:
148 encode_pattern_1 (XEXP (x, 0));
149 break;
150 case PLUS:
151 *patternp++ = '+';
152 encode_pattern_1 (XEXP (x, 0));
153 encode_pattern_1 (XEXP (x, 1));
154 break;
155 case PRE_DEC:
156 *patternp++ = '>';
157 encode_pattern_1 (XEXP (x, 0));
158 break;
159 case POST_INC:
160 *patternp++ = '<';
161 encode_pattern_1 (XEXP (x, 0));
162 break;
163 case LO_SUM:
164 *patternp++ = 'L';
165 encode_pattern_1 (XEXP (x, 0));
166 encode_pattern_1 (XEXP (x, 1));
167 break;
168 case HIGH:
169 *patternp++ = 'H';
170 encode_pattern_1 (XEXP (x, 0));
171 break;
172 case SYMBOL_REF:
173 *patternp++ = 's';
174 break;
175 case LABEL_REF:
176 *patternp++ = 'l';
177 break;
178 case CODE_LABEL:
179 *patternp++ = 'c';
180 break;
181 case CONST_INT:
182 case CONST_DOUBLE:
183 *patternp++ = 'i';
184 break;
185 case UNSPEC:
186 *patternp++ = 'u';
187 *patternp++ = '0' + XCINT (x, 1, UNSPEC);
188 for (i = 0; i < XVECLEN (x, 0); i++)
189 encode_pattern_1 (XVECEXP (x, 0, i));
190 break;
191 case USE:
192 *patternp++ = 'U';
193 break;
194 case PARALLEL:
195 *patternp++ = '|';
196 for (i = 0; i < XVECLEN (x, 0); i++)
197 encode_pattern_1 (XVECEXP (x, 0, i));
198 break;
199 case EXPR_LIST:
200 *patternp++ = 'E';
201 encode_pattern_1 (XEXP (x, 0));
202 if (XEXP (x, 1))
203 encode_pattern_1 (XEXP (x, 1));
204 break;
205 default:
206 *patternp++ = '?';
207 #if DEBUG0
208 fprintf (stderr, "can't encode pattern %s\n",
209 GET_RTX_NAME (GET_CODE (x)));
210 debug_rtx (x);
211 gcc_unreachable ();
212 #endif
213 break;
217 static void
218 encode_pattern (rtx x)
220 patternp = pattern;
221 encode_pattern_1 (x);
222 *patternp = 0;
225 /* Since register names indicate the mode they're used in, we need a
226 way to determine which name to refer to the register with. Called
227 by print_operand(). */
229 static const char *
230 reg_name_with_mode (int regno, enum machine_mode mode)
232 int mlen = GET_MODE_SIZE (mode);
233 if (regno == R0_REGNO && mlen == 1)
234 return "r0l";
235 if (regno == R0_REGNO && (mlen == 3 || mlen == 4))
236 return "r2r0";
237 if (regno == R0_REGNO && mlen == 6)
238 return "r2r1r0";
239 if (regno == R0_REGNO && mlen == 8)
240 return "r3r1r2r0";
241 if (regno == R1_REGNO && mlen == 1)
242 return "r1l";
243 if (regno == R1_REGNO && (mlen == 3 || mlen == 4))
244 return "r3r1";
245 if (regno == A0_REGNO && TARGET_A16 && (mlen == 3 || mlen == 4))
246 return "a1a0";
247 return reg_names[regno];
250 /* How many bytes a register uses on stack when it's pushed. We need
251 to know this because the push opcode needs to explicitly indicate
252 the size of the register, even though the name of the register
253 already tells it that. Used by m32c_output_reg_{push,pop}, which
254 is only used through calls to ASM_OUTPUT_REG_{PUSH,POP}. */
256 static int
257 reg_push_size (int regno)
259 switch (regno)
261 case R0_REGNO:
262 case R1_REGNO:
263 return 2;
264 case R2_REGNO:
265 case R3_REGNO:
266 case FLG_REGNO:
267 return 2;
268 case A0_REGNO:
269 case A1_REGNO:
270 case SB_REGNO:
271 case FB_REGNO:
272 case SP_REGNO:
273 if (TARGET_A16)
274 return 2;
275 else
276 return 3;
277 default:
278 gcc_unreachable ();
282 static int *class_sizes = 0;
284 /* Given two register classes, find the largest intersection between
285 them. If there is no intersection, return RETURNED_IF_EMPTY
286 instead. */
287 static int
288 reduce_class (int original_class, int limiting_class, int returned_if_empty)
290 int cc = class_contents[original_class][0];
291 int i, best = NO_REGS;
292 int best_size = 0;
294 if (original_class == limiting_class)
295 return original_class;
297 if (!class_sizes)
299 int r;
300 class_sizes = (int *) xmalloc (LIM_REG_CLASSES * sizeof (int));
301 for (i = 0; i < LIM_REG_CLASSES; i++)
303 class_sizes[i] = 0;
304 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
305 if (class_contents[i][0] & (1 << r))
306 class_sizes[i]++;
310 cc &= class_contents[limiting_class][0];
311 for (i = 0; i < LIM_REG_CLASSES; i++)
313 int ic = class_contents[i][0];
315 if ((~cc & ic) == 0)
316 if (best_size < class_sizes[i])
318 best = i;
319 best_size = class_sizes[i];
323 if (best == NO_REGS)
324 return returned_if_empty;
325 return best;
328 /* Returns TRUE If there are any registers that exist in both register
329 classes. */
330 static int
331 classes_intersect (int class1, int class2)
333 return class_contents[class1][0] & class_contents[class2][0];
336 /* Used by m32c_register_move_cost to determine if a move is
337 impossibly expensive. */
338 static int
339 class_can_hold_mode (int class, enum machine_mode mode)
341 /* Cache the results: 0=untested 1=no 2=yes */
342 static char results[LIM_REG_CLASSES][MAX_MACHINE_MODE];
343 if (results[class][mode] == 0)
345 int r, n, i;
346 results[class][mode] = 1;
347 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
348 if (class_contents[class][0] & (1 << r)
349 && HARD_REGNO_MODE_OK (r, mode))
351 int ok = 1;
352 n = HARD_REGNO_NREGS (r, mode);
353 for (i = 1; i < n; i++)
354 if (!(class_contents[class][0] & (1 << (r + i))))
355 ok = 0;
356 if (ok)
358 results[class][mode] = 2;
359 break;
363 #if DEBUG0
364 fprintf (stderr, "class %s can hold %s? %s\n",
365 class_names[class], mode_name[mode],
366 (results[class][mode] == 2) ? "yes" : "no");
367 #endif
368 return results[class][mode] == 2;
371 /* Run-time Target Specification. */
373 /* Memregs are memory locations that gcc treats like general
374 registers, as there are a limited number of true registers and the
375 m32c families can use memory in most places that registers can be
376 used.
378 However, since memory accesses are more expensive than registers,
379 we allow the user to limit the number of memregs available, in
380 order to try to persuade gcc to try harder to use real registers.
382 Memregs are provided by m32c-lib1.S.
385 int target_memregs = 16;
386 static bool target_memregs_set = FALSE;
387 int ok_to_change_target_memregs = TRUE;
389 #undef TARGET_HANDLE_OPTION
390 #define TARGET_HANDLE_OPTION m32c_handle_option
391 static bool
392 m32c_handle_option (size_t code,
393 const char *arg ATTRIBUTE_UNUSED,
394 int value ATTRIBUTE_UNUSED)
396 if (code == OPT_memregs_)
398 target_memregs_set = TRUE;
399 target_memregs = atoi (arg);
401 return TRUE;
404 /* Implements OVERRIDE_OPTIONS. We limit memregs to 0..16, and
405 provide a default. */
406 void
407 m32c_override_options (void)
409 if (target_memregs_set)
411 if (target_memregs < 0 || target_memregs > 16)
412 error ("invalid target memregs value '%d'", target_memregs);
414 else
415 target_memregs = "16";
418 /* Defining data structures for per-function information */
420 /* The usual; we set up our machine_function data. */
421 static struct machine_function *
422 m32c_init_machine_status (void)
424 struct machine_function *machine;
425 machine =
426 (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
428 return machine;
431 /* Implements INIT_EXPANDERS. We just set up to call the above
432 function. */
433 void
434 m32c_init_expanders (void)
436 init_machine_status = m32c_init_machine_status;
439 /* Storage Layout */
441 #undef TARGET_PROMOTE_FUNCTION_RETURN
442 #define TARGET_PROMOTE_FUNCTION_RETURN m32c_promote_function_return
443 bool
444 m32c_promote_function_return (tree fntype ATTRIBUTE_UNUSED)
446 return false;
449 /* Register Basics */
451 /* Basic Characteristics of Registers */
453 /* Whether a mode fits in a register is complex enough to warrant a
454 table. */
455 static struct
457 char qi_regs;
458 char hi_regs;
459 char pi_regs;
460 char si_regs;
461 char di_regs;
462 } nregs_table[FIRST_PSEUDO_REGISTER] =
464 { 1, 1, 2, 2, 4 }, /* r0 */
465 { 0, 1, 0, 0, 0 }, /* r2 */
466 { 1, 1, 2, 2, 0 }, /* r1 */
467 { 0, 1, 0, 0, 0 }, /* r3 */
468 { 0, 1, 1, 0, 0 }, /* a0 */
469 { 0, 1, 1, 0, 0 }, /* a1 */
470 { 0, 1, 1, 0, 0 }, /* sb */
471 { 0, 1, 1, 0, 0 }, /* fb */
472 { 0, 1, 1, 0, 0 }, /* sp */
473 { 1, 1, 1, 0, 0 }, /* pc */
474 { 0, 0, 0, 0, 0 }, /* fl */
475 { 1, 1, 1, 0, 0 }, /* ap */
476 { 1, 1, 2, 2, 4 }, /* mem0 */
477 { 1, 1, 2, 2, 4 }, /* mem1 */
478 { 1, 1, 2, 2, 4 }, /* mem2 */
479 { 1, 1, 2, 2, 4 }, /* mem3 */
480 { 1, 1, 2, 2, 4 }, /* mem4 */
481 { 1, 1, 2, 2, 0 }, /* mem5 */
482 { 1, 1, 2, 2, 0 }, /* mem6 */
483 { 1, 1, 0, 0, 0 }, /* mem7 */
486 /* Implements CONDITIONAL_REGISTER_USAGE. We adjust the number of
487 available memregs, and select which registers need to be preserved
488 across calls based on the chip family. */
490 void
491 m32c_conditional_register_usage (void)
493 int memregs;
494 int i;
496 if (0 <= target_memregs && target_memregs <= 16)
498 /* The command line option is bytes, but our "registers" are
499 16-bit words. */
500 for (i = target_memregs/2; i < 8; i++)
502 fixed_regs[MEM0_REGNO + i] = 1;
503 CLEAR_HARD_REG_BIT (reg_class_contents[MEM_REGS], MEM0_REGNO + i);
507 /* M32CM and M32C preserve more registers across function calls. */
508 if (TARGET_A24)
510 call_used_regs[R1_REGNO] = 0;
511 call_used_regs[R2_REGNO] = 0;
512 call_used_regs[R3_REGNO] = 0;
513 call_used_regs[A0_REGNO] = 0;
514 call_used_regs[A1_REGNO] = 0;
518 /* How Values Fit in Registers */
520 /* Implements HARD_REGNO_NREGS. This is complicated by the fact that
521 different registers are different sizes from each other, *and* may
522 be different sizes in different chip families. */
524 m32c_hard_regno_nregs (int regno, enum machine_mode mode)
526 if (regno == FLG_REGNO && mode == CCmode)
527 return 1;
528 if (regno >= FIRST_PSEUDO_REGISTER)
529 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
531 if (regno >= MEM0_REGNO && regno <= MEM7_REGNO)
532 return (GET_MODE_SIZE (mode) + 1) / 2;
534 if (GET_MODE_SIZE (mode) <= 1)
535 return nregs_table[regno].qi_regs;
536 if (GET_MODE_SIZE (mode) <= 2)
537 return nregs_table[regno].hi_regs;
538 if (regno == A0_REGNO && mode == PSImode && TARGET_A16)
539 return 2;
540 if ((GET_MODE_SIZE (mode) <= 3 || mode == PSImode) && TARGET_A24)
541 return nregs_table[regno].pi_regs;
542 if (GET_MODE_SIZE (mode) <= 4)
543 return nregs_table[regno].si_regs;
544 if (GET_MODE_SIZE (mode) <= 8)
545 return nregs_table[regno].di_regs;
546 return 0;
549 /* Implements HARD_REGNO_MODE_OK. The above function does the work
550 already; just test its return value. */
552 m32c_hard_regno_ok (int regno, enum machine_mode mode)
554 return m32c_hard_regno_nregs (regno, mode) != 0;
557 /* Implements MODES_TIEABLE_P. In general, modes aren't tieable since
558 registers are all different sizes. However, since most modes are
559 bigger than our registers anyway, it's easier to implement this
560 function that way, leaving QImode as the only unique case. */
562 m32c_modes_tieable_p (enum machine_mode m1, enum machine_mode m2)
564 if (GET_MODE_SIZE (m1) == GET_MODE_SIZE (m2))
565 return 1;
567 if (m1 == QImode || m2 == QImode)
568 return 0;
570 return 1;
573 /* Register Classes */
575 /* Implements REGNO_REG_CLASS. */
576 enum machine_mode
577 m32c_regno_reg_class (int regno)
579 switch (regno)
581 case R0_REGNO:
582 return R0_REGS;
583 case R1_REGNO:
584 return R1_REGS;
585 case R2_REGNO:
586 return R2_REGS;
587 case R3_REGNO:
588 return R3_REGS;
589 case A0_REGNO:
590 case A1_REGNO:
591 return A_REGS;
592 case SB_REGNO:
593 return SB_REGS;
594 case FB_REGNO:
595 return FB_REGS;
596 case SP_REGNO:
597 return SP_REGS;
598 case FLG_REGNO:
599 return FLG_REGS;
600 default:
601 if (IS_MEM_REGNO (regno))
602 return MEM_REGS;
603 return ALL_REGS;
607 /* Implements REG_CLASS_FROM_CONSTRAINT. Note that some constraints only match
608 for certain chip families. */
610 m32c_reg_class_from_constraint (char c ATTRIBUTE_UNUSED, const char *s)
612 if (memcmp (s, "Rsp", 3) == 0)
613 return SP_REGS;
614 if (memcmp (s, "Rfb", 3) == 0)
615 return FB_REGS;
616 if (memcmp (s, "Rsb", 3) == 0)
617 return SB_REGS;
618 if (memcmp (s, "Rcr", 3) == 0 && TARGET_A16)
619 return CR_REGS;
620 if (memcmp (s, "Rcl", 3) == 0 && TARGET_A24)
621 return CR_REGS;
622 if (memcmp (s, "R0w", 3) == 0)
623 return R0_REGS;
624 if (memcmp (s, "R1w", 3) == 0)
625 return R1_REGS;
626 if (memcmp (s, "R2w", 3) == 0)
627 return R2_REGS;
628 if (memcmp (s, "R3w", 3) == 0)
629 return R3_REGS;
630 if (memcmp (s, "R02", 3) == 0)
631 return R02_REGS;
632 if (memcmp (s, "R03", 3) == 0)
633 return R03_REGS;
634 if (memcmp (s, "Rdi", 3) == 0)
635 return DI_REGS;
636 if (memcmp (s, "Rhl", 3) == 0)
637 return HL_REGS;
638 if (memcmp (s, "R23", 3) == 0)
639 return R23_REGS;
640 if (memcmp (s, "Raa", 3) == 0)
641 return A_REGS;
642 if (memcmp (s, "Raw", 3) == 0 && TARGET_A16)
643 return A_REGS;
644 if (memcmp (s, "Ral", 3) == 0 && TARGET_A24)
645 return A_REGS;
646 if (memcmp (s, "Rqi", 3) == 0)
647 return QI_REGS;
648 if (memcmp (s, "Rad", 3) == 0)
649 return AD_REGS;
650 if (memcmp (s, "Rsi", 3) == 0)
651 return SI_REGS;
652 if (memcmp (s, "Rhi", 3) == 0)
653 return HI_REGS;
654 if (memcmp (s, "Rhc", 3) == 0)
655 return HC_REGS;
656 if (memcmp (s, "Rra", 3) == 0)
657 return RA_REGS;
658 if (memcmp (s, "Rfl", 3) == 0)
659 return FLG_REGS;
660 if (memcmp (s, "Rmm", 3) == 0)
662 if (fixed_regs[MEM0_REGNO])
663 return NO_REGS;
664 return MEM_REGS;
667 /* PSImode registers - i.e. whatever can hold a pointer. */
668 if (memcmp (s, "Rpi", 3) == 0)
670 if (TARGET_A16)
671 return HI_REGS;
672 else
673 return RA_REGS; /* r2r0 and r3r1 can hold pointers. */
676 /* We handle this one as an EXTRA_CONSTRAINT. */
677 if (memcmp (s, "Rpa", 3) == 0)
678 return NO_REGS;
680 return NO_REGS;
683 /* Implements REGNO_OK_FOR_BASE_P. */
685 m32c_regno_ok_for_base_p (int regno)
687 if (regno == A0_REGNO
688 || regno == A1_REGNO || regno >= FIRST_PSEUDO_REGISTER)
689 return 1;
690 return 0;
693 #define DEBUG_RELOAD 0
695 /* Implements PREFERRED_RELOAD_CLASS. In general, prefer general
696 registers of the appropriate size. */
698 m32c_preferred_reload_class (rtx x, int rclass)
700 int newclass = rclass;
702 #if DEBUG_RELOAD
703 fprintf (stderr, "\npreferred_reload_class for %s is ",
704 class_names[rclass]);
705 #endif
706 if (rclass == NO_REGS)
707 rclass = GET_MODE (x) == QImode ? HL_REGS : R03_REGS;
709 if (classes_intersect (rclass, CR_REGS))
711 switch (GET_MODE (x))
713 case QImode:
714 newclass = HL_REGS;
715 break;
716 default:
717 /* newclass = HI_REGS; */
718 break;
722 else if (newclass == QI_REGS && GET_MODE_SIZE (GET_MODE (x)) > 2)
723 newclass = SI_REGS;
724 else if (GET_MODE_SIZE (GET_MODE (x)) > 4
725 && ~class_contents[rclass][0] & 0x000f)
726 newclass = DI_REGS;
728 rclass = reduce_class (rclass, newclass, rclass);
730 if (GET_MODE (x) == QImode)
731 rclass = reduce_class (rclass, HL_REGS, rclass);
733 #if DEBUG_RELOAD
734 fprintf (stderr, "%s\n", class_names[rclass]);
735 debug_rtx (x);
737 if (GET_CODE (x) == MEM
738 && GET_CODE (XEXP (x, 0)) == PLUS
739 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
740 fprintf (stderr, "Glorm!\n");
741 #endif
742 return rclass;
745 /* Implements PREFERRED_OUTPUT_RELOAD_CLASS. */
747 m32c_preferred_output_reload_class (rtx x, int rclass)
749 return m32c_preferred_reload_class (x, rclass);
752 /* Implements LIMIT_RELOAD_CLASS. We basically want to avoid using
753 address registers for reloads since they're needed for address
754 reloads. */
756 m32c_limit_reload_class (enum machine_mode mode, int rclass)
758 #if DEBUG_RELOAD
759 fprintf (stderr, "limit_reload_class for %s: %s ->",
760 mode_name[mode], class_names[rclass]);
761 #endif
763 if (mode == QImode)
764 rclass = reduce_class (rclass, HL_REGS, rclass);
765 else if (mode == HImode)
766 rclass = reduce_class (rclass, HI_REGS, rclass);
767 else if (mode == SImode)
768 rclass = reduce_class (rclass, SI_REGS, rclass);
770 if (rclass != A_REGS)
771 rclass = reduce_class (rclass, DI_REGS, rclass);
773 #if DEBUG_RELOAD
774 fprintf (stderr, " %s\n", class_names[rclass]);
775 #endif
776 return rclass;
779 /* Implements SECONDARY_RELOAD_CLASS. QImode have to be reloaded in
780 r0 or r1, as those are the only real QImode registers. CR regs get
781 reloaded through appropriately sized general or address
782 registers. */
784 m32c_secondary_reload_class (int rclass, enum machine_mode mode, rtx x)
786 int cc = class_contents[rclass][0];
787 #if DEBUG0
788 fprintf (stderr, "\nsecondary reload class %s %s\n",
789 class_names[rclass], mode_name[mode]);
790 debug_rtx (x);
791 #endif
792 if (mode == QImode
793 && GET_CODE (x) == MEM && (cc & ~class_contents[R23_REGS][0]) == 0)
794 return QI_REGS;
795 if (classes_intersect (rclass, CR_REGS)
796 && GET_CODE (x) == REG
797 && REGNO (x) >= SB_REGNO && REGNO (x) <= SP_REGNO)
798 return TARGET_A16 ? HI_REGS : A_REGS;
799 return NO_REGS;
802 /* Implements CLASS_LIKELY_SPILLED_P. A_REGS is needed for address
803 reloads. */
805 m32c_class_likely_spilled_p (int regclass)
807 if (regclass == A_REGS)
808 return 1;
809 return reg_class_size[regclass] == 1;
812 /* Implements CLASS_MAX_NREGS. We calculate this according to its
813 documented meaning, to avoid potential inconsistencies with actual
814 class definitions. */
816 m32c_class_max_nregs (int regclass, enum machine_mode mode)
818 int rn, max = 0;
820 for (rn = 0; rn < FIRST_PSEUDO_REGISTER; rn++)
821 if (class_contents[regclass][0] & (1 << rn))
823 int n = m32c_hard_regno_nregs (rn, mode);
824 if (max < n)
825 max = n;
827 return max;
830 /* Implements CANNOT_CHANGE_MODE_CLASS. Only r0 and r1 can change to
831 QI (r0l, r1l) because the chip doesn't support QI ops on other
832 registers (well, it does on a0/a1 but if we let gcc do that, reload
833 suffers). Otherwise, we allow changes to larger modes. */
835 m32c_cannot_change_mode_class (enum machine_mode from,
836 enum machine_mode to, int rclass)
838 #if DEBUG0
839 fprintf (stderr, "cannot change from %s to %s in %s\n",
840 mode_name[from], mode_name[to], class_names[rclass]);
841 #endif
843 if (to == QImode)
844 return (class_contents[rclass][0] & 0x1ffa);
846 if (class_contents[rclass][0] & 0x0005 /* r0, r1 */
847 && GET_MODE_SIZE (from) > 1)
848 return 0;
849 if (GET_MODE_SIZE (from) > 2) /* all other regs */
850 return 0;
852 return 1;
855 /* Helpers for the rest of the file. */
856 /* TRUE if the rtx is a REG rtx for the given register. */
857 #define IS_REG(rtx,regno) (GET_CODE (rtx) == REG \
858 && REGNO (rtx) == regno)
859 /* TRUE if the rtx is a pseudo - specifically, one we can use as a
860 base register in address calculations (hence the "strict"
861 argument). */
862 #define IS_PSEUDO(rtx,strict) (!strict && GET_CODE (rtx) == REG \
863 && (REGNO (rtx) == AP_REGNO \
864 || REGNO (rtx) >= FIRST_PSEUDO_REGISTER))
866 /* Implements CONST_OK_FOR_CONSTRAINT_P. Currently, all constant
867 constraints start with 'I', with the next two characters indicating
868 the type and size of the range allowed. */
870 m32c_const_ok_for_constraint_p (HOST_WIDE_INT value,
871 char c ATTRIBUTE_UNUSED, const char *str)
873 /* s=signed u=unsigned n=nonzero m=minus l=log2able,
874 [sun] bits [SUN] bytes, p=pointer size
875 I[-0-9][0-9] matches that number */
876 if (memcmp (str, "Is3", 3) == 0)
878 return (-8 <= value && value <= 7);
880 if (memcmp (str, "IS1", 3) == 0)
882 return (-128 <= value && value <= 127);
884 if (memcmp (str, "IS2", 3) == 0)
886 return (-32768 <= value && value <= 32767);
888 if (memcmp (str, "IU2", 3) == 0)
890 return (0 <= value && value <= 65535);
892 if (memcmp (str, "IU3", 3) == 0)
894 return (0 <= value && value <= 0x00ffffff);
896 if (memcmp (str, "In4", 3) == 0)
898 return (-8 <= value && value && value <= 8);
900 if (memcmp (str, "In5", 3) == 0)
902 return (-16 <= value && value && value <= 16);
904 if (memcmp (str, "In6", 3) == 0)
906 return (-32 <= value && value && value <= 32);
908 if (memcmp (str, "IM2", 3) == 0)
910 return (-65536 <= value && value && value <= -1);
912 if (memcmp (str, "Ilb", 3) == 0)
914 int b = exact_log2 (value);
915 return (b >= 1 && b <= 8);
917 if (memcmp (str, "Ilw", 3) == 0)
919 int b = exact_log2 (value);
920 return (b >= 1 && b <= 16);
922 return 0;
925 /* Implements EXTRA_CONSTRAINT_STR (see next function too). 'S' is
926 for memory constraints, plus "Rpa" for PARALLEL rtx's we use for
927 call return values. */
929 m32c_extra_constraint_p2 (rtx value, char c ATTRIBUTE_UNUSED, const char *str)
931 encode_pattern (value);
932 if (memcmp (str, "Sd", 2) == 0)
934 /* This is the common "src/dest" address */
935 rtx r;
936 if (GET_CODE (value) == MEM && CONSTANT_P (XEXP (value, 0)))
937 return 1;
938 if (RTX_IS ("ms") || RTX_IS ("m+si"))
939 return 1;
940 if (RTX_IS ("mr"))
941 r = patternr[1];
942 else if (RTX_IS ("m+ri") || RTX_IS ("m+rs") || RTX_IS ("m+r+si"))
943 r = patternr[2];
944 else
945 return 0;
946 if (REGNO (r) == SP_REGNO)
947 return 0;
948 return m32c_legitimate_address_p (GET_MODE (value), XEXP (value, 0), 1);
950 else if (memcmp (str, "Sa", 2) == 0)
952 rtx r;
953 if (RTX_IS ("mr"))
954 r = patternr[1];
955 else if (RTX_IS ("m+ri"))
956 r = patternr[2];
957 else
958 return 0;
959 return (IS_REG (r, A0_REGNO) || IS_REG (r, A1_REGNO));
961 else if (memcmp (str, "Si", 2) == 0)
963 return (RTX_IS ("mi") || RTX_IS ("ms") || RTX_IS ("m+si"));
965 else if (memcmp (str, "Ss", 2) == 0)
967 return ((RTX_IS ("mr")
968 && (IS_REG (patternr[1], SP_REGNO)))
969 || (RTX_IS ("m+ri") && (IS_REG (patternr[2], SP_REGNO))));
971 else if (memcmp (str, "Sf", 2) == 0)
973 return ((RTX_IS ("mr")
974 && (IS_REG (patternr[1], FB_REGNO)))
975 || (RTX_IS ("m+ri") && (IS_REG (patternr[2], FB_REGNO))));
977 else if (memcmp (str, "Sb", 2) == 0)
979 return ((RTX_IS ("mr")
980 && (IS_REG (patternr[1], SB_REGNO)))
981 || (RTX_IS ("m+ri") && (IS_REG (patternr[2], SB_REGNO))));
983 else if (memcmp (str, "S1", 2) == 0)
985 return r1h_operand (value, QImode);
988 gcc_assert (str[0] != 'S');
990 if (memcmp (str, "Rpa", 2) == 0)
991 return GET_CODE (value) == PARALLEL;
993 return 0;
996 /* This is for when we're debugging the above. */
998 m32c_extra_constraint_p (rtx value, char c, const char *str)
1000 int rv = m32c_extra_constraint_p2 (value, c, str);
1001 #if DEBUG0
1002 fprintf (stderr, "\nconstraint %.*s: %d\n", CONSTRAINT_LEN (c, str), str,
1003 rv);
1004 debug_rtx (value);
1005 #endif
1006 return rv;
1009 /* Implements EXTRA_MEMORY_CONSTRAINT. Currently, we only use strings
1010 starting with 'S'. */
1012 m32c_extra_memory_constraint (char c, const char *str ATTRIBUTE_UNUSED)
1014 return c == 'S';
1017 /* Implements EXTRA_ADDRESS_CONSTRAINT. We reserve 'A' strings for these,
1018 but don't currently define any. */
1020 m32c_extra_address_constraint (char c, const char *str ATTRIBUTE_UNUSED)
1022 return c == 'A';
1025 /* STACK AND CALLING */
1027 /* Frame Layout */
1029 /* Implements RETURN_ADDR_RTX. Note that R8C and M16C push 24 bits
1030 (yes, THREE bytes) onto the stack for the return address, but we
1031 don't support pointers bigger than 16 bits on those chips. This
1032 will likely wreak havoc with exception unwinding. FIXME. */
1034 m32c_return_addr_rtx (int count)
1036 enum machine_mode mode;
1037 int offset;
1038 rtx ra_mem;
1040 if (count)
1041 return NULL_RTX;
1042 /* we want 2[$fb] */
1044 if (TARGET_A24)
1046 mode = SImode;
1047 offset = 4;
1049 else
1051 /* FIXME: it's really 3 bytes */
1052 mode = HImode;
1053 offset = 2;
1056 ra_mem =
1057 gen_rtx_MEM (mode, plus_constant (gen_rtx_REG (Pmode, FP_REGNO), offset));
1058 return copy_to_mode_reg (mode, ra_mem);
1061 /* Implements INCOMING_RETURN_ADDR_RTX. See comment above. */
1063 m32c_incoming_return_addr_rtx (void)
1065 /* we want [sp] */
1066 return gen_rtx_MEM (PSImode, gen_rtx_REG (PSImode, SP_REGNO));
1069 /* Exception Handling Support */
1071 /* Implements EH_RETURN_DATA_REGNO. Choose registers able to hold
1072 pointers. */
1074 m32c_eh_return_data_regno (int n)
1076 switch (n)
1078 case 0:
1079 return A0_REGNO;
1080 case 1:
1081 return A1_REGNO;
1082 default:
1083 return INVALID_REGNUM;
1087 /* Implements EH_RETURN_STACKADJ_RTX. Saved and used later in
1088 m32c_emit_eh_epilogue. */
1090 m32c_eh_return_stackadj_rtx (void)
1092 if (!cfun->machine->eh_stack_adjust)
1094 rtx sa;
1096 sa = gen_reg_rtx (Pmode);
1097 cfun->machine->eh_stack_adjust = sa;
1099 return cfun->machine->eh_stack_adjust;
1102 /* Registers That Address the Stack Frame */
1104 /* Implements DWARF_FRAME_REGNUM and DBX_REGISTER_NUMBER. Note that
1105 the original spec called for dwarf numbers to vary with register
1106 width as well, for example, r0l, r0, and r2r0 would each have
1107 different dwarf numbers. GCC doesn't support this, and we don't do
1108 it, and gdb seems to like it this way anyway. */
1109 unsigned int
1110 m32c_dwarf_frame_regnum (int n)
1112 switch (n)
1114 case R0_REGNO:
1115 return 5;
1116 case R1_REGNO:
1117 return 6;
1118 case R2_REGNO:
1119 return 7;
1120 case R3_REGNO:
1121 return 8;
1122 case A0_REGNO:
1123 return 9;
1124 case A1_REGNO:
1125 return 10;
1126 case FB_REGNO:
1127 return 11;
1128 case SB_REGNO:
1129 return 19;
1131 case SP_REGNO:
1132 return 12;
1133 case PC_REGNO:
1134 return 13;
1135 default:
1136 return DWARF_FRAME_REGISTERS + 1;
1140 /* The frame looks like this:
1142 ap -> +------------------------------
1143 | Return address (3 or 4 bytes)
1144 | Saved FB (2 or 4 bytes)
1145 fb -> +------------------------------
1146 | local vars
1147 | register saves fb
1148 | through r0 as needed
1149 sp -> +------------------------------
1152 /* We use this to wrap all emitted insns in the prologue. */
1153 static rtx
1154 F (rtx x)
1156 RTX_FRAME_RELATED_P (x) = 1;
1157 return x;
1160 /* This maps register numbers to the PUSHM/POPM bitfield, and tells us
1161 how much the stack pointer moves for each, for each cpu family. */
1162 static struct
1164 int reg1;
1165 int bit;
1166 int a16_bytes;
1167 int a24_bytes;
1168 } pushm_info[] =
1170 /* These are in push order. */
1171 { FB_REGNO, 0x01, 2, 4 },
1172 { SB_REGNO, 0x02, 2, 4 },
1173 { A1_REGNO, 0x04, 2, 4 },
1174 { A0_REGNO, 0x08, 2, 4 },
1175 { R3_REGNO, 0x10, 2, 2 },
1176 { R2_REGNO, 0x20, 2, 2 },
1177 { R1_REGNO, 0x40, 2, 2 },
1178 { R0_REGNO, 0x80, 2, 2 }
1181 #define PUSHM_N (sizeof(pushm_info)/sizeof(pushm_info[0]))
1183 /* Returns TRUE if we need to save/restore the given register. We
1184 save everything for exception handlers, so that any register can be
1185 unwound. For interrupt handlers, we save everything if the handler
1186 calls something else (because we don't know what *that* function
1187 might do), but try to be a bit smarter if the handler is a leaf
1188 function. We always save $a0, though, because we use that in the
1189 epilog to copy $fb to $sp. */
1190 static int
1191 need_to_save (int regno)
1193 if (fixed_regs[regno])
1194 return 0;
1195 if (cfun->calls_eh_return)
1196 return 1;
1197 if (regno == FP_REGNO)
1198 return 0;
1199 if (cfun->machine->is_interrupt
1200 && (!cfun->machine->is_leaf || regno == A0_REGNO))
1201 return 1;
1202 if (regs_ever_live[regno]
1203 && (!call_used_regs[regno] || cfun->machine->is_interrupt))
1204 return 1;
1205 return 0;
1208 /* This function contains all the intelligence about saving and
1209 restoring registers. It always figures out the register save set.
1210 When called with PP_justcount, it merely returns the size of the
1211 save set (for eliminating the frame pointer, for example). When
1212 called with PP_pushm or PP_popm, it emits the appropriate
1213 instructions for saving (pushm) or restoring (popm) the
1214 registers. */
1215 static int
1216 m32c_pushm_popm (Push_Pop_Type ppt)
1218 int reg_mask = 0;
1219 int byte_count = 0, bytes;
1220 int i;
1221 rtx dwarf_set[PUSHM_N];
1222 int n_dwarfs = 0;
1223 int nosave_mask = 0;
1225 if (cfun->return_rtx
1226 && GET_CODE (cfun->return_rtx) == PARALLEL
1227 && !(cfun->calls_eh_return || cfun->machine->is_interrupt))
1229 rtx exp = XVECEXP (cfun->return_rtx, 0, 0);
1230 rtx rv = XEXP (exp, 0);
1231 int rv_bytes = GET_MODE_SIZE (GET_MODE (rv));
1233 if (rv_bytes > 2)
1234 nosave_mask |= 0x20; /* PSI, SI */
1235 else
1236 nosave_mask |= 0xf0; /* DF */
1237 if (rv_bytes > 4)
1238 nosave_mask |= 0x50; /* DI */
1241 for (i = 0; i < (int) PUSHM_N; i++)
1243 /* Skip if neither register needs saving. */
1244 if (!need_to_save (pushm_info[i].reg1))
1245 continue;
1247 if (pushm_info[i].bit & nosave_mask)
1248 continue;
1250 reg_mask |= pushm_info[i].bit;
1251 bytes = TARGET_A16 ? pushm_info[i].a16_bytes : pushm_info[i].a24_bytes;
1253 if (ppt == PP_pushm)
1255 enum machine_mode mode = (bytes == 2) ? HImode : SImode;
1256 rtx addr;
1258 /* Always use stack_pointer_rtx instead of calling
1259 rtx_gen_REG ourselves. Code elsewhere in GCC assumes
1260 that there is a single rtx representing the stack pointer,
1261 namely stack_pointer_rtx, and uses == to recognize it. */
1262 addr = stack_pointer_rtx;
1264 if (byte_count != 0)
1265 addr = gen_rtx_PLUS (GET_MODE (addr), addr, GEN_INT (byte_count));
1267 dwarf_set[n_dwarfs++] =
1268 gen_rtx_SET (VOIDmode,
1269 gen_rtx_MEM (mode, addr),
1270 gen_rtx_REG (mode, pushm_info[i].reg1));
1271 F (dwarf_set[n_dwarfs - 1]);
1274 byte_count += bytes;
1277 if (cfun->machine->is_interrupt)
1279 cfun->machine->intr_pushm = reg_mask & 0xfe;
1280 reg_mask = 0;
1281 byte_count = 0;
1284 if (cfun->machine->is_interrupt)
1285 for (i = MEM0_REGNO; i <= MEM7_REGNO; i++)
1286 if (need_to_save (i))
1288 byte_count += 2;
1289 cfun->machine->intr_pushmem[i - MEM0_REGNO] = 1;
1292 if (ppt == PP_pushm && byte_count)
1294 rtx note = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (n_dwarfs + 1));
1295 rtx pushm;
1297 if (reg_mask)
1299 XVECEXP (note, 0, 0)
1300 = gen_rtx_SET (VOIDmode,
1301 stack_pointer_rtx,
1302 gen_rtx_PLUS (GET_MODE (stack_pointer_rtx),
1303 stack_pointer_rtx,
1304 GEN_INT (-byte_count)));
1305 F (XVECEXP (note, 0, 0));
1307 for (i = 0; i < n_dwarfs; i++)
1308 XVECEXP (note, 0, i + 1) = dwarf_set[i];
1310 pushm = F (emit_insn (gen_pushm (GEN_INT (reg_mask))));
1312 REG_NOTES (pushm) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, note,
1313 REG_NOTES (pushm));
1316 if (cfun->machine->is_interrupt)
1317 for (i = MEM0_REGNO; i <= MEM7_REGNO; i++)
1318 if (cfun->machine->intr_pushmem[i - MEM0_REGNO])
1320 if (TARGET_A16)
1321 pushm = emit_insn (gen_pushhi_16 (gen_rtx_REG (HImode, i)));
1322 else
1323 pushm = emit_insn (gen_pushhi_24 (gen_rtx_REG (HImode, i)));
1324 F (pushm);
1327 if (ppt == PP_popm && byte_count)
1329 if (cfun->machine->is_interrupt)
1330 for (i = MEM7_REGNO; i >= MEM0_REGNO; i--)
1331 if (cfun->machine->intr_pushmem[i - MEM0_REGNO])
1333 if (TARGET_A16)
1334 emit_insn (gen_pophi_16 (gen_rtx_REG (HImode, i)));
1335 else
1336 emit_insn (gen_pophi_24 (gen_rtx_REG (HImode, i)));
1338 if (reg_mask)
1339 emit_insn (gen_popm (GEN_INT (reg_mask)));
1342 return byte_count;
1345 /* Implements INITIAL_ELIMINATION_OFFSET. See the comment above that
1346 diagrams our call frame. */
1348 m32c_initial_elimination_offset (int from, int to)
1350 int ofs = 0;
1352 if (from == AP_REGNO)
1354 if (TARGET_A16)
1355 ofs += 5;
1356 else
1357 ofs += 8;
1360 if (to == SP_REGNO)
1362 ofs += m32c_pushm_popm (PP_justcount);
1363 ofs += get_frame_size ();
1366 /* Account for push rounding. */
1367 if (TARGET_A24)
1368 ofs = (ofs + 1) & ~1;
1369 #if DEBUG0
1370 fprintf (stderr, "initial_elimination_offset from=%d to=%d, ofs=%d\n", from,
1371 to, ofs);
1372 #endif
1373 return ofs;
1376 /* Passing Function Arguments on the Stack */
1378 #undef TARGET_PROMOTE_PROTOTYPES
1379 #define TARGET_PROMOTE_PROTOTYPES m32c_promote_prototypes
1380 static bool
1381 m32c_promote_prototypes (tree fntype ATTRIBUTE_UNUSED)
1383 return 0;
1386 /* Implements PUSH_ROUNDING. The R8C and M16C have byte stacks, the
1387 M32C has word stacks. */
1389 m32c_push_rounding (int n)
1391 if (TARGET_R8C || TARGET_M16C)
1392 return n;
1393 return (n + 1) & ~1;
1396 /* Passing Arguments in Registers */
1398 /* Implements FUNCTION_ARG. Arguments are passed partly in registers,
1399 partly on stack. If our function returns a struct, a pointer to a
1400 buffer for it is at the top of the stack (last thing pushed). The
1401 first few real arguments may be in registers as follows:
1403 R8C/M16C: arg1 in r1 if it's QI or HI (else it's pushed on stack)
1404 arg2 in r2 if it's HI (else pushed on stack)
1405 rest on stack
1406 M32C: arg1 in r0 if it's QI or HI (else it's pushed on stack)
1407 rest on stack
1409 Structs are not passed in registers, even if they fit. Only
1410 integer and pointer types are passed in registers.
1412 Note that when arg1 doesn't fit in r1, arg2 may still be passed in
1413 r2 if it fits. */
1415 m32c_function_arg (CUMULATIVE_ARGS * ca,
1416 enum machine_mode mode, tree type, int named)
1418 /* Can return a reg, parallel, or 0 for stack */
1419 rtx rv = NULL_RTX;
1420 #if DEBUG0
1421 fprintf (stderr, "func_arg %d (%s, %d)\n",
1422 ca->parm_num, mode_name[mode], named);
1423 debug_tree (type);
1424 #endif
1426 if (mode == VOIDmode)
1427 return GEN_INT (0);
1429 if (ca->force_mem || !named)
1431 #if DEBUG0
1432 fprintf (stderr, "func arg: force %d named %d, mem\n", ca->force_mem,
1433 named);
1434 #endif
1435 return NULL_RTX;
1438 if (type && INTEGRAL_TYPE_P (type) && POINTER_TYPE_P (type))
1439 return NULL_RTX;
1441 switch (ca->parm_num)
1443 case 1:
1444 if (GET_MODE_SIZE (mode) == 1 || GET_MODE_SIZE (mode) == 2)
1445 rv = gen_rtx_REG (mode, TARGET_A16 ? R1_REGNO : R0_REGNO);
1446 break;
1448 case 2:
1449 if (TARGET_A16 && GET_MODE_SIZE (mode) == 2)
1450 rv = gen_rtx_REG (mode, R2_REGNO);
1451 break;
1454 #if DEBUG0
1455 debug_rtx (rv);
1456 #endif
1457 return rv;
1460 #undef TARGET_PASS_BY_REFERENCE
1461 #define TARGET_PASS_BY_REFERENCE m32c_pass_by_reference
1462 static bool
1463 m32c_pass_by_reference (CUMULATIVE_ARGS * ca ATTRIBUTE_UNUSED,
1464 enum machine_mode mode ATTRIBUTE_UNUSED,
1465 tree type ATTRIBUTE_UNUSED,
1466 bool named ATTRIBUTE_UNUSED)
1468 return 0;
1471 /* Implements INIT_CUMULATIVE_ARGS. */
1472 void
1473 m32c_init_cumulative_args (CUMULATIVE_ARGS * ca,
1474 tree fntype ATTRIBUTE_UNUSED,
1475 rtx libname ATTRIBUTE_UNUSED,
1476 tree fndecl ATTRIBUTE_UNUSED,
1477 int n_named_args ATTRIBUTE_UNUSED)
1479 ca->force_mem = 0;
1480 ca->parm_num = 1;
1483 /* Implements FUNCTION_ARG_ADVANCE. force_mem is set for functions
1484 returning structures, so we always reset that. Otherwise, we only
1485 need to know the sequence number of the argument to know what to do
1486 with it. */
1487 void
1488 m32c_function_arg_advance (CUMULATIVE_ARGS * ca,
1489 enum machine_mode mode ATTRIBUTE_UNUSED,
1490 tree type ATTRIBUTE_UNUSED,
1491 int named ATTRIBUTE_UNUSED)
1493 if (ca->force_mem)
1494 ca->force_mem = 0;
1495 ca->parm_num++;
1498 /* Implements FUNCTION_ARG_REGNO_P. */
1500 m32c_function_arg_regno_p (int r)
1502 if (TARGET_A24)
1503 return (r == R0_REGNO);
1504 return (r == R1_REGNO || r == R2_REGNO);
1507 /* HImode and PSImode are the two "native" modes as far as GCC is
1508 concerned, but the chips also support a 32 bit mode which is used
1509 for some opcodes in R8C/M16C and for reset vectors and such. */
1510 #undef TARGET_VALID_POINTER_MODE
1511 #define TARGET_VALID_POINTER_MODE m32c_valid_pointer_mode
1512 static bool
1513 m32c_valid_pointer_mode (enum machine_mode mode)
1515 if (mode == HImode
1516 || mode == PSImode
1517 || mode == SImode
1519 return 1;
1520 return 0;
1523 /* How Scalar Function Values Are Returned */
1525 /* Implements LIBCALL_VALUE. Most values are returned in $r0, or some
1526 combination of registers starting there (r2r0 for longs, r3r1r2r0
1527 for long long, r3r2r1r0 for doubles), except that that ABI
1528 currently doesn't work because it ends up using all available
1529 general registers and gcc often can't compile it. So, instead, we
1530 return anything bigger than 16 bits in "mem0" (effectively, a
1531 memory location). */
1533 m32c_libcall_value (enum machine_mode mode)
1535 /* return reg or parallel */
1536 #if 0
1537 /* FIXME: GCC has difficulty returning large values in registers,
1538 because that ties up most of the general registers and gives the
1539 register allocator little to work with. Until we can resolve
1540 this, large values are returned in memory. */
1541 if (mode == DFmode)
1543 rtx rv;
1545 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (4));
1546 XVECEXP (rv, 0, 0) = gen_rtx_EXPR_LIST (VOIDmode,
1547 gen_rtx_REG (HImode,
1548 R0_REGNO),
1549 GEN_INT (0));
1550 XVECEXP (rv, 0, 1) = gen_rtx_EXPR_LIST (VOIDmode,
1551 gen_rtx_REG (HImode,
1552 R1_REGNO),
1553 GEN_INT (2));
1554 XVECEXP (rv, 0, 2) = gen_rtx_EXPR_LIST (VOIDmode,
1555 gen_rtx_REG (HImode,
1556 R2_REGNO),
1557 GEN_INT (4));
1558 XVECEXP (rv, 0, 3) = gen_rtx_EXPR_LIST (VOIDmode,
1559 gen_rtx_REG (HImode,
1560 R3_REGNO),
1561 GEN_INT (6));
1562 return rv;
1565 if (TARGET_A24 && GET_MODE_SIZE (mode) > 2)
1567 rtx rv;
1569 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (1));
1570 XVECEXP (rv, 0, 0) = gen_rtx_EXPR_LIST (VOIDmode,
1571 gen_rtx_REG (mode,
1572 R0_REGNO),
1573 GEN_INT (0));
1574 return rv;
1576 #endif
1578 if (GET_MODE_SIZE (mode) > 2)
1579 return gen_rtx_REG (mode, MEM0_REGNO);
1580 return gen_rtx_REG (mode, R0_REGNO);
1583 /* Implements FUNCTION_VALUE. Functions and libcalls have the same
1584 conventions. */
1586 m32c_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
1588 /* return reg or parallel */
1589 enum machine_mode mode = TYPE_MODE (valtype);
1590 return m32c_libcall_value (mode);
1593 /* How Large Values Are Returned */
1595 /* We return structures by pushing the address on the stack, even if
1596 we use registers for the first few "real" arguments. */
1597 #undef TARGET_STRUCT_VALUE_RTX
1598 #define TARGET_STRUCT_VALUE_RTX m32c_struct_value_rtx
1599 static rtx
1600 m32c_struct_value_rtx (tree fndecl ATTRIBUTE_UNUSED,
1601 int incoming ATTRIBUTE_UNUSED)
1603 return 0;
1606 /* Function Entry and Exit */
1608 /* Implements EPILOGUE_USES. Interrupts restore all registers. */
1610 m32c_epilogue_uses (int regno ATTRIBUTE_UNUSED)
1612 if (cfun->machine->is_interrupt)
1613 return 1;
1614 return 0;
1617 /* Implementing the Varargs Macros */
1619 #undef TARGET_STRICT_ARGUMENT_NAMING
1620 #define TARGET_STRICT_ARGUMENT_NAMING m32c_strict_argument_naming
1621 static bool
1622 m32c_strict_argument_naming (CUMULATIVE_ARGS * ca ATTRIBUTE_UNUSED)
1624 return 1;
1627 /* Trampolines for Nested Functions */
1630 m16c:
1631 1 0000 75C43412 mov.w #0x1234,a0
1632 2 0004 FC000000 jmp.a label
1634 m32c:
1635 1 0000 BC563412 mov.l:s #0x123456,a0
1636 2 0004 CC000000 jmp.a label
1639 /* Implements TRAMPOLINE_SIZE. */
1641 m32c_trampoline_size (void)
1643 /* Allocate extra space so we can avoid the messy shifts when we
1644 initialize the trampoline; we just write past the end of the
1645 opcode. */
1646 return TARGET_A16 ? 8 : 10;
1649 /* Implements TRAMPOLINE_ALIGNMENT. */
1651 m32c_trampoline_alignment (void)
1653 return 2;
1656 /* Implements INITIALIZE_TRAMPOLINE. */
1657 void
1658 m32c_initialize_trampoline (rtx tramp, rtx function, rtx chainval)
1660 #define A0(m,i) gen_rtx_MEM (m, plus_constant (tramp, i))
1661 if (TARGET_A16)
1663 /* Note: we subtract a "word" because the moves want signed
1664 constants, not unsigned constants. */
1665 emit_move_insn (A0 (HImode, 0), GEN_INT (0xc475 - 0x10000));
1666 emit_move_insn (A0 (HImode, 2), chainval);
1667 emit_move_insn (A0 (QImode, 4), GEN_INT (0xfc - 0x100));
1668 /* We use 16 bit addresses here, but store the zero to turn it
1669 into a 24 bit offset. */
1670 emit_move_insn (A0 (HImode, 5), function);
1671 emit_move_insn (A0 (QImode, 7), GEN_INT (0x00));
1673 else
1675 /* Note that the PSI moves actually write 4 bytes. Make sure we
1676 write stuff out in the right order, and leave room for the
1677 extra byte at the end. */
1678 emit_move_insn (A0 (QImode, 0), GEN_INT (0xbc - 0x100));
1679 emit_move_insn (A0 (PSImode, 1), chainval);
1680 emit_move_insn (A0 (QImode, 4), GEN_INT (0xcc - 0x100));
1681 emit_move_insn (A0 (PSImode, 5), function);
1683 #undef A0
1686 /* Addressing Modes */
1688 /* Used by GO_IF_LEGITIMATE_ADDRESS. The r8c/m32c family supports a
1689 wide range of non-orthogonal addressing modes, including the
1690 ability to double-indirect on *some* of them. Not all insns
1691 support all modes, either, but we rely on predicates and
1692 constraints to deal with that. */
1694 m32c_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1696 int mode_adjust;
1697 if (CONSTANT_P (x))
1698 return 1;
1700 /* Wide references to memory will be split after reload, so we must
1701 ensure that all parts of such splits remain legitimate
1702 addresses. */
1703 mode_adjust = GET_MODE_SIZE (mode) - 1;
1705 /* allowing PLUS yields mem:HI(plus:SI(mem:SI(plus:SI in m32c_split_move */
1706 if (GET_CODE (x) == PRE_DEC
1707 || GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_MODIFY)
1709 return (GET_CODE (XEXP (x, 0)) == REG
1710 && REGNO (XEXP (x, 0)) == SP_REGNO);
1713 #if 0
1714 /* This is the double indirection detection, but it currently
1715 doesn't work as cleanly as this code implies, so until we've had
1716 a chance to debug it, leave it disabled. */
1717 if (TARGET_A24 && GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) != PLUS)
1719 #if DEBUG_DOUBLE
1720 fprintf (stderr, "double indirect\n");
1721 #endif
1722 x = XEXP (x, 0);
1724 #endif
1726 encode_pattern (x);
1727 if (RTX_IS ("r"))
1729 /* Most indexable registers can be used without displacements,
1730 although some of them will be emitted with an explicit zero
1731 to please the assembler. */
1732 switch (REGNO (patternr[0]))
1734 case A0_REGNO:
1735 case A1_REGNO:
1736 case SB_REGNO:
1737 case FB_REGNO:
1738 case SP_REGNO:
1739 return 1;
1741 default:
1742 if (IS_PSEUDO (patternr[0], strict))
1743 return 1;
1744 return 0;
1747 if (RTX_IS ("+ri"))
1749 /* This is more interesting, because different base registers
1750 allow for different displacements - both range and signedness
1751 - and it differs from chip series to chip series too. */
1752 int rn = REGNO (patternr[1]);
1753 HOST_WIDE_INT offs = INTVAL (patternr[2]);
1754 switch (rn)
1756 case A0_REGNO:
1757 case A1_REGNO:
1758 case SB_REGNO:
1759 /* The syntax only allows positive offsets, but when the
1760 offsets span the entire memory range, we can simulate
1761 negative offsets by wrapping. */
1762 if (TARGET_A16)
1763 return (offs >= -65536 && offs <= 65535 - mode_adjust);
1764 if (rn == SB_REGNO)
1765 return (offs >= 0 && offs <= 65535 - mode_adjust);
1766 /* A0 or A1 */
1767 return (offs >= -16777216 && offs <= 16777215);
1769 case FB_REGNO:
1770 if (TARGET_A16)
1771 return (offs >= -128 && offs <= 127 - mode_adjust);
1772 return (offs >= -65536 && offs <= 65535 - mode_adjust);
1774 case SP_REGNO:
1775 return (offs >= -128 && offs <= 127 - mode_adjust);
1777 default:
1778 if (IS_PSEUDO (patternr[1], strict))
1779 return 1;
1780 return 0;
1783 if (RTX_IS ("+rs") || RTX_IS ("+r+si"))
1785 rtx reg = patternr[1];
1787 /* We don't know where the symbol is, so only allow base
1788 registers which support displacements spanning the whole
1789 address range. */
1790 switch (REGNO (reg))
1792 case A0_REGNO:
1793 case A1_REGNO:
1794 /* $sb needs a secondary reload, but since it's involved in
1795 memory address reloads too, we don't deal with it very
1796 well. */
1797 /* case SB_REGNO: */
1798 return 1;
1799 default:
1800 if (IS_PSEUDO (reg, strict))
1801 return 1;
1802 return 0;
1805 return 0;
1808 /* Implements REG_OK_FOR_BASE_P. */
1810 m32c_reg_ok_for_base_p (rtx x, int strict)
1812 if (GET_CODE (x) != REG)
1813 return 0;
1814 switch (REGNO (x))
1816 case A0_REGNO:
1817 case A1_REGNO:
1818 case SB_REGNO:
1819 case FB_REGNO:
1820 case SP_REGNO:
1821 return 1;
1822 default:
1823 if (IS_PSEUDO (x, strict))
1824 return 1;
1825 return 0;
1829 /* We have three choices for choosing fb->aN offsets. If we choose -128,
1830 we need one MOVA -128[fb],aN opcode and 16 bit aN displacements,
1831 like this:
1832 EB 4B FF mova -128[$fb],$a0
1833 D8 0C FF FF mov.w:Q #0,-1[$a0]
1835 Alternately, we subtract the frame size, and hopefully use 8 bit aN
1836 displacements:
1837 7B F4 stc $fb,$a0
1838 77 54 00 01 sub #256,$a0
1839 D8 08 01 mov.w:Q #0,1[$a0]
1841 If we don't offset (i.e. offset by zero), we end up with:
1842 7B F4 stc $fb,$a0
1843 D8 0C 00 FF mov.w:Q #0,-256[$a0]
1845 We have to subtract *something* so that we have a PLUS rtx to mark
1846 that we've done this reload. The -128 offset will never result in
1847 an 8 bit aN offset, and the payoff for the second case is five
1848 loads *if* those loads are within 256 bytes of the other end of the
1849 frame, so the third case seems best. Note that we subtract the
1850 zero, but detect that in the addhi3 pattern. */
1852 #define BIG_FB_ADJ 0
1854 /* Implements LEGITIMIZE_ADDRESS. The only address we really have to
1855 worry about is frame base offsets, as $fb has a limited
1856 displacement range. We deal with this by attempting to reload $fb
1857 itself into an address register; that seems to result in the best
1858 code. */
1860 m32c_legitimize_address (rtx * x ATTRIBUTE_UNUSED,
1861 rtx oldx ATTRIBUTE_UNUSED,
1862 enum machine_mode mode ATTRIBUTE_UNUSED)
1864 #if DEBUG0
1865 fprintf (stderr, "m32c_legitimize_address for mode %s\n", mode_name[mode]);
1866 debug_rtx (*x);
1867 fprintf (stderr, "\n");
1868 #endif
1870 if (GET_CODE (*x) == PLUS
1871 && GET_CODE (XEXP (*x, 0)) == REG
1872 && REGNO (XEXP (*x, 0)) == FB_REGNO
1873 && GET_CODE (XEXP (*x, 1)) == CONST_INT
1874 && (INTVAL (XEXP (*x, 1)) < -128
1875 || INTVAL (XEXP (*x, 1)) > (128 - GET_MODE_SIZE (mode))))
1877 /* reload FB to A_REGS */
1878 rtx temp = gen_reg_rtx (Pmode);
1879 *x = copy_rtx (*x);
1880 emit_insn (gen_rtx_SET (VOIDmode, temp, XEXP (*x, 0)));
1881 XEXP (*x, 0) = temp;
1882 return 1;
1885 return 0;
1888 /* Implements LEGITIMIZE_RELOAD_ADDRESS. See comment above. */
1890 m32c_legitimize_reload_address (rtx * x,
1891 enum machine_mode mode,
1892 int opnum,
1893 int type, int ind_levels ATTRIBUTE_UNUSED)
1895 #if DEBUG0
1896 fprintf (stderr, "\nm32c_legitimize_reload_address for mode %s\n",
1897 mode_name[mode]);
1898 debug_rtx (*x);
1899 #endif
1901 /* At one point, this function tried to get $fb copied to an address
1902 register, which in theory would maximize sharing, but gcc was
1903 *also* still trying to reload the whole address, and we'd run out
1904 of address registers. So we let gcc do the naive (but safe)
1905 reload instead, when the above function doesn't handle it for
1908 The code below is a second attempt at the above. */
1910 if (GET_CODE (*x) == PLUS
1911 && GET_CODE (XEXP (*x, 0)) == REG
1912 && REGNO (XEXP (*x, 0)) == FB_REGNO
1913 && GET_CODE (XEXP (*x, 1)) == CONST_INT
1914 && (INTVAL (XEXP (*x, 1)) < -128
1915 || INTVAL (XEXP (*x, 1)) > (128 - GET_MODE_SIZE (mode))))
1917 rtx sum;
1918 int offset = INTVAL (XEXP (*x, 1));
1919 int adjustment = -BIG_FB_ADJ;
1921 sum = gen_rtx_PLUS (Pmode, XEXP (*x, 0),
1922 GEN_INT (adjustment));
1923 *x = gen_rtx_PLUS (Pmode, sum, GEN_INT (offset - adjustment));
1924 if (type == RELOAD_OTHER)
1925 type = RELOAD_FOR_OTHER_ADDRESS;
1926 push_reload (sum, NULL_RTX, &XEXP (*x, 0), NULL,
1927 A_REGS, Pmode, VOIDmode, 0, 0, opnum,
1928 type);
1929 return 1;
1932 if (GET_CODE (*x) == PLUS
1933 && GET_CODE (XEXP (*x, 0)) == PLUS
1934 && GET_CODE (XEXP (XEXP (*x, 0), 0)) == REG
1935 && REGNO (XEXP (XEXP (*x, 0), 0)) == FB_REGNO
1936 && GET_CODE (XEXP (XEXP (*x, 0), 1)) == CONST_INT
1937 && GET_CODE (XEXP (*x, 1)) == CONST_INT
1940 if (type == RELOAD_OTHER)
1941 type = RELOAD_FOR_OTHER_ADDRESS;
1942 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1943 A_REGS, Pmode, VOIDmode, 0, 0, opnum,
1944 type);
1945 return 1;
1948 return 0;
1951 /* Used in GO_IF_MODE_DEPENDENT_ADDRESS. */
1953 m32c_mode_dependent_address (rtx addr)
1955 if (GET_CODE (addr) == POST_INC || GET_CODE (addr) == PRE_DEC)
1956 return 1;
1957 return 0;
1960 /* Implements LEGITIMATE_CONSTANT_P. We split large constants anyway,
1961 so we can allow anything. */
1963 m32c_legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
1965 return 1;
1969 /* Condition Code Status */
1971 #undef TARGET_FIXED_CONDITION_CODE_REGS
1972 #define TARGET_FIXED_CONDITION_CODE_REGS m32c_fixed_condition_code_regs
1973 static bool
1974 m32c_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
1976 *p1 = FLG_REGNO;
1977 *p2 = INVALID_REGNUM;
1978 return true;
1981 /* Describing Relative Costs of Operations */
1983 /* Implements REGISTER_MOVE_COST. We make impossible moves
1984 prohibitively expensive, like trying to put QIs in r2/r3 (there are
1985 no opcodes to do that). We also discourage use of mem* registers
1986 since they're really memory. */
1988 m32c_register_move_cost (enum machine_mode mode, int from, int to)
1990 int cost = COSTS_N_INSNS (3);
1991 int cc = class_contents[from][0] | class_contents[to][0];
1992 /* FIXME: pick real values, but not 2 for now. */
1993 if (mode == QImode && (cc & class_contents[R23_REGS][0]))
1995 if (!(cc & ~class_contents[R23_REGS][0]))
1996 cost = COSTS_N_INSNS (1000);
1997 else
1998 cost = COSTS_N_INSNS (80);
2001 if (!class_can_hold_mode (from, mode) || !class_can_hold_mode (to, mode))
2002 cost = COSTS_N_INSNS (1000);
2004 if (classes_intersect (from, CR_REGS))
2005 cost += COSTS_N_INSNS (5);
2007 if (classes_intersect (to, CR_REGS))
2008 cost += COSTS_N_INSNS (5);
2010 if (from == MEM_REGS || to == MEM_REGS)
2011 cost += COSTS_N_INSNS (50);
2012 else if (classes_intersect (from, MEM_REGS)
2013 || classes_intersect (to, MEM_REGS))
2014 cost += COSTS_N_INSNS (10);
2016 #if DEBUG0
2017 fprintf (stderr, "register_move_cost %s from %s to %s = %d\n",
2018 mode_name[mode], class_names[from], class_names[to], cost);
2019 #endif
2020 return cost;
2023 /* Implements MEMORY_MOVE_COST. */
2025 m32c_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
2026 int reg_class ATTRIBUTE_UNUSED,
2027 int in ATTRIBUTE_UNUSED)
2029 /* FIXME: pick real values. */
2030 return COSTS_N_INSNS (10);
2033 /* Defining the Output Assembler Language */
2035 /* The Overall Framework of an Assembler File */
2037 #undef TARGET_HAVE_NAMED_SECTIONS
2038 #define TARGET_HAVE_NAMED_SECTIONS true
2040 /* Output of Data */
2042 /* We may have 24 bit sizes, which is the native address size.
2043 Currently unused, but provided for completeness. */
2044 #undef TARGET_ASM_INTEGER
2045 #define TARGET_ASM_INTEGER m32c_asm_integer
2046 static bool
2047 m32c_asm_integer (rtx x, unsigned int size, int aligned_p)
2049 switch (size)
2051 case 3:
2052 fprintf (asm_out_file, "\t.3byte\t");
2053 output_addr_const (asm_out_file, x);
2054 fputc ('\n', asm_out_file);
2055 return true;
2056 case 4:
2057 if (GET_CODE (x) == SYMBOL_REF)
2059 fprintf (asm_out_file, "\t.long\t");
2060 output_addr_const (asm_out_file, x);
2061 fputc ('\n', asm_out_file);
2062 return true;
2064 break;
2066 return default_assemble_integer (x, size, aligned_p);
2069 /* Output of Assembler Instructions */
2071 /* We use a lookup table because the addressing modes are non-orthogonal. */
2073 static struct
2075 char code;
2076 char const *pattern;
2077 char const *format;
2079 const conversions[] = {
2080 { 0, "r", "0" },
2082 { 0, "mr", "z[1]" },
2083 { 0, "m+ri", "3[2]" },
2084 { 0, "m+rs", "3[2]" },
2085 { 0, "m+r+si", "4+5[2]" },
2086 { 0, "ms", "1" },
2087 { 0, "mi", "1" },
2088 { 0, "m+si", "2+3" },
2090 { 0, "mmr", "[z[2]]" },
2091 { 0, "mm+ri", "[4[3]]" },
2092 { 0, "mm+rs", "[4[3]]" },
2093 { 0, "mm+r+si", "[5+6[3]]" },
2094 { 0, "mms", "[[2]]" },
2095 { 0, "mmi", "[[2]]" },
2096 { 0, "mm+si", "[4[3]]" },
2098 { 0, "i", "#0" },
2099 { 0, "s", "#0" },
2100 { 0, "+si", "#1+2" },
2101 { 0, "l", "#0" },
2103 { 'l', "l", "0" },
2104 { 'd', "i", "0" },
2105 { 'd', "s", "0" },
2106 { 'd', "+si", "1+2" },
2107 { 'D', "i", "0" },
2108 { 'D', "s", "0" },
2109 { 'D', "+si", "1+2" },
2110 { 'x', "i", "#0" },
2111 { 'X', "i", "#0" },
2112 { 'm', "i", "#0" },
2113 { 'b', "i", "#0" },
2114 { 'p', "i", "0" },
2116 { 0, 0, 0 }
2119 /* This is in order according to the bitfield that pushm/popm use. */
2120 static char const *pushm_regs[] = {
2121 "fb", "sb", "a1", "a0", "r3", "r2", "r1", "r0"
2124 /* Implements PRINT_OPERAND. */
2125 void
2126 m32c_print_operand (FILE * file, rtx x, int code)
2128 int i, j, b;
2129 const char *comma;
2130 HOST_WIDE_INT ival;
2131 int unsigned_const = 0;
2133 /* Multiplies; constants are converted to sign-extended format but
2134 we need unsigned, so 'u' and 'U' tell us what size unsigned we
2135 need. */
2136 if (code == 'u')
2138 unsigned_const = 2;
2139 code = 0;
2141 if (code == 'U')
2143 unsigned_const = 1;
2144 code = 0;
2146 /* This one is only for debugging; you can put it in a pattern to
2147 force this error. */
2148 if (code == '!')
2150 fprintf (stderr, "dj: unreviewed pattern:");
2151 if (current_output_insn)
2152 debug_rtx (current_output_insn);
2153 gcc_unreachable ();
2155 /* PSImode operations are either .w or .l depending on the target. */
2156 if (code == '&')
2158 if (TARGET_A16)
2159 fprintf (file, "w");
2160 else
2161 fprintf (file, "l");
2162 return;
2164 /* Inverted conditionals. */
2165 if (code == 'C')
2167 switch (GET_CODE (x))
2169 case LE:
2170 fputs ("gt", file);
2171 break;
2172 case LEU:
2173 fputs ("gtu", file);
2174 break;
2175 case LT:
2176 fputs ("ge", file);
2177 break;
2178 case LTU:
2179 fputs ("geu", file);
2180 break;
2181 case GT:
2182 fputs ("le", file);
2183 break;
2184 case GTU:
2185 fputs ("leu", file);
2186 break;
2187 case GE:
2188 fputs ("lt", file);
2189 break;
2190 case GEU:
2191 fputs ("ltu", file);
2192 break;
2193 case NE:
2194 fputs ("eq", file);
2195 break;
2196 case EQ:
2197 fputs ("ne", file);
2198 break;
2199 default:
2200 gcc_unreachable ();
2202 return;
2204 /* Regular conditionals. */
2205 if (code == 'c')
2207 switch (GET_CODE (x))
2209 case LE:
2210 fputs ("le", file);
2211 break;
2212 case LEU:
2213 fputs ("leu", file);
2214 break;
2215 case LT:
2216 fputs ("lt", file);
2217 break;
2218 case LTU:
2219 fputs ("ltu", file);
2220 break;
2221 case GT:
2222 fputs ("gt", file);
2223 break;
2224 case GTU:
2225 fputs ("gtu", file);
2226 break;
2227 case GE:
2228 fputs ("ge", file);
2229 break;
2230 case GEU:
2231 fputs ("geu", file);
2232 break;
2233 case NE:
2234 fputs ("ne", file);
2235 break;
2236 case EQ:
2237 fputs ("eq", file);
2238 break;
2239 default:
2240 gcc_unreachable ();
2242 return;
2244 /* Used in negsi2 to do HImode ops on the two parts of an SImode
2245 operand. */
2246 if (code == 'h' && GET_MODE (x) == SImode)
2248 x = m32c_subreg (HImode, x, SImode, 0);
2249 code = 0;
2251 if (code == 'H' && GET_MODE (x) == SImode)
2253 x = m32c_subreg (HImode, x, SImode, 2);
2254 code = 0;
2256 /* 'x' and 'X' need to be ignored for non-immediates. */
2257 if ((code == 'x' || code == 'X') && GET_CODE (x) != CONST_INT)
2258 code = 0;
2260 encode_pattern (x);
2261 for (i = 0; conversions[i].pattern; i++)
2262 if (conversions[i].code == code
2263 && streq (conversions[i].pattern, pattern))
2265 for (j = 0; conversions[i].format[j]; j++)
2266 /* backslash quotes the next character in the output pattern. */
2267 if (conversions[i].format[j] == '\\')
2269 fputc (conversions[i].format[j + 1], file);
2270 j++;
2272 /* Digits in the output pattern indicate that the
2273 corresponding RTX is to be output at that point. */
2274 else if (ISDIGIT (conversions[i].format[j]))
2276 rtx r = patternr[conversions[i].format[j] - '0'];
2277 switch (GET_CODE (r))
2279 case REG:
2280 fprintf (file, "%s",
2281 reg_name_with_mode (REGNO (r), GET_MODE (r)));
2282 break;
2283 case CONST_INT:
2284 switch (code)
2286 case 'b':
2287 /* Bit position. */
2288 fprintf (file, "%d", (int) exact_log2 (INTVAL (r)));
2289 break;
2290 case 'x':
2291 /* Unsigned byte. */
2292 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2293 INTVAL (r) & 0xff);
2294 break;
2295 case 'X':
2296 /* Unsigned word. */
2297 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2298 INTVAL (r) & 0xffff);
2299 break;
2300 case 'p':
2301 /* pushm and popm encode a register set into a single byte. */
2302 comma = "";
2303 for (b = 7; b >= 0; b--)
2304 if (INTVAL (r) & (1 << b))
2306 fprintf (file, "%s%s", comma, pushm_regs[b]);
2307 comma = ",";
2309 break;
2310 case 'm':
2311 /* "Minus". Output -X */
2312 ival = (-INTVAL (r) & 0xffff);
2313 if (ival & 0x8000)
2314 ival = ival - 0x10000;
2315 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
2316 break;
2317 default:
2318 ival = INTVAL (r);
2319 if (conversions[i].format[j + 1] == '[' && ival < 0)
2321 /* We can simulate negative displacements by
2322 taking advantage of address space
2323 wrapping when the offset can span the
2324 entire address range. */
2325 rtx base =
2326 patternr[conversions[i].format[j + 2] - '0'];
2327 if (GET_CODE (base) == REG)
2328 switch (REGNO (base))
2330 case A0_REGNO:
2331 case A1_REGNO:
2332 if (TARGET_A24)
2333 ival = 0x1000000 + ival;
2334 else
2335 ival = 0x10000 + ival;
2336 break;
2337 case SB_REGNO:
2338 if (TARGET_A16)
2339 ival = 0x10000 + ival;
2340 break;
2343 else if (code == 'd' && ival < 0 && j == 0)
2344 /* The "mova" opcode is used to do addition by
2345 computing displacements, but again, we need
2346 displacements to be unsigned *if* they're
2347 the only component of the displacement
2348 (i.e. no "symbol-4" type displacement). */
2349 ival = (TARGET_A24 ? 0x1000000 : 0x10000) + ival;
2351 if (conversions[i].format[j] == '0')
2353 /* More conversions to unsigned. */
2354 if (unsigned_const == 2)
2355 ival &= 0xffff;
2356 if (unsigned_const == 1)
2357 ival &= 0xff;
2359 if (streq (conversions[i].pattern, "mi")
2360 || streq (conversions[i].pattern, "mmi"))
2362 /* Integers used as addresses are unsigned. */
2363 ival &= (TARGET_A24 ? 0xffffff : 0xffff);
2365 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
2366 break;
2368 break;
2369 case CONST_DOUBLE:
2370 /* We don't have const_double constants. If it
2371 happens, make it obvious. */
2372 fprintf (file, "[const_double 0x%lx]",
2373 (unsigned long) CONST_DOUBLE_HIGH (r));
2374 break;
2375 case SYMBOL_REF:
2376 assemble_name (file, XSTR (r, 0));
2377 break;
2378 case LABEL_REF:
2379 output_asm_label (r);
2380 break;
2381 default:
2382 fprintf (stderr, "don't know how to print this operand:");
2383 debug_rtx (r);
2384 gcc_unreachable ();
2387 else
2389 if (conversions[i].format[j] == 'z')
2391 /* Some addressing modes *must* have a displacement,
2392 so insert a zero here if needed. */
2393 int k;
2394 for (k = j + 1; conversions[i].format[k]; k++)
2395 if (ISDIGIT (conversions[i].format[k]))
2397 rtx reg = patternr[conversions[i].format[k] - '0'];
2398 if (GET_CODE (reg) == REG
2399 && (REGNO (reg) == SB_REGNO
2400 || REGNO (reg) == FB_REGNO
2401 || REGNO (reg) == SP_REGNO))
2402 fputc ('0', file);
2404 continue;
2406 /* Signed displacements off symbols need to have signs
2407 blended cleanly. */
2408 if (conversions[i].format[j] == '+'
2409 && (!code || code == 'I')
2410 && ISDIGIT (conversions[i].format[j + 1])
2411 && GET_CODE (patternr[conversions[i].format[j + 1] - '0'])
2412 == CONST_INT
2413 && INTVAL (patternr[conversions[i].format[j + 1] - '0']) <
2415 continue;
2416 fputc (conversions[i].format[j], file);
2418 break;
2420 if (!conversions[i].pattern)
2422 fprintf (stderr, "unconvertible operand %c `%s'", code ? code : '-',
2423 pattern);
2424 debug_rtx (x);
2425 fprintf (file, "[%c.%s]", code ? code : '-', pattern);
2428 return;
2431 /* Implements PRINT_OPERAND_PUNCT_VALID_P. See m32c_print_operand
2432 above for descriptions of what these do. */
2434 m32c_print_operand_punct_valid_p (int c)
2436 if (c == '&' || c == '!')
2437 return 1;
2438 return 0;
2441 /* Implements PRINT_OPERAND_ADDRESS. Nothing unusual here. */
2442 void
2443 m32c_print_operand_address (FILE * stream, rtx address)
2445 gcc_assert (GET_CODE (address) == MEM);
2446 m32c_print_operand (stream, XEXP (address, 0), 0);
2449 /* Implements ASM_OUTPUT_REG_PUSH. Control registers are pushed
2450 differently than general registers. */
2451 void
2452 m32c_output_reg_push (FILE * s, int regno)
2454 if (regno == FLG_REGNO)
2455 fprintf (s, "\tpushc\tflg\n");
2456 else
2457 fprintf (s, "\tpush.%c\t%s\n",
2458 " bwll"[reg_push_size (regno)], reg_names[regno]);
2461 /* Likewise for ASM_OUTPUT_REG_POP. */
2462 void
2463 m32c_output_reg_pop (FILE * s, int regno)
2465 if (regno == FLG_REGNO)
2466 fprintf (s, "\tpopc\tflg\n");
2467 else
2468 fprintf (s, "\tpop.%c\t%s\n",
2469 " bwll"[reg_push_size (regno)], reg_names[regno]);
2472 /* Defining target-specific uses of `__attribute__' */
2474 /* Used to simplify the logic below. Find the attributes wherever
2475 they may be. */
2476 #define M32C_ATTRIBUTES(decl) \
2477 (TYPE_P (decl)) ? TYPE_ATTRIBUTES (decl) \
2478 : DECL_ATTRIBUTES (decl) \
2479 ? (DECL_ATTRIBUTES (decl)) \
2480 : TYPE_ATTRIBUTES (TREE_TYPE (decl))
2482 /* Returns TRUE if the given tree has the "interrupt" attribute. */
2483 static int
2484 interrupt_p (tree node ATTRIBUTE_UNUSED)
2486 tree list = M32C_ATTRIBUTES (node);
2487 while (list)
2489 if (is_attribute_p ("interrupt", TREE_PURPOSE (list)))
2490 return 1;
2491 list = TREE_CHAIN (list);
2493 return 0;
2496 static tree
2497 interrupt_handler (tree * node ATTRIBUTE_UNUSED,
2498 tree name ATTRIBUTE_UNUSED,
2499 tree args ATTRIBUTE_UNUSED,
2500 int flags ATTRIBUTE_UNUSED,
2501 bool * no_add_attrs ATTRIBUTE_UNUSED)
2503 return NULL_TREE;
2506 #undef TARGET_ATTRIBUTE_TABLE
2507 #define TARGET_ATTRIBUTE_TABLE m32c_attribute_table
2508 static const struct attribute_spec m32c_attribute_table[] = {
2509 {"interrupt", 0, 0, false, false, false, interrupt_handler},
2510 {0, 0, 0, 0, 0, 0, 0}
2513 #undef TARGET_COMP_TYPE_ATTRIBUTES
2514 #define TARGET_COMP_TYPE_ATTRIBUTES m32c_comp_type_attributes
2515 static int
2516 m32c_comp_type_attributes (tree type1 ATTRIBUTE_UNUSED,
2517 tree type2 ATTRIBUTE_UNUSED)
2519 /* 0=incompatible 1=compatible 2=warning */
2520 return 1;
2523 #undef TARGET_INSERT_ATTRIBUTES
2524 #define TARGET_INSERT_ATTRIBUTES m32c_insert_attributes
2525 static void
2526 m32c_insert_attributes (tree node ATTRIBUTE_UNUSED,
2527 tree * attr_ptr ATTRIBUTE_UNUSED)
2529 /* Nothing to do here. */
2532 /* Predicates */
2534 /* Returns TRUE if we support a move between the first two operands.
2535 At the moment, we just want to discourage mem to mem moves until
2536 after reload, because reload has a hard time with our limited
2537 number of address registers, and we can get into a situation where
2538 we need three of them when we only have two. */
2539 bool
2540 m32c_mov_ok (rtx * operands, enum machine_mode mode ATTRIBUTE_UNUSED)
2542 rtx op0 = operands[0];
2543 rtx op1 = operands[1];
2545 if (TARGET_A24)
2546 return true;
2548 #define DEBUG_MOV_OK 0
2549 #if DEBUG_MOV_OK
2550 fprintf (stderr, "m32c_mov_ok %s\n", mode_name[mode]);
2551 debug_rtx (op0);
2552 debug_rtx (op1);
2553 #endif
2555 if (GET_CODE (op0) == SUBREG)
2556 op0 = XEXP (op0, 0);
2557 if (GET_CODE (op1) == SUBREG)
2558 op1 = XEXP (op1, 0);
2560 if (GET_CODE (op0) == MEM
2561 && GET_CODE (op1) == MEM
2562 && ! reload_completed)
2564 #if DEBUG_MOV_OK
2565 fprintf (stderr, " - no, mem to mem\n");
2566 #endif
2567 return false;
2570 #if DEBUG_MOV_OK
2571 fprintf (stderr, " - ok\n");
2572 #endif
2573 return true;
2576 /* Expanders */
2578 /* Subregs are non-orthogonal for us, because our registers are all
2579 different sizes. */
2580 static rtx
2581 m32c_subreg (enum machine_mode outer,
2582 rtx x, enum machine_mode inner, int byte)
2584 int r, nr = -1;
2586 /* Converting MEMs to different types that are the same size, we
2587 just rewrite them. */
2588 if (GET_CODE (x) == SUBREG
2589 && SUBREG_BYTE (x) == 0
2590 && GET_CODE (SUBREG_REG (x)) == MEM
2591 && (GET_MODE_SIZE (GET_MODE (x))
2592 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
2594 rtx oldx = x;
2595 x = gen_rtx_MEM (GET_MODE (x), XEXP (SUBREG_REG (x), 0));
2596 MEM_COPY_ATTRIBUTES (x, SUBREG_REG (oldx));
2599 /* Push/pop get done as smaller push/pops. */
2600 if (GET_CODE (x) == MEM
2601 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2602 || GET_CODE (XEXP (x, 0)) == POST_INC))
2603 return gen_rtx_MEM (outer, XEXP (x, 0));
2604 if (GET_CODE (x) == SUBREG
2605 && GET_CODE (XEXP (x, 0)) == MEM
2606 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == PRE_DEC
2607 || GET_CODE (XEXP (XEXP (x, 0), 0)) == POST_INC))
2608 return gen_rtx_MEM (outer, XEXP (XEXP (x, 0), 0));
2610 if (GET_CODE (x) != REG)
2611 return simplify_gen_subreg (outer, x, inner, byte);
2613 r = REGNO (x);
2614 if (r >= FIRST_PSEUDO_REGISTER || r == AP_REGNO)
2615 return simplify_gen_subreg (outer, x, inner, byte);
2617 if (IS_MEM_REGNO (r))
2618 return simplify_gen_subreg (outer, x, inner, byte);
2620 /* This is where the complexities of our register layout are
2621 described. */
2622 if (byte == 0)
2623 nr = r;
2624 else if (outer == HImode)
2626 if (r == R0_REGNO && byte == 2)
2627 nr = R2_REGNO;
2628 else if (r == R0_REGNO && byte == 4)
2629 nr = R1_REGNO;
2630 else if (r == R0_REGNO && byte == 6)
2631 nr = R3_REGNO;
2632 else if (r == R1_REGNO && byte == 2)
2633 nr = R3_REGNO;
2634 else if (r == A0_REGNO && byte == 2)
2635 nr = A1_REGNO;
2637 else if (outer == SImode)
2639 if (r == R0_REGNO && byte == 0)
2640 nr = R0_REGNO;
2641 else if (r == R0_REGNO && byte == 4)
2642 nr = R1_REGNO;
2644 if (nr == -1)
2646 fprintf (stderr, "m32c_subreg %s %s %d\n",
2647 mode_name[outer], mode_name[inner], byte);
2648 debug_rtx (x);
2649 gcc_unreachable ();
2651 return gen_rtx_REG (outer, nr);
2654 /* Used to emit move instructions. We split some moves,
2655 and avoid mem-mem moves. */
2657 m32c_prepare_move (rtx * operands, enum machine_mode mode)
2659 if (TARGET_A16 && mode == PSImode)
2660 return m32c_split_move (operands, mode, 1);
2661 if ((GET_CODE (operands[0]) == MEM)
2662 && (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY))
2664 rtx pmv = XEXP (operands[0], 0);
2665 rtx dest_reg = XEXP (pmv, 0);
2666 rtx dest_mod = XEXP (pmv, 1);
2668 emit_insn (gen_rtx_SET (Pmode, dest_reg, dest_mod));
2669 operands[0] = gen_rtx_MEM (mode, dest_reg);
2671 if (!no_new_pseudos && MEM_P (operands[0]) && MEM_P (operands[1]))
2672 operands[1] = copy_to_mode_reg (mode, operands[1]);
2673 return 0;
2676 #define DEBUG_SPLIT 0
2678 /* Returns TRUE if the given PSImode move should be split. We split
2679 for all r8c/m16c moves, since it doesn't support them, and for
2680 POP.L as we can only *push* SImode. */
2682 m32c_split_psi_p (rtx * operands)
2684 #if DEBUG_SPLIT
2685 fprintf (stderr, "\nm32c_split_psi_p\n");
2686 debug_rtx (operands[0]);
2687 debug_rtx (operands[1]);
2688 #endif
2689 if (TARGET_A16)
2691 #if DEBUG_SPLIT
2692 fprintf (stderr, "yes, A16\n");
2693 #endif
2694 return 1;
2696 if (GET_CODE (operands[1]) == MEM
2697 && GET_CODE (XEXP (operands[1], 0)) == POST_INC)
2699 #if DEBUG_SPLIT
2700 fprintf (stderr, "yes, pop.l\n");
2701 #endif
2702 return 1;
2704 #if DEBUG_SPLIT
2705 fprintf (stderr, "no, default\n");
2706 #endif
2707 return 0;
2710 /* Split the given move. SPLIT_ALL is 0 if splitting is optional
2711 (define_expand), 1 if it is not optional (define_insn_and_split),
2712 and 3 for define_split (alternate api). */
2714 m32c_split_move (rtx * operands, enum machine_mode mode, int split_all)
2716 rtx s[4], d[4];
2717 int parts, si, di, rev = 0;
2718 int rv = 0, opi = 2;
2719 enum machine_mode submode = HImode;
2720 rtx *ops, local_ops[10];
2722 /* define_split modifies the existing operands, but the other two
2723 emit new insns. OPS is where we store the operand pairs, which
2724 we emit later. */
2725 if (split_all == 3)
2726 ops = operands;
2727 else
2728 ops = local_ops;
2730 /* Else HImode. */
2731 if (mode == DImode)
2732 submode = SImode;
2734 /* Before splitting mem-mem moves, force one operand into a
2735 register. */
2736 if (!no_new_pseudos && MEM_P (operands[0]) && MEM_P (operands[1]))
2738 #if DEBUG0
2739 fprintf (stderr, "force_reg...\n");
2740 debug_rtx (operands[1]);
2741 #endif
2742 operands[1] = force_reg (mode, operands[1]);
2743 #if DEBUG0
2744 debug_rtx (operands[1]);
2745 #endif
2748 parts = 2;
2750 #if DEBUG_SPLIT
2751 fprintf (stderr, "\nsplit_move %d all=%d\n", no_new_pseudos, split_all);
2752 debug_rtx (operands[0]);
2753 debug_rtx (operands[1]);
2754 #endif
2756 /* Note that split_all is not used to select the api after this
2757 point, so it's safe to set it to 3 even with define_insn. */
2758 /* None of the chips can move SI operands to sp-relative addresses,
2759 so we always split those. */
2760 if (m32c_extra_constraint_p (operands[0], 'S', "Ss"))
2761 split_all = 3;
2763 /* We don't need to split these. */
2764 if (TARGET_A24
2765 && split_all != 3
2766 && (mode == SImode || mode == PSImode)
2767 && !(GET_CODE (operands[1]) == MEM
2768 && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
2769 return 0;
2771 /* First, enumerate the subregs we'll be dealing with. */
2772 for (si = 0; si < parts; si++)
2774 d[si] =
2775 m32c_subreg (submode, operands[0], mode,
2776 si * GET_MODE_SIZE (submode));
2777 s[si] =
2778 m32c_subreg (submode, operands[1], mode,
2779 si * GET_MODE_SIZE (submode));
2782 /* Split pushes by emitting a sequence of smaller pushes. */
2783 if (GET_CODE (d[0]) == MEM && GET_CODE (XEXP (d[0], 0)) == PRE_DEC)
2785 for (si = parts - 1; si >= 0; si--)
2787 ops[opi++] = gen_rtx_MEM (submode,
2788 gen_rtx_PRE_DEC (Pmode,
2789 gen_rtx_REG (Pmode,
2790 SP_REGNO)));
2791 ops[opi++] = s[si];
2794 rv = 1;
2796 /* Likewise for pops. */
2797 else if (GET_CODE (s[0]) == MEM && GET_CODE (XEXP (s[0], 0)) == POST_INC)
2799 for (di = 0; di < parts; di++)
2801 ops[opi++] = d[di];
2802 ops[opi++] = gen_rtx_MEM (submode,
2803 gen_rtx_POST_INC (Pmode,
2804 gen_rtx_REG (Pmode,
2805 SP_REGNO)));
2807 rv = 1;
2809 else if (split_all)
2811 /* if d[di] == s[si] for any di < si, we'll early clobber. */
2812 for (di = 0; di < parts - 1; di++)
2813 for (si = di + 1; si < parts; si++)
2814 if (reg_mentioned_p (d[di], s[si]))
2815 rev = 1;
2817 if (rev)
2818 for (si = 0; si < parts; si++)
2820 ops[opi++] = d[si];
2821 ops[opi++] = s[si];
2823 else
2824 for (si = parts - 1; si >= 0; si--)
2826 ops[opi++] = d[si];
2827 ops[opi++] = s[si];
2829 rv = 1;
2831 /* Now emit any moves we may have accumulated. */
2832 if (rv && split_all != 3)
2834 int i;
2835 for (i = 2; i < opi; i += 2)
2836 emit_move_insn (ops[i], ops[i + 1]);
2838 return rv;
2841 typedef rtx (*shift_gen_func)(rtx, rtx, rtx);
2843 static shift_gen_func
2844 shift_gen_func_for (int mode, int code)
2846 #define GFF(m,c,f) if (mode == m && code == c) return f
2847 GFF(QImode, ASHIFT, gen_ashlqi3_i);
2848 GFF(QImode, ASHIFTRT, gen_ashrqi3_i);
2849 GFF(QImode, LSHIFTRT, gen_lshrqi3_i);
2850 GFF(HImode, ASHIFT, gen_ashlhi3_i);
2851 GFF(HImode, ASHIFTRT, gen_ashrhi3_i);
2852 GFF(HImode, LSHIFTRT, gen_lshrhi3_i);
2853 GFF(PSImode, ASHIFT, gen_ashlpsi3_i);
2854 GFF(PSImode, ASHIFTRT, gen_ashrpsi3_i);
2855 GFF(PSImode, LSHIFTRT, gen_lshrpsi3_i);
2856 GFF(SImode, ASHIFT, TARGET_A16 ? gen_ashlsi3_16 : gen_ashlsi3_24);
2857 GFF(SImode, ASHIFTRT, TARGET_A16 ? gen_ashrsi3_16 : gen_ashrsi3_24);
2858 GFF(SImode, LSHIFTRT, TARGET_A16 ? gen_lshrsi3_16 : gen_lshrsi3_24);
2859 #undef GFF
2862 /* The m32c only has one shift, but it takes a signed count. GCC
2863 doesn't want this, so we fake it by negating any shift count when
2864 we're pretending to shift the other way. */
2866 m32c_prepare_shift (rtx * operands, int scale, int shift_code)
2868 enum machine_mode mode = GET_MODE (operands[0]);
2869 shift_gen_func func = shift_gen_func_for (mode, shift_code);
2870 rtx temp;
2872 if (GET_CODE (operands[2]) == CONST_INT)
2874 int maxc = TARGET_A24 && (mode == PSImode || mode == SImode) ? 32 : 8;
2875 int count = INTVAL (operands[2]) * scale;
2877 while (count > maxc)
2879 temp = gen_reg_rtx (mode);
2880 emit_insn (func (temp, operands[1], GEN_INT (maxc)));
2881 operands[1] = temp;
2882 count -= maxc;
2884 while (count < -maxc)
2886 temp = gen_reg_rtx (mode);
2887 emit_insn (func (temp, operands[1], GEN_INT (-maxc)));
2888 operands[1] = temp;
2889 count += maxc;
2891 emit_insn (func (operands[0], operands[1], GEN_INT (count)));
2892 return 1;
2895 temp = gen_reg_rtx (QImode);
2896 if (scale < 0)
2897 /* The pattern has a NEG that corresponds to this. */
2898 emit_move_insn (temp, gen_rtx_NEG (QImode, operands[2]));
2899 else if (TARGET_A16 && mode == SImode)
2900 /* We do this because the code below may modify this, we don't
2901 want to modify the origin of this value. */
2902 emit_move_insn (temp, operands[2]);
2903 else
2904 /* We'll only use it for the shift, no point emitting a move. */
2905 temp = operands[2];
2908 if (TARGET_A16 && mode == SImode)
2910 /* The m16c has a limit of -16..16 for SI shifts, even when the
2911 shift count is in a register. Since there are so many targets
2912 of these shifts, it's better to expand the RTL here than to
2913 call a helper function.
2915 The resulting code looks something like this:
2917 cmp.b r1h,-16
2918 jge.b 1f
2919 shl.l -16,dest
2920 add.b r1h,16
2921 1f: cmp.b r1h,16
2922 jle.b 1f
2923 shl.l 16,dest
2924 sub.b r1h,16
2925 1f: shl.l r1h,dest
2927 We take advantage of the fact that "negative" shifts are
2928 undefined to skip one of the comparisons. */
2930 rtx count;
2931 rtx label, lref, insn;
2933 count = temp;
2934 label = gen_label_rtx ();
2935 lref = gen_rtx_LABEL_REF (VOIDmode, label);
2936 LABEL_NUSES (label) ++;
2938 if (shift_code == ASHIFT)
2940 /* This is a left shift. We only need check positive counts. */
2941 emit_jump_insn (gen_cbranchqi4 (gen_rtx_LE (VOIDmode, 0, 0),
2942 count, GEN_INT (16), label));
2943 emit_insn (func (operands[1], operands[1], GEN_INT (8)));
2944 emit_insn (func (operands[1], operands[1], GEN_INT (8)));
2945 insn = emit_insn (gen_addqi3 (count, count, GEN_INT (-16)));
2946 emit_label_after (label, insn);
2948 else
2950 /* This is a right shift. We only need check negative counts. */
2951 emit_jump_insn (gen_cbranchqi4 (gen_rtx_GE (VOIDmode, 0, 0),
2952 count, GEN_INT (-16), label));
2953 emit_insn (func (operands[1], operands[1], GEN_INT (-8)));
2954 emit_insn (func (operands[1], operands[1], GEN_INT (-8)));
2955 insn = emit_insn (gen_addqi3 (count, count, GEN_INT (16)));
2956 emit_label_after (label, insn);
2961 operands[2] = temp;
2962 return 0;
2965 /* The m32c has a limited range of operations that work on PSImode
2966 values; we have to expand to SI, do the math, and truncate back to
2967 PSI. Yes, this is expensive, but hopefully gcc will learn to avoid
2968 those cases. */
2969 void
2970 m32c_expand_neg_mulpsi3 (rtx * operands)
2972 /* operands: a = b * i */
2973 rtx temp1; /* b as SI */
2974 rtx temp2; /* -b as SI */
2975 rtx temp3; /* -b as PSI */
2976 rtx scale;
2978 temp1 = gen_reg_rtx (SImode);
2979 temp2 = gen_reg_rtx (SImode);
2980 temp3 = gen_reg_rtx (PSImode);
2981 scale = GEN_INT (- INTVAL (operands[2]));
2983 emit_insn (gen_zero_extendpsisi2 (temp1, operands[1]));
2984 emit_insn (gen_negsi2 (temp2, temp1));
2985 emit_insn (gen_truncsipsi2 (temp3, temp2));
2986 emit_insn (gen_mulpsi3 (operands[0], temp3, scale));
2989 /* Pattern Output Functions */
2991 /* Returns TRUE if the current function is a leaf, and thus we can
2992 determine which registers an interrupt function really needs to
2993 save. The logic below is mostly about finding the insn sequence
2994 that's the function, versus any sequence that might be open for the
2995 current insn. */
2996 static int
2997 m32c_leaf_function_p (void)
2999 rtx saved_first, saved_last;
3000 struct sequence_stack *seq;
3001 int rv;
3003 saved_first = cfun->emit->x_first_insn;
3004 saved_last = cfun->emit->x_last_insn;
3005 for (seq = cfun->emit->sequence_stack; seq && seq->next; seq = seq->next)
3007 if (seq)
3009 cfun->emit->x_first_insn = seq->first;
3010 cfun->emit->x_last_insn = seq->last;
3013 rv = leaf_function_p ();
3015 cfun->emit->x_first_insn = saved_first;
3016 cfun->emit->x_last_insn = saved_last;
3017 return rv;
3020 /* Returns TRUE if the current function needs to use the ENTER/EXIT
3021 opcodes. If the function doesn't need the frame base or stack
3022 pointer, it can use the simpler RTS opcode. */
3023 static bool
3024 m32c_function_needs_enter (void)
3026 rtx insn;
3027 struct sequence_stack *seq;
3028 rtx sp = gen_rtx_REG (Pmode, SP_REGNO);
3029 rtx fb = gen_rtx_REG (Pmode, FB_REGNO);
3031 insn = get_insns ();
3032 for (seq = cfun->emit->sequence_stack;
3033 seq;
3034 insn = seq->first, seq = seq->next);
3036 while (insn)
3038 if (reg_mentioned_p (sp, insn))
3039 return true;
3040 if (reg_mentioned_p (fb, insn))
3041 return true;
3042 insn = NEXT_INSN (insn);
3044 return false;
3047 /* Mark all the subexpressions of the PARALLEL rtx PAR as
3048 frame-related. Return PAR.
3050 dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
3051 PARALLEL rtx other than the first if they do not have the
3052 FRAME_RELATED flag set on them. So this function is handy for
3053 marking up 'enter' instructions. */
3054 static rtx
3055 m32c_all_frame_related (rtx par)
3057 int len = XVECLEN (par, 0);
3058 int i;
3060 for (i = 0; i < len; i++)
3061 F (XVECEXP (par, 0, i));
3063 return par;
3066 /* Emits the prologue. See the frame layout comment earlier in this
3067 file. We can reserve up to 256 bytes with the ENTER opcode, beyond
3068 that we manually update sp. */
3069 void
3070 m32c_emit_prologue (void)
3072 int frame_size, extra_frame_size = 0, reg_save_size;
3073 int complex_prologue = 0;
3075 cfun->machine->is_leaf = m32c_leaf_function_p ();
3076 if (interrupt_p (cfun->decl))
3078 cfun->machine->is_interrupt = 1;
3079 complex_prologue = 1;
3082 reg_save_size = m32c_pushm_popm (PP_justcount);
3084 if (interrupt_p (cfun->decl))
3085 emit_insn (gen_pushm (GEN_INT (cfun->machine->intr_pushm)));
3087 frame_size =
3088 m32c_initial_elimination_offset (FB_REGNO, SP_REGNO) - reg_save_size;
3089 if (frame_size == 0
3090 && !cfun->machine->is_interrupt
3091 && !m32c_function_needs_enter ())
3092 cfun->machine->use_rts = 1;
3094 if (frame_size > 254)
3096 extra_frame_size = frame_size - 254;
3097 frame_size = 254;
3099 if (cfun->machine->use_rts == 0)
3100 F (emit_insn (m32c_all_frame_related
3101 (TARGET_A16
3102 ? gen_prologue_enter_16 (GEN_INT (frame_size))
3103 : gen_prologue_enter_24 (GEN_INT (frame_size)))));
3105 if (extra_frame_size)
3107 complex_prologue = 1;
3108 if (TARGET_A16)
3109 F (emit_insn (gen_addhi3 (gen_rtx_REG (HImode, SP_REGNO),
3110 gen_rtx_REG (HImode, SP_REGNO),
3111 GEN_INT (-extra_frame_size))));
3112 else
3113 F (emit_insn (gen_addpsi3 (gen_rtx_REG (PSImode, SP_REGNO),
3114 gen_rtx_REG (PSImode, SP_REGNO),
3115 GEN_INT (-extra_frame_size))));
3118 complex_prologue += m32c_pushm_popm (PP_pushm);
3120 /* This just emits a comment into the .s file for debugging. */
3121 if (complex_prologue)
3122 emit_insn (gen_prologue_end ());
3125 /* Likewise, for the epilogue. The only exception is that, for
3126 interrupts, we must manually unwind the frame as the REIT opcode
3127 doesn't do that. */
3128 void
3129 m32c_emit_epilogue (void)
3131 /* This just emits a comment into the .s file for debugging. */
3132 if (m32c_pushm_popm (PP_justcount) > 0 || cfun->machine->is_interrupt)
3133 emit_insn (gen_epilogue_start ());
3135 m32c_pushm_popm (PP_popm);
3137 if (cfun->machine->is_interrupt)
3139 enum machine_mode spmode = TARGET_A16 ? HImode : PSImode;
3141 emit_move_insn (gen_rtx_REG (spmode, A0_REGNO),
3142 gen_rtx_REG (spmode, FP_REGNO));
3143 emit_move_insn (gen_rtx_REG (spmode, SP_REGNO),
3144 gen_rtx_REG (spmode, A0_REGNO));
3145 if (TARGET_A16)
3146 emit_insn (gen_pophi_16 (gen_rtx_REG (HImode, FP_REGNO)));
3147 else
3148 emit_insn (gen_poppsi (gen_rtx_REG (PSImode, FP_REGNO)));
3149 emit_insn (gen_popm (GEN_INT (cfun->machine->intr_pushm)));
3150 emit_jump_insn (gen_epilogue_reit (GEN_INT (TARGET_A16 ? 4 : 6)));
3152 else if (cfun->machine->use_rts)
3153 emit_jump_insn (gen_epilogue_rts ());
3154 else
3155 emit_jump_insn (gen_epilogue_exitd (GEN_INT (TARGET_A16 ? 2 : 4)));
3156 emit_barrier ();
3159 void
3160 m32c_emit_eh_epilogue (rtx ret_addr)
3162 /* R0[R2] has the stack adjustment. R1[R3] has the address to
3163 return to. We have to fudge the stack, pop everything, pop SP
3164 (fudged), and return (fudged). This is actually easier to do in
3165 assembler, so punt to libgcc. */
3166 emit_jump_insn (gen_eh_epilogue (ret_addr, cfun->machine->eh_stack_adjust));
3167 /* emit_insn (gen_rtx_CLOBBER (HImode, gen_rtx_REG (HImode, R0L_REGNO))); */
3168 emit_barrier ();
3171 /* The Global `targetm' Variable. */
3173 struct gcc_target targetm = TARGET_INITIALIZER;
3175 #include "gt-m32c.h"